Actual source code: mpiaij.c

  1: #define PETSCMAT_DLL

 3:  #include src/mat/impls/aij/mpi/mpiaij.h
 4:  #include src/inline/spops.h

  6: /* 
  7:   Local utility routine that creates a mapping from the global column 
  8: number to the local number in the off-diagonal part of the local 
  9: storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at 
 10: a slightly higher hash table cost; without it it is not scalable (each processor
 11: has an order N integer array but is fast to acess.
 12: */
 15: PetscErrorCode CreateColmap_MPIAIJ_Private(Mat mat)
 16: {
 17:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
 19:   PetscInt       n = aij->B->cmap.n,i;

 22: #if defined (PETSC_USE_CTABLE)
 23:   PetscTableCreate(n,&aij->colmap);
 24:   for (i=0; i<n; i++){
 25:     PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);
 26:   }
 27: #else
 28:   PetscMalloc((mat->cmap.N+1)*sizeof(PetscInt),&aij->colmap);
 29:   PetscLogObjectMemory(mat,mat->cmap.N*sizeof(PetscInt));
 30:   PetscMemzero(aij->colmap,mat->cmap.N*sizeof(PetscInt));
 31:   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
 32: #endif
 33:   return(0);
 34: }


 37: #define CHUNKSIZE   15
 38: #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
 39: { \
 40:     if (col <= lastcol1) low1 = 0; else high1 = nrow1; \
 41:     lastcol1 = col;\
 42:     while (high1-low1 > 5) { \
 43:       t = (low1+high1)/2; \
 44:       if (rp1[t] > col) high1 = t; \
 45:       else             low1  = t; \
 46:     } \
 47:       for (_i=low1; _i<high1; _i++) { \
 48:         if (rp1[_i] > col) break; \
 49:         if (rp1[_i] == col) { \
 50:           if (addv == ADD_VALUES) ap1[_i] += value;   \
 51:           else                    ap1[_i] = value; \
 52:           goto a_noinsert; \
 53:         } \
 54:       }  \
 55:       if (value == 0.0 && ignorezeroentries) goto a_noinsert; \
 56:       if (nonew == 1) goto a_noinsert; \
 57:       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
 58:       MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew); \
 59:       N = nrow1++ - 1; a->nz++; high1++; \
 60:       /* shift up all the later entries in this row */ \
 61:       for (ii=N; ii>=_i; ii--) { \
 62:         rp1[ii+1] = rp1[ii]; \
 63:         ap1[ii+1] = ap1[ii]; \
 64:       } \
 65:       rp1[_i] = col;  \
 66:       ap1[_i] = value;  \
 67:       a_noinsert: ; \
 68:       ailen[row] = nrow1; \
 69: } 


 72: #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
 73: { \
 74:     if (col <= lastcol2) low2 = 0; else high2 = nrow2; \
 75:     lastcol2 = col;\
 76:     while (high2-low2 > 5) { \
 77:       t = (low2+high2)/2; \
 78:       if (rp2[t] > col) high2 = t; \
 79:       else             low2  = t; \
 80:     } \
 81:        for (_i=low2; _i<high2; _i++) { \
 82:         if (rp2[_i] > col) break; \
 83:         if (rp2[_i] == col) { \
 84:           if (addv == ADD_VALUES) ap2[_i] += value;   \
 85:           else                    ap2[_i] = value; \
 86:           goto b_noinsert; \
 87:         } \
 88:       }  \
 89:       if (value == 0.0 && ignorezeroentries) goto b_noinsert; \
 90:       if (nonew == 1) goto b_noinsert; \
 91:       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
 92:       MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew); \
 93:       N = nrow2++ - 1; b->nz++; high2++;\
 94:       /* shift up all the later entries in this row */ \
 95:       for (ii=N; ii>=_i; ii--) { \
 96:         rp2[ii+1] = rp2[ii]; \
 97:         ap2[ii+1] = ap2[ii]; \
 98:       } \
 99:       rp2[_i] = col;  \
100:       ap2[_i] = value;  \
101:       b_noinsert: ; \
102:       bilen[row] = nrow2; \
103: }

107: PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[])
108: {
109:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)A->data;
110:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data;
112:   PetscInt       l,*garray = mat->garray,diag;

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

117:   /* find size of row to the left of the diagonal part */
118:   MatGetOwnershipRange(A,&diag,0);
119:   row  = row - diag;
120:   for (l=0; l<b->i[row+1]-b->i[row]; l++) {
121:     if (garray[b->j[b->i[row]+l]] > diag) break;
122:   }
123:   PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));

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

128:   /* right of diagonal part */
129:   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));
130:   return(0);
131: }

135: PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
136: {
137:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
138:   PetscScalar    value;
140:   PetscInt       i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
141:   PetscInt       cstart = mat->cmap.rstart,cend = mat->cmap.rend,row,col;
142:   PetscTruth     roworiented = aij->roworiented;

144:   /* Some Variables required in the macro */
145:   Mat            A = aij->A;
146:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
147:   PetscInt       *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
148:   PetscScalar    *aa = a->a;
149:   PetscTruth     ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
150:   Mat            B = aij->B;
151:   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
152:   PetscInt       *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap.n,am = aij->A->rmap.n;
153:   PetscScalar    *ba = b->a;

155:   PetscInt       *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
156:   PetscInt       nonew = a->nonew;
157:   PetscScalar    *ap1,*ap2;

160:   for (i=0; i<m; i++) {
161:     if (im[i] < 0) continue;
162: #if defined(PETSC_USE_DEBUG)
163:     if (im[i] >= mat->rmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap.N-1);
164: #endif
165:     if (im[i] >= rstart && im[i] < rend) {
166:       row      = im[i] - rstart;
167:       lastcol1 = -1;
168:       rp1      = aj + ai[row];
169:       ap1      = aa + ai[row];
170:       rmax1    = aimax[row];
171:       nrow1    = ailen[row];
172:       low1     = 0;
173:       high1    = nrow1;
174:       lastcol2 = -1;
175:       rp2      = bj + bi[row];
176:       ap2      = ba + bi[row];
177:       rmax2    = bimax[row];
178:       nrow2    = bilen[row];
179:       low2     = 0;
180:       high2    = nrow2;

182:       for (j=0; j<n; j++) {
183:         if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
184:         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
185:         if (in[j] >= cstart && in[j] < cend){
186:           col = in[j] - cstart;
187:           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
188:         } else if (in[j] < 0) continue;
189: #if defined(PETSC_USE_DEBUG)
190:         else if (in[j] >= mat->cmap.N) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap.N-1);}
191: #endif
192:         else {
193:           if (mat->was_assembled) {
194:             if (!aij->colmap) {
195:               CreateColmap_MPIAIJ_Private(mat);
196:             }
197: #if defined (PETSC_USE_CTABLE)
198:             PetscTableFind(aij->colmap,in[j]+1,&col);
199:             col--;
200: #else
201:             col = aij->colmap[in[j]] - 1;
202: #endif
203:             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
204:               DisAssemble_MPIAIJ(mat);
205:               col =  in[j];
206:               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
207:               B = aij->B;
208:               b = (Mat_SeqAIJ*)B->data;
209:               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
210:               rp2      = bj + bi[row];
211:               ap2      = ba + bi[row];
212:               rmax2    = bimax[row];
213:               nrow2    = bilen[row];
214:               low2     = 0;
215:               high2    = nrow2;
216:               bm       = aij->B->rmap.n;
217:               ba = b->a;
218:             }
219:           } else col = in[j];
220:           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
221:         }
222:       }
223:     } else {
224:       if (!aij->donotstash) {
225:         if (roworiented) {
226:           if (ignorezeroentries && v[i*n] == 0.0) continue;
227:           MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);
228:         } else {
229:           if (ignorezeroentries && v[i] == 0.0) continue;
230:           MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);
231:         }
232:       }
233:     }
234:   }
235:   return(0);
236: }


241: PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
242: {
243:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
245:   PetscInt       i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
246:   PetscInt       cstart = mat->cmap.rstart,cend = mat->cmap.rend,row,col;

249:   for (i=0; i<m; i++) {
250:     if (idxm[i] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);
251:     if (idxm[i] >= mat->rmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",idxm[i],mat->rmap.N-1);
252:     if (idxm[i] >= rstart && idxm[i] < rend) {
253:       row = idxm[i] - rstart;
254:       for (j=0; j<n; j++) {
255:         if (idxn[j] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]);
256:         if (idxn[j] >= mat->cmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",idxn[j],mat->cmap.N-1);
257:         if (idxn[j] >= cstart && idxn[j] < cend){
258:           col = idxn[j] - cstart;
259:           MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);
260:         } else {
261:           if (!aij->colmap) {
262:             CreateColmap_MPIAIJ_Private(mat);
263:           }
264: #if defined (PETSC_USE_CTABLE)
265:           PetscTableFind(aij->colmap,idxn[j]+1,&col);
266:           col --;
267: #else
268:           col = aij->colmap[idxn[j]] - 1;
269: #endif
270:           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
271:           else {
272:             MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);
273:           }
274:         }
275:       }
276:     } else {
277:       SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
278:     }
279:   }
280:   return(0);
281: }

285: PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
286: {
287:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
289:   PetscInt       nstash,reallocs;
290:   InsertMode     addv;

293:   if (aij->donotstash) {
294:     return(0);
295:   }

297:   /* make sure all processors are either in INSERTMODE or ADDMODE */
298:   MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);
299:   if (addv == (ADD_VALUES|INSERT_VALUES)) {
300:     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
301:   }
302:   mat->insertmode = addv; /* in case this processor had no cache */

304:   MatStashScatterBegin_Private(&mat->stash,mat->rmap.range);
305:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
306:   PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
307:   return(0);
308: }

312: PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
313: {
314:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
315:   Mat_SeqAIJ     *a=(Mat_SeqAIJ *)aij->A->data;
317:   PetscMPIInt    n;
318:   PetscInt       i,j,rstart,ncols,flg;
319:   PetscInt       *row,*col,other_disassembled;
320:   PetscScalar    *val;
321:   InsertMode     addv = mat->insertmode;

323:   /* do not use 'b = (Mat_SeqAIJ *)aij->B->data' as B can be reset in disassembly */
325:   if (!aij->donotstash) {
326:     while (1) {
327:       MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
328:       if (!flg) break;

330:       for (i=0; i<n;) {
331:         /* Now identify the consecutive vals belonging to the same row */
332:         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
333:         if (j < n) ncols = j-i;
334:         else       ncols = n-i;
335:         /* Now assemble all these values with a single function call */
336:         MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);
337:         i = j;
338:       }
339:     }
340:     MatStashScatterEnd_Private(&mat->stash);
341:   }
342:   a->compressedrow.use     = PETSC_FALSE;
343:   MatAssemblyBegin(aij->A,mode);
344:   MatAssemblyEnd(aij->A,mode);

346:   /* determine if any processor has disassembled, if so we must 
347:      also disassemble ourselfs, in order that we may reassemble. */
348:   /*
349:      if nonzero structure of submatrix B cannot change then we know that
350:      no processor disassembled thus we can skip this stuff
351:   */
352:   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
353:     MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);
354:     if (mat->was_assembled && !other_disassembled) {
355:       DisAssemble_MPIAIJ(mat);
356:     }
357:   }
358:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
359:     MatSetUpMultiply_MPIAIJ(mat);
360:   }
361:   MatSetOption(aij->B,MAT_DO_NOT_USE_INODES);
362:   ((Mat_SeqAIJ *)aij->B->data)->compressedrow.use = PETSC_TRUE; /* b->compressedrow.use */
363:   MatAssemblyBegin(aij->B,mode);
364:   MatAssemblyEnd(aij->B,mode);

366:   PetscFree(aij->rowvalues);
367:   aij->rowvalues = 0;

369:   /* used by MatAXPY() */
370:   a->xtoy = 0; ((Mat_SeqAIJ *)aij->B->data)->xtoy = 0;  /* b->xtoy = 0 */
371:   a->XtoY = 0; ((Mat_SeqAIJ *)aij->B->data)->XtoY = 0;  /* b->XtoY = 0 */

373:   return(0);
374: }

378: PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
379: {
380:   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;

384:   MatZeroEntries(l->A);
385:   MatZeroEntries(l->B);
386:   return(0);
387: }

391: PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
392: {
393:   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
395:   PetscMPIInt    size = l->size,imdex,n,rank = l->rank,tag = A->tag,lastidx = -1;
396:   PetscInt       i,*owners = A->rmap.range;
397:   PetscInt       *nprocs,j,idx,nsends,row;
398:   PetscInt       nmax,*svalues,*starts,*owner,nrecvs;
399:   PetscInt       *rvalues,count,base,slen,*source;
400:   PetscInt       *lens,*lrows,*values,rstart=A->rmap.rstart;
401:   MPI_Comm       comm = A->comm;
402:   MPI_Request    *send_waits,*recv_waits;
403:   MPI_Status     recv_status,*send_status;
404: #if defined(PETSC_DEBUG)
405:   PetscTruth     found = PETSC_FALSE;
406: #endif

409:   /*  first count number of contributors to each processor */
410:   PetscMalloc(2*size*sizeof(PetscInt),&nprocs);
411:   PetscMemzero(nprocs,2*size*sizeof(PetscInt));
412:   PetscMalloc((N+1)*sizeof(PetscInt),&owner); /* see note*/
413:   j = 0;
414:   for (i=0; i<N; i++) {
415:     if (lastidx > (idx = rows[i])) j = 0;
416:     lastidx = idx;
417:     for (; j<size; j++) {
418:       if (idx >= owners[j] && idx < owners[j+1]) {
419:         nprocs[2*j]++;
420:         nprocs[2*j+1] = 1;
421:         owner[i] = j;
422: #if defined(PETSC_DEBUG)
423:         found = PETSC_TRUE;
424: #endif
425:         break;
426:       }
427:     }
428: #if defined(PETSC_DEBUG)
429:     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
430:     found = PETSC_FALSE;
431: #endif
432:   }
433:   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}

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

438:   /* post receives:   */
439:   PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);
440:   PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);
441:   for (i=0; i<nrecvs; i++) {
442:     MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);
443:   }

445:   /* do sends:
446:       1) starts[i] gives the starting index in svalues for stuff going to 
447:          the ith processor
448:   */
449:   PetscMalloc((N+1)*sizeof(PetscInt),&svalues);
450:   PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);
451:   PetscMalloc((size+1)*sizeof(PetscInt),&starts);
452:   starts[0] = 0;
453:   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
454:   for (i=0; i<N; i++) {
455:     svalues[starts[owner[i]]++] = rows[i];
456:   }

458:   starts[0] = 0;
459:   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
460:   count = 0;
461:   for (i=0; i<size; i++) {
462:     if (nprocs[2*i+1]) {
463:       MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);
464:     }
465:   }
466:   PetscFree(starts);

468:   base = owners[rank];

470:   /*  wait on receives */
471:   PetscMalloc(2*(nrecvs+1)*sizeof(PetscInt),&lens);
472:   source = lens + nrecvs;
473:   count  = nrecvs; slen = 0;
474:   while (count) {
475:     MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);
476:     /* unpack receives into our local space */
477:     MPI_Get_count(&recv_status,MPIU_INT,&n);
478:     source[imdex]  = recv_status.MPI_SOURCE;
479:     lens[imdex]    = n;
480:     slen          += n;
481:     count--;
482:   }
483:   PetscFree(recv_waits);
484: 
485:   /* move the data into the send scatter */
486:   PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);
487:   count = 0;
488:   for (i=0; i<nrecvs; i++) {
489:     values = rvalues + i*nmax;
490:     for (j=0; j<lens[i]; j++) {
491:       lrows[count++] = values[j] - base;
492:     }
493:   }
494:   PetscFree(rvalues);
495:   PetscFree(lens);
496:   PetscFree(owner);
497:   PetscFree(nprocs);
498: 
499:   /* actually zap the local rows */
500:   /*
501:         Zero the required rows. If the "diagonal block" of the matrix
502:      is square and the user wishes to set the diagonal we use separate
503:      code so that MatSetValues() is not called for each diagonal allocating
504:      new memory, thus calling lots of mallocs and slowing things down.

506:        Contributed by: Matthew Knepley
507:   */
508:   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
509:   MatZeroRows(l->B,slen,lrows,0.0);
510:   if ((diag != 0.0) && (l->A->rmap.N == l->A->cmap.N)) {
511:     MatZeroRows(l->A,slen,lrows,diag);
512:   } else if (diag != 0.0) {
513:     MatZeroRows(l->A,slen,lrows,0.0);
514:     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
515:       SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
516: MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
517:     }
518:     for (i = 0; i < slen; i++) {
519:       row  = lrows[i] + rstart;
520:       MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);
521:     }
522:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
523:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
524:   } else {
525:     MatZeroRows(l->A,slen,lrows,0.0);
526:   }
527:   PetscFree(lrows);

529:   /* wait on sends */
530:   if (nsends) {
531:     PetscMalloc(nsends*sizeof(MPI_Status),&send_status);
532:     MPI_Waitall(nsends,send_waits,send_status);
533:     PetscFree(send_status);
534:   }
535:   PetscFree(send_waits);
536:   PetscFree(svalues);

538:   return(0);
539: }

