Actual source code: aijcusp.cu

petsc-3.8.4 2018-03-24
Report Typos and Errors
  1: /*
  2:   Defines the basic matrix operations for the AIJ (compressed row)
  3:   matrix storage format.
  4: */
  5: #define PETSC_SKIP_COMPLEX
  6: #define PETSC_SKIP_SPINLOCK

  8: #include <petscconf.h>
  9:  #include <../src/mat/impls/aij/seq/aij.h>
 10:  #include <petscbt.h>
 11:  #include <../src/vec/vec/impls/dvecimpl.h>
 12:  #include <petsc/private/vecimpl.h>
 13: #undef VecType
 14:  #include <../src/mat/impls/aij/seq/seqcusp/cuspmatimpl.h>
 15:  #include <../src/vec/vec/impls/seq/seqcusp/cuspvecimpl.h>

 17: const char *const MatCUSPStorageFormats[] = {"CSR","DIA","ELL","MatCUSPStorageFormat","MAT_CUSP_",0};

 19: PETSC_INTERN PetscErrorCode MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);

 21: PetscErrorCode MatCUSPSetStream(Mat A,const cudaStream_t stream)
 22: {
 23:   Mat_SeqAIJCUSP *cuspstruct = (Mat_SeqAIJCUSP*)A->spptr;

 26:   cuspstruct->stream = stream;
 27:   return(0);
 28: }

 30: PetscErrorCode MatCUSPSetFormat_SeqAIJCUSP(Mat A,MatCUSPFormatOperation op,MatCUSPStorageFormat format)
 31: {
 32:   Mat_SeqAIJCUSP *cuspMat = (Mat_SeqAIJCUSP*)A->spptr;

 35:   switch (op) {
 36:   case MAT_CUSP_MULT:
 37:     cuspMat->format = format;
 38:     break;
 39:   case MAT_CUSP_ALL:
 40:     cuspMat->format = format;
 41:     break;
 42:   default:
 43:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unsupported operation %d for MatCUSPFormatOperation. Only MAT_CUSP_MULT and MAT_CUSP_ALL are currently supported.",op);
 44:   }
 45:   return(0);
 46: }

 48: /*@
 49:    MatCUSPSetFormat - Sets the storage format of CUSP matrices for a particular
 50:    operation. Only the MatMult operation can use different GPU storage formats
 51:    for AIJCUSP matrices.

 53:    Not Collective

 55:    Input Parameters:
 56: +  A - Matrix of type SEQAIJCUSP
 57: .  op - MatCUSPFormatOperation. SEQAIJCUSP matrices support MAT_CUSP_MULT and MAT_CUSP_ALL. MPIAIJCUSP matrices support MAT_CUSP_MULT_DIAG, MAT_CUSP_MULT_OFFDIAG, and MAT_CUSP_ALL.
 58: -  format - MatCUSPStorageFormat (one of MAT_CUSP_CSR, MAT_CUSP_DIA, MAT_CUSP_ELL)

 60:    Output Parameter:

 62:    Level: intermediate

 64: .seealso: MatCUSPStorageFormat, MatCUSPFormatOperation
 65: @*/
 66: PetscErrorCode MatCUSPSetFormat(Mat A,MatCUSPFormatOperation op,MatCUSPStorageFormat format)
 67: {

 72:   PetscTryMethod(A, "MatCUSPSetFormat_C",(Mat,MatCUSPFormatOperation,MatCUSPStorageFormat),(A,op,format));
 73:   return(0);
 74: }

 76: PetscErrorCode MatSetFromOptions_SeqAIJCUSP(PetscOptionItems *PetscOptionsObject,Mat A)
 77: {
 78:   Mat_SeqAIJCUSP       *cuspMat = (Mat_SeqAIJCUSP*)A->spptr;
 79:   PetscErrorCode       ierr;
 80:   MatCUSPStorageFormat format;
 81:   PetscBool            flg;

 84:   PetscOptionsHead(PetscOptionsObject,"SeqAIJCUSP options");
 85:   PetscObjectOptionsBegin((PetscObject)A);
 86:   PetscOptionsEnum("-mat_cusp_mult_storage_format","sets storage format of (seq)aijcusp gpu matrices for SpMV",
 87:                           "MatCUSPSetFormat",MatCUSPStorageFormats,(PetscEnum)cuspMat->format,(PetscEnum*)&format,&flg);
 88:   if (flg) {
 89:     MatCUSPSetFormat(A,MAT_CUSP_MULT,format);
 90:   }
 91:   PetscOptionsEnum("-mat_cusp_storage_format","sets storage format of (seq)aijcusp gpu matrices for SpMV",
 92:                           "MatCUSPSetFormat",MatCUSPStorageFormats,(PetscEnum)cuspMat->format,(PetscEnum*)&format,&flg);
 93:   if (flg) {
 94:     MatCUSPSetFormat(A,MAT_CUSP_ALL,format);
 95:   }
 96:   PetscOptionsEnd();
 97:   return(0);

 99: }

