Actual source code: mpibaij.c

petsc-3.5.3 2015-01-31
Report Typos and Errors
  2: #include <../src/mat/impls/baij/mpi/mpibaij.h>   /*I  "petscmat.h"  I*/
  3: #include <petscblaslapack.h>
  4: #include <petscsf.h>

  6: extern PetscErrorCode MatSetUpMultiply_MPIBAIJ(Mat);
  7: extern PetscErrorCode MatDisAssemble_MPIBAIJ(Mat);
  8: extern PetscErrorCode MatGetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt [],PetscScalar []);
  9: extern PetscErrorCode MatSetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt [],const PetscScalar [],InsertMode);
 10: extern PetscErrorCode MatSetValuesBlocked_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
 11: extern PetscErrorCode MatGetRow_SeqBAIJ(Mat,PetscInt,PetscInt*,PetscInt*[],PetscScalar*[]);
 12: extern PetscErrorCode MatRestoreRow_SeqBAIJ(Mat,PetscInt,PetscInt*,PetscInt*[],PetscScalar*[]);
 13: extern PetscErrorCode MatZeroRows_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscScalar,Vec,Vec);

 17: PetscErrorCode MatGetRowMaxAbs_MPIBAIJ(Mat A,Vec v,PetscInt idx[])
 18: {
 19:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
 21:   PetscInt       i,*idxb = 0;
 22:   PetscScalar    *va,*vb;
 23:   Vec            vtmp;

 26:   MatGetRowMaxAbs(a->A,v,idx);
 27:   VecGetArray(v,&va);
 28:   if (idx) {
 29:     for (i=0; i<A->rmap->n; i++) {
 30:       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
 31:     }
 32:   }

 34:   VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);
 35:   if (idx) {PetscMalloc1(A->rmap->n,&idxb);}
 36:   MatGetRowMaxAbs(a->B,vtmp,idxb);
 37:   VecGetArray(vtmp,&vb);

 39:   for (i=0; i<A->rmap->n; i++) {
 40:     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
 41:       va[i] = vb[i];
 42:       if (idx) idx[i] = A->cmap->bs*a->garray[idxb[i]/A->cmap->bs] + (idxb[i] % A->cmap->bs);
 43:     }
 44:   }

 46:   VecRestoreArray(v,&va);
 47:   VecRestoreArray(vtmp,&vb);
 48:   PetscFree(idxb);
 49:   VecDestroy(&vtmp);
 50:   return(0);
 51: }

 55: PetscErrorCode  MatStoreValues_MPIBAIJ(Mat mat)
 56: {
 57:   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ*)mat->data;

 61:   MatStoreValues(aij->A);
 62:   MatStoreValues(aij->B);
 63:   return(0);
 64: }

 68: PetscErrorCode  MatRetrieveValues_MPIBAIJ(Mat mat)
 69: {
 70:   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ*)mat->data;

 74:   MatRetrieveValues(aij->A);
 75:   MatRetrieveValues(aij->B);
 76:   return(0);
 77: }

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

 95: #if defined(PETSC_USE_CTABLE)
 96:   PetscTableCreate(baij->nbs,baij->Nbs+1,&baij->colmap);
 97:   for (i=0; i<nbs; i++) {
 98:     PetscTableAdd(baij->colmap,baij->garray[i]+1,i*bs+1,INSERT_VALUES);
 99:   }
100: #else
101:   PetscMalloc1((baij->Nbs+1),&baij->colmap);
102:   PetscLogObjectMemory((PetscObject)mat,baij->Nbs*sizeof(PetscInt));
103:   PetscMemzero(baij->colmap,baij->Nbs*sizeof(PetscInt));
104:   for (i=0; i<nbs; i++) baij->colmap[baij->garray[i]] = i*bs+1;
105: #endif
106:   return(0);
107: }

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

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

188: PetscErrorCode MatSetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
189: {
190:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
191:   MatScalar      value;
192:   PetscBool      roworiented = baij->roworiented;
194:   PetscInt       i,j,row,col;
195:   PetscInt       rstart_orig=mat->rmap->rstart;
196:   PetscInt       rend_orig  =mat->rmap->rend,cstart_orig=mat->cmap->rstart;
197:   PetscInt       cend_orig  =mat->cmap->rend,bs=mat->rmap->bs;

199:   /* Some Variables required in the macro */
200:   Mat         A     = baij->A;
201:   Mat_SeqBAIJ *a    = (Mat_SeqBAIJ*)(A)->data;
202:   PetscInt    *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
203:   MatScalar   *aa   =a->a;

205:   Mat         B     = baij->B;
206:   Mat_SeqBAIJ *b    = (Mat_SeqBAIJ*)(B)->data;
207:   PetscInt    *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
208:   MatScalar   *ba   =b->a;

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

215:   for (i=0; i<m; i++) {
216:     if (im[i] < 0) continue;
217: #if defined(PETSC_USE_DEBUG)
218:     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);
219: #endif
220:     if (im[i] >= rstart_orig && im[i] < rend_orig) {
221:       row = im[i] - rstart_orig;
222:       for (j=0; j<n; j++) {
223:         if (in[j] >= cstart_orig && in[j] < cend_orig) {
224:           col = in[j] - cstart_orig;
225:           if (roworiented) value = v[i*n+j];
226:           else             value = v[i+j*m];
227:           MatSetValues_SeqBAIJ_A_Private(row,col,value,addv);
228:           /* MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv); */
229:         } else if (in[j] < 0) continue;
230: #if defined(PETSC_USE_DEBUG)
231:         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);
232: #endif
233:         else {
234:           if (mat->was_assembled) {
235:             if (!baij->colmap) {
236:               MatCreateColmap_MPIBAIJ_Private(mat);
237:             }
238: #if defined(PETSC_USE_CTABLE)
239:             PetscTableFind(baij->colmap,in[j]/bs + 1,&col);
240:             col  = col - 1;
241: #else
242:             col = baij->colmap[in[j]/bs] - 1;
243: #endif
244:             if (col < 0 && !((Mat_SeqBAIJ*)(baij->B->data))->nonew) {
245:               MatDisAssemble_MPIBAIJ(mat);
246:               col  =  in[j];
247:               /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
248:               B    = baij->B;
249:               b    = (Mat_SeqBAIJ*)(B)->data;
250:               bimax=b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
251:               ba   =b->a;
252:             } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", im[i], in[j]);
253:             else col += in[j]%bs;
254:           } else col = in[j];
255:           if (roworiented) value = v[i*n+j];
256:           else             value = v[i+j*m];
257:           MatSetValues_SeqBAIJ_B_Private(row,col,value,addv);
258:           /* MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv); */
259:         }
260:       }
261:     } else {
262:       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]);
263:       if (!baij->donotstash) {
264:         mat->assembled = PETSC_FALSE;
265:         if (roworiented) {
266:           MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);
267:         } else {
268:           MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);
269:         }
270:       }
271:     }
272:   }
273:   return(0);
274: }

278: PetscErrorCode MatSetValuesBlocked_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
279: {
280:   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
281:   const PetscScalar *value;
282:   MatScalar         *barray     = baij->barray;
283:   PetscBool         roworiented = baij->roworiented;
284:   PetscErrorCode    ierr;
285:   PetscInt          i,j,ii,jj,row,col,rstart=baij->rstartbs;
286:   PetscInt          rend=baij->rendbs,cstart=baij->cstartbs,stepval;
287:   PetscInt          cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;

290:   if (!barray) {
291:     PetscMalloc1(bs2,&barray);
292:     baij->barray = barray;
293:   }

295:   if (roworiented) stepval = (n-1)*bs;
296:   else stepval = (m-1)*bs;

298:   for (i=0; i<m; i++) {
299:     if (im[i] < 0) continue;
300: #if defined(PETSC_USE_DEBUG)
301:     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);
302: #endif
303:     if (im[i] >= rstart && im[i] < rend) {
304:       row = im[i] - rstart;
305:       for (j=0; j<n; j++) {
306:         /* If NumCol = 1 then a copy is not required */
307:         if ((roworiented) && (n == 1)) {
308:           barray = (MatScalar*)v + i*bs2;
309:         } else if ((!roworiented) && (m == 1)) {
310:           barray = (MatScalar*)v + j*bs2;
311:         } else { /* Here a copy is required */
312:           if (roworiented) {
313:             value = v + (i*(stepval+bs) + j)*bs;
314:           } else {
315:             value = v + (j*(stepval+bs) + i)*bs;
316:           }
317:           for (ii=0; ii<bs; ii++,value+=bs+stepval) {
318:             for (jj=0; jj<bs; jj++) barray[jj] = value[jj];
319:             barray += bs;
320:           }
321:           barray -= bs2;
322:         }

324:         if (in[j] >= cstart && in[j] < cend) {
325:           col  = in[j] - cstart;
326:           MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);
327:         } else if (in[j] < 0) continue;
328: #if defined(PETSC_USE_DEBUG)
329:         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);
330: #endif
331:         else {
332:           if (mat->was_assembled) {
333:             if (!baij->colmap) {
334:               MatCreateColmap_MPIBAIJ_Private(mat);
335:             }

337: #if defined(PETSC_USE_DEBUG)
338: #if defined(PETSC_USE_CTABLE)
339:             { PetscInt data;
340:               PetscTableFind(baij->colmap,in[j]+1,&data);
341:               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
342:             }
343: #else
344:             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
345: #endif
346: #endif
347: #if defined(PETSC_USE_CTABLE)
348:             PetscTableFind(baij->colmap,in[j]+1,&col);
349:             col  = (col - 1)/bs;
350: #else
351:             col = (baij->colmap[in[j]] - 1)/bs;
352: #endif
353:             if (col < 0 && !((Mat_SeqBAIJ*)(baij->B->data))->nonew) {
354:               MatDisAssemble_MPIBAIJ(mat);
355:               col  =  in[j];
356:             } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", bs*im[i], bs*in[j]);
357:           } else col = in[j];
358:           MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);
359:         }
360:       }
361:     } else {
362:       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]);
363:       if (!baij->donotstash) {
364:         if (roworiented) {
365:           MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
366:         } else {
367:           MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
368:         }
369:       }
370:     }
371:   }
372:   return(0);
373: }

375: #define HASH_KEY 0.6180339887
376: #define HASH(size,key,tmp) (tmp = (key)*HASH_KEY,(PetscInt)((size)*(tmp-(PetscInt)tmp)))
377: /* #define HASH(size,key) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
378: /* #define HASH(size,key,tmp) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
381: PetscErrorCode MatSetValues_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
382: {
383:   Mat_MPIBAIJ    *baij       = (Mat_MPIBAIJ*)mat->data;
384:   PetscBool      roworiented = baij->roworiented;
386:   PetscInt       i,j,row,col;
387:   PetscInt       rstart_orig=mat->rmap->rstart;
388:   PetscInt       rend_orig  =mat->rmap->rend,Nbs=baij->Nbs;
389:   PetscInt       h1,key,size=baij->ht_size,bs=mat->rmap->bs,*HT=baij->ht,idx;
390:   PetscReal      tmp;
391:   MatScalar      **HD = baij->hd,value;
392: #if defined(PETSC_USE_DEBUG)
393:   PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
394: #endif

397:   for (i=0; i<m; i++) {
398: #if defined(PETSC_USE_DEBUG)
399:     if (im[i] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
400:     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);
401: #endif
402:     row = im[i];
403:     if (row >= rstart_orig && row < rend_orig) {
404:       for (j=0; j<n; j++) {
405:         col = in[j];
406:         if (roworiented) value = v[i*n+j];
407:         else             value = v[i+j*m];
408:         /* Look up PetscInto the Hash Table */
409:         key = (row/bs)*Nbs+(col/bs)+1;
410:         h1  = HASH(size,key,tmp);


413:         idx = h1;
414: #if defined(PETSC_USE_DEBUG)
415:         insert_ct++;
416:         total_ct++;
417:         if (HT[idx] != key) {
418:           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++) ;
419:           if (idx == size) {
420:             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++) ;
421:             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
422:           }
423:         }
424: #else
425:         if (HT[idx] != key) {
426:           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++) ;
427:           if (idx == size) {
428:             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++) ;
429:             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
430:           }
431:         }
432: #endif
433:         /* A HASH table entry is found, so insert the values at the correct address */
434:         if (addv == ADD_VALUES) *(HD[idx]+ (col % bs)*bs + (row % bs)) += value;
435:         else                    *(HD[idx]+ (col % bs)*bs + (row % bs))  = value;
436:       }
437:     } else if (!baij->donotstash) {
438:       if (roworiented) {
439:         MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);
440:       } else {
441:         MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);
442:       }
443:     }
444:   }
445: #if defined(PETSC_USE_DEBUG)
446:   baij->ht_total_ct  = total_ct;
447:   baij->ht_insert_ct = insert_ct;
448: #endif
449:   return(0);
450: }

454: PetscErrorCode MatSetValuesBlocked_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
455: {
456:   Mat_MPIBAIJ       *baij       = (Mat_MPIBAIJ*)mat->data;
457:   PetscBool         roworiented = baij->roworiented;
458:   PetscErrorCode    ierr;
459:   PetscInt          i,j,ii,jj,row,col;
460:   PetscInt          rstart=baij->rstartbs;
461:   PetscInt          rend  =mat->rmap->rend,stepval,bs=mat->rmap->bs,bs2=baij->bs2,nbs2=n*bs2;
462:   PetscInt          h1,key,size=baij->ht_size,idx,*HT=baij->ht,Nbs=baij->Nbs;
463:   PetscReal         tmp;
464:   MatScalar         **HD = baij->hd,*baij_a;
465:   const PetscScalar *v_t,*value;
466: #if defined(PETSC_USE_DEBUG)
467:   PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
468: #endif

471:   if (roworiented) stepval = (n-1)*bs;
472:   else stepval = (m-1)*bs;

474:   for (i=0; i<m; i++) {
475: #if defined(PETSC_USE_DEBUG)
476:     if (im[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",im[i]);
477:     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);
478: #endif
479:     row = im[i];
480:     v_t = v + i*nbs2;
481:     if (row >= rstart && row < rend) {
482:       for (j=0; j<n; j++) {
483:         col = in[j];

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

489:         idx = h1;
490: #if defined(PETSC_USE_DEBUG)
491:         total_ct++;
492:         insert_ct++;
493:         if (HT[idx] != key) {
494:           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++) ;
495:           if (idx == size) {
496:             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++) ;
497:             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
498:           }
499:         }
500: #else
501:         if (HT[idx] != key) {
502:           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++) ;
503:           if (idx == size) {
504:             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++) ;
505:             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
506:           }
507:         }
508: #endif
509:         baij_a = HD[idx];
510:         if (roworiented) {
511:           /*value = v + i*(stepval+bs)*bs + j*bs;*/
512:           /* value = v + (i*(stepval+bs)+j)*bs; */
513:           value = v_t;
514:           v_t  += bs;
515:           if (addv == ADD_VALUES) {
516:             for (ii=0; ii<bs; ii++,value+=stepval) {
517:               for (jj=ii; jj<bs2; jj+=bs) {
518:                 baij_a[jj] += *value++;
519:               }
520:             }
521:           } else {
522:             for (ii=0; ii<bs; ii++,value+=stepval) {
523:               for (jj=ii; jj<bs2; jj+=bs) {
524:                 baij_a[jj] = *value++;
525:               }
526:             }
527:           }
528:         } else {
529:           value = v + j*(stepval+bs)*bs + i*bs;
530:           if (addv == ADD_VALUES) {
531:             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
532:               for (jj=0; jj<bs; jj++) {
533:                 baij_a[jj] += *value++;
534:               }
535:             }
536:           } else {
537:             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
538:               for (jj=0; jj<bs; jj++) {
539:                 baij_a[jj] = *value++;
540:               }
541:             }
542:           }
543:         }
544:       }
545:     } else {
546:       if (!baij->donotstash) {
547:         if (roworiented) {
548:           MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
549:         } else {
550:           MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
551:         }
552:       }
553:     }
554:   }
555: #if defined(PETSC_USE_DEBUG)
556:   baij->ht_total_ct  = total_ct;
557:   baij->ht_insert_ct = insert_ct;
558: #endif
559:   return(0);
560: }

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

