Actual source code: mpibdiag.c

  1: #define PETSCMAT_DLL

  3: /*
  4:    The basic matrix operations for the Block diagonal parallel 
  5:   matrices.
  6: */
 7:  #include src/mat/impls/bdiag/mpi/mpibdiag.h

 11: PetscErrorCode MatSetValues_MPIBDiag(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv)
 12: {
 13:   Mat_MPIBDiag   *mbd = (Mat_MPIBDiag*)mat->data;
 15:   PetscInt       i,j,row,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
 16:   PetscTruth     roworiented = mbd->roworiented;

 19:   for (i=0; i<m; i++) {
 20:     if (idxm[i] < 0) continue;
 21:     if (idxm[i] >= mat->rmap.N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
 22:     if (idxm[i] >= rstart && idxm[i] < rend) {
 23:       row = idxm[i] - rstart;
 24:       for (j=0; j<n; j++) {
 25:         if (idxn[j] < 0) continue;
 26:         if (idxn[j] >= mat->cmap.N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
 27:         if (roworiented) {
 28:           MatSetValues(mbd->A,1,&row,1,&idxn[j],v+i*n+j,addv);
 29:         } else {
 30:           MatSetValues(mbd->A,1,&row,1,&idxn[j],v+i+j*m,addv);
 31:         }
 32:       }
 33:     } else {
 34:       if (!mbd->donotstash) {
 35:         if (roworiented) {
 36:           MatStashValuesRow_Private(&mat->stash,idxm[i],n,idxn,v+i*n);
 37:         } else {
 38:           MatStashValuesCol_Private(&mat->stash,idxm[i],n,idxn,v+i,m);
 39:         }
 40:       }
 41:     }
 42:   }
 43:   return(0);
 44: }

 48: PetscErrorCode MatGetValues_MPIBDiag(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
 49: {
 50:   Mat_MPIBDiag   *mbd = (Mat_MPIBDiag*)mat->data;
 52:   PetscInt       i,j,row,rstart = mat->rmap.rstart,rend = mat->rmap.rend;

 55:   for (i=0; i<m; i++) {
 56:     if (idxm[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
 57:     if (idxm[i] >= mat->rmap.N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
 58:     if (idxm[i] >= rstart && idxm[i] < rend) {
 59:       row = idxm[i] - rstart;
 60:       for (j=0; j<n; j++) {
 61:         if (idxn[j] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative column");
 62:         if (idxn[j] >= mat->cmap.N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
 63:         MatGetValues(mbd->A,1,&row,1,&idxn[j],v+i*n+j);
 64:       }
 65:     } else {
 66:       SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
 67:     }
 68:   }
 69:   return(0);
 70: }

 74: PetscErrorCode MatAssemblyBegin_MPIBDiag(Mat mat,MatAssemblyType mode)
 75: {
 76:   MPI_Comm       comm = mat->comm;
 78:   PetscInt       nstash,reallocs;
 79:   InsertMode     addv;

 82:   MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,comm);
 83:   if (addv == (ADD_VALUES|INSERT_VALUES)) {
 84:     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix adds/inserts on different procs");
 85:   }
 86:   mat->insertmode = addv; /* in case this processor had no cache */
 87:   MatStashScatterBegin_Private(&mat->stash,mat->rmap.range);
 88:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
 89:   PetscInfo2(0,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);
 90:   return(0);
 91: }

 95: PetscErrorCode MatAssemblyEnd_MPIBDiag(Mat mat,MatAssemblyType mode)
 96: {
 97:   Mat_MPIBDiag   *mbd = (Mat_MPIBDiag*)mat->data;
 98:   Mat_SeqBDiag   *mlocal;
100:   PetscMPIInt    n;
101:   PetscInt       i,*row,*col;
102:   PetscInt       *tmp1,*tmp2,len,ict,Mblock,Nblock,flg,j,rstart,ncols;
103:   PetscScalar    *val;
104:   InsertMode     addv = mat->insertmode;


108:   while (1) {
109:     MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
110:     if (!flg) break;
111: 
112:     for (i=0; i<n;) {
113:       /* Now identify the consecutive vals belonging to the same row */
114:       for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
115:       if (j < n) ncols = j-i;
116:       else       ncols = n-i;
117:       /* Now assemble all these values with a single function call */
118:       MatSetValues_MPIBDiag(mat,1,row+i,ncols,col+i,val+i,addv);
119:       i = j;
120:     }
121:   }
122:   MatStashScatterEnd_Private(&mat->stash);

124:   MatAssemblyBegin(mbd->A,mode);
125:   MatAssemblyEnd(mbd->A,mode);

127:   /* Fix main diagonal location and determine global diagonals */
128:   mlocal         = (Mat_SeqBDiag*)mbd->A->data;
129:   Mblock         = mat->rmap.N/mat->rmap.bs; Nblock = mat->cmap.N/mat->rmap.bs;
130:   len            = Mblock + Nblock + 1; /* add 1 to prevent 0 malloc */
131:   PetscMalloc(2*len*sizeof(PetscInt),&tmp1);
132:   tmp2           = tmp1 + len;
133:   PetscMemzero(tmp1,2*len*sizeof(PetscInt));
134:   mlocal->mainbd = -1;
135:   for (i=0; i<mlocal->nd; i++) {
136:     if (mlocal->diag[i] + mbd->brstart == 0) mlocal->mainbd = i;
137:     tmp1[mlocal->diag[i] + mbd->brstart + Mblock] = 1;
138:   }
139:   MPI_Allreduce(tmp1,tmp2,len,MPIU_INT,MPI_SUM,mat->comm);
140:   ict  = 0;
141:   for (i=0; i<len; i++) {
142:     if (tmp2[i]) {
143:       mbd->gdiag[ict] = i - Mblock;
144:       ict++;
145:     }
146:   }
147:   mbd->gnd = ict;
148:   PetscFree(tmp1);

150:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
151:     MatSetUpMultiply_MPIBDiag(mat);
152:   }
153:   return(0);
154: }

158: PetscErrorCode MatZeroEntries_MPIBDiag(Mat A)
159: {
160:   Mat_MPIBDiag   *l = (Mat_MPIBDiag*)A->data;

164:   MatZeroEntries(l->A);
165:   return(0);
166: }

168: /* again this uses the same basic stratagy as in the assembly and 
169:    scatter create routines, we should try to do it systematically 
170:    if we can figure out the proper level of generality. */

172: /* the code does not do the diagonal entries correctly unless the 
173:    matrix is square and the column and row owerships are identical.
174:    This is a BUG. The only way to fix it seems to be to access 
175:    aij->A and aij->B directly and not through the MatZeroRows() 
176:    routine. 
177: */

181: PetscErrorCode MatZeroRows_MPIBDiag(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
182: {
183:   Mat_MPIBDiag   *l = (Mat_MPIBDiag*)A->data;
185:   PetscMPIInt    n,imdex,size = l->size,rank = l->rank,tag = A->tag;
186:   PetscInt       i,*owners = A->rmap.range;
187:   PetscInt       *nprocs,j,idx,nsends;
188:   PetscInt       nmax,*svalues,*starts,*owner,nrecvs;
189:   PetscInt       *rvalues,count,base,slen,*source;
190:   PetscInt       *lens,*lrows,*values;
191:   MPI_Comm       comm = A->comm;
192:   MPI_Request    *send_waits,*recv_waits;
193:   MPI_Status     recv_status,*send_status;
194:   PetscTruth     found;

197:   /*  first count number of contributors to each processor */
198:   PetscMalloc(2*size*sizeof(PetscInt),&nprocs);
199:   PetscMemzero(nprocs,2*size*sizeof(PetscInt));
200:   PetscMalloc((N+1)*sizeof(PetscInt),&owner); /* see note*/
201:   for (i=0; i<N; i++) {
202:     idx = rows[i];
203:     found = PETSC_FALSE;
204:     for (j=0; j<size; j++) {
205:       if (idx >= owners[j] && idx < owners[j+1]) {
206:         nprocs[2*j]++; nprocs[2*j+1] = 1; owner[i] = j; found = PETSC_TRUE; break;
207:       }
208:     }
209:     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
210:   }
211:   nsends = 0;  for (i=0; i<size; i++) {nsends += nprocs[2*i+1];}

213:   /* inform other processors of number of messages and max length*/
214:   PetscMaxSum(comm,nprocs,&nmax,&nrecvs);

216:   /* post receives:   */
217:   PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);
218:   PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);
219:   for (i=0; i<nrecvs; i++) {
220:     MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);
221:   }

223:   /* do sends:
224:       1) starts[i] gives the starting index in svalues for stuff going to 
225:          the ith processor
226:   */
227:   PetscMalloc((N+1)*sizeof(PetscInt),&svalues);
228:   PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);
229:   PetscMalloc((size+1)*sizeof(PetscInt),&starts);
230:   starts[0] = 0;
231:   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
232:   for (i=0; i<N; i++) {
233:     svalues[starts[owner[i]]++] = rows[i];
234:   }

236:   starts[0] = 0;
237:   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
238:   count = 0;
239:   for (i=0; i<size; i++) {
240:     if (nprocs[2*i+1]) {
241:       MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);
242:     }
243:   }
244:   PetscFree(starts);

246:   base = owners[rank];

248:   /*  wait on receives */
249:   PetscMalloc(2*(nrecvs+1)*sizeof(PetscInt),&lens);
250:   source = lens + nrecvs;
251:   count  = nrecvs;
252:   slen   = 0;
253:   while (count) {
254:     MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);
255:     /* unpack receives into our local space */
256:     MPI_Get_count(&recv_status,MPIU_INT,&n);
257:     source[imdex]  = recv_status.MPI_SOURCE;
258:     lens[imdex]  = n;
259:     slen += n;
260:     count--;
261:   }
262:   PetscFree(recv_waits);
263: 
264:   /* move the data into the send scatter */
265:   PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);
266:   count = 0;
267:   for (i=0; i<nrecvs; i++) {
268:     values = rvalues + i*nmax;
269:     for (j=0; j<lens[i]; j++) {
270:       lrows[count++] = values[j] - base;
271:     }
272:   }
273:   PetscFree(rvalues);
274:   PetscFree(lens);
275:   PetscFree(owner);
276:   PetscFree(nprocs);
277: 
278:   /* actually zap the local rows */
279:   MatZeroRows(l->A,slen,lrows,diag);
280:   PetscFree(lrows);

282:   /* wait on sends */
283:   if (nsends) {
284:     PetscMalloc(nsends*sizeof(MPI_Status),&send_status);
285:     MPI_Waitall(nsends,send_waits,send_status);
286:     PetscFree(send_status);
287:   }
288:   PetscFree(send_waits);
289:   PetscFree(svalues);

291:   return(0);
292: }

