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,1,nrow1,row,col,rmax1,aa,ai,aj,am,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,1,nrow2,row,col,rmax2,ba,bi,bj,bm,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 PETSCMAT_DLLEXPORT 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:   case MAT_STRUCTURALLY_SYMMETRIC:
1241:   case MAT_HERMITIAN:
1242:   case MAT_SYMMETRY_ETERNAL:
1243:     MatSetOption(a->A,op);
1244:     break;
1245:   case MAT_NOT_SYMMETRIC:
1246:   case MAT_NOT_STRUCTURALLY_SYMMETRIC:
1247:   case MAT_NOT_HERMITIAN:
1248:   case MAT_NOT_SYMMETRY_ETERNAL:
1249:     break;
1250:   default:
1251:     SETERRQ(PETSC_ERR_SUP,"unknown option");
1252:   }
1253:   return(0);
1254: }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


1520: PetscErrorCode MatPrintHelp_MPIAIJ(Mat A)
1521: {
1522:   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;

1526:   if (!a->rank) {
1527:     MatPrintHelp_SeqAIJ(a->A);
1528:   }
1529:   return(0);
1530: }

1534: PetscErrorCode MatSetBlockSize_MPIAIJ(Mat A,PetscInt bs)
1535: {
1536:   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;

1540:   MatSetBlockSize(a->A,bs);
1541:   MatSetBlockSize(a->B,bs);
1542:   return(0);
1543: }
1546: PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
1547: {
1548:   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;

1552:   MatSetUnfactored(a->A);
1553:   return(0);
1554: }

1558: PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1559: {
1560:   Mat_MPIAIJ     *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1561:   Mat            a,b,c,d;
1562:   PetscTruth     flg;

1566:   a = matA->A; b = matA->B;
1567:   c = matB->A; d = matB->B;

1569:   MatEqual(a,c,&flg);
1570:   if (flg) {
1571:     MatEqual(b,d,&flg);
1572:   }
1573:   MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);
1574:   return(0);
1575: }

1579: PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1580: {
1582:   Mat_MPIAIJ     *a = (Mat_MPIAIJ *)A->data;
1583:   Mat_MPIAIJ     *b = (Mat_MPIAIJ *)B->data;

1586:   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1587:   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1588:     /* because of the column compression in the off-processor part of the matrix a->B,
1589:        the number of columns in a->B and b->B may be different, hence we cannot call
1590:        the MatCopy() directly on the two parts. If need be, we can provide a more 
1591:        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1592:        then copying the submatrices */
1593:     MatCopy_Basic(A,B,str);
1594:   } else {
1595:     MatCopy(a->A,b->A,str);
1596:     MatCopy(a->B,b->B,str);
1597:   }
1598:   return(0);
1599: }

1603: PetscErrorCode MatSetUpPreallocation_MPIAIJ(Mat A)
1604: {

1608:    MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
1609:   return(0);
1610: }

1612:  #include petscblaslapack.h
1615: PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
1616: {
1618:   PetscInt       i;
1619:   Mat_MPIAIJ     *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
1620:   PetscBLASInt   bnz,one=1;
1621:   Mat_SeqAIJ     *x,*y;

1624:   if (str == SAME_NONZERO_PATTERN) {
1625:     PetscScalar alpha = a;
1626:     x = (Mat_SeqAIJ *)xx->A->data;
1627:     y = (Mat_SeqAIJ *)yy->A->data;
1628:     bnz = (PetscBLASInt)x->nz;
1629:     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1630:     x = (Mat_SeqAIJ *)xx->B->data;
1631:     y = (Mat_SeqAIJ *)yy->B->data;
1632:     bnz = (PetscBLASInt)x->nz;
1633:     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1634:   } else if (str == SUBSET_NONZERO_PATTERN) {
1635:     MatAXPY_SeqAIJ(yy->A,a,xx->A,str);

1637:     x = (Mat_SeqAIJ *)xx->B->data;
1638:     y = (Mat_SeqAIJ *)yy->B->data;
1639:     if (y->xtoy && y->XtoY != xx->B) {
1640:       PetscFree(y->xtoy);
1641:       MatDestroy(y->XtoY);
1642:     }
1643:     if (!y->xtoy) { /* get xtoy */
1644:       MatAXPYGetxtoy_Private(xx->B->rmap.n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);
1645:       y->XtoY = xx->B;
1646:     }
1647:     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
1648:   } else {
1649:     MatAXPY_Basic(Y,a,X,str);
1650:   }
1651:   return(0);
1652: }

1654: EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat);

1658: PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_MPIAIJ(Mat mat)
1659: {
1660: #if defined(PETSC_USE_COMPLEX)
1662:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;

1665:   MatConjugate_SeqAIJ(aij->A);
1666:   MatConjugate_SeqAIJ(aij->B);
1667: #else
1669: #endif
1670:   return(0);
1671: }

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

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

1688: PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
1689: {
1690:   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;

1694:   MatImaginaryPart(a->A);
1695:   MatImaginaryPart(a->B);
1696:   return(0);
1697: }

1699: /* -------------------------------------------------------------------*/
1700: static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1701:        MatGetRow_MPIAIJ,
1702:        MatRestoreRow_MPIAIJ,
1703:        MatMult_MPIAIJ,
1704: /* 4*/ MatMultAdd_MPIAIJ,
1705:        MatMultTranspose_MPIAIJ,
1706:        MatMultTransposeAdd_MPIAIJ,
1707:        0,
1708:        0,
1709:        0,
1710: /*10*/ 0,
1711:        0,
1712:        0,
1713:        MatRelax_MPIAIJ,
1714:        MatTranspose_MPIAIJ,
1715: /*15*/ MatGetInfo_MPIAIJ,
1716:        MatEqual_MPIAIJ,
1717:        MatGetDiagonal_MPIAIJ,
1718:        MatDiagonalScale_MPIAIJ,
1719:        MatNorm_MPIAIJ,
1720: /*20*/ MatAssemblyBegin_MPIAIJ,
1721:        MatAssemblyEnd_MPIAIJ,
1722:        0,
1723:        MatSetOption_MPIAIJ,
1724:        MatZeroEntries_MPIAIJ,
1725: /*25*/ MatZeroRows_MPIAIJ,
1726:        0,
1727:        0,
1728:        0,
1729:        0,
1730: /*30*/ MatSetUpPreallocation_MPIAIJ,
1731:        0,
1732:        0,
1733:        0,
1734:        0,
1735: /*35*/ MatDuplicate_MPIAIJ,
1736:        0,
1737:        0,
1738:        0,
1739:        0,
1740: /*40*/ MatAXPY_MPIAIJ,
1741:        MatGetSubMatrices_MPIAIJ,
1742:        MatIncreaseOverlap_MPIAIJ,
1743:        MatGetValues_MPIAIJ,
1744:        MatCopy_MPIAIJ,
1745: /*45*/ MatPrintHelp_MPIAIJ,
1746:        MatScale_MPIAIJ,
1747:        0,
1748:        0,
1749:        0,
1750: /*50*/ MatSetBlockSize_MPIAIJ,
1751:        0,
1752:        0,
1753:        0,
1754:        0,
1755: /*55*/ MatFDColoringCreate_MPIAIJ,
1756:        0,
1757:        MatSetUnfactored_MPIAIJ,
1758:        MatPermute_MPIAIJ,
1759:        0,
1760: /*60*/ MatGetSubMatrix_MPIAIJ,
1761:        MatDestroy_MPIAIJ,
1762:        MatView_MPIAIJ,
1763:        0,
1764:        0,
1765: /*65*/ 0,
1766:        0,
1767:        0,
1768:        0,
1769:        0,
1770: /*70*/ 0,
1771:        0,
1772:        MatSetColoring_MPIAIJ,
1773: #if defined(PETSC_HAVE_ADIC)
1774:        MatSetValuesAdic_MPIAIJ,
1775: #else
1776:        0,
1777: #endif
1778:        MatSetValuesAdifor_MPIAIJ,
1779: /*75*/ 0,
1780:        0,
1781:        0,
1782:        0,
1783:        0,
1784: /*80*/ 0,
1785:        0,
1786:        0,
1787:        0,
1788: /*84*/ MatLoad_MPIAIJ,
1789:        0,
1790:        0,
1791:        0,
1792:        0,
1793:        0,
1794: /*90*/ MatMatMult_MPIAIJ_MPIAIJ,
1795:        MatMatMultSymbolic_MPIAIJ_MPIAIJ,
1796:        MatMatMultNumeric_MPIAIJ_MPIAIJ,
1797:        MatPtAP_Basic,
1798:        MatPtAPSymbolic_MPIAIJ,
1799: /*95*/ MatPtAPNumeric_MPIAIJ,
1800:        0,
1801:        0,
1802:        0,
1803:        0,
1804: /*100*/0,
1805:        MatPtAPSymbolic_MPIAIJ_MPIAIJ,
1806:        MatPtAPNumeric_MPIAIJ_MPIAIJ,
1807:        MatConjugate_MPIAIJ,
1808:        0,
1809: /*105*/MatSetValuesRow_MPIAIJ,
1810:        MatRealPart_MPIAIJ,
1811:        MatImaginaryPart_MPIAIJ};

1813: /* ----------------------------------------------------------------------------------------*/

1818: PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_MPIAIJ(Mat mat)
1819: {
1820:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;

1824:   MatStoreValues(aij->A);
1825:   MatStoreValues(aij->B);
1826:   return(0);
1827: }

1833: PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_MPIAIJ(Mat mat)
1834: {
1835:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;

1839:   MatRetrieveValues(aij->A);
1840:   MatRetrieveValues(aij->B);
1841:   return(0);
1842: }

1845:  #include petscpc.h
1849: PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
1850: {
1851:   Mat_MPIAIJ     *b;
1853:   PetscInt       i;

1856:   B->preallocated = PETSC_TRUE;
1857:   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
1858:   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
1859:   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz);
1860:   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz);

1862:   B->rmap.bs = B->cmap.bs = 1;
1863:   PetscMapInitialize(B->comm,&B->rmap);
1864:   PetscMapInitialize(B->comm,&B->cmap);
1865:   if (d_nnz) {
1866:     for (i=0; i<B->rmap.n; i++) {
1867:       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]);
1868:     }
1869:   }
1870:   if (o_nnz) {
1871:     for (i=0; i<B->rmap.n; i++) {
1872:       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]);
1873:     }
1874:   }
1875:   b = (Mat_MPIAIJ*)B->data;