101: PetscErrorCode MatCUSPCopyToGPU(Mat A)
102: {

104:   Mat_SeqAIJCUSP *cuspstruct = (Mat_SeqAIJCUSP*)A->spptr;
105:   Mat_SeqAIJ     *a          = (Mat_SeqAIJ*)A->data;
106:   PetscInt       m           = A->rmap->n,*ii,*ridx;
107:   CUSPMATRIX     *mat;

111:   if (A->valid_GPU_matrix == PETSC_CUSP_UNALLOCATED || A->valid_GPU_matrix == PETSC_CUSP_CPU) {
112:     PetscLogEventBegin(MAT_CUSPCopyToGPU,A,0,0,0);
113:     /*
114:       It may be possible to reuse nonzero structure with new matrix values but
115:       for simplicity and insured correctness we delete and build a new matrix on
116:       the GPU. Likely a very small performance hit.
117:     */
118:     if (cuspstruct->mat) {
119:       try {
120:         if (cuspstruct->format==MAT_CUSP_ELL)
121:           delete (CUSPMATRIXELL *) cuspstruct->mat;
122:         else if (cuspstruct->format==MAT_CUSP_DIA)
123:           delete (CUSPMATRIXDIA *) cuspstruct->mat;
124:         else
125:           delete (CUSPMATRIX *) cuspstruct->mat;
126:       } catch(char *ex) {
127:         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSP error: %s", ex);
128:       }
129:     }
130:     try {
131:       cuspstruct->nonzerorow=0;
132:       for (int j = 0; j<m; j++) cuspstruct->nonzerorow += ((a->i[j+1]-a->i[j])>0);
133:       if (a->compressedrow.use) {
134:         m    = a->compressedrow.nrows;
135:         ii   = a->compressedrow.i;
136:         ridx = a->compressedrow.rindex;
137:       } else {
138:         /* Forcing compressed row on the GPU */
139:         int k=0;
140:         PetscMalloc1(cuspstruct->nonzerorow+1, &ii);
141:         PetscMalloc1(cuspstruct->nonzerorow, &ridx);
142:         ii[0]=0;
143:         for (int j = 0; j<m; j++) {
144:           if ((a->i[j+1]-a->i[j])>0) {
145:             ii[k]  = a->i[j];
146:             ridx[k]= j;
147:             k++;
148:           }
149:         }
150:         ii[cuspstruct->nonzerorow] = a->nz;
151:         m = cuspstruct->nonzerorow;
152:       }

154:       /* now build matrix */
155:       mat = new CUSPMATRIX;
156:       mat->resize(m,A->cmap->n,a->nz);
157:       mat->row_offsets.assign(ii,ii+m+1);
158:       mat->column_indices.assign(a->j,a->j+a->nz);
159:       mat->values.assign(a->a,a->a+a->nz);

161:       /* convert to other formats if selected */
162:       if (a->compressedrow.use || cuspstruct->format==MAT_CUSP_CSR) {
163:         cuspstruct->mat = mat;
164:         cuspstruct->format = MAT_CUSP_CSR;
165:       } else {
166:         if (cuspstruct->format==MAT_CUSP_ELL) {
167:           CUSPMATRIXELL *ellMat = new CUSPMATRIXELL(*mat);
168:           cuspstruct->mat = ellMat;
169:         } else {
170:           CUSPMATRIXDIA *diaMat = new CUSPMATRIXDIA(*mat);
171:           cuspstruct->mat = diaMat;
172:         }
173:         delete (CUSPMATRIX*) mat;
174:       }

176:       /* assign the compressed row indices */
177:       if (cuspstruct->indices) delete (CUSPINTARRAYGPU*)cuspstruct->indices;
178:       cuspstruct->indices = new CUSPINTARRAYGPU;
179:       cuspstruct->indices->assign(ridx,ridx+m);

181:       /* free the temporaries */
182:       if (!a->compressedrow.use) {
183:         PetscFree(ii);
184:         PetscFree(ridx);
185:       }
186:       if (cuspstruct->tempvec) delete (CUSPARRAY*)cuspstruct->tempvec;
187:       cuspstruct->tempvec = new CUSPARRAY;
188:       cuspstruct->tempvec->resize(m);
189:     } catch(char *ex) {
190:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSP error: %s", ex);
191:     }
192:     WaitForGPU();CHKERRCUSP(ierr);

194:     A->valid_GPU_matrix = PETSC_CUSP_BOTH;

196:     PetscLogEventEnd(MAT_CUSPCopyToGPU,A,0,0,0);
197:   }
198:   return(0);
199: }

