Actual source code: mpisbaij.c

petsc-master 2019-09-17
Report Typos and Errors

  2:  #include <../src/mat/impls/baij/mpi/mpibaij.h>
  3:  #include <../src/mat/impls/sbaij/mpi/mpisbaij.h>
  4:  #include <../src/mat/impls/sbaij/seq/sbaij.h>
  5:  #include <petscblaslapack.h>

  7: #if defined(PETSC_HAVE_ELEMENTAL)
  8: PETSC_INTERN PetscErrorCode MatConvert_MPISBAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
  9: #endif

 11: /* This could be moved to matimpl.h */
 12: static PetscErrorCode MatPreallocateWithMats_Private(Mat B, PetscInt nm, Mat X[], PetscBool symm[], PetscBool fill)
 13: {
 14:   Mat            preallocator;
 15:   PetscInt       r,rstart,rend;
 16:   PetscInt       bs,i,m,n,M,N;
 17:   PetscBool      cong = PETSC_TRUE;

 23:   for (i = 0; i < nm; i++) {
 25:     PetscLayoutCompare(B->rmap,X[i]->rmap,&cong);
 26:     if (!cong) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_SUP,"Not for different layouts");
 27:   }
 29:   MatGetBlockSize(B,&bs);
 30:   MatGetSize(B,&M,&N);
 31:   MatGetLocalSize(B,&m,&n);
 32:   MatCreate(PetscObjectComm((PetscObject)B),&preallocator);
 33:   MatSetType(preallocator,MATPREALLOCATOR);
 34:   MatSetBlockSize(preallocator,bs);
 35:   MatSetSizes(preallocator,m,n,M,N);
 36:   MatSetUp(preallocator);
 37:   MatGetOwnershipRange(preallocator,&rstart,&rend);
 38:   for (r = rstart; r < rend; ++r) {
 39:     PetscInt          ncols;
 40:     const PetscInt    *row;
 41:     const PetscScalar *vals;

 43:     for (i = 0; i < nm; i++) {
 44:       MatGetRow(X[i],r,&ncols,&row,&vals);
 45:       MatSetValues(preallocator,1,&r,ncols,row,vals,INSERT_VALUES);
 46:       if (symm && symm[i]) {
 47:         MatSetValues(preallocator,ncols,row,1,&r,vals,INSERT_VALUES);
 48:       }
 49:       MatRestoreRow(X[i],r,&ncols,&row,&vals);
 50:     }
 51:   }
 52:   MatAssemblyBegin(preallocator,MAT_FINAL_ASSEMBLY);
 53:   MatAssemblyEnd(preallocator,MAT_FINAL_ASSEMBLY);
 54:   MatPreallocatorPreallocate(preallocator,fill,B);
 55:   MatDestroy(&preallocator);
 56:   return(0);
 57: }

 59: static PetscErrorCode MatConvert_MPISBAIJ_XAIJ(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
 60: {
 61:   Mat            B;
 63:   PetscInt       r;

 66:   if (reuse != MAT_REUSE_MATRIX) {
 67:     PetscBool symm = PETSC_TRUE;
 68:     PetscInt  bs;

 70:     MatCreate(PetscObjectComm((PetscObject)A),&B);
 71:     MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
 72:     MatSetType(B,newtype);
 73:     MatGetBlockSize(A,&bs);
 74:     MatSetBlockSize(B,bs);
 75:     PetscLayoutSetUp(B->rmap);
 76:     PetscLayoutSetUp(B->cmap);
 77:     MatGetRowUpperTriangular(A);
 78:     MatPreallocateWithMats_Private(B,1,&A,&symm,PETSC_TRUE);
 79:     MatRestoreRowUpperTriangular(A);
 80:   } else B = *newmat;

 82:   MatGetRowUpperTriangular(A);
 83:   for (r = A->rmap->rstart; r < A->rmap->rend; r++) {
 84:     PetscInt          ncols;
 85:     const PetscInt    *row;
 86:     const PetscScalar *vals;

 88:     MatGetRow(A,r,&ncols,&row,&vals);
 89:     MatSetValues(B,1,&r,ncols,row,vals,INSERT_VALUES);
 90:     MatSetValues(B,ncols,row,1,&r,vals,INSERT_VALUES);
 91:     MatRestoreRow(A,r,&ncols,&row,&vals);
 92:   }
 93:   MatRestoreRowUpperTriangular(A);
 94:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
 95:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

 97:   if (reuse == MAT_INPLACE_MATRIX) {
 98:     MatHeaderReplace(A,&B);
 99:   } else {
100:     *newmat = B;
101:   }
102:   return(0);
103: }

105: PetscErrorCode  MatStoreValues_MPISBAIJ(Mat mat)
106: {
107:   Mat_MPISBAIJ   *aij = (Mat_MPISBAIJ*)mat->data;

111:   MatStoreValues(aij->A);
112:   MatStoreValues(aij->B);
113:   return(0);
114: }

116: PetscErrorCode  MatRetrieveValues_MPISBAIJ(Mat mat)
117: {
118:   Mat_MPISBAIJ   *aij = (Mat_MPISBAIJ*)mat->data;

122:   MatRetrieveValues(aij->A);
123:   MatRetrieveValues(aij->B);
124:   return(0);
125: }

127: #define  MatSetValues_SeqSBAIJ_A_Private(row,col,value,addv,orow,ocol)      \
128:   { \
129:     brow = row/bs;  \
130:     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
131:     rmax = aimax[brow]; nrow = ailen[brow]; \
132:     bcol = col/bs; \
133:     ridx = row % bs; cidx = col % bs; \
134:     low  = 0; high = nrow; \
135:     while (high-low > 3) { \
136:       t = (low+high)/2; \
137:       if (rp[t] > bcol) high = t; \
138:       else              low  = t; \
139:     } \
140:     for (_i=low; _i<high; _i++) { \
141:       if (rp[_i] > bcol) break; \
142:       if (rp[_i] == bcol) { \
143:         bap = ap + bs2*_i + bs*cidx + ridx; \
144:         if (addv == ADD_VALUES) *bap += value;  \
145:         else                    *bap  = value;  \
146:         goto a_noinsert; \
147:       } \
148:     } \
149:     if (a->nonew == 1) goto a_noinsert; \
150:     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); \
151:     MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,aimax,a->nonew,MatScalar); \
152:     N = nrow++ - 1;  \
153:     /* shift up all the later entries in this row */ \
154:     PetscArraymove(rp+_i+1,rp+_i,N-_i+1); \
155:     PetscArraymove(ap+bs2*(_i+1),ap+bs2*_i,bs2*(N-_i+1)); \
156:     PetscArrayzero(ap+bs2*_i,bs2);  \
157:     rp[_i]                      = bcol;  \
158:     ap[bs2*_i + bs*cidx + ridx] = value;  \
159:     A->nonzerostate++;\
160: a_noinsert:; \
161:     ailen[brow] = nrow; \
162:   }

164: #define  MatSetValues_SeqSBAIJ_B_Private(row,col,value,addv,orow,ocol) \
165:   { \
166:     brow = row/bs;  \
167:     rp   = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
168:     rmax = bimax[brow]; nrow = bilen[brow]; \
169:     bcol = col/bs; \
170:     ridx = row % bs; cidx = col % bs; \
171:     low  = 0; high = nrow; \
172:     while (high-low > 3) { \
173:       t = (low+high)/2; \
174:       if (rp[t] > bcol) high = t; \
175:       else              low  = t; \
176:     } \
177:     for (_i=low; _i<high; _i++) { \
178:       if (rp[_i] > bcol) break; \
179:       if (rp[_i] == bcol) { \
180:         bap = ap + bs2*_i + bs*cidx + ridx; \
181:         if (addv == ADD_VALUES) *bap += value;  \
182:         else                    *bap  = value;  \
183:         goto b_noinsert; \
184:       } \
185:     } \
186:     if (b->nonew == 1) goto b_noinsert; \
187:     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); \
188:     MatSeqXAIJReallocateAIJ(B,b->mbs,bs2,nrow,brow,bcol,rmax,ba,bi,bj,rp,ap,bimax,b->nonew,MatScalar); \
189:     N = nrow++ - 1;  \
190:     /* shift up all the later entries in this row */ \
191:     PetscArraymove(rp+_i+1,rp+_i,N-_i+1); \
192:     PetscArraymove(ap+bs2*(_i+1),ap+bs2*_i,bs2*(N-_i+1)); \
193:     PetscArrayzero(ap+bs2*_i,bs2); \
194:     rp[_i]                      = bcol;  \
195:     ap[bs2*_i + bs*cidx + ridx] = value;  \
196:     B->nonzerostate++;\
197: b_noinsert:; \
198:     bilen[brow] = nrow; \
199:   }

201: /* Only add/insert a(i,j) with i<=j (blocks).
202:    Any a(i,j) with i>j input by user is ingored or generates an error
203: */
204: PetscErrorCode MatSetValues_MPISBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
205: {
206:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
207:   MatScalar      value;
208:   PetscBool      roworiented = baij->roworiented;
210:   PetscInt       i,j,row,col;
211:   PetscInt       rstart_orig=mat->rmap->rstart;
212:   PetscInt       rend_orig  =mat->rmap->rend,cstart_orig=mat->cmap->rstart;
213:   PetscInt       cend_orig  =mat->cmap->rend,bs=mat->rmap->bs;

215:   /* Some Variables required in the macro */
216:   Mat          A     = baij->A;
217:   Mat_SeqSBAIJ *a    = (Mat_SeqSBAIJ*)(A)->data;
218:   PetscInt     *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
219:   MatScalar    *aa   =a->a;

221:   Mat         B     = baij->B;
222:   Mat_SeqBAIJ *b    = (Mat_SeqBAIJ*)(B)->data;
223:   PetscInt    *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
224:   MatScalar   *ba   =b->a;

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

230:   /* for stash */
231:   PetscInt  n_loc, *in_loc = NULL;
232:   MatScalar *v_loc = NULL;

235:   if (!baij->donotstash) {
236:     if (n > baij->n_loc) {
237:       PetscFree(baij->in_loc);
238:       PetscFree(baij->v_loc);
239:       PetscMalloc1(n,&baij->in_loc);
240:       PetscMalloc1(n,&baij->v_loc);

242:       baij->n_loc = n;
243:     }
244:     in_loc = baij->in_loc;
245:     v_loc  = baij->v_loc;
246:   }

248:   for (i=0; i<m; i++) {
249:     if (im[i] < 0) continue;
250: #if defined(PETSC_USE_DEBUG)
251:     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);
252: #endif
253:     if (im[i] >= rstart_orig && im[i] < rend_orig) { /* this processor entry */
254:       row = im[i] - rstart_orig;              /* local row index */
255:       for (j=0; j<n; j++) {
256:         if (im[i]/bs > in[j]/bs) {
257:           if (a->ignore_ltriangular) {
258:             continue;    /* ignore lower triangular blocks */
259:           } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Lower triangular value cannot be set for sbaij format. Ignoring these values, run with -mat_ignore_lower_triangular or call MatSetOption(mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE)");
260:         }
261:         if (in[j] >= cstart_orig && in[j] < cend_orig) {  /* diag entry (A) */
262:           col  = in[j] - cstart_orig;         /* local col index */
263:           brow = row/bs; bcol = col/bs;
264:           if (brow > bcol) continue;  /* ignore lower triangular blocks of A */
265:           if (roworiented) value = v[i*n+j];
266:           else             value = v[i+j*m];
267:           MatSetValues_SeqSBAIJ_A_Private(row,col,value,addv,im[i],in[j]);
268:           /* MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv); */
269:         } else if (in[j] < 0) continue;
270: #if defined(PETSC_USE_DEBUG)
271:         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);
272: #endif
273:         else {  /* off-diag entry (B) */
274:           if (mat->was_assembled) {
275:             if (!baij->colmap) {
276:               MatCreateColmap_MPIBAIJ_Private(mat);
277:             }
278: #if defined(PETSC_USE_CTABLE)
279:             PetscTableFind(baij->colmap,in[j]/bs + 1,&col);
280:             col  = col - 1;
281: #else
282:             col = baij->colmap[in[j]/bs] - 1;
283: #endif
284:             if (col < 0 && !((Mat_SeqSBAIJ*)(baij->A->data))->nonew) {
285:               MatDisAssemble_MPISBAIJ(mat);
286:               col  =  in[j];
287:               /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
288:               B    = baij->B;
289:               b    = (Mat_SeqBAIJ*)(B)->data;
290:               bimax= b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
291:               ba   = b->a;
292:             } else col += in[j]%bs;
293:           } else col = in[j];
294:           if (roworiented) value = v[i*n+j];
295:           else             value = v[i+j*m];
296:           MatSetValues_SeqSBAIJ_B_Private(row,col,value,addv,im[i],in[j]);
297:           /* MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv); */
298:         }
299:       }
300:     } else {  /* off processor entry */
301:       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]);
302:       if (!baij->donotstash) {
303:         mat->assembled = PETSC_FALSE;
304:         n_loc          = 0;
305:         for (j=0; j<n; j++) {
306:           if (im[i]/bs > in[j]/bs) continue; /* ignore lower triangular blocks */
307:           in_loc[n_loc] = in[j];
308:           if (roworiented) {
309:             v_loc[n_loc] = v[i*n+j];
310:           } else {
311:             v_loc[n_loc] = v[j*m+i];
312:           }
313:           n_loc++;
314:         }
315:         MatStashValuesRow_Private(&mat->stash,im[i],n_loc,in_loc,v_loc,PETSC_FALSE);
316:       }
317:     }
318:   }
319:   return(0);
320: }

322: PETSC_STATIC_INLINE PetscErrorCode MatSetValuesBlocked_SeqSBAIJ_Inlined(Mat A,PetscInt row,PetscInt col,const PetscScalar v[],InsertMode is,PetscInt orow,PetscInt ocol)
323: {
324:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
325:   PetscErrorCode    ierr;
326:   PetscInt          *rp,low,high,t,ii,jj,nrow,i,rmax,N;
327:   PetscInt          *imax      =a->imax,*ai=a->i,*ailen=a->ilen;
328:   PetscInt          *aj        =a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs;
329:   PetscBool         roworiented=a->roworiented;
330:   const PetscScalar *value     = v;
331:   MatScalar         *ap,*aa = a->a,*bap;

334:   if (col < row) {
335:     if (a->ignore_ltriangular) return(0); /* ignore lower triangular block */
336:     else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Lower triangular value cannot be set for sbaij format. Ignoring these values, run with -mat_ignore_lower_triangular or call MatSetOption(mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE)");
337:   }
338:   rp   = aj + ai[row];
339:   ap   = aa + bs2*ai[row];
340:   rmax = imax[row];
341:   nrow = ailen[row];
342:   value = v;
343:   low   = 0;
344:   high  = nrow;

346:   while (high-low > 7) {
347:     t = (low+high)/2;
348:     if (rp[t] > col) high = t;
349:     else             low  = t;
350:   }
351:   for (i=low; i<high; i++) {
352:     if (rp[i] > col) break;
353:     if (rp[i] == col) {
354:       bap = ap +  bs2*i;
355:       if (roworiented) {
356:         if (is == ADD_VALUES) {
357:           for (ii=0; ii<bs; ii++) {
358:             for (jj=ii; jj<bs2; jj+=bs) {
359:               bap[jj] += *value++;
360:             }
361:           }
362:         } else {
363:           for (ii=0; ii<bs; ii++) {
364:             for (jj=ii; jj<bs2; jj+=bs) {
365:               bap[jj] = *value++;
366:             }
367:           }
368:         }
369:       } else {
370:         if (is == ADD_VALUES) {
371:           for (ii=0; ii<bs; ii++) {
372:             for (jj=0; jj<bs; jj++) {
373:               *bap++ += *value++;
374:             }
375:           }
376:         } else {
377:           for (ii=0; ii<bs; ii++) {
378:             for (jj=0; jj<bs; jj++) {
379:               *bap++  = *value++;
380:             }
381:           }
382:         }
383:       }
384:       goto noinsert2;
385:     }
386:   }
387:   if (nonew == 1) goto noinsert2;
388:   if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new block index nonzero block (%D, %D) in the matrix", orow, ocol);
389:   MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
390:   N = nrow++ - 1; high++;
391:   /* shift up all the later entries in this row */
392:   PetscArraymove(rp+i+1,rp+i,N-i+1);
393:   PetscArraymove(ap+bs2*(i+1),ap+bs2*i,bs2*(N-i+1));
394:   rp[i] = col;
395:   bap   = ap +  bs2*i;
396:   if (roworiented) {
397:     for (ii=0; ii<bs; ii++) {
398:       for (jj=ii; jj<bs2; jj+=bs) {
399:         bap[jj] = *value++;
400:       }
401:     }
402:   } else {
403:     for (ii=0; ii<bs; ii++) {
404:       for (jj=0; jj<bs; jj++) {
405:         *bap++ = *value++;
406:       }
407:     }
408:   }
409:   noinsert2:;
410:   ailen[row] = nrow;
411:   return(0);
412: }