1877:   /* Explicitly create 2 MATSEQAIJ matrices. */
1878:   MatCreate(PETSC_COMM_SELF,&b->A);
1879:   MatSetSizes(b->A,B->rmap.n,B->cmap.n,B->rmap.n,B->cmap.n);
1880:   MatSetType(b->A,MATSEQAIJ);
1881:   PetscLogObjectParent(B,b->A);
1882:   MatCreate(PETSC_COMM_SELF,&b->B);
1883:   MatSetSizes(b->B,B->rmap.n,B->cmap.N,B->rmap.n,B->cmap.N);
1884:   MatSetType(b->B,MATSEQAIJ);
1885:   PetscLogObjectParent(B,b->B);

1887:   MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);
1888:   MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);

1890:   return(0);
1891: }

1896: PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1897: {
1898:   Mat            mat;
1899:   Mat_MPIAIJ     *a,*oldmat = (Mat_MPIAIJ*)matin->data;

1903:   *newmat       = 0;
1904:   MatCreate(matin->comm,&mat);
1905:   MatSetSizes(mat,matin->rmap.n,matin->cmap.n,matin->rmap.N,matin->cmap.N);
1906:   MatSetType(mat,matin->type_name);
1907:   PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));
1908:   a    = (Mat_MPIAIJ*)mat->data;
1909: 
1910:   mat->factor       = matin->factor;
1911:   mat->rmap.bs      = matin->rmap.bs;
1912:   mat->assembled    = PETSC_TRUE;
1913:   mat->insertmode   = NOT_SET_VALUES;
1914:   mat->preallocated = PETSC_TRUE;

1916:   a->size           = oldmat->size;
1917:   a->rank           = oldmat->rank;
1918:   a->donotstash     = oldmat->donotstash;
1919:   a->roworiented    = oldmat->roworiented;
1920:   a->rowindices     = 0;
1921:   a->rowvalues      = 0;
1922:   a->getrowactive   = PETSC_FALSE;

1924:   PetscMapCopy(mat->comm,&matin->rmap,&mat->rmap);
1925:   PetscMapCopy(mat->comm,&matin->cmap,&mat->cmap);

1927:   MatStashCreate_Private(matin->comm,1,&mat->stash);
1928:   if (oldmat->colmap) {
1929: #if defined (PETSC_USE_CTABLE)
1930:     PetscTableCreateCopy(oldmat->colmap,&a->colmap);
1931: #else
1932:     PetscMalloc((mat->cmap.N)*sizeof(PetscInt),&a->colmap);
1933:     PetscLogObjectMemory(mat,(mat->cmap.N)*sizeof(PetscInt));
1934:     PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap.N)*sizeof(PetscInt));
1935: #endif
1936:   } else a->colmap = 0;
1937:   if (oldmat->garray) {
1938:     PetscInt len;
1939:     len  = oldmat->B->cmap.n;
1940:     PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);
1941:     PetscLogObjectMemory(mat,len*sizeof(PetscInt));
1942:     if (len) { PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt)); }
1943:   } else a->garray = 0;
1944: 
1945:   VecDuplicate(oldmat->lvec,&a->lvec);
1946:   PetscLogObjectParent(mat,a->lvec);
1947:   VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
1948:   PetscLogObjectParent(mat,a->Mvctx);
1949:   MatDuplicate(oldmat->A,cpvalues,&a->A);
1950:   PetscLogObjectParent(mat,a->A);
1951:   MatDuplicate(oldmat->B,cpvalues,&a->B);
1952:   PetscLogObjectParent(mat,a->B);
1953:   PetscFListDuplicate(matin->qlist,&mat->qlist);
1954:   *newmat = mat;
1955:   return(0);
1956: }

1958:  #include petscsys.h

1962: PetscErrorCode MatLoad_MPIAIJ(PetscViewer viewer, MatType type,Mat *newmat)
1963: {
1964:   Mat            A;
1965:   PetscScalar    *vals,*svals;
1966:   MPI_Comm       comm = ((PetscObject)viewer)->comm;
1967:   MPI_Status     status;
1969:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag,maxnz;
1970:   PetscInt       i,nz,j,rstart,rend,mmax;
1971:   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols;
1972:   PetscInt       *ourlens,*procsnz = 0,*offlens,jj,*mycols,*smycols;
1973:   PetscInt       cend,cstart,n,*rowners;
1974:   int            fd;

1977:   MPI_Comm_size(comm,&size);
1978:   MPI_Comm_rank(comm,&rank);
1979:   if (!rank) {
1980:     PetscViewerBinaryGetDescriptor(viewer,&fd);
1981:     PetscBinaryRead(fd,(char *)header,4,PETSC_INT);
1982:     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
1983:   }

1985:   MPI_Bcast(header+1,3,MPIU_INT,0,comm);
1986:   M = header[1]; N = header[2];
1987:   /* determine ownership of all rows */
1988:   m    = M/size + ((M % size) > rank);
1989:   PetscMalloc((size+1)*sizeof(PetscInt),&rowners);
1990:   MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);

1992:   /* First process needs enough room for process with most rows */
1993:   if (!rank) {
1994:     mmax       = rowners[1];
1995:     for (i=2; i<size; i++) {
1996:       mmax = PetscMax(mmax,rowners[i]);
1997:     }
1998:   } else mmax = m;

2000:   rowners[0] = 0;
2001:   for (i=2; i<=size; i++) {
2002:     mmax       = PetscMax(mmax,rowners[i]);
2003:     rowners[i] += rowners[i-1];
2004:   }
2005:   rstart = rowners[rank];
2006:   rend   = rowners[rank+1];

2008:   /* distribute row lengths to all processors */
2009:   PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);
2010:   if (!rank) {
2011:     PetscBinaryRead(fd,ourlens,m,PETSC_INT);
2012:     PetscMalloc(m*sizeof(PetscInt),&rowlengths);
2013:     PetscMalloc(size*sizeof(PetscInt),&procsnz);
2014:     PetscMemzero(procsnz,size*sizeof(PetscInt));
2015:     for (j=0; j<m; j++) {
2016:       procsnz[0] += ourlens[j];
2017:     }
2018:     for (i=1; i<size; i++) {
2019:       PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);
2020:       /* calculate the number of nonzeros on each processor */
2021:       for (j=0; j<rowners[i+1]-rowners[i]; j++) {
2022:         procsnz[i] += rowlengths[j];
2023:       }
2024:       MPI_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);
2025:     }
2026:     PetscFree(rowlengths);
2027:   } else {
2028:     MPI_Recv(ourlens,m,MPIU_INT,0,tag,comm,&status);
2029:   }

2031:   if (!rank) {
2032:     /* determine max buffer needed and allocate it */
2033:     maxnz = 0;
2034:     for (i=0; i<size; i++) {
2035:       maxnz = PetscMax(maxnz,procsnz[i]);
2036:     }
2037:     PetscMalloc(maxnz*sizeof(PetscInt),&cols);

2039:     /* read in my part of the matrix column indices  */
2040:     nz   = procsnz[0];
2041:     PetscMalloc(nz*sizeof(PetscInt),&mycols);
2042:     PetscBinaryRead(fd,mycols,nz,PETSC_INT);

2044:     /* read in every one elses and ship off */
2045:     for (i=1; i<size; i++) {
2046:       nz   = procsnz[i];
2047:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
2048:       MPI_Send(cols,nz,MPIU_INT,i,tag,comm);
2049:     }
2050:     PetscFree(cols);
2051:   } else {
2052:     /* determine buffer space needed for message */
2053:     nz = 0;
2054:     for (i=0; i<m; i++) {
2055:       nz += ourlens[i];
2056:     }
2057:     PetscMalloc(nz*sizeof(PetscInt),&mycols);

2059:     /* receive message of column indices*/
2060:     MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);
2061:     MPI_Get_count(&status,MPIU_INT,&maxnz);
2062:     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2063:   }

2065:   /* determine column ownership if matrix is not square */
2066:   if (N != M) {
2067:     n      = N/size + ((N % size) > rank);
2068:     MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);
2069:     cstart = cend - n;
2070:   } else {
2071:     cstart = rstart;
2072:     cend   = rend;
2073:     n      = cend - cstart;
2074:   }

2076:   /* loop over local rows, determining number of off diagonal entries */
2077:   PetscMemzero(offlens,m*sizeof(PetscInt));
2078:   jj = 0;
2079:   for (i=0; i<m; i++) {
2080:     for (j=0; j<ourlens[i]; j++) {
2081:       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
2082:       jj++;
2083:     }
2084:   }

2086:   /* create our matrix */
2087:   for (i=0; i<m; i++) {
2088:     ourlens[i] -= offlens[i];
2089:   }
2090:   MatCreate(comm,&A);
2091:   MatSetSizes(A,m,n,M,N);
2092:   MatSetType(A,type);
2093:   MatMPIAIJSetPreallocation(A,0,ourlens,0,offlens);

2095:   MatSetOption(A,MAT_COLUMNS_SORTED);
2096:   for (i=0; i<m; i++) {
2097:     ourlens[i] += offlens[i];
2098:   }

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

2103:     /* read in my part of the matrix numerical values  */
2104:     nz   = procsnz[0];
2105:     PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2106: 
2107:     /* insert into matrix */
2108:     jj      = rstart;
2109:     smycols = mycols;
2110:     svals   = vals;
2111:     for (i=0; i<m; i++) {
2112:       MatSetValues_MPIAIJ(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
2113:       smycols += ourlens[i];
2114:       svals   += ourlens[i];
2115:       jj++;
2116:     }

2118:     /* read in other processors and ship out */
2119:     for (i=1; i<size; i++) {
2120:       nz   = procsnz[i];
2121:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2122:       MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);
2123:     }
2124:     PetscFree(procsnz);
2125:   } else {
2126:     /* receive numeric values */
2127:     PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);

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

2134:     /* insert into matrix */
2135:     jj      = rstart;
2136:     smycols = mycols;
2137:     svals   = vals;
2138:     for (i=0; i<m; i++) {
2139:       MatSetValues_MPIAIJ(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
2140:       smycols += ourlens[i];
2141:       svals   += ourlens[i];
2142:       jj++;
2143:     }
2144:   }
2145:   PetscFree2(ourlens,offlens);
2146:   PetscFree(vals);
2147:   PetscFree(mycols);
2148:   PetscFree(rowners);

2150:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
2151:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
2152:   *newmat = A;
2153:   return(0);
2154: }

2158: /*
2159:     Not great since it makes two copies of the submatrix, first an SeqAIJ 
2160:   in local and then by concatenating the local matrices the end result.
2161:   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2162: */
2163: PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
2164: {
2166:   PetscMPIInt    rank,size;
2167:   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j;
2168:   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
2169:   Mat            *local,M,Mreuse;
2170:   PetscScalar    *vwork,*aa;
2171:   MPI_Comm       comm = mat->comm;
2172:   Mat_SeqAIJ     *aij;


2176:   MPI_Comm_rank(comm,&rank);
2177:   MPI_Comm_size(comm,&size);

2179:   if (call ==  MAT_REUSE_MATRIX) {
2180:     PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);
2181:     if (!Mreuse) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2182:     local = &Mreuse;
2183:     MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);
2184:   } else {
2185:     MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);
2186:     Mreuse = *local;
2187:     PetscFree(local);
2188:   }

