Actual source code: mpiaij.c

petsc-3.3-p3 2012-08-29
  2: #include <../src/mat/impls/aij/mpi/mpiaij.h>   /*I "petscmat.h" I*/
  3: #include <petscblaslapack.h>

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

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

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

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

 20:   Level: beginner

 22: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
 23: M*/

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

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

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

 37:   Level: beginner

 39: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
 40: M*/

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

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

107: PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows)
108: {
109:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)M->data;
111:   PetscInt       i,rstart,nrows,*rows;

114:   *zrows = PETSC_NULL;
115:   MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);
116:   MatGetOwnershipRange(M,&rstart,PETSC_NULL);
117:   for (i=0; i<nrows; i++) rows[i] += rstart;
118:   ISCreateGeneral(((PetscObject)M)->comm,nrows,rows,PETSC_OWN_POINTER,zrows);
119:   return(0);
120: }

124: PetscErrorCode MatGetColumnNorms_MPIAIJ(Mat A,NormType type,PetscReal *norms)
125: {
127:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)A->data;
128:   PetscInt       i,n,*garray = aij->garray;
129:   Mat_SeqAIJ     *a_aij = (Mat_SeqAIJ*) aij->A->data;
130:   Mat_SeqAIJ     *b_aij = (Mat_SeqAIJ*) aij->B->data;
131:   PetscReal      *work;

134:   MatGetSize(A,PETSC_NULL,&n);
135:   PetscMalloc(n*sizeof(PetscReal),&work);
136:   PetscMemzero(work,n*sizeof(PetscReal));
137:   if (type == NORM_2) {
138:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
139:       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]*a_aij->a[i]);
140:     }
141:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
142:       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]*b_aij->a[i]);
143:     }
144:   } else if (type == NORM_1) {
145:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
146:       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]);
147:     }
148:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
149:       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]);
150:     }
151:   } else if (type == NORM_INFINITY) {
152:     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
153:       work[A->cmap->rstart + a_aij->j[i]] = PetscMax(PetscAbsScalar(a_aij->a[i]), work[A->cmap->rstart + a_aij->j[i]]);
154:     }
155:     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
156:       work[garray[b_aij->j[i]]] = PetscMax(PetscAbsScalar(b_aij->a[i]),work[garray[b_aij->j[i]]]);
157:     }

159:   } else SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONG,"Unknown NormType");
160:   if (type == NORM_INFINITY) {
161:     MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,A->hdr.comm);
162:   } else {
163:     MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,A->hdr.comm);
164:   }
165:   PetscFree(work);
166:   if (type == NORM_2) {
167:     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
168:   }
169:   return(0);
170: }

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

178:     Only for square matrices
179: */
180: PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat)
181: {
182:   PetscMPIInt    rank,size;
183:   PetscInt       *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz,*gmataj,cnt,row,*ld;
185:   Mat            mat;
186:   Mat_SeqAIJ     *gmata;
187:   PetscMPIInt    tag;
188:   MPI_Status     status;
189:   PetscBool      aij;
190:   MatScalar      *gmataa,*ao,*ad,*gmataarestore=0;

193:   CHKMEMQ;
194:   MPI_Comm_rank(comm,&rank);
195:   MPI_Comm_size(comm,&size);
196:   if (!rank) {
197:     PetscObjectTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);
198:     if (!aij) SETERRQ1(((PetscObject)gmat)->comm,PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name);
199:   }
200:   if (reuse == MAT_INITIAL_MATRIX) {
201:     MatCreate(comm,&mat);
202:     MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);
203:     MatSetBlockSizes(mat,gmat->rmap->bs,gmat->cmap->bs);
204:     MatSetType(mat,MATAIJ);
205:     PetscMalloc((size+1)*sizeof(PetscInt),&rowners);
206:     PetscMalloc2(m,PetscInt,&dlens,m,PetscInt,&olens);
207:     MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);
208:     rowners[0] = 0;
209:     for (i=2; i<=size; i++) {
210:       rowners[i] += rowners[i-1];
211:     }
212:     rstart = rowners[rank];
213:     rend   = rowners[rank+1];
214:     PetscObjectGetNewTag((PetscObject)mat,&tag);
215:     if (!rank) {
216:       gmata = (Mat_SeqAIJ*) gmat->data;
217:       /* send row lengths to all processors */
218:       for (i=0; i<m; i++) dlens[i] = gmata->ilen[i];
219:       for (i=1; i<size; i++) {
220:         MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);
221:       }
222:       /* determine number diagonal and off-diagonal counts */
223:       PetscMemzero(olens,m*sizeof(PetscInt));
224:       PetscMalloc(m*sizeof(PetscInt),&ld);
225:       PetscMemzero(ld,m*sizeof(PetscInt));
226:       jj = 0;
227:       for (i=0; i<m; i++) {
228:         for (j=0; j<dlens[i]; j++) {
229:           if (gmata->j[jj] < rstart) ld[i]++;
230:           if (gmata->j[jj] < rstart || gmata->j[jj] >= rend) olens[i]++;
231:           jj++;
232:         }
233:       }
234:       /* send column indices to other processes */
235:       for (i=1; i<size; i++) {
236:         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
237:         MPI_Send(&nz,1,MPIU_INT,i,tag,comm);
238:         MPI_Send(gmata->j + gmata->i[rowners[i]],nz,MPIU_INT,i,tag,comm);
239:       }

241:       /* send numerical values to other processes */
242:       for (i=1; i<size; i++) {
243:         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
244:         MPI_Send(gmata->a + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);
245:       }
246:       gmataa = gmata->a;
247:       gmataj = gmata->j;

249:     } else {
250:       /* receive row lengths */
251:       MPI_Recv(dlens,m,MPIU_INT,0,tag,comm,&status);
252:       /* receive column indices */
253:       MPI_Recv(&nz,1,MPIU_INT,0,tag,comm,&status);
254:       PetscMalloc2(nz,PetscScalar,&gmataa,nz,PetscInt,&gmataj);
255:       MPI_Recv(gmataj,nz,MPIU_INT,0,tag,comm,&status);
256:       /* determine number diagonal and off-diagonal counts */
257:       PetscMemzero(olens,m*sizeof(PetscInt));
258:       PetscMalloc(m*sizeof(PetscInt),&ld);
259:       PetscMemzero(ld,m*sizeof(PetscInt));
260:       jj = 0;
261:       for (i=0; i<m; i++) {
262:         for (j=0; j<dlens[i]; j++) {
263:           if (gmataj[jj] < rstart) ld[i]++;
264:           if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++;
265:           jj++;
266:         }
267:       }
268:       /* receive numerical values */
269:       PetscMemzero(gmataa,nz*sizeof(PetscScalar));
270:       MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);
271:     }
272:     /* set preallocation */
273:     for (i=0; i<m; i++) {
274:       dlens[i] -= olens[i];
275:     }
276:     MatSeqAIJSetPreallocation(mat,0,dlens);
277:     MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);
278: 
279:     for (i=0; i<m; i++) {
280:       dlens[i] += olens[i];
281:     }
282:     cnt  = 0;
283:     for (i=0; i<m; i++) {
284:       row  = rstart + i;
285:       MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);
286:       cnt += dlens[i];
287:     }
288:     if (rank) {
289:       PetscFree2(gmataa,gmataj);
290:     }
291:     PetscFree2(dlens,olens);
292:     PetscFree(rowners);
293:     ((Mat_MPIAIJ*)(mat->data))->ld = ld;
294:     *inmat = mat;
295:   } else {   /* column indices are already set; only need to move over numerical values from process 0 */
296:     Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data;
297:     Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data;
298:     mat   = *inmat;
299:     PetscObjectGetNewTag((PetscObject)mat,&tag);
300:     if (!rank) {
301:       /* send numerical values to other processes */
302:       gmata = (Mat_SeqAIJ*) gmat->data;
303:       MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);
304:       gmataa = gmata->a;
305:       for (i=1; i<size; i++) {
306:         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
307:         MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);
308:       }
309:       nz   = gmata->i[rowners[1]]-gmata->i[rowners[0]];
310:     } else {
311:       /* receive numerical values from process 0*/
312:       nz   = Ad->nz + Ao->nz;
313:       PetscMalloc(nz*sizeof(PetscScalar),&gmataa); gmataarestore = gmataa;
314:       MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);
315:     }
316:     /* transfer numerical values into the diagonal A and off diagonal B parts of mat */
317:     ld = ((Mat_MPIAIJ*)(mat->data))->ld;
318:     ad = Ad->a;
319:     ao = Ao->a;
320:     if (mat->rmap->n) {
321:       i  = 0;
322:       nz = ld[i];                                   PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar)); ao += nz; gmataa += nz;
323:       nz = Ad->i[i+1] - Ad->i[i];                   PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar)); ad += nz; gmataa += nz;
324:     }
325:     for (i=1; i<mat->rmap->n; i++) {
326:       nz = Ao->i[i] - Ao->i[i-1] - ld[i-1] + ld[i]; PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar)); ao += nz; gmataa += nz;
327:       nz = Ad->i[i+1] - Ad->i[i];                   PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar)); ad += nz; gmataa += nz;
328:     }
329:     i--;
330:     if (mat->rmap->n) {
331:       nz = Ao->i[i+1] - Ao->i[i] - ld[i];           PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar)); ao += nz; gmataa += nz;
332:     }
333:     if (rank) {
334:       PetscFree(gmataarestore);
335:     }
336:   }
337:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
338:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
339:   CHKMEMQ;
340:   return(0);
341: }

343: /* 
344:   Local utility routine that creates a mapping from the global column 
345: number to the local number in the off-diagonal part of the local 
346: storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at 
347: a slightly higher hash table cost; without it it is not scalable (each processor
348: has an order N integer array but is fast to acess.
349: */
352: PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat mat)
353: {
354:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
356:   PetscInt       n = aij->B->cmap->n,i;

359:   if (!aij->garray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPIAIJ Matrix was assembled but is missing garray");
360: #if defined (PETSC_USE_CTABLE)
361:   PetscTableCreate(n,mat->cmap->N+1,&aij->colmap);
362:   for (i=0; i<n; i++){
363:     PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1,INSERT_VALUES);
364:   }
365: #else
366:   PetscMalloc((mat->cmap->N+1)*sizeof(PetscInt),&aij->colmap);
367:   PetscLogObjectMemory(mat,mat->cmap->N*sizeof(PetscInt));
368:   PetscMemzero(aij->colmap,mat->cmap->N*sizeof(PetscInt));
369:   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
370: #endif
371:   return(0);
372: }

374: #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
375: { \
376:     if (col <= lastcol1) low1 = 0; else high1 = nrow1; \
377:     lastcol1 = col;\
378:     while (high1-low1 > 5) { \
379:       t = (low1+high1)/2; \
380:       if (rp1[t] > col) high1 = t; \
381:       else             low1  = t; \
382:     } \
383:       for (_i=low1; _i<high1; _i++) { \
384:         if (rp1[_i] > col) break; \
385:         if (rp1[_i] == col) { \
386:           if (addv == ADD_VALUES) ap1[_i] += value;   \
387:           else                    ap1[_i] = value; \
388:           goto a_noinsert; \
389:         } \
390:       }  \
391:       if (value == 0.0 && ignorezeroentries) {low1 = 0; high1 = nrow1;goto a_noinsert;} \
392:       if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;}                \
393:       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
394:       MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \
395:       N = nrow1++ - 1; a->nz++; high1++; \
396:       /* shift up all the later entries in this row */ \
397:       for (ii=N; ii>=_i; ii--) { \
398:         rp1[ii+1] = rp1[ii]; \
399:         ap1[ii+1] = ap1[ii]; \
400:       } \
401:       rp1[_i] = col;  \
402:       ap1[_i] = value;  \
403:       a_noinsert: ; \
404:       ailen[row] = nrow1; \
405: } 


408: #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
409: { \
410:     if (col <= lastcol2) low2 = 0; else high2 = nrow2; \
411:     lastcol2 = col;\
412:     while (high2-low2 > 5) { \
413:       t = (low2+high2)/2; \
414:       if (rp2[t] > col) high2 = t; \
415:       else             low2  = t; \
416:     } \
417:     for (_i=low2; _i<high2; _i++) {                \
418:       if (rp2[_i] > col) break;                        \
419:       if (rp2[_i] == col) {                              \
420:         if (addv == ADD_VALUES) ap2[_i] += value;     \
421:         else                    ap2[_i] = value;      \
422:         goto b_noinsert;                              \
423:       }                                                      \
424:     }                                                              \
425:     if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \
426:     if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;}                \
427:     if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
428:     MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \
429:     N = nrow2++ - 1; b->nz++; high2++;                                        \
430:     /* shift up all the later entries in this row */                        \
431:     for (ii=N; ii>=_i; ii--) {                                                \
432:       rp2[ii+1] = rp2[ii];                                                \
433:       ap2[ii+1] = ap2[ii];                                                \
434:     }                                                                        \
435:     rp2[_i] = col;                                                        \
436:     ap2[_i] = value;                                                        \
437:     b_noinsert: ;                                                                \
438:     bilen[row] = nrow2;                                                        \
439: }

443: PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[])
444: {
445:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)A->data;
446:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data;
448:   PetscInt       l,*garray = mat->garray,diag;

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

453:   /* find size of row to the left of the diagonal part */
454:   MatGetOwnershipRange(A,&diag,0);
455:   row  = row - diag;
456:   for (l=0; l<b->i[row+1]-b->i[row]; l++) {
457:     if (garray[b->j[b->i[row]+l]] > diag) break;
458:   }
459:   PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));

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

464:   /* right of diagonal part */
465:   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));
466:   return(0);
467: }

471: PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
472: {
473:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
474:   PetscScalar    value;
476:   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
477:   PetscInt       cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
478:   PetscBool      roworiented = aij->roworiented;

480:   /* Some Variables required in the macro */
481:   Mat            A = aij->A;
482:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
483:   PetscInt       *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
484:   MatScalar      *aa = a->a;
485:   PetscBool      ignorezeroentries = a->ignorezeroentries;
486:   Mat            B = aij->B;
487:   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
488:   PetscInt       *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
489:   MatScalar      *ba = b->a;

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

497:   for (i=0; i<m; i++) {
498:     if (im[i] < 0) continue;
499: #if defined(PETSC_USE_DEBUG)
500:     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);
501: #endif
502:     if (im[i] >= rstart && im[i] < rend) {
503:       row      = im[i] - rstart;
504:       lastcol1 = -1;
505:       rp1      = aj + ai[row];
506:       ap1      = aa + ai[row];
507:       rmax1    = aimax[row];
508:       nrow1    = ailen[row];
509:       low1     = 0;
510:       high1    = nrow1;
511:       lastcol2 = -1;
512:       rp2      = bj + bi[row];
513:       ap2      = ba + bi[row];
514:       rmax2    = bimax[row];
515:       nrow2    = bilen[row];
516:       low2     = 0;
517:       high2    = nrow2;

519:       for (j=0; j<n; j++) {
520:         if (v) {if (roworiented) value = v[i*n+j]; else value = v[i+j*m];} else value = 0.0;
521:         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
522:         if (in[j] >= cstart && in[j] < cend){
523:           col = in[j] - cstart;
524:           nonew = a->nonew;
525:           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
526:         } else if (in[j] < 0) continue;
527: #if defined(PETSC_USE_DEBUG)
528:         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);
529: #endif
530:         else {
531:           if (mat->was_assembled) {
532:             if (!aij->colmap) {
533:               MatCreateColmap_MPIAIJ_Private(mat);
534:             }
535: #if defined (PETSC_USE_CTABLE)
536:             PetscTableFind(aij->colmap,in[j]+1,&col);
537:             col--;
538: #else
539:             col = aij->colmap[in[j]] - 1;
540: #endif
541:             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
542:               MatDisAssemble_MPIAIJ(mat);
543:               col =  in[j];
544:               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
545:               B = aij->B;
546:               b = (Mat_SeqAIJ*)B->data;
547:               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a;
548:               rp2      = bj + bi[row];
549:               ap2      = ba + bi[row];
550:               rmax2    = bimax[row];
551:               nrow2    = bilen[row];
552:               low2     = 0;
553:               high2    = nrow2;
554:               bm       = aij->B->rmap->n;
555:               ba = b->a;
556:             }
557:           } else col = in[j];
558:           nonew = b->nonew;
559:           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
560:         }
561:       }
562:     } else {
563:       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]);
564:       if (!aij->donotstash) {
565:         mat->assembled = PETSC_FALSE;
566:         if (roworiented) {
567:           MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
568:         } else {
569:           MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
570:         }
571:       }
572:     }
573:   }
574:   return(0);
575: }

579: PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
580: {
581:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
583:   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
584:   PetscInt       cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;

587:   for (i=0; i<m; i++) {
588:     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
589:     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);
590:     if (idxm[i] >= rstart && idxm[i] < rend) {
591:       row = idxm[i] - rstart;
592:       for (j=0; j<n; j++) {
593:         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
594:         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);
595:         if (idxn[j] >= cstart && idxn[j] < cend){
596:           col = idxn[j] - cstart;
597:           MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);
598:         } else {
599:           if (!aij->colmap) {
600:             MatCreateColmap_MPIAIJ_Private(mat);
601:           }
602: #if defined (PETSC_USE_CTABLE)
603:           PetscTableFind(aij->colmap,idxn[j]+1,&col);
604:           col --;
605: #else
606:           col = aij->colmap[idxn[j]] - 1;
607: #endif
608:           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
609:           else {
610:             MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);
611:           }
612:         }
613:       }
614:     } else {
615:       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
616:     }
617:   }
618:   return(0);
619: }

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

625: PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
626: {
627:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
629:   PetscInt       nstash,reallocs;
630:   InsertMode     addv;

633:   if (aij->donotstash || mat->nooffprocentries) {
634:     return(0);
635:   }

637:   /* make sure all processors are either in INSERTMODE or ADDMODE */
638:   MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,((PetscObject)mat)->comm);
639:   if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
640:   mat->insertmode = addv; /* in case this processor had no cache */

642:   MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
643:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
644:   PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
645:   return(0);
646: }

650: PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
651: {
652:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
653:   Mat_SeqAIJ     *a=(Mat_SeqAIJ *)aij->A->data;
655:   PetscMPIInt    n;
656:   PetscInt       i,j,rstart,ncols,flg;
657:   PetscInt       *row,*col;
658:   PetscBool      other_disassembled;
659:   PetscScalar    *val;
660:   InsertMode     addv = mat->insertmode;

662:   /* do not use 'b = (Mat_SeqAIJ *)aij->B->data' as B can be reset in disassembly */
664:   if (!aij->donotstash && !mat->nooffprocentries) {
665:     while (1) {
666:       MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
667:       if (!flg) break;

669:       for (i=0; i<n;) {
670:         /* Now identify the consecutive vals belonging to the same row */
671:         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
672:         if (j < n) ncols = j-i;
673:         else       ncols = n-i;
674:         /* Now assemble all these values with a single function call */
675:         MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);
676:         i = j;
677:       }
678:     }
679:     MatStashScatterEnd_Private(&mat->stash);
680:   }
681:   MatAssemblyBegin(aij->A,mode);
682:   MatAssemblyEnd(aij->A,mode);

684:   /* determine if any processor has disassembled, if so we must 
685:      also disassemble ourselfs, in order that we may reassemble. */
686:   /*
687:      if nonzero structure of submatrix B cannot change then we know that
688:      no processor disassembled thus we can skip this stuff
689:   */
690:   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
691:     MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,((PetscObject)mat)->comm);
692:     if (mat->was_assembled && !other_disassembled) {
693:       MatDisAssemble_MPIAIJ(mat);
694:     }
695:   }
696:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
697:     MatSetUpMultiply_MPIAIJ(mat);
698:   }
699:   MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);
700:   MatSetOption(aij->B,MAT_CHECK_COMPRESSED_ROW,PETSC_FALSE);
701:   MatAssemblyBegin(aij->B,mode);
702:   MatAssemblyEnd(aij->B,mode);

704:   PetscFree2(aij->rowvalues,aij->rowindices);
705:   aij->rowvalues = 0;

707:   /* used by MatAXPY() */
708:   a->xtoy = 0; ((Mat_SeqAIJ *)aij->B->data)->xtoy = 0;  /* b->xtoy = 0 */
709:   a->XtoY = 0; ((Mat_SeqAIJ *)aij->B->data)->XtoY = 0;  /* b->XtoY = 0 */

711:   VecDestroy(&aij->diag);
712:   if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ;
713:   return(0);
714: }

718: PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
719: {
720:   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;

724:   MatZeroEntries(l->A);
725:   MatZeroEntries(l->B);
726:   return(0);
727: }

731: PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
732: {
733:   Mat_MPIAIJ        *l = (Mat_MPIAIJ*)A->data;
734:   PetscErrorCode    ierr;
735:   PetscMPIInt       size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1;
736:   PetscInt          i,*owners = A->rmap->range;
737:   PetscInt          *nprocs,j,idx,nsends,row;
738:   PetscInt          nmax,*svalues,*starts,*owner,nrecvs;
739:   PetscInt          *rvalues,count,base,slen,*source;
740:   PetscInt          *lens,*lrows,*values,rstart=A->rmap->rstart;
741:   MPI_Comm          comm = ((PetscObject)A)->comm;
742:   MPI_Request       *send_waits,*recv_waits;
743:   MPI_Status        recv_status,*send_status;
744:   const PetscScalar *xx;
745:   PetscScalar       *bb;
746: #if defined(PETSC_DEBUG)
747:   PetscBool      found = PETSC_FALSE;
748: #endif

751:   /*  first count number of contributors to each processor */
752:   PetscMalloc(2*size*sizeof(PetscInt),&nprocs);
753:   PetscMemzero(nprocs,2*size*sizeof(PetscInt));
754:   PetscMalloc((N+1)*sizeof(PetscInt),&owner); /* see note*/
755:   j = 0;
756:   for (i=0; i<N; i++) {
757:     if (lastidx > (idx = rows[i])) j = 0;
758:     lastidx = idx;
759:     for (; j<size; j++) {
760:       if (idx >= owners[j] && idx < owners[j+1]) {
761:         nprocs[2*j]++;
762:         nprocs[2*j+1] = 1;
763:         owner[i] = j;
764: #if defined(PETSC_DEBUG)
765:         found = PETSC_TRUE;
766: #endif
767:         break;
768:       }
769:     }
770: #if defined(PETSC_DEBUG)
771:     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
772:     found = PETSC_FALSE;
773: #endif
774:   }
775:   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}

777:   if (A->nooffproczerorows) {
778:     if (nsends > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"You called MatSetOption(,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) but set an off process zero row");
779:     nrecvs = nsends;
780:     nmax   = N;
781:   } else {
782:     /* inform other processors of number of messages and max length*/
783:     PetscMaxSum(comm,nprocs,&nmax,&nrecvs);
784:   }

786:   /* post receives:   */
787:   PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);
788:   PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);
789:   for (i=0; i<nrecvs; i++) {
790:     MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);
791:   }