414: /*
415:    This routine is exactly duplicated in mpibaij.c
416: */
417: PETSC_STATIC_INLINE PetscErrorCode MatSetValuesBlocked_SeqBAIJ_Inlined(Mat A,PetscInt row,PetscInt col,const PetscScalar v[],InsertMode is,PetscInt orow,PetscInt ocol)
418: {
419:   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
420:   PetscInt          *rp,low,high,t,ii,jj,nrow,i,rmax,N;
421:   PetscInt          *imax=a->imax,*ai=a->i,*ailen=a->ilen;
422:   PetscErrorCode    ierr;
423:   PetscInt          *aj        =a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs;
424:   PetscBool         roworiented=a->roworiented;
425:   const PetscScalar *value     = v;
426:   MatScalar         *ap,*aa = a->a,*bap;

429:   rp   = aj + ai[row];
430:   ap   = aa + bs2*ai[row];
431:   rmax = imax[row];
432:   nrow = ailen[row];
433:   low  = 0;
434:   high = nrow;
435:   value = v;
436:   while (high-low > 7) {
437:     t = (low+high)/2;
438:     if (rp[t] > col) high = t;
439:     else             low  = t;
440:   }
441:   for (i=low; i<high; i++) {
442:     if (rp[i] > col) break;
443:     if (rp[i] == col) {
444:       bap = ap +  bs2*i;
445:       if (roworiented) {
446:         if (is == ADD_VALUES) {
447:           for (ii=0; ii<bs; ii++) {
448:             for (jj=ii; jj<bs2; jj+=bs) {
449:               bap[jj] += *value++;
450:             }
451:           }
452:         } else {
453:           for (ii=0; ii<bs; ii++) {
454:             for (jj=ii; jj<bs2; jj+=bs) {
455:               bap[jj] = *value++;
456:             }
457:           }
458:         }
459:       } else {
460:         if (is == ADD_VALUES) {
461:           for (ii=0; ii<bs; ii++,value+=bs) {
462:             for (jj=0; jj<bs; jj++) {
463:               bap[jj] += value[jj];
464:             }
465:             bap += bs;
466:           }
467:         } else {
468:           for (ii=0; ii<bs; ii++,value+=bs) {
469:             for (jj=0; jj<bs; jj++) {
470:               bap[jj]  = value[jj];
471:             }
472:             bap += bs;
473:           }
474:         }
475:       }
476:       goto noinsert2;
477:     }
478:   }
479:   if (nonew == 1) goto noinsert2;
480:   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);
481:   MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
482:   N = nrow++ - 1; high++;
483:   /* shift up all the later entries in this row */
484:   PetscArraymove(rp+i+1,rp+i,N-i+1);
485:   PetscArraymove(ap+bs2*(i+1),ap+bs2*i,bs2*(N-i+1));
486:   rp[i] = col;
487:   bap   = ap +  bs2*i;
488:   if (roworiented) {
489:     for (ii=0; ii<bs; ii++) {
490:       for (jj=ii; jj<bs2; jj+=bs) {
491:         bap[jj] = *value++;
492:       }
493:     }
494:   } else {
495:     for (ii=0; ii<bs; ii++) {
496:       for (jj=0; jj<bs; jj++) {
497:         *bap++ = *value++;
498:       }
499:     }
500:   }
501:   noinsert2:;
502:   ailen[row] = nrow;
503:   return(0);
504: }

506: /*
507:     This routine could be optimized by removing the need for the block copy below and passing stride information
508:   to the above inline routines; similarly in MatSetValuesBlocked_MPIBAIJ()
509: */
510: PetscErrorCode MatSetValuesBlocked_MPISBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const MatScalar v[],InsertMode addv)
511: {
512:   Mat_MPISBAIJ    *baij = (Mat_MPISBAIJ*)mat->data;
513:   const MatScalar *value;
514:   MatScalar       *barray     =baij->barray;
515:   PetscBool       roworiented = baij->roworiented,ignore_ltriangular = ((Mat_SeqSBAIJ*)baij->A->data)->ignore_ltriangular;
516:   PetscErrorCode  ierr;
517:   PetscInt        i,j,ii,jj,row,col,rstart=baij->rstartbs;
518:   PetscInt        rend=baij->rendbs,cstart=baij->cstartbs,stepval;
519:   PetscInt        cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;

522:   if (!barray) {
523:     PetscMalloc1(bs2,&barray);
524:     baij->barray = barray;
525:   }

527:   if (roworiented) {
528:     stepval = (n-1)*bs;
529:   } else {
530:     stepval = (m-1)*bs;
531:   }
532:   for (i=0; i<m; i++) {
533:     if (im[i] < 0) continue;
534: #if defined(PETSC_USE_DEBUG)
535:     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);
536: #endif
537:     if (im[i] >= rstart && im[i] < rend) {
538:       row = im[i] - rstart;
539:       for (j=0; j<n; j++) {
540:         if (im[i] > in[j]) {
541:           if (ignore_ltriangular) continue; /* ignore lower triangular blocks */
542:           else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Lower triangular value cannot be set for sbaij format. Ignoring these values, run with -mat_ignore_lower_triangular or call MatSetOption(mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE)");
543:         }
544:         /* If NumCol = 1 then a copy is not required */
545:         if ((roworiented) && (n == 1)) {
546:           barray = (MatScalar*) v + i*bs2;
547:         } else if ((!roworiented) && (m == 1)) {
548:           barray = (MatScalar*) v + j*bs2;
549:         } else { /* Here a copy is required */
550:           if (roworiented) {
551:             value = v + i*(stepval+bs)*bs + j*bs;
552:           } else {
553:             value = v + j*(stepval+bs)*bs + i*bs;
554:           }
555:           for (ii=0; ii<bs; ii++,value+=stepval) {
556:             for (jj=0; jj<bs; jj++) {
557:               *barray++ = *value++;
558:             }
559:           }
560:           barray -=bs2;
561:         }

563:         if (in[j] >= cstart && in[j] < cend) {
564:           col  = in[j] - cstart;
565:           MatSetValuesBlocked_SeqSBAIJ_Inlined(baij->A,row,col,barray,addv,im[i],in[j]);
566:         } else if (in[j] < 0) continue;
567: #if defined(PETSC_USE_DEBUG)
568:         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);
569: #endif
570:         else {
571:           if (mat->was_assembled) {
572:             if (!baij->colmap) {
573:               MatCreateColmap_MPIBAIJ_Private(mat);
574:             }

576: #if defined(PETSC_USE_DEBUG)
577: #if defined(PETSC_USE_CTABLE)
578:             { PetscInt data;
579:               PetscTableFind(baij->colmap,in[j]+1,&data);
580:               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
581:             }
582: #else
583:             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
584: #endif
585: #endif
586: #if defined(PETSC_USE_CTABLE)
587:             PetscTableFind(baij->colmap,in[j]+1,&col);
588:             col  = (col - 1)/bs;
589: #else
590:             col = (baij->colmap[in[j]] - 1)/bs;
591: #endif
592:             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
593:               MatDisAssemble_MPISBAIJ(mat);
594:               col  = in[j];
595:             }
596:           } else col = in[j];
597:           MatSetValuesBlocked_SeqBAIJ_Inlined(baij->B,row,col,barray,addv,im[i],in[j]);
598:         }
599:       }
600:     } else {
601:       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]);
602:       if (!baij->donotstash) {
603:         if (roworiented) {
604:           MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
605:         } else {
606:           MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
607:         }
608:       }
609:     }
610:   }
611:   return(0);
612: }

614: PetscErrorCode MatGetValues_MPISBAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
615: {
616:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
618:   PetscInt       bs       = mat->rmap->bs,i,j,bsrstart = mat->rmap->rstart,bsrend = mat->rmap->rend;
619:   PetscInt       bscstart = mat->cmap->rstart,bscend = mat->cmap->rend,row,col,data;

622:   for (i=0; i<m; i++) {
623:     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]); */
624:     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);
625:     if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
626:       row = idxm[i] - bsrstart;
627:       for (j=0; j<n; j++) {
628:         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column %D",idxn[j]); */
629:         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);
630:         if (idxn[j] >= bscstart && idxn[j] < bscend) {
631:           col  = idxn[j] - bscstart;
632:           MatGetValues_SeqSBAIJ(baij->A,1,&row,1,&col,v+i*n+j);
633:         } else {
634:           if (!baij->colmap) {
635:             MatCreateColmap_MPIBAIJ_Private(mat);
636:           }
637: #if defined(PETSC_USE_CTABLE)
638:           PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);
639:           data--;
640: #else
641:           data = baij->colmap[idxn[j]/bs]-1;
642: #endif
643:           if ((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
644:           else {
645:             col  = data + idxn[j]%bs;
646:             MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);
647:           }
648:         }
649:       }
650:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
651:   }
652:   return(0);
653: }

655: PetscErrorCode MatNorm_MPISBAIJ(Mat mat,NormType type,PetscReal *norm)
656: {
657:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
659:   PetscReal      sum[2],*lnorm2;

662:   if (baij->size == 1) {
663:      MatNorm(baij->A,type,norm);
664:   } else {
665:     if (type == NORM_FROBENIUS) {
666:       PetscMalloc1(2,&lnorm2);
667:        MatNorm(baij->A,type,lnorm2);
668:       *lnorm2 = (*lnorm2)*(*lnorm2); lnorm2++;            /* squar power of norm(A) */
669:        MatNorm(baij->B,type,lnorm2);
670:       *lnorm2 = (*lnorm2)*(*lnorm2); lnorm2--;             /* squar power of norm(B) */
671:       MPIU_Allreduce(lnorm2,sum,2,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
672:       *norm   = PetscSqrtReal(sum[0] + 2*sum[1]);
673:       PetscFree(lnorm2);
674:     } else if (type == NORM_INFINITY || type == NORM_1) { /* max row/column sum */
675:       Mat_SeqSBAIJ *amat=(Mat_SeqSBAIJ*)baij->A->data;
676:       Mat_SeqBAIJ  *bmat=(Mat_SeqBAIJ*)baij->B->data;
677:       PetscReal    *rsum,*rsum2,vabs;
678:       PetscInt     *jj,*garray=baij->garray,rstart=baij->rstartbs,nz;
679:       PetscInt     brow,bcol,col,bs=baij->A->rmap->bs,row,grow,gcol,mbs=amat->mbs;
680:       MatScalar    *v;

682:       PetscMalloc2(mat->cmap->N,&rsum,mat->cmap->N,&rsum2);
683:       PetscArrayzero(rsum,mat->cmap->N);
684:       /* Amat */
685:       v = amat->a; jj = amat->j;
686:       for (brow=0; brow<mbs; brow++) {
687:         grow = bs*(rstart + brow);
688:         nz   = amat->i[brow+1] - amat->i[brow];
689:         for (bcol=0; bcol<nz; bcol++) {
690:           gcol = bs*(rstart + *jj); jj++;
691:           for (col=0; col<bs; col++) {
692:             for (row=0; row<bs; row++) {
693:               vabs            = PetscAbsScalar(*v); v++;
694:               rsum[gcol+col] += vabs;
695:               /* non-diagonal block */
696:               if (bcol > 0 && vabs > 0.0) rsum[grow+row] += vabs;
697:             }
698:           }
699:         }
700:         PetscLogFlops(nz*bs*bs);
701:       }
702:       /* Bmat */
703:       v = bmat->a; jj = bmat->j;
704:       for (brow=0; brow<mbs; brow++) {
705:         grow = bs*(rstart + brow);
706:         nz = bmat->i[brow+1] - bmat->i[brow];
707:         for (bcol=0; bcol<nz; bcol++) {
708:           gcol = bs*garray[*jj]; jj++;
709:           for (col=0; col<bs; col++) {
710:             for (row=0; row<bs; row++) {
711:               vabs            = PetscAbsScalar(*v); v++;
712:               rsum[gcol+col] += vabs;
713:               rsum[grow+row] += vabs;
714:             }
715:           }
716:         }
717:         PetscLogFlops(nz*bs*bs);
718:       }
719:       MPIU_Allreduce(rsum,rsum2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
720:       *norm = 0.0;
721:       for (col=0; col<mat->cmap->N; col++) {
722:         if (rsum2[col] > *norm) *norm = rsum2[col];
723:       }
724:       PetscFree2(rsum,rsum2);
725:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for this norm yet");
726:   }
727:   return(0);
728: }

730: PetscErrorCode MatAssemblyBegin_MPISBAIJ(Mat mat,MatAssemblyType mode)
731: {
732:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
734:   PetscInt       nstash,reallocs;

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

739:   MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
740:   MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);
741:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
742:   PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);
743:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
744:   PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
745:   return(0);
746: }

748: PetscErrorCode MatAssemblyEnd_MPISBAIJ(Mat mat,MatAssemblyType mode)
749: {
750:   Mat_MPISBAIJ   *baij=(Mat_MPISBAIJ*)mat->data;
751:   Mat_SeqSBAIJ   *a   =(Mat_SeqSBAIJ*)baij->A->data;
753:   PetscInt       i,j,rstart,ncols,flg,bs2=baij->bs2;
754:   PetscInt       *row,*col;
755:   PetscBool      other_disassembled;
756:   PetscMPIInt    n;
757:   PetscBool      r1,r2,r3;
758:   MatScalar      *val;

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

767:       for (i=0; i<n;) {
768:         /* Now identify the consecutive vals belonging to the same row */
769:         for (j=i,rstart=row[j]; j<n; j++) {
770:           if (row[j] != rstart) break;
771:         }
772:         if (j < n) ncols = j-i;
773:         else       ncols = n-i;
774:         /* Now assemble all these values with a single function call */
775:         MatSetValues_MPISBAIJ(mat,1,row+i,ncols,col+i,val+i,mat->insertmode);
776:         i    = j;
777:       }
778:     }
779:     MatStashScatterEnd_Private(&mat->stash);
780:     /* Now process the block-stash. Since the values are stashed column-oriented,
781:        set the roworiented flag to column oriented, and after MatSetValues()
782:        restore the original flags */
783:     r1 = baij->roworiented;
784:     r2 = a->roworiented;
785:     r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented;

787:     baij->roworiented = PETSC_FALSE;
788:     a->roworiented    = PETSC_FALSE;

790:     ((Mat_SeqBAIJ*)baij->B->data)->roworiented = PETSC_FALSE; /* b->roworinted */
791:     while (1) {
792:       MatStashScatterGetMesg_Private(&mat->bstash,&n,&row,&col,&val,&flg);
793:       if (!flg) break;

795:       for (i=0; i<n;) {
796:         /* Now identify the consecutive vals belonging to the same row */
797:         for (j=i,rstart=row[j]; j<n; j++) {
798:           if (row[j] != rstart) break;
799:         }
800:         if (j < n) ncols = j-i;
801:         else       ncols = n-i;
802:         MatSetValuesBlocked_MPISBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,mat->insertmode);
803:         i    = j;
804:       }
805:     }
806:     MatStashScatterEnd_Private(&mat->bstash);

808:     baij->roworiented = r1;
809:     a->roworiented    = r2;

811:     ((Mat_SeqBAIJ*)baij->B->data)->roworiented = r3; /* b->roworinted */
812:   }

814:   MatAssemblyBegin(baij->A,mode);
815:   MatAssemblyEnd(baij->A,mode);