2190:   /* 
2191:       m - number of local rows
2192:       n - number of columns (same on all processors)
2193:       rstart - first row in new global matrix generated
2194:   */
2195:   MatGetSize(Mreuse,&m,&n);
2196:   if (call == MAT_INITIAL_MATRIX) {
2197:     aij = (Mat_SeqAIJ*)(Mreuse)->data;
2198:     ii  = aij->i;
2199:     jj  = aij->j;

2201:     /*
2202:         Determine the number of non-zeros in the diagonal and off-diagonal 
2203:         portions of the matrix in order to do correct preallocation
2204:     */

2206:     /* first get start and end of "diagonal" columns */
2207:     if (csize == PETSC_DECIDE) {
2208:       ISGetSize(isrow,&mglobal);
2209:       if (mglobal == n) { /* square matrix */
2210:         nlocal = m;
2211:       } else {
2212:         nlocal = n/size + ((n % size) > rank);
2213:       }
2214:     } else {
2215:       nlocal = csize;
2216:     }
2217:     MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
2218:     rstart = rend - nlocal;
2219:     if (rank == size - 1 && rend != n) {
2220:       SETERRQ2(PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);
2221:     }

2223:     /* next, compute all the lengths */
2224:     PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);
2225:     olens = dlens + m;
2226:     for (i=0; i<m; i++) {
2227:       jend = ii[i+1] - ii[i];
2228:       olen = 0;
2229:       dlen = 0;
2230:       for (j=0; j<jend; j++) {
2231:         if (*jj < rstart || *jj >= rend) olen++;
2232:         else dlen++;
2233:         jj++;
2234:       }
2235:       olens[i] = olen;
2236:       dlens[i] = dlen;
2237:     }
2238:     MatCreate(comm,&M);
2239:     MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);
2240:     MatSetType(M,mat->type_name);
2241:     MatMPIAIJSetPreallocation(M,0,dlens,0,olens);
2242:     PetscFree(dlens);
2243:   } else {
2244:     PetscInt ml,nl;

2246:     M = *newmat;
2247:     MatGetLocalSize(M,&ml,&nl);
2248:     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2249:     MatZeroEntries(M);
2250:     /*
2251:          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2252:        rather than the slower MatSetValues().
2253:     */
2254:     M->was_assembled = PETSC_TRUE;
2255:     M->assembled     = PETSC_FALSE;
2256:   }
2257:   MatGetOwnershipRange(M,&rstart,&rend);
2258:   aij = (Mat_SeqAIJ*)(Mreuse)->data;
2259:   ii  = aij->i;
2260:   jj  = aij->j;
2261:   aa  = aij->a;
2262:   for (i=0; i<m; i++) {
2263:     row   = rstart + i;
2264:     nz    = ii[i+1] - ii[i];
2265:     cwork = jj;     jj += nz;
2266:     vwork = aa;     aa += nz;
2267:     MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);
2268:   }

2270:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
2271:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
2272:   *newmat = M;

2274:   /* save submatrix used in processor for next request */
2275:   if (call ==  MAT_INITIAL_MATRIX) {
2276:     PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);
2277:     PetscObjectDereference((PetscObject)Mreuse);
2278:   }

2280:   return(0);
2281: }

2286: PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt I[],const PetscInt J[],const PetscScalar v[])
2287: {
2288:   PetscInt       m,cstart, cend,j,nnz,i,d;
2289:   PetscInt       *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
2290:   const PetscInt *JJ;
2291:   PetscScalar    *values;

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

2297:   B->rmap.bs = B->cmap.bs = 1;
2298:   PetscMapInitialize(B->comm,&B->rmap);
2299:   PetscMapInitialize(B->comm,&B->cmap);
2300:   m      = B->rmap.n;
2301:   cstart = B->cmap.rstart;
2302:   cend   = B->cmap.rend;
2303:   rstart = B->rmap.rstart;

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

2308:   for (i=0; i<m; i++) {
2309:     nnz     = I[i+1]- I[i];
2310:     JJ      = J + I[i];
2311:     nnz_max = PetscMax(nnz_max,nnz);
2312:     if (nnz < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
2313:     for (j=0; j<nnz; j++) {
2314:       if (*JJ >= cstart) break;
2315:       JJ++;
2316:     }
2317:     d = 0;
2318:     for (; j<nnz; j++) {
2319:       if (*JJ++ >= cend) break;
2320:       d++;
2321:     }
2322:     d_nnz[i] = d;
2323:     o_nnz[i] = nnz - d;
2324:   }
2325:   MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);
2326:   PetscFree(d_nnz);

2328:   if (v) values = (PetscScalar*)v;
2329:   else {
2330:     PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);
2331:     PetscMemzero(values,nnz_max*sizeof(PetscScalar));
2332:   }

2334:   MatSetOption(B,MAT_COLUMNS_SORTED);
2335:   for (i=0; i<m; i++) {
2336:     ii   = i + rstart;
2337:     nnz  = I[i+1]- I[i];
2338:     MatSetValues_MPIAIJ(B,1,&ii,nnz,J+I[i],values+(v ? I[i] : 0),INSERT_VALUES);
2339:   }
2340:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2341:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2342:   MatSetOption(B,MAT_COLUMNS_UNSORTED);

2344:   if (!v) {
2345:     PetscFree(values);
2346:   }
2347:   return(0);
2348: }

2353: /*@
2354:    MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
2355:    (the default parallel PETSc format).  

2357:    Collective on MPI_Comm

2359:    Input Parameters:
2360: +  B - the matrix 
2361: .  i - the indices into j for the start of each local row (starts with zero)
2362: .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2363: -  v - optional values in the matrix

2365:    Level: developer

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

2369: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ
2370: @*/
2371: PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2372: {
2373:   PetscErrorCode ierr,(*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);

2376:   PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",(void (**)(void))&f);
2377:   if (f) {
2378:     (*f)(B,i,j,v);
2379:   }
2380:   return(0);
2381: }

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

2392:    Collective on MPI_Comm

2394:    Input Parameters:
2395: +  A - the matrix 
2396: .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2397:            (same value is used for all local rows)
2398: .  d_nnz - array containing the number of nonzeros in the various rows of the 
2399:            DIAGONAL portion of the local submatrix (possibly different for each row)
2400:            or PETSC_NULL, if d_nz is used to specify the nonzero structure. 
2401:            The size of this array is equal to the number of local rows, i.e 'm'. 
2402:            You must leave room for the diagonal entry even if it is zero.
2403: .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2404:            submatrix (same value is used for all local rows).
2405: -  o_nnz - array containing the number of nonzeros in the various rows of the
2406:            OFF-DIAGONAL portion of the local submatrix (possibly different for
2407:            each row) or PETSC_NULL, if o_nz is used to specify the nonzero 
2408:            structure. The size of this array is equal to the number 
2409:            of local rows, i.e 'm'. 

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

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

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

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

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

2430:    Example usage:
2431:   
2432:    Consider the following 8x8 matrix with 34 non-zero values, that is 
2433:    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2434:    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 
2435:    as follows:

2437: .vb
2438:             1  2  0  |  0  3  0  |  0  4
2439:     Proc0   0  5  6  |  7  0  0  |  8  0
2440:             9  0 10  | 11  0  0  | 12  0
2441:     -------------------------------------
2442:            13  0 14  | 15 16 17  |  0  0
2443:     Proc1   0 18  0  | 19 20 21  |  0  0 
2444:             0  0  0  | 22 23  0  | 24  0
2445:     -------------------------------------
2446:     Proc2  25 26 27  |  0  0 28  | 29  0
2447:            30  0  0  | 31 32 33  |  0 34
2448: .ve

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

2452: .vb
2453:       A B C
2454:       D E F
2455:       G H I
2456: .ve

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

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

2465:    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2466:    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2467:    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2468:    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2469:    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2470:    matrix, ans [DF] as another SeqAIJ matrix.

2472:    When d_nz, o_nz parameters are specified, d_nz storage elements are
2473:    allocated for every row of the local diagonal submatrix, and o_nz
2474:    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2475:    One way to choose d_nz and o_nz is to use the max nonzerors per local 
2476:    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 
2477:    In this case, the values of d_nz,o_nz are:
2478: .vb
2479:      proc0 : dnz = 2, o_nz = 2
2480:      proc1 : dnz = 3, o_nz = 2
2481:      proc2 : dnz = 1, o_nz = 4
2482: .ve
2483:    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2484:    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2485:    for proc3. i.e we are using 12+15+10=37 storage locations to store 
2486:    34 values.

2488:    When d_nnz, o_nnz parameters are specified, the storage is specified
2489:    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2490:    In the above case the values for d_nnz,o_nnz are:
2491: .vb
2492:      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2493:      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2494:      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2495: .ve
2496:    Here the space allocated is sum of all the above values i.e 34, and
2497:    hence pre-allocation is perfect.

2499:    Level: intermediate

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

2503: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIAIJ(), MatMPIAIJSetPreallocationCSR(),
2504:           MPIAIJ
2505: @*/
2506: PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
2507: {
2508:   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]);

2511:   PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",(void (**)(void))&f);
2512:   if (f) {
2513:     (*f)(B,d_nz,d_nnz,o_nz,o_nnz);
2514:   }
2515:   return(0);
2516: }

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

2527:    Collective on MPI_Comm

2529:    Input Parameters:
2530: +  comm - MPI communicator
2531: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2532:            This value should be the same as the local size used in creating the 
2533:            y vector for the matrix-vector product y = Ax.
2534: .  n - This value should be the same as the local size used in creating the 
2535:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2536:        calculated if N is given) For square matrices n is almost always m.
2537: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2538: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2539: .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2540:            (same value is used for all local rows)
2541: .  d_nnz - array containing the number of nonzeros in the various rows of the 
2542:            DIAGONAL portion of the local submatrix (possibly different for each row)
2543:            or PETSC_NULL, if d_nz is used to specify the nonzero structure. 
2544:            The size of this array is equal to the number of local rows, i.e 'm'. 
2545:            You must leave room for the diagonal entry even if it is zero.
2546: .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2547:            submatrix (same value is used for all local rows).
2548: -  o_nnz - array containing the number of nonzeros in the various rows of the
2549:            OFF-DIAGONAL portion of the local submatrix (possibly different for
2550:            each row) or PETSC_NULL, if o_nz is used to specify the nonzero 
2551:            structure. The size of this array is equal to the number 
2552:            of local rows, i.e 'm'. 

