Actual source code: mpiaijcusparse.cu

petsc-master 2020-08-04
Report Typos and Errors
  1: #define PETSC_SKIP_SPINLOCK
  2: #define PETSC_SKIP_CXX_COMPLEX_FIX
  3: #define PETSC_SKIP_IMMINTRIN_H_CUDAWORKAROUND 1

  5: #include <petscconf.h>
  6:  #include <../src/mat/impls/aij/mpi/mpiaij.h>
  7:  #include <../src/mat/impls/aij/seq/seqcusparse/cusparsematimpl.h>
  8:  #include <../src/mat/impls/aij/mpi/mpicusparse/mpicusparsematimpl.h>

 10: PetscErrorCode  MatMPIAIJSetPreallocation_MPIAIJCUSPARSE(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
 11: {
 12:   Mat_MPIAIJ         *b               = (Mat_MPIAIJ*)B->data;
 13:   Mat_MPIAIJCUSPARSE * cusparseStruct = (Mat_MPIAIJCUSPARSE*)b->spptr;
 14:   PetscErrorCode     ierr;
 15:   PetscInt           i;

 18:   PetscLayoutSetUp(B->rmap);
 19:   PetscLayoutSetUp(B->cmap);
 20:   if (d_nnz) {
 21:     for (i=0; i<B->rmap->n; i++) {
 22:       if (d_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than 0: local row %D value %D",i,d_nnz[i]);
 23:     }
 24:   }
 25:   if (o_nnz) {
 26:     for (i=0; i<B->rmap->n; i++) {
 27:       if (o_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than 0: local row %D value %D",i,o_nnz[i]);
 28:     }
 29:   }
 30:   if (!B->preallocated) {
 31:     /* Explicitly create 2 MATSEQAIJCUSPARSE matrices. */
 32:     MatCreate(PETSC_COMM_SELF,&b->A);
 33:     MatBindToCPU(b->A,B->boundtocpu);
 34:     MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
 35:     MatSetType(b->A,MATSEQAIJCUSPARSE);
 36:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
 37:     MatCreate(PETSC_COMM_SELF,&b->B);
 38:     MatBindToCPU(b->B,B->boundtocpu);
 39:     MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);
 40:     MatSetType(b->B,MATSEQAIJCUSPARSE);
 41:     PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);
 42:   }
 43:   MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);
 44:   MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);
 45:   MatCUSPARSESetFormat(b->A,MAT_CUSPARSE_MULT,cusparseStruct->diagGPUMatFormat);
 46:   MatCUSPARSESetFormat(b->B,MAT_CUSPARSE_MULT,cusparseStruct->offdiagGPUMatFormat);
 47:   MatCUSPARSESetHandle(b->A,cusparseStruct->handle);
 48:   MatCUSPARSESetHandle(b->B,cusparseStruct->handle);
 49:   MatCUSPARSESetStream(b->A,cusparseStruct->stream);
 50:   MatCUSPARSESetStream(b->B,cusparseStruct->stream);

 52:   B->preallocated = PETSC_TRUE;
 53:   return(0);
 54: }

 56: PetscErrorCode MatMult_MPIAIJCUSPARSE(Mat A,Vec xx,Vec yy)
 57: {
 58:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
 60:   PetscInt       nt;

 63:   VecGetLocalSize(xx,&nt);
 64:   if (nt != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%D) and xx (%D)",A->cmap->n,nt);
 65:   VecScatterInitializeForGPU(a->Mvctx,xx);
 66:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
 67:   (*a->A->ops->mult)(a->A,xx,yy);
 68:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
 69:   (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
 70:   VecScatterFinalizeForGPU(a->Mvctx);
 71:   return(0);
 72: }

 74: PetscErrorCode MatMultAdd_MPIAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz)
 75: {
 76:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
 78:   PetscInt       nt;

 81:   VecGetLocalSize(xx,&nt);
 82:   if (nt != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%D) and xx (%D)",A->cmap->n,nt);
 83:   VecScatterInitializeForGPU(a->Mvctx,xx);
 84:   VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
 85:   (*a->A->ops->multadd)(a->A,xx,yy,zz);
 86:   VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
 87:   (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
 88:   VecScatterFinalizeForGPU(a->Mvctx);
 89:   return(0);
 90: }

 92: PetscErrorCode MatMultTranspose_MPIAIJCUSPARSE(Mat A,Vec xx,Vec yy)
 93: {
 94:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
 96:   PetscInt       nt;

 99:   VecGetLocalSize(xx,&nt);
100:   if (nt != A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%D) and xx (%D)",A->rmap->n,nt);
101:   VecScatterInitializeForGPU(a->Mvctx,a->lvec);
102:   (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
103:   (*a->A->ops->multtranspose)(a->A,xx,yy);
104:   VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
105:   VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
106:   VecScatterFinalizeForGPU(a->Mvctx);
107:   return(0);
108: }

110: PetscErrorCode MatCUSPARSESetFormat_MPIAIJCUSPARSE(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format)
111: {
112:   Mat_MPIAIJ         *a               = (Mat_MPIAIJ*)A->data;
113:   Mat_MPIAIJCUSPARSE * cusparseStruct = (Mat_MPIAIJCUSPARSE*)a->spptr;

116:   switch (op) {
117:   case MAT_CUSPARSE_MULT_DIAG:
118:     cusparseStruct->diagGPUMatFormat = format;
119:     break;
120:   case MAT_CUSPARSE_MULT_OFFDIAG:
121:     cusparseStruct->offdiagGPUMatFormat = format;
122:     break;
123:   case MAT_CUSPARSE_ALL:
124:     cusparseStruct->diagGPUMatFormat    = format;
125:     cusparseStruct->offdiagGPUMatFormat = format;
126:     break;
127:   default:
128:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unsupported operation %d for MatCUSPARSEFormatOperation. Only MAT_CUSPARSE_MULT_DIAG, MAT_CUSPARSE_MULT_DIAG, and MAT_CUSPARSE_MULT_ALL are currently supported.",op);
129:   }
130:   return(0);
131: }

133: PetscErrorCode MatSetFromOptions_MPIAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat A)
134: {
135:   MatCUSPARSEStorageFormat format;
136:   PetscErrorCode           ierr;
137:   PetscBool                flg;
138:   Mat_MPIAIJ               *a = (Mat_MPIAIJ*)A->data;
139:   Mat_MPIAIJCUSPARSE       *cusparseStruct = (Mat_MPIAIJCUSPARSE*)a->spptr;

142:   PetscOptionsHead(PetscOptionsObject,"MPIAIJCUSPARSE options");
143:   if (A->factortype==MAT_FACTOR_NONE) {
144:     PetscOptionsEnum("-mat_cusparse_mult_diag_storage_format","sets storage format of the diagonal blocks of (mpi)aijcusparse gpu matrices for SpMV",
145:                             "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparseStruct->diagGPUMatFormat,(PetscEnum*)&format,&flg);
146:     if (flg) {
147:       MatCUSPARSESetFormat(A,MAT_CUSPARSE_MULT_DIAG,format);
148:     }
149:     PetscOptionsEnum("-mat_cusparse_mult_offdiag_storage_format","sets storage format of the off-diagonal blocks (mpi)aijcusparse gpu matrices for SpMV",
150:                             "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparseStruct->offdiagGPUMatFormat,(PetscEnum*)&format,&flg);
151:     if (flg) {
152:       MatCUSPARSESetFormat(A,MAT_CUSPARSE_MULT_OFFDIAG,format);
153:     }
154:     PetscOptionsEnum("-mat_cusparse_storage_format","sets storage format of the diagonal and off-diagonal blocks (mpi)aijcusparse gpu matrices for SpMV",
155:                             "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparseStruct->diagGPUMatFormat,(PetscEnum*)&format,&flg);
156:     if (flg) {
157:       MatCUSPARSESetFormat(A,MAT_CUSPARSE_ALL,format);
158:     }
159:   }
160:   PetscOptionsTail();
161:   return(0);
162: }

164: PetscErrorCode MatAssemblyEnd_MPIAIJCUSPARSE(Mat A,MatAssemblyType mode)
165: {
167:   Mat_MPIAIJ     *mpiaij;

170:   mpiaij = (Mat_MPIAIJ*)A->data;
171:   MatAssemblyEnd_MPIAIJ(A,mode);
172:   if (!A->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
173:     VecSetType(mpiaij->lvec,VECSEQCUDA);
174:   }
175:   return(0);
176: }

178: PetscErrorCode MatDestroy_MPIAIJCUSPARSE(Mat A)
179: {
180:   PetscErrorCode     ierr;
181:   Mat_MPIAIJ         *a              = (Mat_MPIAIJ*)A->data;
182:   Mat_MPIAIJCUSPARSE *cusparseStruct = (Mat_MPIAIJCUSPARSE*)a->spptr;
183:   cudaError_t        err;
184:   cusparseStatus_t   stat;

187:   try {
188:     if (a->A) { MatCUSPARSEClearHandle(a->A); }
189:     if (a->B) { MatCUSPARSEClearHandle(a->B); }
190:     stat = cusparseDestroy(cusparseStruct->handle);CHKERRCUSPARSE(stat);
191:     if (cusparseStruct->stream) {
192:       err = cudaStreamDestroy(cusparseStruct->stream);CHKERRCUDA(err);
193:     }
194:     delete cusparseStruct;
195:   } catch(char *ex) {
196:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Mat_MPIAIJCUSPARSE error: %s", ex);
197:   }
198:   PetscObjectComposeFunction((PetscObject)A,"MatMPIAIJSetPreallocation_C",NULL);
199:   PetscObjectComposeFunction((PetscObject)A,"MatCUSPARSESetFormat_C",NULL);
200:   MatDestroy_MPIAIJ(A);
201:   return(0);
202: }

204: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJCUSPARSE(Mat B, MatType mtype, MatReuse reuse, Mat* newmat)
205: {
206:   PetscErrorCode     ierr;
207:   Mat_MPIAIJ         *a;
208:   Mat_MPIAIJCUSPARSE *cusparseStruct;
209:   cusparseStatus_t   stat;
210:   Mat                A;

213:   if (reuse == MAT_INITIAL_MATRIX) {
214:     MatDuplicate(B,MAT_COPY_VALUES,newmat);
215:   } else if (reuse == MAT_REUSE_MATRIX) {
216:     MatCopy(B,*newmat,SAME_NONZERO_PATTERN);
217:   }
218:   A = *newmat;

220:   PetscFree(A->defaultvectype);
221:   PetscStrallocpy(VECCUDA,&A->defaultvectype);

223:   a = (Mat_MPIAIJ*)A->data;
224:   if (reuse != MAT_REUSE_MATRIX && !a->spptr) {
225:     a->spptr = new Mat_MPIAIJCUSPARSE;

227:     cusparseStruct                      = (Mat_MPIAIJCUSPARSE*)a->spptr;
228:     cusparseStruct->diagGPUMatFormat    = MAT_CUSPARSE_CSR;
229:     cusparseStruct->offdiagGPUMatFormat = MAT_CUSPARSE_CSR;
230:     cusparseStruct->stream              = 0;
231:     stat = cusparseCreate(&(cusparseStruct->handle));CHKERRCUSPARSE(stat);
232:   }

234:   A->ops->assemblyend    = MatAssemblyEnd_MPIAIJCUSPARSE;
235:   A->ops->mult           = MatMult_MPIAIJCUSPARSE;
236:   A->ops->multadd        = MatMultAdd_MPIAIJCUSPARSE;
237:   A->ops->multtranspose  = MatMultTranspose_MPIAIJCUSPARSE;
238:   A->ops->setfromoptions = MatSetFromOptions_MPIAIJCUSPARSE;
239:   A->ops->destroy        = MatDestroy_MPIAIJCUSPARSE;

241:   PetscObjectChangeTypeName((PetscObject)A,MATMPIAIJCUSPARSE);
242:   PetscObjectComposeFunction((PetscObject)A,"MatMPIAIJSetPreallocation_C",MatMPIAIJSetPreallocation_MPIAIJCUSPARSE);
243:   PetscObjectComposeFunction((PetscObject)A,"MatCUSPARSESetFormat_C",MatCUSPARSESetFormat_MPIAIJCUSPARSE);
244:   return(0);
245: }

247: PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJCUSPARSE(Mat A)
248: {

252:   if (!PetscCUDAInitialized) {PetscCUDAInitializeLazily();}
253:   MatCreate_MPIAIJ(A);
254:   MatConvert_MPIAIJ_MPIAIJCUSPARSE(A,MATMPIAIJCUSPARSE,MAT_INPLACE_MATRIX,&A);
255:   return(0);
256: }

258: /*@
259:    MatCreateAIJCUSPARSE - Creates a sparse matrix in AIJ (compressed row) format
260:    (the default parallel PETSc format).  This matrix will ultimately pushed down
261:    to NVidia GPUs and use the CUSPARSE library for calculations. For good matrix
262:    assembly performance the user should preallocate the matrix storage by setting
263:    the parameter nz (or the array nnz).  By setting these parameters accurately,
264:    performance during matrix assembly can be increased by more than a factor of 50.

266:    Collective

268:    Input Parameters:
269: +  comm - MPI communicator, set to PETSC_COMM_SELF
270: .  m - number of rows
271: .  n - number of columns
272: .  nz - number of nonzeros per row (same for all rows)
273: -  nnz - array containing the number of nonzeros in the various rows
274:          (possibly different for each row) or NULL

276:    Output Parameter:
277: .  A - the matrix

279:    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
280:    MatXXXXSetPreallocation() paradigm instead of this routine directly.
281:    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]

283:    Notes:
284:    If nnz is given then nz is ignored

286:    The AIJ format (also called the Yale sparse matrix format or
287:    compressed row storage), is fully compatible with standard Fortran 77
288:    storage.  That is, the stored row and column indices can begin at
289:    either one (as in Fortran) or zero.  See the users' manual for details.

291:    Specify the preallocated storage with either nz or nnz (not both).
292:    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
293:    allocation.  For large problems you MUST preallocate memory or you
294:    will get TERRIBLE performance, see the users' manual chapter on matrices.

296:    By default, this format uses inodes (identical nodes) when possible, to
297:    improve numerical efficiency of matrix-vector products and solves. We
298:    search for consecutive rows with the same nonzero structure, thereby
299:    reusing matrix information to achieve increased efficiency.

301:    Level: intermediate

303: .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ(), MATMPIAIJCUSPARSE, MATAIJCUSPARSE
304: @*/
305: PetscErrorCode  MatCreateAIJCUSPARSE(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A)
306: {
308:   PetscMPIInt    size;

311:   MatCreate(comm,A);
312:   MatSetSizes(*A,m,n,M,N);
313:   MPI_Comm_size(comm,&size);
314:   if (size > 1) {
315:     MatSetType(*A,MATMPIAIJCUSPARSE);
316:     MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);
317:   } else {
318:     MatSetType(*A,MATSEQAIJCUSPARSE);
319:     MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);
320:   }
321:   return(0);
322: }

324: /*MC
325:    MATAIJCUSPARSE - MATMPIAIJCUSPARSE = "aijcusparse" = "mpiaijcusparse" - A matrix type to be used for sparse matrices.

327:    A matrix type type whose data resides on Nvidia GPUs. These matrices can be in either
328:    CSR, ELL, or Hybrid format. The ELL and HYB formats require CUDA 4.2 or later.
329:    All matrix calculations are performed on Nvidia GPUs using the CUSPARSE library.

331:    This matrix type is identical to MATSEQAIJCUSPARSE when constructed with a single process communicator,
332:    and MATMPIAIJCUSPARSE otherwise.  As a result, for single process communicators,
333:    MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
334:    for communicators controlling multiple processes.  It is recommended that you call both of
335:    the above preallocation routines for simplicity.

337:    Options Database Keys:
338: +  -mat_type mpiaijcusparse - sets the matrix type to "mpiaijcusparse" during a call to MatSetFromOptions()
339: .  -mat_cusparse_storage_format csr - sets the storage format of diagonal and off-diagonal matrices during a call to MatSetFromOptions(). Other options include ell (ellpack) or hyb (hybrid).
340: .  -mat_cusparse_mult_diag_storage_format csr - sets the storage format of diagonal matrix during a call to MatSetFromOptions(). Other options include ell (ellpack) or hyb (hybrid).
341: -  -mat_cusparse_mult_offdiag_storage_format csr - sets the storage format of off-diagonal matrix during a call to MatSetFromOptions(). Other options include ell (ellpack) or hyb (hybrid).

343:   Level: beginner

345:  .seealso: MatCreateAIJCUSPARSE(), MATSEQAIJCUSPARSE, MatCreateSeqAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation
346: M
347: M*/