Actual source code: mpidense.c

petsc-master 2016-08-29
Report Typos and Errors
  2: /*
  3:    Basic functions for basic parallel dense matrices.
  4: */


 7:  #include <../src/mat/impls/dense/mpi/mpidense.h>
 8:  #include <../src/mat/impls/aij/mpi/mpiaij.h>
 9:  #include <petscblaslapack.h>

 13: /*@

 15:       MatDenseGetLocalMatrix - For a MATMPIDENSE or MATSEQDENSE matrix returns the sequential
 16:               matrix that represents the operator. For sequential matrices it returns itself.

 18:     Input Parameter:
 19: .      A - the Seq or MPI dense matrix

 21:     Output Parameter:
 22: .      B - the inner matrix

 24:     Level: intermediate

 26: @*/
 27: PetscErrorCode MatDenseGetLocalMatrix(Mat A,Mat *B)
 28: {
 29:   Mat_MPIDense   *mat = (Mat_MPIDense*)A->data;
 31:   PetscBool      flg;

 34:   PetscObjectTypeCompare((PetscObject)A,MATMPIDENSE,&flg);
 35:   if (flg) *B = mat->A;
 36:   else *B = A;
 37:   return(0);
 38: }

 42: PetscErrorCode MatGetRow_MPIDense(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
 43: {
 44:   Mat_MPIDense   *mat = (Mat_MPIDense*)A->data;
 46:   PetscInt       lrow,rstart = A->rmap->rstart,rend = A->rmap->rend;

 49:   if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"only local rows");
 50:   lrow = row - rstart;
 51:   MatGetRow(mat->A,lrow,nz,(const PetscInt**)idx,(const PetscScalar**)v);
 52:   return(0);
 53: }

 57: PetscErrorCode MatRestoreRow_MPIDense(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
 58: {

 62:   if (idx) {PetscFree(*idx);}
 63:   if (v) {PetscFree(*v);}
 64:   return(0);
 65: }

 69: PetscErrorCode  MatGetDiagonalBlock_MPIDense(Mat A,Mat *a)
 70: {
 71:   Mat_MPIDense   *mdn = (Mat_MPIDense*)A->data;
 73:   PetscInt       m = A->rmap->n,rstart = A->rmap->rstart;
 74:   PetscScalar    *array;
 75:   MPI_Comm       comm;
 76:   Mat            B;

 79:   if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only square matrices supported.");

 81:   PetscObjectQuery((PetscObject)A,"DiagonalBlock",(PetscObject*)&B);
 82:   if (!B) {
 83:     PetscObjectGetComm((PetscObject)(mdn->A),&comm);
 84:     MatCreate(comm,&B);
 85:     MatSetSizes(B,m,m,m,m);
 86:     MatSetType(B,((PetscObject)mdn->A)->type_name);
 87:     MatDenseGetArray(mdn->A,&array);
 88:     MatSeqDenseSetPreallocation(B,array+m*rstart);
 89:     MatDenseRestoreArray(mdn->A,&array);
 90:     MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
 91:     MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
 92:     PetscObjectCompose((PetscObject)A,"DiagonalBlock",(PetscObject)B);
 93:     *a   = B;
 94:     MatDestroy(&B);
 95:   } else *a = B;
 96:   return(0);
 97: }

101: PetscErrorCode MatSetValues_MPIDense(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv)
102: {
103:   Mat_MPIDense   *A = (Mat_MPIDense*)mat->data;
105:   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend,row;
106:   PetscBool      roworiented = A->roworiented;

109:   for (i=0; i<m; i++) {
110:     if (idxm[i] < 0) continue;
111:     if (idxm[i] >= mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
112:     if (idxm[i] >= rstart && idxm[i] < rend) {
113:       row = idxm[i] - rstart;
114:       if (roworiented) {
115:         MatSetValues(A->A,1,&row,n,idxn,v+i*n,addv);
116:       } else {
117:         for (j=0; j<n; j++) {
118:           if (idxn[j] < 0) continue;
119:           if (idxn[j] >= mat->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
120:           MatSetValues(A->A,1,&row,1,&idxn[j],v+i+j*m,addv);
121:         }
122:       }
123:     } else if (!A->donotstash) {
124:       mat->assembled = PETSC_FALSE;
125:       if (roworiented) {
126:         MatStashValuesRow_Private(&mat->stash,idxm[i],n,idxn,v+i*n,PETSC_FALSE);
127:       } else {
128:         MatStashValuesCol_Private(&mat->stash,idxm[i],n,idxn,v+i,m,PETSC_FALSE);
129:       }
130:     }
131:   }
132:   return(0);
133: }

137: PetscErrorCode MatGetValues_MPIDense(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
138: {
139:   Mat_MPIDense   *mdn = (Mat_MPIDense*)mat->data;
141:   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend,row;

144:   for (i=0; i<m; i++) {
145:     if (idxm[i] < 0) continue; /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row"); */
146:     if (idxm[i] >= mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
147:     if (idxm[i] >= rstart && idxm[i] < rend) {
148:       row = idxm[i] - rstart;
149:       for (j=0; j<n; j++) {
150:         if (idxn[j] < 0) continue; /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column"); */
151:         if (idxn[j] >= mat->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
152:         MatGetValues(mdn->A,1,&row,1,&idxn[j],v+i*n+j);
153:       }
154:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
155:   }
156:   return(0);
157: }

161: PetscErrorCode MatDenseGetArray_MPIDense(Mat A,PetscScalar *array[])
162: {
163:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

167:   MatDenseGetArray(a->A,array);
168:   return(0);
169: }

173: static PetscErrorCode MatGetSubMatrix_MPIDense(Mat A,IS isrow,IS iscol,MatReuse scall,Mat *B)
174: {
175:   Mat_MPIDense   *mat  = (Mat_MPIDense*)A->data,*newmatd;
176:   Mat_SeqDense   *lmat = (Mat_SeqDense*)mat->A->data;
178:   PetscInt       i,j,rstart,rend,nrows,ncols,Ncols,nlrows,nlcols;
179:   const PetscInt *irow,*icol;
180:   PetscScalar    *av,*bv,*v = lmat->v;
181:   Mat            newmat;
182:   IS             iscol_local;

185:   ISAllGather(iscol,&iscol_local);
186:   ISGetIndices(isrow,&irow);
187:   ISGetIndices(iscol_local,&icol);
188:   ISGetLocalSize(isrow,&nrows);
189:   ISGetLocalSize(iscol,&ncols);
190:   ISGetSize(iscol,&Ncols); /* global number of columns, size of iscol_local */

192:   /* No parallel redistribution currently supported! Should really check each index set
193:      to comfirm that it is OK.  ... Currently supports only submatrix same partitioning as
194:      original matrix! */

196:   MatGetLocalSize(A,&nlrows,&nlcols);
197:   MatGetOwnershipRange(A,&rstart,&rend);

199:   /* Check submatrix call */
200:   if (scall == MAT_REUSE_MATRIX) {
201:     /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); */
202:     /* Really need to test rows and column sizes! */
203:     newmat = *B;
204:   } else {
205:     /* Create and fill new matrix */
206:     MatCreate(PetscObjectComm((PetscObject)A),&newmat);
207:     MatSetSizes(newmat,nrows,ncols,PETSC_DECIDE,Ncols);
208:     MatSetType(newmat,((PetscObject)A)->type_name);
209:     MatMPIDenseSetPreallocation(newmat,NULL);
210:   }

212:   /* Now extract the data pointers and do the copy, column at a time */
213:   newmatd = (Mat_MPIDense*)newmat->data;
214:   bv      = ((Mat_SeqDense*)newmatd->A->data)->v;

216:   for (i=0; i<Ncols; i++) {
217:     av = v + ((Mat_SeqDense*)mat->A->data)->lda*icol[i];
218:     for (j=0; j<nrows; j++) {
219:       *bv++ = av[irow[j] - rstart];
220:     }
221:   }

223:   /* Assemble the matrices so that the correct flags are set */
224:   MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
225:   MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);

227:   /* Free work space */
228:   ISRestoreIndices(isrow,&irow);
229:   ISRestoreIndices(iscol_local,&icol);
230:   ISDestroy(&iscol_local);
231:   *B   = newmat;
232:   return(0);
233: }

237: PetscErrorCode MatDenseRestoreArray_MPIDense(Mat A,PetscScalar *array[])
238: {
239:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

243:   MatDenseRestoreArray(a->A,array);
244:   return(0);
245: }

249: PetscErrorCode MatAssemblyBegin_MPIDense(Mat mat,MatAssemblyType mode)
250: {
251:   Mat_MPIDense   *mdn = (Mat_MPIDense*)mat->data;
252:   MPI_Comm       comm;
254:   PetscInt       nstash,reallocs;
255:   InsertMode     addv;

258:   PetscObjectGetComm((PetscObject)mat,&comm);
259:   /* make sure all processors are either in INSERTMODE or ADDMODE */
260:   MPIU_Allreduce((PetscEnum*)&mat->insertmode,(PetscEnum*)&addv,1,MPIU_ENUM,MPI_BOR,comm);
261:   if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Cannot mix adds/inserts on different procs");
262:   mat->insertmode = addv; /* in case this processor had no cache */

264:   MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
265:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
266:   PetscInfo2(mdn->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
267:   return(0);
268: }

272: PetscErrorCode MatAssemblyEnd_MPIDense(Mat mat,MatAssemblyType mode)
273: {
274:   Mat_MPIDense   *mdn=(Mat_MPIDense*)mat->data;
276:   PetscInt       i,*row,*col,flg,j,rstart,ncols;
277:   PetscMPIInt    n;
278:   PetscScalar    *val;

281:   /*  wait on receives */
282:   while (1) {
283:     MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
284:     if (!flg) break;

286:     for (i=0; i<n;) {
287:       /* Now identify the consecutive vals belonging to the same row */
288:       for (j=i,rstart=row[j]; j<n; j++) {
289:         if (row[j] != rstart) break;
290:       }
291:       if (j < n) ncols = j-i;
292:       else       ncols = n-i;
293:       /* Now assemble all these values with a single function call */
294:       MatSetValues_MPIDense(mat,1,row+i,ncols,col+i,val+i,mat->insertmode);
295:       i    = j;
296:     }
297:   }
298:   MatStashScatterEnd_Private(&mat->stash);

300:   MatAssemblyBegin(mdn->A,mode);
301:   MatAssemblyEnd(mdn->A,mode);

303:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
304:     MatSetUpMultiply_MPIDense(mat);
305:   }
306:   return(0);
307: }

311: PetscErrorCode MatZeroEntries_MPIDense(Mat A)
312: {
314:   Mat_MPIDense   *l = (Mat_MPIDense*)A->data;

317:   MatZeroEntries(l->A);
318:   return(0);
319: }

321: /* the code does not do the diagonal entries correctly unless the
322:    matrix is square and the column and row owerships are identical.
323:    This is a BUG. The only way to fix it seems to be to access
324:    mdn->A and mdn->B directly and not through the MatZeroRows()
325:    routine.
326: */
329: PetscErrorCode MatZeroRows_MPIDense(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
330: {
331:   Mat_MPIDense      *l = (Mat_MPIDense*)A->data;
332:   PetscErrorCode    ierr;
333:   PetscInt          i,*owners = A->rmap->range;
334:   PetscInt          *sizes,j,idx,nsends;
335:   PetscInt          nmax,*svalues,*starts,*owner,nrecvs;
336:   PetscInt          *rvalues,tag = ((PetscObject)A)->tag,count,base,slen,*source;
337:   PetscInt          *lens,*lrows,*values;
338:   PetscMPIInt       n,imdex,rank = l->rank,size = l->size;
339:   MPI_Comm          comm;
340:   MPI_Request       *send_waits,*recv_waits;
341:   MPI_Status        recv_status,*send_status;
342:   PetscBool         found;
343:   const PetscScalar *xx;
344:   PetscScalar       *bb;

347:   PetscObjectGetComm((PetscObject)A,&comm);
348:   if (A->rmap->N != A->cmap->N) SETERRQ(comm,PETSC_ERR_SUP,"Only handles square matrices");
349:   if (A->rmap->n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only handles matrices with identical column and row ownership");
350:   /*  first count number of contributors to each processor */
351:   PetscCalloc1(2*size,&sizes);
352:   PetscMalloc1(N+1,&owner);  /* see note*/
353:   for (i=0; i<N; i++) {
354:     idx   = rows[i];
355:     found = PETSC_FALSE;
356:     for (j=0; j<size; j++) {
357:       if (idx >= owners[j] && idx < owners[j+1]) {
358:         sizes[2*j]++; sizes[2*j+1] = 1; owner[i] = j; found = PETSC_TRUE; break;
359:       }
360:     }
361:     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
362:   }
363:   nsends = 0;
364:   for (i=0; i<size; i++) nsends += sizes[2*i+1];

366:   /* inform other processors of number of messages and max length*/
367:   PetscMaxSum(comm,sizes,&nmax,&nrecvs);

369:   /* post receives:   */
370:   PetscMalloc1((nrecvs+1)*(nmax+1),&rvalues);
371:   PetscMalloc1(nrecvs+1,&recv_waits);
372:   for (i=0; i<nrecvs; i++) {
373:     MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);
374:   }

376:   /* do sends:
377:       1) starts[i] gives the starting index in svalues for stuff going to
378:          the ith processor
379:   */
380:   PetscMalloc1(N+1,&svalues);
381:   PetscMalloc1(nsends+1,&send_waits);
382:   PetscMalloc1(size+1,&starts);

384:   starts[0] = 0;
385:   for (i=1; i<size; i++) starts[i] = starts[i-1] + sizes[2*i-2];
386:   for (i=0; i<N; i++) svalues[starts[owner[i]]++] = rows[i];

388:   starts[0] = 0;
389:   for (i=1; i<size+1; i++) starts[i] = starts[i-1] + sizes[2*i-2];
390:   count = 0;
391:   for (i=0; i<size; i++) {
392:     if (sizes[2*i+1]) {
393:       MPI_Isend(svalues+starts[i],sizes[2*i],MPIU_INT,i,tag,comm,send_waits+count++);
394:     }
395:   }
396:   PetscFree(starts);

398:   base = owners[rank];

400:   /*  wait on receives */
401:   PetscMalloc2(nrecvs,&lens,nrecvs,&source);
402:   count = nrecvs;
403:   slen  = 0;
404:   while (count) {
405:     MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);
406:     /* unpack receives into our local space */
407:     MPI_Get_count(&recv_status,MPIU_INT,&n);

409:     source[imdex] = recv_status.MPI_SOURCE;
410:     lens[imdex]   = n;
411:     slen += n;
412:     count--;
413:   }
414:   PetscFree(recv_waits);

416:   /* move the data into the send scatter */
417:   PetscMalloc1(slen+1,&lrows);
418:   count = 0;
419:   for (i=0; i<nrecvs; i++) {
420:     values = rvalues + i*nmax;
421:     for (j=0; j<lens[i]; j++) {
422:       lrows[count++] = values[j] - base;
423:     }
424:   }
425:   PetscFree(rvalues);
426:   PetscFree2(lens,source);
427:   PetscFree(owner);
428:   PetscFree(sizes);

430:   /* fix right hand side if needed */
431:   if (x && b) {
432:     VecGetArrayRead(x,&xx);
433:     VecGetArray(b,&bb);
434:     for (i=0; i<slen; i++) {
435:       bb[lrows[i]] = diag*xx[lrows[i]];
436:     }
437:     VecRestoreArrayRead(x,&xx);
438:     VecRestoreArray(b,&bb);
439:   }

441:   /* actually zap the local rows */
442:   MatZeroRows(l->A,slen,lrows,0.0,0,0);
443:   if (diag != 0.0) {
444:     Mat_SeqDense *ll = (Mat_SeqDense*)l->A->data;
445:     PetscInt     m   = ll->lda, i;

447:     for (i=0; i<slen; i++) {
448:       ll->v[lrows[i] + m*(A->cmap->rstart + lrows[i])] = diag;
449:     }
450:   }
451:   PetscFree(lrows);

453:   /* wait on sends */
454:   if (nsends) {
455:     PetscMalloc1(nsends,&send_status);
456:     MPI_Waitall(nsends,send_waits,send_status);
457:     PetscFree(send_status);
458:   }
459:   PetscFree(send_waits);
460:   PetscFree(svalues);
461:   return(0);
462: }

464: PETSC_INTERN PetscErrorCode MatMult_SeqDense(Mat,Vec,Vec);
465: PETSC_INTERN PetscErrorCode MatMultAdd_SeqDense(Mat,Vec,Vec,Vec);
466: PETSC_INTERN PetscErrorCode MatMultTranspose_SeqDense(Mat,Vec,Vec);
467: PETSC_INTERN PetscErrorCode MatMultTransposeAdd_SeqDense(Mat,Vec,Vec,Vec);

471: PetscErrorCode MatMult_MPIDense(Mat mat,Vec xx,Vec yy)
472: {
473:   Mat_MPIDense   *mdn = (Mat_MPIDense*)mat->data;

477:   VecScatterBegin(mdn->Mvctx,xx,mdn->lvec,INSERT_VALUES,SCATTER_FORWARD);
478:   VecScatterEnd(mdn->Mvctx,xx,mdn->lvec,INSERT_VALUES,SCATTER_FORWARD);
479:   MatMult_SeqDense(mdn->A,mdn->lvec,yy);
480:   return(0);
481: }

485: PetscErrorCode MatMultAdd_MPIDense(Mat mat,Vec xx,Vec yy,Vec zz)
486: {
487:   Mat_MPIDense   *mdn = (Mat_MPIDense*)mat->data;

491:   VecScatterBegin(mdn->Mvctx,xx,mdn->lvec,INSERT_VALUES,SCATTER_FORWARD);
492:   VecScatterEnd(mdn->Mvctx,xx,mdn->lvec,INSERT_VALUES,SCATTER_FORWARD);
493:   MatMultAdd_SeqDense(mdn->A,mdn->lvec,yy,zz);
494:   return(0);
495: }

499: PetscErrorCode MatMultTranspose_MPIDense(Mat A,Vec xx,Vec yy)
500: {
501:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;
503:   PetscScalar    zero = 0.0;

506:   VecSet(yy,zero);
507:   MatMultTranspose_SeqDense(a->A,xx,a->lvec);
508:   VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
509:   VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
510:   return(0);
511: }

515: PetscErrorCode MatMultTransposeAdd_MPIDense(Mat A,Vec xx,Vec yy,Vec zz)
516: {
517:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

521:   VecCopy(yy,zz);
522:   MatMultTranspose_SeqDense(a->A,xx,a->lvec);
523:   VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
524:   VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
525:   return(0);
526: }

530: PetscErrorCode MatGetDiagonal_MPIDense(Mat A,Vec v)
531: {
532:   Mat_MPIDense   *a    = (Mat_MPIDense*)A->data;
533:   Mat_SeqDense   *aloc = (Mat_SeqDense*)a->A->data;
535:   PetscInt       len,i,n,m = A->rmap->n,radd;
536:   PetscScalar    *x,zero = 0.0;

539:   VecSet(v,zero);
540:   VecGetArray(v,&x);
541:   VecGetSize(v,&n);
542:   if (n != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming mat and vec");
543:   len  = PetscMin(a->A->rmap->n,a->A->cmap->n);
544:   radd = A->rmap->rstart*m;
545:   for (i=0; i<len; i++) {
546:     x[i] = aloc->v[radd + i*m + i];
547:   }
548:   VecRestoreArray(v,&x);
549:   return(0);
550: }

554: PetscErrorCode MatDestroy_MPIDense(Mat mat)
555: {
556:   Mat_MPIDense   *mdn = (Mat_MPIDense*)mat->data;

560: #if defined(PETSC_USE_LOG)
561:   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N);
562: #endif
563:   MatStashDestroy_Private(&mat->stash);
564:   MatDestroy(&mdn->A);
565:   VecDestroy(&mdn->lvec);
566:   VecScatterDestroy(&mdn->Mvctx);

568:   PetscFree(mat->data);
569:   PetscObjectChangeTypeName((PetscObject)mat,0);

571:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetArray_C",NULL);
572:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreArray_C",NULL);
573: #if defined(PETSC_HAVE_ELEMENTAL)
574:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpidense_elemental_C",NULL);
575: #endif
576:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIDenseSetPreallocation_C",NULL);
577:   PetscObjectComposeFunction((PetscObject)mat,"MatMatMult_mpiaij_mpidense_C",NULL);
578:   PetscObjectComposeFunction((PetscObject)mat,"MatMatMultSymbolic_mpiaij_mpidense_C",NULL);
579:   PetscObjectComposeFunction((PetscObject)mat,"MatMatMultNumeric_mpiaij_mpidense_C",NULL);
580:   PetscObjectComposeFunction((PetscObject)mat,"MatTransposeMatMult_mpiaij_mpidense_C",NULL);
581:   PetscObjectComposeFunction((PetscObject)mat,"MatTransposeMatMultSymbolic_mpiaij_mpidense_C",NULL);
582:   PetscObjectComposeFunction((PetscObject)mat,"MatTransposeMatMultNumeric_mpiaij_mpidense_C",NULL);
583:   return(0);
584: }

588: static PetscErrorCode MatView_MPIDense_Binary(Mat mat,PetscViewer viewer)
589: {
590:   Mat_MPIDense      *mdn = (Mat_MPIDense*)mat->data;
591:   PetscErrorCode    ierr;
592:   PetscViewerFormat format;
593:   int               fd;
594:   PetscInt          header[4],mmax,N = mat->cmap->N,i,j,m,k;
595:   PetscMPIInt       rank,tag  = ((PetscObject)viewer)->tag,size;
596:   PetscScalar       *work,*v,*vv;
597:   Mat_SeqDense      *a = (Mat_SeqDense*)mdn->A->data;

600:   if (mdn->size == 1) {
601:     MatView(mdn->A,viewer);
602:   } else {
603:     PetscViewerBinaryGetDescriptor(viewer,&fd);
604:     MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
605:     MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);

607:     PetscViewerGetFormat(viewer,&format);
608:     if (format == PETSC_VIEWER_NATIVE) {

610:       if (!rank) {
611:         /* store the matrix as a dense matrix */
612:         header[0] = MAT_FILE_CLASSID;
613:         header[1] = mat->rmap->N;
614:         header[2] = N;
615:         header[3] = MATRIX_BINARY_FORMAT_DENSE;
616:         PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);

618:         /* get largest work array needed for transposing array */
619:         mmax = mat->rmap->n;
620:         for (i=1; i<size; i++) {
621:           mmax = PetscMax(mmax,mat->rmap->range[i+1] - mat->rmap->range[i]);
622:         }
623:         PetscMalloc1(mmax*N,&work);

625:         /* write out local array, by rows */
626:         m = mat->rmap->n;
627:         v = a->v;
628:         for (j=0; j<N; j++) {
629:           for (i=0; i<m; i++) {
630:             work[j + i*N] = *v++;
631:           }
632:         }
633:         PetscBinaryWrite(fd,work,m*N,PETSC_SCALAR,PETSC_FALSE);
634:         /* get largest work array to receive messages from other processes, excludes process zero */
635:         mmax = 0;
636:         for (i=1; i<size; i++) {
637:           mmax = PetscMax(mmax,mat->rmap->range[i+1] - mat->rmap->range[i]);
638:         }
639:         PetscMalloc1(mmax*N,&vv);
640:         for (k = 1; k < size; k++) {
641:           v    = vv;
642:           m    = mat->rmap->range[k+1] - mat->rmap->range[k];
643:           MPIULong_Recv(v,m*N,MPIU_SCALAR,k,tag,PetscObjectComm((PetscObject)mat));

645:           for (j = 0; j < N; j++) {
646:             for (i = 0; i < m; i++) {
647:               work[j + i*N] = *v++;
648:             }
649:           }
650:           PetscBinaryWrite(fd,work,m*N,PETSC_SCALAR,PETSC_FALSE);
651:         }
652:         PetscFree(work);
653:         PetscFree(vv);
654:       } else {
655:         MPIULong_Send(a->v,mat->rmap->n*mat->cmap->N,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));
656:       }
657:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"To store a parallel dense matrix you must first call PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE)");
658:   }
659:   return(0);
660: }

662: extern PetscErrorCode MatView_SeqDense(Mat,PetscViewer);
663:  #include <petscdraw.h>
666: static PetscErrorCode MatView_MPIDense_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
667: {
668:   Mat_MPIDense      *mdn = (Mat_MPIDense*)mat->data;
669:   PetscErrorCode    ierr;
670:   PetscMPIInt       rank = mdn->rank;
671:   PetscViewerType   vtype;
672:   PetscBool         iascii,isdraw;
673:   PetscViewer       sviewer;
674:   PetscViewerFormat format;

677:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
678:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
679:   if (iascii) {
680:     PetscViewerGetType(viewer,&vtype);
681:     PetscViewerGetFormat(viewer,&format);
682:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
683:       MatInfo info;
684:       MatGetInfo(mat,MAT_LOCAL,&info);
685:       PetscViewerASCIIPushSynchronized(viewer);
686:       PetscViewerASCIISynchronizedPrintf(viewer,"  [%d] local rows %D nz %D nz alloced %D mem %D \n",rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);
687:       PetscViewerFlush(viewer);
688:       PetscViewerASCIIPopSynchronized(viewer);
689:       VecScatterView(mdn->Mvctx,viewer);
690:       return(0);
691:     } else if (format == PETSC_VIEWER_ASCII_INFO) {
692:       return(0);
693:     }
694:   } else if (isdraw) {
695:     PetscDraw draw;
696:     PetscBool isnull;

698:     PetscViewerDrawGetDraw(viewer,0,&draw);
699:     PetscDrawIsNull(draw,&isnull);
700:     if (isnull) return(0);
701:   }

703:   {
704:     /* assemble the entire matrix onto first processor. */
705:     Mat         A;
706:     PetscInt    M = mat->rmap->N,N = mat->cmap->N,m,row,i,nz;
707:     PetscInt    *cols;
708:     PetscScalar *vals;

710:     MatCreate(PetscObjectComm((PetscObject)mat),&A);
711:     if (!rank) {
712:       MatSetSizes(A,M,N,M,N);
713:     } else {
714:       MatSetSizes(A,0,0,M,N);
715:     }
716:     /* Since this is a temporary matrix, MATMPIDENSE instead of ((PetscObject)A)->type_name here is probably acceptable. */
717:     MatSetType(A,MATMPIDENSE);
718:     MatMPIDenseSetPreallocation(A,NULL);
719:     PetscLogObjectParent((PetscObject)mat,(PetscObject)A);

721:     /* Copy the matrix ... This isn't the most efficient means,
722:        but it's quick for now */
723:     A->insertmode = INSERT_VALUES;

725:     row = mat->rmap->rstart;
726:     m   = mdn->A->rmap->n;
727:     for (i=0; i<m; i++) {
728:       MatGetRow_MPIDense(mat,row,&nz,&cols,&vals);
729:       MatSetValues_MPIDense(A,1,&row,nz,cols,vals,INSERT_VALUES);
730:       MatRestoreRow_MPIDense(mat,row,&nz,&cols,&vals);
731:       row++;
732:     }

734:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
735:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
736:     PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
737:     if (!rank) {
738:         MatView_SeqDense(((Mat_MPIDense*)(A->data))->A,sviewer);
739:     }
740:     PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
741:     PetscViewerFlush(viewer);
742:     MatDestroy(&A);
743:   }
744:   return(0);
745: }

749: PetscErrorCode MatView_MPIDense(Mat mat,PetscViewer viewer)
750: {
752:   PetscBool      iascii,isbinary,isdraw,issocket;

755:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
756:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
757:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
758:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);

760:   if (iascii || issocket || isdraw) {
761:     MatView_MPIDense_ASCIIorDraworSocket(mat,viewer);
762:   } else if (isbinary) {
763:     MatView_MPIDense_Binary(mat,viewer);
764:   }
765:   return(0);
766: }

