Actual source code: aijcusp.cu

petsc-dev 2014-02-02
Report Typos and Errors
  1: /*
  2:   Defines the basic matrix operations for the AIJ (compressed row)
  3:   matrix storage format.
  4: */

  6: #include <petscconf.h>
  7: PETSC_CUDA_EXTERN_C_BEGIN
  8: #include <../src/mat/impls/aij/seq/aij.h>          /*I "petscmat.h" I*/
  9: #include <petscbt.h>
 10: #include <../src/vec/vec/impls/dvecimpl.h>
 11: #include <petsc-private/vecimpl.h>
 12: PETSC_CUDA_EXTERN_C_END
 13: #undef VecType
 14: #include <../src/mat/impls/aij/seq/seqcusp/cuspmatimpl.h>

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

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

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

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

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

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

 54:    Not Collective

 56:    Input Parameters:
 57: +  A - Matrix of type SEQAIJCUSP
 58: .  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.
 59: -  format - MatCUSPStorageFormat (one of MAT_CUSP_CSR, MAT_CUSP_DIA, MAT_CUSP_ELL)

 61:    Output Parameter:

 63:    Level: intermediate

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

 75:   PetscTryMethod(A, "MatCUSPSetFormat_C",(Mat,MatCUSPFormatOperation,MatCUSPStorageFormat),(A,op,format));
 76:   return(0);
 77: }

 81: PetscErrorCode MatSetFromOptions_SeqAIJCUSP(Mat A)
 82: {
 83:   PetscErrorCode       ierr;
 84:   MatCUSPStorageFormat format;
 85:   PetscBool            flg;

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

103: }


108: PetscErrorCode MatCUSPCopyToGPU(Mat A)
109: {

111:   Mat_SeqAIJCUSP *cuspstruct = (Mat_SeqAIJCUSP*)A->spptr;
112:   Mat_SeqAIJ     *a          = (Mat_SeqAIJ*)A->data;
113:   PetscInt       m           = A->rmap->n,*ii,*ridx;
114:   CUSPMATRIX     *mat;

118:   if (A->valid_GPU_matrix == PETSC_CUSP_UNALLOCATED || A->valid_GPU_matrix == PETSC_CUSP_CPU) {
119:     PetscLogEventBegin(MAT_CUSPCopyToGPU,A,0,0,0);
120:     /*
121:       It may be possible to reuse nonzero structure with new matrix values but
122:       for simplicity and insured correctness we delete and build a new matrix on
123:       the GPU. Likely a very small performance hit.
124:     */
125:     if (cuspstruct->mat) {
126:       try {
127:         if (cuspstruct->format==MAT_CUSP_ELL)
128:           delete (CUSPMATRIXELL *) cuspstruct->mat;
129:         else if (cuspstruct->format==MAT_CUSP_DIA)
130:           delete (CUSPMATRIXDIA *) cuspstruct->mat;
131:         else
132:           delete (CUSPMATRIX *) cuspstruct->mat;

134:       } catch(char *ex) {
135:         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSP error: %s", ex);
136:       }
137:     }
138:     try {
139:       cuspstruct->nonzerorow=0;
140:       for (int j = 0; j<m; j++) cuspstruct->nonzerorow += ((a->i[j+1]-a->i[j])>0);

142:       if (a->compressedrow.use) {
143:         m    = a->compressedrow.nrows;
144:         ii   = a->compressedrow.i;
145:         ridx = a->compressedrow.rindex;
146:       } else {
147:         /* Forcing compressed row on the GPU */
148:         int k=0;
149:         PetscMalloc1((cuspstruct->nonzerorow+1), &ii);
150:         PetscMalloc1((cuspstruct->nonzerorow), &ridx);
151:         ii[0]=0;
152:         for (int j = 0; j<m; j++) {
153:           if ((a->i[j+1]-a->i[j])>0) {
154:             ii[k]  = a->i[j];
155:             ridx[k]= j;
156:             k++;
157:           }
158:         }
159:         ii[cuspstruct->nonzerorow] = a->nz;
160:         m = cuspstruct->nonzerorow;
161:       }

163:       /* now build matrix */
164:       mat = new CUSPMATRIX;
165:       mat->resize(m,A->cmap->n,a->nz);
166:       mat->row_offsets.assign(ii,ii+m+1);
167:       mat->column_indices.assign(a->j,a->j+a->nz);
168:       mat->values.assign(a->a,a->a+a->nz);

170:       /* convert to other formats if selected */
171:       if (a->compressedrow.use || cuspstruct->format==MAT_CUSP_CSR) {
172:         cuspstruct->mat = mat;
173:         cuspstruct->format = MAT_CUSP_CSR;
174:       } else {
175:         if (cuspstruct->format==MAT_CUSP_ELL) {
176:           CUSPMATRIXELL *ellMat = new CUSPMATRIXELL(*mat);
177:           cuspstruct->mat = ellMat;
178:         } else {
179:           CUSPMATRIXDIA *diaMat = new CUSPMATRIXDIA(*mat);
180:           cuspstruct->mat = diaMat;
181:         }
182:         delete (CUSPMATRIX*) mat;
183:       }

185:       /* assign the compressed row indices */
186:       if (cuspstruct->indices) delete (CUSPINTARRAYGPU*)cuspstruct->indices;
187:       cuspstruct->indices = new CUSPINTARRAYGPU;
188:       cuspstruct->indices->assign(ridx,ridx+m);

190:       /* free the temporaries */
191:       if (!a->compressedrow.use) {
192:         PetscFree(ii);
193:         PetscFree(ridx);
194:       }
195:       if (cuspstruct->tempvec) delete (CUSPARRAY*)cuspstruct->tempvec;
196:       cuspstruct->tempvec = new CUSPARRAY;
197:       cuspstruct->tempvec->resize(m);
198:     } catch(char *ex) {
199:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSP error: %s", ex);
200:     }
201:     WaitForGPU();CHKERRCUSP(ierr);

203:     A->valid_GPU_matrix = PETSC_CUSP_BOTH;

205:     PetscLogEventEnd(MAT_CUSPCopyToGPU,A,0,0,0);
206:   }
207:   return(0);
208: }