572:   for (i=0; i<m; i++) {
573:     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
574:     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);
575:     if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
576:       row = idxm[i] - bsrstart;
577:       for (j=0; j<n; j++) {
578:         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
579:         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);
580:         if (idxn[j] >= bscstart && idxn[j] < bscend) {
581:           col  = idxn[j] - bscstart;
582:           MatGetValues_SeqBAIJ(baij->A,1,&row,1,&col,v+i*n+j);
583:         } else {
584:           if (!baij->colmap) {
585:             MatCreateColmap_MPIBAIJ_Private(mat);
586:           }
587: #if defined(PETSC_USE_CTABLE)
588:           PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);
589:           data--;
590: #else
591:           data = baij->colmap[idxn[j]/bs]-1;
592: #endif
593:           if ((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
594:           else {
595:             col  = data + idxn[j]%bs;
596:             MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);
597:           }
598:         }
599:       }
600:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
601:   }
602:   return(0);
603: }

607: PetscErrorCode MatNorm_MPIBAIJ(Mat mat,NormType type,PetscReal *nrm)
608: {
609:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
610:   Mat_SeqBAIJ    *amat = (Mat_SeqBAIJ*)baij->A->data,*bmat = (Mat_SeqBAIJ*)baij->B->data;
612:   PetscInt       i,j,bs2=baij->bs2,bs=baij->A->rmap->bs,nz,row,col;
613:   PetscReal      sum = 0.0;
614:   MatScalar      *v;

617:   if (baij->size == 1) {
618:      MatNorm(baij->A,type,nrm);
619:   } else {
620:     if (type == NORM_FROBENIUS) {
621:       v  = amat->a;
622:       nz = amat->nz*bs2;
623:       for (i=0; i<nz; i++) {
624:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
625:       }
626:       v  = bmat->a;
627:       nz = bmat->nz*bs2;
628:       for (i=0; i<nz; i++) {
629:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
630:       }
631:       MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
632:       *nrm = PetscSqrtReal(*nrm);
633:     } else if (type == NORM_1) { /* max column sum */
634:       PetscReal *tmp,*tmp2;
635:       PetscInt  *jj,*garray=baij->garray,cstart=baij->rstartbs;
636:       PetscMalloc2(mat->cmap->N,&tmp,mat->cmap->N,&tmp2);
637:       PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));
638:       v    = amat->a; jj = amat->j;
639:       for (i=0; i<amat->nz; i++) {
640:         for (j=0; j<bs; j++) {
641:           col = bs*(cstart + *jj) + j; /* column index */
642:           for (row=0; row<bs; row++) {
643:             tmp[col] += PetscAbsScalar(*v);  v++;
644:           }
645:         }
646:         jj++;
647:       }
648:       v = bmat->a; jj = bmat->j;
649:       for (i=0; i<bmat->nz; i++) {
650:         for (j=0; j<bs; j++) {
651:           col = bs*garray[*jj] + j;
652:           for (row=0; row<bs; row++) {
653:             tmp[col] += PetscAbsScalar(*v); v++;
654:           }
655:         }
656:         jj++;
657:       }
658:       MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
659:       *nrm = 0.0;
660:       for (j=0; j<mat->cmap->N; j++) {
661:         if (tmp2[j] > *nrm) *nrm = tmp2[j];
662:       }
663:       PetscFree2(tmp,tmp2);
664:     } else if (type == NORM_INFINITY) { /* max row sum */
665:       PetscReal *sums;
666:       PetscMalloc1(bs,&sums);
667:       sum  = 0.0;
668:       for (j=0; j<amat->mbs; j++) {
669:         for (row=0; row<bs; row++) sums[row] = 0.0;
670:         v  = amat->a + bs2*amat->i[j];
671:         nz = amat->i[j+1]-amat->i[j];
672:         for (i=0; i<nz; i++) {
673:           for (col=0; col<bs; col++) {
674:             for (row=0; row<bs; row++) {
675:               sums[row] += PetscAbsScalar(*v); v++;
676:             }
677:           }
678:         }
679:         v  = bmat->a + bs2*bmat->i[j];
680:         nz = bmat->i[j+1]-bmat->i[j];
681:         for (i=0; i<nz; i++) {
682:           for (col=0; col<bs; col++) {
683:             for (row=0; row<bs; row++) {
684:               sums[row] += PetscAbsScalar(*v); v++;
685:             }
686:           }
687:         }
688:         for (row=0; row<bs; row++) {
689:           if (sums[row] > sum) sum = sums[row];
690:         }
691:       }
692:       MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));
693:       PetscFree(sums);
694:     } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for this norm yet");
695:   }
696:   return(0);
697: }

699: /*
700:   Creates the hash table, and sets the table
701:   This table is created only once.
702:   If new entried need to be added to the matrix
703:   then the hash table has to be destroyed and
704:   recreated.
705: */
708: PetscErrorCode MatCreateHashTable_MPIBAIJ_Private(Mat mat,PetscReal factor)
709: {
710:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
711:   Mat            A     = baij->A,B=baij->B;
712:   Mat_SeqBAIJ    *a    = (Mat_SeqBAIJ*)A->data,*b=(Mat_SeqBAIJ*)B->data;
713:   PetscInt       i,j,k,nz=a->nz+b->nz,h1,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
715:   PetscInt       ht_size,bs2=baij->bs2,rstart=baij->rstartbs;
716:   PetscInt       cstart=baij->cstartbs,*garray=baij->garray,row,col,Nbs=baij->Nbs;
717:   PetscInt       *HT,key;
718:   MatScalar      **HD;
719:   PetscReal      tmp;
720: #if defined(PETSC_USE_INFO)
721:   PetscInt ct=0,max=0;
722: #endif

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

727:   baij->ht_size = (PetscInt)(factor*nz);
728:   ht_size       = baij->ht_size;

730:   /* Allocate Memory for Hash Table */
731:   PetscCalloc2(ht_size,&baij->hd,ht_size,&baij->ht);
732:   HD   = baij->hd;
733:   HT   = baij->ht;

735:   /* Loop Over A */
736:   for (i=0; i<a->mbs; i++) {
737:     for (j=ai[i]; j<ai[i+1]; j++) {
738:       row = i+rstart;
739:       col = aj[j]+cstart;

741:       key = row*Nbs + col + 1;
742:       h1  = HASH(ht_size,key,tmp);
743:       for (k=0; k<ht_size; k++) {
744:         if (!HT[(h1+k)%ht_size]) {
745:           HT[(h1+k)%ht_size] = key;
746:           HD[(h1+k)%ht_size] = a->a + j*bs2;
747:           break;
748: #if defined(PETSC_USE_INFO)
749:         } else {
750:           ct++;
751: #endif
752:         }
753:       }
754: #if defined(PETSC_USE_INFO)
755:       if (k> max) max = k;
756: #endif
757:     }
758:   }
759:   /* Loop Over B */
760:   for (i=0; i<b->mbs; i++) {
761:     for (j=bi[i]; j<bi[i+1]; j++) {
762:       row = i+rstart;
763:       col = garray[bj[j]];
764:       key = row*Nbs + col + 1;
765:       h1  = HASH(ht_size,key,tmp);
766:       for (k=0; k<ht_size; k++) {
767:         if (!HT[(h1+k)%ht_size]) {
768:           HT[(h1+k)%ht_size] = key;
769:           HD[(h1+k)%ht_size] = b->a + j*bs2;
770:           break;
771: #if defined(PETSC_USE_INFO)
772:         } else {
773:           ct++;
774: #endif
775:         }
776:       }
777: #if defined(PETSC_USE_INFO)
778:       if (k> max) max = k;
779: #endif
780:     }
781:   }

783:   /* Print Summary */
784: #if defined(PETSC_USE_INFO)
785:   for (i=0,j=0; i<ht_size; i++) {
786:     if (HT[i]) j++;
787:   }
788:   PetscInfo2(mat,"Average Search = %5.2f,max search = %D\n",(!j)? 0.0:((PetscReal)(ct+j))/j,max);
789: #endif
790:   return(0);
791: }

795: PetscErrorCode MatAssemblyBegin_MPIBAIJ(Mat mat,MatAssemblyType mode)
796: {
797:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
799:   PetscInt       nstash,reallocs;
800:   InsertMode     addv;

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

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

810:   MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
811:   MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);
812:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
813:   PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);
814:   MatStashGetInfo_Private(&mat->bstash,&nstash,&reallocs);
815:   PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
816:   return(0);
817: }

821: PetscErrorCode MatAssemblyEnd_MPIBAIJ(Mat mat,MatAssemblyType mode)
822: {
823:   Mat_MPIBAIJ    *baij=(Mat_MPIBAIJ*)mat->data;
824:   Mat_SeqBAIJ    *a   =(Mat_SeqBAIJ*)baij->A->data;
826:   PetscInt       i,j,rstart,ncols,flg,bs2=baij->bs2;
827:   PetscInt       *row,*col;
828:   PetscBool      r1,r2,r3,other_disassembled;
829:   MatScalar      *val;
830:   InsertMode     addv = mat->insertmode;
831:   PetscMPIInt    n;

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

840:       for (i=0; i<n;) {
841:         /* Now identify the consecutive vals belonging to the same row */
842:         for (j=i,rstart=row[j]; j<n; j++) {
843:           if (row[j] != rstart) break;
844:         }
845:         if (j < n) ncols = j-i;
846:         else       ncols = n-i;
847:         /* Now assemble all these values with a single function call */
848:         MatSetValues_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i,addv);
849:         i    = j;
850:       }
851:     }
852:     MatStashScatterEnd_Private(&mat->stash);
853:     /* Now process the block-stash. Since the values are stashed column-oriented,
854:        set the roworiented flag to column oriented, and after MatSetValues()
855:        restore the original flags */
856:     r1 = baij->roworiented;
857:     r2 = a->roworiented;
858:     r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented;

860:     baij->roworiented = PETSC_FALSE;
861:     a->roworiented    = PETSC_FALSE;

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

868:       for (i=0; i<n;) {
869:         /* Now identify the consecutive vals belonging to the same row */
870:         for (j=i,rstart=row[j]; j<n; j++) {
871:           if (row[j] != rstart) break;
872:         }
873:         if (j < n) ncols = j-i;
874:         else       ncols = n-i;
875:         MatSetValuesBlocked_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,addv);
876:         i    = j;
877:       }
878:     }
879:     MatStashScatterEnd_Private(&mat->bstash);

881:     baij->roworiented = r1;
882:     a->roworiented    = r2;

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

887:   MatAssemblyBegin(baij->A,mode);
888:   MatAssemblyEnd(baij->A,mode);

890:   /* determine if any processor has disassembled, if so we must
891:      also disassemble ourselfs, in order that we may reassemble. */
892:   /*
893:      if nonzero structure of submatrix B cannot change then we know that
894:      no processor disassembled thus we can skip this stuff
895:   */
896:   if (!((Mat_SeqBAIJ*)baij->B->data)->nonew) {
897:     MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));
898:     if (mat->was_assembled && !other_disassembled) {
899:       MatDisAssemble_MPIBAIJ(mat);
900:     }
901:   }

903:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
904:     MatSetUpMultiply_MPIBAIJ(mat);
905:   }
906:   MatAssemblyBegin(baij->B,mode);
907:   MatAssemblyEnd(baij->B,mode);

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

913:     baij->ht_total_ct  = 0;
914:     baij->ht_insert_ct = 0;
915:   }
916: #endif
917:   if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) {
918:     MatCreateHashTable_MPIBAIJ_Private(mat,baij->ht_fact);

920:     mat->ops->setvalues        = MatSetValues_MPIBAIJ_HT;
921:     mat->ops->setvaluesblocked = MatSetValuesBlocked_MPIBAIJ_HT;
922:   }

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

926:   baij->rowvalues = 0;

928:   /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
929:   if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
930:     PetscObjectState state = baij->A->nonzerostate + baij->B->nonzerostate;
931:     MPI_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));
932:   }
933:   return(0);
934: }

936: extern PetscErrorCode MatView_SeqBAIJ(Mat,PetscViewer);
937: #include <petscdraw.h>
940: static PetscErrorCode MatView_MPIBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
941: {
942:   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
943:   PetscErrorCode    ierr;
944:   PetscMPIInt       rank = baij->rank;
945:   PetscInt          bs   = mat->rmap->bs;
946:   PetscBool         iascii,isdraw;
947:   PetscViewer       sviewer;
948:   PetscViewerFormat format;

951:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
952:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
953:   if (iascii) {
954:     PetscViewerGetFormat(viewer,&format);
955:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
956:       MatInfo info;
957:       MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
958:       MatGetInfo(mat,MAT_LOCAL,&info);
959:       PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);
960:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D bs %D mem %D\n",
961:                                                 rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,mat->rmap->bs,(PetscInt)info.memory);
962:       MatGetInfo(baij->A,MAT_LOCAL,&info);
963:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
964:       MatGetInfo(baij->B,MAT_LOCAL,&info);
965:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
966:       PetscViewerFlush(viewer);
967:       PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);
968:       PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");
969:       VecScatterView(baij->Mvctx,viewer);
970:       return(0);
971:     } else if (format == PETSC_VIEWER_ASCII_INFO) {
972:       PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);
973:       return(0);
974:     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
975:       return(0);
976:     }
977:   }

979:   if (isdraw) {
980:     PetscDraw draw;
981:     PetscBool isnull;
982:     PetscViewerDrawGetDraw(viewer,0,&draw);
983:     PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
984:   }

986:   {
987:     /* assemble the entire matrix onto first processor. */
988:     Mat         A;
989:     Mat_SeqBAIJ *Aloc;
990:     PetscInt    M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
991:     MatScalar   *a;
992:     const char  *matname;

994:     /* Here we are creating a temporary matrix, so will assume MPIBAIJ is acceptable */
995:     /* Perhaps this should be the type of mat? */
996:     MatCreate(PetscObjectComm((PetscObject)mat),&A);
997:     if (!rank) {
998:       MatSetSizes(A,M,N,M,N);
999:     } else {
1000:       MatSetSizes(A,0,0,M,N);
1001:     }
1002:     MatSetType(A,MATMPIBAIJ);
1003:     MatMPIBAIJSetPreallocation(A,mat->rmap->bs,0,NULL,0,NULL);
1004:     MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
1005:     PetscLogObjectParent((PetscObject)mat,(PetscObject)A);

1007:     /* copy over the A part */
1008:     Aloc = (Mat_SeqBAIJ*)baij->A->data;
1009:     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1010:     PetscMalloc1(bs,&rvals);

1012:     for (i=0; i<mbs; i++) {
1013:       rvals[0] = bs*(baij->rstartbs + i);
1014:       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1015:       for (j=ai[i]; j<ai[i+1]; j++) {
1016:         col = (baij->cstartbs+aj[j])*bs;
1017:         for (k=0; k<bs; k++) {
1018:           MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
1019:           col++; a += bs;
1020:         }
1021:       }
1022:     }
1023:     /* copy over the B part */
1024:     Aloc = (Mat_SeqBAIJ*)baij->B->data;
1025:     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1026:     for (i=0; i<mbs; i++) {
1027:       rvals[0] = bs*(baij->rstartbs + i);
1028:       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1029:       for (j=ai[i]; j<ai[i+1]; j++) {
1030:         col = baij->garray[aj[j]]*bs;
1031:         for (k=0; k<bs; k++) {
1032:           MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
1033:           col++; a += bs;
1034:         }
1035:       }
1036:     }
1037:     PetscFree(rvals);
1038:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1039:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1040:     /*
1041:        Everyone has to call to draw the matrix since the graphics waits are
1042:        synchronized across all processors that share the PetscDraw object
1043:     */
1044:     PetscViewerGetSingleton(viewer,&sviewer);
1045:     PetscObjectGetName((PetscObject)mat,&matname);
1046:     if (!rank) {
1047:       PetscObjectSetName((PetscObject)((Mat_MPIBAIJ*)(A->data))->A,matname);
1048:       MatView_SeqBAIJ(((Mat_MPIBAIJ*)(A->data))->A,sviewer);
1049:     }
1050:     PetscViewerRestoreSingleton(viewer,&sviewer);
1051:     MatDestroy(&A);
1052:   }
1053:   return(0);
1054: }

1058: static PetscErrorCode MatView_MPIBAIJ_Binary(Mat mat,PetscViewer viewer)
1059: {
1060:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)mat->data;
1061:   Mat_SeqBAIJ    *A = (Mat_SeqBAIJ*)a->A->data;
1062:   Mat_SeqBAIJ    *B = (Mat_SeqBAIJ*)a->B->data;
1064:   PetscInt       i,*row_lens,*crow_lens,bs = mat->rmap->bs,j,k,bs2=a->bs2,header[4],nz,rlen;
1065:   PetscInt       *range=0,nzmax,*column_indices,cnt,col,*garray = a->garray,cstart = mat->cmap->rstart/bs,len,pcnt,l,ll;
1066:   int            fd;
1067:   PetscScalar    *column_values;
1068:   FILE           *file;
1069:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
1070:   PetscInt       message_count,flowcontrolcount;

