Actual source code: aijviennacl.cxx

petsc-3.13.6 2020-09-29
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: #define PETSC_SKIP_IMMINTRIN_H_CUDAWORKAROUND 1
 10:  #include <../src/mat/impls/aij/seq/aij.h>
 11:  #include <petscbt.h>
 12:  #include <../src/vec/vec/impls/dvecimpl.h>
 13:  #include <petsc/private/vecimpl.h>

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


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

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

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

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

 35:   if (A->rmap->n > 0 && A->cmap->n > 0 && a->nz) { //some OpenCL SDKs have issues with buffers of size 0
 36:     if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) {
 37:       PetscLogEventBegin(MAT_ViennaCLCopyToGPU,A,0,0,0);

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

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

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

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

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

 58:           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);
 59:           PetscLogCpuToGpu(((2*a->compressedrow.nrows)+1+a->nz)*sizeof(PetscInt) + (a->nz)*sizeof(PetscScalar));
 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:           PetscLogCpuToGpu(((A->rmap->n+1)+a->nz)*sizeof(PetscInt)+(a->nz)*sizeof(PetscScalar));
 76:         }
 77:         ViennaCLWaitForGPU();
 78:       } catch(std::exception const & ex) {
 79:         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
 80:       }

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

 94:       A->offloadmask = PETSC_OFFLOAD_BOTH;

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

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

110:   if (A->offloadmask == PETSC_OFFLOAD_BOTH) return(0);
111:   if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED && Agpu) {
112:     try {
113:       if (a->compressedrow.use) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "ViennaCL: Cannot handle row compression for GPU matrices");
114:       else {

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

130:         a->singlemalloc = PETSC_TRUE;

132:         /* Setup row lengths */
133:         PetscFree(a->imax);
134:         PetscFree(a->ilen);
135:         PetscMalloc1(m,&a->imax);
136:         PetscMalloc1(m,&a->ilen);
137:         PetscLogObjectMemory((PetscObject)A, 2*m*sizeof(PetscInt));

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

142:         // copy row array
143:         viennacl::backend::memory_read(Agpu->handle1(), 0, row_buffer.raw_size(), row_buffer.get());
144:         (a->i)[0] = row_buffer[0];
145:         for (PetscInt i = 0; i < (PetscInt)Agpu->size1(); ++i) {
146:           (a->i)[i+1] = row_buffer[i+1];
147:           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
148:         }

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

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

159:         PetscLogGpuToCpu(row_buffer.raw_size()+col_buffer.raw_size()+(Agpu->nnz()*sizeof(PetscScalar)));
160:         ViennaCLWaitForGPU();
161:         /* TODO: Once a->diag is moved out of MatAssemblyEnd(), invalidate it here. */
162:       }
163:     } catch(std::exception const & ex) {
164:       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
165:     }
166:   } else if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED) {
167:     return(0);
168:   } else {
169:     if (!Agpu && A->offloadmask != PETSC_OFFLOAD_GPU) return(0);

171:     if (a->compressedrow.use) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "ViennaCL: Cannot handle row compression for GPU matrices");
172:     if (!Agpu) {
173:       viennacl::backend::memory_read(viennaclstruct->mat->handle(), 0, sizeof(PetscScalar)*viennaclstruct->mat->nnz(), a->a);
174:     } else {
175:       viennacl::backend::memory_read(Agpu->handle(), 0, sizeof(PetscScalar)*Agpu->nnz(), a->a);
176:     }
177:   }
178:   A->offloadmask = PETSC_OFFLOAD_BOTH;
179:   /* This assembly prevents resetting the flag to PETSC_OFFLOAD_CPU and recopying */
180:   MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
181:   MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
182:   return(0);
183: }

