Actual source code: mpisbaij.c

  1: /*$Id: mpisbaij.c,v 1.61 2001/08/10 03:31:37 bsmith Exp $*/

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

  7: extern int MatSetUpMultiply_MPISBAIJ(Mat);
  8: extern int MatSetUpMultiply_MPISBAIJ_2comm(Mat);
  9: extern int DisAssemble_MPISBAIJ(Mat);
 10: extern int MatIncreaseOverlap_MPISBAIJ(Mat,int,IS[],int);
 11: extern int MatGetValues_SeqSBAIJ(Mat,int,const int[],int,const int[],PetscScalar []);
 12: extern int MatGetValues_SeqBAIJ(Mat,int,const int[],int,const int[],PetscScalar []);
 13: extern int MatSetValues_SeqSBAIJ(Mat,int,const int [],int,const int [],const PetscScalar [],InsertMode);
 14: extern int MatSetValuesBlocked_SeqSBAIJ(Mat,int,const int[],int,const int[],const PetscScalar[],InsertMode);
 15: extern int MatSetValuesBlocked_SeqBAIJ(Mat,int,const int[],int,const int[],const PetscScalar[],InsertMode);
 16: extern int MatGetRow_SeqSBAIJ(Mat,int,int*,int**,PetscScalar**);
 17: extern int MatRestoreRow_SeqSBAIJ(Mat,int,int*,int**,PetscScalar**);
 18: extern int MatPrintHelp_SeqSBAIJ(Mat);
 19: extern int MatZeroRows_SeqSBAIJ(Mat,IS,PetscScalar*);
 20: extern int MatZeroRows_SeqBAIJ(Mat,IS,PetscScalar *);
 21: extern int MatGetRowMax_MPISBAIJ(Mat,Vec);
 22: extern int MatRelax_MPISBAIJ(Mat,Vec,PetscReal,MatSORType,PetscReal,int,int,Vec);

 24: /*  UGLY, ugly, ugly
 25:    When MatScalar == PetscScalar the function MatSetValuesBlocked_MPIBAIJ_MatScalar() does 
 26:    not exist. Otherwise ..._MatScalar() takes matrix elements in single precision and 
 27:    inserts them into the single precision data structure. The function MatSetValuesBlocked_MPIBAIJ()
 28:    converts the entries into single precision and then calls ..._MatScalar() to put them
 29:    into the single precision data structures.
 30: */
 31: #if defined(PETSC_USE_MAT_SINGLE)
 32: extern int MatSetValuesBlocked_SeqSBAIJ_MatScalar(Mat,int,const int[],int,const int[],const MatScalar[],InsertMode);
 33: extern int MatSetValues_MPISBAIJ_MatScalar(Mat,int,const int[],int,const int[],const MatScalar[],InsertMode);
 34: extern int MatSetValuesBlocked_MPISBAIJ_MatScalar(Mat,int,const int[],int,const int[],const MatScalar[],InsertMode);
 35: extern int MatSetValues_MPISBAIJ_HT_MatScalar(Mat,int,const int[],int,const int[],const MatScalar[],InsertMode);
 36: extern int MatSetValuesBlocked_MPISBAIJ_HT_MatScalar(Mat,int,const int[],int,const int[],const MatScalar[],InsertMode);
 37: #else
 38: #define MatSetValuesBlocked_SeqSBAIJ_MatScalar      MatSetValuesBlocked_SeqSBAIJ
 39: #define MatSetValues_MPISBAIJ_MatScalar             MatSetValues_MPISBAIJ
 40: #define MatSetValuesBlocked_MPISBAIJ_MatScalar      MatSetValuesBlocked_MPISBAIJ
 41: #define MatSetValues_MPISBAIJ_HT_MatScalar          MatSetValues_MPISBAIJ_HT
 42: #define MatSetValuesBlocked_MPISBAIJ_HT_MatScalar   MatSetValuesBlocked_MPISBAIJ_HT
 43: #endif

 45: EXTERN_C_BEGIN
 48: int MatStoreValues_MPISBAIJ(Mat mat)
 49: {
 50:   Mat_MPISBAIJ *aij = (Mat_MPISBAIJ *)mat->data;
 51:   int          ierr;

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

 60: EXTERN_C_BEGIN
 63: int MatRetrieveValues_MPISBAIJ(Mat mat)
 64: {
 65:   Mat_MPISBAIJ *aij = (Mat_MPISBAIJ *)mat->data;
 66:   int          ierr;

 69:   MatRetrieveValues(aij->A);
 70:   MatRetrieveValues(aij->B);
 71:   return(0);
 72: }
 73: EXTERN_C_END


 76: #define CHUNKSIZE  10

 78: #define  MatSetValues_SeqSBAIJ_A_Private(row,col,value,addv) \
 79: { \
 80:  \
 81:     brow = row/bs;  \
 82:     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
 83:     rmax = aimax[brow]; nrow = ailen[brow]; \
 84:       bcol = col/bs; \
 85:       ridx = row % bs; cidx = col % bs; \
 86:       low = 0; high = nrow; \
 87:       while (high-low > 3) { \
 88:         t = (low+high)/2; \
 89:         if (rp[t] > bcol) high = t; \
 90:         else              low  = t; \
 91:       } \
 92:       for (_i=low; _i<high; _i++) { \
 93:         if (rp[_i] > bcol) break; \
 94:         if (rp[_i] == bcol) { \
 95:           bap  = ap +  bs2*_i + bs*cidx + ridx; \
 96:           if (addv == ADD_VALUES) *bap += value;  \
 97:           else                    *bap  = value;  \
 98:           goto a_noinsert; \
 99:         } \
100:       } \
101:       if (a->nonew == 1) goto a_noinsert; \
102:       else if (a->nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%d, %d) into matrix", row, col); \
103:       if (nrow >= rmax) { \
104:         /* there is no extra room in row, therefore enlarge */ \
105:         int       new_nz = ai[a->mbs] + CHUNKSIZE,len,*new_i,*new_j; \
106:         MatScalar *new_a; \
107:  \
108:         if (a->nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%d, %d) in the matrix", row, col); \
109:  \
110:         /* malloc new storage space */ \
111:         len   = new_nz*(sizeof(int)+bs2*sizeof(MatScalar))+(a->mbs+1)*sizeof(int); \
112:         PetscMalloc(len,&new_a); \
113:         new_j = (int*)(new_a + bs2*new_nz); \
114:         new_i = new_j + new_nz; \
115:  \
116:         /* copy over old data into new slots */ \
117:         for (ii=0; ii<brow+1; ii++) {new_i[ii] = ai[ii];} \
118:         for (ii=brow+1; ii<a->mbs+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;} \
119:         PetscMemcpy(new_j,aj,(ai[brow]+nrow)*sizeof(int)); \
120:         len = (new_nz - CHUNKSIZE - ai[brow] - nrow); \
121:         PetscMemcpy(new_j+ai[brow]+nrow+CHUNKSIZE,aj+ai[brow]+nrow,len*sizeof(int)); \
122:         PetscMemcpy(new_a,aa,(ai[brow]+nrow)*bs2*sizeof(MatScalar)); \
123:         PetscMemzero(new_a+bs2*(ai[brow]+nrow),bs2*CHUNKSIZE*sizeof(PetscScalar)); \
124:         PetscMemcpy(new_a+bs2*(ai[brow]+nrow+CHUNKSIZE), \
125:                     aa+bs2*(ai[brow]+nrow),bs2*len*sizeof(MatScalar));  \
126:         /* free up old matrix storage */ \
127:         PetscFree(a->a);  \
128:         if (!a->singlemalloc) { \
129:           PetscFree(a->i); \
130:           PetscFree(a->j);\
131:         } \
132:         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;  \
133:         a->singlemalloc = PETSC_TRUE; \
134:  \
135:         rp   = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
136:         rmax = aimax[brow] = aimax[brow] + CHUNKSIZE; \
137:         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + bs2*sizeof(MatScalar))); \
138:         a->maxnz += bs2*CHUNKSIZE; \
139:         a->reallocs++; \
140:         a->nz++; \
141:       } \
142:       N = nrow++ - 1;  \
143:       /* shift up all the later entries in this row */ \
144:       for (ii=N; ii>=_i; ii--) { \
145:         rp[ii+1] = rp[ii]; \
146:         PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar)); \
147:       } \
148:       if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar)); }  \
149:       rp[_i]                      = bcol;  \
150:       ap[bs2*_i + bs*cidx + ridx] = value;  \
151:       a_noinsert:; \
152:     ailen[brow] = nrow; \
153: } 
154: #ifndef MatSetValues_SeqBAIJ_B_Private
155: #define  MatSetValues_SeqSBAIJ_B_Private(row,col,value,addv) \
156: { \
157:     brow = row/bs;  \
158:     rp   = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
159:     rmax = bimax[brow]; nrow = bilen[brow]; \
160:       bcol = col/bs; \
161:       ridx = row % bs; cidx = col % bs; \
162:       low = 0; high = nrow; \
163:       while (high-low > 3) { \
164:         t = (low+high)/2; \
165:         if (rp[t] > bcol) high = t; \
166:         else              low  = t; \
167:       } \
168:       for (_i=low; _i<high; _i++) { \
169:         if (rp[_i] > bcol) break; \
170:         if (rp[_i] == bcol) { \
171:           bap  = ap +  bs2*_i + bs*cidx + ridx; \
172:           if (addv == ADD_VALUES) *bap += value;  \
173:           else                    *bap  = value;  \
174:           goto b_noinsert; \
175:         } \
176:       } \
177:       if (b->nonew == 1) goto b_noinsert; \
178:       else if (b->nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%d, %d) into matrix", row, col); \
179:       if (nrow >= rmax) { \
180:         /* there is no extra room in row, therefore enlarge */ \
181:         int       new_nz = bi[b->mbs] + CHUNKSIZE,len,*new_i,*new_j; \
182:         MatScalar *new_a; \
183:  \
184:         if (b->nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%d, %d) in the matrix", row, col); \
185:  \
186:         /* malloc new storage space */ \
187:         len   = new_nz*(sizeof(int)+bs2*sizeof(MatScalar))+(b->mbs+1)*sizeof(int); \
188:         PetscMalloc(len,&new_a); \
189:         new_j = (int*)(new_a + bs2*new_nz); \
190:         new_i = new_j + new_nz; \
191:  \
192:         /* copy over old data into new slots */ \
193:         for (ii=0; ii<brow+1; ii++) {new_i[ii] = bi[ii];} \
194:         for (ii=brow+1; ii<b->mbs+1; ii++) {new_i[ii] = bi[ii]+CHUNKSIZE;} \
195:         PetscMemcpy(new_j,bj,(bi[brow]+nrow)*sizeof(int)); \
196:         len  = (new_nz - CHUNKSIZE - bi[brow] - nrow); \
197:         PetscMemcpy(new_j+bi[brow]+nrow+CHUNKSIZE,bj+bi[brow]+nrow,len*sizeof(int)); \
198:         PetscMemcpy(new_a,ba,(bi[brow]+nrow)*bs2*sizeof(MatScalar)); \
199:         PetscMemzero(new_a+bs2*(bi[brow]+nrow),bs2*CHUNKSIZE*sizeof(MatScalar)); \
200:         PetscMemcpy(new_a+bs2*(bi[brow]+nrow+CHUNKSIZE), \
201:                     ba+bs2*(bi[brow]+nrow),bs2*len*sizeof(MatScalar));  \
202:         /* free up old matrix storage */ \
203:         PetscFree(b->a);  \
204:         if (!b->singlemalloc) { \
205:           PetscFree(b->i); \
206:           PetscFree(b->j); \
207:         } \
208:         ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j;  \
209:         b->singlemalloc = PETSC_TRUE; \
210:  \
211:         rp   = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
212:         rmax = bimax[brow] = bimax[brow] + CHUNKSIZE; \
213:         PetscLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + bs2*sizeof(MatScalar))); \
214:         b->maxnz += bs2*CHUNKSIZE; \
215:         b->reallocs++; \
216:         b->nz++; \
217:       } \
218:       N = nrow++ - 1;  \
219:       /* shift up all the later entries in this row */ \
220:       for (ii=N; ii>=_i; ii--) { \
221:         rp[ii+1] = rp[ii]; \
222:         PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar)); \
223:       } \
224:       if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));}  \
225:       rp[_i]                      = bcol;  \
226:       ap[bs2*_i + bs*cidx + ridx] = value;  \
227:       b_noinsert:; \
228:     bilen[brow] = nrow; \
229: } 
230: #endif

