Actual source code: mpisbaij.c

petsc-master 2015-09-04
Report Typos and Errors
  2: #include <../src/mat/impls/baij/mpi/mpibaij.h>    /*I "petscmat.h" I*/
  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_EXTERN PetscErrorCode MatConvert_MPISBAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
  9: #endif
 12: PetscErrorCode  MatStoreValues_MPISBAIJ(Mat mat)
 13: {
 14:   Mat_MPISBAIJ   *aij = (Mat_MPISBAIJ*)mat->data;

 18:   MatStoreValues(aij->A);
 19:   MatStoreValues(aij->B);
 20:   return(0);
 21: }

 25: PetscErrorCode  MatRetrieveValues_MPISBAIJ(Mat mat)
 26: {
 27:   Mat_MPISBAIJ   *aij = (Mat_MPISBAIJ*)mat->data;

 31:   MatRetrieveValues(aij->A);
 32:   MatRetrieveValues(aij->B);
 33:   return(0);
 34: }

 36: #define  MatSetValues_SeqSBAIJ_A_Private(row,col,value,addv,orow,ocol)      \
 37:   { \
 38:  \
 39:     brow = row/bs;  \
 40:     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
 41:     rmax = aimax[brow]; nrow = ailen[brow]; \
 42:     bcol = col/bs; \
 43:     ridx = row % bs; cidx = col % bs; \
 44:     low  = 0; high = nrow; \
 45:     while (high-low > 3) { \
 46:       t = (low+high)/2; \
 47:       if (rp[t] > bcol) high = t; \
 48:       else              low  = t; \
 49:     } \
 50:     for (_i=low; _i<high; _i++) { \
 51:       if (rp[_i] > bcol) break; \
 52:       if (rp[_i] == bcol) { \
 53:         bap = ap + bs2*_i + bs*cidx + ridx; \
 54:         if (addv == ADD_VALUES) *bap += value;  \
 55:         else                    *bap  = value;  \
 56:         goto a_noinsert; \
 57:       } \
 58:     } \
 59:     if (a->nonew == 1) goto a_noinsert; \
 60:     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); \
 61:     MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,aimax,a->nonew,MatScalar); \
 62:     N = nrow++ - 1;  \
 63:     /* shift up all the later entries in this row */ \
 64:     for (ii=N; ii>=_i; ii--) { \
 65:       rp[ii+1] = rp[ii]; \
 66:       PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar)); \
 67:     } \
 68:     if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar)); }  \
 69:     rp[_i]                      = bcol;  \
 70:     ap[bs2*_i + bs*cidx + ridx] = value;  \
 71:     A->nonzerostate++;\
 72: a_noinsert:; \
 73:     ailen[brow] = nrow; \
 74:   }

 76: #define  MatSetValues_SeqSBAIJ_B_Private(row,col,value,addv,orow,ocol) \
 77:   { \
 78:     brow = row/bs;  \
 79:     rp   = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
 80:     rmax = bimax[brow]; nrow = bilen[brow]; \
 81:     bcol = col/bs; \
 82:     ridx = row % bs; cidx = col % bs; \
 83:     low  = 0; high = nrow; \
 84:     while (high-low > 3) { \
 85:       t = (low+high)/2; \
 86:       if (rp[t] > bcol) high = t; \
 87:       else              low  = t; \
 88:     } \
 89:     for (_i=low; _i<high; _i++) { \
 90:       if (rp[_i] > bcol) break; \
 91:       if (rp[_i] == bcol) { \
 92:         bap = ap + bs2*_i + bs*cidx + ridx; \
 93:         if (addv == ADD_VALUES) *bap += value;  \
 94:         else                    *bap  = value;  \
 95:         goto b_noinsert; \
 96:       } \
 97:     } \
 98:     if (b->nonew == 1) goto b_noinsert; \
 99:     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); \
100:     MatSeqXAIJReallocateAIJ(B,b->mbs,bs2,nrow,brow,bcol,rmax,ba,bi,bj,rp,ap,bimax,b->nonew,MatScalar); \
101:     N = nrow++ - 1;  \
102:     /* shift up all the later entries in this row */ \
103:     for (ii=N; ii>=_i; ii--) { \
104:       rp[ii+1] = rp[ii]; \
105:       PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar)); \
106:     } \
107:     if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));}  \
108:     rp[_i]                      = bcol;  \
109:     ap[bs2*_i + bs*cidx + ridx] = value;  \
110:     B->nonzerostate++;\
111: b_noinsert:; \
112:     bilen[brow] = nrow; \
113:   }

115: /* Only add/insert a(i,j) with i<=j (blocks).
116:    Any a(i,j) with i>j input by user is ingored.
117: */
120: PetscErrorCode MatSetValues_MPISBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
121: {
122:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
123:   MatScalar      value;
124:   PetscBool      roworiented = baij->roworiented;
126:   PetscInt       i,j,row,col;
127:   PetscInt       rstart_orig=mat->rmap->rstart;
128:   PetscInt       rend_orig  =mat->rmap->rend,cstart_orig=mat->cmap->rstart;
129:   PetscInt       cend_orig  =mat->cmap->rend,bs=mat->rmap->bs;

131:   /* Some Variables required in the macro */
132:   Mat          A     = baij->A;
133:   Mat_SeqSBAIJ *a    = (Mat_SeqSBAIJ*)(A)->data;
134:   PetscInt     *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
135:   MatScalar    *aa   =a->a;

137:   Mat         B     = baij->B;
138:   Mat_SeqBAIJ *b    = (Mat_SeqBAIJ*)(B)->data;
139:   PetscInt    *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
140:   MatScalar   *ba   =b->a;

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

146:   /* for stash */
147:   PetscInt  n_loc, *in_loc = NULL;
148:   MatScalar *v_loc = NULL;

151:   if (!baij->donotstash) {
152:     if (n > baij->n_loc) {
153:       PetscFree(baij->in_loc);
154:       PetscFree(baij->v_loc);
155:       PetscMalloc1(n,&baij->in_loc);
156:       PetscMalloc1(n,&baij->v_loc);

158:       baij->n_loc = n;
159:     }
160:     in_loc = baij->in_loc;
161:     v_loc  = baij->v_loc;
162:   }

164:   for (i=0; i<m; i++) {
165:     if (im[i] < 0) continue;
166: #if defined(PETSC_USE_DEBUG)
167:     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);
168: #endif
169:     if (im[i] >= rstart_orig && im[i] < rend_orig) { /* this processor entry */
170:       row = im[i] - rstart_orig;              /* local row index */
171:       for (j=0; j<n; j++) {
172:         if (im[i]/bs > in[j]/bs) {
173:           if (a->ignore_ltriangular) {
174:             continue;    /* ignore lower triangular blocks */
175:           } 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)");
176:         }
177:         if (in[j] >= cstart_orig && in[j] < cend_orig) {  /* diag entry (A) */
178:           col  = in[j] - cstart_orig;         /* local col index */
179:           brow = row/bs; bcol = col/bs;
180:           if (brow > bcol) continue;  /* ignore lower triangular blocks of A */
181:           if (roworiented) value = v[i*n+j];
182:           else             value = v[i+j*m];
183:           MatSetValues_SeqSBAIJ_A_Private(row,col,value,addv,im[i],in[j]);
184:           /* MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv); */
185:         } else if (in[j] < 0) continue;
186: #if defined(PETSC_USE_DEBUG)
187:         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);
188: #endif
189:         else {  /* off-diag entry (B) */
190:           if (mat->was_assembled) {
191:             if (!baij->colmap) {
192:               MatCreateColmap_MPIBAIJ_Private(mat);
193:             }
194: #if defined(PETSC_USE_CTABLE)
195:             PetscTableFind(baij->colmap,in[j]/bs + 1,&col);
196:             col  = col - 1;
197: #else
198:             col = baij->colmap[in[j]/bs] - 1;
199: #endif
200:             if (col < 0 && !((Mat_SeqSBAIJ*)(baij->A->data))->nonew) {
201:               MatDisAssemble_MPISBAIJ(mat);
202:               col  =  in[j];
203:               /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
204:               B    = baij->B;
205:               b    = (Mat_SeqBAIJ*)(B)->data;
206:               bimax= b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
207:               ba   = b->a;
208:             } else col += in[j]%bs;
209:           } else col = in[j];
210:           if (roworiented) value = v[i*n+j];
211:           else             value = v[i+j*m];
212:           MatSetValues_SeqSBAIJ_B_Private(row,col,value,addv,im[i],in[j]);
213:           /* MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv); */
214:         }
215:       }
216:     } else {  /* off processor entry */
217:       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]);
218:       if (!baij->donotstash) {
219:         mat->assembled = PETSC_FALSE;
220:         n_loc          = 0;
221:         for (j=0; j<n; j++) {
222:           if (im[i]/bs > in[j]/bs) continue; /* ignore lower triangular blocks */
223:           in_loc[n_loc] = in[j];
224:           if (roworiented) {
225:             v_loc[n_loc] = v[i*n+j];
226:           } else {
227:             v_loc[n_loc] = v[j*m+i];
228:           }
229:           n_loc++;
230:         }
231:         MatStashValuesRow_Private(&mat->stash,im[i],n_loc,in_loc,v_loc,PETSC_FALSE);
232:       }
233:     }
234:   }
235:   return(0);
236: }

240: PETSC_STATIC_INLINE PetscErrorCode MatSetValuesBlocked_SeqSBAIJ_Inlined(Mat A,PetscInt row,PetscInt col,const PetscScalar v[],InsertMode is,PetscInt orow,PetscInt ocol)
241: {
242:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
243:   PetscErrorCode    ierr;
244:   PetscInt          *rp,low,high,t,ii,jj,nrow,i,rmax,N;
245:   PetscInt          *imax      =a->imax,*ai=a->i,*ailen=a->ilen;
246:   PetscInt          *aj        =a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs;
247:   PetscBool         roworiented=a->roworiented;
248:   const PetscScalar *value     = v;
249:   MatScalar         *ap,*aa = a->a,*bap;

252:   if (col < row) {
253:     if (a->ignore_ltriangular) return(0); /* ignore lower triangular block */
254:     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)");
255:   }
256:   rp   = aj + ai[row];
257:   ap   = aa + bs2*ai[row];
258:   rmax = imax[row];
259:   nrow = ailen[row];
260:   value = v;
261:   low   = 0;
262:   high  = nrow;

264:   while (high-low > 7) {
265:     t = (low+high)/2;
266:     if (rp[t] > col) high = t;
267:     else             low  = t;
268:   }
269:   for (i=low; i<high; i++) {
270:     if (rp[i] > col) break;
271:     if (rp[i] == col) {
272:       bap = ap +  bs2*i;
273:       if (roworiented) {
274:         if (is == ADD_VALUES) {
275:           for (ii=0; ii<bs; ii++) {
276:             for (jj=ii; jj<bs2; jj+=bs) {
277:               bap[jj] += *value++;
278:             }
279:           }
280:         } else {
281:           for (ii=0; ii<bs; ii++) {
282:             for (jj=ii; jj<bs2; jj+=bs) {
283:               bap[jj] = *value++;
284:             }
285:           }
286:         }
287:       } else {
288:         if (is == ADD_VALUES) {
289:           for (ii=0; ii<bs; ii++) {
290:             for (jj=0; jj<bs; jj++) {
291:               *bap++ += *value++;
292:             }
293:           }
294:         } else {
295:           for (ii=0; ii<bs; ii++) {
296:             for (jj=0; jj<bs; jj++) {
297:               *bap++  = *value++;
298:             }
299:           }
300:         }
301:       }
302:       goto noinsert2;
303:     }
304:   }
305:   if (nonew == 1) goto noinsert2;
306:   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);
307:   MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
308:   N = nrow++ - 1; high++;
309:   /* shift up all the later entries in this row */
310:   for (ii=N; ii>=i; ii--) {
311:     rp[ii+1] = rp[ii];
312:     PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));
313:   }
314:   if (N >= i) {
315:     PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));
316:   }
317:   rp[i] = col;
318:   bap   = ap +  bs2*i;
319:   if (roworiented) {
320:     for (ii=0; ii<bs; ii++) {
321:       for (jj=ii; jj<bs2; jj+=bs) {
322:         bap[jj] = *value++;
323:       }
324:     }
325:   } else {
326:     for (ii=0; ii<bs; ii++) {
327:       for (jj=0; jj<bs; jj++) {
328:         *bap++ = *value++;
329:       }
330:     }
331:   }
332:   noinsert2:;
333:   ailen[row] = nrow;
334:   return(0);
335: }