201: PetscErrorCode MatCUSPCopyFromGPU(Mat A, CUSPMATRIX *Agpu)
202: {
203:   Mat_SeqAIJCUSP *cuspstruct = (Mat_SeqAIJCUSP*) A->spptr;
204:   Mat_SeqAIJ     *a          = (Mat_SeqAIJ*) A->data;
205:   PetscInt       m           = A->rmap->n;
206:   CUSPMATRIX *mat;

210:   /* if the data is stored in non-CSR format, create a temporary */
211:   if (cuspstruct->format==MAT_CUSP_ELL) {
212:     mat = new CUSPMATRIX(*((CUSPMATRIXELL*)cuspstruct->mat));
213:   } else if (cuspstruct->format==MAT_CUSP_DIA) {
214:     mat = new CUSPMATRIX(*((CUSPMATRIXDIA*)cuspstruct->mat));
215:   } else {
216:     mat = (CUSPMATRIX*) cuspstruct->mat;
217:   }

219:   if (A->valid_GPU_matrix == PETSC_CUSP_UNALLOCATED) {
220:     if (A->valid_GPU_matrix == PETSC_CUSP_UNALLOCATED) {
221:       try {
222:         mat = Agpu;
223:         if (a->compressedrow.use) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Cannot handle row compression for GPU matrices");
224:         else {
225:           PetscInt i;

227:           if (m+1 != (PetscInt) mat->row_offsets.size()) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, "GPU matrix has %d rows, should be %d", mat->row_offsets.size()-1, m);
228:           a->nz           = mat->values.size();
229:           a->maxnz        = a->nz; /* Since we allocate exactly the right amount */
230:           A->preallocated = PETSC_TRUE;
231:           if (a->singlemalloc) {
232:             if (a->a) {PetscFree3(a->a,a->j,a->i);}
233:           } else {
234:             if (a->i) {PetscFree(a->i);}
235:             if (a->j) {PetscFree(a->j);}
236:             if (a->a) {PetscFree(a->a);}
237:           }
238:           PetscMalloc3(a->nz,&a->a,a->nz,&a->j,m+1,&a->i);
239:           PetscLogObjectMemory((PetscObject)A, a->nz*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));

241:           a->singlemalloc = PETSC_TRUE;
242:           thrust::copy(mat->row_offsets.begin(), mat->row_offsets.end(), a->i);
243:           thrust::copy(mat->column_indices.begin(), mat->column_indices.end(), a->j);
244:           thrust::copy(mat->values.begin(), mat->values.end(), a->a);
245:           /* Setup row lengths */
246:           if (a->imax) {PetscFree2(a->imax,a->ilen);}
247:           PetscMalloc2(m,&a->imax,m,&a->ilen);
248:           PetscLogObjectMemory((PetscObject)A, 2*m*sizeof(PetscInt));
249:           for (i = 0; i < m; ++i) a->imax[i] = a->ilen[i] = a->i[i+1] - a->i[i];
250:           /* a->diag?*/
251:         }
252:         cuspstruct->tempvec = new CUSPARRAY;
253:         cuspstruct->tempvec->resize(m);
254:       } catch(char *ex) {
255:         SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "CUSP error: %s", ex);
256:       }
257:     }
258:     /* This assembly prevents resetting the flag to PETSC_CUSP_CPU and recopying */
259:     MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
260:     MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);

