Actual source code: mpidense.c


  2: /*
  3:    Basic functions for basic parallel dense matrices.
  4: */

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

 10: /*@

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

 15:     Input Parameter:
 16: .      A - the Seq or MPI dense matrix

 18:     Output Parameter:
 19: .      B - the inner matrix

 21:     Level: intermediate

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

 33:   PetscObjectBaseTypeCompare((PetscObject)A,MATMPIDENSE,&flg);
 34:   if (flg) *B = mat->A;
 35:   else {
 36:     PetscObjectBaseTypeCompare((PetscObject)A,MATSEQDENSE,&flg);
 37:     if (!flg) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Not for matrix type %s",((PetscObject)A)->type_name);
 38:     *B = A;
 39:   }
 40:   return(0);
 41: }

 43: PetscErrorCode MatCopy_MPIDense(Mat A, Mat B, MatStructure s)
 44: {
 45:   Mat_MPIDense   *Amat = (Mat_MPIDense*)A->data;
 46:   Mat_MPIDense   *Bmat = (Mat_MPIDense*)B->data;

 50:   MatCopy(Amat->A,Bmat->A,s);
 51:   return(0);
 52: }

 54: PetscErrorCode MatGetRow_MPIDense(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
 55: {
 56:   Mat_MPIDense   *mat = (Mat_MPIDense*)A->data;
 58:   PetscInt       lrow,rstart = A->rmap->rstart,rend = A->rmap->rend;

 61:   if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"only local rows");
 62:   lrow = row - rstart;
 63:   MatGetRow(mat->A,lrow,nz,(const PetscInt**)idx,(const PetscScalar**)v);
 64:   return(0);
 65: }

 67: PetscErrorCode MatRestoreRow_MPIDense(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
 68: {
 69:   Mat_MPIDense   *mat = (Mat_MPIDense*)A->data;
 71:   PetscInt       lrow,rstart = A->rmap->rstart,rend = A->rmap->rend;

 74:   if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"only local rows");
 75:   lrow = row - rstart;
 76:   MatRestoreRow(mat->A,lrow,nz,(const PetscInt**)idx,(const PetscScalar**)v);
 77:   return(0);
 78: }

 80: PetscErrorCode  MatGetDiagonalBlock_MPIDense(Mat A,Mat *a)
 81: {
 82:   Mat_MPIDense   *mdn = (Mat_MPIDense*)A->data;
 84:   PetscInt       m = A->rmap->n,rstart = A->rmap->rstart;
 85:   PetscScalar    *array;
 86:   MPI_Comm       comm;
 87:   PetscBool      flg;
 88:   Mat            B;

 91:   MatHasCongruentLayouts(A,&flg);
 92:   if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only square matrices supported.");
 93:   PetscObjectQuery((PetscObject)A,"DiagonalBlock",(PetscObject*)&B);
 94:   if (!B) { /* This should use MatDenseGetSubMatrix (not create), but we would need a call like MatRestoreDiagonalBlock */

 96:     PetscObjectTypeCompare((PetscObject)mdn->A,MATSEQDENSECUDA,&flg);
 97:     if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not coded for %s. Send an email to petsc-dev@mcs.anl.gov to request this feature",MATSEQDENSECUDA);
 98:     PetscObjectGetComm((PetscObject)(mdn->A),&comm);
 99:     MatCreate(comm,&B);
100:     MatSetSizes(B,m,m,m,m);
101:     MatSetType(B,((PetscObject)mdn->A)->type_name);
102:     MatDenseGetArrayRead(mdn->A,(const PetscScalar**)&array);
103:     MatSeqDenseSetPreallocation(B,array+m*rstart);
104:     MatDenseRestoreArrayRead(mdn->A,(const PetscScalar**)&array);
105:     MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
106:     MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
107:     PetscObjectCompose((PetscObject)A,"DiagonalBlock",(PetscObject)B);
108:     *a   = B;
109:     MatDestroy(&B);
110:   } else *a = B;
111:   return(0);
112: }

