Actual source code: mpiaij.c

petsc-dev 2014-08-26
Report Typos and Errors
  2: #include <../src/mat/impls/aij/mpi/mpiaij.h>   /*I "petscmat.h" I*/
  3: #include <petsc-private/vecimpl.h>
  4: #include <petscblaslapack.h>
  5: #include <petscsf.h>

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

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

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

 19:   Developer Notes: Subclasses include MATAIJCUSP, MATAIJCUSPARSE, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
 20:    enough exist.

 22:   Level: beginner

 24: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
 25: M*/

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

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

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

 39:   Level: beginner

 41: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
 42: M*/

 46: PetscErrorCode MatFindNonzeroRows_MPIAIJ(Mat M,IS *keptrows)
 47: {
 48:   PetscErrorCode  ierr;
 49:   Mat_MPIAIJ      *mat = (Mat_MPIAIJ*)M->data;
 50:   Mat_SeqAIJ      *a   = (Mat_SeqAIJ*)mat->A->data;
 51:   Mat_SeqAIJ      *b   = (Mat_SeqAIJ*)mat->B->data;
 52:   const PetscInt  *ia,*ib;
 53:   const MatScalar *aa,*bb;
 54:   PetscInt        na,nb,i,j,*rows,cnt=0,n0rows;
 55:   PetscInt        m = M->rmap->n,rstart = M->rmap->rstart;

 58:   *keptrows = 0;
 59:   ia        = a->i;
 60:   ib        = b->i;
 61:   for (i=0; i<m; i++) {
 62:     na = ia[i+1] - ia[i];
 63:     nb = ib[i+1] - ib[i];
 64:     if (!na && !nb) {
 65:       cnt++;
 66:       goto ok1;
 67:     }
 68:     aa = a->a + ia[i];
 69:     for (j=0; j<na; j++) {
 70:       if (aa[j] != 0.0) goto ok1;
 71:     }
 72:     bb = b->a + ib[i];
 73:     for (j=0; j <nb; j++) {
 74:       if (bb[j] != 0.0) goto ok1;
 75:     }
 76:     cnt++;
 77: ok1:;
 78:   }
 79:   MPI_Allreduce(&cnt,&n0rows,1,MPIU_INT,MPIU_SUM,PetscObjectComm((PetscObject)M));
 80:   if (!n0rows) return(0);
 81:   PetscMalloc1((M->rmap->n-cnt),&rows);
 82:   cnt  = 0;
 83:   for (i=0; i<m; i++) {
 84:     na = ia[i+1] - ia[i];
 85:     nb = ib[i+1] - ib[i];
 86:     if (!na && !nb) continue;
 87:     aa = a->a + ia[i];
 88:     for (j=0; j<na;j++) {
 89:       if (aa[j] != 0.0) {
 90:         rows[cnt++] = rstart + i;
 91:         goto ok2;
 92:       }
 93:     }
 94:     bb = b->a + ib[i];
 95:     for (j=0; j<nb; j++) {
 96:       if (bb[j] != 0.0) {
 97:         rows[cnt++] = rstart + i;
 98:         goto ok2;
 99:       }
100:     }
101: ok2:;
102:   }
103:   ISCreateGeneral(PetscObjectComm((PetscObject)M),cnt,rows,PETSC_OWN_POINTER,keptrows);
104:   return(0);
105: }

109: PetscErrorCode  MatDiagonalSet_MPIAIJ(Mat Y,Vec D,InsertMode is)
110: {
111:   PetscErrorCode    ierr;
112:   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*) Y->data;

115:   if (Y->assembled && Y->rmap->rstart == Y->cmap->rstart && Y->rmap->rend == Y->cmap->rend) {
116:     MatDiagonalSet(aij->A,D,is);
117:   } else {
118:     MatDiagonalSet_Default(Y,D,is);
119:   }
120:   return(0);
121: }


126: PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows)
127: {
128:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)M->data;
130:   PetscInt       i,rstart,nrows,*rows;

133:   *zrows = NULL;
134:   MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);
135:   MatGetOwnershipRange(M,&rstart,NULL);
136:   for (i=0; i<nrows; i++) rows[i] += rstart;
137:   ISCreateGeneral(PetscObjectComm((PetscObject)M),nrows,rows,PETSC_OWN_POINTER,zrows);
138:   return(0);
139: }

143: PetscErrorCode MatGetColumnNorms_MPIAIJ(Mat A,NormType type,PetscReal *norms)
144: {
146:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)A->data;
147:   PetscInt       i,n,*garray = aij->garray;
148:   Mat_SeqAIJ     *a_aij = (Mat_SeqAIJ*) aij->A->data;
149:   Mat_SeqAIJ     *b_aij = (Mat_SeqAIJ*) aij->B->data;
150:   PetscReal      *work;

153:   MatGetSize(A,NULL,&n);
154:   PetscCalloc1(n,&work);
155:   if (type == NORM_2) {
156:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
157:       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]*a_aij->a[i]);
158:     }
159:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
160:       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]*b_aij->a[i]);
161:     }
162:   } else if (type == NORM_1) {
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]);
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]);
168:     }
169:   } else if (type == NORM_INFINITY) {
170:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
171:       work[A->cmap->rstart + a_aij->j[i]] = PetscMax(PetscAbsScalar(a_aij->a[i]), work[A->cmap->rstart + a_aij->j[i]]);
172:     }
173:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
174:       work[garray[b_aij->j[i]]] = PetscMax(PetscAbsScalar(b_aij->a[i]),work[garray[b_aij->j[i]]]);
175:     }

177:   } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown NormType");
178:   if (type == NORM_INFINITY) {
179:     MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));
180:   } else {
181:     MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));
182:   }
183:   PetscFree(work);
184:   if (type == NORM_2) {
185:     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
186:   }
187:   return(0);
188: }

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

196:     Only for square matrices

198:     Used by a preconditioner, hence PETSC_EXTERN
199: */
200: PETSC_EXTERN PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat)
201: {
202:   PetscMPIInt    rank,size;
203:   PetscInt       *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz = 0,*gmataj,cnt,row,*ld,bses[2];
205:   Mat            mat;
206:   Mat_SeqAIJ     *gmata;
207:   PetscMPIInt    tag;
208:   MPI_Status     status;
209:   PetscBool      aij;
210:   MatScalar      *gmataa,*ao,*ad,*gmataarestore=0;

213:   MPI_Comm_rank(comm,&rank);
214:   MPI_Comm_size(comm,&size);
215:   if (!rank) {
216:     PetscObjectTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);
217:     if (!aij) SETERRQ1(PetscObjectComm((PetscObject)gmat),PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name);
218:   }
219:   if (reuse == MAT_INITIAL_MATRIX) {
220:     MatCreate(comm,&mat);
221:     MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);
222:     MatGetBlockSizes(gmat,&bses[0],&bses[1]);
223:     MPI_Bcast(bses,2,MPIU_INT,0,comm);
224:     MatSetBlockSizes(mat,bses[0],bses[1]);
225:     MatSetType(mat,MATAIJ);
226:     PetscMalloc1((size+1),&rowners);
227:     PetscMalloc2(m,&dlens,m,&olens);
228:     MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);

230:     rowners[0] = 0;
231:     for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
232:     rstart = rowners[rank];
233:     rend   = rowners[rank+1];
234:     PetscObjectGetNewTag((PetscObject)mat,&tag);
235:     if (!rank) {
236:       gmata = (Mat_SeqAIJ*) gmat->data;
237:       /* send row lengths to all processors */
238:       for (i=0; i<m; i++) dlens[i] = gmata->ilen[i];
239:       for (i=1; i<size; i++) {
240:         MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);
241:       }
242:       /* determine number diagonal and off-diagonal counts */
243:       PetscMemzero(olens,m*sizeof(PetscInt));
244:       PetscCalloc1(m,&ld);
245:       jj   = 0;
246:       for (i=0; i<m; i++) {
247:         for (j=0; j<dlens[i]; j++) {
248:           if (gmata->j[jj] < rstart) ld[i]++;
249:           if (gmata->j[jj] < rstart || gmata->j[jj] >= rend) olens[i]++;
250:           jj++;
251:         }
252:       }
253:       /* send column indices to other processes */
254:       for (i=1; i<size; i++) {
255:         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
256:         MPI_Send(&nz,1,MPIU_INT,i,tag,comm);
257:         MPI_Send(gmata->j + gmata->i[rowners[i]],nz,MPIU_INT,i,tag,comm);
258:       }

260:       /* send numerical values to other processes */
261:       for (i=1; i<size; i++) {
262:         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
263:         MPI_Send(gmata->a + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);
264:       }
265:       gmataa = gmata->a;
266:       gmataj = gmata->j;

268:     } else {
269:       /* receive row lengths */
270:       MPI_Recv(dlens,m,MPIU_INT,0,tag,comm,&status);
271:       /* receive column indices */
272:       MPI_Recv(&nz,1,MPIU_INT,0,tag,comm,&status);
273:       PetscMalloc2(nz,&gmataa,nz,&gmataj);
274:       MPI_Recv(gmataj,nz,MPIU_INT,0,tag,comm,&status);
275:       /* determine number diagonal and off-diagonal counts */
276:       PetscMemzero(olens,m*sizeof(PetscInt));
277:       PetscCalloc1(m,&ld);
278:       jj   = 0;
279:       for (i=0; i<m; i++) {
280:         for (j=0; j<dlens[i]; j++) {
281:           if (gmataj[jj] < rstart) ld[i]++;
282:           if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++;
283:           jj++;
284:         }
285:       }
286:       /* receive numerical values */
287:       PetscMemzero(gmataa,nz*sizeof(PetscScalar));
288:       MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);
289:     }
290:     /* set preallocation */
291:     for (i=0; i<m; i++) {
292:       dlens[i] -= olens[i];
293:     }
294:     MatSeqAIJSetPreallocation(mat,0,dlens);
295:     MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);

297:     for (i=0; i<m; i++) {
298:       dlens[i] += olens[i];
299:     }
300:     cnt = 0;
301:     for (i=0; i<m; i++) {
302:       row  = rstart + i;
303:       MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);
304:       cnt += dlens[i];
305:     }
306:     if (rank) {
307:       PetscFree2(gmataa,gmataj);
308:     }
309:     PetscFree2(dlens,olens);
310:     PetscFree(rowners);

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

314:     *inmat = mat;
315:   } else {   /* column indices are already set; only need to move over numerical values from process 0 */
316:     Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data;
317:     Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data;
318:     mat  = *inmat;
319:     PetscObjectGetNewTag((PetscObject)mat,&tag);
320:     if (!rank) {
321:       /* send numerical values to other processes */
322:       gmata  = (Mat_SeqAIJ*) gmat->data;
323:       MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);
324:       gmataa = gmata->a;
325:       for (i=1; i<size; i++) {
326:         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
327:         MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);
328:       }
329:       nz = gmata->i[rowners[1]]-gmata->i[rowners[0]];
330:     } else {
331:       /* receive numerical values from process 0*/
332:       nz   = Ad->nz + Ao->nz;
333:       PetscMalloc1(nz,&gmataa); gmataarestore = gmataa;
334:       MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);
335:     }
336:     /* transfer numerical values into the diagonal A and off diagonal B parts of mat */
337:     ld = ((Mat_MPIAIJ*)(mat->data))->ld;
338:     ad = Ad->a;
339:     ao = Ao->a;
340:     if (mat->rmap->n) {
341:       i  = 0;
342:       nz = ld[i];                                   PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar)); ao += nz; gmataa += nz;
343:       nz = Ad->i[i+1] - Ad->i[i];                   PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar)); ad += nz; gmataa += nz;
344:     }
345:     for (i=1; i<mat->rmap->n; i++) {
346:       nz = Ao->i[i] - Ao->i[i-1] - ld[i-1] + ld[i]; PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar)); ao += nz; gmataa += nz;
347:       nz = Ad->i[i+1] - Ad->i[i];                   PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar)); ad += nz; gmataa += nz;
348:     }
349:     i--;
350:     if (mat->rmap->n) {
351:       nz = Ao->i[i+1] - Ao->i[i] - ld[i];           PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));
352:     }
353:     if (rank) {
354:       PetscFree(gmataarestore);
355:     }
356:   }
357:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
358:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
359:   return(0);
360: }

362: /*
363:   Local utility routine that creates a mapping from the global column
364: number to the local number in the off-diagonal part of the local
365: storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
366: a slightly higher hash table cost; without it it is not scalable (each processor
367: has an order N integer array but is fast to acess.
368: */
371: PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat mat)
372: {
373:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
375:   PetscInt       n = aij->B->cmap->n,i;

378:   if (!aij->garray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPIAIJ Matrix was assembled but is missing garray");
379: #if defined(PETSC_USE_CTABLE)
380:   PetscTableCreate(n,mat->cmap->N+1,&aij->colmap);
381:   for (i=0; i<n; i++) {
382:     PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1,INSERT_VALUES);
383:   }
384: #else
385:   PetscCalloc1((mat->cmap->N+1),&aij->colmap);
386:   PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N+1)*sizeof(PetscInt));
387:   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
388: #endif
389:   return(0);
390: }

392: #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
393: { \
394:     if (col <= lastcol1)  low1 = 0;     \
395:     else                 high1 = nrow1; \
396:     lastcol1 = col;\
397:     while (high1-low1 > 5) { \
398:       t = (low1+high1)/2; \
399:       if (rp1[t] > col) high1 = t; \
400:       else              low1  = t; \
401:     } \
402:       for (_i=low1; _i<high1; _i++) { \
403:         if (rp1[_i] > col) break; \
404:         if (rp1[_i] == col) { \
405:           if (addv == ADD_VALUES) ap1[_i] += value;   \
406:           else                    ap1[_i] = value; \
407:           goto a_noinsert; \
408:         } \
409:       }  \
410:       if (value == 0.0 && ignorezeroentries) {low1 = 0; high1 = nrow1;goto a_noinsert;} \
411:       if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;}                \
412:       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
413:       MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \
414:       N = nrow1++ - 1; a->nz++; high1++; \
415:       /* shift up all the later entries in this row */ \
416:       for (ii=N; ii>=_i; ii--) { \
417:         rp1[ii+1] = rp1[ii]; \
418:         ap1[ii+1] = ap1[ii]; \
419:       } \
420:       rp1[_i] = col;  \
421:       ap1[_i] = value;  \
422:       A->nonzerostate++;\
423:       a_noinsert: ; \
424:       ailen[row] = nrow1; \
425: }


428: #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
429:   { \
430:     if (col <= lastcol2) low2 = 0;                        \
431:     else high2 = nrow2;                                   \
432:     lastcol2 = col;                                       \
433:     while (high2-low2 > 5) {                              \
434:       t = (low2+high2)/2;                                 \
435:       if (rp2[t] > col) high2 = t;                        \
436:       else             low2  = t;                         \
437:     }                                                     \
438:     for (_i=low2; _i<high2; _i++) {                       \
439:       if (rp2[_i] > col) break;                           \
440:       if (rp2[_i] == col) {                               \
441:         if (addv == ADD_VALUES) ap2[_i] += value;         \
442:         else                    ap2[_i] = value;          \
443:         goto b_noinsert;                                  \
444:       }                                                   \
445:     }                                                     \
446:     if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \
447:     if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;}                        \
448:     if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
449:     MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \
450:     N = nrow2++ - 1; b->nz++; high2++;                    \
451:     /* shift up all the later entries in this row */      \
452:     for (ii=N; ii>=_i; ii--) {                            \
453:       rp2[ii+1] = rp2[ii];                                \
454:       ap2[ii+1] = ap2[ii];                                \
455:     }                                                     \
456:     rp2[_i] = col;                                        \
457:     ap2[_i] = value;                                      \
458:     B->nonzerostate++;                                    \
459:     b_noinsert: ;                                         \
460:     bilen[row] = nrow2;                                   \
461:   }

465: PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[])
466: {
467:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)A->data;
468:   Mat_SeqAIJ     *a   = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data;
470:   PetscInt       l,*garray = mat->garray,diag;

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

475:   /* find size of row to the left of the diagonal part */
476:   MatGetOwnershipRange(A,&diag,0);
477:   row  = row - diag;
478:   for (l=0; l<b->i[row+1]-b->i[row]; l++) {
479:     if (garray[b->j[b->i[row]+l]] > diag) break;
480:   }
481:   PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));

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

486:   /* right of diagonal part */
487:   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));
488:   return(0);
489: }

493: PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
494: {
495:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
496:   PetscScalar    value;
498:   PetscInt       i,j,rstart  = mat->rmap->rstart,rend = mat->rmap->rend;
499:   PetscInt       cstart      = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
500:   PetscBool      roworiented = aij->roworiented;

502:   /* Some Variables required in the macro */
503:   Mat        A                 = aij->A;
504:   Mat_SeqAIJ *a                = (Mat_SeqAIJ*)A->data;
505:   PetscInt   *aimax            = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
506:   MatScalar  *aa               = a->a;
507:   PetscBool  ignorezeroentries = a->ignorezeroentries;
508:   Mat        B                 = aij->B;
509:   Mat_SeqAIJ *b                = (Mat_SeqAIJ*)B->data;
510:   PetscInt   *bimax            = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
511:   MatScalar  *ba               = b->a;

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

518:   for (i=0; i<m; i++) {
519:     if (im[i] < 0) continue;
520: #if defined(PETSC_USE_DEBUG)
521:     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);
522: #endif
523:     if (im[i] >= rstart && im[i] < rend) {
524:       row      = im[i] - rstart;
525:       lastcol1 = -1;
526:       rp1      = aj + ai[row];
527:       ap1      = aa + ai[row];
528:       rmax1    = aimax[row];
529:       nrow1    = ailen[row];
530:       low1     = 0;
531:       high1    = nrow1;
532:       lastcol2 = -1;
533:       rp2      = bj + bi[row];
534:       ap2      = ba + bi[row];
535:       rmax2    = bimax[row];
536:       nrow2    = bilen[row];
537:       low2     = 0;
538:       high2    = nrow2;

540:       for (j=0; j<n; j++) {
541:         if (roworiented) value = v[i*n+j];
542:         else             value = v[i+j*m];
543:         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
544:         if (in[j] >= cstart && in[j] < cend) {
545:           col   = in[j] - cstart;
546:           nonew = a->nonew;
547:           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
548:         } else if (in[j] < 0) continue;
549: #if defined(PETSC_USE_DEBUG)
550:         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);
551: #endif
552:         else {
553:           if (mat->was_assembled) {
554:             if (!aij->colmap) {
555:               MatCreateColmap_MPIAIJ_Private(mat);
556:             }
557: #if defined(PETSC_USE_CTABLE)
558:             PetscTableFind(aij->colmap,in[j]+1,&col);
559:             col--;
560: #else
561:             col = aij->colmap[in[j]] - 1;
562: #endif
563:             if (col < 0 && !((Mat_SeqAIJ*)(aij->B->data))->nonew) {
564:               MatDisAssemble_MPIAIJ(mat);
565:               col  =  in[j];
566:               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
567:               B     = aij->B;
568:               b     = (Mat_SeqAIJ*)B->data;
569:               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a;
570:               rp2   = bj + bi[row];
571:               ap2   = ba + bi[row];
572:               rmax2 = bimax[row];
573:               nrow2 = bilen[row];
574:               low2  = 0;
575:               high2 = nrow2;
576:               bm    = aij->B->rmap->n;
577:               ba    = b->a;
578:             } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", im[i], in[j]);
579:           } else col = in[j];
580:           nonew = b->nonew;
581:           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
582:         }
583:       }
584:     } else {
585:       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]);
586:       if (!aij->donotstash) {
587:         mat->assembled = PETSC_FALSE;
588:         if (roworiented) {
589:           MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
590:         } else {
591:           MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
592:         }
593:       }
594:     }
595:   }
596:   return(0);
597: }

601: PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
602: {
603:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
605:   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
606:   PetscInt       cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;

609:   for (i=0; i<m; i++) {
610:     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
611:     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);
612:     if (idxm[i] >= rstart && idxm[i] < rend) {
613:       row = idxm[i] - rstart;
614:       for (j=0; j<n; j++) {
615:         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
616:         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);
617:         if (idxn[j] >= cstart && idxn[j] < cend) {
618:           col  = idxn[j] - cstart;
619:           MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);
620:         } else {
621:           if (!aij->colmap) {
622:             MatCreateColmap_MPIAIJ_Private(mat);
623:           }
624: #if defined(PETSC_USE_CTABLE)
625:           PetscTableFind(aij->colmap,idxn[j]+1,&col);
626:           col--;
627: #else
628:           col = aij->colmap[idxn[j]] - 1;
629: #endif
630:           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
631:           else {
632:             MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);
633:           }
634:         }
635:       }
636:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
637:   }
638:   return(0);
639: }

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

645: PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
646: {
647:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
649:   PetscInt       nstash,reallocs;
650:   InsertMode     addv;

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

655:   /* make sure all processors are either in INSERTMODE or ADDMODE */
656:   MPI_Allreduce((PetscEnum*)&mat->insertmode,(PetscEnum*)&addv,1,MPIU_ENUM,MPI_BOR,PetscObjectComm((PetscObject)mat));
657:   if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
658:   mat->insertmode = addv; /* in case this processor had no cache */

660:   MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
661:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
662:   PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
663:   return(0);
664: }

668: PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
669: {
670:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
671:   Mat_SeqAIJ     *a   = (Mat_SeqAIJ*)aij->A->data;
673:   PetscMPIInt    n;
674:   PetscInt       i,j,rstart,ncols,flg;
675:   PetscInt       *row,*col;
676:   PetscBool      other_disassembled;
677:   PetscScalar    *val;
678:   InsertMode     addv = mat->insertmode;

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

683:   if (!aij->donotstash && !mat->nooffprocentries) {
684:     while (1) {
685:       MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
686:       if (!flg) break;

688:       for (i=0; i<n; ) {
689:         /* Now identify the consecutive vals belonging to the same row */
690:         for (j=i,rstart=row[j]; j<n; j++) {
691:           if (row[j] != rstart) break;
692:         }
693:         if (j < n) ncols = j-i;
694:         else       ncols = n-i;
695:         /* Now assemble all these values with a single function call */
696:         MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);

698:         i = j;
699:       }
700:     }
701:     MatStashScatterEnd_Private(&mat->stash);
702:   }
703:   MatAssemblyBegin(aij->A,mode);
704:   MatAssemblyEnd(aij->A,mode);

706:   /* determine if any processor has disassembled, if so we must
707:      also disassemble ourselfs, in order that we may reassemble. */
708:   /*
709:      if nonzero structure of submatrix B cannot change then we know that
710:      no processor disassembled thus we can skip this stuff
711:   */
712:   if (!((Mat_SeqAIJ*)aij->B->data)->nonew) {
713:     MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));
714:     if (mat->was_assembled && !other_disassembled) {
715:       MatDisAssemble_MPIAIJ(mat);
716:     }
717:   }
718:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
719:     MatSetUpMultiply_MPIAIJ(mat);
720:   }
721:   MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);
722:   MatAssemblyBegin(aij->B,mode);
723:   MatAssemblyEnd(aij->B,mode);

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

727:   aij->rowvalues = 0;

729:   /* used by MatAXPY() */
730:   a->xtoy = 0; ((Mat_SeqAIJ*)aij->B->data)->xtoy = 0;   /* b->xtoy = 0 */
731:   a->XtoY = 0; ((Mat_SeqAIJ*)aij->B->data)->XtoY = 0;   /* b->XtoY = 0 */

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

