Actual source code: mpiaij.c

  1: #include <../src/mat/impls/aij/mpi/mpiaij.h>
  2: #include <petsc/private/vecimpl.h>
  3: #include <petsc/private/sfimpl.h>
  4: #include <petsc/private/isimpl.h>
  5: #include <petscblaslapack.h>
  6: #include <petscsf.h>
  7: #include <petsc/private/hashmapi.h>

  9: /*MC
 10:    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.

 12:    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
 13:    and MATMPIAIJ otherwise.  As a result, for single process communicators,
 14:   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation() is supported
 15:   for communicators controlling multiple processes.  It is recommended that you call both of
 16:   the above preallocation routines for simplicity.

 18:    Options Database Keys:
 19: . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()

 21:   Developer Notes:
 22:     Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when
 23:    enough exist.

 25:   Level: beginner

 27: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ, MATMPIAIJ
 28: M*/

 30: /*MC
 31:    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.

 33:    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
 34:    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
 35:    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
 36:   for communicators controlling multiple processes.  It is recommended that you call both of
 37:   the above preallocation routines for simplicity.

 39:    Options Database Keys:
 40: . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()

 42:   Level: beginner

 44: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
 45: M*/

 47: static PetscErrorCode MatBindToCPU_MPIAIJ(Mat A,PetscBool flg)
 48: {
 49:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

 53: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
 54:   A->boundtocpu = flg;
 55: #endif
 56:   if (a->A) {
 57:     MatBindToCPU(a->A,flg);
 58:   }
 59:   if (a->B) {
 60:     MatBindToCPU(a->B,flg);
 61:   }
 62:   return(0);
 63: }

 65: PetscErrorCode MatSetBlockSizes_MPIAIJ(Mat M, PetscInt rbs, PetscInt cbs)
 66: {
 68:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)M->data;

 71:   if (mat->A) {
 72:     MatSetBlockSizes(mat->A,rbs,cbs);
 73:     MatSetBlockSizes(mat->B,rbs,1);
 74:   }
 75:   return(0);
 76: }

 78: PetscErrorCode MatFindNonzeroRows_MPIAIJ(Mat M,IS *keptrows)
 79: {
 80:   PetscErrorCode  ierr;
 81:   Mat_MPIAIJ      *mat = (Mat_MPIAIJ*)M->data;
 82:   Mat_SeqAIJ      *a   = (Mat_SeqAIJ*)mat->A->data;
 83:   Mat_SeqAIJ      *b   = (Mat_SeqAIJ*)mat->B->data;
 84:   const PetscInt  *ia,*ib;
 85:   const MatScalar *aa,*bb,*aav,*bav;
 86:   PetscInt        na,nb,i,j,*rows,cnt=0,n0rows;
 87:   PetscInt        m = M->rmap->n,rstart = M->rmap->rstart;

 90:   *keptrows = NULL;

 92:   ia   = a->i;
 93:   ib   = b->i;
 94:   MatSeqAIJGetArrayRead(mat->A,&aav);
 95:   MatSeqAIJGetArrayRead(mat->B,&bav);
 96:   for (i=0; i<m; i++) {
 97:     na = ia[i+1] - ia[i];
 98:     nb = ib[i+1] - ib[i];
 99:     if (!na && !nb) {
100:       cnt++;
101:       goto ok1;
102:     }
103:     aa = aav + ia[i];
104:     for (j=0; j<na; j++) {
105:       if (aa[j] != 0.0) goto ok1;
106:     }
107:     bb = bav + ib[i];
108:     for (j=0; j <nb; j++) {
109:       if (bb[j] != 0.0) goto ok1;
110:     }
111:     cnt++;
112: ok1:;
113:   }
114:   MPIU_Allreduce(&cnt,&n0rows,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)M));
115:   if (!n0rows) {
116:     MatSeqAIJRestoreArrayRead(mat->A,&aav);
117:     MatSeqAIJRestoreArrayRead(mat->B,&bav);
118:     return(0);
119:   }
120:   PetscMalloc1(M->rmap->n-cnt,&rows);
121:   cnt  = 0;
122:   for (i=0; i<m; i++) {
123:     na = ia[i+1] - ia[i];
124:     nb = ib[i+1] - ib[i];
125:     if (!na && !nb) continue;
126:     aa = aav + ia[i];
127:     for (j=0; j<na;j++) {
128:       if (aa[j] != 0.0) {
129:         rows[cnt++] = rstart + i;
130:         goto ok2;
131:       }
132:     }
133:     bb = bav + ib[i];
134:     for (j=0; j<nb; j++) {
135:       if (bb[j] != 0.0) {
136:         rows[cnt++] = rstart + i;
137:         goto ok2;
138:       }
139:     }
140: ok2:;
141:   }
142:   ISCreateGeneral(PetscObjectComm((PetscObject)M),cnt,rows,PETSC_OWN_POINTER,keptrows);
143:   MatSeqAIJRestoreArrayRead(mat->A,&aav);
144:   MatSeqAIJRestoreArrayRead(mat->B,&bav);
145:   return(0);
146: }

148: PetscErrorCode  MatDiagonalSet_MPIAIJ(Mat Y,Vec D,InsertMode is)
149: {
150:   PetscErrorCode    ierr;
151:   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*) Y->data;
152:   PetscBool         cong;

155:   MatHasCongruentLayouts(Y,&cong);
156:   if (Y->assembled && cong) {
157:     MatDiagonalSet(aij->A,D,is);
158:   } else {
159:     MatDiagonalSet_Default(Y,D,is);
160:   }
161:   return(0);
162: }

164: PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows)
165: {
166:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)M->data;
168:   PetscInt       i,rstart,nrows,*rows;

171:   *zrows = NULL;
172:   MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);
173:   MatGetOwnershipRange(M,&rstart,NULL);
174:   for (i=0; i<nrows; i++) rows[i] += rstart;
175:   ISCreateGeneral(PetscObjectComm((PetscObject)M),nrows,rows,PETSC_OWN_POINTER,zrows);
176:   return(0);
177: }

179: PetscErrorCode MatGetColumnReductions_MPIAIJ(Mat A,PetscInt type,PetscReal *reductions)
180: {
181:   PetscErrorCode    ierr;
182:   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)A->data;
183:   PetscInt          i,m,n,*garray = aij->garray;
184:   Mat_SeqAIJ        *a_aij = (Mat_SeqAIJ*) aij->A->data;
185:   Mat_SeqAIJ        *b_aij = (Mat_SeqAIJ*) aij->B->data;
186:   PetscReal         *work;
187:   const PetscScalar *dummy;

190:   MatGetSize(A,&m,&n);
191:   PetscCalloc1(n,&work);
192:   MatSeqAIJGetArrayRead(aij->A,&dummy);
193:   MatSeqAIJRestoreArrayRead(aij->A,&dummy);
194:   MatSeqAIJGetArrayRead(aij->B,&dummy);
195:   MatSeqAIJRestoreArrayRead(aij->B,&dummy);
196:   if (type == NORM_2) {
197:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
198:       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]*a_aij->a[i]);
199:     }
200:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
201:       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]*b_aij->a[i]);
202:     }
203:   } else if (type == NORM_1) {
204:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
205:       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]);
206:     }
207:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
208:       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]);
209:     }
210:   } else if (type == NORM_INFINITY) {
211:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
212:       work[A->cmap->rstart + a_aij->j[i]] = PetscMax(PetscAbsScalar(a_aij->a[i]), work[A->cmap->rstart + a_aij->j[i]]);
213:     }
214:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
215:       work[garray[b_aij->j[i]]] = PetscMax(PetscAbsScalar(b_aij->a[i]),work[garray[b_aij->j[i]]]);
216:     }
217:   } else if (type == REDUCTION_SUM_REALPART || type == REDUCTION_MEAN_REALPART) {
218:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
219:       work[A->cmap->rstart + a_aij->j[i]] += PetscRealPart(a_aij->a[i]);
220:     }
221:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
222:       work[garray[b_aij->j[i]]] += PetscRealPart(b_aij->a[i]);
223:     }
224:   } else if (type == REDUCTION_SUM_IMAGINARYPART || type == REDUCTION_MEAN_IMAGINARYPART) {
225:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
226:       work[A->cmap->rstart + a_aij->j[i]] += PetscImaginaryPart(a_aij->a[i]);
227:     }
228:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
229:       work[garray[b_aij->j[i]]] += PetscImaginaryPart(b_aij->a[i]);
230:     }
231:   } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown reduction type");
232:   if (type == NORM_INFINITY) {
233:     MPIU_Allreduce(work,reductions,n,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));
234:   } else {
235:     MPIU_Allreduce(work,reductions,n,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));
236:   }
237:   PetscFree(work);
238:   if (type == NORM_2) {
239:     for (i=0; i<n; i++) reductions[i] = PetscSqrtReal(reductions[i]);
240:   } else if (type == REDUCTION_MEAN_REALPART || type == REDUCTION_MEAN_IMAGINARYPART) {
241:     for (i=0; i<n; i++) reductions[i] /= m;
242:   }
243:   return(0);
244: }

246: PetscErrorCode MatFindOffBlockDiagonalEntries_MPIAIJ(Mat A,IS *is)
247: {
248:   Mat_MPIAIJ      *a  = (Mat_MPIAIJ*)A->data;
249:   IS              sis,gis;
250:   PetscErrorCode  ierr;
251:   const PetscInt  *isis,*igis;
252:   PetscInt        n,*iis,nsis,ngis,rstart,i;

255:   MatFindOffBlockDiagonalEntries(a->A,&sis);
256:   MatFindNonzeroRows(a->B,&gis);
257:   ISGetSize(gis,&ngis);
258:   ISGetSize(sis,&nsis);
259:   ISGetIndices(sis,&isis);
260:   ISGetIndices(gis,&igis);

262:   PetscMalloc1(ngis+nsis,&iis);
263:   PetscArraycpy(iis,igis,ngis);
264:   PetscArraycpy(iis+ngis,isis,nsis);
265:   n    = ngis + nsis;
266:   PetscSortRemoveDupsInt(&n,iis);
267:   MatGetOwnershipRange(A,&rstart,NULL);
268:   for (i=0; i<n; i++) iis[i] += rstart;
269:   ISCreateGeneral(PetscObjectComm((PetscObject)A),n,iis,PETSC_OWN_POINTER,is);

271:   ISRestoreIndices(sis,&isis);
272:   ISRestoreIndices(gis,&igis);
273:   ISDestroy(&sis);
274:   ISDestroy(&gis);
275:   return(0);
276: }

278: /*
279:   Local utility routine that creates a mapping from the global column
280: number to the local number in the off-diagonal part of the local
281: storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
282: a slightly higher hash table cost; without it it is not scalable (each processor
283: has an order N integer array but is fast to access.
284: */
285: PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat mat)
286: {
287:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
289:   PetscInt       n = aij->B->cmap->n,i;

292:   if (n && !aij->garray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPIAIJ Matrix was assembled but is missing garray");
293: #if defined(PETSC_USE_CTABLE)
294:   PetscTableCreate(n,mat->cmap->N+1,&aij->colmap);
295:   for (i=0; i<n; i++) {
296:     PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1,INSERT_VALUES);
297:   }
298: #else
299:   PetscCalloc1(mat->cmap->N+1,&aij->colmap);
300:   PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N+1)*sizeof(PetscInt));
301:   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
302: #endif
303:   return(0);
304: }

306: #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv,orow,ocol)     \
307: { \
308:     if (col <= lastcol1)  low1 = 0;     \
309:     else                 high1 = nrow1; \
310:     lastcol1 = col;\
311:     while (high1-low1 > 5) { \
312:       t = (low1+high1)/2; \
313:       if (rp1[t] > col) high1 = t; \
314:       else              low1  = t; \
315:     } \
316:       for (_i=low1; _i<high1; _i++) { \
317:         if (rp1[_i] > col) break; \
318:         if (rp1[_i] == col) { \
319:           if (addv == ADD_VALUES) { \
320:             ap1[_i] += value;   \
321:             /* Not sure LogFlops will slow dow the code or not */ \
322:             (void)PetscLogFlops(1.0);   \
323:            } \
324:           else                    ap1[_i] = value; \
325:           inserted = PETSC_TRUE; \
326:           goto a_noinsert; \
327:         } \
328:       }  \
329:       if (value == 0.0 && ignorezeroentries && row != col) {low1 = 0; high1 = nrow1;goto a_noinsert;} \
330:       if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;}                \
331:       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at global row/column (%D, %D) into matrix", orow, ocol); \
332:       MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \
333:       N = nrow1++ - 1; a->nz++; high1++; \
334:       /* shift up all the later entries in this row */ \
335:       PetscArraymove(rp1+_i+1,rp1+_i,N-_i+1);\
336:       PetscArraymove(ap1+_i+1,ap1+_i,N-_i+1);\
337:       rp1[_i] = col;  \
338:       ap1[_i] = value;  \
339:       A->nonzerostate++;\
340:       a_noinsert: ; \
341:       ailen[row] = nrow1; \
342: }

344: #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv,orow,ocol) \
345:   { \
346:     if (col <= lastcol2) low2 = 0;                        \
347:     else high2 = nrow2;                                   \
348:     lastcol2 = col;                                       \
349:     while (high2-low2 > 5) {                              \
350:       t = (low2+high2)/2;                                 \
351:       if (rp2[t] > col) high2 = t;                        \
352:       else             low2  = t;                         \
353:     }                                                     \
354:     for (_i=low2; _i<high2; _i++) {                       \
355:       if (rp2[_i] > col) break;                           \
356:       if (rp2[_i] == col) {                               \
357:         if (addv == ADD_VALUES) {                         \
358:           ap2[_i] += value;                               \
359:           (void)PetscLogFlops(1.0);                       \
360:         }                                                 \
361:         else                    ap2[_i] = value;          \
362:         inserted = PETSC_TRUE;                            \
363:         goto b_noinsert;                                  \
364:       }                                                   \
365:     }                                                     \
366:     if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \
367:     if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;}                        \
368:     if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at global row/column (%D, %D) into matrix", orow, ocol); \
369:     MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \
370:     N = nrow2++ - 1; b->nz++; high2++;                    \
371:     /* shift up all the later entries in this row */      \
372:     PetscArraymove(rp2+_i+1,rp2+_i,N-_i+1);\
373:     PetscArraymove(ap2+_i+1,ap2+_i,N-_i+1);\
374:     rp2[_i] = col;                                        \
375:     ap2[_i] = value;                                      \
376:     B->nonzerostate++;                                    \
377:     b_noinsert: ;                                         \
378:     bilen[row] = nrow2;                                   \
379:   }

381: PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[])
382: {
383:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)A->data;
384:   Mat_SeqAIJ     *a   = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data;
386:   PetscInt       l,*garray = mat->garray,diag;

389:   /* code only works for square matrices A */

391:   /* find size of row to the left of the diagonal part */
392:   MatGetOwnershipRange(A,&diag,NULL);
393:   row  = row - diag;
394:   for (l=0; l<b->i[row+1]-b->i[row]; l++) {
395:     if (garray[b->j[b->i[row]+l]] > diag) break;
396:   }
397:   PetscArraycpy(b->a+b->i[row],v,l);

399:   /* diagonal part */
400:   PetscArraycpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row]));

402:   /* right of diagonal part */
403:   PetscArraycpy(b->a+b->i[row]+l,v+l+a->i[row+1]-a->i[row],b->i[row+1]-b->i[row]-l);
404: #if defined(PETSC_HAVE_DEVICE)
405:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && (l || (a->i[row+1]-a->i[row]) || (b->i[row+1]-b->i[row]-l))) A->offloadmask = PETSC_OFFLOAD_CPU;
406: #endif
407:   return(0);
408: }

410: PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
411: {
412:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
413:   PetscScalar    value = 0.0;
415:   PetscInt       i,j,rstart  = mat->rmap->rstart,rend = mat->rmap->rend;
416:   PetscInt       cstart      = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
417:   PetscBool      roworiented = aij->roworiented;

419:   /* Some Variables required in the macro */
420:   Mat        A                    = aij->A;
421:   Mat_SeqAIJ *a                   = (Mat_SeqAIJ*)A->data;
422:   PetscInt   *aimax               = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
423:   PetscBool  ignorezeroentries    = a->ignorezeroentries;
424:   Mat        B                    = aij->B;
425:   Mat_SeqAIJ *b                   = (Mat_SeqAIJ*)B->data;
426:   PetscInt   *bimax               = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
427:   MatScalar  *aa,*ba;
428:   /* This variable below is only for the PETSC_HAVE_VIENNACL or PETSC_HAVE_CUDA cases, but we define it in all cases because we
429:    * cannot use "#if defined" inside a macro. */
430:   PETSC_UNUSED PetscBool inserted = PETSC_FALSE;

432:   PetscInt  *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
433:   PetscInt  nonew;
434:   MatScalar *ap1,*ap2;

437: #if defined(PETSC_HAVE_DEVICE)
438:   if (A->offloadmask == PETSC_OFFLOAD_GPU) {
439:     const PetscScalar *dummy;
440:     MatSeqAIJGetArrayRead(A,&dummy);
441:     MatSeqAIJRestoreArrayRead(A,&dummy);
442:   }
443:   if (B->offloadmask == PETSC_OFFLOAD_GPU) {
444:     const PetscScalar *dummy;
445:     MatSeqAIJGetArrayRead(B,&dummy);
446:     MatSeqAIJRestoreArrayRead(B,&dummy);
447:   }
448: #endif
449:   aa = a->a;
450:   ba = b->a;
451:   for (i=0; i<m; i++) {
452:     if (im[i] < 0) continue;
453:     if (PetscUnlikely(im[i] >= mat->rmap->N)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
454:     if (im[i] >= rstart && im[i] < rend) {
455:       row      = im[i] - rstart;
456:       lastcol1 = -1;
457:       rp1      = aj + ai[row];
458:       ap1      = aa + ai[row];
459:       rmax1    = aimax[row];
460:       nrow1    = ailen[row];
461:       low1     = 0;
462:       high1    = nrow1;
463:       lastcol2 = -1;
464:       rp2      = bj + bi[row];
465:       ap2      = ba + bi[row];
466:       rmax2    = bimax[row];
467:       nrow2    = bilen[row];
468:       low2     = 0;
469:       high2    = nrow2;

471:       for (j=0; j<n; j++) {
472:         if (v)  value = roworiented ? v[i*n+j] : v[i+j*m];
473:         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES) && im[i] != in[j]) continue;
474:         if (in[j] >= cstart && in[j] < cend) {
475:           col   = in[j] - cstart;
476:           nonew = a->nonew;
477:           MatSetValues_SeqAIJ_A_Private(row,col,value,addv,im[i],in[j]);
478: #if defined(PETSC_HAVE_DEVICE)
479:           if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) A->offloadmask = PETSC_OFFLOAD_CPU;
480: #endif
481:         } else if (in[j] < 0) continue;
482:         else if (in[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap->N-1);
483:         else {
484:           if (mat->was_assembled) {
485:             if (!aij->colmap) {
486:               MatCreateColmap_MPIAIJ_Private(mat);
487:             }
488: #if defined(PETSC_USE_CTABLE)
489:             PetscTableFind(aij->colmap,in[j]+1,&col);
490:             col--;
491: #else
492:             col = aij->colmap[in[j]] - 1;
493: #endif
494:             if (col < 0 && !((Mat_SeqAIJ*)(aij->B->data))->nonew) {
495:               MatDisAssemble_MPIAIJ(mat);
496:               col  =  in[j];
497:               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
498:               B        = aij->B;
499:               b        = (Mat_SeqAIJ*)B->data;
500:               bimax    = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a;
501:               rp2      = bj + bi[row];
502:               ap2      = ba + bi[row];
503:               rmax2    = bimax[row];
504:               nrow2    = bilen[row];
505:               low2     = 0;
506:               high2    = nrow2;
507:               bm       = aij->B->rmap->n;
508:               ba       = b->a;
509:               inserted = PETSC_FALSE;
510:             } else if (col < 0 && !(ignorezeroentries && value == 0.0)) {
511:               if (1 == ((Mat_SeqAIJ*)(aij->B->data))->nonew) {
512:                 PetscInfo3(mat,"Skipping of insertion of new nonzero location in off-diagonal portion of matrix %g(%D,%D)\n",(double)PetscRealPart(value),im[i],in[j]);
513:               } else SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at global row/column (%D, %D) into matrix", im[i], in[j]);
514:             }
515:           } else col = in[j];
516:           nonew = b->nonew;
517:           MatSetValues_SeqAIJ_B_Private(row,col,value,addv,im[i],in[j]);
518: #if defined(PETSC_HAVE_DEVICE)
519:           if (B->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) B->offloadmask = PETSC_OFFLOAD_CPU;
520: #endif
521:         }
522:       }
523:     } else {
524:       if (mat->nooffprocentries) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Setting off process row %D even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set",im[i]);
525:       if (!aij->donotstash) {
526:         mat->assembled = PETSC_FALSE;
527:         if (roworiented) {
528:           MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
529:         } else {
530:           MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
531:         }
532:       }
533:     }
534:   }
535:   return(0);
536: }

538: /*
539:     This function sets the j and ilen arrays (of the diagonal and off-diagonal part) of an MPIAIJ-matrix.
540:     The values in mat_i have to be sorted and the values in mat_j have to be sorted for each row (CSR-like).
541:     No off-processor parts off the matrix are allowed here and mat->was_assembled has to be PETSC_FALSE.
542: */
543: PetscErrorCode MatSetValues_MPIAIJ_CopyFromCSRFormat_Symbolic(Mat mat,const PetscInt mat_j[],const PetscInt mat_i[])
544: {
545:   Mat_MPIAIJ     *aij        = (Mat_MPIAIJ*)mat->data;
546:   Mat            A           = aij->A; /* diagonal part of the matrix */
547:   Mat            B           = aij->B; /* offdiagonal part of the matrix */
548:   Mat_SeqAIJ     *a          = (Mat_SeqAIJ*)A->data;
549:   Mat_SeqAIJ     *b          = (Mat_SeqAIJ*)B->data;
550:   PetscInt       cstart      = mat->cmap->rstart,cend = mat->cmap->rend,col;
551:   PetscInt       *ailen      = a->ilen,*aj = a->j;
552:   PetscInt       *bilen      = b->ilen,*bj = b->j;
553:   PetscInt       am          = aij->A->rmap->n,j;
554:   PetscInt       diag_so_far = 0,dnz;
555:   PetscInt       offd_so_far = 0,onz;

558:   /* Iterate over all rows of the matrix */
559:   for (j=0; j<am; j++) {
560:     dnz = onz = 0;
561:     /*  Iterate over all non-zero columns of the current row */
562:     for (col=mat_i[j]; col<mat_i[j+1]; col++) {
563:       /* If column is in the diagonal */
564:       if (mat_j[col] >= cstart && mat_j[col] < cend) {
565:         aj[diag_so_far++] = mat_j[col] - cstart;
566:         dnz++;
567:       } else { /* off-diagonal entries */
568:         bj[offd_so_far++] = mat_j[col];
569:         onz++;
570:       }
571:     }
572:     ailen[j] = dnz;
573:     bilen[j] = onz;
574:   }
575:   return(0);
576: }

578: /*
579:     This function sets the local j, a and ilen arrays (of the diagonal and off-diagonal part) of an MPIAIJ-matrix.
580:     The values in mat_i have to be sorted and the values in mat_j have to be sorted for each row (CSR-like).
581:     No off-processor parts off the matrix are allowed here, they are set at a later point by MatSetValues_MPIAIJ.
582:     Also, mat->was_assembled has to be false, otherwise the statement aj[rowstart_diag+dnz_row] = mat_j[col] - cstart;
583:     would not be true and the more complex MatSetValues_MPIAIJ has to be used.
584: */
585: PetscErrorCode MatSetValues_MPIAIJ_CopyFromCSRFormat(Mat mat,const PetscInt mat_j[],const PetscInt mat_i[],const PetscScalar mat_a[])
586: {
587:   Mat_MPIAIJ     *aij   = (Mat_MPIAIJ*)mat->data;
588:   Mat            A      = aij->A; /* diagonal part of the matrix */
589:   Mat            B      = aij->B; /* offdiagonal part of the matrix */
590:   Mat_SeqAIJ     *aijd  =(Mat_SeqAIJ*)(aij->A)->data,*aijo=(Mat_SeqAIJ*)(aij->B)->data;
591:   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)A->data;
592:   Mat_SeqAIJ     *b     = (Mat_SeqAIJ*)B->data;
593:   PetscInt       cstart = mat->cmap->rstart,cend = mat->cmap->rend;
594:   PetscInt       *ailen = a->ilen,*aj = a->j;
595:   PetscInt       *bilen = b->ilen,*bj = b->j;
596:   PetscInt       am     = aij->A->rmap->n,j;
597:   PetscInt       *full_diag_i=aijd->i,*full_offd_i=aijo->i; /* These variables can also include non-local elements, which are set at a later point. */
598:   PetscInt       col,dnz_row,onz_row,rowstart_diag,rowstart_offd;
599:   PetscScalar    *aa = a->a,*ba = b->a;

602:   /* Iterate over all rows of the matrix */
603:   for (j=0; j<am; j++) {
604:     dnz_row = onz_row = 0;
605:     rowstart_offd = full_offd_i[j];
606:     rowstart_diag = full_diag_i[j];
607:     /*  Iterate over all non-zero columns of the current row */
608:     for (col=mat_i[j]; col<mat_i[j+1]; col++) {
609:       /* If column is in the diagonal */
610:       if (mat_j[col] >= cstart && mat_j[col] < cend) {
611:         aj[rowstart_diag+dnz_row] = mat_j[col] - cstart;
612:         aa[rowstart_diag+dnz_row] = mat_a[col];
613:         dnz_row++;
614:       } else { /* off-diagonal entries */
615:         bj[rowstart_offd+onz_row] = mat_j[col];
616:         ba[rowstart_offd+onz_row] = mat_a[col];
617:         onz_row++;
618:       }
619:     }
620:     ailen[j] = dnz_row;
621:     bilen[j] = onz_row;
622:   }
623:   return(0);
624: }

626: PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
627: {
628:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
630:   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
631:   PetscInt       cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;

634:   for (i=0; i<m; i++) {
635:     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
636:     if (idxm[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",idxm[i],mat->rmap->N-1);
637:     if (idxm[i] >= rstart && idxm[i] < rend) {
638:       row = idxm[i] - rstart;
639:       for (j=0; j<n; j++) {
640:         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
641:         if (idxn[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",idxn[j],mat->cmap->N-1);
642:         if (idxn[j] >= cstart && idxn[j] < cend) {
643:           col  = idxn[j] - cstart;
644:           MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);
645:         } else {
646:           if (!aij->colmap) {
647:             MatCreateColmap_MPIAIJ_Private(mat);
648:           }
649: #if defined(PETSC_USE_CTABLE)
650:           PetscTableFind(aij->colmap,idxn[j]+1,&col);
651:           col--;
652: #else
653:           col = aij->colmap[idxn[j]] - 1;
654: #endif
655:           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
656:           else {
657:             MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);
658:           }
659:         }
660:       }
661:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
662:   }
663:   return(0);
664: }

666: PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
667: {
668:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
670:   PetscInt       nstash,reallocs;

673:   if (aij->donotstash || mat->nooffprocentries) return(0);

675:   MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
676:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
677:   PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
678:   return(0);
679: }

681: PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
682: {
683:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
685:   PetscMPIInt    n;
686:   PetscInt       i,j,rstart,ncols,flg;
687:   PetscInt       *row,*col;
688:   PetscBool      other_disassembled;
689:   PetscScalar    *val;

691:   /* do not use 'b = (Mat_SeqAIJ*)aij->B->data' as B can be reset in disassembly */

694:   if (!aij->donotstash && !mat->nooffprocentries) {
695:     while (1) {
696:       MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
697:       if (!flg) break;

699:       for (i=0; i<n;) {
700:         /* Now identify the consecutive vals belonging to the same row */
701:         for (j=i,rstart=row[j]; j<n; j++) {
702:           if (row[j] != rstart) break;
703:         }
704:         if (j < n) ncols = j-i;
705:         else       ncols = n-i;
706:         /* Now assemble all these values with a single function call */
707:         MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,mat->insertmode);
708:         i    = j;
709:       }
710:     }
711:     MatStashScatterEnd_Private(&mat->stash);
712:   }
713: #if defined(PETSC_HAVE_DEVICE)
714:   if (mat->offloadmask == PETSC_OFFLOAD_CPU) aij->A->offloadmask = PETSC_OFFLOAD_CPU;
715:   /* We call MatBindToCPU() on aij->A and aij->B here, because if MatBindToCPU_MPIAIJ() is called before assembly, it cannot bind these. */
716:   if (mat->boundtocpu) {
717:     MatBindToCPU(aij->A,PETSC_TRUE);
718:     MatBindToCPU(aij->B,PETSC_TRUE);
719:   }
720: #endif
721:   MatAssemblyBegin(aij->A,mode);
722:   MatAssemblyEnd(aij->A,mode);

724:   /* determine if any processor has disassembled, if so we must
725:      also disassemble ourself, in order that we may reassemble. */
726:   /*
727:      if nonzero structure of submatrix B cannot change then we know that
728:      no processor disassembled thus we can skip this stuff
729:   */
730:   if (!((Mat_SeqAIJ*)aij->B->data)->nonew) {
731:     MPIU_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));
732:     if (mat->was_assembled && !other_disassembled) {
733: #if defined(PETSC_HAVE_DEVICE)
734:       aij->B->offloadmask = PETSC_OFFLOAD_BOTH; /* do not copy on the GPU when assembling inside MatDisAssemble_MPIAIJ */
735: #endif
736:       MatDisAssemble_MPIAIJ(mat);
737:     }
738:   }
739:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
740:     MatSetUpMultiply_MPIAIJ(mat);
741:   }
742:   MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);
743: #if defined(PETSC_HAVE_DEVICE)
744:   if (mat->offloadmask == PETSC_OFFLOAD_CPU && aij->B->offloadmask != PETSC_OFFLOAD_UNALLOCATED) aij->B->offloadmask = PETSC_OFFLOAD_CPU;
745: #endif
746:   MatAssemblyBegin(aij->B,mode);
747:   MatAssemblyEnd(aij->B,mode);

749:   PetscFree2(aij->rowvalues,aij->rowindices);

751:   aij->rowvalues = NULL;

753:   VecDestroy(&aij->diag);

755:   /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
756:   if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
757:     PetscObjectState state = aij->A->nonzerostate + aij->B->nonzerostate;
758:     MPIU_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));
759:   }
760: #if defined(PETSC_HAVE_DEVICE)
761:   mat->offloadmask = PETSC_OFFLOAD_BOTH;
762: #endif
763:   return(0);
764: }

766: PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
767: {
768:   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;

772:   MatZeroEntries(l->A);
773:   MatZeroEntries(l->B);
774:   return(0);
775: }

777: PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
778: {
779:   Mat_MPIAIJ      *mat = (Mat_MPIAIJ *) A->data;
780:   PetscObjectState sA, sB;
781:   PetscInt        *lrows;
782:   PetscInt         r, len;
783:   PetscBool        cong, lch, gch;
784:   PetscErrorCode   ierr;

787:   /* get locally owned rows */
788:   MatZeroRowsMapLocal_Private(A,N,rows,&len,&lrows);
789:   MatHasCongruentLayouts(A,&cong);
790:   /* fix right hand side if needed */
791:   if (x && b) {
792:     const PetscScalar *xx;
793:     PetscScalar       *bb;

795:     if (!cong) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Need matching row/col layout");
796:     VecGetArrayRead(x, &xx);
797:     VecGetArray(b, &bb);
798:     for (r = 0; r < len; ++r) bb[lrows[r]] = diag*xx[lrows[r]];
799:     VecRestoreArrayRead(x, &xx);
800:     VecRestoreArray(b, &bb);
801:   }

803:   sA = mat->A->nonzerostate;
804:   sB = mat->B->nonzerostate;

806:   if (diag != 0.0 && cong) {
807:     MatZeroRows(mat->A, len, lrows, diag, NULL, NULL);
808:     MatZeroRows(mat->B, len, lrows, 0.0, NULL, NULL);
809:   } else if (diag != 0.0) { /* non-square or non congruent layouts -> if keepnonzeropattern is false, we allow for new insertion */
810:     Mat_SeqAIJ *aijA = (Mat_SeqAIJ*)mat->A->data;
811:     Mat_SeqAIJ *aijB = (Mat_SeqAIJ*)mat->B->data;
812:     PetscInt   nnwA, nnwB;
813:     PetscBool  nnzA, nnzB;

815:     nnwA = aijA->nonew;
816:     nnwB = aijB->nonew;
817:     nnzA = aijA->keepnonzeropattern;
818:     nnzB = aijB->keepnonzeropattern;
819:     if (!nnzA) {
820:       PetscInfo(mat->A,"Requested to not keep the pattern and add a nonzero diagonal; may encounter reallocations on diagonal block.\n");
821:       aijA->nonew = 0;
822:     }
823:     if (!nnzB) {
824:       PetscInfo(mat->B,"Requested to not keep the pattern and add a nonzero diagonal; may encounter reallocations on off-diagonal block.\n");
825:       aijB->nonew = 0;
826:     }
827:     /* Must zero here before the next loop */
828:     MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);
829:     MatZeroRows(mat->B, len, lrows, 0.0, NULL, NULL);
830:     for (r = 0; r < len; ++r) {
831:       const PetscInt row = lrows[r] + A->rmap->rstart;
832:       if (row >= A->cmap->N) continue;
833:       MatSetValues(A, 1, &row, 1, &row, &diag, INSERT_VALUES);
834:     }
835:     aijA->nonew = nnwA;
836:     aijB->nonew = nnwB;
837:   } else {
838:     MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);
839:     MatZeroRows(mat->B, len, lrows, 0.0, NULL, NULL);
840:   }
841:   PetscFree(lrows);
842:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
843:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

845:   /* reduce nonzerostate */
846:   lch = (PetscBool)(sA != mat->A->nonzerostate || sB != mat->B->nonzerostate);
847:   MPIU_Allreduce(&lch,&gch,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)A));
848:   if (gch) A->nonzerostate++;
849:   return(0);
850: }

852: PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
853: {
854:   Mat_MPIAIJ        *l = (Mat_MPIAIJ*)A->data;
855:   PetscErrorCode    ierr;
856:   PetscMPIInt       n = A->rmap->n;
857:   PetscInt          i,j,r,m,len = 0;
858:   PetscInt          *lrows,*owners = A->rmap->range;
859:   PetscMPIInt       p = 0;
860:   PetscSFNode       *rrows;
861:   PetscSF           sf;
862:   const PetscScalar *xx;
863:   PetscScalar       *bb,*mask;
864:   Vec               xmask,lmask;
865:   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*)l->B->data;
866:   const PetscInt    *aj, *ii,*ridx;
867:   PetscScalar       *aa;

