Actual source code: mpibaij.c

petsc-master 2016-08-28
Report Typos and Errors
 2:  #include <../src/mat/impls/baij/mpi/mpibaij.h>

 4:  #include <petscblaslapack.h>
 5:  #include <petscsf.h>

  9: PetscErrorCode MatGetRowMaxAbs_MPIBAIJ(Mat A,Vec v,PetscInt idx[])
 10: {
 11:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
 13:   PetscInt       i,*idxb = 0;
 14:   PetscScalar    *va,*vb;
 15:   Vec            vtmp;

 18:   MatGetRowMaxAbs(a->A,v,idx);
 19:   VecGetArray(v,&va);
 20:   if (idx) {
 21:     for (i=0; i<A->rmap->n; i++) {
 22:       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
 23:     }
 24:   }

 26:   VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);
 27:   if (idx) {PetscMalloc1(A->rmap->n,&idxb);}
 28:   MatGetRowMaxAbs(a->B,vtmp,idxb);
 29:   VecGetArray(vtmp,&vb);

 31:   for (i=0; i<A->rmap->n; i++) {
 32:     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
 33:       va[i] = vb[i];
 34:       if (idx) idx[i] = A->cmap->bs*a->garray[idxb[i]/A->cmap->bs] + (idxb[i] % A->cmap->bs);
 35:     }
 36:   }

 38:   VecRestoreArray(v,&va);
 39:   VecRestoreArray(vtmp,&vb);
 40:   PetscFree(idxb);
 41:   VecDestroy(&vtmp);
 42:   return(0);
 43: }

 47: PetscErrorCode  MatStoreValues_MPIBAIJ(Mat mat)
 48: {
 49:   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ*)mat->data;

 53:   MatStoreValues(aij->A);
 54:   MatStoreValues(aij->B);
 55:   return(0);
 56: }

 60: PetscErrorCode  MatRetrieveValues_MPIBAIJ(Mat mat)
 61: {
 62:   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ*)mat->data;

 66:   MatRetrieveValues(aij->A);
 67:   MatRetrieveValues(aij->B);
 68:   return(0);
 69: }

 71: /*
 72:      Local utility routine that creates a mapping from the global column
 73:    number to the local number in the off-diagonal part of the local
 74:    storage of the matrix.  This is done in a non scalable way since the
 75:    length of colmap equals the global matrix length.
 76: */
 79: PetscErrorCode MatCreateColmap_MPIBAIJ_Private(Mat mat)
 80: {
 81:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
 82:   Mat_SeqBAIJ    *B    = (Mat_SeqBAIJ*)baij->B->data;
 84:   PetscInt       nbs = B->nbs,i,bs=mat->rmap->bs;

 87: #if defined(PETSC_USE_CTABLE)
 88:   PetscTableCreate(baij->nbs,baij->Nbs+1,&baij->colmap);
 89:   for (i=0; i<nbs; i++) {
 90:     PetscTableAdd(baij->colmap,baij->garray[i]+1,i*bs+1,INSERT_VALUES);
 91:   }
 92: #else
 93:   PetscMalloc1(baij->Nbs+1,&baij->colmap);
 94:   PetscLogObjectMemory((PetscObject)mat,baij->Nbs*sizeof(PetscInt));
 95:   PetscMemzero(baij->colmap,baij->Nbs*sizeof(PetscInt));
 96:   for (i=0; i<nbs; i++) baij->colmap[baij->garray[i]] = i*bs+1;
 97: #endif
 98:   return(0);
 99: }

101: #define  MatSetValues_SeqBAIJ_A_Private(row,col,value,addv,orow,ocol)       \
102:   { \
103:  \
104:     brow = row/bs;  \
105:     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
106:     rmax = aimax[brow]; nrow = ailen[brow]; \
107:     bcol = col/bs; \
108:     ridx = row % bs; cidx = col % bs; \
109:     low  = 0; high = nrow; \
110:     while (high-low > 3) { \
111:       t = (low+high)/2; \
112:       if (rp[t] > bcol) high = t; \
113:       else              low  = t; \
114:     } \
115:     for (_i=low; _i<high; _i++) { \
116:       if (rp[_i] > bcol) break; \
117:       if (rp[_i] == bcol) { \
118:         bap = ap +  bs2*_i + bs*cidx + ridx; \
119:         if (addv == ADD_VALUES) *bap += value;  \
120:         else                    *bap  = value;  \
121:         goto a_noinsert; \
122:       } \
123:     } \
124:     if (a->nonew == 1) goto a_noinsert; \
125:     if (a->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at global row/column (%D, %D) into matrix", orow, ocol); \
126:     MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,aimax,a->nonew,MatScalar); \
127:     N = nrow++ - 1;  \
128:     /* shift up all the later entries in this row */ \
129:     for (ii=N; ii>=_i; ii--) { \
130:       rp[ii+1] = rp[ii]; \
131:       PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar)); \
132:     } \
133:     if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar)); }  \
134:     rp[_i]                      = bcol;  \
135:     ap[bs2*_i + bs*cidx + ridx] = value;  \
136: a_noinsert:; \
137:     ailen[brow] = nrow; \
138:   }

140: #define  MatSetValues_SeqBAIJ_B_Private(row,col,value,addv,orow,ocol)       \
141:   { \
142:     brow = row/bs;  \
143:     rp   = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
144:     rmax = bimax[brow]; nrow = bilen[brow]; \
145:     bcol = col/bs; \
146:     ridx = row % bs; cidx = col % bs; \
147:     low  = 0; high = nrow; \
148:     while (high-low > 3) { \
149:       t = (low+high)/2; \
150:       if (rp[t] > bcol) high = t; \
151:       else              low  = t; \
152:     } \
153:     for (_i=low; _i<high; _i++) { \
154:       if (rp[_i] > bcol) break; \
155:       if (rp[_i] == bcol) { \
156:         bap = ap +  bs2*_i + bs*cidx + ridx; \
157:         if (addv == ADD_VALUES) *bap += value;  \
158:         else                    *bap  = value;  \
159:         goto b_noinsert; \
160:       } \
161:     } \
162:     if (b->nonew == 1) goto b_noinsert; \
163:     if (b->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at global row/column  (%D, %D) into matrix", orow, ocol); \
164:     MatSeqXAIJReallocateAIJ(B,b->mbs,bs2,nrow,brow,bcol,rmax,ba,bi,bj,rp,ap,bimax,b->nonew,MatScalar); \
165:     N = nrow++ - 1;  \
166:     /* shift up all the later entries in this row */ \
167:     for (ii=N; ii>=_i; ii--) { \
168:       rp[ii+1] = rp[ii]; \
169:       PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar)); \
170:     } \
171:     if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));}  \
172:     rp[_i]                      = bcol;  \
173:     ap[bs2*_i + bs*cidx + ridx] = value;  \
174: b_noinsert:; \
175:     bilen[brow] = nrow; \
176:   }

180: PetscErrorCode MatSetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
181: {
182:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
183:   MatScalar      value;
184:   PetscBool      roworiented = baij->roworiented;
186:   PetscInt       i,j,row,col;
187:   PetscInt       rstart_orig=mat->rmap->rstart;
188:   PetscInt       rend_orig  =mat->rmap->rend,cstart_orig=mat->cmap->rstart;
189:   PetscInt       cend_orig  =mat->cmap->rend,bs=mat->rmap->bs;

191:   /* Some Variables required in the macro */
192:   Mat         A     = baij->A;
193:   Mat_SeqBAIJ *a    = (Mat_SeqBAIJ*)(A)->data;
194:   PetscInt    *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
195:   MatScalar   *aa   =a->a;

197:   Mat         B     = baij->B;
198:   Mat_SeqBAIJ *b    = (Mat_SeqBAIJ*)(B)->data;
199:   PetscInt    *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
200:   MatScalar   *ba   =b->a;

202:   PetscInt  *rp,ii,nrow,_i,rmax,N,brow,bcol;
203:   PetscInt  low,high,t,ridx,cidx,bs2=a->bs2;
204:   MatScalar *ap,*bap;

207:   for (i=0; i<m; i++) {
208:     if (im[i] < 0) continue;
209: #if defined(PETSC_USE_DEBUG)
210:     if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
211: #endif
212:     if (im[i] >= rstart_orig && im[i] < rend_orig) {
213:       row = im[i] - rstart_orig;
214:       for (j=0; j<n; j++) {
215:         if (in[j] >= cstart_orig && in[j] < cend_orig) {
216:           col = in[j] - cstart_orig;
217:           if (roworiented) value = v[i*n+j];
218:           else             value = v[i+j*m];
219:           MatSetValues_SeqBAIJ_A_Private(row,col,value,addv,im[i],in[j]);
220:           /* MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv); */
221:         } else if (in[j] < 0) continue;
222: #if defined(PETSC_USE_DEBUG)
223:         else if (in[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap->N-1);
224: #endif
225:         else {
226:           if (mat->was_assembled) {
227:             if (!baij->colmap) {
228:               MatCreateColmap_MPIBAIJ_Private(mat);
229:             }
230: #if defined(PETSC_USE_CTABLE)
231:             PetscTableFind(baij->colmap,in[j]/bs + 1,&col);
232:             col  = col - 1;
233: #else
234:             col = baij->colmap[in[j]/bs] - 1;
235: #endif
236:             if (col < 0 && !((Mat_SeqBAIJ*)(baij->B->data))->nonew) {
237:               MatDisAssemble_MPIBAIJ(mat);
238:               col  =  in[j];
239:               /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
240:               B    = baij->B;
241:               b    = (Mat_SeqBAIJ*)(B)->data;
242:               bimax=b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
243:               ba   =b->a;
244:             } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", im[i], in[j]);
245:             else col += in[j]%bs;
246:           } else col = in[j];
247:           if (roworiented) value = v[i*n+j];
248:           else             value = v[i+j*m];
249:           MatSetValues_SeqBAIJ_B_Private(row,col,value,addv,im[i],in[j]);
250:           /* MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv); */
251:         }
252:       }
253:     } else {
254:       if (mat->nooffprocentries) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Setting off process row %D even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set",im[i]);
255:       if (!baij->donotstash) {
256:         mat->assembled = PETSC_FALSE;
257:         if (roworiented) {
258:           MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);
259:         } else {
260:           MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);
261:         }
262:       }
263:     }
264:   }
265:   return(0);
266: }

270: PETSC_STATIC_INLINE PetscErrorCode MatSetValuesBlocked_SeqBAIJ_Inlined(Mat A,PetscInt row,PetscInt col,const PetscScalar v[],InsertMode is,PetscInt orow,PetscInt ocol)
271: {
272:   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
273:   PetscInt          *rp,low,high,t,ii,jj,nrow,i,rmax,N;
274:   PetscInt          *imax=a->imax,*ai=a->i,*ailen=a->ilen;
275:   PetscErrorCode    ierr;
276:   PetscInt          *aj        =a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs;
277:   PetscBool         roworiented=a->roworiented;
278:   const PetscScalar *value     = v;
279:   MatScalar         *ap,*aa = a->a,*bap;

282:   rp   = aj + ai[row];
283:   ap   = aa + bs2*ai[row];
284:   rmax = imax[row];
285:   nrow = ailen[row];
286:   value = v;
287:   low = 0;
288:   high = nrow;
289:   while (high-low > 7) {
290:     t = (low+high)/2;
291:     if (rp[t] > col) high = t;
292:     else             low  = t;
293:   }
294:   for (i=low; i<high; i++) {
295:     if (rp[i] > col) break;
296:     if (rp[i] == col) {
297:       bap = ap +  bs2*i;
298:       if (roworiented) {
299:         if (is == ADD_VALUES) {
300:           for (ii=0; ii<bs; ii++) {
301:             for (jj=ii; jj<bs2; jj+=bs) {
302:               bap[jj] += *value++;
303:             }
304:           }
305:         } else {
306:           for (ii=0; ii<bs; ii++) {
307:             for (jj=ii; jj<bs2; jj+=bs) {
308:               bap[jj] = *value++;
309:             }
310:           }
311:         }
312:       } else {
313:         if (is == ADD_VALUES) {
314:           for (ii=0; ii<bs; ii++,value+=bs) {
315:             for (jj=0; jj<bs; jj++) {
316:               bap[jj] += value[jj];
317:             }
318:             bap += bs;
319:           }
320:         } else {
321:           for (ii=0; ii<bs; ii++,value+=bs) {
322:             for (jj=0; jj<bs; jj++) {
323:               bap[jj]  = value[jj];
324:             }
325:             bap += bs;
326:           }
327:         }
328:       }
329:       goto noinsert2;
330:     }
331:   }
332:   if (nonew == 1) goto noinsert2;
333:   if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new global block indexed nonzero block (%D, %D) in the matrix", orow, ocol);
334:   MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
335:   N = nrow++ - 1; high++;
336:   /* shift up all the later entries in this row */
337:   for (ii=N; ii>=i; ii--) {
338:     rp[ii+1] = rp[ii];
339:     PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));
340:   }
341:   if (N >= i) {
342:     PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));
343:   }
344:   rp[i] = col;
345:   bap   = ap +  bs2*i;
346:   if (roworiented) {
347:     for (ii=0; ii<bs; ii++) {
348:       for (jj=ii; jj<bs2; jj+=bs) {
349:         bap[jj] = *value++;
350:       }
351:     }
352:   } else {
353:     for (ii=0; ii<bs; ii++) {
354:       for (jj=0; jj<bs; jj++) {
355:         *bap++ = *value++;
356:       }
357:     }
358:   }
359:   noinsert2:;
360:   ailen[row] = nrow;
361:   return(0);
362: }

366: /*
367:     This routine should be optimized so that the block copy at ** Here a copy is required ** below is not needed
368:     by passing additional stride information into the MatSetValuesBlocked_SeqBAIJ_Inlined() routine
369: */
370: PetscErrorCode MatSetValuesBlocked_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
371: {
372:   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
373:   const PetscScalar *value;
374:   MatScalar         *barray     = baij->barray;
375:   PetscBool         roworiented = baij->roworiented;
376:   PetscErrorCode    ierr;
377:   PetscInt          i,j,ii,jj,row,col,rstart=baij->rstartbs;
378:   PetscInt          rend=baij->rendbs,cstart=baij->cstartbs,stepval;
379:   PetscInt          cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;

382:   if (!barray) {
383:     PetscMalloc1(bs2,&barray);
384:     baij->barray = barray;
385:   }

387:   if (roworiented) stepval = (n-1)*bs;
388:   else stepval = (m-1)*bs;

390:   for (i=0; i<m; i++) {
391:     if (im[i] < 0) continue;
392: #if defined(PETSC_USE_DEBUG)
393:     if (im[i] >= baij->Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Block indexed row too large %D max %D",im[i],baij->Mbs-1);
394: #endif
395:     if (im[i] >= rstart && im[i] < rend) {
396:       row = im[i] - rstart;
397:       for (j=0; j<n; j++) {
398:         /* If NumCol = 1 then a copy is not required */
399:         if ((roworiented) && (n == 1)) {
400:           barray = (MatScalar*)v + i*bs2;
401:         } else if ((!roworiented) && (m == 1)) {
402:           barray = (MatScalar*)v + j*bs2;
403:         } else { /* Here a copy is required */
404:           if (roworiented) {
405:             value = v + (i*(stepval+bs) + j)*bs;
406:           } else {
407:             value = v + (j*(stepval+bs) + i)*bs;
408:           }
409:           for (ii=0; ii<bs; ii++,value+=bs+stepval) {
410:             for (jj=0; jj<bs; jj++) barray[jj] = value[jj];
411:             barray += bs;
412:           }
413:           barray -= bs2;
414:         }

416:         if (in[j] >= cstart && in[j] < cend) {
417:           col  = in[j] - cstart;
418:           MatSetValuesBlocked_SeqBAIJ_Inlined(baij->A,row,col,barray,addv,im[i],in[j]);
419:         } else if (in[j] < 0) continue;
420: #if defined(PETSC_USE_DEBUG)
421:         else if (in[j] >= baij->Nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Block indexed column too large %D max %D",in[j],baij->Nbs-1);
422: #endif
423:         else {
424:           if (mat->was_assembled) {
425:             if (!baij->colmap) {
426:               MatCreateColmap_MPIBAIJ_Private(mat);
427:             }

429: #if defined(PETSC_USE_DEBUG)
430: #if defined(PETSC_USE_CTABLE)
431:             { PetscInt data;
432:               PetscTableFind(baij->colmap,in[j]+1,&data);
433:               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
434:             }
435: #else
436:             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
437: #endif
438: #endif
439: #if defined(PETSC_USE_CTABLE)
440:             PetscTableFind(baij->colmap,in[j]+1,&col);
441:             col  = (col - 1)/bs;
442: #else
443:             col = (baij->colmap[in[j]] - 1)/bs;
444: #endif
445:             if (col < 0 && !((Mat_SeqBAIJ*)(baij->B->data))->nonew) {
446:               MatDisAssemble_MPIBAIJ(mat);
447:               col  =  in[j];
448:             } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new blocked indexed nonzero block (%D, %D) into matrix",im[i],in[j]);
449:           } else col = in[j];
450:           MatSetValuesBlocked_SeqBAIJ_Inlined(baij->B,row,col,barray,addv,im[i],in[j]);
451:         }
452:       }
453:     } else {
454:       if (mat->nooffprocentries) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Setting off process block indexed row %D even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set",im[i]);
455:       if (!baij->donotstash) {
456:         if (roworiented) {
457:           MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
458:         } else {
459:           MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
460:         }
461:       }
462:     }
463:   }
464:   return(0);
465: }

467: #define HASH_KEY 0.6180339887
468: #define HASH(size,key,tmp) (tmp = (key)*HASH_KEY,(PetscInt)((size)*(tmp-(PetscInt)tmp)))
469: /* #define HASH(size,key) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
470: /* #define HASH(size,key,tmp) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
473: PetscErrorCode MatSetValues_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
474: {
475:   Mat_MPIBAIJ    *baij       = (Mat_MPIBAIJ*)mat->data;
476:   PetscBool      roworiented = baij->roworiented;
478:   PetscInt       i,j,row,col;
479:   PetscInt       rstart_orig=mat->rmap->rstart;
480:   PetscInt       rend_orig  =mat->rmap->rend,Nbs=baij->Nbs;
481:   PetscInt       h1,key,size=baij->ht_size,bs=mat->rmap->bs,*HT=baij->ht,idx;
482:   PetscReal      tmp;
483:   MatScalar      **HD = baij->hd,value;
484: #if defined(PETSC_USE_DEBUG)
485:   PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
486: #endif

489:   for (i=0; i<m; i++) {
490: #if defined(PETSC_USE_DEBUG)
491:     if (im[i] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
492:     if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
493: #endif
494:     row = im[i];
495:     if (row >= rstart_orig && row < rend_orig) {
496:       for (j=0; j<n; j++) {
497:         col = in[j];
498:         if (roworiented) value = v[i*n+j];
499:         else             value = v[i+j*m];
500:         /* Look up PetscInto the Hash Table */
501:         key = (row/bs)*Nbs+(col/bs)+1;
502:         h1  = HASH(size,key,tmp);


505:         idx = h1;
506: #if defined(PETSC_USE_DEBUG)
507:         insert_ct++;
508:         total_ct++;
509:         if (HT[idx] != key) {
510:           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++) ;
511:           if (idx == size) {
512:             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++) ;
513:             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
514:           }
515:         }
516: #else
517:         if (HT[idx] != key) {
518:           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++) ;
519:           if (idx == size) {
520:             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++) ;
521:             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
522:           }
523:         }
524: #endif
525:         /* A HASH table entry is found, so insert the values at the correct address */
526:         if (addv == ADD_VALUES) *(HD[idx]+ (col % bs)*bs + (row % bs)) += value;
527:         else                    *(HD[idx]+ (col % bs)*bs + (row % bs))  = value;
528:       }
529:     } else if (!baij->donotstash) {
530:       if (roworiented) {
531:         MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);
532:       } else {
533:         MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);
534:       }
535:     }
536:   }
537: #if defined(PETSC_USE_DEBUG)
538:   baij->ht_total_ct  = total_ct;
539:   baij->ht_insert_ct = insert_ct;
540: #endif
541:   return(0);
542: }