793:   /* do sends:
794:       1) starts[i] gives the starting index in svalues for stuff going to 
795:          the ith processor
796:   */
797:   PetscMalloc((N+1)*sizeof(PetscInt),&svalues);
798:   PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);
799:   PetscMalloc((size+1)*sizeof(PetscInt),&starts);
800:   starts[0] = 0;
801:   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
802:   for (i=0; i<N; i++) {
803:     svalues[starts[owner[i]]++] = rows[i];
804:   }

806:   starts[0] = 0;
807:   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
808:   count = 0;
809:   for (i=0; i<size; i++) {
810:     if (nprocs[2*i+1]) {
811:       MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);
812:     }
813:   }
814:   PetscFree(starts);

816:   base = owners[rank];

818:   /*  wait on receives */
819:   PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);
820:   count  = nrecvs; slen = 0;
821:   while (count) {
822:     MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);
823:     /* unpack receives into our local space */
824:     MPI_Get_count(&recv_status,MPIU_INT,&n);
825:     source[imdex]  = recv_status.MPI_SOURCE;
826:     lens[imdex]    = n;
827:     slen          += n;
828:     count--;
829:   }
830:   PetscFree(recv_waits);
831: 
832:   /* move the data into the send scatter */
833:   PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);
834:   count = 0;
835:   for (i=0; i<nrecvs; i++) {
836:     values = rvalues + i*nmax;
837:     for (j=0; j<lens[i]; j++) {
838:       lrows[count++] = values[j] - base;
839:     }
840:   }
841:   PetscFree(rvalues);
842:   PetscFree2(lens,source);
843:   PetscFree(owner);
844:   PetscFree(nprocs);
845: 
846:   /* fix right hand side if needed */
847:   if (x && b) {
848:     VecGetArrayRead(x,&xx);
849:     VecGetArray(b,&bb);
850:     for (i=0; i<slen; i++) {
851:       bb[lrows[i]] = diag*xx[lrows[i]];
852:     }
853:     VecRestoreArrayRead(x,&xx);
854:     VecRestoreArray(b,&bb);
855:   }
856:   /*
857:         Zero the required rows. If the "diagonal block" of the matrix
858:      is square and the user wishes to set the diagonal we use separate
859:      code so that MatSetValues() is not called for each diagonal allocating
860:      new memory, thus calling lots of mallocs and slowing things down.

862:   */
863:   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
864:   MatZeroRows(l->B,slen,lrows,0.0,0,0);
865:   if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) {
866:     MatZeroRows(l->A,slen,lrows,diag,0,0);
867:   } else if (diag != 0.0) {
868:     MatZeroRows(l->A,slen,lrows,0.0,0,0);
869:     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
870:       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
871: MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
872:     }
873:     for (i = 0; i < slen; i++) {
874:       row  = lrows[i] + rstart;
875:       MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);
876:     }
877:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
878:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
879:   } else {
880:     MatZeroRows(l->A,slen,lrows,0.0,0,0);
881:   }
882:   PetscFree(lrows);

884:   /* wait on sends */
885:   if (nsends) {
886:     PetscMalloc(nsends*sizeof(MPI_Status),&send_status);
887:     MPI_Waitall(nsends,send_waits,send_status);
888:     PetscFree(send_status);
889:   }
890:   PetscFree(send_waits);
891:   PetscFree(svalues);
892:   return(0);
893: }

897: PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
898: {
899:   Mat_MPIAIJ        *l = (Mat_MPIAIJ*)A->data;
900:   PetscErrorCode    ierr;
901:   PetscMPIInt       size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1;
902:   PetscInt          i,*owners = A->rmap->range;
903:   PetscInt          *nprocs,j,idx,nsends;
904:   PetscInt          nmax,*svalues,*starts,*owner,nrecvs;
905:   PetscInt          *rvalues,count,base,slen,*source;
906:   PetscInt          *lens,*lrows,*values,m;
907:   MPI_Comm          comm = ((PetscObject)A)->comm;
908:   MPI_Request       *send_waits,*recv_waits;
909:   MPI_Status        recv_status,*send_status;
910:   const PetscScalar *xx;
911:   PetscScalar       *bb,*mask;
912:   Vec               xmask,lmask;
913:   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*)l->B->data;
914:   const PetscInt    *aj, *ii,*ridx;
915:   PetscScalar       *aa;
916: #if defined(PETSC_DEBUG)
917:   PetscBool         found = PETSC_FALSE;
918: #endif

921:   /*  first count number of contributors to each processor */
922:   PetscMalloc(2*size*sizeof(PetscInt),&nprocs);
923:   PetscMemzero(nprocs,2*size*sizeof(PetscInt));
924:   PetscMalloc((N+1)*sizeof(PetscInt),&owner); /* see note*/
925:   j = 0;
926:   for (i=0; i<N; i++) {
927:     if (lastidx > (idx = rows[i])) j = 0;
928:     lastidx = idx;
929:     for (; j<size; j++) {
930:       if (idx >= owners[j] && idx < owners[j+1]) {
931:         nprocs[2*j]++;
932:         nprocs[2*j+1] = 1;
933:         owner[i] = j;
934: #if defined(PETSC_DEBUG)
935:         found = PETSC_TRUE;
936: #endif
937:         break;
938:       }
939:     }
940: #if defined(PETSC_DEBUG)
941:     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
942:     found = PETSC_FALSE;
943: #endif
944:   }
945:   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}

947:   /* inform other processors of number of messages and max length*/
948:   PetscMaxSum(comm,nprocs,&nmax,&nrecvs);

950:   /* post receives:   */
951:   PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);
952:   PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);
953:   for (i=0; i<nrecvs; i++) {
954:     MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);
955:   }

957:   /* do sends:
958:       1) starts[i] gives the starting index in svalues for stuff going to 
959:          the ith processor
960:   */
961:   PetscMalloc((N+1)*sizeof(PetscInt),&svalues);
962:   PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);
963:   PetscMalloc((size+1)*sizeof(PetscInt),&starts);
964:   starts[0] = 0;
965:   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
966:   for (i=0; i<N; i++) {
967:     svalues[starts[owner[i]]++] = rows[i];
968:   }

970:   starts[0] = 0;
971:   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
972:   count = 0;
973:   for (i=0; i<size; i++) {
974:     if (nprocs[2*i+1]) {
975:       MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);
976:     }
977:   }
978:   PetscFree(starts);

980:   base = owners[rank];

982:   /*  wait on receives */
983:   PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);
984:   count  = nrecvs; slen = 0;
985:   while (count) {
986:     MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);
987:     /* unpack receives into our local space */
988:     MPI_Get_count(&recv_status,MPIU_INT,&n);
989:     source[imdex]  = recv_status.MPI_SOURCE;
990:     lens[imdex]    = n;
991:     slen          += n;
992:     count--;
993:   }
994:   PetscFree(recv_waits);
995: 
996:   /* move the data into the send scatter */
997:   PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);
998:   count = 0;
999:   for (i=0; i<nrecvs; i++) {
1000:     values = rvalues + i*nmax;
1001:     for (j=0; j<lens[i]; j++) {
1002:       lrows[count++] = values[j] - base;
1003:     }
1004:   }
1005:   PetscFree(rvalues);
1006:   PetscFree2(lens,source);
1007:   PetscFree(owner);
1008:   PetscFree(nprocs);
1009:   /* lrows are the local rows to be zeroed, slen is the number of local rows */

1011:   /* zero diagonal part of matrix */
1012:   MatZeroRowsColumns(l->A,slen,lrows,diag,x,b);
1013: 
1014:   /* handle off diagonal part of matrix */
1015:   MatGetVecs(A,&xmask,PETSC_NULL);
1016:   VecDuplicate(l->lvec,&lmask);
1017:   VecGetArray(xmask,&bb);
1018:   for (i=0; i<slen; i++) {
1019:     bb[lrows[i]] = 1;
1020:   }
1021:   VecRestoreArray(xmask,&bb);
1022:   VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
1023:   VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
1024:   VecDestroy(&xmask);
1025:   if (x) {
1026:     VecScatterBegin(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);
1027:     VecScatterEnd(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);
1028:     VecGetArrayRead(l->lvec,&xx);
1029:     VecGetArray(b,&bb);
1030:   }
1031:   VecGetArray(lmask,&mask);

1033:   /* remove zeroed rows of off diagonal matrix */
1034:   ii = aij->i;
1035:   for (i=0; i<slen; i++) {
1036:     PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));
1037:   }

1039:   /* loop over all elements of off process part of matrix zeroing removed columns*/
1040:   if (aij->compressedrow.use){
1041:     m    = aij->compressedrow.nrows;
1042:     ii   = aij->compressedrow.i;
1043:     ridx = aij->compressedrow.rindex;
1044:     for (i=0; i<m; i++){
1045:       n   = ii[i+1] - ii[i];
1046:       aj  = aij->j + ii[i];
1047:       aa  = aij->a + ii[i];

1049:       for (j=0; j<n; j++) {
1050:         if (PetscAbsScalar(mask[*aj])) {
1051:           if (b) bb[*ridx] -= *aa*xx[*aj];
1052:           *aa        = 0.0;
1053:         }
1054:         aa++;
1055:         aj++;
1056:       }
1057:       ridx++;
1058:     }
1059:   } else { /* do not use compressed row format */
1060:     m = l->B->rmap->n;
1061:     for (i=0; i<m; i++) {
1062:       n   = ii[i+1] - ii[i];
1063:       aj  = aij->j + ii[i];
1064:       aa  = aij->a + ii[i];
1065:       for (j=0; j<n; j++) {
1066:         if (PetscAbsScalar(mask[*aj])) {
1067:           if (b) bb[i] -= *aa*xx[*aj];
1068:           *aa    = 0.0;
1069:         }
1070:         aa++;
1071:         aj++;
1072:       }
1073:     }
1074:   }
1075:   if (x) {
1076:     VecRestoreArray(b,&bb);
1077:     VecRestoreArrayRead(l->lvec,&xx);
1078:   }
1079:   VecRestoreArray(lmask,&mask);
1080:   VecDestroy(&lmask);
1081:   PetscFree(lrows);

1083:   /* wait on sends */
1084:   if (nsends) {
1085:     PetscMalloc(nsends*sizeof(MPI_Status),&send_status);
1086:     MPI_Waitall(nsends,send_waits,send_status);
1087:     PetscFree(send_status);
1088:   }
1089:   PetscFree(send_waits);
1090:   PetscFree(svalues);

1092:   return(0);
1093: }

1097: PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
1098: {
1099:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1101:   PetscInt       nt;

1104:   VecGetLocalSize(xx,&nt);
1105:   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);
1106:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1107:   (*a->A->ops->mult)(a->A,xx,yy);
1108:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1109:   (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
1110:   return(0);
1111: }

1115: PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx)
1116: {
1117:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1121:   MatMultDiagonalBlock(a->A,bb,xx);
1122:   return(0);
1123: }

1127: PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1128: {
1129:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1133:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1134:   (*a->A->ops->multadd)(a->A,xx,yy,zz);
1135:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1136:   (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
1137:   return(0);
1138: }

1142: PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
1143: {
1144:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1146:   PetscBool      merged;

1149:   VecScatterGetMerged(a->Mvctx,&merged);
1150:   /* do nondiagonal part */
1151:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1152:   if (!merged) {
1153:     /* send it on its way */
1154:     VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1155:     /* do local part */
1156:     (*a->A->ops->multtranspose)(a->A,xx,yy);
1157:     /* receive remote parts: note this assumes the values are not actually */
1158:     /* added in yy until the next line, */
1159:     VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1160:   } else {
1161:     /* do local part */
1162:     (*a->A->ops->multtranspose)(a->A,xx,yy);
1163:     /* send it on its way */
1164:     VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1165:     /* values actually were received in the Begin() but we need to call this nop */
1166:     VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1167:   }
1168:   return(0);
1169: }

1171: EXTERN_C_BEGIN
1174: PetscErrorCode  MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool  *f)
1175: {
1176:   MPI_Comm       comm;
1177:   Mat_MPIAIJ     *Aij = (Mat_MPIAIJ *) Amat->data, *Bij;
1178:   Mat            Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
1179:   IS             Me,Notme;
1181:   PetscInt       M,N,first,last,*notme,i;
1182:   PetscMPIInt    size;


1186:   /* Easy test: symmetric diagonal block */
1187:   Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A;
1188:   MatIsTranspose(Adia,Bdia,tol,f);
1189:   if (!*f) return(0);
1190:   PetscObjectGetComm((PetscObject)Amat,&comm);
1191:   MPI_Comm_size(comm,&size);
1192:   if (size == 1) return(0);

1194:   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
1195:   MatGetSize(Amat,&M,&N);
1196:   MatGetOwnershipRange(Amat,&first,&last);
1197:   PetscMalloc((N-last+first)*sizeof(PetscInt),&notme);
1198:   for (i=0; i<first; i++) notme[i] = i;
1199:   for (i=last; i<M; i++) notme[i-last+first] = i;
1200:   ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);
1201:   ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);
1202:   MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);
1203:   Aoff = Aoffs[0];
1204:   MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);
1205:   Boff = Boffs[0];
1206:   MatIsTranspose(Aoff,Boff,tol,f);
1207:   MatDestroyMatrices(1,&Aoffs);
1208:   MatDestroyMatrices(1,&Boffs);
1209:   ISDestroy(&Me);
1210:   ISDestroy(&Notme);
1211:   PetscFree(notme);
1212:    return(0);
1213: }
1214: EXTERN_C_END

1218: PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1219: {
1220:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1224:   /* do nondiagonal part */
1225:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1226:   /* send it on its way */
1227:   VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1228:   /* do local part */
1229:   (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);
1230:   /* receive remote parts */
1231:   VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1232:   return(0);
1233: }

1235: /*
1236:   This only works correctly for square matrices where the subblock A->A is the 
1237:    diagonal block
1238: */
1241: PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
1242: {
1244:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1247:   if (A->rmap->N != A->cmap->N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
1248:   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");
1249:   MatGetDiagonal(a->A,v);
1250:   return(0);
1251: }

1255: PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
1256: {
1257:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1261:   MatScale(a->A,aa);
1262:   MatScale(a->B,aa);
1263:   return(0);
1264: }

1268: PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
1269: {
1270:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

1274: #if defined(PETSC_USE_LOG)
1275:   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N);
1276: #endif
1277:   MatStashDestroy_Private(&mat->stash);
1278:   VecDestroy(&aij->diag);
1279:   MatDestroy(&aij->A);
1280:   MatDestroy(&aij->B);
1281: #if defined (PETSC_USE_CTABLE)
1282:   PetscTableDestroy(&aij->colmap);
1283: #else
1284:   PetscFree(aij->colmap);
1285: #endif
1286:   PetscFree(aij->garray);
1287:   VecDestroy(&aij->lvec);
1288:   VecScatterDestroy(&aij->Mvctx);
1289:   PetscFree2(aij->rowvalues,aij->rowindices);
1290:   PetscFree(aij->ld);
1291:   PetscFree(mat->data);

1293:   PetscObjectChangeTypeName((PetscObject)mat,0);
1294:   PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);
1295:   PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);
1296:   PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);
1297:   PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);
1298:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);
1299:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);
1300:   PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);
1301:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C","",PETSC_NULL);
1302:   return(0);
1303: }

1307: PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
1308: {
1309:   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
1310:   Mat_SeqAIJ*       A = (Mat_SeqAIJ*)aij->A->data;
1311:   Mat_SeqAIJ*       B = (Mat_SeqAIJ*)aij->B->data;
1312:   PetscErrorCode    ierr;
1313:   PetscMPIInt       rank,size,tag = ((PetscObject)viewer)->tag;
1314:   int               fd;
1315:   PetscInt          nz,header[4],*row_lengths,*range=0,rlen,i;
1316:   PetscInt          nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz;
1317:   PetscScalar       *column_values;
1318:   PetscInt          message_count,flowcontrolcount;

1321:   MPI_Comm_rank(((PetscObject)mat)->comm,&rank);
1322:   MPI_Comm_size(((PetscObject)mat)->comm,&size);
1323:   nz   = A->nz + B->nz;
1324:   if (!rank) {
1325:     header[0] = MAT_FILE_CLASSID;
1326:     header[1] = mat->rmap->N;
1327:     header[2] = mat->cmap->N;
1328:     MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);
1329:     PetscViewerBinaryGetDescriptor(viewer,&fd);
1330:     PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);
1331:     /* get largest number of rows any processor has */
1332:     rlen = mat->rmap->n;
1333:     range = mat->rmap->range;
1334:     for (i=1; i<size; i++) {
1335:       rlen = PetscMax(rlen,range[i+1] - range[i]);
1336:     }
1337:   } else {
1338:     MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);
1339:     rlen = mat->rmap->n;
1340:   }

1342:   /* load up the local row counts */
1343:   PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);
1344:   for (i=0; i<mat->rmap->n; i++) {
1345:     row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
1346:   }

1348:   /* store the row lengths to the file */
1349:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1350:   if (!rank) {
1351:     PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);
1352:     for (i=1; i<size; i++) {
1353:       PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);
1354:       rlen = range[i+1] - range[i];
1355:       MPIULong_Recv(row_lengths,rlen,MPIU_INT,i,tag,((PetscObject)mat)->comm);
1356:       PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);
1357:     }
1358:     PetscViewerFlowControlEndMaster(viewer,message_count);
1359:   } else {
1360:     PetscViewerFlowControlStepWorker(viewer,rank,message_count);
1361:     MPIULong_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,((PetscObject)mat)->comm);
1362:     PetscViewerFlowControlEndWorker(viewer,message_count);
1363:   }
1364:   PetscFree(row_lengths);

1366:   /* load up the local column indices */
1367:   nzmax = nz; /* )th processor needs space a largest processor needs */
1368:   MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,((PetscObject)mat)->comm);
1369:   PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);
1370:   cnt  = 0;
1371:   for (i=0; i<mat->rmap->n; i++) {
1372:     for (j=B->i[i]; j<B->i[i+1]; j++) {
1373:       if ( (col = garray[B->j[j]]) > cstart) break;
1374:       column_indices[cnt++] = col;
1375:     }
1376:     for (k=A->i[i]; k<A->i[i+1]; k++) {
1377:       column_indices[cnt++] = A->j[k] + cstart;
1378:     }
1379:     for (; j<B->i[i+1]; j++) {
1380:       column_indices[cnt++] = garray[B->j[j]];
1381:     }
1382:   }
1383:   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);

1385:   /* store the column indices to the file */
1386:    PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1387:   if (!rank) {
1388:     MPI_Status status;
1389:     PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);
1390:     for (i=1; i<size; i++) {
1391:       PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);
1392:       MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);
1393:       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1394:       MPIULong_Recv(column_indices,rnz,MPIU_INT,i,tag,((PetscObject)mat)->comm);
1395:       PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);
1396:     }
1397:      PetscViewerFlowControlEndMaster(viewer,message_count);
1398:   } else {
1399:     PetscViewerFlowControlStepWorker(viewer,rank,message_count);
1400:     MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);
1401:     MPIULong_Send(column_indices,nz,MPIU_INT,0,tag,((PetscObject)mat)->comm);
1402:     PetscViewerFlowControlEndWorker(viewer,message_count);
1403:   }
1404:   PetscFree(column_indices);

1406:   /* load up the local column values */
1407:   PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);
1408:   cnt  = 0;
1409:   for (i=0; i<mat->rmap->n; i++) {
1410:     for (j=B->i[i]; j<B->i[i+1]; j++) {
1411:       if ( garray[B->j[j]] > cstart) break;
1412:       column_values[cnt++] = B->a[j];
1413:     }
1414:     for (k=A->i[i]; k<A->i[i+1]; k++) {
1415:       column_values[cnt++] = A->a[k];
1416:     }
1417:     for (; j<B->i[i+1]; j++) {
1418:       column_values[cnt++] = B->a[j];
1419:     }
1420:   }
1421:   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);

1423:   /* store the column values to the file */
1424:    PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1425:   if (!rank) {
1426:     MPI_Status status;
1427:     PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);
1428:     for (i=1; i<size; i++) {
1429:        PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);
1430:       MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);
1431:       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1432:       MPIULong_Recv(column_values,rnz,MPIU_SCALAR,i,tag,((PetscObject)mat)->comm);
1433:       PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);
1434:     }
1435:     PetscViewerFlowControlEndMaster(viewer,message_count);
1436:   } else {
1437:     PetscViewerFlowControlStepWorker(viewer,rank,message_count);
1438:     MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);
1439:     MPIULong_Send(column_values,nz,MPIU_SCALAR,0,tag,((PetscObject)mat)->comm);
1440:     PetscViewerFlowControlEndWorker(viewer,message_count);
1441:   }
1442:   PetscFree(column_values);
1443:   return(0);
1444: }

1448: PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
1449: {
1450:   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
1451:   PetscErrorCode    ierr;
1452:   PetscMPIInt       rank = aij->rank,size = aij->size;
1453:   PetscBool         isdraw,iascii,isbinary;
1454:   PetscViewer       sviewer;
1455:   PetscViewerFormat format;

1458:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1459:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1460:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1461:   if (iascii) {
1462:     PetscViewerGetFormat(viewer,&format);
1463:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1464:       MatInfo    info;
1465:       PetscBool  inodes;

1467:       MPI_Comm_rank(((PetscObject)mat)->comm,&rank);
1468:       MatGetInfo(mat,MAT_LOCAL,&info);
1469:       MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);
1470:       PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);
1471:       if (!inodes) {
1472:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n",
1473:                                               rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);
1474:       } else {
1475:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n",
1476:                     rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);
1477:       }
1478:       MatGetInfo(aij->A,MAT_LOCAL,&info);
1479:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
1480:       MatGetInfo(aij->B,MAT_LOCAL,&info);
1481:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
1482:       PetscViewerFlush(viewer);
1483:       PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);
1484:       PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");
1485:       VecScatterView(aij->Mvctx,viewer);
1486:       return(0);
1487:     } else if (format == PETSC_VIEWER_ASCII_INFO) {
1488:       PetscInt   inodecount,inodelimit,*inodes;
1489:       MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);
1490:       if (inodes) {
1491:         PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);
1492:       } else {
1493:         PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");
1494:       }
1495:       return(0);
1496:     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
1497:       return(0);
1498:     }
1499:   } else if (isbinary) {
1500:     if (size == 1) {
1501:       PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);
1502:       MatView(aij->A,viewer);
1503:     } else {
1504:       MatView_MPIAIJ_Binary(mat,viewer);
1505:     }
1506:     return(0);
1507:   } else if (isdraw) {
1508:     PetscDraw  draw;
1509:     PetscBool  isnull;
1510:     PetscViewerDrawGetDraw(viewer,0,&draw);
1511:     PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
1512:   }

1514:   if (size == 1) {
1515:     PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);
1516:     MatView(aij->A,viewer);
1517:   } else {
1518:     /* assemble the entire matrix onto first processor. */
1519:     Mat         A;
1520:     Mat_SeqAIJ  *Aloc;
1521:     PetscInt    M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct;
1522:     MatScalar   *a;

1524:     if (mat->rmap->N > 1024) {
1525:       PetscBool  flg = PETSC_FALSE;

1527:       PetscOptionsGetBool(((PetscObject) mat)->prefix, "-mat_ascii_output_large", &flg,PETSC_NULL);
1528:       if (!flg) {
1529:         SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"ASCII matrix output not allowed for matrices with more than 1024 rows, use binary format instead.\nYou can override this restriction using -mat_ascii_output_large.");
1530:       }
1531:     }