296: PetscErrorCode MatMult_MPIBDiag(Mat mat,Vec xx,Vec yy)
297: {
298:   Mat_MPIBDiag   *mbd = (Mat_MPIBDiag*)mat->data;

302:   VecScatterBegin(xx,mbd->lvec,INSERT_VALUES,SCATTER_FORWARD,mbd->Mvctx);
303:   VecScatterEnd(xx,mbd->lvec,INSERT_VALUES,SCATTER_FORWARD,mbd->Mvctx);
304:   (*mbd->A->ops->mult)(mbd->A,mbd->lvec,yy);
305:   return(0);
306: }

310: PetscErrorCode MatMultAdd_MPIBDiag(Mat mat,Vec xx,Vec yy,Vec zz)
311: {
312:   Mat_MPIBDiag   *mbd = (Mat_MPIBDiag*)mat->data;

316:   VecScatterBegin(xx,mbd->lvec,INSERT_VALUES,SCATTER_FORWARD,mbd->Mvctx);
317:   VecScatterEnd(xx,mbd->lvec,INSERT_VALUES,SCATTER_FORWARD,mbd->Mvctx);
318:   (*mbd->A->ops->multadd)(mbd->A,mbd->lvec,yy,zz);
319:   return(0);
320: }

324: PetscErrorCode MatMultTranspose_MPIBDiag(Mat A,Vec xx,Vec yy)
325: {
326:   Mat_MPIBDiag   *a = (Mat_MPIBDiag*)A->data;
328:   PetscScalar    zero = 0.0;

331:   VecSet(yy,zero);
332:   (*a->A->ops->multtranspose)(a->A,xx,a->lvec);
333:   VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
334:   VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
335:   return(0);
336: }

340: PetscErrorCode MatMultTransposeAdd_MPIBDiag(Mat A,Vec xx,Vec yy,Vec zz)
341: {
342:   Mat_MPIBDiag   *a = (Mat_MPIBDiag*)A->data;

346:   VecCopy(yy,zz);
347:   (*a->A->ops->multtranspose)(a->A,xx,a->lvec);
348:   VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
349:   VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
350:   return(0);
351: }