870:   /* Create SF where leaves are input rows and roots are owned rows */
871:   PetscMalloc1(n, &lrows);
872:   for (r = 0; r < n; ++r) lrows[r] = -1;
873:   PetscMalloc1(N, &rrows);
874:   for (r = 0; r < N; ++r) {
875:     const PetscInt idx   = rows[r];
876:     if (idx < 0 || A->rmap->N <= idx) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range [0,%D)",idx,A->rmap->N);
877:     if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
878:       PetscLayoutFindOwner(A->rmap,idx,&p);
879:     }
880:     rrows[r].rank  = p;
881:     rrows[r].index = rows[r] - owners[p];
882:   }
883:   PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);
884:   PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);
885:   /* Collect flags for rows to be zeroed */
886:   PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
887:   PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
888:   PetscSFDestroy(&sf);
889:   /* Compress and put in row numbers */
890:   for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
891:   /* zero diagonal part of matrix */
892:   MatZeroRowsColumns(l->A,len,lrows,diag,x,b);
893:   /* handle off diagonal part of matrix */
894:   MatCreateVecs(A,&xmask,NULL);
895:   VecDuplicate(l->lvec,&lmask);
896:   VecGetArray(xmask,&bb);
897:   for (i=0; i<len; i++) bb[lrows[i]] = 1;
898:   VecRestoreArray(xmask,&bb);
899:   VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
900:   VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
901:   VecDestroy(&xmask);
902:   if (x && b) { /* this code is buggy when the row and column layout don't match */
903:     PetscBool cong;

905:     MatHasCongruentLayouts(A,&cong);
906:     if (!cong) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Need matching row/col layout");
907:     VecScatterBegin(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
908:     VecScatterEnd(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
909:     VecGetArrayRead(l->lvec,&xx);
910:     VecGetArray(b,&bb);
911:   }
912:   VecGetArray(lmask,&mask);
913:   /* remove zeroed rows of off diagonal matrix */
914:   ii = aij->i;
915:   for (i=0; i<len; i++) {
916:     PetscArrayzero(aij->a + ii[lrows[i]],ii[lrows[i]+1] - ii[lrows[i]]);
917:   }
918:   /* loop over all elements of off process part of matrix zeroing removed columns*/
919:   if (aij->compressedrow.use) {
920:     m    = aij->compressedrow.nrows;
921:     ii   = aij->compressedrow.i;
922:     ridx = aij->compressedrow.rindex;
923:     for (i=0; i<m; i++) {
924:       n  = ii[i+1] - ii[i];
925:       aj = aij->j + ii[i];
926:       aa = aij->a + ii[i];

928:       for (j=0; j<n; j++) {
929:         if (PetscAbsScalar(mask[*aj])) {
930:           if (b) bb[*ridx] -= *aa*xx[*aj];
931:           *aa = 0.0;
932:         }
933:         aa++;
934:         aj++;
935:       }
936:       ridx++;
937:     }
938:   } else { /* do not use compressed row format */
939:     m = l->B->rmap->n;
940:     for (i=0; i<m; i++) {
941:       n  = ii[i+1] - ii[i];
942:       aj = aij->j + ii[i];
943:       aa = aij->a + ii[i];
944:       for (j=0; j<n; j++) {
945:         if (PetscAbsScalar(mask[*aj])) {
946:           if (b) bb[i] -= *aa*xx[*aj];
947:           *aa = 0.0;
948:         }
949:         aa++;
950:         aj++;
951:       }
952:     }
953:   }
954:   if (x && b) {
955:     VecRestoreArray(b,&bb);
956:     VecRestoreArrayRead(l->lvec,&xx);
957:   }
958:   VecRestoreArray(lmask,&mask);
959:   VecDestroy(&lmask);
960:   PetscFree(lrows);

962:   /* only change matrix nonzero state if pattern was allowed to be changed */
963:   if (!((Mat_SeqAIJ*)(l->A->data))->keepnonzeropattern) {
964:     PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
965:     MPIU_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
966:   }
967:   return(0);
968: }

970: PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
971: {
972:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
974:   PetscInt       nt;
975:   VecScatter     Mvctx = a->Mvctx;

978:   VecGetLocalSize(xx,&nt);
979:   if (nt != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%D) and xx (%D)",A->cmap->n,nt);
980:   VecScatterBegin(Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
981:   (*a->A->ops->mult)(a->A,xx,yy);
982:   VecScatterEnd(Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
983:   (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
984:   return(0);
985: }

987: PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx)
988: {
989:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

993:   MatMultDiagonalBlock(a->A,bb,xx);
994:   return(0);
995: }

997: PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
998: {
999:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1001:   VecScatter     Mvctx = a->Mvctx;

1004:   VecScatterBegin(Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1005:   (*a->A->ops->multadd)(a->A,xx,yy,zz);
1006:   VecScatterEnd(Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1007:   (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
1008:   return(0);
1009: }

1011: PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
1012: {
1013:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1017:   /* do nondiagonal part */
1018:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1019:   /* do local part */
1020:   (*a->A->ops->multtranspose)(a->A,xx,yy);
1021:   /* add partial results together */
1022:   VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1023:   VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1024:   return(0);
1025: }

1027: PetscErrorCode MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool  *f)
1028: {
1029:   MPI_Comm       comm;
1030:   Mat_MPIAIJ     *Aij = (Mat_MPIAIJ*) Amat->data, *Bij;
1031:   Mat            Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
1032:   IS             Me,Notme;
1034:   PetscInt       M,N,first,last,*notme,i;
1035:   PetscBool      lf;
1036:   PetscMPIInt    size;

1039:   /* Easy test: symmetric diagonal block */
1040:   Bij  = (Mat_MPIAIJ*) Bmat->data; Bdia = Bij->A;
1041:   MatIsTranspose(Adia,Bdia,tol,&lf);
1042:   MPIU_Allreduce(&lf,f,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)Amat));
1043:   if (!*f) return(0);
1044:   PetscObjectGetComm((PetscObject)Amat,&comm);
1045:   MPI_Comm_size(comm,&size);
1046:   if (size == 1) return(0);

1048:   /* Hard test: off-diagonal block. This takes a MatCreateSubMatrix. */
1049:   MatGetSize(Amat,&M,&N);
1050:   MatGetOwnershipRange(Amat,&first,&last);
1051:   PetscMalloc1(N-last+first,&notme);
1052:   for (i=0; i<first; i++) notme[i] = i;
1053:   for (i=last; i<M; i++) notme[i-last+first] = i;
1054:   ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);
1055:   ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);
1056:   MatCreateSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);
1057:   Aoff = Aoffs[0];
1058:   MatCreateSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);
1059:   Boff = Boffs[0];
1060:   MatIsTranspose(Aoff,Boff,tol,f);
1061:   MatDestroyMatrices(1,&Aoffs);
1062:   MatDestroyMatrices(1,&Boffs);
1063:   ISDestroy(&Me);
1064:   ISDestroy(&Notme);
1065:   PetscFree(notme);
1066:   return(0);
1067: }

1069: PetscErrorCode MatIsSymmetric_MPIAIJ(Mat A,PetscReal tol,PetscBool  *f)
1070: {

1074:   MatIsTranspose_MPIAIJ(A,A,tol,f);
1075:   return(0);
1076: }

1078: PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1079: {
1080:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1084:   /* do nondiagonal part */
1085:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1086:   /* do local part */
1087:   (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);
1088:   /* add partial results together */
1089:   VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1090:   VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1091:   return(0);
1092: }

1094: /*
1095:   This only works correctly for square matrices where the subblock A->A is the
1096:    diagonal block
1097: */
1098: PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
1099: {
1101:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1104:   if (A->rmap->N != A->cmap->N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
1105:   if (A->rmap->rstart != A->cmap->rstart || A->rmap->rend != A->cmap->rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
1106:   MatGetDiagonal(a->A,v);
1107:   return(0);
1108: }

1110: PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
1111: {
1112:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1116:   MatScale(a->A,aa);
1117:   MatScale(a->B,aa);
1118:   return(0);
1119: }

1121: PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
1122: {
1123:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

1127: #if defined(PETSC_USE_LOG)
1128:   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N);
1129: #endif
1130:   MatStashDestroy_Private(&mat->stash);
1131:   VecDestroy(&aij->diag);
1132:   MatDestroy(&aij->A);
1133:   MatDestroy(&aij->B);
1134: #if defined(PETSC_USE_CTABLE)
1135:   PetscTableDestroy(&aij->colmap);
1136: #else
1137:   PetscFree(aij->colmap);
1138: #endif
1139:   PetscFree(aij->garray);
1140:   VecDestroy(&aij->lvec);
1141:   VecScatterDestroy(&aij->Mvctx);
1142:   PetscFree2(aij->rowvalues,aij->rowindices);
1143:   PetscFree(aij->ld);
1144:   PetscFree(mat->data);

1146:   /* may be created by MatCreateMPIAIJSumSeqAIJSymbolic */
1147:   PetscObjectCompose((PetscObject)mat,"MatMergeSeqsToMPI",NULL);

1149:   PetscObjectChangeTypeName((PetscObject)mat,NULL);
1150:   PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);
1151:   PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);
1152:   PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C",NULL);
1153:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C",NULL);
1154:   PetscObjectComposeFunction((PetscObject)mat,"MatResetPreallocation_C",NULL);
1155:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C",NULL);
1156:   PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);
1157:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpibaij_C",NULL);
1158:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C",NULL);
1159: #if defined(PETSC_HAVE_CUDA)
1160:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpiaijcusparse_C",NULL);
1161: #endif
1162: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
1163:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpiaijkokkos_C",NULL);
1164: #endif
1165:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpidense_C",NULL);
1166: #if defined(PETSC_HAVE_ELEMENTAL)
1167:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_elemental_C",NULL);
1168: #endif
1169: #if defined(PETSC_HAVE_SCALAPACK)
1170:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_scalapack_C",NULL);
1171: #endif
1172: #if defined(PETSC_HAVE_HYPRE)
1173:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_hypre_C",NULL);
1174:   PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_transpose_mpiaij_mpiaij_C",NULL);
1175: #endif
1176:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_is_C",NULL);
1177:   PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_is_mpiaij_C",NULL);
1178:   PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_mpiaij_mpiaij_C",NULL);
1179:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetUseScalableIncreaseOverlap_C",NULL);
1180:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpiaijperm_C",NULL);
1181:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpiaijsell_C",NULL);
1182: #if defined(PETSC_HAVE_MKL_SPARSE)
1183:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpiaijmkl_C",NULL);
1184: #endif
1185:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpiaijcrl_C",NULL);
1186:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_is_C",NULL);
1187:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisell_C",NULL);
1188:   return(0);
1189: }

1191: PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
1192: {
1193:   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
1194:   Mat_SeqAIJ        *A   = (Mat_SeqAIJ*)aij->A->data;
1195:   Mat_SeqAIJ        *B   = (Mat_SeqAIJ*)aij->B->data;
1196:   const PetscInt    *garray = aij->garray;
1197:   const PetscScalar *aa,*ba;
1198:   PetscInt          header[4],M,N,m,rs,cs,nz,cnt,i,ja,jb;
1199:   PetscInt          *rowlens;
1200:   PetscInt          *colidxs;
1201:   PetscScalar       *matvals;
1202:   PetscErrorCode    ierr;

1205:   PetscViewerSetUp(viewer);

1207:   M  = mat->rmap->N;
1208:   N  = mat->cmap->N;
1209:   m  = mat->rmap->n;
1210:   rs = mat->rmap->rstart;
1211:   cs = mat->cmap->rstart;
1212:   nz = A->nz + B->nz;

1214:   /* write matrix header */
1215:   header[0] = MAT_FILE_CLASSID;
1216:   header[1] = M; header[2] = N; header[3] = nz;
1217:   MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1218:   PetscViewerBinaryWrite(viewer,header,4,PETSC_INT);

1220:   /* fill in and store row lengths  */
1221:   PetscMalloc1(m,&rowlens);
1222:   for (i=0; i<m; i++) rowlens[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
1223:   PetscViewerBinaryWriteAll(viewer,rowlens,m,rs,M,PETSC_INT);
1224:   PetscFree(rowlens);

1226:   /* fill in and store column indices */
1227:   PetscMalloc1(nz,&colidxs);
1228:   for (cnt=0, i=0; i<m; i++) {
1229:     for (jb=B->i[i]; jb<B->i[i+1]; jb++) {
1230:       if (garray[B->j[jb]] > cs) break;
1231:       colidxs[cnt++] = garray[B->j[jb]];
1232:     }
1233:     for (ja=A->i[i]; ja<A->i[i+1]; ja++)
1234:       colidxs[cnt++] = A->j[ja] + cs;
1235:     for (; jb<B->i[i+1]; jb++)
1236:       colidxs[cnt++] = garray[B->j[jb]];
1237:   }
1238:   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1239:   PetscViewerBinaryWriteAll(viewer,colidxs,nz,PETSC_DETERMINE,PETSC_DETERMINE,PETSC_INT);
1240:   PetscFree(colidxs);

1242:   /* fill in and store nonzero values */
1243:   MatSeqAIJGetArrayRead(aij->A,&aa);
1244:   MatSeqAIJGetArrayRead(aij->B,&ba);
1245:   PetscMalloc1(nz,&matvals);
1246:   for (cnt=0, i=0; i<m; i++) {
1247:     for (jb=B->i[i]; jb<B->i[i+1]; jb++) {
1248:       if (garray[B->j[jb]] > cs) break;
1249:       matvals[cnt++] = ba[jb];
1250:     }
1251:     for (ja=A->i[i]; ja<A->i[i+1]; ja++)
1252:       matvals[cnt++] = aa[ja];
1253:     for (; jb<B->i[i+1]; jb++)
1254:       matvals[cnt++] = ba[jb];
1255:   }
1256:   MatSeqAIJRestoreArrayRead(aij->A,&aa);
1257:   MatSeqAIJRestoreArrayRead(aij->B,&ba);
1258:   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1259:   PetscViewerBinaryWriteAll(viewer,matvals,nz,PETSC_DETERMINE,PETSC_DETERMINE,PETSC_SCALAR);
1260:   PetscFree(matvals);

1262:   /* write block size option to the viewer's .info file */
1263:   MatView_Binary_BlockSizes(mat,viewer);
1264:   return(0);
1265: }

1267: #include <petscdraw.h>
1268: PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
1269: {
1270:   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
1271:   PetscErrorCode    ierr;
1272:   PetscMPIInt       rank = aij->rank,size = aij->size;
1273:   PetscBool         isdraw,iascii,isbinary;
1274:   PetscViewer       sviewer;
1275:   PetscViewerFormat format;

1278:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1279:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1280:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1281:   if (iascii) {
1282:     PetscViewerGetFormat(viewer,&format);
1283:     if (format == PETSC_VIEWER_LOAD_BALANCE) {
1284:       PetscInt i,nmax = 0,nmin = PETSC_MAX_INT,navg = 0,*nz,nzlocal = ((Mat_SeqAIJ*) (aij->A->data))->nz + ((Mat_SeqAIJ*) (aij->B->data))->nz;
1285:       PetscMalloc1(size,&nz);
1286:       MPI_Allgather(&nzlocal,1,MPIU_INT,nz,1,MPIU_INT,PetscObjectComm((PetscObject)mat));
1287:       for (i=0; i<(PetscInt)size; i++) {
1288:         nmax = PetscMax(nmax,nz[i]);
1289:         nmin = PetscMin(nmin,nz[i]);
1290:         navg += nz[i];
1291:       }
1292:       PetscFree(nz);
1293:       navg = navg/size;
1294:       PetscViewerASCIIPrintf(viewer,"Load Balance - Nonzeros: Min %D  avg %D  max %D\n",nmin,navg,nmax);
1295:       return(0);
1296:     }
1297:     PetscViewerGetFormat(viewer,&format);
1298:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1299:       MatInfo   info;
1300:       PetscInt *inodes=NULL;

1302:       MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
1303:       MatGetInfo(mat,MAT_LOCAL,&info);
1304:       MatInodeGetInodeSizes(aij->A,NULL,&inodes,NULL);
1305:       PetscViewerASCIIPushSynchronized(viewer);
1306:       if (!inodes) {
1307:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %g, not using I-node routines\n",
1308:                                                   rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(double)info.memory);
1309:       } else {
1310:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %g, using I-node routines\n",
1311:                                                   rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(double)info.memory);
1312:       }
1313:       MatGetInfo(aij->A,MAT_LOCAL,&info);
1314:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
1315:       MatGetInfo(aij->B,MAT_LOCAL,&info);
1316:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
1317:       PetscViewerFlush(viewer);
1318:       PetscViewerASCIIPopSynchronized(viewer);
1319:       PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");
1320:       VecScatterView(aij->Mvctx,viewer);
1321:       return(0);
1322:     } else if (format == PETSC_VIEWER_ASCII_INFO) {
1323:       PetscInt inodecount,inodelimit,*inodes;
1324:       MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);
1325:       if (inodes) {
1326:         PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);
1327:       } else {
1328:         PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");
1329:       }
1330:       return(0);
1331:     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
1332:       return(0);
1333:     }
1334:   } else if (isbinary) {
1335:     if (size == 1) {
1336:       PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);
1337:       MatView(aij->A,viewer);
1338:     } else {
1339:       MatView_MPIAIJ_Binary(mat,viewer);
1340:     }
1341:     return(0);
1342:   } else if (iascii && size == 1) {
1343:     PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);
1344:     MatView(aij->A,viewer);
1345:     return(0);
1346:   } else if (isdraw) {
1347:     PetscDraw draw;
1348:     PetscBool isnull;
1349:     PetscViewerDrawGetDraw(viewer,0,&draw);
1350:     PetscDrawIsNull(draw,&isnull);
1351:     if (isnull) return(0);
1352:   }

1354:   { /* assemble the entire matrix onto first processor */
1355:     Mat A = NULL, Av;
1356:     IS  isrow,iscol;

1358:     ISCreateStride(PetscObjectComm((PetscObject)mat),rank == 0 ? mat->rmap->N : 0,0,1,&isrow);
1359:     ISCreateStride(PetscObjectComm((PetscObject)mat),rank == 0 ? mat->cmap->N : 0,0,1,&iscol);
1360:     MatCreateSubMatrix(mat,isrow,iscol,MAT_INITIAL_MATRIX,&A);
1361:     MatMPIAIJGetSeqAIJ(A,&Av,NULL,NULL);
1362: /*  The commented code uses MatCreateSubMatrices instead */
1363: /*
1364:     Mat *AA, A = NULL, Av;
1365:     IS  isrow,iscol;

1367:     ISCreateStride(PetscObjectComm((PetscObject)mat),rank == 0 ? mat->rmap->N : 0,0,1,&isrow);
1368:     ISCreateStride(PetscObjectComm((PetscObject)mat),rank == 0 ? mat->cmap->N : 0,0,1,&iscol);
1369:     MatCreateSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&AA);
1370:     if (rank == 0) {
1371:        PetscObjectReference((PetscObject)AA[0]);
1372:        A    = AA[0];
1373:        Av   = AA[0];
1374:     }
1375:     MatDestroySubMatrices(1,&AA);
1376: */
1377:     ISDestroy(&iscol);
1378:     ISDestroy(&isrow);
1379:     /*
1380:        Everyone has to call to draw the matrix since the graphics waits are
1381:        synchronized across all processors that share the PetscDraw object
1382:     */
1383:     PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
1384:     if (rank == 0) {
1385:       if (((PetscObject)mat)->name) {
1386:         PetscObjectSetName((PetscObject)Av,((PetscObject)mat)->name);
1387:       }
1388:       MatView_SeqAIJ(Av,sviewer);
1389:     }
1390:     PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
1391:     PetscViewerFlush(viewer);
1392:     MatDestroy(&A);
1393:   }
1394:   return(0);
1395: }

1397: PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
1398: {
1400:   PetscBool      iascii,isdraw,issocket,isbinary;

1403:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1404:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1405:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1406:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
1407:   if (iascii || isdraw || isbinary || issocket) {
1408:     MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);
1409:   }
1410:   return(0);
1411: }

1413: PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1414: {
1415:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1417:   Vec            bb1 = NULL;
1418:   PetscBool      hasop;

1421:   if (flag == SOR_APPLY_UPPER) {
1422:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1423:     return(0);
1424:   }

1426:   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) {
1427:     VecDuplicate(bb,&bb1);
1428:   }

1430:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
1431:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1432:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1433:       its--;
1434:     }

1436:     while (its--) {
1437:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1438:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

1440:       /* update rhs: bb1 = bb - B*x */
1441:       VecScale(mat->lvec,-1.0);
1442:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1444:       /* local sweep */
1445:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);
1446:     }
1447:   } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
1448:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1449:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1450:       its--;
1451:     }
1452:     while (its--) {
1453:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1454:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

1456:       /* update rhs: bb1 = bb - B*x */
1457:       VecScale(mat->lvec,-1.0);
1458:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1460:       /* local sweep */
1461:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);
1462:     }
1463:   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
1464:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1465:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1466:       its--;
1467:     }
1468:     while (its--) {
1469:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1470:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

1472:       /* update rhs: bb1 = bb - B*x */
1473:       VecScale(mat->lvec,-1.0);
1474:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1476:       /* local sweep */
1477:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);
1478:     }
1479:   } else if (flag & SOR_EISENSTAT) {
1480:     Vec xx1;

1482:     VecDuplicate(bb,&xx1);
1483:     (*mat->A->ops->sor)(mat->A,bb,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_BACKWARD_SWEEP),fshift,lits,1,xx);

1485:     VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1486:     VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1487:     if (!mat->diag) {
1488:       MatCreateVecs(matin,&mat->diag,NULL);
1489:       MatGetDiagonal(matin,mat->diag);
1490:     }
1491:     MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);
1492:     if (hasop) {
1493:       MatMultDiagonalBlock(matin,xx,bb1);
1494:     } else {
1495:       VecPointwiseMult(bb1,mat->diag,xx);
1496:     }
1497:     VecAYPX(bb1,(omega-2.0)/omega,bb);

1499:     MatMultAdd(mat->B,mat->lvec,bb1,bb1);

1501:     /* local sweep */
1502:     (*mat->A->ops->sor)(mat->A,bb1,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_FORWARD_SWEEP),fshift,lits,1,xx1);
1503:     VecAXPY(xx,1.0,xx1);
1504:     VecDestroy(&xx1);
1505:   } else SETERRQ(PetscObjectComm((PetscObject)matin),PETSC_ERR_SUP,"Parallel SOR not supported");

1507:   VecDestroy(&bb1);

1509:   matin->factorerrortype = mat->A->factorerrortype;
1510:   return(0);
1511: }

1513: PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
1514: {
1515:   Mat            aA,aB,Aperm;
1516:   const PetscInt *rwant,*cwant,*gcols,*ai,*bi,*aj,*bj;
1517:   PetscScalar    *aa,*ba;
1518:   PetscInt       i,j,m,n,ng,anz,bnz,*dnnz,*onnz,*tdnnz,*tonnz,*rdest,*cdest,*work,*gcdest;
1519:   PetscSF        rowsf,sf;
1520:   IS             parcolp = NULL;
1521:   PetscBool      done;

1525:   MatGetLocalSize(A,&m,&n);
1526:   ISGetIndices(rowp,&rwant);
1527:   ISGetIndices(colp,&cwant);
1528:   PetscMalloc3(PetscMax(m,n),&work,m,&rdest,n,&cdest);

1530:   /* Invert row permutation to find out where my rows should go */
1531:   PetscSFCreate(PetscObjectComm((PetscObject)A),&rowsf);
1532:   PetscSFSetGraphLayout(rowsf,A->rmap,A->rmap->n,NULL,PETSC_OWN_POINTER,rwant);
1533:   PetscSFSetFromOptions(rowsf);
1534:   for (i=0; i<m; i++) work[i] = A->rmap->rstart + i;
1535:   PetscSFReduceBegin(rowsf,MPIU_INT,work,rdest,MPI_REPLACE);
1536:   PetscSFReduceEnd(rowsf,MPIU_INT,work,rdest,MPI_REPLACE);

1538:   /* Invert column permutation to find out where my columns should go */
1539:   PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
1540:   PetscSFSetGraphLayout(sf,A->cmap,A->cmap->n,NULL,PETSC_OWN_POINTER,cwant);
1541:   PetscSFSetFromOptions(sf);
1542:   for (i=0; i<n; i++) work[i] = A->cmap->rstart + i;
1543:   PetscSFReduceBegin(sf,MPIU_INT,work,cdest,MPI_REPLACE);
1544:   PetscSFReduceEnd(sf,MPIU_INT,work,cdest,MPI_REPLACE);
1545:   PetscSFDestroy(&sf);

1547:   ISRestoreIndices(rowp,&rwant);
1548:   ISRestoreIndices(colp,&cwant);
1549:   MatMPIAIJGetSeqAIJ(A,&aA,&aB,&gcols);

1551:   /* Find out where my gcols should go */
1552:   MatGetSize(aB,NULL,&ng);
1553:   PetscMalloc1(ng,&gcdest);
1554:   PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
1555:   PetscSFSetGraphLayout(sf,A->cmap,ng,NULL,PETSC_OWN_POINTER,gcols);
1556:   PetscSFSetFromOptions(sf);
1557:   PetscSFBcastBegin(sf,MPIU_INT,cdest,gcdest,MPI_REPLACE);
1558:   PetscSFBcastEnd(sf,MPIU_INT,cdest,gcdest,MPI_REPLACE);
1559:   PetscSFDestroy(&sf);

1561:   PetscCalloc4(m,&dnnz,m,&onnz,m,&tdnnz,m,&tonnz);
1562:   MatGetRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);
1563:   MatGetRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);
1564:   for (i=0; i<m; i++) {
1565:     PetscInt    row = rdest[i];
1566:     PetscMPIInt rowner;
1567:     PetscLayoutFindOwner(A->rmap,row,&rowner);
1568:     for (j=ai[i]; j<ai[i+1]; j++) {
1569:       PetscInt    col = cdest[aj[j]];
1570:       PetscMPIInt cowner;
1571:       PetscLayoutFindOwner(A->cmap,col,&cowner); /* Could build an index for the columns to eliminate this search */
1572:       if (rowner == cowner) dnnz[i]++;
1573:       else onnz[i]++;
1574:     }
1575:     for (j=bi[i]; j<bi[i+1]; j++) {
1576:       PetscInt    col = gcdest[bj[j]];
1577:       PetscMPIInt cowner;
1578:       PetscLayoutFindOwner(A->cmap,col,&cowner);
1579:       if (rowner == cowner) dnnz[i]++;
1580:       else onnz[i]++;
1581:     }
1582:   }
1583:   PetscSFBcastBegin(rowsf,MPIU_INT,dnnz,tdnnz,MPI_REPLACE);
1584:   PetscSFBcastEnd(rowsf,MPIU_INT,dnnz,tdnnz,MPI_REPLACE);
1585:   PetscSFBcastBegin(rowsf,MPIU_INT,onnz,tonnz,MPI_REPLACE);
1586:   PetscSFBcastEnd(rowsf,MPIU_INT,onnz,tonnz,MPI_REPLACE);
1587:   PetscSFDestroy(&rowsf);

1589:   MatCreateAIJ(PetscObjectComm((PetscObject)A),A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N,0,tdnnz,0,tonnz,&Aperm);
1590:   MatSeqAIJGetArray(aA,&aa);
1591:   MatSeqAIJGetArray(aB,&ba);
1592:   for (i=0; i<m; i++) {
1593:     PetscInt *acols = dnnz,*bcols = onnz; /* Repurpose now-unneeded arrays */
1594:     PetscInt j0,rowlen;
1595:     rowlen = ai[i+1] - ai[i];
1596:     for (j0=j=0; j<rowlen; j0=j) { /* rowlen could be larger than number of rows m, so sum in batches */
1597:       for (; j<PetscMin(rowlen,j0+m); j++) acols[j-j0] = cdest[aj[ai[i]+j]];
1598:       MatSetValues(Aperm,1,&rdest[i],j-j0,acols,aa+ai[i]+j0,INSERT_VALUES);
1599:     }
1600:     rowlen = bi[i+1] - bi[i];
1601:     for (j0=j=0; j<rowlen; j0=j) {
1602:       for (; j<PetscMin(rowlen,j0+m); j++) bcols[j-j0] = gcdest[bj[bi[i]+j]];
1603:       MatSetValues(Aperm,1,&rdest[i],j-j0,bcols,ba+bi[i]+j0,INSERT_VALUES);
1604:     }
1605:   }
1606:   MatAssemblyBegin(Aperm,MAT_FINAL_ASSEMBLY);
1607:   MatAssemblyEnd(Aperm,MAT_FINAL_ASSEMBLY);
1608:   MatRestoreRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);
1609:   MatRestoreRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);
1610:   MatSeqAIJRestoreArray(aA,&aa);
1611:   MatSeqAIJRestoreArray(aB,&ba);
1612:   PetscFree4(dnnz,onnz,tdnnz,tonnz);
1613:   PetscFree3(work,rdest,cdest);
1614:   PetscFree(gcdest);
1615:   if (parcolp) {ISDestroy(&colp);}
1616:   *B = Aperm;
1617:   return(0);
1618: }

1620: PetscErrorCode  MatGetGhosts_MPIAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
1621: {
1622:   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;

1626:   MatGetSize(aij->B,NULL,nghosts);
1627:   if (ghosts) *ghosts = aij->garray;
1628:   return(0);
1629: }

1631: PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1632: {
1633:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1634:   Mat            A    = mat->A,B = mat->B;
1636:   PetscLogDouble isend[5],irecv[5];

1639:   info->block_size = 1.0;
1640:   MatGetInfo(A,MAT_LOCAL,info);

1642:   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1643:   isend[3] = info->memory;  isend[4] = info->mallocs;

1645:   MatGetInfo(B,MAT_LOCAL,info);

1647:   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1648:   isend[3] += info->memory;  isend[4] += info->mallocs;
1649:   if (flag == MAT_LOCAL) {
1650:     info->nz_used      = isend[0];
1651:     info->nz_allocated = isend[1];
1652:     info->nz_unneeded  = isend[2];
1653:     info->memory       = isend[3];
1654:     info->mallocs      = isend[4];
1655:   } else if (flag == MAT_GLOBAL_MAX) {
1656:     MPIU_Allreduce(isend,irecv,5,MPIU_PETSCLOGDOUBLE,MPI_MAX,PetscObjectComm((PetscObject)matin));

1658:     info->nz_used      = irecv[0];
1659:     info->nz_allocated = irecv[1];
1660:     info->nz_unneeded  = irecv[2];
1661:     info->memory       = irecv[3];
1662:     info->mallocs      = irecv[4];
1663:   } else if (flag == MAT_GLOBAL_SUM) {
1664:     MPIU_Allreduce(isend,irecv,5,MPIU_PETSCLOGDOUBLE,MPI_SUM,PetscObjectComm((PetscObject)matin));

1666:     info->nz_used      = irecv[0];
1667:     info->nz_allocated = irecv[1];
1668:     info->nz_unneeded  = irecv[2];
1669:     info->memory       = irecv[3];
1670:     info->mallocs      = irecv[4];
1671:   }
1672:   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
1673:   info->fill_ratio_needed = 0;
1674:   info->factor_mallocs    = 0;
1675:   return(0);
1676: }

1678: PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg)
1679: {
1680:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1684:   switch (op) {
1685:   case MAT_NEW_NONZERO_LOCATIONS:
1686:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1687:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1688:   case MAT_KEEP_NONZERO_PATTERN:
1689:   case MAT_NEW_NONZERO_LOCATION_ERR:
1690:   case MAT_USE_INODES:
1691:   case MAT_IGNORE_ZERO_ENTRIES:
1692:   case MAT_FORM_EXPLICIT_TRANSPOSE:
1693:     MatCheckPreallocated(A,1);
1694:     MatSetOption(a->A,op,flg);
1695:     MatSetOption(a->B,op,flg);
1696:     break;
1697:   case MAT_ROW_ORIENTED:
1698:     MatCheckPreallocated(A,1);
1699:     a->roworiented = flg;

1701:     MatSetOption(a->A,op,flg);
1702:     MatSetOption(a->B,op,flg);
1703:     break;
1704:   case MAT_FORCE_DIAGONAL_ENTRIES:
1705:   case MAT_SORTED_FULL:
1706:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1707:     break;
1708:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1709:     a->donotstash = flg;
1710:     break;
1711:   /* Symmetry flags are handled directly by MatSetOption() and they don't affect preallocation */
1712:   case MAT_SPD:
1713:   case MAT_SYMMETRIC:
1714:   case MAT_STRUCTURALLY_SYMMETRIC:
1715:   case MAT_HERMITIAN:
1716:   case MAT_SYMMETRY_ETERNAL:
1717:     break;
1718:   case MAT_SUBMAT_SINGLEIS:
1719:     A->submat_singleis = flg;
1720:     break;
1721:   case MAT_STRUCTURE_ONLY:
1722:     /* The option is handled directly by MatSetOption() */
1723:     break;
1724:   default:
1725:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1726:   }
1727:   return(0);
1728: }

1730: PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1731: {
1732:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1733:   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
1735:   PetscInt       i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart;
1736:   PetscInt       nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend;
1737:   PetscInt       *cmap,*idx_p;

1740:   if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
1741:   mat->getrowactive = PETSC_TRUE;

1743:   if (!mat->rowvalues && (idx || v)) {
1744:     /*
1745:         allocate enough space to hold information from the longest row.
1746:     */
1747:     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1748:     PetscInt   max = 1,tmp;
1749:     for (i=0; i<matin->rmap->n; i++) {
1750:       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1751:       if (max < tmp) max = tmp;
1752:     }
1753:     PetscMalloc2(max,&mat->rowvalues,max,&mat->rowindices);
1754:   }

1756:   if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows");
1757:   lrow = row - rstart;

1759:   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1760:   if (!v)   {pvA = NULL; pvB = NULL;}
1761:   if (!idx) {pcA = NULL; if (!v) pcB = NULL;}
1762:   (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1763:   (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1764:   nztot = nzA + nzB;

1766:   cmap = mat->garray;
1767:   if (v  || idx) {
1768:     if (nztot) {
1769:       /* Sort by increasing column numbers, assuming A and B already sorted */
1770:       PetscInt imark = -1;
1771:       if (v) {
1772:         *v = v_p = mat->rowvalues;
1773:         for (i=0; i<nzB; i++) {
1774:           if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i];
1775:           else break;
1776:         }
1777:         imark = i;
1778:         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1779:         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1780:       }
1781:       if (idx) {
1782:         *idx = idx_p = mat->rowindices;
1783:         if (imark > -1) {
1784:           for (i=0; i<imark; i++) {
1785:             idx_p[i] = cmap[cworkB[i]];
1786:           }
1787:         } else {
1788:           for (i=0; i<nzB; i++) {
1789:             if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]];
1790:             else break;
1791:           }
1792:           imark = i;
1793:         }
1794:         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
1795:         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
1796:       }
1797:     } else {
1798:       if (idx) *idx = NULL;
1799:       if (v)   *v   = NULL;
1800:     }
1801:   }
1802:   *nz  = nztot;
1803:   (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1804:   (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1805:   return(0);
1806: }

1808: PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1809: {
1810:   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;

1813:   if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
1814:   aij->getrowactive = PETSC_FALSE;
1815:   return(0);
1816: }

1818: PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1819: {
1820:   Mat_MPIAIJ     *aij  = (Mat_MPIAIJ*)mat->data;
1821:   Mat_SeqAIJ     *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1823:   PetscInt       i,j,cstart = mat->cmap->rstart;
1824:   PetscReal      sum = 0.0;
1825:   MatScalar      *v;

1828:   if (aij->size == 1) {
1829:      MatNorm(aij->A,type,norm);
1830:   } else {
1831:     if (type == NORM_FROBENIUS) {
1832:       v = amat->a;
1833:       for (i=0; i<amat->nz; i++) {
1834:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1835:       }
1836:       v = bmat->a;
1837:       for (i=0; i<bmat->nz; i++) {
1838:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1839:       }
1840:       MPIU_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
1841:       *norm = PetscSqrtReal(*norm);
1842:       PetscLogFlops(2.0*amat->nz+2.0*bmat->nz);
1843:     } else if (type == NORM_1) { /* max column norm */
1844:       PetscReal *tmp,*tmp2;
1845:       PetscInt  *jj,*garray = aij->garray;
1846:       PetscCalloc1(mat->cmap->N+1,&tmp);
1847:       PetscMalloc1(mat->cmap->N+1,&tmp2);
1848:       *norm = 0.0;
1849:       v     = amat->a; jj = amat->j;
1850:       for (j=0; j<amat->nz; j++) {
1851:         tmp[cstart + *jj++] += PetscAbsScalar(*v);  v++;
1852:       }
1853:       v = bmat->a; jj = bmat->j;
1854:       for (j=0; j<bmat->nz; j++) {
1855:         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
1856:       }
1857:       MPIU_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
1858:       for (j=0; j<mat->cmap->N; j++) {
1859:         if (tmp2[j] > *norm) *norm = tmp2[j];
1860:       }
1861:       PetscFree(tmp);
1862:       PetscFree(tmp2);
1863:       PetscLogFlops(PetscMax(amat->nz+bmat->nz-1,0));
1864:     } else if (type == NORM_INFINITY) { /* max row norm */
1865:       PetscReal ntemp = 0.0;
1866:       for (j=0; j<aij->A->rmap->n; j++) {
1867:         v   = amat->a + amat->i[j];
1868:         sum = 0.0;
1869:         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1870:           sum += PetscAbsScalar(*v); v++;
1871:         }
1872:         v = bmat->a + bmat->i[j];
1873:         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1874:           sum += PetscAbsScalar(*v); v++;
1875:         }
1876:         if (sum > ntemp) ntemp = sum;
1877:       }
1878:       MPIU_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));
1879:       PetscLogFlops(PetscMax(amat->nz+bmat->nz-1,0));
1880:     } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for two norm");
1881:   }
1882:   return(0);
1883: }

1885: PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout)
1886: {
1887:   Mat_MPIAIJ      *a    =(Mat_MPIAIJ*)A->data,*b;
1888:   Mat_SeqAIJ      *Aloc =(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data,*sub_B_diag;
1889:   PetscInt        M     = A->rmap->N,N=A->cmap->N,ma,na,mb,nb,row,*cols,*cols_tmp,*B_diag_ilen,i,ncol,A_diag_ncol;
1890:   const PetscInt  *ai,*aj,*bi,*bj,*B_diag_i;
1891:   PetscErrorCode  ierr;
1892:   Mat             B,A_diag,*B_diag;
1893:   const MatScalar *pbv,*bv;

1896:   ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; nb = a->B->cmap->n;
1897:   ai = Aloc->i; aj = Aloc->j;
1898:   bi = Bloc->i; bj = Bloc->j;
1899:   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
1900:     PetscInt             *d_nnz,*g_nnz,*o_nnz;
1901:     PetscSFNode          *oloc;
1902:     PETSC_UNUSED PetscSF sf;

1904:     PetscMalloc4(na,&d_nnz,na,&o_nnz,nb,&g_nnz,nb,&oloc);
1905:     /* compute d_nnz for preallocation */
1906:     PetscArrayzero(d_nnz,na);
1907:     for (i=0; i<ai[ma]; i++) {
1908:       d_nnz[aj[i]]++;
1909:     }
1910:     /* compute local off-diagonal contributions */
1911:     PetscArrayzero(g_nnz,nb);
1912:     for (i=0; i<bi[ma]; i++) g_nnz[bj[i]]++;
1913:     /* map those to global */
1914:     PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
1915:     PetscSFSetGraphLayout(sf,A->cmap,nb,NULL,PETSC_USE_POINTER,a->garray);
1916:     PetscSFSetFromOptions(sf);
1917:     PetscArrayzero(o_nnz,na);
1918:     PetscSFReduceBegin(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);
1919:     PetscSFReduceEnd(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);
1920:     PetscSFDestroy(&sf);

1922:     MatCreate(PetscObjectComm((PetscObject)A),&B);
1923:     MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);
1924:     MatSetBlockSizes(B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
1925:     MatSetType(B,((PetscObject)A)->type_name);
1926:     MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);
1927:     PetscFree4(d_nnz,o_nnz,g_nnz,oloc);
1928:   } else {
1929:     B    = *matout;
1930:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
1931:   }

1933:   b           = (Mat_MPIAIJ*)B->data;
1934:   A_diag      = a->A;
1935:   B_diag      = &b->A;
1936:   sub_B_diag  = (Mat_SeqAIJ*)(*B_diag)->data;
1937:   A_diag_ncol = A_diag->cmap->N;
1938:   B_diag_ilen = sub_B_diag->ilen;
1939:   B_diag_i    = sub_B_diag->i;

1941:   /* Set ilen for diagonal of B */
1942:   for (i=0; i<A_diag_ncol; i++) {
1943:     B_diag_ilen[i] = B_diag_i[i+1] - B_diag_i[i];
1944:   }