1533:     MatCreate(((PetscObject)mat)->comm,&A);
1534:     if (!rank) {
1535:       MatSetSizes(A,M,N,M,N);
1536:     } else {
1537:       MatSetSizes(A,0,0,M,N);
1538:     }
1539:     /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */
1540:     MatSetType(A,MATMPIAIJ);
1541:     MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);
1542:     MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
1543:     PetscLogObjectParent(mat,A);

1545:     /* copy over the A part */
1546:     Aloc = (Mat_SeqAIJ*)aij->A->data;
1547:     m = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1548:     row = mat->rmap->rstart;
1549:     for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap->rstart ;}
1550:     for (i=0; i<m; i++) {
1551:       MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);
1552:       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1553:     }
1554:     aj = Aloc->j;
1555:     for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap->rstart;}

1557:     /* copy over the B part */
1558:     Aloc = (Mat_SeqAIJ*)aij->B->data;
1559:     m    = aij->B->rmap->n;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1560:     row  = mat->rmap->rstart;
1561:     PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);
1562:     ct   = cols;
1563:     for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];}
1564:     for (i=0; i<m; i++) {
1565:       MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);
1566:       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1567:     }
1568:     PetscFree(ct);
1569:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1570:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1571:     /* 
1572:        Everyone has to call to draw the matrix since the graphics waits are
1573:        synchronized across all processors that share the PetscDraw object
1574:     */
1575:     PetscViewerGetSingleton(viewer,&sviewer);
1576:     if (!rank) {
1577:       PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,((PetscObject)mat)->name);
1578:       /* Set the type name to MATMPIAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqAIJ_ASCII()*/
1579:       PetscStrcpy(((PetscObject)((Mat_MPIAIJ*)(A->data))->A)->type_name,MATMPIAIJ);
1580:       MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);
1581:     }
1582:     PetscViewerRestoreSingleton(viewer,&sviewer);
1583:     MatDestroy(&A);
1584:   }
1585:   return(0);
1586: }

1590: PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
1591: {
1593:   PetscBool      iascii,isdraw,issocket,isbinary;
1594: 
1596:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1597:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1598:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1599:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
1600:   if (iascii || isdraw || isbinary || issocket) {
1601:     MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);
1602:   } else {
1603:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
1604:   }
1605:   return(0);
1606: }

1610: PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1611: {
1612:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1614:   Vec            bb1 = 0;
1615:   PetscBool      hasop;

1618:   if (flag == SOR_APPLY_UPPER) {
1619:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1620:     return(0);
1621:   }

1623:   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) {
1624:     VecDuplicate(bb,&bb1);
1625:   }

1627:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
1628:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1629:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1630:       its--;
1631:     }
1632: 
1633:     while (its--) {
1634:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1635:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

1637:       /* update rhs: bb1 = bb - B*x */
1638:       VecScale(mat->lvec,-1.0);
1639:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1641:       /* local sweep */
1642:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);
1643:     }
1644:   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1645:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1646:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1647:       its--;
1648:     }
1649:     while (its--) {
1650:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1651:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

1653:       /* update rhs: bb1 = bb - B*x */
1654:       VecScale(mat->lvec,-1.0);
1655:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1657:       /* local sweep */
1658:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);
1659:     }
1660:   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1661:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1662:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1663:       its--;
1664:     }
1665:     while (its--) {
1666:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1667:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

1669:       /* update rhs: bb1 = bb - B*x */
1670:       VecScale(mat->lvec,-1.0);
1671:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1673:       /* local sweep */
1674:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);
1675:     }
1676:   }  else if (flag & SOR_EISENSTAT) {
1677:     Vec         xx1;

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

1682:     VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1683:     VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1684:     if (!mat->diag) {
1685:       MatGetVecs(matin,&mat->diag,PETSC_NULL);
1686:       MatGetDiagonal(matin,mat->diag);
1687:     }
1688:     MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);
1689:     if (hasop) {
1690:       MatMultDiagonalBlock(matin,xx,bb1);
1691:     } else {
1692:       VecPointwiseMult(bb1,mat->diag,xx);
1693:     }
1694:     VecAYPX(bb1,(omega-2.0)/omega,bb);

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

1698:     /* local sweep */
1699:     (*mat->A->ops->sor)(mat->A,bb1,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_FORWARD_SWEEP),fshift,lits,1,xx1);
1700:     VecAXPY(xx,1.0,xx1);
1701:     VecDestroy(&xx1);
1702:   } else SETERRQ(((PetscObject)matin)->comm,PETSC_ERR_SUP,"Parallel SOR not supported");

1704:   VecDestroy(&bb1);
1705:   return(0);
1706: }

1710: PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
1711: {
1712:   MPI_Comm       comm;
1713:   PetscInt       first,local_rowsize,local_colsize;
1714:   const PetscInt *rows;
1715:   IS             crowp,growp,irowp,lrowp,lcolp,icolp;

1719:   PetscObjectGetComm((PetscObject)A,&comm);
1720:   /* make a collective version of 'rowp', this is to be tolerant of users who pass serial index sets */
1721:   ISOnComm(rowp,comm,PETSC_USE_POINTER,&crowp);
1722:   /* collect the global row permutation and invert it */
1723:   ISAllGather(crowp,&growp);
1724:   ISSetPermutation(growp);
1725:   ISDestroy(&crowp);
1726:   ISInvertPermutation(growp,PETSC_DECIDE,&irowp);
1727:   ISDestroy(&growp);
1728:   /* get the local target indices */
1729:   MatGetOwnershipRange(A,&first,PETSC_NULL);
1730:   MatGetLocalSize(A,&local_rowsize,&local_colsize);
1731:   ISGetIndices(irowp,&rows);
1732:   ISCreateGeneral(PETSC_COMM_SELF,local_rowsize,rows+first,PETSC_COPY_VALUES,&lrowp);
1733:   ISRestoreIndices(irowp,&rows);
1734:   ISDestroy(&irowp);
1735:   /* the column permutation is so much easier;
1736:      make a local version of 'colp' and invert it */
1737:   ISOnComm(colp,PETSC_COMM_SELF,PETSC_USE_POINTER,&lcolp);
1738:   ISSetPermutation(lcolp);
1739:   ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);
1740:   ISDestroy(&lcolp);
1741:   /* now we just get the submatrix */
1742:   MatGetSubMatrix_MPIAIJ_Private(A,lrowp,icolp,local_colsize,MAT_INITIAL_MATRIX,B);
1743:   /* clean up */
1744:   ISDestroy(&lrowp);
1745:   ISDestroy(&icolp);
1746:   return(0);
1747: }

1751: PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1752: {
1753:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1754:   Mat            A = mat->A,B = mat->B;
1756:   PetscReal      isend[5],irecv[5];

1759:   info->block_size     = 1.0;
1760:   MatGetInfo(A,MAT_LOCAL,info);
1761:   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1762:   isend[3] = info->memory;  isend[4] = info->mallocs;
1763:   MatGetInfo(B,MAT_LOCAL,info);
1764:   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1765:   isend[3] += info->memory;  isend[4] += info->mallocs;
1766:   if (flag == MAT_LOCAL) {
1767:     info->nz_used      = isend[0];
1768:     info->nz_allocated = isend[1];
1769:     info->nz_unneeded  = isend[2];
1770:     info->memory       = isend[3];
1771:     info->mallocs      = isend[4];
1772:   } else if (flag == MAT_GLOBAL_MAX) {
1773:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,((PetscObject)matin)->comm);
1774:     info->nz_used      = irecv[0];
1775:     info->nz_allocated = irecv[1];
1776:     info->nz_unneeded  = irecv[2];
1777:     info->memory       = irecv[3];
1778:     info->mallocs      = irecv[4];
1779:   } else if (flag == MAT_GLOBAL_SUM) {
1780:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,((PetscObject)matin)->comm);
1781:     info->nz_used      = irecv[0];
1782:     info->nz_allocated = irecv[1];
1783:     info->nz_unneeded  = irecv[2];
1784:     info->memory       = irecv[3];
1785:     info->mallocs      = irecv[4];
1786:   }
1787:   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
1788:   info->fill_ratio_needed = 0;
1789:   info->factor_mallocs    = 0;

1791:   return(0);
1792: }

1796: PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool  flg)
1797: {
1798:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1802:   switch (op) {
1803:   case MAT_NEW_NONZERO_LOCATIONS:
1804:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1805:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1806:   case MAT_KEEP_NONZERO_PATTERN:
1807:   case MAT_NEW_NONZERO_LOCATION_ERR:
1808:   case MAT_USE_INODES:
1809:   case MAT_IGNORE_ZERO_ENTRIES:
1810:     MatCheckPreallocated(A,1);
1811:     MatSetOption(a->A,op,flg);
1812:     MatSetOption(a->B,op,flg);
1813:     break;
1814:   case MAT_ROW_ORIENTED:
1815:     a->roworiented = flg;
1816:     MatSetOption(a->A,op,flg);
1817:     MatSetOption(a->B,op,flg);
1818:     break;
1819:   case MAT_NEW_DIAGONALS:
1820:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1821:     break;
1822:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1823:     a->donotstash = flg;
1824:     break;
1825:   case MAT_SPD:
1826:     A->spd_set                         = PETSC_TRUE;
1827:     A->spd                             = flg;
1828:     if (flg) {
1829:       A->symmetric                     = PETSC_TRUE;
1830:       A->structurally_symmetric        = PETSC_TRUE;
1831:       A->symmetric_set                 = PETSC_TRUE;
1832:       A->structurally_symmetric_set    = PETSC_TRUE;
1833:     }
1834:     break;
1835:   case MAT_SYMMETRIC:
1836:     MatSetOption(a->A,op,flg);
1837:     break;
1838:   case MAT_STRUCTURALLY_SYMMETRIC:
1839:     MatSetOption(a->A,op,flg);
1840:     break;
1841:   case MAT_HERMITIAN:
1842:     MatSetOption(a->A,op,flg);
1843:     break;
1844:   case MAT_SYMMETRY_ETERNAL:
1845:     MatSetOption(a->A,op,flg);
1846:     break;
1847:   default:
1848:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1849:   }
1850:   return(0);
1851: }

1855: PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1856: {
1857:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1858:   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
1860:   PetscInt       i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart;
1861:   PetscInt       nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend;
1862:   PetscInt       *cmap,*idx_p;

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

1868:   if (!mat->rowvalues && (idx || v)) {
1869:     /*
1870:         allocate enough space to hold information from the longest row.
1871:     */
1872:     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1873:     PetscInt   max = 1,tmp;
1874:     for (i=0; i<matin->rmap->n; i++) {
1875:       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1876:       if (max < tmp) { max = tmp; }
1877:     }
1878:     PetscMalloc2(max,PetscScalar,&mat->rowvalues,max,PetscInt,&mat->rowindices);
1879:   }

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

1884:   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1885:   if (!v)   {pvA = 0; pvB = 0;}
1886:   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1887:   (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1888:   (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1889:   nztot = nzA + nzB;

1891:   cmap  = mat->garray;
1892:   if (v  || idx) {
1893:     if (nztot) {
1894:       /* Sort by increasing column numbers, assuming A and B already sorted */
1895:       PetscInt imark = -1;
1896:       if (v) {
1897:         *v = v_p = mat->rowvalues;
1898:         for (i=0; i<nzB; i++) {
1899:           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1900:           else break;
1901:         }
1902:         imark = i;
1903:         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1904:         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1905:       }
1906:       if (idx) {
1907:         *idx = idx_p = mat->rowindices;
1908:         if (imark > -1) {
1909:           for (i=0; i<imark; i++) {
1910:             idx_p[i] = cmap[cworkB[i]];
1911:           }
1912:         } else {
1913:           for (i=0; i<nzB; i++) {
1914:             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1915:             else break;
1916:           }
1917:           imark = i;
1918:         }
1919:         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
1920:         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
1921:       }
1922:     } else {
1923:       if (idx) *idx = 0;
1924:       if (v)   *v   = 0;
1925:     }
1926:   }
1927:   *nz = nztot;
1928:   (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1929:   (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1930:   return(0);
1931: }

1935: PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1936: {
1937:   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;

1940:   if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
1941:   aij->getrowactive = PETSC_FALSE;
1942:   return(0);
1943: }

1947: PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1948: {
1949:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1950:   Mat_SeqAIJ     *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1952:   PetscInt       i,j,cstart = mat->cmap->rstart;
1953:   PetscReal      sum = 0.0;
1954:   MatScalar      *v;

1957:   if (aij->size == 1) {
1958:      MatNorm(aij->A,type,norm);
1959:   } else {
1960:     if (type == NORM_FROBENIUS) {
1961:       v = amat->a;
1962:       for (i=0; i<amat->nz; i++) {
1963: #if defined(PETSC_USE_COMPLEX)
1964:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1965: #else
1966:         sum += (*v)*(*v); v++;
1967: #endif
1968:       }
1969:       v = bmat->a;
1970:       for (i=0; i<bmat->nz; i++) {
1971: #if defined(PETSC_USE_COMPLEX)
1972:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1973: #else
1974:         sum += (*v)*(*v); v++;
1975: #endif
1976:       }
1977:       MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,((PetscObject)mat)->comm);
1978:       *norm = PetscSqrtReal(*norm);
1979:     } else if (type == NORM_1) { /* max column norm */
1980:       PetscReal *tmp,*tmp2;
1981:       PetscInt  *jj,*garray = aij->garray;
1982:       PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp);
1983:       PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp2);
1984:       PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));
1985:       *norm = 0.0;
1986:       v = amat->a; jj = amat->j;
1987:       for (j=0; j<amat->nz; j++) {
1988:         tmp[cstart + *jj++ ] += PetscAbsScalar(*v);  v++;
1989:       }
1990:       v = bmat->a; jj = bmat->j;
1991:       for (j=0; j<bmat->nz; j++) {
1992:         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
1993:       }
1994:       MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,((PetscObject)mat)->comm);
1995:       for (j=0; j<mat->cmap->N; j++) {
1996:         if (tmp2[j] > *norm) *norm = tmp2[j];
1997:       }
1998:       PetscFree(tmp);
1999:       PetscFree(tmp2);
2000:     } else if (type == NORM_INFINITY) { /* max row norm */
2001:       PetscReal ntemp = 0.0;
2002:       for (j=0; j<aij->A->rmap->n; j++) {
2003:         v = amat->a + amat->i[j];
2004:         sum = 0.0;
2005:         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
2006:           sum += PetscAbsScalar(*v); v++;
2007:         }
2008:         v = bmat->a + bmat->i[j];
2009:         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
2010:           sum += PetscAbsScalar(*v); v++;
2011:         }
2012:         if (sum > ntemp) ntemp = sum;
2013:       }
2014:       MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,((PetscObject)mat)->comm);
2015:     } else {
2016:       SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"No support for two norm");
2017:     }
2018:   }
2019:   return(0);
2020: }

2024: PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout)
2025: {
2026:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2027:   Mat_SeqAIJ     *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data;
2029:   PetscInt       M = A->rmap->N,N = A->cmap->N,ma,na,mb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i,*d_nnz;
2030:   PetscInt       cstart=A->cmap->rstart,ncol;
2031:   Mat            B;
2032:   MatScalar      *array;

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

2037:   ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n;
2038:   ai = Aloc->i; aj = Aloc->j;
2039:   bi = Bloc->i; bj = Bloc->j;
2040:   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
2041:     /* compute d_nnz for preallocation; o_nnz is approximated by d_nnz to avoid communication */
2042:     PetscMalloc((1+na)*sizeof(PetscInt),&d_nnz);
2043:     PetscMemzero(d_nnz,(1+na)*sizeof(PetscInt));
2044:     for (i=0; i<ai[ma]; i++){
2045:       d_nnz[aj[i]] ++;
2046:       aj[i] += cstart; /* global col index to be used by MatSetValues() */
2047:     }

2049:     MatCreate(((PetscObject)A)->comm,&B);
2050:     MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);
2051:     MatSetBlockSizes(B,A->cmap->bs,A->rmap->bs);
2052:     MatSetType(B,((PetscObject)A)->type_name);
2053:     MatMPIAIJSetPreallocation(B,0,d_nnz,0,d_nnz);
2054:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);
2055:     PetscFree(d_nnz);
2056:   } else {
2057:     B = *matout;
2058:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
2059:     for (i=0; i<ai[ma]; i++){
2060:       aj[i] += cstart; /* global col index to be used by MatSetValues() */
2061:     }
2062:   }

2064:   /* copy over the A part */
2065:   array = Aloc->a;
2066:   row = A->rmap->rstart;
2067:   for (i=0; i<ma; i++) {
2068:     ncol = ai[i+1]-ai[i];
2069:     MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);
2070:     row++; array += ncol; aj += ncol;
2071:   }
2072:   aj = Aloc->j;
2073:   for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */

2075:   /* copy over the B part */
2076:   PetscMalloc(bi[mb]*sizeof(PetscInt),&cols);
2077:   PetscMemzero(cols,bi[mb]*sizeof(PetscInt));
2078:   array = Bloc->a;
2079:   row = A->rmap->rstart;
2080:   for (i=0; i<bi[mb]; i++) {cols[i] = a->garray[bj[i]];}
2081:   cols_tmp = cols;
2082:   for (i=0; i<mb; i++) {
2083:     ncol = bi[i+1]-bi[i];
2084:     MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);
2085:     row++; array += ncol; cols_tmp += ncol;
2086:   }
2087:   PetscFree(cols);
2088: 
2089:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2090:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2091:   if (reuse == MAT_INITIAL_MATRIX || *matout != A) {
2092:     *matout = B;
2093:   } else {
2094:     MatHeaderMerge(A,B);
2095:   }
2096:   return(0);
2097: }

2101: PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
2102: {
2103:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
2104:   Mat            a = aij->A,b = aij->B;
2106:   PetscInt       s1,s2,s3;

2109:   MatGetLocalSize(mat,&s2,&s3);
2110:   if (rr) {
2111:     VecGetLocalSize(rr,&s1);
2112:     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
2113:     /* Overlap communication with computation. */
2114:     VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);
2115:   }
2116:   if (ll) {
2117:     VecGetLocalSize(ll,&s1);
2118:     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
2119:     (*b->ops->diagonalscale)(b,ll,0);
2120:   }
2121:   /* scale  the diagonal block */
2122:   (*a->ops->diagonalscale)(a,ll,rr);

2124:   if (rr) {
2125:     /* Do a scatter end and then right scale the off-diagonal block */
2126:     VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);
2127:     (*b->ops->diagonalscale)(b,0,aij->lvec);
2128:   }
2129: 
2130:   return(0);
2131: }

2135: PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
2136: {
2137:   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;

2141:   MatSetUnfactored(a->A);
2142:   return(0);
2143: }

2147: PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool  *flag)
2148: {
2149:   Mat_MPIAIJ     *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
2150:   Mat            a,b,c,d;
2151:   PetscBool      flg;

2155:   a = matA->A; b = matA->B;
2156:   c = matB->A; d = matB->B;

2158:   MatEqual(a,c,&flg);
2159:   if (flg) {
2160:     MatEqual(b,d,&flg);
2161:   }
2162:   MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,((PetscObject)A)->comm);
2163:   return(0);
2164: }

2168: PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
2169: {
2171:   Mat_MPIAIJ     *a = (Mat_MPIAIJ *)A->data;
2172:   Mat_MPIAIJ     *b = (Mat_MPIAIJ *)B->data;

2175:   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
2176:   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
2177:     /* because of the column compression in the off-processor part of the matrix a->B,
2178:        the number of columns in a->B and b->B may be different, hence we cannot call
2179:        the MatCopy() directly on the two parts. If need be, we can provide a more 
2180:        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
2181:        then copying the submatrices */
2182:     MatCopy_Basic(A,B,str);
2183:   } else {
2184:     MatCopy(a->A,b->A,str);
2185:     MatCopy(a->B,b->B,str);
2186:   }
2187:   return(0);
2188: }

2192: PetscErrorCode MatSetUp_MPIAIJ(Mat A)
2193: {

2197:    MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
2198:   return(0);
2199: }

2203: /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */
2204: static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt* nnz)
2205: {
2206:   PetscInt          i,m=Y->rmap->N;
2207:   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2208:   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2209:   const PetscInt    *xi = x->i,*yi = y->i;

2212:   /* Set the number of nonzeros in the new matrix */
2213:   for(i=0; i<m; i++) {
2214:     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
2215:     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
2216:     nnz[i] = 0;
2217:     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2218:       for (; k<nzy && yltog[yj[k]]<xltog[xj[j]]; k++) nnz[i]++; /* Catch up to X */
2219:       if (k<nzy && yltog[yj[k]]==xltog[xj[j]]) k++;             /* Skip duplicate */
2220:       nnz[i]++;
2221:     }
2222:     for (; k<nzy; k++) nnz[i]++;
2223:   }
2224:   return(0);
2225: }

2229: PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2230: {
2232:   PetscInt       i;
2233:   Mat_MPIAIJ     *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
2234:   PetscBLASInt   bnz,one=1;
2235:   Mat_SeqAIJ     *x,*y;

2238:   if (str == SAME_NONZERO_PATTERN) {
2239:     PetscScalar alpha = a;
2240:     x = (Mat_SeqAIJ *)xx->A->data;
2241:     y = (Mat_SeqAIJ *)yy->A->data;
2242:     bnz = PetscBLASIntCast(x->nz);
2243:     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2244:     x = (Mat_SeqAIJ *)xx->B->data;
2245:     y = (Mat_SeqAIJ *)yy->B->data;
2246:     bnz = PetscBLASIntCast(x->nz);
2247:     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2248:   } else if (str == SUBSET_NONZERO_PATTERN) {
2249:     MatAXPY_SeqAIJ(yy->A,a,xx->A,str);

2251:     x = (Mat_SeqAIJ *)xx->B->data;
2252:     y = (Mat_SeqAIJ *)yy->B->data;
2253:     if (y->xtoy && y->XtoY != xx->B) {
2254:       PetscFree(y->xtoy);
2255:       MatDestroy(&y->XtoY);
2256:     }
2257:     if (!y->xtoy) { /* get xtoy */
2258:       MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);
2259:       y->XtoY = xx->B;
2260:       PetscObjectReference((PetscObject)xx->B);
2261:     }
2262:     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2263:   } else {
2264:     Mat B;
2265:     PetscInt *nnz_d,*nnz_o;
2266:     PetscMalloc(yy->A->rmap->N*sizeof(PetscInt),&nnz_d);
2267:     PetscMalloc(yy->B->rmap->N*sizeof(PetscInt),&nnz_o);
2268:     MatCreate(((PetscObject)Y)->comm,&B);
2269:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2270:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2271:     MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);
2272:     MatSetType(B,MATMPIAIJ);
2273:     MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);
2274:     MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);
2275:     MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);
2276:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2277:     MatHeaderReplace(Y,B);
2278:     PetscFree(nnz_d);
2279:     PetscFree(nnz_o);
2280:   }
2281:   return(0);
2282: }