355: PetscErrorCode MatGetInfo_MPIBDiag(Mat matin,MatInfoType flag,MatInfo *info)
356: {
357:   Mat_MPIBDiag   *mat = (Mat_MPIBDiag*)matin->data;
359:   PetscReal      isend[5],irecv[5];

362:   info->block_size     = (PetscReal)mat->A->rmap.bs;
363:   MatGetInfo(mat->A,MAT_LOCAL,info);
364:   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
365:   isend[3] = info->memory;  isend[4] = info->mallocs;
366:   if (flag == MAT_LOCAL) {
367:     info->nz_used      = isend[0];
368:     info->nz_allocated = isend[1];
369:     info->nz_unneeded  = isend[2];
370:     info->memory       = isend[3];
371:     info->mallocs      = isend[4];
372:   } else if (flag == MAT_GLOBAL_MAX) {
373:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);
374:     info->nz_used      = irecv[0];
375:     info->nz_allocated = irecv[1];
376:     info->nz_unneeded  = irecv[2];
377:     info->memory       = irecv[3];
378:     info->mallocs      = irecv[4];
379:   } else if (flag == MAT_GLOBAL_SUM) {
380:     MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);
381:     info->nz_used      = irecv[0];
382:     info->nz_allocated = irecv[1];
383:     info->nz_unneeded  = irecv[2];
384:     info->memory       = irecv[3];
385:     info->mallocs      = irecv[4];
386:   }
387:   info->rows_global    = (double)matin->rmap.N;
388:   info->columns_global = (double)matin->cmap.N;
389:   info->rows_local     = (double)matin->rmap.n;
390:   info->columns_local  = (double)matin->cmap.N;
391:   return(0);
392: }

396: PetscErrorCode MatGetDiagonal_MPIBDiag(Mat mat,Vec v)
397: {
399:   Mat_MPIBDiag   *A = (Mat_MPIBDiag*)mat->data;

402:   MatGetDiagonal(A->A,v);
403:   return(0);
404: }

408: PetscErrorCode MatDestroy_MPIBDiag(Mat mat)
409: {
410:   Mat_MPIBDiag   *mbd = (Mat_MPIBDiag*)mat->data;
412: #if defined(PETSC_USE_LOG)
413:   Mat_SeqBDiag   *ms = (Mat_SeqBDiag*)mbd->A->data;

416:   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D, BSize=%D, NDiag=%D",mat->rmap.N,mat->cmap.N,mat->rmap.bs,ms->nd);
417: #else
419: #endif
420:   MatStashDestroy_Private(&mat->stash);
421:   PetscFree(mbd->gdiag);
422:   MatDestroy(mbd->A);
423:   if (mbd->lvec) {VecDestroy(mbd->lvec);}
424:   if (mbd->Mvctx) {VecScatterDestroy(mbd->Mvctx);}
425:   PetscFree(mbd);

427:   PetscObjectChangeTypeName((PetscObject)mat,0);
428:   PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);
429:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIBDiagSetPreallocation_C","",PETSC_NULL);
430:   return(0);
431: }


436: static PetscErrorCode MatView_MPIBDiag_Binary(Mat mat,PetscViewer viewer)
437: {
438:   Mat_MPIBDiag   *mbd = (Mat_MPIBDiag*)mat->data;

442:   if (mbd->size == 1) {
443:     MatView(mbd->A,viewer);
444:   } else SETERRQ(PETSC_ERR_SUP,"Only uniprocessor output supported");
445:   return(0);
446: }

450: static PetscErrorCode MatView_MPIBDiag_ASCIIorDraw(Mat mat,PetscViewer viewer)
451: {
452:   Mat_MPIBDiag      *mbd = (Mat_MPIBDiag*)mat->data;
453:   PetscErrorCode    ierr;
454:   PetscMPIInt       size = mbd->size,rank = mbd->rank;
455:   PetscInt          i;
456:   PetscTruth        iascii,isdraw;
457:   PetscViewer       sviewer;
458:   PetscViewerFormat format;

461:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
462:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
463:   if (iascii) {
464:     PetscViewerGetFormat(viewer,&format);
465:     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
466:       PetscInt nline = PetscMin(10,mbd->gnd),k,nk,np;
467:       PetscViewerASCIIPrintf(viewer,"  block size=%D, total number of diagonals=%D\n",mat->rmap.bs,mbd->gnd);
468:       nk = (mbd->gnd-1)/nline + 1;
469:       for (k=0; k<nk; k++) {
470:         PetscViewerASCIIPrintf(viewer,"  global diag numbers:");
471:         np = PetscMin(nline,mbd->gnd - nline*k);
472:         for (i=0; i<np; i++) {
473:           PetscViewerASCIIPrintf(viewer,"  %D",mbd->gdiag[i+nline*k]);
474:         }
475:         PetscViewerASCIIPrintf(viewer,"\n");
476:       }
477:       if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
478:         MatInfo info;
479:         MPI_Comm_rank(mat->comm,&rank);
480:         MatGetInfo(mat,MAT_LOCAL,&info);
481:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] local rows %D nz %D nz alloced %D mem %D \n",rank,mat->rmap.N,
482:             (PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);
483:         PetscViewerFlush(viewer);
484:         VecScatterView(mbd->Mvctx,viewer);
485:       }
486:       return(0);
487:     }
488:   }

490:   if (isdraw) {
491:     PetscDraw       draw;
492:     PetscTruth isnull;
493:     PetscViewerDrawGetDraw(viewer,0,&draw);
494:     PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
495:   }

497:   if (size == 1) {
498:     MatView(mbd->A,viewer);
499:   } else {
500:     /* assemble the entire matrix onto first processor. */
501:     Mat          A;
502:     PetscInt     M = mat->rmap.N,N = mat->cmap.N,m,row,nz,*cols;
503:     PetscScalar  *vals;

505:     /* Here we are constructing a temporary matrix, so we will explicitly set the type to MPIBDiag */
506:     MatCreate(mat->comm,&A);
507:     if (!rank) {
508:       MatSetSizes(A,M,N,M,N);
509:       MatSetType(A,MATMPIBDIAG);
510:       MatMPIBDiagSetPreallocation(A,mbd->gnd,mbd->A->rmap.bs,mbd->gdiag,PETSC_NULL);
511:     } else {
512:       MatSetSizes(A,0,0,M,N);
513:       MatSetType(A,MATMPIBDIAG);
514:       MatMPIBDiagSetPreallocation(A,0,mbd->A->rmap.bs,PETSC_NULL,PETSC_NULL);
515:     }
516:     PetscLogObjectParent(mat,A);

518:     /* Copy the matrix ... This isn't the most efficient means,
519:        but it's quick for now */
520:     row = mat->rmap.rstart;
521:     m = mbd->A->rmap.N;
522:     for (i=0; i<m; i++) {
523:       MatGetRow_MPIBDiag(mat,row,&nz,&cols,&vals);
524:       MatSetValues(A,1,&row,nz,cols,vals,INSERT_VALUES);
525:       MatRestoreRow_MPIBDiag(mat,row,&nz,&cols,&vals);
526:       row++;
527:     }
528:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
529:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
530:     PetscViewerGetSingleton(viewer,&sviewer);
531:     if (!rank) {
532:       MatView(((Mat_MPIBDiag*)(A->data))->A,sviewer);
533:     }
534:     PetscViewerRestoreSingleton(viewer,&sviewer);
535:     PetscViewerFlush(viewer);
536:     MatDestroy(A);
537:   }
538:   return(0);
539: }