339: /*
340:    This routine is exactly duplicated in mpibaij.c
341: */
342: PETSC_STATIC_INLINE PetscErrorCode MatSetValuesBlocked_SeqBAIJ_Inlined(Mat A,PetscInt row,PetscInt col,const PetscScalar v[],InsertMode is,PetscInt orow,PetscInt ocol)
343: {
344:   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
345:   PetscInt          *rp,low,high,t,ii,jj,nrow,i,rmax,N;
346:   PetscInt          *imax=a->imax,*ai=a->i,*ailen=a->ilen;
347:   PetscErrorCode    ierr;
348:   PetscInt          *aj        =a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs;
349:   PetscBool         roworiented=a->roworiented;
350:   const PetscScalar *value     = v;
351:   MatScalar         *ap,*aa = a->a,*bap;

354:   rp   = aj + ai[row];
355:   ap   = aa + bs2*ai[row];
356:   rmax = imax[row];
357:   nrow = ailen[row];
358:   low  = 0;
359:   high = nrow;
360:   value = v;
361:   while (high-low > 7) {
362:     t = (low+high)/2;
363:     if (rp[t] > col) high = t;
364:     else             low  = t;
365:   }
366:   for (i=low; i<high; i++) {
367:     if (rp[i] > col) break;
368:     if (rp[i] == col) {
369:       bap = ap +  bs2*i;
370:       if (roworiented) {
371:         if (is == ADD_VALUES) {
372:           for (ii=0; ii<bs; ii++) {
373:             for (jj=ii; jj<bs2; jj+=bs) {
374:               bap[jj] += *value++;
375:             }
376:           }
377:         } else {
378:           for (ii=0; ii<bs; ii++) {
379:             for (jj=ii; jj<bs2; jj+=bs) {
380:               bap[jj] = *value++;
381:             }
382:           }
383:         }
384:       } else {
385:         if (is == ADD_VALUES) {
386:           for (ii=0; ii<bs; ii++,value+=bs) {
387:             for (jj=0; jj<bs; jj++) {
388:               bap[jj] += value[jj];
389:             }
390:             bap += bs;
391:           }
392:         } else {
393:           for (ii=0; ii<bs; ii++,value+=bs) {
394:             for (jj=0; jj<bs; jj++) {
395:               bap[jj]  = value[jj];
396:             }
397:             bap += bs;
398:           }
399:         }
400:       }
401:       goto noinsert2;
402:     }
403:   }
404:   if (nonew == 1) goto noinsert2;
405:   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);
406:   MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
407:   N = nrow++ - 1; high++;
408:   /* shift up all the later entries in this row */
409:   for (ii=N; ii>=i; ii--) {
410:     rp[ii+1] = rp[ii];
411:     PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));
412:   }
413:   if (N >= i) {
414:     PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));
415:   }
416:   rp[i] = col;
417:   bap   = ap +  bs2*i;
418:   if (roworiented) {
419:     for (ii=0; ii<bs; ii++) {
420:       for (jj=ii; jj<bs2; jj+=bs) {
421:         bap[jj] = *value++;
422:       }
423:     }
424:   } else {
425:     for (ii=0; ii<bs; ii++) {
426:       for (jj=0; jj<bs; jj++) {
427:         *bap++ = *value++;
428:       }
429:     }
430:   }
431:   noinsert2:;
432:   ailen[row] = nrow;
433:   return(0);
434: }

438: /*
439:     This routine could be optimized by removing the need for the block copy below and passing stride information
440:   to the above inline routines; similarly in MatSetValuesBlocked_MPIBAIJ()
441: */
442: PetscErrorCode MatSetValuesBlocked_MPISBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const MatScalar v[],InsertMode addv)
443: {
444:   Mat_MPISBAIJ    *baij = (Mat_MPISBAIJ*)mat->data;
445:   const MatScalar *value;
446:   MatScalar       *barray     =baij->barray;
447:   PetscBool       roworiented = baij->roworiented,ignore_ltriangular = ((Mat_SeqSBAIJ*)baij->A->data)->ignore_ltriangular;
448:   PetscErrorCode  ierr;
449:   PetscInt        i,j,ii,jj,row,col,rstart=baij->rstartbs;
450:   PetscInt        rend=baij->rendbs,cstart=baij->rstartbs,stepval;
451:   PetscInt        cend=baij->rendbs,bs=mat->rmap->bs,bs2=baij->bs2;

454:   if (!barray) {
455:     PetscMalloc1(bs2,&barray);
456:     baij->barray = barray;
457:   }

459:   if (roworiented) {
460:     stepval = (n-1)*bs;
461:   } else {
462:     stepval = (m-1)*bs;
463:   }
464:   for (i=0; i<m; i++) {
465:     if (im[i] < 0) continue;
466: #if defined(PETSC_USE_DEBUG)
467:     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);
468: #endif
469:     if (im[i] >= rstart && im[i] < rend) {
470:       row = im[i] - rstart;
471:       for (j=0; j<n; j++) {
472:         if (im[i] > in[j]) {
473:           if (ignore_ltriangular) continue; /* ignore lower triangular blocks */
474:           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)");
475:         }
476:         /* If NumCol = 1 then a copy is not required */
477:         if ((roworiented) && (n == 1)) {
478:           barray = (MatScalar*) v + i*bs2;
479:         } else if ((!roworiented) && (m == 1)) {
480:           barray = (MatScalar*) v + j*bs2;
481:         } else { /* Here a copy is required */
482:           if (roworiented) {
483:             value = v + i*(stepval+bs)*bs + j*bs;
484:           } else {
485:             value = v + j*(stepval+bs)*bs + i*bs;
486:           }
487:           for (ii=0; ii<bs; ii++,value+=stepval) {
488:             for (jj=0; jj<bs; jj++) {
489:               *barray++ = *value++;
490:             }
491:           }
492:           barray -=bs2;
493:         }

495:         if (in[j] >= cstart && in[j] < cend) {
496:           col  = in[j] - cstart;
497:           MatSetValuesBlocked_SeqSBAIJ_Inlined(baij->A,row,col,barray,addv,im[i],in[j]);
498:         } else if (in[j] < 0) continue;
499: #if defined(PETSC_USE_DEBUG)
500:         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);
501: #endif
502:         else {
503:           if (mat->was_assembled) {
504:             if (!baij->colmap) {
505:               MatCreateColmap_MPIBAIJ_Private(mat);
506:             }

508: #if defined(PETSC_USE_DEBUG)
509: #if defined(PETSC_USE_CTABLE)
510:             { PetscInt data;
511:               PetscTableFind(baij->colmap,in[j]+1,&data);
512:               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
513:             }
514: #else
515:             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
516: #endif
517: #endif
518: #if defined(PETSC_USE_CTABLE)
519:             PetscTableFind(baij->colmap,in[j]+1,&col);
520:             col  = (col - 1)/bs;
521: #else
522:             col = (baij->colmap[in[j]] - 1)/bs;
523: #endif
524:             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
525:               MatDisAssemble_MPISBAIJ(mat);
526:               col  = in[j];
527:             }
528:           } else col = in[j];
529:           MatSetValuesBlocked_SeqBAIJ_Inlined(baij->B,row,col,barray,addv,im[i],in[j]);
530:         }
531:       }
532:     } else {
533:       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]);
534:       if (!baij->donotstash) {
535:         if (roworiented) {
536:           MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
537:         } else {
538:           MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
539:         }
540:       }
541:     }
542:   }
543:   return(0);
544: }

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

556:   for (i=0; i<m; i++) {
557:     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]); */
558:     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);
559:     if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
560:       row = idxm[i] - bsrstart;
561:       for (j=0; j<n; j++) {
562:         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column %D",idxn[j]); */
563:         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);
564:         if (idxn[j] >= bscstart && idxn[j] < bscend) {
565:           col  = idxn[j] - bscstart;
566:           MatGetValues_SeqSBAIJ(baij->A,1,&row,1,&col,v+i*n+j);
567:         } else {
568:           if (!baij->colmap) {
569:             MatCreateColmap_MPIBAIJ_Private(mat);
570:           }
571: #if defined(PETSC_USE_CTABLE)
572:           PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);
573:           data--;
574: #else
575:           data = baij->colmap[idxn[j]/bs]-1;
576: #endif
577:           if ((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
578:           else {
579:             col  = data + idxn[j]%bs;
580:             MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);
581:           }
582:         }
583:       }
584:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
585:   }
586:   return(0);
587: }

591: PetscErrorCode MatNorm_MPISBAIJ(Mat mat,NormType type,PetscReal *norm)
592: {
593:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
595:   PetscReal      sum[2],*lnorm2;

598:   if (baij->size == 1) {
599:      MatNorm(baij->A,type,norm);
600:   } else {
601:     if (type == NORM_FROBENIUS) {
602:       PetscMalloc1(2,&lnorm2);
603:        MatNorm(baij->A,type,lnorm2);
604:       *lnorm2 = (*lnorm2)*(*lnorm2); lnorm2++;            /* squar power of norm(A) */
605:        MatNorm(baij->B,type,lnorm2);
606:       *lnorm2 = (*lnorm2)*(*lnorm2); lnorm2--;             /* squar power of norm(B) */
607:       MPI_Allreduce(lnorm2,sum,2,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
608:       *norm   = PetscSqrtReal(sum[0] + 2*sum[1]);
609:       PetscFree(lnorm2);
610:     } else if (type == NORM_INFINITY || type == NORM_1) { /* max row/column sum */
611:       Mat_SeqSBAIJ *amat=(Mat_SeqSBAIJ*)baij->A->data;
612:       Mat_SeqBAIJ  *bmat=(Mat_SeqBAIJ*)baij->B->data;
613:       PetscReal    *rsum,*rsum2,vabs;
614:       PetscInt     *jj,*garray=baij->garray,rstart=baij->rstartbs,nz;
615:       PetscInt     brow,bcol,col,bs=baij->A->rmap->bs,row,grow,gcol,mbs=amat->mbs;
616:       MatScalar    *v;

618:       PetscMalloc2(mat->cmap->N,&rsum,mat->cmap->N,&rsum2);
619:       PetscMemzero(rsum,mat->cmap->N*sizeof(PetscReal));
620:       /* Amat */
621:       v = amat->a; jj = amat->j;
622:       for (brow=0; brow<mbs; brow++) {
623:         grow = bs*(rstart + brow);
624:         nz   = amat->i[brow+1] - amat->i[brow];
625:         for (bcol=0; bcol<nz; bcol++) {
626:           gcol = bs*(rstart + *jj); jj++;
627:           for (col=0; col<bs; col++) {
628:             for (row=0; row<bs; row++) {
629:               vabs            = PetscAbsScalar(*v); v++;
630:               rsum[gcol+col] += vabs;
631:               /* non-diagonal block */
632:               if (bcol > 0 && vabs > 0.0) rsum[grow+row] += vabs;
633:             }
634:           }
635:         }
636:       }
637:       /* Bmat */
638:       v = bmat->a; jj = bmat->j;
639:       for (brow=0; brow<mbs; brow++) {
640:         grow = bs*(rstart + brow);
641:         nz = bmat->i[brow+1] - bmat->i[brow];
642:         for (bcol=0; bcol<nz; bcol++) {
643:           gcol = bs*garray[*jj]; jj++;
644:           for (col=0; col<bs; col++) {
645:             for (row=0; row<bs; row++) {
646:               vabs            = PetscAbsScalar(*v); v++;
647:               rsum[gcol+col] += vabs;
648:               rsum[grow+row] += vabs;
649:             }
650:           }
651:         }
652:       }
653:       MPI_Allreduce(rsum,rsum2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
654:       *norm = 0.0;
655:       for (col=0; col<mat->cmap->N; col++) {
656:         if (rsum2[col] > *norm) *norm = rsum2[col];
657:       }
658:       PetscFree2(rsum,rsum2);
659:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for this norm yet");
660:   }
661:   return(0);
662: }

666: PetscErrorCode MatAssemblyBegin_MPISBAIJ(Mat mat,MatAssemblyType mode)
667: {
668:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
670:   PetscInt       nstash,reallocs;
671:   InsertMode     addv;

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

676:   /* make sure all processors are either in INSERTMODE or ADDMODE */
677:   MPI_Allreduce((PetscEnum*)&mat->insertmode,(PetscEnum*)&addv,1,MPIU_ENUM,MPI_BOR,PetscObjectComm((PetscObject)mat));
678:   if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
679:   mat->insertmode = addv; /* in case this processor had no cache */

681:   MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
682:   MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);
683:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
684:   PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);
685:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
686:   PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
687:   return(0);
688: }

692: PetscErrorCode MatAssemblyEnd_MPISBAIJ(Mat mat,MatAssemblyType mode)
693: {
694:   Mat_MPISBAIJ   *baij=(Mat_MPISBAIJ*)mat->data;
695:   Mat_SeqSBAIJ   *a   =(Mat_SeqSBAIJ*)baij->A->data;
697:   PetscInt       i,j,rstart,ncols,flg,bs2=baij->bs2;
698:   PetscInt       *row,*col;
699:   PetscBool      other_disassembled;
700:   PetscMPIInt    n;
701:   PetscBool      r1,r2,r3;
702:   MatScalar      *val;
703:   InsertMode     addv = mat->insertmode;

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

712:       for (i=0; i<n;) {
713:         /* Now identify the consecutive vals belonging to the same row */
714:         for (j=i,rstart=row[j]; j<n; j++) {
715:           if (row[j] != rstart) break;
716:         }
717:         if (j < n) ncols = j-i;
718:         else       ncols = n-i;
719:         /* Now assemble all these values with a single function call */
720:         MatSetValues_MPISBAIJ(mat,1,row+i,ncols,col+i,val+i,addv);
721:         i    = j;
722:       }
723:     }
724:     MatStashScatterEnd_Private(&mat->stash);
725:     /* Now process the block-stash. Since the values are stashed column-oriented,
726:        set the roworiented flag to column oriented, and after MatSetValues()
727:        restore the original flags */
728:     r1 = baij->roworiented;
729:     r2 = a->roworiented;
730:     r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented;

732:     baij->roworiented = PETSC_FALSE;
733:     a->roworiented    = PETSC_FALSE;

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

740:       for (i=0; i<n;) {
741:         /* Now identify the consecutive vals belonging to the same row */
742:         for (j=i,rstart=row[j]; j<n; j++) {
743:           if (row[j] != rstart) break;
744:         }
745:         if (j < n) ncols = j-i;
746:         else       ncols = n-i;
747:         MatSetValuesBlocked_MPISBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,addv);
748:         i    = j;
749:       }
750:     }
751:     MatStashScatterEnd_Private(&mat->bstash);