543: PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
544: {
545:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
547:   PetscInt       nt;

550:   VecGetLocalSize(xx,&nt);
551:   if (nt != A->cmap.n) {
552:     SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%D) and xx (%D)",A->cmap.n,nt);
553:   }
554:   VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
555:   (*a->A->ops->mult)(a->A,xx,yy);
556:   VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
557:   (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
558:   return(0);
559: }

563: PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
564: {
565:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

569:   VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
570:   (*a->A->ops->multadd)(a->A,xx,yy,zz);
571:   VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
572:   (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
573:   return(0);
574: }

578: PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
579: {
580:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
582:   PetscTruth     merged;

585:   VecScatterGetMerged(a->Mvctx,&merged);
586:   /* do nondiagonal part */
587:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
588:   if (!merged) {
589:     /* send it on its way */
590:     VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
591:     /* do local part */
592:     (*a->A->ops->multtranspose)(a->A,xx,yy);
593:     /* receive remote parts: note this assumes the values are not actually */
594:     /* added in yy until the next line, */
595:     VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
596:   } else {
597:     /* do local part */
598:     (*a->A->ops->multtranspose)(a->A,xx,yy);
599:     /* send it on its way */
600:     VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
601:     /* values actually were received in the Begin() but we need to call this nop */
602:     VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
603:   }
604:   return(0);
605: }

610: PetscErrorCode  MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscTruth *f)
611: {
612:   MPI_Comm       comm;
613:   Mat_MPIAIJ     *Aij = (Mat_MPIAIJ *) Amat->data, *Bij;
614:   Mat            Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
615:   IS             Me,Notme;
617:   PetscInt       M,N,first,last,*notme,i;
618:   PetscMPIInt    size;


622:   /* Easy test: symmetric diagonal block */
623:   Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A;
624:   MatIsTranspose(Adia,Bdia,tol,f);
625:   if (!*f) return(0);
626:   PetscObjectGetComm((PetscObject)Amat,&comm);
627:   MPI_Comm_size(comm,&size);
628:   if (size == 1) return(0);

630:   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
631:   MatGetSize(Amat,&M,&N);
632:   MatGetOwnershipRange(Amat,&first,&last);
633:   PetscMalloc((N-last+first)*sizeof(PetscInt),&notme);
634:   for (i=0; i<first; i++) notme[i] = i;
635:   for (i=last; i<M; i++) notme[i-last+first] = i;
636:   ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,&Notme);
637:   ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);
638:   MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);
639:   Aoff = Aoffs[0];
640:   MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);
641:   Boff = Boffs[0];
642:   MatIsTranspose(Aoff,Boff,tol,f);
643:   MatDestroyMatrices(1,&Aoffs);
644:   MatDestroyMatrices(1,&Boffs);
645:   ISDestroy(Me);
646:   ISDestroy(Notme);

648:   return(0);
649: }

654: PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
655: {
656:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

660:   /* do nondiagonal part */
661:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
662:   /* send it on its way */
663:   VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
664:   /* do local part */
665:   (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);
666:   /* receive remote parts */
667:   VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
668:   return(0);
669: }

671: /*
672:   This only works correctly for square matrices where the subblock A->A is the 
673:    diagonal block
674: */
677: PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
678: {
680:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

683:   if (A->rmap.N != A->cmap.N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
684:   if (A->rmap.rstart != A->cmap.rstart || A->rmap.rend != A->cmap.rend) {
685:     SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
686:   }
687:   MatGetDiagonal(a->A,v);
688:   return(0);
689: }

693: PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
694: {
695:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

699:   MatScale(a->A,aa);
700:   MatScale(a->B,aa);
701:   return(0);
702: }

706: PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
707: {
708:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;

712: #if defined(PETSC_USE_LOG)
713:   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap.N,mat->cmap.N);
714: #endif
715:   MatStashDestroy_Private(&mat->stash);
716:   MatDestroy(aij->A);
717:   MatDestroy(aij->B);
718: #if defined (PETSC_USE_CTABLE)
719:   if (aij->colmap) {PetscTableDelete(aij->colmap);}
720: #else
721:   PetscFree(aij->colmap);
722: #endif
723:   PetscFree(aij->garray);
724:   if (aij->lvec)   {VecDestroy(aij->lvec);}
725:   if (aij->Mvctx)  {VecScatterDestroy(aij->Mvctx);}
726:   PetscFree(aij->rowvalues);
727:   PetscFree(aij);

729:   PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);
730:   PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);
731:   PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);
732:   PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);
733:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);
734:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);
735:   PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);
736:   return(0);
737: }

741: PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
742: {
743:   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
744:   Mat_SeqAIJ*       A = (Mat_SeqAIJ*)aij->A->data;
745:   Mat_SeqAIJ*       B = (Mat_SeqAIJ*)aij->B->data;
746:   PetscErrorCode    ierr;
747:   PetscMPIInt       rank,size,tag = ((PetscObject)viewer)->tag;
748:   int               fd;
749:   PetscInt          nz,header[4],*row_lengths,*range=0,rlen,i;
750:   PetscInt          nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap.rstart,rnz;
751:   PetscScalar       *column_values;

754:   MPI_Comm_rank(mat->comm,&rank);
755:   MPI_Comm_size(mat->comm,&size);
756:   nz   = A->nz + B->nz;
757:   if (!rank) {
758:     header[0] = MAT_FILE_COOKIE;
759:     header[1] = mat->rmap.N;
760:     header[2] = mat->cmap.N;
761:     MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,mat->comm);
762:     PetscViewerBinaryGetDescriptor(viewer,&fd);
763:     PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);
764:     /* get largest number of rows any processor has */
765:     rlen = mat->rmap.n;
766:     range = mat->rmap.range;
767:     for (i=1; i<size; i++) {
768:       rlen = PetscMax(rlen,range[i+1] - range[i]);
769:     }
770:   } else {
771:     MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,mat->comm);
772:     rlen = mat->rmap.n;
773:   }

775:   /* load up the local row counts */
776:   PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);
777:   for (i=0; i<mat->rmap.n; i++) {
778:     row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
779:   }

781:   /* store the row lengths to the file */
782:   if (!rank) {
783:     MPI_Status status;
784:     PetscBinaryWrite(fd,row_lengths,mat->rmap.n,PETSC_INT,PETSC_TRUE);
785:     for (i=1; i<size; i++) {
786:       rlen = range[i+1] - range[i];
787:       MPI_Recv(row_lengths,rlen,MPIU_INT,i,tag,mat->comm,&status);
788:       PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);
789:     }
790:   } else {
791:     MPI_Send(row_lengths,mat->rmap.n,MPIU_INT,0,tag,mat->comm);
792:   }
793:   PetscFree(row_lengths);

795:   /* load up the local column indices */
796:   nzmax = nz; /* )th processor needs space a largest processor needs */
797:   MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,mat->comm);
798:   PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);
799:   cnt  = 0;
800:   for (i=0; i<mat->rmap.n; i++) {
801:     for (j=B->i[i]; j<B->i[i+1]; j++) {
802:       if ( (col = garray[B->j[j]]) > cstart) break;
803:       column_indices[cnt++] = col;
804:     }
805:     for (k=A->i[i]; k<A->i[i+1]; k++) {
806:       column_indices[cnt++] = A->j[k] + cstart;
807:     }
808:     for (; j<B->i[i+1]; j++) {
809:       column_indices[cnt++] = garray[B->j[j]];
810:     }
811:   }
812:   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz);

814:   /* store the column indices to the file */
815:   if (!rank) {
816:     MPI_Status status;
817:     PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);
818:     for (i=1; i<size; i++) {
819:       MPI_Recv(&rnz,1,MPIU_INT,i,tag,mat->comm,&status);
820:       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
821:       MPI_Recv(column_indices,rnz,MPIU_INT,i,tag,mat->comm,&status);
822:       PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);
823:     }
824:   } else {
825:     MPI_Send(&nz,1,MPIU_INT,0,tag,mat->comm);
826:     MPI_Send(column_indices,nz,MPIU_INT,0,tag,mat->comm);
827:   }
828:   PetscFree(column_indices);

830:   /* load up the local column values */
831:   PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);
832:   cnt  = 0;
833:   for (i=0; i<mat->rmap.n; i++) {
834:     for (j=B->i[i]; j<B->i[i+1]; j++) {
835:       if ( garray[B->j[j]] > cstart) break;
836:       column_values[cnt++] = B->a[j];
837:     }
838:     for (k=A->i[i]; k<A->i[i+1]; k++) {
839:       column_values[cnt++] = A->a[k];
840:     }
841:     for (; j<B->i[i+1]; j++) {
842:       column_values[cnt++] = B->a[j];
843:     }
844:   }
845:   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz);

847:   /* store the column values to the file */
848:   if (!rank) {
849:     MPI_Status status;
850:     PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);
851:     for (i=1; i<size; i++) {
852:       MPI_Recv(&rnz,1,MPIU_INT,i,tag,mat->comm,&status);
853:       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
854:       MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,mat->comm,&status);
855:       PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);
856:     }
857:   } else {
858:     MPI_Send(&nz,1,MPIU_INT,0,tag,mat->comm);
859:     MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,mat->comm);
860:   }
861:   PetscFree(column_values);
862:   return(0);
863: }

867: PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
868: {
869:   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
870:   PetscErrorCode    ierr;
871:   PetscMPIInt       rank = aij->rank,size = aij->size;
872:   PetscTruth        isdraw,iascii,isbinary;
873:   PetscViewer       sviewer;
874:   PetscViewerFormat format;

877:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
878:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
879:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
880:   if (iascii) {
881:     PetscViewerGetFormat(viewer,&format);
882:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
883:       MatInfo    info;
884:       PetscTruth inodes;

886:       MPI_Comm_rank(mat->comm,&rank);
887:       MatGetInfo(mat,MAT_LOCAL,&info);
888:       MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);
889:       if (!inodes) {
890:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n",
891:                                               rank,mat->rmap.n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);
892:       } else {
893:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n",
894:                     rank,mat->rmap.n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);
895:       }
896:       MatGetInfo(aij->A,MAT_LOCAL,&info);
897:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
898:       MatGetInfo(aij->B,MAT_LOCAL,&info);
899:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
900:       PetscViewerFlush(viewer);
901:       VecScatterView(aij->Mvctx,viewer);
902:       return(0);
903:     } else if (format == PETSC_VIEWER_ASCII_INFO) {
904:       PetscInt   inodecount,inodelimit,*inodes;
905:       MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);
906:       if (inodes) {
907:         PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);
908:       } else {
909:         PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");
910:       }
911:       return(0);
912:     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
913:       return(0);
914:     }
915:   } else if (isbinary) {
916:     if (size == 1) {
917:       PetscObjectSetName((PetscObject)aij->A,mat->name);
918:       MatView(aij->A,viewer);
919:     } else {
920:       MatView_MPIAIJ_Binary(mat,viewer);
921:     }
922:     return(0);
923:   } else if (isdraw) {
924:     PetscDraw  draw;
925:     PetscTruth isnull;
926:     PetscViewerDrawGetDraw(viewer,0,&draw);
927:     PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
928:   }

930:   if (size == 1) {
931:     PetscObjectSetName((PetscObject)aij->A,mat->name);
932:     MatView(aij->A,viewer);
933:   } else {
934:     /* assemble the entire matrix onto first processor. */
935:     Mat         A;
936:     Mat_SeqAIJ  *Aloc;
937:     PetscInt    M = mat->rmap.N,N = mat->cmap.N,m,*ai,*aj,row,*cols,i,*ct;
938:     PetscScalar *a;

940:     MatCreate(mat->comm,&A);
941:     if (!rank) {
942:       MatSetSizes(A,M,N,M,N);
943:     } else {
944:       MatSetSizes(A,0,0,M,N);
945:     }
946:     /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */
947:     MatSetType(A,MATMPIAIJ);
948:     MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);
949:     PetscLogObjectParent(mat,A);

951:     /* copy over the A part */
952:     Aloc = (Mat_SeqAIJ*)aij->A->data;
953:     m = aij->A->rmap.n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
954:     row = mat->rmap.rstart;
955:     for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap.rstart ;}
956:     for (i=0; i<m; i++) {
957:       MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);
958:       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
959:     }
960:     aj = Aloc->j;
961:     for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap.rstart;}

963:     /* copy over the B part */
964:     Aloc = (Mat_SeqAIJ*)aij->B->data;
965:     m    = aij->B->rmap.n;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
966:     row  = mat->rmap.rstart;
967:     PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);
968:     ct   = cols;
969:     for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];}
970:     for (i=0; i<m; i++) {
971:       MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);
972:       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
973:     }
974:     PetscFree(ct);
975:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
976:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
977:     /* 
978:        Everyone has to call to draw the matrix since the graphics waits are
979:        synchronized across all processors that share the PetscDraw object
980:     */
981:     PetscViewerGetSingleton(viewer,&sviewer);
982:     if (!rank) {
983:       PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);
984:       MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);
985:     }
986:     PetscViewerRestoreSingleton(viewer,&sviewer);
987:     MatDestroy(A);
988:   }
989:   return(0);
990: }

994: PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
995: {
997:   PetscTruth     iascii,isdraw,issocket,isbinary;
998: 
1000:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
1001:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
1002:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
1003:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);
1004:   if (iascii || isdraw || isbinary || issocket) {
1005:     MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);
1006:   } else {
1007:     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
1008:   }
1009:   return(0);
1010: }



1016: PetscErrorCode MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1017: {
1018:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1020:   Vec            bb1;

1023:   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);

1025:   VecDuplicate(bb,&bb1);

1027:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
1028:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1029:       (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);
1030:       its--;
1031:     }
1032: 
1033:     while (its--) {
1034:       VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);
1035:       VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);

1037:       /* update rhs: bb1 = bb - B*x */
1038:       VecScale(mat->lvec,-1.0);
1039:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1041:       /* local sweep */
1042:       (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
1043: 
1044:     }
1045:   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1046:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1047:       (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);
1048:       its--;
1049:     }
1050:     while (its--) {
1051:       VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);
1052:       VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);

1054:       /* update rhs: bb1 = bb - B*x */
1055:       VecScale(mat->lvec,-1.0);
1056:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1058:       /* local sweep */
1059:       (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1060: 
1061:     }
1062:   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1063:     if (flag & SOR_ZERO_INITIAL_GUESS) {
1064:       (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);
1065:       its--;
1066:     }
1067:     while (its--) {
1068:       VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);
1069:       VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);

1071:       /* update rhs: bb1 = bb - B*x */
1072:       VecScale(mat->lvec,-1.0);
1073:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

1075:       /* local sweep */
1076:       (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1077: 
1078:     }
1079:   } else {
1080:     SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
1081:   }

1083:   VecDestroy(bb1);
1084:   return(0);
1085: }

1089: PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
1090: {
1091:   MPI_Comm       comm,pcomm;
1092:   PetscInt       first,local_size,nrows,*rows;
1093:   int            ntids;
1094:   IS             crowp,growp,irowp,lrowp,lcolp,icolp;

1098:   PetscObjectGetComm((PetscObject)A,&comm);
1099:   /* make a collective version of 'rowp' */
1100:   PetscObjectGetComm((PetscObject)rowp,&pcomm);
1101:   if (pcomm==comm) {
1102:     crowp = rowp;
1103:   } else {
1104:     ISGetSize(rowp,&nrows);
1105:     ISGetIndices(rowp,&rows);
1106:     ISCreateGeneral(comm,nrows,rows,&crowp);
1107:     ISRestoreIndices(rowp,&rows);
1108:   }
1109:   /* collect the global row permutation and invert it */
1110:   ISAllGather(crowp,&growp);
1111:   ISSetPermutation(growp);
1112:   if (pcomm!=comm) {
1113:     ISDestroy(crowp);
1114:   }
1115:   ISInvertPermutation(growp,PETSC_DECIDE,&irowp);
1116:   /* get the local target indices */
1117:   MatGetOwnershipRange(A,&first,PETSC_NULL);
1118:   MatGetLocalSize(A,&local_size,PETSC_NULL);
1119:   ISGetIndices(irowp,&rows);
1120:   ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,&lrowp);
1121:   ISRestoreIndices(irowp,&rows);
1122:   ISDestroy(irowp);
1123:   /* the column permutation is so much easier;
1124:      make a local version of 'colp' and invert it */
1125:   PetscObjectGetComm((PetscObject)colp,&pcomm);
1126:   MPI_Comm_size(pcomm,&ntids);
1127:   if (ntids==1) {
1128:     lcolp = colp;
1129:   } else {
1130:     ISGetSize(colp,&nrows);
1131:     ISGetIndices(colp,&rows);
1132:     ISCreateGeneral(MPI_COMM_SELF,nrows,rows,&lcolp);
1133:   }
1134:   ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);
1135:   ISSetPermutation(lcolp);
1136:   if (ntids>1) {
1137:     ISRestoreIndices(colp,&rows);
1138:     ISDestroy(lcolp);
1139:   }
1140:   /* now we just get the submatrix */
1141:   MatGetSubMatrix(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B);
1142:   /* clean up */
1143:   ISDestroy(lrowp);
1144:   ISDestroy(icolp);
1145:   return(0);
1146: }