2284: extern PetscErrorCode  MatConjugate_SeqAIJ(Mat);

2288: PetscErrorCode  MatConjugate_MPIAIJ(Mat mat)
2289: {
2290: #if defined(PETSC_USE_COMPLEX)
2292:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;

2295:   MatConjugate_SeqAIJ(aij->A);
2296:   MatConjugate_SeqAIJ(aij->B);
2297: #else
2299: #endif
2300:   return(0);
2301: }

2305: PetscErrorCode MatRealPart_MPIAIJ(Mat A)
2306: {
2307:   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;

2311:   MatRealPart(a->A);
2312:   MatRealPart(a->B);
2313:   return(0);
2314: }

2318: PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
2319: {
2320:   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;

2324:   MatImaginaryPart(a->A);
2325:   MatImaginaryPart(a->B);
2326:   return(0);
2327: }

2329: #ifdef PETSC_HAVE_PBGL

2331: #include <boost/parallel/mpi/bsp_process_group.hpp>
2332: #include <boost/graph/distributed/ilu_default_graph.hpp>
2333: #include <boost/graph/distributed/ilu_0_block.hpp>
2334: #include <boost/graph/distributed/ilu_preconditioner.hpp>
2335: #include <boost/graph/distributed/petsc/interface.hpp>
2336: #include <boost/multi_array.hpp>
2337: #include <boost/parallel/distributed_property_map->hpp>

2341: /*
2342:   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
2343: */
2344: PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info)
2345: {
2346:   namespace petsc = boost::distributed::petsc;
2347: 
2348:   namespace graph_dist = boost::graph::distributed;
2349:   using boost::graph::distributed::ilu_default::process_group_type;
2350:   using boost::graph::ilu_permuted;

2352:   PetscBool       row_identity, col_identity;
2353:   PetscContainer  c;
2354:   PetscInt        m, n, M, N;
2355:   PetscErrorCode  ierr;

2358:   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu");
2359:   ISIdentity(isrow, &row_identity);
2360:   ISIdentity(iscol, &col_identity);
2361:   if (!row_identity || !col_identity) {
2362:     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU");
2363:   }

2365:   process_group_type pg;
2366:   typedef graph_dist::ilu_default::ilu_level_graph_type  lgraph_type;
2367:   lgraph_type*   lgraph_p = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg));
2368:   lgraph_type&   level_graph = *lgraph_p;
2369:   graph_dist::ilu_default::graph_type&            graph(level_graph.graph);

2371:   petsc::read_matrix(A, graph, get(boost::edge_weight, graph));
2372:   ilu_permuted(level_graph);

2374:   /* put together the new matrix */
2375:   MatCreate(((PetscObject)A)->comm, fact);
2376:   MatGetLocalSize(A, &m, &n);
2377:   MatGetSize(A, &M, &N);
2378:   MatSetSizes(fact, m, n, M, N);
2379:   MatSetBlockSizes(fact,A->rmap->bs,A->cmap->bs);
2380:   MatSetType(fact, ((PetscObject)A)->type_name);
2381:   MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);
2382:   MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);

2384:   PetscContainerCreate(((PetscObject)A)->comm, &c);
2385:   PetscContainerSetPointer(c, lgraph_p);
2386:   PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c);
2387:   PetscContainerDestroy(&c);
2388:   return(0);
2389: }

2393: PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info)
2394: {
2396:   return(0);
2397: }

2401: /*
2402:   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
2403: */
2404: PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x)
2405: {
2406:   namespace graph_dist = boost::graph::distributed;

2408:   typedef graph_dist::ilu_default::ilu_level_graph_type  lgraph_type;
2409:   lgraph_type*   lgraph_p;
2410:   PetscContainer c;

2414:   PetscObjectQuery((PetscObject) A, "graph", (PetscObject *) &c);
2415:   PetscContainerGetPointer(c, (void **) &lgraph_p);
2416:   VecCopy(b, x);

2418:   PetscScalar* array_x;
2419:   VecGetArray(x, &array_x);
2420:   PetscInt sx;
2421:   VecGetSize(x, &sx);
2422: 
2423:   PetscScalar* array_b;
2424:   VecGetArray(b, &array_b);
2425:   PetscInt sb;
2426:   VecGetSize(b, &sb);

2428:   lgraph_type&   level_graph = *lgraph_p;
2429:   graph_dist::ilu_default::graph_type&            graph(level_graph.graph);

2431:   typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type;
2432:   array_ref_type                                 ref_b(array_b, boost::extents[num_vertices(graph)]),
2433:                                                  ref_x(array_x, boost::extents[num_vertices(graph)]);

2435:   typedef boost::iterator_property_map<array_ref_type::iterator,
2436:                                 boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type>  gvector_type;
2437:   gvector_type                                   vector_b(ref_b.begin(), get(boost::vertex_index, graph)),
2438:                                                  vector_x(ref_x.begin(), get(boost::vertex_index, graph));
2439: 
2440:   ilu_set_solve(*lgraph_p, vector_b, vector_x);

2442:   return(0);
2443: }
2444: #endif

2446: typedef struct { /* used by MatGetRedundantMatrix() for reusing matredundant */
2447:   PetscInt       nzlocal,nsends,nrecvs;
2448:   PetscMPIInt    *send_rank,*recv_rank;
2449:   PetscInt       *sbuf_nz,*rbuf_nz,*sbuf_j,**rbuf_j;
2450:   PetscScalar    *sbuf_a,**rbuf_a;
2451:   PetscErrorCode (*Destroy)(Mat);
2452: } Mat_Redundant;

2456: PetscErrorCode PetscContainerDestroy_MatRedundant(void *ptr)
2457: {
2458:   PetscErrorCode       ierr;
2459:   Mat_Redundant        *redund=(Mat_Redundant*)ptr;
2460:   PetscInt             i;

2463:   PetscFree2(redund->send_rank,redund->recv_rank);
2464:   PetscFree(redund->sbuf_j);
2465:   PetscFree(redund->sbuf_a);
2466:   for (i=0; i<redund->nrecvs; i++){
2467:     PetscFree(redund->rbuf_j[i]);
2468:     PetscFree(redund->rbuf_a[i]);
2469:   }
2470:   PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);
2471:   PetscFree(redund);
2472:   return(0);
2473: }

2477: PetscErrorCode MatDestroy_MatRedundant(Mat A)
2478: {
2479:   PetscErrorCode  ierr;
2480:   PetscContainer  container;
2481:   Mat_Redundant   *redund=PETSC_NULL;

2484:   PetscObjectQuery((PetscObject)A,"Mat_Redundant",(PetscObject *)&container);
2485:   if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit");
2486:   PetscContainerGetPointer(container,(void **)&redund);
2487:   A->ops->destroy = redund->Destroy;
2488:   PetscObjectCompose((PetscObject)A,"Mat_Redundant",0);
2489:   if (A->ops->destroy) {
2490:     (*A->ops->destroy)(A);
2491:   }
2492:   return(0);
2493: }

2497: PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_sub,MatReuse reuse,Mat *matredundant)
2498: {
2499:   PetscMPIInt    rank,size;
2500:   MPI_Comm       comm=((PetscObject)mat)->comm;
2502:   PetscInt       nsends=0,nrecvs=0,i,rownz_max=0;
2503:   PetscMPIInt    *send_rank=PETSC_NULL,*recv_rank=PETSC_NULL;
2504:   PetscInt       *rowrange=mat->rmap->range;
2505:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
2506:   Mat            A=aij->A,B=aij->B,C=*matredundant;
2507:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data;
2508:   PetscScalar    *sbuf_a;
2509:   PetscInt       nzlocal=a->nz+b->nz;
2510:   PetscInt       j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB;
2511:   PetscInt       rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray,M,N;
2512:   PetscInt       *cols,ctmp,lwrite,*rptr,l,*sbuf_j;
2513:   MatScalar      *aworkA,*aworkB;
2514:   PetscScalar    *vals;
2515:   PetscMPIInt    tag1,tag2,tag3,imdex;
2516:   MPI_Request    *s_waits1=PETSC_NULL,*s_waits2=PETSC_NULL,*s_waits3=PETSC_NULL,
2517:                  *r_waits1=PETSC_NULL,*r_waits2=PETSC_NULL,*r_waits3=PETSC_NULL;
2518:   MPI_Status     recv_status,*send_status;
2519:   PetscInt       *sbuf_nz=PETSC_NULL,*rbuf_nz=PETSC_NULL,count;
2520:   PetscInt       **rbuf_j=PETSC_NULL;
2521:   PetscScalar    **rbuf_a=PETSC_NULL;
2522:   Mat_Redundant  *redund=PETSC_NULL;
2523:   PetscContainer container;

2526:   MPI_Comm_rank(comm,&rank);
2527:   MPI_Comm_size(comm,&size);

2529:   if (reuse == MAT_REUSE_MATRIX) {
2530:     MatGetSize(C,&M,&N);
2531:     if (M != N || M != mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size");
2532:     MatGetLocalSize(C,&M,&N);
2533:     if (M != N || M != mlocal_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong local size");
2534:     PetscObjectQuery((PetscObject)C,"Mat_Redundant",(PetscObject *)&container);
2535:     if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit");
2536:     PetscContainerGetPointer(container,(void **)&redund);
2537:     if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal");

2539:     nsends    = redund->nsends;
2540:     nrecvs    = redund->nrecvs;
2541:     send_rank = redund->send_rank;
2542:     recv_rank = redund->recv_rank;
2543:     sbuf_nz   = redund->sbuf_nz;
2544:     rbuf_nz   = redund->rbuf_nz;
2545:     sbuf_j    = redund->sbuf_j;
2546:     sbuf_a    = redund->sbuf_a;
2547:     rbuf_j    = redund->rbuf_j;
2548:     rbuf_a    = redund->rbuf_a;
2549:   }

2551:   if (reuse == MAT_INITIAL_MATRIX){
2552:     PetscMPIInt  subrank,subsize;
2553:     PetscInt     nleftover,np_subcomm;
2554:     /* get the destination processors' id send_rank, nsends and nrecvs */
2555:     MPI_Comm_rank(subcomm,&subrank);
2556:     MPI_Comm_size(subcomm,&subsize);
2557:     PetscMalloc2(size,PetscMPIInt,&send_rank,size,PetscMPIInt,&recv_rank);
2558:     np_subcomm = size/nsubcomm;
2559:     nleftover  = size - nsubcomm*np_subcomm;
2560:     nsends = 0; nrecvs = 0;
2561:     for (i=0; i<size; i++){ /* i=rank*/
2562:       if (subrank == i/nsubcomm && rank != i){ /* my_subrank == other's subrank */
2563:         send_rank[nsends] = i; nsends++;
2564:         recv_rank[nrecvs++] = i;
2565:       }
2566:     }
2567:     if (rank >= size - nleftover){/* this proc is a leftover processor */
2568:       i = size-nleftover-1;
2569:       j = 0;
2570:       while (j < nsubcomm - nleftover){
2571:         send_rank[nsends++] = i;
2572:         i--; j++;
2573:       }
2574:     }

2576:     if (nleftover && subsize == size/nsubcomm && subrank==subsize-1){ /* this proc recvs from leftover processors */
2577:       for (i=0; i<nleftover; i++){
2578:         recv_rank[nrecvs++] = size-nleftover+i;
2579:       }
2580:     }

2582:     /* allocate sbuf_j, sbuf_a */
2583:     i = nzlocal + rowrange[rank+1] - rowrange[rank] + 2;
2584:     PetscMalloc(i*sizeof(PetscInt),&sbuf_j);
2585:     PetscMalloc((nzlocal+1)*sizeof(PetscScalar),&sbuf_a);
2586:   } /* endof if (reuse == MAT_INITIAL_MATRIX) */

2588:   /* copy mat's local entries into the buffers */
2589:   if (reuse == MAT_INITIAL_MATRIX){
2590:     rownz_max = 0;
2591:     rptr = sbuf_j;
2592:     cols = sbuf_j + rend-rstart + 1;
2593:     vals = sbuf_a;
2594:     rptr[0] = 0;
2595:     for (i=0; i<rend-rstart; i++){
2596:       row = i + rstart;
2597:       nzA    = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i];
2598:       ncols  = nzA + nzB;
2599:       cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i];
2600:       aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i];
2601:       /* load the column indices for this row into cols */
2602:       lwrite = 0;
2603:       for (l=0; l<nzB; l++) {
2604:         if ((ctmp = bmap[cworkB[l]]) < cstart){
2605:           vals[lwrite]   = aworkB[l];
2606:           cols[lwrite++] = ctmp;
2607:         }
2608:       }
2609:       for (l=0; l<nzA; l++){
2610:         vals[lwrite]   = aworkA[l];
2611:         cols[lwrite++] = cstart + cworkA[l];
2612:       }
2613:       for (l=0; l<nzB; l++) {
2614:         if ((ctmp = bmap[cworkB[l]]) >= cend){
2615:           vals[lwrite]   = aworkB[l];
2616:           cols[lwrite++] = ctmp;
2617:         }
2618:       }
2619:       vals += ncols;
2620:       cols += ncols;
2621:       rptr[i+1] = rptr[i] + ncols;
2622:       if (rownz_max < ncols) rownz_max = ncols;
2623:     }
2624:     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);
2625:   } else { /* only copy matrix values into sbuf_a */
2626:     rptr = sbuf_j;
2627:     vals = sbuf_a;
2628:     rptr[0] = 0;
2629:     for (i=0; i<rend-rstart; i++){
2630:       row = i + rstart;
2631:       nzA    = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i];
2632:       ncols  = nzA + nzB;
2633:       cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i];
2634:       aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i];
2635:       lwrite = 0;
2636:       for (l=0; l<nzB; l++) {
2637:         if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l];
2638:       }
2639:       for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l];
2640:       for (l=0; l<nzB; l++) {
2641:         if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l];
2642:       }
2643:       vals += ncols;
2644:       rptr[i+1] = rptr[i] + ncols;
2645:     }
2646:   } /* endof if (reuse == MAT_INITIAL_MATRIX) */

2648:   /* send nzlocal to others, and recv other's nzlocal */
2649:   /*--------------------------------------------------*/
2650:   if (reuse == MAT_INITIAL_MATRIX){
2651:     PetscMalloc2(3*(nsends + nrecvs)+1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);
2652:     s_waits2 = s_waits3 + nsends;
2653:     s_waits1 = s_waits2 + nsends;
2654:     r_waits1 = s_waits1 + nsends;
2655:     r_waits2 = r_waits1 + nrecvs;
2656:     r_waits3 = r_waits2 + nrecvs;
2657:   } else {
2658:     PetscMalloc2(nsends + nrecvs +1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);
2659:     r_waits3 = s_waits3 + nsends;
2660:   }

2662:   PetscObjectGetNewTag((PetscObject)mat,&tag3);
2663:   if (reuse == MAT_INITIAL_MATRIX){
2664:     /* get new tags to keep the communication clean */
2665:     PetscObjectGetNewTag((PetscObject)mat,&tag1);
2666:     PetscObjectGetNewTag((PetscObject)mat,&tag2);
2667:     PetscMalloc4(nsends,PetscInt,&sbuf_nz,nrecvs,PetscInt,&rbuf_nz,nrecvs,PetscInt*,&rbuf_j,nrecvs,PetscScalar*,&rbuf_a);

2669:     /* post receives of other's nzlocal */
2670:     for (i=0; i<nrecvs; i++){
2671:       MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);
2672:     }
2673:     /* send nzlocal to others */
2674:     for (i=0; i<nsends; i++){
2675:       sbuf_nz[i] = nzlocal;
2676:       MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);
2677:     }
2678:     /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */
2679:     count = nrecvs;
2680:     while (count) {
2681:       MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);
2682:       recv_rank[imdex] = recv_status.MPI_SOURCE;
2683:       /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */
2684:       PetscMalloc((rbuf_nz[imdex]+1)*sizeof(PetscScalar),&rbuf_a[imdex]);

2686:       i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */
2687:       rbuf_nz[imdex] += i + 2;
2688:       PetscMalloc(rbuf_nz[imdex]*sizeof(PetscInt),&rbuf_j[imdex]);
2689:       MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);
2690:       count--;
2691:     }
2692:     /* wait on sends of nzlocal */
2693:     if (nsends) {MPI_Waitall(nsends,s_waits1,send_status);}
2694:     /* send mat->i,j to others, and recv from other's */
2695:     /*------------------------------------------------*/
2696:     for (i=0; i<nsends; i++){
2697:       j = nzlocal + rowrange[rank+1] - rowrange[rank] + 1;
2698:       MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);
2699:     }
2700:     /* wait on receives of mat->i,j */
2701:     /*------------------------------*/
2702:     count = nrecvs;
2703:     while (count) {
2704:       MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);
2705:       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);
2706:       count--;
2707:     }
2708:     /* wait on sends of mat->i,j */
2709:     /*---------------------------*/
2710:     if (nsends) {
2711:       MPI_Waitall(nsends,s_waits2,send_status);
2712:     }
2713:   } /* endof if (reuse == MAT_INITIAL_MATRIX) */

2715:   /* post receives, send and receive mat->a */
2716:   /*----------------------------------------*/
2717:   for (imdex=0; imdex<nrecvs; imdex++) {
2718:     MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);
2719:   }
2720:   for (i=0; i<nsends; i++){
2721:     MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);
2722:   }
2723:   count = nrecvs;
2724:   while (count) {
2725:     MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);
2726:     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);
2727:     count--;
2728:   }
2729:   if (nsends) {
2730:     MPI_Waitall(nsends,s_waits3,send_status);
2731:   }

2733:   PetscFree2(s_waits3,send_status);

2735:   /* create redundant matrix */
2736:   /*-------------------------*/
2737:   if (reuse == MAT_INITIAL_MATRIX){
2738:     /* compute rownz_max for preallocation */
2739:     for (imdex=0; imdex<nrecvs; imdex++){
2740:       j = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]];
2741:       rptr = rbuf_j[imdex];
2742:       for (i=0; i<j; i++){
2743:         ncols = rptr[i+1] - rptr[i];
2744:         if (rownz_max < ncols) rownz_max = ncols;
2745:       }
2746:     }

2748:     MatCreate(subcomm,&C);
2749:     MatSetSizes(C,mlocal_sub,mlocal_sub,PETSC_DECIDE,PETSC_DECIDE);
2750:     MatSetBlockSizes(C,mat->rmap->bs,mat->cmap->bs);
2751:     MatSetFromOptions(C);
2752:     MatSeqAIJSetPreallocation(C,rownz_max,PETSC_NULL);
2753:     MatMPIAIJSetPreallocation(C,rownz_max,PETSC_NULL,rownz_max,PETSC_NULL);
2754:   } else {
2755:     C = *matredundant;
2756:   }

2758:   /* insert local matrix entries */
2759:   rptr = sbuf_j;
2760:   cols = sbuf_j + rend-rstart + 1;
2761:   vals = sbuf_a;
2762:   for (i=0; i<rend-rstart; i++){
2763:     row   = i + rstart;
2764:     ncols = rptr[i+1] - rptr[i];
2765:     MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);
2766:     vals += ncols;
2767:     cols += ncols;
2768:   }
2769:   /* insert received matrix entries */
2770:   for (imdex=0; imdex<nrecvs; imdex++){
2771:     rstart = rowrange[recv_rank[imdex]];
2772:     rend   = rowrange[recv_rank[imdex]+1];
2773:     rptr = rbuf_j[imdex];
2774:     cols = rbuf_j[imdex] + rend-rstart + 1;
2775:     vals = rbuf_a[imdex];
2776:     for (i=0; i<rend-rstart; i++){
2777:       row   = i + rstart;
2778:       ncols = rptr[i+1] - rptr[i];
2779:       MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);
2780:       vals += ncols;
2781:       cols += ncols;
2782:     }
2783:   }
2784:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2785:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
2786:   MatGetSize(C,&M,&N);
2787:   if (M != mat->rmap->N || N != mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"redundant mat size %d != input mat size %d",M,mat->rmap->N);
2788:   if (reuse == MAT_INITIAL_MATRIX) {
2789:     PetscContainer container;
2790:     *matredundant = C;
2791:     /* create a supporting struct and attach it to C for reuse */
2792:     PetscNewLog(C,Mat_Redundant,&redund);
2793:     PetscContainerCreate(PETSC_COMM_SELF,&container);
2794:     PetscContainerSetPointer(container,redund);
2795:     PetscContainerSetUserDestroy(container,PetscContainerDestroy_MatRedundant);
2796:     PetscObjectCompose((PetscObject)C,"Mat_Redundant",(PetscObject)container);
2797:     PetscContainerDestroy(&container);

2799:     redund->nzlocal = nzlocal;
2800:     redund->nsends  = nsends;
2801:     redund->nrecvs  = nrecvs;
2802:     redund->send_rank = send_rank;
2803:     redund->recv_rank = recv_rank;
2804:     redund->sbuf_nz = sbuf_nz;
2805:     redund->rbuf_nz = rbuf_nz;
2806:     redund->sbuf_j  = sbuf_j;
2807:     redund->sbuf_a  = sbuf_a;
2808:     redund->rbuf_j  = rbuf_j;
2809:     redund->rbuf_a  = rbuf_a;

2811:     redund->Destroy = C->ops->destroy;
2812:     C->ops->destroy = MatDestroy_MatRedundant;
2813:   }
2814:   return(0);
2815: }

2819: PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2820: {
2821:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2823:   PetscInt       i,*idxb = 0;
2824:   PetscScalar    *va,*vb;
2825:   Vec            vtmp;

2828:   MatGetRowMaxAbs(a->A,v,idx);
2829:   VecGetArray(v,&va);
2830:   if (idx) {
2831:     for (i=0; i<A->rmap->n; i++) {
2832:       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2833:     }
2834:   }

2836:   VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);
2837:   if (idx) {
2838:     PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);
2839:   }
2840:   MatGetRowMaxAbs(a->B,vtmp,idxb);
2841:   VecGetArray(vtmp,&vb);

2843:   for (i=0; i<A->rmap->n; i++){
2844:     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
2845:       va[i] = vb[i];
2846:       if (idx) idx[i] = a->garray[idxb[i]];
2847:     }
2848:   }

2850:   VecRestoreArray(v,&va);
2851:   VecRestoreArray(vtmp,&vb);
2852:   PetscFree(idxb);
2853:   VecDestroy(&vtmp);
2854:   return(0);
2855: }

2859: PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2860: {
2861:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2863:   PetscInt       i,*idxb = 0;
2864:   PetscScalar    *va,*vb;
2865:   Vec            vtmp;

2868:   MatGetRowMinAbs(a->A,v,idx);
2869:   VecGetArray(v,&va);
2870:   if (idx) {
2871:     for (i=0; i<A->cmap->n; i++) {
2872:       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2873:     }
2874:   }

2876:   VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);
2877:   if (idx) {
2878:     PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);
2879:   }
2880:   MatGetRowMinAbs(a->B,vtmp,idxb);
2881:   VecGetArray(vtmp,&vb);