753:     baij->roworiented = r1;
754:     a->roworiented    = r2;

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

759:   MatAssemblyBegin(baij->A,mode);
760:   MatAssemblyEnd(baij->A,mode);

762:   /* determine if any processor has disassembled, if so we must
763:      also disassemble ourselfs, in order that we may reassemble. */
764:   /*
765:      if nonzero structure of submatrix B cannot change then we know that
766:      no processor disassembled thus we can skip this stuff
767:   */
768:   if (!((Mat_SeqBAIJ*)baij->B->data)->nonew) {
769:     MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));
770:     if (mat->was_assembled && !other_disassembled) {
771:       MatDisAssemble_MPISBAIJ(mat);
772:     }
773:   }

775:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
776:     MatSetUpMultiply_MPISBAIJ(mat); /* setup Mvctx and sMvctx */
777:   }
778:   MatAssemblyBegin(baij->B,mode);
779:   MatAssemblyEnd(baij->B,mode);

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

783:   baij->rowvalues = 0;

785:   /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
786:   if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
787:     PetscObjectState state = baij->A->nonzerostate + baij->B->nonzerostate;
788:     MPI_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));
789:   }
790:   return(0);
791: }

793: extern PetscErrorCode MatView_SeqSBAIJ_ASCII(Mat,PetscViewer);
794: extern PetscErrorCode MatSetValues_MPIBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
795: #include <petscdraw.h>
798: static PetscErrorCode MatView_MPISBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
799: {
800:   Mat_MPISBAIJ      *baij = (Mat_MPISBAIJ*)mat->data;
801:   PetscErrorCode    ierr;
802:   PetscInt          bs   = mat->rmap->bs;
803:   PetscMPIInt       rank = baij->rank;
804:   PetscBool         iascii,isdraw;
805:   PetscViewer       sviewer;
806:   PetscViewerFormat format;

809:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
810:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
811:   if (iascii) {
812:     PetscViewerGetFormat(viewer,&format);
813:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
814:       MatInfo info;
815:       MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
816:       MatGetInfo(mat,MAT_LOCAL,&info);
817:       PetscViewerASCIIPushSynchronized(viewer);
818:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D bs %D mem %D\n",rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,mat->rmap->bs,(PetscInt)info.memory);
819:       MatGetInfo(baij->A,MAT_LOCAL,&info);
820:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
821:       MatGetInfo(baij->B,MAT_LOCAL,&info);
822:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
823:       PetscViewerFlush(viewer);
824:       PetscViewerASCIIPopSynchronized(viewer);
825:       PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");
826:       VecScatterView(baij->Mvctx,viewer);
827:       return(0);
828:     } else if (format == PETSC_VIEWER_ASCII_INFO) {
829:       PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);
830:       return(0);
831:     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
832:       return(0);
833:     }
834:   }

836:   if (isdraw) {
837:     PetscDraw draw;
838:     PetscBool isnull;
839:     PetscViewerDrawGetDraw(viewer,0,&draw);
840:     PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
841:   }

843:   {
844:     /* assemble the entire matrix onto first processor. */
845:     Mat          A;
846:     Mat_SeqSBAIJ *Aloc;
847:     Mat_SeqBAIJ  *Bloc;
848:     PetscInt     M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
849:     MatScalar    *a;
850:     const char   *matname;

852:     /* Should this be the same type as mat? */
853:     MatCreate(PetscObjectComm((PetscObject)mat),&A);
854:     if (!rank) {
855:       MatSetSizes(A,M,N,M,N);
856:     } else {
857:       MatSetSizes(A,0,0,M,N);
858:     }
859:     MatSetType(A,MATMPISBAIJ);
860:     MatMPISBAIJSetPreallocation(A,mat->rmap->bs,0,NULL,0,NULL);
861:     MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
862:     PetscLogObjectParent((PetscObject)mat,(PetscObject)A);

864:     /* copy over the A part */
865:     Aloc = (Mat_SeqSBAIJ*)baij->A->data;
866:     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
867:     PetscMalloc1(bs,&rvals);

869:     for (i=0; i<mbs; i++) {
870:       rvals[0] = bs*(baij->rstartbs + i);
871:       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
872:       for (j=ai[i]; j<ai[i+1]; j++) {
873:         col = (baij->cstartbs+aj[j])*bs;
874:         for (k=0; k<bs; k++) {
875:           MatSetValues_MPISBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
876:           col++;
877:           a += bs;
878:         }
879:       }
880:     }
881:     /* copy over the B part */
882:     Bloc = (Mat_SeqBAIJ*)baij->B->data;
883:     ai   = Bloc->i; aj = Bloc->j; a = Bloc->a;
884:     for (i=0; i<mbs; i++) {

886:       rvals[0] = bs*(baij->rstartbs + i);
887:       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
888:       for (j=ai[i]; j<ai[i+1]; j++) {
889:         col = baij->garray[aj[j]]*bs;
890:         for (k=0; k<bs; k++) {
891:           MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
892:           col++;
893:           a += bs;
894:         }
895:       }
896:     }
897:     PetscFree(rvals);
898:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
899:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
900:     /*
901:        Everyone has to call to draw the matrix since the graphics waits are
902:        synchronized across all processors that share the PetscDraw object
903:     */
904:     PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
905:     PetscObjectGetName((PetscObject)mat,&matname);
906:     if (!rank) {
907:       PetscObjectSetName((PetscObject)((Mat_MPISBAIJ*)(A->data))->A,matname);
908:       MatView_SeqSBAIJ_ASCII(((Mat_MPISBAIJ*)(A->data))->A,sviewer);
909:     }
910:     PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
911:     PetscViewerFlush(viewer);
912:     MatDestroy(&A);
913:   }
914:   return(0);
915: }

919: PetscErrorCode MatView_MPISBAIJ(Mat mat,PetscViewer viewer)
920: {
922:   PetscBool      iascii,isdraw,issocket,isbinary;

925:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
926:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
927:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
928:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
929:   if (iascii || isdraw || issocket || isbinary) {
930:     MatView_MPISBAIJ_ASCIIorDraworSocket(mat,viewer);
931:   }
932:   return(0);
933: }

937: PetscErrorCode MatDestroy_MPISBAIJ(Mat mat)
938: {
939:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;

943: #if defined(PETSC_USE_LOG)
944:   PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N);
945: #endif
946:   MatStashDestroy_Private(&mat->stash);
947:   MatStashDestroy_Private(&mat->bstash);
948:   MatDestroy(&baij->A);
949:   MatDestroy(&baij->B);
950: #if defined(PETSC_USE_CTABLE)
951:   PetscTableDestroy(&baij->colmap);
952: #else
953:   PetscFree(baij->colmap);
954: #endif
955:   PetscFree(baij->garray);
956:   VecDestroy(&baij->lvec);
957:   VecScatterDestroy(&baij->Mvctx);
958:   VecDestroy(&baij->slvec0);
959:   VecDestroy(&baij->slvec0b);
960:   VecDestroy(&baij->slvec1);
961:   VecDestroy(&baij->slvec1a);
962:   VecDestroy(&baij->slvec1b);
963:   VecScatterDestroy(&baij->sMvctx);
964:   PetscFree2(baij->rowvalues,baij->rowindices);
965:   PetscFree(baij->barray);
966:   PetscFree(baij->hd);
967:   VecDestroy(&baij->diag);
968:   VecDestroy(&baij->bb1);
969:   VecDestroy(&baij->xx1);
970: #if defined(PETSC_USE_REAL_MAT_SINGLE)
971:   PetscFree(baij->setvaluescopy);
972: #endif
973:   PetscFree(baij->in_loc);
974:   PetscFree(baij->v_loc);
975:   PetscFree(baij->rangebs);
976:   PetscFree(mat->data);

978:   PetscObjectChangeTypeName((PetscObject)mat,0);
979:   PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);
980:   PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);
981:   PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C",NULL);
982:   PetscObjectComposeFunction((PetscObject)mat,"MatMPISBAIJSetPreallocation_C",NULL);
983:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpisbaij_mpisbstrm_C",NULL);
984: #if defined(PETSC_HAVE_ELEMENTAL)
985:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpisbaij_elemental_C",NULL);
986: #endif
987:   return(0);
988: }