1073:   MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
1074:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
1075:   nz   = bs2*(A->nz + B->nz);
1076:   rlen = mat->rmap->n;
1077:   if (!rank) {
1078:     header[0] = MAT_FILE_CLASSID;
1079:     header[1] = mat->rmap->N;
1080:     header[2] = mat->cmap->N;

1082:     MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1083:     PetscViewerBinaryGetDescriptor(viewer,&fd);
1084:     PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);
1085:     /* get largest number of rows any processor has */
1086:     range = mat->rmap->range;
1087:     for (i=1; i<size; i++) {
1088:       rlen = PetscMax(rlen,range[i+1] - range[i]);
1089:     }
1090:   } else {
1091:     MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1092:   }

1094:   PetscMalloc1((rlen/bs),&crow_lens);
1095:   /* compute lengths of each row  */
1096:   for (i=0; i<a->mbs; i++) {
1097:     crow_lens[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
1098:   }
1099:   /* store the row lengths to the file */
1100:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1101:   if (!rank) {
1102:     MPI_Status status;
1103:     PetscMalloc1(rlen,&row_lens);
1104:     rlen = (range[1] - range[0])/bs;
1105:     for (i=0; i<rlen; i++) {
1106:       for (j=0; j<bs; j++) {
1107:         row_lens[i*bs+j] = bs*crow_lens[i];
1108:       }
1109:     }
1110:     PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);
1111:     for (i=1; i<size; i++) {
1112:       rlen = (range[i+1] - range[i])/bs;
1113:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1114:       MPI_Recv(crow_lens,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1115:       for (k=0; k<rlen; k++) {
1116:         for (j=0; j<bs; j++) {
1117:           row_lens[k*bs+j] = bs*crow_lens[k];
1118:         }
1119:       }
1120:       PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);
1121:     }
1122:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1123:     PetscFree(row_lens);
1124:   } else {
1125:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1126:     MPI_Send(crow_lens,mat->rmap->n/bs,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1127:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1128:   }
1129:   PetscFree(crow_lens);

1131:   /* load up the local column indices. Include for all rows not just one for each block row since process 0 does not have the
1132:      information needed to make it for each row from a block row. This does require more communication but still not more than
1133:      the communication needed for the nonzero values  */
1134:   nzmax = nz; /*  space a largest processor needs */
1135:   MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));
1136:   PetscMalloc1(nzmax,&column_indices);
1137:   cnt   = 0;
1138:   for (i=0; i<a->mbs; i++) {
1139:     pcnt = cnt;
1140:     for (j=B->i[i]; j<B->i[i+1]; j++) {
1141:       if ((col = garray[B->j[j]]) > cstart) break;
1142:       for (l=0; l<bs; l++) {
1143:         column_indices[cnt++] = bs*col+l;
1144:       }
1145:     }
1146:     for (k=A->i[i]; k<A->i[i+1]; k++) {
1147:       for (l=0; l<bs; l++) {
1148:         column_indices[cnt++] = bs*(A->j[k] + cstart)+l;
1149:       }
1150:     }
1151:     for (; j<B->i[i+1]; j++) {
1152:       for (l=0; l<bs; l++) {
1153:         column_indices[cnt++] = bs*garray[B->j[j]]+l;
1154:       }
1155:     }
1156:     len = cnt - pcnt;
1157:     for (k=1; k<bs; k++) {
1158:       PetscMemcpy(&column_indices[cnt],&column_indices[pcnt],len*sizeof(PetscInt));
1159:       cnt += len;
1160:     }
1161:   }
1162:   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);

1164:   /* store the columns to the file */
1165:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1166:   if (!rank) {
1167:     MPI_Status status;
1168:     PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);
1169:     for (i=1; i<size; i++) {
1170:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1171:       MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1172:       MPI_Recv(column_indices,cnt,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1173:       PetscBinaryWrite(fd,column_indices,cnt,PETSC_INT,PETSC_TRUE);
1174:     }
1175:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1176:   } else {
1177:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1178:     MPI_Send(&cnt,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1179:     MPI_Send(column_indices,cnt,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1180:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1181:   }
1182:   PetscFree(column_indices);

1184:   /* load up the numerical values */
1185:   PetscMalloc1(nzmax,&column_values);
1186:   cnt  = 0;
1187:   for (i=0; i<a->mbs; i++) {
1188:     rlen = bs*(B->i[i+1] - B->i[i] + A->i[i+1] - A->i[i]);
1189:     for (j=B->i[i]; j<B->i[i+1]; j++) {
1190:       if (garray[B->j[j]] > cstart) break;
1191:       for (l=0; l<bs; l++) {
1192:         for (ll=0; ll<bs; ll++) {
1193:           column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1194:         }
1195:       }
1196:       cnt += bs;
1197:     }
1198:     for (k=A->i[i]; k<A->i[i+1]; k++) {
1199:       for (l=0; l<bs; l++) {
1200:         for (ll=0; ll<bs; ll++) {
1201:           column_values[cnt + l*rlen + ll] = A->a[bs2*k+l+bs*ll];
1202:         }
1203:       }
1204:       cnt += bs;
1205:     }
1206:     for (; j<B->i[i+1]; j++) {
1207:       for (l=0; l<bs; l++) {
1208:         for (ll=0; ll<bs; ll++) {
1209:           column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1210:         }
1211:       }
1212:       cnt += bs;
1213:     }
1214:     cnt += (bs-1)*rlen;
1215:   }
1216:   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);

1218:   /* store the column values to the file */
1219:   PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1220:   if (!rank) {
1221:     MPI_Status status;
1222:     PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);
1223:     for (i=1; i<size; i++) {
1224:       PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1225:       MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1226:       MPI_Recv(column_values,cnt,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat),&status);
1227:       PetscBinaryWrite(fd,column_values,cnt,PETSC_SCALAR,PETSC_TRUE);
1228:     }
1229:     PetscViewerFlowControlEndMaster(viewer,&message_count);
1230:   } else {
1231:     PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1232:     MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1233:     MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));
1234:     PetscViewerFlowControlEndWorker(viewer,&message_count);
1235:   }
1236:   PetscFree(column_values);

1238:   PetscViewerBinaryGetInfoPointer(viewer,&file);
1239:   if (file) {
1240:     fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs);
1241:   }
1242:   return(0);
1243: }

1247: PetscErrorCode MatView_MPIBAIJ(Mat mat,PetscViewer viewer)
1248: {
1250:   PetscBool      iascii,isdraw,issocket,isbinary;

1253:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1254:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1255:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
1256:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1257:   if (iascii || isdraw || issocket) {
1258:     MatView_MPIBAIJ_ASCIIorDraworSocket(mat,viewer);
1259:   } else if (isbinary) {
1260:     MatView_MPIBAIJ_Binary(mat,viewer);
1261:   }
1262:   return(0);
1263: }

1267: PetscErrorCode MatDestroy_MPIBAIJ(Mat mat)
1268: {
1269:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;

1273: #if defined(PETSC_USE_LOG)
1274:   PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N);
1275: #endif
1276:   MatStashDestroy_Private(&mat->stash);
1277:   MatStashDestroy_Private(&mat->bstash);
1278:   MatDestroy(&baij->A);
1279:   MatDestroy(&baij->B);
1280: #if defined(PETSC_USE_CTABLE)
1281:   PetscTableDestroy(&baij->colmap);
1282: #else
1283:   PetscFree(baij->colmap);
1284: #endif
1285:   PetscFree(baij->garray);
1286:   VecDestroy(&baij->lvec);
1287:   VecScatterDestroy(&baij->Mvctx);
1288:   PetscFree2(baij->rowvalues,baij->rowindices);
1289:   PetscFree(baij->barray);
1290:   PetscFree2(baij->hd,baij->ht);
1291:   PetscFree(baij->rangebs);
1292:   PetscFree(mat->data);

1294:   PetscObjectChangeTypeName((PetscObject)mat,0);
1295:   PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);
1296:   PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);
1297:   PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C",NULL);
1298:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocation_C",NULL);
1299:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocationCSR_C",NULL);
1300:   PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);
1301:   PetscObjectComposeFunction((PetscObject)mat,"MatSetHashTableFactor_C",NULL);
1302:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpisbaij_C",NULL);
1303:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpibstrm_C",NULL);
1304:   return(0);
1305: }

1309: PetscErrorCode MatMult_MPIBAIJ(Mat A,Vec xx,Vec yy)
1310: {
1311:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1313:   PetscInt       nt;

1316:   VecGetLocalSize(xx,&nt);
1317:   if (nt != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");
1318:   VecGetLocalSize(yy,&nt);
1319:   if (nt != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");
1320:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1321:   (*a->A->ops->mult)(a->A,xx,yy);
1322:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1323:   (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
1324:   return(0);
1325: }

1329: PetscErrorCode MatMultAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1330: {
1331:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

1335:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1336:   (*a->A->ops->multadd)(a->A,xx,yy,zz);
1337:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1338:   (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
1339:   return(0);
1340: }

1344: PetscErrorCode MatMultTranspose_MPIBAIJ(Mat A,Vec xx,Vec yy)
1345: {
1346:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1348:   PetscBool      merged;

1351:   VecScatterGetMerged(a->Mvctx,&merged);
1352:   /* do nondiagonal part */
1353:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1354:   if (!merged) {
1355:     /* send it on its way */
1356:     VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1357:     /* do local part */
1358:     (*a->A->ops->multtranspose)(a->A,xx,yy);
1359:     /* receive remote parts: note this assumes the values are not actually */
1360:     /* inserted in yy until the next line */
1361:     VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1362:   } else {
1363:     /* do local part */
1364:     (*a->A->ops->multtranspose)(a->A,xx,yy);
1365:     /* send it on its way */
1366:     VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1367:     /* values actually were received in the Begin() but we need to call this nop */
1368:     VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1369:   }
1370:   return(0);
1371: }

1375: PetscErrorCode MatMultTransposeAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1376: {
1377:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

1381:   /* do nondiagonal part */
1382:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1383:   /* send it on its way */
1384:   VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1385:   /* do local part */
1386:   (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);
1387:   /* receive remote parts: note this assumes the values are not actually */
1388:   /* inserted in yy until the next line, which is true for my implementation*/
1389:   /* but is not perhaps always true. */
1390:   VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1391:   return(0);
1392: }

1394: /*
1395:   This only works correctly for square matrices where the subblock A->A is the
1396:    diagonal block
1397: */
1400: PetscErrorCode MatGetDiagonal_MPIBAIJ(Mat A,Vec v)
1401: {
1402:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

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

1413: PetscErrorCode MatScale_MPIBAIJ(Mat A,PetscScalar aa)
1414: {
1415:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

1419:   MatScale(a->A,aa);
1420:   MatScale(a->B,aa);
1421:   return(0);
1422: }

1426: PetscErrorCode MatGetRow_MPIBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1427: {
1428:   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
1429:   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
1431:   PetscInt       bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB;
1432:   PetscInt       nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend;
1433:   PetscInt       *cmap,*idx_p,cstart = mat->cstartbs;

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

1440:   if (!mat->rowvalues && (idx || v)) {
1441:     /*
1442:         allocate enough space to hold information from the longest row.
1443:     */
1444:     Mat_SeqBAIJ *Aa = (Mat_SeqBAIJ*)mat->A->data,*Ba = (Mat_SeqBAIJ*)mat->B->data;
1445:     PetscInt    max = 1,mbs = mat->mbs,tmp;
1446:     for (i=0; i<mbs; i++) {
1447:       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1448:       if (max < tmp) max = tmp;
1449:     }
1450:     PetscMalloc2(max*bs2,&mat->rowvalues,max*bs2,&mat->rowindices);
1451:   }
1452:   lrow = row - brstart;

1454:   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1455:   if (!v)   {pvA = 0; pvB = 0;}
1456:   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1457:   (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1458:   (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1459:   nztot = nzA + nzB;

1461:   cmap = mat->garray;
1462:   if (v  || idx) {
1463:     if (nztot) {
1464:       /* Sort by increasing column numbers, assuming A and B already sorted */
1465:       PetscInt imark = -1;
1466:       if (v) {
1467:         *v = v_p = mat->rowvalues;
1468:         for (i=0; i<nzB; i++) {
1469:           if (cmap[cworkB[i]/bs] < cstart) v_p[i] = vworkB[i];
1470:           else break;
1471:         }
1472:         imark = i;
1473:         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1474:         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1475:       }
1476:       if (idx) {
1477:         *idx = idx_p = mat->rowindices;
1478:         if (imark > -1) {
1479:           for (i=0; i<imark; i++) {
1480:             idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1481:           }
1482:         } else {
1483:           for (i=0; i<nzB; i++) {
1484:             if (cmap[cworkB[i]/bs] < cstart) idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1485:             else break;
1486:           }
1487:           imark = i;
1488:         }
1489:         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart*bs + cworkA[i];
1490:         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1491:       }
1492:     } else {
1493:       if (idx) *idx = 0;
1494:       if (v)   *v   = 0;
1495:     }
1496:   }
1497:   *nz  = nztot;
1498:   (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1499:   (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1500:   return(0);
1501: }

1505: PetscErrorCode MatRestoreRow_MPIBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1506: {
1507:   Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;

1510:   if (!baij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
1511:   baij->getrowactive = PETSC_FALSE;
1512:   return(0);
1513: }

1517: PetscErrorCode MatZeroEntries_MPIBAIJ(Mat A)
1518: {
1519:   Mat_MPIBAIJ    *l = (Mat_MPIBAIJ*)A->data;

1523:   MatZeroEntries(l->A);
1524:   MatZeroEntries(l->B);
1525:   return(0);
1526: }

1530: PetscErrorCode MatGetInfo_MPIBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1531: {
1532:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)matin->data;
1533:   Mat            A  = a->A,B = a->B;
1535:   PetscReal      isend[5],irecv[5];

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

1540:   MatGetInfo(A,MAT_LOCAL,info);

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

1545:   MatGetInfo(B,MAT_LOCAL,info);

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

1550:   if (flag == MAT_LOCAL) {
1551:     info->nz_used      = isend[0];
1552:     info->nz_allocated = isend[1];
1553:     info->nz_unneeded  = isend[2];
1554:     info->memory       = isend[3];
1555:     info->mallocs      = isend[4];
1556:   } else if (flag == MAT_GLOBAL_MAX) {
1557:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));

1559:     info->nz_used      = irecv[0];
1560:     info->nz_allocated = irecv[1];
1561:     info->nz_unneeded  = irecv[2];
1562:     info->memory       = irecv[3];
1563:     info->mallocs      = irecv[4];
1564:   } else if (flag == MAT_GLOBAL_SUM) {
1565:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));

1567:     info->nz_used      = irecv[0];
1568:     info->nz_allocated = irecv[1];
1569:     info->nz_unneeded  = irecv[2];
1570:     info->memory       = irecv[3];
1571:     info->mallocs      = irecv[4];
1572:   } else SETERRQ1(PetscObjectComm((PetscObject)matin),PETSC_ERR_ARG_WRONG,"Unknown MatInfoType argument %d",(int)flag);
1573:   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
1574:   info->fill_ratio_needed = 0;
1575:   info->factor_mallocs    = 0;
1576:   return(0);
1577: }

1581: PetscErrorCode MatSetOption_MPIBAIJ(Mat A,MatOption op,PetscBool flg)
1582: {
1583:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

1587:   switch (op) {
1588:   case MAT_NEW_NONZERO_LOCATIONS:
1589:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1590:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1591:   case MAT_KEEP_NONZERO_PATTERN:
1592:   case MAT_NEW_NONZERO_LOCATION_ERR:
1593:     MatSetOption(a->A,op,flg);
1594:     MatSetOption(a->B,op,flg);
1595:     break;
1596:   case MAT_ROW_ORIENTED:
1597:     a->roworiented = flg;

1599:     MatSetOption(a->A,op,flg);
1600:     MatSetOption(a->B,op,flg);
1601:     break;
1602:   case MAT_NEW_DIAGONALS:
1603:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1604:     break;
1605:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1606:     a->donotstash = flg;
1607:     break;
1608:   case MAT_USE_HASH_TABLE:
1609:     a->ht_flag = flg;
1610:     break;
1611:   case MAT_SYMMETRIC:
1612:   case MAT_STRUCTURALLY_SYMMETRIC:
1613:   case MAT_HERMITIAN:
1614:   case MAT_SYMMETRY_ETERNAL:
1615:     MatSetOption(a->A,op,flg);
1616:     break;
1617:   default:
1618:     SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"unknown option %d",op);
1619:   }
1620:   return(0);
1621: }

1625: PetscErrorCode MatTranspose_MPIBAIJ(Mat A,MatReuse reuse,Mat *matout)
1626: {
1627:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)A->data;
1628:   Mat_SeqBAIJ    *Aloc;
1629:   Mat            B;
1631:   PetscInt       M =A->rmap->N,N=A->cmap->N,*ai,*aj,i,*rvals,j,k,col;
1632:   PetscInt       bs=A->rmap->bs,mbs=baij->mbs;
1633:   MatScalar      *a;

1636:   if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1637:   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
1638:     MatCreate(PetscObjectComm((PetscObject)A),&B);
1639:     MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);
1640:     MatSetType(B,((PetscObject)A)->type_name);
1641:     /* Do not know preallocation information, but must set block size */
1642:     MatMPIBAIJSetPreallocation(B,A->rmap->bs,PETSC_DECIDE,NULL,PETSC_DECIDE,NULL);
1643:   } else {
1644:     B = *matout;
1645:   }

1647:   /* copy over the A part */
1648:   Aloc = (Mat_SeqBAIJ*)baij->A->data;
1649:   ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1650:   PetscMalloc1(bs,&rvals);

1652:   for (i=0; i<mbs; i++) {
1653:     rvals[0] = bs*(baij->rstartbs + i);
1654:     for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1655:     for (j=ai[i]; j<ai[i+1]; j++) {
1656:       col = (baij->cstartbs+aj[j])*bs;
1657:       for (k=0; k<bs; k++) {
1658:         MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);

1660:         col++; a += bs;
1661:       }
1662:     }
1663:   }
1664:   /* copy over the B part */
1665:   Aloc = (Mat_SeqBAIJ*)baij->B->data;
1666:   ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1667:   for (i=0; i<mbs; i++) {
1668:     rvals[0] = bs*(baij->rstartbs + i);
1669:     for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1670:     for (j=ai[i]; j<ai[i+1]; j++) {
1671:       col = baij->garray[aj[j]]*bs;
1672:       for (k=0; k<bs; k++) {
1673:         MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);
1674:         col++;
1675:         a += bs;
1676:       }
1677:     }
1678:   }
1679:   PetscFree(rvals);
1680:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
1681:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

