Actual source code: aijviennaclcuda.cu

petsc-3.8.4 2018-03-24
Report Typos and Errors


  3: /*
  4:     Defines the basic matrix operations for the AIJ (compressed row)
  5:   matrix storage format.
  6: */

  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>

 14:  #include <../src/mat/impls/aij/seq/seqviennacl/viennaclmatimpl.h>


 17: #include <algorithm>
 18: #include <vector>
 19: #include <string>

 21: #include "viennacl/linalg/prod.hpp"

 23: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJViennaCL(Mat A, MatType type, MatReuse reuse, Mat *newmat);
 24: PETSC_INTERN PetscErrorCode MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);


 27: PetscErrorCode MatViennaCLCopyToGPU(Mat A)
 28: {

 30:   Mat_SeqAIJViennaCL *viennaclstruct = (Mat_SeqAIJViennaCL*)A->spptr;
 31:   Mat_SeqAIJ         *a              = (Mat_SeqAIJ*)A->data;
 32:   PetscErrorCode     ierr;


 36:   if (A->rmap->n > 0 && A->cmap->n > 0) { //some OpenCL SDKs have issues with buffers of size 0
 37:     if (A->valid_GPU_matrix == PETSC_VIENNACL_UNALLOCATED || A->valid_GPU_matrix == PETSC_VIENNACL_CPU) {
 38:       PetscLogEventBegin(MAT_ViennaCLCopyToGPU,A,0,0,0);

 40:       try {
 41:         if (a->compressedrow.use) {
 42:           if (!viennaclstruct->compressed_mat) viennaclstruct->compressed_mat = new ViennaCLCompressedAIJMatrix();

 44:           // Since PetscInt is different from cl_uint, we have to convert:
 45:           viennacl::backend::mem_handle dummy;

 47:           viennacl::backend::typesafe_host_array<unsigned int> row_buffer; row_buffer.raw_resize(dummy, a->compressedrow.nrows+1);
 48:           for (PetscInt i=0; i<=a->compressedrow.nrows; ++i)
 49:             row_buffer.set(i, (a->compressedrow.i)[i]);

 51:           viennacl::backend::typesafe_host_array<unsigned int> row_indices; row_indices.raw_resize(dummy, a->compressedrow.nrows);
 52:           for (PetscInt i=0; i<a->compressedrow.nrows; ++i)
 53:             row_indices.set(i, (a->compressedrow.rindex)[i]);

 55:           viennacl::backend::typesafe_host_array<unsigned int> col_buffer; col_buffer.raw_resize(dummy, a->nz);
 56:           for (PetscInt i=0; i<a->nz; ++i)
 57:             col_buffer.set(i, (a->j)[i]);

 59:           viennaclstruct->compressed_mat->set(row_buffer.get(), row_indices.get(), col_buffer.get(), a->a, A->rmap->n, A->cmap->n, a->compressedrow.nrows, a->nz);
 60:         } else {
 61:           if (!viennaclstruct->mat) viennaclstruct->mat = new ViennaCLAIJMatrix();

 63:           // Since PetscInt is in general different from cl_uint, we have to convert:
 64:           viennacl::backend::mem_handle dummy;

 66:           viennacl::backend::typesafe_host_array<unsigned int> row_buffer; row_buffer.raw_resize(dummy, A->rmap->n+1);
 67:           for (PetscInt i=0; i<=A->rmap->n; ++i)
 68:             row_buffer.set(i, (a->i)[i]);

 70:           viennacl::backend::typesafe_host_array<unsigned int> col_buffer; col_buffer.raw_resize(dummy, a->nz);
 71:           for (PetscInt i=0; i<a->nz; ++i)
 72:             col_buffer.set(i, (a->j)[i]);

 74:           viennaclstruct->mat->set(row_buffer.get(), col_buffer.get(), a->a, A->rmap->n, A->cmap->n, a->nz);
 75:         }
 76:         ViennaCLWaitForGPU();
 77:       } catch(std::exception const & ex) {
 78:         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
 79:       }

 81:       // Create temporary vector for v += A*x:
 82:       if (viennaclstruct->tempvec) {
 83:         if (viennaclstruct->tempvec->size() != static_cast<std::size_t>(A->rmap->n)) {
 84:           delete (ViennaCLVector*)viennaclstruct->tempvec;
 85:           viennaclstruct->tempvec = new ViennaCLVector(A->rmap->n);
 86:         } else {
 87:           viennaclstruct->tempvec->clear();
 88:         }
 89:       } else {
 90:         viennaclstruct->tempvec = new ViennaCLVector(A->rmap->n);
 91:       }

 93:       A->valid_GPU_matrix = PETSC_VIENNACL_BOTH;

 95:       PetscLogEventEnd(MAT_ViennaCLCopyToGPU,A,0,0,0);
 96:     }
 97:   }
 98:   return(0);
 99: }