543: PetscErrorCode MatView_MPIBDiag(Mat mat,PetscViewer viewer)
544: {
546:   PetscTruth     iascii,isdraw,isbinary;

549:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
550:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
551:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
552:   if (iascii || isdraw) {
553:     MatView_MPIBDiag_ASCIIorDraw(mat,viewer);
554:   } else if (isbinary) {
555:     MatView_MPIBDiag_Binary(mat,viewer);
556:   } else {
557:     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by MPIBdiag matrices",((PetscObject)viewer)->type_name);
558:   }
559:   return(0);
560: }

564: PetscErrorCode MatSetOption_MPIBDiag(Mat A,MatOption op)
565: {
566:   Mat_MPIBDiag   *mbd = (Mat_MPIBDiag*)A->data;

569:   switch (op) {
570:   case MAT_NO_NEW_NONZERO_LOCATIONS:
571:   case MAT_YES_NEW_NONZERO_LOCATIONS:
572:   case MAT_NEW_NONZERO_LOCATION_ERR:
573:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
574:   case MAT_NO_NEW_DIAGONALS:
575:   case MAT_YES_NEW_DIAGONALS:
576:     MatSetOption(mbd->A,op);
577:     break;
578:   case MAT_ROW_ORIENTED:
579:     mbd->roworiented = PETSC_TRUE;
580:     MatSetOption(mbd->A,op);
581:     break;
582:   case MAT_COLUMN_ORIENTED:
583:     mbd->roworiented = PETSC_FALSE;
584:     MatSetOption(mbd->A,op);
585:     break;
586:   case MAT_IGNORE_OFF_PROC_ENTRIES:
587:     mbd->donotstash = PETSC_TRUE;
588:     break;
589:   case MAT_ROWS_SORTED:
590:   case MAT_ROWS_UNSORTED:
591:   case MAT_COLUMNS_SORTED:
592:   case MAT_COLUMNS_UNSORTED:
593:   case MAT_SYMMETRIC:
594:   case MAT_STRUCTURALLY_SYMMETRIC:
595:   case MAT_NOT_SYMMETRIC:
596:   case MAT_NOT_STRUCTURALLY_SYMMETRIC:
597:   case MAT_HERMITIAN:
598:   case MAT_NOT_HERMITIAN:
599:   case MAT_SYMMETRY_ETERNAL:
600:   case MAT_NOT_SYMMETRY_ETERNAL:
601:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
602:     break;
603:   default:
604:     SETERRQ1(PETSC_ERR_SUP,"unknown option %d",op);
605:   }
606:   return(0);
607: }


