Actual source code: mpiaij.c

petsc-master 2018-04-21
Report Typos and Errors


  3:  #include <../src/mat/impls/aij/mpi/mpiaij.h>
  4:  #include <petsc/private/vecimpl.h>
  5:  #include <petsc/private/isimpl.h>
  6:  #include <petscblaslapack.h>
  7:  #include <petscsf.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 MATAIJCUSP, MATAIJCUSPARSE, MATAIJPERM, 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: PetscErrorCode MatSetBlockSizes_MPIAIJ(Mat M, PetscInt rbs, PetscInt cbs)
 48: {
 50:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)M->data;

 53:   if (mat->A) {
 54:     MatSetBlockSizes(mat->A,rbs,cbs);
 55:     MatSetBlockSizes(mat->B,rbs,1);
 56:   }
 57:   return(0);
 58: }

 60: PetscErrorCode MatFindNonzeroRows_MPIAIJ(Mat M,IS *keptrows)
 61: {
 62:   PetscErrorCode  ierr;
 63:   Mat_MPIAIJ      *mat = (Mat_MPIAIJ*)M->data;
 64:   Mat_SeqAIJ      *a   = (Mat_SeqAIJ*)mat->A->data;
 65:   Mat_SeqAIJ      *b   = (Mat_SeqAIJ*)mat->B->data;
 66:   const PetscInt  *ia,*ib;
 67:   const MatScalar *aa,*bb;
 68:   PetscInt        na,nb,i,j,*rows,cnt=0,n0rows;
 69:   PetscInt        m = M->rmap->n,rstart = M->rmap->rstart;

 72:   *keptrows = 0;
 73:   ia        = a->i;
 74:   ib        = b->i;
 75:   for (i=0; i<m; i++) {
 76:     na = ia[i+1] - ia[i];
 77:     nb = ib[i+1] - ib[i];
 78:     if (!na && !nb) {
 79:       cnt++;
 80:       goto ok1;
 81:     }
 82:     aa = a->a + ia[i];
 83:     for (j=0; j<na; j++) {
 84:       if (aa[j] != 0.0) goto ok1;
 85:     }
 86:     bb = b->a + ib[i];
 87:     for (j=0; j <nb; j++) {
 88:       if (bb[j] != 0.0) goto ok1;
 89:     }
 90:     cnt++;
 91: ok1:;
 92:   }
 93:   MPIU_Allreduce(&cnt,&n0rows,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)M));
 94:   if (!n0rows) return(0);
 95:   PetscMalloc1(M->rmap->n-cnt,&rows);
 96:   cnt  = 0;
 97:   for (i=0; i<m; i++) {
 98:     na = ia[i+1] - ia[i];
 99:     nb = ib[i+1] - ib[i];
100:     if (!na && !nb) continue;
101:     aa = a->a + ia[i];
102:     for (j=0; j<na;j++) {
103:       if (aa[j] != 0.0) {
104:         rows[cnt++] = rstart + i;
105:         goto ok2;
106:       }
107:     }
108:     bb = b->a + ib[i];
109:     for (j=0; j<nb; j++) {
110:       if (bb[j] != 0.0) {
111:         rows[cnt++] = rstart + i;
112:         goto ok2;
113:       }
114:     }
115: ok2:;
116:   }
117:   ISCreateGeneral(PetscObjectComm((PetscObject)M),cnt,rows,PETSC_OWN_POINTER,keptrows);
118:   return(0);
119: }

121: PetscErrorCode  MatDiagonalSet_MPIAIJ(Mat Y,Vec D,InsertMode is)
122: {
123:   PetscErrorCode    ierr;
124:   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*) Y->data;

127:   if (Y->assembled && Y->rmap->rstart == Y->cmap->rstart && Y->rmap->rend == Y->cmap->rend) {
128:     MatDiagonalSet(aij->A,D,is);
129:   } else {
130:     MatDiagonalSet_Default(Y,D,is);
131:   }
132:   return(0);
133: }

135: PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows)
136: {
137:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)M->data;
139:   PetscInt       i,rstart,nrows,*rows;

142:   *zrows = NULL;
143:   MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);
144:   MatGetOwnershipRange(M,&rstart,NULL);
145:   for (i=0; i<nrows; i++) rows[i] += rstart;
146:   ISCreateGeneral(PetscObjectComm((PetscObject)M),nrows,rows,PETSC_OWN_POINTER,zrows);
147:   return(0);
148: }

150: PetscErrorCode MatGetColumnNorms_MPIAIJ(Mat A,NormType type,PetscReal *norms)
151: {
153:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)A->data;
154:   PetscInt       i,n,*garray = aij->garray;
155:   Mat_SeqAIJ     *a_aij = (Mat_SeqAIJ*) aij->A->data;
156:   Mat_SeqAIJ     *b_aij = (Mat_SeqAIJ*) aij->B->data;
157:   PetscReal      *work;

160:   MatGetSize(A,NULL,&n);
161:   PetscCalloc1(n,&work);
162:   if (type == NORM_2) {
163:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
164:       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]*a_aij->a[i]);
165:     }
166:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
167:       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]*b_aij->a[i]);
168:     }
169:   } else if (type == NORM_1) {
170:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
171:       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]);
172:     }
173:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
174:       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]);
175:     }
176:   } else if (type == NORM_INFINITY) {
177:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
178:       work[A->cmap->rstart + a_aij->j[i]] = PetscMax(PetscAbsScalar(a_aij->a[i]), work[A->cmap->rstart + a_aij->j[i]]);
179:     }
180:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
181:       work[garray[b_aij->j[i]]] = PetscMax(PetscAbsScalar(b_aij->a[i]),work[garray[b_aij->j[i]]]);
182:     }

184:   } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown NormType");
185:   if (type == NORM_INFINITY) {
186:     MPIU_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));
187:   } else {
188:     MPIU_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));
189:   }
190:   PetscFree(work);
191:   if (type == NORM_2) {
192:     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
193:   }
194:   return(0);
195: }

197: PetscErrorCode MatFindOffBlockDiagonalEntries_MPIAIJ(Mat A,IS *is)
198: {
199:   Mat_MPIAIJ      *a  = (Mat_MPIAIJ*)A->data;
200:   IS              sis,gis;
201:   PetscErrorCode  ierr;
202:   const PetscInt  *isis,*igis;
203:   PetscInt        n,*iis,nsis,ngis,rstart,i;

206:   MatFindOffBlockDiagonalEntries(a->A,&sis);
207:   MatFindNonzeroRows(a->B,&gis);
208:   ISGetSize(gis,&ngis);
209:   ISGetSize(sis,&nsis);
210:   ISGetIndices(sis,&isis);
211:   ISGetIndices(gis,&igis);

213:   PetscMalloc1(ngis+nsis,&iis);
214:   PetscMemcpy(iis,igis,ngis*sizeof(PetscInt));
215:   PetscMemcpy(iis+ngis,isis,nsis*sizeof(PetscInt));
216:   n    = ngis + nsis;
217:   PetscSortRemoveDupsInt(&n,iis);
218:   MatGetOwnershipRange(A,&rstart,NULL);
219:   for (i=0; i<n; i++) iis[i] += rstart;
220:   ISCreateGeneral(PetscObjectComm((PetscObject)A),n,iis,PETSC_OWN_POINTER,is);

222:   ISRestoreIndices(sis,&isis);
223:   ISRestoreIndices(gis,&igis);
224:   ISDestroy(&sis);
225:   ISDestroy(&gis);
226:   return(0);
227: }

229: /*
230:     Distributes a SeqAIJ matrix across a set of processes. Code stolen from
231:     MatLoad_MPIAIJ(). Horrible lack of reuse. Should be a routine for each matrix type.

233:     Only for square matrices

235:     Used by a preconditioner, hence PETSC_EXTERN
236: */
237: PETSC_EXTERN PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat)
238: {
239:   PetscMPIInt    rank,size;
240:   PetscInt       *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz = 0,*gmataj,cnt,row,*ld,bses[2];
242:   Mat            mat;
243:   Mat_SeqAIJ     *gmata;
244:   PetscMPIInt    tag;
245:   MPI_Status     status;
246:   PetscBool      aij;
247:   MatScalar      *gmataa,*ao,*ad,*gmataarestore=0;

250:   MPI_Comm_rank(comm,&rank);
251:   MPI_Comm_size(comm,&size);
252:   if (!rank) {
253:     PetscObjectTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);
254:     if (!aij) SETERRQ1(PetscObjectComm((PetscObject)gmat),PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name);
255:   }
256:   if (reuse == MAT_INITIAL_MATRIX) {
257:     MatCreate(comm,&mat);
258:     MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);
259:     MatGetBlockSizes(gmat,&bses[0],&bses[1]);
260:     MPI_Bcast(bses,2,MPIU_INT,0,comm);
261:     MatSetBlockSizes(mat,bses[0],bses[1]);
262:     MatSetType(mat,MATAIJ);
263:     PetscMalloc1(size+1,&rowners);
264:     PetscMalloc2(m,&dlens,m,&olens);
265:     MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);

267:     rowners[0] = 0;
268:     for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
269:     rstart = rowners[rank];
270:     rend   = rowners[rank+1];
271:     PetscObjectGetNewTag((PetscObject)mat,&tag);
272:     if (!rank) {
273:       gmata = (Mat_SeqAIJ*) gmat->data;
274:       /* send row lengths to all processors */
275:       for (i=0; i<m; i++) dlens[i] = gmata->ilen[i];
276:       for (i=1; i<size; i++) {
277:         MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);
278:       }
279:       /* determine number diagonal and off-diagonal counts */
280:       PetscMemzero(olens,m*sizeof(PetscInt));
281:       PetscCalloc1(m,&ld);
282:       jj   = 0;
283:       for (i=0; i<m; i++) {
284:         for (j=0; j<dlens[i]; j++) {
285:           if (gmata->j[jj] < rstart) ld[i]++;
286:           if (gmata->j[jj] < rstart || gmata->j[jj] >= rend) olens[i]++;
287:           jj++;
288:         }
289:       }
290:       /* send column indices to other processes */
291:       for (i=1; i<size; i++) {
292:         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
293:         MPI_Send(&nz,1,MPIU_INT,i,tag,comm);
294:         MPI_Send(gmata->j + gmata->i[rowners[i]],nz,MPIU_INT,i,tag,comm);
295:       }

297:       /* send numerical values to other processes */
298:       for (i=1; i<size; i++) {
299:         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
300:         MPI_Send(gmata->a + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);
301:       }
302:       gmataa = gmata->a;
303:       gmataj = gmata->j;

305:     } else {
306:       /* receive row lengths */
307:       MPI_Recv(dlens,m,MPIU_INT,0,tag,comm,&status);
308:       /* receive column indices */
309:       MPI_Recv(&nz,1,MPIU_INT,0,tag,comm,&status);
310:       PetscMalloc2(nz,&gmataa,nz,&gmataj);
311:       MPI_Recv(gmataj,nz,MPIU_INT,0,tag,comm,&status);
312:       /* determine number diagonal and off-diagonal counts */
313:       PetscMemzero(olens,m*sizeof(PetscInt));
314:       PetscCalloc1(m,&ld);
315:       jj   = 0;
316:       for (i=0; i<m; i++) {
317:         for (j=0; j<dlens[i]; j++) {
318:           if (gmataj[jj] < rstart) ld[i]++;
319:           if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++;
320:           jj++;
321:         }
322:       }
323:       /* receive numerical values */
324:       PetscMemzero(gmataa,nz*sizeof(PetscScalar));
325:       MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);
326:     }
327:     /* set preallocation */
328:     for (i=0; i<m; i++) {
329:       dlens[i] -= olens[i];
330:     }
331:     MatSeqAIJSetPreallocation(mat,0,dlens);
332:     MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);

334:     for (i=0; i<m; i++) {
335:       dlens[i] += olens[i];
336:     }
337:     cnt = 0;
338:     for (i=0; i<m; i++) {
339:       row  = rstart + i;
340:       MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);
341:       cnt += dlens[i];
342:     }
343:     if (rank) {
344:       PetscFree2(gmataa,gmataj);
345:     }
346:     PetscFree2(dlens,olens);
347:     PetscFree(rowners);

349:     ((Mat_MPIAIJ*)(mat->data))->ld = ld;

351:     *inmat = mat;
352:   } else {   /* column indices are already set; only need to move over numerical values from process 0 */
353:     Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data;
354:     Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data;
355:     mat  = *inmat;
356:     PetscObjectGetNewTag((PetscObject)mat,&tag);
357:     if (!rank) {
358:       /* send numerical values to other processes */
359:       gmata  = (Mat_SeqAIJ*) gmat->data;
360:       MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);
361:       gmataa = gmata->a;
362:       for (i=1; i<size; i++) {
363:         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
364:         MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);
365:       }
366:       nz = gmata->i[rowners[1]]-gmata->i[rowners[0]];
367:     } else {
368:       /* receive numerical values from process 0*/
369:       nz   = Ad->nz + Ao->nz;
370:       PetscMalloc1(nz,&gmataa); gmataarestore = gmataa;
371:       MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);
372:     }
373:     /* transfer numerical values into the diagonal A and off diagonal B parts of mat */
374:     ld = ((Mat_MPIAIJ*)(mat->data))->ld;
375:     ad = Ad->a;
376:     ao = Ao->a;
377:     if (mat->rmap->n) {
378:       i  = 0;
379:       nz = ld[i];                                   PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar)); ao += nz; gmataa += nz;
380:       nz = Ad->i[i+1] - Ad->i[i];                   PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar)); ad += nz; gmataa += nz;
381:     }
382:     for (i=1; i<mat->rmap->n; i++) {
383:       nz = Ao->i[i] - Ao->i[i-1] - ld[i-1] + ld[i]; PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar)); ao += nz; gmataa += nz;
384:       nz = Ad->i[i+1] - Ad->i[i];                   PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar)); ad += nz; gmataa += nz;
385:     }
386:     i--;
387:     if (mat->rmap->n) {
388:       nz = Ao->i[i+1] - Ao->i[i] - ld[i];           PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));
389:     }
390:     if (rank) {
391:       PetscFree(gmataarestore);
392:     }
393:   }
394:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
395:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
396:   return(0);
397: }

399: /*
400:   Local utility routine that creates a mapping from the global column
401: number to the local number in the off-diagonal part of the local
402: storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
403: a slightly higher hash table cost; without it it is not scalable (each processor
404: has an order N integer array but is fast to acess.
405: */
406: PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat mat)
407: {
408:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
410:   PetscInt       n = aij->B->cmap->n,i;

413:   if (!aij->garray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPIAIJ Matrix was assembled but is missing garray");
414: #if defined(PETSC_USE_CTABLE)
415:   PetscTableCreate(n,mat->cmap->N+1,&aij->colmap);
416:   for (i=0; i<n; i++) {
417:     PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1,INSERT_VALUES);
418:   }
419: #else
420:   PetscCalloc1(mat->cmap->N+1,&aij->colmap);
421:   PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N+1)*sizeof(PetscInt));
422:   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
423: #endif
424:   return(0);
425: }

427: #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv,orow,ocol)     \
428: { \
429:     if (col <= lastcol1)  low1 = 0;     \
430:     else                 high1 = nrow1; \
431:     lastcol1 = col;\
432:     while (high1-low1 > 5) { \
433:       t = (low1+high1)/2; \
434:       if (rp1[t] > col) high1 = t; \
435:       else              low1  = t; \
436:     } \
437:       for (_i=low1; _i<high1; _i++) { \
438:         if (rp1[_i] > col) break; \
439:         if (rp1[_i] == col) { \
440:           if (addv == ADD_VALUES) ap1[_i] += value;   \
441:           else                    ap1[_i] = value; \
442:           goto a_noinsert; \
443:         } \
444:       }  \
445:       if (value == 0.0 && ignorezeroentries && row != col) {low1 = 0; high1 = nrow1;goto a_noinsert;} \
446:       if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;}                \
447:       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); \
448:       MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \
449:       N = nrow1++ - 1; a->nz++; high1++; \
450:       /* shift up all the later entries in this row */ \
451:       for (ii=N; ii>=_i; ii--) { \
452:         rp1[ii+1] = rp1[ii]; \
453:         ap1[ii+1] = ap1[ii]; \
454:       } \
455:       rp1[_i] = col;  \
456:       ap1[_i] = value;  \
457:       A->nonzerostate++;\
458:       a_noinsert: ; \
459:       ailen[row] = nrow1; \
460: }

462: #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv,orow,ocol) \
463:   { \
464:     if (col <= lastcol2) low2 = 0;                        \
465:     else high2 = nrow2;                                   \
466:     lastcol2 = col;                                       \
467:     while (high2-low2 > 5) {                              \
468:       t = (low2+high2)/2;                                 \
469:       if (rp2[t] > col) high2 = t;                        \
470:       else             low2  = t;                         \
471:     }                                                     \
472:     for (_i=low2; _i<high2; _i++) {                       \
473:       if (rp2[_i] > col) break;                           \
474:       if (rp2[_i] == col) {                               \
475:         if (addv == ADD_VALUES) ap2[_i] += value;         \
476:         else                    ap2[_i] = value;          \
477:         goto b_noinsert;                                  \
478:       }                                                   \
479:     }                                                     \
480:     if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \
481:     if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;}                        \
482:     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); \
483:     MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \
484:     N = nrow2++ - 1; b->nz++; high2++;                    \
485:     /* shift up all the later entries in this row */      \
486:     for (ii=N; ii>=_i; ii--) {                            \
487:       rp2[ii+1] = rp2[ii];                                \
488:       ap2[ii+1] = ap2[ii];                                \
489:     }                                                     \
490:     rp2[_i] = col;                                        \
491:     ap2[_i] = value;                                      \
492:     B->nonzerostate++;                                    \
493:     b_noinsert: ;                                         \
494:     bilen[row] = nrow2;                                   \
495:   }

497: PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[])
498: {
499:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)A->data;
500:   Mat_SeqAIJ     *a   = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data;
502:   PetscInt       l,*garray = mat->garray,diag;

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

507:   /* find size of row to the left of the diagonal part */
508:   MatGetOwnershipRange(A,&diag,0);
509:   row  = row - diag;
510:   for (l=0; l<b->i[row+1]-b->i[row]; l++) {
511:     if (garray[b->j[b->i[row]+l]] > diag) break;
512:   }
513:   PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));

515:   /* diagonal part */
516:   PetscMemcpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row])*sizeof(PetscScalar));

518:   /* right of diagonal part */
519:   PetscMemcpy(b->a+b->i[row]+l,v+l+a->i[row+1]-a->i[row],(b->i[row+1]-b->i[row]-l)*sizeof(PetscScalar));
520:   return(0);
521: }

523: PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
524: {
525:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
526:   PetscScalar    value;
528:   PetscInt       i,j,rstart  = mat->rmap->rstart,rend = mat->rmap->rend;
529:   PetscInt       cstart      = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
530:   PetscBool      roworiented = aij->roworiented;

532:   /* Some Variables required in the macro */
533:   Mat        A                 = aij->A;
534:   Mat_SeqAIJ *a                = (Mat_SeqAIJ*)A->data;
535:   PetscInt   *aimax            = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
536:   MatScalar  *aa               = a->a;
537:   PetscBool  ignorezeroentries = a->ignorezeroentries;
538:   Mat        B                 = aij->B;
539:   Mat_SeqAIJ *b                = (Mat_SeqAIJ*)B->data;
540:   PetscInt   *bimax            = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
541:   MatScalar  *ba               = b->a;

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

548:   for (i=0; i<m; i++) {
549:     if (im[i] < 0) continue;
550: #if defined(PETSC_USE_DEBUG)
551:     if (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);
552: #endif
553:     if (im[i] >= rstart && im[i] < rend) {
554:       row      = im[i] - rstart;
555:       lastcol1 = -1;
556:       rp1      = aj + ai[row];
557:       ap1      = aa + ai[row];
558:       rmax1    = aimax[row];
559:       nrow1    = ailen[row];
560:       low1     = 0;
561:       high1    = nrow1;
562:       lastcol2 = -1;
563:       rp2      = bj + bi[row];
564:       ap2      = ba + bi[row];
565:       rmax2    = bimax[row];
566:       nrow2    = bilen[row];
567:       low2     = 0;
568:       high2    = nrow2;

570:       for (j=0; j<n; j++) {
571:         if (roworiented) value = v[i*n+j];
572:         else             value = v[i+j*m];
573:         if (in[j] >= cstart && in[j] < cend) {
574:           col   = in[j] - cstart;
575:           nonew = a->nonew;
576:           if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES) && row != col) continue;
577:           MatSetValues_SeqAIJ_A_Private(row,col,value,addv,im[i],in[j]);
578:         } else if (in[j] < 0) continue;
579: #if defined(PETSC_USE_DEBUG)
580:         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);
581: #endif
582:         else {
583:           if (mat->was_assembled) {
584:             if (!aij->colmap) {
585:               MatCreateColmap_MPIAIJ_Private(mat);
586:             }
587: #if defined(PETSC_USE_CTABLE)
588:             PetscTableFind(aij->colmap,in[j]+1,&col);
589:             col--;
590: #else
591:             col = aij->colmap[in[j]] - 1;
592: #endif
593:             if (col < 0 && !((Mat_SeqAIJ*)(aij->B->data))->nonew) {
594:               MatDisAssemble_MPIAIJ(mat);
595:               col  =  in[j];
596:               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
597:               B     = aij->B;
598:               b     = (Mat_SeqAIJ*)B->data;
599:               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a;
600:               rp2   = bj + bi[row];
601:               ap2   = ba + bi[row];
602:               rmax2 = bimax[row];
603:               nrow2 = bilen[row];
604:               low2  = 0;
605:               high2 = nrow2;
606:               bm    = aij->B->rmap->n;
607:               ba    = b->a;
608:             } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at global row/column (%D, %D) into matrix", im[i], in[j]);
609:           } else col = in[j];
610:           nonew = b->nonew;
611:           MatSetValues_SeqAIJ_B_Private(row,col,value,addv,im[i],in[j]);
612:         }
613:       }
614:     } else {
615:       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]);
616:       if (!aij->donotstash) {
617:         mat->assembled = PETSC_FALSE;
618:         if (roworiented) {
619:           MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
620:         } else {
621:           MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
622:         }
623:       }
624:     }
625:   }
626:   return(0);
627: }

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

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

669: extern PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat,Vec,Vec);

671: PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
672: {
673:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
675:   PetscInt       nstash,reallocs;

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

680:   MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
681:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
682:   PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
683:   return(0);
684: }

686: PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
687: {
688:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
689:   Mat_SeqAIJ     *a   = (Mat_SeqAIJ*)aij->A->data;
691:   PetscMPIInt    n;
692:   PetscInt       i,j,rstart,ncols,flg;
693:   PetscInt       *row,*col;
694:   PetscBool      other_disassembled;
695:   PetscScalar    *val;

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

700:   if (!aij->donotstash && !mat->nooffprocentries) {
701:     while (1) {
702:       MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
703:       if (!flg) break;

705:       for (i=0; i<n; ) {
706:         /* Now identify the consecutive vals belonging to the same row */
707:         for (j=i,rstart=row[j]; j<n; j++) {
708:           if (row[j] != rstart) break;
709:         }
710:         if (j < n) ncols = j-i;
711:         else       ncols = n-i;
712:         /* Now assemble all these values with a single function call */
713:         MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,mat->insertmode);

715:         i = j;
716:       }
717:     }
718:     MatStashScatterEnd_Private(&mat->stash);
719:   }
720:   MatAssemblyBegin(aij->A,mode);
721:   MatAssemblyEnd(aij->A,mode);

723:   /* determine if any processor has disassembled, if so we must
724:      also disassemble ourselfs, in order that we may reassemble. */
725:   /*
726:      if nonzero structure of submatrix B cannot change then we know that
727:      no processor disassembled thus we can skip this stuff
728:   */
729:   if (!((Mat_SeqAIJ*)aij->B->data)->nonew) {
730:     MPIU_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));
731:     if (mat->was_assembled && !other_disassembled) {
732:       MatDisAssemble_MPIAIJ(mat);
733:     }
734:   }
735:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
736:     MatSetUpMultiply_MPIAIJ(mat);
737:   }
738:   MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);
739:   MatAssemblyBegin(aij->B,mode);
740:   MatAssemblyEnd(aij->B,mode);

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