1150: PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1151: {
1152:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1153:   Mat            A = mat->A,B = mat->B;
1155:   PetscReal      isend[5],irecv[5];

1158:   info->block_size     = 1.0;
1159:   MatGetInfo(A,MAT_LOCAL,info);
1160:   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1161:   isend[3] = info->memory;  isend[4] = info->mallocs;
1162:   MatGetInfo(B,MAT_LOCAL,info);
1163:   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1164:   isend[3] += info->memory;  isend[4] += info->mallocs;
1165:   if (flag == MAT_LOCAL) {
1166:     info->nz_used      = isend[0];
1167:     info->nz_allocated = isend[1];
1168:     info->nz_unneeded  = isend[2];
1169:     info->memory       = isend[3];
1170:     info->mallocs      = isend[4];
1171:   } else if (flag == MAT_GLOBAL_MAX) {
1172:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);
1173:     info->nz_used      = irecv[0];
1174:     info->nz_allocated = irecv[1];
1175:     info->nz_unneeded  = irecv[2];
1176:     info->memory       = irecv[3];
1177:     info->mallocs      = irecv[4];
1178:   } else if (flag == MAT_GLOBAL_SUM) {
1179:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);
1180:     info->nz_used      = irecv[0];
1181:     info->nz_allocated = irecv[1];
1182:     info->nz_unneeded  = irecv[2];
1183:     info->memory       = irecv[3];
1184:     info->mallocs      = irecv[4];
1185:   }
1186:   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
1187:   info->fill_ratio_needed = 0;
1188:   info->factor_mallocs    = 0;
1189:   info->rows_global       = (double)matin->rmap.N;
1190:   info->columns_global    = (double)matin->cmap.N;
1191:   info->rows_local        = (double)matin->rmap.n;
1192:   info->columns_local     = (double)matin->cmap.N;

1194:   return(0);
1195: }

1199: PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op)
1200: {
1201:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

1205:   switch (op) {
1206:   case MAT_NO_NEW_NONZERO_LOCATIONS:
1207:   case MAT_YES_NEW_NONZERO_LOCATIONS:
1208:   case MAT_COLUMNS_UNSORTED:
1209:   case MAT_COLUMNS_SORTED:
1210:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1211:   case MAT_KEEP_ZEROED_ROWS:
1212:   case MAT_NEW_NONZERO_LOCATION_ERR:
1213:   case MAT_USE_INODES:
1214:   case MAT_DO_NOT_USE_INODES:
1215:   case MAT_IGNORE_ZERO_ENTRIES:
1216:     MatSetOption(a->A,op);
1217:     MatSetOption(a->B,op);
1218:     break;
1219:   case MAT_ROW_ORIENTED:
1220:     a->roworiented = PETSC_TRUE;
1221:     MatSetOption(a->A,op);
1222:     MatSetOption(a->B,op);
1223:     break;
1224:   case MAT_ROWS_SORTED:
1225:   case MAT_ROWS_UNSORTED:
1226:   case MAT_YES_NEW_DIAGONALS:
1227:     PetscInfo(A,"Option ignored\n");
1228:     break;
1229:   case MAT_COLUMN_ORIENTED:
1230:     a->roworiented = PETSC_FALSE;
1231:     MatSetOption(a->A,op);
1232:     MatSetOption(a->B,op);
1233:     break;
1234:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1235:     a->donotstash = PETSC_TRUE;
1236:     break;
1237:   case MAT_NO_NEW_DIAGONALS:
1238:     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
1239:   case MAT_SYMMETRIC:
1240:     MatSetOption(a->A,op);
1241:     break;
1242:   case MAT_STRUCTURALLY_SYMMETRIC:
1243:   case MAT_HERMITIAN:
1244:   case MAT_SYMMETRY_ETERNAL:
1245:     MatSetOption(a->A,op);
1246:     break;
1247:   case MAT_NOT_SYMMETRIC:
1248:   case MAT_NOT_STRUCTURALLY_SYMMETRIC:
1249:   case MAT_NOT_HERMITIAN:
1250:   case MAT_NOT_SYMMETRY_ETERNAL:
1251:     break;
1252:   default:
1253:     SETERRQ1(PETSC_ERR_SUP,"unknown option %d",op);
1254:   }
1255:   return(0);
1256: }

1260: PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1261: {
1262:   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1263:   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
1265:   PetscInt       i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap.rstart;
1266:   PetscInt       nztot,nzA,nzB,lrow,rstart = matin->rmap.rstart,rend = matin->rmap.rend;
1267:   PetscInt       *cmap,*idx_p;

1270:   if (mat->getrowactive) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
1271:   mat->getrowactive = PETSC_TRUE;

1273:   if (!mat->rowvalues && (idx || v)) {
1274:     /*
1275:         allocate enough space to hold information from the longest row.
1276:     */
1277:     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1278:     PetscInt     max = 1,tmp;
1279:     for (i=0; i<matin->rmap.n; i++) {
1280:       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1281:       if (max < tmp) { max = tmp; }
1282:     }
1283:     PetscMalloc(max*(sizeof(PetscInt)+sizeof(PetscScalar)),&mat->rowvalues);
1284:     mat->rowindices = (PetscInt*)(mat->rowvalues + max);
1285:   }

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

1290:   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1291:   if (!v)   {pvA = 0; pvB = 0;}
1292:   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1293:   (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1294:   (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1295:   nztot = nzA + nzB;

1297:   cmap  = mat->garray;
1298:   if (v  || idx) {
1299:     if (nztot) {
1300:       /* Sort by increasing column numbers, assuming A and B already sorted */
1301:       PetscInt imark = -1;
1302:       if (v) {
1303:         *v = v_p = mat->rowvalues;
1304:         for (i=0; i<nzB; i++) {
1305:           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1306:           else break;
1307:         }
1308:         imark = i;
1309:         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1310:         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1311:       }
1312:       if (idx) {
1313:         *idx = idx_p = mat->rowindices;
1314:         if (imark > -1) {
1315:           for (i=0; i<imark; i++) {
1316:             idx_p[i] = cmap[cworkB[i]];
1317:           }
1318:         } else {
1319:           for (i=0; i<nzB; i++) {
1320:             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1321:             else break;
1322:           }
1323:           imark = i;
1324:         }
1325:         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
1326:         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
1327:       }
1328:     } else {
1329:       if (idx) *idx = 0;
1330:       if (v)   *v   = 0;
1331:     }
1332:   }
1333:   *nz = nztot;
1334:   (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1335:   (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1336:   return(0);
1337: }

1341: PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1342: {
1343:   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;

1346:   if (!aij->getrowactive) {
1347:     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
1348:   }
1349:   aij->getrowactive = PETSC_FALSE;
1350:   return(0);
1351: }

1355: PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1356: {
1357:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1358:   Mat_SeqAIJ     *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1360:   PetscInt       i,j,cstart = mat->cmap.rstart;
1361:   PetscReal      sum = 0.0;
1362:   PetscScalar    *v;

1365:   if (aij->size == 1) {
1366:      MatNorm(aij->A,type,norm);
1367:   } else {
1368:     if (type == NORM_FROBENIUS) {
1369:       v = amat->a;
1370:       for (i=0; i<amat->nz; i++) {
1371: #if defined(PETSC_USE_COMPLEX)
1372:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1373: #else
1374:         sum += (*v)*(*v); v++;
1375: #endif
1376:       }
1377:       v = bmat->a;
1378:       for (i=0; i<bmat->nz; i++) {
1379: #if defined(PETSC_USE_COMPLEX)
1380:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1381: #else
1382:         sum += (*v)*(*v); v++;
1383: #endif
1384:       }
1385:       MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,mat->comm);
1386:       *norm = sqrt(*norm);
1387:     } else if (type == NORM_1) { /* max column norm */
1388:       PetscReal *tmp,*tmp2;
1389:       PetscInt    *jj,*garray = aij->garray;
1390:       PetscMalloc((mat->cmap.N+1)*sizeof(PetscReal),&tmp);
1391:       PetscMalloc((mat->cmap.N+1)*sizeof(PetscReal),&tmp2);
1392:       PetscMemzero(tmp,mat->cmap.N*sizeof(PetscReal));
1393:       *norm = 0.0;
1394:       v = amat->a; jj = amat->j;
1395:       for (j=0; j<amat->nz; j++) {
1396:         tmp[cstart + *jj++ ] += PetscAbsScalar(*v);  v++;
1397:       }
1398:       v = bmat->a; jj = bmat->j;
1399:       for (j=0; j<bmat->nz; j++) {
1400:         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
1401:       }
1402:       MPI_Allreduce(tmp,tmp2,mat->cmap.N,MPIU_REAL,MPI_SUM,mat->comm);
1403:       for (j=0; j<mat->cmap.N; j++) {
1404:         if (tmp2[j] > *norm) *norm = tmp2[j];
1405:       }
1406:       PetscFree(tmp);
1407:       PetscFree(tmp2);
1408:     } else if (type == NORM_INFINITY) { /* max row norm */
1409:       PetscReal ntemp = 0.0;
1410:       for (j=0; j<aij->A->rmap.n; j++) {
1411:         v = amat->a + amat->i[j];
1412:         sum = 0.0;
1413:         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1414:           sum += PetscAbsScalar(*v); v++;
1415:         }
1416:         v = bmat->a + bmat->i[j];
1417:         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1418:           sum += PetscAbsScalar(*v); v++;
1419:         }
1420:         if (sum > ntemp) ntemp = sum;
1421:       }
1422:       MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,mat->comm);
1423:     } else {
1424:       SETERRQ(PETSC_ERR_SUP,"No support for two norm");
1425:     }
1426:   }
1427:   return(0);
1428: }

1432: PetscErrorCode MatTranspose_MPIAIJ(Mat A,Mat *matout)
1433: {
1434:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1435:   Mat_SeqAIJ     *Aloc = (Mat_SeqAIJ*)a->A->data;
1437:   PetscInt       M = A->rmap.N,N = A->cmap.N,m,*ai,*aj,row,*cols,i,*ct;
1438:   Mat            B;
1439:   PetscScalar    *array;

1442:   if (!matout && M != N) {
1443:     SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1444:   }

1446:   MatCreate(A->comm,&B);
1447:   MatSetSizes(B,A->cmap.n,A->rmap.n,N,M);
1448:   MatSetType(B,A->type_name);
1449:   MatMPIAIJSetPreallocation(B,0,PETSC_NULL,0,PETSC_NULL);

1451:   /* copy over the A part */
1452:   Aloc = (Mat_SeqAIJ*)a->A->data;
1453:   m = a->A->rmap.n; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1454:   row = A->rmap.rstart;
1455:   for (i=0; i<ai[m]; i++) {aj[i] += A->cmap.rstart ;}
1456:   for (i=0; i<m; i++) {
1457:     MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);
1458:     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1459:   }
1460:   aj = Aloc->j;
1461:   for (i=0; i<ai[m]; i++) {aj[i] -= A->cmap.rstart ;}

1463:   /* copy over the B part */
1464:   Aloc = (Mat_SeqAIJ*)a->B->data;
1465:   m = a->B->rmap.n;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1466:   row  = A->rmap.rstart;
1467:   PetscMalloc((1+ai[m])*sizeof(PetscInt),&cols);
1468:   ct   = cols;
1469:   for (i=0; i<ai[m]; i++) {cols[i] = a->garray[aj[i]];}
1470:   for (i=0; i<m; i++) {
1471:     MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);
1472:     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1473:   }
1474:   PetscFree(ct);
1475:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
1476:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
1477:   if (matout) {
1478:     *matout = B;
1479:   } else {
1480:     MatHeaderCopy(A,B);
1481:   }
1482:   return(0);
1483: }

1487: PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1488: {
1489:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1490:   Mat            a = aij->A,b = aij->B;
1492:   PetscInt       s1,s2,s3;

1495:   MatGetLocalSize(mat,&s2,&s3);
1496:   if (rr) {
1497:     VecGetLocalSize(rr,&s1);
1498:     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
1499:     /* Overlap communication with computation. */
1500:     VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);
1501:   }
1502:   if (ll) {
1503:     VecGetLocalSize(ll,&s1);
1504:     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1505:     (*b->ops->diagonalscale)(b,ll,0);
1506:   }
1507:   /* scale  the diagonal block */
1508:   (*a->ops->diagonalscale)(a,ll,rr);

1510:   if (rr) {
1511:     /* Do a scatter end and then right scale the off-diagonal block */
1512:     VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);
1513:     (*b->ops->diagonalscale)(b,0,aij->lvec);
1514:   }
1515: 
1516:   return(0);
1517: }

1521: PetscErrorCode MatSetBlockSize_MPIAIJ(Mat A,PetscInt bs)
1522: {
1523:   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;

1527:   MatSetBlockSize(a->A,bs);
1528:   MatSetBlockSize(a->B,bs);
1529:   return(0);
1530: }
1533: PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
1534: {
1535:   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;

1539:   MatSetUnfactored(a->A);
1540:   return(0);
1541: }

1545: PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1546: {
1547:   Mat_MPIAIJ     *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1548:   Mat            a,b,c,d;
1549:   PetscTruth     flg;

1553:   a = matA->A; b = matA->B;
1554:   c = matB->A; d = matB->B;

1556:   MatEqual(a,c,&flg);
1557:   if (flg) {
1558:     MatEqual(b,d,&flg);
1559:   }
1560:   MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);
1561:   return(0);
1562: }

1566: PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1567: {
1569:   Mat_MPIAIJ     *a = (Mat_MPIAIJ *)A->data;
1570:   Mat_MPIAIJ     *b = (Mat_MPIAIJ *)B->data;

1573:   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1574:   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1575:     /* because of the column compression in the off-processor part of the matrix a->B,
1576:        the number of columns in a->B and b->B may be different, hence we cannot call
1577:        the MatCopy() directly on the two parts. If need be, we can provide a more 
1578:        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1579:        then copying the submatrices */
1580:     MatCopy_Basic(A,B,str);
1581:   } else {
1582:     MatCopy(a->A,b->A,str);
1583:     MatCopy(a->B,b->B,str);
1584:   }
1585:   return(0);
1586: }

1590: PetscErrorCode MatSetUpPreallocation_MPIAIJ(Mat A)
1591: {

1595:    MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
1596:   return(0);
1597: }

1599:  #include petscblaslapack.h
1602: PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
1603: {
1605:   PetscInt       i;
1606:   Mat_MPIAIJ     *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
1607:   PetscBLASInt   bnz,one=1;
1608:   Mat_SeqAIJ     *x,*y;

1611:   if (str == SAME_NONZERO_PATTERN) {
1612:     PetscScalar alpha = a;
1613:     x = (Mat_SeqAIJ *)xx->A->data;
1614:     y = (Mat_SeqAIJ *)yy->A->data;
1615:     bnz = (PetscBLASInt)x->nz;
1616:     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1617:     x = (Mat_SeqAIJ *)xx->B->data;
1618:     y = (Mat_SeqAIJ *)yy->B->data;
1619:     bnz = (PetscBLASInt)x->nz;
1620:     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1621:   } else if (str == SUBSET_NONZERO_PATTERN) {
1622:     MatAXPY_SeqAIJ(yy->A,a,xx->A,str);

1624:     x = (Mat_SeqAIJ *)xx->B->data;
1625:     y = (Mat_SeqAIJ *)yy->B->data;
1626:     if (y->xtoy && y->XtoY != xx->B) {
1627:       PetscFree(y->xtoy);
1628:       MatDestroy(y->XtoY);
1629:     }
1630:     if (!y->xtoy) { /* get xtoy */
1631:       MatAXPYGetxtoy_Private(xx->B->rmap.n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);
1632:       y->XtoY = xx->B;
1633:     }
1634:     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
1635:   } else {
1636:     MatAXPY_Basic(Y,a,X,str);
1637:   }
1638:   return(0);
1639: }

1641: EXTERN PetscErrorCode  MatConjugate_SeqAIJ(Mat);

1645: PetscErrorCode  MatConjugate_MPIAIJ(Mat mat)
1646: {
1647: #if defined(PETSC_USE_COMPLEX)
1649:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;

1652:   MatConjugate_SeqAIJ(aij->A);
1653:   MatConjugate_SeqAIJ(aij->B);
1654: #else
1656: #endif
1657:   return(0);
1658: }

1662: PetscErrorCode MatRealPart_MPIAIJ(Mat A)
1663: {
1664:   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;

1668:   MatRealPart(a->A);
1669:   MatRealPart(a->B);
1670:   return(0);
1671: }

1675: PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
1676: {
1677:   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;

1681:   MatImaginaryPart(a->A);
1682:   MatImaginaryPart(a->B);
1683:   return(0);
1684: }

1686: #ifdef PETSC_HAVE_PBGL
1687: #include <boost/parallel/mpi/bsp_process_group.hpp>
1688: typedef boost::parallel::mpi::bsp_process_group            process_group_type;

1690: #include <boost/graph/distributed/adjacency_list.hpp>
1691: #include <boost/parallel/mpi/bsp_process_group.hpp>

1693: #include <boost/graph/distributed/petsc/interface.hpp>
1694: #include <boost/graph/distributed/ilu_0.hpp>