262:     A->valid_GPU_matrix = PETSC_CUSP_BOTH;

264:     /* delete the temporary */
265:     if (cuspstruct->format==MAT_CUSP_ELL || cuspstruct->format==MAT_CUSP_DIA)
266:       delete (CUSPMATRIX*) mat;
267:   } else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Only valid for unallocated GPU matrices");
268:   return(0);
269: }

271: PetscErrorCode MatCreateVecs_SeqAIJCUSP(Mat mat, Vec *right, Vec *left)
272: {
274:   PetscInt rbs,cbs;

277:   MatGetBlockSizes(mat,&rbs,&cbs);
278:   if (right) {
279:     VecCreate(PetscObjectComm((PetscObject)mat),right);
280:     VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);
281:     VecSetBlockSize(*right,cbs);
282:     VecSetType(*right,VECSEQCUSP);
283:     PetscLayoutReference(mat->cmap,&(*right)->map);
284:   }
285:   if (left) {
286:     VecCreate(PetscObjectComm((PetscObject)mat),left);
287:     VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);
288:     VecSetBlockSize(*left,rbs);
289:     VecSetType(*left,VECSEQCUSP);
290:     PetscLayoutReference(mat->rmap,&(*left)->map);
291:   }
292:   return(0);
293: }

295: PetscErrorCode MatMult_SeqAIJCUSP(Mat A,Vec xx,Vec yy)
296: {
297:   Mat_SeqAIJ       *a = (Mat_SeqAIJ*)A->data;
298:   PetscErrorCode   ierr;
299:   Mat_SeqAIJCUSP   *cuspstruct = (Mat_SeqAIJCUSP*)A->spptr;
300:   PetscBool        usecprow = a->compressedrow.use;
301:   CUSPARRAY        *xarray=NULL,*yarray=NULL;
302:   static PetscBool cite = PETSC_FALSE;

305:   /*
306:     DM 1/28/2014: As of version 0.4.0 cusp does not handle the case of
307:     zero matrices well.  It produces segfaults on some platforms.
308:     Therefore we manually check for the case of a zero matrix here.
309:   */
310:   if (a->nz == 0) {
311:     return(0);
312:   }
313:   PetscCitationsRegister("@incollection{msk2013,\n  author = {Victor Minden and Barry F. Smith and Matthew G. Knepley},\n  title = {Preliminary Implementation of {PETSc} Using {GPUs}},\n  booktitle = {GPU Solutions to Multi-scale Problems in Science and Engineering},\n  series = {Lecture Notes in Earth System Sciences},\n  editor = {David A. Yuen and Long Wang and Xuebin Chi and Lennart Johnsson and Wei Ge and Yaolin Shi},\n  publisher = {Springer Berlin Heidelberg},\n  pages = {131--140},\n  year = {2013},\n}\n",&cite);
314:   VecCUSPGetArrayRead(xx,&xarray);
315:   VecCUSPGetArrayWrite(yy,&yarray);
316:   try {
317:     if (usecprow) {
318:       /* use compressed row format */
319:       CUSPMATRIX *mat = (CUSPMATRIX*)cuspstruct->mat;
320:       cusp::multiply(*mat,*xarray,*cuspstruct->tempvec);
321:       VecSet_SeqCUSP(yy,0.0);
322:       thrust::copy(cuspstruct->tempvec->begin(),cuspstruct->tempvec->end(),thrust::make_permutation_iterator(yarray->begin(),cuspstruct->indices->begin()));
323:     } else {
324:       /* do not use compressed row format */
325:       if (cuspstruct->format==MAT_CUSP_ELL) {
326:         CUSPMATRIXELL *mat = (CUSPMATRIXELL*)cuspstruct->mat;
327:         cusp::multiply(*mat,*xarray,*yarray);
328:       } else if (cuspstruct->format==MAT_CUSP_DIA) {
329:         CUSPMATRIXDIA *mat = (CUSPMATRIXDIA*)cuspstruct->mat;
330:         cusp::multiply(*mat,*xarray,*yarray);
331:       } else {
332:         CUSPMATRIX *mat = (CUSPMATRIX*)cuspstruct->mat;
333:         cusp::multiply(*mat,*xarray,*yarray);
334:       }
335:     }

337:   } catch (char *ex) {
338:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSP error: %s", ex);
339:   }
340:   VecCUSPRestoreArrayRead(xx,&xarray);
341:   VecCUSPRestoreArrayWrite(yy,&yarray);
342:   if (!cuspstruct->stream) {
343:     WaitForGPU();CHKERRCUSP(ierr);
344:   }
345:   PetscLogFlops(2.0*a->nz - cuspstruct->nonzerorow);
346:   return(0);
347: }


