Actual source code: mpisbaij.c

petsc-master 2014-12-26
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: extern PetscErrorCode MatSetUpMultiply_MPISBAIJ(Mat);
  8: extern PetscErrorCode MatSetUpMultiply_MPISBAIJ_2comm(Mat);
  9: extern PetscErrorCode MatDisAssemble_MPISBAIJ(Mat);
 10: extern PetscErrorCode MatIncreaseOverlap_MPISBAIJ(Mat,PetscInt,IS[],PetscInt);
 11: extern PetscErrorCode MatGetValues_SeqSBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],PetscScalar []);
 12: extern PetscErrorCode MatGetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],PetscScalar []);
 13: extern PetscErrorCode MatSetValues_SeqSBAIJ(Mat,PetscInt,const PetscInt [],PetscInt,const PetscInt [],const PetscScalar [],InsertMode);
 14: extern PetscErrorCode MatSetValuesBlocked_SeqSBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
 15: extern PetscErrorCode MatSetValuesBlocked_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
 16: extern PetscErrorCode MatGetRow_SeqSBAIJ(Mat,PetscInt,PetscInt*,PetscInt**,PetscScalar**);
 17: extern PetscErrorCode MatRestoreRow_SeqSBAIJ(Mat,PetscInt,PetscInt*,PetscInt**,PetscScalar**);
 18: extern PetscErrorCode MatZeroRows_SeqSBAIJ(Mat,IS,PetscScalar*,Vec,Vec);
 19: extern PetscErrorCode MatZeroRows_SeqBAIJ(Mat,IS,PetscScalar*,Vec,Vec);
 20: extern PetscErrorCode MatGetRowMaxAbs_MPISBAIJ(Mat,Vec,PetscInt[]);
 21: extern PetscErrorCode MatSOR_MPISBAIJ(Mat,Vec,PetscReal,MatSORType,PetscReal,PetscInt,PetscInt,Vec);

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

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

 38: PetscErrorCode  MatRetrieveValues_MPISBAIJ(Mat mat)
 39: {
 40:   Mat_MPISBAIJ   *aij = (Mat_MPISBAIJ*)mat->data;

 44:   MatRetrieveValues(aij->A);
 45:   MatRetrieveValues(aij->B);
 46:   return(0);
 47: }

 49: #define  MatSetValues_SeqSBAIJ_A_Private(row,col,value,addv) \
 50:   { \
 51:  \
 52:     brow = row/bs;  \
 53:     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
 54:     rmax = aimax[brow]; nrow = ailen[brow]; \
 55:     bcol = col/bs; \
 56:     ridx = row % bs; cidx = col % bs; \
 57:     low  = 0; high = nrow; \
 58:     while (high-low > 3) { \
 59:       t = (low+high)/2; \
 60:       if (rp[t] > bcol) high = t; \
 61:       else              low  = t; \
 62:     } \
 63:     for (_i=low; _i<high; _i++) { \
 64:       if (rp[_i] > bcol) break; \
 65:       if (rp[_i] == bcol) { \
 66:         bap = ap + bs2*_i + bs*cidx + ridx; \
 67:         if (addv == ADD_VALUES) *bap += value;  \
 68:         else                    *bap  = value;  \
 69:         goto a_noinsert; \
 70:       } \
 71:     } \
 72:     if (a->nonew == 1) goto a_noinsert; \
 73:     if (a->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
 74:     MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,aimax,a->nonew,MatScalar); \
 75:     N = nrow++ - 1;  \
 76:     /* shift up all the later entries in this row */ \
 77:     for (ii=N; ii>=_i; ii--) { \
 78:       rp[ii+1] = rp[ii]; \
 79:       PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar)); \
 80:     } \
 81:     if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar)); }  \
 82:     rp[_i]                      = bcol;  \
 83:     ap[bs2*_i + bs*cidx + ridx] = value;  \
 84:     A->nonzerostate++;\
 85: a_noinsert:; \
 86:     ailen[brow] = nrow; \
 87:   }

 89: #define  MatSetValues_SeqSBAIJ_B_Private(row,col,value,addv) \
 90:   { \
 91:     brow = row/bs;  \
 92:     rp   = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
 93:     rmax = bimax[brow]; nrow = bilen[brow]; \
 94:     bcol = col/bs; \
 95:     ridx = row % bs; cidx = col % bs; \
 96:     low  = 0; high = nrow; \
 97:     while (high-low > 3) { \
 98:       t = (low+high)/2; \
 99:       if (rp[t] > bcol) high = t; \
100:       else              low  = t; \
101:     } \
102:     for (_i=low; _i<high; _i++) { \
103:       if (rp[_i] > bcol) break; \
104:       if (rp[_i] == bcol) { \
105:         bap = ap + bs2*_i + bs*cidx + ridx; \
106:         if (addv == ADD_VALUES) *bap += value;  \
107:         else                    *bap  = value;  \
108:         goto b_noinsert; \
109:       } \
110:     } \
111:     if (b->nonew == 1) goto b_noinsert; \
112:     if (b->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
113:     MatSeqXAIJReallocateAIJ(B,b->mbs,bs2,nrow,brow,bcol,rmax,ba,bi,bj,rp,ap,bimax,b->nonew,MatScalar); \
114:     N = nrow++ - 1;  \
115:     /* shift up all the later entries in this row */ \
116:     for (ii=N; ii>=_i; ii--) { \
117:       rp[ii+1] = rp[ii]; \
118:       PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar)); \
119:     } \
120:     if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));}  \
121:     rp[_i]                      = bcol;  \
122:     ap[bs2*_i + bs*cidx + ridx] = value;  \
123:     B->nonzerostate++;\
124: b_noinsert:; \
125:     bilen[brow] = nrow; \
126:   }

128: /* Only add/insert a(i,j) with i<=j (blocks).
129:    Any a(i,j) with i>j input by user is ingored.
130: */
133: PetscErrorCode MatSetValues_MPISBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
134: {
135:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
136:   MatScalar      value;
137:   PetscBool      roworiented = baij->roworiented;
139:   PetscInt       i,j,row,col;
140:   PetscInt       rstart_orig=mat->rmap->rstart;
141:   PetscInt       rend_orig  =mat->rmap->rend,cstart_orig=mat->cmap->rstart;
142:   PetscInt       cend_orig  =mat->cmap->rend,bs=mat->rmap->bs;

144:   /* Some Variables required in the macro */
145:   Mat          A     = baij->A;
146:   Mat_SeqSBAIJ *a    = (Mat_SeqSBAIJ*)(A)->data;
147:   PetscInt     *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
148:   MatScalar    *aa   =a->a;

150:   Mat         B     = baij->B;
151:   Mat_SeqBAIJ *b    = (Mat_SeqBAIJ*)(B)->data;
152:   PetscInt    *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
153:   MatScalar   *ba   =b->a;

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

159:   /* for stash */
160:   PetscInt  n_loc, *in_loc = NULL;
161:   MatScalar *v_loc = NULL;

164:   if (!baij->donotstash) {
165:     if (n > baij->n_loc) {
166:       PetscFree(baij->in_loc);
167:       PetscFree(baij->v_loc);
168:       PetscMalloc1(n,&baij->in_loc);
169:       PetscMalloc1(n,&baij->v_loc);

171:       baij->n_loc = n;
172:     }
173:     in_loc = baij->in_loc;
174:     v_loc  = baij->v_loc;
175:   }

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

253: PetscErrorCode MatSetValuesBlocked_MPISBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const MatScalar v[],InsertMode addv)
254: {
255:   Mat_MPISBAIJ    *baij = (Mat_MPISBAIJ*)mat->data;
256:   const MatScalar *value;
257:   MatScalar       *barray     =baij->barray;
258:   PetscBool       roworiented = baij->roworiented,ignore_ltriangular = ((Mat_SeqSBAIJ*)baij->A->data)->ignore_ltriangular;
259:   PetscErrorCode  ierr;
260:   PetscInt        i,j,ii,jj,row,col,rstart=baij->rstartbs;
261:   PetscInt        rend=baij->rendbs,cstart=baij->rstartbs,stepval;
262:   PetscInt        cend=baij->rendbs,bs=mat->rmap->bs,bs2=baij->bs2;

265:   if (!barray) {
266:     PetscMalloc1(bs2,&barray);
267:     baij->barray = barray;
268:   }

270:   if (roworiented) {
271:     stepval = (n-1)*bs;
272:   } else {
273:     stepval = (m-1)*bs;
274:   }
275:   for (i=0; i<m; i++) {
276:     if (im[i] < 0) continue;
277: #if defined(PETSC_USE_DEBUG)
278:     if (im[i] >= baij->Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large, row %D max %D",im[i],baij->Mbs-1);
279: #endif
280:     if (im[i] >= rstart && im[i] < rend) {
281:       row = im[i] - rstart;
282:       for (j=0; j<n; j++) {
283:         if (im[i] > in[j]) {
284:           if (ignore_ltriangular) continue; /* ignore lower triangular blocks */
285:           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)");
286:         }
287:         /* If NumCol = 1 then a copy is not required */
288:         if ((roworiented) && (n == 1)) {
289:           barray = (MatScalar*) v + i*bs2;
290:         } else if ((!roworiented) && (m == 1)) {
291:           barray = (MatScalar*) v + j*bs2;
292:         } else { /* Here a copy is required */
293:           if (roworiented) {
294:             value = v + i*(stepval+bs)*bs + j*bs;
295:           } else {
296:             value = v + j*(stepval+bs)*bs + i*bs;
297:           }
298:           for (ii=0; ii<bs; ii++,value+=stepval) {
299:             for (jj=0; jj<bs; jj++) {
300:               *barray++ = *value++;
301:             }
302:           }
303:           barray -=bs2;
304:         }

306:         if (in[j] >= cstart && in[j] < cend) {
307:           col  = in[j] - cstart;
308:           MatSetValuesBlocked_SeqSBAIJ(baij->A,1,&row,1,&col,barray,addv);
309:         } else if (in[j] < 0) continue;
310: #if defined(PETSC_USE_DEBUG)
311:         else if (in[j] >= baij->Nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large, col %D max %D",in[j],baij->Nbs-1);
312: #endif
313:         else {
314:           if (mat->was_assembled) {
315:             if (!baij->colmap) {
316:               MatCreateColmap_MPIBAIJ_Private(mat);
317:             }

319: #if defined(PETSC_USE_DEBUG)
320: #if defined(PETSC_USE_CTABLE)
321:             { PetscInt data;
322:               PetscTableFind(baij->colmap,in[j]+1,&data);
323:               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
324:             }
325: #else
326:             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
327: #endif
328: #endif
329: #if defined(PETSC_USE_CTABLE)
330:             PetscTableFind(baij->colmap,in[j]+1,&col);
331:             col  = (col - 1)/bs;
332: #else
333:             col = (baij->colmap[in[j]] - 1)/bs;
334: #endif
335:             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
336:               MatDisAssemble_MPISBAIJ(mat);
337:               col  = in[j];
338:             }
339:           } else col = in[j];
340:           MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);
341:         }
342:       }
343:     } else {
344:       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]);
345:       if (!baij->donotstash) {
346:         if (roworiented) {
347:           MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
348:         } else {
349:           MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
350:         }
351:       }
352:     }
353:   }
354:   return(0);
355: }

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

367:   for (i=0; i<m; i++) {
368:     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]); */
369:     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);
370:     if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
371:       row = idxm[i] - bsrstart;
372:       for (j=0; j<n; j++) {
373:         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column %D",idxn[j]); */
374:         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);
375:         if (idxn[j] >= bscstart && idxn[j] < bscend) {
376:           col  = idxn[j] - bscstart;
377:           MatGetValues_SeqSBAIJ(baij->A,1,&row,1,&col,v+i*n+j);
378:         } else {
379:           if (!baij->colmap) {
380:             MatCreateColmap_MPIBAIJ_Private(mat);
381:           }
382: #if defined(PETSC_USE_CTABLE)
383:           PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);
384:           data--;
385: #else
386:           data = baij->colmap[idxn[j]/bs]-1;
387: #endif
388:           if ((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
389:           else {
390:             col  = data + idxn[j]%bs;
391:             MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);
392:           }
393:         }
394:       }
395:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
396:   }
397:   return(0);
398: }

