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;
365: PetscFunctionBegin;
366: switch (product->type) {
367: case MATPRODUCT_AB:
368: PetscCall(MatDenseSetUpProduct_Private(C, A->rmap, B->cmap, B, NULL));
369: break;
370: case MATPRODUCT_AtB:
371: PetscCall(MatDenseSetUpProduct_Private(C, A->cmap, B->cmap, B, NULL));
372: break;
373: default:
374: 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);
375: }
376: C->ops->productsymbolic = MatProductSymbolic_X_Dense;
377: C->ops->productnumeric = MatProductNumeric_X_Dense;
378: PetscFunctionReturn(PETSC_SUCCESS);
379: }
381: /* a single driver to query the dispatching */
382: static PetscErrorCode MatProductSetFromOptions_Private(Mat mat)
383: {
384: Mat_Product *product = mat->product;
385: PetscInt Am, An, Bm, Bn, Cm, Cn;
386: Mat A = product->A, B = product->B, C = product->C;
387: const char *const Bnames[] = {"B", "R", "P"};
388: const char *bname;
389: PetscErrorCode (*fA)(Mat);
390: PetscErrorCode (*fB)(Mat);
391: PetscErrorCode (*fC)(Mat);
392: PetscErrorCode (*f)(Mat) = NULL;
394: PetscFunctionBegin;
395: mat->ops->productsymbolic = NULL;
396: mat->ops->productnumeric = NULL;
397: if (product->type == MATPRODUCT_UNSPECIFIED) PetscFunctionReturn(PETSC_SUCCESS);
398: PetscCheck(A, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing A mat");
399: PetscCheck(B, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing B mat");
400: PetscCheck(product->type != MATPRODUCT_ABC || C, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing C mat");
401: if (product->type != MATPRODUCT_ABC) C = NULL; /* do not use C if not needed */
402: if (product->type == MATPRODUCT_RARt) bname = Bnames[1];
403: else if (product->type == MATPRODUCT_PtAP) bname = Bnames[2];
404: else bname = Bnames[0];
406: /* Check matrices sizes */
407: Am = A->rmap->N;
408: An = A->cmap->N;
409: Bm = B->rmap->N;
410: Bn = B->cmap->N;
411: Cm = C ? C->rmap->N : 0;
412: Cn = C ? C->cmap->N : 0;
413: if (product->type == MATPRODUCT_RARt || product->type == MATPRODUCT_ABt) {
414: PetscInt t = Bn;
415: Bn = Bm;
416: Bm = t;
417: }
418: if (product->type == MATPRODUCT_AtB) An = Am;
420: 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,
421: MatProductTypes[product->type], A->rmap->N, A->cmap->N, bname, B->rmap->N, B->cmap->N);
422: 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,
423: MatProductTypes[product->type], B->rmap->N, B->cmap->N, Cm, Cn);
425: /* graph products use the host CSR structure, including for AIJ device types */
426: if (mat->structure_only && (product->type == MATPRODUCT_AB || product->type == MATPRODUCT_AtB)) {
427: PetscBool aseq, bseq, ampi, bmpi;
429: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATSEQAIJ, &aseq));
430: PetscCall(PetscObjectBaseTypeCompare((PetscObject)B, MATSEQAIJ, &bseq));
431: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIAIJ, &i));
432: PetscCall(PetscObjectBaseTypeCompare((PetscObject)B, MATMPIAIJ, &bmpi));
433: if (aseq && bseq) {
434: PetscCall(MatProductSetFromOptions_SeqAIJ(mat));
435: PetscFunctionReturn(PETSC_SUCCESS);
436: } else if (ampi && bmpi) {
437: PetscCall(MatProductSetFromOptions_MPIAIJ(mat));
438: PetscFunctionReturn(PETSC_SUCCESS);
439: }
440: }
442: fA = A->ops->productsetfromoptions;
443: fB = B->ops->productsetfromoptions;
444: fC = C ? C->ops->productsetfromoptions : fA;
445: if (C) {
446: 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));
447: } else {
448: PetscCall(PetscInfo(mat, "MatProductType %s for A %s, %s %s\n", MatProductTypes[product->type], ((PetscObject)A)->type_name, bname, ((PetscObject)B)->type_name));
449: }
450: if (fA == fB && fA == fC && fA) {
451: PetscCall(PetscInfo(mat, " matching op\n"));
452: PetscCall((*fA)(mat));
453: }
454: /* We may have found f but it did not succeed */
455: if (!mat->ops->productsymbolic) { /* query MatProductSetFromOptions_Atype_Btype_Ctype */
456: char mtypes[256];
457: PetscCall(PetscStrncpy(mtypes, "MatProductSetFromOptions_", sizeof(mtypes)));
458: PetscCall(PetscStrlcat(mtypes, ((PetscObject)A)->type_name, sizeof(mtypes)));
459: PetscCall(PetscStrlcat(mtypes, "_", sizeof(mtypes)));
460: PetscCall(PetscStrlcat(mtypes, ((PetscObject)B)->type_name, sizeof(mtypes)));
461: if (C) {
462: PetscCall(PetscStrlcat(mtypes, "_", sizeof(mtypes)));
463: PetscCall(PetscStrlcat(mtypes, ((PetscObject)C)->type_name, sizeof(mtypes)));
464: }
465: PetscCall(PetscStrlcat(mtypes, "_C", sizeof(mtypes)));
466: #if defined(__clang__)
467: PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN("-Wformat-pedantic")
468: #elif defined(__GNUC__) || defined(__GNUG__)
469: PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN("-Wformat")
470: #endif
471: PetscCall(PetscObjectQueryFunction((PetscObject)A, mtypes, &f));
472: PetscCall(PetscInfo(mat, " querying %s from A? %p\n", mtypes, f));
473: if (!f) {
474: PetscCall(PetscObjectQueryFunction((PetscObject)B, mtypes, &f));
475: PetscCall(PetscInfo(mat, " querying %s from %s? %p\n", mtypes, bname, f));
476: }
477: if (!f && C) {
478: PetscCall(PetscObjectQueryFunction((PetscObject)C, mtypes, &f));
479: PetscCall(PetscInfo(mat, " querying %s from C? %p\n", mtypes, f));
480: }
481: if (f) PetscCall((*f)(mat));
483: /* We may have found f but it did not succeed */
484: /* some matrices (i.e. MATTRANSPOSEVIRTUAL, MATSHELL constructed from MatConvert), knows what to do with their inner matrices */
485: if (!mat->ops->productsymbolic) {
486: PetscCall(PetscStrncpy(mtypes, "MatProductSetFromOptions_anytype_C", sizeof(mtypes)));
487: PetscCall(PetscObjectQueryFunction((PetscObject)A, mtypes, &f));
488: PetscCall(PetscInfo(mat, " querying %s from A? %p\n", mtypes, f));
489: if (!f) {
490: PetscCall(PetscObjectQueryFunction((PetscObject)B, mtypes, &f));
491: PetscCall(PetscInfo(mat, " querying %s from %s? %p\n", mtypes, bname, f));
492: }
493: if (!f && C) {
494: PetscCall(PetscObjectQueryFunction((PetscObject)C, mtypes, &f));
495: PetscCall(PetscInfo(mat, " querying %s from C? %p\n", mtypes, f));
496: }
497: }
498: if (f) PetscCall((*f)(mat));
499: }
500: PETSC_PRAGMA_DIAGNOSTIC_IGNORED_END()
501: /* We may have found f but it did not succeed */
502: if (!mat->ops->productsymbolic) {
503: /* we can still compute the product if B is of type dense */
504: if (product->type == MATPRODUCT_AB || product->type == MATPRODUCT_AtB) {
505: PetscBool isdense;
507: PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)B, &isdense, MATSEQDENSE, MATMPIDENSE, ""));
508: if (isdense) {
509: mat->ops->productsymbolic = MatProductSymbolic_X_Dense;
510: PetscCall(PetscInfo(mat, " using basic looping over columns of a dense matrix\n"));
511: }
512: } else if (product->type != MATPRODUCT_ABt) { /* use MatProductSymbolic/Numeric_Unsafe() for triple products only */
513: /*
514: TODO: this should be changed to a proper setfromoptions, not setting the symbolic pointer here, because we do not know if
515: the combination will succeed. In order to be sure, we need MatProductGetProductType to return the type of the result
516: before computing the symbolic phase
517: */
518: PetscCall(PetscInfo(mat, " symbolic product not supported, using MatProductSymbolic_Unsafe() implementation\n"));
519: mat->ops->productsymbolic = MatProductSymbolic_Unsafe;
520: }
521: }
522: if (!mat->ops->productsymbolic) PetscCall(PetscInfo(mat, " symbolic product is not supported\n"));
523: PetscFunctionReturn(PETSC_SUCCESS);
524: }
526: /*@
527: MatProductSetFromOptions - Sets the options for the computation of a matrix-matrix product operation where the type,
528: the algorithm etc are determined from the options database.
530: Logically Collective
532: Input Parameter:
533: . mat - the matrix whose values are computed via a matrix-matrix product operation
535: Options Database Keys:
536: + -mat_product_clear (true|false) - Clear intermediate data structures after `MatProductNumeric()` has been called
537: . -mat_product_algorithm algorithm - Sets the algorithm, see `MatProductAlgorithm` for possible values
538: - -mat_product_algorithm_backend_cpu (true|false) - Use the CPU to perform the computation even if the matrix is a GPU matrix
540: Level: intermediate
542: Note:
543: The `-mat_product_clear` option reduces memory usage but means that the matrix cannot be re-used for a matrix-matrix product operation
545: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatSetFromOptions()`, `MatProductCreate()`, `MatProductCreateWithMat()`, `MatProductNumeric()`,
546: `MatProductSetType()`, `MatProductSetAlgorithm()`, `MatProductAlgorithm`
547: @*/
548: PetscErrorCode MatProductSetFromOptions(Mat mat)
549: {
550: PetscFunctionBegin;
552: MatCheckProduct(mat, 1);
553: PetscCheck(!mat->product->data, PetscObjectComm((PetscObject)mat), PETSC_ERR_ORDER, "Cannot call MatProductSetFromOptions() with already present data");
554: mat->product->setfromoptionscalled = PETSC_TRUE;
555: PetscObjectOptionsBegin((PetscObject)mat);
556: PetscCall(PetscOptionsBool("-mat_product_clear", "Clear intermediate data structures after MatProductNumeric() has been called", "MatProductClear", mat->product->clear, &mat->product->clear, NULL));
557: PetscCall(PetscOptionsDeprecated("-mat_freeintermediatedatastructures", "-mat_product_clear", "3.13", "Or call MatProductClear() after MatProductNumeric()"));
558: PetscOptionsEnd();
559: PetscCall(MatProductSetFromOptions_Private(mat));
560: PetscCheck(mat->product, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing product after setup phase");
561: PetscFunctionReturn(PETSC_SUCCESS);
562: }
564: /*@
565: MatProductView - View the private matrix-matrix algorithm object within a matrix
567: Logically Collective
569: Input Parameters:
570: + mat - the matrix obtained with `MatProductCreate()` or `MatProductCreateWithMat()`
571: - viewer - where the information on the matrix-matrix algorithm of `mat` should be reviewed
573: Level: intermediate
575: Developer Note:
576: Shouldn't this information be printed from an appropriate `MatView()` with perhaps certain formats set?
578: .seealso: [](ch_matrices), `MatProductType`, `Mat`, `MatProductSetFromOptions()`, `MatView()`, `MatProductCreate()`, `MatProductCreateWithMat()`
579: @*/
580: PetscErrorCode MatProductView(Mat mat, PetscViewer viewer)
581: {
582: PetscFunctionBegin;
584: if (!mat->product) PetscFunctionReturn(PETSC_SUCCESS);
585: if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mat), &viewer));
587: PetscCheckSameComm(mat, 1, viewer, 2);
588: if (mat->product->view) PetscCall((*mat->product->view)(mat, viewer));
589: PetscFunctionReturn(PETSC_SUCCESS);
590: }
592: /* these are basic implementations relying on the old function pointers
593: * they are dangerous and should be removed in the future */
594: PetscErrorCode MatProductNumeric_AB(Mat mat)
595: {
596: Mat_Product *product = mat->product;
597: Mat A = product->A, B = product->B;
599: PetscFunctionBegin;
600: PetscCall((*mat->ops->matmultnumeric)(A, B, mat));
601: PetscFunctionReturn(PETSC_SUCCESS);
602: }
604: PetscErrorCode MatProductNumeric_AtB(Mat mat)
605: {
606: Mat_Product *product = mat->product;
607: Mat A = product->A, B = product->B;
609: PetscFunctionBegin;
610: PetscCall((*mat->ops->transposematmultnumeric)(A, B, mat));
611: PetscFunctionReturn(PETSC_SUCCESS);
612: }
614: PetscErrorCode MatProductNumeric_ABt(Mat mat)
615: {
616: Mat_Product *product = mat->product;
617: Mat A = product->A, B = product->B;
619: PetscFunctionBegin;
620: PetscCall((*mat->ops->mattransposemultnumeric)(A, B, mat));
621: PetscFunctionReturn(PETSC_SUCCESS);
622: }
624: PetscErrorCode MatProductNumeric_PtAP(Mat mat)
625: {
626: Mat_Product *product = mat->product;
627: Mat A = product->A, B = product->B;
629: PetscFunctionBegin;
630: PetscCall((*mat->ops->ptapnumeric)(A, B, mat));
631: PetscFunctionReturn(PETSC_SUCCESS);
632: }
634: PetscErrorCode MatProductNumeric_RARt(Mat mat)
635: {
636: Mat_Product *product = mat->product;
637: Mat A = product->A, B = product->B;
639: PetscFunctionBegin;
640: PetscCall((*mat->ops->rartnumeric)(A, B, mat));
641: PetscFunctionReturn(PETSC_SUCCESS);
642: }
644: PetscErrorCode MatProductNumeric_ABC(Mat mat)
645: {
646: Mat_Product *product = mat->product;
647: Mat A = product->A, B = product->B, C = product->C;
649: PetscFunctionBegin;
650: PetscCall((*mat->ops->matmatmultnumeric)(A, B, C, mat));
651: PetscFunctionReturn(PETSC_SUCCESS);
652: }
654: /*@
655: MatProductNumeric - Compute a matrix-matrix product operation with the numerical values
657: Collective
659: Input/Output Parameter:
660: . mat - the matrix whose values are computed via a matrix-matrix product operation
662: Level: intermediate
664: Note:
665: `MatProductSymbolic()` must have been called on `mat` before calling this function
667: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductSetAlgorithm()`, `MatProductSetType()`, `MatProductCreate()`, `MatSetType()`, `MatProductSymbolic()`
668: @*/
669: PetscErrorCode MatProductNumeric(Mat mat)
670: {
671: PetscLogEvent eventtype = -1;
673: PetscFunctionBegin;
675: MatCheckProduct(mat, 1);
676: PetscCheck(!mat->structure_only, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONGSTATE, "Cannot compute numerical values of a MAT_STRUCTURE_ONLY product");
677: switch (mat->product->type) {
678: case MATPRODUCT_AB:
679: eventtype = MAT_MatMultNumeric;
680: break;
681: case MATPRODUCT_AtB:
682: eventtype = MAT_TransposeMatMultNumeric;
683: break;
684: case MATPRODUCT_ABt:
685: eventtype = MAT_MatTransposeMultNumeric;
686: break;
687: case MATPRODUCT_PtAP:
688: eventtype = MAT_PtAPNumeric;
689: break;
690: case MATPRODUCT_RARt:
691: eventtype = MAT_RARtNumeric;
692: break;
693: case MATPRODUCT_ABC:
694: eventtype = MAT_MatMatMultNumeric;
695: break;
696: default:
697: SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "ProductType %s is not supported", MatProductTypes[mat->product->type]);
698: }
700: if (mat->ops->productnumeric) {
701: PetscCall(PetscLogEventBegin(eventtype, mat, 0, 0, 0));
702: PetscUseTypeMethod(mat, productnumeric);
703: PetscCall(PetscLogEventEnd(eventtype, mat, 0, 0, 0));
704: } else if (mat->product) {
705: char errstr[256];
707: if (mat->product->type == MATPRODUCT_ABC) {
708: 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));
709: } else {
710: 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));
711: }
712: SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Unspecified numeric phase for product %s", errstr);
713: }
714: PetscCheck(mat->product, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing struct after numeric phase for product");
716: if (mat->product->clear) PetscCall(MatProductClear(mat));
717: PetscCall(PetscObjectStateIncrease((PetscObject)mat));
718: PetscFunctionReturn(PETSC_SUCCESS);
719: }
721: /* these are basic implementations relying on the old function pointers
722: * they are dangerous and should be removed in the future */
723: PetscErrorCode MatProductSymbolic_AB(Mat mat)
724: {
725: Mat_Product *product = mat->product;
726: Mat A = product->A, B = product->B;
728: PetscFunctionBegin;
729: PetscCall((*mat->ops->matmultsymbolic)(A, B, product->fill, mat));
730: mat->ops->productnumeric = MatProductNumeric_AB;
731: PetscFunctionReturn(PETSC_SUCCESS);
732: }
734: PetscErrorCode MatProductSymbolic_AtB(Mat mat)
735: {
736: Mat_Product *product = mat->product;
737: Mat A = product->A, B = product->B;
739: PetscFunctionBegin;
740: PetscCall((*mat->ops->transposematmultsymbolic)(A, B, product->fill, mat));
741: mat->ops->productnumeric = MatProductNumeric_AtB;
742: PetscFunctionReturn(PETSC_SUCCESS);
743: }
745: PetscErrorCode MatProductSymbolic_ABt(Mat mat)
746: {
747: Mat_Product *product = mat->product;
748: Mat A = product->A, B = product->B;
750: PetscFunctionBegin;
751: PetscCall((*mat->ops->mattransposemultsymbolic)(A, B, product->fill, mat));
752: mat->ops->productnumeric = MatProductNumeric_ABt;
753: PetscFunctionReturn(PETSC_SUCCESS);
754: }
756: PetscErrorCode MatProductSymbolic_ABC(Mat mat)
757: {
758: Mat_Product *product = mat->product;
759: Mat A = product->A, B = product->B, C = product->C;
761: PetscFunctionBegin;
762: PetscCall((*mat->ops->matmatmultsymbolic)(A, B, C, product->fill, mat));
763: mat->ops->productnumeric = MatProductNumeric_ABC;
764: PetscFunctionReturn(PETSC_SUCCESS);
765: }
767: /*@
768: MatProductSymbolic - Perform the symbolic portion of a matrix-matrix product operation, this creates a data structure for use with the numerical
769: product to be done with `MatProductNumeric()`
771: Collective
773: Input/Output Parameter:
774: . mat - the matrix whose values are to be computed via a matrix-matrix product operation
776: Level: intermediate
778: Notes:
779: `MatProductSetFromOptions()` must have been called on `mat` before calling this function.
780: For `MATPRODUCT_AB`, `MATPRODUCT_AtB`, and `MATPRODUCT_ABt`, the result inherits the block sizes of the
781: operand layouts that supply its rows and columns, including when the caller supplies the result matrix.
782: For `MATPRODUCT_AB` and `MATPRODUCT_AtB` with AIJ inputs, setting `MAT_STRUCTURE_ONLY` on `mat` before
783: `MatProductSetFromOptions()` requests an assembled graph without numerical values. These products use the
784: native CPU symbolic algorithms (`sorted` for sequential AB, `outerproduct` for sequential AtB, and `scalable`
785: for MPI products), overriding the selected algorithm, and return `MATSEQAIJ` or `MATMPIAIJ`, including for device AIJ inputs.
786: Call `MatProductClear()` when the product context is no longer needed. `MatProductNumeric()` cannot be used on a structure-only result.
788: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreate()`, `MatProductCreateWithMat()`, `MatProductSetFromOptions()`, `MatProductNumeric()`, `MatProductSetType()`, `MatProductSetAlgorithm()`
789: @*/
790: PetscErrorCode MatProductSymbolic(Mat mat)
791: {
792: PetscLogEvent eventtype = -1;
793: PetscBool missing = PETSC_FALSE;
794: Mat_Product *product = mat->product;
795: Mat A = product->A;
796: Mat B = product->B;
797: Mat C = product->C;
799: PetscFunctionBegin;
801: MatCheckProduct(mat, 1);
802: PetscCheck(!mat->product->data, PetscObjectComm((PetscObject)mat), PETSC_ERR_ORDER, "Cannot run symbolic phase. Product data not empty");
803: switch (mat->product->type) {
804: case MATPRODUCT_AB:
805: eventtype = MAT_MatMultSymbolic;
806: break;
807: case MATPRODUCT_AtB:
808: eventtype = MAT_TransposeMatMultSymbolic;
809: break;
810: case MATPRODUCT_ABt:
811: eventtype = MAT_MatTransposeMultSymbolic;
812: break;
813: case MATPRODUCT_PtAP:
814: eventtype = MAT_PtAPSymbolic;
815: break;
816: case MATPRODUCT_RARt:
817: eventtype = MAT_RARtSymbolic;
818: break;
819: case MATPRODUCT_ABC:
820: eventtype = MAT_MatMatMultSymbolic;
821: break;
822: default:
823: SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "ProductType %s is not supported", MatProductTypes[mat->product->type]);
824: }
825: mat->ops->productnumeric = NULL;
826: if (mat->ops->productsymbolic) {
827: PetscCall(PetscLogEventBegin(eventtype, mat, 0, 0, 0));
828: PetscUseTypeMethod(mat, productsymbolic);
829: PetscCall(PetscLogEventEnd(eventtype, mat, 0, 0, 0));
830: } else missing = PETSC_TRUE;
831: if (missing || !mat->product || !mat->ops->productnumeric) {
832: char errstr[256];
834: if (mat->product->type == MATPRODUCT_ABC) {
835: 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));
836: } else {
837: 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));
838: }
839: PetscCheck(mat->product->setfromoptionscalled, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Unspecified symbolic phase for product %s. Call MatProductSetFromOptions() first", errstr);
840: PetscCheck(!missing, PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "Unspecified symbolic phase for product %s. The product is not supported", errstr);
841: PetscCheck(mat->product, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing struct after symbolic phase for product %s", errstr);
842: }
843: PetscCall(MatGetNonzeroState(mat->product->A, &mat->product->Anonzerostate));
844: #if PetscDefined(HAVE_DEVICE)
845: PetscBool bindingpropagates;
846: bindingpropagates = (PetscBool)((A->boundtocpu && A->bindingpropagates) || (B->boundtocpu && B->bindingpropagates));
847: if (C) bindingpropagates = (PetscBool)(bindingpropagates || (C->boundtocpu && C->bindingpropagates));
848: if (bindingpropagates) {
849: PetscCall(MatBindToCPU(mat, PETSC_TRUE));
850: PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
851: }
852: #endif
853: /* set block sizes */
854: switch (product->type) {
855: case MATPRODUCT_PtAP:
856: if (B->cmap->bs > 1) PetscCall(MatSetBlockSizes(mat, B->cmap->bs, B->cmap->bs));
857: break;
858: case MATPRODUCT_RARt:
859: if (B->rmap->bs > 1) PetscCall(MatSetBlockSizes(mat, B->rmap->bs, B->rmap->bs));
860: break;
861: case MATPRODUCT_ABC:
862: PetscCall(MatSetBlockSizesFromMats(mat, A, C));
863: break;
864: case MATPRODUCT_AB:
865: PetscCall(MatSetBlockSizesFromMats(mat, A, B));
866: break;
867: case MATPRODUCT_AtB:
868: if (mat->rmap->bs != A->cmap->bs || mat->cmap->bs != B->cmap->bs) PetscCall(MatSetBlockSizes(mat, A->cmap->bs, B->cmap->bs));
869: break;
870: case MATPRODUCT_ABt:
871: if (mat->rmap->bs != A->rmap->bs || mat->cmap->bs != B->rmap->bs) PetscCall(MatSetBlockSizes(mat, A->rmap->bs, B->rmap->bs));
872: break;
873: default:
874: SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Not for ProductType %s", MatProductTypes[product->type]);
875: }
876: PetscFunctionReturn(PETSC_SUCCESS);
877: }
879: /*@
880: MatProductSetFill - Set an expected fill of the matrix whose values are computed via a matrix-matrix product operation
882: Collective
884: Input Parameters:
885: + mat - the matrix whose values are to be computed via a matrix-matrix product operation
886: - 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.
887: If the product is a dense matrix, this value is not used.
889: Level: intermediate
891: Notes:
892: Use `fill` of `PETSC_DETERMINE` to use the default value.
894: The deprecated `PETSC_DEFAULT` is also supported to mean use the current value.
896: .seealso: [](ch_matrices), `MatProduct`, `PETSC_DETERMINE`, `Mat`, `MatProductSetFromOptions()`, `MatProductSetType()`, `MatProductSetAlgorithm()`, `MatProductCreate()`
897: @*/
898: PetscErrorCode MatProductSetFill(Mat mat, PetscReal fill)
899: {
900: PetscFunctionBegin;
902: MatCheckProduct(mat, 1);
903: if (fill == (PetscReal)PETSC_DETERMINE) mat->product->fill = mat->product->default_fill;
904: else if (fill != (PetscReal)PETSC_CURRENT) mat->product->fill = fill;
905: PetscFunctionReturn(PETSC_SUCCESS);
906: }
908: /*@
909: MatProductSetAlgorithm - Requests a particular algorithm for a matrix-matrix product operation that will perform to compute the given matrix
911: Collective
913: Input Parameters:
914: + mat - the matrix whose values are computed via a matrix-matrix product operation
915: - alg - particular implementation algorithm of the matrix product, e.g., `MATPRODUCTALGORITHMDEFAULT`.
917: Options Database Key:
918: . -mat_product_algorithm algorithm - Sets the algorithm, see `MatProductAlgorithm`
920: Level: intermediate
922: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductClear()`, `MatProductSetType()`, `MatProductSetFill()`, `MatProductCreate()`, `MatProductAlgorithm`, `MatProductType`, `MatProductGetAlgorithm()`
923: @*/
924: PetscErrorCode MatProductSetAlgorithm(Mat mat, MatProductAlgorithm alg)
925: {
926: PetscFunctionBegin;
928: MatCheckProduct(mat, 1);
929: PetscCall(PetscFree(mat->product->alg));
930: PetscCall(PetscStrallocpy(alg, &mat->product->alg));
931: PetscFunctionReturn(PETSC_SUCCESS);
932: }
934: /*@
935: MatProductGetAlgorithm - Returns the selected algorithm for a matrix-matrix product operation
937: Not Collective
939: Input Parameter:
940: . mat - the matrix whose values are computed via a matrix-matrix product operation
942: Output Parameter:
943: . alg - the selected algorithm of the matrix product, e.g., `MATPRODUCTALGORITHMDEFAULT`.
945: Level: intermediate
947: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductSetAlgorithm()`
948: @*/
949: PetscErrorCode MatProductGetAlgorithm(Mat mat, MatProductAlgorithm *alg)
950: {
951: PetscFunctionBegin;
953: PetscAssertPointer(alg, 2);
954: if (mat->product) *alg = mat->product->alg;
955: else *alg = NULL;
956: PetscFunctionReturn(PETSC_SUCCESS);
957: }
959: /*@
960: MatProductSetType - Sets a particular matrix-matrix product operation to be used to compute the values of the given matrix
962: Collective
964: Input Parameters:
965: + mat - the matrix whose values are computed via a matrix-matrix product operation
966: - productype - matrix product type, e.g., `MATPRODUCT_AB`,`MATPRODUCT_AtB`,`MATPRODUCT_ABt`,`MATPRODUCT_PtAP`,`MATPRODUCT_RARt`,`MATPRODUCT_ABC`,
967: see `MatProductType`
969: Level: intermediate
971: Note:
972: The small t represents the transpose operation.
974: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreate()`, `MatProductType`,
975: `MATPRODUCT_AB`, `MATPRODUCT_AtB`, `MATPRODUCT_ABt`, `MATPRODUCT_PtAP`, `MATPRODUCT_RARt`, `MATPRODUCT_ABC`
976: @*/
977: PetscErrorCode MatProductSetType(Mat mat, MatProductType productype)
978: {
979: PetscFunctionBegin;
981: MatCheckProduct(mat, 1);
983: if (productype != mat->product->type) {
984: if (mat->product->destroy) PetscCall((*mat->product->destroy)(&mat->product->data));
985: mat->product->destroy = NULL;
986: mat->product->data = NULL;
987: mat->ops->productsymbolic = NULL;
988: mat->ops->productnumeric = NULL;
989: }
990: mat->product->type = productype;
991: PetscFunctionReturn(PETSC_SUCCESS);
992: }
994: /*@
995: MatProductClear - Clears from the matrix any internal data structures related to the computation of the values of the matrix from matrix-matrix product operations
997: Collective
999: Input Parameter:
1000: . mat - the matrix whose values are to be computed via a matrix-matrix product operation
1002: Options Database Key:
1003: . -mat_product_clear (true|false) - Clear intermediate data structures after `MatProductNumeric()` has been called
1005: Level: intermediate
1007: Notes:
1008: This function should be called to remove any intermediate data used to compute the matrix to free up memory.
1010: After having called this function, matrix-matrix product operations can no longer be used on `mat`
1012: Developer Note:
1013: This frees the `Mat_Product` context that was attached to the matrix during `MatProductCreate()` or `MatProductCreateWithMat()`
1015: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreate()`
1016: @*/
1017: PetscErrorCode MatProductClear(Mat mat)
1018: {
1019: Mat_Product *product = mat->product;
1021: PetscFunctionBegin;
1023: if (product) {
1024: PetscCall(MatDestroy(&product->A));
1025: PetscCall(MatDestroy(&product->B));
1026: PetscCall(MatDestroy(&product->C));
1027: PetscCall(PetscFree(product->alg));
1028: PetscCall(MatDestroy(&product->Dwork));
1029: if (product->destroy) PetscCall((*product->destroy)(&product->data));
1030: }
1031: PetscCall(PetscFree(mat->product));
1032: mat->ops->productsymbolic = NULL;
1033: mat->ops->productnumeric = NULL;
1034: PetscFunctionReturn(PETSC_SUCCESS);
1035: }
1037: /* Create a supporting struct and attach it to the matrix product */
1038: PetscErrorCode MatProductCreate_Private(Mat A, Mat B, Mat C, Mat D)
1039: {
1040: Mat_Product *product = NULL;
1042: PetscFunctionBegin;
1044: PetscCheck(!D->product, PetscObjectComm((PetscObject)D), PETSC_ERR_PLIB, "Product already present");
1045: PetscCall(PetscNew(&product));
1046: product->A = A;
1047: product->B = B;
1048: product->C = C;
1049: product->type = MATPRODUCT_UNSPECIFIED;
1050: product->Dwork = NULL;
1051: product->api_user = PETSC_FALSE;
1052: product->clear = PETSC_FALSE;
1053: product->setfromoptionscalled = PETSC_FALSE;
1054: PetscObjectParameterSetDefault(product, fill, 2);
1055: D->product = product;
1057: PetscCall(MatProductSetAlgorithm(D, MATPRODUCTALGORITHMDEFAULT));
1058: PetscCall(MatProductSetFill(D, PETSC_DEFAULT));
1060: PetscCall(PetscObjectReference((PetscObject)A));
1061: PetscCall(PetscObjectReference((PetscObject)B));
1062: PetscCall(PetscObjectReference((PetscObject)C));
1063: PetscFunctionReturn(PETSC_SUCCESS);
1064: }
1066: /*@
1067: MatProductCreateWithMat - Set a given matrix to have its values computed via matrix-matrix operations on other matrices.
1069: Collective
1071: Input Parameters:
1072: + A - the first matrix
1073: . B - the second matrix
1074: . C - the third matrix (optional, use `NULL` if not needed)
1075: - D - the matrix whose values are to be computed via a matrix-matrix product operation
1077: Level: intermediate
1079: Notes:
1080: Use `MatProductCreate()` if the matrix you wish computed `D` does not exist
1082: See `MatProductCreate()` for details on the usage of the matrix-matrix product operations
1084: Any product data currently attached to `D` will be freed
1086: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductType`, `MatProductSetType()`, `MatProductAlgorithm`,
1087: `MatProductSetAlgorithm`, `MatProductCreate()`, `MatProductClear()`
1088: @*/
1089: PetscErrorCode MatProductCreateWithMat(Mat A, Mat B, Mat C, Mat D)
1090: {
1091: PetscFunctionBegin;
1094: MatCheckPreallocated(A, 1);
1095: PetscCheck(A->assembled, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
1096: PetscCheck(!A->factortype, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
1100: MatCheckPreallocated(B, 2);
1101: PetscCheck(B->assembled, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
1102: PetscCheck(!B->factortype, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
1104: if (C) {
1107: MatCheckPreallocated(C, 3);
1108: PetscCheck(C->assembled, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
1109: PetscCheck(!C->factortype, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
1110: }
1114: MatCheckPreallocated(D, 4);
1115: PetscCheck(D->assembled, PetscObjectComm((PetscObject)D), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
1116: PetscCheck(!D->factortype, PetscObjectComm((PetscObject)D), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
1118: /* Create a supporting struct and attach it to D */
1119: PetscCall(MatProductClear(D));
1120: PetscCall(MatProductCreate_Private(A, B, C, D));
1121: PetscFunctionReturn(PETSC_SUCCESS);
1122: }
1124: /*@
1125: MatProductCreate - create a matrix to hold the result of a matrix-matrix (or matrix-matrix-matrix) product operation
1127: Collective
1129: Input Parameters:
1130: + A - the first matrix
1131: . B - the second matrix
1132: - C - the third matrix (or `NULL`)
1134: Output Parameter:
1135: . D - the matrix whose values are to be computed via a matrix-matrix product operation
1137: Level: intermediate
1139: Example:
1140: .vb
1141: MatProductCreate(A,B,C,&D); or MatProductCreateWithMat(A,B,C,D)
1142: MatProductSetType(D, MATPRODUCT_AB or MATPRODUCT_AtB or MATPRODUCT_ABt or MATPRODUCT_PtAP or MATPRODUCT_RARt or MATPRODUCT_ABC)
1143: MatProductSetAlgorithm(D, alg)
1144: MatProductSetFill(D,fill)
1145: MatProductSetFromOptions(D)
1146: MatProductSymbolic(D)
1147: MatProductNumeric(D)
1148: Change numerical values in some of the matrices
1149: MatProductNumeric(D)
1150: .ve
1152: Notes:
1153: Use `MatProductCreateWithMat()` if `D` the matrix you wish computed already exists.
1155: The information computed during the symbolic stage can be reused for new numerical computations with the same non-zero structure of the input matrices.
1157: Developer Notes:
1158: It is undocumented what happens if the nonzero structure of the input matrices changes. Is the symbolic stage automatically redone? Does it crash?
1159: Is there error checking for it?
1161: 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
1162: information.
1164: Each `MatProductAlgorithm` associated with a particular `MatType` stores additional data needed for the product computation
1165: (generally this data is computed in `MatProductSymbolic()`) inside the `Mat_Product` context in a `MatProductCtx_XXX` data structure
1166: and provides a `MatProductCtxDestroy_XXX()` routine to free that data. The `MatProductAlgorithm` and `MatType` specific destroy routine is called by
1167: `MatProductClear()`.
1169: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreateWithMat()`, `MatProductSetType()`, `MatProductSetAlgorithm()`, `MatProductClear()`,
1170: `MatProductSymbolic()`, `MatProductNumeric()`, `MatProductAlgorithm`, `MatProductType`
1171: @*/
1172: PetscErrorCode MatProductCreate(Mat A, Mat B, Mat C, Mat *D)
1173: {
1174: PetscFunctionBegin;
1179: PetscCheck(!A->factortype, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix A");
1180: PetscCheck(!B->factortype, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix B");
1182: if (C) {
1185: PetscCheck(!C->factortype, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix C");
1186: }
1188: PetscAssertPointer(D, 4);
1189: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), D));
1190: /* Delay setting type of D to the MatProduct symbolic phase, as we allow sparse A and dense B */
1191: PetscCall(MatProductCreate_Private(A, B, C, *D));
1192: PetscFunctionReturn(PETSC_SUCCESS);
1193: }
1195: /*
1196: These are safe basic implementations of ABC, RARt and PtAP
1197: that do not rely on mat->ops->matmatop function pointers.
1198: They only use the MatProduct API and are currently used by
1199: cuSPARSE and KOKKOS-KERNELS backends
1200: */
1201: typedef struct {
1202: Mat BC;
1203: Mat ABC;
1204: } MatProductCtx_MatMatMatPrivate;
1206: static PetscErrorCode MatProductCtxDestroy_MatMatMatPrivate(PetscCtxRt data)
1207: {
1208: MatProductCtx_MatMatMatPrivate *mmdata = *(MatProductCtx_MatMatMatPrivate **)data;
1210: PetscFunctionBegin;
1211: PetscCall(MatDestroy(&mmdata->BC));
1212: PetscCall(MatDestroy(&mmdata->ABC));
1213: PetscCall(PetscFree(mmdata));
1214: PetscFunctionReturn(PETSC_SUCCESS);
1215: }
1217: static PetscErrorCode MatProductNumeric_ABC_Basic(Mat mat)
1218: {
1219: Mat_Product *product = mat->product;
1220: MatProductCtx_MatMatMatPrivate *mmabc;
1222: PetscFunctionBegin;
1223: MatCheckProduct(mat, 1);
1224: PetscCheck(mat->product->data, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Product data empty");
1225: mmabc = (MatProductCtx_MatMatMatPrivate *)mat->product->data;
1226: PetscCheck(mmabc->BC->ops->productnumeric, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing numeric stage");
1227: /* use function pointer directly to prevent logging */
1228: PetscCall((*mmabc->BC->ops->productnumeric)(mmabc->BC));
1229: /* swap ABC product stuff with that of ABC for the numeric phase on mat */
1230: mat->product = mmabc->ABC->product;
1231: mat->ops->productnumeric = mmabc->ABC->ops->productnumeric;
1232: /* use function pointer directly to prevent logging */
1233: PetscUseTypeMethod(mat, productnumeric);
1234: mat->ops->productnumeric = MatProductNumeric_ABC_Basic;
1235: mat->product = product;
1236: PetscFunctionReturn(PETSC_SUCCESS);
1237: }
1239: PetscErrorCode MatProductSymbolic_ABC_Basic(Mat mat)
1240: {
1241: Mat_Product *product = mat->product;
1242: Mat A, B, C;
1243: MatProductType p1, p2;
1244: MatProductCtx_MatMatMatPrivate *mmabc;
1245: const char *prefix;
1247: PetscFunctionBegin;
1248: MatCheckProduct(mat, 1);
1249: PetscCheck(!mat->product->data, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Product data not empty");
1250: PetscCall(MatGetOptionsPrefix(mat, &prefix));
1251: PetscCall(PetscNew(&mmabc));
1252: product->data = mmabc;
1253: product->destroy = MatProductCtxDestroy_MatMatMatPrivate;
1254: switch (product->type) {
1255: case MATPRODUCT_PtAP:
1256: p1 = MATPRODUCT_AB;
1257: p2 = MATPRODUCT_AtB;
1258: A = product->B;
1259: B = product->A;
1260: C = product->B;
1261: if (A->cmap->bs > 0 && C->cmap->bs > 0) PetscCall(MatSetBlockSizes(mat, A->cmap->bs, C->cmap->bs));
1262: break;
1263: case MATPRODUCT_RARt:
1264: p1 = MATPRODUCT_ABt;
1265: p2 = MATPRODUCT_AB;
1266: A = product->B;
1267: B = product->A;
1268: C = product->B;
1269: if (A->rmap->bs > 0 && C->rmap->bs > 0) PetscCall(MatSetBlockSizes(mat, A->rmap->bs, C->rmap->bs));
1270: break;
1271: case MATPRODUCT_ABC:
1272: p1 = MATPRODUCT_AB;
1273: p2 = MATPRODUCT_AB;
1274: A = product->A;
1275: B = product->B;
1276: C = product->C;
1277: PetscCall(MatSetBlockSizesFromMats(mat, A, C));
1278: break;
1279: default:
1280: SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Not for ProductType %s", MatProductTypes[product->type]);
1281: }
1282: PetscCall(MatProductCreate(B, C, NULL, &mmabc->BC));
1283: PetscCall(MatSetOptionsPrefix(mmabc->BC, prefix));
1284: PetscCall(MatAppendOptionsPrefix(mmabc->BC, "P1_"));
1285: PetscCall(MatProductSetType(mmabc->BC, p1));
1286: PetscCall(MatProductSetAlgorithm(mmabc->BC, MATPRODUCTALGORITHMDEFAULT));
1287: PetscCall(MatProductSetFill(mmabc->BC, product->fill));
1288: mmabc->BC->product->api_user = product->api_user;
1289: PetscCall(MatProductSetFromOptions(mmabc->BC));
1290: 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);
1291: /* use function pointer directly to prevent logging */
1292: PetscCall((*mmabc->BC->ops->productsymbolic)(mmabc->BC));
1294: PetscCall(MatProductCreate(A, mmabc->BC, NULL, &mmabc->ABC));
1295: PetscCall(MatSetOptionsPrefix(mmabc->ABC, prefix));
1296: PetscCall(MatAppendOptionsPrefix(mmabc->ABC, "P2_"));
1297: PetscCall(MatProductSetType(mmabc->ABC, p2));
1298: PetscCall(MatProductSetAlgorithm(mmabc->ABC, MATPRODUCTALGORITHMDEFAULT));
1299: PetscCall(MatProductSetFill(mmabc->ABC, product->fill));
1300: mmabc->ABC->product->api_user = product->api_user;
1301: PetscCall(MatProductSetFromOptions(mmabc->ABC));
1302: 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);
1303: /* swap ABC product stuff with that of ABC for the symbolic phase on mat */
1304: mat->product = mmabc->ABC->product;
1305: mat->ops->productsymbolic = mmabc->ABC->ops->productsymbolic;
1306: /* use function pointer directly to prevent logging */
1307: PetscUseTypeMethod(mat, productsymbolic);
1308: mmabc->ABC->ops->productnumeric = mat->ops->productnumeric;
1309: mat->ops->productsymbolic = MatProductSymbolic_ABC_Basic;
1310: mat->ops->productnumeric = MatProductNumeric_ABC_Basic;
1311: mat->product = product;
1312: PetscFunctionReturn(PETSC_SUCCESS);
1313: }
1315: /*@
1316: MatProductGetType - Returns the type of matrix-matrix product associated with computing values for the given matrix
1318: Not Collective
1320: Input Parameter:
1321: . mat - the matrix whose values are to be computed via a matrix-matrix product operation
1323: Output Parameter:
1324: . mtype - the `MatProductType`
1326: Level: intermediate
1328: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreateWithMat()`, `MatProductSetType()`, `MatProductCreate()`, `MatProductType`, `MatProductAlgorithm`
1329: @*/
1330: PetscErrorCode MatProductGetType(Mat mat, MatProductType *mtype)
1331: {
1332: PetscFunctionBegin;
1334: PetscAssertPointer(mtype, 2);
1335: *mtype = MATPRODUCT_UNSPECIFIED;
1336: if (mat->product) *mtype = mat->product->type;
1337: PetscFunctionReturn(PETSC_SUCCESS);
1338: }
1340: /*@
1341: MatProductGetMats - Returns the matrices associated with the matrix-matrix product associated with computing values for the given matrix
1343: Not Collective
1345: Input Parameter:
1346: . mat - the matrix whose values are to be computed via a matrix-matrix product operation
1348: Output Parameters:
1349: + A - the first matrix
1350: . B - the second matrix
1351: - C - the third matrix (may be `NULL` for some `MatProductType`)
1353: Level: intermediate
1355: .seealso: [](ch_matrices), `MatProduct`, `Mat`, `MatProductCreateWithMat()`, `MatProductSetType()`, `MatProductSetAlgorithm()`, `MatProductCreate()`
1356: @*/
1357: PetscErrorCode MatProductGetMats(Mat mat, Mat *A, Mat *B, Mat *C)
1358: {
1359: PetscFunctionBegin;
1361: if (A) *A = mat->product ? mat->product->A : NULL;
1362: if (B) *B = mat->product ? mat->product->B : NULL;
1363: if (C) *C = mat->product ? mat->product->C : NULL;
1364: PetscFunctionReturn(PETSC_SUCCESS);
1365: }