817:   /* determine if any processor has disassembled, if so we must
818:      also disassemble ourselfs, in order that we may reassemble. */
819:   /*
820:      if nonzero structure of submatrix B cannot change then we know that
821:      no processor disassembled thus we can skip this stuff
822:   */
823:   if (!((Mat_SeqBAIJ*)baij->B->data)->nonew) {
824:     MPIU_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));
825:     if (mat->was_assembled && !other_disassembled) {
826:       MatDisAssemble_MPISBAIJ(mat);
827:     }
828:   }

830:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
831:     MatSetUpMultiply_MPISBAIJ(mat); /* setup Mvctx and sMvctx */
832:   }
833:   MatAssemblyBegin(baij->B,mode);
834:   MatAssemblyEnd(baij->B,mode);

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

838:   baij->rowvalues = 0;

840:   /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
841:   if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
842:     PetscObjectState state = baij->A->nonzerostate + baij->B->nonzerostate;
843:     MPIU_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));
844:   }
845:   return(0);
846: }

848: extern PetscErrorCode MatSetValues_MPIBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
849:  #include <petscdraw.h>
850: static PetscErrorCode MatView_MPISBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
851: {
852:   Mat_MPISBAIJ      *baij = (Mat_MPISBAIJ*)mat->data;
853:   PetscErrorCode    ierr;
854:   PetscInt          bs   = mat->rmap->bs;
855:   PetscMPIInt       rank = baij->rank;
856:   PetscBool         iascii,isdraw;
857:   PetscViewer       sviewer;
858:   PetscViewerFormat format;

861:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
862:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
863:   if (iascii) {
864:     PetscViewerGetFormat(viewer,&format);
865:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
866:       MatInfo info;
867:       MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
868:       MatGetInfo(mat,MAT_LOCAL,&info);
869:       PetscViewerASCIIPushSynchronized(viewer);
870:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D bs %D mem %g\n",rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,mat->rmap->bs,(double)info.memory);
871:       MatGetInfo(baij->A,MAT_LOCAL,&info);
872:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
873:       MatGetInfo(baij->B,MAT_LOCAL,&info);
874:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
875:       PetscViewerFlush(viewer);
876:       PetscViewerASCIIPopSynchronized(viewer);
877:       PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");
878:       VecScatterView(baij->Mvctx,viewer);
879:       return(0);
880:     } else if (format == PETSC_VIEWER_ASCII_INFO) {
881:       PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);
882:       return(0);
883:     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
884:       return(0);
885:     }
886:   }

888:   if (isdraw) {
889:     PetscDraw draw;
890:     PetscBool isnull;
891:     PetscViewerDrawGetDraw(viewer,0,&draw);
892:     PetscDrawIsNull(draw,&isnull);
893:     if (isnull) return(0);
894:   }

896:   {
897:     /* assemble the entire matrix onto first processor. */
898:     Mat          A;
899:     Mat_SeqSBAIJ *Aloc;
900:     Mat_SeqBAIJ  *Bloc;
901:     PetscInt     M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
902:     MatScalar    *a;
903:     const char   *matname;

905:     /* Should this be the same type as mat? */
906:     MatCreate(PetscObjectComm((PetscObject)mat),&A);
907:     if (!rank) {
908:       MatSetSizes(A,M,N,M,N);
909:     } else {
910:       MatSetSizes(A,0,0,M,N);
911:     }
912:     MatSetType(A,MATMPISBAIJ);
913:     MatMPISBAIJSetPreallocation(A,mat->rmap->bs,0,NULL,0,NULL);
914:     MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
915:     PetscLogObjectParent((PetscObject)mat,(PetscObject)A);

917:     /* copy over the A part */
918:     Aloc = (Mat_SeqSBAIJ*)baij->A->data;
919:     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
920:     PetscMalloc1(bs,&rvals);

922:     for (i=0; i<mbs; i++) {
923:       rvals[0] = bs*(baij->rstartbs + i);
924:       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
925:       for (j=ai[i]; j<ai[i+1]; j++) {
926:         col = (baij->cstartbs+aj[j])*bs;
927:         for (k=0; k<bs; k++) {
928:           MatSetValues_MPISBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
929:           col++;
930:           a += bs;
931:         }
932:       }
933:     }
934:     /* copy over the B part */
935:     Bloc = (Mat_SeqBAIJ*)baij->B->data;
936:     ai   = Bloc->i; aj = Bloc->j; a = Bloc->a;
937:     for (i=0; i<mbs; i++) {

939:       rvals[0] = bs*(baij->rstartbs + i);
940:       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
941:       for (j=ai[i]; j<ai[i+1]; j++) {
942:         col = baij->garray[aj[j]]*bs;
943:         for (k=0; k<bs; k++) {
944:           MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
945:           col++;
946:           a += bs;
947:         }
948:       }
949:     }
950:     PetscFree(rvals);
951:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
952:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
953:     /*
954:        Everyone has to call to draw the matrix since the graphics waits are
955:        synchronized across all processors that share the PetscDraw object
956:     */
957:     PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
958:     PetscObjectGetName((PetscObject)mat,&matname);
959:     if (!rank) {
960:       PetscObjectSetName((PetscObject)((Mat_MPISBAIJ*)(A->data))->A,matname);
961:       MatView_SeqSBAIJ(((Mat_MPISBAIJ*)(A->data))->A,sviewer);
962:     }
963:     PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
964:     PetscViewerFlush(viewer);
965:     MatDestroy(&A);
966:   }
967:   return(0);
968: }

970: static PetscErrorCode MatView_MPISBAIJ_Binary(Mat mat,PetscViewer viewer)
971: {
972:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)mat->data;
973:   Mat_SeqSBAIJ   *A = (Mat_SeqSBAIJ*)a->A->data;
974:   Mat_SeqBAIJ    *B = (Mat_SeqBAIJ*)a->B->data;
976:   PetscInt       i,*row_lens,*crow_lens,bs = mat->rmap->bs,j,k,bs2=a->bs2,header[4],nz,rlen;
977:   PetscInt       *range=0,nzmax,*column_indices,cnt,col,*garray = a->garray,cstart = mat->cmap->rstart/bs,len,pcnt,l,ll;
978:   int            fd;
979:   PetscScalar    *column_values;
980:   FILE           *file;
981:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
982:   PetscInt       message_count,flowcontrolcount;

985:   MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
986:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
987:   nz   = bs2*(A->nz + B->nz);
988:   rlen = mat->rmap->n;
989:   PetscViewerBinaryGetDescriptor(viewer,&fd);
990:   if (!rank) {
991:     header[0] = MAT_FILE_CLASSID;
992:     header[1] = mat->rmap->N;
993:     header[2] = mat->cmap->N;

995:     MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
996:     PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);
997:     /* get largest number of rows any processor has */
998:     range = mat->rmap->range;
999:     for (i=1; i<size; i++) {
1000:       rlen = PetscMax(rlen,range[i+1] - range[i]);
1001:     }
1002:   } else {
1003:     MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1004:   }

1006:   PetscMalloc1(rlen/bs,&crow_lens);
1007:   /* compute lengths of each row  */
1008:   for (i=0; i<a->mbs; i++) {
1009:     crow_lens[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
1010:   }
1011:   /* store the row lengths to the file */
1012:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1013:   if (!rank) {
1014:     MPI_Status status;
1015:     PetscMalloc1(rlen,&row_lens);
1016:     rlen = (range[1] - range[0])/bs;
1017:     for (i=0; i<rlen; i++) {
1018:       for (j=0; j<bs; j++) {
1019:         row_lens[i*bs+j] = bs*crow_lens[i];
1020:       }
1021:     }
1022:     PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);
1023:     for (i=1; i<size; i++) {
1024:       rlen = (range[i+1] - range[i])/bs;
1025:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1026:       MPI_Recv(crow_lens,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1027:       for (k=0; k<rlen; k++) {
1028:         for (j=0; j<bs; j++) {
1029:           row_lens[k*bs+j] = bs*crow_lens[k];
1030:         }
1031:       }
1032:       PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);
1033:     }
1034:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1035:     PetscFree(row_lens);
1036:   } else {
1037:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1038:     MPI_Send(crow_lens,mat->rmap->n/bs,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1039:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1040:   }
1041:   PetscFree(crow_lens);

1043:   /* load up the local column indices. Include for all rows not just one for each block row since process 0 does not have the
1044:      information needed to make it for each row from a block row. This does require more communication but still not more than
1045:      the communication needed for the nonzero values  */
1046:   nzmax = nz; /*  space a largest processor needs */
1047:   MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));
1048:   PetscMalloc1(nzmax,&column_indices);
1049:   cnt   = 0;
1050:   for (i=0; i<a->mbs; i++) {
1051:     pcnt = cnt;
1052:     for (j=B->i[i]; j<B->i[i+1]; j++) {
1053:       if ((col = garray[B->j[j]]) > cstart) break;
1054:       for (l=0; l<bs; l++) {
1055:         column_indices[cnt++] = bs*col+l;
1056:       }
1057:     }
1058:     for (k=A->i[i]; k<A->i[i+1]; k++) {
1059:       for (l=0; l<bs; l++) {
1060:         column_indices[cnt++] = bs*(A->j[k] + cstart)+l;
1061:       }
1062:     }
1063:     for (; j<B->i[i+1]; j++) {
1064:       for (l=0; l<bs; l++) {
1065:         column_indices[cnt++] = bs*garray[B->j[j]]+l;
1066:       }
1067:     }
1068:     len = cnt - pcnt;
1069:     for (k=1; k<bs; k++) {
1070:       PetscArraycpy(&column_indices[cnt],&column_indices[pcnt],len);
1071:       cnt += len;
1072:     }
1073:   }
1074:   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);

1076:   /* store the columns to the file */
1077:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1078:   if (!rank) {
1079:     MPI_Status status;
1080:     PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);
1081:     for (i=1; i<size; i++) {
1082:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1083:       MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1084:       MPI_Recv(column_indices,cnt,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1085:       PetscBinaryWrite(fd,column_indices,cnt,PETSC_INT,PETSC_TRUE);
1086:     }
1087:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1088:   } else {
1089:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1090:     MPI_Send(&cnt,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1091:     MPI_Send(column_indices,cnt,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1092:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1093:   }
1094:   PetscFree(column_indices);

1096:   /* load up the numerical values */
1097:   PetscMalloc1(nzmax,&column_values);
1098:   cnt  = 0;
1099:   for (i=0; i<a->mbs; i++) {
1100:     rlen = bs*(B->i[i+1] - B->i[i] + A->i[i+1] - A->i[i]);
1101:     for (j=B->i[i]; j<B->i[i+1]; j++) {
1102:       if (garray[B->j[j]] > cstart) break;
1103:       for (l=0; l<bs; l++) {
1104:         for (ll=0; ll<bs; ll++) {
1105:           column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1106:         }
1107:       }
1108:       cnt += bs;
1109:     }
1110:     for (k=A->i[i]; k<A->i[i+1]; k++) {
1111:       for (l=0; l<bs; l++) {
1112:         for (ll=0; ll<bs; ll++) {
1113:           column_values[cnt + l*rlen + ll] = A->a[bs2*k+l+bs*ll];
1114:         }
1115:       }
1116:       cnt += bs;
1117:     }
1118:     for (; j<B->i[i+1]; j++) {
1119:       for (l=0; l<bs; l++) {
1120:         for (ll=0; ll<bs; ll++) {
1121:           column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1122:         }
1123:       }
1124:       cnt += bs;
1125:     }
1126:     cnt += (bs-1)*rlen;
1127:   }
1128:   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);

1130:   /* store the column values to the file */
1131:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1132:   if (!rank) {
1133:     MPI_Status status;
1134:     PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);
1135:     for (i=1; i<size; i++) {
1136:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1137:       MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1138:       MPI_Recv(column_values,cnt,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat),&status);
1139:       PetscBinaryWrite(fd,column_values,cnt,PETSC_SCALAR,PETSC_TRUE);
1140:     }
1141:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1142:   } else {
1143:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1144:     MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1145:     MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));
1146:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1147:   }
1148:   PetscFree(column_values);

1150:   PetscViewerBinaryGetInfoPointer(viewer,&file);
1151:   if (file) {
1152:     fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs);
1153:   }
1154:   return(0);
1155: }

1157: PetscErrorCode MatView_MPISBAIJ(Mat mat,PetscViewer viewer)
1158: {
1160:   PetscBool      iascii,isdraw,issocket,isbinary;

1163:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1164:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1165:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
1166:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1167:   if (iascii || isdraw || issocket) {
1168:     MatView_MPISBAIJ_ASCIIorDraworSocket(mat,viewer);
1169:   } else if (isbinary) {
1170:     MatView_MPISBAIJ_Binary(mat,viewer);
1171:   }
1172:   return(0);
1173: }

1175: PetscErrorCode MatDestroy_MPISBAIJ(Mat mat)
1176: {
1177:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;

1181: #if defined(PETSC_USE_LOG)
1182:   PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N);
1183: #endif
1184:   MatStashDestroy_Private(&mat->stash);
1185:   MatStashDestroy_Private(&mat->bstash);
1186:   MatDestroy(&baij->A);
1187:   MatDestroy(&baij->B);
1188: #if defined(PETSC_USE_CTABLE)
1189:   PetscTableDestroy(&baij->colmap);
1190: #else
1191:   PetscFree(baij->colmap);
1192: #endif
1193:   PetscFree(baij->garray);
1194:   VecDestroy(&baij->lvec);
1195:   VecScatterDestroy(&baij->Mvctx);
1196:   VecDestroy(&baij->slvec0);
1197:   VecDestroy(&baij->slvec0b);
1198:   VecDestroy(&baij->slvec1);
1199:   VecDestroy(&baij->slvec1a);
1200:   VecDestroy(&baij->slvec1b);
1201:   VecScatterDestroy(&baij->sMvctx);
1202:   PetscFree2(baij->rowvalues,baij->rowindices);
1203:   PetscFree(baij->barray);
1204:   PetscFree(baij->hd);
1205:   VecDestroy(&baij->diag);
1206:   VecDestroy(&baij->bb1);
1207:   VecDestroy(&baij->xx1);
1208: #if defined(PETSC_USE_REAL_MAT_SINGLE)
1209:   PetscFree(baij->setvaluescopy);
1210: #endif
1211:   PetscFree(baij->in_loc);
1212:   PetscFree(baij->v_loc);
1213:   PetscFree(baij->rangebs);
1214:   PetscFree(mat->data);

1216:   PetscObjectChangeTypeName((PetscObject)mat,0);
1217:   PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);
1218:   PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);
1219:   PetscObjectComposeFunction((PetscObject)mat,"MatMPISBAIJSetPreallocation_C",NULL);
1220:   PetscObjectComposeFunction((PetscObject)mat,"MatMPISBAIJSetPreallocationCSR_C",NULL);
1221: #if defined(PETSC_HAVE_ELEMENTAL)
1222:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpisbaij_elemental_C",NULL);
1223: #endif
1224:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpisbaij_mpiaij_C",NULL);
1225:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpisbaij_mpibaij_C",NULL);
1226:   return(0);
1227: }