992: PetscErrorCode MatMult_MPISBAIJ_Hermitian(Mat A,Vec xx,Vec yy)
993: {
994:   Mat_MPISBAIJ      *a = (Mat_MPISBAIJ*)A->data;
995:   PetscErrorCode    ierr;
996:   PetscInt          nt,mbs=a->mbs,bs=A->rmap->bs;
997:   PetscScalar       *from;
998:   const PetscScalar *x;

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

1004:   /* diagonal part */
1005:   (*a->A->ops->mult)(a->A,xx,a->slvec1a);
1006:   VecSet(a->slvec1b,0.0);

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

1011:   /* copy x into the vec slvec0 */
1012:   VecGetArray(a->slvec0,&from);
1013:   VecGetArrayRead(xx,&x);

1015:   PetscMemcpy(from,x,bs*mbs*sizeof(MatScalar));
1016:   VecRestoreArray(a->slvec0,&from);
1017:   VecRestoreArrayRead(xx,&x);

1019:   VecScatterBegin(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
1020:   VecScatterEnd(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
1021:   /* supperdiagonal part */
1022:   (*a->B->ops->multadd)(a->B,a->slvec1b,a->slvec1a,yy);
1023:   return(0);
1024: }

1028: PetscErrorCode MatMult_MPISBAIJ(Mat A,Vec xx,Vec yy)
1029: {
1030:   Mat_MPISBAIJ      *a = (Mat_MPISBAIJ*)A->data;
1031:   PetscErrorCode    ierr;
1032:   PetscInt          nt,mbs=a->mbs,bs=A->rmap->bs;
1033:   PetscScalar       *from;
1034:   const PetscScalar *x;

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

1040:   /* diagonal part */
1041:   (*a->A->ops->mult)(a->A,xx,a->slvec1a);
1042:   VecSet(a->slvec1b,0.0);

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

1047:   /* copy x into the vec slvec0 */
1048:   VecGetArray(a->slvec0,&from);
1049:   VecGetArrayRead(xx,&x);

1051:   PetscMemcpy(from,x,bs*mbs*sizeof(MatScalar));
1052:   VecRestoreArray(a->slvec0,&from);
1053:   VecRestoreArrayRead(xx,&x);

1055:   VecScatterBegin(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
1056:   VecScatterEnd(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
1057:   /* supperdiagonal part */
1058:   (*a->B->ops->multadd)(a->B,a->slvec1b,a->slvec1a,yy);
1059:   return(0);
1060: }

1064: PetscErrorCode MatMult_MPISBAIJ_2comm(Mat A,Vec xx,Vec yy)
1065: {
1066:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;
1068:   PetscInt       nt;

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

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

1077:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1078:   /* do diagonal part */
1079:   (*a->A->ops->mult)(a->A,xx,yy);
1080:   /* do supperdiagonal part */
1081:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1082:   (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
1083:   /* do subdiagonal part */
1084:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1085:   VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1086:   VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1087:   return(0);
1088: }

1092: PetscErrorCode MatMultAdd_MPISBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1093: {
1094:   Mat_MPISBAIJ      *a = (Mat_MPISBAIJ*)A->data;
1095:   PetscErrorCode    ierr;
1096:   PetscInt          mbs=a->mbs,bs=A->rmap->bs;
1097:   PetscScalar       *from,zero=0.0;
1098:   const PetscScalar *x;

1101:   /*
1102:   PetscSynchronizedPrintf(PetscObjectComm((PetscObject)A)," MatMultAdd is called ...\n");
1103:   PetscSynchronizedFlush(PetscObjectComm((PetscObject)A),PETSC_STDOUT);
1104:   */
1105:   /* diagonal part */
1106:   (*a->A->ops->multadd)(a->A,xx,yy,a->slvec1a);
1107:   VecSet(a->slvec1b,zero);

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

1112:   /* copy x into the vec slvec0 */
1113:   VecGetArray(a->slvec0,&from);
1114:   VecGetArrayRead(xx,&x);
1115:   PetscMemcpy(from,x,bs*mbs*sizeof(MatScalar));
1116:   VecRestoreArray(a->slvec0,&from);

1118:   VecScatterBegin(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
1119:   VecRestoreArrayRead(xx,&x);
1120:   VecScatterEnd(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);

1122:   /* supperdiagonal part */
1123:   (*a->B->ops->multadd)(a->B,a->slvec1b,a->slvec1a,zz);
1124:   return(0);
1125: }

1129: PetscErrorCode MatMultAdd_MPISBAIJ_2comm(Mat A,Vec xx,Vec yy,Vec zz)
1130: {
1131:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1135:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1136:   /* do diagonal part */
1137:   (*a->A->ops->multadd)(a->A,xx,yy,zz);
1138:   /* do supperdiagonal part */
1139:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1140:   (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);

1142:   /* do subdiagonal part */
1143:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1144:   VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1145:   VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1146:   return(0);
1147: }

1149: /*
1150:   This only works correctly for square matrices where the subblock A->A is the
1151:    diagonal block
1152: */
1155: PetscErrorCode MatGetDiagonal_MPISBAIJ(Mat A,Vec v)
1156: {
1157:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

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

1168: PetscErrorCode MatScale_MPISBAIJ(Mat A,PetscScalar aa)
1169: {
1170:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1174:   MatScale(a->A,aa);
1175:   MatScale(a->B,aa);
1176:   return(0);
1177: }

1181: PetscErrorCode MatGetRow_MPISBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1182: {
1183:   Mat_MPISBAIJ   *mat = (Mat_MPISBAIJ*)matin->data;
1184:   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
1186:   PetscInt       bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB;
1187:   PetscInt       nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend;
1188:   PetscInt       *cmap,*idx_p,cstart = mat->rstartbs;

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

1194:   if (!mat->rowvalues && (idx || v)) {
1195:     /*
1196:         allocate enough space to hold information from the longest row.
1197:     */
1198:     Mat_SeqSBAIJ *Aa = (Mat_SeqSBAIJ*)mat->A->data;
1199:     Mat_SeqBAIJ  *Ba = (Mat_SeqBAIJ*)mat->B->data;
1200:     PetscInt     max = 1,mbs = mat->mbs,tmp;
1201:     for (i=0; i<mbs; i++) {
1202:       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; /* row length */
1203:       if (max < tmp) max = tmp;
1204:     }
1205:     PetscMalloc2(max*bs2,&mat->rowvalues,max*bs2,&mat->rowindices);
1206:   }

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

1211:   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1212:   if (!v)   {pvA = 0; pvB = 0;}
1213:   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1214:   (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1215:   (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1216:   nztot = nzA + nzB;

1218:   cmap = mat->garray;
1219:   if (v  || idx) {
1220:     if (nztot) {
1221:       /* Sort by increasing column numbers, assuming A and B already sorted */
1222:       PetscInt imark = -1;
1223:       if (v) {
1224:         *v = v_p = mat->rowvalues;
1225:         for (i=0; i<nzB; i++) {
1226:           if (cmap[cworkB[i]/bs] < cstart) v_p[i] = vworkB[i];
1227:           else break;
1228:         }
1229:         imark = i;
1230:         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1231:         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1232:       }
1233:       if (idx) {
1234:         *idx = idx_p = mat->rowindices;
1235:         if (imark > -1) {
1236:           for (i=0; i<imark; i++) {
1237:             idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1238:           }
1239:         } else {
1240:           for (i=0; i<nzB; i++) {
1241:             if (cmap[cworkB[i]/bs] < cstart) idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1242:             else break;
1243:           }
1244:           imark = i;
1245:         }
1246:         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart*bs + cworkA[i];
1247:         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1248:       }
1249:     } else {
1250:       if (idx) *idx = 0;
1251:       if (v)   *v   = 0;
1252:     }
1253:   }
1254:   *nz  = nztot;
1255:   (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1256:   (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1257:   return(0);
1258: }

1262: PetscErrorCode MatRestoreRow_MPISBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1263: {
1264:   Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)mat->data;

1267:   if (!baij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
1268:   baij->getrowactive = PETSC_FALSE;
1269:   return(0);
1270: }

1274: PetscErrorCode MatGetRowUpperTriangular_MPISBAIJ(Mat A)
1275: {
1276:   Mat_MPISBAIJ *a  = (Mat_MPISBAIJ*)A->data;
1277:   Mat_SeqSBAIJ *aA = (Mat_SeqSBAIJ*)a->A->data;

1280:   aA->getrow_utriangular = PETSC_TRUE;
1281:   return(0);
1282: }
1285: PetscErrorCode MatRestoreRowUpperTriangular_MPISBAIJ(Mat A)
1286: {
1287:   Mat_MPISBAIJ *a  = (Mat_MPISBAIJ*)A->data;
1288:   Mat_SeqSBAIJ *aA = (Mat_SeqSBAIJ*)a->A->data;

1291:   aA->getrow_utriangular = PETSC_FALSE;
1292:   return(0);
1293: }

1297: PetscErrorCode MatRealPart_MPISBAIJ(Mat A)
1298: {
1299:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1303:   MatRealPart(a->A);
1304:   MatRealPart(a->B);
1305:   return(0);
1306: }

1310: PetscErrorCode MatImaginaryPart_MPISBAIJ(Mat A)
1311: {
1312:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1316:   MatImaginaryPart(a->A);
1317:   MatImaginaryPart(a->B);
1318:   return(0);
1319: }

1321: /* Check if isrow is a subset of iscol_local, called by MatGetSubMatrix_MPISBAIJ()
1322:    Input: isrow       - distributed(parallel), 
1323:           iscol_local - locally owned (seq) 
1324: */
1327: PetscErrorCode ISEqual_private(IS isrow,IS iscol_local,PetscBool  *flg)
1328: {
1330:   PetscInt       sz1,sz2,*a1,*a2,i,j,k,nmatch;
1331:   const PetscInt *ptr1,*ptr2;

1334:   ISGetLocalSize(isrow,&sz1);
1335:   ISGetLocalSize(iscol_local,&sz2);
1336:   if (sz1 > sz2) {
1337:     *flg = PETSC_FALSE;
1338:     return(0);
1339:   }

1341:   ISGetIndices(isrow,&ptr1);
1342:   ISGetIndices(iscol_local,&ptr2);

1344:   PetscMalloc1(sz1,&a1);
1345:   PetscMalloc1(sz2,&a2);
1346:   PetscMemcpy(a1,ptr1,sz1*sizeof(PetscInt));
1347:   PetscMemcpy(a2,ptr2,sz2*sizeof(PetscInt));
1348:   PetscSortInt(sz1,a1);
1349:   PetscSortInt(sz2,a2);

1351:   nmatch=0;
1352:   k     = 0;
1353:   for (i=0; i<sz1; i++){
1354:     for (j=k; j<sz2; j++){
1355:       if (a1[i] == a2[j]) {
1356:         k = j; nmatch++;
1357:         break;
1358:       }
1359:     }
1360:   }
1361:   ISRestoreIndices(isrow,&ptr1);
1362:   ISRestoreIndices(iscol_local,&ptr2);
1363:   PetscFree(a1);
1364:   PetscFree(a2);
1365:   if (nmatch < sz1) {
1366:     *flg = PETSC_FALSE;
1367:   } else {
1368:     *flg = PETSC_TRUE;
1369:   }
1370:   return(0);
1371: }

1375: PetscErrorCode MatGetSubMatrix_MPISBAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
1376: {
1378:   IS             iscol_local;
1379:   PetscInt       csize;
1380:   PetscBool      isequal;

1383:   ISGetLocalSize(iscol,&csize);
1384:   if (call == MAT_REUSE_MATRIX) {
1385:     PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);
1386:     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
1387:   } else {
1388:     ISAllGather(iscol,&iscol_local);
1389:     ISEqual_private(isrow,iscol_local,&isequal);
1390:     if (!isequal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"For symmetric format, iscol must equal isrow");
1391:   }

1393:   /* now call MatGetSubMatrix_MPIBAIJ() */
1394:   MatGetSubMatrix_MPIBAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);
1395:   if (call == MAT_INITIAL_MATRIX) {
1396:     PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);
1397:     ISDestroy(&iscol_local);
1398:   }
1399:   return(0);
1400: }

1404: PetscErrorCode MatZeroEntries_MPISBAIJ(Mat A)
1405: {
1406:   Mat_MPISBAIJ   *l = (Mat_MPISBAIJ*)A->data;

1410:   MatZeroEntries(l->A);
1411:   MatZeroEntries(l->B);
1412:   return(0);
1413: }

1417: PetscErrorCode MatGetInfo_MPISBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1418: {
1419:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)matin->data;
1420:   Mat            A  = a->A,B = a->B;
1422:   PetscReal      isend[5],irecv[5];

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

1427:   MatGetInfo(A,MAT_LOCAL,info);

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

1432:   MatGetInfo(B,MAT_LOCAL,info);

1434:   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1435:   isend[3] += info->memory;  isend[4] += info->mallocs;
1436:   if (flag == MAT_LOCAL) {
1437:     info->nz_used      = isend[0];
1438:     info->nz_allocated = isend[1];
1439:     info->nz_unneeded  = isend[2];
1440:     info->memory       = isend[3];
1441:     info->mallocs      = isend[4];
1442:   } else if (flag == MAT_GLOBAL_MAX) {
1443:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));

1445:     info->nz_used      = irecv[0];
1446:     info->nz_allocated = irecv[1];
1447:     info->nz_unneeded  = irecv[2];
1448:     info->memory       = irecv[3];
1449:     info->mallocs      = irecv[4];
1450:   } else if (flag == MAT_GLOBAL_SUM) {
1451:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));

1453:     info->nz_used      = irecv[0];
1454:     info->nz_allocated = irecv[1];
1455:     info->nz_unneeded  = irecv[2];
1456:     info->memory       = irecv[3];
1457:     info->mallocs      = irecv[4];
1458:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown MatInfoType argument %d",(int)flag);
1459:   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
1460:   info->fill_ratio_needed = 0;
1461:   info->factor_mallocs    = 0;
1462:   return(0);
1463: }

1467: PetscErrorCode MatSetOption_MPISBAIJ(Mat A,MatOption op,PetscBool flg)
1468: {
1469:   Mat_MPISBAIJ   *a  = (Mat_MPISBAIJ*)A->data;
1470:   Mat_SeqSBAIJ   *aA = (Mat_SeqSBAIJ*)a->A->data;

1474:   switch (op) {
1475:   case MAT_NEW_NONZERO_LOCATIONS:
1476:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1477:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1478:   case MAT_KEEP_NONZERO_PATTERN:
1479:   case MAT_NEW_NONZERO_LOCATION_ERR:
1480:     MatSetOption(a->A,op,flg);
1481:     MatSetOption(a->B,op,flg);
1482:     break;
1483:   case MAT_ROW_ORIENTED:
1484:     a->roworiented = flg;

1486:     MatSetOption(a->A,op,flg);
1487:     MatSetOption(a->B,op,flg);
1488:     break;
1489:   case MAT_NEW_DIAGONALS:
1490:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1491:     break;
1492:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1493:     a->donotstash = flg;
1494:     break;
1495:   case MAT_USE_HASH_TABLE:
1496:     a->ht_flag = flg;
1497:     break;
1498:   case MAT_HERMITIAN:
1499:     if (!A->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatAssemblyEnd() first");
1500:     MatSetOption(a->A,op,flg);

1502:     A->ops->mult = MatMult_MPISBAIJ_Hermitian;
1503:     break;
1504:   case MAT_SPD:
1505:     A->spd_set = PETSC_TRUE;
1506:     A->spd     = flg;
1507:     if (flg) {
1508:       A->symmetric                  = PETSC_TRUE;
1509:       A->structurally_symmetric     = PETSC_TRUE;
1510:       A->symmetric_set              = PETSC_TRUE;
1511:       A->structurally_symmetric_set = PETSC_TRUE;
1512:     }
1513:     break;
1514:   case MAT_SYMMETRIC:
1515:     MatSetOption(a->A,op,flg);
1516:     break;
1517:   case MAT_STRUCTURALLY_SYMMETRIC:
1518:     MatSetOption(a->A,op,flg);
1519:     break;
1520:   case MAT_SYMMETRY_ETERNAL:
1521:     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix must be symmetric");
1522:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1523:     break;
1524:   case MAT_IGNORE_LOWER_TRIANGULAR:
1525:     aA->ignore_ltriangular = flg;
1526:     break;
1527:   case MAT_ERROR_LOWER_TRIANGULAR:
1528:     aA->ignore_ltriangular = flg;
1529:     break;
1530:   case MAT_GETROW_UPPERTRIANGULAR:
1531:     aA->getrow_utriangular = flg;
1532:     break;
1533:   default:
1534:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1535:   }
1536:   return(0);
1537: }

1541: PetscErrorCode MatTranspose_MPISBAIJ(Mat A,MatReuse reuse,Mat *B)
1542: {

1546:   if (MAT_INITIAL_MATRIX || *B != A) {
1547:     MatDuplicate(A,MAT_COPY_VALUES,B);
1548:   }
1549:   return(0);
1550: }

1554: PetscErrorCode MatDiagonalScale_MPISBAIJ(Mat mat,Vec ll,Vec rr)
1555: {
1556:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
1557:   Mat            a     = baij->A, b=baij->B;
1559:   PetscInt       nv,m,n;
1560:   PetscBool      flg;

1563:   if (ll != rr) {
1564:     VecEqual(ll,rr,&flg);
1565:     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"For symmetric format, left and right scaling vectors must be same\n");
1566:   }
1567:   if (!ll) return(0);

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

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

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

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

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

1583:   /* right diagonalscale the off-diagonal part */
1584:   VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);
1585:   (*b->ops->diagonalscale)(b,NULL,baij->lvec);
1586:   return(0);
1587: }

1591: PetscErrorCode MatSetUnfactored_MPISBAIJ(Mat A)
1592: {
1593:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1597:   MatSetUnfactored(a->A);
1598:   return(0);
1599: }

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

1605: PetscErrorCode MatEqual_MPISBAIJ(Mat A,Mat B,PetscBool  *flag)
1606: {
1607:   Mat_MPISBAIJ   *matB = (Mat_MPISBAIJ*)B->data,*matA = (Mat_MPISBAIJ*)A->data;
1608:   Mat            a,b,c,d;
1609:   PetscBool      flg;

1613:   a = matA->A; b = matA->B;
1614:   c = matB->A; d = matB->B;

1616:   MatEqual(a,c,&flg);
1617:   if (flg) {
1618:     MatEqual(b,d,&flg);
1619:   }
1620:   MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
1621:   return(0);
1622: }

1626: PetscErrorCode MatCopy_MPISBAIJ(Mat A,Mat B,MatStructure str)
1627: {
1629:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;
1630:   Mat_MPISBAIJ   *b = (Mat_MPISBAIJ*)B->data;

1633:   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1634:   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1635:     MatGetRowUpperTriangular(A);
1636:     MatCopy_Basic(A,B,str);
1637:     MatRestoreRowUpperTriangular(A);
1638:   } else {
1639:     MatCopy(a->A,b->A,str);
1640:     MatCopy(a->B,b->B,str);
1641:   }
1642:   return(0);
1643: }

1647: PetscErrorCode MatSetUp_MPISBAIJ(Mat A)
1648: {

1652:   MatMPISBAIJSetPreallocation(A,A->rmap->bs,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
1653:   return(0);
1654: }

1658: PetscErrorCode MatAXPY_MPISBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
1659: {
1661:   Mat_MPISBAIJ   *xx=(Mat_MPISBAIJ*)X->data,*yy=(Mat_MPISBAIJ*)Y->data;
1662:   PetscBLASInt   bnz,one=1;
1663:   Mat_SeqSBAIJ   *xa,*ya;
1664:   Mat_SeqBAIJ    *xb,*yb;

1667:   if (str == SAME_NONZERO_PATTERN) {
1668:     PetscScalar alpha = a;
1669:     xa   = (Mat_SeqSBAIJ*)xx->A->data;
1670:     ya   = (Mat_SeqSBAIJ*)yy->A->data;
1671:     PetscBLASIntCast(xa->nz,&bnz);
1672:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,xa->a,&one,ya->a,&one));
1673:     xb   = (Mat_SeqBAIJ*)xx->B->data;
1674:     yb   = (Mat_SeqBAIJ*)yy->B->data;
1675:     PetscBLASIntCast(xb->nz,&bnz);
1676:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,xb->a,&one,yb->a,&one));
1677:     PetscObjectStateIncrease((PetscObject)Y);
1678:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1679:     MatSetOption(X,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE);
1680:     MatAXPY_Basic(Y,a,X,str);
1681:     MatSetOption(X,MAT_GETROW_UPPERTRIANGULAR,PETSC_FALSE);
1682:   } else {
1683:     Mat      B;
1684:     PetscInt *nnz_d,*nnz_o,bs=Y->rmap->bs;
1685:     if (bs != X->rmap->bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrices must have same block size");
1686:     MatGetRowUpperTriangular(X);
1687:     MatGetRowUpperTriangular(Y);
1688:     PetscMalloc1(yy->A->rmap->N,&nnz_d);
1689:     PetscMalloc1(yy->B->rmap->N,&nnz_o);
1690:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
1691:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
1692:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
1693:     MatSetBlockSizesFromMats(B,Y,Y);
1694:     MatSetType(B,MATMPISBAIJ);
1695:     MatAXPYGetPreallocation_SeqSBAIJ(yy->A,xx->A,nnz_d);
1696:     MatAXPYGetPreallocation_MPIBAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);
1697:     MatMPISBAIJSetPreallocation(B,bs,0,nnz_d,0,nnz_o);
1698:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
1699:     MatHeaderReplace(Y,B);
1700:     PetscFree(nnz_d);
1701:     PetscFree(nnz_o);
1702:     MatRestoreRowUpperTriangular(X);
1703:     MatRestoreRowUpperTriangular(Y);
1704:   }
1705:   return(0);
1706: }