736:   /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
737:   if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
738:     PetscObjectState state = aij->A->nonzerostate + aij->B->nonzerostate;
739:     MPI_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));
740:   }
741:   return(0);
742: }

746: PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
747: {
748:   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;

752:   MatZeroEntries(l->A);
753:   MatZeroEntries(l->B);
754:   return(0);
755: }

759: PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
760: {
761:   Mat_MPIAIJ    *mat    = (Mat_MPIAIJ *) A->data;
762:   PetscInt      *owners = A->rmap->range;
763:   PetscInt       n      = A->rmap->n;
764:   PetscMPIInt    size   = mat->size;
765:   PetscSF        sf;
766:   PetscInt      *lrows;
767:   PetscSFNode   *rrows;
768:   PetscInt       lastidx = -1, r, p = 0, len = 0;

772:   /* Create SF where leaves are input rows and roots are owned rows */
773:   PetscMalloc1(n, &lrows);
774:   for (r = 0; r < n; ++r) lrows[r] = -1;
775:   PetscMalloc1(N, &rrows);
776:   for (r = 0; r < N; ++r) {
777:     const PetscInt idx   = rows[r];
778:     PetscBool      found = PETSC_FALSE;
779:     /* Trick for efficient searching for sorted rows */
780:     if (lastidx > idx) p = 0;
781:     lastidx = idx;
782:     for (; p < size; ++p) {
783:       if (idx >= owners[p] && idx < owners[p+1]) {
784:         rrows[r].rank  = p;
785:         rrows[r].index = rows[r] - owners[p];
786:         found = PETSC_TRUE;
787:         break;
788:       }
789:     }
790:     if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %d not found in matrix distribution", idx);
791:   }
792:   PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);
793:   PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);
794:   /* Collect flags for rows to be zeroed */
795:   PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
796:   PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
797:   PetscSFDestroy(&sf);
798:   /* Compress and put in row numbers */
799:   for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
800:   /* fix right hand side if needed */
801:   if (x && b) {
802:     const PetscScalar *xx;
803:     PetscScalar       *bb;

805:     VecGetArrayRead(x, &xx);
806:     VecGetArray(b, &bb);
807:     for (r = 0; r < len; ++r) bb[lrows[r]] = diag*xx[lrows[r]];
808:     VecRestoreArrayRead(x, &xx);
809:     VecRestoreArray(b, &bb);
810:   }
811:   /* Must zero l->B before l->A because the (diag) case below may put values into l->B*/
812:   MatZeroRows(mat->B, len, lrows, 0.0, 0,0);
813:   if ((diag != 0.0) && (mat->A->rmap->N == mat->A->cmap->N)) {
814:     MatZeroRows(mat->A, len, lrows, diag, NULL, NULL);
815:   } else if (diag != 0.0) {
816:     MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);
817:     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");
818:     for (r = 0; r < len; ++r) {
819:       const PetscInt row = lrows[r] + A->rmap->rstart;
820:       MatSetValues(A, 1, &row, 1, &row, &diag, INSERT_VALUES);
821:     }
822:     MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
823:     MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
824:   } else {
825:     MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);
826:   }
827:   PetscFree(lrows);

829:   /* only change matrix nonzero state if pattern was allowed to be changed */
830:   if (!((Mat_SeqAIJ*)(mat->A->data))->keepnonzeropattern) {
831:     PetscObjectState state = mat->A->nonzerostate + mat->B->nonzerostate;
832:     MPI_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
833:   }
834:   return(0);
835: }

839: PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
840: {
841:   Mat_MPIAIJ        *l = (Mat_MPIAIJ*)A->data;
842:   PetscErrorCode    ierr;
843:   PetscMPIInt       size = l->size,n = A->rmap->n,lastidx = -1;
844:   PetscInt          i,j,r,m,p = 0,len = 0;
845:   PetscInt          *lrows,*owners = A->rmap->range;
846:   PetscSFNode       *rrows;
847:   PetscSF           sf;
848:   const PetscScalar *xx;
849:   PetscScalar       *bb,*mask;
850:   Vec               xmask,lmask;
851:   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*)l->B->data;
852:   const PetscInt    *aj, *ii,*ridx;
853:   PetscScalar       *aa;
854: #if defined(PETSC_DEBUG)
855:   PetscBool found = PETSC_FALSE;
856: #endif

859:   /* Create SF where leaves are input rows and roots are owned rows */
860:   PetscMalloc1(n, &lrows);
861:   for (r = 0; r < n; ++r) lrows[r] = -1;
862:   PetscMalloc1(N, &rrows);
863:   for (r = 0; r < N; ++r) {
864:     const PetscInt idx   = rows[r];
865:     PetscBool      found = PETSC_FALSE;
866:     /* Trick for efficient searching for sorted rows */
867:     if (lastidx > idx) p = 0;
868:     lastidx = idx;
869:     for (; p < size; ++p) {
870:       if (idx >= owners[p] && idx < owners[p+1]) {
871:         rrows[r].rank  = p;
872:         rrows[r].index = rows[r] - owners[p];
873:         found = PETSC_TRUE;
874:         break;
875:       }
876:     }
877:     if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %d not found in matrix distribution", idx);
878:   }
879:   PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);
880:   PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);
881:   /* Collect flags for rows to be zeroed */
882:   PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
883:   PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
884:   PetscSFDestroy(&sf);
885:   /* Compress and put in row numbers */
886:   for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
887:   /* zero diagonal part of matrix */
888:   MatZeroRowsColumns(l->A,len,lrows,diag,x,b);
889:   /* handle off diagonal part of matrix */
890:   MatGetVecs(A,&xmask,NULL);
891:   VecDuplicate(l->lvec,&lmask);
892:   VecGetArray(xmask,&bb);
893:   for (i=0; i<len; i++) bb[lrows[i]] = 1;
894:   VecRestoreArray(xmask,&bb);
895:   VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
896:   VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
897:   VecDestroy(&xmask);
898:   if (x) {
899:     VecScatterBegin(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
900:     VecScatterEnd(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
901:     VecGetArrayRead(l->lvec,&xx);
902:     VecGetArray(b,&bb);
903:   }
904:   VecGetArray(lmask,&mask);
905:   /* remove zeroed rows of off diagonal matrix */
906:   ii = aij->i;
907:   for (i=0; i<len; i++) {
908:     PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));
909:   }
910:   /* loop over all elements of off process part of matrix zeroing removed columns*/
911:   if (aij->compressedrow.use) {
912:     m    = aij->compressedrow.nrows;
913:     ii   = aij->compressedrow.i;
914:     ridx = aij->compressedrow.rindex;
915:     for (i=0; i<m; i++) {
916:       n  = ii[i+1] - ii[i];
917:       aj = aij->j + ii[i];
918:       aa = aij->a + ii[i];

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

954:   /* only change matrix nonzero state if pattern was allowed to be changed */
955:   if (!((Mat_SeqAIJ*)(l->A->data))->keepnonzeropattern) {
956:     PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
957:     MPI_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
958:   }
959:   return(0);
960: }

964: PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
965: {
966:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
968:   PetscInt       nt;

971:   VecGetLocalSize(xx,&nt);
972:   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);
973:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
974:   (*a->A->ops->mult)(a->A,xx,yy);
975:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
976:   (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
977:   return(0);
978: }

982: PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx)
983: {
984:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

988:   MatMultDiagonalBlock(a->A,bb,xx);
989:   return(0);
990: }

994: PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
995: {
996:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1000:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1001:   (*a->A->ops->multadd)(a->A,xx,yy,zz);
1002:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1003:   (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
1004:   return(0);
1005: }

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

1016:   VecScatterGetMerged(a->Mvctx,&merged);
1017:   /* do nondiagonal part */
1018:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1019:   if (!merged) {
1020:     /* send it on its way */
1021:     VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1022:     /* do local part */
1023:     (*a->A->ops->multtranspose)(a->A,xx,yy);
1024:     /* receive remote parts: note this assumes the values are not actually */
1025:     /* added in yy until the next line, */
1026:     VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1027:   } else {
1028:     /* do local part */
1029:     (*a->A->ops->multtranspose)(a->A,xx,yy);
1030:     /* send it on its way */
1031:     VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1032:     /* values actually were received in the Begin() but we need to call this nop */
1033:     VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1034:   }
1035:   return(0);
1036: }

1040: PetscErrorCode  MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool  *f)
1041: {
1042:   MPI_Comm       comm;
1043:   Mat_MPIAIJ     *Aij = (Mat_MPIAIJ*) Amat->data, *Bij;
1044:   Mat            Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
1045:   IS             Me,Notme;
1047:   PetscInt       M,N,first,last,*notme,i;
1048:   PetscMPIInt    size;

1051:   /* Easy test: symmetric diagonal block */
1052:   Bij  = (Mat_MPIAIJ*) Bmat->data; Bdia = Bij->A;
1053:   MatIsTranspose(Adia,Bdia,tol,f);
1054:   if (!*f) return(0);
1055:   PetscObjectGetComm((PetscObject)Amat,&comm);
1056:   MPI_Comm_size(comm,&size);
1057:   if (size == 1) return(0);

1059:   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
1060:   MatGetSize(Amat,&M,&N);
1061:   MatGetOwnershipRange(Amat,&first,&last);
1062:   PetscMalloc1((N-last+first),&notme);
1063:   for (i=0; i<first; i++) notme[i] = i;
1064:   for (i=last; i<M; i++) notme[i-last+first] = i;
1065:   ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);
1066:   ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);
1067:   MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);
1068:   Aoff = Aoffs[0];
1069:   MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);
1070:   Boff = Boffs[0];
1071:   MatIsTranspose(Aoff,Boff,tol,f);
1072:   MatDestroyMatrices(1,&Aoffs);
1073:   MatDestroyMatrices(1,&Boffs);
1074:   ISDestroy(&Me);
1075:   ISDestroy(&Notme);
1076:   PetscFree(notme);
1077:   return(0);
1078: }

1082: PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1083: {
1084:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1088:   /* do nondiagonal part */
1089:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1090:   /* send it on its way */
1091:   VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1092:   /* do local part */
1093:   (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);
1094:   /* receive remote parts */
1095:   VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1096:   return(0);
1097: }

1099: /*
1100:   This only works correctly for square matrices where the subblock A->A is the
1101:    diagonal block
1102: */
1105: PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
1106: {
1108:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1111:   if (A->rmap->N != A->cmap->N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
1112:   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");
1113:   MatGetDiagonal(a->A,v);
1114:   return(0);
1115: }

1119: PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
1120: {
1121:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1125:   MatScale(a->A,aa);
1126:   MatScale(a->B,aa);
1127:   return(0);
1128: }

1132: PetscErrorCode MatDestroy_Redundant(Mat_Redundant **redundant)
1133: {
1135:   Mat_Redundant  *redund = *redundant;
1136:   PetscInt       i;

1139:   *redundant = NULL;
1140:   if (redund){
1141:     if (redund->matseq) { /* via MatGetSubMatrices()  */
1142:       ISDestroy(&redund->isrow);
1143:       ISDestroy(&redund->iscol);
1144:       MatDestroy(&redund->matseq[0]);
1145:       PetscFree(redund->matseq);
1146:     } else {
1147:       PetscFree2(redund->send_rank,redund->recv_rank);
1148:       PetscFree(redund->sbuf_j);
1149:       PetscFree(redund->sbuf_a);
1150:       for (i=0; i<redund->nrecvs; i++) {
1151:         PetscFree(redund->rbuf_j[i]);
1152:         PetscFree(redund->rbuf_a[i]);
1153:       }
1154:       PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);
1155:     }

1157:     if (redund->psubcomm) {
1158:       PetscSubcommDestroy(&redund->psubcomm);
1159:     }
1160:     PetscFree(redund);
1161:   }
1162:   return(0);
1163: }

1167: PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
1168: {
1169:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

1173: #if defined(PETSC_USE_LOG)
1174:   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N);
1175: #endif
1176:   MatDestroy_Redundant(&aij->redundant);
1177:   MatStashDestroy_Private(&mat->stash);
1178:   VecDestroy(&aij->diag);
1179:   MatDestroy(&aij->A);
1180:   MatDestroy(&aij->B);
1181: #if defined(PETSC_USE_CTABLE)
1182:   PetscTableDestroy(&aij->colmap);
1183: #else
1184:   PetscFree(aij->colmap);
1185: #endif
1186:   PetscFree(aij->garray);
1187:   VecDestroy(&aij->lvec);
1188:   VecScatterDestroy(&aij->Mvctx);
1189:   PetscFree2(aij->rowvalues,aij->rowindices);
1190:   PetscFree(aij->ld);
1191:   PetscFree(mat->data);

1193:   PetscObjectChangeTypeName((PetscObject)mat,0);
1194:   PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);
1195:   PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);
1196:   PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C",NULL);
1197:   PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C",NULL);
1198:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C",NULL);
1199:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C",NULL);
1200:   PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);
1201:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C",NULL);
1202:   return(0);
1203: }

1207: PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
1208: {
1209:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1210:   Mat_SeqAIJ     *A   = (Mat_SeqAIJ*)aij->A->data;
1211:   Mat_SeqAIJ     *B   = (Mat_SeqAIJ*)aij->B->data;
1213:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
1214:   int            fd;
1215:   PetscInt       nz,header[4],*row_lengths,*range=0,rlen,i;
1216:   PetscInt       nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz = 0;
1217:   PetscScalar    *column_values;
1218:   PetscInt       message_count,flowcontrolcount;
1219:   FILE           *file;

1222:   MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
1223:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
1224:   nz   = A->nz + B->nz;
1225:   if (!rank) {
1226:     header[0] = MAT_FILE_CLASSID;
1227:     header[1] = mat->rmap->N;
1228:     header[2] = mat->cmap->N;

1230:     MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1231:     PetscViewerBinaryGetDescriptor(viewer,&fd);
1232:     PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);
1233:     /* get largest number of rows any processor has */
1234:     rlen  = mat->rmap->n;
1235:     range = mat->rmap->range;
1236:     for (i=1; i<size; i++) rlen = PetscMax(rlen,range[i+1] - range[i]);
1237:   } else {
1238:     MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1239:     rlen = mat->rmap->n;
1240:   }

1242:   /* load up the local row counts */
1243:   PetscMalloc1((rlen+1),&row_lengths);
1244:   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];

1246:   /* store the row lengths to the file */
1247:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1248:   if (!rank) {
1249:     PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);
1250:     for (i=1; i<size; i++) {
1251:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1252:       rlen = range[i+1] - range[i];
1253:       MPIULong_Recv(row_lengths,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));
1254:       PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);
1255:     }
1256:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1257:   } else {
1258:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1259:     MPIULong_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1260:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1261:   }
1262:   PetscFree(row_lengths);

1264:   /* load up the local column indices */
1265:   nzmax = nz; /* th processor needs space a largest processor needs */
1266:   MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));
1267:   PetscMalloc1((nzmax+1),&column_indices);
1268:   cnt   = 0;
1269:   for (i=0; i<mat->rmap->n; i++) {
1270:     for (j=B->i[i]; j<B->i[i+1]; j++) {
1271:       if ((col = garray[B->j[j]]) > cstart) break;
1272:       column_indices[cnt++] = col;
1273:     }
1274:     for (k=A->i[i]; k<A->i[i+1]; k++) column_indices[cnt++] = A->j[k] + cstart;
1275:     for (; j<B->i[i+1]; j++) column_indices[cnt++] = garray[B->j[j]];
1276:   }
1277:   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);

1279:   /* store the column indices to the file */
1280:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1281:   if (!rank) {
1282:     MPI_Status status;
1283:     PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);
1284:     for (i=1; i<size; i++) {
1285:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1286:       MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1287:       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1288:       MPIULong_Recv(column_indices,rnz,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));
1289:       PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);
1290:     }
1291:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1292:   } else {
1293:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1294:     MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1295:     MPIULong_Send(column_indices,nz,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1296:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1297:   }
1298:   PetscFree(column_indices);

1300:   /* load up the local column values */
1301:   PetscMalloc1((nzmax+1),&column_values);
1302:   cnt  = 0;
1303:   for (i=0; i<mat->rmap->n; i++) {
1304:     for (j=B->i[i]; j<B->i[i+1]; j++) {
1305:       if (garray[B->j[j]] > cstart) break;
1306:       column_values[cnt++] = B->a[j];
1307:     }
1308:     for (k=A->i[i]; k<A->i[i+1]; k++) column_values[cnt++] = A->a[k];
1309:     for (; j<B->i[i+1]; j++) column_values[cnt++] = B->a[j];
1310:   }
1311:   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);

1313:   /* store the column values to the file */
1314:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1315:   if (!rank) {
1316:     MPI_Status status;
1317:     PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);
1318:     for (i=1; i<size; i++) {
1319:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1320:       MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1321:       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1322:       MPIULong_Recv(column_values,rnz,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat));
1323:       PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);
1324:     }
1325:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1326:   } else {
1327:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1328:     MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1329:     MPIULong_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));
1330:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1331:   }
1332:   PetscFree(column_values);

1334:   PetscViewerBinaryGetInfoPointer(viewer,&file);
1335:   if (file) fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(mat->rmap->bs));
1336:   return(0);
1337: }

1339: #include <petscdraw.h>
1342: PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
1343: {
1344:   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
1345:   PetscErrorCode    ierr;
1346:   PetscMPIInt       rank = aij->rank,size = aij->size;
1347:   PetscBool         isdraw,iascii,isbinary;
1348:   PetscViewer       sviewer;
1349:   PetscViewerFormat format;

1352:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1353:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1354:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1355:   if (iascii) {
1356:     PetscViewerGetFormat(viewer,&format);
1357:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1358:       MatInfo   info;
1359:       PetscBool inodes;

1361:       MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
1362:       MatGetInfo(mat,MAT_LOCAL,&info);
1363:       MatInodeGetInodeSizes(aij->A,NULL,(PetscInt**)&inodes,NULL);
1364:       PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);
1365:       if (!inodes) {
1366:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n",
1367:                                                   rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);
1368:       } else {
1369:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n",
1370:                                                   rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);
1371:       }
1372:       MatGetInfo(aij->A,MAT_LOCAL,&info);
1373:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
1374:       MatGetInfo(aij->B,MAT_LOCAL,&info);
1375:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
1376:       PetscViewerFlush(viewer);
1377:       PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);
1378:       PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");
1379:       VecScatterView(aij->Mvctx,viewer);
1380:       return(0);
1381:     } else if (format == PETSC_VIEWER_ASCII_INFO) {
1382:       PetscInt inodecount,inodelimit,*inodes;
1383:       MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);
1384:       if (inodes) {
1385:         PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);
1386:       } else {
1387:         PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");
1388:       }
1389:       return(0);
1390:     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
1391:       return(0);
1392:     }
1393:   } else if (isbinary) {
1394:     if (size == 1) {
1395:       PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);
1396:       MatView(aij->A,viewer);
1397:     } else {
1398:       MatView_MPIAIJ_Binary(mat,viewer);
1399:     }
1400:     return(0);
1401:   } else if (isdraw) {
1402:     PetscDraw draw;
1403:     PetscBool isnull;
1404:     PetscViewerDrawGetDraw(viewer,0,&draw);
1405:     PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
1406:   }

1408:   {
1409:     /* assemble the entire matrix onto first processor. */
1410:     Mat        A;
1411:     Mat_SeqAIJ *Aloc;
1412:     PetscInt   M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct;
1413:     MatScalar  *a;

1415:     MatCreate(PetscObjectComm((PetscObject)mat),&A);
1416:     if (!rank) {
1417:       MatSetSizes(A,M,N,M,N);
1418:     } else {
1419:       MatSetSizes(A,0,0,M,N);
1420:     }
1421:     /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */
1422:     MatSetType(A,MATMPIAIJ);
1423:     MatMPIAIJSetPreallocation(A,0,NULL,0,NULL);
1424:     MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
1425:     PetscLogObjectParent((PetscObject)mat,(PetscObject)A);

1427:     /* copy over the A part */
1428:     Aloc = (Mat_SeqAIJ*)aij->A->data;
1429:     m    = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1430:     row  = mat->rmap->rstart;
1431:     for (i=0; i<ai[m]; i++) aj[i] += mat->cmap->rstart;
1432:     for (i=0; i<m; i++) {
1433:       MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);
1434:       row++;
1435:       a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1436:     }
1437:     aj = Aloc->j;
1438:     for (i=0; i<ai[m]; i++) aj[i] -= mat->cmap->rstart;

1440:     /* copy over the B part */
1441:     Aloc = (Mat_SeqAIJ*)aij->B->data;
1442:     m    = aij->B->rmap->n;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1443:     row  = mat->rmap->rstart;
1444:     PetscMalloc1((ai[m]+1),&cols);
1445:     ct   = cols;
1446:     for (i=0; i<ai[m]; i++) cols[i] = aij->garray[aj[i]];
1447:     for (i=0; i<m; i++) {
1448:       MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);
1449:       row++;
1450:       a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1451:     }
1452:     PetscFree(ct);
1453:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1454:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1455:     /*
1456:        Everyone has to call to draw the matrix since the graphics waits are
1457:        synchronized across all processors that share the PetscDraw object
1458:     */
1459:     PetscViewerGetSingleton(viewer,&sviewer);
1460:     if (!rank) {
1461:       MatView_SeqAIJ(((Mat_MPIAIJ*)(A->data))->A,sviewer);
1462:     }
1463:     PetscViewerRestoreSingleton(viewer,&sviewer);
1464:     MatDestroy(&A);
1465:   }
1466:   return(0);
1467: }

1471: PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
1472: {
1474:   PetscBool      iascii,isdraw,issocket,isbinary;

1477:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1478:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1479:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1480:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
1481:   if (iascii || isdraw || isbinary || issocket) {
1482:     MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);
1483:   }
1484:   return(0);
1485: }

1489: PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1490: {
1491:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1493:   Vec            bb1 = 0;
1494:   PetscBool      hasop;

1497:   if (flag == SOR_APPLY_UPPER) {
1498:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1499:     return(0);
1500:   }

1502:   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) {
1503:     VecDuplicate(bb,&bb1);
1504:   }

1506:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
1507:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1508:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1509:       its--;
1510:     }

1512:     while (its--) {
1513:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1514:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

1516:       /* update rhs: bb1 = bb - B*x */
1517:       VecScale(mat->lvec,-1.0);
1518:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1520:       /* local sweep */
1521:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);
1522:     }
1523:   } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
1524:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1525:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1526:       its--;
1527:     }
1528:     while (its--) {
1529:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1530:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

1532:       /* update rhs: bb1 = bb - B*x */
1533:       VecScale(mat->lvec,-1.0);
1534:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1536:       /* local sweep */
1537:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);
1538:     }
1539:   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
1540:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1541:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1542:       its--;
1543:     }
1544:     while (its--) {
1545:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1546:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

1548:       /* update rhs: bb1 = bb - B*x */
1549:       VecScale(mat->lvec,-1.0);
1550:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1552:       /* local sweep */
1553:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);
1554:     }
1555:   } else if (flag & SOR_EISENSTAT) {
1556:     Vec xx1;

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

1561:     VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1562:     VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1563:     if (!mat->diag) {
1564:       MatGetVecs(matin,&mat->diag,NULL);
1565:       MatGetDiagonal(matin,mat->diag);
1566:     }
1567:     MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);
1568:     if (hasop) {
1569:       MatMultDiagonalBlock(matin,xx,bb1);
1570:     } else {
1571:       VecPointwiseMult(bb1,mat->diag,xx);
1572:     }
1573:     VecAYPX(bb1,(omega-2.0)/omega,bb);

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

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