1696: namespace petsc = boost::distributed::petsc;
1697: using namespace std;
1698: typedef double                                             value_type;
1699: typedef boost::graph::distributed::ilu_elimination_state   elimination_state;
1700: typedef boost::adjacency_list<boost::listS,
1701:                        boost::distributedS<process_group_type, boost::vecS>,
1702:                        boost::bidirectionalS,
1703:                        // Vertex properties
1704:                        boost::no_property,
1705:                        // Edge properties
1706:                        boost::property<boost::edge_weight_t, value_type,
1707:                          boost::property<boost::edge_finished_t, elimination_state> > > graph_type;

1709: typedef boost::graph_traits<graph_type>::vertex_descriptor        vertex_type;
1710: typedef boost::graph_traits<graph_type>::edge_descriptor          edge_type;
1711: typedef boost::property_map<graph_type, boost::edge_weight_t>::type      weight_map_type;

1715: /*
1716:   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
1717: */
1718: PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat A, IS isrow, IS iscol, MatFactorInfo *info, Mat *fact)
1719: {
1720:   PetscTruth           row_identity, col_identity;
1721:   PetscObjectContainer c;
1722:   PetscInt             m, n, M, N;
1723:   PetscErrorCode       ierr;

1726:   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu");
1727:   ISIdentity(isrow, &row_identity);
1728:   ISIdentity(iscol, &col_identity);
1729:   if (!row_identity || !col_identity) {
1730:     SETERRQ(PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU");
1731:   }

1733:   process_group_type pg;
1734:   graph_type*        graph_p = new graph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg));
1735:   graph_type&        graph   = *graph_p;
1736:   petsc::read_matrix(A, graph, get(boost::edge_weight, graph));

1738:   //write_graphviz("petsc_matrix_as_graph.dot", graph, default_writer(), matrix_graph_writer<graph_type>(graph));
1739:   boost::property_map<graph_type, boost::edge_finished_t>::type finished = get(boost::edge_finished, graph);
1740:   BGL_FORALL_EDGES(e, graph, graph_type)
1741:     put(finished, e, boost::graph::distributed::unseen);

1743:   ilu_0(graph, get(boost::edge_weight, graph), get(boost::edge_finished, graph));

1745:   /* put together the new matrix */
1746:   MatCreate(A->comm, fact);
1747:   MatGetLocalSize(A, &m, &n);
1748:   MatGetSize(A, &M, &N);
1749:   MatSetSizes(*fact, m, n, M, N);
1750:   MatSetType(*fact, A->type_name);
1751:   MatAssemblyBegin(*fact, MAT_FINAL_ASSEMBLY);
1752:   MatAssemblyEnd(*fact, MAT_FINAL_ASSEMBLY);
1753:   (*fact)->factor = FACTOR_LU;

1755:   PetscObjectContainerCreate(A->comm, &c);
1756:   PetscObjectContainerSetPointer(c, graph_p);
1757:   PetscObjectCompose((PetscObject) (*fact), "graph", (PetscObject) c);
1758:   return(0);
1759: }

1763: PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat A, MatFactorInfo *info, Mat *B)
1764: {
1766:   return(0);
1767: }

1771: /*
1772:   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
1773: */
1774: PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x)
1775: {
1776:   graph_type*          graph_p;
1777:   PetscObjectContainer c;
1778:   PetscErrorCode       ierr;

1781:   PetscObjectQuery((PetscObject) A, "graph", (PetscObject *) &c);
1782:   PetscObjectContainerGetPointer(c, (void **) &graph_p);
1783:   VecCopy(b, x);
1784:   return(0);
1785: }
1786: #endif

1788: /* -------------------------------------------------------------------*/
1789: static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1790:        MatGetRow_MPIAIJ,
1791:        MatRestoreRow_MPIAIJ,
1792:        MatMult_MPIAIJ,
1793: /* 4*/ MatMultAdd_MPIAIJ,
1794:        MatMultTranspose_MPIAIJ,
1795:        MatMultTransposeAdd_MPIAIJ,
1796: #ifdef PETSC_HAVE_PBGL
1797:        MatSolve_MPIAIJ,
1798: #else
1799:        0,
1800: #endif
1801:        0,
1802:        0,
1803: /*10*/ 0,
1804:        0,
1805:        0,
1806:        MatRelax_MPIAIJ,
1807:        MatTranspose_MPIAIJ,
1808: /*15*/ MatGetInfo_MPIAIJ,
1809:        MatEqual_MPIAIJ,
1810:        MatGetDiagonal_MPIAIJ,
1811:        MatDiagonalScale_MPIAIJ,
1812:        MatNorm_MPIAIJ,
1813: /*20*/ MatAssemblyBegin_MPIAIJ,
1814:        MatAssemblyEnd_MPIAIJ,
1815:        0,
1816:        MatSetOption_MPIAIJ,
1817:        MatZeroEntries_MPIAIJ,
1818: /*25*/ MatZeroRows_MPIAIJ,
1819:        0,
1820: #ifdef PETSC_HAVE_PBGL
1821:        MatLUFactorNumeric_MPIAIJ,
1822: #else
1823:        0,
1824: #endif
1825:        0,
1826:        0,
1827: /*30*/ MatSetUpPreallocation_MPIAIJ,
1828: #ifdef PETSC_HAVE_PBGL
1829:        MatILUFactorSymbolic_MPIAIJ,
1830: #else
1831:        0,
1832: #endif
1833:        0,
1834:        0,
1835:        0,
1836: /*35*/ MatDuplicate_MPIAIJ,
1837:        0,
1838:        0,
1839:        0,
1840:        0,
1841: /*40*/ MatAXPY_MPIAIJ,
1842:        MatGetSubMatrices_MPIAIJ,
1843:        MatIncreaseOverlap_MPIAIJ,
1844:        MatGetValues_MPIAIJ,
1845:        MatCopy_MPIAIJ,
1846: /*45*/ 0,
1847:        MatScale_MPIAIJ,
1848:        0,
1849:        0,
1850:        0,
1851: /*50*/ MatSetBlockSize_MPIAIJ,
1852:        0,
1853:        0,
1854:        0,
1855:        0,
1856: /*55*/ MatFDColoringCreate_MPIAIJ,
1857:        0,
1858:        MatSetUnfactored_MPIAIJ,
1859:        MatPermute_MPIAIJ,
1860:        0,
1861: /*60*/ MatGetSubMatrix_MPIAIJ,
1862:        MatDestroy_MPIAIJ,
1863:        MatView_MPIAIJ,
1864:        0,
1865:        0,
1866: /*65*/ 0,
1867:        0,
1868:        0,
1869:        0,
1870:        0,
1871: /*70*/ 0,
1872:        0,
1873:        MatSetColoring_MPIAIJ,
1874: #if defined(PETSC_HAVE_ADIC)
1875:        MatSetValuesAdic_MPIAIJ,
1876: #else
1877:        0,
1878: #endif
1879:        MatSetValuesAdifor_MPIAIJ,
1880: /*75*/ 0,
1881:        0,
1882:        0,
1883:        0,
1884:        0,
1885: /*80*/ 0,
1886:        0,
1887:        0,
1888:        0,
1889: /*84*/ MatLoad_MPIAIJ,
1890:        0,
1891:        0,
1892:        0,
1893:        0,
1894:        0,
1895: /*90*/ MatMatMult_MPIAIJ_MPIAIJ,
1896:        MatMatMultSymbolic_MPIAIJ_MPIAIJ,
1897:        MatMatMultNumeric_MPIAIJ_MPIAIJ,
1898:        MatPtAP_Basic,
1899:        MatPtAPSymbolic_MPIAIJ,
1900: /*95*/ MatPtAPNumeric_MPIAIJ,
1901:        0,
1902:        0,
1903:        0,
1904:        0,
1905: /*100*/0,
1906:        MatPtAPSymbolic_MPIAIJ_MPIAIJ,
1907:        MatPtAPNumeric_MPIAIJ_MPIAIJ,
1908:        MatConjugate_MPIAIJ,
1909:        0,
1910: /*105*/MatSetValuesRow_MPIAIJ,
1911:        MatRealPart_MPIAIJ,
1912:        MatImaginaryPart_MPIAIJ};

1914: /* ----------------------------------------------------------------------------------------*/

1919: PetscErrorCode  MatStoreValues_MPIAIJ(Mat mat)
1920: {
1921:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;

1925:   MatStoreValues(aij->A);
1926:   MatStoreValues(aij->B);
1927:   return(0);
1928: }

1934: PetscErrorCode  MatRetrieveValues_MPIAIJ(Mat mat)
1935: {
1936:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;

1940:   MatRetrieveValues(aij->A);
1941:   MatRetrieveValues(aij->B);
1942:   return(0);
1943: }

1946:  #include petscpc.h
1950: PetscErrorCode  MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
1951: {
1952:   Mat_MPIAIJ     *b;
1954:   PetscInt       i;

1957:   B->preallocated = PETSC_TRUE;
1958:   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
1959:   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
1960:   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz);
1961:   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz);

1963:   B->rmap.bs = B->cmap.bs = 1;
1964:   PetscMapInitialize(B->comm,&B->rmap);
1965:   PetscMapInitialize(B->comm,&B->cmap);
1966:   if (d_nnz) {
1967:     for (i=0; i<B->rmap.n; i++) {
1968:       if (d_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than 0: local row %D value %D",i,d_nnz[i]);
1969:     }
1970:   }
1971:   if (o_nnz) {
1972:     for (i=0; i<B->rmap.n; i++) {
1973:       if (o_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than 0: local row %D value %D",i,o_nnz[i]);
1974:     }
1975:   }
1976:   b = (Mat_MPIAIJ*)B->data;

1978:   /* Explicitly create 2 MATSEQAIJ matrices. */
1979:   MatCreate(PETSC_COMM_SELF,&b->A);
1980:   MatSetSizes(b->A,B->rmap.n,B->cmap.n,B->rmap.n,B->cmap.n);
1981:   MatSetType(b->A,MATSEQAIJ);
1982:   PetscLogObjectParent(B,b->A);
1983:   MatCreate(PETSC_COMM_SELF,&b->B);
1984:   MatSetSizes(b->B,B->rmap.n,B->cmap.N,B->rmap.n,B->cmap.N);
1985:   MatSetType(b->B,MATSEQAIJ);
1986:   PetscLogObjectParent(B,b->B);

1988:   MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);
1989:   MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);

1991:   return(0);
1992: }

1997: PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1998: {
1999:   Mat            mat;
2000:   Mat_MPIAIJ     *a,*oldmat = (Mat_MPIAIJ*)matin->data;

2004:   *newmat       = 0;
2005:   MatCreate(matin->comm,&mat);
2006:   MatSetSizes(mat,matin->rmap.n,matin->cmap.n,matin->rmap.N,matin->cmap.N);
2007:   MatSetType(mat,matin->type_name);
2008:   PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));
2009:   a    = (Mat_MPIAIJ*)mat->data;
2010: 
2011:   mat->factor       = matin->factor;
2012:   mat->rmap.bs      = matin->rmap.bs;
2013:   mat->assembled    = PETSC_TRUE;
2014:   mat->insertmode   = NOT_SET_VALUES;
2015:   mat->preallocated = PETSC_TRUE;

2017:   a->size           = oldmat->size;
2018:   a->rank           = oldmat->rank;
2019:   a->donotstash     = oldmat->donotstash;
2020:   a->roworiented    = oldmat->roworiented;
2021:   a->rowindices     = 0;
2022:   a->rowvalues      = 0;
2023:   a->getrowactive   = PETSC_FALSE;

2025:   PetscMapCopy(mat->comm,&matin->rmap,&mat->rmap);
2026:   PetscMapCopy(mat->comm,&matin->cmap,&mat->cmap);

2028:   MatStashCreate_Private(matin->comm,1,&mat->stash);
2029:   if (oldmat->colmap) {
2030: #if defined (PETSC_USE_CTABLE)
2031:     PetscTableCreateCopy(oldmat->colmap,&a->colmap);
2032: #else
2033:     PetscMalloc((mat->cmap.N)*sizeof(PetscInt),&a->colmap);
2034:     PetscLogObjectMemory(mat,(mat->cmap.N)*sizeof(PetscInt));
2035:     PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap.N)*sizeof(PetscInt));
2036: #endif
2037:   } else a->colmap = 0;
2038:   if (oldmat->garray) {
2039:     PetscInt len;
2040:     len  = oldmat->B->cmap.n;
2041:     PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);
2042:     PetscLogObjectMemory(mat,len*sizeof(PetscInt));
2043:     if (len) { PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt)); }
2044:   } else a->garray = 0;
2045: 
2046:   VecDuplicate(oldmat->lvec,&a->lvec);
2047:   PetscLogObjectParent(mat,a->lvec);
2048:   VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
2049:   PetscLogObjectParent(mat,a->Mvctx);
2050:   MatDuplicate(oldmat->A,cpvalues,&a->A);
2051:   PetscLogObjectParent(mat,a->A);
2052:   MatDuplicate(oldmat->B,cpvalues,&a->B);
2053:   PetscLogObjectParent(mat,a->B);
2054:   PetscFListDuplicate(matin->qlist,&mat->qlist);
2055:   *newmat = mat;
2056:   return(0);
2057: }

2059:  #include petscsys.h

2063: PetscErrorCode MatLoad_MPIAIJ(PetscViewer viewer, MatType type,Mat *newmat)
2064: {
2065:   Mat            A;
2066:   PetscScalar    *vals,*svals;
2067:   MPI_Comm       comm = ((PetscObject)viewer)->comm;
2068:   MPI_Status     status;
2070:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag,maxnz;
2071:   PetscInt       i,nz,j,rstart,rend,mmax;
2072:   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols;
2073:   PetscInt       *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols;
2074:   PetscInt       cend,cstart,n,*rowners;
2075:   int            fd;

2078:   MPI_Comm_size(comm,&size);
2079:   MPI_Comm_rank(comm,&rank);
2080:   if (!rank) {
2081:     PetscViewerBinaryGetDescriptor(viewer,&fd);
2082:     PetscBinaryRead(fd,(char *)header,4,PETSC_INT);
2083:     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
2084:   }

2086:   MPI_Bcast(header+1,3,MPIU_INT,0,comm);
2087:   M = header[1]; N = header[2];
2088:   /* determine ownership of all rows */
2089:   m    = M/size + ((M % size) > rank);
2090:   PetscMalloc((size+1)*sizeof(PetscInt),&rowners);
2091:   MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);

2093:   /* First process needs enough room for process with most rows */
2094:   if (!rank) {
2095:     mmax       = rowners[1];
2096:     for (i=2; i<size; i++) {
2097:       mmax = PetscMax(mmax,rowners[i]);
2098:     }
2099:   } else mmax = m;

2101:   rowners[0] = 0;
2102:   for (i=2; i<=size; i++) {
2103:     mmax       = PetscMax(mmax,rowners[i]);
2104:     rowners[i] += rowners[i-1];
2105:   }
2106:   rstart = rowners[rank];
2107:   rend   = rowners[rank+1];

2109:   /* distribute row lengths to all processors */
2110:   PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);
2111:   if (!rank) {
2112:     PetscBinaryRead(fd,ourlens,m,PETSC_INT);
2113:     PetscMalloc(m*sizeof(PetscInt),&rowlengths);
2114:     PetscMalloc(size*sizeof(PetscInt),&procsnz);
2115:     PetscMemzero(procsnz,size*sizeof(PetscInt));
2116:     for (j=0; j<m; j++) {
2117:       procsnz[0] += ourlens[j];
2118:     }
2119:     for (i=1; i<size; i++) {
2120:       PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);
2121:       /* calculate the number of nonzeros on each processor */
2122:       for (j=0; j<rowners[i+1]-rowners[i]; j++) {
2123:         procsnz[i] += rowlengths[j];
2124:       }
2125:       MPI_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);
2126:     }
2127:     PetscFree(rowlengths);
2128:   } else {
2129:     MPI_Recv(ourlens,m,MPIU_INT,0,tag,comm,&status);
2130:   }

2132:   if (!rank) {
2133:     /* determine max buffer needed and allocate it */
2134:     maxnz = 0;
2135:     for (i=0; i<size; i++) {
2136:       maxnz = PetscMax(maxnz,procsnz[i]);
2137:     }
2138:     PetscMalloc(maxnz*sizeof(PetscInt),&cols);

2140:     /* read in my part of the matrix column indices  */
2141:     nz   = procsnz[0];
2142:     PetscMalloc(nz*sizeof(PetscInt),&mycols);
2143:     PetscBinaryRead(fd,mycols,nz,PETSC_INT);

2145:     /* read in every one elses and ship off */
2146:     for (i=1; i<size; i++) {
2147:       nz   = procsnz[i];
2148:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
2149:       MPI_Send(cols,nz,MPIU_INT,i,tag,comm);
2150:     }
2151:     PetscFree(cols);
2152:   } else {
2153:     /* determine buffer space needed for message */
2154:     nz = 0;
2155:     for (i=0; i<m; i++) {
2156:       nz += ourlens[i];
2157:     }
2158:     PetscMalloc(nz*sizeof(PetscInt),&mycols);

2160:     /* receive message of column indices*/
2161:     MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);
2162:     MPI_Get_count(&status,MPIU_INT,&maxnz);
2163:     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2164:   }