1710: PetscErrorCode MatGetSubMatrices_MPISBAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1711: {
1713:   PetscInt       i;
1714:   PetscBool      flg,sorted;

1717:   for (i = 0; i < n; i++) {
1718:     ISSorted(irow[i],&sorted);
1719:     if (!sorted) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Row index set %d not sorted",i);
1720:     ISSorted(icol[i],&sorted);
1721:     if (!sorted) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Column index set %d not sorted",i);
1722:   }
1723:   MatGetSubMatrices_MPIBAIJ(A,n,irow,icol,scall,B);
1724:   for (i=0; i<n; i++) {
1725:     ISEqual(irow[i],icol[i],&flg);
1726:     if (!flg) { /* *B[i] is non-symmetric, set flag */
1727:       MatSetOption(*B[i],MAT_SYMMETRIC,PETSC_FALSE);
1728:     }
1729:   }
1730:   return(0);
1731: }

1735: PetscErrorCode MatShift_MPISBAIJ(Mat Y,PetscScalar a)
1736: {
1738:   Mat_MPISBAIJ    *maij = (Mat_MPISBAIJ*)Y->data;
1739:   Mat_SeqSBAIJ    *aij = (Mat_SeqSBAIJ*)maij->A->data;

1742:   if (!Y->preallocated) {
1743:     MatMPISBAIJSetPreallocation(Y,Y->rmap->bs,1,NULL,0,NULL);
1744:   } else if (!aij->nz) {
1745:     MatSeqSBAIJSetPreallocation(maij->A,Y->rmap->bs,1,NULL);
1746:   }
1747:   MatShift_Basic(Y,a);
1748:   return(0);
1749: }

1751: /* -------------------------------------------------------------------*/
1752: static struct _MatOps MatOps_Values = {MatSetValues_MPISBAIJ,
1753:                                        MatGetRow_MPISBAIJ,
1754:                                        MatRestoreRow_MPISBAIJ,
1755:                                        MatMult_MPISBAIJ,
1756:                                /*  4*/ MatMultAdd_MPISBAIJ,
1757:                                        MatMult_MPISBAIJ,       /* transpose versions are same as non-transpose */
1758:                                        MatMultAdd_MPISBAIJ,
1759:                                        0,
1760:                                        0,
1761:                                        0,
1762:                                /* 10*/ 0,
1763:                                        0,
1764:                                        0,
1765:                                        MatSOR_MPISBAIJ,
1766:                                        MatTranspose_MPISBAIJ,
1767:                                /* 15*/ MatGetInfo_MPISBAIJ,
1768:                                        MatEqual_MPISBAIJ,
1769:                                        MatGetDiagonal_MPISBAIJ,
1770:                                        MatDiagonalScale_MPISBAIJ,
1771:                                        MatNorm_MPISBAIJ,
1772:                                /* 20*/ MatAssemblyBegin_MPISBAIJ,
1773:                                        MatAssemblyEnd_MPISBAIJ,
1774:                                        MatSetOption_MPISBAIJ,
1775:                                        MatZeroEntries_MPISBAIJ,
1776:                                /* 24*/ 0,
1777:                                        0,
1778:                                        0,
1779:                                        0,
1780:                                        0,
1781:                                /* 29*/ MatSetUp_MPISBAIJ,
1782:                                        0,
1783:                                        0,
1784:                                        0,
1785:                                        0,
1786:                                /* 34*/ MatDuplicate_MPISBAIJ,
1787:                                        0,
1788:                                        0,
1789:                                        0,
1790:                                        0,
1791:                                /* 39*/ MatAXPY_MPISBAIJ,
1792:                                        MatGetSubMatrices_MPISBAIJ,
1793:                                        MatIncreaseOverlap_MPISBAIJ,
1794:                                        MatGetValues_MPISBAIJ,
1795:                                        MatCopy_MPISBAIJ,
1796:                                /* 44*/ 0,
1797:                                        MatScale_MPISBAIJ,
1798:                                        MatShift_MPISBAIJ,
1799:                                        0,
1800:                                        0,
1801:                                /* 49*/ 0,
1802:                                        0,
1803:                                        0,
1804:                                        0,
1805:                                        0,
1806:                                /* 54*/ 0,
1807:                                        0,
1808:                                        MatSetUnfactored_MPISBAIJ,
1809:                                        0,
1810:                                        MatSetValuesBlocked_MPISBAIJ,
1811:                                /* 59*/ MatGetSubMatrix_MPISBAIJ,
1812:                                        0,
1813:                                        0,
1814:                                        0,
1815:                                        0,
1816:                                /* 64*/ 0,
1817:                                        0,
1818:                                        0,
1819:                                        0,
1820:                                        0,
1821:                                /* 69*/ MatGetRowMaxAbs_MPISBAIJ,
1822:                                        0,
1823:                                        0,
1824:                                        0,
1825:                                        0,
1826:                                /* 74*/ 0,
1827:                                        0,
1828:                                        0,
1829:                                        0,
1830:                                        0,
1831:                                /* 79*/ 0,
1832:                                        0,
1833:                                        0,
1834:                                        0,
1835:                                        MatLoad_MPISBAIJ,
1836:                                /* 84*/ 0,
1837:                                        0,
1838:                                        0,
1839:                                        0,
1840:                                        0,
1841:                                /* 89*/ 0,
1842:                                        0,
1843:                                        0,
1844:                                        0,
1845:                                        0,
1846:                                /* 94*/ 0,
1847:                                        0,
1848:                                        0,
1849:                                        0,
1850:                                        0,
1851:                                /* 99*/ 0,
1852:                                        0,
1853:                                        0,
1854:                                        0,
1855:                                        0,
1856:                                /*104*/ 0,
1857:                                        MatRealPart_MPISBAIJ,
1858:                                        MatImaginaryPart_MPISBAIJ,
1859:                                        MatGetRowUpperTriangular_MPISBAIJ,
1860:                                        MatRestoreRowUpperTriangular_MPISBAIJ,
1861:                                /*109*/ 0,
1862:                                        0,
1863:                                        0,
1864:                                        0,
1865:                                        0,
1866:                                /*114*/ 0,
1867:                                        0,
1868:                                        0,
1869:                                        0,
1870:                                        0,
1871:                                /*119*/ 0,
1872:                                        0,
1873:                                        0,
1874:                                        0,
1875:                                        0,
1876:                                /*124*/ 0,
1877:                                        0,
1878:                                        0,
1879:                                        0,
1880:                                        0,
1881:                                /*129*/ 0,
1882:                                        0,
1883:                                        0,
1884:                                        0,
1885:                                        0,
1886:                                /*134*/ 0,
1887:                                        0,
1888:                                        0,
1889:                                        0,
1890:                                        0,
1891:                                /*139*/ 0,
1892:                                        0,
1893:                                        0,
1894:                                        0,
1895:                                        0,
1896:                                 /*144*/MatCreateMPIMatConcatenateSeqMat_MPISBAIJ
1897: };

1901: PetscErrorCode  MatGetDiagonalBlock_MPISBAIJ(Mat A,Mat *a)
1902: {
1904:   *a = ((Mat_MPISBAIJ*)A->data)->A;
1905:   return(0);
1906: }

1910: PetscErrorCode  MatMPISBAIJSetPreallocation_MPISBAIJ(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt *d_nnz,PetscInt o_nz,const PetscInt *o_nnz)
1911: {
1912:   Mat_MPISBAIJ   *b;
1914:   PetscInt       i,mbs,Mbs;

1917:   MatSetBlockSize(B,PetscAbs(bs));
1918:   PetscLayoutSetUp(B->rmap);
1919:   PetscLayoutSetUp(B->cmap);
1920:   PetscLayoutGetBlockSize(B->rmap,&bs);

1922:   b   = (Mat_MPISBAIJ*)B->data;
1923:   mbs = B->rmap->n/bs;
1924:   Mbs = B->rmap->N/bs;
1925:   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);

1927:   B->rmap->bs = bs;
1928:   b->bs2      = bs*bs;
1929:   b->mbs      = mbs;
1930:   b->Mbs      = Mbs;
1931:   b->nbs      = B->cmap->n/bs;
1932:   b->Nbs      = B->cmap->N/bs;

1934:   for (i=0; i<=b->size; i++) {
1935:     b->rangebs[i] = B->rmap->range[i]/bs;
1936:   }
1937:   b->rstartbs = B->rmap->rstart/bs;
1938:   b->rendbs   = B->rmap->rend/bs;

1940:   b->cstartbs = B->cmap->rstart/bs;
1941:   b->cendbs   = B->cmap->rend/bs;

1943:   if (!B->preallocated) {
1944:     MatCreate(PETSC_COMM_SELF,&b->A);
1945:     MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
1946:     MatSetType(b->A,MATSEQSBAIJ);
1947:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
1948:     MatCreate(PETSC_COMM_SELF,&b->B);
1949:     MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);
1950:     MatSetType(b->B,MATSEQBAIJ);
1951:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);
1952:     MatStashCreate_Private(PetscObjectComm((PetscObject)B),bs,&B->bstash);
1953:   }

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

1958:   B->preallocated = PETSC_TRUE;
1959:   return(0);
1960: }

1964: PetscErrorCode MatMPISBAIJSetPreallocationCSR_MPISBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
1965: {
1966:   PetscInt       m,rstart,cstart,cend;
1967:   PetscInt       i,j,d,nz,nz_max=0,*d_nnz=0,*o_nnz=0;
1968:   const PetscInt *JJ    =0;
1969:   PetscScalar    *values=0;

1973:   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
1974:   PetscLayoutSetBlockSize(B->rmap,bs);
1975:   PetscLayoutSetBlockSize(B->cmap,bs);
1976:   PetscLayoutSetUp(B->rmap);
1977:   PetscLayoutSetUp(B->cmap);
1978:   PetscLayoutGetBlockSize(B->rmap,&bs);
1979:   m      = B->rmap->n/bs;
1980:   rstart = B->rmap->rstart/bs;
1981:   cstart = B->cmap->rstart/bs;
1982:   cend   = B->cmap->rend/bs;

1984:   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
1985:   PetscMalloc2(m,&d_nnz,m,&o_nnz);
1986:   for (i=0; i<m; i++) {
1987:     nz = ii[i+1] - ii[i];
1988:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz);
1989:     nz_max = PetscMax(nz_max,nz);
1990:     JJ     = jj + ii[i];
1991:     for (j=0; j<nz; j++) {
1992:       if (*JJ >= cstart) break;
1993:       JJ++;
1994:     }
1995:     d = 0;
1996:     for (; j<nz; j++) {
1997:       if (*JJ++ >= cend) break;
1998:       d++;
1999:     }
2000:     d_nnz[i] = d;
2001:     o_nnz[i] = nz - d;
2002:   }
2003:   MatMPISBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);
2004:   PetscFree2(d_nnz,o_nnz);