1683:   if (reuse == MAT_INITIAL_MATRIX || *matout != A) *matout = B;
1684:   else {
1685:     MatHeaderMerge(A,B);
1686:   }
1687:   return(0);
1688: }

1692: PetscErrorCode MatDiagonalScale_MPIBAIJ(Mat mat,Vec ll,Vec rr)
1693: {
1694:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
1695:   Mat            a     = baij->A,b = baij->B;
1697:   PetscInt       s1,s2,s3;

1700:   MatGetLocalSize(mat,&s2,&s3);
1701:   if (rr) {
1702:     VecGetLocalSize(rr,&s1);
1703:     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
1704:     /* Overlap communication with computation. */
1705:     VecScatterBegin(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);
1706:   }
1707:   if (ll) {
1708:     VecGetLocalSize(ll,&s1);
1709:     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1710:     (*b->ops->diagonalscale)(b,ll,NULL);
1711:   }
1712:   /* scale  the diagonal block */
1713:   (*a->ops->diagonalscale)(a,ll,rr);

1715:   if (rr) {
1716:     /* Do a scatter end and then right scale the off-diagonal block */
1717:     VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);
1718:     (*b->ops->diagonalscale)(b,NULL,baij->lvec);
1719:   }
1720:   return(0);
1721: }

1725: PetscErrorCode MatZeroRows_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1726: {
1727:   Mat_MPIBAIJ   *l      = (Mat_MPIBAIJ *) A->data;
1728:   PetscInt      *owners = A->rmap->range;
1729:   PetscInt       n      = A->rmap->n;
1730:   PetscSF        sf;
1731:   PetscInt      *lrows;
1732:   PetscSFNode   *rrows;
1733:   PetscInt       r, p = 0, len = 0;

1737:   /* Create SF where leaves are input rows and roots are owned rows */
1738:   PetscMalloc1(n, &lrows);
1739:   for (r = 0; r < n; ++r) lrows[r] = -1;
1740:   if (!A->nooffproczerorows) {PetscMalloc1(N, &rrows);}
1741:   for (r = 0; r < N; ++r) {
1742:     const PetscInt idx   = rows[r];
1743:     if (idx < 0 || A->rmap->N <= idx) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range [0,%D)",idx,A->rmap->N);
1744:     if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
1745:       PetscLayoutFindOwner(A->rmap,idx,&p);
1746:     }
1747:     if (A->nooffproczerorows) {
1748:       if (p != l->rank) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"MAT_NO_OFF_PROC_ZERO_ROWS set, but row %D is not owned by rank %d",idx,l->rank);
1749:       lrows[len++] = idx - owners[p];
1750:     } else {
1751:       rrows[r].rank = p;
1752:       rrows[r].index = rows[r] - owners[p];
1753:     }
1754:   }
1755:   if (!A->nooffproczerorows) {
1756:     PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);
1757:     PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);
1758:     /* Collect flags for rows to be zeroed */
1759:     PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1760:     PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1761:     PetscSFDestroy(&sf);
1762:     /* Compress and put in row numbers */
1763:     for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
1764:   }
1765:   /* fix right hand side if needed */
1766:   if (x && b) {
1767:     const PetscScalar *xx;
1768:     PetscScalar       *bb;

1770:     VecGetArrayRead(x,&xx);
1771:     VecGetArray(b,&bb);
1772:     for (r = 0; r < len; ++r) bb[lrows[r]] = diag*xx[lrows[r]];
1773:     VecRestoreArrayRead(x,&xx);
1774:     VecRestoreArray(b,&bb);
1775:   }

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

1784:   */
1785:   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
1786:   MatZeroRows_SeqBAIJ(l->B,len,lrows,0.0,NULL,NULL);
1787:   if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) {
1788:     MatZeroRows_SeqBAIJ(l->A,len,lrows,diag,NULL,NULL);
1789:   } else if (diag != 0.0) {
1790:     MatZeroRows_SeqBAIJ(l->A,len,lrows,0.0,0,0);
1791:     if (((Mat_SeqBAIJ*)l->A->data)->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options \n\
1792:        MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
1793:     for (r = 0; r < len; ++r) {
1794:       const PetscInt row = lrows[r] + A->rmap->rstart;
1795:       MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);
1796:     }
1797:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1798:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1799:   } else {
1800:     MatZeroRows_SeqBAIJ(l->A,len,lrows,0.0,NULL,NULL);
1801:   }
1802:   PetscFree(lrows);

1804:   /* only change matrix nonzero state if pattern was allowed to be changed */
1805:   if (!((Mat_SeqBAIJ*)(l->A->data))->keepnonzeropattern) {
1806:     PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
1807:     MPI_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
1808:   }
1809:   return(0);
1810: }

1814: PetscErrorCode MatZeroRowsColumns_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1815: {
1816:   Mat_MPIBAIJ       *l = (Mat_MPIBAIJ*)A->data;
1817:   PetscErrorCode    ierr;
1818:   PetscMPIInt       n = A->rmap->n;
1819:   PetscInt          i,j,k,r,p = 0,len = 0,row,col,count;
1820:   PetscInt          *lrows,*owners = A->rmap->range;
1821:   PetscSFNode       *rrows;
1822:   PetscSF           sf;
1823:   const PetscScalar *xx;
1824:   PetscScalar       *bb,*mask;
1825:   Vec               xmask,lmask;
1826:   Mat_SeqBAIJ       *baij = (Mat_SeqBAIJ*)l->B->data;
1827:   PetscInt           bs = A->rmap->bs, bs2 = baij->bs2;
1828:   PetscScalar       *aa;

1831:   /* Create SF where leaves are input rows and roots are owned rows */
1832:   PetscMalloc1(n, &lrows);
1833:   for (r = 0; r < n; ++r) lrows[r] = -1;
1834:   PetscMalloc1(N, &rrows);
1835:   for (r = 0; r < N; ++r) {
1836:     const PetscInt idx   = rows[r];
1837:     if (idx < 0 || A->rmap->N <= idx) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range [0,%D)",idx,A->rmap->N);
1838:     if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
1839:       PetscLayoutFindOwner(A->rmap,idx,&p);
1840:     }
1841:     rrows[r].rank  = p;
1842:     rrows[r].index = rows[r] - owners[p];
1843:   }
1844:   PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);
1845:   PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);
1846:   /* Collect flags for rows to be zeroed */
1847:   PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1848:   PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1849:   PetscSFDestroy(&sf);
1850:   /* Compress and put in row numbers */
1851:   for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
1852:   /* zero diagonal part of matrix */
1853:   MatZeroRowsColumns(l->A,len,lrows,diag,x,b);
1854:   /* handle off diagonal part of matrix */
1855:   MatGetVecs(A,&xmask,NULL);
1856:   VecDuplicate(l->lvec,&lmask);
1857:   VecGetArray(xmask,&bb);
1858:   for (i=0; i<len; i++) bb[lrows[i]] = 1;
1859:   VecRestoreArray(xmask,&bb);
1860:   VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
1861:   VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
1862:   VecDestroy(&xmask);
1863:   if (x) {
1864:     VecScatterBegin(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
1865:     VecScatterEnd(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
1866:     VecGetArrayRead(l->lvec,&xx);
1867:     VecGetArray(b,&bb);
1868:   }
1869:   VecGetArray(lmask,&mask);
1870:   /* remove zeroed rows of off diagonal matrix */
1871:   for (i = 0; i < len; ++i) {
1872:     row   = lrows[i];
1873:     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
1874:     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
1875:     for (k = 0; k < count; ++k) {
1876:       aa[0] = 0.0;
1877:       aa   += bs;
1878:     }
1879:   }
1880:   /* loop over all elements of off process part of matrix zeroing removed columns*/
1881:   for (i = 0; i < l->B->rmap->N; ++i) {
1882:     row = i/bs;
1883:     for (j = baij->i[row]; j < baij->i[row+1]; ++j) {
1884:       for (k = 0; k < bs; ++k) {
1885:         col = bs*baij->j[j] + k;
1886:         if (PetscAbsScalar(mask[col])) {
1887:           aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
1888:           if (b) bb[i] -= aa[0]*xx[col];
1889:           aa[0] = 0.0;
1890:         }
1891:       }
1892:     }
1893:   }
1894:   if (x) {
1895:     VecRestoreArray(b,&bb);
1896:     VecRestoreArrayRead(l->lvec,&xx);
1897:   }
1898:   VecRestoreArray(lmask,&mask);
1899:   VecDestroy(&lmask);
1900:   PetscFree(lrows);

1902:   /* only change matrix nonzero state if pattern was allowed to be changed */
1903:   if (!((Mat_SeqBAIJ*)(l->A->data))->keepnonzeropattern) {
1904:     PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
1905:     MPI_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
1906:   }
1907:   return(0);
1908: }

1912: PetscErrorCode MatSetUnfactored_MPIBAIJ(Mat A)
1913: {
1914:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

1918:   MatSetUnfactored(a->A);
1919:   return(0);
1920: }

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

1926: PetscErrorCode MatEqual_MPIBAIJ(Mat A,Mat B,PetscBool  *flag)
1927: {
1928:   Mat_MPIBAIJ    *matB = (Mat_MPIBAIJ*)B->data,*matA = (Mat_MPIBAIJ*)A->data;
1929:   Mat            a,b,c,d;
1930:   PetscBool      flg;

1934:   a = matA->A; b = matA->B;
1935:   c = matB->A; d = matB->B;

1937:   MatEqual(a,c,&flg);
1938:   if (flg) {
1939:     MatEqual(b,d,&flg);
1940:   }
1941:   MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
1942:   return(0);
1943: }

1947: PetscErrorCode MatCopy_MPIBAIJ(Mat A,Mat B,MatStructure str)
1948: {
1950:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1951:   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ*)B->data;

1954:   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1955:   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1956:     MatCopy_Basic(A,B,str);
1957:   } else {
1958:     MatCopy(a->A,b->A,str);
1959:     MatCopy(a->B,b->B,str);
1960:   }
1961:   return(0);
1962: }

1966: PetscErrorCode MatSetUp_MPIBAIJ(Mat A)
1967: {

1971:   MatMPIBAIJSetPreallocation(A,A->rmap->bs,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
1972:   return(0);
1973: }

1977: PetscErrorCode MatAXPYGetPreallocation_MPIBAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz)
1978: {
1980:   PetscInt       bs = Y->rmap->bs,m = Y->rmap->N/bs;
1981:   Mat_SeqBAIJ    *x = (Mat_SeqBAIJ*)X->data;
1982:   Mat_SeqBAIJ    *y = (Mat_SeqBAIJ*)Y->data;

1985:   MatAXPYGetPreallocation_MPIX_private(m,x->i,x->j,xltog,y->i,y->j,yltog,nnz);
1986:   return(0);
1987: }

1991: PetscErrorCode MatAXPY_MPIBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
1992: {
1994:   Mat_MPIBAIJ    *xx=(Mat_MPIBAIJ*)X->data,*yy=(Mat_MPIBAIJ*)Y->data;
1995:   PetscBLASInt   bnz,one=1;
1996:   Mat_SeqBAIJ    *x,*y;

1999:   if (str == SAME_NONZERO_PATTERN) {
2000:     PetscScalar alpha = a;
2001:     x    = (Mat_SeqBAIJ*)xx->A->data;
2002:     y    = (Mat_SeqBAIJ*)yy->A->data;
2003:     PetscBLASIntCast(x->nz,&bnz);
2004:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2005:     x    = (Mat_SeqBAIJ*)xx->B->data;
2006:     y    = (Mat_SeqBAIJ*)yy->B->data;
2007:     PetscBLASIntCast(x->nz,&bnz);
2008:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2009:     PetscObjectStateIncrease((PetscObject)Y);
2010:   } else {
2011:     Mat      B;
2012:     PetscInt *nnz_d,*nnz_o,bs=Y->rmap->bs;
2013:     PetscMalloc1(yy->A->rmap->N,&nnz_d);
2014:     PetscMalloc1(yy->B->rmap->N,&nnz_o);
2015:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
2016:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2017:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2018:     MatSetBlockSizesFromMats(B,Y,Y);
2019:     MatSetType(B,MATMPIBAIJ);
2020:     MatAXPYGetPreallocation_SeqBAIJ(yy->A,xx->A,nnz_d);
2021:     MatAXPYGetPreallocation_MPIBAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);
2022:     MatMPIBAIJSetPreallocation(B,bs,0,nnz_d,0,nnz_o);
2023:     /* MatAXPY_BasicWithPreallocation() for BAIJ matrix is much slower than AIJ, even for bs=1 ! */
2024:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2025:     MatHeaderReplace(Y,B);
2026:     PetscFree(nnz_d);
2027:     PetscFree(nnz_o);
2028:   }
2029:   return(0);
2030: }

2034: PetscErrorCode MatRealPart_MPIBAIJ(Mat A)
2035: {
2036:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

2040:   MatRealPart(a->A);
2041:   MatRealPart(a->B);
2042:   return(0);
2043: }

2047: PetscErrorCode MatImaginaryPart_MPIBAIJ(Mat A)
2048: {
2049:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;

2053:   MatImaginaryPart(a->A);
2054:   MatImaginaryPart(a->B);
2055:   return(0);
2056: }