2166:   /* determine column ownership if matrix is not square */
2167:   if (N != M) {
2168:     n      = N/size + ((N % size) > rank);
2169:     MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);
2170:     cstart = cend - n;
2171:   } else {
2172:     cstart = rstart;
2173:     cend   = rend;
2174:     n      = cend - cstart;
2175:   }

2177:   /* loop over local rows, determining number of off diagonal entries */
2178:   PetscMemzero(offlens,m*sizeof(PetscInt));
2179:   jj = 0;
2180:   for (i=0; i<m; i++) {
2181:     for (j=0; j<ourlens[i]; j++) {
2182:       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
2183:       jj++;
2184:     }
2185:   }

2187:   /* create our matrix */
2188:   for (i=0; i<m; i++) {
2189:     ourlens[i] -= offlens[i];
2190:   }
2191:   MatCreate(comm,&A);
2192:   MatSetSizes(A,m,n,M,N);
2193:   MatSetType(A,type);
2194:   MatMPIAIJSetPreallocation(A,0,ourlens,0,offlens);

2196:   MatSetOption(A,MAT_COLUMNS_SORTED);
2197:   for (i=0; i<m; i++) {
2198:     ourlens[i] += offlens[i];
2199:   }

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

2204:     /* read in my part of the matrix numerical values  */
2205:     nz   = procsnz[0];
2206:     PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2207: 
2208:     /* insert into matrix */
2209:     jj      = rstart;
2210:     smycols = mycols;
2211:     svals   = vals;
2212:     for (i=0; i<m; i++) {
2213:       MatSetValues_MPIAIJ(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
2214:       smycols += ourlens[i];
2215:       svals   += ourlens[i];
2216:       jj++;
2217:     }

2219:     /* read in other processors and ship out */
2220:     for (i=1; i<size; i++) {
2221:       nz   = procsnz[i];
2222:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2223:       MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);
2224:     }
2225:     PetscFree(procsnz);
2226:   } else {
2227:     /* receive numeric values */
2228:     PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);

2230:     /* receive message of values*/
2231:     MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);
2232:     MPI_Get_count(&status,MPIU_SCALAR,&maxnz);
2233:     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");

2235:     /* insert into matrix */
2236:     jj      = rstart;
2237:     smycols = mycols;
2238:     svals   = vals;
2239:     for (i=0; i<m; i++) {
2240:       MatSetValues_MPIAIJ(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
2241:       smycols += ourlens[i];
2242:       svals   += ourlens[i];
2243:       jj++;
2244:     }
2245:   }
2246:   PetscFree2(ourlens,offlens);
2247:   PetscFree(vals);
2248:   PetscFree(mycols);
2249:   PetscFree(rowners);

2251:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
2252:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
2253:   *newmat = A;
2254:   return(0);
2255: }

2259: /*
2260:     Not great since it makes two copies of the submatrix, first an SeqAIJ 
2261:   in local and then by concatenating the local matrices the end result.
2262:   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2263: */
2264: PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
2265: {
2267:   PetscMPIInt    rank,size;
2268:   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j;
2269:   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
2270:   Mat            *local,M,Mreuse;
2271:   PetscScalar    *vwork,*aa;
2272:   MPI_Comm       comm = mat->comm;
2273:   Mat_SeqAIJ     *aij;


2277:   MPI_Comm_rank(comm,&rank);
2278:   MPI_Comm_size(comm,&size);

2280:   if (call ==  MAT_REUSE_MATRIX) {
2281:     PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);
2282:     if (!Mreuse) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2283:     local = &Mreuse;
2284:     MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);
2285:   } else {
2286:     MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);
2287:     Mreuse = *local;
2288:     PetscFree(local);
2289:   }

2291:   /* 
2292:       m - number of local rows
2293:       n - number of columns (same on all processors)
2294:       rstart - first row in new global matrix generated
2295:   */
2296:   MatGetSize(Mreuse,&m,&n);
2297:   if (call == MAT_INITIAL_MATRIX) {
2298:     aij = (Mat_SeqAIJ*)(Mreuse)->data;
2299:     ii  = aij->i;
2300:     jj  = aij->j;

2302:     /*
2303:         Determine the number of non-zeros in the diagonal and off-diagonal 
2304:         portions of the matrix in order to do correct preallocation
2305:     */

2307:     /* first get start and end of "diagonal" columns */
2308:     if (csize == PETSC_DECIDE) {
2309:       ISGetSize(isrow,&mglobal);
2310:       if (mglobal == n) { /* square matrix */
2311:         nlocal = m;
2312:       } else {
2313:         nlocal = n/size + ((n % size) > rank);
2314:       }
2315:     } else {
2316:       nlocal = csize;
2317:     }
2318:     MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
2319:     rstart = rend - nlocal;
2320:     if (rank == size - 1 && rend != n) {
2321:       SETERRQ2(PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);
2322:     }

2324:     /* next, compute all the lengths */
2325:     PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);
2326:     olens = dlens + m;
2327:     for (i=0; i<m; i++) {
2328:       jend = ii[i+1] - ii[i];
2329:       olen = 0;
2330:       dlen = 0;
2331:       for (j=0; j<jend; j++) {
2332:         if (*jj < rstart || *jj >= rend) olen++;
2333:         else dlen++;
2334:         jj++;
2335:       }
2336:       olens[i] = olen;
2337:       dlens[i] = dlen;
2338:     }
2339:     MatCreate(comm,&M);
2340:     MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);
2341:     MatSetType(M,mat->type_name);
2342:     MatMPIAIJSetPreallocation(M,0,dlens,0,olens);
2343:     PetscFree(dlens);
2344:   } else {
2345:     PetscInt ml,nl;

2347:     M = *newmat;
2348:     MatGetLocalSize(M,&ml,&nl);
2349:     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2350:     MatZeroEntries(M);
2351:     /*
2352:          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2353:        rather than the slower MatSetValues().
2354:     */
2355:     M->was_assembled = PETSC_TRUE;
2356:     M->assembled     = PETSC_FALSE;
2357:   }
2358:   MatGetOwnershipRange(M,&rstart,&rend);
2359:   aij = (Mat_SeqAIJ*)(Mreuse)->data;
2360:   ii  = aij->i;
2361:   jj  = aij->j;
2362:   aa  = aij->a;
2363:   for (i=0; i<m; i++) {
2364:     row   = rstart + i;
2365:     nz    = ii[i+1] - ii[i];
2366:     cwork = jj;     jj += nz;
2367:     vwork = aa;     aa += nz;
2368:     MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);
2369:   }

2371:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
2372:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
2373:   *newmat = M;

2375:   /* save submatrix used in processor for next request */
2376:   if (call ==  MAT_INITIAL_MATRIX) {
2377:     PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);
2378:     PetscObjectDereference((PetscObject)Mreuse);
2379:   }

2381:   return(0);
2382: }

2387: PetscErrorCode  MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
2388: {
2389:   PetscInt       m,cstart, cend,j,nnz,i,d;
2390:   PetscInt       *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
2391:   const PetscInt *JJ;
2392:   PetscScalar    *values;

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

2398:   B->rmap.bs = B->cmap.bs = 1;
2399:   PetscMapInitialize(B->comm,&B->rmap);
2400:   PetscMapInitialize(B->comm,&B->cmap);
2401:   m      = B->rmap.n;
2402:   cstart = B->cmap.rstart;
2403:   cend   = B->cmap.rend;
2404:   rstart = B->rmap.rstart;

2406:   PetscMalloc((2*m+1)*sizeof(PetscInt),&d_nnz);
2407:   o_nnz = d_nnz + m;

2409:   for (i=0; i<m; i++) {
2410:     nnz     = Ii[i+1]- Ii[i];
2411:     JJ      = J + Ii[i];
2412:     nnz_max = PetscMax(nnz_max,nnz);
2413:     if (nnz < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
2414:     for (j=0; j<nnz; j++) {
2415:       if (*JJ >= cstart) break;
2416:       JJ++;
2417:     }
2418:     d = 0;
2419:     for (; j<nnz; j++) {
2420:       if (*JJ++ >= cend) break;
2421:       d++;
2422:     }
2423:     d_nnz[i] = d;
2424:     o_nnz[i] = nnz - d;
2425:   }
2426:   MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);
2427:   PetscFree(d_nnz);

2429:   if (v) values = (PetscScalar*)v;
2430:   else {
2431:     PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);
2432:     PetscMemzero(values,nnz_max*sizeof(PetscScalar));
2433:   }

2435:   MatSetOption(B,MAT_COLUMNS_SORTED);
2436:   for (i=0; i<m; i++) {
2437:     ii   = i + rstart;
2438:     nnz  = Ii[i+1]- Ii[i];
2439:     MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);
2440:   }
2441:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2442:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2443:   MatSetOption(B,MAT_COLUMNS_UNSORTED);

2445:   if (!v) {
2446:     PetscFree(values);
2447:   }
2448:   return(0);
2449: }

2454: /*@
2455:    MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
2456:    (the default parallel PETSc format).  

2458:    Collective on MPI_Comm

2460:    Input Parameters:
2461: +  B - the matrix 
2462: .  i - the indices into j for the start of each local row (starts with zero)
2463: .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2464: -  v - optional values in the matrix

2466:    Level: developer

2468:    Notes: this actually copies the values from i[], j[], and a[] to put them into PETSc's internal
2469:      storage format. Thus changing the values in a[] after this call will not effect the matrix values.

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

2473: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ,
2474:           MatCreateSeqAIJWithArrays()
2475: @*/
2476: PetscErrorCode  MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2477: {
2478:   PetscErrorCode ierr,(*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);

2481:   PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",(void (**)(void))&f);
2482:   if (f) {
2483:     (*f)(B,i,j,v);
2484:   }
2485:   return(0);
2486: }

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

2497:    Collective on MPI_Comm

2499:    Input Parameters:
2500: +  A - the matrix 
2501: .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2502:            (same value is used for all local rows)
2503: .  d_nnz - array containing the number of nonzeros in the various rows of the 
2504:            DIAGONAL portion of the local submatrix (possibly different for each row)
2505:            or PETSC_NULL, if d_nz is used to specify the nonzero structure. 
2506:            The size of this array is equal to the number of local rows, i.e 'm'. 
2507:            You must leave room for the diagonal entry even if it is zero.
2508: .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2509:            submatrix (same value is used for all local rows).
2510: -  o_nnz - array containing the number of nonzeros in the various rows of the
2511:            OFF-DIAGONAL portion of the local submatrix (possibly different for
2512:            each row) or PETSC_NULL, if o_nz is used to specify the nonzero 
2513:            structure. The size of this array is equal to the number 
2514:            of local rows, i.e 'm'. 

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

2518:    The AIJ format (also called the Yale sparse matrix format or
2519:    compressed row storage (CSR)), is fully compatible with standard Fortran 77
2520:    storage.  The stored row and column indices begin with zero.  See the users manual for details.

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

2526:    The DIAGONAL portion of the local submatrix of a processor can be defined 
2527:    as the submatrix which is obtained by extraction the part corresponding 
2528:    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the 
2529:    first row that belongs to the processor, and r2 is the last row belonging 
2530:    to the this processor. This is a square mxm matrix. The remaining portion 
2531:    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.

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

2535:    Example usage:
2536:   
2537:    Consider the following 8x8 matrix with 34 non-zero values, that is 
2538:    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2539:    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 
2540:    as follows:

2542: .vb
2543:             1  2  0  |  0  3  0  |  0  4
2544:     Proc0   0  5  6  |  7  0  0  |  8  0
2545:             9  0 10  | 11  0  0  | 12  0
2546:     -------------------------------------
2547:            13  0 14  | 15 16 17  |  0  0
2548:     Proc1   0 18  0  | 19 20 21  |  0  0 
2549:             0  0  0  | 22 23  0  | 24  0
2550:     -------------------------------------
2551:     Proc2  25 26 27  |  0  0 28  | 29  0
2552:            30  0  0  | 31 32 33  |  0 34
2553: .ve

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

2557: .vb
2558:       A B C
2559:       D E F
2560:       G H I
2561: .ve

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

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

2570:    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2571:    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2572:    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2573:    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2574:    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2575:    matrix, ans [DF] as another SeqAIJ matrix.

2577:    When d_nz, o_nz parameters are specified, d_nz storage elements are
2578:    allocated for every row of the local diagonal submatrix, and o_nz
2579:    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2580:    One way to choose d_nz and o_nz is to use the max nonzerors per local 
2581:    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 
2582:    In this case, the values of d_nz,o_nz are:
2583: .vb
2584:      proc0 : dnz = 2, o_nz = 2
2585:      proc1 : dnz = 3, o_nz = 2
2586:      proc2 : dnz = 1, o_nz = 4
2587: .ve
2588:    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2589:    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2590:    for proc3. i.e we are using 12+15+10=37 storage locations to store 
2591:    34 values.

2593:    When d_nnz, o_nnz parameters are specified, the storage is specified
2594:    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2595:    In the above case the values for d_nnz,o_nnz are:
2596: .vb
2597:      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2598:      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2599:      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2600: .ve
2601:    Here the space allocated is sum of all the above values i.e 34, and
2602:    hence pre-allocation is perfect.

2604:    Level: intermediate

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

2608: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIAIJ(), MatMPIAIJSetPreallocationCSR(),
2609:           MPIAIJ
2610: @*/
2611: PetscErrorCode  MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
2612: {
2613:   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]);

2616:   PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",(void (**)(void))&f);
2617:   if (f) {
2618:     (*f)(B,d_nz,d_nnz,o_nz,o_nnz);
2619:   }
2620:   return(0);
2621: }

2625: /*@C
2626:      MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard
2627:          CSR format the local rows.

2629:    Collective on MPI_Comm

2631:    Input Parameters:
2632: +  comm - MPI communicator
2633: .  m - number of local rows (Cannot be PETSC_DECIDE)
2634: .  n - This value should be the same as the local size used in creating the 
2635:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2636:        calculated if N is given) For square matrices n is almost always m.
2637: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2638: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2639: .   i - row indices
2640: .   j - column indices
2641: -   a - matrix values

2643:    Output Parameter:
2644: .   mat - the matrix
2645:    Level: intermediate

2647:    Notes:
2648:        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
2649:      thus you CANNOT change the matrix entries by changing the values of a[] after you have 
2650:      called this routine.

2652:        The i and j indices are 0 based

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

2656: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
2657:           MPIAIJ, MatCreateMPIAIJ()
2658: @*/
2659: PetscErrorCode  MatCreateMPIIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
2660: {

2664:   if (i[0]) {
2665:     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
2666:   }
2667:   if (m < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
2668:   MatCreate(comm,mat);
2669:   MatSetType(*mat,MATMPIAIJ);
2670:   MatMPIAIJSetPreallocationCSR(*mat,i,j,a);
2671:   return(0);
2672: }

2676: /*@C
2677:    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2678:    (the default parallel PETSc format).  For good matrix assembly performance
2679:    the user should preallocate the matrix storage by setting the parameters 
2680:    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2681:    performance can be increased by more than a factor of 50.

2683:    Collective on MPI_Comm

2685:    Input Parameters:
2686: +  comm - MPI communicator
2687: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2688:            This value should be the same as the local size used in creating the 
2689:            y vector for the matrix-vector product y = Ax.
2690: .  n - This value should be the same as the local size used in creating the 
2691:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2692:        calculated if N is given) For square matrices n is almost always m.
2693: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2694: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2695: .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2696:            (same value is used for all local rows)
2697: .  d_nnz - array containing the number of nonzeros in the various rows of the 
2698:            DIAGONAL portion of the local submatrix (possibly different for each row)
2699:            or PETSC_NULL, if d_nz is used to specify the nonzero structure. 
2700:            The size of this array is equal to the number of local rows, i.e 'm'. 
2701:            You must leave room for the diagonal entry even if it is zero.
2702: .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2703:            submatrix (same value is used for all local rows).
2704: -  o_nnz - array containing the number of nonzeros in the various rows of the
2705:            OFF-DIAGONAL portion of the local submatrix (possibly different for
2706:            each row) or PETSC_NULL, if o_nz is used to specify the nonzero 
2707:            structure. The size of this array is equal to the number 
2708:            of local rows, i.e 'm'. 

2710:    Output Parameter:
2711: .  A - the matrix 

2713:    Notes:
2714:    If the *_nnz parameter is given then the *_nz parameter is ignored

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

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

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

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

2731:    The DIAGONAL portion of the local submatrix of a processor can be defined 
2732:    as the submatrix which is obtained by extraction the part corresponding 
2733:    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the 
2734:    first row that belongs to the processor, and r2 is the last row belonging 
2735:    to the this processor. This is a square mxm matrix. The remaining portion 
2736:    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.

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

2740:    When calling this routine with a single process communicator, a matrix of
2741:    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
2742:    type of communicator, use the construction mechanism:
2743:      MatCreate(...,&A); MatSetType(A,MPIAIJ); MatMPIAIJSetPreallocation(A,...);

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

2749:    Options Database Keys:
2750: +  -mat_no_inode  - Do not use inodes
2751: .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2752: -  -mat_aij_oneindex - Internally use indexing starting at 1
2753:         rather than 0.  Note that when calling MatSetValues(),
2754:         the user still MUST index entries starting at 0!


2757:    Example usage:
2758:   
2759:    Consider the following 8x8 matrix with 34 non-zero values, that is 
2760:    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2761:    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 
2762:    as follows:

2764: .vb
2765:             1  2  0  |  0  3  0  |  0  4
2766:     Proc0   0  5  6  |  7  0  0  |  8  0
2767:             9  0 10  | 11  0  0  | 12  0
2768:     -------------------------------------
2769:            13  0 14  | 15 16 17  |  0  0
2770:     Proc1   0 18  0  | 19 20 21  |  0  0 
2771:             0  0  0  | 22 23  0  | 24  0
2772:     -------------------------------------
2773:     Proc2  25 26 27  |  0  0 28  | 29  0
2774:            30  0  0  | 31 32 33  |  0 34
2775: .ve

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

2779: .vb
2780:       A B C
2781:       D E F
2782:       G H I
2783: .ve

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

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

2792:    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2793:    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2794:    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2795:    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2796:    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2797:    matrix, ans [DF] as another SeqAIJ matrix.

2799:    When d_nz, o_nz parameters are specified, d_nz storage elements are
2800:    allocated for every row of the local diagonal submatrix, and o_nz
2801:    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2802:    One way to choose d_nz and o_nz is to use the max nonzerors per local 
2803:    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 
2804:    In this case, the values of d_nz,o_nz are:
2805: .vb
2806:      proc0 : dnz = 2, o_nz = 2
2807:      proc1 : dnz = 3, o_nz = 2
2808:      proc2 : dnz = 1, o_nz = 4
2809: .ve
2810:    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2811:    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2812:    for proc3. i.e we are using 12+15+10=37 storage locations to store 
2813:    34 values.

2815:    When d_nnz, o_nnz parameters are specified, the storage is specified
2816:    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2817:    In the above case the values for d_nnz,o_nnz are:
2818: .vb
2819:      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2820:      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2821:      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2822: .ve
2823:    Here the space allocated is sum of all the above values i.e 34, and
2824:    hence pre-allocation is perfect.

2826:    Level: intermediate

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

2830: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
2831:           MPIAIJ, MatCreateMPIAIJWithArrays()
2832: @*/
2833: PetscErrorCode  MatCreateMPIAIJ(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)
2834: {
2836:   PetscMPIInt    size;

2839:   MatCreate(comm,A);
2840:   MatSetSizes(*A,m,n,M,N);
2841:   MPI_Comm_size(comm,&size);
2842:   if (size > 1) {
2843:     MatSetType(*A,MATMPIAIJ);
2844:     MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);
2845:   } else {
2846:     MatSetType(*A,MATSEQAIJ);
2847:     MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);
2848:   }
2849:   return(0);
2850: }