2006:   values = (PetscScalar*)V;
2007:   if (!values) {
2008:     PetscMalloc1(bs*bs*nz_max,&values);
2009:     PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));
2010:   }
2011:   for (i=0; i<m; i++) {
2012:     PetscInt          row    = i + rstart;
2013:     PetscInt          ncols  = ii[i+1] - ii[i];
2014:     const PetscInt    *icols = jj + ii[i];
2015:     const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2016:     MatSetValuesBlocked_MPISBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);
2017:   }

2019:   if (!V) { PetscFree(values); }
2020:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2021:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2022:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
2023:   return(0);
2024: }

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

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

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

2037:   Level: beginner

2039: .seealso: MatCreateMPISBAIJ
2040: M*/

2042: PETSC_EXTERN PetscErrorCode MatConvert_MPISBAIJ_MPISBSTRM(Mat,MatType,MatReuse,Mat*);

2046: PETSC_EXTERN PetscErrorCode MatCreate_MPISBAIJ(Mat B)
2047: {
2048:   Mat_MPISBAIJ   *b;
2050:   PetscBool      flg = PETSC_FALSE;

2053:   PetscNewLog(B,&b);
2054:   B->data = (void*)b;
2055:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));

2057:   B->ops->destroy = MatDestroy_MPISBAIJ;
2058:   B->ops->view    = MatView_MPISBAIJ;
2059:   B->assembled    = PETSC_FALSE;
2060:   B->insertmode   = NOT_SET_VALUES;

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

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

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

2071:   b->donotstash  = PETSC_FALSE;
2072:   b->colmap      = NULL;
2073:   b->garray      = NULL;
2074:   b->roworiented = PETSC_TRUE;

2076:   /* stuff used in block assembly */
2077:   b->barray = 0;

2079:   /* stuff used for matrix vector multiply */
2080:   b->lvec    = 0;
2081:   b->Mvctx   = 0;
2082:   b->slvec0  = 0;
2083:   b->slvec0b = 0;
2084:   b->slvec1  = 0;
2085:   b->slvec1a = 0;
2086:   b->slvec1b = 0;
2087:   b->sMvctx  = 0;

2089:   /* stuff for MatGetRow() */
2090:   b->rowindices   = 0;
2091:   b->rowvalues    = 0;
2092:   b->getrowactive = PETSC_FALSE;

2094:   /* hash table stuff */
2095:   b->ht           = 0;
2096:   b->hd           = 0;
2097:   b->ht_size      = 0;
2098:   b->ht_flag      = PETSC_FALSE;
2099:   b->ht_fact      = 0;
2100:   b->ht_total_ct  = 0;
2101:   b->ht_insert_ct = 0;

2103:   /* stuff for MatGetSubMatrices_MPIBAIJ_local() */
2104:   b->ijonly = PETSC_FALSE;

2106:   b->in_loc = 0;
2107:   b->v_loc  = 0;
2108:   b->n_loc  = 0;

2110:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPISBAIJ);
2111:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPISBAIJ);
2112:   PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",MatGetDiagonalBlock_MPISBAIJ);
2113:   PetscObjectComposeFunction((PetscObject)B,"MatMPISBAIJSetPreallocation_C",MatMPISBAIJSetPreallocation_MPISBAIJ);
2114:   PetscObjectComposeFunction((PetscObject)B,"MatMPISBAIJSetPreallocationCSR_C",MatMPISBAIJSetPreallocationCSR_MPISBAIJ);
2115:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpisbaij_mpisbstrm_C",MatConvert_MPISBAIJ_MPISBSTRM);
2116: #if defined(PETSC_HAVE_ELEMENTAL)
2117:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpisbaij_elemental_C",MatConvert_MPISBAIJ_Elemental);
2118: #endif

2120:   B->symmetric                  = PETSC_TRUE;
2121:   B->structurally_symmetric     = PETSC_TRUE;
2122:   B->symmetric_set              = PETSC_TRUE;
2123:   B->structurally_symmetric_set = PETSC_TRUE;

2125:   PetscObjectChangeTypeName((PetscObject)B,MATMPISBAIJ);
2126:   PetscOptionsBegin(PetscObjectComm((PetscObject)B),NULL,"Options for loading MPISBAIJ matrix 1","Mat");
2127:   PetscOptionsBool("-mat_use_hash_table","Use hash table to save memory in constructing matrix","MatSetOption",flg,&flg,NULL);
2128:   if (flg) {
2129:     PetscReal fact = 1.39;
2130:     MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);
2131:     PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,NULL);
2132:     if (fact <= 1.0) fact = 1.39;
2133:     MatMPIBAIJSetHashTableFactor(B,fact);
2134:     PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);
2135:   }
2136:   PetscOptionsEnd();
2137:   return(0);
2138: }

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

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

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

2149:   Level: beginner

2151: .seealso: MatCreateMPISBAIJ,MATSEQSBAIJ,MATMPISBAIJ
2152: M*/

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

2162:    Collective on Mat

2164:    Input Parameters:
2165: +  B - the matrix
2166: .  bs   - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
2167:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
2168: .  d_nz  - number of block nonzeros per block row in diagonal portion of local
2169:            submatrix  (same for all local rows)
2170: .  d_nnz - array containing the number of block nonzeros in the various block rows
2171:            in the upper triangular and diagonal part of the in diagonal portion of the local
2172:            (possibly different for each block row) or NULL.  If you plan to factor the matrix you must leave room
2173:            for the diagonal entry and set a value even if it is zero.
2174: .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
2175:            submatrix (same for all local rows).
2176: -  o_nnz - array containing the number of nonzeros in the various block rows of the
2177:            off-diagonal portion of the local submatrix that is right of the diagonal
2178:            (possibly different for each block row) or NULL.


2181:    Options Database Keys:
2182: .   -mat_no_unroll - uses code that does not unroll the loops in the
2183:                      block calculations (much slower)
2184: .   -mat_block_size - size of the blocks to use

2186:    Notes:

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

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

2193:    Storage Information:
2194:    For a square global matrix we define each processor's diagonal portion
2195:    to be its local rows and the corresponding columns (a square submatrix);
2196:    each processor's off-diagonal portion encompasses the remainder of the
2197:    local matrix (a rectangular submatrix).

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

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

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

2213: .vb
2214:            0 1 2 3 4 5 6 7 8 9 10 11
2215:           --------------------------
2216:    row 3  |. . . d d d o o o o  o  o
2217:    row 4  |. . . d d d o o o o  o  o
2218:    row 5  |. . . d d d o o o o  o  o
2219:           --------------------------
2220: .ve

2222:    Thus, any entries in the d locations are stored in the d (diagonal)
2223:    submatrix, and any entries in the o locations are stored in the
2224:    o (off-diagonal) submatrix.  Note that the d matrix is stored in
2225:    MatSeqSBAIJ format and the o submatrix in MATSEQBAIJ format.

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

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

2236:    Level: intermediate

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

2240: .seealso: MatCreate(), MatCreateSeqSBAIJ(), MatSetValues(), MatCreateBAIJ(), PetscSplitOwnership()
2241: @*/
2242: PetscErrorCode  MatMPISBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
2243: {

2250:   PetscTryMethod(B,"MatMPISBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,bs,d_nz,d_nnz,o_nz,o_nnz));
2251:   return(0);
2252: }

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

2263:    Collective on MPI_Comm

2265:    Input Parameters:
2266: +  comm - MPI communicator
2267: .  bs   - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
2268:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
2269: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2270:            This value should be the same as the local size used in creating the
2271:            y vector for the matrix-vector product y = Ax.
2272: .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
2273:            This value should be the same as the local size used in creating the
2274:            x vector for the matrix-vector product y = Ax.
2275: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2276: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2277: .  d_nz  - number of block nonzeros per block row in diagonal portion of local
2278:            submatrix  (same for all local rows)
2279: .  d_nnz - array containing the number of block nonzeros in the various block rows
2280:            in the upper triangular portion of the in diagonal portion of the local
2281:            (possibly different for each block block row) or NULL.
2282:            If you plan to factor the matrix you must leave room for the diagonal entry and
2283:            set its value even if it is zero.
2284: .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
2285:            submatrix (same for all local rows).
2286: -  o_nnz - array containing the number of nonzeros in the various block rows of the
2287:            off-diagonal portion of the local submatrix (possibly different for
2288:            each block row) or NULL.

2290:    Output Parameter:
2291: .  A - the matrix

2293:    Options Database Keys:
2294: .   -mat_no_unroll - uses code that does not unroll the loops in the
2295:                      block calculations (much slower)
2296: .   -mat_block_size - size of the blocks to use
2297: .   -mat_mpi - use the parallel matrix data structures even on one processor
2298:                (defaults to using SeqBAIJ format on one processor)

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

2304:    Notes:
2305:    The number of rows and columns must be divisible by blocksize.
2306:    This matrix type does not support complex Hermitian operation.

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

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

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

2316:    Storage Information:
2317:    For a square global matrix we define each processor's diagonal portion
2318:    to be its local rows and the corresponding columns (a square submatrix);
2319:    each processor's off-diagonal portion encompasses the remainder of the
2320:    local matrix (a rectangular submatrix).

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

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

2331: .vb
2332:            0 1 2 3 4 5 6 7 8 9 10 11
2333:           --------------------------
2334:    row 3  |. . . d d d o o o o  o  o
2335:    row 4  |. . . d d d o o o o  o  o
2336:    row 5  |. . . d d d o o o o  o  o
2337:           --------------------------
2338: .ve

2340:    Thus, any entries in the d locations are stored in the d (diagonal)
2341:    submatrix, and any entries in the o locations are stored in the
2342:    o (off-diagonal) submatrix.  Note that the d matrix is stored in
2343:    MatSeqSBAIJ format and the o submatrix in MATSEQBAIJ format.

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

2353:    Level: intermediate

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

2357: .seealso: MatCreate(), MatCreateSeqSBAIJ(), MatSetValues(), MatCreateBAIJ()
2358: @*/

2360: 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)
2361: {
2363:   PetscMPIInt    size;

2366:   MatCreate(comm,A);
2367:   MatSetSizes(*A,m,n,M,N);
2368:   MPI_Comm_size(comm,&size);
2369:   if (size > 1) {
2370:     MatSetType(*A,MATMPISBAIJ);
2371:     MatMPISBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);
2372:   } else {
2373:     MatSetType(*A,MATSEQSBAIJ);
2374:     MatSeqSBAIJSetPreallocation(*A,bs,d_nz,d_nnz);
2375:   }
2376:   return(0);
2377: }


2382: static PetscErrorCode MatDuplicate_MPISBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
2383: {
2384:   Mat            mat;
2385:   Mat_MPISBAIJ   *a,*oldmat = (Mat_MPISBAIJ*)matin->data;
2387:   PetscInt       len=0,nt,bs=matin->rmap->bs,mbs=oldmat->mbs;
2388:   PetscScalar    *array;

2391:   *newmat = 0;

2393:   MatCreate(PetscObjectComm((PetscObject)matin),&mat);
2394:   MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);
2395:   MatSetType(mat,((PetscObject)matin)->type_name);
2396:   PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));
2397:   PetscLayoutReference(matin->rmap,&mat->rmap);
2398:   PetscLayoutReference(matin->cmap,&mat->cmap);

2400:   mat->factortype   = matin->factortype;
2401:   mat->preallocated = PETSC_TRUE;
2402:   mat->assembled    = PETSC_TRUE;
2403:   mat->insertmode   = NOT_SET_VALUES;

2405:   a      = (Mat_MPISBAIJ*)mat->data;
2406:   a->bs2 = oldmat->bs2;
2407:   a->mbs = oldmat->mbs;
2408:   a->nbs = oldmat->nbs;
2409:   a->Mbs = oldmat->Mbs;
2410:   a->Nbs = oldmat->Nbs;


2413:   a->size         = oldmat->size;
2414:   a->rank         = oldmat->rank;
2415:   a->donotstash   = oldmat->donotstash;
2416:   a->roworiented  = oldmat->roworiented;
2417:   a->rowindices   = 0;
2418:   a->rowvalues    = 0;
2419:   a->getrowactive = PETSC_FALSE;
2420:   a->barray       = 0;
2421:   a->rstartbs     = oldmat->rstartbs;
2422:   a->rendbs       = oldmat->rendbs;
2423:   a->cstartbs     = oldmat->cstartbs;
2424:   a->cendbs       = oldmat->cendbs;

2426:   /* hash table stuff */
2427:   a->ht           = 0;
2428:   a->hd           = 0;
2429:   a->ht_size      = 0;
2430:   a->ht_flag      = oldmat->ht_flag;
2431:   a->ht_fact      = oldmat->ht_fact;
2432:   a->ht_total_ct  = 0;
2433:   a->ht_insert_ct = 0;

2435:   PetscMemcpy(a->rangebs,oldmat->rangebs,(a->size+2)*sizeof(PetscInt));
2436:   if (oldmat->colmap) {
2437: #if defined(PETSC_USE_CTABLE)
2438:     PetscTableCreateCopy(oldmat->colmap,&a->colmap);
2439: #else
2440:     PetscMalloc1(a->Nbs,&a->colmap);
2441:     PetscLogObjectMemory((PetscObject)mat,(a->Nbs)*sizeof(PetscInt));
2442:     PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(PetscInt));
2443: #endif
2444:   } else a->colmap = 0;