2883:   for (i=0; i<A->rmap->n; i++){
2884:     if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) {
2885:       va[i] = vb[i];
2886:       if (idx) idx[i] = a->garray[idxb[i]];
2887:     }
2888:   }

2890:   VecRestoreArray(v,&va);
2891:   VecRestoreArray(vtmp,&vb);
2892:   PetscFree(idxb);
2893:   VecDestroy(&vtmp);
2894:   return(0);
2895: }

2899: PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2900: {
2901:   Mat_MPIAIJ    *mat    = (Mat_MPIAIJ *) A->data;
2902:   PetscInt       n      = A->rmap->n;
2903:   PetscInt       cstart = A->cmap->rstart;
2904:   PetscInt      *cmap   = mat->garray;
2905:   PetscInt      *diagIdx, *offdiagIdx;
2906:   Vec            diagV, offdiagV;
2907:   PetscScalar   *a, *diagA, *offdiagA;
2908:   PetscInt       r;

2912:   PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);
2913:   VecCreateSeq(((PetscObject)A)->comm, n, &diagV);
2914:   VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);
2915:   MatGetRowMin(mat->A, diagV,    diagIdx);
2916:   MatGetRowMin(mat->B, offdiagV, offdiagIdx);
2917:   VecGetArray(v,        &a);
2918:   VecGetArray(diagV,    &diagA);
2919:   VecGetArray(offdiagV, &offdiagA);
2920:   for(r = 0; r < n; ++r) {
2921:     if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) {
2922:       a[r]   = diagA[r];
2923:       idx[r] = cstart + diagIdx[r];
2924:     } else {
2925:       a[r]   = offdiagA[r];
2926:       idx[r] = cmap[offdiagIdx[r]];
2927:     }
2928:   }
2929:   VecRestoreArray(v,        &a);
2930:   VecRestoreArray(diagV,    &diagA);
2931:   VecRestoreArray(offdiagV, &offdiagA);
2932:   VecDestroy(&diagV);
2933:   VecDestroy(&offdiagV);
2934:   PetscFree2(diagIdx, offdiagIdx);
2935:   return(0);
2936: }

2940: PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2941: {
2942:   Mat_MPIAIJ    *mat    = (Mat_MPIAIJ *) A->data;
2943:   PetscInt       n      = A->rmap->n;
2944:   PetscInt       cstart = A->cmap->rstart;
2945:   PetscInt      *cmap   = mat->garray;
2946:   PetscInt      *diagIdx, *offdiagIdx;
2947:   Vec            diagV, offdiagV;
2948:   PetscScalar   *a, *diagA, *offdiagA;
2949:   PetscInt       r;

2953:   PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);
2954:   VecCreateSeq(((PetscObject)A)->comm, n, &diagV);
2955:   VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);
2956:   MatGetRowMax(mat->A, diagV,    diagIdx);
2957:   MatGetRowMax(mat->B, offdiagV, offdiagIdx);
2958:   VecGetArray(v,        &a);
2959:   VecGetArray(diagV,    &diagA);
2960:   VecGetArray(offdiagV, &offdiagA);
2961:   for(r = 0; r < n; ++r) {
2962:     if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) {
2963:       a[r]   = diagA[r];
2964:       idx[r] = cstart + diagIdx[r];
2965:     } else {
2966:       a[r]   = offdiagA[r];
2967:       idx[r] = cmap[offdiagIdx[r]];
2968:     }
2969:   }
2970:   VecRestoreArray(v,        &a);
2971:   VecRestoreArray(diagV,    &diagA);
2972:   VecRestoreArray(offdiagV, &offdiagA);
2973:   VecDestroy(&diagV);
2974:   VecDestroy(&offdiagV);
2975:   PetscFree2(diagIdx, offdiagIdx);
2976:   return(0);
2977: }

2981: PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat)
2982: {
2984:   Mat            *dummy;

2987:   MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);
2988:   *newmat = *dummy;
2989:   PetscFree(dummy);
2990:   return(0);
2991: }

2993: extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);

2997: PetscErrorCode  MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values)
2998: {
2999:   Mat_MPIAIJ    *a = (Mat_MPIAIJ*) A->data;

3003:   MatInvertBlockDiagonal(a->A,values);
3004:   return(0);
3005: }


3008: /* -------------------------------------------------------------------*/
3009: static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
3010:        MatGetRow_MPIAIJ,
3011:        MatRestoreRow_MPIAIJ,
3012:        MatMult_MPIAIJ,
3013: /* 4*/ MatMultAdd_MPIAIJ,
3014:        MatMultTranspose_MPIAIJ,
3015:        MatMultTransposeAdd_MPIAIJ,
3016: #ifdef PETSC_HAVE_PBGL
3017:        MatSolve_MPIAIJ,
3018: #else
3019:        0,
3020: #endif
3021:        0,
3022:        0,
3023: /*10*/ 0,
3024:        0,
3025:        0,
3026:        MatSOR_MPIAIJ,
3027:        MatTranspose_MPIAIJ,
3028: /*15*/ MatGetInfo_MPIAIJ,
3029:        MatEqual_MPIAIJ,
3030:        MatGetDiagonal_MPIAIJ,
3031:        MatDiagonalScale_MPIAIJ,
3032:        MatNorm_MPIAIJ,
3033: /*20*/ MatAssemblyBegin_MPIAIJ,
3034:        MatAssemblyEnd_MPIAIJ,
3035:        MatSetOption_MPIAIJ,
3036:        MatZeroEntries_MPIAIJ,
3037: /*24*/ MatZeroRows_MPIAIJ,
3038:        0,
3039: #ifdef PETSC_HAVE_PBGL
3040:        0,
3041: #else
3042:        0,
3043: #endif
3044:        0,
3045:        0,
3046: /*29*/ MatSetUp_MPIAIJ,
3047: #ifdef PETSC_HAVE_PBGL
3048:        0,
3049: #else
3050:        0,
3051: #endif
3052:        0,
3053:        0,
3054:        0,
3055: /*34*/ MatDuplicate_MPIAIJ,
3056:        0,
3057:        0,
3058:        0,
3059:        0,
3060: /*39*/ MatAXPY_MPIAIJ,
3061:        MatGetSubMatrices_MPIAIJ,
3062:        MatIncreaseOverlap_MPIAIJ,
3063:        MatGetValues_MPIAIJ,
3064:        MatCopy_MPIAIJ,
3065: /*44*/ MatGetRowMax_MPIAIJ,
3066:        MatScale_MPIAIJ,
3067:        0,
3068:        0,
3069:        MatZeroRowsColumns_MPIAIJ,
3070: /*49*/ 0,
3071:        0,
3072:        0,
3073:        0,
3074:        0,
3075: /*54*/ MatFDColoringCreate_MPIAIJ,
3076:        0,
3077:        MatSetUnfactored_MPIAIJ,
3078:        0, /* MatPermute_MPIAIJ, impl currently broken */
3079:        0,
3080: /*59*/ MatGetSubMatrix_MPIAIJ,
3081:        MatDestroy_MPIAIJ,
3082:        MatView_MPIAIJ,
3083:        0,
3084:        0,
3085: /*64*/ 0,
3086:        0,
3087:        0,
3088:        0,
3089:        0,
3090: /*69*/ MatGetRowMaxAbs_MPIAIJ,
3091:        MatGetRowMinAbs_MPIAIJ,
3092:        0,
3093:        MatSetColoring_MPIAIJ,
3094: #if defined(PETSC_HAVE_ADIC)
3095:        MatSetValuesAdic_MPIAIJ,
3096: #else
3097:        0,
3098: #endif
3099:        MatSetValuesAdifor_MPIAIJ,
3100: /*75*/ MatFDColoringApply_AIJ,
3101:        0,
3102:        0,
3103:        0,
3104:        MatFindZeroDiagonals_MPIAIJ,
3105: /*80*/ 0,
3106:        0,
3107:        0,
3108: /*83*/ MatLoad_MPIAIJ,
3109:        0,
3110:        0,
3111:        0,
3112:        0,
3113:        0,
3114: /*89*/ MatMatMult_MPIAIJ_MPIAIJ,
3115:        MatMatMultSymbolic_MPIAIJ_MPIAIJ,
3116:        MatMatMultNumeric_MPIAIJ_MPIAIJ,
3117:        MatPtAP_Basic,
3118:        MatPtAPSymbolic_MPIAIJ,
3119: /*94*/ MatPtAPNumeric_MPIAIJ,
3120:        0,
3121:        0,
3122:        0,
3123:        0,
3124: /*99*/ 0,
3125:        MatPtAPSymbolic_MPIAIJ_MPIAIJ,
3126:        MatPtAPNumeric_MPIAIJ_MPIAIJ,
3127:        MatConjugate_MPIAIJ,
3128:        0,
3129: /*104*/MatSetValuesRow_MPIAIJ,
3130:        MatRealPart_MPIAIJ,
3131:        MatImaginaryPart_MPIAIJ,
3132:        0,
3133:        0,
3134: /*109*/0,
3135:        MatGetRedundantMatrix_MPIAIJ,
3136:        MatGetRowMin_MPIAIJ,
3137:        0,
3138:        0,
3139: /*114*/MatGetSeqNonzeroStructure_MPIAIJ,
3140:        0,
3141:        0,
3142:        0,
3143:        0,
3144: /*119*/0,
3145:        0,
3146:        0,
3147:        0,
3148:        MatGetMultiProcBlock_MPIAIJ,
3149: /*124*/MatFindNonzeroRows_MPIAIJ,
3150:        MatGetColumnNorms_MPIAIJ,
3151:        MatInvertBlockDiagonal_MPIAIJ,
3152:        0,
3153:        MatGetSubMatricesParallel_MPIAIJ,
3154: /*129*/0,
3155:        MatTransposeMatMult_MPIAIJ_MPIAIJ,
3156:        MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ,
3157:        MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ,
3158:        0,
3159: /*134*/0,
3160:        0,
3161:        0,
3162:        0,
3163:        0
3164: };

3166: /* ----------------------------------------------------------------------------------------*/

3168: EXTERN_C_BEGIN
3171: PetscErrorCode  MatStoreValues_MPIAIJ(Mat mat)
3172: {
3173:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;

3177:   MatStoreValues(aij->A);
3178:   MatStoreValues(aij->B);
3179:   return(0);
3180: }
3181: EXTERN_C_END

3183: EXTERN_C_BEGIN
3186: PetscErrorCode  MatRetrieveValues_MPIAIJ(Mat mat)
3187: {
3188:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;

3192:   MatRetrieveValues(aij->A);
3193:   MatRetrieveValues(aij->B);
3194:   return(0);
3195: }
3196: EXTERN_C_END

3198: EXTERN_C_BEGIN
3201: PetscErrorCode  MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3202: {
3203:   Mat_MPIAIJ     *b;
3205:   PetscInt       i;
3206:   PetscBool      d_realalloc = PETSC_FALSE,o_realalloc = PETSC_FALSE;

3209:   if (d_nz >= 0 || d_nnz) d_realalloc = PETSC_TRUE;
3210:   if (o_nz >= 0 || o_nnz) o_realalloc = PETSC_TRUE;
3211:   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
3212:   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
3213:   if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz);
3214:   if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz);

3216:   PetscLayoutSetUp(B->rmap);
3217:   PetscLayoutSetUp(B->cmap);
3218:   if (d_nnz) {
3219:     for (i=0; i<B->rmap->n; i++) {
3220:       if (d_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than 0: local row %D value %D",i,d_nnz[i]);
3221:     }
3222:   }
3223:   if (o_nnz) {
3224:     for (i=0; i<B->rmap->n; i++) {
3225:       if (o_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than 0: local row %D value %D",i,o_nnz[i]);
3226:     }
3227:   }
3228:   b = (Mat_MPIAIJ*)B->data;

3230:   if (!B->preallocated) {
3231:     /* Explicitly create 2 MATSEQAIJ matrices. */
3232:     MatCreate(PETSC_COMM_SELF,&b->A);
3233:     MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
3234:     MatSetBlockSizes(b->A,B->rmap->bs,B->cmap->bs);
3235:     MatSetType(b->A,MATSEQAIJ);
3236:     PetscLogObjectParent(B,b->A);
3237:     MatCreate(PETSC_COMM_SELF,&b->B);
3238:     MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);
3239:     MatSetBlockSizes(b->B,B->rmap->bs,B->cmap->bs);
3240:     MatSetType(b->B,MATSEQAIJ);
3241:     PetscLogObjectParent(B,b->B);
3242:   }

3244:   MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);
3245:   MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);
3246:   /* Do not error if the user did not give real preallocation information. Ugly because this would overwrite a previous user call to MatSetOption(). */
3247:   if (!d_realalloc) {MatSetOption(b->A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);}
3248:   if (!o_realalloc) {MatSetOption(b->B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);}
3249:   B->preallocated = PETSC_TRUE;
3250:   return(0);
3251: }
3252: EXTERN_C_END

3256: PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
3257: {
3258:   Mat            mat;
3259:   Mat_MPIAIJ     *a,*oldmat = (Mat_MPIAIJ*)matin->data;

3263:   *newmat       = 0;
3264:   MatCreate(((PetscObject)matin)->comm,&mat);
3265:   MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);
3266:   MatSetBlockSizes(mat,matin->rmap->bs,matin->cmap->bs);
3267:   MatSetType(mat,((PetscObject)matin)->type_name);
3268:   PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));
3269:   a    = (Mat_MPIAIJ*)mat->data;
3270: 
3271:   mat->factortype    = matin->factortype;
3272:   mat->rmap->bs      = matin->rmap->bs;
3273:   mat->cmap->bs      = matin->cmap->bs;
3274:   mat->assembled    = PETSC_TRUE;
3275:   mat->insertmode   = NOT_SET_VALUES;
3276:   mat->preallocated = PETSC_TRUE;

3278:   a->size           = oldmat->size;
3279:   a->rank           = oldmat->rank;
3280:   a->donotstash     = oldmat->donotstash;
3281:   a->roworiented    = oldmat->roworiented;
3282:   a->rowindices     = 0;
3283:   a->rowvalues      = 0;
3284:   a->getrowactive   = PETSC_FALSE;

3286:   PetscLayoutReference(matin->rmap,&mat->rmap);
3287:   PetscLayoutReference(matin->cmap,&mat->cmap);

3289:   if (oldmat->colmap) {
3290: #if defined (PETSC_USE_CTABLE)
3291:     PetscTableCreateCopy(oldmat->colmap,&a->colmap);
3292: #else
3293:     PetscMalloc((mat->cmap->N)*sizeof(PetscInt),&a->colmap);
3294:     PetscLogObjectMemory(mat,(mat->cmap->N)*sizeof(PetscInt));
3295:     PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));
3296: #endif
3297:   } else a->colmap = 0;
3298:   if (oldmat->garray) {
3299:     PetscInt len;
3300:     len  = oldmat->B->cmap->n;
3301:     PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);
3302:     PetscLogObjectMemory(mat,len*sizeof(PetscInt));
3303:     if (len) { PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt)); }
3304:   } else a->garray = 0;
3305: 
3306:   VecDuplicate(oldmat->lvec,&a->lvec);
3307:   PetscLogObjectParent(mat,a->lvec);
3308:   VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
3309:   PetscLogObjectParent(mat,a->Mvctx);
3310:   MatDuplicate(oldmat->A,cpvalues,&a->A);
3311:   PetscLogObjectParent(mat,a->A);
3312:   MatDuplicate(oldmat->B,cpvalues,&a->B);
3313:   PetscLogObjectParent(mat,a->B);
3314:   PetscFListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);
3315:   *newmat = mat;
3316:   return(0);
3317: }



3323: PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer)
3324: {
3325:   PetscScalar    *vals,*svals;
3326:   MPI_Comm       comm = ((PetscObject)viewer)->comm;
3328:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
3329:   PetscInt       i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols;
3330:   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols;
3331:   PetscInt       *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols;
3332:   PetscInt       cend,cstart,n,*rowners,sizesset=1;
3333:   int            fd;

3336:   MPI_Comm_size(comm,&size);
3337:   MPI_Comm_rank(comm,&rank);
3338:   if (!rank) {
3339:     PetscViewerBinaryGetDescriptor(viewer,&fd);
3340:     PetscBinaryRead(fd,(char *)header,4,PETSC_INT);
3341:     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
3342:   }

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

3346:   MPI_Bcast(header+1,3,MPIU_INT,0,comm);
3347:   M = header[1]; N = header[2];
3348:   /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */
3349:   if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M;
3350:   if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N;
3351: 
3352:   /* If global sizes are set, check if they are consistent with that given in the file */
3353:   if (sizesset) {
3354:     MatGetSize(newMat,&grows,&gcols);
3355:   }
3356:   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);
3357:   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);

3359:   /* determine ownership of all rows */
3360:   if (newMat->rmap->n < 0 ) m    = M/size + ((M % size) > rank); /* PETSC_DECIDE */
3361:   else m = newMat->rmap->n; /* Set by user */
3362: 
3363:   PetscMalloc((size+1)*sizeof(PetscInt),&rowners);
3364:   MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);

3366:   /* First process needs enough room for process with most rows */
3367:   if (!rank) {
3368:     mmax = rowners[1];
3369:     for(i=2; i<=size; i++) {
3370:       mmax = PetscMax(mmax, rowners[i]);
3371:     }
3372:   } else mmax = m;

3374:   rowners[0] = 0;
3375:   for (i=2; i<=size; i++) {
3376:     rowners[i] += rowners[i-1];
3377:   }
3378:   rstart = rowners[rank];
3379:   rend   = rowners[rank+1];

3381:   /* distribute row lengths to all processors */
3382:   PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);
3383:   if (!rank) {
3384:     PetscBinaryRead(fd,ourlens,m,PETSC_INT);
3385:     PetscMalloc(mmax*sizeof(PetscInt),&rowlengths);
3386:     PetscMalloc(size*sizeof(PetscInt),&procsnz);
3387:     PetscMemzero(procsnz,size*sizeof(PetscInt));
3388:     for (j=0; j<m; j++) {
3389:       procsnz[0] += ourlens[j];
3390:     }
3391:     for (i=1; i<size; i++) {
3392:       PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);
3393:       /* calculate the number of nonzeros on each processor */
3394:       for (j=0; j<rowners[i+1]-rowners[i]; j++) {
3395:         procsnz[i] += rowlengths[j];
3396:       }
3397:       MPIULong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);
3398:     }
3399:     PetscFree(rowlengths);
3400:   } else {
3401:     MPIULong_Recv(ourlens,m,MPIU_INT,0,tag,comm);
3402:   }

3404:   if (!rank) {
3405:     /* determine max buffer needed and allocate it */
3406:     maxnz = 0;
3407:     for (i=0; i<size; i++) {
3408:       maxnz = PetscMax(maxnz,procsnz[i]);
3409:     }
3410:     PetscMalloc(maxnz*sizeof(PetscInt),&cols);

3412:     /* read in my part of the matrix column indices  */
3413:     nz   = procsnz[0];
3414:     PetscMalloc(nz*sizeof(PetscInt),&mycols);
3415:     PetscBinaryRead(fd,mycols,nz,PETSC_INT);

3417:     /* read in every one elses and ship off */
3418:     for (i=1; i<size; i++) {
3419:       nz     = procsnz[i];
3420:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
3421:       MPIULong_Send(cols,nz,MPIU_INT,i,tag,comm);
3422:     }
3423:     PetscFree(cols);
3424:   } else {
3425:     /* determine buffer space needed for message */
3426:     nz = 0;
3427:     for (i=0; i<m; i++) {
3428:       nz += ourlens[i];
3429:     }
3430:     PetscMalloc(nz*sizeof(PetscInt),&mycols);

3432:     /* receive message of column indices*/
3433:     MPIULong_Recv(mycols,nz,MPIU_INT,0,tag,comm);
3434:   }

3436:   /* determine column ownership if matrix is not square */
3437:   if (N != M) {
3438:     if (newMat->cmap->n < 0) n      = N/size + ((N % size) > rank);
3439:     else n = newMat->cmap->n;
3440:     MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);
3441:     cstart = cend - n;
3442:   } else {
3443:     cstart = rstart;
3444:     cend   = rend;
3445:     n      = cend - cstart;
3446:   }

3448:   /* loop over local rows, determining number of off diagonal entries */
3449:   PetscMemzero(offlens,m*sizeof(PetscInt));
3450:   jj = 0;
3451:   for (i=0; i<m; i++) {
3452:     for (j=0; j<ourlens[i]; j++) {
3453:       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
3454:       jj++;
3455:     }
3456:   }

3458:   for (i=0; i<m; i++) {
3459:     ourlens[i] -= offlens[i];
3460:   }
3461:   if (!sizesset) {
3462:     MatSetSizes(newMat,m,n,M,N);
3463:   }
3464:   MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);

3466:   for (i=0; i<m; i++) {
3467:     ourlens[i] += offlens[i];
3468:   }

3470:   if (!rank) {
3471:     PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);

3473:     /* read in my part of the matrix numerical values  */
3474:     nz   = procsnz[0];
3475:     PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3476: 
3477:     /* insert into matrix */
3478:     jj      = rstart;
3479:     smycols = mycols;
3480:     svals   = vals;
3481:     for (i=0; i<m; i++) {
3482:       MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
3483:       smycols += ourlens[i];
3484:       svals   += ourlens[i];
3485:       jj++;
3486:     }

3488:     /* read in other processors and ship out */
3489:     for (i=1; i<size; i++) {
3490:       nz     = procsnz[i];
3491:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3492:       MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);
3493:     }
3494:     PetscFree(procsnz);
3495:   } else {
3496:     /* receive numeric values */
3497:     PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);

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

3502:     /* insert into matrix */
3503:     jj      = rstart;
3504:     smycols = mycols;
3505:     svals   = vals;
3506:     for (i=0; i<m; i++) {
3507:       MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
3508:       smycols += ourlens[i];
3509:       svals   += ourlens[i];
3510:       jj++;
3511:     }
3512:   }
3513:   PetscFree2(ourlens,offlens);
3514:   PetscFree(vals);
3515:   PetscFree(mycols);
3516:   PetscFree(rowners);

3518:   MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);
3519:   MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);
3520:   return(0);
3521: }

3525: PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
3526: {
3528:   IS             iscol_local;
3529:   PetscInt       csize;

3532:   ISGetLocalSize(iscol,&csize);
3533:   if (call == MAT_REUSE_MATRIX) {
3534:     PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);
3535:     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3536:   } else {
3537:     PetscInt cbs;
3538:     ISGetBlockSize(iscol,&cbs);
3539:     ISAllGather(iscol,&iscol_local);
3540:     ISSetBlockSize(iscol_local,cbs);
3541:   }
3542:   MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);
3543:   if (call == MAT_INITIAL_MATRIX) {
3544:     PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);
3545:     ISDestroy(&iscol_local);
3546:   }
3547:   return(0);
3548: }

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

