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**); 36470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct**); 37470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct**,MatCUSPARSEStorageFormat); 38470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors**); 39470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSE_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 77ea799195SBarry Smith PetscErrorCode MatFactorGetSolverType_seqaij_cusparse(Mat A,MatSolverType *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 943ca39a21SBarry Smith .seealso: PCFactorSetMatSolverType(), MatSolverType, 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); 1183ca39a21SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)(*B),"MatFactorGetSolverType_C",MatFactorGetSolverType_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; 127ca45077fSPaul Mullowney switch (op) { 128e057df02SPaul Mullowney case MAT_CUSPARSE_MULT: 129aa372e3fSPaul Mullowney cusparsestruct->format = format; 130ca45077fSPaul Mullowney break; 131e057df02SPaul Mullowney case MAT_CUSPARSE_ALL: 132aa372e3fSPaul Mullowney cusparsestruct->format = format; 133ca45077fSPaul Mullowney break; 134ca45077fSPaul Mullowney default: 13536d62e41SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unsupported operation %d for MatCUSPARSEFormatOperation. MAT_CUSPARSE_MULT and MAT_CUSPARSE_ALL are currently supported.",op); 136ca45077fSPaul Mullowney } 137ca45077fSPaul Mullowney PetscFunctionReturn(0); 138ca45077fSPaul Mullowney } 1399ae82921SPaul Mullowney 140e057df02SPaul Mullowney /*@ 141e057df02SPaul Mullowney MatCUSPARSESetFormat - Sets the storage format of CUSPARSE matrices for a particular 142e057df02SPaul Mullowney operation. Only the MatMult operation can use different GPU storage formats 143aa372e3fSPaul Mullowney for MPIAIJCUSPARSE matrices. 144e057df02SPaul Mullowney Not Collective 145e057df02SPaul Mullowney 146e057df02SPaul Mullowney Input Parameters: 1478468deeeSKarl Rupp + A - Matrix of type SEQAIJCUSPARSE 14836d62e41SPaul 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. 1492692e278SPaul Mullowney - format - MatCUSPARSEStorageFormat (one of MAT_CUSPARSE_CSR, MAT_CUSPARSE_ELL, MAT_CUSPARSE_HYB. The latter two require CUDA 4.2) 150e057df02SPaul Mullowney 151e057df02SPaul Mullowney Output Parameter: 152e057df02SPaul Mullowney 153e057df02SPaul Mullowney Level: intermediate 154e057df02SPaul Mullowney 1558468deeeSKarl Rupp .seealso: MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation 156e057df02SPaul Mullowney @*/ 157e057df02SPaul Mullowney PetscErrorCode MatCUSPARSESetFormat(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format) 158e057df02SPaul Mullowney { 159e057df02SPaul Mullowney PetscErrorCode ierr; 1606e111a19SKarl Rupp 161e057df02SPaul Mullowney PetscFunctionBegin; 162e057df02SPaul Mullowney PetscValidHeaderSpecific(A, MAT_CLASSID,1); 163e057df02SPaul Mullowney ierr = PetscTryMethod(A, "MatCUSPARSESetFormat_C",(Mat,MatCUSPARSEFormatOperation,MatCUSPARSEStorageFormat),(A,op,format));CHKERRQ(ierr); 164e057df02SPaul Mullowney PetscFunctionReturn(0); 165e057df02SPaul Mullowney } 166e057df02SPaul Mullowney 1674416b707SBarry Smith static PetscErrorCode MatSetFromOptions_SeqAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat A) 1689ae82921SPaul Mullowney { 1699ae82921SPaul Mullowney PetscErrorCode ierr; 170e057df02SPaul Mullowney MatCUSPARSEStorageFormat format; 1719ae82921SPaul Mullowney PetscBool flg; 172a183c035SDominic Meiser Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1736e111a19SKarl Rupp 1749ae82921SPaul Mullowney PetscFunctionBegin; 175e55864a3SBarry Smith ierr = PetscOptionsHead(PetscOptionsObject,"SeqAIJCUSPARSE options");CHKERRQ(ierr); 1769ae82921SPaul Mullowney if (A->factortype==MAT_FACTOR_NONE) { 177e057df02SPaul Mullowney ierr = PetscOptionsEnum("-mat_cusparse_mult_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV", 178a183c035SDominic Meiser "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr); 179e057df02SPaul Mullowney if (flg) { 180e057df02SPaul Mullowney ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_MULT,format);CHKERRQ(ierr); 181045c96e1SPaul Mullowney } 1829ae82921SPaul Mullowney } 1834c87dfd4SPaul Mullowney ierr = PetscOptionsEnum("-mat_cusparse_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV and TriSolve", 184a183c035SDominic Meiser "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr); 1854c87dfd4SPaul Mullowney if (flg) { 1864c87dfd4SPaul Mullowney ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_ALL,format);CHKERRQ(ierr); 1874c87dfd4SPaul Mullowney } 1880af67c1bSStefano Zampini ierr = PetscOptionsTail();CHKERRQ(ierr); 1899ae82921SPaul Mullowney PetscFunctionReturn(0); 1909ae82921SPaul Mullowney 1919ae82921SPaul Mullowney } 1929ae82921SPaul Mullowney 1936fa9248bSJed Brown static PetscErrorCode MatILUFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 1949ae82921SPaul Mullowney { 1959ae82921SPaul Mullowney PetscErrorCode ierr; 1969ae82921SPaul Mullowney 1979ae82921SPaul Mullowney PetscFunctionBegin; 1989ae82921SPaul Mullowney ierr = MatILUFactorSymbolic_SeqAIJ(B,A,isrow,iscol,info);CHKERRQ(ierr); 1999ae82921SPaul Mullowney B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJCUSPARSE; 2009ae82921SPaul Mullowney PetscFunctionReturn(0); 2019ae82921SPaul Mullowney } 2029ae82921SPaul Mullowney 2036fa9248bSJed Brown static PetscErrorCode MatLUFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 2049ae82921SPaul Mullowney { 2059ae82921SPaul Mullowney PetscErrorCode ierr; 2069ae82921SPaul Mullowney 2079ae82921SPaul Mullowney PetscFunctionBegin; 2089ae82921SPaul Mullowney ierr = MatLUFactorSymbolic_SeqAIJ(B,A,isrow,iscol,info);CHKERRQ(ierr); 2099ae82921SPaul Mullowney B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJCUSPARSE; 2109ae82921SPaul Mullowney PetscFunctionReturn(0); 2119ae82921SPaul Mullowney } 2129ae82921SPaul Mullowney 213087f3262SPaul Mullowney static PetscErrorCode MatICCFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS perm,const MatFactorInfo *info) 214087f3262SPaul Mullowney { 215087f3262SPaul Mullowney PetscErrorCode ierr; 216087f3262SPaul Mullowney 217087f3262SPaul Mullowney PetscFunctionBegin; 218087f3262SPaul Mullowney ierr = MatICCFactorSymbolic_SeqAIJ(B,A,perm,info);CHKERRQ(ierr); 219087f3262SPaul Mullowney B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJCUSPARSE; 220087f3262SPaul Mullowney PetscFunctionReturn(0); 221087f3262SPaul Mullowney } 222087f3262SPaul Mullowney 223087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS perm,const MatFactorInfo *info) 224087f3262SPaul Mullowney { 225087f3262SPaul Mullowney PetscErrorCode ierr; 226087f3262SPaul Mullowney 227087f3262SPaul Mullowney PetscFunctionBegin; 228087f3262SPaul Mullowney ierr = MatCholeskyFactorSymbolic_SeqAIJ(B,A,perm,info);CHKERRQ(ierr); 229087f3262SPaul Mullowney B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJCUSPARSE; 230087f3262SPaul Mullowney PetscFunctionReturn(0); 231087f3262SPaul Mullowney } 232087f3262SPaul Mullowney 233087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILULowerTriMatrix(Mat A) 2349ae82921SPaul Mullowney { 2359ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2369ae82921SPaul Mullowney PetscInt n = A->rmap->n; 2379ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 238aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 2399ae82921SPaul Mullowney cusparseStatus_t stat; 2409ae82921SPaul Mullowney const PetscInt *ai = a->i,*aj = a->j,*vi; 2419ae82921SPaul Mullowney const MatScalar *aa = a->a,*v; 2429ae82921SPaul Mullowney PetscInt *AiLo, *AjLo; 2439ae82921SPaul Mullowney PetscScalar *AALo; 2449ae82921SPaul Mullowney PetscInt i,nz, nzLower, offset, rowOffset; 245b175d8bbSPaul Mullowney PetscErrorCode ierr; 2469ae82921SPaul Mullowney 2479ae82921SPaul Mullowney PetscFunctionBegin; 248cf00fe3bSKarl Rupp if (!n) PetscFunctionReturn(0); 249b8ced49eSKarl Rupp if (A->valid_GPU_matrix == PETSC_OFFLOAD_UNALLOCATED || A->valid_GPU_matrix == PETSC_OFFLOAD_CPU) { 2509ae82921SPaul Mullowney try { 2519ae82921SPaul Mullowney /* first figure out the number of nonzeros in the lower triangular matrix including 1's on the diagonal. */ 2529ae82921SPaul Mullowney nzLower=n+ai[n]-ai[1]; 2539ae82921SPaul Mullowney 2549ae82921SPaul Mullowney /* Allocate Space for the lower triangular matrix */ 255c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AiLo, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr); 256c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AjLo, nzLower*sizeof(PetscInt));CHKERRCUDA(ierr); 257c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AALo, nzLower*sizeof(PetscScalar));CHKERRCUDA(ierr); 2589ae82921SPaul Mullowney 2599ae82921SPaul Mullowney /* Fill the lower triangular matrix */ 2609ae82921SPaul Mullowney AiLo[0] = (PetscInt) 0; 2619ae82921SPaul Mullowney AiLo[n] = nzLower; 2629ae82921SPaul Mullowney AjLo[0] = (PetscInt) 0; 2639ae82921SPaul Mullowney AALo[0] = (MatScalar) 1.0; 2649ae82921SPaul Mullowney v = aa; 2659ae82921SPaul Mullowney vi = aj; 2669ae82921SPaul Mullowney offset = 1; 2679ae82921SPaul Mullowney rowOffset= 1; 2689ae82921SPaul Mullowney for (i=1; i<n; i++) { 2699ae82921SPaul Mullowney nz = ai[i+1] - ai[i]; 270e057df02SPaul Mullowney /* additional 1 for the term on the diagonal */ 2719ae82921SPaul Mullowney AiLo[i] = rowOffset; 2729ae82921SPaul Mullowney rowOffset += nz+1; 2739ae82921SPaul Mullowney 274580bdb30SBarry Smith ierr = PetscArraycpy(&(AjLo[offset]), vi, nz);CHKERRQ(ierr); 275580bdb30SBarry Smith ierr = PetscArraycpy(&(AALo[offset]), v, nz);CHKERRQ(ierr); 2769ae82921SPaul Mullowney 2779ae82921SPaul Mullowney offset += nz; 2789ae82921SPaul Mullowney AjLo[offset] = (PetscInt) i; 2799ae82921SPaul Mullowney AALo[offset] = (MatScalar) 1.0; 2809ae82921SPaul Mullowney offset += 1; 2819ae82921SPaul Mullowney 2829ae82921SPaul Mullowney v += nz; 2839ae82921SPaul Mullowney vi += nz; 2849ae82921SPaul Mullowney } 2852205254eSKarl Rupp 286aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 287aa372e3fSPaul Mullowney loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 2882205254eSKarl Rupp 289aa372e3fSPaul Mullowney /* Create the matrix description */ 290c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUDA(stat); 291c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 292c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat); 293c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_LOWER);CHKERRCUDA(stat); 294c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUDA(stat); 295aa372e3fSPaul Mullowney 296aa372e3fSPaul Mullowney /* Create the solve analysis information */ 297c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUDA(stat); 298aa372e3fSPaul Mullowney 299aa372e3fSPaul Mullowney /* set the operation */ 300aa372e3fSPaul Mullowney loTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 301aa372e3fSPaul Mullowney 302aa372e3fSPaul Mullowney /* set the matrix */ 303aa372e3fSPaul Mullowney loTriFactor->csrMat = new CsrMatrix; 304aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows = n; 305aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols = n; 306aa372e3fSPaul Mullowney loTriFactor->csrMat->num_entries = nzLower; 307aa372e3fSPaul Mullowney 308aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1); 309aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->assign(AiLo, AiLo+n+1); 310aa372e3fSPaul Mullowney 311aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzLower); 312aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->assign(AjLo, AjLo+nzLower); 313aa372e3fSPaul Mullowney 314aa372e3fSPaul Mullowney loTriFactor->csrMat->values = new THRUSTARRAY(nzLower); 315aa372e3fSPaul Mullowney loTriFactor->csrMat->values->assign(AALo, AALo+nzLower); 316aa372e3fSPaul Mullowney 317aa372e3fSPaul Mullowney /* perform the solve analysis */ 318aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp, 319aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr, 320aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(), 321c41cb2e2SAlejandro Lamas Daviña loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUDA(stat); 322aa372e3fSPaul Mullowney 323aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 324aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor; 3252205254eSKarl Rupp 326c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AiLo);CHKERRCUDA(ierr); 327c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AjLo);CHKERRCUDA(ierr); 328c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AALo);CHKERRCUDA(ierr); 3294863603aSSatish Balay ierr = PetscLogCpuToGpu((n+1+nzLower)*sizeof(int)+nzLower*sizeof(PetscScalar));CHKERRQ(ierr); 3309ae82921SPaul Mullowney } catch(char *ex) { 3319ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 3329ae82921SPaul Mullowney } 3339ae82921SPaul Mullowney } 3349ae82921SPaul Mullowney PetscFunctionReturn(0); 3359ae82921SPaul Mullowney } 3369ae82921SPaul Mullowney 337087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(Mat A) 3389ae82921SPaul Mullowney { 3399ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3409ae82921SPaul Mullowney PetscInt n = A->rmap->n; 3419ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 342aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 3439ae82921SPaul Mullowney cusparseStatus_t stat; 3449ae82921SPaul Mullowney const PetscInt *aj = a->j,*adiag = a->diag,*vi; 3459ae82921SPaul Mullowney const MatScalar *aa = a->a,*v; 3469ae82921SPaul Mullowney PetscInt *AiUp, *AjUp; 3479ae82921SPaul Mullowney PetscScalar *AAUp; 3489ae82921SPaul Mullowney PetscInt i,nz, nzUpper, offset; 3499ae82921SPaul Mullowney PetscErrorCode ierr; 3509ae82921SPaul Mullowney 3519ae82921SPaul Mullowney PetscFunctionBegin; 352cf00fe3bSKarl Rupp if (!n) PetscFunctionReturn(0); 353b8ced49eSKarl Rupp if (A->valid_GPU_matrix == PETSC_OFFLOAD_UNALLOCATED || A->valid_GPU_matrix == PETSC_OFFLOAD_CPU) { 3549ae82921SPaul Mullowney try { 3559ae82921SPaul Mullowney /* next, figure out the number of nonzeros in the upper triangular matrix. */ 3569ae82921SPaul Mullowney nzUpper = adiag[0]-adiag[n]; 3579ae82921SPaul Mullowney 3589ae82921SPaul Mullowney /* Allocate Space for the upper triangular matrix */ 359c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr); 360c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(ierr); 361c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr); 3629ae82921SPaul Mullowney 3639ae82921SPaul Mullowney /* Fill the upper triangular matrix */ 3649ae82921SPaul Mullowney AiUp[0]=(PetscInt) 0; 3659ae82921SPaul Mullowney AiUp[n]=nzUpper; 3669ae82921SPaul Mullowney offset = nzUpper; 3679ae82921SPaul Mullowney for (i=n-1; i>=0; i--) { 3689ae82921SPaul Mullowney v = aa + adiag[i+1] + 1; 3699ae82921SPaul Mullowney vi = aj + adiag[i+1] + 1; 3709ae82921SPaul Mullowney 371e057df02SPaul Mullowney /* number of elements NOT on the diagonal */ 3729ae82921SPaul Mullowney nz = adiag[i] - adiag[i+1]-1; 3739ae82921SPaul Mullowney 374e057df02SPaul Mullowney /* decrement the offset */ 3759ae82921SPaul Mullowney offset -= (nz+1); 3769ae82921SPaul Mullowney 377e057df02SPaul Mullowney /* first, set the diagonal elements */ 3789ae82921SPaul Mullowney AjUp[offset] = (PetscInt) i; 37909f51544SAlejandro Lamas Daviña AAUp[offset] = (MatScalar)1./v[nz]; 3809ae82921SPaul Mullowney AiUp[i] = AiUp[i+1] - (nz+1); 3819ae82921SPaul Mullowney 382580bdb30SBarry Smith ierr = PetscArraycpy(&(AjUp[offset+1]), vi, nz);CHKERRQ(ierr); 383580bdb30SBarry Smith ierr = PetscArraycpy(&(AAUp[offset+1]), v, nz);CHKERRQ(ierr); 3849ae82921SPaul Mullowney } 3852205254eSKarl Rupp 386aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 387aa372e3fSPaul Mullowney upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 3882205254eSKarl Rupp 389aa372e3fSPaul Mullowney /* Create the matrix description */ 390c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUDA(stat); 391c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 392c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat); 393c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat); 394c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUDA(stat); 395aa372e3fSPaul Mullowney 396aa372e3fSPaul Mullowney /* Create the solve analysis information */ 397c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUDA(stat); 398aa372e3fSPaul Mullowney 399aa372e3fSPaul Mullowney /* set the operation */ 400aa372e3fSPaul Mullowney upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 401aa372e3fSPaul Mullowney 402aa372e3fSPaul Mullowney /* set the matrix */ 403aa372e3fSPaul Mullowney upTriFactor->csrMat = new CsrMatrix; 404aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows = n; 405aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols = n; 406aa372e3fSPaul Mullowney upTriFactor->csrMat->num_entries = nzUpper; 407aa372e3fSPaul Mullowney 408aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1); 409aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+n+1); 410aa372e3fSPaul Mullowney 411aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzUpper); 412aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+nzUpper); 413aa372e3fSPaul Mullowney 414aa372e3fSPaul Mullowney upTriFactor->csrMat->values = new THRUSTARRAY(nzUpper); 415aa372e3fSPaul Mullowney upTriFactor->csrMat->values->assign(AAUp, AAUp+nzUpper); 416aa372e3fSPaul Mullowney 417aa372e3fSPaul Mullowney /* perform the solve analysis */ 418aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp, 419aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr, 420aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(), 421c41cb2e2SAlejandro Lamas Daviña upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUDA(stat); 422aa372e3fSPaul Mullowney 423aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 424aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor; 4252205254eSKarl Rupp 426c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AiUp);CHKERRCUDA(ierr); 427c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AjUp);CHKERRCUDA(ierr); 428c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AAUp);CHKERRCUDA(ierr); 4294863603aSSatish Balay ierr = PetscLogCpuToGpu((n+1+nzUpper)*sizeof(int)+nzUpper*sizeof(PetscScalar));CHKERRQ(ierr); 4309ae82921SPaul Mullowney } catch(char *ex) { 4319ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 4329ae82921SPaul Mullowney } 4339ae82921SPaul Mullowney } 4349ae82921SPaul Mullowney PetscFunctionReturn(0); 4359ae82921SPaul Mullowney } 4369ae82921SPaul Mullowney 437087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(Mat A) 4389ae82921SPaul Mullowney { 4399ae82921SPaul Mullowney PetscErrorCode ierr; 4409ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 4419ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 4429ae82921SPaul Mullowney IS isrow = a->row,iscol = a->icol; 4439ae82921SPaul Mullowney PetscBool row_identity,col_identity; 4449ae82921SPaul Mullowney const PetscInt *r,*c; 4459ae82921SPaul Mullowney PetscInt n = A->rmap->n; 4469ae82921SPaul Mullowney 4479ae82921SPaul Mullowney PetscFunctionBegin; 448087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildILULowerTriMatrix(A);CHKERRQ(ierr); 449087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(A);CHKERRQ(ierr); 4502205254eSKarl Rupp 451e65717acSKarl Rupp cusparseTriFactors->workVector = new THRUSTARRAY(n); 452aa372e3fSPaul Mullowney cusparseTriFactors->nnz=a->nz; 4539ae82921SPaul Mullowney 454b8ced49eSKarl Rupp A->valid_GPU_matrix = PETSC_OFFLOAD_BOTH; 455e057df02SPaul Mullowney /*lower triangular indices */ 4569ae82921SPaul Mullowney ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 4579ae82921SPaul Mullowney ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 4582205254eSKarl Rupp if (!row_identity) { 459aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n); 460aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices->assign(r, r+n); 4612205254eSKarl Rupp } 4629ae82921SPaul Mullowney ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 4639ae82921SPaul Mullowney 464e057df02SPaul Mullowney /*upper triangular indices */ 4659ae82921SPaul Mullowney ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); 4669ae82921SPaul Mullowney ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 4672205254eSKarl Rupp if (!col_identity) { 468aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n); 469aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices->assign(c, c+n); 4702205254eSKarl Rupp } 4714863603aSSatish Balay 4724863603aSSatish Balay if(!row_identity && !col_identity) { 4734863603aSSatish Balay ierr = PetscLogCpuToGpu(2*n*sizeof(PetscInt));CHKERRQ(ierr); 4744863603aSSatish Balay } else if(!row_identity) { 4754863603aSSatish Balay ierr = PetscLogCpuToGpu(n*sizeof(PetscInt));CHKERRQ(ierr); 4764863603aSSatish Balay } else if(!col_identity) { 4774863603aSSatish Balay ierr = PetscLogCpuToGpu(n*sizeof(PetscInt));CHKERRQ(ierr); 4784863603aSSatish Balay } 4794863603aSSatish Balay 4809ae82921SPaul Mullowney ierr = ISRestoreIndices(iscol,&c);CHKERRQ(ierr); 4819ae82921SPaul Mullowney PetscFunctionReturn(0); 4829ae82921SPaul Mullowney } 4839ae82921SPaul Mullowney 484087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildICCTriMatrices(Mat A) 485087f3262SPaul Mullowney { 486087f3262SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 487087f3262SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 488aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 489aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 490087f3262SPaul Mullowney cusparseStatus_t stat; 491087f3262SPaul Mullowney PetscErrorCode ierr; 492087f3262SPaul Mullowney PetscInt *AiUp, *AjUp; 493087f3262SPaul Mullowney PetscScalar *AAUp; 494087f3262SPaul Mullowney PetscScalar *AALo; 495087f3262SPaul Mullowney PetscInt nzUpper = a->nz,n = A->rmap->n,i,offset,nz,j; 496087f3262SPaul Mullowney Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)A->data; 497087f3262SPaul Mullowney const PetscInt *ai = b->i,*aj = b->j,*vj; 498087f3262SPaul Mullowney const MatScalar *aa = b->a,*v; 499087f3262SPaul Mullowney 500087f3262SPaul Mullowney PetscFunctionBegin; 501cf00fe3bSKarl Rupp if (!n) PetscFunctionReturn(0); 502b8ced49eSKarl Rupp if (A->valid_GPU_matrix == PETSC_OFFLOAD_UNALLOCATED || A->valid_GPU_matrix == PETSC_OFFLOAD_CPU) { 503087f3262SPaul Mullowney try { 504087f3262SPaul Mullowney /* Allocate Space for the upper triangular matrix */ 505c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr); 506c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(ierr); 507c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr); 508c41cb2e2SAlejandro Lamas Daviña ierr = cudaMallocHost((void**) &AALo, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr); 509087f3262SPaul Mullowney 510087f3262SPaul Mullowney /* Fill the upper triangular matrix */ 511087f3262SPaul Mullowney AiUp[0]=(PetscInt) 0; 512087f3262SPaul Mullowney AiUp[n]=nzUpper; 513087f3262SPaul Mullowney offset = 0; 514087f3262SPaul Mullowney for (i=0; i<n; i++) { 515087f3262SPaul Mullowney /* set the pointers */ 516087f3262SPaul Mullowney v = aa + ai[i]; 517087f3262SPaul Mullowney vj = aj + ai[i]; 518087f3262SPaul Mullowney nz = ai[i+1] - ai[i] - 1; /* exclude diag[i] */ 519087f3262SPaul Mullowney 520087f3262SPaul Mullowney /* first, set the diagonal elements */ 521087f3262SPaul Mullowney AjUp[offset] = (PetscInt) i; 52209f51544SAlejandro Lamas Daviña AAUp[offset] = (MatScalar)1.0/v[nz]; 523087f3262SPaul Mullowney AiUp[i] = offset; 52409f51544SAlejandro Lamas Daviña AALo[offset] = (MatScalar)1.0/v[nz]; 525087f3262SPaul Mullowney 526087f3262SPaul Mullowney offset+=1; 527087f3262SPaul Mullowney if (nz>0) { 528f22e0265SBarry Smith ierr = PetscArraycpy(&(AjUp[offset]), vj, nz);CHKERRQ(ierr); 529580bdb30SBarry Smith ierr = PetscArraycpy(&(AAUp[offset]), v, nz);CHKERRQ(ierr); 530087f3262SPaul Mullowney for (j=offset; j<offset+nz; j++) { 531087f3262SPaul Mullowney AAUp[j] = -AAUp[j]; 532087f3262SPaul Mullowney AALo[j] = AAUp[j]/v[nz]; 533087f3262SPaul Mullowney } 534087f3262SPaul Mullowney offset+=nz; 535087f3262SPaul Mullowney } 536087f3262SPaul Mullowney } 537087f3262SPaul Mullowney 538aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 539aa372e3fSPaul Mullowney upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 540087f3262SPaul Mullowney 541aa372e3fSPaul Mullowney /* Create the matrix description */ 542c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUDA(stat); 543c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 544c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat); 545c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat); 546c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUDA(stat); 547087f3262SPaul Mullowney 548aa372e3fSPaul Mullowney /* Create the solve analysis information */ 549c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUDA(stat); 550aa372e3fSPaul Mullowney 551aa372e3fSPaul Mullowney /* set the operation */ 552aa372e3fSPaul Mullowney upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 553aa372e3fSPaul Mullowney 554aa372e3fSPaul Mullowney /* set the matrix */ 555aa372e3fSPaul Mullowney upTriFactor->csrMat = new CsrMatrix; 556aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows = A->rmap->n; 557aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols = A->cmap->n; 558aa372e3fSPaul Mullowney upTriFactor->csrMat->num_entries = a->nz; 559aa372e3fSPaul Mullowney 560aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 561aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1); 562aa372e3fSPaul Mullowney 563aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz); 564aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz); 565aa372e3fSPaul Mullowney 566aa372e3fSPaul Mullowney upTriFactor->csrMat->values = new THRUSTARRAY(a->nz); 567aa372e3fSPaul Mullowney upTriFactor->csrMat->values->assign(AAUp, AAUp+a->nz); 568aa372e3fSPaul Mullowney 569aa372e3fSPaul Mullowney /* perform the solve analysis */ 570aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp, 571aa372e3fSPaul Mullowney upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr, 572aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(), 573c41cb2e2SAlejandro Lamas Daviña upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUDA(stat); 574aa372e3fSPaul Mullowney 575aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 576aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor; 577aa372e3fSPaul Mullowney 578aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 579aa372e3fSPaul Mullowney loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct; 580aa372e3fSPaul Mullowney 581aa372e3fSPaul Mullowney /* Create the matrix description */ 582c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUDA(stat); 583c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 584c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat); 585c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat); 586c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUDA(stat); 587aa372e3fSPaul Mullowney 588aa372e3fSPaul Mullowney /* Create the solve analysis information */ 589c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUDA(stat); 590aa372e3fSPaul Mullowney 591aa372e3fSPaul Mullowney /* set the operation */ 592aa372e3fSPaul Mullowney loTriFactor->solveOp = CUSPARSE_OPERATION_TRANSPOSE; 593aa372e3fSPaul Mullowney 594aa372e3fSPaul Mullowney /* set the matrix */ 595aa372e3fSPaul Mullowney loTriFactor->csrMat = new CsrMatrix; 596aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows = A->rmap->n; 597aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols = A->cmap->n; 598aa372e3fSPaul Mullowney loTriFactor->csrMat->num_entries = a->nz; 599aa372e3fSPaul Mullowney 600aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 601aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1); 602aa372e3fSPaul Mullowney 603aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz); 604aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz); 605aa372e3fSPaul Mullowney 606aa372e3fSPaul Mullowney loTriFactor->csrMat->values = new THRUSTARRAY(a->nz); 607aa372e3fSPaul Mullowney loTriFactor->csrMat->values->assign(AALo, AALo+a->nz); 6084863603aSSatish Balay ierr = PetscLogCpuToGpu(2*(((A->rmap->n+1)+(a->nz))*sizeof(int)+(a->nz)*sizeof(PetscScalar)));CHKERRQ(ierr); 609aa372e3fSPaul Mullowney 610aa372e3fSPaul Mullowney /* perform the solve analysis */ 611aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp, 612aa372e3fSPaul Mullowney loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr, 613aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(), 614c41cb2e2SAlejandro Lamas Daviña loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUDA(stat); 615aa372e3fSPaul Mullowney 616aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 617aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor; 618087f3262SPaul Mullowney 619b8ced49eSKarl Rupp A->valid_GPU_matrix = PETSC_OFFLOAD_BOTH; 620c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AiUp);CHKERRCUDA(ierr); 621c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AjUp);CHKERRCUDA(ierr); 622c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AAUp);CHKERRCUDA(ierr); 623c41cb2e2SAlejandro Lamas Daviña ierr = cudaFreeHost(AALo);CHKERRCUDA(ierr); 624087f3262SPaul Mullowney } catch(char *ex) { 625087f3262SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 626087f3262SPaul Mullowney } 627087f3262SPaul Mullowney } 628087f3262SPaul Mullowney PetscFunctionReturn(0); 629087f3262SPaul Mullowney } 630087f3262SPaul Mullowney 631087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(Mat A) 6329ae82921SPaul Mullowney { 6339ae82921SPaul Mullowney PetscErrorCode ierr; 634087f3262SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 635087f3262SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 636087f3262SPaul Mullowney IS ip = a->row; 637087f3262SPaul Mullowney const PetscInt *rip; 638087f3262SPaul Mullowney PetscBool perm_identity; 639087f3262SPaul Mullowney PetscInt n = A->rmap->n; 640087f3262SPaul Mullowney 641087f3262SPaul Mullowney PetscFunctionBegin; 642087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEBuildICCTriMatrices(A);CHKERRQ(ierr); 643e65717acSKarl Rupp cusparseTriFactors->workVector = new THRUSTARRAY(n); 644aa372e3fSPaul Mullowney cusparseTriFactors->nnz=(a->nz-n)*2 + n; 645aa372e3fSPaul Mullowney 646087f3262SPaul Mullowney /*lower triangular indices */ 647087f3262SPaul Mullowney ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); 648087f3262SPaul Mullowney ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 649087f3262SPaul Mullowney if (!perm_identity) { 650aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n); 651aa372e3fSPaul Mullowney cusparseTriFactors->rpermIndices->assign(rip, rip+n); 652aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n); 653aa372e3fSPaul Mullowney cusparseTriFactors->cpermIndices->assign(rip, rip+n); 6544863603aSSatish Balay ierr = PetscLogCpuToGpu(2*n*sizeof(PetscInt));CHKERRQ(ierr); 655087f3262SPaul Mullowney } 656087f3262SPaul Mullowney ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); 657087f3262SPaul Mullowney PetscFunctionReturn(0); 658087f3262SPaul Mullowney } 659087f3262SPaul Mullowney 6606fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info) 6619ae82921SPaul Mullowney { 6629ae82921SPaul Mullowney Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 6639ae82921SPaul Mullowney IS isrow = b->row,iscol = b->col; 6649ae82921SPaul Mullowney PetscBool row_identity,col_identity; 665b175d8bbSPaul Mullowney PetscErrorCode ierr; 6669ae82921SPaul Mullowney 6679ae82921SPaul Mullowney PetscFunctionBegin; 6689ae82921SPaul Mullowney ierr = MatLUFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr); 669e057df02SPaul Mullowney /* determine which version of MatSolve needs to be used. */ 6709ae82921SPaul Mullowney ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 6719ae82921SPaul Mullowney ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 672bda325fcSPaul Mullowney if (row_identity && col_identity) { 673bda325fcSPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering; 674bda325fcSPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering; 675bda325fcSPaul Mullowney } else { 676bda325fcSPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE; 677bda325fcSPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE; 678bda325fcSPaul Mullowney } 6798dc1d2a3SPaul Mullowney 680e057df02SPaul Mullowney /* get the triangular factors */ 681087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(B);CHKERRQ(ierr); 6829ae82921SPaul Mullowney PetscFunctionReturn(0); 6839ae82921SPaul Mullowney } 6849ae82921SPaul Mullowney 685087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info) 686087f3262SPaul Mullowney { 687087f3262SPaul Mullowney Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 688087f3262SPaul Mullowney IS ip = b->row; 689087f3262SPaul Mullowney PetscBool perm_identity; 690b175d8bbSPaul Mullowney PetscErrorCode ierr; 691087f3262SPaul Mullowney 692087f3262SPaul Mullowney PetscFunctionBegin; 693087f3262SPaul Mullowney ierr = MatCholeskyFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr); 694087f3262SPaul Mullowney 695087f3262SPaul Mullowney /* determine which version of MatSolve needs to be used. */ 696087f3262SPaul Mullowney ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 697087f3262SPaul Mullowney if (perm_identity) { 698087f3262SPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering; 699087f3262SPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering; 700087f3262SPaul Mullowney } else { 701087f3262SPaul Mullowney B->ops->solve = MatSolve_SeqAIJCUSPARSE; 702087f3262SPaul Mullowney B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE; 703087f3262SPaul Mullowney } 704087f3262SPaul Mullowney 705087f3262SPaul Mullowney /* get the triangular factors */ 706087f3262SPaul Mullowney ierr = MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(B);CHKERRQ(ierr); 707087f3262SPaul Mullowney PetscFunctionReturn(0); 708087f3262SPaul Mullowney } 7099ae82921SPaul Mullowney 710b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(Mat A) 711bda325fcSPaul Mullowney { 712bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 713aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 714aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 715aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 716aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 717bda325fcSPaul Mullowney cusparseStatus_t stat; 718aa372e3fSPaul Mullowney cusparseIndexBase_t indexBase; 719aa372e3fSPaul Mullowney cusparseMatrixType_t matrixType; 720aa372e3fSPaul Mullowney cusparseFillMode_t fillMode; 721aa372e3fSPaul Mullowney cusparseDiagType_t diagType; 722b175d8bbSPaul Mullowney 723bda325fcSPaul Mullowney PetscFunctionBegin; 724bda325fcSPaul Mullowney 725aa372e3fSPaul Mullowney /*********************************************/ 726aa372e3fSPaul Mullowney /* Now the Transpose of the Lower Tri Factor */ 727aa372e3fSPaul Mullowney /*********************************************/ 728aa372e3fSPaul Mullowney 729aa372e3fSPaul Mullowney /* allocate space for the transpose of the lower triangular factor */ 730aa372e3fSPaul Mullowney loTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct; 731aa372e3fSPaul Mullowney 732aa372e3fSPaul Mullowney /* set the matrix descriptors of the lower triangular factor */ 733aa372e3fSPaul Mullowney matrixType = cusparseGetMatType(loTriFactor->descr); 734aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(loTriFactor->descr); 735aa372e3fSPaul Mullowney fillMode = cusparseGetMatFillMode(loTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ? 736aa372e3fSPaul Mullowney CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER; 737aa372e3fSPaul Mullowney diagType = cusparseGetMatDiagType(loTriFactor->descr); 738aa372e3fSPaul Mullowney 739aa372e3fSPaul Mullowney /* Create the matrix description */ 740c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&loTriFactorT->descr);CHKERRCUDA(stat); 741c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(loTriFactorT->descr, indexBase);CHKERRCUDA(stat); 742c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(loTriFactorT->descr, matrixType);CHKERRCUDA(stat); 743c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(loTriFactorT->descr, fillMode);CHKERRCUDA(stat); 744c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(loTriFactorT->descr, diagType);CHKERRCUDA(stat); 745aa372e3fSPaul Mullowney 746aa372e3fSPaul Mullowney /* Create the solve analysis information */ 747c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&loTriFactorT->solveInfo);CHKERRCUDA(stat); 748aa372e3fSPaul Mullowney 749aa372e3fSPaul Mullowney /* set the operation */ 750aa372e3fSPaul Mullowney loTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 751aa372e3fSPaul Mullowney 752aa372e3fSPaul Mullowney /* allocate GPU space for the CSC of the lower triangular factor*/ 753aa372e3fSPaul Mullowney loTriFactorT->csrMat = new CsrMatrix; 754aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_rows = loTriFactor->csrMat->num_rows; 755aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_cols = loTriFactor->csrMat->num_cols; 756aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_entries = loTriFactor->csrMat->num_entries; 757aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(loTriFactor->csrMat->num_rows+1); 758aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(loTriFactor->csrMat->num_entries); 759aa372e3fSPaul Mullowney loTriFactorT->csrMat->values = new THRUSTARRAY(loTriFactor->csrMat->num_entries); 760aa372e3fSPaul Mullowney 761aa372e3fSPaul Mullowney /* compute the transpose of the lower triangular factor, i.e. the CSC */ 762aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparseTriFactors->handle, loTriFactor->csrMat->num_rows, 763aa372e3fSPaul Mullowney loTriFactor->csrMat->num_cols, loTriFactor->csrMat->num_entries, 764aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 765aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 766aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 767aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 768aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 769aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 770c41cb2e2SAlejandro Lamas Daviña CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat); 771aa372e3fSPaul Mullowney 772aa372e3fSPaul Mullowney /* perform the solve analysis on the transposed matrix */ 773aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactorT->solveOp, 774aa372e3fSPaul Mullowney loTriFactorT->csrMat->num_rows, loTriFactorT->csrMat->num_entries, 775aa372e3fSPaul Mullowney loTriFactorT->descr, loTriFactorT->csrMat->values->data().get(), 776aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), loTriFactorT->csrMat->column_indices->data().get(), 777c41cb2e2SAlejandro Lamas Daviña loTriFactorT->solveInfo);CHKERRCUDA(stat); 778aa372e3fSPaul Mullowney 779aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 780aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtrTranspose = loTriFactorT; 781aa372e3fSPaul Mullowney 782aa372e3fSPaul Mullowney /*********************************************/ 783aa372e3fSPaul Mullowney /* Now the Transpose of the Upper Tri Factor */ 784aa372e3fSPaul Mullowney /*********************************************/ 785aa372e3fSPaul Mullowney 786aa372e3fSPaul Mullowney /* allocate space for the transpose of the upper triangular factor */ 787aa372e3fSPaul Mullowney upTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct; 788aa372e3fSPaul Mullowney 789aa372e3fSPaul Mullowney /* set the matrix descriptors of the upper triangular factor */ 790aa372e3fSPaul Mullowney matrixType = cusparseGetMatType(upTriFactor->descr); 791aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(upTriFactor->descr); 792aa372e3fSPaul Mullowney fillMode = cusparseGetMatFillMode(upTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ? 793aa372e3fSPaul Mullowney CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER; 794aa372e3fSPaul Mullowney diagType = cusparseGetMatDiagType(upTriFactor->descr); 795aa372e3fSPaul Mullowney 796aa372e3fSPaul Mullowney /* Create the matrix description */ 797c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&upTriFactorT->descr);CHKERRCUDA(stat); 798c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(upTriFactorT->descr, indexBase);CHKERRCUDA(stat); 799c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(upTriFactorT->descr, matrixType);CHKERRCUDA(stat); 800c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatFillMode(upTriFactorT->descr, fillMode);CHKERRCUDA(stat); 801c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatDiagType(upTriFactorT->descr, diagType);CHKERRCUDA(stat); 802aa372e3fSPaul Mullowney 803aa372e3fSPaul Mullowney /* Create the solve analysis information */ 804c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateSolveAnalysisInfo(&upTriFactorT->solveInfo);CHKERRCUDA(stat); 805aa372e3fSPaul Mullowney 806aa372e3fSPaul Mullowney /* set the operation */ 807aa372e3fSPaul Mullowney upTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE; 808aa372e3fSPaul Mullowney 809aa372e3fSPaul Mullowney /* allocate GPU space for the CSC of the upper triangular factor*/ 810aa372e3fSPaul Mullowney upTriFactorT->csrMat = new CsrMatrix; 811aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_rows = upTriFactor->csrMat->num_rows; 812aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_cols = upTriFactor->csrMat->num_cols; 813aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_entries = upTriFactor->csrMat->num_entries; 814aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(upTriFactor->csrMat->num_rows+1); 815aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(upTriFactor->csrMat->num_entries); 816aa372e3fSPaul Mullowney upTriFactorT->csrMat->values = new THRUSTARRAY(upTriFactor->csrMat->num_entries); 817aa372e3fSPaul Mullowney 818aa372e3fSPaul Mullowney /* compute the transpose of the upper triangular factor, i.e. the CSC */ 819aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparseTriFactors->handle, upTriFactor->csrMat->num_rows, 820aa372e3fSPaul Mullowney upTriFactor->csrMat->num_cols, upTriFactor->csrMat->num_entries, 821aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 822aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 823aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 824aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 825aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 826aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 827c41cb2e2SAlejandro Lamas Daviña CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat); 828aa372e3fSPaul Mullowney 829aa372e3fSPaul Mullowney /* perform the solve analysis on the transposed matrix */ 830aa372e3fSPaul Mullowney stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactorT->solveOp, 831aa372e3fSPaul Mullowney upTriFactorT->csrMat->num_rows, upTriFactorT->csrMat->num_entries, 832aa372e3fSPaul Mullowney upTriFactorT->descr, upTriFactorT->csrMat->values->data().get(), 833aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), upTriFactorT->csrMat->column_indices->data().get(), 834c41cb2e2SAlejandro Lamas Daviña upTriFactorT->solveInfo);CHKERRCUDA(stat); 835aa372e3fSPaul Mullowney 836aa372e3fSPaul Mullowney /* assign the pointer. Is this really necessary? */ 837aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtrTranspose = upTriFactorT; 838bda325fcSPaul Mullowney PetscFunctionReturn(0); 839bda325fcSPaul Mullowney } 840bda325fcSPaul Mullowney 841b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEGenerateTransposeForMult(Mat A) 842bda325fcSPaul Mullowney { 843aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 844aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 845aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 846bda325fcSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 847bda325fcSPaul Mullowney cusparseStatus_t stat; 848aa372e3fSPaul Mullowney cusparseIndexBase_t indexBase; 849b06137fdSPaul Mullowney cudaError_t err; 8504863603aSSatish Balay PetscErrorCode ierr; 851b175d8bbSPaul Mullowney 852bda325fcSPaul Mullowney PetscFunctionBegin; 853aa372e3fSPaul Mullowney 854aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 855aa372e3fSPaul Mullowney matstructT = new Mat_SeqAIJCUSPARSEMultStruct; 856c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&matstructT->descr);CHKERRCUDA(stat); 857aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(matstruct->descr); 858c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(matstructT->descr, indexBase);CHKERRCUDA(stat); 859c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(matstructT->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUDA(stat); 860aa372e3fSPaul Mullowney 861b06137fdSPaul Mullowney /* set alpha and beta */ 862c41cb2e2SAlejandro Lamas Daviña err = cudaMalloc((void **)&(matstructT->alpha), sizeof(PetscScalar));CHKERRCUDA(err); 8637656d835SStefano Zampini err = cudaMalloc((void **)&(matstructT->beta_zero),sizeof(PetscScalar));CHKERRCUDA(err); 8647656d835SStefano Zampini err = cudaMalloc((void **)&(matstructT->beta_one), sizeof(PetscScalar));CHKERRCUDA(err); 8657656d835SStefano Zampini err = cudaMemcpy(matstructT->alpha, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 8667656d835SStefano Zampini err = cudaMemcpy(matstructT->beta_zero,&PETSC_CUSPARSE_ZERO,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 8677656d835SStefano Zampini err = cudaMemcpy(matstructT->beta_one, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 868c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat); 869b06137fdSPaul Mullowney 870aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 871aa372e3fSPaul Mullowney CsrMatrix *matrix = (CsrMatrix*)matstruct->mat; 872aa372e3fSPaul Mullowney CsrMatrix *matrixT= new CsrMatrix; 873*a3fdcf43SKarl Rupp thrust::device_vector<int> *rowoffsets_gpu; 874*a3fdcf43SKarl Rupp const int ncomprow = matstruct->cprowIndices->size(); 875*a3fdcf43SKarl Rupp thrust::host_vector<int> cprow(ncomprow); 876*a3fdcf43SKarl Rupp cprow = *matstruct->cprowIndices; // GPU --> CPU 877554b8892SKarl Rupp matrixT->num_rows = A->cmap->n; 878554b8892SKarl Rupp matrixT->num_cols = A->rmap->n; 879aa372e3fSPaul Mullowney matrixT->num_entries = a->nz; 880a8bd5306SMark Adams matrixT->row_offsets = new THRUSTINTARRAY32(matrixT->num_rows+1); 881aa372e3fSPaul Mullowney matrixT->column_indices = new THRUSTINTARRAY32(a->nz); 882aa372e3fSPaul Mullowney matrixT->values = new THRUSTARRAY(a->nz); 883*a3fdcf43SKarl Rupp PetscInt k,i; 884*a3fdcf43SKarl Rupp thrust::host_vector<int> rowst_host(A->rmap->n+1); 885*a3fdcf43SKarl Rupp 886*a3fdcf43SKarl Rupp /* expand compress rows, which is forced in constructor (MatSeqAIJCUSPARSECopyToGPU) */ 887*a3fdcf43SKarl Rupp rowst_host[0] = 0; 888*a3fdcf43SKarl Rupp for (k = 0, i = 0; i < A->rmap->n ; i++) { 889*a3fdcf43SKarl Rupp if (k < ncomprow && i==cprow[k]) { 890*a3fdcf43SKarl Rupp rowst_host[i+1] = a->i[i+1]; 891*a3fdcf43SKarl Rupp k++; 892*a3fdcf43SKarl Rupp } else rowst_host[i+1] = rowst_host[i]; 893*a3fdcf43SKarl Rupp } 894*a3fdcf43SKarl Rupp if (k!=ncomprow) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"%D != %D",k,ncomprow); 895*a3fdcf43SKarl Rupp rowoffsets_gpu = new THRUSTINTARRAY32(A->rmap->n+1); 896*a3fdcf43SKarl Rupp *rowoffsets_gpu = rowst_host; // CPU --> GPU 897aa372e3fSPaul Mullowney 898aa372e3fSPaul Mullowney /* compute the transpose of the upper triangular factor, i.e. the CSC */ 899aa372e3fSPaul Mullowney indexBase = cusparseGetMatIndexBase(matstruct->descr); 900*a3fdcf43SKarl Rupp stat = cusparse_csr2csc(cusparsestruct->handle, A->rmap->n, 901*a3fdcf43SKarl Rupp A->cmap->n, matrix->num_entries, 902aa372e3fSPaul Mullowney matrix->values->data().get(), 903*a3fdcf43SKarl Rupp rowoffsets_gpu->data().get(), 904aa372e3fSPaul Mullowney matrix->column_indices->data().get(), 905aa372e3fSPaul Mullowney matrixT->values->data().get(), 906aa372e3fSPaul Mullowney matrixT->column_indices->data().get(), 907aa372e3fSPaul Mullowney matrixT->row_offsets->data().get(), 908c41cb2e2SAlejandro Lamas Daviña CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat); 909aa372e3fSPaul Mullowney 910aa372e3fSPaul Mullowney /* assign the pointer */ 911aa372e3fSPaul Mullowney matstructT->mat = matrixT; 9124863603aSSatish Balay ierr = PetscLogCpuToGpu(((A->rmap->n+1)+(a->nz))*sizeof(int)+(3+a->nz)*sizeof(PetscScalar));CHKERRQ(ierr); 913aa372e3fSPaul Mullowney } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) { 914aa372e3fSPaul Mullowney /* First convert HYB to CSR */ 915aa372e3fSPaul Mullowney CsrMatrix *temp= new CsrMatrix; 916aa372e3fSPaul Mullowney temp->num_rows = A->rmap->n; 917aa372e3fSPaul Mullowney temp->num_cols = A->cmap->n; 918aa372e3fSPaul Mullowney temp->num_entries = a->nz; 919aa372e3fSPaul Mullowney temp->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 920aa372e3fSPaul Mullowney temp->column_indices = new THRUSTINTARRAY32(a->nz); 921aa372e3fSPaul Mullowney temp->values = new THRUSTARRAY(a->nz); 922aa372e3fSPaul Mullowney 9232692e278SPaul Mullowney 924aa372e3fSPaul Mullowney stat = cusparse_hyb2csr(cusparsestruct->handle, 925aa372e3fSPaul Mullowney matstruct->descr, (cusparseHybMat_t)matstruct->mat, 926aa372e3fSPaul Mullowney temp->values->data().get(), 927aa372e3fSPaul Mullowney temp->row_offsets->data().get(), 928c41cb2e2SAlejandro Lamas Daviña temp->column_indices->data().get());CHKERRCUDA(stat); 929aa372e3fSPaul Mullowney 930aa372e3fSPaul Mullowney /* Next, convert CSR to CSC (i.e. the matrix transpose) */ 931aa372e3fSPaul Mullowney CsrMatrix *tempT= new CsrMatrix; 932aa372e3fSPaul Mullowney tempT->num_rows = A->rmap->n; 933aa372e3fSPaul Mullowney tempT->num_cols = A->cmap->n; 934aa372e3fSPaul Mullowney tempT->num_entries = a->nz; 935aa372e3fSPaul Mullowney tempT->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1); 936aa372e3fSPaul Mullowney tempT->column_indices = new THRUSTINTARRAY32(a->nz); 937aa372e3fSPaul Mullowney tempT->values = new THRUSTARRAY(a->nz); 938aa372e3fSPaul Mullowney 939aa372e3fSPaul Mullowney stat = cusparse_csr2csc(cusparsestruct->handle, temp->num_rows, 940aa372e3fSPaul Mullowney temp->num_cols, temp->num_entries, 941aa372e3fSPaul Mullowney temp->values->data().get(), 942aa372e3fSPaul Mullowney temp->row_offsets->data().get(), 943aa372e3fSPaul Mullowney temp->column_indices->data().get(), 944aa372e3fSPaul Mullowney tempT->values->data().get(), 945aa372e3fSPaul Mullowney tempT->column_indices->data().get(), 946aa372e3fSPaul Mullowney tempT->row_offsets->data().get(), 947c41cb2e2SAlejandro Lamas Daviña CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat); 948aa372e3fSPaul Mullowney 949aa372e3fSPaul Mullowney /* Last, convert CSC to HYB */ 950aa372e3fSPaul Mullowney cusparseHybMat_t hybMat; 951c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateHybMat(&hybMat);CHKERRCUDA(stat); 952aa372e3fSPaul Mullowney cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ? 953aa372e3fSPaul Mullowney CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO; 954aa372e3fSPaul Mullowney stat = cusparse_csr2hyb(cusparsestruct->handle, A->rmap->n, A->cmap->n, 955aa372e3fSPaul Mullowney matstructT->descr, tempT->values->data().get(), 956aa372e3fSPaul Mullowney tempT->row_offsets->data().get(), 957aa372e3fSPaul Mullowney tempT->column_indices->data().get(), 958c41cb2e2SAlejandro Lamas Daviña hybMat, 0, partition);CHKERRCUDA(stat); 959aa372e3fSPaul Mullowney 960aa372e3fSPaul Mullowney /* assign the pointer */ 961aa372e3fSPaul Mullowney matstructT->mat = hybMat; 9624863603aSSatish Balay ierr = PetscLogCpuToGpu((2*(((A->rmap->n+1)+(a->nz))*sizeof(int)+(a->nz)*sizeof(PetscScalar)))+3*sizeof(PetscScalar));CHKERRQ(ierr); 963aa372e3fSPaul Mullowney 964aa372e3fSPaul Mullowney /* delete temporaries */ 965aa372e3fSPaul Mullowney if (tempT) { 966aa372e3fSPaul Mullowney if (tempT->values) delete (THRUSTARRAY*) tempT->values; 967aa372e3fSPaul Mullowney if (tempT->column_indices) delete (THRUSTINTARRAY32*) tempT->column_indices; 968aa372e3fSPaul Mullowney if (tempT->row_offsets) delete (THRUSTINTARRAY32*) tempT->row_offsets; 969aa372e3fSPaul Mullowney delete (CsrMatrix*) tempT; 970087f3262SPaul Mullowney } 971aa372e3fSPaul Mullowney if (temp) { 972aa372e3fSPaul Mullowney if (temp->values) delete (THRUSTARRAY*) temp->values; 973aa372e3fSPaul Mullowney if (temp->column_indices) delete (THRUSTINTARRAY32*) temp->column_indices; 974aa372e3fSPaul Mullowney if (temp->row_offsets) delete (THRUSTINTARRAY32*) temp->row_offsets; 975aa372e3fSPaul Mullowney delete (CsrMatrix*) temp; 976aa372e3fSPaul Mullowney } 977aa372e3fSPaul Mullowney } 978aa372e3fSPaul Mullowney /* assign the compressed row indices */ 979aa372e3fSPaul Mullowney matstructT->cprowIndices = new THRUSTINTARRAY; 980554b8892SKarl Rupp matstructT->cprowIndices->resize(A->cmap->n); 981554b8892SKarl Rupp thrust::sequence(matstructT->cprowIndices->begin(), matstructT->cprowIndices->end()); 982aa372e3fSPaul Mullowney /* assign the pointer */ 983aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)A->spptr)->matTranspose = matstructT; 984bda325fcSPaul Mullowney PetscFunctionReturn(0); 985bda325fcSPaul Mullowney } 986bda325fcSPaul Mullowney 9876fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx) 988bda325fcSPaul Mullowney { 989c41cb2e2SAlejandro Lamas Daviña PetscInt n = xx->map->n; 990465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 991465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 992465f34aeSAlejandro Lamas Daviña thrust::device_ptr<const PetscScalar> bGPU; 993465f34aeSAlejandro Lamas Daviña thrust::device_ptr<PetscScalar> xGPU; 994bda325fcSPaul Mullowney cusparseStatus_t stat; 995bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 996aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 997aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 998aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 999b175d8bbSPaul Mullowney PetscErrorCode ierr; 1000bda325fcSPaul Mullowney 1001bda325fcSPaul Mullowney PetscFunctionBegin; 1002aa372e3fSPaul Mullowney /* Analyze the matrix and create the transpose ... on the fly */ 1003aa372e3fSPaul Mullowney if (!loTriFactorT && !upTriFactorT) { 1004bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr); 1005aa372e3fSPaul Mullowney loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1006aa372e3fSPaul Mullowney upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1007bda325fcSPaul Mullowney } 1008bda325fcSPaul Mullowney 1009bda325fcSPaul Mullowney /* Get the GPU pointers */ 1010c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1011c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1012c41cb2e2SAlejandro Lamas Daviña xGPU = thrust::device_pointer_cast(xarray); 1013c41cb2e2SAlejandro Lamas Daviña bGPU = thrust::device_pointer_cast(barray); 1014bda325fcSPaul Mullowney 10157a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1016aa372e3fSPaul Mullowney /* First, reorder with the row permutation */ 1017c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()), 1018c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(bGPU+n, cusparseTriFactors->rpermIndices->end()), 1019c41cb2e2SAlejandro Lamas Daviña xGPU); 1020aa372e3fSPaul Mullowney 1021aa372e3fSPaul Mullowney /* First, solve U */ 1022aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp, 10237656d835SStefano Zampini upTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactorT->descr, 1024aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 1025aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 1026aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 1027aa372e3fSPaul Mullowney upTriFactorT->solveInfo, 1028c41cb2e2SAlejandro Lamas Daviña xarray, tempGPU->data().get());CHKERRCUDA(stat); 1029aa372e3fSPaul Mullowney 1030aa372e3fSPaul Mullowney /* Then, solve L */ 1031aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp, 10327656d835SStefano Zampini loTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactorT->descr, 1033aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 1034aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 1035aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 1036aa372e3fSPaul Mullowney loTriFactorT->solveInfo, 1037c41cb2e2SAlejandro Lamas Daviña tempGPU->data().get(), xarray);CHKERRCUDA(stat); 1038aa372e3fSPaul Mullowney 1039aa372e3fSPaul Mullowney /* Last, copy the solution, xGPU, into a temporary with the column permutation ... can't be done in place. */ 1040c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->begin()), 1041c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(xGPU+n, cusparseTriFactors->cpermIndices->end()), 1042aa372e3fSPaul Mullowney tempGPU->begin()); 1043aa372e3fSPaul Mullowney 1044aa372e3fSPaul Mullowney /* Copy the temporary to the full solution. */ 1045c41cb2e2SAlejandro Lamas Daviña thrust::copy(tempGPU->begin(), tempGPU->end(), xGPU); 1046bda325fcSPaul Mullowney 1047bda325fcSPaul Mullowney /* restore */ 1048c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1049c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 1050c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1051661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1052958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 1053bda325fcSPaul Mullowney PetscFunctionReturn(0); 1054bda325fcSPaul Mullowney } 1055bda325fcSPaul Mullowney 10566fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx) 1057bda325fcSPaul Mullowney { 1058465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1059465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 1060bda325fcSPaul Mullowney cusparseStatus_t stat; 1061bda325fcSPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1062aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1063aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1064aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1065b175d8bbSPaul Mullowney PetscErrorCode ierr; 1066bda325fcSPaul Mullowney 1067bda325fcSPaul Mullowney PetscFunctionBegin; 1068aa372e3fSPaul Mullowney /* Analyze the matrix and create the transpose ... on the fly */ 1069aa372e3fSPaul Mullowney if (!loTriFactorT && !upTriFactorT) { 1070bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr); 1071aa372e3fSPaul Mullowney loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose; 1072aa372e3fSPaul Mullowney upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose; 1073bda325fcSPaul Mullowney } 1074bda325fcSPaul Mullowney 1075bda325fcSPaul Mullowney /* Get the GPU pointers */ 1076c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1077c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1078bda325fcSPaul Mullowney 10797a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1080aa372e3fSPaul Mullowney /* First, solve U */ 1081aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp, 10827656d835SStefano Zampini upTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactorT->descr, 1083aa372e3fSPaul Mullowney upTriFactorT->csrMat->values->data().get(), 1084aa372e3fSPaul Mullowney upTriFactorT->csrMat->row_offsets->data().get(), 1085aa372e3fSPaul Mullowney upTriFactorT->csrMat->column_indices->data().get(), 1086aa372e3fSPaul Mullowney upTriFactorT->solveInfo, 1087c41cb2e2SAlejandro Lamas Daviña barray, tempGPU->data().get());CHKERRCUDA(stat); 1088aa372e3fSPaul Mullowney 1089aa372e3fSPaul Mullowney /* Then, solve L */ 1090aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp, 10917656d835SStefano Zampini loTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactorT->descr, 1092aa372e3fSPaul Mullowney loTriFactorT->csrMat->values->data().get(), 1093aa372e3fSPaul Mullowney loTriFactorT->csrMat->row_offsets->data().get(), 1094aa372e3fSPaul Mullowney loTriFactorT->csrMat->column_indices->data().get(), 1095aa372e3fSPaul Mullowney loTriFactorT->solveInfo, 1096c41cb2e2SAlejandro Lamas Daviña tempGPU->data().get(), xarray);CHKERRCUDA(stat); 1097bda325fcSPaul Mullowney 1098bda325fcSPaul Mullowney /* restore */ 1099c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1100c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 1101c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1102661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1103958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 1104bda325fcSPaul Mullowney PetscFunctionReturn(0); 1105bda325fcSPaul Mullowney } 1106bda325fcSPaul Mullowney 11076fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx) 11089ae82921SPaul Mullowney { 1109465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1110465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 1111465f34aeSAlejandro Lamas Daviña thrust::device_ptr<const PetscScalar> bGPU; 1112465f34aeSAlejandro Lamas Daviña thrust::device_ptr<PetscScalar> xGPU; 11139ae82921SPaul Mullowney cusparseStatus_t stat; 11149ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1115aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 1116aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 1117aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1118b175d8bbSPaul Mullowney PetscErrorCode ierr; 11199ae82921SPaul Mullowney 11209ae82921SPaul Mullowney PetscFunctionBegin; 1121ebc8f436SDominic Meiser 1122e057df02SPaul Mullowney /* Get the GPU pointers */ 1123c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1124c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 1125c41cb2e2SAlejandro Lamas Daviña xGPU = thrust::device_pointer_cast(xarray); 1126c41cb2e2SAlejandro Lamas Daviña bGPU = thrust::device_pointer_cast(barray); 11279ae82921SPaul Mullowney 11287a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1129aa372e3fSPaul Mullowney /* First, reorder with the row permutation */ 1130c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()), 1131c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->end()), 1132c41cb2e2SAlejandro Lamas Daviña xGPU); 1133aa372e3fSPaul Mullowney 1134aa372e3fSPaul Mullowney /* Next, solve L */ 1135aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp, 11367656d835SStefano Zampini loTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactor->descr, 1137aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 1138aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 1139aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 1140aa372e3fSPaul Mullowney loTriFactor->solveInfo, 1141c41cb2e2SAlejandro Lamas Daviña xarray, tempGPU->data().get());CHKERRCUDA(stat); 1142aa372e3fSPaul Mullowney 1143aa372e3fSPaul Mullowney /* Then, solve U */ 1144aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp, 11457656d835SStefano Zampini upTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactor->descr, 1146aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 1147aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 1148aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 1149aa372e3fSPaul Mullowney upTriFactor->solveInfo, 1150c41cb2e2SAlejandro Lamas Daviña tempGPU->data().get(), xarray);CHKERRCUDA(stat); 1151aa372e3fSPaul Mullowney 1152aa372e3fSPaul Mullowney /* Last, copy the solution, xGPU, into a temporary with the column permutation ... can't be done in place. */ 1153c41cb2e2SAlejandro Lamas Daviña thrust::copy(thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->begin()), 1154c41cb2e2SAlejandro Lamas Daviña thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->end()), 1155aa372e3fSPaul Mullowney tempGPU->begin()); 1156aa372e3fSPaul Mullowney 1157aa372e3fSPaul Mullowney /* Copy the temporary to the full solution. */ 1158c41cb2e2SAlejandro Lamas Daviña thrust::copy(tempGPU->begin(), tempGPU->end(), xGPU); 11599ae82921SPaul Mullowney 1160c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1161c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 1162c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1163661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1164958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 11659ae82921SPaul Mullowney PetscFunctionReturn(0); 11669ae82921SPaul Mullowney } 11679ae82921SPaul Mullowney 11686fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx) 11699ae82921SPaul Mullowney { 1170465f34aeSAlejandro Lamas Daviña const PetscScalar *barray; 1171465f34aeSAlejandro Lamas Daviña PetscScalar *xarray; 11729ae82921SPaul Mullowney cusparseStatus_t stat; 11739ae82921SPaul Mullowney Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr; 1174aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr; 1175aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr; 1176aa372e3fSPaul Mullowney THRUSTARRAY *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector; 1177b175d8bbSPaul Mullowney PetscErrorCode ierr; 11789ae82921SPaul Mullowney 11799ae82921SPaul Mullowney PetscFunctionBegin; 1180e057df02SPaul Mullowney /* Get the GPU pointers */ 1181c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr); 1182c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr); 11839ae82921SPaul Mullowney 11847a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1185aa372e3fSPaul Mullowney /* First, solve L */ 1186aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp, 11877656d835SStefano Zampini loTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactor->descr, 1188aa372e3fSPaul Mullowney loTriFactor->csrMat->values->data().get(), 1189aa372e3fSPaul Mullowney loTriFactor->csrMat->row_offsets->data().get(), 1190aa372e3fSPaul Mullowney loTriFactor->csrMat->column_indices->data().get(), 1191aa372e3fSPaul Mullowney loTriFactor->solveInfo, 1192c41cb2e2SAlejandro Lamas Daviña barray, tempGPU->data().get());CHKERRCUDA(stat); 1193aa372e3fSPaul Mullowney 1194aa372e3fSPaul Mullowney /* Next, solve U */ 1195aa372e3fSPaul Mullowney stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp, 11967656d835SStefano Zampini upTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactor->descr, 1197aa372e3fSPaul Mullowney upTriFactor->csrMat->values->data().get(), 1198aa372e3fSPaul Mullowney upTriFactor->csrMat->row_offsets->data().get(), 1199aa372e3fSPaul Mullowney upTriFactor->csrMat->column_indices->data().get(), 1200aa372e3fSPaul Mullowney upTriFactor->solveInfo, 1201c41cb2e2SAlejandro Lamas Daviña tempGPU->data().get(), xarray);CHKERRCUDA(stat); 12029ae82921SPaul Mullowney 1203c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr); 1204c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr); 1205c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1206661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1207958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr); 12089ae82921SPaul Mullowney PetscFunctionReturn(0); 12099ae82921SPaul Mullowney } 12109ae82921SPaul Mullowney 12116fa9248bSJed Brown static PetscErrorCode MatSeqAIJCUSPARSECopyToGPU(Mat A) 12129ae82921SPaul Mullowney { 1213aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 1214aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 12159ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 12169ae82921SPaul Mullowney PetscInt m = A->rmap->n,*ii,*ridx; 12179ae82921SPaul Mullowney PetscErrorCode ierr; 1218aa372e3fSPaul Mullowney cusparseStatus_t stat; 1219b06137fdSPaul Mullowney cudaError_t err; 12209ae82921SPaul Mullowney 12219ae82921SPaul Mullowney PetscFunctionBegin; 1222fdc842d1SBarry Smith if (A->pinnedtocpu) PetscFunctionReturn(0); 1223b8ced49eSKarl Rupp if (A->valid_GPU_matrix == PETSC_OFFLOAD_UNALLOCATED || A->valid_GPU_matrix == PETSC_OFFLOAD_CPU) { 12249ae82921SPaul Mullowney ierr = PetscLogEventBegin(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr); 122534d6c7a5SJose E. Roman if (A->assembled && A->nonzerostate == cusparsestruct->nonzerostate && cusparsestruct->format == MAT_CUSPARSE_CSR) { 122634d6c7a5SJose E. Roman CsrMatrix *matrix = (CsrMatrix*)matstruct->mat; 122734d6c7a5SJose E. Roman /* copy values only */ 122834d6c7a5SJose E. Roman matrix->values->assign(a->a, a->a+a->nz); 12294863603aSSatish Balay ierr = PetscLogCpuToGpu((a->nz)*sizeof(PetscScalar));CHKERRQ(ierr); 123034d6c7a5SJose E. Roman } else { 1231470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&matstruct,cusparsestruct->format); 12329ae82921SPaul Mullowney try { 1233aa372e3fSPaul Mullowney cusparsestruct->nonzerorow=0; 1234aa372e3fSPaul Mullowney for (int j = 0; j<m; j++) cusparsestruct->nonzerorow += ((a->i[j+1]-a->i[j])>0); 12359ae82921SPaul Mullowney 12369ae82921SPaul Mullowney if (a->compressedrow.use) { 12379ae82921SPaul Mullowney m = a->compressedrow.nrows; 12389ae82921SPaul Mullowney ii = a->compressedrow.i; 12399ae82921SPaul Mullowney ridx = a->compressedrow.rindex; 12409ae82921SPaul Mullowney } else { 1241b06137fdSPaul Mullowney /* Forcing compressed row on the GPU */ 12429ae82921SPaul Mullowney int k=0; 1243854ce69bSBarry Smith ierr = PetscMalloc1(cusparsestruct->nonzerorow+1, &ii);CHKERRQ(ierr); 1244854ce69bSBarry Smith ierr = PetscMalloc1(cusparsestruct->nonzerorow, &ridx);CHKERRQ(ierr); 12459ae82921SPaul Mullowney ii[0]=0; 12469ae82921SPaul Mullowney for (int j = 0; j<m; j++) { 12479ae82921SPaul Mullowney if ((a->i[j+1]-a->i[j])>0) { 12489ae82921SPaul Mullowney ii[k] = a->i[j]; 12499ae82921SPaul Mullowney ridx[k]= j; 12509ae82921SPaul Mullowney k++; 12519ae82921SPaul Mullowney } 12529ae82921SPaul Mullowney } 1253aa372e3fSPaul Mullowney ii[cusparsestruct->nonzerorow] = a->nz; 1254aa372e3fSPaul Mullowney m = cusparsestruct->nonzerorow; 12559ae82921SPaul Mullowney } 12569ae82921SPaul Mullowney 1257aa372e3fSPaul Mullowney /* allocate space for the triangular factor information */ 1258aa372e3fSPaul Mullowney matstruct = new Mat_SeqAIJCUSPARSEMultStruct; 1259c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateMatDescr(&matstruct->descr);CHKERRCUDA(stat); 1260c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatIndexBase(matstruct->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat); 1261c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetMatType(matstruct->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUDA(stat); 12629ae82921SPaul Mullowney 1263c41cb2e2SAlejandro Lamas Daviña err = cudaMalloc((void **)&(matstruct->alpha), sizeof(PetscScalar));CHKERRCUDA(err); 12647656d835SStefano Zampini err = cudaMalloc((void **)&(matstruct->beta_zero),sizeof(PetscScalar));CHKERRCUDA(err); 12657656d835SStefano Zampini err = cudaMalloc((void **)&(matstruct->beta_one), sizeof(PetscScalar));CHKERRCUDA(err); 12667656d835SStefano Zampini err = cudaMemcpy(matstruct->alpha, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 12677656d835SStefano Zampini err = cudaMemcpy(matstruct->beta_zero,&PETSC_CUSPARSE_ZERO,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 12687656d835SStefano Zampini err = cudaMemcpy(matstruct->beta_one, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err); 1269c41cb2e2SAlejandro Lamas Daviña stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat); 1270b06137fdSPaul Mullowney 1271aa372e3fSPaul Mullowney /* Build a hybrid/ellpack matrix if this option is chosen for the storage */ 1272aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1273aa372e3fSPaul Mullowney /* set the matrix */ 1274aa372e3fSPaul Mullowney CsrMatrix *matrix= new CsrMatrix; 1275a65300a6SPaul Mullowney matrix->num_rows = m; 1276aa372e3fSPaul Mullowney matrix->num_cols = A->cmap->n; 1277aa372e3fSPaul Mullowney matrix->num_entries = a->nz; 1278a65300a6SPaul Mullowney matrix->row_offsets = new THRUSTINTARRAY32(m+1); 1279a65300a6SPaul Mullowney matrix->row_offsets->assign(ii, ii + m+1); 12809ae82921SPaul Mullowney 1281aa372e3fSPaul Mullowney matrix->column_indices = new THRUSTINTARRAY32(a->nz); 1282aa372e3fSPaul Mullowney matrix->column_indices->assign(a->j, a->j+a->nz); 1283aa372e3fSPaul Mullowney 1284aa372e3fSPaul Mullowney matrix->values = new THRUSTARRAY(a->nz); 1285aa372e3fSPaul Mullowney matrix->values->assign(a->a, a->a+a->nz); 1286aa372e3fSPaul Mullowney 1287aa372e3fSPaul Mullowney /* assign the pointer */ 1288aa372e3fSPaul Mullowney matstruct->mat = matrix; 1289aa372e3fSPaul Mullowney 1290aa372e3fSPaul Mullowney } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) { 1291aa372e3fSPaul Mullowney CsrMatrix *matrix= new CsrMatrix; 1292a65300a6SPaul Mullowney matrix->num_rows = m; 1293aa372e3fSPaul Mullowney matrix->num_cols = A->cmap->n; 1294aa372e3fSPaul Mullowney matrix->num_entries = a->nz; 1295a65300a6SPaul Mullowney matrix->row_offsets = new THRUSTINTARRAY32(m+1); 1296a65300a6SPaul Mullowney matrix->row_offsets->assign(ii, ii + m+1); 1297aa372e3fSPaul Mullowney 1298aa372e3fSPaul Mullowney matrix->column_indices = new THRUSTINTARRAY32(a->nz); 1299aa372e3fSPaul Mullowney matrix->column_indices->assign(a->j, a->j+a->nz); 1300aa372e3fSPaul Mullowney 1301aa372e3fSPaul Mullowney matrix->values = new THRUSTARRAY(a->nz); 1302aa372e3fSPaul Mullowney matrix->values->assign(a->a, a->a+a->nz); 1303aa372e3fSPaul Mullowney 1304aa372e3fSPaul Mullowney cusparseHybMat_t hybMat; 1305c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreateHybMat(&hybMat);CHKERRCUDA(stat); 1306aa372e3fSPaul Mullowney cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ? 1307aa372e3fSPaul Mullowney CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO; 1308a65300a6SPaul Mullowney stat = cusparse_csr2hyb(cusparsestruct->handle, matrix->num_rows, matrix->num_cols, 1309aa372e3fSPaul Mullowney matstruct->descr, matrix->values->data().get(), 1310aa372e3fSPaul Mullowney matrix->row_offsets->data().get(), 1311aa372e3fSPaul Mullowney matrix->column_indices->data().get(), 1312c41cb2e2SAlejandro Lamas Daviña hybMat, 0, partition);CHKERRCUDA(stat); 1313aa372e3fSPaul Mullowney /* assign the pointer */ 1314aa372e3fSPaul Mullowney matstruct->mat = hybMat; 1315aa372e3fSPaul Mullowney 1316aa372e3fSPaul Mullowney if (matrix) { 1317aa372e3fSPaul Mullowney if (matrix->values) delete (THRUSTARRAY*)matrix->values; 1318aa372e3fSPaul Mullowney if (matrix->column_indices) delete (THRUSTINTARRAY32*)matrix->column_indices; 1319aa372e3fSPaul Mullowney if (matrix->row_offsets) delete (THRUSTINTARRAY32*)matrix->row_offsets; 1320aa372e3fSPaul Mullowney delete (CsrMatrix*)matrix; 1321087f3262SPaul Mullowney } 1322087f3262SPaul Mullowney } 1323ca45077fSPaul Mullowney 1324aa372e3fSPaul Mullowney /* assign the compressed row indices */ 1325aa372e3fSPaul Mullowney matstruct->cprowIndices = new THRUSTINTARRAY(m); 1326aa372e3fSPaul Mullowney matstruct->cprowIndices->assign(ridx,ridx+m); 13274863603aSSatish Balay ierr = PetscLogCpuToGpu(((m+1)+(a->nz))*sizeof(int)+m*sizeof(PetscInt)+(3+(a->nz))*sizeof(PetscScalar));CHKERRQ(ierr); 1328aa372e3fSPaul Mullowney 1329aa372e3fSPaul Mullowney /* assign the pointer */ 1330aa372e3fSPaul Mullowney cusparsestruct->mat = matstruct; 1331aa372e3fSPaul Mullowney 13329ae82921SPaul Mullowney if (!a->compressedrow.use) { 13339ae82921SPaul Mullowney ierr = PetscFree(ii);CHKERRQ(ierr); 13349ae82921SPaul Mullowney ierr = PetscFree(ridx);CHKERRQ(ierr); 13359ae82921SPaul Mullowney } 1336e65717acSKarl Rupp cusparsestruct->workVector = new THRUSTARRAY(m); 13379ae82921SPaul Mullowney } catch(char *ex) { 13389ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 13399ae82921SPaul Mullowney } 134034d6c7a5SJose E. Roman cusparsestruct->nonzerostate = A->nonzerostate; 134134d6c7a5SJose E. Roman } 13427d0e52d8SJose E. Roman ierr = WaitForGPU();CHKERRCUDA(ierr); 1343b8ced49eSKarl Rupp A->valid_GPU_matrix = PETSC_OFFLOAD_BOTH; 13449ae82921SPaul Mullowney ierr = PetscLogEventEnd(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr); 13459ae82921SPaul Mullowney } 13469ae82921SPaul Mullowney PetscFunctionReturn(0); 13479ae82921SPaul Mullowney } 13489ae82921SPaul Mullowney 1349c41cb2e2SAlejandro Lamas Daviña struct VecCUDAPlusEquals 1350aa372e3fSPaul Mullowney { 1351aa372e3fSPaul Mullowney template <typename Tuple> 1352aa372e3fSPaul Mullowney __host__ __device__ 1353aa372e3fSPaul Mullowney void operator()(Tuple t) 1354aa372e3fSPaul Mullowney { 1355aa372e3fSPaul Mullowney thrust::get<1>(t) = thrust::get<1>(t) + thrust::get<0>(t); 1356aa372e3fSPaul Mullowney } 1357aa372e3fSPaul Mullowney }; 1358aa372e3fSPaul Mullowney 13596fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy) 13609ae82921SPaul Mullowney { 1361b175d8bbSPaul Mullowney PetscErrorCode ierr; 13629ae82921SPaul Mullowney 13639ae82921SPaul Mullowney PetscFunctionBegin; 13647656d835SStefano Zampini ierr = MatMultAdd_SeqAIJCUSPARSE(A,xx,NULL,yy);CHKERRQ(ierr); 13659ae82921SPaul Mullowney PetscFunctionReturn(0); 13669ae82921SPaul Mullowney } 13679ae82921SPaul Mullowney 13686fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy) 1369ca45077fSPaul Mullowney { 1370ca45077fSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1371aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 13729ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstructT; 1373465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 1374465f34aeSAlejandro Lamas Daviña PetscScalar *yarray; 1375b175d8bbSPaul Mullowney PetscErrorCode ierr; 1376aa372e3fSPaul Mullowney cusparseStatus_t stat; 1377ca45077fSPaul Mullowney 1378ca45077fSPaul Mullowney PetscFunctionBegin; 137934d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 138034d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 13819ff858a8SKarl Rupp matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1382aa372e3fSPaul Mullowney if (!matstructT) { 1383bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr); 1384aa372e3fSPaul Mullowney matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1385bda325fcSPaul Mullowney } 1386c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1387c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayWrite(yy,&yarray);CHKERRQ(ierr); 1388aa372e3fSPaul Mullowney 13897a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1390aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1391aa372e3fSPaul Mullowney CsrMatrix *mat = (CsrMatrix*)matstructT->mat; 1392aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1393aa372e3fSPaul Mullowney mat->num_rows, mat->num_cols, 1394b06137fdSPaul Mullowney mat->num_entries, matstructT->alpha, matstructT->descr, 1395aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 13967656d835SStefano Zampini mat->column_indices->data().get(), xarray, matstructT->beta_zero, 1397c41cb2e2SAlejandro Lamas Daviña yarray);CHKERRCUDA(stat); 1398aa372e3fSPaul Mullowney } else { 1399aa372e3fSPaul Mullowney cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat; 1400aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1401b06137fdSPaul Mullowney matstructT->alpha, matstructT->descr, hybMat, 14027656d835SStefano Zampini xarray, matstructT->beta_zero, 1403c41cb2e2SAlejandro Lamas Daviña yarray);CHKERRCUDA(stat); 1404ca45077fSPaul Mullowney } 1405c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1406c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayWrite(yy,&yarray);CHKERRQ(ierr); 1407c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 1408661c2d29Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1409958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*a->nz - cusparsestruct->nonzerorow);CHKERRQ(ierr); 1410ca45077fSPaul Mullowney PetscFunctionReturn(0); 1411ca45077fSPaul Mullowney } 1412ca45077fSPaul Mullowney 1413aa372e3fSPaul Mullowney 14146fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz) 14159ae82921SPaul Mullowney { 14169ae82921SPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1417aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 14189ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstruct; 1419465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 14207656d835SStefano Zampini PetscScalar *zarray,*dptr,*beta; 1421b175d8bbSPaul Mullowney PetscErrorCode ierr; 1422aa372e3fSPaul Mullowney cusparseStatus_t stat; 1423c1fb3f03SStefano Zampini PetscBool cmpr; /* if the matrix has been compressed (zero rows) */ 14246e111a19SKarl Rupp 14259ae82921SPaul Mullowney PetscFunctionBegin; 142634d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 142734d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 14289ff858a8SKarl Rupp matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat; 14299ae82921SPaul Mullowney try { 1430c1fb3f03SStefano Zampini cmpr = (PetscBool)(cusparsestruct->workVector->size() == (thrust::detail::vector_base<PetscScalar, thrust::device_malloc_allocator<PetscScalar> >::size_type)(A->rmap->n)); 1431c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1432c1fb3f03SStefano Zampini if (yy && !cmpr) { /* MatMultAdd with noncompressed storage -> need uptodate zz vector */ 1433f2d70e9dSBarry Smith ierr = VecCUDAGetArray(zz,&zarray);CHKERRQ(ierr); 1434c1fb3f03SStefano Zampini } else { 1435c1fb3f03SStefano Zampini ierr = VecCUDAGetArrayWrite(zz,&zarray);CHKERRQ(ierr); 1436c1fb3f03SStefano Zampini } 1437c1fb3f03SStefano Zampini dptr = cmpr ? zarray : cusparsestruct->workVector->data().get(); 14387656d835SStefano Zampini beta = (yy == zz && dptr == zarray) ? matstruct->beta_one : matstruct->beta_zero; 14399ae82921SPaul Mullowney 14407a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 14417656d835SStefano Zampini /* csr_spmv is multiply add */ 1442aa372e3fSPaul Mullowney if (cusparsestruct->format == MAT_CUSPARSE_CSR) { 1443b06137fdSPaul Mullowney /* here we need to be careful to set the number of rows in the multiply to the 1444b06137fdSPaul Mullowney number of compressed rows in the matrix ... which is equivalent to the 1445b06137fdSPaul Mullowney size of the workVector */ 14467656d835SStefano Zampini CsrMatrix *mat = (CsrMatrix*)matstruct->mat; 1447aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1448a65300a6SPaul Mullowney mat->num_rows, mat->num_cols, 1449b06137fdSPaul Mullowney mat->num_entries, matstruct->alpha, matstruct->descr, 1450aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 14517656d835SStefano Zampini mat->column_indices->data().get(), xarray, beta, 14527656d835SStefano Zampini dptr);CHKERRCUDA(stat); 1453aa372e3fSPaul Mullowney } else { 1454a65300a6SPaul Mullowney if (cusparsestruct->workVector->size()) { 1455301298b4SMark Adams cusparseHybMat_t hybMat = (cusparseHybMat_t)matstruct->mat; 1456aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1457b06137fdSPaul Mullowney matstruct->alpha, matstruct->descr, hybMat, 14587656d835SStefano Zampini xarray, beta, 14597656d835SStefano Zampini dptr);CHKERRCUDA(stat); 1460a65300a6SPaul Mullowney } 1461aa372e3fSPaul Mullowney } 1462958c4211Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1463aa372e3fSPaul Mullowney 14647656d835SStefano Zampini if (yy) { 14657656d835SStefano Zampini if (dptr != zarray) { 14667656d835SStefano Zampini ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); 14677656d835SStefano Zampini } else if (zz != yy) { 14687656d835SStefano Zampini ierr = VecAXPY_SeqCUDA(zz,1.0,yy);CHKERRQ(ierr); 14697656d835SStefano Zampini } 14707656d835SStefano Zampini } else if (dptr != zarray) { 1471c1fb3f03SStefano Zampini ierr = VecSet_SeqCUDA(zz,0);CHKERRQ(ierr); 14727656d835SStefano Zampini } 1473aa372e3fSPaul Mullowney /* scatter the data from the temporary into the full vector with a += operation */ 14747a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 14757656d835SStefano Zampini if (dptr != zarray) { 14767656d835SStefano Zampini thrust::device_ptr<PetscScalar> zptr; 14777656d835SStefano Zampini 14787656d835SStefano Zampini zptr = thrust::device_pointer_cast(zarray); 1479c41cb2e2SAlejandro Lamas Daviña thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))), 1480c41cb2e2SAlejandro Lamas Daviña thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))) + cusparsestruct->workVector->size(), 1481c41cb2e2SAlejandro Lamas Daviña VecCUDAPlusEquals()); 14827656d835SStefano Zampini } 1483958c4211Shannah_mairs ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1484c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1485c1fb3f03SStefano Zampini if (yy && !cmpr) { 1486f2d70e9dSBarry Smith ierr = VecCUDARestoreArray(zz,&zarray);CHKERRQ(ierr); 1487c1fb3f03SStefano Zampini } else { 1488c1fb3f03SStefano Zampini ierr = VecCUDARestoreArrayWrite(zz,&zarray);CHKERRQ(ierr); 1489c1fb3f03SStefano Zampini } 14909ae82921SPaul Mullowney } catch(char *ex) { 14919ae82921SPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 14929ae82921SPaul Mullowney } 14937656d835SStefano Zampini if (!yy) { /* MatMult */ 14947656d835SStefano Zampini if (!cusparsestruct->stream) { 1495c41cb2e2SAlejandro Lamas Daviña ierr = WaitForGPU();CHKERRCUDA(ierr); 14967656d835SStefano Zampini } 14977656d835SStefano Zampini } 1498958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr); 14999ae82921SPaul Mullowney PetscFunctionReturn(0); 15009ae82921SPaul Mullowney } 15019ae82921SPaul Mullowney 15026fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz) 1503ca45077fSPaul Mullowney { 1504ca45077fSPaul Mullowney Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1505aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr; 15069ff858a8SKarl Rupp Mat_SeqAIJCUSPARSEMultStruct *matstructT; 1507465f34aeSAlejandro Lamas Daviña const PetscScalar *xarray; 1508*a3fdcf43SKarl Rupp PetscScalar *zarray,*dptr,*beta; 1509b175d8bbSPaul Mullowney PetscErrorCode ierr; 1510aa372e3fSPaul Mullowney cusparseStatus_t stat; 15116e111a19SKarl Rupp 1512ca45077fSPaul Mullowney PetscFunctionBegin; 151334d6c7a5SJose E. Roman /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */ 151434d6c7a5SJose E. Roman ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 15159ff858a8SKarl Rupp matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1516aa372e3fSPaul Mullowney if (!matstructT) { 1517bda325fcSPaul Mullowney ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr); 1518aa372e3fSPaul Mullowney matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose; 1519bda325fcSPaul Mullowney } 1520aa372e3fSPaul Mullowney 1521ca45077fSPaul Mullowney try { 1522c41cb2e2SAlejandro Lamas Daviña ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); 1523c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr); 1524f2d70e9dSBarry Smith ierr = VecCUDAGetArray(zz,&zarray);CHKERRQ(ierr); 1525*a3fdcf43SKarl Rupp dptr = cusparsestruct->workVector->size() == (thrust::detail::vector_base<PetscScalar, thrust::device_malloc_allocator<PetscScalar> >::size_type)(A->cmap->n) ? zarray : cusparsestruct->workVector->data().get(); 1526*a3fdcf43SKarl Rupp beta = (yy == zz && dptr == zarray) ? matstructT->beta_one : matstructT->beta_zero; 1527ca45077fSPaul Mullowney 15287a052e47Shannah_mairs ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1529e057df02SPaul Mullowney /* multiply add with matrix transpose */ 1530aa372e3fSPaul Mullowney if (cusparsestruct->format==MAT_CUSPARSE_CSR) { 1531aa372e3fSPaul Mullowney CsrMatrix *mat = (CsrMatrix*)matstructT->mat; 1532b06137fdSPaul Mullowney /* here we need to be careful to set the number of rows in the multiply to the 1533b06137fdSPaul Mullowney number of compressed rows in the matrix ... which is equivalent to the 1534b06137fdSPaul Mullowney size of the workVector */ 1535aa372e3fSPaul Mullowney stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1536a65300a6SPaul Mullowney mat->num_rows, mat->num_cols, 1537b06137fdSPaul Mullowney mat->num_entries, matstructT->alpha, matstructT->descr, 1538aa372e3fSPaul Mullowney mat->values->data().get(), mat->row_offsets->data().get(), 1539*a3fdcf43SKarl Rupp mat->column_indices->data().get(), xarray, beta, 1540*a3fdcf43SKarl Rupp dptr);CHKERRCUDA(stat); 1541aa372e3fSPaul Mullowney } else { 1542aa372e3fSPaul Mullowney cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat; 1543a65300a6SPaul Mullowney if (cusparsestruct->workVector->size()) { 1544aa372e3fSPaul Mullowney stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE, 1545b06137fdSPaul Mullowney matstructT->alpha, matstructT->descr, hybMat, 1546*a3fdcf43SKarl Rupp xarray, beta, 1547*a3fdcf43SKarl Rupp dptr);CHKERRCUDA(stat); 1548a65300a6SPaul Mullowney } 1549aa372e3fSPaul Mullowney } 1550*a3fdcf43SKarl Rupp ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1551*a3fdcf43SKarl Rupp 1552*a3fdcf43SKarl Rupp if (dptr != zarray) { 1553*a3fdcf43SKarl Rupp ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr); 1554*a3fdcf43SKarl Rupp } else if (zz != yy) { 1555*a3fdcf43SKarl Rupp ierr = VecAXPY_SeqCUDA(zz,1.0,yy);CHKERRQ(ierr); 1556*a3fdcf43SKarl Rupp } 1557*a3fdcf43SKarl Rupp /* scatter the data from the temporary into the full vector with a += operation */ 1558*a3fdcf43SKarl Rupp ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 1559*a3fdcf43SKarl Rupp if (dptr != zarray) { 1560*a3fdcf43SKarl Rupp thrust::device_ptr<PetscScalar> zptr; 1561*a3fdcf43SKarl Rupp 1562*a3fdcf43SKarl Rupp zptr = thrust::device_pointer_cast(zarray); 1563aa372e3fSPaul Mullowney 1564aa372e3fSPaul Mullowney /* scatter the data from the temporary into the full vector with a += operation */ 1565c41cb2e2SAlejandro Lamas Daviña thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstructT->cprowIndices->begin()))), 1566554b8892SKarl Rupp thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstructT->cprowIndices->begin()))) + A->cmap->n, 1567c41cb2e2SAlejandro Lamas Daviña VecCUDAPlusEquals()); 1568*a3fdcf43SKarl Rupp } 1569*a3fdcf43SKarl Rupp ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 1570c41cb2e2SAlejandro Lamas Daviña ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr); 1571f2d70e9dSBarry Smith ierr = VecCUDARestoreArray(zz,&zarray);CHKERRQ(ierr); 1572ca45077fSPaul Mullowney } catch(char *ex) { 1573ca45077fSPaul Mullowney SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex); 1574661c2d29Shannah_mairs } 1575*a3fdcf43SKarl Rupp ierr = WaitForGPU();CHKERRCUDA(ierr); /* is this needed? just for yy==0 in Mult */ 1576958c4211Shannah_mairs ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr); 1577ca45077fSPaul Mullowney PetscFunctionReturn(0); 1578ca45077fSPaul Mullowney } 1579ca45077fSPaul Mullowney 15806fa9248bSJed Brown static PetscErrorCode MatAssemblyEnd_SeqAIJCUSPARSE(Mat A,MatAssemblyType mode) 15819ae82921SPaul Mullowney { 15829ae82921SPaul Mullowney PetscErrorCode ierr; 15836e111a19SKarl Rupp 15849ae82921SPaul Mullowney PetscFunctionBegin; 15859ae82921SPaul Mullowney ierr = MatAssemblyEnd_SeqAIJ(A,mode);CHKERRQ(ierr); 1586489de41dSStefano Zampini if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 1587bc3f50f2SPaul Mullowney if (A->factortype == MAT_FACTOR_NONE) { 1588e057df02SPaul Mullowney ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr); 1589bc3f50f2SPaul Mullowney } 1590bbf3fe20SPaul Mullowney A->ops->mult = MatMult_SeqAIJCUSPARSE; 1591bbf3fe20SPaul Mullowney A->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 1592bbf3fe20SPaul Mullowney A->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1593bbf3fe20SPaul Mullowney A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 15949ae82921SPaul Mullowney PetscFunctionReturn(0); 15959ae82921SPaul Mullowney } 15969ae82921SPaul Mullowney 15979ae82921SPaul Mullowney /* --------------------------------------------------------------------------------*/ 1598e057df02SPaul Mullowney /*@ 15999ae82921SPaul Mullowney MatCreateSeqAIJCUSPARSE - Creates a sparse matrix in AIJ (compressed row) format 1600e057df02SPaul Mullowney (the default parallel PETSc format). This matrix will ultimately pushed down 1601e057df02SPaul Mullowney to NVidia GPUs and use the CUSPARSE library for calculations. For good matrix 1602e057df02SPaul Mullowney assembly performance the user should preallocate the matrix storage by setting 1603e057df02SPaul Mullowney the parameter nz (or the array nnz). By setting these parameters accurately, 1604e057df02SPaul Mullowney performance during matrix assembly can be increased by more than a factor of 50. 16059ae82921SPaul Mullowney 1606d083f849SBarry Smith Collective 16079ae82921SPaul Mullowney 16089ae82921SPaul Mullowney Input Parameters: 16099ae82921SPaul Mullowney + comm - MPI communicator, set to PETSC_COMM_SELF 16109ae82921SPaul Mullowney . m - number of rows 16119ae82921SPaul Mullowney . n - number of columns 16129ae82921SPaul Mullowney . nz - number of nonzeros per row (same for all rows) 16139ae82921SPaul Mullowney - nnz - array containing the number of nonzeros in the various rows 16140298fd71SBarry Smith (possibly different for each row) or NULL 16159ae82921SPaul Mullowney 16169ae82921SPaul Mullowney Output Parameter: 16179ae82921SPaul Mullowney . A - the matrix 16189ae82921SPaul Mullowney 16199ae82921SPaul Mullowney It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 16209ae82921SPaul Mullowney MatXXXXSetPreallocation() paradgm instead of this routine directly. 16219ae82921SPaul Mullowney [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 16229ae82921SPaul Mullowney 16239ae82921SPaul Mullowney Notes: 16249ae82921SPaul Mullowney If nnz is given then nz is ignored 16259ae82921SPaul Mullowney 16269ae82921SPaul Mullowney The AIJ format (also called the Yale sparse matrix format or 16279ae82921SPaul Mullowney compressed row storage), is fully compatible with standard Fortran 77 16289ae82921SPaul Mullowney storage. That is, the stored row and column indices can begin at 16299ae82921SPaul Mullowney either one (as in Fortran) or zero. See the users' manual for details. 16309ae82921SPaul Mullowney 16319ae82921SPaul Mullowney Specify the preallocated storage with either nz or nnz (not both). 16320298fd71SBarry Smith Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory 16339ae82921SPaul Mullowney allocation. For large problems you MUST preallocate memory or you 16349ae82921SPaul Mullowney will get TERRIBLE performance, see the users' manual chapter on matrices. 16359ae82921SPaul Mullowney 16369ae82921SPaul Mullowney By default, this format uses inodes (identical nodes) when possible, to 16379ae82921SPaul Mullowney improve numerical efficiency of matrix-vector products and solves. We 16389ae82921SPaul Mullowney search for consecutive rows with the same nonzero structure, thereby 16399ae82921SPaul Mullowney reusing matrix information to achieve increased efficiency. 16409ae82921SPaul Mullowney 16419ae82921SPaul Mullowney Level: intermediate 16429ae82921SPaul Mullowney 1643e057df02SPaul Mullowney .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ(), MATSEQAIJCUSPARSE, MATAIJCUSPARSE 16449ae82921SPaul Mullowney @*/ 16459ae82921SPaul Mullowney PetscErrorCode MatCreateSeqAIJCUSPARSE(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 16469ae82921SPaul Mullowney { 16479ae82921SPaul Mullowney PetscErrorCode ierr; 16489ae82921SPaul Mullowney 16499ae82921SPaul Mullowney PetscFunctionBegin; 16509ae82921SPaul Mullowney ierr = MatCreate(comm,A);CHKERRQ(ierr); 16519ae82921SPaul Mullowney ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 16529ae82921SPaul Mullowney ierr = MatSetType(*A,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 16539ae82921SPaul Mullowney ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr); 16549ae82921SPaul Mullowney PetscFunctionReturn(0); 16559ae82921SPaul Mullowney } 16569ae82921SPaul Mullowney 16576fa9248bSJed Brown static PetscErrorCode MatDestroy_SeqAIJCUSPARSE(Mat A) 16589ae82921SPaul Mullowney { 16599ae82921SPaul Mullowney PetscErrorCode ierr; 1660ab25e6cbSDominic Meiser 16619ae82921SPaul Mullowney PetscFunctionBegin; 16629ae82921SPaul Mullowney if (A->factortype==MAT_FACTOR_NONE) { 1663b8ced49eSKarl Rupp if (A->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 1664470880abSPatrick Sanan ierr = MatSeqAIJCUSPARSE_Destroy((Mat_SeqAIJCUSPARSE**)&A->spptr);CHKERRQ(ierr); 16659ae82921SPaul Mullowney } 16669ae82921SPaul Mullowney } else { 1667470880abSPatrick Sanan ierr = MatSeqAIJCUSPARSETriFactors_Destroy((Mat_SeqAIJCUSPARSETriFactors**)&A->spptr);CHKERRQ(ierr); 1668aa372e3fSPaul Mullowney } 16699ae82921SPaul Mullowney ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr); 16709ae82921SPaul Mullowney PetscFunctionReturn(0); 16719ae82921SPaul Mullowney } 16729ae82921SPaul Mullowney 16739ff858a8SKarl Rupp static PetscErrorCode MatDuplicate_SeqAIJCUSPARSE(Mat A,MatDuplicateOption cpvalues,Mat *B) 16749ff858a8SKarl Rupp { 16759ff858a8SKarl Rupp PetscErrorCode ierr; 16769ff858a8SKarl Rupp Mat C; 16779ff858a8SKarl Rupp cusparseStatus_t stat; 16789ff858a8SKarl Rupp cusparseHandle_t handle=0; 16799ff858a8SKarl Rupp 16809ff858a8SKarl Rupp PetscFunctionBegin; 16819ff858a8SKarl Rupp ierr = MatDuplicate_SeqAIJ(A,cpvalues,B);CHKERRQ(ierr); 16829ff858a8SKarl Rupp C = *B; 168334136279SStefano Zampini ierr = PetscFree(C->defaultvectype);CHKERRQ(ierr); 168434136279SStefano Zampini ierr = PetscStrallocpy(VECCUDA,&C->defaultvectype);CHKERRQ(ierr); 168534136279SStefano Zampini 16869ff858a8SKarl Rupp /* inject CUSPARSE-specific stuff */ 16879ff858a8SKarl Rupp if (C->factortype==MAT_FACTOR_NONE) { 16889ff858a8SKarl Rupp /* you cannot check the inode.use flag here since the matrix was just created. 16899ff858a8SKarl Rupp now build a GPU matrix data structure */ 16909ff858a8SKarl Rupp C->spptr = new Mat_SeqAIJCUSPARSE; 16919ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->mat = 0; 16929ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->matTranspose = 0; 16939ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->workVector = 0; 16949ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->format = MAT_CUSPARSE_CSR; 16959ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->stream = 0; 16969ff858a8SKarl Rupp stat = cusparseCreate(&handle);CHKERRCUDA(stat); 16979ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->handle = handle; 16989ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)C->spptr)->nonzerostate = 0; 16999ff858a8SKarl Rupp } else { 17009ff858a8SKarl Rupp /* NEXT, set the pointers to the triangular factors */ 17019ff858a8SKarl Rupp C->spptr = new Mat_SeqAIJCUSPARSETriFactors; 17029ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->loTriFactorPtr = 0; 17039ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->upTriFactorPtr = 0; 17049ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->loTriFactorPtrTranspose = 0; 17059ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->upTriFactorPtrTranspose = 0; 17069ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->rpermIndices = 0; 17079ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->cpermIndices = 0; 17089ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->workVector = 0; 17099ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->handle = 0; 17109ff858a8SKarl Rupp stat = cusparseCreate(&handle);CHKERRCUDA(stat); 17119ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->handle = handle; 17129ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->nnz = 0; 17139ff858a8SKarl Rupp } 17149ff858a8SKarl Rupp 17159ff858a8SKarl Rupp C->ops->assemblyend = MatAssemblyEnd_SeqAIJCUSPARSE; 17169ff858a8SKarl Rupp C->ops->destroy = MatDestroy_SeqAIJCUSPARSE; 17179ff858a8SKarl Rupp C->ops->setfromoptions = MatSetFromOptions_SeqAIJCUSPARSE; 17189ff858a8SKarl Rupp C->ops->mult = MatMult_SeqAIJCUSPARSE; 17199ff858a8SKarl Rupp C->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 17209ff858a8SKarl Rupp C->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 17219ff858a8SKarl Rupp C->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 17229ff858a8SKarl Rupp C->ops->duplicate = MatDuplicate_SeqAIJCUSPARSE; 17239ff858a8SKarl Rupp 17249ff858a8SKarl Rupp ierr = PetscObjectChangeTypeName((PetscObject)C,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 17259ff858a8SKarl Rupp 1726b8ced49eSKarl Rupp C->valid_GPU_matrix = PETSC_OFFLOAD_UNALLOCATED; 17279ff858a8SKarl Rupp 17289ff858a8SKarl Rupp ierr = PetscObjectComposeFunction((PetscObject)C, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr); 17299ff858a8SKarl Rupp PetscFunctionReturn(0); 17309ff858a8SKarl Rupp } 17319ff858a8SKarl Rupp 173202fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat B) 17339ae82921SPaul Mullowney { 17349ae82921SPaul Mullowney PetscErrorCode ierr; 1735aa372e3fSPaul Mullowney cusparseStatus_t stat; 1736aa372e3fSPaul Mullowney cusparseHandle_t handle=0; 17379ae82921SPaul Mullowney 17389ae82921SPaul Mullowney PetscFunctionBegin; 173934136279SStefano Zampini ierr = PetscFree(B->defaultvectype);CHKERRQ(ierr); 174034136279SStefano Zampini ierr = PetscStrallocpy(VECCUDA,&B->defaultvectype);CHKERRQ(ierr); 174134136279SStefano Zampini 17429ae82921SPaul Mullowney if (B->factortype==MAT_FACTOR_NONE) { 1743e057df02SPaul Mullowney /* you cannot check the inode.use flag here since the matrix was just created. 1744e057df02SPaul Mullowney now build a GPU matrix data structure */ 17459ae82921SPaul Mullowney B->spptr = new Mat_SeqAIJCUSPARSE; 17469ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->mat = 0; 1747aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->matTranspose = 0; 1748aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->workVector = 0; 1749e057df02SPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->format = MAT_CUSPARSE_CSR; 1750aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->stream = 0; 1751c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreate(&handle);CHKERRCUDA(stat); 1752aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSE*)B->spptr)->handle = handle; 17539ff858a8SKarl Rupp ((Mat_SeqAIJCUSPARSE*)B->spptr)->nonzerostate = 0; 17549ae82921SPaul Mullowney } else { 17559ae82921SPaul Mullowney /* NEXT, set the pointers to the triangular factors */ 1756debe9ee2SPaul Mullowney B->spptr = new Mat_SeqAIJCUSPARSETriFactors; 17579ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtr = 0; 17589ae82921SPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtr = 0; 1759aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtrTranspose = 0; 1760aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtrTranspose = 0; 1761aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->rpermIndices = 0; 1762aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->cpermIndices = 0; 1763aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->workVector = 0; 1764c41cb2e2SAlejandro Lamas Daviña stat = cusparseCreate(&handle);CHKERRCUDA(stat); 1765aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->handle = handle; 1766aa372e3fSPaul Mullowney ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->nnz = 0; 17679ae82921SPaul Mullowney } 1768aa372e3fSPaul Mullowney 17699ae82921SPaul Mullowney B->ops->assemblyend = MatAssemblyEnd_SeqAIJCUSPARSE; 17709ae82921SPaul Mullowney B->ops->destroy = MatDestroy_SeqAIJCUSPARSE; 17719ae82921SPaul Mullowney B->ops->setfromoptions = MatSetFromOptions_SeqAIJCUSPARSE; 1772ca45077fSPaul Mullowney B->ops->mult = MatMult_SeqAIJCUSPARSE; 1773ca45077fSPaul Mullowney B->ops->multadd = MatMultAdd_SeqAIJCUSPARSE; 1774ca45077fSPaul Mullowney B->ops->multtranspose = MatMultTranspose_SeqAIJCUSPARSE; 1775ca45077fSPaul Mullowney B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE; 17769ff858a8SKarl Rupp B->ops->duplicate = MatDuplicate_SeqAIJCUSPARSE; 17772205254eSKarl Rupp 17789ae82921SPaul Mullowney ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJCUSPARSE);CHKERRQ(ierr); 17792205254eSKarl Rupp 1780b8ced49eSKarl Rupp B->valid_GPU_matrix = PETSC_OFFLOAD_UNALLOCATED; 17812205254eSKarl Rupp 1782bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr); 17839ae82921SPaul Mullowney PetscFunctionReturn(0); 17849ae82921SPaul Mullowney } 17859ae82921SPaul Mullowney 178602fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJCUSPARSE(Mat B) 178702fe1965SBarry Smith { 178802fe1965SBarry Smith PetscErrorCode ierr; 178902fe1965SBarry Smith 179002fe1965SBarry Smith PetscFunctionBegin; 179102fe1965SBarry Smith ierr = MatCreate_SeqAIJ(B);CHKERRQ(ierr); 1792489de41dSStefano Zampini ierr = MatConvert_SeqAIJ_SeqAIJCUSPARSE(B);CHKERRQ(ierr); 179302fe1965SBarry Smith PetscFunctionReturn(0); 179402fe1965SBarry Smith } 179502fe1965SBarry Smith 17963ca39a21SBarry Smith /*MC 1797e057df02SPaul Mullowney MATSEQAIJCUSPARSE - MATAIJCUSPARSE = "(seq)aijcusparse" - A matrix type to be used for sparse matrices. 1798e057df02SPaul Mullowney 1799e057df02SPaul Mullowney A matrix type type whose data resides on Nvidia GPUs. These matrices can be in either 18002692e278SPaul Mullowney CSR, ELL, or Hybrid format. The ELL and HYB formats require CUDA 4.2 or later. 18012692e278SPaul Mullowney All matrix calculations are performed on Nvidia GPUs using the CUSPARSE library. 1802e057df02SPaul Mullowney 1803e057df02SPaul Mullowney Options Database Keys: 1804e057df02SPaul Mullowney + -mat_type aijcusparse - sets the matrix type to "seqaijcusparse" during a call to MatSetFromOptions() 1805aa372e3fSPaul 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). 1806a2b725a8SWilliam Gropp - -mat_cusparse_mult_storage_format csr - sets the storage format of matrices (for MatMult) during a call to MatSetFromOptions(). Other options include ell (ellpack) or hyb (hybrid). 1807e057df02SPaul Mullowney 1808e057df02SPaul Mullowney Level: beginner 1809e057df02SPaul Mullowney 18108468deeeSKarl Rupp .seealso: MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation 1811e057df02SPaul Mullowney M*/ 18127f756511SDominic Meiser 181342c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat,MatFactorType,Mat*); 181442c9c57cSBarry Smith 18150f39cd5aSBarry Smith 18163ca39a21SBarry Smith PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_CUSPARSE(void) 181742c9c57cSBarry Smith { 181842c9c57cSBarry Smith PetscErrorCode ierr; 181942c9c57cSBarry Smith 182042c9c57cSBarry Smith PetscFunctionBegin; 18213ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_LU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 18223ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_CHOLESKY,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 18233ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ILU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 18243ca39a21SBarry Smith ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ICC,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr); 182542c9c57cSBarry Smith PetscFunctionReturn(0); 182642c9c57cSBarry Smith } 182729b38603SBarry Smith 182881e08676SBarry Smith 1829470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE **cusparsestruct) 18307f756511SDominic Meiser { 18317f756511SDominic Meiser cusparseStatus_t stat; 18327f756511SDominic Meiser cusparseHandle_t handle; 18337f756511SDominic Meiser 18347f756511SDominic Meiser PetscFunctionBegin; 18357f756511SDominic Meiser if (*cusparsestruct) { 1836470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->mat,(*cusparsestruct)->format); 1837470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->matTranspose,(*cusparsestruct)->format); 18387f756511SDominic Meiser delete (*cusparsestruct)->workVector; 18397f756511SDominic Meiser if (handle = (*cusparsestruct)->handle) { 1840c41cb2e2SAlejandro Lamas Daviña stat = cusparseDestroy(handle);CHKERRCUDA(stat); 18417f756511SDominic Meiser } 18427f756511SDominic Meiser delete *cusparsestruct; 18437f756511SDominic Meiser *cusparsestruct = 0; 18447f756511SDominic Meiser } 18457f756511SDominic Meiser PetscFunctionReturn(0); 18467f756511SDominic Meiser } 18477f756511SDominic Meiser 18487f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix **mat) 18497f756511SDominic Meiser { 18507f756511SDominic Meiser PetscFunctionBegin; 18517f756511SDominic Meiser if (*mat) { 18527f756511SDominic Meiser delete (*mat)->values; 18537f756511SDominic Meiser delete (*mat)->column_indices; 18547f756511SDominic Meiser delete (*mat)->row_offsets; 18557f756511SDominic Meiser delete *mat; 18567f756511SDominic Meiser *mat = 0; 18577f756511SDominic Meiser } 18587f756511SDominic Meiser PetscFunctionReturn(0); 18597f756511SDominic Meiser } 18607f756511SDominic Meiser 1861470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct **trifactor) 18627f756511SDominic Meiser { 18637f756511SDominic Meiser cusparseStatus_t stat; 18647f756511SDominic Meiser PetscErrorCode ierr; 18657f756511SDominic Meiser 18667f756511SDominic Meiser PetscFunctionBegin; 18677f756511SDominic Meiser if (*trifactor) { 1868c41cb2e2SAlejandro Lamas Daviña if ((*trifactor)->descr) { stat = cusparseDestroyMatDescr((*trifactor)->descr);CHKERRCUDA(stat); } 1869c41cb2e2SAlejandro Lamas Daviña if ((*trifactor)->solveInfo) { stat = cusparseDestroySolveAnalysisInfo((*trifactor)->solveInfo);CHKERRCUDA(stat); } 18707f756511SDominic Meiser ierr = CsrMatrix_Destroy(&(*trifactor)->csrMat);CHKERRQ(ierr); 18717f756511SDominic Meiser delete *trifactor; 18727f756511SDominic Meiser *trifactor = 0; 18737f756511SDominic Meiser } 18747f756511SDominic Meiser PetscFunctionReturn(0); 18757f756511SDominic Meiser } 18767f756511SDominic Meiser 1877470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct **matstruct,MatCUSPARSEStorageFormat format) 18787f756511SDominic Meiser { 18797f756511SDominic Meiser CsrMatrix *mat; 18807f756511SDominic Meiser cusparseStatus_t stat; 18817f756511SDominic Meiser cudaError_t err; 18827f756511SDominic Meiser 18837f756511SDominic Meiser PetscFunctionBegin; 18847f756511SDominic Meiser if (*matstruct) { 18857f756511SDominic Meiser if ((*matstruct)->mat) { 18867f756511SDominic Meiser if (format==MAT_CUSPARSE_ELL || format==MAT_CUSPARSE_HYB) { 18877f756511SDominic Meiser cusparseHybMat_t hybMat = (cusparseHybMat_t)(*matstruct)->mat; 1888c41cb2e2SAlejandro Lamas Daviña stat = cusparseDestroyHybMat(hybMat);CHKERRCUDA(stat); 18897f756511SDominic Meiser } else { 18907f756511SDominic Meiser mat = (CsrMatrix*)(*matstruct)->mat; 18917f756511SDominic Meiser CsrMatrix_Destroy(&mat); 18927f756511SDominic Meiser } 18937f756511SDominic Meiser } 1894c41cb2e2SAlejandro Lamas Daviña if ((*matstruct)->descr) { stat = cusparseDestroyMatDescr((*matstruct)->descr);CHKERRCUDA(stat); } 18957f756511SDominic Meiser delete (*matstruct)->cprowIndices; 1896c41cb2e2SAlejandro Lamas Daviña if ((*matstruct)->alpha) { err=cudaFree((*matstruct)->alpha);CHKERRCUDA(err); } 18977656d835SStefano Zampini if ((*matstruct)->beta_zero) { err=cudaFree((*matstruct)->beta_zero);CHKERRCUDA(err); } 18987656d835SStefano Zampini if ((*matstruct)->beta_one) { err=cudaFree((*matstruct)->beta_one);CHKERRCUDA(err); } 18997f756511SDominic Meiser delete *matstruct; 19007f756511SDominic Meiser *matstruct = 0; 19017f756511SDominic Meiser } 19027f756511SDominic Meiser PetscFunctionReturn(0); 19037f756511SDominic Meiser } 19047f756511SDominic Meiser 1905470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors** trifactors) 19067f756511SDominic Meiser { 19077f756511SDominic Meiser cusparseHandle_t handle; 19087f756511SDominic Meiser cusparseStatus_t stat; 19097f756511SDominic Meiser 19107f756511SDominic Meiser PetscFunctionBegin; 19117f756511SDominic Meiser if (*trifactors) { 1912470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->loTriFactorPtr); 1913470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->upTriFactorPtr); 1914470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->loTriFactorPtrTranspose); 1915470880abSPatrick Sanan MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->upTriFactorPtrTranspose); 19167f756511SDominic Meiser delete (*trifactors)->rpermIndices; 19177f756511SDominic Meiser delete (*trifactors)->cpermIndices; 19187f756511SDominic Meiser delete (*trifactors)->workVector; 19197f756511SDominic Meiser if (handle = (*trifactors)->handle) { 1920c41cb2e2SAlejandro Lamas Daviña stat = cusparseDestroy(handle);CHKERRCUDA(stat); 19217f756511SDominic Meiser } 19227f756511SDominic Meiser delete *trifactors; 19237f756511SDominic Meiser *trifactors = 0; 19247f756511SDominic Meiser } 19257f756511SDominic Meiser PetscFunctionReturn(0); 19267f756511SDominic Meiser } 19277f756511SDominic Meiser 1928