212: PetscErrorCode MatCUSPCopyFromGPU(Mat A, CUSPMATRIX *Agpu)
213: {
214:   Mat_SeqAIJCUSP *cuspstruct = (Mat_SeqAIJCUSP*) A->spptr;
215:   Mat_SeqAIJ     *a          = (Mat_SeqAIJ*) A->data;
216:   PetscInt       m           = A->rmap->n;
217:   CUSPMATRIX *mat;

221:   /* if the data is stored in non-CSR format, create a temporary */
222:   if (cuspstruct->format==MAT_CUSP_ELL) {
223:     mat = new CUSPMATRIX(*((CUSPMATRIXELL*)cuspstruct->mat));
224:   } else if (cuspstruct->format==MAT_CUSP_DIA) {
225:     mat = new CUSPMATRIX(*((CUSPMATRIXDIA*)cuspstruct->mat));
226:   } else {
227:     mat = (CUSPMATRIX*) cuspstruct->mat;
228:   }

230:   if (A->valid_GPU_matrix == PETSC_CUSP_UNALLOCATED) {
231:     if (A->valid_GPU_matrix == PETSC_CUSP_UNALLOCATED) {
232:       try {
233:         mat = Agpu;
234:         if (a->compressedrow.use) {
235:           SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Cannot handle row compression for GPU matrices");
236:         } else {
237:           PetscInt i;

239:           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);
240:           a->nz           = mat->values.size();
241:           a->maxnz        = a->nz; /* Since we allocate exactly the right amount */
242:           A->preallocated = PETSC_TRUE;
243:           if (a->singlemalloc) {
244:             if (a->a) {PetscFree3(a->a,a->j,a->i);}
245:           } else {
246:             if (a->i) {PetscFree(a->i);}
247:             if (a->j) {PetscFree(a->j);}
248:             if (a->a) {PetscFree(a->a);}
249:           }
250:           PetscMalloc3(a->nz,&a->a,a->nz,&a->j,m+1,&a->i);
251:           PetscLogObjectMemory((PetscObject)A, a->nz*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));

253:           a->singlemalloc = PETSC_TRUE;
254:           thrust::copy(mat->row_offsets.begin(), mat->row_offsets.end(), a->i);
255:           thrust::copy(mat->column_indices.begin(), mat->column_indices.end(), a->j);
256:           thrust::copy(mat->values.begin(), mat->values.end(), a->a);
257:           /* Setup row lengths */
258:           if (a->imax) {PetscFree2(a->imax,a->ilen);}
259:           PetscMalloc2(m,&a->imax,m,&a->ilen);
260:           PetscLogObjectMemory((PetscObject)A, 2*m*sizeof(PetscInt));
261:           for (i = 0; i < m; ++i) a->imax[i] = a->ilen[i] = a->i[i+1] - a->i[i];
262:           /* a->diag?*/
263:         }
264:         cuspstruct->tempvec = new CUSPARRAY;
265:         cuspstruct->tempvec->resize(m);
266:       } catch(char *ex) {
267:         SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "CUSP error: %s", ex);
268:       }
269:     }
270:     /* This assembly prevents resetting the flag to PETSC_CUSP_CPU and recopying */
271:     MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
272:     MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);