1583:   VecDestroy(&bb1);
1584:   return(0);
1585: }

1589: PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
1590: {
1591:   Mat            aA,aB,Aperm;
1592:   const PetscInt *rwant,*cwant,*gcols,*ai,*bi,*aj,*bj;
1593:   PetscScalar    *aa,*ba;
1594:   PetscInt       i,j,m,n,ng,anz,bnz,*dnnz,*onnz,*tdnnz,*tonnz,*rdest,*cdest,*work,*gcdest;
1595:   PetscSF        rowsf,sf;
1596:   IS             parcolp = NULL;
1597:   PetscBool      done;

1601:   MatGetLocalSize(A,&m,&n);
1602:   ISGetIndices(rowp,&rwant);
1603:   ISGetIndices(colp,&cwant);
1604:   PetscMalloc3(PetscMax(m,n),&work,m,&rdest,n,&cdest);

1606:   /* Invert row permutation to find out where my rows should go */
1607:   PetscSFCreate(PetscObjectComm((PetscObject)A),&rowsf);
1608:   PetscSFSetGraphLayout(rowsf,A->rmap,A->rmap->n,NULL,PETSC_OWN_POINTER,rwant);
1609:   PetscSFSetFromOptions(rowsf);
1610:   for (i=0; i<m; i++) work[i] = A->rmap->rstart + i;
1611:   PetscSFReduceBegin(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);
1612:   PetscSFReduceEnd(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);

1614:   /* Invert column permutation to find out where my columns should go */
1615:   PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
1616:   PetscSFSetGraphLayout(sf,A->cmap,A->cmap->n,NULL,PETSC_OWN_POINTER,cwant);
1617:   PetscSFSetFromOptions(sf);
1618:   for (i=0; i<n; i++) work[i] = A->cmap->rstart + i;
1619:   PetscSFReduceBegin(sf,MPIU_INT,work,cdest,MPIU_REPLACE);
1620:   PetscSFReduceEnd(sf,MPIU_INT,work,cdest,MPIU_REPLACE);
1621:   PetscSFDestroy(&sf);

1623:   ISRestoreIndices(rowp,&rwant);
1624:   ISRestoreIndices(colp,&cwant);
1625:   MatMPIAIJGetSeqAIJ(A,&aA,&aB,&gcols);

1627:   /* Find out where my gcols should go */
1628:   MatGetSize(aB,NULL,&ng);
1629:   PetscMalloc1(ng,&gcdest);
1630:   PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
1631:   PetscSFSetGraphLayout(sf,A->cmap,ng,NULL,PETSC_OWN_POINTER,gcols);
1632:   PetscSFSetFromOptions(sf);
1633:   PetscSFBcastBegin(sf,MPIU_INT,cdest,gcdest);
1634:   PetscSFBcastEnd(sf,MPIU_INT,cdest,gcdest);
1635:   PetscSFDestroy(&sf);

1637:   PetscCalloc4(m,&dnnz,m,&onnz,m,&tdnnz,m,&tonnz);
1638:   MatGetRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);
1639:   MatGetRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);
1640:   for (i=0; i<m; i++) {
1641:     PetscInt row = rdest[i],rowner;
1642:     PetscLayoutFindOwner(A->rmap,row,&rowner);
1643:     for (j=ai[i]; j<ai[i+1]; j++) {
1644:       PetscInt cowner,col = cdest[aj[j]];
1645:       PetscLayoutFindOwner(A->cmap,col,&cowner); /* Could build an index for the columns to eliminate this search */
1646:       if (rowner == cowner) dnnz[i]++;
1647:       else onnz[i]++;
1648:     }
1649:     for (j=bi[i]; j<bi[i+1]; j++) {
1650:       PetscInt cowner,col = gcdest[bj[j]];
1651:       PetscLayoutFindOwner(A->cmap,col,&cowner);
1652:       if (rowner == cowner) dnnz[i]++;
1653:       else onnz[i]++;
1654:     }
1655:   }
1656:   PetscSFBcastBegin(rowsf,MPIU_INT,dnnz,tdnnz);
1657:   PetscSFBcastEnd(rowsf,MPIU_INT,dnnz,tdnnz);
1658:   PetscSFBcastBegin(rowsf,MPIU_INT,onnz,tonnz);
1659:   PetscSFBcastEnd(rowsf,MPIU_INT,onnz,tonnz);
1660:   PetscSFDestroy(&rowsf);

1662:   MatCreateAIJ(PetscObjectComm((PetscObject)A),A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N,0,tdnnz,0,tonnz,&Aperm);
1663:   MatSeqAIJGetArray(aA,&aa);
1664:   MatSeqAIJGetArray(aB,&ba);
1665:   for (i=0; i<m; i++) {
1666:     PetscInt *acols = dnnz,*bcols = onnz; /* Repurpose now-unneeded arrays */
1667:     PetscInt j0,rowlen;
1668:     rowlen = ai[i+1] - ai[i];
1669:     for (j0=j=0; j<rowlen; j0=j) { /* rowlen could be larger than number of rows m, so sum in batches */
1670:       for ( ; j<PetscMin(rowlen,j0+m); j++) acols[j-j0] = cdest[aj[ai[i]+j]];
1671:       MatSetValues(Aperm,1,&rdest[i],j-j0,acols,aa+ai[i]+j0,INSERT_VALUES);
1672:     }
1673:     rowlen = bi[i+1] - bi[i];
1674:     for (j0=j=0; j<rowlen; j0=j) {
1675:       for ( ; j<PetscMin(rowlen,j0+m); j++) bcols[j-j0] = gcdest[bj[bi[i]+j]];
1676:       MatSetValues(Aperm,1,&rdest[i],j-j0,bcols,ba+bi[i]+j0,INSERT_VALUES);
1677:     }
1678:   }
1679:   MatAssemblyBegin(Aperm,MAT_FINAL_ASSEMBLY);
1680:   MatAssemblyEnd(Aperm,MAT_FINAL_ASSEMBLY);
1681:   MatRestoreRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);
1682:   MatRestoreRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);
1683:   MatSeqAIJRestoreArray(aA,&aa);
1684:   MatSeqAIJRestoreArray(aB,&ba);
1685:   PetscFree4(dnnz,onnz,tdnnz,tonnz);
1686:   PetscFree3(work,rdest,cdest);
1687:   PetscFree(gcdest);
1688:   if (parcolp) {ISDestroy(&colp);}
1689:   *B = Aperm;
1690:   return(0);
1691: }

1695: PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1696: {
1697:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1698:   Mat            A    = mat->A,B = mat->B;
1700:   PetscReal      isend[5],irecv[5];

1703:   info->block_size = 1.0;
1704:   MatGetInfo(A,MAT_LOCAL,info);

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

1709:   MatGetInfo(B,MAT_LOCAL,info);

1711:   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1712:   isend[3] += info->memory;  isend[4] += info->mallocs;
1713:   if (flag == MAT_LOCAL) {
1714:     info->nz_used      = isend[0];
1715:     info->nz_allocated = isend[1];
1716:     info->nz_unneeded  = isend[2];
1717:     info->memory       = isend[3];
1718:     info->mallocs      = isend[4];
1719:   } else if (flag == MAT_GLOBAL_MAX) {
1720:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));

1722:     info->nz_used      = irecv[0];
1723:     info->nz_allocated = irecv[1];
1724:     info->nz_unneeded  = irecv[2];
1725:     info->memory       = irecv[3];
1726:     info->mallocs      = irecv[4];
1727:   } else if (flag == MAT_GLOBAL_SUM) {
1728:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));

1730:     info->nz_used      = irecv[0];
1731:     info->nz_allocated = irecv[1];
1732:     info->nz_unneeded  = irecv[2];
1733:     info->memory       = irecv[3];
1734:     info->mallocs      = irecv[4];
1735:   }
1736:   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
1737:   info->fill_ratio_needed = 0;
1738:   info->factor_mallocs    = 0;
1739:   return(0);
1740: }

1744: PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg)
1745: {
1746:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1750:   switch (op) {
1751:   case MAT_NEW_NONZERO_LOCATIONS:
1752:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1753:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1754:   case MAT_KEEP_NONZERO_PATTERN:
1755:   case MAT_NEW_NONZERO_LOCATION_ERR:
1756:   case MAT_USE_INODES:
1757:   case MAT_IGNORE_ZERO_ENTRIES:
1758:     MatCheckPreallocated(A,1);
1759:     MatSetOption(a->A,op,flg);
1760:     MatSetOption(a->B,op,flg);
1761:     break;
1762:   case MAT_ROW_ORIENTED:
1763:     a->roworiented = flg;

1765:     MatSetOption(a->A,op,flg);
1766:     MatSetOption(a->B,op,flg);
1767:     break;
1768:   case MAT_NEW_DIAGONALS:
1769:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1770:     break;
1771:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1772:     a->donotstash = flg;
1773:     break;
1774:   case MAT_SPD:
1775:     A->spd_set = PETSC_TRUE;
1776:     A->spd     = flg;
1777:     if (flg) {
1778:       A->symmetric                  = PETSC_TRUE;
1779:       A->structurally_symmetric     = PETSC_TRUE;
1780:       A->symmetric_set              = PETSC_TRUE;
1781:       A->structurally_symmetric_set = PETSC_TRUE;
1782:     }
1783:     break;
1784:   case MAT_SYMMETRIC:
1785:     MatSetOption(a->A,op,flg);
1786:     break;
1787:   case MAT_STRUCTURALLY_SYMMETRIC:
1788:     MatSetOption(a->A,op,flg);
1789:     break;
1790:   case MAT_HERMITIAN:
1791:     MatSetOption(a->A,op,flg);
1792:     break;
1793:   case MAT_SYMMETRY_ETERNAL:
1794:     MatSetOption(a->A,op,flg);
1795:     break;
1796:   default:
1797:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1798:   }
1799:   return(0);
1800: }

1804: PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1805: {
1806:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1807:   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
1809:   PetscInt       i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart;
1810:   PetscInt       nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend;
1811:   PetscInt       *cmap,*idx_p;

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

1817:   if (!mat->rowvalues && (idx || v)) {
1818:     /*
1819:         allocate enough space to hold information from the longest row.
1820:     */
1821:     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1822:     PetscInt   max = 1,tmp;
1823:     for (i=0; i<matin->rmap->n; i++) {
1824:       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1825:       if (max < tmp) max = tmp;
1826:     }
1827:     PetscMalloc2(max,&mat->rowvalues,max,&mat->rowindices);
1828:   }

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

1833:   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1834:   if (!v)   {pvA = 0; pvB = 0;}
1835:   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1836:   (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1837:   (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1838:   nztot = nzA + nzB;

1840:   cmap = mat->garray;
1841:   if (v  || idx) {
1842:     if (nztot) {
1843:       /* Sort by increasing column numbers, assuming A and B already sorted */
1844:       PetscInt imark = -1;
1845:       if (v) {
1846:         *v = v_p = mat->rowvalues;
1847:         for (i=0; i<nzB; i++) {
1848:           if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i];
1849:           else break;
1850:         }
1851:         imark = i;
1852:         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1853:         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1854:       }
1855:       if (idx) {
1856:         *idx = idx_p = mat->rowindices;
1857:         if (imark > -1) {
1858:           for (i=0; i<imark; i++) {
1859:             idx_p[i] = cmap[cworkB[i]];
1860:           }
1861:         } else {
1862:           for (i=0; i<nzB; i++) {
1863:             if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]];
1864:             else break;
1865:           }
1866:           imark = i;
1867:         }
1868:         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
1869:         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
1870:       }
1871:     } else {
1872:       if (idx) *idx = 0;
1873:       if (v)   *v   = 0;
1874:     }
1875:   }
1876:   *nz  = nztot;
1877:   (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1878:   (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1879:   return(0);
1880: }

1884: PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1885: {
1886:   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;

1889:   if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
1890:   aij->getrowactive = PETSC_FALSE;
1891:   return(0);
1892: }

1896: PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1897: {
1898:   Mat_MPIAIJ     *aij  = (Mat_MPIAIJ*)mat->data;
1899:   Mat_SeqAIJ     *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1901:   PetscInt       i,j,cstart = mat->cmap->rstart;
1902:   PetscReal      sum = 0.0;
1903:   MatScalar      *v;

1906:   if (aij->size == 1) {
1907:      MatNorm(aij->A,type,norm);
1908:   } else {
1909:     if (type == NORM_FROBENIUS) {
1910:       v = amat->a;
1911:       for (i=0; i<amat->nz; i++) {
1912:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1913:       }
1914:       v = bmat->a;
1915:       for (i=0; i<bmat->nz; i++) {
1916:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1917:       }
1918:       MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
1919:       *norm = PetscSqrtReal(*norm);
1920:     } else if (type == NORM_1) { /* max column norm */
1921:       PetscReal *tmp,*tmp2;
1922:       PetscInt  *jj,*garray = aij->garray;
1923:       PetscCalloc1((mat->cmap->N+1),&tmp);
1924:       PetscMalloc1((mat->cmap->N+1),&tmp2);
1925:       *norm = 0.0;
1926:       v     = amat->a; jj = amat->j;
1927:       for (j=0; j<amat->nz; j++) {
1928:         tmp[cstart + *jj++] += PetscAbsScalar(*v);  v++;
1929:       }
1930:       v = bmat->a; jj = bmat->j;
1931:       for (j=0; j<bmat->nz; j++) {
1932:         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
1933:       }
1934:       MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
1935:       for (j=0; j<mat->cmap->N; j++) {
1936:         if (tmp2[j] > *norm) *norm = tmp2[j];
1937:       }
1938:       PetscFree(tmp);
1939:       PetscFree(tmp2);
1940:     } else if (type == NORM_INFINITY) { /* max row norm */
1941:       PetscReal ntemp = 0.0;
1942:       for (j=0; j<aij->A->rmap->n; j++) {
1943:         v   = amat->a + amat->i[j];
1944:         sum = 0.0;
1945:         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1946:           sum += PetscAbsScalar(*v); v++;
1947:         }
1948:         v = bmat->a + bmat->i[j];
1949:         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1950:           sum += PetscAbsScalar(*v); v++;
1951:         }
1952:         if (sum > ntemp) ntemp = sum;
1953:       }
1954:       MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));
1955:     } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for two norm");
1956:   }
1957:   return(0);
1958: }

1962: PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout)
1963: {
1964:   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;
1965:   Mat_SeqAIJ     *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data;
1967:   PetscInt       M      = A->rmap->N,N = A->cmap->N,ma,na,mb,nb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i;
1968:   PetscInt       cstart = A->cmap->rstart,ncol;
1969:   Mat            B;
1970:   MatScalar      *array;

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

1975:   ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; nb = a->B->cmap->n;
1976:   ai = Aloc->i; aj = Aloc->j;
1977:   bi = Bloc->i; bj = Bloc->j;
1978:   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
1979:     PetscInt             *d_nnz,*g_nnz,*o_nnz;
1980:     PetscSFNode          *oloc;
1981:     PETSC_UNUSED PetscSF sf;

1983:     PetscMalloc4(na,&d_nnz,na,&o_nnz,nb,&g_nnz,nb,&oloc);
1984:     /* compute d_nnz for preallocation */
1985:     PetscMemzero(d_nnz,na*sizeof(PetscInt));
1986:     for (i=0; i<ai[ma]; i++) {
1987:       d_nnz[aj[i]]++;
1988:       aj[i] += cstart; /* global col index to be used by MatSetValues() */
1989:     }
1990:     /* compute local off-diagonal contributions */
1991:     PetscMemzero(g_nnz,nb*sizeof(PetscInt));
1992:     for (i=0; i<bi[ma]; i++) g_nnz[bj[i]]++;
1993:     /* map those to global */
1994:     PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
1995:     PetscSFSetGraphLayout(sf,A->cmap,nb,NULL,PETSC_USE_POINTER,a->garray);
1996:     PetscSFSetFromOptions(sf);
1997:     PetscMemzero(o_nnz,na*sizeof(PetscInt));
1998:     PetscSFReduceBegin(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);
1999:     PetscSFReduceEnd(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);
2000:     PetscSFDestroy(&sf);

2002:     MatCreate(PetscObjectComm((PetscObject)A),&B);
2003:     MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);
2004:     MatSetBlockSizes(B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
2005:     MatSetType(B,((PetscObject)A)->type_name);
2006:     MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);
2007:     PetscFree4(d_nnz,o_nnz,g_nnz,oloc);
2008:   } else {
2009:     B    = *matout;
2010:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
2011:     for (i=0; i<ai[ma]; i++) aj[i] += cstart; /* global col index to be used by MatSetValues() */
2012:   }

2014:   /* copy over the A part */
2015:   array = Aloc->a;
2016:   row   = A->rmap->rstart;
2017:   for (i=0; i<ma; i++) {
2018:     ncol = ai[i+1]-ai[i];
2019:     MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);
2020:     row++;
2021:     array += ncol; aj += ncol;
2022:   }
2023:   aj = Aloc->j;
2024:   for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */

2026:   /* copy over the B part */
2027:   PetscCalloc1(bi[mb],&cols);
2028:   array = Bloc->a;
2029:   row   = A->rmap->rstart;
2030:   for (i=0; i<bi[mb]; i++) cols[i] = a->garray[bj[i]];
2031:   cols_tmp = cols;
2032:   for (i=0; i<mb; i++) {
2033:     ncol = bi[i+1]-bi[i];
2034:     MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);
2035:     row++;
2036:     array += ncol; cols_tmp += ncol;
2037:   }
2038:   PetscFree(cols);

2040:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2041:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2042:   if (reuse == MAT_INITIAL_MATRIX || *matout != A) {
2043:     *matout = B;
2044:   } else {
2045:     MatHeaderMerge(A,B);
2046:   }
2047:   return(0);
2048: }

2052: PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
2053: {
2054:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
2055:   Mat            a    = aij->A,b = aij->B;
2057:   PetscInt       s1,s2,s3;

2060:   MatGetLocalSize(mat,&s2,&s3);
2061:   if (rr) {
2062:     VecGetLocalSize(rr,&s1);
2063:     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
2064:     /* Overlap communication with computation. */
2065:     VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);
2066:   }
2067:   if (ll) {
2068:     VecGetLocalSize(ll,&s1);
2069:     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
2070:     (*b->ops->diagonalscale)(b,ll,0);
2071:   }
2072:   /* scale  the diagonal block */
2073:   (*a->ops->diagonalscale)(a,ll,rr);

2075:   if (rr) {
2076:     /* Do a scatter end and then right scale the off-diagonal block */
2077:     VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);
2078:     (*b->ops->diagonalscale)(b,0,aij->lvec);
2079:   }
2080:   return(0);
2081: }

2085: PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
2086: {
2087:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2091:   MatSetUnfactored(a->A);
2092:   return(0);
2093: }

2097: PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool  *flag)
2098: {
2099:   Mat_MPIAIJ     *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
2100:   Mat            a,b,c,d;
2101:   PetscBool      flg;

2105:   a = matA->A; b = matA->B;
2106:   c = matB->A; d = matB->B;

2108:   MatEqual(a,c,&flg);
2109:   if (flg) {
2110:     MatEqual(b,d,&flg);
2111:   }
2112:   MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
2113:   return(0);
2114: }

2118: PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
2119: {
2121:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2122:   Mat_MPIAIJ     *b = (Mat_MPIAIJ*)B->data;

2125:   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
2126:   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
2127:     /* because of the column compression in the off-processor part of the matrix a->B,
2128:        the number of columns in a->B and b->B may be different, hence we cannot call
2129:        the MatCopy() directly on the two parts. If need be, we can provide a more
2130:        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
2131:        then copying the submatrices */
2132:     MatCopy_Basic(A,B,str);
2133:   } else {
2134:     MatCopy(a->A,b->A,str);
2135:     MatCopy(a->B,b->B,str);
2136:   }
2137:   return(0);
2138: }

2142: PetscErrorCode MatSetUp_MPIAIJ(Mat A)
2143: {

2147:    MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
2148:   return(0);
2149: }

2151: /*
2152:    Computes the number of nonzeros per row needed for preallocation when X and Y
2153:    have different nonzero structure.
2154: */
2157: 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)
2158: {
2159:   PetscInt       i,j,k,nzx,nzy;

2162:   /* Set the number of nonzeros in the new matrix */
2163:   for (i=0; i<m; i++) {
2164:     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2165:     nzx = xi[i+1] - xi[i];
2166:     nzy = yi[i+1] - yi[i];
2167:     nnz[i] = 0;
2168:     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2169:       for (; k<nzy && yltog[yjj[k]]<xltog[xjj[j]]; k++) nnz[i]++; /* Catch up to X */
2170:       if (k<nzy && yltog[yjj[k]]==xltog[xjj[j]]) k++;             /* Skip duplicate */
2171:       nnz[i]++;
2172:     }
2173:     for (; k<nzy; k++) nnz[i]++;
2174:   }
2175:   return(0);
2176: }

2178: /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */
2181: static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz)
2182: {
2184:   PetscInt       m = Y->rmap->N;
2185:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2186:   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;

2189:   MatAXPYGetPreallocation_MPIX_private(m,x->i,x->j,xltog,y->i,y->j,yltog,nnz);
2190:   return(0);
2191: }

2195: PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2196: {
2198:   PetscInt       i;
2199:   Mat_MPIAIJ     *xx = (Mat_MPIAIJ*)X->data,*yy = (Mat_MPIAIJ*)Y->data;
2200:   PetscBLASInt   bnz,one=1;
2201:   Mat_SeqAIJ     *x,*y;

2204:   if (str == SAME_NONZERO_PATTERN) {
2205:     PetscScalar alpha = a;
2206:     x    = (Mat_SeqAIJ*)xx->A->data;
2207:     PetscBLASIntCast(x->nz,&bnz);
2208:     y    = (Mat_SeqAIJ*)yy->A->data;
2209:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2210:     x    = (Mat_SeqAIJ*)xx->B->data;
2211:     y    = (Mat_SeqAIJ*)yy->B->data;
2212:     PetscBLASIntCast(x->nz,&bnz);
2213:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2214:     PetscObjectStateIncrease((PetscObject)Y);
2215:   } else if (str == SUBSET_NONZERO_PATTERN) {
2216:     MatAXPY_SeqAIJ(yy->A,a,xx->A,str);

2218:     x = (Mat_SeqAIJ*)xx->B->data;
2219:     y = (Mat_SeqAIJ*)yy->B->data;
2220:     if (y->xtoy && y->XtoY != xx->B) {
2221:       PetscFree(y->xtoy);
2222:       MatDestroy(&y->XtoY);
2223:     }
2224:     if (!y->xtoy) { /* get xtoy */
2225:       MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);
2226:       y->XtoY = xx->B;
2227:       PetscObjectReference((PetscObject)xx->B);
2228:     }
2229:     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2230:     PetscObjectStateIncrease((PetscObject)Y);
2231:   } else {
2232:     Mat      B;
2233:     PetscInt *nnz_d,*nnz_o;
2234:     PetscMalloc1(yy->A->rmap->N,&nnz_d);
2235:     PetscMalloc1(yy->B->rmap->N,&nnz_o);
2236:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
2237:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2238:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2239:     MatSetBlockSizesFromMats(B,Y,Y);
2240:     MatSetType(B,MATMPIAIJ);
2241:     MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);
2242:     MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);
2243:     MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);
2244:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2245:     MatHeaderReplace(Y,B);
2246:     PetscFree(nnz_d);
2247:     PetscFree(nnz_o);
2248:   }
2249:   return(0);
2250: }