3558:   Note: This requires a sequential iscol with all indices.
3559: */
3560: PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
3561: {
3563:   PetscMPIInt    rank,size;
3564:   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs;
3565:   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol;
3566:   PetscBool      allcolumns, colflag;
3567:   Mat            M,Mreuse;
3568:   MatScalar      *vwork,*aa;
3569:   MPI_Comm       comm = ((PetscObject)mat)->comm;
3570:   Mat_SeqAIJ     *aij;


3574:   MPI_Comm_rank(comm,&rank);
3575:   MPI_Comm_size(comm,&size);

3577:   ISIdentity(iscol,&colflag);
3578:   ISGetLocalSize(iscol,&ncol);
3579:   if (colflag && ncol == mat->cmap->N){
3580:     allcolumns = PETSC_TRUE;
3581:   } else {
3582:     allcolumns = PETSC_FALSE;
3583:   }
3584:   if (call ==  MAT_REUSE_MATRIX) {
3585:     PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);
3586:     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3587:     MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&allcolumns,&Mreuse);
3588:   } else {
3589:     MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&allcolumns,&Mreuse);
3590:   }

3592:   /* 
3593:       m - number of local rows
3594:       n - number of columns (same on all processors)
3595:       rstart - first row in new global matrix generated
3596:   */
3597:   MatGetSize(Mreuse,&m,&n);
3598:   MatGetBlockSizes(Mreuse,&bs,&cbs);
3599:   if (call == MAT_INITIAL_MATRIX) {
3600:     aij = (Mat_SeqAIJ*)(Mreuse)->data;
3601:     ii  = aij->i;
3602:     jj  = aij->j;

3604:     /*
3605:         Determine the number of non-zeros in the diagonal and off-diagonal 
3606:         portions of the matrix in order to do correct preallocation
3607:     */

3609:     /* first get start and end of "diagonal" columns */
3610:     if (csize == PETSC_DECIDE) {
3611:       ISGetSize(isrow,&mglobal);
3612:       if (mglobal == n) { /* square matrix */
3613:         nlocal = m;
3614:       } else {
3615:         nlocal = n/size + ((n % size) > rank);
3616:       }
3617:     } else {
3618:       nlocal = csize;
3619:     }
3620:     MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
3621:     rstart = rend - nlocal;
3622:     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);

3624:     /* next, compute all the lengths */
3625:     PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);
3626:     olens = dlens + m;
3627:     for (i=0; i<m; i++) {
3628:       jend = ii[i+1] - ii[i];
3629:       olen = 0;
3630:       dlen = 0;
3631:       for (j=0; j<jend; j++) {
3632:         if (*jj < rstart || *jj >= rend) olen++;
3633:         else dlen++;
3634:         jj++;
3635:       }
3636:       olens[i] = olen;
3637:       dlens[i] = dlen;
3638:     }
3639:     MatCreate(comm,&M);
3640:     MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);
3641:     MatSetBlockSizes(M,bs,cbs);
3642:     MatSetType(M,((PetscObject)mat)->type_name);
3643:     MatMPIAIJSetPreallocation(M,0,dlens,0,olens);
3644:     PetscFree(dlens);
3645:   } else {
3646:     PetscInt ml,nl;

3648:     M = *newmat;
3649:     MatGetLocalSize(M,&ml,&nl);
3650:     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
3651:     MatZeroEntries(M);
3652:     /*
3653:          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
3654:        rather than the slower MatSetValues().
3655:     */
3656:     M->was_assembled = PETSC_TRUE;
3657:     M->assembled     = PETSC_FALSE;
3658:   }
3659:   MatGetOwnershipRange(M,&rstart,&rend);
3660:   aij = (Mat_SeqAIJ*)(Mreuse)->data;
3661:   ii  = aij->i;
3662:   jj  = aij->j;
3663:   aa  = aij->a;
3664:   for (i=0; i<m; i++) {
3665:     row   = rstart + i;
3666:     nz    = ii[i+1] - ii[i];
3667:     cwork = jj;     jj += nz;
3668:     vwork = aa;     aa += nz;
3669:     MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);
3670:   }

3672:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
3673:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
3674:   *newmat = M;

3676:   /* save submatrix used in processor for next request */
3677:   if (call ==  MAT_INITIAL_MATRIX) {
3678:     PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);
3679:     MatDestroy(&Mreuse);
3680:   }

3682:   return(0);
3683: }

3685: EXTERN_C_BEGIN
3688: PetscErrorCode  MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3689: {
3690:   PetscInt       m,cstart, cend,j,nnz,i,d;
3691:   PetscInt       *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
3692:   const PetscInt *JJ;
3693:   PetscScalar    *values;

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

3699:   PetscLayoutSetUp(B->rmap);
3700:   PetscLayoutSetUp(B->cmap);
3701:   m      = B->rmap->n;
3702:   cstart = B->cmap->rstart;
3703:   cend   = B->cmap->rend;
3704:   rstart = B->rmap->rstart;

3706:   PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);

3708: #if defined(PETSC_USE_DEBUGGING)
3709:   for (i=0; i<m; i++) {
3710:     nnz     = Ii[i+1]- Ii[i];
3711:     JJ      = J + Ii[i];
3712:     if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
3713:     if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j);
3714:     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);
3715:   }
3716: #endif

3718:   for (i=0; i<m; i++) {
3719:     nnz     = Ii[i+1]- Ii[i];
3720:     JJ      = J + Ii[i];
3721:     nnz_max = PetscMax(nnz_max,nnz);
3722:     d       = 0;
3723:     for (j=0; j<nnz; j++) {
3724:       if (cstart <= JJ[j] && JJ[j] < cend) d++;
3725:     }
3726:     d_nnz[i] = d;
3727:     o_nnz[i] = nnz - d;
3728:   }
3729:   MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);
3730:   PetscFree2(d_nnz,o_nnz);

3732:   if (v) values = (PetscScalar*)v;
3733:   else {
3734:     PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);
3735:     PetscMemzero(values,nnz_max*sizeof(PetscScalar));
3736:   }

3738:   for (i=0; i<m; i++) {
3739:     ii   = i + rstart;
3740:     nnz  = Ii[i+1]- Ii[i];
3741:     MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);
3742:   }
3743:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3744:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

3746:   if (!v) {
3747:     PetscFree(values);
3748:   }
3749:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3750:   return(0);
3751: }
3752: EXTERN_C_END

3756: /*@
3757:    MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
3758:    (the default parallel PETSc format).  

3760:    Collective on MPI_Comm

3762:    Input Parameters:
3763: +  B - the matrix 
3764: .  i - the indices into j for the start of each local row (starts with zero)
3765: .  j - the column indices for each local row (starts with zero)
3766: -  v - optional values in the matrix

3768:    Level: developer

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

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

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

3781:         1 0 0
3782:         2 0 3     P0
3783:        -------
3784:         4 5 6     P1

3786:      Process0 [P0]: rows_owned=[0,1]
3787:         i =  {0,1,3}  [size = nrow+1  = 2+1]
3788:         j =  {0,0,2}  [size = nz = 6]
3789:         v =  {1,2,3}  [size = nz = 6]

3791:      Process1 [P1]: rows_owned=[2]
3792:         i =  {0,3}    [size = nrow+1  = 1+1]
3793:         j =  {0,1,2}  [size = nz = 6]
3794:         v =  {4,5,6}  [size = nz = 6]

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

3798: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ,
3799:           MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays()
3800: @*/
3801: PetscErrorCode  MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3802: {

3806:   PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
3807:   return(0);
3808: }

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

3819:    Collective on MPI_Comm

3821:    Input Parameters:
3822: +  A - the matrix 
3823: .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
3824:            (same value is used for all local rows)
3825: .  d_nnz - array containing the number of nonzeros in the various rows of the 
3826:            DIAGONAL portion of the local submatrix (possibly different for each row)
3827:            or PETSC_NULL, if d_nz is used to specify the nonzero structure. 
3828:            The size of this array is equal to the number of local rows, i.e 'm'. 
3829:            For matrices that will be factored, you must leave room for (and set)
3830:            the diagonal entry even if it is zero.
3831: .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
3832:            submatrix (same value is used for all local rows).
3833: -  o_nnz - array containing the number of nonzeros in the various rows of the
3834:            OFF-DIAGONAL portion of the local submatrix (possibly different for
3835:            each row) or PETSC_NULL, if o_nz is used to specify the nonzero 
3836:            structure. The size of this array is equal to the number 
3837:            of local rows, i.e 'm'. 

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

3841:    The AIJ format (also called the Yale sparse matrix format or
3842:    compressed row storage (CSR)), is fully compatible with standard Fortran 77
3843:    storage.  The stored row and column indices begin with zero. 
3844:    See the <A href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</A> for details.

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

3850:    The DIAGONAL portion of the local submatrix of a processor can be defined
3851:    as the submatrix which is obtained by extraction the part corresponding to
3852:    the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the
3853:    first row that belongs to the processor, r2 is the last row belonging to
3854:    the this processor, and c1-c2 is range of indices of the local part of a
3855:    vector suitable for applying the matrix to.  This is an mxn matrix.  In the
3856:    common case of a square matrix, the row and column ranges are the same and
3857:    the DIAGONAL part is also square. The remaining portion of the local
3858:    submatrix (mxN) constitute the OFF-DIAGONAL portion.

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

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

3867:    Example usage:
3868:   
3869:    Consider the following 8x8 matrix with 34 non-zero values, that is 
3870:    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
3871:    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 
3872:    as follows:

3874: .vb
3875:             1  2  0  |  0  3  0  |  0  4
3876:     Proc0   0  5  6  |  7  0  0  |  8  0
3877:             9  0 10  | 11  0  0  | 12  0
3878:     -------------------------------------
3879:            13  0 14  | 15 16 17  |  0  0
3880:     Proc1   0 18  0  | 19 20 21  |  0  0 
3881:             0  0  0  | 22 23  0  | 24  0
3882:     -------------------------------------
3883:     Proc2  25 26 27  |  0  0 28  | 29  0
3884:            30  0  0  | 31 32 33  |  0 34
3885: .ve

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

3889: .vb
3890:       A B C
3891:       D E F
3892:       G H I
3893: .ve

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

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

3902:    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
3903:    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
3904:    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
3905:    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
3906:    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
3907:    matrix, ans [DF] as another SeqAIJ matrix.

3909:    When d_nz, o_nz parameters are specified, d_nz storage elements are
3910:    allocated for every row of the local diagonal submatrix, and o_nz
3911:    storage locations are allocated for every row of the OFF-DIAGONAL submat.
3912:    One way to choose d_nz and o_nz is to use the max nonzerors per local 
3913:    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 
3914:    In this case, the values of d_nz,o_nz are:
3915: .vb
3916:      proc0 : dnz = 2, o_nz = 2
3917:      proc1 : dnz = 3, o_nz = 2
3918:      proc2 : dnz = 1, o_nz = 4
3919: .ve
3920:    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
3921:    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
3922:    for proc3. i.e we are using 12+15+10=37 storage locations to store 
3923:    34 values.

3925:    When d_nnz, o_nnz parameters are specified, the storage is specified
3926:    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
3927:    In the above case the values for d_nnz,o_nnz are:
3928: .vb
3929:      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
3930:      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
3931:      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
3932: .ve
3933:    Here the space allocated is sum of all the above values i.e 34, and
3934:    hence pre-allocation is perfect.

3936:    Level: intermediate

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

3940: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(),
3941:           MPIAIJ, MatGetInfo()
3942: @*/
3943: PetscErrorCode  MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3944: {

3950:   PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));
3951:   return(0);
3952: }

3956: /*@
3957:      MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard
3958:          CSR format the local rows.

3960:    Collective on MPI_Comm

3962:    Input Parameters:
3963: +  comm - MPI communicator
3964: .  m - number of local rows (Cannot be PETSC_DECIDE)
3965: .  n - This value should be the same as the local size used in creating the 
3966:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
3967:        calculated if N is given) For square matrices n is almost always m.
3968: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3969: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
3970: .   i - row indices
3971: .   j - column indices
3972: -   a - matrix values

3974:    Output Parameter:
3975: .   mat - the matrix

3977:    Level: intermediate

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

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

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

3990:         1 0 0
3991:         2 0 3     P0
3992:        -------
3993:         4 5 6     P1

3995:      Process0 [P0]: rows_owned=[0,1]
3996:         i =  {0,1,3}  [size = nrow+1  = 2+1]
3997:         j =  {0,0,2}  [size = nz = 6]
3998:         v =  {1,2,3}  [size = nz = 6]

4000:      Process1 [P1]: rows_owned=[2]
4001:         i =  {0,3}    [size = nrow+1  = 1+1]
4002:         j =  {0,1,2}  [size = nz = 6]
4003:         v =  {4,5,6}  [size = nz = 6]

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

4007: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4008:           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
4009: @*/
4010: PetscErrorCode  MatCreateMPIAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
4011: {

4015:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4016:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
4017:   MatCreate(comm,mat);
4018:   MatSetSizes(*mat,m,n,M,N);
4019:   /* MatSetBlockSizes(M,bs,cbs);  */
4020:   MatSetType(*mat,MATMPIAIJ);
4021:   MatMPIAIJSetPreallocationCSR(*mat,i,j,a);
4022:   return(0);
4023: }

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

4034:    Collective on MPI_Comm

4036:    Input Parameters:
4037: +  comm - MPI communicator
4038: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
4039:            This value should be the same as the local size used in creating the 
4040:            y vector for the matrix-vector product y = Ax.
4041: .  n - This value should be the same as the local size used in creating the 
4042:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4043:        calculated if N is given) For square matrices n is almost always m.
4044: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4045: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4046: .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
4047:            (same value is used for all local rows)
4048: .  d_nnz - array containing the number of nonzeros in the various rows of the 
4049:            DIAGONAL portion of the local submatrix (possibly different for each row)
4050:            or PETSC_NULL, if d_nz is used to specify the nonzero structure. 
4051:            The size of this array is equal to the number of local rows, i.e 'm'. 
4052: .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
4053:            submatrix (same value is used for all local rows).
4054: -  o_nnz - array containing the number of nonzeros in the various rows of the
4055:            OFF-DIAGONAL portion of the local submatrix (possibly different for
4056:            each row) or PETSC_NULL, if o_nz is used to specify the nonzero 
4057:            structure. The size of this array is equal to the number 
4058:            of local rows, i.e 'm'. 

4060:    Output Parameter:
4061: .  A - the matrix 

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

4067:    Notes:
4068:    If the *_nnz parameter is given then the *_nz parameter is ignored

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

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

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

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

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

4091:    The DIAGONAL portion of the local submatrix on any given processor
4092:    is the submatrix corresponding to the rows and columns m,n
4093:    corresponding to the given processor. i.e diagonal matrix on
4094:    process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1]
4095:    etc. The remaining portion of the local submatrix [m x (N-n)]
4096:    constitute the OFF-DIAGONAL portion. The example below better
4097:    illustrates this concept.

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

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

4106:    When calling this routine with a single process communicator, a matrix of
4107:    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
4108:    type of communicator, use the construction mechanism:
4109:      MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...);
4110:  
4111:    By default, this format uses inodes (identical nodes) when possible.
4112:    We search for consecutive rows with the same nonzero structure, thereby
4113:    reusing matrix information to achieve increased efficiency.

4115:    Options Database Keys:
4116: +  -mat_no_inode  - Do not use inodes
4117: .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
4118: -  -mat_aij_oneindex - Internally use indexing starting at 1
4119:         rather than 0.  Note that when calling MatSetValues(),
4120:         the user still MUST index entries starting at 0!


4123:    Example usage:
4124:   
4125:    Consider the following 8x8 matrix with 34 non-zero values, that is 
4126:    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
4127:    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 
4128:    as follows:

4130: .vb
4131:             1  2  0  |  0  3  0  |  0  4
4132:     Proc0   0  5  6  |  7  0  0  |  8  0
4133:             9  0 10  | 11  0  0  | 12  0
4134:     -------------------------------------
4135:            13  0 14  | 15 16 17  |  0  0
4136:     Proc1   0 18  0  | 19 20 21  |  0  0 
4137:             0  0  0  | 22 23  0  | 24  0
4138:     -------------------------------------
4139:     Proc2  25 26 27  |  0  0 28  | 29  0
4140:            30  0  0  | 31 32 33  |  0 34
4141: .ve

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

4145: .vb
4146:       A B C
4147:       D E F
4148:       G H I
4149: .ve

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

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

4158:    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
4159:    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
4160:    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
4161:    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
4162:    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
4163:    matrix, ans [DF] as another SeqAIJ matrix.

4165:    When d_nz, o_nz parameters are specified, d_nz storage elements are
4166:    allocated for every row of the local diagonal submatrix, and o_nz
4167:    storage locations are allocated for every row of the OFF-DIAGONAL submat.
4168:    One way to choose d_nz and o_nz is to use the max nonzerors per local 
4169:    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 
4170:    In this case, the values of d_nz,o_nz are:
4171: .vb
4172:      proc0 : dnz = 2, o_nz = 2
4173:      proc1 : dnz = 3, o_nz = 2
4174:      proc2 : dnz = 1, o_nz = 4
4175: .ve
4176:    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
4177:    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
4178:    for proc3. i.e we are using 12+15+10=37 storage locations to store 
4179:    34 values.

4181:    When d_nnz, o_nnz parameters are specified, the storage is specified
4182:    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
4183:    In the above case the values for d_nnz,o_nnz are:
4184: .vb
4185:      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
4186:      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
4187:      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
4188: .ve
4189:    Here the space allocated is sum of all the above values i.e 34, and
4190:    hence pre-allocation is perfect.

4192:    Level: intermediate

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

4196: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4197:           MPIAIJ, MatCreateMPIAIJWithArrays()
4198: @*/
4199: 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)
4200: {
4202:   PetscMPIInt    size;

4205:   MatCreate(comm,A);
4206:   MatSetSizes(*A,m,n,M,N);
4207:   MPI_Comm_size(comm,&size);
4208:   if (size > 1) {
4209:     MatSetType(*A,MATMPIAIJ);
4210:     MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);
4211:   } else {
4212:     MatSetType(*A,MATSEQAIJ);
4213:     MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);
4214:   }
4215:   return(0);
4216: }

4220: PetscErrorCode  MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[])
4221: {
4222:   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;

4225:   *Ad     = a->A;
4226:   *Ao     = a->B;
4227:   *colmap = a->garray;
4228:   return(0);
4229: }

4233: PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
4234: {
4236:   PetscInt       i;
4237:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

4240:   if (coloring->ctype == IS_COLORING_GLOBAL) {
4241:     ISColoringValue *allcolors,*colors;
4242:     ISColoring      ocoloring;

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

4247:     /* set coloring for off-diagonal portion */
4248:     ISAllGatherColors(((PetscObject)A)->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);
4249:     PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);
4250:     for (i=0; i<a->B->cmap->n; i++) {
4251:       colors[i] = allcolors[a->garray[i]];
4252:     }
4253:     PetscFree(allcolors);
4254:     ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);
4255:     MatSetColoring_SeqAIJ(a->B,ocoloring);
4256:     ISColoringDestroy(&ocoloring);
4257:   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
4258:     ISColoringValue *colors;
4259:     PetscInt        *larray;
4260:     ISColoring      ocoloring;

4262:     /* set coloring for diagonal portion */
4263:     PetscMalloc((a->A->cmap->n+1)*sizeof(PetscInt),&larray);
4264:     for (i=0; i<a->A->cmap->n; i++) {
4265:       larray[i] = i + A->cmap->rstart;
4266:     }
4267:     ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,PETSC_NULL,larray);
4268:     PetscMalloc((a->A->cmap->n+1)*sizeof(ISColoringValue),&colors);
4269:     for (i=0; i<a->A->cmap->n; i++) {
4270:       colors[i] = coloring->colors[larray[i]];
4271:     }
4272:     PetscFree(larray);
4273:     ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);
4274:     MatSetColoring_SeqAIJ(a->A,ocoloring);
4275:     ISColoringDestroy(&ocoloring);

4277:     /* set coloring for off-diagonal portion */
4278:     PetscMalloc((a->B->cmap->n+1)*sizeof(PetscInt),&larray);
4279:     ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,PETSC_NULL,larray);
4280:     PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);
4281:     for (i=0; i<a->B->cmap->n; i++) {
4282:       colors[i] = coloring->colors[larray[i]];
4283:     }
4284:     PetscFree(larray);
4285:     ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);
4286:     MatSetColoring_SeqAIJ(a->B,ocoloring);
4287:     ISColoringDestroy(&ocoloring);
4288:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype);

4290:   return(0);
4291: }

4293: #if defined(PETSC_HAVE_ADIC)
4296: PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
4297: {
4298:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

4302:   MatSetValuesAdic_SeqAIJ(a->A,advalues);
4303:   MatSetValuesAdic_SeqAIJ(a->B,advalues);
4304:   return(0);
4305: }
4306: #endif

4310: PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues)
4311: {
4312:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

4316:   MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);
4317:   MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);
4318:   return(0);
4319: }

4323: PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJSymbolic(MPI_Comm comm,Mat inmat,PetscInt n,Mat *outmat)
4324: {
4326:   PetscInt       m,N,i,rstart,nnz,*dnz,*onz,sum,bs,cbs;
4327:   PetscInt       *indx;

4330:   /* This routine will ONLY return MPIAIJ type matrix */
4331:   MatGetSize(inmat,&m,&N);
4332:   MatGetBlockSizes(inmat,&bs,&cbs);
4333:   if (n == PETSC_DECIDE){
4334:     PetscSplitOwnership(comm,&n,&N);
4335:   }
4336:   /* Check sum(n) = N */
4337:   MPI_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);
4338:   if (sum != N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",N);
4339: 
4340:   MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);
4341:   rstart -= m;

4343:   MatPreallocateInitialize(comm,m,n,dnz,onz);
4344:   for (i=0;i<m;i++) {
4345:     MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);
4346:     MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);
4347:     MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);
4348:   }
4349: 
4350:   MatCreate(comm,outmat);
4351:   MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
4352:   MatSetBlockSizes(*outmat,bs,cbs);
4353:   MatSetType(*outmat,MATMPIAIJ);
4354:   MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);
4355:   MatPreallocateFinalize(dnz,onz);
4356:   return(0);
4357: }

4361: PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJNumeric(MPI_Comm comm,Mat inmat,PetscInt n,Mat outmat)
4362: {
4364:   PetscInt       m,N,i,rstart,nnz,Ii;
4365:   PetscInt       *indx;
4366:   PetscScalar    *values;

4369:   MatGetSize(inmat,&m,&N);
4370:   MatGetOwnershipRange(outmat,&rstart,PETSC_NULL);
4371:   for (i=0;i<m;i++) {
4372:     MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
4373:     Ii    = i + rstart;
4374:     MatSetValues_MPIAIJ(outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);
4375:     MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
4376:   }
4377:   MatAssemblyBegin(outmat,MAT_FINAL_ASSEMBLY);
4378:   MatAssemblyEnd(outmat,MAT_FINAL_ASSEMBLY);
4379:   return(0);
4380: }