2060: PetscErrorCode MatGetSubMatrix_MPIBAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
2061: {
2063:   IS             iscol_local;
2064:   PetscInt       csize;

2067:   ISGetLocalSize(iscol,&csize);
2068:   if (call == MAT_REUSE_MATRIX) {
2069:     PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);
2070:     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2071:   } else {
2072:     ISAllGather(iscol,&iscol_local);
2073:   }
2074:   MatGetSubMatrix_MPIBAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);
2075:   if (call == MAT_INITIAL_MATRIX) {
2076:     PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);
2077:     ISDestroy(&iscol_local);
2078:   }
2079:   return(0);
2080: }
2081: extern PetscErrorCode MatGetSubMatrices_MPIBAIJ_local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,PetscBool*,Mat*);
2084: /*
2085:   Not great since it makes two copies of the submatrix, first an SeqBAIJ
2086:   in local and then by concatenating the local matrices the end result.
2087:   Writing it directly would be much like MatGetSubMatrices_MPIBAIJ()
2088: */
2089: PetscErrorCode MatGetSubMatrix_MPIBAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
2090: {
2092:   PetscMPIInt    rank,size;
2093:   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs;
2094:   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol,nrow;
2095:   Mat            M,Mreuse;
2096:   MatScalar      *vwork,*aa;
2097:   MPI_Comm       comm;
2098:   IS             isrow_new, iscol_new;
2099:   PetscBool      idflag,allrows, allcols;
2100:   Mat_SeqBAIJ    *aij;

2103:   PetscObjectGetComm((PetscObject)mat,&comm);
2104:   MPI_Comm_rank(comm,&rank);
2105:   MPI_Comm_size(comm,&size);
2106:   /* The compression and expansion should be avoided. Doesn't point
2107:      out errors, might change the indices, hence buggey */
2108:   ISCompressIndicesGeneral(mat->rmap->N,mat->rmap->n,mat->rmap->bs,1,&isrow,&isrow_new);
2109:   ISCompressIndicesGeneral(mat->cmap->N,mat->cmap->n,mat->cmap->bs,1,&iscol,&iscol_new);

2111:   /* Check for special case: each processor gets entire matrix columns */
2112:   ISIdentity(iscol,&idflag);
2113:   ISGetLocalSize(iscol,&ncol);
2114:   if (idflag && ncol == mat->cmap->N) allcols = PETSC_TRUE;
2115:   else allcols = PETSC_FALSE;

2117:   ISIdentity(isrow,&idflag);
2118:   ISGetLocalSize(isrow,&nrow);
2119:   if (idflag && nrow == mat->rmap->N) allrows = PETSC_TRUE;
2120:   else allrows = PETSC_FALSE;

2122:   if (call ==  MAT_REUSE_MATRIX) {
2123:     PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);
2124:     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2125:     MatGetSubMatrices_MPIBAIJ_local(mat,1,&isrow_new,&iscol_new,MAT_REUSE_MATRIX,&allrows,&allcols,&Mreuse);
2126:   } else {
2127:     MatGetSubMatrices_MPIBAIJ_local(mat,1,&isrow_new,&iscol_new,MAT_INITIAL_MATRIX,&allrows,&allcols,&Mreuse);
2128:   }
2129:   ISDestroy(&isrow_new);
2130:   ISDestroy(&iscol_new);
2131:   /*
2132:       m - number of local rows
2133:       n - number of columns (same on all processors)
2134:       rstart - first row in new global matrix generated
2135:   */
2136:   MatGetBlockSize(mat,&bs);
2137:   MatGetSize(Mreuse,&m,&n);
2138:   m    = m/bs;
2139:   n    = n/bs;

2141:   if (call == MAT_INITIAL_MATRIX) {
2142:     aij = (Mat_SeqBAIJ*)(Mreuse)->data;
2143:     ii  = aij->i;
2144:     jj  = aij->j;

2146:     /*
2147:         Determine the number of non-zeros in the diagonal and off-diagonal
2148:         portions of the matrix in order to do correct preallocation
2149:     */

2151:     /* first get start and end of "diagonal" columns */
2152:     if (csize == PETSC_DECIDE) {
2153:       ISGetSize(isrow,&mglobal);
2154:       if (mglobal == n*bs) { /* square matrix */
2155:         nlocal = m;
2156:       } else {
2157:         nlocal = n/size + ((n % size) > rank);
2158:       }
2159:     } else {
2160:       nlocal = csize/bs;
2161:     }
2162:     MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
2163:     rstart = rend - nlocal;
2164:     if (rank == size - 1 && rend != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);

2166:     /* next, compute all the lengths */
2167:     PetscMalloc2(m+1,&dlens,m+1,&olens);
2168:     for (i=0; i<m; i++) {
2169:       jend = ii[i+1] - ii[i];
2170:       olen = 0;
2171:       dlen = 0;
2172:       for (j=0; j<jend; j++) {
2173:         if (*jj < rstart || *jj >= rend) olen++;
2174:         else dlen++;
2175:         jj++;
2176:       }
2177:       olens[i] = olen;
2178:       dlens[i] = dlen;
2179:     }
2180:     MatCreate(comm,&M);
2181:     MatSetSizes(M,bs*m,bs*nlocal,PETSC_DECIDE,bs*n);
2182:     MatSetType(M,((PetscObject)mat)->type_name);
2183:     MatMPIBAIJSetPreallocation(M,bs,0,dlens,0,olens);
2184:     PetscFree2(dlens,olens);
2185:   } else {
2186:     PetscInt ml,nl;

2188:     M    = *newmat;
2189:     MatGetLocalSize(M,&ml,&nl);
2190:     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2191:     MatZeroEntries(M);
2192:     /*
2193:          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2194:        rather than the slower MatSetValues().
2195:     */
2196:     M->was_assembled = PETSC_TRUE;
2197:     M->assembled     = PETSC_FALSE;
2198:   }
2199:   MatSetOption(M,MAT_ROW_ORIENTED,PETSC_FALSE);
2200:   MatGetOwnershipRange(M,&rstart,&rend);
2201:   aij  = (Mat_SeqBAIJ*)(Mreuse)->data;
2202:   ii   = aij->i;
2203:   jj   = aij->j;
2204:   aa   = aij->a;
2205:   for (i=0; i<m; i++) {
2206:     row   = rstart/bs + i;
2207:     nz    = ii[i+1] - ii[i];
2208:     cwork = jj;     jj += nz;
2209:     vwork = aa;     aa += nz*bs*bs;
2210:     MatSetValuesBlocked_MPIBAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);
2211:   }

2213:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
2214:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
2215:   *newmat = M;

2217:   /* save submatrix used in processor for next request */
2218:   if (call ==  MAT_INITIAL_MATRIX) {
2219:     PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);
2220:     PetscObjectDereference((PetscObject)Mreuse);
2221:   }
2222:   return(0);
2223: }

2227: PetscErrorCode MatPermute_MPIBAIJ(Mat A,IS rowp,IS colp,Mat *B)
2228: {
2229:   MPI_Comm       comm,pcomm;
2230:   PetscInt       clocal_size,nrows;
2231:   const PetscInt *rows;
2232:   PetscMPIInt    size;
2233:   IS             crowp,lcolp;

2237:   PetscObjectGetComm((PetscObject)A,&comm);
2238:   /* make a collective version of 'rowp' */
2239:   PetscObjectGetComm((PetscObject)rowp,&pcomm);
2240:   if (pcomm==comm) {
2241:     crowp = rowp;
2242:   } else {
2243:     ISGetSize(rowp,&nrows);
2244:     ISGetIndices(rowp,&rows);
2245:     ISCreateGeneral(comm,nrows,rows,PETSC_COPY_VALUES,&crowp);
2246:     ISRestoreIndices(rowp,&rows);
2247:   }
2248:   ISSetPermutation(crowp);
2249:   /* make a local version of 'colp' */
2250:   PetscObjectGetComm((PetscObject)colp,&pcomm);
2251:   MPI_Comm_size(pcomm,&size);
2252:   if (size==1) {
2253:     lcolp = colp;
2254:   } else {
2255:     ISAllGather(colp,&lcolp);
2256:   }
2257:   ISSetPermutation(lcolp);
2258:   /* now we just get the submatrix */
2259:   MatGetLocalSize(A,NULL,&clocal_size);
2260:   MatGetSubMatrix_MPIBAIJ_Private(A,crowp,lcolp,clocal_size,MAT_INITIAL_MATRIX,B);
2261:   /* clean up */
2262:   if (pcomm!=comm) {
2263:     ISDestroy(&crowp);
2264:   }
2265:   if (size>1) {
2266:     ISDestroy(&lcolp);
2267:   }
2268:   return(0);
2269: }

2273: PetscErrorCode  MatGetGhosts_MPIBAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
2274: {
2275:   Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*) mat->data;
2276:   Mat_SeqBAIJ *B    = (Mat_SeqBAIJ*)baij->B->data;

2279:   if (nghosts) *nghosts = B->nbs;
2280:   if (ghosts) *ghosts = baij->garray;
2281:   return(0);
2282: }

2286: PetscErrorCode MatGetSeqNonzeroStructure_MPIBAIJ(Mat A,Mat *newmat)
2287: {
2288:   Mat            B;
2289:   Mat_MPIBAIJ    *a  = (Mat_MPIBAIJ*)A->data;
2290:   Mat_SeqBAIJ    *ad = (Mat_SeqBAIJ*)a->A->data,*bd = (Mat_SeqBAIJ*)a->B->data;
2291:   Mat_SeqAIJ     *b;
2293:   PetscMPIInt    size,rank,*recvcounts = 0,*displs = 0;
2294:   PetscInt       sendcount,i,*rstarts = A->rmap->range,n,cnt,j,bs = A->rmap->bs;
2295:   PetscInt       m,*garray = a->garray,*lens,*jsendbuf,*a_jsendbuf,*b_jsendbuf;

2298:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
2299:   MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);

2301:   /* ----------------------------------------------------------------
2302:      Tell every processor the number of nonzeros per row
2303:   */
2304:   PetscMalloc1((A->rmap->N/bs),&lens);
2305:   for (i=A->rmap->rstart/bs; i<A->rmap->rend/bs; i++) {
2306:     lens[i] = ad->i[i-A->rmap->rstart/bs+1] - ad->i[i-A->rmap->rstart/bs] + bd->i[i-A->rmap->rstart/bs+1] - bd->i[i-A->rmap->rstart/bs];
2307:   }
2308:   sendcount = A->rmap->rend/bs - A->rmap->rstart/bs;
2309:   PetscMalloc1(2*size,&recvcounts);
2310:   displs    = recvcounts + size;
2311:   for (i=0; i<size; i++) {
2312:     recvcounts[i] = A->rmap->range[i+1]/bs - A->rmap->range[i]/bs;
2313:     displs[i]     = A->rmap->range[i]/bs;
2314:   }
2315: #if defined(PETSC_HAVE_MPI_IN_PLACE)
2316:   MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,lens,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2317: #else
2318:   MPI_Allgatherv(lens+A->rmap->rstart/bs,sendcount,MPIU_INT,lens,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2319: #endif
2320:   /* ---------------------------------------------------------------
2321:      Create the sequential matrix of the same type as the local block diagonal
2322:   */
2323:   MatCreate(PETSC_COMM_SELF,&B);
2324:   MatSetSizes(B,A->rmap->N/bs,A->cmap->N/bs,PETSC_DETERMINE,PETSC_DETERMINE);
2325:   MatSetType(B,MATSEQAIJ);
2326:   MatSeqAIJSetPreallocation(B,0,lens);
2327:   b    = (Mat_SeqAIJ*)B->data;

2329:   /*--------------------------------------------------------------------
2330:     Copy my part of matrix column indices over
2331:   */
2332:   sendcount  = ad->nz + bd->nz;
2333:   jsendbuf   = b->j + b->i[rstarts[rank]/bs];
2334:   a_jsendbuf = ad->j;
2335:   b_jsendbuf = bd->j;
2336:   n          = A->rmap->rend/bs - A->rmap->rstart/bs;
2337:   cnt        = 0;
2338:   for (i=0; i<n; i++) {

2340:     /* put in lower diagonal portion */
2341:     m = bd->i[i+1] - bd->i[i];
2342:     while (m > 0) {
2343:       /* is it above diagonal (in bd (compressed) numbering) */
2344:       if (garray[*b_jsendbuf] > A->rmap->rstart/bs + i) break;
2345:       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2346:       m--;
2347:     }

2349:     /* put in diagonal portion */
2350:     for (j=ad->i[i]; j<ad->i[i+1]; j++) {
2351:       jsendbuf[cnt++] = A->rmap->rstart/bs + *a_jsendbuf++;
2352:     }

2354:     /* put in upper diagonal portion */
2355:     while (m-- > 0) {
2356:       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2357:     }
2358:   }
2359:   if (cnt != sendcount) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupted PETSc matrix: nz given %D actual nz %D",sendcount,cnt);

2361:   /*--------------------------------------------------------------------
2362:     Gather all column indices to all processors
2363:   */
2364:   for (i=0; i<size; i++) {
2365:     recvcounts[i] = 0;
2366:     for (j=A->rmap->range[i]/bs; j<A->rmap->range[i+1]/bs; j++) {
2367:       recvcounts[i] += lens[j];
2368:     }
2369:   }
2370:   displs[0] = 0;
2371:   for (i=1; i<size; i++) {
2372:     displs[i] = displs[i-1] + recvcounts[i-1];
2373:   }
2374: #if defined(PETSC_HAVE_MPI_IN_PLACE)
2375:   MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,b->j,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2376: #else
2377:   MPI_Allgatherv(jsendbuf,sendcount,MPIU_INT,b->j,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2378: #endif
2379:   /*--------------------------------------------------------------------
2380:     Assemble the matrix into useable form (note numerical values not yet set)
2381:   */
2382:   /* set the b->ilen (length of each row) values */
2383:   PetscMemcpy(b->ilen,lens,(A->rmap->N/bs)*sizeof(PetscInt));
2384:   /* set the b->i indices */
2385:   b->i[0] = 0;
2386:   for (i=1; i<=A->rmap->N/bs; i++) {
2387:     b->i[i] = b->i[i-1] + lens[i-1];
2388:   }
2389:   PetscFree(lens);
2390:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2391:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2392:   PetscFree(recvcounts);

2394:   if (A->symmetric) {
2395:     MatSetOption(B,MAT_SYMMETRIC,PETSC_TRUE);
2396:   } else if (A->hermitian) {
2397:     MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE);
2398:   } else if (A->structurally_symmetric) {
2399:     MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);
2400:   }
2401:   *newmat = B;
2402:   return(0);
2403: }

2407: PetscErrorCode MatSOR_MPIBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2408: {
2409:   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
2411:   Vec            bb1 = 0;

2414:   if (flag == SOR_APPLY_UPPER) {
2415:     (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2416:     return(0);
2417:   }

2419:   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS) {
2420:     VecDuplicate(bb,&bb1);
2421:   }

2423:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2424:     if (flag & SOR_ZERO_INITIAL_GUESS) {
2425:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2426:       its--;
2427:     }

2429:     while (its--) {
2430:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2431:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

2433:       /* update rhs: bb1 = bb - B*x */
2434:       VecScale(mat->lvec,-1.0);
2435:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

2437:       /* local sweep */
2438:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);
2439:     }
2440:   } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
2441:     if (flag & SOR_ZERO_INITIAL_GUESS) {
2442:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2443:       its--;
2444:     }
2445:     while (its--) {
2446:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2447:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

2449:       /* update rhs: bb1 = bb - B*x */
2450:       VecScale(mat->lvec,-1.0);
2451:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

2453:       /* local sweep */
2454:       (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);
2455:     }
2456:   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
2457:     if (flag & SOR_ZERO_INITIAL_GUESS) {
2458:       (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2459:       its--;
2460:     }
2461:     while (its--) {
2462:       VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2463:       VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);

2465:       /* update rhs: bb1 = bb - B*x */
2466:       VecScale(mat->lvec,-1.0);
2467:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);

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

2474:   VecDestroy(&bb1);
2475:   return(0);
2476: }

2480: PetscErrorCode MatGetColumnNorms_MPIBAIJ(Mat A,NormType type,PetscReal *norms)
2481: {
2483:   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ*)A->data;
2484:   PetscInt       N,i,*garray = aij->garray;
2485:   PetscInt       ib,jb,bs = A->rmap->bs;
2486:   Mat_SeqBAIJ    *a_aij = (Mat_SeqBAIJ*) aij->A->data;
2487:   MatScalar      *a_val = a_aij->a;
2488:   Mat_SeqBAIJ    *b_aij = (Mat_SeqBAIJ*) aij->B->data;
2489:   MatScalar      *b_val = b_aij->a;
2490:   PetscReal      *work;