2252: extern PetscErrorCode  MatConjugate_SeqAIJ(Mat);

2256: PetscErrorCode  MatConjugate_MPIAIJ(Mat mat)
2257: {
2258: #if defined(PETSC_USE_COMPLEX)
2260:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

2263:   MatConjugate_SeqAIJ(aij->A);
2264:   MatConjugate_SeqAIJ(aij->B);
2265: #else
2267: #endif
2268:   return(0);
2269: }

2273: PetscErrorCode MatRealPart_MPIAIJ(Mat A)
2274: {
2275:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2279:   MatRealPart(a->A);
2280:   MatRealPart(a->B);
2281:   return(0);
2282: }

2286: PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
2287: {
2288:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2292:   MatImaginaryPart(a->A);
2293:   MatImaginaryPart(a->B);
2294:   return(0);
2295: }

2297: #if defined(PETSC_HAVE_PBGL)

2299: #include <boost/parallel/mpi/bsp_process_group.hpp>
2300: #include <boost/graph/distributed/ilu_default_graph.hpp>
2301: #include <boost/graph/distributed/ilu_0_block.hpp>
2302: #include <boost/graph/distributed/ilu_preconditioner.hpp>
2303: #include <boost/graph/distributed/petsc/interface.hpp>
2304: #include <boost/multi_array.hpp>
2305: #include <boost/parallel/distributed_property_map->hpp>

2309: /*
2310:   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
2311: */
2312: PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info)
2313: {
2314:   namespace petsc = boost::distributed::petsc;

2316:   namespace graph_dist = boost::graph::distributed;
2317:   using boost::graph::distributed::ilu_default::process_group_type;
2318:   using boost::graph::ilu_permuted;

2320:   PetscBool      row_identity, col_identity;
2321:   PetscContainer c;
2322:   PetscInt       m, n, M, N;

2326:   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu");
2327:   ISIdentity(isrow, &row_identity);
2328:   ISIdentity(iscol, &col_identity);
2329:   if (!row_identity || !col_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU");

2331:   process_group_type pg;
2332:   typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type;
2333:   lgraph_type  *lgraph_p   = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg));
2334:   lgraph_type& level_graph = *lgraph_p;
2335:   graph_dist::ilu_default::graph_type&            graph(level_graph.graph);

2337:   petsc::read_matrix(A, graph, get(boost::edge_weight, graph));
2338:   ilu_permuted(level_graph);

2340:   /* put together the new matrix */
2341:   MatCreate(PetscObjectComm((PetscObject)A), fact);
2342:   MatGetLocalSize(A, &m, &n);
2343:   MatGetSize(A, &M, &N);
2344:   MatSetSizes(fact, m, n, M, N);
2345:   MatSetBlockSizesFromMats(fact,A,A);
2346:   MatSetType(fact, ((PetscObject)A)->type_name);
2347:   MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);
2348:   MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);

2350:   PetscContainerCreate(PetscObjectComm((PetscObject)A), &c);
2351:   PetscContainerSetPointer(c, lgraph_p);
2352:   PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c);
2353:   PetscContainerDestroy(&c);
2354:   return(0);
2355: }

2359: PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info)
2360: {
2362:   return(0);
2363: }

2367: /*
2368:   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
2369: */
2370: PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x)
2371: {
2372:   namespace graph_dist = boost::graph::distributed;

2374:   typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type;
2375:   lgraph_type    *lgraph_p;
2376:   PetscContainer c;

2380:   PetscObjectQuery((PetscObject) A, "graph", (PetscObject*) &c);
2381:   PetscContainerGetPointer(c, (void**) &lgraph_p);
2382:   VecCopy(b, x);

2384:   PetscScalar *array_x;
2385:   VecGetArray(x, &array_x);
2386:   PetscInt sx;
2387:   VecGetSize(x, &sx);

2389:   PetscScalar *array_b;
2390:   VecGetArray(b, &array_b);
2391:   PetscInt sb;
2392:   VecGetSize(b, &sb);

2394:   lgraph_type& level_graph = *lgraph_p;
2395:   graph_dist::ilu_default::graph_type&            graph(level_graph.graph);

2397:   typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type;
2398:   array_ref_type                                 ref_b(array_b, boost::extents[num_vertices(graph)]);
2399:   array_ref_type                                 ref_x(array_x, boost::extents[num_vertices(graph)]);

2401:   typedef boost::iterator_property_map<array_ref_type::iterator,
2402:                                        boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type>  gvector_type;
2403:   gvector_type                                   vector_b(ref_b.begin(), get(boost::vertex_index, graph));
2404:   gvector_type                                   vector_x(ref_x.begin(), get(boost::vertex_index, graph));

2406:   ilu_set_solve(*lgraph_p, vector_b, vector_x);
2407:   return(0);
2408: }
2409: #endif


2414: PetscErrorCode MatGetRedundantMatrix_MPIAIJ_interlaced(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant)
2415: {
2416:   PetscMPIInt    rank,size;
2417:   MPI_Comm       comm;
2419:   PetscInt       nsends=0,nrecvs=0,i,rownz_max=0,M=mat->rmap->N,N=mat->cmap->N;
2420:   PetscMPIInt    *send_rank= NULL,*recv_rank=NULL,subrank,subsize;
2421:   PetscInt       *rowrange = mat->rmap->range;
2422:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
2423:   Mat            A = aij->A,B=aij->B,C=*matredundant;
2424:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data;
2425:   PetscScalar    *sbuf_a;
2426:   PetscInt       nzlocal=a->nz+b->nz;
2427:   PetscInt       j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB;
2428:   PetscInt       rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray;
2429:   PetscInt       *cols,ctmp,lwrite,*rptr,l,*sbuf_j;
2430:   MatScalar      *aworkA,*aworkB;
2431:   PetscScalar    *vals;
2432:   PetscMPIInt    tag1,tag2,tag3,imdex;
2433:   MPI_Request    *s_waits1=NULL,*s_waits2=NULL,*s_waits3=NULL;
2434:   MPI_Request    *r_waits1=NULL,*r_waits2=NULL,*r_waits3=NULL;
2435:   MPI_Status     recv_status,*send_status;
2436:   PetscInt       *sbuf_nz=NULL,*rbuf_nz=NULL,count;
2437:   PetscInt       **rbuf_j=NULL;
2438:   PetscScalar    **rbuf_a=NULL;
2439:   Mat_Redundant  *redund =NULL;
2440: 
2442:   PetscObjectGetComm((PetscObject)mat,&comm);
2443:   MPI_Comm_rank(comm,&rank);
2444:   MPI_Comm_size(comm,&size);
2445:   MPI_Comm_rank(subcomm,&subrank);
2446:   MPI_Comm_size(subcomm,&subsize);

2448:   if (reuse == MAT_REUSE_MATRIX) {
2449:     if (M != mat->rmap->N || N != mat->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size");
2450:     if (subsize == 1) {
2451:       Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data;
2452:       redund = c->redundant;
2453:     } else {
2454:       Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data;
2455:       redund = c->redundant;
2456:     }
2457:     if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal");

2459:     nsends    = redund->nsends;
2460:     nrecvs    = redund->nrecvs;
2461:     send_rank = redund->send_rank;
2462:     recv_rank = redund->recv_rank;
2463:     sbuf_nz   = redund->sbuf_nz;
2464:     rbuf_nz   = redund->rbuf_nz;
2465:     sbuf_j    = redund->sbuf_j;
2466:     sbuf_a    = redund->sbuf_a;
2467:     rbuf_j    = redund->rbuf_j;
2468:     rbuf_a    = redund->rbuf_a;
2469:   }

2471:   if (reuse == MAT_INITIAL_MATRIX) {
2472:     PetscInt    nleftover,np_subcomm;

2474:     /* get the destination processors' id send_rank, nsends and nrecvs */
2475:     PetscMalloc2(size,&send_rank,size,&recv_rank);

2477:     np_subcomm = size/nsubcomm;
2478:     nleftover  = size - nsubcomm*np_subcomm;

2480:     /* block of codes below is specific for INTERLACED */
2481:     /* ------------------------------------------------*/
2482:     nsends = 0; nrecvs = 0;
2483:     for (i=0; i<size; i++) {
2484:       if (subrank == i/nsubcomm && i != rank) { /* my_subrank == other's subrank */
2485:         send_rank[nsends++] = i;
2486:         recv_rank[nrecvs++] = i;
2487:       }
2488:     }
2489:     if (rank >= size - nleftover) { /* this proc is a leftover processor */
2490:       i = size-nleftover-1;
2491:       j = 0;
2492:       while (j < nsubcomm - nleftover) {
2493:         send_rank[nsends++] = i;
2494:         i--; j++;
2495:       }
2496:     }

2498:     if (nleftover && subsize == size/nsubcomm && subrank==subsize-1) { /* this proc recvs from leftover processors */
2499:       for (i=0; i<nleftover; i++) {
2500:         recv_rank[nrecvs++] = size-nleftover+i;
2501:       }
2502:     }
2503:     /*----------------------------------------------*/

2505:     /* allocate sbuf_j, sbuf_a */
2506:     i    = nzlocal + rowrange[rank+1] - rowrange[rank] + 2;
2507:     PetscMalloc1(i,&sbuf_j);
2508:     PetscMalloc1((nzlocal+1),&sbuf_a);
2509:     /*
2510:     PetscSynchronizedPrintf(comm,"[%d] nsends %d, nrecvs %d\n",rank,nsends,nrecvs);
2511:     PetscSynchronizedFlush(comm,PETSC_STDOUT);
2512:      */
2513:   } /* endof if (reuse == MAT_INITIAL_MATRIX) */

2515:   /* copy mat's local entries into the buffers */
2516:   if (reuse == MAT_INITIAL_MATRIX) {
2517:     rownz_max = 0;
2518:     rptr      = sbuf_j;
2519:     cols      = sbuf_j + rend-rstart + 1;
2520:     vals      = sbuf_a;
2521:     rptr[0]   = 0;
2522:     for (i=0; i<rend-rstart; i++) {
2523:       row    = i + rstart;
2524:       nzA    = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i];
2525:       ncols  = nzA + nzB;
2526:       cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i];
2527:       aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i];
2528:       /* load the column indices for this row into cols */
2529:       lwrite = 0;
2530:       for (l=0; l<nzB; l++) {
2531:         if ((ctmp = bmap[cworkB[l]]) < cstart) {
2532:           vals[lwrite]   = aworkB[l];
2533:           cols[lwrite++] = ctmp;
2534:         }
2535:       }
2536:       for (l=0; l<nzA; l++) {
2537:         vals[lwrite]   = aworkA[l];
2538:         cols[lwrite++] = cstart + cworkA[l];
2539:       }
2540:       for (l=0; l<nzB; l++) {
2541:         if ((ctmp = bmap[cworkB[l]]) >= cend) {
2542:           vals[lwrite]   = aworkB[l];
2543:           cols[lwrite++] = ctmp;
2544:         }
2545:       }
2546:       vals     += ncols;
2547:       cols     += ncols;
2548:       rptr[i+1] = rptr[i] + ncols;
2549:       if (rownz_max < ncols) rownz_max = ncols;
2550:     }
2551:     if (rptr[rend-rstart] != a->nz + b->nz) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_PLIB, "rptr[%d] %d != %d + %d",rend-rstart,rptr[rend-rstart+1],a->nz,b->nz);
2552:   } else { /* only copy matrix values into sbuf_a */
2553:     rptr    = sbuf_j;
2554:     vals    = sbuf_a;
2555:     rptr[0] = 0;
2556:     for (i=0; i<rend-rstart; i++) {
2557:       row    = i + rstart;
2558:       nzA    = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i];
2559:       ncols  = nzA + nzB;
2560:       cworkB = b->j + b->i[i];
2561:       aworkA = a->a + a->i[i];
2562:       aworkB = b->a + b->i[i];
2563:       lwrite = 0;
2564:       for (l=0; l<nzB; l++) {
2565:         if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l];
2566:       }
2567:       for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l];
2568:       for (l=0; l<nzB; l++) {
2569:         if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l];
2570:       }
2571:       vals     += ncols;
2572:       rptr[i+1] = rptr[i] + ncols;
2573:     }
2574:   } /* endof if (reuse == MAT_INITIAL_MATRIX) */

2576:   /* send nzlocal to others, and recv other's nzlocal */
2577:   /*--------------------------------------------------*/
2578:   if (reuse == MAT_INITIAL_MATRIX) {
2579:     PetscMalloc2(3*(nsends + nrecvs)+1,&s_waits3,nsends+1,&send_status);

2581:     s_waits2 = s_waits3 + nsends;
2582:     s_waits1 = s_waits2 + nsends;
2583:     r_waits1 = s_waits1 + nsends;
2584:     r_waits2 = r_waits1 + nrecvs;
2585:     r_waits3 = r_waits2 + nrecvs;
2586:   } else {
2587:     PetscMalloc2(nsends + nrecvs +1,&s_waits3,nsends+1,&send_status);

2589:     r_waits3 = s_waits3 + nsends;
2590:   }

2592:   PetscObjectGetNewTag((PetscObject)mat,&tag3);
2593:   if (reuse == MAT_INITIAL_MATRIX) {
2594:     /* get new tags to keep the communication clean */
2595:     PetscObjectGetNewTag((PetscObject)mat,&tag1);
2596:     PetscObjectGetNewTag((PetscObject)mat,&tag2);
2597:     PetscMalloc4(nsends,&sbuf_nz,nrecvs,&rbuf_nz,nrecvs,&rbuf_j,nrecvs,&rbuf_a);

2599:     /* post receives of other's nzlocal */
2600:     for (i=0; i<nrecvs; i++) {
2601:       MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);
2602:     }
2603:     /* send nzlocal to others */
2604:     for (i=0; i<nsends; i++) {
2605:       sbuf_nz[i] = nzlocal;
2606:       MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);
2607:     }
2608:     /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */
2609:     count = nrecvs;
2610:     while (count) {
2611:       MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);

2613:       recv_rank[imdex] = recv_status.MPI_SOURCE;
2614:       /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */
2615:       PetscMalloc1((rbuf_nz[imdex]+1),&rbuf_a[imdex]);

2617:       i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */

2619:       rbuf_nz[imdex] += i + 2;

2621:       PetscMalloc1(rbuf_nz[imdex],&rbuf_j[imdex]);
2622:       MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);
2623:       count--;
2624:     }
2625:     /* wait on sends of nzlocal */
2626:     if (nsends) {MPI_Waitall(nsends,s_waits1,send_status);}
2627:     /* send mat->i,j to others, and recv from other's */
2628:     /*------------------------------------------------*/
2629:     for (i=0; i<nsends; i++) {
2630:       j    = nzlocal + rowrange[rank+1] - rowrange[rank] + 1;
2631:       MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);
2632:     }
2633:     /* wait on receives of mat->i,j */
2634:     /*------------------------------*/
2635:     count = nrecvs;
2636:     while (count) {
2637:       MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);
2638:       if (recv_rank[imdex] != recv_status.MPI_SOURCE) SETERRQ2(PETSC_COMM_SELF,1, "recv_rank %d != MPI_SOURCE %d",recv_rank[imdex],recv_status.MPI_SOURCE);
2639:       count--;
2640:     }
2641:     /* wait on sends of mat->i,j */
2642:     /*---------------------------*/
2643:     if (nsends) {
2644:       MPI_Waitall(nsends,s_waits2,send_status);
2645:     }
2646:   } /* endof if (reuse == MAT_INITIAL_MATRIX) */

2648:   /* post receives, send and receive mat->a */
2649:   /*----------------------------------------*/
2650:   for (imdex=0; imdex<nrecvs; imdex++) {
2651:     MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);
2652:   }
2653:   for (i=0; i<nsends; i++) {
2654:     MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);
2655:   }
2656:   count = nrecvs;
2657:   while (count) {
2658:     MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);
2659:     if (recv_rank[imdex] != recv_status.MPI_SOURCE) SETERRQ2(PETSC_COMM_SELF,1, "recv_rank %d != MPI_SOURCE %d",recv_rank[imdex],recv_status.MPI_SOURCE);
2660:     count--;
2661:   }
2662:   if (nsends) {
2663:     MPI_Waitall(nsends,s_waits3,send_status);
2664:   }

2666:   PetscFree2(s_waits3,send_status);

2668:   /* create redundant matrix */
2669:   /*-------------------------*/
2670:   if (reuse == MAT_INITIAL_MATRIX) {
2671:     const PetscInt *range;
2672:     PetscInt       rstart_sub,rend_sub,mloc_sub;

2674:     /* compute rownz_max for preallocation */
2675:     for (imdex=0; imdex<nrecvs; imdex++) {
2676:       j    = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]];
2677:       rptr = rbuf_j[imdex];
2678:       for (i=0; i<j; i++) {
2679:         ncols = rptr[i+1] - rptr[i];
2680:         if (rownz_max < ncols) rownz_max = ncols;
2681:       }
2682:     }

2684:     MatCreate(subcomm,&C);

2686:     /* get local size of redundant matrix
2687:        - mloc_sub is chosen for PETSC_SUBCOMM_INTERLACED, works for other types, but may not efficient! */
2688:     MatGetOwnershipRanges(mat,&range);
2689:     rstart_sub = range[nsubcomm*subrank];
2690:     if (subrank+1 < subsize) { /* not the last proc in subcomm */
2691:       rend_sub = range[nsubcomm*(subrank+1)];
2692:     } else {
2693:       rend_sub = mat->rmap->N;
2694:     }
2695:     mloc_sub = rend_sub - rstart_sub;

2697:     if (M == N) {
2698:       MatSetSizes(C,mloc_sub,mloc_sub,PETSC_DECIDE,PETSC_DECIDE);
2699:     } else { /* non-square matrix */
2700:       MatSetSizes(C,mloc_sub,PETSC_DECIDE,PETSC_DECIDE,mat->cmap->N);
2701:     }
2702:     MatSetBlockSizesFromMats(C,mat,mat);
2703:     MatSetFromOptions(C);
2704:     MatSeqAIJSetPreallocation(C,rownz_max,NULL);
2705:     MatMPIAIJSetPreallocation(C,rownz_max,NULL,rownz_max,NULL);
2706:   } else {
2707:     C = *matredundant;
2708:   }

2710:   /* insert local matrix entries */
2711:   rptr = sbuf_j;
2712:   cols = sbuf_j + rend-rstart + 1;
2713:   vals = sbuf_a;
2714:   for (i=0; i<rend-rstart; i++) {
2715:     row   = i + rstart;
2716:     ncols = rptr[i+1] - rptr[i];
2717:     MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);
2718:     vals += ncols;
2719:     cols += ncols;
2720:   }
2721:   /* insert received matrix entries */
2722:   for (imdex=0; imdex<nrecvs; imdex++) {
2723:     rstart = rowrange[recv_rank[imdex]];
2724:     rend   = rowrange[recv_rank[imdex]+1];
2725:     /* printf("[%d] insert rows %d - %d\n",rank,rstart,rend-1); */
2726:     rptr   = rbuf_j[imdex];
2727:     cols   = rbuf_j[imdex] + rend-rstart + 1;
2728:     vals   = rbuf_a[imdex];
2729:     for (i=0; i<rend-rstart; i++) {
2730:       row   = i + rstart;
2731:       ncols = rptr[i+1] - rptr[i];
2732:       MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);
2733:       vals += ncols;
2734:       cols += ncols;
2735:     }
2736:   }
2737:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2738:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

2740:   if (reuse == MAT_INITIAL_MATRIX) {
2741:     *matredundant = C;

2743:     /* create a supporting struct and attach it to C for reuse */
2744:     PetscNewLog(C,&redund);
2745:     if (subsize == 1) {
2746:       Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data;
2747:       c->redundant = redund;
2748:     } else {
2749:       Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data;
2750:       c->redundant = redund;
2751:     }

2753:     redund->nzlocal   = nzlocal;
2754:     redund->nsends    = nsends;
2755:     redund->nrecvs    = nrecvs;
2756:     redund->send_rank = send_rank;
2757:     redund->recv_rank = recv_rank;
2758:     redund->sbuf_nz   = sbuf_nz;
2759:     redund->rbuf_nz   = rbuf_nz;
2760:     redund->sbuf_j    = sbuf_j;
2761:     redund->sbuf_a    = sbuf_a;
2762:     redund->rbuf_j    = rbuf_j;
2763:     redund->rbuf_a    = rbuf_a;
2764:     redund->psubcomm  = NULL;
2765:   }
2766:   return(0);
2767: }

2771: PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant)
2772: {
2774:   MPI_Comm       comm;
2775:   PetscMPIInt    size,subsize;
2776:   PetscInt       mloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N;
2777:   Mat_Redundant  *redund=NULL;
2778:   PetscSubcomm   psubcomm=NULL;
2779:   MPI_Comm       subcomm_in=subcomm;
2780:   Mat            *matseq;
2781:   IS             isrow,iscol;

2784:   if (subcomm_in == MPI_COMM_NULL) { /* user does not provide subcomm */
2785:     if (reuse ==  MAT_INITIAL_MATRIX) {
2786:       /* create psubcomm, then get subcomm */
2787:       PetscObjectGetComm((PetscObject)mat,&comm);
2788:       MPI_Comm_size(comm,&size);
2789:       if (nsubcomm < 1 || nsubcomm > size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %D",size);

2791:       PetscSubcommCreate(comm,&psubcomm);
2792:       PetscSubcommSetNumber(psubcomm,nsubcomm);
2793:       PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);
2794:       PetscSubcommSetFromOptions(psubcomm);
2795:       subcomm = psubcomm->comm;
2796:     } else { /* retrieve psubcomm and subcomm */
2797:       PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);
2798:       MPI_Comm_size(subcomm,&subsize);
2799:       if (subsize == 1) {
2800:         Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data;
2801:         redund = c->redundant;
2802:       } else {
2803:         Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data;
2804:         redund = c->redundant;
2805:       }
2806:       psubcomm = redund->psubcomm;
2807:     }
2808:     if (psubcomm->type == PETSC_SUBCOMM_INTERLACED) {
2809:       MatGetRedundantMatrix_MPIAIJ_interlaced(mat,nsubcomm,subcomm,reuse,matredundant);
2810:       if (reuse ==  MAT_INITIAL_MATRIX) { /* psubcomm is created in this routine, free it in MatDestroy_Redundant() */
2811:         MPI_Comm_size(psubcomm->comm,&subsize);
2812:         if (subsize == 1) {
2813:           Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data;
2814:           c->redundant->psubcomm = psubcomm;
2815:         } else {
2816:           Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data;
2817:           c->redundant->psubcomm = psubcomm ;
2818:         }
2819:       }
2820:       return(0);
2821:     }
2822:   }

2824:   /* use MPI subcomm via MatGetSubMatrices(); use subcomm_in or psubcomm->comm (psubcomm->type != INTERLACED) */
2825:   MPI_Comm_size(subcomm,&subsize);
2826:   if (reuse == MAT_INITIAL_MATRIX) {
2827:     /* create a local sequential matrix matseq[0] */
2828:     mloc_sub = PETSC_DECIDE;
2829:     PetscSplitOwnership(subcomm,&mloc_sub,&M);
2830:     MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);
2831:     rstart = rend - mloc_sub;
2832:     ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);
2833:     ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);
2834:   } else { /* reuse == MAT_REUSE_MATRIX */
2835:     if (subsize == 1) {
2836:       Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data;
2837:       redund = c->redundant;
2838:     } else {
2839:       Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data;
2840:       redund = c->redundant;
2841:     }

2843:     isrow  = redund->isrow;
2844:     iscol  = redund->iscol;
2845:     matseq = redund->matseq;
2846:   }
2847:   MatGetSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);
2848:   MatCreateMPIAIJConcatenateSeqAIJ(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);