744:   aij->rowvalues = 0;

746:   VecDestroy(&aij->diag);
747:   if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ;

749:   /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
750:   if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
751:     PetscObjectState state = aij->A->nonzerostate + aij->B->nonzerostate;
752:     MPIU_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));
753:   }
754:   return(0);
755: }

757: PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
758: {
759:   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;

763:   MatZeroEntries(l->A);
764:   MatZeroEntries(l->B);
765:   return(0);
766: }

768: PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
769: {
770:   Mat_MPIAIJ    *mat    = (Mat_MPIAIJ *) A->data;
771:   PetscInt      *lrows;
772:   PetscInt       r, len;

776:   /* get locally owned rows */
777:   MatZeroRowsMapLocal_Private(A,N,rows,&len,&lrows);
778:   /* fix right hand side if needed */
779:   if (x && b) {
780:     const PetscScalar *xx;
781:     PetscScalar       *bb;

783:     VecGetArrayRead(x, &xx);
784:     VecGetArray(b, &bb);
785:     for (r = 0; r < len; ++r) bb[lrows[r]] = diag*xx[lrows[r]];
786:     VecRestoreArrayRead(x, &xx);
787:     VecRestoreArray(b, &bb);
788:   }
789:   /* Must zero l->B before l->A because the (diag) case below may put values into l->B*/
790:   MatZeroRows(mat->B, len, lrows, 0.0, NULL, NULL);
791:   if (A->congruentlayouts == -1) { /* first time we compare rows and cols layouts */
792:     PetscBool cong;
793:     PetscLayoutCompare(A->rmap,A->cmap,&cong);
794:     if (cong) A->congruentlayouts = 1;
795:     else      A->congruentlayouts = 0;
796:   }
797:   if ((diag != 0.0) && A->congruentlayouts) {
798:     MatZeroRows(mat->A, len, lrows, diag, NULL, NULL);
799:   } else if (diag != 0.0) {
800:     MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);
801:     if (((Mat_SeqAIJ *) mat->A->data)->nonew) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "MatZeroRows() on rectangular matrices cannot be used with the Mat options\nMAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
802:     for (r = 0; r < len; ++r) {
803:       const PetscInt row = lrows[r] + A->rmap->rstart;
804:       MatSetValues(A, 1, &row, 1, &row, &diag, INSERT_VALUES);
805:     }
806:     MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
807:     MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
808:   } else {
809:     MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);
810:   }
811:   PetscFree(lrows);

813:   /* only change matrix nonzero state if pattern was allowed to be changed */
814:   if (!((Mat_SeqAIJ*)(mat->A->data))->keepnonzeropattern) {
815:     PetscObjectState state = mat->A->nonzerostate + mat->B->nonzerostate;
816:     MPIU_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
817:   }
818:   return(0);
819: }

821: PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
822: {
823:   Mat_MPIAIJ        *l = (Mat_MPIAIJ*)A->data;
824:   PetscErrorCode    ierr;
825:   PetscMPIInt       n = A->rmap->n;
826:   PetscInt          i,j,r,m,p = 0,len = 0;
827:   PetscInt          *lrows,*owners = A->rmap->range;
828:   PetscSFNode       *rrows;
829:   PetscSF           sf;
830:   const PetscScalar *xx;
831:   PetscScalar       *bb,*mask;
832:   Vec               xmask,lmask;
833:   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*)l->B->data;
834:   const PetscInt    *aj, *ii,*ridx;
835:   PetscScalar       *aa;

838:   /* Create SF where leaves are input rows and roots are owned rows */
839:   PetscMalloc1(n, &lrows);
840:   for (r = 0; r < n; ++r) lrows[r] = -1;
841:   PetscMalloc1(N, &rrows);
842:   for (r = 0; r < N; ++r) {
843:     const PetscInt idx   = rows[r];
844:     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);
845:     if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
846:       PetscLayoutFindOwner(A->rmap,idx,&p);
847:     }
848:     rrows[r].rank  = p;
849:     rrows[r].index = rows[r] - owners[p];
850:   }
851:   PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);
852:   PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);
853:   /* Collect flags for rows to be zeroed */
854:   PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
855:   PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
856:   PetscSFDestroy(&sf);
857:   /* Compress and put in row numbers */
858:   for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
859:   /* zero diagonal part of matrix */
860:   MatZeroRowsColumns(l->A,len,lrows,diag,x,b);
861:   /* handle off diagonal part of matrix */
862:   MatCreateVecs(A,&xmask,NULL);
863:   VecDuplicate(l->lvec,&lmask);
864:   VecGetArray(xmask,&bb);
865:   for (i=0; i<len; i++) bb[lrows[i]] = 1;
866:   VecRestoreArray(xmask,&bb);
867:   VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
868:   VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
869:   VecDestroy(&xmask);
870:   if (x) {
871:     VecScatterBegin(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
872:     VecScatterEnd(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
873:     VecGetArrayRead(l->lvec,&xx);
874:     VecGetArray(b,&bb);
875:   }
876:   VecGetArray(lmask,&mask);
877:   /* remove zeroed rows of off diagonal matrix */
878:   ii = aij->i;
879:   for (i=0; i<len; i++) {
880:     PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));
881:   }
882:   /* loop over all elements of off process part of matrix zeroing removed columns*/
883:   if (aij->compressedrow.use) {
884:     m    = aij->compressedrow.nrows;
885:     ii   = aij->compressedrow.i;
886:     ridx = aij->compressedrow.rindex;
887:     for (i=0; i<m; i++) {
888:       n  = ii[i+1] - ii[i];
889:       aj = aij->j + ii[i];
890:       aa = aij->a + ii[i];

892:       for (j=0; j<n; j++) {
893:         if (PetscAbsScalar(mask[*aj])) {
894:           if (b) bb[*ridx] -= *aa*xx[*aj];
895:           *aa = 0.0;
896:         }
897:         aa++;
898:         aj++;
899:       }
900:       ridx++;
901:     }
902:   } else { /* do not use compressed row format */
903:     m = l->B->rmap->n;
904:     for (i=0; i<m; i++) {
905:       n  = ii[i+1] - ii[i];
906:       aj = aij->j + ii[i];
907:       aa = aij->a + ii[i];
908:       for (j=0; j<n; j++) {
909:         if (PetscAbsScalar(mask[*aj])) {
910:           if (b) bb[i] -= *aa*xx[*aj];
911:           *aa = 0.0;
912:         }
913:         aa++;
914:         aj++;
915:       }
916:     }
917:   }
918:   if (x) {
919:     VecRestoreArray(b,&bb);
920:     VecRestoreArrayRead(l->lvec,&xx);
921:   }
922:   VecRestoreArray(lmask,&mask);
923:   VecDestroy(&lmask);
924:   PetscFree(lrows);

926:   /* only change matrix nonzero state if pattern was allowed to be changed */
927:   if (!((Mat_SeqAIJ*)(l->A->data))->keepnonzeropattern) {
928:     PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
929:     MPIU_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
930:   }
931:   return(0);
932: }

934: PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
935: {
936:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
938:   PetscInt       nt;
939:   VecScatter     Mvctx = a->Mvctx;

942:   VecGetLocalSize(xx,&nt);
943:   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);

945:   VecScatterBegin(Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
946:   (*a->A->ops->mult)(a->A,xx,yy);
947:   VecScatterEnd(Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
948:   (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
949:   return(0);
950: }

952: PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx)
953: {
954:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

958:   MatMultDiagonalBlock(a->A,bb,xx);
959:   return(0);
960: }

962: PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
963: {
964:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
966:   VecScatter     Mvctx = a->Mvctx;

969:   if (a->Mvctx_mpi1_flg) Mvctx = a->Mvctx_mpi1;
970:   VecScatterBegin(Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
971:   (*a->A->ops->multadd)(a->A,xx,yy,zz);
972:   VecScatterEnd(Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
973:   (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
974:   return(0);
975: }

977: PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
978: {
979:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
981:   PetscBool      merged;

984:   VecScatterGetMerged(a->Mvctx,&merged);
985:   /* do nondiagonal part */
986:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
987:   if (!merged) {
988:     /* send it on its way */
989:     VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
990:     /* do local part */
991:     (*a->A->ops->multtranspose)(a->A,xx,yy);
992:     /* receive remote parts: note this assumes the values are not actually */
993:     /* added in yy until the next line, */
994:     VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
995:   } else {
996:     /* do local part */
997:     (*a->A->ops->multtranspose)(a->A,xx,yy);
998:     /* send it on its way */
999:     VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1000:     /* values actually were received in the Begin() but we need to call this nop */
1001:     VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1002:   }
1003:   return(0);
1004: }

1006: PetscErrorCode MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool  *f)
1007: {
1008:   MPI_Comm       comm;
1009:   Mat_MPIAIJ     *Aij = (Mat_MPIAIJ*) Amat->data, *Bij;
1010:   Mat            Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
1011:   IS             Me,Notme;
1013:   PetscInt       M,N,first,last,*notme,i;
1014:   PetscMPIInt    size;

1017:   /* Easy test: symmetric diagonal block */
1018:   Bij  = (Mat_MPIAIJ*) Bmat->data; Bdia = Bij->A;
1019:   MatIsTranspose(Adia,Bdia,tol,f);
1020:   if (!*f) return(0);
1021:   PetscObjectGetComm((PetscObject)Amat,&comm);
1022:   MPI_Comm_size(comm,&size);
1023:   if (size == 1) return(0);

1025:   /* Hard test: off-diagonal block. This takes a MatCreateSubMatrix. */
1026:   MatGetSize(Amat,&M,&N);
1027:   MatGetOwnershipRange(Amat,&first,&last);
1028:   PetscMalloc1(N-last+first,&notme);
1029:   for (i=0; i<first; i++) notme[i] = i;
1030:   for (i=last; i<M; i++) notme[i-last+first] = i;
1031:   ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);
1032:   ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);
1033:   MatCreateSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);
1034:   Aoff = Aoffs[0];
1035:   MatCreateSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);
1036:   Boff = Boffs[0];
1037:   MatIsTranspose(Aoff,Boff,tol,f);
1038:   MatDestroyMatrices(1,&Aoffs);
1039:   MatDestroyMatrices(1,&Boffs);
1040:   ISDestroy(&Me);
1041:   ISDestroy(&Notme);
1042:   PetscFree(notme);
1043:   return(0);
1044: }

1046: PetscErrorCode MatIsSymmetric_MPIAIJ(Mat A,PetscReal tol,PetscBool  *f)
1047: {

1051:   MatIsTranspose_MPIAIJ(A,A,tol,f);
1052:   return(0);
1053: }

1055: PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1056: {
1057:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1061:   /* do nondiagonal part */
1062:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1063:   /* send it on its way */
1064:   VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1065:   /* do local part */
1066:   (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);
1067:   /* receive remote parts */
1068:   VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1069:   return(0);
1070: }

1072: /*
1073:   This only works correctly for square matrices where the subblock A->A is the
1074:    diagonal block
1075: */
1076: PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
1077: {
1079:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1082:   if (A->rmap->N != A->cmap->N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
1083:   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");
1084:   MatGetDiagonal(a->A,v);
1085:   return(0);
1086: }

1088: PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
1089: {
1090:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1094:   MatScale(a->A,aa);
1095:   MatScale(a->B,aa);
1096:   return(0);
1097: }

1099: PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
1100: {
1101:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

1105: #if defined(PETSC_USE_LOG)
1106:   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N);
1107: #endif
1108:   MatStashDestroy_Private(&mat->stash);
1109:   VecDestroy(&aij->diag);
1110:   MatDestroy(&aij->A);
1111:   MatDestroy(&aij->B);
1112: #if defined(PETSC_USE_CTABLE)
1113:   PetscTableDestroy(&aij->colmap);
1114: #else
1115:   PetscFree(aij->colmap);
1116: #endif
1117:   PetscFree(aij->garray);
1118:   VecDestroy(&aij->lvec);
1119:   VecScatterDestroy(&aij->Mvctx);
1120:   if (aij->Mvctx_mpi1) {VecScatterDestroy(&aij->Mvctx_mpi1);}
1121:   PetscFree2(aij->rowvalues,aij->rowindices);
1122:   PetscFree(aij->ld);
1123:   PetscFree(mat->data);

1125:   PetscObjectChangeTypeName((PetscObject)mat,0);
1126:   PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);
1127:   PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);
1128:   PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C",NULL);
1129:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C",NULL);
1130:   PetscObjectComposeFunction((PetscObject)mat,"MatResetPreallocation_C",NULL);
1131:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C",NULL);
1132:   PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);
1133:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C",NULL);
1134: #if defined(PETSC_HAVE_ELEMENTAL)
1135:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_elemental_C",NULL);
1136: #endif
1137: #if defined(PETSC_HAVE_HYPRE)
1138:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_hypre_C",NULL);
1139:   PetscObjectComposeFunction((PetscObject)mat,"MatMatMatMult_transpose_mpiaij_mpiaij_C",NULL);
1140: #endif
1141:   return(0);
1142: }

1144: PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
1145: {
1146:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1147:   Mat_SeqAIJ     *A   = (Mat_SeqAIJ*)aij->A->data;
1148:   Mat_SeqAIJ     *B   = (Mat_SeqAIJ*)aij->B->data;
1150:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
1151:   int            fd;
1152:   PetscInt       nz,header[4],*row_lengths,*range=0,rlen,i;
1153:   PetscInt       nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz = 0;
1154:   PetscScalar    *column_values;
1155:   PetscInt       message_count,flowcontrolcount;
1156:   FILE           *file;

1159:   MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
1160:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
1161:   nz   = A->nz + B->nz;
1162:   PetscViewerBinaryGetDescriptor(viewer,&fd);
1163:   if (!rank) {
1164:     header[0] = MAT_FILE_CLASSID;
1165:     header[1] = mat->rmap->N;
1166:     header[2] = mat->cmap->N;

1168:     MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1169:     PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);
1170:     /* get largest number of rows any processor has */
1171:     rlen  = mat->rmap->n;
1172:     range = mat->rmap->range;
1173:     for (i=1; i<size; i++) rlen = PetscMax(rlen,range[i+1] - range[i]);
1174:   } else {
1175:     MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1176:     rlen = mat->rmap->n;
1177:   }

1179:   /* load up the local row counts */
1180:   PetscMalloc1(rlen+1,&row_lengths);
1181:   for (i=0; i<mat->rmap->n; i++) row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];

1183:   /* store the row lengths to the file */
1184:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1185:   if (!rank) {
1186:     PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);
1187:     for (i=1; i<size; i++) {
1188:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1189:       rlen = range[i+1] - range[i];
1190:       MPIULong_Recv(row_lengths,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));
1191:       PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);
1192:     }
1193:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1194:   } else {
1195:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1196:     MPIULong_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1197:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1198:   }
1199:   PetscFree(row_lengths);

1201:   /* load up the local column indices */
1202:   nzmax = nz; /* th processor needs space a largest processor needs */
1203:   MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));
1204:   PetscMalloc1(nzmax+1,&column_indices);
1205:   cnt   = 0;
1206:   for (i=0; i<mat->rmap->n; i++) {
1207:     for (j=B->i[i]; j<B->i[i+1]; j++) {
1208:       if ((col = garray[B->j[j]]) > cstart) break;
1209:       column_indices[cnt++] = col;
1210:     }
1211:     for (k=A->i[i]; k<A->i[i+1]; k++) column_indices[cnt++] = A->j[k] + cstart;
1212:     for (; j<B->i[i+1]; j++) column_indices[cnt++] = garray[B->j[j]];
1213:   }
1214:   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz);

1216:   /* store the column indices to the file */
1217:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1218:   if (!rank) {
1219:     MPI_Status status;
1220:     PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);
1221:     for (i=1; i<size; i++) {
1222:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1223:       MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1224:       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1225:       MPIULong_Recv(column_indices,rnz,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));
1226:       PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);
1227:     }
1228:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1229:   } else {
1230:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1231:     MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1232:     MPIULong_Send(column_indices,nz,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1233:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1234:   }
1235:   PetscFree(column_indices);

1237:   /* load up the local column values */
1238:   PetscMalloc1(nzmax+1,&column_values);
1239:   cnt  = 0;
1240:   for (i=0; i<mat->rmap->n; i++) {
1241:     for (j=B->i[i]; j<B->i[i+1]; j++) {
1242:       if (garray[B->j[j]] > cstart) break;
1243:       column_values[cnt++] = B->a[j];
1244:     }
1245:     for (k=A->i[i]; k<A->i[i+1]; k++) column_values[cnt++] = A->a[k];
1246:     for (; j<B->i[i+1]; j++) column_values[cnt++] = B->a[j];
1247:   }
1248:   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz);

1250:   /* store the column values to the file */
1251:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1252:   if (!rank) {
1253:     MPI_Status status;
1254:     PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);
1255:     for (i=1; i<size; i++) {
1256:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1257:       MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1258:       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1259:       MPIULong_Recv(column_values,rnz,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat));
1260:       PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);
1261:     }
1262:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1263:   } else {
1264:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1265:     MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1266:     MPIULong_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));
1267:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1268:   }
1269:   PetscFree(column_values);

1271:   PetscViewerBinaryGetInfoPointer(viewer,&file);
1272:   if (file) fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(mat->rmap->bs));
1273:   return(0);
1274: }

1276:  #include <petscdraw.h>
1277: PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
1278: {
1279:   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
1280:   PetscErrorCode    ierr;
1281:   PetscMPIInt       rank = aij->rank,size = aij->size;
1282:   PetscBool         isdraw,iascii,isbinary;
1283:   PetscViewer       sviewer;
1284:   PetscViewerFormat format;

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

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

1359:   {
1360:     /* assemble the entire matrix onto first processor. */
1361:     Mat        A;
1362:     Mat_SeqAIJ *Aloc;
1363:     PetscInt   M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct;
1364:     MatScalar  *a;

1366:     MatCreate(PetscObjectComm((PetscObject)mat),&A);
1367:     if (!rank) {
1368:       MatSetSizes(A,M,N,M,N);
1369:     } else {
1370:       MatSetSizes(A,0,0,M,N);
1371:     }
1372:     /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */
1373:     MatSetType(A,MATMPIAIJ);
1374:     MatMPIAIJSetPreallocation(A,0,NULL,0,NULL);
1375:     MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
1376:     PetscLogObjectParent((PetscObject)mat,(PetscObject)A);

1378:     /* copy over the A part */
1379:     Aloc = (Mat_SeqAIJ*)aij->A->data;
1380:     m    = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1381:     row  = mat->rmap->rstart;
1382:     for (i=0; i<ai[m]; i++) aj[i] += mat->cmap->rstart;
1383:     for (i=0; i<m; i++) {
1384:       MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);
1385:       row++;
1386:       a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1387:     }
1388:     aj = Aloc->j;
1389:     for (i=0; i<ai[m]; i++) aj[i] -= mat->cmap->rstart;

1391:     /* copy over the B part */
1392:     Aloc = (Mat_SeqAIJ*)aij->B->data;
1393:     m    = aij->B->rmap->n;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1394:     row  = mat->rmap->rstart;
1395:     PetscMalloc1(ai[m]+1,&cols);
1396:     ct   = cols;
1397:     for (i=0; i<ai[m]; i++) cols[i] = aij->garray[aj[i]];
1398:     for (i=0; i<m; i++) {
1399:       MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);
1400:       row++;
1401:       a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1402:     }
1403:     PetscFree(ct);
1404:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1405:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1406:     /*
1407:        Everyone has to call to draw the matrix since the graphics waits are
1408:        synchronized across all processors that share the PetscDraw object
1409:     */
1410:     PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
1411:     if (!rank) {
1412:       PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,((PetscObject)mat)->name);
1413:       MatView_SeqAIJ(((Mat_MPIAIJ*)(A->data))->A,sviewer);
1414:     }
1415:     PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
1416:     PetscViewerFlush(viewer);
1417:     MatDestroy(&A);
1418:   }
1419:   return(0);
1420: }

1422: PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
1423: {
1425:   PetscBool      iascii,isdraw,issocket,isbinary;

1428:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1429:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1430:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1431:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
1432:   if (iascii || isdraw || isbinary || issocket) {
1433:     MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);
1434:   }
1435:   return(0);
1436: }

1438: PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1439: {
1440:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1442:   Vec            bb1 = 0;
1443:   PetscBool      hasop;

1446:   if (flag == SOR_APPLY_UPPER) {
1447:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1448:     return(0);
1449:   }

1451:   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) {
1452:     VecDuplicate(bb,&bb1);
1453:   }

1455:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
1456:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1457:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1458:       its--;
1459:     }

1461:     while (its--) {
1462:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1463:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

1465:       /* update rhs: bb1 = bb - B*x */
1466:       VecScale(mat->lvec,-1.0);
1467:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1469:       /* local sweep */
1470:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);
1471:     }
1472:   } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
1473:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1474:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1475:       its--;
1476:     }
1477:     while (its--) {
1478:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1479:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

1481:       /* update rhs: bb1 = bb - B*x */
1482:       VecScale(mat->lvec,-1.0);
1483:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1485:       /* local sweep */
1486:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);
1487:     }
1488:   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
1489:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1490:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1491:       its--;
1492:     }
1493:     while (its--) {
1494:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1495:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

1497:       /* update rhs: bb1 = bb - B*x */
1498:       VecScale(mat->lvec,-1.0);
1499:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1501:       /* local sweep */
1502:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);
1503:     }
1504:   } else if (flag & SOR_EISENSTAT) {
1505:     Vec xx1;

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

1510:     VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1511:     VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1512:     if (!mat->diag) {
1513:       MatCreateVecs(matin,&mat->diag,NULL);
1514:       MatGetDiagonal(matin,mat->diag);
1515:     }
1516:     MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);
1517:     if (hasop) {
1518:       MatMultDiagonalBlock(matin,xx,bb1);
1519:     } else {
1520:       VecPointwiseMult(bb1,mat->diag,xx);
1521:     }
1522:     VecAYPX(bb1,(omega-2.0)/omega,bb);

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

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

1532:   VecDestroy(&bb1);

1534:   matin->factorerrortype = mat->A->factorerrortype;
1535:   return(0);
1536: }

1538: PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
1539: {
1540:   Mat            aA,aB,Aperm;
1541:   const PetscInt *rwant,*cwant,*gcols,*ai,*bi,*aj,*bj;
1542:   PetscScalar    *aa,*ba;
1543:   PetscInt       i,j,m,n,ng,anz,bnz,*dnnz,*onnz,*tdnnz,*tonnz,*rdest,*cdest,*work,*gcdest;
1544:   PetscSF        rowsf,sf;
1545:   IS             parcolp = NULL;
1546:   PetscBool      done;

1550:   MatGetLocalSize(A,&m,&n);
1551:   ISGetIndices(rowp,&rwant);
1552:   ISGetIndices(colp,&cwant);
1553:   PetscMalloc3(PetscMax(m,n),&work,m,&rdest,n,&cdest);

1555:   /* Invert row permutation to find out where my rows should go */
1556:   PetscSFCreate(PetscObjectComm((PetscObject)A),&rowsf);
1557:   PetscSFSetGraphLayout(rowsf,A->rmap,A->rmap->n,NULL,PETSC_OWN_POINTER,rwant);
1558:   PetscSFSetFromOptions(rowsf);
1559:   for (i=0; i<m; i++) work[i] = A->rmap->rstart + i;
1560:   PetscSFReduceBegin(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);
1561:   PetscSFReduceEnd(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);

1563:   /* Invert column permutation to find out where my columns should go */
1564:   PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
1565:   PetscSFSetGraphLayout(sf,A->cmap,A->cmap->n,NULL,PETSC_OWN_POINTER,cwant);
1566:   PetscSFSetFromOptions(sf);
1567:   for (i=0; i<n; i++) work[i] = A->cmap->rstart + i;
1568:   PetscSFReduceBegin(sf,MPIU_INT,work,cdest,MPIU_REPLACE);
1569:   PetscSFReduceEnd(sf,MPIU_INT,work,cdest,MPIU_REPLACE);
1570:   PetscSFDestroy(&sf);

1572:   ISRestoreIndices(rowp,&rwant);
1573:   ISRestoreIndices(colp,&cwant);
1574:   MatMPIAIJGetSeqAIJ(A,&aA,&aB,&gcols);

1576:   /* Find out where my gcols should go */
1577:   MatGetSize(aB,NULL,&ng);
1578:   PetscMalloc1(ng,&gcdest);
1579:   PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
1580:   PetscSFSetGraphLayout(sf,A->cmap,ng,NULL,PETSC_OWN_POINTER,gcols);
1581:   PetscSFSetFromOptions(sf);
1582:   PetscSFBcastBegin(sf,MPIU_INT,cdest,gcdest);
1583:   PetscSFBcastEnd(sf,MPIU_INT,cdest,gcdest);
1584:   PetscSFDestroy(&sf);

1586:   PetscCalloc4(m,&dnnz,m,&onnz,m,&tdnnz,m,&tonnz);
1587:   MatGetRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);
1588:   MatGetRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);
1589:   for (i=0; i<m; i++) {
1590:     PetscInt row = rdest[i],rowner;
1591:     PetscLayoutFindOwner(A->rmap,row,&rowner);
1592:     for (j=ai[i]; j<ai[i+1]; j++) {
1593:       PetscInt cowner,col = cdest[aj[j]];
1594:       PetscLayoutFindOwner(A->cmap,col,&cowner); /* Could build an index for the columns to eliminate this search */
1595:       if (rowner == cowner) dnnz[i]++;
1596:       else onnz[i]++;
1597:     }
1598:     for (j=bi[i]; j<bi[i+1]; j++) {
1599:       PetscInt cowner,col = gcdest[bj[j]];
1600:       PetscLayoutFindOwner(A->cmap,col,&cowner);
1601:       if (rowner == cowner) dnnz[i]++;
1602:       else onnz[i]++;
1603:     }
1604:   }
1605:   PetscSFBcastBegin(rowsf,MPIU_INT,dnnz,tdnnz);
1606:   PetscSFBcastEnd(rowsf,MPIU_INT,dnnz,tdnnz);
1607:   PetscSFBcastBegin(rowsf,MPIU_INT,onnz,tonnz);
1608:   PetscSFBcastEnd(rowsf,MPIU_INT,onnz,tonnz);
1609:   PetscSFDestroy(&rowsf);