232: #if defined(PETSC_USE_MAT_SINGLE)
235: int MatSetValues_MPISBAIJ(Mat mat,int m,const int im[],int n,const int in[],const PetscScalar v[],InsertMode addv)
236: {
237:   Mat_MPISBAIJ *b = (Mat_MPISBAIJ*)mat->data;
238:   int          ierr,i,N = m*n;
239:   MatScalar    *vsingle;

242:   if (N > b->setvalueslen) {
243:     if (b->setvaluescopy) {PetscFree(b->setvaluescopy);}
244:     PetscMalloc(N*sizeof(MatScalar),&b->setvaluescopy);
245:     b->setvalueslen  = N;
246:   }
247:   vsingle = b->setvaluescopy;

249:   for (i=0; i<N; i++) {
250:     vsingle[i] = v[i];
251:   }
252:   MatSetValues_MPISBAIJ_MatScalar(mat,m,im,n,in,vsingle,addv);
253:   return(0);
254: }

258: int MatSetValuesBlocked_MPISBAIJ(Mat mat,int m,const int im[],int n,const int in[],const PetscScalar v[],InsertMode addv)
259: {
260:   Mat_MPIBAIJ *b = (Mat_MPIBAIJ*)mat->data;
261:   int         ierr,i,N = m*n*b->bs2;
262:   MatScalar   *vsingle;

265:   if (N > b->setvalueslen) {
266:     if (b->setvaluescopy) {PetscFree(b->setvaluescopy);}
267:     PetscMalloc(N*sizeof(MatScalar),&b->setvaluescopy);
268:     b->setvalueslen  = N;
269:   }
270:   vsingle = b->setvaluescopy;
271:   for (i=0; i<N; i++) {
272:     vsingle[i] = v[i];
273:   }
274:   MatSetValuesBlocked_MPISBAIJ_MatScalar(mat,m,im,n,in,vsingle,addv);
275:   return(0);
276: }

280: int MatSetValues_MPISBAIJ_HT(Mat mat,int m,const int im[],int n,const int in[],const PetscScalar v[],InsertMode addv)
281: {
282:   Mat_MPIBAIJ *b = (Mat_MPIBAIJ*)mat->data;
283:   int         ierr,i,N = m*n;
284:   MatScalar   *vsingle;

287:   SETERRQ(1,"Function not yet written for SBAIJ format");
288:   /* return(0); */
289: }

293: int MatSetValuesBlocked_MPISBAIJ_HT(Mat mat,int m,const int im[],int n,const int in[],const PetscScalar v[],InsertMode addv)
294: {
295:   Mat_MPIBAIJ *b = (Mat_MPIBAIJ*)mat->data;
296:   int         ierr,i,N = m*n*b->bs2;
297:   MatScalar   *vsingle;

300:   SETERRQ(1,"Function not yet written for SBAIJ format");
301:   /* return(0); */
302: }
303: #endif

305: /* Only add/insert a(i,j) with i<=j (blocks). 
306:    Any a(i,j) with i>j input by user is ingored. 
307: */
310: int MatSetValues_MPISBAIJ_MatScalar(Mat mat,int m,const int im[],int n,const int in[],const MatScalar v[],InsertMode addv)
311: {
312:   Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)mat->data;
313:   MatScalar    value;
314:   PetscTruth   roworiented = baij->roworiented;
315:   int          ierr,i,j,row,col;
316:   int          rstart_orig=baij->rstart_bs;
317:   int          rend_orig=baij->rend_bs,cstart_orig=baij->cstart_bs;
318:   int          cend_orig=baij->cend_bs,bs=baij->bs;

320:   /* Some Variables required in the macro */
321:   Mat          A = baij->A;
322:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)(A)->data;
323:   int          *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
324:   MatScalar    *aa=a->a;

326:   Mat          B = baij->B;
327:   Mat_SeqBAIJ  *b = (Mat_SeqBAIJ*)(B)->data;
328:   int          *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
329:   MatScalar    *ba=b->a;

331:   int          *rp,ii,nrow,_i,rmax,N,brow,bcol;
332:   int          low,high,t,ridx,cidx,bs2=a->bs2;
333:   MatScalar    *ap,*bap;

335:   /* for stash */
336:   int          n_loc, *in_loc=0;
337:   MatScalar    *v_loc=0;


341:   if(!baij->donotstash){
342:     PetscMalloc(n*sizeof(int),&in_loc);
343:     PetscMalloc(n*sizeof(MatScalar),&v_loc);
344:   }

346:   for (i=0; i<m; i++) {
347:     if (im[i] < 0) continue;
348: #if defined(PETSC_USE_BOPT_g)
349:     if (im[i] >= mat->M) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",im[i],mat->M-1);
350: #endif
351:     if (im[i] >= rstart_orig && im[i] < rend_orig) { /* this processor entry */
352:       row = im[i] - rstart_orig;              /* local row index */
353:       for (j=0; j<n; j++) {
354:         if (im[i]/bs > in[j]/bs) continue;    /* ignore lower triangular blocks */
355:         if (in[j] >= cstart_orig && in[j] < cend_orig){  /* diag entry (A) */
356:           col = in[j] - cstart_orig;          /* local col index */
357:           brow = row/bs; bcol = col/bs;
358:           if (brow > bcol) continue;  /* ignore lower triangular blocks of A */
359:           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
360:           MatSetValues_SeqSBAIJ_A_Private(row,col,value,addv);
361:           /* MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv); */
362:         } else if (in[j] < 0) continue;
363: #if defined(PETSC_USE_BOPT_g)
364:         else if (in[j] >= mat->N) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[j],mat->N-1);}
365: #endif
366:         else {  /* off-diag entry (B) */
367:           if (mat->was_assembled) {
368:             if (!baij->colmap) {
369:               CreateColmap_MPIBAIJ_Private(mat);
370:             }
371: #if defined (PETSC_USE_CTABLE)
372:             PetscTableFind(baij->colmap,in[j]/bs + 1,&col);
373:             col  = col - 1;
374: #else
375:             col = baij->colmap[in[j]/bs] - 1;
376: #endif
377:             if (col < 0 && !((Mat_SeqSBAIJ*)(baij->A->data))->nonew) {
378:               DisAssemble_MPISBAIJ(mat);
379:               col =  in[j];
380:               /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
381:               B = baij->B;
382:               b = (Mat_SeqBAIJ*)(B)->data;
383:               bimax=b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
384:               ba=b->a;
385:             } else col += in[j]%bs;
386:           } else col = in[j];
387:           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
388:           MatSetValues_SeqSBAIJ_B_Private(row,col,value,addv);
389:           /* MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv); */
390:         }
391:       }
392:     } else {  /* off processor entry */
393:       if (!baij->donotstash) {
394:         n_loc = 0;
395:         for (j=0; j<n; j++){
396:           if (im[i]/bs > in[j]/bs) continue; /* ignore lower triangular blocks */
397:           in_loc[n_loc] = in[j];
398:           if (roworiented) {
399:             v_loc[n_loc] = v[i*n+j];
400:           } else {
401:             v_loc[n_loc] = v[j*m+i];
402:           }
403:           n_loc++;
404:         }
405:         MatStashValuesRow_Private(&mat->stash,im[i],n_loc,in_loc,v_loc);
406:       }
407:     }
408:   }

410:   if(!baij->donotstash){
411:     PetscFree(in_loc);
412:     PetscFree(v_loc);
413:   }
414:   return(0);
415: }

419: int MatSetValuesBlocked_MPISBAIJ_MatScalar(Mat mat,int m,const int im[],int n,const int in[],const MatScalar v[],InsertMode addv)
420: {
421:   Mat_MPISBAIJ    *baij = (Mat_MPISBAIJ*)mat->data;
422:   const MatScalar *value;
423:   MatScalar       *barray=baij->barray;
424:   PetscTruth      roworiented = baij->roworiented;
425:   int             ierr,i,j,ii,jj,row,col,rstart=baij->rstart;
426:   int             rend=baij->rend,cstart=baij->cstart,stepval;
427:   int             cend=baij->cend,bs=baij->bs,bs2=baij->bs2;

430:   if(!barray) {
431:     PetscMalloc(bs2*sizeof(MatScalar),&barray);
432:     baij->barray = barray;
433:   }

435:   if (roworiented) {
436:     stepval = (n-1)*bs;
437:   } else {
438:     stepval = (m-1)*bs;
439:   }
440:   for (i=0; i<m; i++) {
441:     if (im[i] < 0) continue;
442: #if defined(PETSC_USE_BOPT_g)
443:     if (im[i] >= baij->Mbs) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large, row %d max %d",im[i],baij->Mbs-1);
444: #endif
445:     if (im[i] >= rstart && im[i] < rend) {
446:       row = im[i] - rstart;
447:       for (j=0; j<n; j++) {
448:         /* If NumCol = 1 then a copy is not required */
449:         if ((roworiented) && (n == 1)) {
450:           barray = (MatScalar*) v + i*bs2;
451:         } else if((!roworiented) && (m == 1)) {
452:           barray = (MatScalar*) v + j*bs2;
453:         } else { /* Here a copy is required */
454:           if (roworiented) {
455:             value = v + i*(stepval+bs)*bs + j*bs;
456:           } else {
457:             value = v + j*(stepval+bs)*bs + i*bs;
458:           }
459:           for (ii=0; ii<bs; ii++,value+=stepval) {
460:             for (jj=0; jj<bs; jj++) {
461:               *barray++  = *value++;
462:             }
463:           }
464:           barray -=bs2;
465:         }
466: 
467:         if (in[j] >= cstart && in[j] < cend){
468:           col  = in[j] - cstart;
469:           MatSetValuesBlocked_SeqSBAIJ(baij->A,1,&row,1,&col,barray,addv);
470:         }
471:         else if (in[j] < 0) continue;
472: #if defined(PETSC_USE_BOPT_g)
473:         else if (in[j] >= baij->Nbs) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large, col %d max %d",in[j],baij->Nbs-1);}
474: #endif
475:         else {
476:           if (mat->was_assembled) {
477:             if (!baij->colmap) {
478:               CreateColmap_MPIBAIJ_Private(mat);
479:             }

481: #if defined(PETSC_USE_BOPT_g)
482: #if defined (PETSC_USE_CTABLE)
483:             { int data;
484:               PetscTableFind(baij->colmap,in[j]+1,&data);
485:               if ((data - 1) % bs) SETERRQ(PETSC_ERR_PLIB,"Incorrect colmap");
486:             }
487: #else
488:             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_ERR_PLIB,"Incorrect colmap");
489: #endif
490: #endif
491: #if defined (PETSC_USE_CTABLE)
492:             PetscTableFind(baij->colmap,in[j]+1,&col);
493:             col  = (col - 1)/bs;
494: #else
495:             col = (baij->colmap[in[j]] - 1)/bs;
496: #endif
497:             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
498:               DisAssemble_MPISBAIJ(mat);
499:               col =  in[j];
500:             }
501:           }
502:           else col = in[j];
503:           MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);
504:         }
505:       }
506:     } else {
507:       if (!baij->donotstash) {
508:         if (roworiented) {
509:           MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
510:         } else {
511:           MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
512:         }
513:       }
514:     }
515:   }
516:   return(0);
517: }

519: #define HASH_KEY 0.6180339887
520: #define HASH(size,key,tmp) (tmp = (key)*HASH_KEY,(int)((size)*(tmp-(int)tmp)))
521: /* #define HASH(size,key) ((int)((size)*fmod(((key)*HASH_KEY),1))) */
522: /* #define HASH(size,key,tmp) ((int)((size)*fmod(((key)*HASH_KEY),1))) */
525: int MatSetValues_MPISBAIJ_HT_MatScalar(Mat mat,int m,const int im[],int n,const int in[],const MatScalar v[],InsertMode addv)
526: {
528:   SETERRQ(1,"Function not yet written for SBAIJ format");
529:   /* return(0); */
530: }

534: int MatSetValuesBlocked_MPISBAIJ_HT_MatScalar(Mat mat,int m,const int im[],int n,const int in[],const MatScalar v[],InsertMode addv)
535: {
537:   SETERRQ(1,"Function not yet written for SBAIJ format");
538:   /* return(0); */
539: }

543: int MatGetValues_MPISBAIJ(Mat mat,int m,const int idxm[],int n,const int idxn[],PetscScalar v[])
544: {
545:   Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)mat->data;
546:   int          bs=baij->bs,ierr,i,j,bsrstart = baij->rstart*bs,bsrend = baij->rend*bs;
547:   int          bscstart = baij->cstart*bs,bscend = baij->cend*bs,row,col,data;