101: PetscErrorCode MatViennaCLCopyFromGPU(Mat A, const ViennaCLAIJMatrix *Agpu)
102: {
103:   Mat_SeqAIJ         *a              = (Mat_SeqAIJ*)A->data;
104:   PetscInt           m               = A->rmap->n;
105:   PetscErrorCode     ierr;


109:   if (A->valid_GPU_matrix == PETSC_VIENNACL_UNALLOCATED) {
110:     try {
111:       if (a->compressedrow.use) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "ViennaCL: Cannot handle row compression for GPU matrices");
112:       else {

114:         if ((PetscInt)Agpu->size1() != m) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, "GPU matrix has %d rows, should be %d", Agpu->size1(), m);
115:         a->nz           = Agpu->nnz();
116:         a->maxnz        = a->nz; /* Since we allocate exactly the right amount */
117:         A->preallocated = PETSC_TRUE;
118:         if (a->singlemalloc) {
119:           if (a->a) {PetscFree3(a->a,a->j,a->i);}
120:         } else {
121:           if (a->i) {PetscFree(a->i);}
122:           if (a->j) {PetscFree(a->j);}
123:           if (a->a) {PetscFree(a->a);}
124:         }
125:         PetscMalloc3(a->nz,&a->a,a->nz,&a->j,m+1,&a->i);
126:         PetscLogObjectMemory((PetscObject)A, a->nz*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));

128:         a->singlemalloc = PETSC_TRUE;

130:         /* Setup row lengths */
131:         if (a->imax) {PetscFree2(a->imax,a->ilen);}
132:         PetscMalloc2(m,&a->imax,m,&a->ilen);
133:         PetscLogObjectMemory((PetscObject)A, 2*m*sizeof(PetscInt));

135:         /* Copy data back from GPU */
136:         viennacl::backend::typesafe_host_array<unsigned int> row_buffer; row_buffer.raw_resize(Agpu->handle1(), Agpu->size1() + 1);

138:         // copy row array
139:         viennacl::backend::memory_read(Agpu->handle1(), 0, row_buffer.raw_size(), row_buffer.get());
140:         (a->i)[0] = row_buffer[0];
141:         for (PetscInt i = 0; i < (PetscInt)Agpu->size1(); ++i) {
142:           (a->i)[i+1] = row_buffer[i+1];
143:           a->imax[i]  = a->ilen[i] = a->i[i+1] - a->i[i];  //Set imax[] and ilen[] arrays at the same time as i[] for better cache reuse
144:         }

146:         // copy column indices
147:         viennacl::backend::typesafe_host_array<unsigned int> col_buffer; col_buffer.raw_resize(Agpu->handle2(), Agpu->nnz());
148:         viennacl::backend::memory_read(Agpu->handle2(), 0, col_buffer.raw_size(), col_buffer.get());
149:         for (PetscInt i=0; i < (PetscInt)Agpu->nnz(); ++i)
150:           (a->j)[i] = col_buffer[i];

152:         // copy nonzero entries directly to destination (no conversion required)
153:         viennacl::backend::memory_read(Agpu->handle(), 0, sizeof(PetscScalar)*Agpu->nnz(), a->a);

155:         ViennaCLWaitForGPU();
156:         /* TODO: Once a->diag is moved out of MatAssemblyEnd(), invalidate it here. */
157:       }
158:     } catch(std::exception const & ex) {
159:       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
160:     }

162:     /* This assembly prevents resetting the flag to PETSC_VIENNACL_CPU and recopying */
163:     MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
164:     MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);