546: PetscErrorCode MatSetValuesBlocked_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
547: {
548:   Mat_MPIBAIJ       *baij       = (Mat_MPIBAIJ*)mat->data;
549:   PetscBool         roworiented = baij->roworiented;
550:   PetscErrorCode    ierr;
551:   PetscInt          i,j,ii,jj,row,col;
552:   PetscInt          rstart=baij->rstartbs;
553:   PetscInt          rend  =mat->rmap->rend,stepval,bs=mat->rmap->bs,bs2=baij->bs2,nbs2=n*bs2;
554:   PetscInt          h1,key,size=baij->ht_size,idx,*HT=baij->ht,Nbs=baij->Nbs;
555:   PetscReal         tmp;
556:   MatScalar         **HD = baij->hd,*baij_a;
557:   const PetscScalar *v_t,*value;
558: #if defined(PETSC_USE_DEBUG)
559:   PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
560: #endif

563:   if (roworiented) stepval = (n-1)*bs;
564:   else stepval = (m-1)*bs;

566:   for (i=0; i<m; i++) {
567: #if defined(PETSC_USE_DEBUG)
568:     if (im[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",im[i]);
569:     if (im[i] >= baij->Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],baij->Mbs-1);
570: #endif
571:     row = im[i];
572:     v_t = v + i*nbs2;
573:     if (row >= rstart && row < rend) {
574:       for (j=0; j<n; j++) {
575:         col = in[j];

577:         /* Look up into the Hash Table */
578:         key = row*Nbs+col+1;
579:         h1  = HASH(size,key,tmp);

581:         idx = h1;
582: #if defined(PETSC_USE_DEBUG)
583:         total_ct++;
584:         insert_ct++;
585:         if (HT[idx] != key) {
586:           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++) ;
587:           if (idx == size) {
588:             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++) ;
589:             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
590:           }
591:         }
592: #else
593:         if (HT[idx] != key) {
594:           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++) ;
595:           if (idx == size) {
596:             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++) ;
597:             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
598:           }
599:         }
600: #endif
601:         baij_a = HD[idx];
602:         if (roworiented) {
603:           /*value = v + i*(stepval+bs)*bs + j*bs;*/
604:           /* value = v + (i*(stepval+bs)+j)*bs; */
605:           value = v_t;
606:           v_t  += bs;
607:           if (addv == ADD_VALUES) {
608:             for (ii=0; ii<bs; ii++,value+=stepval) {
609:               for (jj=ii; jj<bs2; jj+=bs) {
610:                 baij_a[jj] += *value++;
611:               }
612:             }
613:           } else {
614:             for (ii=0; ii<bs; ii++,value+=stepval) {
615:               for (jj=ii; jj<bs2; jj+=bs) {
616:                 baij_a[jj] = *value++;
617:               }
618:             }
619:           }
620:         } else {
621:           value = v + j*(stepval+bs)*bs + i*bs;
622:           if (addv == ADD_VALUES) {
623:             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
624:               for (jj=0; jj<bs; jj++) {
625:                 baij_a[jj] += *value++;
626:               }
627:             }
628:           } else {
629:             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
630:               for (jj=0; jj<bs; jj++) {
631:                 baij_a[jj] = *value++;
632:               }
633:             }
634:           }
635:         }
636:       }
637:     } else {
638:       if (!baij->donotstash) {
639:         if (roworiented) {
640:           MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
641:         } else {
642:           MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
643:         }
644:       }
645:     }
646:   }
647: #if defined(PETSC_USE_DEBUG)
648:   baij->ht_total_ct  = total_ct;
649:   baij->ht_insert_ct = insert_ct;
650: #endif
651:   return(0);
652: }

656: PetscErrorCode MatGetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
657: {
658:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
660:   PetscInt       bs       = mat->rmap->bs,i,j,bsrstart = mat->rmap->rstart,bsrend = mat->rmap->rend;
661:   PetscInt       bscstart = mat->cmap->rstart,bscend = mat->cmap->rend,row,col,data;

664:   for (i=0; i<m; i++) {
665:     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
666:     if (idxm[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",idxm[i],mat->rmap->N-1);
667:     if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
668:       row = idxm[i] - bsrstart;
669:       for (j=0; j<n; j++) {
670:         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
671:         if (idxn[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",idxn[j],mat->cmap->N-1);
672:         if (idxn[j] >= bscstart && idxn[j] < bscend) {
673:           col  = idxn[j] - bscstart;
674:           MatGetValues_SeqBAIJ(baij->A,1,&row,1,&col,v+i*n+j);
675:         } else {
676:           if (!baij->colmap) {
677:             MatCreateColmap_MPIBAIJ_Private(mat);
678:           }
679: #if defined(PETSC_USE_CTABLE)
680:           PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);
681:           data--;
682: #else
683:           data = baij->colmap[idxn[j]/bs]-1;
684: #endif
685:           if ((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
686:           else {
687:             col  = data + idxn[j]%bs;
688:             MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);
689:           }
690:         }
691:       }
692:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
693:   }
694:   return(0);
695: }

699: PetscErrorCode MatNorm_MPIBAIJ(Mat mat,NormType type,PetscReal *nrm)
700: {
701:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
702:   Mat_SeqBAIJ    *amat = (Mat_SeqBAIJ*)baij->A->data,*bmat = (Mat_SeqBAIJ*)baij->B->data;
704:   PetscInt       i,j,bs2=baij->bs2,bs=baij->A->rmap->bs,nz,row,col;
705:   PetscReal      sum = 0.0;
706:   MatScalar      *v;

709:   if (baij->size == 1) {
710:      MatNorm(baij->A,type,nrm);
711:   } else {
712:     if (type == NORM_FROBENIUS) {
713:       v  = amat->a;
714:       nz = amat->nz*bs2;
715:       for (i=0; i<nz; i++) {
716:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
717:       }
718:       v  = bmat->a;
719:       nz = bmat->nz*bs2;
720:       for (i=0; i<nz; i++) {
721:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
722:       }
723:       MPIU_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
724:       *nrm = PetscSqrtReal(*nrm);
725:     } else if (type == NORM_1) { /* max column sum */
726:       PetscReal *tmp,*tmp2;
727:       PetscInt  *jj,*garray=baij->garray,cstart=baij->rstartbs;
728:       PetscMalloc2(mat->cmap->N,&tmp,mat->cmap->N,&tmp2);
729:       PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));
730:       v    = amat->a; jj = amat->j;
731:       for (i=0; i<amat->nz; i++) {
732:         for (j=0; j<bs; j++) {
733:           col = bs*(cstart + *jj) + j; /* column index */
734:           for (row=0; row<bs; row++) {
735:             tmp[col] += PetscAbsScalar(*v);  v++;
736:           }
737:         }
738:         jj++;
739:       }
740:       v = bmat->a; jj = bmat->j;
741:       for (i=0; i<bmat->nz; i++) {
742:         for (j=0; j<bs; j++) {
743:           col = bs*garray[*jj] + j;
744:           for (row=0; row<bs; row++) {
745:             tmp[col] += PetscAbsScalar(*v); v++;
746:           }
747:         }
748:         jj++;
749:       }
750:       MPIU_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
751:       *nrm = 0.0;
752:       for (j=0; j<mat->cmap->N; j++) {
753:         if (tmp2[j] > *nrm) *nrm = tmp2[j];
754:       }
755:       PetscFree2(tmp,tmp2);
756:     } else if (type == NORM_INFINITY) { /* max row sum */
757:       PetscReal *sums;
758:       PetscMalloc1(bs,&sums);
759:       sum  = 0.0;
760:       for (j=0; j<amat->mbs; j++) {
761:         for (row=0; row<bs; row++) sums[row] = 0.0;
762:         v  = amat->a + bs2*amat->i[j];
763:         nz = amat->i[j+1]-amat->i[j];
764:         for (i=0; i<nz; i++) {
765:           for (col=0; col<bs; col++) {
766:             for (row=0; row<bs; row++) {
767:               sums[row] += PetscAbsScalar(*v); v++;
768:             }
769:           }
770:         }
771:         v  = bmat->a + bs2*bmat->i[j];
772:         nz = bmat->i[j+1]-bmat->i[j];
773:         for (i=0; i<nz; i++) {
774:           for (col=0; col<bs; col++) {
775:             for (row=0; row<bs; row++) {
776:               sums[row] += PetscAbsScalar(*v); v++;
777:             }
778:           }
779:         }
780:         for (row=0; row<bs; row++) {
781:           if (sums[row] > sum) sum = sums[row];
782:         }
783:       }
784:       MPIU_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));
785:       PetscFree(sums);
786:     } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for this norm yet");
787:   }
788:   return(0);
789: }

791: /*
792:   Creates the hash table, and sets the table
793:   This table is created only once.
794:   If new entried need to be added to the matrix
795:   then the hash table has to be destroyed and
796:   recreated.
797: */
800: PetscErrorCode MatCreateHashTable_MPIBAIJ_Private(Mat mat,PetscReal factor)
801: {
802:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
803:   Mat            A     = baij->A,B=baij->B;
804:   Mat_SeqBAIJ    *a    = (Mat_SeqBAIJ*)A->data,*b=(Mat_SeqBAIJ*)B->data;
805:   PetscInt       i,j,k,nz=a->nz+b->nz,h1,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
807:   PetscInt       ht_size,bs2=baij->bs2,rstart=baij->rstartbs;
808:   PetscInt       cstart=baij->cstartbs,*garray=baij->garray,row,col,Nbs=baij->Nbs;
809:   PetscInt       *HT,key;
810:   MatScalar      **HD;
811:   PetscReal      tmp;
812: #if defined(PETSC_USE_INFO)
813:   PetscInt ct=0,max=0;
814: #endif

817:   if (baij->ht) return(0);

819:   baij->ht_size = (PetscInt)(factor*nz);
820:   ht_size       = baij->ht_size;

822:   /* Allocate Memory for Hash Table */
823:   PetscCalloc2(ht_size,&baij->hd,ht_size,&baij->ht);
824:   HD   = baij->hd;
825:   HT   = baij->ht;

827:   /* Loop Over A */
828:   for (i=0; i<a->mbs; i++) {
829:     for (j=ai[i]; j<ai[i+1]; j++) {
830:       row = i+rstart;
831:       col = aj[j]+cstart;

833:       key = row*Nbs + col + 1;
834:       h1  = HASH(ht_size,key,tmp);
835:       for (k=0; k<ht_size; k++) {
836:         if (!HT[(h1+k)%ht_size]) {
837:           HT[(h1+k)%ht_size] = key;
838:           HD[(h1+k)%ht_size] = a->a + j*bs2;
839:           break;
840: #if defined(PETSC_USE_INFO)
841:         } else {
842:           ct++;
843: #endif
844:         }
845:       }
846: #if defined(PETSC_USE_INFO)
847:       if (k> max) max = k;
848: #endif
849:     }
850:   }
851:   /* Loop Over B */
852:   for (i=0; i<b->mbs; i++) {
853:     for (j=bi[i]; j<bi[i+1]; j++) {
854:       row = i+rstart;
855:       col = garray[bj[j]];
856:       key = row*Nbs + col + 1;
857:       h1  = HASH(ht_size,key,tmp);
858:       for (k=0; k<ht_size; k++) {
859:         if (!HT[(h1+k)%ht_size]) {
860:           HT[(h1+k)%ht_size] = key;
861:           HD[(h1+k)%ht_size] = b->a + j*bs2;
862:           break;
863: #if defined(PETSC_USE_INFO)
864:         } else {
865:           ct++;
866: #endif
867:         }
868:       }
869: #if defined(PETSC_USE_INFO)
870:       if (k> max) max = k;
871: #endif
872:     }
873:   }

875:   /* Print Summary */
876: #if defined(PETSC_USE_INFO)
877:   for (i=0,j=0; i<ht_size; i++) {
878:     if (HT[i]) j++;
879:   }
880:   PetscInfo2(mat,"Average Search = %5.2f,max search = %D\n",(!j)? 0.0:((PetscReal)(ct+j))/j,max);
881: #endif
882:   return(0);
883: }

887: PetscErrorCode MatAssemblyBegin_MPIBAIJ(Mat mat,MatAssemblyType mode)
888: {
889:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
891:   PetscInt       nstash,reallocs;

894:   if (baij->donotstash || mat->nooffprocentries) return(0);

896:   MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
897:   MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);
898:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
899:   PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);
900:   MatStashGetInfo_Private(&mat->bstash,&nstash,&reallocs);
901:   PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
902:   return(0);
903: }

907: PetscErrorCode MatAssemblyEnd_MPIBAIJ(Mat mat,MatAssemblyType mode)
908: {
909:   Mat_MPIBAIJ    *baij=(Mat_MPIBAIJ*)mat->data;
910:   Mat_SeqBAIJ    *a   =(Mat_SeqBAIJ*)baij->A->data;
912:   PetscInt       i,j,rstart,ncols,flg,bs2=baij->bs2;
913:   PetscInt       *row,*col;
914:   PetscBool      r1,r2,r3,other_disassembled;
915:   MatScalar      *val;
916:   PetscMPIInt    n;

919:   /* do not use 'b=(Mat_SeqBAIJ*)baij->B->data' as B can be reset in disassembly */
920:   if (!baij->donotstash && !mat->nooffprocentries) {
921:     while (1) {
922:       MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
923:       if (!flg) break;

925:       for (i=0; i<n;) {
926:         /* Now identify the consecutive vals belonging to the same row */
927:         for (j=i,rstart=row[j]; j<n; j++) {
928:           if (row[j] != rstart) break;
929:         }
930:         if (j < n) ncols = j-i;
931:         else       ncols = n-i;
932:         /* Now assemble all these values with a single function call */
933:         MatSetValues_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i,mat->insertmode);
934:         i    = j;
935:       }
936:     }
937:     MatStashScatterEnd_Private(&mat->stash);
938:     /* Now process the block-stash. Since the values are stashed column-oriented,
939:        set the roworiented flag to column oriented, and after MatSetValues()
940:        restore the original flags */
941:     r1 = baij->roworiented;
942:     r2 = a->roworiented;
943:     r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented;

945:     baij->roworiented = PETSC_FALSE;
946:     a->roworiented    = PETSC_FALSE;

948:     (((Mat_SeqBAIJ*)baij->B->data))->roworiented = PETSC_FALSE; /* b->roworiented */
949:     while (1) {
950:       MatStashScatterGetMesg_Private(&mat->bstash,&n,&row,&col,&val,&flg);
951:       if (!flg) break;

953:       for (i=0; i<n;) {
954:         /* Now identify the consecutive vals belonging to the same row */
955:         for (j=i,rstart=row[j]; j<n; j++) {
956:           if (row[j] != rstart) break;
957:         }
958:         if (j < n) ncols = j-i;
959:         else       ncols = n-i;
960:         MatSetValuesBlocked_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,mat->insertmode);
961:         i    = j;
962:       }
963:     }
964:     MatStashScatterEnd_Private(&mat->bstash);

966:     baij->roworiented = r1;
967:     a->roworiented    = r2;

969:     ((Mat_SeqBAIJ*)baij->B->data)->roworiented = r3; /* b->roworiented */
970:   }

972:   MatAssemblyBegin(baij->A,mode);
973:   MatAssemblyEnd(baij->A,mode);

975:   /* determine if any processor has disassembled, if so we must
976:      also disassemble ourselfs, in order that we may reassemble. */
977:   /*
978:      if nonzero structure of submatrix B cannot change then we know that
979:      no processor disassembled thus we can skip this stuff
980:   */
981:   if (!((Mat_SeqBAIJ*)baij->B->data)->nonew) {
982:     MPIU_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));
983:     if (mat->was_assembled && !other_disassembled) {
984:       MatDisAssemble_MPIBAIJ(mat);
985:     }
986:   }

988:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
989:     MatSetUpMultiply_MPIBAIJ(mat);
990:   }
991:   MatAssemblyBegin(baij->B,mode);
992:   MatAssemblyEnd(baij->B,mode);

994: #if defined(PETSC_USE_INFO)
995:   if (baij->ht && mode== MAT_FINAL_ASSEMBLY) {
996:     PetscInfo1(mat,"Average Hash Table Search in MatSetValues = %5.2f\n",((PetscReal)baij->ht_total_ct)/baij->ht_insert_ct);

998:     baij->ht_total_ct  = 0;
999:     baij->ht_insert_ct = 0;
1000:   }
1001: #endif
1002:   if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) {
1003:     MatCreateHashTable_MPIBAIJ_Private(mat,baij->ht_fact);

1005:     mat->ops->setvalues        = MatSetValues_MPIBAIJ_HT;
1006:     mat->ops->setvaluesblocked = MatSetValuesBlocked_MPIBAIJ_HT;
1007:   }

1009:   PetscFree2(baij->rowvalues,baij->rowindices);

1011:   baij->rowvalues = 0;

1013:   /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
1014:   if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
1015:     PetscObjectState state = baij->A->nonzerostate + baij->B->nonzerostate;
1016:     MPIU_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));
1017:   }
1018:   return(0);
1019: }