1229: PetscErrorCode MatMult_MPISBAIJ_Hermitian(Mat A,Vec xx,Vec yy)
1230: {
1231:   Mat_MPISBAIJ      *a = (Mat_MPISBAIJ*)A->data;
1232:   PetscErrorCode    ierr;
1233:   PetscInt          nt,mbs=a->mbs,bs=A->rmap->bs;
1234:   PetscScalar       *from;
1235:   const PetscScalar *x;

1238:   VecGetLocalSize(xx,&nt);
1239:   if (nt != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");

1241:   /* diagonal part */
1242:   (*a->A->ops->mult)(a->A,xx,a->slvec1a);
1243:   VecSet(a->slvec1b,0.0);

1245:   /* subdiagonal part */
1246:   (*a->B->ops->multhermitiantranspose)(a->B,xx,a->slvec0b);

1248:   /* copy x into the vec slvec0 */
1249:   VecGetArray(a->slvec0,&from);
1250:   VecGetArrayRead(xx,&x);

1252:   PetscArraycpy(from,x,bs*mbs);
1253:   VecRestoreArray(a->slvec0,&from);
1254:   VecRestoreArrayRead(xx,&x);

1256:   VecScatterBegin(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
1257:   VecScatterEnd(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
1258:   /* supperdiagonal part */
1259:   (*a->B->ops->multadd)(a->B,a->slvec1b,a->slvec1a,yy);
1260:   return(0);
1261: }

1263: PetscErrorCode MatMult_MPISBAIJ(Mat A,Vec xx,Vec yy)
1264: {
1265:   Mat_MPISBAIJ      *a = (Mat_MPISBAIJ*)A->data;
1266:   PetscErrorCode    ierr;
1267:   PetscInt          nt,mbs=a->mbs,bs=A->rmap->bs;
1268:   PetscScalar       *from;
1269:   const PetscScalar *x;

1272:   VecGetLocalSize(xx,&nt);
1273:   if (nt != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");

1275:   /* diagonal part */
1276:   (*a->A->ops->mult)(a->A,xx,a->slvec1a);
1277:   VecSet(a->slvec1b,0.0);

1279:   /* subdiagonal part */
1280:   (*a->B->ops->multtranspose)(a->B,xx,a->slvec0b);

1282:   /* copy x into the vec slvec0 */
1283:   VecGetArray(a->slvec0,&from);
1284:   VecGetArrayRead(xx,&x);

1286:   PetscArraycpy(from,x,bs*mbs);
1287:   VecRestoreArray(a->slvec0,&from);
1288:   VecRestoreArrayRead(xx,&x);

1290:   VecScatterBegin(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
1291:   VecScatterEnd(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
1292:   /* supperdiagonal part */
1293:   (*a->B->ops->multadd)(a->B,a->slvec1b,a->slvec1a,yy);
1294:   return(0);
1295: }

1297: PetscErrorCode MatMult_MPISBAIJ_2comm(Mat A,Vec xx,Vec yy)
1298: {
1299:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;
1301:   PetscInt       nt;

1304:   VecGetLocalSize(xx,&nt);
1305:   if (nt != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");

1307:   VecGetLocalSize(yy,&nt);
1308:   if (nt != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");

1310:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1311:   /* do diagonal part */
1312:   (*a->A->ops->mult)(a->A,xx,yy);
1313:   /* do supperdiagonal part */
1314:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1315:   (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
1316:   /* do subdiagonal part */
1317:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1318:   VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1319:   VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1320:   return(0);
1321: }

1323: PetscErrorCode MatMultAdd_MPISBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1324: {
1325:   Mat_MPISBAIJ      *a = (Mat_MPISBAIJ*)A->data;
1326:   PetscErrorCode    ierr;
1327:   PetscInt          mbs=a->mbs,bs=A->rmap->bs;
1328:   PetscScalar       *from,zero=0.0;
1329:   const PetscScalar *x;

1332:   /*
1333:   PetscSynchronizedPrintf(PetscObjectComm((PetscObject)A)," MatMultAdd is called ...\n");
1334:   PetscSynchronizedFlush(PetscObjectComm((PetscObject)A),PETSC_STDOUT);
1335:   */
1336:   /* diagonal part */
1337:   (*a->A->ops->multadd)(a->A,xx,yy,a->slvec1a);
1338:   VecSet(a->slvec1b,zero);

1340:   /* subdiagonal part */
1341:   (*a->B->ops->multtranspose)(a->B,xx,a->slvec0b);

1343:   /* copy x into the vec slvec0 */
1344:   VecGetArray(a->slvec0,&from);
1345:   VecGetArrayRead(xx,&x);
1346:   PetscArraycpy(from,x,bs*mbs);
1347:   VecRestoreArray(a->slvec0,&from);

1349:   VecScatterBegin(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
1350:   VecRestoreArrayRead(xx,&x);
1351:   VecScatterEnd(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);

1353:   /* supperdiagonal part */
1354:   (*a->B->ops->multadd)(a->B,a->slvec1b,a->slvec1a,zz);
1355:   return(0);
1356: }

1358: PetscErrorCode MatMultAdd_MPISBAIJ_2comm(Mat A,Vec xx,Vec yy,Vec zz)
1359: {
1360:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1364:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1365:   /* do diagonal part */
1366:   (*a->A->ops->multadd)(a->A,xx,yy,zz);
1367:   /* do supperdiagonal part */
1368:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1369:   (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);

1371:   /* do subdiagonal part */
1372:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1373:   VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1374:   VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1375:   return(0);
1376: }

1378: /*
1379:   This only works correctly for square matrices where the subblock A->A is the
1380:    diagonal block
1381: */
1382: PetscErrorCode MatGetDiagonal_MPISBAIJ(Mat A,Vec v)
1383: {
1384:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1388:   /* if (a->rmap->N != a->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block"); */
1389:   MatGetDiagonal(a->A,v);
1390:   return(0);
1391: }

1393: PetscErrorCode MatScale_MPISBAIJ(Mat A,PetscScalar aa)
1394: {
1395:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1399:   MatScale(a->A,aa);
1400:   MatScale(a->B,aa);
1401:   return(0);
1402: }

1404: PetscErrorCode MatGetRow_MPISBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1405: {
1406:   Mat_MPISBAIJ   *mat = (Mat_MPISBAIJ*)matin->data;
1407:   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
1409:   PetscInt       bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB;
1410:   PetscInt       nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend;
1411:   PetscInt       *cmap,*idx_p,cstart = mat->rstartbs;

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

1417:   if (!mat->rowvalues && (idx || v)) {
1418:     /*
1419:         allocate enough space to hold information from the longest row.
1420:     */
1421:     Mat_SeqSBAIJ *Aa = (Mat_SeqSBAIJ*)mat->A->data;
1422:     Mat_SeqBAIJ  *Ba = (Mat_SeqBAIJ*)mat->B->data;
1423:     PetscInt     max = 1,mbs = mat->mbs,tmp;
1424:     for (i=0; i<mbs; i++) {
1425:       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; /* row length */
1426:       if (max < tmp) max = tmp;
1427:     }
1428:     PetscMalloc2(max*bs2,&mat->rowvalues,max*bs2,&mat->rowindices);
1429:   }

1431:   if (row < brstart || row >= brend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local rows");
1432:   lrow = row - brstart;  /* local row index */

1434:   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1435:   if (!v)   {pvA = 0; pvB = 0;}
1436:   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1437:   (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1438:   (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1439:   nztot = nzA + nzB;

1441:   cmap = mat->garray;
1442:   if (v  || idx) {
1443:     if (nztot) {
1444:       /* Sort by increasing column numbers, assuming A and B already sorted */
1445:       PetscInt imark = -1;
1446:       if (v) {
1447:         *v = v_p = mat->rowvalues;
1448:         for (i=0; i<nzB; i++) {
1449:           if (cmap[cworkB[i]/bs] < cstart) v_p[i] = vworkB[i];
1450:           else break;
1451:         }
1452:         imark = i;
1453:         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1454:         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1455:       }
1456:       if (idx) {
1457:         *idx = idx_p = mat->rowindices;
1458:         if (imark > -1) {
1459:           for (i=0; i<imark; i++) {
1460:             idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1461:           }
1462:         } else {
1463:           for (i=0; i<nzB; i++) {
1464:             if (cmap[cworkB[i]/bs] < cstart) idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1465:             else break;
1466:           }
1467:           imark = i;
1468:         }
1469:         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart*bs + cworkA[i];
1470:         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1471:       }
1472:     } else {
1473:       if (idx) *idx = 0;
1474:       if (v)   *v   = 0;
1475:     }
1476:   }
1477:   *nz  = nztot;
1478:   (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1479:   (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1480:   return(0);
1481: }

1483: PetscErrorCode MatRestoreRow_MPISBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1484: {
1485:   Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)mat->data;

1488:   if (!baij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
1489:   baij->getrowactive = PETSC_FALSE;
1490:   return(0);
1491: }

1493: PetscErrorCode MatGetRowUpperTriangular_MPISBAIJ(Mat A)
1494: {
1495:   Mat_MPISBAIJ *a  = (Mat_MPISBAIJ*)A->data;
1496:   Mat_SeqSBAIJ *aA = (Mat_SeqSBAIJ*)a->A->data;

1499:   aA->getrow_utriangular = PETSC_TRUE;
1500:   return(0);
1501: }
1502: PetscErrorCode MatRestoreRowUpperTriangular_MPISBAIJ(Mat A)
1503: {
1504:   Mat_MPISBAIJ *a  = (Mat_MPISBAIJ*)A->data;
1505:   Mat_SeqSBAIJ *aA = (Mat_SeqSBAIJ*)a->A->data;

1508:   aA->getrow_utriangular = PETSC_FALSE;
1509:   return(0);
1510: }

1512: PetscErrorCode MatRealPart_MPISBAIJ(Mat A)
1513: {
1514:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1518:   MatRealPart(a->A);
1519:   MatRealPart(a->B);
1520:   return(0);
1521: }

1523: PetscErrorCode MatImaginaryPart_MPISBAIJ(Mat A)
1524: {
1525:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1529:   MatImaginaryPart(a->A);
1530:   MatImaginaryPart(a->B);
1531:   return(0);
1532: }

1534: /* Check if isrow is a subset of iscol_local, called by MatCreateSubMatrix_MPISBAIJ()
1535:    Input: isrow       - distributed(parallel),
1536:           iscol_local - locally owned (seq)
1537: */
1538: PetscErrorCode ISEqual_private(IS isrow,IS iscol_local,PetscBool  *flg)
1539: {
1541:   PetscInt       sz1,sz2,*a1,*a2,i,j,k,nmatch;
1542:   const PetscInt *ptr1,*ptr2;

1545:   ISGetLocalSize(isrow,&sz1);
1546:   ISGetLocalSize(iscol_local,&sz2);
1547:   if (sz1 > sz2) {
1548:     *flg = PETSC_FALSE;
1549:     return(0);
1550:   }

1552:   ISGetIndices(isrow,&ptr1);
1553:   ISGetIndices(iscol_local,&ptr2);

1555:   PetscMalloc1(sz1,&a1);
1556:   PetscMalloc1(sz2,&a2);
1557:   PetscArraycpy(a1,ptr1,sz1);
1558:   PetscArraycpy(a2,ptr2,sz2);
1559:   PetscSortInt(sz1,a1);
1560:   PetscSortInt(sz2,a2);

1562:   nmatch=0;
1563:   k     = 0;
1564:   for (i=0; i<sz1; i++){
1565:     for (j=k; j<sz2; j++){
1566:       if (a1[i] == a2[j]) {
1567:         k = j; nmatch++;
1568:         break;
1569:       }
1570:     }
1571:   }
1572:   ISRestoreIndices(isrow,&ptr1);
1573:   ISRestoreIndices(iscol_local,&ptr2);
1574:   PetscFree(a1);
1575:   PetscFree(a2);
1576:   if (nmatch < sz1) {
1577:     *flg = PETSC_FALSE;
1578:   } else {
1579:     *flg = PETSC_TRUE;
1580:   }
1581:   return(0);
1582: }

1584: PetscErrorCode MatCreateSubMatrix_MPISBAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
1585: {
1587:   IS             iscol_local;
1588:   PetscInt       csize;
1589:   PetscBool      isequal;

1592:   ISGetLocalSize(iscol,&csize);
1593:   if (call == MAT_REUSE_MATRIX) {
1594:     PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);
1595:     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
1596:   } else {
1597:     ISAllGather(iscol,&iscol_local);
1598:     ISEqual_private(isrow,iscol_local,&isequal);
1599:     if (!isequal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"For symmetric format, iscol must equal isrow");
1600:   }

1602:   /* now call MatCreateSubMatrix_MPIBAIJ() */
1603:   MatCreateSubMatrix_MPIBAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);
1604:   if (call == MAT_INITIAL_MATRIX) {
1605:     PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);
1606:     ISDestroy(&iscol_local);
1607:   }
1608:   return(0);
1609: }

1611: PetscErrorCode MatZeroEntries_MPISBAIJ(Mat A)
1612: {
1613:   Mat_MPISBAIJ   *l = (Mat_MPISBAIJ*)A->data;

1617:   MatZeroEntries(l->A);
1618:   MatZeroEntries(l->B);
1619:   return(0);
1620: }

1622: PetscErrorCode MatGetInfo_MPISBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1623: {
1624:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)matin->data;
1625:   Mat            A  = a->A,B = a->B;
1627:   PetscReal      isend[5],irecv[5];

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

1632:   MatGetInfo(A,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:   MatGetInfo(B,MAT_LOCAL,info);

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

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

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

1670: PetscErrorCode MatSetOption_MPISBAIJ(Mat A,MatOption op,PetscBool flg)
1671: {
1672:   Mat_MPISBAIJ   *a  = (Mat_MPISBAIJ*)A->data;
1673:   Mat_SeqSBAIJ   *aA = (Mat_SeqSBAIJ*)a->A->data;

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

1692:     MatSetOption(a->A,op,flg);
1693:     MatSetOption(a->B,op,flg);
1694:     break;
1695:   case MAT_NEW_DIAGONALS:
1696:   case MAT_SORTED_FULL:
1697:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1698:     break;
1699:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1700:     a->donotstash = flg;
1701:     break;
1702:   case MAT_USE_HASH_TABLE:
1703:     a->ht_flag = flg;
1704:     break;
1705:   case MAT_HERMITIAN:
1706:     MatCheckPreallocated(A,1);
1707:     if (!A->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatAssemblyEnd() first");
1708:     MatSetOption(a->A,op,flg);
1709: #if defined(PETSC_USE_COMPLEX)
1710:     A->ops->mult = MatMult_MPISBAIJ_Hermitian;
1711: #endif
1712:     break;
1713:   case MAT_SPD:
1714:     A->spd_set = PETSC_TRUE;
1715:     A->spd     = flg;
1716:     if (flg) {
1717:       A->symmetric                  = PETSC_TRUE;
1718:       A->structurally_symmetric     = PETSC_TRUE;
1719:       A->symmetric_set              = PETSC_TRUE;
1720:       A->structurally_symmetric_set = PETSC_TRUE;
1721:     }
1722:     break;
1723:   case MAT_SYMMETRIC:
1724:     MatCheckPreallocated(A,1);
1725:     MatSetOption(a->A,op,flg);
1726:     break;
1727:   case MAT_STRUCTURALLY_SYMMETRIC:
1728:     MatCheckPreallocated(A,1);
1729:     MatSetOption(a->A,op,flg);
1730:     break;
1731:   case MAT_SYMMETRY_ETERNAL:
1732:     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix must be symmetric");
1733:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1734:     break;
1735:   case MAT_IGNORE_LOWER_TRIANGULAR:
1736:     aA->ignore_ltriangular = flg;
1737:     break;
1738:   case MAT_ERROR_LOWER_TRIANGULAR:
1739:     aA->ignore_ltriangular = flg;
1740:     break;
1741:   case MAT_GETROW_UPPERTRIANGULAR:
1742:     aA->getrow_utriangular = flg;
1743:     break;
1744:   default:
1745:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1746:   }
1747:   return(0);
1748: }

1750: PetscErrorCode MatTranspose_MPISBAIJ(Mat A,MatReuse reuse,Mat *B)
1751: {

1755:   if (reuse == MAT_INITIAL_MATRIX) {
1756:     MatDuplicate(A,MAT_COPY_VALUES,B);
1757:   }  else if (reuse == MAT_REUSE_MATRIX) {
1758:     MatCopy(A,*B,SAME_NONZERO_PATTERN);
1759:   }
1760:   return(0);
1761: }

1763: PetscErrorCode MatDiagonalScale_MPISBAIJ(Mat mat,Vec ll,Vec rr)
1764: {
1765:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
1766:   Mat            a     = baij->A, b=baij->B;
1768:   PetscInt       nv,m,n;
1769:   PetscBool      flg;

1772:   if (ll != rr) {
1773:     VecEqual(ll,rr,&flg);
1774:     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"For symmetric format, left and right scaling vectors must be same\n");
1775:   }
1776:   if (!ll) return(0);

1778:   MatGetLocalSize(mat,&m,&n);
1779:   if (m != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"For symmetric format, local size %d %d must be same",m,n);

1781:   VecGetLocalSize(rr,&nv);
1782:   if (nv!=n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left and right vector non-conforming local size");

1784:   VecScatterBegin(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);

1786:   /* left diagonalscale the off-diagonal part */
1787:   (*b->ops->diagonalscale)(b,ll,NULL);

1789:   /* scale the diagonal part */
1790:   (*a->ops->diagonalscale)(a,ll,rr);

1792:   /* right diagonalscale the off-diagonal part */
1793:   VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);
1794:   (*b->ops->diagonalscale)(b,NULL,baij->lvec);
1795:   return(0);
1796: }

1798: PetscErrorCode MatSetUnfactored_MPISBAIJ(Mat A)
1799: {
1800:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1804:   MatSetUnfactored(a->A);
1805:   return(0);
1806: }

1808: static PetscErrorCode MatDuplicate_MPISBAIJ(Mat,MatDuplicateOption,Mat*);

1810: PetscErrorCode MatEqual_MPISBAIJ(Mat A,Mat B,PetscBool  *flag)
1811: {
1812:   Mat_MPISBAIJ   *matB = (Mat_MPISBAIJ*)B->data,*matA = (Mat_MPISBAIJ*)A->data;
1813:   Mat            a,b,c,d;
1814:   PetscBool      flg;

1818:   a = matA->A; b = matA->B;
1819:   c = matB->A; d = matB->B;

1821:   MatEqual(a,c,&flg);
1822:   if (flg) {
1823:     MatEqual(b,d,&flg);
1824:   }
1825:   MPIU_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
1826:   return(0);
1827: }

1829: PetscErrorCode MatCopy_MPISBAIJ(Mat A,Mat B,MatStructure str)
1830: {
1832:   PetscBool      isbaij;

1835:   PetscObjectTypeCompareAny((PetscObject)B,&isbaij,MATSEQSBAIJ,MATMPISBAIJ,"");
1836:   if (!isbaij) SETERRQ1(PetscObjectComm((PetscObject)B),PETSC_ERR_SUP,"Not for matrix type %s",((PetscObject)B)->type_name);
1837:   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1838:   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1839:     MatGetRowUpperTriangular(A);
1840:     MatCopy_Basic(A,B,str);
1841:     MatRestoreRowUpperTriangular(A);
1842:   } else {
1843:     Mat_MPISBAIJ *a = (Mat_MPISBAIJ*)A->data;
1844:     Mat_MPISBAIJ *b = (Mat_MPISBAIJ*)B->data;

1846:     MatCopy(a->A,b->A,str);
1847:     MatCopy(a->B,b->B,str);
1848:   }
1849:   PetscObjectStateIncrease((PetscObject)B);
1850:   return(0);
1851: }

1853: PetscErrorCode MatSetUp_MPISBAIJ(Mat A)
1854: {

1858:   MatMPISBAIJSetPreallocation(A,A->rmap->bs,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
1859:   return(0);
1860: }

1862: PetscErrorCode MatAXPY_MPISBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
1863: {
1865:   Mat_MPISBAIJ   *xx=(Mat_MPISBAIJ*)X->data,*yy=(Mat_MPISBAIJ*)Y->data;
1866:   PetscBLASInt   bnz,one=1;
1867:   Mat_SeqSBAIJ   *xa,*ya;
1868:   Mat_SeqBAIJ    *xb,*yb;

1871:   if (str == SAME_NONZERO_PATTERN) {
1872:     PetscScalar alpha = a;
1873:     xa   = (Mat_SeqSBAIJ*)xx->A->data;
1874:     ya   = (Mat_SeqSBAIJ*)yy->A->data;
1875:     PetscBLASIntCast(xa->nz,&bnz);
1876:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,xa->a,&one,ya->a,&one));
1877:     xb   = (Mat_SeqBAIJ*)xx->B->data;
1878:     yb   = (Mat_SeqBAIJ*)yy->B->data;
1879:     PetscBLASIntCast(xb->nz,&bnz);
1880:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,xb->a,&one,yb->a,&one));
1881:     PetscObjectStateIncrease((PetscObject)Y);
1882:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1883:     MatSetOption(X,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE);
1884:     MatAXPY_Basic(Y,a,X,str);
1885:     MatSetOption(X,MAT_GETROW_UPPERTRIANGULAR,PETSC_FALSE);
1886:   } else {
1887:     Mat      B;
1888:     PetscInt *nnz_d,*nnz_o,bs=Y->rmap->bs;
1889:     if (bs != X->rmap->bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrices must have same block size");
1890:     MatGetRowUpperTriangular(X);
1891:     MatGetRowUpperTriangular(Y);
1892:     PetscMalloc1(yy->A->rmap->N,&nnz_d);
1893:     PetscMalloc1(yy->B->rmap->N,&nnz_o);
1894:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
1895:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
1896:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
1897:     MatSetBlockSizesFromMats(B,Y,Y);
1898:     MatSetType(B,MATMPISBAIJ);
1899:     MatAXPYGetPreallocation_SeqSBAIJ(yy->A,xx->A,nnz_d);
1900:     MatAXPYGetPreallocation_MPIBAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);
1901:     MatMPISBAIJSetPreallocation(B,bs,0,nnz_d,0,nnz_o);
1902:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
1903:     MatHeaderReplace(Y,&B);
1904:     PetscFree(nnz_d);
1905:     PetscFree(nnz_o);
1906:     MatRestoreRowUpperTriangular(X);
1907:     MatRestoreRowUpperTriangular(Y);
1908:   }
1909:   return(0);
1910: }

1912: PetscErrorCode MatCreateSubMatrices_MPISBAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1913: {
1915:   PetscInt       i;
1916:   PetscBool      flg;

1919:   MatCreateSubMatrices_MPIBAIJ(A,n,irow,icol,scall,B); /* B[] are sbaij matrices */
1920:   for (i=0; i<n; i++) {
1921:     ISEqual(irow[i],icol[i],&flg);
1922:     if (!flg) {
1923:       MatSeqSBAIJZeroOps_Private(*B[i]);
1924:     }
1925:   }
1926:   return(0);
1927: }

1929: PetscErrorCode MatShift_MPISBAIJ(Mat Y,PetscScalar a)
1930: {
1932:   Mat_MPISBAIJ    *maij = (Mat_MPISBAIJ*)Y->data;
1933:   Mat_SeqSBAIJ    *aij = (Mat_SeqSBAIJ*)maij->A->data;

1936:   if (!Y->preallocated) {
1937:     MatMPISBAIJSetPreallocation(Y,Y->rmap->bs,1,NULL,0,NULL);
1938:   } else if (!aij->nz) {
1939:     PetscInt nonew = aij->nonew;
1940:     MatSeqSBAIJSetPreallocation(maij->A,Y->rmap->bs,1,NULL);
1941:     aij->nonew = nonew;
1942:   }
1943:   MatShift_Basic(Y,a);
1944:   return(0);
1945: }

1947: PetscErrorCode MatMissingDiagonal_MPISBAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1948: {
1949:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1953:   if (A->rmap->n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only works for square matrices");
1954:   MatMissingDiagonal(a->A,missing,d);
1955:   if (d) {
1956:     PetscInt rstart;
1957:     MatGetOwnershipRange(A,&rstart,NULL);
1958:     *d += rstart/A->rmap->bs;

1960:   }
1961:   return(0);
1962: }

1964: PetscErrorCode  MatGetDiagonalBlock_MPISBAIJ(Mat A,Mat *a)
1965: {
1967:   *a = ((Mat_MPISBAIJ*)A->data)->A;
1968:   return(0);
1969: }

1971: /* -------------------------------------------------------------------*/
1972: static struct _MatOps MatOps_Values = {MatSetValues_MPISBAIJ,
1973:                                        MatGetRow_MPISBAIJ,
1974:                                        MatRestoreRow_MPISBAIJ,
1975:                                        MatMult_MPISBAIJ,
1976:                                /*  4*/ MatMultAdd_MPISBAIJ,
1977:                                        MatMult_MPISBAIJ,       /* transpose versions are same as non-transpose */
1978:                                        MatMultAdd_MPISBAIJ,
1979:                                        0,
1980:                                        0,
1981:                                        0,
1982:                                /* 10*/ 0,
1983:                                        0,
1984:                                        0,
1985:                                        MatSOR_MPISBAIJ,
1986:                                        MatTranspose_MPISBAIJ,
1987:                                /* 15*/ MatGetInfo_MPISBAIJ,
1988:                                        MatEqual_MPISBAIJ,
1989:                                        MatGetDiagonal_MPISBAIJ,
1990:                                        MatDiagonalScale_MPISBAIJ,
1991:                                        MatNorm_MPISBAIJ,
1992:                                /* 20*/ MatAssemblyBegin_MPISBAIJ,
1993:                                        MatAssemblyEnd_MPISBAIJ,
1994:                                        MatSetOption_MPISBAIJ,
1995:                                        MatZeroEntries_MPISBAIJ,
1996:                                /* 24*/ 0,
1997:                                        0,
1998:                                        0,
1999:                                        0,
2000:                                        0,
2001:                                /* 29*/ MatSetUp_MPISBAIJ,
2002:                                        0,
2003:                                        0,
2004:                                        MatGetDiagonalBlock_MPISBAIJ,
2005:                                        0,
2006:                                /* 34*/ MatDuplicate_MPISBAIJ,
2007:                                        0,
2008:                                        0,
2009:                                        0,
2010:                                        0,
2011:                                /* 39*/ MatAXPY_MPISBAIJ,
2012:                                        MatCreateSubMatrices_MPISBAIJ,
2013:                                        MatIncreaseOverlap_MPISBAIJ,
2014:                                        MatGetValues_MPISBAIJ,
2015:                                        MatCopy_MPISBAIJ,
2016:                                /* 44*/ 0,
2017:                                        MatScale_MPISBAIJ,
2018:                                        MatShift_MPISBAIJ,
2019:                                        0,
2020:                                        0,
2021:                                /* 49*/ 0,
2022:                                        0,
2023:                                        0,
2024:                                        0,
2025:                                        0,
2026:                                /* 54*/ 0,
2027:                                        0,
2028:                                        MatSetUnfactored_MPISBAIJ,
2029:                                        0,
2030:                                        MatSetValuesBlocked_MPISBAIJ,
2031:                                /* 59*/ MatCreateSubMatrix_MPISBAIJ,
2032:                                        0,
2033:                                        0,
2034:                                        0,
2035:                                        0,
2036:                                /* 64*/ 0,
2037:                                        0,
2038:                                        0,
2039:                                        0,
2040:                                        0,
2041:                                /* 69*/ MatGetRowMaxAbs_MPISBAIJ,
2042:                                        0,
2043:                                        0,
2044:                                        0,
2045:                                        0,
2046:                                /* 74*/ 0,
2047:                                        0,
2048:                                        0,
2049:                                        0,
2050:                                        0,
2051:                                /* 79*/ 0,
2052:                                        0,
2053:                                        0,
2054:                                        0,
2055:                                        MatLoad_MPISBAIJ,
2056:                                /* 84*/ 0,
2057:                                        0,
2058:                                        0,
2059:                                        0,
2060:                                        0,
2061:                                /* 89*/ 0,
2062:                                        0,
2063:                                        0,
2064:                                        0,
2065:                                        0,
2066:                                /* 94*/ 0,
2067:                                        0,
2068:                                        0,
2069:                                        0,
2070:                                        0,
2071:                                /* 99*/ 0,
2072:                                        0,
2073:                                        0,
2074:                                        0,
2075:                                        0,
2076:                                /*104*/ 0,
2077:                                        MatRealPart_MPISBAIJ,
2078:                                        MatImaginaryPart_MPISBAIJ,
2079:                                        MatGetRowUpperTriangular_MPISBAIJ,
2080:                                        MatRestoreRowUpperTriangular_MPISBAIJ,
2081:                                /*109*/ 0,
2082:                                        0,
2083:                                        0,
2084:                                        0,
2085:                                        MatMissingDiagonal_MPISBAIJ,
2086:                                /*114*/ 0,
2087:                                        0,
2088:                                        0,
2089:                                        0,
2090:                                        0,
2091:                                /*119*/ 0,
2092:                                        0,
2093:                                        0,
2094:                                        0,
2095:                                        0,
2096:                                /*124*/ 0,
2097:                                        0,
2098:                                        0,
2099:                                        0,
2100:                                        0,
2101:                                /*129*/ 0,
2102:                                        0,
2103:                                        0,
2104:                                        0,
2105:                                        0,
2106:                                /*134*/ 0,
2107:                                        0,
2108:                                        0,
2109:                                        0,
2110:                                        0,
2111:                                /*139*/ MatSetBlockSizes_Default,
2112:                                        0,
2113:                                        0,
2114:                                        0,
2115:                                        0,
2116:                                 /*144*/MatCreateMPIMatConcatenateSeqMat_MPISBAIJ
2117: };

2119: PetscErrorCode  MatMPISBAIJSetPreallocation_MPISBAIJ(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt *d_nnz,PetscInt o_nz,const PetscInt *o_nnz)
2120: {
2121:   Mat_MPISBAIJ   *b = (Mat_MPISBAIJ*)B->data;
2123:   PetscInt       i,mbs,Mbs;
2124:   PetscMPIInt    size;

2127:   MatSetBlockSize(B,PetscAbs(bs));
2128:   PetscLayoutSetUp(B->rmap);
2129:   PetscLayoutSetUp(B->cmap);
2130:   PetscLayoutGetBlockSize(B->rmap,&bs);
2131:   if (B->rmap->N > B->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)B),PETSC_ERR_SUP,"MPISBAIJ matrix cannot have more rows %D than columns %D",B->rmap->N,B->cmap->N);
2132:   if (B->rmap->n > B->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"MPISBAIJ matrix cannot have more local rows %D than columns %D",B->rmap->n,B->cmap->n);

2134:   mbs = B->rmap->n/bs;
2135:   Mbs = B->rmap->N/bs;
2136:   if (mbs*bs != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"No of local rows %D must be divisible by blocksize %D",B->rmap->N,bs);

2138:   B->rmap->bs = bs;
2139:   b->bs2      = bs*bs;
2140:   b->mbs      = mbs;
2141:   b->Mbs      = Mbs;
2142:   b->nbs      = B->cmap->n/bs;
2143:   b->Nbs      = B->cmap->N/bs;

2145:   for (i=0; i<=b->size; i++) {
2146:     b->rangebs[i] = B->rmap->range[i]/bs;
2147:   }
2148:   b->rstartbs = B->rmap->rstart/bs;
2149:   b->rendbs   = B->rmap->rend/bs;

2151:   b->cstartbs = B->cmap->rstart/bs;
2152:   b->cendbs   = B->cmap->rend/bs;

2154: #if defined(PETSC_USE_CTABLE)
2155:   PetscTableDestroy(&b->colmap);
2156: #else
2157:   PetscFree(b->colmap);
2158: #endif
2159:   PetscFree(b->garray);
2160:   VecDestroy(&b->lvec);
2161:   VecScatterDestroy(&b->Mvctx);
2162:   VecDestroy(&b->slvec0);
2163:   VecDestroy(&b->slvec0b);
2164:   VecDestroy(&b->slvec1);
2165:   VecDestroy(&b->slvec1a);
2166:   VecDestroy(&b->slvec1b);
2167:   VecScatterDestroy(&b->sMvctx);

2169:   /* Because the B will have been resized we simply destroy it and create a new one each time */
2170:   MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);
2171:   MatDestroy(&b->B);
2172:   MatCreate(PETSC_COMM_SELF,&b->B);
2173:   MatSetSizes(b->B,B->rmap->n,size > 1 ? B->cmap->N : 0,B->rmap->n,size > 1 ? B->cmap->N : 0);
2174:   MatSetType(b->B,MATSEQBAIJ);
2175:   PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);

2177:   if (!B->preallocated) {
2178:     MatCreate(PETSC_COMM_SELF,&b->A);
2179:     MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
2180:     MatSetType(b->A,MATSEQSBAIJ);
2181:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
2182:     MatStashCreate_Private(PetscObjectComm((PetscObject)B),bs,&B->bstash);
2183:   }

2185:   MatSeqSBAIJSetPreallocation(b->A,bs,d_nz,d_nnz);
2186:   MatSeqBAIJSetPreallocation(b->B,bs,o_nz,o_nnz);

2188:   B->preallocated  = PETSC_TRUE;
2189:   B->was_assembled = PETSC_FALSE;
2190:   B->assembled     = PETSC_FALSE;
2191:   return(0);
2192: }

2194: PetscErrorCode MatMPISBAIJSetPreallocationCSR_MPISBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
2195: {
2196:   PetscInt       m,rstart,cend;
2197:   PetscInt       i,j,d,nz,bd, nz_max=0,*d_nnz=0,*o_nnz=0;
2198:   const PetscInt *JJ    =0;
2199:   PetscScalar    *values=0;
2200:   PetscBool      roworiented = ((Mat_MPISBAIJ*)B->data)->roworiented;
2202:   PetscBool      nooffprocentries;

2205:   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
2206:   PetscLayoutSetBlockSize(B->rmap,bs);
2207:   PetscLayoutSetBlockSize(B->cmap,bs);
2208:   PetscLayoutSetUp(B->rmap);
2209:   PetscLayoutSetUp(B->cmap);
2210:   PetscLayoutGetBlockSize(B->rmap,&bs);
2211:   m      = B->rmap->n/bs;
2212:   rstart = B->rmap->rstart/bs;
2213:   cend   = B->cmap->rend/bs;

2215:   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
2216:   PetscMalloc2(m,&d_nnz,m,&o_nnz);
2217:   for (i=0; i<m; i++) {
2218:     nz = ii[i+1] - ii[i];
2219:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz);
2220:     /* count the ones on the diagonal and above, split into diagonal and off diagonal portions. */
2221:     JJ     = jj + ii[i];
2222:     bd     = 0;
2223:     for (j=0; j<nz; j++) {
2224:       if (*JJ >= i + rstart) break;
2225:       JJ++;
2226:       bd++;
2227:     }
2228:     d  = 0;
2229:     for (; j<nz; j++) {
2230:       if (*JJ++ >= cend) break;
2231:       d++;
2232:     }
2233:     d_nnz[i] = d;
2234:     o_nnz[i] = nz - d - bd;
2235:     nz       = nz - bd;
2236:     nz_max = PetscMax(nz_max,nz);
2237:   }
2238:   MatMPISBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);
2239:   MatSetOption(B,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE);
2240:   PetscFree2(d_nnz,o_nnz);

2242:   values = (PetscScalar*)V;
2243:   if (!values) {
2244:     PetscCalloc1(bs*bs*nz_max,&values);
2245:   }
2246:   for (i=0; i<m; i++) {
2247:     PetscInt          row    = i + rstart;
2248:     PetscInt          ncols  = ii[i+1] - ii[i];
2249:     const PetscInt    *icols = jj + ii[i];
2250:     if (bs == 1 || !roworiented) {         /* block ordering matches the non-nested layout of MatSetValues so we can insert entire rows */
2251:       const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2252:       MatSetValuesBlocked_MPISBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);
2253:     } else {                    /* block ordering does not match so we can only insert one block at a time. */
2254:       PetscInt j;
2255:       for (j=0; j<ncols; j++) {
2256:         const PetscScalar *svals = values + (V ? (bs*bs*(ii[i]+j)) : 0);
2257:         MatSetValuesBlocked_MPISBAIJ(B,1,&row,1,&icols[j],svals,INSERT_VALUES);
2258:       }
2259:     }
2260:   }

2262:   if (!V) { PetscFree(values); }
2263:   nooffprocentries    = B->nooffprocentries;
2264:   B->nooffprocentries = PETSC_TRUE;
2265:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2266:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2267:   B->nooffprocentries = nooffprocentries;

2269:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
2270:   return(0);
2271: }

2273: /*MC
2274:    MATMPISBAIJ - MATMPISBAIJ = "mpisbaij" - A matrix type to be used for distributed symmetric sparse block matrices,
2275:    based on block compressed sparse row format.  Only the upper triangular portion of the "diagonal" portion of
2276:    the matrix is stored.

2278:    For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
2279:    can call MatSetOption(Mat, MAT_HERMITIAN);

2281:    Options Database Keys:
2282: . -mat_type mpisbaij - sets the matrix type to "mpisbaij" during a call to MatSetFromOptions()

2284:    Notes:
2285:      The number of rows in the matrix must be less than or equal to the number of columns. Similarly the number of rows in the
2286:      diagonal portion of the matrix of each process has to less than or equal the number of columns.

2288:    Level: beginner

2290: .seealso: MatCreateMPISBAIJ(), MATSEQSBAIJ, MatType
2291: M*/

2293: PETSC_EXTERN PetscErrorCode MatCreate_MPISBAIJ(Mat B)
2294: {
2295:   Mat_MPISBAIJ   *b;
2297:   PetscBool      flg = PETSC_FALSE;

2300:   PetscNewLog(B,&b);
2301:   B->data = (void*)b;
2302:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));

2304:   B->ops->destroy = MatDestroy_MPISBAIJ;
2305:   B->ops->view    = MatView_MPISBAIJ;
2306:   B->assembled    = PETSC_FALSE;
2307:   B->insertmode   = NOT_SET_VALUES;

2309:   MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);
2310:   MPI_Comm_size(PetscObjectComm((PetscObject)B),&b->size);