550:   for (i=0; i<m; i++) {
551:     if (idxm[i] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",idxm[i]);
552:     if (idxm[i] >= mat->M) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",idxm[i],mat->M-1);
553:     if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
554:       row = idxm[i] - bsrstart;
555:       for (j=0; j<n; j++) {
556:         if (idxn[j] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column %d",idxn[j]);
557:         if (idxn[j] >= mat->N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",idxn[j],mat->N-1);
558:         if (idxn[j] >= bscstart && idxn[j] < bscend){
559:           col = idxn[j] - bscstart;
560:           MatGetValues_SeqSBAIJ(baij->A,1,&row,1,&col,v+i*n+j);
561:         } else {
562:           if (!baij->colmap) {
563:             CreateColmap_MPIBAIJ_Private(mat);
564:           }
565: #if defined (PETSC_USE_CTABLE)
566:           PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);
567:           data --;
568: #else
569:           data = baij->colmap[idxn[j]/bs]-1;
570: #endif
571:           if((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
572:           else {
573:             col  = data + idxn[j]%bs;
574:             MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);
575:           }
576:         }
577:       }
578:     } else {
579:       SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
580:     }
581:   }
582:  return(0);
583: }

587: int MatNorm_MPISBAIJ(Mat mat,NormType type,PetscReal *norm)
588: {
589:   Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)mat->data;
590:   /* Mat_SeqSBAIJ *amat = (Mat_SeqSBAIJ*)baij->A->data; */
591:   /* Mat_SeqBAIJ  *bmat = (Mat_SeqBAIJ*)baij->B->data; */
592:   int        ierr;
593:   PetscReal  sum[2],*lnorm2;

596:   if (baij->size == 1) {
597:      MatNorm(baij->A,type,norm);
598:   } else {
599:     if (type == NORM_FROBENIUS) {
600:       PetscMalloc(2*sizeof(PetscReal),&lnorm2);
601:        MatNorm(baij->A,type,lnorm2);
602:       *lnorm2 = (*lnorm2)*(*lnorm2); lnorm2++;            /* squar power of norm(A) */
603:        MatNorm(baij->B,type,lnorm2);
604:       *lnorm2 = (*lnorm2)*(*lnorm2); lnorm2--;             /* squar power of norm(B) */
605:       /*
606:       MPI_Comm_rank(mat->comm,&rank);
607:       PetscSynchronizedPrintf(mat->comm,"[%d], lnorm2=%g, %g\n",rank,lnorm2[0],lnorm2[1]);
608:       */
609:       MPI_Allreduce(lnorm2,&sum,2,MPIU_REAL,MPI_SUM,mat->comm);
610:       /*
611:       PetscSynchronizedPrintf(mat->comm,"[%d], sum=%g, %g\n",rank,sum[0],sum[1]);
612:       PetscSynchronizedFlush(mat->comm); */
613: 
614:       *norm = sqrt(sum[0] + 2*sum[1]);
615:       PetscFree(lnorm2);
616:     } else {
617:       SETERRQ(PETSC_ERR_SUP,"No support for this norm yet");
618:     }
619:   }
620:   return(0);
621: }

623: /*
624:   Creates the hash table, and sets the table 
625:   This table is created only once. 
626:   If new entried need to be added to the matrix
627:   then the hash table has to be destroyed and
628:   recreated.
629: */
632: int MatCreateHashTable_MPISBAIJ_Private(Mat mat,PetscReal factor)
633: {
635:   SETERRQ(1,"Function not yet written for SBAIJ format");
636:   /* return(0); */
637: }

641: int MatAssemblyBegin_MPISBAIJ(Mat mat,MatAssemblyType mode)
642: {
643:   Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)mat->data;
644:   int         ierr,nstash,reallocs;
645:   InsertMode  addv;

648:   if (baij->donotstash) {
649:     return(0);
650:   }

652:   /* make sure all processors are either in INSERTMODE or ADDMODE */
653:   MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);
654:   if (addv == (ADD_VALUES|INSERT_VALUES)) {
655:     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
656:   }
657:   mat->insertmode = addv; /* in case this processor had no cache */

659:   MatStashScatterBegin_Private(&mat->stash,baij->rowners_bs);
660:   MatStashScatterBegin_Private(&mat->bstash,baij->rowners);
661:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
662:   PetscLogInfo(0,"MatAssemblyBegin_MPISBAIJ:Stash has %d entries,uses %d mallocs.\n",nstash,reallocs);
663:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
664:   PetscLogInfo(0,"MatAssemblyBegin_MPISBAIJ:Block-Stash has %d entries, uses %d mallocs.\n",nstash,reallocs);
665:   return(0);
666: }

670: int MatAssemblyEnd_MPISBAIJ(Mat mat,MatAssemblyType mode)
671: {
672:   Mat_MPISBAIJ *baij=(Mat_MPISBAIJ*)mat->data;
673:   Mat_SeqSBAIJ  *a=(Mat_SeqSBAIJ*)baij->A->data;
674:   Mat_SeqBAIJ  *b=(Mat_SeqBAIJ*)baij->B->data;
675:   int         i,j,rstart,ncols,n,ierr,flg,bs2=baij->bs2;
676:   int         *row,*col,other_disassembled;
677:   PetscTruth  r1,r2,r3;
678:   MatScalar   *val;
679:   InsertMode  addv = mat->insertmode;


683:   if (!baij->donotstash) {
684:     while (1) {
685:       MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
686:       /*
687:       PetscSynchronizedPrintf(mat->comm,"[%d]: in AssemblyEnd, stash, flg=%d\n",rank,flg);
688:       PetscSynchronizedFlush(mat->comm); 
689:       */
690:       if (!flg) break;

692:       for (i=0; i<n;) {
693:         /* Now identify the consecutive vals belonging to the same row */
694:         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
695:         if (j < n) ncols = j-i;
696:         else       ncols = n-i;
697:         /* Now assemble all these values with a single function call */
698:         MatSetValues_MPISBAIJ_MatScalar(mat,1,row+i,ncols,col+i,val+i,addv);
699:         i = j;
700:       }
701:     }
702:     MatStashScatterEnd_Private(&mat->stash);
703:     /* Now process the block-stash. Since the values are stashed column-oriented,
704:        set the roworiented flag to column oriented, and after MatSetValues() 
705:        restore the original flags */
706:     r1 = baij->roworiented;
707:     r2 = a->roworiented;
708:     r3 = b->roworiented;
709:     baij->roworiented = PETSC_FALSE;
710:     a->roworiented    = PETSC_FALSE;
711:     b->roworiented    = PETSC_FALSE;
712:     while (1) {
713:       MatStashScatterGetMesg_Private(&mat->bstash,&n,&row,&col,&val,&flg);
714:       if (!flg) break;
715: 
716:       for (i=0; i<n;) {
717:         /* Now identify the consecutive vals belonging to the same row */
718:         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
719:         if (j < n) ncols = j-i;
720:         else       ncols = n-i;
721:         MatSetValuesBlocked_MPISBAIJ_MatScalar(mat,1,row+i,ncols,col+i,val+i*bs2,addv);
722:         i = j;
723:       }
724:     }
725:     MatStashScatterEnd_Private(&mat->bstash);
726:     baij->roworiented = r1;
727:     a->roworiented    = r2;
728:     b->roworiented    = r3;
729:   }

731:   MatAssemblyBegin(baij->A,mode);
732:   MatAssemblyEnd(baij->A,mode);

734:   /* determine if any processor has disassembled, if so we must 
735:      also disassemble ourselfs, in order that we may reassemble. */
736:   /*
737:      if nonzero structure of submatrix B cannot change then we know that
738:      no processor disassembled thus we can skip this stuff
739:   */
740:   if (!((Mat_SeqBAIJ*)baij->B->data)->nonew)  {
741:     MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);
742:     if (mat->was_assembled && !other_disassembled) {
743:       DisAssemble_MPISBAIJ(mat);
744:     }
745:   }

747:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
748:     MatSetUpMultiply_MPISBAIJ(mat); /* setup Mvctx and sMvctx */
749:   }
750:   MatAssemblyBegin(baij->B,mode);
751:   MatAssemblyEnd(baij->B,mode);
752: 
753: #if defined(PETSC_USE_BOPT_g)
754:   if (baij->ht && mode== MAT_FINAL_ASSEMBLY) {
755:     PetscLogInfo(0,"MatAssemblyEnd_MPISBAIJ:Average Hash Table Search in MatSetValues = %5.2f\n",((PetscReal)baij->ht_total_ct)/baij->ht_insert_ct);
756:     baij->ht_total_ct  = 0;
757:     baij->ht_insert_ct = 0;
758:   }
759: #endif
760:   if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) {
761:     MatCreateHashTable_MPISBAIJ_Private(mat,baij->ht_fact);
762:     mat->ops->setvalues        = MatSetValues_MPISBAIJ_HT;
763:     mat->ops->setvaluesblocked = MatSetValuesBlocked_MPISBAIJ_HT;
764:   }

766:   if (baij->rowvalues) {
767:     PetscFree(baij->rowvalues);
768:     baij->rowvalues = 0;
769:   }

771:   return(0);
772: }

776: static int MatView_MPISBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
777: {
778:   Mat_MPISBAIJ      *baij = (Mat_MPISBAIJ*)mat->data;
779:   int               ierr,bs = baij->bs,size = baij->size,rank = baij->rank;
780:   PetscTruth        isascii,isdraw;
781:   PetscViewer       sviewer;
782:   PetscViewerFormat format;

785:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
786:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
787:   if (isascii) {
788:     PetscViewerGetFormat(viewer,&format);
789:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
790:       MatInfo info;
791:       MPI_Comm_rank(mat->comm,&rank);
792:       MatGetInfo(mat,MAT_LOCAL,&info);
793:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d bs %d mem %d\n",
794:               rank,mat->m,(int)info.nz_used*bs,(int)info.nz_allocated*bs,
795:               baij->bs,(int)info.memory);
796:       MatGetInfo(baij->A,MAT_LOCAL,&info);
797:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used*bs);
798:       MatGetInfo(baij->B,MAT_LOCAL,&info);
799:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used*bs);
800:       PetscViewerFlush(viewer);
801:       VecScatterView(baij->Mvctx,viewer);
802:       return(0);
803:     } else if (format == PETSC_VIEWER_ASCII_INFO) {
804:       PetscViewerASCIIPrintf(viewer,"  block size is %d\n",bs);
805:       return(0);
806:     }
807:   }

809:   if (isdraw) {
810:     PetscDraw       draw;
811:     PetscTruth isnull;
812:     PetscViewerDrawGetDraw(viewer,0,&draw);
813:     PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
814:   }

816:   if (size == 1) {
817:     PetscObjectSetName((PetscObject)baij->A,mat->name);
818:     MatView(baij->A,viewer);
819:   } else {
820:     /* assemble the entire matrix onto first processor. */
821:     Mat         A;
822:     Mat_SeqSBAIJ *Aloc;
823:     Mat_SeqBAIJ *Bloc;
824:     int         M = mat->M,N = mat->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
825:     MatScalar   *a;

827:     /* Should this be the same type as mat? */
828:     if (!rank) {
829:       MatCreate(mat->comm,M,N,M,N,&A);
830:     } else {
831:       MatCreate(mat->comm,0,0,M,N,&A);
832:     }
833:     MatSetType(A,MATMPISBAIJ);
834:     MatMPISBAIJSetPreallocation(A,baij->bs,0,PETSC_NULL,0,PETSC_NULL);
835:     PetscLogObjectParent(mat,A);

837:     /* copy over the A part */
838:     Aloc  = (Mat_SeqSBAIJ*)baij->A->data;
839:     ai    = Aloc->i; aj = Aloc->j; a = Aloc->a;
840:     PetscMalloc(bs*sizeof(int),&rvals);

842:     for (i=0; i<mbs; i++) {
843:       rvals[0] = bs*(baij->rstart + i);
844:       for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
845:       for (j=ai[i]; j<ai[i+1]; j++) {
846:         col = (baij->cstart+aj[j])*bs;
847:         for (k=0; k<bs; k++) {
848:           MatSetValues_MPISBAIJ_MatScalar(A,bs,rvals,1,&col,a,INSERT_VALUES);
849:           col++; a += bs;
850:         }
851:       }
852:     }
853:     /* copy over the B part */
854:     Bloc = (Mat_SeqBAIJ*)baij->B->data;
855:     ai = Bloc->i; aj = Bloc->j; a = Bloc->a;
856:     for (i=0; i<mbs; i++) {
857:       rvals[0] = bs*(baij->rstart + i);
858:       for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
859:       for (j=ai[i]; j<ai[i+1]; j++) {
860:         col = baij->garray[aj[j]]*bs;
861:         for (k=0; k<bs; k++) {
862:           MatSetValues_MPISBAIJ_MatScalar(A,bs,rvals,1,&col,a,INSERT_VALUES);
863:           col++; a += bs;
864:         }
865:       }
866:     }
867:     PetscFree(rvals);
868:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
869:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
870:     /* 
871:        Everyone has to call to draw the matrix since the graphics waits are
872:        synchronized across all processors that share the PetscDraw object
873:     */
874:     PetscViewerGetSingleton(viewer,&sviewer);
875:     if (!rank) {
876:       PetscObjectSetName((PetscObject)((Mat_MPISBAIJ*)(A->data))->A,mat->name);
877:       MatView(((Mat_MPISBAIJ*)(A->data))->A,sviewer);
878:     }
879:     PetscViewerRestoreSingleton(viewer,&sviewer);
880:     MatDestroy(A);
881:   }
882:   return(0);
883: }