2554:    Output Parameter:
2555: .  A - the matrix 

2557:    Notes:
2558:    If the *_nnz parameter is given then the *_nz parameter is ignored

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

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

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

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

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

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

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

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

2593:    Options Database Keys:
2594: +  -mat_no_inode  - Do not use inodes
2595: .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2596: -  -mat_aij_oneindex - Internally use indexing starting at 1
2597:         rather than 0.  Note that when calling MatSetValues(),
2598:         the user still MUST index entries starting at 0!


2601:    Example usage:
2602:   
2603:    Consider the following 8x8 matrix with 34 non-zero values, that is 
2604:    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2605:    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 
2606:    as follows:

2608: .vb
2609:             1  2  0  |  0  3  0  |  0  4
2610:     Proc0   0  5  6  |  7  0  0  |  8  0
2611:             9  0 10  | 11  0  0  | 12  0
2612:     -------------------------------------
2613:            13  0 14  | 15 16 17  |  0  0
2614:     Proc1   0 18  0  | 19 20 21  |  0  0 
2615:             0  0  0  | 22 23  0  | 24  0
2616:     -------------------------------------
2617:     Proc2  25 26 27  |  0  0 28  | 29  0
2618:            30  0  0  | 31 32 33  |  0 34
2619: .ve

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

2623: .vb
2624:       A B C
2625:       D E F
2626:       G H I
2627: .ve

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

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

2636:    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2637:    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2638:    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2639:    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2640:    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2641:    matrix, ans [DF] as another SeqAIJ matrix.

2643:    When d_nz, o_nz parameters are specified, d_nz storage elements are
2644:    allocated for every row of the local diagonal submatrix, and o_nz
2645:    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2646:    One way to choose d_nz and o_nz is to use the max nonzerors per local 
2647:    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 
2648:    In this case, the values of d_nz,o_nz are:
2649: .vb
2650:      proc0 : dnz = 2, o_nz = 2
2651:      proc1 : dnz = 3, o_nz = 2
2652:      proc2 : dnz = 1, o_nz = 4
2653: .ve
2654:    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2655:    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2656:    for proc3. i.e we are using 12+15+10=37 storage locations to store 
2657:    34 values.

2659:    When d_nnz, o_nnz parameters are specified, the storage is specified
2660:    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2661:    In the above case the values for d_nnz,o_nnz are:
2662: .vb
2663:      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2664:      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2665:      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2666: .ve
2667:    Here the space allocated is sum of all the above values i.e 34, and
2668:    hence pre-allocation is perfect.

2670:    Level: intermediate

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

2674: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
2675:           MPIAIJ
2676: @*/
2677: PetscErrorCode PETSCMAT_DLLEXPORT 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)
2678: {
2680:   PetscMPIInt    size;

2683:   MatCreate(comm,A);
2684:   MatSetSizes(*A,m,n,M,N);
2685:   MPI_Comm_size(comm,&size);
2686:   if (size > 1) {
2687:     MatSetType(*A,MATMPIAIJ);
2688:     MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);
2689:   } else {
2690:     MatSetType(*A,MATSEQAIJ);
2691:     MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);
2692:   }
2693:   return(0);
2694: }

2698: PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[])
2699: {
2700:   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;

2703:   *Ad     = a->A;
2704:   *Ao     = a->B;
2705:   *colmap = a->garray;
2706:   return(0);
2707: }

2711: PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2712: {
2714:   PetscInt       i;
2715:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2718:   if (coloring->ctype == IS_COLORING_LOCAL) {
2719:     ISColoringValue *allcolors,*colors;
2720:     ISColoring      ocoloring;

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

2725:     /* set coloring for off-diagonal portion */
2726:     ISAllGatherColors(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);
2727:     PetscMalloc((a->B->cmap.n+1)*sizeof(ISColoringValue),&colors);
2728:     for (i=0; i<a->B->cmap.n; i++) {
2729:       colors[i] = allcolors[a->garray[i]];
2730:     }
2731:     PetscFree(allcolors);
2732:     ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap.n,colors,&ocoloring);
2733:     MatSetColoring_SeqAIJ(a->B,ocoloring);
2734:     ISColoringDestroy(ocoloring);
2735:   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
2736:     ISColoringValue *colors;
2737:     PetscInt        *larray;
2738:     ISColoring      ocoloring;

2740:     /* set coloring for diagonal portion */
2741:     PetscMalloc((a->A->cmap.n+1)*sizeof(PetscInt),&larray);
2742:     for (i=0; i<a->A->cmap.n; i++) {
2743:       larray[i] = i + A->cmap.rstart;
2744:     }
2745:     ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->cmap.n,larray,PETSC_NULL,larray);
2746:     PetscMalloc((a->A->cmap.n+1)*sizeof(ISColoringValue),&colors);
2747:     for (i=0; i<a->A->cmap.n; i++) {
2748:       colors[i] = coloring->colors[larray[i]];
2749:     }
2750:     PetscFree(larray);
2751:     ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap.n,colors,&ocoloring);
2752:     MatSetColoring_SeqAIJ(a->A,ocoloring);
2753:     ISColoringDestroy(ocoloring);

2755:     /* set coloring for off-diagonal portion */
2756:     PetscMalloc((a->B->cmap.n+1)*sizeof(PetscInt),&larray);
2757:     ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->cmap.n,a->garray,PETSC_NULL,larray);
2758:     PetscMalloc((a->B->cmap.n+1)*sizeof(ISColoringValue),&colors);
2759:     for (i=0; i<a->B->cmap.n; i++) {
2760:       colors[i] = coloring->colors[larray[i]];
2761:     }
2762:     PetscFree(larray);
2763:     ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap.n,colors,&ocoloring);
2764:     MatSetColoring_SeqAIJ(a->B,ocoloring);
2765:     ISColoringDestroy(ocoloring);
2766:   } else {
2767:     SETERRQ1(PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype);
2768:   }

2770:   return(0);
2771: }

2773: #if defined(PETSC_HAVE_ADIC)
2776: PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2777: {
2778:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2782:   MatSetValuesAdic_SeqAIJ(a->A,advalues);
2783:   MatSetValuesAdic_SeqAIJ(a->B,advalues);
2784:   return(0);
2785: }
2786: #endif

2790: PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues)
2791: {
2792:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;

2796:   MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);
2797:   MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);
2798:   return(0);
2799: }

2803: /*@C
2804:       MatMerge - Creates a single large PETSc matrix by concatinating sequential
2805:                  matrices from each processor

2807:     Collective on MPI_Comm

2809:    Input Parameters:
2810: +    comm - the communicators the parallel matrix will live on
2811: .    inmat - the input sequential matrices
2812: .    n - number of local columns (or PETSC_DECIDE)
2813: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

2815:    Output Parameter:
2816: .    outmat - the parallel matrix generated

2818:     Level: advanced

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

2822: @*/
2823: PetscErrorCode PETSCMAT_DLLEXPORT MatMerge(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
2824: {
2826:   PetscInt       m,N,i,rstart,nnz,I,*dnz,*onz;
2827:   PetscInt       *indx;
2828:   PetscScalar    *values;

2831:   MatGetSize(inmat,&m,&N);
2832:   if (scall == MAT_INITIAL_MATRIX){
2833:     /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */
2834:     if (n == PETSC_DECIDE){
2835:       PetscSplitOwnership(comm,&n,&N);
2836:     }
2837:     MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);
2838:     rstart -= m;

2840:     MatPreallocateInitialize(comm,m,n,dnz,onz);
2841:     for (i=0;i<m;i++) {
2842:       MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);
2843:       MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);
2844:       MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);
2845:     }
2846:     /* This routine will ONLY return MPIAIJ type matrix */
2847:     MatCreate(comm,outmat);
2848:     MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
2849:     MatSetType(*outmat,MATMPIAIJ);
2850:     MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);
2851:     MatPreallocateFinalize(dnz,onz);
2852: 
2853:   } else if (scall == MAT_REUSE_MATRIX){
2854:     MatGetOwnershipRange(*outmat,&rstart,PETSC_NULL);
2855:   } else {
2856:     SETERRQ1(PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
2857:   }

2859:   for (i=0;i<m;i++) {
2860:     MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
2861:     I    = i + rstart;
2862:     MatSetValues(*outmat,1,&I,nnz,indx,values,INSERT_VALUES);
2863:     MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
2864:   }
2865:   MatDestroy(inmat);
2866:   MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);
2867:   MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);

2869:   return(0);
2870: }

2874: PetscErrorCode MatFileSplit(Mat A,char *outfile)
2875: {
2876:   PetscErrorCode    ierr;
2877:   PetscMPIInt       rank;
2878:   PetscInt          m,N,i,rstart,nnz;
2879:   size_t            len;
2880:   const PetscInt    *indx;
2881:   PetscViewer       out;
2882:   char              *name;
2883:   Mat               B;
2884:   const PetscScalar *values;

2887:   MatGetLocalSize(A,&m,0);
2888:   MatGetSize(A,0,&N);
2889:   /* Should this be the type of the diagonal block of A? */
2890:   MatCreate(PETSC_COMM_SELF,&B);
2891:   MatSetSizes(B,m,N,m,N);
2892:   MatSetType(B,MATSEQAIJ);
2893:   MatSeqAIJSetPreallocation(B,0,PETSC_NULL);
2894:   MatGetOwnershipRange(A,&rstart,0);
2895:   for (i=0;i<m;i++) {
2896:     MatGetRow(A,i+rstart,&nnz,&indx,&values);
2897:     MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);
2898:     MatRestoreRow(A,i+rstart,&nnz,&indx,&values);
2899:   }
2900:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2901:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

2903:   MPI_Comm_rank(A->comm,&rank);
2904:   PetscStrlen(outfile,&len);
2905:   PetscMalloc((len+5)*sizeof(char),&name);
2906:   sprintf(name,"%s.%d",outfile,rank);
2907:   PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);
2908:   PetscFree(name);
2909:   MatView(B,out);
2910:   PetscViewerDestroy(out);
2911:   MatDestroy(B);
2912:   return(0);
2913: }