770: PetscErrorCode MatGetInfo_MPIDense(Mat A,MatInfoType flag,MatInfo *info)
771: {
772:   Mat_MPIDense   *mat = (Mat_MPIDense*)A->data;
773:   Mat            mdn  = mat->A;
775:   PetscReal      isend[5],irecv[5];

778:   info->block_size = 1.0;

780:   MatGetInfo(mdn,MAT_LOCAL,info);

782:   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
783:   isend[3] = info->memory;  isend[4] = info->mallocs;
784:   if (flag == MAT_LOCAL) {
785:     info->nz_used      = isend[0];
786:     info->nz_allocated = isend[1];
787:     info->nz_unneeded  = isend[2];
788:     info->memory       = isend[3];
789:     info->mallocs      = isend[4];
790:   } else if (flag == MAT_GLOBAL_MAX) {
791:     MPIU_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));

793:     info->nz_used      = irecv[0];
794:     info->nz_allocated = irecv[1];
795:     info->nz_unneeded  = irecv[2];
796:     info->memory       = irecv[3];
797:     info->mallocs      = irecv[4];
798:   } else if (flag == MAT_GLOBAL_SUM) {
799:     MPIU_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));

801:     info->nz_used      = irecv[0];
802:     info->nz_allocated = irecv[1];
803:     info->nz_unneeded  = irecv[2];
804:     info->memory       = irecv[3];
805:     info->mallocs      = irecv[4];
806:   }
807:   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
808:   info->fill_ratio_needed = 0;
809:   info->factor_mallocs    = 0;
810:   return(0);
811: }