887: int MatView_MPISBAIJ(Mat mat,PetscViewer viewer)
888: {
889:   int        ierr;
890:   PetscTruth isascii,isdraw,issocket,isbinary;

893:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
894:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
895:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);
896:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
897:   if (isascii || isdraw || issocket || isbinary) {
898:     MatView_MPISBAIJ_ASCIIorDraworSocket(mat,viewer);
899:   } else {
900:     SETERRQ1(1,"Viewer type %s not supported by MPISBAIJ matrices",((PetscObject)viewer)->type_name);
901:   }
902:   return(0);
903: }

907: int MatDestroy_MPISBAIJ(Mat mat)
908: {
909:   Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)mat->data;
910:   int         ierr;

913: #if defined(PETSC_USE_LOG)
914:   PetscLogObjectState((PetscObject)mat,"Rows=%d,Cols=%d",mat->M,mat->N);
915: #endif
916:   MatStashDestroy_Private(&mat->stash);
917:   MatStashDestroy_Private(&mat->bstash);
918:   PetscFree(baij->rowners);
919:   MatDestroy(baij->A);
920:   MatDestroy(baij->B);
921: #if defined (PETSC_USE_CTABLE)
922:   if (baij->colmap) {PetscTableDelete(baij->colmap);}
923: #else
924:   if (baij->colmap) {PetscFree(baij->colmap);}
925: #endif
926:   if (baij->garray) {PetscFree(baij->garray);}
927:   if (baij->lvec)   {VecDestroy(baij->lvec);}
928:   if (baij->Mvctx)  {VecScatterDestroy(baij->Mvctx);}
929:   if (baij->slvec0) {
930:     VecDestroy(baij->slvec0);
931:     VecDestroy(baij->slvec0b);
932:   }
933:   if (baij->slvec1) {
934:     VecDestroy(baij->slvec1);
935:     VecDestroy(baij->slvec1a);
936:     VecDestroy(baij->slvec1b);
937:   }
938:   if (baij->sMvctx)  {VecScatterDestroy(baij->sMvctx);}
939:   if (baij->rowvalues) {PetscFree(baij->rowvalues);}
940:   if (baij->barray) {PetscFree(baij->barray);}
941:   if (baij->hd) {PetscFree(baij->hd);}
942: #if defined(PETSC_USE_MAT_SINGLE)
943:   if (baij->setvaluescopy) {PetscFree(baij->setvaluescopy);}
944: #endif
945:   PetscFree(baij);
946:   return(0);
947: }

951: int MatMult_MPISBAIJ(Mat A,Vec xx,Vec yy)
952: {
953:   Mat_MPISBAIJ *a = (Mat_MPISBAIJ*)A->data;
954:   int         ierr,nt,mbs=a->mbs,bs=a->bs;
955:   PetscScalar *x,*from,zero=0.0;
956: 
958:   VecGetLocalSize(xx,&nt);
959:   if (nt != A->n) {
960:     SETERRQ(PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");
961:   }
962:   VecGetLocalSize(yy,&nt);
963:   if (nt != A->m) {
964:     SETERRQ(PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");
965:   }

967:   /* diagonal part */
968:   (*a->A->ops->mult)(a->A,xx,a->slvec1a);
969:   VecSet(&zero,a->slvec1b);

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

974:   /* copy x into the vec slvec0 */
975:   VecGetArray(a->slvec0,&from);
976:   VecGetArray(xx,&x);
977:   PetscMemcpy(from,x,bs*mbs*sizeof(MatScalar));
978:   VecRestoreArray(a->slvec0,&from);
979: 
980:   VecScatterBegin(a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD,a->sMvctx);
981:   VecRestoreArray(xx,&x);
982:   VecScatterEnd(a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD,a->sMvctx);
983: 
984:   /* supperdiagonal part */
985:   (*a->B->ops->multadd)(a->B,a->slvec1b,a->slvec1a,yy);
986: 
987:   return(0);
988: }

992: int MatMult_MPISBAIJ_2comm(Mat A,Vec xx,Vec yy)
993: {
994:   Mat_MPISBAIJ *a = (Mat_MPISBAIJ*)A->data;
995:   int         ierr,nt;

998:   VecGetLocalSize(xx,&nt);
999:   if (nt != A->n) {
1000:     SETERRQ(PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");
1001:   }
1002:   VecGetLocalSize(yy,&nt);
1003:   if (nt != A->m) {
1004:     SETERRQ(PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");
1005:   }

1007:   VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
1008:   /* do diagonal part */
1009:   (*a->A->ops->mult)(a->A,xx,yy);
1010:   /* do supperdiagonal part */
1011:   VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
1012:   (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
1013:   /* do subdiagonal part */
1014:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1015:   VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
1016:   VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);

1018:   return(0);
1019: }

1023: int MatMultAdd_MPISBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1024: {
1025:   Mat_MPISBAIJ *a = (Mat_MPISBAIJ*)A->data;
1026:   int          ierr,mbs=a->mbs,bs=a->bs;
1027:   PetscScalar  *x,*from,zero=0.0;
1028: 
1030:   /*
1031:   PetscSynchronizedPrintf(A->comm," MatMultAdd is called ...\n");
1032:   PetscSynchronizedFlush(A->comm);
1033:   */
1034:   /* diagonal part */
1035:   (*a->A->ops->multadd)(a->A,xx,yy,a->slvec1a);
1036:   VecSet(&zero,a->slvec1b);

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

1041:   /* copy x into the vec slvec0 */
1042:   VecGetArray(a->slvec0,&from);
1043:   VecGetArray(xx,&x);
1044:   PetscMemcpy(from,x,bs*mbs*sizeof(MatScalar));
1045:   VecRestoreArray(a->slvec0,&from);
1046: 
1047:   VecScatterBegin(a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD,a->sMvctx);
1048:   VecRestoreArray(xx,&x);
1049:   VecScatterEnd(a->slvec0,a->slvec1,ADD_VALUES,SCATTER_FORWARD,a->sMvctx);
1050: 
1051:   /* supperdiagonal part */
1052:   (*a->B->ops->multadd)(a->B,a->slvec1b,a->slvec1a,zz);
1053: 
1054:   return(0);
1055: }

1059: int MatMultAdd_MPISBAIJ_2comm(Mat A,Vec xx,Vec yy,Vec zz)
1060: {
1061:   Mat_MPISBAIJ *a = (Mat_MPISBAIJ*)A->data;
1062:   int        ierr;

1065:   VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
1066:   /* do diagonal part */
1067:   (*a->A->ops->multadd)(a->A,xx,yy,zz);
1068:   /* do supperdiagonal part */
1069:   VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
1070:   (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);

1072:   /* do subdiagonal part */
1073:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1074:   VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
1075:   VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);

1077:   return(0);
1078: }

1082: int MatMultTranspose_MPISBAIJ(Mat A,Vec xx,Vec yy)
1083: {

1087:   MatMult(A,xx,yy);
1088:   return(0);
1089: }

1093: int MatMultTransposeAdd_MPISBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1094: {

1098:   MatMultAdd(A,xx,yy,zz);
1099:   return(0);
1100: }

1102: /*
1103:   This only works correctly for square matrices where the subblock A->A is the 
1104:    diagonal block
1105: */
1108: int MatGetDiagonal_MPISBAIJ(Mat A,Vec v)
1109: {
1110:   Mat_MPISBAIJ *a = (Mat_MPISBAIJ*)A->data;
1111:   int         ierr;

1114:   /* if (a->M != a->N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block"); */
1115:   MatGetDiagonal(a->A,v);
1116:   return(0);
1117: }

1121: int MatScale_MPISBAIJ(const PetscScalar *aa,Mat A)
1122: {
1123:   Mat_MPISBAIJ *a = (Mat_MPISBAIJ*)A->data;
1124:   int         ierr;

1127:   MatScale(aa,a->A);
1128:   MatScale(aa,a->B);
1129:   return(0);
1130: }

1134: int MatGetRow_MPISBAIJ(Mat matin,int row,int *nz,int **idx,PetscScalar **v)
1135: {
1136:   Mat_MPISBAIJ   *mat = (Mat_MPISBAIJ*)matin->data;
1137:   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
1138:   int            bs = mat->bs,bs2 = mat->bs2,i,ierr,*cworkA,*cworkB,**pcA,**pcB;
1139:   int            nztot,nzA,nzB,lrow,brstart = mat->rstart*bs,brend = mat->rend*bs;
1140:   int            *cmap,*idx_p,cstart = mat->cstart;

1143:   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
1144:   mat->getrowactive = PETSC_TRUE;

1146:   if (!mat->rowvalues && (idx || v)) {
1147:     /*
1148:         allocate enough space to hold information from the longest row.
1149:     */
1150:     Mat_SeqSBAIJ *Aa = (Mat_SeqSBAIJ*)mat->A->data;
1151:     Mat_SeqBAIJ  *Ba = (Mat_SeqBAIJ*)mat->B->data;
1152:     int     max = 1,mbs = mat->mbs,tmp;
1153:     for (i=0; i<mbs; i++) {
1154:       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; /* row length */
1155:       if (max < tmp) { max = tmp; }
1156:     }
1157:     PetscMalloc(max*bs2*(sizeof(int)+sizeof(PetscScalar)),&mat->rowvalues);
1158:     mat->rowindices = (int*)(mat->rowvalues + max*bs2);
1159:   }
1160: 
1161:   if (row < brstart || row >= brend) SETERRQ(PETSC_ERR_SUP,"Only local rows")
1162:   lrow = row - brstart;  /* local row index */

1164:   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1165:   if (!v)   {pvA = 0; pvB = 0;}
1166:   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1167:   (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1168:   (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1169:   nztot = nzA + nzB;

1171:   cmap  = mat->garray;
1172:   if (v  || idx) {
1173:     if (nztot) {
1174:       /* Sort by increasing column numbers, assuming A and B already sorted */
1175:       int imark = -1;
1176:       if (v) {
1177:         *v = v_p = mat->rowvalues;
1178:         for (i=0; i<nzB; i++) {
1179:           if (cmap[cworkB[i]/bs] < cstart)   v_p[i] = vworkB[i];
1180:           else break;
1181:         }
1182:         imark = i;
1183:         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1184:         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1185:       }
1186:       if (idx) {
1187:         *idx = idx_p = mat->rowindices;
1188:         if (imark > -1) {
1189:           for (i=0; i<imark; i++) {
1190:             idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1191:           }
1192:         } else {
1193:           for (i=0; i<nzB; i++) {
1194:             if (cmap[cworkB[i]/bs] < cstart)
1195:               idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1196:             else break;
1197:           }
1198:           imark = i;
1199:         }
1200:         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart*bs + cworkA[i];
1201:         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1202:       }
1203:     } else {
1204:       if (idx) *idx = 0;
1205:       if (v)   *v   = 0;
1206:     }
1207:   }
1208:   *nz = nztot;
1209:   (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1210:   (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1211:   return(0);
1212: }

1216: int MatRestoreRow_MPISBAIJ(Mat mat,int row,int *nz,int **idx,PetscScalar **v)
1217: {
1218:   Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)mat->data;

1221:   if (baij->getrowactive == PETSC_FALSE) {
1222:     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
1223:   }
1224:   baij->getrowactive = PETSC_FALSE;
1225:   return(0);
1226: }

1230: int MatGetBlockSize_MPISBAIJ(Mat mat,int *bs)
1231: {
1232:   Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)mat->data;

1235:   *bs = baij->bs;
1236:   return(0);
1237: }

1241: int MatZeroEntries_MPISBAIJ(Mat A)
1242: {
1243:   Mat_MPISBAIJ *l = (Mat_MPISBAIJ*)A->data;
1244:   int         ierr;

1247:   MatZeroEntries(l->A);
1248:   MatZeroEntries(l->B);
1249:   return(0);
1250: }

1254: int MatGetInfo_MPISBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1255: {
1256:   Mat_MPISBAIJ *a = (Mat_MPISBAIJ*)matin->data;
1257:   Mat         A = a->A,B = a->B;
1258:   int         ierr;
1259:   PetscReal   isend[5],irecv[5];

1262:   info->block_size     = (PetscReal)a->bs;
1263:   MatGetInfo(A,MAT_LOCAL,info);
1264:   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1265:   isend[3] = info->memory;  isend[4] = info->mallocs;
1266:   MatGetInfo(B,MAT_LOCAL,info);
1267:   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1268:   isend[3] += info->memory;  isend[4] += info->mallocs;
1269:   if (flag == MAT_LOCAL) {
1270:     info->nz_used      = isend[0];
1271:     info->nz_allocated = isend[1];
1272:     info->nz_unneeded  = isend[2];
1273:     info->memory       = isend[3];
1274:     info->mallocs      = isend[4];
1275:   } else if (flag == MAT_GLOBAL_MAX) {
1276:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);
1277:     info->nz_used      = irecv[0];
1278:     info->nz_allocated = irecv[1];
1279:     info->nz_unneeded  = irecv[2];
1280:     info->memory       = irecv[3];
1281:     info->mallocs      = irecv[4];
1282:   } else if (flag == MAT_GLOBAL_SUM) {
1283:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);
1284:     info->nz_used      = irecv[0];
1285:     info->nz_allocated = irecv[1];
1286:     info->nz_unneeded  = irecv[2];
1287:     info->memory       = irecv[3];
1288:     info->mallocs      = irecv[4];
1289:   } else {
1290:     SETERRQ1(1,"Unknown MatInfoType argument %d",flag);
1291:   }
1292:   info->rows_global       = (PetscReal)A->M;
1293:   info->columns_global    = (PetscReal)A->N;
1294:   info->rows_local        = (PetscReal)A->m;
1295:   info->columns_local     = (PetscReal)A->N;
1296:   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
1297:   info->fill_ratio_needed = 0;
1298:   info->factor_mallocs    = 0;
1299:   return(0);
1300: }

1304: int MatSetOption_MPISBAIJ(Mat A,MatOption op)
1305: {
1306:   Mat_MPISBAIJ *a = (Mat_MPISBAIJ*)A->data;
1307:   int         ierr;

1310:   switch (op) {
1311:   case MAT_NO_NEW_NONZERO_LOCATIONS:
1312:   case MAT_YES_NEW_NONZERO_LOCATIONS:
1313:   case MAT_COLUMNS_UNSORTED:
1314:   case MAT_COLUMNS_SORTED:
1315:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1316:   case MAT_KEEP_ZEROED_ROWS:
1317:   case MAT_NEW_NONZERO_LOCATION_ERR:
1318:     MatSetOption(a->A,op);
1319:     MatSetOption(a->B,op);
1320:     break;
1321:   case MAT_ROW_ORIENTED:
1322:     a->roworiented = PETSC_TRUE;
1323:     MatSetOption(a->A,op);
1324:     MatSetOption(a->B,op);
1325:     break;
1326:   case MAT_ROWS_SORTED:
1327:   case MAT_ROWS_UNSORTED:
1328:   case MAT_YES_NEW_DIAGONALS:
1329:     PetscLogInfo(A,"Info:MatSetOption_MPIBAIJ:Option ignored\n");
1330:     break;
1331:   case MAT_COLUMN_ORIENTED:
1332:     a->roworiented = PETSC_FALSE;
1333:     MatSetOption(a->A,op);
1334:     MatSetOption(a->B,op);
1335:     break;
1336:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1337:     a->donotstash = PETSC_TRUE;
1338:     break;
1339:   case MAT_NO_NEW_DIAGONALS:
1340:     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
1341:   case MAT_USE_HASH_TABLE:
1342:     a->ht_flag = PETSC_TRUE;
1343:     break;
1344:   case MAT_NOT_SYMMETRIC:
1345:   case MAT_NOT_STRUCTURALLY_SYMMETRIC:
1346:   case MAT_HERMITIAN:
1347:     SETERRQ(PETSC_ERR_SUP,"Matrix must be symmetric");
1348:   case MAT_SYMMETRIC:
1349:   case MAT_STRUCTURALLY_SYMMETRIC:
1350:   case MAT_NOT_HERMITIAN:
1351:   case MAT_SYMMETRY_ETERNAL:
1352:   case MAT_NOT_SYMMETRY_ETERNAL:
1353:     break;
1354:   default:
1355:     SETERRQ(PETSC_ERR_SUP,"unknown option");
1356:   }
1357:   return(0);
1358: }

1362: int MatTranspose_MPISBAIJ(Mat A,Mat *B)
1363: {
1366:   MatDuplicate(A,MAT_COPY_VALUES,B);
1367:   return(0);
1368: }

1372: int MatDiagonalScale_MPISBAIJ(Mat mat,Vec ll,Vec rr)
1373: {
1374:   Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)mat->data;
1375:   Mat         a = baij->A,b = baij->B;
1376:   int         ierr,s1,s2,s3;

1379:   if (ll != rr) {
1380:     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"For symmetric format, left and right scaling vectors must be same\n");
1381:   }
1382:   MatGetLocalSize(mat,&s2,&s3);
1383:   if (rr) {
1384:     VecGetLocalSize(rr,&s1);
1385:     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
1386:     /* Overlap communication with computation. */
1387:     VecScatterBegin(rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD,baij->Mvctx);
1388:     /*} if (ll) { */
1389:     VecGetLocalSize(ll,&s1);
1390:     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1391:     (*b->ops->diagonalscale)(b,ll,PETSC_NULL);
1392:     /* } */
1393:   /* scale  the diagonal block */
1394:   (*a->ops->diagonalscale)(a,ll,rr);

1396:   /* if (rr) { */
1397:     /* Do a scatter end and then right scale the off-diagonal block */
1398:     VecScatterEnd(rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD,baij->Mvctx);
1399:     (*b->ops->diagonalscale)(b,PETSC_NULL,baij->lvec);
1400:   }
1401: 
1402:   return(0);
1403: }

1407: int MatZeroRows_MPISBAIJ(Mat A,IS is,const PetscScalar *diag)
1408: {
1410:   SETERRQ(PETSC_ERR_SUP,"No support for this function yet");
1411: }

1415: int MatPrintHelp_MPISBAIJ(Mat A)
1416: {
1417:   Mat_MPISBAIJ *a   = (Mat_MPISBAIJ*)A->data;
1418:   MPI_Comm    comm = A->comm;
1419:   static int  called = 0;
1420:   int         ierr;

1423:   if (!a->rank) {
1424:     MatPrintHelp_SeqSBAIJ(a->A);
1425:   }
1426:   if (called) {return(0);} else called = 1;
1427:   (*PetscHelpPrintf)(comm," Options for MATMPISBAIJ matrix format (the defaults):\n");
1428:   (*PetscHelpPrintf)(comm,"  -mat_use_hash_table <factor>: Use hashtable for efficient matrix assembly\n");
1429:   return(0);
1430: }

1434: int MatSetUnfactored_MPISBAIJ(Mat A)
1435: {
1436:   Mat_MPISBAIJ *a   = (Mat_MPISBAIJ*)A->data;
1437:   int         ierr;

1440:   MatSetUnfactored(a->A);
1441:   return(0);
1442: }

1444: static int MatDuplicate_MPISBAIJ(Mat,MatDuplicateOption,Mat *);

1448: int MatEqual_MPISBAIJ(Mat A,Mat B,PetscTruth *flag)
1449: {
1450:   Mat_MPISBAIJ *matB = (Mat_MPISBAIJ*)B->data,*matA = (Mat_MPISBAIJ*)A->data;
1451:   Mat         a,b,c,d;
1452:   PetscTruth  flg;
1453:   int         ierr;

1456:   a = matA->A; b = matA->B;
1457:   c = matB->A; d = matB->B;

1459:   MatEqual(a,c,&flg);
1460:   if (flg == PETSC_TRUE) {
1461:     MatEqual(b,d,&flg);
1462:   }
1463:   MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);
1464:   return(0);
1465: }