1611:   MatCreateAIJ(PetscObjectComm((PetscObject)A),A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N,0,tdnnz,0,tonnz,&Aperm);
1612:   MatSeqAIJGetArray(aA,&aa);
1613:   MatSeqAIJGetArray(aB,&ba);
1614:   for (i=0; i<m; i++) {
1615:     PetscInt *acols = dnnz,*bcols = onnz; /* Repurpose now-unneeded arrays */
1616:     PetscInt j0,rowlen;
1617:     rowlen = ai[i+1] - ai[i];
1618:     for (j0=j=0; j<rowlen; j0=j) { /* rowlen could be larger than number of rows m, so sum in batches */
1619:       for ( ; j<PetscMin(rowlen,j0+m); j++) acols[j-j0] = cdest[aj[ai[i]+j]];
1620:       MatSetValues(Aperm,1,&rdest[i],j-j0,acols,aa+ai[i]+j0,INSERT_VALUES);
1621:     }
1622:     rowlen = bi[i+1] - bi[i];
1623:     for (j0=j=0; j<rowlen; j0=j) {
1624:       for ( ; j<PetscMin(rowlen,j0+m); j++) bcols[j-j0] = gcdest[bj[bi[i]+j]];
1625:       MatSetValues(Aperm,1,&rdest[i],j-j0,bcols,ba+bi[i]+j0,INSERT_VALUES);
1626:     }
1627:   }
1628:   MatAssemblyBegin(Aperm,MAT_FINAL_ASSEMBLY);
1629:   MatAssemblyEnd(Aperm,MAT_FINAL_ASSEMBLY);
1630:   MatRestoreRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);
1631:   MatRestoreRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);
1632:   MatSeqAIJRestoreArray(aA,&aa);
1633:   MatSeqAIJRestoreArray(aB,&ba);
1634:   PetscFree4(dnnz,onnz,tdnnz,tonnz);
1635:   PetscFree3(work,rdest,cdest);
1636:   PetscFree(gcdest);
1637:   if (parcolp) {ISDestroy(&colp);}
1638:   *B = Aperm;
1639:   return(0);
1640: }

1642: PetscErrorCode  MatGetGhosts_MPIAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
1643: {
1644:   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;

1648:   MatGetSize(aij->B,NULL,nghosts);
1649:   if (ghosts) *ghosts = aij->garray;
1650:   return(0);
1651: }

1653: PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1654: {
1655:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1656:   Mat            A    = mat->A,B = mat->B;
1658:   PetscReal      isend[5],irecv[5];

1661:   info->block_size = 1.0;
1662:   MatGetInfo(A,MAT_LOCAL,info);

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

1667:   MatGetInfo(B,MAT_LOCAL,info);

1669:   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1670:   isend[3] += info->memory;  isend[4] += info->mallocs;
1671:   if (flag == MAT_LOCAL) {
1672:     info->nz_used      = isend[0];
1673:     info->nz_allocated = isend[1];
1674:     info->nz_unneeded  = isend[2];
1675:     info->memory       = isend[3];
1676:     info->mallocs      = isend[4];
1677:   } else if (flag == MAT_GLOBAL_MAX) {
1678:     MPIU_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));

1680:     info->nz_used      = irecv[0];
1681:     info->nz_allocated = irecv[1];
1682:     info->nz_unneeded  = irecv[2];
1683:     info->memory       = irecv[3];
1684:     info->mallocs      = irecv[4];
1685:   } else if (flag == MAT_GLOBAL_SUM) {
1686:     MPIU_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));

1688:     info->nz_used      = irecv[0];
1689:     info->nz_allocated = irecv[1];
1690:     info->nz_unneeded  = irecv[2];
1691:     info->memory       = irecv[3];
1692:     info->mallocs      = irecv[4];
1693:   }
1694:   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
1695:   info->fill_ratio_needed = 0;
1696:   info->factor_mallocs    = 0;
1697:   return(0);
1698: }

1700: PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg)
1701: {
1702:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1706:   switch (op) {
1707:   case MAT_NEW_NONZERO_LOCATIONS:
1708:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1709:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1710:   case MAT_KEEP_NONZERO_PATTERN:
1711:   case MAT_NEW_NONZERO_LOCATION_ERR:
1712:   case MAT_USE_INODES:
1713:   case MAT_IGNORE_ZERO_ENTRIES:
1714:     MatCheckPreallocated(A,1);
1715:     MatSetOption(a->A,op,flg);
1716:     MatSetOption(a->B,op,flg);
1717:     break;
1718:   case MAT_ROW_ORIENTED:
1719:     MatCheckPreallocated(A,1);
1720:     a->roworiented = flg;

1722:     MatSetOption(a->A,op,flg);
1723:     MatSetOption(a->B,op,flg);
1724:     break;
1725:   case MAT_NEW_DIAGONALS:
1726:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1727:     break;
1728:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1729:     a->donotstash = flg;
1730:     break;
1731:   case MAT_SPD:
1732:     A->spd_set = PETSC_TRUE;
1733:     A->spd     = flg;
1734:     if (flg) {
1735:       A->symmetric                  = PETSC_TRUE;
1736:       A->structurally_symmetric     = PETSC_TRUE;
1737:       A->symmetric_set              = PETSC_TRUE;
1738:       A->structurally_symmetric_set = PETSC_TRUE;
1739:     }
1740:     break;
1741:   case MAT_SYMMETRIC:
1742:     MatCheckPreallocated(A,1);
1743:     MatSetOption(a->A,op,flg);
1744:     break;
1745:   case MAT_STRUCTURALLY_SYMMETRIC:
1746:     MatCheckPreallocated(A,1);
1747:     MatSetOption(a->A,op,flg);
1748:     break;
1749:   case MAT_HERMITIAN:
1750:     MatCheckPreallocated(A,1);
1751:     MatSetOption(a->A,op,flg);
1752:     break;
1753:   case MAT_SYMMETRY_ETERNAL:
1754:     MatCheckPreallocated(A,1);
1755:     MatSetOption(a->A,op,flg);
1756:     break;
1757:   case MAT_SUBMAT_SINGLEIS:
1758:     A->submat_singleis = flg;
1759:     break;
1760:   case MAT_STRUCTURE_ONLY:
1761:     /* The option is handled directly by MatSetOption() */
1762:     break;
1763:   default:
1764:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1765:   }
1766:   return(0);
1767: }

1769: PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1770: {
1771:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1772:   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
1774:   PetscInt       i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart;
1775:   PetscInt       nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend;
1776:   PetscInt       *cmap,*idx_p;

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

1782:   if (!mat->rowvalues && (idx || v)) {
1783:     /*
1784:         allocate enough space to hold information from the longest row.
1785:     */
1786:     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1787:     PetscInt   max = 1,tmp;
1788:     for (i=0; i<matin->rmap->n; i++) {
1789:       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1790:       if (max < tmp) max = tmp;
1791:     }
1792:     PetscMalloc2(max,&mat->rowvalues,max,&mat->rowindices);
1793:   }

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

1798:   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1799:   if (!v)   {pvA = 0; pvB = 0;}
1800:   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1801:   (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1802:   (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1803:   nztot = nzA + nzB;

1805:   cmap = mat->garray;
1806:   if (v  || idx) {
1807:     if (nztot) {
1808:       /* Sort by increasing column numbers, assuming A and B already sorted */
1809:       PetscInt imark = -1;
1810:       if (v) {
1811:         *v = v_p = mat->rowvalues;
1812:         for (i=0; i<nzB; i++) {
1813:           if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i];
1814:           else break;
1815:         }
1816:         imark = i;
1817:         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1818:         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1819:       }
1820:       if (idx) {
1821:         *idx = idx_p = mat->rowindices;
1822:         if (imark > -1) {
1823:           for (i=0; i<imark; i++) {
1824:             idx_p[i] = cmap[cworkB[i]];
1825:           }
1826:         } else {
1827:           for (i=0; i<nzB; i++) {
1828:             if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]];
1829:             else break;
1830:           }
1831:           imark = i;
1832:         }
1833:         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
1834:         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
1835:       }
1836:     } else {
1837:       if (idx) *idx = 0;
1838:       if (v)   *v   = 0;
1839:     }
1840:   }
1841:   *nz  = nztot;
1842:   (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1843:   (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1844:   return(0);
1845: }

1847: PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1848: {
1849:   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;

1852:   if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
1853:   aij->getrowactive = PETSC_FALSE;
1854:   return(0);
1855: }

1857: PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1858: {
1859:   Mat_MPIAIJ     *aij  = (Mat_MPIAIJ*)mat->data;
1860:   Mat_SeqAIJ     *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1862:   PetscInt       i,j,cstart = mat->cmap->rstart;
1863:   PetscReal      sum = 0.0;
1864:   MatScalar      *v;

1867:   if (aij->size == 1) {
1868:      MatNorm(aij->A,type,norm);
1869:   } else {
1870:     if (type == NORM_FROBENIUS) {
1871:       v = amat->a;
1872:       for (i=0; i<amat->nz; i++) {
1873:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1874:       }
1875:       v = bmat->a;
1876:       for (i=0; i<bmat->nz; i++) {
1877:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1878:       }
1879:       MPIU_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
1880:       *norm = PetscSqrtReal(*norm);
1881:       PetscLogFlops(2*amat->nz+2*bmat->nz);
1882:     } else if (type == NORM_1) { /* max column norm */
1883:       PetscReal *tmp,*tmp2;
1884:       PetscInt  *jj,*garray = aij->garray;
1885:       PetscCalloc1(mat->cmap->N+1,&tmp);
1886:       PetscMalloc1(mat->cmap->N+1,&tmp2);
1887:       *norm = 0.0;
1888:       v     = amat->a; jj = amat->j;
1889:       for (j=0; j<amat->nz; j++) {
1890:         tmp[cstart + *jj++] += PetscAbsScalar(*v);  v++;
1891:       }
1892:       v = bmat->a; jj = bmat->j;
1893:       for (j=0; j<bmat->nz; j++) {
1894:         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
1895:       }
1896:       MPIU_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
1897:       for (j=0; j<mat->cmap->N; j++) {
1898:         if (tmp2[j] > *norm) *norm = tmp2[j];
1899:       }
1900:       PetscFree(tmp);
1901:       PetscFree(tmp2);
1902:       PetscLogFlops(PetscMax(amat->nz+bmat->nz-1,0));
1903:     } else if (type == NORM_INFINITY) { /* max row norm */
1904:       PetscReal ntemp = 0.0;
1905:       for (j=0; j<aij->A->rmap->n; j++) {
1906:         v   = amat->a + amat->i[j];
1907:         sum = 0.0;
1908:         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1909:           sum += PetscAbsScalar(*v); v++;
1910:         }
1911:         v = bmat->a + bmat->i[j];
1912:         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1913:           sum += PetscAbsScalar(*v); v++;
1914:         }
1915:         if (sum > ntemp) ntemp = sum;
1916:       }
1917:       MPIU_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));
1918:       PetscLogFlops(PetscMax(amat->nz+bmat->nz-1,0));
1919:     } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for two norm");
1920:   }
1921:   return(0);
1922: }

1924: PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout)
1925: {
1926:   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;
1927:   Mat_SeqAIJ     *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data;
1929:   PetscInt       M      = A->rmap->N,N = A->cmap->N,ma,na,mb,nb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i;
1930:   PetscInt       cstart = A->cmap->rstart,ncol;
1931:   Mat            B;
1932:   MatScalar      *array;

1935:   if (reuse == MAT_INPLACE_MATRIX && M != N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");

1937:   ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; nb = a->B->cmap->n;
1938:   ai = Aloc->i; aj = Aloc->j;
1939:   bi = Bloc->i; bj = Bloc->j;
1940:   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
1941:     PetscInt             *d_nnz,*g_nnz,*o_nnz;
1942:     PetscSFNode          *oloc;
1943:     PETSC_UNUSED PetscSF sf;

1945:     PetscMalloc4(na,&d_nnz,na,&o_nnz,nb,&g_nnz,nb,&oloc);
1946:     /* compute d_nnz for preallocation */
1947:     PetscMemzero(d_nnz,na*sizeof(PetscInt));
1948:     for (i=0; i<ai[ma]; i++) {
1949:       d_nnz[aj[i]]++;
1950:       aj[i] += cstart; /* global col index to be used by MatSetValues() */
1951:     }
1952:     /* compute local off-diagonal contributions */
1953:     PetscMemzero(g_nnz,nb*sizeof(PetscInt));
1954:     for (i=0; i<bi[ma]; i++) g_nnz[bj[i]]++;
1955:     /* map those to global */
1956:     PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
1957:     PetscSFSetGraphLayout(sf,A->cmap,nb,NULL,PETSC_USE_POINTER,a->garray);
1958:     PetscSFSetFromOptions(sf);
1959:     PetscMemzero(o_nnz,na*sizeof(PetscInt));
1960:     PetscSFReduceBegin(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);
1961:     PetscSFReduceEnd(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);
1962:     PetscSFDestroy(&sf);

1964:     MatCreate(PetscObjectComm((PetscObject)A),&B);
1965:     MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);
1966:     MatSetBlockSizes(B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
1967:     MatSetType(B,((PetscObject)A)->type_name);
1968:     MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);
1969:     PetscFree4(d_nnz,o_nnz,g_nnz,oloc);
1970:   } else {
1971:     B    = *matout;
1972:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
1973:     for (i=0; i<ai[ma]; i++) aj[i] += cstart; /* global col index to be used by MatSetValues() */
1974:   }

1976:   /* copy over the A part */
1977:   array = Aloc->a;
1978:   row   = A->rmap->rstart;
1979:   for (i=0; i<ma; i++) {
1980:     ncol = ai[i+1]-ai[i];
1981:     MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);
1982:     row++;
1983:     array += ncol; aj += ncol;
1984:   }
1985:   aj = Aloc->j;
1986:   for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */

1988:   /* copy over the B part */
1989:   PetscCalloc1(bi[mb],&cols);
1990:   array = Bloc->a;
1991:   row   = A->rmap->rstart;
1992:   for (i=0; i<bi[mb]; i++) cols[i] = a->garray[bj[i]];
1993:   cols_tmp = cols;
1994:   for (i=0; i<mb; i++) {
1995:     ncol = bi[i+1]-bi[i];
1996:     MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);
1997:     row++;
1998:     array += ncol; cols_tmp += ncol;
1999:   }
2000:   PetscFree(cols);

2002:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2003:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2004:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
2005:     *matout = B;
2006:   } else {
2007:     MatHeaderMerge(A,&B);
2008:   }
2009:   return(0);
2010: }

2012: PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
2013: {
2014:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
2015:   Mat            a    = aij->A,b = aij->B;
2017:   PetscInt       s1,s2,s3;

2020:   MatGetLocalSize(mat,&s2,&s3);
2021:   if (rr) {
2022:     VecGetLocalSize(rr,&s1);
2023:     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
2024:     /* Overlap communication with computation. */
2025:     VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);
2026:   }
2027:   if (ll) {
2028:     VecGetLocalSize(ll,&s1);
2029:     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
2030:     (*b->ops->diagonalscale)(b,ll,0);
2031:   }
2032:   /* scale  the diagonal block */
2033:   (*a->ops->diagonalscale)(a,ll,rr);

2035:   if (rr) {
2036:     /* Do a scatter end and then right scale the off-diagonal block */
2037:     VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);
2038:     (*b->ops->diagonalscale)(b,0,aij->lvec);
2039:   }
2040:   return(0);
2041: }

2043: PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
2044: {
2045:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2049:   MatSetUnfactored(a->A);
2050:   return(0);
2051: }

2053: PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool  *flag)
2054: {
2055:   Mat_MPIAIJ     *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
2056:   Mat            a,b,c,d;
2057:   PetscBool      flg;

2061:   a = matA->A; b = matA->B;
2062:   c = matB->A; d = matB->B;

2064:   MatEqual(a,c,&flg);
2065:   if (flg) {
2066:     MatEqual(b,d,&flg);
2067:   }
2068:   MPIU_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
2069:   return(0);
2070: }

2072: PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
2073: {
2075:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2076:   Mat_MPIAIJ     *b = (Mat_MPIAIJ*)B->data;

2079:   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
2080:   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
2081:     /* because of the column compression in the off-processor part of the matrix a->B,
2082:        the number of columns in a->B and b->B may be different, hence we cannot call
2083:        the MatCopy() directly on the two parts. If need be, we can provide a more
2084:        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
2085:        then copying the submatrices */
2086:     MatCopy_Basic(A,B,str);
2087:   } else {
2088:     MatCopy(a->A,b->A,str);
2089:     MatCopy(a->B,b->B,str);
2090:   }
2091:   PetscObjectStateIncrease((PetscObject)B);
2092:   return(0);
2093: }

2095: PetscErrorCode MatSetUp_MPIAIJ(Mat A)
2096: {

2100:    MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
2101:   return(0);
2102: }

2104: /*
2105:    Computes the number of nonzeros per row needed for preallocation when X and Y
2106:    have different nonzero structure.
2107: */
2108: 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)
2109: {
2110:   PetscInt       i,j,k,nzx,nzy;

2113:   /* Set the number of nonzeros in the new matrix */
2114:   for (i=0; i<m; i++) {
2115:     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2116:     nzx = xi[i+1] - xi[i];
2117:     nzy = yi[i+1] - yi[i];
2118:     nnz[i] = 0;
2119:     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2120:       for (; k<nzy && yltog[yjj[k]]<xltog[xjj[j]]; k++) nnz[i]++; /* Catch up to X */
2121:       if (k<nzy && yltog[yjj[k]]==xltog[xjj[j]]) k++;             /* Skip duplicate */
2122:       nnz[i]++;
2123:     }
2124:     for (; k<nzy; k++) nnz[i]++;
2125:   }
2126:   return(0);
2127: }

2129: /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */
2130: static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz)
2131: {
2133:   PetscInt       m = Y->rmap->N;
2134:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2135:   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;

2138:   MatAXPYGetPreallocation_MPIX_private(m,x->i,x->j,xltog,y->i,y->j,yltog,nnz);
2139:   return(0);
2140: }

2142: PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2143: {
2145:   Mat_MPIAIJ     *xx = (Mat_MPIAIJ*)X->data,*yy = (Mat_MPIAIJ*)Y->data;
2146:   PetscBLASInt   bnz,one=1;
2147:   Mat_SeqAIJ     *x,*y;

2150:   if (str == SAME_NONZERO_PATTERN) {
2151:     PetscScalar alpha = a;
2152:     x    = (Mat_SeqAIJ*)xx->A->data;
2153:     PetscBLASIntCast(x->nz,&bnz);
2154:     y    = (Mat_SeqAIJ*)yy->A->data;
2155:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2156:     x    = (Mat_SeqAIJ*)xx->B->data;
2157:     y    = (Mat_SeqAIJ*)yy->B->data;
2158:     PetscBLASIntCast(x->nz,&bnz);
2159:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2160:     PetscObjectStateIncrease((PetscObject)Y);
2161:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2162:     MatAXPY_Basic(Y,a,X,str);
2163:   } else {
2164:     Mat      B;
2165:     PetscInt *nnz_d,*nnz_o;
2166:     PetscMalloc1(yy->A->rmap->N,&nnz_d);
2167:     PetscMalloc1(yy->B->rmap->N,&nnz_o);
2168:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
2169:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2170:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2171:     MatSetBlockSizesFromMats(B,Y,Y);
2172:     MatSetType(B,MATMPIAIJ);
2173:     MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);
2174:     MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);
2175:     MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);
2176:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2177:     MatHeaderReplace(Y,&B);
2178:     PetscFree(nnz_d);
2179:     PetscFree(nnz_o);
2180:   }
2181:   return(0);
2182: }

2184: extern PetscErrorCode  MatConjugate_SeqAIJ(Mat);

2186: PetscErrorCode  MatConjugate_MPIAIJ(Mat mat)
2187: {
2188: #if defined(PETSC_USE_COMPLEX)
2190:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

2193:   MatConjugate_SeqAIJ(aij->A);
2194:   MatConjugate_SeqAIJ(aij->B);
2195: #else
2197: #endif
2198:   return(0);
2199: }

2201: PetscErrorCode MatRealPart_MPIAIJ(Mat A)
2202: {
2203:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2207:   MatRealPart(a->A);
2208:   MatRealPart(a->B);
2209:   return(0);
2210: }

2212: PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
2213: {
2214:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2218:   MatImaginaryPart(a->A);
2219:   MatImaginaryPart(a->B);
2220:   return(0);
2221: }

2223: PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2224: {
2225:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2227:   PetscInt       i,*idxb = 0;
2228:   PetscScalar    *va,*vb;
2229:   Vec            vtmp;

2232:   MatGetRowMaxAbs(a->A,v,idx);
2233:   VecGetArray(v,&va);
2234:   if (idx) {
2235:     for (i=0; i<A->rmap->n; i++) {
2236:       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2237:     }
2238:   }

2240:   VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);
2241:   if (idx) {
2242:     PetscMalloc1(A->rmap->n,&idxb);
2243:   }
2244:   MatGetRowMaxAbs(a->B,vtmp,idxb);
2245:   VecGetArray(vtmp,&vb);

2247:   for (i=0; i<A->rmap->n; i++) {
2248:     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
2249:       va[i] = vb[i];
2250:       if (idx) idx[i] = a->garray[idxb[i]];
2251:     }
2252:   }

2254:   VecRestoreArray(v,&va);
2255:   VecRestoreArray(vtmp,&vb);
2256:   PetscFree(idxb);
2257:   VecDestroy(&vtmp);
2258:   return(0);
2259: }

2261: PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2262: {
2263:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2265:   PetscInt       i,*idxb = 0;
2266:   PetscScalar    *va,*vb;
2267:   Vec            vtmp;

2270:   MatGetRowMinAbs(a->A,v,idx);
2271:   VecGetArray(v,&va);
2272:   if (idx) {
2273:     for (i=0; i<A->cmap->n; i++) {
2274:       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2275:     }
2276:   }

2278:   VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);
2279:   if (idx) {
2280:     PetscMalloc1(A->rmap->n,&idxb);
2281:   }
2282:   MatGetRowMinAbs(a->B,vtmp,idxb);
2283:   VecGetArray(vtmp,&vb);

2285:   for (i=0; i<A->rmap->n; i++) {
2286:     if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) {
2287:       va[i] = vb[i];
2288:       if (idx) idx[i] = a->garray[idxb[i]];
2289:     }
2290:   }