4384: /*@
4385:       MatCreateMPIAIJConcatenateSeqAIJ - Creates a single large PETSc matrix by concatenating sequential
4386:                  matrices from each processor

4388:     Collective on MPI_Comm

4390:    Input Parameters:
4391: +    comm - the communicators the parallel matrix will live on
4392: .    inmat - the input sequential matrices
4393: .    n - number of local columns (or PETSC_DECIDE)
4394: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

4396:    Output Parameter:
4397: .    outmat - the parallel matrix generated

4399:     Level: advanced

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

4403: @*/
4404: PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4405: {

4409:   PetscLogEventBegin(MAT_Merge,inmat,0,0,0);
4410:   if (scall == MAT_INITIAL_MATRIX){
4411:     MatCreateMPIAIJConcatenateSeqAIJSymbolic(comm,inmat,n,outmat);
4412:   }
4413:   MatCreateMPIAIJConcatenateSeqAIJNumeric(comm,inmat,n,*outmat);
4414:   PetscLogEventEnd(MAT_Merge,inmat,0,0,0);
4415:   return(0);
4416: }

4420: PetscErrorCode MatFileSplit(Mat A,char *outfile)
4421: {
4422:   PetscErrorCode    ierr;
4423:   PetscMPIInt       rank;
4424:   PetscInt          m,N,i,rstart,nnz;
4425:   size_t            len;
4426:   const PetscInt    *indx;
4427:   PetscViewer       out;
4428:   char              *name;
4429:   Mat               B;
4430:   const PetscScalar *values;

4433:   MatGetLocalSize(A,&m,0);
4434:   MatGetSize(A,0,&N);
4435:   /* Should this be the type of the diagonal block of A? */
4436:   MatCreate(PETSC_COMM_SELF,&B);
4437:   MatSetSizes(B,m,N,m,N);
4438:   MatSetBlockSizes(B,A->rmap->bs,A->cmap->bs);
4439:   MatSetType(B,MATSEQAIJ);
4440:   MatSeqAIJSetPreallocation(B,0,PETSC_NULL);
4441:   MatGetOwnershipRange(A,&rstart,0);
4442:   for (i=0;i<m;i++) {
4443:     MatGetRow(A,i+rstart,&nnz,&indx,&values);
4444:     MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);
4445:     MatRestoreRow(A,i+rstart,&nnz,&indx,&values);
4446:   }
4447:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
4448:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

4450:   MPI_Comm_rank(((PetscObject)A)->comm,&rank);
4451:   PetscStrlen(outfile,&len);
4452:   PetscMalloc((len+5)*sizeof(char),&name);
4453:   sprintf(name,"%s.%d",outfile,rank);
4454:   PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);
4455:   PetscFree(name);
4456:   MatView(B,out);
4457:   PetscViewerDestroy(&out);
4458:   MatDestroy(&B);
4459:   return(0);
4460: }

4462: extern PetscErrorCode MatDestroy_MPIAIJ(Mat);
4465: PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
4466: {
4467:   PetscErrorCode       ierr;
4468:   Mat_Merge_SeqsToMPI  *merge;
4469:   PetscContainer       container;

4472:   PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);
4473:   if (container) {
4474:     PetscContainerGetPointer(container,(void **)&merge);
4475:     PetscFree(merge->id_r);
4476:     PetscFree(merge->len_s);
4477:     PetscFree(merge->len_r);
4478:     PetscFree(merge->bi);
4479:     PetscFree(merge->bj);
4480:     PetscFree(merge->buf_ri[0]);
4481:     PetscFree(merge->buf_ri);
4482:     PetscFree(merge->buf_rj[0]);
4483:     PetscFree(merge->buf_rj);
4484:     PetscFree(merge->coi);
4485:     PetscFree(merge->coj);
4486:     PetscFree(merge->owners_co);
4487:     PetscLayoutDestroy(&merge->rowmap);
4488:     PetscFree(merge);
4489:     PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);
4490:   }
4491:   MatDestroy_MPIAIJ(A);
4492:   return(0);
4493: }

4495: #include <../src/mat/utils/freespace.h>
4496: #include <petscbt.h>

4500: PetscErrorCode  MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat)
4501: {
4502:   PetscErrorCode       ierr;
4503:   MPI_Comm             comm=((PetscObject)mpimat)->comm;
4504:   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
4505:   PetscMPIInt          size,rank,taga,*len_s;
4506:   PetscInt             N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj=a->j;
4507:   PetscInt             proc,m;
4508:   PetscInt             **buf_ri,**buf_rj;
4509:   PetscInt             k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
4510:   PetscInt             nrows,**buf_ri_k,**nextrow,**nextai;
4511:   MPI_Request          *s_waits,*r_waits;
4512:   MPI_Status           *status;
4513:   MatScalar            *aa=a->a;
4514:   MatScalar            **abuf_r,*ba_i;
4515:   Mat_Merge_SeqsToMPI  *merge;
4516:   PetscContainer       container;

4519:   PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);

4521:   MPI_Comm_size(comm,&size);
4522:   MPI_Comm_rank(comm,&rank);

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

4527:   bi     = merge->bi;
4528:   bj     = merge->bj;
4529:   buf_ri = merge->buf_ri;
4530:   buf_rj = merge->buf_rj;

4532:   PetscMalloc(size*sizeof(MPI_Status),&status);
4533:   owners = merge->rowmap->range;
4534:   len_s  = merge->len_s;

4536:   /* send and recv matrix values */
4537:   /*-----------------------------*/
4538:   PetscObjectGetNewTag((PetscObject)mpimat,&taga);
4539:   PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);

4541:   PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);
4542:   for (proc=0,k=0; proc<size; proc++){
4543:     if (!len_s[proc]) continue;
4544:     i = owners[proc];
4545:     MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);
4546:     k++;
4547:   }

4549:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,r_waits,status);}
4550:   if (merge->nsend) {MPI_Waitall(merge->nsend,s_waits,status);}
4551:   PetscFree(status);

4553:   PetscFree(s_waits);
4554:   PetscFree(r_waits);

4556:   /* insert mat values of mpimat */
4557:   /*----------------------------*/
4558:   PetscMalloc(N*sizeof(PetscScalar),&ba_i);
4559:   PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);

4561:   for (k=0; k<merge->nrecv; k++){
4562:     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
4563:     nrows = *(buf_ri_k[k]);
4564:     nextrow[k]  = buf_ri_k[k]+1;  /* next row number of k-th recved i-structure */
4565:     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
4566:   }

4568:   /* set values of ba */
4569:   m = merge->rowmap->n;
4570:   for (i=0; i<m; i++) {
4571:     arow = owners[rank] + i;
4572:     bj_i = bj+bi[i];  /* col indices of the i-th row of mpimat */
4573:     bnzi = bi[i+1] - bi[i];
4574:     PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));

4576:     /* add local non-zero vals of this proc's seqmat into ba */
4577:     anzi = ai[arow+1] - ai[arow];
4578:     aj   = a->j + ai[arow];
4579:     aa   = a->a + ai[arow];
4580:     nextaj = 0;
4581:     for (j=0; nextaj<anzi; j++){
4582:       if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
4583:         ba_i[j] += aa[nextaj++];
4584:       }
4585:     }

4587:     /* add received vals into ba */
4588:     for (k=0; k<merge->nrecv; k++){ /* k-th received message */
4589:       /* i-th row */
4590:       if (i == *nextrow[k]) {
4591:         anzi = *(nextai[k]+1) - *nextai[k];
4592:         aj   = buf_rj[k] + *(nextai[k]);
4593:         aa   = abuf_r[k] + *(nextai[k]);
4594:         nextaj = 0;
4595:         for (j=0; nextaj<anzi; j++){
4596:           if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
4597:             ba_i[j] += aa[nextaj++];
4598:           }
4599:         }
4600:         nextrow[k]++; nextai[k]++;
4601:       }
4602:     }
4603:     MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);
4604:   }
4605:   MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);
4606:   MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);

4608:   PetscFree(abuf_r[0]);
4609:   PetscFree(abuf_r);
4610:   PetscFree(ba_i);
4611:   PetscFree3(buf_ri_k,nextrow,nextai);
4612:   PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);
4613:   return(0);
4614: }

4616: extern PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat);

4620: PetscErrorCode  MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
4621: {
4622:   PetscErrorCode       ierr;
4623:   Mat                  B_mpi;
4624:   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
4625:   PetscMPIInt          size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
4626:   PetscInt             **buf_rj,**buf_ri,**buf_ri_k;
4627:   PetscInt             M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j;
4628:   PetscInt             len,proc,*dnz,*onz,bs,cbs;
4629:   PetscInt             k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
4630:   PetscInt             nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
4631:   MPI_Request          *si_waits,*sj_waits,*ri_waits,*rj_waits;
4632:   MPI_Status           *status;
4633:   PetscFreeSpaceList   free_space=PETSC_NULL,current_space=PETSC_NULL;
4634:   PetscBT              lnkbt;
4635:   Mat_Merge_SeqsToMPI  *merge;
4636:   PetscContainer       container;

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

4641:   /* make sure it is a PETSc comm */
4642:   PetscCommDuplicate(comm,&comm,PETSC_NULL);
4643:   MPI_Comm_size(comm,&size);
4644:   MPI_Comm_rank(comm,&rank);

4646:   PetscNew(Mat_Merge_SeqsToMPI,&merge);
4647:   PetscMalloc(size*sizeof(MPI_Status),&status);

4649:   /* determine row ownership */
4650:   /*---------------------------------------------------------*/
4651:   PetscLayoutCreate(comm,&merge->rowmap);
4652:   PetscLayoutSetLocalSize(merge->rowmap,m);
4653:   PetscLayoutSetSize(merge->rowmap,M);
4654:   PetscLayoutSetBlockSize(merge->rowmap,1);
4655:   PetscLayoutSetUp(merge->rowmap);
4656:   PetscMalloc(size*sizeof(PetscMPIInt),&len_si);
4657:   PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);

4659:   m      = merge->rowmap->n;
4660:   M      = merge->rowmap->N;
4661:   owners = merge->rowmap->range;

4663:   /* determine the number of messages to send, their lengths */
4664:   /*---------------------------------------------------------*/
4665:   len_s  = merge->len_s;

4667:   len = 0;  /* length of buf_si[] */
4668:   merge->nsend = 0;
4669:   for (proc=0; proc<size; proc++){
4670:     len_si[proc] = 0;
4671:     if (proc == rank){
4672:       len_s[proc] = 0;
4673:     } else {
4674:       len_si[proc] = owners[proc+1] - owners[proc] + 1;
4675:       len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
4676:     }
4677:     if (len_s[proc]) {
4678:       merge->nsend++;
4679:       nrows = 0;
4680:       for (i=owners[proc]; i<owners[proc+1]; i++){
4681:         if (ai[i+1] > ai[i]) nrows++;
4682:       }
4683:       len_si[proc] = 2*(nrows+1);
4684:       len += len_si[proc];
4685:     }
4686:   }

4688:   /* determine the number and length of messages to receive for ij-structure */
4689:   /*-------------------------------------------------------------------------*/
4690:   PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);
4691:   PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);

4693:   /* post the Irecv of j-structure */
4694:   /*-------------------------------*/
4695:   PetscCommGetNewTag(comm,&tagj);
4696:   PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);

4698:   /* post the Isend of j-structure */
4699:   /*--------------------------------*/
4700:   PetscMalloc2(merge->nsend,MPI_Request,&si_waits,merge->nsend,MPI_Request,&sj_waits);

4702:   for (proc=0, k=0; proc<size; proc++){
4703:     if (!len_s[proc]) continue;
4704:     i = owners[proc];
4705:     MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);
4706:     k++;
4707:   }

4709:   /* receives and sends of j-structure are complete */
4710:   /*------------------------------------------------*/
4711:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,rj_waits,status);}
4712:   if (merge->nsend) {MPI_Waitall(merge->nsend,sj_waits,status);}

4714:   /* send and recv i-structure */
4715:   /*---------------------------*/
4716:   PetscCommGetNewTag(comm,&tagi);
4717:   PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);

4719:   PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);
4720:   buf_si = buf_s;  /* points to the beginning of k-th msg to be sent */
4721:   for (proc=0,k=0; proc<size; proc++){
4722:     if (!len_s[proc]) continue;
4723:     /* form outgoing message for i-structure:
4724:          buf_si[0]:                 nrows to be sent
4725:                [1:nrows]:           row index (global)
4726:                [nrows+1:2*nrows+1]: i-structure index
4727:     */
4728:     /*-------------------------------------------*/
4729:     nrows = len_si[proc]/2 - 1;
4730:     buf_si_i    = buf_si + nrows+1;
4731:     buf_si[0]   = nrows;
4732:     buf_si_i[0] = 0;
4733:     nrows = 0;
4734:     for (i=owners[proc]; i<owners[proc+1]; i++){
4735:       anzi = ai[i+1] - ai[i];
4736:       if (anzi) {
4737:         buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
4738:         buf_si[nrows+1] = i-owners[proc]; /* local row index */
4739:         nrows++;
4740:       }
4741:     }
4742:     MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);
4743:     k++;
4744:     buf_si += len_si[proc];
4745:   }

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

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

4755:   PetscFree(len_si);
4756:   PetscFree(len_ri);
4757:   PetscFree(rj_waits);
4758:   PetscFree2(si_waits,sj_waits);
4759:   PetscFree(ri_waits);
4760:   PetscFree(buf_s);
4761:   PetscFree(status);

4763:   /* compute a local seq matrix in each processor */
4764:   /*----------------------------------------------*/
4765:   /* allocate bi array and free space for accumulating nonzero column info */
4766:   PetscMalloc((m+1)*sizeof(PetscInt),&bi);
4767:   bi[0] = 0;

4769:   /* create and initialize a linked list */
4770:   nlnk = N+1;
4771:   PetscLLCreate(N,N,nlnk,lnk,lnkbt);

4773:   /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
4774:   len = 0;
4775:   len  = ai[owners[rank+1]] - ai[owners[rank]];
4776:   PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);
4777:   current_space = free_space;

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

4782:   for (k=0; k<merge->nrecv; k++){
4783:     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
4784:     nrows = *buf_ri_k[k];
4785:     nextrow[k]  = buf_ri_k[k] + 1;  /* next row number of k-th recved i-structure */
4786:     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
4787:   }

4789:   MatPreallocateInitialize(comm,m,n,dnz,onz);
4790:   len = 0;
4791:   for (i=0;i<m;i++) {
4792:     bnzi   = 0;
4793:     /* add local non-zero cols of this proc's seqmat into lnk */
4794:     arow   = owners[rank] + i;
4795:     anzi   = ai[arow+1] - ai[arow];
4796:     aj     = a->j + ai[arow];
4797:     PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);
4798:     bnzi += nlnk;
4799:     /* add received col data into lnk */
4800:     for (k=0; k<merge->nrecv; k++){ /* k-th received message */
4801:       if (i == *nextrow[k]) { /* i-th row */
4802:         anzi = *(nextai[k]+1) - *nextai[k];
4803:         aj   = buf_rj[k] + *nextai[k];
4804:         PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);
4805:         bnzi += nlnk;
4806:         nextrow[k]++; nextai[k]++;
4807:       }
4808:     }
4809:     if (len < bnzi) len = bnzi;  /* =max(bnzi) */

4811:     /* if free space is not available, make more free space */
4812:     if (current_space->local_remaining<bnzi) {
4813:       PetscFreeSpaceGet(bnzi+current_space->total_array_size,&current_space);
4814:       nspacedouble++;
4815:     }
4816:     /* copy data into free space, then initialize lnk */
4817:     PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);
4818:     MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);

4820:     current_space->array           += bnzi;
4821:     current_space->local_used      += bnzi;
4822:     current_space->local_remaining -= bnzi;

4824:     bi[i+1] = bi[i] + bnzi;
4825:   }

4827:   PetscFree3(buf_ri_k,nextrow,nextai);

4829:   PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);
4830:   PetscFreeSpaceContiguous(&free_space,bj);
4831:   PetscLLDestroy(lnk,lnkbt);

4833:   /* create symbolic parallel matrix B_mpi */
4834:   /*---------------------------------------*/
4835:     MatGetBlockSizes(seqmat,&bs,&cbs);
4836:   MatCreate(comm,&B_mpi);
4837:   if (n==PETSC_DECIDE) {
4838:     MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);
4839:   } else {
4840:     MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
4841:   }
4842:   MatSetBlockSizes(B_mpi,bs,cbs);
4843:   MatSetType(B_mpi,MATMPIAIJ);
4844:   MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);
4845:   MatPreallocateFinalize(dnz,onz);
4846:   MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);

4848:   /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */
4849:   B_mpi->assembled     = PETSC_FALSE;
4850:   B_mpi->ops->destroy  = MatDestroy_MPIAIJ_SeqsToMPI;
4851:   merge->bi            = bi;
4852:   merge->bj            = bj;
4853:   merge->buf_ri        = buf_ri;
4854:   merge->buf_rj        = buf_rj;
4855:   merge->coi           = PETSC_NULL;
4856:   merge->coj           = PETSC_NULL;
4857:   merge->owners_co     = PETSC_NULL;

4859:   PetscCommDestroy(&comm);

4861:   /* attach the supporting struct to B_mpi for reuse */
4862:   PetscContainerCreate(PETSC_COMM_SELF,&container);
4863:   PetscContainerSetPointer(container,merge);
4864:   PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);
4865:   PetscContainerDestroy(&container);
4866:   *mpimat = B_mpi;

4868:   PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);
4869:   return(0);
4870: }

4874: /*@C
4875:       MatCreateMPIAIJSumSeqAIJ - Creates a MPIAIJ matrix by adding sequential
4876:                  matrices from each processor

4878:     Collective on MPI_Comm

4880:    Input Parameters:
4881: +    comm - the communicators the parallel matrix will live on
4882: .    seqmat - the input sequential matrices
4883: .    m - number of local rows (or PETSC_DECIDE)
4884: .    n - number of local columns (or PETSC_DECIDE)
4885: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

4887:    Output Parameter:
4888: .    mpimat - the parallel matrix generated

4890:     Level: advanced

4892:    Notes:
4893:      The dimensions of the sequential matrix in each processor MUST be the same.
4894:      The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
4895:      destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
4896: @*/
4897: PetscErrorCode  MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
4898: {
4899:   PetscErrorCode   ierr;
4900:   PetscMPIInt     size;

4903:   MPI_Comm_size(comm,&size);
4904:   if (size == 1){
4905:      PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);
4906:      if (scall == MAT_INITIAL_MATRIX){
4907:        MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);
4908:      } else {
4909:        MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);
4910:      }
4911:      PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);
4912:      return(0);
4913:   }
4914:   PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);
4915:   if (scall == MAT_INITIAL_MATRIX){
4916:     MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);
4917:   }
4918:   MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);
4919:   PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);
4920:   return(0);
4921: }

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

4930:     Not Collective

4932:    Input Parameters:
4933: +    A - the matrix 
4934: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 

4936:    Output Parameter:
4937: .    A_loc - the local sequential matrix generated

4939:     Level: developer

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

4943: @*/
4944: PetscErrorCode  MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
4945: {
4946:   PetscErrorCode  ierr;
4947:   Mat_MPIAIJ      *mpimat=(Mat_MPIAIJ*)A->data;
4948:   Mat_SeqAIJ      *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data;
4949:   PetscInt        *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray;
4950:   MatScalar       *aa=a->a,*ba=b->a,*cam;
4951:   PetscScalar     *ca;
4952:   PetscInt        am=A->rmap->n,i,j,k,cstart=A->cmap->rstart;
4953:   PetscInt        *ci,*cj,col,ncols_d,ncols_o,jo;
4954:   PetscBool       match;

4957:   PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);
4958:   if (!match) SETERRQ(((PetscObject)A)->comm, PETSC_ERR_SUP,"Requires MPIAIJ matrix as input");
4959:   PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);
4960:   if (scall == MAT_INITIAL_MATRIX){
4961:     PetscMalloc((1+am)*sizeof(PetscInt),&ci);
4962:     ci[0] = 0;
4963:     for (i=0; i<am; i++){
4964:       ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
4965:     }
4966:     PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);
4967:     PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);
4968:     k = 0;
4969:     for (i=0; i<am; i++) {
4970:       ncols_o = bi[i+1] - bi[i];
4971:       ncols_d = ai[i+1] - ai[i];
4972:       /* off-diagonal portion of A */
4973:       for (jo=0; jo<ncols_o; jo++) {
4974:         col = cmap[*bj];
4975:         if (col >= cstart) break;
4976:         cj[k]   = col; bj++;
4977:         ca[k++] = *ba++;
4978:       }
4979:       /* diagonal portion of A */
4980:       for (j=0; j<ncols_d; j++) {
4981:         cj[k]   = cstart + *aj++;
4982:         ca[k++] = *aa++;
4983:       }
4984:       /* off-diagonal portion of A */
4985:       for (j=jo; j<ncols_o; j++) {
4986:         cj[k]   = cmap[*bj++];
4987:         ca[k++] = *ba++;
4988:       }
4989:     }
4990:     /* put together the new matrix */
4991:     MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);
4992:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
4993:     /* Since these are PETSc arrays, change flags to free them as necessary. */
4994:     mat          = (Mat_SeqAIJ*)(*A_loc)->data;
4995:     mat->free_a  = PETSC_TRUE;
4996:     mat->free_ij = PETSC_TRUE;
4997:     mat->nonew   = 0;
4998:   } else if (scall == MAT_REUSE_MATRIX){
4999:     mat=(Mat_SeqAIJ*)(*A_loc)->data;
5000:     ci = mat->i; cj = mat->j; cam = mat->a;
5001:     for (i=0; i<am; i++) {
5002:       /* off-diagonal portion of A */
5003:       ncols_o = bi[i+1] - bi[i];
5004:       for (jo=0; jo<ncols_o; jo++) {
5005:         col = cmap[*bj];
5006:         if (col >= cstart) break;
5007:         *cam++ = *ba++; bj++;
5008:       }
5009:       /* diagonal portion of A */
5010:       ncols_d = ai[i+1] - ai[i];
5011:       for (j=0; j<ncols_d; j++) *cam++ = *aa++;
5012:       /* off-diagonal portion of A */
5013:       for (j=jo; j<ncols_o; j++) {
5014:         *cam++ = *ba++; bj++;
5015:       }
5016:     }
5017:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
5018:   PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);
5019:   return(0);
5020: }

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

5027:     Not Collective

5029:    Input Parameters:
5030: +    A - the matrix 
5031: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5032: -    row, col - index sets of rows and columns to extract (or PETSC_NULL)  

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

5037:     Level: developer

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