402: PetscErrorCode MatNorm_MPISBAIJ(Mat mat,NormType type,PetscReal *norm)
403: {
404:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
406:   PetscReal      sum[2],*lnorm2;

409:   if (baij->size == 1) {
410:      MatNorm(baij->A,type,norm);
411:   } else {
412:     if (type == NORM_FROBENIUS) {
413:       PetscMalloc1(2,&lnorm2);
414:        MatNorm(baij->A,type,lnorm2);
415:       *lnorm2 = (*lnorm2)*(*lnorm2); lnorm2++;            /* squar power of norm(A) */
416:        MatNorm(baij->B,type,lnorm2);
417:       *lnorm2 = (*lnorm2)*(*lnorm2); lnorm2--;             /* squar power of norm(B) */
418:       MPI_Allreduce(lnorm2,sum,2,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
419:       *norm   = PetscSqrtReal(sum[0] + 2*sum[1]);
420:       PetscFree(lnorm2);
421:     } else if (type == NORM_INFINITY || type == NORM_1) { /* max row/column sum */
422:       Mat_SeqSBAIJ *amat=(Mat_SeqSBAIJ*)baij->A->data;
423:       Mat_SeqBAIJ  *bmat=(Mat_SeqBAIJ*)baij->B->data;
424:       PetscReal    *rsum,*rsum2,vabs;
425:       PetscInt     *jj,*garray=baij->garray,rstart=baij->rstartbs,nz;
426:       PetscInt     brow,bcol,col,bs=baij->A->rmap->bs,row,grow,gcol,mbs=amat->mbs;
427:       MatScalar    *v;

429:       PetscMalloc2(mat->cmap->N,&rsum,mat->cmap->N,&rsum2);
430:       PetscMemzero(rsum,mat->cmap->N*sizeof(PetscReal));
431:       /* Amat */
432:       v = amat->a; jj = amat->j;
433:       for (brow=0; brow<mbs; brow++) {
434:         grow = bs*(rstart + brow);
435:         nz   = amat->i[brow+1] - amat->i[brow];
436:         for (bcol=0; bcol<nz; bcol++) {
437:           gcol = bs*(rstart + *jj); jj++;
438:           for (col=0; col<bs; col++) {
439:             for (row=0; row<bs; row++) {
440:               vabs            = PetscAbsScalar(*v); v++;
441:               rsum[gcol+col] += vabs;
442:               /* non-diagonal block */
443:               if (bcol > 0 && vabs > 0.0) rsum[grow+row] += vabs;
444:             }
445:           }
446:         }
447:       }
448:       /* Bmat */
449:       v = bmat->a; jj = bmat->j;
450:       for (brow=0; brow<mbs; brow++) {
451:         grow = bs*(rstart + brow);
452:         nz = bmat->i[brow+1] - bmat->i[brow];
453:         for (bcol=0; bcol<nz; bcol++) {
454:           gcol = bs*garray[*jj]; jj++;
455:           for (col=0; col<bs; col++) {
456:             for (row=0; row<bs; row++) {
457:               vabs            = PetscAbsScalar(*v); v++;
458:               rsum[gcol+col] += vabs;
459:               rsum[grow+row] += vabs;
460:             }
461:           }
462:         }
463:       }
464:       MPI_Allreduce(rsum,rsum2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
465:       *norm = 0.0;
466:       for (col=0; col<mat->cmap->N; col++) {
467:         if (rsum2[col] > *norm) *norm = rsum2[col];
468:       }
469:       PetscFree2(rsum,rsum2);
470:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for this norm yet");
471:   }
472:   return(0);
473: }

477: PetscErrorCode MatAssemblyBegin_MPISBAIJ(Mat mat,MatAssemblyType mode)
478: {
479:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
481:   PetscInt       nstash,reallocs;
482:   InsertMode     addv;

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

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

492:   MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
493:   MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);
494:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
495:   PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);
496:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
497:   PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
498:   return(0);
499: }

503: PetscErrorCode MatAssemblyEnd_MPISBAIJ(Mat mat,MatAssemblyType mode)
504: {
505:   Mat_MPISBAIJ   *baij=(Mat_MPISBAIJ*)mat->data;
506:   Mat_SeqSBAIJ   *a   =(Mat_SeqSBAIJ*)baij->A->data;
508:   PetscInt       i,j,rstart,ncols,flg,bs2=baij->bs2;
509:   PetscInt       *row,*col;
510:   PetscBool      other_disassembled;
511:   PetscMPIInt    n;
512:   PetscBool      r1,r2,r3;
513:   MatScalar      *val;
514:   InsertMode     addv = mat->insertmode;

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

523:       for (i=0; i<n;) {
524:         /* Now identify the consecutive vals belonging to the same row */
525:         for (j=i,rstart=row[j]; j<n; j++) {
526:           if (row[j] != rstart) break;
527:         }
528:         if (j < n) ncols = j-i;
529:         else       ncols = n-i;
530:         /* Now assemble all these values with a single function call */
531:         MatSetValues_MPISBAIJ(mat,1,row+i,ncols,col+i,val+i,addv);
532:         i    = j;
533:       }
534:     }
535:     MatStashScatterEnd_Private(&mat->stash);
536:     /* Now process the block-stash. Since the values are stashed column-oriented,
537:        set the roworiented flag to column oriented, and after MatSetValues()
538:        restore the original flags */
539:     r1 = baij->roworiented;
540:     r2 = a->roworiented;
541:     r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented;

543:     baij->roworiented = PETSC_FALSE;
544:     a->roworiented    = PETSC_FALSE;

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

551:       for (i=0; i<n;) {
552:         /* Now identify the consecutive vals belonging to the same row */
553:         for (j=i,rstart=row[j]; j<n; j++) {
554:           if (row[j] != rstart) break;
555:         }
556:         if (j < n) ncols = j-i;
557:         else       ncols = n-i;
558:         MatSetValuesBlocked_MPISBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,addv);
559:         i    = j;
560:       }
561:     }
562:     MatStashScatterEnd_Private(&mat->bstash);

564:     baij->roworiented = r1;
565:     a->roworiented    = r2;

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

570:   MatAssemblyBegin(baij->A,mode);
571:   MatAssemblyEnd(baij->A,mode);

573:   /* determine if any processor has disassembled, if so we must
574:      also disassemble ourselfs, in order that we may reassemble. */
575:   /*
576:      if nonzero structure of submatrix B cannot change then we know that
577:      no processor disassembled thus we can skip this stuff
578:   */
579:   if (!((Mat_SeqBAIJ*)baij->B->data)->nonew) {
580:     MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));
581:     if (mat->was_assembled && !other_disassembled) {
582:       MatDisAssemble_MPISBAIJ(mat);
583:     }
584:   }

586:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
587:     MatSetUpMultiply_MPISBAIJ(mat); /* setup Mvctx and sMvctx */
588:   }
589:   MatAssemblyBegin(baij->B,mode);
590:   MatAssemblyEnd(baij->B,mode);

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

594:   baij->rowvalues = 0;

596:   /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
597:   if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
598:     PetscObjectState state = baij->A->nonzerostate + baij->B->nonzerostate;
599:     MPI_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));
600:   }
601:   return(0);
602: }

604: extern PetscErrorCode MatView_SeqSBAIJ_ASCII(Mat,PetscViewer);
605: extern PetscErrorCode MatSetValues_MPIBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
606: #include <petscdraw.h>
609: static PetscErrorCode MatView_MPISBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
610: {
611:   Mat_MPISBAIJ      *baij = (Mat_MPISBAIJ*)mat->data;
612:   PetscErrorCode    ierr;
613:   PetscInt          bs   = mat->rmap->bs;
614:   PetscMPIInt       rank = baij->rank;
615:   PetscBool         iascii,isdraw;
616:   PetscViewer       sviewer;
617:   PetscViewerFormat format;

620:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
621:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
622:   if (iascii) {
623:     PetscViewerGetFormat(viewer,&format);
624:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
625:       MatInfo info;
626:       MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
627:       MatGetInfo(mat,MAT_LOCAL,&info);
628:       PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);
629:       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);
630:       MatGetInfo(baij->A,MAT_LOCAL,&info);
631:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
632:       MatGetInfo(baij->B,MAT_LOCAL,&info);
633:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
634:       PetscViewerFlush(viewer);
635:       PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);
636:       PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");
637:       VecScatterView(baij->Mvctx,viewer);
638:       return(0);
639:     } else if (format == PETSC_VIEWER_ASCII_INFO) {
640:       PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);
641:       return(0);
642:     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
643:       return(0);
644:     }
645:   }

647:   if (isdraw) {
648:     PetscDraw draw;
649:     PetscBool isnull;
650:     PetscViewerDrawGetDraw(viewer,0,&draw);
651:     PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
652:   }

654:   {
655:     /* assemble the entire matrix onto first processor. */
656:     Mat          A;
657:     Mat_SeqSBAIJ *Aloc;
658:     Mat_SeqBAIJ  *Bloc;
659:     PetscInt     M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
660:     MatScalar    *a;

662:     /* Should this be the same type as mat? */
663:     MatCreate(PetscObjectComm((PetscObject)mat),&A);
664:     if (!rank) {
665:       MatSetSizes(A,M,N,M,N);
666:     } else {
667:       MatSetSizes(A,0,0,M,N);
668:     }
669:     MatSetType(A,MATMPISBAIJ);
670:     MatMPISBAIJSetPreallocation(A,mat->rmap->bs,0,NULL,0,NULL);
671:     MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
672:     PetscLogObjectParent((PetscObject)mat,(PetscObject)A);

674:     /* copy over the A part */
675:     Aloc = (Mat_SeqSBAIJ*)baij->A->data;
676:     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
677:     PetscMalloc1(bs,&rvals);

679:     for (i=0; i<mbs; i++) {
680:       rvals[0] = bs*(baij->rstartbs + i);
681:       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
682:       for (j=ai[i]; j<ai[i+1]; j++) {
683:         col = (baij->cstartbs+aj[j])*bs;
684:         for (k=0; k<bs; k++) {
685:           MatSetValues_MPISBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
686:           col++;
687:           a += bs;
688:         }
689:       }
690:     }
691:     /* copy over the B part */
692:     Bloc = (Mat_SeqBAIJ*)baij->B->data;
693:     ai   = Bloc->i; aj = Bloc->j; a = Bloc->a;
694:     for (i=0; i<mbs; i++) {

696:       rvals[0] = bs*(baij->rstartbs + i);
697:       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
698:       for (j=ai[i]; j<ai[i+1]; j++) {
699:         col = baij->garray[aj[j]]*bs;
700:         for (k=0; k<bs; k++) {
701:           MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
702:           col++;
703:           a += bs;
704:         }
705:       }
706:     }
707:     PetscFree(rvals);
708:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
709:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
710:     /*
711:        Everyone has to call to draw the matrix since the graphics waits are
712:        synchronized across all processors that share the PetscDraw object
713:     */
714:     PetscViewerGetSingleton(viewer,&sviewer);
715:     if (!rank) {
716:         MatView_SeqSBAIJ_ASCII(((Mat_MPISBAIJ*)(A->data))->A,sviewer);
717:     }
718:     PetscViewerRestoreSingleton(viewer,&sviewer);
719:     MatDestroy(&A);
720:   }
721:   return(0);
722: }