2312:   /* build local table of row and column ownerships */
2313:   PetscMalloc1(b->size+2,&b->rangebs);

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

2318:   b->donotstash  = PETSC_FALSE;
2319:   b->colmap      = NULL;
2320:   b->garray      = NULL;
2321:   b->roworiented = PETSC_TRUE;

2323:   /* stuff used in block assembly */
2324:   b->barray = 0;

2326:   /* stuff used for matrix vector multiply */
2327:   b->lvec    = 0;
2328:   b->Mvctx   = 0;
2329:   b->slvec0  = 0;
2330:   b->slvec0b = 0;
2331:   b->slvec1  = 0;
2332:   b->slvec1a = 0;
2333:   b->slvec1b = 0;
2334:   b->sMvctx  = 0;

2336:   /* stuff for MatGetRow() */
2337:   b->rowindices   = 0;
2338:   b->rowvalues    = 0;
2339:   b->getrowactive = PETSC_FALSE;

2341:   /* hash table stuff */
2342:   b->ht           = 0;
2343:   b->hd           = 0;
2344:   b->ht_size      = 0;
2345:   b->ht_flag      = PETSC_FALSE;
2346:   b->ht_fact      = 0;
2347:   b->ht_total_ct  = 0;
2348:   b->ht_insert_ct = 0;