2854: PetscErrorCode  MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[])
2855: {
2856:   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;

2859:   *Ad     = a->A;
2860:   *Ao     = a->B;
2861:   *colmap = a->garray;
2862:   return(0);
2863: }

2867: PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2868: {
2870:   PetscInt       i;
2871:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2874:   if (coloring->ctype == IS_COLORING_LOCAL) {
2875:     ISColoringValue *allcolors,*colors;
2876:     ISColoring      ocoloring;

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

2881:     /* set coloring for off-diagonal portion */
2882:     ISAllGatherColors(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);
2883:     PetscMalloc((a->B->cmap.n+1)*sizeof(ISColoringValue),&colors);
2884:     for (i=0; i<a->B->cmap.n; i++) {
2885:       colors[i] = allcolors[a->garray[i]];
2886:     }
2887:     PetscFree(allcolors);
2888:     ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap.n,colors,&ocoloring);
2889:     MatSetColoring_SeqAIJ(a->B,ocoloring);
2890:     ISColoringDestroy(ocoloring);
2891:   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
2892:     ISColoringValue *colors;
2893:     PetscInt        *larray;
2894:     ISColoring      ocoloring;

2896:     /* set coloring for diagonal portion */
2897:     PetscMalloc((a->A->cmap.n+1)*sizeof(PetscInt),&larray);
2898:     for (i=0; i<a->A->cmap.n; i++) {
2899:       larray[i] = i + A->cmap.rstart;
2900:     }
2901:     ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->cmap.n,larray,PETSC_NULL,larray);
2902:     PetscMalloc((a->A->cmap.n+1)*sizeof(ISColoringValue),&colors);
2903:     for (i=0; i<a->A->cmap.n; i++) {
2904:       colors[i] = coloring->colors[larray[i]];
2905:     }
2906:     PetscFree(larray);
2907:     ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap.n,colors,&ocoloring);
2908:     MatSetColoring_SeqAIJ(a->A,ocoloring);
2909:     ISColoringDestroy(ocoloring);

2911:     /* set coloring for off-diagonal portion */
2912:     PetscMalloc((a->B->cmap.n+1)*sizeof(PetscInt),&larray);
2913:     ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->cmap.n,a->garray,PETSC_NULL,larray);
2914:     PetscMalloc((a->B->cmap.n+1)*sizeof(ISColoringValue),&colors);
2915:     for (i=0; i<a->B->cmap.n; i++) {
2916:       colors[i] = coloring->colors[larray[i]];
2917:     }
2918:     PetscFree(larray);
2919:     ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap.n,colors,&ocoloring);
2920:     MatSetColoring_SeqAIJ(a->B,ocoloring);
2921:     ISColoringDestroy(ocoloring);
2922:   } else {
2923:     SETERRQ1(PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype);
2924:   }

2926:   return(0);
2927: }

2929: #if defined(PETSC_HAVE_ADIC)
2932: PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2933: {
2934:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2938:   MatSetValuesAdic_SeqAIJ(a->A,advalues);
2939:   MatSetValuesAdic_SeqAIJ(a->B,advalues);
2940:   return(0);
2941: }
2942: #endif

2946: PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues)
2947: {
2948:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2952:   MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);
2953:   MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);
2954:   return(0);
2955: }

2959: /*@C
2960:       MatMerge - Creates a single large PETSc matrix by concatinating sequential
2961:                  matrices from each processor

2963:     Collective on MPI_Comm

2965:    Input Parameters:
2966: +    comm - the communicators the parallel matrix will live on
2967: .    inmat - the input sequential matrices
2968: .    n - number of local columns (or PETSC_DECIDE)
2969: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

2971:    Output Parameter:
2972: .    outmat - the parallel matrix generated

2974:     Level: advanced

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

2978: @*/
2979: PetscErrorCode  MatMerge(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
2980: {
2982:   PetscInt       m,N,i,rstart,nnz,Ii,*dnz,*onz;
2983:   PetscInt       *indx;
2984:   PetscScalar    *values;

2987:   MatGetSize(inmat,&m,&N);
2988:   if (scall == MAT_INITIAL_MATRIX){
2989:     /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */
2990:     if (n == PETSC_DECIDE){
2991:       PetscSplitOwnership(comm,&n,&N);
2992:     }
2993:     MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);
2994:     rstart -= m;

2996:     MatPreallocateInitialize(comm,m,n,dnz,onz);
2997:     for (i=0;i<m;i++) {
2998:       MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);
2999:       MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);
3000:       MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);
3001:     }
3002:     /* This routine will ONLY return MPIAIJ type matrix */
3003:     MatCreate(comm,outmat);
3004:     MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
3005:     MatSetType(*outmat,MATMPIAIJ);
3006:     MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);
3007:     MatPreallocateFinalize(dnz,onz);
3008: 
3009:   } else if (scall == MAT_REUSE_MATRIX){
3010:     MatGetOwnershipRange(*outmat,&rstart,PETSC_NULL);
3011:   } else {
3012:     SETERRQ1(PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
3013:   }

3015:   for (i=0;i<m;i++) {
3016:     MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
3017:     Ii    = i + rstart;
3018:     MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);
3019:     MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
3020:   }
3021:   MatDestroy(inmat);
3022:   MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);
3023:   MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);

3025:   return(0);
3026: }

3030: PetscErrorCode MatFileSplit(Mat A,char *outfile)
3031: {
3032:   PetscErrorCode    ierr;
3033:   PetscMPIInt       rank;
3034:   PetscInt          m,N,i,rstart,nnz;
3035:   size_t            len;
3036:   const PetscInt    *indx;
3037:   PetscViewer       out;
3038:   char              *name;
3039:   Mat               B;
3040:   const PetscScalar *values;

3043:   MatGetLocalSize(A,&m,0);
3044:   MatGetSize(A,0,&N);
3045:   /* Should this be the type of the diagonal block of A? */
3046:   MatCreate(PETSC_COMM_SELF,&B);
3047:   MatSetSizes(B,m,N,m,N);
3048:   MatSetType(B,MATSEQAIJ);
3049:   MatSeqAIJSetPreallocation(B,0,PETSC_NULL);
3050:   MatGetOwnershipRange(A,&rstart,0);
3051:   for (i=0;i<m;i++) {
3052:     MatGetRow(A,i+rstart,&nnz,&indx,&values);
3053:     MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);
3054:     MatRestoreRow(A,i+rstart,&nnz,&indx,&values);
3055:   }
3056:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3057:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

3059:   MPI_Comm_rank(A->comm,&rank);
3060:   PetscStrlen(outfile,&len);
3061:   PetscMalloc((len+5)*sizeof(char),&name);
3062:   sprintf(name,"%s.%d",outfile,rank);
3063:   PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);
3064:   PetscFree(name);
3065:   MatView(B,out);
3066:   PetscViewerDestroy(out);
3067:   MatDestroy(B);
3068:   return(0);
3069: }

3071: EXTERN PetscErrorCode MatDestroy_MPIAIJ(Mat);
3074: PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
3075: {
3076:   PetscErrorCode       ierr;
3077:   Mat_Merge_SeqsToMPI  *merge;
3078:   PetscObjectContainer container;

3081:   PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);
3082:   if (container) {
3083:     PetscObjectContainerGetPointer(container,(void **)&merge);
3084:     PetscFree(merge->id_r);
3085:     PetscFree(merge->len_s);
3086:     PetscFree(merge->len_r);
3087:     PetscFree(merge->bi);
3088:     PetscFree(merge->bj);
3089:     PetscFree(merge->buf_ri);
3090:     PetscFree(merge->buf_rj);
3091:     PetscFree(merge->coi);
3092:     PetscFree(merge->coj);
3093:     PetscFree(merge->owners_co);
3094:     PetscFree(merge->rowmap.range);
3095: 
3096:     PetscObjectContainerDestroy(container);
3097:     PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);
3098:   }
3099:   PetscFree(merge);

3101:   MatDestroy_MPIAIJ(A);
3102:   return(0);
3103: }

3105:  #include src/mat/utils/freespace.h
3106:  #include petscbt.h
3107: static PetscEvent logkey_seqstompinum = 0;
3110: /*@C
3111:       MatMerge_SeqsToMPI - Creates a MPIAIJ matrix by adding sequential
3112:                  matrices from each processor

3114:     Collective on MPI_Comm

3116:    Input Parameters:
3117: +    comm - the communicators the parallel matrix will live on
3118: .    seqmat - the input sequential matrices
3119: .    m - number of local rows (or PETSC_DECIDE)
3120: .    n - number of local columns (or PETSC_DECIDE)
3121: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

3123:    Output Parameter:
3124: .    mpimat - the parallel matrix generated

3126:     Level: advanced

3128:    Notes: 
3129:      The dimensions of the sequential matrix in each processor MUST be the same.
3130:      The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
3131:      destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
3132: @*/
3133: PetscErrorCode  MatMerge_SeqsToMPINumeric(Mat seqmat,Mat mpimat)
3134: {
3135:   PetscErrorCode       ierr;
3136:   MPI_Comm             comm=mpimat->comm;
3137:   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
3138:   PetscMPIInt          size,rank,taga,*len_s;
3139:   PetscInt             N=mpimat->cmap.N,i,j,*owners,*ai=a->i,*aj=a->j;
3140:   PetscInt             proc,m;
3141:   PetscInt             **buf_ri,**buf_rj;
3142:   PetscInt             k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
3143:   PetscInt             nrows,**buf_ri_k,**nextrow,**nextai;
3144:   MPI_Request          *s_waits,*r_waits;
3145:   MPI_Status           *status;
3146:   MatScalar            *aa=a->a,**abuf_r,*ba_i;
3147:   Mat_Merge_SeqsToMPI  *merge;
3148:   PetscObjectContainer container;
3149: 
3151:   if (!logkey_seqstompinum) {
3153:   }

3156:   MPI_Comm_size(comm,&size);
3157:   MPI_Comm_rank(comm,&rank);

3159:   PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);
3160:   if (container) {
3161:     PetscObjectContainerGetPointer(container,(void **)&merge);
3162:   }
3163:   bi     = merge->bi;
3164:   bj     = merge->bj;
3165:   buf_ri = merge->buf_ri;
3166:   buf_rj = merge->buf_rj;

3168:   PetscMalloc(size*sizeof(MPI_Status),&status);
3169:   owners = merge->rowmap.range;
3170:   len_s  = merge->len_s;

3172:   /* send and recv matrix values */
3173:   /*-----------------------------*/
3174:   PetscObjectGetNewTag((PetscObject)mpimat,&taga);
3175:   PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);

3177:   PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);
3178:   for (proc=0,k=0; proc<size; proc++){
3179:     if (!len_s[proc]) continue;
3180:     i = owners[proc];
3181:     MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);
3182:     k++;
3183:   }

3185:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,r_waits,status);}
3186:   if (merge->nsend) {MPI_Waitall(merge->nsend,s_waits,status);}
3187:   PetscFree(status);

3189:   PetscFree(s_waits);
3190:   PetscFree(r_waits);

3192:   /* insert mat values of mpimat */
3193:   /*----------------------------*/
3194:   PetscMalloc(N*sizeof(MatScalar),&ba_i);
3195:   PetscMalloc((3*merge->nrecv+1)*sizeof(PetscInt**),&buf_ri_k);
3196:   nextrow = buf_ri_k + merge->nrecv;
3197:   nextai  = nextrow + merge->nrecv;

3199:   for (k=0; k<merge->nrecv; k++){
3200:     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
3201:     nrows = *(buf_ri_k[k]);
3202:     nextrow[k]  = buf_ri_k[k]+1;  /* next row number of k-th recved i-structure */
3203:     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
3204:   }

3206:   /* set values of ba */
3207:   m = merge->rowmap.n;
3208:   for (i=0; i<m; i++) {
3209:     arow = owners[rank] + i;
3210:     bj_i = bj+bi[i];  /* col indices of the i-th row of mpimat */
3211:     bnzi = bi[i+1] - bi[i];
3212:     PetscMemzero(ba_i,bnzi*sizeof(MatScalar));

3214:     /* add local non-zero vals of this proc's seqmat into ba */
3215:     anzi = ai[arow+1] - ai[arow];
3216:     aj   = a->j + ai[arow];
3217:     aa   = a->a + ai[arow];
3218:     nextaj = 0;
3219:     for (j=0; nextaj<anzi; j++){
3220:       if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
3221:         ba_i[j] += aa[nextaj++];
3222:       }
3223:     }

3225:     /* add received vals into ba */
3226:     for (k=0; k<merge->nrecv; k++){ /* k-th received message */
3227:       /* i-th row */
3228:       if (i == *nextrow[k]) {
3229:         anzi = *(nextai[k]+1) - *nextai[k];
3230:         aj   = buf_rj[k] + *(nextai[k]);
3231:         aa   = abuf_r[k] + *(nextai[k]);
3232:         nextaj = 0;
3233:         for (j=0; nextaj<anzi; j++){
3234:           if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
3235:             ba_i[j] += aa[nextaj++];
3236:           }
3237:         }
3238:         nextrow[k]++; nextai[k]++;
3239:       }
3240:     }
3241:     MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);
3242:   }
3243:   MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);
3244:   MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);

3246:   PetscFree(abuf_r);
3247:   PetscFree(ba_i);
3248:   PetscFree(buf_ri_k);
3250:   return(0);
3251: }

3253: static PetscEvent logkey_seqstompisym = 0;
3256: PetscErrorCode  MatMerge_SeqsToMPISymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
3257: {
3258:   PetscErrorCode       ierr;
3259:   Mat                  B_mpi;
3260:   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
3261:   PetscMPIInt          size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
3262:   PetscInt             **buf_rj,**buf_ri,**buf_ri_k;
3263:   PetscInt             M=seqmat->rmap.n,N=seqmat->cmap.n,i,*owners,*ai=a->i,*aj=a->j;
3264:   PetscInt             len,proc,*dnz,*onz;
3265:   PetscInt             k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
3266:   PetscInt             nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
3267:   MPI_Request          *si_waits,*sj_waits,*ri_waits,*rj_waits;
3268:   MPI_Status           *status;
3269:   PetscFreeSpaceList   free_space=PETSC_NULL,current_space=PETSC_NULL;
3270:   PetscBT              lnkbt;
3271:   Mat_Merge_SeqsToMPI  *merge;
3272:   PetscObjectContainer container;

3275:   if (!logkey_seqstompisym) {
3277:   }

3280:   /* make sure it is a PETSc comm */
3281:   PetscCommDuplicate(comm,&comm,PETSC_NULL);
3282:   MPI_Comm_size(comm,&size);
3283:   MPI_Comm_rank(comm,&rank);
3284: 
3285:   PetscNew(Mat_Merge_SeqsToMPI,&merge);
3286:   PetscMalloc(size*sizeof(MPI_Status),&status);

3288:   /* determine row ownership */
3289:   /*---------------------------------------------------------*/
3290:   merge->rowmap.n = m;
3291:   merge->rowmap.N = M;
3292:   merge->rowmap.bs = 1;
3293:   PetscMapInitialize(comm,&merge->rowmap);
3294:   PetscMalloc(size*sizeof(PetscMPIInt),&len_si);
3295:   PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);
3296: 
3297:   m      = merge->rowmap.n;
3298:   M      = merge->rowmap.N;
3299:   owners = merge->rowmap.range;