726: PetscErrorCode MatView_MPISBAIJ(Mat mat,PetscViewer viewer)
727: {
729:   PetscBool      iascii,isdraw,issocket,isbinary;

732:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
733:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
734:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
735:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
736:   if (iascii || isdraw || issocket || isbinary) {
737:     MatView_MPISBAIJ_ASCIIorDraworSocket(mat,viewer);
738:   }
739:   return(0);
740: }

744: PetscErrorCode MatDestroy_MPISBAIJ(Mat mat)
745: {
746:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;

750: #if defined(PETSC_USE_LOG)
751:   PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N);
752: #endif
753:   MatStashDestroy_Private(&mat->stash);
754:   MatStashDestroy_Private(&mat->bstash);
755:   MatDestroy(&baij->A);
756:   MatDestroy(&baij->B);
757: #if defined(PETSC_USE_CTABLE)
758:   PetscTableDestroy(&baij->colmap);
759: #else
760:   PetscFree(baij->colmap);
761: #endif
762:   PetscFree(baij->garray);
763:   VecDestroy(&baij->lvec);
764:   VecScatterDestroy(&baij->Mvctx);
765:   VecDestroy(&baij->slvec0);
766:   VecDestroy(&baij->slvec0b);
767:   VecDestroy(&baij->slvec1);
768:   VecDestroy(&baij->slvec1a);
769:   VecDestroy(&baij->slvec1b);
770:   VecScatterDestroy(&baij->sMvctx);
771:   PetscFree2(baij->rowvalues,baij->rowindices);
772:   PetscFree(baij->barray);
773:   PetscFree(baij->hd);
774:   VecDestroy(&baij->diag);
775:   VecDestroy(&baij->bb1);
776:   VecDestroy(&baij->xx1);
777: #if defined(PETSC_USE_REAL_MAT_SINGLE)
778:   PetscFree(baij->setvaluescopy);
779: #endif
780:   PetscFree(baij->in_loc);
781:   PetscFree(baij->v_loc);
782:   PetscFree(baij->rangebs);
783:   PetscFree(mat->data);

785:   PetscObjectChangeTypeName((PetscObject)mat,0);
786:   PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);
787:   PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);
788:   PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C",NULL);
789:   PetscObjectComposeFunction((PetscObject)mat,"MatMPISBAIJSetPreallocation_C",NULL);
790:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpisbaij_mpisbstrm_C",NULL);
791:   return(0);
792: }

796: PetscErrorCode MatMult_MPISBAIJ_Hermitian(Mat A,Vec xx,Vec yy)
797: {
798:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;
800:   PetscInt       nt,mbs=a->mbs,bs=A->rmap->bs;
801:   PetscScalar    *x,*from;

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

807:   /* diagonal part */
808:   (*a->A->ops->mult)(a->A,xx,a->slvec1a);
809:   VecSet(a->slvec1b,0.0);

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

814:   /* copy x into the vec slvec0 */
815:   VecGetArray(a->slvec0,&from);
816:   VecGetArray(xx,&x);

818:   PetscMemcpy(from,x,bs*mbs*sizeof(MatScalar));
819:   VecRestoreArray(a->slvec0,&from);
820:   VecRestoreArray(xx,&x);

822:   VecScatterBegin(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
823:   VecScatterEnd(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
824:   /* supperdiagonal part */
825:   (*a->B->ops->multadd)(a->B,a->slvec1b,a->slvec1a,yy);
826:   return(0);
827: }

831: PetscErrorCode MatMult_MPISBAIJ(Mat A,Vec xx,Vec yy)
832: {
833:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;
835:   PetscInt       nt,mbs=a->mbs,bs=A->rmap->bs;
836:   PetscScalar    *x,*from;

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

842:   /* diagonal part */
843:   (*a->A->ops->mult)(a->A,xx,a->slvec1a);
844:   VecSet(a->slvec1b,0.0);

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

849:   /* copy x into the vec slvec0 */
850:   VecGetArray(a->slvec0,&from);
851:   VecGetArray(xx,&x);

853:   PetscMemcpy(from,x,bs*mbs*sizeof(MatScalar));
854:   VecRestoreArray(a->slvec0,&from);
855:   VecRestoreArray(xx,&x);

857:   VecScatterBegin(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
858:   VecScatterEnd(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
859:   /* supperdiagonal part */
860:   (*a->B->ops->multadd)(a->B,a->slvec1b,a->slvec1a,yy);
861:   return(0);
862: }

866: PetscErrorCode MatMult_MPISBAIJ_2comm(Mat A,Vec xx,Vec yy)
867: {
868:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;
870:   PetscInt       nt;

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

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

879:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
880:   /* do diagonal part */
881:   (*a->A->ops->mult)(a->A,xx,yy);
882:   /* do supperdiagonal part */
883:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
884:   (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
885:   /* do subdiagonal part */
886:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
887:   VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
888:   VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
889:   return(0);
890: }

894: PetscErrorCode MatMultAdd_MPISBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
895: {
896:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;
898:   PetscInt       mbs=a->mbs,bs=A->rmap->bs;
899:   PetscScalar    *x,*from,zero=0.0;

902:   /*
903:   PetscSynchronizedPrintf(PetscObjectComm((PetscObject)A)," MatMultAdd is called ...\n");
904:   PetscSynchronizedFlush(PetscObjectComm((PetscObject)A),PETSC_STDOUT);
905:   */
906:   /* diagonal part */
907:   (*a->A->ops->multadd)(a->A,xx,yy,a->slvec1a);
908:   VecSet(a->slvec1b,zero);

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

913:   /* copy x into the vec slvec0 */
914:   VecGetArray(a->slvec0,&from);
915:   VecGetArray(xx,&x);
916:   PetscMemcpy(from,x,bs*mbs*sizeof(MatScalar));
917:   VecRestoreArray(a->slvec0,&from);

919:   VecScatterBegin(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);
920:   VecRestoreArray(xx,&x);
921:   VecScatterEnd(a->sMvctx,a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD);

923:   /* supperdiagonal part */
924:   (*a->B->ops->multadd)(a->B,a->slvec1b,a->slvec1a,zz);
925:   return(0);
926: }

930: PetscErrorCode MatMultAdd_MPISBAIJ_2comm(Mat A,Vec xx,Vec yy,Vec zz)
931: {
932:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

936:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
937:   /* do diagonal part */
938:   (*a->A->ops->multadd)(a->A,xx,yy,zz);
939:   /* do supperdiagonal part */
940:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
941:   (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);

943:   /* do subdiagonal part */
944:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
945:   VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
946:   VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
947:   return(0);
948: }

950: /*
951:   This only works correctly for square matrices where the subblock A->A is the
952:    diagonal block
953: */
956: PetscErrorCode MatGetDiagonal_MPISBAIJ(Mat A,Vec v)
957: {
958:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

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

969: PetscErrorCode MatScale_MPISBAIJ(Mat A,PetscScalar aa)
970: {
971:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

975:   MatScale(a->A,aa);
976:   MatScale(a->B,aa);
977:   return(0);
978: }

982: PetscErrorCode MatGetRow_MPISBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
983: {
984:   Mat_MPISBAIJ   *mat = (Mat_MPISBAIJ*)matin->data;
985:   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
987:   PetscInt       bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB;
988:   PetscInt       nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend;
989:   PetscInt       *cmap,*idx_p,cstart = mat->rstartbs;

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

995:   if (!mat->rowvalues && (idx || v)) {
996:     /*
997:         allocate enough space to hold information from the longest row.
998:     */
999:     Mat_SeqSBAIJ *Aa = (Mat_SeqSBAIJ*)mat->A->data;
1000:     Mat_SeqBAIJ  *Ba = (Mat_SeqBAIJ*)mat->B->data;
1001:     PetscInt     max = 1,mbs = mat->mbs,tmp;
1002:     for (i=0; i<mbs; i++) {
1003:       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; /* row length */
1004:       if (max < tmp) max = tmp;
1005:     }
1006:     PetscMalloc2(max*bs2,&mat->rowvalues,max*bs2,&mat->rowindices);
1007:   }

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

1012:   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1013:   if (!v)   {pvA = 0; pvB = 0;}
1014:   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1015:   (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1016:   (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1017:   nztot = nzA + nzB;

1019:   cmap = mat->garray;
1020:   if (v  || idx) {
1021:     if (nztot) {
1022:       /* Sort by increasing column numbers, assuming A and B already sorted */
1023:       PetscInt imark = -1;
1024:       if (v) {
1025:         *v = v_p = mat->rowvalues;
1026:         for (i=0; i<nzB; i++) {
1027:           if (cmap[cworkB[i]/bs] < cstart) v_p[i] = vworkB[i];
1028:           else break;
1029:         }
1030:         imark = i;
1031:         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1032:         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1033:       }
1034:       if (idx) {
1035:         *idx = idx_p = mat->rowindices;
1036:         if (imark > -1) {
1037:           for (i=0; i<imark; i++) {
1038:             idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1039:           }
1040:         } else {
1041:           for (i=0; i<nzB; i++) {
1042:             if (cmap[cworkB[i]/bs] < cstart) idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1043:             else break;
1044:           }
1045:           imark = i;
1046:         }
1047:         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart*bs + cworkA[i];
1048:         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1049:       }
1050:     } else {
1051:       if (idx) *idx = 0;
1052:       if (v)   *v   = 0;
1053:     }
1054:   }
1055:   *nz  = nztot;
1056:   (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1057:   (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1058:   return(0);
1059: }

1063: PetscErrorCode MatRestoreRow_MPISBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1064: {
1065:   Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)mat->data;

1068:   if (!baij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
1069:   baij->getrowactive = PETSC_FALSE;
1070:   return(0);
1071: }

1075: PetscErrorCode MatGetRowUpperTriangular_MPISBAIJ(Mat A)
1076: {
1077:   Mat_MPISBAIJ *a  = (Mat_MPISBAIJ*)A->data;
1078:   Mat_SeqSBAIJ *aA = (Mat_SeqSBAIJ*)a->A->data;

1081:   aA->getrow_utriangular = PETSC_TRUE;
1082:   return(0);
1083: }
1086: PetscErrorCode MatRestoreRowUpperTriangular_MPISBAIJ(Mat A)
1087: {
1088:   Mat_MPISBAIJ *a  = (Mat_MPISBAIJ*)A->data;
1089:   Mat_SeqSBAIJ *aA = (Mat_SeqSBAIJ*)a->A->data;

1092:   aA->getrow_utriangular = PETSC_FALSE;
1093:   return(0);
1094: }

1098: PetscErrorCode MatRealPart_MPISBAIJ(Mat A)
1099: {
1100:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1104:   MatRealPart(a->A);
1105:   MatRealPart(a->B);
1106:   return(0);
1107: }

1111: PetscErrorCode MatImaginaryPart_MPISBAIJ(Mat A)
1112: {
1113:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;

1117:   MatImaginaryPart(a->A);
1118:   MatImaginaryPart(a->B);
1119:   return(0);
1120: }

1124: PetscErrorCode MatZeroEntries_MPISBAIJ(Mat A)
1125: {
1126:   Mat_MPISBAIJ   *l = (Mat_MPISBAIJ*)A->data;

1130:   MatZeroEntries(l->A);
1131:   MatZeroEntries(l->B);
1132:   return(0);
1133: }

1137: PetscErrorCode MatGetInfo_MPISBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1138: {
1139:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)matin->data;
1140:   Mat            A  = a->A,B = a->B;
1142:   PetscReal      isend[5],irecv[5];

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

1147:   MatGetInfo(A,MAT_LOCAL,info);

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

1152:   MatGetInfo(B,MAT_LOCAL,info);

1154:   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1155:   isend[3] += info->memory;  isend[4] += info->mallocs;
1156:   if (flag == MAT_LOCAL) {
1157:     info->nz_used      = isend[0];
1158:     info->nz_allocated = isend[1];
1159:     info->nz_unneeded  = isend[2];
1160:     info->memory       = isend[3];
1161:     info->mallocs      = isend[4];
1162:   } else if (flag == MAT_GLOBAL_MAX) {
1163:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));

1165:     info->nz_used      = irecv[0];
1166:     info->nz_allocated = irecv[1];
1167:     info->nz_unneeded  = irecv[2];
1168:     info->memory       = irecv[3];
1169:     info->mallocs      = irecv[4];
1170:   } else if (flag == MAT_GLOBAL_SUM) {
1171:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));

1173:     info->nz_used      = irecv[0];
1174:     info->nz_allocated = irecv[1];
1175:     info->nz_unneeded  = irecv[2];
1176:     info->memory       = irecv[3];
1177:     info->mallocs      = irecv[4];
1178:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown MatInfoType argument %d",(int)flag);
1179:   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
1180:   info->fill_ratio_needed = 0;
1181:   info->factor_mallocs    = 0;
1182:   return(0);
1183: }

1187: PetscErrorCode MatSetOption_MPISBAIJ(Mat A,MatOption op,PetscBool flg)
1188: {
1189:   Mat_MPISBAIJ   *a  = (Mat_MPISBAIJ*)A->data;
1190:   Mat_SeqSBAIJ   *aA = (Mat_SeqSBAIJ*)a->A->data;

1194:   switch (op) {
1195:   case MAT_NEW_NONZERO_LOCATIONS:
1196:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1197:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1198:   case MAT_KEEP_NONZERO_PATTERN:
1199:   case MAT_NEW_NONZERO_LOCATION_ERR:
1200:     MatSetOption(a->A,op,flg);
1201:     MatSetOption(a->B,op,flg);
1202:     break;
1203:   case MAT_ROW_ORIENTED:
1204:     a->roworiented = flg;

1206:     MatSetOption(a->A,op,flg);
1207:     MatSetOption(a->B,op,flg);
1208:     break;
1209:   case MAT_NEW_DIAGONALS:
1210:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1211:     break;
1212:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1213:     a->donotstash = flg;
1214:     break;
1215:   case MAT_USE_HASH_TABLE:
1216:     a->ht_flag = flg;
1217:     break;
1218:   case MAT_HERMITIAN:
1219:     if (!A->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatAssemblyEnd() first");
1220:     MatSetOption(a->A,op,flg);

1222:     A->ops->mult = MatMult_MPISBAIJ_Hermitian;
1223:     break;
1224:   case MAT_SPD:
1225:     A->spd_set = PETSC_TRUE;
1226:     A->spd     = flg;
1227:     if (flg) {
1228:       A->symmetric                  = PETSC_TRUE;
1229:       A->structurally_symmetric     = PETSC_TRUE;
1230:       A->symmetric_set              = PETSC_TRUE;
1231:       A->structurally_symmetric_set = PETSC_TRUE;
1232:     }
1233:     break;
1234:   case MAT_SYMMETRIC:
1235:     MatSetOption(a->A,op,flg);
1236:     break;
1237:   case MAT_STRUCTURALLY_SYMMETRIC:
1238:     MatSetOption(a->A,op,flg);
1239:     break;
1240:   case MAT_SYMMETRY_ETERNAL:
1241:     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix must be symmetric");
1242:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1243:     break;
1244:   case MAT_IGNORE_LOWER_TRIANGULAR:
1245:     aA->ignore_ltriangular = flg;
1246:     break;
1247:   case MAT_ERROR_LOWER_TRIANGULAR:
1248:     aA->ignore_ltriangular = flg;
1249:     break;
1250:   case MAT_GETROW_UPPERTRIANGULAR:
1251:     aA->getrow_utriangular = flg;
1252:     break;
1253:   default:
1254:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1255:   }
1256:   return(0);
1257: }

1261: PetscErrorCode MatTranspose_MPISBAIJ(Mat A,MatReuse reuse,Mat *B)
1262: {

1266:   if (MAT_INITIAL_MATRIX || *B != A) {
1267:     MatDuplicate(A,MAT_COPY_VALUES,B);
1268:   }
1269:   return(0);
1270: }

1274: PetscErrorCode MatDiagonalScale_MPISBAIJ(Mat mat,Vec ll,Vec rr)
1275: {
1276:   Mat_MPISBAIJ   *baij = (Mat_MPISBAIJ*)mat->data;
1277:   Mat            a     = baij->A, b=baij->B;
1279:   PetscInt       nv,m,n;
1280:   PetscBool      flg;

1283:   if (ll != rr) {
1284:     VecEqual(ll,rr,&flg);
1285:     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"For symmetric format, left and right scaling vectors must be same\n");
1286:   }
1287:   if (!ll) return(0);

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

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

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

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

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

1303:   /* right diagonalscale the off-diagonal part */
1304:   VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);
1305:   (*b->ops->diagonalscale)(b,NULL,baij->lvec);
1306:   return(0);
1307: }

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

1317:   MatSetUnfactored(a->A);
1318:   return(0);
1319: }

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

1325: PetscErrorCode MatEqual_MPISBAIJ(Mat A,Mat B,PetscBool  *flag)
1326: {
1327:   Mat_MPISBAIJ   *matB = (Mat_MPISBAIJ*)B->data,*matA = (Mat_MPISBAIJ*)A->data;
1328:   Mat            a,b,c,d;
1329:   PetscBool      flg;

1333:   a = matA->A; b = matA->B;
1334:   c = matB->A; d = matB->B;

1336:   MatEqual(a,c,&flg);
1337:   if (flg) {
1338:     MatEqual(b,d,&flg);
1339:   }
1340:   MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
1341:   return(0);
1342: }

1346: PetscErrorCode MatCopy_MPISBAIJ(Mat A,Mat B,MatStructure str)
1347: {
1349:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;
1350:   Mat_MPISBAIJ   *b = (Mat_MPISBAIJ*)B->data;

1353:   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1354:   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1355:     MatGetRowUpperTriangular(A);
1356:     MatCopy_Basic(A,B,str);
1357:     MatRestoreRowUpperTriangular(A);
1358:   } else {
1359:     MatCopy(a->A,b->A,str);
1360:     MatCopy(a->B,b->B,str);
1361:   }
1362:   return(0);
1363: }

1367: PetscErrorCode MatSetUp_MPISBAIJ(Mat A)
1368: {

1372:   MatMPISBAIJSetPreallocation(A,A->rmap->bs,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
1373:   return(0);
1374: }

1378: PetscErrorCode MatAXPY_MPISBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
1379: {
1381:   Mat_MPISBAIJ   *xx=(Mat_MPISBAIJ*)X->data,*yy=(Mat_MPISBAIJ*)Y->data;
1382:   PetscBLASInt   bnz,one=1;
1383:   Mat_SeqSBAIJ   *xa,*ya;
1384:   Mat_SeqBAIJ    *xb,*yb;

1387:   if (str == SAME_NONZERO_PATTERN) {
1388:     PetscScalar alpha = a;
1389:     xa   = (Mat_SeqSBAIJ*)xx->A->data;
1390:     ya   = (Mat_SeqSBAIJ*)yy->A->data;
1391:     PetscBLASIntCast(xa->nz,&bnz);
1392:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,xa->a,&one,ya->a,&one));
1393:     xb   = (Mat_SeqBAIJ*)xx->B->data;
1394:     yb   = (Mat_SeqBAIJ*)yy->B->data;
1395:     PetscBLASIntCast(xb->nz,&bnz);
1396:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,xb->a,&one,yb->a,&one));
1397:     PetscObjectStateIncrease((PetscObject)Y);
1398:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1399:     MatSetOption(X,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE);
1400:     MatAXPY_Basic(Y,a,X,str);
1401:     MatSetOption(X,MAT_GETROW_UPPERTRIANGULAR,PETSC_FALSE);
1402:   } else {
1403:     Mat      B;
1404:     PetscInt *nnz_d,*nnz_o,bs=Y->rmap->bs;
1405:     if (bs != X->rmap->bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrices must have same block size");
1406:     MatGetRowUpperTriangular(X);
1407:     MatGetRowUpperTriangular(Y);
1408:     PetscMalloc1(yy->A->rmap->N,&nnz_d);
1409:     PetscMalloc1(yy->B->rmap->N,&nnz_o);
1410:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
1411:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
1412:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
1413:     MatSetBlockSizesFromMats(B,Y,Y);
1414:     MatSetType(B,MATMPISBAIJ);
1415:     MatAXPYGetPreallocation_SeqSBAIJ(yy->A,xx->A,nnz_d);
1416:     MatAXPYGetPreallocation_MPIBAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);
1417:     MatMPISBAIJSetPreallocation(B,bs,0,nnz_d,0,nnz_o);
1418:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
1419:     MatHeaderReplace(Y,B);
1420:     PetscFree(nnz_d);
1421:     PetscFree(nnz_o);
1422:     MatRestoreRowUpperTriangular(X);
1423:     MatRestoreRowUpperTriangular(Y);
1424:   }
1425:   return(0);
1426: }