1469: int MatSetUpPreallocation_MPISBAIJ(Mat A)
1470: {
1471:   int        ierr;

1474:   MatMPISBAIJSetPreallocation(A,1,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
1475:   return(0);
1476: }

1480: int MatGetSubMatrices_MPISBAIJ(Mat A,int n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1481: {
1482:   int        i,ierr;
1483:   PetscTruth flg;

1485:   for (i=0; i<n; i++) {
1486:     ISEqual(irow[i],icol[i],&flg);
1487:     if (!flg) {
1488:       SETERRQ(1,"Can only get symmetric submatrix for MPISBAIJ matrices");
1489:     }
1490:   }
1491:   MatGetSubMatrices_MPIBAIJ(A,n,irow,icol,scall,B);
1492:   return(0);
1493: }
1494: 

1496: /* -------------------------------------------------------------------*/
1497: static struct _MatOps MatOps_Values = {
1498:        MatSetValues_MPISBAIJ,
1499:        MatGetRow_MPISBAIJ,
1500:        MatRestoreRow_MPISBAIJ,
1501:        MatMult_MPISBAIJ,
1502: /* 4*/ MatMultAdd_MPISBAIJ,
1503:        MatMultTranspose_MPISBAIJ,
1504:        MatMultTransposeAdd_MPISBAIJ,
1505:        0,
1506:        0,
1507:        0,
1508: /*10*/ 0,
1509:        0,
1510:        0,
1511:        MatRelax_MPISBAIJ,
1512:        MatTranspose_MPISBAIJ,
1513: /*15*/ MatGetInfo_MPISBAIJ,
1514:        MatEqual_MPISBAIJ,
1515:        MatGetDiagonal_MPISBAIJ,
1516:        MatDiagonalScale_MPISBAIJ,
1517:        MatNorm_MPISBAIJ,
1518: /*20*/ MatAssemblyBegin_MPISBAIJ,
1519:        MatAssemblyEnd_MPISBAIJ,
1520:        0,
1521:        MatSetOption_MPISBAIJ,
1522:        MatZeroEntries_MPISBAIJ,
1523: /*25*/ MatZeroRows_MPISBAIJ,
1524:        0,
1525:        0,
1526:        0,
1527:        0,
1528: /*30*/ MatSetUpPreallocation_MPISBAIJ,
1529:        0,
1530:        0,
1531:        0,
1532:        0,
1533: /*35*/ MatDuplicate_MPISBAIJ,
1534:        0,
1535:        0,
1536:        0,
1537:        0,
1538: /*40*/ 0,
1539:        MatGetSubMatrices_MPISBAIJ,
1540:        MatIncreaseOverlap_MPISBAIJ,
1541:        MatGetValues_MPISBAIJ,
1542:        0,
1543: /*45*/ MatPrintHelp_MPISBAIJ,
1544:        MatScale_MPISBAIJ,
1545:        0,
1546:        0,
1547:        0,
1548: /*50*/ MatGetBlockSize_MPISBAIJ,
1549:        0,
1550:        0,
1551:        0,
1552:        0,
1553: /*55*/ 0,
1554:        0,
1555:        MatSetUnfactored_MPISBAIJ,
1556:        0,
1557:        MatSetValuesBlocked_MPISBAIJ,
1558: /*60*/ 0,
1559:        0,
1560:        0,
1561:        MatGetPetscMaps_Petsc,
1562:        0,
1563: /*65*/ 0,
1564:        0,
1565:        0,
1566:        0,
1567:        0,
1568: /*70*/ MatGetRowMax_MPISBAIJ,
1569:        0,
1570:        0,
1571:        0,
1572:        0,
1573: /*75*/ 0,
1574:        0,
1575:        0,
1576:        0,
1577:        0,
1578: /*80*/ 0,
1579:        0,
1580:        0,
1581:        0,
1582: /*85*/ MatLoad_MPISBAIJ
1583: };


1586: EXTERN_C_BEGIN
1589: int MatGetDiagonalBlock_MPISBAIJ(Mat A,PetscTruth *iscopy,MatReuse reuse,Mat *a)
1590: {
1592:   *a      = ((Mat_MPISBAIJ *)A->data)->A;
1593:   *iscopy = PETSC_FALSE;
1594:   return(0);
1595: }
1596: EXTERN_C_END

1598: EXTERN_C_BEGIN
1601: int MatMPISBAIJSetPreallocation_MPISBAIJ(Mat B,int bs,int d_nz,int *d_nnz,int o_nz,int *o_nnz)
1602: {
1603:   Mat_MPISBAIJ *b;
1604:   int          ierr,i,mbs,Mbs;

1607:   PetscOptionsGetInt(B->prefix,"-mat_block_size",&bs,PETSC_NULL);

1609:   if (bs < 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive");
1610:   if (d_nz == PETSC_DECIDE || d_nz == PETSC_DEFAULT) d_nz = 3;
1611:   if (o_nz == PETSC_DECIDE || o_nz == PETSC_DEFAULT) o_nz = 1;
1612:   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %d",d_nz);
1613:   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %d",o_nz);
1614:   if (d_nnz) {
1615:     for (i=0; i<B->m/bs; i++) {
1616:       if (d_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than -1: local row %d value %d",i,d_nnz[i]);
1617:     }
1618:   }
1619:   if (o_nnz) {
1620:     for (i=0; i<B->m/bs; i++) {
1621:       if (o_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than -1: local row %d value %d",i,o_nnz[i]);
1622:     }
1623:   }
1624:   B->preallocated = PETSC_TRUE;
1625:   PetscSplitOwnershipBlock(B->comm,bs,&B->m,&B->M);
1626:   PetscSplitOwnershipBlock(B->comm,bs,&B->n,&B->N);
1627:   PetscMapCreateMPI(B->comm,B->m,B->M,&B->rmap);
1628:   PetscMapCreateMPI(B->comm,B->m,B->M,&B->cmap);

1630:   b   = (Mat_MPISBAIJ*)B->data;
1631:   mbs = B->m/bs;
1632:   Mbs = B->M/bs;
1633:   if (mbs*bs != B->m) {
1634:     SETERRQ2(PETSC_ERR_ARG_SIZ,"No of local rows %d must be divisible by blocksize %d",B->m,bs);
1635:   }

1637:   b->bs  = bs;
1638:   b->bs2 = bs*bs;
1639:   b->mbs = mbs;
1640:   b->nbs = mbs;
1641:   b->Mbs = Mbs;
1642:   b->Nbs = Mbs;

1644:   MPI_Allgather(&b->mbs,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);
1645:   b->rowners[0]    = 0;
1646:   for (i=2; i<=b->size; i++) {
1647:     b->rowners[i] += b->rowners[i-1];
1648:   }
1649:   b->rstart    = b->rowners[b->rank];
1650:   b->rend      = b->rowners[b->rank+1];
1651:   b->cstart    = b->rstart;
1652:   b->cend      = b->rend;
1653:   for (i=0; i<=b->size; i++) {
1654:     b->rowners_bs[i] = b->rowners[i]*bs;
1655:   }
1656:   b->rstart_bs = b-> rstart*bs;
1657:   b->rend_bs   = b->rend*bs;
1658: 
1659:   b->cstart_bs = b->cstart*bs;
1660:   b->cend_bs   = b->cend*bs;
1661: 
1662:   MatCreate(PETSC_COMM_SELF,B->m,B->m,B->m,B->m,&b->A);
1663:   MatSetType(b->A,MATSEQSBAIJ);
1664:   MatSeqSBAIJSetPreallocation(b->A,bs,d_nz,d_nnz);
1665:   PetscLogObjectParent(B,b->A);

1667:   MatCreate(PETSC_COMM_SELF,B->m,B->M,B->m,B->M,&b->B);
1668:   MatSetType(b->B,MATSEQBAIJ);
1669:   MatSeqBAIJSetPreallocation(b->B,bs,o_nz,o_nnz);
1670:   PetscLogObjectParent(B,b->B);

1672:   /* build cache for off array entries formed */
1673:   MatStashCreate_Private(B->comm,bs,&B->bstash);

1675:   return(0);
1676: }
1677: EXTERN_C_END

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

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

1686:   Level: beginner

1688: .seealso: MatCreateMPISBAIJ
1689: M*/

1691: EXTERN_C_BEGIN
1694: int MatCreate_MPISBAIJ(Mat B)
1695: {
1696:   Mat_MPISBAIJ *b;
1697:   int          ierr;
1698:   PetscTruth   flg;


1702:   PetscNew(Mat_MPISBAIJ,&b);
1703:   B->data = (void*)b;
1704:   PetscMemzero(b,sizeof(Mat_MPISBAIJ));
1705:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));

1707:   B->ops->destroy    = MatDestroy_MPISBAIJ;
1708:   B->ops->view       = MatView_MPISBAIJ;
1709:   B->mapping    = 0;
1710:   B->factor     = 0;
1711:   B->assembled  = PETSC_FALSE;

1713:   B->insertmode = NOT_SET_VALUES;
1714:   MPI_Comm_rank(B->comm,&b->rank);
1715:   MPI_Comm_size(B->comm,&b->size);

1717:   /* build local table of row and column ownerships */
1718:   PetscMalloc(3*(b->size+2)*sizeof(int),&b->rowners);
1719:   b->cowners    = b->rowners + b->size + 2;
1720:   b->rowners_bs = b->cowners + b->size + 2;
1721:   PetscLogObjectMemory(B,3*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPISBAIJ));

1723:   /* build cache for off array entries formed */
1724:   MatStashCreate_Private(B->comm,1,&B->stash);
1725:   b->donotstash  = PETSC_FALSE;
1726:   b->colmap      = PETSC_NULL;
1727:   b->garray      = PETSC_NULL;
1728:   b->roworiented = PETSC_TRUE;

1730: #if defined(PETSC_USE_MAT_SINGLE)
1731:   /* stuff for MatSetValues_XXX in single precision */
1732:   b->setvalueslen     = 0;
1733:   b->setvaluescopy    = PETSC_NULL;
1734: #endif

1736:   /* stuff used in block assembly */
1737:   b->barray       = 0;

1739:   /* stuff used for matrix vector multiply */
1740:   b->lvec         = 0;
1741:   b->Mvctx        = 0;
1742:   b->slvec0       = 0;
1743:   b->slvec0b      = 0;
1744:   b->slvec1       = 0;
1745:   b->slvec1a      = 0;
1746:   b->slvec1b      = 0;
1747:   b->sMvctx       = 0;

1749:   /* stuff for MatGetRow() */
1750:   b->rowindices   = 0;
1751:   b->rowvalues    = 0;
1752:   b->getrowactive = PETSC_FALSE;

1754:   /* hash table stuff */
1755:   b->ht           = 0;
1756:   b->hd           = 0;
1757:   b->ht_size      = 0;
1758:   b->ht_flag      = PETSC_FALSE;
1759:   b->ht_fact      = 0;
1760:   b->ht_total_ct  = 0;
1761:   b->ht_insert_ct = 0;

1763:   PetscOptionsHasName(B->prefix,"-mat_use_hash_table",&flg);
1764:   if (flg) {
1765:     PetscReal fact = 1.39;
1766:     MatSetOption(B,MAT_USE_HASH_TABLE);
1767:     PetscOptionsGetReal(B->prefix,"-mat_use_hash_table",&fact,PETSC_NULL);
1768:     if (fact <= 1.0) fact = 1.39;
1769:     MatMPIBAIJSetHashTableFactor(B,fact);
1770:     PetscLogInfo(0,"MatCreateMPISBAIJ:Hash table Factor used %5.2f\n",fact);
1771:   }
1772:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
1773:                                      "MatStoreValues_MPISBAIJ",
1774:                                      MatStoreValues_MPISBAIJ);
1775:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
1776:                                      "MatRetrieveValues_MPISBAIJ",
1777:                                      MatRetrieveValues_MPISBAIJ);
1778:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
1779:                                      "MatGetDiagonalBlock_MPISBAIJ",
1780:                                      MatGetDiagonalBlock_MPISBAIJ);
1781:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPISBAIJSetPreallocation_C",
1782:                                      "MatMPISBAIJSetPreallocation_MPISBAIJ",
1783:                                      MatMPISBAIJSetPreallocation_MPISBAIJ);
1784:   return(0);
1785: }
1786: EXTERN_C_END

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

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

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