2292:   VecRestoreArray(v,&va);
2293:   VecRestoreArray(vtmp,&vb);
2294:   PetscFree(idxb);
2295:   VecDestroy(&vtmp);
2296:   return(0);
2297: }

2299: PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2300: {
2301:   Mat_MPIAIJ     *mat   = (Mat_MPIAIJ*) A->data;
2302:   PetscInt       n      = A->rmap->n;
2303:   PetscInt       cstart = A->cmap->rstart;
2304:   PetscInt       *cmap  = mat->garray;
2305:   PetscInt       *diagIdx, *offdiagIdx;
2306:   Vec            diagV, offdiagV;
2307:   PetscScalar    *a, *diagA, *offdiagA;
2308:   PetscInt       r;

2312:   PetscMalloc2(n,&diagIdx,n,&offdiagIdx);
2313:   VecCreateSeq(PetscObjectComm((PetscObject)A), n, &diagV);
2314:   VecCreateSeq(PetscObjectComm((PetscObject)A), n, &offdiagV);
2315:   MatGetRowMin(mat->A, diagV,    diagIdx);
2316:   MatGetRowMin(mat->B, offdiagV, offdiagIdx);
2317:   VecGetArray(v,        &a);
2318:   VecGetArray(diagV,    &diagA);
2319:   VecGetArray(offdiagV, &offdiagA);
2320:   for (r = 0; r < n; ++r) {
2321:     if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) {
2322:       a[r]   = diagA[r];
2323:       idx[r] = cstart + diagIdx[r];
2324:     } else {
2325:       a[r]   = offdiagA[r];
2326:       idx[r] = cmap[offdiagIdx[r]];
2327:     }
2328:   }
2329:   VecRestoreArray(v,        &a);
2330:   VecRestoreArray(diagV,    &diagA);
2331:   VecRestoreArray(offdiagV, &offdiagA);
2332:   VecDestroy(&diagV);
2333:   VecDestroy(&offdiagV);
2334:   PetscFree2(diagIdx, offdiagIdx);
2335:   return(0);
2336: }

2338: PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2339: {
2340:   Mat_MPIAIJ     *mat   = (Mat_MPIAIJ*) A->data;
2341:   PetscInt       n      = A->rmap->n;
2342:   PetscInt       cstart = A->cmap->rstart;
2343:   PetscInt       *cmap  = mat->garray;
2344:   PetscInt       *diagIdx, *offdiagIdx;
2345:   Vec            diagV, offdiagV;
2346:   PetscScalar    *a, *diagA, *offdiagA;
2347:   PetscInt       r;

2351:   PetscMalloc2(n,&diagIdx,n,&offdiagIdx);
2352:   VecCreateSeq(PETSC_COMM_SELF, n, &diagV);
2353:   VecCreateSeq(PETSC_COMM_SELF, n, &offdiagV);
2354:   MatGetRowMax(mat->A, diagV,    diagIdx);
2355:   MatGetRowMax(mat->B, offdiagV, offdiagIdx);
2356:   VecGetArray(v,        &a);
2357:   VecGetArray(diagV,    &diagA);
2358:   VecGetArray(offdiagV, &offdiagA);
2359:   for (r = 0; r < n; ++r) {
2360:     if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) {
2361:       a[r]   = diagA[r];
2362:       idx[r] = cstart + diagIdx[r];
2363:     } else {
2364:       a[r]   = offdiagA[r];
2365:       idx[r] = cmap[offdiagIdx[r]];
2366:     }
2367:   }
2368:   VecRestoreArray(v,        &a);
2369:   VecRestoreArray(diagV,    &diagA);
2370:   VecRestoreArray(offdiagV, &offdiagA);
2371:   VecDestroy(&diagV);
2372:   VecDestroy(&offdiagV);
2373:   PetscFree2(diagIdx, offdiagIdx);
2374:   return(0);
2375: }

2377: PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat)
2378: {
2380:   Mat            *dummy;

2383:   MatCreateSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);
2384:   *newmat = *dummy;
2385:   PetscFree(dummy);
2386:   return(0);
2387: }

2389: PetscErrorCode  MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values)
2390: {
2391:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*) A->data;

2395:   MatInvertBlockDiagonal(a->A,values);
2396:   A->factorerrortype = a->A->factorerrortype;
2397:   return(0);
2398: }

2400: static PetscErrorCode  MatSetRandom_MPIAIJ(Mat x,PetscRandom rctx)
2401: {
2403:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)x->data;

2406:   MatSetRandom(aij->A,rctx);
2407:   MatSetRandom(aij->B,rctx);
2408:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
2409:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
2410:   return(0);
2411: }

2413: PetscErrorCode MatMPIAIJSetUseScalableIncreaseOverlap_MPIAIJ(Mat A,PetscBool sc)
2414: {
2416:   if (sc) A->ops->increaseoverlap = MatIncreaseOverlap_MPIAIJ_Scalable;
2417:   else A->ops->increaseoverlap    = MatIncreaseOverlap_MPIAIJ;
2418:   return(0);
2419: }

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

2424:    Collective on Mat

2426:    Input Parameters:
2427: +    A - the matrix
2428: -    sc - PETSC_TRUE indicates use the scalable algorithm (default is not to use the scalable algorithm)

2430:  Level: advanced

2432: @*/
2433: PetscErrorCode MatMPIAIJSetUseScalableIncreaseOverlap(Mat A,PetscBool sc)
2434: {
2435:   PetscErrorCode       ierr;

2438:   PetscTryMethod(A,"MatMPIAIJSetUseScalableIncreaseOverlap_C",(Mat,PetscBool),(A,sc));
2439:   return(0);
2440: }

2442: PetscErrorCode MatSetFromOptions_MPIAIJ(PetscOptionItems *PetscOptionsObject,Mat A)
2443: {
2444:   PetscErrorCode       ierr;
2445:   PetscBool            sc = PETSC_FALSE,flg;

2448:   PetscOptionsHead(PetscOptionsObject,"MPIAIJ options");
2449:   PetscObjectOptionsBegin((PetscObject)A);
2450:     if (A->ops->increaseoverlap == MatIncreaseOverlap_MPIAIJ_Scalable) sc = PETSC_TRUE;
2451:     PetscOptionsBool("-mat_increase_overlap_scalable","Use a scalable algorithm to compute the overlap","MatIncreaseOverlap",sc,&sc,&flg);
2452:     if (flg) {
2453:       MatMPIAIJSetUseScalableIncreaseOverlap(A,sc);
2454:     }
2455:   PetscOptionsEnd();
2456:   return(0);
2457: }

2459: PetscErrorCode MatShift_MPIAIJ(Mat Y,PetscScalar a)
2460: {
2462:   Mat_MPIAIJ     *maij = (Mat_MPIAIJ*)Y->data;
2463:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)maij->A->data;

2466:   if (!Y->preallocated) {
2467:     MatMPIAIJSetPreallocation(Y,1,NULL,0,NULL);
2468:   } else if (!aij->nz) {
2469:     PetscInt nonew = aij->nonew;
2470:     MatSeqAIJSetPreallocation(maij->A,1,NULL);
2471:     aij->nonew = nonew;
2472:   }
2473:   MatShift_Basic(Y,a);
2474:   return(0);
2475: }

2477: PetscErrorCode MatMissingDiagonal_MPIAIJ(Mat A,PetscBool  *missing,PetscInt *d)
2478: {
2479:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2483:   if (A->rmap->n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only works for square matrices");
2484:   MatMissingDiagonal(a->A,missing,d);
2485:   if (d) {
2486:     PetscInt rstart;
2487:     MatGetOwnershipRange(A,&rstart,NULL);
2488:     *d += rstart;

2490:   }
2491:   return(0);
2492: }


2495: /* -------------------------------------------------------------------*/
2496: static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
2497:                                        MatGetRow_MPIAIJ,
2498:                                        MatRestoreRow_MPIAIJ,
2499:                                        MatMult_MPIAIJ,
2500:                                 /* 4*/ MatMultAdd_MPIAIJ,
2501:                                        MatMultTranspose_MPIAIJ,
2502:                                        MatMultTransposeAdd_MPIAIJ,
2503:                                        0,
2504:                                        0,
2505:                                        0,
2506:                                 /*10*/ 0,
2507:                                        0,
2508:                                        0,
2509:                                        MatSOR_MPIAIJ,
2510:                                        MatTranspose_MPIAIJ,
2511:                                 /*15*/ MatGetInfo_MPIAIJ,
2512:                                        MatEqual_MPIAIJ,
2513:                                        MatGetDiagonal_MPIAIJ,
2514:                                        MatDiagonalScale_MPIAIJ,
2515:                                        MatNorm_MPIAIJ,
2516:                                 /*20*/ MatAssemblyBegin_MPIAIJ,
2517:                                        MatAssemblyEnd_MPIAIJ,
2518:                                        MatSetOption_MPIAIJ,
2519:                                        MatZeroEntries_MPIAIJ,
2520:                                 /*24*/ MatZeroRows_MPIAIJ,
2521:                                        0,
2522:                                        0,
2523:                                        0,
2524:                                        0,
2525:                                 /*29*/ MatSetUp_MPIAIJ,
2526:                                        0,
2527:                                        0,
2528:                                        MatGetDiagonalBlock_MPIAIJ,
2529:                                        0,
2530:                                 /*34*/ MatDuplicate_MPIAIJ,
2531:                                        0,
2532:                                        0,
2533:                                        0,
2534:                                        0,
2535:                                 /*39*/ MatAXPY_MPIAIJ,
2536:                                        MatCreateSubMatrices_MPIAIJ,
2537:                                        MatIncreaseOverlap_MPIAIJ,
2538:                                        MatGetValues_MPIAIJ,
2539:                                        MatCopy_MPIAIJ,
2540:                                 /*44*/ MatGetRowMax_MPIAIJ,
2541:                                        MatScale_MPIAIJ,
2542:                                        MatShift_MPIAIJ,
2543:                                        MatDiagonalSet_MPIAIJ,
2544:                                        MatZeroRowsColumns_MPIAIJ,
2545:                                 /*49*/ MatSetRandom_MPIAIJ,
2546:                                        0,
2547:                                        0,
2548:                                        0,
2549:                                        0,
2550:                                 /*54*/ MatFDColoringCreate_MPIXAIJ,
2551:                                        0,
2552:                                        MatSetUnfactored_MPIAIJ,
2553:                                        MatPermute_MPIAIJ,
2554:                                        0,
2555:                                 /*59*/ MatCreateSubMatrix_MPIAIJ,
2556:                                        MatDestroy_MPIAIJ,
2557:                                        MatView_MPIAIJ,
2558:                                        0,
2559:                                        MatMatMatMult_MPIAIJ_MPIAIJ_MPIAIJ,
2560:                                 /*64*/ MatMatMatMultSymbolic_MPIAIJ_MPIAIJ_MPIAIJ,
2561:                                        MatMatMatMultNumeric_MPIAIJ_MPIAIJ_MPIAIJ,
2562:                                        0,
2563:                                        0,
2564:                                        0,
2565:                                 /*69*/ MatGetRowMaxAbs_MPIAIJ,
2566:                                        MatGetRowMinAbs_MPIAIJ,
2567:                                        0,
2568:                                        0,
2569:                                        0,
2570:                                        0,
2571:                                 /*75*/ MatFDColoringApply_AIJ,
2572:                                        MatSetFromOptions_MPIAIJ,
2573:                                        0,
2574:                                        0,
2575:                                        MatFindZeroDiagonals_MPIAIJ,
2576:                                 /*80*/ 0,
2577:                                        0,
2578:                                        0,
2579:                                 /*83*/ MatLoad_MPIAIJ,
2580:                                        MatIsSymmetric_MPIAIJ,
2581:                                        0,
2582:                                        0,
2583:                                        0,
2584:                                        0,
2585:                                 /*89*/ MatMatMult_MPIAIJ_MPIAIJ,
2586:                                        MatMatMultSymbolic_MPIAIJ_MPIAIJ,
2587:                                        MatMatMultNumeric_MPIAIJ_MPIAIJ,
2588:                                        MatPtAP_MPIAIJ_MPIAIJ,
2589:                                        MatPtAPSymbolic_MPIAIJ_MPIAIJ,
2590:                                 /*94*/ MatPtAPNumeric_MPIAIJ_MPIAIJ,
2591:                                        0,
2592:                                        0,
2593:                                        0,
2594:                                        0,
2595:                                 /*99*/ 0,
2596:                                        0,
2597:                                        0,
2598:                                        MatConjugate_MPIAIJ,
2599:                                        0,
2600:                                 /*104*/MatSetValuesRow_MPIAIJ,
2601:                                        MatRealPart_MPIAIJ,
2602:                                        MatImaginaryPart_MPIAIJ,
2603:                                        0,
2604:                                        0,
2605:                                 /*109*/0,
2606:                                        0,
2607:                                        MatGetRowMin_MPIAIJ,
2608:                                        0,
2609:                                        MatMissingDiagonal_MPIAIJ,
2610:                                 /*114*/MatGetSeqNonzeroStructure_MPIAIJ,
2611:                                        0,
2612:                                        MatGetGhosts_MPIAIJ,
2613:                                        0,
2614:                                        0,
2615:                                 /*119*/0,
2616:                                        0,
2617:                                        0,
2618:                                        0,
2619:                                        MatGetMultiProcBlock_MPIAIJ,
2620:                                 /*124*/MatFindNonzeroRows_MPIAIJ,
2621:                                        MatGetColumnNorms_MPIAIJ,
2622:                                        MatInvertBlockDiagonal_MPIAIJ,
2623:                                        0,
2624:                                        MatCreateSubMatricesMPI_MPIAIJ,
2625:                                 /*129*/0,
2626:                                        MatTransposeMatMult_MPIAIJ_MPIAIJ,
2627:                                        MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ,
2628:                                        MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ,
2629:                                        0,
2630:                                 /*134*/0,
2631:                                        0,
2632:                                        MatRARt_MPIAIJ_MPIAIJ,
2633:                                        0,
2634:                                        0,
2635:                                 /*139*/MatSetBlockSizes_MPIAIJ,
2636:                                        0,
2637:                                        0,
2638:                                        MatFDColoringSetUp_MPIXAIJ,
2639:                                        MatFindOffBlockDiagonalEntries_MPIAIJ,
2640:                                 /*144*/MatCreateMPIMatConcatenateSeqMat_MPIAIJ
2641: };

2643: /* ----------------------------------------------------------------------------------------*/

2645: PetscErrorCode  MatStoreValues_MPIAIJ(Mat mat)
2646: {
2647:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

2651:   MatStoreValues(aij->A);
2652:   MatStoreValues(aij->B);
2653:   return(0);
2654: }

2656: PetscErrorCode  MatRetrieveValues_MPIAIJ(Mat mat)
2657: {
2658:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

2662:   MatRetrieveValues(aij->A);
2663:   MatRetrieveValues(aij->B);
2664:   return(0);
2665: }

2667: PetscErrorCode  MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
2668: {
2669:   Mat_MPIAIJ     *b;

2673:   PetscLayoutSetUp(B->rmap);
2674:   PetscLayoutSetUp(B->cmap);
2675:   b = (Mat_MPIAIJ*)B->data;

2677: #if defined(PETSC_USE_CTABLE)
2678:   PetscTableDestroy(&b->colmap);
2679: #else
2680:   PetscFree(b->colmap);
2681: #endif
2682:   PetscFree(b->garray);
2683:   VecDestroy(&b->lvec);
2684:   VecScatterDestroy(&b->Mvctx);

2686:   /* Because the B will have been resized we simply destroy it and create a new one each time */
2687:   MatDestroy(&b->B);
2688:   MatCreate(PETSC_COMM_SELF,&b->B);
2689:   MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);
2690:   MatSetBlockSizesFromMats(b->B,B,B);
2691:   MatSetType(b->B,MATSEQAIJ);
2692:   PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);

2694:   if (!B->preallocated) {
2695:     MatCreate(PETSC_COMM_SELF,&b->A);
2696:     MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
2697:     MatSetBlockSizesFromMats(b->A,B,B);
2698:     MatSetType(b->A,MATSEQAIJ);
2699:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
2700:   }

2702:   MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);
2703:   MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);
2704:   B->preallocated  = PETSC_TRUE;
2705:   B->was_assembled = PETSC_FALSE;
2706:   B->assembled     = PETSC_FALSE;;
2707:   return(0);
2708: }

2710: PetscErrorCode MatResetPreallocation_MPIAIJ(Mat B)
2711: {
2712:   Mat_MPIAIJ     *b;

2717:   PetscLayoutSetUp(B->rmap);
2718:   PetscLayoutSetUp(B->cmap);
2719:   b = (Mat_MPIAIJ*)B->data;

2721: #if defined(PETSC_USE_CTABLE)
2722:   PetscTableDestroy(&b->colmap);
2723: #else
2724:   PetscFree(b->colmap);
2725: #endif
2726:   PetscFree(b->garray);
2727:   VecDestroy(&b->lvec);
2728:   VecScatterDestroy(&b->Mvctx);

2730:   MatResetPreallocation(b->A);
2731:   MatResetPreallocation(b->B);
2732:   B->preallocated  = PETSC_TRUE;
2733:   B->was_assembled = PETSC_FALSE;
2734:   B->assembled = PETSC_FALSE;
2735:   return(0);
2736: }

2738: PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
2739: {
2740:   Mat            mat;
2741:   Mat_MPIAIJ     *a,*oldmat = (Mat_MPIAIJ*)matin->data;

2745:   *newmat = 0;
2746:   MatCreate(PetscObjectComm((PetscObject)matin),&mat);
2747:   MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);
2748:   MatSetBlockSizesFromMats(mat,matin,matin);
2749:   MatSetType(mat,((PetscObject)matin)->type_name);
2750:   PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));
2751:   a       = (Mat_MPIAIJ*)mat->data;

2753:   mat->factortype   = matin->factortype;
2754:   mat->assembled    = PETSC_TRUE;
2755:   mat->insertmode   = NOT_SET_VALUES;
2756:   mat->preallocated = PETSC_TRUE;

2758:   a->size         = oldmat->size;
2759:   a->rank         = oldmat->rank;
2760:   a->donotstash   = oldmat->donotstash;
2761:   a->roworiented  = oldmat->roworiented;
2762:   a->rowindices   = 0;
2763:   a->rowvalues    = 0;
2764:   a->getrowactive = PETSC_FALSE;

2766:   PetscLayoutReference(matin->rmap,&mat->rmap);
2767:   PetscLayoutReference(matin->cmap,&mat->cmap);

2769:   if (oldmat->colmap) {
2770: #if defined(PETSC_USE_CTABLE)
2771:     PetscTableCreateCopy(oldmat->colmap,&a->colmap);
2772: #else
2773:     PetscMalloc1(mat->cmap->N,&a->colmap);
2774:     PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N)*sizeof(PetscInt));
2775:     PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));
2776: #endif
2777:   } else a->colmap = 0;
2778:   if (oldmat->garray) {
2779:     PetscInt len;
2780:     len  = oldmat->B->cmap->n;
2781:     PetscMalloc1(len+1,&a->garray);
2782:     PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));
2783:     if (len) { PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt)); }
2784:   } else a->garray = 0;

2786:   VecDuplicate(oldmat->lvec,&a->lvec);
2787:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);
2788:   VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
2789:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);

2791:   if (oldmat->Mvctx_mpi1) {
2792:     VecScatterCopy(oldmat->Mvctx_mpi1,&a->Mvctx_mpi1);
2793:     PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx_mpi1);
2794:   }

2796:   MatDuplicate(oldmat->A,cpvalues,&a->A);
2797:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
2798:   MatDuplicate(oldmat->B,cpvalues,&a->B);
2799:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);
2800:   PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);
2801:   *newmat = mat;
2802:   return(0);
2803: }

2805: PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer)
2806: {
2807:   PetscScalar    *vals,*svals;
2808:   MPI_Comm       comm;
2810:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
2811:   PetscInt       i,nz,j,rstart,rend,mmax,maxnz = 0;
2812:   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols;
2813:   PetscInt       *ourlens = NULL,*procsnz = NULL,*offlens = NULL,jj,*mycols,*smycols;
2814:   PetscInt       cend,cstart,n,*rowners;
2815:   int            fd;
2816:   PetscInt       bs = newMat->rmap->bs;

2819:   /* force binary viewer to load .info file if it has not yet done so */
2820:   PetscViewerSetUp(viewer);
2821:   PetscObjectGetComm((PetscObject)viewer,&comm);
2822:   MPI_Comm_size(comm,&size);
2823:   MPI_Comm_rank(comm,&rank);
2824:   PetscViewerBinaryGetDescriptor(viewer,&fd);
2825:   if (!rank) {
2826:     PetscBinaryRead(fd,(char*)header,4,PETSC_INT);
2827:     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
2828:     if (header[3] < 0) SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as MATMPIAIJ");
2829:   }

2831:   PetscOptionsBegin(comm,NULL,"Options for loading MATMPIAIJ matrix","Mat");
2832:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
2833:   PetscOptionsEnd();
2834:   if (bs < 0) bs = 1;

2836:   MPI_Bcast(header+1,3,MPIU_INT,0,comm);
2837:   M    = header[1]; N = header[2];

2839:   /* If global sizes are set, check if they are consistent with that given in the file */
2840:   if (newMat->rmap->N >= 0 && newMat->rmap->N != M) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Inconsistent # of rows:Matrix in file has (%D) and input matrix has (%D)",newMat->rmap->N,M);
2841:   if (newMat->cmap->N >=0 && newMat->cmap->N != N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Inconsistent # of cols:Matrix in file has (%D) and input matrix has (%D)",newMat->cmap->N,N);

2843:   /* determine ownership of all (block) rows */
2844:   if (M%bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows (%d) and block size (%d)",M,bs);
2845:   if (newMat->rmap->n < 0) m = bs*((M/bs)/size + (((M/bs) % size) > rank));    /* PETSC_DECIDE */
2846:   else m = newMat->rmap->n; /* Set by user */

2848:   PetscMalloc1(size+1,&rowners);
2849:   MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);

2851:   /* First process needs enough room for process with most rows */
2852:   if (!rank) {
2853:     mmax = rowners[1];
2854:     for (i=2; i<=size; i++) {
2855:       mmax = PetscMax(mmax, rowners[i]);
2856:     }
2857:   } else mmax = -1;             /* unused, but compilers complain */

2859:   rowners[0] = 0;
2860:   for (i=2; i<=size; i++) {
2861:     rowners[i] += rowners[i-1];
2862:   }
2863:   rstart = rowners[rank];
2864:   rend   = rowners[rank+1];

2866:   /* distribute row lengths to all processors */
2867:   PetscMalloc2(m,&ourlens,m,&offlens);
2868:   if (!rank) {
2869:     PetscBinaryRead(fd,ourlens,m,PETSC_INT);
2870:     PetscMalloc1(mmax,&rowlengths);
2871:     PetscCalloc1(size,&procsnz);
2872:     for (j=0; j<m; j++) {
2873:       procsnz[0] += ourlens[j];
2874:     }
2875:     for (i=1; i<size; i++) {
2876:       PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);
2877:       /* calculate the number of nonzeros on each processor */
2878:       for (j=0; j<rowners[i+1]-rowners[i]; j++) {
2879:         procsnz[i] += rowlengths[j];
2880:       }
2881:       MPIULong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);
2882:     }
2883:     PetscFree(rowlengths);
2884:   } else {
2885:     MPIULong_Recv(ourlens,m,MPIU_INT,0,tag,comm);
2886:   }

2888:   if (!rank) {
2889:     /* determine max buffer needed and allocate it */
2890:     maxnz = 0;
2891:     for (i=0; i<size; i++) {
2892:       maxnz = PetscMax(maxnz,procsnz[i]);
2893:     }
2894:     PetscMalloc1(maxnz,&cols);

2896:     /* read in my part of the matrix column indices  */
2897:     nz   = procsnz[0];
2898:     PetscMalloc1(nz,&mycols);
2899:     PetscBinaryRead(fd,mycols,nz,PETSC_INT);

2901:     /* read in every one elses and ship off */
2902:     for (i=1; i<size; i++) {
2903:       nz   = procsnz[i];
2904:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
2905:       MPIULong_Send(cols,nz,MPIU_INT,i,tag,comm);
2906:     }
2907:     PetscFree(cols);
2908:   } else {
2909:     /* determine buffer space needed for message */
2910:     nz = 0;
2911:     for (i=0; i<m; i++) {
2912:       nz += ourlens[i];
2913:     }
2914:     PetscMalloc1(nz,&mycols);

2916:     /* receive message of column indices*/
2917:     MPIULong_Recv(mycols,nz,MPIU_INT,0,tag,comm);
2918:   }