1430: PetscErrorCode MatGetSubMatrices_MPISBAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1431: {
1433:   PetscInt       i;
1434:   PetscBool      flg;

1437:   MatGetSubMatrices_MPIBAIJ(A,n,irow,icol,scall,B);
1438:   for (i=0; i<n; i++) {
1439:     ISEqual(irow[i],icol[i],&flg);
1440:     if (!flg) { /* *B[i] is non-symmetric, set flag */
1441:       MatSetOption(*B[i],MAT_SYMMETRIC,PETSC_FALSE);
1442:     }
1443:   }
1444:   return(0);
1445: }

1447: /* -------------------------------------------------------------------*/
1448: static struct _MatOps MatOps_Values = {MatSetValues_MPISBAIJ,
1449:                                        MatGetRow_MPISBAIJ,
1450:                                        MatRestoreRow_MPISBAIJ,
1451:                                        MatMult_MPISBAIJ,
1452:                                /*  4*/ MatMultAdd_MPISBAIJ,
1453:                                        MatMult_MPISBAIJ,       /* transpose versions are same as non-transpose */
1454:                                        MatMultAdd_MPISBAIJ,
1455:                                        0,
1456:                                        0,
1457:                                        0,
1458:                                /* 10*/ 0,
1459:                                        0,
1460:                                        0,
1461:                                        MatSOR_MPISBAIJ,
1462:                                        MatTranspose_MPISBAIJ,
1463:                                /* 15*/ MatGetInfo_MPISBAIJ,
1464:                                        MatEqual_MPISBAIJ,
1465:                                        MatGetDiagonal_MPISBAIJ,
1466:                                        MatDiagonalScale_MPISBAIJ,
1467:                                        MatNorm_MPISBAIJ,
1468:                                /* 20*/ MatAssemblyBegin_MPISBAIJ,
1469:                                        MatAssemblyEnd_MPISBAIJ,
1470:                                        MatSetOption_MPISBAIJ,
1471:                                        MatZeroEntries_MPISBAIJ,
1472:                                /* 24*/ 0,
1473:                                        0,
1474:                                        0,
1475:                                        0,
1476:                                        0,
1477:                                /* 29*/ MatSetUp_MPISBAIJ,
1478:                                        0,
1479:                                        0,
1480:                                        0,
1481:                                        0,
1482:                                /* 34*/ MatDuplicate_MPISBAIJ,
1483:                                        0,
1484:                                        0,
1485:                                        0,
1486:                                        0,
1487:                                /* 39*/ MatAXPY_MPISBAIJ,
1488:                                        MatGetSubMatrices_MPISBAIJ,
1489:                                        MatIncreaseOverlap_MPISBAIJ,
1490:                                        MatGetValues_MPISBAIJ,
1491:                                        MatCopy_MPISBAIJ,
1492:                                /* 44*/ 0,
1493:                                        MatScale_MPISBAIJ,
1494:                                        0,
1495:                                        0,
1496:                                        0,
1497:                                /* 49*/ 0,
1498:                                        0,
1499:                                        0,
1500:                                        0,
1501:                                        0,
1502:                                /* 54*/ 0,
1503:                                        0,
1504:                                        MatSetUnfactored_MPISBAIJ,
1505:                                        0,
1506:                                        MatSetValuesBlocked_MPISBAIJ,
1507:                                /* 59*/ 0,
1508:                                        0,
1509:                                        0,
1510:                                        0,
1511:                                        0,
1512:                                /* 64*/ 0,
1513:                                        0,
1514:                                        0,
1515:                                        0,
1516:                                        0,
1517:                                /* 69*/ MatGetRowMaxAbs_MPISBAIJ,
1518:                                        0,
1519:                                        0,
1520:                                        0,
1521:                                        0,
1522:                                /* 74*/ 0,
1523:                                        0,
1524:                                        0,
1525:                                        0,
1526:                                        0,
1527:                                /* 79*/ 0,
1528:                                        0,
1529:                                        0,
1530:                                        0,
1531:                                        MatLoad_MPISBAIJ,
1532:                                /* 84*/ 0,
1533:                                        0,
1534:                                        0,
1535:                                        0,
1536:                                        0,
1537:                                /* 89*/ 0,
1538:                                        0,
1539:                                        0,
1540:                                        0,
1541:                                        0,
1542:                                /* 94*/ 0,
1543:                                        0,
1544:                                        0,
1545:                                        0,
1546:                                        0,
1547:                                /* 99*/ 0,
1548:                                        0,
1549:                                        0,
1550:                                        0,
1551:                                        0,
1552:                                /*104*/ 0,
1553:                                        MatRealPart_MPISBAIJ,
1554:                                        MatImaginaryPart_MPISBAIJ,
1555:                                        MatGetRowUpperTriangular_MPISBAIJ,
1556:                                        MatRestoreRowUpperTriangular_MPISBAIJ,
1557:                                /*109*/ 0,
1558:                                        0,
1559:                                        0,
1560:                                        0,
1561:                                        0,
1562:                                /*114*/ 0,
1563:                                        0,
1564:                                        0,
1565:                                        0,
1566:                                        0,
1567:                                /*119*/ 0,
1568:                                        0,
1569:                                        0,
1570:                                        0,
1571:                                        0,
1572:                                /*124*/ 0,
1573:                                        0,
1574:                                        0,
1575:                                        0,
1576:                                        0,
1577:                                /*129*/ 0,
1578:                                        0,
1579:                                        0,
1580:                                        0,
1581:                                        0,
1582:                                /*134*/ 0,
1583:                                        0,
1584:                                        0,
1585:                                        0,
1586:                                        0,
1587:                                /*139*/ 0,
1588:                                        0,
1589:                                        0,
1590:                                        0,
1591:                                        0,
1592:                                 /*144*/MatCreateMPIMatConcatenateSeqMat_MPISBAIJ
1593: };

