Actual source code: mpimatmatmult.c
1: /*
2: Defines matrix-matrix product routines for pairs of MPIAIJ matrices
3: C = A * B
4: */
5: #include <../src/mat/impls/aij/seq/aij.h>
6: #include <../src/mat/utils/freespace.h>
7: #include <../src/mat/impls/aij/mpi/mpiaij.h>
8: #include <../src/mat/impls/dense/mpi/mpidense.h>
9: #include <petsc/private/vecimpl.h>
10: #include <petsc/private/sfimpl.h>
12: #if PetscDefined(HAVE_HYPRE)
13: PETSC_INTERN PetscErrorCode MatMatMultSymbolic_AIJ_AIJ_wHYPRE(Mat, Mat, PetscReal, Mat);
14: #endif
16: PETSC_INTERN PetscErrorCode MatProductSymbolic_ABt_MPIAIJ_MPIAIJ(Mat C)
17: {
18: Mat_Product *product = C->product;
19: Mat B = product->B;
21: PetscFunctionBegin;
22: PetscCall(MatTranspose(B, MAT_INITIAL_MATRIX, &product->B));
23: PetscCall(MatDestroy(&B));
24: PetscCall(MatProductSymbolic_AB_MPIAIJ_MPIAIJ(C));
25: PetscFunctionReturn(PETSC_SUCCESS);
26: }
28: PETSC_INTERN PetscErrorCode MatProductSymbolic_AB_MPIAIJ_MPIAIJ(Mat C)
29: {
30: Mat_Product *product = C->product;
31: Mat A = product->A, B = product->B;
32: MatProductAlgorithm alg = product->alg;
33: PetscReal fill = product->fill;
34: PetscBool flg;
36: PetscFunctionBegin;
37: /* scalable */
38: PetscCall(PetscStrcmp(alg, "scalable", &flg));
39: if (flg || C->structure_only) {
40: if (C->structure_only) PetscCall(MatProductSetAlgorithm(C, "scalable"));
41: PetscCall(MatMatMultSymbolic_MPIAIJ_MPIAIJ(A, B, fill, C));
42: PetscFunctionReturn(PETSC_SUCCESS);
43: }
45: /* nonscalable */
46: PetscCall(PetscStrcmp(alg, "nonscalable", &flg));
47: if (flg) {
48: PetscCall(MatMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(A, B, fill, C));
49: PetscFunctionReturn(PETSC_SUCCESS);
50: }
52: /* seqmpi */
53: PetscCall(PetscStrcmp(alg, "seqmpi", &flg));
54: if (flg) {
55: PetscCall(MatMatMultSymbolic_MPIAIJ_MPIAIJ_seqMPI(A, B, fill, C));
56: PetscFunctionReturn(PETSC_SUCCESS);
57: }
59: /* backend general code */
60: PetscCall(PetscStrcmp(alg, "backend", &flg));
61: if (flg) {
62: PetscCall(MatProductSymbolic_MPIAIJBACKEND(C));
63: PetscFunctionReturn(PETSC_SUCCESS);
64: }
66: #if PetscDefined(HAVE_HYPRE)
67: PetscCall(PetscStrcmp(alg, "hypre", &flg));
68: if (flg) {
69: PetscCall(MatMatMultSymbolic_AIJ_AIJ_wHYPRE(A, B, fill, C));
70: PetscFunctionReturn(PETSC_SUCCESS);
71: }
72: #endif
73: SETERRQ(PetscObjectComm((PetscObject)C), PETSC_ERR_SUP, "Mat Product Algorithm is not supported");
74: }
76: PetscErrorCode MatProductCtxDestroy_MPIAIJ_MatMatMult(PetscCtxRt data)
77: {
78: MatProductCtx_APMPI *ptap = *(MatProductCtx_APMPI **)data;
80: PetscFunctionBegin;
81: PetscCall(PetscFree2(ptap->startsj_s, ptap->startsj_r));
82: PetscCall(PetscFree(ptap->bufa));
83: PetscCall(MatDestroy(&ptap->P_loc));
84: PetscCall(MatDestroy(&ptap->P_oth));
85: PetscCall(MatDestroy(&ptap->Pt));
86: PetscCall(PetscFree(ptap->api));
87: PetscCall(PetscFree(ptap->apj));
88: PetscCall(PetscFree(ptap->apa));
89: PetscCall(PetscFree(ptap));
90: PetscFunctionReturn(PETSC_SUCCESS);
91: }
93: PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable(Mat A, Mat P, Mat C)
94: {
95: Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data, *c = (Mat_MPIAIJ *)C->data;
96: Mat_SeqAIJ *ad = (Mat_SeqAIJ *)a->A->data, *ao = (Mat_SeqAIJ *)a->B->data;
97: Mat_SeqAIJ *cd = (Mat_SeqAIJ *)c->A->data, *co = (Mat_SeqAIJ *)c->B->data;
98: PetscScalar *cda, *coa;
99: Mat_SeqAIJ *p_loc, *p_oth;
100: PetscScalar *apa, *ca;
101: PetscInt cm = C->rmap->n;
102: MatProductCtx_APMPI *ptap;
103: PetscInt *api, *apj, *apJ, i, k;
104: PetscInt cstart = C->cmap->rstart;
105: PetscInt cdnz, conz, k0, k1;
106: const PetscScalar *dummy1, *dummy2, *dummy3, *dummy4;
107: MPI_Comm comm;
108: PetscMPIInt size;
110: PetscFunctionBegin;
111: MatCheckProduct(C, 3);
112: ptap = (MatProductCtx_APMPI *)C->product->data;
113: PetscCheck(ptap, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "PtAP cannot be computed. Missing data");
114: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
115: PetscCallMPI(MPI_Comm_size(comm, &size));
116: PetscCheck(ptap->P_oth || size <= 1, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "AP cannot be reused. Do not call MatProductClear()");
118: /* flag CPU mask for C */
119: #if PetscDefined(HAVE_DEVICE)
120: if (C->offloadmask != PETSC_OFFLOAD_UNALLOCATED) C->offloadmask = PETSC_OFFLOAD_CPU;
121: if (c->A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) c->A->offloadmask = PETSC_OFFLOAD_CPU;
122: if (c->B->offloadmask != PETSC_OFFLOAD_UNALLOCATED) c->B->offloadmask = PETSC_OFFLOAD_CPU;
123: #endif
125: /* 1) get P_oth = ptap->P_oth and P_loc = ptap->P_loc */
126: /* update numerical values of P_oth and P_loc */
127: PetscCall(MatGetBrowsOfAoCols_MPIAIJ(A, P, MAT_REUSE_MATRIX, &ptap->startsj_s, &ptap->startsj_r, &ptap->bufa, &ptap->P_oth));
128: PetscCall(MatMPIAIJGetLocalMat(P, MAT_REUSE_MATRIX, &ptap->P_loc));
130: /* 2) compute numeric C_loc = A_loc*P = Ad*P_loc + Ao*P_oth */
131: /* get data from symbolic products */
132: p_loc = (Mat_SeqAIJ *)ptap->P_loc->data;
133: p_oth = NULL;
134: if (size > 1) p_oth = (Mat_SeqAIJ *)ptap->P_oth->data;
136: /* get apa for storing dense row A[i,:]*P */
137: apa = ptap->apa;
139: api = ptap->api;
140: apj = ptap->apj;
141: /* trigger copy to CPU */
142: PetscCall(MatSeqAIJGetArrayRead(a->A, &dummy1));
143: PetscCall(MatSeqAIJGetArrayRead(a->B, &dummy2));
144: PetscCall(MatSeqAIJGetArrayRead(ptap->P_loc, &dummy3));
145: if (ptap->P_oth) PetscCall(MatSeqAIJGetArrayRead(ptap->P_oth, &dummy4));
146: PetscCall(MatSeqAIJGetArrayWrite(c->A, &cda));
147: PetscCall(MatSeqAIJGetArrayWrite(c->B, &coa));
148: for (i = 0; i < cm; i++) {
149: /* compute apa = A[i,:]*P */
150: AProw_nonscalable(i, ad, ao, p_loc, p_oth, apa);
152: /* set values in C */
153: apJ = PetscSafePointerPlusOffset(apj, api[i]);
154: cdnz = cd->i[i + 1] - cd->i[i];
155: conz = co->i[i + 1] - co->i[i];
157: /* 1st off-diagonal part of C */
158: ca = PetscSafePointerPlusOffset(coa, co->i[i]);
159: k = 0;
160: for (k0 = 0; k0 < conz; k0++) {
161: if (apJ[k] >= cstart) break;
162: ca[k0] = apa[apJ[k]];
163: apa[apJ[k++]] = 0.0;
164: }
166: /* diagonal part of C */
167: ca = PetscSafePointerPlusOffset(cda, cd->i[i]);
168: for (k1 = 0; k1 < cdnz; k1++) {
169: ca[k1] = apa[apJ[k]];
170: apa[apJ[k++]] = 0.0;
171: }
173: /* 2nd off-diagonal part of C */
174: ca = PetscSafePointerPlusOffset(coa, co->i[i]);
175: for (; k0 < conz; k0++) {
176: ca[k0] = apa[apJ[k]];
177: apa[apJ[k++]] = 0.0;
178: }
179: }
180: PetscCall(MatSeqAIJRestoreArrayRead(a->A, &dummy1));
181: PetscCall(MatSeqAIJRestoreArrayRead(a->B, &dummy2));
182: PetscCall(MatSeqAIJRestoreArrayRead(ptap->P_loc, &dummy3));
183: if (ptap->P_oth) PetscCall(MatSeqAIJRestoreArrayRead(ptap->P_oth, &dummy4));
184: PetscCall(MatSeqAIJRestoreArrayWrite(c->A, &cda));
185: PetscCall(MatSeqAIJRestoreArrayWrite(c->B, &coa));
187: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
188: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
189: PetscFunctionReturn(PETSC_SUCCESS);
190: }
192: PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(Mat A, Mat P, PetscReal fill, Mat C)
193: {
194: MPI_Comm comm;
195: PetscMPIInt size;
196: MatProductCtx_APMPI *ptap;
197: PetscFreeSpaceList free_space = NULL, current_space = NULL;
198: Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
199: Mat_SeqAIJ *ad = (Mat_SeqAIJ *)a->A->data, *ao = (Mat_SeqAIJ *)a->B->data, *p_loc, *p_oth;
200: PetscInt *pi_loc, *pj_loc, *pi_oth, *pj_oth, *dnz, *onz;
201: PetscInt *adi = ad->i, *adj = ad->j, *aoi = ao->i, *aoj = ao->j, rstart = A->rmap->rstart;
202: PetscInt *lnk, i, pnz, row, *api, *apj, *Jptr, apnz, nspacedouble = 0, j, nzi;
203: PetscInt am = A->rmap->n, pN = P->cmap->N, pn = P->cmap->n, pm = P->rmap->n;
204: PetscBT lnkbt;
205: PetscReal afill;
206: MatType mtype;
208: PetscFunctionBegin;
209: MatCheckProduct(C, 4);
210: PetscCheck(!C->product->data, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Extra product struct not empty");
211: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
212: PetscCallMPI(MPI_Comm_size(comm, &size));
214: /* create struct MatProductCtx_APMPI and attached it to C later */
215: PetscCall(PetscNew(&ptap));
217: /* get P_oth by taking rows of P (= non-zero cols of local A) from other processors */
218: PetscCall(MatGetBrowsOfAoCols_MPIAIJ(A, P, MAT_INITIAL_MATRIX, &ptap->startsj_s, &ptap->startsj_r, &ptap->bufa, &ptap->P_oth));
220: /* get P_loc by taking all local rows of P */
221: PetscCall(MatMPIAIJGetLocalMat(P, MAT_INITIAL_MATRIX, &ptap->P_loc));
223: p_loc = (Mat_SeqAIJ *)ptap->P_loc->data;
224: pi_loc = p_loc->i;
225: pj_loc = p_loc->j;
226: if (size > 1) {
227: p_oth = (Mat_SeqAIJ *)ptap->P_oth->data;
228: pi_oth = p_oth->i;
229: pj_oth = p_oth->j;
230: } else {
231: p_oth = NULL;
232: pi_oth = NULL;
233: pj_oth = NULL;
234: }
236: /* first, compute symbolic AP = A_loc*P = A_diag*P_loc + A_off*P_oth */
237: PetscCall(PetscMalloc1(am + 1, &api));
238: ptap->api = api;
239: api[0] = 0;
241: /* create and initialize a linked list */
242: PetscCall(PetscLLCondensedCreate(pN, pN, &lnk, &lnkbt));
244: /* Initial FreeSpace size is fill*(nnz(A)+nnz(P)) */
245: PetscCall(PetscFreeSpaceGet(PetscRealIntMultTruncate(fill, PetscIntSumTruncate(adi[am], PetscIntSumTruncate(aoi[am], pi_loc[pm]))), &free_space));
246: current_space = free_space;
248: MatPreallocateBegin(comm, am, pn, dnz, onz);
249: for (i = 0; i < am; i++) {
250: /* diagonal portion of A */
251: nzi = adi[i + 1] - adi[i];
252: for (j = 0; j < nzi; j++) {
253: row = *adj++;
254: pnz = pi_loc[row + 1] - pi_loc[row];
255: Jptr = pj_loc + pi_loc[row];
256: /* add non-zero cols of P into the sorted linked list lnk */
257: PetscCall(PetscLLCondensedAddSorted(pnz, Jptr, lnk, lnkbt));
258: }
259: /* off-diagonal portion of A */
260: nzi = aoi[i + 1] - aoi[i];
261: for (j = 0; j < nzi; j++) {
262: row = *aoj++;
263: pnz = pi_oth[row + 1] - pi_oth[row];
264: Jptr = pj_oth + pi_oth[row];
265: PetscCall(PetscLLCondensedAddSorted(pnz, Jptr, lnk, lnkbt));
266: }
267: /* add possible missing diagonal entry */
268: if (C->force_diagonals) {
269: j = i + rstart; /* column index */
270: PetscCall(PetscLLCondensedAddSorted(1, &j, lnk, lnkbt));
271: }
273: apnz = lnk[0];
274: api[i + 1] = api[i] + apnz;
276: /* if free space is not available, double the total space in the list */
277: if (current_space->local_remaining < apnz) {
278: PetscCall(PetscFreeSpaceGet(PetscIntSumTruncate(apnz, current_space->total_array_size), ¤t_space));
279: nspacedouble++;
280: }
282: /* Copy data into free space, then initialize lnk */
283: PetscCall(PetscLLCondensedClean(pN, apnz, current_space->array, lnk, lnkbt));
284: PetscCall(MatPreallocateSet(i + rstart, apnz, current_space->array, dnz, onz));
286: current_space->array = PetscSafePointerPlusOffset(current_space->array, apnz);
287: current_space->local_used += apnz;
288: current_space->local_remaining -= apnz;
289: }
291: /* Allocate space for apj, initialize apj, and */
292: /* destroy list of free space and other temporary array(s) */
293: PetscCall(PetscMalloc1(api[am], &ptap->apj));
294: apj = ptap->apj;
295: PetscCall(PetscFreeSpaceContiguous(&free_space, ptap->apj));
296: PetscCall(PetscLLDestroy(lnk, lnkbt));
298: /* malloc apa to store dense row A[i,:]*P */
299: PetscCall(PetscCalloc1(pN, &ptap->apa));
301: /* set and assemble symbolic parallel matrix C */
302: PetscCall(MatSetSizes(C, am, pn, PETSC_DETERMINE, PETSC_DETERMINE));
303: PetscCall(MatSetBlockSizesFromMats(C, A, P));
305: PetscCall(MatGetType(A, &mtype));
306: PetscCall(MatSetType(C, mtype));
307: PetscCall(MatMPIAIJSetPreallocation(C, 0, dnz, 0, onz));
308: MatPreallocateEnd(dnz, onz);
310: PetscCall(MatSetValues_MPIAIJ_CopyFromCSRFormat_Symbolic(C, apj, api));
311: PetscCall(MatSetOption(C, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
312: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
313: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
314: PetscCall(MatSetOption(C, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
316: C->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable;
317: C->ops->productnumeric = MatProductNumeric_AB;
319: /* attach the supporting struct to C for reuse */
320: C->product->data = ptap;
321: C->product->destroy = MatProductCtxDestroy_MPIAIJ_MatMatMult;
323: /* set MatInfo */
324: afill = (PetscReal)api[am] / (adi[am] + aoi[am] + pi_loc[pm] + 1) + 1.e-5;
325: if (afill < 1.0) afill = 1.0;
326: C->info.mallocs = nspacedouble;
327: C->info.fill_ratio_given = fill;
328: C->info.fill_ratio_needed = afill;
330: if (PetscDefined(USE_INFO)) {
331: if (api[am]) {
332: PetscCall(PetscInfo(C, "Reallocs %" PetscInt_FMT "; Fill ratio: given %g needed %g.\n", nspacedouble, (double)fill, (double)afill));
333: PetscCall(PetscInfo(C, "Use MatMatMult(A,B,MatReuse,%g,&C) for best performance.;\n", (double)afill));
334: } else PetscCall(PetscInfo(C, "Empty matrix product\n"));
335: }
336: PetscFunctionReturn(PETSC_SUCCESS);
337: }
339: static PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIDense(Mat, Mat, PetscReal, Mat);
340: static PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIDense(Mat, Mat, Mat);
342: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_MPIDense_AB(Mat C)
343: {
344: Mat_Product *product = C->product;
345: Mat A = product->A, B = product->B;
347: PetscFunctionBegin;
348: if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend)
349: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Matrix local dimensions are incompatible, (%" PetscInt_FMT ", %" PetscInt_FMT ") != (%" PetscInt_FMT ",%" PetscInt_FMT ")", A->cmap->rstart, A->cmap->rend, B->rmap->rstart, B->rmap->rend);
351: C->ops->matmultsymbolic = MatMatMultSymbolic_MPIAIJ_MPIDense;
352: C->ops->productsymbolic = MatProductSymbolic_AB;
353: PetscFunctionReturn(PETSC_SUCCESS);
354: }
356: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_MPIDense_AtB(Mat C)
357: {
358: Mat_Product *product = C->product;
359: Mat A = product->A, B = product->B;
361: PetscFunctionBegin;
362: if (A->rmap->rstart != B->rmap->rstart || A->rmap->rend != B->rmap->rend)
363: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Matrix local dimensions are incompatible, (%" PetscInt_FMT ", %" PetscInt_FMT ") != (%" PetscInt_FMT ",%" PetscInt_FMT ")", A->rmap->rstart, A->rmap->rend, B->rmap->rstart, B->rmap->rend);
365: C->ops->transposematmultsymbolic = MatTransposeMatMultSymbolic_MPIAIJ_MPIDense;
366: C->ops->productsymbolic = MatProductSymbolic_AtB;
367: PetscFunctionReturn(PETSC_SUCCESS);
368: }
370: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_MPIAIJ_MPIDense(Mat C)
371: {
372: Mat_Product *product = C->product;
374: PetscFunctionBegin;
375: switch (product->type) {
376: case MATPRODUCT_AB:
377: PetscCall(MatProductSetFromOptions_MPIAIJ_MPIDense_AB(C));
378: break;
379: case MATPRODUCT_AtB:
380: PetscCall(MatProductSetFromOptions_MPIAIJ_MPIDense_AtB(C));
381: break;
382: default:
383: break;
384: }
385: PetscFunctionReturn(PETSC_SUCCESS);
386: }
388: PETSC_INTERN PetscErrorCode MatMPIDenseScatterDestroy_Private(MPIAIJ_MPIDense *contents)
389: {
390: PetscFunctionBegin;
391: PetscCall(MatDestroy(&contents->workB));
392: PetscCall(PetscSFDestroy(&contents->sf[0]));
393: PetscCall(PetscSFDestroy(&contents->sf[1]));
394: for (PetscInt i = 0; i < contents->nsends; i++) PetscCallMPI(MPI_Type_free(&contents->stype[i]));
395: for (PetscInt i = 0; i < contents->nrecvs; i++) PetscCallMPI(MPI_Type_free(&contents->rtype[i]));
396: PetscCall(PetscFree4(contents->stype, contents->rtype, contents->rwaits, contents->swaits));
397: PetscFunctionReturn(PETSC_SUCCESS);
398: }
400: typedef struct {
401: MPIAIJ_MPIDense scatter;
402: Mat workC; /* off-diagonal contribution A_o * workB on the device route, NULL otherwise */
403: } MPIAIJ_MPIDense_AB;
405: static PetscErrorCode MatMPIAIJ_MPIDenseDestroy(PetscCtxRt ctx)
406: {
407: MPIAIJ_MPIDense_AB *data = *(MPIAIJ_MPIDense_AB **)ctx;
409: PetscFunctionBegin;
410: PetscCall(MatDestroy(&data->workC));
411: PetscCall(MatMPIDenseScatterDestroy_Private(&data->scatter));
412: PetscCall(PetscFree(data));
413: PetscFunctionReturn(PETSC_SUCCESS);
414: }
416: /* Creates the parts of the scatter that depend on contents->blda: the strided PetscSFs on the device, the send datatypes on the host */
417: static PetscErrorCode MatMPIDenseScatterSetUpLDA_Private(VecScatter ctx, PetscInt bs, Mat B, MPIAIJ_MPIDense *contents)
418: {
419: PetscInt nrows = contents->workB->rmap->n;
420: MPI_Datatype type1;
421: const PetscInt *sindices, *sstarts;
422: PetscMPIInt *disp;
423: PetscMPIInt nsends, nrows_to, bs_mpi;
425: PetscFunctionBegin;
426: if (contents->ondevice) {
427: PetscCheck(ctx->vscat.bs <= 1 || contents->blda % ctx->vscat.bs == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Leading dimension %" PetscInt_FMT " of the dense matrix must be a multiple of the block size %" PetscInt_FMT, contents->blda, ctx->vscat.bs);
428: PetscCall(PetscSFCreateStridedSF(ctx, contents->ncols[0], contents->blda, nrows, &contents->sf[0]));
429: if (contents->ncols[1] > 0) PetscCall(PetscSFCreateStridedSF(ctx, contents->ncols[1], contents->blda, nrows, &contents->sf[1]));
430: } else {
431: PetscCall(VecScatterGetRemote_Private(ctx, PETSC_TRUE, &nsends, &sstarts, &sindices, NULL, NULL));
432: PetscCall(PetscMalloc1(PetscMax(B->rmap->n, 1), &disp));
433: PetscCall(PetscMPIIntCast(bs, &bs_mpi));
434: for (PetscMPIInt i = 0; i < nsends; i++) {
435: PetscCall(PetscMPIIntCast(sstarts[i + 1] - sstarts[i], &nrows_to));
436: for (PetscInt j = 0; j < nrows_to; j++) PetscCall(PetscMPIIntCast(sindices[sstarts[i] + j] * bs, &disp[j]));
437: PetscCallMPI(MPI_Type_create_indexed_block(nrows_to, bs_mpi, disp, MPIU_SCALAR, &type1));
438: PetscCallMPI(MPI_Type_create_resized(type1, 0, contents->blda * sizeof(PetscScalar), &contents->stype[i]));
439: PetscCallMPI(MPI_Type_commit(&contents->stype[i]));
440: PetscCallMPI(MPI_Type_free(&type1));
441: }
442: PetscCall(PetscFree(disp));
443: PetscCall(VecScatterRestoreRemote_Private(ctx, PETSC_TRUE, &nsends, &sstarts, &sindices, NULL, NULL));
444: }
445: PetscFunctionReturn(PETSC_SUCCESS);
446: }
448: /* MatMPIDenseGetBatchSize_Private - Gets the number of columns of B to process at a time, to reduce the memory of a work matrix with nrows rows
450: Collective
452: Input Parameters:
453: + nrows - number of rows of the work matrix on this process
454: . Am - number of local rows of the sparse matrix A of the product
455: . B - the MATMPIDENSE input matrix
456: - C - the product matrix, whose prefix is used for -matproduct_batch_size
458: Output Parameters:
459: + batchSize - number of columns of B per batch; 0 or the number of columns of B means all the columns are processed at once
460: - numBatches - number of full batches, or 0 if B is not split into batches; a smaller last batch holds the remaining columns
462: Level: developer
464: Note:
465: Each process chooses a batch size that keeps its work matrix to about twice the memory of Am rows with all the columns of B,
466: rounded down to a multiple of the column block size of B. A process with no work rows chooses all the columns. The largest
467: choice over all processes is used, so B is split only if every process wants it split, unless -matproduct_batch_size gives another size.
469: .seealso: `MatMPIDenseScatterSetUp_Private()`, `MatTransposeMatMultSymbolic_MPIAIJ_MPIDense()`
470: */
471: PETSC_INTERN PetscErrorCode MatMPIDenseGetBatchSize_Private(PetscInt nrows, PetscInt Am, Mat B, Mat C, PetscInt *batchSize, PetscInt *numBatches)
472: {
473: PetscInt BN = B->cmap->N, Bbn, Bbs, numBb;
474: MPI_Comm comm;
476: PetscFunctionBegin;
477: PetscCall(PetscObjectGetComm((PetscObject)C, &comm));
478: if (nrows > 0) {
479: Bbn = 2 * Am * BN / nrows;
480: if (Bbn == 0) Bbn = 1;
481: } else Bbn = BN;
482: Bbs = B->cmap->bs;
483: Bbn = Bbn / Bbs * Bbs;
484: if (Bbn > BN) Bbn = BN;
485: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &Bbn, 1, MPIU_INT, MPI_MAX, comm));
487: /* Enable runtime option for Bbn */
488: PetscOptionsBegin(comm, ((PetscObject)C)->prefix, "MatProduct", "Mat");
489: PetscCall(PetscOptionsDeprecated("-matmatmult_Bbn", "-matproduct_batch_size", "3.25", NULL));
490: PetscCall(PetscOptionsBoundedInt("-matproduct_batch_size", "Number of dense columns per batch", "MatProduct", Bbn, &Bbn, NULL, 0));
491: PetscOptionsEnd();
492: Bbn = PetscMin(Bbn, BN);
494: if (Bbn > 0 && Bbn < BN) numBb = BN / Bbn;
495: else numBb = 0;
496: if (numBb > 0) PetscCall(PetscInfo(C, "Using column batches of size %" PetscInt_FMT " for %" PetscInt_FMT " dense columns\n", Bbn, BN));
497: *batchSize = Bbn;
498: *numBatches = numBb;
499: PetscFunctionReturn(PETSC_SUCCESS);
500: }
502: PETSC_INTERN PetscErrorCode MatMPIDenseScatterSetUp_Private(VecScatter ctx, PetscInt nrows, PetscInt bs, PetscInt Am, Mat B, Mat C, MPIAIJ_MPIDense *contents, PetscInt *batchSize, PetscInt *numBatches)
503: {
504: PetscInt BN = B->cmap->N, Bbn, numBb, ncols;
505: MPI_Datatype type1;
506: const PetscInt *rstarts;
507: PetscMPIInt nsends, nrecvs, nrows_from, disp = 0;
508: PetscBool flg;
510: PetscFunctionBegin;
511: PetscCall(MatDenseGetLDA(B, &contents->blda));
512: PetscCall(PetscObjectGetId((PetscObject)B, &contents->bid));
513: /* Create column block of B and C for memory scalability when BN is too large */
514: PetscCall(MatMPIDenseGetBatchSize_Private(nrows, Am, B, C, &Bbn, &numBb));
515: ncols = Bbn > 0 ? Bbn : BN;
517: /* Create work matrix used to store off processor rows of B needed for local product, with the same type as the local block of B */
518: PetscCall(MatCreate(PETSC_COMM_SELF, &contents->workB));
519: PetscCall(MatSetSizes(contents->workB, nrows, ncols, nrows, ncols));
520: PetscCall(MatSetType(contents->workB, ((PetscObject)((Mat_MPIDense *)B->data)->A)->type_name));
521: PetscCall(MatSetUp(contents->workB));
523: PetscCall(PetscObjectTypeCompare((PetscObject)((Mat_MPIDense *)B->data)->A, MATSEQDENSE, &flg));
524: contents->ondevice = (PetscBool)!flg;
525: if (contents->ondevice) {
526: /* Use strided PetscSFs, which support device memory, instead of the MPI derived data types below */
527: contents->ncols[0] = ncols;
528: if (numBb > 0 && BN % Bbn != 0) contents->ncols[1] = BN % Bbn;
529: } else {
530: PetscCall(VecScatterGetRemote_Private(ctx, PETSC_TRUE, &nsends, NULL, NULL, NULL, NULL));
531: PetscCall(VecScatterGetRemoteOrdered_Private(ctx, PETSC_FALSE, &nrecvs, &rstarts, NULL, NULL, NULL));
533: /* Use MPI derived data type to reduce memory required by the send/recv buffers */
534: PetscCall(PetscMalloc4(nsends, &contents->stype, nrecvs, &contents->rtype, nrecvs, &contents->rwaits, nsends, &contents->swaits));
535: contents->nsends = nsends;
536: contents->nrecvs = nrecvs;
538: for (PetscMPIInt i = 0; i < nrecvs; i++) {
539: /* received values from a process form a (nrows_from x Bbn) row block in workB (column-wise) */
540: PetscCall(PetscMPIIntCast((rstarts[i + 1] - rstarts[i]) * bs, &nrows_from));
541: PetscCallMPI(MPI_Type_create_indexed_block(1, nrows_from, &disp, MPIU_SCALAR, &type1));
542: PetscCallMPI(MPI_Type_create_resized(type1, 0, nrows * sizeof(PetscScalar), &contents->rtype[i]));
543: PetscCallMPI(MPI_Type_commit(&contents->rtype[i]));
544: PetscCallMPI(MPI_Type_free(&type1));
545: }
547: PetscCall(VecScatterRestoreRemote_Private(ctx, PETSC_TRUE /*send*/, &nsends, NULL, NULL, NULL, NULL));
548: PetscCall(VecScatterRestoreRemoteOrdered_Private(ctx, PETSC_FALSE /*recv*/, &nrecvs, &rstarts, NULL, NULL, NULL));
549: }
550: PetscCall(MatMPIDenseScatterSetUpLDA_Private(ctx, bs, B, contents));
551: if (batchSize) *batchSize = Bbn;
552: if (numBatches) *numBatches = numBb;
553: PetscFunctionReturn(PETSC_SUCCESS);
554: }
556: /* Recreates the parts of the scatter that depend on the leading dimension when a new input B has a different one */
557: PETSC_INTERN PetscErrorCode MatMPIDenseScatterUpdateLDA_Private(VecScatter ctx, PetscInt bs, Mat B, Mat C, MPIAIJ_MPIDense *contents)
558: {
559: PetscObjectId id;
560: PetscInt blda;
561: PetscBool update;
563: PetscFunctionBegin;
564: PetscCall(PetscObjectGetId((PetscObject)B, &id));
565: if (id == contents->bid) PetscFunctionReturn(PETSC_SUCCESS);
566: PetscCall(MatDenseGetLDA(B, &blda));
567: update = (PetscBool)(blda != contents->blda);
568: if (contents->ondevice) PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &update, 1, MPI_C_BOOL, MPI_LOR, PetscObjectComm((PetscObject)C)));
569: if (update) {
570: PetscCall(PetscInfo(C, "Setting up the scatter again for a dense input with leading dimension %" PetscInt_FMT " instead of %" PetscInt_FMT "\n", blda, contents->blda));
571: PetscCall(PetscSFDestroy(&contents->sf[0]));
572: PetscCall(PetscSFDestroy(&contents->sf[1]));
573: for (PetscInt i = 0; i < contents->nsends; i++) PetscCallMPI(MPI_Type_free(&contents->stype[i]));
574: contents->blda = blda;
575: PetscCall(MatMPIDenseScatterSetUpLDA_Private(ctx, bs, B, contents));
576: }
577: contents->bid = id;
578: PetscFunctionReturn(PETSC_SUCCESS);
579: }
581: static PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIDense(Mat A, Mat B, PetscReal fill, Mat C)
582: {
583: Mat_MPIAIJ *aij = (Mat_MPIAIJ *)A->data;
584: MPIAIJ_MPIDense_AB *data;
585: PetscInt nz = aij->B->cmap->n;
586: VecScatter ctx = aij->Mvctx;
587: PetscInt Am = A->rmap->n, BN = B->cmap->N, Bbn, numBb;
588: Mat workB1, workC1;
589: const char *ctype;
590: PetscBool cisdense;
592: PetscFunctionBegin;
593: MatCheckProduct(C, 4);
594: PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty");
595: PetscCall(PetscObjectBaseTypeCompare((PetscObject)C, MATMPIDENSE, &cisdense));
596: if (!cisdense) {
597: PetscCall(MatSetType(C, ((PetscObject)B)->type_name));
598: PetscCall(MatSetVecType(C, B->defaultvectype));
599: }
600: PetscCall(MatSetSizes(C, Am, B->cmap->n, A->rmap->N, BN));
601: PetscCall(MatSetBlockSizesFromMats(C, A, B));
602: PetscCall(MatSetUp(C));
603: /* The cuSPARSE and hipSPARSE symbolic below re-types a host local block of C in place, so capture the type
604: of the local block of C now to give the work matrix the type the numeric will add it to */
605: ctype = ((PetscObject)((Mat_MPIDense *)C->data)->A)->type_name;
606: PetscCall(PetscNew(&data));
607: PetscCall(MatMPIDenseScatterSetUp_Private(ctx, nz, 1, Am, B, C, &data->scatter, &Bbn, &numBb));
608: PetscCall(MatProductClear(aij->A));
609: PetscCall(MatProductClear(((Mat_MPIDense *)B->data)->A));
610: PetscCall(MatProductClear(((Mat_MPIDense *)C->data)->A));
611: if (data->scatter.ondevice && nz) {
612: /* On the device route the off-diagonal contribution is computed into a work matrix and added to the local
613: block of C, rather than accumulated in place by a host kernel */
614: PetscCall(MatCreate(PETSC_COMM_SELF, &data->workC));
615: PetscCall(MatSetSizes(data->workC, Am, Bbn ? Bbn : BN, Am, Bbn ? Bbn : BN));
616: PetscCall(MatSetType(data->workC, ctype));
617: PetscCall(MatSetUp(data->workC));
618: PetscCall(MatProductCreateWithMat(aij->B, data->scatter.workB, NULL, data->workC));
619: PetscCall(MatProductSetType(data->workC, MATPRODUCT_AB));
620: PetscCall(MatProductSetFromOptions(data->workC));
621: PetscCall(MatProductSymbolic(data->workC));
622: if (numBb && BN % Bbn) {
623: /* the last column batch is smaller, set up the product on the sub-matrices the numeric will use for it */
624: PetscCall(MatDenseGetSubMatrix(data->scatter.workB, PETSC_DECIDE, PETSC_DECIDE, 0, BN % Bbn, &workB1));
625: PetscCall(MatDenseGetSubMatrix(data->workC, PETSC_DECIDE, PETSC_DECIDE, 0, BN % Bbn, &workC1));
626: PetscCall(MatProductCreateWithMat(aij->B, workB1, NULL, workC1));
627: PetscCall(MatProductSetType(workC1, MATPRODUCT_AB));
628: PetscCall(MatProductSetFromOptions(workC1));
629: PetscCall(MatProductSymbolic(workC1));
630: PetscCall(MatDenseRestoreSubMatrix(data->workC, &workC1));
631: PetscCall(MatDenseRestoreSubMatrix(data->scatter.workB, &workB1));
632: }
633: }
634: PetscCall(MatProductCreateWithMat(aij->A, ((Mat_MPIDense *)B->data)->A, NULL, ((Mat_MPIDense *)C->data)->A));
635: PetscCall(MatProductSetType(((Mat_MPIDense *)C->data)->A, MATPRODUCT_AB));
636: PetscCall(MatProductSetFromOptions(((Mat_MPIDense *)C->data)->A));
637: PetscCall(MatProductSymbolic(((Mat_MPIDense *)C->data)->A));
638: C->product->data = data;
639: C->product->destroy = MatMPIAIJ_MPIDenseDestroy;
640: C->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIDense;
641: PetscFunctionReturn(PETSC_SUCCESS);
642: }
644: PETSC_INTERN PetscErrorCode MatMatMultNumericAdd_SeqAIJ_SeqDense(Mat, Mat, Mat, const PetscBool);
646: /*
647: Performs an efficient scatter on the rows of B needed by this process; this is
648: a modification of the VecScatterBegin_() routines.
649: */
650: static PetscErrorCode MatMPIDenseScatterHost_Private(VecScatter ctx, PetscInt bs, Mat workB, MPIAIJ_MPIDense *contents, Mat B, Mat C)
651: {
652: const PetscScalar *b;
653: PetscScalar *rvalues;
654: const PetscInt *sindices, *sstarts, *rstarts;
655: const PetscMPIInt *sprocs, *rprocs;
656: PetscMPIInt nsends, nrecvs;
657: MPI_Comm comm;
658: PetscMPIInt tag = ((PetscObject)ctx)->tag, ncols, nsends_mpi, nrecvs_mpi;
660: PetscFunctionBegin;
661: PetscCall(PetscMPIIntCast(B->cmap->N, &ncols));
662: PetscCall(VecScatterGetRemote_Private(ctx, PETSC_TRUE /*send*/, &nsends, &sstarts, &sindices, &sprocs, NULL /*bs*/));
663: PetscCall(VecScatterGetRemoteOrdered_Private(ctx, PETSC_FALSE /*recv*/, &nrecvs, &rstarts, NULL, &rprocs, NULL /*bs*/));
664: PetscCall(PetscMPIIntCast(nsends, &nsends_mpi));
665: PetscCall(PetscMPIIntCast(nrecvs, &nrecvs_mpi));
667: PetscCall(MatDenseGetArrayRead(B, &b));
668: PetscCall(MatDenseGetArray(workB, &rvalues));
670: /* Post recv, use MPI derived data type to save memory */
671: PetscCall(PetscObjectGetComm((PetscObject)C, &comm));
672: for (PetscMPIInt i = 0; i < nrecvs; i++) PetscCallMPI(MPIU_Irecv(rvalues + ((rstarts[i] - rstarts[0]) * bs), ncols, contents->rtype[i], rprocs[i], tag, comm, contents->rwaits + i));
673: for (PetscMPIInt i = 0; i < nsends; i++) PetscCallMPI(MPIU_Isend(b, ncols, contents->stype[i], sprocs[i], tag, comm, contents->swaits + i));
675: if (nrecvs) PetscCallMPI(MPI_Waitall(nrecvs_mpi, contents->rwaits, MPI_STATUSES_IGNORE));
676: if (nsends) PetscCallMPI(MPI_Waitall(nsends_mpi, contents->swaits, MPI_STATUSES_IGNORE));
678: PetscCall(VecScatterRestoreRemote_Private(ctx, PETSC_TRUE /*send*/, &nsends, &sstarts, &sindices, &sprocs, NULL));
679: PetscCall(VecScatterRestoreRemoteOrdered_Private(ctx, PETSC_FALSE /*recv*/, &nrecvs, &rstarts, NULL, &rprocs, NULL));
680: PetscCall(MatDenseRestoreArrayRead(B, &b));
681: PetscCall(MatDenseRestoreArray(workB, &rvalues));
682: PetscFunctionReturn(PETSC_SUCCESS);
683: }
685: /*
686: Same as MatMPIDenseScatterHost_Private(), but using the strided PetscSFs set up in
687: MatMPIDenseScatterSetUp_Private() so that the data never leaves the device. The scatter is split in two
688: halves so that a caller can run other work while the off-process rows of B are in flight
689: */
690: static PetscErrorCode MatMPIDenseScatterDeviceBegin_Private(Mat workB, MPIAIJ_MPIDense *contents, Mat B)
691: {
692: PetscSF sf;
693: PetscInt k = 0;
694: PetscMemType bmtype, wmtype;
696: PetscFunctionBegin;
697: PetscCheck(!contents->sfinuse, PETSC_COMM_SELF, PETSC_ERR_PLIB, "A scatter is already in flight");
698: if (B->cmap->N != contents->ncols[0]) k = 1;
699: PetscCheck(contents->sf[k] && contents->ncols[k] == B->cmap->N, PETSC_COMM_SELF, PETSC_ERR_PLIB, "No scatter set up for %" PetscInt_FMT " columns", B->cmap->N);
700: sf = contents->sf[k];
701: /* every entry of workB is overwritten, so write-only access is enough */
702: PetscCall(MatDenseGetArrayReadAndMemType(B, &contents->barray, &bmtype));
703: PetscCall(MatDenseGetArrayWriteAndMemType(workB, &contents->warray, &wmtype));
704: PetscCall(PetscSFBcastWithMemTypeBegin(sf, sf->vscat.unit, bmtype, contents->barray, wmtype, contents->warray, MPI_REPLACE));
705: contents->sfinuse = sf;
706: PetscFunctionReturn(PETSC_SUCCESS);
707: }
709: static PetscErrorCode MatMPIDenseScatterDeviceEnd_Private(Mat workB, MPIAIJ_MPIDense *contents, Mat B)
710: {
711: PetscSF sf = contents->sfinuse;
713: PetscFunctionBegin;
714: PetscCheck(sf, PETSC_COMM_SELF, PETSC_ERR_PLIB, "No scatter in flight");
715: PetscCall(PetscSFBcastEnd(sf, sf->vscat.unit, contents->barray, contents->warray, MPI_REPLACE));
716: PetscCall(MatDenseRestoreArrayWriteAndMemType(workB, &contents->warray));
717: PetscCall(MatDenseRestoreArrayReadAndMemType(B, &contents->barray));
718: contents->sfinuse = NULL;
719: PetscFunctionReturn(PETSC_SUCCESS);
720: }
722: /*
723: The checks the host and device scatters share; on the device route they are done by the begin half
724: */
725: static PetscErrorCode MatMPIDenseScatterCheck_Private(PetscInt nrows, Mat workB, MPIAIJ_MPIDense *contents, Mat B, Mat C)
726: {
727: PetscInt blda;
729: PetscFunctionBegin;
730: MatCheckProduct(C, 5);
731: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
732: PetscCheck(nrows == workB->rmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of rows of workB %" PetscInt_FMT " not equal to columns of off-diagonal block %" PetscInt_FMT, workB->rmap->n, nrows);
733: PetscCall(MatDenseGetLDA(B, &blda));
734: PetscCheck(blda == contents->blda, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot reuse an input matrix with lda %" PetscInt_FMT " != %" PetscInt_FMT, blda, contents->blda);
735: PetscFunctionReturn(PETSC_SUCCESS);
736: }
738: PETSC_INTERN PetscErrorCode MatMPIDenseScatter_Private(VecScatter ctx, PetscInt nrows, PetscInt bs, Mat workB, MPIAIJ_MPIDense *contents, Mat B, Mat C)
739: {
740: PetscFunctionBegin;
741: PetscCall(MatMPIDenseScatterCheck_Private(nrows, workB, contents, B, C));
742: if (contents->ondevice) {
743: PetscCall(MatMPIDenseScatterDeviceBegin_Private(workB, contents, B));
744: PetscCall(MatMPIDenseScatterDeviceEnd_Private(workB, contents, B));
745: } else PetscCall(MatMPIDenseScatterHost_Private(ctx, bs, workB, contents, B, C));
746: PetscFunctionReturn(PETSC_SUCCESS);
747: }
749: /*
750: Starts the scatter of the off-process rows of B into workB. On the device route only the begin half of the
751: PetscSF broadcast is posted, so the caller must complete it with MatMPIDenseScatterEnd(); the host scatter
752: is blocking and is done entirely here
753: */
754: static PetscErrorCode MatMPIDenseScatterBegin(Mat A, Mat B, Mat workB, Mat C)
755: {
756: Mat_MPIAIJ *aij = (Mat_MPIAIJ *)A->data;
757: MPIAIJ_MPIDense *contents = &((MPIAIJ_MPIDense_AB *)C->product->data)->scatter;
759: PetscFunctionBegin;
760: if (contents->ondevice) {
761: PetscCall(MatMPIDenseScatterCheck_Private(aij->B->cmap->n, workB, contents, B, C));
762: PetscCall(MatMPIDenseScatterDeviceBegin_Private(workB, contents, B));
763: } else PetscCall(MatMPIDenseScatter_Private(aij->Mvctx, aij->B->cmap->n, 1, workB, contents, B, C));
764: PetscFunctionReturn(PETSC_SUCCESS);
765: }
767: static PetscErrorCode MatMPIDenseScatterEnd(Mat B, Mat workB, Mat C)
768: {
769: MPIAIJ_MPIDense *contents = &((MPIAIJ_MPIDense_AB *)C->product->data)->scatter;
771: PetscFunctionBegin;
772: if (contents->ondevice) PetscCall(MatMPIDenseScatterDeviceEnd_Private(workB, contents, B));
773: PetscFunctionReturn(PETSC_SUCCESS);
774: }
776: /* MatMPIAIJ_MPIDenseProductNumeric_Private - Computes C = A B or C = A^T B for the sequential matrices inside a MATMPIAIJ times MATMPIDENSE product
778: Not Collective
780: Input Parameters:
781: + A - a diagonal or off-diagonal block of the MATMPIAIJ matrix
782: . B - the sequential dense input: the local block of B, one batch of its columns, or the off-process rows of B
783: . C - the sequential dense result, whose product is set up here if it has none
784: . ptype - MATPRODUCT_AB or MATPRODUCT_AtB
785: - clear - whether to clear the product of C after the numeric phase, so that the next call sets it up again
787: Level: developer
789: Note:
790: The product is also set up again when A is not the matrix it was set up with, since its symbolic phase may keep data built from A.
792: .seealso: `MatMatMultNumeric_MPIAIJ_MPIDense()`, `MatTransposeMatMultNumeric_MPIAIJ_MPIDense()`, `MatProductClear()`
793: */
794: PETSC_INTERN PetscErrorCode MatMPIAIJ_MPIDenseProductNumeric_Private(Mat A, Mat B, Mat C, MatProductType ptype, PetscBool clear)
795: {
796: PetscFunctionBegin;
797: if (C->product != NULL && C->product->A != A) PetscCall(MatProductClear(C));
798: if (C->product == NULL) {
799: PetscCall(MatProductCreateWithMat(A, B, NULL, C));
800: PetscCall(MatProductSetType(C, ptype));
801: PetscCall(MatProductSetFromOptions(C));
802: PetscCall(MatProductSymbolic(C));
803: } else PetscCall(MatProductReplaceMats(A, B, NULL, C));
804: if (clear && !C->product->clear) C->product->clear = PETSC_TRUE;
805: PetscCall(MatProductNumeric(C));
806: PetscFunctionReturn(PETSC_SUCCESS);
807: }
809: static PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIDense(Mat A, Mat B, Mat C)
810: {
811: Mat_MPIAIJ *aij = (Mat_MPIAIJ *)A->data;
812: Mat_MPIDense *bdense = (Mat_MPIDense *)B->data;
813: Mat_MPIDense *cdense = (Mat_MPIDense *)C->data;
814: Mat workB;
815: MPIAIJ_MPIDense *contents;
816: MPIAIJ_MPIDense_AB *data;
817: PetscBool clear = PETSC_FALSE, flg, overlap;
819: PetscFunctionBegin;
820: MatCheckProduct(C, 3);
821: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
822: data = (MPIAIJ_MPIDense_AB *)C->product->data;
823: contents = &data->scatter;
824: if (PetscDefined(HAVE_CUPM)) {
825: PetscCall(PetscObjectTypeCompare((PetscObject)C, MATMPIDENSE, &flg));
826: if (flg) PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPIAIJ, &flg));
827: clear = (PetscBool)!flg; /* if either A or C is a device Mat, make sure MatProductClear() is called */
828: }
829: /* The device scatter is non-blocking, so when there are no column batches start it before the product of the
830: diagonal block below to overlap the communication with that product, as MatMult_MPIAIJ() does. The host
831: scatter is blocking, so it is left where it is */
832: PetscCall(MatMPIDenseScatterUpdateLDA_Private(aij->Mvctx, 1, B, C, contents));
833: overlap = (PetscBool)(contents->ondevice && contents->workB->cmap->n == B->cmap->N);
834: if (overlap) PetscCall(MatMPIDenseScatterBegin(A, B, contents->workB, C));
835: /* diagonal block of A times all local rows of B, first make sure that everything is up-to-date */
836: PetscCall(MatMPIAIJ_MPIDenseProductNumeric_Private(aij->A, bdense->A, cdense->A, MATPRODUCT_AB, clear));
837: if (contents->workB->cmap->n == B->cmap->N) {
838: /* get off processor parts of B needed to complete C=A*B */
839: workB = contents->workB;
840: if (!overlap) PetscCall(MatMPIDenseScatterBegin(A, B, workB, C));
841: PetscCall(MatMPIDenseScatterEnd(B, workB, C));
843: /* off-diagonal block of A times nonlocal rows of B */
844: if (data->workC) {
845: PetscCall(MatMPIAIJ_MPIDenseProductNumeric_Private(aij->B, workB, data->workC, MATPRODUCT_AB, clear));
846: PetscCall(MatAXPY(cdense->A, 1.0, data->workC, SAME_NONZERO_PATTERN));
847: } else if (!contents->ondevice) PetscCall(MatMatMultNumericAdd_SeqAIJ_SeqDense(aij->B, workB, cdense->A, PETSC_TRUE)); /* the device route has no work matrix only when the off-diagonal block has no columns */
848: } else {
849: Mat Bb, Cb, workC;
850: PetscInt BN = B->cmap->N, n = contents->workB->cmap->n, cols;
851: PetscBool ccpu = PETSC_FALSE;
853: PetscCheck(n > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Column block size %" PetscInt_FMT " must be positive", n);
854: /* The host route accumulates into the local block of C on the host, bind C to the CPU to avoid copies back
855: and forth from the device when getting and restoring the sub-matrices */
856: if (!contents->ondevice) {
857: PetscCall(MatBoundToCPU(C, &ccpu));
858: PetscCall(MatBindToCPU(C, PETSC_TRUE));
859: }
860: for (PetscInt i = 0; i < BN; i += n) {
861: cols = PetscMin(n, BN - i);
862: workB = contents->workB;
863: workC = data->workC;
864: if (cols != n) {
865: PetscCall(MatDenseGetSubMatrix(contents->workB, PETSC_DECIDE, PETSC_DECIDE, 0, cols, &workB));
866: if (workC) PetscCall(MatDenseGetSubMatrix(data->workC, PETSC_DECIDE, PETSC_DECIDE, 0, cols, &workC));
867: }
868: PetscCall(MatDenseGetSubMatrix(B, PETSC_DECIDE, PETSC_DECIDE, i, i + cols, &Bb));
869: PetscCall(MatDenseGetSubMatrix(C, PETSC_DECIDE, PETSC_DECIDE, i, i + cols, &Cb));
871: /* get off processor parts of B needed to complete C=A*B */
872: PetscCall(MatMPIDenseScatterBegin(A, Bb, workB, C));
873: PetscCall(MatMPIDenseScatterEnd(Bb, workB, C));
875: /* off-diagonal block of A times nonlocal rows of B */
876: if (workC) {
877: PetscCall(MatMPIAIJ_MPIDenseProductNumeric_Private(aij->B, workB, workC, MATPRODUCT_AB, clear));
878: PetscCall(MatAXPY(((Mat_MPIDense *)Cb->data)->A, 1.0, workC, SAME_NONZERO_PATTERN));
879: } else if (!contents->ondevice) PetscCall(MatMatMultNumericAdd_SeqAIJ_SeqDense(aij->B, workB, ((Mat_MPIDense *)Cb->data)->A, PETSC_TRUE));
880: if (cols != n) {
881: if (workC) PetscCall(MatDenseRestoreSubMatrix(data->workC, &workC));
882: PetscCall(MatDenseRestoreSubMatrix(contents->workB, &workB));
883: }
884: PetscCall(MatDenseRestoreSubMatrix(B, &Bb));
885: PetscCall(MatDenseRestoreSubMatrix(C, &Cb));
886: }
887: if (!contents->ondevice) PetscCall(MatBindToCPU(C, ccpu));
888: }
889: PetscFunctionReturn(PETSC_SUCCESS);
890: }
892: PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIAIJ(Mat A, Mat P, Mat C)
893: {
894: Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data, *c = (Mat_MPIAIJ *)C->data;
895: Mat_SeqAIJ *ad = (Mat_SeqAIJ *)a->A->data, *ao = (Mat_SeqAIJ *)a->B->data;
896: Mat_SeqAIJ *cd = (Mat_SeqAIJ *)c->A->data, *co = (Mat_SeqAIJ *)c->B->data;
897: PetscInt *adi = ad->i, *adj, *aoi = ao->i, *aoj;
898: PetscScalar *ada, *aoa, *cda = cd->a, *coa = co->a;
899: Mat_SeqAIJ *p_loc, *p_oth;
900: PetscInt *pi_loc, *pj_loc, *pi_oth, *pj_oth, *pj;
901: PetscScalar *pa_loc, *pa_oth, *pa, valtmp, *ca;
902: PetscInt cm = C->rmap->n, anz, pnz;
903: MatProductCtx_APMPI *ptap;
904: PetscScalar *apa_sparse;
905: const PetscScalar *dummy;
906: PetscInt *api, *apj, *apJ, i, j, k, row;
907: PetscInt cstart = C->cmap->rstart;
908: PetscInt cdnz, conz, k0, k1, nextp;
909: MPI_Comm comm;
910: PetscMPIInt size;
912: PetscFunctionBegin;
913: MatCheckProduct(C, 3);
914: ptap = (MatProductCtx_APMPI *)C->product->data;
915: PetscCheck(ptap, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "PtAP cannot be computed. Missing data");
916: PetscCall(PetscObjectGetComm((PetscObject)C, &comm));
917: PetscCallMPI(MPI_Comm_size(comm, &size));
918: PetscCheck(ptap->P_oth || size <= 1, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "AP cannot be reused. Do not call MatProductClear()");
920: /* flag CPU mask for C */
921: #if PetscDefined(HAVE_DEVICE)
922: if (C->offloadmask != PETSC_OFFLOAD_UNALLOCATED) C->offloadmask = PETSC_OFFLOAD_CPU;
923: if (c->A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) c->A->offloadmask = PETSC_OFFLOAD_CPU;
924: if (c->B->offloadmask != PETSC_OFFLOAD_UNALLOCATED) c->B->offloadmask = PETSC_OFFLOAD_CPU;
925: #endif
926: apa_sparse = ptap->apa;
928: /* 1) get P_oth = ptap->P_oth and P_loc = ptap->P_loc */
929: /* update numerical values of P_oth and P_loc */
930: PetscCall(MatGetBrowsOfAoCols_MPIAIJ(A, P, MAT_REUSE_MATRIX, &ptap->startsj_s, &ptap->startsj_r, &ptap->bufa, &ptap->P_oth));
931: PetscCall(MatMPIAIJGetLocalMat(P, MAT_REUSE_MATRIX, &ptap->P_loc));
933: /* 2) compute numeric C_loc = A_loc*P = Ad*P_loc + Ao*P_oth */
934: /* get data from symbolic products */
935: p_loc = (Mat_SeqAIJ *)ptap->P_loc->data;
936: pi_loc = p_loc->i;
937: pj_loc = p_loc->j;
938: pa_loc = p_loc->a;
939: if (size > 1) {
940: p_oth = (Mat_SeqAIJ *)ptap->P_oth->data;
941: pi_oth = p_oth->i;
942: pj_oth = p_oth->j;
943: pa_oth = p_oth->a;
944: } else {
945: p_oth = NULL;
946: pi_oth = NULL;
947: pj_oth = NULL;
948: pa_oth = NULL;
949: }
951: /* trigger copy to CPU */
952: PetscCall(MatSeqAIJGetArrayRead(a->A, &dummy));
953: PetscCall(MatSeqAIJRestoreArrayRead(a->A, &dummy));
954: PetscCall(MatSeqAIJGetArrayRead(a->B, &dummy));
955: PetscCall(MatSeqAIJRestoreArrayRead(a->B, &dummy));
956: api = ptap->api;
957: apj = ptap->apj;
958: for (i = 0; i < cm; i++) {
959: apJ = apj + api[i];
961: /* diagonal portion of A */
962: anz = adi[i + 1] - adi[i];
963: adj = ad->j + adi[i];
964: ada = ad->a + adi[i];
965: for (j = 0; j < anz; j++) {
966: row = adj[j];
967: pnz = pi_loc[row + 1] - pi_loc[row];
968: pj = pj_loc + pi_loc[row];
969: pa = pa_loc + pi_loc[row];
970: /* perform sparse axpy */
971: valtmp = ada[j];
972: nextp = 0;
973: for (k = 0; nextp < pnz; k++) {
974: if (apJ[k] == pj[nextp]) { /* column of AP == column of P */
975: apa_sparse[k] += valtmp * pa[nextp++];
976: }
977: }
978: PetscCall(PetscLogFlops(2.0 * pnz));
979: }
981: /* off-diagonal portion of A */
982: anz = aoi[i + 1] - aoi[i];
983: aoj = PetscSafePointerPlusOffset(ao->j, aoi[i]);
984: aoa = PetscSafePointerPlusOffset(ao->a, aoi[i]);
985: for (j = 0; j < anz; j++) {
986: row = aoj[j];
987: pnz = pi_oth[row + 1] - pi_oth[row];
988: pj = pj_oth + pi_oth[row];
989: pa = pa_oth + pi_oth[row];
990: /* perform sparse axpy */
991: valtmp = aoa[j];
992: nextp = 0;
993: for (k = 0; nextp < pnz; k++) {
994: if (apJ[k] == pj[nextp]) { /* column of AP == column of P */
995: apa_sparse[k] += valtmp * pa[nextp++];
996: }
997: }
998: PetscCall(PetscLogFlops(2.0 * pnz));
999: }
1001: /* set values in C */
1002: cdnz = cd->i[i + 1] - cd->i[i];
1003: conz = co->i[i + 1] - co->i[i];
1005: /* 1st off-diagonal part of C */
1006: ca = PetscSafePointerPlusOffset(coa, co->i[i]);
1007: k = 0;
1008: for (k0 = 0; k0 < conz; k0++) {
1009: if (apJ[k] >= cstart) break;
1010: ca[k0] = apa_sparse[k];
1011: apa_sparse[k] = 0.0;
1012: k++;
1013: }
1015: /* diagonal part of C */
1016: ca = cda + cd->i[i];
1017: for (k1 = 0; k1 < cdnz; k1++) {
1018: ca[k1] = apa_sparse[k];
1019: apa_sparse[k] = 0.0;
1020: k++;
1021: }
1023: /* 2nd off-diagonal part of C */
1024: ca = PetscSafePointerPlusOffset(coa, co->i[i]);
1025: for (; k0 < conz; k0++) {
1026: ca[k0] = apa_sparse[k];
1027: apa_sparse[k] = 0.0;
1028: k++;
1029: }
1030: }
1031: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
1032: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
1033: PetscFunctionReturn(PETSC_SUCCESS);
1034: }
1036: /* same as MatMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(), except using LLCondensed to avoid O(BN) memory requirement */
1037: PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIAIJ(Mat A, Mat P, PetscReal fill, Mat C)
1038: {
1039: MPI_Comm comm;
1040: PetscMPIInt size;
1041: MatProductCtx_APMPI *ptap;
1042: PetscFreeSpaceList free_space = NULL, current_space = NULL;
1043: Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1044: Mat_SeqAIJ *ad = (Mat_SeqAIJ *)a->A->data, *ao = (Mat_SeqAIJ *)a->B->data, *p_loc, *p_oth;
1045: PetscInt *pi_loc, *pj_loc, *pi_oth, *pj_oth, *dnz, *onz;
1046: PetscInt *adi = ad->i, *adj = ad->j, *aoi = ao->i, *aoj = ao->j, rstart = A->rmap->rstart;
1047: PetscInt i, pnz, row, *api, *apj, *Jptr, apnz, nspacedouble = 0, j, nzi, *lnk, apnz_max = 1;
1048: PetscInt am = A->rmap->n, pn = P->cmap->n, pm = P->rmap->n, lsize = pn + 20;
1049: PetscReal afill;
1050: MatType mtype;
1052: PetscFunctionBegin;
1053: MatCheckProduct(C, 4);
1054: PetscCheck(!C->product->data, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Extra product struct not empty");
1055: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
1056: PetscCallMPI(MPI_Comm_size(comm, &size));
1058: /* create struct MatProductCtx_APMPI and attached it to C later */
1059: PetscCall(PetscNew(&ptap));
1061: /* get P_oth by taking rows of P (= non-zero cols of local A) from other processors */
1062: PetscCall(MatGetBrowsOfAoCols_MPIAIJ_Private(A, P, MAT_INITIAL_MATRIX, C->structure_only, &ptap->startsj_s, &ptap->startsj_r, &ptap->bufa, &ptap->P_oth));
1064: /* get P_loc by taking all local rows of P */
1065: PetscCall(MatMPIAIJGetLocalMat_Private(P, MAT_INITIAL_MATRIX, C->structure_only, &ptap->P_loc));
1067: p_loc = (Mat_SeqAIJ *)ptap->P_loc->data;
1068: pi_loc = p_loc->i;
1069: pj_loc = p_loc->j;
1070: if (size > 1) {
1071: p_oth = (Mat_SeqAIJ *)ptap->P_oth->data;
1072: pi_oth = p_oth->i;
1073: pj_oth = p_oth->j;
1074: } else {
1075: p_oth = NULL;
1076: pi_oth = NULL;
1077: pj_oth = NULL;
1078: }
1080: /* first, compute symbolic AP = A_loc*P = A_diag*P_loc + A_off*P_oth */
1081: PetscCall(PetscMalloc1(am + 1, &api));
1082: ptap->api = api;
1083: api[0] = 0;
1085: PetscCall(PetscLLCondensedCreate_Scalable(lsize, &lnk));
1087: /* Initial FreeSpace size is fill*(nnz(A)+nnz(P)) */
1088: PetscCall(PetscFreeSpaceGet(PetscRealIntMultTruncate(fill, PetscIntSumTruncate(adi[am], PetscIntSumTruncate(aoi[am], pi_loc[pm]))), &free_space));
1089: current_space = free_space;
1090: MatPreallocateBegin(comm, am, pn, dnz, onz);
1091: for (i = 0; i < am; i++) {
1092: /* diagonal portion of A */
1093: nzi = adi[i + 1] - adi[i];
1094: for (j = 0; j < nzi; j++) {
1095: row = *adj++;
1096: pnz = pi_loc[row + 1] - pi_loc[row];
1097: Jptr = pj_loc + pi_loc[row];
1098: /* Expand list if it is not long enough */
1099: if (pnz + apnz_max > lsize) {
1100: lsize = pnz + apnz_max;
1101: PetscCall(PetscLLCondensedExpand_Scalable(lsize, &lnk));
1102: }
1103: /* add non-zero cols of P into the sorted linked list lnk */
1104: PetscCall(PetscLLCondensedAddSorted_Scalable(pnz, Jptr, lnk));
1105: apnz = *lnk; /* The first element in the list is the number of items in the list */
1106: api[i + 1] = api[i] + apnz;
1107: if (apnz > apnz_max) apnz_max = apnz + 1; /* '1' for diagonal entry */
1108: }
1109: /* off-diagonal portion of A */
1110: nzi = aoi[i + 1] - aoi[i];
1111: for (j = 0; j < nzi; j++) {
1112: row = *aoj++;
1113: pnz = pi_oth[row + 1] - pi_oth[row];
1114: Jptr = pj_oth + pi_oth[row];
1115: /* Expand list if it is not long enough */
1116: if (pnz + apnz_max > lsize) {
1117: lsize = pnz + apnz_max;
1118: PetscCall(PetscLLCondensedExpand_Scalable(lsize, &lnk));
1119: }
1120: /* add non-zero cols of P into the sorted linked list lnk */
1121: PetscCall(PetscLLCondensedAddSorted_Scalable(pnz, Jptr, lnk));
1122: apnz = *lnk; /* The first element in the list is the number of items in the list */
1123: api[i + 1] = api[i] + apnz;
1124: if (apnz > apnz_max) apnz_max = apnz + 1; /* '1' for diagonal entry */
1125: }
1127: /* add missing diagonal entry */
1128: if (C->force_diagonals) {
1129: j = i + rstart; /* column index */
1130: PetscCall(PetscLLCondensedAddSorted_Scalable(1, &j, lnk));
1131: }
1133: apnz = *lnk;
1134: api[i + 1] = api[i] + apnz;
1135: if (apnz > apnz_max) apnz_max = apnz;
1137: /* if free space is not available, double the total space in the list */
1138: if (current_space->local_remaining < apnz) {
1139: PetscCall(PetscFreeSpaceGet(PetscIntSumTruncate(apnz, current_space->total_array_size), ¤t_space));
1140: nspacedouble++;
1141: }
1143: /* Copy data into free space, then initialize lnk */
1144: PetscCall(PetscLLCondensedClean_Scalable(apnz, current_space->array, lnk));
1145: PetscCall(MatPreallocateSet(i + rstart, apnz, current_space->array, dnz, onz));
1147: current_space->array += apnz;
1148: current_space->local_used += apnz;
1149: current_space->local_remaining -= apnz;
1150: }
1152: /* Allocate space for apj, initialize apj, and */
1153: /* destroy list of free space and other temporary array(s) */
1154: PetscCall(PetscMalloc1(api[am], &ptap->apj));
1155: apj = ptap->apj;
1156: PetscCall(PetscFreeSpaceContiguous(&free_space, ptap->apj));
1157: PetscCall(PetscLLCondensedDestroy_Scalable(lnk));
1159: /* create and assemble symbolic parallel matrix C */
1160: PetscCall(MatSetSizes(C, am, pn, PETSC_DETERMINE, PETSC_DETERMINE));
1161: PetscCall(MatSetBlockSizesFromMats(C, A, P));
1162: PetscCall(MatGetType(A, &mtype));
1163: PetscCall(MatSetType(C, C->structure_only ? MATMPIAIJ : mtype));
1164: PetscCall(MatMPIAIJSetPreallocation(C, 0, dnz, 0, onz));
1165: MatPreallocateEnd(dnz, onz);
1167: /* malloc apa for assembly C */
1168: if (!C->structure_only) PetscCall(PetscCalloc1(apnz_max, &ptap->apa));
1170: PetscCall(MatSetValues_MPIAIJ_CopyFromCSRFormat_Symbolic(C, apj, api));
1171: PetscCall(MatSetOption(C, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
1172: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
1173: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
1174: PetscCall(MatSetOption(C, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1176: C->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIAIJ;
1177: C->ops->productnumeric = MatProductNumeric_AB;
1179: /* attach the supporting struct to C for reuse */
1180: C->product->data = ptap;
1181: C->product->destroy = MatProductCtxDestroy_MPIAIJ_MatMatMult;
1183: /* set MatInfo */
1184: afill = (PetscReal)api[am] / (adi[am] + aoi[am] + pi_loc[pm] + 1) + 1.e-5;
1185: if (afill < 1.0) afill = 1.0;
1186: C->info.mallocs = nspacedouble;
1187: C->info.fill_ratio_given = fill;
1188: C->info.fill_ratio_needed = afill;
1190: if (PetscDefined(USE_INFO)) {
1191: if (api[am]) {
1192: PetscCall(PetscInfo(C, "Reallocs %" PetscInt_FMT "; Fill ratio: given %g needed %g.\n", nspacedouble, (double)fill, (double)afill));
1193: PetscCall(PetscInfo(C, "Use MatMatMult(A,B,MatReuse,%g,&C) for best performance.;\n", (double)afill));
1194: } else PetscCall(PetscInfo(C, "Empty matrix product\n"));
1195: }
1196: PetscFunctionReturn(PETSC_SUCCESS);
1197: }
1199: /* This function is needed for the seqMPI matrix-matrix multiplication. */
1200: /* Three input arrays are merged to one output array. The size of the */
1201: /* output array is also output. Duplicate entries only show up once. */
1202: static void Merge3SortedArrays(PetscInt size1, PetscInt *in1, PetscInt size2, PetscInt *in2, PetscInt size3, PetscInt *in3, PetscInt *size4, PetscInt *out)
1203: {
1204: int i = 0, j = 0, k = 0, l = 0;
1206: /* Traverse all three arrays */
1207: while (i < size1 && j < size2 && k < size3) {
1208: if (in1[i] < in2[j] && in1[i] < in3[k]) {
1209: out[l++] = in1[i++];
1210: } else if (in2[j] < in1[i] && in2[j] < in3[k]) {
1211: out[l++] = in2[j++];
1212: } else if (in3[k] < in1[i] && in3[k] < in2[j]) {
1213: out[l++] = in3[k++];
1214: } else if (in1[i] == in2[j] && in1[i] < in3[k]) {
1215: out[l++] = in1[i];
1216: i++, j++;
1217: } else if (in1[i] == in3[k] && in1[i] < in2[j]) {
1218: out[l++] = in1[i];
1219: i++, k++;
1220: } else if (in3[k] == in2[j] && in2[j] < in1[i]) {
1221: out[l++] = in2[j];
1222: k++, j++;
1223: } else if (in1[i] == in2[j] && in1[i] == in3[k]) {
1224: out[l++] = in1[i];
1225: i++, j++, k++;
1226: }
1227: }
1229: /* Traverse two remaining arrays */
1230: while (i < size1 && j < size2) {
1231: if (in1[i] < in2[j]) {
1232: out[l++] = in1[i++];
1233: } else if (in1[i] > in2[j]) {
1234: out[l++] = in2[j++];
1235: } else {
1236: out[l++] = in1[i];
1237: i++, j++;
1238: }
1239: }
1241: while (i < size1 && k < size3) {
1242: if (in1[i] < in3[k]) {
1243: out[l++] = in1[i++];
1244: } else if (in1[i] > in3[k]) {
1245: out[l++] = in3[k++];
1246: } else {
1247: out[l++] = in1[i];
1248: i++, k++;
1249: }
1250: }
1252: while (k < size3 && j < size2) {
1253: if (in3[k] < in2[j]) {
1254: out[l++] = in3[k++];
1255: } else if (in3[k] > in2[j]) {
1256: out[l++] = in2[j++];
1257: } else {
1258: out[l++] = in3[k];
1259: k++, j++;
1260: }
1261: }
1263: /* Traverse one remaining array */
1264: while (i < size1) out[l++] = in1[i++];
1265: while (j < size2) out[l++] = in2[j++];
1266: while (k < size3) out[l++] = in3[k++];
1268: *size4 = l;
1269: }
1271: /* This matrix-matrix multiplication algorithm divides the multiplication into three multiplications and */
1272: /* adds up the products. Two of these three multiplications are performed with existing (sequential) */
1273: /* matrix-matrix multiplications. */
1274: PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIAIJ_seqMPI(Mat A, Mat P, PetscReal fill, Mat C)
1275: {
1276: MPI_Comm comm;
1277: PetscMPIInt size;
1278: MatProductCtx_APMPI *ptap;
1279: PetscFreeSpaceList free_space_diag = NULL, current_space = NULL;
1280: Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1281: Mat_SeqAIJ *ad = (Mat_SeqAIJ *)a->A->data, *ao = (Mat_SeqAIJ *)a->B->data, *p_loc;
1282: Mat_MPIAIJ *p = (Mat_MPIAIJ *)P->data;
1283: Mat_SeqAIJ *adpd_seq, *p_off, *aopoth_seq;
1284: PetscInt adponz, adpdnz;
1285: PetscInt *pi_loc, *dnz, *onz;
1286: PetscInt *adi = ad->i, *adj = ad->j, *aoi = ao->i, rstart = A->rmap->rstart;
1287: PetscInt *lnk, i, i1 = 0, pnz, row, *adpoi, *adpoj, *api, *adpoJ, *aopJ, *apJ, *Jptr, aopnz, nspacedouble = 0, j, nzi, *apj, apnz, *adpdi, *adpdj, *adpdJ, *poff_i, *poff_j, *j_temp, *aopothi, *aopothj;
1288: PetscInt am = A->rmap->n, pN = P->cmap->N, pn = P->cmap->n, pm = P->rmap->n, p_colstart, p_colend;
1289: PetscBT lnkbt;
1290: PetscReal afill;
1291: PetscMPIInt rank;
1292: Mat adpd, aopoth;
1293: MatType mtype;
1294: const char *prefix;
1296: PetscFunctionBegin;
1297: MatCheckProduct(C, 4);
1298: PetscCheck(!C->product->data, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Extra product struct not empty");
1299: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
1300: PetscCallMPI(MPI_Comm_size(comm, &size));
1301: PetscCallMPI(MPI_Comm_rank(comm, &rank));
1302: PetscCall(MatGetOwnershipRangeColumn(P, &p_colstart, &p_colend));
1304: /* create struct MatProductCtx_APMPI and attached it to C later */
1305: PetscCall(PetscNew(&ptap));
1307: /* get P_oth by taking rows of P (= non-zero cols of local A) from other processors */
1308: PetscCall(MatGetBrowsOfAoCols_MPIAIJ(A, P, MAT_INITIAL_MATRIX, &ptap->startsj_s, &ptap->startsj_r, &ptap->bufa, &ptap->P_oth));
1310: /* get P_loc by taking all local rows of P */
1311: PetscCall(MatMPIAIJGetLocalMat(P, MAT_INITIAL_MATRIX, &ptap->P_loc));
1313: p_loc = (Mat_SeqAIJ *)ptap->P_loc->data;
1314: pi_loc = p_loc->i;
1316: /* Allocate memory for the i arrays of the matrices A*P, A_diag*P_off and A_offd * P */
1317: PetscCall(PetscMalloc1(am + 1, &api));
1318: PetscCall(PetscMalloc1(am + 1, &adpoi));
1320: adpoi[0] = 0;
1321: ptap->api = api;
1322: api[0] = 0;
1324: /* create and initialize a linked list, will be used for both A_diag * P_loc_off and A_offd * P_oth */
1325: PetscCall(PetscLLCondensedCreate(pN, pN, &lnk, &lnkbt));
1326: MatPreallocateBegin(comm, am, pn, dnz, onz);
1328: /* Symbolic calc of A_loc_diag * P_loc_diag */
1329: PetscCall(MatGetOptionsPrefix(A, &prefix));
1330: PetscCall(MatProductCreate(a->A, p->A, NULL, &adpd));
1331: PetscCall(MatGetOptionsPrefix(A, &prefix));
1332: PetscCall(MatSetOptionsPrefix(adpd, prefix));
1333: PetscCall(MatAppendOptionsPrefix(adpd, "inner_diag_"));
1335: PetscCall(MatProductSetType(adpd, MATPRODUCT_AB));
1336: PetscCall(MatProductSetAlgorithm(adpd, "sorted"));
1337: PetscCall(MatProductSetFill(adpd, fill));
1338: PetscCall(MatProductSetFromOptions(adpd));
1340: adpd->force_diagonals = C->force_diagonals;
1341: PetscCall(MatProductSymbolic(adpd));
1343: adpd_seq = (Mat_SeqAIJ *)adpd->data;
1344: adpdi = adpd_seq->i;
1345: adpdj = adpd_seq->j;
1346: p_off = (Mat_SeqAIJ *)p->B->data;
1347: poff_i = p_off->i;
1348: poff_j = p_off->j;
1350: /* j_temp stores indices of a result row before they are added to the linked list */
1351: PetscCall(PetscMalloc1(pN, &j_temp));
1353: /* Symbolic calc of the A_diag * p_loc_off */
1354: /* Initial FreeSpace size is fill*(nnz(A)+nnz(P)) */
1355: PetscCall(PetscFreeSpaceGet(PetscRealIntMultTruncate(fill, PetscIntSumTruncate(adi[am], PetscIntSumTruncate(aoi[am], pi_loc[pm]))), &free_space_diag));
1356: current_space = free_space_diag;
1358: for (i = 0; i < am; i++) {
1359: /* A_diag * P_loc_off */
1360: nzi = adi[i + 1] - adi[i];
1361: for (j = 0; j < nzi; j++) {
1362: row = *adj++;
1363: pnz = poff_i[row + 1] - poff_i[row];
1364: Jptr = poff_j + poff_i[row];
1365: for (i1 = 0; i1 < pnz; i1++) j_temp[i1] = p->garray[Jptr[i1]];
1366: /* add non-zero cols of P into the sorted linked list lnk */
1367: PetscCall(PetscLLCondensedAddSorted(pnz, j_temp, lnk, lnkbt));
1368: }
1370: adponz = lnk[0];
1371: adpoi[i + 1] = adpoi[i] + adponz;
1373: /* if free space is not available, double the total space in the list */
1374: if (current_space->local_remaining < adponz) {
1375: PetscCall(PetscFreeSpaceGet(PetscIntSumTruncate(adponz, current_space->total_array_size), ¤t_space));
1376: nspacedouble++;
1377: }
1379: /* Copy data into free space, then initialize lnk */
1380: PetscCall(PetscLLCondensedClean(pN, adponz, current_space->array, lnk, lnkbt));
1382: current_space->array += adponz;
1383: current_space->local_used += adponz;
1384: current_space->local_remaining -= adponz;
1385: }
1387: /* Symbolic calc of A_off * P_oth */
1388: PetscCall(MatSetOptionsPrefix(a->B, prefix));
1389: PetscCall(MatAppendOptionsPrefix(a->B, "inner_offdiag_"));
1390: PetscCall(MatCreate(PETSC_COMM_SELF, &aopoth));
1391: PetscCall(MatMatMultSymbolic_SeqAIJ_SeqAIJ(a->B, ptap->P_oth, fill, aopoth));
1392: aopoth_seq = (Mat_SeqAIJ *)aopoth->data;
1393: aopothi = aopoth_seq->i;
1394: aopothj = aopoth_seq->j;
1396: /* Allocate space for apj, adpj, aopj, ... */
1397: /* destroy lists of free space and other temporary array(s) */
1399: PetscCall(PetscMalloc1(aopothi[am] + adpoi[am] + adpdi[am], &ptap->apj));
1400: PetscCall(PetscMalloc1(adpoi[am], &adpoj));
1402: /* Copy from linked list to j-array */
1403: PetscCall(PetscFreeSpaceContiguous(&free_space_diag, adpoj));
1404: PetscCall(PetscLLDestroy(lnk, lnkbt));
1406: adpoJ = adpoj;
1407: adpdJ = adpdj;
1408: aopJ = aopothj;
1409: apj = ptap->apj;
1410: apJ = apj; /* still empty */
1412: /* Merge j-arrays of A_off * P, A_diag * P_loc_off, and */
1413: /* A_diag * P_loc_diag to get A*P */
1414: for (i = 0; i < am; i++) {
1415: aopnz = aopothi[i + 1] - aopothi[i];
1416: adponz = adpoi[i + 1] - adpoi[i];
1417: adpdnz = adpdi[i + 1] - adpdi[i];
1419: /* Correct indices from A_diag*P_diag */
1420: for (i1 = 0; i1 < adpdnz; i1++) adpdJ[i1] += p_colstart;
1421: /* Merge j-arrays of A_diag * P_loc_off and A_diag * P_loc_diag and A_off * P_oth */
1422: Merge3SortedArrays(adponz, adpoJ, adpdnz, adpdJ, aopnz, aopJ, &apnz, apJ);
1423: PetscCall(MatPreallocateSet(i + rstart, apnz, apJ, dnz, onz));
1425: aopJ += aopnz;
1426: adpoJ += adponz;
1427: adpdJ += adpdnz;
1428: apJ += apnz;
1429: api[i + 1] = api[i] + apnz;
1430: }
1432: /* malloc apa to store dense row A[i,:]*P */
1433: PetscCall(PetscCalloc1(pN, &ptap->apa));
1435: /* create and assemble symbolic parallel matrix C */
1436: PetscCall(MatSetSizes(C, am, pn, PETSC_DETERMINE, PETSC_DETERMINE));
1437: PetscCall(MatSetBlockSizesFromMats(C, A, P));
1438: PetscCall(MatGetType(A, &mtype));
1439: PetscCall(MatSetType(C, mtype));
1440: PetscCall(MatMPIAIJSetPreallocation(C, 0, dnz, 0, onz));
1441: MatPreallocateEnd(dnz, onz);
1443: PetscCall(MatSetValues_MPIAIJ_CopyFromCSRFormat_Symbolic(C, apj, api));
1444: PetscCall(MatSetOption(C, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
1445: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
1446: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
1447: PetscCall(MatSetOption(C, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1449: C->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable;
1450: C->ops->productnumeric = MatProductNumeric_AB;
1452: /* attach the supporting struct to C for reuse */
1453: C->product->data = ptap;
1454: C->product->destroy = MatProductCtxDestroy_MPIAIJ_MatMatMult;
1456: /* set MatInfo */
1457: afill = (PetscReal)api[am] / (adi[am] + aoi[am] + pi_loc[pm] + 1) + 1.e-5;
1458: if (afill < 1.0) afill = 1.0;
1459: C->info.mallocs = nspacedouble;
1460: C->info.fill_ratio_given = fill;
1461: C->info.fill_ratio_needed = afill;
1463: if (PetscDefined(USE_INFO)) {
1464: if (api[am]) {
1465: PetscCall(PetscInfo(C, "Reallocs %" PetscInt_FMT "; Fill ratio: given %g needed %g.\n", nspacedouble, (double)fill, (double)afill));
1466: PetscCall(PetscInfo(C, "Use MatMatMult(A,B,MatReuse,%g,&C) for best performance.;\n", (double)afill));
1467: } else PetscCall(PetscInfo(C, "Empty matrix product\n"));
1468: }
1470: PetscCall(MatDestroy(&aopoth));
1471: PetscCall(MatDestroy(&adpd));
1472: PetscCall(PetscFree(j_temp));
1473: PetscCall(PetscFree(adpoj));
1474: PetscCall(PetscFree(adpoi));
1475: PetscFunctionReturn(PETSC_SUCCESS);
1476: }
1478: /* This routine only works when scall=MAT_REUSE_MATRIX! */
1479: PetscErrorCode MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_matmatmult(Mat P, Mat A, Mat C)
1480: {
1481: MatProductCtx_APMPI *ptap;
1482: Mat Pt;
1484: PetscFunctionBegin;
1485: MatCheckProduct(C, 3);
1486: ptap = (MatProductCtx_APMPI *)C->product->data;
1487: PetscCheck(ptap, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "PtAP cannot be computed. Missing data");
1488: PetscCheck(ptap->Pt, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "PtA cannot be reused. Do not call MatProductClear()");
1490: Pt = ptap->Pt;
1491: PetscCall(MatTransposeSetPrecursor(P, Pt));
1492: PetscCall(MatTranspose(P, MAT_REUSE_MATRIX, &Pt));
1493: PetscCall(MatMatMultNumeric_MPIAIJ_MPIAIJ(Pt, A, C));
1494: PetscFunctionReturn(PETSC_SUCCESS);
1495: }
1497: /* This routine is modified from MatPtAPSymbolic_MPIAIJ_MPIAIJ() */
1498: PetscErrorCode MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(Mat P, Mat A, PetscReal fill, Mat C)
1499: {
1500: MatProductCtx_APMPI *ptap;
1501: Mat_MPIAIJ *p = (Mat_MPIAIJ *)P->data;
1502: MPI_Comm comm;
1503: PetscMPIInt size, rank;
1504: PetscFreeSpaceList free_space = NULL, current_space = NULL;
1505: PetscInt pn = P->cmap->n, aN = A->cmap->N, an = A->cmap->n;
1506: PetscInt *lnk, i, k, rstart;
1507: PetscBT lnkbt;
1508: PetscMPIInt tagi, tagj, *len_si, *len_s, *len_ri, nrecv, proc, nsend;
1509: PETSC_UNUSED PetscMPIInt icompleted = 0;
1510: PetscInt **buf_rj, **buf_ri, **buf_ri_k, row, ncols, *cols;
1511: PetscInt len, *dnz, *onz, *owners, nzi;
1512: PetscInt nrows, *buf_s, *buf_si, *buf_si_i, **nextrow, **nextci;
1513: MPI_Request *swaits, *rwaits;
1514: MPI_Status *sstatus, rstatus;
1515: PetscLayout rowmap;
1516: PetscInt *owners_co, *coi, *coj; /* i and j array of (p->B)^T*A*P - used in the communication */
1517: PetscMPIInt *len_r, *id_r; /* array of length of comm->size, store send/recv matrix values */
1518: PetscInt *Jptr, *prmap = p->garray, con, j, Crmax;
1519: Mat_SeqAIJ *a_loc, *c_loc, *c_oth;
1520: PetscHMapI ta;
1521: MatType mtype;
1522: const char *prefix;
1524: PetscFunctionBegin;
1525: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
1526: PetscCallMPI(MPI_Comm_size(comm, &size));
1527: PetscCallMPI(MPI_Comm_rank(comm, &rank));
1529: /* create symbolic parallel matrix C */
1530: PetscCall(MatGetType(A, &mtype));
1531: PetscCall(MatSetType(C, mtype));
1533: C->ops->transposematmultnumeric = MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable;
1535: /* create struct MatProductCtx_APMPI and attached it to C later */
1536: PetscCall(PetscNew(&ptap));
1538: /* (0) compute Rd = Pd^T, Ro = Po^T */
1539: PetscCall(MatTranspose(p->A, MAT_INITIAL_MATRIX, &ptap->Rd));
1540: PetscCall(MatTranspose(p->B, MAT_INITIAL_MATRIX, &ptap->Ro));
1542: /* (1) compute symbolic A_loc */
1543: PetscCall(MatMPIAIJGetLocalMat(A, MAT_INITIAL_MATRIX, &ptap->A_loc));
1545: /* (2-1) compute symbolic C_oth = Ro*A_loc */
1546: PetscCall(MatGetOptionsPrefix(A, &prefix));
1547: PetscCall(MatSetOptionsPrefix(ptap->Ro, prefix));
1548: PetscCall(MatAppendOptionsPrefix(ptap->Ro, "inner_offdiag_"));
1549: PetscCall(MatCreate(PETSC_COMM_SELF, &ptap->C_oth));
1550: PetscCall(MatMatMultSymbolic_SeqAIJ_SeqAIJ(ptap->Ro, ptap->A_loc, fill, ptap->C_oth));
1552: /* (3) send coj of C_oth to other processors */
1553: /* determine row ownership */
1554: PetscCall(PetscLayoutCreate(comm, &rowmap));
1555: rowmap->n = pn;
1556: rowmap->bs = 1;
1557: PetscCall(PetscLayoutSetUp(rowmap));
1558: owners = rowmap->range;
1560: /* determine the number of messages to send, their lengths */
1561: PetscCall(PetscMalloc4(size, &len_s, size, &len_si, size, &sstatus, size + 1, &owners_co));
1562: PetscCall(PetscArrayzero(len_s, size));
1563: PetscCall(PetscArrayzero(len_si, size));
1565: c_oth = (Mat_SeqAIJ *)ptap->C_oth->data;
1566: coi = c_oth->i;
1567: coj = c_oth->j;
1568: con = ptap->C_oth->rmap->n;
1569: proc = 0;
1570: for (i = 0; i < con; i++) {
1571: while (prmap[i] >= owners[proc + 1]) proc++;
1572: len_si[proc]++; /* num of rows in Co(=Pt*A) to be sent to [proc] */
1573: len_s[proc] += coi[i + 1] - coi[i]; /* num of nonzeros in Co to be sent to [proc] */
1574: }
1576: len = 0; /* max length of buf_si[], see (4) */
1577: owners_co[0] = 0;
1578: nsend = 0;
1579: for (proc = 0; proc < size; proc++) {
1580: owners_co[proc + 1] = owners_co[proc] + len_si[proc];
1581: if (len_s[proc]) {
1582: nsend++;
1583: len_si[proc] = 2 * (len_si[proc] + 1); /* length of buf_si to be sent to [proc] */
1584: len += len_si[proc];
1585: }
1586: }
1588: /* determine the number and length of messages to receive for coi and coj */
1589: PetscCall(PetscGatherNumberOfMessages(comm, NULL, len_s, &nrecv));
1590: PetscCall(PetscGatherMessageLengths2(comm, nsend, nrecv, len_s, len_si, &id_r, &len_r, &len_ri));
1592: /* post the Irecv and Isend of coj */
1593: PetscCall(PetscCommGetNewTag(comm, &tagj));
1594: PetscCall(PetscPostIrecvInt(comm, tagj, nrecv, id_r, len_r, &buf_rj, &rwaits));
1595: PetscCall(PetscMalloc1(nsend, &swaits));
1596: for (proc = 0, k = 0; proc < size; proc++) {
1597: if (!len_s[proc]) continue;
1598: i = owners_co[proc];
1599: PetscCallMPI(MPIU_Isend(coj + coi[i], len_s[proc], MPIU_INT, proc, tagj, comm, swaits + k));
1600: k++;
1601: }
1603: /* (2-2) compute symbolic C_loc = Rd*A_loc */
1604: PetscCall(MatSetOptionsPrefix(ptap->Rd, prefix));
1605: PetscCall(MatAppendOptionsPrefix(ptap->Rd, "inner_diag_"));
1606: PetscCall(MatCreate(PETSC_COMM_SELF, &ptap->C_loc));
1607: PetscCall(MatMatMultSymbolic_SeqAIJ_SeqAIJ(ptap->Rd, ptap->A_loc, fill, ptap->C_loc));
1608: c_loc = (Mat_SeqAIJ *)ptap->C_loc->data;
1610: /* receives coj are complete */
1611: for (i = 0; i < nrecv; i++) PetscCallMPI(MPI_Waitany(nrecv, rwaits, &icompleted, &rstatus));
1612: PetscCall(PetscFree(rwaits));
1613: if (nsend) PetscCallMPI(MPI_Waitall(nsend, swaits, sstatus));
1615: /* add received column indices into ta to update Crmax */
1616: a_loc = (Mat_SeqAIJ *)ptap->A_loc->data;
1618: /* create and initialize a linked list */
1619: PetscCall(PetscHMapICreateWithSize(an, &ta)); /* for compute Crmax */
1620: MatRowMergeMax_SeqAIJ(a_loc, ptap->A_loc->rmap->N, ta);
1622: for (k = 0; k < nrecv; k++) { /* k-th received message */
1623: Jptr = buf_rj[k];
1624: for (j = 0; j < len_r[k]; j++) PetscCall(PetscHMapISet(ta, *(Jptr + j) + 1, 1));
1625: }
1626: PetscCall(PetscHMapIGetSize(ta, &Crmax));
1627: PetscCall(PetscHMapIDestroy(&ta));
1629: /* (4) send and recv coi */
1630: PetscCall(PetscCommGetNewTag(comm, &tagi));
1631: PetscCall(PetscPostIrecvInt(comm, tagi, nrecv, id_r, len_ri, &buf_ri, &rwaits));
1632: PetscCall(PetscMalloc1(len, &buf_s));
1633: buf_si = buf_s; /* points to the beginning of k-th msg to be sent */
1634: for (proc = 0, k = 0; proc < size; proc++) {
1635: if (!len_s[proc]) continue;
1636: /* form outgoing message for i-structure:
1637: buf_si[0]: nrows to be sent
1638: [1:nrows]: row index (global)
1639: [nrows+1:2*nrows+1]: i-structure index
1640: */
1641: nrows = len_si[proc] / 2 - 1; /* num of rows in Co to be sent to [proc] */
1642: buf_si_i = buf_si + nrows + 1;
1643: buf_si[0] = nrows;
1644: buf_si_i[0] = 0;
1645: nrows = 0;
1646: for (i = owners_co[proc]; i < owners_co[proc + 1]; i++) {
1647: nzi = coi[i + 1] - coi[i];
1648: buf_si_i[nrows + 1] = buf_si_i[nrows] + nzi; /* i-structure */
1649: buf_si[nrows + 1] = prmap[i] - owners[proc]; /* local row index */
1650: nrows++;
1651: }
1652: PetscCallMPI(MPIU_Isend(buf_si, len_si[proc], MPIU_INT, proc, tagi, comm, swaits + k));
1653: k++;
1654: buf_si += len_si[proc];
1655: }
1656: for (i = 0; i < nrecv; i++) PetscCallMPI(MPI_Waitany(nrecv, rwaits, &icompleted, &rstatus));
1657: PetscCall(PetscFree(rwaits));
1658: if (nsend) PetscCallMPI(MPI_Waitall(nsend, swaits, sstatus));
1660: PetscCall(PetscFree4(len_s, len_si, sstatus, owners_co));
1661: PetscCall(PetscFree(len_ri));
1662: PetscCall(PetscFree(swaits));
1663: PetscCall(PetscFree(buf_s));
1665: /* (5) compute the local portion of C */
1666: /* set initial free space to be Crmax, sufficient for holding nonzeros in each row of C */
1667: PetscCall(PetscFreeSpaceGet(Crmax, &free_space));
1668: current_space = free_space;
1670: PetscCall(PetscMalloc3(nrecv, &buf_ri_k, nrecv, &nextrow, nrecv, &nextci));
1671: for (k = 0; k < nrecv; k++) {
1672: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
1673: nrows = *buf_ri_k[k];
1674: nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */
1675: nextci[k] = buf_ri_k[k] + (nrows + 1); /* points to the next i-structure of k-th recved i-structure */
1676: }
1678: MatPreallocateBegin(comm, pn, an, dnz, onz);
1679: PetscCall(PetscLLCondensedCreate(Crmax, aN, &lnk, &lnkbt));
1680: for (i = 0; i < pn; i++) { /* for each local row of C */
1681: /* add C_loc into C */
1682: nzi = c_loc->i[i + 1] - c_loc->i[i];
1683: Jptr = c_loc->j + c_loc->i[i];
1684: PetscCall(PetscLLCondensedAddSorted(nzi, Jptr, lnk, lnkbt));
1686: /* add received col data into lnk */
1687: for (k = 0; k < nrecv; k++) { /* k-th received message */
1688: if (i == *nextrow[k]) { /* i-th row */
1689: nzi = *(nextci[k] + 1) - *nextci[k];
1690: Jptr = buf_rj[k] + *nextci[k];
1691: PetscCall(PetscLLCondensedAddSorted(nzi, Jptr, lnk, lnkbt));
1692: nextrow[k]++;
1693: nextci[k]++;
1694: }
1695: }
1697: /* add missing diagonal entry */
1698: if (C->force_diagonals) {
1699: k = i + owners[rank]; /* column index */
1700: PetscCall(PetscLLCondensedAddSorted(1, &k, lnk, lnkbt));
1701: }
1703: nzi = lnk[0];
1705: /* copy data into free space, then initialize lnk */
1706: PetscCall(PetscLLCondensedClean(aN, nzi, current_space->array, lnk, lnkbt));
1707: PetscCall(MatPreallocateSet(i + owners[rank], nzi, current_space->array, dnz, onz));
1708: }
1709: PetscCall(PetscFree3(buf_ri_k, nextrow, nextci));
1710: PetscCall(PetscLLDestroy(lnk, lnkbt));
1711: PetscCall(PetscFreeSpaceDestroy(free_space));
1713: /* local sizes and preallocation */
1714: PetscCall(MatSetSizes(C, pn, an, PETSC_DETERMINE, PETSC_DETERMINE));
1715: PetscCall(PetscLayoutSetBlockSize(C->rmap, P->cmap->bs));
1716: PetscCall(PetscLayoutSetBlockSize(C->cmap, A->cmap->bs));
1717: PetscCall(MatMPIAIJSetPreallocation(C, 0, dnz, 0, onz));
1718: MatPreallocateEnd(dnz, onz);
1720: /* add C_loc and C_oth to C */
1721: PetscCall(MatGetOwnershipRange(C, &rstart, NULL));
1722: for (i = 0; i < pn; i++) {
1723: ncols = c_loc->i[i + 1] - c_loc->i[i];
1724: cols = c_loc->j + c_loc->i[i];
1725: row = rstart + i;
1726: PetscCall(MatSetValues(C, 1, (const PetscInt *)&row, ncols, (const PetscInt *)cols, NULL, INSERT_VALUES));
1728: if (C->force_diagonals) PetscCall(MatSetValues(C, 1, (const PetscInt *)&row, 1, (const PetscInt *)&row, NULL, INSERT_VALUES));
1729: }
1730: for (i = 0; i < con; i++) {
1731: ncols = c_oth->i[i + 1] - c_oth->i[i];
1732: cols = c_oth->j + c_oth->i[i];
1733: row = prmap[i];
1734: PetscCall(MatSetValues(C, 1, (const PetscInt *)&row, ncols, (const PetscInt *)cols, NULL, INSERT_VALUES));
1735: }
1736: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
1737: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
1738: PetscCall(MatSetOption(C, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1740: /* members in merge */
1741: PetscCall(PetscFree(id_r));
1742: PetscCall(PetscFree(len_r));
1743: PetscCall(PetscFree(buf_ri[0]));
1744: PetscCall(PetscFree(buf_ri));
1745: PetscCall(PetscFree(buf_rj[0]));
1746: PetscCall(PetscFree(buf_rj));
1747: PetscCall(PetscLayoutDestroy(&rowmap));
1749: /* attach the supporting struct to C for reuse */
1750: C->product->data = ptap;
1751: C->product->destroy = MatProductCtxDestroy_MPIAIJ_PtAP;
1752: PetscFunctionReturn(PETSC_SUCCESS);
1753: }
1755: PetscErrorCode MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable(Mat P, Mat A, Mat C)
1756: {
1757: Mat_MPIAIJ *p = (Mat_MPIAIJ *)P->data;
1758: Mat_SeqAIJ *c_seq;
1759: MatProductCtx_APMPI *ptap;
1760: Mat A_loc, C_loc, C_oth;
1761: PetscInt i, rstart, rend, cm, ncols, row;
1762: const PetscInt *cols;
1763: const PetscScalar *vals;
1765: PetscFunctionBegin;
1766: MatCheckProduct(C, 3);
1767: ptap = (MatProductCtx_APMPI *)C->product->data;
1768: PetscCheck(ptap, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "PtAP cannot be computed. Missing data");
1769: PetscCheck(ptap->A_loc, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "PtA cannot be reused. Do not call MatProductClear()");
1770: PetscCall(MatZeroEntries(C));
1772: /* These matrices are obtained in MatTransposeMatMultSymbolic() */
1773: /* 1) get R = Pd^T, Ro = Po^T */
1774: PetscCall(MatTransposeSetPrecursor(p->A, ptap->Rd));
1775: PetscCall(MatTranspose(p->A, MAT_REUSE_MATRIX, &ptap->Rd));
1776: PetscCall(MatTransposeSetPrecursor(p->B, ptap->Ro));
1777: PetscCall(MatTranspose(p->B, MAT_REUSE_MATRIX, &ptap->Ro));
1779: /* 2) compute numeric A_loc */
1780: PetscCall(MatMPIAIJGetLocalMat(A, MAT_REUSE_MATRIX, &ptap->A_loc));
1782: /* 3) C_loc = Rd*A_loc, C_oth = Ro*A_loc */
1783: A_loc = ptap->A_loc;
1784: PetscCall(ptap->C_loc->ops->matmultnumeric(ptap->Rd, A_loc, ptap->C_loc));
1785: PetscCall(ptap->C_oth->ops->matmultnumeric(ptap->Ro, A_loc, ptap->C_oth));
1786: C_loc = ptap->C_loc;
1787: C_oth = ptap->C_oth;
1789: /* add C_loc and C_oth to C */
1790: PetscCall(MatGetOwnershipRange(C, &rstart, &rend));
1792: /* C_loc -> C */
1793: cm = C_loc->rmap->N;
1794: c_seq = (Mat_SeqAIJ *)C_loc->data;
1795: cols = c_seq->j;
1796: vals = c_seq->a;
1797: for (i = 0; i < cm; i++) {
1798: ncols = c_seq->i[i + 1] - c_seq->i[i];
1799: row = rstart + i;
1800: PetscCall(MatSetValues(C, 1, &row, ncols, cols, vals, ADD_VALUES));
1801: cols += ncols;
1802: vals += ncols;
1803: }
1805: /* Co -> C, off-processor part */
1806: cm = C_oth->rmap->N;
1807: c_seq = (Mat_SeqAIJ *)C_oth->data;
1808: cols = c_seq->j;
1809: vals = c_seq->a;
1810: for (i = 0; i < cm; i++) {
1811: ncols = c_seq->i[i + 1] - c_seq->i[i];
1812: row = p->garray[i];
1813: PetscCall(MatSetValues(C, 1, &row, ncols, cols, vals, ADD_VALUES));
1814: cols += ncols;
1815: vals += ncols;
1816: }
1817: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
1818: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
1819: PetscCall(MatSetOption(C, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1820: PetscFunctionReturn(PETSC_SUCCESS);
1821: }
1823: PetscErrorCode MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ(Mat P, Mat A, Mat C)
1824: {
1825: MatMergeSeqsToMPI *merge;
1826: Mat_MPIAIJ *p = (Mat_MPIAIJ *)P->data;
1827: Mat_SeqAIJ *pd = (Mat_SeqAIJ *)p->A->data, *po = (Mat_SeqAIJ *)p->B->data;
1828: MatProductCtx_APMPI *ap;
1829: PetscInt *adj;
1830: PetscInt i, j, k, anz, pnz, row, *cj, nexta;
1831: MatScalar *ada, *ca, valtmp;
1832: PetscInt am = A->rmap->n, cm = C->rmap->n, pon = p->B->cmap->n;
1833: MPI_Comm comm;
1834: PetscMPIInt size, rank, taga, *len_s, proc;
1835: PetscInt *owners, nrows, **buf_ri_k, **nextrow, **nextci;
1836: PetscInt **buf_ri, **buf_rj;
1837: PetscInt cnz = 0, *bj_i, *bi, *bj, bnz, nextcj; /* bi,bj,ba: local array of C(mpi mat) */
1838: MPI_Request *s_waits, *r_waits;
1839: MPI_Status *status;
1840: MatScalar **abuf_r, *ba_i, *pA, *coa, *ba;
1841: const PetscScalar *dummy;
1842: PetscInt *ai, *aj, *coi, *coj, *poJ, *pdJ;
1843: Mat A_loc;
1844: Mat_SeqAIJ *a_loc;
1846: PetscFunctionBegin;
1847: MatCheckProduct(C, 3);
1848: ap = (MatProductCtx_APMPI *)C->product->data;
1849: PetscCheck(ap, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "PtA cannot be computed. Missing data");
1850: PetscCheck(ap->A_loc, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "PtA cannot be reused. Do not call MatProductClear()");
1851: PetscCall(PetscObjectGetComm((PetscObject)C, &comm));
1852: PetscCallMPI(MPI_Comm_size(comm, &size));
1853: PetscCallMPI(MPI_Comm_rank(comm, &rank));
1855: merge = ap->merge;
1857: /* 2) compute numeric C_seq = P_loc^T*A_loc */
1858: /* get data from symbolic products */
1859: coi = merge->coi;
1860: coj = merge->coj;
1861: PetscCall(PetscCalloc1(coi[pon], &coa));
1862: bi = merge->bi;
1863: bj = merge->bj;
1864: owners = merge->rowmap->range;
1865: PetscCall(PetscCalloc1(bi[cm], &ba));
1867: /* get A_loc by taking all local rows of A */
1868: A_loc = ap->A_loc;
1869: PetscCall(MatMPIAIJGetLocalMat(A, MAT_REUSE_MATRIX, &A_loc));
1870: a_loc = (Mat_SeqAIJ *)A_loc->data;
1871: ai = a_loc->i;
1872: aj = a_loc->j;
1874: /* trigger copy to CPU */
1875: PetscCall(MatSeqAIJGetArrayRead(p->A, &dummy));
1876: PetscCall(MatSeqAIJRestoreArrayRead(p->A, &dummy));
1877: PetscCall(MatSeqAIJGetArrayRead(p->B, &dummy));
1878: PetscCall(MatSeqAIJRestoreArrayRead(p->B, &dummy));
1879: for (i = 0; i < am; i++) {
1880: anz = ai[i + 1] - ai[i];
1881: adj = aj + ai[i];
1882: ada = a_loc->a + ai[i];
1884: /* 2-b) Compute Cseq = P_loc[i,:]^T*A[i,:] using outer product */
1885: /* put the value into Co=(p->B)^T*A (off-diagonal part, send to others) */
1886: pnz = po->i[i + 1] - po->i[i];
1887: poJ = po->j + po->i[i];
1888: pA = po->a + po->i[i];
1889: for (j = 0; j < pnz; j++) {
1890: row = poJ[j];
1891: cj = coj + coi[row];
1892: ca = coa + coi[row];
1893: /* perform sparse axpy */
1894: nexta = 0;
1895: valtmp = pA[j];
1896: for (k = 0; nexta < anz; k++) {
1897: if (cj[k] == adj[nexta]) {
1898: ca[k] += valtmp * ada[nexta];
1899: nexta++;
1900: }
1901: }
1902: PetscCall(PetscLogFlops(2.0 * anz));
1903: }
1905: /* put the value into Cd (diagonal part) */
1906: pnz = pd->i[i + 1] - pd->i[i];
1907: pdJ = pd->j + pd->i[i];
1908: pA = pd->a + pd->i[i];
1909: for (j = 0; j < pnz; j++) {
1910: row = pdJ[j];
1911: cj = bj + bi[row];
1912: ca = ba + bi[row];
1913: /* perform sparse axpy */
1914: nexta = 0;
1915: valtmp = pA[j];
1916: for (k = 0; nexta < anz; k++) {
1917: if (cj[k] == adj[nexta]) {
1918: ca[k] += valtmp * ada[nexta];
1919: nexta++;
1920: }
1921: }
1922: PetscCall(PetscLogFlops(2.0 * anz));
1923: }
1924: }
1926: /* 3) send and recv matrix values coa */
1927: buf_ri = merge->buf_ri;
1928: buf_rj = merge->buf_rj;
1929: len_s = merge->len_s;
1930: PetscCall(PetscCommGetNewTag(comm, &taga));
1931: PetscCall(PetscPostIrecvScalar(comm, taga, merge->nrecv, merge->id_r, merge->len_r, &abuf_r, &r_waits));
1933: PetscCall(PetscMalloc2(merge->nsend, &s_waits, size, &status));
1934: for (proc = 0, k = 0; proc < size; proc++) {
1935: if (!len_s[proc]) continue;
1936: i = merge->owners_co[proc];
1937: PetscCallMPI(MPIU_Isend(coa + coi[i], len_s[proc], MPIU_MATSCALAR, proc, taga, comm, s_waits + k));
1938: k++;
1939: }
1940: if (merge->nrecv) PetscCallMPI(MPI_Waitall(merge->nrecv, r_waits, status));
1941: if (merge->nsend) PetscCallMPI(MPI_Waitall(merge->nsend, s_waits, status));
1943: PetscCall(PetscFree2(s_waits, status));
1944: PetscCall(PetscFree(r_waits));
1945: PetscCall(PetscFree(coa));
1947: /* 4) insert local Cseq and received values into Cmpi */
1948: PetscCall(PetscMalloc3(merge->nrecv, &buf_ri_k, merge->nrecv, &nextrow, merge->nrecv, &nextci));
1949: for (k = 0; k < merge->nrecv; k++) {
1950: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
1951: nrows = *buf_ri_k[k];
1952: nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */
1953: nextci[k] = buf_ri_k[k] + (nrows + 1); /* points to the next i-structure of k-th recved i-structure */
1954: }
1956: for (i = 0; i < cm; i++) {
1957: row = owners[rank] + i; /* global row index of C_seq */
1958: bj_i = bj + bi[i]; /* col indices of the i-th row of C */
1959: ba_i = ba + bi[i];
1960: bnz = bi[i + 1] - bi[i];
1961: /* add received vals into ba */
1962: for (k = 0; k < merge->nrecv; k++) { /* k-th received message */
1963: /* i-th row */
1964: if (i == *nextrow[k]) {
1965: cnz = *(nextci[k] + 1) - *nextci[k];
1966: cj = buf_rj[k] + *nextci[k];
1967: ca = abuf_r[k] + *nextci[k];
1968: nextcj = 0;
1969: for (j = 0; nextcj < cnz; j++) {
1970: if (bj_i[j] == cj[nextcj]) { /* bcol == ccol */
1971: ba_i[j] += ca[nextcj++];
1972: }
1973: }
1974: nextrow[k]++;
1975: nextci[k]++;
1976: PetscCall(PetscLogFlops(2.0 * cnz));
1977: }
1978: }
1979: PetscCall(MatSetValues(C, 1, &row, bnz, bj_i, ba_i, INSERT_VALUES));
1980: }
1981: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
1982: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
1984: PetscCall(PetscFree(ba));
1985: PetscCall(PetscFree(abuf_r[0]));
1986: PetscCall(PetscFree(abuf_r));
1987: PetscCall(PetscFree3(buf_ri_k, nextrow, nextci));
1988: PetscFunctionReturn(PETSC_SUCCESS);
1989: }
1991: PetscErrorCode MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ(Mat P, Mat A, PetscReal fill, Mat C)
1992: {
1993: Mat A_loc;
1994: MatProductCtx_APMPI *ap;
1995: PetscFreeSpaceList free_space = NULL, current_space = NULL;
1996: Mat_MPIAIJ *p = (Mat_MPIAIJ *)P->data, *a = (Mat_MPIAIJ *)A->data;
1997: PetscInt *pdti, *pdtj, *poti, *potj, *ptJ;
1998: PetscInt nnz;
1999: PetscInt *lnk, *owners_co, *coi, *coj, i, k, pnz, row;
2000: PetscInt am = A->rmap->n, pn = P->cmap->n;
2001: MPI_Comm comm;
2002: PetscMPIInt size, rank, tagi, tagj, *len_si, *len_s, *len_ri, proc;
2003: PetscInt **buf_rj, **buf_ri, **buf_ri_k;
2004: PetscInt len, *dnz, *onz, *owners;
2005: PetscInt nzi, *bi, *bj;
2006: PetscInt nrows, *buf_s, *buf_si, *buf_si_i, **nextrow, **nextci;
2007: MPI_Request *swaits, *rwaits;
2008: MPI_Status *sstatus, rstatus;
2009: MatMergeSeqsToMPI *merge;
2010: PetscInt *ai, *aj, *Jptr, anz, *prmap = p->garray, pon, nspacedouble = 0, j;
2011: PetscReal afill = 1.0, afill_tmp;
2012: PetscInt rstart = P->cmap->rstart, rmax, Armax;
2013: Mat_SeqAIJ *a_loc;
2014: PetscHMapI ta;
2015: MatType mtype;
2017: PetscFunctionBegin;
2018: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
2019: /* check if matrix local sizes are compatible */
2020: PetscCheck(A->rmap->rstart == P->rmap->rstart && A->rmap->rend == P->rmap->rend, comm, PETSC_ERR_ARG_SIZ, "Matrix local dimensions are incompatible, A (%" PetscInt_FMT ", %" PetscInt_FMT ") != P (%" PetscInt_FMT ",%" PetscInt_FMT ")", A->rmap->rstart,
2021: A->rmap->rend, P->rmap->rstart, P->rmap->rend);
2023: PetscCallMPI(MPI_Comm_size(comm, &size));
2024: PetscCallMPI(MPI_Comm_rank(comm, &rank));
2026: /* create struct MatProductCtx_APMPI and attached it to C later */
2027: PetscCall(PetscNew(&ap));
2029: /* get A_loc by taking all local rows of A */
2030: PetscCall(MatMPIAIJGetLocalMat_Private(A, MAT_INITIAL_MATRIX, C->structure_only, &A_loc));
2032: ap->A_loc = A_loc;
2033: a_loc = (Mat_SeqAIJ *)A_loc->data;
2034: ai = a_loc->i;
2035: aj = a_loc->j;
2037: /* determine symbolic Co=(p->B)^T*A - send to others */
2038: PetscCall(MatGetSymbolicTranspose_SeqAIJ(p->A, &pdti, &pdtj));
2039: PetscCall(MatGetSymbolicTranspose_SeqAIJ(p->B, &poti, &potj));
2040: pon = p->B->cmap->n; /* total num of rows to be sent to other processors
2041: >= (num of nonzero rows of C_seq) - pn */
2042: PetscCall(PetscMalloc1(pon + 1, &coi));
2043: coi[0] = 0;
2045: /* set initial free space to be fill*(nnz(p->B) + nnz(A)) */
2046: nnz = PetscRealIntMultTruncate(fill, PetscIntSumTruncate(poti[pon], ai[am]));
2047: PetscCall(PetscFreeSpaceGet(nnz, &free_space));
2048: current_space = free_space;
2050: /* create and initialize a linked list */
2051: PetscCall(PetscHMapICreateWithSize(A->cmap->n + a->B->cmap->N, &ta));
2052: MatRowMergeMax_SeqAIJ(a_loc, am, ta);
2053: PetscCall(PetscHMapIGetSize(ta, &Armax));
2055: PetscCall(PetscLLCondensedCreate_Scalable(Armax, &lnk));
2057: for (i = 0; i < pon; i++) {
2058: pnz = poti[i + 1] - poti[i];
2059: ptJ = potj + poti[i];
2060: for (j = 0; j < pnz; j++) {
2061: row = ptJ[j]; /* row of A_loc == col of Pot */
2062: anz = ai[row + 1] - ai[row];
2063: Jptr = aj + ai[row];
2064: /* add non-zero cols of AP into the sorted linked list lnk */
2065: PetscCall(PetscLLCondensedAddSorted_Scalable(anz, Jptr, lnk));
2066: }
2067: nnz = lnk[0];
2069: /* If free space is not available, double the total space in the list */
2070: if (current_space->local_remaining < nnz) {
2071: PetscCall(PetscFreeSpaceGet(PetscIntSumTruncate(nnz, current_space->total_array_size), ¤t_space));
2072: nspacedouble++;
2073: }
2075: /* Copy data into free space, and zero out denserows */
2076: PetscCall(PetscLLCondensedClean_Scalable(nnz, current_space->array, lnk));
2078: current_space->array += nnz;
2079: current_space->local_used += nnz;
2080: current_space->local_remaining -= nnz;
2082: coi[i + 1] = coi[i] + nnz;
2083: }
2085: PetscCall(PetscMalloc1(coi[pon], &coj));
2086: PetscCall(PetscFreeSpaceContiguous(&free_space, coj));
2087: PetscCall(PetscLLCondensedDestroy_Scalable(lnk)); /* must destroy to get a new one for C */
2089: afill_tmp = (PetscReal)coi[pon] / (poti[pon] + ai[am] + 1);
2090: if (afill_tmp > afill) afill = afill_tmp;
2092: /* send j-array (coj) of Co to other processors */
2093: /* determine row ownership */
2094: PetscCall(PetscNew(&merge));
2095: PetscCall(PetscLayoutCreate(comm, &merge->rowmap));
2097: merge->rowmap->n = pn;
2098: merge->rowmap->bs = 1;
2100: PetscCall(PetscLayoutSetUp(merge->rowmap));
2101: owners = merge->rowmap->range;
2103: /* determine the number of messages to send, their lengths */
2104: PetscCall(PetscCalloc1(size, &len_si));
2105: PetscCall(PetscCalloc1(size, &merge->len_s));
2107: len_s = merge->len_s;
2108: merge->nsend = 0;
2110: PetscCall(PetscMalloc1(size + 1, &owners_co));
2112: proc = 0;
2113: for (i = 0; i < pon; i++) {
2114: while (prmap[i] >= owners[proc + 1]) proc++;
2115: len_si[proc]++; /* num of rows in Co to be sent to [proc] */
2116: len_s[proc] += coi[i + 1] - coi[i];
2117: }
2119: len = 0; /* max length of buf_si[] */
2120: owners_co[0] = 0;
2121: for (proc = 0; proc < size; proc++) {
2122: owners_co[proc + 1] = owners_co[proc] + len_si[proc];
2123: if (len_s[proc]) {
2124: merge->nsend++;
2125: len_si[proc] = 2 * (len_si[proc] + 1);
2126: len += len_si[proc];
2127: }
2128: }
2130: /* determine the number and length of messages to receive for coi and coj */
2131: PetscCall(PetscGatherNumberOfMessages(comm, NULL, len_s, &merge->nrecv));
2132: PetscCall(PetscGatherMessageLengths2(comm, merge->nsend, merge->nrecv, len_s, len_si, &merge->id_r, &merge->len_r, &len_ri));
2134: /* post the Irecv and Isend of coj */
2135: PetscCall(PetscCommGetNewTag(comm, &tagj));
2136: PetscCall(PetscPostIrecvInt(comm, tagj, merge->nrecv, merge->id_r, merge->len_r, &buf_rj, &rwaits));
2137: PetscCall(PetscMalloc1(merge->nsend, &swaits));
2138: for (proc = 0, k = 0; proc < size; proc++) {
2139: if (!len_s[proc]) continue;
2140: i = owners_co[proc];
2141: PetscCallMPI(MPIU_Isend(coj + coi[i], len_s[proc], MPIU_INT, proc, tagj, comm, swaits + k));
2142: k++;
2143: }
2145: /* receives and sends of coj are complete */
2146: PetscCall(PetscMalloc1(size, &sstatus));
2147: for (i = 0; i < merge->nrecv; i++) {
2148: PETSC_UNUSED PetscMPIInt icompleted;
2149: PetscCallMPI(MPI_Waitany(merge->nrecv, rwaits, &icompleted, &rstatus));
2150: }
2151: PetscCall(PetscFree(rwaits));
2152: if (merge->nsend) PetscCallMPI(MPI_Waitall(merge->nsend, swaits, sstatus));
2154: /* add received column indices into table to update Armax */
2155: /* Armax can be as large as aN if a P[row,:] is dense, see src/ksp/ksp/tutorials/ex56.c! */
2156: for (k = 0; k < merge->nrecv; k++) { /* k-th received message */
2157: Jptr = buf_rj[k];
2158: for (j = 0; j < merge->len_r[k]; j++) PetscCall(PetscHMapISet(ta, *(Jptr + j) + 1, 1));
2159: }
2160: PetscCall(PetscHMapIGetSize(ta, &Armax));
2162: /* send and recv coi */
2163: PetscCall(PetscCommGetNewTag(comm, &tagi));
2164: PetscCall(PetscPostIrecvInt(comm, tagi, merge->nrecv, merge->id_r, len_ri, &buf_ri, &rwaits));
2165: PetscCall(PetscMalloc1(len, &buf_s));
2166: buf_si = buf_s; /* points to the beginning of k-th msg to be sent */
2167: for (proc = 0, k = 0; proc < size; proc++) {
2168: if (!len_s[proc]) continue;
2169: /* form outgoing message for i-structure:
2170: buf_si[0]: nrows to be sent
2171: [1:nrows]: row index (global)
2172: [nrows+1:2*nrows+1]: i-structure index
2173: */
2174: nrows = len_si[proc] / 2 - 1;
2175: buf_si_i = buf_si + nrows + 1;
2176: buf_si[0] = nrows;
2177: buf_si_i[0] = 0;
2178: nrows = 0;
2179: for (i = owners_co[proc]; i < owners_co[proc + 1]; i++) {
2180: nzi = coi[i + 1] - coi[i];
2181: buf_si_i[nrows + 1] = buf_si_i[nrows] + nzi; /* i-structure */
2182: buf_si[nrows + 1] = prmap[i] - owners[proc]; /* local row index */
2183: nrows++;
2184: }
2185: PetscCallMPI(MPIU_Isend(buf_si, len_si[proc], MPIU_INT, proc, tagi, comm, swaits + k));
2186: k++;
2187: buf_si += len_si[proc];
2188: }
2189: i = merge->nrecv;
2190: while (i--) {
2191: PETSC_UNUSED PetscMPIInt icompleted;
2192: PetscCallMPI(MPI_Waitany(merge->nrecv, rwaits, &icompleted, &rstatus));
2193: }
2194: PetscCall(PetscFree(rwaits));
2195: if (merge->nsend) PetscCallMPI(MPI_Waitall(merge->nsend, swaits, sstatus));
2196: PetscCall(PetscFree(len_si));
2197: PetscCall(PetscFree(len_ri));
2198: PetscCall(PetscFree(swaits));
2199: PetscCall(PetscFree(sstatus));
2200: PetscCall(PetscFree(buf_s));
2202: /* compute the local portion of C (mpi mat) */
2203: /* allocate bi array and free space for accumulating nonzero column info */
2204: PetscCall(PetscMalloc1(pn + 1, &bi));
2205: bi[0] = 0;
2207: /* set initial free space to be fill*(nnz(P) + nnz(AP)) */
2208: nnz = PetscRealIntMultTruncate(fill, PetscIntSumTruncate(pdti[pn], PetscIntSumTruncate(poti[pon], ai[am])));
2209: PetscCall(PetscFreeSpaceGet(nnz, &free_space));
2210: current_space = free_space;
2212: PetscCall(PetscMalloc3(merge->nrecv, &buf_ri_k, merge->nrecv, &nextrow, merge->nrecv, &nextci));
2213: for (k = 0; k < merge->nrecv; k++) {
2214: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
2215: nrows = *buf_ri_k[k];
2216: nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */
2217: nextci[k] = buf_ri_k[k] + (nrows + 1); /* points to the next i-structure of k-th received i-structure */
2218: }
2220: PetscCall(PetscLLCondensedCreate_Scalable(Armax, &lnk));
2221: MatPreallocateBegin(comm, pn, A->cmap->n, dnz, onz);
2222: rmax = 0;
2223: for (i = 0; i < pn; i++) {
2224: /* add pdt[i,:]*AP into lnk */
2225: pnz = pdti[i + 1] - pdti[i];
2226: ptJ = pdtj + pdti[i];
2227: for (j = 0; j < pnz; j++) {
2228: row = ptJ[j]; /* row of AP == col of Pt */
2229: anz = ai[row + 1] - ai[row];
2230: Jptr = aj + ai[row];
2231: /* add non-zero cols of AP into the sorted linked list lnk */
2232: PetscCall(PetscLLCondensedAddSorted_Scalable(anz, Jptr, lnk));
2233: }
2235: /* add received col data into lnk */
2236: for (k = 0; k < merge->nrecv; k++) { /* k-th received message */
2237: if (i == *nextrow[k]) { /* i-th row */
2238: nzi = *(nextci[k] + 1) - *nextci[k];
2239: Jptr = buf_rj[k] + *nextci[k];
2240: PetscCall(PetscLLCondensedAddSorted_Scalable(nzi, Jptr, lnk));
2241: nextrow[k]++;
2242: nextci[k]++;
2243: }
2244: }
2246: /* add missing diagonal entry */
2247: if (C->force_diagonals) {
2248: k = i + owners[rank]; /* column index */
2249: PetscCall(PetscLLCondensedAddSorted_Scalable(1, &k, lnk));
2250: }
2252: nnz = lnk[0];
2254: /* if free space is not available, make more free space */
2255: if (current_space->local_remaining < nnz) {
2256: PetscCall(PetscFreeSpaceGet(PetscIntSumTruncate(nnz, current_space->total_array_size), ¤t_space));
2257: nspacedouble++;
2258: }
2259: /* copy data into free space, then initialize lnk */
2260: PetscCall(PetscLLCondensedClean_Scalable(nnz, current_space->array, lnk));
2261: PetscCall(MatPreallocateSet(i + owners[rank], nnz, current_space->array, dnz, onz));
2263: current_space->array += nnz;
2264: current_space->local_used += nnz;
2265: current_space->local_remaining -= nnz;
2267: bi[i + 1] = bi[i] + nnz;
2268: if (nnz > rmax) rmax = nnz;
2269: }
2270: PetscCall(PetscFree3(buf_ri_k, nextrow, nextci));
2272: PetscCall(PetscMalloc1(bi[pn], &bj));
2273: PetscCall(PetscFreeSpaceContiguous(&free_space, bj));
2274: afill_tmp = (PetscReal)bi[pn] / (pdti[pn] + poti[pon] + ai[am] + 1);
2275: if (afill_tmp > afill) afill = afill_tmp;
2276: PetscCall(PetscLLCondensedDestroy_Scalable(lnk));
2277: PetscCall(PetscHMapIDestroy(&ta));
2278: PetscCall(MatRestoreSymbolicTranspose_SeqAIJ(p->A, &pdti, &pdtj));
2279: PetscCall(MatRestoreSymbolicTranspose_SeqAIJ(p->B, &poti, &potj));
2281: /* create symbolic parallel matrix C - why cannot be assembled in Numeric part */
2282: PetscCall(MatSetSizes(C, pn, A->cmap->n, PETSC_DETERMINE, PETSC_DETERMINE));
2283: PetscCall(MatSetBlockSizes(C, P->cmap->bs, A->cmap->bs));
2284: PetscCall(MatGetType(A, &mtype));
2285: PetscCall(MatSetType(C, C->structure_only ? MATMPIAIJ : mtype));
2286: PetscCall(MatMPIAIJSetPreallocation(C, 0, dnz, 0, onz));
2287: MatPreallocateEnd(dnz, onz);
2288: PetscCall(MatSetBlockSize(C, 1));
2289: PetscCall(MatSetOption(C, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
2290: for (i = 0; i < pn; i++) {
2291: row = i + rstart;
2292: nnz = bi[i + 1] - bi[i];
2293: Jptr = bj + bi[i];
2294: PetscCall(MatSetValues(C, 1, &row, nnz, Jptr, NULL, INSERT_VALUES));
2295: }
2296: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
2297: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
2298: PetscCall(MatSetOption(C, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
2299: merge->bi = bi;
2300: merge->bj = bj;
2301: merge->coi = coi;
2302: merge->coj = coj;
2303: merge->buf_ri = buf_ri;
2304: merge->buf_rj = buf_rj;
2305: merge->owners_co = owners_co;
2307: /* attach the supporting struct to C for reuse */
2308: C->product->data = ap;
2309: C->product->destroy = MatProductCtxDestroy_MPIAIJ_PtAP;
2310: ap->merge = merge;
2312: C->ops->mattransposemultnumeric = MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ;
2314: if (PetscDefined(USE_INFO)) {
2315: if (bi[pn] != 0) {
2316: PetscCall(PetscInfo(C, "Reallocs %" PetscInt_FMT "; Fill ratio: given %g needed %g.\n", nspacedouble, (double)fill, (double)afill));
2317: PetscCall(PetscInfo(C, "Use MatTransposeMatMult(A,B,MatReuse,%g,&C) for best performance.\n", (double)afill));
2318: } else PetscCall(PetscInfo(C, "Empty matrix product\n"));
2319: }
2320: PetscFunctionReturn(PETSC_SUCCESS);
2321: }
2323: static PetscErrorCode MatProductSymbolic_AtB_MPIAIJ_MPIAIJ(Mat C)
2324: {
2325: Mat_Product *product = C->product;
2326: Mat A = product->A, B = product->B;
2327: PetscReal fill = product->fill;
2328: PetscBool flg;
2330: PetscFunctionBegin;
2331: /* scalable */
2332: PetscCall(PetscStrcmp(product->alg, "scalable", &flg));
2333: if (flg || C->structure_only) {
2334: if (C->structure_only) PetscCall(MatProductSetAlgorithm(C, "scalable"));
2335: PetscCall(MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ(A, B, fill, C));
2336: goto next;
2337: }
2339: /* nonscalable */
2340: PetscCall(PetscStrcmp(product->alg, "nonscalable", &flg));
2341: if (flg) {
2342: PetscCall(MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(A, B, fill, C));
2343: goto next;
2344: }
2346: /* matmatmult */
2347: PetscCall(PetscStrcmp(product->alg, "at*b", &flg));
2348: if (flg) {
2349: Mat At;
2350: MatProductCtx_APMPI *ptap;
2352: PetscCall(MatTranspose(A, MAT_INITIAL_MATRIX, &At));
2353: PetscCall(MatMatMultSymbolic_MPIAIJ_MPIAIJ(At, B, fill, C));
2354: ptap = (MatProductCtx_APMPI *)C->product->data;
2355: if (ptap) {
2356: ptap->Pt = At;
2357: C->product->destroy = MatProductCtxDestroy_MPIAIJ_PtAP;
2358: }
2359: C->ops->transposematmultnumeric = MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_matmatmult;
2360: goto next;
2361: }
2363: /* backend general code */
2364: PetscCall(PetscStrcmp(product->alg, "backend", &flg));
2365: if (flg) {
2366: PetscCall(MatProductSymbolic_MPIAIJBACKEND(C));
2367: PetscFunctionReturn(PETSC_SUCCESS);
2368: }
2370: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "MatProduct type is not supported");
2372: next:
2373: C->ops->productnumeric = MatProductNumeric_AtB;
2374: PetscFunctionReturn(PETSC_SUCCESS);
2375: }
2377: /* Set options for MatMatMultxxx_MPIAIJ_MPIAIJ */
2378: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_AB(Mat C)
2379: {
2380: Mat_Product *product = C->product;
2381: Mat A = product->A, B = product->B;
2382: #if PetscDefined(HAVE_HYPRE)
2383: const char *algTypes[5] = {"scalable", "nonscalable", "seqmpi", "backend", "hypre"};
2384: PetscInt nalg = 5;
2385: #else
2386: const char *algTypes[4] = {
2387: "scalable",
2388: "nonscalable",
2389: "seqmpi",
2390: "backend",
2391: };
2392: PetscInt nalg = 4;
2393: #endif
2394: PetscInt alg = 1; /* set nonscalable algorithm as default */
2395: PetscBool flg;
2396: MPI_Comm comm;
2398: PetscFunctionBegin;
2399: PetscCall(PetscObjectGetComm((PetscObject)C, &comm));
2401: /* Set "nonscalable" as default algorithm */
2402: PetscCall(PetscStrcmp(C->product->alg, "default", &flg));
2403: if (flg) {
2404: PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2406: /* Set "scalable" as default if BN and local nonzeros of A and B are large */
2407: if (B->cmap->N > 100000) { /* may switch to scalable algorithm as default */
2408: MatInfo Ainfo, Binfo;
2409: PetscInt nz_local;
2410: PetscBool alg_scalable = PETSC_FALSE;
2412: PetscCall(MatGetInfo(A, MAT_LOCAL, &Ainfo));
2413: PetscCall(MatGetInfo(B, MAT_LOCAL, &Binfo));
2414: nz_local = (PetscInt)(Ainfo.nz_allocated + Binfo.nz_allocated);
2416: if (B->cmap->N > product->fill * nz_local) alg_scalable = PETSC_TRUE;
2417: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &alg_scalable, 1, MPI_C_BOOL, MPI_LOR, comm));
2419: if (alg_scalable) {
2420: alg = 0; /* scalable algorithm would 50% slower than nonscalable algorithm */
2421: PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2422: PetscCall(PetscInfo(B, "Use scalable algorithm, BN %" PetscInt_FMT ", fill*nz_allocated %g\n", B->cmap->N, (double)(product->fill * nz_local)));
2423: }
2424: }
2425: }
2427: /* Get runtime option */
2428: if (product->api_user) {
2429: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatMatMult", "Mat");
2430: PetscCall(PetscOptionsEList("-matmatmult_via", "Algorithmic approach", "MatMatMult", algTypes, nalg, algTypes[alg], &alg, &flg));
2431: PetscOptionsEnd();
2432: } else {
2433: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatProduct_AB", "Mat");
2434: PetscCall(PetscOptionsEList("-mat_product_algorithm", "Algorithmic approach", "MatMatMult", algTypes, nalg, algTypes[alg], &alg, &flg));
2435: PetscOptionsEnd();
2436: }
2437: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2439: C->ops->productsymbolic = MatProductSymbolic_AB_MPIAIJ_MPIAIJ;
2440: PetscFunctionReturn(PETSC_SUCCESS);
2441: }
2443: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_ABt(Mat C)
2444: {
2445: PetscFunctionBegin;
2446: PetscCall(MatProductSetFromOptions_MPIAIJ_AB(C));
2447: C->ops->productsymbolic = MatProductSymbolic_ABt_MPIAIJ_MPIAIJ;
2448: PetscFunctionReturn(PETSC_SUCCESS);
2449: }
2451: /* Set options for MatTransposeMatMultXXX_MPIAIJ_MPIAIJ */
2452: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_AtB(Mat C)
2453: {
2454: Mat_Product *product = C->product;
2455: Mat A = product->A, B = product->B;
2456: const char *algTypes[4] = {"scalable", "nonscalable", "at*b", "backend"};
2457: PetscInt nalg = 4;
2458: PetscInt alg = 1; /* set default algorithm */
2459: PetscBool flg;
2460: MPI_Comm comm;
2462: PetscFunctionBegin;
2463: /* Check matrix local sizes */
2464: PetscCall(PetscObjectGetComm((PetscObject)C, &comm));
2465: PetscCheck(A->rmap->rstart == B->rmap->rstart && A->rmap->rend == B->rmap->rend, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Matrix local dimensions are incompatible, A (%" PetscInt_FMT ", %" PetscInt_FMT ") != B (%" PetscInt_FMT ",%" PetscInt_FMT ")",
2466: A->rmap->rstart, A->rmap->rend, B->rmap->rstart, B->rmap->rend);
2468: /* Set default algorithm */
2469: PetscCall(PetscStrcmp(C->product->alg, "default", &flg));
2470: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2472: /* Set "scalable" as default if BN and local nonzeros of A and B are large */
2473: if (alg && B->cmap->N > 100000) { /* may switch to scalable algorithm as default */
2474: MatInfo Ainfo, Binfo;
2475: PetscInt nz_local;
2476: PetscBool alg_scalable = PETSC_FALSE;
2478: PetscCall(MatGetInfo(A, MAT_LOCAL, &Ainfo));
2479: PetscCall(MatGetInfo(B, MAT_LOCAL, &Binfo));
2480: nz_local = (PetscInt)(Ainfo.nz_allocated + Binfo.nz_allocated);
2482: if (B->cmap->N > product->fill * nz_local) alg_scalable = PETSC_TRUE;
2483: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &alg_scalable, 1, MPI_C_BOOL, MPI_LOR, comm));
2485: if (alg_scalable) {
2486: alg = 0; /* scalable algorithm would 50% slower than nonscalable algorithm */
2487: PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2488: PetscCall(PetscInfo(B, "Use scalable algorithm, BN %" PetscInt_FMT ", fill*nz_allocated %g\n", B->cmap->N, (double)(product->fill * nz_local)));
2489: }
2490: }
2492: /* Get runtime option */
2493: if (product->api_user) {
2494: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatTransposeMatMult", "Mat");
2495: PetscCall(PetscOptionsEList("-mattransposematmult_via", "Algorithmic approach", "MatTransposeMatMult", algTypes, nalg, algTypes[alg], &alg, &flg));
2496: PetscOptionsEnd();
2497: } else {
2498: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatProduct_AtB", "Mat");
2499: PetscCall(PetscOptionsEList("-mat_product_algorithm", "Algorithmic approach", "MatTransposeMatMult", algTypes, nalg, algTypes[alg], &alg, &flg));
2500: PetscOptionsEnd();
2501: }
2502: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2504: C->ops->productsymbolic = MatProductSymbolic_AtB_MPIAIJ_MPIAIJ;
2505: PetscFunctionReturn(PETSC_SUCCESS);
2506: }
2508: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_PtAP(Mat C)
2509: {
2510: Mat_Product *product = C->product;
2511: Mat A = product->A, P = product->B;
2512: MPI_Comm comm;
2513: PetscBool flg;
2514: PetscInt alg = 1; /* set default algorithm */
2515: #if !PetscDefined(HAVE_HYPRE)
2516: const char *algTypes[5] = {"scalable", "nonscalable", "allatonce", "allatonce_merged", "backend"};
2517: PetscInt nalg = 5;
2518: #else
2519: const char *algTypes[6] = {"scalable", "nonscalable", "allatonce", "allatonce_merged", "backend", "hypre"};
2520: PetscInt nalg = 6;
2521: #endif
2522: PetscInt pN = P->cmap->N;
2524: PetscFunctionBegin;
2525: /* Check matrix local sizes */
2526: PetscCall(PetscObjectGetComm((PetscObject)C, &comm));
2527: PetscCheck(A->rmap->rstart == P->rmap->rstart && A->rmap->rend == P->rmap->rend, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Matrix local dimensions are incompatible, Arow (%" PetscInt_FMT ", %" PetscInt_FMT ") != Prow (%" PetscInt_FMT ",%" PetscInt_FMT ")",
2528: A->rmap->rstart, A->rmap->rend, P->rmap->rstart, P->rmap->rend);
2529: PetscCheck(A->cmap->rstart == P->rmap->rstart && A->cmap->rend == P->rmap->rend, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Matrix local dimensions are incompatible, Acol (%" PetscInt_FMT ", %" PetscInt_FMT ") != Prow (%" PetscInt_FMT ",%" PetscInt_FMT ")",
2530: A->cmap->rstart, A->cmap->rend, P->rmap->rstart, P->rmap->rend);
2532: /* Set "nonscalable" as default algorithm */
2533: PetscCall(PetscStrcmp(C->product->alg, "default", &flg));
2534: if (flg) {
2535: PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2537: /* Set "scalable" as default if BN and local nonzeros of A and B are large */
2538: if (pN > 100000) {
2539: MatInfo Ainfo, Pinfo;
2540: PetscInt nz_local;
2541: PetscBool alg_scalable = PETSC_FALSE;
2543: PetscCall(MatGetInfo(A, MAT_LOCAL, &Ainfo));
2544: PetscCall(MatGetInfo(P, MAT_LOCAL, &Pinfo));
2545: nz_local = (PetscInt)(Ainfo.nz_allocated + Pinfo.nz_allocated);
2547: if (pN > product->fill * nz_local) alg_scalable = PETSC_TRUE;
2548: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &alg_scalable, 1, MPI_C_BOOL, MPI_LOR, comm));
2550: if (alg_scalable) {
2551: alg = 0; /* scalable algorithm would 50% slower than nonscalable algorithm */
2552: PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2553: }
2554: }
2555: }
2557: /* Get runtime option */
2558: if (product->api_user) {
2559: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatPtAP", "Mat");
2560: PetscCall(PetscOptionsEList("-matptap_via", "Algorithmic approach", "MatPtAP", algTypes, nalg, algTypes[alg], &alg, &flg));
2561: PetscOptionsEnd();
2562: } else {
2563: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatProduct_PtAP", "Mat");
2564: PetscCall(PetscOptionsEList("-mat_product_algorithm", "Algorithmic approach", "MatPtAP", algTypes, nalg, algTypes[alg], &alg, &flg));
2565: PetscOptionsEnd();
2566: }
2567: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2569: C->ops->productsymbolic = MatProductSymbolic_PtAP_MPIAIJ_MPIAIJ;
2570: PetscFunctionReturn(PETSC_SUCCESS);
2571: }
2573: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_RARt(Mat C)
2574: {
2575: Mat_Product *product = C->product;
2576: Mat A = product->A, R = product->B;
2578: PetscFunctionBegin;
2579: /* Check matrix local sizes */
2580: PetscCheck(A->cmap->n == R->cmap->n && A->rmap->n == R->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Matrix local dimensions are incompatible, A local (%" PetscInt_FMT ", %" PetscInt_FMT "), R local (%" PetscInt_FMT ",%" PetscInt_FMT ")", A->rmap->n,
2581: A->rmap->n, R->rmap->n, R->cmap->n);
2583: C->ops->productsymbolic = MatProductSymbolic_RARt_MPIAIJ_MPIAIJ;
2584: PetscFunctionReturn(PETSC_SUCCESS);
2585: }
2587: /*
2588: Set options for ABC = A*B*C = A*(B*C); ABC's algorithm must be chosen from AB's algorithm
2589: */
2590: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_ABC(Mat C)
2591: {
2592: Mat_Product *product = C->product;
2593: PetscBool flg = PETSC_FALSE;
2594: PetscInt alg = 1; /* default algorithm */
2595: const char *algTypes[3] = {"scalable", "nonscalable", "seqmpi"};
2596: PetscInt nalg = 3;
2598: PetscFunctionBegin;
2599: /* Set default algorithm */
2600: PetscCall(PetscStrcmp(C->product->alg, "default", &flg));
2601: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2603: /* Get runtime option */
2604: if (product->api_user) {
2605: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatMatMatMult", "Mat");
2606: PetscCall(PetscOptionsEList("-matmatmatmult_via", "Algorithmic approach", "MatMatMatMult", algTypes, nalg, algTypes[alg], &alg, &flg));
2607: PetscOptionsEnd();
2608: } else {
2609: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatProduct_ABC", "Mat");
2610: PetscCall(PetscOptionsEList("-mat_product_algorithm", "Algorithmic approach", "MatProduct_ABC", algTypes, nalg, algTypes[alg], &alg, &flg));
2611: PetscOptionsEnd();
2612: }
2613: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2615: C->ops->matmatmultsymbolic = MatMatMatMultSymbolic_MPIAIJ_MPIAIJ_MPIAIJ;
2616: C->ops->productsymbolic = MatProductSymbolic_ABC;
2617: PetscFunctionReturn(PETSC_SUCCESS);
2618: }
2620: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_MPIAIJ(Mat C)
2621: {
2622: Mat_Product *product = C->product;
2624: PetscFunctionBegin;
2625: switch (product->type) {
2626: case MATPRODUCT_AB:
2627: PetscCall(MatProductSetFromOptions_MPIAIJ_AB(C));
2628: break;
2629: case MATPRODUCT_ABt:
2630: PetscCall(MatProductSetFromOptions_MPIAIJ_ABt(C));
2631: break;
2632: case MATPRODUCT_AtB:
2633: PetscCall(MatProductSetFromOptions_MPIAIJ_AtB(C));
2634: break;
2635: case MATPRODUCT_PtAP:
2636: PetscCall(MatProductSetFromOptions_MPIAIJ_PtAP(C));
2637: break;
2638: case MATPRODUCT_RARt:
2639: PetscCall(MatProductSetFromOptions_MPIAIJ_RARt(C));
2640: break;
2641: case MATPRODUCT_ABC:
2642: PetscCall(MatProductSetFromOptions_MPIAIJ_ABC(C));
2643: break;
2644: default:
2645: break;
2646: }
2647: PetscFunctionReturn(PETSC_SUCCESS);
2648: }