2493:   MatGetSize(A,NULL,&N);
2494:   PetscCalloc1(N,&work);
2495:   if (type == NORM_2) {
2496:     for (i=a_aij->i[0]; i<a_aij->i[aij->A->rmap->n/bs]; i++) {
2497:       for (jb=0; jb<bs; jb++) {
2498:         for (ib=0; ib<bs; ib++) {
2499:           work[A->cmap->rstart + a_aij->j[i] * bs + jb] += PetscAbsScalar(*a_val * *a_val);
2500:           a_val++;
2501:         }
2502:       }
2503:     }
2504:     for (i=b_aij->i[0]; i<b_aij->i[aij->B->rmap->n/bs]; i++) {
2505:       for (jb=0; jb<bs; jb++) {
2506:         for (ib=0; ib<bs; ib++) {
2507:           work[garray[b_aij->j[i]] * bs + jb] += PetscAbsScalar(*b_val * *b_val);
2508:           b_val++;
2509:         }
2510:       }
2511:     }
2512:   } else if (type == NORM_1) {
2513:     for (i=a_aij->i[0]; i<a_aij->i[aij->A->rmap->n/bs]; i++) {
2514:       for (jb=0; jb<bs; jb++) {
2515:         for (ib=0; ib<bs; ib++) {
2516:           work[A->cmap->rstart + a_aij->j[i] * bs + jb] += PetscAbsScalar(*a_val);
2517:           a_val++;
2518:         }
2519:       }
2520:     }
2521:     for (i=b_aij->i[0]; i<b_aij->i[aij->B->rmap->n/bs]; i++) {
2522:       for (jb=0; jb<bs; jb++) {
2523:        for (ib=0; ib<bs; ib++) {
2524:           work[garray[b_aij->j[i]] * bs + jb] += PetscAbsScalar(*b_val);
2525:           b_val++;
2526:         }
2527:       }
2528:     }
2529:   } else if (type == NORM_INFINITY) {
2530:     for (i=a_aij->i[0]; i<a_aij->i[aij->A->rmap->n/bs]; i++) {
2531:       for (jb=0; jb<bs; jb++) {
2532:         for (ib=0; ib<bs; ib++) {
2533:           int col = A->cmap->rstart + a_aij->j[i] * bs + jb;
2534:           work[col] = PetscMax(PetscAbsScalar(*a_val), work[col]);
2535:           a_val++;
2536:         }
2537:       }
2538:     }
2539:     for (i=b_aij->i[0]; i<b_aij->i[aij->B->rmap->n/bs]; i++) {
2540:       for (jb=0; jb<bs; jb++) {
2541:         for (ib=0; ib<bs; ib++) {
2542:           int col = garray[b_aij->j[i]] * bs + jb;
2543:           work[col] = PetscMax(PetscAbsScalar(*b_val), work[col]);
2544:           b_val++;
2545:         }
2546:       }
2547:     }
2548:   } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown NormType");
2549:   if (type == NORM_INFINITY) {
2550:     MPI_Allreduce(work,norms,N,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));
2551:   } else {
2552:     MPI_Allreduce(work,norms,N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));
2553:   }
2554:   PetscFree(work);
2555:   if (type == NORM_2) {
2556:     for (i=0; i<N; i++) norms[i] = PetscSqrtReal(norms[i]);
2557:   }
2558:   return(0);
2559: }

2563: PetscErrorCode  MatInvertBlockDiagonal_MPIBAIJ(Mat A,const PetscScalar **values)
2564: {
2565:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*) A->data;

2569:   MatInvertBlockDiagonal(a->A,values);
2570:   return(0);
2571: }


2574: /* -------------------------------------------------------------------*/
2575: static struct _MatOps MatOps_Values = {MatSetValues_MPIBAIJ,
2576:                                        MatGetRow_MPIBAIJ,
2577:                                        MatRestoreRow_MPIBAIJ,
2578:                                        MatMult_MPIBAIJ,
2579:                                 /* 4*/ MatMultAdd_MPIBAIJ,
2580:                                        MatMultTranspose_MPIBAIJ,
2581:                                        MatMultTransposeAdd_MPIBAIJ,
2582:                                        0,
2583:                                        0,
2584:                                        0,
2585:                                 /*10*/ 0,
2586:                                        0,
2587:                                        0,
2588:                                        MatSOR_MPIBAIJ,
2589:                                        MatTranspose_MPIBAIJ,
2590:                                 /*15*/ MatGetInfo_MPIBAIJ,
2591:                                        MatEqual_MPIBAIJ,
2592:                                        MatGetDiagonal_MPIBAIJ,
2593:                                        MatDiagonalScale_MPIBAIJ,
2594:                                        MatNorm_MPIBAIJ,
2595:                                 /*20*/ MatAssemblyBegin_MPIBAIJ,
2596:                                        MatAssemblyEnd_MPIBAIJ,
2597:                                        MatSetOption_MPIBAIJ,
2598:                                        MatZeroEntries_MPIBAIJ,
2599:                                 /*24*/ MatZeroRows_MPIBAIJ,
2600:                                        0,
2601:                                        0,
2602:                                        0,
2603:                                        0,
2604:                                 /*29*/ MatSetUp_MPIBAIJ,
2605:                                        0,
2606:                                        0,
2607:                                        0,
2608:                                        0,
2609:                                 /*34*/ MatDuplicate_MPIBAIJ,
2610:                                        0,
2611:                                        0,
2612:                                        0,
2613:                                        0,
2614:                                 /*39*/ MatAXPY_MPIBAIJ,
2615:                                        MatGetSubMatrices_MPIBAIJ,
2616:                                        MatIncreaseOverlap_MPIBAIJ,
2617:                                        MatGetValues_MPIBAIJ,
2618:                                        MatCopy_MPIBAIJ,
2619:                                 /*44*/ 0,
2620:                                        MatScale_MPIBAIJ,
2621:                                        0,
2622:                                        0,
2623:                                        MatZeroRowsColumns_MPIBAIJ,
2624:                                 /*49*/ 0,
2625:                                        0,
2626:                                        0,
2627:                                        0,
2628:                                        0,
2629:                                 /*54*/ MatFDColoringCreate_MPIXAIJ,
2630:                                        0,
2631:                                        MatSetUnfactored_MPIBAIJ,
2632:                                        MatPermute_MPIBAIJ,
2633:                                        MatSetValuesBlocked_MPIBAIJ,
2634:                                 /*59*/ MatGetSubMatrix_MPIBAIJ,
2635:                                        MatDestroy_MPIBAIJ,
2636:                                        MatView_MPIBAIJ,
2637:                                        0,
2638:                                        0,
2639:                                 /*64*/ 0,
2640:                                        0,
2641:                                        0,
2642:                                        0,
2643:                                        0,
2644:                                 /*69*/ MatGetRowMaxAbs_MPIBAIJ,
2645:                                        0,
2646:                                        0,
2647:                                        0,
2648:                                        0,
2649:                                 /*74*/ 0,
2650:                                        MatFDColoringApply_BAIJ,
2651:                                        0,
2652:                                        0,
2653:                                        0,
2654:                                 /*79*/ 0,
2655:                                        0,
2656:                                        0,
2657:                                        0,
2658:                                        MatLoad_MPIBAIJ,
2659:                                 /*84*/ 0,
2660:                                        0,
2661:                                        0,
2662:                                        0,
2663:                                        0,
2664:                                 /*89*/ 0,
2665:                                        0,
2666:                                        0,
2667:                                        0,
2668:                                        0,
2669:                                 /*94*/ 0,
2670:                                        0,
2671:                                        0,
2672:                                        0,
2673:                                        0,
2674:                                 /*99*/ 0,
2675:                                        0,
2676:                                        0,
2677:                                        0,
2678:                                        0,
2679:                                 /*104*/0,
2680:                                        MatRealPart_MPIBAIJ,
2681:                                        MatImaginaryPart_MPIBAIJ,
2682:                                        0,
2683:                                        0,
2684:                                 /*109*/0,
2685:                                        0,
2686:                                        0,
2687:                                        0,
2688:                                        0,
2689:                                 /*114*/MatGetSeqNonzeroStructure_MPIBAIJ,
2690:                                        0,
2691:                                        MatGetGhosts_MPIBAIJ,
2692:                                        0,
2693:                                        0,
2694:                                 /*119*/0,
2695:                                        0,
2696:                                        0,
2697:                                        0,
2698:                                        MatGetMultiProcBlock_MPIBAIJ,
2699:                                 /*124*/0,
2700:                                        MatGetColumnNorms_MPIBAIJ,
2701:                                        MatInvertBlockDiagonal_MPIBAIJ,
2702:                                        0,
2703:                                        0,
2704:                                /*129*/ 0,
2705:                                        0,
2706:                                        0,
2707:                                        0,
2708:                                        0,
2709:                                /*134*/ 0,
2710:                                        0,
2711:                                        0,
2712:                                        0,
2713:                                        0,
2714:                                /*139*/ 0,
2715:                                        0,
2716:                                        0,
2717:                                        MatFDColoringSetUp_MPIXAIJ
2718: };

2722: PetscErrorCode  MatGetDiagonalBlock_MPIBAIJ(Mat A,Mat *a)
2723: {
2725:   *a = ((Mat_MPIBAIJ*)A->data)->A;
2726:   return(0);
2727: }

2729: PETSC_EXTERN PetscErrorCode MatConvert_MPIBAIJ_MPISBAIJ(Mat, MatType,MatReuse,Mat*);

2733: PetscErrorCode MatMPIBAIJSetPreallocationCSR_MPIBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
2734: {
2735:   PetscInt       m,rstart,cstart,cend;
2736:   PetscInt       i,j,d,nz,nz_max=0,*d_nnz=0,*o_nnz=0;
2737:   const PetscInt *JJ    =0;
2738:   PetscScalar    *values=0;
2739:   PetscBool      roworiented = ((Mat_MPIBAIJ*)B->data)->roworiented;

2743:   PetscLayoutSetBlockSize(B->rmap,bs);
2744:   PetscLayoutSetBlockSize(B->cmap,bs);
2745:   PetscLayoutSetUp(B->rmap);
2746:   PetscLayoutSetUp(B->cmap);
2747:   PetscLayoutGetBlockSize(B->rmap,&bs);
2748:   m      = B->rmap->n/bs;
2749:   rstart = B->rmap->rstart/bs;
2750:   cstart = B->cmap->rstart/bs;
2751:   cend   = B->cmap->rend/bs;

2753:   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
2754:   PetscMalloc2(m,&d_nnz,m,&o_nnz);
2755:   for (i=0; i<m; i++) {
2756:     nz = ii[i+1] - ii[i];
2757:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz);
2758:     nz_max = PetscMax(nz_max,nz);
2759:     JJ     = jj + ii[i];
2760:     for (j=0; j<nz; j++) {
2761:       if (*JJ >= cstart) break;
2762:       JJ++;
2763:     }
2764:     d = 0;
2765:     for (; j<nz; j++) {
2766:       if (*JJ++ >= cend) break;
2767:       d++;
2768:     }
2769:     d_nnz[i] = d;
2770:     o_nnz[i] = nz - d;
2771:   }
2772:   MatMPIBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);
2773:   PetscFree2(d_nnz,o_nnz);

2775:   values = (PetscScalar*)V;
2776:   if (!values) {
2777:     PetscMalloc1(bs*bs*nz_max,&values);
2778:     PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));
2779:   }
2780:   for (i=0; i<m; i++) {
2781:     PetscInt          row    = i + rstart;
2782:     PetscInt          ncols  = ii[i+1] - ii[i];
2783:     const PetscInt    *icols = jj + ii[i];
2784:     if (!roworiented) {         /* block ordering matches the non-nested layout of MatSetValues so we can insert entire rows */
2785:       const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2786:       MatSetValuesBlocked_MPIBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);
2787:     } else {                    /* block ordering does not match so we can only insert one block at a time. */
2788:       PetscInt j;
2789:       for (j=0; j<ncols; j++) {
2790:         const PetscScalar *svals = values + (V ? (bs*bs*(ii[i]+j)) : 0);
2791:         MatSetValuesBlocked_MPIBAIJ(B,1,&row,1,&icols[j],svals,INSERT_VALUES);
2792:       }
2793:     }
2794:   }

2796:   if (!V) { PetscFree(values); }
2797:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2798:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2799:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
2800:   return(0);
2801: }

2805: /*@C
2806:    MatMPIBAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in BAIJ format
2807:    (the default parallel PETSc format).

2809:    Collective on MPI_Comm

2811:    Input Parameters:
2812: +  B - the matrix
2813: .  bs - the block size
2814: .  i - the indices into j for the start of each local row (starts with zero)
2815: .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2816: -  v - optional values in the matrix

2818:    Level: developer

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

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

2828: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ, MatCreateMPIBAIJWithArrays(), MPIBAIJ
2829: @*/
2830: PetscErrorCode  MatMPIBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2831: {

2838:   PetscTryMethod(B,"MatMPIBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));
2839:   return(0);
2840: }

2844: PetscErrorCode  MatMPIBAIJSetPreallocation_MPIBAIJ(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt *d_nnz,PetscInt o_nz,const PetscInt *o_nnz)
2845: {
2846:   Mat_MPIBAIJ    *b;
2848:   PetscInt       i;

2851:   MatSetBlockSize(B,PetscAbs(bs));
2852:   PetscLayoutSetUp(B->rmap);
2853:   PetscLayoutSetUp(B->cmap);
2854:   PetscLayoutGetBlockSize(B->rmap,&bs);

2856:   if (d_nnz) {
2857:     for (i=0; i<B->rmap->n/bs; i++) {
2858:       if (d_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than -1: local row %D value %D",i,d_nnz[i]);
2859:     }
2860:   }
2861:   if (o_nnz) {
2862:     for (i=0; i<B->rmap->n/bs; i++) {
2863:       if (o_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than -1: local row %D value %D",i,o_nnz[i]);
2864:     }
2865:   }

2867:   b      = (Mat_MPIBAIJ*)B->data;
2868:   b->bs2 = bs*bs;
2869:   b->mbs = B->rmap->n/bs;
2870:   b->nbs = B->cmap->n/bs;
2871:   b->Mbs = B->rmap->N/bs;
2872:   b->Nbs = B->cmap->N/bs;

2874:   for (i=0; i<=b->size; i++) {
2875:     b->rangebs[i] = B->rmap->range[i]/bs;
2876:   }
2877:   b->rstartbs = B->rmap->rstart/bs;
2878:   b->rendbs   = B->rmap->rend/bs;
2879:   b->cstartbs = B->cmap->rstart/bs;
2880:   b->cendbs   = B->cmap->rend/bs;

2882:   if (!B->preallocated) {
2883:     MatCreate(PETSC_COMM_SELF,&b->A);
2884:     MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
2885:     MatSetType(b->A,MATSEQBAIJ);
2886:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
2887:     MatCreate(PETSC_COMM_SELF,&b->B);
2888:     MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);
2889:     MatSetType(b->B,MATSEQBAIJ);
2890:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);
2891:     MatStashCreate_Private(PetscObjectComm((PetscObject)B),bs,&B->bstash);
2892:   }

2894:   MatSeqBAIJSetPreallocation(b->A,bs,d_nz,d_nnz);
2895:   MatSeqBAIJSetPreallocation(b->B,bs,o_nz,o_nnz);
2896:   B->preallocated = PETSC_TRUE;
2897:   return(0);
2898: }

2900: extern PetscErrorCode  MatDiagonalScaleLocal_MPIBAIJ(Mat,Vec);
2901: extern PetscErrorCode  MatSetHashTableFactor_MPIBAIJ(Mat,PetscReal);

2905: PETSC_EXTERN PetscErrorCode MatConvert_MPIBAIJ_MPIAdj(Mat B, MatType newtype,MatReuse reuse,Mat *adj)
2906: {
2907:   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ*)B->data;
2909:   Mat_SeqBAIJ    *d  = (Mat_SeqBAIJ*) b->A->data,*o = (Mat_SeqBAIJ*) b->B->data;
2910:   PetscInt       M   = B->rmap->n/B->rmap->bs,i,*ii,*jj,cnt,j,k,rstart = B->rmap->rstart/B->rmap->bs;
2911:   const PetscInt *id = d->i, *jd = d->j, *io = o->i, *jo = o->j, *garray = b->garray;

2914:   PetscMalloc1((M+1),&ii);
2915:   ii[0] = 0;
2916:   for (i=0; i<M; i++) {
2917:     if ((id[i+1] - id[i]) < 0) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Indices wrong %D %D %D",i,id[i],id[i+1]);
2918:     if ((io[i+1] - io[i]) < 0) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Indices wrong %D %D %D",i,io[i],io[i+1]);
2919:     ii[i+1] = ii[i] + id[i+1] - id[i] + io[i+1] - io[i];
2920:     /* remove one from count of matrix has diagonal */
2921:     for (j=id[i]; j<id[i+1]; j++) {
2922:       if (jd[j] == i) {ii[i+1]--;break;}
2923:     }
2924:   }
2925:   PetscMalloc1(ii[M],&jj);
2926:   cnt  = 0;
2927:   for (i=0; i<M; i++) {
2928:     for (j=io[i]; j<io[i+1]; j++) {
2929:       if (garray[jo[j]] > rstart) break;
2930:       jj[cnt++] = garray[jo[j]];
2931:     }
2932:     for (k=id[i]; k<id[i+1]; k++) {
2933:       if (jd[k] != i) {
2934:         jj[cnt++] = rstart + jd[k];
2935:       }
2936:     }
2937:     for (; j<io[i+1]; j++) {
2938:       jj[cnt++] = garray[jo[j]];
2939:     }
2940:   }
2941:   MatCreateMPIAdj(PetscObjectComm((PetscObject)B),M,B->cmap->N/B->rmap->bs,ii,jj,NULL,adj);
2942:   return(0);
2943: }

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