1021: extern PetscErrorCode MatView_SeqBAIJ(Mat,PetscViewer);
1022:  #include <petscdraw.h>
1025: static PetscErrorCode MatView_MPIBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
1026: {
1027:   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
1028:   PetscErrorCode    ierr;
1029:   PetscMPIInt       rank = baij->rank;
1030:   PetscInt          bs   = mat->rmap->bs;
1031:   PetscBool         iascii,isdraw;
1032:   PetscViewer       sviewer;
1033:   PetscViewerFormat format;

1036:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1037:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1038:   if (iascii) {
1039:     PetscViewerGetFormat(viewer,&format);
1040:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1041:       MatInfo info;
1042:       MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
1043:       MatGetInfo(mat,MAT_LOCAL,&info);
1044:       PetscViewerASCIIPushSynchronized(viewer);
1045:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D bs %D mem %D\n",
1046:                                                 rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,mat->rmap->bs,(PetscInt)info.memory);
1047:       MatGetInfo(baij->A,MAT_LOCAL,&info);
1048:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
1049:       MatGetInfo(baij->B,MAT_LOCAL,&info);
1050:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
1051:       PetscViewerFlush(viewer);
1052:       PetscViewerASCIIPopSynchronized(viewer);
1053:       PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");
1054:       VecScatterView(baij->Mvctx,viewer);
1055:       return(0);
1056:     } else if (format == PETSC_VIEWER_ASCII_INFO) {
1057:       PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);
1058:       return(0);
1059:     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
1060:       return(0);
1061:     }
1062:   }

1064:   if (isdraw) {
1065:     PetscDraw draw;
1066:     PetscBool isnull;
1067:     PetscViewerDrawGetDraw(viewer,0,&draw);
1068:     PetscDrawIsNull(draw,&isnull);
1069:     if (isnull) return(0);
1070:   }

1072:   {
1073:     /* assemble the entire matrix onto first processor. */
1074:     Mat         A;
1075:     Mat_SeqBAIJ *Aloc;
1076:     PetscInt    M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
1077:     MatScalar   *a;
1078:     const char  *matname;

1080:     /* Here we are creating a temporary matrix, so will assume MPIBAIJ is acceptable */
1081:     /* Perhaps this should be the type of mat? */
1082:     MatCreate(PetscObjectComm((PetscObject)mat),&A);
1083:     if (!rank) {
1084:       MatSetSizes(A,M,N,M,N);
1085:     } else {
1086:       MatSetSizes(A,0,0,M,N);
1087:     }
1088:     MatSetType(A,MATMPIBAIJ);
1089:     MatMPIBAIJSetPreallocation(A,mat->rmap->bs,0,NULL,0,NULL);
1090:     MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
1091:     PetscLogObjectParent((PetscObject)mat,(PetscObject)A);

1093:     /* copy over the A part */
1094:     Aloc = (Mat_SeqBAIJ*)baij->A->data;
1095:     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1096:     PetscMalloc1(bs,&rvals);

1098:     for (i=0; i<mbs; i++) {
1099:       rvals[0] = bs*(baij->rstartbs + i);
1100:       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1101:       for (j=ai[i]; j<ai[i+1]; j++) {
1102:         col = (baij->cstartbs+aj[j])*bs;
1103:         for (k=0; k<bs; k++) {
1104:           MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
1105:           col++; a += bs;
1106:         }
1107:       }
1108:     }
1109:     /* copy over the B part */
1110:     Aloc = (Mat_SeqBAIJ*)baij->B->data;
1111:     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1112:     for (i=0; i<mbs; i++) {
1113:       rvals[0] = bs*(baij->rstartbs + i);
1114:       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1115:       for (j=ai[i]; j<ai[i+1]; j++) {
1116:         col = baij->garray[aj[j]]*bs;
1117:         for (k=0; k<bs; k++) {
1118:           MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
1119:           col++; a += bs;
1120:         }
1121:       }
1122:     }
1123:     PetscFree(rvals);
1124:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1125:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1126:     /*
1127:        Everyone has to call to draw the matrix since the graphics waits are
1128:        synchronized across all processors that share the PetscDraw object
1129:     */
1130:     PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
1131:     PetscObjectGetName((PetscObject)mat,&matname);
1132:     if (!rank) {
1133:       PetscObjectSetName((PetscObject)((Mat_MPIBAIJ*)(A->data))->A,matname);
1134:       MatView_SeqBAIJ(((Mat_MPIBAIJ*)(A->data))->A,sviewer);
1135:     }
1136:     PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
1137:     PetscViewerFlush(viewer);
1138:     MatDestroy(&A);
1139:   }
1140:   return(0);
1141: }

1145: static PetscErrorCode MatView_MPIBAIJ_Binary(Mat mat,PetscViewer viewer)
1146: {
1147:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)mat->data;
1148:   Mat_SeqBAIJ    *A = (Mat_SeqBAIJ*)a->A->data;
1149:   Mat_SeqBAIJ    *B = (Mat_SeqBAIJ*)a->B->data;
1151:   PetscInt       i,*row_lens,*crow_lens,bs = mat->rmap->bs,j,k,bs2=a->bs2,header[4],nz,rlen;
1152:   PetscInt       *range=0,nzmax,*column_indices,cnt,col,*garray = a->garray,cstart = mat->cmap->rstart/bs,len,pcnt,l,ll;
1153:   int            fd;
1154:   PetscScalar    *column_values;
1155:   FILE           *file;
1156:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
1157:   PetscInt       message_count,flowcontrolcount;

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

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

1181:   PetscMalloc1(rlen/bs,&crow_lens);
1182:   /* compute lengths of each row  */
1183:   for (i=0; i<a->mbs; i++) {
1184:     crow_lens[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
1185:   }
1186:   /* store the row lengths to the file */
1187:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1188:   if (!rank) {
1189:     MPI_Status status;
1190:     PetscMalloc1(rlen,&row_lens);
1191:     rlen = (range[1] - range[0])/bs;
1192:     for (i=0; i<rlen; i++) {
1193:       for (j=0; j<bs; j++) {
1194:         row_lens[i*bs+j] = bs*crow_lens[i];
1195:       }
1196:     }
1197:     PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);
1198:     for (i=1; i<size; i++) {
1199:       rlen = (range[i+1] - range[i])/bs;
1200:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1201:       MPI_Recv(crow_lens,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1202:       for (k=0; k<rlen; k++) {
1203:         for (j=0; j<bs; j++) {
1204:           row_lens[k*bs+j] = bs*crow_lens[k];
1205:         }
1206:       }
1207:       PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);
1208:     }
1209:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1210:     PetscFree(row_lens);
1211:   } else {
1212:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1213:     MPI_Send(crow_lens,mat->rmap->n/bs,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1214:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1215:   }
1216:   PetscFree(crow_lens);

1218:   /* load up the local column indices. Include for all rows not just one for each block row since process 0 does not have the
1219:      information needed to make it for each row from a block row. This does require more communication but still not more than
1220:      the communication needed for the nonzero values  */
1221:   nzmax = nz; /*  space a largest processor needs */
1222:   MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));
1223:   PetscMalloc1(nzmax,&column_indices);
1224:   cnt   = 0;
1225:   for (i=0; i<a->mbs; i++) {
1226:     pcnt = cnt;
1227:     for (j=B->i[i]; j<B->i[i+1]; j++) {
1228:       if ((col = garray[B->j[j]]) > cstart) break;
1229:       for (l=0; l<bs; l++) {
1230:         column_indices[cnt++] = bs*col+l;
1231:       }
1232:     }
1233:     for (k=A->i[i]; k<A->i[i+1]; k++) {
1234:       for (l=0; l<bs; l++) {
1235:         column_indices[cnt++] = bs*(A->j[k] + cstart)+l;
1236:       }
1237:     }
1238:     for (; j<B->i[i+1]; j++) {
1239:       for (l=0; l<bs; l++) {
1240:         column_indices[cnt++] = bs*garray[B->j[j]]+l;
1241:       }
1242:     }
1243:     len = cnt - pcnt;
1244:     for (k=1; k<bs; k++) {
1245:       PetscMemcpy(&column_indices[cnt],&column_indices[pcnt],len*sizeof(PetscInt));
1246:       cnt += len;
1247:     }
1248:   }
1249:   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);

1251:   /* store the columns to the file */
1252:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1253:   if (!rank) {
1254:     MPI_Status status;
1255:     PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);
1256:     for (i=1; i<size; i++) {
1257:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1258:       MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1259:       MPI_Recv(column_indices,cnt,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1260:       PetscBinaryWrite(fd,column_indices,cnt,PETSC_INT,PETSC_TRUE);
1261:     }
1262:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1263:   } else {
1264:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1265:     MPI_Send(&cnt,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1266:     MPI_Send(column_indices,cnt,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1267:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1268:   }
1269:   PetscFree(column_indices);

1271:   /* load up the numerical values */
1272:   PetscMalloc1(nzmax,&column_values);
1273:   cnt  = 0;
1274:   for (i=0; i<a->mbs; i++) {
1275:     rlen = bs*(B->i[i+1] - B->i[i] + A->i[i+1] - A->i[i]);
1276:     for (j=B->i[i]; j<B->i[i+1]; j++) {
1277:       if (garray[B->j[j]] > cstart) break;
1278:       for (l=0; l<bs; l++) {
1279:         for (ll=0; ll<bs; ll++) {
1280:           column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1281:         }
1282:       }
1283:       cnt += bs;
1284:     }
1285:     for (k=A->i[i]; k<A->i[i+1]; k++) {
1286:       for (l=0; l<bs; l++) {
1287:         for (ll=0; ll<bs; ll++) {
1288:           column_values[cnt + l*rlen + ll] = A->a[bs2*k+l+bs*ll];
1289:         }
1290:       }
1291:       cnt += bs;
1292:     }
1293:     for (; j<B->i[i+1]; j++) {
1294:       for (l=0; l<bs; l++) {
1295:         for (ll=0; ll<bs; ll++) {
1296:           column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1297:         }
1298:       }
1299:       cnt += bs;
1300:     }
1301:     cnt += (bs-1)*rlen;
1302:   }
1303:   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);

1305:   /* store the column values to the file */
1306:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1307:   if (!rank) {
1308:     MPI_Status status;
1309:     PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);
1310:     for (i=1; i<size; i++) {
1311:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1312:       MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1313:       MPI_Recv(column_values,cnt,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat),&status);
1314:       PetscBinaryWrite(fd,column_values,cnt,PETSC_SCALAR,PETSC_TRUE);
1315:     }
1316:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1317:   } else {
1318:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1319:     MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1320:     MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));
1321:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1322:   }
1323:   PetscFree(column_values);

1325:   PetscViewerBinaryGetInfoPointer(viewer,&file);
1326:   if (file) {
1327:     fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs);
1328:   }
1329:   return(0);
1330: }

1334: PetscErrorCode MatView_MPIBAIJ(Mat mat,PetscViewer viewer)
1335: {
1337:   PetscBool      iascii,isdraw,issocket,isbinary;

1340:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1341:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1342:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
1343:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1344:   if (iascii || isdraw || issocket) {
1345:     MatView_MPIBAIJ_ASCIIorDraworSocket(mat,viewer);
1346:   } else if (isbinary) {
1347:     MatView_MPIBAIJ_Binary(mat,viewer);
1348:   }
1349:   return(0);
1350: }

1354: PetscErrorCode MatDestroy_MPIBAIJ(Mat mat)
1355: {
1356:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;

1360: #if defined(PETSC_USE_LOG)
1361:   PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N);
1362: #endif
1363:   MatStashDestroy_Private(&mat->stash);
1364:   MatStashDestroy_Private(&mat->bstash);
1365:   MatDestroy(&baij->A);
1366:   MatDestroy(&baij->B);
1367: #if defined(PETSC_USE_CTABLE)
1368:   PetscTableDestroy(&baij->colmap);
1369: #else
1370:   PetscFree(baij->colmap);
1371: #endif
1372:   PetscFree(baij->garray);
1373:   VecDestroy(&baij->lvec);
1374:   VecScatterDestroy(&baij->Mvctx);
1375:   PetscFree2(baij->rowvalues,baij->rowindices);
1376:   PetscFree(baij->barray);
1377:   PetscFree2(baij->hd,baij->ht);
1378:   PetscFree(baij->rangebs);
1379:   PetscFree(mat->data);

1381:   PetscObjectChangeTypeName((PetscObject)mat,0);
1382:   PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);
1383:   PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);
1384:   PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C",NULL);
1385:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocation_C",NULL);
1386:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocationCSR_C",NULL);
1387:   PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);
1388:   PetscObjectComposeFunction((PetscObject)mat,"MatSetHashTableFactor_C",NULL);
1389:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpisbaij_C",NULL);
1390:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpibstrm_C",NULL);
1391:   return(0);
1392: }

1396: PetscErrorCode MatMult_MPIBAIJ(Mat A,Vec xx,Vec yy)
1397: {
1398:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1400:   PetscInt       nt;

1403:   VecGetLocalSize(xx,&nt);
1404:   if (nt != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");
1405:   VecGetLocalSize(yy,&nt);
1406:   if (nt != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");
1407:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1408:   (*a->A->ops->mult)(a->A,xx,yy);
1409:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1410:   (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
1411:   return(0);
1412: }

1416: PetscErrorCode MatMultAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1417: {
1418:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

1422:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1423:   (*a->A->ops->multadd)(a->A,xx,yy,zz);
1424:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1425:   (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
1426:   return(0);
1427: }

1431: PetscErrorCode MatMultTranspose_MPIBAIJ(Mat A,Vec xx,Vec yy)
1432: {
1433:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1435:   PetscBool      merged;

1438:   VecScatterGetMerged(a->Mvctx,&merged);
1439:   /* do nondiagonal part */
1440:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1441:   if (!merged) {
1442:     /* send it on its way */
1443:     VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1444:     /* do local part */
1445:     (*a->A->ops->multtranspose)(a->A,xx,yy);
1446:     /* receive remote parts: note this assumes the values are not actually */
1447:     /* inserted in yy until the next line */
1448:     VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1449:   } else {
1450:     /* do local part */
1451:     (*a->A->ops->multtranspose)(a->A,xx,yy);
1452:     /* send it on its way */
1453:     VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1454:     /* values actually were received in the Begin() but we need to call this nop */
1455:     VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1456:   }
1457:   return(0);
1458: }

1462: PetscErrorCode MatMultTransposeAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1463: {
1464:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

1468:   /* do nondiagonal part */
1469:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1470:   /* send it on its way */
1471:   VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1472:   /* do local part */
1473:   (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);
1474:   /* receive remote parts: note this assumes the values are not actually */
1475:   /* inserted in yy until the next line, which is true for my implementation*/
1476:   /* but is not perhaps always true. */
1477:   VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1478:   return(0);
1479: }

1481: /*
1482:   This only works correctly for square matrices where the subblock A->A is the
1483:    diagonal block
1484: */
1487: PetscErrorCode MatGetDiagonal_MPIBAIJ(Mat A,Vec v)
1488: {
1489:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

1493:   if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
1494:   MatGetDiagonal(a->A,v);
1495:   return(0);
1496: }

1500: PetscErrorCode MatScale_MPIBAIJ(Mat A,PetscScalar aa)
1501: {
1502:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

1506:   MatScale(a->A,aa);
1507:   MatScale(a->B,aa);
1508:   return(0);
1509: }

1513: PetscErrorCode MatGetRow_MPIBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1514: {
1515:   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
1516:   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
1518:   PetscInt       bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB;
1519:   PetscInt       nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend;
1520:   PetscInt       *cmap,*idx_p,cstart = mat->cstartbs;

1523:   if (row < brstart || row >= brend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local rows");
1524:   if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
1525:   mat->getrowactive = PETSC_TRUE;

1527:   if (!mat->rowvalues && (idx || v)) {
1528:     /*
1529:         allocate enough space to hold information from the longest row.
1530:     */
1531:     Mat_SeqBAIJ *Aa = (Mat_SeqBAIJ*)mat->A->data,*Ba = (Mat_SeqBAIJ*)mat->B->data;
1532:     PetscInt    max = 1,mbs = mat->mbs,tmp;
1533:     for (i=0; i<mbs; i++) {
1534:       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1535:       if (max < tmp) max = tmp;
1536:     }
1537:     PetscMalloc2(max*bs2,&mat->rowvalues,max*bs2,&mat->rowindices);
1538:   }
1539:   lrow = row - brstart;

1541:   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1542:   if (!v)   {pvA = 0; pvB = 0;}
1543:   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1544:   (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1545:   (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1546:   nztot = nzA + nzB;

1548:   cmap = mat->garray;
1549:   if (v  || idx) {
1550:     if (nztot) {
1551:       /* Sort by increasing column numbers, assuming A and B already sorted */
1552:       PetscInt imark = -1;
1553:       if (v) {
1554:         *v = v_p = mat->rowvalues;
1555:         for (i=0; i<nzB; i++) {
1556:           if (cmap[cworkB[i]/bs] < cstart) v_p[i] = vworkB[i];
1557:           else break;
1558:         }
1559:         imark = i;
1560:         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1561:         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1562:       }
1563:       if (idx) {
1564:         *idx = idx_p = mat->rowindices;
1565:         if (imark > -1) {
1566:           for (i=0; i<imark; i++) {
1567:             idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1568:           }
1569:         } else {
1570:           for (i=0; i<nzB; i++) {
1571:             if (cmap[cworkB[i]/bs] < cstart) idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1572:             else break;
1573:           }
1574:           imark = i;
1575:         }
1576:         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart*bs + cworkA[i];
1577:         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1578:       }
1579:     } else {
1580:       if (idx) *idx = 0;
1581:       if (v)   *v   = 0;
1582:     }
1583:   }
1584:   *nz  = nztot;
1585:   (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1586:   (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1587:   return(0);
1588: }

1592: PetscErrorCode MatRestoreRow_MPIBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1593: {
1594:   Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;

1597:   if (!baij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
1598:   baij->getrowactive = PETSC_FALSE;
1599:   return(0);
1600: }

1604: PetscErrorCode MatZeroEntries_MPIBAIJ(Mat A)
1605: {
1606:   Mat_MPIBAIJ    *l = (Mat_MPIBAIJ*)A->data;

1610:   MatZeroEntries(l->A);
1611:   MatZeroEntries(l->B);
1612:   return(0);
1613: }

1617: PetscErrorCode MatGetInfo_MPIBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1618: {
1619:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)matin->data;
1620:   Mat            A  = a->A,B = a->B;
1622:   PetscReal      isend[5],irecv[5];

1625:   info->block_size = (PetscReal)matin->rmap->bs;

1627:   MatGetInfo(A,MAT_LOCAL,info);

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

1632:   MatGetInfo(B,MAT_LOCAL,info);

1634:   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1635:   isend[3] += info->memory;  isend[4] += info->mallocs;

1637:   if (flag == MAT_LOCAL) {
1638:     info->nz_used      = isend[0];
1639:     info->nz_allocated = isend[1];
1640:     info->nz_unneeded  = isend[2];
1641:     info->memory       = isend[3];
1642:     info->mallocs      = isend[4];
1643:   } else if (flag == MAT_GLOBAL_MAX) {
1644:     MPIU_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));

1646:     info->nz_used      = irecv[0];
1647:     info->nz_allocated = irecv[1];
1648:     info->nz_unneeded  = irecv[2];
1649:     info->memory       = irecv[3];
1650:     info->mallocs      = irecv[4];
1651:   } else if (flag == MAT_GLOBAL_SUM) {
1652:     MPIU_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));

1654:     info->nz_used      = irecv[0];
1655:     info->nz_allocated = irecv[1];
1656:     info->nz_unneeded  = irecv[2];
1657:     info->memory       = irecv[3];
1658:     info->mallocs      = irecv[4];
1659:   } else SETERRQ1(PetscObjectComm((PetscObject)matin),PETSC_ERR_ARG_WRONG,"Unknown MatInfoType argument %d",(int)flag);
1660:   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
1661:   info->fill_ratio_needed = 0;
1662:   info->factor_mallocs    = 0;
1663:   return(0);
1664: }

1668: PetscErrorCode MatSetOption_MPIBAIJ(Mat A,MatOption op,PetscBool flg)
1669: {
1670:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

1674:   switch (op) {
1675:   case MAT_NEW_NONZERO_LOCATIONS:
1676:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1677:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1678:   case MAT_KEEP_NONZERO_PATTERN:
1679:   case MAT_NEW_NONZERO_LOCATION_ERR:
1680:     MatCheckPreallocated(A,1);
1681:     MatSetOption(a->A,op,flg);
1682:     MatSetOption(a->B,op,flg);
1683:     break;
1684:   case MAT_ROW_ORIENTED:
1685:     MatCheckPreallocated(A,1);
1686:     a->roworiented = flg;

1688:     MatSetOption(a->A,op,flg);
1689:     MatSetOption(a->B,op,flg);
1690:     break;
1691:   case MAT_NEW_DIAGONALS:
1692:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1693:     break;
1694:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1695:     a->donotstash = flg;
1696:     break;
1697:   case MAT_USE_HASH_TABLE:
1698:     a->ht_flag = flg;
1699:     break;
1700:   case MAT_SYMMETRIC:
1701:   case MAT_STRUCTURALLY_SYMMETRIC:
1702:   case MAT_HERMITIAN:
1703:   case MAT_SYMMETRY_ETERNAL:
1704:     MatCheckPreallocated(A,1);
1705:     MatSetOption(a->A,op,flg);
1706:     break;
1707:   default:
1708:     SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"unknown option %d",op);
1709:   }
1710:   return(0);
1711: }

1715: PetscErrorCode MatTranspose_MPIBAIJ(Mat A,MatReuse reuse,Mat *matout)
1716: {
1717:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)A->data;
1718:   Mat_SeqBAIJ    *Aloc;
1719:   Mat            B;
1721:   PetscInt       M =A->rmap->N,N=A->cmap->N,*ai,*aj,i,*rvals,j,k,col;
1722:   PetscInt       bs=A->rmap->bs,mbs=baij->mbs;
1723:   MatScalar      *a;

1726:   if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1727:   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
1728:     MatCreate(PetscObjectComm((PetscObject)A),&B);
1729:     MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);
1730:     MatSetType(B,((PetscObject)A)->type_name);
1731:     /* Do not know preallocation information, but must set block size */
1732:     MatMPIBAIJSetPreallocation(B,A->rmap->bs,PETSC_DECIDE,NULL,PETSC_DECIDE,NULL);
1733:   } else {
1734:     B = *matout;
1735:   }

1737:   /* copy over the A part */
1738:   Aloc = (Mat_SeqBAIJ*)baij->A->data;
1739:   ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1740:   PetscMalloc1(bs,&rvals);

1742:   for (i=0; i<mbs; i++) {
1743:     rvals[0] = bs*(baij->rstartbs + i);
1744:     for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1745:     for (j=ai[i]; j<ai[i+1]; j++) {
1746:       col = (baij->cstartbs+aj[j])*bs;
1747:       for (k=0; k<bs; k++) {
1748:         MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);

1750:         col++; a += bs;
1751:       }
1752:     }
1753:   }
1754:   /* copy over the B part */
1755:   Aloc = (Mat_SeqBAIJ*)baij->B->data;
1756:   ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1757:   for (i=0; i<mbs; i++) {
1758:     rvals[0] = bs*(baij->rstartbs + i);
1759:     for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1760:     for (j=ai[i]; j<ai[i+1]; j++) {
1761:       col = baij->garray[aj[j]]*bs;
1762:       for (k=0; k<bs; k++) {
1763:         MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);
1764:         col++;
1765:         a += bs;
1766:       }
1767:     }
1768:   }
1769:   PetscFree(rvals);
1770:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
1771:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