185: PetscErrorCode MatMult_SeqAIJViennaCL(Mat A,Vec xx,Vec yy)
186: {
187:   Mat_SeqAIJ           *a = (Mat_SeqAIJ*)A->data;
188:   PetscErrorCode       ierr;
189:   Mat_SeqAIJViennaCL   *viennaclstruct = (Mat_SeqAIJViennaCL*)A->spptr;
190:   const ViennaCLVector *xgpu=NULL;
191:   ViennaCLVector       *ygpu=NULL;

194:   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
195:   MatViennaCLCopyToGPU(A);
196:   if (A->rmap->n > 0 && A->cmap->n > 0 && a->nz) {
197:     VecViennaCLGetArrayRead(xx,&xgpu);
198:     VecViennaCLGetArrayWrite(yy,&ygpu);
199:     PetscLogGpuTimeBegin();
200:     try {
201:       if (a->compressedrow.use) {
202:         *ygpu = viennacl::linalg::prod(*viennaclstruct->compressed_mat, *xgpu);
203:       } else {
204:         *ygpu = viennacl::linalg::prod(*viennaclstruct->mat,*xgpu);
205:       }
206:       ViennaCLWaitForGPU();
207:     } catch (std::exception const & ex) {
208:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
209:     }
210:     PetscLogGpuTimeEnd();
211:     VecViennaCLRestoreArrayRead(xx,&xgpu);
212:     VecViennaCLRestoreArrayWrite(yy,&ygpu);
213:     PetscLogGpuFlops(2.0*a->nz - a->nonzerorowcnt);
214:   } else {
215:     VecSet_SeqViennaCL(yy,0);
216:   }
217:   return(0);
218: }

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

229:   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
230:   MatViennaCLCopyToGPU(A);
231:   if (A->rmap->n > 0 && A->cmap->n > 0 && a->nz) {
232:     try {
233:       VecViennaCLGetArrayRead(xx,&xgpu);
234:       VecViennaCLGetArrayRead(yy,&ygpu);
235:       VecViennaCLGetArrayWrite(zz,&zgpu);
236:       PetscLogGpuTimeBegin();
237:       if (a->compressedrow.use) *viennaclstruct->tempvec = viennacl::linalg::prod(*viennaclstruct->compressed_mat, *xgpu);
238:       else *viennaclstruct->tempvec = viennacl::linalg::prod(*viennaclstruct->mat, *xgpu);
239:       if (zz != yy) *zgpu = *ygpu + *viennaclstruct->tempvec;
240:       else *zgpu += *viennaclstruct->tempvec;
241:       ViennaCLWaitForGPU();
242:       PetscLogGpuTimeEnd();
243:       VecViennaCLRestoreArrayRead(xx,&xgpu);
244:       VecViennaCLRestoreArrayRead(yy,&ygpu);
245:       VecViennaCLRestoreArrayWrite(zz,&zgpu);

247:     } catch(std::exception const & ex) {
248:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
249:     }
250:     PetscLogGpuFlops(2.0*a->nz);
251:   } else {
252:     VecCopy_SeqViennaCL(yy,zz);
253:   }
254:   return(0);
255: }

257: PetscErrorCode MatAssemblyEnd_SeqAIJViennaCL(Mat A,MatAssemblyType mode)
258: {

262:   MatAssemblyEnd_SeqAIJ(A,mode);
263:   if (mode == MAT_FLUSH_ASSEMBLY) return(0);
264:   if (!A->boundtocpu) {
265:     MatViennaCLCopyToGPU(A);
266:   }
267:   return(0);
268: }

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


280:    Collective

282:    Input Parameters:
283: +  comm - MPI communicator, set to PETSC_COMM_SELF
284: .  m - number of rows
285: .  n - number of columns
286: .  nz - number of nonzeros per row (same for all rows)
287: -  nnz - array containing the number of nonzeros in the various rows
288:          (possibly different for each row) or NULL

290:    Output Parameter:
291: .  A - the matrix

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

297:    Notes:
298:    If nnz is given then nz is ignored

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

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

310:    Level: intermediate