5041: @*/
5042: PetscErrorCode  MatMPIAIJGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
5043: {
5044:   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
5045:   PetscErrorCode    ierr;
5046:   PetscInt          i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
5047:   IS                isrowa,iscola;
5048:   Mat               *aloc;
5049:   PetscBool       match;

5052:   PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);
5053:   if (!match) SETERRQ(((PetscObject)A)->comm, PETSC_ERR_SUP,"Requires MPIAIJ matrix as input");
5054:   PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);
5055:   if (!row){
5056:     start = A->rmap->rstart; end = A->rmap->rend;
5057:     ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);
5058:   } else {
5059:     isrowa = *row;
5060:   }
5061:   if (!col){
5062:     start = A->cmap->rstart;
5063:     cmap  = a->garray;
5064:     nzA   = a->A->cmap->n;
5065:     nzB   = a->B->cmap->n;
5066:     PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);
5067:     ncols = 0;
5068:     for (i=0; i<nzB; i++) {
5069:       if (cmap[i] < start) idx[ncols++] = cmap[i];
5070:       else break;
5071:     }
5072:     imark = i;
5073:     for (i=0; i<nzA; i++) idx[ncols++] = start + i;
5074:     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
5075:     ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);
5076:   } else {
5077:     iscola = *col;
5078:   }
5079:   if (scall != MAT_INITIAL_MATRIX){
5080:     PetscMalloc(sizeof(Mat),&aloc);
5081:     aloc[0] = *A_loc;
5082:   }
5083:   MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);
5084:   *A_loc = aloc[0];
5085:   PetscFree(aloc);
5086:   if (!row){
5087:     ISDestroy(&isrowa);
5088:   }
5089:   if (!col){
5090:     ISDestroy(&iscola);
5091:   }
5092:   PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);
5093:   return(0);
5094: }

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

5101:     Collective on Mat

5103:    Input Parameters:
5104: +    A,B - the matrices in mpiaij format
5105: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5106: -    rowb, colb - index sets of rows and columns of B to extract (or PETSC_NULL)   

5108:    Output Parameter:
5109: +    rowb, colb - index sets of rows and columns of B to extract 
5110: -    B_seq - the sequential matrix generated

5112:     Level: developer

5114: @*/
5115: PetscErrorCode  MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,Mat *B_seq)
5116: {
5117:   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
5118:   PetscErrorCode    ierr;
5119:   PetscInt          *idx,i,start,ncols,nzA,nzB,*cmap,imark;
5120:   IS                isrowb,iscolb;
5121:   Mat               *bseq=PETSC_NULL;
5122: 
5124:   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){
5125:     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);
5126:   }
5127:   PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);
5128: 
5129:   if (scall == MAT_INITIAL_MATRIX){
5130:     start = A->cmap->rstart;
5131:     cmap  = a->garray;
5132:     nzA   = a->A->cmap->n;
5133:     nzB   = a->B->cmap->n;
5134:     PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);
5135:     ncols = 0;
5136:     for (i=0; i<nzB; i++) {  /* row < local row index */
5137:       if (cmap[i] < start) idx[ncols++] = cmap[i];
5138:       else break;
5139:     }
5140:     imark = i;
5141:     for (i=0; i<nzA; i++) idx[ncols++] = start + i;  /* local rows */
5142:     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
5143:     ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);
5144:     ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);
5145:   } else {
5146:     if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
5147:     isrowb = *rowb; iscolb = *colb;
5148:     PetscMalloc(sizeof(Mat),&bseq);
5149:     bseq[0] = *B_seq;
5150:   }
5151:   MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);
5152:   *B_seq = bseq[0];
5153:   PetscFree(bseq);
5154:   if (!rowb){
5155:     ISDestroy(&isrowb);
5156:   } else {
5157:     *rowb = isrowb;
5158:   }
5159:   if (!colb){
5160:     ISDestroy(&iscolb);
5161:   } else {
5162:     *colb = iscolb;
5163:   }
5164:   PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);
5165:   return(0);
5166: }

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

5174:     Collective on Mat

5176:    Input Parameters:
5177: +    A,B - the matrices in mpiaij format
5178: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

5180:    Output Parameter:
5181: +    startsj_s - starting point in B's sending j-arrays, saved for MAT_REUSE (or PETSC_NULL) 
5182: .    startsj_r - starting point in B's receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL)
5183: .    bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL) 
5184: -    B_oth - the sequential matrix generated with size aBn=a->B->cmap->n by B->cmap->N

5186:     Level: developer

5188: */
5189: PetscErrorCode  MatGetBrowsOfAoCols_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscInt **startsj_s,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth)
5190: {
5191:   VecScatter_MPI_General *gen_to,*gen_from;
5192:   PetscErrorCode         ierr;
5193:   Mat_MPIAIJ             *a=(Mat_MPIAIJ*)A->data;
5194:   Mat_SeqAIJ             *b_oth;
5195:   VecScatter             ctx=a->Mvctx;
5196:   MPI_Comm               comm=((PetscObject)ctx)->comm;
5197:   PetscMPIInt            *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank;
5198:   PetscInt               *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj;
5199:   PetscScalar            *rvalues,*svalues;
5200:   MatScalar              *b_otha,*bufa,*bufA;
5201:   PetscInt               i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len;
5202:   MPI_Request            *rwaits = PETSC_NULL,*swaits = PETSC_NULL;
5203:   MPI_Status             *sstatus,rstatus;
5204:   PetscMPIInt            jj;
5205:   PetscInt               *cols,sbs,rbs;
5206:   PetscScalar            *vals;

5209:   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){
5210:     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);
5211:   }
5212:   PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);
5213:   MPI_Comm_rank(comm,&rank);

5215:   gen_to   = (VecScatter_MPI_General*)ctx->todata;
5216:   gen_from = (VecScatter_MPI_General*)ctx->fromdata;
5217:   rvalues  = gen_from->values; /* holds the length of receiving row */
5218:   svalues  = gen_to->values;   /* holds the length of sending row */
5219:   nrecvs   = gen_from->n;
5220:   nsends   = gen_to->n;

5222:   PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);
5223:   srow     = gen_to->indices;   /* local row index to be sent */
5224:   sstarts  = gen_to->starts;
5225:   sprocs   = gen_to->procs;
5226:   sstatus  = gen_to->sstatus;
5227:   sbs      = gen_to->bs;
5228:   rstarts  = gen_from->starts;
5229:   rprocs   = gen_from->procs;
5230:   rbs      = gen_from->bs;

5232:   if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
5233:   if (scall == MAT_INITIAL_MATRIX){
5234:     /* i-array */
5235:     /*---------*/
5236:     /*  post receives */
5237:     for (i=0; i<nrecvs; i++){
5238:       rowlen = (PetscInt*)rvalues + rstarts[i]*rbs;
5239:       nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */
5240:       MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
5241:     }

5243:     /* pack the outgoing message */
5244:     PetscMalloc2(nsends+1,PetscInt,&sstartsj,nrecvs+1,PetscInt,&rstartsj);
5245:     sstartsj[0] = 0;  rstartsj[0] = 0;
5246:     len = 0; /* total length of j or a array to be sent */
5247:     k = 0;
5248:     for (i=0; i<nsends; i++){
5249:       rowlen = (PetscInt*)svalues + sstarts[i]*sbs;
5250:       nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5251:       for (j=0; j<nrows; j++) {
5252:         row = srow[k] + B->rmap->range[rank]; /* global row idx */
5253:         for (l=0; l<sbs; l++){
5254:           MatGetRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL); /* rowlength */
5255:           rowlen[j*sbs+l] = ncols;
5256:           len += ncols;
5257:           MatRestoreRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);
5258:         }
5259:         k++;
5260:       }
5261:       MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);
5262:       sstartsj[i+1] = len;  /* starting point of (i+1)-th outgoing msg in bufj and bufa */
5263:     }
5264:     /* recvs and sends of i-array are completed */
5265:     i = nrecvs;
5266:     while (i--) {
5267:       MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);
5268:     }
5269:     if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}

5271:     /* allocate buffers for sending j and a arrays */
5272:     PetscMalloc((len+1)*sizeof(PetscInt),&bufj);
5273:     PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);

5275:     /* create i-array of B_oth */
5276:     PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);
5277:     b_othi[0] = 0;
5278:     len = 0; /* total length of j or a array to be received */
5279:     k = 0;
5280:     for (i=0; i<nrecvs; i++){
5281:       rowlen = (PetscInt*)rvalues + rstarts[i]*rbs;
5282:       nrows = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */
5283:       for (j=0; j<nrows; j++) {
5284:         b_othi[k+1] = b_othi[k] + rowlen[j];
5285:         len += rowlen[j]; k++;
5286:       }
5287:       rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
5288:     }

5290:     /* allocate space for j and a arrrays of B_oth */
5291:     PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);
5292:     PetscMalloc((b_othi[aBn]+1)*sizeof(MatScalar),&b_otha);

5294:     /* j-array */
5295:     /*---------*/
5296:     /*  post receives of j-array */
5297:     for (i=0; i<nrecvs; i++){
5298:       nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
5299:       MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
5300:     }

5302:     /* pack the outgoing message j-array */
5303:     k = 0;
5304:     for (i=0; i<nsends; i++){
5305:       nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5306:       bufJ = bufj+sstartsj[i];
5307:       for (j=0; j<nrows; j++) {
5308:         row  = srow[k++] + B->rmap->range[rank]; /* global row idx */
5309:         for (ll=0; ll<sbs; ll++){
5310:           MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);
5311:           for (l=0; l<ncols; l++){
5312:             *bufJ++ = cols[l];
5313:           }
5314:           MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);
5315:         }
5316:       }
5317:       MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);
5318:     }

5320:     /* recvs and sends of j-array are completed */
5321:     i = nrecvs;
5322:     while (i--) {
5323:       MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);
5324:     }
5325:     if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
5326:   } else if (scall == MAT_REUSE_MATRIX){
5327:     sstartsj = *startsj_s;
5328:     rstartsj = *startsj_r;
5329:     bufa     = *bufa_ptr;
5330:     b_oth    = (Mat_SeqAIJ*)(*B_oth)->data;
5331:     b_otha   = b_oth->a;
5332:   } else {
5333:     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");
5334:   }

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

5344:   /* pack the outgoing message a-array */
5345:   k = 0;
5346:   for (i=0; i<nsends; i++){
5347:     nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5348:     bufA = bufa+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,PETSC_NULL,&vals);
5353:         for (l=0; l<ncols; l++){
5354:           *bufA++ = vals[l];
5355:         }
5356:         MatRestoreRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);
5357:       }
5358:     }
5359:     MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);
5360:   }
5361:   /* recvs and sends of a-array are completed */
5362:   i = nrecvs;
5363:   while (i--) {
5364:     MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);
5365:   }
5366:   if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
5367:   PetscFree2(rwaits,swaits);

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

5373:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5374:     /* Since these are PETSc arrays, change flags to free them as necessary. */
5375:     b_oth          = (Mat_SeqAIJ *)(*B_oth)->data;
5376:     b_oth->free_a  = PETSC_TRUE;
5377:     b_oth->free_ij = PETSC_TRUE;
5378:     b_oth->nonew   = 0;

5380:     PetscFree(bufj);
5381:     if (!startsj_s || !bufa_ptr){
5382:       PetscFree2(sstartsj,rstartsj);
5383:       PetscFree(bufa_ptr);
5384:     } else {
5385:       *startsj_s = sstartsj;
5386:       *startsj_r = rstartsj;
5387:       *bufa_ptr  = bufa;
5388:     }
5389:   }
5390:   PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);
5391:   return(0);
5392: }

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

5399:   Not Collective

5401:   Input Parameters:
5402: . A - The matrix in mpiaij format

5404:   Output Parameter:
5405: + lvec - The local vector holding off-process values from the argument to a matrix-vector product
5406: . colmap - A map from global column index to local index into lvec
5407: - multScatter - A scatter from the argument of a matrix-vector product to lvec

5409:   Level: developer

5411: @*/
5412: #if defined (PETSC_USE_CTABLE)
5413: PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
5414: #else
5415: PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
5416: #endif
5417: {
5418:   Mat_MPIAIJ *a;

5425:   a = (Mat_MPIAIJ *) A->data;
5426:   if (lvec) *lvec = a->lvec;
5427:   if (colmap) *colmap = a->colmap;
5428:   if (multScatter) *multScatter = a->Mvctx;
5429:   return(0);
5430: }

5432: EXTERN_C_BEGIN
5433: extern PetscErrorCode  MatConvert_MPIAIJ_MPIAIJCRL(Mat,const MatType,MatReuse,Mat*);
5434: extern PetscErrorCode  MatConvert_MPIAIJ_MPIAIJPERM(Mat,const MatType,MatReuse,Mat*);
5435: extern PetscErrorCode  MatConvert_MPIAIJ_MPISBAIJ(Mat,const MatType,MatReuse,Mat*);
5436: EXTERN_C_END

5440: /*
5441:     Computes (B'*A')' since computing B*A directly is untenable

5443:                n                       p                          p
5444:         (              )       (              )         (                  )
5445:       m (      A       )  *  n (       B      )   =   m (         C        )
5446:         (              )       (              )         (                  )

5448: */
5449: PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C)
5450: {
5451:   PetscErrorCode     ierr;
5452:   Mat                At,Bt,Ct;

5455:   MatTranspose(A,MAT_INITIAL_MATRIX,&At);
5456:   MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);
5457:   MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);
5458:   MatDestroy(&At);
5459:   MatDestroy(&Bt);
5460:   MatTranspose(Ct,MAT_REUSE_MATRIX,&C);
5461:   MatDestroy(&Ct);
5462:   return(0);
5463: }

5467: PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
5468: {
5470:   PetscInt       m=A->rmap->n,n=B->cmap->n;
5471:   Mat            Cmat;

5474:   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);
5475:   MatCreate(((PetscObject)A)->comm,&Cmat);
5476:   MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
5477:   MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);
5478:   MatSetType(Cmat,MATMPIDENSE);
5479:   MatMPIDenseSetPreallocation(Cmat,PETSC_NULL);
5480:   MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);
5481:   MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);
5482:   *C   = Cmat;
5483:   (*C)->ops->matmult = MatMatMult_MPIDense_MPIAIJ;
5484:   return(0);
5485: }

5487: /* ----------------------------------------------------------------*/
5490: PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
5491: {

5495:   if (scall == MAT_INITIAL_MATRIX){
5496:     MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);
5497:   }
5498:   MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);
5499:   return(0);
5500: }

5502: EXTERN_C_BEGIN
5503: #if defined(PETSC_HAVE_MUMPS)
5504: extern PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
5505: #endif
5506: #if defined(PETSC_HAVE_PASTIX)
5507: extern PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*);
5508: #endif
5509: #if defined(PETSC_HAVE_SUPERLU_DIST)
5510: extern PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*);
5511: #endif
5512: #if defined(PETSC_HAVE_SPOOLES)
5513: extern PetscErrorCode MatGetFactor_mpiaij_spooles(Mat,MatFactorType,Mat*);
5514: #endif
5515: EXTERN_C_END

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

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

5523:   Level: beginner

5525: .seealso: MatCreateAIJ()
5526: M*/

5528: EXTERN_C_BEGIN
5531: PetscErrorCode  MatCreate_MPIAIJ(Mat B)
5532: {
5533:   Mat_MPIAIJ     *b;
5535:   PetscMPIInt    size;

5538:   MPI_Comm_size(((PetscObject)B)->comm,&size);

5540:   PetscNewLog(B,Mat_MPIAIJ,&b);
5541:   B->data         = (void*)b;
5542:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
5543:   B->assembled    = PETSC_FALSE;

5545:   B->insertmode   = NOT_SET_VALUES;
5546:   b->size         = size;
5547:   MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);

5549:   /* build cache for off array entries formed */
5550:   MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);
5551:   b->donotstash  = PETSC_FALSE;
5552:   b->colmap      = 0;
5553:   b->garray      = 0;
5554:   b->roworiented = PETSC_TRUE;

5556:   /* stuff used for matrix vector multiply */
5557:   b->lvec      = PETSC_NULL;
5558:   b->Mvctx     = PETSC_NULL;

5560:   /* stuff for MatGetRow() */
5561:   b->rowindices   = 0;
5562:   b->rowvalues    = 0;
5563:   b->getrowactive = PETSC_FALSE;

5565: #if defined(PETSC_HAVE_SPOOLES)
5566:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
5567:                                      "MatGetFactor_mpiaij_spooles",
5568:                                      MatGetFactor_mpiaij_spooles);
5569: #endif
5570: #if defined(PETSC_HAVE_MUMPS)
5571:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
5572:                                      "MatGetFactor_aij_mumps",
5573:                                      MatGetFactor_aij_mumps);
5574: #endif
5575: #if defined(PETSC_HAVE_PASTIX)
5576:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
5577:                                            "MatGetFactor_mpiaij_pastix",
5578:                                            MatGetFactor_mpiaij_pastix);
5579: #endif
5580: #if defined(PETSC_HAVE_SUPERLU_DIST)
5581:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C",
5582:                                      "MatGetFactor_mpiaij_superlu_dist",
5583:                                      MatGetFactor_mpiaij_superlu_dist);
5584: #endif
5585:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
5586:                                      "MatStoreValues_MPIAIJ",
5587:                                      MatStoreValues_MPIAIJ);
5588:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
5589:                                      "MatRetrieveValues_MPIAIJ",
5590:                                      MatRetrieveValues_MPIAIJ);
5591:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
5592:                                      "MatGetDiagonalBlock_MPIAIJ",
5593:                                      MatGetDiagonalBlock_MPIAIJ);
5594:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
5595:                                      "MatIsTranspose_MPIAIJ",
5596:                                      MatIsTranspose_MPIAIJ);
5597:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C",
5598:                                      "MatMPIAIJSetPreallocation_MPIAIJ",
5599:                                      MatMPIAIJSetPreallocation_MPIAIJ);
5600:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",
5601:                                      "MatMPIAIJSetPreallocationCSR_MPIAIJ",
5602:                                      MatMPIAIJSetPreallocationCSR_MPIAIJ);
5603:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
5604:                                      "MatDiagonalScaleLocal_MPIAIJ",
5605:                                      MatDiagonalScaleLocal_MPIAIJ);
5606:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C",
5607:                                      "MatConvert_MPIAIJ_MPIAIJPERM",
5608:                                       MatConvert_MPIAIJ_MPIAIJPERM);
5609:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C",
5610:                                      "MatConvert_MPIAIJ_MPIAIJCRL",
5611:                                       MatConvert_MPIAIJ_MPIAIJCRL);
5612:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C",
5613:                                      "MatConvert_MPIAIJ_MPISBAIJ",
5614:                                       MatConvert_MPIAIJ_MPISBAIJ);
5615:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_mpidense_mpiaij_C",
5616:                                      "MatMatMult_MPIDense_MPIAIJ",
5617:                                       MatMatMult_MPIDense_MPIAIJ);
5618:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C",
5619:                                      "MatMatMultSymbolic_MPIDense_MPIAIJ",
5620:                                      MatMatMultSymbolic_MPIDense_MPIAIJ);
5621:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C",
5622:                                      "MatMatMultNumeric_MPIDense_MPIAIJ",
5623:                                       MatMatMultNumeric_MPIDense_MPIAIJ);
5624:   PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);
5625:   return(0);
5626: }
5627: EXTERN_C_END

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

5635:    Collective on MPI_Comm

5637:    Input Parameters:
5638: +  comm - MPI communicator
5639: .  m - number of local rows (Cannot be PETSC_DECIDE)
5640: .  n - This value should be the same as the local size used in creating the 
5641:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
5642:        calculated if N is given) For square matrices n is almost always m.
5643: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
5644: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
5645: .   i - row indices for "diagonal" portion of matrix
5646: .   j - column indices
5647: .   a - matrix values
5648: .   oi - row indices for "off-diagonal" portion of matrix
5649: .   oj - column indices
5650: -   oa - matrix values

5652:    Output Parameter:
5653: .   mat - the matrix

5655:    Level: advanced

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

5661:        The i and j indices are 0 based
5662:  
5663:        See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix

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

5667:        You cannot later use MatSetValues() to change values in this matrix.

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

5671: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
5672:           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays()
5673: @*/
5674: PetscErrorCode  MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[],
5675:                                                                 PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat)
5676: {
5678:   Mat_MPIAIJ     *maij;

5681:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
5682:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
5683:   if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0");
5684:   MatCreate(comm,mat);
5685:   MatSetSizes(*mat,m,n,M,N);
5686:   MatSetType(*mat,MATMPIAIJ);
5687:   maij = (Mat_MPIAIJ*) (*mat)->data;
5688:   maij->donotstash     = PETSC_TRUE;
5689:   (*mat)->preallocated = PETSC_TRUE;

5691:   PetscLayoutSetUp((*mat)->rmap);
5692:   PetscLayoutSetUp((*mat)->cmap);

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

5697:   MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);
5698:   MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);
5699:   MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);
5700:   MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);

5702:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
5703:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
5704:   return(0);
5705: }

5707: /*
5708:     Special version for direct calls from Fortran 
5709: */
5710: #include <petsc-private/fortranimpl.h>

5712: #if defined(PETSC_HAVE_FORTRAN_CAPS)
5713: #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
5714: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
5715: #define matsetvaluesmpiaij_ matsetvaluesmpiaij
5716: #endif

5718: /* Change these macros so can be used in void function */
5719: #undef CHKERRQ
5720: #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr) 
5721: #undef SETERRQ2
5722: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 
5723: #undef SETERRQ3
5724: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
5725: #undef SETERRQ
5726: #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr) 

5728: EXTERN_C_BEGIN
5731: void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr)
5732: {
5733:   Mat             mat = *mmat;
5734:   PetscInt        m = *mm, n = *mn;
5735:   InsertMode      addv = *maddv;
5736:   Mat_MPIAIJ      *aij = (Mat_MPIAIJ*)mat->data;
5737:   PetscScalar     value;
5738:   PetscErrorCode  ierr;

5740:   MatCheckPreallocated(mat,1);
5741:   if (mat->insertmode == NOT_SET_VALUES) {
5742:     mat->insertmode = addv;
5743:   }
5744: #if defined(PETSC_USE_DEBUG)
5745:   else if (mat->insertmode != addv) {
5746:     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
5747:   }
5748: #endif
5749:   {
5750:   PetscInt        i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
5751:   PetscInt        cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
5752:   PetscBool       roworiented = aij->roworiented;

5754:   /* Some Variables required in the macro */
5755:   Mat             A = aij->A;
5756:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
5757:   PetscInt        *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
5758:   MatScalar       *aa = a->a;
5759:   PetscBool       ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
5760:   Mat             B = aij->B;
5761:   Mat_SeqAIJ      *b = (Mat_SeqAIJ*)B->data;
5762:   PetscInt        *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
5763:   MatScalar       *ba = b->a;

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

5770:   for (i=0; i<m; i++) {
5771:     if (im[i] < 0) continue;
5772: #if defined(PETSC_USE_DEBUG)
5773:     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);
5774: #endif
5775:     if (im[i] >= rstart && im[i] < rend) {
5776:       row      = im[i] - rstart;
5777:       lastcol1 = -1;
5778:       rp1      = aj + ai[row];
5779:       ap1      = aa + ai[row];
5780:       rmax1    = aimax[row];
5781:       nrow1    = ailen[row];
5782:       low1     = 0;
5783:       high1    = nrow1;
5784:       lastcol2 = -1;
5785:       rp2      = bj + bi[row];
5786:       ap2      = ba + bi[row];
5787:       rmax2    = bimax[row];
5788:       nrow2    = bilen[row];
5789:       low2     = 0;
5790:       high2    = nrow2;

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