1797:   Level: beginner

1799: .seealso: MatCreateMPISBAIJ,MATSEQSBAIJ,MATMPISBAIJ
1800: M*/

1802: EXTERN_C_BEGIN
1805: int MatCreate_SBAIJ(Mat A) {
1806:   int ierr,size;

1809:   PetscObjectChangeTypeName((PetscObject)A,MATSBAIJ);
1810:   MPI_Comm_size(A->comm,&size);
1811:   if (size == 1) {
1812:     MatSetType(A,MATSEQSBAIJ);
1813:   } else {
1814:     MatSetType(A,MATMPISBAIJ);
1815:   }
1816:   return(0);
1817: }
1818: EXTERN_C_END

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

1828:    Collective on Mat

1830:    Input Parameters:
1831: +  A - the matrix 
1832: .  bs   - size of blockk
1833: .  d_nz  - number of block nonzeros per block row in diagonal portion of local 
1834:            submatrix  (same for all local rows)
1835: .  d_nnz - array containing the number of block nonzeros in the various block rows 
1836:            in the upper triangular and diagonal part of the in diagonal portion of the local
1837:            (possibly different for each block row) or PETSC_NULL.  You must leave room 
1838:            for the diagonal entry even if it is zero.
1839: .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
1840:            submatrix (same for all local rows).
1841: -  o_nnz - array containing the number of nonzeros in the various block rows of the
1842:            off-diagonal portion of the local submatrix (possibly different for
1843:            each block row) or PETSC_NULL.


1846:    Options Database Keys:
1847: .   -mat_no_unroll - uses code that does not unroll the loops in the 
1848:                      block calculations (much slower)
1849: .   -mat_block_size - size of the blocks to use

1851:    Notes:

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

1856:    Storage Information:
1857:    For a square global matrix we define each processor's diagonal portion 
1858:    to be its local rows and the corresponding columns (a square submatrix);  
1859:    each processor's off-diagonal portion encompasses the remainder of the
1860:    local matrix (a rectangular submatrix). 

1862:    The user can specify preallocated storage for the diagonal part of
1863:    the local submatrix with either d_nz or d_nnz (not both).  Set 
1864:    d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic
1865:    memory allocation.  Likewise, specify preallocated storage for the
1866:    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).

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

1871: .vb
1872:            0 1 2 3 4 5 6 7 8 9 10 11
1873:           -------------------
1874:    row 3  |  o o o d d d o o o o o o
1875:    row 4  |  o o o d d d o o o o o o
1876:    row 5  |  o o o d d d o o o o o o
1877:           -------------------
1878: .ve
1879:   
1880:    Thus, any entries in the d locations are stored in the d (diagonal) 
1881:    submatrix, and any entries in the o locations are stored in the
1882:    o (off-diagonal) submatrix.  Note that the d matrix is stored in
1883:    MatSeqSBAIJ format and the o submatrix in MATSEQBAIJ format.

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

1893:    Level: intermediate

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

1897: .seealso: MatCreate(), MatCreateSeqSBAIJ(), MatSetValues(), MatCreateMPIBAIJ()
1898: @*/
1899: int MatMPISBAIJSetPreallocation(Mat B,int bs,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[])
1900: {
1901:   int ierr,(*f)(Mat,int,int,const int[],int,const int[]);

1904:   PetscObjectQueryFunction((PetscObject)B,"MatMPISBAIJSetPreallocation_C",(void (**)(void))&f);
1905:   if (f) {
1906:     (*f)(B,bs,d_nz,d_nnz,o_nz,o_nnz);
1907:   }
1908:   return(0);
1909: }

1913: /*@C
1914:    MatCreateMPISBAIJ - Creates a sparse parallel matrix in symmetric block AIJ format
1915:    (block compressed row).  For good matrix assembly performance
1916:    the user should preallocate the matrix storage by setting the parameters 
1917:    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
1918:    performance can be increased by more than a factor of 50.

1920:    Collective on MPI_Comm

1922:    Input Parameters:
1923: +  comm - MPI communicator
1924: .  bs   - size of blockk
1925: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
1926:            This value should be the same as the local size used in creating the 
1927:            y vector for the matrix-vector product y = Ax.
1928: .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
1929:            This value should be the same as the local size used in creating the 
1930:            x vector for the matrix-vector product y = Ax.
1931: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
1932: .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
1933: .  d_nz  - number of block nonzeros per block row in diagonal portion of local 
1934:            submatrix  (same for all local rows)
1935: .  d_nnz - array containing the number of block nonzeros in the various block rows 
1936:            in the upper triangular portion of the in diagonal portion of the local 
1937:            (possibly different for each block block row) or PETSC_NULL.  
1938:            You must leave room for the diagonal entry even if it is zero.
1939: .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
1940:            submatrix (same for all local rows).
1941: -  o_nnz - array containing the number of nonzeros in the various block rows of the
1942:            off-diagonal portion of the local submatrix (possibly different for
1943:            each block row) or PETSC_NULL.

1945:    Output Parameter:
1946: .  A - the matrix 

1948:    Options Database Keys:
1949: .   -mat_no_unroll - uses code that does not unroll the loops in the 
1950:                      block calculations (much slower)
1951: .   -mat_block_size - size of the blocks to use
1952: .   -mat_mpi - use the parallel matrix data structures even on one processor 
1953:                (defaults to using SeqBAIJ format on one processor)

1955:    Notes:
1956:    The user MUST specify either the local or global matrix dimensions
1957:    (possibly both).

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

1962:    Storage Information:
1963:    For a square global matrix we define each processor's diagonal portion 
1964:    to be its local rows and the corresponding columns (a square submatrix);  
1965:    each processor's off-diagonal portion encompasses the remainder of the
1966:    local matrix (a rectangular submatrix). 

1968:    The user can specify preallocated storage for the diagonal part of
1969:    the local submatrix with either d_nz or d_nnz (not both).  Set 
1970:    d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic
1971:    memory allocation.  Likewise, specify preallocated storage for the
1972:    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).

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

1977: .vb
1978:            0 1 2 3 4 5 6 7 8 9 10 11
1979:           -------------------
1980:    row 3  |  o o o d d d o o o o o o
1981:    row 4  |  o o o d d d o o o o o o
1982:    row 5  |  o o o d d d o o o o o o
1983:           -------------------
1984: .ve
1985:   
1986:    Thus, any entries in the d locations are stored in the d (diagonal) 
1987:    submatrix, and any entries in the o locations are stored in the
1988:    o (off-diagonal) submatrix.  Note that the d matrix is stored in
1989:    MatSeqSBAIJ format and the o submatrix in MATSEQBAIJ format.

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

1999:    Level: intermediate

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

2003: .seealso: MatCreate(), MatCreateSeqSBAIJ(), MatSetValues(), MatCreateMPIBAIJ()
2004: @*/