114: PetscErrorCode MatSetValues_MPIDense(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv)
115: {
116:   Mat_MPIDense   *A = (Mat_MPIDense*)mat->data;
118:   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend,row;
119:   PetscBool      roworiented = A->roworiented;

122:   for (i=0; i<m; i++) {
123:     if (idxm[i] < 0) continue;
124:     if (idxm[i] >= mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
125:     if (idxm[i] >= rstart && idxm[i] < rend) {
126:       row = idxm[i] - rstart;
127:       if (roworiented) {
128:         MatSetValues(A->A,1,&row,n,idxn,v+i*n,addv);
129:       } else {
130:         for (j=0; j<n; j++) {
131:           if (idxn[j] < 0) continue;
132:           if (idxn[j] >= mat->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
133:           MatSetValues(A->A,1,&row,1,&idxn[j],v+i+j*m,addv);
134:         }
135:       }
136:     } else if (!A->donotstash) {
137:       mat->assembled = PETSC_FALSE;
138:       if (roworiented) {
139:         MatStashValuesRow_Private(&mat->stash,idxm[i],n,idxn,v+i*n,PETSC_FALSE);
140:       } else {
141:         MatStashValuesCol_Private(&mat->stash,idxm[i],n,idxn,v+i,m,PETSC_FALSE);
142:       }
143:     }
144:   }
145:   return(0);
146: }

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

155:   for (i=0; i<m; i++) {
156:     if (idxm[i] < 0) continue; /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row"); */
157:     if (idxm[i] >= mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
158:     if (idxm[i] >= rstart && idxm[i] < rend) {
159:       row = idxm[i] - rstart;
160:       for (j=0; j<n; j++) {
161:         if (idxn[j] < 0) continue; /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column"); */
162:         if (idxn[j] >= mat->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
163:         MatGetValues(mdn->A,1,&row,1,&idxn[j],v+i*n+j);
164:       }
165:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
166:   }
167:   return(0);
168: }

170: static PetscErrorCode MatDenseGetLDA_MPIDense(Mat A,PetscInt *lda)
171: {
172:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

176:   MatDenseGetLDA(a->A,lda);
177:   return(0);
178: }

180: static PetscErrorCode MatDenseSetLDA_MPIDense(Mat A,PetscInt lda)
181: {
182:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;
183:   PetscBool      iscuda;

187:   if (!a->A) {
188:     if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
189:     PetscLayoutSetUp(A->rmap);
190:     PetscLayoutSetUp(A->cmap);
191:     MatCreate(PETSC_COMM_SELF,&a->A);
192:     PetscLogObjectParent((PetscObject)A,(PetscObject)a->A);
193:     MatSetSizes(a->A,A->rmap->n,A->cmap->N,A->rmap->n,A->cmap->N);
194:     PetscObjectTypeCompare((PetscObject)A,MATMPIDENSECUDA,&iscuda);
195:     MatSetType(a->A,iscuda ? MATSEQDENSECUDA : MATSEQDENSE);
196:   }
197:   MatDenseSetLDA(a->A,lda);
198:   return(0);
199: }

201: static PetscErrorCode MatDenseGetArray_MPIDense(Mat A,PetscScalar **array)
202: {
203:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

207:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
208:   MatDenseGetArray(a->A,array);
209:   return(0);
210: }

212: static PetscErrorCode MatDenseGetArrayRead_MPIDense(Mat A,const PetscScalar **array)
213: {
214:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

218:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
219:   MatDenseGetArrayRead(a->A,array);
220:   return(0);
221: }

223: static PetscErrorCode MatDenseGetArrayWrite_MPIDense(Mat A,PetscScalar **array)
224: {
225:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

229:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
230:   MatDenseGetArrayWrite(a->A,array);
231:   return(0);
232: }

234: static PetscErrorCode MatDensePlaceArray_MPIDense(Mat A,const PetscScalar *array)
235: {
236:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

240:   if (a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
241:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
242:   MatDensePlaceArray(a->A,array);
243:   return(0);
244: }

246: static PetscErrorCode MatDenseResetArray_MPIDense(Mat A)
247: {
248:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

252:   if (a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
253:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
254:   MatDenseResetArray(a->A);
255:   return(0);
256: }

258: static PetscErrorCode MatDenseReplaceArray_MPIDense(Mat A,const PetscScalar *array)
259: {
260:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

264:   if (a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
265:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
266:   MatDenseReplaceArray(a->A,array);
267:   return(0);
268: }

270: static PetscErrorCode MatCreateSubMatrix_MPIDense(Mat A,IS isrow,IS iscol,MatReuse scall,Mat *B)
271: {
272:   Mat_MPIDense      *mat  = (Mat_MPIDense*)A->data,*newmatd;
273:   PetscErrorCode    ierr;
274:   PetscInt          lda,i,j,rstart,rend,nrows,ncols,Ncols,nlrows,nlcols;
275:   const PetscInt    *irow,*icol;
276:   const PetscScalar *v;
277:   PetscScalar       *bv;
278:   Mat               newmat;
279:   IS                iscol_local;
280:   MPI_Comm          comm_is,comm_mat;

283:   PetscObjectGetComm((PetscObject)A,&comm_mat);
284:   PetscObjectGetComm((PetscObject)iscol,&comm_is);
285:   if (comm_mat != comm_is) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"IS communicator must match matrix communicator");

287:   ISAllGather(iscol,&iscol_local);
288:   ISGetIndices(isrow,&irow);
289:   ISGetIndices(iscol_local,&icol);
290:   ISGetLocalSize(isrow,&nrows);
291:   ISGetLocalSize(iscol,&ncols);
292:   ISGetSize(iscol,&Ncols); /* global number of columns, size of iscol_local */

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

298:   MatGetLocalSize(A,&nlrows,&nlcols);
299:   MatGetOwnershipRange(A,&rstart,&rend);

301:   /* Check submatrix call */
302:   if (scall == MAT_REUSE_MATRIX) {
303:     /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); */
304:     /* Really need to test rows and column sizes! */
305:     newmat = *B;
306:   } else {
307:     /* Create and fill new matrix */
308:     MatCreate(PetscObjectComm((PetscObject)A),&newmat);
309:     MatSetSizes(newmat,nrows,ncols,PETSC_DECIDE,Ncols);
310:     MatSetType(newmat,((PetscObject)A)->type_name);
311:     MatMPIDenseSetPreallocation(newmat,NULL);
312:   }

314:   /* Now extract the data pointers and do the copy, column at a time */
315:   newmatd = (Mat_MPIDense*)newmat->data;
316:   MatDenseGetArray(newmatd->A,&bv);
317:   MatDenseGetArrayRead(mat->A,&v);
318:   MatDenseGetLDA(mat->A,&lda);
319:   for (i=0; i<Ncols; i++) {
320:     const PetscScalar *av = v + lda*icol[i];
321:     for (j=0; j<nrows; j++) {
322:       *bv++ = av[irow[j] - rstart];
323:     }
324:   }
325:   MatDenseRestoreArrayRead(mat->A,&v);
326:   MatDenseRestoreArray(newmatd->A,&bv);

328:   /* Assemble the matrices so that the correct flags are set */
329:   MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
330:   MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);

332:   /* Free work space */
333:   ISRestoreIndices(isrow,&irow);
334:   ISRestoreIndices(iscol_local,&icol);
335:   ISDestroy(&iscol_local);
336:   *B   = newmat;
337:   return(0);
338: }

340: PetscErrorCode MatDenseRestoreArray_MPIDense(Mat A,PetscScalar **array)
341: {
342:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

346:   MatDenseRestoreArray(a->A,array);
347:   return(0);
348: }

350: PetscErrorCode MatDenseRestoreArrayRead_MPIDense(Mat A,const PetscScalar **array)
351: {
352:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

356:   MatDenseRestoreArrayRead(a->A,array);
357:   return(0);
358: }

360: PetscErrorCode MatDenseRestoreArrayWrite_MPIDense(Mat A,PetscScalar **array)
361: {
362:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

366:   MatDenseRestoreArrayWrite(a->A,array);
367:   return(0);
368: }

370: PetscErrorCode MatAssemblyBegin_MPIDense(Mat mat,MatAssemblyType mode)
371: {
372:   Mat_MPIDense   *mdn = (Mat_MPIDense*)mat->data;
374:   PetscInt       nstash,reallocs;

377:   if (mdn->donotstash || mat->nooffprocentries) return(0);

379:   MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
380:   MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
381:   PetscInfo2(mdn->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
382:   return(0);
383: }

385: PetscErrorCode MatAssemblyEnd_MPIDense(Mat mat,MatAssemblyType mode)
386: {
387:   Mat_MPIDense   *mdn=(Mat_MPIDense*)mat->data;
389:   PetscInt       i,*row,*col,flg,j,rstart,ncols;
390:   PetscMPIInt    n;
391:   PetscScalar    *val;

394:   if (!mdn->donotstash && !mat->nooffprocentries) {
395:     /*  wait on receives */
396:     while (1) {
397:       MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
398:       if (!flg) break;

400:       for (i=0; i<n;) {
401:         /* Now identify the consecutive vals belonging to the same row */
402:         for (j=i,rstart=row[j]; j<n; j++) {
403:           if (row[j] != rstart) break;
404:         }
405:         if (j < n) ncols = j-i;
406:         else       ncols = n-i;
407:         /* Now assemble all these values with a single function call */
408:         MatSetValues_MPIDense(mat,1,row+i,ncols,col+i,val+i,mat->insertmode);
409:         i    = j;
410:       }
411:     }
412:     MatStashScatterEnd_Private(&mat->stash);
413:   }

415:   MatAssemblyBegin(mdn->A,mode);
416:   MatAssemblyEnd(mdn->A,mode);

418:   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
419:     MatSetUpMultiply_MPIDense(mat);
420:   }
421:   return(0);
422: }

424: PetscErrorCode MatZeroEntries_MPIDense(Mat A)
425: {
427:   Mat_MPIDense   *l = (Mat_MPIDense*)A->data;

430:   MatZeroEntries(l->A);
431:   return(0);
432: }

434: PetscErrorCode MatZeroRows_MPIDense(Mat A,PetscInt n,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
435: {
436:   Mat_MPIDense      *l = (Mat_MPIDense*)A->data;
437:   PetscErrorCode    ierr;
438:   PetscInt          i,len,*lrows;

441:   /* get locally owned rows */
442:   PetscLayoutMapLocal(A->rmap,n,rows,&len,&lrows,NULL);
443:   /* fix right hand side if needed */
444:   if (x && b) {
445:     const PetscScalar *xx;
446:     PetscScalar       *bb;

448:     VecGetArrayRead(x, &xx);
449:     VecGetArrayWrite(b, &bb);
450:     for (i=0;i<len;++i) bb[lrows[i]] = diag*xx[lrows[i]];
451:     VecRestoreArrayRead(x, &xx);
452:     VecRestoreArrayWrite(b, &bb);
453:   }
454:   MatZeroRows(l->A,len,lrows,0.0,NULL,NULL);
455:   if (diag != 0.0) {
456:     Vec d;

458:     MatCreateVecs(A,NULL,&d);
459:     VecSet(d,diag);
460:     MatDiagonalSet(A,d,INSERT_VALUES);
461:     VecDestroy(&d);
462:   }
463:   PetscFree(lrows);
464:   return(0);
465: }

467: PETSC_INTERN PetscErrorCode MatMult_SeqDense(Mat,Vec,Vec);
468: PETSC_INTERN PetscErrorCode MatMultAdd_SeqDense(Mat,Vec,Vec,Vec);
469: PETSC_INTERN PetscErrorCode MatMultTranspose_SeqDense(Mat,Vec,Vec);
470: PETSC_INTERN PetscErrorCode MatMultTransposeAdd_SeqDense(Mat,Vec,Vec,Vec);

472: PetscErrorCode MatMult_MPIDense(Mat mat,Vec xx,Vec yy)
473: {
474:   Mat_MPIDense      *mdn = (Mat_MPIDense*)mat->data;
475:   PetscErrorCode    ierr;
476:   const PetscScalar *ax;
477:   PetscScalar       *ay;
478:   PetscMemType      axmtype,aymtype;

481:   VecGetArrayReadAndMemType(xx,&ax,&axmtype);
482:   VecGetArrayAndMemType(mdn->lvec,&ay,&aymtype);
483:   PetscSFBcastWithMemTypeBegin(mdn->Mvctx,MPIU_SCALAR,axmtype,ax,aymtype,ay,MPI_REPLACE);
484:   PetscSFBcastEnd(mdn->Mvctx,MPIU_SCALAR,ax,ay,MPI_REPLACE);
485:   VecRestoreArrayAndMemType(mdn->lvec,&ay);
486:   VecRestoreArrayReadAndMemType(xx,&ax);
487:   (*mdn->A->ops->mult)(mdn->A,mdn->lvec,yy);
488:   return(0);
489: }

491: PetscErrorCode MatMultAdd_MPIDense(Mat mat,Vec xx,Vec yy,Vec zz)
492: {
493:   Mat_MPIDense      *mdn = (Mat_MPIDense*)mat->data;
494:   PetscErrorCode    ierr;
495:   const PetscScalar *ax;
496:   PetscScalar       *ay;
497:   PetscMemType      axmtype,aymtype;

500:   VecGetArrayReadAndMemType(xx,&ax,&axmtype);
501:   VecGetArrayAndMemType(mdn->lvec,&ay,&aymtype);
502:   PetscSFBcastWithMemTypeBegin(mdn->Mvctx,MPIU_SCALAR,axmtype,ax,aymtype,ay,MPI_REPLACE);
503:   PetscSFBcastEnd(mdn->Mvctx,MPIU_SCALAR,ax,ay,MPI_REPLACE);
504:   VecRestoreArrayAndMemType(mdn->lvec,&ay);
505:   VecRestoreArrayReadAndMemType(xx,&ax);
506:   (*mdn->A->ops->multadd)(mdn->A,mdn->lvec,yy,zz);
507:   return(0);
508: }

510: PetscErrorCode MatMultTranspose_MPIDense(Mat A,Vec xx,Vec yy)
511: {
512:   Mat_MPIDense      *a = (Mat_MPIDense*)A->data;
513:   PetscErrorCode    ierr;
514:   const PetscScalar *ax;
515:   PetscScalar       *ay;
516:   PetscMemType      axmtype,aymtype;

519:   VecSet(yy,0.0);
520:   (*a->A->ops->multtranspose)(a->A,xx,a->lvec);
521:   VecGetArrayReadAndMemType(a->lvec,&ax,&axmtype);
522:   VecGetArrayAndMemType(yy,&ay,&aymtype);
523:   PetscSFReduceWithMemTypeBegin(a->Mvctx,MPIU_SCALAR,axmtype,ax,aymtype,ay,MPIU_SUM);
524:   PetscSFReduceEnd(a->Mvctx,MPIU_SCALAR,ax,ay,MPIU_SUM);
525:   VecRestoreArrayReadAndMemType(a->lvec,&ax);
526:   VecRestoreArrayAndMemType(yy,&ay);
527:   return(0);
528: }

530: PetscErrorCode MatMultTransposeAdd_MPIDense(Mat A,Vec xx,Vec yy,Vec zz)
531: {
532:   Mat_MPIDense      *a = (Mat_MPIDense*)A->data;
533:   PetscErrorCode    ierr;
534:   const PetscScalar *ax;
535:   PetscScalar       *ay;
536:   PetscMemType      axmtype,aymtype;

539:   VecCopy(yy,zz);
540:   (*a->A->ops->multtranspose)(a->A,xx,a->lvec);
541:   VecGetArrayReadAndMemType(a->lvec,&ax,&axmtype);
542:   VecGetArrayAndMemType(zz,&ay,&aymtype);
543:   PetscSFReduceWithMemTypeBegin(a->Mvctx,MPIU_SCALAR,axmtype,ax,aymtype,ay,MPIU_SUM);
544:   PetscSFReduceEnd(a->Mvctx,MPIU_SCALAR,ax,ay,MPIU_SUM);
545:   VecRestoreArrayReadAndMemType(a->lvec,&ax);
546:   VecRestoreArrayAndMemType(zz,&ay);
547:   return(0);
548: }

550: PetscErrorCode MatGetDiagonal_MPIDense(Mat A,Vec v)
551: {
552:   Mat_MPIDense      *a    = (Mat_MPIDense*)A->data;
553:   PetscErrorCode    ierr;
554:   PetscInt          lda,len,i,n,m = A->rmap->n,radd;
555:   PetscScalar       *x,zero = 0.0;
556:   const PetscScalar *av;

559:   VecSet(v,zero);
560:   VecGetArray(v,&x);
561:   VecGetSize(v,&n);
562:   if (n != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming mat and vec");
563:   len  = PetscMin(a->A->rmap->n,a->A->cmap->n);
564:   radd = A->rmap->rstart*m;
565:   MatDenseGetArrayRead(a->A,&av);
566:   MatDenseGetLDA(a->A,&lda);
567:   for (i=0; i<len; i++) {
568:     x[i] = av[radd + i*lda + i];
569:   }
570:   MatDenseRestoreArrayRead(a->A,&av);
571:   VecRestoreArray(v,&x);
572:   return(0);
573: }

575: PetscErrorCode MatDestroy_MPIDense(Mat mat)
576: {
577:   Mat_MPIDense   *mdn = (Mat_MPIDense*)mat->data;

581: #if defined(PETSC_USE_LOG)
582:   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N);
583: #endif
584:   MatStashDestroy_Private(&mat->stash);
585:   if (mdn->vecinuse) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
586:   if (mdn->matinuse) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
587:   MatDestroy(&mdn->A);
588:   VecDestroy(&mdn->lvec);
589:   PetscSFDestroy(&mdn->Mvctx);
590:   VecDestroy(&mdn->cvec);
591:   MatDestroy(&mdn->cmat);

593:   PetscFree(mat->data);
594:   PetscObjectChangeTypeName((PetscObject)mat,NULL);

596:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetLDA_C",NULL);
597:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseSetLDA_C",NULL);
598:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetArray_C",NULL);
599:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreArray_C",NULL);
600:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetArrayRead_C",NULL);
601:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreArrayRead_C",NULL);
602:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetArrayWrite_C",NULL);
603:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreArrayWrite_C",NULL);
604:   PetscObjectComposeFunction((PetscObject)mat,"MatDensePlaceArray_C",NULL);
605:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseResetArray_C",NULL);
606:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseReplaceArray_C",NULL);
607:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpidense_C",NULL);
608:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpidense_mpiaij_C",NULL);
609: #if defined(PETSC_HAVE_ELEMENTAL)
610:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpidense_elemental_C",NULL);
611: #endif
612: #if defined(PETSC_HAVE_SCALAPACK)
613:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpidense_scalapack_C",NULL);
614: #endif
615:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIDenseSetPreallocation_C",NULL);
616:   PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_mpiaij_mpidense_C",NULL);
617:   PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_mpidense_mpiaij_C",NULL);
618: #if defined (PETSC_HAVE_CUDA)
619:   PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_mpiaijcusparse_mpidense_C",NULL);
620:   PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_mpidense_mpiaijcusparse_C",NULL);
621: #endif
622:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumn_C",NULL);
623:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumn_C",NULL);
624:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumnVec_C",NULL);
625:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumnVec_C",NULL);
626:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumnVecRead_C",NULL);
627:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumnVecRead_C",NULL);
628:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumnVecWrite_C",NULL);
629:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumnVecWrite_C",NULL);
630:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetSubMatrix_C",NULL);
631:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreSubMatrix_C",NULL);
632:   return(0);
633: }

635: PETSC_INTERN PetscErrorCode MatView_SeqDense(Mat,PetscViewer);

637: #include <petscdraw.h>
638: static PetscErrorCode MatView_MPIDense_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
639: {
640:   Mat_MPIDense      *mdn = (Mat_MPIDense*)mat->data;
641:   PetscErrorCode    ierr;
642:   PetscMPIInt       rank;
643:   PetscViewerType   vtype;
644:   PetscBool         iascii,isdraw;
645:   PetscViewer       sviewer;
646:   PetscViewerFormat format;

649:   MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
650:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
651:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
652:   if (iascii) {
653:     PetscViewerGetType(viewer,&vtype);
654:     PetscViewerGetFormat(viewer,&format);
655:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
656:       MatInfo info;
657:       MatGetInfo(mat,MAT_LOCAL,&info);
658:       PetscViewerASCIIPushSynchronized(viewer);
659:       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);
660:       PetscViewerFlush(viewer);
661:       PetscViewerASCIIPopSynchronized(viewer);
662:       PetscSFView(mdn->Mvctx,viewer);
663:       return(0);
664:     } else if (format == PETSC_VIEWER_ASCII_INFO) {
665:       return(0);
666:     }
667:   } else if (isdraw) {
668:     PetscDraw draw;
669:     PetscBool isnull;

671:     PetscViewerDrawGetDraw(viewer,0,&draw);
672:     PetscDrawIsNull(draw,&isnull);
673:     if (isnull) return(0);
674:   }

676:   {
677:     /* assemble the entire matrix onto first processor. */
678:     Mat         A;
679:     PetscInt    M = mat->rmap->N,N = mat->cmap->N,m,row,i,nz;
680:     PetscInt    *cols;
681:     PetscScalar *vals;

683:     MatCreate(PetscObjectComm((PetscObject)mat),&A);
684:     if (rank == 0) {
685:       MatSetSizes(A,M,N,M,N);
686:     } else {
687:       MatSetSizes(A,0,0,M,N);
688:     }
689:     /* Since this is a temporary matrix, MATMPIDENSE instead of ((PetscObject)A)->type_name here is probably acceptable. */
690:     MatSetType(A,MATMPIDENSE);
691:     MatMPIDenseSetPreallocation(A,NULL);
692:     PetscLogObjectParent((PetscObject)mat,(PetscObject)A);

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

698:     row = mat->rmap->rstart;
699:     m   = mdn->A->rmap->n;
700:     for (i=0; i<m; i++) {
701:       MatGetRow_MPIDense(mat,row,&nz,&cols,&vals);
702:       MatSetValues_MPIDense(A,1,&row,nz,cols,vals,INSERT_VALUES);
703:       MatRestoreRow_MPIDense(mat,row,&nz,&cols,&vals);
704:       row++;
705:     }

707:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
708:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
709:     PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
710:     if (rank == 0) {
711:       PetscObjectSetName((PetscObject)((Mat_MPIDense*)(A->data))->A,((PetscObject)mat)->name);
712:       MatView_SeqDense(((Mat_MPIDense*)(A->data))->A,sviewer);
713:     }
714:     PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
715:     PetscViewerFlush(viewer);
716:     MatDestroy(&A);
717:   }
718:   return(0);
719: }

721: PetscErrorCode MatView_MPIDense(Mat mat,PetscViewer viewer)
722: {
724:   PetscBool      iascii,isbinary,isdraw,issocket;

727:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
728:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
729:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
730:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);

732:   if (iascii || issocket || isdraw) {
733:     MatView_MPIDense_ASCIIorDraworSocket(mat,viewer);
734:   } else if (isbinary) {
735:     MatView_Dense_Binary(mat,viewer);
736:   }
737:   return(0);
738: }

740: PetscErrorCode MatGetInfo_MPIDense(Mat A,MatInfoType flag,MatInfo *info)
741: {
742:   Mat_MPIDense   *mat = (Mat_MPIDense*)A->data;
743:   Mat            mdn  = mat->A;
745:   PetscLogDouble isend[5],irecv[5];

748:   info->block_size = 1.0;

750:   MatGetInfo(mdn,MAT_LOCAL,info);

752:   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
753:   isend[3] = info->memory;  isend[4] = info->mallocs;
754:   if (flag == MAT_LOCAL) {
755:     info->nz_used      = isend[0];
756:     info->nz_allocated = isend[1];
757:     info->nz_unneeded  = isend[2];
758:     info->memory       = isend[3];
759:     info->mallocs      = isend[4];
760:   } else if (flag == MAT_GLOBAL_MAX) {
761:     MPIU_Allreduce(isend,irecv,5,MPIU_PETSCLOGDOUBLE,MPI_MAX,PetscObjectComm((PetscObject)A));

763:     info->nz_used      = irecv[0];
764:     info->nz_allocated = irecv[1];
765:     info->nz_unneeded  = irecv[2];
766:     info->memory       = irecv[3];
767:     info->mallocs      = irecv[4];
768:   } else if (flag == MAT_GLOBAL_SUM) {
769:     MPIU_Allreduce(isend,irecv,5,MPIU_PETSCLOGDOUBLE,MPI_SUM,PetscObjectComm((PetscObject)A));

771:     info->nz_used      = irecv[0];
772:     info->nz_allocated = irecv[1];
773:     info->nz_unneeded  = irecv[2];
774:     info->memory       = irecv[3];
775:     info->mallocs      = irecv[4];
776:   }
777:   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
778:   info->fill_ratio_needed = 0;
779:   info->factor_mallocs    = 0;
780:   return(0);
781: }

783: PetscErrorCode MatSetOption_MPIDense(Mat A,MatOption op,PetscBool flg)
784: {
785:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

789:   switch (op) {
790:   case MAT_NEW_NONZERO_LOCATIONS:
791:   case MAT_NEW_NONZERO_LOCATION_ERR:
792:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
793:     MatCheckPreallocated(A,1);
794:     MatSetOption(a->A,op,flg);
795:     break;
796:   case MAT_ROW_ORIENTED:
797:     MatCheckPreallocated(A,1);
798:     a->roworiented = flg;
799:     MatSetOption(a->A,op,flg);
800:     break;
801:   case MAT_FORCE_DIAGONAL_ENTRIES:
802:   case MAT_KEEP_NONZERO_PATTERN:
803:   case MAT_USE_HASH_TABLE:
804:   case MAT_SORTED_FULL:
805:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
806:     break;
807:   case MAT_IGNORE_OFF_PROC_ENTRIES:
808:     a->donotstash = flg;
809:     break;
810:   case MAT_SYMMETRIC:
811:   case MAT_STRUCTURALLY_SYMMETRIC:
812:   case MAT_HERMITIAN:
813:   case MAT_SYMMETRY_ETERNAL:
814:   case MAT_IGNORE_LOWER_TRIANGULAR:
815:   case MAT_IGNORE_ZERO_ENTRIES:
816:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
817:     break;
818:   default:
819:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %s",MatOptions[op]);
820:   }
821:   return(0);
822: }

824: PetscErrorCode MatDiagonalScale_MPIDense(Mat A,Vec ll,Vec rr)
825: {
826:   Mat_MPIDense      *mdn = (Mat_MPIDense*)A->data;
827:   const PetscScalar *l;
828:   PetscScalar       x,*v,*vv,*r;
829:   PetscErrorCode    ierr;
830:   PetscInt          i,j,s2a,s3a,s2,s3,m=mdn->A->rmap->n,n=mdn->A->cmap->n,lda;

833:   MatDenseGetArray(mdn->A,&vv);
834:   MatDenseGetLDA(mdn->A,&lda);
835:   MatGetLocalSize(A,&s2,&s3);
836:   if (ll) {
837:     VecGetLocalSize(ll,&s2a);
838:     if (s2a != s2) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector non-conforming local size, %D != %D", s2a, s2);
839:     VecGetArrayRead(ll,&l);
840:     for (i=0; i<m; i++) {
841:       x = l[i];
842:       v = vv + i;
843:       for (j=0; j<n; j++) { (*v) *= x; v+= lda;}
844:     }
845:     VecRestoreArrayRead(ll,&l);
846:     PetscLogFlops(1.0*n*m);
847:   }
848:   if (rr) {
849:     const PetscScalar *ar;

851:     VecGetLocalSize(rr,&s3a);
852:     if (s3a != s3) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vec non-conforming local size, %d != %d.", s3a, s3);
853:     VecGetArrayRead(rr,&ar);
854:     VecGetArray(mdn->lvec,&r);
855:     PetscSFBcastBegin(mdn->Mvctx,MPIU_SCALAR,ar,r,MPI_REPLACE);
856:     PetscSFBcastEnd(mdn->Mvctx,MPIU_SCALAR,ar,r,MPI_REPLACE);
857:     VecRestoreArrayRead(rr,&ar);
858:     for (i=0; i<n; i++) {
859:       x = r[i];
860:       v = vv + i*lda;
861:       for (j=0; j<m; j++) (*v++) *= x;
862:     }
863:     VecRestoreArray(mdn->lvec,&r);
864:     PetscLogFlops(1.0*n*m);
865:   }
866:   MatDenseRestoreArray(mdn->A,&vv);
867:   return(0);
868: }

870: PetscErrorCode MatNorm_MPIDense(Mat A,NormType type,PetscReal *nrm)
871: {
872:   Mat_MPIDense      *mdn = (Mat_MPIDense*)A->data;
873:   PetscErrorCode    ierr;
874:   PetscInt          i,j;
875:   PetscMPIInt       size;
876:   PetscReal         sum = 0.0;
877:   const PetscScalar *av,*v;

880:   MatDenseGetArrayRead(mdn->A,&av);
881:   v    = av;
882:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
883:   if (size == 1) {
884:     MatNorm(mdn->A,type,nrm);
885:   } else {
886:     if (type == NORM_FROBENIUS) {
887:       for (i=0; i<mdn->A->cmap->n*mdn->A->rmap->n; i++) {
888:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
889:       }
890:       MPIU_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));
891:       *nrm = PetscSqrtReal(*nrm);
892:       PetscLogFlops(2.0*mdn->A->cmap->n*mdn->A->rmap->n);
893:     } else if (type == NORM_1) {
894:       PetscReal *tmp,*tmp2;
895:       PetscCalloc2(A->cmap->N,&tmp,A->cmap->N,&tmp2);
896:       *nrm = 0.0;
897:       v    = av;
898:       for (j=0; j<mdn->A->cmap->n; j++) {
899:         for (i=0; i<mdn->A->rmap->n; i++) {
900:           tmp[j] += PetscAbsScalar(*v);  v++;
901:         }
902:       }
903:       MPIU_Allreduce(tmp,tmp2,A->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));
904:       for (j=0; j<A->cmap->N; j++) {
905:         if (tmp2[j] > *nrm) *nrm = tmp2[j];
906:       }
907:       PetscFree2(tmp,tmp2);
908:       PetscLogFlops(A->cmap->n*A->rmap->n);
909:     } else if (type == NORM_INFINITY) { /* max row norm */
910:       PetscReal ntemp;
911:       MatNorm(mdn->A,type,&ntemp);
912:       MPIU_Allreduce(&ntemp,nrm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));
913:     } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"No support for two norm");
914:   }
915:   MatDenseRestoreArrayRead(mdn->A,&av);
916:   return(0);
917: }