612: PetscErrorCode MatGetRow_MPIBDiag(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
613: {
614:   Mat_MPIBDiag   *mat = (Mat_MPIBDiag*)matin->data;
616:   PetscInt       lrow;

619:   if (row < matin->rmap.rstart || row >= matin->rmap.rend) SETERRQ(PETSC_ERR_SUP,"only for local rows")
620:   lrow = row - matin->rmap.rstart;
621:   MatGetRow_SeqBDiag(mat->A,lrow,nz,idx,v);
622:   return(0);
623: }

627: PetscErrorCode MatRestoreRow_MPIBDiag(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
628: {
629:   Mat_MPIBDiag   *mat = (Mat_MPIBDiag*)matin->data;
631:   PetscInt       lrow;

634:   lrow = row - matin->rmap.rstart;
635:   MatRestoreRow_SeqBDiag(mat->A,lrow,nz,idx,v);
636:   return(0);
637: }


642: PetscErrorCode MatNorm_MPIBDiag(Mat A,NormType type,PetscReal *nrm)
643: {
644:   Mat_MPIBDiag   *mbd = (Mat_MPIBDiag*)A->data;
645:   Mat_SeqBDiag   *a = (Mat_SeqBDiag*)mbd->A->data;
646:   PetscReal      sum = 0.0;
648:   PetscInt       d,i,nd = a->nd,bs = A->rmap.bs,len;
649:   PetscScalar    *dv;

652:   if (type == NORM_FROBENIUS) {
653:     for (d=0; d<nd; d++) {
654:       dv   = a->diagv[d];
655:       len  = a->bdlen[d]*bs*bs;
656:       for (i=0; i<len; i++) {
657: #if defined(PETSC_USE_COMPLEX)
658:         sum += PetscRealPart(PetscConj(dv[i])*dv[i]);
659: #else
660:         sum += dv[i]*dv[i];
661: #endif
662:       }
663:     }
664:     MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPI_SUM,A->comm);
665:     *nrm = sqrt(*nrm);
666:     PetscLogFlops(2*A->rmap.n*A->rmap.N);
667:   } else if (type == NORM_1) { /* max column norm */
668:     PetscReal *tmp,*tmp2;
669:     PetscInt    j;
670:     PetscMalloc((mbd->A->cmap.n+1)*sizeof(PetscReal),&tmp);
671:     PetscMalloc((mbd->A->cmap.n+1)*sizeof(PetscReal),&tmp2);
672:     MatNorm_SeqBDiag_Columns(mbd->A,tmp,mbd->A->cmap.n);
673:     *nrm = 0.0;
674:     MPI_Allreduce(tmp,tmp2,mbd->A->cmap.n,MPIU_REAL,MPI_SUM,A->comm);
675:     for (j=0; j<mbd->A->cmap.n; j++) {
676:       if (tmp2[j] > *nrm) *nrm = tmp2[j];
677:     }
678:     PetscFree(tmp);
679:     PetscFree(tmp2);
680:   } else if (type == NORM_INFINITY) { /* max row norm */
681:     PetscReal normtemp;
682:     MatNorm(mbd->A,type,&normtemp);
683:     MPI_Allreduce(&normtemp,nrm,1,MPIU_REAL,MPI_MAX,A->comm);
684:   }
685:   return(0);
686: }

690: PetscErrorCode MatScale_MPIBDiag(Mat A,PetscScalar alpha)
691: {
693:   Mat_MPIBDiag   *a = (Mat_MPIBDiag*)A->data;

696:   MatScale_SeqBDiag(a->A,alpha);
697:   return(0);
698: }

702: PetscErrorCode MatSetUpPreallocation_MPIBDiag(Mat A)
703: {

707:    MatMPIBDiagSetPreallocation(A,PETSC_DEFAULT,PETSC_DEFAULT,0,0);
708:   return(0);
709: }

711: /* -------------------------------------------------------------------*/

713: static struct _MatOps MatOps_Values = {MatSetValues_MPIBDiag,
714:        MatGetRow_MPIBDiag,
715:        MatRestoreRow_MPIBDiag,
716:        MatMult_MPIBDiag,
717: /* 4*/ MatMultAdd_MPIBDiag,
718:        MatMultTranspose_MPIBDiag,
719:        MatMultTransposeAdd_MPIBDiag,
720:        0,
721:        0,
722:        0,
723: /*10*/ 0,
724:        0,
725:        0,
726:        0,
727:        0,
728: /*15*/ MatGetInfo_MPIBDiag,
729:        0,
730:        MatGetDiagonal_MPIBDiag,
731:        0,
732:        MatNorm_MPIBDiag,
733: /*20*/ MatAssemblyBegin_MPIBDiag,
734:        MatAssemblyEnd_MPIBDiag,
735:        0,
736:        MatSetOption_MPIBDiag,
737:        MatZeroEntries_MPIBDiag,
738: /*25*/ MatZeroRows_MPIBDiag,
739:        0,
740:        0,
741:        0,
742:        0,
743: /*30*/ MatSetUpPreallocation_MPIBDiag,
744:        0,
745:        0,
746:        0,
747:        0,
748: /*35*/ 0,
749:        0,
750:        0,
751:        0,
752:        0,
753: /*40*/ 0,
754:        0,
755:        0,
756:        MatGetValues_MPIBDiag,
757:        0,
758: /*45*/ 0,
759:        MatScale_MPIBDiag,
760:        0,
761:        0,
762:        0,
763: /*50*/ 0,
764:        0,
765:        0,
766:        0,
767:        0,
768: /*55*/ 0,
769:        0,
770:        0,
771:        0,
772:        0,
773: /*60*/ 0,
774:        MatDestroy_MPIBDiag,
775:        MatView_MPIBDiag,
776:        0,
777:        0,
778: /*65*/ 0,
779:        0,
780:        0,
781:        0,
782:        0,
783: /*70*/ 0,
784:        0,
785:        0,
786:        0,
787:        0,
788: /*75*/ 0,
789:        0,
790:        0,
791:        0,
792:        0,
793: /*80*/ 0,
794:        0,
795:        0,
796:        0,
797:        MatLoad_MPIBDiag,
798: /*85*/ 0,
799:        0,
800:        0,
801:        0,
802:        0,
803: /*90*/ 0,
804:        0,
805:        0,
806:        0,
807:        0,
808: /*95*/ 0,
809:        0,
810:        0,
811:        0};

816: PetscErrorCode  MatGetDiagonalBlock_MPIBDiag(Mat A,PetscTruth *iscopy,MatReuse reuse,Mat *a)
817: {
818:   Mat_MPIBDiag   *matin = (Mat_MPIBDiag *)A->data;
820:   PetscInt       lrows,lcols,rstart,rend;
821:   IS             localc,localr;

824:   MatGetLocalSize(A,&lrows,&lcols);
825:   MatGetOwnershipRange(A,&rstart,&rend);
826:   ISCreateStride(PETSC_COMM_SELF,lrows,rstart,1,&localc);
827:   ISCreateStride(PETSC_COMM_SELF,lrows,0,1,&localr);
828:   MatGetSubMatrix(matin->A,localr,localc,PETSC_DECIDE,reuse,a);
829:   ISDestroy(localr);
830:   ISDestroy(localc);

832:   *iscopy = PETSC_TRUE;
833:   return(0);
834: }

840: PetscErrorCode  MatMPIBDiagSetPreallocation_MPIBDiag(Mat B,PetscInt nd,PetscInt bs,PetscInt *diag,PetscScalar **diagv)
841: {
842:   Mat_MPIBDiag   *b;
844:   PetscInt       i,k,*ldiag,len,nd2;
845:   PetscScalar    **ldiagv = 0;
846:   PetscTruth     flg2;

849:   B->preallocated = PETSC_TRUE;
850:   if (bs == PETSC_DEFAULT) bs = 1;
851:   if (nd == PETSC_DEFAULT) nd = 0;
852:   PetscOptionsGetInt(PETSC_NULL,"-mat_block_size",&bs,PETSC_NULL);
853:   PetscOptionsGetInt(PETSC_NULL,"-mat_bdiag_ndiag",&nd,PETSC_NULL);
854:   PetscOptionsHasName(PETSC_NULL,"-mat_bdiag_diags",&flg2);
855:   if (nd && !diag) {
856:     PetscMalloc(nd*sizeof(PetscInt),&diag);
857:     nd2  = nd;
858:     PetscOptionsGetIntArray(PETSC_NULL,"-mat_bdiag_dvals",diag,&nd2,PETSC_NULL);
859:     if (nd2 != nd) {
860:       SETERRQ(PETSC_ERR_ARG_INCOMP,"Incompatible number of diags and diagonal vals");
861:     }
862:   } else if (flg2) {
863:     SETERRQ(PETSC_ERR_ARG_WRONG,"Must specify number of diagonals with -mat_bdiag_ndiag");
864:   }

866:   if (bs <= 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Blocksize must be positive");

868:   B->rmap.bs = B->cmap.bs = bs;

870:   PetscMapInitialize(B->comm,&B->rmap);
871:   PetscMapInitialize(B->comm,&B->cmap);

873:   if ((B->cmap.N%bs)) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size - bad column number");
874:   if ((B->rmap.N%bs)) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size - bad local row number");
875:   if ((B->rmap.N%bs)) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size - bad global row number");


878:   b          = (Mat_MPIBDiag*)B->data;
879:   b->gnd     = nd;
880:   b->brstart = (B->rmap.rstart)/bs;
881:   b->brend   = (B->rmap.rend)/bs;


884:   /* Determine local diagonals; for now, assume global rows = global cols */
885:   /* These are sorted in MatCreateSeqBDiag */
886:   PetscMalloc((nd+1)*sizeof(PetscInt),&ldiag);
887:   len  = B->rmap.N/bs + B->cmap.N/bs + 1;
888:   PetscMalloc(len*sizeof(PetscInt),&b->gdiag);
889:   k    = 0;
890:   if (diagv) {
891:     PetscMalloc((nd+1)*sizeof(PetscScalar*),&ldiagv);
892:   }
893:   for (i=0; i<nd; i++) {
894:     b->gdiag[i] = diag[i];
895:     if (diag[i] > 0) { /* lower triangular */
896:       if (diag[i] < b->brend) {
897:         ldiag[k] = diag[i] - b->brstart;
898:         if (diagv) ldiagv[k] = diagv[i];
899:         k++;
900:       }
901:     } else { /* upper triangular */
902:       if (B->rmap.N/bs - diag[i] > B->cmap.N/bs) {
903:         if (B->rmap.N/bs + diag[i] > b->brstart) {
904:           ldiag[k] = diag[i] - b->brstart;
905:           if (diagv) ldiagv[k] = diagv[i];
906:           k++;
907:         }
908:       } else {
909:         if (B->rmap.N/bs > b->brstart) {
910:           ldiag[k] = diag[i] - b->brstart;
911:           if (diagv) ldiagv[k] = diagv[i];
912:           k++;
913:         }
914:       }
915:     }
916:   }

918:   /* Form local matrix */
919:   MatCreate(PETSC_COMM_SELF,&b->A);
920:   MatSetSizes(b->A,B->rmap.n,B->cmap.N,B->rmap.n,B->cmap.N);
921:   MatSetType(b->A,MATSEQBDIAG);
922:   MatSeqBDiagSetPreallocation(b->A,k,bs,ldiag,ldiagv);
923:   PetscLogObjectParent(B,b->A);
924:   PetscFree(ldiag);
925:   PetscFree(ldiagv);

927:   return(0);
928: }

931: /*MC
932:    MATMPIBDIAG - MATMPIBDIAG = "mpibdiag" - A matrix type to be used for distributed block diagonal matrices.

934:    Options Database Keys:
935: . -mat_type mpibdiag - sets the matrix type to "mpibdiag" during a call to MatSetFromOptions()

937:   Level: beginner

939: .seealso: MatCreateMPIBDiag
940: M*/

945: PetscErrorCode  MatCreate_MPIBDiag(Mat B)
946: {
947:   Mat_MPIBDiag   *b;

951:   PetscNew(Mat_MPIBDiag,&b);
952:   B->data         = (void*)b;
953:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
954:   B->factor       = 0;
955:   B->mapping      = 0;

957:   B->insertmode = NOT_SET_VALUES;
958:   MPI_Comm_rank(B->comm,&b->rank);
959:   MPI_Comm_size(B->comm,&b->size);

961:   /* build cache for off array entries formed */
962:   MatStashCreate_Private(B->comm,1,&B->stash);
963:   b->donotstash = PETSC_FALSE;

965:   /* stuff used for matrix-vector multiply */
966:   b->lvec        = 0;
967:   b->Mvctx       = 0;

969:   /* used for MatSetValues() input */
970:   b->roworiented = PETSC_TRUE;

972:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
973:                                      "MatGetDiagonalBlock_MPIBDiag",
974:                                       MatGetDiagonalBlock_MPIBDiag);
975:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIBDiagSetPreallocation_C",
976:                                      "MatMPIBDiagSetPreallocation_MPIBDiag",
977:                                       MatMPIBDiagSetPreallocation_MPIBDiag);
978:   PetscObjectChangeTypeName((PetscObject)B,MATMPIBDIAG);
979:   return(0);
980: }