1597: PetscErrorCode  MatGetDiagonalBlock_MPISBAIJ(Mat A,Mat *a)
1598: {
1600:   *a = ((Mat_MPISBAIJ*)A->data)->A;
1601:   return(0);
1602: }

1606: PetscErrorCode  MatMPISBAIJSetPreallocation_MPISBAIJ(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt *d_nnz,PetscInt o_nz,const PetscInt *o_nnz)
1607: {
1608:   Mat_MPISBAIJ   *b;
1610:   PetscInt       i,mbs,Mbs;

1613:   MatSetBlockSize(B,PetscAbs(bs));
1614:   PetscLayoutSetUp(B->rmap);
1615:   PetscLayoutSetUp(B->cmap);
1616:   PetscLayoutGetBlockSize(B->rmap,&bs);

1618:   b   = (Mat_MPISBAIJ*)B->data;
1619:   mbs = B->rmap->n/bs;
1620:   Mbs = B->rmap->N/bs;
1621:   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);

1623:   B->rmap->bs = bs;
1624:   b->bs2      = bs*bs;
1625:   b->mbs      = mbs;
1626:   b->Mbs      = Mbs;
1627:   b->nbs      = B->cmap->n/bs;
1628:   b->Nbs      = B->cmap->N/bs;

1630:   for (i=0; i<=b->size; i++) {
1631:     b->rangebs[i] = B->rmap->range[i]/bs;
1632:   }
1633:   b->rstartbs = B->rmap->rstart/bs;
1634:   b->rendbs   = B->rmap->rend/bs;

1636:   b->cstartbs = B->cmap->rstart/bs;
1637:   b->cendbs   = B->cmap->rend/bs;

1639:   if (!B->preallocated) {
1640:     MatCreate(PETSC_COMM_SELF,&b->A);
1641:     MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
1642:     MatSetType(b->A,MATSEQSBAIJ);
1643:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
1644:     MatCreate(PETSC_COMM_SELF,&b->B);
1645:     MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);
1646:     MatSetType(b->B,MATSEQBAIJ);
1647:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);
1648:     MatStashCreate_Private(PetscObjectComm((PetscObject)B),bs,&B->bstash);
1649:   }

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

1654:   B->preallocated = PETSC_TRUE;
1655:   return(0);
1656: }

1660: PetscErrorCode MatMPISBAIJSetPreallocationCSR_MPISBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
1661: {
1662:   PetscInt       m,rstart,cstart,cend;
1663:   PetscInt       i,j,d,nz,nz_max=0,*d_nnz=0,*o_nnz=0;
1664:   const PetscInt *JJ    =0;
1665:   PetscScalar    *values=0;

1669:   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
1670:   PetscLayoutSetBlockSize(B->rmap,bs);
1671:   PetscLayoutSetBlockSize(B->cmap,bs);
1672:   PetscLayoutSetUp(B->rmap);
1673:   PetscLayoutSetUp(B->cmap);
1674:   PetscLayoutGetBlockSize(B->rmap,&bs);
1675:   m      = B->rmap->n/bs;
1676:   rstart = B->rmap->rstart/bs;
1677:   cstart = B->cmap->rstart/bs;
1678:   cend   = B->cmap->rend/bs;

1680:   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
1681:   PetscMalloc2(m,&d_nnz,m,&o_nnz);
1682:   for (i=0; i<m; i++) {
1683:     nz = ii[i+1] - ii[i];
1684:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz);
1685:     nz_max = PetscMax(nz_max,nz);
1686:     JJ     = jj + ii[i];
1687:     for (j=0; j<nz; j++) {
1688:       if (*JJ >= cstart) break;
1689:       JJ++;
1690:     }
1691:     d = 0;
1692:     for (; j<nz; j++) {
1693:       if (*JJ++ >= cend) break;
1694:       d++;
1695:     }
1696:     d_nnz[i] = d;
1697:     o_nnz[i] = nz - d;
1698:   }
1699:   MatMPISBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);
1700:   PetscFree2(d_nnz,o_nnz);

1702:   values = (PetscScalar*)V;
1703:   if (!values) {
1704:     PetscMalloc1(bs*bs*nz_max,&values);
1705:     PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));
1706:   }
1707:   for (i=0; i<m; i++) {
1708:     PetscInt          row    = i + rstart;
1709:     PetscInt          ncols  = ii[i+1] - ii[i];
1710:     const PetscInt    *icols = jj + ii[i];
1711:     const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
1712:     MatSetValuesBlocked_MPISBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);
1713:   }

1715:   if (!V) { PetscFree(values); }
1716:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
1717:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
1718:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
1719:   return(0);
1720: }

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

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

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

1733:   Level: beginner

1735: .seealso: MatCreateMPISBAIJ
1736: M*/

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

1742: PETSC_EXTERN PetscErrorCode MatCreate_MPISBAIJ(Mat B)
1743: {
1744:   Mat_MPISBAIJ   *b;
1746:   PetscBool      flg = PETSC_FALSE;

1749:   PetscNewLog(B,&b);
1750:   B->data = (void*)b;
1751:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));

1753:   B->ops->destroy = MatDestroy_MPISBAIJ;
1754:   B->ops->view    = MatView_MPISBAIJ;
1755:   B->assembled    = PETSC_FALSE;
1756:   B->insertmode   = NOT_SET_VALUES;

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

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

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

1767:   b->donotstash  = PETSC_FALSE;
1768:   b->colmap      = NULL;
1769:   b->garray      = NULL;
1770:   b->roworiented = PETSC_TRUE;

1772:   /* stuff used in block assembly */
1773:   b->barray = 0;

1775:   /* stuff used for matrix vector multiply */
1776:   b->lvec    = 0;
1777:   b->Mvctx   = 0;
1778:   b->slvec0  = 0;
1779:   b->slvec0b = 0;
1780:   b->slvec1  = 0;
1781:   b->slvec1a = 0;
1782:   b->slvec1b = 0;
1783:   b->sMvctx  = 0;

1785:   /* stuff for MatGetRow() */
1786:   b->rowindices   = 0;
1787:   b->rowvalues    = 0;
1788:   b->getrowactive = PETSC_FALSE;

1790:   /* hash table stuff */
1791:   b->ht           = 0;
1792:   b->hd           = 0;
1793:   b->ht_size      = 0;
1794:   b->ht_flag      = PETSC_FALSE;
1795:   b->ht_fact      = 0;
1796:   b->ht_total_ct  = 0;
1797:   b->ht_insert_ct = 0;

1799:   /* stuff for MatGetSubMatrices_MPIBAIJ_local() */
1800:   b->ijonly = PETSC_FALSE;

1802:   b->in_loc = 0;
1803:   b->v_loc  = 0;
1804:   b->n_loc  = 0;

1806:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPISBAIJ);
1807:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPISBAIJ);
1808:   PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",MatGetDiagonalBlock_MPISBAIJ);
1809:   PetscObjectComposeFunction((PetscObject)B,"MatMPISBAIJSetPreallocation_C",MatMPISBAIJSetPreallocation_MPISBAIJ);
1810:   PetscObjectComposeFunction((PetscObject)B,"MatMPISBAIJSetPreallocationCSR_C",MatMPISBAIJSetPreallocationCSR_MPISBAIJ);
1811:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpisbaij_mpisbstrm_C",MatConvert_MPISBAIJ_MPISBSTRM);

1813:   B->symmetric                  = PETSC_TRUE;
1814:   B->structurally_symmetric     = PETSC_TRUE;
1815:   B->symmetric_set              = PETSC_TRUE;
1816:   B->structurally_symmetric_set = PETSC_TRUE;

1818:   PetscObjectChangeTypeName((PetscObject)B,MATMPISBAIJ);
1819:   PetscOptionsBegin(PetscObjectComm((PetscObject)B),NULL,"Options for loading MPISBAIJ matrix 1","Mat");
1820:   PetscOptionsBool("-mat_use_hash_table","Use hash table to save memory in constructing matrix","MatSetOption",flg,&flg,NULL);
1821:   if (flg) {
1822:     PetscReal fact = 1.39;
1823:     MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);
1824:     PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,NULL);
1825:     if (fact <= 1.0) fact = 1.39;
1826:     MatMPIBAIJSetHashTableFactor(B,fact);
1827:     PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);
1828:   }
1829:   PetscOptionsEnd();
1830:   return(0);
1831: }

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

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

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

1842:   Level: beginner

1844: .seealso: MatCreateMPISBAIJ,MATSEQSBAIJ,MATMPISBAIJ
1845: M*/

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

1855:    Collective on Mat

1857:    Input Parameters:
1858: +  B - the matrix
1859: .  bs   - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
1860:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
1861: .  d_nz  - number of block nonzeros per block row in diagonal portion of local
1862:            submatrix  (same for all local rows)
1863: .  d_nnz - array containing the number of block nonzeros in the various block rows
1864:            in the upper triangular and diagonal part of the in diagonal portion of the local
1865:            (possibly different for each block row) or NULL.  If you plan to factor the matrix you must leave room
1866:            for the diagonal entry and set a value even if it is zero.
1867: .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
1868:            submatrix (same for all local rows).
1869: -  o_nnz - array containing the number of nonzeros in the various block rows of the
1870:            off-diagonal portion of the local submatrix that is right of the diagonal
1871:            (possibly different for each block row) or NULL.