2947: PETSC_EXTERN PetscErrorCode MatConvert_SeqBAIJ_SeqAIJ(Mat,MatType,MatReuse,Mat*);

2951: PETSC_EXTERN PetscErrorCode MatConvert_MPIBAIJ_MPIAIJ(Mat A,MatType newtype,MatReuse reuse,Mat *newmat)
2952: {
2954:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
2955:   Mat            B;
2956:   Mat_MPIAIJ     *b;

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

2961:   MatCreate(PetscObjectComm((PetscObject)A),&B);
2962:   MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
2963:   MatSetType(B,MATMPIAIJ);
2964:   MatSeqAIJSetPreallocation(B,0,NULL);
2965:   MatMPIAIJSetPreallocation(B,0,NULL,0,NULL);
2966:   b    = (Mat_MPIAIJ*) B->data;

2968:   MatDestroy(&b->A);
2969:   MatDestroy(&b->B);
2970:   MatDisAssemble_MPIBAIJ(A);
2971:   MatConvert_SeqBAIJ_SeqAIJ(a->A, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->A);
2972:   MatConvert_SeqBAIJ_SeqAIJ(a->B, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->B);
2973:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2974:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2975:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
2976:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
2977:   if (reuse == MAT_REUSE_MATRIX) {
2978:     MatHeaderReplace(A,B);
2979:   } else {
2980:    *newmat = B;
2981:   }
2982:   return(0);
2983: }

2985: #if defined(PETSC_HAVE_MUMPS)
2986: PETSC_EXTERN PetscErrorCode MatGetFactor_baij_mumps(Mat,MatFactorType,Mat*);
2987: #endif

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

2992:    Options Database Keys:
2993: + -mat_type mpibaij - sets the matrix type to "mpibaij" during a call to MatSetFromOptions()
2994: . -mat_block_size <bs> - set the blocksize used to store the matrix
2995: - -mat_use_hash_table <fact>

2997:   Level: beginner

2999: .seealso: MatCreateMPIBAIJ
3000: M*/

3002: PETSC_EXTERN PetscErrorCode MatConvert_MPIBAIJ_MPIBSTRM(Mat,MatType,MatReuse,Mat*);

3006: PETSC_EXTERN PetscErrorCode MatCreate_MPIBAIJ(Mat B)
3007: {
3008:   Mat_MPIBAIJ    *b;
3010:   PetscBool      flg;

3013:   PetscNewLog(B,&b);
3014:   B->data = (void*)b;

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

3019:   B->insertmode = NOT_SET_VALUES;
3020:   MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);
3021:   MPI_Comm_size(PetscObjectComm((PetscObject)B),&b->size);

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

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

3029:   b->donotstash  = PETSC_FALSE;
3030:   b->colmap      = NULL;
3031:   b->garray      = NULL;
3032:   b->roworiented = PETSC_TRUE;

3034:   /* stuff used in block assembly */
3035:   b->barray = 0;

3037:   /* stuff used for matrix vector multiply */
3038:   b->lvec  = 0;
3039:   b->Mvctx = 0;

3041:   /* stuff for MatGetRow() */
3042:   b->rowindices   = 0;
3043:   b->rowvalues    = 0;
3044:   b->getrowactive = PETSC_FALSE;

3046:   /* hash table stuff */
3047:   b->ht           = 0;
3048:   b->hd           = 0;
3049:   b->ht_size      = 0;
3050:   b->ht_flag      = PETSC_FALSE;
3051:   b->ht_fact      = 0;
3052:   b->ht_total_ct  = 0;
3053:   b->ht_insert_ct = 0;

3055:   /* stuff for MatGetSubMatrices_MPIBAIJ_local() */
3056:   b->ijonly = PETSC_FALSE;

3058:   PetscOptionsBegin(PetscObjectComm((PetscObject)B),NULL,"Options for loading MPIBAIJ matrix 1","Mat");
3059:   PetscOptionsBool("-mat_use_hash_table","Use hash table to save memory in constructing matrix","MatSetOption",PETSC_FALSE,&flg,NULL);
3060:   if (flg) {
3061:     PetscReal fact = 1.39;
3062:     MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);
3063:     PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,NULL);
3064:     if (fact <= 1.0) fact = 1.39;
3065:     MatMPIBAIJSetHashTableFactor(B,fact);
3066:     PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);
3067:   }
3068:   PetscOptionsEnd();

3070: #if defined(PETSC_HAVE_MUMPS)
3071:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_baij_mumps);
3072: #endif
3073:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpiadj_C",MatConvert_MPIBAIJ_MPIAdj);
3074:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpiaij_C",MatConvert_MPIBAIJ_MPIAIJ);
3075:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpisbaij_C",MatConvert_MPIBAIJ_MPISBAIJ);
3076:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIBAIJ);
3077:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIBAIJ);
3078:   PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",MatGetDiagonalBlock_MPIBAIJ);
3079:   PetscObjectComposeFunction((PetscObject)B,"MatMPIBAIJSetPreallocation_C",MatMPIBAIJSetPreallocation_MPIBAIJ);
3080:   PetscObjectComposeFunction((PetscObject)B,"MatMPIBAIJSetPreallocationCSR_C",MatMPIBAIJSetPreallocationCSR_MPIBAIJ);
3081:   PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIBAIJ);
3082:   PetscObjectComposeFunction((PetscObject)B,"MatSetHashTableFactor_C",MatSetHashTableFactor_MPIBAIJ);
3083:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpibstrm_C",MatConvert_MPIBAIJ_MPIBSTRM);
3084:   PetscObjectChangeTypeName((PetscObject)B,MATMPIBAIJ);
3085:   return(0);
3086: }

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

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

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

3097:   Level: beginner

3099: .seealso: MatCreateBAIJ(),MATSEQBAIJ,MATMPIBAIJ, MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
3100: M*/

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

3111:    Collective on Mat

3113:    Input Parameters:
3114: +  B - the matrix
3115: .  bs   - size of block
3116: .  d_nz  - number of block nonzeros per block row in diagonal portion of local
3117:            submatrix  (same for all local rows)
3118: .  d_nnz - array containing the number of block nonzeros in the various block rows
3119:            of the in diagonal portion of the local (possibly different for each block
3120:            row) or NULL.  If you plan to factor the matrix you must leave room for the diagonal entry and
3121:            set it even if it is zero.
3122: .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
3123:            submatrix (same for all local rows).
3124: -  o_nnz - array containing the number of nonzeros in the various block rows of the
3125:            off-diagonal portion of the local submatrix (possibly different for
3126:            each block row) or NULL.

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

3130:    Options Database Keys:
3131: +   -mat_block_size - size of the blocks to use
3132: -   -mat_use_hash_table <fact>

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

3138:    Storage Information:
3139:    For a square global matrix we define each processor's diagonal portion
3140:    to be its local rows and the corresponding columns (a square submatrix);
3141:    each processor's off-diagonal portion encompasses the remainder of the
3142:    local matrix (a rectangular submatrix).

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

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

3153: .vb
3154:            0 1 2 3 4 5 6 7 8 9 10 11
3155:           --------------------------
3156:    row 3  |o o o d d d o o o o  o  o
3157:    row 4  |o o o d d d o o o o  o  o
3158:    row 5  |o o o d d d o o o o  o  o
3159:           --------------------------
3160: .ve

3162:    Thus, any entries in the d locations are stored in the d (diagonal)
3163:    submatrix, and any entries in the o locations are stored in the
3164:    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
3165:    stored simply in the MATSEQBAIJ format for compressed row storage.

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

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

3179:    Level: intermediate

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

3183: .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateBAIJ(), MatMPIBAIJSetPreallocationCSR(), PetscSplitOwnership()
3184: @*/
3185: PetscErrorCode  MatMPIBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3186: {

3193:   PetscTryMethod(B,"MatMPIBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,bs,d_nz,d_nnz,o_nz,o_nnz));
3194:   return(0);
3195: }

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

3206:    Collective on MPI_Comm

3208:    Input Parameters:
3209: +  comm - MPI communicator
3210: .  bs   - size of blockk
3211: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
3212:            This value should be the same as the local size used in creating the
3213:            y vector for the matrix-vector product y = Ax.
3214: .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
3215:            This value should be the same as the local size used in creating the
3216:            x vector for the matrix-vector product y = Ax.
3217: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3218: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
3219: .  d_nz  - number of nonzero blocks per block row in diagonal portion of local
3220:            submatrix  (same for all local rows)
3221: .  d_nnz - array containing the number of nonzero blocks in the various block rows
3222:            of the in diagonal portion of the local (possibly different for each block
3223:            row) or NULL.  If you plan to factor the matrix you must leave room for the diagonal entry
3224:            and set it even if it is zero.
3225: .  o_nz  - number of nonzero blocks per block row in the off-diagonal portion of local
3226:            submatrix (same for all local rows).
3227: -  o_nnz - array containing the number of nonzero blocks in the various block rows of the
3228:            off-diagonal portion of the local submatrix (possibly different for
3229:            each block row) or NULL.

3231:    Output Parameter:
3232: .  A - the matrix

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

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

3242:    Notes:
3243:    If the *_nnz parameter is given then the *_nz parameter is ignored

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

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

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

3253:    Storage Information:
3254:    For a square global matrix we define each processor's diagonal portion
3255:    to be its local rows and the corresponding columns (a square submatrix);
3256:    each processor's off-diagonal portion encompasses the remainder of the
3257:    local matrix (a rectangular submatrix).

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

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

3268: .vb
3269:            0 1 2 3 4 5 6 7 8 9 10 11
3270:           --------------------------
3271:    row 3  |o o o d d d o o o o  o  o
3272:    row 4  |o o o d d d o o o o  o  o
3273:    row 5  |o o o d d d o o o o  o  o
3274:           --------------------------
3275: .ve

3277:    Thus, any entries in the d locations are stored in the d (diagonal)
3278:    submatrix, and any entries in the o locations are stored in the
3279:    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
3280:    stored simply in the MATSEQBAIJ format for compressed row storage.

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

3289:    Level: intermediate

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

3293: .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateBAIJ(), MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
3294: @*/
3295: PetscErrorCode  MatCreateBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A)
3296: {
3298:   PetscMPIInt    size;

3301:   MatCreate(comm,A);
3302:   MatSetSizes(*A,m,n,M,N);
3303:   MPI_Comm_size(comm,&size);
3304:   if (size > 1) {
3305:     MatSetType(*A,MATMPIBAIJ);
3306:     MatMPIBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);
3307:   } else {
3308:     MatSetType(*A,MATSEQBAIJ);
3309:     MatSeqBAIJSetPreallocation(*A,bs,d_nz,d_nnz);
3310:   }
3311:   return(0);
3312: }

3316: static PetscErrorCode MatDuplicate_MPIBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
3317: {
3318:   Mat            mat;
3319:   Mat_MPIBAIJ    *a,*oldmat = (Mat_MPIBAIJ*)matin->data;
3321:   PetscInt       len=0;

3324:   *newmat = 0;
3325:   MatCreate(PetscObjectComm((PetscObject)matin),&mat);
3326:   MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);
3327:   MatSetType(mat,((PetscObject)matin)->type_name);
3328:   PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));

3330:   mat->factortype   = matin->factortype;
3331:   mat->preallocated = PETSC_TRUE;
3332:   mat->assembled    = PETSC_TRUE;
3333:   mat->insertmode   = NOT_SET_VALUES;

3335:   a             = (Mat_MPIBAIJ*)mat->data;
3336:   mat->rmap->bs = matin->rmap->bs;
3337:   a->bs2        = oldmat->bs2;
3338:   a->mbs        = oldmat->mbs;
3339:   a->nbs        = oldmat->nbs;
3340:   a->Mbs        = oldmat->Mbs;
3341:   a->Nbs        = oldmat->Nbs;

3343:   PetscLayoutReference(matin->rmap,&mat->rmap);
3344:   PetscLayoutReference(matin->cmap,&mat->cmap);

3346:   a->size         = oldmat->size;
3347:   a->rank         = oldmat->rank;
3348:   a->donotstash   = oldmat->donotstash;
3349:   a->roworiented  = oldmat->roworiented;
3350:   a->rowindices   = 0;
3351:   a->rowvalues    = 0;
3352:   a->getrowactive = PETSC_FALSE;
3353:   a->barray       = 0;
3354:   a->rstartbs     = oldmat->rstartbs;
3355:   a->rendbs       = oldmat->rendbs;
3356:   a->cstartbs     = oldmat->cstartbs;
3357:   a->cendbs       = oldmat->cendbs;

3359:   /* hash table stuff */
3360:   a->ht           = 0;
3361:   a->hd           = 0;
3362:   a->ht_size      = 0;
3363:   a->ht_flag      = oldmat->ht_flag;
3364:   a->ht_fact      = oldmat->ht_fact;
3365:   a->ht_total_ct  = 0;
3366:   a->ht_insert_ct = 0;

3368:   PetscMemcpy(a->rangebs,oldmat->rangebs,(a->size+1)*sizeof(PetscInt));
3369:   if (oldmat->colmap) {
3370: #if defined(PETSC_USE_CTABLE)
3371:     PetscTableCreateCopy(oldmat->colmap,&a->colmap);
3372: #else
3373:     PetscMalloc1((a->Nbs),&a->colmap);
3374:     PetscLogObjectMemory((PetscObject)mat,(a->Nbs)*sizeof(PetscInt));
3375:     PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(PetscInt));
3376: #endif
3377:   } else a->colmap = 0;

3379:   if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
3380:     PetscMalloc1(len,&a->garray);
3381:     PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));
3382:     PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));
3383:   } else a->garray = 0;

3385:   MatStashCreate_Private(PetscObjectComm((PetscObject)matin),matin->rmap->bs,&mat->bstash);
3386:   VecDuplicate(oldmat->lvec,&a->lvec);
3387:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);
3388:   VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
3389:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);

3391:   MatDuplicate(oldmat->A,cpvalues,&a->A);
3392:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
3393:   MatDuplicate(oldmat->B,cpvalues,&a->B);
3394:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);
3395:   PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);
3396:   *newmat = mat;
3397:   return(0);
3398: }

3402: PetscErrorCode MatLoad_MPIBAIJ(Mat newmat,PetscViewer viewer)
3403: {
3405:   int            fd;
3406:   PetscInt       i,nz,j,rstart,rend;
3407:   PetscScalar    *vals,*buf;
3408:   MPI_Comm       comm;
3409:   MPI_Status     status;
3410:   PetscMPIInt    rank,size,maxnz;
3411:   PetscInt       header[4],*rowlengths = 0,M,N,m,*rowners,*cols;
3412:   PetscInt       *locrowlens = NULL,*procsnz = NULL,*browners = NULL;
3413:   PetscInt       jj,*mycols,*ibuf,bs = newmat->rmap->bs,Mbs,mbs,extra_rows,mmax;
3414:   PetscMPIInt    tag    = ((PetscObject)viewer)->tag;
3415:   PetscInt       *dlens = NULL,*odlens = NULL,*mask = NULL,*masked1 = NULL,*masked2 = NULL,rowcount,odcount;
3416:   PetscInt       dcount,kmax,k,nzcount,tmp,mend,sizesset=1,grows,gcols;

3419:   PetscObjectGetComm((PetscObject)viewer,&comm);
3420:   PetscOptionsBegin(comm,NULL,"Options for loading MPIBAIJ matrix 2","Mat");
3421:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
3422:   PetscOptionsEnd();
3423:   if (bs < 0) bs = 1;

3425:   MPI_Comm_size(comm,&size);
3426:   MPI_Comm_rank(comm,&rank);
3427:   if (!rank) {
3428:     PetscViewerBinaryGetDescriptor(viewer,&fd);
3429:     PetscBinaryRead(fd,(char*)header,4,PETSC_INT);
3430:     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
3431:   }

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

3435:   MPI_Bcast(header+1,3,MPIU_INT,0,comm);
3436:   M    = header[1]; N = header[2];

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

3442:   /* If global sizes are set, check if they are consistent with that given in the file */
3443:   if (sizesset) {
3444:     MatGetSize(newmat,&grows,&gcols);
3445:   }
3446:   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);
3447:   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);

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