274:     A->valid_GPU_matrix = PETSC_CUSP_BOTH;

276:     /* delete the temporary */
277:     if (cuspstruct->format==MAT_CUSP_ELL || cuspstruct->format==MAT_CUSP_DIA)
278:       delete (CUSPMATRIX*) mat;
279:   } else {
280:     SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Only valid for unallocated GPU matrices");
281:   }
282:   return(0);
283: }

287: PetscErrorCode MatGetVecs_SeqAIJCUSP(Mat mat, Vec *right, Vec *left)
288: {

292:   if (right) {
293:     VecCreate(PetscObjectComm((PetscObject)mat),right);
294:     VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);
295:     VecSetBlockSize(*right,mat->rmap->bs);
296:     VecSetType(*right,VECSEQCUSP);
297:     PetscLayoutReference(mat->cmap,&(*right)->map);
298:   }
299:   if (left) {
300:     VecCreate(PetscObjectComm((PetscObject)mat),left);
301:     VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);
302:     VecSetBlockSize(*left,mat->rmap->bs);
303:     VecSetType(*left,VECSEQCUSP);
304:     PetscLayoutReference(mat->rmap,&(*left)->map);
305:   }
306:   return(0);
307: }

311: PetscErrorCode MatMult_SeqAIJCUSP(Mat A,Vec xx,Vec yy)
312: {
313:   Mat_SeqAIJ       *a = (Mat_SeqAIJ*)A->data;
314:   PetscErrorCode   ierr;
315:   Mat_SeqAIJCUSP   *cuspstruct = (Mat_SeqAIJCUSP*)A->spptr;
316:   PetscBool        usecprow = a->compressedrow.use;
317:   CUSPARRAY        *xarray=NULL,*yarray=NULL;
318:   static PetscBool cite = PETSC_FALSE;

321:   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);
322:   /* The line below should not be necessary as it has been moved to MatAssemblyEnd_SeqAIJCUSP
323:      MatCUSPCopyToGPU(A); */
324:   VecCUSPGetArrayRead(xx,&xarray);
325:   VecCUSPGetArrayWrite(yy,&yarray);
326:   try {
327:     if (usecprow) {
328:       /* use compressed row format */
329:       CUSPMATRIX *mat = (CUSPMATRIX*)cuspstruct->mat;
330:       cusp::multiply(*mat,*xarray,*cuspstruct->tempvec);
331:       VecSet_SeqCUSP(yy,0.0);
332:       thrust::copy(cuspstruct->tempvec->begin(),cuspstruct->tempvec->end(),thrust::make_permutation_iterator(yarray->begin(),cuspstruct->indices->begin()));
333:     } else {
334:       /* do not use compressed row format */
335:       if (cuspstruct->format==MAT_CUSP_ELL) {
336:         CUSPMATRIXELL *mat = (CUSPMATRIXELL*)cuspstruct->mat;
337:         cusp::multiply(*mat,*xarray,*yarray);
338:       } else if (cuspstruct->format==MAT_CUSP_DIA) {
339:         CUSPMATRIXDIA *mat = (CUSPMATRIXDIA*)cuspstruct->mat;
340:         cusp::multiply(*mat,*xarray,*yarray);
341:       } else {
342:         CUSPMATRIX *mat = (CUSPMATRIX*)cuspstruct->mat;
343:         cusp::multiply(*mat,*xarray,*yarray);
344:       }
345:     }

347:   } catch (char *ex) {
348:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSP error: %s", ex);
349:   }
350:   VecCUSPRestoreArrayRead(xx,&xarray);
351:   VecCUSPRestoreArrayWrite(yy,&yarray);
352:   if (!cuspstruct->stream) {
353:     WaitForGPU();CHKERRCUSP(ierr);
354:   }
355:   PetscLogFlops(2.0*a->nz - cuspstruct->nonzerorow);
356:   return(0);
357: }


360: struct VecCUSPPlusEquals
361: {
362:   template <typename Tuple>
363:   __host__ __device__
364:   void operator()(Tuple t)
365:   {
366:     thrust::get<1>(t) = thrust::get<1>(t) + thrust::get<0>(t);
367:   }
368: };