2446:   if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
2447:     PetscMalloc1(len,&a->garray);
2448:     PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));
2449:     PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));
2450:   } else a->garray = 0;

2452:   MatStashCreate_Private(PetscObjectComm((PetscObject)matin),matin->rmap->bs,&mat->bstash);
2453:   VecDuplicate(oldmat->lvec,&a->lvec);
2454:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);
2455:   VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
2456:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);

2458:   VecDuplicate(oldmat->slvec0,&a->slvec0);
2459:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec0);
2460:   VecDuplicate(oldmat->slvec1,&a->slvec1);
2461:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec1);

2463:   VecGetLocalSize(a->slvec1,&nt);
2464:   VecGetArray(a->slvec1,&array);
2465:   VecCreateSeqWithArray(PETSC_COMM_SELF,1,bs*mbs,array,&a->slvec1a);
2466:   VecCreateSeqWithArray(PETSC_COMM_SELF,1,nt-bs*mbs,array+bs*mbs,&a->slvec1b);
2467:   VecRestoreArray(a->slvec1,&array);
2468:   VecGetArray(a->slvec0,&array);
2469:   VecCreateSeqWithArray(PETSC_COMM_SELF,1,nt-bs*mbs,array+bs*mbs,&a->slvec0b);
2470:   VecRestoreArray(a->slvec0,&array);
2471:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec0);
2472:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec1);
2473:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec0b);
2474:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec1a);
2475:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec1b);

2477:   /*  VecScatterCopy(oldmat->sMvctx,&a->sMvctx); - not written yet, replaced by the lazy trick: */
2478:   PetscObjectReference((PetscObject)oldmat->sMvctx);
2479:   a->sMvctx = oldmat->sMvctx;
2480:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->sMvctx);

2482:   MatDuplicate(oldmat->A,cpvalues,&a->A);
2483:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
2484:   MatDuplicate(oldmat->B,cpvalues,&a->B);
2485:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);
2486:   PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);
2487:   *newmat = mat;
2488:   return(0);
2489: }

2493: PetscErrorCode MatLoad_MPISBAIJ(Mat newmat,PetscViewer viewer)
2494: {
2496:   PetscInt       i,nz,j,rstart,rend;
2497:   PetscScalar    *vals,*buf;
2498:   MPI_Comm       comm;
2499:   MPI_Status     status;
2500:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag,*sndcounts = 0,*browners,maxnz,*rowners,mmbs;
2501:   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols,*locrowlens;
2502:   PetscInt       *procsnz = 0,jj,*mycols,*ibuf;
2503:   PetscInt       bs = newmat->rmap->bs,Mbs,mbs,extra_rows;
2504:   PetscInt       *dlens,*odlens,*mask,*masked1,*masked2,rowcount,odcount;
2505:   PetscInt       dcount,kmax,k,nzcount,tmp;
2506:   int            fd;

2509:   /* force binary viewer to load .info file if it has not yet done so */
2510:   PetscViewerSetUp(viewer);
2511:   PetscObjectGetComm((PetscObject)viewer,&comm);
2512:   PetscOptionsBegin(comm,NULL,"Options for loading MPISBAIJ matrix 2","Mat");
2513:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
2514:   PetscOptionsEnd();
2515:   if (bs < 0) bs = 1;

2517:   MPI_Comm_size(comm,&size);
2518:   MPI_Comm_rank(comm,&rank);
2519:   PetscViewerBinaryGetDescriptor(viewer,&fd);
2520:   if (!rank) {
2521:     PetscBinaryRead(fd,(char*)header,4,PETSC_INT);
2522:     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
2523:     if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as MPISBAIJ");
2524:   }

2526:   MPI_Bcast(header+1,3,MPIU_INT,0,comm);
2527:   M    = header[1];
2528:   N    = header[2];

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

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

2536:   /*
2537:      This code adds extra rows to make sure the number of rows is
2538:      divisible by the blocksize
2539:   */
2540:   Mbs        = M/bs;
2541:   extra_rows = bs - M + bs*(Mbs);
2542:   if (extra_rows == bs) extra_rows = 0;
2543:   else                  Mbs++;
2544:   if (extra_rows &&!rank) {
2545:     PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");
2546:   }

2548:   /* determine ownership of all rows */
2549:   if (newmat->rmap->n < 0) { /* PETSC_DECIDE */
2550:     mbs = Mbs/size + ((Mbs % size) > rank);
2551:     m   = mbs*bs;
2552:   } else { /* User Set */
2553:     m   = newmat->rmap->n;
2554:     mbs = m/bs;
2555:   }
2556:   PetscMalloc2(size+1,&rowners,size+1,&browners);
2557:   PetscMPIIntCast(mbs,&mmbs);
2558:   MPI_Allgather(&mmbs,1,MPI_INT,rowners+1,1,MPI_INT,comm);
2559:   rowners[0] = 0;
2560:   for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
2561:   for (i=0; i<=size; i++) browners[i] = rowners[i]*bs;
2562:   rstart = rowners[rank];
2563:   rend   = rowners[rank+1];

2565:   /* distribute row lengths to all processors */
2566:   PetscMalloc1((rend-rstart)*bs,&locrowlens);
2567:   if (!rank) {
2568:     PetscMalloc1(M+extra_rows,&rowlengths);
2569:     PetscBinaryRead(fd,rowlengths,M,PETSC_INT);
2570:     for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
2571:     PetscMalloc1(size,&sndcounts);
2572:     for (i=0; i<size; i++) sndcounts[i] = browners[i+1] - browners[i];
2573:     MPI_Scatterv(rowlengths,sndcounts,browners,MPIU_INT,locrowlens,(rend-rstart)*bs,MPIU_INT,0,comm);
2574:     PetscFree(sndcounts);
2575:   } else {
2576:     MPI_Scatterv(0,0,0,MPIU_INT,locrowlens,(rend-rstart)*bs,MPIU_INT,0,comm);
2577:   }

2579:   if (!rank) {   /* procs[0] */
2580:     /* calculate the number of nonzeros on each processor */
2581:     PetscMalloc1(size,&procsnz);
2582:     PetscMemzero(procsnz,size*sizeof(PetscInt));
2583:     for (i=0; i<size; i++) {
2584:       for (j=rowners[i]*bs; j< rowners[i+1]*bs; j++) {
2585:         procsnz[i] += rowlengths[j];
2586:       }
2587:     }
2588:     PetscFree(rowlengths);

2590:     /* determine max buffer needed and allocate it */
2591:     maxnz = 0;
2592:     for (i=0; i<size; i++) {
2593:       maxnz = PetscMax(maxnz,procsnz[i]);
2594:     }
2595:     PetscMalloc1(maxnz,&cols);

2597:     /* read in my part of the matrix column indices  */
2598:     nz     = procsnz[0];
2599:     PetscMalloc1(nz,&ibuf);
2600:     mycols = ibuf;
2601:     if (size == 1) nz -= extra_rows;
2602:     PetscBinaryRead(fd,mycols,nz,PETSC_INT);
2603:     if (size == 1) {
2604:       for (i=0; i< extra_rows; i++) mycols[nz+i] = M+i;
2605:     }

2607:     /* read in every ones (except the last) and ship off */
2608:     for (i=1; i<size-1; i++) {
2609:       nz   = procsnz[i];
2610:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
2611:       MPI_Send(cols,nz,MPIU_INT,i,tag,comm);
2612:     }
2613:     /* read in the stuff for the last proc */
2614:     if (size != 1) {
2615:       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
2616:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
2617:       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
2618:       MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);
2619:     }
2620:     PetscFree(cols);
2621:   } else {  /* procs[i], i>0 */
2622:     /* determine buffer space needed for message */
2623:     nz = 0;
2624:     for (i=0; i<m; i++) nz += locrowlens[i];
2625:     PetscMalloc1(nz,&ibuf);
2626:     mycols = ibuf;
2627:     /* receive message of column indices*/
2628:     MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);
2629:     MPI_Get_count(&status,MPIU_INT,&maxnz);
2630:     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2631:   }

2633:   /* loop over local rows, determining number of off diagonal entries */
2634:   PetscMalloc2(rend-rstart,&dlens,rend-rstart,&odlens);
2635:   PetscMalloc3(Mbs,&mask,Mbs,&masked1,Mbs,&masked2);
2636:   PetscMemzero(mask,Mbs*sizeof(PetscInt));
2637:   PetscMemzero(masked1,Mbs*sizeof(PetscInt));
2638:   PetscMemzero(masked2,Mbs*sizeof(PetscInt));
2639:   rowcount = 0;
2640:   nzcount  = 0;
2641:   for (i=0; i<mbs; i++) {
2642:     dcount  = 0;
2643:     odcount = 0;
2644:     for (j=0; j<bs; j++) {
2645:       kmax = locrowlens[rowcount];
2646:       for (k=0; k<kmax; k++) {
2647:         tmp = mycols[nzcount++]/bs; /* block col. index */
2648:         if (!mask[tmp]) {
2649:           mask[tmp] = 1;
2650:           if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp; /* entry in off-diag portion */
2651:           else masked1[dcount++] = tmp; /* entry in diag portion */
2652:         }
2653:       }
2654:       rowcount++;
2655:     }

2657:     dlens[i]  = dcount;  /* d_nzz[i] */
2658:     odlens[i] = odcount; /* o_nzz[i] */

2660:     /* zero out the mask elements we set */
2661:     for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
2662:     for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
2663:   }
2664:   MatSetSizes(newmat,m,m,M+extra_rows,N+extra_rows);
2665:   MatMPISBAIJSetPreallocation(newmat,bs,0,dlens,0,odlens);
2666:   MatSetOption(newmat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE);

2668:   if (!rank) {
2669:     PetscMalloc1(maxnz,&buf);
2670:     /* read in my part of the matrix numerical values  */
2671:     nz     = procsnz[0];
2672:     vals   = buf;
2673:     mycols = ibuf;
2674:     if (size == 1) nz -= extra_rows;
2675:     PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2676:     if (size == 1) {
2677:       for (i=0; i< extra_rows; i++) vals[nz+i] = 1.0;
2678:     }

2680:     /* insert into matrix */
2681:     jj = rstart*bs;
2682:     for (i=0; i<m; i++) {
2683:       MatSetValues(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
2684:       mycols += locrowlens[i];
2685:       vals   += locrowlens[i];
2686:       jj++;
2687:     }

2689:     /* read in other processors (except the last one) and ship out */
2690:     for (i=1; i<size-1; i++) {
2691:       nz   = procsnz[i];
2692:       vals = buf;
2693:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2694:       MPI_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);
2695:     }
2696:     /* the last proc */
2697:     if (size != 1) {
2698:       nz   = procsnz[i] - extra_rows;
2699:       vals = buf;
2700:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2701:       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
2702:       MPI_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)newmat)->tag,comm);
2703:     }
2704:     PetscFree(procsnz);

2706:   } else {
2707:     /* receive numeric values */
2708:     PetscMalloc1(nz,&buf);

2710:     /* receive message of values*/
2711:     vals   = buf;
2712:     mycols = ibuf;
2713:     MPI_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm,&status);
2714:     MPI_Get_count(&status,MPIU_SCALAR,&maxnz);
2715:     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");

2717:     /* insert into matrix */
2718:     jj = rstart*bs;
2719:     for (i=0; i<m; i++) {
2720:       MatSetValues_MPISBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
2721:       mycols += locrowlens[i];
2722:       vals   += locrowlens[i];
2723:       jj++;
2724:     }
2725:   }

2727:   PetscFree(locrowlens);
2728:   PetscFree(buf);
2729:   PetscFree(ibuf);
2730:   PetscFree2(rowners,browners);
2731:   PetscFree2(dlens,odlens);
2732:   PetscFree3(mask,masked1,masked2);
2733:   MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
2734:   MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
2735:   return(0);
2736: }

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

2743:    Input Parameters:
2744: .  mat  - the matrix
2745: .  fact - factor

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

2749:    Level: advanced

2751:   Notes:
2752:    This can also be set by the command line option: -mat_use_hash_table fact

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

2756: .seealso: MatSetOption()
2757: @XXXXX*/


2762: PetscErrorCode MatGetRowMaxAbs_MPISBAIJ(Mat A,Vec v,PetscInt idx[])
2763: {
2764:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;
2765:   Mat_SeqBAIJ    *b = (Mat_SeqBAIJ*)(a->B)->data;
2766:   PetscReal      atmp;
2767:   PetscReal      *work,*svalues,*rvalues;
2769:   PetscInt       i,bs,mbs,*bi,*bj,brow,j,ncols,krow,kcol,col,row,Mbs,bcol;
2770:   PetscMPIInt    rank,size;
2771:   PetscInt       *rowners_bs,dest,count,source;
2772:   PetscScalar    *va;
2773:   MatScalar      *ba;
2774:   MPI_Status     stat;

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

2781:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
2782:   MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);

2784:   bs  = A->rmap->bs;
2785:   mbs = a->mbs;
2786:   Mbs = a->Mbs;
2787:   ba  = b->a;
2788:   bi  = b->i;
2789:   bj  = b->j;

2791:   /* find ownerships */
2792:   rowners_bs = A->rmap->range;

2794:   /* each proc creates an array to be distributed */
2795:   PetscMalloc1(bs*Mbs,&work);
2796:   PetscMemzero(work,bs*Mbs*sizeof(PetscReal));