815: PetscErrorCode MatSetOption_MPIDense(Mat A,MatOption op,PetscBool flg)
816: {
817:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

821:   switch (op) {
822:   case MAT_NEW_NONZERO_LOCATIONS:
823:   case MAT_NEW_NONZERO_LOCATION_ERR:
824:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
825:     MatCheckPreallocated(A,1);
826:     MatSetOption(a->A,op,flg);
827:     break;
828:   case MAT_ROW_ORIENTED:
829:     MatCheckPreallocated(A,1);
830:     a->roworiented = flg;
831:     MatSetOption(a->A,op,flg);
832:     break;
833:   case MAT_NEW_DIAGONALS:
834:   case MAT_KEEP_NONZERO_PATTERN:
835:   case MAT_USE_HASH_TABLE:
836:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
837:     break;
838:   case MAT_IGNORE_OFF_PROC_ENTRIES:
839:     a->donotstash = flg;
840:     break;
841:   case MAT_SYMMETRIC:
842:   case MAT_STRUCTURALLY_SYMMETRIC:
843:   case MAT_HERMITIAN:
844:   case MAT_SYMMETRY_ETERNAL:
845:   case MAT_IGNORE_LOWER_TRIANGULAR:
846:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
847:     break;
848:   default:
849:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %s",MatOptions[op]);
850:   }
851:   return(0);
852: }