372: PetscErrorCode MatMultAdd_SeqAIJCUSP(Mat A,Vec xx,Vec yy,Vec zz)
373: {
374:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
376:   PetscBool      usecprow = a->compressedrow.use;
377:   Mat_SeqAIJCUSP *cuspstruct = (Mat_SeqAIJCUSP*)A->spptr;
378:   CUSPARRAY      *xarray     = NULL,*yarray=NULL,*zarray=NULL;

381:   /* The line below should not be necessary as it has been moved to MatAssemblyEnd_SeqAIJCUSP
382:      MatCUSPCopyToGPU(A); */
383:   try {
384:     VecCopy_SeqCUSP(yy,zz);
385:     VecCUSPGetArrayRead(xx,&xarray);
386:     VecCUSPGetArrayRead(yy,&yarray);
387:     VecCUSPGetArrayWrite(zz,&zarray);

389:     if (usecprow) {
390:       /* use compressed row format */
391:       CUSPMATRIX *mat = (CUSPMATRIX*)cuspstruct->mat;
392:       cusp::multiply(*mat,*xarray,*cuspstruct->tempvec);
393:       thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cuspstruct->tempvec->begin(),
394:                                                                     thrust::make_permutation_iterator(zarray->begin(), cuspstruct->indices->begin()))),
395:                        thrust::make_zip_iterator(thrust::make_tuple(cuspstruct->tempvec->end(),
396:                                                                     thrust::make_permutation_iterator(zarray->end(),cuspstruct->indices->end()))),
397:                        VecCUSPPlusEquals());
398:     } else {

400:       if (cuspstruct->format==MAT_CUSP_ELL) {
401:         CUSPMATRIXELL *mat = (CUSPMATRIXELL*)cuspstruct->mat;
402:         cusp::multiply(*mat,*xarray,*cuspstruct->tempvec);
403:       } else if (cuspstruct->format==MAT_CUSP_DIA) {
404:         CUSPMATRIXDIA *mat = (CUSPMATRIXDIA*)cuspstruct->mat;
405:         cusp::multiply(*mat,*xarray,*cuspstruct->tempvec);
406:       } else {
407:         CUSPMATRIX *mat = (CUSPMATRIX*)cuspstruct->mat;
408:         cusp::multiply(*mat,*xarray,*cuspstruct->tempvec);
409:       }

411:       if (zarray->size() == cuspstruct->indices->size()) {
412:         thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cuspstruct->tempvec->begin(),zarray->begin())),
413:                          thrust::make_zip_iterator(thrust::make_tuple(cuspstruct->tempvec->end(),zarray->end())),
414:                          VecCUSPPlusEquals());
415:       } else {
416:         thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cuspstruct->tempvec->begin(),
417:                              thrust::make_permutation_iterator(zarray->begin(), cuspstruct->indices->begin()))),
418:                          thrust::make_zip_iterator(thrust::make_tuple(cuspstruct->tempvec->end(),
419:                              thrust::make_permutation_iterator(zarray->end(),cuspstruct->indices->end()))),
420:                          VecCUSPPlusEquals());
421:       }
422:     }
423:     VecCUSPRestoreArrayRead(xx,&xarray);
424:     VecCUSPRestoreArrayRead(yy,&yarray);
425:     VecCUSPRestoreArrayWrite(zz,&zarray);

427:   } catch(char *ex) {
428:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSP error: %s", ex);
429:   }
430:   WaitForGPU();CHKERRCUSP(ierr);
431:   PetscLogFlops(2.0*a->nz);
432:   return(0);
433: }

437: PetscErrorCode MatAssemblyEnd_SeqAIJCUSP(Mat A,MatAssemblyType mode)
438: {

442:   MatAssemblyEnd_SeqAIJ(A,mode);
443:   MatCUSPCopyToGPU(A);
444:   if (mode == MAT_FLUSH_ASSEMBLY) return(0);
445:   A->ops->mult    = MatMult_SeqAIJCUSP;
446:   A->ops->multadd = MatMultAdd_SeqAIJCUSP;
447:   return(0);
448: }

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


462:    Collective on MPI_Comm

464:    Input Parameters:
465: +  comm - MPI communicator, set to PETSC_COMM_SELF
466: .  m - number of rows
467: .  n - number of columns
468: .  nz - number of nonzeros per row (same for all rows)
469: -  nnz - array containing the number of nonzeros in the various rows
470:          (possibly different for each row) or NULL

472:    Output Parameter:
473: .  A - the matrix

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

479:    Notes:
480:    If nnz is given then nz is ignored

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

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

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

497:    Level: intermediate

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

501: @*/
502: PetscErrorCode  MatCreateSeqAIJCUSP(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
503: {

507:   MatCreate(comm,A);
508:   MatSetSizes(*A,m,n,m,n);
509:   MatSetType(*A,MATSEQAIJCUSP);
510:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);
511:   return(0);
512: }

