Date: 2022-03-31T04:46:47+00:00

Actual source code: aijviennacl.cxx

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

      7: #include <petscconf.h>
      8: #define PETSC_SKIP_IMMINTRIN_H_CUDAWORKAROUND 1
      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>

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

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

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

     26: PetscErrorCode MatViennaCLCopyToGPU(Mat A)
     27: {
     28:   Mat_SeqAIJViennaCL *viennaclstruct = (Mat_SeqAIJViennaCL*)A->spptr;
     29:   Mat_SeqAIJ         *a              = (Mat_SeqAIJ*)A->data;

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

     35:       try {
     36:         if (a->compressedrow.use) {
     37:           if (!viennaclstruct->compressed_mat) viennaclstruct->compressed_mat = new ViennaCLCompressedAIJMatrix();

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

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

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

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

     54:           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);
     55:           PetscLogCpuToGpu(((2*a->compressedrow.nrows)+1+a->nz)*sizeof(PetscInt) + (a->nz)*sizeof(PetscScalar));
     56:         } else {
     57:           if (!viennaclstruct->mat) viennaclstruct->mat = new ViennaCLAIJMatrix();

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

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

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

     70:           viennaclstruct->mat->set(row_buffer.get(), col_buffer.get(), a->a, A->rmap->n, A->cmap->n, a->nz);
     71:           PetscLogCpuToGpu(((A->rmap->n+1)+a->nz)*sizeof(PetscInt)+(a->nz)*sizeof(PetscScalar));
     72:         }
     73:         ViennaCLWaitForGPU();
     74:       } catch(std::exception const & ex) {
     75:         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
     76:       }

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

     90:       A->offloadmask = PETSC_OFFLOAD_BOTH;

     92:       PetscLogEventEnd(MAT_ViennaCLCopyToGPU,A,0,0,0);
     93:     }
     94:   }
     95:   return 0;
     96: }

     98: PetscErrorCode MatViennaCLCopyFromGPU(Mat A, const ViennaCLAIJMatrix *Agpu)
     99: {
    100:   Mat_SeqAIJViennaCL *viennaclstruct = (Mat_SeqAIJViennaCL*)A->spptr;
    101:   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
    102:   PetscInt           m  = A->rmap->n;

    104:   if (A->offloadmask == PETSC_OFFLOAD_BOTH) return 0;
    105:   if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED && Agpu) {
    106:     try {
    108:       else {

    111:         a->nz           = Agpu->nnz();
    112:         a->maxnz        = a->nz; /* Since we allocate exactly the right amount */
    113:         A->preallocated = PETSC_TRUE;
    114:         if (a->singlemalloc) {
    115:           if (a->a) PetscFree3(a->a,a->j,a->i);
    116:         } else {
    117:           if (a->i) PetscFree(a->i);
    118:           if (a->j) PetscFree(a->j);
    119:           if (a->a) PetscFree(a->a);
    120:         }
    121:         PetscMalloc3(a->nz,&a->a,a->nz,&a->j,m+1,&a->i);
    122:         PetscLogObjectMemory((PetscObject)A, a->nz*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));

    124:         a->singlemalloc = PETSC_TRUE;

    126:         /* Setup row lengths */
    127:         PetscFree(a->imax);
    128:         PetscFree(a->ilen);
    129:         PetscMalloc1(m,&a->imax);
    130:         PetscMalloc1(m,&a->ilen);
    131:         PetscLogObjectMemory((PetscObject)A, 2*m*sizeof(PetscInt));

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

    136:         // copy row array
    137:         viennacl::backend::memory_read(Agpu->handle1(), 0, row_buffer.raw_size(), row_buffer.get());
    138:         (a->i)[0] = row_buffer[0];
    139:         for (PetscInt i = 0; i < (PetscInt)Agpu->size1(); ++i) {
    140:           (a->i)[i+1] = row_buffer[i+1];
    141:           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
    142:         }

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

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

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

    166:     if (!Agpu) {
    167:       viennacl::backend::memory_read(viennaclstruct->mat->handle(), 0, sizeof(PetscScalar)*viennaclstruct->mat->nnz(), a->a);
    168:     } else {
    169:       viennacl::backend::memory_read(Agpu->handle(), 0, sizeof(PetscScalar)*Agpu->nnz(), a->a);
    170:     }
    171:   }
    172:   A->offloadmask = PETSC_OFFLOAD_BOTH;
    173:   /* This assembly prevents resetting the flag to PETSC_OFFLOAD_CPU and recopying */
    174:   MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
    175:   MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
    176:   return 0;
    177: }

    179: PetscErrorCode MatMult_SeqAIJViennaCL(Mat A,Vec xx,Vec yy)
    180: {
    181:   Mat_SeqAIJ           *a = (Mat_SeqAIJ*)A->data;
    182:   Mat_SeqAIJViennaCL   *viennaclstruct = (Mat_SeqAIJViennaCL*)A->spptr;
    183:   const ViennaCLVector *xgpu=NULL;
    184:   ViennaCLVector       *ygpu=NULL;

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

    212: PetscErrorCode MatMultAdd_SeqAIJViennaCL(Mat A,Vec xx,Vec yy,Vec zz)
    213: {
    214:   Mat_SeqAIJ           *a = (Mat_SeqAIJ*)A->data;
    215:   Mat_SeqAIJViennaCL   *viennaclstruct = (Mat_SeqAIJViennaCL*)A->spptr;
    216:   const ViennaCLVector *xgpu=NULL,*ygpu=NULL;
    217:   ViennaCLVector       *zgpu=NULL;

    219:   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
    220:   MatViennaCLCopyToGPU(A);
    221:   if (A->rmap->n > 0 && A->cmap->n > 0 && a->nz) {
    222:     try {
    223:       VecViennaCLGetArrayRead(xx,&xgpu);
    224:       VecViennaCLGetArrayRead(yy,&ygpu);
    225:       VecViennaCLGetArrayWrite(zz,&zgpu);
    226:       PetscLogGpuTimeBegin();
    227:       if (a->compressedrow.use) *viennaclstruct->tempvec = viennacl::linalg::prod(*viennaclstruct->compressed_mat, *xgpu);
    228:       else *viennaclstruct->tempvec = viennacl::linalg::prod(*viennaclstruct->mat, *xgpu);
    229:       if (zz != yy) *zgpu = *ygpu + *viennaclstruct->tempvec;
    230:       else *zgpu += *viennaclstruct->tempvec;
    231:       ViennaCLWaitForGPU();
    232:       PetscLogGpuTimeEnd();
    233:       VecViennaCLRestoreArrayRead(xx,&xgpu);
    234:       VecViennaCLRestoreArrayRead(yy,&ygpu);
    235:       VecViennaCLRestoreArrayWrite(zz,&zgpu);

    237:     } catch(std::exception const & ex) {
    238:       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
    239:     }
    240:     PetscLogGpuFlops(2.0*a->nz);
    241:   } else {
    242:     VecCopy_SeqViennaCL(yy,zz);
    243:   }
    244:   return 0;
    245: }

    247: PetscErrorCode MatAssemblyEnd_SeqAIJViennaCL(Mat A,MatAssemblyType mode)
    248: {
    249:   MatAssemblyEnd_SeqAIJ(A,mode);
    250:   if (mode == MAT_FLUSH_ASSEMBLY) return 0;
    251:   if (!A->boundtocpu) MatViennaCLCopyToGPU(A);
    252:   return 0;
    253: }

    255: /* --------------------------------------------------------------------------------*/
    256: /*@C
    257:    MatCreateSeqAIJViennaCL - Creates a sparse matrix in AIJ (compressed row) format
    258:    (the default parallel PETSc format).  This matrix will ultimately be pushed down
    259:    to GPUs and use the ViennaCL library for calculations. For good matrix
    260:    assembly performance the user should preallocate the matrix storage by setting
    261:    the parameter nz (or the array nnz).  By setting these parameters accurately,
    262:    performance during matrix assembly can be increased substantially.

    264:    Collective

    266:    Input Parameters:
    267: +  comm - MPI communicator, set to PETSC_COMM_SELF
    268: .  m - number of rows
    269: .  n - number of columns
    270: .  nz - number of nonzeros per row (same for all rows)
    271: -  nnz - array containing the number of nonzeros in the various rows
    272:          (possibly different for each row) or NULL

    274:    Output Parameter:
    275: .  A - the matrix

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

    281:    Notes:
    282:    If nnz is given then nz is ignored

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

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

    294:    Level: intermediate

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

    298: @*/
    299: PetscErrorCode  MatCreateSeqAIJViennaCL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
    300: {
    301:   MatCreate(comm,A);
    302:   MatSetSizes(*A,m,n,m,n);
    303:   MatSetType(*A,MATSEQAIJVIENNACL);
    304:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);
    305:   return 0;
    306: }

    308: PetscErrorCode MatDestroy_SeqAIJViennaCL(Mat A)
    309: {
    310:   Mat_SeqAIJViennaCL *viennaclcontainer = (Mat_SeqAIJViennaCL*)A->spptr;

    312:   try {
    313:     if (viennaclcontainer) {
    314:       delete viennaclcontainer->tempvec;
    315:       delete viennaclcontainer->mat;
    316:       delete viennaclcontainer->compressed_mat;
    317:       delete viennaclcontainer;
    318:     }
    319:     A->offloadmask = PETSC_OFFLOAD_UNALLOCATED;
    320:   } catch(std::exception const & ex) {
    321:     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
    322:   }

    324:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijviennacl_C",NULL);
    325:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaijviennacl_seqdense_C",NULL);
    326:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaijviennacl_seqaij_C",NULL);

    328:   /* 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 */
    329:   A->spptr = 0;
    330:   MatDestroy_SeqAIJ(A);
    331:   return 0;
    332: }

    334: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJViennaCL(Mat B)
    335: {
    336:   MatCreate_SeqAIJ(B);
    337:   MatConvert_SeqAIJ_SeqAIJViennaCL(B,MATSEQAIJVIENNACL,MAT_INPLACE_MATRIX,&B);
    338:   return 0;
    339: }

    341: static PetscErrorCode MatBindToCPU_SeqAIJViennaCL(Mat,PetscBool);
    342: static PetscErrorCode MatDuplicate_SeqAIJViennaCL(Mat A,MatDuplicateOption cpvalues,Mat *B)
    343: {
    344:   Mat C;

    346:   MatDuplicate_SeqAIJ(A,cpvalues,B);
    347:   C = *B;

    349:   MatBindToCPU_SeqAIJViennaCL(A,PETSC_FALSE);
    350:   C->ops->bindtocpu = MatBindToCPU_SeqAIJViennaCL;

    352:   C->spptr = new Mat_SeqAIJViennaCL();
    353:   ((Mat_SeqAIJViennaCL*)C->spptr)->tempvec        = NULL;
    354:   ((Mat_SeqAIJViennaCL*)C->spptr)->mat            = NULL;
    355:   ((Mat_SeqAIJViennaCL*)C->spptr)->compressed_mat = NULL;

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

    359:   C->offloadmask = PETSC_OFFLOAD_UNALLOCATED;

    361:   /* If the source matrix is already assembled, copy the destination matrix to the GPU */
    362:   if (C->assembled) MatViennaCLCopyToGPU(C);
    363:   return 0;
    364: }

    366: static PetscErrorCode MatSeqAIJGetArray_SeqAIJViennaCL(Mat A,PetscScalar *array[])
    367: {
    368:   MatViennaCLCopyFromGPU(A,(const ViennaCLAIJMatrix *)NULL);
    369:   *array = ((Mat_SeqAIJ*)A->data)->a;
    370:   return 0;
    371: }

    373: static PetscErrorCode MatSeqAIJRestoreArray_SeqAIJViennaCL(Mat A,PetscScalar *array[])
    374: {
    375:   A->offloadmask = PETSC_OFFLOAD_CPU;
    376:   *array         = NULL;
    377:   return 0;
    378: }

    380: static PetscErrorCode MatSeqAIJGetArrayRead_SeqAIJViennaCL(Mat A,const PetscScalar *array[])
    381: {
    382:   MatViennaCLCopyFromGPU(A,(const ViennaCLAIJMatrix *)NULL);
    383:   *array = ((Mat_SeqAIJ*)A->data)->a;
    384:   return 0;
    385: }

    387: static PetscErrorCode MatSeqAIJRestoreArrayRead_SeqAIJViennaCL(Mat A,const PetscScalar *array[])
    388: {
    389:   *array = NULL;
    390:   /* No A->offloadmask = PETSC_OFFLOAD_CPU since if A->offloadmask was PETSC_OFFLOAD_BOTH, it is still BOTH */
    391:   return 0;
    392: }

    394: static PetscErrorCode MatSeqAIJGetArrayWrite_SeqAIJViennaCL(Mat A,PetscScalar *array[])
    395: {
    396:   *array = ((Mat_SeqAIJ*)A->data)->a;
    397:   return 0;
    398: }

    400: static PetscErrorCode MatSeqAIJRestoreArrayWrite_SeqAIJViennaCL(Mat A,PetscScalar *array[])
    401: {
    402:   A->offloadmask = PETSC_OFFLOAD_CPU;
    403:   *array         = NULL;
    404:   return 0;
    405: }

    407: static PetscErrorCode MatBindToCPU_SeqAIJViennaCL(Mat A,PetscBool flg)
    408: {
    409:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

    411:   A->boundtocpu  = flg;
    412:   if (flg && a->inode.size) {
    413:     a->inode.use = PETSC_TRUE;
    414:   } else {
    415:     a->inode.use = PETSC_FALSE;
    416:   }
    417:   if (flg) {
    418:     /* make sure we have an up-to-date copy on the CPU */
    419:     MatViennaCLCopyFromGPU(A,(const ViennaCLAIJMatrix *)NULL);
    420:     A->ops->mult        = MatMult_SeqAIJ;
    421:     A->ops->multadd     = MatMultAdd_SeqAIJ;
    422:     A->ops->assemblyend = MatAssemblyEnd_SeqAIJ;
    423:     A->ops->duplicate   = MatDuplicate_SeqAIJ;
    424:     PetscMemzero(a->ops,sizeof(Mat_SeqAIJOps));
    425:   } else {
    426:     A->ops->mult        = MatMult_SeqAIJViennaCL;
    427:     A->ops->multadd     = MatMultAdd_SeqAIJViennaCL;
    428:     A->ops->assemblyend = MatAssemblyEnd_SeqAIJViennaCL;
    429:     A->ops->destroy     = MatDestroy_SeqAIJViennaCL;
    430:     A->ops->duplicate   = MatDuplicate_SeqAIJViennaCL;

    432:     a->ops->getarray           = MatSeqAIJGetArray_SeqAIJViennaCL;
    433:     a->ops->restorearray       = MatSeqAIJRestoreArray_SeqAIJViennaCL;
    434:     a->ops->getarrayread       = MatSeqAIJGetArrayRead_SeqAIJViennaCL;
    435:     a->ops->restorearrayread   = MatSeqAIJRestoreArrayRead_SeqAIJViennaCL;
    436:     a->ops->getarraywrite      = MatSeqAIJGetArrayWrite_SeqAIJViennaCL;
    437:     a->ops->restorearraywrite  = MatSeqAIJRestoreArrayWrite_SeqAIJViennaCL;
    438:   }
    439:   return 0;
    440: }

    442: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJViennaCL(Mat A,MatType type,MatReuse reuse,Mat *newmat)
    443: {
    444:   Mat B;

    448:   if (reuse == MAT_INITIAL_MATRIX) MatDuplicate(A,MAT_COPY_VALUES,newmat);

    450:   B = *newmat;

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

    454:   ((Mat_SeqAIJViennaCL*)B->spptr)->tempvec        = NULL;
    455:   ((Mat_SeqAIJViennaCL*)B->spptr)->mat            = NULL;
    456:   ((Mat_SeqAIJViennaCL*)B->spptr)->compressed_mat = NULL;

    458:   MatBindToCPU_SeqAIJViennaCL(A,PETSC_FALSE);
    459:   A->ops->bindtocpu = MatBindToCPU_SeqAIJViennaCL;

    461:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJVIENNACL);
    462:   PetscFree(B->defaultvectype);
    463:   PetscStrallocpy(VECVIENNACL,&B->defaultvectype);

    465:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijviennacl_C",MatConvert_SeqAIJ_SeqAIJViennaCL);
    466:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaijviennacl_seqdense_C",MatProductSetFromOptions_SeqAIJ_SeqDense);
    467:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaijviennacl_seqaij_C",MatProductSetFromOptions_SeqAIJ);

    469:   B->offloadmask = PETSC_OFFLOAD_UNALLOCATED;

    471:   /* If the source matrix is already assembled, copy the destination matrix to the GPU */
    472:   if (B->assembled) MatViennaCLCopyToGPU(B);
    473:   return 0;
    474: }

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

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

    482:    Options Database Keys:
    483: +  -mat_type aijviennacl - sets the matrix type to "seqaijviennacl" during a call to MatSetFromOptions()
    484: .  -mat_viennacl_storage_format csr - sets the storage format of matrices for MatMult during a call to MatSetFromOptions().
    485: -  -mat_viennacl_mult_storage_format csr - sets the storage format of matrices for MatMult during a call to MatSetFromOptions().

    487:   Level: beginner

    489: .seealso: MatCreateSeqAIJViennaCL(), MATAIJVIENNACL, MatCreateAIJViennaCL()
    490: M*/

    492: PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_ViennaCL(void)
    493: {
    494:   MatSolverTypeRegister(MATSOLVERPETSC, MATSEQAIJVIENNACL, MAT_FACTOR_LU,MatGetFactor_seqaij_petsc);
    495:   MatSolverTypeRegister(MATSOLVERPETSC, MATSEQAIJVIENNACL, MAT_FACTOR_CHOLESKY,MatGetFactor_seqaij_petsc);
    496:   MatSolverTypeRegister(MATSOLVERPETSC, MATSEQAIJVIENNACL, MAT_FACTOR_ILU,MatGetFactor_seqaij_petsc);
    497:   MatSolverTypeRegister(MATSOLVERPETSC, MATSEQAIJVIENNACL, MAT_FACTOR_ICC,MatGetFactor_seqaij_petsc);
    498:   return 0;
    499: }