857: PetscErrorCode MatDiagonalScale_MPIDense(Mat A,Vec ll,Vec rr)
858: {
859:   Mat_MPIDense   *mdn = (Mat_MPIDense*)A->data;
860:   Mat_SeqDense   *mat = (Mat_SeqDense*)mdn->A->data;
861:   PetscScalar    *l,*r,x,*v;
863:   PetscInt       i,j,s2a,s3a,s2,s3,m=mdn->A->rmap->n,n=mdn->A->cmap->n;

866:   MatGetLocalSize(A,&s2,&s3);
867:   if (ll) {
868:     VecGetLocalSize(ll,&s2a);
869:     if (s2a != s2) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector non-conforming local size, %d != %d.", s2a, s2);
870:     VecGetArray(ll,&l);
871:     for (i=0; i<m; i++) {
872:       x = l[i];
873:       v = mat->v + i;
874:       for (j=0; j<n; j++) { (*v) *= x; v+= m;}
875:     }
876:     VecRestoreArray(ll,&l);
877:     PetscLogFlops(n*m);
878:   }
879:   if (rr) {
880:     VecGetLocalSize(rr,&s3a);
881:     if (s3a != s3) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vec non-conforming local size, %d != %d.", s3a, s3);
882:     VecScatterBegin(mdn->Mvctx,rr,mdn->lvec,INSERT_VALUES,SCATTER_FORWARD);
883:     VecScatterEnd(mdn->Mvctx,rr,mdn->lvec,INSERT_VALUES,SCATTER_FORWARD);
884:     VecGetArray(mdn->lvec,&r);
885:     for (i=0; i<n; i++) {
886:       x = r[i];
887:       v = mat->v + i*m;
888:       for (j=0; j<m; j++) (*v++) *= x;
889:     }
890:     VecRestoreArray(mdn->lvec,&r);
891:     PetscLogFlops(n*m);
892:   }
893:   return(0);
894: }

898: PetscErrorCode MatNorm_MPIDense(Mat A,NormType type,PetscReal *nrm)
899: {
900:   Mat_MPIDense   *mdn = (Mat_MPIDense*)A->data;
901:   Mat_SeqDense   *mat = (Mat_SeqDense*)mdn->A->data;
903:   PetscInt       i,j;
904:   PetscReal      sum = 0.0;
905:   PetscScalar    *v  = mat->v;

908:   if (mdn->size == 1) {
909:      MatNorm(mdn->A,type,nrm);
910:   } else {
911:     if (type == NORM_FROBENIUS) {
912:       for (i=0; i<mdn->A->cmap->n*mdn->A->rmap->n; i++) {
913:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
914:       }
915:       MPIU_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));
916:       *nrm = PetscSqrtReal(*nrm);
917:       PetscLogFlops(2.0*mdn->A->cmap->n*mdn->A->rmap->n);
918:     } else if (type == NORM_1) {
919:       PetscReal *tmp,*tmp2;
920:       PetscMalloc2(A->cmap->N,&tmp,A->cmap->N,&tmp2);
921:       PetscMemzero(tmp,A->cmap->N*sizeof(PetscReal));
922:       PetscMemzero(tmp2,A->cmap->N*sizeof(PetscReal));
923:       *nrm = 0.0;
924:       v    = mat->v;
925:       for (j=0; j<mdn->A->cmap->n; j++) {
926:         for (i=0; i<mdn->A->rmap->n; i++) {
927:           tmp[j] += PetscAbsScalar(*v);  v++;
928:         }
929:       }
930:       MPIU_Allreduce(tmp,tmp2,A->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));
931:       for (j=0; j<A->cmap->N; j++) {
932:         if (tmp2[j] > *nrm) *nrm = tmp2[j];
933:       }
934:       PetscFree2(tmp,tmp2);
935:       PetscLogFlops(A->cmap->n*A->rmap->n);
936:     } else if (type == NORM_INFINITY) { /* max row norm */
937:       PetscReal ntemp;
938:       MatNorm(mdn->A,type,&ntemp);
939:       MPIU_Allreduce(&ntemp,nrm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));
940:     } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"No support for two norm");
941:   }
942:   return(0);
943: }

947: PetscErrorCode MatTranspose_MPIDense(Mat A,MatReuse reuse,Mat *matout)
948: {
949:   Mat_MPIDense   *a    = (Mat_MPIDense*)A->data;
950:   Mat_SeqDense   *Aloc = (Mat_SeqDense*)a->A->data;
951:   Mat            B;
952:   PetscInt       M = A->rmap->N,N = A->cmap->N,m,n,*rwork,rstart = A->rmap->rstart;
954:   PetscInt       j,i;
955:   PetscScalar    *v;

958:   if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Supports square matrix only in-place");
959:   if (reuse == MAT_INITIAL_MATRIX || A == *matout) {
960:     MatCreate(PetscObjectComm((PetscObject)A),&B);
961:     MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);
962:     MatSetType(B,((PetscObject)A)->type_name);
963:     MatMPIDenseSetPreallocation(B,NULL);
964:   } else {
965:     B = *matout;
966:   }

968:   m    = a->A->rmap->n; n = a->A->cmap->n; v = Aloc->v;
969:   PetscMalloc1(m,&rwork);
970:   for (i=0; i<m; i++) rwork[i] = rstart + i;
971:   for (j=0; j<n; j++) {
972:     MatSetValues(B,1,&j,m,rwork,v,INSERT_VALUES);
973:     v   += m;
974:   }
975:   PetscFree(rwork);
976:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
977:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
978:   if (reuse == MAT_INITIAL_MATRIX || *matout != A) {
979:     *matout = B;
980:   } else {
981:     MatHeaderMerge(A,&B);
982:   }
983:   return(0);
984: }


987: static PetscErrorCode MatDuplicate_MPIDense(Mat,MatDuplicateOption,Mat*);
988: extern PetscErrorCode MatScale_MPIDense(Mat,PetscScalar);

992: PetscErrorCode MatSetUp_MPIDense(Mat A)
993: {

997:    MatMPIDenseSetPreallocation(A,0);
998:   return(0);
999: }

1003: PetscErrorCode MatAXPY_MPIDense(Mat Y,PetscScalar alpha,Mat X,MatStructure str)
1004: {
1006:   Mat_MPIDense   *A = (Mat_MPIDense*)Y->data, *B = (Mat_MPIDense*)X->data;

1009:   MatAXPY(A->A,alpha,B->A,str);
1010:   PetscObjectStateIncrease((PetscObject)Y);
1011:   return(0);
1012: }

1016: PetscErrorCode  MatConjugate_MPIDense(Mat mat)
1017: {
1018:   Mat_MPIDense   *a = (Mat_MPIDense*)mat->data;

1022:   MatConjugate(a->A);
1023:   return(0);
1024: }

1028: PetscErrorCode MatRealPart_MPIDense(Mat A)
1029: {
1030:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

1034:   MatRealPart(a->A);
1035:   return(0);
1036: }