2920:   /* determine column ownership if matrix is not square */
2921:   if (N != M) {
2922:     if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank);
2923:     else n = newMat->cmap->n;
2924:     MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);
2925:     cstart = cend - n;
2926:   } else {
2927:     cstart = rstart;
2928:     cend   = rend;
2929:     n      = cend - cstart;
2930:   }

2932:   /* loop over local rows, determining number of off diagonal entries */
2933:   PetscMemzero(offlens,m*sizeof(PetscInt));
2934:   jj   = 0;
2935:   for (i=0; i<m; i++) {
2936:     for (j=0; j<ourlens[i]; j++) {
2937:       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
2938:       jj++;
2939:     }
2940:   }

2942:   for (i=0; i<m; i++) {
2943:     ourlens[i] -= offlens[i];
2944:   }
2945:   MatSetSizes(newMat,m,n,M,N);

2947:   if (bs > 1) {MatSetBlockSize(newMat,bs);}

2949:   MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);

2951:   for (i=0; i<m; i++) {
2952:     ourlens[i] += offlens[i];
2953:   }

2955:   if (!rank) {
2956:     PetscMalloc1(maxnz+1,&vals);

2958:     /* read in my part of the matrix numerical values  */
2959:     nz   = procsnz[0];
2960:     PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);

2962:     /* insert into matrix */
2963:     jj      = rstart;
2964:     smycols = mycols;
2965:     svals   = vals;
2966:     for (i=0; i<m; i++) {
2967:       MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
2968:       smycols += ourlens[i];
2969:       svals   += ourlens[i];
2970:       jj++;
2971:     }

2973:     /* read in other processors and ship out */
2974:     for (i=1; i<size; i++) {
2975:       nz   = procsnz[i];
2976:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2977:       MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);
2978:     }
2979:     PetscFree(procsnz);
2980:   } else {
2981:     /* receive numeric values */
2982:     PetscMalloc1(nz+1,&vals);

2984:     /* receive message of values*/
2985:     MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);

2987:     /* insert into matrix */
2988:     jj      = rstart;
2989:     smycols = mycols;
2990:     svals   = vals;
2991:     for (i=0; i<m; i++) {
2992:       MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
2993:       smycols += ourlens[i];
2994:       svals   += ourlens[i];
2995:       jj++;
2996:     }
2997:   }
2998:   PetscFree2(ourlens,offlens);
2999:   PetscFree(vals);
3000:   PetscFree(mycols);
3001:   PetscFree(rowners);
3002:   MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);
3003:   MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);
3004:   return(0);
3005: }

3007: /* Not scalable because of ISAllGather() unless getting all columns. */
3008: PetscErrorCode ISGetSeqIS_Private(Mat mat,IS iscol,IS *isseq)
3009: {
3011:   IS             iscol_local;
3012:   PetscBool      isstride;
3013:   PetscMPIInt    lisstride=0,gisstride;

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

3019:   if (isstride) {
3020:     PetscInt  start,len,mstart,mlen;
3021:     ISStrideGetInfo(iscol,&start,NULL);
3022:     ISGetLocalSize(iscol,&len);
3023:     MatGetOwnershipRangeColumn(mat,&mstart,&mlen);
3024:     if (mstart == start && mlen-mstart == len) lisstride = 1;
3025:   }

3027:   MPIU_Allreduce(&lisstride,&gisstride,1,MPI_INT,MPI_MIN,PetscObjectComm((PetscObject)mat));
3028:   if (gisstride) {
3029:     PetscInt N;
3030:     MatGetSize(mat,NULL,&N);
3031:     ISCreateStride(PetscObjectComm((PetscObject)mat),N,0,1,&iscol_local);
3032:     ISSetIdentity(iscol_local);
3033:     PetscInfo(mat,"Optimizing for obtaining all columns of the matrix; skipping ISAllGather()\n");
3034:   } else {
3035:     PetscInt cbs;
3036:     ISGetBlockSize(iscol,&cbs);
3037:     ISAllGather(iscol,&iscol_local);
3038:     ISSetBlockSize(iscol_local,cbs);
3039:   }

3041:   *isseq = iscol_local;
3042:   return(0);
3043: }

3045: /*
3046:  Used by MatCreateSubMatrix_MPIAIJ_SameRowColDist() to avoid ISAllGather() and global size of iscol_local
3047:  (see MatCreateSubMatrix_MPIAIJ_nonscalable)

3049:  Input Parameters:
3050:    mat - matrix
3051:    isrow - parallel row index set; its local indices are a subset of local columns of mat,
3052:            i.e., mat->rstart <= isrow[i] < mat->rend
3053:    iscol - parallel column index set; its local indices are a subset of local columns of mat,
3054:            i.e., mat->cstart <= iscol[i] < mat->cend
3055:  Output Parameter:
3056:    isrow_d,iscol_d - sequential row and column index sets for retrieving mat->A
3057:    iscol_o - sequential column index set for retrieving mat->B
3058:    garray - column map; garray[i] indicates global location of iscol_o[i] in iscol
3059:  */
3060: PetscErrorCode ISGetSeqIS_SameColDist_Private(Mat mat,IS isrow,IS iscol,IS *isrow_d,IS *iscol_d,IS *iscol_o,const PetscInt *garray[])
3061: {
3063:   Vec            x,cmap;
3064:   const PetscInt *is_idx;
3065:   PetscScalar    *xarray,*cmaparray;
3066:   PetscInt       ncols,isstart,*idx,m,rstart,*cmap1,count;
3067:   Mat_MPIAIJ     *a=(Mat_MPIAIJ*)mat->data;
3068:   Mat            B=a->B;
3069:   Vec            lvec=a->lvec,lcmap;
3070:   PetscInt       i,cstart,cend,Bn=B->cmap->N;
3071:   MPI_Comm       comm;
3072:   VecScatter     Mvctx=a->Mvctx;

3075:   PetscObjectGetComm((PetscObject)mat,&comm);
3076:   ISGetLocalSize(iscol,&ncols);

3078:   /* (1) iscol is a sub-column vector of mat, pad it with '-1.' to form a full vector x */
3079:   MatCreateVecs(mat,&x,NULL);
3080:   VecSet(x,-1.0);
3081:   VecDuplicate(x,&cmap);
3082:   VecSet(cmap,-1.0);

3084:   /* Get start indices */
3085:   MPI_Scan(&ncols,&isstart,1,MPIU_INT,MPI_SUM,comm);
3086:   isstart -= ncols;
3087:   MatGetOwnershipRangeColumn(mat,&cstart,&cend);

3089:   ISGetIndices(iscol,&is_idx);
3090:   VecGetArray(x,&xarray);
3091:   VecGetArray(cmap,&cmaparray);
3092:   PetscMalloc1(ncols,&idx);
3093:   for (i=0; i<ncols; i++) {
3094:     xarray[is_idx[i]-cstart]    = (PetscScalar)is_idx[i];
3095:     cmaparray[is_idx[i]-cstart] = i + isstart;      /* global index of iscol[i] */
3096:     idx[i]                      = is_idx[i]-cstart; /* local index of iscol[i]  */
3097:   }
3098:   VecRestoreArray(x,&xarray);
3099:   VecRestoreArray(cmap,&cmaparray);
3100:   ISRestoreIndices(iscol,&is_idx);

3102:   /* Get iscol_d */
3103:   ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,iscol_d);
3104:   ISGetBlockSize(iscol,&i);
3105:   ISSetBlockSize(*iscol_d,i);

3107:   /* Get isrow_d */
3108:   ISGetLocalSize(isrow,&m);
3109:   rstart = mat->rmap->rstart;
3110:   PetscMalloc1(m,&idx);
3111:   ISGetIndices(isrow,&is_idx);
3112:   for (i=0; i<m; i++) idx[i] = is_idx[i]-rstart;
3113:   ISRestoreIndices(isrow,&is_idx);

3115:   ISCreateGeneral(PETSC_COMM_SELF,m,idx,PETSC_OWN_POINTER,isrow_d);
3116:   ISGetBlockSize(isrow,&i);
3117:   ISSetBlockSize(*isrow_d,i);

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

3123:   VecDuplicate(lvec,&lcmap);

3125:   VecScatterBegin(Mvctx,cmap,lcmap,INSERT_VALUES,SCATTER_FORWARD);
3126:   VecScatterEnd(Mvctx,cmap,lcmap,INSERT_VALUES,SCATTER_FORWARD);

3128:   /* (3) create sequential iscol_o (a subset of iscol) and isgarray */
3129:   /* off-process column indices */
3130:   count = 0;
3131:   PetscMalloc1(Bn,&idx);
3132:   PetscMalloc1(Bn,&cmap1);

3134:   VecGetArray(lvec,&xarray);
3135:   VecGetArray(lcmap,&cmaparray);
3136:   for (i=0; i<Bn; i++) {
3137:     if (PetscRealPart(xarray[i]) > -1.0) {
3138:       idx[count]     = i;                   /* local column index in off-diagonal part B */
3139:       cmap1[count] = (PetscInt)PetscRealPart(cmaparray[i]);  /* column index in submat */
3140:       count++;
3141:     }
3142:   }
3143:   VecRestoreArray(lvec,&xarray);
3144:   VecRestoreArray(lcmap,&cmaparray);

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

3149:   PetscFree(idx);
3150:   *garray = cmap1;

3152:   VecDestroy(&x);
3153:   VecDestroy(&cmap);
3154:   VecDestroy(&lcmap);
3155:   return(0);
3156: }

3158: /* isrow and iscol have same processor distribution as mat, output *submat is a submatrix of local mat */
3159: PetscErrorCode MatCreateSubMatrix_MPIAIJ_SameRowColDist(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *submat)
3160: {
3162:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)mat->data,*asub;
3163:   Mat            M = NULL;
3164:   MPI_Comm       comm;
3165:   IS             iscol_d,isrow_d,iscol_o;
3166:   Mat            Asub = NULL,Bsub = NULL;
3167:   PetscInt       n;

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

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

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

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

3183:     /* Update diagonal and off-diagonal portions of submat */
3184:     asub = (Mat_MPIAIJ*)(*submat)->data;
3185:     MatCreateSubMatrix_SeqAIJ(a->A,isrow_d,iscol_d,PETSC_DECIDE,MAT_REUSE_MATRIX,&asub->A);
3186:     ISGetLocalSize(iscol_o,&n);
3187:     if (n) {
3188:       MatCreateSubMatrix_SeqAIJ(a->B,isrow_d,iscol_o,PETSC_DECIDE,MAT_REUSE_MATRIX,&asub->B);
3189:     }
3190:     MatAssemblyBegin(*submat,MAT_FINAL_ASSEMBLY);
3191:     MatAssemblyEnd(*submat,MAT_FINAL_ASSEMBLY);

3193:   } else { /* call == MAT_INITIAL_MATRIX) */
3194:     const PetscInt *garray;
3195:     PetscInt        BsubN;

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

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

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

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

3210:     ISGetLocalSize(iscol_o,&BsubN);
3211:     n = asub->B->cmap->N;
3212:     if (BsubN > n) {
3213:       /* This case can be tested using ~petsc/src/tao/bound/examples/tutorials/runplate2_3 */
3214:       const PetscInt *idx;
3215:       PetscInt       i,j,*idx_new,*subgarray = asub->garray;
3216:       PetscInfo2(M,"submatrix Bn %D != BsubN %D, update iscol_o\n",n,BsubN);

3218:       PetscMalloc1(n,&idx_new);
3219:       j = 0;
3220:       ISGetIndices(iscol_o,&idx);
3221:       for (i=0; i<n; i++) {
3222:         if (j >= BsubN) break;
3223:         while (subgarray[i] > garray[j]) j++;

3225:         if (subgarray[i] == garray[j]) {
3226:           idx_new[i] = idx[j++];
3227:         } else SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"subgarray[%D]=%D cannot < garray[%D]=%D",i,subgarray[i],j,garray[j]);
3228:       }
3229:       ISRestoreIndices(iscol_o,&idx);

3231:       ISDestroy(&iscol_o);
3232:       ISCreateGeneral(PETSC_COMM_SELF,n,idx_new,PETSC_OWN_POINTER,&iscol_o);

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

3238:     PetscFree(garray);
3239:     *submat = M;

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

3245:     PetscObjectCompose((PetscObject)M,"iscol_d",(PetscObject)iscol_d);
3246:     ISDestroy(&iscol_d);

3248:     PetscObjectCompose((PetscObject)M,"iscol_o",(PetscObject)iscol_o);
3249:     ISDestroy(&iscol_o);
3250:   }
3251:   return(0);
3252: }

3254: PetscErrorCode MatCreateSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
3255: {
3257:   IS             iscol_local=NULL,isrow_d;
3258:   PetscInt       csize;
3259:   PetscInt       n,i,j,start,end;
3260:   PetscBool      sameRowDist=PETSC_FALSE,sameDist[2],tsameDist[2];
3261:   MPI_Comm       comm;

3264:   /* If isrow has same processor distribution as mat,
3265:      call MatCreateSubMatrix_MPIAIJ_SameRowDist() to avoid using a hash table with global size of iscol */
3266:   if (call == MAT_REUSE_MATRIX) {
3267:     PetscObjectQuery((PetscObject)*newmat,"isrow_d",(PetscObject*)&isrow_d);
3268:     if (isrow_d) {
3269:       sameRowDist  = PETSC_TRUE;
3270:       tsameDist[1] = PETSC_TRUE; /* sameColDist */
3271:     } else {
3272:       PetscObjectQuery((PetscObject)*newmat,"SubIScol",(PetscObject*)&iscol_local);
3273:       if (iscol_local) {
3274:         sameRowDist  = PETSC_TRUE;
3275:         tsameDist[1] = PETSC_FALSE; /* !sameColDist */
3276:       }
3277:     }
3278:   } else {
3279:     /* Check if isrow has same processor distribution as mat */
3280:     sameDist[0] = PETSC_FALSE;
3281:     ISGetLocalSize(isrow,&n);
3282:     if (!n) {
3283:       sameDist[0] = PETSC_TRUE;
3284:     } else {
3285:       ISGetMinMax(isrow,&i,&j);
3286:       MatGetOwnershipRange(mat,&start,&end);
3287:       if (i >= start && j < end) {
3288:         sameDist[0] = PETSC_TRUE;
3289:       }
3290:     }

3292:     /* Check if iscol has same processor distribution as mat */
3293:     sameDist[1] = PETSC_FALSE;
3294:     ISGetLocalSize(iscol,&n);
3295:     if (!n) {
3296:       sameDist[1] = PETSC_TRUE;
3297:     } else {
3298:       ISGetMinMax(iscol,&i,&j);
3299:       MatGetOwnershipRangeColumn(mat,&start,&end);
3300:       if (i >= start && j < end) sameDist[1] = PETSC_TRUE;
3301:     }

3303:     PetscObjectGetComm((PetscObject)mat,&comm);
3304:     MPIU_Allreduce(&sameDist,&tsameDist,2,MPIU_BOOL,MPI_LAND,comm);
3305:     sameRowDist = tsameDist[0];
3306:   }

3308:   if (sameRowDist) {
3309:     if (tsameDist[1]) { /* sameRowDist & sameColDist */
3310:       /* isrow and iscol have same processor distribution as mat */
3311:       MatCreateSubMatrix_MPIAIJ_SameRowColDist(mat,isrow,iscol,call,newmat);
3312:       return(0);
3313:     } else { /* sameRowDist */
3314:       /* isrow has same processor distribution as mat */
3315:       if (call == MAT_INITIAL_MATRIX) {
3316:         PetscBool sorted;
3317:         ISGetSeqIS_Private(mat,iscol,&iscol_local);
3318:         ISGetLocalSize(iscol_local,&n); /* local size of iscol_local = global columns of newmat */
3319:         ISGetSize(iscol,&i);
3320:         if (n != i) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"n %d != size of iscol %d",n,i);

3322:         ISSorted(iscol_local,&sorted);
3323:         if (sorted) {
3324:           /* MatCreateSubMatrix_MPIAIJ_SameRowDist() requires iscol_local be sorted; it can have duplicate indices */
3325:           MatCreateSubMatrix_MPIAIJ_SameRowDist(mat,isrow,iscol,iscol_local,MAT_INITIAL_MATRIX,newmat);
3326:           return(0);
3327:         }
3328:       } else { /* call == MAT_REUSE_MATRIX */
3329:         IS    iscol_sub;
3330:         PetscObjectQuery((PetscObject)*newmat,"SubIScol",(PetscObject*)&iscol_sub);
3331:         if (iscol_sub) {
3332:           MatCreateSubMatrix_MPIAIJ_SameRowDist(mat,isrow,iscol,NULL,call,newmat);
3333:           return(0);
3334:         }
3335:       }
3336:     }
3337:   }

3339:   /* General case: iscol -> iscol_local which has global size of iscol */
3340:   if (call == MAT_REUSE_MATRIX) {
3341:     PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);
3342:     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3343:   } else {
3344:     if (!iscol_local) {
3345:       ISGetSeqIS_Private(mat,iscol,&iscol_local);
3346:     }
3347:   }

3349:   ISGetLocalSize(iscol,&csize);
3350:   MatCreateSubMatrix_MPIAIJ_nonscalable(mat,isrow,iscol_local,csize,call,newmat);

3352:   if (call == MAT_INITIAL_MATRIX) {
3353:     PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);
3354:     ISDestroy(&iscol_local);
3355:   }
3356:   return(0);
3357: }

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

3363:    Collective on MPI_Comm

3365:    Input Parameters:
3366: +  comm - MPI communicator
3367: .  A - "diagonal" portion of matrix
3368: .  B - "off-diagonal" portion of matrix, may have empty columns, will be destroyed by this routine
3369: -  garray - global index of B columns

3371:    Output Parameter:
3372: .   mat - the matrix, with input A as its local diagonal matrix
3373:    Level: advanced

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

3379: .seealso: MatCreateMPIAIJWithSplitArrays()
3380: @*/
3381: PetscErrorCode MatCreateMPIAIJWithSeqAIJ(MPI_Comm comm,Mat A,Mat B,const PetscInt garray[],Mat *mat)
3382: {
3384:   Mat_MPIAIJ     *maij;
3385:   Mat_SeqAIJ     *b=(Mat_SeqAIJ*)B->data,*bnew;
3386:   PetscInt       *oi=b->i,*oj=b->j,i,nz,col;
3387:   PetscScalar    *oa=b->a;
3388:   Mat            Bnew;
3389:   PetscInt       m,n,N;

3392:   MatCreate(comm,mat);
3393:   MatGetSize(A,&m,&n);
3394:   if (m != B->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Am %D != Bm %D",m,B->rmap->N);
3395:   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);
3396:   /* remove check below; When B is created using iscol_o from ISGetSeqIS_SameColDist_Private(), its bs may not be same as A */
3397:   /* 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); */

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

3402:   MatSetSizes(*mat,m,n,PETSC_DECIDE,N);
3403:   MatSetType(*mat,MATMPIAIJ);
3404:   MatSetBlockSizes(*mat,A->rmap->bs,A->cmap->bs);
3405:   maij = (Mat_MPIAIJ*)(*mat)->data;

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

3409:   PetscLayoutSetUp((*mat)->rmap);
3410:   PetscLayoutSetUp((*mat)->cmap);

3412:   /* Set A as diagonal portion of *mat */
3413:   maij->A = A;

3415:   nz = oi[m];
3416:   for (i=0; i<nz; i++) {
3417:     col   = oj[i];
3418:     oj[i] = garray[col];
3419:   }

3421:    /* Set Bnew as off-diagonal portion of *mat */
3422:   MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,N,oi,oj,oa,&Bnew);
3423:   bnew        = (Mat_SeqAIJ*)Bnew->data;
3424:   bnew->maxnz = b->maxnz; /* allocated nonzeros of B */
3425:   maij->B     = Bnew;

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

3429:   b->singlemalloc = PETSC_FALSE; /* B arrays are shared by Bnew */
3430:   b->free_a       = PETSC_FALSE;
3431:   b->free_ij      = PETSC_FALSE;
3432:   MatDestroy(&B);

3434:   bnew->singlemalloc = PETSC_TRUE; /* arrays will be freed by MatDestroy(&Bnew) */
3435:   bnew->free_a       = PETSC_TRUE;
3436:   bnew->free_ij      = PETSC_TRUE;

3438:   /* condense columns of maij->B */
3439:   MatSetOption(*mat,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);
3440:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
3441:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
3442:   MatSetOption(*mat,MAT_NO_OFF_PROC_ENTRIES,PETSC_FALSE);
3443:   MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3444:   return(0);
3445: }

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

