Actual source code: mpimatmatmult.c
2: /*
3: Defines matrix-matrix product routines for pairs of MPIAIJ matrices
4: C = A * B
5: */
6: #include <../src/mat/impls/aij/seq/aij.h>
7: #include <../src/mat/utils/freespace.h>
8: #include <../src/mat/impls/aij/mpi/mpiaij.h>
9: #include <petscbt.h>
10: #include <../src/mat/impls/dense/mpi/mpidense.h>
11: #include <petsc/private/vecimpl.h>
12: #include <petsc/private/sfimpl.h>
14: #if defined(PETSC_HAVE_HYPRE)
15: PETSC_INTERN PetscErrorCode MatMatMultSymbolic_AIJ_AIJ_wHYPRE(Mat,Mat,PetscReal,Mat);
16: #endif
18: PETSC_INTERN PetscErrorCode MatProductSymbolic_AB_MPIAIJ_MPIAIJ(Mat C)
19: {
20: PetscErrorCode ierr;
21: Mat_Product *product = C->product;
22: Mat A=product->A,B=product->B;
23: MatProductAlgorithm alg=product->alg;
24: PetscReal fill=product->fill;
25: PetscBool flg;
28: /* scalable */
29: PetscStrcmp(alg,"scalable",&flg);
30: if (flg) {
31: MatMatMultSymbolic_MPIAIJ_MPIAIJ(A,B,fill,C);
32: return(0);
33: }
35: /* nonscalable */
36: PetscStrcmp(alg,"nonscalable",&flg);
37: if (flg) {
38: MatMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(A,B,fill,C);
39: return(0);
40: }
42: /* seqmpi */
43: PetscStrcmp(alg,"seqmpi",&flg);
44: if (flg) {
45: MatMatMultSymbolic_MPIAIJ_MPIAIJ_seqMPI(A,B,fill,C);
46: return(0);
47: }
49: /* backend general code */
50: PetscStrcmp(alg,"backend",&flg);
51: if (flg) {
52: MatProductSymbolic_MPIAIJBACKEND(C);
53: return(0);
54: }
56: #if defined(PETSC_HAVE_HYPRE)
57: PetscStrcmp(alg,"hypre",&flg);
58: if (flg) {
59: MatMatMultSymbolic_AIJ_AIJ_wHYPRE(A,B,fill,C);
60: return(0);
61: }
62: #endif
63: SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_SUP,"Mat Product Algorithm is not supported");
64: }
66: PetscErrorCode MatDestroy_MPIAIJ_MatMatMult(void *data)
67: {
69: Mat_APMPI *ptap = (Mat_APMPI*)data;
72: PetscFree2(ptap->startsj_s,ptap->startsj_r);
73: PetscFree(ptap->bufa);
74: MatDestroy(&ptap->P_loc);
75: MatDestroy(&ptap->P_oth);
76: MatDestroy(&ptap->Pt);
77: PetscFree(ptap->api);
78: PetscFree(ptap->apj);
79: PetscFree(ptap->apa);
80: PetscFree(ptap);
81: return(0);
82: }
84: PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable(Mat A,Mat P,Mat C)
85: {
86: PetscErrorCode ierr;
87: Mat_MPIAIJ *a =(Mat_MPIAIJ*)A->data,*c=(Mat_MPIAIJ*)C->data;
88: Mat_SeqAIJ *ad =(Mat_SeqAIJ*)(a->A)->data,*ao=(Mat_SeqAIJ*)(a->B)->data;
89: Mat_SeqAIJ *cd =(Mat_SeqAIJ*)(c->A)->data,*co=(Mat_SeqAIJ*)(c->B)->data;
90: PetscScalar *cda=cd->a,*coa=co->a;
91: Mat_SeqAIJ *p_loc,*p_oth;
92: PetscScalar *apa,*ca;
93: PetscInt cm =C->rmap->n;
94: Mat_APMPI *ptap;
95: PetscInt *api,*apj,*apJ,i,k;
96: PetscInt cstart=C->cmap->rstart;
97: PetscInt cdnz,conz,k0,k1;
98: const PetscScalar *dummy;
99: MPI_Comm comm;
100: PetscMPIInt size;
103: MatCheckProduct(C,3);
104: ptap = (Mat_APMPI*)C->product->data;
105: if (!ptap) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"PtAP cannot be computed. Missing data");
106: PetscObjectGetComm((PetscObject)A,&comm);
107: MPI_Comm_size(comm,&size);
109: if (!ptap->P_oth && size>1) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"AP cannot be reused. Do not call MatProductClear()");
111: /* flag CPU mask for C */
112: #if defined(PETSC_HAVE_DEVICE)
113: if (C->offloadmask != PETSC_OFFLOAD_UNALLOCATED) C->offloadmask = PETSC_OFFLOAD_CPU;
114: if (c->A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) c->A->offloadmask = PETSC_OFFLOAD_CPU;
115: if (c->B->offloadmask != PETSC_OFFLOAD_UNALLOCATED) c->B->offloadmask = PETSC_OFFLOAD_CPU;
116: #endif
118: /* 1) get P_oth = ptap->P_oth and P_loc = ptap->P_loc */
119: /*-----------------------------------------------------*/
120: /* update numerical values of P_oth and P_loc */
121: MatGetBrowsOfAoCols_MPIAIJ(A,P,MAT_REUSE_MATRIX,&ptap->startsj_s,&ptap->startsj_r,&ptap->bufa,&ptap->P_oth);
122: MatMPIAIJGetLocalMat(P,MAT_REUSE_MATRIX,&ptap->P_loc);
124: /* 2) compute numeric C_loc = A_loc*P = Ad*P_loc + Ao*P_oth */
125: /*----------------------------------------------------------*/
126: /* get data from symbolic products */
127: p_loc = (Mat_SeqAIJ*)(ptap->P_loc)->data;
128: p_oth = NULL;
129: if (size >1) {
130: p_oth = (Mat_SeqAIJ*)(ptap->P_oth)->data;
131: }
133: /* get apa for storing dense row A[i,:]*P */
134: apa = ptap->apa;
136: api = ptap->api;
137: apj = ptap->apj;
138: /* trigger copy to CPU */
139: MatSeqAIJGetArrayRead(a->A,&dummy);
140: MatSeqAIJRestoreArrayRead(a->A,&dummy);
141: MatSeqAIJGetArrayRead(a->B,&dummy);
142: MatSeqAIJRestoreArrayRead(a->B,&dummy);
143: for (i=0; i<cm; i++) {
144: /* compute apa = A[i,:]*P */
145: AProw_nonscalable(i,ad,ao,p_loc,p_oth,apa);
147: /* set values in C */
148: apJ = apj + api[i];
149: cdnz = cd->i[i+1] - cd->i[i];
150: conz = co->i[i+1] - co->i[i];
152: /* 1st off-diagonal part of C */
153: ca = coa + co->i[i];
154: k = 0;
155: for (k0=0; k0<conz; k0++) {
156: if (apJ[k] >= cstart) break;
157: ca[k0] = apa[apJ[k]];
158: apa[apJ[k++]] = 0.0;
159: }
161: /* diagonal part of C */
162: ca = cda + cd->i[i];
163: for (k1=0; k1<cdnz; k1++) {
164: ca[k1] = apa[apJ[k]];
165: apa[apJ[k++]] = 0.0;
166: }
168: /* 2nd off-diagonal part of C */
169: ca = coa + co->i[i];
170: for (; k0<conz; k0++) {
171: ca[k0] = apa[apJ[k]];
172: apa[apJ[k++]] = 0.0;
173: }
174: }
175: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
176: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
177: return(0);
178: }
180: PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(Mat A,Mat P,PetscReal fill,Mat C)
181: {
182: PetscErrorCode ierr;
183: MPI_Comm comm;
184: PetscMPIInt size;
185: Mat_APMPI *ptap;
186: PetscFreeSpaceList free_space=NULL,current_space=NULL;
187: Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data;
188: Mat_SeqAIJ *ad=(Mat_SeqAIJ*)(a->A)->data,*ao=(Mat_SeqAIJ*)(a->B)->data,*p_loc,*p_oth;
189: PetscInt *pi_loc,*pj_loc,*pi_oth,*pj_oth,*dnz,*onz;
190: PetscInt *adi=ad->i,*adj=ad->j,*aoi=ao->i,*aoj=ao->j,rstart=A->rmap->rstart;
191: PetscInt *lnk,i,pnz,row,*api,*apj,*Jptr,apnz,nspacedouble=0,j,nzi;
192: PetscInt am=A->rmap->n,pN=P->cmap->N,pn=P->cmap->n,pm=P->rmap->n;
193: PetscBT lnkbt;
194: PetscReal afill;
195: MatType mtype;
198: MatCheckProduct(C,4);
199: if (C->product->data) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Extra product struct not empty");
200: PetscObjectGetComm((PetscObject)A,&comm);
201: MPI_Comm_size(comm,&size);
203: /* create struct Mat_APMPI and attached it to C later */
204: PetscNew(&ptap);
206: /* get P_oth by taking rows of P (= non-zero cols of local A) from other processors */
207: MatGetBrowsOfAoCols_MPIAIJ(A,P,MAT_INITIAL_MATRIX,&ptap->startsj_s,&ptap->startsj_r,&ptap->bufa,&ptap->P_oth);
209: /* get P_loc by taking all local rows of P */
210: MatMPIAIJGetLocalMat(P,MAT_INITIAL_MATRIX,&ptap->P_loc);
212: p_loc = (Mat_SeqAIJ*)(ptap->P_loc)->data;
213: pi_loc = p_loc->i; pj_loc = p_loc->j;
214: if (size > 1) {
215: p_oth = (Mat_SeqAIJ*)(ptap->P_oth)->data;
216: pi_oth = p_oth->i; pj_oth = p_oth->j;
217: } else {
218: p_oth = NULL;
219: pi_oth = NULL; pj_oth = NULL;
220: }
222: /* first, compute symbolic AP = A_loc*P = A_diag*P_loc + A_off*P_oth */
223: /*-------------------------------------------------------------------*/
224: PetscMalloc1(am+2,&api);
225: ptap->api = api;
226: api[0] = 0;
228: /* create and initialize a linked list */
229: PetscLLCondensedCreate(pN,pN,&lnk,&lnkbt);
231: /* Initial FreeSpace size is fill*(nnz(A)+nnz(P)) */
232: PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,PetscIntSumTruncate(adi[am],PetscIntSumTruncate(aoi[am],pi_loc[pm]))),&free_space);
233: current_space = free_space;
235: MatPreallocateInitialize(comm,am,pn,dnz,onz);
236: for (i=0; i<am; i++) {
237: /* diagonal portion of A */
238: nzi = adi[i+1] - adi[i];
239: for (j=0; j<nzi; j++) {
240: row = *adj++;
241: pnz = pi_loc[row+1] - pi_loc[row];
242: Jptr = pj_loc + pi_loc[row];
243: /* add non-zero cols of P into the sorted linked list lnk */
244: PetscLLCondensedAddSorted(pnz,Jptr,lnk,lnkbt);
245: }
246: /* off-diagonal portion of A */
247: nzi = aoi[i+1] - aoi[i];
248: for (j=0; j<nzi; j++) {
249: row = *aoj++;
250: pnz = pi_oth[row+1] - pi_oth[row];
251: Jptr = pj_oth + pi_oth[row];
252: PetscLLCondensedAddSorted(pnz,Jptr,lnk,lnkbt);
253: }
254: /* add possible missing diagonal entry */
255: if (C->force_diagonals) {
256: j = i + rstart; /* column index */
257: PetscLLCondensedAddSorted(1,&j,lnk,lnkbt);
258: }
260: apnz = lnk[0];
261: api[i+1] = api[i] + apnz;
263: /* if free space is not available, double the total space in the list */
264: if (current_space->local_remaining<apnz) {
265: PetscFreeSpaceGet(PetscIntSumTruncate(apnz,current_space->total_array_size),¤t_space);
266: nspacedouble++;
267: }
269: /* Copy data into free space, then initialize lnk */
270: PetscLLCondensedClean(pN,apnz,current_space->array,lnk,lnkbt);
271: MatPreallocateSet(i+rstart,apnz,current_space->array,dnz,onz);
273: current_space->array += apnz;
274: current_space->local_used += apnz;
275: current_space->local_remaining -= apnz;
276: }
278: /* Allocate space for apj, initialize apj, and */
279: /* destroy list of free space and other temporary array(s) */
280: PetscMalloc1(api[am]+1,&ptap->apj);
281: apj = ptap->apj;
282: PetscFreeSpaceContiguous(&free_space,ptap->apj);
283: PetscLLDestroy(lnk,lnkbt);
285: /* malloc apa to store dense row A[i,:]*P */
286: PetscCalloc1(pN,&ptap->apa);
288: /* set and assemble symbolic parallel matrix C */
289: /*---------------------------------------------*/
290: MatSetSizes(C,am,pn,PETSC_DETERMINE,PETSC_DETERMINE);
291: MatSetBlockSizesFromMats(C,A,P);
293: MatGetType(A,&mtype);
294: MatSetType(C,mtype);
295: MatMPIAIJSetPreallocation(C,0,dnz,0,onz);
296: MatPreallocateFinalize(dnz,onz);
298: MatSetValues_MPIAIJ_CopyFromCSRFormat_Symbolic(C, apj, api);
299: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
300: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
301: MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
303: C->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable;
304: C->ops->productnumeric = MatProductNumeric_AB;
306: /* attach the supporting struct to C for reuse */
307: C->product->data = ptap;
308: C->product->destroy = MatDestroy_MPIAIJ_MatMatMult;
310: /* set MatInfo */
311: afill = (PetscReal)api[am]/(adi[am]+aoi[am]+pi_loc[pm]+1) + 1.e-5;
312: if (afill < 1.0) afill = 1.0;
313: C->info.mallocs = nspacedouble;
314: C->info.fill_ratio_given = fill;
315: C->info.fill_ratio_needed = afill;
317: #if defined(PETSC_USE_INFO)
318: if (api[am]) {
319: PetscInfo3(C,"Reallocs %D; Fill ratio: given %g needed %g.\n",nspacedouble,(double)fill,(double)afill);
320: PetscInfo1(C,"Use MatMatMult(A,B,MatReuse,%g,&C) for best performance.;\n",(double)afill);
321: } else {
322: PetscInfo(C,"Empty matrix product\n");
323: }
324: #endif
325: return(0);
326: }
328: /* ------------------------------------------------------- */
329: static PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIDense(Mat,Mat,PetscReal,Mat);
330: static PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIDense(Mat,Mat,Mat);
332: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_MPIDense_AB(Mat C)
333: {
334: Mat_Product *product = C->product;
335: Mat A = product->A,B=product->B;
338: if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend)
339: SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%D, %D) != (%D,%D)",A->cmap->rstart,A->cmap->rend,B->rmap->rstart,B->rmap->rend);
341: C->ops->matmultsymbolic = MatMatMultSymbolic_MPIAIJ_MPIDense;
342: C->ops->productsymbolic = MatProductSymbolic_AB;
343: return(0);
344: }
345: /* -------------------------------------------------------------------- */
346: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_MPIDense_AtB(Mat C)
347: {
348: Mat_Product *product = C->product;
349: Mat A = product->A,B=product->B;
352: if (A->rmap->rstart != B->rmap->rstart || A->rmap->rend != B->rmap->rend)
353: SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%D, %D) != (%D,%D)",A->rmap->rstart,A->rmap->rend,B->rmap->rstart,B->rmap->rend);
355: C->ops->transposematmultsymbolic = MatTransposeMatMultSymbolic_MPIAIJ_MPIDense;
356: C->ops->productsymbolic = MatProductSymbolic_AtB;
357: return(0);
358: }
360: /* --------------------------------------------------------------------- */
361: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_MPIAIJ_MPIDense(Mat C)
362: {
364: Mat_Product *product = C->product;
367: switch (product->type) {
368: case MATPRODUCT_AB:
369: MatProductSetFromOptions_MPIAIJ_MPIDense_AB(C);
370: break;
371: case MATPRODUCT_AtB:
372: MatProductSetFromOptions_MPIAIJ_MPIDense_AtB(C);
373: break;
374: default:
375: break;
376: }
377: return(0);
378: }
379: /* ------------------------------------------------------- */
381: typedef struct {
382: Mat workB,workB1;
383: MPI_Request *rwaits,*swaits;
384: PetscInt nsends,nrecvs;
385: MPI_Datatype *stype,*rtype;
386: PetscInt blda;
387: } MPIAIJ_MPIDense;
389: PetscErrorCode MatMPIAIJ_MPIDenseDestroy(void *ctx)
390: {
391: MPIAIJ_MPIDense *contents = (MPIAIJ_MPIDense*)ctx;
392: PetscErrorCode ierr;
393: PetscInt i;
396: MatDestroy(&contents->workB);
397: MatDestroy(&contents->workB1);
398: for (i=0; i<contents->nsends; i++) {
399: MPI_Type_free(&contents->stype[i]);
400: }
401: for (i=0; i<contents->nrecvs; i++) {
402: MPI_Type_free(&contents->rtype[i]);
403: }
404: PetscFree4(contents->stype,contents->rtype,contents->rwaits,contents->swaits);
405: PetscFree(contents);
406: return(0);
407: }
409: static PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIDense(Mat A,Mat B,PetscReal fill,Mat C)
410: {
411: PetscErrorCode ierr;
412: Mat_MPIAIJ *aij=(Mat_MPIAIJ*)A->data;
413: PetscInt nz=aij->B->cmap->n,nsends,nrecvs,i,nrows_to,j,blda,clda,m,M,n,N;
414: MPIAIJ_MPIDense *contents;
415: VecScatter ctx=aij->Mvctx;
416: PetscInt Am=A->rmap->n,Bm=B->rmap->n,BN=B->cmap->N,Bbn,Bbn1,bs,nrows_from,numBb;
417: MPI_Comm comm;
418: MPI_Datatype type1,*stype,*rtype;
419: const PetscInt *sindices,*sstarts,*rstarts;
420: PetscMPIInt *disp;
421: PetscBool cisdense;
424: MatCheckProduct(C,4);
425: if (C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data not empty");
426: PetscObjectGetComm((PetscObject)A,&comm);
427: PetscObjectBaseTypeCompare((PetscObject)C,MATMPIDENSE,&cisdense);
428: if (!cisdense) {
429: MatSetType(C,((PetscObject)B)->type_name);
430: }
431: MatGetLocalSize(C,&m,&n);
432: MatGetSize(C,&M,&N);
433: if (m == PETSC_DECIDE || n == PETSC_DECIDE || M == PETSC_DECIDE || N == PETSC_DECIDE) {
434: MatSetSizes(C,Am,B->cmap->n,A->rmap->N,BN);
435: }
436: MatSetBlockSizesFromMats(C,A,B);
437: MatSetUp(C);
438: MatDenseGetLDA(B,&blda);
439: MatDenseGetLDA(C,&clda);
440: PetscNew(&contents);
442: VecScatterGetRemote_Private(ctx,PETSC_TRUE/*send*/,&nsends,&sstarts,&sindices,NULL,NULL);
443: VecScatterGetRemoteOrdered_Private(ctx,PETSC_FALSE/*recv*/,&nrecvs,&rstarts,NULL,NULL,NULL);
445: /* Create column block of B and C for memory scalability when BN is too large */
446: /* Estimate Bbn, column size of Bb */
447: if (nz) {
448: Bbn1 = 2*Am*BN/nz;
449: if (!Bbn1) Bbn1 = 1;
450: } else Bbn1 = BN;
452: bs = PetscAbs(B->cmap->bs);
453: Bbn1 = Bbn1/bs *bs; /* Bbn1 is a multiple of bs */
454: if (Bbn1 > BN) Bbn1 = BN;
455: MPI_Allreduce(&Bbn1,&Bbn,1,MPIU_INT,MPI_MAX,comm);
457: /* Enable runtime option for Bbn */
458: PetscOptionsBegin(comm,((PetscObject)C)->prefix,"MatMatMult","Mat");
459: PetscOptionsInt("-matmatmult_Bbn","Number of columns in Bb","MatMatMult",Bbn,&Bbn,NULL);
460: PetscOptionsEnd();
461: Bbn = PetscMin(Bbn,BN);
463: if (Bbn > 0 && Bbn < BN) {
464: numBb = BN/Bbn;
465: Bbn1 = BN - numBb*Bbn;
466: } else numBb = 0;
468: if (numBb) {
469: PetscInfo3(C,"use Bb, BN=%D, Bbn=%D; numBb=%D\n",BN,Bbn,numBb);
470: if (Bbn1) { /* Create workB1 for the remaining columns */
471: PetscInfo2(C,"use Bb1, BN=%D, Bbn1=%D\n",BN,Bbn1);
472: /* Create work matrix used to store off processor rows of B needed for local product */
473: MatCreateSeqDense(PETSC_COMM_SELF,nz,Bbn1,NULL,&contents->workB1);
474: } else contents->workB1 = NULL;
475: }
477: /* Create work matrix used to store off processor rows of B needed for local product */
478: MatCreateSeqDense(PETSC_COMM_SELF,nz,Bbn,NULL,&contents->workB);
480: /* Use MPI derived data type to reduce memory required by the send/recv buffers */
481: PetscMalloc4(nsends,&stype,nrecvs,&rtype,nrecvs,&contents->rwaits,nsends,&contents->swaits);
482: contents->stype = stype;
483: contents->nsends = nsends;
485: contents->rtype = rtype;
486: contents->nrecvs = nrecvs;
487: contents->blda = blda;
489: PetscMalloc1(Bm+1,&disp);
490: for (i=0; i<nsends; i++) {
491: nrows_to = sstarts[i+1]-sstarts[i];
492: for (j=0; j<nrows_to; j++){
493: disp[j] = sindices[sstarts[i]+j]; /* rowB to be sent */
494: }
495: MPI_Type_create_indexed_block(nrows_to,1,(const PetscMPIInt *)disp,MPIU_SCALAR,&type1);
497: MPI_Type_create_resized(type1,0,blda*sizeof(PetscScalar),&stype[i]);
498: MPI_Type_commit(&stype[i]);
499: MPI_Type_free(&type1);
500: }
502: for (i=0; i<nrecvs; i++) {
503: /* received values from a process form a (nrows_from x Bbn) row block in workB (column-wise) */
504: nrows_from = rstarts[i+1]-rstarts[i];
505: disp[0] = 0;
506: MPI_Type_create_indexed_block(1, nrows_from, (const PetscMPIInt *)disp, MPIU_SCALAR, &type1);
507: MPI_Type_create_resized(type1, 0, nz*sizeof(PetscScalar), &rtype[i]);
508: MPI_Type_commit(&rtype[i]);
509: MPI_Type_free(&type1);
510: }
512: PetscFree(disp);
513: VecScatterRestoreRemote_Private(ctx,PETSC_TRUE/*send*/,&nsends,&sstarts,&sindices,NULL,NULL);
514: VecScatterRestoreRemoteOrdered_Private(ctx,PETSC_FALSE/*recv*/,&nrecvs,&rstarts,NULL,NULL,NULL);
515: MatSetOption(C,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);
516: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
517: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
518: MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
520: C->product->data = contents;
521: C->product->destroy = MatMPIAIJ_MPIDenseDestroy;
522: C->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIDense;
523: return(0);
524: }
526: PETSC_INTERN PetscErrorCode MatMatMultNumericAdd_SeqAIJ_SeqDense(Mat,Mat,Mat,const PetscBool);
527: /*
528: Performs an efficient scatter on the rows of B needed by this process; this is
529: a modification of the VecScatterBegin_() routines.
531: Input: Bbidx = 0: B = Bb
532: = 1: B = Bb1, see MatMatMultSymbolic_MPIAIJ_MPIDense()
533: */
534: PetscErrorCode MatMPIDenseScatter(Mat A,Mat B,PetscInt Bbidx,Mat C,Mat *outworkB)
535: {
536: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)A->data;
537: PetscErrorCode ierr;
538: const PetscScalar *b;
539: PetscScalar *rvalues;
540: VecScatter ctx = aij->Mvctx;
541: const PetscInt *sindices,*sstarts,*rstarts;
542: const PetscMPIInt *sprocs,*rprocs;
543: PetscInt i,nsends,nrecvs;
544: MPI_Request *swaits,*rwaits;
545: MPI_Comm comm;
546: PetscMPIInt tag=((PetscObject)ctx)->tag,ncols=B->cmap->N,nrows=aij->B->cmap->n,nsends_mpi,nrecvs_mpi;
547: MPIAIJ_MPIDense *contents;
548: Mat workB;
549: MPI_Datatype *stype,*rtype;
550: PetscInt blda;
553: MatCheckProduct(C,4);
554: if (!C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data empty");
555: contents = (MPIAIJ_MPIDense*)C->product->data;
556: VecScatterGetRemote_Private(ctx,PETSC_TRUE/*send*/,&nsends,&sstarts,&sindices,&sprocs,NULL/*bs*/);
557: VecScatterGetRemoteOrdered_Private(ctx,PETSC_FALSE/*recv*/,&nrecvs,&rstarts,NULL,&rprocs,NULL/*bs*/);
558: PetscMPIIntCast(nsends,&nsends_mpi);
559: PetscMPIIntCast(nrecvs,&nrecvs_mpi);
560: if (Bbidx == 0) {
561: workB = *outworkB = contents->workB;
562: } else {
563: workB = *outworkB = contents->workB1;
564: }
565: if (nrows != workB->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Number of rows of workB %D not equal to columns of aij->B %D",workB->cmap->n,nrows);
566: swaits = contents->swaits;
567: rwaits = contents->rwaits;
569: MatDenseGetArrayRead(B,&b);
570: MatDenseGetLDA(B,&blda);
571: if (blda != contents->blda) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot reuse an input matrix with lda %D != %D",blda,contents->blda);
572: MatDenseGetArray(workB,&rvalues);
574: /* Post recv, use MPI derived data type to save memory */
575: PetscObjectGetComm((PetscObject)C,&comm);
576: rtype = contents->rtype;
577: for (i=0; i<nrecvs; i++) {
578: MPI_Irecv(rvalues+(rstarts[i]-rstarts[0]),ncols,rtype[i],rprocs[i],tag,comm,rwaits+i);
579: }
581: stype = contents->stype;
582: for (i=0; i<nsends; i++) {
583: MPI_Isend(b,ncols,stype[i],sprocs[i],tag,comm,swaits+i);
584: }
586: if (nrecvs) {MPI_Waitall(nrecvs_mpi,rwaits,MPI_STATUSES_IGNORE);}
587: if (nsends) {MPI_Waitall(nsends_mpi,swaits,MPI_STATUSES_IGNORE);}
589: VecScatterRestoreRemote_Private(ctx,PETSC_TRUE/*send*/,&nsends,&sstarts,&sindices,&sprocs,NULL);
590: VecScatterRestoreRemoteOrdered_Private(ctx,PETSC_FALSE/*recv*/,&nrecvs,&rstarts,NULL,&rprocs,NULL);
591: MatDenseRestoreArrayRead(B,&b);
592: MatDenseRestoreArray(workB,&rvalues);
593: return(0);
594: }
596: static PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIDense(Mat A,Mat B,Mat C)
597: {
598: PetscErrorCode ierr;
599: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)A->data;
600: Mat_MPIDense *bdense = (Mat_MPIDense*)B->data;
601: Mat_MPIDense *cdense = (Mat_MPIDense*)C->data;
602: Mat workB;
603: MPIAIJ_MPIDense *contents;
606: MatCheckProduct(C,3);
607: if (!C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data empty");
608: contents = (MPIAIJ_MPIDense*)C->product->data;
609: /* diagonal block of A times all local rows of B */
610: /* TODO: this calls a symbolic multiplication every time, which could be avoided */
611: MatMatMult(aij->A,bdense->A,MAT_REUSE_MATRIX,PETSC_DEFAULT,&cdense->A);
612: if (contents->workB->cmap->n == B->cmap->N) {
613: /* get off processor parts of B needed to complete C=A*B */
614: MatMPIDenseScatter(A,B,0,C,&workB);
616: /* off-diagonal block of A times nonlocal rows of B */
617: MatMatMultNumericAdd_SeqAIJ_SeqDense(aij->B,workB,cdense->A,PETSC_TRUE);
618: } else {
619: Mat Bb,Cb;
620: PetscInt BN=B->cmap->N,n=contents->workB->cmap->n,i;
621: if (n <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Column block size %D must be positive",n);
623: for (i=0; i<BN; i+=n) {
624: MatDenseGetSubMatrix(B,i,PetscMin(i+n,BN),&Bb);
625: MatDenseGetSubMatrix(C,i,PetscMin(i+n,BN),&Cb);
627: /* get off processor parts of B needed to complete C=A*B */
628: MatMPIDenseScatter(A,Bb,i+n>BN,C,&workB);
630: /* off-diagonal block of A times nonlocal rows of B */
631: cdense = (Mat_MPIDense*)Cb->data;
632: MatMatMultNumericAdd_SeqAIJ_SeqDense(aij->B,workB,cdense->A,PETSC_TRUE);
634: MatDenseRestoreSubMatrix(B,&Bb);
635: MatDenseRestoreSubMatrix(C,&Cb);
636: }
637: }
638: return(0);
639: }
641: PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIAIJ(Mat A,Mat P,Mat C)
642: {
643: PetscErrorCode ierr;
644: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data,*c=(Mat_MPIAIJ*)C->data;
645: Mat_SeqAIJ *ad = (Mat_SeqAIJ*)(a->A)->data,*ao=(Mat_SeqAIJ*)(a->B)->data;
646: Mat_SeqAIJ *cd = (Mat_SeqAIJ*)(c->A)->data,*co=(Mat_SeqAIJ*)(c->B)->data;
647: PetscInt *adi = ad->i,*adj,*aoi=ao->i,*aoj;
648: PetscScalar *ada,*aoa,*cda=cd->a,*coa=co->a;
649: Mat_SeqAIJ *p_loc,*p_oth;
650: PetscInt *pi_loc,*pj_loc,*pi_oth,*pj_oth,*pj;
651: PetscScalar *pa_loc,*pa_oth,*pa,valtmp,*ca;
652: PetscInt cm = C->rmap->n,anz,pnz;
653: Mat_APMPI *ptap;
654: PetscScalar *apa_sparse;
655: const PetscScalar *dummy;
656: PetscInt *api,*apj,*apJ,i,j,k,row;
657: PetscInt cstart = C->cmap->rstart;
658: PetscInt cdnz,conz,k0,k1,nextp;
659: MPI_Comm comm;
660: PetscMPIInt size;
663: MatCheckProduct(C,3);
664: ptap = (Mat_APMPI*)C->product->data;
665: if (!ptap) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"PtAP cannot be computed. Missing data");
666: PetscObjectGetComm((PetscObject)C,&comm);
667: MPI_Comm_size(comm,&size);
668: if (!ptap->P_oth && size>1) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"AP cannot be reused. Do not call MatProductClear()");
670: /* flag CPU mask for C */
671: #if defined(PETSC_HAVE_DEVICE)
672: if (C->offloadmask != PETSC_OFFLOAD_UNALLOCATED) C->offloadmask = PETSC_OFFLOAD_CPU;
673: if (c->A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) c->A->offloadmask = PETSC_OFFLOAD_CPU;
674: if (c->B->offloadmask != PETSC_OFFLOAD_UNALLOCATED) c->B->offloadmask = PETSC_OFFLOAD_CPU;
675: #endif
676: apa_sparse = ptap->apa;
678: /* 1) get P_oth = ptap->P_oth and P_loc = ptap->P_loc */
679: /*-----------------------------------------------------*/
680: /* update numerical values of P_oth and P_loc */
681: MatGetBrowsOfAoCols_MPIAIJ(A,P,MAT_REUSE_MATRIX,&ptap->startsj_s,&ptap->startsj_r,&ptap->bufa,&ptap->P_oth);
682: MatMPIAIJGetLocalMat(P,MAT_REUSE_MATRIX,&ptap->P_loc);
684: /* 2) compute numeric C_loc = A_loc*P = Ad*P_loc + Ao*P_oth */
685: /*----------------------------------------------------------*/
686: /* get data from symbolic products */
687: p_loc = (Mat_SeqAIJ*)(ptap->P_loc)->data;
688: pi_loc = p_loc->i; pj_loc = p_loc->j; pa_loc = p_loc->a;
689: if (size >1) {
690: p_oth = (Mat_SeqAIJ*)(ptap->P_oth)->data;
691: pi_oth = p_oth->i; pj_oth = p_oth->j; pa_oth = p_oth->a;
692: } else {
693: p_oth = NULL; pi_oth = NULL; pj_oth = NULL; pa_oth = NULL;
694: }
696: /* trigger copy to CPU */
697: MatSeqAIJGetArrayRead(a->A,&dummy);
698: MatSeqAIJRestoreArrayRead(a->A,&dummy);
699: MatSeqAIJGetArrayRead(a->B,&dummy);
700: MatSeqAIJRestoreArrayRead(a->B,&dummy);
701: api = ptap->api;
702: apj = ptap->apj;
703: for (i=0; i<cm; i++) {
704: apJ = apj + api[i];
706: /* diagonal portion of A */
707: anz = adi[i+1] - adi[i];
708: adj = ad->j + adi[i];
709: ada = ad->a + adi[i];
710: for (j=0; j<anz; j++) {
711: row = adj[j];
712: pnz = pi_loc[row+1] - pi_loc[row];
713: pj = pj_loc + pi_loc[row];
714: pa = pa_loc + pi_loc[row];
715: /* perform sparse axpy */
716: valtmp = ada[j];
717: nextp = 0;
718: for (k=0; nextp<pnz; k++) {
719: if (apJ[k] == pj[nextp]) { /* column of AP == column of P */
720: apa_sparse[k] += valtmp*pa[nextp++];
721: }
722: }
723: PetscLogFlops(2.0*pnz);
724: }
726: /* off-diagonal portion of A */
727: anz = aoi[i+1] - aoi[i];
728: aoj = ao->j + aoi[i];
729: aoa = ao->a + aoi[i];
730: for (j=0; j<anz; j++) {
731: row = aoj[j];
732: pnz = pi_oth[row+1] - pi_oth[row];
733: pj = pj_oth + pi_oth[row];
734: pa = pa_oth + pi_oth[row];
735: /* perform sparse axpy */
736: valtmp = aoa[j];
737: nextp = 0;
738: for (k=0; nextp<pnz; k++) {
739: if (apJ[k] == pj[nextp]) { /* column of AP == column of P */
740: apa_sparse[k] += valtmp*pa[nextp++];
741: }
742: }
743: PetscLogFlops(2.0*pnz);
744: }
746: /* set values in C */
747: cdnz = cd->i[i+1] - cd->i[i];
748: conz = co->i[i+1] - co->i[i];
750: /* 1st off-diagonal part of C */
751: ca = coa + co->i[i];
752: k = 0;
753: for (k0=0; k0<conz; k0++) {
754: if (apJ[k] >= cstart) break;
755: ca[k0] = apa_sparse[k];
756: apa_sparse[k] = 0.0;
757: k++;
758: }
760: /* diagonal part of C */
761: ca = cda + cd->i[i];
762: for (k1=0; k1<cdnz; k1++) {
763: ca[k1] = apa_sparse[k];
764: apa_sparse[k] = 0.0;
765: k++;
766: }
768: /* 2nd off-diagonal part of C */
769: ca = coa + co->i[i];
770: for (; k0<conz; k0++) {
771: ca[k0] = apa_sparse[k];
772: apa_sparse[k] = 0.0;
773: k++;
774: }
775: }
776: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
777: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
778: return(0);
779: }
781: /* same as MatMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(), except using LLCondensed to avoid O(BN) memory requirement */
782: PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIAIJ(Mat A,Mat P,PetscReal fill,Mat C)
783: {
784: PetscErrorCode ierr;
785: MPI_Comm comm;
786: PetscMPIInt size;
787: Mat_APMPI *ptap;
788: PetscFreeSpaceList free_space = NULL,current_space=NULL;
789: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
790: Mat_SeqAIJ *ad = (Mat_SeqAIJ*)(a->A)->data,*ao=(Mat_SeqAIJ*)(a->B)->data,*p_loc,*p_oth;
791: PetscInt *pi_loc,*pj_loc,*pi_oth,*pj_oth,*dnz,*onz;
792: PetscInt *adi=ad->i,*adj=ad->j,*aoi=ao->i,*aoj=ao->j,rstart=A->rmap->rstart;
793: PetscInt i,pnz,row,*api,*apj,*Jptr,apnz,nspacedouble=0,j,nzi,*lnk,apnz_max=1;
794: PetscInt am=A->rmap->n,pn=P->cmap->n,pm=P->rmap->n,lsize=pn+20;
795: PetscReal afill;
796: MatType mtype;
799: MatCheckProduct(C,4);
800: if (C->product->data) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Extra product struct not empty");
801: PetscObjectGetComm((PetscObject)A,&comm);
802: MPI_Comm_size(comm,&size);
804: /* create struct Mat_APMPI and attached it to C later */
805: PetscNew(&ptap);
807: /* get P_oth by taking rows of P (= non-zero cols of local A) from other processors */
808: MatGetBrowsOfAoCols_MPIAIJ(A,P,MAT_INITIAL_MATRIX,&ptap->startsj_s,&ptap->startsj_r,&ptap->bufa,&ptap->P_oth);
810: /* get P_loc by taking all local rows of P */
811: MatMPIAIJGetLocalMat(P,MAT_INITIAL_MATRIX,&ptap->P_loc);
813: p_loc = (Mat_SeqAIJ*)(ptap->P_loc)->data;
814: pi_loc = p_loc->i; pj_loc = p_loc->j;
815: if (size > 1) {
816: p_oth = (Mat_SeqAIJ*)(ptap->P_oth)->data;
817: pi_oth = p_oth->i; pj_oth = p_oth->j;
818: } else {
819: p_oth = NULL;
820: pi_oth = NULL; pj_oth = NULL;
821: }
823: /* first, compute symbolic AP = A_loc*P = A_diag*P_loc + A_off*P_oth */
824: /*-------------------------------------------------------------------*/
825: PetscMalloc1(am+2,&api);
826: ptap->api = api;
827: api[0] = 0;
829: PetscLLCondensedCreate_Scalable(lsize,&lnk);
831: /* Initial FreeSpace size is fill*(nnz(A)+nnz(P)) */
832: PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,PetscIntSumTruncate(adi[am],PetscIntSumTruncate(aoi[am],pi_loc[pm]))),&free_space);
833: current_space = free_space;
834: MatPreallocateInitialize(comm,am,pn,dnz,onz);
835: for (i=0; i<am; i++) {
836: /* diagonal portion of A */
837: nzi = adi[i+1] - adi[i];
838: for (j=0; j<nzi; j++) {
839: row = *adj++;
840: pnz = pi_loc[row+1] - pi_loc[row];
841: Jptr = pj_loc + pi_loc[row];
842: /* Expand list if it is not long enough */
843: if (pnz+apnz_max > lsize) {
844: lsize = pnz+apnz_max;
845: PetscLLCondensedExpand_Scalable(lsize, &lnk);
846: }
847: /* add non-zero cols of P into the sorted linked list lnk */
848: PetscLLCondensedAddSorted_Scalable(pnz,Jptr,lnk);
849: apnz = *lnk; /* The first element in the list is the number of items in the list */
850: api[i+1] = api[i] + apnz;
851: if (apnz > apnz_max) apnz_max = apnz + 1; /* '1' for diagonal entry */
852: }
853: /* off-diagonal portion of A */
854: nzi = aoi[i+1] - aoi[i];
855: for (j=0; j<nzi; j++) {
856: row = *aoj++;
857: pnz = pi_oth[row+1] - pi_oth[row];
858: Jptr = pj_oth + pi_oth[row];
859: /* Expand list if it is not long enough */
860: if (pnz+apnz_max > lsize) {
861: lsize = pnz + apnz_max;
862: PetscLLCondensedExpand_Scalable(lsize, &lnk);
863: }
864: /* add non-zero cols of P into the sorted linked list lnk */
865: PetscLLCondensedAddSorted_Scalable(pnz,Jptr,lnk);
866: apnz = *lnk; /* The first element in the list is the number of items in the list */
867: api[i+1] = api[i] + apnz;
868: if (apnz > apnz_max) apnz_max = apnz + 1; /* '1' for diagonal entry */
869: }
871: /* add missing diagonal entry */
872: if (C->force_diagonals) {
873: j = i + rstart; /* column index */
874: PetscLLCondensedAddSorted_Scalable(1,&j,lnk);
875: }
877: apnz = *lnk;
878: api[i+1] = api[i] + apnz;
879: if (apnz > apnz_max) apnz_max = apnz;
881: /* if free space is not available, double the total space in the list */
882: if (current_space->local_remaining<apnz) {
883: PetscFreeSpaceGet(PetscIntSumTruncate(apnz,current_space->total_array_size),¤t_space);
884: nspacedouble++;
885: }
887: /* Copy data into free space, then initialize lnk */
888: PetscLLCondensedClean_Scalable(apnz,current_space->array,lnk);
889: MatPreallocateSet(i+rstart,apnz,current_space->array,dnz,onz);
891: current_space->array += apnz;
892: current_space->local_used += apnz;
893: current_space->local_remaining -= apnz;
894: }
896: /* Allocate space for apj, initialize apj, and */
897: /* destroy list of free space and other temporary array(s) */
898: PetscMalloc1(api[am]+1,&ptap->apj);
899: apj = ptap->apj;
900: PetscFreeSpaceContiguous(&free_space,ptap->apj);
901: PetscLLCondensedDestroy_Scalable(lnk);
903: /* create and assemble symbolic parallel matrix C */
904: /*----------------------------------------------------*/
905: MatSetSizes(C,am,pn,PETSC_DETERMINE,PETSC_DETERMINE);
906: MatSetBlockSizesFromMats(C,A,P);
907: MatGetType(A,&mtype);
908: MatSetType(C,mtype);
909: MatMPIAIJSetPreallocation(C,0,dnz,0,onz);
910: MatPreallocateFinalize(dnz,onz);
912: /* malloc apa for assembly C */
913: PetscCalloc1(apnz_max,&ptap->apa);
915: MatSetValues_MPIAIJ_CopyFromCSRFormat_Symbolic(C, apj, api);
916: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
917: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
918: MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
920: C->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIAIJ;
921: C->ops->productnumeric = MatProductNumeric_AB;
923: /* attach the supporting struct to C for reuse */
924: C->product->data = ptap;
925: C->product->destroy = MatDestroy_MPIAIJ_MatMatMult;
927: /* set MatInfo */
928: afill = (PetscReal)api[am]/(adi[am]+aoi[am]+pi_loc[pm]+1) + 1.e-5;
929: if (afill < 1.0) afill = 1.0;
930: C->info.mallocs = nspacedouble;
931: C->info.fill_ratio_given = fill;
932: C->info.fill_ratio_needed = afill;
934: #if defined(PETSC_USE_INFO)
935: if (api[am]) {
936: PetscInfo3(C,"Reallocs %D; Fill ratio: given %g needed %g.\n",nspacedouble,(double)fill,(double)afill);
937: PetscInfo1(C,"Use MatMatMult(A,B,MatReuse,%g,&C) for best performance.;\n",(double)afill);
938: } else {
939: PetscInfo(C,"Empty matrix product\n");
940: }
941: #endif
942: return(0);
943: }
945: /* This function is needed for the seqMPI matrix-matrix multiplication. */
946: /* Three input arrays are merged to one output array. The size of the */
947: /* output array is also output. Duplicate entries only show up once. */
948: static void Merge3SortedArrays(PetscInt size1, PetscInt *in1,
949: PetscInt size2, PetscInt *in2,
950: PetscInt size3, PetscInt *in3,
951: PetscInt *size4, PetscInt *out)
952: {
953: int i = 0, j = 0, k = 0, l = 0;
955: /* Traverse all three arrays */
956: while (i<size1 && j<size2 && k<size3) {
957: if (in1[i] < in2[j] && in1[i] < in3[k]) {
958: out[l++] = in1[i++];
959: }
960: else if (in2[j] < in1[i] && in2[j] < in3[k]) {
961: out[l++] = in2[j++];
962: }
963: else if (in3[k] < in1[i] && in3[k] < in2[j]) {
964: out[l++] = in3[k++];
965: }
966: else if (in1[i] == in2[j] && in1[i] < in3[k]) {
967: out[l++] = in1[i];
968: i++, j++;
969: }
970: else if (in1[i] == in3[k] && in1[i] < in2[j]) {
971: out[l++] = in1[i];
972: i++, k++;
973: }
974: else if (in3[k] == in2[j] && in2[j] < in1[i]) {
975: out[l++] = in2[j];
976: k++, j++;
977: }
978: else if (in1[i] == in2[j] && in1[i] == in3[k]) {
979: out[l++] = in1[i];
980: i++, j++, k++;
981: }
982: }
984: /* Traverse two remaining arrays */
985: while (i<size1 && j<size2) {
986: if (in1[i] < in2[j]) {
987: out[l++] = in1[i++];
988: }
989: else if (in1[i] > in2[j]) {
990: out[l++] = in2[j++];
991: }
992: else {
993: out[l++] = in1[i];
994: i++, j++;
995: }
996: }
998: while (i<size1 && k<size3) {
999: if (in1[i] < in3[k]) {
1000: out[l++] = in1[i++];
1001: }
1002: else if (in1[i] > in3[k]) {
1003: out[l++] = in3[k++];
1004: }
1005: else {
1006: out[l++] = in1[i];
1007: i++, k++;
1008: }
1009: }
1011: while (k<size3 && j<size2) {
1012: if (in3[k] < in2[j]) {
1013: out[l++] = in3[k++];
1014: }
1015: else if (in3[k] > in2[j]) {
1016: out[l++] = in2[j++];
1017: }
1018: else {
1019: out[l++] = in3[k];
1020: k++, j++;
1021: }
1022: }
1024: /* Traverse one remaining array */
1025: while (i<size1) out[l++] = in1[i++];
1026: while (j<size2) out[l++] = in2[j++];
1027: while (k<size3) out[l++] = in3[k++];
1029: *size4 = l;
1030: }
1032: /* This matrix-matrix multiplication algorithm divides the multiplication into three multiplications and */
1033: /* adds up the products. Two of these three multiplications are performed with existing (sequential) */
1034: /* matrix-matrix multiplications. */
1035: PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIAIJ_seqMPI(Mat A, Mat P, PetscReal fill, Mat C)
1036: {
1037: PetscErrorCode ierr;
1038: MPI_Comm comm;
1039: PetscMPIInt size;
1040: Mat_APMPI *ptap;
1041: PetscFreeSpaceList free_space_diag=NULL, current_space=NULL;
1042: Mat_MPIAIJ *a =(Mat_MPIAIJ*)A->data;
1043: Mat_SeqAIJ *ad =(Mat_SeqAIJ*)(a->A)->data,*ao=(Mat_SeqAIJ*)(a->B)->data,*p_loc;
1044: Mat_MPIAIJ *p =(Mat_MPIAIJ*)P->data;
1045: Mat_SeqAIJ *adpd_seq, *p_off, *aopoth_seq;
1046: PetscInt adponz, adpdnz;
1047: PetscInt *pi_loc,*dnz,*onz;
1048: PetscInt *adi=ad->i,*adj=ad->j,*aoi=ao->i,rstart=A->rmap->rstart;
1049: PetscInt *lnk,i, i1=0,pnz,row,*adpoi,*adpoj, *api, *adpoJ, *aopJ, *apJ,*Jptr, aopnz, nspacedouble=0,j,nzi,
1050: *apj,apnz, *adpdi, *adpdj, *adpdJ, *poff_i, *poff_j, *j_temp, *aopothi, *aopothj;
1051: PetscInt am=A->rmap->n,pN=P->cmap->N,pn=P->cmap->n,pm=P->rmap->n, p_colstart, p_colend;
1052: PetscBT lnkbt;
1053: PetscReal afill;
1054: PetscMPIInt rank;
1055: Mat adpd, aopoth;
1056: MatType mtype;
1057: const char *prefix;
1060: MatCheckProduct(C,4);
1061: if (C->product->data) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Extra product struct not empty");
1062: PetscObjectGetComm((PetscObject)A,&comm);
1063: MPI_Comm_size(comm,&size);
1064: MPI_Comm_rank(comm, &rank);
1065: MatGetOwnershipRangeColumn(P, &p_colstart, &p_colend);
1067: /* create struct Mat_APMPI and attached it to C later */
1068: PetscNew(&ptap);
1070: /* get P_oth by taking rows of P (= non-zero cols of local A) from other processors */
1071: MatGetBrowsOfAoCols_MPIAIJ(A,P,MAT_INITIAL_MATRIX,&ptap->startsj_s,&ptap->startsj_r,&ptap->bufa,&ptap->P_oth);
1073: /* get P_loc by taking all local rows of P */
1074: MatMPIAIJGetLocalMat(P,MAT_INITIAL_MATRIX,&ptap->P_loc);
1077: p_loc = (Mat_SeqAIJ*)(ptap->P_loc)->data;
1078: pi_loc = p_loc->i;
1080: /* Allocate memory for the i arrays of the matrices A*P, A_diag*P_off and A_offd * P */
1081: PetscMalloc1(am+2,&api);
1082: PetscMalloc1(am+2,&adpoi);
1084: adpoi[0] = 0;
1085: ptap->api = api;
1086: api[0] = 0;
1088: /* create and initialize a linked list, will be used for both A_diag * P_loc_off and A_offd * P_oth */
1089: PetscLLCondensedCreate(pN,pN,&lnk,&lnkbt);
1090: MatPreallocateInitialize(comm,am,pn,dnz,onz);
1092: /* Symbolic calc of A_loc_diag * P_loc_diag */
1093: MatGetOptionsPrefix(A,&prefix);
1094: MatProductCreate(a->A,p->A,NULL,&adpd);
1095: MatGetOptionsPrefix(A,&prefix);
1096: MatSetOptionsPrefix(adpd,prefix);
1097: MatAppendOptionsPrefix(adpd,"inner_diag_");
1099: MatProductSetType(adpd,MATPRODUCT_AB);
1100: MatProductSetAlgorithm(adpd,"sorted");
1101: MatProductSetFill(adpd,fill);
1102: MatProductSetFromOptions(adpd);
1104: adpd->force_diagonals = C->force_diagonals;
1105: MatProductSymbolic(adpd);
1107: adpd_seq = (Mat_SeqAIJ*)((adpd)->data);
1108: adpdi = adpd_seq->i; adpdj = adpd_seq->j;
1109: p_off = (Mat_SeqAIJ*)((p->B)->data);
1110: poff_i = p_off->i; poff_j = p_off->j;
1112: /* j_temp stores indices of a result row before they are added to the linked list */
1113: PetscMalloc1(pN+2,&j_temp);
1116: /* Symbolic calc of the A_diag * p_loc_off */
1117: /* Initial FreeSpace size is fill*(nnz(A)+nnz(P)) */
1118: PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,PetscIntSumTruncate(adi[am],PetscIntSumTruncate(aoi[am],pi_loc[pm]))),&free_space_diag);
1119: current_space = free_space_diag;
1121: for (i=0; i<am; i++) {
1122: /* A_diag * P_loc_off */
1123: nzi = adi[i+1] - adi[i];
1124: for (j=0; j<nzi; j++) {
1125: row = *adj++;
1126: pnz = poff_i[row+1] - poff_i[row];
1127: Jptr = poff_j + poff_i[row];
1128: for (i1 = 0; i1 < pnz; i1++) {
1129: j_temp[i1] = p->garray[Jptr[i1]];
1130: }
1131: /* add non-zero cols of P into the sorted linked list lnk */
1132: PetscLLCondensedAddSorted(pnz,j_temp,lnk,lnkbt);
1133: }
1135: adponz = lnk[0];
1136: adpoi[i+1] = adpoi[i] + adponz;
1138: /* if free space is not available, double the total space in the list */
1139: if (current_space->local_remaining<adponz) {
1140: PetscFreeSpaceGet(PetscIntSumTruncate(adponz,current_space->total_array_size),¤t_space);
1141: nspacedouble++;
1142: }
1144: /* Copy data into free space, then initialize lnk */
1145: PetscLLCondensedClean(pN,adponz,current_space->array,lnk,lnkbt);
1147: current_space->array += adponz;
1148: current_space->local_used += adponz;
1149: current_space->local_remaining -= adponz;
1150: }
1152: /* Symbolic calc of A_off * P_oth */
1153: MatSetOptionsPrefix(a->B,prefix);
1154: MatAppendOptionsPrefix(a->B,"inner_offdiag_");
1155: MatCreate(PETSC_COMM_SELF,&aopoth);
1156: MatMatMultSymbolic_SeqAIJ_SeqAIJ(a->B, ptap->P_oth, fill, aopoth);
1157: aopoth_seq = (Mat_SeqAIJ*)((aopoth)->data);
1158: aopothi = aopoth_seq->i; aopothj = aopoth_seq->j;
1160: /* Allocate space for apj, adpj, aopj, ... */
1161: /* destroy lists of free space and other temporary array(s) */
1163: PetscMalloc1(aopothi[am] + adpoi[am] + adpdi[am]+2, &ptap->apj);
1164: PetscMalloc1(adpoi[am]+2, &adpoj);
1166: /* Copy from linked list to j-array */
1167: PetscFreeSpaceContiguous(&free_space_diag,adpoj);
1168: PetscLLDestroy(lnk,lnkbt);
1170: adpoJ = adpoj;
1171: adpdJ = adpdj;
1172: aopJ = aopothj;
1173: apj = ptap->apj;
1174: apJ = apj; /* still empty */
1176: /* Merge j-arrays of A_off * P, A_diag * P_loc_off, and */
1177: /* A_diag * P_loc_diag to get A*P */
1178: for (i = 0; i < am; i++) {
1179: aopnz = aopothi[i+1] - aopothi[i];
1180: adponz = adpoi[i+1] - adpoi[i];
1181: adpdnz = adpdi[i+1] - adpdi[i];
1183: /* Correct indices from A_diag*P_diag */
1184: for (i1 = 0; i1 < adpdnz; i1++) {
1185: adpdJ[i1] += p_colstart;
1186: }
1187: /* Merge j-arrays of A_diag * P_loc_off and A_diag * P_loc_diag and A_off * P_oth */
1188: Merge3SortedArrays(adponz, adpoJ, adpdnz, adpdJ, aopnz, aopJ, &apnz, apJ);
1189: MatPreallocateSet(i+rstart, apnz, apJ, dnz, onz);
1191: aopJ += aopnz;
1192: adpoJ += adponz;
1193: adpdJ += adpdnz;
1194: apJ += apnz;
1195: api[i+1] = api[i] + apnz;
1196: }
1198: /* malloc apa to store dense row A[i,:]*P */
1199: PetscCalloc1(pN+2,&ptap->apa);
1201: /* create and assemble symbolic parallel matrix C */
1202: MatSetSizes(C,am,pn,PETSC_DETERMINE,PETSC_DETERMINE);
1203: MatSetBlockSizesFromMats(C,A,P);
1204: MatGetType(A,&mtype);
1205: MatSetType(C,mtype);
1206: MatMPIAIJSetPreallocation(C,0,dnz,0,onz);
1207: MatPreallocateFinalize(dnz,onz);
1209: MatSetValues_MPIAIJ_CopyFromCSRFormat_Symbolic(C, apj, api);
1210: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
1211: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
1212: MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
1214: C->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable;
1215: C->ops->productnumeric = MatProductNumeric_AB;
1217: /* attach the supporting struct to C for reuse */
1218: C->product->data = ptap;
1219: C->product->destroy = MatDestroy_MPIAIJ_MatMatMult;
1221: /* set MatInfo */
1222: afill = (PetscReal)api[am]/(adi[am]+aoi[am]+pi_loc[pm]+1) + 1.e-5;
1223: if (afill < 1.0) afill = 1.0;
1224: C->info.mallocs = nspacedouble;
1225: C->info.fill_ratio_given = fill;
1226: C->info.fill_ratio_needed = afill;
1228: #if defined(PETSC_USE_INFO)
1229: if (api[am]) {
1230: PetscInfo3(C,"Reallocs %D; Fill ratio: given %g needed %g.\n",nspacedouble,(double)fill,(double)afill);
1231: PetscInfo1(C,"Use MatMatMult(A,B,MatReuse,%g,&C) for best performance.;\n",(double)afill);
1232: } else {
1233: PetscInfo(C,"Empty matrix product\n");
1234: }
1235: #endif
1237: MatDestroy(&aopoth);
1238: MatDestroy(&adpd);
1239: PetscFree(j_temp);
1240: PetscFree(adpoj);
1241: PetscFree(adpoi);
1242: return(0);
1243: }
1245: /*-------------------------------------------------------------------------*/
1246: /* This routine only works when scall=MAT_REUSE_MATRIX! */
1247: PetscErrorCode MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_matmatmult(Mat P,Mat A,Mat C)
1248: {
1250: Mat_APMPI *ptap;
1251: Mat Pt;
1254: MatCheckProduct(C,3);
1255: ptap = (Mat_APMPI*)C->product->data;
1256: if (!ptap) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"PtAP cannot be computed. Missing data");
1257: if (!ptap->Pt) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"PtA cannot be reused. Do not call MatProductClear()");
1259: Pt = ptap->Pt;
1260: MatTranspose(P,MAT_REUSE_MATRIX,&Pt);
1261: MatMatMultNumeric_MPIAIJ_MPIAIJ(Pt,A,C);
1262: return(0);
1263: }
1265: /* This routine is modified from MatPtAPSymbolic_MPIAIJ_MPIAIJ() */
1266: PetscErrorCode MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(Mat P,Mat A,PetscReal fill,Mat C)
1267: {
1268: PetscErrorCode ierr;
1269: Mat_APMPI *ptap;
1270: Mat_MPIAIJ *p=(Mat_MPIAIJ*)P->data;
1271: MPI_Comm comm;
1272: PetscMPIInt size,rank;
1273: PetscFreeSpaceList free_space=NULL,current_space=NULL;
1274: PetscInt pn=P->cmap->n,aN=A->cmap->N,an=A->cmap->n;
1275: PetscInt *lnk,i,k,nsend,rstart;
1276: PetscBT lnkbt;
1277: PetscMPIInt tagi,tagj,*len_si,*len_s,*len_ri,nrecv;
1278: PETSC_UNUSED PetscMPIInt icompleted=0;
1279: PetscInt **buf_rj,**buf_ri,**buf_ri_k,row,ncols,*cols;
1280: PetscInt len,proc,*dnz,*onz,*owners,nzi;
1281: PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextci;
1282: MPI_Request *swaits,*rwaits;
1283: MPI_Status *sstatus,rstatus;
1284: PetscLayout rowmap;
1285: PetscInt *owners_co,*coi,*coj; /* i and j array of (p->B)^T*A*P - used in the communication */
1286: PetscMPIInt *len_r,*id_r; /* array of length of comm->size, store send/recv matrix values */
1287: PetscInt *Jptr,*prmap=p->garray,con,j,Crmax;
1288: Mat_SeqAIJ *a_loc,*c_loc,*c_oth;
1289: PetscTable ta;
1290: MatType mtype;
1291: const char *prefix;
1294: PetscObjectGetComm((PetscObject)A,&comm);
1295: MPI_Comm_size(comm,&size);
1296: MPI_Comm_rank(comm,&rank);
1298: /* create symbolic parallel matrix C */
1299: MatGetType(A,&mtype);
1300: MatSetType(C,mtype);
1302: C->ops->transposematmultnumeric = MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable;
1304: /* create struct Mat_APMPI and attached it to C later */
1305: PetscNew(&ptap);
1306: ptap->reuse = MAT_INITIAL_MATRIX;
1308: /* (0) compute Rd = Pd^T, Ro = Po^T */
1309: /* --------------------------------- */
1310: MatTranspose_SeqAIJ(p->A,MAT_INITIAL_MATRIX,&ptap->Rd);
1311: MatTranspose_SeqAIJ(p->B,MAT_INITIAL_MATRIX,&ptap->Ro);
1313: /* (1) compute symbolic A_loc */
1314: /* ---------------------------*/
1315: MatMPIAIJGetLocalMat(A,MAT_INITIAL_MATRIX,&ptap->A_loc);
1317: /* (2-1) compute symbolic C_oth = Ro*A_loc */
1318: /* ------------------------------------ */
1319: MatGetOptionsPrefix(A,&prefix);
1320: MatSetOptionsPrefix(ptap->Ro,prefix);
1321: MatAppendOptionsPrefix(ptap->Ro,"inner_offdiag_");
1322: MatCreate(PETSC_COMM_SELF,&ptap->C_oth);
1323: MatMatMultSymbolic_SeqAIJ_SeqAIJ(ptap->Ro,ptap->A_loc,fill,ptap->C_oth);
1325: /* (3) send coj of C_oth to other processors */
1326: /* ------------------------------------------ */
1327: /* determine row ownership */
1328: PetscLayoutCreate(comm,&rowmap);
1329: rowmap->n = pn;
1330: rowmap->bs = 1;
1331: PetscLayoutSetUp(rowmap);
1332: owners = rowmap->range;
1334: /* determine the number of messages to send, their lengths */
1335: PetscMalloc4(size,&len_s,size,&len_si,size,&sstatus,size+2,&owners_co);
1336: PetscArrayzero(len_s,size);
1337: PetscArrayzero(len_si,size);
1339: c_oth = (Mat_SeqAIJ*)ptap->C_oth->data;
1340: coi = c_oth->i; coj = c_oth->j;
1341: con = ptap->C_oth->rmap->n;
1342: proc = 0;
1343: for (i=0; i<con; i++) {
1344: while (prmap[i] >= owners[proc+1]) proc++;
1345: len_si[proc]++; /* num of rows in Co(=Pt*A) to be sent to [proc] */
1346: len_s[proc] += coi[i+1] - coi[i]; /* num of nonzeros in Co to be sent to [proc] */
1347: }
1349: len = 0; /* max length of buf_si[], see (4) */
1350: owners_co[0] = 0;
1351: nsend = 0;
1352: for (proc=0; proc<size; proc++) {
1353: owners_co[proc+1] = owners_co[proc] + len_si[proc];
1354: if (len_s[proc]) {
1355: nsend++;
1356: len_si[proc] = 2*(len_si[proc] + 1); /* length of buf_si to be sent to [proc] */
1357: len += len_si[proc];
1358: }
1359: }
1361: /* determine the number and length of messages to receive for coi and coj */
1362: PetscGatherNumberOfMessages(comm,NULL,len_s,&nrecv);
1363: PetscGatherMessageLengths2(comm,nsend,nrecv,len_s,len_si,&id_r,&len_r,&len_ri);
1365: /* post the Irecv and Isend of coj */
1366: PetscCommGetNewTag(comm,&tagj);
1367: PetscPostIrecvInt(comm,tagj,nrecv,id_r,len_r,&buf_rj,&rwaits);
1368: PetscMalloc1(nsend+1,&swaits);
1369: for (proc=0, k=0; proc<size; proc++) {
1370: if (!len_s[proc]) continue;
1371: i = owners_co[proc];
1372: MPI_Isend(coj+coi[i],len_s[proc],MPIU_INT,proc,tagj,comm,swaits+k);
1373: k++;
1374: }
1376: /* (2-2) compute symbolic C_loc = Rd*A_loc */
1377: /* ---------------------------------------- */
1378: MatSetOptionsPrefix(ptap->Rd,prefix);
1379: MatAppendOptionsPrefix(ptap->Rd,"inner_diag_");
1380: MatCreate(PETSC_COMM_SELF,&ptap->C_loc);
1381: MatMatMultSymbolic_SeqAIJ_SeqAIJ(ptap->Rd,ptap->A_loc,fill,ptap->C_loc);
1382: c_loc = (Mat_SeqAIJ*)ptap->C_loc->data;
1384: /* receives coj are complete */
1385: for (i=0; i<nrecv; i++) {
1386: MPI_Waitany(nrecv,rwaits,&icompleted,&rstatus);
1387: }
1388: PetscFree(rwaits);
1389: if (nsend) {MPI_Waitall(nsend,swaits,sstatus);}
1391: /* add received column indices into ta to update Crmax */
1392: a_loc = (Mat_SeqAIJ*)(ptap->A_loc)->data;
1394: /* create and initialize a linked list */
1395: PetscTableCreate(an,aN,&ta); /* for compute Crmax */
1396: MatRowMergeMax_SeqAIJ(a_loc,ptap->A_loc->rmap->N,ta);
1398: for (k=0; k<nrecv; k++) {/* k-th received message */
1399: Jptr = buf_rj[k];
1400: for (j=0; j<len_r[k]; j++) {
1401: PetscTableAdd(ta,*(Jptr+j)+1,1,INSERT_VALUES);
1402: }
1403: }
1404: PetscTableGetCount(ta,&Crmax);
1405: PetscTableDestroy(&ta);
1407: /* (4) send and recv coi */
1408: /*-----------------------*/
1409: PetscCommGetNewTag(comm,&tagi);
1410: PetscPostIrecvInt(comm,tagi,nrecv,id_r,len_ri,&buf_ri,&rwaits);
1411: PetscMalloc1(len+1,&buf_s);
1412: buf_si = buf_s; /* points to the beginning of k-th msg to be sent */
1413: for (proc=0,k=0; proc<size; proc++) {
1414: if (!len_s[proc]) continue;
1415: /* form outgoing message for i-structure:
1416: buf_si[0]: nrows to be sent
1417: [1:nrows]: row index (global)
1418: [nrows+1:2*nrows+1]: i-structure index
1419: */
1420: /*-------------------------------------------*/
1421: nrows = len_si[proc]/2 - 1; /* num of rows in Co to be sent to [proc] */
1422: buf_si_i = buf_si + nrows+1;
1423: buf_si[0] = nrows;
1424: buf_si_i[0] = 0;
1425: nrows = 0;
1426: for (i=owners_co[proc]; i<owners_co[proc+1]; i++) {
1427: nzi = coi[i+1] - coi[i];
1428: buf_si_i[nrows+1] = buf_si_i[nrows] + nzi; /* i-structure */
1429: buf_si[nrows+1] = prmap[i] -owners[proc]; /* local row index */
1430: nrows++;
1431: }
1432: MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,swaits+k);
1433: k++;
1434: buf_si += len_si[proc];
1435: }
1436: for (i=0; i<nrecv; i++) {
1437: MPI_Waitany(nrecv,rwaits,&icompleted,&rstatus);
1438: }
1439: PetscFree(rwaits);
1440: if (nsend) {MPI_Waitall(nsend,swaits,sstatus);}
1442: PetscFree4(len_s,len_si,sstatus,owners_co);
1443: PetscFree(len_ri);
1444: PetscFree(swaits);
1445: PetscFree(buf_s);
1447: /* (5) compute the local portion of C */
1448: /* ------------------------------------------ */
1449: /* set initial free space to be Crmax, sufficient for holding nozeros in each row of C */
1450: PetscFreeSpaceGet(Crmax,&free_space);
1451: current_space = free_space;
1453: PetscMalloc3(nrecv,&buf_ri_k,nrecv,&nextrow,nrecv,&nextci);
1454: for (k=0; k<nrecv; k++) {
1455: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
1456: nrows = *buf_ri_k[k];
1457: nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */
1458: nextci[k] = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure */
1459: }
1461: MatPreallocateInitialize(comm,pn,an,dnz,onz);
1462: PetscLLCondensedCreate(Crmax,aN,&lnk,&lnkbt);
1463: for (i=0; i<pn; i++) { /* for each local row of C */
1464: /* add C_loc into C */
1465: nzi = c_loc->i[i+1] - c_loc->i[i];
1466: Jptr = c_loc->j + c_loc->i[i];
1467: PetscLLCondensedAddSorted(nzi,Jptr,lnk,lnkbt);
1469: /* add received col data into lnk */
1470: for (k=0; k<nrecv; k++) { /* k-th received message */
1471: if (i == *nextrow[k]) { /* i-th row */
1472: nzi = *(nextci[k]+1) - *nextci[k];
1473: Jptr = buf_rj[k] + *nextci[k];
1474: PetscLLCondensedAddSorted(nzi,Jptr,lnk,lnkbt);
1475: nextrow[k]++; nextci[k]++;
1476: }
1477: }
1479: /* add missing diagonal entry */
1480: if (C->force_diagonals) {
1481: k = i + owners[rank]; /* column index */
1482: PetscLLCondensedAddSorted(1,&k,lnk,lnkbt);
1483: }
1485: nzi = lnk[0];
1487: /* copy data into free space, then initialize lnk */
1488: PetscLLCondensedClean(aN,nzi,current_space->array,lnk,lnkbt);
1489: MatPreallocateSet(i+owners[rank],nzi,current_space->array,dnz,onz);
1490: }
1491: PetscFree3(buf_ri_k,nextrow,nextci);
1492: PetscLLDestroy(lnk,lnkbt);
1493: PetscFreeSpaceDestroy(free_space);
1495: /* local sizes and preallocation */
1496: MatSetSizes(C,pn,an,PETSC_DETERMINE,PETSC_DETERMINE);
1497: if (P->cmap->bs > 0) {PetscLayoutSetBlockSize(C->rmap,P->cmap->bs);}
1498: if (A->cmap->bs > 0) {PetscLayoutSetBlockSize(C->cmap,A->cmap->bs);}
1499: MatMPIAIJSetPreallocation(C,0,dnz,0,onz);
1500: MatPreallocateFinalize(dnz,onz);
1502: /* add C_loc and C_oth to C */
1503: MatGetOwnershipRange(C,&rstart,NULL);
1504: for (i=0; i<pn; i++) {
1505: ncols = c_loc->i[i+1] - c_loc->i[i];
1506: cols = c_loc->j + c_loc->i[i];
1507: row = rstart + i;
1508: MatSetValues(C,1,(const PetscInt*)&row,ncols,(const PetscInt*)cols,NULL,INSERT_VALUES);
1510: if (C->force_diagonals) {
1511: MatSetValues(C,1,(const PetscInt*)&row,1,(const PetscInt*)&row,NULL,INSERT_VALUES);
1512: }
1513: }
1514: for (i=0; i<con; i++) {
1515: ncols = c_oth->i[i+1] - c_oth->i[i];
1516: cols = c_oth->j + c_oth->i[i];
1517: row = prmap[i];
1518: MatSetValues(C,1,(const PetscInt*)&row,ncols,(const PetscInt*)cols,NULL,INSERT_VALUES);
1519: }
1520: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
1521: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
1522: MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
1524: /* members in merge */
1525: PetscFree(id_r);
1526: PetscFree(len_r);
1527: PetscFree(buf_ri[0]);
1528: PetscFree(buf_ri);
1529: PetscFree(buf_rj[0]);
1530: PetscFree(buf_rj);
1531: PetscLayoutDestroy(&rowmap);
1533: /* attach the supporting struct to C for reuse */
1534: C->product->data = ptap;
1535: C->product->destroy = MatDestroy_MPIAIJ_PtAP;
1536: return(0);
1537: }
1539: PetscErrorCode MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable(Mat P,Mat A,Mat C)
1540: {
1541: PetscErrorCode ierr;
1542: Mat_MPIAIJ *p=(Mat_MPIAIJ*)P->data;
1543: Mat_SeqAIJ *c_seq;
1544: Mat_APMPI *ptap;
1545: Mat A_loc,C_loc,C_oth;
1546: PetscInt i,rstart,rend,cm,ncols,row;
1547: const PetscInt *cols;
1548: const PetscScalar *vals;
1551: MatCheckProduct(C,3);
1552: ptap = (Mat_APMPI*)C->product->data;
1553: if (!ptap) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"PtAP cannot be computed. Missing data");
1554: if (!ptap->A_loc) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"PtA cannot be reused. Do not call MatProductClear()");
1555: MatZeroEntries(C);
1557: if (ptap->reuse == MAT_REUSE_MATRIX) {
1558: /* These matrices are obtained in MatTransposeMatMultSymbolic() */
1559: /* 1) get R = Pd^T, Ro = Po^T */
1560: /*----------------------------*/
1561: MatTranspose_SeqAIJ(p->A,MAT_REUSE_MATRIX,&ptap->Rd);
1562: MatTranspose_SeqAIJ(p->B,MAT_REUSE_MATRIX,&ptap->Ro);
1564: /* 2) compute numeric A_loc */
1565: /*--------------------------*/
1566: MatMPIAIJGetLocalMat(A,MAT_REUSE_MATRIX,&ptap->A_loc);
1567: }
1569: /* 3) C_loc = Rd*A_loc, C_oth = Ro*A_loc */
1570: A_loc = ptap->A_loc;
1571: ((ptap->C_loc)->ops->matmultnumeric)(ptap->Rd,A_loc,ptap->C_loc);
1572: ((ptap->C_oth)->ops->matmultnumeric)(ptap->Ro,A_loc,ptap->C_oth);
1573: C_loc = ptap->C_loc;
1574: C_oth = ptap->C_oth;
1576: /* add C_loc and C_oth to C */
1577: MatGetOwnershipRange(C,&rstart,&rend);
1579: /* C_loc -> C */
1580: cm = C_loc->rmap->N;
1581: c_seq = (Mat_SeqAIJ*)C_loc->data;
1582: cols = c_seq->j;
1583: vals = c_seq->a;
1584: for (i=0; i<cm; i++) {
1585: ncols = c_seq->i[i+1] - c_seq->i[i];
1586: row = rstart + i;
1587: MatSetValues(C,1,&row,ncols,cols,vals,ADD_VALUES);
1588: cols += ncols; vals += ncols;
1589: }
1591: /* Co -> C, off-processor part */
1592: cm = C_oth->rmap->N;
1593: c_seq = (Mat_SeqAIJ*)C_oth->data;
1594: cols = c_seq->j;
1595: vals = c_seq->a;
1596: for (i=0; i<cm; i++) {
1597: ncols = c_seq->i[i+1] - c_seq->i[i];
1598: row = p->garray[i];
1599: MatSetValues(C,1,&row,ncols,cols,vals,ADD_VALUES);
1600: cols += ncols; vals += ncols;
1601: }
1602: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
1603: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
1604: MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
1606: ptap->reuse = MAT_REUSE_MATRIX;
1607: return(0);
1608: }
1610: PetscErrorCode MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ(Mat P,Mat A,Mat C)
1611: {
1612: PetscErrorCode ierr;
1613: Mat_Merge_SeqsToMPI *merge;
1614: Mat_MPIAIJ *p =(Mat_MPIAIJ*)P->data;
1615: Mat_SeqAIJ *pd=(Mat_SeqAIJ*)(p->A)->data,*po=(Mat_SeqAIJ*)(p->B)->data;
1616: Mat_APMPI *ptap;
1617: PetscInt *adj;
1618: PetscInt i,j,k,anz,pnz,row,*cj,nexta;
1619: MatScalar *ada,*ca,valtmp;
1620: PetscInt am=A->rmap->n,cm=C->rmap->n,pon=(p->B)->cmap->n;
1621: MPI_Comm comm;
1622: PetscMPIInt size,rank,taga,*len_s;
1623: PetscInt *owners,proc,nrows,**buf_ri_k,**nextrow,**nextci;
1624: PetscInt **buf_ri,**buf_rj;
1625: PetscInt cnz=0,*bj_i,*bi,*bj,bnz,nextcj; /* bi,bj,ba: local array of C(mpi mat) */
1626: MPI_Request *s_waits,*r_waits;
1627: MPI_Status *status;
1628: MatScalar **abuf_r,*ba_i,*pA,*coa,*ba;
1629: const PetscScalar *dummy;
1630: PetscInt *ai,*aj,*coi,*coj,*poJ,*pdJ;
1631: Mat A_loc;
1632: Mat_SeqAIJ *a_loc;
1635: MatCheckProduct(C,3);
1636: ptap = (Mat_APMPI*)C->product->data;
1637: if (!ptap) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"PtAP cannot be computed. Missing data");
1638: if (!ptap->A_loc) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"PtA cannot be reused. Do not call MatProductClear()");
1639: PetscObjectGetComm((PetscObject)C,&comm);
1640: MPI_Comm_size(comm,&size);
1641: MPI_Comm_rank(comm,&rank);
1643: merge = ptap->merge;
1645: /* 2) compute numeric C_seq = P_loc^T*A_loc */
1646: /*------------------------------------------*/
1647: /* get data from symbolic products */
1648: coi = merge->coi; coj = merge->coj;
1649: PetscCalloc1(coi[pon]+1,&coa);
1650: bi = merge->bi; bj = merge->bj;
1651: owners = merge->rowmap->range;
1652: PetscCalloc1(bi[cm]+1,&ba);
1654: /* get A_loc by taking all local rows of A */
1655: A_loc = ptap->A_loc;
1656: MatMPIAIJGetLocalMat(A,MAT_REUSE_MATRIX,&A_loc);
1657: a_loc = (Mat_SeqAIJ*)(A_loc)->data;
1658: ai = a_loc->i;
1659: aj = a_loc->j;
1661: /* trigger copy to CPU */
1662: MatSeqAIJGetArrayRead(p->A,&dummy);
1663: MatSeqAIJRestoreArrayRead(p->A,&dummy);
1664: MatSeqAIJGetArrayRead(p->B,&dummy);
1665: MatSeqAIJRestoreArrayRead(p->B,&dummy);
1666: for (i=0; i<am; i++) {
1667: anz = ai[i+1] - ai[i];
1668: adj = aj + ai[i];
1669: ada = a_loc->a + ai[i];
1671: /* 2-b) Compute Cseq = P_loc[i,:]^T*A[i,:] using outer product */
1672: /*-------------------------------------------------------------*/
1673: /* put the value into Co=(p->B)^T*A (off-diagonal part, send to others) */
1674: pnz = po->i[i+1] - po->i[i];
1675: poJ = po->j + po->i[i];
1676: pA = po->a + po->i[i];
1677: for (j=0; j<pnz; j++) {
1678: row = poJ[j];
1679: cj = coj + coi[row];
1680: ca = coa + coi[row];
1681: /* perform sparse axpy */
1682: nexta = 0;
1683: valtmp = pA[j];
1684: for (k=0; nexta<anz; k++) {
1685: if (cj[k] == adj[nexta]) {
1686: ca[k] += valtmp*ada[nexta];
1687: nexta++;
1688: }
1689: }
1690: PetscLogFlops(2.0*anz);
1691: }
1693: /* put the value into Cd (diagonal part) */
1694: pnz = pd->i[i+1] - pd->i[i];
1695: pdJ = pd->j + pd->i[i];
1696: pA = pd->a + pd->i[i];
1697: for (j=0; j<pnz; j++) {
1698: row = pdJ[j];
1699: cj = bj + bi[row];
1700: ca = ba + bi[row];
1701: /* perform sparse axpy */
1702: nexta = 0;
1703: valtmp = pA[j];
1704: for (k=0; nexta<anz; k++) {
1705: if (cj[k] == adj[nexta]) {
1706: ca[k] += valtmp*ada[nexta];
1707: nexta++;
1708: }
1709: }
1710: PetscLogFlops(2.0*anz);
1711: }
1712: }
1714: /* 3) send and recv matrix values coa */
1715: /*------------------------------------*/
1716: buf_ri = merge->buf_ri;
1717: buf_rj = merge->buf_rj;
1718: len_s = merge->len_s;
1719: PetscCommGetNewTag(comm,&taga);
1720: PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);
1722: PetscMalloc2(merge->nsend+1,&s_waits,size,&status);
1723: for (proc=0,k=0; proc<size; proc++) {
1724: if (!len_s[proc]) continue;
1725: i = merge->owners_co[proc];
1726: MPI_Isend(coa+coi[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);
1727: k++;
1728: }
1729: if (merge->nrecv) {MPI_Waitall(merge->nrecv,r_waits,status);}
1730: if (merge->nsend) {MPI_Waitall(merge->nsend,s_waits,status);}
1732: PetscFree2(s_waits,status);
1733: PetscFree(r_waits);
1734: PetscFree(coa);
1736: /* 4) insert local Cseq and received values into Cmpi */
1737: /*----------------------------------------------------*/
1738: PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextci);
1739: for (k=0; k<merge->nrecv; k++) {
1740: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
1741: nrows = *(buf_ri_k[k]);
1742: nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */
1743: nextci[k] = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure */
1744: }
1746: for (i=0; i<cm; i++) {
1747: row = owners[rank] + i; /* global row index of C_seq */
1748: bj_i = bj + bi[i]; /* col indices of the i-th row of C */
1749: ba_i = ba + bi[i];
1750: bnz = bi[i+1] - bi[i];
1751: /* add received vals into ba */
1752: for (k=0; k<merge->nrecv; k++) { /* k-th received message */
1753: /* i-th row */
1754: if (i == *nextrow[k]) {
1755: cnz = *(nextci[k]+1) - *nextci[k];
1756: cj = buf_rj[k] + *(nextci[k]);
1757: ca = abuf_r[k] + *(nextci[k]);
1758: nextcj = 0;
1759: for (j=0; nextcj<cnz; j++) {
1760: if (bj_i[j] == cj[nextcj]) { /* bcol == ccol */
1761: ba_i[j] += ca[nextcj++];
1762: }
1763: }
1764: nextrow[k]++; nextci[k]++;
1765: PetscLogFlops(2.0*cnz);
1766: }
1767: }
1768: MatSetValues(C,1,&row,bnz,bj_i,ba_i,INSERT_VALUES);
1769: }
1770: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
1771: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
1773: PetscFree(ba);
1774: PetscFree(abuf_r[0]);
1775: PetscFree(abuf_r);
1776: PetscFree3(buf_ri_k,nextrow,nextci);
1777: return(0);
1778: }
1780: PetscErrorCode MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ(Mat P,Mat A,PetscReal fill,Mat C)
1781: {
1782: PetscErrorCode ierr;
1783: Mat A_loc;
1784: Mat_APMPI *ptap;
1785: PetscFreeSpaceList free_space=NULL,current_space=NULL;
1786: Mat_MPIAIJ *p=(Mat_MPIAIJ*)P->data,*a=(Mat_MPIAIJ*)A->data;
1787: PetscInt *pdti,*pdtj,*poti,*potj,*ptJ;
1788: PetscInt nnz;
1789: PetscInt *lnk,*owners_co,*coi,*coj,i,k,pnz,row;
1790: PetscInt am =A->rmap->n,pn=P->cmap->n;
1791: MPI_Comm comm;
1792: PetscMPIInt size,rank,tagi,tagj,*len_si,*len_s,*len_ri;
1793: PetscInt **buf_rj,**buf_ri,**buf_ri_k;
1794: PetscInt len,proc,*dnz,*onz,*owners;
1795: PetscInt nzi,*bi,*bj;
1796: PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextci;
1797: MPI_Request *swaits,*rwaits;
1798: MPI_Status *sstatus,rstatus;
1799: Mat_Merge_SeqsToMPI *merge;
1800: PetscInt *ai,*aj,*Jptr,anz,*prmap=p->garray,pon,nspacedouble=0,j;
1801: PetscReal afill =1.0,afill_tmp;
1802: PetscInt rstart = P->cmap->rstart,rmax,aN=A->cmap->N,Armax;
1803: Mat_SeqAIJ *a_loc;
1804: PetscTable ta;
1805: MatType mtype;
1808: PetscObjectGetComm((PetscObject)A,&comm);
1809: /* check if matrix local sizes are compatible */
1810: if (A->rmap->rstart != P->rmap->rstart || A->rmap->rend != P->rmap->rend) SETERRQ4(comm,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, A (%D, %D) != P (%D,%D)",A->rmap->rstart,A->rmap->rend,P->rmap->rstart,P->rmap->rend);
1812: MPI_Comm_size(comm,&size);
1813: MPI_Comm_rank(comm,&rank);
1815: /* create struct Mat_APMPI and attached it to C later */
1816: PetscNew(&ptap);
1818: /* get A_loc by taking all local rows of A */
1819: MatMPIAIJGetLocalMat(A,MAT_INITIAL_MATRIX,&A_loc);
1821: ptap->A_loc = A_loc;
1822: a_loc = (Mat_SeqAIJ*)(A_loc)->data;
1823: ai = a_loc->i;
1824: aj = a_loc->j;
1826: /* determine symbolic Co=(p->B)^T*A - send to others */
1827: /*----------------------------------------------------*/
1828: MatGetSymbolicTranspose_SeqAIJ(p->A,&pdti,&pdtj);
1829: MatGetSymbolicTranspose_SeqAIJ(p->B,&poti,&potj);
1830: pon = (p->B)->cmap->n; /* total num of rows to be sent to other processors
1831: >= (num of nonzero rows of C_seq) - pn */
1832: PetscMalloc1(pon+1,&coi);
1833: coi[0] = 0;
1835: /* set initial free space to be fill*(nnz(p->B) + nnz(A)) */
1836: nnz = PetscRealIntMultTruncate(fill,PetscIntSumTruncate(poti[pon],ai[am]));
1837: PetscFreeSpaceGet(nnz,&free_space);
1838: current_space = free_space;
1840: /* create and initialize a linked list */
1841: PetscTableCreate(A->cmap->n + a->B->cmap->N,aN,&ta);
1842: MatRowMergeMax_SeqAIJ(a_loc,am,ta);
1843: PetscTableGetCount(ta,&Armax);
1845: PetscLLCondensedCreate_Scalable(Armax,&lnk);
1847: for (i=0; i<pon; i++) {
1848: pnz = poti[i+1] - poti[i];
1849: ptJ = potj + poti[i];
1850: for (j=0; j<pnz; j++) {
1851: row = ptJ[j]; /* row of A_loc == col of Pot */
1852: anz = ai[row+1] - ai[row];
1853: Jptr = aj + ai[row];
1854: /* add non-zero cols of AP into the sorted linked list lnk */
1855: PetscLLCondensedAddSorted_Scalable(anz,Jptr,lnk);
1856: }
1857: nnz = lnk[0];
1859: /* If free space is not available, double the total space in the list */
1860: if (current_space->local_remaining<nnz) {
1861: PetscFreeSpaceGet(PetscIntSumTruncate(nnz,current_space->total_array_size),¤t_space);
1862: nspacedouble++;
1863: }
1865: /* Copy data into free space, and zero out denserows */
1866: PetscLLCondensedClean_Scalable(nnz,current_space->array,lnk);
1868: current_space->array += nnz;
1869: current_space->local_used += nnz;
1870: current_space->local_remaining -= nnz;
1872: coi[i+1] = coi[i] + nnz;
1873: }
1875: PetscMalloc1(coi[pon]+1,&coj);
1876: PetscFreeSpaceContiguous(&free_space,coj);
1877: PetscLLCondensedDestroy_Scalable(lnk); /* must destroy to get a new one for C */
1879: afill_tmp = (PetscReal)coi[pon]/(poti[pon] + ai[am]+1);
1880: if (afill_tmp > afill) afill = afill_tmp;
1882: /* send j-array (coj) of Co to other processors */
1883: /*----------------------------------------------*/
1884: /* determine row ownership */
1885: PetscNew(&merge);
1886: PetscLayoutCreate(comm,&merge->rowmap);
1888: merge->rowmap->n = pn;
1889: merge->rowmap->bs = 1;
1891: PetscLayoutSetUp(merge->rowmap);
1892: owners = merge->rowmap->range;
1894: /* determine the number of messages to send, their lengths */
1895: PetscCalloc1(size,&len_si);
1896: PetscCalloc1(size,&merge->len_s);
1898: len_s = merge->len_s;
1899: merge->nsend = 0;
1901: PetscMalloc1(size+2,&owners_co);
1903: proc = 0;
1904: for (i=0; i<pon; i++) {
1905: while (prmap[i] >= owners[proc+1]) proc++;
1906: len_si[proc]++; /* num of rows in Co to be sent to [proc] */
1907: len_s[proc] += coi[i+1] - coi[i];
1908: }
1910: len = 0; /* max length of buf_si[] */
1911: owners_co[0] = 0;
1912: for (proc=0; proc<size; proc++) {
1913: owners_co[proc+1] = owners_co[proc] + len_si[proc];
1914: if (len_si[proc]) {
1915: merge->nsend++;
1916: len_si[proc] = 2*(len_si[proc] + 1);
1917: len += len_si[proc];
1918: }
1919: }
1921: /* determine the number and length of messages to receive for coi and coj */
1922: PetscGatherNumberOfMessages(comm,NULL,len_s,&merge->nrecv);
1923: PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);
1925: /* post the Irecv and Isend of coj */
1926: PetscCommGetNewTag(comm,&tagj);
1927: PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rwaits);
1928: PetscMalloc1(merge->nsend+1,&swaits);
1929: for (proc=0, k=0; proc<size; proc++) {
1930: if (!len_s[proc]) continue;
1931: i = owners_co[proc];
1932: MPI_Isend(coj+coi[i],len_s[proc],MPIU_INT,proc,tagj,comm,swaits+k);
1933: k++;
1934: }
1936: /* receives and sends of coj are complete */
1937: PetscMalloc1(size,&sstatus);
1938: for (i=0; i<merge->nrecv; i++) {
1939: PETSC_UNUSED PetscMPIInt icompleted;
1940: MPI_Waitany(merge->nrecv,rwaits,&icompleted,&rstatus);
1941: }
1942: PetscFree(rwaits);
1943: if (merge->nsend) {MPI_Waitall(merge->nsend,swaits,sstatus);}
1945: /* add received column indices into table to update Armax */
1946: /* Armax can be as large as aN if a P[row,:] is dense, see src/ksp/ksp/tutorials/ex56.c! */
1947: for (k=0; k<merge->nrecv; k++) {/* k-th received message */
1948: Jptr = buf_rj[k];
1949: for (j=0; j<merge->len_r[k]; j++) {
1950: PetscTableAdd(ta,*(Jptr+j)+1,1,INSERT_VALUES);
1951: }
1952: }
1953: PetscTableGetCount(ta,&Armax);
1954: /* printf("Armax %d, an %d + Bn %d = %d, aN %d\n",Armax,A->cmap->n,a->B->cmap->N,A->cmap->n+a->B->cmap->N,aN); */
1956: /* send and recv coi */
1957: /*-------------------*/
1958: PetscCommGetNewTag(comm,&tagi);
1959: PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&rwaits);
1960: PetscMalloc1(len+1,&buf_s);
1961: buf_si = buf_s; /* points to the beginning of k-th msg to be sent */
1962: for (proc=0,k=0; proc<size; proc++) {
1963: if (!len_s[proc]) continue;
1964: /* form outgoing message for i-structure:
1965: buf_si[0]: nrows to be sent
1966: [1:nrows]: row index (global)
1967: [nrows+1:2*nrows+1]: i-structure index
1968: */
1969: /*-------------------------------------------*/
1970: nrows = len_si[proc]/2 - 1;
1971: buf_si_i = buf_si + nrows+1;
1972: buf_si[0] = nrows;
1973: buf_si_i[0] = 0;
1974: nrows = 0;
1975: for (i=owners_co[proc]; i<owners_co[proc+1]; i++) {
1976: nzi = coi[i+1] - coi[i];
1977: buf_si_i[nrows+1] = buf_si_i[nrows] + nzi; /* i-structure */
1978: buf_si[nrows+1] = prmap[i] -owners[proc]; /* local row index */
1979: nrows++;
1980: }
1981: MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,swaits+k);
1982: k++;
1983: buf_si += len_si[proc];
1984: }
1985: i = merge->nrecv;
1986: while (i--) {
1987: PETSC_UNUSED PetscMPIInt icompleted;
1988: MPI_Waitany(merge->nrecv,rwaits,&icompleted,&rstatus);
1989: }
1990: PetscFree(rwaits);
1991: if (merge->nsend) {MPI_Waitall(merge->nsend,swaits,sstatus);}
1992: PetscFree(len_si);
1993: PetscFree(len_ri);
1994: PetscFree(swaits);
1995: PetscFree(sstatus);
1996: PetscFree(buf_s);
1998: /* compute the local portion of C (mpi mat) */
1999: /*------------------------------------------*/
2000: /* allocate bi array and free space for accumulating nonzero column info */
2001: PetscMalloc1(pn+1,&bi);
2002: bi[0] = 0;
2004: /* set initial free space to be fill*(nnz(P) + nnz(AP)) */
2005: nnz = PetscRealIntMultTruncate(fill,PetscIntSumTruncate(pdti[pn],PetscIntSumTruncate(poti[pon],ai[am])));
2006: PetscFreeSpaceGet(nnz,&free_space);
2007: current_space = free_space;
2009: PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextci);
2010: for (k=0; k<merge->nrecv; k++) {
2011: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
2012: nrows = *buf_ri_k[k];
2013: nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */
2014: nextci[k] = buf_ri_k[k] + (nrows + 1); /* points to the next i-structure of k-th received i-structure */
2015: }
2017: PetscLLCondensedCreate_Scalable(Armax,&lnk);
2018: MatPreallocateInitialize(comm,pn,A->cmap->n,dnz,onz);
2019: rmax = 0;
2020: for (i=0; i<pn; i++) {
2021: /* add pdt[i,:]*AP into lnk */
2022: pnz = pdti[i+1] - pdti[i];
2023: ptJ = pdtj + pdti[i];
2024: for (j=0; j<pnz; j++) {
2025: row = ptJ[j]; /* row of AP == col of Pt */
2026: anz = ai[row+1] - ai[row];
2027: Jptr = aj + ai[row];
2028: /* add non-zero cols of AP into the sorted linked list lnk */
2029: PetscLLCondensedAddSorted_Scalable(anz,Jptr,lnk);
2030: }
2032: /* add received col data into lnk */
2033: for (k=0; k<merge->nrecv; k++) { /* k-th received message */
2034: if (i == *nextrow[k]) { /* i-th row */
2035: nzi = *(nextci[k]+1) - *nextci[k];
2036: Jptr = buf_rj[k] + *nextci[k];
2037: PetscLLCondensedAddSorted_Scalable(nzi,Jptr,lnk);
2038: nextrow[k]++; nextci[k]++;
2039: }
2040: }
2042: /* add missing diagonal entry */
2043: if (C->force_diagonals) {
2044: k = i + owners[rank]; /* column index */
2045: PetscLLCondensedAddSorted_Scalable(1,&k,lnk);
2046: }
2048: nnz = lnk[0];
2050: /* if free space is not available, make more free space */
2051: if (current_space->local_remaining<nnz) {
2052: PetscFreeSpaceGet(PetscIntSumTruncate(nnz,current_space->total_array_size),¤t_space);
2053: nspacedouble++;
2054: }
2055: /* copy data into free space, then initialize lnk */
2056: PetscLLCondensedClean_Scalable(nnz,current_space->array,lnk);
2057: MatPreallocateSet(i+owners[rank],nnz,current_space->array,dnz,onz);
2059: current_space->array += nnz;
2060: current_space->local_used += nnz;
2061: current_space->local_remaining -= nnz;
2063: bi[i+1] = bi[i] + nnz;
2064: if (nnz > rmax) rmax = nnz;
2065: }
2066: PetscFree3(buf_ri_k,nextrow,nextci);
2068: PetscMalloc1(bi[pn]+1,&bj);
2069: PetscFreeSpaceContiguous(&free_space,bj);
2070: afill_tmp = (PetscReal)bi[pn]/(pdti[pn] + poti[pon] + ai[am]+1);
2071: if (afill_tmp > afill) afill = afill_tmp;
2072: PetscLLCondensedDestroy_Scalable(lnk);
2073: PetscTableDestroy(&ta);
2074: MatRestoreSymbolicTranspose_SeqAIJ(p->A,&pdti,&pdtj);
2075: MatRestoreSymbolicTranspose_SeqAIJ(p->B,&poti,&potj);
2077: /* create symbolic parallel matrix C - why cannot be assembled in Numeric part */
2078: /*-------------------------------------------------------------------------------*/
2079: MatSetSizes(C,pn,A->cmap->n,PETSC_DETERMINE,PETSC_DETERMINE);
2080: MatSetBlockSizes(C,PetscAbs(P->cmap->bs),PetscAbs(A->cmap->bs));
2081: MatGetType(A,&mtype);
2082: MatSetType(C,mtype);
2083: MatMPIAIJSetPreallocation(C,0,dnz,0,onz);
2084: MatPreallocateFinalize(dnz,onz);
2085: MatSetBlockSize(C,1);
2086: MatSetOption(C,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);
2087: for (i=0; i<pn; i++) {
2088: row = i + rstart;
2089: nnz = bi[i+1] - bi[i];
2090: Jptr = bj + bi[i];
2091: MatSetValues(C,1,&row,nnz,Jptr,NULL,INSERT_VALUES);
2092: }
2093: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2094: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
2095: MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
2096: merge->bi = bi;
2097: merge->bj = bj;
2098: merge->coi = coi;
2099: merge->coj = coj;
2100: merge->buf_ri = buf_ri;
2101: merge->buf_rj = buf_rj;
2102: merge->owners_co = owners_co;
2104: /* attach the supporting struct to C for reuse */
2105: C->product->data = ptap;
2106: C->product->destroy = MatDestroy_MPIAIJ_PtAP;
2107: ptap->merge = merge;
2109: C->ops->mattransposemultnumeric = MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ;
2111: #if defined(PETSC_USE_INFO)
2112: if (bi[pn] != 0) {
2113: PetscInfo3(C,"Reallocs %D; Fill ratio: given %g needed %g.\n",nspacedouble,(double)fill,(double)afill);
2114: PetscInfo1(C,"Use MatTransposeMatMult(A,B,MatReuse,%g,&C) for best performance.\n",(double)afill);
2115: } else {
2116: PetscInfo(C,"Empty matrix product\n");
2117: }
2118: #endif
2119: return(0);
2120: }
2122: /* ---------------------------------------------------------------- */
2123: static PetscErrorCode MatProductSymbolic_AtB_MPIAIJ_MPIAIJ(Mat C)
2124: {
2126: Mat_Product *product = C->product;
2127: Mat A=product->A,B=product->B;
2128: PetscReal fill=product->fill;
2129: PetscBool flg;
2132: /* scalable */
2133: PetscStrcmp(product->alg,"scalable",&flg);
2134: if (flg) {
2135: MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ(A,B,fill,C);
2136: goto next;
2137: }
2139: /* nonscalable */
2140: PetscStrcmp(product->alg,"nonscalable",&flg);
2141: if (flg) {
2142: MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(A,B,fill,C);
2143: goto next;
2144: }
2146: /* matmatmult */
2147: PetscStrcmp(product->alg,"at*b",&flg);
2148: if (flg) {
2149: Mat At;
2150: Mat_APMPI *ptap;
2152: MatTranspose(A,MAT_INITIAL_MATRIX,&At);
2153: MatMatMultSymbolic_MPIAIJ_MPIAIJ(At,B,fill,C);
2154: ptap = (Mat_APMPI*)C->product->data;
2155: if (ptap) {
2156: ptap->Pt = At;
2157: C->product->destroy = MatDestroy_MPIAIJ_PtAP;
2158: }
2159: C->ops->transposematmultnumeric = MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_matmatmult;
2160: goto next;
2161: }
2163: /* backend general code */
2164: PetscStrcmp(product->alg,"backend",&flg);
2165: if (flg) {
2166: MatProductSymbolic_MPIAIJBACKEND(C);
2167: return(0);
2168: }
2170: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatProduct type is not supported");
2172: next:
2173: C->ops->productnumeric = MatProductNumeric_AtB;
2174: return(0);
2175: }
2177: /* ---------------------------------------------------------------- */
2178: /* Set options for MatMatMultxxx_MPIAIJ_MPIAIJ */
2179: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_AB(Mat C)
2180: {
2182: Mat_Product *product = C->product;
2183: Mat A=product->A,B=product->B;
2184: #if defined(PETSC_HAVE_HYPRE)
2185: const char *algTypes[5] = {"scalable","nonscalable","seqmpi","backend","hypre"};
2186: PetscInt nalg = 5;
2187: #else
2188: const char *algTypes[4] = {"scalable","nonscalable","seqmpi","backend",};
2189: PetscInt nalg = 4;
2190: #endif
2191: PetscInt alg = 1; /* set nonscalable algorithm as default */
2192: PetscBool flg;
2193: MPI_Comm comm;
2196: /* Check matrix local sizes */
2197: PetscObjectGetComm((PetscObject)C,&comm);
2198: if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%D, %D) != (%D,%D)",A->cmap->rstart,A->cmap->rend,B->rmap->rstart,B->rmap->rend);
2200: /* Set "nonscalable" as default algorithm */
2201: PetscStrcmp(C->product->alg,"default",&flg);
2202: if (flg) {
2203: MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
2205: /* Set "scalable" as default if BN and local nonzeros of A and B are large */
2206: if (B->cmap->N > 100000) { /* may switch to scalable algorithm as default */
2207: MatInfo Ainfo,Binfo;
2208: PetscInt nz_local;
2209: PetscBool alg_scalable_loc=PETSC_FALSE,alg_scalable;
2211: MatGetInfo(A,MAT_LOCAL,&Ainfo);
2212: MatGetInfo(B,MAT_LOCAL,&Binfo);
2213: nz_local = (PetscInt)(Ainfo.nz_allocated + Binfo.nz_allocated);
2215: if (B->cmap->N > product->fill*nz_local) alg_scalable_loc = PETSC_TRUE;
2216: MPIU_Allreduce(&alg_scalable_loc,&alg_scalable,1,MPIU_BOOL,MPI_LOR,comm);
2218: if (alg_scalable) {
2219: alg = 0; /* scalable algorithm would 50% slower than nonscalable algorithm */
2220: MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
2221: PetscInfo2(B,"Use scalable algorithm, BN %D, fill*nz_allocated %g\n",B->cmap->N,product->fill*nz_local);
2222: }
2223: }
2224: }
2226: /* Get runtime option */
2227: if (product->api_user) {
2228: PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatMatMult","Mat");
2229: PetscOptionsEList("-matmatmult_via","Algorithmic approach","MatMatMult",algTypes,nalg,algTypes[alg],&alg,&flg);
2230: PetscOptionsEnd();
2231: } else {
2232: PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatProduct_AB","Mat");
2233: PetscOptionsEList("-matproduct_ab_via","Algorithmic approach","MatMatMult",algTypes,nalg,algTypes[alg],&alg,&flg);
2234: PetscOptionsEnd();
2235: }
2236: if (flg) {
2237: MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
2238: }
2240: C->ops->productsymbolic = MatProductSymbolic_AB_MPIAIJ_MPIAIJ;
2241: return(0);
2242: }
2244: /* Set options for MatTransposeMatMultXXX_MPIAIJ_MPIAIJ */
2245: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_AtB(Mat C)
2246: {
2248: Mat_Product *product = C->product;
2249: Mat A=product->A,B=product->B;
2250: const char *algTypes[4] = {"scalable","nonscalable","at*b","backend"};
2251: PetscInt nalg = 4;
2252: PetscInt alg = 1; /* set default algorithm */
2253: PetscBool flg;
2254: MPI_Comm comm;
2257: /* Check matrix local sizes */
2258: PetscObjectGetComm((PetscObject)C,&comm);
2259: if (A->rmap->rstart != B->rmap->rstart || A->rmap->rend != B->rmap->rend) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, A (%D, %D) != B (%D,%D)",A->rmap->rstart,A->rmap->rend,B->rmap->rstart,B->rmap->rend);
2261: /* Set default algorithm */
2262: PetscStrcmp(C->product->alg,"default",&flg);
2263: if (flg) {
2264: MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
2265: }
2267: /* Set "scalable" as default if BN and local nonzeros of A and B are large */
2268: if (alg && B->cmap->N > 100000) { /* may switch to scalable algorithm as default */
2269: MatInfo Ainfo,Binfo;
2270: PetscInt nz_local;
2271: PetscBool alg_scalable_loc=PETSC_FALSE,alg_scalable;
2273: MatGetInfo(A,MAT_LOCAL,&Ainfo);
2274: MatGetInfo(B,MAT_LOCAL,&Binfo);
2275: nz_local = (PetscInt)(Ainfo.nz_allocated + Binfo.nz_allocated);
2277: if (B->cmap->N > product->fill*nz_local) alg_scalable_loc = PETSC_TRUE;
2278: MPIU_Allreduce(&alg_scalable_loc,&alg_scalable,1,MPIU_BOOL,MPI_LOR,comm);
2280: if (alg_scalable) {
2281: alg = 0; /* scalable algorithm would 50% slower than nonscalable algorithm */
2282: MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
2283: PetscInfo2(B,"Use scalable algorithm, BN %D, fill*nz_allocated %g\n",B->cmap->N,product->fill*nz_local);
2284: }
2285: }
2287: /* Get runtime option */
2288: if (product->api_user) {
2289: PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatTransposeMatMult","Mat");
2290: PetscOptionsEList("-mattransposematmult_via","Algorithmic approach","MatTransposeMatMult",algTypes,nalg,algTypes[alg],&alg,&flg);
2291: PetscOptionsEnd();
2292: } else {
2293: PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatProduct_AtB","Mat");
2294: PetscOptionsEList("-matproduct_atb_via","Algorithmic approach","MatTransposeMatMult",algTypes,nalg,algTypes[alg],&alg,&flg);
2295: PetscOptionsEnd();
2296: }
2297: if (flg) {
2298: MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
2299: }
2301: C->ops->productsymbolic = MatProductSymbolic_AtB_MPIAIJ_MPIAIJ;
2302: return(0);
2303: }
2305: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_PtAP(Mat C)
2306: {
2308: Mat_Product *product = C->product;
2309: Mat A=product->A,P=product->B;
2310: MPI_Comm comm;
2311: PetscBool flg;
2312: PetscInt alg=1; /* set default algorithm */
2313: #if !defined(PETSC_HAVE_HYPRE)
2314: const char *algTypes[5] = {"scalable","nonscalable","allatonce","allatonce_merged","backend"};
2315: PetscInt nalg=5;
2316: #else
2317: const char *algTypes[6] = {"scalable","nonscalable","allatonce","allatonce_merged","backend","hypre"};
2318: PetscInt nalg=6;
2319: #endif
2320: PetscInt pN=P->cmap->N;
2323: /* Check matrix local sizes */
2324: PetscObjectGetComm((PetscObject)C,&comm);
2325: if (A->rmap->rstart != P->rmap->rstart || A->rmap->rend != P->rmap->rend) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, Arow (%D, %D) != Prow (%D,%D)",A->rmap->rstart,A->rmap->rend,P->rmap->rstart,P->rmap->rend);
2326: if (A->cmap->rstart != P->rmap->rstart || A->cmap->rend != P->rmap->rend) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, Acol (%D, %D) != Prow (%D,%D)",A->cmap->rstart,A->cmap->rend,P->rmap->rstart,P->rmap->rend);
2328: /* Set "nonscalable" as default algorithm */
2329: PetscStrcmp(C->product->alg,"default",&flg);
2330: if (flg) {
2331: MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
2333: /* Set "scalable" as default if BN and local nonzeros of A and B are large */
2334: if (pN > 100000) {
2335: MatInfo Ainfo,Pinfo;
2336: PetscInt nz_local;
2337: PetscBool alg_scalable_loc=PETSC_FALSE,alg_scalable;
2339: MatGetInfo(A,MAT_LOCAL,&Ainfo);
2340: MatGetInfo(P,MAT_LOCAL,&Pinfo);
2341: nz_local = (PetscInt)(Ainfo.nz_allocated + Pinfo.nz_allocated);
2343: if (pN > product->fill*nz_local) alg_scalable_loc = PETSC_TRUE;
2344: MPIU_Allreduce(&alg_scalable_loc,&alg_scalable,1,MPIU_BOOL,MPI_LOR,comm);
2346: if (alg_scalable) {
2347: alg = 0; /* scalable algorithm would 50% slower than nonscalable algorithm */
2348: MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
2349: }
2350: }
2351: }
2353: /* Get runtime option */
2354: if (product->api_user) {
2355: PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatPtAP","Mat");
2356: PetscOptionsEList("-matptap_via","Algorithmic approach","MatPtAP",algTypes,nalg,algTypes[alg],&alg,&flg);
2357: PetscOptionsEnd();
2358: } else {
2359: PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatProduct_PtAP","Mat");
2360: PetscOptionsEList("-matproduct_ptap_via","Algorithmic approach","MatPtAP",algTypes,nalg,algTypes[alg],&alg,&flg);
2361: PetscOptionsEnd();
2362: }
2363: if (flg) {
2364: MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
2365: }
2367: C->ops->productsymbolic = MatProductSymbolic_PtAP_MPIAIJ_MPIAIJ;
2368: return(0);
2369: }
2371: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_RARt(Mat C)
2372: {
2373: Mat_Product *product = C->product;
2374: Mat A = product->A,R=product->B;
2377: /* Check matrix local sizes */
2378: if (A->cmap->n != R->cmap->n || A->rmap->n != R->cmap->n) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, A local (%D, %D), R local (%D,%D)",A->rmap->n,A->rmap->n,R->rmap->n,R->cmap->n);
2380: C->ops->productsymbolic = MatProductSymbolic_RARt_MPIAIJ_MPIAIJ;
2381: return(0);
2382: }
2384: /*
2385: Set options for ABC = A*B*C = A*(B*C); ABC's algorithm must be chosen from AB's algorithm
2386: */
2387: static PetscErrorCode MatProductSetFromOptions_MPIAIJ_ABC(Mat C)
2388: {
2390: Mat_Product *product = C->product;
2391: PetscBool flg = PETSC_FALSE;
2392: PetscInt alg = 1; /* default algorithm */
2393: const char *algTypes[3] = {"scalable","nonscalable","seqmpi"};
2394: PetscInt nalg = 3;
2397: /* Set default algorithm */
2398: PetscStrcmp(C->product->alg,"default",&flg);
2399: if (flg) {
2400: MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
2401: }
2403: /* Get runtime option */
2404: if (product->api_user) {
2405: PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatMatMatMult","Mat");
2406: PetscOptionsEList("-matmatmatmult_via","Algorithmic approach","MatMatMatMult",algTypes,nalg,algTypes[alg],&alg,&flg);
2407: PetscOptionsEnd();
2408: } else {
2409: PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatProduct_ABC","Mat");
2410: PetscOptionsEList("-matproduct_abc_via","Algorithmic approach","MatProduct_ABC",algTypes,nalg,algTypes[alg],&alg,&flg);
2411: PetscOptionsEnd();
2412: }
2413: if (flg) {
2414: MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
2415: }
2417: C->ops->matmatmultsymbolic = MatMatMatMultSymbolic_MPIAIJ_MPIAIJ_MPIAIJ;
2418: C->ops->productsymbolic = MatProductSymbolic_ABC;
2419: return(0);
2420: }
2422: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_MPIAIJ(Mat C)
2423: {
2425: Mat_Product *product = C->product;
2428: switch (product->type) {
2429: case MATPRODUCT_AB:
2430: MatProductSetFromOptions_MPIAIJ_AB(C);
2431: break;
2432: case MATPRODUCT_AtB:
2433: MatProductSetFromOptions_MPIAIJ_AtB(C);
2434: break;
2435: case MATPRODUCT_PtAP:
2436: MatProductSetFromOptions_MPIAIJ_PtAP(C);
2437: break;
2438: case MATPRODUCT_RARt:
2439: MatProductSetFromOptions_MPIAIJ_RARt(C);
2440: break;
2441: case MATPRODUCT_ABC:
2442: MatProductSetFromOptions_MPIAIJ_ABC(C);
2443: break;
2444: default:
2445: break;
2446: }
2447: return(0);
2448: }