1040: PetscErrorCode MatImaginaryPart_MPIDense(Mat A)
1041: {
1042:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

1046:   MatImaginaryPart(a->A);
1047:   return(0);
1048: }

1050: extern PetscErrorCode MatGetColumnNorms_SeqDense(Mat,NormType,PetscReal*);
1053: PetscErrorCode MatGetColumnNorms_MPIDense(Mat A,NormType type,PetscReal *norms)
1054: {
1056:   PetscInt       i,n;
1057:   Mat_MPIDense   *a = (Mat_MPIDense*) A->data;
1058:   PetscReal      *work;

1061:   MatGetSize(A,NULL,&n);
1062:   PetscMalloc1(n,&work);
1063:   MatGetColumnNorms_SeqDense(a->A,type,work);
1064:   if (type == NORM_2) {
1065:     for (i=0; i<n; i++) work[i] *= work[i];
1066:   }
1067:   if (type == NORM_INFINITY) {
1068:     MPIU_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,A->hdr.comm);
1069:   } else {
1070:     MPIU_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,A->hdr.comm);
1071:   }
1072:   PetscFree(work);
1073:   if (type == NORM_2) {
1074:     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
1075:   }
1076:   return(0);
1077: }

1081: static PetscErrorCode  MatSetRandom_MPIDense(Mat x,PetscRandom rctx)
1082: {
1083:   Mat_MPIDense   *d = (Mat_MPIDense*)x->data;
1085:   PetscScalar    *a;
1086:   PetscInt       m,n,i;

1089:   MatGetSize(d->A,&m,&n);
1090:   MatDenseGetArray(d->A,&a);
1091:   for (i=0; i<m*n; i++) {
1092:     PetscRandomGetValue(rctx,a+i);
1093:   }
1094:   MatDenseRestoreArray(d->A,&a);
1095:   return(0);
1096: }

1098: extern PetscErrorCode MatMatMultNumeric_MPIDense(Mat A,Mat,Mat);

1102: static PetscErrorCode MatMissingDiagonal_MPIDense(Mat A,PetscBool  *missing,PetscInt *d)
1103: {
1105:   *missing = PETSC_FALSE;
1106:   return(0);
1107: }

1109: /* -------------------------------------------------------------------*/
1110: static struct _MatOps MatOps_Values = { MatSetValues_MPIDense,
1111:                                         MatGetRow_MPIDense,
1112:                                         MatRestoreRow_MPIDense,
1113:                                         MatMult_MPIDense,
1114:                                 /*  4*/ MatMultAdd_MPIDense,
1115:                                         MatMultTranspose_MPIDense,
1116:                                         MatMultTransposeAdd_MPIDense,
1117:                                         0,
1118:                                         0,
1119:                                         0,
1120:                                 /* 10*/ 0,
1121:                                         0,
1122:                                         0,
1123:                                         0,
1124:                                         MatTranspose_MPIDense,
1125:                                 /* 15*/ MatGetInfo_MPIDense,
1126:                                         MatEqual_MPIDense,
1127:                                         MatGetDiagonal_MPIDense,
1128:                                         MatDiagonalScale_MPIDense,
1129:                                         MatNorm_MPIDense,
1130:                                 /* 20*/ MatAssemblyBegin_MPIDense,
1131:                                         MatAssemblyEnd_MPIDense,
1132:                                         MatSetOption_MPIDense,
1133:                                         MatZeroEntries_MPIDense,
1134:                                 /* 24*/ MatZeroRows_MPIDense,
1135:                                         0,
1136:                                         0,
1137:                                         0,
1138:                                         0,
1139:                                 /* 29*/ MatSetUp_MPIDense,
1140:                                         0,
1141:                                         0,
1142:                                         MatGetDiagonalBlock_MPIDense,
1143:                                         0,
1144:                                 /* 34*/ MatDuplicate_MPIDense,
1145:                                         0,
1146:                                         0,
1147:                                         0,
1148:                                         0,
1149:                                 /* 39*/ MatAXPY_MPIDense,
1150:                                         MatGetSubMatrices_MPIDense,
1151:                                         0,
1152:                                         MatGetValues_MPIDense,
1153:                                         0,
1154:                                 /* 44*/ 0,
1155:                                         MatScale_MPIDense,
1156:                                         MatShift_Basic,
1157:                                         0,
1158:                                         0,
1159:                                 /* 49*/ MatSetRandom_MPIDense,
1160:                                         0,
1161:                                         0,
1162:                                         0,
1163:                                         0,
1164:                                 /* 54*/ 0,
1165:                                         0,
1166:                                         0,
1167:                                         0,
1168:                                         0,
1169:                                 /* 59*/ MatGetSubMatrix_MPIDense,
1170:                                         MatDestroy_MPIDense,
1171:                                         MatView_MPIDense,
1172:                                         0,
1173:                                         0,
1174:                                 /* 64*/ 0,
1175:                                         0,
1176:                                         0,
1177:                                         0,
1178:                                         0,
1179:                                 /* 69*/ 0,
1180:                                         0,
1181:                                         0,
1182:                                         0,
1183:                                         0,
1184:                                 /* 74*/ 0,
1185:                                         0,
1186:                                         0,
1187:                                         0,
1188:                                         0,
1189:                                 /* 79*/ 0,
1190:                                         0,
1191:                                         0,
1192:                                         0,
1193:                                 /* 83*/ MatLoad_MPIDense,
1194:                                         0,
1195:                                         0,
1196:                                         0,
1197:                                         0,
1198:                                         0,
1199: #if defined(PETSC_HAVE_ELEMENTAL)
1200:                                 /* 89*/ MatMatMult_MPIDense_MPIDense,
1201:                                         MatMatMultSymbolic_MPIDense_MPIDense,
1202: #else
1203:                                 /* 89*/ 0,
1204:                                         0,
1205: #endif
1206:                                         MatMatMultNumeric_MPIDense,
1207:                                         0,
1208:                                         0,
1209:                                 /* 94*/ 0,
1210:                                         0,
1211:                                         0,
1212:                                         0,
1213:                                         0,
1214:                                 /* 99*/ 0,
1215:                                         0,
1216:                                         0,
1217:                                         MatConjugate_MPIDense,
1218:                                         0,
1219:                                 /*104*/ 0,
1220:                                         MatRealPart_MPIDense,
1221:                                         MatImaginaryPart_MPIDense,
1222:                                         0,
1223:                                         0,
1224:                                 /*109*/ 0,
1225:                                         0,
1226:                                         0,
1227:                                         0,
1228:                                         MatMissingDiagonal_MPIDense,
1229:                                 /*114*/ 0,
1230:                                         0,
1231:                                         0,
1232:                                         0,
1233:                                         0,
1234:                                 /*119*/ 0,
1235:                                         0,
1236:                                         0,
1237:                                         0,
1238:                                         0,
1239:                                 /*124*/ 0,
1240:                                         MatGetColumnNorms_MPIDense,
1241:                                         0,
1242:                                         0,
1243:                                         0,
1244:                                 /*129*/ 0,
1245:                                         MatTransposeMatMult_MPIDense_MPIDense,
1246:                                         MatTransposeMatMultSymbolic_MPIDense_MPIDense,
1247:                                         MatTransposeMatMultNumeric_MPIDense_MPIDense,
1248:                                         0,
1249:                                 /*134*/ 0,
1250:                                         0,
1251:                                         0,
1252:                                         0,
1253:                                         0,
1254:                                 /*139*/ 0,
1255:                                         0,
1256:                                         0
1257: };

1261: PetscErrorCode  MatMPIDenseSetPreallocation_MPIDense(Mat mat,PetscScalar *data)
1262: {
1263:   Mat_MPIDense   *a;

1267:   mat->preallocated = PETSC_TRUE;
1268:   /* Note:  For now, when data is specified above, this assumes the user correctly
1269:    allocates the local dense storage space.  We should add error checking. */

1271:   a       = (Mat_MPIDense*)mat->data;
1272:   PetscLayoutSetUp(mat->rmap);
1273:   PetscLayoutSetUp(mat->cmap);
1274:   a->nvec = mat->cmap->n;

1276:   MatCreate(PETSC_COMM_SELF,&a->A);
1277:   MatSetSizes(a->A,mat->rmap->n,mat->cmap->N,mat->rmap->n,mat->cmap->N);
1278:   MatSetType(a->A,MATSEQDENSE);
1279:   MatSeqDenseSetPreallocation(a->A,data);
1280:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
1281:   return(0);
1282: }

1284: #if defined(PETSC_HAVE_ELEMENTAL)
1287: PETSC_INTERN PetscErrorCode MatConvert_MPIDense_Elemental(Mat A, MatType newtype,MatReuse reuse,Mat *newmat)
1288: {
1289:   Mat            mat_elemental;
1291:   PetscScalar    *v;
1292:   PetscInt       m=A->rmap->n,N=A->cmap->N,rstart=A->rmap->rstart,i,*rows,*cols;
1293: 
1295:   if (reuse == MAT_REUSE_MATRIX) {
1296:     mat_elemental = *newmat;
1297:     MatZeroEntries(*newmat);
1298:   } else {
1299:     MatCreate(PetscObjectComm((PetscObject)A), &mat_elemental);
1300:     MatSetSizes(mat_elemental,PETSC_DECIDE,PETSC_DECIDE,A->rmap->N,A->cmap->N);
1301:     MatSetType(mat_elemental,MATELEMENTAL);
1302:     MatSetUp(mat_elemental);
1303:     MatSetOption(mat_elemental,MAT_ROW_ORIENTED,PETSC_FALSE);
1304:   }

1306:   PetscMalloc2(m,&rows,N,&cols);
1307:   for (i=0; i<N; i++) cols[i] = i;
1308:   for (i=0; i<m; i++) rows[i] = rstart + i;
1309: 
1310:   /* PETSc-Elemental interaface uses axpy for setting off-processor entries, only ADD_VALUES is allowed */
1311:   MatDenseGetArray(A,&v);
1312:   MatSetValues(mat_elemental,m,rows,N,cols,v,ADD_VALUES);
1313:   MatAssemblyBegin(mat_elemental, MAT_FINAL_ASSEMBLY);
1314:   MatAssemblyEnd(mat_elemental, MAT_FINAL_ASSEMBLY);
1315:   MatDenseRestoreArray(A,&v);
1316:   PetscFree2(rows,cols);

1318:   if (reuse == MAT_INPLACE_MATRIX) {
1319:     MatHeaderReplace(A,&mat_elemental);
1320:   } else {
1321:     *newmat = mat_elemental;
1322:   }
1323:   return(0);
1324: }
1325: #endif

