xref: /petsc/src/mat/impls/aij/seq/seqcusparse/aijcusparse.cu (revision c34f1ff0f3fb0a13532ed82549e966d87fe9d12d)
19ae82921SPaul Mullowney /*
29ae82921SPaul Mullowney   Defines the basic matrix operations for the AIJ (compressed row)
3bc3f50f2SPaul Mullowney   matrix storage format using the CUSPARSE library,
49ae82921SPaul Mullowney */
5dced61a5SBarry Smith #define PETSC_SKIP_SPINLOCK
653800007SKarl Rupp #define PETSC_SKIP_CXX_COMPLEX_FIX
799acd6aaSStefano Zampini #define PETSC_SKIP_IMMINTRIN_H_CUDAWORKAROUND 1
89ae82921SPaul Mullowney 
93d13b8fdSMatthew G. Knepley #include <petscconf.h>
103d13b8fdSMatthew G. Knepley #include <../src/mat/impls/aij/seq/aij.h>          /*I "petscmat.h" I*/
11087f3262SPaul Mullowney #include <../src/mat/impls/sbaij/seq/sbaij.h>
123d13b8fdSMatthew G. Knepley #include <../src/vec/vec/impls/dvecimpl.h>
13af0996ceSBarry Smith #include <petsc/private/vecimpl.h>
149ae82921SPaul Mullowney #undef VecType
153d13b8fdSMatthew G. Knepley #include <../src/mat/impls/aij/seq/seqcusparse/cusparsematimpl.h>
16bc3f50f2SPaul Mullowney 
17e057df02SPaul Mullowney const char *const MatCUSPARSEStorageFormats[] = {"CSR","ELL","HYB","MatCUSPARSEStorageFormat","MAT_CUSPARSE_",0};
189ae82921SPaul Mullowney 
19087f3262SPaul Mullowney static PetscErrorCode MatICCFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,const MatFactorInfo*);
20087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,const MatFactorInfo*);
21087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat,Mat,const MatFactorInfo*);
22087f3262SPaul Mullowney 
236fa9248bSJed Brown static PetscErrorCode MatILUFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,IS,const MatFactorInfo*);
246fa9248bSJed Brown static PetscErrorCode MatLUFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,IS,const MatFactorInfo*);
256fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat,Mat,const MatFactorInfo*);
26087f3262SPaul Mullowney 
276fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat,Vec,Vec);
286fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat,Vec,Vec);
296fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat,Vec,Vec);
306fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat,Vec,Vec);
314416b707SBarry Smith static PetscErrorCode MatSetFromOptions_SeqAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat);
326fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat,Vec,Vec);
336fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat,Vec,Vec,Vec);
346fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat,Vec,Vec);
356fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat,Vec,Vec,Vec);
369ae82921SPaul Mullowney 
377f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix**);
38470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct**);
39470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct**,MatCUSPARSEStorageFormat);
40470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors**);
41470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE**);
427f756511SDominic Meiser 
43b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSESetStream(Mat A,const cudaStream_t stream)
44b06137fdSPaul Mullowney {
45b06137fdSPaul Mullowney   cusparseStatus_t   stat;
46b06137fdSPaul Mullowney   Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
47b06137fdSPaul Mullowney 
48b06137fdSPaul Mullowney   PetscFunctionBegin;
49b06137fdSPaul Mullowney   cusparsestruct->stream = stream;
5057d48284SJunchao Zhang   stat = cusparseSetStream(cusparsestruct->handle,cusparsestruct->stream);CHKERRCUSPARSE(stat);
51b06137fdSPaul Mullowney   PetscFunctionReturn(0);
52b06137fdSPaul Mullowney }
53b06137fdSPaul Mullowney 
54b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSESetHandle(Mat A,const cusparseHandle_t handle)
55b06137fdSPaul Mullowney {
56b06137fdSPaul Mullowney   cusparseStatus_t   stat;
57b06137fdSPaul Mullowney   Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
58b06137fdSPaul Mullowney 
59b06137fdSPaul Mullowney   PetscFunctionBegin;
606b1cf21dSAlejandro Lamas Daviña   if (cusparsestruct->handle != handle) {
6116a2e217SAlejandro Lamas Daviña     if (cusparsestruct->handle) {
6257d48284SJunchao Zhang       stat = cusparseDestroy(cusparsestruct->handle);CHKERRCUSPARSE(stat);
6316a2e217SAlejandro Lamas Daviña     }
64b06137fdSPaul Mullowney     cusparsestruct->handle = handle;
656b1cf21dSAlejandro Lamas Daviña   }
6657d48284SJunchao Zhang   stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUSPARSE(stat);
67b06137fdSPaul Mullowney   PetscFunctionReturn(0);
68b06137fdSPaul Mullowney }
69b06137fdSPaul Mullowney 
70b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSEClearHandle(Mat A)
71b06137fdSPaul Mullowney {
72b06137fdSPaul Mullowney   Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
73b06137fdSPaul Mullowney   PetscFunctionBegin;
74b06137fdSPaul Mullowney   if (cusparsestruct->handle)
75b06137fdSPaul Mullowney     cusparsestruct->handle = 0;
76b06137fdSPaul Mullowney   PetscFunctionReturn(0);
77b06137fdSPaul Mullowney }
78b06137fdSPaul Mullowney 
79ea799195SBarry Smith PetscErrorCode MatFactorGetSolverType_seqaij_cusparse(Mat A,MatSolverType *type)
809ae82921SPaul Mullowney {
819ae82921SPaul Mullowney   PetscFunctionBegin;
829ae82921SPaul Mullowney   *type = MATSOLVERCUSPARSE;
839ae82921SPaul Mullowney   PetscFunctionReturn(0);
849ae82921SPaul Mullowney }
859ae82921SPaul Mullowney 
86c708e6cdSJed Brown /*MC
87087f3262SPaul Mullowney   MATSOLVERCUSPARSE = "cusparse" - A matrix type providing triangular solvers for seq matrices
88087f3262SPaul Mullowney   on a single GPU of type, seqaijcusparse, aijcusparse, or seqaijcusp, aijcusp. Currently supported
89087f3262SPaul Mullowney   algorithms are ILU(k) and ICC(k). Typically, deeper factorizations (larger k) results in poorer
90087f3262SPaul Mullowney   performance in the triangular solves. Full LU, and Cholesky decompositions can be solved through the
91087f3262SPaul Mullowney   CUSPARSE triangular solve algorithm. However, the performance can be quite poor and thus these
92087f3262SPaul Mullowney   algorithms are not recommended. This class does NOT support direct solver operations.
93c708e6cdSJed Brown 
949ae82921SPaul Mullowney   Level: beginner
95c708e6cdSJed Brown 
963ca39a21SBarry Smith .seealso: PCFactorSetMatSolverType(), MatSolverType, MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation
97c708e6cdSJed Brown M*/
989ae82921SPaul Mullowney 
9942c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat A,MatFactorType ftype,Mat *B)
1009ae82921SPaul Mullowney {
1019ae82921SPaul Mullowney   PetscErrorCode ierr;
102bc3f50f2SPaul Mullowney   PetscInt       n = A->rmap->n;
1039ae82921SPaul Mullowney 
1049ae82921SPaul Mullowney   PetscFunctionBegin;
105bc3f50f2SPaul Mullowney   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
106404133a2SPaul Mullowney   (*B)->factortype = ftype;
107bc3f50f2SPaul Mullowney   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
1089ae82921SPaul Mullowney   ierr = MatSetType(*B,MATSEQAIJCUSPARSE);CHKERRQ(ierr);
1092205254eSKarl Rupp 
110087f3262SPaul Mullowney   if (ftype == MAT_FACTOR_LU || ftype == MAT_FACTOR_ILU || ftype == MAT_FACTOR_ILUDT) {
11133d57670SJed Brown     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
1129ae82921SPaul Mullowney     (*B)->ops->ilufactorsymbolic = MatILUFactorSymbolic_SeqAIJCUSPARSE;
1139ae82921SPaul Mullowney     (*B)->ops->lufactorsymbolic  = MatLUFactorSymbolic_SeqAIJCUSPARSE;
114087f3262SPaul Mullowney   } else if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
115087f3262SPaul Mullowney     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqAIJCUSPARSE;
116087f3262SPaul Mullowney     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqAIJCUSPARSE;
1179ae82921SPaul Mullowney   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported for CUSPARSE Matrix Types");
118bc3f50f2SPaul Mullowney 
119fa03d054SJed Brown   ierr = MatSeqAIJSetPreallocation(*B,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr);
1203ca39a21SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)(*B),"MatFactorGetSolverType_C",MatFactorGetSolverType_seqaij_cusparse);CHKERRQ(ierr);
1219ae82921SPaul Mullowney   PetscFunctionReturn(0);
1229ae82921SPaul Mullowney }
1239ae82921SPaul Mullowney 
124bc3f50f2SPaul Mullowney PETSC_INTERN PetscErrorCode MatCUSPARSESetFormat_SeqAIJCUSPARSE(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format)
125ca45077fSPaul Mullowney {
126aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
1276e111a19SKarl Rupp 
128ca45077fSPaul Mullowney   PetscFunctionBegin;
129ca45077fSPaul Mullowney   switch (op) {
130e057df02SPaul Mullowney   case MAT_CUSPARSE_MULT:
131aa372e3fSPaul Mullowney     cusparsestruct->format = format;
132ca45077fSPaul Mullowney     break;
133e057df02SPaul Mullowney   case MAT_CUSPARSE_ALL:
134aa372e3fSPaul Mullowney     cusparsestruct->format = format;
135ca45077fSPaul Mullowney     break;
136ca45077fSPaul Mullowney   default:
13736d62e41SPaul Mullowney     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unsupported operation %d for MatCUSPARSEFormatOperation. MAT_CUSPARSE_MULT and MAT_CUSPARSE_ALL are currently supported.",op);
138ca45077fSPaul Mullowney   }
139ca45077fSPaul Mullowney   PetscFunctionReturn(0);
140ca45077fSPaul Mullowney }
1419ae82921SPaul Mullowney 
142e057df02SPaul Mullowney /*@
143e057df02SPaul Mullowney    MatCUSPARSESetFormat - Sets the storage format of CUSPARSE matrices for a particular
144e057df02SPaul Mullowney    operation. Only the MatMult operation can use different GPU storage formats
145aa372e3fSPaul Mullowney    for MPIAIJCUSPARSE matrices.
146e057df02SPaul Mullowney    Not Collective
147e057df02SPaul Mullowney 
148e057df02SPaul Mullowney    Input Parameters:
1498468deeeSKarl Rupp +  A - Matrix of type SEQAIJCUSPARSE
15036d62e41SPaul Mullowney .  op - MatCUSPARSEFormatOperation. SEQAIJCUSPARSE matrices support MAT_CUSPARSE_MULT and MAT_CUSPARSE_ALL. MPIAIJCUSPARSE matrices support MAT_CUSPARSE_MULT_DIAG, MAT_CUSPARSE_MULT_OFFDIAG, and MAT_CUSPARSE_ALL.
1512692e278SPaul Mullowney -  format - MatCUSPARSEStorageFormat (one of MAT_CUSPARSE_CSR, MAT_CUSPARSE_ELL, MAT_CUSPARSE_HYB. The latter two require CUDA 4.2)
152e057df02SPaul Mullowney 
153e057df02SPaul Mullowney    Output Parameter:
154e057df02SPaul Mullowney 
155e057df02SPaul Mullowney    Level: intermediate
156e057df02SPaul Mullowney 
1578468deeeSKarl Rupp .seealso: MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation
158e057df02SPaul Mullowney @*/
159e057df02SPaul Mullowney PetscErrorCode MatCUSPARSESetFormat(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format)
160e057df02SPaul Mullowney {
161e057df02SPaul Mullowney   PetscErrorCode ierr;
1626e111a19SKarl Rupp 
163e057df02SPaul Mullowney   PetscFunctionBegin;
164e057df02SPaul Mullowney   PetscValidHeaderSpecific(A, MAT_CLASSID,1);
165e057df02SPaul Mullowney   ierr = PetscTryMethod(A, "MatCUSPARSESetFormat_C",(Mat,MatCUSPARSEFormatOperation,MatCUSPARSEStorageFormat),(A,op,format));CHKERRQ(ierr);
166e057df02SPaul Mullowney   PetscFunctionReturn(0);
167e057df02SPaul Mullowney }
168e057df02SPaul Mullowney 
1694416b707SBarry Smith static PetscErrorCode MatSetFromOptions_SeqAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat A)
1709ae82921SPaul Mullowney {
1719ae82921SPaul Mullowney   PetscErrorCode           ierr;
172e057df02SPaul Mullowney   MatCUSPARSEStorageFormat format;
1739ae82921SPaul Mullowney   PetscBool                flg;
174a183c035SDominic Meiser   Mat_SeqAIJCUSPARSE       *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
1756e111a19SKarl Rupp 
1769ae82921SPaul Mullowney   PetscFunctionBegin;
177e55864a3SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"SeqAIJCUSPARSE options");CHKERRQ(ierr);
1789ae82921SPaul Mullowney   if (A->factortype==MAT_FACTOR_NONE) {
179e057df02SPaul Mullowney     ierr = PetscOptionsEnum("-mat_cusparse_mult_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV",
180a183c035SDominic Meiser                             "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr);
181e057df02SPaul Mullowney     if (flg) {
182e057df02SPaul Mullowney       ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_MULT,format);CHKERRQ(ierr);
183045c96e1SPaul Mullowney     }
1849ae82921SPaul Mullowney   }
1854c87dfd4SPaul Mullowney   ierr = PetscOptionsEnum("-mat_cusparse_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV and TriSolve",
186a183c035SDominic Meiser                           "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr);
1874c87dfd4SPaul Mullowney   if (flg) {
1884c87dfd4SPaul Mullowney     ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_ALL,format);CHKERRQ(ierr);
1894c87dfd4SPaul Mullowney   }
1900af67c1bSStefano Zampini   ierr = PetscOptionsTail();CHKERRQ(ierr);
1919ae82921SPaul Mullowney   PetscFunctionReturn(0);
1929ae82921SPaul Mullowney 
1939ae82921SPaul Mullowney }
1949ae82921SPaul Mullowney 
1956fa9248bSJed Brown static PetscErrorCode MatILUFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
1969ae82921SPaul Mullowney {
1979ae82921SPaul Mullowney   PetscErrorCode ierr;
1989ae82921SPaul Mullowney 
1999ae82921SPaul Mullowney   PetscFunctionBegin;
2009ae82921SPaul Mullowney   ierr = MatILUFactorSymbolic_SeqAIJ(B,A,isrow,iscol,info);CHKERRQ(ierr);
2019ae82921SPaul Mullowney   B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJCUSPARSE;
2029ae82921SPaul Mullowney   PetscFunctionReturn(0);
2039ae82921SPaul Mullowney }
2049ae82921SPaul Mullowney 
2056fa9248bSJed Brown static PetscErrorCode MatLUFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
2069ae82921SPaul Mullowney {
2079ae82921SPaul Mullowney   PetscErrorCode ierr;
2089ae82921SPaul Mullowney 
2099ae82921SPaul Mullowney   PetscFunctionBegin;
2109ae82921SPaul Mullowney   ierr = MatLUFactorSymbolic_SeqAIJ(B,A,isrow,iscol,info);CHKERRQ(ierr);
2119ae82921SPaul Mullowney   B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJCUSPARSE;
2129ae82921SPaul Mullowney   PetscFunctionReturn(0);
2139ae82921SPaul Mullowney }
2149ae82921SPaul Mullowney 
215087f3262SPaul Mullowney static PetscErrorCode MatICCFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS perm,const MatFactorInfo *info)
216087f3262SPaul Mullowney {
217087f3262SPaul Mullowney   PetscErrorCode ierr;
218087f3262SPaul Mullowney 
219087f3262SPaul Mullowney   PetscFunctionBegin;
220087f3262SPaul Mullowney   ierr = MatICCFactorSymbolic_SeqAIJ(B,A,perm,info);CHKERRQ(ierr);
221087f3262SPaul Mullowney   B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJCUSPARSE;
222087f3262SPaul Mullowney   PetscFunctionReturn(0);
223087f3262SPaul Mullowney }
224087f3262SPaul Mullowney 
225087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS perm,const MatFactorInfo *info)
226087f3262SPaul Mullowney {
227087f3262SPaul Mullowney   PetscErrorCode ierr;
228087f3262SPaul Mullowney 
229087f3262SPaul Mullowney   PetscFunctionBegin;
230087f3262SPaul Mullowney   ierr = MatCholeskyFactorSymbolic_SeqAIJ(B,A,perm,info);CHKERRQ(ierr);
231087f3262SPaul Mullowney   B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJCUSPARSE;
232087f3262SPaul Mullowney   PetscFunctionReturn(0);
233087f3262SPaul Mullowney }
234087f3262SPaul Mullowney 
235087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILULowerTriMatrix(Mat A)
2369ae82921SPaul Mullowney {
2379ae82921SPaul Mullowney   Mat_SeqAIJ                        *a = (Mat_SeqAIJ*)A->data;
2389ae82921SPaul Mullowney   PetscInt                          n = A->rmap->n;
2399ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
240aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
2419ae82921SPaul Mullowney   cusparseStatus_t                  stat;
2429ae82921SPaul Mullowney   const PetscInt                    *ai = a->i,*aj = a->j,*vi;
2439ae82921SPaul Mullowney   const MatScalar                   *aa = a->a,*v;
2449ae82921SPaul Mullowney   PetscInt                          *AiLo, *AjLo;
2459ae82921SPaul Mullowney   PetscScalar                       *AALo;
2469ae82921SPaul Mullowney   PetscInt                          i,nz, nzLower, offset, rowOffset;
247b175d8bbSPaul Mullowney   PetscErrorCode                    ierr;
24857d48284SJunchao Zhang   cudaError_t                       cerr;
2499ae82921SPaul Mullowney 
2509ae82921SPaul Mullowney   PetscFunctionBegin;
251cf00fe3bSKarl Rupp   if (!n) PetscFunctionReturn(0);
252c70f7ee4SJunchao Zhang   if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) {
2539ae82921SPaul Mullowney     try {
2549ae82921SPaul Mullowney       /* first figure out the number of nonzeros in the lower triangular matrix including 1's on the diagonal. */
2559ae82921SPaul Mullowney       nzLower=n+ai[n]-ai[1];
2569ae82921SPaul Mullowney 
2579ae82921SPaul Mullowney       /* Allocate Space for the lower triangular matrix */
25857d48284SJunchao Zhang       cerr = cudaMallocHost((void**) &AiLo, (n+1)*sizeof(PetscInt));CHKERRCUDA(cerr);
25957d48284SJunchao Zhang       cerr = cudaMallocHost((void**) &AjLo, nzLower*sizeof(PetscInt));CHKERRCUDA(cerr);
26057d48284SJunchao Zhang       cerr = cudaMallocHost((void**) &AALo, nzLower*sizeof(PetscScalar));CHKERRCUDA(cerr);
2619ae82921SPaul Mullowney 
2629ae82921SPaul Mullowney       /* Fill the lower triangular matrix */
2639ae82921SPaul Mullowney       AiLo[0]  = (PetscInt) 0;
2649ae82921SPaul Mullowney       AiLo[n]  = nzLower;
2659ae82921SPaul Mullowney       AjLo[0]  = (PetscInt) 0;
2669ae82921SPaul Mullowney       AALo[0]  = (MatScalar) 1.0;
2679ae82921SPaul Mullowney       v        = aa;
2689ae82921SPaul Mullowney       vi       = aj;
2699ae82921SPaul Mullowney       offset   = 1;
2709ae82921SPaul Mullowney       rowOffset= 1;
2719ae82921SPaul Mullowney       for (i=1; i<n; i++) {
2729ae82921SPaul Mullowney         nz = ai[i+1] - ai[i];
273e057df02SPaul Mullowney         /* additional 1 for the term on the diagonal */
2749ae82921SPaul Mullowney         AiLo[i]    = rowOffset;
2759ae82921SPaul Mullowney         rowOffset += nz+1;
2769ae82921SPaul Mullowney 
277580bdb30SBarry Smith         ierr = PetscArraycpy(&(AjLo[offset]), vi, nz);CHKERRQ(ierr);
278580bdb30SBarry Smith         ierr = PetscArraycpy(&(AALo[offset]), v, nz);CHKERRQ(ierr);
2799ae82921SPaul Mullowney 
2809ae82921SPaul Mullowney         offset      += nz;
2819ae82921SPaul Mullowney         AjLo[offset] = (PetscInt) i;
2829ae82921SPaul Mullowney         AALo[offset] = (MatScalar) 1.0;
2839ae82921SPaul Mullowney         offset      += 1;
2849ae82921SPaul Mullowney 
2859ae82921SPaul Mullowney         v  += nz;
2869ae82921SPaul Mullowney         vi += nz;
2879ae82921SPaul Mullowney       }
2882205254eSKarl Rupp 
289aa372e3fSPaul Mullowney       /* allocate space for the triangular factor information */
290aa372e3fSPaul Mullowney       loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct;
2912205254eSKarl Rupp 
292aa372e3fSPaul Mullowney       /* Create the matrix description */
29357d48284SJunchao Zhang       stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUSPARSE(stat);
29457d48284SJunchao Zhang       stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat);
29557d48284SJunchao Zhang       stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUSPARSE(stat);
29657d48284SJunchao Zhang       stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_LOWER);CHKERRCUSPARSE(stat);
29757d48284SJunchao Zhang       stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUSPARSE(stat);
298aa372e3fSPaul Mullowney 
299aa372e3fSPaul Mullowney       /* Create the solve analysis information */
30057d48284SJunchao Zhang       stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUSPARSE(stat);
301aa372e3fSPaul Mullowney 
302aa372e3fSPaul Mullowney       /* set the operation */
303aa372e3fSPaul Mullowney       loTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
304aa372e3fSPaul Mullowney 
305aa372e3fSPaul Mullowney       /* set the matrix */
306aa372e3fSPaul Mullowney       loTriFactor->csrMat = new CsrMatrix;
307aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_rows = n;
308aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_cols = n;
309aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_entries = nzLower;
310aa372e3fSPaul Mullowney 
311aa372e3fSPaul Mullowney       loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1);
312aa372e3fSPaul Mullowney       loTriFactor->csrMat->row_offsets->assign(AiLo, AiLo+n+1);
313aa372e3fSPaul Mullowney 
314aa372e3fSPaul Mullowney       loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzLower);
315aa372e3fSPaul Mullowney       loTriFactor->csrMat->column_indices->assign(AjLo, AjLo+nzLower);
316aa372e3fSPaul Mullowney 
317aa372e3fSPaul Mullowney       loTriFactor->csrMat->values = new THRUSTARRAY(nzLower);
318aa372e3fSPaul Mullowney       loTriFactor->csrMat->values->assign(AALo, AALo+nzLower);
319aa372e3fSPaul Mullowney 
320aa372e3fSPaul Mullowney       /* perform the solve analysis */
321aa372e3fSPaul Mullowney       stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp,
322aa372e3fSPaul Mullowney                                loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr,
323aa372e3fSPaul Mullowney                                loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(),
32457d48284SJunchao Zhang                                loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUSPARSE(stat);
325aa372e3fSPaul Mullowney 
326aa372e3fSPaul Mullowney       /* assign the pointer. Is this really necessary? */
327aa372e3fSPaul Mullowney       ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor;
3282205254eSKarl Rupp 
32957d48284SJunchao Zhang       cerr = cudaFreeHost(AiLo);CHKERRCUDA(cerr);
33057d48284SJunchao Zhang       cerr = cudaFreeHost(AjLo);CHKERRCUDA(cerr);
33157d48284SJunchao Zhang       cerr = cudaFreeHost(AALo);CHKERRCUDA(cerr);
3324863603aSSatish Balay       ierr = PetscLogCpuToGpu((n+1+nzLower)*sizeof(int)+nzLower*sizeof(PetscScalar));CHKERRQ(ierr);
3339ae82921SPaul Mullowney     } catch(char *ex) {
3349ae82921SPaul Mullowney       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
3359ae82921SPaul Mullowney     }
3369ae82921SPaul Mullowney   }
3379ae82921SPaul Mullowney   PetscFunctionReturn(0);
3389ae82921SPaul Mullowney }
3399ae82921SPaul Mullowney 
340087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(Mat A)
3419ae82921SPaul Mullowney {
3429ae82921SPaul Mullowney   Mat_SeqAIJ                        *a = (Mat_SeqAIJ*)A->data;
3439ae82921SPaul Mullowney   PetscInt                          n = A->rmap->n;
3449ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
345aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
3469ae82921SPaul Mullowney   cusparseStatus_t                  stat;
3479ae82921SPaul Mullowney   const PetscInt                    *aj = a->j,*adiag = a->diag,*vi;
3489ae82921SPaul Mullowney   const MatScalar                   *aa = a->a,*v;
3499ae82921SPaul Mullowney   PetscInt                          *AiUp, *AjUp;
3509ae82921SPaul Mullowney   PetscScalar                       *AAUp;
3519ae82921SPaul Mullowney   PetscInt                          i,nz, nzUpper, offset;
3529ae82921SPaul Mullowney   PetscErrorCode                    ierr;
35357d48284SJunchao Zhang   cudaError_t                       cerr;
3549ae82921SPaul Mullowney 
3559ae82921SPaul Mullowney   PetscFunctionBegin;
356cf00fe3bSKarl Rupp   if (!n) PetscFunctionReturn(0);
357c70f7ee4SJunchao Zhang   if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) {
3589ae82921SPaul Mullowney     try {
3599ae82921SPaul Mullowney       /* next, figure out the number of nonzeros in the upper triangular matrix. */
3609ae82921SPaul Mullowney       nzUpper = adiag[0]-adiag[n];
3619ae82921SPaul Mullowney 
3629ae82921SPaul Mullowney       /* Allocate Space for the upper triangular matrix */
36357d48284SJunchao Zhang       cerr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(cerr);
36457d48284SJunchao Zhang       cerr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(cerr);
36557d48284SJunchao Zhang       cerr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(cerr);
3669ae82921SPaul Mullowney 
3679ae82921SPaul Mullowney       /* Fill the upper triangular matrix */
3689ae82921SPaul Mullowney       AiUp[0]=(PetscInt) 0;
3699ae82921SPaul Mullowney       AiUp[n]=nzUpper;
3709ae82921SPaul Mullowney       offset = nzUpper;
3719ae82921SPaul Mullowney       for (i=n-1; i>=0; i--) {
3729ae82921SPaul Mullowney         v  = aa + adiag[i+1] + 1;
3739ae82921SPaul Mullowney         vi = aj + adiag[i+1] + 1;
3749ae82921SPaul Mullowney 
375e057df02SPaul Mullowney         /* number of elements NOT on the diagonal */
3769ae82921SPaul Mullowney         nz = adiag[i] - adiag[i+1]-1;
3779ae82921SPaul Mullowney 
378e057df02SPaul Mullowney         /* decrement the offset */
3799ae82921SPaul Mullowney         offset -= (nz+1);
3809ae82921SPaul Mullowney 
381e057df02SPaul Mullowney         /* first, set the diagonal elements */
3829ae82921SPaul Mullowney         AjUp[offset] = (PetscInt) i;
38309f51544SAlejandro Lamas Daviña         AAUp[offset] = (MatScalar)1./v[nz];
3849ae82921SPaul Mullowney         AiUp[i]      = AiUp[i+1] - (nz+1);
3859ae82921SPaul Mullowney 
386580bdb30SBarry Smith         ierr = PetscArraycpy(&(AjUp[offset+1]), vi, nz);CHKERRQ(ierr);
387580bdb30SBarry Smith         ierr = PetscArraycpy(&(AAUp[offset+1]), v, nz);CHKERRQ(ierr);
3889ae82921SPaul Mullowney       }
3892205254eSKarl Rupp 
390aa372e3fSPaul Mullowney       /* allocate space for the triangular factor information */
391aa372e3fSPaul Mullowney       upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct;
3922205254eSKarl Rupp 
393aa372e3fSPaul Mullowney       /* Create the matrix description */
39457d48284SJunchao Zhang       stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUSPARSE(stat);
39557d48284SJunchao Zhang       stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat);
39657d48284SJunchao Zhang       stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUSPARSE(stat);
39757d48284SJunchao Zhang       stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUSPARSE(stat);
39857d48284SJunchao Zhang       stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUSPARSE(stat);
399aa372e3fSPaul Mullowney 
400aa372e3fSPaul Mullowney       /* Create the solve analysis information */
40157d48284SJunchao Zhang       stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUSPARSE(stat);
402aa372e3fSPaul Mullowney 
403aa372e3fSPaul Mullowney       /* set the operation */
404aa372e3fSPaul Mullowney       upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
405aa372e3fSPaul Mullowney 
406aa372e3fSPaul Mullowney       /* set the matrix */
407aa372e3fSPaul Mullowney       upTriFactor->csrMat = new CsrMatrix;
408aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_rows = n;
409aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_cols = n;
410aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_entries = nzUpper;
411aa372e3fSPaul Mullowney 
412aa372e3fSPaul Mullowney       upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1);
413aa372e3fSPaul Mullowney       upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+n+1);
414aa372e3fSPaul Mullowney 
415aa372e3fSPaul Mullowney       upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzUpper);
416aa372e3fSPaul Mullowney       upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+nzUpper);
417aa372e3fSPaul Mullowney 
418aa372e3fSPaul Mullowney       upTriFactor->csrMat->values = new THRUSTARRAY(nzUpper);
419aa372e3fSPaul Mullowney       upTriFactor->csrMat->values->assign(AAUp, AAUp+nzUpper);
420aa372e3fSPaul Mullowney 
421aa372e3fSPaul Mullowney       /* perform the solve analysis */
422aa372e3fSPaul Mullowney       stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp,
423aa372e3fSPaul Mullowney                                upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr,
424aa372e3fSPaul Mullowney                                upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(),
42557d48284SJunchao Zhang                                upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUSPARSE(stat);
426aa372e3fSPaul Mullowney 
427aa372e3fSPaul Mullowney       /* assign the pointer. Is this really necessary? */
428aa372e3fSPaul Mullowney       ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor;
4292205254eSKarl Rupp 
43057d48284SJunchao Zhang       cerr = cudaFreeHost(AiUp);CHKERRCUDA(cerr);
43157d48284SJunchao Zhang       cerr = cudaFreeHost(AjUp);CHKERRCUDA(cerr);
43257d48284SJunchao Zhang       cerr = cudaFreeHost(AAUp);CHKERRCUDA(cerr);
4334863603aSSatish Balay       ierr = PetscLogCpuToGpu((n+1+nzUpper)*sizeof(int)+nzUpper*sizeof(PetscScalar));CHKERRQ(ierr);
4349ae82921SPaul Mullowney     } catch(char *ex) {
4359ae82921SPaul Mullowney       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
4369ae82921SPaul Mullowney     }
4379ae82921SPaul Mullowney   }
4389ae82921SPaul Mullowney   PetscFunctionReturn(0);
4399ae82921SPaul Mullowney }
4409ae82921SPaul Mullowney 
441087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(Mat A)
4429ae82921SPaul Mullowney {
4439ae82921SPaul Mullowney   PetscErrorCode               ierr;
4449ae82921SPaul Mullowney   Mat_SeqAIJ                   *a                  = (Mat_SeqAIJ*)A->data;
4459ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
4469ae82921SPaul Mullowney   IS                           isrow = a->row,iscol = a->icol;
4479ae82921SPaul Mullowney   PetscBool                    row_identity,col_identity;
4489ae82921SPaul Mullowney   const PetscInt               *r,*c;
4499ae82921SPaul Mullowney   PetscInt                     n = A->rmap->n;
4509ae82921SPaul Mullowney 
4519ae82921SPaul Mullowney   PetscFunctionBegin;
452087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEBuildILULowerTriMatrix(A);CHKERRQ(ierr);
453087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(A);CHKERRQ(ierr);
4542205254eSKarl Rupp 
455e65717acSKarl Rupp   cusparseTriFactors->workVector = new THRUSTARRAY(n);
456aa372e3fSPaul Mullowney   cusparseTriFactors->nnz=a->nz;
4579ae82921SPaul Mullowney 
458c70f7ee4SJunchao Zhang   A->offloadmask = PETSC_OFFLOAD_BOTH;
459e057df02SPaul Mullowney   /* lower triangular indices */
4609ae82921SPaul Mullowney   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
4619ae82921SPaul Mullowney   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
4622205254eSKarl Rupp   if (!row_identity) {
463aa372e3fSPaul Mullowney     cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n);
464aa372e3fSPaul Mullowney     cusparseTriFactors->rpermIndices->assign(r, r+n);
4652205254eSKarl Rupp   }
4669ae82921SPaul Mullowney   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
4679ae82921SPaul Mullowney 
468e057df02SPaul Mullowney   /* upper triangular indices */
4699ae82921SPaul Mullowney   ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr);
4709ae82921SPaul Mullowney   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
4712205254eSKarl Rupp   if (!col_identity) {
472aa372e3fSPaul Mullowney     cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n);
473aa372e3fSPaul Mullowney     cusparseTriFactors->cpermIndices->assign(c, c+n);
4742205254eSKarl Rupp   }
4754863603aSSatish Balay 
4764863603aSSatish Balay   if (!row_identity && !col_identity) {
4774863603aSSatish Balay     ierr = PetscLogCpuToGpu(2*n*sizeof(PetscInt));CHKERRQ(ierr);
4784863603aSSatish Balay   } else if(!row_identity) {
4794863603aSSatish Balay     ierr = PetscLogCpuToGpu(n*sizeof(PetscInt));CHKERRQ(ierr);
4804863603aSSatish Balay   } else if(!col_identity) {
4814863603aSSatish Balay     ierr = PetscLogCpuToGpu(n*sizeof(PetscInt));CHKERRQ(ierr);
4824863603aSSatish Balay   }
4834863603aSSatish Balay 
4849ae82921SPaul Mullowney   ierr = ISRestoreIndices(iscol,&c);CHKERRQ(ierr);
4859ae82921SPaul Mullowney   PetscFunctionReturn(0);
4869ae82921SPaul Mullowney }
4879ae82921SPaul Mullowney 
488087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildICCTriMatrices(Mat A)
489087f3262SPaul Mullowney {
490087f3262SPaul Mullowney   Mat_SeqAIJ                        *a = (Mat_SeqAIJ*)A->data;
491087f3262SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
492aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
493aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
494087f3262SPaul Mullowney   cusparseStatus_t                  stat;
495087f3262SPaul Mullowney   PetscErrorCode                    ierr;
49657d48284SJunchao Zhang   cudaError_t                       cerr;
497087f3262SPaul Mullowney   PetscInt                          *AiUp, *AjUp;
498087f3262SPaul Mullowney   PetscScalar                       *AAUp;
499087f3262SPaul Mullowney   PetscScalar                       *AALo;
500087f3262SPaul Mullowney   PetscInt                          nzUpper = a->nz,n = A->rmap->n,i,offset,nz,j;
501087f3262SPaul Mullowney   Mat_SeqSBAIJ                      *b = (Mat_SeqSBAIJ*)A->data;
502087f3262SPaul Mullowney   const PetscInt                    *ai = b->i,*aj = b->j,*vj;
503087f3262SPaul Mullowney   const MatScalar                   *aa = b->a,*v;
504087f3262SPaul Mullowney 
505087f3262SPaul Mullowney   PetscFunctionBegin;
506cf00fe3bSKarl Rupp   if (!n) PetscFunctionReturn(0);
507c70f7ee4SJunchao Zhang   if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) {
508087f3262SPaul Mullowney     try {
509087f3262SPaul Mullowney       /* Allocate Space for the upper triangular matrix */
51057d48284SJunchao Zhang       cerr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(cerr);
51157d48284SJunchao Zhang       cerr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(cerr);
51257d48284SJunchao Zhang       cerr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(cerr);
51357d48284SJunchao Zhang       cerr = cudaMallocHost((void**) &AALo, nzUpper*sizeof(PetscScalar));CHKERRCUDA(cerr);
514087f3262SPaul Mullowney 
515087f3262SPaul Mullowney       /* Fill the upper triangular matrix */
516087f3262SPaul Mullowney       AiUp[0]=(PetscInt) 0;
517087f3262SPaul Mullowney       AiUp[n]=nzUpper;
518087f3262SPaul Mullowney       offset = 0;
519087f3262SPaul Mullowney       for (i=0; i<n; i++) {
520087f3262SPaul Mullowney         /* set the pointers */
521087f3262SPaul Mullowney         v  = aa + ai[i];
522087f3262SPaul Mullowney         vj = aj + ai[i];
523087f3262SPaul Mullowney         nz = ai[i+1] - ai[i] - 1; /* exclude diag[i] */
524087f3262SPaul Mullowney 
525087f3262SPaul Mullowney         /* first, set the diagonal elements */
526087f3262SPaul Mullowney         AjUp[offset] = (PetscInt) i;
52709f51544SAlejandro Lamas Daviña         AAUp[offset] = (MatScalar)1.0/v[nz];
528087f3262SPaul Mullowney         AiUp[i]      = offset;
52909f51544SAlejandro Lamas Daviña         AALo[offset] = (MatScalar)1.0/v[nz];
530087f3262SPaul Mullowney 
531087f3262SPaul Mullowney         offset+=1;
532087f3262SPaul Mullowney         if (nz>0) {
533f22e0265SBarry Smith           ierr = PetscArraycpy(&(AjUp[offset]), vj, nz);CHKERRQ(ierr);
534580bdb30SBarry Smith           ierr = PetscArraycpy(&(AAUp[offset]), v, nz);CHKERRQ(ierr);
535087f3262SPaul Mullowney           for (j=offset; j<offset+nz; j++) {
536087f3262SPaul Mullowney             AAUp[j] = -AAUp[j];
537087f3262SPaul Mullowney             AALo[j] = AAUp[j]/v[nz];
538087f3262SPaul Mullowney           }
539087f3262SPaul Mullowney           offset+=nz;
540087f3262SPaul Mullowney         }
541087f3262SPaul Mullowney       }
542087f3262SPaul Mullowney 
543aa372e3fSPaul Mullowney       /* allocate space for the triangular factor information */
544aa372e3fSPaul Mullowney       upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct;
545087f3262SPaul Mullowney 
546aa372e3fSPaul Mullowney       /* Create the matrix description */
54757d48284SJunchao Zhang       stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUSPARSE(stat);
54857d48284SJunchao Zhang       stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat);
54957d48284SJunchao Zhang       stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUSPARSE(stat);
55057d48284SJunchao Zhang       stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUSPARSE(stat);
55157d48284SJunchao Zhang       stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUSPARSE(stat);
552087f3262SPaul Mullowney 
553aa372e3fSPaul Mullowney       /* Create the solve analysis information */
55457d48284SJunchao Zhang       stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUSPARSE(stat);
555aa372e3fSPaul Mullowney 
556aa372e3fSPaul Mullowney       /* set the operation */
557aa372e3fSPaul Mullowney       upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
558aa372e3fSPaul Mullowney 
559aa372e3fSPaul Mullowney       /* set the matrix */
560aa372e3fSPaul Mullowney       upTriFactor->csrMat = new CsrMatrix;
561aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_rows = A->rmap->n;
562aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_cols = A->cmap->n;
563aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_entries = a->nz;
564aa372e3fSPaul Mullowney 
565aa372e3fSPaul Mullowney       upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
566aa372e3fSPaul Mullowney       upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1);
567aa372e3fSPaul Mullowney 
568aa372e3fSPaul Mullowney       upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz);
569aa372e3fSPaul Mullowney       upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz);
570aa372e3fSPaul Mullowney 
571aa372e3fSPaul Mullowney       upTriFactor->csrMat->values = new THRUSTARRAY(a->nz);
572aa372e3fSPaul Mullowney       upTriFactor->csrMat->values->assign(AAUp, AAUp+a->nz);
573aa372e3fSPaul Mullowney 
574aa372e3fSPaul Mullowney       /* perform the solve analysis */
575aa372e3fSPaul Mullowney       stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp,
576aa372e3fSPaul Mullowney                                upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr,
577aa372e3fSPaul Mullowney                                upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(),
57857d48284SJunchao Zhang                                upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUSPARSE(stat);
579aa372e3fSPaul Mullowney 
580aa372e3fSPaul Mullowney       /* assign the pointer. Is this really necessary? */
581aa372e3fSPaul Mullowney       ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor;
582aa372e3fSPaul Mullowney 
583aa372e3fSPaul Mullowney       /* allocate space for the triangular factor information */
584aa372e3fSPaul Mullowney       loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct;
585aa372e3fSPaul Mullowney 
586aa372e3fSPaul Mullowney       /* Create the matrix description */
58757d48284SJunchao Zhang       stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUSPARSE(stat);
58857d48284SJunchao Zhang       stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat);
58957d48284SJunchao Zhang       stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUSPARSE(stat);
59057d48284SJunchao Zhang       stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUSPARSE(stat);
59157d48284SJunchao Zhang       stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUSPARSE(stat);
592aa372e3fSPaul Mullowney 
593aa372e3fSPaul Mullowney       /* Create the solve analysis information */
59457d48284SJunchao Zhang       stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUSPARSE(stat);
595aa372e3fSPaul Mullowney 
596aa372e3fSPaul Mullowney       /* set the operation */
597aa372e3fSPaul Mullowney       loTriFactor->solveOp = CUSPARSE_OPERATION_TRANSPOSE;
598aa372e3fSPaul Mullowney 
599aa372e3fSPaul Mullowney       /* set the matrix */
600aa372e3fSPaul Mullowney       loTriFactor->csrMat = new CsrMatrix;
601aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_rows = A->rmap->n;
602aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_cols = A->cmap->n;
603aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_entries = a->nz;
604aa372e3fSPaul Mullowney 
605aa372e3fSPaul Mullowney       loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
606aa372e3fSPaul Mullowney       loTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1);
607aa372e3fSPaul Mullowney 
608aa372e3fSPaul Mullowney       loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz);
609aa372e3fSPaul Mullowney       loTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz);
610aa372e3fSPaul Mullowney 
611aa372e3fSPaul Mullowney       loTriFactor->csrMat->values = new THRUSTARRAY(a->nz);
612aa372e3fSPaul Mullowney       loTriFactor->csrMat->values->assign(AALo, AALo+a->nz);
6134863603aSSatish Balay       ierr = PetscLogCpuToGpu(2*(((A->rmap->n+1)+(a->nz))*sizeof(int)+(a->nz)*sizeof(PetscScalar)));CHKERRQ(ierr);
614aa372e3fSPaul Mullowney 
615aa372e3fSPaul Mullowney       /* perform the solve analysis */
616aa372e3fSPaul Mullowney       stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp,
617aa372e3fSPaul Mullowney                                loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr,
618aa372e3fSPaul Mullowney                                loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(),
61957d48284SJunchao Zhang                                loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUSPARSE(stat);
620aa372e3fSPaul Mullowney 
621aa372e3fSPaul Mullowney       /* assign the pointer. Is this really necessary? */
622aa372e3fSPaul Mullowney       ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor;
623087f3262SPaul Mullowney 
624c70f7ee4SJunchao Zhang       A->offloadmask = PETSC_OFFLOAD_BOTH;
62557d48284SJunchao Zhang       cerr = cudaFreeHost(AiUp);CHKERRCUDA(cerr);
62657d48284SJunchao Zhang       cerr = cudaFreeHost(AjUp);CHKERRCUDA(cerr);
62757d48284SJunchao Zhang       cerr = cudaFreeHost(AAUp);CHKERRCUDA(cerr);
62857d48284SJunchao Zhang       cerr = cudaFreeHost(AALo);CHKERRCUDA(cerr);
629087f3262SPaul Mullowney     } catch(char *ex) {
630087f3262SPaul Mullowney       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
631087f3262SPaul Mullowney     }
632087f3262SPaul Mullowney   }
633087f3262SPaul Mullowney   PetscFunctionReturn(0);
634087f3262SPaul Mullowney }
635087f3262SPaul Mullowney 
636087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(Mat A)
6379ae82921SPaul Mullowney {
6389ae82921SPaul Mullowney   PetscErrorCode               ierr;
639087f3262SPaul Mullowney   Mat_SeqAIJ                   *a                  = (Mat_SeqAIJ*)A->data;
640087f3262SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
641087f3262SPaul Mullowney   IS                           ip = a->row;
642087f3262SPaul Mullowney   const PetscInt               *rip;
643087f3262SPaul Mullowney   PetscBool                    perm_identity;
644087f3262SPaul Mullowney   PetscInt                     n = A->rmap->n;
645087f3262SPaul Mullowney 
646087f3262SPaul Mullowney   PetscFunctionBegin;
647087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEBuildICCTriMatrices(A);CHKERRQ(ierr);
648e65717acSKarl Rupp   cusparseTriFactors->workVector = new THRUSTARRAY(n);
649aa372e3fSPaul Mullowney   cusparseTriFactors->nnz=(a->nz-n)*2 + n;
650aa372e3fSPaul Mullowney 
651087f3262SPaul Mullowney   /* lower triangular indices */
652087f3262SPaul Mullowney   ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr);
653087f3262SPaul Mullowney   ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr);
654087f3262SPaul Mullowney   if (!perm_identity) {
6554e4bbfaaSStefano Zampini     IS             iip;
6564e4bbfaaSStefano Zampini     const PetscInt *irip;
6574e4bbfaaSStefano Zampini 
6584e4bbfaaSStefano Zampini     ierr = ISInvertPermutation(ip,PETSC_DECIDE,&iip);CHKERRQ(ierr);
6594e4bbfaaSStefano Zampini     ierr = ISGetIndices(iip,&irip);CHKERRQ(ierr);
660aa372e3fSPaul Mullowney     cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n);
661aa372e3fSPaul Mullowney     cusparseTriFactors->rpermIndices->assign(rip, rip+n);
662aa372e3fSPaul Mullowney     cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n);
6634e4bbfaaSStefano Zampini     cusparseTriFactors->cpermIndices->assign(irip, irip+n);
6644e4bbfaaSStefano Zampini     ierr = ISRestoreIndices(iip,&irip);CHKERRQ(ierr);
6654e4bbfaaSStefano Zampini     ierr = ISDestroy(&iip);CHKERRQ(ierr);
6664863603aSSatish Balay     ierr = PetscLogCpuToGpu(2*n*sizeof(PetscInt));CHKERRQ(ierr);
667087f3262SPaul Mullowney   }
668087f3262SPaul Mullowney   ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr);
669087f3262SPaul Mullowney   PetscFunctionReturn(0);
670087f3262SPaul Mullowney }
671087f3262SPaul Mullowney 
6726fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info)
6739ae82921SPaul Mullowney {
6749ae82921SPaul Mullowney   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
6759ae82921SPaul Mullowney   IS             isrow = b->row,iscol = b->col;
6769ae82921SPaul Mullowney   PetscBool      row_identity,col_identity;
677b175d8bbSPaul Mullowney   PetscErrorCode ierr;
6789ae82921SPaul Mullowney 
6799ae82921SPaul Mullowney   PetscFunctionBegin;
6809ae82921SPaul Mullowney   ierr = MatLUFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr);
681e057df02SPaul Mullowney   /* determine which version of MatSolve needs to be used. */
6829ae82921SPaul Mullowney   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
6839ae82921SPaul Mullowney   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
684bda325fcSPaul Mullowney   if (row_identity && col_identity) {
685bda325fcSPaul Mullowney     B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering;
686bda325fcSPaul Mullowney     B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering;
6874e4bbfaaSStefano Zampini     B->ops->matsolve = NULL;
6884e4bbfaaSStefano Zampini     B->ops->matsolvetranspose = NULL;
689bda325fcSPaul Mullowney   } else {
690bda325fcSPaul Mullowney     B->ops->solve = MatSolve_SeqAIJCUSPARSE;
691bda325fcSPaul Mullowney     B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE;
6924e4bbfaaSStefano Zampini     B->ops->matsolve = NULL;
6934e4bbfaaSStefano Zampini     B->ops->matsolvetranspose = NULL;
694bda325fcSPaul Mullowney   }
6958dc1d2a3SPaul Mullowney 
696e057df02SPaul Mullowney   /* get the triangular factors */
697087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(B);CHKERRQ(ierr);
6989ae82921SPaul Mullowney   PetscFunctionReturn(0);
6999ae82921SPaul Mullowney }
7009ae82921SPaul Mullowney 
701087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info)
702087f3262SPaul Mullowney {
703087f3262SPaul Mullowney   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
704087f3262SPaul Mullowney   IS             ip = b->row;
705087f3262SPaul Mullowney   PetscBool      perm_identity;
706b175d8bbSPaul Mullowney   PetscErrorCode ierr;
707087f3262SPaul Mullowney 
708087f3262SPaul Mullowney   PetscFunctionBegin;
709087f3262SPaul Mullowney   ierr = MatCholeskyFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr);
710087f3262SPaul Mullowney 
711087f3262SPaul Mullowney   /* determine which version of MatSolve needs to be used. */
712087f3262SPaul Mullowney   ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr);
713087f3262SPaul Mullowney   if (perm_identity) {
714087f3262SPaul Mullowney     B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering;
715087f3262SPaul Mullowney     B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering;
7164e4bbfaaSStefano Zampini     B->ops->matsolve = NULL;
7174e4bbfaaSStefano Zampini     B->ops->matsolvetranspose = NULL;
718087f3262SPaul Mullowney   } else {
719087f3262SPaul Mullowney     B->ops->solve = MatSolve_SeqAIJCUSPARSE;
720087f3262SPaul Mullowney     B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE;
7214e4bbfaaSStefano Zampini     B->ops->matsolve = NULL;
7224e4bbfaaSStefano Zampini     B->ops->matsolvetranspose = NULL;
723087f3262SPaul Mullowney   }
724087f3262SPaul Mullowney 
725087f3262SPaul Mullowney   /* get the triangular factors */
726087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(B);CHKERRQ(ierr);
727087f3262SPaul Mullowney   PetscFunctionReturn(0);
728087f3262SPaul Mullowney }
7299ae82921SPaul Mullowney 
730b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(Mat A)
731bda325fcSPaul Mullowney {
732bda325fcSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
733aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
734aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
735aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
736aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
737bda325fcSPaul Mullowney   cusparseStatus_t                  stat;
738aa372e3fSPaul Mullowney   cusparseIndexBase_t               indexBase;
739aa372e3fSPaul Mullowney   cusparseMatrixType_t              matrixType;
740aa372e3fSPaul Mullowney   cusparseFillMode_t                fillMode;
741aa372e3fSPaul Mullowney   cusparseDiagType_t                diagType;
742b175d8bbSPaul Mullowney 
743bda325fcSPaul Mullowney   PetscFunctionBegin;
744bda325fcSPaul Mullowney 
745aa372e3fSPaul Mullowney   /*********************************************/
746aa372e3fSPaul Mullowney   /* Now the Transpose of the Lower Tri Factor */
747aa372e3fSPaul Mullowney   /*********************************************/
748aa372e3fSPaul Mullowney 
749aa372e3fSPaul Mullowney   /* allocate space for the transpose of the lower triangular factor */
750aa372e3fSPaul Mullowney   loTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct;
751aa372e3fSPaul Mullowney 
752aa372e3fSPaul Mullowney   /* set the matrix descriptors of the lower triangular factor */
753aa372e3fSPaul Mullowney   matrixType = cusparseGetMatType(loTriFactor->descr);
754aa372e3fSPaul Mullowney   indexBase = cusparseGetMatIndexBase(loTriFactor->descr);
755aa372e3fSPaul Mullowney   fillMode = cusparseGetMatFillMode(loTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ?
756aa372e3fSPaul Mullowney     CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER;
757aa372e3fSPaul Mullowney   diagType = cusparseGetMatDiagType(loTriFactor->descr);
758aa372e3fSPaul Mullowney 
759aa372e3fSPaul Mullowney   /* Create the matrix description */
76057d48284SJunchao Zhang   stat = cusparseCreateMatDescr(&loTriFactorT->descr);CHKERRCUSPARSE(stat);
76157d48284SJunchao Zhang   stat = cusparseSetMatIndexBase(loTriFactorT->descr, indexBase);CHKERRCUSPARSE(stat);
76257d48284SJunchao Zhang   stat = cusparseSetMatType(loTriFactorT->descr, matrixType);CHKERRCUSPARSE(stat);
76357d48284SJunchao Zhang   stat = cusparseSetMatFillMode(loTriFactorT->descr, fillMode);CHKERRCUSPARSE(stat);
76457d48284SJunchao Zhang   stat = cusparseSetMatDiagType(loTriFactorT->descr, diagType);CHKERRCUSPARSE(stat);
765aa372e3fSPaul Mullowney 
766aa372e3fSPaul Mullowney   /* Create the solve analysis information */
76757d48284SJunchao Zhang   stat = cusparseCreateSolveAnalysisInfo(&loTriFactorT->solveInfo);CHKERRCUSPARSE(stat);
768aa372e3fSPaul Mullowney 
769aa372e3fSPaul Mullowney   /* set the operation */
770aa372e3fSPaul Mullowney   loTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
771aa372e3fSPaul Mullowney 
772aa372e3fSPaul Mullowney   /* allocate GPU space for the CSC of the lower triangular factor*/
773aa372e3fSPaul Mullowney   loTriFactorT->csrMat = new CsrMatrix;
774aa372e3fSPaul Mullowney   loTriFactorT->csrMat->num_rows = loTriFactor->csrMat->num_rows;
775aa372e3fSPaul Mullowney   loTriFactorT->csrMat->num_cols = loTriFactor->csrMat->num_cols;
776aa372e3fSPaul Mullowney   loTriFactorT->csrMat->num_entries = loTriFactor->csrMat->num_entries;
777aa372e3fSPaul Mullowney   loTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(loTriFactor->csrMat->num_rows+1);
778aa372e3fSPaul Mullowney   loTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(loTriFactor->csrMat->num_entries);
779aa372e3fSPaul Mullowney   loTriFactorT->csrMat->values = new THRUSTARRAY(loTriFactor->csrMat->num_entries);
780aa372e3fSPaul Mullowney 
781aa372e3fSPaul Mullowney   /* compute the transpose of the lower triangular factor, i.e. the CSC */
782aa372e3fSPaul Mullowney   stat = cusparse_csr2csc(cusparseTriFactors->handle, loTriFactor->csrMat->num_rows,
783aa372e3fSPaul Mullowney                           loTriFactor->csrMat->num_cols, loTriFactor->csrMat->num_entries,
784aa372e3fSPaul Mullowney                           loTriFactor->csrMat->values->data().get(),
785aa372e3fSPaul Mullowney                           loTriFactor->csrMat->row_offsets->data().get(),
786aa372e3fSPaul Mullowney                           loTriFactor->csrMat->column_indices->data().get(),
787aa372e3fSPaul Mullowney                           loTriFactorT->csrMat->values->data().get(),
788aa372e3fSPaul Mullowney                           loTriFactorT->csrMat->column_indices->data().get(),
789aa372e3fSPaul Mullowney                           loTriFactorT->csrMat->row_offsets->data().get(),
79057d48284SJunchao Zhang                           CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUSPARSE(stat);
791aa372e3fSPaul Mullowney 
792aa372e3fSPaul Mullowney   /* perform the solve analysis on the transposed matrix */
793aa372e3fSPaul Mullowney   stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactorT->solveOp,
794aa372e3fSPaul Mullowney                            loTriFactorT->csrMat->num_rows, loTriFactorT->csrMat->num_entries,
795aa372e3fSPaul Mullowney                            loTriFactorT->descr, loTriFactorT->csrMat->values->data().get(),
796aa372e3fSPaul Mullowney                            loTriFactorT->csrMat->row_offsets->data().get(), loTriFactorT->csrMat->column_indices->data().get(),
79757d48284SJunchao Zhang                            loTriFactorT->solveInfo);CHKERRCUSPARSE(stat);
798aa372e3fSPaul Mullowney 
799aa372e3fSPaul Mullowney   /* assign the pointer. Is this really necessary? */
800aa372e3fSPaul Mullowney   ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtrTranspose = loTriFactorT;
801aa372e3fSPaul Mullowney 
802aa372e3fSPaul Mullowney   /*********************************************/
803aa372e3fSPaul Mullowney   /* Now the Transpose of the Upper Tri Factor */
804aa372e3fSPaul Mullowney   /*********************************************/
805aa372e3fSPaul Mullowney 
806aa372e3fSPaul Mullowney   /* allocate space for the transpose of the upper triangular factor */
807aa372e3fSPaul Mullowney   upTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct;
808aa372e3fSPaul Mullowney 
809aa372e3fSPaul Mullowney   /* set the matrix descriptors of the upper triangular factor */
810aa372e3fSPaul Mullowney   matrixType = cusparseGetMatType(upTriFactor->descr);
811aa372e3fSPaul Mullowney   indexBase = cusparseGetMatIndexBase(upTriFactor->descr);
812aa372e3fSPaul Mullowney   fillMode = cusparseGetMatFillMode(upTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ?
813aa372e3fSPaul Mullowney     CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER;
814aa372e3fSPaul Mullowney   diagType = cusparseGetMatDiagType(upTriFactor->descr);
815aa372e3fSPaul Mullowney 
816aa372e3fSPaul Mullowney   /* Create the matrix description */
81757d48284SJunchao Zhang   stat = cusparseCreateMatDescr(&upTriFactorT->descr);CHKERRCUSPARSE(stat);
81857d48284SJunchao Zhang   stat = cusparseSetMatIndexBase(upTriFactorT->descr, indexBase);CHKERRCUSPARSE(stat);
81957d48284SJunchao Zhang   stat = cusparseSetMatType(upTriFactorT->descr, matrixType);CHKERRCUSPARSE(stat);
82057d48284SJunchao Zhang   stat = cusparseSetMatFillMode(upTriFactorT->descr, fillMode);CHKERRCUSPARSE(stat);
82157d48284SJunchao Zhang   stat = cusparseSetMatDiagType(upTriFactorT->descr, diagType);CHKERRCUSPARSE(stat);
822aa372e3fSPaul Mullowney 
823aa372e3fSPaul Mullowney   /* Create the solve analysis information */
82457d48284SJunchao Zhang   stat = cusparseCreateSolveAnalysisInfo(&upTriFactorT->solveInfo);CHKERRCUSPARSE(stat);
825aa372e3fSPaul Mullowney 
826aa372e3fSPaul Mullowney   /* set the operation */
827aa372e3fSPaul Mullowney   upTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
828aa372e3fSPaul Mullowney 
829aa372e3fSPaul Mullowney   /* allocate GPU space for the CSC of the upper triangular factor*/
830aa372e3fSPaul Mullowney   upTriFactorT->csrMat = new CsrMatrix;
831aa372e3fSPaul Mullowney   upTriFactorT->csrMat->num_rows = upTriFactor->csrMat->num_rows;
832aa372e3fSPaul Mullowney   upTriFactorT->csrMat->num_cols = upTriFactor->csrMat->num_cols;
833aa372e3fSPaul Mullowney   upTriFactorT->csrMat->num_entries = upTriFactor->csrMat->num_entries;
834aa372e3fSPaul Mullowney   upTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(upTriFactor->csrMat->num_rows+1);
835aa372e3fSPaul Mullowney   upTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(upTriFactor->csrMat->num_entries);
836aa372e3fSPaul Mullowney   upTriFactorT->csrMat->values = new THRUSTARRAY(upTriFactor->csrMat->num_entries);
837aa372e3fSPaul Mullowney 
838aa372e3fSPaul Mullowney   /* compute the transpose of the upper triangular factor, i.e. the CSC */
839aa372e3fSPaul Mullowney   stat = cusparse_csr2csc(cusparseTriFactors->handle, upTriFactor->csrMat->num_rows,
840aa372e3fSPaul Mullowney                           upTriFactor->csrMat->num_cols, upTriFactor->csrMat->num_entries,
841aa372e3fSPaul Mullowney                           upTriFactor->csrMat->values->data().get(),
842aa372e3fSPaul Mullowney                           upTriFactor->csrMat->row_offsets->data().get(),
843aa372e3fSPaul Mullowney                           upTriFactor->csrMat->column_indices->data().get(),
844aa372e3fSPaul Mullowney                           upTriFactorT->csrMat->values->data().get(),
845aa372e3fSPaul Mullowney                           upTriFactorT->csrMat->column_indices->data().get(),
846aa372e3fSPaul Mullowney                           upTriFactorT->csrMat->row_offsets->data().get(),
84757d48284SJunchao Zhang                           CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUSPARSE(stat);
848aa372e3fSPaul Mullowney 
849aa372e3fSPaul Mullowney   /* perform the solve analysis on the transposed matrix */
850aa372e3fSPaul Mullowney   stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactorT->solveOp,
851aa372e3fSPaul Mullowney                            upTriFactorT->csrMat->num_rows, upTriFactorT->csrMat->num_entries,
852aa372e3fSPaul Mullowney                            upTriFactorT->descr, upTriFactorT->csrMat->values->data().get(),
853aa372e3fSPaul Mullowney                            upTriFactorT->csrMat->row_offsets->data().get(), upTriFactorT->csrMat->column_indices->data().get(),
85457d48284SJunchao Zhang                            upTriFactorT->solveInfo);CHKERRCUSPARSE(stat);
855aa372e3fSPaul Mullowney 
856aa372e3fSPaul Mullowney   /* assign the pointer. Is this really necessary? */
857aa372e3fSPaul Mullowney   ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtrTranspose = upTriFactorT;
858bda325fcSPaul Mullowney   PetscFunctionReturn(0);
859bda325fcSPaul Mullowney }
860bda325fcSPaul Mullowney 
861b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEGenerateTransposeForMult(Mat A)
862bda325fcSPaul Mullowney {
863aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE           *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
864aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat;
865aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
866bda325fcSPaul Mullowney   Mat_SeqAIJ                   *a = (Mat_SeqAIJ*)A->data;
867bda325fcSPaul Mullowney   cusparseStatus_t             stat;
868aa372e3fSPaul Mullowney   cusparseIndexBase_t          indexBase;
869b06137fdSPaul Mullowney   cudaError_t                  err;
8704863603aSSatish Balay   PetscErrorCode               ierr;
871b175d8bbSPaul Mullowney 
872bda325fcSPaul Mullowney   PetscFunctionBegin;
873aa372e3fSPaul Mullowney 
874aa372e3fSPaul Mullowney   /* allocate space for the triangular factor information */
875aa372e3fSPaul Mullowney   matstructT = new Mat_SeqAIJCUSPARSEMultStruct;
87657d48284SJunchao Zhang   stat = cusparseCreateMatDescr(&matstructT->descr);CHKERRCUSPARSE(stat);
877aa372e3fSPaul Mullowney   indexBase = cusparseGetMatIndexBase(matstruct->descr);
87857d48284SJunchao Zhang   stat = cusparseSetMatIndexBase(matstructT->descr, indexBase);CHKERRCUSPARSE(stat);
87957d48284SJunchao Zhang   stat = cusparseSetMatType(matstructT->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUSPARSE(stat);
880aa372e3fSPaul Mullowney 
881b06137fdSPaul Mullowney   /* set alpha and beta */
882c41cb2e2SAlejandro Lamas Daviña   err = cudaMalloc((void **)&(matstructT->alpha),    sizeof(PetscScalar));CHKERRCUDA(err);
8837656d835SStefano Zampini   err = cudaMalloc((void **)&(matstructT->beta_zero),sizeof(PetscScalar));CHKERRCUDA(err);
8847656d835SStefano Zampini   err = cudaMalloc((void **)&(matstructT->beta_one), sizeof(PetscScalar));CHKERRCUDA(err);
8857656d835SStefano Zampini   err = cudaMemcpy(matstructT->alpha,    &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
8867656d835SStefano Zampini   err = cudaMemcpy(matstructT->beta_zero,&PETSC_CUSPARSE_ZERO,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
8877656d835SStefano Zampini   err = cudaMemcpy(matstructT->beta_one, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
88857d48284SJunchao Zhang   stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUSPARSE(stat);
889b06137fdSPaul Mullowney 
890aa372e3fSPaul Mullowney   if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
891aa372e3fSPaul Mullowney     CsrMatrix *matrix = (CsrMatrix*)matstruct->mat;
892aa372e3fSPaul Mullowney     CsrMatrix *matrixT= new CsrMatrix;
893554b8892SKarl Rupp     matrixT->num_rows = A->cmap->n;
894554b8892SKarl Rupp     matrixT->num_cols = A->rmap->n;
895aa372e3fSPaul Mullowney     matrixT->num_entries = a->nz;
896a8bd5306SMark Adams     matrixT->row_offsets = new THRUSTINTARRAY32(matrixT->num_rows+1);
897aa372e3fSPaul Mullowney     matrixT->column_indices = new THRUSTINTARRAY32(a->nz);
898aa372e3fSPaul Mullowney     matrixT->values = new THRUSTARRAY(a->nz);
899a3fdcf43SKarl Rupp 
90081902715SJunchao Zhang     cusparsestruct->rowoffsets_gpu = new THRUSTINTARRAY32(A->rmap->n+1);
90181902715SJunchao Zhang     cusparsestruct->rowoffsets_gpu->assign(a->i,a->i+A->rmap->n+1);
90281902715SJunchao Zhang     /* compute the transpose, i.e. the CSC */
903aa372e3fSPaul Mullowney     indexBase = cusparseGetMatIndexBase(matstruct->descr);
904a3fdcf43SKarl Rupp     stat = cusparse_csr2csc(cusparsestruct->handle, A->rmap->n,
905a3fdcf43SKarl Rupp                             A->cmap->n, matrix->num_entries,
906aa372e3fSPaul Mullowney                             matrix->values->data().get(),
90781902715SJunchao Zhang                             cusparsestruct->rowoffsets_gpu->data().get(),
908aa372e3fSPaul Mullowney                             matrix->column_indices->data().get(),
909aa372e3fSPaul Mullowney                             matrixT->values->data().get(),
910aa372e3fSPaul Mullowney                             matrixT->column_indices->data().get(),
911aa372e3fSPaul Mullowney                             matrixT->row_offsets->data().get(),
91257d48284SJunchao Zhang                             CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUSPARSE(stat);
913aa372e3fSPaul Mullowney     /* assign the pointer */
914aa372e3fSPaul Mullowney     matstructT->mat = matrixT;
9154863603aSSatish Balay     ierr = PetscLogCpuToGpu(((A->rmap->n+1)+(a->nz))*sizeof(int)+(3+a->nz)*sizeof(PetscScalar));CHKERRQ(ierr);
916aa372e3fSPaul Mullowney   } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) {
917aa372e3fSPaul Mullowney     /* First convert HYB to CSR */
918aa372e3fSPaul Mullowney     CsrMatrix *temp= new CsrMatrix;
919aa372e3fSPaul Mullowney     temp->num_rows = A->rmap->n;
920aa372e3fSPaul Mullowney     temp->num_cols = A->cmap->n;
921aa372e3fSPaul Mullowney     temp->num_entries = a->nz;
922aa372e3fSPaul Mullowney     temp->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
923aa372e3fSPaul Mullowney     temp->column_indices = new THRUSTINTARRAY32(a->nz);
924aa372e3fSPaul Mullowney     temp->values = new THRUSTARRAY(a->nz);
925aa372e3fSPaul Mullowney 
9262692e278SPaul Mullowney 
927aa372e3fSPaul Mullowney     stat = cusparse_hyb2csr(cusparsestruct->handle,
928aa372e3fSPaul Mullowney                             matstruct->descr, (cusparseHybMat_t)matstruct->mat,
929aa372e3fSPaul Mullowney                             temp->values->data().get(),
930aa372e3fSPaul Mullowney                             temp->row_offsets->data().get(),
93157d48284SJunchao Zhang                             temp->column_indices->data().get());CHKERRCUSPARSE(stat);
932aa372e3fSPaul Mullowney 
933aa372e3fSPaul Mullowney     /* Next, convert CSR to CSC (i.e. the matrix transpose) */
934aa372e3fSPaul Mullowney     CsrMatrix *tempT= new CsrMatrix;
935aa372e3fSPaul Mullowney     tempT->num_rows = A->rmap->n;
936aa372e3fSPaul Mullowney     tempT->num_cols = A->cmap->n;
937aa372e3fSPaul Mullowney     tempT->num_entries = a->nz;
938aa372e3fSPaul Mullowney     tempT->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
939aa372e3fSPaul Mullowney     tempT->column_indices = new THRUSTINTARRAY32(a->nz);
940aa372e3fSPaul Mullowney     tempT->values = new THRUSTARRAY(a->nz);
941aa372e3fSPaul Mullowney 
942aa372e3fSPaul Mullowney     stat = cusparse_csr2csc(cusparsestruct->handle, temp->num_rows,
943aa372e3fSPaul Mullowney                             temp->num_cols, temp->num_entries,
944aa372e3fSPaul Mullowney                             temp->values->data().get(),
945aa372e3fSPaul Mullowney                             temp->row_offsets->data().get(),
946aa372e3fSPaul Mullowney                             temp->column_indices->data().get(),
947aa372e3fSPaul Mullowney                             tempT->values->data().get(),
948aa372e3fSPaul Mullowney                             tempT->column_indices->data().get(),
949aa372e3fSPaul Mullowney                             tempT->row_offsets->data().get(),
95057d48284SJunchao Zhang                             CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUSPARSE(stat);
951aa372e3fSPaul Mullowney 
952aa372e3fSPaul Mullowney     /* Last, convert CSC to HYB */
953aa372e3fSPaul Mullowney     cusparseHybMat_t hybMat;
95457d48284SJunchao Zhang     stat = cusparseCreateHybMat(&hybMat);CHKERRCUSPARSE(stat);
955aa372e3fSPaul Mullowney     cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ?
956aa372e3fSPaul Mullowney       CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO;
957aa372e3fSPaul Mullowney     stat = cusparse_csr2hyb(cusparsestruct->handle, A->rmap->n, A->cmap->n,
958aa372e3fSPaul Mullowney                             matstructT->descr, tempT->values->data().get(),
959aa372e3fSPaul Mullowney                             tempT->row_offsets->data().get(),
960aa372e3fSPaul Mullowney                             tempT->column_indices->data().get(),
96157d48284SJunchao Zhang                             hybMat, 0, partition);CHKERRCUSPARSE(stat);
962aa372e3fSPaul Mullowney 
963aa372e3fSPaul Mullowney     /* assign the pointer */
964aa372e3fSPaul Mullowney     matstructT->mat = hybMat;
9654863603aSSatish Balay     ierr = PetscLogCpuToGpu((2*(((A->rmap->n+1)+(a->nz))*sizeof(int)+(a->nz)*sizeof(PetscScalar)))+3*sizeof(PetscScalar));CHKERRQ(ierr);
966aa372e3fSPaul Mullowney 
967aa372e3fSPaul Mullowney     /* delete temporaries */
968aa372e3fSPaul Mullowney     if (tempT) {
969aa372e3fSPaul Mullowney       if (tempT->values) delete (THRUSTARRAY*) tempT->values;
970aa372e3fSPaul Mullowney       if (tempT->column_indices) delete (THRUSTINTARRAY32*) tempT->column_indices;
971aa372e3fSPaul Mullowney       if (tempT->row_offsets) delete (THRUSTINTARRAY32*) tempT->row_offsets;
972aa372e3fSPaul Mullowney       delete (CsrMatrix*) tempT;
973087f3262SPaul Mullowney     }
974aa372e3fSPaul Mullowney     if (temp) {
975aa372e3fSPaul Mullowney       if (temp->values) delete (THRUSTARRAY*) temp->values;
976aa372e3fSPaul Mullowney       if (temp->column_indices) delete (THRUSTINTARRAY32*) temp->column_indices;
977aa372e3fSPaul Mullowney       if (temp->row_offsets) delete (THRUSTINTARRAY32*) temp->row_offsets;
978aa372e3fSPaul Mullowney       delete (CsrMatrix*) temp;
979aa372e3fSPaul Mullowney     }
980aa372e3fSPaul Mullowney   }
981aa372e3fSPaul Mullowney   /* assign the compressed row indices */
982aa372e3fSPaul Mullowney   matstructT->cprowIndices = new THRUSTINTARRAY;
983554b8892SKarl Rupp   matstructT->cprowIndices->resize(A->cmap->n);
984554b8892SKarl Rupp   thrust::sequence(matstructT->cprowIndices->begin(), matstructT->cprowIndices->end());
985aa372e3fSPaul Mullowney   /* assign the pointer */
986aa372e3fSPaul Mullowney   ((Mat_SeqAIJCUSPARSE*)A->spptr)->matTranspose = matstructT;
987bda325fcSPaul Mullowney   PetscFunctionReturn(0);
988bda325fcSPaul Mullowney }
989bda325fcSPaul Mullowney 
9904e4bbfaaSStefano Zampini /* Why do we need to analyze the tranposed matrix again? Can't we just use op(A) = CUSPARSE_OPERATION_TRANSPOSE in MatSolve_SeqAIJCUSPARSE? */
9916fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx)
992bda325fcSPaul Mullowney {
993c41cb2e2SAlejandro Lamas Daviña   PetscInt                              n = xx->map->n;
994465f34aeSAlejandro Lamas Daviña   const PetscScalar                     *barray;
995465f34aeSAlejandro Lamas Daviña   PetscScalar                           *xarray;
996465f34aeSAlejandro Lamas Daviña   thrust::device_ptr<const PetscScalar> bGPU;
997465f34aeSAlejandro Lamas Daviña   thrust::device_ptr<PetscScalar>       xGPU;
998bda325fcSPaul Mullowney   cusparseStatus_t                      stat;
999bda325fcSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors          *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
1000aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct     *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
1001aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct     *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
1002aa372e3fSPaul Mullowney   THRUSTARRAY                           *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector;
1003b175d8bbSPaul Mullowney   PetscErrorCode                        ierr;
100457d48284SJunchao Zhang   cudaError_t                           cerr;
1005bda325fcSPaul Mullowney 
1006bda325fcSPaul Mullowney   PetscFunctionBegin;
1007aa372e3fSPaul Mullowney   /* Analyze the matrix and create the transpose ... on the fly */
1008aa372e3fSPaul Mullowney   if (!loTriFactorT && !upTriFactorT) {
1009bda325fcSPaul Mullowney     ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr);
1010aa372e3fSPaul Mullowney     loTriFactorT       = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
1011aa372e3fSPaul Mullowney     upTriFactorT       = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
1012bda325fcSPaul Mullowney   }
1013bda325fcSPaul Mullowney 
1014bda325fcSPaul Mullowney   /* Get the GPU pointers */
1015c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr);
1016c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr);
1017c41cb2e2SAlejandro Lamas Daviña   xGPU = thrust::device_pointer_cast(xarray);
1018c41cb2e2SAlejandro Lamas Daviña   bGPU = thrust::device_pointer_cast(barray);
1019bda325fcSPaul Mullowney 
10207a052e47Shannah_mairs   ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
1021aa372e3fSPaul Mullowney   /* First, reorder with the row permutation */
1022c41cb2e2SAlejandro Lamas Daviña   thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()),
1023c41cb2e2SAlejandro Lamas Daviña                thrust::make_permutation_iterator(bGPU+n, cusparseTriFactors->rpermIndices->end()),
1024c41cb2e2SAlejandro Lamas Daviña                xGPU);
1025aa372e3fSPaul Mullowney 
1026aa372e3fSPaul Mullowney   /* First, solve U */
1027aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp,
10287656d835SStefano Zampini                         upTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactorT->descr,
1029aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->values->data().get(),
1030aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->row_offsets->data().get(),
1031aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->column_indices->data().get(),
1032aa372e3fSPaul Mullowney                         upTriFactorT->solveInfo,
103357d48284SJunchao Zhang                         xarray, tempGPU->data().get());CHKERRCUSPARSE(stat);
1034aa372e3fSPaul Mullowney 
1035aa372e3fSPaul Mullowney   /* Then, solve L */
1036aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp,
10377656d835SStefano Zampini                         loTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactorT->descr,
1038aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->values->data().get(),
1039aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->row_offsets->data().get(),
1040aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->column_indices->data().get(),
1041aa372e3fSPaul Mullowney                         loTriFactorT->solveInfo,
104257d48284SJunchao Zhang                         tempGPU->data().get(), xarray);CHKERRCUSPARSE(stat);
1043aa372e3fSPaul Mullowney 
1044aa372e3fSPaul Mullowney   /* Last, copy the solution, xGPU, into a temporary with the column permutation ... can't be done in place. */
1045c41cb2e2SAlejandro Lamas Daviña   thrust::copy(thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->begin()),
1046c41cb2e2SAlejandro Lamas Daviña                thrust::make_permutation_iterator(xGPU+n, cusparseTriFactors->cpermIndices->end()),
1047aa372e3fSPaul Mullowney                tempGPU->begin());
1048aa372e3fSPaul Mullowney 
1049aa372e3fSPaul Mullowney   /* Copy the temporary to the full solution. */
1050c41cb2e2SAlejandro Lamas Daviña   thrust::copy(tempGPU->begin(), tempGPU->end(), xGPU);
1051bda325fcSPaul Mullowney 
1052bda325fcSPaul Mullowney   /* restore */
1053c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr);
1054c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr);
105557d48284SJunchao Zhang   cerr = WaitForGPU();CHKERRCUDA(cerr);
1056661c2d29Shannah_mairs   ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1057958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr);
1058bda325fcSPaul Mullowney   PetscFunctionReturn(0);
1059bda325fcSPaul Mullowney }
1060bda325fcSPaul Mullowney 
10616fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx)
1062bda325fcSPaul Mullowney {
1063465f34aeSAlejandro Lamas Daviña   const PetscScalar                 *barray;
1064465f34aeSAlejandro Lamas Daviña   PetscScalar                       *xarray;
1065bda325fcSPaul Mullowney   cusparseStatus_t                  stat;
1066bda325fcSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
1067aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
1068aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
1069aa372e3fSPaul Mullowney   THRUSTARRAY                       *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector;
1070b175d8bbSPaul Mullowney   PetscErrorCode                    ierr;
107157d48284SJunchao Zhang   cudaError_t                       cerr;
1072bda325fcSPaul Mullowney 
1073bda325fcSPaul Mullowney   PetscFunctionBegin;
1074aa372e3fSPaul Mullowney   /* Analyze the matrix and create the transpose ... on the fly */
1075aa372e3fSPaul Mullowney   if (!loTriFactorT && !upTriFactorT) {
1076bda325fcSPaul Mullowney     ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr);
1077aa372e3fSPaul Mullowney     loTriFactorT       = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
1078aa372e3fSPaul Mullowney     upTriFactorT       = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
1079bda325fcSPaul Mullowney   }
1080bda325fcSPaul Mullowney 
1081bda325fcSPaul Mullowney   /* Get the GPU pointers */
1082c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr);
1083c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr);
1084bda325fcSPaul Mullowney 
10857a052e47Shannah_mairs   ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
1086aa372e3fSPaul Mullowney   /* First, solve U */
1087aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp,
10887656d835SStefano Zampini                         upTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactorT->descr,
1089aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->values->data().get(),
1090aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->row_offsets->data().get(),
1091aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->column_indices->data().get(),
1092aa372e3fSPaul Mullowney                         upTriFactorT->solveInfo,
109357d48284SJunchao Zhang                         barray, tempGPU->data().get());CHKERRCUSPARSE(stat);
1094aa372e3fSPaul Mullowney 
1095aa372e3fSPaul Mullowney   /* Then, solve L */
1096aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp,
10977656d835SStefano Zampini                         loTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactorT->descr,
1098aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->values->data().get(),
1099aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->row_offsets->data().get(),
1100aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->column_indices->data().get(),
1101aa372e3fSPaul Mullowney                         loTriFactorT->solveInfo,
110257d48284SJunchao Zhang                         tempGPU->data().get(), xarray);CHKERRCUSPARSE(stat);
1103bda325fcSPaul Mullowney 
1104bda325fcSPaul Mullowney   /* restore */
1105c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr);
1106c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr);
110757d48284SJunchao Zhang   cerr = WaitForGPU();CHKERRCUDA(cerr);
1108661c2d29Shannah_mairs   ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1109958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr);
1110bda325fcSPaul Mullowney   PetscFunctionReturn(0);
1111bda325fcSPaul Mullowney }
1112bda325fcSPaul Mullowney 
11136fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx)
11149ae82921SPaul Mullowney {
1115465f34aeSAlejandro Lamas Daviña   const PetscScalar                     *barray;
1116465f34aeSAlejandro Lamas Daviña   PetscScalar                           *xarray;
1117465f34aeSAlejandro Lamas Daviña   thrust::device_ptr<const PetscScalar> bGPU;
1118465f34aeSAlejandro Lamas Daviña   thrust::device_ptr<PetscScalar>       xGPU;
11199ae82921SPaul Mullowney   cusparseStatus_t                      stat;
11209ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors          *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
1121aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct     *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
1122aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct     *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
1123aa372e3fSPaul Mullowney   THRUSTARRAY                           *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector;
1124b175d8bbSPaul Mullowney   PetscErrorCode                        ierr;
112557d48284SJunchao Zhang   cudaError_t                           cerr;
11269ae82921SPaul Mullowney 
11279ae82921SPaul Mullowney   PetscFunctionBegin;
1128ebc8f436SDominic Meiser 
1129e057df02SPaul Mullowney   /* Get the GPU pointers */
1130c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr);
1131c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr);
1132c41cb2e2SAlejandro Lamas Daviña   xGPU = thrust::device_pointer_cast(xarray);
1133c41cb2e2SAlejandro Lamas Daviña   bGPU = thrust::device_pointer_cast(barray);
11349ae82921SPaul Mullowney 
11357a052e47Shannah_mairs   ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
1136aa372e3fSPaul Mullowney   /* First, reorder with the row permutation */
1137c41cb2e2SAlejandro Lamas Daviña   thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()),
1138c41cb2e2SAlejandro Lamas Daviña                thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->end()),
11394e4bbfaaSStefano Zampini                tempGPU->begin());
1140aa372e3fSPaul Mullowney 
1141aa372e3fSPaul Mullowney   /* Next, solve L */
1142aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp,
11437656d835SStefano Zampini                         loTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactor->descr,
1144aa372e3fSPaul Mullowney                         loTriFactor->csrMat->values->data().get(),
1145aa372e3fSPaul Mullowney                         loTriFactor->csrMat->row_offsets->data().get(),
1146aa372e3fSPaul Mullowney                         loTriFactor->csrMat->column_indices->data().get(),
1147aa372e3fSPaul Mullowney                         loTriFactor->solveInfo,
114857d48284SJunchao Zhang                         tempGPU->data().get(), xarray);CHKERRCUSPARSE(stat);
1149aa372e3fSPaul Mullowney 
1150aa372e3fSPaul Mullowney   /* Then, solve U */
1151aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp,
11527656d835SStefano Zampini                         upTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactor->descr,
1153aa372e3fSPaul Mullowney                         upTriFactor->csrMat->values->data().get(),
1154aa372e3fSPaul Mullowney                         upTriFactor->csrMat->row_offsets->data().get(),
1155aa372e3fSPaul Mullowney                         upTriFactor->csrMat->column_indices->data().get(),
1156aa372e3fSPaul Mullowney                         upTriFactor->solveInfo,
115757d48284SJunchao Zhang                         xarray, tempGPU->data().get());CHKERRCUSPARSE(stat);
1158aa372e3fSPaul Mullowney 
11594e4bbfaaSStefano Zampini   /* Last, reorder with the column permutation */
11604e4bbfaaSStefano Zampini   thrust::copy(thrust::make_permutation_iterator(tempGPU->begin(), cusparseTriFactors->cpermIndices->begin()),
11614e4bbfaaSStefano Zampini                thrust::make_permutation_iterator(tempGPU->begin(), cusparseTriFactors->cpermIndices->end()),
11624e4bbfaaSStefano Zampini                xGPU);
11639ae82921SPaul Mullowney 
1164c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr);
1165c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr);
116657d48284SJunchao Zhang   cerr = WaitForGPU();CHKERRCUDA(cerr);
1167661c2d29Shannah_mairs   ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1168958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr);
11699ae82921SPaul Mullowney   PetscFunctionReturn(0);
11709ae82921SPaul Mullowney }
11719ae82921SPaul Mullowney 
11726fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx)
11739ae82921SPaul Mullowney {
1174465f34aeSAlejandro Lamas Daviña   const PetscScalar                 *barray;
1175465f34aeSAlejandro Lamas Daviña   PetscScalar                       *xarray;
11769ae82921SPaul Mullowney   cusparseStatus_t                  stat;
11779ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
1178aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
1179aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
1180aa372e3fSPaul Mullowney   THRUSTARRAY                       *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector;
1181b175d8bbSPaul Mullowney   PetscErrorCode                    ierr;
118257d48284SJunchao Zhang   cudaError_t                       cerr;
11839ae82921SPaul Mullowney 
11849ae82921SPaul Mullowney   PetscFunctionBegin;
1185e057df02SPaul Mullowney   /* Get the GPU pointers */
1186c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr);
1187c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr);
11889ae82921SPaul Mullowney 
11897a052e47Shannah_mairs   ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
1190aa372e3fSPaul Mullowney   /* First, solve L */
1191aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp,
11927656d835SStefano Zampini                         loTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactor->descr,
1193aa372e3fSPaul Mullowney                         loTriFactor->csrMat->values->data().get(),
1194aa372e3fSPaul Mullowney                         loTriFactor->csrMat->row_offsets->data().get(),
1195aa372e3fSPaul Mullowney                         loTriFactor->csrMat->column_indices->data().get(),
1196aa372e3fSPaul Mullowney                         loTriFactor->solveInfo,
119757d48284SJunchao Zhang                         barray, tempGPU->data().get());CHKERRCUSPARSE(stat);
1198aa372e3fSPaul Mullowney 
1199aa372e3fSPaul Mullowney   /* Next, solve U */
1200aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp,
12017656d835SStefano Zampini                         upTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactor->descr,
1202aa372e3fSPaul Mullowney                         upTriFactor->csrMat->values->data().get(),
1203aa372e3fSPaul Mullowney                         upTriFactor->csrMat->row_offsets->data().get(),
1204aa372e3fSPaul Mullowney                         upTriFactor->csrMat->column_indices->data().get(),
1205aa372e3fSPaul Mullowney                         upTriFactor->solveInfo,
120657d48284SJunchao Zhang                         tempGPU->data().get(), xarray);CHKERRCUSPARSE(stat);
12079ae82921SPaul Mullowney 
1208c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr);
1209c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr);
121057d48284SJunchao Zhang   cerr = WaitForGPU();CHKERRCUDA(cerr);
1211661c2d29Shannah_mairs   ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1212958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr);
12139ae82921SPaul Mullowney   PetscFunctionReturn(0);
12149ae82921SPaul Mullowney }
12159ae82921SPaul Mullowney 
12166fa9248bSJed Brown static PetscErrorCode MatSeqAIJCUSPARSECopyToGPU(Mat A)
12179ae82921SPaul Mullowney {
1218aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE           *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
12197c700b8dSJunchao Zhang   Mat_SeqAIJCUSPARSEMultStruct *matstruct = cusparsestruct->mat;
12209ae82921SPaul Mullowney   Mat_SeqAIJ                   *a = (Mat_SeqAIJ*)A->data;
12219ae82921SPaul Mullowney   PetscInt                     m = A->rmap->n,*ii,*ridx;
12229ae82921SPaul Mullowney   PetscErrorCode               ierr;
1223aa372e3fSPaul Mullowney   cusparseStatus_t             stat;
1224b06137fdSPaul Mullowney   cudaError_t                  err;
12259ae82921SPaul Mullowney 
12269ae82921SPaul Mullowney   PetscFunctionBegin;
122795639643SRichard Tran Mills   if (A->boundtocpu) PetscFunctionReturn(0);
1228c70f7ee4SJunchao Zhang   if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) {
12299ae82921SPaul Mullowney     ierr = PetscLogEventBegin(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr);
123081902715SJunchao Zhang     if (A->was_assembled && A->nonzerostate == cusparsestruct->nonzerostate && cusparsestruct->format == MAT_CUSPARSE_CSR) {
123181902715SJunchao Zhang       /* Copy values only */
123281902715SJunchao Zhang       CsrMatrix *mat,*matT;
123381902715SJunchao Zhang       mat  = (CsrMatrix*)cusparsestruct->mat->mat;
123481902715SJunchao Zhang       mat->values->assign(a->a, a->a+a->nz);
12354863603aSSatish Balay       ierr = PetscLogCpuToGpu((a->nz)*sizeof(PetscScalar));CHKERRQ(ierr);
123681902715SJunchao Zhang 
123781902715SJunchao Zhang       /* Update matT when it was built before */
123881902715SJunchao Zhang       if (cusparsestruct->matTranspose) {
123981902715SJunchao Zhang         cusparseIndexBase_t indexBase = cusparseGetMatIndexBase(cusparsestruct->mat->descr);
124081902715SJunchao Zhang         matT = (CsrMatrix*)cusparsestruct->matTranspose->mat;
124181902715SJunchao Zhang         stat = cusparse_csr2csc(cusparsestruct->handle, A->rmap->n,
124281902715SJunchao Zhang                                  A->cmap->n, mat->num_entries,
124381902715SJunchao Zhang                                  mat->values->data().get(),
124481902715SJunchao Zhang                                  cusparsestruct->rowoffsets_gpu->data().get(),
124581902715SJunchao Zhang                                  mat->column_indices->data().get(),
124681902715SJunchao Zhang                                  matT->values->data().get(),
124781902715SJunchao Zhang                                  matT->column_indices->data().get(),
124881902715SJunchao Zhang                                  matT->row_offsets->data().get(),
12499f74ec24SSatish Balay                                  CUSPARSE_ACTION_NUMERIC,indexBase);CHKERRCUSPARSE(stat);
125081902715SJunchao Zhang       }
125134d6c7a5SJose E. Roman     } else {
12527c700b8dSJunchao Zhang       ierr = MatSeqAIJCUSPARSEMultStruct_Destroy(&cusparsestruct->mat,cusparsestruct->format);CHKERRQ(ierr);
12537c700b8dSJunchao Zhang       ierr = MatSeqAIJCUSPARSEMultStruct_Destroy(&cusparsestruct->matTranspose,cusparsestruct->format);CHKERRQ(ierr);
12547c700b8dSJunchao Zhang       delete cusparsestruct->workVector;
125581902715SJunchao Zhang       delete cusparsestruct->rowoffsets_gpu;
12569ae82921SPaul Mullowney       try {
1257aa372e3fSPaul Mullowney         cusparsestruct->nonzerorow=0;
1258aa372e3fSPaul Mullowney         for (int j = 0; j<m; j++) cusparsestruct->nonzerorow += ((a->i[j+1]-a->i[j])>0);
12599ae82921SPaul Mullowney 
12609ae82921SPaul Mullowney         if (a->compressedrow.use) {
12619ae82921SPaul Mullowney           m    = a->compressedrow.nrows;
12629ae82921SPaul Mullowney           ii   = a->compressedrow.i;
12639ae82921SPaul Mullowney           ridx = a->compressedrow.rindex;
12649ae82921SPaul Mullowney         } else {
1265b06137fdSPaul Mullowney           /* Forcing compressed row on the GPU */
12669ae82921SPaul Mullowney           int k=0;
1267854ce69bSBarry Smith           ierr = PetscMalloc1(cusparsestruct->nonzerorow+1, &ii);CHKERRQ(ierr);
1268854ce69bSBarry Smith           ierr = PetscMalloc1(cusparsestruct->nonzerorow, &ridx);CHKERRQ(ierr);
12699ae82921SPaul Mullowney           ii[0]=0;
12709ae82921SPaul Mullowney           for (int j = 0; j<m; j++) {
12719ae82921SPaul Mullowney             if ((a->i[j+1]-a->i[j])>0) {
12729ae82921SPaul Mullowney               ii[k]  = a->i[j];
12739ae82921SPaul Mullowney               ridx[k]= j;
12749ae82921SPaul Mullowney               k++;
12759ae82921SPaul Mullowney             }
12769ae82921SPaul Mullowney           }
1277aa372e3fSPaul Mullowney           ii[cusparsestruct->nonzerorow] = a->nz;
1278aa372e3fSPaul Mullowney           m = cusparsestruct->nonzerorow;
12799ae82921SPaul Mullowney         }
12809ae82921SPaul Mullowney 
1281aa372e3fSPaul Mullowney         /* allocate space for the triangular factor information */
1282aa372e3fSPaul Mullowney         matstruct = new Mat_SeqAIJCUSPARSEMultStruct;
128357d48284SJunchao Zhang         stat = cusparseCreateMatDescr(&matstruct->descr);CHKERRCUSPARSE(stat);
128457d48284SJunchao Zhang         stat = cusparseSetMatIndexBase(matstruct->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUSPARSE(stat);
128557d48284SJunchao Zhang         stat = cusparseSetMatType(matstruct->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUSPARSE(stat);
12869ae82921SPaul Mullowney 
1287c41cb2e2SAlejandro Lamas Daviña         err = cudaMalloc((void **)&(matstruct->alpha),    sizeof(PetscScalar));CHKERRCUDA(err);
12887656d835SStefano Zampini         err = cudaMalloc((void **)&(matstruct->beta_zero),sizeof(PetscScalar));CHKERRCUDA(err);
12897656d835SStefano Zampini         err = cudaMalloc((void **)&(matstruct->beta_one), sizeof(PetscScalar));CHKERRCUDA(err);
12907656d835SStefano Zampini         err = cudaMemcpy(matstruct->alpha,    &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
12917656d835SStefano Zampini         err = cudaMemcpy(matstruct->beta_zero,&PETSC_CUSPARSE_ZERO,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
12927656d835SStefano Zampini         err = cudaMemcpy(matstruct->beta_one, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
129357d48284SJunchao Zhang         stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUSPARSE(stat);
1294b06137fdSPaul Mullowney 
1295aa372e3fSPaul Mullowney         /* Build a hybrid/ellpack matrix if this option is chosen for the storage */
1296aa372e3fSPaul Mullowney         if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
1297aa372e3fSPaul Mullowney           /* set the matrix */
1298aa372e3fSPaul Mullowney           CsrMatrix *matrix= new CsrMatrix;
1299a65300a6SPaul Mullowney           matrix->num_rows = m;
1300aa372e3fSPaul Mullowney           matrix->num_cols = A->cmap->n;
1301aa372e3fSPaul Mullowney           matrix->num_entries = a->nz;
1302a65300a6SPaul Mullowney           matrix->row_offsets = new THRUSTINTARRAY32(m+1);
1303a65300a6SPaul Mullowney           matrix->row_offsets->assign(ii, ii + m+1);
13049ae82921SPaul Mullowney 
1305aa372e3fSPaul Mullowney           matrix->column_indices = new THRUSTINTARRAY32(a->nz);
1306aa372e3fSPaul Mullowney           matrix->column_indices->assign(a->j, a->j+a->nz);
1307aa372e3fSPaul Mullowney 
1308aa372e3fSPaul Mullowney           matrix->values = new THRUSTARRAY(a->nz);
1309aa372e3fSPaul Mullowney           matrix->values->assign(a->a, a->a+a->nz);
1310aa372e3fSPaul Mullowney 
1311aa372e3fSPaul Mullowney           /* assign the pointer */
1312aa372e3fSPaul Mullowney           matstruct->mat = matrix;
1313aa372e3fSPaul Mullowney 
1314aa372e3fSPaul Mullowney         } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) {
1315aa372e3fSPaul Mullowney           CsrMatrix *matrix= new CsrMatrix;
1316a65300a6SPaul Mullowney           matrix->num_rows = m;
1317aa372e3fSPaul Mullowney           matrix->num_cols = A->cmap->n;
1318aa372e3fSPaul Mullowney           matrix->num_entries = a->nz;
1319a65300a6SPaul Mullowney           matrix->row_offsets = new THRUSTINTARRAY32(m+1);
1320a65300a6SPaul Mullowney           matrix->row_offsets->assign(ii, ii + m+1);
1321aa372e3fSPaul Mullowney 
1322aa372e3fSPaul Mullowney           matrix->column_indices = new THRUSTINTARRAY32(a->nz);
1323aa372e3fSPaul Mullowney           matrix->column_indices->assign(a->j, a->j+a->nz);
1324aa372e3fSPaul Mullowney 
1325aa372e3fSPaul Mullowney           matrix->values = new THRUSTARRAY(a->nz);
1326aa372e3fSPaul Mullowney           matrix->values->assign(a->a, a->a+a->nz);
1327aa372e3fSPaul Mullowney 
1328aa372e3fSPaul Mullowney           cusparseHybMat_t hybMat;
132957d48284SJunchao Zhang           stat = cusparseCreateHybMat(&hybMat);CHKERRCUSPARSE(stat);
1330aa372e3fSPaul Mullowney           cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ?
1331aa372e3fSPaul Mullowney             CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO;
1332a65300a6SPaul Mullowney           stat = cusparse_csr2hyb(cusparsestruct->handle, matrix->num_rows, matrix->num_cols,
1333aa372e3fSPaul Mullowney               matstruct->descr, matrix->values->data().get(),
1334aa372e3fSPaul Mullowney               matrix->row_offsets->data().get(),
1335aa372e3fSPaul Mullowney               matrix->column_indices->data().get(),
133657d48284SJunchao Zhang               hybMat, 0, partition);CHKERRCUSPARSE(stat);
1337aa372e3fSPaul Mullowney           /* assign the pointer */
1338aa372e3fSPaul Mullowney           matstruct->mat = hybMat;
1339aa372e3fSPaul Mullowney 
1340aa372e3fSPaul Mullowney           if (matrix) {
1341aa372e3fSPaul Mullowney             if (matrix->values) delete (THRUSTARRAY*)matrix->values;
1342aa372e3fSPaul Mullowney             if (matrix->column_indices) delete (THRUSTINTARRAY32*)matrix->column_indices;
1343aa372e3fSPaul Mullowney             if (matrix->row_offsets) delete (THRUSTINTARRAY32*)matrix->row_offsets;
1344aa372e3fSPaul Mullowney             delete (CsrMatrix*)matrix;
1345087f3262SPaul Mullowney           }
1346087f3262SPaul Mullowney         }
1347ca45077fSPaul Mullowney 
1348aa372e3fSPaul Mullowney         /* assign the compressed row indices */
1349aa372e3fSPaul Mullowney         matstruct->cprowIndices = new THRUSTINTARRAY(m);
1350aa372e3fSPaul Mullowney         matstruct->cprowIndices->assign(ridx,ridx+m);
13514863603aSSatish Balay         ierr = PetscLogCpuToGpu(((m+1)+(a->nz))*sizeof(int)+m*sizeof(PetscInt)+(3+(a->nz))*sizeof(PetscScalar));CHKERRQ(ierr);
1352aa372e3fSPaul Mullowney 
1353aa372e3fSPaul Mullowney         /* assign the pointer */
1354aa372e3fSPaul Mullowney         cusparsestruct->mat = matstruct;
1355aa372e3fSPaul Mullowney 
13569ae82921SPaul Mullowney         if (!a->compressedrow.use) {
13579ae82921SPaul Mullowney           ierr = PetscFree(ii);CHKERRQ(ierr);
13589ae82921SPaul Mullowney           ierr = PetscFree(ridx);CHKERRQ(ierr);
13599ae82921SPaul Mullowney         }
1360e65717acSKarl Rupp         cusparsestruct->workVector = new THRUSTARRAY(m);
13619ae82921SPaul Mullowney       } catch(char *ex) {
13629ae82921SPaul Mullowney         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
13639ae82921SPaul Mullowney       }
136434d6c7a5SJose E. Roman       cusparsestruct->nonzerostate = A->nonzerostate;
136534d6c7a5SJose E. Roman     }
136657d48284SJunchao Zhang     err  = WaitForGPU();CHKERRCUDA(err);
1367c70f7ee4SJunchao Zhang     A->offloadmask = PETSC_OFFLOAD_BOTH;
13689ae82921SPaul Mullowney     ierr = PetscLogEventEnd(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr);
13699ae82921SPaul Mullowney   }
13709ae82921SPaul Mullowney   PetscFunctionReturn(0);
13719ae82921SPaul Mullowney }
13729ae82921SPaul Mullowney 
1373c41cb2e2SAlejandro Lamas Daviña struct VecCUDAPlusEquals
1374aa372e3fSPaul Mullowney {
1375aa372e3fSPaul Mullowney   template <typename Tuple>
1376aa372e3fSPaul Mullowney   __host__ __device__
1377aa372e3fSPaul Mullowney   void operator()(Tuple t)
1378aa372e3fSPaul Mullowney   {
1379aa372e3fSPaul Mullowney     thrust::get<1>(t) = thrust::get<1>(t) + thrust::get<0>(t);
1380aa372e3fSPaul Mullowney   }
1381aa372e3fSPaul Mullowney };
1382aa372e3fSPaul Mullowney 
13836fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy)
13849ae82921SPaul Mullowney {
1385b175d8bbSPaul Mullowney   PetscErrorCode ierr;
13869ae82921SPaul Mullowney 
13879ae82921SPaul Mullowney   PetscFunctionBegin;
13887656d835SStefano Zampini   ierr = MatMultAdd_SeqAIJCUSPARSE(A,xx,NULL,yy);CHKERRQ(ierr);
13899ae82921SPaul Mullowney   PetscFunctionReturn(0);
13909ae82921SPaul Mullowney }
13919ae82921SPaul Mullowney 
13926fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy)
1393ca45077fSPaul Mullowney {
1394ca45077fSPaul Mullowney   Mat_SeqAIJ                   *a = (Mat_SeqAIJ*)A->data;
1395aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE           *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
13969ff858a8SKarl Rupp   Mat_SeqAIJCUSPARSEMultStruct *matstructT;
1397465f34aeSAlejandro Lamas Daviña   const PetscScalar            *xarray;
1398465f34aeSAlejandro Lamas Daviña   PetscScalar                  *yarray;
1399b175d8bbSPaul Mullowney   PetscErrorCode               ierr;
140057d48284SJunchao Zhang   cudaError_t                  cerr;
1401aa372e3fSPaul Mullowney   cusparseStatus_t             stat;
1402ca45077fSPaul Mullowney 
1403ca45077fSPaul Mullowney   PetscFunctionBegin;
140434d6c7a5SJose E. Roman   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
140534d6c7a5SJose E. Roman   ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
14069ff858a8SKarl Rupp   matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
1407aa372e3fSPaul Mullowney   if (!matstructT) {
1408bda325fcSPaul Mullowney     ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr);
1409aa372e3fSPaul Mullowney     matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
1410bda325fcSPaul Mullowney   }
1411c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr);
1412c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(yy,&yarray);CHKERRQ(ierr);
1413f6ae8131SMark   if (yy->map->n) {
1414f6ae8131SMark     PetscInt                     n = yy->map->n;
1415f6ae8131SMark     cudaError_t                  err;
1416f6ae8131SMark     err = cudaMemset(yarray,0,n*sizeof(PetscScalar));CHKERRCUDA(err); /* hack to fix numerical errors from reading output vector yy, apparently */
1417f6ae8131SMark   }
1418aa372e3fSPaul Mullowney 
14197a052e47Shannah_mairs   ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
1420aa372e3fSPaul Mullowney   if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
1421aa372e3fSPaul Mullowney     CsrMatrix *mat = (CsrMatrix*)matstructT->mat;
1422aa372e3fSPaul Mullowney     stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1423aa372e3fSPaul Mullowney                              mat->num_rows, mat->num_cols,
1424b06137fdSPaul Mullowney                              mat->num_entries, matstructT->alpha, matstructT->descr,
1425aa372e3fSPaul Mullowney                              mat->values->data().get(), mat->row_offsets->data().get(),
14267656d835SStefano Zampini                              mat->column_indices->data().get(), xarray, matstructT->beta_zero,
142757d48284SJunchao Zhang                              yarray);CHKERRCUSPARSE(stat);
1428aa372e3fSPaul Mullowney   } else {
1429aa372e3fSPaul Mullowney     cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat;
1430aa372e3fSPaul Mullowney     stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1431b06137fdSPaul Mullowney                              matstructT->alpha, matstructT->descr, hybMat,
14327656d835SStefano Zampini                              xarray, matstructT->beta_zero,
143357d48284SJunchao Zhang                              yarray);CHKERRCUSPARSE(stat);
1434ca45077fSPaul Mullowney   }
1435c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr);
1436c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(yy,&yarray);CHKERRQ(ierr);
143757d48284SJunchao Zhang   cerr = WaitForGPU();CHKERRCUDA(cerr);
1438661c2d29Shannah_mairs   ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1439958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*a->nz - cusparsestruct->nonzerorow);CHKERRQ(ierr);
1440ca45077fSPaul Mullowney   PetscFunctionReturn(0);
1441ca45077fSPaul Mullowney }
1442ca45077fSPaul Mullowney 
1443aa372e3fSPaul Mullowney 
14446fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz)
14459ae82921SPaul Mullowney {
14469ae82921SPaul Mullowney   Mat_SeqAIJ                   *a = (Mat_SeqAIJ*)A->data;
1447aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE           *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
14489ff858a8SKarl Rupp   Mat_SeqAIJCUSPARSEMultStruct *matstruct;
1449465f34aeSAlejandro Lamas Daviña   const PetscScalar            *xarray;
14507656d835SStefano Zampini   PetscScalar                  *zarray,*dptr,*beta;
1451b175d8bbSPaul Mullowney   PetscErrorCode               ierr;
145257d48284SJunchao Zhang   cudaError_t                  cerr;
1453aa372e3fSPaul Mullowney   cusparseStatus_t             stat;
1454c1fb3f03SStefano Zampini   PetscBool                    cmpr; /* if the matrix has been compressed (zero rows) */
14556e111a19SKarl Rupp 
14569ae82921SPaul Mullowney   PetscFunctionBegin;
145734d6c7a5SJose E. Roman   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
145834d6c7a5SJose E. Roman   ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
14599ff858a8SKarl Rupp   matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat;
14609ae82921SPaul Mullowney   try {
1461c1fb3f03SStefano Zampini     cmpr = (PetscBool)(cusparsestruct->workVector->size() == (thrust::detail::vector_base<PetscScalar, thrust::device_malloc_allocator<PetscScalar> >::size_type)(A->rmap->n));
1462c41cb2e2SAlejandro Lamas Daviña     ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr);
1463c1fb3f03SStefano Zampini     if (yy && !cmpr) { /* MatMultAdd with noncompressed storage -> need uptodate zz vector */
1464f2d70e9dSBarry Smith       ierr = VecCUDAGetArray(zz,&zarray);CHKERRQ(ierr);
1465c1fb3f03SStefano Zampini     } else {
1466c1fb3f03SStefano Zampini       ierr = VecCUDAGetArrayWrite(zz,&zarray);CHKERRQ(ierr);
1467c1fb3f03SStefano Zampini     }
1468c1fb3f03SStefano Zampini     dptr = cmpr ? zarray : cusparsestruct->workVector->data().get();
14697656d835SStefano Zampini     beta = (yy == zz && dptr == zarray) ? matstruct->beta_one : matstruct->beta_zero;
14709ae82921SPaul Mullowney 
14717a052e47Shannah_mairs     ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
14727656d835SStefano Zampini     /* csr_spmv is multiply add */
1473aa372e3fSPaul Mullowney     if (cusparsestruct->format == MAT_CUSPARSE_CSR) {
1474b06137fdSPaul Mullowney       /* here we need to be careful to set the number of rows in the multiply to the
1475b06137fdSPaul Mullowney          number of compressed rows in the matrix ... which is equivalent to the
1476b06137fdSPaul Mullowney          size of the workVector */
14777656d835SStefano Zampini       CsrMatrix *mat = (CsrMatrix*)matstruct->mat;
1478aa372e3fSPaul Mullowney       stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1479a65300a6SPaul Mullowney                                mat->num_rows, mat->num_cols,
1480b06137fdSPaul Mullowney                                mat->num_entries, matstruct->alpha, matstruct->descr,
1481aa372e3fSPaul Mullowney                                mat->values->data().get(), mat->row_offsets->data().get(),
14827656d835SStefano Zampini                                mat->column_indices->data().get(), xarray, beta,
148357d48284SJunchao Zhang                                dptr);CHKERRCUSPARSE(stat);
1484aa372e3fSPaul Mullowney     } else {
1485a65300a6SPaul Mullowney       if (cusparsestruct->workVector->size()) {
1486301298b4SMark Adams         cusparseHybMat_t hybMat = (cusparseHybMat_t)matstruct->mat;
1487aa372e3fSPaul Mullowney         stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1488b06137fdSPaul Mullowney                                  matstruct->alpha, matstruct->descr, hybMat,
14897656d835SStefano Zampini                                  xarray, beta,
149057d48284SJunchao Zhang                                  dptr);CHKERRCUSPARSE(stat);
1491a65300a6SPaul Mullowney       }
1492aa372e3fSPaul Mullowney     }
1493958c4211Shannah_mairs     ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1494aa372e3fSPaul Mullowney 
14957656d835SStefano Zampini     if (yy) {
14967656d835SStefano Zampini       if (dptr != zarray) {
14977656d835SStefano Zampini         ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr);
14987656d835SStefano Zampini       } else if (zz != yy) {
14997656d835SStefano Zampini         ierr = VecAXPY_SeqCUDA(zz,1.0,yy);CHKERRQ(ierr);
15007656d835SStefano Zampini       }
15017656d835SStefano Zampini     } else if (dptr != zarray) {
1502c1fb3f03SStefano Zampini       ierr = VecSet_SeqCUDA(zz,0);CHKERRQ(ierr);
15037656d835SStefano Zampini     }
1504aa372e3fSPaul Mullowney     /* scatter the data from the temporary into the full vector with a += operation */
15057a052e47Shannah_mairs     ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
15067656d835SStefano Zampini     if (dptr != zarray) {
15077656d835SStefano Zampini       thrust::device_ptr<PetscScalar> zptr;
15087656d835SStefano Zampini 
15097656d835SStefano Zampini       zptr = thrust::device_pointer_cast(zarray);
1510c41cb2e2SAlejandro Lamas Daviña       thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))),
1511c41cb2e2SAlejandro Lamas Daviña                        thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))) + cusparsestruct->workVector->size(),
1512c41cb2e2SAlejandro Lamas Daviña                        VecCUDAPlusEquals());
15137656d835SStefano Zampini     }
1514958c4211Shannah_mairs     ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1515c41cb2e2SAlejandro Lamas Daviña     ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr);
1516c1fb3f03SStefano Zampini     if (yy && !cmpr) {
1517f2d70e9dSBarry Smith       ierr = VecCUDARestoreArray(zz,&zarray);CHKERRQ(ierr);
1518c1fb3f03SStefano Zampini     } else {
1519c1fb3f03SStefano Zampini       ierr = VecCUDARestoreArrayWrite(zz,&zarray);CHKERRQ(ierr);
1520c1fb3f03SStefano Zampini     }
15219ae82921SPaul Mullowney   } catch(char *ex) {
15229ae82921SPaul Mullowney     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
15239ae82921SPaul Mullowney   }
152457d48284SJunchao Zhang   cerr = WaitForGPU();CHKERRCUDA(cerr);
1525958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr);
15269ae82921SPaul Mullowney   PetscFunctionReturn(0);
15279ae82921SPaul Mullowney }
15289ae82921SPaul Mullowney 
15296fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz)
1530ca45077fSPaul Mullowney {
1531ca45077fSPaul Mullowney   Mat_SeqAIJ                      *a = (Mat_SeqAIJ*)A->data;
1532aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE              *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
15339ff858a8SKarl Rupp   Mat_SeqAIJCUSPARSEMultStruct    *matstructT;
1534465f34aeSAlejandro Lamas Daviña   const PetscScalar               *xarray;
153520291eb5SJunchao Zhang   PetscScalar                     *zarray,*beta;
1536b175d8bbSPaul Mullowney   PetscErrorCode                  ierr;
153757d48284SJunchao Zhang   cudaError_t                     cerr;
1538aa372e3fSPaul Mullowney   cusparseStatus_t                stat;
15396e111a19SKarl Rupp 
1540ca45077fSPaul Mullowney   PetscFunctionBegin;
154134d6c7a5SJose E. Roman   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
154234d6c7a5SJose E. Roman   ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
15439ff858a8SKarl Rupp   matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
1544aa372e3fSPaul Mullowney   if (!matstructT) {
1545bda325fcSPaul Mullowney     ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr);
1546aa372e3fSPaul Mullowney     matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
1547bda325fcSPaul Mullowney   }
1548aa372e3fSPaul Mullowney 
154920291eb5SJunchao Zhang   /* Note unlike Mat, MatTranspose uses non-compressed row storage */
1550ca45077fSPaul Mullowney   try {
1551c41cb2e2SAlejandro Lamas Daviña     ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr);
1552c41cb2e2SAlejandro Lamas Daviña     ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr);
1553f2d70e9dSBarry Smith     ierr = VecCUDAGetArray(zz,&zarray);CHKERRQ(ierr);
155420291eb5SJunchao Zhang     beta = (yy == zz) ? matstructT->beta_one : matstructT->beta_zero;
1555ca45077fSPaul Mullowney 
15567a052e47Shannah_mairs     ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
1557e057df02SPaul Mullowney     /* multiply add with matrix transpose */
1558aa372e3fSPaul Mullowney     if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
1559aa372e3fSPaul Mullowney       CsrMatrix *mat = (CsrMatrix*)matstructT->mat;
1560aa372e3fSPaul Mullowney       stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1561a65300a6SPaul Mullowney                                mat->num_rows, mat->num_cols,
1562b06137fdSPaul Mullowney                                mat->num_entries, matstructT->alpha, matstructT->descr,
1563aa372e3fSPaul Mullowney                                mat->values->data().get(), mat->row_offsets->data().get(),
1564a3fdcf43SKarl Rupp                                mat->column_indices->data().get(), xarray, beta,
15659f74ec24SSatish Balay                                zarray);CHKERRCUSPARSE(stat);
1566aa372e3fSPaul Mullowney     } else {
1567aa372e3fSPaul Mullowney       cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat;
1568a65300a6SPaul Mullowney       if (cusparsestruct->workVector->size()) {
1569aa372e3fSPaul Mullowney         stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1570b06137fdSPaul Mullowney                                  matstructT->alpha, matstructT->descr, hybMat,
1571a3fdcf43SKarl Rupp                                  xarray, beta,
15729f74ec24SSatish Balay                                  zarray);CHKERRCUSPARSE(stat);
1573a65300a6SPaul Mullowney       }
1574aa372e3fSPaul Mullowney     }
1575a3fdcf43SKarl Rupp     ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1576a3fdcf43SKarl Rupp 
157720291eb5SJunchao Zhang     if (zz != yy) {ierr = VecAXPY_SeqCUDA(zz,1.0,yy);CHKERRQ(ierr);}
1578c41cb2e2SAlejandro Lamas Daviña     ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr);
1579f2d70e9dSBarry Smith     ierr = VecCUDARestoreArray(zz,&zarray);CHKERRQ(ierr);
1580ca45077fSPaul Mullowney   } catch(char *ex) {
1581ca45077fSPaul Mullowney     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
1582661c2d29Shannah_mairs   }
15839f74ec24SSatish Balay   cerr = WaitForGPU();CHKERRCUDA(cerr);
1584958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr);
1585ca45077fSPaul Mullowney   PetscFunctionReturn(0);
1586ca45077fSPaul Mullowney }
1587ca45077fSPaul Mullowney 
15886fa9248bSJed Brown static PetscErrorCode MatAssemblyEnd_SeqAIJCUSPARSE(Mat A,MatAssemblyType mode)
15899ae82921SPaul Mullowney {
15909ae82921SPaul Mullowney   PetscErrorCode ierr;
15916e111a19SKarl Rupp 
15929ae82921SPaul Mullowney   PetscFunctionBegin;
15939ae82921SPaul Mullowney   ierr = MatAssemblyEnd_SeqAIJ(A,mode);CHKERRQ(ierr);
159495639643SRichard Tran Mills   if (mode == MAT_FLUSH_ASSEMBLY || A->boundtocpu) PetscFunctionReturn(0);
1595bc3f50f2SPaul Mullowney   if (A->factortype == MAT_FACTOR_NONE) {
1596e057df02SPaul Mullowney     ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
1597bc3f50f2SPaul Mullowney   }
1598bbf3fe20SPaul Mullowney   A->ops->mult             = MatMult_SeqAIJCUSPARSE;
1599bbf3fe20SPaul Mullowney   A->ops->multadd          = MatMultAdd_SeqAIJCUSPARSE;
1600bbf3fe20SPaul Mullowney   A->ops->multtranspose    = MatMultTranspose_SeqAIJCUSPARSE;
1601bbf3fe20SPaul Mullowney   A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE;
16029ae82921SPaul Mullowney   PetscFunctionReturn(0);
16039ae82921SPaul Mullowney }
16049ae82921SPaul Mullowney 
16059ae82921SPaul Mullowney /* --------------------------------------------------------------------------------*/
1606e057df02SPaul Mullowney /*@
16079ae82921SPaul Mullowney    MatCreateSeqAIJCUSPARSE - Creates a sparse matrix in AIJ (compressed row) format
1608e057df02SPaul Mullowney    (the default parallel PETSc format). This matrix will ultimately pushed down
1609e057df02SPaul Mullowney    to NVidia GPUs and use the CUSPARSE library for calculations. For good matrix
1610e057df02SPaul Mullowney    assembly performance the user should preallocate the matrix storage by setting
1611e057df02SPaul Mullowney    the parameter nz (or the array nnz).  By setting these parameters accurately,
1612e057df02SPaul Mullowney    performance during matrix assembly can be increased by more than a factor of 50.
16139ae82921SPaul Mullowney 
1614d083f849SBarry Smith    Collective
16159ae82921SPaul Mullowney 
16169ae82921SPaul Mullowney    Input Parameters:
16179ae82921SPaul Mullowney +  comm - MPI communicator, set to PETSC_COMM_SELF
16189ae82921SPaul Mullowney .  m - number of rows
16199ae82921SPaul Mullowney .  n - number of columns
16209ae82921SPaul Mullowney .  nz - number of nonzeros per row (same for all rows)
16219ae82921SPaul Mullowney -  nnz - array containing the number of nonzeros in the various rows
16220298fd71SBarry Smith          (possibly different for each row) or NULL
16239ae82921SPaul Mullowney 
16249ae82921SPaul Mullowney    Output Parameter:
16259ae82921SPaul Mullowney .  A - the matrix
16269ae82921SPaul Mullowney 
16279ae82921SPaul Mullowney    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
16289ae82921SPaul Mullowney    MatXXXXSetPreallocation() paradgm instead of this routine directly.
16299ae82921SPaul Mullowney    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
16309ae82921SPaul Mullowney 
16319ae82921SPaul Mullowney    Notes:
16329ae82921SPaul Mullowney    If nnz is given then nz is ignored
16339ae82921SPaul Mullowney 
16349ae82921SPaul Mullowney    The AIJ format (also called the Yale sparse matrix format or
16359ae82921SPaul Mullowney    compressed row storage), is fully compatible with standard Fortran 77
16369ae82921SPaul Mullowney    storage.  That is, the stored row and column indices can begin at
16379ae82921SPaul Mullowney    either one (as in Fortran) or zero.  See the users' manual for details.
16389ae82921SPaul Mullowney 
16399ae82921SPaul Mullowney    Specify the preallocated storage with either nz or nnz (not both).
16400298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
16419ae82921SPaul Mullowney    allocation.  For large problems you MUST preallocate memory or you
16429ae82921SPaul Mullowney    will get TERRIBLE performance, see the users' manual chapter on matrices.
16439ae82921SPaul Mullowney 
16449ae82921SPaul Mullowney    By default, this format uses inodes (identical nodes) when possible, to
16459ae82921SPaul Mullowney    improve numerical efficiency of matrix-vector products and solves. We
16469ae82921SPaul Mullowney    search for consecutive rows with the same nonzero structure, thereby
16479ae82921SPaul Mullowney    reusing matrix information to achieve increased efficiency.
16489ae82921SPaul Mullowney 
16499ae82921SPaul Mullowney    Level: intermediate
16509ae82921SPaul Mullowney 
1651e057df02SPaul Mullowney .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ(), MATSEQAIJCUSPARSE, MATAIJCUSPARSE
16529ae82921SPaul Mullowney @*/
16539ae82921SPaul Mullowney PetscErrorCode  MatCreateSeqAIJCUSPARSE(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
16549ae82921SPaul Mullowney {
16559ae82921SPaul Mullowney   PetscErrorCode ierr;
16569ae82921SPaul Mullowney 
16579ae82921SPaul Mullowney   PetscFunctionBegin;
16589ae82921SPaul Mullowney   ierr = MatCreate(comm,A);CHKERRQ(ierr);
16599ae82921SPaul Mullowney   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
16609ae82921SPaul Mullowney   ierr = MatSetType(*A,MATSEQAIJCUSPARSE);CHKERRQ(ierr);
16619ae82921SPaul Mullowney   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
16629ae82921SPaul Mullowney   PetscFunctionReturn(0);
16639ae82921SPaul Mullowney }
16649ae82921SPaul Mullowney 
16656fa9248bSJed Brown static PetscErrorCode MatDestroy_SeqAIJCUSPARSE(Mat A)
16669ae82921SPaul Mullowney {
16679ae82921SPaul Mullowney   PetscErrorCode   ierr;
1668ab25e6cbSDominic Meiser 
16699ae82921SPaul Mullowney   PetscFunctionBegin;
16709ae82921SPaul Mullowney   if (A->factortype==MAT_FACTOR_NONE) {
1671c70f7ee4SJunchao Zhang     if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) {
1672470880abSPatrick Sanan       ierr = MatSeqAIJCUSPARSE_Destroy((Mat_SeqAIJCUSPARSE**)&A->spptr);CHKERRQ(ierr);
16739ae82921SPaul Mullowney     }
16749ae82921SPaul Mullowney   } else {
1675470880abSPatrick Sanan     ierr = MatSeqAIJCUSPARSETriFactors_Destroy((Mat_SeqAIJCUSPARSETriFactors**)&A->spptr);CHKERRQ(ierr);
1676aa372e3fSPaul Mullowney   }
16779ae82921SPaul Mullowney   ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr);
16789ae82921SPaul Mullowney   PetscFunctionReturn(0);
16799ae82921SPaul Mullowney }
16809ae82921SPaul Mullowney 
168195639643SRichard Tran Mills static PetscErrorCode MatBindToCPU_SeqAIJCUSPARSE(Mat,PetscBool);
16829ff858a8SKarl Rupp static PetscErrorCode MatDuplicate_SeqAIJCUSPARSE(Mat A,MatDuplicateOption cpvalues,Mat *B)
16839ff858a8SKarl Rupp {
16849ff858a8SKarl Rupp   PetscErrorCode ierr;
16859ff858a8SKarl Rupp   Mat C;
16869ff858a8SKarl Rupp   cusparseStatus_t stat;
16879ff858a8SKarl Rupp   cusparseHandle_t handle=0;
16889ff858a8SKarl Rupp 
16899ff858a8SKarl Rupp   PetscFunctionBegin;
16909ff858a8SKarl Rupp   ierr = MatDuplicate_SeqAIJ(A,cpvalues,B);CHKERRQ(ierr);
16919ff858a8SKarl Rupp   C    = *B;
169234136279SStefano Zampini   ierr = PetscFree(C->defaultvectype);CHKERRQ(ierr);
169334136279SStefano Zampini   ierr = PetscStrallocpy(VECCUDA,&C->defaultvectype);CHKERRQ(ierr);
169434136279SStefano Zampini 
16959ff858a8SKarl Rupp   /* inject CUSPARSE-specific stuff */
16969ff858a8SKarl Rupp   if (C->factortype==MAT_FACTOR_NONE) {
16979ff858a8SKarl Rupp     /* you cannot check the inode.use flag here since the matrix was just created.
16989ff858a8SKarl Rupp        now build a GPU matrix data structure */
16999ff858a8SKarl Rupp     C->spptr = new Mat_SeqAIJCUSPARSE;
17009ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->mat            = 0;
17019ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->matTranspose   = 0;
17029ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->workVector     = 0;
170381902715SJunchao Zhang     ((Mat_SeqAIJCUSPARSE*)C->spptr)->rowoffsets_gpu = 0;
17049ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->format         = MAT_CUSPARSE_CSR;
17059ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->stream         = 0;
170657d48284SJunchao Zhang     stat = cusparseCreate(&handle);CHKERRCUSPARSE(stat);
17079ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->handle         = handle;
17089ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->nonzerostate   = 0;
17099ff858a8SKarl Rupp   } else {
17109ff858a8SKarl Rupp     /* NEXT, set the pointers to the triangular factors */
17119ff858a8SKarl Rupp     C->spptr = new Mat_SeqAIJCUSPARSETriFactors;
17129ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->loTriFactorPtr          = 0;
17139ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->upTriFactorPtr          = 0;
17149ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->loTriFactorPtrTranspose = 0;
17159ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->upTriFactorPtrTranspose = 0;
17169ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->rpermIndices            = 0;
17179ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->cpermIndices            = 0;
17189ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->workVector              = 0;
17199ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->handle                  = 0;
172057d48284SJunchao Zhang     stat = cusparseCreate(&handle);CHKERRCUSPARSE(stat);
17219ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->handle                  = handle;
17229ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->nnz                     = 0;
17239ff858a8SKarl Rupp   }
17249ff858a8SKarl Rupp 
17259ff858a8SKarl Rupp   C->ops->assemblyend      = MatAssemblyEnd_SeqAIJCUSPARSE;
17269ff858a8SKarl Rupp   C->ops->destroy          = MatDestroy_SeqAIJCUSPARSE;
17279ff858a8SKarl Rupp   C->ops->setfromoptions   = MatSetFromOptions_SeqAIJCUSPARSE;
17289ff858a8SKarl Rupp   C->ops->mult             = MatMult_SeqAIJCUSPARSE;
17299ff858a8SKarl Rupp   C->ops->multadd          = MatMultAdd_SeqAIJCUSPARSE;
17309ff858a8SKarl Rupp   C->ops->multtranspose    = MatMultTranspose_SeqAIJCUSPARSE;
17319ff858a8SKarl Rupp   C->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE;
17329ff858a8SKarl Rupp   C->ops->duplicate        = MatDuplicate_SeqAIJCUSPARSE;
173395639643SRichard Tran Mills   C->ops->bindtocpu        = MatBindToCPU_SeqAIJCUSPARSE;
17349ff858a8SKarl Rupp 
17359ff858a8SKarl Rupp   ierr = PetscObjectChangeTypeName((PetscObject)C,MATSEQAIJCUSPARSE);CHKERRQ(ierr);
17369ff858a8SKarl Rupp 
173795639643SRichard Tran Mills   C->boundtocpu  = PETSC_FALSE;
1738c70f7ee4SJunchao Zhang   C->offloadmask = PETSC_OFFLOAD_UNALLOCATED;
17399ff858a8SKarl Rupp 
17409ff858a8SKarl Rupp   ierr = PetscObjectComposeFunction((PetscObject)C, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr);
17419ff858a8SKarl Rupp   PetscFunctionReturn(0);
17429ff858a8SKarl Rupp }
17439ff858a8SKarl Rupp 
174495639643SRichard Tran Mills 
174595639643SRichard Tran Mills static PetscErrorCode MatBindToCPU_SeqAIJCUSPARSE(Mat A,PetscBool flg)
174695639643SRichard Tran Mills {
174795639643SRichard Tran Mills   PetscFunctionBegin;
1748*c34f1ff0SRichard Tran Mills   /* Currently, there is no case in which an AIJCUSPARSE matrix ever has its offloadmask set to PETS_OFFLOAD_GPU.
174995639643SRichard Tran Mills      If this changes, we need to implement a routine to update the CPU (host) version of the matrix from the GPU one.
175095639643SRichard Tran Mills      Right now, for safety we simply check for PETSC_OFFLOAD_GPU and have MatBindToCPU() do nothing in this case.
175195639643SRichard Tran Mills      TODO: Add MatAIJCUSPARSECopyFromGPU() and make MatBindToCPU() functional for AIJCUSPARSE matries;
175295639643SRichard Tran Mills            can follow the example of MatBindToCPU_SeqAIJViennaCL(). */
175395639643SRichard Tran Mills   if (A->offloadmask == PETSC_OFFLOAD_GPU) PetscFunctionReturn(0);
175495639643SRichard Tran Mills   if (flg) {
175595639643SRichard Tran Mills     A->ops->mult             = MatMult_SeqAIJ;
175695639643SRichard Tran Mills     A->ops->multadd          = MatMultAdd_SeqAIJ;
1757*c34f1ff0SRichard Tran Mills     A->ops->multtranspose    = MatMultTranspose_SeqAIJ;
1758*c34f1ff0SRichard Tran Mills     A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJ;
175995639643SRichard Tran Mills     A->ops->assemblyend      = MatAssemblyEnd_SeqAIJ;
176095639643SRichard Tran Mills     A->ops->duplicate        = MatDuplicate_SeqAIJ;
176195639643SRichard Tran Mills   } else {
176295639643SRichard Tran Mills     A->ops->mult             = MatMult_SeqAIJCUSPARSE;
176395639643SRichard Tran Mills     A->ops->multadd          = MatMultAdd_SeqAIJCUSPARSE;
176495639643SRichard Tran Mills     A->ops->multtranspose    = MatMultTranspose_SeqAIJCUSPARSE;
176595639643SRichard Tran Mills     A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE;
176695639643SRichard Tran Mills     A->ops->assemblyend      = MatAssemblyEnd_SeqAIJCUSPARSE;
176795639643SRichard Tran Mills     A->ops->destroy          = MatDestroy_SeqAIJCUSPARSE;
176895639643SRichard Tran Mills     A->ops->duplicate        = MatDuplicate_SeqAIJCUSPARSE;
176995639643SRichard Tran Mills   }
177095639643SRichard Tran Mills   A->boundtocpu = flg;
177195639643SRichard Tran Mills   PetscFunctionReturn(0);
177295639643SRichard Tran Mills }
177395639643SRichard Tran Mills 
177402fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat B)
17759ae82921SPaul Mullowney {
17769ae82921SPaul Mullowney   PetscErrorCode ierr;
1777aa372e3fSPaul Mullowney   cusparseStatus_t stat;
1778aa372e3fSPaul Mullowney   cusparseHandle_t handle=0;
17799ae82921SPaul Mullowney 
17809ae82921SPaul Mullowney   PetscFunctionBegin;
178134136279SStefano Zampini   ierr = PetscFree(B->defaultvectype);CHKERRQ(ierr);
178234136279SStefano Zampini   ierr = PetscStrallocpy(VECCUDA,&B->defaultvectype);CHKERRQ(ierr);
178334136279SStefano Zampini 
17849ae82921SPaul Mullowney   if (B->factortype==MAT_FACTOR_NONE) {
1785e057df02SPaul Mullowney     /* you cannot check the inode.use flag here since the matrix was just created.
1786e057df02SPaul Mullowney        now build a GPU matrix data structure */
17879ae82921SPaul Mullowney     B->spptr = new Mat_SeqAIJCUSPARSE;
17889ae82921SPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->mat            = 0;
1789aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->matTranspose   = 0;
1790aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->workVector     = 0;
179181902715SJunchao Zhang     ((Mat_SeqAIJCUSPARSE*)B->spptr)->rowoffsets_gpu = 0;
1792e057df02SPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->format         = MAT_CUSPARSE_CSR;
1793aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->stream         = 0;
179457d48284SJunchao Zhang     stat = cusparseCreate(&handle);CHKERRCUSPARSE(stat);
1795aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->handle         = handle;
17969ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)B->spptr)->nonzerostate   = 0;
17979ae82921SPaul Mullowney   } else {
17989ae82921SPaul Mullowney     /* NEXT, set the pointers to the triangular factors */
1799debe9ee2SPaul Mullowney     B->spptr = new Mat_SeqAIJCUSPARSETriFactors;
18009ae82921SPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtr          = 0;
18019ae82921SPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtr          = 0;
1802aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtrTranspose = 0;
1803aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtrTranspose = 0;
1804aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->rpermIndices            = 0;
1805aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->cpermIndices            = 0;
1806aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->workVector              = 0;
180757d48284SJunchao Zhang     stat = cusparseCreate(&handle);CHKERRCUSPARSE(stat);
1808aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->handle                  = handle;
1809aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->nnz                     = 0;
18109ae82921SPaul Mullowney   }
1811aa372e3fSPaul Mullowney 
18129ae82921SPaul Mullowney   B->ops->assemblyend      = MatAssemblyEnd_SeqAIJCUSPARSE;
18139ae82921SPaul Mullowney   B->ops->destroy          = MatDestroy_SeqAIJCUSPARSE;
18149ae82921SPaul Mullowney   B->ops->setfromoptions   = MatSetFromOptions_SeqAIJCUSPARSE;
1815ca45077fSPaul Mullowney   B->ops->mult             = MatMult_SeqAIJCUSPARSE;
1816ca45077fSPaul Mullowney   B->ops->multadd          = MatMultAdd_SeqAIJCUSPARSE;
1817ca45077fSPaul Mullowney   B->ops->multtranspose    = MatMultTranspose_SeqAIJCUSPARSE;
1818ca45077fSPaul Mullowney   B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE;
18199ff858a8SKarl Rupp   B->ops->duplicate        = MatDuplicate_SeqAIJCUSPARSE;
182095639643SRichard Tran Mills   B->ops->bindtocpu        = MatBindToCPU_SeqAIJCUSPARSE;
18212205254eSKarl Rupp 
18229ae82921SPaul Mullowney   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJCUSPARSE);CHKERRQ(ierr);
18232205254eSKarl Rupp 
182495639643SRichard Tran Mills   B->boundtocpu  = PETSC_FALSE;
1825c70f7ee4SJunchao Zhang   B->offloadmask = PETSC_OFFLOAD_UNALLOCATED;
18262205254eSKarl Rupp 
1827bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr);
18289ae82921SPaul Mullowney   PetscFunctionReturn(0);
18299ae82921SPaul Mullowney }
18309ae82921SPaul Mullowney 
183102fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJCUSPARSE(Mat B)
183202fe1965SBarry Smith {
183302fe1965SBarry Smith   PetscErrorCode ierr;
183402fe1965SBarry Smith 
183502fe1965SBarry Smith   PetscFunctionBegin;
183602fe1965SBarry Smith   ierr = MatCreate_SeqAIJ(B);CHKERRQ(ierr);
1837489de41dSStefano Zampini   ierr = MatConvert_SeqAIJ_SeqAIJCUSPARSE(B);CHKERRQ(ierr);
183802fe1965SBarry Smith   PetscFunctionReturn(0);
183902fe1965SBarry Smith }
184002fe1965SBarry Smith 
18413ca39a21SBarry Smith /*MC
1842e057df02SPaul Mullowney    MATSEQAIJCUSPARSE - MATAIJCUSPARSE = "(seq)aijcusparse" - A matrix type to be used for sparse matrices.
1843e057df02SPaul Mullowney 
1844e057df02SPaul Mullowney    A matrix type type whose data resides on Nvidia GPUs. These matrices can be in either
18452692e278SPaul Mullowney    CSR, ELL, or Hybrid format. The ELL and HYB formats require CUDA 4.2 or later.
18462692e278SPaul Mullowney    All matrix calculations are performed on Nvidia GPUs using the CUSPARSE library.
1847e057df02SPaul Mullowney 
1848e057df02SPaul Mullowney    Options Database Keys:
1849e057df02SPaul Mullowney +  -mat_type aijcusparse - sets the matrix type to "seqaijcusparse" during a call to MatSetFromOptions()
1850aa372e3fSPaul 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).
1851a2b725a8SWilliam 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).
1852e057df02SPaul Mullowney 
1853e057df02SPaul Mullowney   Level: beginner
1854e057df02SPaul Mullowney 
18558468deeeSKarl Rupp .seealso: MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation
1856e057df02SPaul Mullowney M*/
18577f756511SDominic Meiser 
185842c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat,MatFactorType,Mat*);
185942c9c57cSBarry Smith 
18600f39cd5aSBarry Smith 
18613ca39a21SBarry Smith PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_CUSPARSE(void)
186242c9c57cSBarry Smith {
186342c9c57cSBarry Smith   PetscErrorCode ierr;
186442c9c57cSBarry Smith 
186542c9c57cSBarry Smith   PetscFunctionBegin;
18663ca39a21SBarry Smith   ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_LU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr);
18673ca39a21SBarry Smith   ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_CHOLESKY,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr);
18683ca39a21SBarry Smith   ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ILU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr);
18693ca39a21SBarry Smith   ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ICC,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr);
187042c9c57cSBarry Smith   PetscFunctionReturn(0);
187142c9c57cSBarry Smith }
187229b38603SBarry Smith 
187381e08676SBarry Smith 
1874470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE **cusparsestruct)
18757f756511SDominic Meiser {
18767f756511SDominic Meiser   cusparseStatus_t stat;
18777f756511SDominic Meiser   cusparseHandle_t handle;
18787f756511SDominic Meiser 
18797f756511SDominic Meiser   PetscFunctionBegin;
18807f756511SDominic Meiser   if (*cusparsestruct) {
1881470880abSPatrick Sanan     MatSeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->mat,(*cusparsestruct)->format);
1882470880abSPatrick Sanan     MatSeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->matTranspose,(*cusparsestruct)->format);
18837f756511SDominic Meiser     delete (*cusparsestruct)->workVector;
188481902715SJunchao Zhang     delete (*cusparsestruct)->rowoffsets_gpu;
18857f756511SDominic Meiser     if (handle = (*cusparsestruct)->handle) {
188657d48284SJunchao Zhang       stat = cusparseDestroy(handle);CHKERRCUSPARSE(stat);
18877f756511SDominic Meiser     }
18887f756511SDominic Meiser     delete *cusparsestruct;
18897f756511SDominic Meiser     *cusparsestruct = 0;
18907f756511SDominic Meiser   }
18917f756511SDominic Meiser   PetscFunctionReturn(0);
18927f756511SDominic Meiser }
18937f756511SDominic Meiser 
18947f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix **mat)
18957f756511SDominic Meiser {
18967f756511SDominic Meiser   PetscFunctionBegin;
18977f756511SDominic Meiser   if (*mat) {
18987f756511SDominic Meiser     delete (*mat)->values;
18997f756511SDominic Meiser     delete (*mat)->column_indices;
19007f756511SDominic Meiser     delete (*mat)->row_offsets;
19017f756511SDominic Meiser     delete *mat;
19027f756511SDominic Meiser     *mat = 0;
19037f756511SDominic Meiser   }
19047f756511SDominic Meiser   PetscFunctionReturn(0);
19057f756511SDominic Meiser }
19067f756511SDominic Meiser 
1907470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct **trifactor)
19087f756511SDominic Meiser {
19097f756511SDominic Meiser   cusparseStatus_t stat;
19107f756511SDominic Meiser   PetscErrorCode   ierr;
19117f756511SDominic Meiser 
19127f756511SDominic Meiser   PetscFunctionBegin;
19137f756511SDominic Meiser   if (*trifactor) {
191457d48284SJunchao Zhang     if ((*trifactor)->descr) { stat = cusparseDestroyMatDescr((*trifactor)->descr);CHKERRCUSPARSE(stat); }
191557d48284SJunchao Zhang     if ((*trifactor)->solveInfo) { stat = cusparseDestroySolveAnalysisInfo((*trifactor)->solveInfo);CHKERRCUSPARSE(stat); }
19167f756511SDominic Meiser     ierr = CsrMatrix_Destroy(&(*trifactor)->csrMat);CHKERRQ(ierr);
19177f756511SDominic Meiser     delete *trifactor;
19187f756511SDominic Meiser     *trifactor = 0;
19197f756511SDominic Meiser   }
19207f756511SDominic Meiser   PetscFunctionReturn(0);
19217f756511SDominic Meiser }
19227f756511SDominic Meiser 
1923470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct **matstruct,MatCUSPARSEStorageFormat format)
19247f756511SDominic Meiser {
19257f756511SDominic Meiser   CsrMatrix        *mat;
19267f756511SDominic Meiser   cusparseStatus_t stat;
19277f756511SDominic Meiser   cudaError_t      err;
19287f756511SDominic Meiser 
19297f756511SDominic Meiser   PetscFunctionBegin;
19307f756511SDominic Meiser   if (*matstruct) {
19317f756511SDominic Meiser     if ((*matstruct)->mat) {
19327f756511SDominic Meiser       if (format==MAT_CUSPARSE_ELL || format==MAT_CUSPARSE_HYB) {
19337f756511SDominic Meiser         cusparseHybMat_t hybMat = (cusparseHybMat_t)(*matstruct)->mat;
193457d48284SJunchao Zhang         stat = cusparseDestroyHybMat(hybMat);CHKERRCUSPARSE(stat);
19357f756511SDominic Meiser       } else {
19367f756511SDominic Meiser         mat = (CsrMatrix*)(*matstruct)->mat;
19377f756511SDominic Meiser         CsrMatrix_Destroy(&mat);
19387f756511SDominic Meiser       }
19397f756511SDominic Meiser     }
194057d48284SJunchao Zhang     if ((*matstruct)->descr) { stat = cusparseDestroyMatDescr((*matstruct)->descr);CHKERRCUSPARSE(stat); }
19417f756511SDominic Meiser     delete (*matstruct)->cprowIndices;
1942c41cb2e2SAlejandro Lamas Daviña     if ((*matstruct)->alpha)     { err=cudaFree((*matstruct)->alpha);CHKERRCUDA(err); }
19437656d835SStefano Zampini     if ((*matstruct)->beta_zero) { err=cudaFree((*matstruct)->beta_zero);CHKERRCUDA(err); }
19447656d835SStefano Zampini     if ((*matstruct)->beta_one)  { err=cudaFree((*matstruct)->beta_one);CHKERRCUDA(err); }
19457f756511SDominic Meiser     delete *matstruct;
19467f756511SDominic Meiser     *matstruct = 0;
19477f756511SDominic Meiser   }
19487f756511SDominic Meiser   PetscFunctionReturn(0);
19497f756511SDominic Meiser }
19507f756511SDominic Meiser 
1951470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors** trifactors)
19527f756511SDominic Meiser {
19537f756511SDominic Meiser   cusparseHandle_t handle;
19547f756511SDominic Meiser   cusparseStatus_t stat;
19557f756511SDominic Meiser 
19567f756511SDominic Meiser   PetscFunctionBegin;
19577f756511SDominic Meiser   if (*trifactors) {
1958470880abSPatrick Sanan     MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->loTriFactorPtr);
1959470880abSPatrick Sanan     MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->upTriFactorPtr);
1960470880abSPatrick Sanan     MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->loTriFactorPtrTranspose);
1961470880abSPatrick Sanan     MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->upTriFactorPtrTranspose);
19627f756511SDominic Meiser     delete (*trifactors)->rpermIndices;
19637f756511SDominic Meiser     delete (*trifactors)->cpermIndices;
19647f756511SDominic Meiser     delete (*trifactors)->workVector;
19657f756511SDominic Meiser     if (handle = (*trifactors)->handle) {
196657d48284SJunchao Zhang       stat = cusparseDestroy(handle);CHKERRCUSPARSE(stat);
19677f756511SDominic Meiser     }
19687f756511SDominic Meiser     delete *trifactors;
19697f756511SDominic Meiser     *trifactors = 0;
19707f756511SDominic Meiser   }
19717f756511SDominic Meiser   PetscFunctionReturn(0);
19727f756511SDominic Meiser }
19737f756511SDominic Meiser 
1974