2915: EXTERN PetscErrorCode MatDestroy_MPIAIJ(Mat);
2918: PetscErrorCode PETSCMAT_DLLEXPORT MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
2919: {
2920:   PetscErrorCode       ierr;
2921:   Mat_Merge_SeqsToMPI  *merge;
2922:   PetscObjectContainer container;

2925:   PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);
2926:   if (container) {
2927:     PetscObjectContainerGetPointer(container,(void **)&merge);
2928:     PetscFree(merge->id_r);
2929:     PetscFree(merge->len_s);
2930:     PetscFree(merge->len_r);
2931:     PetscFree(merge->bi);
2932:     PetscFree(merge->bj);
2933:     PetscFree(merge->buf_ri);
2934:     PetscFree(merge->buf_rj);
2935:     PetscFree(merge->coi);
2936:     PetscFree(merge->coj);
2937:     PetscFree(merge->owners_co);
2938:     PetscFree(merge->rowmap.range);
2939: 
2940:     PetscObjectContainerDestroy(container);
2941:     PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);
2942:   }
2943:   PetscFree(merge);

2945:   MatDestroy_MPIAIJ(A);
2946:   return(0);
2947: }

2949:  #include src/mat/utils/freespace.h
2950:  #include petscbt.h
2951: static PetscEvent logkey_seqstompinum = 0;
2954: /*@C
2955:       MatMerge_SeqsToMPI - Creates a MPIAIJ matrix by adding sequential
2956:                  matrices from each processor

2958:     Collective on MPI_Comm

2960:    Input Parameters:
2961: +    comm - the communicators the parallel matrix will live on
2962: .    seqmat - the input sequential matrices
2963: .    m - number of local rows (or PETSC_DECIDE)
2964: .    n - number of local columns (or PETSC_DECIDE)
2965: -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

2967:    Output Parameter:
2968: .    mpimat - the parallel matrix generated

2970:     Level: advanced

2972:    Notes: 
2973:      The dimensions of the sequential matrix in each processor MUST be the same.
2974:      The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
2975:      destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
2976: @*/
2977: PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPINumeric(Mat seqmat,Mat mpimat)
2978: {
2979:   PetscErrorCode       ierr;
2980:   MPI_Comm             comm=mpimat->comm;
2981:   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
2982:   PetscMPIInt          size,rank,taga,*len_s;
2983:   PetscInt             N=mpimat->cmap.N,i,j,*owners,*ai=a->i,*aj=a->j;
2984:   PetscInt             proc,m;
2985:   PetscInt             **buf_ri,**buf_rj;
2986:   PetscInt             k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
2987:   PetscInt             nrows,**buf_ri_k,**nextrow,**nextai;
2988:   MPI_Request          *s_waits,*r_waits;
2989:   MPI_Status           *status;
2990:   MatScalar            *aa=a->a,**abuf_r,*ba_i;
2991:   Mat_Merge_SeqsToMPI  *merge;
2992:   PetscObjectContainer container;
2993: 
2995:   if (!logkey_seqstompinum) {
2996:     PetscLogEventRegister(&logkey_seqstompinum,"MatMerge_SeqsToMPINumeric",MAT_COOKIE);
2997:   }
2998:   PetscLogEventBegin(logkey_seqstompinum,seqmat,0,0,0);

3000:   MPI_Comm_size(comm,&size);
3001:   MPI_Comm_rank(comm,&rank);

3003:   PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);
3004:   if (container) {
3005:     PetscObjectContainerGetPointer(container,(void **)&merge);
3006:   }
3007:   bi     = merge->bi;
3008:   bj     = merge->bj;
3009:   buf_ri = merge->buf_ri;
3010:   buf_rj = merge->buf_rj;

3012:   PetscMalloc(size*sizeof(MPI_Status),&status);
3013:   owners = merge->rowmap.range;
3014:   len_s  = merge->len_s;

3016:   /* send and recv matrix values */
3017:   /*-----------------------------*/
3018:   PetscObjectGetNewTag((PetscObject)mpimat,&taga);
3019:   PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);

3021:   PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);
3022:   for (proc=0,k=0; proc<size; proc++){
3023:     if (!len_s[proc]) continue;
3024:     i = owners[proc];
3025:     MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);
3026:     k++;
3027:   }

3029:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,r_waits,status);}
3030:   if (merge->nsend) {MPI_Waitall(merge->nsend,s_waits,status);}
3031:   PetscFree(status);

3033:   PetscFree(s_waits);
3034:   PetscFree(r_waits);

3036:   /* insert mat values of mpimat */
3037:   /*----------------------------*/
3038:   PetscMalloc(N*sizeof(MatScalar),&ba_i);
3039:   PetscMalloc((3*merge->nrecv+1)*sizeof(PetscInt**),&buf_ri_k);
3040:   nextrow = buf_ri_k + merge->nrecv;
3041:   nextai  = nextrow + merge->nrecv;

3043:   for (k=0; k<merge->nrecv; k++){
3044:     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
3045:     nrows = *(buf_ri_k[k]);
3046:     nextrow[k]  = buf_ri_k[k]+1;  /* next row number of k-th recved i-structure */
3047:     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
3048:   }

3050:   /* set values of ba */
3051:   m = merge->rowmap.n;
3052:   for (i=0; i<m; i++) {
3053:     arow = owners[rank] + i;
3054:     bj_i = bj+bi[i];  /* col indices of the i-th row of mpimat */
3055:     bnzi = bi[i+1] - bi[i];
3056:     PetscMemzero(ba_i,bnzi*sizeof(MatScalar));

3058:     /* add local non-zero vals of this proc's seqmat into ba */
3059:     anzi = ai[arow+1] - ai[arow];
3060:     aj   = a->j + ai[arow];
3061:     aa   = a->a + ai[arow];
3062:     nextaj = 0;
3063:     for (j=0; nextaj<anzi; j++){
3064:       if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
3065:         ba_i[j] += aa[nextaj++];
3066:       }
3067:     }

3069:     /* add received vals into ba */
3070:     for (k=0; k<merge->nrecv; k++){ /* k-th received message */
3071:       /* i-th row */
3072:       if (i == *nextrow[k]) {
3073:         anzi = *(nextai[k]+1) - *nextai[k];
3074:         aj   = buf_rj[k] + *(nextai[k]);
3075:         aa   = abuf_r[k] + *(nextai[k]);
3076:         nextaj = 0;
3077:         for (j=0; nextaj<anzi; j++){
3078:           if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
3079:             ba_i[j] += aa[nextaj++];
3080:           }
3081:         }
3082:         nextrow[k]++; nextai[k]++;
3083:       }
3084:     }
3085:     MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);
3086:   }
3087:   MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);
3088:   MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);

3090:   PetscFree(abuf_r);
3091:   PetscFree(ba_i);
3092:   PetscFree(buf_ri_k);
3093:   PetscLogEventEnd(logkey_seqstompinum,seqmat,0,0,0);
3094:   return(0);
3095: }

3097: static PetscEvent logkey_seqstompisym = 0;
3100: PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPISymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
3101: {
3102:   PetscErrorCode       ierr;
3103:   Mat                  B_mpi;
3104:   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
3105:   PetscMPIInt          size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
3106:   PetscInt             **buf_rj,**buf_ri,**buf_ri_k;
3107:   PetscInt             M=seqmat->rmap.n,N=seqmat->cmap.n,i,*owners,*ai=a->i,*aj=a->j;
3108:   PetscInt             len,proc,*dnz,*onz;
3109:   PetscInt             k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
3110:   PetscInt             nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
3111:   MPI_Request          *si_waits,*sj_waits,*ri_waits,*rj_waits;
3112:   MPI_Status           *status;
3113:   PetscFreeSpaceList   free_space=PETSC_NULL,current_space=PETSC_NULL;
3114:   PetscBT              lnkbt;
3115:   Mat_Merge_SeqsToMPI  *merge;
3116:   PetscObjectContainer container;

3119:   if (!logkey_seqstompisym) {
3120:     PetscLogEventRegister(&logkey_seqstompisym,"MatMerge_SeqsToMPISymbolic",MAT_COOKIE);
3121:   }
3122:   PetscLogEventBegin(logkey_seqstompisym,seqmat,0,0,0);

3124:   /* make sure it is a PETSc comm */
3125:   PetscCommDuplicate(comm,&comm,PETSC_NULL);
3126:   MPI_Comm_size(comm,&size);
3127:   MPI_Comm_rank(comm,&rank);
3128: 
3129:   PetscNew(Mat_Merge_SeqsToMPI,&merge);
3130:   PetscMalloc(size*sizeof(MPI_Status),&status);

3132:   /* determine row ownership */
3133:   /*---------------------------------------------------------*/
3134:   merge->rowmap.n = m;
3135:   merge->rowmap.N = M;
3136:   merge->rowmap.bs = 1;
3137:   PetscMapInitialize(comm,&merge->rowmap);
3138:   PetscMalloc(size*sizeof(PetscMPIInt),&len_si);
3139:   PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);
3140: 
3141:   m      = merge->rowmap.n;
3142:   M      = merge->rowmap.N;
3143:   owners = merge->rowmap.range;

3145:   /* determine the number of messages to send, their lengths */
3146:   /*---------------------------------------------------------*/
3147:   len_s  = merge->len_s;

3149:   len = 0;  /* length of buf_si[] */
3150:   merge->nsend = 0;
3151:   for (proc=0; proc<size; proc++){
3152:     len_si[proc] = 0;
3153:     if (proc == rank){
3154:       len_s[proc] = 0;
3155:     } else {
3156:       len_si[proc] = owners[proc+1] - owners[proc] + 1;
3157:       len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
3158:     }
3159:     if (len_s[proc]) {
3160:       merge->nsend++;
3161:       nrows = 0;
3162:       for (i=owners[proc]; i<owners[proc+1]; i++){
3163:         if (ai[i+1] > ai[i]) nrows++;
3164:       }
3165:       len_si[proc] = 2*(nrows+1);
3166:       len += len_si[proc];
3167:     }
3168:   }

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

3175:   /* post the Irecv of j-structure */
3176:   /*-------------------------------*/
3177:   PetscCommGetNewTag(comm,&tagj);
3178:   PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);

3180:   /* post the Isend of j-structure */
3181:   /*--------------------------------*/
3182:   PetscMalloc((2*merge->nsend+1)*sizeof(MPI_Request),&si_waits);
3183:   sj_waits = si_waits + merge->nsend;

3185:   for (proc=0, k=0; proc<size; proc++){
3186:     if (!len_s[proc]) continue;
3187:     i = owners[proc];
3188:     MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);
3189:     k++;
3190:   }