1329: PETSC_EXTERN PetscErrorCode MatCreate_MPIDense(Mat mat)
1330: {
1331:   Mat_MPIDense   *a;

1335:   PetscNewLog(mat,&a);
1336:   mat->data = (void*)a;
1337:   PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));

1339:   mat->insertmode = NOT_SET_VALUES;
1340:   MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&a->rank);
1341:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&a->size);

1343:   /* build cache for off array entries formed */
1344:   a->donotstash = PETSC_FALSE;

1346:   MatStashCreate_Private(PetscObjectComm((PetscObject)mat),1,&mat->stash);

1348:   /* stuff used for matrix vector multiply */
1349:   a->lvec        = 0;
1350:   a->Mvctx       = 0;
1351:   a->roworiented = PETSC_TRUE;

1353:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetArray_C",MatDenseGetArray_MPIDense);
1354:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreArray_C",MatDenseRestoreArray_MPIDense);
1355: #if defined(PETSC_HAVE_ELEMENTAL)
1356:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpidense_elemental_C",MatConvert_MPIDense_Elemental);
1357: #endif
1358:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIDenseSetPreallocation_C",MatMPIDenseSetPreallocation_MPIDense);
1359:   PetscObjectComposeFunction((PetscObject)mat,"MatMatMult_mpiaij_mpidense_C",MatMatMult_MPIAIJ_MPIDense);
1360:   PetscObjectComposeFunction((PetscObject)mat,"MatMatMultSymbolic_mpiaij_mpidense_C",MatMatMultSymbolic_MPIAIJ_MPIDense);
1361:   PetscObjectComposeFunction((PetscObject)mat,"MatMatMultNumeric_mpiaij_mpidense_C",MatMatMultNumeric_MPIAIJ_MPIDense);

1363:   PetscObjectComposeFunction((PetscObject)mat,"MatTransposeMatMult_mpiaij_mpidense_C",MatTransposeMatMult_MPIAIJ_MPIDense);
1364:   PetscObjectComposeFunction((PetscObject)mat,"MatTransposeMatMultSymbolic_mpiaij_mpidense_C",MatTransposeMatMultSymbolic_MPIAIJ_MPIDense);
1365:   PetscObjectComposeFunction((PetscObject)mat,"MatTransposeMatMultNumeric_mpiaij_mpidense_C",MatTransposeMatMultNumeric_MPIAIJ_MPIDense);
1366:   PetscObjectChangeTypeName((PetscObject)mat,MATMPIDENSE);
1367:   return(0);
1368: }

1370: /*MC
1371:    MATDENSE - MATDENSE = "dense" - A matrix type to be used for dense matrices.

1373:    This matrix type is identical to MATSEQDENSE when constructed with a single process communicator,
1374:    and MATMPIDENSE otherwise.

1376:    Options Database Keys:
1377: . -mat_type dense - sets the matrix type to "dense" during a call to MatSetFromOptions()

1379:   Level: beginner


1382: .seealso: MatCreateMPIDense,MATSEQDENSE,MATMPIDENSE
1383: M*/

1387: /*@C
1388:    MatMPIDenseSetPreallocation - Sets the array used to store the matrix entries

1390:    Not collective

1392:    Input Parameters:
1393: .  B - the matrix
1394: -  data - optional location of matrix data.  Set data=NULL for PETSc
1395:    to control all matrix memory allocation.

1397:    Notes:
1398:    The dense format is fully compatible with standard Fortran 77
1399:    storage by columns.

1401:    The data input variable is intended primarily for Fortran programmers
1402:    who wish to allocate their own matrix memory space.  Most users should
1403:    set data=NULL.

1405:    Level: intermediate

1407: .keywords: matrix,dense, parallel

1409: .seealso: MatCreate(), MatCreateSeqDense(), MatSetValues()
1410: @*/
1411: PetscErrorCode  MatMPIDenseSetPreallocation(Mat B,PetscScalar *data)
1412: {

1416:   PetscTryMethod(B,"MatMPIDenseSetPreallocation_C",(Mat,PetscScalar*),(B,data));
1417:   return(0);
1418: }

1422: /*@C
1423:    MatCreateDense - Creates a parallel matrix in dense format.

1425:    Collective on MPI_Comm

1427:    Input Parameters:
1428: +  comm - MPI communicator
1429: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
1430: .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
1431: .  M - number of global rows (or PETSC_DECIDE to have calculated if m is given)
1432: .  N - number of global columns (or PETSC_DECIDE to have calculated if n is given)
1433: -  data - optional location of matrix data.  Set data=NULL (PETSC_NULL_SCALAR for Fortran users) for PETSc
1434:    to control all matrix memory allocation.

1436:    Output Parameter:
1437: .  A - the matrix

1439:    Notes:
1440:    The dense format is fully compatible with standard Fortran 77
1441:    storage by columns.

1443:    The data input variable is intended primarily for Fortran programmers
1444:    who wish to allocate their own matrix memory space.  Most users should
1445:    set data=NULL (PETSC_NULL_SCALAR for Fortran users).

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

1450:    Level: intermediate

1452: .keywords: matrix,dense, parallel

1454: .seealso: MatCreate(), MatCreateSeqDense(), MatSetValues()
1455: @*/
1456: PetscErrorCode  MatCreateDense(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscScalar *data,Mat *A)
1457: {
1459:   PetscMPIInt    size;

1462:   MatCreate(comm,A);
1463:   MatSetSizes(*A,m,n,M,N);
1464:   MPI_Comm_size(comm,&size);
1465:   if (size > 1) {
1466:     MatSetType(*A,MATMPIDENSE);
1467:     MatMPIDenseSetPreallocation(*A,data);
1468:     if (data) {  /* user provided data array, so no need to assemble */
1469:       MatSetUpMultiply_MPIDense(*A);
1470:       (*A)->assembled = PETSC_TRUE;
1471:     }
1472:   } else {
1473:     MatSetType(*A,MATSEQDENSE);
1474:     MatSeqDenseSetPreallocation(*A,data);
1475:   }
1476:   return(0);
1477: }

1481: static PetscErrorCode MatDuplicate_MPIDense(Mat A,MatDuplicateOption cpvalues,Mat *newmat)
1482: {
1483:   Mat            mat;
1484:   Mat_MPIDense   *a,*oldmat = (Mat_MPIDense*)A->data;

1488:   *newmat = 0;
1489:   MatCreate(PetscObjectComm((PetscObject)A),&mat);
1490:   MatSetSizes(mat,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
1491:   MatSetType(mat,((PetscObject)A)->type_name);
1492:   a       = (Mat_MPIDense*)mat->data;
1493:   PetscMemcpy(mat->ops,A->ops,sizeof(struct _MatOps));

1495:   mat->factortype   = A->factortype;
1496:   mat->assembled    = PETSC_TRUE;
1497:   mat->preallocated = PETSC_TRUE;

1499:   a->size         = oldmat->size;
1500:   a->rank         = oldmat->rank;
1501:   mat->insertmode = NOT_SET_VALUES;
1502:   a->nvec         = oldmat->nvec;
1503:   a->donotstash   = oldmat->donotstash;

1505:   PetscLayoutReference(A->rmap,&mat->rmap);
1506:   PetscLayoutReference(A->cmap,&mat->cmap);

1508:   MatSetUpMultiply_MPIDense(mat);
1509:   MatDuplicate(oldmat->A,cpvalues,&a->A);
1510:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);

1512:   *newmat = mat;
1513:   return(0);
1514: }

