Actual source code: matproduct.c
1: /*
2: Routines for matrix products. Calling procedure:
4: MatProductCreate(A,B,C,&D); or MatProductCreateWithMat(A,B,C,D)
5: MatProductSetType(D, MATPRODUCT_AB/AtB/ABt/PtAP/RARt/ABC)
6: MatProductSetAlgorithm(D, alg)
7: MatProductSetFill(D,fill)
8: MatProductSetFromOptions(D)
9: -> MatProductSetFromOptions_Private(D)
10: # Check matrix global sizes
11: if the matrices have the same setfromoptions routine, use it
12: if not, try:
13: -> Query MatProductSetFromOptions_Atype_Btype_Ctype_C(D) from A, B and C (in order)
14: if found -> run the specific setup that must set the symbolic operation (these callbacks should never fail)
15: if callback not found or no symbolic operation set
16: -> Query MatProductSetFromOptions_anytype_C(D) from A, B and C (in order) (e.g, matrices may have inner matrices like MATTRANSPOSEVIRTUAL)
17: if dispatch found but combination still not present do
18: -> check if B is dense and product type AtB or AB -> if true, basic looping of dense columns
19: -> check if triple product (PtAP, RARt or ABC) -> if true, set the Basic routines
21: # The setfromoptions calls MatProductSetFromOptions_Atype_Btype_Ctype should
22: # Check matrix local sizes for mpi matrices
23: # Set default algorithm
24: # Get runtime option
25: # Set D->ops->productsymbolic = MatProductSymbolic_productype_Atype_Btype_Ctype if found
27: MatProductSymbolic(D)
28: # Call MatProductSymbolic_productype_Atype_Btype_Ctype()
29: the callback must set the numeric phase D->ops->productnumeric = MatProductNumeric_productype_Atype_Btype_Ctype
31: MatProductNumeric(D)
32: # Call the numeric phase
34: # The symbolic phases are allowed to set extra data structures and attach those to the product
35: # this additional data can be reused between multiple numeric phases with the same matrices
36: # if not needed, call
37: MatProductClear(D)
38: */
40: #include <petsc/private/matimpl.h>
42: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_SeqAIJ(Mat);
43: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_MPIAIJ(Mat);
45: const char *const MatProductTypes[] = {"UNSPECIFIED", "AB", "AtB", "ABt", "PtAP", "RARt", "ABC"};
47: /* these are basic implementations relying on the old function pointers
48: * they are dangerous and should be removed in the future */
49: static PetscErrorCode MatProductNumeric_PtAP_Unsafe(Mat C)
50: {
51: Mat_Product *product = C->product;
52: Mat P = product->B, AP = product->Dwork;
54: PetscFunctionBegin;
55: /* AP = A*P */
56: PetscCall(MatProductNumeric(AP));
57: /* C = P^T*AP */
58: product->type = MATPRODUCT_AtB;
59: PetscCall((*C->ops->transposematmultnumeric)(P, AP, C));
60: product->type = MATPRODUCT_PtAP;
61: PetscFunctionReturn(PETSC_SUCCESS);
62: }
64: static PetscErrorCode MatProductSymbolic_PtAP_Unsafe(Mat C)
65: {
66: Mat_Product *product = C->product;
67: Mat A = product->A, P = product->B, AP;
68: PetscReal fill = product->fill;
70: PetscFunctionBegin;
71: PetscCall(PetscInfo(C, "for A %s, P %s is used\n", ((PetscObject)product->A)->type_name, ((PetscObject)product->B)->type_name));
72: /* AP = A*P */
73: PetscCall(MatProductCreate(A, P, NULL, &AP));
74: PetscCall(MatProductSetType(AP, MATPRODUCT_AB));
75: PetscCall(MatProductSetAlgorithm(AP, MATPRODUCTALGORITHMDEFAULT));
76: PetscCall(MatProductSetFill(AP, fill));
77: PetscCall(MatProductSetFromOptions(AP));
78: PetscCall(MatProductSymbolic(AP));
80: /* C = P^T*AP */
81: PetscCall(MatProductSetType(C, MATPRODUCT_AtB));
82: PetscCall(MatProductSetAlgorithm(C, MATPRODUCTALGORITHMDEFAULT));
83: product->A = P;
84: product->B = AP;
85: PetscCall(MatProductSetFromOptions(C));
86: PetscCall(MatProductSymbolic(C));
88: /* resume user's original input matrix setting for A and B */
89: product->type = MATPRODUCT_PtAP;
90: product->A = A;
91: product->B = P;
92: product->Dwork = AP;
94: C->ops->productnumeric = MatProductNumeric_PtAP_Unsafe;
95: PetscFunctionReturn(PETSC_SUCCESS);
96: }
98: static PetscErrorCode MatProductNumeric_RARt_Unsafe(Mat C)
99: {
100: Mat_Product *product = C->product;
101: Mat R = product->B, RA = product->Dwork;
103: PetscFunctionBegin;
104: /* RA = R*A */
105: PetscCall(MatProductNumeric(RA));
106: /* C = RA*R^T */
107: product->type = MATPRODUCT_ABt;
108: PetscCall((*C->ops->mattransposemultnumeric)(RA, R, C));
109: product->type = MATPRODUCT_RARt;
110: PetscFunctionReturn(PETSC_SUCCESS);
111: }
113: static PetscErrorCode MatProductSymbolic_RARt_Unsafe(Mat C)
114: {
115: Mat_Product *product = C->product;
116: Mat A = product->A, R = product->B, RA;
117: PetscReal fill = product->fill;
119: PetscFunctionBegin;
120: PetscCall(PetscInfo(C, "for A %s, R %s is used\n", ((PetscObject)product->A)->type_name, ((PetscObject)product->B)->type_name));
121: /* RA = R*A */
122: PetscCall(MatProductCreate(R, A, NULL, &RA));
123: PetscCall(MatProductSetType(RA, MATPRODUCT_AB));
124: PetscCall(MatProductSetAlgorithm(RA, MATPRODUCTALGORITHMDEFAULT));
125: PetscCall(MatProductSetFill(RA, fill));
126: PetscCall(MatProductSetFromOptions(RA));
127: PetscCall(MatProductSymbolic(RA));
129: /* C = RA*R^T */
130: PetscCall(MatProductSetType(C, MATPRODUCT_ABt));
131: PetscCall(MatProductSetAlgorithm(C, MATPRODUCTALGORITHMDEFAULT));
132: product->A = RA;
133: PetscCall(MatProductSetFromOptions(C));
134: PetscCall(MatProductSymbolic(C));
136: /* resume user's original input matrix setting for A */
137: product->type = MATPRODUCT_RARt;
138: product->A = A;
139: product->Dwork = RA; /* save here so it will be destroyed with product C */
140: C->ops->productnumeric = MatProductNumeric_RARt_Unsafe;
141: PetscFunctionReturn(PETSC_SUCCESS);
142: }
144: static PetscErrorCode MatProductNumeric_ABC_Unsafe(Mat mat)
145: {
146: Mat_Product *product = mat->product;
147: Mat A = product->A, BC = product->Dwork;
149: PetscFunctionBegin;
150: /* Numeric BC = B*C */
151: PetscCall(MatProductNumeric(BC));
152: /* Numeric mat = A*BC */
153: product->type = MATPRODUCT_AB;
154: PetscCall((*mat->ops->matmultnumeric)(A, BC, mat));
155: product->type = MATPRODUCT_ABC;
156: PetscFunctionReturn(PETSC_SUCCESS);
157: }
159: static PetscErrorCode MatProductSymbolic_ABC_Unsafe(Mat mat)
160: {
161: Mat_Product *product = mat->product;
162: Mat B = product->B, C = product->C, BC;
163: PetscReal fill = product->fill;
165: PetscFunctionBegin;
166: PetscCall(PetscInfo(mat, "for A %s, B %s, C %s is used\n", ((PetscObject)product->A)->type_name, ((PetscObject)product->B)->type_name, ((PetscObject)product->C)->type_name));
167: /* Symbolic BC = B*C */
168: PetscCall(MatProductCreate(B, C, NULL, &BC));
169: PetscCall(MatProductSetType(BC, MATPRODUCT_AB));
170: PetscCall(MatProductSetAlgorithm(BC, MATPRODUCTALGORITHMDEFAULT));
171: PetscCall(MatProductSetFill(BC, fill));
172: PetscCall(MatProductSetFromOptions(BC));
173: PetscCall(MatProductSymbolic(BC));
175: /* Symbolic mat = A*BC */
176: PetscCall(MatProductSetType(mat, MATPRODUCT_AB));
177: PetscCall(MatProductSetAlgorithm(mat, MATPRODUCTALGORITHMDEFAULT));
178: product->B = BC;
179: product->Dwork = BC;
180: PetscCall(MatProductSetFromOptions(mat));
181: PetscCall(MatProductSymbolic(mat));
183: /* resume user's original input matrix setting for B */
184: product->type = MATPRODUCT_ABC;
185: product->B = B;
186: mat->ops->productnumeric = MatProductNumeric_ABC_Unsafe;
187: PetscFunctionReturn(PETSC_SUCCESS);
188: }
190: static PetscErrorCode MatProductSymbolic_Unsafe(Mat mat)
191: {
192: Mat_Product *product = mat->product;
194: PetscFunctionBegin;
195: switch (product->type) {
196: case MATPRODUCT_PtAP:
197: PetscCall(MatProductSymbolic_PtAP_Unsafe(mat));
198: break;
199: case MATPRODUCT_RARt:
200: PetscCall(MatProductSymbolic_RARt_Unsafe(mat));
201: break;
202: case MATPRODUCT_ABC:
203: PetscCall(MatProductSymbolic_ABC_Unsafe(mat));
204: break;
205: default:
206: SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "ProductType %s is not supported", MatProductTypes[product->type]);
207: }
208: PetscFunctionReturn(PETSC_SUCCESS);
209: }
211: /*@
212: MatProductReplaceMats - Replace the input matrices for the matrix-matrix product operation inside the computed matrix
214: Collective
216: Input Parameters:
217: + A - the matrix or `NULL` if not being replaced
218: . B - the matrix or `NULL` if not being replaced
219: . C - the matrix or `NULL` if not being replaced
220: - D - the matrix whose values are computed via a matrix-matrix product operation
222: Level: intermediate
224: Note:
225: To reuse the symbolic phase, the input matrices must have exactly the same data structure as the replaced one.
226: If the type of any of the input matrices is different than what was previously used, or their symmetry flag changed but
227: the symbolic phase took advantage of their symmetry, the product is cleared and `MatProductSetFromOptions()`
228: and `MatProductSymbolic()` are invoked again.
230: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreate()`, `MatProductSetFromOptions()`, `MatProductSymbolic()`, `MatProductClear()`
231: @*/
232: PetscErrorCode MatProductReplaceMats(Mat A, Mat B, Mat C, Mat D)
233: {
234: Mat_Product *product;
235: PetscBool flgA = PETSC_TRUE, flgB = PETSC_TRUE, flgC = PETSC_TRUE, isset, issym;
237: PetscFunctionBegin;
239: MatCheckProduct(D, 4);
240: product = D->product;
241: if (A) {
243: PetscCall(PetscObjectReference((PetscObject)A));
244: PetscCall(PetscObjectTypeCompare((PetscObject)product->A, ((PetscObject)A)->type_name, &flgA));
245: PetscCall(MatIsSymmetricKnown(A, &isset, &issym));
246: if (product->symbolic_used_the_fact_A_is_symmetric && isset && !issym) { /* symbolic was built around a symmetric A, but the new A is not anymore */
247: flgA = PETSC_FALSE;
248: product->symbolic_used_the_fact_A_is_symmetric = PETSC_FALSE; /* reinit */
249: }
250: PetscCall(MatDestroy(&product->A));
251: product->A = A;
252: }
253: if (B) {
255: PetscCall(PetscObjectReference((PetscObject)B));
256: PetscCall(PetscObjectTypeCompare((PetscObject)product->B, ((PetscObject)B)->type_name, &flgB));
257: PetscCall(MatIsSymmetricKnown(B, &isset, &issym));
258: if (product->symbolic_used_the_fact_B_is_symmetric && isset && !issym) {
259: flgB = PETSC_FALSE;
260: product->symbolic_used_the_fact_B_is_symmetric = PETSC_FALSE; /* reinit */
261: }
262: PetscCall(MatDestroy(&product->B));
263: product->B = B;
264: }
265: if (C) {
267: PetscCall(PetscObjectReference((PetscObject)C));
268: PetscCall(PetscObjectTypeCompare((PetscObject)product->C, ((PetscObject)C)->type_name, &flgC));
269: PetscCall(MatIsSymmetricKnown(C, &isset, &issym));
270: if (product->symbolic_used_the_fact_C_is_symmetric && isset && !issym) {
271: flgC = PETSC_FALSE;
272: product->symbolic_used_the_fact_C_is_symmetric = PETSC_FALSE; /* reinit */
273: }
274: PetscCall(MatDestroy(&product->C));
275: product->C = C;
276: }
277: /* Any of the replaced mats is of a different type, reset */
278: if (!flgA || !flgB || !flgC) {
279: if (D->product->destroy) PetscCall((*D->product->destroy)(&D->product->data));
280: D->product->destroy = NULL;
281: D->product->data = NULL;
282: if (D->ops->productnumeric || D->ops->productsymbolic) {
283: PetscCall(MatProductSetFromOptions(D));
284: PetscCall(MatProductSymbolic(D));
285: }
286: }
287: PetscFunctionReturn(PETSC_SUCCESS);
288: }
290: static PetscErrorCode MatProductNumeric_X_Dense(Mat C)
291: {
292: Mat_Product *product = C->product;
293: Mat A = product->A, B = product->B;
294: PetscInt k, K = B->cmap->N;
295: PetscBool t = PETSC_TRUE, iscuda = PETSC_FALSE;
296: PetscBool Bcpu = PETSC_TRUE, Ccpu = PETSC_TRUE;
297: char *Btype = NULL, *Ctype = NULL;
299: PetscFunctionBegin;
300: switch (product->type) {
301: case MATPRODUCT_AB:
302: t = PETSC_FALSE;
303: case MATPRODUCT_AtB:
304: break;
305: default:
306: SETERRQ(PetscObjectComm((PetscObject)C), PETSC_ERR_SUP, "MatProductNumeric type %s not supported for %s and %s matrices", MatProductTypes[product->type], ((PetscObject)A)->type_name, ((PetscObject)B)->type_name);
307: }
308: if (PetscDefined(HAVE_CUDA)) {
309: VecType vtype;
311: PetscCall(MatGetVecType(A, &vtype));
312: PetscCall(PetscStrcmp(vtype, VECCUDA, &iscuda));
313: if (!iscuda) PetscCall(PetscStrcmp(vtype, VECSEQCUDA, &iscuda));
314: if (!iscuda) PetscCall(PetscStrcmp(vtype, VECMPICUDA, &iscuda));
315: if (iscuda) { /* Make sure we have up-to-date data on the GPU */
316: PetscCall(PetscStrallocpy(((PetscObject)B)->type_name, &Btype));
317: PetscCall(PetscStrallocpy(((PetscObject)C)->type_name, &Ctype));
318: PetscCall(MatConvert(B, MATDENSECUDA, MAT_INPLACE_MATRIX, &B));
319: if (!C->assembled) { /* need to flag the matrix as assembled, otherwise MatConvert will complain */
320: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
321: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
322: }
323: PetscCall(MatConvert(C, MATDENSECUDA, MAT_INPLACE_MATRIX, &C));
324: } else { /* Make sure we have up-to-date data on the CPU */
325: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_VIENNACL)
326: Bcpu = B->boundtocpu;
327: Ccpu = C->boundtocpu;
328: #endif
329: PetscCall(MatBindToCPU(B, PETSC_TRUE));
330: PetscCall(MatBindToCPU(C, PETSC_TRUE));
331: }
332: }
333: for (k = 0; k < K; k++) {
334: Vec x, y;
336: PetscCall(MatDenseGetColumnVecRead(B, k, &x));
337: PetscCall(MatDenseGetColumnVecWrite(C, k, &y));
338: if (t) {
339: PetscCall(MatMultTranspose(A, x, y));
340: } else {
341: PetscCall(MatMult(A, x, y));
342: }
343: PetscCall(MatDenseRestoreColumnVecRead(B, k, &x));
344: PetscCall(MatDenseRestoreColumnVecWrite(C, k, &y));
345: }
346: if (PetscDefined(HAVE_CUDA)) {
347: if (iscuda) {
348: PetscCall(MatConvert(B, Btype, MAT_INPLACE_MATRIX, &B));
349: PetscCall(MatConvert(C, Ctype, MAT_INPLACE_MATRIX, &C));
350: } else {
351: PetscCall(MatBindToCPU(B, Bcpu));
352: PetscCall(MatBindToCPU(C, Ccpu));
353: }
354: }
355: PetscCall(PetscFree(Btype));
356: PetscCall(PetscFree(Ctype));
357: PetscFunctionReturn(PETSC_SUCCESS);
358: }
360: static PetscErrorCode MatProductSymbolic_X_Dense(Mat C)
361: {
362: Mat_Product *product = C->product;
363: Mat A = product->A, B = product->B;
364: PetscBool isdense;
366: PetscFunctionBegin;
367: switch (product->type) {
368: case MATPRODUCT_AB:
369: PetscCall(MatSetSizes(C, A->rmap->n, B->cmap->n, A->rmap->N, B->cmap->N));
370: break;
371: case MATPRODUCT_AtB:
372: PetscCall(MatSetSizes(C, A->cmap->n, B->cmap->n, A->cmap->N, B->cmap->N));
373: break;
374: default:
375: SETERRQ(PetscObjectComm((PetscObject)C), PETSC_ERR_SUP, "MatProductSymbolic type %s not supported for %s and %s matrices", MatProductTypes[product->type], ((PetscObject)A)->type_name, ((PetscObject)B)->type_name);
376: }
377: PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)C, &isdense, MATSEQDENSE, MATMPIDENSE, ""));
378: if (!isdense) {
379: PetscCall(MatSetType(C, ((PetscObject)B)->type_name));
380: PetscCall(MatSetVecType(C, B->defaultvectype));
381: /* If matrix type of C was not set or not dense, we need to reset the pointer */
382: C->ops->productsymbolic = MatProductSymbolic_X_Dense;
383: }
384: C->ops->productnumeric = MatProductNumeric_X_Dense;
385: PetscCall(MatSetUp(C));
386: PetscFunctionReturn(PETSC_SUCCESS);
387: }
389: /* a single driver to query the dispatching */
390: static PetscErrorCode MatProductSetFromOptions_Private(Mat mat)
391: {
392: Mat_Product *product = mat->product;
393: PetscInt Am, An, Bm, Bn, Cm, Cn;
394: Mat A = product->A, B = product->B, C = product->C;
395: const char *const Bnames[] = {"B", "R", "P"};
396: const char *bname;
397: PetscErrorCode (*fA)(Mat);
398: PetscErrorCode (*fB)(Mat);
399: PetscErrorCode (*fC)(Mat);
400: PetscErrorCode (*f)(Mat) = NULL;
402: PetscFunctionBegin;
403: mat->ops->productsymbolic = NULL;
404: mat->ops->productnumeric = NULL;
405: if (product->type == MATPRODUCT_UNSPECIFIED) PetscFunctionReturn(PETSC_SUCCESS);
406: PetscCheck(A, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing A mat");
407: PetscCheck(B, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing B mat");
408: PetscCheck(product->type != MATPRODUCT_ABC || C, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing C mat");
409: if (product->type != MATPRODUCT_ABC) C = NULL; /* do not use C if not needed */
410: if (product->type == MATPRODUCT_RARt) bname = Bnames[1];
411: else if (product->type == MATPRODUCT_PtAP) bname = Bnames[2];
412: else bname = Bnames[0];
414: /* Check matrices sizes */
415: Am = A->rmap->N;
416: An = A->cmap->N;
417: Bm = B->rmap->N;
418: Bn = B->cmap->N;
419: Cm = C ? C->rmap->N : 0;
420: Cn = C ? C->cmap->N : 0;
421: if (product->type == MATPRODUCT_RARt || product->type == MATPRODUCT_ABt) {
422: PetscInt t = Bn;
423: Bn = Bm;
424: Bm = t;
425: }
426: if (product->type == MATPRODUCT_AtB) An = Am;
428: PetscCheck(An == Bm, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_SIZ, "Matrix dimensions of A and %s are incompatible for MatProductType %s: A %" PetscInt_FMT "x%" PetscInt_FMT ", %s %" PetscInt_FMT "x%" PetscInt_FMT, bname,
429: MatProductTypes[product->type], A->rmap->N, A->cmap->N, bname, B->rmap->N, B->cmap->N);
430: PetscCheck(!Cm || Cm == Bn, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_SIZ, "Matrix dimensions of B and C are incompatible for MatProductType %s: B %" PetscInt_FMT "x%" PetscInt_FMT ", C %" PetscInt_FMT "x%" PetscInt_FMT,
431: MatProductTypes[product->type], B->rmap->N, B->cmap->N, Cm, Cn);
433: /* graph products use the host CSR structure, including for AIJ device types */
434: if (mat->structure_only && (product->type == MATPRODUCT_AB || product->type == MATPRODUCT_AtB)) {
435: PetscBool aseq, bseq, ampi, bmpi;
437: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATSEQAIJ, &aseq));
438: PetscCall(PetscObjectBaseTypeCompare((PetscObject)B, MATSEQAIJ, &bseq));
439: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIAIJ, &i));
440: PetscCall(PetscObjectBaseTypeCompare((PetscObject)B, MATMPIAIJ, &bmpi));
441: if (aseq && bseq) {
442: PetscCall(MatProductSetFromOptions_SeqAIJ(mat));
443: PetscFunctionReturn(PETSC_SUCCESS);
444: } else if (ampi && bmpi) {
445: PetscCall(MatProductSetFromOptions_MPIAIJ(mat));
446: PetscFunctionReturn(PETSC_SUCCESS);
447: }
448: }
450: fA = A->ops->productsetfromoptions;
451: fB = B->ops->productsetfromoptions;
452: fC = C ? C->ops->productsetfromoptions : fA;
453: if (C) {
454: PetscCall(PetscInfo(mat, "MatProductType %s for A %s, %s %s, C %s\n", MatProductTypes[product->type], ((PetscObject)A)->type_name, bname, ((PetscObject)B)->type_name, ((PetscObject)C)->type_name));
455: } else {
456: PetscCall(PetscInfo(mat, "MatProductType %s for A %s, %s %s\n", MatProductTypes[product->type], ((PetscObject)A)->type_name, bname, ((PetscObject)B)->type_name));
457: }
458: if (fA == fB && fA == fC && fA) {
459: PetscCall(PetscInfo(mat, " matching op\n"));
460: PetscCall((*fA)(mat));
461: }
462: /* We may have found f but it did not succeed */
463: if (!mat->ops->productsymbolic) { /* query MatProductSetFromOptions_Atype_Btype_Ctype */
464: char mtypes[256];
465: PetscCall(PetscStrncpy(mtypes, "MatProductSetFromOptions_", sizeof(mtypes)));
466: PetscCall(PetscStrlcat(mtypes, ((PetscObject)A)->type_name, sizeof(mtypes)));
467: PetscCall(PetscStrlcat(mtypes, "_", sizeof(mtypes)));
468: PetscCall(PetscStrlcat(mtypes, ((PetscObject)B)->type_name, sizeof(mtypes)));
469: if (C) {
470: PetscCall(PetscStrlcat(mtypes, "_", sizeof(mtypes)));
471: PetscCall(PetscStrlcat(mtypes, ((PetscObject)C)->type_name, sizeof(mtypes)));
472: }
473: PetscCall(PetscStrlcat(mtypes, "_C", sizeof(mtypes)));
474: #if defined(__clang__)
475: PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN("-Wformat-pedantic")
476: #elif defined(__GNUC__) || defined(__GNUG__)
477: PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN("-Wformat")
478: #endif
479: PetscCall(PetscObjectQueryFunction((PetscObject)A, mtypes, &f));
480: PetscCall(PetscInfo(mat, " querying %s from A? %p\n", mtypes, f));
481: if (!f) {
482: PetscCall(PetscObjectQueryFunction((PetscObject)B, mtypes, &f));
483: PetscCall(PetscInfo(mat, " querying %s from %s? %p\n", mtypes, bname, f));
484: }
485: if (!f && C) {
486: PetscCall(PetscObjectQueryFunction((PetscObject)C, mtypes, &f));
487: PetscCall(PetscInfo(mat, " querying %s from C? %p\n", mtypes, f));
488: }
489: if (f) PetscCall((*f)(mat));
491: /* We may have found f but it did not succeed */
492: /* some matrices (i.e. MATTRANSPOSEVIRTUAL, MATSHELL constructed from MatConvert), knows what to do with their inner matrices */
493: if (!mat->ops->productsymbolic) {
494: PetscCall(PetscStrncpy(mtypes, "MatProductSetFromOptions_anytype_C", sizeof(mtypes)));
495: PetscCall(PetscObjectQueryFunction((PetscObject)A, mtypes, &f));
496: PetscCall(PetscInfo(mat, " querying %s from A? %p\n", mtypes, f));
497: if (!f) {
498: PetscCall(PetscObjectQueryFunction((PetscObject)B, mtypes, &f));
499: PetscCall(PetscInfo(mat, " querying %s from %s? %p\n", mtypes, bname, f));
500: }
501: if (!f && C) {
502: PetscCall(PetscObjectQueryFunction((PetscObject)C, mtypes, &f));
503: PetscCall(PetscInfo(mat, " querying %s from C? %p\n", mtypes, f));
504: }
505: }
506: if (f) PetscCall((*f)(mat));
507: }
508: PETSC_PRAGMA_DIAGNOSTIC_IGNORED_END()
509: /* We may have found f but it did not succeed */
510: if (!mat->ops->productsymbolic) {
511: /* we can still compute the product if B is of type dense */
512: if (product->type == MATPRODUCT_AB || product->type == MATPRODUCT_AtB) {
513: PetscBool isdense;
515: PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)B, &isdense, MATSEQDENSE, MATMPIDENSE, ""));
516: if (isdense) {
517: mat->ops->productsymbolic = MatProductSymbolic_X_Dense;
518: PetscCall(PetscInfo(mat, " using basic looping over columns of a dense matrix\n"));
519: }
520: } else if (product->type != MATPRODUCT_ABt) { /* use MatProductSymbolic/Numeric_Unsafe() for triple products only */
521: /*
522: TODO: this should be changed to a proper setfromoptions, not setting the symbolic pointer here, because we do not know if
523: the combination will succeed. In order to be sure, we need MatProductGetProductType to return the type of the result
524: before computing the symbolic phase
525: */
526: PetscCall(PetscInfo(mat, " symbolic product not supported, using MatProductSymbolic_Unsafe() implementation\n"));
527: mat->ops->productsymbolic = MatProductSymbolic_Unsafe;
528: }
529: }
530: if (!mat->ops->productsymbolic) PetscCall(PetscInfo(mat, " symbolic product is not supported\n"));
531: PetscFunctionReturn(PETSC_SUCCESS);
532: }
534: /*@
535: MatProductSetFromOptions - Sets the options for the computation of a matrix-matrix product operation where the type,
536: the algorithm etc are determined from the options database.
538: Logically Collective
540: Input Parameter:
541: . mat - the matrix whose values are computed via a matrix-matrix product operation
543: Options Database Keys:
544: + -mat_product_clear - Clear intermediate data structures after `MatProductNumeric()` has been called
545: . -mat_product_algorithm algorithm - Sets the algorithm, see `MatProductAlgorithm` for possible values
546: - -mat_product_algorithm_backend_cpu - Use the CPU to perform the computation even if the matrix is a GPU matrix
548: Level: intermediate
550: Note:
551: The `-mat_product_clear` option reduces memory usage but means that the matrix cannot be re-used for a matrix-matrix product operation
553: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatSetFromOptions()`, `MatProductCreate()`, `MatProductCreateWithMat()`, `MatProductNumeric()`,
554: `MatProductSetType()`, `MatProductSetAlgorithm()`, `MatProductAlgorithm`
555: @*/
556: PetscErrorCode MatProductSetFromOptions(Mat mat)
557: {
558: PetscFunctionBegin;
560: MatCheckProduct(mat, 1);
561: PetscCheck(!mat->product->data, PetscObjectComm((PetscObject)mat), PETSC_ERR_ORDER, "Cannot call MatProductSetFromOptions() with already present data");
562: mat->product->setfromoptionscalled = PETSC_TRUE;
563: PetscObjectOptionsBegin((PetscObject)mat);
564: PetscCall(PetscOptionsBool("-mat_product_clear", "Clear intermediate data structures after MatProductNumeric() has been called", "MatProductClear", mat->product->clear, &mat->product->clear, NULL));
565: PetscCall(PetscOptionsDeprecated("-mat_freeintermediatedatastructures", "-mat_product_clear", "3.13", "Or call MatProductClear() after MatProductNumeric()"));
566: PetscOptionsEnd();
567: PetscCall(MatProductSetFromOptions_Private(mat));
568: PetscCheck(mat->product, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing product after setup phase");
569: PetscFunctionReturn(PETSC_SUCCESS);
570: }
572: /*@
573: MatProductView - View the private matrix-matrix algorithm object within a matrix
575: Logically Collective
577: Input Parameters:
578: + mat - the matrix obtained with `MatProductCreate()` or `MatProductCreateWithMat()`
579: - viewer - where the information on the matrix-matrix algorithm of `mat` should be reviewed
581: Level: intermediate
583: Developer Note:
584: Shouldn't this information be printed from an appropriate `MatView()` with perhaps certain formats set?
586: .seealso: [](ch_matrices), `MatProductType`, `Mat`, `MatProductSetFromOptions()`, `MatView()`, `MatProductCreate()`, `MatProductCreateWithMat()`
587: @*/
588: PetscErrorCode MatProductView(Mat mat, PetscViewer viewer)
589: {
590: PetscFunctionBegin;
592: if (!mat->product) PetscFunctionReturn(PETSC_SUCCESS);
593: if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mat), &viewer));
595: PetscCheckSameComm(mat, 1, viewer, 2);
596: if (mat->product->view) PetscCall((*mat->product->view)(mat, viewer));
597: PetscFunctionReturn(PETSC_SUCCESS);
598: }
600: /* these are basic implementations relying on the old function pointers
601: * they are dangerous and should be removed in the future */
602: PetscErrorCode MatProductNumeric_AB(Mat mat)
603: {
604: Mat_Product *product = mat->product;
605: Mat A = product->A, B = product->B;
607: PetscFunctionBegin;
608: PetscCall((*mat->ops->matmultnumeric)(A, B, mat));
609: PetscFunctionReturn(PETSC_SUCCESS);
610: }
612: PetscErrorCode MatProductNumeric_AtB(Mat mat)
613: {
614: Mat_Product *product = mat->product;
615: Mat A = product->A, B = product->B;
617: PetscFunctionBegin;
618: PetscCall((*mat->ops->transposematmultnumeric)(A, B, mat));
619: PetscFunctionReturn(PETSC_SUCCESS);
620: }
622: PetscErrorCode MatProductNumeric_ABt(Mat mat)
623: {
624: Mat_Product *product = mat->product;
625: Mat A = product->A, B = product->B;
627: PetscFunctionBegin;
628: PetscCall((*mat->ops->mattransposemultnumeric)(A, B, mat));
629: PetscFunctionReturn(PETSC_SUCCESS);
630: }
632: PetscErrorCode MatProductNumeric_PtAP(Mat mat)
633: {
634: Mat_Product *product = mat->product;
635: Mat A = product->A, B = product->B;
637: PetscFunctionBegin;
638: PetscCall((*mat->ops->ptapnumeric)(A, B, mat));
639: PetscFunctionReturn(PETSC_SUCCESS);
640: }
642: PetscErrorCode MatProductNumeric_RARt(Mat mat)
643: {
644: Mat_Product *product = mat->product;
645: Mat A = product->A, B = product->B;
647: PetscFunctionBegin;
648: PetscCall((*mat->ops->rartnumeric)(A, B, mat));
649: PetscFunctionReturn(PETSC_SUCCESS);
650: }
652: PetscErrorCode MatProductNumeric_ABC(Mat mat)
653: {
654: Mat_Product *product = mat->product;
655: Mat A = product->A, B = product->B, C = product->C;
657: PetscFunctionBegin;
658: PetscCall((*mat->ops->matmatmultnumeric)(A, B, C, mat));
659: PetscFunctionReturn(PETSC_SUCCESS);
660: }
662: /*@
663: MatProductNumeric - Compute a matrix-matrix product operation with the numerical values
665: Collective
667: Input/Output Parameter:
668: . mat - the matrix whose values are computed via a matrix-matrix product operation
670: Level: intermediate
672: Note:
673: `MatProductSymbolic()` must have been called on `mat` before calling this function
675: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductSetAlgorithm()`, `MatProductSetType()`, `MatProductCreate()`, `MatSetType()`, `MatProductSymbolic()`
676: @*/
677: PetscErrorCode MatProductNumeric(Mat mat)
678: {
679: PetscLogEvent eventtype = -1;
681: PetscFunctionBegin;
683: MatCheckProduct(mat, 1);
684: PetscCheck(!mat->structure_only, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONGSTATE, "Cannot compute numerical values of a MAT_STRUCTURE_ONLY product");
685: switch (mat->product->type) {
686: case MATPRODUCT_AB:
687: eventtype = MAT_MatMultNumeric;
688: break;
689: case MATPRODUCT_AtB:
690: eventtype = MAT_TransposeMatMultNumeric;
691: break;
692: case MATPRODUCT_ABt:
693: eventtype = MAT_MatTransposeMultNumeric;
694: break;
695: case MATPRODUCT_PtAP:
696: eventtype = MAT_PtAPNumeric;
697: break;
698: case MATPRODUCT_RARt:
699: eventtype = MAT_RARtNumeric;
700: break;
701: case MATPRODUCT_ABC:
702: eventtype = MAT_MatMatMultNumeric;
703: break;
704: default:
705: SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "ProductType %s is not supported", MatProductTypes[mat->product->type]);
706: }
708: if (mat->ops->productnumeric) {
709: PetscCall(PetscLogEventBegin(eventtype, mat, 0, 0, 0));
710: PetscUseTypeMethod(mat, productnumeric);
711: PetscCall(PetscLogEventEnd(eventtype, mat, 0, 0, 0));
712: } else if (mat->product) {
713: char errstr[256];
715: if (mat->product->type == MATPRODUCT_ABC) {
716: PetscCall(PetscSNPrintf(errstr, 256, "%s with A %s, B %s, C %s", MatProductTypes[mat->product->type], ((PetscObject)mat->product->A)->type_name, ((PetscObject)mat->product->B)->type_name, ((PetscObject)mat->product->C)->type_name));
717: } else {
718: PetscCall(PetscSNPrintf(errstr, 256, "%s with A %s, B %s", MatProductTypes[mat->product->type], ((PetscObject)mat->product->A)->type_name, ((PetscObject)mat->product->B)->type_name));
719: }
720: SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Unspecified numeric phase for product %s", errstr);
721: }
722: PetscCheck(mat->product, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing struct after numeric phase for product");
724: if (mat->product->clear) PetscCall(MatProductClear(mat));
725: PetscCall(PetscObjectStateIncrease((PetscObject)mat));
726: PetscFunctionReturn(PETSC_SUCCESS);
727: }
729: /* these are basic implementations relying on the old function pointers
730: * they are dangerous and should be removed in the future */
731: PetscErrorCode MatProductSymbolic_AB(Mat mat)
732: {
733: Mat_Product *product = mat->product;
734: Mat A = product->A, B = product->B;
736: PetscFunctionBegin;
737: PetscCall((*mat->ops->matmultsymbolic)(A, B, product->fill, mat));
738: mat->ops->productnumeric = MatProductNumeric_AB;
739: PetscFunctionReturn(PETSC_SUCCESS);
740: }
742: PetscErrorCode MatProductSymbolic_AtB(Mat mat)
743: {
744: Mat_Product *product = mat->product;
745: Mat A = product->A, B = product->B;
747: PetscFunctionBegin;
748: PetscCall((*mat->ops->transposematmultsymbolic)(A, B, product->fill, mat));
749: mat->ops->productnumeric = MatProductNumeric_AtB;
750: PetscFunctionReturn(PETSC_SUCCESS);
751: }
753: PetscErrorCode MatProductSymbolic_ABt(Mat mat)
754: {
755: Mat_Product *product = mat->product;
756: Mat A = product->A, B = product->B;
758: PetscFunctionBegin;
759: PetscCall((*mat->ops->mattransposemultsymbolic)(A, B, product->fill, mat));
760: mat->ops->productnumeric = MatProductNumeric_ABt;
761: PetscFunctionReturn(PETSC_SUCCESS);
762: }
764: PetscErrorCode MatProductSymbolic_ABC(Mat mat)
765: {
766: Mat_Product *product = mat->product;
767: Mat A = product->A, B = product->B, C = product->C;
769: PetscFunctionBegin;
770: PetscCall((*mat->ops->matmatmultsymbolic)(A, B, C, product->fill, mat));
771: mat->ops->productnumeric = MatProductNumeric_ABC;
772: PetscFunctionReturn(PETSC_SUCCESS);
773: }
775: /*@
776: MatProductSymbolic - Perform the symbolic portion of a matrix-matrix product operation, this creates a data structure for use with the numerical
777: product to be done with `MatProductNumeric()`
779: Collective
781: Input/Output Parameter:
782: . mat - the matrix whose values are to be computed via a matrix-matrix product operation
784: Level: intermediate
786: Notes:
787: `MatProductSetFromOptions()` must have been called on `mat` before calling this function.
788: For `MATPRODUCT_AB` and `MATPRODUCT_AtB` with AIJ inputs, setting `MAT_STRUCTURE_ONLY` on `mat` before
789: `MatProductSetFromOptions()` requests an assembled graph without numerical values. These products use the
790: native CPU symbolic algorithms (`sorted` for sequential AB, `outerproduct` for sequential AtB, and `scalable`
791: for MPI products), overriding the selected algorithm, and return `MATSEQAIJ` or `MATMPIAIJ`, including for device AIJ inputs.
792: Call `MatProductClear()` when the product context is no longer needed. `MatProductNumeric()` cannot be used on a structure-only result.
794: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreate()`, `MatProductCreateWithMat()`, `MatProductSetFromOptions()`, `MatProductNumeric()`, `MatProductSetType()`, `MatProductSetAlgorithm()`
795: @*/
796: PetscErrorCode MatProductSymbolic(Mat mat)
797: {
798: PetscLogEvent eventtype = -1;
799: PetscBool missing = PETSC_FALSE;
800: Mat_Product *product = mat->product;
801: Mat A = product->A;
802: Mat B = product->B;
803: Mat C = product->C;
805: PetscFunctionBegin;
807: MatCheckProduct(mat, 1);
808: PetscCheck(!mat->product->data, PetscObjectComm((PetscObject)mat), PETSC_ERR_ORDER, "Cannot run symbolic phase. Product data not empty");
809: switch (mat->product->type) {
810: case MATPRODUCT_AB:
811: eventtype = MAT_MatMultSymbolic;
812: break;
813: case MATPRODUCT_AtB:
814: eventtype = MAT_TransposeMatMultSymbolic;
815: break;
816: case MATPRODUCT_ABt:
817: eventtype = MAT_MatTransposeMultSymbolic;
818: break;
819: case MATPRODUCT_PtAP:
820: eventtype = MAT_PtAPSymbolic;
821: break;
822: case MATPRODUCT_RARt:
823: eventtype = MAT_RARtSymbolic;
824: break;
825: case MATPRODUCT_ABC:
826: eventtype = MAT_MatMatMultSymbolic;
827: break;
828: default:
829: SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "ProductType %s is not supported", MatProductTypes[mat->product->type]);
830: }
831: mat->ops->productnumeric = NULL;
832: if (mat->ops->productsymbolic) {
833: PetscCall(PetscLogEventBegin(eventtype, mat, 0, 0, 0));
834: PetscUseTypeMethod(mat, productsymbolic);
835: PetscCall(PetscLogEventEnd(eventtype, mat, 0, 0, 0));
836: } else missing = PETSC_TRUE;
837: if (missing || !mat->product || !mat->ops->productnumeric) {
838: char errstr[256];
840: if (mat->product->type == MATPRODUCT_ABC) {
841: PetscCall(PetscSNPrintf(errstr, 256, "%s with A %s, B %s, C %s", MatProductTypes[mat->product->type], ((PetscObject)mat->product->A)->type_name, ((PetscObject)mat->product->B)->type_name, ((PetscObject)mat->product->C)->type_name));
842: } else {
843: PetscCall(PetscSNPrintf(errstr, 256, "%s with A %s, B %s", MatProductTypes[mat->product->type], ((PetscObject)mat->product->A)->type_name, ((PetscObject)mat->product->B)->type_name));
844: }
845: PetscCheck(mat->product->setfromoptionscalled, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Unspecified symbolic phase for product %s. Call MatProductSetFromOptions() first", errstr);
846: PetscCheck(!missing, PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "Unspecified symbolic phase for product %s. The product is not supported", errstr);
847: PetscCheck(mat->product, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing struct after symbolic phase for product %s", errstr);
848: }
849: PetscCall(MatGetNonzeroState(mat->product->A, &mat->product->Anonzerostate));
850: #if PetscDefined(HAVE_DEVICE)
851: PetscBool bindingpropagates;
852: bindingpropagates = (PetscBool)((A->boundtocpu && A->bindingpropagates) || (B->boundtocpu && B->bindingpropagates));
853: if (C) bindingpropagates = (PetscBool)(bindingpropagates || (C->boundtocpu && C->bindingpropagates));
854: if (bindingpropagates) {
855: PetscCall(MatBindToCPU(mat, PETSC_TRUE));
856: PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
857: }
858: #endif
859: /* set block sizes */
860: switch (product->type) {
861: case MATPRODUCT_PtAP:
862: if (B->cmap->bs > 1) PetscCall(MatSetBlockSizes(mat, B->cmap->bs, B->cmap->bs));
863: break;
864: case MATPRODUCT_RARt:
865: if (B->rmap->bs > 1) PetscCall(MatSetBlockSizes(mat, B->rmap->bs, B->rmap->bs));
866: break;
867: case MATPRODUCT_ABC:
868: PetscCall(MatSetBlockSizesFromMats(mat, A, C));
869: break;
870: case MATPRODUCT_AB:
871: PetscCall(MatSetBlockSizesFromMats(mat, A, B));
872: break;
873: case MATPRODUCT_AtB:
874: if (A->cmap->bs > 1 || B->cmap->bs > 1) PetscCall(MatSetBlockSizes(mat, A->cmap->bs, B->cmap->bs));
875: break;
876: case MATPRODUCT_ABt:
877: if (A->rmap->bs > 1 || B->rmap->bs > 1) PetscCall(MatSetBlockSizes(mat, A->rmap->bs, B->rmap->bs));
878: break;
879: default:
880: SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Not for ProductType %s", MatProductTypes[product->type]);
881: }
882: PetscFunctionReturn(PETSC_SUCCESS);
883: }
885: /*@
886: MatProductSetFill - Set an expected fill of the matrix whose values are computed via a matrix-matrix product operation
888: Collective
890: Input Parameters:
891: + mat - the matrix whose values are to be computed via a matrix-matrix product operation
892: - fill - expected fill as ratio of nnz(mat)/(nnz(A) + nnz(B) + nnz(C)); use `PETSC_DETERMINE` or `PETSC_CURRENT` if you do not have a good estimate.
893: If the product is a dense matrix, this value is not used.
895: Level: intermediate
897: Notes:
898: Use `fill` of `PETSC_DETERMINE` to use the default value.
900: The deprecated `PETSC_DEFAULT` is also supported to mean use the current value.
902: .seealso: [](ch_matrices), `MatProduct`, `PETSC_DETERMINE`, `Mat`, `MatProductSetFromOptions()`, `MatProductSetType()`, `MatProductSetAlgorithm()`, `MatProductCreate()`
903: @*/
904: PetscErrorCode MatProductSetFill(Mat mat, PetscReal fill)
905: {
906: PetscFunctionBegin;
908: MatCheckProduct(mat, 1);
909: if (fill == (PetscReal)PETSC_DETERMINE) mat->product->fill = mat->product->default_fill;
910: else if (fill != (PetscReal)PETSC_CURRENT) mat->product->fill = fill;
911: PetscFunctionReturn(PETSC_SUCCESS);
912: }
914: /*@
915: MatProductSetAlgorithm - Requests a particular algorithm for a matrix-matrix product operation that will perform to compute the given matrix
917: Collective
919: Input Parameters:
920: + mat - the matrix whose values are computed via a matrix-matrix product operation
921: - alg - particular implementation algorithm of the matrix product, e.g., `MATPRODUCTALGORITHMDEFAULT`.
923: Options Database Key:
924: . -mat_product_algorithm algorithm - Sets the algorithm, see `MatProductAlgorithm`
926: Level: intermediate
928: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductClear()`, `MatProductSetType()`, `MatProductSetFill()`, `MatProductCreate()`, `MatProductAlgorithm`, `MatProductType`, `MatProductGetAlgorithm()`
929: @*/
930: PetscErrorCode MatProductSetAlgorithm(Mat mat, MatProductAlgorithm alg)
931: {
932: PetscFunctionBegin;
934: MatCheckProduct(mat, 1);
935: PetscCall(PetscFree(mat->product->alg));
936: PetscCall(PetscStrallocpy(alg, &mat->product->alg));
937: PetscFunctionReturn(PETSC_SUCCESS);
938: }
940: /*@
941: MatProductGetAlgorithm - Returns the selected algorithm for a matrix-matrix product operation
943: Not Collective
945: Input Parameter:
946: . mat - the matrix whose values are computed via a matrix-matrix product operation
948: Output Parameter:
949: . alg - the selected algorithm of the matrix product, e.g., `MATPRODUCTALGORITHMDEFAULT`.
951: Level: intermediate
953: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductSetAlgorithm()`
954: @*/
955: PetscErrorCode MatProductGetAlgorithm(Mat mat, MatProductAlgorithm *alg)
956: {
957: PetscFunctionBegin;
959: PetscAssertPointer(alg, 2);
960: if (mat->product) *alg = mat->product->alg;
961: else *alg = NULL;
962: PetscFunctionReturn(PETSC_SUCCESS);
963: }
965: /*@
966: MatProductSetType - Sets a particular matrix-matrix product operation to be used to compute the values of the given matrix
968: Collective
970: Input Parameters:
971: + mat - the matrix whose values are computed via a matrix-matrix product operation
972: - productype - matrix product type, e.g., `MATPRODUCT_AB`,`MATPRODUCT_AtB`,`MATPRODUCT_ABt`,`MATPRODUCT_PtAP`,`MATPRODUCT_RARt`,`MATPRODUCT_ABC`,
973: see `MatProductType`
975: Level: intermediate
977: Note:
978: The small t represents the transpose operation.
980: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreate()`, `MatProductType`,
981: `MATPRODUCT_AB`, `MATPRODUCT_AtB`, `MATPRODUCT_ABt`, `MATPRODUCT_PtAP`, `MATPRODUCT_RARt`, `MATPRODUCT_ABC`
982: @*/
983: PetscErrorCode MatProductSetType(Mat mat, MatProductType productype)
984: {
985: PetscFunctionBegin;
987: MatCheckProduct(mat, 1);
989: if (productype != mat->product->type) {
990: if (mat->product->destroy) PetscCall((*mat->product->destroy)(&mat->product->data));
991: mat->product->destroy = NULL;
992: mat->product->data = NULL;
993: mat->ops->productsymbolic = NULL;
994: mat->ops->productnumeric = NULL;
995: }
996: mat->product->type = productype;
997: PetscFunctionReturn(PETSC_SUCCESS);
998: }
1000: /*@
1001: MatProductClear - Clears from the matrix any internal data structures related to the computation of the values of the matrix from matrix-matrix product operations
1003: Collective
1005: Input Parameter:
1006: . mat - the matrix whose values are to be computed via a matrix-matrix product operation
1008: Options Database Key:
1009: . -mat_product_clear - Clear intermediate data structures after `MatProductNumeric()` has been called
1011: Level: intermediate
1013: Notes:
1014: This function should be called to remove any intermediate data used to compute the matrix to free up memory.
1016: After having called this function, matrix-matrix product operations can no longer be used on `mat`
1018: Developer Note:
1019: This frees the `Mat_Product` context that was attached to the matrix during `MatProductCreate()` or `MatProductCreateWithMat()`
1021: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreate()`
1022: @*/
1023: PetscErrorCode MatProductClear(Mat mat)
1024: {
1025: Mat_Product *product = mat->product;
1027: PetscFunctionBegin;
1029: if (product) {
1030: PetscCall(MatDestroy(&product->A));
1031: PetscCall(MatDestroy(&product->B));
1032: PetscCall(MatDestroy(&product->C));
1033: PetscCall(PetscFree(product->alg));
1034: PetscCall(MatDestroy(&product->Dwork));
1035: if (product->destroy) PetscCall((*product->destroy)(&product->data));
1036: }
1037: PetscCall(PetscFree(mat->product));
1038: mat->ops->productsymbolic = NULL;
1039: mat->ops->productnumeric = NULL;
1040: PetscFunctionReturn(PETSC_SUCCESS);
1041: }
1043: /* Create a supporting struct and attach it to the matrix product */
1044: PetscErrorCode MatProductCreate_Private(Mat A, Mat B, Mat C, Mat D)
1045: {
1046: Mat_Product *product = NULL;
1048: PetscFunctionBegin;
1050: PetscCheck(!D->product, PetscObjectComm((PetscObject)D), PETSC_ERR_PLIB, "Product already present");
1051: PetscCall(PetscNew(&product));
1052: product->A = A;
1053: product->B = B;
1054: product->C = C;
1055: product->type = MATPRODUCT_UNSPECIFIED;
1056: product->Dwork = NULL;
1057: product->api_user = PETSC_FALSE;
1058: product->clear = PETSC_FALSE;
1059: product->setfromoptionscalled = PETSC_FALSE;
1060: PetscObjectParameterSetDefault(product, fill, 2);
1061: D->product = product;
1063: PetscCall(MatProductSetAlgorithm(D, MATPRODUCTALGORITHMDEFAULT));
1064: PetscCall(MatProductSetFill(D, PETSC_DEFAULT));
1066: PetscCall(PetscObjectReference((PetscObject)A));
1067: PetscCall(PetscObjectReference((PetscObject)B));
1068: PetscCall(PetscObjectReference((PetscObject)C));
1069: PetscFunctionReturn(PETSC_SUCCESS);
1070: }
1072: /*@
1073: MatProductCreateWithMat - Set a given matrix to have its values computed via matrix-matrix operations on other matrices.
1075: Collective
1077: Input Parameters:
1078: + A - the first matrix
1079: . B - the second matrix
1080: . C - the third matrix (optional, use `NULL` if not needed)
1081: - D - the matrix whose values are to be computed via a matrix-matrix product operation
1083: Level: intermediate
1085: Notes:
1086: Use `MatProductCreate()` if the matrix you wish computed `D` does not exist
1088: See `MatProductCreate()` for details on the usage of the matrix-matrix product operations
1090: Any product data currently attached to `D` will be freed
1092: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductType`, `MatProductSetType()`, `MatProductAlgorithm`,
1093: `MatProductSetAlgorithm`, `MatProductCreate()`, `MatProductClear()`
1094: @*/
1095: PetscErrorCode MatProductCreateWithMat(Mat A, Mat B, Mat C, Mat D)
1096: {
1097: PetscFunctionBegin;
1100: MatCheckPreallocated(A, 1);
1101: PetscCheck(A->assembled, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
1102: PetscCheck(!A->factortype, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
1106: MatCheckPreallocated(B, 2);
1107: PetscCheck(B->assembled, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
1108: PetscCheck(!B->factortype, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
1110: if (C) {
1113: MatCheckPreallocated(C, 3);
1114: PetscCheck(C->assembled, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
1115: PetscCheck(!C->factortype, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
1116: }
1120: MatCheckPreallocated(D, 4);
1121: PetscCheck(D->assembled, PetscObjectComm((PetscObject)D), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
1122: PetscCheck(!D->factortype, PetscObjectComm((PetscObject)D), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
1124: /* Create a supporting struct and attach it to D */
1125: PetscCall(MatProductClear(D));
1126: PetscCall(MatProductCreate_Private(A, B, C, D));
1127: PetscFunctionReturn(PETSC_SUCCESS);
1128: }
1130: /*@
1131: MatProductCreate - create a matrix to hold the result of a matrix-matrix (or matrix-matrix-matrix) product operation
1133: Collective
1135: Input Parameters:
1136: + A - the first matrix
1137: . B - the second matrix
1138: - C - the third matrix (or `NULL`)
1140: Output Parameter:
1141: . D - the matrix whose values are to be computed via a matrix-matrix product operation
1143: Level: intermediate
1145: Example:
1146: .vb
1147: MatProductCreate(A,B,C,&D); or MatProductCreateWithMat(A,B,C,D)
1148: MatProductSetType(D, MATPRODUCT_AB or MATPRODUCT_AtB or MATPRODUCT_ABt or MATPRODUCT_PtAP or MATPRODUCT_RARt or MATPRODUCT_ABC)
1149: MatProductSetAlgorithm(D, alg)
1150: MatProductSetFill(D,fill)
1151: MatProductSetFromOptions(D)
1152: MatProductSymbolic(D)
1153: MatProductNumeric(D)
1154: Change numerical values in some of the matrices
1155: MatProductNumeric(D)
1156: .ve
1158: Notes:
1159: Use `MatProductCreateWithMat()` if `D` the matrix you wish computed already exists.
1161: The information computed during the symbolic stage can be reused for new numerical computations with the same non-zero structure of the input matrices.
1163: Developer Notes:
1164: It is undocumented what happens if the nonzero structure of the input matrices changes. Is the symbolic stage automatically redone? Does it crash?
1165: Is there error checking for it?
1167: On this call, auxiliary data needed to compute the product is stored in `D` in a `Mat_Product` context. A call to `MatProductClear()` frees this
1168: information.
1170: Each `MatProductAlgorithm` associated with a particular `MatType` stores additional data needed for the product computation
1171: (generally this data is computed in `MatProductSymbolic()`) inside the `Mat_Product` context in a `MatProductCtx_XXX` data structure
1172: and provides a `MatProductCtxDestroy_XXX()` routine to free that data. The `MatProductAlgorithm` and `MatType` specific destroy routine is called by
1173: `MatProductClear()`.
1175: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreateWithMat()`, `MatProductSetType()`, `MatProductSetAlgorithm()`, `MatProductClear()`,
1176: `MatProductSymbolic()`, `MatProductNumeric()`, `MatProductAlgorithm`, `MatProductType`
1177: @*/
1178: PetscErrorCode MatProductCreate(Mat A, Mat B, Mat C, Mat *D)
1179: {
1180: PetscFunctionBegin;
1185: PetscCheck(!A->factortype, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix A");
1186: PetscCheck(!B->factortype, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix B");
1188: if (C) {
1191: PetscCheck(!C->factortype, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix C");
1192: }
1194: PetscAssertPointer(D, 4);
1195: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), D));
1196: /* Delay setting type of D to the MatProduct symbolic phase, as we allow sparse A and dense B */
1197: PetscCall(MatProductCreate_Private(A, B, C, *D));
1198: PetscFunctionReturn(PETSC_SUCCESS);
1199: }
1201: /*
1202: These are safe basic implementations of ABC, RARt and PtAP
1203: that do not rely on mat->ops->matmatop function pointers.
1204: They only use the MatProduct API and are currently used by
1205: cuSPARSE and KOKKOS-KERNELS backends
1206: */
1207: typedef struct {
1208: Mat BC;
1209: Mat ABC;
1210: } MatProductCtx_MatMatMatPrivate;
1212: static PetscErrorCode MatProductCtxDestroy_MatMatMatPrivate(PetscCtxRt data)
1213: {
1214: MatProductCtx_MatMatMatPrivate *mmdata = *(MatProductCtx_MatMatMatPrivate **)data;
1216: PetscFunctionBegin;
1217: PetscCall(MatDestroy(&mmdata->BC));
1218: PetscCall(MatDestroy(&mmdata->ABC));
1219: PetscCall(PetscFree(mmdata));
1220: PetscFunctionReturn(PETSC_SUCCESS);
1221: }
1223: static PetscErrorCode MatProductNumeric_ABC_Basic(Mat mat)
1224: {
1225: Mat_Product *product = mat->product;
1226: MatProductCtx_MatMatMatPrivate *mmabc;
1228: PetscFunctionBegin;
1229: MatCheckProduct(mat, 1);
1230: PetscCheck(mat->product->data, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Product data empty");
1231: mmabc = (MatProductCtx_MatMatMatPrivate *)mat->product->data;
1232: PetscCheck(mmabc->BC->ops->productnumeric, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing numeric stage");
1233: /* use function pointer directly to prevent logging */
1234: PetscCall((*mmabc->BC->ops->productnumeric)(mmabc->BC));
1235: /* swap ABC product stuff with that of ABC for the numeric phase on mat */
1236: mat->product = mmabc->ABC->product;
1237: mat->ops->productnumeric = mmabc->ABC->ops->productnumeric;
1238: /* use function pointer directly to prevent logging */
1239: PetscUseTypeMethod(mat, productnumeric);
1240: mat->ops->productnumeric = MatProductNumeric_ABC_Basic;
1241: mat->product = product;
1242: PetscFunctionReturn(PETSC_SUCCESS);
1243: }
1245: PetscErrorCode MatProductSymbolic_ABC_Basic(Mat mat)
1246: {
1247: Mat_Product *product = mat->product;
1248: Mat A, B, C;
1249: MatProductType p1, p2;
1250: MatProductCtx_MatMatMatPrivate *mmabc;
1251: const char *prefix;
1253: PetscFunctionBegin;
1254: MatCheckProduct(mat, 1);
1255: PetscCheck(!mat->product->data, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Product data not empty");
1256: PetscCall(MatGetOptionsPrefix(mat, &prefix));
1257: PetscCall(PetscNew(&mmabc));
1258: product->data = mmabc;
1259: product->destroy = MatProductCtxDestroy_MatMatMatPrivate;
1260: switch (product->type) {
1261: case MATPRODUCT_PtAP:
1262: p1 = MATPRODUCT_AB;
1263: p2 = MATPRODUCT_AtB;
1264: A = product->B;
1265: B = product->A;
1266: C = product->B;
1267: if (A->cmap->bs > 0 && C->cmap->bs > 0) PetscCall(MatSetBlockSizes(mat, A->cmap->bs, C->cmap->bs));
1268: break;
1269: case MATPRODUCT_RARt:
1270: p1 = MATPRODUCT_ABt;
1271: p2 = MATPRODUCT_AB;
1272: A = product->B;
1273: B = product->A;
1274: C = product->B;
1275: if (A->rmap->bs > 0 && C->rmap->bs > 0) PetscCall(MatSetBlockSizes(mat, A->rmap->bs, C->rmap->bs));
1276: break;
1277: case MATPRODUCT_ABC:
1278: p1 = MATPRODUCT_AB;
1279: p2 = MATPRODUCT_AB;
1280: A = product->A;
1281: B = product->B;
1282: C = product->C;
1283: PetscCall(MatSetBlockSizesFromMats(mat, A, C));
1284: break;
1285: default:
1286: SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Not for ProductType %s", MatProductTypes[product->type]);
1287: }
1288: PetscCall(MatProductCreate(B, C, NULL, &mmabc->BC));
1289: PetscCall(MatSetOptionsPrefix(mmabc->BC, prefix));
1290: PetscCall(MatAppendOptionsPrefix(mmabc->BC, "P1_"));
1291: PetscCall(MatProductSetType(mmabc->BC, p1));
1292: PetscCall(MatProductSetAlgorithm(mmabc->BC, MATPRODUCTALGORITHMDEFAULT));
1293: PetscCall(MatProductSetFill(mmabc->BC, product->fill));
1294: mmabc->BC->product->api_user = product->api_user;
1295: PetscCall(MatProductSetFromOptions(mmabc->BC));
1296: PetscCheck(mmabc->BC->ops->productsymbolic, PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "Symbolic ProductType %s not supported with %s and %s", MatProductTypes[p1], ((PetscObject)B)->type_name, ((PetscObject)C)->type_name);
1297: /* use function pointer directly to prevent logging */
1298: PetscCall((*mmabc->BC->ops->productsymbolic)(mmabc->BC));
1300: PetscCall(MatProductCreate(A, mmabc->BC, NULL, &mmabc->ABC));
1301: PetscCall(MatSetOptionsPrefix(mmabc->ABC, prefix));
1302: PetscCall(MatAppendOptionsPrefix(mmabc->ABC, "P2_"));
1303: PetscCall(MatProductSetType(mmabc->ABC, p2));
1304: PetscCall(MatProductSetAlgorithm(mmabc->ABC, MATPRODUCTALGORITHMDEFAULT));
1305: PetscCall(MatProductSetFill(mmabc->ABC, product->fill));
1306: mmabc->ABC->product->api_user = product->api_user;
1307: PetscCall(MatProductSetFromOptions(mmabc->ABC));
1308: PetscCheck(mmabc->ABC->ops->productsymbolic, PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "Symbolic ProductType %s not supported with %s and %s", MatProductTypes[p2], ((PetscObject)A)->type_name, ((PetscObject)mmabc->BC)->type_name);
1309: /* swap ABC product stuff with that of ABC for the symbolic phase on mat */
1310: mat->product = mmabc->ABC->product;
1311: mat->ops->productsymbolic = mmabc->ABC->ops->productsymbolic;
1312: /* use function pointer directly to prevent logging */
1313: PetscUseTypeMethod(mat, productsymbolic);
1314: mmabc->ABC->ops->productnumeric = mat->ops->productnumeric;
1315: mat->ops->productsymbolic = MatProductSymbolic_ABC_Basic;
1316: mat->ops->productnumeric = MatProductNumeric_ABC_Basic;
1317: mat->product = product;
1318: PetscFunctionReturn(PETSC_SUCCESS);
1319: }
1321: /*@
1322: MatProductGetType - Returns the type of matrix-matrix product associated with computing values for the given matrix
1324: Not Collective
1326: Input Parameter:
1327: . mat - the matrix whose values are to be computed via a matrix-matrix product operation
1329: Output Parameter:
1330: . mtype - the `MatProductType`
1332: Level: intermediate
1334: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreateWithMat()`, `MatProductSetType()`, `MatProductCreate()`, `MatProductType`, `MatProductAlgorithm`
1335: @*/
1336: PetscErrorCode MatProductGetType(Mat mat, MatProductType *mtype)
1337: {
1338: PetscFunctionBegin;
1340: PetscAssertPointer(mtype, 2);
1341: *mtype = MATPRODUCT_UNSPECIFIED;
1342: if (mat->product) *mtype = mat->product->type;
1343: PetscFunctionReturn(PETSC_SUCCESS);
1344: }
1346: /*@
1347: MatProductGetMats - Returns the matrices associated with the matrix-matrix product associated with computing values for the given matrix
1349: Not Collective
1351: Input Parameter:
1352: . mat - the matrix whose values are to be computed via a matrix-matrix product operation
1354: Output Parameters:
1355: + A - the first matrix
1356: . B - the second matrix
1357: - C - the third matrix (may be `NULL` for some `MatProductType`)
1359: Level: intermediate
1361: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreateWithMat()`, `MatProductSetType()`, `MatProductSetAlgorithm()`, `MatProductCreate()`
1362: @*/
1363: PetscErrorCode MatProductGetMats(Mat mat, Mat *A, Mat *B, Mat *C)
1364: {
1365: PetscFunctionBegin;
1367: if (A) *A = mat->product ? mat->product->A : NULL;
1368: if (B) *B = mat->product ? mat->product->B : NULL;
1369: if (C) *C = mat->product ? mat->product->C : NULL;
1370: PetscFunctionReturn(PETSC_SUCCESS);
1371: }