1946:   /* Transpose the diagonal part of the matrix. In contrast to the offdiagonal part, this can be done
1947:   very quickly (=without using MatSetValues), because all writes are local. */
1948:   MatTranspose(A_diag,MAT_REUSE_MATRIX,B_diag);

1950:   /* copy over the B part */
1951:   PetscMalloc1(bi[mb],&cols);
1952:   MatSeqAIJGetArrayRead(a->B,&bv);
1953:   pbv  = bv;
1954:   row  = A->rmap->rstart;
1955:   for (i=0; i<bi[mb]; i++) cols[i] = a->garray[bj[i]];
1956:   cols_tmp = cols;
1957:   for (i=0; i<mb; i++) {
1958:     ncol = bi[i+1]-bi[i];
1959:     MatSetValues(B,ncol,cols_tmp,1,&row,pbv,INSERT_VALUES);
1960:     row++;
1961:     pbv += ncol; cols_tmp += ncol;
1962:   }
1963:   PetscFree(cols);
1964:   MatSeqAIJRestoreArrayRead(a->B,&bv);

1966:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
1967:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
1968:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
1969:     *matout = B;
1970:   } else {
1971:     MatHeaderMerge(A,&B);
1972:   }
1973:   return(0);
1974: }

1976: PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1977: {
1978:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1979:   Mat            a    = aij->A,b = aij->B;
1981:   PetscInt       s1,s2,s3;

1984:   MatGetLocalSize(mat,&s2,&s3);
1985:   if (rr) {
1986:     VecGetLocalSize(rr,&s1);
1987:     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
1988:     /* Overlap communication with computation. */
1989:     VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);
1990:   }
1991:   if (ll) {
1992:     VecGetLocalSize(ll,&s1);
1993:     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1994:     (*b->ops->diagonalscale)(b,ll,NULL);
1995:   }
1996:   /* scale  the diagonal block */
1997:   (*a->ops->diagonalscale)(a,ll,rr);

1999:   if (rr) {
2000:     /* Do a scatter end and then right scale the off-diagonal block */
2001:     VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);
2002:     (*b->ops->diagonalscale)(b,NULL,aij->lvec);
2003:   }
2004:   return(0);
2005: }

2007: PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
2008: {
2009:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2013:   MatSetUnfactored(a->A);
2014:   return(0);
2015: }

2017: PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool  *flag)
2018: {
2019:   Mat_MPIAIJ     *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
2020:   Mat            a,b,c,d;
2021:   PetscBool      flg;

2025:   a = matA->A; b = matA->B;
2026:   c = matB->A; d = matB->B;

2028:   MatEqual(a,c,&flg);
2029:   if (flg) {
2030:     MatEqual(b,d,&flg);
2031:   }
2032:   MPIU_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
2033:   return(0);
2034: }

2036: PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
2037: {
2039:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2040:   Mat_MPIAIJ     *b = (Mat_MPIAIJ*)B->data;

2043:   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
2044:   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
2045:     /* because of the column compression in the off-processor part of the matrix a->B,
2046:        the number of columns in a->B and b->B may be different, hence we cannot call
2047:        the MatCopy() directly on the two parts. If need be, we can provide a more
2048:        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
2049:        then copying the submatrices */
2050:     MatCopy_Basic(A,B,str);
2051:   } else {
2052:     MatCopy(a->A,b->A,str);
2053:     MatCopy(a->B,b->B,str);
2054:   }
2055:   PetscObjectStateIncrease((PetscObject)B);
2056:   return(0);
2057: }

2059: PetscErrorCode MatSetUp_MPIAIJ(Mat A)
2060: {

2064:   MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,NULL,PETSC_DEFAULT,NULL);
2065:   return(0);
2066: }

2068: /*
2069:    Computes the number of nonzeros per row needed for preallocation when X and Y
2070:    have different nonzero structure.
2071: */
2072: PetscErrorCode MatAXPYGetPreallocation_MPIX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *xltog,const PetscInt *yi,const PetscInt *yj,const PetscInt *yltog,PetscInt *nnz)
2073: {
2074:   PetscInt       i,j,k,nzx,nzy;

2077:   /* Set the number of nonzeros in the new matrix */
2078:   for (i=0; i<m; i++) {
2079:     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2080:     nzx = xi[i+1] - xi[i];
2081:     nzy = yi[i+1] - yi[i];
2082:     nnz[i] = 0;
2083:     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2084:       for (; k<nzy && yltog[yjj[k]]<xltog[xjj[j]]; k++) nnz[i]++; /* Catch up to X */
2085:       if (k<nzy && yltog[yjj[k]]==xltog[xjj[j]]) k++;             /* Skip duplicate */
2086:       nnz[i]++;
2087:     }
2088:     for (; k<nzy; k++) nnz[i]++;
2089:   }
2090:   return(0);
2091: }

2093: /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */
2094: static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz)
2095: {
2097:   PetscInt       m = Y->rmap->N;
2098:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2099:   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;

2102:   MatAXPYGetPreallocation_MPIX_private(m,x->i,x->j,xltog,y->i,y->j,yltog,nnz);
2103:   return(0);
2104: }

2106: PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2107: {
2109:   Mat_MPIAIJ     *xx = (Mat_MPIAIJ*)X->data,*yy = (Mat_MPIAIJ*)Y->data;

2112:   if (str == SAME_NONZERO_PATTERN) {
2113:     MatAXPY(yy->A,a,xx->A,str);
2114:     MatAXPY(yy->B,a,xx->B,str);
2115:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2116:     MatAXPY_Basic(Y,a,X,str);
2117:   } else {
2118:     Mat      B;
2119:     PetscInt *nnz_d,*nnz_o;

2121:     PetscMalloc1(yy->A->rmap->N,&nnz_d);
2122:     PetscMalloc1(yy->B->rmap->N,&nnz_o);
2123:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
2124:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2125:     MatSetLayouts(B,Y->rmap,Y->cmap);
2126:     MatSetType(B,((PetscObject)Y)->type_name);
2127:     MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);
2128:     MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);
2129:     MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);
2130:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2131:     MatHeaderReplace(Y,&B);
2132:     PetscFree(nnz_d);
2133:     PetscFree(nnz_o);
2134:   }
2135:   return(0);
2136: }

2138: PETSC_INTERN PetscErrorCode MatConjugate_SeqAIJ(Mat);

2140: PetscErrorCode MatConjugate_MPIAIJ(Mat mat)
2141: {
2142: #if defined(PETSC_USE_COMPLEX)
2144:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

2147:   MatConjugate_SeqAIJ(aij->A);
2148:   MatConjugate_SeqAIJ(aij->B);
2149: #else
2151: #endif
2152:   return(0);
2153: }

2155: PetscErrorCode MatRealPart_MPIAIJ(Mat A)
2156: {
2157:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2161:   MatRealPart(a->A);
2162:   MatRealPart(a->B);
2163:   return(0);
2164: }

2166: PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
2167: {
2168:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2172:   MatImaginaryPart(a->A);
2173:   MatImaginaryPart(a->B);
2174:   return(0);
2175: }

2177: PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A,Vec v,PetscInt idx[])
2178: {
2179:   Mat_MPIAIJ        *a = (Mat_MPIAIJ*)A->data;
2180:   PetscErrorCode    ierr;
2181:   PetscInt          i,*idxb = NULL,m = A->rmap->n;
2182:   PetscScalar       *va,*vv;
2183:   Vec               vB,vA;
2184:   const PetscScalar *vb;

2187:   VecCreateSeq(PETSC_COMM_SELF,m,&vA);
2188:   MatGetRowMaxAbs(a->A,vA,idx);

2190:   VecGetArrayWrite(vA,&va);
2191:   if (idx) {
2192:     for (i=0; i<m; i++) {
2193:       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2194:     }
2195:   }

2197:   VecCreateSeq(PETSC_COMM_SELF,m,&vB);
2198:   PetscMalloc1(m,&idxb);
2199:   MatGetRowMaxAbs(a->B,vB,idxb);

2201:   VecGetArrayWrite(v,&vv);
2202:   VecGetArrayRead(vB,&vb);
2203:   for (i=0; i<m; i++) {
2204:     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
2205:       vv[i] = vb[i];
2206:       if (idx) idx[i] = a->garray[idxb[i]];
2207:     } else {
2208:       vv[i] = va[i];
2209:       if (idx && PetscAbsScalar(va[i]) == PetscAbsScalar(vb[i]) && idxb[i] != -1 && idx[i] > a->garray[idxb[i]])
2210:         idx[i] = a->garray[idxb[i]];
2211:     }
2212:   }
2213:   VecRestoreArrayWrite(vA,&vv);
2214:   VecRestoreArrayWrite(vA,&va);
2215:   VecRestoreArrayRead(vB,&vb);
2216:   PetscFree(idxb);
2217:   VecDestroy(&vA);
2218:   VecDestroy(&vB);
2219:   return(0);
2220: }

2222: PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2223: {
2224:   Mat_MPIAIJ        *mat   = (Mat_MPIAIJ*) A->data;
2225:   PetscInt          m = A->rmap->n,n = A->cmap->n;
2226:   PetscInt          cstart = A->cmap->rstart,cend = A->cmap->rend;
2227:   PetscInt          *cmap  = mat->garray;
2228:   PetscInt          *diagIdx, *offdiagIdx;
2229:   Vec               diagV, offdiagV;
2230:   PetscScalar       *a, *diagA, *offdiagA;
2231:   const PetscScalar *ba,*bav;
2232:   PetscInt          r,j,col,ncols,*bi,*bj;
2233:   PetscErrorCode    ierr;
2234:   Mat               B = mat->B;
2235:   Mat_SeqAIJ        *b = (Mat_SeqAIJ*)B->data;

2238:   /* When a process holds entire A and other processes have no entry */
2239:   if (A->cmap->N == n) {
2240:     VecGetArrayWrite(v,&diagA);
2241:     VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,diagA,&diagV);
2242:     MatGetRowMinAbs(mat->A,diagV,idx);
2243:     VecDestroy(&diagV);
2244:     VecRestoreArrayWrite(v,&diagA);
2245:     return(0);
2246:   } else if (n == 0) {
2247:     if (m) {
2248:       VecGetArrayWrite(v,&a);
2249:       for (r = 0; r < m; r++) {a[r] = 0.0; if (idx) idx[r] = -1;}
2250:       VecRestoreArrayWrite(v,&a);
2251:     }
2252:     return(0);
2253:   }

2255:   PetscMalloc2(m,&diagIdx,m,&offdiagIdx);
2256:   VecCreateSeq(PETSC_COMM_SELF, m, &diagV);
2257:   VecCreateSeq(PETSC_COMM_SELF, m, &offdiagV);
2258:   MatGetRowMinAbs(mat->A, diagV, diagIdx);

2260:   /* Get offdiagIdx[] for implicit 0.0 */
2261:   MatSeqAIJGetArrayRead(B,&bav);
2262:   ba   = bav;
2263:   bi   = b->i;
2264:   bj   = b->j;
2265:   VecGetArrayWrite(offdiagV, &offdiagA);
2266:   for (r = 0; r < m; r++) {
2267:     ncols = bi[r+1] - bi[r];
2268:     if (ncols == A->cmap->N - n) { /* Brow is dense */
2269:       offdiagA[r] = *ba; offdiagIdx[r] = cmap[0];
2270:     } else { /* Brow is sparse so already KNOW maximum is 0.0 or higher */
2271:       offdiagA[r] = 0.0;

2273:       /* Find first hole in the cmap */
2274:       for (j=0; j<ncols; j++) {
2275:         col = cmap[bj[j]]; /* global column number = cmap[B column number] */
2276:         if (col > j && j < cstart) {
2277:           offdiagIdx[r] = j; /* global column number of first implicit 0.0 */
2278:           break;
2279:         } else if (col > j + n && j >= cstart) {
2280:           offdiagIdx[r] = j + n; /* global column number of first implicit 0.0 */
2281:           break;
2282:         }
2283:       }
2284:       if (j == ncols && ncols < A->cmap->N - n) {
2285:         /* a hole is outside compressed Bcols */
2286:         if (ncols == 0) {
2287:           if (cstart) {
2288:             offdiagIdx[r] = 0;
2289:           } else offdiagIdx[r] = cend;
2290:         } else { /* ncols > 0 */
2291:           offdiagIdx[r] = cmap[ncols-1] + 1;
2292:           if (offdiagIdx[r] == cstart) offdiagIdx[r] += n;
2293:         }
2294:       }
2295:     }

2297:     for (j=0; j<ncols; j++) {
2298:       if (PetscAbsScalar(offdiagA[r]) > PetscAbsScalar(*ba)) {offdiagA[r] = *ba; offdiagIdx[r] = cmap[*bj];}
2299:       ba++; bj++;
2300:     }
2301:   }

2303:   VecGetArrayWrite(v, &a);
2304:   VecGetArrayRead(diagV, (const PetscScalar**)&diagA);
2305:   for (r = 0; r < m; ++r) {
2306:     if (PetscAbsScalar(diagA[r]) < PetscAbsScalar(offdiagA[r])) {
2307:       a[r]   = diagA[r];
2308:       if (idx) idx[r] = cstart + diagIdx[r];
2309:     } else if (PetscAbsScalar(diagA[r]) == PetscAbsScalar(offdiagA[r])) {
2310:       a[r] = diagA[r];
2311:       if (idx) {
2312:         if (cstart + diagIdx[r] <= offdiagIdx[r]) {
2313:           idx[r] = cstart + diagIdx[r];
2314:         } else idx[r] = offdiagIdx[r];
2315:       }
2316:     } else {
2317:       a[r]   = offdiagA[r];
2318:       if (idx) idx[r] = offdiagIdx[r];
2319:     }
2320:   }
2321:   MatSeqAIJRestoreArrayRead(B,&bav);
2322:   VecRestoreArrayWrite(v, &a);
2323:   VecRestoreArrayRead(diagV, (const PetscScalar**)&diagA);
2324:   VecRestoreArrayWrite(offdiagV, &offdiagA);
2325:   VecDestroy(&diagV);
2326:   VecDestroy(&offdiagV);
2327:   PetscFree2(diagIdx, offdiagIdx);
2328:   return(0);
2329: }

2331: PetscErrorCode MatGetRowMin_MPIAIJ(Mat A,Vec v,PetscInt idx[])
2332: {
2333:   Mat_MPIAIJ        *mat = (Mat_MPIAIJ*) A->data;
2334:   PetscInt          m = A->rmap->n,n = A->cmap->n;
2335:   PetscInt          cstart = A->cmap->rstart,cend = A->cmap->rend;
2336:   PetscInt          *cmap  = mat->garray;
2337:   PetscInt          *diagIdx, *offdiagIdx;
2338:   Vec               diagV, offdiagV;
2339:   PetscScalar       *a, *diagA, *offdiagA;
2340:   const PetscScalar *ba,*bav;
2341:   PetscInt          r,j,col,ncols,*bi,*bj;
2342:   PetscErrorCode    ierr;
2343:   Mat               B = mat->B;
2344:   Mat_SeqAIJ        *b = (Mat_SeqAIJ*)B->data;

2347:   /* When a process holds entire A and other processes have no entry */
2348:   if (A->cmap->N == n) {
2349:     VecGetArrayWrite(v,&diagA);
2350:     VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,diagA,&diagV);
2351:     MatGetRowMin(mat->A,diagV,idx);
2352:     VecDestroy(&diagV);
2353:     VecRestoreArrayWrite(v,&diagA);
2354:     return(0);
2355:   } else if (n == 0) {
2356:     if (m) {
2357:       VecGetArrayWrite(v,&a);
2358:       for (r = 0; r < m; r++) {a[r] = PETSC_MAX_REAL; if (idx) idx[r] = -1;}
2359:       VecRestoreArrayWrite(v,&a);
2360:     }
2361:     return(0);
2362:   }

2364:   PetscCalloc2(m,&diagIdx,m,&offdiagIdx);
2365:   VecCreateSeq(PETSC_COMM_SELF, m, &diagV);
2366:   VecCreateSeq(PETSC_COMM_SELF, m, &offdiagV);
2367:   MatGetRowMin(mat->A, diagV, diagIdx);

2369:   /* Get offdiagIdx[] for implicit 0.0 */
2370:   MatSeqAIJGetArrayRead(B,&bav);
2371:   ba   = bav;
2372:   bi   = b->i;
2373:   bj   = b->j;
2374:   VecGetArrayWrite(offdiagV, &offdiagA);
2375:   for (r = 0; r < m; r++) {
2376:     ncols = bi[r+1] - bi[r];
2377:     if (ncols == A->cmap->N - n) { /* Brow is dense */
2378:       offdiagA[r] = *ba; offdiagIdx[r] = cmap[0];
2379:     } else { /* Brow is sparse so already KNOW maximum is 0.0 or higher */
2380:       offdiagA[r] = 0.0;

2382:       /* Find first hole in the cmap */
2383:       for (j=0; j<ncols; j++) {
2384:         col = cmap[bj[j]]; /* global column number = cmap[B column number] */
2385:         if (col > j && j < cstart) {
2386:           offdiagIdx[r] = j; /* global column number of first implicit 0.0 */
2387:           break;
2388:         } else if (col > j + n && j >= cstart) {
2389:           offdiagIdx[r] = j + n; /* global column number of first implicit 0.0 */
2390:           break;
2391:         }
2392:       }
2393:       if (j == ncols && ncols < A->cmap->N - n) {
2394:         /* a hole is outside compressed Bcols */
2395:         if (ncols == 0) {
2396:           if (cstart) {
2397:             offdiagIdx[r] = 0;
2398:           } else offdiagIdx[r] = cend;
2399:         } else { /* ncols > 0 */
2400:           offdiagIdx[r] = cmap[ncols-1] + 1;
2401:           if (offdiagIdx[r] == cstart) offdiagIdx[r] += n;
2402:         }
2403:       }
2404:     }

2406:     for (j=0; j<ncols; j++) {
2407:       if (PetscRealPart(offdiagA[r]) > PetscRealPart(*ba)) {offdiagA[r] = *ba; offdiagIdx[r] = cmap[*bj];}
2408:       ba++; bj++;
2409:     }
2410:   }

2412:   VecGetArrayWrite(v, &a);
2413:   VecGetArrayRead(diagV, (const PetscScalar**)&diagA);
2414:   for (r = 0; r < m; ++r) {
2415:     if (PetscRealPart(diagA[r]) < PetscRealPart(offdiagA[r])) {
2416:       a[r]   = diagA[r];
2417:       if (idx) idx[r] = cstart + diagIdx[r];
2418:     } else if (PetscRealPart(diagA[r]) == PetscRealPart(offdiagA[r])) {
2419:       a[r] = diagA[r];
2420:       if (idx) {
2421:         if (cstart + diagIdx[r] <= offdiagIdx[r]) {
2422:           idx[r] = cstart + diagIdx[r];
2423:         } else idx[r] = offdiagIdx[r];
2424:       }
2425:     } else {
2426:       a[r]   = offdiagA[r];
2427:       if (idx) idx[r] = offdiagIdx[r];
2428:     }
2429:   }
2430:   MatSeqAIJRestoreArrayRead(B,&bav);
2431:   VecRestoreArrayWrite(v, &a);
2432:   VecRestoreArrayRead(diagV, (const PetscScalar**)&diagA);
2433:   VecRestoreArrayWrite(offdiagV, &offdiagA);
2434:   VecDestroy(&diagV);
2435:   VecDestroy(&offdiagV);
2436:   PetscFree2(diagIdx, offdiagIdx);
2437:   return(0);
2438: }

2440: PetscErrorCode MatGetRowMax_MPIAIJ(Mat A,Vec v,PetscInt idx[])
2441: {
2442:   Mat_MPIAIJ        *mat = (Mat_MPIAIJ*)A->data;
2443:   PetscInt          m = A->rmap->n,n = A->cmap->n;
2444:   PetscInt          cstart = A->cmap->rstart,cend = A->cmap->rend;
2445:   PetscInt          *cmap  = mat->garray;
2446:   PetscInt          *diagIdx, *offdiagIdx;
2447:   Vec               diagV, offdiagV;
2448:   PetscScalar       *a, *diagA, *offdiagA;
2449:   const PetscScalar *ba,*bav;
2450:   PetscInt          r,j,col,ncols,*bi,*bj;
2451:   PetscErrorCode    ierr;
2452:   Mat               B = mat->B;
2453:   Mat_SeqAIJ        *b = (Mat_SeqAIJ*)B->data;

2456:   /* When a process holds entire A and other processes have no entry */
2457:   if (A->cmap->N == n) {
2458:     VecGetArrayWrite(v,&diagA);
2459:     VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,diagA,&diagV);
2460:     MatGetRowMax(mat->A,diagV,idx);
2461:     VecDestroy(&diagV);
2462:     VecRestoreArrayWrite(v,&diagA);
2463:     return(0);
2464:   } else if (n == 0) {
2465:     if (m) {
2466:       VecGetArrayWrite(v,&a);
2467:       for (r = 0; r < m; r++) {a[r] = PETSC_MIN_REAL; if (idx) idx[r] = -1;}
2468:       VecRestoreArrayWrite(v,&a);
2469:     }
2470:     return(0);
2471:   }

2473:   PetscMalloc2(m,&diagIdx,m,&offdiagIdx);
2474:   VecCreateSeq(PETSC_COMM_SELF, m, &diagV);
2475:   VecCreateSeq(PETSC_COMM_SELF, m, &offdiagV);
2476:   MatGetRowMax(mat->A, diagV, diagIdx);

2478:   /* Get offdiagIdx[] for implicit 0.0 */
2479:   MatSeqAIJGetArrayRead(B,&bav);
2480:   ba   = bav;
2481:   bi   = b->i;
2482:   bj   = b->j;
2483:   VecGetArrayWrite(offdiagV, &offdiagA);
2484:   for (r = 0; r < m; r++) {
2485:     ncols = bi[r+1] - bi[r];
2486:     if (ncols == A->cmap->N - n) { /* Brow is dense */
2487:       offdiagA[r] = *ba; offdiagIdx[r] = cmap[0];
2488:     } else { /* Brow is sparse so already KNOW maximum is 0.0 or higher */
2489:       offdiagA[r] = 0.0;

2491:       /* Find first hole in the cmap */
2492:       for (j=0; j<ncols; j++) {
2493:         col = cmap[bj[j]]; /* global column number = cmap[B column number] */
2494:         if (col > j && j < cstart) {
2495:           offdiagIdx[r] = j; /* global column number of first implicit 0.0 */
2496:           break;
2497:         } else if (col > j + n && j >= cstart) {
2498:           offdiagIdx[r] = j + n; /* global column number of first implicit 0.0 */
2499:           break;
2500:         }
2501:       }
2502:       if (j == ncols && ncols < A->cmap->N - n) {
2503:         /* a hole is outside compressed Bcols */
2504:         if (ncols == 0) {
2505:           if (cstart) {
2506:             offdiagIdx[r] = 0;
2507:           } else offdiagIdx[r] = cend;
2508:         } else { /* ncols > 0 */
2509:           offdiagIdx[r] = cmap[ncols-1] + 1;
2510:           if (offdiagIdx[r] == cstart) offdiagIdx[r] += n;
2511:         }
2512:       }
2513:     }

2515:     for (j=0; j<ncols; j++) {
2516:       if (PetscRealPart(offdiagA[r]) < PetscRealPart(*ba)) {offdiagA[r] = *ba; offdiagIdx[r] = cmap[*bj];}
2517:       ba++; bj++;
2518:     }
2519:   }

2521:   VecGetArrayWrite(v,    &a);
2522:   VecGetArrayRead(diagV,(const PetscScalar**)&diagA);
2523:   for (r = 0; r < m; ++r) {
2524:     if (PetscRealPart(diagA[r]) > PetscRealPart(offdiagA[r])) {
2525:       a[r] = diagA[r];
2526:       if (idx) idx[r] = cstart + diagIdx[r];
2527:     } else if (PetscRealPart(diagA[r]) == PetscRealPart(offdiagA[r])) {
2528:       a[r] = diagA[r];
2529:       if (idx) {
2530:         if (cstart + diagIdx[r] <= offdiagIdx[r]) {
2531:           idx[r] = cstart + diagIdx[r];
2532:         } else idx[r] = offdiagIdx[r];
2533:       }
2534:     } else {
2535:       a[r] = offdiagA[r];
2536:       if (idx) idx[r] = offdiagIdx[r];
2537:     }
2538:   }
2539:   MatSeqAIJRestoreArrayRead(B,&bav);
2540:   VecRestoreArrayWrite(v,       &a);
2541:   VecRestoreArrayRead(diagV,   (const PetscScalar**)&diagA);
2542:   VecRestoreArrayWrite(offdiagV,&offdiagA);
2543:   VecDestroy(&diagV);
2544:   VecDestroy(&offdiagV);
2545:   PetscFree2(diagIdx, offdiagIdx);
2546:   return(0);
2547: }

2549: PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat)
2550: {
2552:   Mat            *dummy;

2555:   MatCreateSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);
2556:   *newmat = *dummy;
2557:   PetscFree(dummy);
2558:   return(0);
2559: }

2561: PetscErrorCode  MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values)
2562: {
2563:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*) A->data;

2567:   MatInvertBlockDiagonal(a->A,values);
2568:   A->factorerrortype = a->A->factorerrortype;
2569:   return(0);
2570: }

2572: static PetscErrorCode  MatSetRandom_MPIAIJ(Mat x,PetscRandom rctx)
2573: {
2575:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)x->data;

2578:   if (!x->assembled && !x->preallocated) SETERRQ(PetscObjectComm((PetscObject)x), PETSC_ERR_ARG_WRONGSTATE, "MatSetRandom on an unassembled and unpreallocated MATMPIAIJ is not allowed");
2579:   MatSetRandom(aij->A,rctx);
2580:   if (x->assembled) {
2581:     MatSetRandom(aij->B,rctx);
2582:   } else {
2583:     MatSetRandomSkipColumnRange_SeqAIJ_Private(aij->B,x->cmap->rstart,x->cmap->rend,rctx);
2584:   }
2585:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
2586:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
2587:   return(0);
2588: }

2590: PetscErrorCode MatMPIAIJSetUseScalableIncreaseOverlap_MPIAIJ(Mat A,PetscBool sc)
2591: {
2593:   if (sc) A->ops->increaseoverlap = MatIncreaseOverlap_MPIAIJ_Scalable;
2594:   else A->ops->increaseoverlap    = MatIncreaseOverlap_MPIAIJ;
2595:   return(0);
2596: }

2598: /*@
2599:    MatMPIAIJSetUseScalableIncreaseOverlap - Determine if the matrix uses a scalable algorithm to compute the overlap

2601:    Collective on Mat

2603:    Input Parameters:
2604: +    A - the matrix
2605: -    sc - PETSC_TRUE indicates use the scalable algorithm (default is not to use the scalable algorithm)

2607:  Level: advanced

2609: @*/
2610: PetscErrorCode MatMPIAIJSetUseScalableIncreaseOverlap(Mat A,PetscBool sc)
2611: {
2612:   PetscErrorCode       ierr;

2615:   PetscTryMethod(A,"MatMPIAIJSetUseScalableIncreaseOverlap_C",(Mat,PetscBool),(A,sc));
2616:   return(0);
2617: }

2619: PetscErrorCode MatSetFromOptions_MPIAIJ(PetscOptionItems *PetscOptionsObject,Mat A)
2620: {
2621:   PetscErrorCode       ierr;
2622:   PetscBool            sc = PETSC_FALSE,flg;

2625:   PetscOptionsHead(PetscOptionsObject,"MPIAIJ options");
2626:   if (A->ops->increaseoverlap == MatIncreaseOverlap_MPIAIJ_Scalable) sc = PETSC_TRUE;
2627:   PetscOptionsBool("-mat_increase_overlap_scalable","Use a scalable algorithm to compute the overlap","MatIncreaseOverlap",sc,&sc,&flg);
2628:   if (flg) {
2629:     MatMPIAIJSetUseScalableIncreaseOverlap(A,sc);
2630:   }
2631:   PetscOptionsTail();
2632:   return(0);
2633: }

2635: PetscErrorCode MatShift_MPIAIJ(Mat Y,PetscScalar a)
2636: {
2638:   Mat_MPIAIJ     *maij = (Mat_MPIAIJ*)Y->data;
2639:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)maij->A->data;

2642:   if (!Y->preallocated) {
2643:     MatMPIAIJSetPreallocation(Y,1,NULL,0,NULL);
2644:   } else if (!aij->nz) {
2645:     PetscInt nonew = aij->nonew;
2646:     MatSeqAIJSetPreallocation(maij->A,1,NULL);
2647:     aij->nonew = nonew;
2648:   }
2649:   MatShift_Basic(Y,a);
2650:   return(0);
2651: }

2653: PetscErrorCode MatMissingDiagonal_MPIAIJ(Mat A,PetscBool  *missing,PetscInt *d)
2654: {
2655:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2659:   if (A->rmap->n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only works for square matrices");
2660:   MatMissingDiagonal(a->A,missing,d);
2661:   if (d) {
2662:     PetscInt rstart;
2663:     MatGetOwnershipRange(A,&rstart,NULL);
2664:     *d += rstart;

2666:   }
2667:   return(0);
2668: }

2670: PetscErrorCode MatInvertVariableBlockDiagonal_MPIAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag)
2671: {
2672:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2676:   MatInvertVariableBlockDiagonal(a->A,nblocks,bsizes,diag);
2677:   return(0);
2678: }

2680: /* -------------------------------------------------------------------*/
2681: static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
2682:                                        MatGetRow_MPIAIJ,
2683:                                        MatRestoreRow_MPIAIJ,
2684:                                        MatMult_MPIAIJ,
2685:                                 /* 4*/ MatMultAdd_MPIAIJ,
2686:                                        MatMultTranspose_MPIAIJ,
2687:                                        MatMultTransposeAdd_MPIAIJ,
2688:                                        NULL,
2689:                                        NULL,
2690:                                        NULL,
2691:                                 /*10*/ NULL,
2692:                                        NULL,
2693:                                        NULL,
2694:                                        MatSOR_MPIAIJ,
2695:                                        MatTranspose_MPIAIJ,
2696:                                 /*15*/ MatGetInfo_MPIAIJ,
2697:                                        MatEqual_MPIAIJ,
2698:                                        MatGetDiagonal_MPIAIJ,
2699:                                        MatDiagonalScale_MPIAIJ,
2700:                                        MatNorm_MPIAIJ,
2701:                                 /*20*/ MatAssemblyBegin_MPIAIJ,
2702:                                        MatAssemblyEnd_MPIAIJ,
2703:                                        MatSetOption_MPIAIJ,
2704:                                        MatZeroEntries_MPIAIJ,
2705:                                 /*24*/ MatZeroRows_MPIAIJ,
2706:                                        NULL,
2707:                                        NULL,
2708:                                        NULL,
2709:                                        NULL,
2710:                                 /*29*/ MatSetUp_MPIAIJ,
2711:                                        NULL,
2712:                                        NULL,
2713:                                        MatGetDiagonalBlock_MPIAIJ,
2714:                                        NULL,
2715:                                 /*34*/ MatDuplicate_MPIAIJ,
2716:                                        NULL,
2717:                                        NULL,
2718:                                        NULL,
2719:                                        NULL,
2720:                                 /*39*/ MatAXPY_MPIAIJ,
2721:                                        MatCreateSubMatrices_MPIAIJ,
2722:                                        MatIncreaseOverlap_MPIAIJ,
2723:                                        MatGetValues_MPIAIJ,
2724:                                        MatCopy_MPIAIJ,
2725:                                 /*44*/ MatGetRowMax_MPIAIJ,
2726:                                        MatScale_MPIAIJ,
2727:                                        MatShift_MPIAIJ,
2728:                                        MatDiagonalSet_MPIAIJ,
2729:                                        MatZeroRowsColumns_MPIAIJ,
2730:                                 /*49*/ MatSetRandom_MPIAIJ,
2731:                                        NULL,
2732:                                        NULL,
2733:                                        NULL,
2734:                                        NULL,
2735:                                 /*54*/ MatFDColoringCreate_MPIXAIJ,
2736:                                        NULL,
2737:                                        MatSetUnfactored_MPIAIJ,
2738:                                        MatPermute_MPIAIJ,
2739:                                        NULL,
2740:                                 /*59*/ MatCreateSubMatrix_MPIAIJ,
2741:                                        MatDestroy_MPIAIJ,
2742:                                        MatView_MPIAIJ,
2743:                                        NULL,
2744:                                        NULL,
2745:                                 /*64*/ NULL,
2746:                                        MatMatMatMultNumeric_MPIAIJ_MPIAIJ_MPIAIJ,
2747:                                        NULL,
2748:                                        NULL,
2749:                                        NULL,
2750:                                 /*69*/ MatGetRowMaxAbs_MPIAIJ,
2751:                                        MatGetRowMinAbs_MPIAIJ,
2752:                                        NULL,
2753:                                        NULL,
2754:                                        NULL,
2755:                                        NULL,
2756:                                 /*75*/ MatFDColoringApply_AIJ,
2757:                                        MatSetFromOptions_MPIAIJ,
2758:                                        NULL,
2759:                                        NULL,
2760:                                        MatFindZeroDiagonals_MPIAIJ,
2761:                                 /*80*/ NULL,
2762:                                        NULL,
2763:                                        NULL,
2764:                                 /*83*/ MatLoad_MPIAIJ,
2765:                                        MatIsSymmetric_MPIAIJ,
2766:                                        NULL,
2767:                                        NULL,
2768:                                        NULL,
2769:                                        NULL,
2770:                                 /*89*/ NULL,
2771:                                        NULL,
2772:                                        MatMatMultNumeric_MPIAIJ_MPIAIJ,
2773:                                        NULL,
2774:                                        NULL,
2775:                                 /*94*/ MatPtAPNumeric_MPIAIJ_MPIAIJ,
2776:                                        NULL,
2777:                                        NULL,
2778:                                        NULL,
2779:                                        MatBindToCPU_MPIAIJ,
2780:                                 /*99*/ MatProductSetFromOptions_MPIAIJ,
2781:                                        NULL,
2782:                                        NULL,
2783:                                        MatConjugate_MPIAIJ,
2784:                                        NULL,
2785:                                 /*104*/MatSetValuesRow_MPIAIJ,
2786:                                        MatRealPart_MPIAIJ,
2787:                                        MatImaginaryPart_MPIAIJ,
2788:                                        NULL,
2789:                                        NULL,
2790:                                 /*109*/NULL,
2791:                                        NULL,
2792:                                        MatGetRowMin_MPIAIJ,
2793:                                        NULL,
2794:                                        MatMissingDiagonal_MPIAIJ,
2795:                                 /*114*/MatGetSeqNonzeroStructure_MPIAIJ,
2796:                                        NULL,
2797:                                        MatGetGhosts_MPIAIJ,
2798:                                        NULL,
2799:                                        NULL,
2800:                                 /*119*/MatMultDiagonalBlock_MPIAIJ,
2801:                                        NULL,
2802:                                        NULL,
2803:                                        NULL,
2804:                                        MatGetMultiProcBlock_MPIAIJ,
2805:                                 /*124*/MatFindNonzeroRows_MPIAIJ,
2806:                                        MatGetColumnReductions_MPIAIJ,
2807:                                        MatInvertBlockDiagonal_MPIAIJ,
2808:                                        MatInvertVariableBlockDiagonal_MPIAIJ,
2809:                                        MatCreateSubMatricesMPI_MPIAIJ,
2810:                                 /*129*/NULL,
2811:                                        NULL,
2812:                                        NULL,
2813:                                        MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ,
2814:                                        NULL,
2815:                                 /*134*/NULL,
2816:                                        NULL,
2817:                                        NULL,
2818:                                        NULL,
2819:                                        NULL,
2820:                                 /*139*/MatSetBlockSizes_MPIAIJ,
2821:                                        NULL,
2822:                                        NULL,
2823:                                        MatFDColoringSetUp_MPIXAIJ,
2824:                                        MatFindOffBlockDiagonalEntries_MPIAIJ,
2825:                                        MatCreateMPIMatConcatenateSeqMat_MPIAIJ,
2826:                                 /*145*/NULL,
2827:                                        NULL,
2828:                                        NULL
2829: };

2831: /* ----------------------------------------------------------------------------------------*/

2833: PetscErrorCode  MatStoreValues_MPIAIJ(Mat mat)
2834: {
2835:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

2839:   MatStoreValues(aij->A);
2840:   MatStoreValues(aij->B);
2841:   return(0);
2842: }

2844: PetscErrorCode  MatRetrieveValues_MPIAIJ(Mat mat)
2845: {
2846:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

2850:   MatRetrieveValues(aij->A);
2851:   MatRetrieveValues(aij->B);
2852:   return(0);
2853: }

2855: PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
2856: {
2857:   Mat_MPIAIJ     *b;
2859:   PetscMPIInt    size;

2862:   PetscLayoutSetUp(B->rmap);
2863:   PetscLayoutSetUp(B->cmap);
2864:   b = (Mat_MPIAIJ*)B->data;

2866: #if defined(PETSC_USE_CTABLE)
2867:   PetscTableDestroy(&b->colmap);
2868: #else
2869:   PetscFree(b->colmap);
2870: #endif
2871:   PetscFree(b->garray);
2872:   VecDestroy(&b->lvec);
2873:   VecScatterDestroy(&b->Mvctx);

2875:   /* Because the B will have been resized we simply destroy it and create a new one each time */
2876:   MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);
2877:   MatDestroy(&b->B);
2878:   MatCreate(PETSC_COMM_SELF,&b->B);
2879:   MatSetSizes(b->B,B->rmap->n,size > 1 ? B->cmap->N : 0,B->rmap->n,size > 1 ? B->cmap->N : 0);
2880:   MatSetBlockSizesFromMats(b->B,B,B);
2881:   MatSetType(b->B,MATSEQAIJ);
2882:   PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);

