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 69ae82921SPaul Mullowney 73d13b8fdSMatthew G. Knepley #include <petscconf.h> 83d13b8fdSMatthew G. Knepley #include <../src/mat/impls/aij/seq/aij.h> /*I "petscmat.h" I*/ 9087f3262SPaul Mullowney #include <../src/mat/impls/sbaij/seq/sbaij.h> 103d13b8fdSMatthew G. Knepley #include <../src/vec/vec/impls/dvecimpl.h> 11af0996ceSBarry Smith #include <petsc/private/vecimpl.h> 129ae82921SPaul Mullowney #undef VecType 133d13b8fdSMatthew G. Knepley #include <../src/mat/impls/aij/seq/seqcusparse/cusparsematimpl.h> 14bc3f50f2SPaul Mullowney 15e057df02SPaul Mullowney const char *const MatCUSPARSEStorageFormats[] = {"CSR","ELL","HYB","MatCUSPARSEStorageFormat","MAT_CUSPARSE_",0}; 169ae82921SPaul Mullowney 17087f3262SPaul Mullowney static PetscErrorCode MatICCFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,const MatFactorInfo*); 18087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,const MatFactorInfo*); 19087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat,Mat,const MatFactorInfo*); 20087f3262SPaul Mullowney 216fa9248bSJed Brown static PetscErrorCode MatILUFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,IS,const MatFactorInfo*); 226fa9248bSJed Brown static PetscErrorCode MatLUFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,IS,const MatFactorInfo*); 236fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat,Mat,const MatFactorInfo*); 24087f3262SPaul Mullowney 256fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat,Vec,Vec); 266fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat,Vec,Vec); 276fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat,Vec,Vec); 286fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat,Vec,Vec); 294416b707SBarry Smith static PetscErrorCode MatSetFromOptions_SeqAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat); 306fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat,Vec,Vec); 316fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat,Vec,Vec,Vec); 326fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat,Vec,Vec); 336fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat,Vec,Vec,Vec); 349ae82921SPaul Mullowney 357f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix**); 367f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSETriFactorStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct**); 377f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct**,MatCUSPARSEStorageFormat); 387f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors**); 397f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE**); 407f756511SDominic Meiser 41b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSESetStream(Mat A,const cudaStream_t stream) 42b06137fdSPaul Mullowney { 43b06137fdSPaul Mullowney cusparseStatus_t stat; 44b06137fdSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 45b06137fdSPaul Mullowney 46b06137fdSPaul Mullowney PetscFunctionBegin; 47b06137fdSPaul Mullowney cusparsestruct->stream = stream; 48c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetStream(cusparsestruct->handle,cusparsestruct->stream);CHKERRCUDA(stat); 49b06137fdSPaul Mullowney PetscFunctionReturn(0); 50b06137fdSPaul Mullowney } 51b06137fdSPaul Mullowney 52b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSESetHandle(Mat A,const cusparseHandle_t handle) 53b06137fdSPaul Mullowney { 54b06137fdSPaul Mullowney cusparseStatus_t stat; 55b06137fdSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 56b06137fdSPaul Mullowney 57b06137fdSPaul Mullowney PetscFunctionBegin; 586b1cf21dSAlejandro Lamas Daviña if (cusparsestruct->handle != handle) { 5916a2e217SAlejandro Lamas Daviña if (cusparsestruct->handle) { 60c41cb2e2SAlejandro Lamas Daviña stat = cusparseDestroy(cusparsestruct->handle);CHKERRCUDA(stat); 6116a2e217SAlejandro Lamas Daviña } 62b06137fdSPaul Mullowney cusparsestruct->handle = handle; 636b1cf21dSAlejandro Lamas Daviña } 64c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat); 65b06137fdSPaul Mullowney PetscFunctionReturn(0); 66b06137fdSPaul Mullowney } 67b06137fdSPaul Mullowney 68b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSEClearHandle(Mat A) 69b06137fdSPaul Mullowney { 70b06137fdSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 71b06137fdSPaul Mullowney PetscFunctionBegin; 72b06137fdSPaul Mullowney if (cusparsestruct->handle) 73b06137fdSPaul Mullowney cusparsestruct->handle = 0; 74b06137fdSPaul Mullowney PetscFunctionReturn(0); 75b06137fdSPaul Mullowney } 76b06137fdSPaul Mullowney 779ae82921SPaul Mullowney PetscErrorCode MatFactorGetSolverPackage_seqaij_cusparse(Mat A,const MatSolverPackage *type) 789ae82921SPaul Mullowney { 799ae82921SPaul Mullowney PetscFunctionBegin; 809ae82921SPaul Mullowney *type = MATSOLVERCUSPARSE; 819ae82921SPaul Mullowney PetscFunctionReturn(0); 829ae82921SPaul Mullowney } 839ae82921SPaul Mullowney 84c708e6cdSJed Brown /*MC 85087f3262SPaul Mullowney MATSOLVERCUSPARSE = "cusparse" - A matrix type providing triangular solvers for seq matrices 86087f3262SPaul Mullowney on a single GPU of type, seqaijcusparse, aijcusparse, or seqaijcusp, aijcusp. Currently supported 87087f3262SPaul Mullowney algorithms are ILU(k) and ICC(k). Typically, deeper factorizations (larger k) results in poorer 88087f3262SPaul Mullowney performance in the triangular solves. Full LU, and Cholesky decompositions can be solved through the 89087f3262SPaul Mullowney CUSPARSE triangular solve algorithm. However, the performance can be quite poor and thus these 90087f3262SPaul Mullowney algorithms are not recommended. This class does NOT support direct solver operations. 91c708e6cdSJed Brown 929ae82921SPaul Mullowney Level: beginner 93c708e6cdSJed Brown 94c708e6cdSJed Brown .seealso: PCFactorSetMatSolverPackage(), MatSolverPackage, MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation 95c708e6cdSJed Brown M*/ 969ae82921SPaul Mullowney 9742c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat A,MatFactorType ftype,Mat *B) 989ae82921SPaul Mullowney { 999ae82921SPaul Mullowney PetscErrorCode ierr; 100bc3f50f2SPaul Mullowney PetscInt n = A->rmap->n; 1019ae82921SPaul Mullowney 1029ae82921SPaul Mullowney PetscFunctionBegin; 103bc3f50f2SPaul Mullowney ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr); 104404133a2SPaul Mullowney (*B)->factortype = ftype; 105bc3f50f2SPaul Mullowney ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr); 1069ae82921SPaul Mullowney ierr = MatSetType(*B,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 1072205254eSKarl Rupp 108087f3262SPaul Mullowney if (ftype == MAT_FACTOR_LU || ftype == MAT_FACTOR_ILU || ftype == MAT_FACTOR_ILUDT) { 10933d57670SJed Brown ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr); 1109ae82921SPaul Mullowney (*B)->ops->ilufactorsymbolic = MatILUFactorSymbolic_SeqAIJCUSPARSE; 1119ae82921SPaul Mullowney (*B)->ops->lufactorsymbolic = MatLUFactorSymbolic_SeqAIJCUSPARSE; 112087f3262SPaul Mullowney } else if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) { 113087f3262SPaul Mullowney (*B)->ops->iccfactorsymbolic = MatICCFactorSymbolic_SeqAIJCUSPARSE; 114087f3262SPaul Mullowney (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqAIJCUSPARSE; 1159ae82921SPaul Mullowney } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported for CUSPARSE Matrix Types"); 116bc3f50f2SPaul Mullowney 117fa03d054SJed Brown ierr = MatSeqAIJSetPreallocation(*B,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr); 11862a20339SJed Brown ierr = PetscObjectComposeFunction((PetscObject)(*B),"MatFactorGetSolverPackage_C",MatFactorGetSolverPackage_seqaij_cusparse);CHKERRQ(ierr); 1199ae82921SPaul Mullowney PetscFunctionReturn(0); 1209ae82921SPaul Mullowney } 1219ae82921SPaul Mullowney 122bc3f50f2SPaul Mullowney PETSC_INTERN PetscErrorCode MatCUSPARSESetFormat_SeqAIJCUSPARSE(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format) 123ca45077fSPaul Mullowney { 124aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1256e111a19SKarl Rupp 126ca45077fSPaul Mullowney PetscFunctionBegin; 1272692e278SPaul Mullowney #if CUDA_VERSION>=4020 128ca45077fSPaul Mullowney switch (op) { 129e057df02SPaul Mullowney case MAT_CUSPARSE_MULT: 130aa372e3fSPaul Mullowney cusparsestruct->format = format; 131ca45077fSPaul Mullowney break; 132e057df02SPaul Mullowney case MAT_CUSPARSE_ALL: 133aa372e3fSPaul Mullowney cusparsestruct->format = format; 134ca45077fSPaul Mullowney break; 135ca45077fSPaul Mullowney default: 13636d62e41SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unsupported operation %d for MatCUSPARSEFormatOperation. MAT_CUSPARSE_MULT and MAT_CUSPARSE_ALL are currently supported.",op); 137ca45077fSPaul Mullowney } 1382692e278SPaul Mullowney #else 1396c4ed002SBarry Smith if (format==MAT_CUSPARSE_ELL || format==MAT_CUSPARSE_HYB) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"ELL (Ellpack) and HYB (Hybrid) storage format require CUDA 4.2 or later."); 1402692e278SPaul Mullowney #endif 141ca45077fSPaul Mullowney PetscFunctionReturn(0); 142ca45077fSPaul Mullowney } 1439ae82921SPaul Mullowney 144e057df02SPaul Mullowney /*@ 145e057df02SPaul Mullowney MatCUSPARSESetFormat - Sets the storage format of CUSPARSE matrices for a particular 146e057df02SPaul Mullowney operation. Only the MatMult operation can use different GPU storage formats 147aa372e3fSPaul Mullowney for MPIAIJCUSPARSE matrices. 148e057df02SPaul Mullowney Not Collective 149e057df02SPaul Mullowney 150e057df02SPaul Mullowney Input Parameters: 1518468deeeSKarl Rupp + A - Matrix of type SEQAIJCUSPARSE 15236d62e41SPaul 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. 1532692e278SPaul Mullowney - format - MatCUSPARSEStorageFormat (one of MAT_CUSPARSE_CSR, MAT_CUSPARSE_ELL, MAT_CUSPARSE_HYB. The latter two require CUDA 4.2) 154e057df02SPaul Mullowney 155e057df02SPaul Mullowney Output Parameter: 156e057df02SPaul Mullowney 157e057df02SPaul Mullowney Level: intermediate 158e057df02SPaul Mullowney 1598468deeeSKarl Rupp .seealso: MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation 160e057df02SPaul Mullowney @*/ 161e057df02SPaul Mullowney PetscErrorCode MatCUSPARSESetFormat(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format) 162e057df02SPaul Mullowney { 163e057df02SPaul Mullowney PetscErrorCode ierr; 1646e111a19SKarl Rupp 165e057df02SPaul Mullowney PetscFunctionBegin; 166e057df02SPaul Mullowney PetscValidHeaderSpecific(A, MAT_CLASSID,1); 167e057df02SPaul Mullowney ierr = PetscTryMethod(A, "MatCUSPARSESetFormat_C",(Mat,MatCUSPARSEFormatOperation,MatCUSPARSEStorageFormat),(A,op,format));CHKERRQ(ierr); 168e057df02SPaul Mullowney PetscFunctionReturn(0); 169e057df02SPaul Mullowney } 170e057df02SPaul Mullowney 1714416b707SBarry Smith static PetscErrorCode MatSetFromOptions_SeqAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat A) 1729ae82921SPaul Mullowney { 1739ae82921SPaul Mullowney PetscErrorCode ierr; 174e057df02SPaul Mullowney MatCUSPARSEStorageFormat format; 1759ae82921SPaul Mullowney PetscBool flg; 176a183c035SDominic Meiser Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1776e111a19SKarl Rupp 1789ae82921SPaul Mullowney PetscFunctionBegin; 179e55864a3SBarry Smith ierr = PetscOptionsHead(PetscOptionsObject,"SeqAIJCUSPARSE options");CHKERRQ(ierr); 180e057df02SPaul Mullowney ierr = PetscObjectOptionsBegin((PetscObject)A); 1819ae82921SPaul Mullowney if (A->factortype==MAT_FACTOR_NONE) { 182e057df02SPaul Mullowney ierr = PetscOptionsEnum("-mat_cusparse_mult_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV", 183a183c035SDominic Meiser "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr); 184e057df02SPaul Mullowney if (flg) { 185e057df02SPaul Mullowney ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_MULT,format);CHKERRQ(ierr); 186045c96e1SPaul Mullowney } 1879ae82921SPaul Mullowney } 1884c87dfd4SPaul Mullowney ierr = PetscOptionsEnum("-mat_cusparse_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV and TriSolve", 189a183c035SDominic Meiser "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr); 1904c87dfd4SPaul Mullowney if (flg) { 1914c87dfd4SPaul Mullowney ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_ALL,format);CHKERRQ(ierr); 1924c87dfd4SPaul Mullowney } 1939ae82921SPaul Mullowney ierr = PetscOptionsEnd();CHKERRQ(ierr); 1949ae82921SPaul Mullowney PetscFunctionReturn(0); 1959ae82921SPaul Mullowney 1969ae82921SPaul Mullowney } 1979ae82921SPaul Mullowney 1986fa9248bSJed Brown static PetscErrorCode MatILUFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 1999ae82921SPaul Mullowney { 2009ae82921SPaul Mullowney PetscErrorCode ierr; 2019ae82921SPaul Mullowney 2029ae82921SPaul Mullowney PetscFunctionBegin; 2039ae82921SPaul Mullowney ierr = MatILUFactorSymbolic_SeqAIJ(B,A,isrow,iscol,info);CHKERRQ(ierr); 2049ae82921SPaul Mullowney B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJCUSPARSE; 2059ae82921SPaul Mullowney PetscFunctionReturn(0); 2069ae82921SPaul Mullowney } 2079ae82921SPaul Mullowney 2086fa9248bSJed Brown static PetscErrorCode MatLUFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 2099ae82921SPaul Mullowney { 2109ae82921SPaul Mullowney PetscErrorCode ierr; 2119ae82921SPaul Mullowney 2129ae82921SPaul Mullowney PetscFunctionBegin; 2139ae82921SPaul Mullowney ierr = MatLUFactorSymbolic_SeqAIJ(B,A,isrow,iscol,info);CHKERRQ(ierr); 2149ae82921SPaul Mullowney B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJCUSPARSE; 2159ae82921SPaul Mullowney PetscFunctionReturn(0); 2169ae82921SPaul Mullowney } 2179ae82921SPaul Mullowney 218087f3262SPaul Mullowney static PetscErrorCode MatICCFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS perm,const MatFactorInfo *info) 219087f3262SPaul Mullowney { 220087f3262SPaul Mullowney PetscErrorCode ierr; 221087f3262SPaul Mullowney 222087f3262SPaul Mullowney PetscFunctionBegin; 223087f3262SPaul Mullowney ierr = MatICCFactorSymbolic_SeqAIJ(B,A,perm,info);CHKERRQ(ierr); 224087f3262SPaul Mullowney B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJCUSPARSE; 225087f3262SPaul Mullowney PetscFunctionReturn(0); 226087f3262SPaul Mullowney } 227087f3262SPaul Mullowney 228087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS perm,const MatFactorInfo *info) 229087f3262SPaul Mullowney { 230087f3262SPaul Mullowney PetscErrorCode ierr; 231087f3262SPaul Mullowney 232087f3262SPaul Mullowney PetscFunctionBegin; 233087f3262SPaul Mullowney ierr = MatCholeskyFactorSymbolic_SeqAIJ(B,A,perm,info);CHKERRQ(ierr); 234087f3262SPaul Mullowney B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJCUSPARSE; 235087f3262SPaul Mullowney PetscFunctionReturn(0); 236087f3262SPaul Mullowney } 237087f3262SPaul Mullowney 238087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILULowerTriMatrix(Mat A) 2399ae82921SPaul Mullowney { 2409ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2419ae82921SPaul Mullowney PetscInt n = A->rmap->n; 2429ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 243aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 2449ae82921SPaul Mullowney cusparseStatus_t stat; 2459ae82921SPaul Mullowney const PetscInt *ai = a->i,*aj = a->j,*vi; 2469ae82921SPaul Mullowney const MatScalar *aa = a->a,*v; 2479ae82921SPaul Mullowney PetscInt *AiLo, *AjLo; 2489ae82921SPaul Mullowney PetscScalar *AALo; 2499ae82921SPaul Mullowney PetscInt i,nz, nzLower, offset, rowOffset; 250b175d8bbSPaul Mullowney PetscErrorCode ierr; 2519ae82921SPaul Mullowney 2529ae82921SPaul Mullowney PetscFunctionBegin; 253c41cb2e2SAlejandro Lamas Daviña if (A->valid_GPU_matrix == PETSC_CUDA_UNALLOCATED || A->valid_GPU_matrix == PETSC_CUDA_CPU) { 2549ae82921SPaul Mullowney try { 2559ae82921SPaul Mullowney /* first figure out the number of nonzeros in the lower triangular matrix including 1's on the diagonal. */ 2569ae82921SPaul Mullowney nzLower=n+ai[n]-ai[1]; 2579ae82921SPaul Mullowney 2589ae82921SPaul Mullowney /* Allocate Space for the lower triangular matrix */ 259c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AiLo, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr); 260c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AjLo, nzLower*sizeof(PetscInt));CHKERRCUDA(ierr); 261c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AALo, nzLower*sizeof(PetscScalar));CHKERRCUDA(ierr); 2629ae82921SPaul Mullowney 2639ae82921SPaul Mullowney /* Fill the lower triangular matrix */ 2649ae82921SPaul Mullowney AiLo[0] = (PetscInt) 0; 2659ae82921SPaul Mullowney AiLo[n] = nzLower; 2669ae82921SPaul Mullowney AjLo[0] = (PetscInt) 0; 2679ae82921SPaul Mullowney AALo[0] = (MatScalar) 1.0; 2689ae82921SPaul Mullowney v = aa; 2699ae82921SPaul Mullowney vi = aj; 2709ae82921SPaul Mullowney offset = 1; 2719ae82921SPaul Mullowney rowOffset= 1; 2729ae82921SPaul Mullowney for (i=1; i<n; i++) { 2739ae82921SPaul Mullowney nz = ai[i+1] - ai[i]; 274e057df02SPaul Mullowney /* additional 1 for the term on the diagonal */ 2759ae82921SPaul Mullowney AiLo[i] = rowOffset; 2769ae82921SPaul Mullowney rowOffset += nz+1; 2779ae82921SPaul Mullowney 2789ae82921SPaul Mullowney ierr = PetscMemcpy(&(AjLo[offset]), vi, nz*sizeof(PetscInt));CHKERRQ(ierr); 2799ae82921SPaul Mullowney ierr = PetscMemcpy(&(AALo[offset]), v, nz*sizeof(PetscScalar));CHKERRQ(ierr); 2809ae82921SPaul Mullowney 2819ae82921SPaul Mullowney offset += nz; 2829ae82921SPaul Mullowney AjLo[offset] = (PetscInt) i; 2839ae82921SPaul Mullowney AALo[offset] = (MatScalar) 1.0; 2849ae82921SPaul Mullowney offset += 1; 2859ae82921SPaul Mullowney 2869ae82921SPaul Mullowney v += nz; 2879ae82921SPaul Mullowney vi += nz; 2889ae82921SPaul Mullowney } 2892205254eSKarl Rupp 290aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 291aa372e3fSPaul Mullowney loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 2922205254eSKarl Rupp 293aa372e3fSPaul Mullowney /* Create the matrix description */ 294c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUDA(stat); 295c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 296c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat); 297c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_LOWER);CHKERRCUDA(stat); 298c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUDA(stat); 299aa372e3fSPaul Mullowney 300aa372e3fSPaul Mullowney /* Create the solve analysis information */ 301c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUDA(stat); 302aa372e3fSPaul Mullowney 303aa372e3fSPaul Mullowney /* set the operation */ 304aa372e3fSPaul Mullowney loTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 305aa372e3fSPaul Mullowney 306aa372e3fSPaul Mullowney /* set the matrix */ 307aa372e3fSPaul Mullowney loTriFactor->csrMat = new CsrMatrix; 308aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows = n; 309aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols = n; 310aa372e3fSPaul Mullowney loTriFactor->csrMat->num_entries = nzLower; 311aa372e3fSPaul Mullowney 312aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1); 313aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->assign(AiLo, AiLo+n+1); 314aa372e3fSPaul Mullowney 315aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzLower); 316aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->assign(AjLo, AjLo+nzLower); 317aa372e3fSPaul Mullowney 318aa372e3fSPaul Mullowney loTriFactor->csrMat->values = new THRUSTARRAY(nzLower); 319aa372e3fSPaul Mullowney loTriFactor->csrMat->values->assign(AALo, AALo+nzLower); 320aa372e3fSPaul Mullowney 321aa372e3fSPaul Mullowney /* perform the solve analysis */ 322aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp, 323aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr, 324aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(), 325c41cb2e2SAlejandro Lamas Daviña loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUDA(stat); 326aa372e3fSPaul Mullowney 327aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 328aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor; 3292205254eSKarl Rupp 330c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AiLo);CHKERRCUDA(ierr); 331c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AjLo);CHKERRCUDA(ierr); 332c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AALo);CHKERRCUDA(ierr); 3339ae82921SPaul Mullowney } catch(char *ex) { 3349ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 3359ae82921SPaul Mullowney } 3369ae82921SPaul Mullowney } 3379ae82921SPaul Mullowney PetscFunctionReturn(0); 3389ae82921SPaul Mullowney } 3399ae82921SPaul Mullowney 340087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(Mat A) 3419ae82921SPaul Mullowney { 3429ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3439ae82921SPaul Mullowney PetscInt n = A->rmap->n; 3449ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 345aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 3469ae82921SPaul Mullowney cusparseStatus_t stat; 3479ae82921SPaul Mullowney const PetscInt *aj = a->j,*adiag = a->diag,*vi; 3489ae82921SPaul Mullowney const MatScalar *aa = a->a,*v; 3499ae82921SPaul Mullowney PetscInt *AiUp, *AjUp; 3509ae82921SPaul Mullowney PetscScalar *AAUp; 3519ae82921SPaul Mullowney PetscInt i,nz, nzUpper, offset; 3529ae82921SPaul Mullowney PetscErrorCode ierr; 3539ae82921SPaul Mullowney 3549ae82921SPaul Mullowney PetscFunctionBegin; 355c41cb2e2SAlejandro Lamas Daviña if (A->valid_GPU_matrix == PETSC_CUDA_UNALLOCATED || A->valid_GPU_matrix == PETSC_CUDA_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 3849ae82921SPaul Mullowney ierr = PetscMemcpy(&(AjUp[offset+1]), vi, nz*sizeof(PetscInt));CHKERRQ(ierr); 3859ae82921SPaul Mullowney ierr = PetscMemcpy(&(AAUp[offset+1]), v, nz*sizeof(PetscScalar));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); 4319ae82921SPaul Mullowney } catch(char *ex) { 4329ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 4339ae82921SPaul Mullowney } 4349ae82921SPaul Mullowney } 4359ae82921SPaul Mullowney PetscFunctionReturn(0); 4369ae82921SPaul Mullowney } 4379ae82921SPaul Mullowney 438087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(Mat A) 4399ae82921SPaul Mullowney { 4409ae82921SPaul Mullowney PetscErrorCode ierr; 4419ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 4429ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 4439ae82921SPaul Mullowney IS isrow = a->row,iscol = a->icol; 4449ae82921SPaul Mullowney PetscBool row_identity,col_identity; 4459ae82921SPaul Mullowney const PetscInt *r,*c; 4469ae82921SPaul Mullowney PetscInt n = A->rmap->n; 4479ae82921SPaul Mullowney 4489ae82921SPaul Mullowney PetscFunctionBegin; 449087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildILULowerTriMatrix(A);CHKERRQ(ierr); 450087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(A);CHKERRQ(ierr); 4512205254eSKarl Rupp 452aa372e3fSPaul Mullowney cusparseTriFactors->workVector = new THRUSTARRAY; 453aa372e3fSPaul Mullowney cusparseTriFactors->workVector->resize(n); 454aa372e3fSPaul Mullowney cusparseTriFactors->nnz=a->nz; 4559ae82921SPaul Mullowney 456c41cb2e2SAlejandro Lamas Daviña A->valid_GPU_matrix = PETSC_CUDA_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 } 4739ae82921SPaul Mullowney ierr = ISRestoreIndices(iscol,&c);CHKERRQ(ierr); 4749ae82921SPaul Mullowney PetscFunctionReturn(0); 4759ae82921SPaul Mullowney } 4769ae82921SPaul Mullowney 477087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildICCTriMatrices(Mat A) 478087f3262SPaul Mullowney { 479087f3262SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 480087f3262SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 481aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 482aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 483087f3262SPaul Mullowney cusparseStatus_t stat; 484087f3262SPaul Mullowney PetscErrorCode ierr; 485087f3262SPaul Mullowney PetscInt *AiUp, *AjUp; 486087f3262SPaul Mullowney PetscScalar *AAUp; 487087f3262SPaul Mullowney PetscScalar *AALo; 488087f3262SPaul Mullowney PetscInt nzUpper = a->nz,n = A->rmap->n,i,offset,nz,j; 489087f3262SPaul Mullowney Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)A->data; 490087f3262SPaul Mullowney const PetscInt *ai = b->i,*aj = b->j,*vj; 491087f3262SPaul Mullowney const MatScalar *aa = b->a,*v; 492087f3262SPaul Mullowney 493087f3262SPaul Mullowney PetscFunctionBegin; 494c41cb2e2SAlejandro Lamas Daviña if (A->valid_GPU_matrix == PETSC_CUDA_UNALLOCATED || A->valid_GPU_matrix == PETSC_CUDA_CPU) { 495087f3262SPaul Mullowney try { 496087f3262SPaul Mullowney /* Allocate Space for the upper triangular matrix */ 497c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr); 498c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(ierr); 499c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr); 500c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AALo, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr); 501087f3262SPaul Mullowney 502087f3262SPaul Mullowney /* Fill the upper triangular matrix */ 503087f3262SPaul Mullowney AiUp[0]=(PetscInt) 0; 504087f3262SPaul Mullowney AiUp[n]=nzUpper; 505087f3262SPaul Mullowney offset = 0; 506087f3262SPaul Mullowney for (i=0; i<n; i++) { 507087f3262SPaul Mullowney /* set the pointers */ 508087f3262SPaul Mullowney v = aa + ai[i]; 509087f3262SPaul Mullowney vj = aj + ai[i]; 510087f3262SPaul Mullowney nz = ai[i+1] - ai[i] - 1; /* exclude diag[i] */ 511087f3262SPaul Mullowney 512087f3262SPaul Mullowney /* first, set the diagonal elements */ 513087f3262SPaul Mullowney AjUp[offset] = (PetscInt) i; 51409f51544SAlejandro Lamas Daviña AAUp[offset] = (MatScalar)1.0/v[nz]; 515087f3262SPaul Mullowney AiUp[i] = offset; 51609f51544SAlejandro Lamas Daviña AALo[offset] = (MatScalar)1.0/v[nz]; 517087f3262SPaul Mullowney 518087f3262SPaul Mullowney offset+=1; 519087f3262SPaul Mullowney if (nz>0) { 520087f3262SPaul Mullowney ierr = PetscMemcpy(&(AjUp[offset]), vj, nz*sizeof(PetscInt));CHKERRQ(ierr); 521087f3262SPaul Mullowney ierr = PetscMemcpy(&(AAUp[offset]), v, nz*sizeof(PetscScalar));CHKERRQ(ierr); 522087f3262SPaul Mullowney for (j=offset; j<offset+nz; j++) { 523087f3262SPaul Mullowney AAUp[j] = -AAUp[j]; 524087f3262SPaul Mullowney AALo[j] = AAUp[j]/v[nz]; 525087f3262SPaul Mullowney } 526087f3262SPaul Mullowney offset+=nz; 527087f3262SPaul Mullowney } 528087f3262SPaul Mullowney } 529087f3262SPaul Mullowney 530aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 531aa372e3fSPaul Mullowney upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 532087f3262SPaul Mullowney 533aa372e3fSPaul Mullowney /* Create the matrix description */ 534c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUDA(stat); 535c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 536c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat); 537c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat); 538c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUDA(stat); 539087f3262SPaul Mullowney 540aa372e3fSPaul Mullowney /* Create the solve analysis information */ 541c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUDA(stat); 542aa372e3fSPaul Mullowney 543aa372e3fSPaul Mullowney /* set the operation */ 544aa372e3fSPaul Mullowney upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 545aa372e3fSPaul Mullowney 546aa372e3fSPaul Mullowney /* set the matrix */ 547aa372e3fSPaul Mullowney upTriFactor->csrMat = new CsrMatrix; 548aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows = A->rmap->n; 549aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols = A->cmap->n; 550aa372e3fSPaul Mullowney upTriFactor->csrMat->num_entries = a->nz; 551aa372e3fSPaul Mullowney 552aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 553aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1); 554aa372e3fSPaul Mullowney 555aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz); 556aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz); 557aa372e3fSPaul Mullowney 558aa372e3fSPaul Mullowney upTriFactor->csrMat->values = new THRUSTARRAY(a->nz); 559aa372e3fSPaul Mullowney upTriFactor->csrMat->values->assign(AAUp, AAUp+a->nz); 560aa372e3fSPaul Mullowney 561aa372e3fSPaul Mullowney /* perform the solve analysis */ 562aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp, 563aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr, 564aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(), 565c41cb2e2SAlejandro Lamas Daviña upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUDA(stat); 566aa372e3fSPaul Mullowney 567aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 568aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor; 569aa372e3fSPaul Mullowney 570aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 571aa372e3fSPaul Mullowney loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 572aa372e3fSPaul Mullowney 573aa372e3fSPaul Mullowney /* Create the matrix description */ 574c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUDA(stat); 575c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 576c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat); 577c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat); 578c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUDA(stat); 579aa372e3fSPaul Mullowney 580aa372e3fSPaul Mullowney /* Create the solve analysis information */ 581c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUDA(stat); 582aa372e3fSPaul Mullowney 583aa372e3fSPaul Mullowney /* set the operation */ 584aa372e3fSPaul Mullowney loTriFactor->solveOp = CUSPARSE_OPERATION_TRANSPOSE; 585aa372e3fSPaul Mullowney 586aa372e3fSPaul Mullowney /* set the matrix */ 587aa372e3fSPaul Mullowney loTriFactor->csrMat = new CsrMatrix; 588aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows = A->rmap->n; 589aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols = A->cmap->n; 590aa372e3fSPaul Mullowney loTriFactor->csrMat->num_entries = a->nz; 591aa372e3fSPaul Mullowney 592aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 593aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1); 594aa372e3fSPaul Mullowney 595aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz); 596aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz); 597aa372e3fSPaul Mullowney 598aa372e3fSPaul Mullowney loTriFactor->csrMat->values = new THRUSTARRAY(a->nz); 599aa372e3fSPaul Mullowney loTriFactor->csrMat->values->assign(AALo, AALo+a->nz); 600aa372e3fSPaul Mullowney 601aa372e3fSPaul Mullowney /* perform the solve analysis */ 602aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp, 603aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr, 604aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(), 605c41cb2e2SAlejandro Lamas Daviña loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUDA(stat); 606aa372e3fSPaul Mullowney 607aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 608aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor; 609087f3262SPaul Mullowney 610c41cb2e2SAlejandro Lamas Daviña A->valid_GPU_matrix = PETSC_CUDA_BOTH; 611c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AiUp);CHKERRCUDA(ierr); 612c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AjUp);CHKERRCUDA(ierr); 613c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AAUp);CHKERRCUDA(ierr); 614c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AALo);CHKERRCUDA(ierr); 615087f3262SPaul Mullowney } catch(char *ex) { 616087f3262SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 617087f3262SPaul Mullowney } 618087f3262SPaul Mullowney } 619087f3262SPaul Mullowney PetscFunctionReturn(0); 620087f3262SPaul Mullowney } 621087f3262SPaul Mullowney 622087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(Mat A) 6239ae82921SPaul Mullowney { 6249ae82921SPaul Mullowney PetscErrorCode ierr; 625087f3262SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 626087f3262SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 627087f3262SPaul Mullowney IS ip = a->row; 628087f3262SPaul Mullowney const PetscInt *rip; 629087f3262SPaul Mullowney PetscBool perm_identity; 630087f3262SPaul Mullowney PetscInt n = A->rmap->n; 631087f3262SPaul Mullowney 632087f3262SPaul Mullowney PetscFunctionBegin; 633087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildICCTriMatrices(A);CHKERRQ(ierr); 634aa372e3fSPaul Mullowney cusparseTriFactors->workVector = new THRUSTARRAY; 635aa372e3fSPaul Mullowney cusparseTriFactors->workVector->resize(n); 636aa372e3fSPaul Mullowney cusparseTriFactors->nnz=(a->nz-n)*2 + n; 637aa372e3fSPaul Mullowney 638087f3262SPaul Mullowney /*lower triangular indices */ 639087f3262SPaul Mullowney ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); 640087f3262SPaul Mullowney ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 641087f3262SPaul Mullowney if (!perm_identity) { 642aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n); 643aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices->assign(rip, rip+n); 644aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n); 645aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices->assign(rip, rip+n); 646087f3262SPaul Mullowney } 647087f3262SPaul Mullowney ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); 648087f3262SPaul Mullowney PetscFunctionReturn(0); 649087f3262SPaul Mullowney } 650087f3262SPaul Mullowney 6516fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info) 6529ae82921SPaul Mullowney { 6539ae82921SPaul Mullowney Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 6549ae82921SPaul Mullowney IS isrow = b->row,iscol = b->col; 6559ae82921SPaul Mullowney PetscBool row_identity,col_identity; 656b175d8bbSPaul Mullowney PetscErrorCode ierr; 6579ae82921SPaul Mullowney 6589ae82921SPaul Mullowney PetscFunctionBegin; 6599ae82921SPaul Mullowney ierr = MatLUFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr); 660e057df02SPaul Mullowney /* determine which version of MatSolve needs to be used. */ 6619ae82921SPaul Mullowney ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 6629ae82921SPaul Mullowney ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 663bda325fcSPaul Mullowney if (row_identity && col_identity) { 664bda325fcSPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering; 665bda325fcSPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering; 666bda325fcSPaul Mullowney } else { 667bda325fcSPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE; 668bda325fcSPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE; 669bda325fcSPaul Mullowney } 6708dc1d2a3SPaul Mullowney 671e057df02SPaul Mullowney /* get the triangular factors */ 672087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(B);CHKERRQ(ierr); 6739ae82921SPaul Mullowney PetscFunctionReturn(0); 6749ae82921SPaul Mullowney } 6759ae82921SPaul Mullowney 676087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info) 677087f3262SPaul Mullowney { 678087f3262SPaul Mullowney Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 679087f3262SPaul Mullowney IS ip = b->row; 680087f3262SPaul Mullowney PetscBool perm_identity; 681b175d8bbSPaul Mullowney PetscErrorCode ierr; 682087f3262SPaul Mullowney 683087f3262SPaul Mullowney PetscFunctionBegin; 684087f3262SPaul Mullowney ierr = MatCholeskyFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr); 685087f3262SPaul Mullowney 686087f3262SPaul Mullowney /* determine which version of MatSolve needs to be used. */ 687087f3262SPaul Mullowney ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 688087f3262SPaul Mullowney if (perm_identity) { 689087f3262SPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering; 690087f3262SPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering; 691087f3262SPaul Mullowney } else { 692087f3262SPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE; 693087f3262SPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE; 694087f3262SPaul Mullowney } 695087f3262SPaul Mullowney 696087f3262SPaul Mullowney /* get the triangular factors */ 697087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(B);CHKERRQ(ierr); 698087f3262SPaul Mullowney PetscFunctionReturn(0); 699087f3262SPaul Mullowney } 7009ae82921SPaul Mullowney 701b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(Mat A) 702bda325fcSPaul Mullowney { 703bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 704aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 705aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 706aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 707aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 708bda325fcSPaul Mullowney cusparseStatus_t stat; 709aa372e3fSPaul Mullowney cusparseIndexBase_t indexBase; 710aa372e3fSPaul Mullowney cusparseMatrixType_t matrixType; 711aa372e3fSPaul Mullowney cusparseFillMode_t fillMode; 712aa372e3fSPaul Mullowney cusparseDiagType_t diagType; 713b175d8bbSPaul Mullowney 714bda325fcSPaul Mullowney PetscFunctionBegin; 715bda325fcSPaul Mullowney 716aa372e3fSPaul Mullowney /*********************************************/ 717aa372e3fSPaul Mullowney /* Now the Transpose of the Lower Tri Factor */ 718aa372e3fSPaul Mullowney /*********************************************/ 719aa372e3fSPaul Mullowney 720aa372e3fSPaul Mullowney /* allocate space for the transpose of the lower triangular factor */ 721aa372e3fSPaul Mullowney loTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct; 722aa372e3fSPaul Mullowney 723aa372e3fSPaul Mullowney /* set the matrix descriptors of the lower triangular factor */ 724aa372e3fSPaul Mullowney matrixType = cusparseGetMatType(loTriFactor->descr); 725aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(loTriFactor->descr); 726aa372e3fSPaul Mullowney fillMode = cusparseGetMatFillMode(loTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ? 727aa372e3fSPaul Mullowney CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER; 728aa372e3fSPaul Mullowney diagType = cusparseGetMatDiagType(loTriFactor->descr); 729aa372e3fSPaul Mullowney 730aa372e3fSPaul Mullowney /* Create the matrix description */ 731c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&loTriFactorT->descr);CHKERRCUDA(stat); 732c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(loTriFactorT->descr, indexBase);CHKERRCUDA(stat); 733c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(loTriFactorT->descr, matrixType);CHKERRCUDA(stat); 734c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(loTriFactorT->descr, fillMode);CHKERRCUDA(stat); 735c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(loTriFactorT->descr, diagType);CHKERRCUDA(stat); 736aa372e3fSPaul Mullowney 737aa372e3fSPaul Mullowney /* Create the solve analysis information */ 738c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&loTriFactorT->solveInfo);CHKERRCUDA(stat); 739aa372e3fSPaul Mullowney 740aa372e3fSPaul Mullowney /* set the operation */ 741aa372e3fSPaul Mullowney loTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 742aa372e3fSPaul Mullowney 743aa372e3fSPaul Mullowney /* allocate GPU space for the CSC of the lower triangular factor*/ 744aa372e3fSPaul Mullowney loTriFactorT->csrMat = new CsrMatrix; 745aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_rows = loTriFactor->csrMat->num_rows; 746aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_cols = loTriFactor->csrMat->num_cols; 747aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_entries = loTriFactor->csrMat->num_entries; 748aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(loTriFactor->csrMat->num_rows+1); 749aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(loTriFactor->csrMat->num_entries); 750aa372e3fSPaul Mullowney loTriFactorT->csrMat->values = new THRUSTARRAY(loTriFactor->csrMat->num_entries); 751aa372e3fSPaul Mullowney 752aa372e3fSPaul Mullowney /* compute the transpose of the lower triangular factor, i.e. the CSC */ 753aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparseTriFactors->handle, loTriFactor->csrMat->num_rows, 754aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols, loTriFactor->csrMat->num_entries, 755aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 756aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 757aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 758aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 759aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 760aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 761c41cb2e2SAlejandro Lamas Daviña CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat); 762aa372e3fSPaul Mullowney 763aa372e3fSPaul Mullowney /* perform the solve analysis on the transposed matrix */ 764aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactorT->solveOp, 765aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_rows, loTriFactorT->csrMat->num_entries, 766aa372e3fSPaul Mullowney loTriFactorT->descr, loTriFactorT->csrMat->values->data().get(), 767aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), loTriFactorT->csrMat->column_indices->data().get(), 768c41cb2e2SAlejandro Lamas Daviña loTriFactorT->solveInfo);CHKERRCUDA(stat); 769aa372e3fSPaul Mullowney 770aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 771aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtrTranspose = loTriFactorT; 772aa372e3fSPaul Mullowney 773aa372e3fSPaul Mullowney /*********************************************/ 774aa372e3fSPaul Mullowney /* Now the Transpose of the Upper Tri Factor */ 775aa372e3fSPaul Mullowney /*********************************************/ 776aa372e3fSPaul Mullowney 777aa372e3fSPaul Mullowney /* allocate space for the transpose of the upper triangular factor */ 778aa372e3fSPaul Mullowney upTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct; 779aa372e3fSPaul Mullowney 780aa372e3fSPaul Mullowney /* set the matrix descriptors of the upper triangular factor */ 781aa372e3fSPaul Mullowney matrixType = cusparseGetMatType(upTriFactor->descr); 782aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(upTriFactor->descr); 783aa372e3fSPaul Mullowney fillMode = cusparseGetMatFillMode(upTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ? 784aa372e3fSPaul Mullowney CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER; 785aa372e3fSPaul Mullowney diagType = cusparseGetMatDiagType(upTriFactor->descr); 786aa372e3fSPaul Mullowney 787aa372e3fSPaul Mullowney /* Create the matrix description */ 788c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&upTriFactorT->descr);CHKERRCUDA(stat); 789c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(upTriFactorT->descr, indexBase);CHKERRCUDA(stat); 790c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(upTriFactorT->descr, matrixType);CHKERRCUDA(stat); 791c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(upTriFactorT->descr, fillMode);CHKERRCUDA(stat); 792c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(upTriFactorT->descr, diagType);CHKERRCUDA(stat); 793aa372e3fSPaul Mullowney 794aa372e3fSPaul Mullowney /* Create the solve analysis information */ 795c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&upTriFactorT->solveInfo);CHKERRCUDA(stat); 796aa372e3fSPaul Mullowney 797aa372e3fSPaul Mullowney /* set the operation */ 798aa372e3fSPaul Mullowney upTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 799aa372e3fSPaul Mullowney 800aa372e3fSPaul Mullowney /* allocate GPU space for the CSC of the upper triangular factor*/ 801aa372e3fSPaul Mullowney upTriFactorT->csrMat = new CsrMatrix; 802aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_rows = upTriFactor->csrMat->num_rows; 803aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_cols = upTriFactor->csrMat->num_cols; 804aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_entries = upTriFactor->csrMat->num_entries; 805aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(upTriFactor->csrMat->num_rows+1); 806aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(upTriFactor->csrMat->num_entries); 807aa372e3fSPaul Mullowney upTriFactorT->csrMat->values = new THRUSTARRAY(upTriFactor->csrMat->num_entries); 808aa372e3fSPaul Mullowney 809aa372e3fSPaul Mullowney /* compute the transpose of the upper triangular factor, i.e. the CSC */ 810aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparseTriFactors->handle, upTriFactor->csrMat->num_rows, 811aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols, upTriFactor->csrMat->num_entries, 812aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 813aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 814aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 815aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 816aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 817aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 818c41cb2e2SAlejandro Lamas Daviña CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat); 819aa372e3fSPaul Mullowney 820aa372e3fSPaul Mullowney /* perform the solve analysis on the transposed matrix */ 821aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactorT->solveOp, 822aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_rows, upTriFactorT->csrMat->num_entries, 823aa372e3fSPaul Mullowney upTriFactorT->descr, upTriFactorT->csrMat->values->data().get(), 824aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), upTriFactorT->csrMat->column_indices->data().get(), 825c41cb2e2SAlejandro Lamas Daviña upTriFactorT->solveInfo);CHKERRCUDA(stat); 826aa372e3fSPaul Mullowney 827aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 828aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtrTranspose = upTriFactorT; 829bda325fcSPaul Mullowney PetscFunctionReturn(0); 830bda325fcSPaul Mullowney } 831bda325fcSPaul Mullowney 832b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEGenerateTransposeForMult(Mat A) 833bda325fcSPaul Mullowney { 834aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 835aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 836aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 837bda325fcSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 838bda325fcSPaul Mullowney cusparseStatus_t stat; 839aa372e3fSPaul Mullowney cusparseIndexBase_t indexBase; 840b06137fdSPaul Mullowney cudaError_t err; 841b175d8bbSPaul Mullowney 842bda325fcSPaul Mullowney PetscFunctionBegin; 843aa372e3fSPaul Mullowney 844aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 845aa372e3fSPaul Mullowney matstructT = new Mat_SeqAIJCUSPARSEMultStruct; 846c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&matstructT->descr);CHKERRCUDA(stat); 847aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(matstruct->descr); 848c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(matstructT->descr, indexBase);CHKERRCUDA(stat); 849c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(matstructT->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUDA(stat); 850aa372e3fSPaul Mullowney 851b06137fdSPaul Mullowney /* set alpha and beta */ 852c41cb2e2SAlejandro Lamas Daviña err = cudaMalloc((void **)&(matstructT->alpha),sizeof(PetscScalar));CHKERRCUDA(err); 853c41cb2e2SAlejandro Lamas Daviña err = cudaMemcpy(matstructT->alpha,&ALPHA,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 854c41cb2e2SAlejandro Lamas Daviña err = cudaMalloc((void **)&(matstructT->beta),sizeof(PetscScalar));CHKERRCUDA(err); 855c41cb2e2SAlejandro Lamas Daviña err = cudaMemcpy(matstructT->beta,&BETA,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 856c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat); 857b06137fdSPaul Mullowney 858aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 859aa372e3fSPaul Mullowney CsrMatrix *matrix = (CsrMatrix*)matstruct->mat; 860aa372e3fSPaul Mullowney CsrMatrix *matrixT= new CsrMatrix; 861aa372e3fSPaul Mullowney matrixT->num_rows = A->rmap->n; 862aa372e3fSPaul Mullowney matrixT->num_cols = A->cmap->n; 863aa372e3fSPaul Mullowney matrixT->num_entries = a->nz; 864aa372e3fSPaul Mullowney matrixT->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 865aa372e3fSPaul Mullowney matrixT->column_indices = new THRUSTINTARRAY32(a->nz); 866aa372e3fSPaul Mullowney matrixT->values = new THRUSTARRAY(a->nz); 867aa372e3fSPaul Mullowney 868aa372e3fSPaul Mullowney /* compute the transpose of the upper triangular factor, i.e. the CSC */ 869aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(matstruct->descr); 870aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparsestruct->handle, matrix->num_rows, 871aa372e3fSPaul Mullowney matrix->num_cols, matrix->num_entries, 872aa372e3fSPaul Mullowney matrix->values->data().get(), 873aa372e3fSPaul Mullowney matrix->row_offsets->data().get(), 874aa372e3fSPaul Mullowney matrix->column_indices->data().get(), 875aa372e3fSPaul Mullowney matrixT->values->data().get(), 876aa372e3fSPaul Mullowney matrixT->column_indices->data().get(), 877aa372e3fSPaul Mullowney matrixT->row_offsets->data().get(), 878c41cb2e2SAlejandro Lamas Daviña CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat); 879aa372e3fSPaul Mullowney 880aa372e3fSPaul Mullowney /* assign the pointer */ 881aa372e3fSPaul Mullowney matstructT->mat = matrixT; 882aa372e3fSPaul Mullowney 883aa372e3fSPaul Mullowney } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) { 8842692e278SPaul Mullowney #if CUDA_VERSION>=5000 885aa372e3fSPaul Mullowney /* First convert HYB to CSR */ 886aa372e3fSPaul Mullowney CsrMatrix *temp= new CsrMatrix; 887aa372e3fSPaul Mullowney temp->num_rows = A->rmap->n; 888aa372e3fSPaul Mullowney temp->num_cols = A->cmap->n; 889aa372e3fSPaul Mullowney temp->num_entries = a->nz; 890aa372e3fSPaul Mullowney temp->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 891aa372e3fSPaul Mullowney temp->column_indices = new THRUSTINTARRAY32(a->nz); 892aa372e3fSPaul Mullowney temp->values = new THRUSTARRAY(a->nz); 893aa372e3fSPaul Mullowney 8942692e278SPaul Mullowney 895aa372e3fSPaul Mullowney stat = cusparse_hyb2csr(cusparsestruct->handle, 896aa372e3fSPaul Mullowney matstruct->descr, (cusparseHybMat_t)matstruct->mat, 897aa372e3fSPaul Mullowney temp->values->data().get(), 898aa372e3fSPaul Mullowney temp->row_offsets->data().get(), 899c41cb2e2SAlejandro Lamas Daviña temp->column_indices->data().get());CHKERRCUDA(stat); 900aa372e3fSPaul Mullowney 901aa372e3fSPaul Mullowney /* Next, convert CSR to CSC (i.e. the matrix transpose) */ 902aa372e3fSPaul Mullowney CsrMatrix *tempT= new CsrMatrix; 903aa372e3fSPaul Mullowney tempT->num_rows = A->rmap->n; 904aa372e3fSPaul Mullowney tempT->num_cols = A->cmap->n; 905aa372e3fSPaul Mullowney tempT->num_entries = a->nz; 906aa372e3fSPaul Mullowney tempT->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 907aa372e3fSPaul Mullowney tempT->column_indices = new THRUSTINTARRAY32(a->nz); 908aa372e3fSPaul Mullowney tempT->values = new THRUSTARRAY(a->nz); 909aa372e3fSPaul Mullowney 910aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparsestruct->handle, temp->num_rows, 911aa372e3fSPaul Mullowney temp->num_cols, temp->num_entries, 912aa372e3fSPaul Mullowney temp->values->data().get(), 913aa372e3fSPaul Mullowney temp->row_offsets->data().get(), 914aa372e3fSPaul Mullowney temp->column_indices->data().get(), 915aa372e3fSPaul Mullowney tempT->values->data().get(), 916aa372e3fSPaul Mullowney tempT->column_indices->data().get(), 917aa372e3fSPaul Mullowney tempT->row_offsets->data().get(), 918c41cb2e2SAlejandro Lamas Daviña CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat); 919aa372e3fSPaul Mullowney 920aa372e3fSPaul Mullowney /* Last, convert CSC to HYB */ 921aa372e3fSPaul Mullowney cusparseHybMat_t hybMat; 922c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateHybMat(&hybMat);CHKERRCUDA(stat); 923aa372e3fSPaul Mullowney cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ? 924aa372e3fSPaul Mullowney CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO; 925aa372e3fSPaul Mullowney stat = cusparse_csr2hyb(cusparsestruct->handle, A->rmap->n, A->cmap->n, 926aa372e3fSPaul Mullowney matstructT->descr, tempT->values->data().get(), 927aa372e3fSPaul Mullowney tempT->row_offsets->data().get(), 928aa372e3fSPaul Mullowney tempT->column_indices->data().get(), 929c41cb2e2SAlejandro Lamas Daviña hybMat, 0, partition);CHKERRCUDA(stat); 930aa372e3fSPaul Mullowney 931aa372e3fSPaul Mullowney /* assign the pointer */ 932aa372e3fSPaul Mullowney matstructT->mat = hybMat; 933aa372e3fSPaul Mullowney 934aa372e3fSPaul Mullowney /* delete temporaries */ 935aa372e3fSPaul Mullowney if (tempT) { 936aa372e3fSPaul Mullowney if (tempT->values) delete (THRUSTARRAY*) tempT->values; 937aa372e3fSPaul Mullowney if (tempT->column_indices) delete (THRUSTINTARRAY32*) tempT->column_indices; 938aa372e3fSPaul Mullowney if (tempT->row_offsets) delete (THRUSTINTARRAY32*) tempT->row_offsets; 939aa372e3fSPaul Mullowney delete (CsrMatrix*) tempT; 940087f3262SPaul Mullowney } 941aa372e3fSPaul Mullowney if (temp) { 942aa372e3fSPaul Mullowney if (temp->values) delete (THRUSTARRAY*) temp->values; 943aa372e3fSPaul Mullowney if (temp->column_indices) delete (THRUSTINTARRAY32*) temp->column_indices; 944aa372e3fSPaul Mullowney if (temp->row_offsets) delete (THRUSTINTARRAY32*) temp->row_offsets; 945aa372e3fSPaul Mullowney delete (CsrMatrix*) temp; 946aa372e3fSPaul Mullowney } 9472692e278SPaul Mullowney #else 9482692e278SPaul Mullowney SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"ELL (Ellpack) and HYB (Hybrid) storage format for the Matrix Transpose (in MatMultTranspose) require CUDA 5.0 or later."); 9492692e278SPaul Mullowney #endif 950aa372e3fSPaul Mullowney } 951aa372e3fSPaul Mullowney /* assign the compressed row indices */ 952aa372e3fSPaul Mullowney matstructT->cprowIndices = new THRUSTINTARRAY; 953aa372e3fSPaul Mullowney 954aa372e3fSPaul Mullowney /* assign the pointer */ 955aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)A->spptr)->matTranspose = matstructT; 956bda325fcSPaul Mullowney PetscFunctionReturn(0); 957bda325fcSPaul Mullowney } 958bda325fcSPaul Mullowney 9596fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx) 960bda325fcSPaul Mullowney { 961c41cb2e2SAlejandro Lamas Daviña PetscInt n = xx->map->n; 962465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 963465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 964465f34aeSAlejandro Lamas Daviña thrust::device_ptr<const PetscScalar> bGPU; 965465f34aeSAlejandro Lamas Daviña thrust::device_ptr<PetscScalar> xGPU; 966bda325fcSPaul Mullowney cusparseStatus_t stat; 967bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 968aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 969aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 970aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 971b175d8bbSPaul Mullowney PetscErrorCode ierr; 972bda325fcSPaul Mullowney 973bda325fcSPaul Mullowney PetscFunctionBegin; 974aa372e3fSPaul Mullowney /* Analyze the matrix and create the transpose ... on the fly */ 975aa372e3fSPaul Mullowney if (!loTriFactorT && !upTriFactorT) { 976bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr); 977aa372e3fSPaul Mullowney loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 978aa372e3fSPaul Mullowney upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 979bda325fcSPaul Mullowney } 980bda325fcSPaul Mullowney 981bda325fcSPaul Mullowney /* Get the GPU pointers */ 982c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 983c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 984c41cb2e2SAlejandro Lamas Daviña xGPU = thrust::device_pointer_cast(xarray); 985c41cb2e2SAlejandro Lamas Daviña bGPU = thrust::device_pointer_cast(barray); 986bda325fcSPaul Mullowney 987aa372e3fSPaul Mullowney /* First, reorder with the row permutation */ 988c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()), 989c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(bGPU+n, cusparseTriFactors->rpermIndices->end()), 990c41cb2e2SAlejandro Lamas Daviña xGPU); 991aa372e3fSPaul Mullowney 992aa372e3fSPaul Mullowney /* First, solve U */ 993aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp, 994aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_rows, &ALPHA, upTriFactorT->descr, 995aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 996aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 997aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 998aa372e3fSPaul Mullowney upTriFactorT->solveInfo, 999c41cb2e2SAlejandro Lamas Daviña xarray, tempGPU->data().get());CHKERRCUDA(stat); 1000aa372e3fSPaul Mullowney 1001aa372e3fSPaul Mullowney /* Then, solve L */ 1002aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp, 1003aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_rows, &ALPHA, loTriFactorT->descr, 1004aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 1005aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 1006aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 1007aa372e3fSPaul Mullowney loTriFactorT->solveInfo, 1008c41cb2e2SAlejandro Lamas Daviña tempGPU->data().get(), xarray);CHKERRCUDA(stat); 1009aa372e3fSPaul Mullowney 1010aa372e3fSPaul Mullowney /* Last, copy the solution, xGPU, into a temporary with the column permutation ... can't be done in place. */ 1011c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->begin()), 1012c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(xGPU+n, cusparseTriFactors->cpermIndices->end()), 1013aa372e3fSPaul Mullowney tempGPU->begin()); 1014aa372e3fSPaul Mullowney 1015aa372e3fSPaul Mullowney /* Copy the temporary to the full solution. */ 1016c41cb2e2SAlejandro Lamas Daviña thrust::copy(tempGPU->begin(), tempGPU->end(), xGPU); 1017bda325fcSPaul Mullowney 1018bda325fcSPaul Mullowney /* restore */ 1019c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1020c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 1021c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1022087f3262SPaul Mullowney 1023aa372e3fSPaul Mullowney ierr = PetscLogFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 1024bda325fcSPaul Mullowney PetscFunctionReturn(0); 1025bda325fcSPaul Mullowney } 1026bda325fcSPaul Mullowney 10276fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx) 1028bda325fcSPaul Mullowney { 1029465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1030465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 1031bda325fcSPaul Mullowney cusparseStatus_t stat; 1032bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1033aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1034aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1035aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1036b175d8bbSPaul Mullowney PetscErrorCode ierr; 1037bda325fcSPaul Mullowney 1038bda325fcSPaul Mullowney PetscFunctionBegin; 1039aa372e3fSPaul Mullowney /* Analyze the matrix and create the transpose ... on the fly */ 1040aa372e3fSPaul Mullowney if (!loTriFactorT && !upTriFactorT) { 1041bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr); 1042aa372e3fSPaul Mullowney loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1043aa372e3fSPaul Mullowney upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1044bda325fcSPaul Mullowney } 1045bda325fcSPaul Mullowney 1046bda325fcSPaul Mullowney /* Get the GPU pointers */ 1047c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1048c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1049bda325fcSPaul Mullowney 1050aa372e3fSPaul Mullowney /* First, solve U */ 1051aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp, 1052aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_rows, &ALPHA, upTriFactorT->descr, 1053aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 1054aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 1055aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 1056aa372e3fSPaul Mullowney upTriFactorT->solveInfo, 1057c41cb2e2SAlejandro Lamas Daviña barray, tempGPU->data().get());CHKERRCUDA(stat); 1058aa372e3fSPaul Mullowney 1059aa372e3fSPaul Mullowney /* Then, solve L */ 1060aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp, 1061aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_rows, &ALPHA, loTriFactorT->descr, 1062aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 1063aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 1064aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 1065aa372e3fSPaul Mullowney loTriFactorT->solveInfo, 1066c41cb2e2SAlejandro Lamas Daviña tempGPU->data().get(), xarray);CHKERRCUDA(stat); 1067bda325fcSPaul Mullowney 1068bda325fcSPaul Mullowney /* restore */ 1069c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1070c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 1071c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1072aa372e3fSPaul Mullowney ierr = PetscLogFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 1073bda325fcSPaul Mullowney PetscFunctionReturn(0); 1074bda325fcSPaul Mullowney } 1075bda325fcSPaul Mullowney 10766fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx) 10779ae82921SPaul Mullowney { 1078465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1079465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 1080465f34aeSAlejandro Lamas Daviña thrust::device_ptr<const PetscScalar> bGPU; 1081465f34aeSAlejandro Lamas Daviña thrust::device_ptr<PetscScalar> xGPU; 10829ae82921SPaul Mullowney cusparseStatus_t stat; 10839ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1084aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 1085aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 1086aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1087b175d8bbSPaul Mullowney PetscErrorCode ierr; 10889ae82921SPaul Mullowney 10899ae82921SPaul Mullowney PetscFunctionBegin; 1090ebc8f436SDominic Meiser 1091e057df02SPaul Mullowney /* Get the GPU pointers */ 1092c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1093c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1094c41cb2e2SAlejandro Lamas Daviña xGPU = thrust::device_pointer_cast(xarray); 1095c41cb2e2SAlejandro Lamas Daviña bGPU = thrust::device_pointer_cast(barray); 10969ae82921SPaul Mullowney 1097aa372e3fSPaul Mullowney /* First, reorder with the row permutation */ 1098c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()), 1099c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->end()), 1100c41cb2e2SAlejandro Lamas Daviña xGPU); 1101aa372e3fSPaul Mullowney 1102aa372e3fSPaul Mullowney /* Next, solve L */ 1103aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp, 1104aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows, &ALPHA, loTriFactor->descr, 1105aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 1106aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 1107aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 1108aa372e3fSPaul Mullowney loTriFactor->solveInfo, 1109c41cb2e2SAlejandro Lamas Daviña xarray, tempGPU->data().get());CHKERRCUDA(stat); 1110aa372e3fSPaul Mullowney 1111aa372e3fSPaul Mullowney /* Then, solve U */ 1112aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp, 1113aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows, &ALPHA, upTriFactor->descr, 1114aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 1115aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 1116aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 1117aa372e3fSPaul Mullowney upTriFactor->solveInfo, 1118c41cb2e2SAlejandro Lamas Daviña tempGPU->data().get(), xarray);CHKERRCUDA(stat); 1119aa372e3fSPaul Mullowney 1120aa372e3fSPaul Mullowney /* Last, copy the solution, xGPU, into a temporary with the column permutation ... can't be done in place. */ 1121c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->begin()), 1122c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->end()), 1123aa372e3fSPaul Mullowney tempGPU->begin()); 1124aa372e3fSPaul Mullowney 1125aa372e3fSPaul Mullowney /* Copy the temporary to the full solution. */ 1126c41cb2e2SAlejandro Lamas Daviña thrust::copy(tempGPU->begin(), tempGPU->end(), xGPU); 11279ae82921SPaul Mullowney 1128c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1129c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 1130c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1131aa372e3fSPaul Mullowney ierr = PetscLogFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 11329ae82921SPaul Mullowney PetscFunctionReturn(0); 11339ae82921SPaul Mullowney } 11349ae82921SPaul Mullowney 11356fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx) 11369ae82921SPaul Mullowney { 1137465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1138465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 11399ae82921SPaul Mullowney cusparseStatus_t stat; 11409ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1141aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 1142aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 1143aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1144b175d8bbSPaul Mullowney PetscErrorCode ierr; 11459ae82921SPaul Mullowney 11469ae82921SPaul Mullowney PetscFunctionBegin; 1147e057df02SPaul Mullowney /* Get the GPU pointers */ 1148c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1149c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 11509ae82921SPaul Mullowney 1151aa372e3fSPaul Mullowney /* First, solve L */ 1152aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp, 1153aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows, &ALPHA, loTriFactor->descr, 1154aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 1155aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 1156aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 1157aa372e3fSPaul Mullowney loTriFactor->solveInfo, 1158c41cb2e2SAlejandro Lamas Daviña barray, tempGPU->data().get());CHKERRCUDA(stat); 1159aa372e3fSPaul Mullowney 1160aa372e3fSPaul Mullowney /* Next, solve U */ 1161aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp, 1162aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows, &ALPHA, upTriFactor->descr, 1163aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 1164aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 1165aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 1166aa372e3fSPaul Mullowney upTriFactor->solveInfo, 1167c41cb2e2SAlejandro Lamas Daviña tempGPU->data().get(), xarray);CHKERRCUDA(stat); 11689ae82921SPaul Mullowney 1169c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1170c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 1171c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1172aa372e3fSPaul Mullowney ierr = PetscLogFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 11739ae82921SPaul Mullowney PetscFunctionReturn(0); 11749ae82921SPaul Mullowney } 11759ae82921SPaul Mullowney 11766fa9248bSJed Brown static PetscErrorCode MatSeqAIJCUSPARSECopyToGPU(Mat A) 11779ae82921SPaul Mullowney { 11789ae82921SPaul Mullowney 1179aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1180aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 11819ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 11829ae82921SPaul Mullowney PetscInt m = A->rmap->n,*ii,*ridx; 11839ae82921SPaul Mullowney PetscErrorCode ierr; 1184aa372e3fSPaul Mullowney cusparseStatus_t stat; 1185b06137fdSPaul Mullowney cudaError_t err; 11869ae82921SPaul Mullowney 11879ae82921SPaul Mullowney PetscFunctionBegin; 1188c41cb2e2SAlejandro Lamas Daviña if (A->valid_GPU_matrix == PETSC_CUDA_UNALLOCATED || A->valid_GPU_matrix == PETSC_CUDA_CPU) { 11899ae82921SPaul Mullowney ierr = PetscLogEventBegin(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr); 119034d6c7a5SJose E. Roman if (A->assembled && A->nonzerostate == cusparsestruct->nonzerostate && cusparsestruct->format == MAT_CUSPARSE_CSR) { 119134d6c7a5SJose E. Roman CsrMatrix *matrix = (CsrMatrix*)matstruct->mat; 119234d6c7a5SJose E. Roman /* copy values only */ 119334d6c7a5SJose E. Roman matrix->values->assign(a->a, a->a+a->nz); 119434d6c7a5SJose E. Roman } else { 1195ce814652SDominic Meiser Mat_SeqAIJCUSPARSEMultStruct_Destroy(&matstruct,cusparsestruct->format); 11969ae82921SPaul Mullowney try { 1197aa372e3fSPaul Mullowney cusparsestruct->nonzerorow=0; 1198aa372e3fSPaul Mullowney for (int j = 0; j<m; j++) cusparsestruct->nonzerorow += ((a->i[j+1]-a->i[j])>0); 11999ae82921SPaul Mullowney 12009ae82921SPaul Mullowney if (a->compressedrow.use) { 12019ae82921SPaul Mullowney m = a->compressedrow.nrows; 12029ae82921SPaul Mullowney ii = a->compressedrow.i; 12039ae82921SPaul Mullowney ridx = a->compressedrow.rindex; 12049ae82921SPaul Mullowney } else { 1205b06137fdSPaul Mullowney /* Forcing compressed row on the GPU */ 12069ae82921SPaul Mullowney int k=0; 1207854ce69bSBarry Smith ierr = PetscMalloc1(cusparsestruct->nonzerorow+1, &ii);CHKERRQ(ierr); 1208854ce69bSBarry Smith ierr = PetscMalloc1(cusparsestruct->nonzerorow, &ridx);CHKERRQ(ierr); 12099ae82921SPaul Mullowney ii[0]=0; 12109ae82921SPaul Mullowney for (int j = 0; j<m; j++) { 12119ae82921SPaul Mullowney if ((a->i[j+1]-a->i[j])>0) { 12129ae82921SPaul Mullowney ii[k] = a->i[j]; 12139ae82921SPaul Mullowney ridx[k]= j; 12149ae82921SPaul Mullowney k++; 12159ae82921SPaul Mullowney } 12169ae82921SPaul Mullowney } 1217aa372e3fSPaul Mullowney ii[cusparsestruct->nonzerorow] = a->nz; 1218aa372e3fSPaul Mullowney m = cusparsestruct->nonzerorow; 12199ae82921SPaul Mullowney } 12209ae82921SPaul Mullowney 1221aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 1222aa372e3fSPaul Mullowney matstruct = new Mat_SeqAIJCUSPARSEMultStruct; 1223c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&matstruct->descr);CHKERRCUDA(stat); 1224c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(matstruct->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 1225c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(matstruct->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUDA(stat); 12269ae82921SPaul Mullowney 1227c41cb2e2SAlejandro Lamas Daviña err = cudaMalloc((void **)&(matstruct->alpha),sizeof(PetscScalar));CHKERRCUDA(err); 1228c41cb2e2SAlejandro Lamas Daviña err = cudaMemcpy(matstruct->alpha,&ALPHA,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 1229c41cb2e2SAlejandro Lamas Daviña err = cudaMalloc((void **)&(matstruct->beta),sizeof(PetscScalar));CHKERRCUDA(err); 1230c41cb2e2SAlejandro Lamas Daviña err = cudaMemcpy(matstruct->beta,&BETA,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 1231c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat); 1232b06137fdSPaul Mullowney 1233aa372e3fSPaul Mullowney /* Build a hybrid/ellpack matrix if this option is chosen for the storage */ 1234aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1235aa372e3fSPaul Mullowney /* set the matrix */ 1236aa372e3fSPaul Mullowney CsrMatrix *matrix= new CsrMatrix; 1237a65300a6SPaul Mullowney matrix->num_rows = m; 1238aa372e3fSPaul Mullowney matrix->num_cols = A->cmap->n; 1239aa372e3fSPaul Mullowney matrix->num_entries = a->nz; 1240a65300a6SPaul Mullowney matrix->row_offsets = new THRUSTINTARRAY32(m+1); 1241a65300a6SPaul Mullowney matrix->row_offsets->assign(ii, ii + m+1); 12429ae82921SPaul Mullowney 1243aa372e3fSPaul Mullowney matrix->column_indices = new THRUSTINTARRAY32(a->nz); 1244aa372e3fSPaul Mullowney matrix->column_indices->assign(a->j, a->j+a->nz); 1245aa372e3fSPaul Mullowney 1246aa372e3fSPaul Mullowney matrix->values = new THRUSTARRAY(a->nz); 1247aa372e3fSPaul Mullowney matrix->values->assign(a->a, a->a+a->nz); 1248aa372e3fSPaul Mullowney 1249aa372e3fSPaul Mullowney /* assign the pointer */ 1250aa372e3fSPaul Mullowney matstruct->mat = matrix; 1251aa372e3fSPaul Mullowney 1252aa372e3fSPaul Mullowney } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) { 12532692e278SPaul Mullowney #if CUDA_VERSION>=4020 1254aa372e3fSPaul Mullowney CsrMatrix *matrix= new CsrMatrix; 1255a65300a6SPaul Mullowney matrix->num_rows = m; 1256aa372e3fSPaul Mullowney matrix->num_cols = A->cmap->n; 1257aa372e3fSPaul Mullowney matrix->num_entries = a->nz; 1258a65300a6SPaul Mullowney matrix->row_offsets = new THRUSTINTARRAY32(m+1); 1259a65300a6SPaul Mullowney matrix->row_offsets->assign(ii, ii + m+1); 1260aa372e3fSPaul Mullowney 1261aa372e3fSPaul Mullowney matrix->column_indices = new THRUSTINTARRAY32(a->nz); 1262aa372e3fSPaul Mullowney matrix->column_indices->assign(a->j, a->j+a->nz); 1263aa372e3fSPaul Mullowney 1264aa372e3fSPaul Mullowney matrix->values = new THRUSTARRAY(a->nz); 1265aa372e3fSPaul Mullowney matrix->values->assign(a->a, a->a+a->nz); 1266aa372e3fSPaul Mullowney 1267aa372e3fSPaul Mullowney cusparseHybMat_t hybMat; 1268c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateHybMat(&hybMat);CHKERRCUDA(stat); 1269aa372e3fSPaul Mullowney cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ? 1270aa372e3fSPaul Mullowney CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO; 1271a65300a6SPaul Mullowney stat = cusparse_csr2hyb(cusparsestruct->handle, matrix->num_rows, matrix->num_cols, 1272aa372e3fSPaul Mullowney matstruct->descr, matrix->values->data().get(), 1273aa372e3fSPaul Mullowney matrix->row_offsets->data().get(), 1274aa372e3fSPaul Mullowney matrix->column_indices->data().get(), 1275c41cb2e2SAlejandro Lamas Daviña hybMat, 0, partition);CHKERRCUDA(stat); 1276aa372e3fSPaul Mullowney /* assign the pointer */ 1277aa372e3fSPaul Mullowney matstruct->mat = hybMat; 1278aa372e3fSPaul Mullowney 1279aa372e3fSPaul Mullowney if (matrix) { 1280aa372e3fSPaul Mullowney if (matrix->values) delete (THRUSTARRAY*)matrix->values; 1281aa372e3fSPaul Mullowney if (matrix->column_indices) delete (THRUSTINTARRAY32*)matrix->column_indices; 1282aa372e3fSPaul Mullowney if (matrix->row_offsets) delete (THRUSTINTARRAY32*)matrix->row_offsets; 1283aa372e3fSPaul Mullowney delete (CsrMatrix*)matrix; 1284087f3262SPaul Mullowney } 12852692e278SPaul Mullowney #endif 1286087f3262SPaul Mullowney } 1287ca45077fSPaul Mullowney 1288aa372e3fSPaul Mullowney /* assign the compressed row indices */ 1289aa372e3fSPaul Mullowney matstruct->cprowIndices = new THRUSTINTARRAY(m); 1290aa372e3fSPaul Mullowney matstruct->cprowIndices->assign(ridx,ridx+m); 1291aa372e3fSPaul Mullowney 1292aa372e3fSPaul Mullowney /* assign the pointer */ 1293aa372e3fSPaul Mullowney cusparsestruct->mat = matstruct; 1294aa372e3fSPaul Mullowney 12959ae82921SPaul Mullowney if (!a->compressedrow.use) { 12969ae82921SPaul Mullowney ierr = PetscFree(ii);CHKERRQ(ierr); 12979ae82921SPaul Mullowney ierr = PetscFree(ridx);CHKERRQ(ierr); 12989ae82921SPaul Mullowney } 1299aa372e3fSPaul Mullowney cusparsestruct->workVector = new THRUSTARRAY; 1300aa372e3fSPaul Mullowney cusparsestruct->workVector->resize(m); 13019ae82921SPaul Mullowney } catch(char *ex) { 13029ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 13039ae82921SPaul Mullowney } 130434d6c7a5SJose E. Roman cusparsestruct->nonzerostate = A->nonzerostate; 130534d6c7a5SJose E. Roman } 13067d0e52d8SJose E. Roman ierr = WaitForGPU();CHKERRCUDA(ierr); 13077d0e52d8SJose E. Roman A->valid_GPU_matrix = PETSC_CUDA_BOTH; 13089ae82921SPaul Mullowney ierr = PetscLogEventEnd(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr); 13099ae82921SPaul Mullowney } 13109ae82921SPaul Mullowney PetscFunctionReturn(0); 13119ae82921SPaul Mullowney } 13129ae82921SPaul Mullowney 13132a7a6963SBarry Smith static PetscErrorCode MatCreateVecs_SeqAIJCUSPARSE(Mat mat, Vec *right, Vec *left) 13149ae82921SPaul Mullowney { 13159ae82921SPaul Mullowney PetscErrorCode ierr; 131633d57670SJed Brown PetscInt rbs,cbs; 13179ae82921SPaul Mullowney 13189ae82921SPaul Mullowney PetscFunctionBegin; 131933d57670SJed Brown ierr = MatGetBlockSizes(mat,&rbs,&cbs);CHKERRQ(ierr); 13209ae82921SPaul Mullowney if (right) { 1321ce94432eSBarry Smith ierr = VecCreate(PetscObjectComm((PetscObject)mat),right);CHKERRQ(ierr); 13229ae82921SPaul Mullowney ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 132333d57670SJed Brown ierr = VecSetBlockSize(*right,cbs);CHKERRQ(ierr); 1324c41cb2e2SAlejandro Lamas Daviña ierr = VecSetType(*right,VECSEQCUDA);CHKERRQ(ierr); 13259ae82921SPaul Mullowney ierr = PetscLayoutReference(mat->cmap,&(*right)->map);CHKERRQ(ierr); 13269ae82921SPaul Mullowney } 13279ae82921SPaul Mullowney if (left) { 1328ce94432eSBarry Smith ierr = VecCreate(PetscObjectComm((PetscObject)mat),left);CHKERRQ(ierr); 13299ae82921SPaul Mullowney ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 133033d57670SJed Brown ierr = VecSetBlockSize(*left,rbs);CHKERRQ(ierr); 1331c41cb2e2SAlejandro Lamas Daviña ierr = VecSetType(*left,VECSEQCUDA);CHKERRQ(ierr); 13329ae82921SPaul Mullowney ierr = PetscLayoutReference(mat->rmap,&(*left)->map);CHKERRQ(ierr); 13339ae82921SPaul Mullowney } 13349ae82921SPaul Mullowney PetscFunctionReturn(0); 13359ae82921SPaul Mullowney } 13369ae82921SPaul Mullowney 1337c41cb2e2SAlejandro Lamas Daviña struct VecCUDAPlusEquals 1338aa372e3fSPaul Mullowney { 1339aa372e3fSPaul Mullowney template <typename Tuple> 1340aa372e3fSPaul Mullowney __host__ __device__ 1341aa372e3fSPaul Mullowney void operator()(Tuple t) 1342aa372e3fSPaul Mullowney { 1343aa372e3fSPaul Mullowney thrust::get<1>(t) = thrust::get<1>(t) + thrust::get<0>(t); 1344aa372e3fSPaul Mullowney } 1345aa372e3fSPaul Mullowney }; 1346aa372e3fSPaul Mullowney 13476fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy) 13489ae82921SPaul Mullowney { 13499ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1350aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1351*9ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstruct; /* Do not initialize yet, because GPU data may not be available yet */ 1352465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 1353465f34aeSAlejandro Lamas Daviña PetscScalar *yarray; 1354b175d8bbSPaul Mullowney PetscErrorCode ierr; 1355aa372e3fSPaul Mullowney cusparseStatus_t stat; 13569ae82921SPaul Mullowney 13579ae82921SPaul Mullowney PetscFunctionBegin; 135834d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 135934d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 1360c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1361ca6ae6e6SAlejandro Lamas Daviña ierr = VecSet(yy,0);CHKERRQ(ierr); 1362c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(yy,&yarray);CHKERRQ(ierr); 1363*9ff858a8SKarl Rupp matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 1364aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1365aa372e3fSPaul Mullowney CsrMatrix *mat = (CsrMatrix*)matstruct->mat; 1366aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1367aa372e3fSPaul Mullowney mat->num_rows, mat->num_cols, mat->num_entries, 1368b06137fdSPaul Mullowney matstruct->alpha, matstruct->descr, mat->values->data().get(), mat->row_offsets->data().get(), 1369c41cb2e2SAlejandro Lamas Daviña mat->column_indices->data().get(), xarray, matstruct->beta, 1370c41cb2e2SAlejandro Lamas Daviña yarray);CHKERRCUDA(stat); 1371aa372e3fSPaul Mullowney } else { 13722692e278SPaul Mullowney #if CUDA_VERSION>=4020 1373aa372e3fSPaul Mullowney cusparseHybMat_t hybMat = (cusparseHybMat_t)matstruct->mat; 1374aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1375b06137fdSPaul Mullowney matstruct->alpha, matstruct->descr, hybMat, 1376c41cb2e2SAlejandro Lamas Daviña xarray, matstruct->beta, 1377c41cb2e2SAlejandro Lamas Daviña yarray);CHKERRCUDA(stat); 13782692e278SPaul Mullowney #endif 13799ae82921SPaul Mullowney } 1380c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1381c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(yy,&yarray);CHKERRQ(ierr); 1382aa372e3fSPaul Mullowney if (!cusparsestruct->stream) { 1383c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1384ca45077fSPaul Mullowney } 1385aa372e3fSPaul Mullowney ierr = PetscLogFlops(2.0*a->nz - cusparsestruct->nonzerorow);CHKERRQ(ierr); 13869ae82921SPaul Mullowney PetscFunctionReturn(0); 13879ae82921SPaul Mullowney } 13889ae82921SPaul Mullowney 13896fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy) 1390ca45077fSPaul Mullowney { 1391ca45077fSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1392aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1393*9ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstructT; 1394465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 1395465f34aeSAlejandro Lamas Daviña PetscScalar *yarray; 1396b175d8bbSPaul Mullowney PetscErrorCode ierr; 1397aa372e3fSPaul Mullowney cusparseStatus_t stat; 1398ca45077fSPaul Mullowney 1399ca45077fSPaul Mullowney PetscFunctionBegin; 140034d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 140134d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 1402*9ff858a8SKarl Rupp matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1403aa372e3fSPaul Mullowney if (!matstructT) { 1404bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr); 1405aa372e3fSPaul Mullowney matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1406bda325fcSPaul Mullowney } 1407c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1408ca6ae6e6SAlejandro Lamas Daviña ierr = VecSet(yy,0);CHKERRQ(ierr); 1409c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(yy,&yarray);CHKERRQ(ierr); 1410aa372e3fSPaul Mullowney 1411aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1412aa372e3fSPaul Mullowney CsrMatrix *mat = (CsrMatrix*)matstructT->mat; 1413aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1414aa372e3fSPaul Mullowney mat->num_rows, mat->num_cols, 1415b06137fdSPaul Mullowney mat->num_entries, matstructT->alpha, matstructT->descr, 1416aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 1417c41cb2e2SAlejandro Lamas Daviña mat->column_indices->data().get(), xarray, matstructT->beta, 1418c41cb2e2SAlejandro Lamas Daviña yarray);CHKERRCUDA(stat); 1419aa372e3fSPaul Mullowney } else { 14202692e278SPaul Mullowney #if CUDA_VERSION>=4020 1421aa372e3fSPaul Mullowney cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat; 1422aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1423b06137fdSPaul Mullowney matstructT->alpha, matstructT->descr, hybMat, 1424c41cb2e2SAlejandro Lamas Daviña xarray, matstructT->beta, 1425c41cb2e2SAlejandro Lamas Daviña yarray);CHKERRCUDA(stat); 14262692e278SPaul Mullowney #endif 1427ca45077fSPaul Mullowney } 1428c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1429c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(yy,&yarray);CHKERRQ(ierr); 1430aa372e3fSPaul Mullowney if (!cusparsestruct->stream) { 1431c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1432ca45077fSPaul Mullowney } 1433aa372e3fSPaul Mullowney ierr = PetscLogFlops(2.0*a->nz - cusparsestruct->nonzerorow);CHKERRQ(ierr); 1434ca45077fSPaul Mullowney PetscFunctionReturn(0); 1435ca45077fSPaul Mullowney } 1436ca45077fSPaul Mullowney 1437aa372e3fSPaul Mullowney 14386fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz) 14399ae82921SPaul Mullowney { 14409ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1441aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1442*9ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstruct; 1443c41cb2e2SAlejandro Lamas Daviña thrust::device_ptr<PetscScalar> zptr; 1444465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 1445465f34aeSAlejandro Lamas Daviña PetscScalar *zarray; 1446b175d8bbSPaul Mullowney PetscErrorCode ierr; 1447aa372e3fSPaul Mullowney cusparseStatus_t stat; 14486e111a19SKarl Rupp 14499ae82921SPaul Mullowney PetscFunctionBegin; 145034d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 145134d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 1452*9ff858a8SKarl Rupp matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 14539ae82921SPaul Mullowney try { 1454c41cb2e2SAlejandro Lamas Daviña ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); 1455c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 145634d6c7a5SJose E. Roman ierr = VecCUDAGetArrayReadWrite(zz,&zarray);CHKERRQ(ierr); 1457c41cb2e2SAlejandro Lamas Daviña zptr = thrust::device_pointer_cast(zarray); 14589ae82921SPaul Mullowney 1459e057df02SPaul Mullowney /* multiply add */ 1460aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1461aa372e3fSPaul Mullowney CsrMatrix *mat = (CsrMatrix*)matstruct->mat; 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 */ 1465aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1466a65300a6SPaul Mullowney mat->num_rows, mat->num_cols, 1467b06137fdSPaul Mullowney mat->num_entries, matstruct->alpha, matstruct->descr, 1468aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 1469c41cb2e2SAlejandro Lamas Daviña mat->column_indices->data().get(), xarray, matstruct->beta, 1470c41cb2e2SAlejandro Lamas Daviña cusparsestruct->workVector->data().get());CHKERRCUDA(stat); 1471aa372e3fSPaul Mullowney } else { 14722692e278SPaul Mullowney #if CUDA_VERSION>=4020 1473aa372e3fSPaul Mullowney cusparseHybMat_t hybMat = (cusparseHybMat_t)matstruct->mat; 1474a65300a6SPaul Mullowney if (cusparsestruct->workVector->size()) { 1475aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1476b06137fdSPaul Mullowney matstruct->alpha, matstruct->descr, hybMat, 1477c41cb2e2SAlejandro Lamas Daviña xarray, matstruct->beta, 1478c41cb2e2SAlejandro Lamas Daviña cusparsestruct->workVector->data().get());CHKERRCUDA(stat); 1479a65300a6SPaul Mullowney } 14802692e278SPaul Mullowney #endif 1481aa372e3fSPaul Mullowney } 1482aa372e3fSPaul Mullowney 1483aa372e3fSPaul Mullowney /* scatter the data from the temporary into the full vector with a += operation */ 1484c41cb2e2SAlejandro Lamas Daviña thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))), 1485c41cb2e2SAlejandro Lamas Daviña thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))) + cusparsestruct->workVector->size(), 1486c41cb2e2SAlejandro Lamas Daviña VecCUDAPlusEquals()); 1487c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 148834d6c7a5SJose E. Roman ierr = VecCUDARestoreArrayReadWrite(zz,&zarray);CHKERRQ(ierr); 14899ae82921SPaul Mullowney 14909ae82921SPaul Mullowney } catch(char *ex) { 14919ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 14929ae82921SPaul Mullowney } 1493c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 14949ae82921SPaul Mullowney ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 14959ae82921SPaul Mullowney PetscFunctionReturn(0); 14969ae82921SPaul Mullowney } 14979ae82921SPaul Mullowney 14986fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz) 1499ca45077fSPaul Mullowney { 1500ca45077fSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1501aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1502*9ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstructT; 1503c41cb2e2SAlejandro Lamas Daviña thrust::device_ptr<PetscScalar> zptr; 1504465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 1505465f34aeSAlejandro Lamas Daviña PetscScalar *zarray; 1506b175d8bbSPaul Mullowney PetscErrorCode ierr; 1507aa372e3fSPaul Mullowney cusparseStatus_t stat; 15086e111a19SKarl Rupp 1509ca45077fSPaul Mullowney PetscFunctionBegin; 151034d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 151134d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 1512*9ff858a8SKarl Rupp matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1513aa372e3fSPaul Mullowney if (!matstructT) { 1514bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr); 1515aa372e3fSPaul Mullowney matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1516bda325fcSPaul Mullowney } 1517aa372e3fSPaul Mullowney 1518ca45077fSPaul Mullowney try { 1519c41cb2e2SAlejandro Lamas Daviña ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); 1520c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 152134d6c7a5SJose E. Roman ierr = VecCUDAGetArrayReadWrite(zz,&zarray);CHKERRQ(ierr); 1522c41cb2e2SAlejandro Lamas Daviña zptr = thrust::device_pointer_cast(zarray); 1523ca45077fSPaul Mullowney 1524e057df02SPaul Mullowney /* multiply add with matrix transpose */ 1525aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1526aa372e3fSPaul Mullowney CsrMatrix *mat = (CsrMatrix*)matstructT->mat; 1527b06137fdSPaul Mullowney /* here we need to be careful to set the number of rows in the multiply to the 1528b06137fdSPaul Mullowney number of compressed rows in the matrix ... which is equivalent to the 1529b06137fdSPaul Mullowney size of the workVector */ 1530aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1531a65300a6SPaul Mullowney mat->num_rows, mat->num_cols, 1532b06137fdSPaul Mullowney mat->num_entries, matstructT->alpha, matstructT->descr, 1533aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 1534c41cb2e2SAlejandro Lamas Daviña mat->column_indices->data().get(), xarray, matstructT->beta, 1535c41cb2e2SAlejandro Lamas Daviña cusparsestruct->workVector->data().get());CHKERRCUDA(stat); 1536aa372e3fSPaul Mullowney } else { 15372692e278SPaul Mullowney #if CUDA_VERSION>=4020 1538aa372e3fSPaul Mullowney cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat; 1539a65300a6SPaul Mullowney if (cusparsestruct->workVector->size()) { 1540aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1541b06137fdSPaul Mullowney matstructT->alpha, matstructT->descr, hybMat, 1542c41cb2e2SAlejandro Lamas Daviña xarray, matstructT->beta, 1543c41cb2e2SAlejandro Lamas Daviña cusparsestruct->workVector->data().get());CHKERRCUDA(stat); 1544a65300a6SPaul Mullowney } 15452692e278SPaul Mullowney #endif 1546aa372e3fSPaul Mullowney } 1547aa372e3fSPaul Mullowney 1548aa372e3fSPaul Mullowney /* scatter the data from the temporary into the full vector with a += operation */ 1549c41cb2e2SAlejandro Lamas Daviña thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstructT->cprowIndices->begin()))), 1550c41cb2e2SAlejandro Lamas Daviña thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstructT->cprowIndices->begin()))) + cusparsestruct->workVector->size(), 1551c41cb2e2SAlejandro Lamas Daviña VecCUDAPlusEquals()); 1552ca45077fSPaul Mullowney 1553c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 155434d6c7a5SJose E. Roman ierr = VecCUDARestoreArrayReadWrite(zz,&zarray);CHKERRQ(ierr); 1555ca45077fSPaul Mullowney 1556ca45077fSPaul Mullowney } catch(char *ex) { 1557ca45077fSPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 1558ca45077fSPaul Mullowney } 1559c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1560ca45077fSPaul Mullowney ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1561ca45077fSPaul Mullowney PetscFunctionReturn(0); 1562ca45077fSPaul Mullowney } 1563ca45077fSPaul Mullowney 15646fa9248bSJed Brown static PetscErrorCode MatAssemblyEnd_SeqAIJCUSPARSE(Mat A,MatAssemblyType mode) 15659ae82921SPaul Mullowney { 15669ae82921SPaul Mullowney PetscErrorCode ierr; 15676e111a19SKarl Rupp 15689ae82921SPaul Mullowney PetscFunctionBegin; 15699ae82921SPaul Mullowney ierr = MatAssemblyEnd_SeqAIJ(A,mode);CHKERRQ(ierr); 1570bc3f50f2SPaul Mullowney if (A->factortype==MAT_FACTOR_NONE) { 1571e057df02SPaul Mullowney ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 1572bc3f50f2SPaul Mullowney } 15739ae82921SPaul Mullowney if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 1574bbf3fe20SPaul Mullowney A->ops->mult = MatMult_SeqAIJCUSPARSE; 1575bbf3fe20SPaul Mullowney A->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 1576bbf3fe20SPaul Mullowney A->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1577bbf3fe20SPaul Mullowney A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 15789ae82921SPaul Mullowney PetscFunctionReturn(0); 15799ae82921SPaul Mullowney } 15809ae82921SPaul Mullowney 15819ae82921SPaul Mullowney /* --------------------------------------------------------------------------------*/ 1582e057df02SPaul Mullowney /*@ 15839ae82921SPaul Mullowney MatCreateSeqAIJCUSPARSE - Creates a sparse matrix in AIJ (compressed row) format 1584e057df02SPaul Mullowney (the default parallel PETSc format). This matrix will ultimately pushed down 1585e057df02SPaul Mullowney to NVidia GPUs and use the CUSPARSE library for calculations. For good matrix 1586e057df02SPaul Mullowney assembly performance the user should preallocate the matrix storage by setting 1587e057df02SPaul Mullowney the parameter nz (or the array nnz). By setting these parameters accurately, 1588e057df02SPaul Mullowney performance during matrix assembly can be increased by more than a factor of 50. 15899ae82921SPaul Mullowney 15909ae82921SPaul Mullowney Collective on MPI_Comm 15919ae82921SPaul Mullowney 15929ae82921SPaul Mullowney Input Parameters: 15939ae82921SPaul Mullowney + comm - MPI communicator, set to PETSC_COMM_SELF 15949ae82921SPaul Mullowney . m - number of rows 15959ae82921SPaul Mullowney . n - number of columns 15969ae82921SPaul Mullowney . nz - number of nonzeros per row (same for all rows) 15979ae82921SPaul Mullowney - nnz - array containing the number of nonzeros in the various rows 15980298fd71SBarry Smith (possibly different for each row) or NULL 15999ae82921SPaul Mullowney 16009ae82921SPaul Mullowney Output Parameter: 16019ae82921SPaul Mullowney . A - the matrix 16029ae82921SPaul Mullowney 16039ae82921SPaul Mullowney It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 16049ae82921SPaul Mullowney MatXXXXSetPreallocation() paradgm instead of this routine directly. 16059ae82921SPaul Mullowney [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 16069ae82921SPaul Mullowney 16079ae82921SPaul Mullowney Notes: 16089ae82921SPaul Mullowney If nnz is given then nz is ignored 16099ae82921SPaul Mullowney 16109ae82921SPaul Mullowney The AIJ format (also called the Yale sparse matrix format or 16119ae82921SPaul Mullowney compressed row storage), is fully compatible with standard Fortran 77 16129ae82921SPaul Mullowney storage. That is, the stored row and column indices can begin at 16139ae82921SPaul Mullowney either one (as in Fortran) or zero. See the users' manual for details. 16149ae82921SPaul Mullowney 16159ae82921SPaul Mullowney Specify the preallocated storage with either nz or nnz (not both). 16160298fd71SBarry Smith Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory 16179ae82921SPaul Mullowney allocation. For large problems you MUST preallocate memory or you 16189ae82921SPaul Mullowney will get TERRIBLE performance, see the users' manual chapter on matrices. 16199ae82921SPaul Mullowney 16209ae82921SPaul Mullowney By default, this format uses inodes (identical nodes) when possible, to 16219ae82921SPaul Mullowney improve numerical efficiency of matrix-vector products and solves. We 16229ae82921SPaul Mullowney search for consecutive rows with the same nonzero structure, thereby 16239ae82921SPaul Mullowney reusing matrix information to achieve increased efficiency. 16249ae82921SPaul Mullowney 16259ae82921SPaul Mullowney Level: intermediate 16269ae82921SPaul Mullowney 1627e057df02SPaul Mullowney .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ(), MATSEQAIJCUSPARSE, MATAIJCUSPARSE 16289ae82921SPaul Mullowney @*/ 16299ae82921SPaul Mullowney PetscErrorCode MatCreateSeqAIJCUSPARSE(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 16309ae82921SPaul Mullowney { 16319ae82921SPaul Mullowney PetscErrorCode ierr; 16329ae82921SPaul Mullowney 16339ae82921SPaul Mullowney PetscFunctionBegin; 16349ae82921SPaul Mullowney ierr = MatCreate(comm,A);CHKERRQ(ierr); 16359ae82921SPaul Mullowney ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 16369ae82921SPaul Mullowney ierr = MatSetType(*A,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 16379ae82921SPaul Mullowney ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr); 16389ae82921SPaul Mullowney PetscFunctionReturn(0); 16399ae82921SPaul Mullowney } 16409ae82921SPaul Mullowney 16416fa9248bSJed Brown static PetscErrorCode MatDestroy_SeqAIJCUSPARSE(Mat A) 16429ae82921SPaul Mullowney { 16439ae82921SPaul Mullowney PetscErrorCode ierr; 1644ab25e6cbSDominic Meiser 16459ae82921SPaul Mullowney PetscFunctionBegin; 16469ae82921SPaul Mullowney if (A->factortype==MAT_FACTOR_NONE) { 1647c41cb2e2SAlejandro Lamas Daviña if (A->valid_GPU_matrix != PETSC_CUDA_UNALLOCATED) { 1648ab25e6cbSDominic Meiser ierr = Mat_SeqAIJCUSPARSE_Destroy((Mat_SeqAIJCUSPARSE**)&A->spptr);CHKERRQ(ierr); 16499ae82921SPaul Mullowney } 16509ae82921SPaul Mullowney } else { 1651ab25e6cbSDominic Meiser ierr = Mat_SeqAIJCUSPARSETriFactors_Destroy((Mat_SeqAIJCUSPARSETriFactors**)&A->spptr);CHKERRQ(ierr); 1652aa372e3fSPaul Mullowney } 16539ae82921SPaul Mullowney ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr); 16549ae82921SPaul Mullowney PetscFunctionReturn(0); 16559ae82921SPaul Mullowney } 16569ae82921SPaul Mullowney 1657*9ff858a8SKarl Rupp static PetscErrorCode MatDuplicate_SeqAIJCUSPARSE(Mat A,MatDuplicateOption cpvalues,Mat *B) 1658*9ff858a8SKarl Rupp { 1659*9ff858a8SKarl Rupp PetscErrorCode ierr; 1660*9ff858a8SKarl Rupp Mat C; 1661*9ff858a8SKarl Rupp cusparseStatus_t stat; 1662*9ff858a8SKarl Rupp cusparseHandle_t handle=0; 1663*9ff858a8SKarl Rupp 1664*9ff858a8SKarl Rupp PetscFunctionBegin; 1665*9ff858a8SKarl Rupp ierr = MatDuplicate_SeqAIJ(A,cpvalues,B);CHKERRQ(ierr); 1666*9ff858a8SKarl Rupp C = *B; 1667*9ff858a8SKarl Rupp /* inject CUSPARSE-specific stuff */ 1668*9ff858a8SKarl Rupp if (C->factortype==MAT_FACTOR_NONE) { 1669*9ff858a8SKarl Rupp /* you cannot check the inode.use flag here since the matrix was just created. 1670*9ff858a8SKarl Rupp now build a GPU matrix data structure */ 1671*9ff858a8SKarl Rupp C->spptr = new Mat_SeqAIJCUSPARSE; 1672*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->mat = 0; 1673*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->matTranspose = 0; 1674*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->workVector = 0; 1675*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->format = MAT_CUSPARSE_CSR; 1676*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->stream = 0; 1677*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->handle = 0; 1678*9ff858a8SKarl Rupp stat = cusparseCreate(&handle);CHKERRCUDA(stat); 1679*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->handle = handle; 1680*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->stream = 0; 1681*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->nonzerostate = 0; 1682*9ff858a8SKarl Rupp } else { 1683*9ff858a8SKarl Rupp /* NEXT, set the pointers to the triangular factors */ 1684*9ff858a8SKarl Rupp C->spptr = new Mat_SeqAIJCUSPARSETriFactors; 1685*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->loTriFactorPtr = 0; 1686*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->upTriFactorPtr = 0; 1687*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->loTriFactorPtrTranspose = 0; 1688*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->upTriFactorPtrTranspose = 0; 1689*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->rpermIndices = 0; 1690*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->cpermIndices = 0; 1691*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->workVector = 0; 1692*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->handle = 0; 1693*9ff858a8SKarl Rupp stat = cusparseCreate(&handle);CHKERRCUDA(stat); 1694*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->handle = handle; 1695*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->nnz = 0; 1696*9ff858a8SKarl Rupp } 1697*9ff858a8SKarl Rupp 1698*9ff858a8SKarl Rupp C->ops->assemblyend = MatAssemblyEnd_SeqAIJCUSPARSE; 1699*9ff858a8SKarl Rupp C->ops->destroy = MatDestroy_SeqAIJCUSPARSE; 1700*9ff858a8SKarl Rupp C->ops->getvecs = MatCreateVecs_SeqAIJCUSPARSE; 1701*9ff858a8SKarl Rupp C->ops->setfromoptions = MatSetFromOptions_SeqAIJCUSPARSE; 1702*9ff858a8SKarl Rupp C->ops->mult = MatMult_SeqAIJCUSPARSE; 1703*9ff858a8SKarl Rupp C->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 1704*9ff858a8SKarl Rupp C->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1705*9ff858a8SKarl Rupp C->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 1706*9ff858a8SKarl Rupp C->ops->duplicate = MatDuplicate_SeqAIJCUSPARSE; 1707*9ff858a8SKarl Rupp 1708*9ff858a8SKarl Rupp ierr = PetscObjectChangeTypeName((PetscObject)C,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 1709*9ff858a8SKarl Rupp 1710*9ff858a8SKarl Rupp C->valid_GPU_matrix = PETSC_CUDA_UNALLOCATED; 1711*9ff858a8SKarl Rupp 1712*9ff858a8SKarl Rupp ierr = PetscObjectComposeFunction((PetscObject)C, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr); 1713*9ff858a8SKarl Rupp PetscFunctionReturn(0); 1714*9ff858a8SKarl Rupp } 1715*9ff858a8SKarl Rupp 17168cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJCUSPARSE(Mat B) 17179ae82921SPaul Mullowney { 17189ae82921SPaul Mullowney PetscErrorCode ierr; 1719aa372e3fSPaul Mullowney cusparseStatus_t stat; 1720aa372e3fSPaul Mullowney cusparseHandle_t handle=0; 17219ae82921SPaul Mullowney 17229ae82921SPaul Mullowney PetscFunctionBegin; 17239ae82921SPaul Mullowney ierr = MatCreate_SeqAIJ(B);CHKERRQ(ierr); 17249ae82921SPaul Mullowney if (B->factortype==MAT_FACTOR_NONE) { 1725e057df02SPaul Mullowney /* you cannot check the inode.use flag here since the matrix was just created. 1726e057df02SPaul Mullowney now build a GPU matrix data structure */ 17279ae82921SPaul Mullowney B->spptr = new Mat_SeqAIJCUSPARSE; 17289ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->mat = 0; 1729aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->matTranspose = 0; 1730aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->workVector = 0; 1731e057df02SPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->format = MAT_CUSPARSE_CSR; 1732aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->stream = 0; 1733aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->handle = 0; 1734c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreate(&handle);CHKERRCUDA(stat); 1735aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->handle = handle; 1736aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->stream = 0; 1737*9ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)B->spptr)->nonzerostate = 0; 17389ae82921SPaul Mullowney } else { 17399ae82921SPaul Mullowney /* NEXT, set the pointers to the triangular factors */ 1740debe9ee2SPaul Mullowney B->spptr = new Mat_SeqAIJCUSPARSETriFactors; 17419ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtr = 0; 17429ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtr = 0; 1743aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtrTranspose = 0; 1744aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtrTranspose = 0; 1745aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->rpermIndices = 0; 1746aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->cpermIndices = 0; 1747aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->workVector = 0; 1748aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->handle = 0; 1749c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreate(&handle);CHKERRCUDA(stat); 1750aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->handle = handle; 1751aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->nnz = 0; 17529ae82921SPaul Mullowney } 1753aa372e3fSPaul Mullowney 17549ae82921SPaul Mullowney B->ops->assemblyend = MatAssemblyEnd_SeqAIJCUSPARSE; 17559ae82921SPaul Mullowney B->ops->destroy = MatDestroy_SeqAIJCUSPARSE; 17562a7a6963SBarry Smith B->ops->getvecs = MatCreateVecs_SeqAIJCUSPARSE; 17579ae82921SPaul Mullowney B->ops->setfromoptions = MatSetFromOptions_SeqAIJCUSPARSE; 1758ca45077fSPaul Mullowney B->ops->mult = MatMult_SeqAIJCUSPARSE; 1759ca45077fSPaul Mullowney B->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 1760ca45077fSPaul Mullowney B->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1761ca45077fSPaul Mullowney B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 1762*9ff858a8SKarl Rupp B->ops->duplicate = MatDuplicate_SeqAIJCUSPARSE; 17632205254eSKarl Rupp 17649ae82921SPaul Mullowney ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 17652205254eSKarl Rupp 1766c41cb2e2SAlejandro Lamas Daviña B->valid_GPU_matrix = PETSC_CUDA_UNALLOCATED; 17672205254eSKarl Rupp 1768bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr); 17699ae82921SPaul Mullowney PetscFunctionReturn(0); 17709ae82921SPaul Mullowney } 17719ae82921SPaul Mullowney 1772e057df02SPaul Mullowney /*M 1773e057df02SPaul Mullowney MATSEQAIJCUSPARSE - MATAIJCUSPARSE = "(seq)aijcusparse" - A matrix type to be used for sparse matrices. 1774e057df02SPaul Mullowney 1775e057df02SPaul Mullowney A matrix type type whose data resides on Nvidia GPUs. These matrices can be in either 17762692e278SPaul Mullowney CSR, ELL, or Hybrid format. The ELL and HYB formats require CUDA 4.2 or later. 17772692e278SPaul Mullowney All matrix calculations are performed on Nvidia GPUs using the CUSPARSE library. 1778e057df02SPaul Mullowney 1779e057df02SPaul Mullowney Options Database Keys: 1780e057df02SPaul Mullowney + -mat_type aijcusparse - sets the matrix type to "seqaijcusparse" during a call to MatSetFromOptions() 1781aa372e3fSPaul 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). 1782aa372e3fSPaul Mullowney . -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). 1783e057df02SPaul Mullowney 1784e057df02SPaul Mullowney Level: beginner 1785e057df02SPaul Mullowney 17868468deeeSKarl Rupp .seealso: MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation 1787e057df02SPaul Mullowney M*/ 17887f756511SDominic Meiser 178942c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat,MatFactorType,Mat*); 179042c9c57cSBarry Smith 17910f39cd5aSBarry Smith 179229b38603SBarry Smith PETSC_EXTERN PetscErrorCode MatSolverPackageRegister_CUSPARSE(void) 179342c9c57cSBarry Smith { 179442c9c57cSBarry Smith PetscErrorCode ierr; 179542c9c57cSBarry Smith 179642c9c57cSBarry Smith PetscFunctionBegin; 179742c9c57cSBarry Smith ierr = MatSolverPackageRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_LU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 179842c9c57cSBarry Smith ierr = MatSolverPackageRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_CHOLESKY,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 179942c9c57cSBarry Smith ierr = MatSolverPackageRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ILU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 180042c9c57cSBarry Smith ierr = MatSolverPackageRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ICC,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 180142c9c57cSBarry Smith PetscFunctionReturn(0); 180242c9c57cSBarry Smith } 180329b38603SBarry Smith 180481e08676SBarry Smith 18057f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE **cusparsestruct) 18067f756511SDominic Meiser { 18077f756511SDominic Meiser cusparseStatus_t stat; 18087f756511SDominic Meiser cusparseHandle_t handle; 18097f756511SDominic Meiser 18107f756511SDominic Meiser PetscFunctionBegin; 18117f756511SDominic Meiser if (*cusparsestruct) { 18127f756511SDominic Meiser Mat_SeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->mat,(*cusparsestruct)->format); 18137f756511SDominic Meiser Mat_SeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->matTranspose,(*cusparsestruct)->format); 18147f756511SDominic Meiser delete (*cusparsestruct)->workVector; 18157f756511SDominic Meiser if (handle = (*cusparsestruct)->handle) { 1816c41cb2e2SAlejandro Lamas Daviña stat = cusparseDestroy(handle);CHKERRCUDA(stat); 18177f756511SDominic Meiser } 18187f756511SDominic Meiser delete *cusparsestruct; 18197f756511SDominic Meiser *cusparsestruct = 0; 18207f756511SDominic Meiser } 18217f756511SDominic Meiser PetscFunctionReturn(0); 18227f756511SDominic Meiser } 18237f756511SDominic Meiser 18247f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix **mat) 18257f756511SDominic Meiser { 18267f756511SDominic Meiser PetscFunctionBegin; 18277f756511SDominic Meiser if (*mat) { 18287f756511SDominic Meiser delete (*mat)->values; 18297f756511SDominic Meiser delete (*mat)->column_indices; 18307f756511SDominic Meiser delete (*mat)->row_offsets; 18317f756511SDominic Meiser delete *mat; 18327f756511SDominic Meiser *mat = 0; 18337f756511SDominic Meiser } 18347f756511SDominic Meiser PetscFunctionReturn(0); 18357f756511SDominic Meiser } 18367f756511SDominic Meiser 18377f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSETriFactorStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct **trifactor) 18387f756511SDominic Meiser { 18397f756511SDominic Meiser cusparseStatus_t stat; 18407f756511SDominic Meiser PetscErrorCode ierr; 18417f756511SDominic Meiser 18427f756511SDominic Meiser PetscFunctionBegin; 18437f756511SDominic Meiser if (*trifactor) { 1844c41cb2e2SAlejandro Lamas Daviña if ((*trifactor)->descr) { stat = cusparseDestroyMatDescr((*trifactor)->descr);CHKERRCUDA(stat); } 1845c41cb2e2SAlejandro Lamas Daviña if ((*trifactor)->solveInfo) { stat = cusparseDestroySolveAnalysisInfo((*trifactor)->solveInfo);CHKERRCUDA(stat); } 18467f756511SDominic Meiser ierr = CsrMatrix_Destroy(&(*trifactor)->csrMat);CHKERRQ(ierr); 18477f756511SDominic Meiser delete *trifactor; 18487f756511SDominic Meiser *trifactor = 0; 18497f756511SDominic Meiser } 18507f756511SDominic Meiser PetscFunctionReturn(0); 18517f756511SDominic Meiser } 18527f756511SDominic Meiser 18537f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct **matstruct,MatCUSPARSEStorageFormat format) 18547f756511SDominic Meiser { 18557f756511SDominic Meiser CsrMatrix *mat; 18567f756511SDominic Meiser cusparseStatus_t stat; 18577f756511SDominic Meiser cudaError_t err; 18587f756511SDominic Meiser 18597f756511SDominic Meiser PetscFunctionBegin; 18607f756511SDominic Meiser if (*matstruct) { 18617f756511SDominic Meiser if ((*matstruct)->mat) { 18627f756511SDominic Meiser if (format==MAT_CUSPARSE_ELL || format==MAT_CUSPARSE_HYB) { 18637f756511SDominic Meiser cusparseHybMat_t hybMat = (cusparseHybMat_t)(*matstruct)->mat; 1864c41cb2e2SAlejandro Lamas Daviña stat = cusparseDestroyHybMat(hybMat);CHKERRCUDA(stat); 18657f756511SDominic Meiser } else { 18667f756511SDominic Meiser mat = (CsrMatrix*)(*matstruct)->mat; 18677f756511SDominic Meiser CsrMatrix_Destroy(&mat); 18687f756511SDominic Meiser } 18697f756511SDominic Meiser } 1870c41cb2e2SAlejandro Lamas Daviña if ((*matstruct)->descr) { stat = cusparseDestroyMatDescr((*matstruct)->descr);CHKERRCUDA(stat); } 18717f756511SDominic Meiser delete (*matstruct)->cprowIndices; 1872c41cb2e2SAlejandro Lamas Daviña if ((*matstruct)->alpha) { err=cudaFree((*matstruct)->alpha);CHKERRCUDA(err); } 1873c41cb2e2SAlejandro Lamas Daviña if ((*matstruct)->beta) { err=cudaFree((*matstruct)->beta);CHKERRCUDA(err); } 18747f756511SDominic Meiser delete *matstruct; 18757f756511SDominic Meiser *matstruct = 0; 18767f756511SDominic Meiser } 18777f756511SDominic Meiser PetscFunctionReturn(0); 18787f756511SDominic Meiser } 18797f756511SDominic Meiser 18807f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors** trifactors) 18817f756511SDominic Meiser { 18827f756511SDominic Meiser cusparseHandle_t handle; 18837f756511SDominic Meiser cusparseStatus_t stat; 18847f756511SDominic Meiser 18857f756511SDominic Meiser PetscFunctionBegin; 18867f756511SDominic Meiser if (*trifactors) { 18877f756511SDominic Meiser Mat_SeqAIJCUSPARSETriFactorStruct_Destroy(&(*trifactors)->loTriFactorPtr); 18887f756511SDominic Meiser Mat_SeqAIJCUSPARSETriFactorStruct_Destroy(&(*trifactors)->upTriFactorPtr); 18897f756511SDominic Meiser Mat_SeqAIJCUSPARSETriFactorStruct_Destroy(&(*trifactors)->loTriFactorPtrTranspose); 18907f756511SDominic Meiser Mat_SeqAIJCUSPARSETriFactorStruct_Destroy(&(*trifactors)->upTriFactorPtrTranspose); 18917f756511SDominic Meiser delete (*trifactors)->rpermIndices; 18927f756511SDominic Meiser delete (*trifactors)->cpermIndices; 18937f756511SDominic Meiser delete (*trifactors)->workVector; 18947f756511SDominic Meiser if (handle = (*trifactors)->handle) { 1895c41cb2e2SAlejandro Lamas Daviña stat = cusparseDestroy(handle);CHKERRCUDA(stat); 18967f756511SDominic Meiser } 18977f756511SDominic Meiser delete *trifactors; 18987f756511SDominic Meiser *trifactors = 0; 18997f756511SDominic Meiser } 19007f756511SDominic Meiser PetscFunctionReturn(0); 19017f756511SDominic Meiser } 19027f756511SDominic Meiser 1903