3192:   /* receives and sends of j-structure are complete */
3193:   /*------------------------------------------------*/
3194:   if (merge->nrecv) {MPI_Waitall(merge->nrecv,rj_waits,status);}
3195:   if (merge->nsend) {MPI_Waitall(merge->nsend,sj_waits,status);}
3196: 
3197:   /* send and recv i-structure */
3198:   /*---------------------------*/
3199:   PetscCommGetNewTag(comm,&tagi);
3200:   PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);
3201: 
3202:   PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);
3203:   buf_si = buf_s;  /* points to the beginning of k-th msg to be sent */
3204:   for (proc=0,k=0; proc<size; proc++){
3205:     if (!len_s[proc]) continue;
3206:     /* form outgoing message for i-structure: 
3207:          buf_si[0]:                 nrows to be sent
3208:                [1:nrows]:           row index (global)
3209:                [nrows+1:2*nrows+1]: i-structure index
3210:     */
3211:     /*-------------------------------------------*/
3212:     nrows = len_si[proc]/2 - 1;
3213:     buf_si_i    = buf_si + nrows+1;
3214:     buf_si[0]   = nrows;
3215:     buf_si_i[0] = 0;
3216:     nrows = 0;
3217:     for (i=owners[proc]; i<owners[proc+1]; i++){
3218:       anzi = ai[i+1] - ai[i];
3219:       if (anzi) {
3220:         buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
3221:         buf_si[nrows+1] = i-owners[proc]; /* local row index */
3222:         nrows++;
3223:       }
3224:     }
3225:     MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);
3226:     k++;
3227:     buf_si += len_si[proc];
3228:   }

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

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

3238:   PetscFree(len_si);
3239:   PetscFree(len_ri);
3240:   PetscFree(rj_waits);
3241:   PetscFree(si_waits);
3242:   PetscFree(ri_waits);
3243:   PetscFree(buf_s);
3244:   PetscFree(status);

3246:   /* compute a local seq matrix in each processor */
3247:   /*----------------------------------------------*/
3248:   /* allocate bi array and free space for accumulating nonzero column info */
3249:   PetscMalloc((m+1)*sizeof(PetscInt),&bi);
3250:   bi[0] = 0;

3252:   /* create and initialize a linked list */
3253:   nlnk = N+1;
3254:   PetscLLCreate(N,N,nlnk,lnk,lnkbt);
3255: 
3256:   /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
3257:   len = 0;
3258:   len  = ai[owners[rank+1]] - ai[owners[rank]];
3259:   PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);
3260:   current_space = free_space;

3262:   /* determine symbolic info for each local row */
3263:   PetscMalloc((3*merge->nrecv+1)*sizeof(PetscInt**),&buf_ri_k);
3264:   nextrow = buf_ri_k + merge->nrecv;
3265:   nextai  = nextrow + merge->nrecv;
3266:   for (k=0; k<merge->nrecv; k++){
3267:     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
3268:     nrows = *buf_ri_k[k];
3269:     nextrow[k]  = buf_ri_k[k] + 1;  /* next row number of k-th recved i-structure */
3270:     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
3271:   }

3273:   MatPreallocateInitialize(comm,m,n,dnz,onz);
3274:   len = 0;
3275:   for (i=0;i<m;i++) {
3276:     bnzi   = 0;
3277:     /* add local non-zero cols of this proc's seqmat into lnk */
3278:     arow   = owners[rank] + i;
3279:     anzi   = ai[arow+1] - ai[arow];
3280:     aj     = a->j + ai[arow];
3281:     PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);
3282:     bnzi += nlnk;
3283:     /* add received col data into lnk */
3284:     for (k=0; k<merge->nrecv; k++){ /* k-th received message */
3285:       if (i == *nextrow[k]) { /* i-th row */
3286:         anzi = *(nextai[k]+1) - *nextai[k];
3287:         aj   = buf_rj[k] + *nextai[k];
3288:         PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);
3289:         bnzi += nlnk;
3290:         nextrow[k]++; nextai[k]++;
3291:       }
3292:     }
3293:     if (len < bnzi) len = bnzi;  /* =max(bnzi) */

3295:     /* if free space is not available, make more free space */
3296:     if (current_space->local_remaining<bnzi) {
3297:       PetscFreeSpaceGet(current_space->total_array_size,&current_space);
3298:       nspacedouble++;
3299:     }
3300:     /* copy data into free space, then initialize lnk */
3301:     PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);
3302:     MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);

3304:     current_space->array           += bnzi;
3305:     current_space->local_used      += bnzi;
3306:     current_space->local_remaining -= bnzi;
3307: 
3308:     bi[i+1] = bi[i] + bnzi;
3309:   }
3310: 
3311:   PetscFree(buf_ri_k);

3313:   PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);
3314:   PetscFreeSpaceContiguous(&free_space,bj);
3315:   PetscLLDestroy(lnk,lnkbt);

3317:   /* create symbolic parallel matrix B_mpi */
3318:   /*---------------------------------------*/
3319:   MatCreate(comm,&B_mpi);
3320:   if (n==PETSC_DECIDE) {
3321:     MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);
3322:   } else {
3323:     MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
3324:   }
3325:   MatSetType(B_mpi,MATMPIAIJ);
3326:   MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);
3327:   MatPreallocateFinalize(dnz,onz);

3329:   /* B_mpi is not ready for use - assembly will be done by MatMerge_SeqsToMPINumeric() */
3330:   B_mpi->assembled     = PETSC_FALSE;
3331:   B_mpi->ops->destroy  = MatDestroy_MPIAIJ_SeqsToMPI;
3332:   merge->bi            = bi;
3333:   merge->bj            = bj;
3334:   merge->buf_ri        = buf_ri;
3335:   merge->buf_rj        = buf_rj;
3336:   merge->coi           = PETSC_NULL;
3337:   merge->coj           = PETSC_NULL;
3338:   merge->owners_co     = PETSC_NULL;

3340:   /* attach the supporting struct to B_mpi for reuse */
3341:   PetscObjectContainerCreate(PETSC_COMM_SELF,&container);
3342:   PetscObjectContainerSetPointer(container,merge);
3343:   PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);
3344:   *mpimat = B_mpi;

3346:   PetscCommDestroy(&comm);
3347:   PetscLogEventEnd(logkey_seqstompisym,seqmat,0,0,0);
3348:   return(0);
3349: }

3351: static PetscEvent logkey_seqstompi = 0;
3354: PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPI(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
3355: {
3356:   PetscErrorCode   ierr;

3359:   if (!logkey_seqstompi) {
3360:     PetscLogEventRegister(&logkey_seqstompi,"MatMerge_SeqsToMPI",MAT_COOKIE);
3361:   }
3362:   PetscLogEventBegin(logkey_seqstompi,seqmat,0,0,0);
3363:   if (scall == MAT_INITIAL_MATRIX){
3364:     MatMerge_SeqsToMPISymbolic(comm,seqmat,m,n,mpimat);
3365:   }
3366:   MatMerge_SeqsToMPINumeric(seqmat,*mpimat);
3367:   PetscLogEventEnd(logkey_seqstompi,seqmat,0,0,0);
3368:   return(0);
3369: }
3370: static PetscEvent logkey_getlocalmat = 0;
3373: /*@C
3374:      MatGetLocalMat - Creates a SeqAIJ matrix by taking all its local rows

3376:     Not Collective

3378:    Input Parameters:
3379: +    A - the matrix 
3380: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 

3382:    Output Parameter:
3383: .    A_loc - the local sequential matrix generated

3385:     Level: developer

3387: @*/
3388: PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
3389: {
3390:   PetscErrorCode  ierr;
3391:   Mat_MPIAIJ      *mpimat=(Mat_MPIAIJ*)A->data;
3392:   Mat_SeqAIJ      *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data;
3393:   PetscInt        *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray;
3394:   PetscScalar     *aa=a->a,*ba=b->a,*ca;
3395:   PetscInt        am=A->rmap.n,i,j,k,cstart=A->cmap.rstart;
3396:   PetscInt        *ci,*cj,col,ncols_d,ncols_o,jo;

3399:   if (!logkey_getlocalmat) {
3400:     PetscLogEventRegister(&logkey_getlocalmat,"MatGetLocalMat",MAT_COOKIE);
3401:   }
3402:   PetscLogEventBegin(logkey_getlocalmat,A,0,0,0);
3403:   if (scall == MAT_INITIAL_MATRIX){
3404:     PetscMalloc((1+am)*sizeof(PetscInt),&ci);
3405:     ci[0] = 0;
3406:     for (i=0; i<am; i++){
3407:       ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
3408:     }
3409:     PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);
3410:     PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);
3411:     k = 0;
3412:     for (i=0; i<am; i++) {
3413:       ncols_o = bi[i+1] - bi[i];
3414:       ncols_d = ai[i+1] - ai[i];
3415:       /* off-diagonal portion of A */
3416:       for (jo=0; jo<ncols_o; jo++) {
3417:         col = cmap[*bj];
3418:         if (col >= cstart) break;
3419:         cj[k]   = col; bj++;
3420:         ca[k++] = *ba++;
3421:       }
3422:       /* diagonal portion of A */
3423:       for (j=0; j<ncols_d; j++) {
3424:         cj[k]   = cstart + *aj++;
3425:         ca[k++] = *aa++;
3426:       }
3427:       /* off-diagonal portion of A */
3428:       for (j=jo; j<ncols_o; j++) {
3429:         cj[k]   = cmap[*bj++];
3430:         ca[k++] = *ba++;
3431:       }
3432:     }
3433:     /* put together the new matrix */
3434:     MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap.N,ci,cj,ca,A_loc);
3435:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
3436:     /* Since these are PETSc arrays, change flags to free them as necessary. */
3437:     mat = (Mat_SeqAIJ*)(*A_loc)->data;
3438:     mat->freedata = PETSC_TRUE;
3439:     mat->nonew    = 0;
3440:   } else if (scall == MAT_REUSE_MATRIX){
3441:     mat=(Mat_SeqAIJ*)(*A_loc)->data;
3442:     ci = mat->i; cj = mat->j; ca = mat->a;
3443:     for (i=0; i<am; i++) {
3444:       /* off-diagonal portion of A */
3445:       ncols_o = bi[i+1] - bi[i];
3446:       for (jo=0; jo<ncols_o; jo++) {
3447:         col = cmap[*bj];
3448:         if (col >= cstart) break;
3449:         *ca++ = *ba++; bj++;
3450:       }
3451:       /* diagonal portion of A */
3452:       ncols_d = ai[i+1] - ai[i];
3453:       for (j=0; j<ncols_d; j++) *ca++ = *aa++;
3454:       /* off-diagonal portion of A */
3455:       for (j=jo; j<ncols_o; j++) {
3456:         *ca++ = *ba++; bj++;
3457:       }
3458:     }
3459:   } else {
3460:     SETERRQ1(PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
3461:   }

3463:   PetscLogEventEnd(logkey_getlocalmat,A,0,0,0);
3464:   return(0);
3465: }

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

3473:     Not Collective

3475:    Input Parameters:
3476: +    A - the matrix 
3477: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3478: -    row, col - index sets of rows and columns to extract (or PETSC_NULL)  