983: /*MC
984:    MATBDIAG - MATBDIAG = "bdiag" - A matrix type to be used for block diagonal matrices.

986:    This matrix type is identical to MATSEQBDIAG when constructed with a single process communicator,
987:    and MATMPIBDIAG otherwise.

989:    Options Database Keys:
990: . -mat_type bdiag - sets the matrix type to "bdiag" during a call to MatSetFromOptions()

992:   Level: beginner

994: .seealso: MatCreateMPIBDiag,MATSEQBDIAG,MATMPIBDIAG
995: M*/

1000: PetscErrorCode  MatCreate_BDiag(Mat A)
1001: {
1003:   PetscMPIInt   size;

1006:   MPI_Comm_size(A->comm,&size);
1007:   if (size == 1) {
1008:     MatSetType(A,MATSEQBDIAG);
1009:   } else {
1010:     MatSetType(A,MATMPIBDIAG);
1011:   }
1012:   return(0);
1013: }

1018: /*@C
1019:    MatMPIBDiagSetPreallocation - 

1021:    Collective on Mat

1023:    Input Parameters:
1024: +  A - the matrix 
1025: .  nd - number of block diagonals (global) (optional)
1026: .  bs - each element of a diagonal is an bs x bs dense matrix
1027: .  diag - optional array of block diagonal numbers (length nd).
1028:    For a matrix element A[i,j], where i=row and j=column, the
1029:    diagonal number is
1030: $     diag = i/bs - j/bs  (integer division)
1031:    Set diag=PETSC_NULL on input for PETSc to dynamically allocate memory as 
1032:    needed (expensive).
1033: -  diagv  - pointer to actual diagonals (in same order as diag array), 
1034:    if allocated by user. Otherwise, set diagv=PETSC_NULL on input for PETSc
1035:    to control memory allocation.


1038:    Options Database Keys:
1039: .  -mat_block_size <bs> - Sets blocksize
1040: .  -mat_bdiag_diags <s1,s2,s3,...> - Sets diagonal numbers

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

1046:    The parallel matrix is partitioned across the processors by rows, where
1047:    each local rectangular matrix is stored in the uniprocessor block 
1048:    diagonal format.  See the users manual for further details.

1050:    The user MUST specify either the local or global numbers of rows
1051:    (possibly both).

1053:    The case bs=1 (conventional diagonal storage) is implemented as
1054:    a special case.

1056:    Fortran Notes:
1057:    Fortran programmers cannot set diagv; this variable is ignored.

1059:    Level: intermediate

1061: .keywords: matrix, block, diagonal, parallel, sparse

1063: .seealso: MatCreate(), MatCreateSeqBDiag(), MatSetValues()
1064: @*/
1065: PetscErrorCode  MatMPIBDiagSetPreallocation(Mat B,PetscInt nd,PetscInt bs,const PetscInt diag[],PetscScalar *diagv[])
1066: {
1067:   PetscErrorCode ierr,(*f)(Mat,PetscInt,PetscInt,const PetscInt[],PetscScalar*[]);

1070:   PetscObjectQueryFunction((PetscObject)B,"MatMPIBDiagSetPreallocation_C",(void (**)(void))&f);
1071:   if (f) {
1072:     (*f)(B,nd,bs,diag,diagv);
1073:   }
1074:   return(0);
1075: }