2884:   if (!B->preallocated) {
2885:     MatCreate(PETSC_COMM_SELF,&b->A);
2886:     MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
2887:     MatSetBlockSizesFromMats(b->A,B,B);
2888:     MatSetType(b->A,MATSEQAIJ);
2889:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
2890:   }

2892:   MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);
2893:   MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);
2894:   B->preallocated  = PETSC_TRUE;
2895:   B->was_assembled = PETSC_FALSE;
2896:   B->assembled     = PETSC_FALSE;
2897:   return(0);
2898: }

2900: PetscErrorCode MatResetPreallocation_MPIAIJ(Mat B)
2901: {
2902:   Mat_MPIAIJ     *b;

2907:   PetscLayoutSetUp(B->rmap);
2908:   PetscLayoutSetUp(B->cmap);
2909:   b = (Mat_MPIAIJ*)B->data;

2911: #if defined(PETSC_USE_CTABLE)
2912:   PetscTableDestroy(&b->colmap);
2913: #else
2914:   PetscFree(b->colmap);
2915: #endif
2916:   PetscFree(b->garray);
2917:   VecDestroy(&b->lvec);
2918:   VecScatterDestroy(&b->Mvctx);

2920:   MatResetPreallocation(b->A);
2921:   MatResetPreallocation(b->B);
2922:   B->preallocated  = PETSC_TRUE;
2923:   B->was_assembled = PETSC_FALSE;
2924:   B->assembled = PETSC_FALSE;
2925:   return(0);
2926: }

2928: PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
2929: {
2930:   Mat            mat;
2931:   Mat_MPIAIJ     *a,*oldmat = (Mat_MPIAIJ*)matin->data;

2935:   *newmat = NULL;
2936:   MatCreate(PetscObjectComm((PetscObject)matin),&mat);
2937:   MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);
2938:   MatSetBlockSizesFromMats(mat,matin,matin);
2939:   MatSetType(mat,((PetscObject)matin)->type_name);
2940:   a       = (Mat_MPIAIJ*)mat->data;

2942:   mat->factortype   = matin->factortype;
2943:   mat->assembled    = matin->assembled;
2944:   mat->insertmode   = NOT_SET_VALUES;
2945:   mat->preallocated = matin->preallocated;

2947:   a->size         = oldmat->size;
2948:   a->rank         = oldmat->rank;
2949:   a->donotstash   = oldmat->donotstash;
2950:   a->roworiented  = oldmat->roworiented;
2951:   a->rowindices   = NULL;
2952:   a->rowvalues    = NULL;
2953:   a->getrowactive = PETSC_FALSE;

2955:   PetscLayoutReference(matin->rmap,&mat->rmap);
2956:   PetscLayoutReference(matin->cmap,&mat->cmap);

2958:   if (oldmat->colmap) {
2959: #if defined(PETSC_USE_CTABLE)
2960:     PetscTableCreateCopy(oldmat->colmap,&a->colmap);
2961: #else
2962:     PetscMalloc1(mat->cmap->N,&a->colmap);
2963:     PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N)*sizeof(PetscInt));
2964:     PetscArraycpy(a->colmap,oldmat->colmap,mat->cmap->N);
2965: #endif
2966:   } else a->colmap = NULL;
2967:   if (oldmat->garray) {
2968:     PetscInt len;
2969:     len  = oldmat->B->cmap->n;
2970:     PetscMalloc1(len+1,&a->garray);
2971:     PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));
2972:     if (len) { PetscArraycpy(a->garray,oldmat->garray,len); }
2973:   } else a->garray = NULL;

2975:   /* It may happen MatDuplicate is called with a non-assembled matrix
2976:      In fact, MatDuplicate only requires the matrix to be preallocated
2977:      This may happen inside a DMCreateMatrix_Shell */
2978:   if (oldmat->lvec) {
2979:     VecDuplicate(oldmat->lvec,&a->lvec);
2980:     PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);
2981:   }
2982:   if (oldmat->Mvctx) {
2983:     VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
2984:     PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);
2985:   }
2986:   MatDuplicate(oldmat->A,cpvalues,&a->A);
2987:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
2988:   MatDuplicate(oldmat->B,cpvalues,&a->B);
2989:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);
2990:   PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);
2991:   *newmat = mat;
2992:   return(0);
2993: }

2995: PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer)
2996: {
2997:   PetscBool      isbinary, ishdf5;

3003:   /* force binary viewer to load .info file if it has not yet done so */
3004:   PetscViewerSetUp(viewer);
3005:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
3006:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,  &ishdf5);
3007:   if (isbinary) {
3008:     MatLoad_MPIAIJ_Binary(newMat,viewer);
3009:   } else if (ishdf5) {
3010: #if defined(PETSC_HAVE_HDF5)
3011:     MatLoad_AIJ_HDF5(newMat,viewer);
3012: #else
3013:     SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
3014: #endif
3015:   } else {
3016:     SETERRQ2(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"Viewer type %s not yet supported for reading %s matrices",((PetscObject)viewer)->type_name,((PetscObject)newMat)->type_name);
3017:   }
3018:   return(0);
3019: }

3021: PetscErrorCode MatLoad_MPIAIJ_Binary(Mat mat, PetscViewer viewer)
3022: {
3023:   PetscInt       header[4],M,N,m,nz,rows,cols,sum,i;
3024:   PetscInt       *rowidxs,*colidxs;
3025:   PetscScalar    *matvals;

3029:   PetscViewerSetUp(viewer);

3031:   /* read in matrix header */
3032:   PetscViewerBinaryRead(viewer,header,4,NULL,PETSC_INT);
3033:   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Not a matrix object in file");
3034:   M  = header[1]; N = header[2]; nz = header[3];
3035:   if (M < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix row size (%D) in file is negative",M);
3036:   if (N < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix column size (%D) in file is negative",N);
3037:   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk, cannot load as MPIAIJ");

3039:   /* set block sizes from the viewer's .info file */
3040:   MatLoad_Binary_BlockSizes(mat,viewer);
3041:   /* set global sizes if not set already */
3042:   if (mat->rmap->N < 0) mat->rmap->N = M;
3043:   if (mat->cmap->N < 0) mat->cmap->N = N;
3044:   PetscLayoutSetUp(mat->rmap);
3045:   PetscLayoutSetUp(mat->cmap);

3047:   /* check if the matrix sizes are correct */
3048:   MatGetSize(mat,&rows,&cols);
3049:   if (M != rows || N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different sizes (%D, %D) than the input matrix (%D, %D)",M,N,rows,cols);

3051:   /* read in row lengths and build row indices */
3052:   MatGetLocalSize(mat,&m,NULL);
3053:   PetscMalloc1(m+1,&rowidxs);
3054:   PetscViewerBinaryReadAll(viewer,rowidxs+1,m,PETSC_DECIDE,M,PETSC_INT);
3055:   rowidxs[0] = 0; for (i=0; i<m; i++) rowidxs[i+1] += rowidxs[i];
3056:   MPIU_Allreduce(&rowidxs[m],&sum,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)viewer));
3057:   if (sum != nz) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Inconsistent matrix data in file: nonzeros = %D, sum-row-lengths = %D\n",nz,sum);
3058:   /* read in column indices and matrix values */
3059:   PetscMalloc2(rowidxs[m],&colidxs,rowidxs[m],&matvals);
3060:   PetscViewerBinaryReadAll(viewer,colidxs,rowidxs[m],PETSC_DETERMINE,PETSC_DETERMINE,PETSC_INT);
3061:   PetscViewerBinaryReadAll(viewer,matvals,rowidxs[m],PETSC_DETERMINE,PETSC_DETERMINE,PETSC_SCALAR);
3062:   /* store matrix indices and values */
3063:   MatMPIAIJSetPreallocationCSR(mat,rowidxs,colidxs,matvals);
3064:   PetscFree(rowidxs);
3065:   PetscFree2(colidxs,matvals);
3066:   return(0);
3067: }

3069: /* Not scalable because of ISAllGather() unless getting all columns. */
3070: PetscErrorCode ISGetSeqIS_Private(Mat mat,IS iscol,IS *isseq)
3071: {
3073:   IS             iscol_local;
3074:   PetscBool      isstride;
3075:   PetscMPIInt    lisstride=0,gisstride;

3078:   /* check if we are grabbing all columns*/
3079:   PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&isstride);

3081:   if (isstride) {
3082:     PetscInt  start,len,mstart,mlen;
3083:     ISStrideGetInfo(iscol,&start,NULL);
3084:     ISGetLocalSize(iscol,&len);
3085:     MatGetOwnershipRangeColumn(mat,&mstart,&mlen);
3086:     if (mstart == start && mlen-mstart == len) lisstride = 1;
3087:   }

3089:   MPIU_Allreduce(&lisstride,&gisstride,1,MPI_INT,MPI_MIN,PetscObjectComm((PetscObject)mat));
3090:   if (gisstride) {
3091:     PetscInt N;
3092:     MatGetSize(mat,NULL,&N);
3093:     ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol_local);
3094:     ISSetIdentity(iscol_local);
3095:     PetscInfo(mat,"Optimizing for obtaining all columns of the matrix; skipping ISAllGather()\n");
3096:   } else {
3097:     PetscInt cbs;
3098:     ISGetBlockSize(iscol,&cbs);
3099:     ISAllGather(iscol,&iscol_local);
3100:     ISSetBlockSize(iscol_local,cbs);
3101:   }

3103:   *isseq = iscol_local;
3104:   return(0);
3105: }

3107: /*
3108:  Used by MatCreateSubMatrix_MPIAIJ_SameRowColDist() to avoid ISAllGather() and global size of iscol_local
3109:  (see MatCreateSubMatrix_MPIAIJ_nonscalable)

3111:  Input Parameters:
3112:    mat - matrix
3113:    isrow - parallel row index set; its local indices are a subset of local columns of mat,
3114:            i.e., mat->rstart <= isrow[i] < mat->rend
3115:    iscol - parallel column index set; its local indices are a subset of local columns of mat,
3116:            i.e., mat->cstart <= iscol[i] < mat->cend
3117:  Output Parameter:
3118:    isrow_d,iscol_d - sequential row and column index sets for retrieving mat->A
3119:    iscol_o - sequential column index set for retrieving mat->B
3120:    garray - column map; garray[i] indicates global location of iscol_o[i] in iscol
3121:  */
3122: PetscErrorCode ISGetSeqIS_SameColDist_Private(Mat mat,IS isrow,IS iscol,IS *isrow_d,IS *iscol_d,IS *iscol_o,const PetscInt *garray[])
3123: {
3125:   Vec            x,cmap;
3126:   const PetscInt *is_idx;
3127:   PetscScalar    *xarray,*cmaparray;
3128:   PetscInt       ncols,isstart,*idx,m,rstart,*cmap1,count;
3129:   Mat_MPIAIJ     *a=(Mat_MPIAIJ*)mat->data;
3130:   Mat            B=a->B;
3131:   Vec            lvec=a->lvec,lcmap;
3132:   PetscInt       i,cstart,cend,Bn=B->cmap->N;
3133:   MPI_Comm       comm;
3134:   VecScatter     Mvctx=a->Mvctx;

3137:   PetscObjectGetComm((PetscObject)mat,&comm);
3138:   ISGetLocalSize(iscol,&ncols);

3140:   /* (1) iscol is a sub-column vector of mat, pad it with '-1.' to form a full vector x */
3141:   MatCreateVecs(mat,&x,NULL);
3142:   VecSet(x,-1.0);
3143:   VecDuplicate(x,&cmap);
3144:   VecSet(cmap,-1.0);

3146:   /* Get start indices */
3147:   MPI_Scan(&ncols,&isstart,1,MPIU_INT,MPI_SUM,comm);
3148:   isstart -= ncols;
3149:   MatGetOwnershipRangeColumn(mat,&cstart,&cend);

3151:   ISGetIndices(iscol,&is_idx);
3152:   VecGetArray(x,&xarray);
3153:   VecGetArray(cmap,&cmaparray);
3154:   PetscMalloc1(ncols,&idx);
3155:   for (i=0; i<ncols; i++) {
3156:     xarray[is_idx[i]-cstart]    = (PetscScalar)is_idx[i];
3157:     cmaparray[is_idx[i]-cstart] = i + isstart;      /* global index of iscol[i] */
3158:     idx[i]                      = is_idx[i]-cstart; /* local index of iscol[i]  */
3159:   }
3160:   VecRestoreArray(x,&xarray);
3161:   VecRestoreArray(cmap,&cmaparray);
3162:   ISRestoreIndices(iscol,&is_idx);

3164:   /* Get iscol_d */
3165:   ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,iscol_d);
3166:   ISGetBlockSize(iscol,&i);
3167:   ISSetBlockSize(*iscol_d,i);

3169:   /* Get isrow_d */
3170:   ISGetLocalSize(isrow,&m);
3171:   rstart = mat->rmap->rstart;
3172:   PetscMalloc1(m,&idx);
3173:   ISGetIndices(isrow,&is_idx);
3174:   for (i=0; i<m; i++) idx[i] = is_idx[i]-rstart;
3175:   ISRestoreIndices(isrow,&is_idx);

3177:   ISCreateGeneral(PETSC_COMM_SELF,m,idx,PETSC_OWN_POINTER,isrow_d);
3178:   ISGetBlockSize(isrow,&i);
3179:   ISSetBlockSize(*isrow_d,i);

3181:   /* (2) Scatter x and cmap using aij->Mvctx to get their off-process portions (see MatMult_MPIAIJ) */
3182:   VecScatterBegin(Mvctx,x,lvec,INSERT_VALUES,SCATTER_FORWARD);
3183:   VecScatterEnd(Mvctx,x,lvec,INSERT_VALUES,SCATTER_FORWARD);

3185:   VecDuplicate(lvec,&lcmap);

3187:   VecScatterBegin(Mvctx,cmap,lcmap,INSERT_VALUES,SCATTER_FORWARD);
3188:   VecScatterEnd(Mvctx,cmap,lcmap,INSERT_VALUES,SCATTER_FORWARD);

3190:   /* (3) create sequential iscol_o (a subset of iscol) and isgarray */
3191:   /* off-process column indices */
3192:   count = 0;
3193:   PetscMalloc1(Bn,&idx);
3194:   PetscMalloc1(Bn,&cmap1);

3196:   VecGetArray(lvec,&xarray);
3197:   VecGetArray(lcmap,&cmaparray);
3198:   for (i=0; i<Bn; i++) {
3199:     if (PetscRealPart(xarray[i]) > -1.0) {
3200:       idx[count]     = i;                   /* local column index in off-diagonal part B */
3201:       cmap1[count] = (PetscInt)PetscRealPart(cmaparray[i]);  /* column index in submat */
3202:       count++;
3203:     }
3204:   }
3205:   VecRestoreArray(lvec,&xarray);
3206:   VecRestoreArray(lcmap,&cmaparray);

3208:   ISCreateGeneral(PETSC_COMM_SELF,count,idx,PETSC_COPY_VALUES,iscol_o);
3209:   /* cannot ensure iscol_o has same blocksize as iscol! */

3211:   PetscFree(idx);
3212:   *garray = cmap1;

3214:   VecDestroy(&x);
3215:   VecDestroy(&cmap);
3216:   VecDestroy(&lcmap);
3217:   return(0);
3218: }

3220: /* isrow and iscol have same processor distribution as mat, output *submat is a submatrix of local mat */
3221: PetscErrorCode MatCreateSubMatrix_MPIAIJ_SameRowColDist(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *submat)
3222: {
3224:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)mat->data,*asub;
3225:   Mat            M = NULL;
3226:   MPI_Comm       comm;
3227:   IS             iscol_d,isrow_d,iscol_o;
3228:   Mat            Asub = NULL,Bsub = NULL;
3229:   PetscInt       n;

3232:   PetscObjectGetComm((PetscObject)mat,&comm);

3234:   if (call == MAT_REUSE_MATRIX) {
3235:     /* Retrieve isrow_d, iscol_d and iscol_o from submat */
3236:     PetscObjectQuery((PetscObject)*submat,"isrow_d",(PetscObject*)&isrow_d);
3237:     if (!isrow_d) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"isrow_d passed in was not used before, cannot reuse");

3239:     PetscObjectQuery((PetscObject)*submat,"iscol_d",(PetscObject*)&iscol_d);
3240:     if (!iscol_d) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"iscol_d passed in was not used before, cannot reuse");

3242:     PetscObjectQuery((PetscObject)*submat,"iscol_o",(PetscObject*)&iscol_o);
3243:     if (!iscol_o) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"iscol_o passed in was not used before, cannot reuse");

3245:     /* Update diagonal and off-diagonal portions of submat */
3246:     asub = (Mat_MPIAIJ*)(*submat)->data;
3247:     MatCreateSubMatrix_SeqAIJ(a->A,isrow_d,iscol_d,PETSC_DECIDE,MAT_REUSE_MATRIX,&asub->A);
3248:     ISGetLocalSize(iscol_o,&n);
3249:     if (n) {
3250:       MatCreateSubMatrix_SeqAIJ(a->B,isrow_d,iscol_o,PETSC_DECIDE,MAT_REUSE_MATRIX,&asub->B);
3251:     }
3252:     MatAssemblyBegin(*submat,MAT_FINAL_ASSEMBLY);
3253:     MatAssemblyEnd(*submat,MAT_FINAL_ASSEMBLY);

3255:   } else { /* call == MAT_INITIAL_MATRIX) */
3256:     const PetscInt *garray;
3257:     PetscInt        BsubN;

3259:     /* Create isrow_d, iscol_d, iscol_o and isgarray (replace isgarray with array?) */
3260:     ISGetSeqIS_SameColDist_Private(mat,isrow,iscol,&isrow_d,&iscol_d,&iscol_o,&garray);

3262:     /* Create local submatrices Asub and Bsub */
3263:     MatCreateSubMatrix_SeqAIJ(a->A,isrow_d,iscol_d,PETSC_DECIDE,MAT_INITIAL_MATRIX,&Asub);
3264:     MatCreateSubMatrix_SeqAIJ(a->B,isrow_d,iscol_o,PETSC_DECIDE,MAT_INITIAL_MATRIX,&Bsub);

3266:     /* Create submatrix M */
3267:     MatCreateMPIAIJWithSeqAIJ(comm,Asub,Bsub,garray,&M);

3269:     /* If Bsub has empty columns, compress iscol_o such that it will retrieve condensed Bsub from a->B during reuse */
3270:     asub = (Mat_MPIAIJ*)M->data;

3272:     ISGetLocalSize(iscol_o,&BsubN);
3273:     n = asub->B->cmap->N;
3274:     if (BsubN > n) {
3275:       /* This case can be tested using ~petsc/src/tao/bound/tutorials/runplate2_3 */
3276:       const PetscInt *idx;
3277:       PetscInt       i,j,*idx_new,*subgarray = asub->garray;
3278:       PetscInfo2(M,"submatrix Bn %D != BsubN %D, update iscol_o\n",n,BsubN);

3280:       PetscMalloc1(n,&idx_new);
3281:       j = 0;
3282:       ISGetIndices(iscol_o,&idx);
3283:       for (i=0; i<n; i++) {
3284:         if (j >= BsubN) break;
3285:         while (subgarray[i] > garray[j]) j++;

3287:         if (subgarray[i] == garray[j]) {
3288:           idx_new[i] = idx[j++];
3289:         } else SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"subgarray[%D]=%D cannot < garray[%D]=%D",i,subgarray[i],j,garray[j]);
3290:       }
3291:       ISRestoreIndices(iscol_o,&idx);

3293:       ISDestroy(&iscol_o);
3294:       ISCreateGeneral(PETSC_COMM_SELF,n,idx_new,PETSC_OWN_POINTER,&iscol_o);

3296:     } else if (BsubN < n) {
3297:       SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Columns of Bsub cannot be smaller than B's",BsubN,asub->B->cmap->N);
3298:     }

3300:     PetscFree(garray);
3301:     *submat = M;

3303:     /* Save isrow_d, iscol_d and iscol_o used in processor for next request */
3304:     PetscObjectCompose((PetscObject)M,"isrow_d",(PetscObject)isrow_d);
3305:     ISDestroy(&isrow_d);

3307:     PetscObjectCompose((PetscObject)M,"iscol_d",(PetscObject)iscol_d);
3308:     ISDestroy(&iscol_d);

3310:     PetscObjectCompose((PetscObject)M,"iscol_o",(PetscObject)iscol_o);
3311:     ISDestroy(&iscol_o);
3312:   }
3313:   return(0);
3314: }

3316: PetscErrorCode MatCreateSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
3317: {
3319:   IS             iscol_local=NULL,isrow_d;
3320:   PetscInt       csize;
3321:   PetscInt       n,i,j,start,end;
3322:   PetscBool      sameRowDist=PETSC_FALSE,sameDist[2],tsameDist[2];
3323:   MPI_Comm       comm;

3326:   /* If isrow has same processor distribution as mat,
3327:      call MatCreateSubMatrix_MPIAIJ_SameRowDist() to avoid using a hash table with global size of iscol */
3328:   if (call == MAT_REUSE_MATRIX) {
3329:     PetscObjectQuery((PetscObject)*newmat,"isrow_d",(PetscObject*)&isrow_d);
3330:     if (isrow_d) {
3331:       sameRowDist  = PETSC_TRUE;
3332:       tsameDist[1] = PETSC_TRUE; /* sameColDist */
3333:     } else {
3334:       PetscObjectQuery((PetscObject)*newmat,"SubIScol",(PetscObject*)&iscol_local);
3335:       if (iscol_local) {
3336:         sameRowDist  = PETSC_TRUE;
3337:         tsameDist[1] = PETSC_FALSE; /* !sameColDist */
3338:       }
3339:     }
3340:   } else {
3341:     /* Check if isrow has same processor distribution as mat */
3342:     sameDist[0] = PETSC_FALSE;
3343:     ISGetLocalSize(isrow,&n);
3344:     if (!n) {
3345:       sameDist[0] = PETSC_TRUE;
3346:     } else {
3347:       ISGetMinMax(isrow,&i,&j);
3348:       MatGetOwnershipRange(mat,&start,&end);
3349:       if (i >= start && j < end) {
3350:         sameDist[0] = PETSC_TRUE;
3351:       }
3352:     }

3354:     /* Check if iscol has same processor distribution as mat */
3355:     sameDist[1] = PETSC_FALSE;
3356:     ISGetLocalSize(iscol,&n);
3357:     if (!n) {
3358:       sameDist[1] = PETSC_TRUE;
3359:     } else {
3360:       ISGetMinMax(iscol,&i,&j);
3361:       MatGetOwnershipRangeColumn(mat,&start,&end);
3362:       if (i >= start && j < end) sameDist[1] = PETSC_TRUE;
3363:     }

3365:     PetscObjectGetComm((PetscObject)mat,&comm);
3366:     MPIU_Allreduce(&sameDist,&tsameDist,2,MPIU_BOOL,MPI_LAND,comm);
3367:     sameRowDist = tsameDist[0];
3368:   }

3370:   if (sameRowDist) {
3371:     if (tsameDist[1]) { /* sameRowDist & sameColDist */
3372:       /* isrow and iscol have same processor distribution as mat */
3373:       MatCreateSubMatrix_MPIAIJ_SameRowColDist(mat,isrow,iscol,call,newmat);
3374:       return(0);
3375:     } else { /* sameRowDist */
3376:       /* isrow has same processor distribution as mat */
3377:       if (call == MAT_INITIAL_MATRIX) {
3378:         PetscBool sorted;
3379:         ISGetSeqIS_Private(mat,iscol,&iscol_local);
3380:         ISGetLocalSize(iscol_local,&n); /* local size of iscol_local = global columns of newmat */
3381:         ISGetSize(iscol,&i);
3382:         if (n != i) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"n %d != size of iscol %d",n,i);

3384:         ISSorted(iscol_local,&sorted);
3385:         if (sorted) {
3386:           /* MatCreateSubMatrix_MPIAIJ_SameRowDist() requires iscol_local be sorted; it can have duplicate indices */
3387:           MatCreateSubMatrix_MPIAIJ_SameRowDist(mat,isrow,iscol,iscol_local,MAT_INITIAL_MATRIX,newmat);
3388:           return(0);
3389:         }
3390:       } else { /* call == MAT_REUSE_MATRIX */
3391:         IS iscol_sub;
3392:         PetscObjectQuery((PetscObject)*newmat,"SubIScol",(PetscObject*)&iscol_sub);
3393:         if (iscol_sub) {
3394:           MatCreateSubMatrix_MPIAIJ_SameRowDist(mat,isrow,iscol,NULL,call,newmat);
3395:           return(0);
3396:         }
3397:       }
3398:     }
3399:   }

3401:   /* General case: iscol -> iscol_local which has global size of iscol */
3402:   if (call == MAT_REUSE_MATRIX) {
3403:     PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);
3404:     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3405:   } else {
3406:     if (!iscol_local) {
3407:       ISGetSeqIS_Private(mat,iscol,&iscol_local);
3408:     }
3409:   }

3411:   ISGetLocalSize(iscol,&csize);
3412:   MatCreateSubMatrix_MPIAIJ_nonscalable(mat,isrow,iscol_local,csize,call,newmat);

3414:   if (call == MAT_INITIAL_MATRIX) {
3415:     PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);
3416:     ISDestroy(&iscol_local);
3417:   }
3418:   return(0);
3419: }

3421: /*@C
3422:      MatCreateMPIAIJWithSeqAIJ - creates a MPIAIJ matrix using SeqAIJ matrices that contain the "diagonal"
3423:          and "off-diagonal" part of the matrix in CSR format.

3425:    Collective

3427:    Input Parameters:
3428: +  comm - MPI communicator
3429: .  A - "diagonal" portion of matrix
3430: .  B - "off-diagonal" portion of matrix, may have empty columns, will be destroyed by this routine
3431: -  garray - global index of B columns

3433:    Output Parameter:
3434: .   mat - the matrix, with input A as its local diagonal matrix
3435:    Level: advanced

3437:    Notes:
3438:        See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix.
3439:        A becomes part of output mat, B is destroyed by this routine. The user cannot use A and B anymore.

3441: .seealso: MatCreateMPIAIJWithSplitArrays()
3442: @*/
3443: PetscErrorCode MatCreateMPIAIJWithSeqAIJ(MPI_Comm comm,Mat A,Mat B,const PetscInt garray[],Mat *mat)
3444: {
3445:   PetscErrorCode    ierr;
3446:   Mat_MPIAIJ        *maij;
3447:   Mat_SeqAIJ        *b=(Mat_SeqAIJ*)B->data,*bnew;
3448:   PetscInt          *oi=b->i,*oj=b->j,i,nz,col;
3449:   const PetscScalar *oa;
3450:   Mat               Bnew;
3451:   PetscInt          m,n,N;

3454:   MatCreate(comm,mat);
3455:   MatGetSize(A,&m,&n);
3456:   if (m != B->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Am %D != Bm %D",m,B->rmap->N);
3457:   if (A->rmap->bs != B->rmap->bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"A row bs %D != B row bs %D",A->rmap->bs,B->rmap->bs);
3458:   /* remove check below; When B is created using iscol_o from ISGetSeqIS_SameColDist_Private(), its bs may not be same as A */
3459:   /* if (A->cmap->bs != B->cmap->bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"A column bs %D != B column bs %D",A->cmap->bs,B->cmap->bs); */

3461:   /* Get global columns of mat */
3462:   MPIU_Allreduce(&n,&N,1,MPIU_INT,MPI_SUM,comm);

3464:   MatSetSizes(*mat,m,n,PETSC_DECIDE,N);
3465:   MatSetType(*mat,MATMPIAIJ);
3466:   MatSetBlockSizes(*mat,A->rmap->bs,A->cmap->bs);
3467:   maij = (Mat_MPIAIJ*)(*mat)->data;

3469:   (*mat)->preallocated = PETSC_TRUE;

3471:   PetscLayoutSetUp((*mat)->rmap);
3472:   PetscLayoutSetUp((*mat)->cmap);

3474:   /* Set A as diagonal portion of *mat */
3475:   maij->A = A;

3477:   nz = oi[m];
3478:   for (i=0; i<nz; i++) {
3479:     col   = oj[i];
3480:     oj[i] = garray[col];
3481:   }

3483:   /* Set Bnew as off-diagonal portion of *mat */
3484:   MatSeqAIJGetArrayRead(B,&oa);
3485:   MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,N,oi,oj,(PetscScalar*)oa,&Bnew);
3486:   MatSeqAIJRestoreArrayRead(B,&oa);
3487:   bnew        = (Mat_SeqAIJ*)Bnew->data;
3488:   bnew->maxnz = b->maxnz; /* allocated nonzeros of B */
3489:   maij->B     = Bnew;

3491:   if (B->rmap->N != Bnew->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"BN %d != BnewN %d",B->rmap->N,Bnew->rmap->N);

3493:   b->singlemalloc = PETSC_FALSE; /* B arrays are shared by Bnew */
3494:   b->free_a       = PETSC_FALSE;
3495:   b->free_ij      = PETSC_FALSE;
3496:   MatDestroy(&B);

3498:   bnew->singlemalloc = PETSC_TRUE; /* arrays will be freed by MatDestroy(&Bnew) */
3499:   bnew->free_a       = PETSC_TRUE;
3500:   bnew->free_ij      = PETSC_TRUE;

3502:   /* condense columns of maij->B */
3503:   MatSetOption(*mat,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);
3504:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
3505:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
3506:   MatSetOption(*mat,MAT_NO_OFF_PROC_ENTRIES,PETSC_FALSE);
3507:   MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3508:   return(0);
3509: }

3511: extern PetscErrorCode MatCreateSubMatrices_MPIAIJ_SingleIS_Local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool,Mat*);

3513: PetscErrorCode MatCreateSubMatrix_MPIAIJ_SameRowDist(Mat mat,IS isrow,IS iscol,IS iscol_local,MatReuse call,Mat *newmat)
3514: {
3516:   PetscInt       i,m,n,rstart,row,rend,nz,j,bs,cbs;
3517:   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
3518:   Mat_MPIAIJ     *a=(Mat_MPIAIJ*)mat->data;
3519:   Mat            M,Msub,B=a->B;
3520:   MatScalar      *aa;
3521:   Mat_SeqAIJ     *aij;
3522:   PetscInt       *garray = a->garray,*colsub,Ncols;
3523:   PetscInt       count,Bn=B->cmap->N,cstart=mat->cmap->rstart,cend=mat->cmap->rend;
3524:   IS             iscol_sub,iscmap;
3525:   const PetscInt *is_idx,*cmap;
3526:   PetscBool      allcolumns=PETSC_FALSE;
3527:   MPI_Comm       comm;

3530:   PetscObjectGetComm((PetscObject)mat,&comm);
3531:   if (call == MAT_REUSE_MATRIX) {
3532:     PetscObjectQuery((PetscObject)*newmat,"SubIScol",(PetscObject*)&iscol_sub);
3533:     if (!iscol_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SubIScol passed in was not used before, cannot reuse");
3534:     ISGetLocalSize(iscol_sub,&count);

3536:     PetscObjectQuery((PetscObject)*newmat,"Subcmap",(PetscObject*)&iscmap);
3537:     if (!iscmap) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Subcmap passed in was not used before, cannot reuse");

3539:     PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Msub);
3540:     if (!Msub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");

3542:     MatCreateSubMatrices_MPIAIJ_SingleIS_Local(mat,1,&isrow,&iscol_sub,MAT_REUSE_MATRIX,PETSC_FALSE,&Msub);

3544:   } else { /* call == MAT_INITIAL_MATRIX) */
3545:     PetscBool flg;

3547:     ISGetLocalSize(iscol,&n);
3548:     ISGetSize(iscol,&Ncols);

3550:     /* (1) iscol -> nonscalable iscol_local */
3551:     /* Check for special case: each processor gets entire matrix columns */
3552:     ISIdentity(iscol_local,&flg);
3553:     if (flg && n == mat->cmap->N) allcolumns = PETSC_TRUE;
3554:     MPIU_Allreduce(MPI_IN_PLACE,&allcolumns,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)mat));
3555:     if (allcolumns) {
3556:       iscol_sub = iscol_local;
3557:       PetscObjectReference((PetscObject)iscol_local);
3558:       ISCreateStride(PETSC_COMM_SELF,n,0,1,&iscmap);

3560:     } else {
3561:       /* (2) iscol_local -> iscol_sub and iscmap. Implementation below requires iscol_local be sorted, it can have duplicate indices */
3562:       PetscInt *idx,*cmap1,k;
3563:       PetscMalloc1(Ncols,&idx);
3564:       PetscMalloc1(Ncols,&cmap1);
3565:       ISGetIndices(iscol_local,&is_idx);
3566:       count = 0;
3567:       k     = 0;
3568:       for (i=0; i<Ncols; i++) {
3569:         j = is_idx[i];
3570:         if (j >= cstart && j < cend) {
3571:           /* diagonal part of mat */
3572:           idx[count]     = j;
3573:           cmap1[count++] = i; /* column index in submat */
3574:         } else if (Bn) {
3575:           /* off-diagonal part of mat */
3576:           if (j == garray[k]) {
3577:             idx[count]     = j;
3578:             cmap1[count++] = i;  /* column index in submat */
3579:           } else if (j > garray[k]) {
3580:             while (j > garray[k] && k < Bn-1) k++;
3581:             if (j == garray[k]) {
3582:               idx[count]     = j;
3583:               cmap1[count++] = i; /* column index in submat */
3584:             }
3585:           }
3586:         }
3587:       }
3588:       ISRestoreIndices(iscol_local,&is_idx);

3590:       ISCreateGeneral(PETSC_COMM_SELF,count,idx,PETSC_OWN_POINTER,&iscol_sub);
3591:       ISGetBlockSize(iscol,&cbs);
3592:       ISSetBlockSize(iscol_sub,cbs);

3594:       ISCreateGeneral(PetscObjectComm((PetscObject)iscol_local),count,cmap1,PETSC_OWN_POINTER,&iscmap);
3595:     }

3597:     /* (3) Create sequential Msub */
3598:     MatCreateSubMatrices_MPIAIJ_SingleIS_Local(mat,1,&isrow,&iscol_sub,MAT_INITIAL_MATRIX,allcolumns,&Msub);
3599:   }

3601:   ISGetLocalSize(iscol_sub,&count);
3602:   aij  = (Mat_SeqAIJ*)(Msub)->data;
3603:   ii   = aij->i;
3604:   ISGetIndices(iscmap,&cmap);

3606:   /*
3607:       m - number of local rows
3608:       Ncols - number of columns (same on all processors)
3609:       rstart - first row in new global matrix generated
3610:   */
3611:   MatGetSize(Msub,&m,NULL);

3613:   if (call == MAT_INITIAL_MATRIX) {
3614:     /* (4) Create parallel newmat */
3615:     PetscMPIInt    rank,size;
3616:     PetscInt       csize;

3618:     MPI_Comm_size(comm,&size);
3619:     MPI_Comm_rank(comm,&rank);

3621:     /*
3622:         Determine the number of non-zeros in the diagonal and off-diagonal
3623:         portions of the matrix in order to do correct preallocation
3624:     */

3626:     /* first get start and end of "diagonal" columns */
3627:     ISGetLocalSize(iscol,&csize);
3628:     if (csize == PETSC_DECIDE) {
3629:       ISGetSize(isrow,&mglobal);
3630:       if (mglobal == Ncols) { /* square matrix */
3631:         nlocal = m;
3632:       } else {
3633:         nlocal = Ncols/size + ((Ncols % size) > rank);
3634:       }
3635:     } else {
3636:       nlocal = csize;
3637:     }
3638:     MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
3639:     rstart = rend - nlocal;
3640:     if (rank == size - 1 && rend != Ncols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,Ncols);

3642:     /* next, compute all the lengths */
3643:     jj    = aij->j;
3644:     PetscMalloc1(2*m+1,&dlens);
3645:     olens = dlens + m;
3646:     for (i=0; i<m; i++) {
3647:       jend = ii[i+1] - ii[i];
3648:       olen = 0;
3649:       dlen = 0;
3650:       for (j=0; j<jend; j++) {
3651:         if (cmap[*jj] < rstart || cmap[*jj] >= rend) olen++;
3652:         else dlen++;
3653:         jj++;
3654:       }
3655:       olens[i] = olen;
3656:       dlens[i] = dlen;
3657:     }

3659:     ISGetBlockSize(isrow,&bs);
3660:     ISGetBlockSize(iscol,&cbs);

3662:     MatCreate(comm,&M);
3663:     MatSetSizes(M,m,nlocal,PETSC_DECIDE,Ncols);
3664:     MatSetBlockSizes(M,bs,cbs);
3665:     MatSetType(M,((PetscObject)mat)->type_name);
3666:     MatMPIAIJSetPreallocation(M,0,dlens,0,olens);
3667:     PetscFree(dlens);

3669:   } else { /* call == MAT_REUSE_MATRIX */
3670:     M    = *newmat;
3671:     MatGetLocalSize(M,&i,NULL);
3672:     if (i != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
3673:     MatZeroEntries(M);
3674:     /*
3675:          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
3676:        rather than the slower MatSetValues().
3677:     */
3678:     M->was_assembled = PETSC_TRUE;
3679:     M->assembled     = PETSC_FALSE;
3680:   }

3682:   /* (5) Set values of Msub to *newmat */
3683:   PetscMalloc1(count,&colsub);
3684:   MatGetOwnershipRange(M,&rstart,NULL);

3686:   jj   = aij->j;
3687:   MatSeqAIJGetArrayRead(Msub,(const PetscScalar**)&aa);
3688:   for (i=0; i<m; i++) {
3689:     row = rstart + i;
3690:     nz  = ii[i+1] - ii[i];
3691:     for (j=0; j<nz; j++) colsub[j] = cmap[jj[j]];
3692:     MatSetValues_MPIAIJ(M,1,&row,nz,colsub,aa,INSERT_VALUES);
3693:     jj += nz; aa += nz;
3694:   }
3695:   MatSeqAIJRestoreArrayRead(Msub,(const PetscScalar**)&aa);
3696:   ISRestoreIndices(iscmap,&cmap);

3698:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
3699:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);