1773:   if (reuse == MAT_INITIAL_MATRIX || *matout != A) *matout = B;
1774:   else {
1775:     MatHeaderMerge(A,&B);
1776:   }
1777:   return(0);
1778: }

1782: PetscErrorCode MatDiagonalScale_MPIBAIJ(Mat mat,Vec ll,Vec rr)
1783: {
1784:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
1785:   Mat            a     = baij->A,b = baij->B;
1787:   PetscInt       s1,s2,s3;

1790:   MatGetLocalSize(mat,&s2,&s3);
1791:   if (rr) {
1792:     VecGetLocalSize(rr,&s1);
1793:     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
1794:     /* Overlap communication with computation. */
1795:     VecScatterBegin(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);
1796:   }
1797:   if (ll) {
1798:     VecGetLocalSize(ll,&s1);
1799:     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1800:     (*b->ops->diagonalscale)(b,ll,NULL);
1801:   }
1802:   /* scale  the diagonal block */
1803:   (*a->ops->diagonalscale)(a,ll,rr);

1805:   if (rr) {
1806:     /* Do a scatter end and then right scale the off-diagonal block */
1807:     VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);
1808:     (*b->ops->diagonalscale)(b,NULL,baij->lvec);
1809:   }
1810:   return(0);
1811: }

1815: PetscErrorCode MatZeroRows_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1816: {
1817:   Mat_MPIBAIJ   *l      = (Mat_MPIBAIJ *) A->data;
1818:   PetscInt      *lrows;
1819:   PetscInt       r, len;

1823:   /* get locally owned rows */
1824:   MatZeroRowsMapLocal_Private(A,N,rows,&len,&lrows);
1825:   /* fix right hand side if needed */
1826:   if (x && b) {
1827:     const PetscScalar *xx;
1828:     PetscScalar       *bb;

1830:     VecGetArrayRead(x,&xx);
1831:     VecGetArray(b,&bb);
1832:     for (r = 0; r < len; ++r) bb[lrows[r]] = diag*xx[lrows[r]];
1833:     VecRestoreArrayRead(x,&xx);
1834:     VecRestoreArray(b,&bb);
1835:   }

1837:   /* actually zap the local rows */
1838:   /*
1839:         Zero the required rows. If the "diagonal block" of the matrix
1840:      is square and the user wishes to set the diagonal we use separate
1841:      code so that MatSetValues() is not called for each diagonal allocating
1842:      new memory, thus calling lots of mallocs and slowing things down.

1844:   */
1845:   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
1846:   MatZeroRows_SeqBAIJ(l->B,len,lrows,0.0,NULL,NULL);
1847:   if (A->congruentlayouts == -1) { /* first time we compare rows and cols layouts */
1848:     PetscBool cong;
1849:     PetscLayoutCompare(A->rmap,A->cmap,&cong);
1850:     if (cong) A->congruentlayouts = 1;
1851:     else      A->congruentlayouts = 0;
1852:   }
1853:   if ((diag != 0.0) && A->congruentlayouts) {
1854:     MatZeroRows_SeqBAIJ(l->A,len,lrows,diag,NULL,NULL);
1855:   } else if (diag != 0.0) {
1856:     MatZeroRows_SeqBAIJ(l->A,len,lrows,0.0,0,0);
1857:     if (((Mat_SeqBAIJ*)l->A->data)->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options \n\
1858:        MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
1859:     for (r = 0; r < len; ++r) {
1860:       const PetscInt row = lrows[r] + A->rmap->rstart;
1861:       MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);
1862:     }
1863:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1864:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1865:   } else {
1866:     MatZeroRows_SeqBAIJ(l->A,len,lrows,0.0,NULL,NULL);
1867:   }
1868:   PetscFree(lrows);

1870:   /* only change matrix nonzero state if pattern was allowed to be changed */
1871:   if (!((Mat_SeqBAIJ*)(l->A->data))->keepnonzeropattern) {
1872:     PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
1873:     MPIU_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
1874:   }
1875:   return(0);
1876: }

1880: PetscErrorCode MatZeroRowsColumns_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1881: {
1882:   Mat_MPIBAIJ       *l = (Mat_MPIBAIJ*)A->data;
1883:   PetscErrorCode    ierr;
1884:   PetscMPIInt       n = A->rmap->n;
1885:   PetscInt          i,j,k,r,p = 0,len = 0,row,col,count;
1886:   PetscInt          *lrows,*owners = A->rmap->range;
1887:   PetscSFNode       *rrows;
1888:   PetscSF           sf;
1889:   const PetscScalar *xx;
1890:   PetscScalar       *bb,*mask;
1891:   Vec               xmask,lmask;
1892:   Mat_SeqBAIJ       *baij = (Mat_SeqBAIJ*)l->B->data;
1893:   PetscInt           bs = A->rmap->bs, bs2 = baij->bs2;
1894:   PetscScalar       *aa;

1897:   /* Create SF where leaves are input rows and roots are owned rows */
1898:   PetscMalloc1(n, &lrows);
1899:   for (r = 0; r < n; ++r) lrows[r] = -1;
1900:   PetscMalloc1(N, &rrows);
1901:   for (r = 0; r < N; ++r) {
1902:     const PetscInt idx   = rows[r];
1903:     if (idx < 0 || A->rmap->N <= idx) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range [0,%D)",idx,A->rmap->N);
1904:     if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
1905:       PetscLayoutFindOwner(A->rmap,idx,&p);
1906:     }
1907:     rrows[r].rank  = p;
1908:     rrows[r].index = rows[r] - owners[p];
1909:   }
1910:   PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);
1911:   PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);
1912:   /* Collect flags for rows to be zeroed */
1913:   PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1914:   PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1915:   PetscSFDestroy(&sf);
1916:   /* Compress and put in row numbers */
1917:   for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
1918:   /* zero diagonal part of matrix */
1919:   MatZeroRowsColumns(l->A,len,lrows,diag,x,b);
1920:   /* handle off diagonal part of matrix */
1921:   MatCreateVecs(A,&xmask,NULL);
1922:   VecDuplicate(l->lvec,&lmask);
1923:   VecGetArray(xmask,&bb);
1924:   for (i=0; i<len; i++) bb[lrows[i]] = 1;
1925:   VecRestoreArray(xmask,&bb);
1926:   VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
1927:   VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
1928:   VecDestroy(&xmask);
1929:   if (x) {
1930:     VecScatterBegin(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
1931:     VecScatterEnd(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
1932:     VecGetArrayRead(l->lvec,&xx);
1933:     VecGetArray(b,&bb);
1934:   }
1935:   VecGetArray(lmask,&mask);
1936:   /* remove zeroed rows of off diagonal matrix */
1937:   for (i = 0; i < len; ++i) {
1938:     row   = lrows[i];
1939:     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
1940:     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
1941:     for (k = 0; k < count; ++k) {
1942:       aa[0] = 0.0;
1943:       aa   += bs;
1944:     }
1945:   }
1946:   /* loop over all elements of off process part of matrix zeroing removed columns*/
1947:   for (i = 0; i < l->B->rmap->N; ++i) {
1948:     row = i/bs;
1949:     for (j = baij->i[row]; j < baij->i[row+1]; ++j) {
1950:       for (k = 0; k < bs; ++k) {
1951:         col = bs*baij->j[j] + k;
1952:         if (PetscAbsScalar(mask[col])) {
1953:           aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
1954:           if (x) bb[i] -= aa[0]*xx[col];
1955:           aa[0] = 0.0;
1956:         }
1957:       }
1958:     }
1959:   }
1960:   if (x) {
1961:     VecRestoreArray(b,&bb);
1962:     VecRestoreArrayRead(l->lvec,&xx);
1963:   }
1964:   VecRestoreArray(lmask,&mask);
1965:   VecDestroy(&lmask);
1966:   PetscFree(lrows);

1968:   /* only change matrix nonzero state if pattern was allowed to be changed */
1969:   if (!((Mat_SeqBAIJ*)(l->A->data))->keepnonzeropattern) {
1970:     PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
1971:     MPIU_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
1972:   }
1973:   return(0);
1974: }

1978: PetscErrorCode MatSetUnfactored_MPIBAIJ(Mat A)
1979: {
1980:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

1984:   MatSetUnfactored(a->A);
1985:   return(0);
1986: }

1988: static PetscErrorCode MatDuplicate_MPIBAIJ(Mat,MatDuplicateOption,Mat*);

1992: PetscErrorCode MatEqual_MPIBAIJ(Mat A,Mat B,PetscBool  *flag)
1993: {
1994:   Mat_MPIBAIJ    *matB = (Mat_MPIBAIJ*)B->data,*matA = (Mat_MPIBAIJ*)A->data;
1995:   Mat            a,b,c,d;
1996:   PetscBool      flg;

2000:   a = matA->A; b = matA->B;
2001:   c = matB->A; d = matB->B;

2003:   MatEqual(a,c,&flg);
2004:   if (flg) {
2005:     MatEqual(b,d,&flg);
2006:   }
2007:   MPIU_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
2008:   return(0);
2009: }

2013: PetscErrorCode MatCopy_MPIBAIJ(Mat A,Mat B,MatStructure str)
2014: {
2016:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
2017:   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ*)B->data;

2020:   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
2021:   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
2022:     MatCopy_Basic(A,B,str);
2023:   } else {
2024:     MatCopy(a->A,b->A,str);
2025:     MatCopy(a->B,b->B,str);
2026:   }
2027:   return(0);
2028: }

2032: PetscErrorCode MatSetUp_MPIBAIJ(Mat A)
2033: {

2037:   MatMPIBAIJSetPreallocation(A,A->rmap->bs,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
2038:   return(0);
2039: }

2043: PetscErrorCode MatAXPYGetPreallocation_MPIBAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz)
2044: {
2046:   PetscInt       bs = Y->rmap->bs,m = Y->rmap->N/bs;
2047:   Mat_SeqBAIJ    *x = (Mat_SeqBAIJ*)X->data;
2048:   Mat_SeqBAIJ    *y = (Mat_SeqBAIJ*)Y->data;

2051:   MatAXPYGetPreallocation_MPIX_private(m,x->i,x->j,xltog,y->i,y->j,yltog,nnz);
2052:   return(0);
2053: }

2057: PetscErrorCode MatAXPY_MPIBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2058: {
2060:   Mat_MPIBAIJ    *xx=(Mat_MPIBAIJ*)X->data,*yy=(Mat_MPIBAIJ*)Y->data;
2061:   PetscBLASInt   bnz,one=1;
2062:   Mat_SeqBAIJ    *x,*y;

2065:   if (str == SAME_NONZERO_PATTERN) {
2066:     PetscScalar alpha = a;
2067:     x    = (Mat_SeqBAIJ*)xx->A->data;
2068:     y    = (Mat_SeqBAIJ*)yy->A->data;
2069:     PetscBLASIntCast(x->nz,&bnz);
2070:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2071:     x    = (Mat_SeqBAIJ*)xx->B->data;
2072:     y    = (Mat_SeqBAIJ*)yy->B->data;
2073:     PetscBLASIntCast(x->nz,&bnz);
2074:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2075:     PetscObjectStateIncrease((PetscObject)Y);
2076:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2077:     MatAXPY_Basic(Y,a,X,str);
2078:   } else {
2079:     Mat      B;
2080:     PetscInt *nnz_d,*nnz_o,bs=Y->rmap->bs;
2081:     PetscMalloc1(yy->A->rmap->N,&nnz_d);
2082:     PetscMalloc1(yy->B->rmap->N,&nnz_o);
2083:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
2084:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2085:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2086:     MatSetBlockSizesFromMats(B,Y,Y);
2087:     MatSetType(B,MATMPIBAIJ);
2088:     MatAXPYGetPreallocation_SeqBAIJ(yy->A,xx->A,nnz_d);
2089:     MatAXPYGetPreallocation_MPIBAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);
2090:     MatMPIBAIJSetPreallocation(B,bs,0,nnz_d,0,nnz_o);
2091:     /* MatAXPY_BasicWithPreallocation() for BAIJ matrix is much slower than AIJ, even for bs=1 ! */
2092:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2093:     MatHeaderReplace(Y,&B);
2094:     PetscFree(nnz_d);
2095:     PetscFree(nnz_o);
2096:   }
2097:   return(0);
2098: }

2102: PetscErrorCode MatRealPart_MPIBAIJ(Mat A)
2103: {
2104:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

2108:   MatRealPart(a->A);
2109:   MatRealPart(a->B);
2110:   return(0);
2111: }

2115: PetscErrorCode MatImaginaryPart_MPIBAIJ(Mat A)
2116: {
2117:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

2121:   MatImaginaryPart(a->A);
2122:   MatImaginaryPart(a->B);
2123:   return(0);
2124: }

