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); 40470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors**); 41470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE**); 427f756511SDominic Meiser 43b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSESetStream(Mat A,const cudaStream_t stream) 44b06137fdSPaul Mullowney { 45b06137fdSPaul Mullowney cusparseStatus_t stat; 46b06137fdSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 47b06137fdSPaul Mullowney 48b06137fdSPaul Mullowney PetscFunctionBegin; 49b06137fdSPaul Mullowney cusparsestruct->stream = stream; 5057d48284SJunchao Zhang stat = cusparseSetStream(cusparsestruct->handle,cusparsestruct->stream);CHKERRCUSPARSE(stat); 51b06137fdSPaul Mullowney PetscFunctionReturn(0); 52b06137fdSPaul Mullowney } 53b06137fdSPaul Mullowney 54b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSESetHandle(Mat A,const cusparseHandle_t handle) 55b06137fdSPaul Mullowney { 56b06137fdSPaul Mullowney cusparseStatus_t stat; 57b06137fdSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 58b06137fdSPaul Mullowney 59b06137fdSPaul Mullowney PetscFunctionBegin; 606b1cf21dSAlejandro Lamas Daviña if (cusparsestruct->handle != handle) { 6116a2e217SAlejandro Lamas Daviña if (cusparsestruct->handle) { 6257d48284SJunchao Zhang stat = cusparseDestroy(cusparsestruct->handle);CHKERRCUSPARSE(stat); 6316a2e217SAlejandro Lamas Daviña } 64b06137fdSPaul Mullowney cusparsestruct->handle = handle; 656b1cf21dSAlejandro Lamas Daviña } 6657d48284SJunchao Zhang stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUSPARSE(stat); 67b06137fdSPaul Mullowney PetscFunctionReturn(0); 68b06137fdSPaul Mullowney } 69b06137fdSPaul Mullowney 70b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSEClearHandle(Mat A) 71b06137fdSPaul Mullowney { 72b06137fdSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 73b06137fdSPaul Mullowney PetscFunctionBegin; 74b06137fdSPaul Mullowney if (cusparsestruct->handle) 75b06137fdSPaul Mullowney cusparsestruct->handle = 0; 76b06137fdSPaul Mullowney PetscFunctionReturn(0); 77b06137fdSPaul Mullowney } 78b06137fdSPaul Mullowney 79ea799195SBarry Smith PetscErrorCode MatFactorGetSolverType_seqaij_cusparse(Mat A,MatSolverType *type) 809ae82921SPaul Mullowney { 819ae82921SPaul Mullowney PetscFunctionBegin; 829ae82921SPaul Mullowney *type = MATSOLVERCUSPARSE; 839ae82921SPaul Mullowney PetscFunctionReturn(0); 849ae82921SPaul Mullowney } 859ae82921SPaul Mullowney 86c708e6cdSJed Brown /*MC 87087f3262SPaul Mullowney MATSOLVERCUSPARSE = "cusparse" - A matrix type providing triangular solvers for seq matrices 88087f3262SPaul Mullowney on a single GPU of type, seqaijcusparse, aijcusparse, or seqaijcusp, aijcusp. Currently supported 89087f3262SPaul Mullowney algorithms are ILU(k) and ICC(k). Typically, deeper factorizations (larger k) results in poorer 90087f3262SPaul Mullowney performance in the triangular solves. Full LU, and Cholesky decompositions can be solved through the 91087f3262SPaul Mullowney CUSPARSE triangular solve algorithm. However, the performance can be quite poor and thus these 92087f3262SPaul Mullowney algorithms are not recommended. This class does NOT support direct solver operations. 93c708e6cdSJed Brown 949ae82921SPaul Mullowney Level: beginner 95c708e6cdSJed Brown 963ca39a21SBarry Smith .seealso: PCFactorSetMatSolverType(), MatSolverType, MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation 97c708e6cdSJed Brown M*/ 989ae82921SPaul Mullowney 9942c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat A,MatFactorType ftype,Mat *B) 1009ae82921SPaul Mullowney { 1019ae82921SPaul Mullowney PetscErrorCode ierr; 102bc3f50f2SPaul Mullowney PetscInt n = A->rmap->n; 1039ae82921SPaul Mullowney 1049ae82921SPaul Mullowney PetscFunctionBegin; 105bc3f50f2SPaul Mullowney ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr); 106404133a2SPaul Mullowney (*B)->factortype = ftype; 107bc3f50f2SPaul Mullowney ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr); 1089ae82921SPaul Mullowney ierr = MatSetType(*B,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 1092205254eSKarl Rupp 110087f3262SPaul Mullowney if (ftype == MAT_FACTOR_LU || ftype == MAT_FACTOR_ILU || ftype == MAT_FACTOR_ILUDT) { 11133d57670SJed Brown ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr); 1129ae82921SPaul Mullowney (*B)->ops->ilufactorsymbolic = MatILUFactorSymbolic_SeqAIJCUSPARSE; 1139ae82921SPaul Mullowney (*B)->ops->lufactorsymbolic = MatLUFactorSymbolic_SeqAIJCUSPARSE; 114087f3262SPaul Mullowney } else if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) { 115087f3262SPaul Mullowney (*B)->ops->iccfactorsymbolic = MatICCFactorSymbolic_SeqAIJCUSPARSE; 116087f3262SPaul Mullowney (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqAIJCUSPARSE; 1179ae82921SPaul Mullowney } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported for CUSPARSE Matrix Types"); 118bc3f50f2SPaul Mullowney 119fa03d054SJed Brown ierr = MatSeqAIJSetPreallocation(*B,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr); 1203ca39a21SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)(*B),"MatFactorGetSolverType_C",MatFactorGetSolverType_seqaij_cusparse);CHKERRQ(ierr); 1219ae82921SPaul Mullowney PetscFunctionReturn(0); 1229ae82921SPaul Mullowney } 1239ae82921SPaul Mullowney 124bc3f50f2SPaul Mullowney PETSC_INTERN PetscErrorCode MatCUSPARSESetFormat_SeqAIJCUSPARSE(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format) 125ca45077fSPaul Mullowney { 126aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1276e111a19SKarl Rupp 128ca45077fSPaul Mullowney PetscFunctionBegin; 129ca45077fSPaul Mullowney switch (op) { 130e057df02SPaul Mullowney case MAT_CUSPARSE_MULT: 131aa372e3fSPaul Mullowney cusparsestruct->format = format; 132ca45077fSPaul Mullowney break; 133e057df02SPaul Mullowney case MAT_CUSPARSE_ALL: 134aa372e3fSPaul Mullowney cusparsestruct->format = format; 135ca45077fSPaul Mullowney break; 136ca45077fSPaul Mullowney default: 13736d62e41SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unsupported operation %d for MatCUSPARSEFormatOperation. MAT_CUSPARSE_MULT and MAT_CUSPARSE_ALL are currently supported.",op); 138ca45077fSPaul Mullowney } 139ca45077fSPaul Mullowney PetscFunctionReturn(0); 140ca45077fSPaul Mullowney } 1419ae82921SPaul Mullowney 142e057df02SPaul Mullowney /*@ 143e057df02SPaul Mullowney MatCUSPARSESetFormat - Sets the storage format of CUSPARSE matrices for a particular 144e057df02SPaul Mullowney operation. Only the MatMult operation can use different GPU storage formats 145aa372e3fSPaul Mullowney for MPIAIJCUSPARSE matrices. 146e057df02SPaul Mullowney Not Collective 147e057df02SPaul Mullowney 148e057df02SPaul Mullowney Input Parameters: 1498468deeeSKarl Rupp + A - Matrix of type SEQAIJCUSPARSE 15036d62e41SPaul 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. 1512692e278SPaul Mullowney - format - MatCUSPARSEStorageFormat (one of MAT_CUSPARSE_CSR, MAT_CUSPARSE_ELL, MAT_CUSPARSE_HYB. The latter two require CUDA 4.2) 152e057df02SPaul Mullowney 153e057df02SPaul Mullowney Output Parameter: 154e057df02SPaul Mullowney 155e057df02SPaul Mullowney Level: intermediate 156e057df02SPaul Mullowney 1578468deeeSKarl Rupp .seealso: MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation 158e057df02SPaul Mullowney @*/ 159e057df02SPaul Mullowney PetscErrorCode MatCUSPARSESetFormat(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format) 160e057df02SPaul Mullowney { 161e057df02SPaul Mullowney PetscErrorCode ierr; 1626e111a19SKarl Rupp 163e057df02SPaul Mullowney PetscFunctionBegin; 164e057df02SPaul Mullowney PetscValidHeaderSpecific(A, MAT_CLASSID,1); 165e057df02SPaul Mullowney ierr = PetscTryMethod(A, "MatCUSPARSESetFormat_C",(Mat,MatCUSPARSEFormatOperation,MatCUSPARSEStorageFormat),(A,op,format));CHKERRQ(ierr); 166e057df02SPaul Mullowney PetscFunctionReturn(0); 167e057df02SPaul Mullowney } 168e057df02SPaul Mullowney 1694416b707SBarry Smith static PetscErrorCode MatSetFromOptions_SeqAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat A) 1709ae82921SPaul Mullowney { 1719ae82921SPaul Mullowney PetscErrorCode ierr; 172e057df02SPaul Mullowney MatCUSPARSEStorageFormat format; 1739ae82921SPaul Mullowney PetscBool flg; 174a183c035SDominic Meiser Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1756e111a19SKarl Rupp 1769ae82921SPaul Mullowney PetscFunctionBegin; 177e55864a3SBarry Smith ierr = PetscOptionsHead(PetscOptionsObject,"SeqAIJCUSPARSE options");CHKERRQ(ierr); 1789ae82921SPaul Mullowney if (A->factortype==MAT_FACTOR_NONE) { 179e057df02SPaul Mullowney ierr = PetscOptionsEnum("-mat_cusparse_mult_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV", 180a183c035SDominic Meiser "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr); 181e057df02SPaul Mullowney if (flg) { 182e057df02SPaul Mullowney ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_MULT,format);CHKERRQ(ierr); 183045c96e1SPaul Mullowney } 1849ae82921SPaul Mullowney } 1854c87dfd4SPaul Mullowney ierr = PetscOptionsEnum("-mat_cusparse_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV and TriSolve", 186a183c035SDominic Meiser "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr); 1874c87dfd4SPaul Mullowney if (flg) { 1884c87dfd4SPaul Mullowney ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_ALL,format);CHKERRQ(ierr); 1894c87dfd4SPaul Mullowney } 1900af67c1bSStefano Zampini ierr = PetscOptionsTail();CHKERRQ(ierr); 1919ae82921SPaul Mullowney PetscFunctionReturn(0); 1929ae82921SPaul Mullowney 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; 452087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildILULowerTriMatrix(A);CHKERRQ(ierr); 453087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(A);CHKERRQ(ierr); 4542205254eSKarl Rupp 455e65717acSKarl Rupp cusparseTriFactors->workVector = new THRUSTARRAY(n); 456aa372e3fSPaul Mullowney cusparseTriFactors->nnz=a->nz; 4579ae82921SPaul Mullowney 458c70f7ee4SJunchao Zhang A->offloadmask = PETSC_OFFLOAD_BOTH; 459e057df02SPaul Mullowney /* lower triangular indices */ 4609ae82921SPaul Mullowney ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 4619ae82921SPaul Mullowney ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 4622205254eSKarl Rupp if (!row_identity) { 463aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n); 464aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices->assign(r, r+n); 4652205254eSKarl Rupp } 4669ae82921SPaul Mullowney ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 4679ae82921SPaul Mullowney 468e057df02SPaul Mullowney /* upper triangular indices */ 4699ae82921SPaul Mullowney ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); 4709ae82921SPaul Mullowney ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 4712205254eSKarl Rupp if (!col_identity) { 472aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n); 473aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices->assign(c, c+n); 4742205254eSKarl Rupp } 4754863603aSSatish Balay 4764863603aSSatish Balay if (!row_identity && !col_identity) { 4774863603aSSatish Balay ierr = PetscLogCpuToGpu(2*n*sizeof(PetscInt));CHKERRQ(ierr); 4784863603aSSatish Balay } else if(!row_identity) { 4794863603aSSatish Balay ierr = PetscLogCpuToGpu(n*sizeof(PetscInt));CHKERRQ(ierr); 4804863603aSSatish Balay } else if(!col_identity) { 4814863603aSSatish Balay ierr = PetscLogCpuToGpu(n*sizeof(PetscInt));CHKERRQ(ierr); 4824863603aSSatish Balay } 4834863603aSSatish Balay 4849ae82921SPaul Mullowney ierr = ISRestoreIndices(iscol,&c);CHKERRQ(ierr); 4859ae82921SPaul Mullowney PetscFunctionReturn(0); 4869ae82921SPaul Mullowney } 4879ae82921SPaul Mullowney 488087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildICCTriMatrices(Mat A) 489087f3262SPaul Mullowney { 490087f3262SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 491087f3262SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 492aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 493aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 494087f3262SPaul Mullowney cusparseStatus_t stat; 495087f3262SPaul Mullowney PetscErrorCode ierr; 49657d48284SJunchao Zhang cudaError_t cerr; 497087f3262SPaul Mullowney PetscInt *AiUp, *AjUp; 498087f3262SPaul Mullowney PetscScalar *AAUp; 499087f3262SPaul Mullowney PetscScalar *AALo; 500087f3262SPaul Mullowney PetscInt nzUpper = a->nz,n = A->rmap->n,i,offset,nz,j; 501087f3262SPaul Mullowney Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)A->data; 502087f3262SPaul Mullowney const PetscInt *ai = b->i,*aj = b->j,*vj; 503087f3262SPaul Mullowney const MatScalar *aa = b->a,*v; 504087f3262SPaul Mullowney 505087f3262SPaul Mullowney PetscFunctionBegin; 506cf00fe3bSKarl Rupp if (!n) PetscFunctionReturn(0); 507c70f7ee4SJunchao Zhang if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) { 508087f3262SPaul Mullowney try { 509087f3262SPaul Mullowney /* Allocate Space for the upper triangular matrix */ 51057d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(cerr); 51157d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(cerr); 51257d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(cerr); 51357d48284SJunchao Zhang cerr = cudaMallocHost((void**) &AALo, nzUpper*sizeof(PetscScalar));CHKERRCUDA(cerr); 514087f3262SPaul Mullowney 515087f3262SPaul Mullowney /* Fill the upper triangular matrix */ 516087f3262SPaul Mullowney AiUp[0]=(PetscInt) 0; 517087f3262SPaul Mullowney AiUp[n]=nzUpper; 518087f3262SPaul Mullowney offset = 0; 519087f3262SPaul Mullowney for (i=0; i<n; i++) { 520087f3262SPaul Mullowney /* set the pointers */ 521087f3262SPaul Mullowney v = aa + ai[i]; 522087f3262SPaul Mullowney vj = aj + ai[i]; 523087f3262SPaul Mullowney nz = ai[i+1] - ai[i] - 1; /* exclude diag[i] */ 524087f3262SPaul Mullowney 525087f3262SPaul Mullowney /* first, set the diagonal elements */ 526087f3262SPaul Mullowney AjUp[offset] = (PetscInt) i; 52709f51544SAlejandro Lamas Daviña AAUp[offset] = (MatScalar)1.0/v[nz]; 528087f3262SPaul Mullowney AiUp[i] = offset; 52909f51544SAlejandro Lamas Daviña AALo[offset] = (MatScalar)1.0/v[nz]; 530087f3262SPaul Mullowney 531087f3262SPaul Mullowney offset+=1; 532087f3262SPaul Mullowney if (nz>0) { 533f22e0265SBarry Smith ierr = PetscArraycpy(&(AjUp[offset]), vj, nz);CHKERRQ(ierr); 534580bdb30SBarry Smith ierr = PetscArraycpy(&(AAUp[offset]), v, nz);CHKERRQ(ierr); 535087f3262SPaul Mullowney for (j=offset; j<offset+nz; j++) { 536087f3262SPaul Mullowney AAUp[j] = -AAUp[j]; 537087f3262SPaul Mullowney AALo[j] = AAUp[j]/v[nz]; 538087f3262SPaul Mullowney } 539087f3262SPaul Mullowney offset+=nz; 540087f3262SPaul Mullowney } 541087f3262SPaul Mullowney } 542087f3262SPaul Mullowney 543aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 544aa372e3fSPaul Mullowney upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 545087f3262SPaul Mullowney 546aa372e3fSPaul Mullowney /* Create the matrix description */ 54757d48284SJunchao Zhang stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUSPARSE(stat); 54857d48284SJunchao Zhang stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat); 54957d48284SJunchao Zhang stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUSPARSE(stat); 55057d48284SJunchao Zhang stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUSPARSE(stat); 55157d48284SJunchao Zhang stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUSPARSE(stat); 552087f3262SPaul Mullowney 553aa372e3fSPaul Mullowney /* Create the solve analysis information */ 55457d48284SJunchao Zhang stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUSPARSE(stat); 555aa372e3fSPaul Mullowney 556aa372e3fSPaul Mullowney /* set the operation */ 557aa372e3fSPaul Mullowney upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 558aa372e3fSPaul Mullowney 559aa372e3fSPaul Mullowney /* set the matrix */ 560aa372e3fSPaul Mullowney upTriFactor->csrMat = new CsrMatrix; 561aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows = A->rmap->n; 562aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols = A->cmap->n; 563aa372e3fSPaul Mullowney upTriFactor->csrMat->num_entries = a->nz; 564aa372e3fSPaul Mullowney 565aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 566aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1); 567aa372e3fSPaul Mullowney 568aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz); 569aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz); 570aa372e3fSPaul Mullowney 571aa372e3fSPaul Mullowney upTriFactor->csrMat->values = new THRUSTARRAY(a->nz); 572aa372e3fSPaul Mullowney upTriFactor->csrMat->values->assign(AAUp, AAUp+a->nz); 573aa372e3fSPaul Mullowney 574aa372e3fSPaul Mullowney /* perform the solve analysis */ 575aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp, 576aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr, 577aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(), 57857d48284SJunchao Zhang upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUSPARSE(stat); 579aa372e3fSPaul Mullowney 580aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 581aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor; 582aa372e3fSPaul Mullowney 583aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 584aa372e3fSPaul Mullowney loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 585aa372e3fSPaul Mullowney 586aa372e3fSPaul Mullowney /* Create the matrix description */ 58757d48284SJunchao Zhang stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUSPARSE(stat); 58857d48284SJunchao Zhang stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat); 58957d48284SJunchao Zhang stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUSPARSE(stat); 59057d48284SJunchao Zhang stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUSPARSE(stat); 59157d48284SJunchao Zhang stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUSPARSE(stat); 592aa372e3fSPaul Mullowney 593aa372e3fSPaul Mullowney /* Create the solve analysis information */ 59457d48284SJunchao Zhang stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUSPARSE(stat); 595aa372e3fSPaul Mullowney 596aa372e3fSPaul Mullowney /* set the operation */ 597aa372e3fSPaul Mullowney loTriFactor->solveOp = CUSPARSE_OPERATION_TRANSPOSE; 598aa372e3fSPaul Mullowney 599aa372e3fSPaul Mullowney /* set the matrix */ 600aa372e3fSPaul Mullowney loTriFactor->csrMat = new CsrMatrix; 601aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows = A->rmap->n; 602aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols = A->cmap->n; 603aa372e3fSPaul Mullowney loTriFactor->csrMat->num_entries = a->nz; 604aa372e3fSPaul Mullowney 605aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 606aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1); 607aa372e3fSPaul Mullowney 608aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz); 609aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz); 610aa372e3fSPaul Mullowney 611aa372e3fSPaul Mullowney loTriFactor->csrMat->values = new THRUSTARRAY(a->nz); 612aa372e3fSPaul Mullowney loTriFactor->csrMat->values->assign(AALo, AALo+a->nz); 6134863603aSSatish Balay ierr = PetscLogCpuToGpu(2*(((A->rmap->n+1)+(a->nz))*sizeof(int)+(a->nz)*sizeof(PetscScalar)));CHKERRQ(ierr); 614aa372e3fSPaul Mullowney 615aa372e3fSPaul Mullowney /* perform the solve analysis */ 616aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp, 617aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr, 618aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(), 61957d48284SJunchao Zhang loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUSPARSE(stat); 620aa372e3fSPaul Mullowney 621aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 622aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor; 623087f3262SPaul Mullowney 624c70f7ee4SJunchao Zhang A->offloadmask = PETSC_OFFLOAD_BOTH; 62557d48284SJunchao Zhang cerr = cudaFreeHost(AiUp);CHKERRCUDA(cerr); 62657d48284SJunchao Zhang cerr = cudaFreeHost(AjUp);CHKERRCUDA(cerr); 62757d48284SJunchao Zhang cerr = cudaFreeHost(AAUp);CHKERRCUDA(cerr); 62857d48284SJunchao Zhang cerr = cudaFreeHost(AALo);CHKERRCUDA(cerr); 629087f3262SPaul Mullowney } catch(char *ex) { 630087f3262SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 631087f3262SPaul Mullowney } 632087f3262SPaul Mullowney } 633087f3262SPaul Mullowney PetscFunctionReturn(0); 634087f3262SPaul Mullowney } 635087f3262SPaul Mullowney 636087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(Mat A) 6379ae82921SPaul Mullowney { 6389ae82921SPaul Mullowney PetscErrorCode ierr; 639087f3262SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 640087f3262SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 641087f3262SPaul Mullowney IS ip = a->row; 642087f3262SPaul Mullowney const PetscInt *rip; 643087f3262SPaul Mullowney PetscBool perm_identity; 644087f3262SPaul Mullowney PetscInt n = A->rmap->n; 645087f3262SPaul Mullowney 646087f3262SPaul Mullowney PetscFunctionBegin; 647087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildICCTriMatrices(A);CHKERRQ(ierr); 648e65717acSKarl Rupp cusparseTriFactors->workVector = new THRUSTARRAY(n); 649aa372e3fSPaul Mullowney cusparseTriFactors->nnz=(a->nz-n)*2 + n; 650aa372e3fSPaul Mullowney 651087f3262SPaul Mullowney /* lower triangular indices */ 652087f3262SPaul Mullowney ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); 653087f3262SPaul Mullowney ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 654087f3262SPaul Mullowney if (!perm_identity) { 6554e4bbfaaSStefano Zampini IS iip; 6564e4bbfaaSStefano Zampini const PetscInt *irip; 6574e4bbfaaSStefano Zampini 6584e4bbfaaSStefano Zampini ierr = ISInvertPermutation(ip,PETSC_DECIDE,&iip);CHKERRQ(ierr); 6594e4bbfaaSStefano Zampini ierr = ISGetIndices(iip,&irip);CHKERRQ(ierr); 660aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n); 661aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices->assign(rip, rip+n); 662aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n); 6634e4bbfaaSStefano Zampini cusparseTriFactors->cpermIndices->assign(irip, irip+n); 6644e4bbfaaSStefano Zampini ierr = ISRestoreIndices(iip,&irip);CHKERRQ(ierr); 6654e4bbfaaSStefano Zampini ierr = ISDestroy(&iip);CHKERRQ(ierr); 6664863603aSSatish Balay ierr = PetscLogCpuToGpu(2*n*sizeof(PetscInt));CHKERRQ(ierr); 667087f3262SPaul Mullowney } 668087f3262SPaul Mullowney ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); 669087f3262SPaul Mullowney PetscFunctionReturn(0); 670087f3262SPaul Mullowney } 671087f3262SPaul Mullowney 6726fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info) 6739ae82921SPaul Mullowney { 6749ae82921SPaul Mullowney Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 6759ae82921SPaul Mullowney IS isrow = b->row,iscol = b->col; 6769ae82921SPaul Mullowney PetscBool row_identity,col_identity; 677b175d8bbSPaul Mullowney PetscErrorCode ierr; 6789ae82921SPaul Mullowney 6799ae82921SPaul Mullowney PetscFunctionBegin; 6809ae82921SPaul Mullowney ierr = MatLUFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr); 681e057df02SPaul Mullowney /* determine which version of MatSolve needs to be used. */ 6829ae82921SPaul Mullowney ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 6839ae82921SPaul Mullowney ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 684bda325fcSPaul Mullowney if (row_identity && col_identity) { 685bda325fcSPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering; 686bda325fcSPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering; 6874e4bbfaaSStefano Zampini B->ops->matsolve = NULL; 6884e4bbfaaSStefano Zampini B->ops->matsolvetranspose = NULL; 689bda325fcSPaul Mullowney } else { 690bda325fcSPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE; 691bda325fcSPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE; 6924e4bbfaaSStefano Zampini B->ops->matsolve = NULL; 6934e4bbfaaSStefano Zampini B->ops->matsolvetranspose = NULL; 694bda325fcSPaul Mullowney } 6958dc1d2a3SPaul Mullowney 696e057df02SPaul Mullowney /* get the triangular factors */ 697087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(B);CHKERRQ(ierr); 6989ae82921SPaul Mullowney PetscFunctionReturn(0); 6999ae82921SPaul Mullowney } 7009ae82921SPaul Mullowney 701087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info) 702087f3262SPaul Mullowney { 703087f3262SPaul Mullowney Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 704087f3262SPaul Mullowney IS ip = b->row; 705087f3262SPaul Mullowney PetscBool perm_identity; 706b175d8bbSPaul Mullowney PetscErrorCode ierr; 707087f3262SPaul Mullowney 708087f3262SPaul Mullowney PetscFunctionBegin; 709087f3262SPaul Mullowney ierr = MatCholeskyFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr); 710087f3262SPaul Mullowney 711087f3262SPaul Mullowney /* determine which version of MatSolve needs to be used. */ 712087f3262SPaul Mullowney ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 713087f3262SPaul Mullowney if (perm_identity) { 714087f3262SPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering; 715087f3262SPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering; 7164e4bbfaaSStefano Zampini B->ops->matsolve = NULL; 7174e4bbfaaSStefano Zampini B->ops->matsolvetranspose = NULL; 718087f3262SPaul Mullowney } else { 719087f3262SPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE; 720087f3262SPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE; 7214e4bbfaaSStefano Zampini B->ops->matsolve = NULL; 7224e4bbfaaSStefano Zampini B->ops->matsolvetranspose = NULL; 723087f3262SPaul Mullowney } 724087f3262SPaul Mullowney 725087f3262SPaul Mullowney /* get the triangular factors */ 726087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(B);CHKERRQ(ierr); 727087f3262SPaul Mullowney PetscFunctionReturn(0); 728087f3262SPaul Mullowney } 7299ae82921SPaul Mullowney 730b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(Mat A) 731bda325fcSPaul Mullowney { 732bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 733aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 734aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 735aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 736aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 737bda325fcSPaul Mullowney cusparseStatus_t stat; 738aa372e3fSPaul Mullowney cusparseIndexBase_t indexBase; 739aa372e3fSPaul Mullowney cusparseMatrixType_t matrixType; 740aa372e3fSPaul Mullowney cusparseFillMode_t fillMode; 741aa372e3fSPaul Mullowney cusparseDiagType_t diagType; 742b175d8bbSPaul Mullowney 743bda325fcSPaul Mullowney PetscFunctionBegin; 744bda325fcSPaul Mullowney 745aa372e3fSPaul Mullowney /*********************************************/ 746aa372e3fSPaul Mullowney /* Now the Transpose of the Lower Tri Factor */ 747aa372e3fSPaul Mullowney /*********************************************/ 748aa372e3fSPaul Mullowney 749aa372e3fSPaul Mullowney /* allocate space for the transpose of the lower triangular factor */ 750aa372e3fSPaul Mullowney loTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct; 751aa372e3fSPaul Mullowney 752aa372e3fSPaul Mullowney /* set the matrix descriptors of the lower triangular factor */ 753aa372e3fSPaul Mullowney matrixType = cusparseGetMatType(loTriFactor->descr); 754aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(loTriFactor->descr); 755aa372e3fSPaul Mullowney fillMode = cusparseGetMatFillMode(loTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ? 756aa372e3fSPaul Mullowney CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER; 757aa372e3fSPaul Mullowney diagType = cusparseGetMatDiagType(loTriFactor->descr); 758aa372e3fSPaul Mullowney 759aa372e3fSPaul Mullowney /* Create the matrix description */ 76057d48284SJunchao Zhang stat = cusparseCreateMatDescr(&loTriFactorT->descr);CHKERRCUSPARSE(stat); 76157d48284SJunchao Zhang stat = cusparseSetMatIndexBase(loTriFactorT->descr, indexBase);CHKERRCUSPARSE(stat); 76257d48284SJunchao Zhang stat = cusparseSetMatType(loTriFactorT->descr, matrixType);CHKERRCUSPARSE(stat); 76357d48284SJunchao Zhang stat = cusparseSetMatFillMode(loTriFactorT->descr, fillMode);CHKERRCUSPARSE(stat); 76457d48284SJunchao Zhang stat = cusparseSetMatDiagType(loTriFactorT->descr, diagType);CHKERRCUSPARSE(stat); 765aa372e3fSPaul Mullowney 766aa372e3fSPaul Mullowney /* Create the solve analysis information */ 76757d48284SJunchao Zhang stat = cusparseCreateSolveAnalysisInfo(&loTriFactorT->solveInfo);CHKERRCUSPARSE(stat); 768aa372e3fSPaul Mullowney 769aa372e3fSPaul Mullowney /* set the operation */ 770aa372e3fSPaul Mullowney loTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 771aa372e3fSPaul Mullowney 772aa372e3fSPaul Mullowney /* allocate GPU space for the CSC of the lower triangular factor*/ 773aa372e3fSPaul Mullowney loTriFactorT->csrMat = new CsrMatrix; 774aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_rows = loTriFactor->csrMat->num_rows; 775aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_cols = loTriFactor->csrMat->num_cols; 776aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_entries = loTriFactor->csrMat->num_entries; 777aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(loTriFactor->csrMat->num_rows+1); 778aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(loTriFactor->csrMat->num_entries); 779aa372e3fSPaul Mullowney loTriFactorT->csrMat->values = new THRUSTARRAY(loTriFactor->csrMat->num_entries); 780aa372e3fSPaul Mullowney 781aa372e3fSPaul Mullowney /* compute the transpose of the lower triangular factor, i.e. the CSC */ 782aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparseTriFactors->handle, loTriFactor->csrMat->num_rows, 783aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols, loTriFactor->csrMat->num_entries, 784aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 785aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 786aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 787aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 788aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 789aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 79057d48284SJunchao Zhang CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUSPARSE(stat); 791aa372e3fSPaul Mullowney 792aa372e3fSPaul Mullowney /* perform the solve analysis on the transposed matrix */ 793aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactorT->solveOp, 794aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_rows, loTriFactorT->csrMat->num_entries, 795aa372e3fSPaul Mullowney loTriFactorT->descr, loTriFactorT->csrMat->values->data().get(), 796aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), loTriFactorT->csrMat->column_indices->data().get(), 79757d48284SJunchao Zhang loTriFactorT->solveInfo);CHKERRCUSPARSE(stat); 798aa372e3fSPaul Mullowney 799aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 800aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtrTranspose = loTriFactorT; 801aa372e3fSPaul Mullowney 802aa372e3fSPaul Mullowney /*********************************************/ 803aa372e3fSPaul Mullowney /* Now the Transpose of the Upper Tri Factor */ 804aa372e3fSPaul Mullowney /*********************************************/ 805aa372e3fSPaul Mullowney 806aa372e3fSPaul Mullowney /* allocate space for the transpose of the upper triangular factor */ 807aa372e3fSPaul Mullowney upTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct; 808aa372e3fSPaul Mullowney 809aa372e3fSPaul Mullowney /* set the matrix descriptors of the upper triangular factor */ 810aa372e3fSPaul Mullowney matrixType = cusparseGetMatType(upTriFactor->descr); 811aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(upTriFactor->descr); 812aa372e3fSPaul Mullowney fillMode = cusparseGetMatFillMode(upTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ? 813aa372e3fSPaul Mullowney CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER; 814aa372e3fSPaul Mullowney diagType = cusparseGetMatDiagType(upTriFactor->descr); 815aa372e3fSPaul Mullowney 816aa372e3fSPaul Mullowney /* Create the matrix description */ 81757d48284SJunchao Zhang stat = cusparseCreateMatDescr(&upTriFactorT->descr);CHKERRCUSPARSE(stat); 81857d48284SJunchao Zhang stat = cusparseSetMatIndexBase(upTriFactorT->descr, indexBase);CHKERRCUSPARSE(stat); 81957d48284SJunchao Zhang stat = cusparseSetMatType(upTriFactorT->descr, matrixType);CHKERRCUSPARSE(stat); 82057d48284SJunchao Zhang stat = cusparseSetMatFillMode(upTriFactorT->descr, fillMode);CHKERRCUSPARSE(stat); 82157d48284SJunchao Zhang stat = cusparseSetMatDiagType(upTriFactorT->descr, diagType);CHKERRCUSPARSE(stat); 822aa372e3fSPaul Mullowney 823aa372e3fSPaul Mullowney /* Create the solve analysis information */ 82457d48284SJunchao Zhang stat = cusparseCreateSolveAnalysisInfo(&upTriFactorT->solveInfo);CHKERRCUSPARSE(stat); 825aa372e3fSPaul Mullowney 826aa372e3fSPaul Mullowney /* set the operation */ 827aa372e3fSPaul Mullowney upTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 828aa372e3fSPaul Mullowney 829aa372e3fSPaul Mullowney /* allocate GPU space for the CSC of the upper triangular factor*/ 830aa372e3fSPaul Mullowney upTriFactorT->csrMat = new CsrMatrix; 831aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_rows = upTriFactor->csrMat->num_rows; 832aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_cols = upTriFactor->csrMat->num_cols; 833aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_entries = upTriFactor->csrMat->num_entries; 834aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(upTriFactor->csrMat->num_rows+1); 835aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(upTriFactor->csrMat->num_entries); 836aa372e3fSPaul Mullowney upTriFactorT->csrMat->values = new THRUSTARRAY(upTriFactor->csrMat->num_entries); 837aa372e3fSPaul Mullowney 838aa372e3fSPaul Mullowney /* compute the transpose of the upper triangular factor, i.e. the CSC */ 839aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparseTriFactors->handle, upTriFactor->csrMat->num_rows, 840aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols, upTriFactor->csrMat->num_entries, 841aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 842aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 843aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 844aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 845aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 846aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 84757d48284SJunchao Zhang CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUSPARSE(stat); 848aa372e3fSPaul Mullowney 849aa372e3fSPaul Mullowney /* perform the solve analysis on the transposed matrix */ 850aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactorT->solveOp, 851aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_rows, upTriFactorT->csrMat->num_entries, 852aa372e3fSPaul Mullowney upTriFactorT->descr, upTriFactorT->csrMat->values->data().get(), 853aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), upTriFactorT->csrMat->column_indices->data().get(), 85457d48284SJunchao Zhang upTriFactorT->solveInfo);CHKERRCUSPARSE(stat); 855aa372e3fSPaul Mullowney 856aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 857aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtrTranspose = upTriFactorT; 858bda325fcSPaul Mullowney PetscFunctionReturn(0); 859bda325fcSPaul Mullowney } 860bda325fcSPaul Mullowney 861b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEGenerateTransposeForMult(Mat A) 862bda325fcSPaul Mullowney { 863aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 864aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 865aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 866bda325fcSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 867bda325fcSPaul Mullowney cusparseStatus_t stat; 868aa372e3fSPaul Mullowney cusparseIndexBase_t indexBase; 869b06137fdSPaul Mullowney cudaError_t err; 8704863603aSSatish Balay PetscErrorCode ierr; 871b175d8bbSPaul Mullowney 872bda325fcSPaul Mullowney PetscFunctionBegin; 873aa372e3fSPaul Mullowney 874aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 875aa372e3fSPaul Mullowney matstructT = new Mat_SeqAIJCUSPARSEMultStruct; 87657d48284SJunchao Zhang stat = cusparseCreateMatDescr(&matstructT->descr);CHKERRCUSPARSE(stat); 877aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(matstruct->descr); 87857d48284SJunchao Zhang stat = cusparseSetMatIndexBase(matstructT->descr, indexBase);CHKERRCUSPARSE(stat); 87957d48284SJunchao Zhang stat = cusparseSetMatType(matstructT->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUSPARSE(stat); 880aa372e3fSPaul Mullowney 881b06137fdSPaul Mullowney /* set alpha and beta */ 882c41cb2e2SAlejandro Lamas Daviña err = cudaMalloc((void **)&(matstructT->alpha), sizeof(PetscScalar));CHKERRCUDA(err); 8837656d835SStefano Zampini err = cudaMalloc((void **)&(matstructT->beta_zero),sizeof(PetscScalar));CHKERRCUDA(err); 8847656d835SStefano Zampini err = cudaMalloc((void **)&(matstructT->beta_one), sizeof(PetscScalar));CHKERRCUDA(err); 8857656d835SStefano Zampini err = cudaMemcpy(matstructT->alpha, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 8867656d835SStefano Zampini err = cudaMemcpy(matstructT->beta_zero,&PETSC_CUSPARSE_ZERO,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 8877656d835SStefano Zampini err = cudaMemcpy(matstructT->beta_one, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 88857d48284SJunchao Zhang stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUSPARSE(stat); 889b06137fdSPaul Mullowney 890aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 891aa372e3fSPaul Mullowney CsrMatrix *matrix = (CsrMatrix*)matstruct->mat; 892aa372e3fSPaul Mullowney CsrMatrix *matrixT= new CsrMatrix; 893a3fdcf43SKarl Rupp thrust::device_vector<int> *rowoffsets_gpu; 894a3fdcf43SKarl Rupp const int ncomprow = matstruct->cprowIndices->size(); 895a3fdcf43SKarl Rupp thrust::host_vector<int> cprow(ncomprow); 896a3fdcf43SKarl Rupp cprow = *matstruct->cprowIndices; // GPU --> CPU 897554b8892SKarl Rupp matrixT->num_rows = A->cmap->n; 898554b8892SKarl Rupp matrixT->num_cols = A->rmap->n; 899aa372e3fSPaul Mullowney matrixT->num_entries = a->nz; 900a8bd5306SMark Adams matrixT->row_offsets = new THRUSTINTARRAY32(matrixT->num_rows+1); 901aa372e3fSPaul Mullowney matrixT->column_indices = new THRUSTINTARRAY32(a->nz); 902aa372e3fSPaul Mullowney matrixT->values = new THRUSTARRAY(a->nz); 903a3fdcf43SKarl Rupp PetscInt k,i; 904a3fdcf43SKarl Rupp thrust::host_vector<int> rowst_host(A->rmap->n+1); 905a3fdcf43SKarl Rupp 906a3fdcf43SKarl Rupp /* expand compress rows, which is forced in constructor (MatSeqAIJCUSPARSECopyToGPU) */ 907a3fdcf43SKarl Rupp rowst_host[0] = 0; 908a3fdcf43SKarl Rupp for (k = 0, i = 0; i < A->rmap->n ; i++) { 909a3fdcf43SKarl Rupp if (k < ncomprow && i==cprow[k]) { 910a3fdcf43SKarl Rupp rowst_host[i+1] = a->i[i+1]; 911a3fdcf43SKarl Rupp k++; 912a3fdcf43SKarl Rupp } else rowst_host[i+1] = rowst_host[i]; 913a3fdcf43SKarl Rupp } 914a3fdcf43SKarl Rupp if (k!=ncomprow) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"%D != %D",k,ncomprow); 915a3fdcf43SKarl Rupp rowoffsets_gpu = new THRUSTINTARRAY32(A->rmap->n+1); 916a3fdcf43SKarl Rupp *rowoffsets_gpu = rowst_host; // CPU --> GPU 917aa372e3fSPaul Mullowney 918aa372e3fSPaul Mullowney /* compute the transpose of the upper triangular factor, i.e. the CSC */ 919aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(matstruct->descr); 920a3fdcf43SKarl Rupp stat = cusparse_csr2csc(cusparsestruct->handle, A->rmap->n, 921a3fdcf43SKarl Rupp A->cmap->n, matrix->num_entries, 922aa372e3fSPaul Mullowney matrix->values->data().get(), 923a3fdcf43SKarl Rupp rowoffsets_gpu->data().get(), 924aa372e3fSPaul Mullowney matrix->column_indices->data().get(), 925aa372e3fSPaul Mullowney matrixT->values->data().get(), 926aa372e3fSPaul Mullowney matrixT->column_indices->data().get(), 927aa372e3fSPaul Mullowney matrixT->row_offsets->data().get(), 92857d48284SJunchao Zhang CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUSPARSE(stat); 929aa372e3fSPaul Mullowney 930aa372e3fSPaul Mullowney /* assign the pointer */ 931aa372e3fSPaul Mullowney matstructT->mat = matrixT; 9324863603aSSatish Balay ierr = PetscLogCpuToGpu(((A->rmap->n+1)+(a->nz))*sizeof(int)+(3+a->nz)*sizeof(PetscScalar));CHKERRQ(ierr); 933aa372e3fSPaul Mullowney } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) { 934aa372e3fSPaul Mullowney /* First convert HYB to CSR */ 935aa372e3fSPaul Mullowney CsrMatrix *temp= new CsrMatrix; 936aa372e3fSPaul Mullowney temp->num_rows = A->rmap->n; 937aa372e3fSPaul Mullowney temp->num_cols = A->cmap->n; 938aa372e3fSPaul Mullowney temp->num_entries = a->nz; 939aa372e3fSPaul Mullowney temp->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 940aa372e3fSPaul Mullowney temp->column_indices = new THRUSTINTARRAY32(a->nz); 941aa372e3fSPaul Mullowney temp->values = new THRUSTARRAY(a->nz); 942aa372e3fSPaul Mullowney 9432692e278SPaul Mullowney 944aa372e3fSPaul Mullowney stat = cusparse_hyb2csr(cusparsestruct->handle, 945aa372e3fSPaul Mullowney matstruct->descr, (cusparseHybMat_t)matstruct->mat, 946aa372e3fSPaul Mullowney temp->values->data().get(), 947aa372e3fSPaul Mullowney temp->row_offsets->data().get(), 94857d48284SJunchao Zhang temp->column_indices->data().get());CHKERRCUSPARSE(stat); 949aa372e3fSPaul Mullowney 950aa372e3fSPaul Mullowney /* Next, convert CSR to CSC (i.e. the matrix transpose) */ 951aa372e3fSPaul Mullowney CsrMatrix *tempT= new CsrMatrix; 952aa372e3fSPaul Mullowney tempT->num_rows = A->rmap->n; 953aa372e3fSPaul Mullowney tempT->num_cols = A->cmap->n; 954aa372e3fSPaul Mullowney tempT->num_entries = a->nz; 955aa372e3fSPaul Mullowney tempT->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 956aa372e3fSPaul Mullowney tempT->column_indices = new THRUSTINTARRAY32(a->nz); 957aa372e3fSPaul Mullowney tempT->values = new THRUSTARRAY(a->nz); 958aa372e3fSPaul Mullowney 959aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparsestruct->handle, temp->num_rows, 960aa372e3fSPaul Mullowney temp->num_cols, temp->num_entries, 961aa372e3fSPaul Mullowney temp->values->data().get(), 962aa372e3fSPaul Mullowney temp->row_offsets->data().get(), 963aa372e3fSPaul Mullowney temp->column_indices->data().get(), 964aa372e3fSPaul Mullowney tempT->values->data().get(), 965aa372e3fSPaul Mullowney tempT->column_indices->data().get(), 966aa372e3fSPaul Mullowney tempT->row_offsets->data().get(), 96757d48284SJunchao Zhang CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUSPARSE(stat); 968aa372e3fSPaul Mullowney 969aa372e3fSPaul Mullowney /* Last, convert CSC to HYB */ 970aa372e3fSPaul Mullowney cusparseHybMat_t hybMat; 97157d48284SJunchao Zhang stat = cusparseCreateHybMat(&hybMat);CHKERRCUSPARSE(stat); 972aa372e3fSPaul Mullowney cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ? 973aa372e3fSPaul Mullowney CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO; 974aa372e3fSPaul Mullowney stat = cusparse_csr2hyb(cusparsestruct->handle, A->rmap->n, A->cmap->n, 975aa372e3fSPaul Mullowney matstructT->descr, tempT->values->data().get(), 976aa372e3fSPaul Mullowney tempT->row_offsets->data().get(), 977aa372e3fSPaul Mullowney tempT->column_indices->data().get(), 97857d48284SJunchao Zhang hybMat, 0, partition);CHKERRCUSPARSE(stat); 979aa372e3fSPaul Mullowney 980aa372e3fSPaul Mullowney /* assign the pointer */ 981aa372e3fSPaul Mullowney matstructT->mat = hybMat; 9824863603aSSatish Balay ierr = PetscLogCpuToGpu((2*(((A->rmap->n+1)+(a->nz))*sizeof(int)+(a->nz)*sizeof(PetscScalar)))+3*sizeof(PetscScalar));CHKERRQ(ierr); 983aa372e3fSPaul Mullowney 984aa372e3fSPaul Mullowney /* delete temporaries */ 985aa372e3fSPaul Mullowney if (tempT) { 986aa372e3fSPaul Mullowney if (tempT->values) delete (THRUSTARRAY*) tempT->values; 987aa372e3fSPaul Mullowney if (tempT->column_indices) delete (THRUSTINTARRAY32*) tempT->column_indices; 988aa372e3fSPaul Mullowney if (tempT->row_offsets) delete (THRUSTINTARRAY32*) tempT->row_offsets; 989aa372e3fSPaul Mullowney delete (CsrMatrix*) tempT; 990087f3262SPaul Mullowney } 991aa372e3fSPaul Mullowney if (temp) { 992aa372e3fSPaul Mullowney if (temp->values) delete (THRUSTARRAY*) temp->values; 993aa372e3fSPaul Mullowney if (temp->column_indices) delete (THRUSTINTARRAY32*) temp->column_indices; 994aa372e3fSPaul Mullowney if (temp->row_offsets) delete (THRUSTINTARRAY32*) temp->row_offsets; 995aa372e3fSPaul Mullowney delete (CsrMatrix*) temp; 996aa372e3fSPaul Mullowney } 997aa372e3fSPaul Mullowney } 998aa372e3fSPaul Mullowney /* assign the compressed row indices */ 999aa372e3fSPaul Mullowney matstructT->cprowIndices = new THRUSTINTARRAY; 1000554b8892SKarl Rupp matstructT->cprowIndices->resize(A->cmap->n); 1001554b8892SKarl Rupp thrust::sequence(matstructT->cprowIndices->begin(), matstructT->cprowIndices->end()); 1002aa372e3fSPaul Mullowney /* assign the pointer */ 1003aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)A->spptr)->matTranspose = matstructT; 1004bda325fcSPaul Mullowney PetscFunctionReturn(0); 1005bda325fcSPaul Mullowney } 1006bda325fcSPaul Mullowney 10074e4bbfaaSStefano Zampini /* Why do we need to analyze the tranposed matrix again? Can't we just use op(A) = CUSPARSE_OPERATION_TRANSPOSE in MatSolve_SeqAIJCUSPARSE? */ 10086fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx) 1009bda325fcSPaul Mullowney { 1010c41cb2e2SAlejandro Lamas Daviña PetscInt n = xx->map->n; 1011465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1012465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 1013465f34aeSAlejandro Lamas Daviña thrust::device_ptr<const PetscScalar> bGPU; 1014465f34aeSAlejandro Lamas Daviña thrust::device_ptr<PetscScalar> xGPU; 1015bda325fcSPaul Mullowney cusparseStatus_t stat; 1016bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1017aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1018aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1019aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1020b175d8bbSPaul Mullowney PetscErrorCode ierr; 102157d48284SJunchao Zhang cudaError_t cerr; 1022bda325fcSPaul Mullowney 1023bda325fcSPaul Mullowney PetscFunctionBegin; 1024aa372e3fSPaul Mullowney /* Analyze the matrix and create the transpose ... on the fly */ 1025aa372e3fSPaul Mullowney if (!loTriFactorT && !upTriFactorT) { 1026bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr); 1027aa372e3fSPaul Mullowney loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1028aa372e3fSPaul Mullowney upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1029bda325fcSPaul Mullowney } 1030bda325fcSPaul Mullowney 1031bda325fcSPaul Mullowney /* Get the GPU pointers */ 1032c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1033c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1034c41cb2e2SAlejandro Lamas Daviña xGPU = thrust::device_pointer_cast(xarray); 1035c41cb2e2SAlejandro Lamas Daviña bGPU = thrust::device_pointer_cast(barray); 1036bda325fcSPaul Mullowney 10377a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1038aa372e3fSPaul Mullowney /* First, reorder with the row permutation */ 1039c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()), 1040c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(bGPU+n, cusparseTriFactors->rpermIndices->end()), 1041c41cb2e2SAlejandro Lamas Daviña xGPU); 1042aa372e3fSPaul Mullowney 1043aa372e3fSPaul Mullowney /* First, solve U */ 1044aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp, 10457656d835SStefano Zampini upTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactorT->descr, 1046aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 1047aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 1048aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 1049aa372e3fSPaul Mullowney upTriFactorT->solveInfo, 105057d48284SJunchao Zhang xarray, tempGPU->data().get());CHKERRCUSPARSE(stat); 1051aa372e3fSPaul Mullowney 1052aa372e3fSPaul Mullowney /* Then, solve L */ 1053aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp, 10547656d835SStefano Zampini loTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactorT->descr, 1055aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 1056aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 1057aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 1058aa372e3fSPaul Mullowney loTriFactorT->solveInfo, 105957d48284SJunchao Zhang tempGPU->data().get(), xarray);CHKERRCUSPARSE(stat); 1060aa372e3fSPaul Mullowney 1061aa372e3fSPaul Mullowney /* Last, copy the solution, xGPU, into a temporary with the column permutation ... can't be done in place. */ 1062c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->begin()), 1063c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(xGPU+n, cusparseTriFactors->cpermIndices->end()), 1064aa372e3fSPaul Mullowney tempGPU->begin()); 1065aa372e3fSPaul Mullowney 1066aa372e3fSPaul Mullowney /* Copy the temporary to the full solution. */ 1067c41cb2e2SAlejandro Lamas Daviña thrust::copy(tempGPU->begin(), tempGPU->end(), xGPU); 1068bda325fcSPaul Mullowney 1069bda325fcSPaul Mullowney /* restore */ 1070c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1071c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 107257d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); 1073661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1074958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 1075bda325fcSPaul Mullowney PetscFunctionReturn(0); 1076bda325fcSPaul Mullowney } 1077bda325fcSPaul Mullowney 10786fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx) 1079bda325fcSPaul Mullowney { 1080465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1081465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 1082bda325fcSPaul Mullowney cusparseStatus_t stat; 1083bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1084aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1085aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1086aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1087b175d8bbSPaul Mullowney PetscErrorCode ierr; 108857d48284SJunchao Zhang cudaError_t cerr; 1089bda325fcSPaul Mullowney 1090bda325fcSPaul Mullowney PetscFunctionBegin; 1091aa372e3fSPaul Mullowney /* Analyze the matrix and create the transpose ... on the fly */ 1092aa372e3fSPaul Mullowney if (!loTriFactorT && !upTriFactorT) { 1093bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr); 1094aa372e3fSPaul Mullowney loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1095aa372e3fSPaul Mullowney upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1096bda325fcSPaul Mullowney } 1097bda325fcSPaul Mullowney 1098bda325fcSPaul Mullowney /* Get the GPU pointers */ 1099c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1100c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1101bda325fcSPaul Mullowney 11027a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1103aa372e3fSPaul Mullowney /* First, solve U */ 1104aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp, 11057656d835SStefano Zampini upTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactorT->descr, 1106aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 1107aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 1108aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 1109aa372e3fSPaul Mullowney upTriFactorT->solveInfo, 111057d48284SJunchao Zhang barray, tempGPU->data().get());CHKERRCUSPARSE(stat); 1111aa372e3fSPaul Mullowney 1112aa372e3fSPaul Mullowney /* Then, solve L */ 1113aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp, 11147656d835SStefano Zampini loTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactorT->descr, 1115aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 1116aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 1117aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 1118aa372e3fSPaul Mullowney loTriFactorT->solveInfo, 111957d48284SJunchao Zhang tempGPU->data().get(), xarray);CHKERRCUSPARSE(stat); 1120bda325fcSPaul Mullowney 1121bda325fcSPaul Mullowney /* restore */ 1122c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1123c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 112457d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); 1125661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1126958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 1127bda325fcSPaul Mullowney PetscFunctionReturn(0); 1128bda325fcSPaul Mullowney } 1129bda325fcSPaul Mullowney 11306fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx) 11319ae82921SPaul Mullowney { 1132465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1133465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 1134465f34aeSAlejandro Lamas Daviña thrust::device_ptr<const PetscScalar> bGPU; 1135465f34aeSAlejandro Lamas Daviña thrust::device_ptr<PetscScalar> xGPU; 11369ae82921SPaul Mullowney cusparseStatus_t stat; 11379ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1138aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 1139aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 1140aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1141b175d8bbSPaul Mullowney PetscErrorCode ierr; 114257d48284SJunchao Zhang cudaError_t cerr; 11439ae82921SPaul Mullowney 11449ae82921SPaul Mullowney PetscFunctionBegin; 1145ebc8f436SDominic Meiser 1146e057df02SPaul Mullowney /* Get the GPU pointers */ 1147c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1148c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1149c41cb2e2SAlejandro Lamas Daviña xGPU = thrust::device_pointer_cast(xarray); 1150c41cb2e2SAlejandro Lamas Daviña bGPU = thrust::device_pointer_cast(barray); 11519ae82921SPaul Mullowney 11527a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1153aa372e3fSPaul Mullowney /* First, reorder with the row permutation */ 1154c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()), 1155c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->end()), 11564e4bbfaaSStefano Zampini tempGPU->begin()); 1157aa372e3fSPaul Mullowney 1158aa372e3fSPaul Mullowney /* Next, solve L */ 1159aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp, 11607656d835SStefano Zampini loTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactor->descr, 1161aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 1162aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 1163aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 1164aa372e3fSPaul Mullowney loTriFactor->solveInfo, 116557d48284SJunchao Zhang tempGPU->data().get(), xarray);CHKERRCUSPARSE(stat); 1166aa372e3fSPaul Mullowney 1167aa372e3fSPaul Mullowney /* Then, solve U */ 1168aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp, 11697656d835SStefano Zampini upTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactor->descr, 1170aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 1171aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 1172aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 1173aa372e3fSPaul Mullowney upTriFactor->solveInfo, 117457d48284SJunchao Zhang xarray, tempGPU->data().get());CHKERRCUSPARSE(stat); 1175aa372e3fSPaul Mullowney 11764e4bbfaaSStefano Zampini /* Last, reorder with the column permutation */ 11774e4bbfaaSStefano Zampini thrust::copy(thrust::make_permutation_iterator(tempGPU->begin(), cusparseTriFactors->cpermIndices->begin()), 11784e4bbfaaSStefano Zampini thrust::make_permutation_iterator(tempGPU->begin(), cusparseTriFactors->cpermIndices->end()), 11794e4bbfaaSStefano Zampini xGPU); 11809ae82921SPaul Mullowney 1181c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1182c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 118357d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); 1184661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1185958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 11869ae82921SPaul Mullowney PetscFunctionReturn(0); 11879ae82921SPaul Mullowney } 11889ae82921SPaul Mullowney 11896fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx) 11909ae82921SPaul Mullowney { 1191465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1192465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 11939ae82921SPaul Mullowney cusparseStatus_t stat; 11949ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1195aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 1196aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 1197aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1198b175d8bbSPaul Mullowney PetscErrorCode ierr; 119957d48284SJunchao Zhang cudaError_t cerr; 12009ae82921SPaul Mullowney 12019ae82921SPaul Mullowney PetscFunctionBegin; 1202e057df02SPaul Mullowney /* Get the GPU pointers */ 1203c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1204c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 12059ae82921SPaul Mullowney 12067a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1207aa372e3fSPaul Mullowney /* First, solve L */ 1208aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp, 12097656d835SStefano Zampini loTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactor->descr, 1210aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 1211aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 1212aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 1213aa372e3fSPaul Mullowney loTriFactor->solveInfo, 121457d48284SJunchao Zhang barray, tempGPU->data().get());CHKERRCUSPARSE(stat); 1215aa372e3fSPaul Mullowney 1216aa372e3fSPaul Mullowney /* Next, solve U */ 1217aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp, 12187656d835SStefano Zampini upTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactor->descr, 1219aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 1220aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 1221aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 1222aa372e3fSPaul Mullowney upTriFactor->solveInfo, 122357d48284SJunchao Zhang tempGPU->data().get(), xarray);CHKERRCUSPARSE(stat); 12249ae82921SPaul Mullowney 1225c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1226c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 122757d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); 1228661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1229958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 12309ae82921SPaul Mullowney PetscFunctionReturn(0); 12319ae82921SPaul Mullowney } 12329ae82921SPaul Mullowney 12336fa9248bSJed Brown static PetscErrorCode MatSeqAIJCUSPARSECopyToGPU(Mat A) 12349ae82921SPaul Mullowney { 1235aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1236aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 12379ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 12389ae82921SPaul Mullowney PetscInt m = A->rmap->n,*ii,*ridx; 12399ae82921SPaul Mullowney PetscErrorCode ierr; 1240aa372e3fSPaul Mullowney cusparseStatus_t stat; 1241b06137fdSPaul Mullowney cudaError_t err; 12429ae82921SPaul Mullowney 12439ae82921SPaul Mullowney PetscFunctionBegin; 1244*95639643SRichard Tran Mills if (A->boundtocpu) PetscFunctionReturn(0); 1245c70f7ee4SJunchao Zhang if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) { 12469ae82921SPaul Mullowney ierr = PetscLogEventBegin(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr); 124734d6c7a5SJose E. Roman if (A->assembled && A->nonzerostate == cusparsestruct->nonzerostate && cusparsestruct->format == MAT_CUSPARSE_CSR) { 124834d6c7a5SJose E. Roman CsrMatrix *matrix = (CsrMatrix*)matstruct->mat; 124934d6c7a5SJose E. Roman /* copy values only */ 125034d6c7a5SJose E. Roman matrix->values->assign(a->a, a->a+a->nz); 12514863603aSSatish Balay ierr = PetscLogCpuToGpu((a->nz)*sizeof(PetscScalar));CHKERRQ(ierr); 125234d6c7a5SJose E. Roman } else { 1253470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&matstruct,cusparsestruct->format); 12549ae82921SPaul Mullowney try { 1255aa372e3fSPaul Mullowney cusparsestruct->nonzerorow=0; 1256aa372e3fSPaul Mullowney for (int j = 0; j<m; j++) cusparsestruct->nonzerorow += ((a->i[j+1]-a->i[j])>0); 12579ae82921SPaul Mullowney 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 { 1263b06137fdSPaul Mullowney /* Forcing compressed row on the GPU */ 12649ae82921SPaul Mullowney int k=0; 1265854ce69bSBarry Smith ierr = PetscMalloc1(cusparsestruct->nonzerorow+1, &ii);CHKERRQ(ierr); 1266854ce69bSBarry Smith ierr = PetscMalloc1(cusparsestruct->nonzerorow, &ridx);CHKERRQ(ierr); 12679ae82921SPaul Mullowney ii[0]=0; 12689ae82921SPaul Mullowney for (int j = 0; j<m; j++) { 12699ae82921SPaul Mullowney if ((a->i[j+1]-a->i[j])>0) { 12709ae82921SPaul Mullowney ii[k] = a->i[j]; 12719ae82921SPaul Mullowney ridx[k]= j; 12729ae82921SPaul Mullowney k++; 12739ae82921SPaul Mullowney } 12749ae82921SPaul Mullowney } 1275aa372e3fSPaul Mullowney ii[cusparsestruct->nonzerorow] = a->nz; 1276aa372e3fSPaul Mullowney m = cusparsestruct->nonzerorow; 12779ae82921SPaul Mullowney } 12789ae82921SPaul Mullowney 1279aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 1280aa372e3fSPaul Mullowney matstruct = new Mat_SeqAIJCUSPARSEMultStruct; 128157d48284SJunchao Zhang stat = cusparseCreateMatDescr(&matstruct->descr);CHKERRCUSPARSE(stat); 128257d48284SJunchao Zhang stat = cusparseSetMatIndexBase(matstruct->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat); 128357d48284SJunchao Zhang stat = cusparseSetMatType(matstruct->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUSPARSE(stat); 12849ae82921SPaul Mullowney 1285c41cb2e2SAlejandro Lamas Daviña err = cudaMalloc((void **)&(matstruct->alpha), sizeof(PetscScalar));CHKERRCUDA(err); 12867656d835SStefano Zampini err = cudaMalloc((void **)&(matstruct->beta_zero),sizeof(PetscScalar));CHKERRCUDA(err); 12877656d835SStefano Zampini err = cudaMalloc((void **)&(matstruct->beta_one), sizeof(PetscScalar));CHKERRCUDA(err); 12887656d835SStefano Zampini err = cudaMemcpy(matstruct->alpha, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 12897656d835SStefano Zampini err = cudaMemcpy(matstruct->beta_zero,&PETSC_CUSPARSE_ZERO,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 12907656d835SStefano Zampini err = cudaMemcpy(matstruct->beta_one, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 129157d48284SJunchao Zhang stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUSPARSE(stat); 1292b06137fdSPaul Mullowney 1293aa372e3fSPaul Mullowney /* Build a hybrid/ellpack matrix if this option is chosen for the storage */ 1294aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1295aa372e3fSPaul Mullowney /* set the matrix */ 1296aa372e3fSPaul Mullowney CsrMatrix *matrix= new CsrMatrix; 1297a65300a6SPaul Mullowney matrix->num_rows = m; 1298aa372e3fSPaul Mullowney matrix->num_cols = A->cmap->n; 1299aa372e3fSPaul Mullowney matrix->num_entries = a->nz; 1300a65300a6SPaul Mullowney matrix->row_offsets = new THRUSTINTARRAY32(m+1); 1301a65300a6SPaul Mullowney matrix->row_offsets->assign(ii, ii + m+1); 13029ae82921SPaul Mullowney 1303aa372e3fSPaul Mullowney matrix->column_indices = new THRUSTINTARRAY32(a->nz); 1304aa372e3fSPaul Mullowney matrix->column_indices->assign(a->j, a->j+a->nz); 1305aa372e3fSPaul Mullowney 1306aa372e3fSPaul Mullowney matrix->values = new THRUSTARRAY(a->nz); 1307aa372e3fSPaul Mullowney matrix->values->assign(a->a, a->a+a->nz); 1308aa372e3fSPaul Mullowney 1309aa372e3fSPaul Mullowney /* assign the pointer */ 1310aa372e3fSPaul Mullowney matstruct->mat = matrix; 1311aa372e3fSPaul Mullowney 1312aa372e3fSPaul Mullowney } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) { 1313aa372e3fSPaul Mullowney CsrMatrix *matrix= new CsrMatrix; 1314a65300a6SPaul Mullowney matrix->num_rows = m; 1315aa372e3fSPaul Mullowney matrix->num_cols = A->cmap->n; 1316aa372e3fSPaul Mullowney matrix->num_entries = a->nz; 1317a65300a6SPaul Mullowney matrix->row_offsets = new THRUSTINTARRAY32(m+1); 1318a65300a6SPaul Mullowney matrix->row_offsets->assign(ii, ii + m+1); 1319aa372e3fSPaul Mullowney 1320aa372e3fSPaul Mullowney matrix->column_indices = new THRUSTINTARRAY32(a->nz); 1321aa372e3fSPaul Mullowney matrix->column_indices->assign(a->j, a->j+a->nz); 1322aa372e3fSPaul Mullowney 1323aa372e3fSPaul Mullowney matrix->values = new THRUSTARRAY(a->nz); 1324aa372e3fSPaul Mullowney matrix->values->assign(a->a, a->a+a->nz); 1325aa372e3fSPaul Mullowney 1326aa372e3fSPaul Mullowney cusparseHybMat_t hybMat; 132757d48284SJunchao Zhang stat = cusparseCreateHybMat(&hybMat);CHKERRCUSPARSE(stat); 1328aa372e3fSPaul Mullowney cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ? 1329aa372e3fSPaul Mullowney CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO; 1330a65300a6SPaul Mullowney stat = cusparse_csr2hyb(cusparsestruct->handle, matrix->num_rows, matrix->num_cols, 1331aa372e3fSPaul Mullowney matstruct->descr, matrix->values->data().get(), 1332aa372e3fSPaul Mullowney matrix->row_offsets->data().get(), 1333aa372e3fSPaul Mullowney matrix->column_indices->data().get(), 133457d48284SJunchao Zhang hybMat, 0, partition);CHKERRCUSPARSE(stat); 1335aa372e3fSPaul Mullowney /* assign the pointer */ 1336aa372e3fSPaul Mullowney matstruct->mat = hybMat; 1337aa372e3fSPaul Mullowney 1338aa372e3fSPaul Mullowney if (matrix) { 1339aa372e3fSPaul Mullowney if (matrix->values) delete (THRUSTARRAY*)matrix->values; 1340aa372e3fSPaul Mullowney if (matrix->column_indices) delete (THRUSTINTARRAY32*)matrix->column_indices; 1341aa372e3fSPaul Mullowney if (matrix->row_offsets) delete (THRUSTINTARRAY32*)matrix->row_offsets; 1342aa372e3fSPaul Mullowney delete (CsrMatrix*)matrix; 1343087f3262SPaul Mullowney } 1344087f3262SPaul Mullowney } 1345ca45077fSPaul Mullowney 1346aa372e3fSPaul Mullowney /* assign the compressed row indices */ 1347aa372e3fSPaul Mullowney matstruct->cprowIndices = new THRUSTINTARRAY(m); 1348aa372e3fSPaul Mullowney matstruct->cprowIndices->assign(ridx,ridx+m); 13494863603aSSatish Balay ierr = PetscLogCpuToGpu(((m+1)+(a->nz))*sizeof(int)+m*sizeof(PetscInt)+(3+(a->nz))*sizeof(PetscScalar));CHKERRQ(ierr); 1350aa372e3fSPaul Mullowney 1351aa372e3fSPaul Mullowney /* assign the pointer */ 1352aa372e3fSPaul Mullowney cusparsestruct->mat = matstruct; 1353aa372e3fSPaul Mullowney 13549ae82921SPaul Mullowney if (!a->compressedrow.use) { 13559ae82921SPaul Mullowney ierr = PetscFree(ii);CHKERRQ(ierr); 13569ae82921SPaul Mullowney ierr = PetscFree(ridx);CHKERRQ(ierr); 13579ae82921SPaul Mullowney } 1358e65717acSKarl Rupp cusparsestruct->workVector = new THRUSTARRAY(m); 13599ae82921SPaul Mullowney } catch(char *ex) { 13609ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 13619ae82921SPaul Mullowney } 136234d6c7a5SJose E. Roman cusparsestruct->nonzerostate = A->nonzerostate; 136334d6c7a5SJose E. Roman } 136457d48284SJunchao Zhang err = WaitForGPU();CHKERRCUDA(err); 1365c70f7ee4SJunchao Zhang A->offloadmask = PETSC_OFFLOAD_BOTH; 13669ae82921SPaul Mullowney ierr = PetscLogEventEnd(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr); 13679ae82921SPaul Mullowney } 13689ae82921SPaul Mullowney PetscFunctionReturn(0); 13699ae82921SPaul Mullowney } 13709ae82921SPaul Mullowney 1371c41cb2e2SAlejandro Lamas Daviña struct VecCUDAPlusEquals 1372aa372e3fSPaul Mullowney { 1373aa372e3fSPaul Mullowney template <typename Tuple> 1374aa372e3fSPaul Mullowney __host__ __device__ 1375aa372e3fSPaul Mullowney void operator()(Tuple t) 1376aa372e3fSPaul Mullowney { 1377aa372e3fSPaul Mullowney thrust::get<1>(t) = thrust::get<1>(t) + thrust::get<0>(t); 1378aa372e3fSPaul Mullowney } 1379aa372e3fSPaul Mullowney }; 1380aa372e3fSPaul Mullowney 13816fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy) 13829ae82921SPaul Mullowney { 1383b175d8bbSPaul Mullowney PetscErrorCode ierr; 13849ae82921SPaul Mullowney 13859ae82921SPaul Mullowney PetscFunctionBegin; 13867656d835SStefano Zampini ierr = MatMultAdd_SeqAIJCUSPARSE(A,xx,NULL,yy);CHKERRQ(ierr); 13879ae82921SPaul Mullowney PetscFunctionReturn(0); 13889ae82921SPaul Mullowney } 13899ae82921SPaul Mullowney 13906fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy) 1391ca45077fSPaul Mullowney { 1392ca45077fSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1393aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 13949ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstructT; 1395465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 1396465f34aeSAlejandro Lamas Daviña PetscScalar *yarray; 1397b175d8bbSPaul Mullowney PetscErrorCode ierr; 139857d48284SJunchao Zhang cudaError_t cerr; 1399aa372e3fSPaul Mullowney cusparseStatus_t stat; 1400ca45077fSPaul Mullowney 1401ca45077fSPaul Mullowney PetscFunctionBegin; 140234d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 140334d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 14049ff858a8SKarl Rupp matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1405aa372e3fSPaul Mullowney if (!matstructT) { 1406bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr); 1407aa372e3fSPaul Mullowney matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1408bda325fcSPaul Mullowney } 1409c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1410c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(yy,&yarray);CHKERRQ(ierr); 1411f6ae8131SMark if (yy->map->n) { 1412f6ae8131SMark PetscInt n = yy->map->n; 1413f6ae8131SMark cudaError_t err; 1414f6ae8131SMark err = cudaMemset(yarray,0,n*sizeof(PetscScalar));CHKERRCUDA(err); /* hack to fix numerical errors from reading output vector yy, apparently */ 1415f6ae8131SMark } 1416aa372e3fSPaul Mullowney 14177a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1418aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1419aa372e3fSPaul Mullowney CsrMatrix *mat = (CsrMatrix*)matstructT->mat; 1420aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1421aa372e3fSPaul Mullowney mat->num_rows, mat->num_cols, 1422b06137fdSPaul Mullowney mat->num_entries, matstructT->alpha, matstructT->descr, 1423aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 14247656d835SStefano Zampini mat->column_indices->data().get(), xarray, matstructT->beta_zero, 142557d48284SJunchao Zhang yarray);CHKERRCUSPARSE(stat); 1426aa372e3fSPaul Mullowney } else { 1427aa372e3fSPaul Mullowney cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat; 1428aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1429b06137fdSPaul Mullowney matstructT->alpha, matstructT->descr, hybMat, 14307656d835SStefano Zampini xarray, matstructT->beta_zero, 143157d48284SJunchao Zhang yarray);CHKERRCUSPARSE(stat); 1432ca45077fSPaul Mullowney } 1433c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1434c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(yy,&yarray);CHKERRQ(ierr); 143557d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); 1436661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1437958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*a->nz - cusparsestruct->nonzerorow);CHKERRQ(ierr); 1438ca45077fSPaul Mullowney PetscFunctionReturn(0); 1439ca45077fSPaul Mullowney } 1440ca45077fSPaul Mullowney 1441aa372e3fSPaul Mullowney 14426fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz) 14439ae82921SPaul Mullowney { 14449ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1445aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 14469ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstruct; 1447465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 14487656d835SStefano Zampini PetscScalar *zarray,*dptr,*beta; 1449b175d8bbSPaul Mullowney PetscErrorCode ierr; 145057d48284SJunchao Zhang cudaError_t cerr; 1451aa372e3fSPaul Mullowney cusparseStatus_t stat; 1452c1fb3f03SStefano Zampini PetscBool cmpr; /* if the matrix has been compressed (zero rows) */ 14536e111a19SKarl Rupp 14549ae82921SPaul Mullowney PetscFunctionBegin; 145534d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 145634d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 14579ff858a8SKarl Rupp matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 14589ae82921SPaul Mullowney try { 1459c1fb3f03SStefano Zampini cmpr = (PetscBool)(cusparsestruct->workVector->size() == (thrust::detail::vector_base<PetscScalar, thrust::device_malloc_allocator<PetscScalar> >::size_type)(A->rmap->n)); 1460c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1461c1fb3f03SStefano Zampini if (yy && !cmpr) { /* MatMultAdd with noncompressed storage -> need uptodate zz vector */ 1462f2d70e9dSBarry Smith ierr = VecCUDAGetArray(zz,&zarray);CHKERRQ(ierr); 1463c1fb3f03SStefano Zampini } else { 1464c1fb3f03SStefano Zampini ierr = VecCUDAGetArrayWrite(zz,&zarray);CHKERRQ(ierr); 1465c1fb3f03SStefano Zampini } 1466c1fb3f03SStefano Zampini dptr = cmpr ? zarray : cusparsestruct->workVector->data().get(); 14677656d835SStefano Zampini beta = (yy == zz && dptr == zarray) ? matstruct->beta_one : matstruct->beta_zero; 14689ae82921SPaul Mullowney 14697a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 14707656d835SStefano Zampini /* csr_spmv is multiply add */ 1471aa372e3fSPaul Mullowney if (cusparsestruct->format == MAT_CUSPARSE_CSR) { 1472b06137fdSPaul Mullowney /* here we need to be careful to set the number of rows in the multiply to the 1473b06137fdSPaul Mullowney number of compressed rows in the matrix ... which is equivalent to the 1474b06137fdSPaul Mullowney size of the workVector */ 14757656d835SStefano Zampini CsrMatrix *mat = (CsrMatrix*)matstruct->mat; 1476aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1477a65300a6SPaul Mullowney mat->num_rows, mat->num_cols, 1478b06137fdSPaul Mullowney mat->num_entries, matstruct->alpha, matstruct->descr, 1479aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 14807656d835SStefano Zampini mat->column_indices->data().get(), xarray, beta, 148157d48284SJunchao Zhang dptr);CHKERRCUSPARSE(stat); 1482aa372e3fSPaul Mullowney } else { 1483a65300a6SPaul Mullowney if (cusparsestruct->workVector->size()) { 1484301298b4SMark Adams cusparseHybMat_t hybMat = (cusparseHybMat_t)matstruct->mat; 1485aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1486b06137fdSPaul Mullowney matstruct->alpha, matstruct->descr, hybMat, 14877656d835SStefano Zampini xarray, beta, 148857d48284SJunchao Zhang dptr);CHKERRCUSPARSE(stat); 1489a65300a6SPaul Mullowney } 1490aa372e3fSPaul Mullowney } 1491958c4211Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1492aa372e3fSPaul Mullowney 14937656d835SStefano Zampini if (yy) { 14947656d835SStefano Zampini if (dptr != zarray) { 14957656d835SStefano Zampini ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); 14967656d835SStefano Zampini } else if (zz != yy) { 14977656d835SStefano Zampini ierr = VecAXPY_SeqCUDA(zz,1.0,yy);CHKERRQ(ierr); 14987656d835SStefano Zampini } 14997656d835SStefano Zampini } else if (dptr != zarray) { 1500c1fb3f03SStefano Zampini ierr = VecSet_SeqCUDA(zz,0);CHKERRQ(ierr); 15017656d835SStefano Zampini } 1502aa372e3fSPaul Mullowney /* scatter the data from the temporary into the full vector with a += operation */ 15037a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 15047656d835SStefano Zampini if (dptr != zarray) { 15057656d835SStefano Zampini thrust::device_ptr<PetscScalar> zptr; 15067656d835SStefano Zampini 15077656d835SStefano Zampini zptr = thrust::device_pointer_cast(zarray); 1508c41cb2e2SAlejandro Lamas Daviña thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))), 1509c41cb2e2SAlejandro Lamas Daviña thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))) + cusparsestruct->workVector->size(), 1510c41cb2e2SAlejandro Lamas Daviña VecCUDAPlusEquals()); 15117656d835SStefano Zampini } 1512958c4211Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1513c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1514c1fb3f03SStefano Zampini if (yy && !cmpr) { 1515f2d70e9dSBarry Smith ierr = VecCUDARestoreArray(zz,&zarray);CHKERRQ(ierr); 1516c1fb3f03SStefano Zampini } else { 1517c1fb3f03SStefano Zampini ierr = VecCUDARestoreArrayWrite(zz,&zarray);CHKERRQ(ierr); 1518c1fb3f03SStefano Zampini } 15199ae82921SPaul Mullowney } catch(char *ex) { 15209ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 15219ae82921SPaul Mullowney } 152257d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); 1523958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr); 15249ae82921SPaul Mullowney PetscFunctionReturn(0); 15259ae82921SPaul Mullowney } 15269ae82921SPaul Mullowney 15276fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz) 1528ca45077fSPaul Mullowney { 1529ca45077fSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1530aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 15319ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstructT; 1532465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 1533a3fdcf43SKarl Rupp PetscScalar *zarray,*dptr,*beta; 1534b175d8bbSPaul Mullowney PetscErrorCode ierr; 153557d48284SJunchao Zhang cudaError_t cerr; 1536aa372e3fSPaul Mullowney cusparseStatus_t stat; 15376e111a19SKarl Rupp 1538ca45077fSPaul Mullowney PetscFunctionBegin; 153934d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 154034d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 15419ff858a8SKarl Rupp matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1542aa372e3fSPaul Mullowney if (!matstructT) { 1543bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr); 1544aa372e3fSPaul Mullowney matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1545bda325fcSPaul Mullowney } 1546aa372e3fSPaul Mullowney 1547ca45077fSPaul Mullowney try { 1548c41cb2e2SAlejandro Lamas Daviña ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); 1549c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1550f2d70e9dSBarry Smith ierr = VecCUDAGetArray(zz,&zarray);CHKERRQ(ierr); 1551a3fdcf43SKarl Rupp dptr = cusparsestruct->workVector->size() == (thrust::detail::vector_base<PetscScalar, thrust::device_malloc_allocator<PetscScalar> >::size_type)(A->cmap->n) ? zarray : cusparsestruct->workVector->data().get(); 1552a3fdcf43SKarl Rupp beta = (yy == zz && dptr == zarray) ? matstructT->beta_one : matstructT->beta_zero; 1553ca45077fSPaul Mullowney 15547a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1555e057df02SPaul Mullowney /* multiply add with matrix transpose */ 1556aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1557aa372e3fSPaul Mullowney CsrMatrix *mat = (CsrMatrix*)matstructT->mat; 1558b06137fdSPaul Mullowney /* here we need to be careful to set the number of rows in the multiply to the 1559b06137fdSPaul Mullowney number of compressed rows in the matrix ... which is equivalent to the 1560b06137fdSPaul Mullowney size of the workVector */ 1561aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1562a65300a6SPaul Mullowney mat->num_rows, mat->num_cols, 1563b06137fdSPaul Mullowney mat->num_entries, matstructT->alpha, matstructT->descr, 1564aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 1565a3fdcf43SKarl Rupp mat->column_indices->data().get(), xarray, beta, 156657d48284SJunchao Zhang dptr);CHKERRCUSPARSE(stat); 1567aa372e3fSPaul Mullowney } else { 1568aa372e3fSPaul Mullowney cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat; 1569a65300a6SPaul Mullowney if (cusparsestruct->workVector->size()) { 1570aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1571b06137fdSPaul Mullowney matstructT->alpha, matstructT->descr, hybMat, 1572a3fdcf43SKarl Rupp xarray, beta, 157357d48284SJunchao Zhang dptr);CHKERRCUSPARSE(stat); 1574a65300a6SPaul Mullowney } 1575aa372e3fSPaul Mullowney } 1576a3fdcf43SKarl Rupp ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1577a3fdcf43SKarl Rupp 1578a3fdcf43SKarl Rupp if (dptr != zarray) { 1579a3fdcf43SKarl Rupp ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); 1580a3fdcf43SKarl Rupp } else if (zz != yy) { 1581a3fdcf43SKarl Rupp ierr = VecAXPY_SeqCUDA(zz,1.0,yy);CHKERRQ(ierr); 1582a3fdcf43SKarl Rupp } 1583a3fdcf43SKarl Rupp /* scatter the data from the temporary into the full vector with a += operation */ 1584a3fdcf43SKarl Rupp ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1585a3fdcf43SKarl Rupp if (dptr != zarray) { 1586a3fdcf43SKarl Rupp thrust::device_ptr<PetscScalar> zptr; 1587a3fdcf43SKarl Rupp 1588a3fdcf43SKarl Rupp zptr = thrust::device_pointer_cast(zarray); 1589aa372e3fSPaul Mullowney 1590aa372e3fSPaul Mullowney /* scatter the data from the temporary into the full vector with a += operation */ 1591c41cb2e2SAlejandro Lamas Daviña thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstructT->cprowIndices->begin()))), 1592554b8892SKarl Rupp thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstructT->cprowIndices->begin()))) + A->cmap->n, 1593c41cb2e2SAlejandro Lamas Daviña VecCUDAPlusEquals()); 1594a3fdcf43SKarl Rupp } 1595a3fdcf43SKarl Rupp ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1596c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1597f2d70e9dSBarry Smith ierr = VecCUDARestoreArray(zz,&zarray);CHKERRQ(ierr); 1598ca45077fSPaul Mullowney } catch(char *ex) { 1599ca45077fSPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 1600661c2d29Shannah_mairs } 160157d48284SJunchao Zhang cerr = WaitForGPU();CHKERRCUDA(cerr); /* is this needed? just for yy==0 in Mult */ 1602958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr); 1603ca45077fSPaul Mullowney PetscFunctionReturn(0); 1604ca45077fSPaul Mullowney } 1605ca45077fSPaul Mullowney 16066fa9248bSJed Brown static PetscErrorCode MatAssemblyEnd_SeqAIJCUSPARSE(Mat A,MatAssemblyType mode) 16079ae82921SPaul Mullowney { 16089ae82921SPaul Mullowney PetscErrorCode ierr; 16096e111a19SKarl Rupp 16109ae82921SPaul Mullowney PetscFunctionBegin; 16119ae82921SPaul Mullowney ierr = MatAssemblyEnd_SeqAIJ(A,mode);CHKERRQ(ierr); 1612*95639643SRichard Tran Mills if (mode == MAT_FLUSH_ASSEMBLY || A->boundtocpu) PetscFunctionReturn(0); 1613bc3f50f2SPaul Mullowney if (A->factortype == MAT_FACTOR_NONE) { 1614e057df02SPaul Mullowney ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 1615bc3f50f2SPaul Mullowney } 1616bbf3fe20SPaul Mullowney A->ops->mult = MatMult_SeqAIJCUSPARSE; 1617bbf3fe20SPaul Mullowney A->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 1618bbf3fe20SPaul Mullowney A->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1619bbf3fe20SPaul Mullowney A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 16209ae82921SPaul Mullowney PetscFunctionReturn(0); 16219ae82921SPaul Mullowney } 16229ae82921SPaul Mullowney 16239ae82921SPaul Mullowney /* --------------------------------------------------------------------------------*/ 1624e057df02SPaul Mullowney /*@ 16259ae82921SPaul Mullowney MatCreateSeqAIJCUSPARSE - Creates a sparse matrix in AIJ (compressed row) format 1626e057df02SPaul Mullowney (the default parallel PETSc format). This matrix will ultimately pushed down 1627e057df02SPaul Mullowney to NVidia GPUs and use the CUSPARSE library for calculations. For good matrix 1628e057df02SPaul Mullowney assembly performance the user should preallocate the matrix storage by setting 1629e057df02SPaul Mullowney the parameter nz (or the array nnz). By setting these parameters accurately, 1630e057df02SPaul Mullowney performance during matrix assembly can be increased by more than a factor of 50. 16319ae82921SPaul Mullowney 1632d083f849SBarry Smith Collective 16339ae82921SPaul Mullowney 16349ae82921SPaul Mullowney Input Parameters: 16359ae82921SPaul Mullowney + comm - MPI communicator, set to PETSC_COMM_SELF 16369ae82921SPaul Mullowney . m - number of rows 16379ae82921SPaul Mullowney . n - number of columns 16389ae82921SPaul Mullowney . nz - number of nonzeros per row (same for all rows) 16399ae82921SPaul Mullowney - nnz - array containing the number of nonzeros in the various rows 16400298fd71SBarry Smith (possibly different for each row) or NULL 16419ae82921SPaul Mullowney 16429ae82921SPaul Mullowney Output Parameter: 16439ae82921SPaul Mullowney . A - the matrix 16449ae82921SPaul Mullowney 16459ae82921SPaul Mullowney It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 16469ae82921SPaul Mullowney MatXXXXSetPreallocation() paradgm instead of this routine directly. 16479ae82921SPaul Mullowney [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 16489ae82921SPaul Mullowney 16499ae82921SPaul Mullowney Notes: 16509ae82921SPaul Mullowney If nnz is given then nz is ignored 16519ae82921SPaul Mullowney 16529ae82921SPaul Mullowney The AIJ format (also called the Yale sparse matrix format or 16539ae82921SPaul Mullowney compressed row storage), is fully compatible with standard Fortran 77 16549ae82921SPaul Mullowney storage. That is, the stored row and column indices can begin at 16559ae82921SPaul Mullowney either one (as in Fortran) or zero. See the users' manual for details. 16569ae82921SPaul Mullowney 16579ae82921SPaul Mullowney Specify the preallocated storage with either nz or nnz (not both). 16580298fd71SBarry Smith Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory 16599ae82921SPaul Mullowney allocation. For large problems you MUST preallocate memory or you 16609ae82921SPaul Mullowney will get TERRIBLE performance, see the users' manual chapter on matrices. 16619ae82921SPaul Mullowney 16629ae82921SPaul Mullowney By default, this format uses inodes (identical nodes) when possible, to 16639ae82921SPaul Mullowney improve numerical efficiency of matrix-vector products and solves. We 16649ae82921SPaul Mullowney search for consecutive rows with the same nonzero structure, thereby 16659ae82921SPaul Mullowney reusing matrix information to achieve increased efficiency. 16669ae82921SPaul Mullowney 16679ae82921SPaul Mullowney Level: intermediate 16689ae82921SPaul Mullowney 1669e057df02SPaul Mullowney .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ(), MATSEQAIJCUSPARSE, MATAIJCUSPARSE 16709ae82921SPaul Mullowney @*/ 16719ae82921SPaul Mullowney PetscErrorCode MatCreateSeqAIJCUSPARSE(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 16729ae82921SPaul Mullowney { 16739ae82921SPaul Mullowney PetscErrorCode ierr; 16749ae82921SPaul Mullowney 16759ae82921SPaul Mullowney PetscFunctionBegin; 16769ae82921SPaul Mullowney ierr = MatCreate(comm,A);CHKERRQ(ierr); 16779ae82921SPaul Mullowney ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 16789ae82921SPaul Mullowney ierr = MatSetType(*A,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 16799ae82921SPaul Mullowney ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr); 16809ae82921SPaul Mullowney PetscFunctionReturn(0); 16819ae82921SPaul Mullowney } 16829ae82921SPaul Mullowney 16836fa9248bSJed Brown static PetscErrorCode MatDestroy_SeqAIJCUSPARSE(Mat A) 16849ae82921SPaul Mullowney { 16859ae82921SPaul Mullowney PetscErrorCode ierr; 1686ab25e6cbSDominic Meiser 16879ae82921SPaul Mullowney PetscFunctionBegin; 16889ae82921SPaul Mullowney if (A->factortype==MAT_FACTOR_NONE) { 1689c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) { 1690470880abSPatrick Sanan ierr = MatSeqAIJCUSPARSE_Destroy((Mat_SeqAIJCUSPARSE**)&A->spptr);CHKERRQ(ierr); 16919ae82921SPaul Mullowney } 16929ae82921SPaul Mullowney } else { 1693470880abSPatrick Sanan ierr = MatSeqAIJCUSPARSETriFactors_Destroy((Mat_SeqAIJCUSPARSETriFactors**)&A->spptr);CHKERRQ(ierr); 1694aa372e3fSPaul Mullowney } 16959ae82921SPaul Mullowney ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr); 16969ae82921SPaul Mullowney PetscFunctionReturn(0); 16979ae82921SPaul Mullowney } 16989ae82921SPaul Mullowney 1699*95639643SRichard Tran Mills static PetscErrorCode MatBindToCPU_SeqAIJCUSPARSE(Mat,PetscBool); 17009ff858a8SKarl Rupp static PetscErrorCode MatDuplicate_SeqAIJCUSPARSE(Mat A,MatDuplicateOption cpvalues,Mat *B) 17019ff858a8SKarl Rupp { 17029ff858a8SKarl Rupp PetscErrorCode ierr; 17039ff858a8SKarl Rupp Mat C; 17049ff858a8SKarl Rupp cusparseStatus_t stat; 17059ff858a8SKarl Rupp cusparseHandle_t handle=0; 17069ff858a8SKarl Rupp 17079ff858a8SKarl Rupp PetscFunctionBegin; 17089ff858a8SKarl Rupp ierr = MatDuplicate_SeqAIJ(A,cpvalues,B);CHKERRQ(ierr); 17099ff858a8SKarl Rupp C = *B; 171034136279SStefano Zampini ierr = PetscFree(C->defaultvectype);CHKERRQ(ierr); 171134136279SStefano Zampini ierr = PetscStrallocpy(VECCUDA,&C->defaultvectype);CHKERRQ(ierr); 171234136279SStefano Zampini 17139ff858a8SKarl Rupp /* inject CUSPARSE-specific stuff */ 17149ff858a8SKarl Rupp if (C->factortype==MAT_FACTOR_NONE) { 17159ff858a8SKarl Rupp /* you cannot check the inode.use flag here since the matrix was just created. 17169ff858a8SKarl Rupp now build a GPU matrix data structure */ 17179ff858a8SKarl Rupp C->spptr = new Mat_SeqAIJCUSPARSE; 17189ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->mat = 0; 17199ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->matTranspose = 0; 17209ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->workVector = 0; 17219ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->format = MAT_CUSPARSE_CSR; 17229ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->stream = 0; 172357d48284SJunchao Zhang stat = cusparseCreate(&handle);CHKERRCUSPARSE(stat); 17249ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->handle = handle; 17259ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->nonzerostate = 0; 17269ff858a8SKarl Rupp } else { 17279ff858a8SKarl Rupp /* NEXT, set the pointers to the triangular factors */ 17289ff858a8SKarl Rupp C->spptr = new Mat_SeqAIJCUSPARSETriFactors; 17299ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->loTriFactorPtr = 0; 17309ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->upTriFactorPtr = 0; 17319ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->loTriFactorPtrTranspose = 0; 17329ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->upTriFactorPtrTranspose = 0; 17339ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->rpermIndices = 0; 17349ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->cpermIndices = 0; 17359ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->workVector = 0; 17369ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->handle = 0; 173757d48284SJunchao Zhang stat = cusparseCreate(&handle);CHKERRCUSPARSE(stat); 17389ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->handle = handle; 17399ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->nnz = 0; 17409ff858a8SKarl Rupp } 17419ff858a8SKarl Rupp 17429ff858a8SKarl Rupp C->ops->assemblyend = MatAssemblyEnd_SeqAIJCUSPARSE; 17439ff858a8SKarl Rupp C->ops->destroy = MatDestroy_SeqAIJCUSPARSE; 17449ff858a8SKarl Rupp C->ops->setfromoptions = MatSetFromOptions_SeqAIJCUSPARSE; 17459ff858a8SKarl Rupp C->ops->mult = MatMult_SeqAIJCUSPARSE; 17469ff858a8SKarl Rupp C->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 17479ff858a8SKarl Rupp C->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 17489ff858a8SKarl Rupp C->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 17499ff858a8SKarl Rupp C->ops->duplicate = MatDuplicate_SeqAIJCUSPARSE; 1750*95639643SRichard Tran Mills C->ops->bindtocpu = MatBindToCPU_SeqAIJCUSPARSE; 17519ff858a8SKarl Rupp 17529ff858a8SKarl Rupp ierr = PetscObjectChangeTypeName((PetscObject)C,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 17539ff858a8SKarl Rupp 1754*95639643SRichard Tran Mills C->boundtocpu = PETSC_FALSE; 1755c70f7ee4SJunchao Zhang C->offloadmask = PETSC_OFFLOAD_UNALLOCATED; 17569ff858a8SKarl Rupp 17579ff858a8SKarl Rupp ierr = PetscObjectComposeFunction((PetscObject)C, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr); 17589ff858a8SKarl Rupp PetscFunctionReturn(0); 17599ff858a8SKarl Rupp } 17609ff858a8SKarl Rupp 1761*95639643SRichard Tran Mills 1762*95639643SRichard Tran Mills static PetscErrorCode MatBindToCPU_SeqAIJCUSPARSE(Mat A,PetscBool flg) 1763*95639643SRichard Tran Mills { 1764*95639643SRichard Tran Mills PetscErrorCode ierr; 1765*95639643SRichard Tran Mills 1766*95639643SRichard Tran Mills PetscFunctionBegin; 1767*95639643SRichard Tran Mills /* Currently, there is case in which an AIJCUSPARSE matrix ever has its offloadmask set to PETS_OFFLOAD_GPU. 1768*95639643SRichard Tran Mills If this changes, we need to implement a routine to update the CPU (host) version of the matrix from the GPU one. 1769*95639643SRichard Tran Mills Right now, for safety we simply check for PETSC_OFFLOAD_GPU and have MatBindToCPU() do nothing in this case. 1770*95639643SRichard Tran Mills TODO: Add MatAIJCUSPARSECopyFromGPU() and make MatBindToCPU() functional for AIJCUSPARSE matries; 1771*95639643SRichard Tran Mills can follow the example of MatBindToCPU_SeqAIJViennaCL(). */ 1772*95639643SRichard Tran Mills if (A->offloadmask == PETSC_OFFLOAD_GPU) PetscFunctionReturn(0); 1773*95639643SRichard Tran Mills if (flg) { 1774*95639643SRichard Tran Mills A->ops->mult = MatMult_SeqAIJ; 1775*95639643SRichard Tran Mills A->ops->multadd = MatMultAdd_SeqAIJ; 1776*95639643SRichard Tran Mills A->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1777*95639643SRichard Tran Mills A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 1778*95639643SRichard Tran Mills A->ops->assemblyend = MatAssemblyEnd_SeqAIJ; 1779*95639643SRichard Tran Mills A->ops->duplicate = MatDuplicate_SeqAIJ; 1780*95639643SRichard Tran Mills } else { 1781*95639643SRichard Tran Mills A->ops->mult = MatMult_SeqAIJCUSPARSE; 1782*95639643SRichard Tran Mills A->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 1783*95639643SRichard Tran Mills A->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1784*95639643SRichard Tran Mills A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 1785*95639643SRichard Tran Mills A->ops->assemblyend = MatAssemblyEnd_SeqAIJCUSPARSE; 1786*95639643SRichard Tran Mills A->ops->destroy = MatDestroy_SeqAIJCUSPARSE; 1787*95639643SRichard Tran Mills A->ops->duplicate = MatDuplicate_SeqAIJCUSPARSE; 1788*95639643SRichard Tran Mills } 1789*95639643SRichard Tran Mills A->boundtocpu = flg; 1790*95639643SRichard Tran Mills PetscFunctionReturn(0); 1791*95639643SRichard Tran Mills } 1792*95639643SRichard Tran Mills 179302fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat B) 17949ae82921SPaul Mullowney { 17959ae82921SPaul Mullowney PetscErrorCode ierr; 1796aa372e3fSPaul Mullowney cusparseStatus_t stat; 1797aa372e3fSPaul Mullowney cusparseHandle_t handle=0; 17989ae82921SPaul Mullowney 17999ae82921SPaul Mullowney PetscFunctionBegin; 180034136279SStefano Zampini ierr = PetscFree(B->defaultvectype);CHKERRQ(ierr); 180134136279SStefano Zampini ierr = PetscStrallocpy(VECCUDA,&B->defaultvectype);CHKERRQ(ierr); 180234136279SStefano Zampini 18039ae82921SPaul Mullowney if (B->factortype==MAT_FACTOR_NONE) { 1804e057df02SPaul Mullowney /* you cannot check the inode.use flag here since the matrix was just created. 1805e057df02SPaul Mullowney now build a GPU matrix data structure */ 18069ae82921SPaul Mullowney B->spptr = new Mat_SeqAIJCUSPARSE; 18079ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->mat = 0; 1808aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->matTranspose = 0; 1809aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->workVector = 0; 1810e057df02SPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->format = MAT_CUSPARSE_CSR; 1811aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->stream = 0; 181257d48284SJunchao Zhang stat = cusparseCreate(&handle);CHKERRCUSPARSE(stat); 1813aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->handle = handle; 18149ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)B->spptr)->nonzerostate = 0; 18159ae82921SPaul Mullowney } else { 18169ae82921SPaul Mullowney /* NEXT, set the pointers to the triangular factors */ 1817debe9ee2SPaul Mullowney B->spptr = new Mat_SeqAIJCUSPARSETriFactors; 18189ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtr = 0; 18199ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtr = 0; 1820aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtrTranspose = 0; 1821aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtrTranspose = 0; 1822aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->rpermIndices = 0; 1823aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->cpermIndices = 0; 1824aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->workVector = 0; 182557d48284SJunchao Zhang stat = cusparseCreate(&handle);CHKERRCUSPARSE(stat); 1826aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->handle = handle; 1827aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->nnz = 0; 18289ae82921SPaul Mullowney } 1829aa372e3fSPaul Mullowney 18309ae82921SPaul Mullowney B->ops->assemblyend = MatAssemblyEnd_SeqAIJCUSPARSE; 18319ae82921SPaul Mullowney B->ops->destroy = MatDestroy_SeqAIJCUSPARSE; 18329ae82921SPaul Mullowney B->ops->setfromoptions = MatSetFromOptions_SeqAIJCUSPARSE; 1833ca45077fSPaul Mullowney B->ops->mult = MatMult_SeqAIJCUSPARSE; 1834ca45077fSPaul Mullowney B->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 1835ca45077fSPaul Mullowney B->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1836ca45077fSPaul Mullowney B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 18379ff858a8SKarl Rupp B->ops->duplicate = MatDuplicate_SeqAIJCUSPARSE; 1838*95639643SRichard Tran Mills B->ops->bindtocpu = MatBindToCPU_SeqAIJCUSPARSE; 18392205254eSKarl Rupp 18409ae82921SPaul Mullowney ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 18412205254eSKarl Rupp 1842*95639643SRichard Tran Mills B->boundtocpu = PETSC_FALSE; 1843c70f7ee4SJunchao Zhang B->offloadmask = PETSC_OFFLOAD_UNALLOCATED; 18442205254eSKarl Rupp 1845bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr); 18469ae82921SPaul Mullowney PetscFunctionReturn(0); 18479ae82921SPaul Mullowney } 18489ae82921SPaul Mullowney 184902fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJCUSPARSE(Mat B) 185002fe1965SBarry Smith { 185102fe1965SBarry Smith PetscErrorCode ierr; 185202fe1965SBarry Smith 185302fe1965SBarry Smith PetscFunctionBegin; 185402fe1965SBarry Smith ierr = MatCreate_SeqAIJ(B);CHKERRQ(ierr); 1855489de41dSStefano Zampini ierr = MatConvert_SeqAIJ_SeqAIJCUSPARSE(B);CHKERRQ(ierr); 185602fe1965SBarry Smith PetscFunctionReturn(0); 185702fe1965SBarry Smith } 185802fe1965SBarry Smith 18593ca39a21SBarry Smith /*MC 1860e057df02SPaul Mullowney MATSEQAIJCUSPARSE - MATAIJCUSPARSE = "(seq)aijcusparse" - A matrix type to be used for sparse matrices. 1861e057df02SPaul Mullowney 1862e057df02SPaul Mullowney A matrix type type whose data resides on Nvidia GPUs. These matrices can be in either 18632692e278SPaul Mullowney CSR, ELL, or Hybrid format. The ELL and HYB formats require CUDA 4.2 or later. 18642692e278SPaul Mullowney All matrix calculations are performed on Nvidia GPUs using the CUSPARSE library. 1865e057df02SPaul Mullowney 1866e057df02SPaul Mullowney Options Database Keys: 1867e057df02SPaul Mullowney + -mat_type aijcusparse - sets the matrix type to "seqaijcusparse" during a call to MatSetFromOptions() 1868aa372e3fSPaul 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). 1869a2b725a8SWilliam 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). 1870e057df02SPaul Mullowney 1871e057df02SPaul Mullowney Level: beginner 1872e057df02SPaul Mullowney 18738468deeeSKarl Rupp .seealso: MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation 1874e057df02SPaul Mullowney M*/ 18757f756511SDominic Meiser 187642c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat,MatFactorType,Mat*); 187742c9c57cSBarry Smith 18780f39cd5aSBarry Smith 18793ca39a21SBarry Smith PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_CUSPARSE(void) 188042c9c57cSBarry Smith { 188142c9c57cSBarry Smith PetscErrorCode ierr; 188242c9c57cSBarry Smith 188342c9c57cSBarry Smith PetscFunctionBegin; 18843ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_LU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 18853ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_CHOLESKY,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 18863ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ILU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 18873ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ICC,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 188842c9c57cSBarry Smith PetscFunctionReturn(0); 188942c9c57cSBarry Smith } 189029b38603SBarry Smith 189181e08676SBarry Smith 1892470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE **cusparsestruct) 18937f756511SDominic Meiser { 18947f756511SDominic Meiser cusparseStatus_t stat; 18957f756511SDominic Meiser cusparseHandle_t handle; 18967f756511SDominic Meiser 18977f756511SDominic Meiser PetscFunctionBegin; 18987f756511SDominic Meiser if (*cusparsestruct) { 1899470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->mat,(*cusparsestruct)->format); 1900470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->matTranspose,(*cusparsestruct)->format); 19017f756511SDominic Meiser delete (*cusparsestruct)->workVector; 19027f756511SDominic Meiser if (handle = (*cusparsestruct)->handle) { 190357d48284SJunchao Zhang stat = cusparseDestroy(handle);CHKERRCUSPARSE(stat); 19047f756511SDominic Meiser } 19057f756511SDominic Meiser delete *cusparsestruct; 19067f756511SDominic Meiser *cusparsestruct = 0; 19077f756511SDominic Meiser } 19087f756511SDominic Meiser PetscFunctionReturn(0); 19097f756511SDominic Meiser } 19107f756511SDominic Meiser 19117f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix **mat) 19127f756511SDominic Meiser { 19137f756511SDominic Meiser PetscFunctionBegin; 19147f756511SDominic Meiser if (*mat) { 19157f756511SDominic Meiser delete (*mat)->values; 19167f756511SDominic Meiser delete (*mat)->column_indices; 19177f756511SDominic Meiser delete (*mat)->row_offsets; 19187f756511SDominic Meiser delete *mat; 19197f756511SDominic Meiser *mat = 0; 19207f756511SDominic Meiser } 19217f756511SDominic Meiser PetscFunctionReturn(0); 19227f756511SDominic Meiser } 19237f756511SDominic Meiser 1924470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct **trifactor) 19257f756511SDominic Meiser { 19267f756511SDominic Meiser cusparseStatus_t stat; 19277f756511SDominic Meiser PetscErrorCode ierr; 19287f756511SDominic Meiser 19297f756511SDominic Meiser PetscFunctionBegin; 19307f756511SDominic Meiser if (*trifactor) { 193157d48284SJunchao Zhang if ((*trifactor)->descr) { stat = cusparseDestroyMatDescr((*trifactor)->descr);CHKERRCUSPARSE(stat); } 193257d48284SJunchao Zhang if ((*trifactor)->solveInfo) { stat = cusparseDestroySolveAnalysisInfo((*trifactor)->solveInfo);CHKERRCUSPARSE(stat); } 19337f756511SDominic Meiser ierr = CsrMatrix_Destroy(&(*trifactor)->csrMat);CHKERRQ(ierr); 19347f756511SDominic Meiser delete *trifactor; 19357f756511SDominic Meiser *trifactor = 0; 19367f756511SDominic Meiser } 19377f756511SDominic Meiser PetscFunctionReturn(0); 19387f756511SDominic Meiser } 19397f756511SDominic Meiser 1940470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct **matstruct,MatCUSPARSEStorageFormat format) 19417f756511SDominic Meiser { 19427f756511SDominic Meiser CsrMatrix *mat; 19437f756511SDominic Meiser cusparseStatus_t stat; 19447f756511SDominic Meiser cudaError_t err; 19457f756511SDominic Meiser 19467f756511SDominic Meiser PetscFunctionBegin; 19477f756511SDominic Meiser if (*matstruct) { 19487f756511SDominic Meiser if ((*matstruct)->mat) { 19497f756511SDominic Meiser if (format==MAT_CUSPARSE_ELL || format==MAT_CUSPARSE_HYB) { 19507f756511SDominic Meiser cusparseHybMat_t hybMat = (cusparseHybMat_t)(*matstruct)->mat; 195157d48284SJunchao Zhang stat = cusparseDestroyHybMat(hybMat);CHKERRCUSPARSE(stat); 19527f756511SDominic Meiser } else { 19537f756511SDominic Meiser mat = (CsrMatrix*)(*matstruct)->mat; 19547f756511SDominic Meiser CsrMatrix_Destroy(&mat); 19557f756511SDominic Meiser } 19567f756511SDominic Meiser } 195757d48284SJunchao Zhang if ((*matstruct)->descr) { stat = cusparseDestroyMatDescr((*matstruct)->descr);CHKERRCUSPARSE(stat); } 19587f756511SDominic Meiser delete (*matstruct)->cprowIndices; 1959c41cb2e2SAlejandro Lamas Daviña if ((*matstruct)->alpha) { err=cudaFree((*matstruct)->alpha);CHKERRCUDA(err); } 19607656d835SStefano Zampini if ((*matstruct)->beta_zero) { err=cudaFree((*matstruct)->beta_zero);CHKERRCUDA(err); } 19617656d835SStefano Zampini if ((*matstruct)->beta_one) { err=cudaFree((*matstruct)->beta_one);CHKERRCUDA(err); } 19627f756511SDominic Meiser delete *matstruct; 19637f756511SDominic Meiser *matstruct = 0; 19647f756511SDominic Meiser } 19657f756511SDominic Meiser PetscFunctionReturn(0); 19667f756511SDominic Meiser } 19677f756511SDominic Meiser 1968470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors** trifactors) 19697f756511SDominic Meiser { 19707f756511SDominic Meiser cusparseHandle_t handle; 19717f756511SDominic Meiser cusparseStatus_t stat; 19727f756511SDominic Meiser 19737f756511SDominic Meiser PetscFunctionBegin; 19747f756511SDominic Meiser if (*trifactors) { 1975470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->loTriFactorPtr); 1976470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->upTriFactorPtr); 1977470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->loTriFactorPtrTranspose); 1978470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->upTriFactorPtrTranspose); 19797f756511SDominic Meiser delete (*trifactors)->rpermIndices; 19807f756511SDominic Meiser delete (*trifactors)->cpermIndices; 19817f756511SDominic Meiser delete (*trifactors)->workVector; 19827f756511SDominic Meiser if (handle = (*trifactors)->handle) { 198357d48284SJunchao Zhang stat = cusparseDestroy(handle);CHKERRCUSPARSE(stat); 19847f756511SDominic Meiser } 19857f756511SDominic Meiser delete *trifactors; 19867f756511SDominic Meiser *trifactors = 0; 19877f756511SDominic Meiser } 19887f756511SDominic Meiser PetscFunctionReturn(0); 19897f756511SDominic Meiser } 19907f756511SDominic Meiser 1991