166:     A->valid_GPU_matrix = PETSC_VIENNACL_BOTH;
167:   } else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "ViennaCL error: Only valid for unallocated GPU matrices");
168:   return(0);
169: }

171: PetscErrorCode MatCreateVecs_SeqAIJViennaCL(Mat mat, Vec *right, Vec *left)
172: {
174:   PetscInt rbs,cbs;

177:   MatGetBlockSizes(mat,&rbs,&cbs);
178:   if (right) {
179:     VecCreate(PetscObjectComm((PetscObject)mat),right);
180:     VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);
181:     VecSetBlockSize(*right,cbs);
182:     VecSetType(*right,VECSEQVIENNACL);
183:     PetscLayoutReference(mat->cmap,&(*right)->map);
184:   }
185:   if (left) {
186:     VecCreate(PetscObjectComm((PetscObject)mat),left);
187:     VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);
188:     VecSetBlockSize(*left,rbs);
189:     VecSetType(*left,VECSEQVIENNACL);
190:     PetscLayoutReference(mat->rmap,&(*left)->map);
191:   }
192:   return(0);
193: }

195: PetscErrorCode MatMult_SeqAIJViennaCL(Mat A,Vec xx,Vec yy)
196: {
197:   Mat_SeqAIJ           *a = (Mat_SeqAIJ*)A->data;
198:   PetscErrorCode       ierr;
199:   Mat_SeqAIJViennaCL   *viennaclstruct = (Mat_SeqAIJViennaCL*)A->spptr;
200:   const ViennaCLVector *xgpu=NULL;
201:   ViennaCLVector       *ygpu=NULL;

204:   if (A->rmap->n > 0 && A->cmap->n > 0) {
205:     VecViennaCLGetArrayRead(xx,&xgpu);
206:     VecViennaCLGetArrayWrite(yy,&ygpu);
207:     try {
208:       *ygpu = viennacl::linalg::prod(*viennaclstruct->mat,*xgpu);
209:       ViennaCLWaitForGPU();
210:     } catch (std::exception const & ex) {
211:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
212:     }
213:     VecViennaCLRestoreArrayRead(xx,&xgpu);
214:     VecViennaCLRestoreArrayWrite(yy,&ygpu);
215:     PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);
216:   }
217:   return(0);
218: }



222: PetscErrorCode MatMultAdd_SeqAIJViennaCL(Mat A,Vec xx,Vec yy,Vec zz)
223: {
224:   Mat_SeqAIJ           *a = (Mat_SeqAIJ*)A->data;
225:   PetscErrorCode       ierr;
226:   Mat_SeqAIJViennaCL   *viennaclstruct = (Mat_SeqAIJViennaCL*)A->spptr;
227:   const ViennaCLVector *xgpu=NULL,*ygpu=NULL;
228:   ViennaCLVector       *zgpu=NULL;

231:   if (A->rmap->n > 0 && A->cmap->n > 0) {
232:     try {
233:       VecViennaCLGetArrayRead(xx,&xgpu);
234:       VecViennaCLGetArrayRead(yy,&ygpu);
235:       VecViennaCLGetArrayWrite(zz,&zgpu);

237:       if (a->compressedrow.use) {
238:         ViennaCLVector temp = viennacl::linalg::prod(*viennaclstruct->compressed_mat, *xgpu);
239:         *zgpu = *ygpu + temp;
240:         ViennaCLWaitForGPU();
241:       } else {
242:         if (zz == xx || zz == yy) { //temporary required
243:           ViennaCLVector temp = viennacl::linalg::prod(*viennaclstruct->mat, *xgpu);
244:           *zgpu = *ygpu;
245:           *zgpu += temp;
246:           ViennaCLWaitForGPU();
247:         } else {
248:           *viennaclstruct->tempvec = viennacl::linalg::prod(*viennaclstruct->mat, *xgpu);
249:           *zgpu = *ygpu + *viennaclstruct->tempvec;
250:           ViennaCLWaitForGPU();
251:         }
252:       }

254:       VecViennaCLRestoreArrayRead(xx,&xgpu);
255:       VecViennaCLRestoreArrayRead(yy,&ygpu);
256:       VecViennaCLRestoreArrayWrite(zz,&zgpu);

258:     } catch(std::exception const & ex) {
259:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
260:     }
261:     PetscLogFlops(2.0*a->nz);
262:   }
263:   return(0);
264: }