2128: PetscErrorCode MatGetSubMatrix_MPIBAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
2129: {
2131:   IS             iscol_local;
2132:   PetscInt       csize;

2135:   ISGetLocalSize(iscol,&csize);
2136:   if (call == MAT_REUSE_MATRIX) {
2137:     PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);
2138:     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2139:   } else {
2140:     ISAllGather(iscol,&iscol_local);
2141:   }
2142:   MatGetSubMatrix_MPIBAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);
2143:   if (call == MAT_INITIAL_MATRIX) {
2144:     PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);
2145:     ISDestroy(&iscol_local);
2146:   }
2147:   return(0);
2148: }
2149: extern PetscErrorCode MatGetSubMatrices_MPIBAIJ_local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,PetscBool*,Mat*);
2152: /*
2153:   Not great since it makes two copies of the submatrix, first an SeqBAIJ
2154:   in local and then by concatenating the local matrices the end result.
2155:   Writing it directly would be much like MatGetSubMatrices_MPIBAIJ().
2156:   This routine is used for BAIJ and SBAIJ matrices (unfortunate dependency).
2157: */
2158: PetscErrorCode MatGetSubMatrix_MPIBAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
2159: {
2161:   PetscMPIInt    rank,size;
2162:   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs;
2163:   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol,nrow;
2164:   Mat            M,Mreuse;
2165:   MatScalar      *vwork,*aa;
2166:   MPI_Comm       comm;
2167:   IS             isrow_new, iscol_new;
2168:   PetscBool      idflag,allrows, allcols;
2169:   Mat_SeqBAIJ    *aij;

2172:   PetscObjectGetComm((PetscObject)mat,&comm);
2173:   MPI_Comm_rank(comm,&rank);
2174:   MPI_Comm_size(comm,&size);
2175:   /* The compression and expansion should be avoided. Doesn't point
2176:      out errors, might change the indices, hence buggey */
2177:   ISCompressIndicesGeneral(mat->rmap->N,mat->rmap->n,mat->rmap->bs,1,&isrow,&isrow_new);
2178:   ISCompressIndicesGeneral(mat->cmap->N,mat->cmap->n,mat->cmap->bs,1,&iscol,&iscol_new);

2180:   /* Check for special case: each processor gets entire matrix columns */
2181:   ISIdentity(iscol,&idflag);
2182:   ISGetLocalSize(iscol,&ncol);
2183:   if (idflag && ncol == mat->cmap->N) allcols = PETSC_TRUE;
2184:   else allcols = PETSC_FALSE;

2186:   ISIdentity(isrow,&idflag);
2187:   ISGetLocalSize(isrow,&nrow);
2188:   if (idflag && nrow == mat->rmap->N) allrows = PETSC_TRUE;
2189:   else allrows = PETSC_FALSE;

2191:   if (call ==  MAT_REUSE_MATRIX) {
2192:     PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);
2193:     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2194:     MatGetSubMatrices_MPIBAIJ_local(mat,1,&isrow_new,&iscol_new,MAT_REUSE_MATRIX,&allrows,&allcols,&Mreuse);
2195:   } else {
2196:     MatGetSubMatrices_MPIBAIJ_local(mat,1,&isrow_new,&iscol_new,MAT_INITIAL_MATRIX,&allrows,&allcols,&Mreuse);
2197:   }
2198:   ISDestroy(&isrow_new);
2199:   ISDestroy(&iscol_new);
2200:   /*
2201:       m - number of local rows
2202:       n - number of columns (same on all processors)
2203:       rstart - first row in new global matrix generated
2204:   */
2205:   MatGetBlockSize(mat,&bs);
2206:   MatGetSize(Mreuse,&m,&n);
2207:   m    = m/bs;
2208:   n    = n/bs;

2210:   if (call == MAT_INITIAL_MATRIX) {
2211:     aij = (Mat_SeqBAIJ*)(Mreuse)->data;
2212:     ii  = aij->i;
2213:     jj  = aij->j;

2215:     /*
2216:         Determine the number of non-zeros in the diagonal and off-diagonal
2217:         portions of the matrix in order to do correct preallocation
2218:     */

2220:     /* first get start and end of "diagonal" columns */
2221:     if (csize == PETSC_DECIDE) {
2222:       ISGetSize(isrow,&mglobal);
2223:       if (mglobal == n*bs) { /* square matrix */
2224:         nlocal = m;
2225:       } else {
2226:         nlocal = n/size + ((n % size) > rank);
2227:       }
2228:     } else {
2229:       nlocal = csize/bs;
2230:     }
2231:     MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
2232:     rstart = rend - nlocal;
2233:     if (rank == size - 1 && rend != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);

2235:     /* next, compute all the lengths */
2236:     PetscMalloc2(m+1,&dlens,m+1,&olens);
2237:     for (i=0; i<m; i++) {
2238:       jend = ii[i+1] - ii[i];
2239:       olen = 0;
2240:       dlen = 0;
2241:       for (j=0; j<jend; j++) {
2242:         if (*jj < rstart || *jj >= rend) olen++;
2243:         else dlen++;
2244:         jj++;
2245:       }
2246:       olens[i] = olen;
2247:       dlens[i] = dlen;
2248:     }
2249:     MatCreate(comm,&M);
2250:     MatSetSizes(M,bs*m,bs*nlocal,PETSC_DECIDE,bs*n);
2251:     MatSetType(M,((PetscObject)mat)->type_name);
2252:     MatMPIBAIJSetPreallocation(M,bs,0,dlens,0,olens);
2253:     MatMPISBAIJSetPreallocation(M,bs,0,dlens,0,olens);
2254:     PetscFree2(dlens,olens);
2255:   } else {
2256:     PetscInt ml,nl;

2258:     M    = *newmat;
2259:     MatGetLocalSize(M,&ml,&nl);
2260:     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2261:     MatZeroEntries(M);
2262:     /*
2263:          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2264:        rather than the slower MatSetValues().
2265:     */
2266:     M->was_assembled = PETSC_TRUE;
2267:     M->assembled     = PETSC_FALSE;
2268:   }
2269:   MatSetOption(M,MAT_ROW_ORIENTED,PETSC_FALSE);
2270:   MatGetOwnershipRange(M,&rstart,&rend);
2271:   aij  = (Mat_SeqBAIJ*)(Mreuse)->data;
2272:   ii   = aij->i;
2273:   jj   = aij->j;
2274:   aa   = aij->a;
2275:   for (i=0; i<m; i++) {
2276:     row   = rstart/bs + i;
2277:     nz    = ii[i+1] - ii[i];
2278:     cwork = jj;     jj += nz;
2279:     vwork = aa;     aa += nz*bs*bs;
2280:     MatSetValuesBlocked_MPIBAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);
2281:   }

2283:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
2284:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
2285:   *newmat = M;

2287:   /* save submatrix used in processor for next request */
2288:   if (call ==  MAT_INITIAL_MATRIX) {
2289:     PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);
2290:     PetscObjectDereference((PetscObject)Mreuse);
2291:   }
2292:   return(0);
2293: }

2297: PetscErrorCode MatPermute_MPIBAIJ(Mat A,IS rowp,IS colp,Mat *B)
2298: {
2299:   MPI_Comm       comm,pcomm;
2300:   PetscInt       clocal_size,nrows;
2301:   const PetscInt *rows;
2302:   PetscMPIInt    size;
2303:   IS             crowp,lcolp;

2307:   PetscObjectGetComm((PetscObject)A,&comm);
2308:   /* make a collective version of 'rowp' */
2309:   PetscObjectGetComm((PetscObject)rowp,&pcomm);
2310:   if (pcomm==comm) {
2311:     crowp = rowp;
2312:   } else {
2313:     ISGetSize(rowp,&nrows);
2314:     ISGetIndices(rowp,&rows);
2315:     ISCreateGeneral(comm,nrows,rows,PETSC_COPY_VALUES,&crowp);
2316:     ISRestoreIndices(rowp,&rows);
2317:   }
2318:   ISSetPermutation(crowp);
2319:   /* make a local version of 'colp' */
2320:   PetscObjectGetComm((PetscObject)colp,&pcomm);
2321:   MPI_Comm_size(pcomm,&size);
2322:   if (size==1) {
2323:     lcolp = colp;
2324:   } else {
2325:     ISAllGather(colp,&lcolp);
2326:   }
2327:   ISSetPermutation(lcolp);
2328:   /* now we just get the submatrix */
2329:   MatGetLocalSize(A,NULL,&clocal_size);
2330:   MatGetSubMatrix_MPIBAIJ_Private(A,crowp,lcolp,clocal_size,MAT_INITIAL_MATRIX,B);
2331:   /* clean up */
2332:   if (pcomm!=comm) {
2333:     ISDestroy(&crowp);
2334:   }
2335:   if (size>1) {
2336:     ISDestroy(&lcolp);
2337:   }
2338:   return(0);
2339: }

2343: PetscErrorCode  MatGetGhosts_MPIBAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
2344: {
2345:   Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*) mat->data;
2346:   Mat_SeqBAIJ *B    = (Mat_SeqBAIJ*)baij->B->data;

2349:   if (nghosts) *nghosts = B->nbs;
2350:   if (ghosts) *ghosts = baij->garray;
2351:   return(0);
2352: }

2356: PetscErrorCode MatGetSeqNonzeroStructure_MPIBAIJ(Mat A,Mat *newmat)
2357: {
2358:   Mat            B;
2359:   Mat_MPIBAIJ    *a  = (Mat_MPIBAIJ*)A->data;
2360:   Mat_SeqBAIJ    *ad = (Mat_SeqBAIJ*)a->A->data,*bd = (Mat_SeqBAIJ*)a->B->data;
2361:   Mat_SeqAIJ     *b;
2363:   PetscMPIInt    size,rank,*recvcounts = 0,*displs = 0;
2364:   PetscInt       sendcount,i,*rstarts = A->rmap->range,n,cnt,j,bs = A->rmap->bs;
2365:   PetscInt       m,*garray = a->garray,*lens,*jsendbuf,*a_jsendbuf,*b_jsendbuf;

2368:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
2369:   MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);

2371:   /* ----------------------------------------------------------------
2372:      Tell every processor the number of nonzeros per row
2373:   */
2374:   PetscMalloc1(A->rmap->N/bs,&lens);
2375:   for (i=A->rmap->rstart/bs; i<A->rmap->rend/bs; i++) {
2376:     lens[i] = ad->i[i-A->rmap->rstart/bs+1] - ad->i[i-A->rmap->rstart/bs] + bd->i[i-A->rmap->rstart/bs+1] - bd->i[i-A->rmap->rstart/bs];
2377:   }
2378:   PetscMalloc1(2*size,&recvcounts);
2379:   displs    = recvcounts + size;
2380:   for (i=0; i<size; i++) {
2381:     recvcounts[i] = A->rmap->range[i+1]/bs - A->rmap->range[i]/bs;
2382:     displs[i]     = A->rmap->range[i]/bs;
2383:   }
2384: #if defined(PETSC_HAVE_MPI_IN_PLACE)
2385:   MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,lens,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2386: #else
2387:   sendcount = A->rmap->rend/bs - A->rmap->rstart/bs;
2388:   MPI_Allgatherv(lens+A->rmap->rstart/bs,sendcount,MPIU_INT,lens,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2389: #endif
2390:   /* ---------------------------------------------------------------
2391:      Create the sequential matrix of the same type as the local block diagonal
2392:   */
2393:   MatCreate(PETSC_COMM_SELF,&B);
2394:   MatSetSizes(B,A->rmap->N/bs,A->cmap->N/bs,PETSC_DETERMINE,PETSC_DETERMINE);
2395:   MatSetType(B,MATSEQAIJ);
2396:   MatSeqAIJSetPreallocation(B,0,lens);
2397:   b    = (Mat_SeqAIJ*)B->data;

2399:   /*--------------------------------------------------------------------
2400:     Copy my part of matrix column indices over
2401:   */
2402:   sendcount  = ad->nz + bd->nz;
2403:   jsendbuf   = b->j + b->i[rstarts[rank]/bs];
2404:   a_jsendbuf = ad->j;
2405:   b_jsendbuf = bd->j;
2406:   n          = A->rmap->rend/bs - A->rmap->rstart/bs;
2407:   cnt        = 0;
2408:   for (i=0; i<n; i++) {

2410:     /* put in lower diagonal portion */
2411:     m = bd->i[i+1] - bd->i[i];
2412:     while (m > 0) {
2413:       /* is it above diagonal (in bd (compressed) numbering) */
2414:       if (garray[*b_jsendbuf] > A->rmap->rstart/bs + i) break;
2415:       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2416:       m--;
2417:     }

2419:     /* put in diagonal portion */
2420:     for (j=ad->i[i]; j<ad->i[i+1]; j++) {
2421:       jsendbuf[cnt++] = A->rmap->rstart/bs + *a_jsendbuf++;
2422:     }

2424:     /* put in upper diagonal portion */
2425:     while (m-- > 0) {
2426:       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2427:     }
2428:   }
2429:   if (cnt != sendcount) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupted PETSc matrix: nz given %D actual nz %D",sendcount,cnt);

2431:   /*--------------------------------------------------------------------
2432:     Gather all column indices to all processors
2433:   */
2434:   for (i=0; i<size; i++) {
2435:     recvcounts[i] = 0;
2436:     for (j=A->rmap->range[i]/bs; j<A->rmap->range[i+1]/bs; j++) {
2437:       recvcounts[i] += lens[j];
2438:     }
2439:   }
2440:   displs[0] = 0;
2441:   for (i=1; i<size; i++) {
2442:     displs[i] = displs[i-1] + recvcounts[i-1];
2443:   }
2444: #if defined(PETSC_HAVE_MPI_IN_PLACE)
2445:   MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,b->j,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2446: #else
2447:   MPI_Allgatherv(jsendbuf,sendcount,MPIU_INT,b->j,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2448: #endif
2449:   /*--------------------------------------------------------------------
2450:     Assemble the matrix into useable form (note numerical values not yet set)
2451:   */
2452:   /* set the b->ilen (length of each row) values */
2453:   PetscMemcpy(b->ilen,lens,(A->rmap->N/bs)*sizeof(PetscInt));
2454:   /* set the b->i indices */
2455:   b->i[0] = 0;
2456:   for (i=1; i<=A->rmap->N/bs; i++) {
2457:     b->i[i] = b->i[i-1] + lens[i-1];
2458:   }
2459:   PetscFree(lens);
2460:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2461:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2462:   PetscFree(recvcounts);

2464:   if (A->symmetric) {
2465:     MatSetOption(B,MAT_SYMMETRIC,PETSC_TRUE);
2466:   } else if (A->hermitian) {
2467:     MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE);
2468:   } else if (A->structurally_symmetric) {
2469:     MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);
2470:   }
2471:   *newmat = B;
2472:   return(0);
2473: }

2477: PetscErrorCode MatSOR_MPIBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2478: {
2479:   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
2481:   Vec            bb1 = 0;

2484:   if (flag == SOR_APPLY_UPPER) {
2485:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2486:     return(0);
2487:   }

2489:   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS) {
2490:     VecDuplicate(bb,&bb1);
2491:   }

2493:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2494:     if (flag & SOR_ZERO_INITIAL_GUESS) {
2495:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2496:       its--;
2497:     }

2499:     while (its--) {
2500:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2501:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

2503:       /* update rhs: bb1 = bb - B*x */
2504:       VecScale(mat->lvec,-1.0);
2505:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

2507:       /* local sweep */
2508:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);
2509:     }
2510:   } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
2511:     if (flag & SOR_ZERO_INITIAL_GUESS) {
2512:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2513:       its--;
2514:     }
2515:     while (its--) {
2516:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2517:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

2519:       /* update rhs: bb1 = bb - B*x */
2520:       VecScale(mat->lvec,-1.0);
2521:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

2523:       /* local sweep */
2524:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);
2525:     }
2526:   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
2527:     if (flag & SOR_ZERO_INITIAL_GUESS) {
2528:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2529:       its--;
2530:     }
2531:     while (its--) {
2532:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2533:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

2535:       /* update rhs: bb1 = bb - B*x */
2536:       VecScale(mat->lvec,-1.0);
2537:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

2539:       /* local sweep */
2540:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);
2541:     }
2542:   } else SETERRQ(PetscObjectComm((PetscObject)matin),PETSC_ERR_SUP,"Parallel version of SOR requested not supported");

2544:   VecDestroy(&bb1);
2545:   return(0);
2546: }

2550: PetscErrorCode MatGetColumnNorms_MPIBAIJ(Mat A,NormType type,PetscReal *norms)
2551: {
2553:   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ*)A->data;
2554:   PetscInt       N,i,*garray = aij->garray;
2555:   PetscInt       ib,jb,bs = A->rmap->bs;
2556:   Mat_SeqBAIJ    *a_aij = (Mat_SeqBAIJ*) aij->A->data;
2557:   MatScalar      *a_val = a_aij->a;
2558:   Mat_SeqBAIJ    *b_aij = (Mat_SeqBAIJ*) aij->B->data;
2559:   MatScalar      *b_val = b_aij->a;
2560:   PetscReal      *work;

2563:   MatGetSize(A,NULL,&N);
2564:   PetscCalloc1(N,&work);
2565:   if (type == NORM_2) {
2566:     for (i=a_aij->i[0]; i<a_aij->i[aij->A->rmap->n/bs]; i++) {
2567:       for (jb=0; jb<bs; jb++) {
2568:         for (ib=0; ib<bs; ib++) {
2569:           work[A->cmap->rstart + a_aij->j[i] * bs + jb] += PetscAbsScalar(*a_val * *a_val);
2570:           a_val++;
2571:         }
2572:       }
2573:     }
2574:     for (i=b_aij->i[0]; i<b_aij->i[aij->B->rmap->n/bs]; i++) {
2575:       for (jb=0; jb<bs; jb++) {
2576:         for (ib=0; ib<bs; ib++) {
2577:           work[garray[b_aij->j[i]] * bs + jb] += PetscAbsScalar(*b_val * *b_val);
2578:           b_val++;
2579:         }
2580:       }
2581:     }
2582:   } else if (type == NORM_1) {
2583:     for (i=a_aij->i[0]; i<a_aij->i[aij->A->rmap->n/bs]; i++) {
2584:       for (jb=0; jb<bs; jb++) {
2585:         for (ib=0; ib<bs; ib++) {
2586:           work[A->cmap->rstart + a_aij->j[i] * bs + jb] += PetscAbsScalar(*a_val);
2587:           a_val++;
2588:         }
2589:       }
2590:     }
2591:     for (i=b_aij->i[0]; i<b_aij->i[aij->B->rmap->n/bs]; i++) {
2592:       for (jb=0; jb<bs; jb++) {
2593:        for (ib=0; ib<bs; ib++) {
2594:           work[garray[b_aij->j[i]] * bs + jb] += PetscAbsScalar(*b_val);
2595:           b_val++;
2596:         }
2597:       }
2598:     }
2599:   } else if (type == NORM_INFINITY) {
2600:     for (i=a_aij->i[0]; i<a_aij->i[aij->A->rmap->n/bs]; i++) {
2601:       for (jb=0; jb<bs; jb++) {
2602:         for (ib=0; ib<bs; ib++) {
2603:           int col = A->cmap->rstart + a_aij->j[i] * bs + jb;
2604:           work[col] = PetscMax(PetscAbsScalar(*a_val), work[col]);
2605:           a_val++;
2606:         }
2607:       }
2608:     }
2609:     for (i=b_aij->i[0]; i<b_aij->i[aij->B->rmap->n/bs]; i++) {
2610:       for (jb=0; jb<bs; jb++) {
2611:         for (ib=0; ib<bs; ib++) {
2612:           int col = garray[b_aij->j[i]] * bs + jb;
2613:           work[col] = PetscMax(PetscAbsScalar(*b_val), work[col]);
2614:           b_val++;
2615:         }
2616:       }
2617:     }
2618:   } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown NormType");
2619:   if (type == NORM_INFINITY) {
2620:     MPIU_Allreduce(work,norms,N,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));
2621:   } else {
2622:     MPIU_Allreduce(work,norms,N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));
2623:   }
2624:   PetscFree(work);
2625:   if (type == NORM_2) {
2626:     for (i=0; i<N; i++) norms[i] = PetscSqrtReal(norms[i]);
2627:   }
2628:   return(0);
2629: }