3701:   PetscFree(colsub);

3703:   /* save Msub, iscol_sub and iscmap used in processor for next request */
3704:   if (call == MAT_INITIAL_MATRIX) {
3705:     *newmat = M;
3706:     PetscObjectCompose((PetscObject)(*newmat),"SubMatrix",(PetscObject)Msub);
3707:     MatDestroy(&Msub);

3709:     PetscObjectCompose((PetscObject)(*newmat),"SubIScol",(PetscObject)iscol_sub);
3710:     ISDestroy(&iscol_sub);

3712:     PetscObjectCompose((PetscObject)(*newmat),"Subcmap",(PetscObject)iscmap);
3713:     ISDestroy(&iscmap);

3715:     if (iscol_local) {
3716:       PetscObjectCompose((PetscObject)(*newmat),"ISAllGather",(PetscObject)iscol_local);
3717:       ISDestroy(&iscol_local);
3718:     }
3719:   }
3720:   return(0);
3721: }

3723: /*
3724:     Not great since it makes two copies of the submatrix, first an SeqAIJ
3725:   in local and then by concatenating the local matrices the end result.
3726:   Writing it directly would be much like MatCreateSubMatrices_MPIAIJ()

3728:   Note: This requires a sequential iscol with all indices.
3729: */
3730: PetscErrorCode MatCreateSubMatrix_MPIAIJ_nonscalable(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
3731: {
3733:   PetscMPIInt    rank,size;
3734:   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs;
3735:   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
3736:   Mat            M,Mreuse;
3737:   MatScalar      *aa,*vwork;
3738:   MPI_Comm       comm;
3739:   Mat_SeqAIJ     *aij;
3740:   PetscBool      colflag,allcolumns=PETSC_FALSE;

3743:   PetscObjectGetComm((PetscObject)mat,&comm);
3744:   MPI_Comm_rank(comm,&rank);
3745:   MPI_Comm_size(comm,&size);

3747:   /* Check for special case: each processor gets entire matrix columns */
3748:   ISIdentity(iscol,&colflag);
3749:   ISGetLocalSize(iscol,&n);
3750:   if (colflag && n == mat->cmap->N) allcolumns = PETSC_TRUE;
3751:   MPIU_Allreduce(MPI_IN_PLACE,&allcolumns,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)mat));

3753:   if (call ==  MAT_REUSE_MATRIX) {
3754:     PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);
3755:     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3756:     MatCreateSubMatrices_MPIAIJ_SingleIS_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,allcolumns,&Mreuse);
3757:   } else {
3758:     MatCreateSubMatrices_MPIAIJ_SingleIS_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,allcolumns,&Mreuse);
3759:   }

3761:   /*
3762:       m - number of local rows
3763:       n - number of columns (same on all processors)
3764:       rstart - first row in new global matrix generated
3765:   */
3766:   MatGetSize(Mreuse,&m,&n);
3767:   MatGetBlockSizes(Mreuse,&bs,&cbs);
3768:   if (call == MAT_INITIAL_MATRIX) {
3769:     aij = (Mat_SeqAIJ*)(Mreuse)->data;
3770:     ii  = aij->i;
3771:     jj  = aij->j;

3773:     /*
3774:         Determine the number of non-zeros in the diagonal and off-diagonal
3775:         portions of the matrix in order to do correct preallocation
3776:     */

3778:     /* first get start and end of "diagonal" columns */
3779:     if (csize == PETSC_DECIDE) {
3780:       ISGetSize(isrow,&mglobal);
3781:       if (mglobal == n) { /* square matrix */
3782:         nlocal = m;
3783:       } else {
3784:         nlocal = n/size + ((n % size) > rank);
3785:       }
3786:     } else {
3787:       nlocal = csize;
3788:     }
3789:     MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
3790:     rstart = rend - nlocal;
3791:     if (rank == size - 1 && rend != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);

3793:     /* next, compute all the lengths */
3794:     PetscMalloc1(2*m+1,&dlens);
3795:     olens = dlens + m;
3796:     for (i=0; i<m; i++) {
3797:       jend = ii[i+1] - ii[i];
3798:       olen = 0;
3799:       dlen = 0;
3800:       for (j=0; j<jend; j++) {
3801:         if (*jj < rstart || *jj >= rend) olen++;
3802:         else dlen++;
3803:         jj++;
3804:       }
3805:       olens[i] = olen;
3806:       dlens[i] = dlen;
3807:     }
3808:     MatCreate(comm,&M);
3809:     MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);
3810:     MatSetBlockSizes(M,bs,cbs);
3811:     MatSetType(M,((PetscObject)mat)->type_name);
3812:     MatMPIAIJSetPreallocation(M,0,dlens,0,olens);
3813:     PetscFree(dlens);
3814:   } else {
3815:     PetscInt ml,nl;

3817:     M    = *newmat;
3818:     MatGetLocalSize(M,&ml,&nl);
3819:     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
3820:     MatZeroEntries(M);
3821:     /*
3822:          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
3823:        rather than the slower MatSetValues().
3824:     */
3825:     M->was_assembled = PETSC_TRUE;
3826:     M->assembled     = PETSC_FALSE;
3827:   }
3828:   MatGetOwnershipRange(M,&rstart,&rend);
3829:   aij  = (Mat_SeqAIJ*)(Mreuse)->data;
3830:   ii   = aij->i;
3831:   jj   = aij->j;

3833:   /* trigger copy to CPU if needed */
3834:   MatSeqAIJGetArrayRead(Mreuse,(const PetscScalar**)&aa);
3835:   for (i=0; i<m; i++) {
3836:     row   = rstart + i;
3837:     nz    = ii[i+1] - ii[i];
3838:     cwork = jj; jj += nz;
3839:     vwork = aa; aa += nz;
3840:     MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);
3841:   }
3842:   MatSeqAIJRestoreArrayRead(Mreuse,(const PetscScalar**)&aa);

3844:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
3845:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
3846:   *newmat = M;

3848:   /* save submatrix used in processor for next request */
3849:   if (call ==  MAT_INITIAL_MATRIX) {
3850:     PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);
3851:     MatDestroy(&Mreuse);
3852:   }
3853:   return(0);
3854: }

3856: PetscErrorCode MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3857: {
3858:   PetscInt       m,cstart, cend,j,nnz,i,d;
3859:   PetscInt       *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
3860:   const PetscInt *JJ;
3862:   PetscBool      nooffprocentries;

3865:   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]);

3867:   PetscLayoutSetUp(B->rmap);
3868:   PetscLayoutSetUp(B->cmap);
3869:   m      = B->rmap->n;
3870:   cstart = B->cmap->rstart;
3871:   cend   = B->cmap->rend;
3872:   rstart = B->rmap->rstart;

3874:   PetscCalloc2(m,&d_nnz,m,&o_nnz);

3876:   if (PetscDefined(USE_DEBUG)) {
3877:     for (i=0; i<m; i++) {
3878:       nnz = Ii[i+1]- Ii[i];
3879:       JJ  = J + Ii[i];
3880:       if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
3881:       if (nnz && (JJ[0] < 0)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,JJ[0]);
3882:       if (nnz && (JJ[nnz-1] >= B->cmap->N)) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Row %D ends with too large a column index %D (max allowed %D)",i,JJ[nnz-1],B->cmap->N);
3883:     }
3884:   }

3886:   for (i=0; i<m; i++) {
3887:     nnz     = Ii[i+1]- Ii[i];
3888:     JJ      = J + Ii[i];
3889:     nnz_max = PetscMax(nnz_max,nnz);
3890:     d       = 0;
3891:     for (j=0; j<nnz; j++) {
3892:       if (cstart <= JJ[j] && JJ[j] < cend) d++;
3893:     }
3894:     d_nnz[i] = d;
3895:     o_nnz[i] = nnz - d;
3896:   }
3897:   MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);
3898:   PetscFree2(d_nnz,o_nnz);

3900:   for (i=0; i<m; i++) {
3901:     ii   = i + rstart;
3902:     MatSetValues_MPIAIJ(B,1,&ii,Ii[i+1] - Ii[i],J+Ii[i], v ? v + Ii[i] : NULL,INSERT_VALUES);
3903:   }
3904:   nooffprocentries    = B->nooffprocentries;
3905:   B->nooffprocentries = PETSC_TRUE;
3906:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3907:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
3908:   B->nooffprocentries = nooffprocentries;

3910:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3911:   return(0);
3912: }

3914: /*@
3915:    MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
3916:    (the default parallel PETSc format).

3918:    Collective

3920:    Input Parameters:
3921: +  B - the matrix
3922: .  i - the indices into j for the start of each local row (starts with zero)
3923: .  j - the column indices for each local row (starts with zero)
3924: -  v - optional values in the matrix

3926:    Level: developer

3928:    Notes:
3929:        The i, j, and v arrays ARE copied by this routine into the internal format used by PETSc;
3930:      thus you CANNOT change the matrix entries by changing the values of v[] after you have
3931:      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.

3933:        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.

3935:        The format which is used for the sparse matrix input, is equivalent to a
3936:     row-major ordering.. i.e for the following matrix, the input data expected is
3937:     as shown

3939: $        1 0 0
3940: $        2 0 3     P0
3941: $       -------
3942: $        4 5 6     P1
3943: $
3944: $     Process0 [P0]: rows_owned=[0,1]
3945: $        i =  {0,1,3}  [size = nrow+1  = 2+1]
3946: $        j =  {0,0,2}  [size = 3]
3947: $        v =  {1,2,3}  [size = 3]
3948: $
3949: $     Process1 [P1]: rows_owned=[2]
3950: $        i =  {0,3}    [size = nrow+1  = 1+1]
3951: $        j =  {0,1,2}  [size = 3]
3952: $        v =  {4,5,6}  [size = 3]

3954: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MATMPIAIJ,
3955:           MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays()
3956: @*/
3957: PetscErrorCode  MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3958: {

3962:   PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
3963:   return(0);
3964: }

3966: /*@C
3967:    MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format
3968:    (the default parallel PETSc format).  For good matrix assembly performance
3969:    the user should preallocate the matrix storage by setting the parameters
3970:    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
3971:    performance can be increased by more than a factor of 50.

3973:    Collective

3975:    Input Parameters:
3976: +  B - the matrix
3977: .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
3978:            (same value is used for all local rows)
3979: .  d_nnz - array containing the number of nonzeros in the various rows of the
3980:            DIAGONAL portion of the local submatrix (possibly different for each row)
3981:            or NULL (PETSC_NULL_INTEGER in Fortran), if d_nz is used to specify the nonzero structure.
3982:            The size of this array is equal to the number of local rows, i.e 'm'.
3983:            For matrices that will be factored, you must leave room for (and set)
3984:            the diagonal entry even if it is zero.
3985: .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
3986:            submatrix (same value is used for all local rows).
3987: -  o_nnz - array containing the number of nonzeros in the various rows of the
3988:            OFF-DIAGONAL portion of the local submatrix (possibly different for
3989:            each row) or NULL (PETSC_NULL_INTEGER in Fortran), if o_nz is used to specify the nonzero
3990:            structure. The size of this array is equal to the number
3991:            of local rows, i.e 'm'.

3993:    If the *_nnz parameter is given then the *_nz parameter is ignored

3995:    The AIJ format (also called the Yale sparse matrix format or
3996:    compressed row storage (CSR)), is fully compatible with standard Fortran 77
3997:    storage.  The stored row and column indices begin with zero.
3998:    See Users-Manual: ch_mat for details.

4000:    The parallel matrix is partitioned such that the first m0 rows belong to
4001:    process 0, the next m1 rows belong to process 1, the next m2 rows belong
4002:    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.

4004:    The DIAGONAL portion of the local submatrix of a processor can be defined
4005:    as the submatrix which is obtained by extraction the part corresponding to
4006:    the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the
4007:    first row that belongs to the processor, r2 is the last row belonging to
4008:    the this processor, and c1-c2 is range of indices of the local part of a
4009:    vector suitable for applying the matrix to.  This is an mxn matrix.  In the
4010:    common case of a square matrix, the row and column ranges are the same and
4011:    the DIAGONAL part is also square. The remaining portion of the local
4012:    submatrix (mxN) constitute the OFF-DIAGONAL portion.

4014:    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.

4016:    You can call MatGetInfo() to get information on how effective the preallocation was;
4017:    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
4018:    You can also run with the option -info and look for messages with the string
4019:    malloc in them to see if additional memory allocation was needed.

4021:    Example usage:

4023:    Consider the following 8x8 matrix with 34 non-zero values, that is
4024:    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
4025:    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
4026:    as follows:

4028: .vb
4029:             1  2  0  |  0  3  0  |  0  4
4030:     Proc0   0  5  6  |  7  0  0  |  8  0
4031:             9  0 10  | 11  0  0  | 12  0
4032:     -------------------------------------
4033:            13  0 14  | 15 16 17  |  0  0
4034:     Proc1   0 18  0  | 19 20 21  |  0  0
4035:             0  0  0  | 22 23  0  | 24  0
4036:     -------------------------------------
4037:     Proc2  25 26 27  |  0  0 28  | 29  0
4038:            30  0  0  | 31 32 33  |  0 34
4039: .ve

4041:    This can be represented as a collection of submatrices as:

4043: .vb
4044:       A B C
4045:       D E F
4046:       G H I
4047: .ve

4049:    Where the submatrices A,B,C are owned by proc0, D,E,F are
4050:    owned by proc1, G,H,I are owned by proc2.

4052:    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4053:    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4054:    The 'M','N' parameters are 8,8, and have the same values on all procs.

4056:    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
4057:    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
4058:    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
4059:    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
4060:    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
4061:    matrix, ans [DF] as another SeqAIJ matrix.

4063:    When d_nz, o_nz parameters are specified, d_nz storage elements are
4064:    allocated for every row of the local diagonal submatrix, and o_nz
4065:    storage locations are allocated for every row of the OFF-DIAGONAL submat.
4066:    One way to choose d_nz and o_nz is to use the max nonzerors per local
4067:    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
4068:    In this case, the values of d_nz,o_nz are:
4069: .vb
4070:      proc0 : dnz = 2, o_nz = 2
4071:      proc1 : dnz = 3, o_nz = 2
4072:      proc2 : dnz = 1, o_nz = 4
4073: .ve
4074:    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
4075:    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
4076:    for proc3. i.e we are using 12+15+10=37 storage locations to store
4077:    34 values.

4079:    When d_nnz, o_nnz parameters are specified, the storage is specified
4080:    for every row, corresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
4081:    In the above case the values for d_nnz,o_nnz are:
4082: .vb
4083:      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
4084:      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
4085:      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
4086: .ve
4087:    Here the space allocated is sum of all the above values i.e 34, and
4088:    hence pre-allocation is perfect.

4090:    Level: intermediate

4092: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(),
4093:           MATMPIAIJ, MatGetInfo(), PetscSplitOwnership()
4094: @*/
4095: PetscErrorCode MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
4096: {

4102:   PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));
4103:   return(0);
4104: }

4106: /*@
4107:      MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard
4108:          CSR format for the local rows.

4110:    Collective

4112:    Input Parameters:
4113: +  comm - MPI communicator
4114: .  m - number of local rows (Cannot be PETSC_DECIDE)
4115: .  n - This value should be the same as the local size used in creating the
4116:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4117:        calculated if N is given) For square matrices n is almost always m.
4118: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4119: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4120: .   i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
4121: .   j - column indices
4122: -   a - matrix values

4124:    Output Parameter:
4125: .   mat - the matrix

4127:    Level: intermediate

4129:    Notes:
4130:        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
4131:      thus you CANNOT change the matrix entries by changing the values of a[] after you have
4132:      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.

4134:        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.

4136:        The format which is used for the sparse matrix input, is equivalent to a
4137:     row-major ordering.. i.e for the following matrix, the input data expected is
4138:     as shown

4140:        Once you have created the matrix you can update it with new numerical values using MatUpdateMPIAIJWithArrays

4142: $        1 0 0
4143: $        2 0 3     P0
4144: $       -------
4145: $        4 5 6     P1
4146: $
4147: $     Process0 [P0]: rows_owned=[0,1]
4148: $        i =  {0,1,3}  [size = nrow+1  = 2+1]
4149: $        j =  {0,0,2}  [size = 3]
4150: $        v =  {1,2,3}  [size = 3]
4151: $
4152: $     Process1 [P1]: rows_owned=[2]
4153: $        i =  {0,3}    [size = nrow+1  = 1+1]
4154: $        j =  {0,1,2}  [size = 3]
4155: $        v =  {4,5,6}  [size = 3]

4157: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4158:           MATMPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays(), MatUpdateMPIAIJWithArrays()
4159: @*/
4160: PetscErrorCode MatCreateMPIAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
4161: {

4165:   if (i && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4166:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
4167:   MatCreate(comm,mat);
4168:   MatSetSizes(*mat,m,n,M,N);
4169:   /* MatSetBlockSizes(M,bs,cbs); */
4170:   MatSetType(*mat,MATMPIAIJ);
4171:   MatMPIAIJSetPreallocationCSR(*mat,i,j,a);
4172:   return(0);
4173: }

4175: /*@
4176:      MatUpdateMPIAIJWithArrays - updates a MPI AIJ matrix using arrays that contain in standard
4177:          CSR format for the local rows. Only the numerical values are updated the other arrays must be identical

4179:    Collective

4181:    Input Parameters:
4182: +  mat - the matrix
4183: .  m - number of local rows (Cannot be PETSC_DECIDE)
4184: .  n - This value should be the same as the local size used in creating the
4185:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4186:        calculated if N is given) For square matrices n is almost always m.
4187: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4188: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4189: .  Ii - row indices; that is Ii[0] = 0, Ii[row] = Ii[row-1] + number of elements in that row of the matrix
4190: .  J - column indices
4191: -  v - matrix values

4193:    Level: intermediate

4195: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4196:           MATMPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays(), MatUpdateMPIAIJWithArrays()
4197: @*/
4198: PetscErrorCode MatUpdateMPIAIJWithArrays(Mat mat,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
4199: {
4201:   PetscInt       cstart,nnz,i,j;
4202:   PetscInt       *ld;
4203:   PetscBool      nooffprocentries;
4204:   Mat_MPIAIJ     *Aij = (Mat_MPIAIJ*)mat->data;
4205:   Mat_SeqAIJ     *Ad  = (Mat_SeqAIJ*)Aij->A->data, *Ao  = (Mat_SeqAIJ*)Aij->B->data;
4206:   PetscScalar    *ad = Ad->a, *ao = Ao->a;
4207:   const PetscInt *Adi = Ad->i;
4208:   PetscInt       ldi,Iii,md;

4211:   if (Ii[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4212:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
4213:   if (m != mat->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Local number of rows cannot change from call to MatUpdateMPIAIJWithArrays()");
4214:   if (n != mat->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Local number of columns cannot change from call to MatUpdateMPIAIJWithArrays()");

4216:   cstart = mat->cmap->rstart;
4217:   if (!Aij->ld) {
4218:     /* count number of entries below block diagonal */
4219:     PetscCalloc1(m,&ld);
4220:     Aij->ld = ld;
4221:     for (i=0; i<m; i++) {
4222:       nnz  = Ii[i+1]- Ii[i];
4223:       j     = 0;
4224:       while  (J[j] < cstart && j < nnz) {j++;}
4225:       J    += nnz;
4226:       ld[i] = j;
4227:     }
4228:   } else {
4229:     ld = Aij->ld;
4230:   }

4232:   for (i=0; i<m; i++) {
4233:     nnz  = Ii[i+1]- Ii[i];
4234:     Iii  = Ii[i];
4235:     ldi  = ld[i];
4236:     md   = Adi[i+1]-Adi[i];
4237:     PetscArraycpy(ao,v + Iii,ldi);
4238:     PetscArraycpy(ad,v + Iii + ldi,md);
4239:     PetscArraycpy(ao + ldi,v + Iii + ldi + md,nnz - ldi - md);
4240:     ad  += md;
4241:     ao  += nnz - md;
4242:   }
4243:   nooffprocentries      = mat->nooffprocentries;
4244:   mat->nooffprocentries = PETSC_TRUE;
4245:   PetscObjectStateIncrease((PetscObject)Aij->A);
4246:   PetscObjectStateIncrease((PetscObject)Aij->B);
4247:   PetscObjectStateIncrease((PetscObject)mat);
4248:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
4249:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
4250:   mat->nooffprocentries = nooffprocentries;
4251:   return(0);
4252: }

4254: /*@C
4255:    MatCreateAIJ - Creates a sparse parallel matrix in AIJ format
4256:    (the default parallel PETSc format).  For good matrix assembly performance
4257:    the user should preallocate the matrix storage by setting the parameters
4258:    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
4259:    performance can be increased by more than a factor of 50.

4261:    Collective

4263:    Input Parameters:
4264: +  comm - MPI communicator
4265: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
4266:            This value should be the same as the local size used in creating the
4267:            y vector for the matrix-vector product y = Ax.
4268: .  n - This value should be the same as the local size used in creating the
4269:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4270:        calculated if N is given) For square matrices n is almost always m.
4271: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4272: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4273: .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
4274:            (same value is used for all local rows)
4275: .  d_nnz - array containing the number of nonzeros in the various rows of the
4276:            DIAGONAL portion of the local submatrix (possibly different for each row)
4277:            or NULL, if d_nz is used to specify the nonzero structure.
4278:            The size of this array is equal to the number of local rows, i.e 'm'.
4279: .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
4280:            submatrix (same value is used for all local rows).
4281: -  o_nnz - array containing the number of nonzeros in the various rows of the
4282:            OFF-DIAGONAL portion of the local submatrix (possibly different for
4283:            each row) or NULL, if o_nz is used to specify the nonzero
4284:            structure. The size of this array is equal to the number
4285:            of local rows, i.e 'm'.

4287:    Output Parameter:
4288: .  A - the matrix

4290:    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
4291:    MatXXXXSetPreallocation() paradigm instead of this routine directly.
4292:    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]

4294:    Notes:
4295:    If the *_nnz parameter is given then the *_nz parameter is ignored

4297:    m,n,M,N parameters specify the size of the matrix, and its partitioning across
4298:    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
4299:    storage requirements for this matrix.

4301:    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
4302:    processor than it must be used on all processors that share the object for
4303:    that argument.

4305:    The user MUST specify either the local or global matrix dimensions
4306:    (possibly both).

4308:    The parallel matrix is partitioned across processors such that the
4309:    first m0 rows belong to process 0, the next m1 rows belong to
4310:    process 1, the next m2 rows belong to process 2 etc.. where
4311:    m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores
4312:    values corresponding to [m x N] submatrix.

4314:    The columns are logically partitioned with the n0 columns belonging
4315:    to 0th partition, the next n1 columns belonging to the next
4316:    partition etc.. where n0,n1,n2... are the input parameter 'n'.

4318:    The DIAGONAL portion of the local submatrix on any given processor
4319:    is the submatrix corresponding to the rows and columns m,n
4320:    corresponding to the given processor. i.e diagonal matrix on
4321:    process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1]
4322:    etc. The remaining portion of the local submatrix [m x (N-n)]
4323:    constitute the OFF-DIAGONAL portion. The example below better
4324:    illustrates this concept.

4326:    For a square global matrix we define each processor's diagonal portion
4327:    to be its local rows and the corresponding columns (a square submatrix);
4328:    each processor's off-diagonal portion encompasses the remainder of the
4329:    local matrix (a rectangular submatrix).

4331:    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.

4333:    When calling this routine with a single process communicator, a matrix of
4334:    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
4335:    type of communicator, use the construction mechanism
4336: .vb
4337:      MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...);
4338: .ve

4340: $     MatCreate(...,&A);
4341: $     MatSetType(A,MATMPIAIJ);
4342: $     MatSetSizes(A, m,n,M,N);
4343: $     MatMPIAIJSetPreallocation(A,...);

4345:    By default, this format uses inodes (identical nodes) when possible.
4346:    We search for consecutive rows with the same nonzero structure, thereby
4347:    reusing matrix information to achieve increased efficiency.

4349:    Options Database Keys:
4350: +  -mat_no_inode  - Do not use inodes
4351: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

4353:    Example usage:

4355:    Consider the following 8x8 matrix with 34 non-zero values, that is
4356:    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
4357:    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
4358:    as follows

4360: .vb
4361:             1  2  0  |  0  3  0  |  0  4
4362:     Proc0   0  5  6  |  7  0  0  |  8  0
4363:             9  0 10  | 11  0  0  | 12  0
4364:     -------------------------------------
4365:            13  0 14  | 15 16 17  |  0  0
4366:     Proc1   0 18  0  | 19 20 21  |  0  0
4367:             0  0  0  | 22 23  0  | 24  0
4368:     -------------------------------------
4369:     Proc2  25 26 27  |  0  0 28  | 29  0
4370:            30  0  0  | 31 32 33  |  0 34
4371: .ve

4373:    This can be represented as a collection of submatrices as

4375: .vb
4376:       A B C
4377:       D E F
4378:       G H I
4379: .ve

4381:    Where the submatrices A,B,C are owned by proc0, D,E,F are
4382:    owned by proc1, G,H,I are owned by proc2.

4384:    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4385:    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4386:    The 'M','N' parameters are 8,8, and have the same values on all procs.

4388:    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
4389:    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
4390:    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
4391:    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
4392:    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
4393:    matrix, ans [DF] as another SeqAIJ matrix.

4395:    When d_nz, o_nz parameters are specified, d_nz storage elements are
4396:    allocated for every row of the local diagonal submatrix, and o_nz
4397:    storage locations are allocated for every row of the OFF-DIAGONAL submat.
4398:    One way to choose d_nz and o_nz is to use the max nonzerors per local
4399:    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
4400:    In this case, the values of d_nz,o_nz are
4401: .vb
4402:      proc0 : dnz = 2, o_nz = 2
4403:      proc1 : dnz = 3, o_nz = 2
4404:      proc2 : dnz = 1, o_nz = 4
4405: .ve
4406:    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
4407:    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
4408:    for proc3. i.e we are using 12+15+10=37 storage locations to store
4409:    34 values.

4411:    When d_nnz, o_nnz parameters are specified, the storage is specified
4412:    for every row, corresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
4413:    In the above case the values for d_nnz,o_nnz are
4414: .vb
4415:      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
4416:      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
4417:      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
4418: .ve
4419:    Here the space allocated is sum of all the above values i.e 34, and
4420:    hence pre-allocation is perfect.

4422:    Level: intermediate

4424: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4425:           MATMPIAIJ, MatCreateMPIAIJWithArrays()
4426: @*/
4427: PetscErrorCode  MatCreateAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A)
4428: {
4430:   PetscMPIInt    size;

4433:   MatCreate(comm,A);
4434:   MatSetSizes(*A,m,n,M,N);
4435:   MPI_Comm_size(comm,&size);
4436:   if (size > 1) {
4437:     MatSetType(*A,MATMPIAIJ);
4438:     MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);
4439:   } else {
4440:     MatSetType(*A,MATSEQAIJ);
4441:     MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);
4442:   }
4443:   return(0);
4444: }

4446: /*@C
4447:   MatMPIAIJGetSeqAIJ - Returns the local piece of this distributed matrix

4449:   Not collective

4451:   Input Parameter:
4452: . A - The MPIAIJ matrix

4454:   Output Parameters:
4455: + Ad - The local diagonal block as a SeqAIJ matrix
4456: . Ao - The local off-diagonal block as a SeqAIJ matrix
4457: - colmap - An array mapping local column numbers of Ao to global column numbers of the parallel matrix

4459:   Note: The rows in Ad and Ao are in [0, Nr), where Nr is the number of local rows on this process. The columns
4460:   in Ad are in [0, Nc) where Nc is the number of local columns. The columns are Ao are in [0, Nco), where Nco is
4461:   the number of nonzero columns in the local off-diagonal piece of the matrix A. The array colmap maps these
4462:   local column numbers to global column numbers in the original matrix.

4464:   Level: intermediate

4466: .seealso: MatMPIAIJGetLocalMat(), MatMPIAIJGetLocalMatCondensed(), MatCreateAIJ(), MATMPIAIJ, MATSEQAIJ
4467: @*/
4468: PetscErrorCode MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[])
4469: {
4470:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
4471:   PetscBool      flg;

4475:   PetscStrbeginswith(((PetscObject)A)->type_name,MATMPIAIJ,&flg);
4476:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"This function requires a MATMPIAIJ matrix as input");
4477:   if (Ad)     *Ad     = a->A;
4478:   if (Ao)     *Ao     = a->B;
4479:   if (colmap) *colmap = a->garray;
4480:   return(0);
4481: }

4483: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPIAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4484: {
4486:   PetscInt       m,N,i,rstart,nnz,Ii;
4487:   PetscInt       *indx;
4488:   PetscScalar    *values;

4491:   MatGetSize(inmat,&m,&N);
4492:   if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
4493:     PetscInt       *dnz,*onz,sum,bs,cbs;

4495:     if (n == PETSC_DECIDE) {
4496:       PetscSplitOwnership(comm,&n,&N);
4497:     }
4498:     /* Check sum(n) = N */
4499:     MPIU_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);
4500:     if (sum != N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns %D != global columns %D",sum,N);

4502:     MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);
4503:     rstart -= m;

4505:     MatPreallocateInitialize(comm,m,n,dnz,onz);
4506:     for (i=0; i<m; i++) {
4507:       MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);
4508:       MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);
4509:       MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);
4510:     }

4512:     MatCreate(comm,outmat);
4513:     MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
4514:     MatGetBlockSizes(inmat,&bs,&cbs);
4515:     MatSetBlockSizes(*outmat,bs,cbs);
4516:     MatSetType(*outmat,MATAIJ);
4517:     MatSeqAIJSetPreallocation(*outmat,0,dnz);
4518:     MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);
4519:     MatPreallocateFinalize(dnz,onz);
4520:     MatSetOption(*outmat,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);
4521:   }

4523:   /* numeric phase */
4524:   MatGetOwnershipRange(*outmat,&rstart,NULL);
4525:   for (i=0; i<m; i++) {
4526:     MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
4527:     Ii   = i + rstart;
4528:     MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);
4529:     MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
4530:   }
4531:   MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);
4532:   MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);
4533:   return(0);
4534: }

4536: PetscErrorCode MatFileSplit(Mat A,char *outfile)
4537: {
4538:   PetscErrorCode    ierr;
4539:   PetscMPIInt       rank;
4540:   PetscInt          m,N,i,rstart,nnz;
4541:   size_t            len;
4542:   const PetscInt    *indx;
4543:   PetscViewer       out;
4544:   char              *name;
4545:   Mat               B;
4546:   const PetscScalar *values;

4549:   MatGetLocalSize(A,&m,NULL);
4550:   MatGetSize(A,NULL,&N);
4551:   /* Should this be the type of the diagonal block of A? */
4552:   MatCreate(PETSC_COMM_SELF,&B);
4553:   MatSetSizes(B,m,N,m,N);
4554:   MatSetBlockSizesFromMats(B,A,A);
4555:   MatSetType(B,MATSEQAIJ);
4556:   MatSeqAIJSetPreallocation(B,0,NULL);
4557:   MatGetOwnershipRange(A,&rstart,NULL);
4558:   for (i=0; i<m; i++) {
4559:     MatGetRow(A,i+rstart,&nnz,&indx,&values);
4560:     MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);
4561:     MatRestoreRow(A,i+rstart,&nnz,&indx,&values);
4562:   }
4563:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
4564:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

4566:   MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);
4567:   PetscStrlen(outfile,&len);
4568:   PetscMalloc1(len+6,&name);
4569:   PetscSNPrintf(name,len+6,"%s.%d",outfile,rank);
4570:   PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);
4571:   PetscFree(name);
4572:   MatView(B,out);
4573:   PetscViewerDestroy(&out);
4574:   MatDestroy(&B);
4575:   return(0);
4576: }

4578: static PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(void *data)
4579: {
4580:   PetscErrorCode      ierr;
4581:   Mat_Merge_SeqsToMPI *merge = (Mat_Merge_SeqsToMPI *)data;

4584:   if (!merge) return(0);
4585:   PetscFree(merge->id_r);
4586:   PetscFree(merge->len_s);
4587:   PetscFree(merge->len_r);
4588:   PetscFree(merge->bi);
4589:   PetscFree(merge->bj);
4590:   PetscFree(merge->buf_ri[0]);
4591:   PetscFree(merge->buf_ri);
4592:   PetscFree(merge->buf_rj[0]);
4593:   PetscFree(merge->buf_rj);
4594:   PetscFree(merge->coi);
4595:   PetscFree(merge->coj);
4596:   PetscFree(merge->owners_co);
4597:   PetscLayoutDestroy(&merge->rowmap);
4598:   PetscFree(merge);
4599:   return(0);
4600: }

4602: #include <../src/mat/utils/freespace.h>
4603: #include <petscbt.h>

4605: PetscErrorCode MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat)
4606: {
4607:   PetscErrorCode      ierr;
4608:   MPI_Comm            comm;
4609:   Mat_SeqAIJ          *a  =(Mat_SeqAIJ*)seqmat->data;
4610:   PetscMPIInt         size,rank,taga,*len_s;
4611:   PetscInt            N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj;
4612:   PetscInt            proc,m;
4613:   PetscInt            **buf_ri,**buf_rj;
4614:   PetscInt            k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
4615:   PetscInt            nrows,**buf_ri_k,**nextrow,**nextai;
4616:   MPI_Request         *s_waits,*r_waits;
4617:   MPI_Status          *status;
4618:   MatScalar           *aa=a->a;
4619:   MatScalar           **abuf_r,*ba_i;
4620:   Mat_Merge_SeqsToMPI *merge;
4621:   PetscContainer      container;

4624:   PetscObjectGetComm((PetscObject)mpimat,&comm);
4625:   PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);

4627:   MPI_Comm_size(comm,&size);
4628:   MPI_Comm_rank(comm,&rank);

4630:   PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject*)&container);
4631:   if (!container) SETERRQ(PetscObjectComm((PetscObject)mpimat),PETSC_ERR_PLIB,"Mat not created from MatCreateMPIAIJSumSeqAIJSymbolic");
4632:   PetscContainerGetPointer(container,(void**)&merge);

4634:   bi     = merge->bi;
4635:   bj     = merge->bj;
4636:   buf_ri = merge->buf_ri;
4637:   buf_rj = merge->buf_rj;

4639:   PetscMalloc1(size,&status);
4640:   owners = merge->rowmap->range;
4641:   len_s  = merge->len_s;

4643:   /* send and recv matrix values */
4644:   /*-----------------------------*/
4645:   PetscObjectGetNewTag((PetscObject)mpimat,&taga);
4646:   PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);

4648:   PetscMalloc1(merge->nsend+1,&s_waits);
4649:   for (proc=0,k=0; proc<size; proc++) {
4650:     if (!len_s[proc]) continue;
4651:     i    = owners[proc];
4652:     MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);
4653:     k++;
4654:   }

4656:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,r_waits,status);}
4657:   if (merge->nsend) {MPI_Waitall(merge->nsend,s_waits,status);}
4658:   PetscFree(status);

4660:   PetscFree(s_waits);
4661:   PetscFree(r_waits);

4663:   /* insert mat values of mpimat */
4664:   /*----------------------------*/
4665:   PetscMalloc1(N,&ba_i);
4666:   PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);

4668:   for (k=0; k<merge->nrecv; k++) {
4669:     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
4670:     nrows       = *(buf_ri_k[k]);
4671:     nextrow[k]  = buf_ri_k[k]+1;  /* next row number of k-th recved i-structure */
4672:     nextai[k]   = buf_ri_k[k] + (nrows + 1); /* points to the next i-structure of k-th recved i-structure  */
4673:   }

4675:   /* set values of ba */
4676:   m = merge->rowmap->n;
4677:   for (i=0; i<m; i++) {
4678:     arow = owners[rank] + i;
4679:     bj_i = bj+bi[i];  /* col indices of the i-th row of mpimat */
4680:     bnzi = bi[i+1] - bi[i];
4681:     PetscArrayzero(ba_i,bnzi);

4683:     /* add local non-zero vals of this proc's seqmat into ba */
4684:     anzi   = ai[arow+1] - ai[arow];
4685:     aj     = a->j + ai[arow];
4686:     aa     = a->a + ai[arow];
4687:     nextaj = 0;
4688:     for (j=0; nextaj<anzi; j++) {
4689:       if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
4690:         ba_i[j] += aa[nextaj++];
4691:       }
4692:     }

4694:     /* add received vals into ba */
4695:     for (k=0; k<merge->nrecv; k++) { /* k-th received message */
4696:       /* i-th row */
4697:       if (i == *nextrow[k]) {
4698:         anzi   = *(nextai[k]+1) - *nextai[k];
4699:         aj     = buf_rj[k] + *(nextai[k]);
4700:         aa     = abuf_r[k] + *(nextai[k]);
4701:         nextaj = 0;
4702:         for (j=0; nextaj<anzi; j++) {
4703:           if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
4704:             ba_i[j] += aa[nextaj++];
4705:           }
4706:         }
4707:         nextrow[k]++; nextai[k]++;
4708:       }
4709:     }
4710:     MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);
4711:   }
4712:   MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);
4713:   MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);

4715:   PetscFree(abuf_r[0]);
4716:   PetscFree(abuf_r);
4717:   PetscFree(ba_i);
4718:   PetscFree3(buf_ri_k,nextrow,nextai);
4719:   PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);
4720:   return(0);
4721: }