3480:    Output Parameter:
3481: .    A_loc - the local sequential matrix generated

3483:     Level: developer

3485: @*/
3486: PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
3487: {
3488:   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
3489:   PetscErrorCode    ierr;
3490:   PetscInt          i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
3491:   IS                isrowa,iscola;
3492:   Mat               *aloc;

3495:   if (!logkey_getlocalmatcondensed) {
3496:     PetscLogEventRegister(&logkey_getlocalmatcondensed,"MatGetLocalMatCondensed",MAT_COOKIE);
3497:   }
3498:   PetscLogEventBegin(logkey_getlocalmatcondensed,A,0,0,0);
3499:   if (!row){
3500:     start = A->rmap.rstart; end = A->rmap.rend;
3501:     ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);
3502:   } else {
3503:     isrowa = *row;
3504:   }
3505:   if (!col){
3506:     start = A->cmap.rstart;
3507:     cmap  = a->garray;
3508:     nzA   = a->A->cmap.n;
3509:     nzB   = a->B->cmap.n;
3510:     PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);
3511:     ncols = 0;
3512:     for (i=0; i<nzB; i++) {
3513:       if (cmap[i] < start) idx[ncols++] = cmap[i];
3514:       else break;
3515:     }
3516:     imark = i;
3517:     for (i=0; i<nzA; i++) idx[ncols++] = start + i;
3518:     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
3519:     ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,&iscola);
3520:     PetscFree(idx);
3521:   } else {
3522:     iscola = *col;
3523:   }
3524:   if (scall != MAT_INITIAL_MATRIX){
3525:     PetscMalloc(sizeof(Mat),&aloc);
3526:     aloc[0] = *A_loc;
3527:   }
3528:   MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);
3529:   *A_loc = aloc[0];
3530:   PetscFree(aloc);
3531:   if (!row){
3532:     ISDestroy(isrowa);
3533:   }
3534:   if (!col){
3535:     ISDestroy(iscola);
3536:   }
3537:   PetscLogEventEnd(logkey_getlocalmatcondensed,A,0,0,0);
3538:   return(0);
3539: }

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

3547:     Collective on Mat

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

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

3559:     Level: developer

3561: @*/
3562: PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,PetscInt *brstart,Mat *B_seq)
3563: {
3564:   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
3565:   PetscErrorCode    ierr;
3566:   PetscInt          *idx,i,start,ncols,nzA,nzB,*cmap,imark;
3567:   IS                isrowb,iscolb;
3568:   Mat               *bseq;
3569: 
3571:   if (A->cmap.rstart != B->rmap.rstart || A->cmap.rend != B->rmap.rend){
3572:     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);
3573:   }
3574:   if (!logkey_GetBrowsOfAcols) {
3575:     PetscLogEventRegister(&logkey_GetBrowsOfAcols,"MatGetBrowsOfAcols",MAT_COOKIE);
3576:   }
3577:   PetscLogEventBegin(logkey_GetBrowsOfAcols,A,B,0,0);
3578: 
3579:   if (scall == MAT_INITIAL_MATRIX){
3580:     start = A->cmap.rstart;
3581:     cmap  = a->garray;
3582:     nzA   = a->A->cmap.n;
3583:     nzB   = a->B->cmap.n;
3584:     PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);
3585:     ncols = 0;
3586:     for (i=0; i<nzB; i++) {  /* row < local row index */
3587:       if (cmap[i] < start) idx[ncols++] = cmap[i];
3588:       else break;
3589:     }
3590:     imark = i;
3591:     for (i=0; i<nzA; i++) idx[ncols++] = start + i;  /* local rows */
3592:     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
3593:     ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,&isrowb);
3594:     PetscFree(idx);
3595:     *brstart = imark;
3596:     ISCreateStride(PETSC_COMM_SELF,B->cmap.N,0,1,&iscolb);
3597:   } else {
3598:     if (!rowb || !colb) SETERRQ(PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
3599:     isrowb = *rowb; iscolb = *colb;
3600:     PetscMalloc(sizeof(Mat),&bseq);
3601:     bseq[0] = *B_seq;
3602:   }
3603:   MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);
3604:   *B_seq = bseq[0];
3605:   PetscFree(bseq);
3606:   if (!rowb){
3607:     ISDestroy(isrowb);
3608:   } else {
3609:     *rowb = isrowb;
3610:   }
3611:   if (!colb){
3612:     ISDestroy(iscolb);
3613:   } else {
3614:     *colb = iscolb;
3615:   }
3616:   PetscLogEventEnd(logkey_GetBrowsOfAcols,A,B,0,0);
3617:   return(0);
3618: }

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

3627:     Collective on Mat

3629:    Input Parameters:
3630: +    A,B - the matrices in mpiaij format
3631: .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3632: .    startsj - starting point in B's sending and receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL) 
3633: -    bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL) 

3635:    Output Parameter:
3636: +    B_oth - the sequential matrix generated

3638:     Level: developer

3640: @*/
3641: PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAoCols(Mat A,Mat B,MatReuse scall,PetscInt **startsj,PetscScalar **bufa_ptr,Mat *B_oth)
3642: {
3643:   VecScatter_MPI_General *gen_to,*gen_from;
3644:   PetscErrorCode         ierr;
3645:   Mat_MPIAIJ             *a=(Mat_MPIAIJ*)A->data;
3646:   Mat_SeqAIJ             *b_oth;
3647:   VecScatter             ctx=a->Mvctx;
3648:   MPI_Comm               comm=ctx->comm;
3649:   PetscMPIInt            *rprocs,*sprocs,tag=ctx->tag,rank;
3650:   PetscInt               *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap.n,row,*b_othi,*b_othj;
3651:   PetscScalar            *rvalues,*svalues,*b_otha,*bufa,*bufA;
3652:   PetscInt               i,k,l,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len;
3653:   MPI_Request            *rwaits,*swaits;
3654:   MPI_Status             *sstatus,rstatus;
3655:   PetscInt               *cols;
3656:   PetscScalar            *vals;
3657:   PetscMPIInt            j;
3658: 
3660:   if (A->cmap.rstart != B->rmap.rstart || A->cmap.rend != B->rmap.rend){
3661:     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);
3662:   }
3663:   if (!logkey_GetBrowsOfAocols) {
3664:     PetscLogEventRegister(&logkey_GetBrowsOfAocols,"MatGetBrAoCol",MAT_COOKIE);
3665:   }
3666:   PetscLogEventBegin(logkey_GetBrowsOfAocols,A,B,0,0);
3667:   MPI_Comm_rank(comm,&rank);

3669:   gen_to   = (VecScatter_MPI_General*)ctx->todata;
3670:   gen_from = (VecScatter_MPI_General*)ctx->fromdata;
3671:   rvalues  = gen_from->values; /* holds the length of sending row */
3672:   svalues  = gen_to->values;   /* holds the length of receiving row */
3673:   nrecvs   = gen_from->n;
3674:   nsends   = gen_to->n;
3675:   rwaits   = gen_from->requests;
3676:   swaits   = gen_to->requests;
3677:   srow     = gen_to->indices;   /* local row index to be sent */
3678:   rstarts  = gen_from->starts;
3679:   sstarts  = gen_to->starts;
3680:   rprocs   = gen_from->procs;
3681:   sprocs   = gen_to->procs;
3682:   sstatus  = gen_to->sstatus;

3684:   if (!startsj || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
3685:   if (scall == MAT_INITIAL_MATRIX){
3686:     /* i-array */
3687:     /*---------*/
3688:     /*  post receives */
3689:     for (i=0; i<nrecvs; i++){
3690:       rowlen = (PetscInt*)rvalues + rstarts[i];
3691:       nrows = rstarts[i+1]-rstarts[i];
3692:       MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
3693:     }

3695:     /* pack the outgoing message */
3696:     PetscMalloc((nsends+nrecvs+3)*sizeof(PetscInt),&sstartsj);
3697:     rstartsj = sstartsj + nsends +1;
3698:     sstartsj[0] = 0;  rstartsj[0] = 0;
3699:     len = 0; /* total length of j or a array to be sent */
3700:     k = 0;
3701:     for (i=0; i<nsends; i++){
3702:       rowlen = (PetscInt*)svalues + sstarts[i];
3703:       nrows = sstarts[i+1]-sstarts[i]; /* num of rows */
3704:       for (j=0; j<nrows; j++) {
3705:         row = srow[k] + B->rmap.range[rank]; /* global row idx */
3706:         MatGetRow_MPIAIJ(B,row,&rowlen[j],PETSC_NULL,PETSC_NULL); /* rowlength */
3707:         len += rowlen[j];
3708:         MatRestoreRow_MPIAIJ(B,row,&ncols,PETSC_NULL,PETSC_NULL);
3709:         k++;
3710:       }
3711:       MPI_Isend(rowlen,nrows,MPIU_INT,sprocs[i],tag,comm,swaits+i);
3712:        sstartsj[i+1] = len;  /* starting point of (i+1)-th outgoing msg in bufj and bufa */
3713:     }
3714:     /* recvs and sends of i-array are completed */
3715:     i = nrecvs;
3716:     while (i--) {
3717:       MPI_Waitany(nrecvs,rwaits,&j,&rstatus);
3718:     }
3719:     if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
3720:     /* allocate buffers for sending j and a arrays */
3721:     PetscMalloc((len+1)*sizeof(PetscInt),&bufj);
3722:     PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);

3724:     /* create i-array of B_oth */
3725:     PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);
3726:     b_othi[0] = 0;
3727:     len = 0; /* total length of j or a array to be received */
3728:     k = 0;
3729:     for (i=0; i<nrecvs; i++){
3730:       rowlen = (PetscInt*)rvalues + rstarts[i];
3731:       nrows = rstarts[i+1]-rstarts[i];
3732:       for (j=0; j<nrows; j++) {
3733:         b_othi[k+1] = b_othi[k] + rowlen[j];
3734:         len += rowlen[j]; k++;
3735:       }
3736:       rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
3737:     }

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

3743:     /* j-array */
3744:     /*---------*/
3745:     /*  post receives of j-array */
3746:     for (i=0; i<nrecvs; i++){
3747:       nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
3748:       MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
3749:     }
3750:     k = 0;
3751:     for (i=0; i<nsends; i++){
3752:       nrows = sstarts[i+1]-sstarts[i]; /* num of rows */
3753:       bufJ = bufj+sstartsj[i];
3754:       for (j=0; j<nrows; j++) {
3755:         row  = srow[k++] + B->rmap.range[rank]; /* global row idx */
3756:         MatGetRow_MPIAIJ(B,row,&ncols,&cols,PETSC_NULL);
3757:         for (l=0; l<ncols; l++){
3758:           *bufJ++ = cols[l];
3759:         }
3760:         MatRestoreRow_MPIAIJ(B,row,&ncols,&cols,PETSC_NULL);
3761:       }
3762:       MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);
3763:     }