350: struct VecCUSPPlusEquals
351: {
352:   template <typename Tuple>
353:   __host__ __device__
354:   void operator()(Tuple t)
355:   {
356:     thrust::get<1>(t) = thrust::get<1>(t) + thrust::get<0>(t);
357:   }
358: };

360: PetscErrorCode MatMultAdd_SeqAIJCUSP(Mat A,Vec xx,Vec yy,Vec zz)
361: {
362:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
364:   PetscBool      usecprow = a->compressedrow.use;
365:   Mat_SeqAIJCUSP *cuspstruct = (Mat_SeqAIJCUSP*)A->spptr;
366:   CUSPARRAY      *xarray     = NULL,*yarray=NULL,*zarray=NULL;

369:   /*
370:     DM 1/28/2014: As of version 0.4.0 cusp does not handle the case of
371:     zero matrices well.  It produces segfaults on some platforms.
372:     Therefore we manually check for the case of a zero matrix here.
373:   */
374:   if (a->nz == 0) {
375:     return(0);
376:   }
377:   try {
378:     VecCopy_SeqCUSP(yy,zz);
379:     VecCUSPGetArrayRead(xx,&xarray);
380:     VecCUSPGetArrayRead(yy,&yarray);
381:     VecCUSPGetArrayWrite(zz,&zarray);

383:     if (usecprow) {
384:       /* use compressed row format */
385:       CUSPMATRIX *mat = (CUSPMATRIX*)cuspstruct->mat;
386:       cusp::multiply(*mat,*xarray,*cuspstruct->tempvec);
387:       thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cuspstruct->tempvec->begin(),
388:                                                                     thrust::make_permutation_iterator(zarray->begin(), cuspstruct->indices->begin()))),
389:                        thrust::make_zip_iterator(thrust::make_tuple(cuspstruct->tempvec->end(),
390:                                                                     thrust::make_permutation_iterator(zarray->end(),cuspstruct->indices->end()))),
391:                        VecCUSPPlusEquals());
392:     } else {

394:       if (cuspstruct->format==MAT_CUSP_ELL) {
395:         CUSPMATRIXELL *mat = (CUSPMATRIXELL*)cuspstruct->mat;
396:         cusp::multiply(*mat,*xarray,*cuspstruct->tempvec);
397:       } else if (cuspstruct->format==MAT_CUSP_DIA) {
398:         CUSPMATRIXDIA *mat = (CUSPMATRIXDIA*)cuspstruct->mat;
399:         cusp::multiply(*mat,*xarray,*cuspstruct->tempvec);
400:       } else {
401:         CUSPMATRIX *mat = (CUSPMATRIX*)cuspstruct->mat;
402:         cusp::multiply(*mat,*xarray,*cuspstruct->tempvec);
403:       }

405:       if (zarray->size() == cuspstruct->indices->size()) {
406:         thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cuspstruct->tempvec->begin(),zarray->begin())),
407:             thrust::make_zip_iterator(thrust::make_tuple(cuspstruct->tempvec->end(),zarray->end())),
408:             VecCUSPPlusEquals());
409:       } else {
410:         thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cuspstruct->tempvec->begin(),
411:                 thrust::make_permutation_iterator(zarray->begin(), cuspstruct->indices->begin()))),
412:             thrust::make_zip_iterator(thrust::make_tuple(cuspstruct->tempvec->end(),
413:                 thrust::make_permutation_iterator(zarray->end(),cuspstruct->indices->end()))),
414:             VecCUSPPlusEquals());
415:       }
416:     }
417:     VecCUSPRestoreArrayRead(xx,&xarray);
418:     VecCUSPRestoreArrayRead(yy,&yarray);
419:     VecCUSPRestoreArrayWrite(zz,&zarray);