2006: int MatCreateMPISBAIJ(MPI_Comm comm,int bs,int m,int n,int M,int N,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[],Mat *A)
2007: {
2008:   int ierr,size;

2011:   MatCreate(comm,m,n,M,N,A);
2012:   MPI_Comm_size(comm,&size);
2013:   if (size > 1) {
2014:     MatSetType(*A,MATMPISBAIJ);
2015:     MatMPISBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);
2016:   } else {
2017:     MatSetType(*A,MATSEQSBAIJ);
2018:     MatSeqSBAIJSetPreallocation(*A,bs,d_nz,d_nnz);
2019:   }
2020:   return(0);
2021: }


2026: static int MatDuplicate_MPISBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
2027: {
2028:   Mat          mat;
2029:   Mat_MPISBAIJ *a,*oldmat = (Mat_MPISBAIJ*)matin->data;
2030:   int          ierr,len=0,nt,bs=oldmat->bs,mbs=oldmat->mbs;
2031:   PetscScalar  *array;

2034:   *newmat       = 0;
2035:   MatCreate(matin->comm,matin->m,matin->n,matin->M,matin->N,&mat);
2036:   MatSetType(mat,matin->type_name);

2038:   /* PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps)); */ /*-- cause error? */
2039:   mat->factor       = matin->factor;
2040:   mat->preallocated = PETSC_TRUE;
2041:   mat->assembled    = PETSC_TRUE;
2042:   mat->insertmode   = NOT_SET_VALUES;

2044:   a = (Mat_MPISBAIJ*)mat->data;
2045:   a->bs  = oldmat->bs;
2046:   a->bs2 = oldmat->bs2;
2047:   a->mbs = oldmat->mbs;
2048:   a->nbs = oldmat->nbs;
2049:   a->Mbs = oldmat->Mbs;
2050:   a->Nbs = oldmat->Nbs;
2051: 
2052:   a->rstart       = oldmat->rstart;
2053:   a->rend         = oldmat->rend;
2054:   a->cstart       = oldmat->cstart;
2055:   a->cend         = oldmat->cend;
2056:   a->size         = oldmat->size;
2057:   a->rank         = oldmat->rank;
2058:   a->donotstash   = oldmat->donotstash;
2059:   a->roworiented  = oldmat->roworiented;
2060:   a->rowindices   = 0;
2061:   a->rowvalues    = 0;
2062:   a->getrowactive = PETSC_FALSE;
2063:   a->barray       = 0;
2064:   a->rstart_bs    = oldmat->rstart_bs;
2065:   a->rend_bs      = oldmat->rend_bs;
2066:   a->cstart_bs    = oldmat->cstart_bs;
2067:   a->cend_bs      = oldmat->cend_bs;

2069:   /* hash table stuff */
2070:   a->ht           = 0;
2071:   a->hd           = 0;
2072:   a->ht_size      = 0;
2073:   a->ht_flag      = oldmat->ht_flag;
2074:   a->ht_fact      = oldmat->ht_fact;
2075:   a->ht_total_ct  = 0;
2076:   a->ht_insert_ct = 0;
2077: 
2078:   PetscMemcpy(a->rowners,oldmat->rowners,3*(a->size+2)*sizeof(int));
2079:   MatStashCreate_Private(matin->comm,1,&mat->stash);
2080:   MatStashCreate_Private(matin->comm,oldmat->bs,&mat->bstash);
2081:   if (oldmat->colmap) {
2082: #if defined (PETSC_USE_CTABLE)
2083:     PetscTableCreateCopy(oldmat->colmap,&a->colmap);
2084: #else
2085:     PetscMalloc((a->Nbs)*sizeof(int),&a->colmap);
2086:     PetscLogObjectMemory(mat,(a->Nbs)*sizeof(int));
2087:     PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(int));
2088: #endif
2089:   } else a->colmap = 0;

2091:   if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
2092:     PetscMalloc(len*sizeof(int),&a->garray);
2093:     PetscLogObjectMemory(mat,len*sizeof(int));
2094:     PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));
2095:   } else a->garray = 0;
2096: 
2097:    VecDuplicate(oldmat->lvec,&a->lvec);
2098:   PetscLogObjectParent(mat,a->lvec);
2099:    VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
2100:   PetscLogObjectParent(mat,a->Mvctx);

2102:    VecDuplicate(oldmat->slvec0,&a->slvec0);
2103:   PetscLogObjectParent(mat,a->slvec0);
2104:    VecDuplicate(oldmat->slvec1,&a->slvec1);
2105:   PetscLogObjectParent(mat,a->slvec1);

2107:   VecGetLocalSize(a->slvec1,&nt);
2108:   VecGetArray(a->slvec1,&array);
2109:   VecCreateSeqWithArray(PETSC_COMM_SELF,bs*mbs,array,&a->slvec1a);
2110:   VecCreateSeqWithArray(PETSC_COMM_SELF,nt-bs*mbs,array+bs*mbs,&a->slvec1b);
2111:   VecRestoreArray(a->slvec1,&array);
2112:   VecGetArray(a->slvec0,&array);
2113:   VecCreateSeqWithArray(PETSC_COMM_SELF,nt-bs*mbs,array+bs*mbs,&a->slvec0b);
2114:   VecRestoreArray(a->slvec0,&array);
2115:   PetscLogObjectParent(mat,a->slvec0);
2116:   PetscLogObjectParent(mat,a->slvec1);
2117:   PetscLogObjectParent(mat,a->slvec0b);
2118:   PetscLogObjectParent(mat,a->slvec1a);
2119:   PetscLogObjectParent(mat,a->slvec1b);

2121:   /*  VecScatterCopy(oldmat->sMvctx,&a->sMvctx); - not written yet, replaced by the lazy trick: */
2122:   PetscObjectReference((PetscObject)oldmat->sMvctx);
2123:   a->sMvctx = oldmat->sMvctx;
2124:   PetscLogObjectParent(mat,a->sMvctx);

2126:    MatDuplicate(oldmat->A,cpvalues,&a->A);
2127:   PetscLogObjectParent(mat,a->A);
2128:    MatDuplicate(oldmat->B,cpvalues,&a->B);
2129:   PetscLogObjectParent(mat,a->B);
2130:   PetscFListDuplicate(mat->qlist,&matin->qlist);
2131:   *newmat = mat;
2132:   return(0);
2133: }

2135:  #include petscsys.h

2139: int MatLoad_MPISBAIJ(PetscViewer viewer,const MatType type,Mat *newmat)
2140: {
2141:   Mat          A;
2142:   int          i,nz,ierr,j,rstart,rend,fd;
2143:   PetscScalar  *vals,*buf;
2144:   MPI_Comm     comm = ((PetscObject)viewer)->comm;
2145:   MPI_Status   status;
2146:   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,*browners,maxnz,*cols;
2147:   int          *locrowlens,*sndcounts = 0,*procsnz = 0,jj,*mycols,*ibuf;
2148:   int          tag = ((PetscObject)viewer)->tag,bs=1,Mbs,mbs,extra_rows;
2149:   int          *dlens,*odlens,*mask,*masked1,*masked2,rowcount,odcount;
2150:   int          dcount,kmax,k,nzcount,tmp;
2151: 
2153:   PetscOptionsGetInt(PETSC_NULL,"-matload_block_size",&bs,PETSC_NULL);

2155:   MPI_Comm_size(comm,&size);
2156:   MPI_Comm_rank(comm,&rank);
2157:   if (!rank) {
2158:     PetscViewerBinaryGetDescriptor(viewer,&fd);
2159:     PetscBinaryRead(fd,(char *)header,4,PETSC_INT);
2160:     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
2161:     if (header[3] < 0) {
2162:       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as MPISBAIJ");
2163:     }
2164:   }

2166:   MPI_Bcast(header+1,3,MPI_INT,0,comm);
2167:   M = header[1]; N = header[2];

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

2171:   /* 
2172:      This code adds extra rows to make sure the number of rows is 
2173:      divisible by the blocksize
2174:   */
2175:   Mbs        = M/bs;
2176:   extra_rows = bs - M + bs*(Mbs);
2177:   if (extra_rows == bs) extra_rows = 0;
2178:   else                  Mbs++;
2179:   if (extra_rows &&!rank) {
2180:     PetscLogInfo(0,"MatLoad_MPISBAIJ:Padding loaded matrix to match blocksize\n");
2181:   }

2183:   /* determine ownership of all rows */
2184:   mbs        = Mbs/size + ((Mbs % size) > rank);
2185:   m          = mbs*bs;
2186:   PetscMalloc(2*(size+2)*sizeof(int),&rowners);
2187:   browners   = rowners + size + 1;
2188:   MPI_Allgather(&mbs,1,MPI_INT,rowners+1,1,MPI_INT,comm);
2189:   rowners[0] = 0;
2190:   for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
2191:   for (i=0; i<=size;  i++) browners[i] = rowners[i]*bs;
2192:   rstart = rowners[rank];
2193:   rend   = rowners[rank+1];
2194: 
2195:   /* distribute row lengths to all processors */
2196:   PetscMalloc((rend-rstart)*bs*sizeof(int),&locrowlens);
2197:   if (!rank) {
2198:     PetscMalloc((M+extra_rows)*sizeof(int),&rowlengths);
2199:     PetscBinaryRead(fd,rowlengths,M,PETSC_INT);
2200:     for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
2201:     PetscMalloc(size*sizeof(int),&sndcounts);
2202:     for (i=0; i<size; i++) sndcounts[i] = browners[i+1] - browners[i];
2203:     MPI_Scatterv(rowlengths,sndcounts,browners,MPI_INT,locrowlens,(rend-rstart)*bs,MPI_INT,0,comm);
2204:     PetscFree(sndcounts);
2205:   } else {
2206:     MPI_Scatterv(0,0,0,MPI_INT,locrowlens,(rend-rstart)*bs,MPI_INT,0,comm);
2207:   }
2208: 
2209:   if (!rank) {   /* procs[0] */
2210:     /* calculate the number of nonzeros on each processor */
2211:     PetscMalloc(size*sizeof(int),&procsnz);
2212:     PetscMemzero(procsnz,size*sizeof(int));
2213:     for (i=0; i<size; i++) {
2214:       for (j=rowners[i]*bs; j< rowners[i+1]*bs; j++) {
2215:         procsnz[i] += rowlengths[j];
2216:       }
2217:     }
2218:     PetscFree(rowlengths);
2219: 
2220:     /* determine max buffer needed and allocate it */
2221:     maxnz = 0;
2222:     for (i=0; i<size; i++) {
2223:       maxnz = PetscMax(maxnz,procsnz[i]);
2224:     }
2225:     PetscMalloc(maxnz*sizeof(int),&cols);

2227:     /* read in my part of the matrix column indices  */
2228:     nz     = procsnz[0];
2229:     PetscMalloc(nz*sizeof(int),&ibuf);
2230:     mycols = ibuf;
2231:     if (size == 1)  nz -= extra_rows;
2232:     PetscBinaryRead(fd,mycols,nz,PETSC_INT);
2233:     if (size == 1)  for (i=0; i< extra_rows; i++) { mycols[nz+i] = M+i; }

2235:     /* read in every ones (except the last) and ship off */
2236:     for (i=1; i<size-1; i++) {
2237:       nz   = procsnz[i];
2238:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
2239:       MPI_Send(cols,nz,MPI_INT,i,tag,comm);
2240:     }
2241:     /* read in the stuff for the last proc */
2242:     if (size != 1) {
2243:       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
2244:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
2245:       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
2246:       MPI_Send(cols,nz+extra_rows,MPI_INT,size-1,tag,comm);
2247:     }
2248:     PetscFree(cols);
2249:   } else {  /* procs[i], i>0 */
2250:     /* determine buffer space needed for message */
2251:     nz = 0;
2252:     for (i=0; i<m; i++) {
2253:       nz += locrowlens[i];
2254:     }
2255:     PetscMalloc(nz*sizeof(int),&ibuf);
2256:     mycols = ibuf;
2257:     /* receive message of column indices*/
2258:     MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);
2259:     MPI_Get_count(&status,MPI_INT,&maxnz);
2260:     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2261:   }