312: .seealso: MatCreate(), MatCreateAIJ(), MatCreateAIJCUSPARSE(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ()

314: @*/
315: PetscErrorCode  MatCreateSeqAIJViennaCL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
316: {

320:   MatCreate(comm,A);
321:   MatSetSizes(*A,m,n,m,n);
322:   MatSetType(*A,MATSEQAIJVIENNACL);
323:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);
324:   return(0);
325: }


328: PetscErrorCode MatDestroy_SeqAIJViennaCL(Mat A)
329: {
331:   Mat_SeqAIJViennaCL *viennaclcontainer = (Mat_SeqAIJViennaCL*)A->spptr;

334:   try {
335:     if (viennaclcontainer) {
336:       delete viennaclcontainer->tempvec;
337:       delete viennaclcontainer->mat;
338:       delete viennaclcontainer->compressed_mat;
339:       delete viennaclcontainer;
340:     }
341:     A->offloadmask = PETSC_OFFLOAD_UNALLOCATED;
342:   } catch(std::exception const & ex) {
343:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
344:   }

346:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijviennacl_C",NULL);
347:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaijviennacl_seqdense_C",NULL);
348:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaijviennacl_seqaij_C",NULL);

350:   /* 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 */
351:   A->spptr = 0;
352:   MatDestroy_SeqAIJ(A);
353:   return(0);
354: }


357: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJViennaCL(Mat B)
358: {

362:   MatCreate_SeqAIJ(B);
363:   MatConvert_SeqAIJ_SeqAIJViennaCL(B,MATSEQAIJVIENNACL,MAT_INPLACE_MATRIX,&B);
364:   return(0);
365: }

367: static PetscErrorCode MatBindToCPU_SeqAIJViennaCL(Mat,PetscBool);
368: static PetscErrorCode MatDuplicate_SeqAIJViennaCL(Mat A,MatDuplicateOption cpvalues,Mat *B)
369: {
371:   Mat            C;

374:   MatDuplicate_SeqAIJ(A,cpvalues,B);
375:   C = *B;

377:   MatBindToCPU_SeqAIJViennaCL(A,PETSC_FALSE);
378:   C->ops->bindtocpu = MatBindToCPU_SeqAIJViennaCL;

380:   C->spptr = new Mat_SeqAIJViennaCL();
381:   ((Mat_SeqAIJViennaCL*)C->spptr)->tempvec        = NULL;
382:   ((Mat_SeqAIJViennaCL*)C->spptr)->mat            = NULL;
383:   ((Mat_SeqAIJViennaCL*)C->spptr)->compressed_mat = NULL;

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

387:   C->offloadmask = PETSC_OFFLOAD_UNALLOCATED;

389:   /* If the source matrix is already assembled, copy the destination matrix to the GPU */
390:   if (C->assembled) {
391:     MatViennaCLCopyToGPU(C);
392:   }

394:   return(0);
395: }

397: static PetscErrorCode MatSeqAIJGetArray_SeqAIJViennaCL(Mat A,PetscScalar *array[])
398: {
399:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

403:   MatViennaCLCopyFromGPU(A,(const ViennaCLAIJMatrix *)NULL);
404:   *array = a->a;
405:   A->offloadmask = PETSC_OFFLOAD_CPU;
406:   return(0);
407: }


410: static PetscErrorCode MatSeqAIJRestoreArray_SeqAIJViennaCL(Mat A,PetscScalar *array[])
411: {
413:   *array = NULL;
414:   return(0);
415: }

417: static PetscErrorCode MatBindToCPU_SeqAIJViennaCL(Mat A,PetscBool flg)
418: {

422:   A->boundtocpu = flg;
423:   if (flg) {
424:     /* make sure we have an up-to-date copy on the CPU */
425:     MatViennaCLCopyFromGPU(A,(const ViennaCLAIJMatrix *)NULL);
426:     PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);
427:     PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);

429:     A->ops->mult        = MatMult_SeqAIJ;
430:     A->ops->multadd     = MatMultAdd_SeqAIJ;
431:     A->ops->assemblyend = MatAssemblyEnd_SeqAIJ;
432:     A->ops->duplicate   = MatDuplicate_SeqAIJ;
433:   } else {
434:     PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJViennaCL);
435:     PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJViennaCL);