3301:   /* determine the number of messages to send, their lengths */
3302:   /*---------------------------------------------------------*/
3303:   len_s  = merge->len_s;

3305:   len = 0;  /* length of buf_si[] */
3306:   merge->nsend = 0;
3307:   for (proc=0; proc<size; proc++){
3308:     len_si[proc] = 0;
3309:     if (proc == rank){
3310:       len_s[proc] = 0;
3311:     } else {
3312:       len_si[proc] = owners[proc+1] - owners[proc] + 1;
3313:       len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
3314:     }
3315:     if (len_s[proc]) {
3316:       merge->nsend++;
3317:       nrows = 0;
3318:       for (i=owners[proc]; i<owners[proc+1]; i++){
3319:         if (ai[i+1] > ai[i]) nrows++;
3320:       }
3321:       len_si[proc] = 2*(nrows+1);
3322:       len += len_si[proc];
3323:     }
3324:   }

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

3331:   /* post the Irecv of j-structure */
3332:   /*-------------------------------*/
3333:   PetscCommGetNewTag(comm,&tagj);
3334:   PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);

3336:   /* post the Isend of j-structure */
3337:   /*--------------------------------*/
3338:   PetscMalloc((2*merge->nsend+1)*sizeof(MPI_Request),&si_waits);
3339:   sj_waits = si_waits + merge->nsend;

3341:   for (proc=0, k=0; proc<size; proc++){
3342:     if (!len_s[proc]) continue;
3343:     i = owners[proc];
3344:     MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);
3345:     k++;
3346:   }

3348:   /* receives and sends of j-structure are complete */
3349:   /*------------------------------------------------*/
3350:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,rj_waits,status);}
3351:   if (merge->nsend) {MPI_Waitall(merge->nsend,sj_waits,status);}
3352: 
3353:   /* send and recv i-structure */
3354:   /*---------------------------*/
3355:   PetscCommGetNewTag(comm,&tagi);
3356:   PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);
3357: 
3358:   PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);
3359:   buf_si = buf_s;  /* points to the beginning of k-th msg to be sent */
3360:   for (proc=0,k=0; proc<size; proc++){
3361:     if (!len_s[proc]) continue;
3362:     /* form outgoing message for i-structure: 
3363:          buf_si[0]:                 nrows to be sent
3364:                [1:nrows]:           row index (global)
3365:                [nrows+1:2*nrows+1]: i-structure index
3366:     */
3367:     /*-------------------------------------------*/
3368:     nrows = len_si[proc]/2 - 1;
3369:     buf_si_i    = buf_si + nrows+1;
3370:     buf_si[0]   = nrows;
3371:     buf_si_i[0] = 0;
3372:     nrows = 0;
3373:     for (i=owners[proc]; i<owners[proc+1]; i++){
3374:       anzi = ai[i+1] - ai[i];
3375:       if (anzi) {
3376:         buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
3377:         buf_si[nrows+1] = i-owners[proc]; /* local row index */
3378:         nrows++;
3379:       }
3380:     }
3381:     MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);
3382:     k++;
3383:     buf_si += len_si[proc];
3384:   }

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

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

3394:   PetscFree(len_si);
3395:   PetscFree(len_ri);
3396:   PetscFree(rj_waits);
3397:   PetscFree(si_waits);
3398:   PetscFree(ri_waits);
3399:   PetscFree(buf_s);
3400:   PetscFree(status);

3402:   /* compute a local seq matrix in each processor */
3403:   /*----------------------------------------------*/
3404:   /* allocate bi array and free space for accumulating nonzero column info */
3405:   PetscMalloc((m+1)*sizeof(PetscInt),&bi);
3406:   bi[0] = 0;

3408:   /* create and initialize a linked list */
3409:   nlnk = N+1;
3410:   PetscLLCreate(N,N,nlnk,lnk,lnkbt);
3411: 
3412:   /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
3413:   len = 0;
3414:   len  = ai[owners[rank+1]] - ai[owners[rank]];
3415:   PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);
3416:   current_space = free_space;

3418:   /* determine symbolic info for each local row */
3419:   PetscMalloc((3*merge->nrecv+1)*sizeof(PetscInt**),&buf_ri_k);
3420:   nextrow = buf_ri_k + merge->nrecv;
3421:   nextai  = nextrow + merge->nrecv;
3422:   for (k=0; k<merge->nrecv; k++){
3423:     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
3424:     nrows = *buf_ri_k[k];
3425:     nextrow[k]  = buf_ri_k[k] + 1;  /* next row number of k-th recved i-structure */
3426:     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
3427:   }

3429:   MatPreallocateInitialize(comm,m,n,dnz,onz);
3430:   len = 0;
3431:   for (i=0;i<m;i++) {
3432:     bnzi   = 0;
3433:     /* add local non-zero cols of this proc's seqmat into lnk */
3434:     arow   = owners[rank] + i;
3435:     anzi   = ai[arow+1] - ai[arow];
3436:     aj     = a->j + ai[arow];
3437:     PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);
3438:     bnzi += nlnk;
3439:     /* add received col data into lnk */
3440:     for (k=0; k<merge->nrecv; k++){ /* k-th received message */
3441:       if (i == *nextrow[k]) { /* i-th row */
3442:         anzi = *(nextai[k]+1) - *nextai[k];
3443:         aj   = buf_rj[k] + *nextai[k];
3444:         PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);
3445:         bnzi += nlnk;
3446:         nextrow[k]++; nextai[k]++;
3447:       }
3448:     }
3449:     if (len < bnzi) len = bnzi;  /* =max(bnzi) */

3451:     /* if free space is not available, make more free space */
3452:     if (current_space->local_remaining<bnzi) {
3453:       PetscFreeSpaceGet(current_space->total_array_size,&current_space);
3454:       nspacedouble++;
3455:     }
3456:     /* copy data into free space, then initialize lnk */
3457:     PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);
3458:     MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);

3460:     current_space->array           += bnzi;
3461:     current_space->local_used      += bnzi;
3462:     current_space->local_remaining -= bnzi;
3463: 
3464:     bi[i+1] = bi[i] + bnzi;
3465:   }
3466: 
3467:   PetscFree(buf_ri_k);

3469:   PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);
3470:   PetscFreeSpaceContiguous(&free_space,bj);
3471:   PetscLLDestroy(lnk,lnkbt);

3473:   /* create symbolic parallel matrix B_mpi */
3474:   /*---------------------------------------*/
3475:   MatCreate(comm,&B_mpi);
3476:   if (n==PETSC_DECIDE) {
3477:     MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);
3478:   } else {
3479:     MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
3480:   }
3481:   MatSetType(B_mpi,MATMPIAIJ);
3482:   MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);
3483:   MatPreallocateFinalize(dnz,onz);

3485:   /* B_mpi is not ready for use - assembly will be done by MatMerge_SeqsToMPINumeric() */
3486:   B_mpi->assembled     = PETSC_FALSE;
3487:   B_mpi->ops->destroy  = MatDestroy_MPIAIJ_SeqsToMPI;
3488:   merge->bi            = bi;
3489:   merge->bj            = bj;
3490:   merge->buf_ri        = buf_ri;
3491:   merge->buf_rj        = buf_rj;
3492:   merge->coi           = PETSC_NULL;
3493:   merge->coj           = PETSC_NULL;
3494:   merge->owners_co     = PETSC_NULL;

3496:   /* attach the supporting struct to B_mpi for reuse */
3497:   PetscObjectContainerCreate(PETSC_COMM_SELF,&container);
3498:   PetscObjectContainerSetPointer(container,merge);
3499:   PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);
3500:   *mpimat = B_mpi;

3502:   PetscCommDestroy(&comm);
3504:   return(0);
3505: }

3507: static PetscEvent logkey_seqstompi = 0;
3510: PetscErrorCode  MatMerge_SeqsToMPI(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
3511: {
3512:   PetscErrorCode   ierr;

3515:   if (!logkey_seqstompi) {
3517:   }
3519:   if (scall == MAT_INITIAL_MATRIX){
3520:     MatMerge_SeqsToMPISymbolic(comm,seqmat,m,n,mpimat);
3521:   }
3522:   MatMerge_SeqsToMPINumeric(seqmat,*mpimat);
3524:   return(0);
3525: }
3526: static PetscEvent logkey_getlocalmat = 0;
3529: /*@C
3530:      MatGetLocalMat - Creates a SeqAIJ matrix by taking all its local rows

3532:     Not Collective

3534:    Input Parameters:
3535: +    A - the matrix 
3536: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 

3538:    Output Parameter:
3539: .    A_loc - the local sequential matrix generated

3541:     Level: developer

3543: @*/
3544: PetscErrorCode  MatGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
3545: {
3546:   PetscErrorCode  ierr;
3547:   Mat_MPIAIJ      *mpimat=(Mat_MPIAIJ*)A->data;
3548:   Mat_SeqAIJ      *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data;
3549:   PetscInt        *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray;
3550:   PetscScalar     *aa=a->a,*ba=b->a,*ca;
3551:   PetscInt        am=A->rmap.n,i,j,k,cstart=A->cmap.rstart;
3552:   PetscInt        *ci,*cj,col,ncols_d,ncols_o,jo;

3555:   if (!logkey_getlocalmat) {
3557:   }
3559:   if (scall == MAT_INITIAL_MATRIX){
3560:     PetscMalloc((1+am)*sizeof(PetscInt),&ci);
3561:     ci[0] = 0;
3562:     for (i=0; i<am; i++){
3563:       ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
3564:     }
3565:     PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);
3566:     PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);
3567:     k = 0;
3568:     for (i=0; i<am; i++) {
3569:       ncols_o = bi[i+1] - bi[i];
3570:       ncols_d = ai[i+1] - ai[i];
3571:       /* off-diagonal portion of A */
3572:       for (jo=0; jo<ncols_o; jo++) {
3573:         col = cmap[*bj];
3574:         if (col >= cstart) break;
3575:         cj[k]   = col; bj++;
3576:         ca[k++] = *ba++;
3577:       }
3578:       /* diagonal portion of A */
3579:       for (j=0; j<ncols_d; j++) {
3580:         cj[k]   = cstart + *aj++;
3581:         ca[k++] = *aa++;
3582:       }
3583:       /* off-diagonal portion of A */
3584:       for (j=jo; j<ncols_o; j++) {
3585:         cj[k]   = cmap[*bj++];
3586:         ca[k++] = *ba++;
3587:       }
3588:     }
3589:     /* put together the new matrix */
3590:     MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap.N,ci,cj,ca,A_loc);
3591:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
3592:     /* Since these are PETSc arrays, change flags to free them as necessary. */
3593:     mat          = (Mat_SeqAIJ*)(*A_loc)->data;
3594:     mat->free_a  = PETSC_TRUE;
3595:     mat->free_ij = PETSC_TRUE;
3596:     mat->nonew   = 0;
3597:   } else if (scall == MAT_REUSE_MATRIX){
3598:     mat=(Mat_SeqAIJ*)(*A_loc)->data;
3599:     ci = mat->i; cj = mat->j; ca = mat->a;
3600:     for (i=0; i<am; i++) {
3601:       /* off-diagonal portion of A */
3602:       ncols_o = bi[i+1] - bi[i];
3603:       for (jo=0; jo<ncols_o; jo++) {
3604:         col = cmap[*bj];
3605:         if (col >= cstart) break;
3606:         *ca++ = *ba++; bj++;
3607:       }
3608:       /* diagonal portion of A */
3609:       ncols_d = ai[i+1] - ai[i];
3610:       for (j=0; j<ncols_d; j++) *ca++ = *aa++;
3611:       /* off-diagonal portion of A */
3612:       for (j=jo; j<ncols_o; j++) {
3613:         *ca++ = *ba++; bj++;
3614:       }
3615:     }
3616:   } else {
3617:     SETERRQ1(PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
3618:   }

3621:   return(0);
3622: }

3624: static PetscEvent logkey_getlocalmatcondensed = 0;
3627: /*@C
3628:      MatGetLocalMatCondensed - Creates a SeqAIJ matrix by taking all its local rows and NON-ZERO columns

3630:     Not Collective

3632:    Input Parameters:
3633: +    A - the matrix 
3634: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3635: -    row, col - index sets of rows and columns to extract (or PETSC_NULL)  

3637:    Output Parameter:
3638: .    A_loc - the local sequential matrix generated

3640:     Level: developer

3642: @*/
3643: PetscErrorCode  MatGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
3644: {
3645:   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
3646:   PetscErrorCode    ierr;
3647:   PetscInt          i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
3648:   IS                isrowa,iscola;
3649:   Mat               *aloc;

3652:   if (!logkey_getlocalmatcondensed) {
3654:   }
3656:   if (!row){
3657:     start = A->rmap.rstart; end = A->rmap.rend;
3658:     ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);
3659:   } else {
3660:     isrowa = *row;
3661:   }
3662:   if (!col){
3663:     start = A->cmap.rstart;
3664:     cmap  = a->garray;
3665:     nzA   = a->A->cmap.n;
3666:     nzB   = a->B->cmap.n;
3667:     PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);
3668:     ncols = 0;
3669:     for (i=0; i<nzB; i++) {
3670:       if (cmap[i] < start) idx[ncols++] = cmap[i];
3671:       else break;
3672:     }
3673:     imark = i;
3674:     for (i=0; i<nzA; i++) idx[ncols++] = start + i;
3675:     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
3676:     ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,&iscola);
3677:     PetscFree(idx);
3678:   } else {
3679:     iscola = *col;
3680:   }
3681:   if (scall != MAT_INITIAL_MATRIX){
3682:     PetscMalloc(sizeof(Mat),&aloc);
3683:     aloc[0] = *A_loc;
3684:   }
3685:   MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);
3686:   *A_loc = aloc[0];
3687:   PetscFree(aloc);
3688:   if (!row){
3689:     ISDestroy(isrowa);
3690:   }
3691:   if (!col){
3692:     ISDestroy(iscola);
3693:   }
3695:   return(0);
3696: }

3698: static PetscEvent logkey_GetBrowsOfAcols = 0;
3701: /*@C
3702:     MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A 

3704:     Collective on Mat

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

3711:    Output Parameter:
3712: +    rowb, colb - index sets of rows and columns of B to extract 
3713: .    brstart - row index of B_seq from which next B->rmap.n rows are taken from B's local rows
3714: -    B_seq - the sequential matrix generated

3716:     Level: developer

3718: @*/
3719: PetscErrorCode  MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,PetscInt *brstart,Mat *B_seq)
3720: {
3721:   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
3722:   PetscErrorCode    ierr;
3723:   PetscInt          *idx,i,start,ncols,nzA,nzB,*cmap,imark;
3724:   IS                isrowb,iscolb;
3725:   Mat               *bseq;
3726: 
3728:   if (A->cmap.rstart != B->rmap.rstart || A->cmap.rend != B->rmap.rend){
3729:     SETERRQ4(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);
3730:   }
3731:   if (!logkey_GetBrowsOfAcols) {
3733:   }
3735: 
3736:   if (scall == MAT_INITIAL_MATRIX){
3737:     start = A->cmap.rstart;
3738:     cmap  = a->garray;
3739:     nzA   = a->A->cmap.n;
3740:     nzB   = a->B->cmap.n;
3741:     PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);
3742:     ncols = 0;
3743:     for (i=0; i<nzB; i++) {  /* row < local row index */
3744:       if (cmap[i] < start) idx[ncols++] = cmap[i];
3745:       else break;
3746:     }
3747:     imark = i;
3748:     for (i=0; i<nzA; i++) idx[ncols++] = start + i;  /* local rows */
3749:     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
3750:     ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,&isrowb);
3751:     PetscFree(idx);
3752:     *brstart = imark;
3753:     ISCreateStride(PETSC_COMM_SELF,B->cmap.N,0,1,&iscolb);
3754:   } else {
3755:     if (!rowb || !colb) SETERRQ(PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
3756:     isrowb = *rowb; iscolb = *colb;
3757:     PetscMalloc(sizeof(Mat),&bseq);
3758:     bseq[0] = *B_seq;
3759:   }
3760:   MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);
3761:   *B_seq = bseq[0];
3762:   PetscFree(bseq);
3763:   if (!rowb){
3764:     ISDestroy(isrowb);
3765:   } else {
3766:     *rowb = isrowb;
3767:   }
3768:   if (!colb){
3769:     ISDestroy(iscolb);
3770:   } else {
3771:     *colb = iscolb;
3772:   }
3774:   return(0);
3775: }