2350:   /* stuff for MatCreateSubMatrices_MPIBAIJ_local() */
2351:   b->ijonly = PETSC_FALSE;

2353:   b->in_loc = 0;
2354:   b->v_loc  = 0;
2355:   b->n_loc  = 0;

2357:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPISBAIJ);
2358:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPISBAIJ);
2359:   PetscObjectComposeFunction((PetscObject)B,"MatMPISBAIJSetPreallocation_C",MatMPISBAIJSetPreallocation_MPISBAIJ);
2360:   PetscObjectComposeFunction((PetscObject)B,"MatMPISBAIJSetPreallocationCSR_C",MatMPISBAIJSetPreallocationCSR_MPISBAIJ);
2361: #if defined(PETSC_HAVE_ELEMENTAL)
2362:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpisbaij_elemental_C",MatConvert_MPISBAIJ_Elemental);
2363: #endif
2364:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpisbaij_mpiaij_C",MatConvert_MPISBAIJ_XAIJ);
2365:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpisbaij_mpibaij_C",MatConvert_MPISBAIJ_XAIJ);

2367:   B->symmetric                  = PETSC_TRUE;
2368:   B->structurally_symmetric     = PETSC_TRUE;
2369:   B->symmetric_set              = PETSC_TRUE;
2370:   B->structurally_symmetric_set = PETSC_TRUE;
2371:   B->symmetric_eternal          = PETSC_TRUE;

2373:   B->hermitian                  = PETSC_FALSE;
2374:   B->hermitian_set              = PETSC_FALSE;

2376:   PetscObjectChangeTypeName((PetscObject)B,MATMPISBAIJ);
2377:   PetscOptionsBegin(PetscObjectComm((PetscObject)B),NULL,"Options for loading MPISBAIJ matrix 1","Mat");
2378:   PetscOptionsBool("-mat_use_hash_table","Use hash table to save memory in constructing matrix","MatSetOption",flg,&flg,NULL);
2379:   if (flg) {
2380:     PetscReal fact = 1.39;
2381:     MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);
2382:     PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,NULL);
2383:     if (fact <= 1.0) fact = 1.39;
2384:     MatMPIBAIJSetHashTableFactor(B,fact);
2385:     PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);
2386:   }
2387:   PetscOptionsEnd();
2388:   return(0);
2389: }

2391: /*MC
2392:    MATSBAIJ - MATSBAIJ = "sbaij" - A matrix type to be used for symmetric block sparse matrices.

2394:    This matrix type is identical to MATSEQSBAIJ when constructed with a single process communicator,
2395:    and MATMPISBAIJ otherwise.

2397:    Options Database Keys:
2398: . -mat_type sbaij - sets the matrix type to "sbaij" during a call to MatSetFromOptions()

2400:   Level: beginner

2402: .seealso: MatCreateMPISBAIJ,MATSEQSBAIJ,MATMPISBAIJ
2403: M*/

2405: /*@C
2406:    MatMPISBAIJSetPreallocation - For good matrix assembly performance
2407:    the user should preallocate the matrix storage by setting the parameters
2408:    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2409:    performance can be increased by more than a factor of 50.

2411:    Collective on Mat

2413:    Input Parameters:
2414: +  B - the matrix
2415: .  bs   - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
2416:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
2417: .  d_nz  - number of block nonzeros per block row in diagonal portion of local
2418:            submatrix  (same for all local rows)
2419: .  d_nnz - array containing the number of block nonzeros in the various block rows
2420:            in the upper triangular and diagonal part of the in diagonal portion of the local
2421:            (possibly different for each block row) or NULL.  If you plan to factor the matrix you must leave room
2422:            for the diagonal entry and set a value even if it is zero.
2423: .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
2424:            submatrix (same for all local rows).
2425: -  o_nnz - array containing the number of nonzeros in the various block rows of the
2426:            off-diagonal portion of the local submatrix that is right of the diagonal
2427:            (possibly different for each block row) or NULL.


2430:    Options Database Keys:
2431: +   -mat_no_unroll - uses code that does not unroll the loops in the
2432:                      block calculations (much slower)
2433: -   -mat_block_size - size of the blocks to use

2435:    Notes:

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

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

2442:    Storage Information:
2443:    For a square global matrix we define each processor's diagonal portion
2444:    to be its local rows and the corresponding columns (a square submatrix);
2445:    each processor's off-diagonal portion encompasses the remainder of the
2446:    local matrix (a rectangular submatrix).

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

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

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

2462: .vb
2463:            0 1 2 3 4 5 6 7 8 9 10 11
2464:           --------------------------
2465:    row 3  |. . . d d d o o o o  o  o
2466:    row 4  |. . . d d d o o o o  o  o
2467:    row 5  |. . . d d d o o o o  o  o
2468:           --------------------------
2469: .ve

2471:    Thus, any entries in the d locations are stored in the d (diagonal)
2472:    submatrix, and any entries in the o locations are stored in the
2473:    o (off-diagonal) submatrix.  Note that the d matrix is stored in
2474:    MatSeqSBAIJ format and the o submatrix in MATSEQBAIJ format.

2476:    Now d_nz should indicate the number of block nonzeros per row in the upper triangular
2477:    plus the diagonal part of the d matrix,
2478:    and o_nz should indicate the number of block nonzeros per row in the o matrix

2480:    In general, for PDE problems in which most nonzeros are near the diagonal,
2481:    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
2482:    or you will get TERRIBLE performance; see the users' manual chapter on
2483:    matrices.

2485:    Level: intermediate

2487: .seealso: MatCreate(), MatCreateSeqSBAIJ(), MatSetValues(), MatCreateBAIJ(), PetscSplitOwnership()
2488: @*/
2489: PetscErrorCode  MatMPISBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
2490: {

2497:   PetscTryMethod(B,"MatMPISBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,bs,d_nz,d_nnz,o_nz,o_nnz));
2498:   return(0);
2499: }

2501: /*@C
2502:    MatCreateSBAIJ - Creates a sparse parallel matrix in symmetric block AIJ format
2503:    (block compressed row).  For good matrix assembly performance
2504:    the user should preallocate the matrix storage by setting the parameters
2505:    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2506:    performance can be increased by more than a factor of 50.

2508:    Collective

2510:    Input Parameters:
2511: +  comm - MPI communicator
2512: .  bs   - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
2513:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
2514: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2515:            This value should be the same as the local size used in creating the
2516:            y vector for the matrix-vector product y = Ax.
2517: .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
2518:            This value should be the same as the local size used in creating the
2519:            x vector for the matrix-vector product y = Ax.
2520: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2521: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2522: .  d_nz  - number of block nonzeros per block row in diagonal portion of local
2523:            submatrix  (same for all local rows)
2524: .  d_nnz - array containing the number of block nonzeros in the various block rows
2525:            in the upper triangular portion of the in diagonal portion of the local
2526:            (possibly different for each block block row) or NULL.
2527:            If you plan to factor the matrix you must leave room for the diagonal entry and
2528:            set its value even if it is zero.
2529: .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
2530:            submatrix (same for all local rows).
2531: -  o_nnz - array containing the number of nonzeros in the various block rows of the
2532:            off-diagonal portion of the local submatrix (possibly different for
2533:            each block row) or NULL.

2535:    Output Parameter:
2536: .  A - the matrix

2538:    Options Database Keys:
2539: +   -mat_no_unroll - uses code that does not unroll the loops in the
2540:                      block calculations (much slower)
2541: .   -mat_block_size - size of the blocks to use
2542: -   -mat_mpi - use the parallel matrix data structures even on one processor
2543:                (defaults to using SeqBAIJ format on one processor)

2545:    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2546:    MatXXXXSetPreallocation() paradigm instead of this routine directly.
2547:    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]

2549:    Notes:
2550:    The number of rows and columns must be divisible by blocksize.
2551:    This matrix type does not support complex Hermitian operation.

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

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

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

2561:    Storage Information:
2562:    For a square global matrix we define each processor's diagonal portion
2563:    to be its local rows and the corresponding columns (a square submatrix);
2564:    each processor's off-diagonal portion encompasses the remainder of the
2565:    local matrix (a rectangular submatrix).

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

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

2576: .vb
2577:            0 1 2 3 4 5 6 7 8 9 10 11
2578:           --------------------------
2579:    row 3  |. . . d d d o o o o  o  o
2580:    row 4  |. . . d d d o o o o  o  o
2581:    row 5  |. . . d d d o o o o  o  o
2582:           --------------------------
2583: .ve

2585:    Thus, any entries in the d locations are stored in the d (diagonal)
2586:    submatrix, and any entries in the o locations are stored in the
2587:    o (off-diagonal) submatrix.  Note that the d matrix is stored in
2588:    MatSeqSBAIJ format and the o submatrix in MATSEQBAIJ format.

2590:    Now d_nz should indicate the number of block nonzeros per row in the upper triangular
2591:    plus the diagonal part of the d matrix,
2592:    and o_nz should indicate the number of block nonzeros per row in the o matrix.
2593:    In general, for PDE problems in which most nonzeros are near the diagonal,
2594:    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
2595:    or you will get TERRIBLE performance; see the users' manual chapter on
2596:    matrices.

2598:    Level: intermediate

2600: .seealso: MatCreate(), MatCreateSeqSBAIJ(), MatSetValues(), MatCreateBAIJ()
2601: @*/

2603: PetscErrorCode  MatCreateSBAIJ(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)
2604: {
2606:   PetscMPIInt    size;

2609:   MatCreate(comm,A);
2610:   MatSetSizes(*A,m,n,M,N);
2611:   MPI_Comm_size(comm,&size);
2612:   if (size > 1) {
2613:     MatSetType(*A,MATMPISBAIJ);
2614:     MatMPISBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);
2615:   } else {
2616:     MatSetType(*A,MATSEQSBAIJ);
2617:     MatSeqSBAIJSetPreallocation(*A,bs,d_nz,d_nnz);
2618:   }
2619:   return(0);
2620: }