3765:     /* recvs and sends of j-array are completed */
3766:     i = nrecvs;
3767:     while (i--) {
3768:       MPI_Waitany(nrecvs,rwaits,&j,&rstatus);
3769:     }
3770:     if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
3771:   } else if (scall == MAT_REUSE_MATRIX){
3772:     sstartsj = *startsj;
3773:     rstartsj = sstartsj + nsends +1;
3774:     bufa     = *bufa_ptr;
3775:     b_oth    = (Mat_SeqAIJ*)(*B_oth)->data;
3776:     b_otha   = b_oth->a;
3777:   } else {
3778:     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");
3779:   }

3781:   /* a-array */
3782:   /*---------*/
3783:   /*  post receives of a-array */
3784:   for (i=0; i<nrecvs; i++){
3785:     nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
3786:     MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);
3787:   }
3788:   k = 0;
3789:   for (i=0; i<nsends; i++){
3790:     nrows = sstarts[i+1]-sstarts[i];
3791:     bufA = bufa+sstartsj[i];
3792:     for (j=0; j<nrows; j++) {
3793:       row  = srow[k++] + B->rmap.range[rank]; /* global row idx */
3794:       MatGetRow_MPIAIJ(B,row,&ncols,PETSC_NULL,&vals);
3795:       for (l=0; l<ncols; l++){
3796:         *bufA++ = vals[l];
3797:       }
3798:       MatRestoreRow_MPIAIJ(B,row,&ncols,PETSC_NULL,&vals);

3800:     }
3801:     MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);
3802:   }
3803:   /* recvs and sends of a-array are completed */
3804:   i = nrecvs;
3805:   while (i--) {
3806:     MPI_Waitany(nrecvs,rwaits,&j,&rstatus);
3807:   }
3808:    if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
3809: 
3810:   if (scall == MAT_INITIAL_MATRIX){
3811:     /* put together the new matrix */
3812:     MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap.N,b_othi,b_othj,b_otha,B_oth);

3814:     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
3815:     /* Since these are PETSc arrays, change flags to free them as necessary. */
3816:     b_oth = (Mat_SeqAIJ *)(*B_oth)->data;
3817:     b_oth->freedata = PETSC_TRUE;
3818:     b_oth->nonew    = 0;

3820:     PetscFree(bufj);
3821:     if (!startsj || !bufa_ptr){
3822:       PetscFree(sstartsj);
3823:       PetscFree(bufa_ptr);
3824:     } else {
3825:       *startsj  = sstartsj;
3826:       *bufa_ptr = bufa;
3827:     }
3828:   }
3829:   PetscLogEventEnd(logkey_GetBrowsOfAocols,A,B,0,0);
3830: 
3831:   return(0);
3832: }

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

3839:   Not Collective

3841:   Input Parameters:
3842: . A - The matrix in mpiaij format

3844:   Output Parameter:
3845: + lvec - The local vector holding off-process values from the argument to a matrix-vector product
3846: . colmap - A map from global column index to local index into lvec
3847: - multScatter - A scatter from the argument of a matrix-vector product to lvec

3849:   Level: developer

3851: @*/
3852: #if defined (PETSC_USE_CTABLE)
3853: PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
3854: #else
3855: PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
3856: #endif
3857: {
3858:   Mat_MPIAIJ *a;

3865:   a = (Mat_MPIAIJ *) A->data;
3866:   if (lvec) *lvec = a->lvec;
3867:   if (colmap) *colmap = a->colmap;
3868:   if (multScatter) *multScatter = a->Mvctx;
3869:   return(0);
3870: }

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

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

3878:   Level: beginner

3880: .seealso: MatCreateMPIAIJ
3881: M*/

3886: PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MPIAIJ(Mat B)
3887: {
3888:   Mat_MPIAIJ     *b;
3890:   PetscMPIInt    size;

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

3895:   PetscNew(Mat_MPIAIJ,&b);
3896:   B->data         = (void*)b;
3897:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
3898:   B->factor       = 0;
3899:   B->rmap.bs      = 1;
3900:   B->assembled    = PETSC_FALSE;
3901:   B->mapping      = 0;

3903:   B->insertmode      = NOT_SET_VALUES;
3904:   b->size            = size;
3905:   MPI_Comm_rank(B->comm,&b->rank);

3907:   /* build cache for off array entries formed */
3908:   MatStashCreate_Private(B->comm,1,&B->stash);
3909:   b->donotstash  = PETSC_FALSE;
3910:   b->colmap      = 0;
3911:   b->garray      = 0;
3912:   b->roworiented = PETSC_TRUE;

3914:   /* stuff used for matrix vector multiply */
3915:   b->lvec      = PETSC_NULL;
3916:   b->Mvctx     = PETSC_NULL;

3918:   /* stuff for MatGetRow() */
3919:   b->rowindices   = 0;
3920:   b->rowvalues    = 0;
3921:   b->getrowactive = PETSC_FALSE;


3924:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3925:                                      "MatStoreValues_MPIAIJ",
3926:                                      MatStoreValues_MPIAIJ);
3927:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3928:                                      "MatRetrieveValues_MPIAIJ",
3929:                                      MatRetrieveValues_MPIAIJ);
3930:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
3931:                                      "MatGetDiagonalBlock_MPIAIJ",
3932:                                      MatGetDiagonalBlock_MPIAIJ);
3933:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
3934:                                      "MatIsTranspose_MPIAIJ",
3935:                                      MatIsTranspose_MPIAIJ);
3936:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C",
3937:                                      "MatMPIAIJSetPreallocation_MPIAIJ",
3938:                                      MatMPIAIJSetPreallocation_MPIAIJ);
3939:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",
3940:                                      "MatMPIAIJSetPreallocationCSR_MPIAIJ",
3941:                                      MatMPIAIJSetPreallocationCSR_MPIAIJ);
3942:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
3943:                                      "MatDiagonalScaleLocal_MPIAIJ",
3944:                                      MatDiagonalScaleLocal_MPIAIJ);
3945:   return(0);
3946: }

3949: /*
3950:     Special version for direct calls from Fortran 
3951: */
3952: #if defined(PETSC_HAVE_FORTRAN_CAPS)
3953: #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
3954: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
3955: #define matsetvaluesmpiaij_ matsetvaluesmpiaij
3956: #endif

3958: /* Change these macros so can be used in void function */
3959: #undef CHKERRQ
3960: #define CHKERRQ(ierr) CHKERRABORT(mat->comm,ierr) 
3961: #undef SETERRQ2
3962: #define SETERRQ2(ierr,b,c,d) CHKERRABORT(mat->comm,ierr) 
3963: #undef SETERRQ
3964: #define SETERRQ(ierr,b) CHKERRABORT(mat->comm,ierr) 

3969: void matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv)
3970: {
3971:   Mat            mat = *mmat;
3972:   PetscInt       m = *mm, n = *mn;
3973:   InsertMode     addv = *maddv;
3974:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
3975:   PetscScalar    value;

3978:   MatPreallocated(mat);
3979:   if (mat->insertmode == NOT_SET_VALUES) {
3980:     mat->insertmode = addv;
3981:   }
3982: #if defined(PETSC_USE_DEBUG)
3983:   else if (mat->insertmode != addv) {
3984:     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
3985:   }
3986: #endif
3987:   {
3988:   PetscInt       i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
3989:   PetscInt       cstart = mat->cmap.rstart,cend = mat->cmap.rend,row,col;
3990:   PetscTruth     roworiented = aij->roworiented;

3992:   /* Some Variables required in the macro */
3993:   Mat            A = aij->A;
3994:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3995:   PetscInt       *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
3996:   PetscScalar    *aa = a->a;
3997:   PetscTruth     ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
3998:   Mat            B = aij->B;
3999:   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
4000:   PetscInt       *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap.n,am = aij->A->rmap.n;
4001:   PetscScalar    *ba = b->a;

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

4008:   for (i=0; i<m; i++) {
4009:     if (im[i] < 0) continue;
4010: #if defined(PETSC_USE_DEBUG)
4011:     if (im[i] >= mat->rmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap.N-1);
4012: #endif
4013:     if (im[i] >= rstart && im[i] < rend) {
4014:       row      = im[i] - rstart;
4015:       lastcol1 = -1;
4016:       rp1      = aj + ai[row];
4017:       ap1      = aa + ai[row];
4018:       rmax1    = aimax[row];
4019:       nrow1    = ailen[row];
4020:       low1     = 0;
4021:       high1    = nrow1;
4022:       lastcol2 = -1;
4023:       rp2      = bj + bi[row];
4024:       ap2      = ba + bi[row];
4025:       rmax2    = bimax[row];
4026:       nrow2    = bilen[row];
4027:       low2     = 0;
4028:       high2    = nrow2;

4030:       for (j=0; j<n; j++) {
4031:         if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
4032:         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
4033:         if (in[j] >= cstart && in[j] < cend){
4034:           col = in[j] - cstart;
4035:           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
4036:         } else if (in[j] < 0) continue;
4037: #if defined(PETSC_USE_DEBUG)
4038:         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);}
4039: #endif
4040:         else {
4041:           if (mat->was_assembled) {
4042:             if (!aij->colmap) {
4043:               CreateColmap_MPIAIJ_Private(mat);
4044:             }
4045: #if defined (PETSC_USE_CTABLE)
4046:             PetscTableFind(aij->colmap,in[j]+1,&col);
4047:             col--;
4048: #else
4049:             col = aij->colmap[in[j]] - 1;
4050: #endif
4051:             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
4052:               DisAssemble_MPIAIJ(mat);
4053:               col =  in[j];
4054:               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
4055:               B = aij->B;
4056:               b = (Mat_SeqAIJ*)B->data;
4057:               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
4058:               rp2      = bj + bi[row];
4059:               ap2      = ba + bi[row];
4060:               rmax2    = bimax[row];
4061:               nrow2    = bilen[row];
4062:               low2     = 0;
4063:               high2    = nrow2;
4064:               bm       = aij->B->rmap.n;
4065:               ba = b->a;
4066:             }
4067:           } else col = in[j];
4068:           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
4069:         }
4070:       }
4071:     } else {
4072:       if (!aij->donotstash) {
4073:         if (roworiented) {
4074:           if (ignorezeroentries && v[i*n] == 0.0) continue;
4075:           MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);
4076:         } else {
4077:           if (ignorezeroentries && v[i] == 0.0) continue;
4078:           MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);
4079:         }
4080:       }
4081:     }
4082:   }}
4083:   PetscFunctionReturnVoid();
4084: }