3449: PetscErrorCode MatCreateSubMatrix_MPIAIJ_SameRowDist(Mat mat,IS isrow,IS iscol,IS iscol_local,MatReuse call,Mat *newmat)
3450: {
3452:   PetscInt       i,m,n,rstart,row,rend,nz,j,bs,cbs;
3453:   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
3454:   Mat_MPIAIJ     *a=(Mat_MPIAIJ*)mat->data;
3455:   Mat            M,Msub,B=a->B;
3456:   MatScalar      *aa;
3457:   Mat_SeqAIJ     *aij;
3458:   PetscInt       *garray = a->garray,*colsub,Ncols;
3459:   PetscInt       count,Bn=B->cmap->N,cstart=mat->cmap->rstart,cend=mat->cmap->rend;
3460:   IS             iscol_sub,iscmap;
3461:   const PetscInt *is_idx,*cmap;
3462:   PetscBool      allcolumns=PETSC_FALSE;
3463:   MPI_Comm       comm;

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

3468:   if (call == MAT_REUSE_MATRIX) {
3469:     PetscObjectQuery((PetscObject)*newmat,"SubIScol",(PetscObject*)&iscol_sub);
3470:     if (!iscol_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SubIScol passed in was not used before, cannot reuse");
3471:     ISGetLocalSize(iscol_sub,&count);

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

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

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

3481:   } else { /* call == MAT_INITIAL_MATRIX) */
3482:     PetscBool flg;

3484:     ISGetLocalSize(iscol,&n);
3485:     ISGetSize(iscol,&Ncols);

3487:     /* (1) iscol -> nonscalable iscol_local */
3488:     /* Check for special case: each processor gets entire matrix columns */
3489:     ISIdentity(iscol_local,&flg);
3490:     if (flg && n == mat->cmap->N) allcolumns = PETSC_TRUE;
3491:     if (allcolumns) {
3492:       iscol_sub = iscol_local;
3493:       PetscObjectReference((PetscObject)iscol_local);
3494:       ISCreateStride(PETSC_COMM_SELF,n,0,1,&iscmap);

3496:     } else {
3497:       /* (2) iscol_local -> iscol_sub and iscmap. Implementation below requires iscol_local be sorted, it can have duplicate indices */
3498:       PetscInt *idx,*cmap1,k;
3499:       PetscMalloc1(Ncols,&idx);
3500:       PetscMalloc1(Ncols,&cmap1);
3501:       ISGetIndices(iscol_local,&is_idx);
3502:       count = 0;
3503:       k     = 0;
3504:       for (i=0; i<Ncols; i++) {
3505:         j = is_idx[i];
3506:         if (j >= cstart && j < cend) {
3507:           /* diagonal part of mat */
3508:           idx[count]     = j;
3509:           cmap1[count++] = i; /* column index in submat */
3510:         } else if (Bn) {
3511:           /* off-diagonal part of mat */
3512:           if (j == garray[k]) {
3513:             idx[count]     = j;
3514:             cmap1[count++] = i;  /* column index in submat */
3515:           } else if (j > garray[k]) {
3516:             while (j > garray[k] && k < Bn-1) k++;
3517:             if (j == garray[k]) {
3518:               idx[count]     = j;
3519:               cmap1[count++] = i; /* column index in submat */
3520:             }
3521:           }
3522:         }
3523:       }
3524:       ISRestoreIndices(iscol_local,&is_idx);

3526:       ISCreateGeneral(PETSC_COMM_SELF,count,idx,PETSC_OWN_POINTER,&iscol_sub);
3527:       ISGetBlockSize(iscol,&cbs);
3528:       ISSetBlockSize(iscol_sub,cbs);

3530:       ISCreateGeneral(PetscObjectComm((PetscObject)iscol_local),count,cmap1,PETSC_OWN_POINTER,&iscmap);
3531:     }

3533:     /* (3) Create sequential Msub */
3534:     MatCreateSubMatrices_MPIAIJ_SingleIS_Local(mat,1,&isrow,&iscol_sub,MAT_INITIAL_MATRIX,allcolumns,&Msub);
3535:   }

3537:   ISGetLocalSize(iscol_sub,&count);
3538:   aij  = (Mat_SeqAIJ*)(Msub)->data;
3539:   ii   = aij->i;
3540:   ISGetIndices(iscmap,&cmap);

3542:   /*
3543:       m - number of local rows
3544:       Ncols - number of columns (same on all processors)
3545:       rstart - first row in new global matrix generated
3546:   */
3547:   MatGetSize(Msub,&m,NULL);

3549:   if (call == MAT_INITIAL_MATRIX) {
3550:     /* (4) Create parallel newmat */
3551:     PetscMPIInt    rank,size;
3552:     PetscInt       csize;

3554:     MPI_Comm_size(comm,&size);
3555:     MPI_Comm_rank(comm,&rank);

3557:     /*
3558:         Determine the number of non-zeros in the diagonal and off-diagonal
3559:         portions of the matrix in order to do correct preallocation
3560:     */

3562:     /* first get start and end of "diagonal" columns */
3563:     ISGetLocalSize(iscol,&csize);
3564:     if (csize == PETSC_DECIDE) {
3565:       ISGetSize(isrow,&mglobal);
3566:       if (mglobal == Ncols) { /* square matrix */
3567:         nlocal = m;
3568:       } else {
3569:         nlocal = Ncols/size + ((Ncols % size) > rank);
3570:       }
3571:     } else {
3572:       nlocal = csize;
3573:     }
3574:     MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
3575:     rstart = rend - nlocal;
3576:     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);

3578:     /* next, compute all the lengths */
3579:     jj    = aij->j;
3580:     PetscMalloc1(2*m+1,&dlens);
3581:     olens = dlens + m;
3582:     for (i=0; i<m; i++) {
3583:       jend = ii[i+1] - ii[i];
3584:       olen = 0;
3585:       dlen = 0;
3586:       for (j=0; j<jend; j++) {
3587:         if (cmap[*jj] < rstart || cmap[*jj] >= rend) olen++;
3588:         else dlen++;
3589:         jj++;
3590:       }
3591:       olens[i] = olen;
3592:       dlens[i] = dlen;
3593:     }

3595:     ISGetBlockSize(isrow,&bs);
3596:     ISGetBlockSize(iscol,&cbs);

3598:     MatCreate(comm,&M);
3599:     MatSetSizes(M,m,nlocal,PETSC_DECIDE,Ncols);
3600:     MatSetBlockSizes(M,bs,cbs);
3601:     MatSetType(M,((PetscObject)mat)->type_name);
3602:     MatMPIAIJSetPreallocation(M,0,dlens,0,olens);
3603:     PetscFree(dlens);

3605:   } else { /* call == MAT_REUSE_MATRIX */
3606:     M    = *newmat;
3607:     MatGetLocalSize(M,&i,NULL);
3608:     if (i != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
3609:     MatZeroEntries(M);
3610:     /*
3611:          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
3612:        rather than the slower MatSetValues().
3613:     */
3614:     M->was_assembled = PETSC_TRUE;
3615:     M->assembled     = PETSC_FALSE;
3616:   }

3618:   /* (5) Set values of Msub to *newmat */
3619:   PetscMalloc1(count,&colsub);
3620:   MatGetOwnershipRange(M,&rstart,NULL);

3622:   jj   = aij->j;
3623:   aa   = aij->a;
3624:   for (i=0; i<m; i++) {
3625:     row = rstart + i;
3626:     nz  = ii[i+1] - ii[i];
3627:     for (j=0; j<nz; j++) colsub[j] = cmap[jj[j]];
3628:     MatSetValues_MPIAIJ(M,1,&row,nz,colsub,aa,INSERT_VALUES);
3629:     jj += nz; aa += nz;
3630:   }
3631:   ISRestoreIndices(iscmap,&cmap);

3633:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
3634:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);

3636:   PetscFree(colsub);

3638:   /* save Msub, iscol_sub and iscmap used in processor for next request */
3639:   if (call ==  MAT_INITIAL_MATRIX) {
3640:     *newmat = M;
3641:     PetscObjectCompose((PetscObject)(*newmat),"SubMatrix",(PetscObject)Msub);
3642:     MatDestroy(&Msub);

3644:     PetscObjectCompose((PetscObject)(*newmat),"SubIScol",(PetscObject)iscol_sub);
3645:     ISDestroy(&iscol_sub);

3647:     PetscObjectCompose((PetscObject)(*newmat),"Subcmap",(PetscObject)iscmap);
3648:     ISDestroy(&iscmap);

3650:     if (iscol_local) {
3651:       PetscObjectCompose((PetscObject)(*newmat),"ISAllGather",(PetscObject)iscol_local);
3652:       ISDestroy(&iscol_local);
3653:     }
3654:   }
3655:   return(0);
3656: }

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

3663:   Note: This requires a sequential iscol with all indices.
3664: */
3665: PetscErrorCode MatCreateSubMatrix_MPIAIJ_nonscalable(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
3666: {
3668:   PetscMPIInt    rank,size;
3669:   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs;
3670:   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
3671:   Mat            M,Mreuse;
3672:   MatScalar      *aa,*vwork;
3673:   MPI_Comm       comm;
3674:   Mat_SeqAIJ     *aij;
3675:   PetscBool      colflag,allcolumns=PETSC_FALSE;

3678:   PetscObjectGetComm((PetscObject)mat,&comm);
3679:   MPI_Comm_rank(comm,&rank);
3680:   MPI_Comm_size(comm,&size);

3682:   /* Check for special case: each processor gets entire matrix columns */
3683:   ISIdentity(iscol,&colflag);
3684:   ISGetLocalSize(iscol,&n);
3685:   if (colflag && n == mat->cmap->N) allcolumns = PETSC_TRUE;

3687:   if (call ==  MAT_REUSE_MATRIX) {
3688:     PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);
3689:     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3690:     MatCreateSubMatrices_MPIAIJ_SingleIS_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,allcolumns,&Mreuse);
3691:   } else {
3692:     MatCreateSubMatrices_MPIAIJ_SingleIS_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,allcolumns,&Mreuse);
3693:   }

3695:   /*
3696:       m - number of local rows
3697:       n - number of columns (same on all processors)
3698:       rstart - first row in new global matrix generated
3699:   */
3700:   MatGetSize(Mreuse,&m,&n);
3701:   MatGetBlockSizes(Mreuse,&bs,&cbs);
3702:   if (call == MAT_INITIAL_MATRIX) {
3703:     aij = (Mat_SeqAIJ*)(Mreuse)->data;
3704:     ii  = aij->i;
3705:     jj  = aij->j;

3707:     /*
3708:         Determine the number of non-zeros in the diagonal and off-diagonal
3709:         portions of the matrix in order to do correct preallocation
3710:     */

3712:     /* first get start and end of "diagonal" columns */
3713:     if (csize == PETSC_DECIDE) {
3714:       ISGetSize(isrow,&mglobal);
3715:       if (mglobal == n) { /* square matrix */
3716:         nlocal = m;
3717:       } else {
3718:         nlocal = n/size + ((n % size) > rank);
3719:       }
3720:     } else {
3721:       nlocal = csize;
3722:     }
3723:     MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
3724:     rstart = rend - nlocal;
3725:     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);

3727:     /* next, compute all the lengths */
3728:     PetscMalloc1(2*m+1,&dlens);
3729:     olens = dlens + m;
3730:     for (i=0; i<m; i++) {
3731:       jend = ii[i+1] - ii[i];
3732:       olen = 0;
3733:       dlen = 0;
3734:       for (j=0; j<jend; j++) {
3735:         if (*jj < rstart || *jj >= rend) olen++;
3736:         else dlen++;
3737:         jj++;
3738:       }
3739:       olens[i] = olen;
3740:       dlens[i] = dlen;
3741:     }
3742:     MatCreate(comm,&M);
3743:     MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);
3744:     MatSetBlockSizes(M,bs,cbs);
3745:     MatSetType(M,((PetscObject)mat)->type_name);
3746:     MatMPIAIJSetPreallocation(M,0,dlens,0,olens);
3747:     PetscFree(dlens);
3748:   } else {
3749:     PetscInt ml,nl;

3751:     M    = *newmat;
3752:     MatGetLocalSize(M,&ml,&nl);
3753:     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
3754:     MatZeroEntries(M);
3755:     /*
3756:          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
3757:        rather than the slower MatSetValues().
3758:     */
3759:     M->was_assembled = PETSC_TRUE;
3760:     M->assembled     = PETSC_FALSE;
3761:   }
3762:   MatGetOwnershipRange(M,&rstart,&rend);
3763:   aij  = (Mat_SeqAIJ*)(Mreuse)->data;
3764:   ii   = aij->i;
3765:   jj   = aij->j;
3766:   aa   = aij->a;
3767:   for (i=0; i<m; i++) {
3768:     row   = rstart + i;
3769:     nz    = ii[i+1] - ii[i];
3770:     cwork = jj;     jj += nz;
3771:     vwork = aa;     aa += nz;
3772:     MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);
3773:   }

3775:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
3776:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
3777:   *newmat = M;

3779:   /* save submatrix used in processor for next request */
3780:   if (call ==  MAT_INITIAL_MATRIX) {
3781:     PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);
3782:     MatDestroy(&Mreuse);
3783:   }
3784:   return(0);
3785: }

3787: PetscErrorCode MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3788: {
3789:   PetscInt       m,cstart, cend,j,nnz,i,d;
3790:   PetscInt       *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
3791:   const PetscInt *JJ;
3792:   PetscScalar    *values;
3794:   PetscBool      nooffprocentries;

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

3799:   PetscLayoutSetUp(B->rmap);
3800:   PetscLayoutSetUp(B->cmap);
3801:   m      = B->rmap->n;
3802:   cstart = B->cmap->rstart;
3803:   cend   = B->cmap->rend;
3804:   rstart = B->rmap->rstart;

3806:   PetscMalloc2(m,&d_nnz,m,&o_nnz);

3808: #if defined(PETSC_USE_DEBUG)
3809:   for (i=0; i<m; i++) {
3810:     nnz = Ii[i+1]- Ii[i];
3811:     JJ  = J + Ii[i];
3812:     if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
3813:     if (nnz && (JJ[0] < 0)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,JJ[0]);
3814:     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);
3815:   }
3816: #endif

3818:   for (i=0; i<m; i++) {
3819:     nnz     = Ii[i+1]- Ii[i];
3820:     JJ      = J + Ii[i];
3821:     nnz_max = PetscMax(nnz_max,nnz);
3822:     d       = 0;
3823:     for (j=0; j<nnz; j++) {
3824:       if (cstart <= JJ[j] && JJ[j] < cend) d++;
3825:     }
3826:     d_nnz[i] = d;
3827:     o_nnz[i] = nnz - d;
3828:   }
3829:   MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);
3830:   PetscFree2(d_nnz,o_nnz);

3832:   if (v) values = (PetscScalar*)v;
3833:   else {
3834:     PetscCalloc1(nnz_max+1,&values);
3835:   }

3837:   for (i=0; i<m; i++) {
3838:     ii   = i + rstart;
3839:     nnz  = Ii[i+1]- Ii[i];
3840:     MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);
3841:   }
3842:   nooffprocentries    = B->nooffprocentries;
3843:   B->nooffprocentries = PETSC_TRUE;
3844:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3845:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
3846:   B->nooffprocentries = nooffprocentries;

3848:   if (!v) {
3849:     PetscFree(values);
3850:   }
3851:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3852:   return(0);
3853: }

3855: /*@
3856:    MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
3857:    (the default parallel PETSc format).

3859:    Collective on MPI_Comm

3861:    Input Parameters:
3862: +  B - the matrix
3863: .  i - the indices into j for the start of each local row (starts with zero)
3864: .  j - the column indices for each local row (starts with zero)
3865: -  v - optional values in the matrix

3867:    Level: developer

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

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

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

3880: $        1 0 0
3881: $        2 0 3     P0
3882: $       -------
3883: $        4 5 6     P1
3884: $
3885: $     Process0 [P0]: rows_owned=[0,1]
3886: $        i =  {0,1,3}  [size = nrow+1  = 2+1]
3887: $        j =  {0,0,2}  [size = 3]
3888: $        v =  {1,2,3}  [size = 3]
3889: $
3890: $     Process1 [P1]: rows_owned=[2]
3891: $        i =  {0,3}    [size = nrow+1  = 1+1]
3892: $        j =  {0,1,2}  [size = 3]
3893: $        v =  {4,5,6}  [size = 3]

3895: .keywords: matrix, aij, compressed row, sparse, parallel

3897: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MATMPIAIJ,
3898:           MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays()
3899: @*/
3900: PetscErrorCode  MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3901: {

3905:   PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
3906:   return(0);
3907: }

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

3916:    Collective on MPI_Comm

3918:    Input Parameters:
3919: +  B - the matrix
3920: .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
3921:            (same value is used for all local rows)
3922: .  d_nnz - array containing the number of nonzeros in the various rows of the
3923:            DIAGONAL portion of the local submatrix (possibly different for each row)
3924:            or NULL (PETSC_NULL_INTEGER in Fortran), if d_nz is used to specify the nonzero structure.
3925:            The size of this array is equal to the number of local rows, i.e 'm'.
3926:            For matrices that will be factored, you must leave room for (and set)
3927:            the diagonal entry even if it is zero.
3928: .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
3929:            submatrix (same value is used for all local rows).
3930: -  o_nnz - array containing the number of nonzeros in the various rows of the
3931:            OFF-DIAGONAL portion of the local submatrix (possibly different for
3932:            each row) or NULL (PETSC_NULL_INTEGER in Fortran), if o_nz is used to specify the nonzero
3933:            structure. The size of this array is equal to the number
3934:            of local rows, i.e 'm'.

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

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

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

3947:    The DIAGONAL portion of the local submatrix of a processor can be defined
3948:    as the submatrix which is obtained by extraction the part corresponding to
3949:    the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the
3950:    first row that belongs to the processor, r2 is the last row belonging to
3951:    the this processor, and c1-c2 is range of indices of the local part of a
3952:    vector suitable for applying the matrix to.  This is an mxn matrix.  In the
3953:    common case of a square matrix, the row and column ranges are the same and
3954:    the DIAGONAL part is also square. The remaining portion of the local
3955:    submatrix (mxN) constitute the OFF-DIAGONAL portion.

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

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

3964:    Example usage:

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

3971: .vb
3972:             1  2  0  |  0  3  0  |  0  4
3973:     Proc0   0  5  6  |  7  0  0  |  8  0
3974:             9  0 10  | 11  0  0  | 12  0
3975:     -------------------------------------
3976:            13  0 14  | 15 16 17  |  0  0
3977:     Proc1   0 18  0  | 19 20 21  |  0  0
3978:             0  0  0  | 22 23  0  | 24  0
3979:     -------------------------------------
3980:     Proc2  25 26 27  |  0  0 28  | 29  0
3981:            30  0  0  | 31 32 33  |  0 34
3982: .ve

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

3986: .vb
3987:       A B C
3988:       D E F
3989:       G H I
3990: .ve

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

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

3999:    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
4000:    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
4001:    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
4002:    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
4003:    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
4004:    matrix, ans [DF] as another SeqAIJ matrix.

4006:    When d_nz, o_nz parameters are specified, d_nz storage elements are
4007:    allocated for every row of the local diagonal submatrix, and o_nz
4008:    storage locations are allocated for every row of the OFF-DIAGONAL submat.
4009:    One way to choose d_nz and o_nz is to use the max nonzerors per local
4010:    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
4011:    In this case, the values of d_nz,o_nz are:
4012: .vb
4013:      proc0 : dnz = 2, o_nz = 2
4014:      proc1 : dnz = 3, o_nz = 2
4015:      proc2 : dnz = 1, o_nz = 4
4016: .ve
4017:    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
4018:    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
4019:    for proc3. i.e we are using 12+15+10=37 storage locations to store
4020:    34 values.

4022:    When d_nnz, o_nnz parameters are specified, the storage is specified
4023:    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
4024:    In the above case the values for d_nnz,o_nnz are:
4025: .vb
4026:      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
4027:      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
4028:      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
4029: .ve
4030:    Here the space allocated is sum of all the above values i.e 34, and
4031:    hence pre-allocation is perfect.

4033:    Level: intermediate

4035: .keywords: matrix, aij, compressed row, sparse, parallel

4037: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(),
4038:           MATMPIAIJ, MatGetInfo(), PetscSplitOwnership()
4039: @*/
4040: PetscErrorCode MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
4041: {

4047:   PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));
4048:   return(0);
4049: }

4051: /*@
4052:      MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard
4053:          CSR format the local rows.

4055:    Collective on MPI_Comm

4057:    Input Parameters:
4058: +  comm - MPI communicator
4059: .  m - number of local rows (Cannot be PETSC_DECIDE)
4060: .  n - This value should be the same as the local size used in creating the
4061:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4062:        calculated if N is given) For square matrices n is almost always m.
4063: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4064: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4065: .   i - row indices
4066: .   j - column indices
4067: -   a - matrix values

4069:    Output Parameter:
4070: .   mat - the matrix

4072:    Level: intermediate

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

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

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

4085: $        1 0 0
4086: $        2 0 3     P0
4087: $       -------
4088: $        4 5 6     P1
4089: $
4090: $     Process0 [P0]: rows_owned=[0,1]
4091: $        i =  {0,1,3}  [size = nrow+1  = 2+1]
4092: $        j =  {0,0,2}  [size = 3]
4093: $        v =  {1,2,3}  [size = 3]
4094: $
4095: $     Process1 [P1]: rows_owned=[2]
4096: $        i =  {0,3}    [size = nrow+1  = 1+1]
4097: $        j =  {0,1,2}  [size = 3]
4098: $        v =  {4,5,6}  [size = 3]

4100: .keywords: matrix, aij, compressed row, sparse, parallel

4102: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4103:           MATMPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
4104: @*/
4105: PetscErrorCode MatCreateMPIAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
4106: {

4110:   if (i && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4111:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
4112:   MatCreate(comm,mat);
4113:   MatSetSizes(*mat,m,n,M,N);
4114:   /* MatSetBlockSizes(M,bs,cbs); */
4115:   MatSetType(*mat,MATMPIAIJ);
4116:   MatMPIAIJSetPreallocationCSR(*mat,i,j,a);
4117:   return(0);
4118: }

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

4127:    Collective on MPI_Comm

4129:    Input Parameters:
4130: +  comm - MPI communicator
4131: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
4132:            This value should be the same as the local size used in creating the
4133:            y vector for the matrix-vector product y = Ax.
4134: .  n - This value should be the same as the local size used in creating the
4135:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4136:        calculated if N is given) For square matrices n is almost always m.
4137: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4138: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4139: .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
4140:            (same value is used for all local rows)
4141: .  d_nnz - array containing the number of nonzeros in the various rows of the
4142:            DIAGONAL portion of the local submatrix (possibly different for each row)
4143:            or NULL, if d_nz is used to specify the nonzero structure.
4144:            The size of this array is equal to the number of local rows, i.e 'm'.
4145: .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
4146:            submatrix (same value is used for all local rows).
4147: -  o_nnz - array containing the number of nonzeros in the various rows of the
4148:            OFF-DIAGONAL portion of the local submatrix (possibly different for
4149:            each row) or NULL, if o_nz is used to specify the nonzero
4150:            structure. The size of this array is equal to the number
4151:            of local rows, i.e 'm'.

4153:    Output Parameter:
4154: .  A - the matrix

4156:    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
4157:    MatXXXXSetPreallocation() paradgm instead of this routine directly.
4158:    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]

4160:    Notes:
4161:    If the *_nnz parameter is given then the *_nz parameter is ignored

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

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

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

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

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

4184:    The DIAGONAL portion of the local submatrix on any given processor
4185:    is the submatrix corresponding to the rows and columns m,n
4186:    corresponding to the given processor. i.e diagonal matrix on
4187:    process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1]
4188:    etc. The remaining portion of the local submatrix [m x (N-n)]
4189:    constitute the OFF-DIAGONAL portion. The example below better
4190:    illustrates this concept.

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

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

4199:    When calling this routine with a single process communicator, a matrix of
4200:    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
4201:    type of communicator, use the construction mechanism
4202: .vb
4203:      MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...);
4204: .ve

4206: $     MatCreate(...,&A);
4207: $     MatSetType(A,MATMPIAIJ);
4208: $     MatSetSizes(A, m,n,M,N);
4209: $     MatMPIAIJSetPreallocation(A,...);

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

4215:    Options Database Keys:
4216: +  -mat_no_inode  - Do not use inodes
4217: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)



4221:    Example usage:

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

4228: .vb
4229:             1  2  0  |  0  3  0  |  0  4
4230:     Proc0   0  5  6  |  7  0  0  |  8  0
4231:             9  0 10  | 11  0  0  | 12  0
4232:     -------------------------------------
4233:            13  0 14  | 15 16 17  |  0  0
4234:     Proc1   0 18  0  | 19 20 21  |  0  0
4235:             0  0  0  | 22 23  0  | 24  0
4236:     -------------------------------------
4237:     Proc2  25 26 27  |  0  0 28  | 29  0
4238:            30  0  0  | 31 32 33  |  0 34
4239: .ve

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

4243: .vb
4244:       A B C
4245:       D E F
4246:       G H I
4247: .ve

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

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

4256:    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
4257:    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
4258:    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
4259:    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
4260:    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
4261:    matrix, ans [DF] as another SeqAIJ matrix.

4263:    When d_nz, o_nz parameters are specified, d_nz storage elements are
4264:    allocated for every row of the local diagonal submatrix, and o_nz
4265:    storage locations are allocated for every row of the OFF-DIAGONAL submat.
4266:    One way to choose d_nz and o_nz is to use the max nonzerors per local
4267:    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
4268:    In this case, the values of d_nz,o_nz are
4269: .vb
4270:      proc0 : dnz = 2, o_nz = 2
4271:      proc1 : dnz = 3, o_nz = 2
4272:      proc2 : dnz = 1, o_nz = 4
4273: .ve
4274:    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
4275:    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
4276:    for proc3. i.e we are using 12+15+10=37 storage locations to store
4277:    34 values.

4279:    When d_nnz, o_nnz parameters are specified, the storage is specified
4280:    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
4281:    In the above case the values for d_nnz,o_nnz are
4282: .vb
4283:      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
4284:      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
4285:      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
4286: .ve
4287:    Here the space allocated is sum of all the above values i.e 34, and
4288:    hence pre-allocation is perfect.

4290:    Level: intermediate

4292: .keywords: matrix, aij, compressed row, sparse, parallel

4294: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4295:           MATMPIAIJ, MatCreateMPIAIJWithArrays()
4296: @*/
4297: 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)
4298: {
4300:   PetscMPIInt    size;

4303:   MatCreate(comm,A);
4304:   MatSetSizes(*A,m,n,M,N);
4305:   MPI_Comm_size(comm,&size);
4306:   if (size > 1) {
4307:     MatSetType(*A,MATMPIAIJ);
4308:     MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);
4309:   } else {
4310:     MatSetType(*A,MATSEQAIJ);
4311:     MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);
4312:   }
4313:   return(0);
4314: }

4316: PetscErrorCode MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[])
4317: {
4318:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
4319:   PetscBool      flg;
4321: 
4323:   PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&flg);
4324:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"This function requires a MATMPIAIJ matrix as input");
4325:   if (Ad)     *Ad     = a->A;
4326:   if (Ao)     *Ao     = a->B;
4327:   if (colmap) *colmap = a->garray;
4328:   return(0);
4329: }