1079: /*@C
1080:    MatCreateMPIBDiag - Creates a sparse parallel matrix in MPIBDiag format.

1082:    Collective on MPI_Comm

1084:    Input Parameters:
1085: +  comm - MPI communicator
1086: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
1087: .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
1088: .  N - number of columns (local and global)
1089: .  nd - number of block diagonals (global) (optional)
1090: .  bs - each element of a diagonal is an bs x bs dense matrix
1091: .  diag - optional array of block diagonal numbers (length nd).
1092:    For a matrix element A[i,j], where i=row and j=column, the
1093:    diagonal number is
1094: $     diag = i/bs - j/bs  (integer division)
1095:    Set diag=PETSC_NULL on input for PETSc to dynamically allocate memory as 
1096:    needed (expensive).
1097: -  diagv  - pointer to actual diagonals (in same order as diag array), 
1098:    if allocated by user. Otherwise, set diagv=PETSC_NULL on input for PETSc
1099:    to control memory allocation.

1101:    Output Parameter:
1102: .  A - the matrix 

1104:    Options Database Keys:
1105: .  -mat_block_size <bs> - Sets blocksize
1106: .  -mat_bdiag_diags <s1,s2,s3,...> - Sets diagonal numbers

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

1112:    The parallel matrix is partitioned across the processors by rows, where
1113:    each local rectangular matrix is stored in the uniprocessor block 
1114:    diagonal format.  See the users manual for further details.

1116:    The user MUST specify either the local or global numbers of rows
1117:    (possibly both).

1119:    The case bs=1 (conventional diagonal storage) is implemented as
1120:    a special case.

1122:    Fortran Notes:
1123:    Fortran programmers cannot set diagv; this variable is ignored.

1125:    Level: intermediate

1127: .keywords: matrix, block, diagonal, parallel, sparse

1129: .seealso: MatCreate(), MatCreateSeqBDiag(), MatSetValues()
1130: @*/
1131: PetscErrorCode  MatCreateMPIBDiag(MPI_Comm comm,PetscInt m,PetscInt M,PetscInt N,PetscInt nd,PetscInt bs,const PetscInt diag[],PetscScalar *diagv[],Mat *A)
1132: {
1134:   PetscMPIInt    size;

1137:   MatCreate(comm,A);
1138:   MatSetSizes(*A,m,m,M,N);
1139:   MPI_Comm_size(comm,&size);
1140:   if (size > 1) {
1141:     MatSetType(*A,MATMPIBDIAG);
1142:     MatMPIBDiagSetPreallocation(*A,nd,bs,diag,diagv);
1143:   } else {
1144:     MatSetType(*A,MATSEQBDIAG);
1145:     MatSeqBDiagSetPreallocation(*A,nd,bs,diag,diagv);
1146:   }
1147:   return(0);
1148: }

1152: /*@C
1153:    MatBDiagGetData - Gets the data for the block diagonal matrix format.
1154:    For the parallel case, this returns information for the local submatrix.

1156:    Input Parameters:
1157: .  mat - the matrix, stored in block diagonal format.

1159:    Not Collective

1161:    Output Parameters:
1162: +  m - number of rows
1163: .  n - number of columns
1164: .  nd - number of block diagonals
1165: .  bs - each element of a diagonal is an bs x bs dense matrix
1166: .  bdlen - array of total block lengths of block diagonals
1167: .  diag - optional array of block diagonal numbers (length nd).
1168:    For a matrix element A[i,j], where i=row and j=column, the
1169:    diagonal number is
1170: $     diag = i/bs - j/bs  (integer division)
1171:    Set diag=PETSC_NULL on input for PETSc to dynamically allocate memory as 
1172:    needed (expensive).
1173: -  diagv - pointer to actual diagonals (in same order as diag array), 

1175:    Level: advanced

1177:    Notes:
1178:    See the users manual for further details regarding this storage format.

1180: .keywords: matrix, block, diagonal, get, data

1182: .seealso: MatCreateSeqBDiag(), MatCreateMPIBDiag()
1183: @*/
1184: PetscErrorCode  MatBDiagGetData(Mat mat,PetscInt *nd,PetscInt *bs,PetscInt *diag[],PetscInt *bdlen[],PetscScalar ***diagv)
1185: {
1186:   Mat_MPIBDiag   *pdmat;
1187:   Mat_SeqBDiag   *dmat = 0;
1188:   PetscTruth     isseq,ismpi;

1193:   PetscTypeCompare((PetscObject)mat,MATSEQBDIAG,&isseq);
1194:   PetscTypeCompare((PetscObject)mat,MATMPIBDIAG,&ismpi);
1195:   if (isseq) {
1196:     dmat = (Mat_SeqBDiag*)mat->data;
1197:   } else if (ismpi) {
1198:     pdmat = (Mat_MPIBDiag*)mat->data;
1199:     dmat = (Mat_SeqBDiag*)pdmat->A->data;
1200:   } else SETERRQ(PETSC_ERR_SUP,"Valid only for MATSEQBDIAG and MATMPIBDIAG formats");
1201:   *nd    = dmat->nd;
1202:   *bs    = mat->rmap.bs;
1203:   *diag  = dmat->diag;
1204:   *bdlen = dmat->bdlen;
1205:   *diagv = dmat->diagv;
1206:   return(0);
1207: }

1209:  #include petscsys.h