266: PetscErrorCode MatAssemblyEnd_SeqAIJViennaCL(Mat A,MatAssemblyType mode)
267: {

271:   MatAssemblyEnd_SeqAIJ(A,mode);
272:   MatViennaCLCopyToGPU(A);
273:   return(0);
274: }

276: /* --------------------------------------------------------------------------------*/
277: /*@
278:    MatCreateSeqAIJViennaCL - Creates a sparse matrix in AIJ (compressed row) format
279:    (the default parallel PETSc format).  This matrix will ultimately be pushed down
280:    to GPUs and use the ViennaCL library for calculations. For good matrix
281:    assembly performance the user should preallocate the matrix storage by setting
282:    the parameter nz (or the array nnz).  By setting these parameters accurately,
283:    performance during matrix assembly can be increased substantially.


286:    Collective on MPI_Comm

288:    Input Parameters:
289: +  comm - MPI communicator, set to PETSC_COMM_SELF
290: .  m - number of rows
291: .  n - number of columns
292: .  nz - number of nonzeros per row (same for all rows)
293: -  nnz - array containing the number of nonzeros in the various rows
294:          (possibly different for each row) or NULL

296:    Output Parameter:
297: .  A - the matrix

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

303:    Notes:
304:    If nnz is given then nz is ignored

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

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

316:    Level: intermediate

318: .seealso: MatCreate(), MatCreateAIJ(), MatCreateAIJCUSP(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ()

320: @*/
321: PetscErrorCode  MatCreateSeqAIJViennaCL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
322: {

326:   MatCreate(comm,A);
327:   MatSetSizes(*A,m,n,m,n);
328:   MatSetType(*A,MATSEQAIJVIENNACL);
329:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);
330:   return(0);
331: }


334: PetscErrorCode MatDestroy_SeqAIJViennaCL(Mat A)
335: {
337:   Mat_SeqAIJViennaCL *viennaclcontainer = (Mat_SeqAIJViennaCL*)A->spptr;

340:   try {
341:     if (viennaclcontainer) {
342:       delete viennaclcontainer->tempvec;
343:       delete viennaclcontainer->mat;
344:       delete viennaclcontainer->compressed_mat;
345:       delete viennaclcontainer;
346:     }
347:     A->valid_GPU_matrix = PETSC_VIENNACL_UNALLOCATED;
348:   } catch(std::exception const & ex) {
349:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
350:   }

352:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijviennacl_C",NULL);

354:   /* 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 */
355:   A->spptr = 0;
356:   MatDestroy_SeqAIJ(A);
357:   return(0);
358: }


361: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJViennaCL(Mat B)
362: {

366:   MatCreate_SeqAIJ(B);
367:   MatConvert_SeqAIJ_SeqAIJViennaCL(B,MATSEQAIJVIENNACL,MAT_INPLACE_MATRIX,&B);
368:   return(0);
369: }

371: static PetscErrorCode MatDuplicate_SeqAIJViennaCL(Mat A,MatDuplicateOption cpvalues,Mat *B)
372: {
374:   Mat C;

377:   MatDuplicate_SeqAIJ(A,cpvalues,B);
378:   C = *B;

380:   C->ops->mult        = MatMult_SeqAIJViennaCL;
381:   C->ops->multadd     = MatMultAdd_SeqAIJViennaCL;
382:   C->ops->assemblyend = MatAssemblyEnd_SeqAIJViennaCL;
383:   C->ops->destroy     = MatDestroy_SeqAIJViennaCL;
384:   C->ops->getvecs     = MatCreateVecs_SeqAIJViennaCL;
385:   C->ops->duplicate   = MatDuplicate_SeqAIJViennaCL;

387:   C->spptr        = new Mat_SeqAIJViennaCL();
388:   ((Mat_SeqAIJViennaCL*)C->spptr)->tempvec        = NULL;
389:   ((Mat_SeqAIJViennaCL*)C->spptr)->mat            = NULL;
390:   ((Mat_SeqAIJViennaCL*)C->spptr)->compressed_mat = NULL;

392:   PetscObjectChangeTypeName((PetscObject)C,MATSEQAIJVIENNACL);

394:   C->valid_GPU_matrix = PETSC_VIENNACL_UNALLOCATED;

396:   /* If the source matrix is already assembled, copy the destination matrix to the GPU */
397:   if (C->assembled) {
398:     MatViennaCLCopyToGPU(C);
399:   }

401:   return(0);
402: }