1518: PetscErrorCode MatLoad_MPIDense_DenseInFile(MPI_Comm comm,PetscInt fd,PetscInt M,PetscInt N,Mat newmat)
1519: {
1521:   PetscMPIInt    rank,size;
1522:   const PetscInt *rowners;
1523:   PetscInt       i,m,n,nz,j,mMax;
1524:   PetscScalar    *array,*vals,*vals_ptr;
1525:   Mat_MPIDense   *a = (Mat_MPIDense*)newmat->data;

1528:   MPI_Comm_rank(comm,&rank);
1529:   MPI_Comm_size(comm,&size);

1531:   /* determine ownership of rows and columns */
1532:   m = (newmat->rmap->n < 0) ? PETSC_DECIDE : newmat->rmap->n;
1533:   n = (newmat->cmap->n < 0) ? PETSC_DECIDE : newmat->cmap->n;

1535:   MatSetSizes(newmat,m,n,M,N);
1536:   if (!a->A || !((Mat_SeqDense*)(a->A->data))->user_alloc) {
1537:     MatMPIDenseSetPreallocation(newmat,NULL);
1538:   }
1539:   MatDenseGetArray(newmat,&array);
1540:   MatGetLocalSize(newmat,&m,NULL);
1541:   MatGetOwnershipRanges(newmat,&rowners);
1542:   MPI_Reduce(&m,&mMax,1,MPIU_INT,MPI_MAX,0,comm);
1543:   if (!rank) {
1544:     PetscMalloc1(mMax*N,&vals);

1546:     /* read in my part of the matrix numerical values  */
1547:     PetscBinaryRead(fd,vals,m*N,PETSC_SCALAR);

1549:     /* insert into matrix-by row (this is why cannot directly read into array */
1550:     vals_ptr = vals;
1551:     for (i=0; i<m; i++) {
1552:       for (j=0; j<N; j++) {
1553:         array[i + j*m] = *vals_ptr++;
1554:       }
1555:     }

1557:     /* read in other processors and ship out */
1558:     for (i=1; i<size; i++) {
1559:       nz   = (rowners[i+1] - rowners[i])*N;
1560:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
1561:       MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)(newmat))->tag,comm);
1562:     }
1563:   } else {
1564:     /* receive numeric values */
1565:     PetscMalloc1(m*N,&vals);

1567:     /* receive message of values*/
1568:     MPIULong_Recv(vals,m*N,MPIU_SCALAR,0,((PetscObject)(newmat))->tag,comm);

1570:     /* insert into matrix-by row (this is why cannot directly read into array */
1571:     vals_ptr = vals;
1572:     for (i=0; i<m; i++) {
1573:       for (j=0; j<N; j++) {
1574:         array[i + j*m] = *vals_ptr++;
1575:       }
1576:     }
1577:   }
1578:   MatDenseRestoreArray(newmat,&array);
1579:   PetscFree(vals);
1580:   MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
1581:   MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
1582:   return(0);
1583: }

1587: PetscErrorCode MatLoad_MPIDense(Mat newmat,PetscViewer viewer)
1588: {
1589:   Mat_MPIDense   *a;
1590:   PetscScalar    *vals,*svals;
1591:   MPI_Comm       comm;
1592:   MPI_Status     status;
1593:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag,*rowners,*sndcounts,m,n,maxnz;
1594:   PetscInt       header[4],*rowlengths = 0,M,N,*cols;
1595:   PetscInt       *ourlens,*procsnz = 0,jj,*mycols,*smycols;
1596:   PetscInt       i,nz,j,rstart,rend;
1597:   int            fd;

1601:   /* force binary viewer to load .info file if it has not yet done so */
1602:   PetscViewerSetUp(viewer);
1603:   PetscObjectGetComm((PetscObject)viewer,&comm);
1604:   MPI_Comm_size(comm,&size);
1605:   MPI_Comm_rank(comm,&rank);
1606:   PetscViewerBinaryGetDescriptor(viewer,&fd);
1607:   if (!rank) {
1608:     PetscBinaryRead(fd,(char*)header,4,PETSC_INT);
1609:     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
1610:   }
1611:   MPI_Bcast(header+1,3,MPIU_INT,0,comm);
1612:   M    = header[1]; N = header[2]; nz = header[3];

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

1618:   if (newmat->rmap->N != M) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows:Matrix in file has (%D) and input matrix has (%D)",M,newmat->rmap->N);
1619:   if (newmat->cmap->N != N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of cols:Matrix in file has (%D) and input matrix has (%D)",N,newmat->cmap->N);

1621:   /*
1622:        Handle case where matrix is stored on disk as a dense matrix
1623:   */
1624:   if (nz == MATRIX_BINARY_FORMAT_DENSE) {
1625:     MatLoad_MPIDense_DenseInFile(comm,fd,M,N,newmat);
1626:     return(0);
1627:   }

1629:   /* determine ownership of all rows */
1630:   if (newmat->rmap->n < 0) {
1631:     PetscMPIIntCast(M/size + ((M % size) > rank),&m);
1632:   } else {
1633:     PetscMPIIntCast(newmat->rmap->n,&m);
1634:   }
1635:   if (newmat->cmap->n < 0) {
1636:     n = PETSC_DECIDE;
1637:   } else {
1638:     PetscMPIIntCast(newmat->cmap->n,&n);
1639:   }

1641:   PetscMalloc1(size+2,&rowners);
1642:   MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);
1643:   rowners[0] = 0;
1644:   for (i=2; i<=size; i++) {
1645:     rowners[i] += rowners[i-1];
1646:   }
1647:   rstart = rowners[rank];
1648:   rend   = rowners[rank+1];

1650:   /* distribute row lengths to all processors */
1651:   PetscMalloc1(rend-rstart,&ourlens);
1652:   if (!rank) {
1653:     PetscMalloc1(M,&rowlengths);
1654:     PetscBinaryRead(fd,rowlengths,M,PETSC_INT);
1655:     PetscMalloc1(size,&sndcounts);
1656:     for (i=0; i<size; i++) sndcounts[i] = rowners[i+1] - rowners[i];
1657:     MPI_Scatterv(rowlengths,sndcounts,rowners,MPIU_INT,ourlens,rend-rstart,MPIU_INT,0,comm);
1658:     PetscFree(sndcounts);
1659:   } else {
1660:     MPI_Scatterv(0,0,0,MPIU_INT,ourlens,rend-rstart,MPIU_INT,0,comm);
1661:   }

1663:   if (!rank) {
1664:     /* calculate the number of nonzeros on each processor */
1665:     PetscMalloc1(size,&procsnz);
1666:     PetscMemzero(procsnz,size*sizeof(PetscInt));
1667:     for (i=0; i<size; i++) {
1668:       for (j=rowners[i]; j< rowners[i+1]; j++) {
1669:         procsnz[i] += rowlengths[j];
1670:       }
1671:     }
1672:     PetscFree(rowlengths);

1674:     /* determine max buffer needed and allocate it */
1675:     maxnz = 0;
1676:     for (i=0; i<size; i++) {
1677:       maxnz = PetscMax(maxnz,procsnz[i]);
1678:     }
1679:     PetscMalloc1(maxnz,&cols);

1681:     /* read in my part of the matrix column indices  */
1682:     nz   = procsnz[0];
1683:     PetscMalloc1(nz,&mycols);
1684:     PetscBinaryRead(fd,mycols,nz,PETSC_INT);

1686:     /* read in every one elses and ship off */
1687:     for (i=1; i<size; i++) {
1688:       nz   = procsnz[i];
1689:       PetscBinaryRead(fd,cols,nz,PETSC_INT);
1690:       MPI_Send(cols,nz,MPIU_INT,i,tag,comm);
1691:     }
1692:     PetscFree(cols);
1693:   } else {
1694:     /* determine buffer space needed for message */
1695:     nz = 0;
1696:     for (i=0; i<m; i++) {
1697:       nz += ourlens[i];
1698:     }
1699:     PetscMalloc1(nz+1,&mycols);

1701:     /* receive message of column indices*/
1702:     MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);
1703:     MPI_Get_count(&status,MPIU_INT,&maxnz);
1704:     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1705:   }

1707:   MatSetSizes(newmat,m,n,M,N);
1708:   a = (Mat_MPIDense*)newmat->data;
1709:   if (!a->A || !((Mat_SeqDense*)(a->A->data))->user_alloc) {
1710:     MatMPIDenseSetPreallocation(newmat,NULL);
1711:   }

1713:   if (!rank) {
1714:     PetscMalloc1(maxnz,&vals);

1716:     /* read in my part of the matrix numerical values  */
1717:     nz   = procsnz[0];
1718:     PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);

1720:     /* insert into matrix */
1721:     jj      = rstart;
1722:     smycols = mycols;
1723:     svals   = vals;
1724:     for (i=0; i<m; i++) {
1725:       MatSetValues(newmat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
1726:       smycols += ourlens[i];
1727:       svals   += ourlens[i];
1728:       jj++;
1729:     }

1731:     /* read in other processors and ship out */
1732:     for (i=1; i<size; i++) {
1733:       nz   = procsnz[i];
1734:       PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
1735:       MPI_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);
1736:     }
1737:     PetscFree(procsnz);
1738:   } else {
1739:     /* receive numeric values */
1740:     PetscMalloc1(nz+1,&vals);

1742:     /* receive message of values*/
1743:     MPI_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm,&status);
1744:     MPI_Get_count(&status,MPIU_SCALAR,&maxnz);
1745:     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");

1747:     /* insert into matrix */
1748:     jj      = rstart;
1749:     smycols = mycols;
1750:     svals   = vals;
1751:     for (i=0; i<m; i++) {
1752:       MatSetValues(newmat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
1753:       smycols += ourlens[i];
1754:       svals   += ourlens[i];
1755:       jj++;
1756:     }
1757:   }
1758:   PetscFree(ourlens);
1759:   PetscFree(vals);
1760:   PetscFree(mycols);
1761:   PetscFree(rowners);

1763:   MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
1764:   MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
1765:   return(0);
1766: }

1770: PetscErrorCode MatEqual_MPIDense(Mat A,Mat B,PetscBool  *flag)
1771: {
1772:   Mat_MPIDense   *matB = (Mat_MPIDense*)B->data,*matA = (Mat_MPIDense*)A->data;
1773:   Mat            a,b;
1774:   PetscBool      flg;

1778:   a    = matA->A;
1779:   b    = matB->A;
1780:   MatEqual(a,b,&flg);
1781:   MPIU_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
1782:   return(0);
1783: }