2633: PetscErrorCode MatInvertBlockDiagonal_MPIBAIJ(Mat A,const PetscScalar **values)
2634: {
2635:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*) A->data;

2639:   MatInvertBlockDiagonal(a->A,values);
2640:   A->errortype = a->A->errortype;
2641:   return(0);
2642: }

2646: PetscErrorCode MatShift_MPIBAIJ(Mat Y,PetscScalar a)
2647: {
2649:   Mat_MPIBAIJ    *maij = (Mat_MPIBAIJ*)Y->data;
2650:   Mat_SeqBAIJ    *aij = (Mat_SeqBAIJ*)maij->A->data;

2653:   if (!Y->preallocated) {
2654:     MatMPIBAIJSetPreallocation(Y,Y->rmap->bs,1,NULL,0,NULL);
2655:   } else if (!aij->nz) {
2656:     PetscInt nonew = aij->nonew;
2657:     MatSeqBAIJSetPreallocation(maij->A,Y->rmap->bs,1,NULL);
2658:     aij->nonew = nonew;
2659:   }
2660:   MatShift_Basic(Y,a);
2661:   return(0);
2662: }

2666: PetscErrorCode MatMissingDiagonal_MPIBAIJ(Mat A,PetscBool  *missing,PetscInt *d)
2667: {
2668:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

2672:   if (A->rmap->n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only works for square matrices");
2673:   MatMissingDiagonal(a->A,missing,d);
2674:   if (d) {
2675:     PetscInt rstart;
2676:     MatGetOwnershipRange(A,&rstart,NULL);
2677:     *d += rstart/A->rmap->bs;

2679:   }
2680:   return(0);
2681: }

2683: /* -------------------------------------------------------------------*/
2684: static struct _MatOps MatOps_Values = {MatSetValues_MPIBAIJ,
2685:                                        MatGetRow_MPIBAIJ,
2686:                                        MatRestoreRow_MPIBAIJ,
2687:                                        MatMult_MPIBAIJ,
2688:                                 /* 4*/ MatMultAdd_MPIBAIJ,
2689:                                        MatMultTranspose_MPIBAIJ,
2690:                                        MatMultTransposeAdd_MPIBAIJ,
2691:                                        0,
2692:                                        0,
2693:                                        0,
2694:                                 /*10*/ 0,
2695:                                        0,
2696:                                        0,
2697:                                        MatSOR_MPIBAIJ,
2698:                                        MatTranspose_MPIBAIJ,
2699:                                 /*15*/ MatGetInfo_MPIBAIJ,
2700:                                        MatEqual_MPIBAIJ,
2701:                                        MatGetDiagonal_MPIBAIJ,
2702:                                        MatDiagonalScale_MPIBAIJ,
2703:                                        MatNorm_MPIBAIJ,
2704:                                 /*20*/ MatAssemblyBegin_MPIBAIJ,
2705:                                        MatAssemblyEnd_MPIBAIJ,
2706:                                        MatSetOption_MPIBAIJ,
2707:                                        MatZeroEntries_MPIBAIJ,
2708:                                 /*24*/ MatZeroRows_MPIBAIJ,
2709:                                        0,
2710:                                        0,
2711:                                        0,
2712:                                        0,
2713:                                 /*29*/ MatSetUp_MPIBAIJ,
2714:                                        0,
2715:                                        0,
2716:                                        0,
2717:                                        0,
2718:                                 /*34*/ MatDuplicate_MPIBAIJ,
2719:                                        0,
2720:                                        0,
2721:                                        0,
2722:                                        0,
2723:                                 /*39*/ MatAXPY_MPIBAIJ,
2724:                                        MatGetSubMatrices_MPIBAIJ,
2725:                                        MatIncreaseOverlap_MPIBAIJ,
2726:                                        MatGetValues_MPIBAIJ,
2727:                                        MatCopy_MPIBAIJ,
2728:                                 /*44*/ 0,
2729:                                        MatScale_MPIBAIJ,
2730:                                        MatShift_MPIBAIJ,
2731:                                        0,
2732:                                        MatZeroRowsColumns_MPIBAIJ,
2733:                                 /*49*/ 0,
2734:                                        0,
2735:                                        0,
2736:                                        0,
2737:                                        0,
2738:                                 /*54*/ MatFDColoringCreate_MPIXAIJ,
2739:                                        0,
2740:                                        MatSetUnfactored_MPIBAIJ,
2741:                                        MatPermute_MPIBAIJ,
2742:                                        MatSetValuesBlocked_MPIBAIJ,
2743:                                 /*59*/ MatGetSubMatrix_MPIBAIJ,
2744:                                        MatDestroy_MPIBAIJ,
2745:                                        MatView_MPIBAIJ,
2746:                                        0,
2747:                                        0,
2748:                                 /*64*/ 0,
2749:                                        0,
2750:                                        0,
2751:                                        0,
2752:                                        0,
2753:                                 /*69*/ MatGetRowMaxAbs_MPIBAIJ,
2754:                                        0,
2755:                                        0,
2756:                                        0,
2757:                                        0,
2758:                                 /*74*/ 0,
2759:                                        MatFDColoringApply_BAIJ,
2760:                                        0,
2761:                                        0,
2762:                                        0,
2763:                                 /*79*/ 0,
2764:                                        0,
2765:                                        0,
2766:                                        0,
2767:                                        MatLoad_MPIBAIJ,
2768:                                 /*84*/ 0,
2769:                                        0,
2770:                                        0,
2771:                                        0,
2772:                                        0,
2773:                                 /*89*/ 0,
2774:                                        0,
2775:                                        0,
2776:                                        0,
2777:                                        0,
2778:                                 /*94*/ 0,
2779:                                        0,
2780:                                        0,
2781:                                        0,
2782:                                        0,
2783:                                 /*99*/ 0,
2784:                                        0,
2785:                                        0,
2786:                                        0,
2787:                                        0,
2788:                                 /*104*/0,
2789:                                        MatRealPart_MPIBAIJ,
2790:                                        MatImaginaryPart_MPIBAIJ,
2791:                                        0,
2792:                                        0,
2793:                                 /*109*/0,
2794:                                        0,
2795:                                        0,
2796:                                        0,
2797:                                        MatMissingDiagonal_MPIBAIJ,
2798:                                 /*114*/MatGetSeqNonzeroStructure_MPIBAIJ,
2799:                                        0,
2800:                                        MatGetGhosts_MPIBAIJ,
2801:                                        0,
2802:                                        0,
2803:                                 /*119*/0,
2804:                                        0,
2805:                                        0,
2806:                                        0,
2807:                                        MatGetMultiProcBlock_MPIBAIJ,
2808:                                 /*124*/0,
2809:                                        MatGetColumnNorms_MPIBAIJ,
2810:                                        MatInvertBlockDiagonal_MPIBAIJ,
2811:                                        0,
2812:                                        0,
2813:                                /*129*/ 0,
2814:                                        0,
2815:                                        0,
2816:                                        0,
2817:                                        0,
2818:                                /*134*/ 0,
2819:                                        0,
2820:                                        0,
2821:                                        0,
2822:                                        0,
2823:                                /*139*/ 0,
2824:                                        0,
2825:                                        0,
2826:                                        MatFDColoringSetUp_MPIXAIJ,
2827:                                        0,
2828:                                 /*144*/MatCreateMPIMatConcatenateSeqMat_MPIBAIJ
2829: };

2833: PetscErrorCode  MatGetDiagonalBlock_MPIBAIJ(Mat A,Mat *a)
2834: {
2836:   *a = ((Mat_MPIBAIJ*)A->data)->A;
2837:   return(0);
2838: }

2840: PETSC_INTERN PetscErrorCode MatConvert_MPIBAIJ_MPISBAIJ(Mat, MatType,MatReuse,Mat*);

2844: PetscErrorCode MatMPIBAIJSetPreallocationCSR_MPIBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
2845: {
2846:   PetscInt       m,rstart,cstart,cend;
2847:   PetscInt       i,j,d,nz,nz_max=0,*d_nnz=0,*o_nnz=0;
2848:   const PetscInt *JJ    =0;
2849:   PetscScalar    *values=0;
2850:   PetscBool      roworiented = ((Mat_MPIBAIJ*)B->data)->roworiented;

2854:   PetscLayoutSetBlockSize(B->rmap,bs);
2855:   PetscLayoutSetBlockSize(B->cmap,bs);
2856:   PetscLayoutSetUp(B->rmap);
2857:   PetscLayoutSetUp(B->cmap);
2858:   PetscLayoutGetBlockSize(B->rmap,&bs);
2859:   m      = B->rmap->n/bs;
2860:   rstart = B->rmap->rstart/bs;
2861:   cstart = B->cmap->rstart/bs;
2862:   cend   = B->cmap->rend/bs;

2864:   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
2865:   PetscMalloc2(m,&d_nnz,m,&o_nnz);
2866:   for (i=0; i<m; i++) {
2867:     nz = ii[i+1] - ii[i];
2868:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz);
2869:     nz_max = PetscMax(nz_max,nz);
2870:     JJ     = jj + ii[i];
2871:     for (j=0; j<nz; j++) {
2872:       if (*JJ >= cstart) break;
2873:       JJ++;
2874:     }
2875:     d = 0;
2876:     for (; j<nz; j++) {
2877:       if (*JJ++ >= cend) break;
2878:       d++;
2879:     }
2880:     d_nnz[i] = d;
2881:     o_nnz[i] = nz - d;
2882:   }
2883:   MatMPIBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);
2884:   PetscFree2(d_nnz,o_nnz);

2886:   values = (PetscScalar*)V;
2887:   if (!values) {
2888:     PetscMalloc1(bs*bs*nz_max,&values);
2889:     PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));
2890:   }
2891:   for (i=0; i<m; i++) {
2892:     PetscInt          row    = i + rstart;
2893:     PetscInt          ncols  = ii[i+1] - ii[i];
2894:     const PetscInt    *icols = jj + ii[i];
2895:     if (!roworiented) {         /* block ordering matches the non-nested layout of MatSetValues so we can insert entire rows */
2896:       const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2897:       MatSetValuesBlocked_MPIBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);
2898:     } else {                    /* block ordering does not match so we can only insert one block at a time. */
2899:       PetscInt j;
2900:       for (j=0; j<ncols; j++) {
2901:         const PetscScalar *svals = values + (V ? (bs*bs*(ii[i]+j)) : 0);
2902:         MatSetValuesBlocked_MPIBAIJ(B,1,&row,1,&icols[j],svals,INSERT_VALUES);
2903:       }
2904:     }
2905:   }

2907:   if (!V) { PetscFree(values); }
2908:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2909:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2910:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
2911:   return(0);
2912: }

2916: /*@C
2917:    MatMPIBAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in BAIJ format
2918:    (the default parallel PETSc format).

2920:    Collective on MPI_Comm

2922:    Input Parameters:
2923: +  B - the matrix
2924: .  bs - the block size
2925: .  i - the indices into j for the start of each local row (starts with zero)
2926: .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2927: -  v - optional values in the matrix

2929:    Level: developer

2931:    Notes: The order of the entries in values is specified by the MatOption MAT_ROW_ORIENTED.  For example, C programs
2932:    may want to use the default MAT_ROW_ORIENTED=PETSC_TRUE and use an array v[nnz][bs][bs] where the second index is
2933:    over rows within a block and the last index is over columns within a block row.  Fortran programs will likely set
2934:    MAT_ROW_ORIENTED=PETSC_FALSE and use a Fortran array v(bs,bs,nnz) in which the first index is over rows within a
2935:    block column and the second index is over columns within a block.

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

2939: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ, MatCreateMPIBAIJWithArrays(), MPIBAIJ
2940: @*/
2941: PetscErrorCode  MatMPIBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2942: {

2949:   PetscTryMethod(B,"MatMPIBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));
2950:   return(0);
2951: }

2955: PetscErrorCode  MatMPIBAIJSetPreallocation_MPIBAIJ(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt *d_nnz,PetscInt o_nz,const PetscInt *o_nnz)
2956: {
2957:   Mat_MPIBAIJ    *b;
2959:   PetscInt       i;

2962:   MatSetBlockSize(B,PetscAbs(bs));
2963:   PetscLayoutSetUp(B->rmap);
2964:   PetscLayoutSetUp(B->cmap);
2965:   PetscLayoutGetBlockSize(B->rmap,&bs);

2967:   if (d_nnz) {
2968:     for (i=0; i<B->rmap->n/bs; i++) {
2969:       if (d_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than -1: local row %D value %D",i,d_nnz[i]);
2970:     }
2971:   }
2972:   if (o_nnz) {
2973:     for (i=0; i<B->rmap->n/bs; i++) {
2974:       if (o_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than -1: local row %D value %D",i,o_nnz[i]);
2975:     }
2976:   }

2978:   b      = (Mat_MPIBAIJ*)B->data;
2979:   b->bs2 = bs*bs;
2980:   b->mbs = B->rmap->n/bs;
2981:   b->nbs = B->cmap->n/bs;
2982:   b->Mbs = B->rmap->N/bs;
2983:   b->Nbs = B->cmap->N/bs;

2985:   for (i=0; i<=b->size; i++) {
2986:     b->rangebs[i] = B->rmap->range[i]/bs;
2987:   }
2988:   b->rstartbs = B->rmap->rstart/bs;
2989:   b->rendbs   = B->rmap->rend/bs;
2990:   b->cstartbs = B->cmap->rstart/bs;
2991:   b->cendbs   = B->cmap->rend/bs;

2993:   if (!B->preallocated) {
2994:     MatCreate(PETSC_COMM_SELF,&b->A);
2995:     MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
2996:     MatSetType(b->A,MATSEQBAIJ);
2997:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
2998:     MatCreate(PETSC_COMM_SELF,&b->B);
2999:     MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);
3000:     MatSetType(b->B,MATSEQBAIJ);
3001:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);
3002:     MatStashCreate_Private(PetscObjectComm((PetscObject)B),bs,&B->bstash);
3003:   }

3005:   MatSeqBAIJSetPreallocation(b->A,bs,d_nz,d_nnz);
3006:   MatSeqBAIJSetPreallocation(b->B,bs,o_nz,o_nnz);
3007:   B->preallocated = PETSC_TRUE;
3008:   return(0);
3009: }

3011: extern PetscErrorCode  MatDiagonalScaleLocal_MPIBAIJ(Mat,Vec);
3012: extern PetscErrorCode  MatSetHashTableFactor_MPIBAIJ(Mat,PetscReal);

3016: PETSC_INTERN PetscErrorCode MatConvert_MPIBAIJ_MPIAdj(Mat B, MatType newtype,MatReuse reuse,Mat *adj)
3017: {
3018:   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ*)B->data;
3020:   Mat_SeqBAIJ    *d  = (Mat_SeqBAIJ*) b->A->data,*o = (Mat_SeqBAIJ*) b->B->data;
3021:   PetscInt       M   = B->rmap->n/B->rmap->bs,i,*ii,*jj,cnt,j,k,rstart = B->rmap->rstart/B->rmap->bs;
3022:   const PetscInt *id = d->i, *jd = d->j, *io = o->i, *jo = o->j, *garray = b->garray;

3025:   PetscMalloc1(M+1,&ii);
3026:   ii[0] = 0;
3027:   for (i=0; i<M; i++) {
3028:     if ((id[i+1] - id[i]) < 0) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Indices wrong %D %D %D",i,id[i],id[i+1]);
3029:     if ((io[i+1] - io[i]) < 0) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Indices wrong %D %D %D",i,io[i],io[i+1]);
3030:     ii[i+1] = ii[i] + id[i+1] - id[i] + io[i+1] - io[i];
3031:     /* remove one from count of matrix has diagonal */
3032:     for (j=id[i]; j<id[i+1]; j++) {
3033:       if (jd[j] == i) {ii[i+1]--;break;}
3034:     }
3035:   }
3036:   PetscMalloc1(ii[M],&jj);
3037:   cnt  = 0;
3038:   for (i=0; i<M; i++) {
3039:     for (j=io[i]; j<io[i+1]; j++) {
3040:       if (garray[jo[j]] > rstart) break;
3041:       jj[cnt++] = garray[jo[j]];
3042:     }
3043:     for (k=id[i]; k<id[i+1]; k++) {
3044:       if (jd[k] != i) {
3045:         jj[cnt++] = rstart + jd[k];
3046:       }
3047:     }
3048:     for (; j<io[i+1]; j++) {
3049:       jj[cnt++] = garray[jo[j]];
3050:     }
3051:   }
3052:   MatCreateMPIAdj(PetscObjectComm((PetscObject)B),M,B->cmap->N/B->rmap->bs,ii,jj,NULL,adj);
3053:   return(0);
3054: }

3056:  #include <../src/mat/impls/aij/mpi/mpiaij.h>

3058: PETSC_INTERN PetscErrorCode MatConvert_SeqBAIJ_SeqAIJ(Mat,MatType,MatReuse,Mat*);

3062: PETSC_INTERN PetscErrorCode MatConvert_MPIBAIJ_MPIAIJ(Mat A,MatType newtype,MatReuse reuse,Mat *newmat)
3063: {
3065:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
3066:   Mat            B;
3067:   Mat_MPIAIJ     *b;

3070:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix must be assembled");

3072:   MatCreate(PetscObjectComm((PetscObject)A),&B);
3073:   MatSetType(B,MATMPIAIJ);
3074:   MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
3075:   MatSetBlockSizes(B,A->rmap->bs,A->cmap->bs);
3076:   MatSeqAIJSetPreallocation(B,0,NULL);
3077:   MatMPIAIJSetPreallocation(B,0,NULL,0,NULL);
3078:   b    = (Mat_MPIAIJ*) B->data;

3080:   MatDestroy(&b->A);
3081:   MatDestroy(&b->B);
3082:   MatDisAssemble_MPIBAIJ(A);
3083:   MatConvert_SeqBAIJ_SeqAIJ(a->A, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->A);
3084:   MatConvert_SeqBAIJ_SeqAIJ(a->B, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->B);
3085:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3086:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
3087:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
3088:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
3089:   if (reuse == MAT_INPLACE_MATRIX) {
3090:     MatHeaderReplace(A,&B);
3091:   } else {
3092:    *newmat = B;
3093:   }
3094:   return(0);
3095: }