4331: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPIAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4332: {
4334:   PetscInt       m,N,i,rstart,nnz,Ii;
4335:   PetscInt       *indx;
4336:   PetscScalar    *values;

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

4343:     if (n == PETSC_DECIDE) {
4344:       PetscSplitOwnership(comm,&n,&N);
4345:     }
4346:     /* Check sum(n) = N */
4347:     MPIU_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);
4348:     if (sum != N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns %D != global columns %D",sum,N);

4350:     MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);
4351:     rstart -= m;

4353:     MatPreallocateInitialize(comm,m,n,dnz,onz);
4354:     for (i=0; i<m; i++) {
4355:       MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);
4356:       MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);
4357:       MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);
4358:     }

4360:     MatCreate(comm,outmat);
4361:     MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
4362:     MatGetBlockSizes(inmat,&bs,&cbs);
4363:     MatSetBlockSizes(*outmat,bs,cbs);
4364:     MatSetType(*outmat,MATAIJ);
4365:     MatSeqAIJSetPreallocation(*outmat,0,dnz);
4366:     MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);
4367:     MatPreallocateFinalize(dnz,onz);
4368:   }

4370:   /* numeric phase */
4371:   MatGetOwnershipRange(*outmat,&rstart,NULL);
4372:   for (i=0; i<m; i++) {
4373:     MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
4374:     Ii   = i + rstart;
4375:     MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);
4376:     MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
4377:   }
4378:   MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);
4379:   MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);
4380:   return(0);
4381: }

4383: PetscErrorCode MatFileSplit(Mat A,char *outfile)
4384: {
4385:   PetscErrorCode    ierr;
4386:   PetscMPIInt       rank;
4387:   PetscInt          m,N,i,rstart,nnz;
4388:   size_t            len;
4389:   const PetscInt    *indx;
4390:   PetscViewer       out;
4391:   char              *name;
4392:   Mat               B;
4393:   const PetscScalar *values;

4396:   MatGetLocalSize(A,&m,0);
4397:   MatGetSize(A,0,&N);
4398:   /* Should this be the type of the diagonal block of A? */
4399:   MatCreate(PETSC_COMM_SELF,&B);
4400:   MatSetSizes(B,m,N,m,N);
4401:   MatSetBlockSizesFromMats(B,A,A);
4402:   MatSetType(B,MATSEQAIJ);
4403:   MatSeqAIJSetPreallocation(B,0,NULL);
4404:   MatGetOwnershipRange(A,&rstart,0);
4405:   for (i=0; i<m; i++) {
4406:     MatGetRow(A,i+rstart,&nnz,&indx,&values);
4407:     MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);
4408:     MatRestoreRow(A,i+rstart,&nnz,&indx,&values);
4409:   }
4410:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
4411:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

4413:   MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);
4414:   PetscStrlen(outfile,&len);
4415:   PetscMalloc1(len+5,&name);
4416:   sprintf(name,"%s.%d",outfile,rank);
4417:   PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);
4418:   PetscFree(name);
4419:   MatView(B,out);
4420:   PetscViewerDestroy(&out);
4421:   MatDestroy(&B);
4422:   return(0);
4423: }

4425: PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
4426: {
4427:   PetscErrorCode      ierr;
4428:   Mat_Merge_SeqsToMPI *merge;
4429:   PetscContainer      container;

4432:   PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject*)&container);
4433:   if (container) {
4434:     PetscContainerGetPointer(container,(void**)&merge);
4435:     PetscFree(merge->id_r);
4436:     PetscFree(merge->len_s);
4437:     PetscFree(merge->len_r);
4438:     PetscFree(merge->bi);
4439:     PetscFree(merge->bj);
4440:     PetscFree(merge->buf_ri[0]);
4441:     PetscFree(merge->buf_ri);
4442:     PetscFree(merge->buf_rj[0]);
4443:     PetscFree(merge->buf_rj);
4444:     PetscFree(merge->coi);
4445:     PetscFree(merge->coj);
4446:     PetscFree(merge->owners_co);
4447:     PetscLayoutDestroy(&merge->rowmap);
4448:     PetscFree(merge);
4449:     PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);
4450:   }
4451:   MatDestroy_MPIAIJ(A);
4452:   return(0);
4453: }

4455:  #include <../src/mat/utils/freespace.h>
4456:  #include <petscbt.h>

4458: PetscErrorCode MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat)
4459: {
4460:   PetscErrorCode      ierr;
4461:   MPI_Comm            comm;
4462:   Mat_SeqAIJ          *a  =(Mat_SeqAIJ*)seqmat->data;
4463:   PetscMPIInt         size,rank,taga,*len_s;
4464:   PetscInt            N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj;
4465:   PetscInt            proc,m;
4466:   PetscInt            **buf_ri,**buf_rj;
4467:   PetscInt            k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
4468:   PetscInt            nrows,**buf_ri_k,**nextrow,**nextai;
4469:   MPI_Request         *s_waits,*r_waits;
4470:   MPI_Status          *status;
4471:   MatScalar           *aa=a->a;
4472:   MatScalar           **abuf_r,*ba_i;
4473:   Mat_Merge_SeqsToMPI *merge;
4474:   PetscContainer      container;

4477:   PetscObjectGetComm((PetscObject)mpimat,&comm);
4478:   PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);

4480:   MPI_Comm_size(comm,&size);
4481:   MPI_Comm_rank(comm,&rank);

4483:   PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject*)&container);
4484:   PetscContainerGetPointer(container,(void**)&merge);

4486:   bi     = merge->bi;
4487:   bj     = merge->bj;
4488:   buf_ri = merge->buf_ri;
4489:   buf_rj = merge->buf_rj;

4491:   PetscMalloc1(size,&status);
4492:   owners = merge->rowmap->range;
4493:   len_s  = merge->len_s;

4495:   /* send and recv matrix values */
4496:   /*-----------------------------*/
4497:   PetscObjectGetNewTag((PetscObject)mpimat,&taga);
4498:   PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);

4500:   PetscMalloc1(merge->nsend+1,&s_waits);
4501:   for (proc=0,k=0; proc<size; proc++) {
4502:     if (!len_s[proc]) continue;
4503:     i    = owners[proc];
4504:     MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);
4505:     k++;
4506:   }

4508:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,r_waits,status);}
4509:   if (merge->nsend) {MPI_Waitall(merge->nsend,s_waits,status);}
4510:   PetscFree(status);

4512:   PetscFree(s_waits);
4513:   PetscFree(r_waits);

4515:   /* insert mat values of mpimat */
4516:   /*----------------------------*/
4517:   PetscMalloc1(N,&ba_i);
4518:   PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);

4520:   for (k=0; k<merge->nrecv; k++) {
4521:     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
4522:     nrows       = *(buf_ri_k[k]);
4523:     nextrow[k]  = buf_ri_k[k]+1;  /* next row number of k-th recved i-structure */
4524:     nextai[k]   = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure  */
4525:   }

4527:   /* set values of ba */
4528:   m = merge->rowmap->n;
4529:   for (i=0; i<m; i++) {
4530:     arow = owners[rank] + i;
4531:     bj_i = bj+bi[i];  /* col indices of the i-th row of mpimat */
4532:     bnzi = bi[i+1] - bi[i];
4533:     PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));

4535:     /* add local non-zero vals of this proc's seqmat into ba */
4536:     anzi   = ai[arow+1] - ai[arow];
4537:     aj     = a->j + ai[arow];
4538:     aa     = a->a + ai[arow];
4539:     nextaj = 0;
4540:     for (j=0; nextaj<anzi; j++) {
4541:       if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
4542:         ba_i[j] += aa[nextaj++];
4543:       }
4544:     }

4546:     /* add received vals into ba */
4547:     for (k=0; k<merge->nrecv; k++) { /* k-th received message */
4548:       /* i-th row */
4549:       if (i == *nextrow[k]) {
4550:         anzi   = *(nextai[k]+1) - *nextai[k];
4551:         aj     = buf_rj[k] + *(nextai[k]);
4552:         aa     = abuf_r[k] + *(nextai[k]);
4553:         nextaj = 0;
4554:         for (j=0; nextaj<anzi; j++) {
4555:           if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
4556:             ba_i[j] += aa[nextaj++];
4557:           }
4558:         }
4559:         nextrow[k]++; nextai[k]++;
4560:       }
4561:     }
4562:     MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);
4563:   }
4564:   MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);
4565:   MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);

4567:   PetscFree(abuf_r[0]);
4568:   PetscFree(abuf_r);
4569:   PetscFree(ba_i);
4570:   PetscFree3(buf_ri_k,nextrow,nextai);
4571:   PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);
4572:   return(0);
4573: }

4575: PetscErrorCode  MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
4576: {
4577:   PetscErrorCode      ierr;
4578:   Mat                 B_mpi;
4579:   Mat_SeqAIJ          *a=(Mat_SeqAIJ*)seqmat->data;
4580:   PetscMPIInt         size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
4581:   PetscInt            **buf_rj,**buf_ri,**buf_ri_k;
4582:   PetscInt            M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j;
4583:   PetscInt            len,proc,*dnz,*onz,bs,cbs;
4584:   PetscInt            k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
4585:   PetscInt            nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
4586:   MPI_Request         *si_waits,*sj_waits,*ri_waits,*rj_waits;
4587:   MPI_Status          *status;
4588:   PetscFreeSpaceList  free_space=NULL,current_space=NULL;
4589:   PetscBT             lnkbt;
4590:   Mat_Merge_SeqsToMPI *merge;
4591:   PetscContainer      container;

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

4596:   /* make sure it is a PETSc comm */
4597:   PetscCommDuplicate(comm,&comm,NULL);
4598:   MPI_Comm_size(comm,&size);
4599:   MPI_Comm_rank(comm,&rank);

4601:   PetscNew(&merge);
4602:   PetscMalloc1(size,&status);

4604:   /* determine row ownership */
4605:   /*---------------------------------------------------------*/
4606:   PetscLayoutCreate(comm,&merge->rowmap);
4607:   PetscLayoutSetLocalSize(merge->rowmap,m);
4608:   PetscLayoutSetSize(merge->rowmap,M);
4609:   PetscLayoutSetBlockSize(merge->rowmap,1);
4610:   PetscLayoutSetUp(merge->rowmap);
4611:   PetscMalloc1(size,&len_si);
4612:   PetscMalloc1(size,&merge->len_s);

4614:   m      = merge->rowmap->n;
4615:   owners = merge->rowmap->range;

4617:   /* determine the number of messages to send, their lengths */
4618:   /*---------------------------------------------------------*/
4619:   len_s = merge->len_s;

4621:   len          = 0; /* length of buf_si[] */
4622:   merge->nsend = 0;
4623:   for (proc=0; proc<size; proc++) {
4624:     len_si[proc] = 0;
4625:     if (proc == rank) {
4626:       len_s[proc] = 0;
4627:     } else {
4628:       len_si[proc] = owners[proc+1] - owners[proc] + 1;
4629:       len_s[proc]  = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
4630:     }
4631:     if (len_s[proc]) {
4632:       merge->nsend++;
4633:       nrows = 0;
4634:       for (i=owners[proc]; i<owners[proc+1]; i++) {
4635:         if (ai[i+1] > ai[i]) nrows++;
4636:       }
4637:       len_si[proc] = 2*(nrows+1);
4638:       len         += len_si[proc];
4639:     }
4640:   }

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

4647:   /* post the Irecv of j-structure */
4648:   /*-------------------------------*/
4649:   PetscCommGetNewTag(comm,&tagj);
4650:   PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);

4652:   /* post the Isend of j-structure */
4653:   /*--------------------------------*/
4654:   PetscMalloc2(merge->nsend,&si_waits,merge->nsend,&sj_waits);

4656:   for (proc=0, k=0; proc<size; proc++) {
4657:     if (!len_s[proc]) continue;
4658:     i    = owners[proc];
4659:     MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);
4660:     k++;
4661:   }

4663:   /* receives and sends of j-structure are complete */
4664:   /*------------------------------------------------*/
4665:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,rj_waits,status);}
4666:   if (merge->nsend) {MPI_Waitall(merge->nsend,sj_waits,status);}

4668:   /* send and recv i-structure */
4669:   /*---------------------------*/
4670:   PetscCommGetNewTag(comm,&tagi);
4671:   PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);

4673:   PetscMalloc1(len+1,&buf_s);
4674:   buf_si = buf_s;  /* points to the beginning of k-th msg to be sent */
4675:   for (proc=0,k=0; proc<size; proc++) {
4676:     if (!len_s[proc]) continue;
4677:     /* form outgoing message for i-structure:
4678:          buf_si[0]:                 nrows to be sent
4679:                [1:nrows]:           row index (global)
4680:                [nrows+1:2*nrows+1]: i-structure index
4681:     */
4682:     /*-------------------------------------------*/
4683:     nrows       = len_si[proc]/2 - 1;
4684:     buf_si_i    = buf_si + nrows+1;
4685:     buf_si[0]   = nrows;
4686:     buf_si_i[0] = 0;
4687:     nrows       = 0;
4688:     for (i=owners[proc]; i<owners[proc+1]; i++) {
4689:       anzi = ai[i+1] - ai[i];
4690:       if (anzi) {
4691:         buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
4692:         buf_si[nrows+1]   = i-owners[proc]; /* local row index */
4693:         nrows++;
4694:       }
4695:     }
4696:     MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);
4697:     k++;
4698:     buf_si += len_si[proc];
4699:   }

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

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

4709:   PetscFree(len_si);
4710:   PetscFree(len_ri);
4711:   PetscFree(rj_waits);
4712:   PetscFree2(si_waits,sj_waits);
4713:   PetscFree(ri_waits);
4714:   PetscFree(buf_s);
4715:   PetscFree(status);

4717:   /* compute a local seq matrix in each processor */
4718:   /*----------------------------------------------*/
4719:   /* allocate bi array and free space for accumulating nonzero column info */
4720:   PetscMalloc1(m+1,&bi);
4721:   bi[0] = 0;

4723:   /* create and initialize a linked list */
4724:   nlnk = N+1;
4725:   PetscLLCreate(N,N,nlnk,lnk,lnkbt);

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

4731:   current_space = free_space;

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

4736:   for (k=0; k<merge->nrecv; k++) {
4737:     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
4738:     nrows       = *buf_ri_k[k];
4739:     nextrow[k]  = buf_ri_k[k] + 1;  /* next row number of k-th recved i-structure */
4740:     nextai[k]   = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure  */
4741:   }

4743:   MatPreallocateInitialize(comm,m,n,dnz,onz);
4744:   len  = 0;
4745:   for (i=0; i<m; i++) {
4746:     bnzi = 0;
4747:     /* add local non-zero cols of this proc's seqmat into lnk */
4748:     arow  = owners[rank] + i;
4749:     anzi  = ai[arow+1] - ai[arow];
4750:     aj    = a->j + ai[arow];
4751:     PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);
4752:     bnzi += nlnk;
4753:     /* add received col data into lnk */
4754:     for (k=0; k<merge->nrecv; k++) { /* k-th received message */
4755:       if (i == *nextrow[k]) { /* i-th row */
4756:         anzi  = *(nextai[k]+1) - *nextai[k];
4757:         aj    = buf_rj[k] + *nextai[k];
4758:         PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);
4759:         bnzi += nlnk;
4760:         nextrow[k]++; nextai[k]++;
4761:       }
4762:     }
4763:     if (len < bnzi) len = bnzi;  /* =max(bnzi) */

4765:     /* if free space is not available, make more free space */
4766:     if (current_space->local_remaining<bnzi) {
4767:       PetscFreeSpaceGet(PetscIntSumTruncate(bnzi,current_space->total_array_size),&current_space);
4768:       nspacedouble++;
4769:     }
4770:     /* copy data into free space, then initialize lnk */
4771:     PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);
4772:     MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);

4774:     current_space->array           += bnzi;
4775:     current_space->local_used      += bnzi;
4776:     current_space->local_remaining -= bnzi;

4778:     bi[i+1] = bi[i] + bnzi;
4779:   }

4781:   PetscFree3(buf_ri_k,nextrow,nextai);

4783:   PetscMalloc1(bi[m]+1,&bj);
4784:   PetscFreeSpaceContiguous(&free_space,bj);
4785:   PetscLLDestroy(lnk,lnkbt);

4787:   /* create symbolic parallel matrix B_mpi */
4788:   /*---------------------------------------*/
4789:   MatGetBlockSizes(seqmat,&bs,&cbs);
4790:   MatCreate(comm,&B_mpi);
4791:   if (n==PETSC_DECIDE) {
4792:     MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);
4793:   } else {
4794:     MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
4795:   }
4796:   MatSetBlockSizes(B_mpi,bs,cbs);
4797:   MatSetType(B_mpi,MATMPIAIJ);
4798:   MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);
4799:   MatPreallocateFinalize(dnz,onz);
4800:   MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);

4802:   /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */
4803:   B_mpi->assembled    = PETSC_FALSE;
4804:   B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI;
4805:   merge->bi           = bi;
4806:   merge->bj           = bj;
4807:   merge->buf_ri       = buf_ri;
4808:   merge->buf_rj       = buf_rj;
4809:   merge->coi          = NULL;
4810:   merge->coj          = NULL;
4811:   merge->owners_co    = NULL;

4813:   PetscCommDestroy(&comm);

4815:   /* attach the supporting struct to B_mpi for reuse */
4816:   PetscContainerCreate(PETSC_COMM_SELF,&container);
4817:   PetscContainerSetPointer(container,merge);
4818:   PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);
4819:   PetscContainerDestroy(&container);
4820:   *mpimat = B_mpi;

4822:   PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);
4823:   return(0);
4824: }

4826: /*@C
4827:       MatCreateMPIAIJSumSeqAIJ - Creates a MATMPIAIJ matrix by adding sequential
4828:                  matrices from each processor

4830:     Collective on MPI_Comm

4832:    Input Parameters:
4833: +    comm - the communicators the parallel matrix will live on
4834: .    seqmat - the input sequential matrices
4835: .    m - number of local rows (or PETSC_DECIDE)
4836: .    n - number of local columns (or PETSC_DECIDE)
4837: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

4839:    Output Parameter:
4840: .    mpimat - the parallel matrix generated

4842:     Level: advanced

4844:    Notes:
4845:      The dimensions of the sequential matrix in each processor MUST be the same.
4846:      The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
4847:      destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
4848: @*/
4849: PetscErrorCode MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
4850: {
4852:   PetscMPIInt    size;

4855:   MPI_Comm_size(comm,&size);
4856:   if (size == 1) {
4857:     PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);
4858:     if (scall == MAT_INITIAL_MATRIX) {
4859:       MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);
4860:     } else {
4861:       MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);
4862:     }
4863:     PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);
4864:     return(0);
4865:   }
4866:   PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);
4867:   if (scall == MAT_INITIAL_MATRIX) {
4868:     MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);
4869:   }
4870:   MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);
4871:   PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);
4872:   return(0);
4873: }

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

4880:     Not Collective

4882:    Input Parameters:
4883: +    A - the matrix
4884: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

4886:    Output Parameter:
4887: .    A_loc - the local sequential matrix generated

4889:     Level: developer

4891: .seealso: MatGetOwnerShipRange(), MatMPIAIJGetLocalMatCondensed()

4893: @*/
4894: PetscErrorCode MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
4895: {
4897:   Mat_MPIAIJ     *mpimat=(Mat_MPIAIJ*)A->data;
4898:   Mat_SeqAIJ     *mat,*a,*b;
4899:   PetscInt       *ai,*aj,*bi,*bj,*cmap=mpimat->garray;
4900:   MatScalar      *aa,*ba,*cam;
4901:   PetscScalar    *ca;
4902:   PetscInt       am=A->rmap->n,i,j,k,cstart=A->cmap->rstart;
4903:   PetscInt       *ci,*cj,col,ncols_d,ncols_o,jo;
4904:   PetscBool      match;
4905:   MPI_Comm       comm;
4906:   PetscMPIInt    size;

4909:   PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);
4910:   if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MATMPIAIJ matrix as input");
4911:   PetscObjectGetComm((PetscObject)A,&comm);
4912:   MPI_Comm_size(comm,&size);
4913:   if (size == 1 && scall == MAT_REUSE_MATRIX) return(0);

4915:   PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);
4916:   a = (Mat_SeqAIJ*)(mpimat->A)->data;
4917:   b = (Mat_SeqAIJ*)(mpimat->B)->data;
4918:   ai = a->i; aj = a->j; bi = b->i; bj = b->j;
4919:   aa = a->a; ba = b->a;
4920:   if (scall == MAT_INITIAL_MATRIX) {
4921:     if (size == 1) {
4922:       MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ai,aj,aa,A_loc);
4923:       return(0);
4924:     }

4926:     PetscMalloc1(1+am,&ci);
4927:     ci[0] = 0;
4928:     for (i=0; i<am; i++) {
4929:       ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
4930:     }
4931:     PetscMalloc1(1+ci[am],&cj);
4932:     PetscMalloc1(1+ci[am],&ca);
4933:     k    = 0;
4934:     for (i=0; i<am; i++) {
4935:       ncols_o = bi[i+1] - bi[i];
4936:       ncols_d = ai[i+1] - ai[i];
4937:       /* off-diagonal portion of A */
4938:       for (jo=0; jo<ncols_o; jo++) {
4939:         col = cmap[*bj];
4940:         if (col >= cstart) break;
4941:         cj[k]   = col; bj++;
4942:         ca[k++] = *ba++;
4943:       }
4944:       /* diagonal portion of A */
4945:       for (j=0; j<ncols_d; j++) {
4946:         cj[k]   = cstart + *aj++;
4947:         ca[k++] = *aa++;
4948:       }
4949:       /* off-diagonal portion of A */
4950:       for (j=jo; j<ncols_o; j++) {
4951:         cj[k]   = cmap[*bj++];
4952:         ca[k++] = *ba++;
4953:       }
4954:     }
4955:     /* put together the new matrix */
4956:     MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);
4957:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
4958:     /* Since these are PETSc arrays, change flags to free them as necessary. */
4959:     mat          = (Mat_SeqAIJ*)(*A_loc)->data;
4960:     mat->free_a  = PETSC_TRUE;
4961:     mat->free_ij = PETSC_TRUE;
4962:     mat->nonew   = 0;
4963:   } else if (scall == MAT_REUSE_MATRIX) {
4964:     mat=(Mat_SeqAIJ*)(*A_loc)->data;
4965:     ci = mat->i; cj = mat->j; cam = mat->a;
4966:     for (i=0; i<am; i++) {
4967:       /* off-diagonal portion of A */
4968:       ncols_o = bi[i+1] - bi[i];
4969:       for (jo=0; jo<ncols_o; jo++) {
4970:         col = cmap[*bj];
4971:         if (col >= cstart) break;
4972:         *cam++ = *ba++; bj++;
4973:       }
4974:       /* diagonal portion of A */
4975:       ncols_d = ai[i+1] - ai[i];
4976:       for (j=0; j<ncols_d; j++) *cam++ = *aa++;
4977:       /* off-diagonal portion of A */
4978:       for (j=jo; j<ncols_o; j++) {
4979:         *cam++ = *ba++; bj++;
4980:       }
4981:     }
4982:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
4983:   PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);
4984:   return(0);
4985: }

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

4990:     Not Collective

4992:    Input Parameters:
4993: +    A - the matrix
4994: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
4995: -    row, col - index sets of rows and columns to extract (or NULL)

4997:    Output Parameter:
4998: .    A_loc - the local sequential matrix generated

5000:     Level: developer

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

5004: @*/
5005: PetscErrorCode MatMPIAIJGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
5006: {
5007:   Mat_MPIAIJ     *a=(Mat_MPIAIJ*)A->data;
5009:   PetscInt       i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
5010:   IS             isrowa,iscola;
5011:   Mat            *aloc;
5012:   PetscBool      match;

5015:   PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);
5016:   if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MATMPIAIJ matrix as input");
5017:   PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);
5018:   if (!row) {
5019:     start = A->rmap->rstart; end = A->rmap->rend;
5020:     ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);
5021:   } else {
5022:     isrowa = *row;
5023:   }
5024:   if (!col) {
5025:     start = A->cmap->rstart;
5026:     cmap  = a->garray;
5027:     nzA   = a->A->cmap->n;
5028:     nzB   = a->B->cmap->n;
5029:     PetscMalloc1(nzA+nzB, &idx);
5030:     ncols = 0;
5031:     for (i=0; i<nzB; i++) {
5032:       if (cmap[i] < start) idx[ncols++] = cmap[i];
5033:       else break;
5034:     }
5035:     imark = i;
5036:     for (i=0; i<nzA; i++) idx[ncols++] = start + i;
5037:     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
5038:     ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);
5039:   } else {
5040:     iscola = *col;
5041:   }
5042:   if (scall != MAT_INITIAL_MATRIX) {
5043:     PetscMalloc1(1,&aloc);
5044:     aloc[0] = *A_loc;
5045:   }
5046:   MatCreateSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);
5047:   *A_loc = aloc[0];
5048:   PetscFree(aloc);
5049:   if (!row) {
5050:     ISDestroy(&isrowa);
5051:   }
5052:   if (!col) {
5053:     ISDestroy(&iscola);
5054:   }
5055:   PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);
5056:   return(0);
5057: }

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