4723: PetscErrorCode  MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
4724: {
4725:   PetscErrorCode      ierr;
4726:   Mat                 B_mpi;
4727:   Mat_SeqAIJ          *a=(Mat_SeqAIJ*)seqmat->data;
4728:   PetscMPIInt         size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
4729:   PetscInt            **buf_rj,**buf_ri,**buf_ri_k;
4730:   PetscInt            M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j;
4731:   PetscInt            len,proc,*dnz,*onz,bs,cbs;
4732:   PetscInt            k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
4733:   PetscInt            nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
4734:   MPI_Request         *si_waits,*sj_waits,*ri_waits,*rj_waits;
4735:   MPI_Status          *status;
4736:   PetscFreeSpaceList  free_space=NULL,current_space=NULL;
4737:   PetscBT             lnkbt;
4738:   Mat_Merge_SeqsToMPI *merge;
4739:   PetscContainer      container;

4742:   PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);

4744:   /* make sure it is a PETSc comm */
4745:   PetscCommDuplicate(comm,&comm,NULL);
4746:   MPI_Comm_size(comm,&size);
4747:   MPI_Comm_rank(comm,&rank);

4749:   PetscNew(&merge);
4750:   PetscMalloc1(size,&status);

4752:   /* determine row ownership */
4753:   /*---------------------------------------------------------*/
4754:   PetscLayoutCreate(comm,&merge->rowmap);
4755:   PetscLayoutSetLocalSize(merge->rowmap,m);
4756:   PetscLayoutSetSize(merge->rowmap,M);
4757:   PetscLayoutSetBlockSize(merge->rowmap,1);
4758:   PetscLayoutSetUp(merge->rowmap);
4759:   PetscMalloc1(size,&len_si);
4760:   PetscMalloc1(size,&merge->len_s);

4762:   m      = merge->rowmap->n;
4763:   owners = merge->rowmap->range;

4765:   /* determine the number of messages to send, their lengths */
4766:   /*---------------------------------------------------------*/
4767:   len_s = merge->len_s;

4769:   len          = 0; /* length of buf_si[] */
4770:   merge->nsend = 0;
4771:   for (proc=0; proc<size; proc++) {
4772:     len_si[proc] = 0;
4773:     if (proc == rank) {
4774:       len_s[proc] = 0;
4775:     } else {
4776:       len_si[proc] = owners[proc+1] - owners[proc] + 1;
4777:       len_s[proc]  = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
4778:     }
4779:     if (len_s[proc]) {
4780:       merge->nsend++;
4781:       nrows = 0;
4782:       for (i=owners[proc]; i<owners[proc+1]; i++) {
4783:         if (ai[i+1] > ai[i]) nrows++;
4784:       }
4785:       len_si[proc] = 2*(nrows+1);
4786:       len         += len_si[proc];
4787:     }
4788:   }

4790:   /* determine the number and length of messages to receive for ij-structure */
4791:   /*-------------------------------------------------------------------------*/
4792:   PetscGatherNumberOfMessages(comm,NULL,len_s,&merge->nrecv);
4793:   PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);

4795:   /* post the Irecv of j-structure */
4796:   /*-------------------------------*/
4797:   PetscCommGetNewTag(comm,&tagj);
4798:   PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);

4800:   /* post the Isend of j-structure */
4801:   /*--------------------------------*/
4802:   PetscMalloc2(merge->nsend,&si_waits,merge->nsend,&sj_waits);

4804:   for (proc=0, k=0; proc<size; proc++) {
4805:     if (!len_s[proc]) continue;
4806:     i    = owners[proc];
4807:     MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);
4808:     k++;
4809:   }

4811:   /* receives and sends of j-structure are complete */
4812:   /*------------------------------------------------*/
4813:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,rj_waits,status);}
4814:   if (merge->nsend) {MPI_Waitall(merge->nsend,sj_waits,status);}

4816:   /* send and recv i-structure */
4817:   /*---------------------------*/
4818:   PetscCommGetNewTag(comm,&tagi);
4819:   PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);

4821:   PetscMalloc1(len+1,&buf_s);
4822:   buf_si = buf_s;  /* points to the beginning of k-th msg to be sent */
4823:   for (proc=0,k=0; proc<size; proc++) {
4824:     if (!len_s[proc]) continue;
4825:     /* form outgoing message for i-structure:
4826:          buf_si[0]:                 nrows to be sent
4827:                [1:nrows]:           row index (global)
4828:                [nrows+1:2*nrows+1]: i-structure index
4829:     */
4830:     /*-------------------------------------------*/
4831:     nrows       = len_si[proc]/2 - 1;
4832:     buf_si_i    = buf_si + nrows+1;
4833:     buf_si[0]   = nrows;
4834:     buf_si_i[0] = 0;
4835:     nrows       = 0;
4836:     for (i=owners[proc]; i<owners[proc+1]; i++) {
4837:       anzi = ai[i+1] - ai[i];
4838:       if (anzi) {
4839:         buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
4840:         buf_si[nrows+1]   = i-owners[proc]; /* local row index */
4841:         nrows++;
4842:       }
4843:     }
4844:     MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);
4845:     k++;
4846:     buf_si += len_si[proc];
4847:   }

4849:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,ri_waits,status);}
4850:   if (merge->nsend) {MPI_Waitall(merge->nsend,si_waits,status);}

4852:   PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);
4853:   for (i=0; i<merge->nrecv; i++) {
4854:     PetscInfo3(seqmat,"recv len_ri=%D, len_rj=%D from [%D]\n",len_ri[i],merge->len_r[i],merge->id_r[i]);
4855:   }

4857:   PetscFree(len_si);
4858:   PetscFree(len_ri);
4859:   PetscFree(rj_waits);
4860:   PetscFree2(si_waits,sj_waits);
4861:   PetscFree(ri_waits);
4862:   PetscFree(buf_s);
4863:   PetscFree(status);

4865:   /* compute a local seq matrix in each processor */
4866:   /*----------------------------------------------*/
4867:   /* allocate bi array and free space for accumulating nonzero column info */
4868:   PetscMalloc1(m+1,&bi);
4869:   bi[0] = 0;

4871:   /* create and initialize a linked list */
4872:   nlnk = N+1;
4873:   PetscLLCreate(N,N,nlnk,lnk,lnkbt);

4875:   /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
4876:   len  = ai[owners[rank+1]] - ai[owners[rank]];
4877:   PetscFreeSpaceGet(PetscIntMultTruncate(2,len)+1,&free_space);

4879:   current_space = free_space;

4881:   /* determine symbolic info for each local row */
4882:   PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);

4884:   for (k=0; k<merge->nrecv; k++) {
4885:     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
4886:     nrows       = *buf_ri_k[k];
4887:     nextrow[k]  = buf_ri_k[k] + 1;  /* next row number of k-th recved i-structure */
4888:     nextai[k]   = buf_ri_k[k] + (nrows + 1); /* points to the next i-structure of k-th recved i-structure  */
4889:   }

4891:   MatPreallocateInitialize(comm,m,n,dnz,onz);
4892:   len  = 0;
4893:   for (i=0; i<m; i++) {
4894:     bnzi = 0;
4895:     /* add local non-zero cols of this proc's seqmat into lnk */
4896:     arow  = owners[rank] + i;
4897:     anzi  = ai[arow+1] - ai[arow];
4898:     aj    = a->j + ai[arow];
4899:     PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);
4900:     bnzi += nlnk;
4901:     /* add received col data into lnk */
4902:     for (k=0; k<merge->nrecv; k++) { /* k-th received message */
4903:       if (i == *nextrow[k]) { /* i-th row */
4904:         anzi  = *(nextai[k]+1) - *nextai[k];
4905:         aj    = buf_rj[k] + *nextai[k];
4906:         PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);
4907:         bnzi += nlnk;
4908:         nextrow[k]++; nextai[k]++;
4909:       }
4910:     }
4911:     if (len < bnzi) len = bnzi;  /* =max(bnzi) */

4913:     /* if free space is not available, make more free space */
4914:     if (current_space->local_remaining<bnzi) {
4915:       PetscFreeSpaceGet(PetscIntSumTruncate(bnzi,current_space->total_array_size),&current_space);
4916:       nspacedouble++;
4917:     }
4918:     /* copy data into free space, then initialize lnk */
4919:     PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);
4920:     MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);

4922:     current_space->array           += bnzi;
4923:     current_space->local_used      += bnzi;
4924:     current_space->local_remaining -= bnzi;

4926:     bi[i+1] = bi[i] + bnzi;
4927:   }

4929:   PetscFree3(buf_ri_k,nextrow,nextai);

4931:   PetscMalloc1(bi[m]+1,&bj);
4932:   PetscFreeSpaceContiguous(&free_space,bj);
4933:   PetscLLDestroy(lnk,lnkbt);

4935:   /* create symbolic parallel matrix B_mpi */
4936:   /*---------------------------------------*/
4937:   MatGetBlockSizes(seqmat,&bs,&cbs);
4938:   MatCreate(comm,&B_mpi);
4939:   if (n==PETSC_DECIDE) {
4940:     MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);
4941:   } else {
4942:     MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
4943:   }
4944:   MatSetBlockSizes(B_mpi,bs,cbs);
4945:   MatSetType(B_mpi,MATMPIAIJ);
4946:   MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);
4947:   MatPreallocateFinalize(dnz,onz);
4948:   MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);

4950:   /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */
4951:   B_mpi->assembled  = PETSC_FALSE;
4952:   merge->bi         = bi;
4953:   merge->bj         = bj;
4954:   merge->buf_ri     = buf_ri;
4955:   merge->buf_rj     = buf_rj;
4956:   merge->coi        = NULL;
4957:   merge->coj        = NULL;
4958:   merge->owners_co  = NULL;

4960:   PetscCommDestroy(&comm);

4962:   /* attach the supporting struct to B_mpi for reuse */
4963:   PetscContainerCreate(PETSC_COMM_SELF,&container);
4964:   PetscContainerSetPointer(container,merge);
4965:   PetscContainerSetUserDestroy(container,MatDestroy_MPIAIJ_SeqsToMPI);
4966:   PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);
4967:   PetscContainerDestroy(&container);
4968:   *mpimat = B_mpi;

4970:   PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);
4971:   return(0);
4972: }

4974: /*@C
4975:       MatCreateMPIAIJSumSeqAIJ - Creates a MATMPIAIJ matrix by adding sequential
4976:                  matrices from each processor

4978:     Collective

4980:    Input Parameters:
4981: +    comm - the communicators the parallel matrix will live on
4982: .    seqmat - the input sequential matrices
4983: .    m - number of local rows (or PETSC_DECIDE)
4984: .    n - number of local columns (or PETSC_DECIDE)
4985: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

4987:    Output Parameter:
4988: .    mpimat - the parallel matrix generated

4990:     Level: advanced

4992:    Notes:
4993:      The dimensions of the sequential matrix in each processor MUST be the same.
4994:      The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
4995:      destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
4996: @*/
4997: PetscErrorCode MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
4998: {
5000:   PetscMPIInt    size;

5003:   MPI_Comm_size(comm,&size);
5004:   if (size == 1) {
5005:     PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);
5006:     if (scall == MAT_INITIAL_MATRIX) {
5007:       MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);
5008:     } else {
5009:       MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);
5010:     }
5011:     PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);
5012:     return(0);
5013:   }
5014:   PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);
5015:   if (scall == MAT_INITIAL_MATRIX) {
5016:     MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);
5017:   }
5018:   MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);
5019:   PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);
5020:   return(0);
5021: }

5023: /*@
5024:      MatMPIAIJGetLocalMat - Creates a SeqAIJ from a MATMPIAIJ matrix by taking all its local rows and putting them into a sequential matrix with
5025:           mlocal rows and n columns. Where mlocal is the row count obtained with MatGetLocalSize() and n is the global column count obtained
5026:           with MatGetSize()

5028:     Not Collective

5030:    Input Parameters:
5031: +    A - the matrix
5032: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

5034:    Output Parameter:
5035: .    A_loc - the local sequential matrix generated

5037:     Level: developer

5039:    Notes:
5040:      When the communicator associated with A has size 1 and MAT_INITIAL_MATRIX is requested, the matrix returned is the diagonal part of A.
5041:      If MAT_REUSE_MATRIX is requested with comm size 1, MatCopy(Adiag,*A_loc,SAME_NONZERO_PATTERN) is called.
5042:      This means that one can preallocate the proper sequential matrix first and then call this routine with MAT_REUSE_MATRIX to safely
5043:      modify the values of the returned A_loc.

5045: .seealso: MatGetOwnershipRange(), MatMPIAIJGetLocalMatCondensed(), MatMPIAIJGetLocalMatMerge()
5046: @*/
5047: PetscErrorCode MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
5048: {
5049:   PetscErrorCode    ierr;
5050:   Mat_MPIAIJ        *mpimat=(Mat_MPIAIJ*)A->data;
5051:   Mat_SeqAIJ        *mat,*a,*b;
5052:   PetscInt          *ai,*aj,*bi,*bj,*cmap=mpimat->garray;
5053:   const PetscScalar *aa,*ba,*aav,*bav;
5054:   PetscScalar       *ca,*cam;
5055:   PetscMPIInt       size;
5056:   PetscInt          am=A->rmap->n,i,j,k,cstart=A->cmap->rstart;
5057:   PetscInt          *ci,*cj,col,ncols_d,ncols_o,jo;
5058:   PetscBool         match;

5061:   PetscStrbeginswith(((PetscObject)A)->type_name,MATMPIAIJ,&match);
5062:   if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MATMPIAIJ matrix as input");
5063:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
5064:   if (size == 1) {
5065:     if (scall == MAT_INITIAL_MATRIX) {
5066:       PetscObjectReference((PetscObject)mpimat->A);
5067:       *A_loc = mpimat->A;
5068:     } else if (scall == MAT_REUSE_MATRIX) {
5069:       MatCopy(mpimat->A,*A_loc,SAME_NONZERO_PATTERN);
5070:     }
5071:     return(0);
5072:   }

5074:   PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);
5075:   a = (Mat_SeqAIJ*)(mpimat->A)->data;
5076:   b = (Mat_SeqAIJ*)(mpimat->B)->data;
5077:   ai = a->i; aj = a->j; bi = b->i; bj = b->j;
5078:   MatSeqAIJGetArrayRead(mpimat->A,&aav);
5079:   MatSeqAIJGetArrayRead(mpimat->B,&bav);
5080:   aa   = aav;
5081:   ba   = bav;
5082:   if (scall == MAT_INITIAL_MATRIX) {
5083:     PetscMalloc1(1+am,&ci);
5084:     ci[0] = 0;
5085:     for (i=0; i<am; i++) {
5086:       ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
5087:     }
5088:     PetscMalloc1(1+ci[am],&cj);
5089:     PetscMalloc1(1+ci[am],&ca);
5090:     k    = 0;
5091:     for (i=0; i<am; i++) {
5092:       ncols_o = bi[i+1] - bi[i];
5093:       ncols_d = ai[i+1] - ai[i];
5094:       /* off-diagonal portion of A */
5095:       for (jo=0; jo<ncols_o; jo++) {
5096:         col = cmap[*bj];
5097:         if (col >= cstart) break;
5098:         cj[k]   = col; bj++;
5099:         ca[k++] = *ba++;
5100:       }
5101:       /* diagonal portion of A */
5102:       for (j=0; j<ncols_d; j++) {
5103:         cj[k]   = cstart + *aj++;
5104:         ca[k++] = *aa++;
5105:       }
5106:       /* off-diagonal portion of A */
5107:       for (j=jo; j<ncols_o; j++) {
5108:         cj[k]   = cmap[*bj++];
5109:         ca[k++] = *ba++;
5110:       }
5111:     }
5112:     /* put together the new matrix */
5113:     MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);
5114:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5115:     /* Since these are PETSc arrays, change flags to free them as necessary. */
5116:     mat          = (Mat_SeqAIJ*)(*A_loc)->data;
5117:     mat->free_a  = PETSC_TRUE;
5118:     mat->free_ij = PETSC_TRUE;
5119:     mat->nonew   = 0;
5120:   } else if (scall == MAT_REUSE_MATRIX) {
5121:     mat=(Mat_SeqAIJ*)(*A_loc)->data;
5122: #if defined(PETSC_USE_DEVICE)
5123:     (*A_loc)->offloadmask = PETSC_OFFLOAD_CPU;
5124: #endif
5125:     ci = mat->i; cj = mat->j; cam = mat->a;
5126:     for (i=0; i<am; i++) {
5127:       /* off-diagonal portion of A */
5128:       ncols_o = bi[i+1] - bi[i];
5129:       for (jo=0; jo<ncols_o; jo++) {
5130:         col = cmap[*bj];
5131:         if (col >= cstart) break;
5132:         *cam++ = *ba++; bj++;
5133:       }
5134:       /* diagonal portion of A */
5135:       ncols_d = ai[i+1] - ai[i];
5136:       for (j=0; j<ncols_d; j++) *cam++ = *aa++;
5137:       /* off-diagonal portion of A */
5138:       for (j=jo; j<ncols_o; j++) {
5139:         *cam++ = *ba++; bj++;
5140:       }
5141:     }
5142:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
5143:   MatSeqAIJRestoreArrayRead(mpimat->A,&aav);
5144:   MatSeqAIJRestoreArrayRead(mpimat->B,&bav);
5145:   PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);
5146:   return(0);
5147: }

5149: /*@
5150:      MatMPIAIJGetLocalMatMerge - Creates a SeqAIJ from a MATMPIAIJ matrix by taking all its local rows and putting them into a sequential matrix with
5151:           mlocal rows and n columns. Where n is the sum of the number of columns of the diagonal and offdiagonal part

5153:     Not Collective

5155:    Input Parameters:
5156: +    A - the matrix
5157: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

5159:    Output Parameters:
5160: +    glob - sequential IS with global indices associated with the columns of the local sequential matrix generated (can be NULL)
5161: -    A_loc - the local sequential matrix generated

5163:     Level: developer

5165:    Notes:
5166:      This is different from MatMPIAIJGetLocalMat() since the first columns in the returning matrix are those associated with the diagonal part, then those associated with the offdiagonal part (in its local ordering)

5168: .seealso: MatGetOwnershipRange(), MatMPIAIJGetLocalMat(), MatMPIAIJGetLocalMatCondensed()

5170: @*/
5171: PetscErrorCode MatMPIAIJGetLocalMatMerge(Mat A,MatReuse scall,IS *glob,Mat *A_loc)
5172: {
5174:   Mat            Ao,Ad;
5175:   const PetscInt *cmap;
5176:   PetscMPIInt    size;
5177:   PetscErrorCode (*f)(Mat,MatReuse,IS*,Mat*);

5180:   MatMPIAIJGetSeqAIJ(A,&Ad,&Ao,&cmap);
5181:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
5182:   if (size == 1) {
5183:     if (scall == MAT_INITIAL_MATRIX) {
5184:       PetscObjectReference((PetscObject)Ad);
5185:       *A_loc = Ad;
5186:     } else if (scall == MAT_REUSE_MATRIX) {
5187:       MatCopy(Ad,*A_loc,SAME_NONZERO_PATTERN);
5188:     }
5189:     if (glob) { ISCreateStride(PetscObjectComm((PetscObject)Ad),Ad->cmap->n,Ad->cmap->rstart,1,glob); }
5190:     return(0);
5191:   }
5192:   PetscObjectQueryFunction((PetscObject)A,"MatMPIAIJGetLocalMatMerge_C",&f);
5193:   PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);
5194:   if (f) {
5195:     (*f)(A,scall,glob,A_loc);
5196:   } else {
5197:     Mat_SeqAIJ        *a = (Mat_SeqAIJ*)Ad->data;
5198:     Mat_SeqAIJ        *b = (Mat_SeqAIJ*)Ao->data;
5199:     Mat_SeqAIJ        *c;
5200:     PetscInt          *ai = a->i, *aj = a->j;
5201:     PetscInt          *bi = b->i, *bj = b->j;
5202:     PetscInt          *ci,*cj;
5203:     const PetscScalar *aa,*ba;
5204:     PetscScalar       *ca;
5205:     PetscInt          i,j,am,dn,on;

5207:     MatGetLocalSize(Ad,&am,&dn);
5208:     MatGetLocalSize(Ao,NULL,&on);
5209:     MatSeqAIJGetArrayRead(Ad,&aa);
5210:     MatSeqAIJGetArrayRead(Ao,&ba);
5211:     if (scall == MAT_INITIAL_MATRIX) {
5212:       PetscInt k;
5213:       PetscMalloc1(1+am,&ci);
5214:       PetscMalloc1(ai[am]+bi[am],&cj);
5215:       PetscMalloc1(ai[am]+bi[am],&ca);
5216:       ci[0] = 0;
5217:       for (i=0,k=0; i<am; i++) {
5218:         const PetscInt ncols_o = bi[i+1] - bi[i];
5219:         const PetscInt ncols_d = ai[i+1] - ai[i];
5220:         ci[i+1] = ci[i] + ncols_o + ncols_d;
5221:         /* diagonal portion of A */
5222:         for (j=0; j<ncols_d; j++,k++) {
5223:           cj[k] = *aj++;
5224:           ca[k] = *aa++;
5225:         }
5226:         /* off-diagonal portion of A */
5227:         for (j=0; j<ncols_o; j++,k++) {
5228:           cj[k] = dn + *bj++;
5229:           ca[k] = *ba++;
5230:         }
5231:       }
5232:       /* put together the new matrix */
5233:       MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,dn+on,ci,cj,ca,A_loc);
5234:       /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5235:       /* Since these are PETSc arrays, change flags to free them as necessary. */
5236:       c          = (Mat_SeqAIJ*)(*A_loc)->data;
5237:       c->free_a  = PETSC_TRUE;
5238:       c->free_ij = PETSC_TRUE;
5239:       c->nonew   = 0;
5240:       MatSetType(*A_loc,((PetscObject)Ad)->type_name);
5241:     } else if (scall == MAT_REUSE_MATRIX) {
5242: #if defined(PETSC_HAVE_DEVICE)
5243:       (*A_loc)->offloadmask = PETSC_OFFLOAD_CPU;
5244: #endif
5245:       c  = (Mat_SeqAIJ*)(*A_loc)->data;
5246:       ca = c->a;
5247:       for (i=0; i<am; i++) {
5248:         const PetscInt ncols_d = ai[i+1] - ai[i];
5249:         const PetscInt ncols_o = bi[i+1] - bi[i];
5250:         /* diagonal portion of A */
5251:         for (j=0; j<ncols_d; j++) *ca++ = *aa++;
5252:         /* off-diagonal portion of A */
5253:         for (j=0; j<ncols_o; j++) *ca++ = *ba++;
5254:       }
5255:     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
5256:     MatSeqAIJRestoreArrayRead(Ad,&aa);
5257:     MatSeqAIJRestoreArrayRead(Ao,&aa);
5258:     if (glob) {
5259:       PetscInt cst, *gidx;

5261:       MatGetOwnershipRangeColumn(A,&cst,NULL);
5262:       PetscMalloc1(dn+on,&gidx);
5263:       for (i=0; i<dn; i++) gidx[i]    = cst + i;
5264:       for (i=0; i<on; i++) gidx[i+dn] = cmap[i];
5265:       ISCreateGeneral(PetscObjectComm((PetscObject)Ad),dn+on,gidx,PETSC_OWN_POINTER,glob);
5266:     }
5267:   }
5268:   PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);
5269:   return(0);
5270: }

5272: /*@C
5273:      MatMPIAIJGetLocalMatCondensed - Creates a SeqAIJ matrix from an MATMPIAIJ matrix by taking all its local rows and NON-ZERO columns

5275:     Not Collective

5277:    Input Parameters:
5278: +    A - the matrix
5279: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5280: -    row, col - index sets of rows and columns to extract (or NULL)

5282:    Output Parameter:
5283: .    A_loc - the local sequential matrix generated

5285:     Level: developer

5287: .seealso: MatGetOwnershipRange(), MatMPIAIJGetLocalMat()

5289: @*/
5290: PetscErrorCode MatMPIAIJGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
5291: {
5292:   Mat_MPIAIJ     *a=(Mat_MPIAIJ*)A->data;
5294:   PetscInt       i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
5295:   IS             isrowa,iscola;
5296:   Mat            *aloc;
5297:   PetscBool      match;

5300:   PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);
5301:   if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MATMPIAIJ matrix as input");
5302:   PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);
5303:   if (!row) {
5304:     start = A->rmap->rstart; end = A->rmap->rend;
5305:     ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);
5306:   } else {
5307:     isrowa = *row;
5308:   }
5309:   if (!col) {
5310:     start = A->cmap->rstart;
5311:     cmap  = a->garray;
5312:     nzA   = a->A->cmap->n;
5313:     nzB   = a->B->cmap->n;
5314:     PetscMalloc1(nzA+nzB, &idx);
5315:     ncols = 0;
5316:     for (i=0; i<nzB; i++) {
5317:       if (cmap[i] < start) idx[ncols++] = cmap[i];
5318:       else break;
5319:     }
5320:     imark = i;
5321:     for (i=0; i<nzA; i++) idx[ncols++] = start + i;
5322:     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
5323:     ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);
5324:   } else {
5325:     iscola = *col;
5326:   }
5327:   if (scall != MAT_INITIAL_MATRIX) {
5328:     PetscMalloc1(1,&aloc);
5329:     aloc[0] = *A_loc;
5330:   }
5331:   MatCreateSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);
5332:   if (!col) { /* attach global id of condensed columns */
5333:     PetscObjectCompose((PetscObject)aloc[0],"_petsc_GetLocalMatCondensed_iscol",(PetscObject)iscola);
5334:   }
5335:   *A_loc = aloc[0];
5336:   PetscFree(aloc);
5337:   if (!row) {
5338:     ISDestroy(&isrowa);
5339:   }
5340:   if (!col) {
5341:     ISDestroy(&iscola);
5342:   }
5343:   PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);
5344:   return(0);
5345: }

5347: /*
5348:  * Create a sequential AIJ matrix based on row indices. a whole column is extracted once a row is matched.
5349:  * Row could be local or remote.The routine is designed to be scalable in memory so that nothing is based
5350:  * on a global size.
5351:  * */
5352: PetscErrorCode MatCreateSeqSubMatrixWithRows_Private(Mat P,IS rows,Mat *P_oth)
5353: {
5354:   Mat_MPIAIJ               *p=(Mat_MPIAIJ*)P->data;
5355:   Mat_SeqAIJ               *pd=(Mat_SeqAIJ*)(p->A)->data,*po=(Mat_SeqAIJ*)(p->B)->data,*p_oth;
5356:   PetscInt                 plocalsize,nrows,*ilocal,*oilocal,i,lidx,*nrcols,*nlcols,ncol;
5357:   PetscMPIInt              owner;
5358:   PetscSFNode              *iremote,*oiremote;
5359:   const PetscInt           *lrowindices;
5360:   PetscErrorCode           ierr;
5361:   PetscSF                  sf,osf;
5362:   PetscInt                 pcstart,*roffsets,*loffsets,*pnnz,j;
5363:   PetscInt                 ontotalcols,dntotalcols,ntotalcols,nout;
5364:   MPI_Comm                 comm;
5365:   ISLocalToGlobalMapping   mapping;

5368:   PetscObjectGetComm((PetscObject)P,&comm);
5369:   /* plocalsize is the number of roots
5370:    * nrows is the number of leaves
5371:    * */
5372:   MatGetLocalSize(P,&plocalsize,NULL);
5373:   ISGetLocalSize(rows,&nrows);
5374:   PetscCalloc1(nrows,&iremote);
5375:   ISGetIndices(rows,&lrowindices);
5376:   for (i=0;i<nrows;i++) {
5377:     /* Find a remote index and an owner for a row
5378:      * The row could be local or remote
5379:      * */
5380:     owner = 0;
5381:     lidx  = 0;
5382:     PetscLayoutFindOwnerIndex(P->rmap,lrowindices[i],&owner,&lidx);
5383:     iremote[i].index = lidx;
5384:     iremote[i].rank  = owner;
5385:   }
5386:   /* Create SF to communicate how many nonzero columns for each row */
5387:   PetscSFCreate(comm,&sf);
5388:   /* SF will figure out the number of nonzero colunms for each row, and their
5389:    * offsets
5390:    * */
5391:   PetscSFSetGraph(sf,plocalsize,nrows,NULL,PETSC_OWN_POINTER,iremote,PETSC_OWN_POINTER);
5392:   PetscSFSetFromOptions(sf);
5393:   PetscSFSetUp(sf);

5395:   PetscCalloc1(2*(plocalsize+1),&roffsets);
5396:   PetscCalloc1(2*plocalsize,&nrcols);
5397:   PetscCalloc1(nrows,&pnnz);
5398:   roffsets[0] = 0;
5399:   roffsets[1] = 0;
5400:   for (i=0;i<plocalsize;i++) {
5401:     /* diag */
5402:     nrcols[i*2+0] = pd->i[i+1] - pd->i[i];
5403:     /* off diag */
5404:     nrcols[i*2+1] = po->i[i+1] - po->i[i];
5405:     /* compute offsets so that we relative location for each row */
5406:     roffsets[(i+1)*2+0] = roffsets[i*2+0] + nrcols[i*2+0];
5407:     roffsets[(i+1)*2+1] = roffsets[i*2+1] + nrcols[i*2+1];
5408:   }
5409:   PetscCalloc1(2*nrows,&nlcols);
5410:   PetscCalloc1(2*nrows,&loffsets);
5411:   /* 'r' means root, and 'l' means leaf */
5412:   PetscSFBcastBegin(sf,MPIU_2INT,nrcols,nlcols,MPI_REPLACE);
5413:   PetscSFBcastBegin(sf,MPIU_2INT,roffsets,loffsets,MPI_REPLACE);
5414:   PetscSFBcastEnd(sf,MPIU_2INT,nrcols,nlcols,MPI_REPLACE);
5415:   PetscSFBcastEnd(sf,MPIU_2INT,roffsets,loffsets,MPI_REPLACE);
5416:   PetscSFDestroy(&sf);
5417:   PetscFree(roffsets);
5418:   PetscFree(nrcols);
5419:   dntotalcols = 0;
5420:   ontotalcols = 0;
5421:   ncol = 0;
5422:   for (i=0;i<nrows;i++) {
5423:     pnnz[i] = nlcols[i*2+0] + nlcols[i*2+1];
5424:     ncol = PetscMax(pnnz[i],ncol);
5425:     /* diag */
5426:     dntotalcols += nlcols[i*2+0];
5427:     /* off diag */
5428:     ontotalcols += nlcols[i*2+1];
5429:   }
5430:   /* We do not need to figure the right number of columns
5431:    * since all the calculations will be done by going through the raw data
5432:    * */
5433:   MatCreateSeqAIJ(PETSC_COMM_SELF,nrows,ncol,0,pnnz,P_oth);
5434:   MatSetUp(*P_oth);
5435:   PetscFree(pnnz);
5436:   p_oth = (Mat_SeqAIJ*) (*P_oth)->data;
5437:   /* diag */
5438:   PetscCalloc1(dntotalcols,&iremote);
5439:   /* off diag */
5440:   PetscCalloc1(ontotalcols,&oiremote);
5441:   /* diag */
5442:   PetscCalloc1(dntotalcols,&ilocal);
5443:   /* off diag */
5444:   PetscCalloc1(ontotalcols,&oilocal);
5445:   dntotalcols = 0;
5446:   ontotalcols = 0;
5447:   ntotalcols  = 0;
5448:   for (i=0;i<nrows;i++) {
5449:     owner = 0;
5450:     PetscLayoutFindOwnerIndex(P->rmap,lrowindices[i],&owner,NULL);
5451:     /* Set iremote for diag matrix */
5452:     for (j=0;j<nlcols[i*2+0];j++) {
5453:       iremote[dntotalcols].index   = loffsets[i*2+0] + j;
5454:       iremote[dntotalcols].rank    = owner;
5455:       /* P_oth is seqAIJ so that ilocal need to point to the first part of memory */
5456:       ilocal[dntotalcols++]        = ntotalcols++;
5457:     }
5458:     /* off diag */
5459:     for (j=0;j<nlcols[i*2+1];j++) {
5460:       oiremote[ontotalcols].index   = loffsets[i*2+1] + j;
5461:       oiremote[ontotalcols].rank    = owner;
5462:       oilocal[ontotalcols++]        = ntotalcols++;
5463:     }
5464:   }
5465:   ISRestoreIndices(rows,&lrowindices);
5466:   PetscFree(loffsets);
5467:   PetscFree(nlcols);
5468:   PetscSFCreate(comm,&sf);
5469:   /* P serves as roots and P_oth is leaves
5470:    * Diag matrix
5471:    * */
5472:   PetscSFSetGraph(sf,pd->i[plocalsize],dntotalcols,ilocal,PETSC_OWN_POINTER,iremote,PETSC_OWN_POINTER);
5473:   PetscSFSetFromOptions(sf);
5474:   PetscSFSetUp(sf);

5476:   PetscSFCreate(comm,&osf);
5477:   /* Off diag */
5478:   PetscSFSetGraph(osf,po->i[plocalsize],ontotalcols,oilocal,PETSC_OWN_POINTER,oiremote,PETSC_OWN_POINTER);
5479:   PetscSFSetFromOptions(osf);
5480:   PetscSFSetUp(osf);
5481:   /* We operate on the matrix internal data for saving memory */
5482:   PetscSFBcastBegin(sf,MPIU_SCALAR,pd->a,p_oth->a,MPI_REPLACE);
5483:   PetscSFBcastBegin(osf,MPIU_SCALAR,po->a,p_oth->a,MPI_REPLACE);
5484:   MatGetOwnershipRangeColumn(P,&pcstart,NULL);
5485:   /* Convert to global indices for diag matrix */
5486:   for (i=0;i<pd->i[plocalsize];i++) pd->j[i] += pcstart;
5487:   PetscSFBcastBegin(sf,MPIU_INT,pd->j,p_oth->j,MPI_REPLACE);
5488:   /* We want P_oth store global indices */
5489:   ISLocalToGlobalMappingCreate(comm,1,p->B->cmap->n,p->garray,PETSC_COPY_VALUES,&mapping);
5490:   /* Use memory scalable approach */
5491:   ISLocalToGlobalMappingSetType(mapping,ISLOCALTOGLOBALMAPPINGHASH);
5492:   ISLocalToGlobalMappingApply(mapping,po->i[plocalsize],po->j,po->j);
5493:   PetscSFBcastBegin(osf,MPIU_INT,po->j,p_oth->j,MPI_REPLACE);
5494:   PetscSFBcastEnd(sf,MPIU_INT,pd->j,p_oth->j,MPI_REPLACE);
5495:   /* Convert back to local indices */
5496:   for (i=0;i<pd->i[plocalsize];i++) pd->j[i] -= pcstart;
5497:   PetscSFBcastEnd(osf,MPIU_INT,po->j,p_oth->j,MPI_REPLACE);
5498:   nout = 0;
5499:   ISGlobalToLocalMappingApply(mapping,IS_GTOLM_DROP,po->i[plocalsize],po->j,&nout,po->j);
5500:   if (nout != po->i[plocalsize]) SETERRQ2(comm,PETSC_ERR_ARG_INCOMP,"n %D does not equal to nout %D \n",po->i[plocalsize],nout);
5501:   ISLocalToGlobalMappingDestroy(&mapping);
5502:   /* Exchange values */
5503:   PetscSFBcastEnd(sf,MPIU_SCALAR,pd->a,p_oth->a,MPI_REPLACE);
5504:   PetscSFBcastEnd(osf,MPIU_SCALAR,po->a,p_oth->a,MPI_REPLACE);
5505:   /* Stop PETSc from shrinking memory */
5506:   for (i=0;i<nrows;i++) p_oth->ilen[i] = p_oth->imax[i];
5507:   MatAssemblyBegin(*P_oth,MAT_FINAL_ASSEMBLY);
5508:   MatAssemblyEnd(*P_oth,MAT_FINAL_ASSEMBLY);
5509:   /* Attach PetscSF objects to P_oth so that we can reuse it later */
5510:   PetscObjectCompose((PetscObject)*P_oth,"diagsf",(PetscObject)sf);
5511:   PetscObjectCompose((PetscObject)*P_oth,"offdiagsf",(PetscObject)osf);
5512:   PetscSFDestroy(&sf);
5513:   PetscSFDestroy(&osf);
5514:   return(0);
5515: }