1874:    Options Database Keys:
1875: .   -mat_no_unroll - uses code that does not unroll the loops in the
1876:                      block calculations (much slower)
1877: .   -mat_block_size - size of the blocks to use

1879:    Notes:

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

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

1886:    Storage Information:
1887:    For a square global matrix we define each processor's diagonal portion
1888:    to be its local rows and the corresponding columns (a square submatrix);
1889:    each processor's off-diagonal portion encompasses the remainder of the
1890:    local matrix (a rectangular submatrix).

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

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

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

1906: .vb
1907:            0 1 2 3 4 5 6 7 8 9 10 11
1908:           --------------------------
1909:    row 3  |. . . d d d o o o o  o  o
1910:    row 4  |. . . d d d o o o o  o  o
1911:    row 5  |. . . d d d o o o o  o  o
1912:           --------------------------
1913: .ve

1915:    Thus, any entries in the d locations are stored in the d (diagonal)
1916:    submatrix, and any entries in the o locations are stored in the
1917:    o (off-diagonal) submatrix.  Note that the d matrix is stored in
1918:    MatSeqSBAIJ format and the o submatrix in MATSEQBAIJ format.

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

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

1929:    Level: intermediate

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

1933: .seealso: MatCreate(), MatCreateSeqSBAIJ(), MatSetValues(), MatCreateBAIJ(), PetscSplitOwnership()
1934: @*/
1935: PetscErrorCode  MatMPISBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
1936: {

1943:   PetscTryMethod(B,"MatMPISBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,bs,d_nz,d_nnz,o_nz,o_nnz));
1944:   return(0);
1945: }

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

1956:    Collective on MPI_Comm

1958:    Input Parameters:
1959: +  comm - MPI communicator
1960: .  bs   - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
1961:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
1962: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
1963:            This value should be the same as the local size used in creating the
1964:            y vector for the matrix-vector product y = Ax.
1965: .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
1966:            This value should be the same as the local size used in creating the
1967:            x vector for the matrix-vector product y = Ax.
1968: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
1969: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
1970: .  d_nz  - number of block nonzeros per block row in diagonal portion of local
1971:            submatrix  (same for all local rows)
1972: .  d_nnz - array containing the number of block nonzeros in the various block rows
1973:            in the upper triangular portion of the in diagonal portion of the local
1974:            (possibly different for each block block row) or NULL.
1975:            If you plan to factor the matrix you must leave room for the diagonal entry and
1976:            set its value even if it is zero.
1977: .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
1978:            submatrix (same for all local rows).
1979: -  o_nnz - array containing the number of nonzeros in the various block rows of the
1980:            off-diagonal portion of the local submatrix (possibly different for
1981:            each block row) or NULL.

1983:    Output Parameter:
1984: .  A - the matrix

1986:    Options Database Keys:
1987: .   -mat_no_unroll - uses code that does not unroll the loops in the
1988:                      block calculations (much slower)
1989: .   -mat_block_size - size of the blocks to use
1990: .   -mat_mpi - use the parallel matrix data structures even on one processor
1991:                (defaults to using SeqBAIJ format on one processor)

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

1997:    Notes:
1998:    The number of rows and columns must be divisible by blocksize.
1999:    This matrix type does not support complex Hermitian operation.

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

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

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

2009:    Storage Information:
2010:    For a square global matrix we define each processor's diagonal portion
2011:    to be its local rows and the corresponding columns (a square submatrix);
2012:    each processor's off-diagonal portion encompasses the remainder of the
2013:    local matrix (a rectangular submatrix).

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

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

2024: .vb
2025:            0 1 2 3 4 5 6 7 8 9 10 11
2026:           --------------------------
2027:    row 3  |. . . d d d o o o o  o  o
2028:    row 4  |. . . d d d o o o o  o  o
2029:    row 5  |. . . d d d o o o o  o  o
2030:           --------------------------
2031: .ve

2033:    Thus, any entries in the d locations are stored in the d (diagonal)
2034:    submatrix, and any entries in the o locations are stored in the
2035:    o (off-diagonal) submatrix.  Note that the d matrix is stored in
2036:    MatSeqSBAIJ format and the o submatrix in MATSEQBAIJ format.

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

2046:    Level: intermediate

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

2050: .seealso: MatCreate(), MatCreateSeqSBAIJ(), MatSetValues(), MatCreateBAIJ()
2051: @*/

2053: 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)
2054: {
2056:   PetscMPIInt    size;

2059:   MatCreate(comm,A);
2060:   MatSetSizes(*A,m,n,M,N);
2061:   MPI_Comm_size(comm,&size);
2062:   if (size > 1) {
2063:     MatSetType(*A,MATMPISBAIJ);
2064:     MatMPISBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);
2065:   } else {
2066:     MatSetType(*A,MATSEQSBAIJ);
2067:     MatSeqSBAIJSetPreallocation(*A,bs,d_nz,d_nnz);
2068:   }
2069:   return(0);
2070: }


2075: static PetscErrorCode MatDuplicate_MPISBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
2076: {
2077:   Mat            mat;
2078:   Mat_MPISBAIJ   *a,*oldmat = (Mat_MPISBAIJ*)matin->data;
2080:   PetscInt       len=0,nt,bs=matin->rmap->bs,mbs=oldmat->mbs;
2081:   PetscScalar    *array;

2084:   *newmat = 0;

2086:   MatCreate(PetscObjectComm((PetscObject)matin),&mat);
2087:   MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);
2088:   MatSetType(mat,((PetscObject)matin)->type_name);
2089:   PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));
2090:   PetscLayoutReference(matin->rmap,&mat->rmap);
2091:   PetscLayoutReference(matin->cmap,&mat->cmap);

2093:   mat->factortype   = matin->factortype;
2094:   mat->preallocated = PETSC_TRUE;
2095:   mat->assembled    = PETSC_TRUE;
2096:   mat->insertmode   = NOT_SET_VALUES;

2098:   a      = (Mat_MPISBAIJ*)mat->data;
2099:   a->bs2 = oldmat->bs2;
2100:   a->mbs = oldmat->mbs;
2101:   a->nbs = oldmat->nbs;
2102:   a->Mbs = oldmat->Mbs;
2103:   a->Nbs = oldmat->Nbs;


2106:   a->size         = oldmat->size;
2107:   a->rank         = oldmat->rank;
2108:   a->donotstash   = oldmat->donotstash;
2109:   a->roworiented  = oldmat->roworiented;
2110:   a->rowindices   = 0;
2111:   a->rowvalues    = 0;
2112:   a->getrowactive = PETSC_FALSE;
2113:   a->barray       = 0;
2114:   a->rstartbs     = oldmat->rstartbs;
2115:   a->rendbs       = oldmat->rendbs;
2116:   a->cstartbs     = oldmat->cstartbs;
2117:   a->cendbs       = oldmat->cendbs;

2119:   /* hash table stuff */
2120:   a->ht           = 0;
2121:   a->hd           = 0;
2122:   a->ht_size      = 0;
2123:   a->ht_flag      = oldmat->ht_flag;
2124:   a->ht_fact      = oldmat->ht_fact;
2125:   a->ht_total_ct  = 0;
2126:   a->ht_insert_ct = 0;

2128:   PetscMemcpy(a->rangebs,oldmat->rangebs,(a->size+2)*sizeof(PetscInt));
2129:   if (oldmat->colmap) {
2130: #if defined(PETSC_USE_CTABLE)
2131:     PetscTableCreateCopy(oldmat->colmap,&a->colmap);
2132: #else
2133:     PetscMalloc1(a->Nbs,&a->colmap);
2134:     PetscLogObjectMemory((PetscObject)mat,(a->Nbs)*sizeof(PetscInt));
2135:     PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(PetscInt));
2136: #endif
2137:   } else a->colmap = 0;

2139:   if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
2140:     PetscMalloc1(len,&a->garray);
2141:     PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));
2142:     PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));
2143:   } else a->garray = 0;

2145:   MatStashCreate_Private(PetscObjectComm((PetscObject)matin),matin->rmap->bs,&mat->bstash);
2146:   VecDuplicate(oldmat->lvec,&a->lvec);
2147:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);
2148:   VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
2149:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);

2151:    VecDuplicate(oldmat->slvec0,&a->slvec0);
2152:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec0);
2153:    VecDuplicate(oldmat->slvec1,&a->slvec1);
2154:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec1);

2156:   VecGetLocalSize(a->slvec1,&nt);
2157:   VecGetArray(a->slvec1,&array);
2158:   VecCreateSeqWithArray(PETSC_COMM_SELF,1,bs*mbs,array,&a->slvec1a);
2159:   VecCreateSeqWithArray(PETSC_COMM_SELF,1,nt-bs*mbs,array+bs*mbs,&a->slvec1b);
2160:   VecRestoreArray(a->slvec1,&array);
2161:   VecGetArray(a->slvec0,&array);
2162:   VecCreateSeqWithArray(PETSC_COMM_SELF,1,nt-bs*mbs,array+bs*mbs,&a->slvec0b);
2163:   VecRestoreArray(a->slvec0,&array);
2164:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec0);
2165:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec1);
2166:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec0b);
2167:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec1a);
2168:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->slvec1b);

2170:   /*  VecScatterCopy(oldmat->sMvctx,&a->sMvctx); - not written yet, replaced by the lazy trick: */
2171:   PetscObjectReference((PetscObject)oldmat->sMvctx);
2172:   a->sMvctx = oldmat->sMvctx;
2173:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->sMvctx);

2175:    MatDuplicate(oldmat->A,cpvalues,&a->A);
2176:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
2177:    MatDuplicate(oldmat->B,cpvalues,&a->B);
2178:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);
2179:   PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);
2180:   *newmat = mat;
2181:   return(0);
2182: }

2186: PetscErrorCode MatLoad_MPISBAIJ(Mat newmat,PetscViewer viewer)
2187: {
2189:   PetscInt       i,nz,j,rstart,rend;
2190:   PetscScalar    *vals,*buf;
2191:   MPI_Comm       comm;
2192:   MPI_Status     status;
2193:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag,*sndcounts = 0,*browners,maxnz,*rowners,mmbs;
2194:   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols,*locrowlens;
2195:   PetscInt       *procsnz = 0,jj,*mycols,*ibuf;
2196:   PetscInt       bs = newmat->rmap->bs,Mbs,mbs,extra_rows;
2197:   PetscInt       *dlens,*odlens,*mask,*masked1,*masked2,rowcount,odcount;
2198:   PetscInt       dcount,kmax,k,nzcount,tmp,sizesset=1,grows,gcols;
2199:   int            fd;

2202:   PetscObjectGetComm((PetscObject)viewer,&comm);
2203:   PetscOptionsBegin(comm,NULL,"Options for loading MPISBAIJ matrix 2","Mat");
2204:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
2205:   PetscOptionsEnd();
2206:   if (bs < 0) bs = 1;

2208:   MPI_Comm_size(comm,&size);
2209:   MPI_Comm_rank(comm,&rank);
2210:   if (!rank) {
2211:     PetscViewerBinaryGetDescriptor(viewer,&fd);
2212:     PetscBinaryRead(fd,(char*)header,4,PETSC_INT);
2213:     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
2214:     if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as MPISBAIJ");
2215:   }

2217:   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) sizesset = 0;

2219:   MPI_Bcast(header+1,3,MPIU_INT,0,comm);
2220:   M    = header[1];
2221:   N    = header[2];

2223:   /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */
2224:   if (sizesset && newmat->rmap->N < 0) newmat->rmap->N = M;
2225:   if (sizesset && newmat->cmap->N < 0) newmat->cmap->N = N;