421:   } catch(char *ex) {
422:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSP error: %s", ex);
423:   }
424:   WaitForGPU();CHKERRCUSP(ierr);
425:   PetscLogFlops(2.0*a->nz);
426:   return(0);
427: }

429: PetscErrorCode MatAssemblyEnd_SeqAIJCUSP(Mat A,MatAssemblyType mode)
430: {

434:   MatAssemblyEnd_SeqAIJ(A,mode);
435:   MatCUSPCopyToGPU(A);
436:   if (mode == MAT_FLUSH_ASSEMBLY) return(0);
437:   A->ops->mult    = MatMult_SeqAIJCUSP;
438:   A->ops->multadd = MatMultAdd_SeqAIJCUSP;
439:   return(0);
440: }

442: /* --------------------------------------------------------------------------------*/
443: /*@
444:    MatCreateSeqAIJCUSP - Creates a sparse matrix in AIJ (compressed row) format
445:    (the default parallel PETSc format).  This matrix will ultimately pushed down
446:    to NVidia GPUs and use the CUSP library for calculations. For good matrix
447:    assembly performance the user should preallocate the matrix storage by setting
448:    the parameter nz (or the array nnz).  By setting these parameters accurately,
449:    performance during matrix assembly can be increased by more than a factor of 50.


452:    Collective on MPI_Comm

454:    Input Parameters:
455: +  comm - MPI communicator, set to PETSC_COMM_SELF
456: .  m - number of rows
457: .  n - number of columns
458: .  nz - number of nonzeros per row (same for all rows)
459: -  nnz - array containing the number of nonzeros in the various rows
460:          (possibly different for each row) or NULL

462:    Output Parameter:
463: .  A - the matrix

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

469:    Notes:
470:    If nnz is given then nz is ignored

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

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

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

487:    Level: intermediate

489: .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ()

491: @*/
492: PetscErrorCode  MatCreateSeqAIJCUSP(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
493: {

497:   MatCreate(comm,A);
498:   MatSetSizes(*A,m,n,m,n);
499:   MatSetType(*A,MATSEQAIJCUSP);
500:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);
501:   return(0);
502: }

504: PetscErrorCode MatDestroy_SeqAIJCUSP(Mat A)
505: {
507:   Mat_SeqAIJCUSP *cuspcontainer = (Mat_SeqAIJCUSP*)A->spptr;

510:   try {
511:     if (A->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
512:       if (cuspcontainer->format==MAT_CUSP_ELL) {
513:         delete (CUSPMATRIXELL*)(cuspcontainer->mat);
514:       } else if (cuspcontainer->format==MAT_CUSP_DIA) {
515:         delete (CUSPMATRIXDIA*)(cuspcontainer->mat);
516:       } else {
517:         delete (CUSPMATRIX*)(cuspcontainer->mat);
518:       }
519:       if (cuspcontainer->indices) delete (CUSPINTARRAYGPU*)cuspcontainer->indices;
520:       if (cuspcontainer->tempvec) delete (CUSPARRAY*)cuspcontainer->tempvec;
521:     }
522:     delete cuspcontainer;
523:     A->valid_GPU_matrix = PETSC_CUSP_UNALLOCATED;
524:   } catch(char *ex) {
525:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSP error: %s", ex);
526:   }
527:   /*this next line is because MatDestroy tries to PetscFree spptr if it is not zero, and PetscFree only works if the memory was allocated with PetscNew or PetscMalloc, which don't call the constructor */
528:   A->spptr = 0;
529:   MatDestroy_SeqAIJ(A);
530:   return(0);
531: }