5517: /*
5518:  * Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A
5519:  * This supports MPIAIJ and MAIJ
5520:  * */
5521: PetscErrorCode MatGetBrowsOfAcols_MPIXAIJ(Mat A,Mat P,PetscInt dof,MatReuse reuse,Mat *P_oth)
5522: {
5523:   Mat_MPIAIJ            *a=(Mat_MPIAIJ*)A->data,*p=(Mat_MPIAIJ*)P->data;
5524:   Mat_SeqAIJ            *p_oth;
5525:   Mat_SeqAIJ            *pd=(Mat_SeqAIJ*)(p->A)->data,*po=(Mat_SeqAIJ*)(p->B)->data;
5526:   IS                    rows,map;
5527:   PetscHMapI            hamp;
5528:   PetscInt              i,htsize,*rowindices,off,*mapping,key,count;
5529:   MPI_Comm              comm;
5530:   PetscSF               sf,osf;
5531:   PetscBool             has;
5532:   PetscErrorCode        ierr;

5535:   PetscObjectGetComm((PetscObject)A,&comm);
5536:   PetscLogEventBegin(MAT_GetBrowsOfAocols,A,P,0,0);
5537:   /* If it is the first time, create an index set of off-diag nonzero columns of A,
5538:    *  and then create a submatrix (that often is an overlapping matrix)
5539:    * */
5540:   if (reuse == MAT_INITIAL_MATRIX) {
5541:     /* Use a hash table to figure out unique keys */
5542:     PetscHMapICreate(&hamp);
5543:     PetscHMapIResize(hamp,a->B->cmap->n);
5544:     PetscCalloc1(a->B->cmap->n,&mapping);
5545:     count = 0;
5546:     /* Assume that  a->g is sorted, otherwise the following does not make sense */
5547:     for (i=0;i<a->B->cmap->n;i++) {
5548:       key  = a->garray[i]/dof;
5549:       PetscHMapIHas(hamp,key,&has);
5550:       if (!has) {
5551:         mapping[i] = count;
5552:         PetscHMapISet(hamp,key,count++);
5553:       } else {
5554:         /* Current 'i' has the same value the previous step */
5555:         mapping[i] = count-1;
5556:       }
5557:     }
5558:     ISCreateGeneral(comm,a->B->cmap->n,mapping,PETSC_OWN_POINTER,&map);
5559:     PetscHMapIGetSize(hamp,&htsize);
5560:     if (htsize!=count) SETERRQ2(comm,PETSC_ERR_ARG_INCOMP," Size of hash map %D is inconsistent with count %D \n",htsize,count);
5561:     PetscCalloc1(htsize,&rowindices);
5562:     off = 0;
5563:     PetscHMapIGetKeys(hamp,&off,rowindices);
5564:     PetscHMapIDestroy(&hamp);
5565:     PetscSortInt(htsize,rowindices);
5566:     ISCreateGeneral(comm,htsize,rowindices,PETSC_OWN_POINTER,&rows);
5567:     /* In case, the matrix was already created but users want to recreate the matrix */
5568:     MatDestroy(P_oth);
5569:     MatCreateSeqSubMatrixWithRows_Private(P,rows,P_oth);
5570:     PetscObjectCompose((PetscObject)*P_oth,"aoffdiagtopothmapping",(PetscObject)map);
5571:     ISDestroy(&map);
5572:     ISDestroy(&rows);
5573:   } else if (reuse == MAT_REUSE_MATRIX) {
5574:     /* If matrix was already created, we simply update values using SF objects
5575:      * that as attached to the matrix ealier.
5576:      *  */
5577:     PetscObjectQuery((PetscObject)*P_oth,"diagsf",(PetscObject*)&sf);
5578:     PetscObjectQuery((PetscObject)*P_oth,"offdiagsf",(PetscObject*)&osf);
5579:     if (!sf || !osf) SETERRQ(comm,PETSC_ERR_ARG_NULL,"Matrix is not initialized yet");
5580:     p_oth = (Mat_SeqAIJ*) (*P_oth)->data;
5581:     /* Update values in place */
5582:     PetscSFBcastBegin(sf,MPIU_SCALAR,pd->a,p_oth->a,MPI_REPLACE);
5583:     PetscSFBcastBegin(osf,MPIU_SCALAR,po->a,p_oth->a,MPI_REPLACE);
5584:     PetscSFBcastEnd(sf,MPIU_SCALAR,pd->a,p_oth->a,MPI_REPLACE);
5585:     PetscSFBcastEnd(osf,MPIU_SCALAR,po->a,p_oth->a,MPI_REPLACE);
5586:   } else SETERRQ(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown reuse type");
5587:   PetscLogEventEnd(MAT_GetBrowsOfAocols,A,P,0,0);
5588:   return(0);
5589: }

5591: /*@C
5592:     MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A

5594:     Collective on Mat

5596:    Input Parameters:
5597: +    A - the first matrix in mpiaij format
5598: .    B - the second matrix in mpiaij format
5599: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

5601:    Input/Output Parameters:
5602: +    rowb - index sets of rows of B to extract (or NULL), modified on output
5603: -    colb - index sets of columns of B to extract (or NULL), modified on output

5605:    Output Parameter:
5606: .    B_seq - the sequential matrix generated

5608:     Level: developer

5610: @*/
5611: PetscErrorCode MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,Mat *B_seq)
5612: {
5613:   Mat_MPIAIJ     *a=(Mat_MPIAIJ*)A->data;
5615:   PetscInt       *idx,i,start,ncols,nzA,nzB,*cmap,imark;
5616:   IS             isrowb,iscolb;
5617:   Mat            *bseq=NULL;

5620:   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5621:     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);
5622:   }
5623:   PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);

5625:   if (scall == MAT_INITIAL_MATRIX) {
5626:     start = A->cmap->rstart;
5627:     cmap  = a->garray;
5628:     nzA   = a->A->cmap->n;
5629:     nzB   = a->B->cmap->n;
5630:     PetscMalloc1(nzA+nzB, &idx);
5631:     ncols = 0;
5632:     for (i=0; i<nzB; i++) {  /* row < local row index */
5633:       if (cmap[i] < start) idx[ncols++] = cmap[i];
5634:       else break;
5635:     }
5636:     imark = i;
5637:     for (i=0; i<nzA; i++) idx[ncols++] = start + i;  /* local rows */
5638:     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
5639:     ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);
5640:     ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);
5641:   } else {
5642:     if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
5643:     isrowb  = *rowb; iscolb = *colb;
5644:     PetscMalloc1(1,&bseq);
5645:     bseq[0] = *B_seq;
5646:   }
5647:   MatCreateSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);
5648:   *B_seq = bseq[0];
5649:   PetscFree(bseq);
5650:   if (!rowb) {
5651:     ISDestroy(&isrowb);
5652:   } else {
5653:     *rowb = isrowb;
5654:   }
5655:   if (!colb) {
5656:     ISDestroy(&iscolb);
5657:   } else {
5658:     *colb = iscolb;
5659:   }
5660:   PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);
5661:   return(0);
5662: }

5664: /*
5665:     MatGetBrowsOfAoCols_MPIAIJ - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns
5666:     of the OFF-DIAGONAL portion of local A

5668:     Collective on Mat

5670:    Input Parameters:
5671: +    A,B - the matrices in mpiaij format
5672: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

5674:    Output Parameter:
5675: +    startsj_s - starting point in B's sending j-arrays, saved for MAT_REUSE (or NULL)
5676: .    startsj_r - starting point in B's receiving j-arrays, saved for MAT_REUSE (or NULL)
5677: .    bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or NULL)
5678: -    B_oth - the sequential matrix generated with size aBn=a->B->cmap->n by B->cmap->N

5680:     Developer Notes: This directly accesses information inside the VecScatter associated with the matrix-vector product
5681:      for this matrix. This is not desirable..

5683:     Level: developer

5685: */
5686: PetscErrorCode MatGetBrowsOfAoCols_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscInt **startsj_s,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth)
5687: {
5688:   PetscErrorCode         ierr;
5689:   Mat_MPIAIJ             *a=(Mat_MPIAIJ*)A->data;
5690:   Mat_SeqAIJ             *b_oth;
5691:   VecScatter             ctx;
5692:   MPI_Comm               comm;
5693:   const PetscMPIInt      *rprocs,*sprocs;
5694:   const PetscInt         *srow,*rstarts,*sstarts;
5695:   PetscInt               *rowlen,*bufj,*bufJ,ncols = 0,aBn=a->B->cmap->n,row,*b_othi,*b_othj,*rvalues=NULL,*svalues=NULL,*cols,sbs,rbs;
5696:   PetscInt               i,j,k=0,l,ll,nrecvs,nsends,nrows,*rstartsj = NULL,*sstartsj,len;
5697:   PetscScalar            *b_otha,*bufa,*bufA,*vals = NULL;
5698:   MPI_Request            *reqs = NULL,*rwaits = NULL,*swaits = NULL;
5699:   PetscMPIInt            size,tag,rank,nreqs;

5702:   PetscObjectGetComm((PetscObject)A,&comm);
5703:   MPI_Comm_size(comm,&size);

5705:   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5706:     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);
5707:   }
5708:   PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);
5709:   MPI_Comm_rank(comm,&rank);

5711:   if (size == 1) {
5712:     startsj_s = NULL;
5713:     bufa_ptr  = NULL;
5714:     *B_oth    = NULL;
5715:     return(0);
5716:   }

5718:   ctx = a->Mvctx;
5719:   tag = ((PetscObject)ctx)->tag;

5721:   VecScatterGetRemote_Private(ctx,PETSC_TRUE/*send*/,&nsends,&sstarts,&srow,&sprocs,&sbs);
5722:   /* rprocs[] must be ordered so that indices received from them are ordered in rvalues[], which is key to algorithms used in this subroutine */
5723:   VecScatterGetRemoteOrdered_Private(ctx,PETSC_FALSE/*recv*/,&nrecvs,&rstarts,NULL/*indices not needed*/,&rprocs,&rbs);
5724:   PetscMPIIntCast(nsends+nrecvs,&nreqs);
5725:   PetscMalloc1(nreqs,&reqs);
5726:   rwaits = reqs;
5727:   swaits = reqs + nrecvs;

5729:   if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
5730:   if (scall == MAT_INITIAL_MATRIX) {
5731:     /* i-array */
5732:     /*---------*/
5733:     /*  post receives */
5734:     if (nrecvs) {PetscMalloc1(rbs*(rstarts[nrecvs] - rstarts[0]),&rvalues);} /* rstarts can be NULL when nrecvs=0 */
5735:     for (i=0; i<nrecvs; i++) {
5736:       rowlen = rvalues + rstarts[i]*rbs;
5737:       nrows  = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */
5738:       MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
5739:     }

5741:     /* pack the outgoing message */
5742:     PetscMalloc2(nsends+1,&sstartsj,nrecvs+1,&rstartsj);

5744:     sstartsj[0] = 0;
5745:     rstartsj[0] = 0;
5746:     len         = 0; /* total length of j or a array to be sent */
5747:     if (nsends) {
5748:       k    = sstarts[0]; /* ATTENTION: sstarts[0] and rstarts[0] are not necessarily zero */
5749:       PetscMalloc1(sbs*(sstarts[nsends]-sstarts[0]),&svalues);
5750:     }
5751:     for (i=0; i<nsends; i++) {
5752:       rowlen = svalues + (sstarts[i]-sstarts[0])*sbs;
5753:       nrows  = sstarts[i+1]-sstarts[i]; /* num of block rows */
5754:       for (j=0; j<nrows; j++) {
5755:         row = srow[k] + B->rmap->range[rank]; /* global row idx */
5756:         for (l=0; l<sbs; l++) {
5757:           MatGetRow_MPIAIJ(B,row+l,&ncols,NULL,NULL); /* rowlength */

5759:           rowlen[j*sbs+l] = ncols;

5761:           len += ncols;
5762:           MatRestoreRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);
5763:         }
5764:         k++;
5765:       }
5766:       MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);

5768:       sstartsj[i+1] = len;  /* starting point of (i+1)-th outgoing msg in bufj and bufa */
5769:     }
5770:     /* recvs and sends of i-array are completed */
5771:     if (nreqs) {MPI_Waitall(nreqs,reqs,MPI_STATUSES_IGNORE);}
5772:     PetscFree(svalues);

5774:     /* allocate buffers for sending j and a arrays */
5775:     PetscMalloc1(len+1,&bufj);
5776:     PetscMalloc1(len+1,&bufa);

5778:     /* create i-array of B_oth */
5779:     PetscMalloc1(aBn+2,&b_othi);

5781:     b_othi[0] = 0;
5782:     len       = 0; /* total length of j or a array to be received */
5783:     k         = 0;
5784:     for (i=0; i<nrecvs; i++) {
5785:       rowlen = rvalues + (rstarts[i]-rstarts[0])*rbs;
5786:       nrows  = (rstarts[i+1]-rstarts[i])*rbs; /* num of rows to be received */
5787:       for (j=0; j<nrows; j++) {
5788:         b_othi[k+1] = b_othi[k] + rowlen[j];
5789:         PetscIntSumError(rowlen[j],len,&len);
5790:         k++;
5791:       }
5792:       rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
5793:     }
5794:     PetscFree(rvalues);

5796:     /* allocate space for j and a arrrays of B_oth */
5797:     PetscMalloc1(b_othi[aBn]+1,&b_othj);
5798:     PetscMalloc1(b_othi[aBn]+1,&b_otha);

5800:     /* j-array */
5801:     /*---------*/
5802:     /*  post receives of j-array */
5803:     for (i=0; i<nrecvs; i++) {
5804:       nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
5805:       MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
5806:     }

5808:     /* pack the outgoing message j-array */
5809:     if (nsends) k = sstarts[0];
5810:     for (i=0; i<nsends; i++) {
5811:       nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5812:       bufJ  = bufj+sstartsj[i];
5813:       for (j=0; j<nrows; j++) {
5814:         row = srow[k++] + B->rmap->range[rank];  /* global row idx */
5815:         for (ll=0; ll<sbs; ll++) {
5816:           MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);
5817:           for (l=0; l<ncols; l++) {
5818:             *bufJ++ = cols[l];
5819:           }
5820:           MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);
5821:         }
5822:       }
5823:       MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);
5824:     }

5826:     /* recvs and sends of j-array are completed */
5827:     if (nreqs) {MPI_Waitall(nreqs,reqs,MPI_STATUSES_IGNORE);}
5828:   } else if (scall == MAT_REUSE_MATRIX) {
5829:     sstartsj = *startsj_s;
5830:     rstartsj = *startsj_r;
5831:     bufa     = *bufa_ptr;
5832:     b_oth    = (Mat_SeqAIJ*)(*B_oth)->data;
5833:     b_otha   = b_oth->a;
5834: #if defined(PETSC_HAVE_DEVICE)
5835:     (*B_oth)->offloadmask = PETSC_OFFLOAD_CPU;
5836: #endif
5837:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not possess an object container");

5839:   /* a-array */
5840:   /*---------*/
5841:   /*  post receives of a-array */
5842:   for (i=0; i<nrecvs; i++) {
5843:     nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
5844:     MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);
5845:   }

5847:   /* pack the outgoing message a-array */
5848:   if (nsends) k = sstarts[0];
5849:   for (i=0; i<nsends; i++) {
5850:     nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5851:     bufA  = bufa+sstartsj[i];
5852:     for (j=0; j<nrows; j++) {
5853:       row = srow[k++] + B->rmap->range[rank];  /* global row idx */
5854:       for (ll=0; ll<sbs; ll++) {
5855:         MatGetRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);
5856:         for (l=0; l<ncols; l++) {
5857:           *bufA++ = vals[l];
5858:         }
5859:         MatRestoreRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);
5860:       }
5861:     }
5862:     MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);
5863:   }
5864:   /* recvs and sends of a-array are completed */
5865:   if (nreqs) {MPI_Waitall(nreqs,reqs,MPI_STATUSES_IGNORE);}
5866:   PetscFree(reqs);

5868:   if (scall == MAT_INITIAL_MATRIX) {
5869:     /* put together the new matrix */
5870:     MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);

5872:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5873:     /* Since these are PETSc arrays, change flags to free them as necessary. */
5874:     b_oth          = (Mat_SeqAIJ*)(*B_oth)->data;
5875:     b_oth->free_a  = PETSC_TRUE;
5876:     b_oth->free_ij = PETSC_TRUE;
5877:     b_oth->nonew   = 0;

5879:     PetscFree(bufj);
5880:     if (!startsj_s || !bufa_ptr) {
5881:       PetscFree2(sstartsj,rstartsj);
5882:       PetscFree(bufa_ptr);
5883:     } else {
5884:       *startsj_s = sstartsj;
5885:       *startsj_r = rstartsj;
5886:       *bufa_ptr  = bufa;
5887:     }
5888:   }

5890:   VecScatterRestoreRemote_Private(ctx,PETSC_TRUE,&nsends,&sstarts,&srow,&sprocs,&sbs);
5891:   VecScatterRestoreRemoteOrdered_Private(ctx,PETSC_FALSE,&nrecvs,&rstarts,NULL,&rprocs,&rbs);
5892:   PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);
5893:   return(0);
5894: }

5896: /*@C
5897:   MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication.

5899:   Not Collective

5901:   Input Parameter:
5902: . A - The matrix in mpiaij format

5904:   Output Parameters:
5905: + lvec - The local vector holding off-process values from the argument to a matrix-vector product
5906: . colmap - A map from global column index to local index into lvec
5907: - multScatter - A scatter from the argument of a matrix-vector product to lvec

5909:   Level: developer

5911: @*/
5912: #if defined(PETSC_USE_CTABLE)
5913: PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
5914: #else
5915: PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
5916: #endif
5917: {
5918:   Mat_MPIAIJ *a;

5925:   a = (Mat_MPIAIJ*) A->data;
5926:   if (lvec) *lvec = a->lvec;
5927:   if (colmap) *colmap = a->colmap;
5928:   if (multScatter) *multScatter = a->Mvctx;
5929:   return(0);
5930: }

5932: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJCRL(Mat,MatType,MatReuse,Mat*);
5933: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJPERM(Mat,MatType,MatReuse,Mat*);
5934: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJSELL(Mat,MatType,MatReuse,Mat*);
5935: #if defined(PETSC_HAVE_MKL_SPARSE)
5936: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJMKL(Mat,MatType,MatReuse,Mat*);
5937: #endif
5938: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIBAIJ(Mat,MatType,MatReuse,Mat*);
5939: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPISBAIJ(Mat,MatType,MatReuse,Mat*);
5940: #if defined(PETSC_HAVE_ELEMENTAL)
5941: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
5942: #endif
5943: #if defined(PETSC_HAVE_SCALAPACK)
5944: PETSC_INTERN PetscErrorCode MatConvert_AIJ_ScaLAPACK(Mat,MatType,MatReuse,Mat*);
5945: #endif
5946: #if defined(PETSC_HAVE_HYPRE)
5947: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat,MatType,MatReuse,Mat*);
5948: #endif
5949: #if defined(PETSC_HAVE_CUDA)
5950: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJCUSPARSE(Mat,MatType,MatReuse,Mat*);
5951: #endif
5952: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
5953: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJKokkos(Mat,MatType,MatReuse,Mat*);
5954: #endif
5955: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPISELL(Mat,MatType,MatReuse,Mat*);
5956: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);
5957: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat);

5959: /*
5960:     Computes (B'*A')' since computing B*A directly is untenable

5962:                n                       p                          p
5963:         [             ]       [             ]         [                 ]
5964:       m [      A      ]  *  n [       B     ]   =   m [         C       ]
5965:         [             ]       [             ]         [                 ]

5967: */
5968: static PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C)
5969: {
5971:   Mat            At,Bt,Ct;

5974:   MatTranspose(A,MAT_INITIAL_MATRIX,&At);
5975:   MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);
5976:   MatMatMult(Bt,At,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&Ct);
5977:   MatDestroy(&At);
5978:   MatDestroy(&Bt);
5979:   MatTranspose(Ct,MAT_REUSE_MATRIX,&C);
5980:   MatDestroy(&Ct);
5981:   return(0);
5982: }

5984: static PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat C)
5985: {
5987:   PetscBool      cisdense;

5990:   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n);
5991:   MatSetSizes(C,A->rmap->n,B->cmap->n,A->rmap->N,B->cmap->N);
5992:   MatSetBlockSizesFromMats(C,A,B);
5993:   PetscObjectTypeCompareAny((PetscObject)C,&cisdense,MATMPIDENSE,MATMPIDENSECUDA,"");
5994:   if (!cisdense) {
5995:     MatSetType(C,((PetscObject)A)->type_name);
5996:   }
5997:   MatSetUp(C);

5999:   C->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIAIJ;
6000:   return(0);
6001: }

6003: /* ----------------------------------------------------------------*/
6004: static PetscErrorCode MatProductSetFromOptions_MPIDense_MPIAIJ_AB(Mat C)
6005: {
6006:   Mat_Product *product = C->product;
6007:   Mat         A = product->A,B=product->B;

6010:   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend)
6011:     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);

6013:   C->ops->matmultsymbolic = MatMatMultSymbolic_MPIDense_MPIAIJ;
6014:   C->ops->productsymbolic = MatProductSymbolic_AB;
6015:   return(0);
6016: }

6018: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_MPIDense_MPIAIJ(Mat C)
6019: {
6021:   Mat_Product    *product = C->product;

6024:   if (product->type == MATPRODUCT_AB) {
6025:     MatProductSetFromOptions_MPIDense_MPIAIJ_AB(C);
6026:   }
6027:   return(0);
6028: }
6029: /* ----------------------------------------------------------------*/

6031: /*MC
6032:    MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices.

6034:    Options Database Keys:
6035: . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions()

6037:    Level: beginner

6039:    Notes:
6040:     MatSetValues() may be called for this matrix type with a NULL argument for the numerical values,
6041:     in this case the values associated with the rows and columns one passes in are set to zero
6042:     in the matrix

6044:     MatSetOptions(,MAT_STRUCTURE_ONLY,PETSC_TRUE) may be called for this matrix type. In this no
6045:     space is allocated for the nonzero entries and any entries passed with MatSetValues() are ignored

6047: .seealso: MatCreateAIJ()
6048: M*/

6050: PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJ(Mat B)
6051: {
6052:   Mat_MPIAIJ     *b;
6054:   PetscMPIInt    size;

6057:   MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);

6059:   PetscNewLog(B,&b);
6060:   B->data       = (void*)b;
6061:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
6062:   B->assembled  = PETSC_FALSE;
6063:   B->insertmode = NOT_SET_VALUES;
6064:   b->size       = size;

6066:   MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);

6068:   /* build cache for off array entries formed */
6069:   MatStashCreate_Private(PetscObjectComm((PetscObject)B),1,&B->stash);

6071:   b->donotstash  = PETSC_FALSE;
6072:   b->colmap      = NULL;
6073:   b->garray      = NULL;
6074:   b->roworiented = PETSC_TRUE;

6076:   /* stuff used for matrix vector multiply */
6077:   b->lvec  = NULL;
6078:   b->Mvctx = NULL;

6080:   /* stuff for MatGetRow() */
6081:   b->rowindices   = NULL;
6082:   b->rowvalues    = NULL;
6083:   b->getrowactive = PETSC_FALSE;

6085:   /* flexible pointer used in CUSPARSE classes */
6086:   b->spptr = NULL;

6088:   PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetUseScalableIncreaseOverlap_C",MatMPIAIJSetUseScalableIncreaseOverlap_MPIAIJ);
6089:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIAIJ);
6090:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIAIJ);
6091:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_MPIAIJ);
6092:   PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",MatMPIAIJSetPreallocation_MPIAIJ);
6093:   PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_MPIAIJ);
6094:   PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",MatMPIAIJSetPreallocationCSR_MPIAIJ);
6095:   PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIAIJ);
6096:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C",MatConvert_MPIAIJ_MPIAIJPERM);
6097:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijsell_C",MatConvert_MPIAIJ_MPIAIJSELL);
6098: #if defined(PETSC_HAVE_CUDA)
6099:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijcusparse_C",MatConvert_MPIAIJ_MPIAIJCUSPARSE);
6100: #endif
6101: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
6102:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijkokkos_C",MatConvert_MPIAIJ_MPIAIJKokkos);
6103: #endif
6104: #if defined(PETSC_HAVE_MKL_SPARSE)
6105:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijmkl_C",MatConvert_MPIAIJ_MPIAIJMKL);
6106: #endif
6107:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C",MatConvert_MPIAIJ_MPIAIJCRL);
6108:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpibaij_C",MatConvert_MPIAIJ_MPIBAIJ);
6109:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C",MatConvert_MPIAIJ_MPISBAIJ);
6110:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpidense_C",MatConvert_MPIAIJ_MPIDense);
6111: #if defined(PETSC_HAVE_ELEMENTAL)
6112:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_elemental_C",MatConvert_MPIAIJ_Elemental);
6113: #endif
6114: #if defined(PETSC_HAVE_SCALAPACK)
6115:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_scalapack_C",MatConvert_AIJ_ScaLAPACK);
6116: #endif
6117:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_is_C",MatConvert_XAIJ_IS);
6118:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpisell_C",MatConvert_MPIAIJ_MPISELL);
6119: #if defined(PETSC_HAVE_HYPRE)
6120:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_hypre_C",MatConvert_AIJ_HYPRE);
6121:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_transpose_mpiaij_mpiaij_C",MatProductSetFromOptions_Transpose_AIJ_AIJ);
6122: #endif
6123:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_is_mpiaij_C",MatProductSetFromOptions_IS_XAIJ);
6124:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_mpiaij_mpiaij_C",MatProductSetFromOptions_MPIAIJ);
6125:   PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);
6126:   return(0);
6127: }

6129: /*@C
6130:      MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal"
6131:          and "off-diagonal" part of the matrix in CSR format.

6133:    Collective

6135:    Input Parameters:
6136: +  comm - MPI communicator
6137: .  m - number of local rows (Cannot be PETSC_DECIDE)
6138: .  n - This value should be the same as the local size used in creating the
6139:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
6140:        calculated if N is given) For square matrices n is almost always m.
6141: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
6142: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
6143: .   i - row indices for "diagonal" portion of matrix; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
6144: .   j - column indices, which must be local, i.e., based off the start column of the diagonal portion
6145: .   a - matrix values
6146: .   oi - row indices for "off-diagonal" portion of matrix; that is oi[0] = 0, oi[row] = oi[row-1] + number of elements in that row of the matrix
6147: .   oj - column indices, which must be global, representing global columns in the MPIAIJ matrix
6148: -   oa - matrix values

6150:    Output Parameter:
6151: .   mat - the matrix

6153:    Level: advanced

6155:    Notes:
6156:        The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. The user
6157:        must free the arrays once the matrix has been destroyed and not before.

6159:        The i and j indices are 0 based

6161:        See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix

6163:        This sets local rows and cannot be used to set off-processor values.

6165:        Use of this routine is discouraged because it is inflexible and cumbersome to use. It is extremely rare that a
6166:        legacy application natively assembles into exactly this split format. The code to do so is nontrivial and does
6167:        not easily support in-place reassembly. It is recommended to use MatSetValues() (or a variant thereof) because
6168:        the resulting assembly is easier to implement, will work with any matrix format, and the user does not have to
6169:        keep track of the underlying array. Use MatSetOption(A,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) to disable all
6170:        communication if it is known that only local entries will be set.

6172: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
6173:           MATMPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays()
6174: @*/
6175: PetscErrorCode MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[],PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat)
6176: {
6178:   Mat_MPIAIJ     *maij;

6181:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
6182:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
6183:   if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0");
6184:   MatCreate(comm,mat);
6185:   MatSetSizes(*mat,m,n,M,N);
6186:   MatSetType(*mat,MATMPIAIJ);
6187:   maij = (Mat_MPIAIJ*) (*mat)->data;

6189:   (*mat)->preallocated = PETSC_TRUE;

6191:   PetscLayoutSetUp((*mat)->rmap);
6192:   PetscLayoutSetUp((*mat)->cmap);

6194:   MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);
6195:   MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);

6197:   MatSetOption(*mat,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);
6198:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
6199:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
6200:   MatSetOption(*mat,MAT_NO_OFF_PROC_ENTRIES,PETSC_FALSE);
6201:   MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
6202:   return(0);
6203: }

6205: /*
6206:     Special version for direct calls from Fortran
6207: */
6208: #include <petsc/private/fortranimpl.h>

6210: /* Change these macros so can be used in void function */
6211: #undef CHKERRQ
6212: #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr)
6213: #undef SETERRQ2
6214: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
6215: #undef SETERRQ3
6216: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
6217: #undef SETERRQ
6218: #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr)

6220: #if defined(PETSC_HAVE_FORTRAN_CAPS)
6221: #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
6222: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
6223: #define matsetvaluesmpiaij_ matsetvaluesmpiaij
6224: #else
6225: #endif
6226: PETSC_EXTERN void matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr)
6227: {
6228:   Mat            mat  = *mmat;
6229:   PetscInt       m    = *mm, n = *mn;
6230:   InsertMode     addv = *maddv;
6231:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
6232:   PetscScalar    value;

6235:   MatCheckPreallocated(mat,1);
6236:   if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;
6237:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
6238:   {
6239:     PetscInt  i,j,rstart  = mat->rmap->rstart,rend = mat->rmap->rend;
6240:     PetscInt  cstart      = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
6241:     PetscBool roworiented = aij->roworiented;

6243:     /* Some Variables required in the macro */
6244:     Mat        A                    = aij->A;
6245:     Mat_SeqAIJ *a                   = (Mat_SeqAIJ*)A->data;
6246:     PetscInt   *aimax               = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
6247:     MatScalar  *aa                  = a->a;
6248:     PetscBool  ignorezeroentries    = (((a->ignorezeroentries)&&(addv==ADD_VALUES)) ? PETSC_TRUE : PETSC_FALSE);
6249:     Mat        B                    = aij->B;
6250:     Mat_SeqAIJ *b                   = (Mat_SeqAIJ*)B->data;
6251:     PetscInt   *bimax               = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
6252:     MatScalar  *ba                  = b->a;
6253:     /* This variable below is only for the PETSC_HAVE_VIENNACL or PETSC_HAVE_CUDA cases, but we define it in all cases because we
6254:      * cannot use "#if defined" inside a macro. */
6255:     PETSC_UNUSED PetscBool inserted = PETSC_FALSE;

6257:     PetscInt  *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
6258:     PetscInt  nonew = a->nonew;
6259:     MatScalar *ap1,*ap2;

6262:     for (i=0; i<m; i++) {
6263:       if (im[i] < 0) continue;
6264:       if (PetscUnlikelyDebug(im[i] >= mat->rmap->N)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
6265:       if (im[i] >= rstart && im[i] < rend) {
6266:         row      = im[i] - rstart;
6267:         lastcol1 = -1;
6268:         rp1      = aj + ai[row];
6269:         ap1      = aa + ai[row];
6270:         rmax1    = aimax[row];
6271:         nrow1    = ailen[row];
6272:         low1     = 0;
6273:         high1    = nrow1;
6274:         lastcol2 = -1;
6275:         rp2      = bj + bi[row];
6276:         ap2      = ba + bi[row];
6277:         rmax2    = bimax[row];
6278:         nrow2    = bilen[row];
6279:         low2     = 0;
6280:         high2    = nrow2;

6282:         for (j=0; j<n; j++) {
6283:           if (roworiented) value = v[i*n+j];
6284:           else value = v[i+j*m];
6285:           if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES) && im[i] != in[j]) continue;
6286:           if (in[j] >= cstart && in[j] < cend) {
6287:             col = in[j] - cstart;
6288:             MatSetValues_SeqAIJ_A_Private(row,col,value,addv,im[i],in[j]);
6289: #if defined(PETSC_HAVE_DEVICE)
6290:             if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) A->offloadmask = PETSC_OFFLOAD_CPU;
6291: #endif
6292:           } else if (in[j] < 0) continue;
6293:           else if (PetscUnlikelyDebug(in[j] >= mat->cmap->N)) {
6294:             /* extra brace on SETERRQ2() is required for --with-errorchecking=0 - due to the next 'else' clause */
6295:             SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap->N-1);
6296:           } else {
6297:             if (mat->was_assembled) {
6298:               if (!aij->colmap) {
6299:                 MatCreateColmap_MPIAIJ_Private(mat);
6300:               }
6301: #if defined(PETSC_USE_CTABLE)
6302:               PetscTableFind(aij->colmap,in[j]+1,&col);
6303:               col--;
6304: #else
6305:               col = aij->colmap[in[j]] - 1;
6306: #endif
6307:               if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
6308:                 MatDisAssemble_MPIAIJ(mat);
6309:                 col  =  in[j];
6310:                 /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
6311:                 B        = aij->B;
6312:                 b        = (Mat_SeqAIJ*)B->data;
6313:                 bimax    = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
6314:                 rp2      = bj + bi[row];
6315:                 ap2      = ba + bi[row];
6316:                 rmax2    = bimax[row];
6317:                 nrow2    = bilen[row];
6318:                 low2     = 0;
6319:                 high2    = nrow2;
6320:                 bm       = aij->B->rmap->n;
6321:                 ba       = b->a;
6322:                 inserted = PETSC_FALSE;
6323:               }
6324:             } else col = in[j];
6325:             MatSetValues_SeqAIJ_B_Private(row,col,value,addv,im[i],in[j]);
6326: #if defined(PETSC_HAVE_DEVICE)
6327:             if (B->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) B->offloadmask = PETSC_OFFLOAD_CPU;
6328: #endif
6329:           }
6330:         }
6331:       } else if (!aij->donotstash) {
6332:         if (roworiented) {
6333:           MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
6334:         } else {
6335:           MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
6336:         }
6337:       }
6338:     }
6339:   }
6340:   PetscFunctionReturnVoid();
6341: }

6343: typedef struct {
6344:   Mat       *mp;    /* intermediate products */
6345:   PetscBool *mptmp; /* is the intermediate product temporary ? */
6346:   PetscInt  cp;     /* number of intermediate products */

6348:   /* support for MatGetBrowsOfAoCols_MPIAIJ for P_oth */
6349:   PetscInt    *startsj_s,*startsj_r;
6350:   PetscScalar *bufa;
6351:   Mat         P_oth;

6353:   /* may take advantage of merging product->B */
6354:   Mat Bloc; /* B-local by merging diag and off-diag */

6356:   /* cusparse does not have support to split between symbolic and numeric phases.
6357:      When api_user is true, we don't need to update the numerical values
6358:      of the temporary storage */
6359:   PetscBool reusesym;

6361:   /* support for COO values insertion */
6362:   PetscScalar  *coo_v,*coo_w; /* store on-process and off-process COO scalars, and used as MPI recv/send buffers respectively */
6363:   PetscInt     **own; /* own[i] points to address of on-process COO indices for Mat mp[i] */
6364:   PetscInt     **off; /* off[i] points to address of off-process COO indices for Mat mp[i] */
6365:   PetscBool    hasoffproc; /* if true, have off-process values insertion (i.e. AtB or PtAP) */
6366:   PetscSF      sf; /* used for non-local values insertion and memory malloc */
6367:   PetscMemType mtype;

6369:   /* customization */
6370:   PetscBool abmerge;
6371:   PetscBool P_oth_bind;
6372: } MatMatMPIAIJBACKEND;

6374: PetscErrorCode MatDestroy_MatMatMPIAIJBACKEND(void *data)
6375: {
6376:   MatMatMPIAIJBACKEND *mmdata = (MatMatMPIAIJBACKEND*)data;
6377:   PetscInt            i;
6378:   PetscErrorCode      ierr;

6381:   PetscFree2(mmdata->startsj_s,mmdata->startsj_r);
6382:   PetscFree(mmdata->bufa);
6383:   PetscSFFree(mmdata->sf,mmdata->mtype,mmdata->coo_v);
6384:   PetscSFFree(mmdata->sf,mmdata->mtype,mmdata->coo_w);
6385:   MatDestroy(&mmdata->P_oth);
6386:   MatDestroy(&mmdata->Bloc);
6387:   PetscSFDestroy(&mmdata->sf);
6388:   for (i = 0; i < mmdata->cp; i++) {
6389:     MatDestroy(&mmdata->mp[i]);
6390:   }
6391:   PetscFree2(mmdata->mp,mmdata->mptmp);
6392:   PetscFree(mmdata->own[0]);
6393:   PetscFree(mmdata->own);
6394:   PetscFree(mmdata->off[0]);
6395:   PetscFree(mmdata->off);
6396:   PetscFree(mmdata);
6397:   return(0);
6398: }

6400: static PetscErrorCode MatSeqAIJCopySubArray(Mat A, PetscInt n, const PetscInt idx[], PetscScalar v[])
6401: {
6402:   PetscErrorCode (*f)(Mat,PetscInt,const PetscInt[],PetscScalar[]);

6406:   PetscObjectQueryFunction((PetscObject)A,"MatSeqAIJCopySubArray_C",&f);
6407:   if (f) {
6408:     (*f)(A,n,idx,v);
6409:   } else {
6410:     const PetscScalar *vv;

6412:     MatSeqAIJGetArrayRead(A,&vv);
6413:     if (n && idx) {
6414:       PetscScalar    *w = v;
6415:       const PetscInt *oi = idx;
6416:       PetscInt       j;

6418:       for (j = 0; j < n; j++) *w++ = vv[*oi++];
6419:     } else {
6420:       PetscArraycpy(v,vv,n);
6421:     }
6422:     MatSeqAIJRestoreArrayRead(A,&vv);
6423:   }
6424:   return(0);
6425: }