919: PetscErrorCode MatTranspose_MPIDense(Mat A,MatReuse reuse,Mat *matout)
920: {
921:   Mat_MPIDense   *a    = (Mat_MPIDense*)A->data;
922:   Mat            B;
923:   PetscInt       M = A->rmap->N,N = A->cmap->N,m,n,*rwork,rstart = A->rmap->rstart;
925:   PetscInt       j,i,lda;
926:   PetscScalar    *v;

929:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) {
930:     MatCreate(PetscObjectComm((PetscObject)A),&B);
931:     MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);
932:     MatSetType(B,((PetscObject)A)->type_name);
933:     MatMPIDenseSetPreallocation(B,NULL);
934:   } else B = *matout;

936:   m    = a->A->rmap->n; n = a->A->cmap->n;
937:   MatDenseGetArrayRead(a->A,(const PetscScalar**)&v);
938:   MatDenseGetLDA(a->A,&lda);
939:   PetscMalloc1(m,&rwork);
940:   for (i=0; i<m; i++) rwork[i] = rstart + i;
941:   for (j=0; j<n; j++) {
942:     MatSetValues(B,1,&j,m,rwork,v,INSERT_VALUES);
943:     v   += lda;
944:   }
945:   MatDenseRestoreArrayRead(a->A,(const PetscScalar**)&v);
946:   PetscFree(rwork);
947:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
948:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
949:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
950:     *matout = B;
951:   } else {
952:     MatHeaderMerge(A,&B);
953:   }
954:   return(0);
955: }

957: static PetscErrorCode MatDuplicate_MPIDense(Mat,MatDuplicateOption,Mat*);
958: PETSC_INTERN PetscErrorCode MatScale_MPIDense(Mat,PetscScalar);

960: PetscErrorCode MatSetUp_MPIDense(Mat A)
961: {

965:   PetscLayoutSetUp(A->rmap);
966:   PetscLayoutSetUp(A->cmap);
967:   if (!A->preallocated) {
968:     MatMPIDenseSetPreallocation(A,NULL);
969:   }
970:   return(0);
971: }

973: PetscErrorCode MatAXPY_MPIDense(Mat Y,PetscScalar alpha,Mat X,MatStructure str)
974: {
976:   Mat_MPIDense   *A = (Mat_MPIDense*)Y->data, *B = (Mat_MPIDense*)X->data;

979:   MatAXPY(A->A,alpha,B->A,str);
980:   return(0);
981: }

983: PetscErrorCode MatConjugate_MPIDense(Mat mat)
984: {
985:   Mat_MPIDense   *a = (Mat_MPIDense*)mat->data;

989:   MatConjugate(a->A);
990:   return(0);
991: }

993: PetscErrorCode MatRealPart_MPIDense(Mat A)
994: {
995:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

999:   MatRealPart(a->A);
1000:   return(0);
1001: }

1003: PetscErrorCode MatImaginaryPart_MPIDense(Mat A)
1004: {
1005:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

1009:   MatImaginaryPart(a->A);
1010:   return(0);
1011: }

1013: static PetscErrorCode MatGetColumnVector_MPIDense(Mat A,Vec v,PetscInt col)
1014: {
1016:   Mat_MPIDense   *a = (Mat_MPIDense*) A->data;

1019:   if (!a->A) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Missing local matrix");
1020:   if (!a->A->ops->getcolumnvector) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Missing get column operation");
1021:   (*a->A->ops->getcolumnvector)(a->A,v,col);
1022:   return(0);
1023: }

1025: PETSC_INTERN PetscErrorCode MatGetColumnReductions_SeqDense(Mat,PetscInt,PetscReal*);

1027: PetscErrorCode MatGetColumnReductions_MPIDense(Mat A,PetscInt type,PetscReal *reductions)
1028: {
1030:   PetscInt       i,m,n;
1031:   Mat_MPIDense   *a = (Mat_MPIDense*) A->data;
1032:   PetscReal      *work;

1035:   MatGetSize(A,&m,&n);
1036:   PetscMalloc1(n,&work);
1037:   if (type == REDUCTION_MEAN_REALPART) {
1038:     MatGetColumnReductions_SeqDense(a->A,(PetscInt)REDUCTION_SUM_REALPART,work);
1039:   } else if (type == REDUCTION_MEAN_IMAGINARYPART) {
1040:     MatGetColumnReductions_SeqDense(a->A,(PetscInt)REDUCTION_SUM_IMAGINARYPART,work);
1041:   } else {
1042:     MatGetColumnReductions_SeqDense(a->A,type,work);
1043:   }
1044:   if (type == NORM_2) {
1045:     for (i=0; i<n; i++) work[i] *= work[i];
1046:   }
1047:   if (type == NORM_INFINITY) {
1048:     MPIU_Allreduce(work,reductions,n,MPIU_REAL,MPIU_MAX,A->hdr.comm);
1049:   } else {
1050:     MPIU_Allreduce(work,reductions,n,MPIU_REAL,MPIU_SUM,A->hdr.comm);
1051:   }
1052:   PetscFree(work);
1053:   if (type == NORM_2) {
1054:     for (i=0; i<n; i++) reductions[i] = PetscSqrtReal(reductions[i]);
1055:   } else if (type == REDUCTION_MEAN_REALPART || type == REDUCTION_MEAN_IMAGINARYPART) {
1056:     for (i=0; i<n; i++) reductions[i] /= m;
1057:   }
1058:   return(0);
1059: }