1787: PetscErrorCode MatDestroy_MatTransMatMult_MPIDense_MPIDense(Mat A)
1788: {
1789:   PetscErrorCode        ierr;
1790:   Mat_MPIDense          *a = (Mat_MPIDense*)A->data;
1791:   Mat_TransMatMultDense *atb = a->atbdense;

1794:   PetscFree3(atb->sendbuf,atb->atbarray,atb->recvcounts);
1795:   (atb->destroy)(A);
1796:   PetscFree(atb);
1797:   return(0);
1798: }

1802: PetscErrorCode MatTransposeMatMultNumeric_MPIDense_MPIDense(Mat A,Mat B,Mat C)
1803: {
1804:   Mat_MPIDense   *a=(Mat_MPIDense*)A->data, *b=(Mat_MPIDense*)B->data, *c=(Mat_MPIDense*)C->data;
1805:   Mat_SeqDense   *aseq=(Mat_SeqDense*)(a->A)->data, *bseq=(Mat_SeqDense*)(b->A)->data;
1806:   Mat_TransMatMultDense *atb = c->atbdense;
1808:   MPI_Comm       comm;
1809:   PetscMPIInt    rank,size,*recvcounts=atb->recvcounts;
1810:   PetscScalar    *carray,*atbarray=atb->atbarray,*sendbuf=atb->sendbuf;
1811:   PetscInt       i,cN=C->cmap->N,cM=C->rmap->N,proc,k,j;
1812:   PetscScalar    _DOne=1.0,_DZero=0.0;
1813:   PetscBLASInt   am,an,bn,aN;
1814:   const PetscInt *ranges;

1817:   PetscObjectGetComm((PetscObject)A,&comm);
1818:   MPI_Comm_rank(comm,&rank);
1819:   MPI_Comm_size(comm,&size);

1821:   /* compute atbarray = aseq^T * bseq */
1822:   PetscBLASIntCast(a->A->cmap->n,&an);
1823:   PetscBLASIntCast(b->A->cmap->n,&bn);
1824:   PetscBLASIntCast(a->A->rmap->n,&am);
1825:   PetscBLASIntCast(A->cmap->N,&aN);
1826:   PetscStackCallBLAS("BLASgemm",BLASgemm_("T","N",&an,&bn,&am,&_DOne,aseq->v,&aseq->lda,bseq->v,&bseq->lda,&_DZero,atbarray,&aN));
1827: 
1828:   MatGetOwnershipRanges(C,&ranges);
1829:   for (i=0; i<size; i++) recvcounts[i] = (ranges[i+1] - ranges[i])*cN;
1830: 
1831:   /* arrange atbarray into sendbuf */
1832:   k = 0;
1833:   for (proc=0; proc<size; proc++) {
1834:     for (j=0; j<cN; j++) {
1835:       for (i=ranges[proc]; i<ranges[proc+1]; i++) sendbuf[k++] = atbarray[i+j*cM];
1836:     }
1837:   }
1838:   /* sum all atbarray to local values of C */
1839:   MatDenseGetArray(c->A,&carray);
1840:   MPI_Reduce_scatter(sendbuf,carray,recvcounts,MPIU_SCALAR,MPIU_SUM,comm);
1841:   MatDenseRestoreArray(c->A,&carray);
1842:   return(0);
1843: }

1847: PetscErrorCode MatTransposeMatMultSymbolic_MPIDense_MPIDense(Mat A,Mat B,PetscReal fill,Mat *C)
1848: {
1849:   PetscErrorCode        ierr;
1850:   Mat                   Cdense;
1851:   MPI_Comm              comm;
1852:   PetscMPIInt           size;
1853:   PetscInt              cm=A->cmap->n,cM,cN=B->cmap->N;
1854:   Mat_MPIDense          *c;
1855:   Mat_TransMatMultDense *atb;

1858:   PetscObjectGetComm((PetscObject)A,&comm);
1859:   if (A->rmap->rstart != B->rmap->rstart || A->rmap->rend != B->rmap->rend) {
1860:     SETERRQ4(comm,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, A (%D, %D) != B (%D,%D)",A->rmap->rstart,A->rmap->rend,B->rmap->rstart,B->rmap->rend);
1861:   }

1863:   /* create matrix product Cdense */
1864:   MatCreate(comm,&Cdense);
1865:   MatSetSizes(Cdense,cm,B->cmap->n,PETSC_DECIDE,PETSC_DECIDE);
1866:   MatSetType(Cdense,MATMPIDENSE);
1867:   MatMPIDenseSetPreallocation(Cdense,NULL);
1868:   MatAssemblyBegin(Cdense,MAT_FINAL_ASSEMBLY);
1869:   MatAssemblyEnd(Cdense,MAT_FINAL_ASSEMBLY);
1870:   *C   = Cdense;

1872:   /* create data structure for reuse Cdense */
1873:   MPI_Comm_size(comm,&size);
1874:   PetscNew(&atb);
1875:   cM = Cdense->rmap->N;
1876:   PetscMalloc3(cM*cN,&atb->sendbuf,cM*cN,&atb->atbarray,size,&atb->recvcounts);
1877: 
1878:   c                    = (Mat_MPIDense*)Cdense->data;
1879:   c->atbdense          = atb;
1880:   atb->destroy         = Cdense->ops->destroy;
1881:   Cdense->ops->destroy = MatDestroy_MatTransMatMult_MPIDense_MPIDense;
1882:   return(0);
1883: }

1887: PetscErrorCode MatTransposeMatMult_MPIDense_MPIDense(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
1888: {

1892:   if (scall == MAT_INITIAL_MATRIX) {
1893:     MatTransposeMatMultSymbolic_MPIDense_MPIDense(A,B,fill,C);
1894:   }
1895:   MatTransposeMatMultNumeric_MPIDense_MPIDense(A,B,*C);
1896:   return(0);
1897: }

1901: PetscErrorCode MatDestroy_MatMatMult_MPIDense_MPIDense(Mat A)
1902: {
1903:   PetscErrorCode   ierr;
1904:   Mat_MPIDense     *a = (Mat_MPIDense*)A->data;
1905:   Mat_MatMultDense *ab = a->abdense;

1908:   MatDestroy(&ab->Ce);
1909:   MatDestroy(&ab->Ae);
1910:   MatDestroy(&ab->Be);

1912:   (ab->destroy)(A);
1913:   PetscFree(ab);
1914:   return(0);
1915: }

1917: #if defined(PETSC_HAVE_ELEMENTAL)
1920: PetscErrorCode MatMatMultNumeric_MPIDense_MPIDense(Mat A,Mat B,Mat C)
1921: {
1922:   PetscErrorCode   ierr;
1923:   Mat_MPIDense     *c=(Mat_MPIDense*)C->data;
1924:   Mat_MatMultDense *ab=c->abdense;

1927:   MatConvert_MPIDense_Elemental(A,MATELEMENTAL,MAT_REUSE_MATRIX, &ab->Ae);
1928:   MatConvert_MPIDense_Elemental(B,MATELEMENTAL,MAT_REUSE_MATRIX, &ab->Be);
1929:   MatMatMultNumeric(ab->Ae,ab->Be,ab->Ce);
1930:   MatConvert(ab->Ce,MATMPIDENSE,MAT_REUSE_MATRIX,&C);
1931:   return(0);
1932: }

1936: PetscErrorCode MatMatMultSymbolic_MPIDense_MPIDense(Mat A,Mat B,PetscReal fill,Mat *C)
1937: {
1938:   PetscErrorCode   ierr;
1939:   Mat              Ae,Be,Ce;
1940:   Mat_MPIDense     *c;
1941:   Mat_MatMultDense *ab;

1944:   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
1945:     SETERRQ4(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, A (%D, %D) != B (%D,%D)",A->rmap->rstart,A->rmap->rend,B->rmap->rstart,B->rmap->rend);
1946:   }

1948:   /* convert A and B to Elemental matrices Ae and Be */
1949:   MatConvert(A,MATELEMENTAL,MAT_INITIAL_MATRIX, &Ae);
1950:   MatConvert(B,MATELEMENTAL,MAT_INITIAL_MATRIX, &Be);

1952:   /* Ce = Ae*Be */
1953:   MatMatMultSymbolic(Ae,Be,fill,&Ce);
1954:   MatMatMultNumeric(Ae,Be,Ce);
1955: 
1956:   /* convert Ce to C */
1957:   MatConvert(Ce,MATMPIDENSE,MAT_INITIAL_MATRIX,C);

1959:   /* create data structure for reuse Cdense */
1960:   PetscNew(&ab);
1961:   c                  = (Mat_MPIDense*)(*C)->data;
1962:   c->abdense         = ab;

1964:   ab->Ae             = Ae;
1965:   ab->Be             = Be;
1966:   ab->Ce             = Ce;
1967:   ab->destroy        = (*C)->ops->destroy;
1968:   (*C)->ops->destroy        = MatDestroy_MatMatMult_MPIDense_MPIDense;
1969:   (*C)->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIDense;
1970:   return(0);
1971: }

1975: PETSC_INTERN PetscErrorCode MatMatMult_MPIDense_MPIDense(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
1976: {

1980:   if (scall == MAT_INITIAL_MATRIX) { /* simbolic product includes numeric product */
1981:     PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
1982:     MatMatMultSymbolic_MPIDense_MPIDense(A,B,fill,C);
1983:     PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
1984:   } else {
1985:     PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
1986:     MatMatMultNumeric_MPIDense_MPIDense(A,B,*C);
1987:     PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
1988:   }
1989:   return(0);
1990: }
1991: #endif