5062:     Collective on Mat

5064:    Input Parameters:
5065: +    A,B - the matrices in mpiaij format
5066: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5067: -    rowb, colb - index sets of rows and columns of B to extract (or NULL)

5069:    Output Parameter:
5070: +    rowb, colb - index sets of rows and columns of B to extract
5071: -    B_seq - the sequential matrix generated

5073:     Level: developer

5075: @*/
5076: PetscErrorCode MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,Mat *B_seq)
5077: {
5078:   Mat_MPIAIJ     *a=(Mat_MPIAIJ*)A->data;
5080:   PetscInt       *idx,i,start,ncols,nzA,nzB,*cmap,imark;
5081:   IS             isrowb,iscolb;
5082:   Mat            *bseq=NULL;

5085:   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5086:     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);
5087:   }
5088:   PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);

5090:   if (scall == MAT_INITIAL_MATRIX) {
5091:     start = A->cmap->rstart;
5092:     cmap  = a->garray;
5093:     nzA   = a->A->cmap->n;
5094:     nzB   = a->B->cmap->n;
5095:     PetscMalloc1(nzA+nzB, &idx);
5096:     ncols = 0;
5097:     for (i=0; i<nzB; i++) {  /* row < local row index */
5098:       if (cmap[i] < start) idx[ncols++] = cmap[i];
5099:       else break;
5100:     }
5101:     imark = i;
5102:     for (i=0; i<nzA; i++) idx[ncols++] = start + i;  /* local rows */
5103:     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
5104:     ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);
5105:     ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);
5106:   } else {
5107:     if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
5108:     isrowb  = *rowb; iscolb = *colb;
5109:     PetscMalloc1(1,&bseq);
5110:     bseq[0] = *B_seq;
5111:   }
5112:   MatCreateSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);
5113:   *B_seq = bseq[0];
5114:   PetscFree(bseq);
5115:   if (!rowb) {
5116:     ISDestroy(&isrowb);
5117:   } else {
5118:     *rowb = isrowb;
5119:   }
5120:   if (!colb) {
5121:     ISDestroy(&iscolb);
5122:   } else {
5123:     *colb = iscolb;
5124:   }
5125:   PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);
5126:   return(0);
5127: }

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

5133:     Collective on Mat

5135:    Input Parameters:
5136: +    A,B - the matrices in mpiaij format
5137: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

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

5145:     Level: developer

5147: */
5148: PetscErrorCode MatGetBrowsOfAoCols_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscInt **startsj_s,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth)
5149: {
5150:   VecScatter_MPI_General *gen_to,*gen_from;
5151:   PetscErrorCode         ierr;
5152:   Mat_MPIAIJ             *a=(Mat_MPIAIJ*)A->data;
5153:   Mat_SeqAIJ             *b_oth;
5154:   VecScatter             ctx;
5155:   MPI_Comm               comm;
5156:   PetscMPIInt            *rprocs,*sprocs,tag,rank;
5157:   PetscInt               *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj;
5158:   PetscInt               *rvalues,*svalues,*cols,sbs,rbs;
5159:   PetscScalar              *b_otha,*bufa,*bufA,*vals;
5160:   PetscInt               i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len;
5161:   MPI_Request            *rwaits = NULL,*swaits = NULL;
5162:   MPI_Status             *sstatus,rstatus;
5163:   PetscMPIInt            jj,size;
5164:   VecScatterType         type;
5165:   PetscBool              mpi1;

5168:   PetscObjectGetComm((PetscObject)A,&comm);
5169:   MPI_Comm_size(comm,&size);

5171:   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5172:     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);
5173:   }
5174:   PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);
5175:   MPI_Comm_rank(comm,&rank);

5177:   if (size == 1) {
5178:     startsj_s = NULL;
5179:     bufa_ptr  = NULL;
5180:     *B_oth    = NULL;
5181:     return(0);
5182:   }

5184:   ctx = a->Mvctx;
5185:   VecScatterGetType(ctx,&type);
5186:   PetscStrcmp(type,"mpi1",&mpi1);
5187:   if (!mpi1) {
5188:     /* a->Mvctx is not type MPI1 which is not implemented for Mat-Mat ops,
5189:      thus create a->Mvctx_mpi1 */
5190:     if (!a->Mvctx_mpi1) {
5191:       a->Mvctx_mpi1_flg = PETSC_TRUE;
5192:       MatSetUpMultiply_MPIAIJ(A);
5193:     }
5194:     ctx = a->Mvctx_mpi1;
5195:   }
5196:   tag = ((PetscObject)ctx)->tag;

5198:   gen_to   = (VecScatter_MPI_General*)ctx->todata;
5199:   gen_from = (VecScatter_MPI_General*)ctx->fromdata;
5200:   nrecvs   = gen_from->n;
5201:   nsends   = gen_to->n;

5203:   PetscMalloc2(nrecvs,&rwaits,nsends,&swaits);
5204:   srow    = gen_to->indices;    /* local row index to be sent */
5205:   sstarts = gen_to->starts;
5206:   sprocs  = gen_to->procs;
5207:   sstatus = gen_to->sstatus;
5208:   sbs     = gen_to->bs;
5209:   rstarts = gen_from->starts;
5210:   rprocs  = gen_from->procs;
5211:   rbs     = gen_from->bs;

5213:   if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
5214:   if (scall == MAT_INITIAL_MATRIX) {
5215:     /* i-array */
5216:     /*---------*/
5217:     /*  post receives */
5218:     PetscMalloc1(rbs*(rstarts[nrecvs] - rstarts[0]),&rvalues);
5219:     for (i=0; i<nrecvs; i++) {
5220:       rowlen = rvalues + rstarts[i]*rbs;
5221:       nrows  = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */
5222:       MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
5223:     }

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

5228:     sstartsj[0] = 0;
5229:     rstartsj[0] = 0;
5230:     len         = 0; /* total length of j or a array to be sent */
5231:     k           = 0;
5232:     PetscMalloc1(sbs*(sstarts[nsends] - sstarts[0]),&svalues);
5233:     for (i=0; i<nsends; i++) {
5234:       rowlen = svalues + sstarts[i]*sbs;
5235:       nrows  = sstarts[i+1]-sstarts[i]; /* num of block rows */
5236:       for (j=0; j<nrows; j++) {
5237:         row = srow[k] + B->rmap->range[rank]; /* global row idx */
5238:         for (l=0; l<sbs; l++) {
5239:           MatGetRow_MPIAIJ(B,row+l,&ncols,NULL,NULL); /* rowlength */

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

5243:           len += ncols;
5244:           MatRestoreRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);
5245:         }
5246:         k++;
5247:       }
5248:       MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);

5250:       sstartsj[i+1] = len;  /* starting point of (i+1)-th outgoing msg in bufj and bufa */
5251:     }
5252:     /* recvs and sends of i-array are completed */
5253:     i = nrecvs;
5254:     while (i--) {
5255:       MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);
5256:     }
5257:     if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
5258:     PetscFree(svalues);

5260:     /* allocate buffers for sending j and a arrays */
5261:     PetscMalloc1(len+1,&bufj);
5262:     PetscMalloc1(len+1,&bufa);

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

5267:     b_othi[0] = 0;
5268:     len       = 0; /* total length of j or a array to be received */
5269:     k         = 0;
5270:     for (i=0; i<nrecvs; i++) {
5271:       rowlen = rvalues + rstarts[i]*rbs;
5272:       nrows  = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be received */
5273:       for (j=0; j<nrows; j++) {
5274:         b_othi[k+1] = b_othi[k] + rowlen[j];
5275:         PetscIntSumError(rowlen[j],len,&len);
5276:         k++;
5277:       }
5278:       rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
5279:     }
5280:     PetscFree(rvalues);

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

5286:     /* j-array */
5287:     /*---------*/
5288:     /*  post receives of j-array */
5289:     for (i=0; i<nrecvs; i++) {
5290:       nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
5291:       MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
5292:     }

5294:     /* pack the outgoing message j-array */
5295:     k = 0;
5296:     for (i=0; i<nsends; i++) {
5297:       nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5298:       bufJ  = bufj+sstartsj[i];
5299:       for (j=0; j<nrows; j++) {
5300:         row = srow[k++] + B->rmap->range[rank];  /* global row idx */
5301:         for (ll=0; ll<sbs; ll++) {
5302:           MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);
5303:           for (l=0; l<ncols; l++) {
5304:             *bufJ++ = cols[l];
5305:           }
5306:           MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);
5307:         }
5308:       }
5309:       MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);
5310:     }

5312:     /* recvs and sends of j-array are completed */
5313:     i = nrecvs;
5314:     while (i--) {
5315:       MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);
5316:     }
5317:     if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
5318:   } else if (scall == MAT_REUSE_MATRIX) {
5319:     sstartsj = *startsj_s;
5320:     rstartsj = *startsj_r;
5321:     bufa     = *bufa_ptr;
5322:     b_oth    = (Mat_SeqAIJ*)(*B_oth)->data;
5323:     b_otha   = b_oth->a;
5324:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");

5326:   /* a-array */
5327:   /*---------*/
5328:   /*  post receives of a-array */
5329:   for (i=0; i<nrecvs; i++) {
5330:     nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
5331:     MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);
5332:   }

5334:   /* pack the outgoing message a-array */
5335:   k = 0;
5336:   for (i=0; i<nsends; i++) {
5337:     nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5338:     bufA  = bufa+sstartsj[i];
5339:     for (j=0; j<nrows; j++) {
5340:       row = srow[k++] + B->rmap->range[rank];  /* global row idx */
5341:       for (ll=0; ll<sbs; ll++) {
5342:         MatGetRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);
5343:         for (l=0; l<ncols; l++) {
5344:           *bufA++ = vals[l];
5345:         }
5346:         MatRestoreRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);
5347:       }
5348:     }
5349:     MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);
5350:   }
5351:   /* recvs and sends of a-array are completed */
5352:   i = nrecvs;
5353:   while (i--) {
5354:     MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);
5355:   }
5356:   if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
5357:   PetscFree2(rwaits,swaits);

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

5363:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5364:     /* Since these are PETSc arrays, change flags to free them as necessary. */
5365:     b_oth          = (Mat_SeqAIJ*)(*B_oth)->data;
5366:     b_oth->free_a  = PETSC_TRUE;
5367:     b_oth->free_ij = PETSC_TRUE;
5368:     b_oth->nonew   = 0;

5370:     PetscFree(bufj);
5371:     if (!startsj_s || !bufa_ptr) {
5372:       PetscFree2(sstartsj,rstartsj);
5373:       PetscFree(bufa_ptr);
5374:     } else {
5375:       *startsj_s = sstartsj;
5376:       *startsj_r = rstartsj;
5377:       *bufa_ptr  = bufa;
5378:     }
5379:   }
5380:   PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);
5381:   return(0);
5382: }

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

5387:   Not Collective

5389:   Input Parameters:
5390: . A - The matrix in mpiaij format

5392:   Output Parameter:
5393: + lvec - The local vector holding off-process values from the argument to a matrix-vector product
5394: . colmap - A map from global column index to local index into lvec
5395: - multScatter - A scatter from the argument of a matrix-vector product to lvec

5397:   Level: developer

5399: @*/
5400: #if defined(PETSC_USE_CTABLE)
5401: PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
5402: #else
5403: PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
5404: #endif
5405: {
5406:   Mat_MPIAIJ *a;

5413:   a = (Mat_MPIAIJ*) A->data;
5414:   if (lvec) *lvec = a->lvec;
5415:   if (colmap) *colmap = a->colmap;
5416:   if (multScatter) *multScatter = a->Mvctx;
5417:   return(0);
5418: }

5420: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJCRL(Mat,MatType,MatReuse,Mat*);
5421: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJPERM(Mat,MatType,MatReuse,Mat*);
5422: #if defined(PETSC_HAVE_MKL_SPARSE)
5423: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJMKL(Mat,MatType,MatReuse,Mat*);
5424: #endif
5425: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPISBAIJ(Mat,MatType,MatReuse,Mat*);
5426: #if defined(PETSC_HAVE_ELEMENTAL)
5427: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
5428: #endif
5429: #if defined(PETSC_HAVE_HYPRE)
5430: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat,MatType,MatReuse,Mat*);
5431: PETSC_INTERN PetscErrorCode MatMatMatMult_Transpose_AIJ_AIJ(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
5432: #endif
5433: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_IS(Mat,MatType,MatReuse,Mat*);
5434: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPISELL(Mat,MatType,MatReuse,Mat*);

5436: /*
5437:     Computes (B'*A')' since computing B*A directly is untenable

5439:                n                       p                          p
5440:         (              )       (              )         (                  )
5441:       m (      A       )  *  n (       B      )   =   m (         C        )
5442:         (              )       (              )         (                  )

5444: */
5445: PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C)
5446: {
5448:   Mat            At,Bt,Ct;

5451:   MatTranspose(A,MAT_INITIAL_MATRIX,&At);
5452:   MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);
5453:   MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);
5454:   MatDestroy(&At);
5455:   MatDestroy(&Bt);
5456:   MatTranspose(Ct,MAT_REUSE_MATRIX,&C);
5457:   MatDestroy(&Ct);
5458:   return(0);
5459: }

5461: PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
5462: {
5464:   PetscInt       m=A->rmap->n,n=B->cmap->n;
5465:   Mat            Cmat;

5468:   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);
5469:   MatCreate(PetscObjectComm((PetscObject)A),&Cmat);
5470:   MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
5471:   MatSetBlockSizesFromMats(Cmat,A,B);
5472:   MatSetType(Cmat,MATMPIDENSE);
5473:   MatMPIDenseSetPreallocation(Cmat,NULL);
5474:   MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);
5475:   MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);

5477:   Cmat->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIAIJ;

5479:   *C = Cmat;
5480:   return(0);
5481: }

5483: /* ----------------------------------------------------------------*/
5484: PETSC_INTERN PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
5485: {

5489:   if (scall == MAT_INITIAL_MATRIX) {
5490:     PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
5491:     MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);
5492:     PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
5493:   }
5494:   PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
5495:   MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);
5496:   PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
5497:   return(0);
5498: }

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

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

5506:   Level: beginner

5508: .seealso: MatCreateAIJ()
5509: M*/

5511: PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJ(Mat B)
5512: {
5513:   Mat_MPIAIJ     *b;
5515:   PetscMPIInt    size;

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

5520:   PetscNewLog(B,&b);
5521:   B->data       = (void*)b;
5522:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
5523:   B->assembled  = PETSC_FALSE;
5524:   B->insertmode = NOT_SET_VALUES;
5525:   b->size       = size;

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

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

5532:   b->donotstash  = PETSC_FALSE;
5533:   b->colmap      = 0;
5534:   b->garray      = 0;
5535:   b->roworiented = PETSC_TRUE;

5537:   /* stuff used for matrix vector multiply */
5538:   b->lvec  = NULL;
5539:   b->Mvctx = NULL;

5541:   /* stuff for MatGetRow() */
5542:   b->rowindices   = 0;
5543:   b->rowvalues    = 0;
5544:   b->getrowactive = PETSC_FALSE;

5546:   /* flexible pointer used in CUSP/CUSPARSE classes */
5547:   b->spptr = NULL;

5549:   PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetUseScalableIncreaseOverlap_C",MatMPIAIJSetUseScalableIncreaseOverlap_MPIAIJ);
5550:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIAIJ);
5551:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIAIJ);
5552:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_MPIAIJ);
5553:   PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",MatMPIAIJSetPreallocation_MPIAIJ);
5554:   PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_MPIAIJ);
5555:   PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",MatMPIAIJSetPreallocationCSR_MPIAIJ);
5556:   PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIAIJ);
5557:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C",MatConvert_MPIAIJ_MPIAIJPERM);
5558: #if defined(PETSC_HAVE_MKL_SPARSE)
5559:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijmkl_C",MatConvert_MPIAIJ_MPIAIJMKL);
5560: #endif
5561:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C",MatConvert_MPIAIJ_MPIAIJCRL);
5562:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C",MatConvert_MPIAIJ_MPISBAIJ);
5563: #if defined(PETSC_HAVE_ELEMENTAL)
5564:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_elemental_C",MatConvert_MPIAIJ_Elemental);
5565: #endif
5566: #if defined(PETSC_HAVE_HYPRE)
5567:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_hypre_C",MatConvert_AIJ_HYPRE);
5568: #endif
5569:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_is_C",MatConvert_MPIAIJ_IS);
5570:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpisell_C",MatConvert_MPIAIJ_MPISELL);
5571:   PetscObjectComposeFunction((PetscObject)B,"MatMatMult_mpidense_mpiaij_C",MatMatMult_MPIDense_MPIAIJ);
5572:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C",MatMatMultSymbolic_MPIDense_MPIAIJ);
5573:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C",MatMatMultNumeric_MPIDense_MPIAIJ);
5574: #if defined(PETSC_HAVE_HYPRE)
5575:   PetscObjectComposeFunction((PetscObject)B,"MatMatMatMult_transpose_mpiaij_mpiaij_C",MatMatMatMult_Transpose_AIJ_AIJ);
5576: #endif
5577:   PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);
5578:   return(0);
5579: }

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

5585:    Collective on MPI_Comm

5587:    Input Parameters:
5588: +  comm - MPI communicator
5589: .  m - number of local rows (Cannot be PETSC_DECIDE)
5590: .  n - This value should be the same as the local size used in creating the
5591:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
5592:        calculated if N is given) For square matrices n is almost always m.
5593: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
5594: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
5595: .   i - row indices for "diagonal" portion of matrix
5596: .   j - column indices
5597: .   a - matrix values
5598: .   oi - row indices for "off-diagonal" portion of matrix
5599: .   oj - column indices
5600: -   oa - matrix values

5602:    Output Parameter:
5603: .   mat - the matrix

5605:    Level: advanced

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

5611:        The i and j indices are 0 based

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

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

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

5624: .keywords: matrix, aij, compressed row, sparse, parallel

5626: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
5627:           MATMPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays()
5628: @*/
5629: 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)
5630: {
5632:   Mat_MPIAIJ     *maij;

5635:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
5636:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
5637:   if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0");
5638:   MatCreate(comm,mat);
5639:   MatSetSizes(*mat,m,n,M,N);
5640:   MatSetType(*mat,MATMPIAIJ);
5641:   maij = (Mat_MPIAIJ*) (*mat)->data;

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

5645:   PetscLayoutSetUp((*mat)->rmap);
5646:   PetscLayoutSetUp((*mat)->cmap);

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

5651:   MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);
5652:   MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);
5653:   MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);
5654:   MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);

5656:   MatSetOption(*mat,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);
5657:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
5658:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
5659:   MatSetOption(*mat,MAT_NO_OFF_PROC_ENTRIES,PETSC_FALSE);
5660:   MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
5661:   return(0);
5662: }

5664: /*
5665:     Special version for direct calls from Fortran
5666: */
5667:  #include <petsc/private/fortranimpl.h>

5669: /* Change these macros so can be used in void function */
5670: #undef CHKERRQ
5671: #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr)
5672: #undef SETERRQ2
5673: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
5674: #undef SETERRQ3
5675: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
5676: #undef SETERRQ
5677: #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr)

5679: #if defined(PETSC_HAVE_FORTRAN_CAPS)
5680: #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
5681: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
5682: #define matsetvaluesmpiaij_ matsetvaluesmpiaij
5683: #else
5684: #endif
5685: PETSC_EXTERN void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr)
5686: {
5687:   Mat            mat  = *mmat;
5688:   PetscInt       m    = *mm, n = *mn;
5689:   InsertMode     addv = *maddv;
5690:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
5691:   PetscScalar    value;

5694:   MatCheckPreallocated(mat,1);
5695:   if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;

5697: #if defined(PETSC_USE_DEBUG)
5698:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
5699: #endif
5700:   {
5701:     PetscInt  i,j,rstart  = mat->rmap->rstart,rend = mat->rmap->rend;
5702:     PetscInt  cstart      = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
5703:     PetscBool roworiented = aij->roworiented;

5705:     /* Some Variables required in the macro */
5706:     Mat        A                 = aij->A;
5707:     Mat_SeqAIJ *a                = (Mat_SeqAIJ*)A->data;
5708:     PetscInt   *aimax            = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
5709:     MatScalar  *aa               = a->a;
5710:     PetscBool  ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES)) ? PETSC_TRUE : PETSC_FALSE);
5711:     Mat        B                 = aij->B;
5712:     Mat_SeqAIJ *b                = (Mat_SeqAIJ*)B->data;
5713:     PetscInt   *bimax            = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
5714:     MatScalar  *ba               = b->a;

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

5721:     for (i=0; i<m; i++) {
5722:       if (im[i] < 0) continue;
5723: #if defined(PETSC_USE_DEBUG)
5724:       if (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);
5725: #endif
5726:       if (im[i] >= rstart && im[i] < rend) {
5727:         row      = im[i] - rstart;
5728:         lastcol1 = -1;
5729:         rp1      = aj + ai[row];
5730:         ap1      = aa + ai[row];
5731:         rmax1    = aimax[row];
5732:         nrow1    = ailen[row];
5733:         low1     = 0;
5734:         high1    = nrow1;
5735:         lastcol2 = -1;
5736:         rp2      = bj + bi[row];
5737:         ap2      = ba + bi[row];
5738:         rmax2    = bimax[row];
5739:         nrow2    = bilen[row];
5740:         low2     = 0;
5741:         high2    = nrow2;

5743:         for (j=0; j<n; j++) {
5744:           if (roworiented) value = v[i*n+j];
5745:           else value = v[i+j*m];
5746:           if (in[j] >= cstart && in[j] < cend) {
5747:             col = in[j] - cstart;
5748:             if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES) && row != col) continue;
5749:             MatSetValues_SeqAIJ_A_Private(row,col,value,addv,im[i],in[j]);
5750:           } else if (in[j] < 0) continue;
5751: #if defined(PETSC_USE_DEBUG)
5752:           /* extra brace on SETERRQ2() is required for --with-errorchecking=0 - due to the next 'else' clause */
5753:           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);}
5754: #endif
5755:           else {
5756:             if (mat->was_assembled) {
5757:               if (!aij->colmap) {
5758:                 MatCreateColmap_MPIAIJ_Private(mat);
5759:               }
5760: #if defined(PETSC_USE_CTABLE)
5761:               PetscTableFind(aij->colmap,in[j]+1,&col);
5762:               col--;
5763: #else
5764:               col = aij->colmap[in[j]] - 1;
5765: #endif
5766:               if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES) && row != col) continue;
5767:               if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
5768:                 MatDisAssemble_MPIAIJ(mat);
5769:                 col  =  in[j];
5770:                 /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
5771:                 B     = aij->B;
5772:                 b     = (Mat_SeqAIJ*)B->data;
5773:                 bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
5774:                 rp2   = bj + bi[row];
5775:                 ap2   = ba + bi[row];
5776:                 rmax2 = bimax[row];
5777:                 nrow2 = bilen[row];
5778:                 low2  = 0;
5779:                 high2 = nrow2;
5780:                 bm    = aij->B->rmap->n;
5781:                 ba    = b->a;
5782:               }
5783:             } else col = in[j];
5784:             MatSetValues_SeqAIJ_B_Private(row,col,value,addv,im[i],in[j]);
5785:           }
5786:         }
5787:       } else if (!aij->donotstash) {
5788:         if (roworiented) {
5789:           MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
5790:         } else {
5791:           MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
5792:         }
5793:       }
5794:     }
5795:   }
5796:   PetscFunctionReturnVoid();
5797: }