516: PetscErrorCode MatDestroy_SeqAIJCUSP(Mat A)
517: {
519:   Mat_SeqAIJCUSP *cuspcontainer = (Mat_SeqAIJCUSP*)A->spptr;

522:   try {
523:     if (A->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
524:       if (cuspcontainer->format==MAT_CUSP_ELL)
525:         delete (CUSPMATRIXELL*)(cuspcontainer->mat);
526:       else if (cuspcontainer->format==MAT_CUSP_DIA)
527:         delete (CUSPMATRIXDIA*)(cuspcontainer->mat);
528:       else
529:         delete (CUSPMATRIX*)(cuspcontainer->mat);

531:       if (cuspcontainer->indices) delete (CUSPINTARRAYGPU*)cuspcontainer->indices;
532:       if (cuspcontainer->tempvec) delete (CUSPARRAY*)cuspcontainer->tempvec;
533:     }
534:     delete cuspcontainer;
535:     A->valid_GPU_matrix = PETSC_CUSP_UNALLOCATED;
536:   } catch(char *ex) {
537:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSP error: %s", ex);
538:   }
539:   /*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 */
540:   A->spptr = 0;
541:   MatDestroy_SeqAIJ(A);
542:   return(0);
543: }

545: /*
548: PetscErrorCode MatCreateSeqAIJCUSPFromTriple(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt* i, PetscInt* j, PetscScalar*a, Mat *mat, PetscInt nz, PetscBool idx)
549: {
550:   CUSPMATRIX *gpucsr;

554:   if (idx) {
555:   }
556:   return(0);
557: }*/

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

561: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_cusparse(Mat,MatFactorType,Mat*);
562: PETSC_EXTERN PetscErrorCode MatFactorGetSolverPackage_seqaij_cusparse(Mat,const MatSolverPackage*);

566: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJCUSP(Mat B)
567: {
569:   Mat_SeqAIJ     *aij;

572:   MatCreate_SeqAIJ(B);
573:   aij             = (Mat_SeqAIJ*)B->data;
574:   aij->inode.use  = PETSC_FALSE;
575:   B->ops->mult    = MatMult_SeqAIJCUSP;
576:   B->ops->multadd = MatMultAdd_SeqAIJCUSP;
577:   B->spptr        = new Mat_SeqAIJCUSP;

579:   if (B->factortype==MAT_FACTOR_NONE) {
580:     ((Mat_SeqAIJCUSP*)B->spptr)->mat     = 0;
581:     ((Mat_SeqAIJCUSP*)B->spptr)->tempvec = 0;
582:     ((Mat_SeqAIJCUSP*)B->spptr)->indices = 0;
583:     ((Mat_SeqAIJCUSP*)B->spptr)->nonzerorow = 0;
584:     ((Mat_SeqAIJCUSP*)B->spptr)->stream = 0;
585:     ((Mat_SeqAIJCUSP*)B->spptr)->format = MAT_CUSP_CSR;
586:   }

588:   B->ops->assemblyend    = MatAssemblyEnd_SeqAIJCUSP;
589:   B->ops->destroy        = MatDestroy_SeqAIJCUSP;
590:   B->ops->getvecs        = MatGetVecs_SeqAIJCUSP;
591:   B->ops->setvaluesbatch = MatSetValuesBatch_SeqAIJCUSP;
592:   B->ops->setfromoptions = MatSetFromOptions_SeqAIJCUSP;

594:   /* Here we overload MatGetFactor_cusparse_C which enables -pc_factor_mat_solver_package cusparse to work with
595:      -mat_type aijcusp. That is, an aijcusp matrix can call the cusparse tri solve.
596:      Note the difference with the implementation in MatCreate_SeqAIJCUSPARSE in ../seqcusparse/aijcusparse.cu */
597:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_cusparse_C",MatGetFactor_seqaij_cusparse);
598:   PetscObjectComposeFunction((PetscObject)B,"MatCUSPSetFormat_C", MatCUSPSetFormat_SeqAIJCUSP);
599:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJCUSP);

601:   B->valid_GPU_matrix = PETSC_CUSP_UNALLOCATED;
602:   return(0);
603: }

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

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

612:    Options Database Keys:
613: +  -mat_type aijcusp - sets the matrix type to "seqaijcusp" during a call to MatSetFromOptions()
614: .  -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).
615: -  -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).

617:   Level: beginner

619: .seealso: MatCreateSeqAIJCUSP(), MATAIJCUSP, MatCreateAIJCUSP(), MatCUSPSetFormat(), MatCUSPStorageFormat, MatCUSPFormatOperation
620: M*/