2850:   if (reuse == MAT_INITIAL_MATRIX) {
2851:     /* create a supporting struct and attach it to C for reuse */
2852:     PetscNewLog(*matredundant,&redund);
2853:     if (subsize == 1) {
2854:       Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data;
2855:       c->redundant = redund;
2856:     } else {
2857:       Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data;
2858:       c->redundant = redund;
2859:     }
2860:     redund->isrow    = isrow;
2861:     redund->iscol    = iscol;
2862:     redund->matseq   = matseq;
2863:     redund->psubcomm = psubcomm;
2864:   }
2865:   return(0);
2866: }

2870: PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2871: {
2872:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2874:   PetscInt       i,*idxb = 0;
2875:   PetscScalar    *va,*vb;
2876:   Vec            vtmp;

2879:   MatGetRowMaxAbs(a->A,v,idx);
2880:   VecGetArray(v,&va);
2881:   if (idx) {
2882:     for (i=0; i<A->rmap->n; i++) {
2883:       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2884:     }
2885:   }

2887:   VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);
2888:   if (idx) {
2889:     PetscMalloc1(A->rmap->n,&idxb);
2890:   }
2891:   MatGetRowMaxAbs(a->B,vtmp,idxb);
2892:   VecGetArray(vtmp,&vb);

2894:   for (i=0; i<A->rmap->n; i++) {
2895:     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
2896:       va[i] = vb[i];
2897:       if (idx) idx[i] = a->garray[idxb[i]];
2898:     }
2899:   }

2901:   VecRestoreArray(v,&va);
2902:   VecRestoreArray(vtmp,&vb);
2903:   PetscFree(idxb);
2904:   VecDestroy(&vtmp);
2905:   return(0);
2906: }

2910: PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2911: {
2912:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2914:   PetscInt       i,*idxb = 0;
2915:   PetscScalar    *va,*vb;
2916:   Vec            vtmp;

2919:   MatGetRowMinAbs(a->A,v,idx);
2920:   VecGetArray(v,&va);
2921:   if (idx) {
2922:     for (i=0; i<A->cmap->n; i++) {
2923:       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2924:     }
2925:   }

2927:   VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);
2928:   if (idx) {
2929:     PetscMalloc1(A->rmap->n,&idxb);
2930:   }
2931:   MatGetRowMinAbs(a->B,vtmp,idxb);
2932:   VecGetArray(vtmp,&vb);

2934:   for (i=0; i<A->rmap->n; i++) {
2935:     if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) {
2936:       va[i] = vb[i];
2937:       if (idx) idx[i] = a->garray[idxb[i]];
2938:     }
2939:   }

2941:   VecRestoreArray(v,&va);
2942:   VecRestoreArray(vtmp,&vb);
2943:   PetscFree(idxb);
2944:   VecDestroy(&vtmp);
2945:   return(0);
2946: }

2950: PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2951: {
2952:   Mat_MPIAIJ     *mat   = (Mat_MPIAIJ*) A->data;
2953:   PetscInt       n      = A->rmap->n;
2954:   PetscInt       cstart = A->cmap->rstart;
2955:   PetscInt       *cmap  = mat->garray;
2956:   PetscInt       *diagIdx, *offdiagIdx;
2957:   Vec            diagV, offdiagV;
2958:   PetscScalar    *a, *diagA, *offdiagA;
2959:   PetscInt       r;

2963:   PetscMalloc2(n,&diagIdx,n,&offdiagIdx);
2964:   VecCreateSeq(PetscObjectComm((PetscObject)A), n, &diagV);
2965:   VecCreateSeq(PetscObjectComm((PetscObject)A), n, &offdiagV);
2966:   MatGetRowMin(mat->A, diagV,    diagIdx);
2967:   MatGetRowMin(mat->B, offdiagV, offdiagIdx);
2968:   VecGetArray(v,        &a);
2969:   VecGetArray(diagV,    &diagA);
2970:   VecGetArray(offdiagV, &offdiagA);
2971:   for (r = 0; r < n; ++r) {
2972:     if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) {
2973:       a[r]   = diagA[r];
2974:       idx[r] = cstart + diagIdx[r];
2975:     } else {
2976:       a[r]   = offdiagA[r];
2977:       idx[r] = cmap[offdiagIdx[r]];
2978:     }
2979:   }
2980:   VecRestoreArray(v,        &a);
2981:   VecRestoreArray(diagV,    &diagA);
2982:   VecRestoreArray(offdiagV, &offdiagA);
2983:   VecDestroy(&diagV);
2984:   VecDestroy(&offdiagV);
2985:   PetscFree2(diagIdx, offdiagIdx);
2986:   return(0);
2987: }

2991: PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2992: {
2993:   Mat_MPIAIJ     *mat   = (Mat_MPIAIJ*) A->data;
2994:   PetscInt       n      = A->rmap->n;
2995:   PetscInt       cstart = A->cmap->rstart;
2996:   PetscInt       *cmap  = mat->garray;
2997:   PetscInt       *diagIdx, *offdiagIdx;
2998:   Vec            diagV, offdiagV;
2999:   PetscScalar    *a, *diagA, *offdiagA;
3000:   PetscInt       r;

3004:   PetscMalloc2(n,&diagIdx,n,&offdiagIdx);
3005:   VecCreateSeq(PETSC_COMM_SELF, n, &diagV);
3006:   VecCreateSeq(PETSC_COMM_SELF, n, &offdiagV);
3007:   MatGetRowMax(mat->A, diagV,    diagIdx);
3008:   MatGetRowMax(mat->B, offdiagV, offdiagIdx);
3009:   VecGetArray(v,        &a);
3010:   VecGetArray(diagV,    &diagA);
3011:   VecGetArray(offdiagV, &offdiagA);
3012:   for (r = 0; r < n; ++r) {
3013:     if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) {
3014:       a[r]   = diagA[r];
3015:       idx[r] = cstart + diagIdx[r];
3016:     } else {
3017:       a[r]   = offdiagA[r];
3018:       idx[r] = cmap[offdiagIdx[r]];
3019:     }
3020:   }
3021:   VecRestoreArray(v,        &a);
3022:   VecRestoreArray(diagV,    &diagA);
3023:   VecRestoreArray(offdiagV, &offdiagA);
3024:   VecDestroy(&diagV);
3025:   VecDestroy(&offdiagV);
3026:   PetscFree2(diagIdx, offdiagIdx);
3027:   return(0);
3028: }

3032: PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat)
3033: {
3035:   Mat            *dummy;

3038:   MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);
3039:   *newmat = *dummy;
3040:   PetscFree(dummy);
3041:   return(0);
3042: }

3046: PetscErrorCode  MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values)
3047: {
3048:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*) A->data;

3052:   MatInvertBlockDiagonal(a->A,values);
3053:   return(0);
3054: }

3058: static PetscErrorCode  MatSetRandom_MPIAIJ(Mat x,PetscRandom rctx)
3059: {
3061:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)x->data;

3064:   MatSetRandom(aij->A,rctx);
3065:   MatSetRandom(aij->B,rctx);
3066:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3067:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3068:   return(0);
3069: }

3071: /* -------------------------------------------------------------------*/
3072: static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
3073:                                        MatGetRow_MPIAIJ,
3074:                                        MatRestoreRow_MPIAIJ,
3075:                                        MatMult_MPIAIJ,
3076:                                 /* 4*/ MatMultAdd_MPIAIJ,
3077:                                        MatMultTranspose_MPIAIJ,
3078:                                        MatMultTransposeAdd_MPIAIJ,
3079: #if defined(PETSC_HAVE_PBGL)
3080:                                        MatSolve_MPIAIJ,
3081: #else
3082:                                        0,
3083: #endif
3084:                                        0,
3085:                                        0,
3086:                                 /*10*/ 0,
3087:                                        0,
3088:                                        0,
3089:                                        MatSOR_MPIAIJ,
3090:                                        MatTranspose_MPIAIJ,
3091:                                 /*15*/ MatGetInfo_MPIAIJ,
3092:                                        MatEqual_MPIAIJ,
3093:                                        MatGetDiagonal_MPIAIJ,
3094:                                        MatDiagonalScale_MPIAIJ,
3095:                                        MatNorm_MPIAIJ,
3096:                                 /*20*/ MatAssemblyBegin_MPIAIJ,
3097:                                        MatAssemblyEnd_MPIAIJ,
3098:                                        MatSetOption_MPIAIJ,
3099:                                        MatZeroEntries_MPIAIJ,
3100:                                 /*24*/ MatZeroRows_MPIAIJ,
3101:                                        0,
3102: #if defined(PETSC_HAVE_PBGL)
3103:                                        0,
3104: #else
3105:                                        0,
3106: #endif
3107:                                        0,
3108:                                        0,
3109:                                 /*29*/ MatSetUp_MPIAIJ,
3110: #if defined(PETSC_HAVE_PBGL)
3111:                                        0,
3112: #else
3113:                                        0,
3114: #endif
3115:                                        0,
3116:                                        0,
3117:                                        0,
3118:                                 /*34*/ MatDuplicate_MPIAIJ,
3119:                                        0,
3120:                                        0,
3121:                                        0,
3122:                                        0,
3123:                                 /*39*/ MatAXPY_MPIAIJ,
3124:                                        MatGetSubMatrices_MPIAIJ,
3125:                                        MatIncreaseOverlap_MPIAIJ,
3126:                                        MatGetValues_MPIAIJ,
3127:                                        MatCopy_MPIAIJ,
3128:                                 /*44*/ MatGetRowMax_MPIAIJ,
3129:                                        MatScale_MPIAIJ,
3130:                                        0,
3131:                                        MatDiagonalSet_MPIAIJ,
3132:                                        MatZeroRowsColumns_MPIAIJ,
3133:                                 /*49*/ MatSetRandom_MPIAIJ,
3134:                                        0,
3135:                                        0,
3136:                                        0,
3137:                                        0,
3138:                                 /*54*/ MatFDColoringCreate_MPIXAIJ,
3139:                                        0,
3140:                                        MatSetUnfactored_MPIAIJ,
3141:                                        MatPermute_MPIAIJ,
3142:                                        0,
3143:                                 /*59*/ MatGetSubMatrix_MPIAIJ,
3144:                                        MatDestroy_MPIAIJ,
3145:                                        MatView_MPIAIJ,
3146:                                        0,
3147:                                        MatMatMatMult_MPIAIJ_MPIAIJ_MPIAIJ,
3148:                                 /*64*/ MatMatMatMultSymbolic_MPIAIJ_MPIAIJ_MPIAIJ,
3149:                                        MatMatMatMultNumeric_MPIAIJ_MPIAIJ_MPIAIJ,
3150:                                        0,
3151:                                        0,
3152:                                        0,
3153:                                 /*69*/ MatGetRowMaxAbs_MPIAIJ,
3154:                                        MatGetRowMinAbs_MPIAIJ,
3155:                                        0,
3156:                                        MatSetColoring_MPIAIJ,
3157:                                        0,
3158:                                        MatSetValuesAdifor_MPIAIJ,
3159:                                 /*75*/ MatFDColoringApply_AIJ,
3160:                                        0,
3161:                                        0,
3162:                                        0,
3163:                                        MatFindZeroDiagonals_MPIAIJ,
3164:                                 /*80*/ 0,
3165:                                        0,
3166:                                        0,
3167:                                 /*83*/ MatLoad_MPIAIJ,
3168:                                        0,
3169:                                        0,
3170:                                        0,
3171:                                        0,
3172:                                        0,
3173:                                 /*89*/ MatMatMult_MPIAIJ_MPIAIJ,
3174:                                        MatMatMultSymbolic_MPIAIJ_MPIAIJ,
3175:                                        MatMatMultNumeric_MPIAIJ_MPIAIJ,
3176:                                        MatPtAP_MPIAIJ_MPIAIJ,
3177:                                        MatPtAPSymbolic_MPIAIJ_MPIAIJ,
3178:                                 /*94*/ MatPtAPNumeric_MPIAIJ_MPIAIJ,
3179:                                        0,
3180:                                        0,
3181:                                        0,
3182:                                        0,
3183:                                 /*99*/ 0,
3184:                                        0,
3185:                                        0,
3186:                                        MatConjugate_MPIAIJ,
3187:                                        0,
3188:                                 /*104*/MatSetValuesRow_MPIAIJ,
3189:                                        MatRealPart_MPIAIJ,
3190:                                        MatImaginaryPart_MPIAIJ,
3191:                                        0,
3192:                                        0,
3193:                                 /*109*/0,
3194:                                        MatGetRedundantMatrix_MPIAIJ,
3195:                                        MatGetRowMin_MPIAIJ,
3196:                                        0,
3197:                                        0,
3198:                                 /*114*/MatGetSeqNonzeroStructure_MPIAIJ,
3199:                                        0,
3200:                                        0,
3201:                                        0,
3202:                                        0,
3203:                                 /*119*/0,
3204:                                        0,
3205:                                        0,
3206:                                        0,
3207:                                        MatGetMultiProcBlock_MPIAIJ,
3208:                                 /*124*/MatFindNonzeroRows_MPIAIJ,
3209:                                        MatGetColumnNorms_MPIAIJ,
3210:                                        MatInvertBlockDiagonal_MPIAIJ,
3211:                                        0,
3212:                                        MatGetSubMatricesParallel_MPIAIJ,
3213:                                 /*129*/0,
3214:                                        MatTransposeMatMult_MPIAIJ_MPIAIJ,
3215:                                        MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ,
3216:                                        MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ,
3217:                                        0,
3218:                                 /*134*/0,
3219:                                        0,
3220:                                        0,
3221:                                        0,
3222:                                        0,
3223:                                 /*139*/0,
3224:                                        0,
3225:                                        0,
3226:                                        MatFDColoringSetUp_MPIXAIJ
3227: };

3229: /* ----------------------------------------------------------------------------------------*/

3233: PetscErrorCode  MatStoreValues_MPIAIJ(Mat mat)
3234: {
3235:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

3239:   MatStoreValues(aij->A);
3240:   MatStoreValues(aij->B);
3241:   return(0);
3242: }

3246: PetscErrorCode  MatRetrieveValues_MPIAIJ(Mat mat)
3247: {
3248:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

3252:   MatRetrieveValues(aij->A);
3253:   MatRetrieveValues(aij->B);
3254:   return(0);
3255: }

3259: PetscErrorCode  MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3260: {
3261:   Mat_MPIAIJ     *b;

3265:   PetscLayoutSetUp(B->rmap);
3266:   PetscLayoutSetUp(B->cmap);
3267:   b = (Mat_MPIAIJ*)B->data;

3269:   if (!B->preallocated) {
3270:     /* Explicitly create 2 MATSEQAIJ matrices. */
3271:     MatCreate(PETSC_COMM_SELF,&b->A);
3272:     MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
3273:     MatSetBlockSizesFromMats(b->A,B,B);
3274:     MatSetType(b->A,MATSEQAIJ);
3275:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
3276:     MatCreate(PETSC_COMM_SELF,&b->B);
3277:     MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);
3278:     MatSetBlockSizesFromMats(b->B,B,B);
3279:     MatSetType(b->B,MATSEQAIJ);
3280:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);
3281:   }

3283:   MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);
3284:   MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);
3285:   B->preallocated = PETSC_TRUE;
3286:   return(0);
3287: }

3291: PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
3292: {
3293:   Mat            mat;
3294:   Mat_MPIAIJ     *a,*oldmat = (Mat_MPIAIJ*)matin->data;

3298:   *newmat = 0;
3299:   MatCreate(PetscObjectComm((PetscObject)matin),&mat);
3300:   MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);
3301:   MatSetBlockSizesFromMats(mat,matin,matin);
3302:   MatSetType(mat,((PetscObject)matin)->type_name);
3303:   PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));
3304:   a       = (Mat_MPIAIJ*)mat->data;

3306:   mat->factortype   = matin->factortype;
3307:   mat->assembled    = PETSC_TRUE;
3308:   mat->insertmode   = NOT_SET_VALUES;
3309:   mat->preallocated = PETSC_TRUE;

3311:   a->size         = oldmat->size;
3312:   a->rank         = oldmat->rank;
3313:   a->donotstash   = oldmat->donotstash;
3314:   a->roworiented  = oldmat->roworiented;
3315:   a->rowindices   = 0;
3316:   a->rowvalues    = 0;
3317:   a->getrowactive = PETSC_FALSE;

3319:   PetscLayoutReference(matin->rmap,&mat->rmap);
3320:   PetscLayoutReference(matin->cmap,&mat->cmap);

3322:   if (oldmat->colmap) {
3323: #if defined(PETSC_USE_CTABLE)
3324:     PetscTableCreateCopy(oldmat->colmap,&a->colmap);
3325: #else
3326:     PetscMalloc1((mat->cmap->N),&a->colmap);
3327:     PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N)*sizeof(PetscInt));
3328:     PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));
3329: #endif
3330:   } else a->colmap = 0;
3331:   if (oldmat->garray) {
3332:     PetscInt len;
3333:     len  = oldmat->B->cmap->n;
3334:     PetscMalloc1((len+1),&a->garray);
3335:     PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));
3336:     if (len) { PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt)); }
3337:   } else a->garray = 0;

3339:   VecDuplicate(oldmat->lvec,&a->lvec);
3340:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);
3341:   VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
3342:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);
3343:   MatDuplicate(oldmat->A,cpvalues,&a->A);
3344:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
3345:   MatDuplicate(oldmat->B,cpvalues,&a->B);
3346:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);
3347:   PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);
3348:   *newmat = mat;
3349:   return(0);
3350: }



3356: PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer)
3357: {
3358:   PetscScalar    *vals,*svals;
3359:   MPI_Comm       comm;
3361:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
3362:   PetscInt       i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols;
3363:   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols;
3364:   PetscInt       *ourlens = NULL,*procsnz = NULL,*offlens = NULL,jj,*mycols,*smycols;
3365:   PetscInt       cend,cstart,n,*rowners,sizesset=1;
3366:   int            fd;
3367:   PetscInt       bs = 1;

3370:   PetscObjectGetComm((PetscObject)viewer,&comm);
3371:   MPI_Comm_size(comm,&size);
3372:   MPI_Comm_rank(comm,&rank);
3373:   if (!rank) {
3374:     PetscViewerBinaryGetDescriptor(viewer,&fd);
3375:     PetscBinaryRead(fd,(char*)header,4,PETSC_INT);
3376:     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
3377:   }

3379:   PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");
3380:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
3381:   PetscOptionsEnd();

3383:   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0;

3385:   MPI_Bcast(header+1,3,MPIU_INT,0,comm);
3386:   M    = header[1]; N = header[2];
3387:   /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */
3388:   if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M;
3389:   if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N;

3391:   /* If global sizes are set, check if they are consistent with that given in the file */
3392:   if (sizesset) {
3393:     MatGetSize(newMat,&grows,&gcols);
3394:   }
3395:   if (sizesset && newMat->rmap->N != grows) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows:Matrix in file has (%d) and input matrix has (%d)",M,grows);
3396:   if (sizesset && newMat->cmap->N != gcols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of cols:Matrix in file has (%d) and input matrix has (%d)",N,gcols);

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

3403:   PetscMalloc1((size+1),&rowners);
3404:   MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);

3406:   /* First process needs enough room for process with most rows */
3407:   if (!rank) {
3408:     mmax = rowners[1];
3409:     for (i=2; i<=size; i++) {
3410:       mmax = PetscMax(mmax, rowners[i]);
3411:     }
3412:   } else mmax = -1;             /* unused, but compilers complain */

3414:   rowners[0] = 0;
3415:   for (i=2; i<=size; i++) {
3416:     rowners[i] += rowners[i-1];
3417:   }
3418:   rstart = rowners[rank];
3419:   rend   = rowners[rank+1];

3421:   /* distribute row lengths to all processors */
3422:   PetscMalloc2(m,&ourlens,m,&offlens);
3423:   if (!rank) {
3424:     PetscBinaryRead(fd,ourlens,m,PETSC_INT);
3425:     PetscMalloc1(mmax,&rowlengths);
3426:     PetscCalloc1(size,&procsnz);
3427:     for (j=0; j<m; j++) {
3428:       procsnz[0] += ourlens[j];
3429:     }
3430:     for (i=1; i<size; i++) {
3431:       PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);
3432:       /* calculate the number of nonzeros on each processor */
3433:       for (j=0; j<rowners[i+1]-rowners[i]; j++) {
3434:         procsnz[i] += rowlengths[j];
3435:       }
3436:       MPIULong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);
3437:     }
3438:     PetscFree(rowlengths);
3439:   } else {
3440:     MPIULong_Recv(ourlens,m,MPIU_INT,0,tag,comm);
3441:   }

3443:   if (!rank) {
3444:     /* determine max buffer needed and allocate it */
3445:     maxnz = 0;
3446:     for (i=0; i<size; i++) {
3447:       maxnz = PetscMax(maxnz,procsnz[i]);
3448:     }
3449:     PetscMalloc1(maxnz,&cols);

3451:     /* read in my part of the matrix column indices  */
3452:     nz   = procsnz[0];
3453:     PetscMalloc1(nz,&mycols);
3454:     PetscBinaryRead(fd,mycols,nz,PETSC_INT);

3456:     /* read in every one elses and ship off */
3457:     for (i=1; i<size; i++) {
3458:       nz   = procsnz[i];
3459:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
3460:       MPIULong_Send(cols,nz,MPIU_INT,i,tag,comm);
3461:     }
3462:     PetscFree(cols);
3463:   } else {
3464:     /* determine buffer space needed for message */
3465:     nz = 0;
3466:     for (i=0; i<m; i++) {
3467:       nz += ourlens[i];
3468:     }
3469:     PetscMalloc1(nz,&mycols);

3471:     /* receive message of column indices*/
3472:     MPIULong_Recv(mycols,nz,MPIU_INT,0,tag,comm);
3473:   }

3475:   /* determine column ownership if matrix is not square */
3476:   if (N != M) {
3477:     if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank);
3478:     else n = newMat->cmap->n;
3479:     MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);
3480:     cstart = cend - n;
3481:   } else {
3482:     cstart = rstart;
3483:     cend   = rend;
3484:     n      = cend - cstart;
3485:   }

3487:   /* loop over local rows, determining number of off diagonal entries */
3488:   PetscMemzero(offlens,m*sizeof(PetscInt));
3489:   jj   = 0;
3490:   for (i=0; i<m; i++) {
3491:     for (j=0; j<ourlens[i]; j++) {
3492:       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
3493:       jj++;
3494:     }
3495:   }

3497:   for (i=0; i<m; i++) {
3498:     ourlens[i] -= offlens[i];
3499:   }
3500:   if (!sizesset) {
3501:     MatSetSizes(newMat,m,n,M,N);
3502:   }

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

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

3508:   for (i=0; i<m; i++) {
3509:     ourlens[i] += offlens[i];
3510:   }