3097: /*MC
3098:    MATMPIBAIJ - MATMPIBAIJ = "mpibaij" - A matrix type to be used for distributed block sparse matrices.

3100:    Options Database Keys:
3101: + -mat_type mpibaij - sets the matrix type to "mpibaij" during a call to MatSetFromOptions()
3102: . -mat_block_size <bs> - set the blocksize used to store the matrix
3103: - -mat_use_hash_table <fact>

3105:   Level: beginner

3107: .seealso: MatCreateMPIBAIJ
3108: M*/

3110: PETSC_INTERN PetscErrorCode MatConvert_MPIBAIJ_MPIBSTRM(Mat,MatType,MatReuse,Mat*);

3114: PETSC_EXTERN PetscErrorCode MatCreate_MPIBAIJ(Mat B)
3115: {
3116:   Mat_MPIBAIJ    *b;
3118:   PetscBool      flg = PETSC_FALSE;

3121:   PetscNewLog(B,&b);
3122:   B->data = (void*)b;

3124:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
3125:   B->assembled = PETSC_FALSE;

3127:   B->insertmode = NOT_SET_VALUES;
3128:   MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);
3129:   MPI_Comm_size(PetscObjectComm((PetscObject)B),&b->size);

3131:   /* build local table of row and column ownerships */
3132:   PetscMalloc1(b->size+1,&b->rangebs);

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

3137:   b->donotstash  = PETSC_FALSE;
3138:   b->colmap      = NULL;
3139:   b->garray      = NULL;
3140:   b->roworiented = PETSC_TRUE;

3142:   /* stuff used in block assembly */
3143:   b->barray = 0;

3145:   /* stuff used for matrix vector multiply */
3146:   b->lvec  = 0;
3147:   b->Mvctx = 0;

3149:   /* stuff for MatGetRow() */
3150:   b->rowindices   = 0;
3151:   b->rowvalues    = 0;
3152:   b->getrowactive = PETSC_FALSE;

3154:   /* hash table stuff */
3155:   b->ht           = 0;
3156:   b->hd           = 0;
3157:   b->ht_size      = 0;
3158:   b->ht_flag      = PETSC_FALSE;
3159:   b->ht_fact      = 0;
3160:   b->ht_total_ct  = 0;
3161:   b->ht_insert_ct = 0;

3163:   /* stuff for MatGetSubMatrices_MPIBAIJ_local() */
3164:   b->ijonly = PETSC_FALSE;


3167:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpiadj_C",MatConvert_MPIBAIJ_MPIAdj);
3168:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpiaij_C",MatConvert_MPIBAIJ_MPIAIJ);
3169:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpisbaij_C",MatConvert_MPIBAIJ_MPISBAIJ);
3170:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIBAIJ);
3171:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIBAIJ);
3172:   PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",MatGetDiagonalBlock_MPIBAIJ);
3173:   PetscObjectComposeFunction((PetscObject)B,"MatMPIBAIJSetPreallocation_C",MatMPIBAIJSetPreallocation_MPIBAIJ);
3174:   PetscObjectComposeFunction((PetscObject)B,"MatMPIBAIJSetPreallocationCSR_C",MatMPIBAIJSetPreallocationCSR_MPIBAIJ);
3175:   PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIBAIJ);
3176:   PetscObjectComposeFunction((PetscObject)B,"MatSetHashTableFactor_C",MatSetHashTableFactor_MPIBAIJ);
3177:   PetscObjectChangeTypeName((PetscObject)B,MATMPIBAIJ);

3179:   PetscOptionsBegin(PetscObjectComm((PetscObject)B),NULL,"Options for loading MPIBAIJ matrix 1","Mat");
3180:   PetscOptionsBool("-mat_use_hash_table","Use hash table to save memory in constructing matrix","MatSetOption",flg,&flg,NULL);
3181:   if (flg) {
3182:     PetscReal fact = 1.39;
3183:     MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);
3184:     PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,NULL);
3185:     if (fact <= 1.0) fact = 1.39;
3186:     MatMPIBAIJSetHashTableFactor(B,fact);
3187:     PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);
3188:   }
3189:   PetscOptionsEnd();
3190:   return(0);
3191: }

3193: /*MC
3194:    MATBAIJ - MATBAIJ = "baij" - A matrix type to be used for block sparse matrices.

3196:    This matrix type is identical to MATSEQBAIJ when constructed with a single process communicator,
3197:    and MATMPIBAIJ otherwise.

3199:    Options Database Keys:
3200: . -mat_type baij - sets the matrix type to "baij" during a call to MatSetFromOptions()

3202:   Level: beginner

3204: .seealso: MatCreateBAIJ(),MATSEQBAIJ,MATMPIBAIJ, MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
3205: M*/

3209: /*@C
3210:    MatMPIBAIJSetPreallocation - Allocates memory for a sparse parallel matrix in block AIJ format
3211:    (block compressed row).  For good matrix assembly performance
3212:    the user should preallocate the matrix storage by setting the parameters
3213:    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
3214:    performance can be increased by more than a factor of 50.

3216:    Collective on Mat

3218:    Input Parameters:
3219: +  B - the matrix
3220: .  bs   - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
3221:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
3222: .  d_nz  - number of block nonzeros per block row in diagonal portion of local
3223:            submatrix  (same for all local rows)
3224: .  d_nnz - array containing the number of block nonzeros in the various block rows
3225:            of the in diagonal portion of the local (possibly different for each block
3226:            row) or NULL.  If you plan to factor the matrix you must leave room for the diagonal entry and
3227:            set it even if it is zero.
3228: .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
3229:            submatrix (same for all local rows).
3230: -  o_nnz - array containing the number of nonzeros in the various block rows of the
3231:            off-diagonal portion of the local submatrix (possibly different for
3232:            each block row) or NULL.

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

3236:    Options Database Keys:
3237: +   -mat_block_size - size of the blocks to use
3238: -   -mat_use_hash_table <fact>

3240:    Notes:
3241:    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
3242:    than it must be used on all processors that share the object for that argument.

3244:    Storage Information:
3245:    For a square global matrix we define each processor's diagonal portion
3246:    to be its local rows and the corresponding columns (a square submatrix);
3247:    each processor's off-diagonal portion encompasses the remainder of the
3248:    local matrix (a rectangular submatrix).

3250:    The user can specify preallocated storage for the diagonal part of
3251:    the local submatrix with either d_nz or d_nnz (not both).  Set
3252:    d_nz=PETSC_DEFAULT and d_nnz=NULL for PETSc to control dynamic
3253:    memory allocation.  Likewise, specify preallocated storage for the
3254:    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).

3256:    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3257:    the figure below we depict these three local rows and all columns (0-11).

3259: .vb
3260:            0 1 2 3 4 5 6 7 8 9 10 11
3261:           --------------------------
3262:    row 3  |o o o d d d o o o o  o  o
3263:    row 4  |o o o d d d o o o o  o  o
3264:    row 5  |o o o d d d o o o o  o  o
3265:           --------------------------
3266: .ve

3268:    Thus, any entries in the d locations are stored in the d (diagonal)
3269:    submatrix, and any entries in the o locations are stored in the
3270:    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
3271:    stored simply in the MATSEQBAIJ format for compressed row storage.

3273:    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3274:    and o_nz should indicate the number of block nonzeros per row in the o matrix.
3275:    In general, for PDE problems in which most nonzeros are near the diagonal,
3276:    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
3277:    or you will get TERRIBLE performance; see the users' manual chapter on
3278:    matrices.

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

3285:    Level: intermediate

3287: .keywords: matrix, block, aij, compressed row, sparse, parallel

3289: .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateBAIJ(), MatMPIBAIJSetPreallocationCSR(), PetscSplitOwnership()
3290: @*/
3291: PetscErrorCode  MatMPIBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3292: {

3299:   PetscTryMethod(B,"MatMPIBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,bs,d_nz,d_nnz,o_nz,o_nnz));
3300:   return(0);
3301: }

3305: /*@C
3306:    MatCreateBAIJ - Creates a sparse parallel matrix in block AIJ format
3307:    (block compressed row).  For good matrix assembly performance
3308:    the user should preallocate the matrix storage by setting the parameters
3309:    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
3310:    performance can be increased by more than a factor of 50.

3312:    Collective on MPI_Comm

3314:    Input Parameters:
3315: +  comm - MPI communicator
3316: .  bs   - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
3317:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
3318: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
3319:            This value should be the same as the local size used in creating the
3320:            y vector for the matrix-vector product y = Ax.
3321: .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
3322:            This value should be the same as the local size used in creating the
3323:            x vector for the matrix-vector product y = Ax.
3324: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3325: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
3326: .  d_nz  - number of nonzero blocks per block row in diagonal portion of local
3327:            submatrix  (same for all local rows)
3328: .  d_nnz - array containing the number of nonzero blocks in the various block rows
3329:            of the in diagonal portion of the local (possibly different for each block
3330:            row) or NULL.  If you plan to factor the matrix you must leave room for the diagonal entry
3331:            and set it even if it is zero.
3332: .  o_nz  - number of nonzero blocks per block row in the off-diagonal portion of local
3333:            submatrix (same for all local rows).
3334: -  o_nnz - array containing the number of nonzero blocks in the various block rows of the
3335:            off-diagonal portion of the local submatrix (possibly different for
3336:            each block row) or NULL.

3338:    Output Parameter:
3339: .  A - the matrix

3341:    Options Database Keys:
3342: +   -mat_block_size - size of the blocks to use
3343: -   -mat_use_hash_table <fact>

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

3349:    Notes:
3350:    If the *_nnz parameter is given then the *_nz parameter is ignored

3352:    A nonzero block is any block that as 1 or more nonzeros in it

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

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

3360:    Storage Information:
3361:    For a square global matrix we define each processor's diagonal portion
3362:    to be its local rows and the corresponding columns (a square submatrix);
3363:    each processor's off-diagonal portion encompasses the remainder of the
3364:    local matrix (a rectangular submatrix).

3366:    The user can specify preallocated storage for the diagonal part of
3367:    the local submatrix with either d_nz or d_nnz (not both).  Set
3368:    d_nz=PETSC_DEFAULT and d_nnz=NULL for PETSc to control dynamic
3369:    memory allocation.  Likewise, specify preallocated storage for the
3370:    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).

3372:    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3373:    the figure below we depict these three local rows and all columns (0-11).

3375: .vb
3376:            0 1 2 3 4 5 6 7 8 9 10 11
3377:           --------------------------
3378:    row 3  |o o o d d d o o o o  o  o
3379:    row 4  |o o o d d d o o o o  o  o
3380:    row 5  |o o o d d d o o o o  o  o
3381:           --------------------------
3382: .ve

3384:    Thus, any entries in the d locations are stored in the d (diagonal)
3385:    submatrix, and any entries in the o locations are stored in the
3386:    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
3387:    stored simply in the MATSEQBAIJ format for compressed row storage.

3389:    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3390:    and o_nz should indicate the number of block nonzeros per row in the o matrix.
3391:    In general, for PDE problems in which most nonzeros are near the diagonal,
3392:    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
3393:    or you will get TERRIBLE performance; see the users' manual chapter on
3394:    matrices.

3396:    Level: intermediate

3398: .keywords: matrix, block, aij, compressed row, sparse, parallel

3400: .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateBAIJ(), MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
3401: @*/
3402: PetscErrorCode  MatCreateBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A)
3403: {
3405:   PetscMPIInt    size;

3408:   MatCreate(comm,A);
3409:   MatSetSizes(*A,m,n,M,N);
3410:   MPI_Comm_size(comm,&size);
3411:   if (size > 1) {
3412:     MatSetType(*A,MATMPIBAIJ);
3413:     MatMPIBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);
3414:   } else {
3415:     MatSetType(*A,MATSEQBAIJ);
3416:     MatSeqBAIJSetPreallocation(*A,bs,d_nz,d_nnz);
3417:   }
3418:   return(0);
3419: }

3423: static PetscErrorCode MatDuplicate_MPIBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
3424: {
3425:   Mat            mat;
3426:   Mat_MPIBAIJ    *a,*oldmat = (Mat_MPIBAIJ*)matin->data;
3428:   PetscInt       len=0;

3431:   *newmat = 0;
3432:   MatCreate(PetscObjectComm((PetscObject)matin),&mat);
3433:   MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);
3434:   MatSetType(mat,((PetscObject)matin)->type_name);
3435:   PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));

3437:   mat->factortype   = matin->factortype;
3438:   mat->preallocated = PETSC_TRUE;
3439:   mat->assembled    = PETSC_TRUE;
3440:   mat->insertmode   = NOT_SET_VALUES;

3442:   a             = (Mat_MPIBAIJ*)mat->data;
3443:   mat->rmap->bs = matin->rmap->bs;
3444:   a->bs2        = oldmat->bs2;
3445:   a->mbs        = oldmat->mbs;
3446:   a->nbs        = oldmat->nbs;
3447:   a->Mbs        = oldmat->Mbs;
3448:   a->Nbs        = oldmat->Nbs;

3450:   PetscLayoutReference(matin->rmap,&mat->rmap);
3451:   PetscLayoutReference(matin->cmap,&mat->cmap);

3453:   a->size         = oldmat->size;
3454:   a->rank         = oldmat->rank;
3455:   a->donotstash   = oldmat->donotstash;
3456:   a->roworiented  = oldmat->roworiented;
3457:   a->rowindices   = 0;
3458:   a->rowvalues    = 0;
3459:   a->getrowactive = PETSC_FALSE;
3460:   a->barray       = 0;
3461:   a->rstartbs     = oldmat->rstartbs;
3462:   a->rendbs       = oldmat->rendbs;
3463:   a->cstartbs     = oldmat->cstartbs;
3464:   a->cendbs       = oldmat->cendbs;

3466:   /* hash table stuff */
3467:   a->ht           = 0;
3468:   a->hd           = 0;
3469:   a->ht_size      = 0;
3470:   a->ht_flag      = oldmat->ht_flag;
3471:   a->ht_fact      = oldmat->ht_fact;
3472:   a->ht_total_ct  = 0;
3473:   a->ht_insert_ct = 0;

3475:   PetscMemcpy(a->rangebs,oldmat->rangebs,(a->size+1)*sizeof(PetscInt));
3476:   if (oldmat->colmap) {
3477: #if defined(PETSC_USE_CTABLE)
3478:     PetscTableCreateCopy(oldmat->colmap,&a->colmap);
3479: #else
3480:     PetscMalloc1(a->Nbs,&a->colmap);
3481:     PetscLogObjectMemory((PetscObject)mat,(a->Nbs)*sizeof(PetscInt));
3482:     PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(PetscInt));
3483: #endif
3484:   } else a->colmap = 0;

3486:   if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
3487:     PetscMalloc1(len,&a->garray);
3488:     PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));
3489:     PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));
3490:   } else a->garray = 0;

3492:   MatStashCreate_Private(PetscObjectComm((PetscObject)matin),matin->rmap->bs,&mat->bstash);
3493:   VecDuplicate(oldmat->lvec,&a->lvec);
3494:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);
3495:   VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
3496:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);

3498:   MatDuplicate(oldmat->A,cpvalues,&a->A);
3499:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
3500:   MatDuplicate(oldmat->B,cpvalues,&a->B);
3501:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);
3502:   PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);
3503:   *newmat = mat;
3504:   return(0);
3505: }