2263:   /* loop over local rows, determining number of off diagonal entries */
2264:   PetscMalloc(2*(rend-rstart+1)*sizeof(int),&dlens);
2265:   odlens   = dlens + (rend-rstart);
2266:   PetscMalloc(3*Mbs*sizeof(int),&mask);
2267:   PetscMemzero(mask,3*Mbs*sizeof(int));
2268:   masked1  = mask    + Mbs;
2269:   masked2  = masked1 + Mbs;
2270:   rowcount = 0; nzcount = 0;
2271:   for (i=0; i<mbs; i++) {
2272:     dcount  = 0;
2273:     odcount = 0;
2274:     for (j=0; j<bs; j++) {
2275:       kmax = locrowlens[rowcount];
2276:       for (k=0; k<kmax; k++) {
2277:         tmp = mycols[nzcount++]/bs; /* block col. index */
2278:         if (!mask[tmp]) {
2279:           mask[tmp] = 1;
2280:           if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp; /* entry in off-diag portion */
2281:           else masked1[dcount++] = tmp; /* entry in diag portion */
2282:         }
2283:       }
2284:       rowcount++;
2285:     }
2286: 
2287:     dlens[i]  = dcount;  /* d_nzz[i] */
2288:     odlens[i] = odcount; /* o_nzz[i] */

2290:     /* zero out the mask elements we set */
2291:     for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
2292:     for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
2293:   }
2294: 
2295:   /* create our matrix */
2296:   MatCreate(comm,m,m,PETSC_DETERMINE,PETSC_DETERMINE,&A);
2297:   MatSetType(A,type);
2298:   MatMPISBAIJSetPreallocation(A,bs,0,dlens,0,odlens);
2299:   MatSetOption(A,MAT_COLUMNS_SORTED);
2300: 
2301:   if (!rank) {
2302:     PetscMalloc(maxnz*sizeof(PetscScalar),&buf);
2303:     /* read in my part of the matrix numerical values  */
2304:     nz = procsnz[0];
2305:     vals = buf;
2306:     mycols = ibuf;
2307:     if (size == 1)  nz -= extra_rows;
2308:     PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2309:     if (size == 1)  for (i=0; i< extra_rows; i++) { vals[nz+i] = 1.0; }

2311:     /* insert into matrix */
2312:     jj      = rstart*bs;
2313:     for (i=0; i<m; i++) {
2314:       MatSetValues(A,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
2315:       mycols += locrowlens[i];
2316:       vals   += locrowlens[i];
2317:       jj++;
2318:     }

2320:     /* read in other processors (except the last one) and ship out */
2321:     for (i=1; i<size-1; i++) {
2322:       nz   = procsnz[i];
2323:       vals = buf;
2324:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2325:       MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);
2326:     }
2327:     /* the last proc */
2328:     if (size != 1){
2329:       nz   = procsnz[i] - extra_rows;
2330:       vals = buf;
2331:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2332:       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
2333:       MPI_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,A->tag,comm);
2334:     }
2335:     PetscFree(procsnz);

2337:   } else {
2338:     /* receive numeric values */
2339:     PetscMalloc(nz*sizeof(PetscScalar),&buf);

2341:     /* receive message of values*/
2342:     vals   = buf;
2343:     mycols = ibuf;
2344:     MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);
2345:     MPI_Get_count(&status,MPIU_SCALAR,&maxnz);
2346:     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");

2348:     /* insert into matrix */
2349:     jj      = rstart*bs;
2350:     for (i=0; i<m; i++) {
2351:       MatSetValues_MPISBAIJ(A,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
2352:       mycols += locrowlens[i];
2353:       vals   += locrowlens[i];
2354:       jj++;
2355:     }
2356:   }

2358:   PetscFree(locrowlens);
2359:   PetscFree(buf);
2360:   PetscFree(ibuf);
2361:   PetscFree(rowners);
2362:   PetscFree(dlens);
2363:   PetscFree(mask);
2364:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
2365:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
2366:   *newmat = A;
2367:   return(0);
2368: }

2372: /*@
2373:    MatMPISBAIJSetHashTableFactor - Sets the factor required to compute the size of the HashTable.

2375:    Input Parameters:
2376: .  mat  - the matrix
2377: .  fact - factor

2379:    Collective on Mat

2381:    Level: advanced

2383:   Notes:
2384:    This can also be set by the command line option: -mat_use_hash_table fact

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

2388: .seealso: MatSetOption()
2389: @*/
2390: int MatMPISBAIJSetHashTableFactor(Mat mat,PetscReal fact)
2391: {
2393:   SETERRQ(1,"Function not yet written for SBAIJ format");
2394:   /* return(0); */
2395: }

2399: int MatGetRowMax_MPISBAIJ(Mat A,Vec v)
2400: {
2401:   Mat_MPISBAIJ *a = (Mat_MPISBAIJ*)A->data;
2402:   Mat_SeqBAIJ  *b = (Mat_SeqBAIJ*)(a->B)->data;
2403:   PetscReal    atmp;
2404:   PetscReal    *work,*svalues,*rvalues;
2405:   int          ierr,i,bs,mbs,*bi,*bj,brow,j,ncols,krow,kcol,col,row,Mbs,bcol;
2406:   int          rank,size,*rowners_bs,dest,count,source;
2407:   PetscScalar  *va;
2408:   MatScalar    *ba;
2409:   MPI_Status   stat;

2412:   MatGetRowMax(a->A,v);
2413:   VecGetArray(v,&va);

2415:   MPI_Comm_size(A->comm,&size);
2416:   MPI_Comm_rank(A->comm,&rank);

2418:   bs   = a->bs;
2419:   mbs  = a->mbs;
2420:   Mbs  = a->Mbs;
2421:   ba   = b->a;
2422:   bi   = b->i;
2423:   bj   = b->j;
2424:   /*
2425:   PetscSynchronizedPrintf(A->comm,"[%d] M: %d, bs: %d, mbs: %d \n",rank,bs*Mbs,bs,mbs); 
2426:   PetscSynchronizedFlush(A->comm);
2427:   */

2429:   /* find ownerships */
2430:   rowners_bs = a->rowners_bs;
2431:   /*
2432:   if (!rank){
2433:     for (i=0; i<size+1; i++) PetscPrintf(PETSC_COMM_SELF," rowners_bs[%d]: %d\n",i,rowners_bs[i]); 
2434:   }
2435:   */

2437:   /* each proc creates an array to be distributed */
2438:   PetscMalloc(bs*Mbs*sizeof(PetscReal),&work);
2439:   PetscMemzero(work,bs*Mbs*sizeof(PetscReal));

2441:   /* row_max for B */
2442:   if (rank != size-1){
2443:     for (i=0; i<mbs; i++) {
2444:       ncols = bi[1] - bi[0]; bi++;
2445:       brow  = bs*i;
2446:       for (j=0; j<ncols; j++){
2447:         bcol = bs*(*bj);
2448:         for (kcol=0; kcol<bs; kcol++){
2449:           col = bcol + kcol;                 /* local col index */
2450:           col += rowners_bs[rank+1];      /* global col index */
2451:           /* PetscPrintf(PETSC_COMM_SELF,"[%d], col: %d\n",rank,col); */
2452:           for (krow=0; krow<bs; krow++){
2453:             atmp = PetscAbsScalar(*ba); ba++;
2454:             row = brow + krow;    /* local row index */
2455:             /* printf("val[%d,%d]: %g\n",row,col,atmp); */
2456:             if (PetscRealPart(va[row]) < atmp) va[row] = atmp;
2457:             if (work[col] < atmp) work[col] = atmp;
2458:           }
2459:         }
2460:         bj++;
2461:       }
2462:     }
2463:     /*
2464:       PetscPrintf(PETSC_COMM_SELF,"[%d], work: ",rank);
2465:       for (i=0; i<bs*Mbs; i++) PetscPrintf(PETSC_COMM_SELF,"%g ",work[i]);
2466:       PetscPrintf(PETSC_COMM_SELF,"[%d]: \n");
2467:       */

2469:     /* send values to its owners */
2470:     for (dest=rank+1; dest<size; dest++){
2471:       svalues = work + rowners_bs[dest];
2472:       count   = rowners_bs[dest+1]-rowners_bs[dest];
2473:       MPI_Send(svalues,count,MPIU_REAL,dest,rank,A->comm);
2474:       /*
2475:       PetscSynchronizedPrintf(A->comm,"[%d] sends %d values to [%d]: %g, %g, %g, %g\n",rank,count,dest,svalues[0],svalues[1],svalues[2],svalues[3]); 
2476:       PetscSynchronizedFlush(A->comm);
2477:       */
2478:     }
2479:   }
2480: 
2481:   /* receive values */
2482:   if (rank){
2483:     rvalues = work;
2484:     count   = rowners_bs[rank+1]-rowners_bs[rank];
2485:     for (source=0; source<rank; source++){
2486:       MPI_Recv(rvalues,count,MPIU_REAL,MPI_ANY_SOURCE,MPI_ANY_TAG,A->comm,&stat);
2487:       /* process values */
2488:       for (i=0; i<count; i++){
2489:         if (PetscRealPart(va[i]) < rvalues[i]) va[i] = rvalues[i];
2490:       }
2491:       /*
2492:       PetscSynchronizedPrintf(A->comm,"[%d] received %d values from [%d]: %g, %g, %g, %g \n",rank,count,stat.MPI_SOURCE,rvalues[0],rvalues[1],rvalues[2],rvalues[3]);  
2493:       PetscSynchronizedFlush(A->comm);
2494:       */
2495:     }
2496:   }

2498:   VecRestoreArray(v,&va);
2499:   PetscFree(work);
2500:   return(0);
2501: }

2505: int MatRelax_MPISBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
2506: {
2507:   Mat_MPISBAIJ   *mat = (Mat_MPISBAIJ*)matin->data;
2508:   int            ierr,mbs=mat->mbs,bs=mat->bs;
2509:   PetscScalar    mone=-1.0,*x,*b,*ptr,zero=0.0;
2510:   Vec            bb1;
2511: 
2513:   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
2514:   if (bs > 1)
2515:     SETERRQ(PETSC_ERR_SUP,"SSOR for block size > 1 is not yet implemented");

2517:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
2518:     if ( flag & SOR_ZERO_INITIAL_GUESS ) {
2519:       (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);
2520:       its--;
2521:     }

2523:     VecDuplicate(bb,&bb1);
2524:     while (its--){
2525: 
2526:       /* lower triangular part: slvec0b = - B^T*xx */
2527:       (*mat->B->ops->multtranspose)(mat->B,xx,mat->slvec0b);
2528: 
2529:       /* copy xx into slvec0a */
2530:       VecGetArray(mat->slvec0,&ptr);
2531:       VecGetArray(xx,&x);
2532:       PetscMemcpy(ptr,x,bs*mbs*sizeof(MatScalar));
2533:       VecRestoreArray(mat->slvec0,&ptr);

2535:       VecScale(&mone,mat->slvec0);

2537:       /* copy bb into slvec1a */
2538:       VecGetArray(mat->slvec1,&ptr);
2539:       VecGetArray(bb,&b);
2540:       PetscMemcpy(ptr,b,bs*mbs*sizeof(MatScalar));
2541:       VecRestoreArray(mat->slvec1,&ptr);

2543:       /* set slvec1b = 0 */
2544:       VecSet(&zero,mat->slvec1b);

2546:       VecScatterBegin(mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD,mat->sMvctx);
2547:       VecRestoreArray(xx,&x);
2548:       VecRestoreArray(bb,&b);
2549:       VecScatterEnd(mat->slvec0,mat->slvec1,ADD_VALUES,SCATTER_FORWARD,mat->sMvctx);

2551:       /* upper triangular part: bb1 = bb1 - B*x */
2552:       (*mat->B->ops->multadd)(mat->B,mat->slvec1b,mat->slvec1a,bb1);
2553: 
2554:       /* local diagonal sweep */
2555:       (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
2556:     }
2557:     VecDestroy(bb1);
2558:   } else {
2559:     SETERRQ(PETSC_ERR_SUP,"MatSORType is not supported for SBAIJ matrix format");
2560:   }
2561:   return(0);
2562: }

2566: int MatRelax_MPISBAIJ_2comm(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
2567: {
2568:   Mat_MPISBAIJ   *mat = (Mat_MPISBAIJ*)matin->data;
2569:   int            ierr;
2570:   PetscScalar    mone=-1.0;
2571:   Vec            lvec1,bb1;
2572: 
2574:   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
2575:   if (mat->bs > 1)
2576:     SETERRQ(PETSC_ERR_SUP,"SSOR for block size > 1 is not yet implemented");

2578:   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
2579:     if ( flag & SOR_ZERO_INITIAL_GUESS ) {
2580:       (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);
2581:       its--;
2582:     }

2584:     VecDuplicate(mat->lvec,&lvec1);
2585:     VecDuplicate(bb,&bb1);
2586:     while (its--){
2587:       VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);
2588: 
2589:       /* lower diagonal part: bb1 = bb - B^T*xx */
2590:       (*mat->B->ops->multtranspose)(mat->B,xx,lvec1);
2591:       VecScale(&mone,lvec1);

2593:       VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);
2594:       VecCopy(bb,bb1);
2595:       VecScatterBegin(lvec1,bb1,ADD_VALUES,SCATTER_REVERSE,mat->Mvctx);

2597:       /* upper diagonal part: bb1 = bb1 - B*x */
2598:       VecScale(&mone,mat->lvec);
2599:       (*mat->B->ops->multadd)(mat->B,mat->lvec,bb1,bb1);

2601:       VecScatterEnd(lvec1,bb1,ADD_VALUES,SCATTER_REVERSE,mat->Mvctx);
2602: 
2603:       /* diagonal sweep */
2604:       (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
2605:     }
2606:     VecDestroy(lvec1);
2607:     VecDestroy(bb1);
2608:   } else {
2609:     SETERRQ(PETSC_ERR_SUP,"MatSORType is not supported for SBAIJ matrix format");
2610:   }
2611:   return(0);
2612: }