2623: static PetscErrorCode MatDuplicate_MPISBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
2624: {
2625:   Mat            mat;
2626:   Mat_MPISBAIJ   *a,*oldmat = (Mat_MPISBAIJ*)matin->data;
2628:   PetscInt       len=0,nt,bs=matin->rmap->bs,mbs=oldmat->mbs;
2629:   PetscScalar    *array;

2632:   *newmat = 0;

2634:   MatCreate(PetscObjectComm((PetscObject)matin),&mat);
2635:   MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);
2636:   MatSetType(mat,((PetscObject)matin)->type_name);
2637:   PetscLayoutReference(matin->rmap,&mat->rmap);
2638:   PetscLayoutReference(matin->cmap,&mat->cmap);

2640:   mat->factortype   = matin->factortype;
2641:   mat->preallocated = PETSC_TRUE;
2642:   mat->assembled    = PETSC_TRUE;
2643:   mat->insertmode   = NOT_SET_VALUES;

2645:   a      = (Mat_MPISBAIJ*)mat->data;
2646:   a->bs2 = oldmat->bs2;
2647:   a->mbs = oldmat->mbs;
2648:   a->nbs = oldmat->nbs;
2649:   a->Mbs = oldmat->Mbs;
2650:   a->Nbs = oldmat->Nbs;

2652:   a->size         = oldmat->size;
2653:   a->rank         = oldmat->rank;
2654:   a->donotstash   = oldmat->donotstash;
2655:   a->roworiented  = oldmat->roworiented;
2656:   a->rowindices   = 0;
2657:   a->rowvalues    = 0;
2658:   a->getrowactive = PETSC_FALSE;
2659:   a->barray       = 0;
2660:   a->rstartbs     = oldmat->rstartbs;
2661:   a->rendbs       = oldmat->rendbs;
2662:   a->cstartbs     = oldmat->cstartbs;
2663:   a->cendbs       = oldmat->cendbs;

2665:   /* hash table stuff */
2666:   a->ht           = 0;
2667:   a->hd           = 0;
2668:   a->ht_size      = 0;
2669:   a->ht_flag      = oldmat->ht_flag;
2670:   a->ht_fact      = oldmat->ht_fact;
2671:   a->ht_total_ct  = 0;
2672:   a->ht_insert_ct = 0;

2674:   PetscArraycpy(a->rangebs,oldmat->rangebs,a->size+2);
2675:   if (oldmat->colmap) {
2676: #if defined(PETSC_USE_CTABLE)
2677:     PetscTableCreateCopy(oldmat->colmap,&a->colmap);
2678: #else
2679:     PetscMalloc1(a->Nbs,&a->colmap);
2680:     PetscLogObjectMemory((PetscObject)mat,(a->Nbs)*sizeof(PetscInt));
2681:     PetscArraycpy(a->colmap,oldmat->colmap,a->Nbs);
2682: #endif
2683:   } else a->colmap = 0;

2685:   if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
2686:     PetscMalloc1(len,&a->garray);
2687:     PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));
2688:     PetscArraycpy(a->garray,oldmat->garray,len);
2689:   } else a->garray = 0;

2691:   MatStashCreate_Private(PetscObjectComm((PetscObject)matin),matin->rmap->bs,&mat->bstash);
2692:   VecDuplicate(oldmat->lvec,&a->lvec);
2693:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);
2694:   VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
2695:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);

2697:   VecDuplicate(oldmat->slvec0,&a->slvec0);
2698:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec0);
2699:   VecDuplicate(oldmat->slvec1,&a->slvec1);
2700:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec1);

2702:   VecGetLocalSize(a->slvec1,&nt);
2703:   VecGetArray(a->slvec1,&array);
2704:   VecCreateSeqWithArray(PETSC_COMM_SELF,1,bs*mbs,array,&a->slvec1a);
2705:   VecCreateSeqWithArray(PETSC_COMM_SELF,1,nt-bs*mbs,array+bs*mbs,&a->slvec1b);
2706:   VecRestoreArray(a->slvec1,&array);
2707:   VecGetArray(a->slvec0,&array);
2708:   VecCreateSeqWithArray(PETSC_COMM_SELF,1,nt-bs*mbs,array+bs*mbs,&a->slvec0b);
2709:   VecRestoreArray(a->slvec0,&array);
2710:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec0);
2711:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec1);
2712:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec0b);
2713:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec1a);
2714:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec1b);

2716:   /*  VecScatterCopy(oldmat->sMvctx,&a->sMvctx); - not written yet, replaced by the lazy trick: */
2717:   PetscObjectReference((PetscObject)oldmat->sMvctx);
2718:   a->sMvctx = oldmat->sMvctx;
2719:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->sMvctx);

2721:   MatDuplicate(oldmat->A,cpvalues,&a->A);
2722:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
2723:   MatDuplicate(oldmat->B,cpvalues,&a->B);
2724:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);
2725:   PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);
2726:   *newmat = mat;
2727:   return(0);
2728: }

2730: PetscErrorCode MatLoad_MPISBAIJ(Mat newmat,PetscViewer viewer)
2731: {
2733:   PetscInt       i,nz,j,rstart,rend;
2734:   PetscScalar    *vals,*buf;
2735:   MPI_Comm       comm;
2736:   MPI_Status     status;
2737:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag,*sndcounts = 0,*browners,maxnz,*rowners,mmbs;
2738:   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols,*locrowlens;
2739:   PetscInt       *procsnz = 0,jj,*mycols,*ibuf;
2740:   PetscInt       bs = newmat->rmap->bs,Mbs,mbs,extra_rows;
2741:   PetscInt       *dlens,*odlens,*mask,*masked1,*masked2,rowcount,odcount;
2742:   PetscInt       dcount,kmax,k,nzcount,tmp;
2743:   int            fd;
2744:   PetscBool      isbinary;

2747:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
2748:   if (!isbinary) SETERRQ2(PetscObjectComm((PetscObject)newmat),PETSC_ERR_SUP,"Viewer type %s not yet supported for reading %s matrices",((PetscObject)viewer)->type_name,((PetscObject)newmat)->type_name);

2750:   /* force binary viewer to load .info file if it has not yet done so */
2751:   PetscViewerSetUp(viewer);
2752:   PetscObjectGetComm((PetscObject)viewer,&comm);
2753:   PetscOptionsBegin(comm,NULL,"Options for loading MPISBAIJ matrix 2","Mat");
2754:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
2755:   PetscOptionsEnd();
2756:   if (bs < 0) bs = 1;

2758:   MPI_Comm_size(comm,&size);
2759:   MPI_Comm_rank(comm,&rank);
2760:   PetscViewerBinaryGetDescriptor(viewer,&fd);
2761:   if (!rank) {
2762:     PetscBinaryRead(fd,(char*)header,4,NULL,PETSC_INT);
2763:     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
2764:     if (header[3] < 0) SETERRQ(PetscObjectComm((PetscObject)newmat),PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as MPISBAIJ");
2765:   }

2767:   MPI_Bcast(header+1,3,MPIU_INT,0,comm);
2768:   M    = header[1];
2769:   N    = header[2];

2771:   /* If global sizes are set, check if they are consistent with that given in the file */
2772:   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);
2773:   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);

2775:   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");

2777:   /*
2778:      This code adds extra rows to make sure the number of rows is
2779:      divisible by the blocksize
2780:   */
2781:   Mbs        = M/bs;
2782:   extra_rows = bs - M + bs*(Mbs);
2783:   if (extra_rows == bs) extra_rows = 0;
2784:   else                  Mbs++;
2785:   if (extra_rows &&!rank) {
2786:     PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");
2787:   }

2789:   /* determine ownership of all rows */
2790:   if (newmat->rmap->n < 0) { /* PETSC_DECIDE */
2791:     mbs = Mbs/size + ((Mbs % size) > rank);
2792:     m   = mbs*bs;
2793:   } else { /* User Set */
2794:     m   = newmat->rmap->n;
2795:     mbs = m/bs;
2796:   }
2797:   PetscMalloc2(size+1,&rowners,size+1,&browners);
2798:   PetscMPIIntCast(mbs,&mmbs);
2799:   MPI_Allgather(&mmbs,1,MPI_INT,rowners+1,1,MPI_INT,comm);
2800:   rowners[0] = 0;
2801:   for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
2802:   for (i=0; i<=size; i++) browners[i] = rowners[i]*bs;
2803:   rstart = rowners[rank];
2804:   rend   = rowners[rank+1];

2806:   /* distribute row lengths to all processors */
2807:   PetscMalloc1((rend-rstart)*bs,&locrowlens);
2808:   if (!rank) {
2809:     PetscMalloc1(M+extra_rows,&rowlengths);
2810:     PetscBinaryRead(fd,rowlengths,M,NULL,PETSC_INT);
2811:     for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
2812:     PetscMalloc1(size,&sndcounts);
2813:     for (i=0; i<size; i++) sndcounts[i] = browners[i+1] - browners[i];
2814:     MPI_Scatterv(rowlengths,sndcounts,browners,MPIU_INT,locrowlens,(rend-rstart)*bs,MPIU_INT,0,comm);
2815:     PetscFree(sndcounts);
2816:   } else {
2817:     MPI_Scatterv(0,0,0,MPIU_INT,locrowlens,(rend-rstart)*bs,MPIU_INT,0,comm);
2818:   }

2820:   if (!rank) {   /* procs[0] */
2821:     /* calculate the number of nonzeros on each processor */
2822:     PetscCalloc1(size,&procsnz);
2823:     for (i=0; i<size; i++) {
2824:       for (j=rowners[i]*bs; j< rowners[i+1]*bs; j++) {
2825:         procsnz[i] += rowlengths[j];
2826:       }
2827:     }
2828:     PetscFree(rowlengths);

2830:     /* determine max buffer needed and allocate it */
2831:     maxnz = 0;
2832:     for (i=0; i<size; i++) {
2833:       maxnz = PetscMax(maxnz,procsnz[i]);
2834:     }
2835:     PetscMalloc1(maxnz,&cols);

2837:     /* read in my part of the matrix column indices  */
2838:     nz     = procsnz[0];
2839:     PetscMalloc1(nz,&ibuf);
2840:     mycols = ibuf;
2841:     if (size == 1) nz -= extra_rows;
2842:     PetscBinaryRead(fd,mycols,nz,NULL,PETSC_INT);
2843:     if (size == 1) {
2844:       for (i=0; i< extra_rows; i++) mycols[nz+i] = M+i;
2845:     }

2847:     /* read in every ones (except the last) and ship off */
2848:     for (i=1; i<size-1; i++) {
2849:       nz   = procsnz[i];
2850:       PetscBinaryRead(fd,cols,nz,NULL,PETSC_INT);
2851:       MPI_Send(cols,nz,MPIU_INT,i,tag,comm);
2852:     }
2853:     /* read in the stuff for the last proc */
2854:     if (size != 1) {
2855:       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
2856:       PetscBinaryRead(fd,cols,nz,NULL,PETSC_INT);
2857:       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
2858:       MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);
2859:     }
2860:     PetscFree(cols);
2861:   } else {  /* procs[i], i>0 */
2862:     /* determine buffer space needed for message */
2863:     nz = 0;
2864:     for (i=0; i<m; i++) nz += locrowlens[i];
2865:     PetscMalloc1(nz,&ibuf);
2866:     mycols = ibuf;
2867:     /* receive message of column indices*/
2868:     MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);
2869:     MPI_Get_count(&status,MPIU_INT,&maxnz);
2870:     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2871:   }

2873:   /* loop over local rows, determining number of off diagonal entries */
2874:   PetscMalloc2(rend-rstart,&dlens,rend-rstart,&odlens);
2875:   PetscCalloc3(Mbs,&mask,Mbs,&masked1,Mbs,&masked2);
2876:   rowcount = 0;
2877:   nzcount  = 0;
2878:   for (i=0; i<mbs; i++) {
2879:     dcount  = 0;
2880:     odcount = 0;
2881:     for (j=0; j<bs; j++) {
2882:       kmax = locrowlens[rowcount];
2883:       for (k=0; k<kmax; k++) {
2884:         tmp = mycols[nzcount++]/bs; /* block col. index */
2885:         if (!mask[tmp]) {
2886:           mask[tmp] = 1;
2887:           if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp; /* entry in off-diag portion */
2888:           else masked1[dcount++] = tmp; /* entry in diag portion */
2889:         }
2890:       }
2891:       rowcount++;
2892:     }

2894:     dlens[i]  = dcount;  /* d_nzz[i] */
2895:     odlens[i] = odcount; /* o_nzz[i] */

2897:     /* zero out the mask elements we set */
2898:     for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
2899:     for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
2900:   }
2901:   MatSetSizes(newmat,m,m,M+extra_rows,N+extra_rows);
2902:   MatMPISBAIJSetPreallocation(newmat,bs,0,dlens,0,odlens);
2903:   MatSetOption(newmat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE);

2905:   if (!rank) {
2906:     PetscMalloc1(maxnz,&buf);
2907:     /* read in my part of the matrix numerical values  */
2908:     nz     = procsnz[0];
2909:     vals   = buf;
2910:     mycols = ibuf;
2911:     if (size == 1) nz -= extra_rows;
2912:     PetscBinaryRead(fd,vals,nz,NULL,PETSC_SCALAR);
2913:     if (size == 1) {
2914:       for (i=0; i< extra_rows; i++) vals[nz+i] = 1.0;
2915:     }

2917:     /* insert into matrix */
2918:     jj = rstart*bs;
2919:     for (i=0; i<m; i++) {
2920:       MatSetValues(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
2921:       mycols += locrowlens[i];
2922:       vals   += locrowlens[i];
2923:       jj++;
2924:     }

2926:     /* read in other processors (except the last one) and ship out */
2927:     for (i=1; i<size-1; i++) {
2928:       nz   = procsnz[i];
2929:       vals = buf;
2930:       PetscBinaryRead(fd,vals,nz,NULL,PETSC_SCALAR);
2931:       MPI_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);
2932:     }
2933:     /* the last proc */
2934:     if (size != 1) {
2935:       nz   = procsnz[i] - extra_rows;
2936:       vals = buf;
2937:       PetscBinaryRead(fd,vals,nz,NULL,PETSC_SCALAR);
2938:       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
2939:       MPI_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)newmat)->tag,comm);
2940:     }
2941:     PetscFree(procsnz);

2943:   } else {
2944:     /* receive numeric values */
2945:     PetscMalloc1(nz,&buf);

2947:     /* receive message of values*/
2948:     vals   = buf;
2949:     mycols = ibuf;
2950:     MPI_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm,&status);
2951:     MPI_Get_count(&status,MPIU_SCALAR,&maxnz);
2952:     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");

2954:     /* insert into matrix */
2955:     jj = rstart*bs;
2956:     for (i=0; i<m; i++) {
2957:       MatSetValues_MPISBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
2958:       mycols += locrowlens[i];
2959:       vals   += locrowlens[i];
2960:       jj++;
2961:     }
2962:   }

2964:   PetscFree(locrowlens);
2965:   PetscFree(buf);
2966:   PetscFree(ibuf);
2967:   PetscFree2(rowners,browners);
2968:   PetscFree2(dlens,odlens);
2969:   PetscFree3(mask,masked1,masked2);
2970:   MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
2971:   MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
2972:   return(0);
2973: }

2975: /*XXXXX@
2976:    MatMPISBAIJSetHashTableFactor - Sets the factor required to compute the size of the HashTable.

2978:    Input Parameters:
2979: .  mat  - the matrix
2980: .  fact - factor

2982:    Not Collective on Mat, each process can have a different hash factor

2984:    Level: advanced

2986:   Notes:
2987:    This can also be set by the command line option: -mat_use_hash_table fact

2989: .seealso: MatSetOption()
2990: @XXXXX*/