3512:   if (!rank) {
3513:     PetscMalloc1((maxnz+1),&vals);

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

3519:     /* insert into matrix */
3520:     jj      = rstart;
3521:     smycols = mycols;
3522:     svals   = vals;
3523:     for (i=0; i<m; i++) {
3524:       MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
3525:       smycols += ourlens[i];
3526:       svals   += ourlens[i];
3527:       jj++;
3528:     }

3530:     /* read in other processors and ship out */
3531:     for (i=1; i<size; i++) {
3532:       nz   = procsnz[i];
3533:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3534:       MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);
3535:     }
3536:     PetscFree(procsnz);
3537:   } else {
3538:     /* receive numeric values */
3539:     PetscMalloc1((nz+1),&vals);

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

3544:     /* insert into matrix */
3545:     jj      = rstart;
3546:     smycols = mycols;
3547:     svals   = vals;
3548:     for (i=0; i<m; i++) {
3549:       MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
3550:       smycols += ourlens[i];
3551:       svals   += ourlens[i];
3552:       jj++;
3553:     }
3554:   }
3555:   PetscFree2(ourlens,offlens);
3556:   PetscFree(vals);
3557:   PetscFree(mycols);
3558:   PetscFree(rowners);
3559:   MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);
3560:   MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);
3561:   return(0);
3562: }

3566: PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
3567: {
3569:   IS             iscol_local;
3570:   PetscInt       csize;

3573:   ISGetLocalSize(iscol,&csize);
3574:   if (call == MAT_REUSE_MATRIX) {
3575:     PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);
3576:     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3577:   } else {
3578:     PetscInt cbs;
3579:     ISGetBlockSize(iscol,&cbs);
3580:     ISAllGather(iscol,&iscol_local);
3581:     ISSetBlockSize(iscol_local,cbs);
3582:   }
3583:   MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);
3584:   if (call == MAT_INITIAL_MATRIX) {
3585:     PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);
3586:     ISDestroy(&iscol_local);
3587:   }
3588:   return(0);
3589: }

3591: extern PetscErrorCode MatGetSubMatrices_MPIAIJ_Local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,Mat*);
3594: /*
3595:     Not great since it makes two copies of the submatrix, first an SeqAIJ
3596:   in local and then by concatenating the local matrices the end result.
3597:   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()

3599:   Note: This requires a sequential iscol with all indices.
3600: */
3601: PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
3602: {
3604:   PetscMPIInt    rank,size;
3605:   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs;
3606:   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol;
3607:   PetscBool      allcolumns, colflag;
3608:   Mat            M,Mreuse;
3609:   MatScalar      *vwork,*aa;
3610:   MPI_Comm       comm;
3611:   Mat_SeqAIJ     *aij;

3614:   PetscObjectGetComm((PetscObject)mat,&comm);
3615:   MPI_Comm_rank(comm,&rank);
3616:   MPI_Comm_size(comm,&size);

3618:   ISIdentity(iscol,&colflag);
3619:   ISGetLocalSize(iscol,&ncol);
3620:   if (colflag && ncol == mat->cmap->N) {
3621:     allcolumns = PETSC_TRUE;
3622:   } else {
3623:     allcolumns = PETSC_FALSE;
3624:   }
3625:   if (call ==  MAT_REUSE_MATRIX) {
3626:     PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);
3627:     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3628:     MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&allcolumns,&Mreuse);
3629:   } else {
3630:     MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&allcolumns,&Mreuse);
3631:   }

3633:   /*
3634:       m - number of local rows
3635:       n - number of columns (same on all processors)
3636:       rstart - first row in new global matrix generated
3637:   */
3638:   MatGetSize(Mreuse,&m,&n);
3639:   MatGetBlockSizes(Mreuse,&bs,&cbs);
3640:   if (call == MAT_INITIAL_MATRIX) {
3641:     aij = (Mat_SeqAIJ*)(Mreuse)->data;
3642:     ii  = aij->i;
3643:     jj  = aij->j;

3645:     /*
3646:         Determine the number of non-zeros in the diagonal and off-diagonal
3647:         portions of the matrix in order to do correct preallocation
3648:     */

3650:     /* first get start and end of "diagonal" columns */
3651:     if (csize == PETSC_DECIDE) {
3652:       ISGetSize(isrow,&mglobal);
3653:       if (mglobal == n) { /* square matrix */
3654:         nlocal = m;
3655:       } else {
3656:         nlocal = n/size + ((n % size) > rank);
3657:       }
3658:     } else {
3659:       nlocal = csize;
3660:     }
3661:     MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
3662:     rstart = rend - nlocal;
3663:     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);

3665:     /* next, compute all the lengths */
3666:     PetscMalloc1((2*m+1),&dlens);
3667:     olens = dlens + m;
3668:     for (i=0; i<m; i++) {
3669:       jend = ii[i+1] - ii[i];
3670:       olen = 0;
3671:       dlen = 0;
3672:       for (j=0; j<jend; j++) {
3673:         if (*jj < rstart || *jj >= rend) olen++;
3674:         else dlen++;
3675:         jj++;
3676:       }
3677:       olens[i] = olen;
3678:       dlens[i] = dlen;
3679:     }
3680:     MatCreate(comm,&M);
3681:     MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);
3682:     MatSetBlockSizes(M,bs,cbs);
3683:     MatSetType(M,((PetscObject)mat)->type_name);
3684:     MatMPIAIJSetPreallocation(M,0,dlens,0,olens);
3685:     PetscFree(dlens);
3686:   } else {
3687:     PetscInt ml,nl;

3689:     M    = *newmat;
3690:     MatGetLocalSize(M,&ml,&nl);
3691:     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
3692:     MatZeroEntries(M);
3693:     /*
3694:          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
3695:        rather than the slower MatSetValues().
3696:     */
3697:     M->was_assembled = PETSC_TRUE;
3698:     M->assembled     = PETSC_FALSE;
3699:   }
3700:   MatGetOwnershipRange(M,&rstart,&rend);
3701:   aij  = (Mat_SeqAIJ*)(Mreuse)->data;
3702:   ii   = aij->i;
3703:   jj   = aij->j;
3704:   aa   = aij->a;
3705:   for (i=0; i<m; i++) {
3706:     row   = rstart + i;
3707:     nz    = ii[i+1] - ii[i];
3708:     cwork = jj;     jj += nz;
3709:     vwork = aa;     aa += nz;
3710:     MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);
3711:   }

3713:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
3714:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
3715:   *newmat = M;

3717:   /* save submatrix used in processor for next request */
3718:   if (call ==  MAT_INITIAL_MATRIX) {
3719:     PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);
3720:     MatDestroy(&Mreuse);
3721:   }
3722:   return(0);
3723: }

3727: PetscErrorCode  MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3728: {
3729:   PetscInt       m,cstart, cend,j,nnz,i,d;
3730:   PetscInt       *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
3731:   const PetscInt *JJ;
3732:   PetscScalar    *values;

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

3738:   PetscLayoutSetUp(B->rmap);
3739:   PetscLayoutSetUp(B->cmap);
3740:   m      = B->rmap->n;
3741:   cstart = B->cmap->rstart;
3742:   cend   = B->cmap->rend;
3743:   rstart = B->rmap->rstart;

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

3747: #if defined(PETSC_USE_DEBUGGING)
3748:   for (i=0; i<m; i++) {
3749:     nnz = Ii[i+1]- Ii[i];
3750:     JJ  = J + Ii[i];
3751:     if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
3752:     if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j);
3753:     if (nnz && (JJ[nnz-1] >= B->cmap->N) SETERRRQ3(PETSC_ERR_ARG_WRONGSTATE,"Row %D ends with too large a column index %D (max allowed %D)",i,JJ[nnz-1],B->cmap->N);
3754:   }
3755: #endif

3757:   for (i=0; i<m; i++) {
3758:     nnz     = Ii[i+1]- Ii[i];
3759:     JJ      = J + Ii[i];
3760:     nnz_max = PetscMax(nnz_max,nnz);
3761:     d       = 0;
3762:     for (j=0; j<nnz; j++) {
3763:       if (cstart <= JJ[j] && JJ[j] < cend) d++;
3764:     }
3765:     d_nnz[i] = d;
3766:     o_nnz[i] = nnz - d;
3767:   }
3768:   MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);
3769:   PetscFree2(d_nnz,o_nnz);

3771:   if (v) values = (PetscScalar*)v;
3772:   else {
3773:     PetscCalloc1((nnz_max+1),&values);
3774:   }

3776:   for (i=0; i<m; i++) {
3777:     ii   = i + rstart;
3778:     nnz  = Ii[i+1]- Ii[i];
3779:     MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);
3780:   }
3781:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3782:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

3784:   if (!v) {
3785:     PetscFree(values);
3786:   }
3787:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3788:   return(0);
3789: }

3793: /*@
3794:    MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
3795:    (the default parallel PETSc format).

3797:    Collective on MPI_Comm

3799:    Input Parameters:
3800: +  B - the matrix
3801: .  i - the indices into j for the start of each local row (starts with zero)
3802: .  j - the column indices for each local row (starts with zero)
3803: -  v - optional values in the matrix

3805:    Level: developer

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

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

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

3818:         1 0 0
3819:         2 0 3     P0
3820:        -------
3821:         4 5 6     P1

3823:      Process0 [P0]: rows_owned=[0,1]
3824:         i =  {0,1,3}  [size = nrow+1  = 2+1]
3825:         j =  {0,0,2}  [size = nz = 6]
3826:         v =  {1,2,3}  [size = nz = 6]

3828:      Process1 [P1]: rows_owned=[2]
3829:         i =  {0,3}    [size = nrow+1  = 1+1]
3830:         j =  {0,1,2}  [size = nz = 6]
3831:         v =  {4,5,6}  [size = nz = 6]

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

3835: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ,
3836:           MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays()
3837: @*/
3838: PetscErrorCode  MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3839: {

3843:   PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
3844:   return(0);
3845: }

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

3856:    Collective on MPI_Comm

3858:    Input Parameters:
3859: +  B - the matrix
3860: .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
3861:            (same value is used for all local rows)
3862: .  d_nnz - array containing the number of nonzeros in the various rows of the
3863:            DIAGONAL portion of the local submatrix (possibly different for each row)
3864:            or NULL, if d_nz is used to specify the nonzero structure.
3865:            The size of this array is equal to the number of local rows, i.e 'm'.
3866:            For matrices that will be factored, you must leave room for (and set)
3867:            the diagonal entry even if it is zero.
3868: .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
3869:            submatrix (same value is used for all local rows).
3870: -  o_nnz - array containing the number of nonzeros in the various rows of the
3871:            OFF-DIAGONAL portion of the local submatrix (possibly different for
3872:            each row) or NULL, if o_nz is used to specify the nonzero
3873:            structure. The size of this array is equal to the number
3874:            of local rows, i.e 'm'.

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

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

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

3887:    The DIAGONAL portion of the local submatrix of a processor can be defined
3888:    as the submatrix which is obtained by extraction the part corresponding to
3889:    the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the
3890:    first row that belongs to the processor, r2 is the last row belonging to
3891:    the this processor, and c1-c2 is range of indices of the local part of a
3892:    vector suitable for applying the matrix to.  This is an mxn matrix.  In the
3893:    common case of a square matrix, the row and column ranges are the same and
3894:    the DIAGONAL part is also square. The remaining portion of the local
3895:    submatrix (mxN) constitute the OFF-DIAGONAL portion.

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

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

3904:    Example usage:

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

3911: .vb
3912:             1  2  0  |  0  3  0  |  0  4
3913:     Proc0   0  5  6  |  7  0  0  |  8  0
3914:             9  0 10  | 11  0  0  | 12  0
3915:     -------------------------------------
3916:            13  0 14  | 15 16 17  |  0  0
3917:     Proc1   0 18  0  | 19 20 21  |  0  0
3918:             0  0  0  | 22 23  0  | 24  0
3919:     -------------------------------------
3920:     Proc2  25 26 27  |  0  0 28  | 29  0
3921:            30  0  0  | 31 32 33  |  0 34
3922: .ve

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

3926: .vb
3927:       A B C
3928:       D E F
3929:       G H I
3930: .ve

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

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

3939:    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
3940:    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
3941:    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
3942:    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
3943:    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
3944:    matrix, ans [DF] as another SeqAIJ matrix.

3946:    When d_nz, o_nz parameters are specified, d_nz storage elements are
3947:    allocated for every row of the local diagonal submatrix, and o_nz
3948:    storage locations are allocated for every row of the OFF-DIAGONAL submat.
3949:    One way to choose d_nz and o_nz is to use the max nonzerors per local
3950:    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
3951:    In this case, the values of d_nz,o_nz are:
3952: .vb
3953:      proc0 : dnz = 2, o_nz = 2
3954:      proc1 : dnz = 3, o_nz = 2
3955:      proc2 : dnz = 1, o_nz = 4
3956: .ve
3957:    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
3958:    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
3959:    for proc3. i.e we are using 12+15+10=37 storage locations to store
3960:    34 values.

3962:    When d_nnz, o_nnz parameters are specified, the storage is specified
3963:    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
3964:    In the above case the values for d_nnz,o_nnz are:
3965: .vb
3966:      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
3967:      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
3968:      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
3969: .ve
3970:    Here the space allocated is sum of all the above values i.e 34, and
3971:    hence pre-allocation is perfect.

3973:    Level: intermediate

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

3977: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(),
3978:           MPIAIJ, MatGetInfo(), PetscSplitOwnership()
3979: @*/
3980: PetscErrorCode  MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3981: {

3987:   PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));
3988:   return(0);
3989: }

3993: /*@
3994:      MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard
3995:          CSR format the local rows.

3997:    Collective on MPI_Comm

3999:    Input Parameters:
4000: +  comm - MPI communicator
4001: .  m - number of local rows (Cannot be PETSC_DECIDE)
4002: .  n - This value should be the same as the local size used in creating the
4003:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4004:        calculated if N is given) For square matrices n is almost always m.
4005: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4006: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4007: .   i - row indices
4008: .   j - column indices
4009: -   a - matrix values

4011:    Output Parameter:
4012: .   mat - the matrix

4014:    Level: intermediate

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

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

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

4027:         1 0 0
4028:         2 0 3     P0
4029:        -------
4030:         4 5 6     P1

4032:      Process0 [P0]: rows_owned=[0,1]
4033:         i =  {0,1,3}  [size = nrow+1  = 2+1]
4034:         j =  {0,0,2}  [size = nz = 6]
4035:         v =  {1,2,3}  [size = nz = 6]

4037:      Process1 [P1]: rows_owned=[2]
4038:         i =  {0,3}    [size = nrow+1  = 1+1]
4039:         j =  {0,1,2}  [size = nz = 6]
4040:         v =  {4,5,6}  [size = nz = 6]

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

4044: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4045:           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
4046: @*/
4047: PetscErrorCode  MatCreateMPIAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
4048: {

4052:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4053:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
4054:   MatCreate(comm,mat);
4055:   MatSetSizes(*mat,m,n,M,N);
4056:   /* MatSetBlockSizes(M,bs,cbs); */
4057:   MatSetType(*mat,MATMPIAIJ);
4058:   MatMPIAIJSetPreallocationCSR(*mat,i,j,a);
4059:   return(0);
4060: }

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

4071:    Collective on MPI_Comm

4073:    Input Parameters:
4074: +  comm - MPI communicator
4075: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
4076:            This value should be the same as the local size used in creating the
4077:            y vector for the matrix-vector product y = Ax.
4078: .  n - This value should be the same as the local size used in creating the
4079:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4080:        calculated if N is given) For square matrices n is almost always m.
4081: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4082: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4083: .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
4084:            (same value is used for all local rows)
4085: .  d_nnz - array containing the number of nonzeros in the various rows of the
4086:            DIAGONAL portion of the local submatrix (possibly different for each row)
4087:            or NULL, if d_nz is used to specify the nonzero structure.
4088:            The size of this array is equal to the number of local rows, i.e 'm'.
4089: .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
4090:            submatrix (same value is used for all local rows).
4091: -  o_nnz - array containing the number of nonzeros in the various rows of the
4092:            OFF-DIAGONAL portion of the local submatrix (possibly different for
4093:            each row) or NULL, if o_nz is used to specify the nonzero
4094:            structure. The size of this array is equal to the number
4095:            of local rows, i.e 'm'.

4097:    Output Parameter:
4098: .  A - the matrix

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

4104:    Notes:
4105:    If the *_nnz parameter is given then the *_nz parameter is ignored

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

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

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

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

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

4128:    The DIAGONAL portion of the local submatrix on any given processor
4129:    is the submatrix corresponding to the rows and columns m,n
4130:    corresponding to the given processor. i.e diagonal matrix on
4131:    process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1]
4132:    etc. The remaining portion of the local submatrix [m x (N-n)]
4133:    constitute the OFF-DIAGONAL portion. The example below better
4134:    illustrates this concept.

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

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

4143:    When calling this routine with a single process communicator, a matrix of
4144:    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
4145:    type of communicator, use the construction mechanism:
4146:      MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...);

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

4152:    Options Database Keys:
4153: +  -mat_no_inode  - Do not use inodes
4154: .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
4155: -  -mat_aij_oneindex - Internally use indexing starting at 1
4156:         rather than 0.  Note that when calling MatSetValues(),
4157:         the user still MUST index entries starting at 0!


4160:    Example usage:

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

4167: .vb
4168:             1  2  0  |  0  3  0  |  0  4
4169:     Proc0   0  5  6  |  7  0  0  |  8  0
4170:             9  0 10  | 11  0  0  | 12  0
4171:     -------------------------------------
4172:            13  0 14  | 15 16 17  |  0  0
4173:     Proc1   0 18  0  | 19 20 21  |  0  0
4174:             0  0  0  | 22 23  0  | 24  0
4175:     -------------------------------------
4176:     Proc2  25 26 27  |  0  0 28  | 29  0
4177:            30  0  0  | 31 32 33  |  0 34
4178: .ve

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

4182: .vb
4183:       A B C
4184:       D E F
4185:       G H I
4186: .ve

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

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

4195:    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
4196:    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
4197:    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
4198:    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
4199:    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
4200:    matrix, ans [DF] as another SeqAIJ matrix.

4202:    When d_nz, o_nz parameters are specified, d_nz storage elements are
4203:    allocated for every row of the local diagonal submatrix, and o_nz
4204:    storage locations are allocated for every row of the OFF-DIAGONAL submat.
4205:    One way to choose d_nz and o_nz is to use the max nonzerors per local
4206:    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
4207:    In this case, the values of d_nz,o_nz are:
4208: .vb
4209:      proc0 : dnz = 2, o_nz = 2
4210:      proc1 : dnz = 3, o_nz = 2
4211:      proc2 : dnz = 1, o_nz = 4
4212: .ve
4213:    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
4214:    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
4215:    for proc3. i.e we are using 12+15+10=37 storage locations to store
4216:    34 values.

4218:    When d_nnz, o_nnz parameters are specified, the storage is specified
4219:    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
4220:    In the above case the values for d_nnz,o_nnz are:
4221: .vb
4222:      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
4223:      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
4224:      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
4225: .ve
4226:    Here the space allocated is sum of all the above values i.e 34, and
4227:    hence pre-allocation is perfect.

4229:    Level: intermediate

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

4233: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4234:           MPIAIJ, MatCreateMPIAIJWithArrays()
4235: @*/
4236: 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)
4237: {
4239:   PetscMPIInt    size;

4242:   MatCreate(comm,A);
4243:   MatSetSizes(*A,m,n,M,N);
4244:   MPI_Comm_size(comm,&size);
4245:   if (size > 1) {
4246:     MatSetType(*A,MATMPIAIJ);
4247:     MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);
4248:   } else {
4249:     MatSetType(*A,MATSEQAIJ);
4250:     MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);
4251:   }
4252:   return(0);
4253: }

4257: PetscErrorCode  MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[])
4258: {
4259:   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;

4262:   if (Ad)     *Ad     = a->A;
4263:   if (Ao)     *Ao     = a->B;
4264:   if (colmap) *colmap = a->garray;
4265:   return(0);
4266: }

4270: PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
4271: {
4273:   PetscInt       i;
4274:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

4277:   if (coloring->ctype == IS_COLORING_GLOBAL) {
4278:     ISColoringValue *allcolors,*colors;
4279:     ISColoring      ocoloring;

4281:     /* set coloring for diagonal portion */
4282:     MatSetColoring_SeqAIJ(a->A,coloring);

4284:     /* set coloring for off-diagonal portion */
4285:     ISAllGatherColors(PetscObjectComm((PetscObject)A),coloring->n,coloring->colors,NULL,&allcolors);
4286:     PetscMalloc1((a->B->cmap->n+1),&colors);
4287:     for (i=0; i<a->B->cmap->n; i++) {
4288:       colors[i] = allcolors[a->garray[i]];
4289:     }
4290:     PetscFree(allcolors);
4291:     ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);
4292:     MatSetColoring_SeqAIJ(a->B,ocoloring);
4293:     ISColoringDestroy(&ocoloring);
4294:   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
4295:     ISColoringValue *colors;
4296:     PetscInt        *larray;
4297:     ISColoring      ocoloring;

4299:     /* set coloring for diagonal portion */
4300:     PetscMalloc1((a->A->cmap->n+1),&larray);
4301:     for (i=0; i<a->A->cmap->n; i++) {
4302:       larray[i] = i + A->cmap->rstart;
4303:     }
4304:     ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,NULL,larray);
4305:     PetscMalloc1((a->A->cmap->n+1),&colors);
4306:     for (i=0; i<a->A->cmap->n; i++) {
4307:       colors[i] = coloring->colors[larray[i]];
4308:     }
4309:     PetscFree(larray);
4310:     ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);
4311:     MatSetColoring_SeqAIJ(a->A,ocoloring);
4312:     ISColoringDestroy(&ocoloring);

4314:     /* set coloring for off-diagonal portion */
4315:     PetscMalloc1((a->B->cmap->n+1),&larray);
4316:     ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,NULL,larray);
4317:     PetscMalloc1((a->B->cmap->n+1),&colors);
4318:     for (i=0; i<a->B->cmap->n; i++) {
4319:       colors[i] = coloring->colors[larray[i]];
4320:     }
4321:     PetscFree(larray);
4322:     ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);
4323:     MatSetColoring_SeqAIJ(a->B,ocoloring);
4324:     ISColoringDestroy(&ocoloring);
4325:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype);
4326:   return(0);
4327: }

4331: PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues)
4332: {
4333:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

4337:   MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);
4338:   MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);
4339:   return(0);
4340: }

4344: PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJSymbolic(MPI_Comm comm,Mat inmat,PetscInt n,Mat *outmat)
4345: {
4347:   PetscInt       m,N,i,rstart,nnz,*dnz,*onz,sum,bs,cbs;
4348:   PetscInt       *indx;

4351:   /* This routine will ONLY return MPIAIJ type matrix */
4352:   MatGetSize(inmat,&m,&N);
4353:   MatGetBlockSizes(inmat,&bs,&cbs);
4354:   if (n == PETSC_DECIDE) {
4355:     PetscSplitOwnership(comm,&n,&N);
4356:   }
4357:   /* Check sum(n) = N */
4358:   MPI_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);
4359:   if (sum != N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",N);

4361:   MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);
4362:   rstart -= m;

4364:   MatPreallocateInitialize(comm,m,n,dnz,onz);
4365:   for (i=0; i<m; i++) {
4366:     MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);
4367:     MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);
4368:     MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);
4369:   }

4371:   MatCreate(comm,outmat);
4372:   MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
4373:   MatSetBlockSizes(*outmat,bs,cbs);
4374:   MatSetType(*outmat,MATMPIAIJ);
4375:   MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);
4376:   MatPreallocateFinalize(dnz,onz);
4377:   return(0);
4378: }

