19ae82921SPaul Mullowney /* 29ae82921SPaul Mullowney Defines the basic matrix operations for the AIJ (compressed row) 3bc3f50f2SPaul Mullowney matrix storage format using the CUSPARSE library, 49ae82921SPaul Mullowney */ 5dced61a5SBarry Smith #define PETSC_SKIP_SPINLOCK 653800007SKarl Rupp #define PETSC_SKIP_CXX_COMPLEX_FIX 799acd6aaSStefano Zampini #define PETSC_SKIP_IMMINTRIN_H_CUDAWORKAROUND 1 89ae82921SPaul Mullowney 93d13b8fdSMatthew G. Knepley #include <petscconf.h> 103d13b8fdSMatthew G. Knepley #include <../src/mat/impls/aij/seq/aij.h> /*I "petscmat.h" I*/ 11087f3262SPaul Mullowney #include <../src/mat/impls/sbaij/seq/sbaij.h> 123d13b8fdSMatthew G. Knepley #include <../src/vec/vec/impls/dvecimpl.h> 13af0996ceSBarry Smith #include <petsc/private/vecimpl.h> 149ae82921SPaul Mullowney #undef VecType 153d13b8fdSMatthew G. Knepley #include <../src/mat/impls/aij/seq/seqcusparse/cusparsematimpl.h> 16bc3f50f2SPaul Mullowney 17e057df02SPaul Mullowney const char *const MatCUSPARSEStorageFormats[] = {"CSR","ELL","HYB","MatCUSPARSEStorageFormat","MAT_CUSPARSE_",0}; 189ae82921SPaul Mullowney 19087f3262SPaul Mullowney static PetscErrorCode MatICCFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,const MatFactorInfo*); 20087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,const MatFactorInfo*); 21087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat,Mat,const MatFactorInfo*); 22087f3262SPaul Mullowney 236fa9248bSJed Brown static PetscErrorCode MatILUFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,IS,const MatFactorInfo*); 246fa9248bSJed Brown static PetscErrorCode MatLUFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,IS,const MatFactorInfo*); 256fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat,Mat,const MatFactorInfo*); 26087f3262SPaul Mullowney 276fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat,Vec,Vec); 286fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat,Vec,Vec); 296fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat,Vec,Vec); 306fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat,Vec,Vec); 314416b707SBarry Smith static PetscErrorCode MatSetFromOptions_SeqAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat); 326fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat,Vec,Vec); 336fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat,Vec,Vec,Vec); 346fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat,Vec,Vec); 356fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat,Vec,Vec,Vec); 369ae82921SPaul Mullowney 377f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix**); 38470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct**); 39470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct**,MatCUSPARSEStorageFormat); 40*ccdfe979SStefano Zampini static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Reset(Mat_SeqAIJCUSPARSETriFactors**); 41470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors**); 42470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE**); 437f756511SDominic Meiser 44b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSESetStream(Mat A,const cudaStream_t stream) 45b06137fdSPaul Mullowney { 46b06137fdSPaul Mullowney cusparseStatus_t stat; 47b06137fdSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 48b06137fdSPaul Mullowney 49b06137fdSPaul Mullowney PetscFunctionBegin; 50b06137fdSPaul Mullowney cusparsestruct->stream = stream; 5157d48284SJunchao Zhang stat = cusparseSetStream(cusparsestruct->handle,cusparsestruct->stream);CHKERRCUSPARSE(stat); 52b06137fdSPaul Mullowney PetscFunctionReturn(0); 53b06137fdSPaul Mullowney } 54b06137fdSPaul Mullowney 55b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSESetHandle(Mat A,const cusparseHandle_t handle) 56b06137fdSPaul Mullowney { 57b06137fdSPaul Mullowney cusparseStatus_t stat; 58b06137fdSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 59b06137fdSPaul Mullowney 60b06137fdSPaul Mullowney PetscFunctionBegin; 616b1cf21dSAlejandro Lamas Daviña if (cusparsestruct->handle != handle) { 6216a2e217SAlejandro Lamas Daviña if (cusparsestruct->handle) { 6357d48284SJunchao Zhang stat = cusparseDestroy(cusparsestruct->handle);CHKERRCUSPARSE(stat); 6416a2e217SAlejandro Lamas Daviña } 65b06137fdSPaul Mullowney cusparsestruct->handle = handle; 666b1cf21dSAlejandro Lamas Daviña } 6757d48284SJunchao Zhang stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUSPARSE(stat); 68b06137fdSPaul Mullowney PetscFunctionReturn(0); 69b06137fdSPaul Mullowney } 70b06137fdSPaul Mullowney 71b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSEClearHandle(Mat A) 72b06137fdSPaul Mullowney { 73b06137fdSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 74*ccdfe979SStefano Zampini 75b06137fdSPaul Mullowney PetscFunctionBegin; 76*ccdfe979SStefano Zampini if (cusparsestruct->handle) cusparsestruct->handle = 0; 77b06137fdSPaul Mullowney PetscFunctionReturn(0); 78b06137fdSPaul Mullowney } 79b06137fdSPaul Mullowney 80ea799195SBarry Smith PetscErrorCode MatFactorGetSolverType_seqaij_cusparse(Mat A,MatSolverType *type) 819ae82921SPaul Mullowney { 829ae82921SPaul Mullowney PetscFunctionBegin; 839ae82921SPaul Mullowney *type = MATSOLVERCUSPARSE; 849ae82921SPaul Mullowney PetscFunctionReturn(0); 859ae82921SPaul Mullowney } 869ae82921SPaul Mullowney 87c708e6cdSJed Brown /*MC 88087f3262SPaul Mullowney MATSOLVERCUSPARSE = "cusparse" - A matrix type providing triangular solvers for seq matrices 89087f3262SPaul Mullowney on a single GPU of type, seqaijcusparse, aijcusparse, or seqaijcusp, aijcusp. Currently supported 90087f3262SPaul Mullowney algorithms are ILU(k) and ICC(k). Typically, deeper factorizations (larger k) results in poorer 91087f3262SPaul Mullowney performance in the triangular solves. Full LU, and Cholesky decompositions can be solved through the 92087f3262SPaul Mullowney CUSPARSE triangular solve algorithm. However, the performance can be quite poor and thus these 93087f3262SPaul Mullowney algorithms are not recommended. This class does NOT support direct solver operations. 94c708e6cdSJed Brown 959ae82921SPaul Mullowney Level: beginner 96c708e6cdSJed Brown 973ca39a21SBarry Smith .seealso: PCFactorSetMatSolverType(), MatSolverType, MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation 98c708e6cdSJed Brown M*/ 999ae82921SPaul Mullowney 10042c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat A,MatFactorType ftype,Mat *B) 1019ae82921SPaul Mullowney { 1029ae82921SPaul Mullowney PetscErrorCode ierr; 103bc3f50f2SPaul Mullowney PetscInt n = A->rmap->n; 1049ae82921SPaul Mullowney 1059ae82921SPaul Mullowney PetscFunctionBegin; 106bc3f50f2SPaul Mullowney ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr); 107404133a2SPaul Mullowney (*B)->factortype = ftype; 108bc3f50f2SPaul Mullowney ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr); 1099ae82921SPaul Mullowney ierr = MatSetType(*B,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 1102205254eSKarl Rupp 111087f3262SPaul Mullowney if (ftype == MAT_FACTOR_LU || ftype == MAT_FACTOR_ILU || ftype == MAT_FACTOR_ILUDT) { 11233d57670SJed Brown ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr); 1139ae82921SPaul Mullowney (*B)->ops->ilufactorsymbolic = MatILUFactorSymbolic_SeqAIJCUSPARSE; 1149ae82921SPaul Mullowney (*B)->ops->lufactorsymbolic = MatLUFactorSymbolic_SeqAIJCUSPARSE; 115087f3262SPaul Mullowney } else if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) { 116087f3262SPaul Mullowney (*B)->ops->iccfactorsymbolic = MatICCFactorSymbolic_SeqAIJCUSPARSE; 117087f3262SPaul Mullowney (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqAIJCUSPARSE; 1189ae82921SPaul Mullowney } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported for CUSPARSE Matrix Types"); 119bc3f50f2SPaul Mullowney 120fa03d054SJed Brown ierr = MatSeqAIJSetPreallocation(*B,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr); 1213ca39a21SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)(*B),"MatFactorGetSolverType_C",MatFactorGetSolverType_seqaij_cusparse);CHKERRQ(ierr); 1229ae82921SPaul Mullowney PetscFunctionReturn(0); 1239ae82921SPaul Mullowney } 1249ae82921SPaul Mullowney 125bc3f50f2SPaul Mullowney PETSC_INTERN PetscErrorCode MatCUSPARSESetFormat_SeqAIJCUSPARSE(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format) 126ca45077fSPaul Mullowney { 127aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1286e111a19SKarl Rupp 129ca45077fSPaul Mullowney PetscFunctionBegin; 130ca45077fSPaul Mullowney switch (op) { 131e057df02SPaul Mullowney case MAT_CUSPARSE_MULT: 132aa372e3fSPaul Mullowney cusparsestruct->format = format; 133ca45077fSPaul Mullowney break; 134e057df02SPaul Mullowney case MAT_CUSPARSE_ALL: 135aa372e3fSPaul Mullowney cusparsestruct->format = format; 136ca45077fSPaul Mullowney break; 137ca45077fSPaul Mullowney default: 13836d62e41SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unsupported operation %d for MatCUSPARSEFormatOperation. MAT_CUSPARSE_MULT and MAT_CUSPARSE_ALL are currently supported.",op); 139ca45077fSPaul Mullowney } 140ca45077fSPaul Mullowney PetscFunctionReturn(0); 141ca45077fSPaul Mullowney } 1429ae82921SPaul Mullowney 143e057df02SPaul Mullowney /*@ 144e057df02SPaul Mullowney MatCUSPARSESetFormat - Sets the storage format of CUSPARSE matrices for a particular 145e057df02SPaul Mullowney operation. Only the MatMult operation can use different GPU storage formats 146aa372e3fSPaul Mullowney for MPIAIJCUSPARSE matrices. 147e057df02SPaul Mullowney Not Collective 148e057df02SPaul Mullowney 149e057df02SPaul Mullowney Input Parameters: 1508468deeeSKarl Rupp + A - Matrix of type SEQAIJCUSPARSE 15136d62e41SPaul Mullowney . op - MatCUSPARSEFormatOperation. SEQAIJCUSPARSE matrices support MAT_CUSPARSE_MULT and MAT_CUSPARSE_ALL. MPIAIJCUSPARSE matrices support MAT_CUSPARSE_MULT_DIAG, MAT_CUSPARSE_MULT_OFFDIAG, and MAT_CUSPARSE_ALL. 1522692e278SPaul Mullowney - format - MatCUSPARSEStorageFormat (one of MAT_CUSPARSE_CSR, MAT_CUSPARSE_ELL, MAT_CUSPARSE_HYB. The latter two require CUDA 4.2) 153e057df02SPaul Mullowney 154e057df02SPaul Mullowney Output Parameter: 155e057df02SPaul Mullowney 156e057df02SPaul Mullowney Level: intermediate 157e057df02SPaul Mullowney 1588468deeeSKarl Rupp .seealso: MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation 159e057df02SPaul Mullowney @*/ 160e057df02SPaul Mullowney PetscErrorCode MatCUSPARSESetFormat(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format) 161e057df02SPaul Mullowney { 162e057df02SPaul Mullowney PetscErrorCode ierr; 1636e111a19SKarl Rupp 164e057df02SPaul Mullowney PetscFunctionBegin; 165e057df02SPaul Mullowney PetscValidHeaderSpecific(A, MAT_CLASSID,1); 166e057df02SPaul Mullowney ierr = PetscTryMethod(A,"MatCUSPARSESetFormat_C",(Mat,MatCUSPARSEFormatOperation,MatCUSPARSEStorageFormat),(A,op,format));CHKERRQ(ierr); 167e057df02SPaul Mullowney PetscFunctionReturn(0); 168e057df02SPaul Mullowney } 169e057df02SPaul Mullowney 1704416b707SBarry Smith static PetscErrorCode MatSetFromOptions_SeqAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat A) 1719ae82921SPaul Mullowney { 1729ae82921SPaul Mullowney PetscErrorCode ierr; 173e057df02SPaul Mullowney MatCUSPARSEStorageFormat format; 1749ae82921SPaul Mullowney PetscBool flg; 175a183c035SDominic Meiser Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1766e111a19SKarl Rupp 1779ae82921SPaul Mullowney PetscFunctionBegin; 178e55864a3SBarry Smith ierr = PetscOptionsHead(PetscOptionsObject,"SeqAIJCUSPARSE options");CHKERRQ(ierr); 1799ae82921SPaul Mullowney if (A->factortype==MAT_FACTOR_NONE) { 180e057df02SPaul Mullowney ierr = PetscOptionsEnum("-mat_cusparse_mult_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV", 181a183c035SDominic Meiser "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr); 182e057df02SPaul Mullowney if (flg) { 183e057df02SPaul Mullowney ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_MULT,format);CHKERRQ(ierr); 184045c96e1SPaul Mullowney } 1859ae82921SPaul Mullowney } 1864c87dfd4SPaul Mullowney ierr = PetscOptionsEnum("-mat_cusparse_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV and TriSolve", 187a183c035SDominic Meiser "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr); 1884c87dfd4SPaul Mullowney if (flg) { 1894c87dfd4SPaul Mullowney ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_ALL,format);CHKERRQ(ierr); 1904c87dfd4SPaul Mullowney } 1910af67c1bSStefano Zampini ierr = PetscOptionsTail();CHKERRQ(ierr); 1929ae82921SPaul Mullowney PetscFunctionReturn(0); 1939ae82921SPaul Mullowney } 1949ae82921SPaul Mullowney 1956fa9248bSJed Brown static PetscErrorCode MatILUFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 1969ae82921SPaul Mullowney { 1979ae82921SPaul Mullowney PetscErrorCode ierr; 1989ae82921SPaul Mullowney 1999ae82921SPaul Mullowney PetscFunctionBegin; 2009ae82921SPaul Mullowney ierr = MatILUFactorSymbolic_SeqAIJ(B,A,isrow,iscol,info);CHKERRQ(ierr); 2019ae82921SPaul Mullowney B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJCUSPARSE; 2029ae82921SPaul Mullowney PetscFunctionReturn(0); 2039ae82921SPaul Mullowney } 2049ae82921SPaul Mullowney 2056fa9248bSJed Brown static PetscErrorCode MatLUFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 2069ae82921SPaul Mullowney { 2079ae82921SPaul Mullowney PetscErrorCode ierr; 2089ae82921SPaul Mullowney 2099ae82921SPaul Mullowney PetscFunctionBegin; 2109ae82921SPaul Mullowney ierr = MatLUFactorSymbolic_SeqAIJ(B,A,isrow,iscol,info);CHKERRQ(ierr); 2119ae82921SPaul Mullowney B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJCUSPARSE; 2129ae82921SPaul Mullowney PetscFunctionReturn(0); 2139ae82921SPaul Mullowney } 2149ae82921SPaul Mullowney 215087f3262SPaul Mullowney static PetscErrorCode MatICCFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS perm,const MatFactorInfo *info) 216087f3262SPaul Mullowney { 217087f3262SPaul Mullowney PetscErrorCode ierr; 218087f3262SPaul Mullowney 219087f3262SPaul Mullowney PetscFunctionBegin; 220087f3262SPaul Mullowney ierr = MatICCFactorSymbolic_SeqAIJ(B,A,perm,info);CHKERRQ(ierr); 221087f3262SPaul Mullowney B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJCUSPARSE; 222087f3262SPaul Mullowney PetscFunctionReturn(0); 223087f3262SPaul Mullowney } 224087f3262SPaul Mullowney 225087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS perm,const MatFactorInfo *info) 226087f3262SPaul Mullowney { 227087f3262SPaul Mullowney PetscErrorCode ierr; 228087f3262SPaul Mullowney 229087f3262SPaul Mullowney PetscFunctionBegin; 230087f3262SPaul Mullowney ierr = MatCholeskyFactorSymbolic_SeqAIJ(B,A,perm,info);CHKERRQ(ierr); 231087f3262SPaul Mullowney B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJCUSPARSE; 232087f3262SPaul Mullowney PetscFunctionReturn(0); 233087f3262SPaul Mullowney } 234087f3262SPaul Mullowney 235087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILULowerTriMatrix(Mat A) 2369ae82921SPaul Mullowney { 2379ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2389ae82921SPaul Mullowney PetscInt n = A->rmap->n; 2399ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 240aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 2419ae82921SPaul Mullowney cusparseStatus_t stat; 2429ae82921SPaul Mullowney const PetscInt *ai = a->i,*aj = a->j,*vi; 2439ae82921SPaul Mullowney const MatScalar *aa = a->a,*v; 2449ae82921SPaul Mullowney PetscInt *AiLo, *AjLo; 2459ae82921SPaul Mullowney PetscScalar *AALo; 2469ae82921SPaul Mullowney PetscInt i,nz, nzLower, offset, rowOffset; 247b175d8bbSPaul Mullowney PetscErrorCode ierr; 24857d48284SJunchao Zhang cudaError_t cerr; 2499ae82921SPaul Mullowney 2509ae82921SPaul Mullowney PetscFunctionBegin; 251cf00fe3bSKarl Rupp if (!n) PetscFunctionReturn(0); 252c70f7ee4SJunchao Zhang if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) { 2539ae82921SPaul Mullowney try { 2549ae82921SPaul Mullowney /* first figure out the number of nonzeros in the lower triangular matrix including 1's on the diagonal. */ 2559ae82921SPaul Mullowney nzLower=n+ai[n]-ai[1]; 2569ae82921SPaul Mullowney 2579ae82921SPaul Mullowney /* Allocate Space for the lower triangular matrix */ 25857d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AiLo, (n+1)*sizeof(PetscInt));CHKERRCUDA(cerr); 25957d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AjLo, nzLower*sizeof(PetscInt));CHKERRCUDA(cerr); 26057d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AALo, nzLower*sizeof(PetscScalar));CHKERRCUDA(cerr); 2619ae82921SPaul Mullowney 2629ae82921SPaul Mullowney /* Fill the lower triangular matrix */ 2639ae82921SPaul Mullowney AiLo[0] = (PetscInt) 0; 2649ae82921SPaul Mullowney AiLo[n] = nzLower; 2659ae82921SPaul Mullowney AjLo[0] = (PetscInt) 0; 2669ae82921SPaul Mullowney AALo[0] = (MatScalar) 1.0; 2679ae82921SPaul Mullowney v = aa; 2689ae82921SPaul Mullowney vi = aj; 2699ae82921SPaul Mullowney offset = 1; 2709ae82921SPaul Mullowney rowOffset= 1; 2719ae82921SPaul Mullowney for (i=1; i<n; i++) { 2729ae82921SPaul Mullowney nz = ai[i+1] - ai[i]; 273e057df02SPaul Mullowney /* additional 1 for the term on the diagonal */ 2749ae82921SPaul Mullowney AiLo[i] = rowOffset; 2759ae82921SPaul Mullowney rowOffset += nz+1; 2769ae82921SPaul Mullowney 277580bdb30SBarry Smith ierr = PetscArraycpy(&(AjLo[offset]), vi, nz);CHKERRQ(ierr); 278580bdb30SBarry Smith ierr = PetscArraycpy(&(AALo[offset]), v, nz);CHKERRQ(ierr); 2799ae82921SPaul Mullowney 2809ae82921SPaul Mullowney offset += nz; 2819ae82921SPaul Mullowney AjLo[offset] = (PetscInt) i; 2829ae82921SPaul Mullowney AALo[offset] = (MatScalar) 1.0; 2839ae82921SPaul Mullowney offset += 1; 2849ae82921SPaul Mullowney 2859ae82921SPaul Mullowney v += nz; 2869ae82921SPaul Mullowney vi += nz; 2879ae82921SPaul Mullowney } 2882205254eSKarl Rupp 289aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 290aa372e3fSPaul Mullowney loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 2912205254eSKarl Rupp 292aa372e3fSPaul Mullowney /* Create the matrix description */ 29357d48284SJunchao Zhang stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUSPARSE(stat); 29457d48284SJunchao Zhang stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat); 29557d48284SJunchao Zhang stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUSPARSE(stat); 29657d48284SJunchao Zhang stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_LOWER);CHKERRCUSPARSE(stat); 29757d48284SJunchao Zhang stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUSPARSE(stat); 298aa372e3fSPaul Mullowney 299aa372e3fSPaul Mullowney /* Create the solve analysis information */ 30057d48284SJunchao Zhang stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUSPARSE(stat); 301aa372e3fSPaul Mullowney 302aa372e3fSPaul Mullowney /* set the operation */ 303aa372e3fSPaul Mullowney loTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 304aa372e3fSPaul Mullowney 305aa372e3fSPaul Mullowney /* set the matrix */ 306aa372e3fSPaul Mullowney loTriFactor->csrMat = new CsrMatrix; 307aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows = n; 308aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols = n; 309aa372e3fSPaul Mullowney loTriFactor->csrMat->num_entries = nzLower; 310aa372e3fSPaul Mullowney 311aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1); 312aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->assign(AiLo, AiLo+n+1); 313aa372e3fSPaul Mullowney 314aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzLower); 315aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->assign(AjLo, AjLo+nzLower); 316aa372e3fSPaul Mullowney 317aa372e3fSPaul Mullowney loTriFactor->csrMat->values = new THRUSTARRAY(nzLower); 318aa372e3fSPaul Mullowney loTriFactor->csrMat->values->assign(AALo, AALo+nzLower); 319aa372e3fSPaul Mullowney 320aa372e3fSPaul Mullowney /* perform the solve analysis */ 321aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp, 322aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr, 323aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(), 32457d48284SJunchao Zhang loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUSPARSE(stat); 325aa372e3fSPaul Mullowney 326aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 327aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor; 3282205254eSKarl Rupp 32957d48284SJunchao Zhang cerr = cudaFreeHost(AiLo);CHKERRCUDA(cerr); 33057d48284SJunchao Zhang cerr = cudaFreeHost(AjLo);CHKERRCUDA(cerr); 33157d48284SJunchao Zhang cerr = cudaFreeHost(AALo);CHKERRCUDA(cerr); 3324863603aSSatish Balay ierr = PetscLogCpuToGpu((n+1+nzLower)*sizeof(int)+nzLower*sizeof(PetscScalar));CHKERRQ(ierr); 3339ae82921SPaul Mullowney } catch(char *ex) { 3349ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 3359ae82921SPaul Mullowney } 3369ae82921SPaul Mullowney } 3379ae82921SPaul Mullowney PetscFunctionReturn(0); 3389ae82921SPaul Mullowney } 3399ae82921SPaul Mullowney 340087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(Mat A) 3419ae82921SPaul Mullowney { 3429ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3439ae82921SPaul Mullowney PetscInt n = A->rmap->n; 3449ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 345aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 3469ae82921SPaul Mullowney cusparseStatus_t stat; 3479ae82921SPaul Mullowney const PetscInt *aj = a->j,*adiag = a->diag,*vi; 3489ae82921SPaul Mullowney const MatScalar *aa = a->a,*v; 3499ae82921SPaul Mullowney PetscInt *AiUp, *AjUp; 3509ae82921SPaul Mullowney PetscScalar *AAUp; 3519ae82921SPaul Mullowney PetscInt i,nz, nzUpper, offset; 3529ae82921SPaul Mullowney PetscErrorCode ierr; 35357d48284SJunchao Zhang cudaError_t cerr; 3549ae82921SPaul Mullowney 3559ae82921SPaul Mullowney PetscFunctionBegin; 356cf00fe3bSKarl Rupp if (!n) PetscFunctionReturn(0); 357c70f7ee4SJunchao Zhang if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) { 3589ae82921SPaul Mullowney try { 3599ae82921SPaul Mullowney /* next, figure out the number of nonzeros in the upper triangular matrix. */ 3609ae82921SPaul Mullowney nzUpper = adiag[0]-adiag[n]; 3619ae82921SPaul Mullowney 3629ae82921SPaul Mullowney /* Allocate Space for the upper triangular matrix */ 36357d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(cerr); 36457d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(cerr); 36557d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(cerr); 3669ae82921SPaul Mullowney 3679ae82921SPaul Mullowney /* Fill the upper triangular matrix */ 3689ae82921SPaul Mullowney AiUp[0]=(PetscInt) 0; 3699ae82921SPaul Mullowney AiUp[n]=nzUpper; 3709ae82921SPaul Mullowney offset = nzUpper; 3719ae82921SPaul Mullowney for (i=n-1; i>=0; i--) { 3729ae82921SPaul Mullowney v = aa + adiag[i+1] + 1; 3739ae82921SPaul Mullowney vi = aj + adiag[i+1] + 1; 3749ae82921SPaul Mullowney 375e057df02SPaul Mullowney /* number of elements NOT on the diagonal */ 3769ae82921SPaul Mullowney nz = adiag[i] - adiag[i+1]-1; 3779ae82921SPaul Mullowney 378e057df02SPaul Mullowney /* decrement the offset */ 3799ae82921SPaul Mullowney offset -= (nz+1); 3809ae82921SPaul Mullowney 381e057df02SPaul Mullowney /* first, set the diagonal elements */ 3829ae82921SPaul Mullowney AjUp[offset] = (PetscInt) i; 38309f51544SAlejandro Lamas Daviña AAUp[offset] = (MatScalar)1./v[nz]; 3849ae82921SPaul Mullowney AiUp[i] = AiUp[i+1] - (nz+1); 3859ae82921SPaul Mullowney 386580bdb30SBarry Smith ierr = PetscArraycpy(&(AjUp[offset+1]), vi, nz);CHKERRQ(ierr); 387580bdb30SBarry Smith ierr = PetscArraycpy(&(AAUp[offset+1]), v, nz);CHKERRQ(ierr); 3889ae82921SPaul Mullowney } 3892205254eSKarl Rupp 390aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 391aa372e3fSPaul Mullowney upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 3922205254eSKarl Rupp 393aa372e3fSPaul Mullowney /* Create the matrix description */ 39457d48284SJunchao Zhang stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUSPARSE(stat); 39557d48284SJunchao Zhang stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat); 39657d48284SJunchao Zhang stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUSPARSE(stat); 39757d48284SJunchao Zhang stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUSPARSE(stat); 39857d48284SJunchao Zhang stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUSPARSE(stat); 399aa372e3fSPaul Mullowney 400aa372e3fSPaul Mullowney /* Create the solve analysis information */ 40157d48284SJunchao Zhang stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUSPARSE(stat); 402aa372e3fSPaul Mullowney 403aa372e3fSPaul Mullowney /* set the operation */ 404aa372e3fSPaul Mullowney upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 405aa372e3fSPaul Mullowney 406aa372e3fSPaul Mullowney /* set the matrix */ 407aa372e3fSPaul Mullowney upTriFactor->csrMat = new CsrMatrix; 408aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows = n; 409aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols = n; 410aa372e3fSPaul Mullowney upTriFactor->csrMat->num_entries = nzUpper; 411aa372e3fSPaul Mullowney 412aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1); 413aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+n+1); 414aa372e3fSPaul Mullowney 415aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzUpper); 416aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+nzUpper); 417aa372e3fSPaul Mullowney 418aa372e3fSPaul Mullowney upTriFactor->csrMat->values = new THRUSTARRAY(nzUpper); 419aa372e3fSPaul Mullowney upTriFactor->csrMat->values->assign(AAUp, AAUp+nzUpper); 420aa372e3fSPaul Mullowney 421aa372e3fSPaul Mullowney /* perform the solve analysis */ 422aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp, 423aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr, 424aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(), 42557d48284SJunchao Zhang upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUSPARSE(stat); 426aa372e3fSPaul Mullowney 427aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 428aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor; 4292205254eSKarl Rupp 43057d48284SJunchao Zhang cerr = cudaFreeHost(AiUp);CHKERRCUDA(cerr); 43157d48284SJunchao Zhang cerr = cudaFreeHost(AjUp);CHKERRCUDA(cerr); 43257d48284SJunchao Zhang cerr = cudaFreeHost(AAUp);CHKERRCUDA(cerr); 4334863603aSSatish Balay ierr = PetscLogCpuToGpu((n+1+nzUpper)*sizeof(int)+nzUpper*sizeof(PetscScalar));CHKERRQ(ierr); 4349ae82921SPaul Mullowney } catch(char *ex) { 4359ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 4369ae82921SPaul Mullowney } 4379ae82921SPaul Mullowney } 4389ae82921SPaul Mullowney PetscFunctionReturn(0); 4399ae82921SPaul Mullowney } 4409ae82921SPaul Mullowney 441087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(Mat A) 4429ae82921SPaul Mullowney { 4439ae82921SPaul Mullowney PetscErrorCode ierr; 4449ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 4459ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 4469ae82921SPaul Mullowney IS isrow = a->row,iscol = a->icol; 4479ae82921SPaul Mullowney PetscBool row_identity,col_identity; 4489ae82921SPaul Mullowney const PetscInt *r,*c; 4499ae82921SPaul Mullowney PetscInt n = A->rmap->n; 4509ae82921SPaul Mullowney 4519ae82921SPaul Mullowney PetscFunctionBegin; 452*ccdfe979SStefano Zampini ierr = MatSeqAIJCUSPARSETriFactors_Reset(&cusparseTriFactors);CHKERRQ(ierr); 453087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildILULowerTriMatrix(A);CHKERRQ(ierr); 454087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(A);CHKERRQ(ierr); 4552205254eSKarl Rupp 456e65717acSKarl Rupp cusparseTriFactors->workVector = new THRUSTARRAY(n); 457aa372e3fSPaul Mullowney cusparseTriFactors->nnz=a->nz; 4589ae82921SPaul Mullowney 459c70f7ee4SJunchao Zhang A->offloadmask = PETSC_OFFLOAD_BOTH; 460e057df02SPaul Mullowney /* lower triangular indices */ 4619ae82921SPaul Mullowney ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 4629ae82921SPaul Mullowney ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 4632205254eSKarl Rupp if (!row_identity) { 464aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n); 465aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices->assign(r, r+n); 4662205254eSKarl Rupp } 4679ae82921SPaul Mullowney ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 4689ae82921SPaul Mullowney 469e057df02SPaul Mullowney /* upper triangular indices */ 4709ae82921SPaul Mullowney ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); 4719ae82921SPaul Mullowney ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 4722205254eSKarl Rupp if (!col_identity) { 473aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n); 474aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices->assign(c, c+n); 4752205254eSKarl Rupp } 4764863603aSSatish Balay 4774863603aSSatish Balay if (!row_identity && !col_identity) { 4784863603aSSatish Balay ierr = PetscLogCpuToGpu(2*n*sizeof(PetscInt));CHKERRQ(ierr); 4794863603aSSatish Balay } else if(!row_identity) { 4804863603aSSatish Balay ierr = PetscLogCpuToGpu(n*sizeof(PetscInt));CHKERRQ(ierr); 4814863603aSSatish Balay } else if(!col_identity) { 4824863603aSSatish Balay ierr = PetscLogCpuToGpu(n*sizeof(PetscInt));CHKERRQ(ierr); 4834863603aSSatish Balay } 4844863603aSSatish Balay 4859ae82921SPaul Mullowney ierr = ISRestoreIndices(iscol,&c);CHKERRQ(ierr); 4869ae82921SPaul Mullowney PetscFunctionReturn(0); 4879ae82921SPaul Mullowney } 4889ae82921SPaul Mullowney 489087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildICCTriMatrices(Mat A) 490087f3262SPaul Mullowney { 491087f3262SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 492087f3262SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 493aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 494aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 495087f3262SPaul Mullowney cusparseStatus_t stat; 496087f3262SPaul Mullowney PetscErrorCode ierr; 49757d48284SJunchao Zhang cudaError_t cerr; 498087f3262SPaul Mullowney PetscInt *AiUp, *AjUp; 499087f3262SPaul Mullowney PetscScalar *AAUp; 500087f3262SPaul Mullowney PetscScalar *AALo; 501087f3262SPaul Mullowney PetscInt nzUpper = a->nz,n = A->rmap->n,i,offset,nz,j; 502087f3262SPaul Mullowney Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)A->data; 503087f3262SPaul Mullowney const PetscInt *ai = b->i,*aj = b->j,*vj; 504087f3262SPaul Mullowney const MatScalar *aa = b->a,*v; 505087f3262SPaul Mullowney 506087f3262SPaul Mullowney PetscFunctionBegin; 507cf00fe3bSKarl Rupp if (!n) PetscFunctionReturn(0); 508c70f7ee4SJunchao Zhang if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) { 509087f3262SPaul Mullowney try { 510087f3262SPaul Mullowney /* Allocate Space for the upper triangular matrix */ 51157d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(cerr); 51257d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(cerr); 51357d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(cerr); 51457d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AALo, nzUpper*sizeof(PetscScalar));CHKERRCUDA(cerr); 515087f3262SPaul Mullowney 516087f3262SPaul Mullowney /* Fill the upper triangular matrix */ 517087f3262SPaul Mullowney AiUp[0]=(PetscInt) 0; 518087f3262SPaul Mullowney AiUp[n]=nzUpper; 519087f3262SPaul Mullowney offset = 0; 520087f3262SPaul Mullowney for (i=0; i<n; i++) { 521087f3262SPaul Mullowney /* set the pointers */ 522087f3262SPaul Mullowney v = aa + ai[i]; 523087f3262SPaul Mullowney vj = aj + ai[i]; 524087f3262SPaul Mullowney nz = ai[i+1] - ai[i] - 1; /* exclude diag[i] */ 525087f3262SPaul Mullowney 526087f3262SPaul Mullowney /* first, set the diagonal elements */ 527087f3262SPaul Mullowney AjUp[offset] = (PetscInt) i; 52809f51544SAlejandro Lamas Daviña AAUp[offset] = (MatScalar)1.0/v[nz]; 529087f3262SPaul Mullowney AiUp[i] = offset; 53009f51544SAlejandro Lamas Daviña AALo[offset] = (MatScalar)1.0/v[nz]; 531087f3262SPaul Mullowney 532087f3262SPaul Mullowney offset+=1; 533087f3262SPaul Mullowney if (nz>0) { 534f22e0265SBarry Smith ierr = PetscArraycpy(&(AjUp[offset]), vj, nz);CHKERRQ(ierr); 535580bdb30SBarry Smith ierr = PetscArraycpy(&(AAUp[offset]), v, nz);CHKERRQ(ierr); 536087f3262SPaul Mullowney for (j=offset; j<offset+nz; j++) { 537087f3262SPaul Mullowney AAUp[j] = -AAUp[j]; 538087f3262SPaul Mullowney AALo[j] = AAUp[j]/v[nz]; 539087f3262SPaul Mullowney } 540087f3262SPaul Mullowney offset+=nz; 541087f3262SPaul Mullowney } 542087f3262SPaul Mullowney } 543087f3262SPaul Mullowney 544aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 545aa372e3fSPaul Mullowney upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 546087f3262SPaul Mullowney 547aa372e3fSPaul Mullowney /* Create the matrix description */ 54857d48284SJunchao Zhang stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUSPARSE(stat); 54957d48284SJunchao Zhang stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat); 55057d48284SJunchao Zhang stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUSPARSE(stat); 55157d48284SJunchao Zhang stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUSPARSE(stat); 55257d48284SJunchao Zhang stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUSPARSE(stat); 553087f3262SPaul Mullowney 554aa372e3fSPaul Mullowney /* Create the solve analysis information */ 55557d48284SJunchao Zhang stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUSPARSE(stat); 556aa372e3fSPaul Mullowney 557aa372e3fSPaul Mullowney /* set the operation */ 558aa372e3fSPaul Mullowney upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 559aa372e3fSPaul Mullowney 560aa372e3fSPaul Mullowney /* set the matrix */ 561aa372e3fSPaul Mullowney upTriFactor->csrMat = new CsrMatrix; 562aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows = A->rmap->n; 563aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols = A->cmap->n; 564aa372e3fSPaul Mullowney upTriFactor->csrMat->num_entries = a->nz; 565aa372e3fSPaul Mullowney 566aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 567aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1); 568aa372e3fSPaul Mullowney 569aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz); 570aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz); 571aa372e3fSPaul Mullowney 572aa372e3fSPaul Mullowney upTriFactor->csrMat->values = new THRUSTARRAY(a->nz); 573aa372e3fSPaul Mullowney upTriFactor->csrMat->values->assign(AAUp, AAUp+a->nz); 574aa372e3fSPaul Mullowney 575aa372e3fSPaul Mullowney /* perform the solve analysis */ 576aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp, 577aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr, 578aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(), 57957d48284SJunchao Zhang upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUSPARSE(stat); 580aa372e3fSPaul Mullowney 581aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 582aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor; 583aa372e3fSPaul Mullowney 584aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 585aa372e3fSPaul Mullowney loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 586aa372e3fSPaul Mullowney 587aa372e3fSPaul Mullowney /* Create the matrix description */ 58857d48284SJunchao Zhang stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUSPARSE(stat); 58957d48284SJunchao Zhang stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat); 59057d48284SJunchao Zhang stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUSPARSE(stat); 59157d48284SJunchao Zhang stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUSPARSE(stat); 59257d48284SJunchao Zhang stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUSPARSE(stat); 593aa372e3fSPaul Mullowney 594aa372e3fSPaul Mullowney /* Create the solve analysis information */ 59557d48284SJunchao Zhang stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUSPARSE(stat); 596aa372e3fSPaul Mullowney 597aa372e3fSPaul Mullowney /* set the operation */ 598aa372e3fSPaul Mullowney loTriFactor->solveOp = CUSPARSE_OPERATION_TRANSPOSE; 599aa372e3fSPaul Mullowney 600aa372e3fSPaul Mullowney /* set the matrix */ 601aa372e3fSPaul Mullowney loTriFactor->csrMat = new CsrMatrix; 602aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows = A->rmap->n; 603aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols = A->cmap->n; 604aa372e3fSPaul Mullowney loTriFactor->csrMat->num_entries = a->nz; 605aa372e3fSPaul Mullowney 606aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 607aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1); 608aa372e3fSPaul Mullowney 609aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz); 610aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz); 611aa372e3fSPaul Mullowney 612aa372e3fSPaul Mullowney loTriFactor->csrMat->values = new THRUSTARRAY(a->nz); 613aa372e3fSPaul Mullowney loTriFactor->csrMat->values->assign(AALo, AALo+a->nz); 6144863603aSSatish Balay ierr = PetscLogCpuToGpu(2*(((A->rmap->n+1)+(a->nz))*sizeof(int)+(a->nz)*sizeof(PetscScalar)));CHKERRQ(ierr); 615aa372e3fSPaul Mullowney 616aa372e3fSPaul Mullowney /* perform the solve analysis */ 617aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp, 618aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr, 619aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(), 62057d48284SJunchao Zhang loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUSPARSE(stat); 621aa372e3fSPaul Mullowney 622aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 623aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor; 624087f3262SPaul Mullowney 625c70f7ee4SJunchao Zhang A->offloadmask = PETSC_OFFLOAD_BOTH; 62657d48284SJunchao Zhang cerr = cudaFreeHost(AiUp);CHKERRCUDA(cerr); 62757d48284SJunchao Zhang cerr = cudaFreeHost(AjUp);CHKERRCUDA(cerr); 62857d48284SJunchao Zhang cerr = cudaFreeHost(AAUp);CHKERRCUDA(cerr); 62957d48284SJunchao Zhang cerr = cudaFreeHost(AALo);CHKERRCUDA(cerr); 630087f3262SPaul Mullowney } catch(char *ex) { 631087f3262SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 632087f3262SPaul Mullowney } 633087f3262SPaul Mullowney } 634087f3262SPaul Mullowney PetscFunctionReturn(0); 635087f3262SPaul Mullowney } 636087f3262SPaul Mullowney 637087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(Mat A) 6389ae82921SPaul Mullowney { 6399ae82921SPaul Mullowney PetscErrorCode ierr; 640087f3262SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 641087f3262SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 642087f3262SPaul Mullowney IS ip = a->row; 643087f3262SPaul Mullowney const PetscInt *rip; 644087f3262SPaul Mullowney PetscBool perm_identity; 645087f3262SPaul Mullowney PetscInt n = A->rmap->n; 646087f3262SPaul Mullowney 647087f3262SPaul Mullowney PetscFunctionBegin; 648*ccdfe979SStefano Zampini ierr = MatSeqAIJCUSPARSETriFactors_Reset(&cusparseTriFactors);CHKERRQ(ierr); 649087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildICCTriMatrices(A);CHKERRQ(ierr); 650e65717acSKarl Rupp cusparseTriFactors->workVector = new THRUSTARRAY(n); 651aa372e3fSPaul Mullowney cusparseTriFactors->nnz=(a->nz-n)*2 + n; 652aa372e3fSPaul Mullowney 653087f3262SPaul Mullowney /* lower triangular indices */ 654087f3262SPaul Mullowney ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); 655087f3262SPaul Mullowney ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 656087f3262SPaul Mullowney if (!perm_identity) { 6574e4bbfaaSStefano Zampini IS iip; 6584e4bbfaaSStefano Zampini const PetscInt *irip; 6594e4bbfaaSStefano Zampini 6604e4bbfaaSStefano Zampini ierr = ISInvertPermutation(ip,PETSC_DECIDE,&iip);CHKERRQ(ierr); 6614e4bbfaaSStefano Zampini ierr = ISGetIndices(iip,&irip);CHKERRQ(ierr); 662aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n); 663aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices->assign(rip, rip+n); 664aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n); 6654e4bbfaaSStefano Zampini cusparseTriFactors->cpermIndices->assign(irip, irip+n); 6664e4bbfaaSStefano Zampini ierr = ISRestoreIndices(iip,&irip);CHKERRQ(ierr); 6674e4bbfaaSStefano Zampini ierr = ISDestroy(&iip);CHKERRQ(ierr); 6684863603aSSatish Balay ierr = PetscLogCpuToGpu(2*n*sizeof(PetscInt));CHKERRQ(ierr); 669087f3262SPaul Mullowney } 670087f3262SPaul Mullowney ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); 671087f3262SPaul Mullowney PetscFunctionReturn(0); 672087f3262SPaul Mullowney } 673087f3262SPaul Mullowney 6746fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info) 6759ae82921SPaul Mullowney { 6769ae82921SPaul Mullowney Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 6779ae82921SPaul Mullowney IS isrow = b->row,iscol = b->col; 6789ae82921SPaul Mullowney PetscBool row_identity,col_identity; 679b175d8bbSPaul Mullowney PetscErrorCode ierr; 6809ae82921SPaul Mullowney 6819ae82921SPaul Mullowney PetscFunctionBegin; 6829ae82921SPaul Mullowney ierr = MatLUFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr); 683*ccdfe979SStefano Zampini B->offloadmask = PETSC_OFFLOAD_CPU; 684e057df02SPaul Mullowney /* determine which version of MatSolve needs to be used. */ 6859ae82921SPaul Mullowney ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 6869ae82921SPaul Mullowney ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 687bda325fcSPaul Mullowney if (row_identity && col_identity) { 688bda325fcSPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering; 689bda325fcSPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering; 6904e4bbfaaSStefano Zampini B->ops->matsolve = NULL; 6914e4bbfaaSStefano Zampini B->ops->matsolvetranspose = NULL; 692bda325fcSPaul Mullowney } else { 693bda325fcSPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE; 694bda325fcSPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE; 6954e4bbfaaSStefano Zampini B->ops->matsolve = NULL; 6964e4bbfaaSStefano Zampini B->ops->matsolvetranspose = NULL; 697bda325fcSPaul Mullowney } 6988dc1d2a3SPaul Mullowney 699e057df02SPaul Mullowney /* get the triangular factors */ 700087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(B);CHKERRQ(ierr); 7019ae82921SPaul Mullowney PetscFunctionReturn(0); 7029ae82921SPaul Mullowney } 7039ae82921SPaul Mullowney 704087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info) 705087f3262SPaul Mullowney { 706087f3262SPaul Mullowney Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 707087f3262SPaul Mullowney IS ip = b->row; 708087f3262SPaul Mullowney PetscBool perm_identity; 709b175d8bbSPaul Mullowney PetscErrorCode ierr; 710087f3262SPaul Mullowney 711087f3262SPaul Mullowney PetscFunctionBegin; 712087f3262SPaul Mullowney ierr = MatCholeskyFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr); 713*ccdfe979SStefano Zampini B->offloadmask = PETSC_OFFLOAD_CPU; 714087f3262SPaul Mullowney /* determine which version of MatSolve needs to be used. */ 715087f3262SPaul Mullowney ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 716087f3262SPaul Mullowney if (perm_identity) { 717087f3262SPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering; 718087f3262SPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering; 7194e4bbfaaSStefano Zampini B->ops->matsolve = NULL; 7204e4bbfaaSStefano Zampini B->ops->matsolvetranspose = NULL; 721087f3262SPaul Mullowney } else { 722087f3262SPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE; 723087f3262SPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE; 7244e4bbfaaSStefano Zampini B->ops->matsolve = NULL; 7254e4bbfaaSStefano Zampini B->ops->matsolvetranspose = NULL; 726087f3262SPaul Mullowney } 727087f3262SPaul Mullowney 728087f3262SPaul Mullowney /* get the triangular factors */ 729087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(B);CHKERRQ(ierr); 730087f3262SPaul Mullowney PetscFunctionReturn(0); 731087f3262SPaul Mullowney } 7329ae82921SPaul Mullowney 733b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(Mat A) 734bda325fcSPaul Mullowney { 735bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 736aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 737aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 738aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 739aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 740bda325fcSPaul Mullowney cusparseStatus_t stat; 741aa372e3fSPaul Mullowney cusparseIndexBase_t indexBase; 742aa372e3fSPaul Mullowney cusparseMatrixType_t matrixType; 743aa372e3fSPaul Mullowney cusparseFillMode_t fillMode; 744aa372e3fSPaul Mullowney cusparseDiagType_t diagType; 745b175d8bbSPaul Mullowney 746bda325fcSPaul Mullowney PetscFunctionBegin; 747bda325fcSPaul Mullowney 748aa372e3fSPaul Mullowney /*********************************************/ 749aa372e3fSPaul Mullowney /* Now the Transpose of the Lower Tri Factor */ 750aa372e3fSPaul Mullowney /*********************************************/ 751aa372e3fSPaul Mullowney 752aa372e3fSPaul Mullowney /* allocate space for the transpose of the lower triangular factor */ 753aa372e3fSPaul Mullowney loTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct; 754aa372e3fSPaul Mullowney 755aa372e3fSPaul Mullowney /* set the matrix descriptors of the lower triangular factor */ 756aa372e3fSPaul Mullowney matrixType = cusparseGetMatType(loTriFactor->descr); 757aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(loTriFactor->descr); 758aa372e3fSPaul Mullowney fillMode = cusparseGetMatFillMode(loTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ? 759aa372e3fSPaul Mullowney CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER; 760aa372e3fSPaul Mullowney diagType = cusparseGetMatDiagType(loTriFactor->descr); 761aa372e3fSPaul Mullowney 762aa372e3fSPaul Mullowney /* Create the matrix description */ 76357d48284SJunchao Zhang stat = cusparseCreateMatDescr(&loTriFactorT->descr);CHKERRCUSPARSE(stat); 76457d48284SJunchao Zhang stat = cusparseSetMatIndexBase(loTriFactorT->descr, indexBase);CHKERRCUSPARSE(stat); 76557d48284SJunchao Zhang stat = cusparseSetMatType(loTriFactorT->descr, matrixType);CHKERRCUSPARSE(stat); 76657d48284SJunchao Zhang stat = cusparseSetMatFillMode(loTriFactorT->descr, fillMode);CHKERRCUSPARSE(stat); 76757d48284SJunchao Zhang stat = cusparseSetMatDiagType(loTriFactorT->descr, diagType);CHKERRCUSPARSE(stat); 768aa372e3fSPaul Mullowney 769aa372e3fSPaul Mullowney /* Create the solve analysis information */ 77057d48284SJunchao Zhang stat = cusparseCreateSolveAnalysisInfo(&loTriFactorT->solveInfo);CHKERRCUSPARSE(stat); 771aa372e3fSPaul Mullowney 772aa372e3fSPaul Mullowney /* set the operation */ 773aa372e3fSPaul Mullowney loTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 774aa372e3fSPaul Mullowney 775aa372e3fSPaul Mullowney /* allocate GPU space for the CSC of the lower triangular factor*/ 776aa372e3fSPaul Mullowney loTriFactorT->csrMat = new CsrMatrix; 777aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_rows = loTriFactor->csrMat->num_rows; 778aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_cols = loTriFactor->csrMat->num_cols; 779aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_entries = loTriFactor->csrMat->num_entries; 780aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(loTriFactor->csrMat->num_rows+1); 781aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(loTriFactor->csrMat->num_entries); 782aa372e3fSPaul Mullowney loTriFactorT->csrMat->values = new THRUSTARRAY(loTriFactor->csrMat->num_entries); 783aa372e3fSPaul Mullowney 784aa372e3fSPaul Mullowney /* compute the transpose of the lower triangular factor, i.e. the CSC */ 785aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparseTriFactors->handle, loTriFactor->csrMat->num_rows, 786aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols, loTriFactor->csrMat->num_entries, 787aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 788aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 789aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 790aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 791aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 792aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 79357d48284SJunchao Zhang CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUSPARSE(stat); 794aa372e3fSPaul Mullowney 795aa372e3fSPaul Mullowney /* perform the solve analysis on the transposed matrix */ 796aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactorT->solveOp, 797aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_rows, loTriFactorT->csrMat->num_entries, 798aa372e3fSPaul Mullowney loTriFactorT->descr, loTriFactorT->csrMat->values->data().get(), 799aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), loTriFactorT->csrMat->column_indices->data().get(), 80057d48284SJunchao Zhang loTriFactorT->solveInfo);CHKERRCUSPARSE(stat); 801aa372e3fSPaul Mullowney 802aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 803aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtrTranspose = loTriFactorT; 804aa372e3fSPaul Mullowney 805aa372e3fSPaul Mullowney /*********************************************/ 806aa372e3fSPaul Mullowney /* Now the Transpose of the Upper Tri Factor */ 807aa372e3fSPaul Mullowney /*********************************************/ 808aa372e3fSPaul Mullowney 809aa372e3fSPaul Mullowney /* allocate space for the transpose of the upper triangular factor */ 810aa372e3fSPaul Mullowney upTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct; 811aa372e3fSPaul Mullowney 812aa372e3fSPaul Mullowney /* set the matrix descriptors of the upper triangular factor */ 813aa372e3fSPaul Mullowney matrixType = cusparseGetMatType(upTriFactor->descr); 814aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(upTriFactor->descr); 815aa372e3fSPaul Mullowney fillMode = cusparseGetMatFillMode(upTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ? 816aa372e3fSPaul Mullowney CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER; 817aa372e3fSPaul Mullowney diagType = cusparseGetMatDiagType(upTriFactor->descr); 818aa372e3fSPaul Mullowney 819aa372e3fSPaul Mullowney /* Create the matrix description */ 82057d48284SJunchao Zhang stat = cusparseCreateMatDescr(&upTriFactorT->descr);CHKERRCUSPARSE(stat); 82157d48284SJunchao Zhang stat = cusparseSetMatIndexBase(upTriFactorT->descr, indexBase);CHKERRCUSPARSE(stat); 82257d48284SJunchao Zhang stat = cusparseSetMatType(upTriFactorT->descr, matrixType);CHKERRCUSPARSE(stat); 82357d48284SJunchao Zhang stat = cusparseSetMatFillMode(upTriFactorT->descr, fillMode);CHKERRCUSPARSE(stat); 82457d48284SJunchao Zhang stat = cusparseSetMatDiagType(upTriFactorT->descr, diagType);CHKERRCUSPARSE(stat); 825aa372e3fSPaul Mullowney 826aa372e3fSPaul Mullowney /* Create the solve analysis information */ 82757d48284SJunchao Zhang stat = cusparseCreateSolveAnalysisInfo(&upTriFactorT->solveInfo);CHKERRCUSPARSE(stat); 828aa372e3fSPaul Mullowney 829aa372e3fSPaul Mullowney /* set the operation */ 830aa372e3fSPaul Mullowney upTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 831aa372e3fSPaul Mullowney 832aa372e3fSPaul Mullowney /* allocate GPU space for the CSC of the upper triangular factor*/ 833aa372e3fSPaul Mullowney upTriFactorT->csrMat = new CsrMatrix; 834aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_rows = upTriFactor->csrMat->num_rows; 835aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_cols = upTriFactor->csrMat->num_cols; 836aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_entries = upTriFactor->csrMat->num_entries; 837aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(upTriFactor->csrMat->num_rows+1); 838aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(upTriFactor->csrMat->num_entries); 839aa372e3fSPaul Mullowney upTriFactorT->csrMat->values = new THRUSTARRAY(upTriFactor->csrMat->num_entries); 840aa372e3fSPaul Mullowney 841aa372e3fSPaul Mullowney /* compute the transpose of the upper triangular factor, i.e. the CSC */ 842aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparseTriFactors->handle, upTriFactor->csrMat->num_rows, 843aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols, upTriFactor->csrMat->num_entries, 844aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 845aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 846aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 847aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 848aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 849aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 85057d48284SJunchao Zhang CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUSPARSE(stat); 851aa372e3fSPaul Mullowney 852aa372e3fSPaul Mullowney /* perform the solve analysis on the transposed matrix */ 853aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactorT->solveOp, 854aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_rows, upTriFactorT->csrMat->num_entries, 855aa372e3fSPaul Mullowney upTriFactorT->descr, upTriFactorT->csrMat->values->data().get(), 856aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), upTriFactorT->csrMat->column_indices->data().get(), 85757d48284SJunchao Zhang upTriFactorT->solveInfo);CHKERRCUSPARSE(stat); 858aa372e3fSPaul Mullowney 859aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 860aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtrTranspose = upTriFactorT; 861bda325fcSPaul Mullowney PetscFunctionReturn(0); 862bda325fcSPaul Mullowney } 863bda325fcSPaul Mullowney 864b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEGenerateTransposeForMult(Mat A) 865bda325fcSPaul Mullowney { 866aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 867aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 868aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 869bda325fcSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 870bda325fcSPaul Mullowney cusparseStatus_t stat; 871aa372e3fSPaul Mullowney cusparseIndexBase_t indexBase; 872b06137fdSPaul Mullowney cudaError_t err; 8734863603aSSatish Balay PetscErrorCode ierr; 874b175d8bbSPaul Mullowney 875bda325fcSPaul Mullowney PetscFunctionBegin; 876aa372e3fSPaul Mullowney 877aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 878aa372e3fSPaul Mullowney matstructT = new Mat_SeqAIJCUSPARSEMultStruct; 87957d48284SJunchao Zhang stat = cusparseCreateMatDescr(&matstructT->descr);CHKERRCUSPARSE(stat); 880aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(matstruct->descr); 88157d48284SJunchao Zhang stat = cusparseSetMatIndexBase(matstructT->descr, indexBase);CHKERRCUSPARSE(stat); 88257d48284SJunchao Zhang stat = cusparseSetMatType(matstructT->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUSPARSE(stat); 883aa372e3fSPaul Mullowney 884b06137fdSPaul Mullowney /* set alpha and beta */ 885c41cb2e2SAlejandro Lamas Daviña err = cudaMalloc((void **)&(matstructT->alpha), sizeof(PetscScalar));CHKERRCUDA(err); 8867656d835SStefano Zampini err = cudaMalloc((void **)&(matstructT->beta_zero),sizeof(PetscScalar));CHKERRCUDA(err); 8877656d835SStefano Zampini err = cudaMalloc((void **)&(matstructT->beta_one), sizeof(PetscScalar));CHKERRCUDA(err); 8887656d835SStefano Zampini err = cudaMemcpy(matstructT->alpha, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 8897656d835SStefano Zampini err = cudaMemcpy(matstructT->beta_zero,&PETSC_CUSPARSE_ZERO,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 8907656d835SStefano Zampini err = cudaMemcpy(matstructT->beta_one, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 89157d48284SJunchao Zhang stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUSPARSE(stat); 892b06137fdSPaul Mullowney 893aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 894aa372e3fSPaul Mullowney CsrMatrix *matrix = (CsrMatrix*)matstruct->mat; 895aa372e3fSPaul Mullowney CsrMatrix *matrixT= new CsrMatrix; 896554b8892SKarl Rupp matrixT->num_rows = A->cmap->n; 897554b8892SKarl Rupp matrixT->num_cols = A->rmap->n; 898aa372e3fSPaul Mullowney matrixT->num_entries = a->nz; 899a8bd5306SMark Adams matrixT->row_offsets = new THRUSTINTARRAY32(matrixT->num_rows+1); 900aa372e3fSPaul Mullowney matrixT->column_indices = new THRUSTINTARRAY32(a->nz); 901aa372e3fSPaul Mullowney matrixT->values = new THRUSTARRAY(a->nz); 902a3fdcf43SKarl Rupp 90381902715SJunchao Zhang cusparsestruct->rowoffsets_gpu = new THRUSTINTARRAY32(A->rmap->n+1); 90481902715SJunchao Zhang cusparsestruct->rowoffsets_gpu->assign(a->i,a->i+A->rmap->n+1); 90581902715SJunchao Zhang /* compute the transpose, i.e. the CSC */ 906aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(matstruct->descr); 907a3fdcf43SKarl Rupp stat = cusparse_csr2csc(cusparsestruct->handle, A->rmap->n, 908a3fdcf43SKarl Rupp A->cmap->n, matrix->num_entries, 909aa372e3fSPaul Mullowney matrix->values->data().get(), 91081902715SJunchao Zhang cusparsestruct->rowoffsets_gpu->data().get(), 911aa372e3fSPaul Mullowney matrix->column_indices->data().get(), 912aa372e3fSPaul Mullowney matrixT->values->data().get(), 913aa372e3fSPaul Mullowney matrixT->column_indices->data().get(), 914aa372e3fSPaul Mullowney matrixT->row_offsets->data().get(), 91557d48284SJunchao Zhang CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUSPARSE(stat); 916aa372e3fSPaul Mullowney /* assign the pointer */ 917aa372e3fSPaul Mullowney matstructT->mat = matrixT; 9184863603aSSatish Balay ierr = PetscLogCpuToGpu(((A->rmap->n+1)+(a->nz))*sizeof(int)+(3+a->nz)*sizeof(PetscScalar));CHKERRQ(ierr); 919aa372e3fSPaul Mullowney } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) { 920aa372e3fSPaul Mullowney /* First convert HYB to CSR */ 921aa372e3fSPaul Mullowney CsrMatrix *temp= new CsrMatrix; 922aa372e3fSPaul Mullowney temp->num_rows = A->rmap->n; 923aa372e3fSPaul Mullowney temp->num_cols = A->cmap->n; 924aa372e3fSPaul Mullowney temp->num_entries = a->nz; 925aa372e3fSPaul Mullowney temp->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 926aa372e3fSPaul Mullowney temp->column_indices = new THRUSTINTARRAY32(a->nz); 927aa372e3fSPaul Mullowney temp->values = new THRUSTARRAY(a->nz); 928aa372e3fSPaul Mullowney 9292692e278SPaul Mullowney 930aa372e3fSPaul Mullowney stat = cusparse_hyb2csr(cusparsestruct->handle, 931aa372e3fSPaul Mullowney matstruct->descr, (cusparseHybMat_t)matstruct->mat, 932aa372e3fSPaul Mullowney temp->values->data().get(), 933aa372e3fSPaul Mullowney temp->row_offsets->data().get(), 93457d48284SJunchao Zhang temp->column_indices->data().get());CHKERRCUSPARSE(stat); 935aa372e3fSPaul Mullowney 936aa372e3fSPaul Mullowney /* Next, convert CSR to CSC (i.e. the matrix transpose) */ 937aa372e3fSPaul Mullowney CsrMatrix *tempT= new CsrMatrix; 938aa372e3fSPaul Mullowney tempT->num_rows = A->rmap->n; 939aa372e3fSPaul Mullowney tempT->num_cols = A->cmap->n; 940aa372e3fSPaul Mullowney tempT->num_entries = a->nz; 941aa372e3fSPaul Mullowney tempT->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 942aa372e3fSPaul Mullowney tempT->column_indices = new THRUSTINTARRAY32(a->nz); 943aa372e3fSPaul Mullowney tempT->values = new THRUSTARRAY(a->nz); 944aa372e3fSPaul Mullowney 945aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparsestruct->handle, temp->num_rows, 946aa372e3fSPaul Mullowney temp->num_cols, temp->num_entries, 947aa372e3fSPaul Mullowney temp->values->data().get(), 948aa372e3fSPaul Mullowney temp->row_offsets->data().get(), 949aa372e3fSPaul Mullowney temp->column_indices->data().get(), 950aa372e3fSPaul Mullowney tempT->values->data().get(), 951aa372e3fSPaul Mullowney tempT->column_indices->data().get(), 952aa372e3fSPaul Mullowney tempT->row_offsets->data().get(), 95357d48284SJunchao Zhang CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUSPARSE(stat); 954aa372e3fSPaul Mullowney 955aa372e3fSPaul Mullowney /* Last, convert CSC to HYB */ 956aa372e3fSPaul Mullowney cusparseHybMat_t hybMat; 95757d48284SJunchao Zhang stat = cusparseCreateHybMat(&hybMat);CHKERRCUSPARSE(stat); 958aa372e3fSPaul Mullowney cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ? 959aa372e3fSPaul Mullowney CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO; 960aa372e3fSPaul Mullowney stat = cusparse_csr2hyb(cusparsestruct->handle, A->rmap->n, A->cmap->n, 961aa372e3fSPaul Mullowney matstructT->descr, tempT->values->data().get(), 962aa372e3fSPaul Mullowney tempT->row_offsets->data().get(), 963aa372e3fSPaul Mullowney tempT->column_indices->data().get(), 96457d48284SJunchao Zhang hybMat, 0, partition);CHKERRCUSPARSE(stat); 965aa372e3fSPaul Mullowney 966aa372e3fSPaul Mullowney /* assign the pointer */ 967aa372e3fSPaul Mullowney matstructT->mat = hybMat; 9684863603aSSatish Balay ierr = PetscLogCpuToGpu((2*(((A->rmap->n+1)+(a->nz))*sizeof(int)+(a->nz)*sizeof(PetscScalar)))+3*sizeof(PetscScalar));CHKERRQ(ierr); 969aa372e3fSPaul Mullowney 970aa372e3fSPaul Mullowney /* delete temporaries */ 971aa372e3fSPaul Mullowney if (tempT) { 972aa372e3fSPaul Mullowney if (tempT->values) delete (THRUSTARRAY*) tempT->values; 973aa372e3fSPaul Mullowney if (tempT->column_indices) delete (THRUSTINTARRAY32*) tempT->column_indices; 974aa372e3fSPaul Mullowney if (tempT->row_offsets) delete (THRUSTINTARRAY32*) tempT->row_offsets; 975aa372e3fSPaul Mullowney delete (CsrMatrix*) tempT; 976087f3262SPaul Mullowney } 977aa372e3fSPaul Mullowney if (temp) { 978aa372e3fSPaul Mullowney if (temp->values) delete (THRUSTARRAY*) temp->values; 979aa372e3fSPaul Mullowney if (temp->column_indices) delete (THRUSTINTARRAY32*) temp->column_indices; 980aa372e3fSPaul Mullowney if (temp->row_offsets) delete (THRUSTINTARRAY32*) temp->row_offsets; 981aa372e3fSPaul Mullowney delete (CsrMatrix*) temp; 982aa372e3fSPaul Mullowney } 983aa372e3fSPaul Mullowney } 984213423ffSJunchao Zhang /* the compressed row indices is not used for matTranspose */ 985213423ffSJunchao Zhang matstructT->cprowIndices = NULL; 986aa372e3fSPaul Mullowney /* assign the pointer */ 987aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)A->spptr)->matTranspose = matstructT; 988bda325fcSPaul Mullowney PetscFunctionReturn(0); 989bda325fcSPaul Mullowney } 990bda325fcSPaul Mullowney 9914e4bbfaaSStefano Zampini /* Why do we need to analyze the tranposed matrix again? Can't we just use op(A) = CUSPARSE_OPERATION_TRANSPOSE in MatSolve_SeqAIJCUSPARSE? */ 9926fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx) 993bda325fcSPaul Mullowney { 994c41cb2e2SAlejandro Lamas Daviña PetscInt n = xx->map->n; 995465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 996465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 997465f34aeSAlejandro Lamas Daviña thrust::device_ptr<const PetscScalar> bGPU; 998465f34aeSAlejandro Lamas Daviña thrust::device_ptr<PetscScalar> xGPU; 999bda325fcSPaul Mullowney cusparseStatus_t stat; 1000bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1001aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1002aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1003aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1004b175d8bbSPaul Mullowney PetscErrorCode ierr; 100557d48284SJunchao Zhang cudaError_t cerr; 1006bda325fcSPaul Mullowney 1007bda325fcSPaul Mullowney PetscFunctionBegin; 1008aa372e3fSPaul Mullowney /* Analyze the matrix and create the transpose ... on the fly */ 1009aa372e3fSPaul Mullowney if (!loTriFactorT && !upTriFactorT) { 1010bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr); 1011aa372e3fSPaul Mullowney loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1012aa372e3fSPaul Mullowney upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1013bda325fcSPaul Mullowney } 1014bda325fcSPaul Mullowney 1015bda325fcSPaul Mullowney /* Get the GPU pointers */ 1016c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1017c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1018c41cb2e2SAlejandro Lamas Daviña xGPU = thrust::device_pointer_cast(xarray); 1019c41cb2e2SAlejandro Lamas Daviña bGPU = thrust::device_pointer_cast(barray); 1020bda325fcSPaul Mullowney 10217a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1022aa372e3fSPaul Mullowney /* First, reorder with the row permutation */ 1023c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()), 1024c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(bGPU+n, cusparseTriFactors->rpermIndices->end()), 1025c41cb2e2SAlejandro Lamas Daviña xGPU); 1026aa372e3fSPaul Mullowney 1027aa372e3fSPaul Mullowney /* First, solve U */ 1028aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp, 10297656d835SStefano Zampini upTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactorT->descr, 1030aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 1031aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 1032aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 1033aa372e3fSPaul Mullowney upTriFactorT->solveInfo, 103457d48284SJunchao Zhang xarray, tempGPU->data().get());CHKERRCUSPARSE(stat); 1035aa372e3fSPaul Mullowney 1036aa372e3fSPaul Mullowney /* Then, solve L */ 1037aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp, 10387656d835SStefano Zampini loTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactorT->descr, 1039aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 1040aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 1041aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 1042aa372e3fSPaul Mullowney loTriFactorT->solveInfo, 104357d48284SJunchao Zhang tempGPU->data().get(), xarray);CHKERRCUSPARSE(stat); 1044aa372e3fSPaul Mullowney 1045aa372e3fSPaul Mullowney /* Last, copy the solution, xGPU, into a temporary with the column permutation ... can't be done in place. */ 1046c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->begin()), 1047c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(xGPU+n, cusparseTriFactors->cpermIndices->end()), 1048aa372e3fSPaul Mullowney tempGPU->begin()); 1049aa372e3fSPaul Mullowney 1050aa372e3fSPaul Mullowney /* Copy the temporary to the full solution. */ 1051c41cb2e2SAlejandro Lamas Daviña thrust::copy(tempGPU->begin(), tempGPU->end(), xGPU); 1052bda325fcSPaul Mullowney 1053bda325fcSPaul Mullowney /* restore */ 1054c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1055c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 105657d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); 1057661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1058958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 1059bda325fcSPaul Mullowney PetscFunctionReturn(0); 1060bda325fcSPaul Mullowney } 1061bda325fcSPaul Mullowney 10626fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx) 1063bda325fcSPaul Mullowney { 1064465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1065465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 1066bda325fcSPaul Mullowney cusparseStatus_t stat; 1067bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1068aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1069aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1070aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1071b175d8bbSPaul Mullowney PetscErrorCode ierr; 107257d48284SJunchao Zhang cudaError_t cerr; 1073bda325fcSPaul Mullowney 1074bda325fcSPaul Mullowney PetscFunctionBegin; 1075aa372e3fSPaul Mullowney /* Analyze the matrix and create the transpose ... on the fly */ 1076aa372e3fSPaul Mullowney if (!loTriFactorT && !upTriFactorT) { 1077bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr); 1078aa372e3fSPaul Mullowney loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1079aa372e3fSPaul Mullowney upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1080bda325fcSPaul Mullowney } 1081bda325fcSPaul Mullowney 1082bda325fcSPaul Mullowney /* Get the GPU pointers */ 1083c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1084c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1085bda325fcSPaul Mullowney 10867a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1087aa372e3fSPaul Mullowney /* First, solve U */ 1088aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp, 10897656d835SStefano Zampini upTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactorT->descr, 1090aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 1091aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 1092aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 1093aa372e3fSPaul Mullowney upTriFactorT->solveInfo, 109457d48284SJunchao Zhang barray, tempGPU->data().get());CHKERRCUSPARSE(stat); 1095aa372e3fSPaul Mullowney 1096aa372e3fSPaul Mullowney /* Then, solve L */ 1097aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp, 10987656d835SStefano Zampini loTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactorT->descr, 1099aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 1100aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 1101aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 1102aa372e3fSPaul Mullowney loTriFactorT->solveInfo, 110357d48284SJunchao Zhang tempGPU->data().get(), xarray);CHKERRCUSPARSE(stat); 1104bda325fcSPaul Mullowney 1105bda325fcSPaul Mullowney /* restore */ 1106c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1107c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 110857d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); 1109661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1110958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 1111bda325fcSPaul Mullowney PetscFunctionReturn(0); 1112bda325fcSPaul Mullowney } 1113bda325fcSPaul Mullowney 11146fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx) 11159ae82921SPaul Mullowney { 1116465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1117465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 1118465f34aeSAlejandro Lamas Daviña thrust::device_ptr<const PetscScalar> bGPU; 1119465f34aeSAlejandro Lamas Daviña thrust::device_ptr<PetscScalar> xGPU; 11209ae82921SPaul Mullowney cusparseStatus_t stat; 11219ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1122aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 1123aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 1124aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1125b175d8bbSPaul Mullowney PetscErrorCode ierr; 112657d48284SJunchao Zhang cudaError_t cerr; 11279ae82921SPaul Mullowney 11289ae82921SPaul Mullowney PetscFunctionBegin; 1129ebc8f436SDominic Meiser 1130e057df02SPaul Mullowney /* Get the GPU pointers */ 1131c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1132c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1133c41cb2e2SAlejandro Lamas Daviña xGPU = thrust::device_pointer_cast(xarray); 1134c41cb2e2SAlejandro Lamas Daviña bGPU = thrust::device_pointer_cast(barray); 11359ae82921SPaul Mullowney 11367a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1137aa372e3fSPaul Mullowney /* First, reorder with the row permutation */ 1138c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()), 1139c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->end()), 11404e4bbfaaSStefano Zampini tempGPU->begin()); 1141aa372e3fSPaul Mullowney 1142aa372e3fSPaul Mullowney /* Next, solve L */ 1143aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp, 11447656d835SStefano Zampini loTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactor->descr, 1145aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 1146aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 1147aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 1148aa372e3fSPaul Mullowney loTriFactor->solveInfo, 114957d48284SJunchao Zhang tempGPU->data().get(), xarray);CHKERRCUSPARSE(stat); 1150aa372e3fSPaul Mullowney 1151aa372e3fSPaul Mullowney /* Then, solve U */ 1152aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp, 11537656d835SStefano Zampini upTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactor->descr, 1154aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 1155aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 1156aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 1157aa372e3fSPaul Mullowney upTriFactor->solveInfo, 115857d48284SJunchao Zhang xarray, tempGPU->data().get());CHKERRCUSPARSE(stat); 1159aa372e3fSPaul Mullowney 11604e4bbfaaSStefano Zampini /* Last, reorder with the column permutation */ 11614e4bbfaaSStefano Zampini thrust::copy(thrust::make_permutation_iterator(tempGPU->begin(), cusparseTriFactors->cpermIndices->begin()), 11624e4bbfaaSStefano Zampini thrust::make_permutation_iterator(tempGPU->begin(), cusparseTriFactors->cpermIndices->end()), 11634e4bbfaaSStefano Zampini xGPU); 11649ae82921SPaul Mullowney 1165c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1166c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 116757d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); 1168661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1169958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 11709ae82921SPaul Mullowney PetscFunctionReturn(0); 11719ae82921SPaul Mullowney } 11729ae82921SPaul Mullowney 11736fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx) 11749ae82921SPaul Mullowney { 1175465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1176465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 11779ae82921SPaul Mullowney cusparseStatus_t stat; 11789ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1179aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 1180aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 1181aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1182b175d8bbSPaul Mullowney PetscErrorCode ierr; 118357d48284SJunchao Zhang cudaError_t cerr; 11849ae82921SPaul Mullowney 11859ae82921SPaul Mullowney PetscFunctionBegin; 1186e057df02SPaul Mullowney /* Get the GPU pointers */ 1187c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1188c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 11899ae82921SPaul Mullowney 11907a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1191aa372e3fSPaul Mullowney /* First, solve L */ 1192aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp, 11937656d835SStefano Zampini loTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactor->descr, 1194aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 1195aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 1196aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 1197aa372e3fSPaul Mullowney loTriFactor->solveInfo, 119857d48284SJunchao Zhang barray, tempGPU->data().get());CHKERRCUSPARSE(stat); 1199aa372e3fSPaul Mullowney 1200aa372e3fSPaul Mullowney /* Next, solve U */ 1201aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp, 12027656d835SStefano Zampini upTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactor->descr, 1203aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 1204aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 1205aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 1206aa372e3fSPaul Mullowney upTriFactor->solveInfo, 120757d48284SJunchao Zhang tempGPU->data().get(), xarray);CHKERRCUSPARSE(stat); 12089ae82921SPaul Mullowney 1209c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1210c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 121157d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); 1212661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1213958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 12149ae82921SPaul Mullowney PetscFunctionReturn(0); 12159ae82921SPaul Mullowney } 12169ae82921SPaul Mullowney 12176fa9248bSJed Brown static PetscErrorCode MatSeqAIJCUSPARSECopyToGPU(Mat A) 12189ae82921SPaul Mullowney { 1219aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 12207c700b8dSJunchao Zhang Mat_SeqAIJCUSPARSEMultStruct *matstruct = cusparsestruct->mat; 12219ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1222213423ffSJunchao Zhang PetscInt m = A->rmap->n,*ii,*ridx,tmp; 12239ae82921SPaul Mullowney PetscErrorCode ierr; 1224aa372e3fSPaul Mullowney cusparseStatus_t stat; 1225b06137fdSPaul Mullowney cudaError_t err; 12269ae82921SPaul Mullowney 12279ae82921SPaul Mullowney PetscFunctionBegin; 122895639643SRichard Tran Mills if (A->boundtocpu) PetscFunctionReturn(0); 1229c70f7ee4SJunchao Zhang if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) { 12309ae82921SPaul Mullowney ierr = PetscLogEventBegin(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr); 123181902715SJunchao Zhang if (A->was_assembled && A->nonzerostate == cusparsestruct->nonzerostate && cusparsestruct->format == MAT_CUSPARSE_CSR) { 123281902715SJunchao Zhang /* Copy values only */ 123381902715SJunchao Zhang CsrMatrix *mat,*matT; 123481902715SJunchao Zhang mat = (CsrMatrix*)cusparsestruct->mat->mat; 123581902715SJunchao Zhang mat->values->assign(a->a, a->a+a->nz); 12364863603aSSatish Balay ierr = PetscLogCpuToGpu((a->nz)*sizeof(PetscScalar));CHKERRQ(ierr); 123781902715SJunchao Zhang 123881902715SJunchao Zhang /* Update matT when it was built before */ 123981902715SJunchao Zhang if (cusparsestruct->matTranspose) { 124081902715SJunchao Zhang cusparseIndexBase_t indexBase = cusparseGetMatIndexBase(cusparsestruct->mat->descr); 124181902715SJunchao Zhang matT = (CsrMatrix*)cusparsestruct->matTranspose->mat; 124281902715SJunchao Zhang stat = cusparse_csr2csc(cusparsestruct->handle, A->rmap->n, 124381902715SJunchao Zhang A->cmap->n, mat->num_entries, 124481902715SJunchao Zhang mat->values->data().get(), 124581902715SJunchao Zhang cusparsestruct->rowoffsets_gpu->data().get(), 124681902715SJunchao Zhang mat->column_indices->data().get(), 124781902715SJunchao Zhang matT->values->data().get(), 124881902715SJunchao Zhang matT->column_indices->data().get(), 124981902715SJunchao Zhang matT->row_offsets->data().get(), 12509f74ec24SSatish Balay CUSPARSE_ACTION_NUMERIC,indexBase);CHKERRCUSPARSE(stat); 125181902715SJunchao Zhang } 125234d6c7a5SJose E. Roman } else { 12537c700b8dSJunchao Zhang ierr = MatSeqAIJCUSPARSEMultStruct_Destroy(&cusparsestruct->mat,cusparsestruct->format);CHKERRQ(ierr); 12547c700b8dSJunchao Zhang ierr = MatSeqAIJCUSPARSEMultStruct_Destroy(&cusparsestruct->matTranspose,cusparsestruct->format);CHKERRQ(ierr); 12557c700b8dSJunchao Zhang delete cusparsestruct->workVector; 125681902715SJunchao Zhang delete cusparsestruct->rowoffsets_gpu; 12579ae82921SPaul Mullowney try { 12589ae82921SPaul Mullowney if (a->compressedrow.use) { 12599ae82921SPaul Mullowney m = a->compressedrow.nrows; 12609ae82921SPaul Mullowney ii = a->compressedrow.i; 12619ae82921SPaul Mullowney ridx = a->compressedrow.rindex; 12629ae82921SPaul Mullowney } else { 1263213423ffSJunchao Zhang m = A->rmap->n; 1264213423ffSJunchao Zhang ii = a->i; 1265213423ffSJunchao Zhang ridx = NULL; /* In this case, one should not dereference ridx */ 12669ae82921SPaul Mullowney } 1267213423ffSJunchao Zhang cusparsestruct->nrows = m; 12689ae82921SPaul Mullowney 1269aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 1270aa372e3fSPaul Mullowney matstruct = new Mat_SeqAIJCUSPARSEMultStruct; 127157d48284SJunchao Zhang stat = cusparseCreateMatDescr(&matstruct->descr);CHKERRCUSPARSE(stat); 127257d48284SJunchao Zhang stat = cusparseSetMatIndexBase(matstruct->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat); 127357d48284SJunchao Zhang stat = cusparseSetMatType(matstruct->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUSPARSE(stat); 12749ae82921SPaul Mullowney 1275c41cb2e2SAlejandro Lamas Daviña err = cudaMalloc((void **)&(matstruct->alpha), sizeof(PetscScalar));CHKERRCUDA(err); 12767656d835SStefano Zampini err = cudaMalloc((void **)&(matstruct->beta_zero),sizeof(PetscScalar));CHKERRCUDA(err); 12777656d835SStefano Zampini err = cudaMalloc((void **)&(matstruct->beta_one), sizeof(PetscScalar));CHKERRCUDA(err); 12787656d835SStefano Zampini err = cudaMemcpy(matstruct->alpha, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 12797656d835SStefano Zampini err = cudaMemcpy(matstruct->beta_zero,&PETSC_CUSPARSE_ZERO,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 12807656d835SStefano Zampini err = cudaMemcpy(matstruct->beta_one, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 128157d48284SJunchao Zhang stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUSPARSE(stat); 1282b06137fdSPaul Mullowney 1283aa372e3fSPaul Mullowney /* Build a hybrid/ellpack matrix if this option is chosen for the storage */ 1284aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1285aa372e3fSPaul Mullowney /* set the matrix */ 1286aa372e3fSPaul Mullowney CsrMatrix *matrix= new CsrMatrix; 1287a65300a6SPaul Mullowney matrix->num_rows = m; 1288aa372e3fSPaul Mullowney matrix->num_cols = A->cmap->n; 1289aa372e3fSPaul Mullowney matrix->num_entries = a->nz; 1290a65300a6SPaul Mullowney matrix->row_offsets = new THRUSTINTARRAY32(m+1); 1291a65300a6SPaul Mullowney matrix->row_offsets->assign(ii, ii + m+1); 12929ae82921SPaul Mullowney 1293aa372e3fSPaul Mullowney matrix->column_indices = new THRUSTINTARRAY32(a->nz); 1294aa372e3fSPaul Mullowney matrix->column_indices->assign(a->j, a->j+a->nz); 1295aa372e3fSPaul Mullowney 1296aa372e3fSPaul Mullowney matrix->values = new THRUSTARRAY(a->nz); 1297aa372e3fSPaul Mullowney matrix->values->assign(a->a, a->a+a->nz); 1298aa372e3fSPaul Mullowney 1299aa372e3fSPaul Mullowney /* assign the pointer */ 1300aa372e3fSPaul Mullowney matstruct->mat = matrix; 1301aa372e3fSPaul Mullowney 1302aa372e3fSPaul Mullowney } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) { 1303aa372e3fSPaul Mullowney CsrMatrix *matrix= new CsrMatrix; 1304a65300a6SPaul Mullowney matrix->num_rows = m; 1305aa372e3fSPaul Mullowney matrix->num_cols = A->cmap->n; 1306aa372e3fSPaul Mullowney matrix->num_entries = a->nz; 1307a65300a6SPaul Mullowney matrix->row_offsets = new THRUSTINTARRAY32(m+1); 1308a65300a6SPaul Mullowney matrix->row_offsets->assign(ii, ii + m+1); 1309aa372e3fSPaul Mullowney 1310aa372e3fSPaul Mullowney matrix->column_indices = new THRUSTINTARRAY32(a->nz); 1311aa372e3fSPaul Mullowney matrix->column_indices->assign(a->j, a->j+a->nz); 1312aa372e3fSPaul Mullowney 1313aa372e3fSPaul Mullowney matrix->values = new THRUSTARRAY(a->nz); 1314aa372e3fSPaul Mullowney matrix->values->assign(a->a, a->a+a->nz); 1315aa372e3fSPaul Mullowney 1316aa372e3fSPaul Mullowney cusparseHybMat_t hybMat; 131757d48284SJunchao Zhang stat = cusparseCreateHybMat(&hybMat);CHKERRCUSPARSE(stat); 1318aa372e3fSPaul Mullowney cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ? 1319aa372e3fSPaul Mullowney CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO; 1320a65300a6SPaul Mullowney stat = cusparse_csr2hyb(cusparsestruct->handle, matrix->num_rows, matrix->num_cols, 1321aa372e3fSPaul Mullowney matstruct->descr, matrix->values->data().get(), 1322aa372e3fSPaul Mullowney matrix->row_offsets->data().get(), 1323aa372e3fSPaul Mullowney matrix->column_indices->data().get(), 132457d48284SJunchao Zhang hybMat, 0, partition);CHKERRCUSPARSE(stat); 1325aa372e3fSPaul Mullowney /* assign the pointer */ 1326aa372e3fSPaul Mullowney matstruct->mat = hybMat; 1327aa372e3fSPaul Mullowney 1328aa372e3fSPaul Mullowney if (matrix) { 1329aa372e3fSPaul Mullowney if (matrix->values) delete (THRUSTARRAY*)matrix->values; 1330aa372e3fSPaul Mullowney if (matrix->column_indices) delete (THRUSTINTARRAY32*)matrix->column_indices; 1331aa372e3fSPaul Mullowney if (matrix->row_offsets) delete (THRUSTINTARRAY32*)matrix->row_offsets; 1332aa372e3fSPaul Mullowney delete (CsrMatrix*)matrix; 1333087f3262SPaul Mullowney } 1334087f3262SPaul Mullowney } 1335ca45077fSPaul Mullowney 1336aa372e3fSPaul Mullowney /* assign the compressed row indices */ 1337213423ffSJunchao Zhang if (a->compressedrow.use) { 1338213423ffSJunchao Zhang cusparsestruct->workVector = new THRUSTARRAY(m); 1339aa372e3fSPaul Mullowney matstruct->cprowIndices = new THRUSTINTARRAY(m); 1340aa372e3fSPaul Mullowney matstruct->cprowIndices->assign(ridx,ridx+m); 1341213423ffSJunchao Zhang tmp = m; 1342213423ffSJunchao Zhang } else { 1343213423ffSJunchao Zhang cusparsestruct->workVector = NULL; 1344213423ffSJunchao Zhang matstruct->cprowIndices = NULL; 1345213423ffSJunchao Zhang tmp = 0; 1346213423ffSJunchao Zhang } 1347213423ffSJunchao Zhang ierr = PetscLogCpuToGpu(((m+1)+(a->nz))*sizeof(int)+tmp*sizeof(PetscInt)+(3+(a->nz))*sizeof(PetscScalar));CHKERRQ(ierr); 1348aa372e3fSPaul Mullowney 1349aa372e3fSPaul Mullowney /* assign the pointer */ 1350aa372e3fSPaul Mullowney cusparsestruct->mat = matstruct; 13519ae82921SPaul Mullowney } catch(char *ex) { 13529ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 13539ae82921SPaul Mullowney } 135434d6c7a5SJose E. Roman cusparsestruct->nonzerostate = A->nonzerostate; 135534d6c7a5SJose E. Roman } 135657d48284SJunchao Zhang err = WaitForGPU();CHKERRCUDA(err); 1357c70f7ee4SJunchao Zhang A->offloadmask = PETSC_OFFLOAD_BOTH; 13589ae82921SPaul Mullowney ierr = PetscLogEventEnd(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr); 13599ae82921SPaul Mullowney } 13609ae82921SPaul Mullowney PetscFunctionReturn(0); 13619ae82921SPaul Mullowney } 13629ae82921SPaul Mullowney 1363c41cb2e2SAlejandro Lamas Daviña struct VecCUDAPlusEquals 1364aa372e3fSPaul Mullowney { 1365aa372e3fSPaul Mullowney template <typename Tuple> 1366aa372e3fSPaul Mullowney __host__ __device__ 1367aa372e3fSPaul Mullowney void operator()(Tuple t) 1368aa372e3fSPaul Mullowney { 1369aa372e3fSPaul Mullowney thrust::get<1>(t) = thrust::get<1>(t) + thrust::get<0>(t); 1370aa372e3fSPaul Mullowney } 1371aa372e3fSPaul Mullowney }; 1372aa372e3fSPaul Mullowney 1373*ccdfe979SStefano Zampini typedef struct { 1374*ccdfe979SStefano Zampini PetscBool cisdense; 1375*ccdfe979SStefano Zampini PetscScalar *Bt; 1376*ccdfe979SStefano Zampini Mat X; 1377*ccdfe979SStefano Zampini } MatMatCusparse; 1378*ccdfe979SStefano Zampini 1379*ccdfe979SStefano Zampini static PetscErrorCode MatDestroy_MatMatCusparse(void *data) 1380*ccdfe979SStefano Zampini { 1381*ccdfe979SStefano Zampini PetscErrorCode ierr; 1382*ccdfe979SStefano Zampini MatMatCusparse *mmdata = (MatMatCusparse *)data; 1383*ccdfe979SStefano Zampini cudaError_t cerr; 1384*ccdfe979SStefano Zampini 1385*ccdfe979SStefano Zampini PetscFunctionBegin; 1386*ccdfe979SStefano Zampini cerr = cudaFree(mmdata->Bt);CHKERRCUDA(cerr); 1387*ccdfe979SStefano Zampini ierr = MatDestroy(&mmdata->X);CHKERRQ(ierr); 1388*ccdfe979SStefano Zampini ierr = PetscFree(data);CHKERRQ(ierr); 1389*ccdfe979SStefano Zampini PetscFunctionReturn(0); 1390*ccdfe979SStefano Zampini } 1391*ccdfe979SStefano Zampini 1392*ccdfe979SStefano Zampini PETSC_INTERN PetscErrorCode MatMatMultNumeric_SeqDenseCUDA_SeqDenseCUDA_Private(Mat,Mat,Mat,PetscBool,PetscBool); 1393*ccdfe979SStefano Zampini 1394*ccdfe979SStefano Zampini static PetscErrorCode MatProductNumeric_SeqAIJCUSPARSE_SeqDENSECUDA(Mat C) 1395*ccdfe979SStefano Zampini { 1396*ccdfe979SStefano Zampini Mat_Product *product = C->product; 1397*ccdfe979SStefano Zampini Mat A,B; 1398*ccdfe979SStefano Zampini PetscInt m,n,k,blda,clda; 1399*ccdfe979SStefano Zampini PetscBool flg,biscuda; 1400*ccdfe979SStefano Zampini Mat_SeqAIJCUSPARSE *cusp; 1401*ccdfe979SStefano Zampini cusparseStatus_t stat; 1402*ccdfe979SStefano Zampini cusparseOperation_t opA; 1403*ccdfe979SStefano Zampini cusparseMatDescr_t matA; 1404*ccdfe979SStefano Zampini const PetscScalar *barray; 1405*ccdfe979SStefano Zampini PetscScalar *carray; 1406*ccdfe979SStefano Zampini PetscErrorCode ierr; 1407*ccdfe979SStefano Zampini MatMatCusparse *mmdata; 1408*ccdfe979SStefano Zampini Mat_SeqAIJCUSPARSEMultStruct *mat; 1409*ccdfe979SStefano Zampini CsrMatrix *csrmat; 1410*ccdfe979SStefano Zampini 1411*ccdfe979SStefano Zampini PetscFunctionBegin; 1412*ccdfe979SStefano Zampini MatCheckProduct(C,1); 1413*ccdfe979SStefano Zampini if (!C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data empty"); 1414*ccdfe979SStefano Zampini mmdata = (MatMatCusparse*)product->data; 1415*ccdfe979SStefano Zampini A = product->A; 1416*ccdfe979SStefano Zampini B = product->B; 1417*ccdfe979SStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQAIJCUSPARSE,&flg);CHKERRQ(ierr); 1418*ccdfe979SStefano Zampini if (!flg) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_PLIB,"Not for type %s",((PetscObject)A)->type_name); 1419*ccdfe979SStefano Zampini /* currently CopyToGpu does not copy if the matrix is bound to CPU 1420*ccdfe979SStefano Zampini Instead of silently accepting the wrong answer, I prefer to raise the error */ 1421*ccdfe979SStefano Zampini if (A->boundtocpu) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Cannot bind to CPU a CUSPARSE matrix between MatProductSymbolic and MatProductNumeric phases"); 1422*ccdfe979SStefano Zampini ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 1423*ccdfe979SStefano Zampini cusp = (Mat_SeqAIJCUSPARSE*)A->spptr; 1424*ccdfe979SStefano Zampini switch (product->type) { 1425*ccdfe979SStefano Zampini case MATPRODUCT_AB: 1426*ccdfe979SStefano Zampini case MATPRODUCT_PtAP: 1427*ccdfe979SStefano Zampini mat = cusp->mat; 1428*ccdfe979SStefano Zampini opA = CUSPARSE_OPERATION_NON_TRANSPOSE; 1429*ccdfe979SStefano Zampini m = A->rmap->n; 1430*ccdfe979SStefano Zampini k = A->cmap->n; 1431*ccdfe979SStefano Zampini n = B->cmap->n; 1432*ccdfe979SStefano Zampini break; 1433*ccdfe979SStefano Zampini case MATPRODUCT_AtB: 1434*ccdfe979SStefano Zampini if (!cusp->matTranspose) { 1435*ccdfe979SStefano Zampini ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr); 1436*ccdfe979SStefano Zampini } 1437*ccdfe979SStefano Zampini mat = cusp->matTranspose; 1438*ccdfe979SStefano Zampini opA = CUSPARSE_OPERATION_NON_TRANSPOSE; 1439*ccdfe979SStefano Zampini m = A->cmap->n; 1440*ccdfe979SStefano Zampini k = A->rmap->n; 1441*ccdfe979SStefano Zampini n = B->cmap->n; 1442*ccdfe979SStefano Zampini break; 1443*ccdfe979SStefano Zampini case MATPRODUCT_ABt: 1444*ccdfe979SStefano Zampini case MATPRODUCT_RARt: 1445*ccdfe979SStefano Zampini mat = cusp->mat; 1446*ccdfe979SStefano Zampini opA = CUSPARSE_OPERATION_NON_TRANSPOSE; 1447*ccdfe979SStefano Zampini m = A->rmap->n; 1448*ccdfe979SStefano Zampini k = B->cmap->n; 1449*ccdfe979SStefano Zampini n = B->rmap->n; 1450*ccdfe979SStefano Zampini break; 1451*ccdfe979SStefano Zampini break; 1452*ccdfe979SStefano Zampini default: 1453*ccdfe979SStefano Zampini SETERRQ1(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Unsupported product type %s",MatProductTypes[product->type]); 1454*ccdfe979SStefano Zampini } 1455*ccdfe979SStefano Zampini if (!mat) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Missing Mat_SeqAIJCUSPARSEMultStruct"); 1456*ccdfe979SStefano Zampini matA = mat->descr; 1457*ccdfe979SStefano Zampini csrmat = (CsrMatrix*)mat->mat; 1458*ccdfe979SStefano Zampini /* if the user passed a CPU matrix, copy the data to the GPU */ 1459*ccdfe979SStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)B,MATSEQDENSECUDA,&biscuda);CHKERRQ(ierr); 1460*ccdfe979SStefano Zampini if (!biscuda) { 1461*ccdfe979SStefano Zampini ierr = MatConvert(B,MATSEQDENSECUDA,MAT_INPLACE_MATRIX,&B);CHKERRQ(ierr); 1462*ccdfe979SStefano Zampini } 1463*ccdfe979SStefano Zampini ierr = MatDenseCUDAGetArrayRead(B,&barray);CHKERRQ(ierr); 1464*ccdfe979SStefano Zampini ierr = MatDenseGetLDA(B,&blda);CHKERRQ(ierr); 1465*ccdfe979SStefano Zampini /* cusparse spmm does not support transpose on B */ 1466*ccdfe979SStefano Zampini if (product->type == MATPRODUCT_ABt || product->type == MATPRODUCT_RARt) { 1467*ccdfe979SStefano Zampini cublasHandle_t cublasv2handle; 1468*ccdfe979SStefano Zampini cublasStatus_t cerr; 1469*ccdfe979SStefano Zampini 1470*ccdfe979SStefano Zampini ierr = PetscCUBLASGetHandle(&cublasv2handle);CHKERRQ(ierr); 1471*ccdfe979SStefano Zampini cerr = cublasXgeam(cublasv2handle,CUBLAS_OP_T,CUBLAS_OP_T, 1472*ccdfe979SStefano Zampini B->cmap->n,B->rmap->n, 1473*ccdfe979SStefano Zampini &PETSC_CUSPARSE_ONE ,barray,blda, 1474*ccdfe979SStefano Zampini &PETSC_CUSPARSE_ZERO,barray,blda, 1475*ccdfe979SStefano Zampini mmdata->Bt,B->cmap->n);CHKERRCUBLAS(cerr); 1476*ccdfe979SStefano Zampini blda = B->cmap->n; 1477*ccdfe979SStefano Zampini } 1478*ccdfe979SStefano Zampini if (product->type == MATPRODUCT_RARt || product->type == MATPRODUCT_PtAP) { 1479*ccdfe979SStefano Zampini ierr = MatDenseCUDAGetArrayWrite(mmdata->X,&carray);CHKERRQ(ierr); 1480*ccdfe979SStefano Zampini ierr = MatDenseGetLDA(mmdata->X,&clda);CHKERRQ(ierr); 1481*ccdfe979SStefano Zampini } else { 1482*ccdfe979SStefano Zampini ierr = MatDenseCUDAGetArrayWrite(C,&carray);CHKERRQ(ierr); 1483*ccdfe979SStefano Zampini ierr = MatDenseGetLDA(C,&clda);CHKERRQ(ierr); 1484*ccdfe979SStefano Zampini } 1485*ccdfe979SStefano Zampini 1486*ccdfe979SStefano Zampini /* perform the MatMat operation */ 1487*ccdfe979SStefano Zampini stat = cusparse_csr_spmm(cusp->handle,opA,m,n,k, 1488*ccdfe979SStefano Zampini csrmat->num_entries,mat->alpha,matA, 1489*ccdfe979SStefano Zampini csrmat->values->data().get(), 1490*ccdfe979SStefano Zampini csrmat->row_offsets->data().get(), 1491*ccdfe979SStefano Zampini csrmat->column_indices->data().get(), 1492*ccdfe979SStefano Zampini mmdata->Bt ? mmdata->Bt : barray,blda,mat->beta_zero, 1493*ccdfe979SStefano Zampini carray,clda);CHKERRCUSPARSE(stat); 1494*ccdfe979SStefano Zampini 1495*ccdfe979SStefano Zampini ierr = MatDenseCUDARestoreArrayRead(B,&barray);CHKERRQ(ierr); 1496*ccdfe979SStefano Zampini if (product->type == MATPRODUCT_RARt) { 1497*ccdfe979SStefano Zampini ierr = MatDenseCUDARestoreArrayWrite(mmdata->X,&carray);CHKERRQ(ierr); 1498*ccdfe979SStefano Zampini ierr = MatMatMultNumeric_SeqDenseCUDA_SeqDenseCUDA_Private(B,mmdata->X,C,PETSC_FALSE,PETSC_FALSE);CHKERRQ(ierr); 1499*ccdfe979SStefano Zampini } else if (product->type == MATPRODUCT_PtAP) { 1500*ccdfe979SStefano Zampini ierr = MatDenseCUDARestoreArrayWrite(mmdata->X,&carray);CHKERRQ(ierr); 1501*ccdfe979SStefano Zampini ierr = MatMatMultNumeric_SeqDenseCUDA_SeqDenseCUDA_Private(B,mmdata->X,C,PETSC_TRUE,PETSC_FALSE);CHKERRQ(ierr); 1502*ccdfe979SStefano Zampini } else { 1503*ccdfe979SStefano Zampini ierr = MatDenseCUDARestoreArrayWrite(C,&carray);CHKERRQ(ierr); 1504*ccdfe979SStefano Zampini } 1505*ccdfe979SStefano Zampini if (mmdata->cisdense) { 1506*ccdfe979SStefano Zampini ierr = MatConvert(C,MATSEQDENSE,MAT_INPLACE_MATRIX,&C);CHKERRQ(ierr); 1507*ccdfe979SStefano Zampini } 1508*ccdfe979SStefano Zampini if (!biscuda) { 1509*ccdfe979SStefano Zampini ierr = MatConvert(B,MATSEQDENSE,MAT_INPLACE_MATRIX,&B);CHKERRQ(ierr); 1510*ccdfe979SStefano Zampini } 1511*ccdfe979SStefano Zampini PetscFunctionReturn(0); 1512*ccdfe979SStefano Zampini } 1513*ccdfe979SStefano Zampini 1514*ccdfe979SStefano Zampini static PetscErrorCode MatProductSymbolic_SeqAIJCUSPARSE_SeqDENSECUDA(Mat C) 1515*ccdfe979SStefano Zampini { 1516*ccdfe979SStefano Zampini Mat_Product *product = C->product; 1517*ccdfe979SStefano Zampini Mat A,B; 1518*ccdfe979SStefano Zampini PetscInt m,n; 1519*ccdfe979SStefano Zampini PetscBool cisdense,flg; 1520*ccdfe979SStefano Zampini PetscErrorCode ierr; 1521*ccdfe979SStefano Zampini MatMatCusparse *mmdata; 1522*ccdfe979SStefano Zampini Mat_SeqAIJCUSPARSE *cusp; 1523*ccdfe979SStefano Zampini 1524*ccdfe979SStefano Zampini PetscFunctionBegin; 1525*ccdfe979SStefano Zampini MatCheckProduct(C,1); 1526*ccdfe979SStefano Zampini if (C->product->data) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Product data not empty"); 1527*ccdfe979SStefano Zampini A = product->A; 1528*ccdfe979SStefano Zampini B = product->B; 1529*ccdfe979SStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQAIJCUSPARSE,&flg);CHKERRQ(ierr); 1530*ccdfe979SStefano Zampini if (!flg) SETERRQ1(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Not for type %s",((PetscObject)A)->type_name); 1531*ccdfe979SStefano Zampini cusp = (Mat_SeqAIJCUSPARSE*)A->spptr; 1532*ccdfe979SStefano Zampini if (cusp->format != MAT_CUSPARSE_CSR) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Only for MAT_CUSPARSE_CSR format"); 1533*ccdfe979SStefano Zampini switch (product->type) { 1534*ccdfe979SStefano Zampini case MATPRODUCT_AB: 1535*ccdfe979SStefano Zampini m = A->rmap->n; 1536*ccdfe979SStefano Zampini n = B->cmap->n; 1537*ccdfe979SStefano Zampini break; 1538*ccdfe979SStefano Zampini case MATPRODUCT_AtB: 1539*ccdfe979SStefano Zampini m = A->cmap->n; 1540*ccdfe979SStefano Zampini n = B->cmap->n; 1541*ccdfe979SStefano Zampini break; 1542*ccdfe979SStefano Zampini case MATPRODUCT_ABt: 1543*ccdfe979SStefano Zampini m = A->rmap->n; 1544*ccdfe979SStefano Zampini n = B->rmap->n; 1545*ccdfe979SStefano Zampini break; 1546*ccdfe979SStefano Zampini case MATPRODUCT_PtAP: 1547*ccdfe979SStefano Zampini m = B->cmap->n; 1548*ccdfe979SStefano Zampini n = B->cmap->n; 1549*ccdfe979SStefano Zampini break; 1550*ccdfe979SStefano Zampini case MATPRODUCT_RARt: 1551*ccdfe979SStefano Zampini m = B->rmap->n; 1552*ccdfe979SStefano Zampini n = B->rmap->n; 1553*ccdfe979SStefano Zampini break; 1554*ccdfe979SStefano Zampini default: 1555*ccdfe979SStefano Zampini SETERRQ1(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Unsupported product type %s",MatProductTypes[product->type]); 1556*ccdfe979SStefano Zampini } 1557*ccdfe979SStefano Zampini ierr = MatSetSizes(C,m,n,m,n);CHKERRQ(ierr); 1558*ccdfe979SStefano Zampini /* if C is of type MATSEQDENSE (CPU), perform the operation on the GPU and then copy on the CPU */ 1559*ccdfe979SStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)C,MATSEQDENSE,&cisdense);CHKERRQ(ierr); 1560*ccdfe979SStefano Zampini ierr = MatSetType(C,MATSEQDENSECUDA);CHKERRQ(ierr); 1561*ccdfe979SStefano Zampini 1562*ccdfe979SStefano Zampini /* product data */ 1563*ccdfe979SStefano Zampini ierr = PetscNew(&mmdata);CHKERRQ(ierr); 1564*ccdfe979SStefano Zampini mmdata->cisdense = cisdense; 1565*ccdfe979SStefano Zampini /* cusparse spmm does not support transpose on B */ 1566*ccdfe979SStefano Zampini if (product->type == MATPRODUCT_ABt || product->type == MATPRODUCT_RARt) { 1567*ccdfe979SStefano Zampini cudaError_t cerr; 1568*ccdfe979SStefano Zampini 1569*ccdfe979SStefano Zampini cerr = cudaMalloc((void**)&mmdata->Bt,(size_t)B->rmap->n*(size_t)B->cmap->n*sizeof(PetscScalar));CHKERRCUDA(cerr); 1570*ccdfe979SStefano Zampini } 1571*ccdfe979SStefano Zampini /* for these products we need intermediate storage */ 1572*ccdfe979SStefano Zampini if (product->type == MATPRODUCT_RARt || product->type == MATPRODUCT_PtAP) { 1573*ccdfe979SStefano Zampini ierr = MatCreate(PetscObjectComm((PetscObject)C),&mmdata->X);CHKERRQ(ierr); 1574*ccdfe979SStefano Zampini ierr = MatSetType(mmdata->X,MATSEQDENSECUDA);CHKERRQ(ierr); 1575*ccdfe979SStefano Zampini if (product->type == MATPRODUCT_RARt) { /* do not preallocate, since the first call to MatDenseCUDAGetArray will preallocate on the GPU for us */ 1576*ccdfe979SStefano Zampini ierr = MatSetSizes(mmdata->X,A->rmap->n,B->rmap->n,A->rmap->n,B->rmap->n);CHKERRQ(ierr); 1577*ccdfe979SStefano Zampini } else { 1578*ccdfe979SStefano Zampini ierr = MatSetSizes(mmdata->X,A->rmap->n,B->cmap->n,A->rmap->n,B->cmap->n);CHKERRQ(ierr); 1579*ccdfe979SStefano Zampini } 1580*ccdfe979SStefano Zampini } 1581*ccdfe979SStefano Zampini C->product->data = mmdata; 1582*ccdfe979SStefano Zampini C->product->destroy = MatDestroy_MatMatCusparse; 1583*ccdfe979SStefano Zampini 1584*ccdfe979SStefano Zampini C->ops->productnumeric = MatProductNumeric_SeqAIJCUSPARSE_SeqDENSECUDA; 1585*ccdfe979SStefano Zampini PetscFunctionReturn(0); 1586*ccdfe979SStefano Zampini } 1587*ccdfe979SStefano Zampini 1588*ccdfe979SStefano Zampini PETSC_INTERN PetscErrorCode MatProductSetFromOptions_SeqAIJ_SeqDense(Mat); 1589*ccdfe979SStefano Zampini 1590*ccdfe979SStefano Zampini /* handles dense B */ 1591*ccdfe979SStefano Zampini static PetscErrorCode MatProductSetFromOptions_SeqAIJCUSPARSE(Mat C) 1592*ccdfe979SStefano Zampini { 1593*ccdfe979SStefano Zampini Mat_Product *product = C->product; 1594*ccdfe979SStefano Zampini PetscErrorCode ierr; 1595*ccdfe979SStefano Zampini 1596*ccdfe979SStefano Zampini PetscFunctionBegin; 1597*ccdfe979SStefano Zampini MatCheckProduct(C,1); 1598*ccdfe979SStefano Zampini if (!product->A) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing A"); 1599*ccdfe979SStefano Zampini if (product->A->boundtocpu) { 1600*ccdfe979SStefano Zampini ierr = MatProductSetFromOptions_SeqAIJ_SeqDense(C);CHKERRQ(ierr); 1601*ccdfe979SStefano Zampini PetscFunctionReturn(0); 1602*ccdfe979SStefano Zampini } 1603*ccdfe979SStefano Zampini switch (product->type) { 1604*ccdfe979SStefano Zampini case MATPRODUCT_AB: 1605*ccdfe979SStefano Zampini case MATPRODUCT_AtB: 1606*ccdfe979SStefano Zampini case MATPRODUCT_ABt: 1607*ccdfe979SStefano Zampini case MATPRODUCT_PtAP: 1608*ccdfe979SStefano Zampini case MATPRODUCT_RARt: 1609*ccdfe979SStefano Zampini C->ops->productsymbolic = MatProductSymbolic_SeqAIJCUSPARSE_SeqDENSECUDA; 1610*ccdfe979SStefano Zampini default: 1611*ccdfe979SStefano Zampini break; 1612*ccdfe979SStefano Zampini } 1613*ccdfe979SStefano Zampini PetscFunctionReturn(0); 1614*ccdfe979SStefano Zampini } 1615*ccdfe979SStefano Zampini 16166fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy) 16179ae82921SPaul Mullowney { 1618b175d8bbSPaul Mullowney PetscErrorCode ierr; 16199ae82921SPaul Mullowney 16209ae82921SPaul Mullowney PetscFunctionBegin; 16217656d835SStefano Zampini ierr = MatMultAdd_SeqAIJCUSPARSE(A,xx,NULL,yy);CHKERRQ(ierr); 16229ae82921SPaul Mullowney PetscFunctionReturn(0); 16239ae82921SPaul Mullowney } 16249ae82921SPaul Mullowney 16256fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy) 1626ca45077fSPaul Mullowney { 1627ca45077fSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1628aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 16299ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstructT; 1630465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 1631465f34aeSAlejandro Lamas Daviña PetscScalar *yarray; 1632b175d8bbSPaul Mullowney PetscErrorCode ierr; 163357d48284SJunchao Zhang cudaError_t cerr; 1634aa372e3fSPaul Mullowney cusparseStatus_t stat; 1635ca45077fSPaul Mullowney 1636ca45077fSPaul Mullowney PetscFunctionBegin; 163734d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 163834d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 16399ff858a8SKarl Rupp matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1640aa372e3fSPaul Mullowney if (!matstructT) { 1641bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr); 1642aa372e3fSPaul Mullowney matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1643bda325fcSPaul Mullowney } 1644c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1645c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(yy,&yarray);CHKERRQ(ierr); 1646f6ae8131SMark if (yy->map->n) { 1647f6ae8131SMark PetscInt n = yy->map->n; 1648f6ae8131SMark cudaError_t err; 1649f6ae8131SMark err = cudaMemset(yarray,0,n*sizeof(PetscScalar));CHKERRCUDA(err); /* hack to fix numerical errors from reading output vector yy, apparently */ 1650f6ae8131SMark } 1651aa372e3fSPaul Mullowney 16527a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1653aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1654aa372e3fSPaul Mullowney CsrMatrix *mat = (CsrMatrix*)matstructT->mat; 1655aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1656aa372e3fSPaul Mullowney mat->num_rows, mat->num_cols, 1657b06137fdSPaul Mullowney mat->num_entries, matstructT->alpha, matstructT->descr, 1658aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 16597656d835SStefano Zampini mat->column_indices->data().get(), xarray, matstructT->beta_zero, 166057d48284SJunchao Zhang yarray);CHKERRCUSPARSE(stat); 1661aa372e3fSPaul Mullowney } else { 1662aa372e3fSPaul Mullowney cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat; 1663aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1664b06137fdSPaul Mullowney matstructT->alpha, matstructT->descr, hybMat, 16657656d835SStefano Zampini xarray, matstructT->beta_zero, 166657d48284SJunchao Zhang yarray);CHKERRCUSPARSE(stat); 1667ca45077fSPaul Mullowney } 1668c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1669c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(yy,&yarray);CHKERRQ(ierr); 167057d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); 1671661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1672213423ffSJunchao Zhang ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr); 1673ca45077fSPaul Mullowney PetscFunctionReturn(0); 1674ca45077fSPaul Mullowney } 1675ca45077fSPaul Mullowney 1676aa372e3fSPaul Mullowney 16776fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz) 16789ae82921SPaul Mullowney { 16799ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1680aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 16819ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstruct; 1682465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 16837656d835SStefano Zampini PetscScalar *zarray,*dptr,*beta; 1684b175d8bbSPaul Mullowney PetscErrorCode ierr; 168557d48284SJunchao Zhang cudaError_t cerr; 1686aa372e3fSPaul Mullowney cusparseStatus_t stat; 1687213423ffSJunchao Zhang PetscBool compressed = a->compressedrow.use; /* Does the matrix use compressed rows (i.e., drop zero rows)? */ 16886e111a19SKarl Rupp 16899ae82921SPaul Mullowney PetscFunctionBegin; 169034d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 169134d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 16929ff858a8SKarl Rupp matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 16939ae82921SPaul Mullowney try { 1694c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1695213423ffSJunchao Zhang 1696213423ffSJunchao Zhang if (yy == zz) {ierr = VecCUDAGetArray(zz,&zarray);CHKERRQ(ierr);} /* read & write zz, so need to get uptodate zarray on GPU */ 1697213423ffSJunchao Zhang else {ierr = VecCUDAGetArrayWrite(zz,&zarray);CHKERRQ(ierr);} /* write zz, so no need to init zarray on GPU */ 1698213423ffSJunchao Zhang 1699213423ffSJunchao Zhang dptr = compressed ? cusparsestruct->workVector->data().get() : zarray; /* Use a work vector for SpMv result if compressed */ 1700213423ffSJunchao Zhang beta = (yy == zz && !compressed) ? matstruct->beta_one : matstruct->beta_zero; 17019ae82921SPaul Mullowney 17027a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1703213423ffSJunchao Zhang /* csr_spmv does y = alpha*Ax + beta*y */ 1704aa372e3fSPaul Mullowney if (cusparsestruct->format == MAT_CUSPARSE_CSR) { 1705b06137fdSPaul Mullowney /* here we need to be careful to set the number of rows in the multiply to the 1706b06137fdSPaul Mullowney number of compressed rows in the matrix ... which is equivalent to the 1707b06137fdSPaul Mullowney size of the workVector */ 17087656d835SStefano Zampini CsrMatrix *mat = (CsrMatrix*)matstruct->mat; 1709aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1710a65300a6SPaul Mullowney mat->num_rows, mat->num_cols, 1711b06137fdSPaul Mullowney mat->num_entries, matstruct->alpha, matstruct->descr, 1712aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 17137656d835SStefano Zampini mat->column_indices->data().get(), xarray, beta, 171457d48284SJunchao Zhang dptr);CHKERRCUSPARSE(stat); 1715aa372e3fSPaul Mullowney } else { 1716213423ffSJunchao Zhang if (cusparsestruct->nrows) { 1717301298b4SMark Adams cusparseHybMat_t hybMat = (cusparseHybMat_t)matstruct->mat; 1718aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1719b06137fdSPaul Mullowney matstruct->alpha, matstruct->descr, hybMat, 17207656d835SStefano Zampini xarray, beta, 172157d48284SJunchao Zhang dptr);CHKERRCUSPARSE(stat); 1722a65300a6SPaul Mullowney } 1723aa372e3fSPaul Mullowney } 1724958c4211Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1725aa372e3fSPaul Mullowney 1726213423ffSJunchao Zhang if (yy) { /* MatMultAdd: zz = A*xx + yy */ 1727213423ffSJunchao Zhang if (compressed) { /* A is compressed. We first copy yy to zz, then ScatterAdd the work vector to zz */ 1728213423ffSJunchao Zhang ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); /* zz = yy */ 1729213423ffSJunchao Zhang } else if (zz != yy) { /* A's not compreseed. zz already contains A*xx, and we just need to add yy */ 1730213423ffSJunchao Zhang ierr = VecAXPY_SeqCUDA(zz,1.0,yy);CHKERRQ(ierr); /* zz += yy */ 17317656d835SStefano Zampini } 1732213423ffSJunchao Zhang } else if (compressed) { /* MatMult: zz = A*xx. A is compressed, so we zero zz first, then ScatterAdd the work vector to zz */ 1733c1fb3f03SStefano Zampini ierr = VecSet_SeqCUDA(zz,0);CHKERRQ(ierr); 17347656d835SStefano Zampini } 17357656d835SStefano Zampini 1736213423ffSJunchao Zhang /* ScatterAdd the result from work vector into the full vector when A is compressed */ 1737213423ffSJunchao Zhang ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1738213423ffSJunchao Zhang if (compressed) { 1739213423ffSJunchao Zhang thrust::device_ptr<PetscScalar> zptr = thrust::device_pointer_cast(zarray); 1740c41cb2e2SAlejandro Lamas Daviña thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))), 1741c41cb2e2SAlejandro Lamas Daviña thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))) + cusparsestruct->workVector->size(), 1742c41cb2e2SAlejandro Lamas Daviña VecCUDAPlusEquals()); 17437656d835SStefano Zampini } 1744958c4211Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1745c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1746213423ffSJunchao Zhang if (yy == zz) {ierr = VecCUDARestoreArray(zz,&zarray);CHKERRQ(ierr);} 1747213423ffSJunchao Zhang else {ierr = VecCUDARestoreArrayWrite(zz,&zarray);CHKERRQ(ierr);} 17489ae82921SPaul Mullowney } catch(char *ex) { 17499ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 17509ae82921SPaul Mullowney } 175157d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); 1752958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr); 17539ae82921SPaul Mullowney PetscFunctionReturn(0); 17549ae82921SPaul Mullowney } 17559ae82921SPaul Mullowney 17566fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz) 1757ca45077fSPaul Mullowney { 1758ca45077fSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1759aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 17609ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstructT; 1761465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 176220291eb5SJunchao Zhang PetscScalar *zarray,*beta; 1763b175d8bbSPaul Mullowney PetscErrorCode ierr; 176457d48284SJunchao Zhang cudaError_t cerr; 1765aa372e3fSPaul Mullowney cusparseStatus_t stat; 17666e111a19SKarl Rupp 1767ca45077fSPaul Mullowney PetscFunctionBegin; 176834d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 176934d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 17709ff858a8SKarl Rupp matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1771aa372e3fSPaul Mullowney if (!matstructT) { 1772bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr); 1773aa372e3fSPaul Mullowney matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1774bda325fcSPaul Mullowney } 1775aa372e3fSPaul Mullowney 177620291eb5SJunchao Zhang /* Note unlike Mat, MatTranspose uses non-compressed row storage */ 1777ca45077fSPaul Mullowney try { 1778c41cb2e2SAlejandro Lamas Daviña ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); 1779c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1780f2d70e9dSBarry Smith ierr = VecCUDAGetArray(zz,&zarray);CHKERRQ(ierr); 178120291eb5SJunchao Zhang beta = (yy == zz) ? matstructT->beta_one : matstructT->beta_zero; 1782ca45077fSPaul Mullowney 17837a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1784e057df02SPaul Mullowney /* multiply add with matrix transpose */ 1785aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1786aa372e3fSPaul Mullowney CsrMatrix *mat = (CsrMatrix*)matstructT->mat; 1787aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1788a65300a6SPaul Mullowney mat->num_rows, mat->num_cols, 1789b06137fdSPaul Mullowney mat->num_entries, matstructT->alpha, matstructT->descr, 1790aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 1791a3fdcf43SKarl Rupp mat->column_indices->data().get(), xarray, beta, 17929f74ec24SSatish Balay zarray);CHKERRCUSPARSE(stat); 1793aa372e3fSPaul Mullowney } else { 1794aa372e3fSPaul Mullowney cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat; 1795213423ffSJunchao Zhang if (cusparsestruct->nrows) { 1796aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1797b06137fdSPaul Mullowney matstructT->alpha, matstructT->descr, hybMat, 1798a3fdcf43SKarl Rupp xarray, beta, 17999f74ec24SSatish Balay zarray);CHKERRCUSPARSE(stat); 1800a65300a6SPaul Mullowney } 1801aa372e3fSPaul Mullowney } 1802a3fdcf43SKarl Rupp ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1803a3fdcf43SKarl Rupp 180420291eb5SJunchao Zhang if (zz != yy) {ierr = VecAXPY_SeqCUDA(zz,1.0,yy);CHKERRQ(ierr);} 1805c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1806f2d70e9dSBarry Smith ierr = VecCUDARestoreArray(zz,&zarray);CHKERRQ(ierr); 1807ca45077fSPaul Mullowney } catch(char *ex) { 1808ca45077fSPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 1809661c2d29Shannah_mairs } 18109f74ec24SSatish Balay cerr = WaitForGPU();CHKERRCUDA(cerr); 1811958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr); 1812ca45077fSPaul Mullowney PetscFunctionReturn(0); 1813ca45077fSPaul Mullowney } 1814ca45077fSPaul Mullowney 18156fa9248bSJed Brown static PetscErrorCode MatAssemblyEnd_SeqAIJCUSPARSE(Mat A,MatAssemblyType mode) 18169ae82921SPaul Mullowney { 18179ae82921SPaul Mullowney PetscErrorCode ierr; 18186e111a19SKarl Rupp 18199ae82921SPaul Mullowney PetscFunctionBegin; 18209ae82921SPaul Mullowney ierr = MatAssemblyEnd_SeqAIJ(A,mode);CHKERRQ(ierr); 182195639643SRichard Tran Mills if (mode == MAT_FLUSH_ASSEMBLY || A->boundtocpu) PetscFunctionReturn(0); 1822bc3f50f2SPaul Mullowney if (A->factortype == MAT_FACTOR_NONE) { 1823e057df02SPaul Mullowney ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 1824bc3f50f2SPaul Mullowney } 1825bbf3fe20SPaul Mullowney A->ops->mult = MatMult_SeqAIJCUSPARSE; 1826bbf3fe20SPaul Mullowney A->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 1827bbf3fe20SPaul Mullowney A->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1828bbf3fe20SPaul Mullowney A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 18299ae82921SPaul Mullowney PetscFunctionReturn(0); 18309ae82921SPaul Mullowney } 18319ae82921SPaul Mullowney 18329ae82921SPaul Mullowney /* --------------------------------------------------------------------------------*/ 1833e057df02SPaul Mullowney /*@ 18349ae82921SPaul Mullowney MatCreateSeqAIJCUSPARSE - Creates a sparse matrix in AIJ (compressed row) format 1835e057df02SPaul Mullowney (the default parallel PETSc format). This matrix will ultimately pushed down 1836e057df02SPaul Mullowney to NVidia GPUs and use the CUSPARSE library for calculations. For good matrix 1837e057df02SPaul Mullowney assembly performance the user should preallocate the matrix storage by setting 1838e057df02SPaul Mullowney the parameter nz (or the array nnz). By setting these parameters accurately, 1839e057df02SPaul Mullowney performance during matrix assembly can be increased by more than a factor of 50. 18409ae82921SPaul Mullowney 1841d083f849SBarry Smith Collective 18429ae82921SPaul Mullowney 18439ae82921SPaul Mullowney Input Parameters: 18449ae82921SPaul Mullowney + comm - MPI communicator, set to PETSC_COMM_SELF 18459ae82921SPaul Mullowney . m - number of rows 18469ae82921SPaul Mullowney . n - number of columns 18479ae82921SPaul Mullowney . nz - number of nonzeros per row (same for all rows) 18489ae82921SPaul Mullowney - nnz - array containing the number of nonzeros in the various rows 18490298fd71SBarry Smith (possibly different for each row) or NULL 18509ae82921SPaul Mullowney 18519ae82921SPaul Mullowney Output Parameter: 18529ae82921SPaul Mullowney . A - the matrix 18539ae82921SPaul Mullowney 18549ae82921SPaul Mullowney It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 18559ae82921SPaul Mullowney MatXXXXSetPreallocation() paradgm instead of this routine directly. 18569ae82921SPaul Mullowney [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 18579ae82921SPaul Mullowney 18589ae82921SPaul Mullowney Notes: 18599ae82921SPaul Mullowney If nnz is given then nz is ignored 18609ae82921SPaul Mullowney 18619ae82921SPaul Mullowney The AIJ format (also called the Yale sparse matrix format or 18629ae82921SPaul Mullowney compressed row storage), is fully compatible with standard Fortran 77 18639ae82921SPaul Mullowney storage. That is, the stored row and column indices can begin at 18649ae82921SPaul Mullowney either one (as in Fortran) or zero. See the users' manual for details. 18659ae82921SPaul Mullowney 18669ae82921SPaul Mullowney Specify the preallocated storage with either nz or nnz (not both). 18670298fd71SBarry Smith Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory 18689ae82921SPaul Mullowney allocation. For large problems you MUST preallocate memory or you 18699ae82921SPaul Mullowney will get TERRIBLE performance, see the users' manual chapter on matrices. 18709ae82921SPaul Mullowney 18719ae82921SPaul Mullowney By default, this format uses inodes (identical nodes) when possible, to 18729ae82921SPaul Mullowney improve numerical efficiency of matrix-vector products and solves. We 18739ae82921SPaul Mullowney search for consecutive rows with the same nonzero structure, thereby 18749ae82921SPaul Mullowney reusing matrix information to achieve increased efficiency. 18759ae82921SPaul Mullowney 18769ae82921SPaul Mullowney Level: intermediate 18779ae82921SPaul Mullowney 1878e057df02SPaul Mullowney .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ(), MATSEQAIJCUSPARSE, MATAIJCUSPARSE 18799ae82921SPaul Mullowney @*/ 18809ae82921SPaul Mullowney PetscErrorCode MatCreateSeqAIJCUSPARSE(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 18819ae82921SPaul Mullowney { 18829ae82921SPaul Mullowney PetscErrorCode ierr; 18839ae82921SPaul Mullowney 18849ae82921SPaul Mullowney PetscFunctionBegin; 18859ae82921SPaul Mullowney ierr = MatCreate(comm,A);CHKERRQ(ierr); 18869ae82921SPaul Mullowney ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 18879ae82921SPaul Mullowney ierr = MatSetType(*A,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 18889ae82921SPaul Mullowney ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr); 18899ae82921SPaul Mullowney PetscFunctionReturn(0); 18909ae82921SPaul Mullowney } 18919ae82921SPaul Mullowney 18926fa9248bSJed Brown static PetscErrorCode MatDestroy_SeqAIJCUSPARSE(Mat A) 18939ae82921SPaul Mullowney { 18949ae82921SPaul Mullowney PetscErrorCode ierr; 1895ab25e6cbSDominic Meiser 18969ae82921SPaul Mullowney PetscFunctionBegin; 18979ae82921SPaul Mullowney if (A->factortype == MAT_FACTOR_NONE) { 1898c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) { 1899470880abSPatrick Sanan ierr = MatSeqAIJCUSPARSE_Destroy((Mat_SeqAIJCUSPARSE**)&A->spptr);CHKERRQ(ierr); 19009ae82921SPaul Mullowney } 19019ae82921SPaul Mullowney } else { 1902470880abSPatrick Sanan ierr = MatSeqAIJCUSPARSETriFactors_Destroy((Mat_SeqAIJCUSPARSETriFactors**)&A->spptr);CHKERRQ(ierr); 1903aa372e3fSPaul Mullowney } 1904*ccdfe979SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatCUSPARSESetFormat_C",NULL);CHKERRQ(ierr); 1905*ccdfe979SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaijcusparse_seqdensecuda_C",NULL);CHKERRQ(ierr); 1906*ccdfe979SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaijcusparse_seqdense_C",NULL);CHKERRQ(ierr); 1907*ccdfe979SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatFactorGetSolverType_C",NULL);CHKERRQ(ierr); 19089ae82921SPaul Mullowney ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr); 19099ae82921SPaul Mullowney PetscFunctionReturn(0); 19109ae82921SPaul Mullowney } 19119ae82921SPaul Mullowney 1912*ccdfe979SStefano Zampini PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat,MatType,MatReuse,Mat*); 191395639643SRichard Tran Mills static PetscErrorCode MatBindToCPU_SeqAIJCUSPARSE(Mat,PetscBool); 19149ff858a8SKarl Rupp static PetscErrorCode MatDuplicate_SeqAIJCUSPARSE(Mat A,MatDuplicateOption cpvalues,Mat *B) 19159ff858a8SKarl Rupp { 19169ff858a8SKarl Rupp PetscErrorCode ierr; 19179ff858a8SKarl Rupp 19189ff858a8SKarl Rupp PetscFunctionBegin; 19199ff858a8SKarl Rupp ierr = MatDuplicate_SeqAIJ(A,cpvalues,B);CHKERRQ(ierr); 1920*ccdfe979SStefano Zampini ierr = MatConvert_SeqAIJ_SeqAIJCUSPARSE(*B,MATSEQAIJCUSPARSE,MAT_INPLACE_MATRIX,B);CHKERRQ(ierr); 19219ff858a8SKarl Rupp PetscFunctionReturn(0); 19229ff858a8SKarl Rupp } 19239ff858a8SKarl Rupp 192495639643SRichard Tran Mills static PetscErrorCode MatBindToCPU_SeqAIJCUSPARSE(Mat A,PetscBool flg) 192595639643SRichard Tran Mills { 192695639643SRichard Tran Mills PetscFunctionBegin; 1927c34f1ff0SRichard Tran Mills /* Currently, there is no case in which an AIJCUSPARSE matrix ever has its offloadmask set to PETS_OFFLOAD_GPU. 192895639643SRichard Tran Mills If this changes, we need to implement a routine to update the CPU (host) version of the matrix from the GPU one. 192995639643SRichard Tran Mills Right now, for safety we simply check for PETSC_OFFLOAD_GPU and have MatBindToCPU() do nothing in this case. 193095639643SRichard Tran Mills TODO: Add MatAIJCUSPARSECopyFromGPU() and make MatBindToCPU() functional for AIJCUSPARSE matries; 193195639643SRichard Tran Mills can follow the example of MatBindToCPU_SeqAIJViennaCL(). */ 1932*ccdfe979SStefano Zampini /* We need to take care of function composition also */ 193395639643SRichard Tran Mills if (A->offloadmask == PETSC_OFFLOAD_GPU) PetscFunctionReturn(0); 193495639643SRichard Tran Mills if (flg) { 193595639643SRichard Tran Mills A->ops->mult = MatMult_SeqAIJ; 193695639643SRichard Tran Mills A->ops->multadd = MatMultAdd_SeqAIJ; 1937c34f1ff0SRichard Tran Mills A->ops->multtranspose = MatMultTranspose_SeqAIJ; 1938c34f1ff0SRichard Tran Mills A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJ; 193995639643SRichard Tran Mills A->ops->assemblyend = MatAssemblyEnd_SeqAIJ; 194095639643SRichard Tran Mills A->ops->duplicate = MatDuplicate_SeqAIJ; 194195639643SRichard Tran Mills } else { 194295639643SRichard Tran Mills A->ops->mult = MatMult_SeqAIJCUSPARSE; 194395639643SRichard Tran Mills A->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 194495639643SRichard Tran Mills A->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 194595639643SRichard Tran Mills A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 194695639643SRichard Tran Mills A->ops->assemblyend = MatAssemblyEnd_SeqAIJCUSPARSE; 194795639643SRichard Tran Mills A->ops->destroy = MatDestroy_SeqAIJCUSPARSE; 194895639643SRichard Tran Mills A->ops->duplicate = MatDuplicate_SeqAIJCUSPARSE; 194995639643SRichard Tran Mills } 195095639643SRichard Tran Mills A->boundtocpu = flg; 195195639643SRichard Tran Mills PetscFunctionReturn(0); 195295639643SRichard Tran Mills } 195395639643SRichard Tran Mills 19540ce8acdeSStefano Zampini PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat B, MatType mtype, MatReuse reuse, Mat* newmat) 19559ae82921SPaul Mullowney { 19569ae82921SPaul Mullowney PetscErrorCode ierr; 1957aa372e3fSPaul Mullowney cusparseStatus_t stat; 1958aa372e3fSPaul Mullowney cusparseHandle_t handle=0; 19599ae82921SPaul Mullowney 19609ae82921SPaul Mullowney PetscFunctionBegin; 19610ce8acdeSStefano Zampini if (reuse != MAT_INPLACE_MATRIX) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet supported"); 196234136279SStefano Zampini ierr = PetscFree(B->defaultvectype);CHKERRQ(ierr); 196334136279SStefano Zampini ierr = PetscStrallocpy(VECCUDA,&B->defaultvectype);CHKERRQ(ierr); 196434136279SStefano Zampini 19659ae82921SPaul Mullowney if (B->factortype == MAT_FACTOR_NONE) { 1966e057df02SPaul Mullowney /* you cannot check the inode.use flag here since the matrix was just created. 1967e057df02SPaul Mullowney now build a GPU matrix data structure */ 19689ae82921SPaul Mullowney B->spptr = new Mat_SeqAIJCUSPARSE; 19699ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->mat = 0; 1970aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->matTranspose = 0; 1971aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->workVector = 0; 197281902715SJunchao Zhang ((Mat_SeqAIJCUSPARSE*)B->spptr)->rowoffsets_gpu = 0; 1973e057df02SPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->format = MAT_CUSPARSE_CSR; 1974aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->stream = 0; 197557d48284SJunchao Zhang stat = cusparseCreate(&handle);CHKERRCUSPARSE(stat); 1976aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->handle = handle; 19779ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)B->spptr)->nonzerostate = 0; 19789ae82921SPaul Mullowney } else { 19799ae82921SPaul Mullowney /* NEXT, set the pointers to the triangular factors */ 1980debe9ee2SPaul Mullowney B->spptr = new Mat_SeqAIJCUSPARSETriFactors; 19819ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtr = 0; 19829ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtr = 0; 1983aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtrTranspose = 0; 1984aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtrTranspose = 0; 1985aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->rpermIndices = 0; 1986aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->cpermIndices = 0; 1987aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->workVector = 0; 198857d48284SJunchao Zhang stat = cusparseCreate(&handle);CHKERRCUSPARSE(stat); 1989aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->handle = handle; 1990aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->nnz = 0; 19919ae82921SPaul Mullowney } 1992aa372e3fSPaul Mullowney 19939ae82921SPaul Mullowney B->ops->assemblyend = MatAssemblyEnd_SeqAIJCUSPARSE; 19949ae82921SPaul Mullowney B->ops->destroy = MatDestroy_SeqAIJCUSPARSE; 19959ae82921SPaul Mullowney B->ops->setfromoptions = MatSetFromOptions_SeqAIJCUSPARSE; 1996ca45077fSPaul Mullowney B->ops->mult = MatMult_SeqAIJCUSPARSE; 1997ca45077fSPaul Mullowney B->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 1998ca45077fSPaul Mullowney B->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1999ca45077fSPaul Mullowney B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 20009ff858a8SKarl Rupp B->ops->duplicate = MatDuplicate_SeqAIJCUSPARSE; 200195639643SRichard Tran Mills B->ops->bindtocpu = MatBindToCPU_SeqAIJCUSPARSE; 20022205254eSKarl Rupp 20039ae82921SPaul Mullowney ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 20042205254eSKarl Rupp 200595639643SRichard Tran Mills B->boundtocpu = PETSC_FALSE; 2006c70f7ee4SJunchao Zhang B->offloadmask = PETSC_OFFLOAD_UNALLOCATED; 20072205254eSKarl Rupp 2008bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatCUSPARSESetFormat_C",MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr); 2009*ccdfe979SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaijcusparse_seqdensecuda_C",MatProductSetFromOptions_SeqAIJCUSPARSE);CHKERRQ(ierr); 2010*ccdfe979SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaijcusparse_seqdense_C",MatProductSetFromOptions_SeqAIJCUSPARSE);CHKERRQ(ierr); 20119ae82921SPaul Mullowney PetscFunctionReturn(0); 20129ae82921SPaul Mullowney } 20139ae82921SPaul Mullowney 201402fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJCUSPARSE(Mat B) 201502fe1965SBarry Smith { 201602fe1965SBarry Smith PetscErrorCode ierr; 201702fe1965SBarry Smith 201802fe1965SBarry Smith PetscFunctionBegin; 201902fe1965SBarry Smith ierr = MatCreate_SeqAIJ(B);CHKERRQ(ierr); 20200ce8acdeSStefano Zampini ierr = MatConvert_SeqAIJ_SeqAIJCUSPARSE(B,MATSEQAIJCUSPARSE,MAT_INPLACE_MATRIX,&B);CHKERRQ(ierr); 202102fe1965SBarry Smith PetscFunctionReturn(0); 202202fe1965SBarry Smith } 202302fe1965SBarry Smith 20243ca39a21SBarry Smith /*MC 2025e057df02SPaul Mullowney MATSEQAIJCUSPARSE - MATAIJCUSPARSE = "(seq)aijcusparse" - A matrix type to be used for sparse matrices. 2026e057df02SPaul Mullowney 2027e057df02SPaul Mullowney A matrix type type whose data resides on Nvidia GPUs. These matrices can be in either 20282692e278SPaul Mullowney CSR, ELL, or Hybrid format. The ELL and HYB formats require CUDA 4.2 or later. 20292692e278SPaul Mullowney All matrix calculations are performed on Nvidia GPUs using the CUSPARSE library. 2030e057df02SPaul Mullowney 2031e057df02SPaul Mullowney Options Database Keys: 2032e057df02SPaul Mullowney + -mat_type aijcusparse - sets the matrix type to "seqaijcusparse" during a call to MatSetFromOptions() 2033aa372e3fSPaul Mullowney . -mat_cusparse_storage_format csr - sets the storage format of matrices (for MatMult and factors in MatSolve) during a call to MatSetFromOptions(). Other options include ell (ellpack) or hyb (hybrid). 2034a2b725a8SWilliam Gropp - -mat_cusparse_mult_storage_format csr - sets the storage format of matrices (for MatMult) during a call to MatSetFromOptions(). Other options include ell (ellpack) or hyb (hybrid). 2035e057df02SPaul Mullowney 2036e057df02SPaul Mullowney Level: beginner 2037e057df02SPaul Mullowney 20388468deeeSKarl Rupp .seealso: MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation 2039e057df02SPaul Mullowney M*/ 20407f756511SDominic Meiser 204142c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat,MatFactorType,Mat*); 204242c9c57cSBarry Smith 20430f39cd5aSBarry Smith 20443ca39a21SBarry Smith PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_CUSPARSE(void) 204542c9c57cSBarry Smith { 204642c9c57cSBarry Smith PetscErrorCode ierr; 204742c9c57cSBarry Smith 204842c9c57cSBarry Smith PetscFunctionBegin; 20493ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_LU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 20503ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_CHOLESKY,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 20513ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ILU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 20523ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ICC,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 205342c9c57cSBarry Smith PetscFunctionReturn(0); 205442c9c57cSBarry Smith } 205529b38603SBarry Smith 205681e08676SBarry Smith 2057470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE **cusparsestruct) 20587f756511SDominic Meiser { 20597f756511SDominic Meiser cusparseStatus_t stat; 20607f756511SDominic Meiser cusparseHandle_t handle; 20617f756511SDominic Meiser 20627f756511SDominic Meiser PetscFunctionBegin; 20637f756511SDominic Meiser if (*cusparsestruct) { 2064470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->mat,(*cusparsestruct)->format); 2065470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->matTranspose,(*cusparsestruct)->format); 20667f756511SDominic Meiser delete (*cusparsestruct)->workVector; 206781902715SJunchao Zhang delete (*cusparsestruct)->rowoffsets_gpu; 20687f756511SDominic Meiser if (handle = (*cusparsestruct)->handle) { 206957d48284SJunchao Zhang stat = cusparseDestroy(handle);CHKERRCUSPARSE(stat); 20707f756511SDominic Meiser } 20717f756511SDominic Meiser delete *cusparsestruct; 20727f756511SDominic Meiser *cusparsestruct = 0; 20737f756511SDominic Meiser } 20747f756511SDominic Meiser PetscFunctionReturn(0); 20757f756511SDominic Meiser } 20767f756511SDominic Meiser 20777f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix **mat) 20787f756511SDominic Meiser { 20797f756511SDominic Meiser PetscFunctionBegin; 20807f756511SDominic Meiser if (*mat) { 20817f756511SDominic Meiser delete (*mat)->values; 20827f756511SDominic Meiser delete (*mat)->column_indices; 20837f756511SDominic Meiser delete (*mat)->row_offsets; 20847f756511SDominic Meiser delete *mat; 20857f756511SDominic Meiser *mat = 0; 20867f756511SDominic Meiser } 20877f756511SDominic Meiser PetscFunctionReturn(0); 20887f756511SDominic Meiser } 20897f756511SDominic Meiser 2090470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct **trifactor) 20917f756511SDominic Meiser { 20927f756511SDominic Meiser cusparseStatus_t stat; 20937f756511SDominic Meiser PetscErrorCode ierr; 20947f756511SDominic Meiser 20957f756511SDominic Meiser PetscFunctionBegin; 20967f756511SDominic Meiser if (*trifactor) { 209757d48284SJunchao Zhang if ((*trifactor)->descr) { stat = cusparseDestroyMatDescr((*trifactor)->descr);CHKERRCUSPARSE(stat); } 209857d48284SJunchao Zhang if ((*trifactor)->solveInfo) { stat = cusparseDestroySolveAnalysisInfo((*trifactor)->solveInfo);CHKERRCUSPARSE(stat); } 20997f756511SDominic Meiser ierr = CsrMatrix_Destroy(&(*trifactor)->csrMat);CHKERRQ(ierr); 21007f756511SDominic Meiser delete *trifactor; 21017f756511SDominic Meiser *trifactor = 0; 21027f756511SDominic Meiser } 21037f756511SDominic Meiser PetscFunctionReturn(0); 21047f756511SDominic Meiser } 21057f756511SDominic Meiser 2106470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct **matstruct,MatCUSPARSEStorageFormat format) 21077f756511SDominic Meiser { 21087f756511SDominic Meiser CsrMatrix *mat; 21097f756511SDominic Meiser cusparseStatus_t stat; 21107f756511SDominic Meiser cudaError_t err; 21117f756511SDominic Meiser 21127f756511SDominic Meiser PetscFunctionBegin; 21137f756511SDominic Meiser if (*matstruct) { 21147f756511SDominic Meiser if ((*matstruct)->mat) { 21157f756511SDominic Meiser if (format==MAT_CUSPARSE_ELL || format==MAT_CUSPARSE_HYB) { 21167f756511SDominic Meiser cusparseHybMat_t hybMat = (cusparseHybMat_t)(*matstruct)->mat; 211757d48284SJunchao Zhang stat = cusparseDestroyHybMat(hybMat);CHKERRCUSPARSE(stat); 21187f756511SDominic Meiser } else { 21197f756511SDominic Meiser mat = (CsrMatrix*)(*matstruct)->mat; 21207f756511SDominic Meiser CsrMatrix_Destroy(&mat); 21217f756511SDominic Meiser } 21227f756511SDominic Meiser } 212357d48284SJunchao Zhang if ((*matstruct)->descr) { stat = cusparseDestroyMatDescr((*matstruct)->descr);CHKERRCUSPARSE(stat); } 21247f756511SDominic Meiser delete (*matstruct)->cprowIndices; 2125c41cb2e2SAlejandro Lamas Daviña if ((*matstruct)->alpha) { err=cudaFree((*matstruct)->alpha);CHKERRCUDA(err); } 21267656d835SStefano Zampini if ((*matstruct)->beta_zero) { err=cudaFree((*matstruct)->beta_zero);CHKERRCUDA(err); } 21277656d835SStefano Zampini if ((*matstruct)->beta_one) { err=cudaFree((*matstruct)->beta_one);CHKERRCUDA(err); } 21287f756511SDominic Meiser delete *matstruct; 21297f756511SDominic Meiser *matstruct = 0; 21307f756511SDominic Meiser } 21317f756511SDominic Meiser PetscFunctionReturn(0); 21327f756511SDominic Meiser } 21337f756511SDominic Meiser 2134*ccdfe979SStefano Zampini static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Reset(Mat_SeqAIJCUSPARSETriFactors** trifactors) 21357f756511SDominic Meiser { 21367f756511SDominic Meiser PetscFunctionBegin; 21377f756511SDominic Meiser if (*trifactors) { 2138470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->loTriFactorPtr); 2139470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->upTriFactorPtr); 2140470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->loTriFactorPtrTranspose); 2141470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->upTriFactorPtrTranspose); 21427f756511SDominic Meiser delete (*trifactors)->rpermIndices; 21437f756511SDominic Meiser delete (*trifactors)->cpermIndices; 21447f756511SDominic Meiser delete (*trifactors)->workVector; 2145*ccdfe979SStefano Zampini (*trifactors)->rpermIndices = 0; 2146*ccdfe979SStefano Zampini (*trifactors)->cpermIndices = 0; 2147*ccdfe979SStefano Zampini (*trifactors)->workVector = 0; 2148*ccdfe979SStefano Zampini } 2149*ccdfe979SStefano Zampini PetscFunctionReturn(0); 2150*ccdfe979SStefano Zampini } 2151*ccdfe979SStefano Zampini 2152*ccdfe979SStefano Zampini static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors** trifactors) 2153*ccdfe979SStefano Zampini { 2154*ccdfe979SStefano Zampini cusparseHandle_t handle; 2155*ccdfe979SStefano Zampini cusparseStatus_t stat; 2156*ccdfe979SStefano Zampini 2157*ccdfe979SStefano Zampini PetscFunctionBegin; 2158*ccdfe979SStefano Zampini if (*trifactors) { 2159*ccdfe979SStefano Zampini MatSeqAIJCUSPARSETriFactors_Reset(trifactors); 21607f756511SDominic Meiser if (handle = (*trifactors)->handle) { 216157d48284SJunchao Zhang stat = cusparseDestroy(handle);CHKERRCUSPARSE(stat); 21627f756511SDominic Meiser } 21637f756511SDominic Meiser delete *trifactors; 21647f756511SDominic Meiser *trifactors = 0; 21657f756511SDominic Meiser } 21667f756511SDominic Meiser PetscFunctionReturn(0); 21677f756511SDominic Meiser } 21687f756511SDominic Meiser 2169