2798:   /* row_max for B */
2799:   if (rank != size-1) {
2800:     for (i=0; i<mbs; i++) {
2801:       ncols = bi[1] - bi[0]; bi++;
2802:       brow  = bs*i;
2803:       for (j=0; j<ncols; j++) {
2804:         bcol = bs*(*bj);
2805:         for (kcol=0; kcol<bs; kcol++) {
2806:           col  = bcol + kcol;                /* local col index */
2807:           col += rowners_bs[rank+1];      /* global col index */
2808:           for (krow=0; krow<bs; krow++) {
2809:             atmp = PetscAbsScalar(*ba); ba++;
2810:             row  = brow + krow;   /* local row index */
2811:             if (PetscRealPart(va[row]) < atmp) va[row] = atmp;
2812:             if (work[col] < atmp) work[col] = atmp;
2813:           }
2814:         }
2815:         bj++;
2816:       }
2817:     }

2819:     /* send values to its owners */
2820:     for (dest=rank+1; dest<size; dest++) {
2821:       svalues = work + rowners_bs[dest];
2822:       count   = rowners_bs[dest+1]-rowners_bs[dest];
2823:       MPI_Send(svalues,count,MPIU_REAL,dest,rank,PetscObjectComm((PetscObject)A));
2824:     }
2825:   }

2827:   /* receive values */
2828:   if (rank) {
2829:     rvalues = work;
2830:     count   = rowners_bs[rank+1]-rowners_bs[rank];
2831:     for (source=0; source<rank; source++) {
2832:       MPI_Recv(rvalues,count,MPIU_REAL,MPI_ANY_SOURCE,MPI_ANY_TAG,PetscObjectComm((PetscObject)A),&stat);
2833:       /* process values */
2834:       for (i=0; i<count; i++) {
2835:         if (PetscRealPart(va[i]) < rvalues[i]) va[i] = rvalues[i];
2836:       }
2837:     }
2838:   }

2840:   VecRestoreArray(v,&va);
2841:   PetscFree(work);
2842:   return(0);
2843: }

2847: PetscErrorCode MatSOR_MPISBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2848: {
2849:   Mat_MPISBAIJ      *mat = (Mat_MPISBAIJ*)matin->data;
2850:   PetscErrorCode    ierr;
2851:   PetscInt          mbs=mat->mbs,bs=matin->rmap->bs;
2852:   PetscScalar       *x,*ptr,*from;
2853:   Vec               bb1;
2854:   const PetscScalar *b;

2857:   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);
2858:   if (bs > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SSOR for block size > 1 is not yet implemented");

2860:   if (flag == SOR_APPLY_UPPER) {
2861:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2862:     return(0);
2863:   }

2865:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2866:     if (flag & SOR_ZERO_INITIAL_GUESS) {
2867:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,lits,xx);
2868:       its--;
2869:     }

2871:     VecDuplicate(bb,&bb1);
2872:     while (its--) {

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

2877:       /* copy xx into slvec0a */
2878:       VecGetArray(mat->slvec0,&ptr);
2879:       VecGetArray(xx,&x);
2880:       PetscMemcpy(ptr,x,bs*mbs*sizeof(MatScalar));
2881:       VecRestoreArray(mat->slvec0,&ptr);

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

2885:       /* copy bb into slvec1a */
2886:       VecGetArray(mat->slvec1,&ptr);
2887:       VecGetArrayRead(bb,&b);
2888:       PetscMemcpy(ptr,b,bs*mbs*sizeof(MatScalar));
2889:       VecRestoreArray(mat->slvec1,&ptr);

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

2894:       VecScatterBegin(mat->sMvctx,mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD);
2895:       VecRestoreArray(xx,&x);
2896:       VecRestoreArrayRead(bb,&b);
2897:       VecScatterEnd(mat->sMvctx,mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD);

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

2902:       /* local diagonal sweep */
2903:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
2904:     }
2905:     VecDestroy(&bb1);
2906:   } else if ((flag & SOR_LOCAL_FORWARD_SWEEP) && (its == 1) && (flag & SOR_ZERO_INITIAL_GUESS)) {
2907:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2908:   } else if ((flag & SOR_LOCAL_BACKWARD_SWEEP) && (its == 1) && (flag & SOR_ZERO_INITIAL_GUESS)) {
2909:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2910:   } else if (flag & SOR_EISENSTAT) {
2911:     Vec               xx1;
2912:     PetscBool         hasop;
2913:     const PetscScalar *diag;
2914:     PetscScalar       *sl,scale = (omega - 2.0)/omega;
2915:     PetscInt          i,n;

2917:     if (!mat->xx1) {
2918:       VecDuplicate(bb,&mat->xx1);
2919:       VecDuplicate(bb,&mat->bb1);
2920:     }
2921:     xx1 = mat->xx1;
2922:     bb1 = mat->bb1;

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

2926:     if (!mat->diag) {
2927:       /* this is wrong for same matrix with new nonzero values */
2928:       MatCreateVecs(matin,&mat->diag,NULL);
2929:       MatGetDiagonal(matin,mat->diag);
2930:     }
2931:     MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);

2933:     if (hasop) {
2934:       MatMultDiagonalBlock(matin,xx,bb1);
2935:       VecAYPX(mat->slvec1a,scale,bb);
2936:     } else {
2937:       /*
2938:           These two lines are replaced by code that may be a bit faster for a good compiler
2939:       VecPointwiseMult(mat->slvec1a,mat->diag,xx);
2940:       VecAYPX(mat->slvec1a,scale,bb);
2941:       */
2942:       VecGetArray(mat->slvec1a,&sl);
2943:       VecGetArrayRead(mat->diag,&diag);
2944:       VecGetArrayRead(bb,&b);
2945:       VecGetArray(xx,&x);
2946:       VecGetLocalSize(xx,&n);
2947:       if (omega == 1.0) {
2948:         for (i=0; i<n; i++) sl[i] = b[i] - diag[i]*x[i];
2949:         PetscLogFlops(2.0*n);
2950:       } else {
2951:         for (i=0; i<n; i++) sl[i] = b[i] + scale*diag[i]*x[i];
2952:         PetscLogFlops(3.0*n);
2953:       }
2954:       VecRestoreArray(mat->slvec1a,&sl);
2955:       VecRestoreArrayRead(mat->diag,&diag);
2956:       VecRestoreArrayRead(bb,&b);
2957:       VecRestoreArray(xx,&x);
2958:     }

2960:     /* multiply off-diagonal portion of matrix */
2961:     VecSet(mat->slvec1b,0.0);
2962:     (*mat->B->ops->multtranspose)(mat->B,xx,mat->slvec0b);
2963:     VecGetArray(mat->slvec0,&from);
2964:     VecGetArray(xx,&x);
2965:     PetscMemcpy(from,x,bs*mbs*sizeof(MatScalar));
2966:     VecRestoreArray(mat->slvec0,&from);
2967:     VecRestoreArray(xx,&x);
2968:     VecScatterBegin(mat->sMvctx,mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD);
2969:     VecScatterEnd(mat->sMvctx,mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD);
2970:     (*mat->B->ops->multadd)(mat->B,mat->slvec1b,mat->slvec1a,mat->slvec1a);

2972:     /* local sweep */
2973:     (*mat->A->ops->sor)(mat->A,mat->slvec1a,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_FORWARD_SWEEP),fshift,lits,1,xx1);
2974:     VecAXPY(xx,1.0,xx1);
2975:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatSORType is not supported for SBAIJ matrix format");
2976:   return(0);
2977: }

2981: PetscErrorCode MatSOR_MPISBAIJ_2comm(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2982: {
2983:   Mat_MPISBAIJ   *mat = (Mat_MPISBAIJ*)matin->data;
2985:   Vec            lvec1,bb1;

2988:   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);
2989:   if (matin->rmap->bs > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SSOR for block size > 1 is not yet implemented");

2991:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2992:     if (flag & SOR_ZERO_INITIAL_GUESS) {
2993:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,lits,xx);
2994:       its--;
2995:     }

2997:     VecDuplicate(mat->lvec,&lvec1);
2998:     VecDuplicate(bb,&bb1);
2999:     while (its--) {
3000:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

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

3006:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
3007:       VecCopy(bb,bb1);
3008:       VecScatterBegin(mat->Mvctx,lvec1,bb1,ADD_VALUES,SCATTER_REVERSE);

3010:       /* upper diagonal part: bb1 = bb1 - B*x */
3011:       VecScale(mat->lvec,-1.0);
3012:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb1,bb1);

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

3016:       /* diagonal sweep */
3017:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
3018:     }
3019:     VecDestroy(&lvec1);
3020:     VecDestroy(&bb1);
3021:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatSORType is not supported for SBAIJ matrix format");
3022:   return(0);
3023: }

3027: /*@
3028:      MatCreateMPISBAIJWithArrays - creates a MPI SBAIJ matrix using arrays that contain in standard
3029:          CSR format the local rows.

3031:    Collective on MPI_Comm

3033:    Input Parameters:
3034: +  comm - MPI communicator
3035: .  bs - the block size, only a block size of 1 is supported
3036: .  m - number of local rows (Cannot be PETSC_DECIDE)
3037: .  n - This value should be the same as the local size used in creating the
3038:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
3039:        calculated if N is given) For square matrices n is almost always m.
3040: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3041: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
3042: .   i - row indices
3043: .   j - column indices
3044: -   a - matrix values

3046:    Output Parameter:
3047: .   mat - the matrix

3049:    Level: intermediate

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

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

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

3060: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
3061:           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
3062: @*/
3063: 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)
3064: {


3069:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
3070:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
3071:   MatCreate(comm,mat);
3072:   MatSetSizes(*mat,m,n,M,N);
3073:   MatSetType(*mat,MATMPISBAIJ);
3074:   MatMPISBAIJSetPreallocationCSR(*mat,bs,i,j,a);
3075:   return(0);
3076: }


3081: /*@C
3082:    MatMPISBAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in BAIJ format
3083:    (the default parallel PETSc format).

3085:    Collective on MPI_Comm

3087:    Input Parameters:
3088: +  B - the matrix
3089: .  bs - the block size
3090: .  i - the indices into j for the start of each local row (starts with zero)
3091: .  j - the column indices for each local row (starts with zero) these must be sorted for each row
3092: -  v - optional values in the matrix

3094:    Level: developer

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

3098: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ
3099: @*/
3100: PetscErrorCode  MatMPISBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3101: {

3105:   PetscTryMethod(B,"MatMPISBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));
3106:   return(0);
3107: }

3111: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPISBAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
3112: {
3114:   PetscInt       m,N,i,rstart,nnz,Ii,bs,cbs;
3115:   PetscInt       *indx;
3116:   PetscScalar    *values;

3119:   MatGetSize(inmat,&m,&N);
3120:   if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
3121:     Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)inmat->data;
3122:     PetscInt       *dnz,*onz,sum,bs,cbs,mbs,Nbs;
3123:     PetscInt       *bindx,rmax=a->rmax,j;
3124: 
3125:     MatGetBlockSizes(inmat,&bs,&cbs);
3126:     mbs = m/bs; Nbs = N/cbs;
3127:     if (n == PETSC_DECIDE) {
3128:       PetscSplitOwnership(comm,&n,&Nbs);
3129:     }
3130:     /* Check sum(n) = Nbs */
3131:     MPI_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);
3132:     if (sum != Nbs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",Nbs);

3134:     MPI_Scan(&mbs, &rstart,1,MPIU_INT,MPI_SUM,comm);
3135:     rstart -= mbs;

3137:     PetscMalloc1(rmax,&bindx);
3138:     MatPreallocateInitialize(comm,mbs,n,dnz,onz);
3139:     MatSetOption(inmat,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE);
3140:     for (i=0; i<mbs; i++) {
3141:       MatGetRow_SeqSBAIJ(inmat,i*bs,&nnz,&indx,NULL); /* non-blocked nnz and indx */
3142:       nnz = nnz/bs;
3143:       for (j=0; j<nnz; j++) bindx[j] = indx[j*bs]/bs;
3144:       MatPreallocateSet(i+rstart,nnz,bindx,dnz,onz);
3145:       MatRestoreRow_SeqSBAIJ(inmat,i*bs,&nnz,&indx,NULL);
3146:     }
3147:     MatSetOption(inmat,MAT_GETROW_UPPERTRIANGULAR,PETSC_FALSE);
3148:     PetscFree(bindx);

3150:     MatCreate(comm,outmat);
3151:     MatSetSizes(*outmat,m,n*bs,PETSC_DETERMINE,PETSC_DETERMINE);
3152:     MatSetBlockSizes(*outmat,bs,cbs);
3153:     MatSetType(*outmat,MATMPISBAIJ);
3154:     MatMPISBAIJSetPreallocation(*outmat,bs,0,dnz,0,onz);
3155:     MatPreallocateFinalize(dnz,onz);
3156:   }
3157: 
3158:   /* numeric phase */
3159:   MatGetBlockSizes(inmat,&bs,&cbs);
3160:   MatGetOwnershipRange(*outmat,&rstart,NULL);

3162:   MatSetOption(inmat,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE);
3163:   for (i=0; i<m; i++) {
3164:     MatGetRow_SeqSBAIJ(inmat,i,&nnz,&indx,&values);
3165:     Ii   = i + rstart;
3166:     MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);
3167:     MatRestoreRow_SeqSBAIJ(inmat,i,&nnz,&indx,&values);
3168:   }
3169:   MatSetOption(inmat,MAT_GETROW_UPPERTRIANGULAR,PETSC_FALSE);
3170:   MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);
3171:   MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);
3172:   return(0);
3173: }