4382: PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJNumeric(MPI_Comm comm,Mat inmat,PetscInt n,Mat outmat)
4383: {
4385:   PetscInt       m,N,i,rstart,nnz,Ii;
4386:   PetscInt       *indx;
4387:   PetscScalar    *values;

4390:   MatGetSize(inmat,&m,&N);
4391:   MatGetOwnershipRange(outmat,&rstart,NULL);
4392:   for (i=0; i<m; i++) {
4393:     MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
4394:     Ii   = i + rstart;
4395:     MatSetValues(outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);
4396:     MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
4397:   }
4398:   MatAssemblyBegin(outmat,MAT_FINAL_ASSEMBLY);
4399:   MatAssemblyEnd(outmat,MAT_FINAL_ASSEMBLY);
4400:   return(0);
4401: }

4405: /*@
4406:       MatCreateMPIAIJConcatenateSeqAIJ - Creates a single large PETSc matrix by concatenating sequential
4407:                  matrices from each processor

4409:     Collective on MPI_Comm

4411:    Input Parameters:
4412: +    comm - the communicators the parallel matrix will live on
4413: .    inmat - the input sequential matrices
4414: .    n - number of local columns (or PETSC_DECIDE)
4415: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

4417:    Output Parameter:
4418: .    outmat - the parallel matrix generated

4420:     Level: advanced

4422:    Notes: The number of columns of the matrix in EACH processor MUST be the same.

4424: @*/
4425: PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4426: {
4428:   PetscMPIInt    size;

4431:   MPI_Comm_size(comm,&size);
4432:   PetscLogEventBegin(MAT_Merge,inmat,0,0,0);
4433:   if (size == 1) {
4434:     if (scall == MAT_INITIAL_MATRIX) {
4435:       MatDuplicate(inmat,MAT_COPY_VALUES,outmat);
4436:     } else {
4437:       MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);
4438:     }
4439:   } else {
4440:     if (scall == MAT_INITIAL_MATRIX) {
4441:       MatCreateMPIAIJConcatenateSeqAIJSymbolic(comm,inmat,n,outmat);
4442:     }
4443:     MatCreateMPIAIJConcatenateSeqAIJNumeric(comm,inmat,n,*outmat);
4444:   }
4445:   PetscLogEventEnd(MAT_Merge,inmat,0,0,0);
4446:   return(0);
4447: }

4451: PetscErrorCode MatFileSplit(Mat A,char *outfile)
4452: {
4453:   PetscErrorCode    ierr;
4454:   PetscMPIInt       rank;
4455:   PetscInt          m,N,i,rstart,nnz;
4456:   size_t            len;
4457:   const PetscInt    *indx;
4458:   PetscViewer       out;
4459:   char              *name;
4460:   Mat               B;
4461:   const PetscScalar *values;

4464:   MatGetLocalSize(A,&m,0);
4465:   MatGetSize(A,0,&N);
4466:   /* Should this be the type of the diagonal block of A? */
4467:   MatCreate(PETSC_COMM_SELF,&B);
4468:   MatSetSizes(B,m,N,m,N);
4469:   MatSetBlockSizesFromMats(B,A,A);
4470:   MatSetType(B,MATSEQAIJ);
4471:   MatSeqAIJSetPreallocation(B,0,NULL);
4472:   MatGetOwnershipRange(A,&rstart,0);
4473:   for (i=0; i<m; i++) {
4474:     MatGetRow(A,i+rstart,&nnz,&indx,&values);
4475:     MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);
4476:     MatRestoreRow(A,i+rstart,&nnz,&indx,&values);
4477:   }
4478:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
4479:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

4481:   MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);
4482:   PetscStrlen(outfile,&len);
4483:   PetscMalloc1((len+5),&name);
4484:   sprintf(name,"%s.%d",outfile,rank);
4485:   PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);
4486:   PetscFree(name);
4487:   MatView(B,out);
4488:   PetscViewerDestroy(&out);
4489:   MatDestroy(&B);
4490:   return(0);
4491: }

4493: extern PetscErrorCode MatDestroy_MPIAIJ(Mat);
4496: PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
4497: {
4498:   PetscErrorCode      ierr;
4499:   Mat_Merge_SeqsToMPI *merge;
4500:   PetscContainer      container;

4503:   PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject*)&container);
4504:   if (container) {
4505:     PetscContainerGetPointer(container,(void**)&merge);
4506:     PetscFree(merge->id_r);
4507:     PetscFree(merge->len_s);
4508:     PetscFree(merge->len_r);
4509:     PetscFree(merge->bi);
4510:     PetscFree(merge->bj);
4511:     PetscFree(merge->buf_ri[0]);
4512:     PetscFree(merge->buf_ri);
4513:     PetscFree(merge->buf_rj[0]);
4514:     PetscFree(merge->buf_rj);
4515:     PetscFree(merge->coi);
4516:     PetscFree(merge->coj);
4517:     PetscFree(merge->owners_co);
4518:     PetscLayoutDestroy(&merge->rowmap);
4519:     PetscFree(merge);
4520:     PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);
4521:   }
4522:   MatDestroy_MPIAIJ(A);
4523:   return(0);
4524: }

4526: #include <../src/mat/utils/freespace.h>
4527: #include <petscbt.h>

4531: PetscErrorCode  MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat)
4532: {
4533:   PetscErrorCode      ierr;
4534:   MPI_Comm            comm;
4535:   Mat_SeqAIJ          *a  =(Mat_SeqAIJ*)seqmat->data;
4536:   PetscMPIInt         size,rank,taga,*len_s;
4537:   PetscInt            N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj;
4538:   PetscInt            proc,m;
4539:   PetscInt            **buf_ri,**buf_rj;
4540:   PetscInt            k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
4541:   PetscInt            nrows,**buf_ri_k,**nextrow,**nextai;
4542:   MPI_Request         *s_waits,*r_waits;
4543:   MPI_Status          *status;
4544:   MatScalar           *aa=a->a;
4545:   MatScalar           **abuf_r,*ba_i;
4546:   Mat_Merge_SeqsToMPI *merge;
4547:   PetscContainer      container;

4550:   PetscObjectGetComm((PetscObject)mpimat,&comm);
4551:   PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);

4553:   MPI_Comm_size(comm,&size);
4554:   MPI_Comm_rank(comm,&rank);

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

4559:   bi     = merge->bi;
4560:   bj     = merge->bj;
4561:   buf_ri = merge->buf_ri;
4562:   buf_rj = merge->buf_rj;

4564:   PetscMalloc1(size,&status);
4565:   owners = merge->rowmap->range;
4566:   len_s  = merge->len_s;

4568:   /* send and recv matrix values */
4569:   /*-----------------------------*/
4570:   PetscObjectGetNewTag((PetscObject)mpimat,&taga);
4571:   PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);

4573:   PetscMalloc1((merge->nsend+1),&s_waits);
4574:   for (proc=0,k=0; proc<size; proc++) {
4575:     if (!len_s[proc]) continue;
4576:     i    = owners[proc];
4577:     MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);
4578:     k++;
4579:   }

4581:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,r_waits,status);}
4582:   if (merge->nsend) {MPI_Waitall(merge->nsend,s_waits,status);}
4583:   PetscFree(status);

4585:   PetscFree(s_waits);
4586:   PetscFree(r_waits);

4588:   /* insert mat values of mpimat */
4589:   /*----------------------------*/
4590:   PetscMalloc1(N,&ba_i);
4591:   PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);

4593:   for (k=0; k<merge->nrecv; k++) {
4594:     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
4595:     nrows       = *(buf_ri_k[k]);
4596:     nextrow[k]  = buf_ri_k[k]+1;  /* next row number of k-th recved i-structure */
4597:     nextai[k]   = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure  */
4598:   }

4600:   /* set values of ba */
4601:   m = merge->rowmap->n;
4602:   for (i=0; i<m; i++) {
4603:     arow = owners[rank] + i;
4604:     bj_i = bj+bi[i];  /* col indices of the i-th row of mpimat */
4605:     bnzi = bi[i+1] - bi[i];
4606:     PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));

4608:     /* add local non-zero vals of this proc's seqmat into ba */
4609:     anzi   = ai[arow+1] - ai[arow];
4610:     aj     = a->j + ai[arow];
4611:     aa     = a->a + ai[arow];
4612:     nextaj = 0;
4613:     for (j=0; nextaj<anzi; j++) {
4614:       if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
4615:         ba_i[j] += aa[nextaj++];
4616:       }
4617:     }

4619:     /* add received vals into ba */
4620:     for (k=0; k<merge->nrecv; k++) { /* k-th received message */
4621:       /* i-th row */
4622:       if (i == *nextrow[k]) {
4623:         anzi   = *(nextai[k]+1) - *nextai[k];
4624:         aj     = buf_rj[k] + *(nextai[k]);
4625:         aa     = abuf_r[k] + *(nextai[k]);
4626:         nextaj = 0;
4627:         for (j=0; nextaj<anzi; j++) {
4628:           if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
4629:             ba_i[j] += aa[nextaj++];
4630:           }
4631:         }
4632:         nextrow[k]++; nextai[k]++;
4633:       }
4634:     }
4635:     MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);
4636:   }
4637:   MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);
4638:   MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);

4640:   PetscFree(abuf_r[0]);
4641:   PetscFree(abuf_r);
4642:   PetscFree(ba_i);
4643:   PetscFree3(buf_ri_k,nextrow,nextai);
4644:   PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);
4645:   return(0);
4646: }

4648: extern PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat);

4652: PetscErrorCode  MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
4653: {
4654:   PetscErrorCode      ierr;
4655:   Mat                 B_mpi;
4656:   Mat_SeqAIJ          *a=(Mat_SeqAIJ*)seqmat->data;
4657:   PetscMPIInt         size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
4658:   PetscInt            **buf_rj,**buf_ri,**buf_ri_k;
4659:   PetscInt            M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j;
4660:   PetscInt            len,proc,*dnz,*onz,bs,cbs;
4661:   PetscInt            k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
4662:   PetscInt            nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
4663:   MPI_Request         *si_waits,*sj_waits,*ri_waits,*rj_waits;
4664:   MPI_Status          *status;
4665:   PetscFreeSpaceList  free_space=NULL,current_space=NULL;
4666:   PetscBT             lnkbt;
4667:   Mat_Merge_SeqsToMPI *merge;
4668:   PetscContainer      container;

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

4673:   /* make sure it is a PETSc comm */
4674:   PetscCommDuplicate(comm,&comm,NULL);
4675:   MPI_Comm_size(comm,&size);
4676:   MPI_Comm_rank(comm,&rank);

4678:   PetscNew(&merge);
4679:   PetscMalloc1(size,&status);

4681:   /* determine row ownership */
4682:   /*---------------------------------------------------------*/
4683:   PetscLayoutCreate(comm,&merge->rowmap);
4684:   PetscLayoutSetLocalSize(merge->rowmap,m);
4685:   PetscLayoutSetSize(merge->rowmap,M);
4686:   PetscLayoutSetBlockSize(merge->rowmap,1);
4687:   PetscLayoutSetUp(merge->rowmap);
4688:   PetscMalloc1(size,&len_si);
4689:   PetscMalloc1(size,&merge->len_s);

4691:   m      = merge->rowmap->n;
4692:   owners = merge->rowmap->range;

4694:   /* determine the number of messages to send, their lengths */
4695:   /*---------------------------------------------------------*/
4696:   len_s = merge->len_s;

4698:   len          = 0; /* length of buf_si[] */
4699:   merge->nsend = 0;
4700:   for (proc=0; proc<size; proc++) {
4701:     len_si[proc] = 0;
4702:     if (proc == rank) {
4703:       len_s[proc] = 0;
4704:     } else {
4705:       len_si[proc] = owners[proc+1] - owners[proc] + 1;
4706:       len_s[proc]  = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
4707:     }
4708:     if (len_s[proc]) {
4709:       merge->nsend++;
4710:       nrows = 0;
4711:       for (i=owners[proc]; i<owners[proc+1]; i++) {
4712:         if (ai[i+1] > ai[i]) nrows++;
4713:       }
4714:       len_si[proc] = 2*(nrows+1);
4715:       len         += len_si[proc];
4716:     }
4717:   }

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

4724:   /* post the Irecv of j-structure */
4725:   /*-------------------------------*/
4726:   PetscCommGetNewTag(comm,&tagj);
4727:   PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);

4729:   /* post the Isend of j-structure */
4730:   /*--------------------------------*/
4731:   PetscMalloc2(merge->nsend,&si_waits,merge->nsend,&sj_waits);

4733:   for (proc=0, k=0; proc<size; proc++) {
4734:     if (!len_s[proc]) continue;
4735:     i    = owners[proc];
4736:     MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);
4737:     k++;
4738:   }

4740:   /* receives and sends of j-structure are complete */
4741:   /*------------------------------------------------*/
4742:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,rj_waits,status);}
4743:   if (merge->nsend) {MPI_Waitall(merge->nsend,sj_waits,status);}

4745:   /* send and recv i-structure */
4746:   /*---------------------------*/
4747:   PetscCommGetNewTag(comm,&tagi);
4748:   PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);

4750:   PetscMalloc1((len+1),&buf_s);
4751:   buf_si = buf_s;  /* points to the beginning of k-th msg to be sent */
4752:   for (proc=0,k=0; proc<size; proc++) {
4753:     if (!len_s[proc]) continue;
4754:     /* form outgoing message for i-structure:
4755:          buf_si[0]:                 nrows to be sent
4756:                [1:nrows]:           row index (global)
4757:                [nrows+1:2*nrows+1]: i-structure index
4758:     */
4759:     /*-------------------------------------------*/
4760:     nrows       = len_si[proc]/2 - 1;
4761:     buf_si_i    = buf_si + nrows+1;
4762:     buf_si[0]   = nrows;
4763:     buf_si_i[0] = 0;
4764:     nrows       = 0;
4765:     for (i=owners[proc]; i<owners[proc+1]; i++) {
4766:       anzi = ai[i+1] - ai[i];
4767:       if (anzi) {
4768:         buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
4769:         buf_si[nrows+1]   = i-owners[proc]; /* local row index */
4770:         nrows++;
4771:       }
4772:     }
4773:     MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);
4774:     k++;
4775:     buf_si += len_si[proc];
4776:   }

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

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

4786:   PetscFree(len_si);
4787:   PetscFree(len_ri);
4788:   PetscFree(rj_waits);
4789:   PetscFree2(si_waits,sj_waits);
4790:   PetscFree(ri_waits);
4791:   PetscFree(buf_s);
4792:   PetscFree(status);

4794:   /* compute a local seq matrix in each processor */
4795:   /*----------------------------------------------*/
4796:   /* allocate bi array and free space for accumulating nonzero column info */
4797:   PetscMalloc1((m+1),&bi);
4798:   bi[0] = 0;

4800:   /* create and initialize a linked list */
4801:   nlnk = N+1;
4802:   PetscLLCreate(N,N,nlnk,lnk,lnkbt);

4804:   /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
4805:   len  = ai[owners[rank+1]] - ai[owners[rank]];
4806:   PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);

4808:   current_space = free_space;

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

4813:   for (k=0; k<merge->nrecv; k++) {
4814:     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
4815:     nrows       = *buf_ri_k[k];
4816:     nextrow[k]  = buf_ri_k[k] + 1;  /* next row number of k-th recved i-structure */
4817:     nextai[k]   = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure  */
4818:   }

4820:   MatPreallocateInitialize(comm,m,n,dnz,onz);
4821:   len  = 0;
4822:   for (i=0; i<m; i++) {
4823:     bnzi = 0;
4824:     /* add local non-zero cols of this proc's seqmat into lnk */
4825:     arow  = owners[rank] + i;
4826:     anzi  = ai[arow+1] - ai[arow];
4827:     aj    = a->j + ai[arow];
4828:     PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);
4829:     bnzi += nlnk;
4830:     /* add received col data into lnk */
4831:     for (k=0; k<merge->nrecv; k++) { /* k-th received message */
4832:       if (i == *nextrow[k]) { /* i-th row */
4833:         anzi  = *(nextai[k]+1) - *nextai[k];
4834:         aj    = buf_rj[k] + *nextai[k];
4835:         PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);
4836:         bnzi += nlnk;
4837:         nextrow[k]++; nextai[k]++;
4838:       }
4839:     }
4840:     if (len < bnzi) len = bnzi;  /* =max(bnzi) */

4842:     /* if free space is not available, make more free space */
4843:     if (current_space->local_remaining<bnzi) {
4844:       PetscFreeSpaceGet(bnzi+current_space->total_array_size,&current_space);
4845:       nspacedouble++;
4846:     }
4847:     /* copy data into free space, then initialize lnk */
4848:     PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);
4849:     MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);

4851:     current_space->array           += bnzi;
4852:     current_space->local_used      += bnzi;
4853:     current_space->local_remaining -= bnzi;

4855:     bi[i+1] = bi[i] + bnzi;
4856:   }

4858:   PetscFree3(buf_ri_k,nextrow,nextai);

4860:   PetscMalloc1((bi[m]+1),&bj);
4861:   PetscFreeSpaceContiguous(&free_space,bj);
4862:   PetscLLDestroy(lnk,lnkbt);

4864:   /* create symbolic parallel matrix B_mpi */
4865:   /*---------------------------------------*/
4866:   MatGetBlockSizes(seqmat,&bs,&cbs);
4867:   MatCreate(comm,&B_mpi);
4868:   if (n==PETSC_DECIDE) {
4869:     MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);
4870:   } else {
4871:     MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
4872:   }
4873:   MatSetBlockSizes(B_mpi,bs,cbs);
4874:   MatSetType(B_mpi,MATMPIAIJ);
4875:   MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);
4876:   MatPreallocateFinalize(dnz,onz);
4877:   MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);

4879:   /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */
4880:   B_mpi->assembled    = PETSC_FALSE;
4881:   B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI;
4882:   merge->bi           = bi;
4883:   merge->bj           = bj;
4884:   merge->buf_ri       = buf_ri;
4885:   merge->buf_rj       = buf_rj;
4886:   merge->coi          = NULL;
4887:   merge->coj          = NULL;
4888:   merge->owners_co    = NULL;

4890:   PetscCommDestroy(&comm);

4892:   /* attach the supporting struct to B_mpi for reuse */
4893:   PetscContainerCreate(PETSC_COMM_SELF,&container);
4894:   PetscContainerSetPointer(container,merge);
4895:   PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);
4896:   PetscContainerDestroy(&container);
4897:   *mpimat = B_mpi;

4899:   PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);
4900:   return(0);
4901: }

4905: /*@C
4906:       MatCreateMPIAIJSumSeqAIJ - Creates a MPIAIJ matrix by adding sequential
4907:                  matrices from each processor

4909:     Collective on MPI_Comm

4911:    Input Parameters:
4912: +    comm - the communicators the parallel matrix will live on
4913: .    seqmat - the input sequential matrices
4914: .    m - number of local rows (or PETSC_DECIDE)
4915: .    n - number of local columns (or PETSC_DECIDE)
4916: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

4918:    Output Parameter:
4919: .    mpimat - the parallel matrix generated

4921:     Level: advanced

4923:    Notes:
4924:      The dimensions of the sequential matrix in each processor MUST be the same.
4925:      The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
4926:      destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
4927: @*/
4928: PetscErrorCode  MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
4929: {
4931:   PetscMPIInt    size;

4934:   MPI_Comm_size(comm,&size);
4935:   if (size == 1) {
4936:     PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);
4937:     if (scall == MAT_INITIAL_MATRIX) {
4938:       MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);
4939:     } else {
4940:       MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);
4941:     }
4942:     PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);
4943:     return(0);
4944:   }
4945:   PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);
4946:   if (scall == MAT_INITIAL_MATRIX) {
4947:     MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);
4948:   }
4949:   MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);
4950:   PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);
4951:   return(0);
4952: }

4956: /*@
4957:      MatMPIAIJGetLocalMat - Creates a SeqAIJ from a MPIAIJ matrix by taking all its local rows and putting them into a sequential vector with
4958:           mlocal rows and n columns. Where mlocal is the row count obtained with MatGetLocalSize() and n is the global column count obtained
4959:           with MatGetSize()

4961:     Not Collective

4963:    Input Parameters:
4964: +    A - the matrix
4965: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

4967:    Output Parameter:
4968: .    A_loc - the local sequential matrix generated

4970:     Level: developer

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

4974: @*/
4975: PetscErrorCode  MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
4976: {
4978:   Mat_MPIAIJ     *mpimat=(Mat_MPIAIJ*)A->data;
4979:   Mat_SeqAIJ     *mat,*a,*b;
4980:   PetscInt       *ai,*aj,*bi,*bj,*cmap=mpimat->garray;
4981:   MatScalar      *aa,*ba,*cam;
4982:   PetscScalar    *ca;
4983:   PetscInt       am=A->rmap->n,i,j,k,cstart=A->cmap->rstart;
4984:   PetscInt       *ci,*cj,col,ncols_d,ncols_o,jo;
4985:   PetscBool      match;

4988:   PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);
4989:   if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MPIAIJ matrix as input");
4990:   PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);
4991:   a = (Mat_SeqAIJ*)(mpimat->A)->data;
4992:   b = (Mat_SeqAIJ*)(mpimat->B)->data;
4993:   ai = a->i; aj = a->j; bi = b->i; bj = b->j;
4994:   aa = a->a; ba = b->a;
4995:   if (scall == MAT_INITIAL_MATRIX) {
4996:     PetscMalloc1((1+am),&ci);
4997:     ci[0] = 0;
4998:     for (i=0; i<am; i++) {
4999:       ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
5000:     }
5001:     PetscMalloc1((1+ci[am]),&cj);
5002:     PetscMalloc1((1+ci[am]),&ca);
5003:     k    = 0;
5004:     for (i=0; i<am; i++) {
5005:       ncols_o = bi[i+1] - bi[i];
5006:       ncols_d = ai[i+1] - ai[i];
5007:       /* off-diagonal portion of A */
5008:       for (jo=0; jo<ncols_o; jo++) {
5009:         col = cmap[*bj];
5010:         if (col >= cstart) break;
5011:         cj[k]   = col; bj++;
5012:         ca[k++] = *ba++;
5013:       }
5014:       /* diagonal portion of A */
5015:       for (j=0; j<ncols_d; j++) {
5016:         cj[k]   = cstart + *aj++;
5017:         ca[k++] = *aa++;
5018:       }
5019:       /* off-diagonal portion of A */
5020:       for (j=jo; j<ncols_o; j++) {
5021:         cj[k]   = cmap[*bj++];
5022:         ca[k++] = *ba++;
5023:       }
5024:     }
5025:     /* put together the new matrix */
5026:     MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);
5027:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5028:     /* Since these are PETSc arrays, change flags to free them as necessary. */
5029:     mat          = (Mat_SeqAIJ*)(*A_loc)->data;
5030:     mat->free_a  = PETSC_TRUE;
5031:     mat->free_ij = PETSC_TRUE;
5032:     mat->nonew   = 0;
5033:   } else if (scall == MAT_REUSE_MATRIX) {
5034:     mat=(Mat_SeqAIJ*)(*A_loc)->data;
5035:     ci = mat->i; cj = mat->j; cam = mat->a;
5036:     for (i=0; i<am; i++) {
5037:       /* off-diagonal portion of A */
5038:       ncols_o = bi[i+1] - bi[i];
5039:       for (jo=0; jo<ncols_o; jo++) {
5040:         col = cmap[*bj];
5041:         if (col >= cstart) break;
5042:         *cam++ = *ba++; bj++;
5043:       }
5044:       /* diagonal portion of A */
5045:       ncols_d = ai[i+1] - ai[i];
5046:       for (j=0; j<ncols_d; j++) *cam++ = *aa++;
5047:       /* off-diagonal portion of A */
5048:       for (j=jo; j<ncols_o; j++) {
5049:         *cam++ = *ba++; bj++;
5050:       }
5051:     }
5052:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
5053:   PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);
5054:   return(0);
5055: }