533: extern PetscErrorCode MatSetValuesBatch_SeqAIJCUSP(Mat, PetscInt, PetscInt, PetscInt*,const PetscScalar*);

535: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJCUSP(Mat B)
536: {
538:   Mat_SeqAIJ     *aij;

541:   MatCreate_SeqAIJ(B);
542:   aij             = (Mat_SeqAIJ*)B->data;
543:   aij->inode.use  = PETSC_FALSE;
544:   B->ops->mult    = MatMult_SeqAIJCUSP;
545:   B->ops->multadd = MatMultAdd_SeqAIJCUSP;
546:   B->spptr        = new Mat_SeqAIJCUSP;

548:   if (B->factortype==MAT_FACTOR_NONE) {
549:     ((Mat_SeqAIJCUSP*)B->spptr)->mat     = 0;
550:     ((Mat_SeqAIJCUSP*)B->spptr)->tempvec = 0;
551:     ((Mat_SeqAIJCUSP*)B->spptr)->indices = 0;
552:     ((Mat_SeqAIJCUSP*)B->spptr)->nonzerorow = 0;
553:     ((Mat_SeqAIJCUSP*)B->spptr)->stream = 0;
554:     ((Mat_SeqAIJCUSP*)B->spptr)->format = MAT_CUSP_CSR;
555:   }

557:   B->ops->assemblyend    = MatAssemblyEnd_SeqAIJCUSP;
558:   B->ops->destroy        = MatDestroy_SeqAIJCUSP;
559:   B->ops->getvecs        = MatCreateVecs_SeqAIJCUSP;
560:   B->ops->setvaluesbatch = MatSetValuesBatch_SeqAIJCUSP;
561:   B->ops->setfromoptions = MatSetFromOptions_SeqAIJCUSP;

563:   PetscObjectComposeFunction((PetscObject)B,"MatCUSPSetFormat_C", MatCUSPSetFormat_SeqAIJCUSP);
564:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJCUSP);

566:   B->valid_GPU_matrix = PETSC_CUSP_UNALLOCATED;
567:   return(0);
568: }

570: /*M
571:    MATSEQAIJCUSP - MATAIJCUSP = "aijcusp" = "seqaijcusp" - A matrix type to be used for sparse matrices.

573:    A matrix type type whose data resides on Nvidia GPUs. These matrices are in CSR format by
574:    default. All matrix calculations are performed using the CUSP library. DIA and ELL formats are 
575:    also available.

577:    Options Database Keys:
578: +  -mat_type aijcusp - sets the matrix type to "seqaijcusp" during a call to MatSetFromOptions()
579: .  -mat_cusp_storage_format csr - sets the storage format of matrices for MatMult during a call to MatSetFromOptions(). Other options include dia (diagonal) or ell (ellpack).
580: -  -mat_cusp_mult_storage_format csr - sets the storage format of matrices for MatMult during a call to MatSetFromOptions(). Other options include dia (diagonal) or ell (ellpack).

582:   Level: beginner

584: .seealso: MatCreateSeqAIJCUSP(), MATAIJCUSP, MatCreateAIJCUSP(), MatCUSPSetFormat(), MatCUSPStorageFormat, MatCUSPFormatOperation
585: M*/
586: PETSC_EXTERN PetscErrorCode MatSolverPackageRegister_CUSP(void)
587: {

591:   MatSolverPackageRegister(MATSOLVERPETSC, MATSEQAIJCUSP,    MAT_FACTOR_LU,MatGetFactor_seqaij_petsc);
592:   MatSolverPackageRegister(MATSOLVERPETSC, MATSEQAIJCUSP,    MAT_FACTOR_CHOLESKY,MatGetFactor_seqaij_petsc);
593:   MatSolverPackageRegister(MATSOLVERPETSC, MATSEQAIJCUSP,    MAT_FACTOR_ILU,MatGetFactor_seqaij_petsc);
594:   MatSolverPackageRegister(MATSOLVERPETSC, MATSEQAIJCUSP,    MAT_FACTOR_ICC,MatGetFactor_seqaij_petsc);
595:   return(0);
596: }