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; 50c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetStream(cusparsestruct->handle,cusparsestruct->stream);CHKERRCUDA(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) { 62c41cb2e2SAlejandro Lamas Daviña stat = cusparseDestroy(cusparsestruct->handle);CHKERRCUDA(stat); 6316a2e217SAlejandro Lamas Daviña } 64b06137fdSPaul Mullowney cusparsestruct->handle = handle; 656b1cf21dSAlejandro Lamas Daviña } 66c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(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; 2489ae82921SPaul Mullowney 2499ae82921SPaul Mullowney PetscFunctionBegin; 250cf00fe3bSKarl Rupp if (!n) PetscFunctionReturn(0); 251*c70f7ee4SJunchao Zhang if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) { 2529ae82921SPaul Mullowney try { 2539ae82921SPaul Mullowney /* first figure out the number of nonzeros in the lower triangular matrix including 1's on the diagonal. */ 2549ae82921SPaul Mullowney nzLower=n+ai[n]-ai[1]; 2559ae82921SPaul Mullowney 2569ae82921SPaul Mullowney /* Allocate Space for the lower triangular matrix */ 257c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AiLo, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr); 258c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AjLo, nzLower*sizeof(PetscInt));CHKERRCUDA(ierr); 259c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AALo, nzLower*sizeof(PetscScalar));CHKERRCUDA(ierr); 2609ae82921SPaul Mullowney 2619ae82921SPaul Mullowney /* Fill the lower triangular matrix */ 2629ae82921SPaul Mullowney AiLo[0] = (PetscInt) 0; 2639ae82921SPaul Mullowney AiLo[n] = nzLower; 2649ae82921SPaul Mullowney AjLo[0] = (PetscInt) 0; 2659ae82921SPaul Mullowney AALo[0] = (MatScalar) 1.0; 2669ae82921SPaul Mullowney v = aa; 2679ae82921SPaul Mullowney vi = aj; 2689ae82921SPaul Mullowney offset = 1; 2699ae82921SPaul Mullowney rowOffset= 1; 2709ae82921SPaul Mullowney for (i=1; i<n; i++) { 2719ae82921SPaul Mullowney nz = ai[i+1] - ai[i]; 272e057df02SPaul Mullowney /* additional 1 for the term on the diagonal */ 2739ae82921SPaul Mullowney AiLo[i] = rowOffset; 2749ae82921SPaul Mullowney rowOffset += nz+1; 2759ae82921SPaul Mullowney 276580bdb30SBarry Smith ierr = PetscArraycpy(&(AjLo[offset]), vi, nz);CHKERRQ(ierr); 277580bdb30SBarry Smith ierr = PetscArraycpy(&(AALo[offset]), v, nz);CHKERRQ(ierr); 2789ae82921SPaul Mullowney 2799ae82921SPaul Mullowney offset += nz; 2809ae82921SPaul Mullowney AjLo[offset] = (PetscInt) i; 2819ae82921SPaul Mullowney AALo[offset] = (MatScalar) 1.0; 2829ae82921SPaul Mullowney offset += 1; 2839ae82921SPaul Mullowney 2849ae82921SPaul Mullowney v += nz; 2859ae82921SPaul Mullowney vi += nz; 2869ae82921SPaul Mullowney } 2872205254eSKarl Rupp 288aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 289aa372e3fSPaul Mullowney loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 2902205254eSKarl Rupp 291aa372e3fSPaul Mullowney /* Create the matrix description */ 292c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUDA(stat); 293c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 294c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat); 295c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_LOWER);CHKERRCUDA(stat); 296c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUDA(stat); 297aa372e3fSPaul Mullowney 298aa372e3fSPaul Mullowney /* Create the solve analysis information */ 299c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUDA(stat); 300aa372e3fSPaul Mullowney 301aa372e3fSPaul Mullowney /* set the operation */ 302aa372e3fSPaul Mullowney loTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 303aa372e3fSPaul Mullowney 304aa372e3fSPaul Mullowney /* set the matrix */ 305aa372e3fSPaul Mullowney loTriFactor->csrMat = new CsrMatrix; 306aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows = n; 307aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols = n; 308aa372e3fSPaul Mullowney loTriFactor->csrMat->num_entries = nzLower; 309aa372e3fSPaul Mullowney 310aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1); 311aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->assign(AiLo, AiLo+n+1); 312aa372e3fSPaul Mullowney 313aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzLower); 314aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->assign(AjLo, AjLo+nzLower); 315aa372e3fSPaul Mullowney 316aa372e3fSPaul Mullowney loTriFactor->csrMat->values = new THRUSTARRAY(nzLower); 317aa372e3fSPaul Mullowney loTriFactor->csrMat->values->assign(AALo, AALo+nzLower); 318aa372e3fSPaul Mullowney 319aa372e3fSPaul Mullowney /* perform the solve analysis */ 320aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp, 321aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr, 322aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(), 323c41cb2e2SAlejandro Lamas Daviña loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUDA(stat); 324aa372e3fSPaul Mullowney 325aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 326aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor; 3272205254eSKarl Rupp 328c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AiLo);CHKERRCUDA(ierr); 329c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AjLo);CHKERRCUDA(ierr); 330c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AALo);CHKERRCUDA(ierr); 3314863603aSSatish Balay ierr = PetscLogCpuToGpu((n+1+nzLower)*sizeof(int)+nzLower*sizeof(PetscScalar));CHKERRQ(ierr); 3329ae82921SPaul Mullowney } catch(char *ex) { 3339ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 3349ae82921SPaul Mullowney } 3359ae82921SPaul Mullowney } 3369ae82921SPaul Mullowney PetscFunctionReturn(0); 3379ae82921SPaul Mullowney } 3389ae82921SPaul Mullowney 339087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(Mat A) 3409ae82921SPaul Mullowney { 3419ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3429ae82921SPaul Mullowney PetscInt n = A->rmap->n; 3439ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 344aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 3459ae82921SPaul Mullowney cusparseStatus_t stat; 3469ae82921SPaul Mullowney const PetscInt *aj = a->j,*adiag = a->diag,*vi; 3479ae82921SPaul Mullowney const MatScalar *aa = a->a,*v; 3489ae82921SPaul Mullowney PetscInt *AiUp, *AjUp; 3499ae82921SPaul Mullowney PetscScalar *AAUp; 3509ae82921SPaul Mullowney PetscInt i,nz, nzUpper, offset; 3519ae82921SPaul Mullowney PetscErrorCode ierr; 3529ae82921SPaul Mullowney 3539ae82921SPaul Mullowney PetscFunctionBegin; 354cf00fe3bSKarl Rupp if (!n) PetscFunctionReturn(0); 355*c70f7ee4SJunchao Zhang if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) { 3569ae82921SPaul Mullowney try { 3579ae82921SPaul Mullowney /* next, figure out the number of nonzeros in the upper triangular matrix. */ 3589ae82921SPaul Mullowney nzUpper = adiag[0]-adiag[n]; 3599ae82921SPaul Mullowney 3609ae82921SPaul Mullowney /* Allocate Space for the upper triangular matrix */ 361c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr); 362c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(ierr); 363c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr); 3649ae82921SPaul Mullowney 3659ae82921SPaul Mullowney /* Fill the upper triangular matrix */ 3669ae82921SPaul Mullowney AiUp[0]=(PetscInt) 0; 3679ae82921SPaul Mullowney AiUp[n]=nzUpper; 3689ae82921SPaul Mullowney offset = nzUpper; 3699ae82921SPaul Mullowney for (i=n-1; i>=0; i--) { 3709ae82921SPaul Mullowney v = aa + adiag[i+1] + 1; 3719ae82921SPaul Mullowney vi = aj + adiag[i+1] + 1; 3729ae82921SPaul Mullowney 373e057df02SPaul Mullowney /* number of elements NOT on the diagonal */ 3749ae82921SPaul Mullowney nz = adiag[i] - adiag[i+1]-1; 3759ae82921SPaul Mullowney 376e057df02SPaul Mullowney /* decrement the offset */ 3779ae82921SPaul Mullowney offset -= (nz+1); 3789ae82921SPaul Mullowney 379e057df02SPaul Mullowney /* first, set the diagonal elements */ 3809ae82921SPaul Mullowney AjUp[offset] = (PetscInt) i; 38109f51544SAlejandro Lamas Daviña AAUp[offset] = (MatScalar)1./v[nz]; 3829ae82921SPaul Mullowney AiUp[i] = AiUp[i+1] - (nz+1); 3839ae82921SPaul Mullowney 384580bdb30SBarry Smith ierr = PetscArraycpy(&(AjUp[offset+1]), vi, nz);CHKERRQ(ierr); 385580bdb30SBarry Smith ierr = PetscArraycpy(&(AAUp[offset+1]), v, nz);CHKERRQ(ierr); 3869ae82921SPaul Mullowney } 3872205254eSKarl Rupp 388aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 389aa372e3fSPaul Mullowney upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 3902205254eSKarl Rupp 391aa372e3fSPaul Mullowney /* Create the matrix description */ 392c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUDA(stat); 393c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 394c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat); 395c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat); 396c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUDA(stat); 397aa372e3fSPaul Mullowney 398aa372e3fSPaul Mullowney /* Create the solve analysis information */ 399c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUDA(stat); 400aa372e3fSPaul Mullowney 401aa372e3fSPaul Mullowney /* set the operation */ 402aa372e3fSPaul Mullowney upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 403aa372e3fSPaul Mullowney 404aa372e3fSPaul Mullowney /* set the matrix */ 405aa372e3fSPaul Mullowney upTriFactor->csrMat = new CsrMatrix; 406aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows = n; 407aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols = n; 408aa372e3fSPaul Mullowney upTriFactor->csrMat->num_entries = nzUpper; 409aa372e3fSPaul Mullowney 410aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1); 411aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+n+1); 412aa372e3fSPaul Mullowney 413aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzUpper); 414aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+nzUpper); 415aa372e3fSPaul Mullowney 416aa372e3fSPaul Mullowney upTriFactor->csrMat->values = new THRUSTARRAY(nzUpper); 417aa372e3fSPaul Mullowney upTriFactor->csrMat->values->assign(AAUp, AAUp+nzUpper); 418aa372e3fSPaul Mullowney 419aa372e3fSPaul Mullowney /* perform the solve analysis */ 420aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp, 421aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr, 422aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(), 423c41cb2e2SAlejandro Lamas Daviña upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUDA(stat); 424aa372e3fSPaul Mullowney 425aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 426aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor; 4272205254eSKarl Rupp 428c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AiUp);CHKERRCUDA(ierr); 429c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AjUp);CHKERRCUDA(ierr); 430c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AAUp);CHKERRCUDA(ierr); 4314863603aSSatish Balay ierr = PetscLogCpuToGpu((n+1+nzUpper)*sizeof(int)+nzUpper*sizeof(PetscScalar));CHKERRQ(ierr); 4329ae82921SPaul Mullowney } catch(char *ex) { 4339ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 4349ae82921SPaul Mullowney } 4359ae82921SPaul Mullowney } 4369ae82921SPaul Mullowney PetscFunctionReturn(0); 4379ae82921SPaul Mullowney } 4389ae82921SPaul Mullowney 439087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(Mat A) 4409ae82921SPaul Mullowney { 4419ae82921SPaul Mullowney PetscErrorCode ierr; 4429ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 4439ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 4449ae82921SPaul Mullowney IS isrow = a->row,iscol = a->icol; 4459ae82921SPaul Mullowney PetscBool row_identity,col_identity; 4469ae82921SPaul Mullowney const PetscInt *r,*c; 4479ae82921SPaul Mullowney PetscInt n = A->rmap->n; 4489ae82921SPaul Mullowney 4499ae82921SPaul Mullowney PetscFunctionBegin; 450087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildILULowerTriMatrix(A);CHKERRQ(ierr); 451087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(A);CHKERRQ(ierr); 4522205254eSKarl Rupp 453e65717acSKarl Rupp cusparseTriFactors->workVector = new THRUSTARRAY(n); 454aa372e3fSPaul Mullowney cusparseTriFactors->nnz=a->nz; 4559ae82921SPaul Mullowney 456*c70f7ee4SJunchao Zhang A->offloadmask = PETSC_OFFLOAD_BOTH; 457e057df02SPaul Mullowney /* lower triangular indices */ 4589ae82921SPaul Mullowney ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 4599ae82921SPaul Mullowney ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 4602205254eSKarl Rupp if (!row_identity) { 461aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n); 462aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices->assign(r, r+n); 4632205254eSKarl Rupp } 4649ae82921SPaul Mullowney ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 4659ae82921SPaul Mullowney 466e057df02SPaul Mullowney /* upper triangular indices */ 4679ae82921SPaul Mullowney ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); 4689ae82921SPaul Mullowney ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 4692205254eSKarl Rupp if (!col_identity) { 470aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n); 471aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices->assign(c, c+n); 4722205254eSKarl Rupp } 4734863603aSSatish Balay 4744863603aSSatish Balay if (!row_identity && !col_identity) { 4754863603aSSatish Balay ierr = PetscLogCpuToGpu(2*n*sizeof(PetscInt));CHKERRQ(ierr); 4764863603aSSatish Balay } else if(!row_identity) { 4774863603aSSatish Balay ierr = PetscLogCpuToGpu(n*sizeof(PetscInt));CHKERRQ(ierr); 4784863603aSSatish Balay } else if(!col_identity) { 4794863603aSSatish Balay ierr = PetscLogCpuToGpu(n*sizeof(PetscInt));CHKERRQ(ierr); 4804863603aSSatish Balay } 4814863603aSSatish Balay 4829ae82921SPaul Mullowney ierr = ISRestoreIndices(iscol,&c);CHKERRQ(ierr); 4839ae82921SPaul Mullowney PetscFunctionReturn(0); 4849ae82921SPaul Mullowney } 4859ae82921SPaul Mullowney 486087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildICCTriMatrices(Mat A) 487087f3262SPaul Mullowney { 488087f3262SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 489087f3262SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 490aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 491aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 492087f3262SPaul Mullowney cusparseStatus_t stat; 493087f3262SPaul Mullowney PetscErrorCode ierr; 494087f3262SPaul Mullowney PetscInt *AiUp, *AjUp; 495087f3262SPaul Mullowney PetscScalar *AAUp; 496087f3262SPaul Mullowney PetscScalar *AALo; 497087f3262SPaul Mullowney PetscInt nzUpper = a->nz,n = A->rmap->n,i,offset,nz,j; 498087f3262SPaul Mullowney Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)A->data; 499087f3262SPaul Mullowney const PetscInt *ai = b->i,*aj = b->j,*vj; 500087f3262SPaul Mullowney const MatScalar *aa = b->a,*v; 501087f3262SPaul Mullowney 502087f3262SPaul Mullowney PetscFunctionBegin; 503cf00fe3bSKarl Rupp if (!n) PetscFunctionReturn(0); 504*c70f7ee4SJunchao Zhang if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) { 505087f3262SPaul Mullowney try { 506087f3262SPaul Mullowney /* Allocate Space for the upper triangular matrix */ 507c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr); 508c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(ierr); 509c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr); 510c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AALo, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr); 511087f3262SPaul Mullowney 512087f3262SPaul Mullowney /* Fill the upper triangular matrix */ 513087f3262SPaul Mullowney AiUp[0]=(PetscInt) 0; 514087f3262SPaul Mullowney AiUp[n]=nzUpper; 515087f3262SPaul Mullowney offset = 0; 516087f3262SPaul Mullowney for (i=0; i<n; i++) { 517087f3262SPaul Mullowney /* set the pointers */ 518087f3262SPaul Mullowney v = aa + ai[i]; 519087f3262SPaul Mullowney vj = aj + ai[i]; 520087f3262SPaul Mullowney nz = ai[i+1] - ai[i] - 1; /* exclude diag[i] */ 521087f3262SPaul Mullowney 522087f3262SPaul Mullowney /* first, set the diagonal elements */ 523087f3262SPaul Mullowney AjUp[offset] = (PetscInt) i; 52409f51544SAlejandro Lamas Daviña AAUp[offset] = (MatScalar)1.0/v[nz]; 525087f3262SPaul Mullowney AiUp[i] = offset; 52609f51544SAlejandro Lamas Daviña AALo[offset] = (MatScalar)1.0/v[nz]; 527087f3262SPaul Mullowney 528087f3262SPaul Mullowney offset+=1; 529087f3262SPaul Mullowney if (nz>0) { 530f22e0265SBarry Smith ierr = PetscArraycpy(&(AjUp[offset]), vj, nz);CHKERRQ(ierr); 531580bdb30SBarry Smith ierr = PetscArraycpy(&(AAUp[offset]), v, nz);CHKERRQ(ierr); 532087f3262SPaul Mullowney for (j=offset; j<offset+nz; j++) { 533087f3262SPaul Mullowney AAUp[j] = -AAUp[j]; 534087f3262SPaul Mullowney AALo[j] = AAUp[j]/v[nz]; 535087f3262SPaul Mullowney } 536087f3262SPaul Mullowney offset+=nz; 537087f3262SPaul Mullowney } 538087f3262SPaul Mullowney } 539087f3262SPaul Mullowney 540aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 541aa372e3fSPaul Mullowney upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 542087f3262SPaul Mullowney 543aa372e3fSPaul Mullowney /* Create the matrix description */ 544c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUDA(stat); 545c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 546c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat); 547c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat); 548c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUDA(stat); 549087f3262SPaul Mullowney 550aa372e3fSPaul Mullowney /* Create the solve analysis information */ 551c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUDA(stat); 552aa372e3fSPaul Mullowney 553aa372e3fSPaul Mullowney /* set the operation */ 554aa372e3fSPaul Mullowney upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 555aa372e3fSPaul Mullowney 556aa372e3fSPaul Mullowney /* set the matrix */ 557aa372e3fSPaul Mullowney upTriFactor->csrMat = new CsrMatrix; 558aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows = A->rmap->n; 559aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols = A->cmap->n; 560aa372e3fSPaul Mullowney upTriFactor->csrMat->num_entries = a->nz; 561aa372e3fSPaul Mullowney 562aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 563aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1); 564aa372e3fSPaul Mullowney 565aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz); 566aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz); 567aa372e3fSPaul Mullowney 568aa372e3fSPaul Mullowney upTriFactor->csrMat->values = new THRUSTARRAY(a->nz); 569aa372e3fSPaul Mullowney upTriFactor->csrMat->values->assign(AAUp, AAUp+a->nz); 570aa372e3fSPaul Mullowney 571aa372e3fSPaul Mullowney /* perform the solve analysis */ 572aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp, 573aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr, 574aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(), 575c41cb2e2SAlejandro Lamas Daviña upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUDA(stat); 576aa372e3fSPaul Mullowney 577aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 578aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor; 579aa372e3fSPaul Mullowney 580aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 581aa372e3fSPaul Mullowney loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 582aa372e3fSPaul Mullowney 583aa372e3fSPaul Mullowney /* Create the matrix description */ 584c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUDA(stat); 585c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 586c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat); 587c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat); 588c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUDA(stat); 589aa372e3fSPaul Mullowney 590aa372e3fSPaul Mullowney /* Create the solve analysis information */ 591c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUDA(stat); 592aa372e3fSPaul Mullowney 593aa372e3fSPaul Mullowney /* set the operation */ 594aa372e3fSPaul Mullowney loTriFactor->solveOp = CUSPARSE_OPERATION_TRANSPOSE; 595aa372e3fSPaul Mullowney 596aa372e3fSPaul Mullowney /* set the matrix */ 597aa372e3fSPaul Mullowney loTriFactor->csrMat = new CsrMatrix; 598aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows = A->rmap->n; 599aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols = A->cmap->n; 600aa372e3fSPaul Mullowney loTriFactor->csrMat->num_entries = a->nz; 601aa372e3fSPaul Mullowney 602aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 603aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1); 604aa372e3fSPaul Mullowney 605aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz); 606aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz); 607aa372e3fSPaul Mullowney 608aa372e3fSPaul Mullowney loTriFactor->csrMat->values = new THRUSTARRAY(a->nz); 609aa372e3fSPaul Mullowney loTriFactor->csrMat->values->assign(AALo, AALo+a->nz); 6104863603aSSatish Balay ierr = PetscLogCpuToGpu(2*(((A->rmap->n+1)+(a->nz))*sizeof(int)+(a->nz)*sizeof(PetscScalar)));CHKERRQ(ierr); 611aa372e3fSPaul Mullowney 612aa372e3fSPaul Mullowney /* perform the solve analysis */ 613aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp, 614aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr, 615aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(), 616c41cb2e2SAlejandro Lamas Daviña loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUDA(stat); 617aa372e3fSPaul Mullowney 618aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 619aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor; 620087f3262SPaul Mullowney 621*c70f7ee4SJunchao Zhang A->offloadmask = PETSC_OFFLOAD_BOTH; 622c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AiUp);CHKERRCUDA(ierr); 623c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AjUp);CHKERRCUDA(ierr); 624c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AAUp);CHKERRCUDA(ierr); 625c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AALo);CHKERRCUDA(ierr); 626087f3262SPaul Mullowney } catch(char *ex) { 627087f3262SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 628087f3262SPaul Mullowney } 629087f3262SPaul Mullowney } 630087f3262SPaul Mullowney PetscFunctionReturn(0); 631087f3262SPaul Mullowney } 632087f3262SPaul Mullowney 633087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(Mat A) 6349ae82921SPaul Mullowney { 6359ae82921SPaul Mullowney PetscErrorCode ierr; 636087f3262SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 637087f3262SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 638087f3262SPaul Mullowney IS ip = a->row; 639087f3262SPaul Mullowney const PetscInt *rip; 640087f3262SPaul Mullowney PetscBool perm_identity; 641087f3262SPaul Mullowney PetscInt n = A->rmap->n; 642087f3262SPaul Mullowney 643087f3262SPaul Mullowney PetscFunctionBegin; 644087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildICCTriMatrices(A);CHKERRQ(ierr); 645e65717acSKarl Rupp cusparseTriFactors->workVector = new THRUSTARRAY(n); 646aa372e3fSPaul Mullowney cusparseTriFactors->nnz=(a->nz-n)*2 + n; 647aa372e3fSPaul Mullowney 648087f3262SPaul Mullowney /* lower triangular indices */ 649087f3262SPaul Mullowney ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); 650087f3262SPaul Mullowney ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 651087f3262SPaul Mullowney if (!perm_identity) { 6524e4bbfaaSStefano Zampini IS iip; 6534e4bbfaaSStefano Zampini const PetscInt *irip; 6544e4bbfaaSStefano Zampini 6554e4bbfaaSStefano Zampini ierr = ISInvertPermutation(ip,PETSC_DECIDE,&iip);CHKERRQ(ierr); 6564e4bbfaaSStefano Zampini ierr = ISGetIndices(iip,&irip);CHKERRQ(ierr); 657aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n); 658aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices->assign(rip, rip+n); 659aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n); 6604e4bbfaaSStefano Zampini cusparseTriFactors->cpermIndices->assign(irip, irip+n); 6614e4bbfaaSStefano Zampini ierr = ISRestoreIndices(iip,&irip);CHKERRQ(ierr); 6624e4bbfaaSStefano Zampini ierr = ISDestroy(&iip);CHKERRQ(ierr); 6634863603aSSatish Balay ierr = PetscLogCpuToGpu(2*n*sizeof(PetscInt));CHKERRQ(ierr); 664087f3262SPaul Mullowney } 665087f3262SPaul Mullowney ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); 666087f3262SPaul Mullowney PetscFunctionReturn(0); 667087f3262SPaul Mullowney } 668087f3262SPaul Mullowney 6696fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info) 6709ae82921SPaul Mullowney { 6719ae82921SPaul Mullowney Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 6729ae82921SPaul Mullowney IS isrow = b->row,iscol = b->col; 6739ae82921SPaul Mullowney PetscBool row_identity,col_identity; 674b175d8bbSPaul Mullowney PetscErrorCode ierr; 6759ae82921SPaul Mullowney 6769ae82921SPaul Mullowney PetscFunctionBegin; 6779ae82921SPaul Mullowney ierr = MatLUFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr); 678e057df02SPaul Mullowney /* determine which version of MatSolve needs to be used. */ 6799ae82921SPaul Mullowney ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 6809ae82921SPaul Mullowney ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 681bda325fcSPaul Mullowney if (row_identity && col_identity) { 682bda325fcSPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering; 683bda325fcSPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering; 6844e4bbfaaSStefano Zampini B->ops->matsolve = NULL; 6854e4bbfaaSStefano Zampini B->ops->matsolvetranspose = NULL; 686bda325fcSPaul Mullowney } else { 687bda325fcSPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE; 688bda325fcSPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE; 6894e4bbfaaSStefano Zampini B->ops->matsolve = NULL; 6904e4bbfaaSStefano Zampini B->ops->matsolvetranspose = NULL; 691bda325fcSPaul Mullowney } 6928dc1d2a3SPaul Mullowney 693e057df02SPaul Mullowney /* get the triangular factors */ 694087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(B);CHKERRQ(ierr); 6959ae82921SPaul Mullowney PetscFunctionReturn(0); 6969ae82921SPaul Mullowney } 6979ae82921SPaul Mullowney 698087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info) 699087f3262SPaul Mullowney { 700087f3262SPaul Mullowney Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 701087f3262SPaul Mullowney IS ip = b->row; 702087f3262SPaul Mullowney PetscBool perm_identity; 703b175d8bbSPaul Mullowney PetscErrorCode ierr; 704087f3262SPaul Mullowney 705087f3262SPaul Mullowney PetscFunctionBegin; 706087f3262SPaul Mullowney ierr = MatCholeskyFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr); 707087f3262SPaul Mullowney 708087f3262SPaul Mullowney /* determine which version of MatSolve needs to be used. */ 709087f3262SPaul Mullowney ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 710087f3262SPaul Mullowney if (perm_identity) { 711087f3262SPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering; 712087f3262SPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering; 7134e4bbfaaSStefano Zampini B->ops->matsolve = NULL; 7144e4bbfaaSStefano Zampini B->ops->matsolvetranspose = NULL; 715087f3262SPaul Mullowney } else { 716087f3262SPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE; 717087f3262SPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE; 7184e4bbfaaSStefano Zampini B->ops->matsolve = NULL; 7194e4bbfaaSStefano Zampini B->ops->matsolvetranspose = NULL; 720087f3262SPaul Mullowney } 721087f3262SPaul Mullowney 722087f3262SPaul Mullowney /* get the triangular factors */ 723087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(B);CHKERRQ(ierr); 724087f3262SPaul Mullowney PetscFunctionReturn(0); 725087f3262SPaul Mullowney } 7269ae82921SPaul Mullowney 727b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(Mat A) 728bda325fcSPaul Mullowney { 729bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 730aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 731aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 732aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 733aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 734bda325fcSPaul Mullowney cusparseStatus_t stat; 735aa372e3fSPaul Mullowney cusparseIndexBase_t indexBase; 736aa372e3fSPaul Mullowney cusparseMatrixType_t matrixType; 737aa372e3fSPaul Mullowney cusparseFillMode_t fillMode; 738aa372e3fSPaul Mullowney cusparseDiagType_t diagType; 739b175d8bbSPaul Mullowney 740bda325fcSPaul Mullowney PetscFunctionBegin; 741bda325fcSPaul Mullowney 742aa372e3fSPaul Mullowney /*********************************************/ 743aa372e3fSPaul Mullowney /* Now the Transpose of the Lower Tri Factor */ 744aa372e3fSPaul Mullowney /*********************************************/ 745aa372e3fSPaul Mullowney 746aa372e3fSPaul Mullowney /* allocate space for the transpose of the lower triangular factor */ 747aa372e3fSPaul Mullowney loTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct; 748aa372e3fSPaul Mullowney 749aa372e3fSPaul Mullowney /* set the matrix descriptors of the lower triangular factor */ 750aa372e3fSPaul Mullowney matrixType = cusparseGetMatType(loTriFactor->descr); 751aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(loTriFactor->descr); 752aa372e3fSPaul Mullowney fillMode = cusparseGetMatFillMode(loTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ? 753aa372e3fSPaul Mullowney CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER; 754aa372e3fSPaul Mullowney diagType = cusparseGetMatDiagType(loTriFactor->descr); 755aa372e3fSPaul Mullowney 756aa372e3fSPaul Mullowney /* Create the matrix description */ 757c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&loTriFactorT->descr);CHKERRCUDA(stat); 758c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(loTriFactorT->descr, indexBase);CHKERRCUDA(stat); 759c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(loTriFactorT->descr, matrixType);CHKERRCUDA(stat); 760c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(loTriFactorT->descr, fillMode);CHKERRCUDA(stat); 761c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(loTriFactorT->descr, diagType);CHKERRCUDA(stat); 762aa372e3fSPaul Mullowney 763aa372e3fSPaul Mullowney /* Create the solve analysis information */ 764c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&loTriFactorT->solveInfo);CHKERRCUDA(stat); 765aa372e3fSPaul Mullowney 766aa372e3fSPaul Mullowney /* set the operation */ 767aa372e3fSPaul Mullowney loTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 768aa372e3fSPaul Mullowney 769aa372e3fSPaul Mullowney /* allocate GPU space for the CSC of the lower triangular factor*/ 770aa372e3fSPaul Mullowney loTriFactorT->csrMat = new CsrMatrix; 771aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_rows = loTriFactor->csrMat->num_rows; 772aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_cols = loTriFactor->csrMat->num_cols; 773aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_entries = loTriFactor->csrMat->num_entries; 774aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(loTriFactor->csrMat->num_rows+1); 775aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(loTriFactor->csrMat->num_entries); 776aa372e3fSPaul Mullowney loTriFactorT->csrMat->values = new THRUSTARRAY(loTriFactor->csrMat->num_entries); 777aa372e3fSPaul Mullowney 778aa372e3fSPaul Mullowney /* compute the transpose of the lower triangular factor, i.e. the CSC */ 779aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparseTriFactors->handle, loTriFactor->csrMat->num_rows, 780aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols, loTriFactor->csrMat->num_entries, 781aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 782aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 783aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 784aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 785aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 786aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 787c41cb2e2SAlejandro Lamas Daviña CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat); 788aa372e3fSPaul Mullowney 789aa372e3fSPaul Mullowney /* perform the solve analysis on the transposed matrix */ 790aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactorT->solveOp, 791aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_rows, loTriFactorT->csrMat->num_entries, 792aa372e3fSPaul Mullowney loTriFactorT->descr, loTriFactorT->csrMat->values->data().get(), 793aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), loTriFactorT->csrMat->column_indices->data().get(), 794c41cb2e2SAlejandro Lamas Daviña loTriFactorT->solveInfo);CHKERRCUDA(stat); 795aa372e3fSPaul Mullowney 796aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 797aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtrTranspose = loTriFactorT; 798aa372e3fSPaul Mullowney 799aa372e3fSPaul Mullowney /*********************************************/ 800aa372e3fSPaul Mullowney /* Now the Transpose of the Upper Tri Factor */ 801aa372e3fSPaul Mullowney /*********************************************/ 802aa372e3fSPaul Mullowney 803aa372e3fSPaul Mullowney /* allocate space for the transpose of the upper triangular factor */ 804aa372e3fSPaul Mullowney upTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct; 805aa372e3fSPaul Mullowney 806aa372e3fSPaul Mullowney /* set the matrix descriptors of the upper triangular factor */ 807aa372e3fSPaul Mullowney matrixType = cusparseGetMatType(upTriFactor->descr); 808aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(upTriFactor->descr); 809aa372e3fSPaul Mullowney fillMode = cusparseGetMatFillMode(upTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ? 810aa372e3fSPaul Mullowney CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER; 811aa372e3fSPaul Mullowney diagType = cusparseGetMatDiagType(upTriFactor->descr); 812aa372e3fSPaul Mullowney 813aa372e3fSPaul Mullowney /* Create the matrix description */ 814c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&upTriFactorT->descr);CHKERRCUDA(stat); 815c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(upTriFactorT->descr, indexBase);CHKERRCUDA(stat); 816c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(upTriFactorT->descr, matrixType);CHKERRCUDA(stat); 817c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(upTriFactorT->descr, fillMode);CHKERRCUDA(stat); 818c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(upTriFactorT->descr, diagType);CHKERRCUDA(stat); 819aa372e3fSPaul Mullowney 820aa372e3fSPaul Mullowney /* Create the solve analysis information */ 821c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&upTriFactorT->solveInfo);CHKERRCUDA(stat); 822aa372e3fSPaul Mullowney 823aa372e3fSPaul Mullowney /* set the operation */ 824aa372e3fSPaul Mullowney upTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 825aa372e3fSPaul Mullowney 826aa372e3fSPaul Mullowney /* allocate GPU space for the CSC of the upper triangular factor*/ 827aa372e3fSPaul Mullowney upTriFactorT->csrMat = new CsrMatrix; 828aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_rows = upTriFactor->csrMat->num_rows; 829aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_cols = upTriFactor->csrMat->num_cols; 830aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_entries = upTriFactor->csrMat->num_entries; 831aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(upTriFactor->csrMat->num_rows+1); 832aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(upTriFactor->csrMat->num_entries); 833aa372e3fSPaul Mullowney upTriFactorT->csrMat->values = new THRUSTARRAY(upTriFactor->csrMat->num_entries); 834aa372e3fSPaul Mullowney 835aa372e3fSPaul Mullowney /* compute the transpose of the upper triangular factor, i.e. the CSC */ 836aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparseTriFactors->handle, upTriFactor->csrMat->num_rows, 837aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols, upTriFactor->csrMat->num_entries, 838aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 839aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 840aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 841aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 842aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 843aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 844c41cb2e2SAlejandro Lamas Daviña CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat); 845aa372e3fSPaul Mullowney 846aa372e3fSPaul Mullowney /* perform the solve analysis on the transposed matrix */ 847aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactorT->solveOp, 848aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_rows, upTriFactorT->csrMat->num_entries, 849aa372e3fSPaul Mullowney upTriFactorT->descr, upTriFactorT->csrMat->values->data().get(), 850aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), upTriFactorT->csrMat->column_indices->data().get(), 851c41cb2e2SAlejandro Lamas Daviña upTriFactorT->solveInfo);CHKERRCUDA(stat); 852aa372e3fSPaul Mullowney 853aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 854aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtrTranspose = upTriFactorT; 855bda325fcSPaul Mullowney PetscFunctionReturn(0); 856bda325fcSPaul Mullowney } 857bda325fcSPaul Mullowney 858b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEGenerateTransposeForMult(Mat A) 859bda325fcSPaul Mullowney { 860aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 861aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 862aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 863bda325fcSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 864bda325fcSPaul Mullowney cusparseStatus_t stat; 865aa372e3fSPaul Mullowney cusparseIndexBase_t indexBase; 866b06137fdSPaul Mullowney cudaError_t err; 8674863603aSSatish Balay PetscErrorCode ierr; 868b175d8bbSPaul Mullowney 869bda325fcSPaul Mullowney PetscFunctionBegin; 870aa372e3fSPaul Mullowney 871aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 872aa372e3fSPaul Mullowney matstructT = new Mat_SeqAIJCUSPARSEMultStruct; 873c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&matstructT->descr);CHKERRCUDA(stat); 874aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(matstruct->descr); 875c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(matstructT->descr, indexBase);CHKERRCUDA(stat); 876c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(matstructT->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUDA(stat); 877aa372e3fSPaul Mullowney 878b06137fdSPaul Mullowney /* set alpha and beta */ 879c41cb2e2SAlejandro Lamas Daviña err = cudaMalloc((void **)&(matstructT->alpha), sizeof(PetscScalar));CHKERRCUDA(err); 8807656d835SStefano Zampini err = cudaMalloc((void **)&(matstructT->beta_zero),sizeof(PetscScalar));CHKERRCUDA(err); 8817656d835SStefano Zampini err = cudaMalloc((void **)&(matstructT->beta_one), sizeof(PetscScalar));CHKERRCUDA(err); 8827656d835SStefano Zampini err = cudaMemcpy(matstructT->alpha, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 8837656d835SStefano Zampini err = cudaMemcpy(matstructT->beta_zero,&PETSC_CUSPARSE_ZERO,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 8847656d835SStefano Zampini err = cudaMemcpy(matstructT->beta_one, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 885c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat); 886b06137fdSPaul Mullowney 887aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 888aa372e3fSPaul Mullowney CsrMatrix *matrix = (CsrMatrix*)matstruct->mat; 889aa372e3fSPaul Mullowney CsrMatrix *matrixT= new CsrMatrix; 890a3fdcf43SKarl Rupp thrust::device_vector<int> *rowoffsets_gpu; 891a3fdcf43SKarl Rupp const int ncomprow = matstruct->cprowIndices->size(); 892a3fdcf43SKarl Rupp thrust::host_vector<int> cprow(ncomprow); 893a3fdcf43SKarl Rupp cprow = *matstruct->cprowIndices; // GPU --> CPU 894554b8892SKarl Rupp matrixT->num_rows = A->cmap->n; 895554b8892SKarl Rupp matrixT->num_cols = A->rmap->n; 896aa372e3fSPaul Mullowney matrixT->num_entries = a->nz; 897a8bd5306SMark Adams matrixT->row_offsets = new THRUSTINTARRAY32(matrixT->num_rows+1); 898aa372e3fSPaul Mullowney matrixT->column_indices = new THRUSTINTARRAY32(a->nz); 899aa372e3fSPaul Mullowney matrixT->values = new THRUSTARRAY(a->nz); 900a3fdcf43SKarl Rupp PetscInt k,i; 901a3fdcf43SKarl Rupp thrust::host_vector<int> rowst_host(A->rmap->n+1); 902a3fdcf43SKarl Rupp 903a3fdcf43SKarl Rupp /* expand compress rows, which is forced in constructor (MatSeqAIJCUSPARSECopyToGPU) */ 904a3fdcf43SKarl Rupp rowst_host[0] = 0; 905a3fdcf43SKarl Rupp for (k = 0, i = 0; i < A->rmap->n ; i++) { 906a3fdcf43SKarl Rupp if (k < ncomprow && i==cprow[k]) { 907a3fdcf43SKarl Rupp rowst_host[i+1] = a->i[i+1]; 908a3fdcf43SKarl Rupp k++; 909a3fdcf43SKarl Rupp } else rowst_host[i+1] = rowst_host[i]; 910a3fdcf43SKarl Rupp } 911a3fdcf43SKarl Rupp if (k!=ncomprow) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"%D != %D",k,ncomprow); 912a3fdcf43SKarl Rupp rowoffsets_gpu = new THRUSTINTARRAY32(A->rmap->n+1); 913a3fdcf43SKarl Rupp *rowoffsets_gpu = rowst_host; // CPU --> GPU 914aa372e3fSPaul Mullowney 915aa372e3fSPaul Mullowney /* compute the transpose of the upper triangular factor, i.e. the CSC */ 916aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(matstruct->descr); 917a3fdcf43SKarl Rupp stat = cusparse_csr2csc(cusparsestruct->handle, A->rmap->n, 918a3fdcf43SKarl Rupp A->cmap->n, matrix->num_entries, 919aa372e3fSPaul Mullowney matrix->values->data().get(), 920a3fdcf43SKarl Rupp rowoffsets_gpu->data().get(), 921aa372e3fSPaul Mullowney matrix->column_indices->data().get(), 922aa372e3fSPaul Mullowney matrixT->values->data().get(), 923aa372e3fSPaul Mullowney matrixT->column_indices->data().get(), 924aa372e3fSPaul Mullowney matrixT->row_offsets->data().get(), 925c41cb2e2SAlejandro Lamas Daviña CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat); 926aa372e3fSPaul Mullowney 927aa372e3fSPaul Mullowney /* assign the pointer */ 928aa372e3fSPaul Mullowney matstructT->mat = matrixT; 9294863603aSSatish Balay ierr = PetscLogCpuToGpu(((A->rmap->n+1)+(a->nz))*sizeof(int)+(3+a->nz)*sizeof(PetscScalar));CHKERRQ(ierr); 930aa372e3fSPaul Mullowney } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) { 931aa372e3fSPaul Mullowney /* First convert HYB to CSR */ 932aa372e3fSPaul Mullowney CsrMatrix *temp= new CsrMatrix; 933aa372e3fSPaul Mullowney temp->num_rows = A->rmap->n; 934aa372e3fSPaul Mullowney temp->num_cols = A->cmap->n; 935aa372e3fSPaul Mullowney temp->num_entries = a->nz; 936aa372e3fSPaul Mullowney temp->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 937aa372e3fSPaul Mullowney temp->column_indices = new THRUSTINTARRAY32(a->nz); 938aa372e3fSPaul Mullowney temp->values = new THRUSTARRAY(a->nz); 939aa372e3fSPaul Mullowney 9402692e278SPaul Mullowney 941aa372e3fSPaul Mullowney stat = cusparse_hyb2csr(cusparsestruct->handle, 942aa372e3fSPaul Mullowney matstruct->descr, (cusparseHybMat_t)matstruct->mat, 943aa372e3fSPaul Mullowney temp->values->data().get(), 944aa372e3fSPaul Mullowney temp->row_offsets->data().get(), 945c41cb2e2SAlejandro Lamas Daviña temp->column_indices->data().get());CHKERRCUDA(stat); 946aa372e3fSPaul Mullowney 947aa372e3fSPaul Mullowney /* Next, convert CSR to CSC (i.e. the matrix transpose) */ 948aa372e3fSPaul Mullowney CsrMatrix *tempT= new CsrMatrix; 949aa372e3fSPaul Mullowney tempT->num_rows = A->rmap->n; 950aa372e3fSPaul Mullowney tempT->num_cols = A->cmap->n; 951aa372e3fSPaul Mullowney tempT->num_entries = a->nz; 952aa372e3fSPaul Mullowney tempT->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 953aa372e3fSPaul Mullowney tempT->column_indices = new THRUSTINTARRAY32(a->nz); 954aa372e3fSPaul Mullowney tempT->values = new THRUSTARRAY(a->nz); 955aa372e3fSPaul Mullowney 956aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparsestruct->handle, temp->num_rows, 957aa372e3fSPaul Mullowney temp->num_cols, temp->num_entries, 958aa372e3fSPaul Mullowney temp->values->data().get(), 959aa372e3fSPaul Mullowney temp->row_offsets->data().get(), 960aa372e3fSPaul Mullowney temp->column_indices->data().get(), 961aa372e3fSPaul Mullowney tempT->values->data().get(), 962aa372e3fSPaul Mullowney tempT->column_indices->data().get(), 963aa372e3fSPaul Mullowney tempT->row_offsets->data().get(), 964c41cb2e2SAlejandro Lamas Daviña CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat); 965aa372e3fSPaul Mullowney 966aa372e3fSPaul Mullowney /* Last, convert CSC to HYB */ 967aa372e3fSPaul Mullowney cusparseHybMat_t hybMat; 968c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateHybMat(&hybMat);CHKERRCUDA(stat); 969aa372e3fSPaul Mullowney cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ? 970aa372e3fSPaul Mullowney CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO; 971aa372e3fSPaul Mullowney stat = cusparse_csr2hyb(cusparsestruct->handle, A->rmap->n, A->cmap->n, 972aa372e3fSPaul Mullowney matstructT->descr, tempT->values->data().get(), 973aa372e3fSPaul Mullowney tempT->row_offsets->data().get(), 974aa372e3fSPaul Mullowney tempT->column_indices->data().get(), 975c41cb2e2SAlejandro Lamas Daviña hybMat, 0, partition);CHKERRCUDA(stat); 976aa372e3fSPaul Mullowney 977aa372e3fSPaul Mullowney /* assign the pointer */ 978aa372e3fSPaul Mullowney matstructT->mat = hybMat; 9794863603aSSatish Balay ierr = PetscLogCpuToGpu((2*(((A->rmap->n+1)+(a->nz))*sizeof(int)+(a->nz)*sizeof(PetscScalar)))+3*sizeof(PetscScalar));CHKERRQ(ierr); 980aa372e3fSPaul Mullowney 981aa372e3fSPaul Mullowney /* delete temporaries */ 982aa372e3fSPaul Mullowney if (tempT) { 983aa372e3fSPaul Mullowney if (tempT->values) delete (THRUSTARRAY*) tempT->values; 984aa372e3fSPaul Mullowney if (tempT->column_indices) delete (THRUSTINTARRAY32*) tempT->column_indices; 985aa372e3fSPaul Mullowney if (tempT->row_offsets) delete (THRUSTINTARRAY32*) tempT->row_offsets; 986aa372e3fSPaul Mullowney delete (CsrMatrix*) tempT; 987087f3262SPaul Mullowney } 988aa372e3fSPaul Mullowney if (temp) { 989aa372e3fSPaul Mullowney if (temp->values) delete (THRUSTARRAY*) temp->values; 990aa372e3fSPaul Mullowney if (temp->column_indices) delete (THRUSTINTARRAY32*) temp->column_indices; 991aa372e3fSPaul Mullowney if (temp->row_offsets) delete (THRUSTINTARRAY32*) temp->row_offsets; 992aa372e3fSPaul Mullowney delete (CsrMatrix*) temp; 993aa372e3fSPaul Mullowney } 994aa372e3fSPaul Mullowney } 995aa372e3fSPaul Mullowney /* assign the compressed row indices */ 996aa372e3fSPaul Mullowney matstructT->cprowIndices = new THRUSTINTARRAY; 997554b8892SKarl Rupp matstructT->cprowIndices->resize(A->cmap->n); 998554b8892SKarl Rupp thrust::sequence(matstructT->cprowIndices->begin(), matstructT->cprowIndices->end()); 999aa372e3fSPaul Mullowney /* assign the pointer */ 1000aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)A->spptr)->matTranspose = matstructT; 1001bda325fcSPaul Mullowney PetscFunctionReturn(0); 1002bda325fcSPaul Mullowney } 1003bda325fcSPaul Mullowney 10044e4bbfaaSStefano Zampini /* Why do we need to analyze the tranposed matrix again? Can't we just use op(A) = CUSPARSE_OPERATION_TRANSPOSE in MatSolve_SeqAIJCUSPARSE? */ 10056fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx) 1006bda325fcSPaul Mullowney { 1007c41cb2e2SAlejandro Lamas Daviña PetscInt n = xx->map->n; 1008465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1009465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 1010465f34aeSAlejandro Lamas Daviña thrust::device_ptr<const PetscScalar> bGPU; 1011465f34aeSAlejandro Lamas Daviña thrust::device_ptr<PetscScalar> xGPU; 1012bda325fcSPaul Mullowney cusparseStatus_t stat; 1013bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1014aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1015aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1016aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1017b175d8bbSPaul Mullowney PetscErrorCode ierr; 1018bda325fcSPaul Mullowney 1019bda325fcSPaul Mullowney PetscFunctionBegin; 1020aa372e3fSPaul Mullowney /* Analyze the matrix and create the transpose ... on the fly */ 1021aa372e3fSPaul Mullowney if (!loTriFactorT && !upTriFactorT) { 1022bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr); 1023aa372e3fSPaul Mullowney loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1024aa372e3fSPaul Mullowney upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1025bda325fcSPaul Mullowney } 1026bda325fcSPaul Mullowney 1027bda325fcSPaul Mullowney /* Get the GPU pointers */ 1028c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1029c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1030c41cb2e2SAlejandro Lamas Daviña xGPU = thrust::device_pointer_cast(xarray); 1031c41cb2e2SAlejandro Lamas Daviña bGPU = thrust::device_pointer_cast(barray); 1032bda325fcSPaul Mullowney 10337a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1034aa372e3fSPaul Mullowney /* First, reorder with the row permutation */ 1035c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()), 1036c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(bGPU+n, cusparseTriFactors->rpermIndices->end()), 1037c41cb2e2SAlejandro Lamas Daviña xGPU); 1038aa372e3fSPaul Mullowney 1039aa372e3fSPaul Mullowney /* First, solve U */ 1040aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp, 10417656d835SStefano Zampini upTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactorT->descr, 1042aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 1043aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 1044aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 1045aa372e3fSPaul Mullowney upTriFactorT->solveInfo, 1046c41cb2e2SAlejandro Lamas Daviña xarray, tempGPU->data().get());CHKERRCUDA(stat); 1047aa372e3fSPaul Mullowney 1048aa372e3fSPaul Mullowney /* Then, solve L */ 1049aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp, 10507656d835SStefano Zampini loTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactorT->descr, 1051aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 1052aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 1053aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 1054aa372e3fSPaul Mullowney loTriFactorT->solveInfo, 1055c41cb2e2SAlejandro Lamas Daviña tempGPU->data().get(), xarray);CHKERRCUDA(stat); 1056aa372e3fSPaul Mullowney 1057aa372e3fSPaul Mullowney /* Last, copy the solution, xGPU, into a temporary with the column permutation ... can't be done in place. */ 1058c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->begin()), 1059c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(xGPU+n, cusparseTriFactors->cpermIndices->end()), 1060aa372e3fSPaul Mullowney tempGPU->begin()); 1061aa372e3fSPaul Mullowney 1062aa372e3fSPaul Mullowney /* Copy the temporary to the full solution. */ 1063c41cb2e2SAlejandro Lamas Daviña thrust::copy(tempGPU->begin(), tempGPU->end(), xGPU); 1064bda325fcSPaul Mullowney 1065bda325fcSPaul Mullowney /* restore */ 1066c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1067c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 1068c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1069661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1070958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 1071bda325fcSPaul Mullowney PetscFunctionReturn(0); 1072bda325fcSPaul Mullowney } 1073bda325fcSPaul Mullowney 10746fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx) 1075bda325fcSPaul Mullowney { 1076465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1077465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 1078bda325fcSPaul Mullowney cusparseStatus_t stat; 1079bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1080aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1081aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1082aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1083b175d8bbSPaul Mullowney PetscErrorCode ierr; 1084bda325fcSPaul Mullowney 1085bda325fcSPaul Mullowney PetscFunctionBegin; 1086aa372e3fSPaul Mullowney /* Analyze the matrix and create the transpose ... on the fly */ 1087aa372e3fSPaul Mullowney if (!loTriFactorT && !upTriFactorT) { 1088bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr); 1089aa372e3fSPaul Mullowney loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1090aa372e3fSPaul Mullowney upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1091bda325fcSPaul Mullowney } 1092bda325fcSPaul Mullowney 1093bda325fcSPaul Mullowney /* Get the GPU pointers */ 1094c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1095c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1096bda325fcSPaul Mullowney 10977a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1098aa372e3fSPaul Mullowney /* First, solve U */ 1099aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp, 11007656d835SStefano Zampini upTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactorT->descr, 1101aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 1102aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 1103aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 1104aa372e3fSPaul Mullowney upTriFactorT->solveInfo, 1105c41cb2e2SAlejandro Lamas Daviña barray, tempGPU->data().get());CHKERRCUDA(stat); 1106aa372e3fSPaul Mullowney 1107aa372e3fSPaul Mullowney /* Then, solve L */ 1108aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp, 11097656d835SStefano Zampini loTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactorT->descr, 1110aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 1111aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 1112aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 1113aa372e3fSPaul Mullowney loTriFactorT->solveInfo, 1114c41cb2e2SAlejandro Lamas Daviña tempGPU->data().get(), xarray);CHKERRCUDA(stat); 1115bda325fcSPaul Mullowney 1116bda325fcSPaul Mullowney /* restore */ 1117c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1118c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 1119c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1120661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1121958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 1122bda325fcSPaul Mullowney PetscFunctionReturn(0); 1123bda325fcSPaul Mullowney } 1124bda325fcSPaul Mullowney 11256fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx) 11269ae82921SPaul Mullowney { 1127465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1128465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 1129465f34aeSAlejandro Lamas Daviña thrust::device_ptr<const PetscScalar> bGPU; 1130465f34aeSAlejandro Lamas Daviña thrust::device_ptr<PetscScalar> xGPU; 11319ae82921SPaul Mullowney cusparseStatus_t stat; 11329ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1133aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 1134aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 1135aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1136b175d8bbSPaul Mullowney PetscErrorCode ierr; 11379ae82921SPaul Mullowney 11389ae82921SPaul Mullowney PetscFunctionBegin; 1139ebc8f436SDominic Meiser 1140e057df02SPaul Mullowney /* Get the GPU pointers */ 1141c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1142c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1143c41cb2e2SAlejandro Lamas Daviña xGPU = thrust::device_pointer_cast(xarray); 1144c41cb2e2SAlejandro Lamas Daviña bGPU = thrust::device_pointer_cast(barray); 11459ae82921SPaul Mullowney 11467a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1147aa372e3fSPaul Mullowney /* First, reorder with the row permutation */ 1148c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()), 1149c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->end()), 11504e4bbfaaSStefano Zampini tempGPU->begin()); 1151aa372e3fSPaul Mullowney 1152aa372e3fSPaul Mullowney /* Next, solve L */ 1153aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp, 11547656d835SStefano Zampini loTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactor->descr, 1155aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 1156aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 1157aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 1158aa372e3fSPaul Mullowney loTriFactor->solveInfo, 11594e4bbfaaSStefano Zampini tempGPU->data().get(), xarray);CHKERRCUDA(stat); 1160aa372e3fSPaul Mullowney 1161aa372e3fSPaul Mullowney /* Then, solve U */ 1162aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp, 11637656d835SStefano Zampini upTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactor->descr, 1164aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 1165aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 1166aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 1167aa372e3fSPaul Mullowney upTriFactor->solveInfo, 11684e4bbfaaSStefano Zampini xarray, tempGPU->data().get());CHKERRCUDA(stat); 1169aa372e3fSPaul Mullowney 11704e4bbfaaSStefano Zampini /* Last, reorder with the column permutation */ 11714e4bbfaaSStefano Zampini thrust::copy(thrust::make_permutation_iterator(tempGPU->begin(), cusparseTriFactors->cpermIndices->begin()), 11724e4bbfaaSStefano Zampini thrust::make_permutation_iterator(tempGPU->begin(), cusparseTriFactors->cpermIndices->end()), 11734e4bbfaaSStefano Zampini xGPU); 11749ae82921SPaul Mullowney 1175c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1176c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 1177c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1178661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1179958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 11809ae82921SPaul Mullowney PetscFunctionReturn(0); 11819ae82921SPaul Mullowney } 11829ae82921SPaul Mullowney 11836fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx) 11849ae82921SPaul Mullowney { 1185465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1186465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 11879ae82921SPaul Mullowney cusparseStatus_t stat; 11889ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1189aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 1190aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 1191aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1192b175d8bbSPaul Mullowney PetscErrorCode ierr; 11939ae82921SPaul Mullowney 11949ae82921SPaul Mullowney PetscFunctionBegin; 1195e057df02SPaul Mullowney /* Get the GPU pointers */ 1196c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1197c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 11989ae82921SPaul Mullowney 11997a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1200aa372e3fSPaul Mullowney /* First, solve L */ 1201aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp, 12027656d835SStefano Zampini loTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactor->descr, 1203aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 1204aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 1205aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 1206aa372e3fSPaul Mullowney loTriFactor->solveInfo, 1207c41cb2e2SAlejandro Lamas Daviña barray, tempGPU->data().get());CHKERRCUDA(stat); 1208aa372e3fSPaul Mullowney 1209aa372e3fSPaul Mullowney /* Next, solve U */ 1210aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp, 12117656d835SStefano Zampini upTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactor->descr, 1212aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 1213aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 1214aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 1215aa372e3fSPaul Mullowney upTriFactor->solveInfo, 1216c41cb2e2SAlejandro Lamas Daviña tempGPU->data().get(), xarray);CHKERRCUDA(stat); 12179ae82921SPaul Mullowney 1218c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1219c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 1220c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1221661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1222958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 12239ae82921SPaul Mullowney PetscFunctionReturn(0); 12249ae82921SPaul Mullowney } 12259ae82921SPaul Mullowney 12266fa9248bSJed Brown static PetscErrorCode MatSeqAIJCUSPARSECopyToGPU(Mat A) 12279ae82921SPaul Mullowney { 1228aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1229aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 12309ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 12319ae82921SPaul Mullowney PetscInt m = A->rmap->n,*ii,*ridx; 12329ae82921SPaul Mullowney PetscErrorCode ierr; 1233aa372e3fSPaul Mullowney cusparseStatus_t stat; 1234b06137fdSPaul Mullowney cudaError_t err; 12359ae82921SPaul Mullowney 12369ae82921SPaul Mullowney PetscFunctionBegin; 1237*c70f7ee4SJunchao Zhang if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) { 12389ae82921SPaul Mullowney ierr = PetscLogEventBegin(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr); 123934d6c7a5SJose E. Roman if (A->assembled && A->nonzerostate == cusparsestruct->nonzerostate && cusparsestruct->format == MAT_CUSPARSE_CSR) { 124034d6c7a5SJose E. Roman CsrMatrix *matrix = (CsrMatrix*)matstruct->mat; 124134d6c7a5SJose E. Roman /* copy values only */ 124234d6c7a5SJose E. Roman matrix->values->assign(a->a, a->a+a->nz); 12434863603aSSatish Balay ierr = PetscLogCpuToGpu((a->nz)*sizeof(PetscScalar));CHKERRQ(ierr); 124434d6c7a5SJose E. Roman } else { 1245470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&matstruct,cusparsestruct->format); 12469ae82921SPaul Mullowney try { 1247aa372e3fSPaul Mullowney cusparsestruct->nonzerorow=0; 1248aa372e3fSPaul Mullowney for (int j = 0; j<m; j++) cusparsestruct->nonzerorow += ((a->i[j+1]-a->i[j])>0); 12499ae82921SPaul Mullowney 12509ae82921SPaul Mullowney if (a->compressedrow.use) { 12519ae82921SPaul Mullowney m = a->compressedrow.nrows; 12529ae82921SPaul Mullowney ii = a->compressedrow.i; 12539ae82921SPaul Mullowney ridx = a->compressedrow.rindex; 12549ae82921SPaul Mullowney } else { 1255b06137fdSPaul Mullowney /* Forcing compressed row on the GPU */ 12569ae82921SPaul Mullowney int k=0; 1257854ce69bSBarry Smith ierr = PetscMalloc1(cusparsestruct->nonzerorow+1, &ii);CHKERRQ(ierr); 1258854ce69bSBarry Smith ierr = PetscMalloc1(cusparsestruct->nonzerorow, &ridx);CHKERRQ(ierr); 12599ae82921SPaul Mullowney ii[0]=0; 12609ae82921SPaul Mullowney for (int j = 0; j<m; j++) { 12619ae82921SPaul Mullowney if ((a->i[j+1]-a->i[j])>0) { 12629ae82921SPaul Mullowney ii[k] = a->i[j]; 12639ae82921SPaul Mullowney ridx[k]= j; 12649ae82921SPaul Mullowney k++; 12659ae82921SPaul Mullowney } 12669ae82921SPaul Mullowney } 1267aa372e3fSPaul Mullowney ii[cusparsestruct->nonzerorow] = a->nz; 1268aa372e3fSPaul Mullowney m = cusparsestruct->nonzerorow; 12699ae82921SPaul Mullowney } 12709ae82921SPaul Mullowney 1271aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 1272aa372e3fSPaul Mullowney matstruct = new Mat_SeqAIJCUSPARSEMultStruct; 1273c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&matstruct->descr);CHKERRCUDA(stat); 1274c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(matstruct->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 1275c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(matstruct->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUDA(stat); 12769ae82921SPaul Mullowney 1277c41cb2e2SAlejandro Lamas Daviña err = cudaMalloc((void **)&(matstruct->alpha), sizeof(PetscScalar));CHKERRCUDA(err); 12787656d835SStefano Zampini err = cudaMalloc((void **)&(matstruct->beta_zero),sizeof(PetscScalar));CHKERRCUDA(err); 12797656d835SStefano Zampini err = cudaMalloc((void **)&(matstruct->beta_one), sizeof(PetscScalar));CHKERRCUDA(err); 12807656d835SStefano Zampini err = cudaMemcpy(matstruct->alpha, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 12817656d835SStefano Zampini err = cudaMemcpy(matstruct->beta_zero,&PETSC_CUSPARSE_ZERO,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 12827656d835SStefano Zampini err = cudaMemcpy(matstruct->beta_one, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 1283c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat); 1284b06137fdSPaul Mullowney 1285aa372e3fSPaul Mullowney /* Build a hybrid/ellpack matrix if this option is chosen for the storage */ 1286aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1287aa372e3fSPaul Mullowney /* set the matrix */ 1288aa372e3fSPaul Mullowney CsrMatrix *matrix= new CsrMatrix; 1289a65300a6SPaul Mullowney matrix->num_rows = m; 1290aa372e3fSPaul Mullowney matrix->num_cols = A->cmap->n; 1291aa372e3fSPaul Mullowney matrix->num_entries = a->nz; 1292a65300a6SPaul Mullowney matrix->row_offsets = new THRUSTINTARRAY32(m+1); 1293a65300a6SPaul Mullowney matrix->row_offsets->assign(ii, ii + m+1); 12949ae82921SPaul Mullowney 1295aa372e3fSPaul Mullowney matrix->column_indices = new THRUSTINTARRAY32(a->nz); 1296aa372e3fSPaul Mullowney matrix->column_indices->assign(a->j, a->j+a->nz); 1297aa372e3fSPaul Mullowney 1298aa372e3fSPaul Mullowney matrix->values = new THRUSTARRAY(a->nz); 1299aa372e3fSPaul Mullowney matrix->values->assign(a->a, a->a+a->nz); 1300aa372e3fSPaul Mullowney 1301aa372e3fSPaul Mullowney /* assign the pointer */ 1302aa372e3fSPaul Mullowney matstruct->mat = matrix; 1303aa372e3fSPaul Mullowney 1304aa372e3fSPaul Mullowney } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) { 1305aa372e3fSPaul Mullowney CsrMatrix *matrix= new CsrMatrix; 1306a65300a6SPaul Mullowney matrix->num_rows = m; 1307aa372e3fSPaul Mullowney matrix->num_cols = A->cmap->n; 1308aa372e3fSPaul Mullowney matrix->num_entries = a->nz; 1309a65300a6SPaul Mullowney matrix->row_offsets = new THRUSTINTARRAY32(m+1); 1310a65300a6SPaul Mullowney matrix->row_offsets->assign(ii, ii + m+1); 1311aa372e3fSPaul Mullowney 1312aa372e3fSPaul Mullowney matrix->column_indices = new THRUSTINTARRAY32(a->nz); 1313aa372e3fSPaul Mullowney matrix->column_indices->assign(a->j, a->j+a->nz); 1314aa372e3fSPaul Mullowney 1315aa372e3fSPaul Mullowney matrix->values = new THRUSTARRAY(a->nz); 1316aa372e3fSPaul Mullowney matrix->values->assign(a->a, a->a+a->nz); 1317aa372e3fSPaul Mullowney 1318aa372e3fSPaul Mullowney cusparseHybMat_t hybMat; 1319c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateHybMat(&hybMat);CHKERRCUDA(stat); 1320aa372e3fSPaul Mullowney cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ? 1321aa372e3fSPaul Mullowney CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO; 1322a65300a6SPaul Mullowney stat = cusparse_csr2hyb(cusparsestruct->handle, matrix->num_rows, matrix->num_cols, 1323aa372e3fSPaul Mullowney matstruct->descr, matrix->values->data().get(), 1324aa372e3fSPaul Mullowney matrix->row_offsets->data().get(), 1325aa372e3fSPaul Mullowney matrix->column_indices->data().get(), 1326c41cb2e2SAlejandro Lamas Daviña hybMat, 0, partition);CHKERRCUDA(stat); 1327aa372e3fSPaul Mullowney /* assign the pointer */ 1328aa372e3fSPaul Mullowney matstruct->mat = hybMat; 1329aa372e3fSPaul Mullowney 1330aa372e3fSPaul Mullowney if (matrix) { 1331aa372e3fSPaul Mullowney if (matrix->values) delete (THRUSTARRAY*)matrix->values; 1332aa372e3fSPaul Mullowney if (matrix->column_indices) delete (THRUSTINTARRAY32*)matrix->column_indices; 1333aa372e3fSPaul Mullowney if (matrix->row_offsets) delete (THRUSTINTARRAY32*)matrix->row_offsets; 1334aa372e3fSPaul Mullowney delete (CsrMatrix*)matrix; 1335087f3262SPaul Mullowney } 1336087f3262SPaul Mullowney } 1337ca45077fSPaul Mullowney 1338aa372e3fSPaul Mullowney /* assign the compressed row indices */ 1339aa372e3fSPaul Mullowney matstruct->cprowIndices = new THRUSTINTARRAY(m); 1340aa372e3fSPaul Mullowney matstruct->cprowIndices->assign(ridx,ridx+m); 13414863603aSSatish Balay ierr = PetscLogCpuToGpu(((m+1)+(a->nz))*sizeof(int)+m*sizeof(PetscInt)+(3+(a->nz))*sizeof(PetscScalar));CHKERRQ(ierr); 1342aa372e3fSPaul Mullowney 1343aa372e3fSPaul Mullowney /* assign the pointer */ 1344aa372e3fSPaul Mullowney cusparsestruct->mat = matstruct; 1345aa372e3fSPaul Mullowney 13469ae82921SPaul Mullowney if (!a->compressedrow.use) { 13479ae82921SPaul Mullowney ierr = PetscFree(ii);CHKERRQ(ierr); 13489ae82921SPaul Mullowney ierr = PetscFree(ridx);CHKERRQ(ierr); 13499ae82921SPaul Mullowney } 1350e65717acSKarl Rupp cusparsestruct->workVector = new THRUSTARRAY(m); 13519ae82921SPaul Mullowney } catch(char *ex) { 13529ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 13539ae82921SPaul Mullowney } 135434d6c7a5SJose E. Roman cusparsestruct->nonzerostate = A->nonzerostate; 135534d6c7a5SJose E. Roman } 13567d0e52d8SJose E. Roman ierr = WaitForGPU();CHKERRCUDA(ierr); 1357*c70f7ee4SJunchao Zhang A->offloadmask = PETSC_OFFLOAD_BOTH; 13589ae82921SPaul Mullowney ierr = PetscLogEventEnd(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr); 13599ae82921SPaul Mullowney } 13609ae82921SPaul Mullowney PetscFunctionReturn(0); 13619ae82921SPaul Mullowney } 13629ae82921SPaul Mullowney 1363c41cb2e2SAlejandro Lamas Daviña struct VecCUDAPlusEquals 1364aa372e3fSPaul Mullowney { 1365aa372e3fSPaul Mullowney template <typename Tuple> 1366aa372e3fSPaul Mullowney __host__ __device__ 1367aa372e3fSPaul Mullowney void operator()(Tuple t) 1368aa372e3fSPaul Mullowney { 1369aa372e3fSPaul Mullowney thrust::get<1>(t) = thrust::get<1>(t) + thrust::get<0>(t); 1370aa372e3fSPaul Mullowney } 1371aa372e3fSPaul Mullowney }; 1372aa372e3fSPaul Mullowney 13736fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy) 13749ae82921SPaul Mullowney { 1375b175d8bbSPaul Mullowney PetscErrorCode ierr; 13769ae82921SPaul Mullowney 13779ae82921SPaul Mullowney PetscFunctionBegin; 13787656d835SStefano Zampini ierr = MatMultAdd_SeqAIJCUSPARSE(A,xx,NULL,yy);CHKERRQ(ierr); 13799ae82921SPaul Mullowney PetscFunctionReturn(0); 13809ae82921SPaul Mullowney } 13819ae82921SPaul Mullowney 13826fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy) 1383ca45077fSPaul Mullowney { 1384ca45077fSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1385aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 13869ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstructT; 1387465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 1388465f34aeSAlejandro Lamas Daviña PetscScalar *yarray; 1389b175d8bbSPaul Mullowney PetscErrorCode ierr; 1390aa372e3fSPaul Mullowney cusparseStatus_t stat; 1391ca45077fSPaul Mullowney 1392ca45077fSPaul Mullowney PetscFunctionBegin; 139334d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 139434d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 13959ff858a8SKarl Rupp matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1396aa372e3fSPaul Mullowney if (!matstructT) { 1397bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr); 1398aa372e3fSPaul Mullowney matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1399bda325fcSPaul Mullowney } 1400c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1401c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(yy,&yarray);CHKERRQ(ierr); 1402f6ae8131SMark if (yy->map->n) { 1403f6ae8131SMark PetscInt n = yy->map->n; 1404f6ae8131SMark cudaError_t err; 1405f6ae8131SMark err = cudaMemset(yarray,0,n*sizeof(PetscScalar));CHKERRCUDA(err); /* hack to fix numerical errors from reading output vector yy, apparently */ 1406f6ae8131SMark } 1407aa372e3fSPaul Mullowney 14087a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1409aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1410aa372e3fSPaul Mullowney CsrMatrix *mat = (CsrMatrix*)matstructT->mat; 1411aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1412aa372e3fSPaul Mullowney mat->num_rows, mat->num_cols, 1413b06137fdSPaul Mullowney mat->num_entries, matstructT->alpha, matstructT->descr, 1414aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 14157656d835SStefano Zampini mat->column_indices->data().get(), xarray, matstructT->beta_zero, 1416c41cb2e2SAlejandro Lamas Daviña yarray);CHKERRCUDA(stat); 1417aa372e3fSPaul Mullowney } else { 1418aa372e3fSPaul Mullowney cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat; 1419aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1420b06137fdSPaul Mullowney matstructT->alpha, matstructT->descr, hybMat, 14217656d835SStefano Zampini xarray, matstructT->beta_zero, 1422c41cb2e2SAlejandro Lamas Daviña yarray);CHKERRCUDA(stat); 1423ca45077fSPaul Mullowney } 1424c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1425c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(yy,&yarray);CHKERRQ(ierr); 1426c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1427661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1428958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*a->nz - cusparsestruct->nonzerorow);CHKERRQ(ierr); 1429ca45077fSPaul Mullowney PetscFunctionReturn(0); 1430ca45077fSPaul Mullowney } 1431ca45077fSPaul Mullowney 1432aa372e3fSPaul Mullowney 14336fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz) 14349ae82921SPaul Mullowney { 14359ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1436aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 14379ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstruct; 1438465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 14397656d835SStefano Zampini PetscScalar *zarray,*dptr,*beta; 1440b175d8bbSPaul Mullowney PetscErrorCode ierr; 1441aa372e3fSPaul Mullowney cusparseStatus_t stat; 1442c1fb3f03SStefano Zampini PetscBool cmpr; /* if the matrix has been compressed (zero rows) */ 14436e111a19SKarl Rupp 14449ae82921SPaul Mullowney PetscFunctionBegin; 144534d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 144634d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 14479ff858a8SKarl Rupp matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 14489ae82921SPaul Mullowney try { 1449c1fb3f03SStefano Zampini cmpr = (PetscBool)(cusparsestruct->workVector->size() == (thrust::detail::vector_base<PetscScalar, thrust::device_malloc_allocator<PetscScalar> >::size_type)(A->rmap->n)); 1450c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1451c1fb3f03SStefano Zampini if (yy && !cmpr) { /* MatMultAdd with noncompressed storage -> need uptodate zz vector */ 1452f2d70e9dSBarry Smith ierr = VecCUDAGetArray(zz,&zarray);CHKERRQ(ierr); 1453c1fb3f03SStefano Zampini } else { 1454c1fb3f03SStefano Zampini ierr = VecCUDAGetArrayWrite(zz,&zarray);CHKERRQ(ierr); 1455c1fb3f03SStefano Zampini } 1456c1fb3f03SStefano Zampini dptr = cmpr ? zarray : cusparsestruct->workVector->data().get(); 14577656d835SStefano Zampini beta = (yy == zz && dptr == zarray) ? matstruct->beta_one : matstruct->beta_zero; 14589ae82921SPaul Mullowney 14597a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 14607656d835SStefano Zampini /* csr_spmv is multiply add */ 1461aa372e3fSPaul Mullowney if (cusparsestruct->format == MAT_CUSPARSE_CSR) { 1462b06137fdSPaul Mullowney /* here we need to be careful to set the number of rows in the multiply to the 1463b06137fdSPaul Mullowney number of compressed rows in the matrix ... which is equivalent to the 1464b06137fdSPaul Mullowney size of the workVector */ 14657656d835SStefano Zampini CsrMatrix *mat = (CsrMatrix*)matstruct->mat; 1466aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1467a65300a6SPaul Mullowney mat->num_rows, mat->num_cols, 1468b06137fdSPaul Mullowney mat->num_entries, matstruct->alpha, matstruct->descr, 1469aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 14707656d835SStefano Zampini mat->column_indices->data().get(), xarray, beta, 14717656d835SStefano Zampini dptr);CHKERRCUDA(stat); 1472aa372e3fSPaul Mullowney } else { 1473a65300a6SPaul Mullowney if (cusparsestruct->workVector->size()) { 1474301298b4SMark Adams cusparseHybMat_t hybMat = (cusparseHybMat_t)matstruct->mat; 1475aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1476b06137fdSPaul Mullowney matstruct->alpha, matstruct->descr, hybMat, 14777656d835SStefano Zampini xarray, beta, 14787656d835SStefano Zampini dptr);CHKERRCUDA(stat); 1479a65300a6SPaul Mullowney } 1480aa372e3fSPaul Mullowney } 1481958c4211Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1482aa372e3fSPaul Mullowney 14837656d835SStefano Zampini if (yy) { 14847656d835SStefano Zampini if (dptr != zarray) { 14857656d835SStefano Zampini ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); 14867656d835SStefano Zampini } else if (zz != yy) { 14877656d835SStefano Zampini ierr = VecAXPY_SeqCUDA(zz,1.0,yy);CHKERRQ(ierr); 14887656d835SStefano Zampini } 14897656d835SStefano Zampini } else if (dptr != zarray) { 1490c1fb3f03SStefano Zampini ierr = VecSet_SeqCUDA(zz,0);CHKERRQ(ierr); 14917656d835SStefano Zampini } 1492aa372e3fSPaul Mullowney /* scatter the data from the temporary into the full vector with a += operation */ 14937a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 14947656d835SStefano Zampini if (dptr != zarray) { 14957656d835SStefano Zampini thrust::device_ptr<PetscScalar> zptr; 14967656d835SStefano Zampini 14977656d835SStefano Zampini zptr = thrust::device_pointer_cast(zarray); 1498c41cb2e2SAlejandro Lamas Daviña thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))), 1499c41cb2e2SAlejandro Lamas Daviña thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))) + cusparsestruct->workVector->size(), 1500c41cb2e2SAlejandro Lamas Daviña VecCUDAPlusEquals()); 15017656d835SStefano Zampini } 1502958c4211Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1503c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1504c1fb3f03SStefano Zampini if (yy && !cmpr) { 1505f2d70e9dSBarry Smith ierr = VecCUDARestoreArray(zz,&zarray);CHKERRQ(ierr); 1506c1fb3f03SStefano Zampini } else { 1507c1fb3f03SStefano Zampini ierr = VecCUDARestoreArrayWrite(zz,&zarray);CHKERRQ(ierr); 1508c1fb3f03SStefano Zampini } 15099ae82921SPaul Mullowney } catch(char *ex) { 15109ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 15119ae82921SPaul Mullowney } 1512c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1513958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr); 15149ae82921SPaul Mullowney PetscFunctionReturn(0); 15159ae82921SPaul Mullowney } 15169ae82921SPaul Mullowney 15176fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz) 1518ca45077fSPaul Mullowney { 1519ca45077fSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1520aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 15219ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstructT; 1522465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 1523a3fdcf43SKarl Rupp PetscScalar *zarray,*dptr,*beta; 1524b175d8bbSPaul Mullowney PetscErrorCode ierr; 1525aa372e3fSPaul Mullowney cusparseStatus_t stat; 15266e111a19SKarl Rupp 1527ca45077fSPaul Mullowney PetscFunctionBegin; 152834d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 152934d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 15309ff858a8SKarl Rupp matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1531aa372e3fSPaul Mullowney if (!matstructT) { 1532bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr); 1533aa372e3fSPaul Mullowney matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1534bda325fcSPaul Mullowney } 1535aa372e3fSPaul Mullowney 1536ca45077fSPaul Mullowney try { 1537c41cb2e2SAlejandro Lamas Daviña ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); 1538c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1539f2d70e9dSBarry Smith ierr = VecCUDAGetArray(zz,&zarray);CHKERRQ(ierr); 1540a3fdcf43SKarl 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(); 1541a3fdcf43SKarl Rupp beta = (yy == zz && dptr == zarray) ? matstructT->beta_one : matstructT->beta_zero; 1542ca45077fSPaul Mullowney 15437a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1544e057df02SPaul Mullowney /* multiply add with matrix transpose */ 1545aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1546aa372e3fSPaul Mullowney CsrMatrix *mat = (CsrMatrix*)matstructT->mat; 1547b06137fdSPaul Mullowney /* here we need to be careful to set the number of rows in the multiply to the 1548b06137fdSPaul Mullowney number of compressed rows in the matrix ... which is equivalent to the 1549b06137fdSPaul Mullowney size of the workVector */ 1550aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1551a65300a6SPaul Mullowney mat->num_rows, mat->num_cols, 1552b06137fdSPaul Mullowney mat->num_entries, matstructT->alpha, matstructT->descr, 1553aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 1554a3fdcf43SKarl Rupp mat->column_indices->data().get(), xarray, beta, 1555a3fdcf43SKarl Rupp dptr);CHKERRCUDA(stat); 1556aa372e3fSPaul Mullowney } else { 1557aa372e3fSPaul Mullowney cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat; 1558a65300a6SPaul Mullowney if (cusparsestruct->workVector->size()) { 1559aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1560b06137fdSPaul Mullowney matstructT->alpha, matstructT->descr, hybMat, 1561a3fdcf43SKarl Rupp xarray, beta, 1562a3fdcf43SKarl Rupp dptr);CHKERRCUDA(stat); 1563a65300a6SPaul Mullowney } 1564aa372e3fSPaul Mullowney } 1565a3fdcf43SKarl Rupp ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1566a3fdcf43SKarl Rupp 1567a3fdcf43SKarl Rupp if (dptr != zarray) { 1568a3fdcf43SKarl Rupp ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); 1569a3fdcf43SKarl Rupp } else if (zz != yy) { 1570a3fdcf43SKarl Rupp ierr = VecAXPY_SeqCUDA(zz,1.0,yy);CHKERRQ(ierr); 1571a3fdcf43SKarl Rupp } 1572a3fdcf43SKarl Rupp /* scatter the data from the temporary into the full vector with a += operation */ 1573a3fdcf43SKarl Rupp ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1574a3fdcf43SKarl Rupp if (dptr != zarray) { 1575a3fdcf43SKarl Rupp thrust::device_ptr<PetscScalar> zptr; 1576a3fdcf43SKarl Rupp 1577a3fdcf43SKarl Rupp zptr = thrust::device_pointer_cast(zarray); 1578aa372e3fSPaul Mullowney 1579aa372e3fSPaul Mullowney /* scatter the data from the temporary into the full vector with a += operation */ 1580c41cb2e2SAlejandro Lamas Daviña thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstructT->cprowIndices->begin()))), 1581554b8892SKarl Rupp thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstructT->cprowIndices->begin()))) + A->cmap->n, 1582c41cb2e2SAlejandro Lamas Daviña VecCUDAPlusEquals()); 1583a3fdcf43SKarl Rupp } 1584a3fdcf43SKarl Rupp ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1585c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1586f2d70e9dSBarry Smith ierr = VecCUDARestoreArray(zz,&zarray);CHKERRQ(ierr); 1587ca45077fSPaul Mullowney } catch(char *ex) { 1588ca45077fSPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 1589661c2d29Shannah_mairs } 1590a3fdcf43SKarl Rupp ierr = WaitForGPU();CHKERRCUDA(ierr); /* is this needed? just for yy==0 in Mult */ 1591958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr); 1592ca45077fSPaul Mullowney PetscFunctionReturn(0); 1593ca45077fSPaul Mullowney } 1594ca45077fSPaul Mullowney 15956fa9248bSJed Brown static PetscErrorCode MatAssemblyEnd_SeqAIJCUSPARSE(Mat A,MatAssemblyType mode) 15969ae82921SPaul Mullowney { 15979ae82921SPaul Mullowney PetscErrorCode ierr; 15986e111a19SKarl Rupp 15999ae82921SPaul Mullowney PetscFunctionBegin; 16009ae82921SPaul Mullowney ierr = MatAssemblyEnd_SeqAIJ(A,mode);CHKERRQ(ierr); 1601489de41dSStefano Zampini if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 1602bc3f50f2SPaul Mullowney if (A->factortype == MAT_FACTOR_NONE) { 1603e057df02SPaul Mullowney ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 1604bc3f50f2SPaul Mullowney } 1605bbf3fe20SPaul Mullowney A->ops->mult = MatMult_SeqAIJCUSPARSE; 1606bbf3fe20SPaul Mullowney A->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 1607bbf3fe20SPaul Mullowney A->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1608bbf3fe20SPaul Mullowney A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 16099ae82921SPaul Mullowney PetscFunctionReturn(0); 16109ae82921SPaul Mullowney } 16119ae82921SPaul Mullowney 16129ae82921SPaul Mullowney /* --------------------------------------------------------------------------------*/ 1613e057df02SPaul Mullowney /*@ 16149ae82921SPaul Mullowney MatCreateSeqAIJCUSPARSE - Creates a sparse matrix in AIJ (compressed row) format 1615e057df02SPaul Mullowney (the default parallel PETSc format). This matrix will ultimately pushed down 1616e057df02SPaul Mullowney to NVidia GPUs and use the CUSPARSE library for calculations. For good matrix 1617e057df02SPaul Mullowney assembly performance the user should preallocate the matrix storage by setting 1618e057df02SPaul Mullowney the parameter nz (or the array nnz). By setting these parameters accurately, 1619e057df02SPaul Mullowney performance during matrix assembly can be increased by more than a factor of 50. 16209ae82921SPaul Mullowney 1621d083f849SBarry Smith Collective 16229ae82921SPaul Mullowney 16239ae82921SPaul Mullowney Input Parameters: 16249ae82921SPaul Mullowney + comm - MPI communicator, set to PETSC_COMM_SELF 16259ae82921SPaul Mullowney . m - number of rows 16269ae82921SPaul Mullowney . n - number of columns 16279ae82921SPaul Mullowney . nz - number of nonzeros per row (same for all rows) 16289ae82921SPaul Mullowney - nnz - array containing the number of nonzeros in the various rows 16290298fd71SBarry Smith (possibly different for each row) or NULL 16309ae82921SPaul Mullowney 16319ae82921SPaul Mullowney Output Parameter: 16329ae82921SPaul Mullowney . A - the matrix 16339ae82921SPaul Mullowney 16349ae82921SPaul Mullowney It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 16359ae82921SPaul Mullowney MatXXXXSetPreallocation() paradgm instead of this routine directly. 16369ae82921SPaul Mullowney [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 16379ae82921SPaul Mullowney 16389ae82921SPaul Mullowney Notes: 16399ae82921SPaul Mullowney If nnz is given then nz is ignored 16409ae82921SPaul Mullowney 16419ae82921SPaul Mullowney The AIJ format (also called the Yale sparse matrix format or 16429ae82921SPaul Mullowney compressed row storage), is fully compatible with standard Fortran 77 16439ae82921SPaul Mullowney storage. That is, the stored row and column indices can begin at 16449ae82921SPaul Mullowney either one (as in Fortran) or zero. See the users' manual for details. 16459ae82921SPaul Mullowney 16469ae82921SPaul Mullowney Specify the preallocated storage with either nz or nnz (not both). 16470298fd71SBarry Smith Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory 16489ae82921SPaul Mullowney allocation. For large problems you MUST preallocate memory or you 16499ae82921SPaul Mullowney will get TERRIBLE performance, see the users' manual chapter on matrices. 16509ae82921SPaul Mullowney 16519ae82921SPaul Mullowney By default, this format uses inodes (identical nodes) when possible, to 16529ae82921SPaul Mullowney improve numerical efficiency of matrix-vector products and solves. We 16539ae82921SPaul Mullowney search for consecutive rows with the same nonzero structure, thereby 16549ae82921SPaul Mullowney reusing matrix information to achieve increased efficiency. 16559ae82921SPaul Mullowney 16569ae82921SPaul Mullowney Level: intermediate 16579ae82921SPaul Mullowney 1658e057df02SPaul Mullowney .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ(), MATSEQAIJCUSPARSE, MATAIJCUSPARSE 16599ae82921SPaul Mullowney @*/ 16609ae82921SPaul Mullowney PetscErrorCode MatCreateSeqAIJCUSPARSE(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 16619ae82921SPaul Mullowney { 16629ae82921SPaul Mullowney PetscErrorCode ierr; 16639ae82921SPaul Mullowney 16649ae82921SPaul Mullowney PetscFunctionBegin; 16659ae82921SPaul Mullowney ierr = MatCreate(comm,A);CHKERRQ(ierr); 16669ae82921SPaul Mullowney ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 16679ae82921SPaul Mullowney ierr = MatSetType(*A,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 16689ae82921SPaul Mullowney ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr); 16699ae82921SPaul Mullowney PetscFunctionReturn(0); 16709ae82921SPaul Mullowney } 16719ae82921SPaul Mullowney 16726fa9248bSJed Brown static PetscErrorCode MatDestroy_SeqAIJCUSPARSE(Mat A) 16739ae82921SPaul Mullowney { 16749ae82921SPaul Mullowney PetscErrorCode ierr; 1675ab25e6cbSDominic Meiser 16769ae82921SPaul Mullowney PetscFunctionBegin; 16779ae82921SPaul Mullowney if (A->factortype==MAT_FACTOR_NONE) { 1678*c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) { 1679470880abSPatrick Sanan ierr = MatSeqAIJCUSPARSE_Destroy((Mat_SeqAIJCUSPARSE**)&A->spptr);CHKERRQ(ierr); 16809ae82921SPaul Mullowney } 16819ae82921SPaul Mullowney } else { 1682470880abSPatrick Sanan ierr = MatSeqAIJCUSPARSETriFactors_Destroy((Mat_SeqAIJCUSPARSETriFactors**)&A->spptr);CHKERRQ(ierr); 1683aa372e3fSPaul Mullowney } 16849ae82921SPaul Mullowney ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr); 16859ae82921SPaul Mullowney PetscFunctionReturn(0); 16869ae82921SPaul Mullowney } 16879ae82921SPaul Mullowney 16889ff858a8SKarl Rupp static PetscErrorCode MatDuplicate_SeqAIJCUSPARSE(Mat A,MatDuplicateOption cpvalues,Mat *B) 16899ff858a8SKarl Rupp { 16909ff858a8SKarl Rupp PetscErrorCode ierr; 16919ff858a8SKarl Rupp Mat C; 16929ff858a8SKarl Rupp cusparseStatus_t stat; 16939ff858a8SKarl Rupp cusparseHandle_t handle=0; 16949ff858a8SKarl Rupp 16959ff858a8SKarl Rupp PetscFunctionBegin; 16969ff858a8SKarl Rupp ierr = MatDuplicate_SeqAIJ(A,cpvalues,B);CHKERRQ(ierr); 16979ff858a8SKarl Rupp C = *B; 169834136279SStefano Zampini ierr = PetscFree(C->defaultvectype);CHKERRQ(ierr); 169934136279SStefano Zampini ierr = PetscStrallocpy(VECCUDA,&C->defaultvectype);CHKERRQ(ierr); 170034136279SStefano Zampini 17019ff858a8SKarl Rupp /* inject CUSPARSE-specific stuff */ 17029ff858a8SKarl Rupp if (C->factortype==MAT_FACTOR_NONE) { 17039ff858a8SKarl Rupp /* you cannot check the inode.use flag here since the matrix was just created. 17049ff858a8SKarl Rupp now build a GPU matrix data structure */ 17059ff858a8SKarl Rupp C->spptr = new Mat_SeqAIJCUSPARSE; 17069ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->mat = 0; 17079ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->matTranspose = 0; 17089ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->workVector = 0; 17099ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->format = MAT_CUSPARSE_CSR; 17109ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->stream = 0; 17119ff858a8SKarl Rupp stat = cusparseCreate(&handle);CHKERRCUDA(stat); 17129ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->handle = handle; 17139ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->nonzerostate = 0; 17149ff858a8SKarl Rupp } else { 17159ff858a8SKarl Rupp /* NEXT, set the pointers to the triangular factors */ 17169ff858a8SKarl Rupp C->spptr = new Mat_SeqAIJCUSPARSETriFactors; 17179ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->loTriFactorPtr = 0; 17189ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->upTriFactorPtr = 0; 17199ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->loTriFactorPtrTranspose = 0; 17209ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->upTriFactorPtrTranspose = 0; 17219ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->rpermIndices = 0; 17229ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->cpermIndices = 0; 17239ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->workVector = 0; 17249ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->handle = 0; 17259ff858a8SKarl Rupp stat = cusparseCreate(&handle);CHKERRCUDA(stat); 17269ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->handle = handle; 17279ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->nnz = 0; 17289ff858a8SKarl Rupp } 17299ff858a8SKarl Rupp 17309ff858a8SKarl Rupp C->ops->assemblyend = MatAssemblyEnd_SeqAIJCUSPARSE; 17319ff858a8SKarl Rupp C->ops->destroy = MatDestroy_SeqAIJCUSPARSE; 17329ff858a8SKarl Rupp C->ops->setfromoptions = MatSetFromOptions_SeqAIJCUSPARSE; 17339ff858a8SKarl Rupp C->ops->mult = MatMult_SeqAIJCUSPARSE; 17349ff858a8SKarl Rupp C->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 17359ff858a8SKarl Rupp C->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 17369ff858a8SKarl Rupp C->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 17379ff858a8SKarl Rupp C->ops->duplicate = MatDuplicate_SeqAIJCUSPARSE; 17389ff858a8SKarl Rupp 17399ff858a8SKarl Rupp ierr = PetscObjectChangeTypeName((PetscObject)C,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 17409ff858a8SKarl Rupp 1741*c70f7ee4SJunchao Zhang C->offloadmask = PETSC_OFFLOAD_UNALLOCATED; 17429ff858a8SKarl Rupp 17439ff858a8SKarl Rupp ierr = PetscObjectComposeFunction((PetscObject)C, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr); 17449ff858a8SKarl Rupp PetscFunctionReturn(0); 17459ff858a8SKarl Rupp } 17469ff858a8SKarl Rupp 174702fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat B) 17489ae82921SPaul Mullowney { 17499ae82921SPaul Mullowney PetscErrorCode ierr; 1750aa372e3fSPaul Mullowney cusparseStatus_t stat; 1751aa372e3fSPaul Mullowney cusparseHandle_t handle=0; 17529ae82921SPaul Mullowney 17539ae82921SPaul Mullowney PetscFunctionBegin; 175434136279SStefano Zampini ierr = PetscFree(B->defaultvectype);CHKERRQ(ierr); 175534136279SStefano Zampini ierr = PetscStrallocpy(VECCUDA,&B->defaultvectype);CHKERRQ(ierr); 175634136279SStefano Zampini 17579ae82921SPaul Mullowney if (B->factortype==MAT_FACTOR_NONE) { 1758e057df02SPaul Mullowney /* you cannot check the inode.use flag here since the matrix was just created. 1759e057df02SPaul Mullowney now build a GPU matrix data structure */ 17609ae82921SPaul Mullowney B->spptr = new Mat_SeqAIJCUSPARSE; 17619ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->mat = 0; 1762aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->matTranspose = 0; 1763aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->workVector = 0; 1764e057df02SPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->format = MAT_CUSPARSE_CSR; 1765aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->stream = 0; 1766c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreate(&handle);CHKERRCUDA(stat); 1767aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->handle = handle; 17689ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)B->spptr)->nonzerostate = 0; 17699ae82921SPaul Mullowney } else { 17709ae82921SPaul Mullowney /* NEXT, set the pointers to the triangular factors */ 1771debe9ee2SPaul Mullowney B->spptr = new Mat_SeqAIJCUSPARSETriFactors; 17729ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtr = 0; 17739ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtr = 0; 1774aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtrTranspose = 0; 1775aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtrTranspose = 0; 1776aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->rpermIndices = 0; 1777aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->cpermIndices = 0; 1778aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->workVector = 0; 1779c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreate(&handle);CHKERRCUDA(stat); 1780aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->handle = handle; 1781aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->nnz = 0; 17829ae82921SPaul Mullowney } 1783aa372e3fSPaul Mullowney 17849ae82921SPaul Mullowney B->ops->assemblyend = MatAssemblyEnd_SeqAIJCUSPARSE; 17859ae82921SPaul Mullowney B->ops->destroy = MatDestroy_SeqAIJCUSPARSE; 17869ae82921SPaul Mullowney B->ops->setfromoptions = MatSetFromOptions_SeqAIJCUSPARSE; 1787ca45077fSPaul Mullowney B->ops->mult = MatMult_SeqAIJCUSPARSE; 1788ca45077fSPaul Mullowney B->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 1789ca45077fSPaul Mullowney B->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1790ca45077fSPaul Mullowney B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 17919ff858a8SKarl Rupp B->ops->duplicate = MatDuplicate_SeqAIJCUSPARSE; 17922205254eSKarl Rupp 17939ae82921SPaul Mullowney ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 17942205254eSKarl Rupp 1795*c70f7ee4SJunchao Zhang B->offloadmask = PETSC_OFFLOAD_UNALLOCATED; 17962205254eSKarl Rupp 1797bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr); 17989ae82921SPaul Mullowney PetscFunctionReturn(0); 17999ae82921SPaul Mullowney } 18009ae82921SPaul Mullowney 180102fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJCUSPARSE(Mat B) 180202fe1965SBarry Smith { 180302fe1965SBarry Smith PetscErrorCode ierr; 180402fe1965SBarry Smith 180502fe1965SBarry Smith PetscFunctionBegin; 180602fe1965SBarry Smith ierr = MatCreate_SeqAIJ(B);CHKERRQ(ierr); 1807489de41dSStefano Zampini ierr = MatConvert_SeqAIJ_SeqAIJCUSPARSE(B);CHKERRQ(ierr); 180802fe1965SBarry Smith PetscFunctionReturn(0); 180902fe1965SBarry Smith } 181002fe1965SBarry Smith 18113ca39a21SBarry Smith /*MC 1812e057df02SPaul Mullowney MATSEQAIJCUSPARSE - MATAIJCUSPARSE = "(seq)aijcusparse" - A matrix type to be used for sparse matrices. 1813e057df02SPaul Mullowney 1814e057df02SPaul Mullowney A matrix type type whose data resides on Nvidia GPUs. These matrices can be in either 18152692e278SPaul Mullowney CSR, ELL, or Hybrid format. The ELL and HYB formats require CUDA 4.2 or later. 18162692e278SPaul Mullowney All matrix calculations are performed on Nvidia GPUs using the CUSPARSE library. 1817e057df02SPaul Mullowney 1818e057df02SPaul Mullowney Options Database Keys: 1819e057df02SPaul Mullowney + -mat_type aijcusparse - sets the matrix type to "seqaijcusparse" during a call to MatSetFromOptions() 1820aa372e3fSPaul 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). 1821a2b725a8SWilliam 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). 1822e057df02SPaul Mullowney 1823e057df02SPaul Mullowney Level: beginner 1824e057df02SPaul Mullowney 18258468deeeSKarl Rupp .seealso: MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation 1826e057df02SPaul Mullowney M*/ 18277f756511SDominic Meiser 182842c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat,MatFactorType,Mat*); 182942c9c57cSBarry Smith 18300f39cd5aSBarry Smith 18313ca39a21SBarry Smith PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_CUSPARSE(void) 183242c9c57cSBarry Smith { 183342c9c57cSBarry Smith PetscErrorCode ierr; 183442c9c57cSBarry Smith 183542c9c57cSBarry Smith PetscFunctionBegin; 18363ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_LU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 18373ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_CHOLESKY,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 18383ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ILU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 18393ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ICC,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 184042c9c57cSBarry Smith PetscFunctionReturn(0); 184142c9c57cSBarry Smith } 184229b38603SBarry Smith 184381e08676SBarry Smith 1844470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE **cusparsestruct) 18457f756511SDominic Meiser { 18467f756511SDominic Meiser cusparseStatus_t stat; 18477f756511SDominic Meiser cusparseHandle_t handle; 18487f756511SDominic Meiser 18497f756511SDominic Meiser PetscFunctionBegin; 18507f756511SDominic Meiser if (*cusparsestruct) { 1851470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->mat,(*cusparsestruct)->format); 1852470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->matTranspose,(*cusparsestruct)->format); 18537f756511SDominic Meiser delete (*cusparsestruct)->workVector; 18547f756511SDominic Meiser if (handle = (*cusparsestruct)->handle) { 1855c41cb2e2SAlejandro Lamas Daviña stat = cusparseDestroy(handle);CHKERRCUDA(stat); 18567f756511SDominic Meiser } 18577f756511SDominic Meiser delete *cusparsestruct; 18587f756511SDominic Meiser *cusparsestruct = 0; 18597f756511SDominic Meiser } 18607f756511SDominic Meiser PetscFunctionReturn(0); 18617f756511SDominic Meiser } 18627f756511SDominic Meiser 18637f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix **mat) 18647f756511SDominic Meiser { 18657f756511SDominic Meiser PetscFunctionBegin; 18667f756511SDominic Meiser if (*mat) { 18677f756511SDominic Meiser delete (*mat)->values; 18687f756511SDominic Meiser delete (*mat)->column_indices; 18697f756511SDominic Meiser delete (*mat)->row_offsets; 18707f756511SDominic Meiser delete *mat; 18717f756511SDominic Meiser *mat = 0; 18727f756511SDominic Meiser } 18737f756511SDominic Meiser PetscFunctionReturn(0); 18747f756511SDominic Meiser } 18757f756511SDominic Meiser 1876470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct **trifactor) 18777f756511SDominic Meiser { 18787f756511SDominic Meiser cusparseStatus_t stat; 18797f756511SDominic Meiser PetscErrorCode ierr; 18807f756511SDominic Meiser 18817f756511SDominic Meiser PetscFunctionBegin; 18827f756511SDominic Meiser if (*trifactor) { 1883c41cb2e2SAlejandro Lamas Daviña if ((*trifactor)->descr) { stat = cusparseDestroyMatDescr((*trifactor)->descr);CHKERRCUDA(stat); } 1884c41cb2e2SAlejandro Lamas Daviña if ((*trifactor)->solveInfo) { stat = cusparseDestroySolveAnalysisInfo((*trifactor)->solveInfo);CHKERRCUDA(stat); } 18857f756511SDominic Meiser ierr = CsrMatrix_Destroy(&(*trifactor)->csrMat);CHKERRQ(ierr); 18867f756511SDominic Meiser delete *trifactor; 18877f756511SDominic Meiser *trifactor = 0; 18887f756511SDominic Meiser } 18897f756511SDominic Meiser PetscFunctionReturn(0); 18907f756511SDominic Meiser } 18917f756511SDominic Meiser 1892470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct **matstruct,MatCUSPARSEStorageFormat format) 18937f756511SDominic Meiser { 18947f756511SDominic Meiser CsrMatrix *mat; 18957f756511SDominic Meiser cusparseStatus_t stat; 18967f756511SDominic Meiser cudaError_t err; 18977f756511SDominic Meiser 18987f756511SDominic Meiser PetscFunctionBegin; 18997f756511SDominic Meiser if (*matstruct) { 19007f756511SDominic Meiser if ((*matstruct)->mat) { 19017f756511SDominic Meiser if (format==MAT_CUSPARSE_ELL || format==MAT_CUSPARSE_HYB) { 19027f756511SDominic Meiser cusparseHybMat_t hybMat = (cusparseHybMat_t)(*matstruct)->mat; 1903c41cb2e2SAlejandro Lamas Daviña stat = cusparseDestroyHybMat(hybMat);CHKERRCUDA(stat); 19047f756511SDominic Meiser } else { 19057f756511SDominic Meiser mat = (CsrMatrix*)(*matstruct)->mat; 19067f756511SDominic Meiser CsrMatrix_Destroy(&mat); 19077f756511SDominic Meiser } 19087f756511SDominic Meiser } 1909c41cb2e2SAlejandro Lamas Daviña if ((*matstruct)->descr) { stat = cusparseDestroyMatDescr((*matstruct)->descr);CHKERRCUDA(stat); } 19107f756511SDominic Meiser delete (*matstruct)->cprowIndices; 1911c41cb2e2SAlejandro Lamas Daviña if ((*matstruct)->alpha) { err=cudaFree((*matstruct)->alpha);CHKERRCUDA(err); } 19127656d835SStefano Zampini if ((*matstruct)->beta_zero) { err=cudaFree((*matstruct)->beta_zero);CHKERRCUDA(err); } 19137656d835SStefano Zampini if ((*matstruct)->beta_one) { err=cudaFree((*matstruct)->beta_one);CHKERRCUDA(err); } 19147f756511SDominic Meiser delete *matstruct; 19157f756511SDominic Meiser *matstruct = 0; 19167f756511SDominic Meiser } 19177f756511SDominic Meiser PetscFunctionReturn(0); 19187f756511SDominic Meiser } 19197f756511SDominic Meiser 1920470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors** trifactors) 19217f756511SDominic Meiser { 19227f756511SDominic Meiser cusparseHandle_t handle; 19237f756511SDominic Meiser cusparseStatus_t stat; 19247f756511SDominic Meiser 19257f756511SDominic Meiser PetscFunctionBegin; 19267f756511SDominic Meiser if (*trifactors) { 1927470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->loTriFactorPtr); 1928470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->upTriFactorPtr); 1929470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->loTriFactorPtrTranspose); 1930470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->upTriFactorPtrTranspose); 19317f756511SDominic Meiser delete (*trifactors)->rpermIndices; 19327f756511SDominic Meiser delete (*trifactors)->cpermIndices; 19337f756511SDominic Meiser delete (*trifactors)->workVector; 19347f756511SDominic Meiser if (handle = (*trifactors)->handle) { 1935c41cb2e2SAlejandro Lamas Daviña stat = cusparseDestroy(handle);CHKERRCUDA(stat); 19367f756511SDominic Meiser } 19377f756511SDominic Meiser delete *trifactors; 19387f756511SDominic Meiser *trifactors = 0; 19397f756511SDominic Meiser } 19407f756511SDominic Meiser PetscFunctionReturn(0); 19417f756511SDominic Meiser } 19427f756511SDominic Meiser 1943