2993: PetscErrorCode MatGetRowMaxAbs_MPISBAIJ(Mat A,Vec v,PetscInt idx[])
2994: {
2995:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;
2996:   Mat_SeqBAIJ    *b = (Mat_SeqBAIJ*)(a->B)->data;
2997:   PetscReal      atmp;
2998:   PetscReal      *work,*svalues,*rvalues;
3000:   PetscInt       i,bs,mbs,*bi,*bj,brow,j,ncols,krow,kcol,col,row,Mbs,bcol;
3001:   PetscMPIInt    rank,size;
3002:   PetscInt       *rowners_bs,dest,count,source;
3003:   PetscScalar    *va;
3004:   MatScalar      *ba;
3005:   MPI_Status     stat;

3008:   if (idx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Send email to petsc-maint@mcs.anl.gov");
3009:   MatGetRowMaxAbs(a->A,v,NULL);
3010:   VecGetArray(v,&va);

3012:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
3013:   MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);

3015:   bs  = A->rmap->bs;
3016:   mbs = a->mbs;
3017:   Mbs = a->Mbs;
3018:   ba  = b->a;
3019:   bi  = b->i;
3020:   bj  = b->j;

3022:   /* find ownerships */
3023:   rowners_bs = A->rmap->range;

3025:   /* each proc creates an array to be distributed */
3026:   PetscCalloc1(bs*Mbs,&work);

3028:   /* row_max for B */
3029:   if (rank != size-1) {
3030:     for (i=0; i<mbs; i++) {
3031:       ncols = bi[1] - bi[0]; bi++;
3032:       brow  = bs*i;
3033:       for (j=0; j<ncols; j++) {
3034:         bcol = bs*(*bj);
3035:         for (kcol=0; kcol<bs; kcol++) {
3036:           col  = bcol + kcol;                /* local col index */
3037:           col += rowners_bs[rank+1];      /* global col index */
3038:           for (krow=0; krow<bs; krow++) {
3039:             atmp = PetscAbsScalar(*ba); ba++;
3040:             row  = brow + krow;   /* local row index */
3041:             if (PetscRealPart(va[row]) < atmp) va[row] = atmp;
3042:             if (work[col] < atmp) work[col] = atmp;
3043:           }
3044:         }
3045:         bj++;
3046:       }
3047:     }

3049:     /* send values to its owners */
3050:     for (dest=rank+1; dest<size; dest++) {
3051:       svalues = work + rowners_bs[dest];
3052:       count   = rowners_bs[dest+1]-rowners_bs[dest];
3053:       MPI_Send(svalues,count,MPIU_REAL,dest,rank,PetscObjectComm((PetscObject)A));
3054:     }
3055:   }

3057:   /* receive values */
3058:   if (rank) {
3059:     rvalues = work;
3060:     count   = rowners_bs[rank+1]-rowners_bs[rank];
3061:     for (source=0; source<rank; source++) {
3062:       MPI_Recv(rvalues,count,MPIU_REAL,MPI_ANY_SOURCE,MPI_ANY_TAG,PetscObjectComm((PetscObject)A),&stat);
3063:       /* process values */
3064:       for (i=0; i<count; i++) {
3065:         if (PetscRealPart(va[i]) < rvalues[i]) va[i] = rvalues[i];
3066:       }
3067:     }
3068:   }

3070:   VecRestoreArray(v,&va);
3071:   PetscFree(work);
3072:   return(0);
3073: }

3075: PetscErrorCode MatSOR_MPISBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
3076: {
3077:   Mat_MPISBAIJ      *mat = (Mat_MPISBAIJ*)matin->data;
3078:   PetscErrorCode    ierr;
3079:   PetscInt          mbs=mat->mbs,bs=matin->rmap->bs;
3080:   PetscScalar       *x,*ptr,*from;
3081:   Vec               bb1;
3082:   const PetscScalar *b;

3085:   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
3086:   if (bs > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SSOR for block size > 1 is not yet implemented");

3088:   if (flag == SOR_APPLY_UPPER) {
3089:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
3090:     return(0);
3091:   }

3093:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
3094:     if (flag & SOR_ZERO_INITIAL_GUESS) {
3095:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,lits,xx);
3096:       its--;
3097:     }

3099:     VecDuplicate(bb,&bb1);
3100:     while (its--) {

3102:       /* lower triangular part: slvec0b = - B^T*xx */
3103:       (*mat->B->ops->multtranspose)(mat->B,xx,mat->slvec0b);

3105:       /* copy xx into slvec0a */
3106:       VecGetArray(mat->slvec0,&ptr);
3107:       VecGetArray(xx,&x);
3108:       PetscArraycpy(ptr,x,bs*mbs);
3109:       VecRestoreArray(mat->slvec0,&ptr);

3111:       VecScale(mat->slvec0,-1.0);

3113:       /* copy bb into slvec1a */
3114:       VecGetArray(mat->slvec1,&ptr);
3115:       VecGetArrayRead(bb,&b);
3116:       PetscArraycpy(ptr,b,bs*mbs);
3117:       VecRestoreArray(mat->slvec1,&ptr);

3119:       /* set slvec1b = 0 */
3120:       VecSet(mat->slvec1b,0.0);

3122:       VecScatterBegin(mat->sMvctx,mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD);
3123:       VecRestoreArray(xx,&x);
3124:       VecRestoreArrayRead(bb,&b);
3125:       VecScatterEnd(mat->sMvctx,mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD);

3127:       /* upper triangular part: bb1 = bb1 - B*x */
3128:       (*mat->B->ops->multadd)(mat->B,mat->slvec1b,mat->slvec1a,bb1);

3130:       /* local diagonal sweep */
3131:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
3132:     }
3133:     VecDestroy(&bb1);
3134:   } else if ((flag & SOR_LOCAL_FORWARD_SWEEP) && (its == 1) && (flag & SOR_ZERO_INITIAL_GUESS)) {
3135:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
3136:   } else if ((flag & SOR_LOCAL_BACKWARD_SWEEP) && (its == 1) && (flag & SOR_ZERO_INITIAL_GUESS)) {
3137:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
3138:   } else if (flag & SOR_EISENSTAT) {
3139:     Vec               xx1;
3140:     PetscBool         hasop;
3141:     const PetscScalar *diag;
3142:     PetscScalar       *sl,scale = (omega - 2.0)/omega;
3143:     PetscInt          i,n;

3145:     if (!mat->xx1) {
3146:       VecDuplicate(bb,&mat->xx1);
3147:       VecDuplicate(bb,&mat->bb1);
3148:     }
3149:     xx1 = mat->xx1;
3150:     bb1 = mat->bb1;

3152:     (*mat->A->ops->sor)(mat->A,bb,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_BACKWARD_SWEEP),fshift,lits,1,xx);

3154:     if (!mat->diag) {
3155:       /* this is wrong for same matrix with new nonzero values */
3156:       MatCreateVecs(matin,&mat->diag,NULL);
3157:       MatGetDiagonal(matin,mat->diag);
3158:     }
3159:     MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);

3161:     if (hasop) {
3162:       MatMultDiagonalBlock(matin,xx,bb1);
3163:       VecAYPX(mat->slvec1a,scale,bb);
3164:     } else {
3165:       /*
3166:           These two lines are replaced by code that may be a bit faster for a good compiler
3167:       VecPointwiseMult(mat->slvec1a,mat->diag,xx);
3168:       VecAYPX(mat->slvec1a,scale,bb);
3169:       */
3170:       VecGetArray(mat->slvec1a,&sl);
3171:       VecGetArrayRead(mat->diag,&diag);
3172:       VecGetArrayRead(bb,&b);
3173:       VecGetArray(xx,&x);
3174:       VecGetLocalSize(xx,&n);
3175:       if (omega == 1.0) {
3176:         for (i=0; i<n; i++) sl[i] = b[i] - diag[i]*x[i];
3177:         PetscLogFlops(2.0*n);
3178:       } else {
3179:         for (i=0; i<n; i++) sl[i] = b[i] + scale*diag[i]*x[i];
3180:         PetscLogFlops(3.0*n);
3181:       }
3182:       VecRestoreArray(mat->slvec1a,&sl);
3183:       VecRestoreArrayRead(mat->diag,&diag);
3184:       VecRestoreArrayRead(bb,&b);
3185:       VecRestoreArray(xx,&x);
3186:     }

3188:     /* multiply off-diagonal portion of matrix */
3189:     VecSet(mat->slvec1b,0.0);
3190:     (*mat->B->ops->multtranspose)(mat->B,xx,mat->slvec0b);
3191:     VecGetArray(mat->slvec0,&from);
3192:     VecGetArray(xx,&x);
3193:     PetscArraycpy(from,x,bs*mbs);
3194:     VecRestoreArray(mat->slvec0,&from);
3195:     VecRestoreArray(xx,&x);
3196:     VecScatterBegin(mat->sMvctx,mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD);
3197:     VecScatterEnd(mat->sMvctx,mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD);
3198:     (*mat->B->ops->multadd)(mat->B,mat->slvec1b,mat->slvec1a,mat->slvec1a);

3200:     /* local sweep */
3201:     (*mat->A->ops->sor)(mat->A,mat->slvec1a,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_FORWARD_SWEEP),fshift,lits,1,xx1);
3202:     VecAXPY(xx,1.0,xx1);
3203:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatSORType is not supported for SBAIJ matrix format");
3204:   return(0);
3205: }

3207: PetscErrorCode MatSOR_MPISBAIJ_2comm(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
3208: {
3209:   Mat_MPISBAIJ   *mat = (Mat_MPISBAIJ*)matin->data;
3211:   Vec            lvec1,bb1;

3214:   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
3215:   if (matin->rmap->bs > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SSOR for block size > 1 is not yet implemented");

3217:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
3218:     if (flag & SOR_ZERO_INITIAL_GUESS) {
3219:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,lits,xx);
3220:       its--;
3221:     }

3223:     VecDuplicate(mat->lvec,&lvec1);
3224:     VecDuplicate(bb,&bb1);
3225:     while (its--) {
3226:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

3228:       /* lower diagonal part: bb1 = bb - B^T*xx */
3229:       (*mat->B->ops->multtranspose)(mat->B,xx,lvec1);
3230:       VecScale(lvec1,-1.0);

3232:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
3233:       VecCopy(bb,bb1);
3234:       VecScatterBegin(mat->Mvctx,lvec1,bb1,ADD_VALUES,SCATTER_REVERSE);

3236:       /* upper diagonal part: bb1 = bb1 - B*x */
3237:       VecScale(mat->lvec,-1.0);
3238:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb1,bb1);

3240:       VecScatterEnd(mat->Mvctx,lvec1,bb1,ADD_VALUES,SCATTER_REVERSE);

3242:       /* diagonal sweep */
3243:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
3244:     }
3245:     VecDestroy(&lvec1);
3246:     VecDestroy(&bb1);
3247:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatSORType is not supported for SBAIJ matrix format");
3248:   return(0);
3249: }

3251: /*@
3252:      MatCreateMPISBAIJWithArrays - creates a MPI SBAIJ matrix using arrays that contain in standard
3253:          CSR format the local rows.

3255:    Collective

3257:    Input Parameters:
3258: +  comm - MPI communicator
3259: .  bs - the block size, only a block size of 1 is supported
3260: .  m - number of local rows (Cannot be PETSC_DECIDE)
3261: .  n - This value should be the same as the local size used in creating the
3262:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
3263:        calculated if N is given) For square matrices n is almost always m.
3264: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3265: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
3266: .   i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of block elements in that row block row of the matrix
3267: .   j - column indices
3268: -   a - matrix values

3270:    Output Parameter:
3271: .   mat - the matrix

3273:    Level: intermediate

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

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

3282: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
3283:           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
3284: @*/
3285: PetscErrorCode  MatCreateMPISBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
3286: {


3291:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
3292:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
3293:   MatCreate(comm,mat);
3294:   MatSetSizes(*mat,m,n,M,N);
3295:   MatSetType(*mat,MATMPISBAIJ);
3296:   MatMPISBAIJSetPreallocationCSR(*mat,bs,i,j,a);
3297:   return(0);
3298: }


3301: /*@C
3302:    MatMPISBAIJSetPreallocationCSR - Creates a sparse parallel matrix in SBAIJ format using the given nonzero structure and (optional) numerical values

3304:    Collective

3306:    Input Parameters:
3307: +  B - the matrix
3308: .  bs - the block size
3309: .  i - the indices into j for the start of each local row (starts with zero)
3310: .  j - the column indices for each local row (starts with zero) these must be sorted for each row
3311: -  v - optional values in the matrix

3313:    Level: advanced

3315:    Notes:
3316:    Though this routine has Preallocation() in the name it also sets the exact nonzero locations of the matrix entries
3317:    and usually the numerical values as well

3319:    Any entries below the diagonal are ignored

3321: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ
3322: @*/
3323: PetscErrorCode  MatMPISBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3324: {

3328:   PetscTryMethod(B,"MatMPISBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));
3329:   return(0);
3330: }

3332: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPISBAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
3333: {
3335:   PetscInt       m,N,i,rstart,nnz,Ii,bs,cbs;
3336:   PetscInt       *indx;
3337:   PetscScalar    *values;

3340:   MatGetSize(inmat,&m,&N);
3341:   if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
3342:     Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)inmat->data;
3343:     PetscInt       *dnz,*onz,mbs,Nbs,nbs;
3344:     PetscInt       *bindx,rmax=a->rmax,j;
3345:     PetscMPIInt    rank,size;

3347:     MatGetBlockSizes(inmat,&bs,&cbs);
3348:     mbs = m/bs; Nbs = N/cbs;
3349:     if (n == PETSC_DECIDE) {
3350:       PetscSplitOwnershipBlock(comm,cbs,&n,&N);
3351:     }
3352:     nbs = n/cbs;

3354:     PetscMalloc1(rmax,&bindx);
3355:     MatPreallocateInitialize(comm,mbs,nbs,dnz,onz); /* inline function, output __end and __rstart are used below */

3357:     MPI_Comm_rank(comm,&rank);
3358:     MPI_Comm_rank(comm,&size);
3359:     if (rank == size-1) {
3360:       /* Check sum(nbs) = Nbs */
3361:       if (__end != Nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local block columns %D != global block columns %D",__end,Nbs);
3362:     }

3364:     rstart = __rstart; /* block rstart of *outmat; see inline function MatPreallocateInitialize */
3365:     MatSetOption(inmat,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE);
3366:     for (i=0; i<mbs; i++) {
3367:       MatGetRow_SeqSBAIJ(inmat,i*bs,&nnz,&indx,NULL); /* non-blocked nnz and indx */
3368:       nnz  = nnz/bs;
3369:       for (j=0; j<nnz; j++) bindx[j] = indx[j*bs]/bs;
3370:       MatPreallocateSet(i+rstart,nnz,bindx,dnz,onz);
3371:       MatRestoreRow_SeqSBAIJ(inmat,i*bs,&nnz,&indx,NULL);
3372:     }
3373:     MatSetOption(inmat,MAT_GETROW_UPPERTRIANGULAR,PETSC_FALSE);
3374:     PetscFree(bindx);

3376:     MatCreate(comm,outmat);
3377:     MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
3378:     MatSetBlockSizes(*outmat,bs,cbs);
3379:     MatSetType(*outmat,MATSBAIJ);
3380:     MatSeqSBAIJSetPreallocation(*outmat,bs,0,dnz);
3381:     MatMPISBAIJSetPreallocation(*outmat,bs,0,dnz,0,onz);
3382:     MatPreallocateFinalize(dnz,onz);
3383:   }

3385:   /* numeric phase */
3386:   MatGetBlockSizes(inmat,&bs,&cbs);
3387:   MatGetOwnershipRange(*outmat,&rstart,NULL);

3389:   MatSetOption(inmat,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE);
3390:   for (i=0; i<m; i++) {
3391:     MatGetRow_SeqSBAIJ(inmat,i,&nnz,&indx,&values);
3392:     Ii   = i + rstart;
3393:     MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);
3394:     MatRestoreRow_SeqSBAIJ(inmat,i,&nnz,&indx,&values);
3395:   }
3396:   MatSetOption(inmat,MAT_GETROW_UPPERTRIANGULAR,PETSC_FALSE);
3397:   MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);
3398:   MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);
3399:   return(0);
3400: }