3451:   /*
3452:      This code adds extra rows to make sure the number of rows is
3453:      divisible by the blocksize
3454:   */
3455:   Mbs        = M/bs;
3456:   extra_rows = bs - M + bs*Mbs;
3457:   if (extra_rows == bs) extra_rows = 0;
3458:   else                  Mbs++;
3459:   if (extra_rows && !rank) {
3460:     PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");
3461:   }

3463:   /* determine ownership of all rows */
3464:   if (newmat->rmap->n < 0) { /* PETSC_DECIDE */
3465:     mbs = Mbs/size + ((Mbs % size) > rank);
3466:     m   = mbs*bs;
3467:   } else { /* User set */
3468:     m   = newmat->rmap->n;
3469:     mbs = m/bs;
3470:   }
3471:   PetscMalloc2(size+1,&rowners,size+1,&browners);
3472:   MPI_Allgather(&mbs,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);

3474:   /* process 0 needs enough room for process with most rows */
3475:   if (!rank) {
3476:     mmax = rowners[1];
3477:     for (i=2; i<=size; i++) {
3478:       mmax = PetscMax(mmax,rowners[i]);
3479:     }
3480:     mmax*=bs;
3481:   } else mmax = -1;             /* unused, but compiler warns anyway */

3483:   rowners[0] = 0;
3484:   for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
3485:   for (i=0; i<=size; i++) browners[i] = rowners[i]*bs;
3486:   rstart = rowners[rank];
3487:   rend   = rowners[rank+1];

3489:   /* distribute row lengths to all processors */
3490:   PetscMalloc1(m,&locrowlens);
3491:   if (!rank) {
3492:     mend = m;
3493:     if (size == 1) mend = mend - extra_rows;
3494:     PetscBinaryRead(fd,locrowlens,mend,PETSC_INT);
3495:     for (j=mend; j<m; j++) locrowlens[j] = 1;
3496:     PetscMalloc1(mmax,&rowlengths);
3497:     PetscCalloc1(size,&procsnz);
3498:     for (j=0; j<m; j++) {
3499:       procsnz[0] += locrowlens[j];
3500:     }
3501:     for (i=1; i<size; i++) {
3502:       mend = browners[i+1] - browners[i];
3503:       if (i == size-1) mend = mend - extra_rows;
3504:       PetscBinaryRead(fd,rowlengths,mend,PETSC_INT);
3505:       for (j=mend; j<browners[i+1] - browners[i]; j++) rowlengths[j] = 1;
3506:       /* calculate the number of nonzeros on each processor */
3507:       for (j=0; j<browners[i+1]-browners[i]; j++) {
3508:         procsnz[i] += rowlengths[j];
3509:       }
3510:       MPI_Send(rowlengths,browners[i+1]-browners[i],MPIU_INT,i,tag,comm);
3511:     }
3512:     PetscFree(rowlengths);
3513:   } else {
3514:     MPI_Recv(locrowlens,m,MPIU_INT,0,tag,comm,&status);
3515:   }

3517:   if (!rank) {
3518:     /* determine max buffer needed and allocate it */
3519:     maxnz = procsnz[0];
3520:     for (i=1; i<size; i++) {
3521:       maxnz = PetscMax(maxnz,procsnz[i]);
3522:     }
3523:     PetscMalloc1(maxnz,&cols);

3525:     /* read in my part of the matrix column indices  */
3526:     nz     = procsnz[0];
3527:     PetscMalloc1((nz+1),&ibuf);
3528:     mycols = ibuf;
3529:     if (size == 1) nz -= extra_rows;
3530:     PetscBinaryRead(fd,mycols,nz,PETSC_INT);
3531:     if (size == 1) {
3532:       for (i=0; i< extra_rows; i++) mycols[nz+i] = M+i;
3533:     }

3535:     /* read in every ones (except the last) and ship off */
3536:     for (i=1; i<size-1; i++) {
3537:       nz   = procsnz[i];
3538:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
3539:       MPI_Send(cols,nz,MPIU_INT,i,tag,comm);
3540:     }
3541:     /* read in the stuff for the last proc */
3542:     if (size != 1) {
3543:       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
3544:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
3545:       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
3546:       MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);
3547:     }
3548:     PetscFree(cols);
3549:   } else {
3550:     /* determine buffer space needed for message */
3551:     nz = 0;
3552:     for (i=0; i<m; i++) {
3553:       nz += locrowlens[i];
3554:     }
3555:     PetscMalloc1((nz+1),&ibuf);
3556:     mycols = ibuf;
3557:     /* receive message of column indices*/
3558:     MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);
3559:     MPI_Get_count(&status,MPIU_INT,&maxnz);
3560:     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
3561:   }

3563:   /* loop over local rows, determining number of off diagonal entries */
3564:   PetscMalloc2(rend-rstart,&dlens,rend-rstart,&odlens);
3565:   PetscCalloc3(Mbs,&mask,Mbs,&masked1,Mbs,&masked2);
3566:   rowcount = 0; nzcount = 0;
3567:   for (i=0; i<mbs; i++) {
3568:     dcount  = 0;
3569:     odcount = 0;
3570:     for (j=0; j<bs; j++) {
3571:       kmax = locrowlens[rowcount];
3572:       for (k=0; k<kmax; k++) {
3573:         tmp = mycols[nzcount++]/bs;
3574:         if (!mask[tmp]) {
3575:           mask[tmp] = 1;
3576:           if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp;
3577:           else masked1[dcount++] = tmp;
3578:         }
3579:       }
3580:       rowcount++;
3581:     }

3583:     dlens[i]  = dcount;
3584:     odlens[i] = odcount;

3586:     /* zero out the mask elements we set */
3587:     for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
3588:     for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
3589:   }


3592:   if (!sizesset) {
3593:     MatSetSizes(newmat,m,m,M+extra_rows,N+extra_rows);
3594:   }
3595:   MatMPIBAIJSetPreallocation(newmat,bs,0,dlens,0,odlens);

3597:   if (!rank) {
3598:     PetscMalloc1((maxnz+1),&buf);
3599:     /* read in my part of the matrix numerical values  */
3600:     nz     = procsnz[0];
3601:     vals   = buf;
3602:     mycols = ibuf;
3603:     if (size == 1) nz -= extra_rows;
3604:     PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3605:     if (size == 1) {
3606:       for (i=0; i< extra_rows; i++) vals[nz+i] = 1.0;
3607:     }

3609:     /* insert into matrix */
3610:     jj = rstart*bs;
3611:     for (i=0; i<m; i++) {
3612:       MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
3613:       mycols += locrowlens[i];
3614:       vals   += locrowlens[i];
3615:       jj++;
3616:     }
3617:     /* read in other processors (except the last one) and ship out */
3618:     for (i=1; i<size-1; i++) {
3619:       nz   = procsnz[i];
3620:       vals = buf;
3621:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3622:       MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);
3623:     }
3624:     /* the last proc */
3625:     if (size != 1) {
3626:       nz   = procsnz[i] - extra_rows;
3627:       vals = buf;
3628:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3629:       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
3630:       MPIULong_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)newmat)->tag,comm);
3631:     }
3632:     PetscFree(procsnz);
3633:   } else {
3634:     /* receive numeric values */
3635:     PetscMalloc1((nz+1),&buf);

3637:     /* receive message of values*/
3638:     vals   = buf;
3639:     mycols = ibuf;
3640:     MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm);

3642:     /* insert into matrix */
3643:     jj = rstart*bs;
3644:     for (i=0; i<m; i++) {
3645:       MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
3646:       mycols += locrowlens[i];
3647:       vals   += locrowlens[i];
3648:       jj++;
3649:     }
3650:   }
3651:   PetscFree(locrowlens);
3652:   PetscFree(buf);
3653:   PetscFree(ibuf);
3654:   PetscFree2(rowners,browners);
3655:   PetscFree2(dlens,odlens);
3656:   PetscFree3(mask,masked1,masked2);
3657:   MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
3658:   MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
3659:   return(0);
3660: }

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

3667:    Input Parameters:
3668: .  mat  - the matrix
3669: .  fact - factor

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

3673:    Level: advanced

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

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

3680: .seealso: MatSetOption()
3681: @*/
3682: PetscErrorCode  MatMPIBAIJSetHashTableFactor(Mat mat,PetscReal fact)
3683: {

3687:   PetscTryMethod(mat,"MatSetHashTableFactor_C",(Mat,PetscReal),(mat,fact));
3688:   return(0);
3689: }

3693: PetscErrorCode  MatSetHashTableFactor_MPIBAIJ(Mat mat,PetscReal fact)
3694: {
3695:   Mat_MPIBAIJ *baij;

3698:   baij          = (Mat_MPIBAIJ*)mat->data;
3699:   baij->ht_fact = fact;
3700:   return(0);
3701: }

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

3710:   if (Ad)     *Ad     = a->A;
3711:   if (Ao)     *Ao     = a->B;
3712:   if (colmap) *colmap = a->garray;
3713:   return(0);
3714: }

3716: /*
3717:     Special version for direct calls from Fortran (to eliminate two function call overheads
3718: */
3719: #if defined(PETSC_HAVE_FORTRAN_CAPS)
3720: #define matmpibaijsetvaluesblocked_ MATMPIBAIJSETVALUESBLOCKED
3721: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
3722: #define matmpibaijsetvaluesblocked_ matmpibaijsetvaluesblocked
3723: #endif

3727: /*@C
3728:   MatMPIBAIJSetValuesBlocked - Direct Fortran call to replace call to MatSetValuesBlocked()

3730:   Collective on Mat

3732:   Input Parameters:
3733: + mat - the matrix
3734: . min - number of input rows
3735: . im - input rows
3736: . nin - number of input columns
3737: . in - input columns
3738: . v - numerical values input
3739: - addvin - INSERT_VALUES or ADD_VALUES

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

3743:   Level: advanced

3745: .seealso:   MatSetValuesBlocked()
3746: @*/
3747: PetscErrorCode matmpibaijsetvaluesblocked_(Mat *matin,PetscInt *min,const PetscInt im[],PetscInt *nin,const PetscInt in[],const MatScalar v[],InsertMode *addvin)
3748: {
3749:   /* convert input arguments to C version */
3750:   Mat        mat  = *matin;
3751:   PetscInt   m    = *min, n = *nin;
3752:   InsertMode addv = *addvin;

3754:   Mat_MPIBAIJ     *baij = (Mat_MPIBAIJ*)mat->data;
3755:   const MatScalar *value;
3756:   MatScalar       *barray     = baij->barray;
3757:   PetscBool       roworiented = baij->roworiented;
3758:   PetscErrorCode  ierr;
3759:   PetscInt        i,j,ii,jj,row,col,rstart=baij->rstartbs;
3760:   PetscInt        rend=baij->rendbs,cstart=baij->cstartbs,stepval;
3761:   PetscInt        cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;

3764:   /* tasks normally handled by MatSetValuesBlocked() */
3765:   if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;
3766: #if defined(PETSC_USE_DEBUG)
3767:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
3768:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3769: #endif
3770:   if (mat->assembled) {
3771:     mat->was_assembled = PETSC_TRUE;
3772:     mat->assembled     = PETSC_FALSE;
3773:   }
3774:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);


3777:   if (!barray) {
3778:     PetscMalloc1(bs2,&barray);
3779:     baij->barray = barray;
3780:   }

3782:   if (roworiented) stepval = (n-1)*bs;
3783:   else stepval = (m-1)*bs;

3785:   for (i=0; i<m; i++) {
3786:     if (im[i] < 0) continue;
3787: #if defined(PETSC_USE_DEBUG)
3788:     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);
3789: #endif
3790:     if (im[i] >= rstart && im[i] < rend) {
3791:       row = im[i] - rstart;
3792:       for (j=0; j<n; j++) {
3793:         /* If NumCol = 1 then a copy is not required */
3794:         if ((roworiented) && (n == 1)) {
3795:           barray = (MatScalar*)v + i*bs2;
3796:         } else if ((!roworiented) && (m == 1)) {
3797:           barray = (MatScalar*)v + j*bs2;
3798:         } else { /* Here a copy is required */
3799:           if (roworiented) {
3800:             value = v + i*(stepval+bs)*bs + j*bs;
3801:           } else {
3802:             value = v + j*(stepval+bs)*bs + i*bs;
3803:           }
3804:           for (ii=0; ii<bs; ii++,value+=stepval) {
3805:             for (jj=0; jj<bs; jj++) {
3806:               *barray++ = *value++;
3807:             }
3808:           }
3809:           barray -=bs2;
3810:         }

3812:         if (in[j] >= cstart && in[j] < cend) {
3813:           col  = in[j] - cstart;
3814:           MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);
3815:         } else if (in[j] < 0) continue;
3816: #if defined(PETSC_USE_DEBUG)
3817:         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);
3818: #endif
3819:         else {
3820:           if (mat->was_assembled) {
3821:             if (!baij->colmap) {
3822:               MatCreateColmap_MPIBAIJ_Private(mat);
3823:             }

3825: #if defined(PETSC_USE_DEBUG)
3826: #if defined(PETSC_USE_CTABLE)
3827:             { PetscInt data;
3828:               PetscTableFind(baij->colmap,in[j]+1,&data);
3829:               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
3830:             }
3831: #else
3832:             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
3833: #endif
3834: #endif
3835: #if defined(PETSC_USE_CTABLE)
3836:             PetscTableFind(baij->colmap,in[j]+1,&col);
3837:             col  = (col - 1)/bs;
3838: #else
3839:             col = (baij->colmap[in[j]] - 1)/bs;
3840: #endif
3841:             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
3842:               MatDisAssemble_MPIBAIJ(mat);
3843:               col  =  in[j];
3844:             }
3845:           } else col = in[j];
3846:           MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);
3847:         }
3848:       }
3849:     } else {
3850:       if (!baij->donotstash) {
3851:         if (roworiented) {
3852:           MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
3853:         } else {
3854:           MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
3855:         }
3856:       }
3857:     }
3858:   }

3860:   /* task normally handled by MatSetValuesBlocked() */
3861:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
3862:   return(0);
3863: }

3867: /*@
3868:      MatCreateMPIBAIJWithArrays - creates a MPI BAIJ matrix using arrays that contain in standard
3869:          CSR format the local rows.

3871:    Collective on MPI_Comm

3873:    Input Parameters:
3874: +  comm - MPI communicator
3875: .  bs - the block size, only a block size of 1 is supported
3876: .  m - number of local rows (Cannot be PETSC_DECIDE)
3877: .  n - This value should be the same as the local size used in creating the
3878:        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
3879:        calculated if N is given) For square matrices n is almost always m.
3880: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3881: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
3882: .   i - row indices
3883: .   j - column indices
3884: -   a - matrix values

3886:    Output Parameter:
3887: .   mat - the matrix

3889:    Level: intermediate

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

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

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

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

3905: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
3906:           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
3907: @*/
3908: PetscErrorCode  MatCreateMPIBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
3909: {

3913:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
3914:   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
3915:   MatCreate(comm,mat);
3916:   MatSetSizes(*mat,m,n,M,N);
3917:   MatSetType(*mat,MATMPISBAIJ);
3918:   MatSetOption(*mat,MAT_ROW_ORIENTED,PETSC_FALSE);
3919:   MatMPIBAIJSetPreallocationCSR(*mat,bs,i,j,a);
3920:   MatSetOption(*mat,MAT_ROW_ORIENTED,PETSC_TRUE);
3921:   return(0);
3922: }