6427: static PetscErrorCode MatProductNumeric_MPIAIJBACKEND(Mat C)
6428: {
6429:   MatMatMPIAIJBACKEND *mmdata;
6430:   PetscInt            i,n_d,n_o;
6431:   PetscErrorCode      ierr;

6434:   MatCheckProduct(C,1);
6435:   if (!C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data empty");
6436:   mmdata = (MatMatMPIAIJBACKEND*)C->product->data;
6437:   if (!mmdata->reusesym) { /* update temporary matrices */
6438:     if (mmdata->P_oth) {
6439:       MatGetBrowsOfAoCols_MPIAIJ(C->product->A,C->product->B,MAT_REUSE_MATRIX,&mmdata->startsj_s,&mmdata->startsj_r,&mmdata->bufa,&mmdata->P_oth);
6440:     }
6441:     if (mmdata->Bloc) {
6442:       MatMPIAIJGetLocalMatMerge(C->product->B,MAT_REUSE_MATRIX,NULL,&mmdata->Bloc);
6443:     }
6444:   }
6445:   mmdata->reusesym = PETSC_FALSE;

6447:   for (i = 0; i < mmdata->cp; i++) {
6448:     if (!mmdata->mp[i]->ops->productnumeric) SETERRQ1(PetscObjectComm((PetscObject)mmdata->mp[i]),PETSC_ERR_PLIB,"Missing numeric op for %s",MatProductTypes[mmdata->mp[i]->product->type]);
6449:     (*mmdata->mp[i]->ops->productnumeric)(mmdata->mp[i]);
6450:   }
6451:   for (i = 0, n_d = 0, n_o = 0; i < mmdata->cp; i++) {
6452:     PetscInt noff = mmdata->off[i+1] - mmdata->off[i];

6454:     if (mmdata->mptmp[i]) continue;
6455:     if (noff) {
6456:       PetscInt nown = mmdata->own[i+1] - mmdata->own[i];

6458:       MatSeqAIJCopySubArray(mmdata->mp[i],noff,mmdata->off[i],mmdata->coo_w + n_o);
6459:       MatSeqAIJCopySubArray(mmdata->mp[i],nown,mmdata->own[i],mmdata->coo_v + n_d);
6460:       n_o += noff;
6461:       n_d += nown;
6462:     } else {
6463:       Mat_SeqAIJ *mm = (Mat_SeqAIJ*)mmdata->mp[i]->data;

6465:       MatSeqAIJCopySubArray(mmdata->mp[i],mm->nz,NULL,mmdata->coo_v + n_d);
6466:       n_d += mm->nz;
6467:     }
6468:   }
6469:   if (mmdata->hasoffproc) { /* offprocess insertion */
6470:     PetscSFGatherBegin(mmdata->sf,MPIU_SCALAR,mmdata->coo_w,mmdata->coo_v+n_d);
6471:     PetscSFGatherEnd(mmdata->sf,MPIU_SCALAR,mmdata->coo_w,mmdata->coo_v+n_d);
6472:   }
6473:   MatSetValuesCOO(C,mmdata->coo_v,INSERT_VALUES);
6474:   return(0);
6475: }

6477: /* Support for Pt * A, A * P, or Pt * A * P */
6478: #define MAX_NUMBER_INTERMEDIATE 4
6479: PetscErrorCode MatProductSymbolic_MPIAIJBACKEND(Mat C)
6480: {
6481:   Mat_Product            *product = C->product;
6482:   Mat                    A,P,mp[MAX_NUMBER_INTERMEDIATE]; /* A, P and a series of intermediate matrices */
6483:   Mat_MPIAIJ             *a,*p;
6484:   MatMatMPIAIJBACKEND    *mmdata;
6485:   ISLocalToGlobalMapping P_oth_l2g = NULL;
6486:   IS                     glob = NULL;
6487:   const char             *prefix;
6488:   char                   pprefix[256];
6489:   const PetscInt         *globidx,*P_oth_idx;
6490:   PetscInt               i,j,cp,m,n,M,N,ncoo,ncoo_d,ncoo_o,ncoo_oown,*coo_i,*coo_j;
6491:   PetscInt               cmapt[MAX_NUMBER_INTERMEDIATE],rmapt[MAX_NUMBER_INTERMEDIATE]; /* col/row map type for each Mat in mp[]. */
6492:                                                                                         /* type-0: consecutive, start from 0; type-1: consecutive with */
6493:                                                                                         /* a base offset; type-2: sparse with a local to global map table */
6494:   const PetscInt         *cmapa[MAX_NUMBER_INTERMEDIATE],*rmapa[MAX_NUMBER_INTERMEDIATE]; /* col/row local to global map array (table) for type-2 map type */

6496:   MatProductType         ptype;
6497:   PetscBool              mptmp[MAX_NUMBER_INTERMEDIATE],hasoffproc = PETSC_FALSE,iscuda,iskokk;
6498:   PetscMPIInt            size;
6499:   PetscErrorCode         ierr;

6502:   MatCheckProduct(C,1);
6503:   if (product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data not empty");
6504:   ptype = product->type;
6505:   if (product->A->symmetric && ptype == MATPRODUCT_AtB) ptype = MATPRODUCT_AB;
6506:   switch (ptype) {
6507:   case MATPRODUCT_AB:
6508:     A = product->A;
6509:     P = product->B;
6510:     m = A->rmap->n;
6511:     n = P->cmap->n;
6512:     M = A->rmap->N;
6513:     N = P->cmap->N;
6514:     hasoffproc = PETSC_FALSE; /* will not scatter mat product values to other processes */
6515:     break;
6516:   case MATPRODUCT_AtB:
6517:     P = product->A;
6518:     A = product->B;
6519:     m = P->cmap->n;
6520:     n = A->cmap->n;
6521:     M = P->cmap->N;
6522:     N = A->cmap->N;
6523:     hasoffproc = PETSC_TRUE;
6524:     break;
6525:   case MATPRODUCT_PtAP:
6526:     A = product->A;
6527:     P = product->B;
6528:     m = P->cmap->n;
6529:     n = P->cmap->n;
6530:     M = P->cmap->N;
6531:     N = P->cmap->N;
6532:     hasoffproc = PETSC_TRUE;
6533:     break;
6534:   default:
6535:     SETERRQ1(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Not for product type %s",MatProductTypes[ptype]);
6536:   }
6537:   MPI_Comm_size(PetscObjectComm((PetscObject)C),&size);
6538:   if (size == 1) hasoffproc = PETSC_FALSE;

6540:   /* defaults */
6541:   for (i=0;i<MAX_NUMBER_INTERMEDIATE;i++) {
6542:     mp[i]    = NULL;
6543:     mptmp[i] = PETSC_FALSE;
6544:     rmapt[i] = -1;
6545:     cmapt[i] = -1;
6546:     rmapa[i] = NULL;
6547:     cmapa[i] = NULL;
6548:   }

6550:   /* customization */
6551:   PetscNew(&mmdata);
6552:   mmdata->reusesym = product->api_user;
6553:   if (ptype == MATPRODUCT_AB) {
6554:     if (product->api_user) {
6555:       PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatMatMult","Mat");
6556:       PetscOptionsBool("-matmatmult_backend_mergeB","Merge product->B local matrices","MatMatMult",mmdata->abmerge,&mmdata->abmerge,NULL);
6557:       PetscOptionsBool("-matmatmult_backend_pothbind","Bind P_oth to CPU","MatBindToCPU",mmdata->P_oth_bind,&mmdata->P_oth_bind,NULL);
6558:       PetscOptionsEnd();
6559:     } else {
6560:       PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatProduct_AB","Mat");
6561:       PetscOptionsBool("-matproduct_ab_backend_mergeB","Merge product->B local matrices","MatMatMult",mmdata->abmerge,&mmdata->abmerge,NULL);
6562:       PetscOptionsBool("-matproduct_ab_backend_pothbind","Bind P_oth to CPU","MatBindToCPU",mmdata->P_oth_bind,&mmdata->P_oth_bind,NULL);
6563:       PetscOptionsEnd();
6564:     }
6565:   } else if (ptype == MATPRODUCT_PtAP) {
6566:     if (product->api_user) {
6567:       PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatPtAP","Mat");
6568:       PetscOptionsBool("-matptap_backend_pothbind","Bind P_oth to CPU","MatBindToCPU",mmdata->P_oth_bind,&mmdata->P_oth_bind,NULL);
6569:       PetscOptionsEnd();
6570:     } else {
6571:       PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatProduct_PtAP","Mat");
6572:       PetscOptionsBool("-matproduct_ptap_backend_pothbind","Bind P_oth to CPU","MatBindToCPU",mmdata->P_oth_bind,&mmdata->P_oth_bind,NULL);
6573:       PetscOptionsEnd();
6574:     }
6575:   }
6576:   a = (Mat_MPIAIJ*)A->data;
6577:   p = (Mat_MPIAIJ*)P->data;
6578:   MatSetSizes(C,m,n,M,N);
6579:   PetscLayoutSetUp(C->rmap);
6580:   PetscLayoutSetUp(C->cmap);
6581:   MatSetType(C,((PetscObject)A)->type_name);
6582:   MatGetOptionsPrefix(C,&prefix);

6584:   cp   = 0;
6585:   switch (ptype) {
6586:   case MATPRODUCT_AB: /* A * P */
6587:     MatGetBrowsOfAoCols_MPIAIJ(A,P,MAT_INITIAL_MATRIX,&mmdata->startsj_s,&mmdata->startsj_r,&mmdata->bufa,&mmdata->P_oth);

6589:     /* A_diag * P_local (merged or not) */
6590:     if (mmdata->abmerge) { /* P's diagonal and off-diag blocks are merged to one matrix, then multiplied by A_diag */
6591:       /* P is product->B */
6592:       MatMPIAIJGetLocalMatMerge(P,MAT_INITIAL_MATRIX,&glob,&mmdata->Bloc);
6593:       MatProductCreate(a->A,mmdata->Bloc,NULL,&mp[cp]);
6594:       MatProductSetType(mp[cp],MATPRODUCT_AB);
6595:       MatProductSetFill(mp[cp],product->fill);
6596:       PetscSNPrintf(pprefix,sizeof(pprefix),"backend_p%D_",cp);
6597:       MatSetOptionsPrefix(mp[cp],prefix);
6598:       MatAppendOptionsPrefix(mp[cp],pprefix);
6599:       mp[cp]->product->api_user = product->api_user;
6600:       MatProductSetFromOptions(mp[cp]);
6601:       if (!mp[cp]->ops->productsymbolic) SETERRQ1(PetscObjectComm((PetscObject)mp[cp]),PETSC_ERR_PLIB,"Missing symbolic op for %s",MatProductTypes[mp[cp]->product->type]);
6602:       (*mp[cp]->ops->productsymbolic)(mp[cp]);
6603:       ISGetIndices(glob,&globidx);
6604:       rmapt[cp] = 1;
6605:       cmapt[cp] = 2;
6606:       cmapa[cp] = globidx;
6607:       mptmp[cp] = PETSC_FALSE;
6608:       cp++;
6609:     } else { /* A_diag * P_diag and A_diag * P_off */
6610:       MatProductCreate(a->A,p->A,NULL,&mp[cp]);
6611:       MatProductSetType(mp[cp],MATPRODUCT_AB);
6612:       MatProductSetFill(mp[cp],product->fill);
6613:       PetscSNPrintf(pprefix,sizeof(pprefix),"backend_p%D_",cp);
6614:       MatSetOptionsPrefix(mp[cp],prefix);
6615:       MatAppendOptionsPrefix(mp[cp],pprefix);
6616:       mp[cp]->product->api_user = product->api_user;
6617:       MatProductSetFromOptions(mp[cp]);
6618:       if (!mp[cp]->ops->productsymbolic) SETERRQ1(PetscObjectComm((PetscObject)mp[cp]),PETSC_ERR_PLIB,"Missing symbolic op for %s",MatProductTypes[mp[cp]->product->type]);
6619:       (*mp[cp]->ops->productsymbolic)(mp[cp]);
6620:       rmapt[cp] = 1;
6621:       cmapt[cp] = 1;
6622:       mptmp[cp] = PETSC_FALSE;
6623:       cp++;
6624:       MatProductCreate(a->A,p->B,NULL,&mp[cp]);
6625:       MatProductSetType(mp[cp],MATPRODUCT_AB);
6626:       MatProductSetFill(mp[cp],product->fill);
6627:       PetscSNPrintf(pprefix,sizeof(pprefix),"backend_p%D_",cp);
6628:       MatSetOptionsPrefix(mp[cp],prefix);
6629:       MatAppendOptionsPrefix(mp[cp],pprefix);
6630:       mp[cp]->product->api_user = product->api_user;
6631:       MatProductSetFromOptions(mp[cp]);
6632:       if (!mp[cp]->ops->productsymbolic) SETERRQ1(PetscObjectComm((PetscObject)mp[cp]),PETSC_ERR_PLIB,"Missing symbolic op for %s",MatProductTypes[mp[cp]->product->type]);
6633:       (*mp[cp]->ops->productsymbolic)(mp[cp]);
6634:       rmapt[cp] = 1;
6635:       cmapt[cp] = 2;
6636:       cmapa[cp] = p->garray;
6637:       mptmp[cp] = PETSC_FALSE;
6638:       cp++;
6639:     }

6641:     /* A_off * P_other */
6642:     if (mmdata->P_oth) {
6643:       MatSeqAIJCompactOutExtraColumns_SeqAIJ(mmdata->P_oth,&P_oth_l2g); /* make P_oth use local col ids */
6644:       ISLocalToGlobalMappingGetIndices(P_oth_l2g,&P_oth_idx);
6645:       MatSetType(mmdata->P_oth,((PetscObject)(a->B))->type_name);
6646:       MatBindToCPU(mmdata->P_oth,mmdata->P_oth_bind);
6647:       MatProductCreate(a->B,mmdata->P_oth,NULL,&mp[cp]);
6648:       MatProductSetType(mp[cp],MATPRODUCT_AB);
6649:       MatProductSetFill(mp[cp],product->fill);
6650:       PetscSNPrintf(pprefix,sizeof(pprefix),"backend_p%D_",cp);
6651:       MatSetOptionsPrefix(mp[cp],prefix);
6652:       MatAppendOptionsPrefix(mp[cp],pprefix);
6653:       mp[cp]->product->api_user = product->api_user;
6654:       MatProductSetFromOptions(mp[cp]);
6655:       if (!mp[cp]->ops->productsymbolic) SETERRQ1(PetscObjectComm((PetscObject)mp[cp]),PETSC_ERR_PLIB,"Missing symbolic op for %s",MatProductTypes[mp[cp]->product->type]);
6656:       (*mp[cp]->ops->productsymbolic)(mp[cp]);
6657:       rmapt[cp] = 1;
6658:       cmapt[cp] = 2;
6659:       cmapa[cp] = P_oth_idx;
6660:       mptmp[cp] = PETSC_FALSE;
6661:       cp++;
6662:     }
6663:     break;

6665:   case MATPRODUCT_AtB: /* (P^t * A): P_diag * A_loc + P_off * A_loc */
6666:     /* A is product->B */
6667:     MatMPIAIJGetLocalMatMerge(A,MAT_INITIAL_MATRIX,&glob,&mmdata->Bloc);
6668:     if (A == P) { /* when A==P, we can take advantage of the already merged mmdata->Bloc */
6669:       MatProductCreate(mmdata->Bloc,mmdata->Bloc,NULL,&mp[cp]);
6670:       MatProductSetType(mp[cp],MATPRODUCT_AtB);
6671:       MatProductSetFill(mp[cp],product->fill);
6672:       PetscSNPrintf(pprefix,sizeof(pprefix),"backend_p%D_",cp);
6673:       MatSetOptionsPrefix(mp[cp],prefix);
6674:       MatAppendOptionsPrefix(mp[cp],pprefix);
6675:       mp[cp]->product->api_user = product->api_user;
6676:       MatProductSetFromOptions(mp[cp]);
6677:       if (!mp[cp]->ops->productsymbolic) SETERRQ1(PetscObjectComm((PetscObject)mp[cp]),PETSC_ERR_PLIB,"Missing symbolic op for %s",MatProductTypes[mp[cp]->product->type]);
6678:       (*mp[cp]->ops->productsymbolic)(mp[cp]);
6679:       ISGetIndices(glob,&globidx);
6680:       rmapt[cp] = 2;
6681:       rmapa[cp] = globidx;
6682:       cmapt[cp] = 2;
6683:       cmapa[cp] = globidx;
6684:       mptmp[cp] = PETSC_FALSE;
6685:       cp++;
6686:     } else {
6687:       MatProductCreate(p->A,mmdata->Bloc,NULL,&mp[cp]);
6688:       MatProductSetType(mp[cp],MATPRODUCT_AtB);
6689:       MatProductSetFill(mp[cp],product->fill);
6690:       PetscSNPrintf(pprefix,sizeof(pprefix),"backend_p%D_",cp);
6691:       MatSetOptionsPrefix(mp[cp],prefix);
6692:       MatAppendOptionsPrefix(mp[cp],pprefix);
6693:       mp[cp]->product->api_user = product->api_user;
6694:       MatProductSetFromOptions(mp[cp]);
6695:       if (!mp[cp]->ops->productsymbolic) SETERRQ1(PetscObjectComm((PetscObject)mp[cp]),PETSC_ERR_PLIB,"Missing symbolic op for %s",MatProductTypes[mp[cp]->product->type]);
6696:       (*mp[cp]->ops->productsymbolic)(mp[cp]);
6697:       ISGetIndices(glob,&globidx);
6698:       rmapt[cp] = 1;
6699:       cmapt[cp] = 2;
6700:       cmapa[cp] = globidx;
6701:       mptmp[cp] = PETSC_FALSE;
6702:       cp++;
6703:       MatProductCreate(p->B,mmdata->Bloc,NULL,&mp[cp]);
6704:       MatProductSetType(mp[cp],MATPRODUCT_AtB);
6705:       MatProductSetFill(mp[cp],product->fill);
6706:       PetscSNPrintf(pprefix,sizeof(pprefix),"backend_p%D_",cp);
6707:       MatSetOptionsPrefix(mp[cp],prefix);
6708:       MatAppendOptionsPrefix(mp[cp],pprefix);
6709:       mp[cp]->product->api_user = product->api_user;
6710:       MatProductSetFromOptions(mp[cp]);
6711:       if (!mp[cp]->ops->productsymbolic) SETERRQ1(PetscObjectComm((PetscObject)mp[cp]),PETSC_ERR_PLIB,"Missing symbolic op for %s",MatProductTypes[mp[cp]->product->type]);
6712:       (*mp[cp]->ops->productsymbolic)(mp[cp]);
6713:       rmapt[cp] = 2;
6714:       rmapa[cp] = p->garray;
6715:       cmapt[cp] = 2;
6716:       cmapa[cp] = globidx;
6717:       mptmp[cp] = PETSC_FALSE;
6718:       cp++;
6719:     }
6720:     break;
6721:   case MATPRODUCT_PtAP:
6722:     MatGetBrowsOfAoCols_MPIAIJ(A,P,MAT_INITIAL_MATRIX,&mmdata->startsj_s,&mmdata->startsj_r,&mmdata->bufa,&mmdata->P_oth);
6723:     /* P is product->B */
6724:     MatMPIAIJGetLocalMatMerge(P,MAT_INITIAL_MATRIX,&glob,&mmdata->Bloc);
6725:     MatProductCreate(a->A,mmdata->Bloc,NULL,&mp[cp]);
6726:     MatProductSetType(mp[cp],MATPRODUCT_PtAP);
6727:     MatProductSetFill(mp[cp],product->fill);
6728:     PetscSNPrintf(pprefix,sizeof(pprefix),"backend_p%D_",cp);
6729:     MatSetOptionsPrefix(mp[cp],prefix);
6730:     MatAppendOptionsPrefix(mp[cp],pprefix);
6731:     mp[cp]->product->api_user = product->api_user;
6732:     MatProductSetFromOptions(mp[cp]);
6733:     if (!mp[cp]->ops->productsymbolic) SETERRQ1(PetscObjectComm((PetscObject)mp[cp]),PETSC_ERR_PLIB,"Missing symbolic op for %s",MatProductTypes[mp[cp]->product->type]);
6734:     (*mp[cp]->ops->productsymbolic)(mp[cp]);
6735:     ISGetIndices(glob,&globidx);
6736:     rmapt[cp] = 2;
6737:     rmapa[cp] = globidx;
6738:     cmapt[cp] = 2;
6739:     cmapa[cp] = globidx;
6740:     mptmp[cp] = PETSC_FALSE;
6741:     cp++;
6742:     if (mmdata->P_oth) {
6743:       MatSeqAIJCompactOutExtraColumns_SeqAIJ(mmdata->P_oth,&P_oth_l2g);
6744:       ISLocalToGlobalMappingGetIndices(P_oth_l2g,&P_oth_idx);
6745:       MatSetType(mmdata->P_oth,((PetscObject)(a->B))->type_name);
6746:       MatBindToCPU(mmdata->P_oth,mmdata->P_oth_bind);
6747:       MatProductCreate(a->B,mmdata->P_oth,NULL,&mp[cp]);
6748:       MatProductSetType(mp[cp],MATPRODUCT_AB);
6749:       MatProductSetFill(mp[cp],product->fill);
6750:       PetscSNPrintf(pprefix,sizeof(pprefix),"backend_p%D_",cp);
6751:       MatSetOptionsPrefix(mp[cp],prefix);
6752:       MatAppendOptionsPrefix(mp[cp],pprefix);
6753:       mp[cp]->product->api_user = product->api_user;
6754:       MatProductSetFromOptions(mp[cp]);
6755:       if (!mp[cp]->ops->productsymbolic) SETERRQ1(PetscObjectComm((PetscObject)mp[cp]),PETSC_ERR_PLIB,"Missing symbolic op for %s",MatProductTypes[mp[cp]->product->type]);
6756:       (*mp[cp]->ops->productsymbolic)(mp[cp]);
6757:       mptmp[cp] = PETSC_TRUE;
6758:       cp++;
6759:       MatProductCreate(mmdata->Bloc,mp[1],NULL,&mp[cp]);
6760:       MatProductSetType(mp[cp],MATPRODUCT_AtB);
6761:       MatProductSetFill(mp[cp],product->fill);
6762:       PetscSNPrintf(pprefix,sizeof(pprefix),"backend_p%D_",cp);
6763:       MatSetOptionsPrefix(mp[cp],prefix);
6764:       MatAppendOptionsPrefix(mp[cp],pprefix);
6765:       mp[cp]->product->api_user = product->api_user;
6766:       MatProductSetFromOptions(mp[cp]);
6767:       if (!mp[cp]->ops->productsymbolic) SETERRQ1(PetscObjectComm((PetscObject)mp[cp]),PETSC_ERR_PLIB,"Missing symbolic op for %s",MatProductTypes[mp[cp]->product->type]);
6768:       (*mp[cp]->ops->productsymbolic)(mp[cp]);
6769:       rmapt[cp] = 2;
6770:       rmapa[cp] = globidx;
6771:       cmapt[cp] = 2;
6772:       cmapa[cp] = P_oth_idx;
6773:       mptmp[cp] = PETSC_FALSE;
6774:       cp++;
6775:     }
6776:     break;
6777:   default:
6778:     SETERRQ1(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Not for product type %s",MatProductTypes[ptype]);
6779:   }
6780:   /* sanity check */
6781:   if (size > 1) for (i = 0; i < cp; i++) if (rmapt[i] == 2 && !hasoffproc) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected offproc map type for product %D",i);

6783:   PetscMalloc2(cp,&mmdata->mp,cp,&mmdata->mptmp);
6784:   for (i = 0; i < cp; i++) {
6785:     mmdata->mp[i]    = mp[i];
6786:     mmdata->mptmp[i] = mptmp[i];
6787:   }
6788:   mmdata->cp = cp;
6789:   C->product->data       = mmdata;
6790:   C->product->destroy    = MatDestroy_MatMatMPIAIJBACKEND;
6791:   C->ops->productnumeric = MatProductNumeric_MPIAIJBACKEND;

6793:   /* memory type */
6794:   mmdata->mtype = PETSC_MEMTYPE_HOST;
6795:   PetscObjectTypeCompareAny((PetscObject)C,&iscuda,MATSEQAIJCUSPARSE,MATMPIAIJCUSPARSE,"");
6796:   PetscObjectTypeCompareAny((PetscObject)C,&iskokk,MATSEQAIJKOKKOS,MATMPIAIJKOKKOS,"");
6797:   if (iscuda) mmdata->mtype = PETSC_MEMTYPE_CUDA;
6798:   // enable the line below MatSeqAIJCopySubArray_SeqAIJKokkos is implemented
6799:   //else if (iskokk) mmdata->mtype = PETSC_MEMTYPE_DEVICE;

6801:   /* prepare coo coordinates for values insertion */

6803:   /* count total nonzeros of those intermediate seqaij Mats
6804:     ncoo_d:    # of nonzeros of matrices that do not have offproc entries
6805:     ncoo_o:    # of nonzeros (of matrices that might have offproc entries) that will be inserted to remote procs
6806:     ncoo_oown: # of nonzeros (of matrices that might have offproc entries) that will be inserted locally
6807:   */
6808:   for (cp = 0, ncoo_d = 0, ncoo_o = 0, ncoo_oown = 0; cp < mmdata->cp; cp++) {
6809:     Mat_SeqAIJ *mm = (Mat_SeqAIJ*)mp[cp]->data;
6810:     if (mptmp[cp]) continue;
6811:     if (rmapt[cp] == 2 && hasoffproc) { /* the rows need to be scatter to all processes (might include self) */
6812:       const PetscInt *rmap = rmapa[cp];
6813:       const PetscInt mr = mp[cp]->rmap->n;
6814:       const PetscInt rs = C->rmap->rstart;
6815:       const PetscInt re = C->rmap->rend;
6816:       const PetscInt *ii  = mm->i;
6817:       for (i = 0; i < mr; i++) {
6818:         const PetscInt gr = rmap[i];
6819:         const PetscInt nz = ii[i+1] - ii[i];
6820:         if (gr < rs || gr >= re) ncoo_o += nz; /* this row is offproc */
6821:         else ncoo_oown += nz; /* this row is local */
6822:       }
6823:     } else ncoo_d += mm->nz;
6824:   }

6826:   /*
6827:     ncoo: total number of nonzeros (including those inserted by remote procs) belonging to this proc

6829:     ncoo = ncoo_d + ncoo_oown + ncoo2, which ncoo2 is number of nonzeros inserted to me by other procs.

6831:     off[0] points to a big index array, which is shared by off[1,2,...]. Similarily, for own[0].

6833:     off[p]: points to the segment for matrix mp[p], storing location of nonzeros that mp[p] will insert to others
6834:     own[p]: points to the segment for matrix mp[p], storing location of nonzeros that mp[p] will insert locally
6835:     so, off[p+1]-off[p] is the number of nonzeros that mp[p] will send to others.

6837:     coo_i/j/v[]: [ncoo] row/col/val of nonzeros belonging to this proc.
6838:     Ex. coo_i[]: the beginning part (of size ncoo_d + ncoo_oown) stores i of local nonzeros, and the remaing part stores i of nonzeros I will receive.
6839:   */
6840:   PetscCalloc1(mmdata->cp+1,&mmdata->off); /* +1 to make a csr-like data structure */
6841:   PetscCalloc1(mmdata->cp+1,&mmdata->own);

6843:   /* gather (i,j) of nonzeros inserted by remote procs */
6844:   if (hasoffproc) {
6845:     PetscSF  msf;
6846:     PetscInt ncoo2,*coo_i2,*coo_j2;

6848:     PetscMalloc1(ncoo_o,&mmdata->off[0]);
6849:     PetscMalloc1(ncoo_oown,&mmdata->own[0]);
6850:     PetscMalloc2(ncoo_o,&coo_i,ncoo_o,&coo_j); /* to collect (i,j) of entries to be sent to others */

6852:     for (cp = 0, ncoo_o = 0; cp < mmdata->cp; cp++) {
6853:       Mat_SeqAIJ *mm = (Mat_SeqAIJ*)mp[cp]->data;
6854:       PetscInt   *idxoff = mmdata->off[cp];
6855:       PetscInt   *idxown = mmdata->own[cp];
6856:       if (!mptmp[cp] && rmapt[cp] == 2) { /* row map is sparse */
6857:         const PetscInt *rmap = rmapa[cp];
6858:         const PetscInt *cmap = cmapa[cp];
6859:         const PetscInt *ii  = mm->i;
6860:         PetscInt       *coi = coo_i + ncoo_o;
6861:         PetscInt       *coj = coo_j + ncoo_o;
6862:         const PetscInt mr = mp[cp]->rmap->n;
6863:         const PetscInt rs = C->rmap->rstart;
6864:         const PetscInt re = C->rmap->rend;
6865:         const PetscInt cs = C->cmap->rstart;
6866:         for (i = 0; i < mr; i++) {
6867:           const PetscInt *jj = mm->j + ii[i];
6868:           const PetscInt gr  = rmap[i];
6869:           const PetscInt nz  = ii[i+1] - ii[i];
6870:           if (gr < rs || gr >= re) { /* this is an offproc row */
6871:             for (j = ii[i]; j < ii[i+1]; j++) {
6872:               *coi++ = gr;
6873:               *idxoff++ = j;
6874:             }
6875:             if (!cmapt[cp]) { /* already global */
6876:               for (j = 0; j < nz; j++) *coj++ = jj[j];
6877:             } else if (cmapt[cp] == 1) { /* local to global for owned columns of C */
6878:               for (j = 0; j < nz; j++) *coj++ = jj[j] + cs;
6879:             } else { /* offdiag */
6880:               for (j = 0; j < nz; j++) *coj++ = cmap[jj[j]];
6881:             }
6882:             ncoo_o += nz;
6883:           } else { /* this is a local row */
6884:             for (j = ii[i]; j < ii[i+1]; j++) *idxown++ = j;
6885:           }
6886:         }
6887:       }
6888:       mmdata->off[cp + 1] = idxoff;
6889:       mmdata->own[cp + 1] = idxown;
6890:     }

6892:     PetscSFCreate(PetscObjectComm((PetscObject)C),&mmdata->sf);
6893:     PetscSFSetGraphLayout(mmdata->sf,C->rmap,ncoo_o/*nleaves*/,NULL/*ilocal*/,PETSC_OWN_POINTER,coo_i);
6894:     PetscSFGetMultiSF(mmdata->sf,&msf);
6895:     PetscSFGetGraph(msf,&ncoo2/*nroots*/,NULL,NULL,NULL);
6896:     ncoo = ncoo_d + ncoo_oown + ncoo2;
6897:     PetscMalloc2(ncoo,&coo_i2,ncoo,&coo_j2);
6898:     PetscSFGatherBegin(mmdata->sf,MPIU_INT,coo_i,coo_i2 + ncoo_d + ncoo_oown); /* put (i,j) of remote nonzeros at back */
6899:     PetscSFGatherEnd(mmdata->sf,MPIU_INT,coo_i,coo_i2 + ncoo_d + ncoo_oown);
6900:     PetscSFGatherBegin(mmdata->sf,MPIU_INT,coo_j,coo_j2 + ncoo_d + ncoo_oown);
6901:     PetscSFGatherEnd(mmdata->sf,MPIU_INT,coo_j,coo_j2 + ncoo_d + ncoo_oown);
6902:     PetscFree2(coo_i,coo_j);
6903:     /* allocate MPI send buffer to collect nonzero values to be sent to remote procs */
6904:     PetscSFMalloc(mmdata->sf,mmdata->mtype,ncoo_o*sizeof(PetscScalar),(void**)&mmdata->coo_w);
6905:     coo_i = coo_i2;
6906:     coo_j = coo_j2;
6907:   } else { /* no offproc values insertion */
6908:     ncoo = ncoo_d;
6909:     PetscMalloc2(ncoo,&coo_i,ncoo,&coo_j);

6911:     PetscSFCreate(PetscObjectComm((PetscObject)C),&mmdata->sf);
6912:     PetscSFSetGraph(mmdata->sf,0,0,NULL,PETSC_OWN_POINTER,NULL,PETSC_OWN_POINTER);
6913:     PetscSFSetUp(mmdata->sf);
6914:   }
6915:   mmdata->hasoffproc = hasoffproc;

6917:    /* gather (i,j) of nonzeros inserted locally */
6918:   for (cp = 0, ncoo_d = 0; cp < mmdata->cp; cp++) {
6919:     Mat_SeqAIJ     *mm = (Mat_SeqAIJ*)mp[cp]->data;
6920:     PetscInt       *coi = coo_i + ncoo_d;
6921:     PetscInt       *coj = coo_j + ncoo_d;
6922:     const PetscInt *jj  = mm->j;
6923:     const PetscInt *ii  = mm->i;
6924:     const PetscInt *cmap = cmapa[cp];
6925:     const PetscInt *rmap = rmapa[cp];
6926:     const PetscInt mr = mp[cp]->rmap->n;
6927:     const PetscInt rs = C->rmap->rstart;
6928:     const PetscInt re = C->rmap->rend;
6929:     const PetscInt cs = C->cmap->rstart;

6931:     if (mptmp[cp]) continue;
6932:     if (rmapt[cp] == 1) { /* consecutive rows */
6933:       /* fill coo_i */
6934:       for (i = 0; i < mr; i++) {
6935:         const PetscInt gr = i + rs;
6936:         for (j = ii[i]; j < ii[i+1]; j++) coi[j] = gr;
6937:       }
6938:       /* fill coo_j */
6939:       if (!cmapt[cp]) { /* type-0, already global */
6940:         PetscArraycpy(coj,jj,mm->nz);
6941:       } else if (cmapt[cp] == 1) { /* type-1, local to global for consecutive columns of C */
6942:         for (j = 0; j < mm->nz; j++) coj[j] = jj[j] + cs; /* lid + col start */
6943:       } else { /* type-2, local to global for sparse columns */
6944:         for (j = 0; j < mm->nz; j++) coj[j] = cmap[jj[j]];
6945:       }
6946:       ncoo_d += mm->nz;
6947:     } else if (rmapt[cp] == 2) { /* sparse rows */
6948:       for (i = 0; i < mr; i++) {
6949:         const PetscInt *jj = mm->j + ii[i];
6950:         const PetscInt gr  = rmap[i];
6951:         const PetscInt nz  = ii[i+1] - ii[i];
6952:         if (gr >= rs && gr < re) { /* local rows */
6953:           for (j = ii[i]; j < ii[i+1]; j++) *coi++ = gr;
6954:           if (!cmapt[cp]) { /* type-0, already global */
6955:             for (j = 0; j < nz; j++) *coj++ = jj[j];
6956:           } else if (cmapt[cp] == 1) { /* local to global for owned columns of C */
6957:             for (j = 0; j < nz; j++) *coj++ = jj[j] + cs;
6958:           } else { /* type-2, local to global for sparse columns */
6959:             for (j = 0; j < nz; j++) *coj++ = cmap[jj[j]];
6960:           }
6961:           ncoo_d += nz;
6962:         }
6963:       }
6964:     }
6965:   }
6966:   if (glob) {
6967:     ISRestoreIndices(glob,&globidx);
6968:   }
6969:   ISDestroy(&glob);
6970:   if (P_oth_l2g) {
6971:     ISLocalToGlobalMappingRestoreIndices(P_oth_l2g,&P_oth_idx);
6972:   }
6973:   ISLocalToGlobalMappingDestroy(&P_oth_l2g);
6974:   /* allocate an array to store all nonzeros (inserted locally or remotely) belonging to this proc */
6975:   PetscSFMalloc(mmdata->sf,mmdata->mtype,ncoo*sizeof(PetscScalar),(void**)&mmdata->coo_v);

6977:   /* preallocate with COO data */
6978:   MatSetPreallocationCOO(C,ncoo,coo_i,coo_j);
6979:   PetscFree2(coo_i,coo_j);
6980:   return(0);
6981: }

6983: PetscErrorCode MatProductSetFromOptions_MPIAIJBACKEND(Mat mat)
6984: {
6985:   Mat_Product    *product = mat->product;
6987: #if defined(PETSC_HAVE_DEVICE)
6988:   PetscBool      match = PETSC_FALSE;
6989:   PetscBool      usecpu = PETSC_FALSE;
6990: #else
6991:   PetscBool      match = PETSC_TRUE;
6992: #endif

6995:   MatCheckProduct(mat,1);
6996: #if defined(PETSC_HAVE_DEVICE)
6997:   if (!product->A->boundtocpu && !product->B->boundtocpu) {
6998:     PetscObjectTypeCompare((PetscObject)product->B,((PetscObject)product->A)->type_name,&match);
6999:   }
7000:   if (match) { /* we can always fallback to the CPU if requested */
7001:     switch (product->type) {
7002:     case MATPRODUCT_AB:
7003:       if (product->api_user) {
7004:         PetscOptionsBegin(PetscObjectComm((PetscObject)mat),((PetscObject)mat)->prefix,"MatMatMult","Mat");
7005:         PetscOptionsBool("-matmatmult_backend_cpu","Use CPU code","MatMatMult",usecpu,&usecpu,NULL);
7006:         PetscOptionsEnd();
7007:       } else {
7008:         PetscOptionsBegin(PetscObjectComm((PetscObject)mat),((PetscObject)mat)->prefix,"MatProduct_AB","Mat");
7009:         PetscOptionsBool("-matproduct_ab_backend_cpu","Use CPU code","MatMatMult",usecpu,&usecpu,NULL);
7010:         PetscOptionsEnd();
7011:       }
7012:       break;
7013:     case MATPRODUCT_AtB:
7014:       if (product->api_user) {
7015:         PetscOptionsBegin(PetscObjectComm((PetscObject)mat),((PetscObject)mat)->prefix,"MatTransposeMatMult","Mat");
7016:         PetscOptionsBool("-mattransposematmult_backend_cpu","Use CPU code","MatTransposeMatMult",usecpu,&usecpu,NULL);
7017:         PetscOptionsEnd();
7018:       } else {
7019:         PetscOptionsBegin(PetscObjectComm((PetscObject)mat),((PetscObject)mat)->prefix,"MatProduct_AtB","Mat");
7020:         PetscOptionsBool("-matproduct_atb_backend_cpu","Use CPU code","MatTransposeMatMult",usecpu,&usecpu,NULL);
7021:         PetscOptionsEnd();
7022:       }
7023:       break;
7024:     case MATPRODUCT_PtAP:
7025:       if (product->api_user) {
7026:         PetscOptionsBegin(PetscObjectComm((PetscObject)mat),((PetscObject)mat)->prefix,"MatPtAP","Mat");
7027:         PetscOptionsBool("-matptap_backend_cpu","Use CPU code","MatPtAP",usecpu,&usecpu,NULL);
7028:         PetscOptionsEnd();
7029:       } else {
7030:         PetscOptionsBegin(PetscObjectComm((PetscObject)mat),((PetscObject)mat)->prefix,"MatProduct_PtAP","Mat");
7031:         PetscOptionsBool("-matproduct_ptap_backend_cpu","Use CPU code","MatPtAP",usecpu,&usecpu,NULL);
7032:         PetscOptionsEnd();
7033:       }
7034:       break;
7035:     default:
7036:       break;
7037:     }
7038:     match = (PetscBool)!usecpu;
7039:   }
7040: #endif
7041:   if (match) {
7042:     switch (product->type) {
7043:     case MATPRODUCT_AB:
7044:     case MATPRODUCT_AtB:
7045:     case MATPRODUCT_PtAP:
7046:       mat->ops->productsymbolic = MatProductSymbolic_MPIAIJBACKEND;
7047:       break;
7048:     default:
7049:       break;
7050:     }
7051:   }
7052:   /* fallback to MPIAIJ ops */
7053:   if (!mat->ops->productsymbolic) {
7054:     MatProductSetFromOptions_MPIAIJ(mat);
7055:   }
7056:   return(0);
7057: }