1061: #if defined(PETSC_HAVE_CUDA)
1062: static PetscErrorCode MatDenseGetColumnVec_MPIDenseCUDA(Mat A,PetscInt col,Vec *v)
1063: {
1064:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;
1066:   PetscInt       lda;

1069:   if (a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
1070:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1071:   if (!a->cvec) {
1072:     VecCreateMPICUDAWithArray(PetscObjectComm((PetscObject)A),A->rmap->bs,A->rmap->n,A->rmap->N,NULL,&a->cvec);
1073:     PetscLogObjectParent((PetscObject)A,(PetscObject)a->cvec);
1074:   }
1075:   a->vecinuse = col + 1;
1076:   MatDenseGetLDA(a->A,&lda);
1077:   MatDenseCUDAGetArray(a->A,(PetscScalar**)&a->ptrinuse);
1078:   VecCUDAPlaceArray(a->cvec,a->ptrinuse + (size_t)col * (size_t)lda);
1079:   *v   = a->cvec;
1080:   return(0);
1081: }

1083: static PetscErrorCode MatDenseRestoreColumnVec_MPIDenseCUDA(Mat A,PetscInt col,Vec *v)
1084: {
1085:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

1089:   if (!a->vecinuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Need to call MatDenseGetColumnVec() first");
1090:   if (!a->cvec) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing internal column vector");
1091:   a->vecinuse = 0;
1092:   MatDenseCUDARestoreArray(a->A,(PetscScalar**)&a->ptrinuse);
1093:   VecCUDAResetArray(a->cvec);
1094:   *v   = NULL;
1095:   return(0);
1096: }

1098: static PetscErrorCode MatDenseGetColumnVecRead_MPIDenseCUDA(Mat A,PetscInt col,Vec *v)
1099: {
1100:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;
1101:   PetscInt       lda;

1105:   if (a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
1106:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1107:   if (!a->cvec) {
1108:     VecCreateMPICUDAWithArray(PetscObjectComm((PetscObject)A),A->rmap->bs,A->rmap->n,A->rmap->N,NULL,&a->cvec);
1109:     PetscLogObjectParent((PetscObject)A,(PetscObject)a->cvec);
1110:   }
1111:   a->vecinuse = col + 1;
1112:   MatDenseGetLDA(a->A,&lda);
1113:   MatDenseCUDAGetArrayRead(a->A,&a->ptrinuse);
1114:   VecCUDAPlaceArray(a->cvec,a->ptrinuse + (size_t)col * (size_t)lda);
1115:   VecLockReadPush(a->cvec);
1116:   *v   = a->cvec;
1117:   return(0);
1118: }

1120: static PetscErrorCode MatDenseRestoreColumnVecRead_MPIDenseCUDA(Mat A,PetscInt col,Vec *v)
1121: {
1122:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

1126:   if (!a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseGetColumnVec() first");
1127:   if (!a->cvec) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_PLIB,"Missing internal column vector");
1128:   a->vecinuse = 0;
1129:   MatDenseCUDARestoreArrayRead(a->A,&a->ptrinuse);
1130:   VecLockReadPop(a->cvec);
1131:   VecCUDAResetArray(a->cvec);
1132:   *v   = NULL;
1133:   return(0);
1134: }

1136: static PetscErrorCode MatDenseGetColumnVecWrite_MPIDenseCUDA(Mat A,PetscInt col,Vec *v)
1137: {
1138:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;
1140:   PetscInt       lda;

1143:   if (a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
1144:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1145:   if (!a->cvec) {
1146:     VecCreateMPICUDAWithArray(PetscObjectComm((PetscObject)A),A->rmap->bs,A->rmap->n,A->rmap->N,NULL,&a->cvec);
1147:     PetscLogObjectParent((PetscObject)A,(PetscObject)a->cvec);
1148:   }
1149:   a->vecinuse = col + 1;
1150:   MatDenseGetLDA(a->A,&lda);
1151:   MatDenseCUDAGetArrayWrite(a->A,(PetscScalar**)&a->ptrinuse);
1152:   VecCUDAPlaceArray(a->cvec,a->ptrinuse + (size_t)col * (size_t)lda);
1153:   *v   = a->cvec;
1154:   return(0);
1155: }

1157: static PetscErrorCode MatDenseRestoreColumnVecWrite_MPIDenseCUDA(Mat A,PetscInt col,Vec *v)
1158: {
1159:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

1163:   if (!a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseGetColumnVec() first");
1164:   if (!a->cvec) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_PLIB,"Missing internal column vector");
1165:   a->vecinuse = 0;
1166:   MatDenseCUDARestoreArrayWrite(a->A,(PetscScalar**)&a->ptrinuse);
1167:   VecCUDAResetArray(a->cvec);
1168:   *v   = NULL;
1169:   return(0);
1170: }

1172: static PetscErrorCode MatDenseCUDAPlaceArray_MPIDenseCUDA(Mat A, const PetscScalar *a)
1173: {
1174:   Mat_MPIDense   *l = (Mat_MPIDense*) A->data;

1178:   if (l->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
1179:   if (l->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1180:   MatDenseCUDAPlaceArray(l->A,a);
1181:   return(0);
1182: }

1184: static PetscErrorCode MatDenseCUDAResetArray_MPIDenseCUDA(Mat A)
1185: {
1186:   Mat_MPIDense   *l = (Mat_MPIDense*) A->data;

1190:   if (l->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
1191:   if (l->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1192:   MatDenseCUDAResetArray(l->A);
1193:   return(0);
1194: }

1196: static PetscErrorCode MatDenseCUDAReplaceArray_MPIDenseCUDA(Mat A, const PetscScalar *a)
1197: {
1198:   Mat_MPIDense   *l = (Mat_MPIDense*) A->data;

1202:   if (l->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
1203:   if (l->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1204:   MatDenseCUDAReplaceArray(l->A,a);
1205:   return(0);
1206: }

1208: static PetscErrorCode MatDenseCUDAGetArrayWrite_MPIDenseCUDA(Mat A, PetscScalar **a)
1209: {
1210:   Mat_MPIDense   *l = (Mat_MPIDense*) A->data;

1214:   MatDenseCUDAGetArrayWrite(l->A,a);
1215:   return(0);
1216: }

1218: static PetscErrorCode MatDenseCUDARestoreArrayWrite_MPIDenseCUDA(Mat A, PetscScalar **a)
1219: {
1220:   Mat_MPIDense   *l = (Mat_MPIDense*) A->data;

1224:   MatDenseCUDARestoreArrayWrite(l->A,a);
1225:   return(0);
1226: }

1228: static PetscErrorCode MatDenseCUDAGetArrayRead_MPIDenseCUDA(Mat A, const PetscScalar **a)
1229: {
1230:   Mat_MPIDense   *l = (Mat_MPIDense*) A->data;

1234:   MatDenseCUDAGetArrayRead(l->A,a);
1235:   return(0);
1236: }

1238: static PetscErrorCode MatDenseCUDARestoreArrayRead_MPIDenseCUDA(Mat A, const PetscScalar **a)
1239: {
1240:   Mat_MPIDense   *l = (Mat_MPIDense*) A->data;

1244:   MatDenseCUDARestoreArrayRead(l->A,a);
1245:   return(0);
1246: }

1248: static PetscErrorCode MatDenseCUDAGetArray_MPIDenseCUDA(Mat A, PetscScalar **a)
1249: {
1250:   Mat_MPIDense   *l = (Mat_MPIDense*) A->data;

1254:   MatDenseCUDAGetArray(l->A,a);
1255:   return(0);
1256: }

1258: static PetscErrorCode MatDenseCUDARestoreArray_MPIDenseCUDA(Mat A, PetscScalar **a)
1259: {
1260:   Mat_MPIDense   *l = (Mat_MPIDense*) A->data;

1264:   MatDenseCUDARestoreArray(l->A,a);
1265:   return(0);
1266: }

1268: static PetscErrorCode MatDenseGetColumnVecWrite_MPIDense(Mat,PetscInt,Vec*);
1269: static PetscErrorCode MatDenseGetColumnVecRead_MPIDense(Mat,PetscInt,Vec*);
1270: static PetscErrorCode MatDenseGetColumnVec_MPIDense(Mat,PetscInt,Vec*);
1271: static PetscErrorCode MatDenseRestoreColumnVecWrite_MPIDense(Mat,PetscInt,Vec*);
1272: static PetscErrorCode MatDenseRestoreColumnVecRead_MPIDense(Mat,PetscInt,Vec*);
1273: static PetscErrorCode MatDenseRestoreColumnVec_MPIDense(Mat,PetscInt,Vec*);
1274: static PetscErrorCode MatDenseRestoreSubMatrix_MPIDense(Mat,Mat*);

1276: static PetscErrorCode MatBindToCPU_MPIDenseCUDA(Mat mat,PetscBool bind)
1277: {
1278:   Mat_MPIDense   *d = (Mat_MPIDense*)mat->data;

1282:   if (d->vecinuse) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
1283:   if (d->matinuse) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1284:   if (d->A) {
1285:     MatBindToCPU(d->A,bind);
1286:   }
1287:   mat->boundtocpu = bind;
1288:   if (!bind) {
1289:     PetscBool iscuda;

1291:     PetscObjectTypeCompare((PetscObject)d->cvec,VECMPICUDA,&iscuda);
1292:     if (!iscuda) {
1293:       VecDestroy(&d->cvec);
1294:     }
1295:     PetscObjectTypeCompare((PetscObject)d->cmat,MATMPIDENSECUDA,&iscuda);
1296:     if (!iscuda) {
1297:       MatDestroy(&d->cmat);
1298:     }
1299:     PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumnVec_C",MatDenseGetColumnVec_MPIDenseCUDA);
1300:     PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumnVec_C",MatDenseRestoreColumnVec_MPIDenseCUDA);
1301:     PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumnVecRead_C",MatDenseGetColumnVecRead_MPIDenseCUDA);
1302:     PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumnVecRead_C",MatDenseRestoreColumnVecRead_MPIDenseCUDA);
1303:     PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumnVecWrite_C",MatDenseGetColumnVecWrite_MPIDenseCUDA);
1304:     PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumnVecWrite_C",MatDenseRestoreColumnVecWrite_MPIDenseCUDA);
1305:   } else {
1306:     PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumnVec_C",MatDenseGetColumnVec_MPIDense);
1307:     PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumnVec_C",MatDenseRestoreColumnVec_MPIDense);
1308:     PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumnVecRead_C",MatDenseGetColumnVecRead_MPIDense);
1309:     PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumnVecRead_C",MatDenseRestoreColumnVecRead_MPIDense);
1310:     PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumnVecWrite_C",MatDenseGetColumnVecWrite_MPIDense);
1311:     PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumnVecWrite_C",MatDenseRestoreColumnVecWrite_MPIDense);
1312:   }
1313:   if (d->cmat) {
1314:     MatBindToCPU(d->cmat,bind);
1315:   }
1316:   return(0);
1317: }

1319: PetscErrorCode MatMPIDenseCUDASetPreallocation(Mat A, PetscScalar *d_data)
1320: {
1321:   Mat_MPIDense   *d = (Mat_MPIDense*)A->data;
1323:   PetscBool      iscuda;

1327:   PetscObjectTypeCompare((PetscObject)A,MATMPIDENSECUDA,&iscuda);
1328:   if (!iscuda) return(0);
1329:   PetscLayoutSetUp(A->rmap);
1330:   PetscLayoutSetUp(A->cmap);
1331:   if (!d->A) {
1332:     MatCreate(PETSC_COMM_SELF,&d->A);
1333:     PetscLogObjectParent((PetscObject)A,(PetscObject)d->A);
1334:     MatSetSizes(d->A,A->rmap->n,A->cmap->N,A->rmap->n,A->cmap->N);
1335:   }
1336:   MatSetType(d->A,MATSEQDENSECUDA);
1337:   MatSeqDenseCUDASetPreallocation(d->A,d_data);
1338:   A->preallocated = PETSC_TRUE;
1339:   return(0);
1340: }
1341: #endif

1343: static PetscErrorCode MatSetRandom_MPIDense(Mat x,PetscRandom rctx)
1344: {
1345:   Mat_MPIDense   *d = (Mat_MPIDense*)x->data;

1349:   MatSetRandom(d->A,rctx);
1350:   return(0);
1351: }

1353: static PetscErrorCode MatMissingDiagonal_MPIDense(Mat A,PetscBool  *missing,PetscInt *d)
1354: {
1356:   *missing = PETSC_FALSE;
1357:   return(0);
1358: }

1360: static PetscErrorCode MatMatTransposeMultSymbolic_MPIDense_MPIDense(Mat,Mat,PetscReal,Mat);
1361: static PetscErrorCode MatMatTransposeMultNumeric_MPIDense_MPIDense(Mat,Mat,Mat);
1362: static PetscErrorCode MatTransposeMatMultNumeric_MPIDense_MPIDense(Mat,Mat,Mat);
1363: static PetscErrorCode MatTransposeMatMultSymbolic_MPIDense_MPIDense(Mat,Mat,PetscReal,Mat);
1364: static PetscErrorCode MatEqual_MPIDense(Mat,Mat,PetscBool*);
1365: static PetscErrorCode MatLoad_MPIDense(Mat,PetscViewer);

1367: /* -------------------------------------------------------------------*/
1368: static struct _MatOps MatOps_Values = { MatSetValues_MPIDense,
1369:                                         MatGetRow_MPIDense,
1370:                                         MatRestoreRow_MPIDense,
1371:                                         MatMult_MPIDense,
1372:                                 /*  4*/ MatMultAdd_MPIDense,
1373:                                         MatMultTranspose_MPIDense,
1374:                                         MatMultTransposeAdd_MPIDense,
1375:                                         NULL,
1376:                                         NULL,
1377:                                         NULL,
1378:                                 /* 10*/ NULL,
1379:                                         NULL,
1380:                                         NULL,
1381:                                         NULL,
1382:                                         MatTranspose_MPIDense,
1383:                                 /* 15*/ MatGetInfo_MPIDense,
1384:                                         MatEqual_MPIDense,
1385:                                         MatGetDiagonal_MPIDense,
1386:                                         MatDiagonalScale_MPIDense,
1387:                                         MatNorm_MPIDense,
1388:                                 /* 20*/ MatAssemblyBegin_MPIDense,
1389:                                         MatAssemblyEnd_MPIDense,
1390:                                         MatSetOption_MPIDense,
1391:                                         MatZeroEntries_MPIDense,
1392:                                 /* 24*/ MatZeroRows_MPIDense,
1393:                                         NULL,
1394:                                         NULL,
1395:                                         NULL,
1396:                                         NULL,
1397:                                 /* 29*/ MatSetUp_MPIDense,
1398:                                         NULL,
1399:                                         NULL,
1400:                                         MatGetDiagonalBlock_MPIDense,
1401:                                         NULL,
1402:                                 /* 34*/ MatDuplicate_MPIDense,
1403:                                         NULL,
1404:                                         NULL,
1405:                                         NULL,
1406:                                         NULL,
1407:                                 /* 39*/ MatAXPY_MPIDense,
1408:                                         MatCreateSubMatrices_MPIDense,
1409:                                         NULL,
1410:                                         MatGetValues_MPIDense,
1411:                                         MatCopy_MPIDense,
1412:                                 /* 44*/ NULL,
1413:                                         MatScale_MPIDense,
1414:                                         MatShift_Basic,
1415:                                         NULL,
1416:                                         NULL,
1417:                                 /* 49*/ MatSetRandom_MPIDense,
1418:                                         NULL,
1419:                                         NULL,
1420:                                         NULL,
1421:                                         NULL,
1422:                                 /* 54*/ NULL,
1423:                                         NULL,
1424:                                         NULL,
1425:                                         NULL,
1426:                                         NULL,
1427:                                 /* 59*/ MatCreateSubMatrix_MPIDense,
1428:                                         MatDestroy_MPIDense,
1429:                                         MatView_MPIDense,
1430:                                         NULL,
1431:                                         NULL,
1432:                                 /* 64*/ NULL,
1433:                                         NULL,
1434:                                         NULL,
1435:                                         NULL,
1436:                                         NULL,
1437:                                 /* 69*/ NULL,
1438:                                         NULL,
1439:                                         NULL,
1440:                                         NULL,
1441:                                         NULL,
1442:                                 /* 74*/ NULL,
1443:                                         NULL,
1444:                                         NULL,
1445:                                         NULL,
1446:                                         NULL,
1447:                                 /* 79*/ NULL,
1448:                                         NULL,
1449:                                         NULL,
1450:                                         NULL,
1451:                                 /* 83*/ MatLoad_MPIDense,
1452:                                         NULL,
1453:                                         NULL,
1454:                                         NULL,
1455:                                         NULL,
1456:                                         NULL,
1457:                                 /* 89*/ NULL,
1458:                                         NULL,
1459:                                         NULL,
1460:                                         NULL,
1461:                                         NULL,
1462:                                 /* 94*/ NULL,
1463:                                         NULL,
1464:                                         MatMatTransposeMultSymbolic_MPIDense_MPIDense,
1465:                                         MatMatTransposeMultNumeric_MPIDense_MPIDense,
1466:                                         NULL,
1467:                                 /* 99*/ MatProductSetFromOptions_MPIDense,
1468:                                         NULL,
1469:                                         NULL,
1470:                                         MatConjugate_MPIDense,
1471:                                         NULL,
1472:                                 /*104*/ NULL,
1473:                                         MatRealPart_MPIDense,
1474:                                         MatImaginaryPart_MPIDense,
1475:                                         NULL,
1476:                                         NULL,
1477:                                 /*109*/ NULL,
1478:                                         NULL,
1479:                                         NULL,
1480:                                         MatGetColumnVector_MPIDense,
1481:                                         MatMissingDiagonal_MPIDense,
1482:                                 /*114*/ NULL,
1483:                                         NULL,
1484:                                         NULL,
1485:                                         NULL,
1486:                                         NULL,
1487:                                 /*119*/ NULL,
1488:                                         NULL,
1489:                                         NULL,
1490:                                         NULL,
1491:                                         NULL,
1492:                                 /*124*/ NULL,
1493:                                         MatGetColumnReductions_MPIDense,
1494:                                         NULL,
1495:                                         NULL,
1496:                                         NULL,
1497:                                 /*129*/ NULL,
1498:                                         NULL,
1499:                                         MatTransposeMatMultSymbolic_MPIDense_MPIDense,
1500:                                         MatTransposeMatMultNumeric_MPIDense_MPIDense,
1501:                                         NULL,
1502:                                 /*134*/ NULL,
1503:                                         NULL,
1504:                                         NULL,
1505:                                         NULL,
1506:                                         NULL,
1507:                                 /*139*/ NULL,
1508:                                         NULL,
1509:                                         NULL,
1510:                                         NULL,
1511:                                         NULL,
1512:                                         MatCreateMPIMatConcatenateSeqMat_MPIDense,
1513:                                 /*145*/ NULL,
1514:                                         NULL,
1515:                                         NULL
1516: };

1518: PetscErrorCode  MatMPIDenseSetPreallocation_MPIDense(Mat mat,PetscScalar *data)
1519: {
1520:   Mat_MPIDense   *a = (Mat_MPIDense*)mat->data;
1521:   PetscBool      iscuda;

1525:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1526:   PetscLayoutSetUp(mat->rmap);
1527:   PetscLayoutSetUp(mat->cmap);
1528:   if (!a->A) {
1529:     MatCreate(PETSC_COMM_SELF,&a->A);
1530:     PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
1531:     MatSetSizes(a->A,mat->rmap->n,mat->cmap->N,mat->rmap->n,mat->cmap->N);
1532:   }
1533:   PetscObjectTypeCompare((PetscObject)mat,MATMPIDENSECUDA,&iscuda);
1534:   MatSetType(a->A,iscuda ? MATSEQDENSECUDA : MATSEQDENSE);
1535:   MatSeqDenseSetPreallocation(a->A,data);
1536:   mat->preallocated = PETSC_TRUE;
1537:   return(0);
1538: }

1540: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIDense(Mat A, MatType newtype,MatReuse reuse,Mat *newmat)
1541: {
1542:   Mat            B,C;

1546:   MatMPIAIJGetLocalMat(A,MAT_INITIAL_MATRIX,&C);
1547:   MatConvert_SeqAIJ_SeqDense(C,MATSEQDENSE,MAT_INITIAL_MATRIX,&B);
1548:   MatDestroy(&C);
1549:   if (reuse == MAT_REUSE_MATRIX) {
1550:     C = *newmat;
1551:   } else C = NULL;
1552:   MatCreateMPIMatConcatenateSeqMat(PetscObjectComm((PetscObject)A),B,A->cmap->n,!C?MAT_INITIAL_MATRIX:MAT_REUSE_MATRIX,&C);
1553:   MatDestroy(&B);
1554:   if (reuse == MAT_INPLACE_MATRIX) {
1555:     MatHeaderReplace(A,&C);
1556:   } else if (reuse == MAT_INITIAL_MATRIX) *newmat = C;
1557:   return(0);
1558: }

1560: PetscErrorCode MatConvert_MPIDense_MPIAIJ(Mat A, MatType newtype,MatReuse reuse,Mat *newmat)
1561: {
1562:   Mat            B,C;

1566:   MatDenseGetLocalMatrix(A,&C);
1567:   MatConvert_SeqDense_SeqAIJ(C,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);
1568:   if (reuse == MAT_REUSE_MATRIX) {
1569:     C = *newmat;
1570:   } else C = NULL;
1571:   MatCreateMPIMatConcatenateSeqMat(PetscObjectComm((PetscObject)A),B,A->cmap->n,!C?MAT_INITIAL_MATRIX:MAT_REUSE_MATRIX,&C);
1572:   MatDestroy(&B);
1573:   if (reuse == MAT_INPLACE_MATRIX) {
1574:     MatHeaderReplace(A,&C);
1575:   } else if (reuse == MAT_INITIAL_MATRIX) *newmat = C;
1576:   return(0);
1577: }

1579: #if defined(PETSC_HAVE_ELEMENTAL)
1580: PETSC_INTERN PetscErrorCode MatConvert_MPIDense_Elemental(Mat A, MatType newtype,MatReuse reuse,Mat *newmat)
1581: {
1582:   Mat            mat_elemental;
1584:   PetscScalar    *v;
1585:   PetscInt       m=A->rmap->n,N=A->cmap->N,rstart=A->rmap->rstart,i,*rows,*cols;

1588:   if (reuse == MAT_REUSE_MATRIX) {
1589:     mat_elemental = *newmat;
1590:     MatZeroEntries(*newmat);
1591:   } else {
1592:     MatCreate(PetscObjectComm((PetscObject)A), &mat_elemental);
1593:     MatSetSizes(mat_elemental,PETSC_DECIDE,PETSC_DECIDE,A->rmap->N,A->cmap->N);
1594:     MatSetType(mat_elemental,MATELEMENTAL);
1595:     MatSetUp(mat_elemental);
1596:     MatSetOption(mat_elemental,MAT_ROW_ORIENTED,PETSC_FALSE);
1597:   }

1599:   PetscMalloc2(m,&rows,N,&cols);
1600:   for (i=0; i<N; i++) cols[i] = i;
1601:   for (i=0; i<m; i++) rows[i] = rstart + i;

1603:   /* PETSc-Elemental interface uses axpy for setting off-processor entries, only ADD_VALUES is allowed */
1604:   MatDenseGetArray(A,&v);
1605:   MatSetValues(mat_elemental,m,rows,N,cols,v,ADD_VALUES);
1606:   MatAssemblyBegin(mat_elemental, MAT_FINAL_ASSEMBLY);
1607:   MatAssemblyEnd(mat_elemental, MAT_FINAL_ASSEMBLY);
1608:   MatDenseRestoreArray(A,&v);
1609:   PetscFree2(rows,cols);

1611:   if (reuse == MAT_INPLACE_MATRIX) {
1612:     MatHeaderReplace(A,&mat_elemental);
1613:   } else {
1614:     *newmat = mat_elemental;
1615:   }
1616:   return(0);
1617: }
1618: #endif

1620: static PetscErrorCode MatDenseGetColumn_MPIDense(Mat A,PetscInt col,PetscScalar **vals)
1621: {
1622:   Mat_MPIDense   *mat = (Mat_MPIDense*)A->data;

1626:   MatDenseGetColumn(mat->A,col,vals);
1627:   return(0);
1628: }

1630: static PetscErrorCode MatDenseRestoreColumn_MPIDense(Mat A,PetscScalar **vals)
1631: {
1632:   Mat_MPIDense   *mat = (Mat_MPIDense*)A->data;

1636:   MatDenseRestoreColumn(mat->A,vals);
1637:   return(0);
1638: }

1640: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPIDense(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
1641: {
1643:   Mat_MPIDense   *mat;
1644:   PetscInt       m,nloc,N;

1647:   MatGetSize(inmat,&m,&N);
1648:   MatGetLocalSize(inmat,NULL,&nloc);
1649:   if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
1650:     PetscInt sum;

1652:     if (n == PETSC_DECIDE) {
1653:       PetscSplitOwnership(comm,&n,&N);
1654:     }
1655:     /* Check sum(n) = N */
1656:     MPIU_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);
1657:     if (sum != N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns %D != global columns %D",sum,N);

1659:     MatCreateDense(comm,m,n,PETSC_DETERMINE,N,NULL,outmat);
1660:     MatSetOption(*outmat,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);
1661:   }

1663:   /* numeric phase */
1664:   mat = (Mat_MPIDense*)(*outmat)->data;
1665:   MatCopy(inmat,mat->A,SAME_NONZERO_PATTERN);
1666:   MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);
1667:   MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);
1668:   return(0);
1669: }

1671: #if defined(PETSC_HAVE_CUDA)
1672: PetscErrorCode MatConvert_MPIDenseCUDA_MPIDense(Mat M,MatType type,MatReuse reuse,Mat *newmat)
1673: {
1674:   Mat            B;
1675:   Mat_MPIDense   *m;

1679:   if (reuse == MAT_INITIAL_MATRIX) {
1680:     MatDuplicate(M,MAT_COPY_VALUES,newmat);
1681:   } else if (reuse == MAT_REUSE_MATRIX) {
1682:     MatCopy(M,*newmat,SAME_NONZERO_PATTERN);
1683:   }

1685:   B    = *newmat;
1686:   MatBindToCPU_MPIDenseCUDA(B,PETSC_TRUE);
1687:   PetscFree(B->defaultvectype);
1688:   PetscStrallocpy(VECSTANDARD,&B->defaultvectype);
1689:   PetscObjectChangeTypeName((PetscObject)B,MATMPIDENSE);
1690:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpidensecuda_mpidense_C",NULL);
1691:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_mpiaij_mpidensecuda_C",NULL);
1692:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_mpiaijcusparse_mpidensecuda_C",NULL);
1693:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_mpidensecuda_mpiaij_C",NULL);
1694:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_mpidensecuda_mpiaijcusparse_C",NULL);
1695:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDAGetArray_C",NULL);
1696:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDAGetArrayRead_C",NULL);
1697:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDAGetArrayWrite_C",NULL);
1698:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDARestoreArray_C",NULL);
1699:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDARestoreArrayRead_C",NULL);
1700:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDARestoreArrayWrite_C",NULL);
1701:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDAPlaceArray_C",NULL);
1702:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDAResetArray_C",NULL);
1703:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDAReplaceArray_C",NULL);
1704:   m    = (Mat_MPIDense*)(B)->data;
1705:   if (m->A) {
1706:     MatConvert(m->A,MATSEQDENSE,MAT_INPLACE_MATRIX,&m->A);
1707:     MatSetUpMultiply_MPIDense(B);
1708:   }
1709:   B->ops->bindtocpu = NULL;
1710:   B->offloadmask    = PETSC_OFFLOAD_CPU;
1711:   return(0);
1712: }

1714: PetscErrorCode MatConvert_MPIDense_MPIDenseCUDA(Mat M,MatType type,MatReuse reuse,Mat *newmat)
1715: {
1716:   Mat            B;
1717:   Mat_MPIDense   *m;

1721:   if (reuse == MAT_INITIAL_MATRIX) {
1722:     MatDuplicate(M,MAT_COPY_VALUES,newmat);
1723:   } else if (reuse == MAT_REUSE_MATRIX) {
1724:     MatCopy(M,*newmat,SAME_NONZERO_PATTERN);
1725:   }

1727:   B    = *newmat;
1728:   PetscFree(B->defaultvectype);
1729:   PetscStrallocpy(VECCUDA,&B->defaultvectype);
1730:   PetscObjectChangeTypeName((PetscObject)B,MATMPIDENSECUDA);
1731:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpidensecuda_mpidense_C",                    MatConvert_MPIDenseCUDA_MPIDense);
1732:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_mpiaij_mpidensecuda_C",        MatProductSetFromOptions_MPIAIJ_MPIDense);
1733:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_mpiaijcusparse_mpidensecuda_C",MatProductSetFromOptions_MPIAIJ_MPIDense);
1734:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_mpidensecuda_mpiaij_C",        MatProductSetFromOptions_MPIDense_MPIAIJ);
1735:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_mpidensecuda_mpiaijcusparse_C",MatProductSetFromOptions_MPIDense_MPIAIJ);
1736:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDAGetArray_C",                                MatDenseCUDAGetArray_MPIDenseCUDA);
1737:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDAGetArrayRead_C",                            MatDenseCUDAGetArrayRead_MPIDenseCUDA);
1738:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDAGetArrayWrite_C",                           MatDenseCUDAGetArrayWrite_MPIDenseCUDA);
1739:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDARestoreArray_C",                            MatDenseCUDARestoreArray_MPIDenseCUDA);
1740:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDARestoreArrayRead_C",                        MatDenseCUDARestoreArrayRead_MPIDenseCUDA);
1741:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDARestoreArrayWrite_C",                       MatDenseCUDARestoreArrayWrite_MPIDenseCUDA);
1742:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDAPlaceArray_C",                              MatDenseCUDAPlaceArray_MPIDenseCUDA);
1743:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDAResetArray_C",                              MatDenseCUDAResetArray_MPIDenseCUDA);
1744:   PetscObjectComposeFunction((PetscObject)B,"MatDenseCUDAReplaceArray_C",                            MatDenseCUDAReplaceArray_MPIDenseCUDA);
1745:   m    = (Mat_MPIDense*)(B->data);
1746:   if (m->A) {
1747:     MatConvert(m->A,MATSEQDENSECUDA,MAT_INPLACE_MATRIX,&m->A);
1748:     MatSetUpMultiply_MPIDense(B);
1749:     B->offloadmask = PETSC_OFFLOAD_BOTH;
1750:   } else {
1751:     B->offloadmask = PETSC_OFFLOAD_UNALLOCATED;
1752:   }
1753:   MatBindToCPU_MPIDenseCUDA(B,PETSC_FALSE);

1755:   B->ops->bindtocpu = MatBindToCPU_MPIDenseCUDA;
1756:   return(0);
1757: }
1758: #endif

1760: PetscErrorCode MatDenseGetColumnVec_MPIDense(Mat A,PetscInt col,Vec *v)
1761: {
1762:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;
1764:   PetscInt       lda;

1767:   if (a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
1768:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1769:   if (!a->cvec) {
1770:     VecCreateMPIWithArray(PetscObjectComm((PetscObject)A),A->rmap->bs,A->rmap->n,A->rmap->N,NULL,&a->cvec);
1771:   }
1772:   a->vecinuse = col + 1;
1773:   MatDenseGetLDA(a->A,&lda);
1774:   MatDenseGetArray(a->A,(PetscScalar**)&a->ptrinuse);
1775:   VecPlaceArray(a->cvec,a->ptrinuse + (size_t)col * (size_t)lda);
1776:   *v   = a->cvec;
1777:   return(0);
1778: }

1780: PetscErrorCode MatDenseRestoreColumnVec_MPIDense(Mat A,PetscInt col,Vec *v)
1781: {
1782:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

1786:   if (!a->vecinuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Need to call MatDenseGetColumnVec() first");
1787:   if (!a->cvec) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing internal column vector");
1788:   a->vecinuse = 0;
1789:   MatDenseRestoreArray(a->A,(PetscScalar**)&a->ptrinuse);
1790:   VecResetArray(a->cvec);
1791:   *v   = NULL;
1792:   return(0);
1793: }

1795: PetscErrorCode MatDenseGetColumnVecRead_MPIDense(Mat A,PetscInt col,Vec *v)
1796: {
1797:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;
1799:   PetscInt       lda;

1802:   if (a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
1803:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1804:   if (!a->cvec) {
1805:     VecCreateMPIWithArray(PetscObjectComm((PetscObject)A),A->rmap->bs,A->rmap->n,A->rmap->N,NULL,&a->cvec);
1806:   }
1807:   a->vecinuse = col + 1;
1808:   MatDenseGetLDA(a->A,&lda);
1809:   MatDenseGetArrayRead(a->A,&a->ptrinuse);
1810:   VecPlaceArray(a->cvec,a->ptrinuse + (size_t)col * (size_t)lda);
1811:   VecLockReadPush(a->cvec);
1812:   *v   = a->cvec;
1813:   return(0);
1814: }

1816: PetscErrorCode MatDenseRestoreColumnVecRead_MPIDense(Mat A,PetscInt col,Vec *v)
1817: {
1818:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

1822:   if (!a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseGetColumnVec() first");
1823:   if (!a->cvec) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_PLIB,"Missing internal column vector");
1824:   a->vecinuse = 0;
1825:   MatDenseRestoreArrayRead(a->A,&a->ptrinuse);
1826:   VecLockReadPop(a->cvec);
1827:   VecResetArray(a->cvec);
1828:   *v   = NULL;
1829:   return(0);
1830: }

1832: PetscErrorCode MatDenseGetColumnVecWrite_MPIDense(Mat A,PetscInt col,Vec *v)
1833: {
1834:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;
1836:   PetscInt       lda;

1839:   if (a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
1840:   if (a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1841:   if (!a->cvec) {
1842:     VecCreateMPIWithArray(PetscObjectComm((PetscObject)A),A->rmap->bs,A->rmap->n,A->rmap->N,NULL,&a->cvec);
1843:   }
1844:   a->vecinuse = col + 1;
1845:   MatDenseGetLDA(a->A,&lda);
1846:   MatDenseGetArrayWrite(a->A,(PetscScalar**)&a->ptrinuse);
1847:   VecPlaceArray(a->cvec,a->ptrinuse + (size_t)col * (size_t)lda);
1848:   *v   = a->cvec;
1849:   return(0);
1850: }

1852: PetscErrorCode MatDenseRestoreColumnVecWrite_MPIDense(Mat A,PetscInt col,Vec *v)
1853: {
1854:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;

1858:   if (!a->vecinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseGetColumnVec() first");
1859:   if (!a->cvec) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_PLIB,"Missing internal column vector");
1860:   a->vecinuse = 0;
1861:   MatDenseRestoreArrayWrite(a->A,(PetscScalar**)&a->ptrinuse);
1862:   VecResetArray(a->cvec);
1863:   *v   = NULL;
1864:   return(0);
1865: }

1867: PetscErrorCode MatDenseGetSubMatrix_MPIDense(Mat A,PetscInt cbegin,PetscInt cend,Mat *v)
1868: {
1869:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;
1870:   Mat_MPIDense   *c;
1872:   MPI_Comm       comm;
1873:   PetscBool      setup = PETSC_FALSE;

1876:   PetscObjectGetComm((PetscObject)A,&comm);
1877:   if (a->vecinuse) SETERRQ(comm,PETSC_ERR_ORDER,"Need to call MatDenseRestoreColumnVec() first");
1878:   if (a->matinuse) SETERRQ(comm,PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1879:   if (!a->cmat) {
1880:     setup = PETSC_TRUE;
1881:     MatCreate(comm,&a->cmat);
1882:     PetscLogObjectParent((PetscObject)A,(PetscObject)a->cmat);
1883:     MatSetType(a->cmat,((PetscObject)A)->type_name);
1884:     PetscLayoutReference(A->rmap,&a->cmat->rmap);
1885:     PetscLayoutSetSize(a->cmat->cmap,cend-cbegin);
1886:     PetscLayoutSetUp(a->cmat->cmap);
1887:   } else if (cend-cbegin != a->cmat->cmap->N) {
1888:     setup = PETSC_TRUE;
1889:     PetscLayoutDestroy(&a->cmat->cmap);
1890:     PetscLayoutCreate(comm,&a->cmat->cmap);
1891:     PetscLayoutSetSize(a->cmat->cmap,cend-cbegin);
1892:     PetscLayoutSetUp(a->cmat->cmap);
1893:   }
1894:   c = (Mat_MPIDense*)a->cmat->data;
1895:   if (c->A) SETERRQ(comm,PETSC_ERR_ORDER,"Need to call MatDenseRestoreSubMatrix() first");
1896:   MatDenseGetSubMatrix(a->A,cbegin,cend,&c->A);
1897:   if (setup) { /* do we really need this? */
1898:     MatSetUpMultiply_MPIDense(a->cmat);
1899:   }
1900:   a->cmat->preallocated = PETSC_TRUE;
1901:   a->cmat->assembled = PETSC_TRUE;
1902:   a->matinuse = cbegin + 1;
1903:   *v = a->cmat;
1904:   return(0);
1905: }

1907: PetscErrorCode MatDenseRestoreSubMatrix_MPIDense(Mat A,Mat *v)
1908: {
1909:   Mat_MPIDense   *a = (Mat_MPIDense*)A->data;
1910:   Mat_MPIDense   *c;

1914:   if (!a->matinuse) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ORDER,"Need to call MatDenseGetSubMatrix() first");
1915:   if (!a->cmat) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_PLIB,"Missing internal matrix");
1916:   if (*v != a->cmat) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not the matrix obtained from MatDenseGetSubMatrix()");
1917:   a->matinuse = 0;
1918:   c    = (Mat_MPIDense*)a->cmat->data;
1919:   MatDenseRestoreSubMatrix(a->A,&c->A);
1920:   *v   = NULL;
1921:   return(0);
1922: }

1924: /*MC
1925:    MATMPIDENSE - MATMPIDENSE = "mpidense" - A matrix type to be used for distributed dense matrices.

1927:    Options Database Keys:
1928: . -mat_type mpidense - sets the matrix type to "mpidense" during a call to MatSetFromOptions()

1930:   Level: beginner

1932: .seealso: MatCreateDense()

1934: M*/
1935: PETSC_EXTERN PetscErrorCode MatCreate_MPIDense(Mat mat)
1936: {
1937:   Mat_MPIDense   *a;

1941:   PetscNewLog(mat,&a);
1942:   mat->data = (void*)a;
1943:   PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));

1945:   mat->insertmode = NOT_SET_VALUES;

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

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

1952:   /* stuff used for matrix vector multiply */
1953:   a->lvec        = NULL;
1954:   a->Mvctx       = NULL;
1955:   a->roworiented = PETSC_TRUE;

1957:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetLDA_C",MatDenseGetLDA_MPIDense);
1958:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseSetLDA_C",MatDenseSetLDA_MPIDense);
1959:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetArray_C",MatDenseGetArray_MPIDense);
1960:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreArray_C",MatDenseRestoreArray_MPIDense);
1961:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetArrayRead_C",MatDenseGetArrayRead_MPIDense);
1962:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreArrayRead_C",MatDenseRestoreArrayRead_MPIDense);
1963:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetArrayWrite_C",MatDenseGetArrayWrite_MPIDense);
1964:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreArrayWrite_C",MatDenseRestoreArrayWrite_MPIDense);
1965:   PetscObjectComposeFunction((PetscObject)mat,"MatDensePlaceArray_C",MatDensePlaceArray_MPIDense);
1966:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseResetArray_C",MatDenseResetArray_MPIDense);
1967:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseReplaceArray_C",MatDenseReplaceArray_MPIDense);
1968:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumnVec_C",MatDenseGetColumnVec_MPIDense);
1969:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumnVec_C",MatDenseRestoreColumnVec_MPIDense);
1970:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumnVecRead_C",MatDenseGetColumnVecRead_MPIDense);
1971:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumnVecRead_C",MatDenseRestoreColumnVecRead_MPIDense);
1972:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumnVecWrite_C",MatDenseGetColumnVecWrite_MPIDense);
1973:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumnVecWrite_C",MatDenseRestoreColumnVecWrite_MPIDense);
1974:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetSubMatrix_C",MatDenseGetSubMatrix_MPIDense);
1975:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreSubMatrix_C",MatDenseRestoreSubMatrix_MPIDense);
1976:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpidense_C",MatConvert_MPIAIJ_MPIDense);
1977:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpidense_mpiaij_C",MatConvert_MPIDense_MPIAIJ);
1978: #if defined(PETSC_HAVE_ELEMENTAL)
1979:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpidense_elemental_C",MatConvert_MPIDense_Elemental);
1980: #endif
1981: #if defined(PETSC_HAVE_SCALAPACK)
1982:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpidense_scalapack_C",MatConvert_Dense_ScaLAPACK);
1983: #endif
1984: #if defined(PETSC_HAVE_CUDA)
1985:   PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpidense_mpidensecuda_C",MatConvert_MPIDense_MPIDenseCUDA);
1986: #endif
1987:   PetscObjectComposeFunction((PetscObject)mat,"MatMPIDenseSetPreallocation_C",MatMPIDenseSetPreallocation_MPIDense);
1988:   PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_mpiaij_mpidense_C",MatProductSetFromOptions_MPIAIJ_MPIDense);
1989:   PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_mpidense_mpiaij_C",MatProductSetFromOptions_MPIDense_MPIAIJ);
1990: #if defined(PETSC_HAVE_CUDA)
1991:   PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_mpiaijcusparse_mpidense_C",MatProductSetFromOptions_MPIAIJ_MPIDense);
1992:   PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_mpidense_mpiaijcusparse_C",MatProductSetFromOptions_MPIDense_MPIAIJ);
1993: #endif

1995:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetColumn_C",MatDenseGetColumn_MPIDense);
1996:   PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreColumn_C",MatDenseRestoreColumn_MPIDense);
1997:   PetscObjectChangeTypeName((PetscObject)mat,MATMPIDENSE);
1998:   return(0);
1999: }

2001: /*MC
2002:    MATMPIDENSECUDA - MATMPIDENSECUDA = "mpidensecuda" - A matrix type to be used for distributed dense matrices on GPUs.

2004:    Options Database Keys:
2005: . -mat_type mpidensecuda - sets the matrix type to "mpidensecuda" during a call to MatSetFromOptions()

2007:   Level: beginner

2009: .seealso:

2011: M*/
2012: #if defined(PETSC_HAVE_CUDA)
2013: PETSC_EXTERN PetscErrorCode MatCreate_MPIDenseCUDA(Mat B)
2014: {

2018:   PetscCUDAInitializeCheck();
2019:   MatCreate_MPIDense(B);
2020:   MatConvert_MPIDense_MPIDenseCUDA(B,MATMPIDENSECUDA,MAT_INPLACE_MATRIX,&B);
2021:   return(0);
2022: }
2023: #endif

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

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

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

2034:   Level: beginner

2036: .seealso: MATSEQDENSE,MATMPIDENSE,MATDENSECUDA
2037: M*/

2039: /*MC
2040:    MATDENSECUDA - MATDENSECUDA = "densecuda" - A matrix type to be used for dense matrices on GPUs.

2042:    This matrix type is identical to MATSEQDENSECUDA when constructed with a single process communicator,
2043:    and MATMPIDENSECUDA otherwise.

2045:    Options Database Keys:
2046: . -mat_type densecuda - sets the matrix type to "densecuda" during a call to MatSetFromOptions()

2048:   Level: beginner

2050: .seealso: MATSEQDENSECUDA,MATMPIDENSECUDA,MATDENSE
2051: M*/

2053: /*@C
2054:    MatMPIDenseSetPreallocation - Sets the array used to store the matrix entries

2056:    Collective

2058:    Input Parameters:
2059: .  B - the matrix
2060: -  data - optional location of matrix data.  Set data=NULL for PETSc
2061:    to control all matrix memory allocation.

2063:    Notes:
2064:    The dense format is fully compatible with standard Fortran 77
2065:    storage by columns.

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

2071:    Level: intermediate

2073: .seealso: MatCreate(), MatCreateSeqDense(), MatSetValues()
2074: @*/
2075: PetscErrorCode  MatMPIDenseSetPreallocation(Mat B,PetscScalar *data)
2076: {

2081:   PetscTryMethod(B,"MatMPIDenseSetPreallocation_C",(Mat,PetscScalar*),(B,data));
2082:   return(0);
2083: }

2085: /*@
2086:    MatDensePlaceArray - Allows one to replace the array in a dense matrix with an
2087:    array provided by the user. This is useful to avoid copying an array
2088:    into a matrix

2090:    Not Collective

2092:    Input Parameters:
2093: +  mat - the matrix
2094: -  array - the array in column major order

2096:    Notes:
2097:    You can return to the original array with a call to MatDenseResetArray(). The user is responsible for freeing this array; it will not be
2098:    freed when the matrix is destroyed.

2100:    Level: developer

2102: .seealso: MatDenseGetArray(), MatDenseResetArray(), VecPlaceArray(), VecGetArray(), VecRestoreArray(), VecReplaceArray(), VecResetArray()

2104: @*/
2105: PetscErrorCode  MatDensePlaceArray(Mat mat,const PetscScalar *array)
2106: {

2111:   PetscUseMethod(mat,"MatDensePlaceArray_C",(Mat,const PetscScalar*),(mat,array));
2112:   PetscObjectStateIncrease((PetscObject)mat);
2113: #if defined(PETSC_HAVE_CUDA)
2114:   mat->offloadmask = PETSC_OFFLOAD_CPU;
2115: #endif
2116:   return(0);
2117: }

2119: /*@
2120:    MatDenseResetArray - Resets the matrix array to that it previously had before the call to MatDensePlaceArray()

2122:    Not Collective

2124:    Input Parameters:
2125: .  mat - the matrix

2127:    Notes:
2128:    You can only call this after a call to MatDensePlaceArray()

2130:    Level: developer

2132: .seealso: MatDenseGetArray(), MatDensePlaceArray(), VecPlaceArray(), VecGetArray(), VecRestoreArray(), VecReplaceArray(), VecResetArray()

2134: @*/
2135: PetscErrorCode  MatDenseResetArray(Mat mat)
2136: {

2141:   PetscUseMethod(mat,"MatDenseResetArray_C",(Mat),(mat));
2142:   PetscObjectStateIncrease((PetscObject)mat);
2143:   return(0);
2144: }

2146: /*@
2147:    MatDenseReplaceArray - Allows one to replace the array in a dense matrix with an
2148:    array provided by the user. This is useful to avoid copying an array
2149:    into a matrix

2151:    Not Collective

2153:    Input Parameters:
2154: +  mat - the matrix
2155: -  array - the array in column major order

2157:    Notes:
2158:    The memory passed in MUST be obtained with PetscMalloc() and CANNOT be
2159:    freed by the user. It will be freed when the matrix is destroyed.

2161:    Level: developer

2163: .seealso: MatDenseGetArray(), VecReplaceArray()
2164: @*/
2165: PetscErrorCode  MatDenseReplaceArray(Mat mat,const PetscScalar *array)
2166: {

2171:   PetscUseMethod(mat,"MatDenseReplaceArray_C",(Mat,const PetscScalar*),(mat,array));
2172:   PetscObjectStateIncrease((PetscObject)mat);
2173: #if defined(PETSC_HAVE_CUDA)
2174:   mat->offloadmask = PETSC_OFFLOAD_CPU;
2175: #endif
2176:   return(0);
2177: }

2179: #if defined(PETSC_HAVE_CUDA)
2180: /*@C
2181:    MatDenseCUDAPlaceArray - Allows one to replace the GPU array in a dense matrix with an
2182:    array provided by the user. This is useful to avoid copying an array
2183:    into a matrix

2185:    Not Collective

2187:    Input Parameters:
2188: +  mat - the matrix
2189: -  array - the array in column major order

2191:    Notes:
2192:    You can return to the original array with a call to MatDenseCUDAResetArray(). The user is responsible for freeing this array; it will not be
2193:    freed when the matrix is destroyed. The array must have been allocated with cudaMalloc().

2195:    Level: developer

2197: .seealso: MatDenseCUDAGetArray(), MatDenseCUDAResetArray()
2198: @*/
2199: PetscErrorCode  MatDenseCUDAPlaceArray(Mat mat,const PetscScalar *array)
2200: {

2205:   PetscUseMethod(mat,"MatDenseCUDAPlaceArray_C",(Mat,const PetscScalar*),(mat,array));
2206:   PetscObjectStateIncrease((PetscObject)mat);
2207:   mat->offloadmask = PETSC_OFFLOAD_GPU;
2208:   return(0);
2209: }

2211: /*@C
2212:    MatDenseCUDAResetArray - Resets the matrix array to that it previously had before the call to MatDenseCUDAPlaceArray()

2214:    Not Collective

2216:    Input Parameters:
2217: .  mat - the matrix

2219:    Notes:
2220:    You can only call this after a call to MatDenseCUDAPlaceArray()

2222:    Level: developer

2224: .seealso: MatDenseCUDAGetArray(), MatDenseCUDAPlaceArray()

2226: @*/
2227: PetscErrorCode  MatDenseCUDAResetArray(Mat mat)
2228: {

2233:   PetscUseMethod(mat,"MatDenseCUDAResetArray_C",(Mat),(mat));
2234:   PetscObjectStateIncrease((PetscObject)mat);
2235:   return(0);
2236: }

2238: /*@C
2239:    MatDenseCUDAReplaceArray - Allows one to replace the GPU array in a dense matrix with an
2240:    array provided by the user. This is useful to avoid copying an array
2241:    into a matrix

2243:    Not Collective

2245:    Input Parameters:
2246: +  mat - the matrix
2247: -  array - the array in column major order

2249:    Notes:
2250:    This permanently replaces the GPU array and frees the memory associated with the old GPU array.
2251:    The memory passed in CANNOT be freed by the user. It will be freed
2252:    when the matrix is destroyed. The array should respect the matrix leading dimension.

2254:    Level: developer

2256: .seealso: MatDenseCUDAGetArray(), MatDenseCUDAPlaceArray(), MatDenseCUDAResetArray()
2257: @*/
2258: PetscErrorCode  MatDenseCUDAReplaceArray(Mat mat,const PetscScalar *array)
2259: {

2264:   PetscUseMethod(mat,"MatDenseCUDAReplaceArray_C",(Mat,const PetscScalar*),(mat,array));
2265:   PetscObjectStateIncrease((PetscObject)mat);
2266:   mat->offloadmask = PETSC_OFFLOAD_GPU;
2267:   return(0);
2268: }

2270: /*@C
2271:    MatDenseCUDAGetArrayWrite - Provides write access to the CUDA buffer inside a dense matrix.

2273:    Not Collective

2275:    Input Parameters:
2276: .  A - the matrix

2278:    Output Parameters
2279: .  array - the GPU array in column major order

2281:    Notes:
2282:    The data on the GPU may not be updated due to operations done on the CPU. If you need updated data, use MatDenseCUDAGetArray(). The array must be restored with MatDenseCUDARestoreArrayWrite() when no longer needed.

2284:    Level: developer

2286: .seealso: MatDenseCUDAGetArray(), MatDenseCUDARestoreArray(), MatDenseCUDARestoreArrayWrite(), MatDenseCUDAGetArrayRead(), MatDenseCUDARestoreArrayRead()
2287: @*/
2288: PetscErrorCode MatDenseCUDAGetArrayWrite(Mat A, PetscScalar **a)
2289: {

2294:   PetscUseMethod(A,"MatDenseCUDAGetArrayWrite_C",(Mat,PetscScalar**),(A,a));
2295:   PetscObjectStateIncrease((PetscObject)A);
2296:   return(0);
2297: }

2299: /*@C
2300:    MatDenseCUDARestoreArrayWrite - Restore write access to the CUDA buffer inside a dense matrix previously obtained with MatDenseCUDAGetArrayWrite().

2302:    Not Collective

2304:    Input Parameters:
2305: +  A - the matrix
2306: -  array - the GPU array in column major order

2308:    Notes:

2310:    Level: developer

2312: .seealso: MatDenseCUDAGetArray(), MatDenseCUDARestoreArray(), MatDenseCUDAGetArrayWrite(), MatDenseCUDARestoreArrayRead(), MatDenseCUDAGetArrayRead()
2313: @*/
2314: PetscErrorCode MatDenseCUDARestoreArrayWrite(Mat A, PetscScalar **a)
2315: {

2320:   PetscUseMethod(A,"MatDenseCUDARestoreArrayWrite_C",(Mat,PetscScalar**),(A,a));
2321:   PetscObjectStateIncrease((PetscObject)A);
2322:   A->offloadmask = PETSC_OFFLOAD_GPU;
2323:   return(0);
2324: }

2326: /*@C
2327:    MatDenseCUDAGetArrayRead - Provides read-only access to the CUDA buffer inside a dense matrix. The array must be restored with MatDenseCUDARestoreArrayRead() when no longer needed.

2329:    Not Collective

2331:    Input Parameters:
2332: .  A - the matrix

2334:    Output Parameters
2335: .  array - the GPU array in column major order

2337:    Notes:
2338:    Data can be copied to the GPU due to operations done on the CPU. If you need write only access, use MatDenseCUDAGetArrayWrite().

2340:    Level: developer

2342: .seealso: MatDenseCUDAGetArray(), MatDenseCUDARestoreArray(), MatDenseCUDARestoreArrayWrite(), MatDenseCUDAGetArrayWrite(), MatDenseCUDARestoreArrayRead()
2343: @*/
2344: PetscErrorCode MatDenseCUDAGetArrayRead(Mat A, const PetscScalar **a)
2345: {

2350:   PetscUseMethod(A,"MatDenseCUDAGetArrayRead_C",(Mat,const PetscScalar**),(A,a));
2351:   return(0);
2352: }

2354: /*@C
2355:    MatDenseCUDARestoreArrayRead - Restore read-only access to the CUDA buffer inside a dense matrix previously obtained with a call to MatDenseCUDAGetArrayRead().

2357:    Not Collective

2359:    Input Parameters:
2360: +  A - the matrix
2361: -  array - the GPU array in column major order

2363:    Notes:
2364:    Data can be copied to the GPU due to operations done on the CPU. If you need write only access, use MatDenseCUDAGetArrayWrite().

2366:    Level: developer

2368: .seealso: MatDenseCUDAGetArray(), MatDenseCUDARestoreArray(), MatDenseCUDARestoreArrayWrite(), MatDenseCUDAGetArrayWrite(), MatDenseCUDAGetArrayRead()
2369: @*/
2370: PetscErrorCode MatDenseCUDARestoreArrayRead(Mat A, const PetscScalar **a)
2371: {

2375:   PetscUseMethod(A,"MatDenseCUDARestoreArrayRead_C",(Mat,const PetscScalar**),(A,a));
2376:   return(0);
2377: }

2379: /*@C
2380:    MatDenseCUDAGetArray - Provides access to the CUDA buffer inside a dense matrix. The array must be restored with MatDenseCUDARestoreArray() when no longer needed.

2382:    Not Collective

2384:    Input Parameters:
2385: .  A - the matrix

2387:    Output Parameters
2388: .  array - the GPU array in column major order

2390:    Notes:
2391:    Data can be copied to the GPU due to operations done on the CPU. If you need write only access, use MatDenseCUDAGetArrayWrite(). For read-only access, use MatDenseCUDAGetArrayRead().

2393:    Level: developer

2395: .seealso: MatDenseCUDAGetArrayRead(), MatDenseCUDARestoreArray(), MatDenseCUDARestoreArrayWrite(), MatDenseCUDAGetArrayWrite(), MatDenseCUDARestoreArrayRead()
2396: @*/
2397: PetscErrorCode MatDenseCUDAGetArray(Mat A, PetscScalar **a)
2398: {

2403:   PetscUseMethod(A,"MatDenseCUDAGetArray_C",(Mat,PetscScalar**),(A,a));
2404:   PetscObjectStateIncrease((PetscObject)A);
2405:   return(0);
2406: }

2408: /*@C
2409:    MatDenseCUDARestoreArray - Restore access to the CUDA buffer inside a dense matrix previously obtained with MatDenseCUDAGetArray().

2411:    Not Collective

2413:    Input Parameters:
2414: +  A - the matrix
2415: -  array - the GPU array in column major order

2417:    Notes:

2419:    Level: developer

2421: .seealso: MatDenseCUDAGetArray(), MatDenseCUDARestoreArrayWrite(), MatDenseCUDAGetArrayWrite(), MatDenseCUDARestoreArrayRead(), MatDenseCUDAGetArrayRead()
2422: @*/
2423: PetscErrorCode MatDenseCUDARestoreArray(Mat A, PetscScalar **a)
2424: {

2429:   PetscUseMethod(A,"MatDenseCUDARestoreArray_C",(Mat,PetscScalar**),(A,a));
2430:   PetscObjectStateIncrease((PetscObject)A);
2431:   A->offloadmask = PETSC_OFFLOAD_GPU;
2432:   return(0);
2433: }
2434: #endif

2436: /*@C
2437:    MatCreateDense - Creates a matrix in dense format.

2439:    Collective

2441:    Input Parameters:
2442: +  comm - MPI communicator
2443: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2444: .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
2445: .  M - number of global rows (or PETSC_DECIDE to have calculated if m is given)
2446: .  N - number of global columns (or PETSC_DECIDE to have calculated if n is given)
2447: -  data - optional location of matrix data.  Set data=NULL (PETSC_NULL_SCALAR for Fortran users) for PETSc
2448:    to control all matrix memory allocation.

2450:    Output Parameter:
2451: .  A - the matrix

2453:    Notes:
2454:    The dense format is fully compatible with standard Fortran 77
2455:    storage by columns.
2456:    Note that, although local portions of the matrix are stored in column-major
2457:    order, the matrix is partitioned across MPI ranks by row.

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

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

2466:    Level: intermediate

2468: .seealso: MatCreate(), MatCreateSeqDense(), MatSetValues()
2469: @*/
2470: PetscErrorCode  MatCreateDense(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscScalar *data,Mat *A)
2471: {
2473:   PetscMPIInt    size;

2476:   MatCreate(comm,A);
2477:   MatSetSizes(*A,m,n,M,N);
2478:   MPI_Comm_size(comm,&size);
2479:   if (size > 1) {
2480:     PetscBool havedata = (PetscBool)!!data;

2482:     MatSetType(*A,MATMPIDENSE);
2483:     MatMPIDenseSetPreallocation(*A,data);
2484:     MPIU_Allreduce(MPI_IN_PLACE,&havedata,1,MPIU_BOOL,MPI_LOR,comm);
2485:     if (havedata) {  /* user provided data array, so no need to assemble */
2486:       MatSetUpMultiply_MPIDense(*A);
2487:       (*A)->assembled = PETSC_TRUE;
2488:     }
2489:   } else {
2490:     MatSetType(*A,MATSEQDENSE);
2491:     MatSeqDenseSetPreallocation(*A,data);
2492:   }
2493:   return(0);
2494: }

2496: #if defined(PETSC_HAVE_CUDA)
2497: /*@C
2498:    MatCreateDenseCUDA - Creates a matrix in dense format using CUDA.

2500:    Collective

2502:    Input Parameters:
2503: +  comm - MPI communicator
2504: .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2505: .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
2506: .  M - number of global rows (or PETSC_DECIDE to have calculated if m is given)
2507: .  N - number of global columns (or PETSC_DECIDE to have calculated if n is given)
2508: -  data - optional location of GPU matrix data.  Set data=NULL for PETSc
2509:    to control matrix memory allocation.

2511:    Output Parameter:
2512: .  A - the matrix

2514:    Notes:

2516:    Level: intermediate

2518: .seealso: MatCreate(), MatCreateDense()
2519: @*/
2520: PetscErrorCode  MatCreateDenseCUDA(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscScalar *data,Mat *A)
2521: {
2523:   PetscMPIInt    size;

2526:   MatCreate(comm,A);
2528:   MatSetSizes(*A,m,n,M,N);
2529:   MPI_Comm_size(comm,&size);
2530:   if (size > 1) {
2531:     MatSetType(*A,MATMPIDENSECUDA);
2532:     MatMPIDenseCUDASetPreallocation(*A,data);
2533:     if (data) {  /* user provided data array, so no need to assemble */
2534:       MatSetUpMultiply_MPIDense(*A);
2535:       (*A)->assembled = PETSC_TRUE;
2536:     }
2537:   } else {
2538:     MatSetType(*A,MATSEQDENSECUDA);
2539:     MatSeqDenseCUDASetPreallocation(*A,data);
2540:   }
2541:   return(0);
2542: }
2543: #endif

2545: static PetscErrorCode MatDuplicate_MPIDense(Mat A,MatDuplicateOption cpvalues,Mat *newmat)
2546: {
2547:   Mat            mat;
2548:   Mat_MPIDense   *a,*oldmat = (Mat_MPIDense*)A->data;

2552:   *newmat = NULL;
2553:   MatCreate(PetscObjectComm((PetscObject)A),&mat);
2554:   MatSetSizes(mat,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
2555:   MatSetType(mat,((PetscObject)A)->type_name);
2556:   a       = (Mat_MPIDense*)mat->data;

2558:   mat->factortype   = A->factortype;
2559:   mat->assembled    = PETSC_TRUE;
2560:   mat->preallocated = PETSC_TRUE;

2562:   mat->insertmode = NOT_SET_VALUES;
2563:   a->donotstash   = oldmat->donotstash;

2565:   PetscLayoutReference(A->rmap,&mat->rmap);
2566:   PetscLayoutReference(A->cmap,&mat->cmap);

2568:   MatDuplicate(oldmat->A,cpvalues,&a->A);
2569:   PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
2570:   MatSetUpMultiply_MPIDense(mat);

2572:   *newmat = mat;
2573:   return(0);
2574: }

2576: PetscErrorCode MatLoad_MPIDense(Mat newMat, PetscViewer viewer)
2577: {
2579:   PetscBool      isbinary;
2580: #if defined(PETSC_HAVE_HDF5)
2581:   PetscBool      ishdf5;
2582: #endif

2587:   /* force binary viewer to load .info file if it has not yet done so */
2588:   PetscViewerSetUp(viewer);
2589:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
2590: #if defined(PETSC_HAVE_HDF5)
2591:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,  &ishdf5);
2592: #endif
2593:   if (isbinary) {
2594:     MatLoad_Dense_Binary(newMat,viewer);
2595: #if defined(PETSC_HAVE_HDF5)
2596:   } else if (ishdf5) {
2597:     MatLoad_Dense_HDF5(newMat,viewer);
2598: #endif
2599:   } else SETERRQ2(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"Viewer type %s not yet supported for reading %s matrices",((PetscObject)viewer)->type_name,((PetscObject)newMat)->type_name);
2600:   return(0);
2601: }

2603: static PetscErrorCode MatEqual_MPIDense(Mat A,Mat B,PetscBool  *flag)
2604: {
2605:   Mat_MPIDense   *matB = (Mat_MPIDense*)B->data,*matA = (Mat_MPIDense*)A->data;
2606:   Mat            a,b;
2607:   PetscBool      flg;

2611:   a    = matA->A;
2612:   b    = matB->A;
2613:   MatEqual(a,b,&flg);
2614:   MPIU_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
2615:   return(0);
2616: }

2618: PetscErrorCode MatDestroy_MatTransMatMult_MPIDense_MPIDense(void *data)
2619: {
2620:   PetscErrorCode        ierr;
2621:   Mat_TransMatMultDense *atb = (Mat_TransMatMultDense *)data;

2624:   PetscFree2(atb->sendbuf,atb->recvcounts);
2625:   MatDestroy(&atb->atb);
2626:   PetscFree(atb);
2627:   return(0);
2628: }

2630: PetscErrorCode MatDestroy_MatMatTransMult_MPIDense_MPIDense(void *data)
2631: {
2632:   PetscErrorCode        ierr;
2633:   Mat_MatTransMultDense *abt = (Mat_MatTransMultDense *)data;

2636:   PetscFree2(abt->buf[0],abt->buf[1]);
2637:   PetscFree2(abt->recvcounts,abt->recvdispls);
2638:   PetscFree(abt);
2639:   return(0);
2640: }

2642: static PetscErrorCode MatTransposeMatMultNumeric_MPIDense_MPIDense(Mat A,Mat B,Mat C)
2643: {
2644:   Mat_MPIDense          *a=(Mat_MPIDense*)A->data, *b=(Mat_MPIDense*)B->data, *c=(Mat_MPIDense*)C->data;
2645:   Mat_TransMatMultDense *atb;
2646:   PetscErrorCode        ierr;
2647:   MPI_Comm              comm;
2648:   PetscMPIInt           size,*recvcounts;
2649:   PetscScalar           *carray,*sendbuf;
2650:   const PetscScalar     *atbarray;
2651:   PetscInt              i,cN=C->cmap->N,cM=C->rmap->N,proc,k,j;
2652:   const PetscInt        *ranges;

2655:   MatCheckProduct(C,3);
2656:   if (!C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data empty");
2657:   atb = (Mat_TransMatMultDense *)C->product->data;
2658:   recvcounts = atb->recvcounts;
2659:   sendbuf = atb->sendbuf;

2661:   PetscObjectGetComm((PetscObject)A,&comm);
2662:   MPI_Comm_size(comm,&size);

2664:   /* compute atbarray = aseq^T * bseq */
2665:   MatTransposeMatMult(a->A,b->A,atb->atb ? MAT_REUSE_MATRIX : MAT_INITIAL_MATRIX,PETSC_DEFAULT,&atb->atb);

2667:   MatGetOwnershipRanges(C,&ranges);

2669:   /* arrange atbarray into sendbuf */
2670:   MatDenseGetArrayRead(atb->atb,&atbarray);
2671:   for (proc=0, k=0; proc<size; proc++) {
2672:     for (j=0; j<cN; j++) {
2673:       for (i=ranges[proc]; i<ranges[proc+1]; i++) sendbuf[k++] = atbarray[i+j*cM];
2674:     }
2675:   }
2676:   MatDenseRestoreArrayRead(atb->atb,&atbarray);

2678:   /* sum all atbarray to local values of C */
2679:   MatDenseGetArrayWrite(c->A,&carray);
2680:   MPI_Reduce_scatter(sendbuf,carray,recvcounts,MPIU_SCALAR,MPIU_SUM,comm);
2681:   MatDenseRestoreArrayWrite(c->A,&carray);
2682:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2683:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
2684:   return(0);
2685: }

2687: static PetscErrorCode MatTransposeMatMultSymbolic_MPIDense_MPIDense(Mat A,Mat B,PetscReal fill,Mat C)
2688: {
2689:   PetscErrorCode        ierr;
2690:   MPI_Comm              comm;
2691:   PetscMPIInt           size;
2692:   PetscInt              cm=A->cmap->n,cM,cN=B->cmap->N;
2693:   Mat_TransMatMultDense *atb;
2694:   PetscBool             cisdense;
2695:   PetscInt              i;
2696:   const PetscInt        *ranges;

2699:   MatCheckProduct(C,3);
2700:   if (C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data not empty");
2701:   PetscObjectGetComm((PetscObject)A,&comm);
2702:   if (A->rmap->rstart != B->rmap->rstart || A->rmap->rend != B->rmap->rend) {
2703:     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);
2704:   }

2706:   /* create matrix product C */
2707:   MatSetSizes(C,cm,B->cmap->n,A->cmap->N,B->cmap->N);
2708:   PetscObjectTypeCompareAny((PetscObject)C,&cisdense,MATMPIDENSE,MATMPIDENSECUDA,"");
2709:   if (!cisdense) {
2710:     MatSetType(C,((PetscObject)A)->type_name);
2711:   }
2712:   MatSetUp(C);

2714:   /* create data structure for reuse C */
2715:   MPI_Comm_size(comm,&size);
2716:   PetscNew(&atb);
2717:   cM   = C->rmap->N;
2718:   PetscMalloc2((size_t)cM*(size_t)cN,&atb->sendbuf,size,&atb->recvcounts);
2719:   MatGetOwnershipRanges(C,&ranges);
2720:   for (i=0; i<size; i++) atb->recvcounts[i] = (ranges[i+1] - ranges[i])*cN;

2722:   C->product->data    = atb;
2723:   C->product->destroy = MatDestroy_MatTransMatMult_MPIDense_MPIDense;
2724:   return(0);
2725: }

2727: static PetscErrorCode MatMatTransposeMultSymbolic_MPIDense_MPIDense(Mat A, Mat B, PetscReal fill, Mat C)
2728: {
2729:   PetscErrorCode        ierr;
2730:   MPI_Comm              comm;
2731:   PetscMPIInt           i, size;
2732:   PetscInt              maxRows, bufsiz;
2733:   PetscMPIInt           tag;
2734:   PetscInt              alg;
2735:   Mat_MatTransMultDense *abt;
2736:   Mat_Product           *product = C->product;
2737:   PetscBool             flg;

2740:   MatCheckProduct(C,4);
2741:   if (C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data not empty");
2742:   /* check local size of A and B */
2743:   if (A->cmap->n != B->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local column dimensions are incompatible, A (%D) != B (%D)",A->cmap->n,B->cmap->n);

2745:   PetscStrcmp(product->alg,"allgatherv",&flg);
2746:   alg  = flg ? 0 : 1;

2748:   /* setup matrix product C */
2749:   MatSetSizes(C,A->rmap->n,B->rmap->n,A->rmap->N,B->rmap->N);
2750:   MatSetType(C,MATMPIDENSE);
2751:   MatSetUp(C);
2752:   PetscObjectGetNewTag((PetscObject)C,&tag);

2754:   /* create data structure for reuse C */
2755:   PetscObjectGetComm((PetscObject)C,&comm);
2756:   MPI_Comm_size(comm,&size);
2757:   PetscNew(&abt);
2758:   abt->tag = tag;
2759:   abt->alg = alg;
2760:   switch (alg) {
2761:   case 1: /* alg: "cyclic" */
2762:     for (maxRows = 0, i = 0; i < size; i++) maxRows = PetscMax(maxRows, (B->rmap->range[i + 1] - B->rmap->range[i]));
2763:     bufsiz = A->cmap->N * maxRows;
2764:     PetscMalloc2(bufsiz,&(abt->buf[0]),bufsiz,&(abt->buf[1]));
2765:     break;
2766:   default: /* alg: "allgatherv" */
2767:     PetscMalloc2(B->rmap->n * B->cmap->N, &(abt->buf[0]), B->rmap->N * B->cmap->N, &(abt->buf[1]));
2768:     PetscMalloc2(size,&(abt->recvcounts),size+1,&(abt->recvdispls));
2769:     for (i = 0; i <= size; i++) abt->recvdispls[i] = B->rmap->range[i] * A->cmap->N;
2770:     for (i = 0; i < size; i++) abt->recvcounts[i] = abt->recvdispls[i + 1] - abt->recvdispls[i];
2771:     break;
2772:   }

2774:   C->product->data    = abt;
2775:   C->product->destroy = MatDestroy_MatMatTransMult_MPIDense_MPIDense;
2776:   C->ops->mattransposemultnumeric = MatMatTransposeMultNumeric_MPIDense_MPIDense;
2777:   return(0);
2778: }

2780: static PetscErrorCode MatMatTransposeMultNumeric_MPIDense_MPIDense_Cyclic(Mat A, Mat B, Mat C)
2781: {
2782:   Mat_MPIDense          *a=(Mat_MPIDense*)A->data, *b=(Mat_MPIDense*)B->data, *c=(Mat_MPIDense*)C->data;
2783:   Mat_MatTransMultDense *abt;
2784:   PetscErrorCode        ierr;
2785:   MPI_Comm              comm;
2786:   PetscMPIInt           rank,size, sendsiz, recvsiz, sendto, recvfrom, recvisfrom;
2787:   PetscScalar           *sendbuf, *recvbuf=NULL, *cv;
2788:   PetscInt              i,cK=A->cmap->N,k,j,bn;
2789:   PetscScalar           _DOne=1.0,_DZero=0.0;
2790:   const PetscScalar     *av,*bv;
2791:   PetscBLASInt          cm, cn, ck, alda, blda = 0, clda;
2792:   MPI_Request           reqs[2];
2793:   const PetscInt        *ranges;

2796:   MatCheckProduct(C,3);
2797:   if (!C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data empty");
2798:   abt  = (Mat_MatTransMultDense*)C->product->data;
2799:   PetscObjectGetComm((PetscObject)C,&comm);
2800:   MPI_Comm_rank(comm,&rank);
2801:   MPI_Comm_size(comm,&size);
2802:   MatDenseGetArrayRead(a->A,&av);
2803:   MatDenseGetArrayRead(b->A,&bv);
2804:   MatDenseGetArrayWrite(c->A,&cv);
2805:   MatDenseGetLDA(a->A,&i);
2806:   PetscBLASIntCast(i,&alda);
2807:   MatDenseGetLDA(b->A,&i);
2808:   PetscBLASIntCast(i,&blda);
2809:   MatDenseGetLDA(c->A,&i);
2810:   PetscBLASIntCast(i,&clda);
2811:   MatGetOwnershipRanges(B,&ranges);
2812:   bn   = B->rmap->n;
2813:   if (blda == bn) {
2814:     sendbuf = (PetscScalar*)bv;
2815:   } else {
2816:     sendbuf = abt->buf[0];
2817:     for (k = 0, i = 0; i < cK; i++) {
2818:       for (j = 0; j < bn; j++, k++) {
2819:         sendbuf[k] = bv[i * blda + j];
2820:       }
2821:     }
2822:   }
2823:   if (size > 1) {
2824:     sendto = (rank + size - 1) % size;
2825:     recvfrom = (rank + size + 1) % size;
2826:   } else {
2827:     sendto = recvfrom = 0;
2828:   }
2829:   PetscBLASIntCast(cK,&ck);
2830:   PetscBLASIntCast(c->A->rmap->n,&cm);
2831:   recvisfrom = rank;
2832:   for (i = 0; i < size; i++) {
2833:     /* we have finished receiving in sending, bufs can be read/modified */
2834:     PetscInt nextrecvisfrom = (recvisfrom + 1) % size; /* which process the next recvbuf will originate on */
2835:     PetscInt nextbn = ranges[nextrecvisfrom + 1] - ranges[nextrecvisfrom];

2837:     if (nextrecvisfrom != rank) {
2838:       /* start the cyclic sends from sendbuf, to recvbuf (which will switch to sendbuf) */
2839:       sendsiz = cK * bn;
2840:       recvsiz = cK * nextbn;
2841:       recvbuf = (i & 1) ? abt->buf[0] : abt->buf[1];
2842:       MPI_Isend(sendbuf, sendsiz, MPIU_SCALAR, sendto, abt->tag, comm, &reqs[0]);
2843:       MPI_Irecv(recvbuf, recvsiz, MPIU_SCALAR, recvfrom, abt->tag, comm, &reqs[1]);
2844:     }

2846:     /* local aseq * sendbuf^T */
2847:     PetscBLASIntCast(ranges[recvisfrom + 1] - ranges[recvisfrom], &cn);
2848:     if (cm && cn && ck) PetscStackCallBLAS("BLASgemm",BLASgemm_("N","T",&cm,&cn,&ck,&_DOne,av,&alda,sendbuf,&cn,&_DZero,cv + clda * ranges[recvisfrom],&clda));

2850:     if (nextrecvisfrom != rank) {
2851:       /* wait for the sends and receives to complete, swap sendbuf and recvbuf */
2852:       MPI_Waitall(2, reqs, MPI_STATUSES_IGNORE);
2853:     }
2854:     bn = nextbn;
2855:     recvisfrom = nextrecvisfrom;
2856:     sendbuf = recvbuf;
2857:   }
2858:   MatDenseRestoreArrayRead(a->A,&av);
2859:   MatDenseRestoreArrayRead(b->A,&bv);
2860:   MatDenseRestoreArrayWrite(c->A,&cv);
2861:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2862:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
2863:   return(0);
2864: }

2866: static PetscErrorCode MatMatTransposeMultNumeric_MPIDense_MPIDense_Allgatherv(Mat A, Mat B, Mat C)
2867: {
2868:   Mat_MPIDense          *a=(Mat_MPIDense*)A->data, *b=(Mat_MPIDense*)B->data, *c=(Mat_MPIDense*)C->data;
2869:   Mat_MatTransMultDense *abt;
2870:   PetscErrorCode        ierr;
2871:   MPI_Comm              comm;
2872:   PetscMPIInt           size;
2873:   PetscScalar           *cv, *sendbuf, *recvbuf;
2874:   const PetscScalar     *av,*bv;
2875:   PetscInt              blda,i,cK=A->cmap->N,k,j,bn;
2876:   PetscScalar           _DOne=1.0,_DZero=0.0;
2877:   PetscBLASInt          cm, cn, ck, alda, clda;

2880:   MatCheckProduct(C,3);
2881:   if (!C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data empty");
2882:   abt  = (Mat_MatTransMultDense*)C->product->data;
2883:   PetscObjectGetComm((PetscObject)A,&comm);
2884:   MPI_Comm_size(comm,&size);
2885:   MatDenseGetArrayRead(a->A,&av);
2886:   MatDenseGetArrayRead(b->A,&bv);
2887:   MatDenseGetArrayWrite(c->A,&cv);
2888:   MatDenseGetLDA(a->A,&i);
2889:   PetscBLASIntCast(i,&alda);
2890:   MatDenseGetLDA(b->A,&blda);
2891:   MatDenseGetLDA(c->A,&i);
2892:   PetscBLASIntCast(i,&clda);
2893:   /* copy transpose of B into buf[0] */
2894:   bn      = B->rmap->n;
2895:   sendbuf = abt->buf[0];
2896:   recvbuf = abt->buf[1];
2897:   for (k = 0, j = 0; j < bn; j++) {
2898:     for (i = 0; i < cK; i++, k++) {
2899:       sendbuf[k] = bv[i * blda + j];
2900:     }
2901:   }
2902:   MatDenseRestoreArrayRead(b->A,&bv);
2903:   MPI_Allgatherv(sendbuf, bn * cK, MPIU_SCALAR, recvbuf, abt->recvcounts, abt->recvdispls, MPIU_SCALAR, comm);
2904:   PetscBLASIntCast(cK,&ck);
2905:   PetscBLASIntCast(c->A->rmap->n,&cm);
2906:   PetscBLASIntCast(c->A->cmap->n,&cn);
2907:   if (cm && cn && ck) PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&cm,&cn,&ck,&_DOne,av,&alda,recvbuf,&ck,&_DZero,cv,&clda));
2908:   MatDenseRestoreArrayRead(a->A,&av);
2909:   MatDenseRestoreArrayRead(b->A,&bv);
2910:   MatDenseRestoreArrayWrite(c->A,&cv);
2911:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2912:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
2913:   return(0);
2914: }

2916: static PetscErrorCode MatMatTransposeMultNumeric_MPIDense_MPIDense(Mat A, Mat B, Mat C)
2917: {
2918:   Mat_MatTransMultDense *abt;
2919:   PetscErrorCode        ierr;

2922:   MatCheckProduct(C,3);
2923:   if (!C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data empty");
2924:   abt = (Mat_MatTransMultDense*)C->product->data;
2925:   switch (abt->alg) {
2926:   case 1:
2927:     MatMatTransposeMultNumeric_MPIDense_MPIDense_Cyclic(A, B, C);
2928:     break;
2929:   default:
2930:     MatMatTransposeMultNumeric_MPIDense_MPIDense_Allgatherv(A, B, C);
2931:     break;
2932:   }
2933:   return(0);
2934: }

2936: PetscErrorCode MatDestroy_MatMatMult_MPIDense_MPIDense(void *data)
2937: {
2938:   PetscErrorCode   ierr;
2939:   Mat_MatMultDense *ab = (Mat_MatMultDense*)data;

2942:   MatDestroy(&ab->Ce);
2943:   MatDestroy(&ab->Ae);
2944:   MatDestroy(&ab->Be);
2945:   PetscFree(ab);
2946:   return(0);
2947: }

2949: #if defined(PETSC_HAVE_ELEMENTAL)
2950: PetscErrorCode MatMatMultNumeric_MPIDense_MPIDense(Mat A,Mat B,Mat C)
2951: {
2952:   PetscErrorCode   ierr;
2953:   Mat_MatMultDense *ab;

2956:   MatCheckProduct(C,3);
2957:   if (!C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Missing product data");
2958:   ab   = (Mat_MatMultDense*)C->product->data;
2959:   MatConvert_MPIDense_Elemental(A,MATELEMENTAL,MAT_REUSE_MATRIX, &ab->Ae);
2960:   MatConvert_MPIDense_Elemental(B,MATELEMENTAL,MAT_REUSE_MATRIX, &ab->Be);
2961:   MatMatMultNumeric_Elemental(ab->Ae,ab->Be,ab->Ce);
2962:   MatConvert(ab->Ce,MATMPIDENSE,MAT_REUSE_MATRIX,&C);
2963:   return(0);
2964: }

2966: static PetscErrorCode MatMatMultSymbolic_MPIDense_MPIDense(Mat A,Mat B,PetscReal fill,Mat C)
2967: {
2968:   PetscErrorCode   ierr;
2969:   Mat              Ae,Be,Ce;
2970:   Mat_MatMultDense *ab;

2973:   MatCheckProduct(C,4);
2974:   if (C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data not empty");
2975:   /* check local size of A and B */
2976:   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
2977:     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);
2978:   }

2980:   /* create elemental matrices Ae and Be */
2981:   MatCreate(PetscObjectComm((PetscObject)A), &Ae);
2982:   MatSetSizes(Ae,PETSC_DECIDE,PETSC_DECIDE,A->rmap->N,A->cmap->N);
2983:   MatSetType(Ae,MATELEMENTAL);
2984:   MatSetUp(Ae);
2985:   MatSetOption(Ae,MAT_ROW_ORIENTED,PETSC_FALSE);

2987:   MatCreate(PetscObjectComm((PetscObject)B), &Be);
2988:   MatSetSizes(Be,PETSC_DECIDE,PETSC_DECIDE,B->rmap->N,B->cmap->N);
2989:   MatSetType(Be,MATELEMENTAL);
2990:   MatSetUp(Be);
2991:   MatSetOption(Be,MAT_ROW_ORIENTED,PETSC_FALSE);

2993:   /* compute symbolic Ce = Ae*Be */
2994:   MatCreate(PetscObjectComm((PetscObject)C),&Ce);
2995:   MatMatMultSymbolic_Elemental(Ae,Be,fill,Ce);

2997:   /* setup C */
2998:   MatSetSizes(C,A->rmap->n,B->cmap->n,PETSC_DECIDE,PETSC_DECIDE);
2999:   MatSetType(C,MATDENSE);
3000:   MatSetUp(C);

3002:   /* create data structure for reuse Cdense */
3003:   PetscNew(&ab);
3004:   ab->Ae = Ae;
3005:   ab->Be = Be;
3006:   ab->Ce = Ce;

3008:   C->product->data    = ab;
3009:   C->product->destroy = MatDestroy_MatMatMult_MPIDense_MPIDense;
3010:   C->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIDense;
3011:   return(0);
3012: }
3013: #endif
3014: /* ----------------------------------------------- */
3015: #if defined(PETSC_HAVE_ELEMENTAL)
3016: static PetscErrorCode MatProductSetFromOptions_MPIDense_AB(Mat C)
3017: {
3019:   C->ops->matmultsymbolic = MatMatMultSymbolic_MPIDense_MPIDense;
3020:   C->ops->productsymbolic = MatProductSymbolic_AB;
3021:   return(0);
3022: }
3023: #endif

3025: static PetscErrorCode MatProductSetFromOptions_MPIDense_AtB(Mat C)
3026: {
3027:   Mat_Product *product = C->product;
3028:   Mat         A = product->A,B=product->B;

3031:   if (A->rmap->rstart != B->rmap->rstart || A->rmap->rend != B->rmap->rend)
3032:     SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%D, %D) != (%D,%D)",A->rmap->rstart,A->rmap->rend,B->rmap->rstart,B->rmap->rend);
3033:   C->ops->transposematmultsymbolic = MatTransposeMatMultSymbolic_MPIDense_MPIDense;
3034:   C->ops->productsymbolic = MatProductSymbolic_AtB;
3035:   return(0);
3036: }

3038: static PetscErrorCode MatProductSetFromOptions_MPIDense_ABt(Mat C)
3039: {
3041:   Mat_Product    *product = C->product;
3042:   const char     *algTypes[2] = {"allgatherv","cyclic"};
3043:   PetscInt       alg,nalg = 2;
3044:   PetscBool      flg = PETSC_FALSE;

3047:   /* Set default algorithm */
3048:   alg = 0; /* default is allgatherv */
3049:   PetscStrcmp(product->alg,"default",&flg);
3050:   if (flg) {
3051:     MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
3052:   }

3054:   /* Get runtime option */
3055:   if (product->api_user) {
3056:     PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatMatTransposeMult","Mat");
3057:     PetscOptionsEList("-matmattransmult_mpidense_mpidense_via","Algorithmic approach","MatMatTransposeMult",algTypes,nalg,algTypes[alg],&alg,&flg);
3058:     PetscOptionsEnd();
3059:   } else {
3060:     PetscOptionsBegin(PetscObjectComm((PetscObject)C),((PetscObject)C)->prefix,"MatProduct_ABt","Mat");
3061:     PetscOptionsEList("-matproduct_abt_mpidense_mpidense_via","Algorithmic approach","MatProduct_ABt",algTypes,nalg,algTypes[alg],&alg,&flg);
3062:     PetscOptionsEnd();
3063:   }
3064:   if (flg) {
3065:     MatProductSetAlgorithm(C,(MatProductAlgorithm)algTypes[alg]);
3066:   }

3068:   C->ops->mattransposemultsymbolic = MatMatTransposeMultSymbolic_MPIDense_MPIDense;
3069:   C->ops->productsymbolic          = MatProductSymbolic_ABt;
3070:   return(0);
3071: }

3073: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_MPIDense(Mat C)
3074: {
3076:   Mat_Product    *product = C->product;

3079:   switch (product->type) {
3080: #if defined(PETSC_HAVE_ELEMENTAL)
3081:   case MATPRODUCT_AB:
3082:     MatProductSetFromOptions_MPIDense_AB(C);
3083:     break;
3084: #endif
3085:   case MATPRODUCT_AtB:
3086:     MatProductSetFromOptions_MPIDense_AtB(C);
3087:     break;
3088:   case MATPRODUCT_ABt:
3089:     MatProductSetFromOptions_MPIDense_ABt(C);
3090:     break;
3091:   default:
3092:     break;
3093:   }
3094:   return(0);
3095: }