3509: PetscErrorCode MatLoad_MPIBAIJ(Mat newmat,PetscViewer viewer)
3510: {
3512:   int            fd;
3513:   PetscInt       i,nz,j,rstart,rend;
3514:   PetscScalar    *vals,*buf;
3515:   MPI_Comm       comm;
3516:   MPI_Status     status;
3517:   PetscMPIInt    rank,size,maxnz;
3518:   PetscInt       header[4],*rowlengths = 0,M,N,m,*rowners,*cols;
3519:   PetscInt       *locrowlens = NULL,*procsnz = NULL,*browners = NULL;
3520:   PetscInt       jj,*mycols,*ibuf,bs = newmat->rmap->bs,Mbs,mbs,extra_rows,mmax;
3521:   PetscMPIInt    tag    = ((PetscObject)viewer)->tag;
3522:   PetscInt       *dlens = NULL,*odlens = NULL,*mask = NULL,*masked1 = NULL,*masked2 = NULL,rowcount,odcount;
3523:   PetscInt       dcount,kmax,k,nzcount,tmp,mend;

3526:   /* force binary viewer to load .info file if it has not yet done so */
3527:   PetscViewerSetUp(viewer);
3528:   PetscObjectGetComm((PetscObject)viewer,&comm);
3529:   PetscOptionsBegin(comm,NULL,"Options for loading MPIBAIJ matrix 2","Mat");
3530:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
3531:   PetscOptionsEnd();
3532:   if (bs < 0) bs = 1;

3534:   MPI_Comm_size(comm,&size);
3535:   MPI_Comm_rank(comm,&rank);
3536:   PetscViewerBinaryGetDescriptor(viewer,&fd);
3537:   if (!rank) {
3538:     PetscBinaryRead(fd,(char*)header,4,PETSC_INT);
3539:     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
3540:     if (header[3] < 0) SETERRQ(PetscObjectComm((PetscObject)newmat),PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk, cannot load as MPIAIJ");
3541:   }
3542:   MPI_Bcast(header+1,3,MPIU_INT,0,comm);
3543:   M    = header[1]; N = header[2];

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

3549:   if (M != N) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Can only do square matrices");

3551:   /*
3552:      This code adds extra rows to make sure the number of rows is
3553:      divisible by the blocksize
3554:   */
3555:   Mbs        = M/bs;
3556:   extra_rows = bs - M + bs*Mbs;
3557:   if (extra_rows == bs) extra_rows = 0;
3558:   else                  Mbs++;
3559:   if (extra_rows && !rank) {
3560:     PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");
3561:   }

3563:   /* determine ownership of all rows */
3564:   if (newmat->rmap->n < 0) { /* PETSC_DECIDE */
3565:     mbs = Mbs/size + ((Mbs % size) > rank);
3566:     m   = mbs*bs;
3567:   } else { /* User set */
3568:     m   = newmat->rmap->n;
3569:     mbs = m/bs;
3570:   }
3571:   PetscMalloc2(size+1,&rowners,size+1,&browners);
3572:   MPI_Allgather(&mbs,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);

3574:   /* process 0 needs enough room for process with most rows */
3575:   if (!rank) {
3576:     mmax = rowners[1];
3577:     for (i=2; i<=size; i++) {
3578:       mmax = PetscMax(mmax,rowners[i]);
3579:     }
3580:     mmax*=bs;
3581:   } else mmax = -1;             /* unused, but compiler warns anyway */

3583:   rowners[0] = 0;
3584:   for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
3585:   for (i=0; i<=size; i++) browners[i] = rowners[i]*bs;
3586:   rstart = rowners[rank];
3587:   rend   = rowners[rank+1];

3589:   /* distribute row lengths to all processors */
3590:   PetscMalloc1(m,&locrowlens);
3591:   if (!rank) {
3592:     mend = m;
3593:     if (size == 1) mend = mend - extra_rows;
3594:     PetscBinaryRead(fd,locrowlens,mend,PETSC_INT);
3595:     for (j=mend; j<m; j++) locrowlens[j] = 1;
3596:     PetscMalloc1(mmax,&rowlengths);
3597:     PetscCalloc1(size,&procsnz);
3598:     for (j=0; j<m; j++) {
3599:       procsnz[0] += locrowlens[j];
3600:     }
3601:     for (i=1; i<size; i++) {
3602:       mend = browners[i+1] - browners[i];
3603:       if (i == size-1) mend = mend - extra_rows;
3604:       PetscBinaryRead(fd,rowlengths,mend,PETSC_INT);
3605:       for (j=mend; j<browners[i+1] - browners[i]; j++) rowlengths[j] = 1;
3606:       /* calculate the number of nonzeros on each processor */
3607:       for (j=0; j<browners[i+1]-browners[i]; j++) {
3608:         procsnz[i] += rowlengths[j];
3609:       }
3610:       MPI_Send(rowlengths,browners[i+1]-browners[i],MPIU_INT,i,tag,comm);
3611:     }
3612:     PetscFree(rowlengths);
3613:   } else {
3614:     MPI_Recv(locrowlens,m,MPIU_INT,0,tag,comm,&status);
3615:   }

3617:   if (!rank) {
3618:     /* determine max buffer needed and allocate it */
3619:     maxnz = procsnz[0];
3620:     for (i=1; i<size; i++) {
3621:       maxnz = PetscMax(maxnz,procsnz[i]);
3622:     }
3623:     PetscMalloc1(maxnz,&cols);

3625:     /* read in my part of the matrix column indices  */
3626:     nz     = procsnz[0];
3627:     PetscMalloc1(nz+1,&ibuf);
3628:     mycols = ibuf;
3629:     if (size == 1) nz -= extra_rows;
3630:     PetscBinaryRead(fd,mycols,nz,PETSC_INT);
3631:     if (size == 1) {
3632:       for (i=0; i< extra_rows; i++) mycols[nz+i] = M+i;
3633:     }

3635:     /* read in every ones (except the last) and ship off */
3636:     for (i=1; i<size-1; i++) {
3637:       nz   = procsnz[i];
3638:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
3639:       MPI_Send(cols,nz,MPIU_INT,i,tag,comm);
3640:     }
3641:     /* read in the stuff for the last proc */
3642:     if (size != 1) {
3643:       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
3644:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
3645:       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
3646:       MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);
3647:     }
3648:     PetscFree(cols);
3649:   } else {
3650:     /* determine buffer space needed for message */
3651:     nz = 0;
3652:     for (i=0; i<m; i++) {
3653:       nz += locrowlens[i];
3654:     }
3655:     PetscMalloc1(nz+1,&ibuf);
3656:     mycols = ibuf;
3657:     /* receive message of column indices*/
3658:     MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);
3659:     MPI_Get_count(&status,MPIU_INT,&maxnz);
3660:     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
3661:   }

3663:   /* loop over local rows, determining number of off diagonal entries */
3664:   PetscMalloc2(rend-rstart,&dlens,rend-rstart,&odlens);
3665:   PetscCalloc3(Mbs,&mask,Mbs,&masked1,Mbs,&masked2);
3666:   rowcount = 0; nzcount = 0;
3667:   for (i=0; i<mbs; i++) {
3668:     dcount  = 0;
3669:     odcount = 0;
3670:     for (j=0; j<bs; j++) {
3671:       kmax = locrowlens[rowcount];
3672:       for (k=0; k<kmax; k++) {
3673:         tmp = mycols[nzcount++]/bs;
3674:         if (!mask[tmp]) {
3675:           mask[tmp] = 1;
3676:           if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp;
3677:           else masked1[dcount++] = tmp;
3678:         }
3679:       }
3680:       rowcount++;
3681:     }

3683:     dlens[i]  = dcount;
3684:     odlens[i] = odcount;

3686:     /* zero out the mask elements we set */
3687:     for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
3688:     for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
3689:   }

3691:   MatSetSizes(newmat,m,m,M+extra_rows,N+extra_rows);
3692:   MatMPIBAIJSetPreallocation(newmat,bs,0,dlens,0,odlens);

3694:   if (!rank) {
3695:     PetscMalloc1(maxnz+1,&buf);
3696:     /* read in my part of the matrix numerical values  */
3697:     nz     = procsnz[0];
3698:     vals   = buf;
3699:     mycols = ibuf;
3700:     if (size == 1) nz -= extra_rows;
3701:     PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3702:     if (size == 1) {
3703:       for (i=0; i< extra_rows; i++) vals[nz+i] = 1.0;
3704:     }

3706:     /* insert into matrix */
3707:     jj = rstart*bs;
3708:     for (i=0; i<m; i++) {
3709:       MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
3710:       mycols += locrowlens[i];
3711:       vals   += locrowlens[i];
3712:       jj++;
3713:     }
3714:     /* read in other processors (except the last one) and ship out */
3715:     for (i=1; i<size-1; i++) {
3716:       nz   = procsnz[i];
3717:       vals = buf;
3718:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3719:       MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);
3720:     }
3721:     /* the last proc */
3722:     if (size != 1) {
3723:       nz   = procsnz[i] - extra_rows;
3724:       vals = buf;
3725:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3726:       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
3727:       MPIULong_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)newmat)->tag,comm);
3728:     }
3729:     PetscFree(procsnz);
3730:   } else {
3731:     /* receive numeric values */
3732:     PetscMalloc1(nz+1,&buf);

3734:     /* receive message of values*/
3735:     vals   = buf;
3736:     mycols = ibuf;
3737:     MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm);

3739:     /* insert into matrix */
3740:     jj = rstart*bs;
3741:     for (i=0; i<m; i++) {
3742:       MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
3743:       mycols += locrowlens[i];
3744:       vals   += locrowlens[i];
3745:       jj++;
3746:     }
3747:   }
3748:   PetscFree(locrowlens);
3749:   PetscFree(buf);
3750:   PetscFree(ibuf);
3751:   PetscFree2(rowners,browners);
3752:   PetscFree2(dlens,odlens);
3753:   PetscFree3(mask,masked1,masked2);
3754:   MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
3755:   MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
3756:   return(0);
3757: }

3761: /*@
3762:    MatMPIBAIJSetHashTableFactor - Sets the factor required to compute the size of the HashTable.

3764:    Input Parameters:
3765: .  mat  - the matrix
3766: .  fact - factor

3768:    Not Collective, each process can use a different factor

3770:    Level: advanced

3772:   Notes:
3773:    This can also be set by the command line option: -mat_use_hash_table <fact>

3775: .keywords: matrix, hashtable, factor, HT

3777: .seealso: MatSetOption()
3778: @*/
3779: PetscErrorCode  MatMPIBAIJSetHashTableFactor(Mat mat,PetscReal fact)
3780: {

3784:   PetscTryMethod(mat,"MatSetHashTableFactor_C",(Mat,PetscReal),(mat,fact));
3785:   return(0);
3786: }

3790: PetscErrorCode  MatSetHashTableFactor_MPIBAIJ(Mat mat,PetscReal fact)
3791: {
3792:   Mat_MPIBAIJ *baij;

3795:   baij          = (Mat_MPIBAIJ*)mat->data;
3796:   baij->ht_fact = fact;
3797:   return(0);
3798: }

3802: PetscErrorCode  MatMPIBAIJGetSeqBAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[])
3803: {
3804:   Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;

3807:   if (Ad)     *Ad     = a->A;
3808:   if (Ao)     *Ao     = a->B;
3809:   if (colmap) *colmap = a->garray;
3810:   return(0);
3811: }

3813: /*
3814:     Special version for direct calls from Fortran (to eliminate two function call overheads
3815: */
3816: #if defined(PETSC_HAVE_FORTRAN_CAPS)
3817: #define matmpibaijsetvaluesblocked_ MATMPIBAIJSETVALUESBLOCKED
3818: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
3819: #define matmpibaijsetvaluesblocked_ matmpibaijsetvaluesblocked
3820: #endif

3824: /*@C
3825:   MatMPIBAIJSetValuesBlocked - Direct Fortran call to replace call to MatSetValuesBlocked()

3827:   Collective on Mat

3829:   Input Parameters:
3830: + mat - the matrix
3831: . min - number of input rows
3832: . im - input rows
3833: . nin - number of input columns
3834: . in - input columns
3835: . v - numerical values input
3836: - addvin - INSERT_VALUES or ADD_VALUES

3838:   Notes: This has a complete copy of MatSetValuesBlocked_MPIBAIJ() which is terrible code un-reuse.

3840:   Level: advanced

3842: .seealso:   MatSetValuesBlocked()
3843: @*/
3844: PetscErrorCode matmpibaijsetvaluesblocked_(Mat *matin,PetscInt *min,const PetscInt im[],PetscInt *nin,const PetscInt in[],const MatScalar v[],InsertMode *addvin)
3845: {
3846:   /* convert input arguments to C version */
3847:   Mat        mat  = *matin;
3848:   PetscInt   m    = *min, n = *nin;
3849:   InsertMode addv = *addvin;

3851:   Mat_MPIBAIJ     *baij = (Mat_MPIBAIJ*)mat->data;
3852:   const MatScalar *value;
3853:   MatScalar       *barray     = baij->barray;
3854:   PetscBool       roworiented = baij->roworiented;
3855:   PetscErrorCode  ierr;
3856:   PetscInt        i,j,ii,jj,row,col,rstart=baij->rstartbs;
3857:   PetscInt        rend=baij->rendbs,cstart=baij->cstartbs,stepval;
3858:   PetscInt        cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;

3861:   /* tasks normally handled by MatSetValuesBlocked() */
3862:   if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;
3863: #if defined(PETSC_USE_DEBUG)
3864:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
3865:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3866: #endif
3867:   if (mat->assembled) {
3868:     mat->was_assembled = PETSC_TRUE;
3869:     mat->assembled     = PETSC_FALSE;
3870:   }
3871:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);


3874:   if (!barray) {
3875:     PetscMalloc1(bs2,&barray);
3876:     baij->barray = barray;
3877:   }

3879:   if (roworiented) stepval = (n-1)*bs;
3880:   else stepval = (m-1)*bs;

3882:   for (i=0; i<m; i++) {
3883:     if (im[i] < 0) continue;
3884: #if defined(PETSC_USE_DEBUG)
3885:     if (im[i] >= baij->Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large, row %D max %D",im[i],baij->Mbs-1);
3886: #endif
3887:     if (im[i] >= rstart && im[i] < rend) {
3888:       row = im[i] - rstart;
3889:       for (j=0; j<n; j++) {
3890:         /* If NumCol = 1 then a copy is not required */
3891:         if ((roworiented) && (n == 1)) {
3892:           barray = (MatScalar*)v + i*bs2;
3893:         } else if ((!roworiented) && (m == 1)) {
3894:           barray = (MatScalar*)v + j*bs2;
3895:         } else { /* Here a copy is required */
3896:           if (roworiented) {
3897:             value = v + i*(stepval+bs)*bs + j*bs;
3898:           } else {
3899:             value = v + j*(stepval+bs)*bs + i*bs;
3900:           }
3901:           for (ii=0; ii<bs; ii++,value+=stepval) {
3902:             for (jj=0; jj<bs; jj++) {
3903:               *barray++ = *value++;
3904:             }
3905:           }
3906:           barray -=bs2;
3907:         }

3909:         if (in[j] >= cstart && in[j] < cend) {
3910:           col  = in[j] - cstart;
3911:           MatSetValuesBlocked_SeqBAIJ_Inlined(baij->A,row,col,barray,addv,im[i],in[j]);
3912:         } else if (in[j] < 0) continue;
3913: #if defined(PETSC_USE_DEBUG)
3914:         else if (in[j] >= baij->Nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large, col %D max %D",in[j],baij->Nbs-1);
3915: #endif
3916:         else {
3917:           if (mat->was_assembled) {
3918:             if (!baij->colmap) {
3919:               MatCreateColmap_MPIBAIJ_Private(mat);
3920:             }

3922: #if defined(PETSC_USE_DEBUG)
3923: #if defined(PETSC_USE_CTABLE)
3924:             { PetscInt data;
3925:               PetscTableFind(baij->colmap,in[j]+1,&data);
3926:               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
3927:             }
3928: #else
3929:             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
3930: #endif
3931: #endif
3932: #if defined(PETSC_USE_CTABLE)
3933:             PetscTableFind(baij->colmap,in[j]+1,&col);
3934:             col  = (col - 1)/bs;
3935: #else
3936:             col = (baij->colmap[in[j]] - 1)/bs;
3937: #endif
3938:             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
3939:               MatDisAssemble_MPIBAIJ(mat);
3940:               col  =  in[j];
3941:             }
3942:           } else col = in[j];
3943:           MatSetValuesBlocked_SeqBAIJ_Inlined(baij->B,row,col,barray,addv,im[i],in[j]);
3944:         }
3945:       }
3946:     } else {
3947:       if (!baij->donotstash) {
3948:         if (roworiented) {
3949:           MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
3950:         } else {
3951:           MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
3952:         }
3953:       }
3954:     }
3955:   }

3957:   /* task normally handled by MatSetValuesBlocked() */
3958:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
3959:   return(0);
3960: }

3964: /*@
3965:      MatCreateMPIBAIJWithArrays - creates a MPI BAIJ matrix using arrays that contain in standard
3966:          CSR format the local rows.

3968:    Collective on MPI_Comm

3970:    Input Parameters:
3971: +  comm - MPI communicator
3972: .  bs - the block size, only a block size of 1 is supported
3973: .  m - number of local rows (Cannot be PETSC_DECIDE)
3974: .  n - This value should be the same as the local size used in creating the
3975:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
3976:        calculated if N is given) For square matrices n is almost always m.
3977: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3978: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
3979: .   i - row indices
3980: .   j - column indices
3981: -   a - matrix values

3983:    Output Parameter:
3984: .   mat - the matrix

3986:    Level: intermediate

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

3993:      The order of the entries in values is the same as the block compressed sparse row storage format; that is, it is
3994:      the same as a three dimensional array in Fortran values(bs,bs,nnz) that contains the first column of the first
3995:      block, followed by the second column of the first block etc etc.  That is, the blocks are contiguous in memory
3996:      with column-major ordering within blocks.

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

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

4002: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4003:           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
4004: @*/
4005: PetscErrorCode  MatCreateMPIBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
4006: {

4010:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4011:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
4012:   MatCreate(comm,mat);
4013:   MatSetSizes(*mat,m,n,M,N);
4014:   MatSetType(*mat,MATMPISBAIJ);
4015:   MatSetOption(*mat,MAT_ROW_ORIENTED,PETSC_FALSE);
4016:   MatMPIBAIJSetPreallocationCSR(*mat,bs,i,j,a);
4017:   MatSetOption(*mat,MAT_ROW_ORIENTED,PETSC_TRUE);
4018:   return(0);
4019: }

4023: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPIBAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4024: {
4026:   PetscInt       m,N,i,rstart,nnz,Ii,bs,cbs;
4027:   PetscInt       *indx;
4028:   PetscScalar    *values;

4031:   MatGetSize(inmat,&m,&N);
4032:   if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
4033:     Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)inmat->data;
4034:     PetscInt       *dnz,*onz,sum,mbs,Nbs;
4035:     PetscInt       *bindx,rmax=a->rmax,j;
4036: 
4037:     MatGetBlockSizes(inmat,&bs,&cbs);
4038:     mbs = m/bs; Nbs = N/cbs;
4039:     if (n == PETSC_DECIDE) {
4040:       PetscSplitOwnership(comm,&n,&Nbs);
4041:     }
4042:     /* Check sum(n) = Nbs */
4043:     MPIU_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);
4044:     if (sum != Nbs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",Nbs);

4046:     MPI_Scan(&mbs, &rstart,1,MPIU_INT,MPI_SUM,comm);
4047:     rstart -= mbs;

4049:     PetscMalloc1(rmax,&bindx);
4050:     MatPreallocateInitialize(comm,mbs,n,dnz,onz);
4051:     for (i=0; i<mbs; i++) {
4052:       MatGetRow_SeqBAIJ(inmat,i*bs,&nnz,&indx,NULL); /* non-blocked nnz and indx */
4053:       nnz = nnz/bs;
4054:       for (j=0; j<nnz; j++) bindx[j] = indx[j*bs]/bs;
4055:       MatPreallocateSet(i+rstart,nnz,bindx,dnz,onz);
4056:       MatRestoreRow_SeqBAIJ(inmat,i*bs,&nnz,&indx,NULL);
4057:     }
4058:     PetscFree(bindx);

4060:     MatCreate(comm,outmat);
4061:     MatSetSizes(*outmat,m,n*bs,PETSC_DETERMINE,PETSC_DETERMINE);
4062:     MatSetBlockSizes(*outmat,bs,cbs);
4063:     MatSetType(*outmat,MATMPIBAIJ);
4064:     MatMPIBAIJSetPreallocation(*outmat,bs,0,dnz,0,onz);
4065:     MatPreallocateFinalize(dnz,onz);
4066:   }
4067: 
4068:   /* numeric phase */
4069:   MatGetBlockSizes(inmat,&bs,&cbs);
4070:   MatGetOwnershipRange(*outmat,&rstart,NULL);

4072:   for (i=0; i<m; i++) {
4073:     MatGetRow_SeqBAIJ(inmat,i,&nnz,&indx,&values);
4074:     Ii   = i + rstart;
4075:     MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);
4076:     MatRestoreRow_SeqBAIJ(inmat,i,&nnz,&indx,&values);
4077:   }
4078:   MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);
4079:   MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);
4080:   return(0);
4081: }