1213: PetscErrorCode MatLoad_MPIBDiag(PetscViewer viewer, MatType type,Mat *newmat)
1214: {
1215:   Mat            A;
1216:   PetscScalar    *vals,*svals;
1217:   MPI_Comm       comm = ((PetscObject)viewer)->comm;
1218:   MPI_Status     status;
1220:   int            fd;
1221:   PetscMPIInt    tag = ((PetscObject)viewer)->tag,rank,size,*sndcounts = 0,*rowners,maxnz,mm;
1222:   PetscInt       bs,i,nz,j,rstart,rend,*cols;
1223:   PetscInt       header[4],*rowlengths = 0,M,N,m,Mbs;
1224:   PetscInt       *ourlens,*procsnz = 0,jj,*mycols,*smycols;
1225:   PetscInt       extra_rows;

1228:   MPI_Comm_size(comm,&size);
1229:   MPI_Comm_rank(comm,&rank);
1230:   if (!rank) {
1231:     PetscViewerBinaryGetDescriptor(viewer,&fd);
1232:     PetscBinaryRead(fd,(char *)header,4,PETSC_INT);
1233:     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
1234:     if (header[3] < 0) {
1235:       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format,cannot load as MPIBDiag");
1236:     }
1237:   }
1238:   MPI_Bcast(header+1,3,MPIU_INT,0,comm);
1239:   M = header[1]; N = header[2];

1241:   bs = 1;   /* uses a block size of 1 by default; */
1242:   PetscOptionsGetInt(PETSC_NULL,"-matload_block_size",&bs,PETSC_NULL);

1244:   /* 
1245:      This code adds extra rows to make sure the number of rows is 
1246:      divisible by the blocksize
1247:   */
1248:   Mbs        = M/bs;
1249:   extra_rows = bs - M + bs*(Mbs);
1250:   if (extra_rows == bs) extra_rows = 0;
1251:   else                  Mbs++;
1252:   if (extra_rows && !rank) {
1253:     PetscInfo(0,"Padding loaded matrix to match blocksize\n");
1254:   }

1256:   /* determine ownership of all rows */
1257:   m          = bs*(Mbs/size + ((Mbs % size) > rank));
1258:   PetscMalloc((size+2)*sizeof(PetscInt),&rowners);
1259:   mm         = (PetscMPIInt)m;
1260:   MPI_Allgather(&mm,1,MPI_INT,rowners+1,1,MPI_INT,comm);
1261:   rowners[0] = 0;
1262:   for (i=2; i<=size; i++) {
1263:     rowners[i] += rowners[i-1];
1264:   }
1265:   rstart = rowners[rank];
1266:   rend   = rowners[rank+1];

1268:   /* distribute row lengths to all processors */
1269:   PetscMalloc((rend-rstart)*sizeof(PetscInt),&ourlens);
1270:   if (!rank) {
1271:     PetscMalloc((M+extra_rows)*sizeof(PetscInt),&rowlengths);
1272:     PetscBinaryRead(fd,rowlengths,M,PETSC_INT);
1273:     for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
1274:     PetscMalloc(size*sizeof(PetscMPIInt),&sndcounts);
1275:     for (i=0; i<size; i++) sndcounts[i] = rowners[i+1] - rowners[i];
1276:     MPI_Scatterv(rowlengths,sndcounts,rowners,MPIU_INT,ourlens,rend-rstart,MPIU_INT,0,comm);
1277:     PetscFree(sndcounts);
1278:   } else {
1279:     MPI_Scatterv(0,0,0,MPIU_INT,ourlens,rend-rstart,MPIU_INT,0,comm);
1280:   }

1282:   if (!rank) {
1283:     /* calculate the number of nonzeros on each processor */
1284:     PetscMalloc(size*sizeof(PetscInt),&procsnz);
1285:     PetscMemzero(procsnz,size*sizeof(PetscInt));
1286:     for (i=0; i<size; i++) {
1287:       for (j=rowners[i]; j<rowners[i+1]; j++) {
1288:         procsnz[i] += rowlengths[j];
1289:       }
1290:     }
1291:     PetscFree(rowlengths);

1293:     /* determine max buffer needed and allocate it */
1294:     maxnz = 0;
1295:     for (i=0; i<size; i++) {
1296:       maxnz = PetscMax(maxnz,procsnz[i]);
1297:     }
1298:     PetscMalloc(maxnz*sizeof(PetscInt),&cols);

1300:     /* read in my part of the matrix column indices  */
1301:     nz   = procsnz[0];
1302:     PetscMalloc(nz*sizeof(PetscInt),&mycols);
1303:     if (size == 1)  nz -= extra_rows;
1304:     PetscBinaryRead(fd,mycols,nz,PETSC_INT);
1305:     if (size == 1)  for (i=0; i<extra_rows; i++) { mycols[nz+i] = M+i; }

1307:     /* read in every one elses and ship off */
1308:     for (i=1; i<size-1; i++) {
1309:       nz   = procsnz[i];
1310:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
1311:       MPI_Send(cols,nz,MPIU_INT,i,tag,comm);
1312:     }
1313:     /* read in the stuff for the last proc */
1314:     if (size != 1) {
1315:       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
1316:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
1317:       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
1318:       MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);
1319:     }
1320:     PetscFree(cols);
1321:   } else {
1322:     /* determine buffer space needed for message */
1323:     nz = 0;
1324:     for (i=0; i<m; i++) {
1325:       nz += ourlens[i];
1326:     }
1327:     PetscMalloc(nz*sizeof(PetscInt),&mycols);

1329:     /* receive message of column indices*/
1330:     MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);
1331:     MPI_Get_count(&status,MPIU_INT,&maxnz);
1332:     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1333:   }

1335:   MatCreate(comm,newmat);
1336:   MatSetSizes(*newmat,m,m,M+extra_rows,N+extra_rows);
1337:   MatSetType(*newmat,type);
1338:   MatMPIBDiagSetPreallocation(*newmat,0,bs,PETSC_NULL,PETSC_NULL);
1339:   A = *newmat;

1341:   if (!rank) {
1342:     PetscMalloc(maxnz*sizeof(PetscScalar),&vals);

1344:     /* read in my part of the matrix numerical values  */
1345:     nz = procsnz[0];
1346:     if (size == 1)  nz -= extra_rows;
1347:     PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
1348:     if (size == 1)  for (i=0; i<extra_rows; i++) { vals[nz+i] = 1.0; }

1350:     /* insert into matrix */
1351:     jj      = rstart;
1352:     smycols = mycols;
1353:     svals   = vals;
1354:     for (i=0; i<m; i++) {
1355:       MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
1356:       smycols += ourlens[i];
1357:       svals   += ourlens[i];
1358:       jj++;
1359:     }

1361:     /* read in other processors (except the last one) and ship out */
1362:     for (i=1; i<size-1; i++) {
1363:       nz   = procsnz[i];
1364:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
1365:       MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);
1366:     }
1367:     /* the last proc */
1368:     if (size != 1){
1369:       nz   = procsnz[i] - extra_rows;
1370:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
1371:       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
1372:       MPI_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,A->tag,comm);
1373:     }
1374:     PetscFree(procsnz);
1375:   } else {
1376:     /* receive numeric values */
1377:     PetscMalloc(nz*sizeof(PetscScalar),&vals);

1379:     /* receive message of values*/
1380:     MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);
1381:     MPI_Get_count(&status,MPIU_SCALAR,&maxnz);
1382:     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");

1384:     /* insert into matrix */
1385:     jj      = rstart;
1386:     smycols = mycols;
1387:     svals   = vals;
1388:     for (i=0; i<m; i++) {
1389:       MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
1390:       smycols += ourlens[i];
1391:       svals   += ourlens[i];
1392:       jj++;
1393:     }
1394:   }
1395:   PetscFree(ourlens);
1396:   PetscFree(vals);
1397:   PetscFree(mycols);
1398:   PetscFree(rowners);

1400:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1401:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1402:   return(0);
1403: }