2227:   /* If global sizes are set, check if they are consistent with that given in the file */
2228:   if (sizesset) {
2229:     MatGetSize(newmat,&grows,&gcols);
2230:   }
2231:   if (sizesset && newmat->rmap->N != grows) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows:Matrix in file has (%d) and input matrix has (%d)",M,grows);
2232:   if (sizesset && newmat->cmap->N != gcols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of cols:Matrix in file has (%d) and input matrix has (%d)",N,gcols);

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

2236:   /*
2237:      This code adds extra rows to make sure the number of rows is
2238:      divisible by the blocksize
2239:   */
2240:   Mbs        = M/bs;
2241:   extra_rows = bs - M + bs*(Mbs);
2242:   if (extra_rows == bs) extra_rows = 0;
2243:   else                  Mbs++;
2244:   if (extra_rows &&!rank) {
2245:     PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");
2246:   }

2248:   /* determine ownership of all rows */
2249:   if (newmat->rmap->n < 0) { /* PETSC_DECIDE */
2250:     mbs = Mbs/size + ((Mbs % size) > rank);
2251:     m   = mbs*bs;
2252:   } else { /* User Set */
2253:     m   = newmat->rmap->n;
2254:     mbs = m/bs;
2255:   }
2256:   PetscMalloc2(size+1,&rowners,size+1,&browners);
2257:   PetscMPIIntCast(mbs,&mmbs);
2258:   MPI_Allgather(&mmbs,1,MPI_INT,rowners+1,1,MPI_INT,comm);
2259:   rowners[0] = 0;
2260:   for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
2261:   for (i=0; i<=size; i++) browners[i] = rowners[i]*bs;
2262:   rstart = rowners[rank];
2263:   rend   = rowners[rank+1];

2265:   /* distribute row lengths to all processors */
2266:   PetscMalloc1((rend-rstart)*bs,&locrowlens);
2267:   if (!rank) {
2268:     PetscMalloc1(M+extra_rows,&rowlengths);
2269:     PetscBinaryRead(fd,rowlengths,M,PETSC_INT);
2270:     for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
2271:     PetscMalloc1(size,&sndcounts);
2272:     for (i=0; i<size; i++) sndcounts[i] = browners[i+1] - browners[i];
2273:     MPI_Scatterv(rowlengths,sndcounts,browners,MPIU_INT,locrowlens,(rend-rstart)*bs,MPIU_INT,0,comm);
2274:     PetscFree(sndcounts);
2275:   } else {
2276:     MPI_Scatterv(0,0,0,MPIU_INT,locrowlens,(rend-rstart)*bs,MPIU_INT,0,comm);
2277:   }

2279:   if (!rank) {   /* procs[0] */
2280:     /* calculate the number of nonzeros on each processor */
2281:     PetscMalloc1(size,&procsnz);
2282:     PetscMemzero(procsnz,size*sizeof(PetscInt));
2283:     for (i=0; i<size; i++) {
2284:       for (j=rowners[i]*bs; j< rowners[i+1]*bs; j++) {
2285:         procsnz[i] += rowlengths[j];
2286:       }
2287:     }
2288:     PetscFree(rowlengths);

2290:     /* determine max buffer needed and allocate it */
2291:     maxnz = 0;
2292:     for (i=0; i<size; i++) {
2293:       maxnz = PetscMax(maxnz,procsnz[i]);
2294:     }
2295:     PetscMalloc1(maxnz,&cols);

2297:     /* read in my part of the matrix column indices  */
2298:     nz     = procsnz[0];
2299:     PetscMalloc1(nz,&ibuf);
2300:     mycols = ibuf;
2301:     if (size == 1) nz -= extra_rows;
2302:     PetscBinaryRead(fd,mycols,nz,PETSC_INT);
2303:     if (size == 1) {
2304:       for (i=0; i< extra_rows; i++) mycols[nz+i] = M+i;
2305:     }

2307:     /* read in every ones (except the last) and ship off */
2308:     for (i=1; i<size-1; i++) {
2309:       nz   = procsnz[i];
2310:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
2311:       MPI_Send(cols,nz,MPIU_INT,i,tag,comm);
2312:     }
2313:     /* read in the stuff for the last proc */
2314:     if (size != 1) {
2315:       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
2316:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
2317:       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
2318:       MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);
2319:     }
2320:     PetscFree(cols);
2321:   } else {  /* procs[i], i>0 */
2322:     /* determine buffer space needed for message */
2323:     nz = 0;
2324:     for (i=0; i<m; i++) nz += locrowlens[i];
2325:     PetscMalloc1(nz,&ibuf);
2326:     mycols = ibuf;
2327:     /* receive message of column indices*/
2328:     MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);
2329:     MPI_Get_count(&status,MPIU_INT,&maxnz);
2330:     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2331:   }

2333:   /* loop over local rows, determining number of off diagonal entries */
2334:   PetscMalloc2(rend-rstart,&dlens,rend-rstart,&odlens);
2335:   PetscMalloc3(Mbs,&mask,Mbs,&masked1,Mbs,&masked2);
2336:   PetscMemzero(mask,Mbs*sizeof(PetscInt));
2337:   PetscMemzero(masked1,Mbs*sizeof(PetscInt));
2338:   PetscMemzero(masked2,Mbs*sizeof(PetscInt));
2339:   rowcount = 0;
2340:   nzcount  = 0;
2341:   for (i=0; i<mbs; i++) {
2342:     dcount  = 0;
2343:     odcount = 0;
2344:     for (j=0; j<bs; j++) {
2345:       kmax = locrowlens[rowcount];
2346:       for (k=0; k<kmax; k++) {
2347:         tmp = mycols[nzcount++]/bs; /* block col. index */
2348:         if (!mask[tmp]) {
2349:           mask[tmp] = 1;
2350:           if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp; /* entry in off-diag portion */
2351:           else masked1[dcount++] = tmp; /* entry in diag portion */
2352:         }
2353:       }
2354:       rowcount++;
2355:     }

2357:     dlens[i]  = dcount;  /* d_nzz[i] */
2358:     odlens[i] = odcount; /* o_nzz[i] */

2360:     /* zero out the mask elements we set */
2361:     for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
2362:     for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
2363:   }
2364:   if (!sizesset) {
2365:     MatSetSizes(newmat,m,m,M+extra_rows,N+extra_rows);
2366:   }
2367:   MatMPISBAIJSetPreallocation(newmat,bs,0,dlens,0,odlens);
2368:   MatSetOption(newmat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE);

2370:   if (!rank) {
2371:     PetscMalloc1(maxnz,&buf);
2372:     /* read in my part of the matrix numerical values  */
2373:     nz     = procsnz[0];
2374:     vals   = buf;
2375:     mycols = ibuf;
2376:     if (size == 1) nz -= extra_rows;
2377:     PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2378:     if (size == 1) {
2379:       for (i=0; i< extra_rows; i++) vals[nz+i] = 1.0;
2380:     }

2382:     /* insert into matrix */
2383:     jj = rstart*bs;
2384:     for (i=0; i<m; i++) {
2385:       MatSetValues(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
2386:       mycols += locrowlens[i];
2387:       vals   += locrowlens[i];
2388:       jj++;
2389:     }

2391:     /* read in other processors (except the last one) and ship out */
2392:     for (i=1; i<size-1; i++) {
2393:       nz   = procsnz[i];
2394:       vals = buf;
2395:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2396:       MPI_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);
2397:     }
2398:     /* the last proc */
2399:     if (size != 1) {
2400:       nz   = procsnz[i] - extra_rows;
2401:       vals = buf;
2402:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2403:       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
2404:       MPI_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)newmat)->tag,comm);
2405:     }
2406:     PetscFree(procsnz);

2408:   } else {
2409:     /* receive numeric values */
2410:     PetscMalloc1(nz,&buf);

2412:     /* receive message of values*/
2413:     vals   = buf;
2414:     mycols = ibuf;
2415:     MPI_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm,&status);
2416:     MPI_Get_count(&status,MPIU_SCALAR,&maxnz);
2417:     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");

2419:     /* insert into matrix */
2420:     jj = rstart*bs;
2421:     for (i=0; i<m; i++) {
2422:       MatSetValues_MPISBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
2423:       mycols += locrowlens[i];
2424:       vals   += locrowlens[i];
2425:       jj++;
2426:     }
2427:   }

2429:   PetscFree(locrowlens);
2430:   PetscFree(buf);
2431:   PetscFree(ibuf);
2432:   PetscFree2(rowners,browners);
2433:   PetscFree2(dlens,odlens);
2434:   PetscFree3(mask,masked1,masked2);
2435:   MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
2436:   MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
2437:   return(0);
2438: }

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

2445:    Input Parameters:
2446: .  mat  - the matrix
2447: .  fact - factor

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

2451:    Level: advanced

2453:   Notes:
2454:    This can also be set by the command line option: -mat_use_hash_table fact

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

2458: .seealso: MatSetOption()
2459: @XXXXX*/


2464: PetscErrorCode MatGetRowMaxAbs_MPISBAIJ(Mat A,Vec v,PetscInt idx[])
2465: {
2466:   Mat_MPISBAIJ   *a = (Mat_MPISBAIJ*)A->data;
2467:   Mat_SeqBAIJ    *b = (Mat_SeqBAIJ*)(a->B)->data;
2468:   PetscReal      atmp;
2469:   PetscReal      *work,*svalues,*rvalues;
2471:   PetscInt       i,bs,mbs,*bi,*bj,brow,j,ncols,krow,kcol,col,row,Mbs,bcol;
2472:   PetscMPIInt    rank,size;
2473:   PetscInt       *rowners_bs,dest,count,source;
2474:   PetscScalar    *va;
2475:   MatScalar      *ba;
2476:   MPI_Status     stat;

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

2483:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
2484:   MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);

2486:   bs  = A->rmap->bs;
2487:   mbs = a->mbs;
2488:   Mbs = a->Mbs;
2489:   ba  = b->a;
2490:   bi  = b->i;
2491:   bj  = b->j;

2493:   /* find ownerships */
2494:   rowners_bs = A->rmap->range;

2496:   /* each proc creates an array to be distributed */
2497:   PetscMalloc1(bs*Mbs,&work);
2498:   PetscMemzero(work,bs*Mbs*sizeof(PetscReal));

2500:   /* row_max for B */
2501:   if (rank != size-1) {
2502:     for (i=0; i<mbs; i++) {
2503:       ncols = bi[1] - bi[0]; bi++;
2504:       brow  = bs*i;
2505:       for (j=0; j<ncols; j++) {
2506:         bcol = bs*(*bj);
2507:         for (kcol=0; kcol<bs; kcol++) {
2508:           col  = bcol + kcol;                /* local col index */
2509:           col += rowners_bs[rank+1];      /* global col index */
2510:           for (krow=0; krow<bs; krow++) {
2511:             atmp = PetscAbsScalar(*ba); ba++;
2512:             row  = brow + krow;   /* local row index */
2513:             if (PetscRealPart(va[row]) < atmp) va[row] = atmp;
2514:             if (work[col] < atmp) work[col] = atmp;
2515:           }
2516:         }
2517:         bj++;
2518:       }
2519:     }

2521:     /* send values to its owners */
2522:     for (dest=rank+1; dest<size; dest++) {
2523:       svalues = work + rowners_bs[dest];
2524:       count   = rowners_bs[dest+1]-rowners_bs[dest];
2525:       MPI_Send(svalues,count,MPIU_REAL,dest,rank,PetscObjectComm((PetscObject)A));
2526:     }
2527:   }

