Actual source code: aijcusp.cu

petsc-3.5.4 2015-05-23
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: {
290:   PetscInt rbs,cbs;

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

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

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

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


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

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

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

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

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

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

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

439: PetscErrorCode MatAssemblyEnd_SeqAIJCUSP(Mat A,MatAssemblyType mode)
440: {

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

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


464:    Collective on MPI_Comm

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

474:    Output Parameter:
475: .  A - the matrix

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

481:    Notes:
482:    If nnz is given then nz is ignored

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

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

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

499:    Level: intermediate

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

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

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

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

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

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

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

556:   if (idx) {
557:   }
558:   return(0);
559: }*/

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

563: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_cusparse(Mat,MatFactorType,Mat*);
564: PETSC_EXTERN PetscErrorCode MatFactorGetSolverPackage_seqaij_cusparse(Mat,const MatSolverPackage*);

568: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJCUSP(Mat B)
569: {
571:   Mat_SeqAIJ     *aij;

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

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

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

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

603:   B->valid_GPU_matrix = PETSC_CUSP_UNALLOCATED;
604:   return(0);
605: }

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

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

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

619:   Level: beginner

621: .seealso: MatCreateSeqAIJCUSP(), MATAIJCUSP, MatCreateAIJCUSP(), MatCUSPSetFormat(), MatCUSPStorageFormat, MatCUSPFormatOperation
622: M*/