3777: static PetscEvent logkey_GetBrowsOfAocols = 0;
3780: /*@C
3781:     MatGetBrowsOfAoCols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns
3782:     of the OFF-DIAGONAL portion of local A 

3784:     Collective on Mat

3786:    Input Parameters:
3787: +    A,B - the matrices in mpiaij format
3788: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3789: .    startsj - starting point in B's sending and receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL) 
3790: -    bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL) 

3792:    Output Parameter:
3793: +    B_oth - the sequential matrix generated

3795:     Level: developer

3797: @*/
3798: PetscErrorCode  MatGetBrowsOfAoCols(Mat A,Mat B,MatReuse scall,PetscInt **startsj,PetscScalar **bufa_ptr,Mat *B_oth)
3799: {
3800:   VecScatter_MPI_General *gen_to,*gen_from;
3801:   PetscErrorCode         ierr;
3802:   Mat_MPIAIJ             *a=(Mat_MPIAIJ*)A->data;
3803:   Mat_SeqAIJ             *b_oth;
3804:   VecScatter             ctx=a->Mvctx;
3805:   MPI_Comm               comm=ctx->comm;
3806:   PetscMPIInt            *rprocs,*sprocs,tag=ctx->tag,rank;
3807:   PetscInt               *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap.n,row,*b_othi,*b_othj;
3808:   PetscScalar            *rvalues,*svalues,*b_otha,*bufa,*bufA;
3809:   PetscInt               i,k,l,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len;
3810:   MPI_Request            *rwaits = PETSC_NULL,*swaits = PETSC_NULL;
3811:   MPI_Status             *sstatus,rstatus;
3812:   PetscInt               *cols;
3813:   PetscScalar            *vals;
3814:   PetscMPIInt            j;
3815: 
3817:   if (A->cmap.rstart != B->rmap.rstart || A->cmap.rend != B->rmap.rend){
3818:     SETERRQ4(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);
3819:   }
3820:   if (!logkey_GetBrowsOfAocols) {
3822:   }
3824:   MPI_Comm_rank(comm,&rank);

3826:   gen_to   = (VecScatter_MPI_General*)ctx->todata;
3827:   gen_from = (VecScatter_MPI_General*)ctx->fromdata;
3828:   rvalues  = gen_from->values; /* holds the length of sending row */
3829:   svalues  = gen_to->values;   /* holds the length of receiving row */
3830:   nrecvs   = gen_from->n;
3831:   nsends   = gen_to->n;

3833:   PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);
3834:   srow     = gen_to->indices;   /* local row index to be sent */
3835:   rstarts  = gen_from->starts;
3836:   sstarts  = gen_to->starts;
3837:   rprocs   = gen_from->procs;
3838:   sprocs   = gen_to->procs;
3839:   sstatus  = gen_to->sstatus;

3841:   if (!startsj || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
3842:   if (scall == MAT_INITIAL_MATRIX){
3843:     /* i-array */
3844:     /*---------*/
3845:     /*  post receives */
3846:     for (i=0; i<nrecvs; i++){
3847:       rowlen = (PetscInt*)rvalues + rstarts[i];
3848:       nrows = rstarts[i+1]-rstarts[i];
3849:       MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
3850:     }

3852:     /* pack the outgoing message */
3853:     PetscMalloc((nsends+nrecvs+3)*sizeof(PetscInt),&sstartsj);
3854:     rstartsj = sstartsj + nsends +1;
3855:     sstartsj[0] = 0;  rstartsj[0] = 0;
3856:     len = 0; /* total length of j or a array to be sent */
3857:     k = 0;
3858:     for (i=0; i<nsends; i++){
3859:       rowlen = (PetscInt*)svalues + sstarts[i];
3860:       nrows = sstarts[i+1]-sstarts[i]; /* num of rows */
3861:       for (j=0; j<nrows; j++) {
3862:         row = srow[k] + B->rmap.range[rank]; /* global row idx */
3863:         MatGetRow_MPIAIJ(B,row,&rowlen[j],PETSC_NULL,PETSC_NULL); /* rowlength */
3864:         len += rowlen[j];
3865:         MatRestoreRow_MPIAIJ(B,row,&ncols,PETSC_NULL,PETSC_NULL);
3866:         k++;
3867:       }
3868:       MPI_Isend(rowlen,nrows,MPIU_INT,sprocs[i],tag,comm,swaits+i);
3869:        sstartsj[i+1] = len;  /* starting point of (i+1)-th outgoing msg in bufj and bufa */
3870:     }
3871:     /* recvs and sends of i-array are completed */
3872:     i = nrecvs;
3873:     while (i--) {
3874:       MPI_Waitany(nrecvs,rwaits,&j,&rstatus);
3875:     }
3876:     if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
3877:     /* allocate buffers for sending j and a arrays */
3878:     PetscMalloc((len+1)*sizeof(PetscInt),&bufj);
3879:     PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);

3881:     /* create i-array of B_oth */
3882:     PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);
3883:     b_othi[0] = 0;
3884:     len = 0; /* total length of j or a array to be received */
3885:     k = 0;
3886:     for (i=0; i<nrecvs; i++){
3887:       rowlen = (PetscInt*)rvalues + rstarts[i];
3888:       nrows = rstarts[i+1]-rstarts[i];
3889:       for (j=0; j<nrows; j++) {
3890:         b_othi[k+1] = b_othi[k] + rowlen[j];
3891:         len += rowlen[j]; k++;
3892:       }
3893:       rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
3894:     }

3896:     /* allocate space for j and a arrrays of B_oth */
3897:     PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);
3898:     PetscMalloc((b_othi[aBn]+1)*sizeof(PetscScalar),&b_otha);

3900:     /* j-array */
3901:     /*---------*/
3902:     /*  post receives of j-array */
3903:     for (i=0; i<nrecvs; i++){
3904:       nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
3905:       MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
3906:     }
3907:     k = 0;
3908:     for (i=0; i<nsends; i++){
3909:       nrows = sstarts[i+1]-sstarts[i]; /* num of rows */
3910:       bufJ = bufj+sstartsj[i];
3911:       for (j=0; j<nrows; j++) {
3912:         row  = srow[k++] + B->rmap.range[rank]; /* global row idx */
3913:         MatGetRow_MPIAIJ(B,row,&ncols,&cols,PETSC_NULL);
3914:         for (l=0; l<ncols; l++){
3915:           *bufJ++ = cols[l];
3916:         }
3917:         MatRestoreRow_MPIAIJ(B,row,&ncols,&cols,PETSC_NULL);
3918:       }
3919:       MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);
3920:     }

3922:     /* recvs and sends of j-array are completed */
3923:     i = nrecvs;
3924:     while (i--) {
3925:       MPI_Waitany(nrecvs,rwaits,&j,&rstatus);
3926:     }
3927:     if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
3928:   } else if (scall == MAT_REUSE_MATRIX){
3929:     sstartsj = *startsj;
3930:     rstartsj = sstartsj + nsends +1;
3931:     bufa     = *bufa_ptr;
3932:     b_oth    = (Mat_SeqAIJ*)(*B_oth)->data;
3933:     b_otha   = b_oth->a;
3934:   } else {
3935:     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");
3936:   }

3938:   /* a-array */
3939:   /*---------*/
3940:   /*  post receives of a-array */
3941:   for (i=0; i<nrecvs; i++){
3942:     nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
3943:     MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);
3944:   }
3945:   k = 0;
3946:   for (i=0; i<nsends; i++){
3947:     nrows = sstarts[i+1]-sstarts[i];
3948:     bufA = bufa+sstartsj[i];
3949:     for (j=0; j<nrows; j++) {
3950:       row  = srow[k++] + B->rmap.range[rank]; /* global row idx */
3951:       MatGetRow_MPIAIJ(B,row,&ncols,PETSC_NULL,&vals);
3952:       for (l=0; l<ncols; l++){
3953:         *bufA++ = vals[l];
3954:       }
3955:       MatRestoreRow_MPIAIJ(B,row,&ncols,PETSC_NULL,&vals);

3957:     }
3958:     MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);
3959:   }
3960:   /* recvs and sends of a-array are completed */
3961:   i = nrecvs;
3962:   while (i--) {
3963:     MPI_Waitany(nrecvs,rwaits,&j,&rstatus);
3964:   }
3965:   if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
3966:   PetscFree2(rwaits,swaits);

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

3972:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
3973:     /* Since these are PETSc arrays, change flags to free them as necessary. */
3974:     b_oth          = (Mat_SeqAIJ *)(*B_oth)->data;
3975:     b_oth->free_a  = PETSC_TRUE;
3976:     b_oth->free_ij = PETSC_TRUE;
3977:     b_oth->nonew   = 0;

3979:     PetscFree(bufj);
3980:     if (!startsj || !bufa_ptr){
3981:       PetscFree(sstartsj);
3982:       PetscFree(bufa_ptr);
3983:     } else {
3984:       *startsj  = sstartsj;
3985:       *bufa_ptr = bufa;
3986:     }
3987:   }
3989: 
3990:   return(0);
3991: }

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

3998:   Not Collective

4000:   Input Parameters:
4001: . A - The matrix in mpiaij format

4003:   Output Parameter:
4004: + lvec - The local vector holding off-process values from the argument to a matrix-vector product
4005: . colmap - A map from global column index to local index into lvec
4006: - multScatter - A scatter from the argument of a matrix-vector product to lvec

4008:   Level: developer

4010: @*/
4011: #if defined (PETSC_USE_CTABLE)
4012: PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
4013: #else
4014: PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
4015: #endif
4016: {
4017:   Mat_MPIAIJ *a;

4024:   a = (Mat_MPIAIJ *) A->data;
4025:   if (lvec) *lvec = a->lvec;
4026:   if (colmap) *colmap = a->colmap;
4027:   if (multScatter) *multScatter = a->Mvctx;
4028:   return(0);
4029: }


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

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

4042:   Level: beginner

4044: .seealso: MatCreateMPIAIJ
4045: M*/

4050: PetscErrorCode  MatCreate_MPIAIJ(Mat B)
4051: {
4052:   Mat_MPIAIJ     *b;
4054:   PetscMPIInt    size;

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

4059:   PetscNew(Mat_MPIAIJ,&b);
4060:   B->data         = (void*)b;
4061:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
4062:   B->factor       = 0;
4063:   B->rmap.bs      = 1;
4064:   B->assembled    = PETSC_FALSE;
4065:   B->mapping      = 0;

4067:   B->insertmode      = NOT_SET_VALUES;
4068:   b->size            = size;
4069:   MPI_Comm_rank(B->comm,&b->rank);

4071:   /* build cache for off array entries formed */
4072:   MatStashCreate_Private(B->comm,1,&B->stash);
4073:   b->donotstash  = PETSC_FALSE;
4074:   b->colmap      = 0;
4075:   b->garray      = 0;
4076:   b->roworiented = PETSC_TRUE;

4078:   /* stuff used for matrix vector multiply */
4079:   b->lvec      = PETSC_NULL;
4080:   b->Mvctx     = PETSC_NULL;

4082:   /* stuff for MatGetRow() */
4083:   b->rowindices   = 0;
4084:   b->rowvalues    = 0;
4085:   b->getrowactive = PETSC_FALSE;


4088:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
4089:                                      "MatStoreValues_MPIAIJ",
4090:                                      MatStoreValues_MPIAIJ);
4091:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
4092:                                      "MatRetrieveValues_MPIAIJ",
4093:                                      MatRetrieveValues_MPIAIJ);
4094:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
4095:                                      "MatGetDiagonalBlock_MPIAIJ",
4096:                                      MatGetDiagonalBlock_MPIAIJ);
4097:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
4098:                                      "MatIsTranspose_MPIAIJ",
4099:                                      MatIsTranspose_MPIAIJ);
4100:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C",
4101:                                      "MatMPIAIJSetPreallocation_MPIAIJ",
4102:                                      MatMPIAIJSetPreallocation_MPIAIJ);
4103:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",
4104:                                      "MatMPIAIJSetPreallocationCSR_MPIAIJ",
4105:                                      MatMPIAIJSetPreallocationCSR_MPIAIJ);
4106:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
4107:                                      "MatDiagonalScaleLocal_MPIAIJ",
4108:                                      MatDiagonalScaleLocal_MPIAIJ);
4109:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpicsrperm_C",
4110:                                      "MatConvert_MPIAIJ_MPICSRPERM",
4111:                                       MatConvert_MPIAIJ_MPICSRPERM);
4112:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpicrl_C",
4113:                                      "MatConvert_MPIAIJ_MPICRL",
4114:                                       MatConvert_MPIAIJ_MPICRL);
4115:   PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);
4116:   return(0);
4117: }

4120: /*
4121:     Special version for direct calls from Fortran 
4122: */
4123: #if defined(PETSC_HAVE_FORTRAN_CAPS)
4124: #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
4125: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
4126: #define matsetvaluesmpiaij_ matsetvaluesmpiaij
4127: #endif

4129: /* Change these macros so can be used in void function */
4130: #undef CHKERRQ
4131: #define CHKERRQ(ierr) CHKERRABORT(mat->comm,ierr) 
4132: #undef SETERRQ2
4133: #define SETERRQ2(ierr,b,c,d) CHKERRABORT(mat->comm,ierr) 
4134: #undef SETERRQ
4135: #define SETERRQ(ierr,b) CHKERRABORT(mat->comm,ierr) 

4140: void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr)
4141: {
4142:   Mat            mat = *mmat;
4143:   PetscInt       m = *mm, n = *mn;
4144:   InsertMode     addv = *maddv;
4145:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
4146:   PetscScalar    value;

4149:   MatPreallocated(mat);
4150:   if (mat->insertmode == NOT_SET_VALUES) {
4151:     mat->insertmode = addv;
4152:   }
4153: #if defined(PETSC_USE_DEBUG)
4154:   else if (mat->insertmode != addv) {
4155:     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
4156:   }
4157: #endif
4158:   {
4159:   PetscInt       i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
4160:   PetscInt       cstart = mat->cmap.rstart,cend = mat->cmap.rend,row,col;
4161:   PetscTruth     roworiented = aij->roworiented;

4163:   /* Some Variables required in the macro */
4164:   Mat            A = aij->A;
4165:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
4166:   PetscInt       *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
4167:   PetscScalar    *aa = a->a;
4168:   PetscTruth     ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
4169:   Mat            B = aij->B;
4170:   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
4171:   PetscInt       *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap.n,am = aij->A->rmap.n;
4172:   PetscScalar    *ba = b->a;

4174:   PetscInt       *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
4175:   PetscInt       nonew = a->nonew;
4176:   PetscScalar    *ap1,*ap2;

4179:   for (i=0; i<m; i++) {
4180:     if (im[i] < 0) continue;
4181: #if defined(PETSC_USE_DEBUG)
4182:     if (im[i] >= mat->rmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap.N-1);
4183: #endif
4184:     if (im[i] >= rstart && im[i] < rend) {
4185:       row      = im[i] - rstart;
4186:       lastcol1 = -1;
4187:       rp1      = aj + ai[row];
4188:       ap1      = aa + ai[row];
4189:       rmax1    = aimax[row];
4190:       nrow1    = ailen[row];
4191:       low1     = 0;
4192:       high1    = nrow1;
4193:       lastcol2 = -1;
4194:       rp2      = bj + bi[row];
4195:       ap2      = ba + bi[row];
4196:       rmax2    = bimax[row];
4197:       nrow2    = bilen[row];
4198:       low2     = 0;
4199:       high2    = nrow2;

4201:       for (j=0; j<n; j++) {
4202:         if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
4203:         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
4204:         if (in[j] >= cstart && in[j] < cend){
4205:           col = in[j] - cstart;
4206:           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
4207:         } else if (in[j] < 0) continue;
4208: #if defined(PETSC_USE_DEBUG)
4209:         else if (in[j] >= mat->cmap.N) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap.N-1);}
4210: #endif
4211:         else {
4212:           if (mat->was_assembled) {
4213:             if (!aij->colmap) {
4214:               CreateColmap_MPIAIJ_Private(mat);
4215:             }
4216: #if defined (PETSC_USE_CTABLE)
4217:             PetscTableFind(aij->colmap,in[j]+1,&col);
4218:             col--;
4219: #else
4220:             col = aij->colmap[in[j]] - 1;
4221: #endif
4222:             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
4223:               DisAssemble_MPIAIJ(mat);
4224:               col =  in[j];
4225:               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
4226:               B = aij->B;
4227:               b = (Mat_SeqAIJ*)B->data;
4228:               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
4229:               rp2      = bj + bi[row];
4230:               ap2      = ba + bi[row];
4231:               rmax2    = bimax[row];
4232:               nrow2    = bilen[row];
4233:               low2     = 0;
4234:               high2    = nrow2;
4235:               bm       = aij->B->rmap.n;
4236:               ba = b->a;
4237:             }
4238:           } else col = in[j];
4239:           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
4240:         }
4241:       }
4242:     } else {
4243:       if (!aij->donotstash) {
4244:         if (roworiented) {
4245:           if (ignorezeroentries && v[i*n] == 0.0) continue;
4246:           MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);
4247:         } else {
4248:           if (ignorezeroentries && v[i] == 0.0) continue;
4249:           MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);
4250:         }
4251:       }
4252:     }
4253:   }}
4254:   PetscFunctionReturnVoid();
4255: }