2529:   /* receive values */
2530:   if (rank) {
2531:     rvalues = work;
2532:     count   = rowners_bs[rank+1]-rowners_bs[rank];
2533:     for (source=0; source<rank; source++) {
2534:       MPI_Recv(rvalues,count,MPIU_REAL,MPI_ANY_SOURCE,MPI_ANY_TAG,PetscObjectComm((PetscObject)A),&stat);
2535:       /* process values */
2536:       for (i=0; i<count; i++) {
2537:         if (PetscRealPart(va[i]) < rvalues[i]) va[i] = rvalues[i];
2538:       }
2539:     }
2540:   }

2542:   VecRestoreArray(v,&va);
2543:   PetscFree(work);
2544:   return(0);
2545: }

2549: PetscErrorCode MatSOR_MPISBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2550: {
2551:   Mat_MPISBAIJ      *mat = (Mat_MPISBAIJ*)matin->data;
2552:   PetscErrorCode    ierr;
2553:   PetscInt          mbs=mat->mbs,bs=matin->rmap->bs;
2554:   PetscScalar       *x,*ptr,*from;
2555:   Vec               bb1;
2556:   const PetscScalar *b;

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

2562:   if (flag == SOR_APPLY_UPPER) {
2563:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2564:     return(0);
2565:   }

2567:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2568:     if (flag & SOR_ZERO_INITIAL_GUESS) {
2569:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,lits,xx);
2570:       its--;
2571:     }

2573:     VecDuplicate(bb,&bb1);
2574:     while (its--) {

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

2579:       /* copy xx into slvec0a */
2580:       VecGetArray(mat->slvec0,&ptr);
2581:       VecGetArray(xx,&x);
2582:       PetscMemcpy(ptr,x,bs*mbs*sizeof(MatScalar));
2583:       VecRestoreArray(mat->slvec0,&ptr);

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

2587:       /* copy bb into slvec1a */
2588:       VecGetArray(mat->slvec1,&ptr);
2589:       VecGetArrayRead(bb,&b);
2590:       PetscMemcpy(ptr,b,bs*mbs*sizeof(MatScalar));
2591:       VecRestoreArray(mat->slvec1,&ptr);

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

2596:       VecScatterBegin(mat->sMvctx,mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD);
2597:       VecRestoreArray(xx,&x);
2598:       VecRestoreArrayRead(bb,&b);
2599:       VecScatterEnd(mat->sMvctx,mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD);

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

2604:       /* local diagonal sweep */
2605:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
2606:     }
2607:     VecDestroy(&bb1);
2608:   } else if ((flag & SOR_LOCAL_FORWARD_SWEEP) && (its == 1) && (flag & SOR_ZERO_INITIAL_GUESS)) {
2609:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2610:   } else if ((flag & SOR_LOCAL_BACKWARD_SWEEP) && (its == 1) && (flag & SOR_ZERO_INITIAL_GUESS)) {
2611:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2612:   } else if (flag & SOR_EISENSTAT) {
2613:     Vec               xx1;
2614:     PetscBool         hasop;
2615:     const PetscScalar *diag;
2616:     PetscScalar       *sl,scale = (omega - 2.0)/omega;
2617:     PetscInt          i,n;

2619:     if (!mat->xx1) {
2620:       VecDuplicate(bb,&mat->xx1);
2621:       VecDuplicate(bb,&mat->bb1);
2622:     }
2623:     xx1 = mat->xx1;
2624:     bb1 = mat->bb1;

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

2628:     if (!mat->diag) {
2629:       /* this is wrong for same matrix with new nonzero values */
2630:       MatCreateVecs(matin,&mat->diag,NULL);
2631:       MatGetDiagonal(matin,mat->diag);
2632:     }
2633:     MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);

2635:     if (hasop) {
2636:       MatMultDiagonalBlock(matin,xx,bb1);
2637:       VecAYPX(mat->slvec1a,scale,bb);
2638:     } else {
2639:       /*
2640:           These two lines are replaced by code that may be a bit faster for a good compiler
2641:       VecPointwiseMult(mat->slvec1a,mat->diag,xx);
2642:       VecAYPX(mat->slvec1a,scale,bb);
2643:       */
2644:       VecGetArray(mat->slvec1a,&sl);
2645:       VecGetArrayRead(mat->diag,&diag);
2646:       VecGetArrayRead(bb,&b);
2647:       VecGetArray(xx,&x);
2648:       VecGetLocalSize(xx,&n);
2649:       if (omega == 1.0) {
2650:         for (i=0; i<n; i++) sl[i] = b[i] - diag[i]*x[i];
2651:         PetscLogFlops(2.0*n);
2652:       } else {
2653:         for (i=0; i<n; i++) sl[i] = b[i] + scale*diag[i]*x[i];
2654:         PetscLogFlops(3.0*n);
2655:       }
2656:       VecRestoreArray(mat->slvec1a,&sl);
2657:       VecRestoreArrayRead(mat->diag,&diag);
2658:       VecRestoreArrayRead(bb,&b);
2659:       VecRestoreArray(xx,&x);
2660:     }

2662:     /* multiply off-diagonal portion of matrix */
2663:     VecSet(mat->slvec1b,0.0);
2664:     (*mat->B->ops->multtranspose)(mat->B,xx,mat->slvec0b);
2665:     VecGetArray(mat->slvec0,&from);
2666:     VecGetArray(xx,&x);
2667:     PetscMemcpy(from,x,bs*mbs*sizeof(MatScalar));
2668:     VecRestoreArray(mat->slvec0,&from);
2669:     VecRestoreArray(xx,&x);
2670:     VecScatterBegin(mat->sMvctx,mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD);
2671:     VecScatterEnd(mat->sMvctx,mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD);
2672:     (*mat->B->ops->multadd)(mat->B,mat->slvec1b,mat->slvec1a,mat->slvec1a);

2674:     /* local sweep */
2675:     (*mat->A->ops->sor)(mat->A,mat->slvec1a,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_FORWARD_SWEEP),fshift,lits,1,xx1);
2676:     VecAXPY(xx,1.0,xx1);
2677:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatSORType is not supported for SBAIJ matrix format");
2678:   return(0);
2679: }

2683: PetscErrorCode MatSOR_MPISBAIJ_2comm(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2684: {
2685:   Mat_MPISBAIJ   *mat = (Mat_MPISBAIJ*)matin->data;
2687:   Vec            lvec1,bb1;

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

2693:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2694:     if (flag & SOR_ZERO_INITIAL_GUESS) {
2695:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,lits,xx);
2696:       its--;
2697:     }

2699:     VecDuplicate(mat->lvec,&lvec1);
2700:     VecDuplicate(bb,&bb1);
2701:     while (its--) {
2702:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

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

2708:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2709:       VecCopy(bb,bb1);
2710:       VecScatterBegin(mat->Mvctx,lvec1,bb1,ADD_VALUES,SCATTER_REVERSE);

2712:       /* upper diagonal part: bb1 = bb1 - B*x */
2713:       VecScale(mat->lvec,-1.0);
2714:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb1,bb1);

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

2718:       /* diagonal sweep */
2719:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
2720:     }
2721:     VecDestroy(&lvec1);
2722:     VecDestroy(&bb1);
2723:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatSORType is not supported for SBAIJ matrix format");
2724:   return(0);
2725: }

2729: /*@
2730:      MatCreateMPISBAIJWithArrays - creates a MPI SBAIJ matrix using arrays that contain in standard
2731:          CSR format the local rows.

2733:    Collective on MPI_Comm

2735:    Input Parameters:
2736: +  comm - MPI communicator
2737: .  bs - the block size, only a block size of 1 is supported
2738: .  m - number of local rows (Cannot be PETSC_DECIDE)
2739: .  n - This value should be the same as the local size used in creating the
2740:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2741:        calculated if N is given) For square matrices n is almost always m.
2742: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2743: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2744: .   i - row indices
2745: .   j - column indices
2746: -   a - matrix values

2748:    Output Parameter:
2749: .   mat - the matrix

2751:    Level: intermediate

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

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

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

2762: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
2763:           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
2764: @*/
2765: 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)
2766: {


2771:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
2772:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
2773:   MatCreate(comm,mat);
2774:   MatSetSizes(*mat,m,n,M,N);
2775:   MatSetType(*mat,MATMPISBAIJ);
2776:   MatMPISBAIJSetPreallocationCSR(*mat,bs,i,j,a);
2777:   return(0);
2778: }


2783: /*@C
2784:    MatMPISBAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in BAIJ format
2785:    (the default parallel PETSc format).

2787:    Collective on MPI_Comm

2789:    Input Parameters:
2790: +  B - the matrix
2791: .  bs - the block size
2792: .  i - the indices into j for the start of each local row (starts with zero)
2793: .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2794: -  v - optional values in the matrix

2796:    Level: developer

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

2800: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ
2801: @*/
2802: PetscErrorCode  MatMPISBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2803: {

2807:   PetscTryMethod(B,"MatMPISBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));
2808:   return(0);
2809: }

2813: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPISBAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
2814: {
2816:   PetscInt       m,N,i,rstart,nnz,Ii,bs,cbs;
2817:   PetscInt       *indx;
2818:   PetscScalar    *values;

2821:   MatGetSize(inmat,&m,&N);
2822:   if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
2823:     Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)inmat->data;
2824:     PetscInt       *dnz,*onz,sum,bs,cbs,mbs,Nbs;
2825:     PetscInt       *bindx,rmax=a->rmax,j;
2826: 
2827:     MatGetBlockSizes(inmat,&bs,&cbs);
2828:     mbs = m/bs; Nbs = N/cbs;
2829:     if (n == PETSC_DECIDE) {
2830:       PetscSplitOwnership(comm,&n,&Nbs);
2831:     }
2832:     /* Check sum(n) = Nbs */
2833:     MPI_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);
2834:     if (sum != Nbs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",Nbs);

2836:     MPI_Scan(&mbs, &rstart,1,MPIU_INT,MPI_SUM,comm);
2837:     rstart -= mbs;

2839:     PetscMalloc1(rmax,&bindx);
2840:     MatPreallocateInitialize(comm,mbs,n,dnz,onz);
2841:     MatSetOption(inmat,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE);
2842:     for (i=0; i<mbs; i++) {
2843:       MatGetRow_SeqSBAIJ(inmat,i*bs,&nnz,&indx,NULL); /* non-blocked nnz and indx */
2844:       nnz = nnz/bs;
2845:       for (j=0; j<nnz; j++) bindx[j] = indx[j*bs]/bs;
2846:       MatPreallocateSet(i+rstart,nnz,bindx,dnz,onz);
2847:       MatRestoreRow_SeqSBAIJ(inmat,i*bs,&nnz,&indx,NULL);
2848:     }
2849:     MatSetOption(inmat,MAT_GETROW_UPPERTRIANGULAR,PETSC_FALSE);
2850:     PetscFree(bindx);

2852:     MatCreate(comm,outmat);
2853:     MatSetSizes(*outmat,m,n*bs,PETSC_DETERMINE,PETSC_DETERMINE);
2854:     MatSetBlockSizes(*outmat,bs,cbs);
2855:     MatSetType(*outmat,MATMPISBAIJ);
2856:     MatMPISBAIJSetPreallocation(*outmat,bs,0,dnz,0,onz);
2857:     MatPreallocateFinalize(dnz,onz);
2858:   }
2859: 
2860:   /* numeric phase */
2861:   MatGetBlockSizes(inmat,&bs,&cbs);
2862:   MatGetOwnershipRange(*outmat,&rstart,NULL);

2864:   MatSetOption(inmat,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE);
2865:   for (i=0; i<m; i++) {
2866:     MatGetRow_SeqSBAIJ(inmat,i,&nnz,&indx,&values);
2867:     Ii   = i + rstart;
2868:     MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);
2869:     MatRestoreRow_SeqSBAIJ(inmat,i,&nnz,&indx,&values);
2870:   }
2871:   MatSetOption(inmat,MAT_GETROW_UPPERTRIANGULAR,PETSC_FALSE);
2872:   MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);
2873:   MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);
2874:   return(0);
2875: }