Actual source code: mpimatmatmult.c
petsc-3.11.0 2019-03-29
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/vecscatterimpl.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 MatMatMult_MPIAIJ_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill, Mat *C)
19: {
21: #if defined(PETSC_HAVE_HYPRE)
22: const char *algTypes[4] = {"scalable","nonscalable","seqmpi","hypre"};
23: PetscInt nalg = 4;
24: #else
25: const char *algTypes[3] = {"scalable","nonscalable","seqmpi"};
26: PetscInt nalg = 3;
27: #endif
28: PetscInt alg = 1; /* set nonscalable algorithm as default */
29: MPI_Comm comm;
30: PetscBool flg;
33: if (scall == MAT_INITIAL_MATRIX) {
34: PetscObjectGetComm((PetscObject)A,&comm);
35: if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) SETERRQ4(comm,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);
37: PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"MatMatMult","Mat");
38: PetscOptionsEList("-matmatmult_via","Algorithmic approach","MatMatMult",algTypes,nalg,algTypes[1],&alg,&flg);
39: PetscOptionsEnd();
41: if (!flg && B->cmap->N > 100000) { /* may switch to scalable algorithm as default */
42: MatInfo Ainfo,Binfo;
43: PetscInt nz_local;
44: PetscBool alg_scalable_loc=PETSC_FALSE,alg_scalable;
46: MatGetInfo(A,MAT_LOCAL,&Ainfo);
47: MatGetInfo(B,MAT_LOCAL,&Binfo);
48: nz_local = (PetscInt)(Ainfo.nz_allocated + Binfo.nz_allocated);
50: if (B->cmap->N > fill*nz_local) alg_scalable_loc = PETSC_TRUE;
51: MPIU_Allreduce(&alg_scalable_loc,&alg_scalable,1,MPIU_BOOL,MPI_LOR,comm);
53: if (alg_scalable) {
54: alg = 0; /* scalable algorithm would 50% slower than nonscalable algorithm */
55: PetscInfo2(B,"Use scalable algorithm, BN %D, fill*nz_allocated %g\n",B->cmap->N,fill*nz_local);
56: }
57: }
59: PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
60: switch (alg) {
61: case 1:
62: MatMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(A,B,fill,C);
63: break;
64: case 2:
65: MatMatMultSymbolic_MPIAIJ_MPIAIJ_seqMPI(A,B,fill,C);
66: break;
67: #if defined(PETSC_HAVE_HYPRE)
68: case 3:
69: MatMatMultSymbolic_AIJ_AIJ_wHYPRE(A,B,fill,C);
70: break;
71: #endif
72: default:
73: MatMatMultSymbolic_MPIAIJ_MPIAIJ(A,B,fill,C);
74: break;
75: }
76: PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
78: if (alg == 0 || alg == 1) {
79: Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*C)->data;
80: Mat_APMPI *ap = c->ap;
81: PetscOptionsBegin(PetscObjectComm((PetscObject)(*C)),((PetscObject)(*C))->prefix,"MatFreeIntermediateDataStructures","Mat");
82: ap->freestruct = PETSC_FALSE;
83: PetscOptionsBool("-mat_freeintermediatedatastructures","Free intermediate data structures", "MatFreeIntermediateDataStructures",ap->freestruct,&ap->freestruct, NULL);
84: PetscOptionsEnd();
85: }
86: }
88: PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
89: (*(*C)->ops->matmultnumeric)(A,B,*C);
90: PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
91: return(0);
92: }
94: PetscErrorCode MatDestroy_MPIAIJ_MatMatMult(Mat A)
95: {
97: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
98: Mat_APMPI *ptap = a->ap;
101: PetscFree2(ptap->startsj_s,ptap->startsj_r);
102: PetscFree(ptap->bufa);
103: MatDestroy(&ptap->P_loc);
104: MatDestroy(&ptap->P_oth);
105: MatDestroy(&ptap->Pt);
106: PetscFree(ptap->api);
107: PetscFree(ptap->apj);
108: PetscFree(ptap->apa);
109: ptap->destroy(A);
110: PetscFree(ptap);
111: return(0);
112: }
114: PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable(Mat A,Mat P,Mat C)
115: {
117: Mat_MPIAIJ *a =(Mat_MPIAIJ*)A->data,*c=(Mat_MPIAIJ*)C->data;
118: Mat_SeqAIJ *ad =(Mat_SeqAIJ*)(a->A)->data,*ao=(Mat_SeqAIJ*)(a->B)->data;
119: Mat_SeqAIJ *cd =(Mat_SeqAIJ*)(c->A)->data,*co=(Mat_SeqAIJ*)(c->B)->data;
120: PetscScalar *cda=cd->a,*coa=co->a;
121: Mat_SeqAIJ *p_loc,*p_oth;
122: PetscScalar *apa,*ca;
123: PetscInt cm =C->rmap->n;
124: Mat_APMPI *ptap=c->ap;
125: PetscInt *api,*apj,*apJ,i,k;
126: PetscInt cstart=C->cmap->rstart;
127: PetscInt cdnz,conz,k0,k1;
128: MPI_Comm comm;
129: PetscMPIInt size;
132: PetscObjectGetComm((PetscObject)A,&comm);
133: MPI_Comm_size(comm,&size);
135: if (!ptap) SETERRQ(comm,PETSC_ERR_ARG_WRONGSTATE,"AP cannot be reused. Do not call MatFreeIntermediateDataStructures() or use '-mat_freeintermediatedatastructures'");
137: /* 1) get P_oth = ptap->P_oth and P_loc = ptap->P_loc */
138: /*-----------------------------------------------------*/
139: /* update numerical values of P_oth and P_loc */
140: MatGetBrowsOfAoCols_MPIAIJ(A,P,MAT_REUSE_MATRIX,&ptap->startsj_s,&ptap->startsj_r,&ptap->bufa,&ptap->P_oth);
141: MatMPIAIJGetLocalMat(P,MAT_REUSE_MATRIX,&ptap->P_loc);
143: /* 2) compute numeric C_loc = A_loc*P = Ad*P_loc + Ao*P_oth */
144: /*----------------------------------------------------------*/
145: /* get data from symbolic products */
146: p_loc = (Mat_SeqAIJ*)(ptap->P_loc)->data;
147: p_oth = NULL;
148: if (size >1) {
149: p_oth = (Mat_SeqAIJ*)(ptap->P_oth)->data;
150: }
152: /* get apa for storing dense row A[i,:]*P */
153: apa = ptap->apa;
155: api = ptap->api;
156: apj = ptap->apj;
157: for (i=0; i<cm; i++) {
158: /* compute apa = A[i,:]*P */
159: AProw_nonscalable(i,ad,ao,p_loc,p_oth,apa);
161: /* set values in C */
162: apJ = apj + api[i];
163: cdnz = cd->i[i+1] - cd->i[i];
164: conz = co->i[i+1] - co->i[i];
166: /* 1st off-diagoanl part of C */
167: ca = coa + co->i[i];
168: k = 0;
169: for (k0=0; k0<conz; k0++) {
170: if (apJ[k] >= cstart) break;
171: ca[k0] = apa[apJ[k]];
172: apa[apJ[k++]] = 0.0;
173: }
175: /* diagonal part of C */
176: ca = cda + cd->i[i];
177: for (k1=0; k1<cdnz; k1++) {
178: ca[k1] = apa[apJ[k]];
179: apa[apJ[k++]] = 0.0;
180: }
182: /* 2nd off-diagoanl part of C */
183: ca = coa + co->i[i];
184: for (; k0<conz; k0++) {
185: ca[k0] = apa[apJ[k]];
186: apa[apJ[k++]] = 0.0;
187: }
188: }
189: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
190: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
192: if (ptap->freestruct) {
193: MatFreeIntermediateDataStructures(C);
194: }
195: return(0);
196: }
198: PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(Mat A,Mat P,PetscReal fill,Mat *C)
199: {
200: PetscErrorCode ierr;
201: MPI_Comm comm;
202: PetscMPIInt size;
203: Mat Cmpi;
204: Mat_APMPI *ptap;
205: PetscFreeSpaceList free_space=NULL,current_space=NULL;
206: Mat_MPIAIJ *a =(Mat_MPIAIJ*)A->data,*c;
207: Mat_SeqAIJ *ad =(Mat_SeqAIJ*)(a->A)->data,*ao=(Mat_SeqAIJ*)(a->B)->data,*p_loc,*p_oth;
208: PetscInt *pi_loc,*pj_loc,*pi_oth,*pj_oth,*dnz,*onz;
209: PetscInt *adi=ad->i,*adj=ad->j,*aoi=ao->i,*aoj=ao->j,rstart=A->rmap->rstart;
210: PetscInt *lnk,i,pnz,row,*api,*apj,*Jptr,apnz,nspacedouble=0,j,nzi;
211: PetscInt am=A->rmap->n,pN=P->cmap->N,pn=P->cmap->n,pm=P->rmap->n;
212: PetscBT lnkbt;
213: PetscScalar *apa;
214: PetscReal afill;
215: MatType mtype;
218: PetscObjectGetComm((PetscObject)A,&comm);
219: MPI_Comm_size(comm,&size);
221: /* create struct Mat_APMPI and attached it to C later */
222: PetscNew(&ptap);
224: /* get P_oth by taking rows of P (= non-zero cols of local A) from other processors */
225: MatGetBrowsOfAoCols_MPIAIJ(A,P,MAT_INITIAL_MATRIX,&ptap->startsj_s,&ptap->startsj_r,&ptap->bufa,&ptap->P_oth);
227: /* get P_loc by taking all local rows of P */
228: MatMPIAIJGetLocalMat(P,MAT_INITIAL_MATRIX,&ptap->P_loc);
230: p_loc = (Mat_SeqAIJ*)(ptap->P_loc)->data;
231: pi_loc = p_loc->i; pj_loc = p_loc->j;
232: if (size > 1) {
233: p_oth = (Mat_SeqAIJ*)(ptap->P_oth)->data;
234: pi_oth = p_oth->i; pj_oth = p_oth->j;
235: } else {
236: p_oth = NULL;
237: pi_oth = NULL; pj_oth = NULL;
238: }
240: /* first, compute symbolic AP = A_loc*P = A_diag*P_loc + A_off*P_oth */
241: /*-------------------------------------------------------------------*/
242: PetscMalloc1(am+2,&api);
243: ptap->api = api;
244: api[0] = 0;
246: /* create and initialize a linked list */
247: PetscLLCondensedCreate(pN,pN,&lnk,&lnkbt);
249: /* Initial FreeSpace size is fill*(nnz(A)+nnz(P)) */
250: PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,PetscIntSumTruncate(adi[am],PetscIntSumTruncate(aoi[am],pi_loc[pm]))),&free_space);
251: current_space = free_space;
253: MatPreallocateInitialize(comm,am,pn,dnz,onz);
254: for (i=0; i<am; i++) {
255: /* diagonal portion of A */
256: nzi = adi[i+1] - adi[i];
257: for (j=0; j<nzi; j++) {
258: row = *adj++;
259: pnz = pi_loc[row+1] - pi_loc[row];
260: Jptr = pj_loc + pi_loc[row];
261: /* add non-zero cols of P into the sorted linked list lnk */
262: PetscLLCondensedAddSorted(pnz,Jptr,lnk,lnkbt);
263: }
264: /* off-diagonal portion of A */
265: nzi = aoi[i+1] - aoi[i];
266: for (j=0; j<nzi; j++) {
267: row = *aoj++;
268: pnz = pi_oth[row+1] - pi_oth[row];
269: Jptr = pj_oth + pi_oth[row];
270: PetscLLCondensedAddSorted(pnz,Jptr,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: PetscFreeSpaceGet(PetscIntSumTruncate(apnz,current_space->total_array_size),¤t_space);
279: nspacedouble++;
280: }
282: /* Copy data into free space, then initialize lnk */
283: PetscLLCondensedClean(pN,apnz,current_space->array,lnk,lnkbt);
284: MatPreallocateSet(i+rstart,apnz,current_space->array,dnz,onz);
286: 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: PetscMalloc1(api[am]+1,&ptap->apj);
294: apj = ptap->apj;
295: PetscFreeSpaceContiguous(&free_space,ptap->apj);
296: PetscLLDestroy(lnk,lnkbt);
298: /* malloc apa to store dense row A[i,:]*P */
299: PetscCalloc1(pN,&apa);
301: ptap->apa = apa;
303: /* create and assemble symbolic parallel matrix Cmpi */
304: /*----------------------------------------------------*/
305: MatCreate(comm,&Cmpi);
306: MatSetSizes(Cmpi,am,pn,PETSC_DETERMINE,PETSC_DETERMINE);
307: MatSetBlockSizesFromMats(Cmpi,A,P);
309: MatGetType(A,&mtype);
310: MatSetType(Cmpi,mtype);
311: MatMPIAIJSetPreallocation(Cmpi,0,dnz,0,onz);
313: MatSetValues_MPIAIJ_CopyFromCSRFormat_Symbolic(Cmpi, apj, api);
314: MatAssemblyBegin(Cmpi,MAT_FINAL_ASSEMBLY);
315: MatAssemblyEnd(Cmpi,MAT_FINAL_ASSEMBLY);
316: MatPreallocateFinalize(dnz,onz);
318: ptap->destroy = Cmpi->ops->destroy;
319: ptap->duplicate = Cmpi->ops->duplicate;
320: Cmpi->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable;
321: Cmpi->ops->destroy = MatDestroy_MPIAIJ_MatMatMult;
322: Cmpi->ops->freeintermediatedatastructures = MatFreeIntermediateDataStructures_MPIAIJ_AP;
324: /* attach the supporting struct to Cmpi for reuse */
325: c = (Mat_MPIAIJ*)Cmpi->data;
326: c->ap = ptap;
328: *C = Cmpi;
330: /* set MatInfo */
331: afill = (PetscReal)api[am]/(adi[am]+aoi[am]+pi_loc[pm]+1) + 1.e-5;
332: if (afill < 1.0) afill = 1.0;
333: Cmpi->info.mallocs = nspacedouble;
334: Cmpi->info.fill_ratio_given = fill;
335: Cmpi->info.fill_ratio_needed = afill;
337: #if defined(PETSC_USE_INFO)
338: if (api[am]) {
339: PetscInfo3(Cmpi,"Reallocs %D; Fill ratio: given %g needed %g.\n",nspacedouble,(double)fill,(double)afill);
340: PetscInfo1(Cmpi,"Use MatMatMult(A,B,MatReuse,%g,&C) for best performance.;\n",(double)afill);
341: } else {
342: PetscInfo(Cmpi,"Empty matrix product\n");
343: }
344: #endif
345: return(0);
346: }
348: PETSC_INTERN PetscErrorCode MatMatMult_MPIAIJ_MPIDense(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
349: {
353: if (scall == MAT_INITIAL_MATRIX) {
354: PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
355: MatMatMultSymbolic_MPIAIJ_MPIDense(A,B,fill,C);
356: PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
357: }
358: PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
359: MatMatMultNumeric_MPIAIJ_MPIDense(A,B,*C);
360: PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
361: return(0);
362: }
364: typedef struct {
365: Mat workB;
366: PetscScalar *rvalues,*svalues;
367: MPI_Request *rwaits,*swaits;
368: } MPIAIJ_MPIDense;
370: PetscErrorCode MatMPIAIJ_MPIDenseDestroy(void *ctx)
371: {
372: MPIAIJ_MPIDense *contents = (MPIAIJ_MPIDense*) ctx;
373: PetscErrorCode ierr;
376: MatDestroy(&contents->workB);
377: PetscFree4(contents->rvalues,contents->svalues,contents->rwaits,contents->swaits);
378: PetscFree(contents);
379: return(0);
380: }
382: /*
383: This is a "dummy function" that handles the case where matrix C was created as a dense matrix
384: directly by the user and passed to MatMatMult() with the MAT_REUSE_MATRIX option
386: It is the same as MatMatMultSymbolic_MPIAIJ_MPIDense() except does not create C
387: */
388: PetscErrorCode MatMatMultNumeric_MPIDense(Mat A,Mat B,Mat C)
389: {
390: PetscErrorCode ierr;
391: PetscBool flg;
392: Mat_MPIAIJ *aij = (Mat_MPIAIJ*) A->data;
393: PetscInt nz = aij->B->cmap->n,to_n,to_entries,from_n,from_entries;
394: PetscContainer container;
395: MPIAIJ_MPIDense *contents;
396: VecScatter ctx = aij->Mvctx;
399: PetscObjectTypeCompare((PetscObject)B,MATMPIDENSE,&flg);
400: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Second matrix must be mpidense");
402: /* Handle case where where user provided the final C matrix rather than calling MatMatMult() with MAT_INITIAL_MATRIX*/
403: PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&flg);
404: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"First matrix must be MPIAIJ");
406: C->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIDense;
408: PetscNew(&contents);
409: /* Create work matrix used to store off processor rows of B needed for local product */
410: MatCreateSeqDense(PETSC_COMM_SELF,nz,B->cmap->N,NULL,&contents->workB);
411: /* Create work arrays needed */
412: VecScatterGetRemoteCount_Private(ctx,PETSC_TRUE/*send*/,&to_n,&to_entries);
413: VecScatterGetRemoteCount_Private(ctx,PETSC_FALSE/*recv*/,&from_n,&from_entries);
414: PetscMalloc4(B->cmap->N*from_entries,&contents->rvalues,B->cmap->N*to_entries,&contents->svalues,from_n,&contents->rwaits,to_n,&contents->swaits);
416: PetscContainerCreate(PetscObjectComm((PetscObject)A),&container);
417: PetscContainerSetPointer(container,contents);
418: PetscContainerSetUserDestroy(container,MatMPIAIJ_MPIDenseDestroy);
419: PetscObjectCompose((PetscObject)C,"workB",(PetscObject)container);
420: PetscContainerDestroy(&container);
422: (*C->ops->matmultnumeric)(A,B,C);
423: return(0);
424: }
426: PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIDense(Mat A,Mat B,PetscReal fill,Mat *C)
427: {
428: PetscErrorCode ierr;
429: Mat_MPIAIJ *aij = (Mat_MPIAIJ*) A->data;
430: PetscInt nz = aij->B->cmap->n,to_n,to_entries,from_n,from_entries;
431: PetscContainer container;
432: MPIAIJ_MPIDense *contents;
433: VecScatter ctx = aij->Mvctx;
434: PetscInt m = A->rmap->n,n=B->cmap->n;
437: MatCreate(PetscObjectComm((PetscObject)B),C);
438: MatSetSizes(*C,m,n,A->rmap->N,B->cmap->N);
439: MatSetBlockSizesFromMats(*C,A,B);
440: MatSetType(*C,MATMPIDENSE);
441: MatMPIDenseSetPreallocation(*C,NULL);
442: MatAssemblyBegin(*C,MAT_FINAL_ASSEMBLY);
443: MatAssemblyEnd(*C,MAT_FINAL_ASSEMBLY);
445: (*C)->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIDense;
447: PetscNew(&contents);
448: /* Create work matrix used to store off processor rows of B needed for local product */
449: MatCreateSeqDense(PETSC_COMM_SELF,nz,B->cmap->N,NULL,&contents->workB);
450: /* Create work arrays needed */
451: VecScatterGetRemoteCount_Private(ctx,PETSC_TRUE/*send*/,&to_n,&to_entries);
452: VecScatterGetRemoteCount_Private(ctx,PETSC_FALSE/*recv*/,&from_n,&from_entries);
453: PetscMalloc4(B->cmap->N*from_entries,&contents->rvalues,B->cmap->N*to_entries,&contents->svalues,from_n,&contents->rwaits,to_n,&contents->swaits);
455: PetscContainerCreate(PetscObjectComm((PetscObject)A),&container);
456: PetscContainerSetPointer(container,contents);
457: PetscContainerSetUserDestroy(container,MatMPIAIJ_MPIDenseDestroy);
458: PetscObjectCompose((PetscObject)(*C),"workB",(PetscObject)container);
459: PetscContainerDestroy(&container);
460: return(0);
461: }
463: /*
464: Performs an efficient scatter on the rows of B needed by this process; this is
465: a modification of the VecScatterBegin_() routines.
466: */
467: PetscErrorCode MatMPIDenseScatter(Mat A,Mat B,Mat C,Mat *outworkB)
468: {
469: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)A->data;
470: PetscErrorCode ierr;
471: PetscScalar *b,*w,*svalues,*rvalues;
472: VecScatter ctx = aij->Mvctx;
473: PetscInt i,j,k;
474: const PetscInt *sindices,*sstarts,*rindices,*rstarts;
475: const PetscMPIInt *sprocs,*rprocs;
476: PetscInt nsends,nrecvs,nrecvs2;
477: MPI_Request *swaits,*rwaits;
478: MPI_Comm comm;
479: PetscMPIInt tag = ((PetscObject)ctx)->tag,ncols = B->cmap->N, nrows = aij->B->cmap->n,imdex,nrowsB = B->rmap->n,nsends_mpi,nrecvs_mpi;
480: MPI_Status status;
481: MPIAIJ_MPIDense *contents;
482: PetscContainer container;
483: Mat workB;
486: PetscObjectGetComm((PetscObject)A,&comm);
487: PetscObjectQuery((PetscObject)C,"workB",(PetscObject*)&container);
488: if (!container) SETERRQ(comm,PETSC_ERR_PLIB,"Container does not exist");
489: PetscContainerGetPointer(container,(void**)&contents);
491: workB = *outworkB = contents->workB;
492: if (nrows != workB->rmap->n) SETERRQ2(comm,PETSC_ERR_PLIB,"Number of rows of workB %D not equal to columns of aij->B %D",nrows,workB->cmap->n);
493: VecScatterGetRemote_Private(ctx,PETSC_TRUE/*send*/,&nsends,&sstarts,&sindices,&sprocs,NULL/*bs*/);
494: VecScatterGetRemoteOrdered_Private(ctx,PETSC_FALSE/*recv*/,&nrecvs,&rstarts,&rindices,&rprocs,NULL/*bs*/);
495: PetscMPIIntCast(nsends,&nsends_mpi);
496: PetscMPIIntCast(nrecvs,&nrecvs_mpi);
497: svalues = contents->svalues;
498: rvalues = contents->rvalues;
499: swaits = contents->swaits;
500: rwaits = contents->rwaits;
502: MatDenseGetArray(B,&b);
503: MatDenseGetArray(workB,&w);
505: for (i=0; i<nrecvs; i++) {
506: MPI_Irecv(rvalues+ncols*(rstarts[i]-rstarts[0]),ncols*(rstarts[i+1]-rstarts[i]),MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);
507: }
509: for (i=0; i<nsends; i++) {
510: /* pack a message at a time */
511: for (j=0; j<sstarts[i+1]-sstarts[i]; j++) {
512: for (k=0; k<ncols; k++) {
513: svalues[ncols*(sstarts[i]-sstarts[0]+j) + k] = b[sindices[sstarts[i]+j] + nrowsB*k];
514: }
515: }
516: MPI_Isend(svalues+ncols*(sstarts[i]-sstarts[0]),ncols*(sstarts[i+1]-sstarts[i]),MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);
517: }
519: nrecvs2 = nrecvs;
520: while (nrecvs2) {
521: MPI_Waitany(nrecvs_mpi,rwaits,&imdex,&status);
522: nrecvs2--;
523: /* unpack a message at a time */
524: for (j=0; j<rstarts[imdex+1]-rstarts[imdex]; j++) {
525: for (k=0; k<ncols; k++) {
526: w[rindices[rstarts[imdex]+j] + nrows*k] = rvalues[ncols*(rstarts[imdex]-rstarts[0]+j) + k];
527: }
528: }
529: }
530: if (nsends) {MPI_Waitall(nsends_mpi,swaits,MPI_STATUSES_IGNORE);}
532: VecScatterRestoreRemote_Private(ctx,PETSC_TRUE/*send*/,&nsends,&sstarts,&sindices,&sprocs,NULL/*bs*/);
533: VecScatterRestoreRemoteOrdered_Private(ctx,PETSC_FALSE/*recv*/,&nrecvs,&rstarts,&rindices,&rprocs,NULL/*bs*/);
534: MatDenseRestoreArray(B,&b);
535: MatDenseRestoreArray(workB,&w);
536: MatAssemblyBegin(workB,MAT_FINAL_ASSEMBLY);
537: MatAssemblyEnd(workB,MAT_FINAL_ASSEMBLY);
538: return(0);
540: }
541: extern PetscErrorCode MatMatMultNumericAdd_SeqAIJ_SeqDense(Mat,Mat,Mat);
543: PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIDense(Mat A,Mat B,Mat C)
544: {
546: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)A->data;
547: Mat_MPIDense *bdense = (Mat_MPIDense*)B->data;
548: Mat_MPIDense *cdense = (Mat_MPIDense*)C->data;
549: Mat workB;
552: /* diagonal block of A times all local rows of B*/
553: MatMatMultNumeric_SeqAIJ_SeqDense(aij->A,bdense->A,cdense->A);
555: /* get off processor parts of B needed to complete the product */
556: MatMPIDenseScatter(A,B,C,&workB);
558: /* off-diagonal block of A times nonlocal rows of B */
559: MatMatMultNumericAdd_SeqAIJ_SeqDense(aij->B,workB,cdense->A);
560: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
561: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
562: return(0);
563: }
565: PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIAIJ(Mat A,Mat P,Mat C)
566: {
568: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data,*c=(Mat_MPIAIJ*)C->data;
569: Mat_SeqAIJ *ad = (Mat_SeqAIJ*)(a->A)->data,*ao=(Mat_SeqAIJ*)(a->B)->data;
570: Mat_SeqAIJ *cd = (Mat_SeqAIJ*)(c->A)->data,*co=(Mat_SeqAIJ*)(c->B)->data;
571: PetscInt *adi = ad->i,*adj,*aoi=ao->i,*aoj;
572: PetscScalar *ada,*aoa,*cda=cd->a,*coa=co->a;
573: Mat_SeqAIJ *p_loc,*p_oth;
574: PetscInt *pi_loc,*pj_loc,*pi_oth,*pj_oth,*pj;
575: PetscScalar *pa_loc,*pa_oth,*pa,valtmp,*ca;
576: PetscInt cm = C->rmap->n,anz,pnz;
577: Mat_APMPI *ptap = c->ap;
578: PetscScalar *apa_sparse;
579: PetscInt *api,*apj,*apJ,i,j,k,row;
580: PetscInt cstart = C->cmap->rstart;
581: PetscInt cdnz,conz,k0,k1,nextp;
582: MPI_Comm comm;
583: PetscMPIInt size;
586: PetscObjectGetComm((PetscObject)C,&comm);
587: MPI_Comm_size(comm,&size);
589: if (!ptap) {
590: SETERRQ(comm,PETSC_ERR_ARG_WRONGSTATE,"AP cannot be reused. Do not call MatFreeIntermediateDataStructures() or use '-mat_freeintermediatedatastructures'");
591: }
592: apa_sparse = ptap->apa;
594: /* 1) get P_oth = ptap->P_oth and P_loc = ptap->P_loc */
595: /*-----------------------------------------------------*/
596: /* update numerical values of P_oth and P_loc */
597: MatGetBrowsOfAoCols_MPIAIJ(A,P,MAT_REUSE_MATRIX,&ptap->startsj_s,&ptap->startsj_r,&ptap->bufa,&ptap->P_oth);
598: MatMPIAIJGetLocalMat(P,MAT_REUSE_MATRIX,&ptap->P_loc);
600: /* 2) compute numeric C_loc = A_loc*P = Ad*P_loc + Ao*P_oth */
601: /*----------------------------------------------------------*/
602: /* get data from symbolic products */
603: p_loc = (Mat_SeqAIJ*)(ptap->P_loc)->data;
604: pi_loc = p_loc->i; pj_loc = p_loc->j; pa_loc = p_loc->a;
605: if (size >1) {
606: p_oth = (Mat_SeqAIJ*)(ptap->P_oth)->data;
607: pi_oth = p_oth->i; pj_oth = p_oth->j; pa_oth = p_oth->a;
608: } else {
609: p_oth = NULL; pi_oth = NULL; pj_oth = NULL; pa_oth = NULL;
610: }
612: api = ptap->api;
613: apj = ptap->apj;
614: for (i=0; i<cm; i++) {
615: apJ = apj + api[i];
617: /* diagonal portion of A */
618: anz = adi[i+1] - adi[i];
619: adj = ad->j + adi[i];
620: ada = ad->a + adi[i];
621: for (j=0; j<anz; j++) {
622: row = adj[j];
623: pnz = pi_loc[row+1] - pi_loc[row];
624: pj = pj_loc + pi_loc[row];
625: pa = pa_loc + pi_loc[row];
626: /* perform sparse axpy */
627: valtmp = ada[j];
628: nextp = 0;
629: for (k=0; nextp<pnz; k++) {
630: if (apJ[k] == pj[nextp]) { /* column of AP == column of P */
631: apa_sparse[k] += valtmp*pa[nextp++];
632: }
633: }
634: PetscLogFlops(2.0*pnz);
635: }
637: /* off-diagonal portion of A */
638: anz = aoi[i+1] - aoi[i];
639: aoj = ao->j + aoi[i];
640: aoa = ao->a + aoi[i];
641: for (j=0; j<anz; j++) {
642: row = aoj[j];
643: pnz = pi_oth[row+1] - pi_oth[row];
644: pj = pj_oth + pi_oth[row];
645: pa = pa_oth + pi_oth[row];
646: /* perform sparse axpy */
647: valtmp = aoa[j];
648: nextp = 0;
649: for (k=0; nextp<pnz; k++) {
650: if (apJ[k] == pj[nextp]) { /* column of AP == column of P */
651: apa_sparse[k] += valtmp*pa[nextp++];
652: }
653: }
654: PetscLogFlops(2.0*pnz);
655: }
657: /* set values in C */
658: cdnz = cd->i[i+1] - cd->i[i];
659: conz = co->i[i+1] - co->i[i];
661: /* 1st off-diagoanl part of C */
662: ca = coa + co->i[i];
663: k = 0;
664: for (k0=0; k0<conz; k0++) {
665: if (apJ[k] >= cstart) break;
666: ca[k0] = apa_sparse[k];
667: apa_sparse[k] = 0.0;
668: k++;
669: }
671: /* diagonal part of C */
672: ca = cda + cd->i[i];
673: for (k1=0; k1<cdnz; k1++) {
674: ca[k1] = apa_sparse[k];
675: apa_sparse[k] = 0.0;
676: k++;
677: }
679: /* 2nd off-diagoanl part of C */
680: ca = coa + co->i[i];
681: for (; k0<conz; k0++) {
682: ca[k0] = apa_sparse[k];
683: apa_sparse[k] = 0.0;
684: k++;
685: }
686: }
687: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
688: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
690: if (ptap->freestruct) {
691: MatFreeIntermediateDataStructures(C);
692: }
693: return(0);
694: }
696: /* same as MatMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(), except using LLCondensed to avoid O(BN) memory requirement */
697: PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIAIJ(Mat A,Mat P,PetscReal fill,Mat *C)
698: {
699: PetscErrorCode ierr;
700: MPI_Comm comm;
701: PetscMPIInt size;
702: Mat Cmpi;
703: Mat_APMPI *ptap;
704: PetscFreeSpaceList free_space = NULL,current_space=NULL;
705: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data,*c;
706: Mat_SeqAIJ *ad = (Mat_SeqAIJ*)(a->A)->data,*ao=(Mat_SeqAIJ*)(a->B)->data,*p_loc,*p_oth;
707: PetscInt *pi_loc,*pj_loc,*pi_oth,*pj_oth,*dnz,*onz;
708: PetscInt *adi=ad->i,*adj=ad->j,*aoi=ao->i,*aoj=ao->j,rstart=A->rmap->rstart;
709: PetscInt i,pnz,row,*api,*apj,*Jptr,apnz,nspacedouble=0,j,nzi,*lnk,apnz_max=0;
710: PetscInt am=A->rmap->n,pn=P->cmap->n,pm=P->rmap->n,lsize=pn+20;
711: PetscReal afill;
712: PetscScalar *apa;
713: MatType mtype;
716: PetscObjectGetComm((PetscObject)A,&comm);
717: MPI_Comm_size(comm,&size);
719: /* create struct Mat_APMPI and attached it to C later */
720: PetscNew(&ptap);
722: /* get P_oth by taking rows of P (= non-zero cols of local A) from other processors */
723: MatGetBrowsOfAoCols_MPIAIJ(A,P,MAT_INITIAL_MATRIX,&ptap->startsj_s,&ptap->startsj_r,&ptap->bufa,&ptap->P_oth);
725: /* get P_loc by taking all local rows of P */
726: MatMPIAIJGetLocalMat(P,MAT_INITIAL_MATRIX,&ptap->P_loc);
728: p_loc = (Mat_SeqAIJ*)(ptap->P_loc)->data;
729: pi_loc = p_loc->i; pj_loc = p_loc->j;
730: if (size > 1) {
731: p_oth = (Mat_SeqAIJ*)(ptap->P_oth)->data;
732: pi_oth = p_oth->i; pj_oth = p_oth->j;
733: } else {
734: p_oth = NULL;
735: pi_oth = NULL; pj_oth = NULL;
736: }
738: /* first, compute symbolic AP = A_loc*P = A_diag*P_loc + A_off*P_oth */
739: /*-------------------------------------------------------------------*/
740: PetscMalloc1(am+2,&api);
741: ptap->api = api;
742: api[0] = 0;
744: PetscLLCondensedCreate_Scalable(lsize,&lnk);
746: /* Initial FreeSpace size is fill*(nnz(A)+nnz(P)) */
747: PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,PetscIntSumTruncate(adi[am],PetscIntSumTruncate(aoi[am],pi_loc[pm]))),&free_space);
748: current_space = free_space;
749: MatPreallocateInitialize(comm,am,pn,dnz,onz);
750: for (i=0; i<am; i++) {
751: /* diagonal portion of A */
752: nzi = adi[i+1] - adi[i];
753: for (j=0; j<nzi; j++) {
754: row = *adj++;
755: pnz = pi_loc[row+1] - pi_loc[row];
756: Jptr = pj_loc + pi_loc[row];
757: /* Expand list if it is not long enough */
758: if (pnz+apnz_max > lsize) {
759: lsize = pnz+apnz_max;
760: PetscLLCondensedExpand_Scalable(lsize, &lnk);
761: }
762: /* add non-zero cols of P into the sorted linked list lnk */
763: PetscLLCondensedAddSorted_Scalable(pnz,Jptr,lnk);
764: apnz = *lnk; /* The first element in the list is the number of items in the list */
765: api[i+1] = api[i] + apnz;
766: if (apnz > apnz_max) apnz_max = apnz;
767: }
768: /* off-diagonal portion of A */
769: nzi = aoi[i+1] - aoi[i];
770: for (j=0; j<nzi; j++) {
771: row = *aoj++;
772: pnz = pi_oth[row+1] - pi_oth[row];
773: Jptr = pj_oth + pi_oth[row];
774: /* Expand list if it is not long enough */
775: if (pnz+apnz_max > lsize) {
776: lsize = pnz + apnz_max;
777: PetscLLCondensedExpand_Scalable(lsize, &lnk);
778: }
779: /* add non-zero cols of P into the sorted linked list lnk */
780: PetscLLCondensedAddSorted_Scalable(pnz,Jptr,lnk);
781: apnz = *lnk; /* The first element in the list is the number of items in the list */
782: api[i+1] = api[i] + apnz;
783: if (apnz > apnz_max) apnz_max = apnz;
784: }
785: apnz = *lnk;
786: api[i+1] = api[i] + apnz;
787: if (apnz > apnz_max) apnz_max = apnz;
789: /* if free space is not available, double the total space in the list */
790: if (current_space->local_remaining<apnz) {
791: PetscFreeSpaceGet(PetscIntSumTruncate(apnz,current_space->total_array_size),¤t_space);
792: nspacedouble++;
793: }
795: /* Copy data into free space, then initialize lnk */
796: PetscLLCondensedClean_Scalable(apnz,current_space->array,lnk);
797: MatPreallocateSet(i+rstart,apnz,current_space->array,dnz,onz);
799: current_space->array += apnz;
800: current_space->local_used += apnz;
801: current_space->local_remaining -= apnz;
802: }
804: /* Allocate space for apj, initialize apj, and */
805: /* destroy list of free space and other temporary array(s) */
806: PetscMalloc1(api[am]+1,&ptap->apj);
807: apj = ptap->apj;
808: PetscFreeSpaceContiguous(&free_space,ptap->apj);
809: PetscLLCondensedDestroy_Scalable(lnk);
811: /* create and assemble symbolic parallel matrix Cmpi */
812: /*----------------------------------------------------*/
813: MatCreate(comm,&Cmpi);
814: MatSetSizes(Cmpi,am,pn,PETSC_DETERMINE,PETSC_DETERMINE);
815: MatSetBlockSizesFromMats(Cmpi,A,P);
816: MatGetType(A,&mtype);
817: MatSetType(Cmpi,mtype);
818: MatMPIAIJSetPreallocation(Cmpi,0,dnz,0,onz);
820: /* malloc apa for assembly Cmpi */
821: PetscCalloc1(apnz_max,&apa);
822: ptap->apa = apa;
824: MatSetValues_MPIAIJ_CopyFromCSRFormat_Symbolic(Cmpi, apj, api);
825: MatAssemblyBegin(Cmpi,MAT_FINAL_ASSEMBLY);
826: MatAssemblyEnd(Cmpi,MAT_FINAL_ASSEMBLY);
827: MatPreallocateFinalize(dnz,onz);
829: ptap->destroy = Cmpi->ops->destroy;
830: ptap->duplicate = Cmpi->ops->duplicate;
831: Cmpi->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIAIJ;
832: Cmpi->ops->destroy = MatDestroy_MPIAIJ_MatMatMult;
833: Cmpi->ops->freeintermediatedatastructures = MatFreeIntermediateDataStructures_MPIAIJ_AP;
835: /* attach the supporting struct to Cmpi for reuse */
836: c = (Mat_MPIAIJ*)Cmpi->data;
837: c->ap = ptap;
838: *C = Cmpi;
840: /* set MatInfo */
841: afill = (PetscReal)api[am]/(adi[am]+aoi[am]+pi_loc[pm]+1) + 1.e-5;
842: if (afill < 1.0) afill = 1.0;
843: Cmpi->info.mallocs = nspacedouble;
844: Cmpi->info.fill_ratio_given = fill;
845: Cmpi->info.fill_ratio_needed = afill;
847: #if defined(PETSC_USE_INFO)
848: if (api[am]) {
849: PetscInfo3(Cmpi,"Reallocs %D; Fill ratio: given %g needed %g.\n",nspacedouble,(double)fill,(double)afill);
850: PetscInfo1(Cmpi,"Use MatMatMult(A,B,MatReuse,%g,&C) for best performance.;\n",(double)afill);
851: } else {
852: PetscInfo(Cmpi,"Empty matrix product\n");
853: }
854: #endif
855: return(0);
856: }
858: /* This function is needed for the seqMPI matrix-matrix multiplication. */
859: /* Three input arrays are merged to one output array. The size of the */
860: /* output array is also output. Duplicate entries only show up once. */
861: static void Merge3SortedArrays(PetscInt size1, PetscInt *in1,
862: PetscInt size2, PetscInt *in2,
863: PetscInt size3, PetscInt *in3,
864: PetscInt *size4, PetscInt *out)
865: {
866: int i = 0, j = 0, k = 0, l = 0;
868: /* Traverse all three arrays */
869: while (i<size1 && j<size2 && k<size3) {
870: if (in1[i] < in2[j] && in1[i] < in3[k]) {
871: out[l++] = in1[i++];
872: }
873: else if(in2[j] < in1[i] && in2[j] < in3[k]) {
874: out[l++] = in2[j++];
875: }
876: else if(in3[k] < in1[i] && in3[k] < in2[j]) {
877: out[l++] = in3[k++];
878: }
879: else if(in1[i] == in2[j] && in1[i] < in3[k]) {
880: out[l++] = in1[i];
881: i++, j++;
882: }
883: else if(in1[i] == in3[k] && in1[i] < in2[j]) {
884: out[l++] = in1[i];
885: i++, k++;
886: }
887: else if(in3[k] == in2[j] && in2[j] < in1[i]) {
888: out[l++] = in2[j];
889: k++, j++;
890: }
891: else if(in1[i] == in2[j] && in1[i] == in3[k]) {
892: out[l++] = in1[i];
893: i++, j++, k++;
894: }
895: }
897: /* Traverse two remaining arrays */
898: while (i<size1 && j<size2) {
899: if (in1[i] < in2[j]) {
900: out[l++] = in1[i++];
901: }
902: else if(in1[i] > in2[j]) {
903: out[l++] = in2[j++];
904: }
905: else {
906: out[l++] = in1[i];
907: i++, j++;
908: }
909: }
911: while (i<size1 && k<size3) {
912: if (in1[i] < in3[k]) {
913: out[l++] = in1[i++];
914: }
915: else if(in1[i] > in3[k]) {
916: out[l++] = in3[k++];
917: }
918: else {
919: out[l++] = in1[i];
920: i++, k++;
921: }
922: }
924: while (k<size3 && j<size2) {
925: if (in3[k] < in2[j]) {
926: out[l++] = in3[k++];
927: }
928: else if(in3[k] > in2[j]) {
929: out[l++] = in2[j++];
930: }
931: else {
932: out[l++] = in3[k];
933: k++, j++;
934: }
935: }
937: /* Traverse one remaining array */
938: while (i<size1) out[l++] = in1[i++];
939: while (j<size2) out[l++] = in2[j++];
940: while (k<size3) out[l++] = in3[k++];
942: *size4 = l;
943: }
945: /* This matrix-matrix multiplication algorithm divides the multiplication into three multiplications and */
946: /* adds up the products. Two of these three multiplications are performed with existing (sequential) */
947: /* matrix-matrix multiplications. */
950: PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIAIJ_seqMPI(Mat A, Mat P, PetscReal fill, Mat *C)
951: {
952: PetscErrorCode ierr;
953: MPI_Comm comm;
954: PetscMPIInt size;
955: Mat Cmpi;
956: Mat_APMPI *ptap;
957: PetscFreeSpaceList free_space_diag=NULL, current_space=NULL;
958: Mat_MPIAIJ *a =(Mat_MPIAIJ*)A->data;
959: Mat_SeqAIJ *ad =(Mat_SeqAIJ*)(a->A)->data,*ao=(Mat_SeqAIJ*)(a->B)->data,*p_loc;
960: Mat_MPIAIJ *p =(Mat_MPIAIJ*)P->data;
961: Mat_MPIAIJ *c;
962: Mat_SeqAIJ *adpd_seq, *p_off, *aopoth_seq;
963: PetscInt adponz, adpdnz;
964: PetscInt *pi_loc,*dnz,*onz;
965: PetscInt *adi=ad->i,*adj=ad->j,*aoi=ao->i,rstart=A->rmap->rstart;
966: PetscInt *lnk,i, i1=0,pnz,row,*adpoi,*adpoj, *api, *adpoJ, *aopJ, *apJ,*Jptr, aopnz, nspacedouble=0,j,nzi,
967: *apj,apnz, *adpdi, *adpdj, *adpdJ, *poff_i, *poff_j, *j_temp, *aopothi, *aopothj;
968: PetscInt am=A->rmap->n,pN=P->cmap->N,pn=P->cmap->n,pm=P->rmap->n, p_colstart, p_colend;
969: PetscBT lnkbt;
970: PetscScalar *apa;
971: PetscReal afill;
972: PetscMPIInt rank;
973: Mat adpd, aopoth;
974: MatType mtype;
975: const char *prefix;
978: PetscObjectGetComm((PetscObject)A,&comm);
979: MPI_Comm_size(comm,&size);
980: MPI_Comm_rank(comm, &rank);
981: MatGetOwnershipRangeColumn(P, &p_colstart, &p_colend);
983: /* create struct Mat_APMPI and attached it to C later */
984: PetscNew(&ptap);
986: /* get P_oth by taking rows of P (= non-zero cols of local A) from other processors */
987: MatGetBrowsOfAoCols_MPIAIJ(A,P,MAT_INITIAL_MATRIX,&ptap->startsj_s,&ptap->startsj_r,&ptap->bufa,&ptap->P_oth);
989: /* get P_loc by taking all local rows of P */
990: MatMPIAIJGetLocalMat(P,MAT_INITIAL_MATRIX,&ptap->P_loc);
993: p_loc = (Mat_SeqAIJ*)(ptap->P_loc)->data;
994: pi_loc = p_loc->i;
996: /* Allocate memory for the i arrays of the matrices A*P, A_diag*P_off and A_offd * P */
997: PetscMalloc1(am+2,&api);
998: PetscMalloc1(am+2,&adpoi);
1000: adpoi[0] = 0;
1001: ptap->api = api;
1002: api[0] = 0;
1004: /* create and initialize a linked list, will be used for both A_diag * P_loc_off and A_offd * P_oth */
1005: PetscLLCondensedCreate(pN,pN,&lnk,&lnkbt);
1006: MatPreallocateInitialize(comm,am,pn,dnz,onz);
1008: /* Symbolic calc of A_loc_diag * P_loc_diag */
1009: MatGetOptionsPrefix(A,&prefix);
1010: MatSetOptionsPrefix(a->A,prefix);
1011: MatAppendOptionsPrefix(a->A,"inner_diag_");
1012: MatMatMultSymbolic_SeqAIJ_SeqAIJ(a->A, p->A, fill, &adpd);
1013: adpd_seq = (Mat_SeqAIJ*)((adpd)->data);
1014: adpdi = adpd_seq->i; adpdj = adpd_seq->j;
1015: p_off = (Mat_SeqAIJ*)((p->B)->data);
1016: poff_i = p_off->i; poff_j = p_off->j;
1018: /* j_temp stores indices of a result row before they are added to the linked list */
1019: PetscMalloc1(pN+2,&j_temp);
1022: /* Symbolic calc of the A_diag * p_loc_off */
1023: /* Initial FreeSpace size is fill*(nnz(A)+nnz(P)) */
1024: PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,PetscIntSumTruncate(adi[am],PetscIntSumTruncate(aoi[am],pi_loc[pm]))),&free_space_diag);
1025: current_space = free_space_diag;
1027: for (i=0; i<am; i++) {
1028: /* A_diag * P_loc_off */
1029: nzi = adi[i+1] - adi[i];
1030: for (j=0; j<nzi; j++) {
1031: row = *adj++;
1032: pnz = poff_i[row+1] - poff_i[row];
1033: Jptr = poff_j + poff_i[row];
1034: for(i1 = 0; i1 < pnz; i1++) {
1035: j_temp[i1] = p->garray[Jptr[i1]];
1036: }
1037: /* add non-zero cols of P into the sorted linked list lnk */
1038: PetscLLCondensedAddSorted(pnz,j_temp,lnk,lnkbt);
1039: }
1041: adponz = lnk[0];
1042: adpoi[i+1] = adpoi[i] + adponz;
1044: /* if free space is not available, double the total space in the list */
1045: if (current_space->local_remaining<adponz) {
1046: PetscFreeSpaceGet(PetscIntSumTruncate(adponz,current_space->total_array_size),¤t_space);
1047: nspacedouble++;
1048: }
1050: /* Copy data into free space, then initialize lnk */
1051: PetscLLCondensedClean(pN,adponz,current_space->array,lnk,lnkbt);
1053: current_space->array += adponz;
1054: current_space->local_used += adponz;
1055: current_space->local_remaining -= adponz;
1056: }
1058: /* Symbolic calc of A_off * P_oth */
1059: MatSetOptionsPrefix(a->B,prefix);
1060: MatAppendOptionsPrefix(a->B,"inner_offdiag_");
1061: MatMatMultSymbolic_SeqAIJ_SeqAIJ(a->B, ptap->P_oth, fill, &aopoth);
1062: aopoth_seq = (Mat_SeqAIJ*)((aopoth)->data);
1063: aopothi = aopoth_seq->i; aopothj = aopoth_seq->j;
1065: /* Allocate space for apj, adpj, aopj, ... */
1066: /* destroy lists of free space and other temporary array(s) */
1068: PetscMalloc1(aopothi[am] + adpoi[am] + adpdi[am]+2, &ptap->apj);
1069: PetscMalloc1(adpoi[am]+2, &adpoj);
1071: /* Copy from linked list to j-array */
1072: PetscFreeSpaceContiguous(&free_space_diag,adpoj);
1073: PetscLLDestroy(lnk,lnkbt);
1075: adpoJ = adpoj;
1076: adpdJ = adpdj;
1077: aopJ = aopothj;
1078: apj = ptap->apj;
1079: apJ = apj; /* still empty */
1081: /* Merge j-arrays of A_off * P, A_diag * P_loc_off, and */
1082: /* A_diag * P_loc_diag to get A*P */
1083: for (i = 0; i < am; i++) {
1084: aopnz = aopothi[i+1] - aopothi[i];
1085: adponz = adpoi[i+1] - adpoi[i];
1086: adpdnz = adpdi[i+1] - adpdi[i];
1088: /* Correct indices from A_diag*P_diag */
1089: for(i1 = 0; i1 < adpdnz; i1++) {
1090: adpdJ[i1] += p_colstart;
1091: }
1092: /* Merge j-arrays of A_diag * P_loc_off and A_diag * P_loc_diag and A_off * P_oth */
1093: Merge3SortedArrays(adponz, adpoJ, adpdnz, adpdJ, aopnz, aopJ, &apnz, apJ);
1094: MatPreallocateSet(i+rstart, apnz, apJ, dnz, onz);
1096: aopJ += aopnz;
1097: adpoJ += adponz;
1098: adpdJ += adpdnz;
1099: apJ += apnz;
1100: api[i+1] = api[i] + apnz;
1101: }
1103: /* malloc apa to store dense row A[i,:]*P */
1104: PetscCalloc1(pN+2,&apa);
1106: ptap->apa = apa;
1107: /* create and assemble symbolic parallel matrix Cmpi */
1108: MatCreate(comm,&Cmpi);
1109: MatSetSizes(Cmpi,am,pn,PETSC_DETERMINE,PETSC_DETERMINE);
1110: MatSetBlockSizesFromMats(Cmpi,A,P);
1111: MatGetType(A,&mtype);
1112: MatSetType(Cmpi,mtype);
1113: MatMPIAIJSetPreallocation(Cmpi,0,dnz,0,onz);
1116: MatSetValues_MPIAIJ_CopyFromCSRFormat_Symbolic(Cmpi, apj, api);
1117: MatAssemblyBegin(Cmpi,MAT_FINAL_ASSEMBLY);
1118: MatAssemblyEnd(Cmpi,MAT_FINAL_ASSEMBLY);
1119: MatPreallocateFinalize(dnz,onz);
1122: ptap->destroy = Cmpi->ops->destroy;
1123: ptap->duplicate = Cmpi->ops->duplicate;
1124: Cmpi->ops->matmultnumeric = MatMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable;
1125: Cmpi->ops->destroy = MatDestroy_MPIAIJ_MatMatMult;
1127: /* attach the supporting struct to Cmpi for reuse */
1128: c = (Mat_MPIAIJ*)Cmpi->data;
1129: c->ap = ptap;
1130: *C = Cmpi;
1132: /* set MatInfo */
1133: afill = (PetscReal)api[am]/(adi[am]+aoi[am]+pi_loc[pm]+1) + 1.e-5;
1134: if (afill < 1.0) afill = 1.0;
1135: Cmpi->info.mallocs = nspacedouble;
1136: Cmpi->info.fill_ratio_given = fill;
1137: Cmpi->info.fill_ratio_needed = afill;
1139: #if defined(PETSC_USE_INFO)
1140: if (api[am]) {
1141: PetscInfo3(Cmpi,"Reallocs %D; Fill ratio: given %g needed %g.\n",nspacedouble,(double)fill,(double)afill);
1142: PetscInfo1(Cmpi,"Use MatMatMult(A,B,MatReuse,%g,&C) for best performance.;\n",(double)afill);
1143: } else {
1144: PetscInfo(Cmpi,"Empty matrix product\n");
1145: }
1146: #endif
1148: MatDestroy(&aopoth);
1149: MatDestroy(&adpd);
1150: PetscFree(j_temp);
1151: PetscFree(adpoj);
1152: PetscFree(adpoi);
1153: return(0);
1154: }
1157: /*-------------------------------------------------------------------------*/
1158: PetscErrorCode MatTransposeMatMult_MPIAIJ_MPIAIJ(Mat P,Mat A,MatReuse scall,PetscReal fill,Mat *C)
1159: {
1161: const char *algTypes[3] = {"scalable","nonscalable","matmatmult"};
1162: PetscInt aN=A->cmap->N,alg=1; /* set default algorithm */
1163: PetscBool flg;
1166: if (scall == MAT_INITIAL_MATRIX) {
1167: PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"MatTransposeMatMult","Mat");
1168: PetscOptionsEList("-mattransposematmult_via","Algorithmic approach","MatTransposeMatMult",algTypes,3,algTypes[1],&alg,&flg);
1169: PetscOptionsEnd();
1171: PetscLogEventBegin(MAT_TransposeMatMultSymbolic,P,A,0,0);
1172: switch (alg) {
1173: case 1:
1174: if (!flg && aN > 100000) { /* may switch to scalable algorithm as default */
1175: MatInfo Ainfo,Pinfo;
1176: PetscInt nz_local;
1177: PetscBool alg_scalable_loc=PETSC_FALSE,alg_scalable;
1178: MPI_Comm comm;
1180: MatGetInfo(A,MAT_LOCAL,&Ainfo);
1181: MatGetInfo(P,MAT_LOCAL,&Pinfo);
1182: nz_local = (PetscInt)(Ainfo.nz_allocated + Pinfo.nz_allocated); /* estimated local nonzero entries */
1184: if (aN > fill*nz_local) alg_scalable_loc = PETSC_TRUE;
1185: PetscObjectGetComm((PetscObject)A,&comm);
1186: MPIU_Allreduce(&alg_scalable_loc,&alg_scalable,1,MPIU_BOOL,MPI_LOR,comm);
1188: if (alg_scalable) {
1189: alg = 0; /* scalable algorithm would slower than nonscalable algorithm */
1190: MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ(P,A,fill,C);
1191: break;
1192: }
1193: }
1194: MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(P,A,fill,C);
1195: break;
1196: case 2:
1197: {
1198: Mat Pt;
1199: Mat_APMPI *ptap;
1200: Mat_MPIAIJ *c;
1201: MatTranspose(P,MAT_INITIAL_MATRIX,&Pt);
1202: MatMatMult(Pt,A,MAT_INITIAL_MATRIX,fill,C);
1203: c = (Mat_MPIAIJ*)(*C)->data;
1204: ptap = c->ap;
1205: if (ptap) {
1206: ptap->Pt = Pt;
1207: (*C)->ops->freeintermediatedatastructures = MatFreeIntermediateDataStructures_MPIAIJ_AP;
1208: }
1209: (*C)->ops->mattransposemultnumeric = MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_matmatmult;
1210: return(0);
1211: }
1212: break;
1213: default: /* scalable algorithm */
1214: MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ(P,A,fill,C);
1215: break;
1216: }
1217: PetscLogEventEnd(MAT_TransposeMatMultSymbolic,P,A,0,0);
1219: {
1220: Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*C)->data;
1221: Mat_APMPI *ap = c->ap;
1222: PetscOptionsBegin(PetscObjectComm((PetscObject)(*C)),((PetscObject)(*C))->prefix,"MatFreeIntermediateDataStructures","Mat");
1223: ap->freestruct = PETSC_FALSE;
1224: PetscOptionsBool("-mat_freeintermediatedatastructures","Free intermediate data structures", "MatFreeIntermediateDataStructures",ap->freestruct,&ap->freestruct, NULL);
1225: PetscOptionsEnd();
1226: }
1227: }
1229: PetscLogEventBegin(MAT_TransposeMatMultNumeric,P,A,0,0);
1230: (*(*C)->ops->mattransposemultnumeric)(P,A,*C);
1231: PetscLogEventEnd(MAT_TransposeMatMultNumeric,P,A,0,0);
1232: return(0);
1233: }
1235: /* This routine only works when scall=MAT_REUSE_MATRIX! */
1236: PetscErrorCode MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_matmatmult(Mat P,Mat A,Mat C)
1237: {
1239: Mat_MPIAIJ *c=(Mat_MPIAIJ*)C->data;
1240: Mat_APMPI *ptap= c->ap;
1241: Mat Pt;
1244: if (!ptap) {
1245: MPI_Comm comm;
1246: PetscObjectGetComm((PetscObject)C,&comm);
1247: SETERRQ(comm,PETSC_ERR_ARG_WRONGSTATE,"PtA cannot be reused. Do not call MatFreeIntermediateDataStructures() or use '-mat_freeintermediatedatastructures'");
1248: }
1250: Pt=ptap->Pt;
1251: MatTranspose(P,MAT_REUSE_MATRIX,&Pt);
1252: MatMatMultNumeric(Pt,A,C);
1254: /* supporting struct ptap consumes almost same amount of memory as C=PtAP, release it if C will not be updated by A and P */
1255: if (ptap->freestruct) {
1256: MatFreeIntermediateDataStructures(C);
1257: }
1258: return(0);
1259: }
1261: /* This routine is modified from MatPtAPSymbolic_MPIAIJ_MPIAIJ() */
1262: PetscErrorCode MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(Mat P,Mat A,PetscReal fill,Mat *C)
1263: {
1264: PetscErrorCode ierr;
1265: Mat_APMPI *ptap;
1266: Mat_MPIAIJ *p=(Mat_MPIAIJ*)P->data,*c;
1267: MPI_Comm comm;
1268: PetscMPIInt size,rank;
1269: Mat Cmpi;
1270: PetscFreeSpaceList free_space=NULL,current_space=NULL;
1271: PetscInt pn=P->cmap->n,aN=A->cmap->N,an=A->cmap->n;
1272: PetscInt *lnk,i,k,nsend;
1273: PetscBT lnkbt;
1274: PetscMPIInt tagi,tagj,*len_si,*len_s,*len_ri,icompleted=0,nrecv;
1275: PetscInt **buf_rj,**buf_ri,**buf_ri_k;
1276: PetscInt len,proc,*dnz,*onz,*owners,nzi;
1277: PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextci;
1278: MPI_Request *swaits,*rwaits;
1279: MPI_Status *sstatus,rstatus;
1280: PetscLayout rowmap;
1281: PetscInt *owners_co,*coi,*coj; /* i and j array of (p->B)^T*A*P - used in the communication */
1282: PetscMPIInt *len_r,*id_r; /* array of length of comm->size, store send/recv matrix values */
1283: PetscInt *Jptr,*prmap=p->garray,con,j,Crmax;
1284: Mat_SeqAIJ *a_loc,*c_loc,*c_oth;
1285: PetscTable ta;
1286: MatType mtype;
1287: const char *prefix;
1290: PetscObjectGetComm((PetscObject)A,&comm);
1291: MPI_Comm_size(comm,&size);
1292: MPI_Comm_rank(comm,&rank);
1294: /* create symbolic parallel matrix Cmpi */
1295: MatCreate(comm,&Cmpi);
1296: MatGetType(A,&mtype);
1297: MatSetType(Cmpi,mtype);
1299: Cmpi->ops->mattransposemultnumeric = MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable;
1301: /* create struct Mat_APMPI and attached it to C later */
1302: PetscNew(&ptap);
1303: ptap->reuse = MAT_INITIAL_MATRIX;
1305: /* (0) compute Rd = Pd^T, Ro = Po^T */
1306: /* --------------------------------- */
1307: MatTranspose_SeqAIJ(p->A,MAT_INITIAL_MATRIX,&ptap->Rd);
1308: MatTranspose_SeqAIJ(p->B,MAT_INITIAL_MATRIX,&ptap->Ro);
1310: /* (1) compute symbolic A_loc */
1311: /* ---------------------------*/
1312: MatMPIAIJGetLocalMat(A,MAT_INITIAL_MATRIX,&ptap->A_loc);
1314: /* (2-1) compute symbolic C_oth = Ro*A_loc */
1315: /* ------------------------------------ */
1316: MatGetOptionsPrefix(A,&prefix);
1317: MatSetOptionsPrefix(ptap->Ro,prefix);
1318: MatAppendOptionsPrefix(ptap->Ro,"inner_offdiag_");
1319: MatMatMultSymbolic_SeqAIJ_SeqAIJ(ptap->Ro,ptap->A_loc,fill,&ptap->C_oth);
1321: /* (3) send coj of C_oth to other processors */
1322: /* ------------------------------------------ */
1323: /* determine row ownership */
1324: PetscLayoutCreate(comm,&rowmap);
1325: rowmap->n = pn;
1326: rowmap->bs = 1;
1327: PetscLayoutSetUp(rowmap);
1328: owners = rowmap->range;
1330: /* determine the number of messages to send, their lengths */
1331: PetscMalloc4(size,&len_s,size,&len_si,size,&sstatus,size+2,&owners_co);
1332: PetscMemzero(len_s,size*sizeof(PetscMPIInt));
1333: PetscMemzero(len_si,size*sizeof(PetscMPIInt));
1335: c_oth = (Mat_SeqAIJ*)ptap->C_oth->data;
1336: coi = c_oth->i; coj = c_oth->j;
1337: con = ptap->C_oth->rmap->n;
1338: proc = 0;
1339: for (i=0; i<con; i++) {
1340: while (prmap[i] >= owners[proc+1]) proc++;
1341: len_si[proc]++; /* num of rows in Co(=Pt*A) to be sent to [proc] */
1342: len_s[proc] += coi[i+1] - coi[i]; /* num of nonzeros in Co to be sent to [proc] */
1343: }
1345: len = 0; /* max length of buf_si[], see (4) */
1346: owners_co[0] = 0;
1347: nsend = 0;
1348: for (proc=0; proc<size; proc++) {
1349: owners_co[proc+1] = owners_co[proc] + len_si[proc];
1350: if (len_s[proc]) {
1351: nsend++;
1352: len_si[proc] = 2*(len_si[proc] + 1); /* length of buf_si to be sent to [proc] */
1353: len += len_si[proc];
1354: }
1355: }
1357: /* determine the number and length of messages to receive for coi and coj */
1358: PetscGatherNumberOfMessages(comm,NULL,len_s,&nrecv);
1359: PetscGatherMessageLengths2(comm,nsend,nrecv,len_s,len_si,&id_r,&len_r,&len_ri);
1361: /* post the Irecv and Isend of coj */
1362: PetscCommGetNewTag(comm,&tagj);
1363: PetscPostIrecvInt(comm,tagj,nrecv,id_r,len_r,&buf_rj,&rwaits);
1364: PetscMalloc1(nsend+1,&swaits);
1365: for (proc=0, k=0; proc<size; proc++) {
1366: if (!len_s[proc]) continue;
1367: i = owners_co[proc];
1368: MPI_Isend(coj+coi[i],len_s[proc],MPIU_INT,proc,tagj,comm,swaits+k);
1369: k++;
1370: }
1372: /* (2-2) compute symbolic C_loc = Rd*A_loc */
1373: /* ---------------------------------------- */
1374: MatSetOptionsPrefix(ptap->Rd,prefix);
1375: MatAppendOptionsPrefix(ptap->Rd,"inner_diag_");
1376: MatMatMultSymbolic_SeqAIJ_SeqAIJ(ptap->Rd,ptap->A_loc,fill,&ptap->C_loc);
1377: c_loc = (Mat_SeqAIJ*)ptap->C_loc->data;
1379: /* receives coj are complete */
1380: for (i=0; i<nrecv; i++) {
1381: MPI_Waitany(nrecv,rwaits,&icompleted,&rstatus);
1382: }
1383: PetscFree(rwaits);
1384: if (nsend) {MPI_Waitall(nsend,swaits,sstatus);}
1386: /* add received column indices into ta to update Crmax */
1387: a_loc = (Mat_SeqAIJ*)(ptap->A_loc)->data;
1389: /* create and initialize a linked list */
1390: PetscTableCreate(an,aN,&ta); /* for compute Crmax */
1391: MatRowMergeMax_SeqAIJ(a_loc,ptap->A_loc->rmap->N,ta);
1393: for (k=0; k<nrecv; k++) {/* k-th received message */
1394: Jptr = buf_rj[k];
1395: for (j=0; j<len_r[k]; j++) {
1396: PetscTableAdd(ta,*(Jptr+j)+1,1,INSERT_VALUES);
1397: }
1398: }
1399: PetscTableGetCount(ta,&Crmax);
1400: PetscTableDestroy(&ta);
1402: /* (4) send and recv coi */
1403: /*-----------------------*/
1404: PetscCommGetNewTag(comm,&tagi);
1405: PetscPostIrecvInt(comm,tagi,nrecv,id_r,len_ri,&buf_ri,&rwaits);
1406: PetscMalloc1(len+1,&buf_s);
1407: buf_si = buf_s; /* points to the beginning of k-th msg to be sent */
1408: for (proc=0,k=0; proc<size; proc++) {
1409: if (!len_s[proc]) continue;
1410: /* form outgoing message for i-structure:
1411: buf_si[0]: nrows to be sent
1412: [1:nrows]: row index (global)
1413: [nrows+1:2*nrows+1]: i-structure index
1414: */
1415: /*-------------------------------------------*/
1416: nrows = len_si[proc]/2 - 1; /* num of rows in Co to be sent to [proc] */
1417: buf_si_i = buf_si + nrows+1;
1418: buf_si[0] = nrows;
1419: buf_si_i[0] = 0;
1420: nrows = 0;
1421: for (i=owners_co[proc]; i<owners_co[proc+1]; i++) {
1422: nzi = coi[i+1] - coi[i];
1423: buf_si_i[nrows+1] = buf_si_i[nrows] + nzi; /* i-structure */
1424: buf_si[nrows+1] = prmap[i] -owners[proc]; /* local row index */
1425: nrows++;
1426: }
1427: MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,swaits+k);
1428: k++;
1429: buf_si += len_si[proc];
1430: }
1431: for (i=0; i<nrecv; i++) {
1432: MPI_Waitany(nrecv,rwaits,&icompleted,&rstatus);
1433: }
1434: PetscFree(rwaits);
1435: if (nsend) {MPI_Waitall(nsend,swaits,sstatus);}
1437: PetscFree4(len_s,len_si,sstatus,owners_co);
1438: PetscFree(len_ri);
1439: PetscFree(swaits);
1440: PetscFree(buf_s);
1442: /* (5) compute the local portion of Cmpi */
1443: /* ------------------------------------------ */
1444: /* set initial free space to be Crmax, sufficient for holding nozeros in each row of Cmpi */
1445: PetscFreeSpaceGet(Crmax,&free_space);
1446: current_space = free_space;
1448: PetscMalloc3(nrecv,&buf_ri_k,nrecv,&nextrow,nrecv,&nextci);
1449: for (k=0; k<nrecv; k++) {
1450: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
1451: nrows = *buf_ri_k[k];
1452: nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */
1453: nextci[k] = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure */
1454: }
1456: MatPreallocateInitialize(comm,pn,an,dnz,onz);
1457: PetscLLCondensedCreate(Crmax,aN,&lnk,&lnkbt);
1458: for (i=0; i<pn; i++) {
1459: /* add C_loc into Cmpi */
1460: nzi = c_loc->i[i+1] - c_loc->i[i];
1461: Jptr = c_loc->j + c_loc->i[i];
1462: PetscLLCondensedAddSorted(nzi,Jptr,lnk,lnkbt);
1464: /* add received col data into lnk */
1465: for (k=0; k<nrecv; k++) { /* k-th received message */
1466: if (i == *nextrow[k]) { /* i-th row */
1467: nzi = *(nextci[k]+1) - *nextci[k];
1468: Jptr = buf_rj[k] + *nextci[k];
1469: PetscLLCondensedAddSorted(nzi,Jptr,lnk,lnkbt);
1470: nextrow[k]++; nextci[k]++;
1471: }
1472: }
1473: nzi = lnk[0];
1475: /* copy data into free space, then initialize lnk */
1476: PetscLLCondensedClean(aN,nzi,current_space->array,lnk,lnkbt);
1477: MatPreallocateSet(i+owners[rank],nzi,current_space->array,dnz,onz);
1478: }
1479: PetscFree3(buf_ri_k,nextrow,nextci);
1480: PetscLLDestroy(lnk,lnkbt);
1481: PetscFreeSpaceDestroy(free_space);
1483: /* local sizes and preallocation */
1484: MatSetSizes(Cmpi,pn,an,PETSC_DETERMINE,PETSC_DETERMINE);
1485: MatSetBlockSizes(Cmpi,PetscAbs(P->cmap->bs),PetscAbs(P->cmap->bs));
1486: MatMPIAIJSetPreallocation(Cmpi,0,dnz,0,onz);
1487: MatPreallocateFinalize(dnz,onz);
1489: /* members in merge */
1490: PetscFree(id_r);
1491: PetscFree(len_r);
1492: PetscFree(buf_ri[0]);
1493: PetscFree(buf_ri);
1494: PetscFree(buf_rj[0]);
1495: PetscFree(buf_rj);
1496: PetscLayoutDestroy(&rowmap);
1498: /* attach the supporting struct to Cmpi for reuse */
1499: c = (Mat_MPIAIJ*)Cmpi->data;
1500: c->ap = ptap;
1501: ptap->destroy = Cmpi->ops->destroy;
1503: /* Cmpi is not ready for use - assembly will be done by MatPtAPNumeric() */
1504: Cmpi->assembled = PETSC_FALSE;
1505: Cmpi->ops->destroy = MatDestroy_MPIAIJ_PtAP;
1506: Cmpi->ops->freeintermediatedatastructures = MatFreeIntermediateDataStructures_MPIAIJ_AP;
1508: *C = Cmpi;
1509: return(0);
1510: }
1512: PetscErrorCode MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable(Mat P,Mat A,Mat C)
1513: {
1514: PetscErrorCode ierr;
1515: Mat_MPIAIJ *p=(Mat_MPIAIJ*)P->data,*c=(Mat_MPIAIJ*)C->data;
1516: Mat_SeqAIJ *c_seq;
1517: Mat_APMPI *ptap = c->ap;
1518: Mat A_loc,C_loc,C_oth;
1519: PetscInt i,rstart,rend,cm,ncols,row;
1520: const PetscInt *cols;
1521: const PetscScalar *vals;
1524: if (!ptap) {
1525: MPI_Comm comm;
1526: PetscObjectGetComm((PetscObject)C,&comm);
1527: SETERRQ(comm,PETSC_ERR_ARG_WRONGSTATE,"PtA cannot be reused. Do not call MatFreeIntermediateDataStructures() or use '-mat_freeintermediatedatastructures'");
1528: }
1530: MatZeroEntries(C);
1532: if (ptap->reuse == MAT_REUSE_MATRIX) {
1533: /* These matrices are obtained in MatTransposeMatMultSymbolic() */
1534: /* 1) get R = Pd^T, Ro = Po^T */
1535: /*----------------------------*/
1536: MatTranspose_SeqAIJ(p->A,MAT_REUSE_MATRIX,&ptap->Rd);
1537: MatTranspose_SeqAIJ(p->B,MAT_REUSE_MATRIX,&ptap->Ro);
1539: /* 2) compute numeric A_loc */
1540: /*--------------------------*/
1541: MatMPIAIJGetLocalMat(A,MAT_REUSE_MATRIX,&ptap->A_loc);
1542: }
1544: /* 3) C_loc = Rd*A_loc, C_oth = Ro*A_loc */
1545: A_loc = ptap->A_loc;
1546: ((ptap->C_loc)->ops->matmultnumeric)(ptap->Rd,A_loc,ptap->C_loc);
1547: ((ptap->C_oth)->ops->matmultnumeric)(ptap->Ro,A_loc,ptap->C_oth);
1548: C_loc = ptap->C_loc;
1549: C_oth = ptap->C_oth;
1551: /* add C_loc and Co to to C */
1552: MatGetOwnershipRange(C,&rstart,&rend);
1554: /* C_loc -> C */
1555: cm = C_loc->rmap->N;
1556: c_seq = (Mat_SeqAIJ*)C_loc->data;
1557: cols = c_seq->j;
1558: vals = c_seq->a;
1559: for (i=0; i<cm; i++) {
1560: ncols = c_seq->i[i+1] - c_seq->i[i];
1561: row = rstart + i;
1562: MatSetValues(C,1,&row,ncols,cols,vals,ADD_VALUES);
1563: cols += ncols; vals += ncols;
1564: }
1566: /* Co -> C, off-processor part */
1567: cm = C_oth->rmap->N;
1568: c_seq = (Mat_SeqAIJ*)C_oth->data;
1569: cols = c_seq->j;
1570: vals = c_seq->a;
1571: for (i=0; i<cm; i++) {
1572: ncols = c_seq->i[i+1] - c_seq->i[i];
1573: row = p->garray[i];
1574: MatSetValues(C,1,&row,ncols,cols,vals,ADD_VALUES);
1575: cols += ncols; vals += ncols;
1576: }
1577: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
1578: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
1580: ptap->reuse = MAT_REUSE_MATRIX;
1582: /* supporting struct ptap consumes almost same amount of memory as C=PtAP, release it if C will not be updated by A and P */
1583: if (ptap->freestruct) {
1584: MatFreeIntermediateDataStructures(C);
1585: }
1586: return(0);
1587: }
1589: PetscErrorCode MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ(Mat P,Mat A,Mat C)
1590: {
1591: PetscErrorCode ierr;
1592: Mat_Merge_SeqsToMPI *merge;
1593: Mat_MPIAIJ *p =(Mat_MPIAIJ*)P->data,*c=(Mat_MPIAIJ*)C->data;
1594: Mat_SeqAIJ *pd=(Mat_SeqAIJ*)(p->A)->data,*po=(Mat_SeqAIJ*)(p->B)->data;
1595: Mat_APMPI *ptap;
1596: PetscInt *adj;
1597: PetscInt i,j,k,anz,pnz,row,*cj,nexta;
1598: MatScalar *ada,*ca,valtmp;
1599: PetscInt am =A->rmap->n,cm=C->rmap->n,pon=(p->B)->cmap->n;
1600: MPI_Comm comm;
1601: PetscMPIInt size,rank,taga,*len_s;
1602: PetscInt *owners,proc,nrows,**buf_ri_k,**nextrow,**nextci;
1603: PetscInt **buf_ri,**buf_rj;
1604: PetscInt cnz=0,*bj_i,*bi,*bj,bnz,nextcj; /* bi,bj,ba: local array of C(mpi mat) */
1605: MPI_Request *s_waits,*r_waits;
1606: MPI_Status *status;
1607: MatScalar **abuf_r,*ba_i,*pA,*coa,*ba;
1608: PetscInt *ai,*aj,*coi,*coj,*poJ,*pdJ;
1609: Mat A_loc;
1610: Mat_SeqAIJ *a_loc;
1613: PetscObjectGetComm((PetscObject)C,&comm);
1614: MPI_Comm_size(comm,&size);
1615: MPI_Comm_rank(comm,&rank);
1617: ptap = c->ap;
1618: if (!ptap) SETERRQ(comm,PETSC_ERR_ARG_WRONGSTATE,"PtA cannot be reused. Do not call MatFreeIntermediateDataStructures() or use '-mat_freeintermediatedatastructures'");
1619: merge = ptap->merge;
1621: /* 2) compute numeric C_seq = P_loc^T*A_loc */
1622: /*------------------------------------------*/
1623: /* get data from symbolic products */
1624: coi = merge->coi; coj = merge->coj;
1625: PetscCalloc1(coi[pon]+1,&coa);
1626: bi = merge->bi; bj = merge->bj;
1627: owners = merge->rowmap->range;
1628: PetscCalloc1(bi[cm]+1,&ba);
1630: /* get A_loc by taking all local rows of A */
1631: A_loc = ptap->A_loc;
1632: MatMPIAIJGetLocalMat(A,MAT_REUSE_MATRIX,&A_loc);
1633: a_loc = (Mat_SeqAIJ*)(A_loc)->data;
1634: ai = a_loc->i;
1635: aj = a_loc->j;
1637: for (i=0; i<am; i++) {
1638: anz = ai[i+1] - ai[i];
1639: adj = aj + ai[i];
1640: ada = a_loc->a + ai[i];
1642: /* 2-b) Compute Cseq = P_loc[i,:]^T*A[i,:] using outer product */
1643: /*-------------------------------------------------------------*/
1644: /* put the value into Co=(p->B)^T*A (off-diagonal part, send to others) */
1645: pnz = po->i[i+1] - po->i[i];
1646: poJ = po->j + po->i[i];
1647: pA = po->a + po->i[i];
1648: for (j=0; j<pnz; j++) {
1649: row = poJ[j];
1650: cj = coj + coi[row];
1651: ca = coa + coi[row];
1652: /* perform sparse axpy */
1653: nexta = 0;
1654: valtmp = pA[j];
1655: for (k=0; nexta<anz; k++) {
1656: if (cj[k] == adj[nexta]) {
1657: ca[k] += valtmp*ada[nexta];
1658: nexta++;
1659: }
1660: }
1661: PetscLogFlops(2.0*anz);
1662: }
1664: /* put the value into Cd (diagonal part) */
1665: pnz = pd->i[i+1] - pd->i[i];
1666: pdJ = pd->j + pd->i[i];
1667: pA = pd->a + pd->i[i];
1668: for (j=0; j<pnz; j++) {
1669: row = pdJ[j];
1670: cj = bj + bi[row];
1671: ca = ba + bi[row];
1672: /* perform sparse axpy */
1673: nexta = 0;
1674: valtmp = pA[j];
1675: for (k=0; nexta<anz; k++) {
1676: if (cj[k] == adj[nexta]) {
1677: ca[k] += valtmp*ada[nexta];
1678: nexta++;
1679: }
1680: }
1681: PetscLogFlops(2.0*anz);
1682: }
1683: }
1685: /* 3) send and recv matrix values coa */
1686: /*------------------------------------*/
1687: buf_ri = merge->buf_ri;
1688: buf_rj = merge->buf_rj;
1689: len_s = merge->len_s;
1690: PetscCommGetNewTag(comm,&taga);
1691: PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);
1693: PetscMalloc2(merge->nsend+1,&s_waits,size,&status);
1694: for (proc=0,k=0; proc<size; proc++) {
1695: if (!len_s[proc]) continue;
1696: i = merge->owners_co[proc];
1697: MPI_Isend(coa+coi[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);
1698: k++;
1699: }
1700: if (merge->nrecv) {MPI_Waitall(merge->nrecv,r_waits,status);}
1701: if (merge->nsend) {MPI_Waitall(merge->nsend,s_waits,status);}
1703: PetscFree2(s_waits,status);
1704: PetscFree(r_waits);
1705: PetscFree(coa);
1707: /* 4) insert local Cseq and received values into Cmpi */
1708: /*----------------------------------------------------*/
1709: PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextci);
1710: for (k=0; k<merge->nrecv; k++) {
1711: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
1712: nrows = *(buf_ri_k[k]);
1713: nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */
1714: nextci[k] = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure */
1715: }
1717: for (i=0; i<cm; i++) {
1718: row = owners[rank] + i; /* global row index of C_seq */
1719: bj_i = bj + bi[i]; /* col indices of the i-th row of C */
1720: ba_i = ba + bi[i];
1721: bnz = bi[i+1] - bi[i];
1722: /* add received vals into ba */
1723: for (k=0; k<merge->nrecv; k++) { /* k-th received message */
1724: /* i-th row */
1725: if (i == *nextrow[k]) {
1726: cnz = *(nextci[k]+1) - *nextci[k];
1727: cj = buf_rj[k] + *(nextci[k]);
1728: ca = abuf_r[k] + *(nextci[k]);
1729: nextcj = 0;
1730: for (j=0; nextcj<cnz; j++) {
1731: if (bj_i[j] == cj[nextcj]) { /* bcol == ccol */
1732: ba_i[j] += ca[nextcj++];
1733: }
1734: }
1735: nextrow[k]++; nextci[k]++;
1736: PetscLogFlops(2.0*cnz);
1737: }
1738: }
1739: MatSetValues(C,1,&row,bnz,bj_i,ba_i,INSERT_VALUES);
1740: }
1741: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
1742: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
1744: PetscFree(ba);
1745: PetscFree(abuf_r[0]);
1746: PetscFree(abuf_r);
1747: PetscFree3(buf_ri_k,nextrow,nextci);
1749: if (ptap->freestruct) {
1750: MatFreeIntermediateDataStructures(C);
1751: }
1752: return(0);
1753: }
1755: PetscErrorCode MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ(Mat P,Mat A,PetscReal fill,Mat *C)
1756: {
1757: PetscErrorCode ierr;
1758: Mat Cmpi,A_loc,POt,PDt;
1759: Mat_APMPI *ptap;
1760: PetscFreeSpaceList free_space=NULL,current_space=NULL;
1761: Mat_MPIAIJ *p=(Mat_MPIAIJ*)P->data,*a=(Mat_MPIAIJ*)A->data,*c;
1762: PetscInt *pdti,*pdtj,*poti,*potj,*ptJ;
1763: PetscInt nnz;
1764: PetscInt *lnk,*owners_co,*coi,*coj,i,k,pnz,row;
1765: PetscInt am =A->rmap->n,pn=P->cmap->n;
1766: MPI_Comm comm;
1767: PetscMPIInt size,rank,tagi,tagj,*len_si,*len_s,*len_ri;
1768: PetscInt **buf_rj,**buf_ri,**buf_ri_k;
1769: PetscInt len,proc,*dnz,*onz,*owners;
1770: PetscInt nzi,*bi,*bj;
1771: PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextci;
1772: MPI_Request *swaits,*rwaits;
1773: MPI_Status *sstatus,rstatus;
1774: Mat_Merge_SeqsToMPI *merge;
1775: PetscInt *ai,*aj,*Jptr,anz,*prmap=p->garray,pon,nspacedouble=0,j;
1776: PetscReal afill =1.0,afill_tmp;
1777: PetscInt rstart = P->cmap->rstart,rmax,aN=A->cmap->N,Armax;
1778: PetscScalar *vals;
1779: Mat_SeqAIJ *a_loc,*pdt,*pot;
1780: PetscTable ta;
1781: MatType mtype;
1784: PetscObjectGetComm((PetscObject)A,&comm);
1785: /* check if matrix local sizes are compatible */
1786: 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);
1788: MPI_Comm_size(comm,&size);
1789: MPI_Comm_rank(comm,&rank);
1791: /* create struct Mat_APMPI and attached it to C later */
1792: PetscNew(&ptap);
1794: /* get A_loc by taking all local rows of A */
1795: MatMPIAIJGetLocalMat(A,MAT_INITIAL_MATRIX,&A_loc);
1797: ptap->A_loc = A_loc;
1798: a_loc = (Mat_SeqAIJ*)(A_loc)->data;
1799: ai = a_loc->i;
1800: aj = a_loc->j;
1802: /* determine symbolic Co=(p->B)^T*A - send to others */
1803: /*----------------------------------------------------*/
1804: MatTransposeSymbolic_SeqAIJ(p->A,&PDt);
1805: pdt = (Mat_SeqAIJ*)PDt->data;
1806: pdti = pdt->i; pdtj = pdt->j;
1808: MatTransposeSymbolic_SeqAIJ(p->B,&POt);
1809: pot = (Mat_SeqAIJ*)POt->data;
1810: poti = pot->i; potj = pot->j;
1812: /* then, compute symbolic Co = (p->B)^T*A */
1813: pon = (p->B)->cmap->n; /* total num of rows to be sent to other processors
1814: >= (num of nonzero rows of C_seq) - pn */
1815: PetscMalloc1(pon+1,&coi);
1816: coi[0] = 0;
1818: /* set initial free space to be fill*(nnz(p->B) + nnz(A)) */
1819: nnz = PetscRealIntMultTruncate(fill,PetscIntSumTruncate(poti[pon],ai[am]));
1820: PetscFreeSpaceGet(nnz,&free_space);
1821: current_space = free_space;
1823: /* create and initialize a linked list */
1824: PetscTableCreate(A->cmap->n + a->B->cmap->N,aN,&ta);
1825: MatRowMergeMax_SeqAIJ(a_loc,am,ta);
1826: PetscTableGetCount(ta,&Armax);
1828: PetscLLCondensedCreate_Scalable(Armax,&lnk);
1830: for (i=0; i<pon; i++) {
1831: pnz = poti[i+1] - poti[i];
1832: ptJ = potj + poti[i];
1833: for (j=0; j<pnz; j++) {
1834: row = ptJ[j]; /* row of A_loc == col of Pot */
1835: anz = ai[row+1] - ai[row];
1836: Jptr = aj + ai[row];
1837: /* add non-zero cols of AP into the sorted linked list lnk */
1838: PetscLLCondensedAddSorted_Scalable(anz,Jptr,lnk);
1839: }
1840: nnz = lnk[0];
1842: /* If free space is not available, double the total space in the list */
1843: if (current_space->local_remaining<nnz) {
1844: PetscFreeSpaceGet(PetscIntSumTruncate(nnz,current_space->total_array_size),¤t_space);
1845: nspacedouble++;
1846: }
1848: /* Copy data into free space, and zero out denserows */
1849: PetscLLCondensedClean_Scalable(nnz,current_space->array,lnk);
1851: current_space->array += nnz;
1852: current_space->local_used += nnz;
1853: current_space->local_remaining -= nnz;
1855: coi[i+1] = coi[i] + nnz;
1856: }
1858: PetscMalloc1(coi[pon]+1,&coj);
1859: PetscFreeSpaceContiguous(&free_space,coj);
1860: PetscLLCondensedDestroy_Scalable(lnk); /* must destroy to get a new one for C */
1862: afill_tmp = (PetscReal)coi[pon]/(poti[pon] + ai[am]+1);
1863: if (afill_tmp > afill) afill = afill_tmp;
1865: /* send j-array (coj) of Co to other processors */
1866: /*----------------------------------------------*/
1867: /* determine row ownership */
1868: PetscNew(&merge);
1869: PetscLayoutCreate(comm,&merge->rowmap);
1871: merge->rowmap->n = pn;
1872: merge->rowmap->bs = 1;
1874: PetscLayoutSetUp(merge->rowmap);
1875: owners = merge->rowmap->range;
1877: /* determine the number of messages to send, their lengths */
1878: PetscCalloc1(size,&len_si);
1879: PetscMalloc1(size,&merge->len_s);
1881: len_s = merge->len_s;
1882: merge->nsend = 0;
1884: PetscMalloc1(size+2,&owners_co);
1885: PetscMemzero(len_s,size*sizeof(PetscMPIInt));
1887: proc = 0;
1888: for (i=0; i<pon; i++) {
1889: while (prmap[i] >= owners[proc+1]) proc++;
1890: len_si[proc]++; /* num of rows in Co to be sent to [proc] */
1891: len_s[proc] += coi[i+1] - coi[i];
1892: }
1894: len = 0; /* max length of buf_si[] */
1895: owners_co[0] = 0;
1896: for (proc=0; proc<size; proc++) {
1897: owners_co[proc+1] = owners_co[proc] + len_si[proc];
1898: if (len_si[proc]) {
1899: merge->nsend++;
1900: len_si[proc] = 2*(len_si[proc] + 1);
1901: len += len_si[proc];
1902: }
1903: }
1905: /* determine the number and length of messages to receive for coi and coj */
1906: PetscGatherNumberOfMessages(comm,NULL,len_s,&merge->nrecv);
1907: PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);
1909: /* post the Irecv and Isend of coj */
1910: PetscCommGetNewTag(comm,&tagj);
1911: PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rwaits);
1912: PetscMalloc1(merge->nsend+1,&swaits);
1913: for (proc=0, k=0; proc<size; proc++) {
1914: if (!len_s[proc]) continue;
1915: i = owners_co[proc];
1916: MPI_Isend(coj+coi[i],len_s[proc],MPIU_INT,proc,tagj,comm,swaits+k);
1917: k++;
1918: }
1920: /* receives and sends of coj are complete */
1921: PetscMalloc1(size,&sstatus);
1922: for (i=0; i<merge->nrecv; i++) {
1923: PetscMPIInt icompleted;
1924: MPI_Waitany(merge->nrecv,rwaits,&icompleted,&rstatus);
1925: }
1926: PetscFree(rwaits);
1927: if (merge->nsend) {MPI_Waitall(merge->nsend,swaits,sstatus);}
1929: /* add received column indices into table to update Armax */
1930: /* Armax can be as large as aN if a P[row,:] is dense, see src/ksp/ksp/examples/tutorials/ex56.c! */
1931: for (k=0; k<merge->nrecv; k++) {/* k-th received message */
1932: Jptr = buf_rj[k];
1933: for (j=0; j<merge->len_r[k]; j++) {
1934: PetscTableAdd(ta,*(Jptr+j)+1,1,INSERT_VALUES);
1935: }
1936: }
1937: PetscTableGetCount(ta,&Armax);
1938: /* 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); */
1940: /* send and recv coi */
1941: /*-------------------*/
1942: PetscCommGetNewTag(comm,&tagi);
1943: PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&rwaits);
1944: PetscMalloc1(len+1,&buf_s);
1945: buf_si = buf_s; /* points to the beginning of k-th msg to be sent */
1946: for (proc=0,k=0; proc<size; proc++) {
1947: if (!len_s[proc]) continue;
1948: /* form outgoing message for i-structure:
1949: buf_si[0]: nrows to be sent
1950: [1:nrows]: row index (global)
1951: [nrows+1:2*nrows+1]: i-structure index
1952: */
1953: /*-------------------------------------------*/
1954: nrows = len_si[proc]/2 - 1;
1955: buf_si_i = buf_si + nrows+1;
1956: buf_si[0] = nrows;
1957: buf_si_i[0] = 0;
1958: nrows = 0;
1959: for (i=owners_co[proc]; i<owners_co[proc+1]; i++) {
1960: nzi = coi[i+1] - coi[i];
1961: buf_si_i[nrows+1] = buf_si_i[nrows] + nzi; /* i-structure */
1962: buf_si[nrows+1] = prmap[i] -owners[proc]; /* local row index */
1963: nrows++;
1964: }
1965: MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,swaits+k);
1966: k++;
1967: buf_si += len_si[proc];
1968: }
1969: i = merge->nrecv;
1970: while (i--) {
1971: PetscMPIInt icompleted;
1972: MPI_Waitany(merge->nrecv,rwaits,&icompleted,&rstatus);
1973: }
1974: PetscFree(rwaits);
1975: if (merge->nsend) {MPI_Waitall(merge->nsend,swaits,sstatus);}
1976: PetscFree(len_si);
1977: PetscFree(len_ri);
1978: PetscFree(swaits);
1979: PetscFree(sstatus);
1980: PetscFree(buf_s);
1982: /* compute the local portion of C (mpi mat) */
1983: /*------------------------------------------*/
1984: /* allocate bi array and free space for accumulating nonzero column info */
1985: PetscMalloc1(pn+1,&bi);
1986: bi[0] = 0;
1988: /* set initial free space to be fill*(nnz(P) + nnz(AP)) */
1989: nnz = PetscRealIntMultTruncate(fill,PetscIntSumTruncate(pdti[pn],PetscIntSumTruncate(poti[pon],ai[am])));
1990: PetscFreeSpaceGet(nnz,&free_space);
1991: current_space = free_space;
1993: PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextci);
1994: for (k=0; k<merge->nrecv; k++) {
1995: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
1996: nrows = *buf_ri_k[k];
1997: nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */
1998: nextci[k] = buf_ri_k[k] + (nrows + 1); /* points to the next i-structure of k-th recieved i-structure */
1999: }
2001: PetscLLCondensedCreate_Scalable(Armax,&lnk);
2002: MatPreallocateInitialize(comm,pn,A->cmap->n,dnz,onz);
2003: rmax = 0;
2004: for (i=0; i<pn; i++) {
2005: /* add pdt[i,:]*AP into lnk */
2006: pnz = pdti[i+1] - pdti[i];
2007: ptJ = pdtj + pdti[i];
2008: for (j=0; j<pnz; j++) {
2009: row = ptJ[j]; /* row of AP == col of Pt */
2010: anz = ai[row+1] - ai[row];
2011: Jptr = aj + ai[row];
2012: /* add non-zero cols of AP into the sorted linked list lnk */
2013: PetscLLCondensedAddSorted_Scalable(anz,Jptr,lnk);
2014: }
2016: /* add received col data into lnk */
2017: for (k=0; k<merge->nrecv; k++) { /* k-th received message */
2018: if (i == *nextrow[k]) { /* i-th row */
2019: nzi = *(nextci[k]+1) - *nextci[k];
2020: Jptr = buf_rj[k] + *nextci[k];
2021: PetscLLCondensedAddSorted_Scalable(nzi,Jptr,lnk);
2022: nextrow[k]++; nextci[k]++;
2023: }
2024: }
2025: nnz = lnk[0];
2027: /* if free space is not available, make more free space */
2028: if (current_space->local_remaining<nnz) {
2029: PetscFreeSpaceGet(PetscIntSumTruncate(nnz,current_space->total_array_size),¤t_space);
2030: nspacedouble++;
2031: }
2032: /* copy data into free space, then initialize lnk */
2033: PetscLLCondensedClean_Scalable(nnz,current_space->array,lnk);
2034: MatPreallocateSet(i+owners[rank],nnz,current_space->array,dnz,onz);
2036: current_space->array += nnz;
2037: current_space->local_used += nnz;
2038: current_space->local_remaining -= nnz;
2040: bi[i+1] = bi[i] + nnz;
2041: if (nnz > rmax) rmax = nnz;
2042: }
2043: PetscFree3(buf_ri_k,nextrow,nextci);
2045: PetscMalloc1(bi[pn]+1,&bj);
2046: PetscFreeSpaceContiguous(&free_space,bj);
2047: afill_tmp = (PetscReal)bi[pn]/(pdti[pn] + poti[pon] + ai[am]+1);
2048: if (afill_tmp > afill) afill = afill_tmp;
2049: PetscLLCondensedDestroy_Scalable(lnk);
2050: PetscTableDestroy(&ta);
2052: MatDestroy(&POt);
2053: MatDestroy(&PDt);
2055: /* create symbolic parallel matrix Cmpi - why cannot be assembled in Numeric part */
2056: /*----------------------------------------------------------------------------------*/
2057: PetscCalloc1(rmax+1,&vals);
2059: MatCreate(comm,&Cmpi);
2060: MatSetSizes(Cmpi,pn,A->cmap->n,PETSC_DETERMINE,PETSC_DETERMINE);
2061: MatSetBlockSizes(Cmpi,PetscAbs(P->cmap->bs),PetscAbs(A->cmap->bs));
2062: MatGetType(A,&mtype);
2063: MatSetType(Cmpi,mtype);
2064: MatMPIAIJSetPreallocation(Cmpi,0,dnz,0,onz);
2065: MatPreallocateFinalize(dnz,onz);
2066: MatSetBlockSize(Cmpi,1);
2067: for (i=0; i<pn; i++) {
2068: row = i + rstart;
2069: nnz = bi[i+1] - bi[i];
2070: Jptr = bj + bi[i];
2071: MatSetValues(Cmpi,1,&row,nnz,Jptr,vals,INSERT_VALUES);
2072: }
2073: MatAssemblyBegin(Cmpi,MAT_FINAL_ASSEMBLY);
2074: MatAssemblyEnd(Cmpi,MAT_FINAL_ASSEMBLY);
2075: PetscFree(vals);
2077: merge->bi = bi;
2078: merge->bj = bj;
2079: merge->coi = coi;
2080: merge->coj = coj;
2081: merge->buf_ri = buf_ri;
2082: merge->buf_rj = buf_rj;
2083: merge->owners_co = owners_co;
2085: /* attach the supporting struct to Cmpi for reuse */
2086: c = (Mat_MPIAIJ*)Cmpi->data;
2088: c->ap = ptap;
2089: ptap->api = NULL;
2090: ptap->apj = NULL;
2091: ptap->merge = merge;
2092: ptap->apa = NULL;
2093: ptap->destroy = Cmpi->ops->destroy;
2094: ptap->duplicate = Cmpi->ops->duplicate;
2096: Cmpi->ops->mattransposemultnumeric = MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ;
2097: Cmpi->ops->destroy = MatDestroy_MPIAIJ_PtAP;
2098: Cmpi->ops->freeintermediatedatastructures = MatFreeIntermediateDataStructures_MPIAIJ_AP;
2100: *C = Cmpi;
2101: #if defined(PETSC_USE_INFO)
2102: if (bi[pn] != 0) {
2103: PetscInfo3(Cmpi,"Reallocs %D; Fill ratio: given %g needed %g.\n",nspacedouble,(double)fill,(double)afill);
2104: PetscInfo1(Cmpi,"Use MatTransposeMatMult(A,B,MatReuse,%g,&C) for best performance.\n",(double)afill);
2105: } else {
2106: PetscInfo(Cmpi,"Empty matrix product\n");
2107: }
2108: #endif
2109: return(0);
2110: }