5059: /*@C
5060:      MatMPIAIJGetLocalMatCondensed - Creates a SeqAIJ matrix from an MPIAIJ matrix by taking all its local rows and NON-ZERO columns

5062:     Not Collective

5064:    Input Parameters:
5065: +    A - the matrix
5066: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5067: -    row, col - index sets of rows and columns to extract (or NULL)

5069:    Output Parameter:
5070: .    A_loc - the local sequential matrix generated

5072:     Level: developer

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

5076: @*/
5077: PetscErrorCode  MatMPIAIJGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
5078: {
5079:   Mat_MPIAIJ     *a=(Mat_MPIAIJ*)A->data;
5081:   PetscInt       i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
5082:   IS             isrowa,iscola;
5083:   Mat            *aloc;
5084:   PetscBool      match;

5087:   PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);
5088:   if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MPIAIJ matrix as input");
5089:   PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);
5090:   if (!row) {
5091:     start = A->rmap->rstart; end = A->rmap->rend;
5092:     ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);
5093:   } else {
5094:     isrowa = *row;
5095:   }
5096:   if (!col) {
5097:     start = A->cmap->rstart;
5098:     cmap  = a->garray;
5099:     nzA   = a->A->cmap->n;
5100:     nzB   = a->B->cmap->n;
5101:     PetscMalloc1((nzA+nzB), &idx);
5102:     ncols = 0;
5103:     for (i=0; i<nzB; i++) {
5104:       if (cmap[i] < start) idx[ncols++] = cmap[i];
5105:       else break;
5106:     }
5107:     imark = i;
5108:     for (i=0; i<nzA; i++) idx[ncols++] = start + i;
5109:     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
5110:     ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);
5111:   } else {
5112:     iscola = *col;
5113:   }
5114:   if (scall != MAT_INITIAL_MATRIX) {
5115:     PetscMalloc(sizeof(Mat),&aloc);
5116:     aloc[0] = *A_loc;
5117:   }
5118:   MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);
5119:   *A_loc = aloc[0];
5120:   PetscFree(aloc);
5121:   if (!row) {
5122:     ISDestroy(&isrowa);
5123:   }
5124:   if (!col) {
5125:     ISDestroy(&iscola);
5126:   }
5127:   PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);
5128:   return(0);
5129: }

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

5136:     Collective on Mat

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

5143:    Output Parameter:
5144: +    rowb, colb - index sets of rows and columns of B to extract
5145: -    B_seq - the sequential matrix generated

5147:     Level: developer

5149: @*/
5150: PetscErrorCode  MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,Mat *B_seq)
5151: {
5152:   Mat_MPIAIJ     *a=(Mat_MPIAIJ*)A->data;
5154:   PetscInt       *idx,i,start,ncols,nzA,nzB,*cmap,imark;
5155:   IS             isrowb,iscolb;
5156:   Mat            *bseq=NULL;

5159:   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5160:     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);
5161:   }
5162:   PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);

5164:   if (scall == MAT_INITIAL_MATRIX) {
5165:     start = A->cmap->rstart;
5166:     cmap  = a->garray;
5167:     nzA   = a->A->cmap->n;
5168:     nzB   = a->B->cmap->n;
5169:     PetscMalloc1((nzA+nzB), &idx);
5170:     ncols = 0;
5171:     for (i=0; i<nzB; i++) {  /* row < local row index */
5172:       if (cmap[i] < start) idx[ncols++] = cmap[i];
5173:       else break;
5174:     }
5175:     imark = i;
5176:     for (i=0; i<nzA; i++) idx[ncols++] = start + i;  /* local rows */
5177:     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
5178:     ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);
5179:     ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);
5180:   } else {
5181:     if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
5182:     isrowb  = *rowb; iscolb = *colb;
5183:     PetscMalloc(sizeof(Mat),&bseq);
5184:     bseq[0] = *B_seq;
5185:   }
5186:   MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);
5187:   *B_seq = bseq[0];
5188:   PetscFree(bseq);
5189:   if (!rowb) {
5190:     ISDestroy(&isrowb);
5191:   } else {
5192:     *rowb = isrowb;
5193:   }
5194:   if (!colb) {
5195:     ISDestroy(&iscolb);
5196:   } else {
5197:     *colb = iscolb;
5198:   }
5199:   PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);
5200:   return(0);
5201: }

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

5209:     Collective on Mat

5211:    Input Parameters:
5212: +    A,B - the matrices in mpiaij format
5213: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

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

5221:     Level: developer

5223: */
5224: PetscErrorCode  MatGetBrowsOfAoCols_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscInt **startsj_s,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth)
5225: {
5226:   VecScatter_MPI_General *gen_to,*gen_from;
5227:   PetscErrorCode         ierr;
5228:   Mat_MPIAIJ             *a=(Mat_MPIAIJ*)A->data;
5229:   Mat_SeqAIJ             *b_oth;
5230:   VecScatter             ctx =a->Mvctx;
5231:   MPI_Comm               comm;
5232:   PetscMPIInt            *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank;
5233:   PetscInt               *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj;
5234:   PetscScalar            *rvalues,*svalues;
5235:   MatScalar              *b_otha,*bufa,*bufA;
5236:   PetscInt               i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len;
5237:   MPI_Request            *rwaits = NULL,*swaits = NULL;
5238:   MPI_Status             *sstatus,rstatus;
5239:   PetscMPIInt            jj;
5240:   PetscInt               *cols,sbs,rbs;
5241:   PetscScalar            *vals;

5244:   PetscObjectGetComm((PetscObject)A,&comm);
5245:   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5246:     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);
5247:   }
5248:   PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);
5249:   MPI_Comm_rank(comm,&rank);

5251:   gen_to   = (VecScatter_MPI_General*)ctx->todata;
5252:   gen_from = (VecScatter_MPI_General*)ctx->fromdata;
5253:   rvalues  = gen_from->values; /* holds the length of receiving row */
5254:   svalues  = gen_to->values;   /* holds the length of sending row */
5255:   nrecvs   = gen_from->n;
5256:   nsends   = gen_to->n;

5258:   PetscMalloc2(nrecvs,&rwaits,nsends,&swaits);
5259:   srow    = gen_to->indices;    /* local row index to be sent */
5260:   sstarts = gen_to->starts;
5261:   sprocs  = gen_to->procs;
5262:   sstatus = gen_to->sstatus;
5263:   sbs     = gen_to->bs;
5264:   rstarts = gen_from->starts;
5265:   rprocs  = gen_from->procs;
5266:   rbs     = gen_from->bs;

5268:   if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
5269:   if (scall == MAT_INITIAL_MATRIX) {
5270:     /* i-array */
5271:     /*---------*/
5272:     /*  post receives */
5273:     for (i=0; i<nrecvs; i++) {
5274:       rowlen = (PetscInt*)rvalues + rstarts[i]*rbs;
5275:       nrows  = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */
5276:       MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
5277:     }

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

5282:     sstartsj[0] = 0;
5283:     rstartsj[0] = 0;
5284:     len         = 0; /* total length of j or a array to be sent */
5285:     k           = 0;
5286:     for (i=0; i<nsends; i++) {
5287:       rowlen = (PetscInt*)svalues + sstarts[i]*sbs;
5288:       nrows  = sstarts[i+1]-sstarts[i]; /* num of block rows */
5289:       for (j=0; j<nrows; j++) {
5290:         row = srow[k] + B->rmap->range[rank]; /* global row idx */
5291:         for (l=0; l<sbs; l++) {
5292:           MatGetRow_MPIAIJ(B,row+l,&ncols,NULL,NULL); /* rowlength */

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

5296:           len += ncols;
5297:           MatRestoreRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);
5298:         }
5299:         k++;
5300:       }
5301:       MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);

5303:       sstartsj[i+1] = len;  /* starting point of (i+1)-th outgoing msg in bufj and bufa */
5304:     }
5305:     /* recvs and sends of i-array are completed */
5306:     i = nrecvs;
5307:     while (i--) {
5308:       MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);
5309:     }
5310:     if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}

5312:     /* allocate buffers for sending j and a arrays */
5313:     PetscMalloc1((len+1),&bufj);
5314:     PetscMalloc1((len+1),&bufa);

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

5319:     b_othi[0] = 0;
5320:     len       = 0; /* total length of j or a array to be received */
5321:     k         = 0;
5322:     for (i=0; i<nrecvs; i++) {
5323:       rowlen = (PetscInt*)rvalues + rstarts[i]*rbs;
5324:       nrows  = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */
5325:       for (j=0; j<nrows; j++) {
5326:         b_othi[k+1] = b_othi[k] + rowlen[j];
5327:         len        += rowlen[j]; k++;
5328:       }
5329:       rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
5330:     }

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

5336:     /* j-array */
5337:     /*---------*/
5338:     /*  post receives of j-array */
5339:     for (i=0; i<nrecvs; i++) {
5340:       nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
5341:       MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
5342:     }

5344:     /* pack the outgoing message j-array */
5345:     k = 0;
5346:     for (i=0; i<nsends; i++) {
5347:       nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5348:       bufJ  = bufj+sstartsj[i];
5349:       for (j=0; j<nrows; j++) {
5350:         row = srow[k++] + B->rmap->range[rank];  /* global row idx */
5351:         for (ll=0; ll<sbs; ll++) {
5352:           MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);
5353:           for (l=0; l<ncols; l++) {
5354:             *bufJ++ = cols[l];
5355:           }
5356:           MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);
5357:         }
5358:       }
5359:       MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);
5360:     }

5362:     /* recvs and sends of j-array are completed */
5363:     i = nrecvs;
5364:     while (i--) {
5365:       MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);
5366:     }
5367:     if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
5368:   } else if (scall == MAT_REUSE_MATRIX) {
5369:     sstartsj = *startsj_s;
5370:     rstartsj = *startsj_r;
5371:     bufa     = *bufa_ptr;
5372:     b_oth    = (Mat_SeqAIJ*)(*B_oth)->data;
5373:     b_otha   = b_oth->a;
5374:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");

5376:   /* a-array */
5377:   /*---------*/
5378:   /*  post receives of a-array */
5379:   for (i=0; i<nrecvs; i++) {
5380:     nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
5381:     MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);
5382:   }

5384:   /* pack the outgoing message a-array */
5385:   k = 0;
5386:   for (i=0; i<nsends; i++) {
5387:     nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5388:     bufA  = bufa+sstartsj[i];
5389:     for (j=0; j<nrows; j++) {
5390:       row = srow[k++] + B->rmap->range[rank];  /* global row idx */
5391:       for (ll=0; ll<sbs; ll++) {
5392:         MatGetRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);
5393:         for (l=0; l<ncols; l++) {
5394:           *bufA++ = vals[l];
5395:         }
5396:         MatRestoreRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);
5397:       }
5398:     }
5399:     MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);
5400:   }
5401:   /* recvs and sends of a-array are completed */
5402:   i = nrecvs;
5403:   while (i--) {
5404:     MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);
5405:   }
5406:   if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
5407:   PetscFree2(rwaits,swaits);

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

5413:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5414:     /* Since these are PETSc arrays, change flags to free them as necessary. */
5415:     b_oth          = (Mat_SeqAIJ*)(*B_oth)->data;
5416:     b_oth->free_a  = PETSC_TRUE;
5417:     b_oth->free_ij = PETSC_TRUE;
5418:     b_oth->nonew   = 0;

5420:     PetscFree(bufj);
5421:     if (!startsj_s || !bufa_ptr) {
5422:       PetscFree2(sstartsj,rstartsj);
5423:       PetscFree(bufa_ptr);
5424:     } else {
5425:       *startsj_s = sstartsj;
5426:       *startsj_r = rstartsj;
5427:       *bufa_ptr  = bufa;
5428:     }
5429:   }
5430:   PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);
5431:   return(0);
5432: }

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

5439:   Not Collective

5441:   Input Parameters:
5442: . A - The matrix in mpiaij format

5444:   Output Parameter:
5445: + lvec - The local vector holding off-process values from the argument to a matrix-vector product
5446: . colmap - A map from global column index to local index into lvec
5447: - multScatter - A scatter from the argument of a matrix-vector product to lvec

5449:   Level: developer

5451: @*/
5452: #if defined(PETSC_USE_CTABLE)
5453: PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
5454: #else
5455: PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
5456: #endif
5457: {
5458:   Mat_MPIAIJ *a;

5465:   a = (Mat_MPIAIJ*) A->data;
5466:   if (lvec) *lvec = a->lvec;
5467:   if (colmap) *colmap = a->colmap;
5468:   if (multScatter) *multScatter = a->Mvctx;
5469:   return(0);
5470: }

5472: PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJCRL(Mat,MatType,MatReuse,Mat*);
5473: PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJPERM(Mat,MatType,MatReuse,Mat*);
5474: PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPISBAIJ(Mat,MatType,MatReuse,Mat*);

5478: /*
5479:     Computes (B'*A')' since computing B*A directly is untenable

5481:                n                       p                          p
5482:         (              )       (              )         (                  )
5483:       m (      A       )  *  n (       B      )   =   m (         C        )
5484:         (              )       (              )         (                  )

5486: */
5487: PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C)
5488: {
5490:   Mat            At,Bt,Ct;

5493:   MatTranspose(A,MAT_INITIAL_MATRIX,&At);
5494:   MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);
5495:   MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);
5496:   MatDestroy(&At);
5497:   MatDestroy(&Bt);
5498:   MatTranspose(Ct,MAT_REUSE_MATRIX,&C);
5499:   MatDestroy(&Ct);
5500:   return(0);
5501: }

5505: PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
5506: {
5508:   PetscInt       m=A->rmap->n,n=B->cmap->n;
5509:   Mat            Cmat;

5512:   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);
5513:   MatCreate(PetscObjectComm((PetscObject)A),&Cmat);
5514:   MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
5515:   MatSetBlockSizesFromMats(Cmat,A,B);
5516:   MatSetType(Cmat,MATMPIDENSE);
5517:   MatMPIDenseSetPreallocation(Cmat,NULL);
5518:   MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);
5519:   MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);

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

5523:   *C = Cmat;
5524:   return(0);
5525: }

5527: /* ----------------------------------------------------------------*/
5530: PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
5531: {

5535:   if (scall == MAT_INITIAL_MATRIX) {
5536:     PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
5537:     MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);
5538:     PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
5539:   }
5540:   PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
5541:   MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);
5542:   PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
5543:   return(0);
5544: }

5546: #if defined(PETSC_HAVE_MUMPS)
5547: PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
5548: #endif
5549: #if defined(PETSC_HAVE_PASTIX)
5550: PETSC_EXTERN PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*);
5551: #endif
5552: #if defined(PETSC_HAVE_SUPERLU_DIST)
5553: PETSC_EXTERN PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*);
5554: #endif
5555: #if defined(PETSC_HAVE_CLIQUE)
5556: PETSC_EXTERN PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*);
5557: #endif

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

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

5565:   Level: beginner

5567: .seealso: MatCreateAIJ()
5568: M*/

5572: PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJ(Mat B)
5573: {
5574:   Mat_MPIAIJ     *b;
5576:   PetscMPIInt    size;

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

5581:   PetscNewLog(B,&b);
5582:   B->data       = (void*)b;
5583:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
5584:   B->assembled  = PETSC_FALSE;
5585:   B->insertmode = NOT_SET_VALUES;
5586:   b->size       = size;

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

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

5593:   b->donotstash  = PETSC_FALSE;
5594:   b->colmap      = 0;
5595:   b->garray      = 0;
5596:   b->roworiented = PETSC_TRUE;

5598:   /* stuff used for matrix vector multiply */
5599:   b->lvec  = NULL;
5600:   b->Mvctx = NULL;

5602:   /* stuff for MatGetRow() */
5603:   b->rowindices   = 0;
5604:   b->rowvalues    = 0;
5605:   b->getrowactive = PETSC_FALSE;

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

5610: #if defined(PETSC_HAVE_MUMPS)
5611:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_aij_mumps);
5612: #endif
5613: #if defined(PETSC_HAVE_PASTIX)
5614:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_pastix_C",MatGetFactor_mpiaij_pastix);
5615: #endif
5616: #if defined(PETSC_HAVE_SUPERLU_DIST)
5617:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_dist_C",MatGetFactor_mpiaij_superlu_dist);
5618: #endif
5619: #if defined(PETSC_HAVE_CLIQUE)
5620:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_clique_C",MatGetFactor_aij_clique);
5621: #endif
5622:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIAIJ);
5623:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIAIJ);
5624:   PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",MatGetDiagonalBlock_MPIAIJ);
5625:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_MPIAIJ);
5626:   PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",MatMPIAIJSetPreallocation_MPIAIJ);
5627:   PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",MatMPIAIJSetPreallocationCSR_MPIAIJ);
5628:   PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIAIJ);
5629:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C",MatConvert_MPIAIJ_MPIAIJPERM);
5630:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C",MatConvert_MPIAIJ_MPIAIJCRL);
5631:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C",MatConvert_MPIAIJ_MPISBAIJ);
5632:   PetscObjectComposeFunction((PetscObject)B,"MatMatMult_mpidense_mpiaij_C",MatMatMult_MPIDense_MPIAIJ);
5633:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C",MatMatMultSymbolic_MPIDense_MPIAIJ);
5634:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C",MatMatMultNumeric_MPIDense_MPIAIJ);
5635:   PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);
5636:   return(0);
5637: }

5641: /*@
5642:      MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal"
5643:          and "off-diagonal" part of the matrix in CSR format.

5645:    Collective on MPI_Comm

5647:    Input Parameters:
5648: +  comm - MPI communicator
5649: .  m - number of local rows (Cannot be PETSC_DECIDE)
5650: .  n - This value should be the same as the local size used in creating the
5651:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
5652:        calculated if N is given) For square matrices n is almost always m.
5653: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
5654: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
5655: .   i - row indices for "diagonal" portion of matrix
5656: .   j - column indices
5657: .   a - matrix values
5658: .   oi - row indices for "off-diagonal" portion of matrix
5659: .   oj - column indices
5660: -   oa - matrix values

5662:    Output Parameter:
5663: .   mat - the matrix

5665:    Level: advanced

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

5671:        The i and j indices are 0 based

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

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

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

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

5686: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
5687:           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays()
5688: @*/
5689: 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)
5690: {
5692:   Mat_MPIAIJ     *maij;

5695:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
5696:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
5697:   if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0");
5698:   MatCreate(comm,mat);
5699:   MatSetSizes(*mat,m,n,M,N);
5700:   MatSetType(*mat,MATMPIAIJ);
5701:   maij = (Mat_MPIAIJ*) (*mat)->data;

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

5705:   PetscLayoutSetUp((*mat)->rmap);
5706:   PetscLayoutSetUp((*mat)->cmap);

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

5711:   MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);
5712:   MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);
5713:   MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);
5714:   MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);

5716:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
5717:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
5718:   MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
5719:   return(0);
5720: }

5722: /*
5723:     Special version for direct calls from Fortran
5724: */
5725: #include <petsc-private/fortranimpl.h>

5727: #if defined(PETSC_HAVE_FORTRAN_CAPS)
5728: #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
5729: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
5730: #define matsetvaluesmpiaij_ matsetvaluesmpiaij
5731: #endif

5733: /* Change these macros so can be used in void function */
5734: #undef CHKERRQ
5735: #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr)
5736: #undef SETERRQ2
5737: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
5738: #undef SETERRQ3
5739: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
5740: #undef SETERRQ
5741: #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr)

5745: 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)
5746: {
5747:   Mat            mat  = *mmat;
5748:   PetscInt       m    = *mm, n = *mn;
5749:   InsertMode     addv = *maddv;
5750:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
5751:   PetscScalar    value;

5754:   MatCheckPreallocated(mat,1);
5755:   if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;

5757: #if defined(PETSC_USE_DEBUG)
5758:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
5759: #endif
5760:   {
5761:     PetscInt  i,j,rstart  = mat->rmap->rstart,rend = mat->rmap->rend;
5762:     PetscInt  cstart      = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
5763:     PetscBool roworiented = aij->roworiented;

5765:     /* Some Variables required in the macro */
5766:     Mat        A                 = aij->A;
5767:     Mat_SeqAIJ *a                = (Mat_SeqAIJ*)A->data;
5768:     PetscInt   *aimax            = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
5769:     MatScalar  *aa               = a->a;
5770:     PetscBool  ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES)) ? PETSC_TRUE : PETSC_FALSE);
5771:     Mat        B                 = aij->B;
5772:     Mat_SeqAIJ *b                = (Mat_SeqAIJ*)B->data;
5773:     PetscInt   *bimax            = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
5774:     MatScalar  *ba               = b->a;

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

5781:     for (i=0; i<m; i++) {
5782:       if (im[i] < 0) continue;
5783: #if defined(PETSC_USE_DEBUG)
5784:       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);
5785: #endif
5786:       if (im[i] >= rstart && im[i] < rend) {
5787:         row      = im[i] - rstart;
5788:         lastcol1 = -1;
5789:         rp1      = aj + ai[row];
5790:         ap1      = aa + ai[row];
5791:         rmax1    = aimax[row];
5792:         nrow1    = ailen[row];
5793:         low1     = 0;
5794:         high1    = nrow1;
5795:         lastcol2 = -1;
5796:         rp2      = bj + bi[row];
5797:         ap2      = ba + bi[row];
5798:         rmax2    = bimax[row];
5799:         nrow2    = bilen[row];
5800:         low2     = 0;
5801:         high2    = nrow2;

5803:         for (j=0; j<n; j++) {
5804:           if (roworiented) value = v[i*n+j];
5805:           else value = v[i+j*m];
5806:           if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
5807:           if (in[j] >= cstart && in[j] < cend) {
5808:             col = in[j] - cstart;
5809:             MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
5810:           } else if (in[j] < 0) continue;
5811: #if defined(PETSC_USE_DEBUG)
5812:           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);
5813: #endif
5814:           else {
5815:             if (mat->was_assembled) {
5816:               if (!aij->colmap) {
5817:                 MatCreateColmap_MPIAIJ_Private(mat);
5818:               }
5819: #if defined(PETSC_USE_CTABLE)
5820:               PetscTableFind(aij->colmap,in[j]+1,&col);
5821:               col--;
5822: #else
5823:               col = aij->colmap[in[j]] - 1;
5824: #endif
5825:               if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
5826:                 MatDisAssemble_MPIAIJ(mat);
5827:                 col  =  in[j];
5828:                 /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
5829:                 B     = aij->B;
5830:                 b     = (Mat_SeqAIJ*)B->data;
5831:                 bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
5832:                 rp2   = bj + bi[row];
5833:                 ap2   = ba + bi[row];
5834:                 rmax2 = bimax[row];
5835:                 nrow2 = bilen[row];
5836:                 low2  = 0;
5837:                 high2 = nrow2;
5838:                 bm    = aij->B->rmap->n;
5839:                 ba    = b->a;
5840:               }
5841:             } else col = in[j];
5842:             MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
5843:           }
5844:         }
5845:       } else if (!aij->donotstash) {
5846:         if (roworiented) {
5847:           MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
5848:         } else {
5849:           MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
5850:         }
5851:       }
5852:     }
5853:   }
5854:   PetscFunctionReturnVoid();
5855: }