437:     A->ops->mult        = MatMult_SeqAIJViennaCL;
438:     A->ops->multadd     = MatMultAdd_SeqAIJViennaCL;
439:     A->ops->assemblyend = MatAssemblyEnd_SeqAIJViennaCL;
440:     A->ops->destroy     = MatDestroy_SeqAIJViennaCL;
441:     A->ops->duplicate   = MatDuplicate_SeqAIJViennaCL;
442:   }
443:   return(0);
444: }

446: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJViennaCL(Mat A,MatType type,MatReuse reuse,Mat *newmat)
447: {
449:   Mat            B;
450:   Mat_SeqAIJ     *aij;


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

456:   if (reuse == MAT_INITIAL_MATRIX) {
457:     MatDuplicate(A,MAT_COPY_VALUES,newmat);
458:   }

460:   B = *newmat;

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

465:   B->spptr        = new Mat_SeqAIJViennaCL();

467:   ((Mat_SeqAIJViennaCL*)B->spptr)->tempvec        = NULL;
468:   ((Mat_SeqAIJViennaCL*)B->spptr)->mat            = NULL;
469:   ((Mat_SeqAIJViennaCL*)B->spptr)->compressed_mat = NULL;

471:   MatBindToCPU_SeqAIJViennaCL(A,PETSC_FALSE);
472:   A->ops->bindtocpu = MatBindToCPU_SeqAIJViennaCL;

474:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJVIENNACL);
475:   PetscFree(B->defaultvectype);
476:   PetscStrallocpy(VECVIENNACL,&B->defaultvectype);

478:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijviennacl_C",MatConvert_SeqAIJ_SeqAIJViennaCL);
479:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaijviennacl_seqdense_C",MatProductSetFromOptions_SeqAIJ_SeqDense);
480:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaijviennacl_seqaij_C",MatProductSetFromOptions_SeqAIJ);

482:   B->offloadmask = PETSC_OFFLOAD_UNALLOCATED;

484:   /* If the source matrix is already assembled, copy the destination matrix to the GPU */
485:   if (B->assembled) {
486:     MatViennaCLCopyToGPU(B);
487:   }

489:   return(0);
490: }


493: /*MC
494:    MATSEQAIJVIENNACL - MATAIJVIENNACL = "aijviennacl" = "seqaijviennacl" - A matrix type to be used for sparse matrices.

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

499:    Options Database Keys:
500: +  -mat_type aijviennacl - sets the matrix type to "seqaijviennacl" during a call to MatSetFromOptions()
501: .  -mat_viennacl_storage_format csr - sets the storage format of matrices for MatMult during a call to MatSetFromOptions().
502: -  -mat_viennacl_mult_storage_format csr - sets the storage format of matrices for MatMult during a call to MatSetFromOptions().

504:   Level: beginner

506: .seealso: MatCreateSeqAIJViennaCL(), MATAIJVIENNACL, MatCreateAIJViennaCL()
507: M*/


510: PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_ViennaCL(void)
511: {

515:   MatSolverTypeRegister(MATSOLVERPETSC, MATSEQAIJVIENNACL, MAT_FACTOR_LU,MatGetFactor_seqaij_petsc);
516:   MatSolverTypeRegister(MATSOLVERPETSC, MATSEQAIJVIENNACL, MAT_FACTOR_CHOLESKY,MatGetFactor_seqaij_petsc);
517:   MatSolverTypeRegister(MATSOLVERPETSC, MATSEQAIJVIENNACL, MAT_FACTOR_ILU,MatGetFactor_seqaij_petsc);
518:   MatSolverTypeRegister(MATSOLVERPETSC, MATSEQAIJVIENNACL, MAT_FACTOR_ICC,MatGetFactor_seqaij_petsc);
519:   return(0);
520: }