404: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJViennaCL(Mat A,MatType type,MatReuse reuse,Mat *newmat)
405: {
407:   Mat            B;
408:   Mat_SeqAIJ     *aij;


412:   if (reuse == MAT_REUSE_MATRIX) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"MAT_REUSE_MATRIX is not supported. Consider using MAT_INPLACE_MATRIX instead");

414:   if (reuse == MAT_INITIAL_MATRIX) {
415:     MatDuplicate(A,MAT_COPY_VALUES,newmat);
416:   }

418:   B = *newmat;

420:   aij             = (Mat_SeqAIJ*)B->data;
421:   aij->inode.use  = PETSC_FALSE;

423:   B->ops->mult    = MatMult_SeqAIJViennaCL;
424:   B->ops->multadd = MatMultAdd_SeqAIJViennaCL;
425:   B->spptr        = new Mat_SeqAIJViennaCL();

427:   ((Mat_SeqAIJViennaCL*)B->spptr)->tempvec        = NULL;
428:   ((Mat_SeqAIJViennaCL*)B->spptr)->mat            = NULL;
429:   ((Mat_SeqAIJViennaCL*)B->spptr)->compressed_mat = NULL;

431:   B->ops->assemblyend    = MatAssemblyEnd_SeqAIJViennaCL;
432:   B->ops->destroy        = MatDestroy_SeqAIJViennaCL;
433:   B->ops->getvecs        = MatCreateVecs_SeqAIJViennaCL;
434:   B->ops->duplicate      = MatDuplicate_SeqAIJViennaCL;

436:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJVIENNACL);

438:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijviennacl_C",MatConvert_SeqAIJ_SeqAIJViennaCL);

440:   B->valid_GPU_matrix = PETSC_VIENNACL_UNALLOCATED;

442:   /* If the source matrix is already assembled, copy the destination matrix to the GPU */
443:   if (B->assembled) {
444:     MatViennaCLCopyToGPU(B);
445:   }

447:   return(0);
448: }


451: /*M
452:    MATSEQAIJVIENNACL - MATAIJVIENNACL = "aijviennacl" = "seqaijviennacl" - A matrix type to be used for sparse matrices.

454:    A matrix type type whose data resides on GPUs. These matrices are in CSR format by
455:    default. All matrix calculations are performed using the ViennaCL library.

457:    Options Database Keys:
458: +  -mat_type aijviennacl - sets the matrix type to "seqaijviennacl" during a call to MatSetFromOptions()
459: .  -mat_viennacl_storage_format csr - sets the storage format of matrices for MatMult during a call to MatSetFromOptions().
460: -  -mat_viennacl_mult_storage_format csr - sets the storage format of matrices for MatMult during a call to MatSetFromOptions().

462:   Level: beginner

464: .seealso: MatCreateSeqAIJViennaCL(), MATAIJVIENNACL, MatCreateAIJViennaCL()
465: M*/


468: PETSC_EXTERN PetscErrorCode MatSolverPackageRegister_ViennaCL(void)
469: {

473:   MatSolverPackageRegister(MATSOLVERPETSC, MATSEQAIJVIENNACL,    MAT_FACTOR_LU,MatGetFactor_seqaij_petsc);
474:   MatSolverPackageRegister(MATSOLVERPETSC, MATSEQAIJVIENNACL,    MAT_FACTOR_CHOLESKY,MatGetFactor_seqaij_petsc);
475:   MatSolverPackageRegister(MATSOLVERPETSC, MATSEQAIJVIENNACL,    MAT_FACTOR_ILU,MatGetFactor_seqaij_petsc);
476:   MatSolverPackageRegister(MATSOLVERPETSC, MATSEQAIJVIENNACL,    MAT_FACTOR_ICC,MatGetFactor_seqaij_petsc);
477:   return(0);
478: }