xref: /petsc/src/mat/impls/aij/seq/seqcusparse/aijcusparse.cu (revision 53800007fd6c7b4835acc3912dc4427fee16917e)
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
6*53800007SKarl Rupp #define PETSC_SKIP_CXX_COMPLEX_FIX
79ae82921SPaul Mullowney 
83d13b8fdSMatthew G. Knepley #include <petscconf.h>
93d13b8fdSMatthew G. Knepley #include <../src/mat/impls/aij/seq/aij.h>          /*I "petscmat.h" I*/
10087f3262SPaul Mullowney #include <../src/mat/impls/sbaij/seq/sbaij.h>
113d13b8fdSMatthew G. Knepley #include <../src/vec/vec/impls/dvecimpl.h>
12af0996ceSBarry Smith #include <petsc/private/vecimpl.h>
139ae82921SPaul Mullowney #undef VecType
143d13b8fdSMatthew G. Knepley #include <../src/mat/impls/aij/seq/seqcusparse/cusparsematimpl.h>
15bc3f50f2SPaul Mullowney 
16e057df02SPaul Mullowney const char *const MatCUSPARSEStorageFormats[] = {"CSR","ELL","HYB","MatCUSPARSEStorageFormat","MAT_CUSPARSE_",0};
179ae82921SPaul Mullowney 
18087f3262SPaul Mullowney static PetscErrorCode MatICCFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,const MatFactorInfo*);
19087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,const MatFactorInfo*);
20087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat,Mat,const MatFactorInfo*);
21087f3262SPaul Mullowney 
226fa9248bSJed Brown static PetscErrorCode MatILUFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,IS,const MatFactorInfo*);
236fa9248bSJed Brown static PetscErrorCode MatLUFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,IS,const MatFactorInfo*);
246fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat,Mat,const MatFactorInfo*);
25087f3262SPaul Mullowney 
266fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat,Vec,Vec);
276fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat,Vec,Vec);
286fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat,Vec,Vec);
296fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat,Vec,Vec);
304416b707SBarry Smith static PetscErrorCode MatSetFromOptions_SeqAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat);
316fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat,Vec,Vec);
326fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat,Vec,Vec,Vec);
336fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat,Vec,Vec);
346fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat,Vec,Vec,Vec);
359ae82921SPaul Mullowney 
367f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix**);
37470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct**);
38470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct**,MatCUSPARSEStorageFormat);
39470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors**);
40470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE**);
417f756511SDominic Meiser 
42b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSESetStream(Mat A,const cudaStream_t stream)
43b06137fdSPaul Mullowney {
44b06137fdSPaul Mullowney   cusparseStatus_t   stat;
45b06137fdSPaul Mullowney   Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
46b06137fdSPaul Mullowney 
47b06137fdSPaul Mullowney   PetscFunctionBegin;
48b06137fdSPaul Mullowney   cusparsestruct->stream = stream;
49c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetStream(cusparsestruct->handle,cusparsestruct->stream);CHKERRCUDA(stat);
50b06137fdSPaul Mullowney   PetscFunctionReturn(0);
51b06137fdSPaul Mullowney }
52b06137fdSPaul Mullowney 
53b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSESetHandle(Mat A,const cusparseHandle_t handle)
54b06137fdSPaul Mullowney {
55b06137fdSPaul Mullowney   cusparseStatus_t   stat;
56b06137fdSPaul Mullowney   Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
57b06137fdSPaul Mullowney 
58b06137fdSPaul Mullowney   PetscFunctionBegin;
596b1cf21dSAlejandro Lamas Daviña   if (cusparsestruct->handle != handle) {
6016a2e217SAlejandro Lamas Daviña     if (cusparsestruct->handle) {
61c41cb2e2SAlejandro Lamas Daviña       stat = cusparseDestroy(cusparsestruct->handle);CHKERRCUDA(stat);
6216a2e217SAlejandro Lamas Daviña     }
63b06137fdSPaul Mullowney     cusparsestruct->handle = handle;
646b1cf21dSAlejandro Lamas Daviña   }
65c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat);
66b06137fdSPaul Mullowney   PetscFunctionReturn(0);
67b06137fdSPaul Mullowney }
68b06137fdSPaul Mullowney 
69b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSEClearHandle(Mat A)
70b06137fdSPaul Mullowney {
71b06137fdSPaul Mullowney   Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
72b06137fdSPaul Mullowney   PetscFunctionBegin;
73b06137fdSPaul Mullowney   if (cusparsestruct->handle)
74b06137fdSPaul Mullowney     cusparsestruct->handle = 0;
75b06137fdSPaul Mullowney   PetscFunctionReturn(0);
76b06137fdSPaul Mullowney }
77b06137fdSPaul Mullowney 
78ea799195SBarry Smith PetscErrorCode MatFactorGetSolverType_seqaij_cusparse(Mat A,MatSolverType *type)
799ae82921SPaul Mullowney {
809ae82921SPaul Mullowney   PetscFunctionBegin;
819ae82921SPaul Mullowney   *type = MATSOLVERCUSPARSE;
829ae82921SPaul Mullowney   PetscFunctionReturn(0);
839ae82921SPaul Mullowney }
849ae82921SPaul Mullowney 
85c708e6cdSJed Brown /*MC
86087f3262SPaul Mullowney   MATSOLVERCUSPARSE = "cusparse" - A matrix type providing triangular solvers for seq matrices
87087f3262SPaul Mullowney   on a single GPU of type, seqaijcusparse, aijcusparse, or seqaijcusp, aijcusp. Currently supported
88087f3262SPaul Mullowney   algorithms are ILU(k) and ICC(k). Typically, deeper factorizations (larger k) results in poorer
89087f3262SPaul Mullowney   performance in the triangular solves. Full LU, and Cholesky decompositions can be solved through the
90087f3262SPaul Mullowney   CUSPARSE triangular solve algorithm. However, the performance can be quite poor and thus these
91087f3262SPaul Mullowney   algorithms are not recommended. This class does NOT support direct solver operations.
92c708e6cdSJed Brown 
939ae82921SPaul Mullowney   Level: beginner
94c708e6cdSJed Brown 
953ca39a21SBarry Smith .seealso: PCFactorSetMatSolverType(), MatSolverType, MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation
96c708e6cdSJed Brown M*/
979ae82921SPaul Mullowney 
9842c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat A,MatFactorType ftype,Mat *B)
999ae82921SPaul Mullowney {
1009ae82921SPaul Mullowney   PetscErrorCode ierr;
101bc3f50f2SPaul Mullowney   PetscInt       n = A->rmap->n;
1029ae82921SPaul Mullowney 
1039ae82921SPaul Mullowney   PetscFunctionBegin;
104bc3f50f2SPaul Mullowney   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
105404133a2SPaul Mullowney   (*B)->factortype = ftype;
106bc3f50f2SPaul Mullowney   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
1079ae82921SPaul Mullowney   ierr = MatSetType(*B,MATSEQAIJCUSPARSE);CHKERRQ(ierr);
1082205254eSKarl Rupp 
109087f3262SPaul Mullowney   if (ftype == MAT_FACTOR_LU || ftype == MAT_FACTOR_ILU || ftype == MAT_FACTOR_ILUDT) {
11033d57670SJed Brown     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
1119ae82921SPaul Mullowney     (*B)->ops->ilufactorsymbolic = MatILUFactorSymbolic_SeqAIJCUSPARSE;
1129ae82921SPaul Mullowney     (*B)->ops->lufactorsymbolic  = MatLUFactorSymbolic_SeqAIJCUSPARSE;
113087f3262SPaul Mullowney   } else if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
114087f3262SPaul Mullowney     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqAIJCUSPARSE;
115087f3262SPaul Mullowney     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqAIJCUSPARSE;
1169ae82921SPaul Mullowney   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported for CUSPARSE Matrix Types");
117bc3f50f2SPaul Mullowney 
118fa03d054SJed Brown   ierr = MatSeqAIJSetPreallocation(*B,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr);
1193ca39a21SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)(*B),"MatFactorGetSolverType_C",MatFactorGetSolverType_seqaij_cusparse);CHKERRQ(ierr);
1209ae82921SPaul Mullowney   PetscFunctionReturn(0);
1219ae82921SPaul Mullowney }
1229ae82921SPaul Mullowney 
123bc3f50f2SPaul Mullowney PETSC_INTERN PetscErrorCode MatCUSPARSESetFormat_SeqAIJCUSPARSE(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format)
124ca45077fSPaul Mullowney {
125aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
1266e111a19SKarl Rupp 
127ca45077fSPaul Mullowney   PetscFunctionBegin;
1282692e278SPaul Mullowney #if CUDA_VERSION>=4020
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   }
1392692e278SPaul Mullowney #else
1406c4ed002SBarry Smith   if (format==MAT_CUSPARSE_ELL || format==MAT_CUSPARSE_HYB) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"ELL (Ellpack) and HYB (Hybrid) storage format require CUDA 4.2 or later.");
1412692e278SPaul Mullowney #endif
142ca45077fSPaul Mullowney   PetscFunctionReturn(0);
143ca45077fSPaul Mullowney }
1449ae82921SPaul Mullowney 
145e057df02SPaul Mullowney /*@
146e057df02SPaul Mullowney    MatCUSPARSESetFormat - Sets the storage format of CUSPARSE matrices for a particular
147e057df02SPaul Mullowney    operation. Only the MatMult operation can use different GPU storage formats
148aa372e3fSPaul Mullowney    for MPIAIJCUSPARSE matrices.
149e057df02SPaul Mullowney    Not Collective
150e057df02SPaul Mullowney 
151e057df02SPaul Mullowney    Input Parameters:
1528468deeeSKarl Rupp +  A - Matrix of type SEQAIJCUSPARSE
15336d62e41SPaul 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.
1542692e278SPaul Mullowney -  format - MatCUSPARSEStorageFormat (one of MAT_CUSPARSE_CSR, MAT_CUSPARSE_ELL, MAT_CUSPARSE_HYB. The latter two require CUDA 4.2)
155e057df02SPaul Mullowney 
156e057df02SPaul Mullowney    Output Parameter:
157e057df02SPaul Mullowney 
158e057df02SPaul Mullowney    Level: intermediate
159e057df02SPaul Mullowney 
1608468deeeSKarl Rupp .seealso: MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation
161e057df02SPaul Mullowney @*/
162e057df02SPaul Mullowney PetscErrorCode MatCUSPARSESetFormat(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format)
163e057df02SPaul Mullowney {
164e057df02SPaul Mullowney   PetscErrorCode ierr;
1656e111a19SKarl Rupp 
166e057df02SPaul Mullowney   PetscFunctionBegin;
167e057df02SPaul Mullowney   PetscValidHeaderSpecific(A, MAT_CLASSID,1);
168e057df02SPaul Mullowney   ierr = PetscTryMethod(A, "MatCUSPARSESetFormat_C",(Mat,MatCUSPARSEFormatOperation,MatCUSPARSEStorageFormat),(A,op,format));CHKERRQ(ierr);
169e057df02SPaul Mullowney   PetscFunctionReturn(0);
170e057df02SPaul Mullowney }
171e057df02SPaul Mullowney 
1724416b707SBarry Smith static PetscErrorCode MatSetFromOptions_SeqAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat A)
1739ae82921SPaul Mullowney {
1749ae82921SPaul Mullowney   PetscErrorCode           ierr;
175e057df02SPaul Mullowney   MatCUSPARSEStorageFormat format;
1769ae82921SPaul Mullowney   PetscBool                flg;
177a183c035SDominic Meiser   Mat_SeqAIJCUSPARSE       *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
1786e111a19SKarl Rupp 
1799ae82921SPaul Mullowney   PetscFunctionBegin;
180e55864a3SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"SeqAIJCUSPARSE options");CHKERRQ(ierr);
1819ae82921SPaul Mullowney   if (A->factortype==MAT_FACTOR_NONE) {
182e057df02SPaul Mullowney     ierr = PetscOptionsEnum("-mat_cusparse_mult_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV",
183a183c035SDominic Meiser                             "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr);
184e057df02SPaul Mullowney     if (flg) {
185e057df02SPaul Mullowney       ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_MULT,format);CHKERRQ(ierr);
186045c96e1SPaul Mullowney     }
1879ae82921SPaul Mullowney   }
1884c87dfd4SPaul Mullowney   ierr = PetscOptionsEnum("-mat_cusparse_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV and TriSolve",
189a183c035SDominic Meiser                           "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr);
1904c87dfd4SPaul Mullowney   if (flg) {
1914c87dfd4SPaul Mullowney     ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_ALL,format);CHKERRQ(ierr);
1924c87dfd4SPaul Mullowney   }
1930af67c1bSStefano Zampini   ierr = PetscOptionsTail();CHKERRQ(ierr);
1949ae82921SPaul Mullowney   PetscFunctionReturn(0);
1959ae82921SPaul Mullowney 
1969ae82921SPaul Mullowney }
1979ae82921SPaul Mullowney 
1986fa9248bSJed Brown static PetscErrorCode MatILUFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
1999ae82921SPaul Mullowney {
2009ae82921SPaul Mullowney   PetscErrorCode ierr;
2019ae82921SPaul Mullowney 
2029ae82921SPaul Mullowney   PetscFunctionBegin;
2039ae82921SPaul Mullowney   ierr = MatILUFactorSymbolic_SeqAIJ(B,A,isrow,iscol,info);CHKERRQ(ierr);
2049ae82921SPaul Mullowney   B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJCUSPARSE;
2059ae82921SPaul Mullowney   PetscFunctionReturn(0);
2069ae82921SPaul Mullowney }
2079ae82921SPaul Mullowney 
2086fa9248bSJed Brown static PetscErrorCode MatLUFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
2099ae82921SPaul Mullowney {
2109ae82921SPaul Mullowney   PetscErrorCode ierr;
2119ae82921SPaul Mullowney 
2129ae82921SPaul Mullowney   PetscFunctionBegin;
2139ae82921SPaul Mullowney   ierr = MatLUFactorSymbolic_SeqAIJ(B,A,isrow,iscol,info);CHKERRQ(ierr);
2149ae82921SPaul Mullowney   B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJCUSPARSE;
2159ae82921SPaul Mullowney   PetscFunctionReturn(0);
2169ae82921SPaul Mullowney }
2179ae82921SPaul Mullowney 
218087f3262SPaul Mullowney static PetscErrorCode MatICCFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS perm,const MatFactorInfo *info)
219087f3262SPaul Mullowney {
220087f3262SPaul Mullowney   PetscErrorCode ierr;
221087f3262SPaul Mullowney 
222087f3262SPaul Mullowney   PetscFunctionBegin;
223087f3262SPaul Mullowney   ierr = MatICCFactorSymbolic_SeqAIJ(B,A,perm,info);CHKERRQ(ierr);
224087f3262SPaul Mullowney   B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJCUSPARSE;
225087f3262SPaul Mullowney   PetscFunctionReturn(0);
226087f3262SPaul Mullowney }
227087f3262SPaul Mullowney 
228087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS perm,const MatFactorInfo *info)
229087f3262SPaul Mullowney {
230087f3262SPaul Mullowney   PetscErrorCode ierr;
231087f3262SPaul Mullowney 
232087f3262SPaul Mullowney   PetscFunctionBegin;
233087f3262SPaul Mullowney   ierr = MatCholeskyFactorSymbolic_SeqAIJ(B,A,perm,info);CHKERRQ(ierr);
234087f3262SPaul Mullowney   B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJCUSPARSE;
235087f3262SPaul Mullowney   PetscFunctionReturn(0);
236087f3262SPaul Mullowney }
237087f3262SPaul Mullowney 
238087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILULowerTriMatrix(Mat A)
2399ae82921SPaul Mullowney {
2409ae82921SPaul Mullowney   Mat_SeqAIJ                        *a = (Mat_SeqAIJ*)A->data;
2419ae82921SPaul Mullowney   PetscInt                          n = A->rmap->n;
2429ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
243aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
2449ae82921SPaul Mullowney   cusparseStatus_t                  stat;
2459ae82921SPaul Mullowney   const PetscInt                    *ai = a->i,*aj = a->j,*vi;
2469ae82921SPaul Mullowney   const MatScalar                   *aa = a->a,*v;
2479ae82921SPaul Mullowney   PetscInt                          *AiLo, *AjLo;
2489ae82921SPaul Mullowney   PetscScalar                       *AALo;
2499ae82921SPaul Mullowney   PetscInt                          i,nz, nzLower, offset, rowOffset;
250b175d8bbSPaul Mullowney   PetscErrorCode                    ierr;
2519ae82921SPaul Mullowney 
2529ae82921SPaul Mullowney   PetscFunctionBegin;
253b8ced49eSKarl Rupp   if (A->valid_GPU_matrix == PETSC_OFFLOAD_UNALLOCATED || A->valid_GPU_matrix == PETSC_OFFLOAD_CPU) {
2549ae82921SPaul Mullowney     try {
2559ae82921SPaul Mullowney       /* first figure out the number of nonzeros in the lower triangular matrix including 1's on the diagonal. */
2569ae82921SPaul Mullowney       nzLower=n+ai[n]-ai[1];
2579ae82921SPaul Mullowney 
2589ae82921SPaul Mullowney       /* Allocate Space for the lower triangular matrix */
259c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AiLo, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr);
260c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AjLo, nzLower*sizeof(PetscInt));CHKERRCUDA(ierr);
261c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AALo, nzLower*sizeof(PetscScalar));CHKERRCUDA(ierr);
2629ae82921SPaul Mullowney 
2639ae82921SPaul Mullowney       /* Fill the lower triangular matrix */
2649ae82921SPaul Mullowney       AiLo[0]  = (PetscInt) 0;
2659ae82921SPaul Mullowney       AiLo[n]  = nzLower;
2669ae82921SPaul Mullowney       AjLo[0]  = (PetscInt) 0;
2679ae82921SPaul Mullowney       AALo[0]  = (MatScalar) 1.0;
2689ae82921SPaul Mullowney       v        = aa;
2699ae82921SPaul Mullowney       vi       = aj;
2709ae82921SPaul Mullowney       offset   = 1;
2719ae82921SPaul Mullowney       rowOffset= 1;
2729ae82921SPaul Mullowney       for (i=1; i<n; i++) {
2739ae82921SPaul Mullowney         nz = ai[i+1] - ai[i];
274e057df02SPaul Mullowney         /* additional 1 for the term on the diagonal */
2759ae82921SPaul Mullowney         AiLo[i]    = rowOffset;
2769ae82921SPaul Mullowney         rowOffset += nz+1;
2779ae82921SPaul Mullowney 
278580bdb30SBarry Smith         ierr = PetscArraycpy(&(AjLo[offset]), vi, nz);CHKERRQ(ierr);
279580bdb30SBarry Smith         ierr = PetscArraycpy(&(AALo[offset]), v, nz);CHKERRQ(ierr);
2809ae82921SPaul Mullowney 
2819ae82921SPaul Mullowney         offset      += nz;
2829ae82921SPaul Mullowney         AjLo[offset] = (PetscInt) i;
2839ae82921SPaul Mullowney         AALo[offset] = (MatScalar) 1.0;
2849ae82921SPaul Mullowney         offset      += 1;
2859ae82921SPaul Mullowney 
2869ae82921SPaul Mullowney         v  += nz;
2879ae82921SPaul Mullowney         vi += nz;
2889ae82921SPaul Mullowney       }
2892205254eSKarl Rupp 
290aa372e3fSPaul Mullowney       /* allocate space for the triangular factor information */
291aa372e3fSPaul Mullowney       loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct;
2922205254eSKarl Rupp 
293aa372e3fSPaul Mullowney       /* Create the matrix description */
294c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUDA(stat);
295c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat);
296c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat);
297c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_LOWER);CHKERRCUDA(stat);
298c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUDA(stat);
299aa372e3fSPaul Mullowney 
300aa372e3fSPaul Mullowney       /* Create the solve analysis information */
301c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUDA(stat);
302aa372e3fSPaul Mullowney 
303aa372e3fSPaul Mullowney       /* set the operation */
304aa372e3fSPaul Mullowney       loTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
305aa372e3fSPaul Mullowney 
306aa372e3fSPaul Mullowney       /* set the matrix */
307aa372e3fSPaul Mullowney       loTriFactor->csrMat = new CsrMatrix;
308aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_rows = n;
309aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_cols = n;
310aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_entries = nzLower;
311aa372e3fSPaul Mullowney 
312aa372e3fSPaul Mullowney       loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1);
313aa372e3fSPaul Mullowney       loTriFactor->csrMat->row_offsets->assign(AiLo, AiLo+n+1);
314aa372e3fSPaul Mullowney 
315aa372e3fSPaul Mullowney       loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzLower);
316aa372e3fSPaul Mullowney       loTriFactor->csrMat->column_indices->assign(AjLo, AjLo+nzLower);
317aa372e3fSPaul Mullowney 
318aa372e3fSPaul Mullowney       loTriFactor->csrMat->values = new THRUSTARRAY(nzLower);
319aa372e3fSPaul Mullowney       loTriFactor->csrMat->values->assign(AALo, AALo+nzLower);
320aa372e3fSPaul Mullowney 
321aa372e3fSPaul Mullowney       /* perform the solve analysis */
322aa372e3fSPaul Mullowney       stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp,
323aa372e3fSPaul Mullowney                                loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr,
324aa372e3fSPaul Mullowney                                loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(),
325c41cb2e2SAlejandro Lamas Daviña                                loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUDA(stat);
326aa372e3fSPaul Mullowney 
327aa372e3fSPaul Mullowney       /* assign the pointer. Is this really necessary? */
328aa372e3fSPaul Mullowney       ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor;
3292205254eSKarl Rupp 
330c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AiLo);CHKERRCUDA(ierr);
331c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AjLo);CHKERRCUDA(ierr);
332c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AALo);CHKERRCUDA(ierr);
3334863603aSSatish Balay       ierr = PetscLogCpuToGpu((n+1+nzLower)*sizeof(int)+nzLower*sizeof(PetscScalar));CHKERRQ(ierr);
3349ae82921SPaul Mullowney     } catch(char *ex) {
3359ae82921SPaul Mullowney       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
3369ae82921SPaul Mullowney     }
3379ae82921SPaul Mullowney   }
3389ae82921SPaul Mullowney   PetscFunctionReturn(0);
3399ae82921SPaul Mullowney }
3409ae82921SPaul Mullowney 
341087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(Mat A)
3429ae82921SPaul Mullowney {
3439ae82921SPaul Mullowney   Mat_SeqAIJ                        *a = (Mat_SeqAIJ*)A->data;
3449ae82921SPaul Mullowney   PetscInt                          n = A->rmap->n;
3459ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
346aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
3479ae82921SPaul Mullowney   cusparseStatus_t                  stat;
3489ae82921SPaul Mullowney   const PetscInt                    *aj = a->j,*adiag = a->diag,*vi;
3499ae82921SPaul Mullowney   const MatScalar                   *aa = a->a,*v;
3509ae82921SPaul Mullowney   PetscInt                          *AiUp, *AjUp;
3519ae82921SPaul Mullowney   PetscScalar                       *AAUp;
3529ae82921SPaul Mullowney   PetscInt                          i,nz, nzUpper, offset;
3539ae82921SPaul Mullowney   PetscErrorCode                    ierr;
3549ae82921SPaul Mullowney 
3559ae82921SPaul Mullowney   PetscFunctionBegin;
356b8ced49eSKarl Rupp   if (A->valid_GPU_matrix == PETSC_OFFLOAD_UNALLOCATED || A->valid_GPU_matrix == PETSC_OFFLOAD_CPU) {
3579ae82921SPaul Mullowney     try {
3589ae82921SPaul Mullowney       /* next, figure out the number of nonzeros in the upper triangular matrix. */
3599ae82921SPaul Mullowney       nzUpper = adiag[0]-adiag[n];
3609ae82921SPaul Mullowney 
3619ae82921SPaul Mullowney       /* Allocate Space for the upper triangular matrix */
362c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr);
363c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(ierr);
364c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr);
3659ae82921SPaul Mullowney 
3669ae82921SPaul Mullowney       /* Fill the upper triangular matrix */
3679ae82921SPaul Mullowney       AiUp[0]=(PetscInt) 0;
3689ae82921SPaul Mullowney       AiUp[n]=nzUpper;
3699ae82921SPaul Mullowney       offset = nzUpper;
3709ae82921SPaul Mullowney       for (i=n-1; i>=0; i--) {
3719ae82921SPaul Mullowney         v  = aa + adiag[i+1] + 1;
3729ae82921SPaul Mullowney         vi = aj + adiag[i+1] + 1;
3739ae82921SPaul Mullowney 
374e057df02SPaul Mullowney         /* number of elements NOT on the diagonal */
3759ae82921SPaul Mullowney         nz = adiag[i] - adiag[i+1]-1;
3769ae82921SPaul Mullowney 
377e057df02SPaul Mullowney         /* decrement the offset */
3789ae82921SPaul Mullowney         offset -= (nz+1);
3799ae82921SPaul Mullowney 
380e057df02SPaul Mullowney         /* first, set the diagonal elements */
3819ae82921SPaul Mullowney         AjUp[offset] = (PetscInt) i;
38209f51544SAlejandro Lamas Daviña         AAUp[offset] = (MatScalar)1./v[nz];
3839ae82921SPaul Mullowney         AiUp[i]      = AiUp[i+1] - (nz+1);
3849ae82921SPaul Mullowney 
385580bdb30SBarry Smith         ierr = PetscArraycpy(&(AjUp[offset+1]), vi, nz);CHKERRQ(ierr);
386580bdb30SBarry Smith         ierr = PetscArraycpy(&(AAUp[offset+1]), v, nz);CHKERRQ(ierr);
3879ae82921SPaul Mullowney       }
3882205254eSKarl Rupp 
389aa372e3fSPaul Mullowney       /* allocate space for the triangular factor information */
390aa372e3fSPaul Mullowney       upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct;
3912205254eSKarl Rupp 
392aa372e3fSPaul Mullowney       /* Create the matrix description */
393c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUDA(stat);
394c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat);
395c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat);
396c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat);
397c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUDA(stat);
398aa372e3fSPaul Mullowney 
399aa372e3fSPaul Mullowney       /* Create the solve analysis information */
400c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUDA(stat);
401aa372e3fSPaul Mullowney 
402aa372e3fSPaul Mullowney       /* set the operation */
403aa372e3fSPaul Mullowney       upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
404aa372e3fSPaul Mullowney 
405aa372e3fSPaul Mullowney       /* set the matrix */
406aa372e3fSPaul Mullowney       upTriFactor->csrMat = new CsrMatrix;
407aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_rows = n;
408aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_cols = n;
409aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_entries = nzUpper;
410aa372e3fSPaul Mullowney 
411aa372e3fSPaul Mullowney       upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1);
412aa372e3fSPaul Mullowney       upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+n+1);
413aa372e3fSPaul Mullowney 
414aa372e3fSPaul Mullowney       upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzUpper);
415aa372e3fSPaul Mullowney       upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+nzUpper);
416aa372e3fSPaul Mullowney 
417aa372e3fSPaul Mullowney       upTriFactor->csrMat->values = new THRUSTARRAY(nzUpper);
418aa372e3fSPaul Mullowney       upTriFactor->csrMat->values->assign(AAUp, AAUp+nzUpper);
419aa372e3fSPaul Mullowney 
420aa372e3fSPaul Mullowney       /* perform the solve analysis */
421aa372e3fSPaul Mullowney       stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp,
422aa372e3fSPaul Mullowney                                upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr,
423aa372e3fSPaul Mullowney                                upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(),
424c41cb2e2SAlejandro Lamas Daviña                                upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUDA(stat);
425aa372e3fSPaul Mullowney 
426aa372e3fSPaul Mullowney       /* assign the pointer. Is this really necessary? */
427aa372e3fSPaul Mullowney       ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor;
4282205254eSKarl Rupp 
429c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AiUp);CHKERRCUDA(ierr);
430c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AjUp);CHKERRCUDA(ierr);
431c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AAUp);CHKERRCUDA(ierr);
4324863603aSSatish Balay       ierr = PetscLogCpuToGpu((n+1+nzUpper)*sizeof(int)+nzUpper*sizeof(PetscScalar));CHKERRQ(ierr);
4339ae82921SPaul Mullowney     } catch(char *ex) {
4349ae82921SPaul Mullowney       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
4359ae82921SPaul Mullowney     }
4369ae82921SPaul Mullowney   }
4379ae82921SPaul Mullowney   PetscFunctionReturn(0);
4389ae82921SPaul Mullowney }
4399ae82921SPaul Mullowney 
440087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(Mat A)
4419ae82921SPaul Mullowney {
4429ae82921SPaul Mullowney   PetscErrorCode               ierr;
4439ae82921SPaul Mullowney   Mat_SeqAIJ                   *a                  = (Mat_SeqAIJ*)A->data;
4449ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
4459ae82921SPaul Mullowney   IS                           isrow = a->row,iscol = a->icol;
4469ae82921SPaul Mullowney   PetscBool                    row_identity,col_identity;
4479ae82921SPaul Mullowney   const PetscInt               *r,*c;
4489ae82921SPaul Mullowney   PetscInt                     n = A->rmap->n;
4499ae82921SPaul Mullowney 
4509ae82921SPaul Mullowney   PetscFunctionBegin;
451087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEBuildILULowerTriMatrix(A);CHKERRQ(ierr);
452087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(A);CHKERRQ(ierr);
4532205254eSKarl Rupp 
454e65717acSKarl Rupp   cusparseTriFactors->workVector = new THRUSTARRAY(n);
455aa372e3fSPaul Mullowney   cusparseTriFactors->nnz=a->nz;
4569ae82921SPaul Mullowney 
457b8ced49eSKarl Rupp   A->valid_GPU_matrix = PETSC_OFFLOAD_BOTH;
458e057df02SPaul Mullowney   /*lower triangular indices */
4599ae82921SPaul Mullowney   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
4609ae82921SPaul Mullowney   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
4612205254eSKarl Rupp   if (!row_identity) {
462aa372e3fSPaul Mullowney     cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n);
463aa372e3fSPaul Mullowney     cusparseTriFactors->rpermIndices->assign(r, r+n);
4642205254eSKarl Rupp   }
4659ae82921SPaul Mullowney   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
4669ae82921SPaul Mullowney 
467e057df02SPaul Mullowney   /*upper triangular indices */
4689ae82921SPaul Mullowney   ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr);
4699ae82921SPaul Mullowney   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
4702205254eSKarl Rupp   if (!col_identity) {
471aa372e3fSPaul Mullowney     cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n);
472aa372e3fSPaul Mullowney     cusparseTriFactors->cpermIndices->assign(c, c+n);
4732205254eSKarl Rupp   }
4744863603aSSatish Balay 
4754863603aSSatish Balay   if(!row_identity && !col_identity) {
4764863603aSSatish Balay     ierr = PetscLogCpuToGpu(2*n*sizeof(PetscInt));CHKERRQ(ierr);
4774863603aSSatish Balay   } else if(!row_identity) {
4784863603aSSatish Balay     ierr = PetscLogCpuToGpu(n*sizeof(PetscInt));CHKERRQ(ierr);
4794863603aSSatish Balay   } else if(!col_identity) {
4804863603aSSatish Balay     ierr = PetscLogCpuToGpu(n*sizeof(PetscInt));CHKERRQ(ierr);
4814863603aSSatish Balay   }
4824863603aSSatish Balay 
4839ae82921SPaul Mullowney   ierr = ISRestoreIndices(iscol,&c);CHKERRQ(ierr);
4849ae82921SPaul Mullowney   PetscFunctionReturn(0);
4859ae82921SPaul Mullowney }
4869ae82921SPaul Mullowney 
487087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildICCTriMatrices(Mat A)
488087f3262SPaul Mullowney {
489087f3262SPaul Mullowney   Mat_SeqAIJ                        *a = (Mat_SeqAIJ*)A->data;
490087f3262SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
491aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
492aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
493087f3262SPaul Mullowney   cusparseStatus_t                  stat;
494087f3262SPaul Mullowney   PetscErrorCode                    ierr;
495087f3262SPaul Mullowney   PetscInt                          *AiUp, *AjUp;
496087f3262SPaul Mullowney   PetscScalar                       *AAUp;
497087f3262SPaul Mullowney   PetscScalar                       *AALo;
498087f3262SPaul Mullowney   PetscInt                          nzUpper = a->nz,n = A->rmap->n,i,offset,nz,j;
499087f3262SPaul Mullowney   Mat_SeqSBAIJ                      *b = (Mat_SeqSBAIJ*)A->data;
500087f3262SPaul Mullowney   const PetscInt                    *ai = b->i,*aj = b->j,*vj;
501087f3262SPaul Mullowney   const MatScalar                   *aa = b->a,*v;
502087f3262SPaul Mullowney 
503087f3262SPaul Mullowney   PetscFunctionBegin;
504b8ced49eSKarl Rupp   if (A->valid_GPU_matrix == PETSC_OFFLOAD_UNALLOCATED || A->valid_GPU_matrix == PETSC_OFFLOAD_CPU) {
505087f3262SPaul Mullowney     try {
506087f3262SPaul Mullowney       /* Allocate Space for the upper triangular matrix */
507c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr);
508c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(ierr);
509c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr);
510c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AALo, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr);
511087f3262SPaul Mullowney 
512087f3262SPaul Mullowney       /* Fill the upper triangular matrix */
513087f3262SPaul Mullowney       AiUp[0]=(PetscInt) 0;
514087f3262SPaul Mullowney       AiUp[n]=nzUpper;
515087f3262SPaul Mullowney       offset = 0;
516087f3262SPaul Mullowney       for (i=0; i<n; i++) {
517087f3262SPaul Mullowney         /* set the pointers */
518087f3262SPaul Mullowney         v  = aa + ai[i];
519087f3262SPaul Mullowney         vj = aj + ai[i];
520087f3262SPaul Mullowney         nz = ai[i+1] - ai[i] - 1; /* exclude diag[i] */
521087f3262SPaul Mullowney 
522087f3262SPaul Mullowney         /* first, set the diagonal elements */
523087f3262SPaul Mullowney         AjUp[offset] = (PetscInt) i;
52409f51544SAlejandro Lamas Daviña         AAUp[offset] = (MatScalar)1.0/v[nz];
525087f3262SPaul Mullowney         AiUp[i]      = offset;
52609f51544SAlejandro Lamas Daviña         AALo[offset] = (MatScalar)1.0/v[nz];
527087f3262SPaul Mullowney 
528087f3262SPaul Mullowney         offset+=1;
529087f3262SPaul Mullowney         if (nz>0) {
530f22e0265SBarry Smith           ierr = PetscArraycpy(&(AjUp[offset]), vj, nz);CHKERRQ(ierr);
531580bdb30SBarry Smith           ierr = PetscArraycpy(&(AAUp[offset]), v, nz);CHKERRQ(ierr);
532087f3262SPaul Mullowney           for (j=offset; j<offset+nz; j++) {
533087f3262SPaul Mullowney             AAUp[j] = -AAUp[j];
534087f3262SPaul Mullowney             AALo[j] = AAUp[j]/v[nz];
535087f3262SPaul Mullowney           }
536087f3262SPaul Mullowney           offset+=nz;
537087f3262SPaul Mullowney         }
538087f3262SPaul Mullowney       }
539087f3262SPaul Mullowney 
540aa372e3fSPaul Mullowney       /* allocate space for the triangular factor information */
541aa372e3fSPaul Mullowney       upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct;
542087f3262SPaul Mullowney 
543aa372e3fSPaul Mullowney       /* Create the matrix description */
544c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUDA(stat);
545c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat);
546c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat);
547c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat);
548c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUDA(stat);
549087f3262SPaul Mullowney 
550aa372e3fSPaul Mullowney       /* Create the solve analysis information */
551c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUDA(stat);
552aa372e3fSPaul Mullowney 
553aa372e3fSPaul Mullowney       /* set the operation */
554aa372e3fSPaul Mullowney       upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
555aa372e3fSPaul Mullowney 
556aa372e3fSPaul Mullowney       /* set the matrix */
557aa372e3fSPaul Mullowney       upTriFactor->csrMat = new CsrMatrix;
558aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_rows = A->rmap->n;
559aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_cols = A->cmap->n;
560aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_entries = a->nz;
561aa372e3fSPaul Mullowney 
562aa372e3fSPaul Mullowney       upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
563aa372e3fSPaul Mullowney       upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1);
564aa372e3fSPaul Mullowney 
565aa372e3fSPaul Mullowney       upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz);
566aa372e3fSPaul Mullowney       upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz);
567aa372e3fSPaul Mullowney 
568aa372e3fSPaul Mullowney       upTriFactor->csrMat->values = new THRUSTARRAY(a->nz);
569aa372e3fSPaul Mullowney       upTriFactor->csrMat->values->assign(AAUp, AAUp+a->nz);
570aa372e3fSPaul Mullowney 
571aa372e3fSPaul Mullowney       /* perform the solve analysis */
572aa372e3fSPaul Mullowney       stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp,
573aa372e3fSPaul Mullowney                                upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr,
574aa372e3fSPaul Mullowney                                upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(),
575c41cb2e2SAlejandro Lamas Daviña                                upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUDA(stat);
576aa372e3fSPaul Mullowney 
577aa372e3fSPaul Mullowney       /* assign the pointer. Is this really necessary? */
578aa372e3fSPaul Mullowney       ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor;
579aa372e3fSPaul Mullowney 
580aa372e3fSPaul Mullowney       /* allocate space for the triangular factor information */
581aa372e3fSPaul Mullowney       loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct;
582aa372e3fSPaul Mullowney 
583aa372e3fSPaul Mullowney       /* Create the matrix description */
584c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUDA(stat);
585c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat);
586c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat);
587c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat);
588c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUDA(stat);
589aa372e3fSPaul Mullowney 
590aa372e3fSPaul Mullowney       /* Create the solve analysis information */
591c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUDA(stat);
592aa372e3fSPaul Mullowney 
593aa372e3fSPaul Mullowney       /* set the operation */
594aa372e3fSPaul Mullowney       loTriFactor->solveOp = CUSPARSE_OPERATION_TRANSPOSE;
595aa372e3fSPaul Mullowney 
596aa372e3fSPaul Mullowney       /* set the matrix */
597aa372e3fSPaul Mullowney       loTriFactor->csrMat = new CsrMatrix;
598aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_rows = A->rmap->n;
599aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_cols = A->cmap->n;
600aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_entries = a->nz;
601aa372e3fSPaul Mullowney 
602aa372e3fSPaul Mullowney       loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
603aa372e3fSPaul Mullowney       loTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1);
604aa372e3fSPaul Mullowney 
605aa372e3fSPaul Mullowney       loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz);
606aa372e3fSPaul Mullowney       loTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz);
607aa372e3fSPaul Mullowney 
608aa372e3fSPaul Mullowney       loTriFactor->csrMat->values = new THRUSTARRAY(a->nz);
609aa372e3fSPaul Mullowney       loTriFactor->csrMat->values->assign(AALo, AALo+a->nz);
6104863603aSSatish Balay       ierr = PetscLogCpuToGpu(2*(((A->rmap->n+1)+(a->nz))*sizeof(int)+(a->nz)*sizeof(PetscScalar)));CHKERRQ(ierr);
611aa372e3fSPaul Mullowney 
612aa372e3fSPaul Mullowney       /* perform the solve analysis */
613aa372e3fSPaul Mullowney       stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp,
614aa372e3fSPaul Mullowney                                loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr,
615aa372e3fSPaul Mullowney                                loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(),
616c41cb2e2SAlejandro Lamas Daviña                                loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUDA(stat);
617aa372e3fSPaul Mullowney 
618aa372e3fSPaul Mullowney       /* assign the pointer. Is this really necessary? */
619aa372e3fSPaul Mullowney       ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor;
620087f3262SPaul Mullowney 
621b8ced49eSKarl Rupp       A->valid_GPU_matrix = PETSC_OFFLOAD_BOTH;
622c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AiUp);CHKERRCUDA(ierr);
623c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AjUp);CHKERRCUDA(ierr);
624c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AAUp);CHKERRCUDA(ierr);
625c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AALo);CHKERRCUDA(ierr);
626087f3262SPaul Mullowney     } catch(char *ex) {
627087f3262SPaul Mullowney       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
628087f3262SPaul Mullowney     }
629087f3262SPaul Mullowney   }
630087f3262SPaul Mullowney   PetscFunctionReturn(0);
631087f3262SPaul Mullowney }
632087f3262SPaul Mullowney 
633087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(Mat A)
6349ae82921SPaul Mullowney {
6359ae82921SPaul Mullowney   PetscErrorCode               ierr;
636087f3262SPaul Mullowney   Mat_SeqAIJ                   *a                  = (Mat_SeqAIJ*)A->data;
637087f3262SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
638087f3262SPaul Mullowney   IS                           ip = a->row;
639087f3262SPaul Mullowney   const PetscInt               *rip;
640087f3262SPaul Mullowney   PetscBool                    perm_identity;
641087f3262SPaul Mullowney   PetscInt                     n = A->rmap->n;
642087f3262SPaul Mullowney 
643087f3262SPaul Mullowney   PetscFunctionBegin;
644087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEBuildICCTriMatrices(A);CHKERRQ(ierr);
645e65717acSKarl Rupp   cusparseTriFactors->workVector = new THRUSTARRAY(n);
646aa372e3fSPaul Mullowney   cusparseTriFactors->nnz=(a->nz-n)*2 + n;
647aa372e3fSPaul Mullowney 
648087f3262SPaul Mullowney   /*lower triangular indices */
649087f3262SPaul Mullowney   ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr);
650087f3262SPaul Mullowney   ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr);
651087f3262SPaul Mullowney   if (!perm_identity) {
652aa372e3fSPaul Mullowney     cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n);
653aa372e3fSPaul Mullowney     cusparseTriFactors->rpermIndices->assign(rip, rip+n);
654aa372e3fSPaul Mullowney     cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n);
655aa372e3fSPaul Mullowney     cusparseTriFactors->cpermIndices->assign(rip, rip+n);
6564863603aSSatish Balay     ierr = PetscLogCpuToGpu(2*n*sizeof(PetscInt));CHKERRQ(ierr);
657087f3262SPaul Mullowney   }
658087f3262SPaul Mullowney   ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr);
659087f3262SPaul Mullowney   PetscFunctionReturn(0);
660087f3262SPaul Mullowney }
661087f3262SPaul Mullowney 
6626fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info)
6639ae82921SPaul Mullowney {
6649ae82921SPaul Mullowney   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
6659ae82921SPaul Mullowney   IS             isrow = b->row,iscol = b->col;
6669ae82921SPaul Mullowney   PetscBool      row_identity,col_identity;
667b175d8bbSPaul Mullowney   PetscErrorCode ierr;
6689ae82921SPaul Mullowney 
6699ae82921SPaul Mullowney   PetscFunctionBegin;
6709ae82921SPaul Mullowney   ierr = MatLUFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr);
671e057df02SPaul Mullowney   /* determine which version of MatSolve needs to be used. */
6729ae82921SPaul Mullowney   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
6739ae82921SPaul Mullowney   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
674bda325fcSPaul Mullowney   if (row_identity && col_identity) {
675bda325fcSPaul Mullowney     B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering;
676bda325fcSPaul Mullowney     B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering;
677bda325fcSPaul Mullowney   } else {
678bda325fcSPaul Mullowney     B->ops->solve = MatSolve_SeqAIJCUSPARSE;
679bda325fcSPaul Mullowney     B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE;
680bda325fcSPaul Mullowney   }
6818dc1d2a3SPaul Mullowney 
682e057df02SPaul Mullowney   /* get the triangular factors */
683087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(B);CHKERRQ(ierr);
6849ae82921SPaul Mullowney   PetscFunctionReturn(0);
6859ae82921SPaul Mullowney }
6869ae82921SPaul Mullowney 
687087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info)
688087f3262SPaul Mullowney {
689087f3262SPaul Mullowney   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
690087f3262SPaul Mullowney   IS             ip = b->row;
691087f3262SPaul Mullowney   PetscBool      perm_identity;
692b175d8bbSPaul Mullowney   PetscErrorCode ierr;
693087f3262SPaul Mullowney 
694087f3262SPaul Mullowney   PetscFunctionBegin;
695087f3262SPaul Mullowney   ierr = MatCholeskyFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr);
696087f3262SPaul Mullowney 
697087f3262SPaul Mullowney   /* determine which version of MatSolve needs to be used. */
698087f3262SPaul Mullowney   ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr);
699087f3262SPaul Mullowney   if (perm_identity) {
700087f3262SPaul Mullowney     B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering;
701087f3262SPaul Mullowney     B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering;
702087f3262SPaul Mullowney   } else {
703087f3262SPaul Mullowney     B->ops->solve = MatSolve_SeqAIJCUSPARSE;
704087f3262SPaul Mullowney     B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE;
705087f3262SPaul Mullowney   }
706087f3262SPaul Mullowney 
707087f3262SPaul Mullowney   /* get the triangular factors */
708087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(B);CHKERRQ(ierr);
709087f3262SPaul Mullowney   PetscFunctionReturn(0);
710087f3262SPaul Mullowney }
7119ae82921SPaul Mullowney 
712b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(Mat A)
713bda325fcSPaul Mullowney {
714bda325fcSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
715aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
716aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
717aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
718aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
719bda325fcSPaul Mullowney   cusparseStatus_t                  stat;
720aa372e3fSPaul Mullowney   cusparseIndexBase_t               indexBase;
721aa372e3fSPaul Mullowney   cusparseMatrixType_t              matrixType;
722aa372e3fSPaul Mullowney   cusparseFillMode_t                fillMode;
723aa372e3fSPaul Mullowney   cusparseDiagType_t                diagType;
724b175d8bbSPaul Mullowney 
725bda325fcSPaul Mullowney   PetscFunctionBegin;
726bda325fcSPaul Mullowney 
727aa372e3fSPaul Mullowney   /*********************************************/
728aa372e3fSPaul Mullowney   /* Now the Transpose of the Lower Tri Factor */
729aa372e3fSPaul Mullowney   /*********************************************/
730aa372e3fSPaul Mullowney 
731aa372e3fSPaul Mullowney   /* allocate space for the transpose of the lower triangular factor */
732aa372e3fSPaul Mullowney   loTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct;
733aa372e3fSPaul Mullowney 
734aa372e3fSPaul Mullowney   /* set the matrix descriptors of the lower triangular factor */
735aa372e3fSPaul Mullowney   matrixType = cusparseGetMatType(loTriFactor->descr);
736aa372e3fSPaul Mullowney   indexBase = cusparseGetMatIndexBase(loTriFactor->descr);
737aa372e3fSPaul Mullowney   fillMode = cusparseGetMatFillMode(loTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ?
738aa372e3fSPaul Mullowney     CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER;
739aa372e3fSPaul Mullowney   diagType = cusparseGetMatDiagType(loTriFactor->descr);
740aa372e3fSPaul Mullowney 
741aa372e3fSPaul Mullowney   /* Create the matrix description */
742c41cb2e2SAlejandro Lamas Daviña   stat = cusparseCreateMatDescr(&loTriFactorT->descr);CHKERRCUDA(stat);
743c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatIndexBase(loTriFactorT->descr, indexBase);CHKERRCUDA(stat);
744c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatType(loTriFactorT->descr, matrixType);CHKERRCUDA(stat);
745c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatFillMode(loTriFactorT->descr, fillMode);CHKERRCUDA(stat);
746c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatDiagType(loTriFactorT->descr, diagType);CHKERRCUDA(stat);
747aa372e3fSPaul Mullowney 
748aa372e3fSPaul Mullowney   /* Create the solve analysis information */
749c41cb2e2SAlejandro Lamas Daviña   stat = cusparseCreateSolveAnalysisInfo(&loTriFactorT->solveInfo);CHKERRCUDA(stat);
750aa372e3fSPaul Mullowney 
751aa372e3fSPaul Mullowney   /* set the operation */
752aa372e3fSPaul Mullowney   loTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
753aa372e3fSPaul Mullowney 
754aa372e3fSPaul Mullowney   /* allocate GPU space for the CSC of the lower triangular factor*/
755aa372e3fSPaul Mullowney   loTriFactorT->csrMat = new CsrMatrix;
756aa372e3fSPaul Mullowney   loTriFactorT->csrMat->num_rows = loTriFactor->csrMat->num_rows;
757aa372e3fSPaul Mullowney   loTriFactorT->csrMat->num_cols = loTriFactor->csrMat->num_cols;
758aa372e3fSPaul Mullowney   loTriFactorT->csrMat->num_entries = loTriFactor->csrMat->num_entries;
759aa372e3fSPaul Mullowney   loTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(loTriFactor->csrMat->num_rows+1);
760aa372e3fSPaul Mullowney   loTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(loTriFactor->csrMat->num_entries);
761aa372e3fSPaul Mullowney   loTriFactorT->csrMat->values = new THRUSTARRAY(loTriFactor->csrMat->num_entries);
762aa372e3fSPaul Mullowney 
763aa372e3fSPaul Mullowney   /* compute the transpose of the lower triangular factor, i.e. the CSC */
764aa372e3fSPaul Mullowney   stat = cusparse_csr2csc(cusparseTriFactors->handle, loTriFactor->csrMat->num_rows,
765aa372e3fSPaul Mullowney                           loTriFactor->csrMat->num_cols, loTriFactor->csrMat->num_entries,
766aa372e3fSPaul Mullowney                           loTriFactor->csrMat->values->data().get(),
767aa372e3fSPaul Mullowney                           loTriFactor->csrMat->row_offsets->data().get(),
768aa372e3fSPaul Mullowney                           loTriFactor->csrMat->column_indices->data().get(),
769aa372e3fSPaul Mullowney                           loTriFactorT->csrMat->values->data().get(),
770aa372e3fSPaul Mullowney                           loTriFactorT->csrMat->column_indices->data().get(),
771aa372e3fSPaul Mullowney                           loTriFactorT->csrMat->row_offsets->data().get(),
772c41cb2e2SAlejandro Lamas Daviña                           CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat);
773aa372e3fSPaul Mullowney 
774aa372e3fSPaul Mullowney   /* perform the solve analysis on the transposed matrix */
775aa372e3fSPaul Mullowney   stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactorT->solveOp,
776aa372e3fSPaul Mullowney                            loTriFactorT->csrMat->num_rows, loTriFactorT->csrMat->num_entries,
777aa372e3fSPaul Mullowney                            loTriFactorT->descr, loTriFactorT->csrMat->values->data().get(),
778aa372e3fSPaul Mullowney                            loTriFactorT->csrMat->row_offsets->data().get(), loTriFactorT->csrMat->column_indices->data().get(),
779c41cb2e2SAlejandro Lamas Daviña                            loTriFactorT->solveInfo);CHKERRCUDA(stat);
780aa372e3fSPaul Mullowney 
781aa372e3fSPaul Mullowney   /* assign the pointer. Is this really necessary? */
782aa372e3fSPaul Mullowney   ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtrTranspose = loTriFactorT;
783aa372e3fSPaul Mullowney 
784aa372e3fSPaul Mullowney   /*********************************************/
785aa372e3fSPaul Mullowney   /* Now the Transpose of the Upper Tri Factor */
786aa372e3fSPaul Mullowney   /*********************************************/
787aa372e3fSPaul Mullowney 
788aa372e3fSPaul Mullowney   /* allocate space for the transpose of the upper triangular factor */
789aa372e3fSPaul Mullowney   upTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct;
790aa372e3fSPaul Mullowney 
791aa372e3fSPaul Mullowney   /* set the matrix descriptors of the upper triangular factor */
792aa372e3fSPaul Mullowney   matrixType = cusparseGetMatType(upTriFactor->descr);
793aa372e3fSPaul Mullowney   indexBase = cusparseGetMatIndexBase(upTriFactor->descr);
794aa372e3fSPaul Mullowney   fillMode = cusparseGetMatFillMode(upTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ?
795aa372e3fSPaul Mullowney     CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER;
796aa372e3fSPaul Mullowney   diagType = cusparseGetMatDiagType(upTriFactor->descr);
797aa372e3fSPaul Mullowney 
798aa372e3fSPaul Mullowney   /* Create the matrix description */
799c41cb2e2SAlejandro Lamas Daviña   stat = cusparseCreateMatDescr(&upTriFactorT->descr);CHKERRCUDA(stat);
800c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatIndexBase(upTriFactorT->descr, indexBase);CHKERRCUDA(stat);
801c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatType(upTriFactorT->descr, matrixType);CHKERRCUDA(stat);
802c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatFillMode(upTriFactorT->descr, fillMode);CHKERRCUDA(stat);
803c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatDiagType(upTriFactorT->descr, diagType);CHKERRCUDA(stat);
804aa372e3fSPaul Mullowney 
805aa372e3fSPaul Mullowney   /* Create the solve analysis information */
806c41cb2e2SAlejandro Lamas Daviña   stat = cusparseCreateSolveAnalysisInfo(&upTriFactorT->solveInfo);CHKERRCUDA(stat);
807aa372e3fSPaul Mullowney 
808aa372e3fSPaul Mullowney   /* set the operation */
809aa372e3fSPaul Mullowney   upTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
810aa372e3fSPaul Mullowney 
811aa372e3fSPaul Mullowney   /* allocate GPU space for the CSC of the upper triangular factor*/
812aa372e3fSPaul Mullowney   upTriFactorT->csrMat = new CsrMatrix;
813aa372e3fSPaul Mullowney   upTriFactorT->csrMat->num_rows = upTriFactor->csrMat->num_rows;
814aa372e3fSPaul Mullowney   upTriFactorT->csrMat->num_cols = upTriFactor->csrMat->num_cols;
815aa372e3fSPaul Mullowney   upTriFactorT->csrMat->num_entries = upTriFactor->csrMat->num_entries;
816aa372e3fSPaul Mullowney   upTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(upTriFactor->csrMat->num_rows+1);
817aa372e3fSPaul Mullowney   upTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(upTriFactor->csrMat->num_entries);
818aa372e3fSPaul Mullowney   upTriFactorT->csrMat->values = new THRUSTARRAY(upTriFactor->csrMat->num_entries);
819aa372e3fSPaul Mullowney 
820aa372e3fSPaul Mullowney   /* compute the transpose of the upper triangular factor, i.e. the CSC */
821aa372e3fSPaul Mullowney   stat = cusparse_csr2csc(cusparseTriFactors->handle, upTriFactor->csrMat->num_rows,
822aa372e3fSPaul Mullowney                           upTriFactor->csrMat->num_cols, upTriFactor->csrMat->num_entries,
823aa372e3fSPaul Mullowney                           upTriFactor->csrMat->values->data().get(),
824aa372e3fSPaul Mullowney                           upTriFactor->csrMat->row_offsets->data().get(),
825aa372e3fSPaul Mullowney                           upTriFactor->csrMat->column_indices->data().get(),
826aa372e3fSPaul Mullowney                           upTriFactorT->csrMat->values->data().get(),
827aa372e3fSPaul Mullowney                           upTriFactorT->csrMat->column_indices->data().get(),
828aa372e3fSPaul Mullowney                           upTriFactorT->csrMat->row_offsets->data().get(),
829c41cb2e2SAlejandro Lamas Daviña                           CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat);
830aa372e3fSPaul Mullowney 
831aa372e3fSPaul Mullowney   /* perform the solve analysis on the transposed matrix */
832aa372e3fSPaul Mullowney   stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactorT->solveOp,
833aa372e3fSPaul Mullowney                            upTriFactorT->csrMat->num_rows, upTriFactorT->csrMat->num_entries,
834aa372e3fSPaul Mullowney                            upTriFactorT->descr, upTriFactorT->csrMat->values->data().get(),
835aa372e3fSPaul Mullowney                            upTriFactorT->csrMat->row_offsets->data().get(), upTriFactorT->csrMat->column_indices->data().get(),
836c41cb2e2SAlejandro Lamas Daviña                            upTriFactorT->solveInfo);CHKERRCUDA(stat);
837aa372e3fSPaul Mullowney 
838aa372e3fSPaul Mullowney   /* assign the pointer. Is this really necessary? */
839aa372e3fSPaul Mullowney   ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtrTranspose = upTriFactorT;
840bda325fcSPaul Mullowney   PetscFunctionReturn(0);
841bda325fcSPaul Mullowney }
842bda325fcSPaul Mullowney 
843b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEGenerateTransposeForMult(Mat A)
844bda325fcSPaul Mullowney {
845aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE           *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
846aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat;
847aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
848bda325fcSPaul Mullowney   Mat_SeqAIJ                   *a = (Mat_SeqAIJ*)A->data;
849bda325fcSPaul Mullowney   cusparseStatus_t             stat;
850aa372e3fSPaul Mullowney   cusparseIndexBase_t          indexBase;
851b06137fdSPaul Mullowney   cudaError_t                  err;
8524863603aSSatish Balay   PetscErrorCode               ierr;
853b175d8bbSPaul Mullowney 
854bda325fcSPaul Mullowney   PetscFunctionBegin;
855aa372e3fSPaul Mullowney 
856aa372e3fSPaul Mullowney   /* allocate space for the triangular factor information */
857aa372e3fSPaul Mullowney   matstructT = new Mat_SeqAIJCUSPARSEMultStruct;
858c41cb2e2SAlejandro Lamas Daviña   stat = cusparseCreateMatDescr(&matstructT->descr);CHKERRCUDA(stat);
859aa372e3fSPaul Mullowney   indexBase = cusparseGetMatIndexBase(matstruct->descr);
860c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatIndexBase(matstructT->descr, indexBase);CHKERRCUDA(stat);
861c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatType(matstructT->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUDA(stat);
862aa372e3fSPaul Mullowney 
863b06137fdSPaul Mullowney   /* set alpha and beta */
864c41cb2e2SAlejandro Lamas Daviña   err = cudaMalloc((void **)&(matstructT->alpha),    sizeof(PetscScalar));CHKERRCUDA(err);
8657656d835SStefano Zampini   err = cudaMalloc((void **)&(matstructT->beta_zero),sizeof(PetscScalar));CHKERRCUDA(err);
8667656d835SStefano Zampini   err = cudaMalloc((void **)&(matstructT->beta_one), sizeof(PetscScalar));CHKERRCUDA(err);
8677656d835SStefano Zampini   err = cudaMemcpy(matstructT->alpha,    &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
8687656d835SStefano Zampini   err = cudaMemcpy(matstructT->beta_zero,&PETSC_CUSPARSE_ZERO,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
8697656d835SStefano Zampini   err = cudaMemcpy(matstructT->beta_one, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
870c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat);
871b06137fdSPaul Mullowney 
872aa372e3fSPaul Mullowney   if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
873aa372e3fSPaul Mullowney     CsrMatrix *matrix = (CsrMatrix*)matstruct->mat;
874aa372e3fSPaul Mullowney     CsrMatrix *matrixT= new CsrMatrix;
875554b8892SKarl Rupp     matrixT->num_rows = A->cmap->n;
876554b8892SKarl Rupp     matrixT->num_cols = A->rmap->n;
877aa372e3fSPaul Mullowney     matrixT->num_entries = a->nz;
878aa372e3fSPaul Mullowney     matrixT->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
879aa372e3fSPaul Mullowney     matrixT->column_indices = new THRUSTINTARRAY32(a->nz);
880aa372e3fSPaul Mullowney     matrixT->values = new THRUSTARRAY(a->nz);
881aa372e3fSPaul Mullowney 
882aa372e3fSPaul Mullowney     /* compute the transpose of the upper triangular factor, i.e. the CSC */
883aa372e3fSPaul Mullowney     indexBase = cusparseGetMatIndexBase(matstruct->descr);
884aa372e3fSPaul Mullowney     stat = cusparse_csr2csc(cusparsestruct->handle, matrix->num_rows,
885aa372e3fSPaul Mullowney                             matrix->num_cols, matrix->num_entries,
886aa372e3fSPaul Mullowney                             matrix->values->data().get(),
887aa372e3fSPaul Mullowney                             matrix->row_offsets->data().get(),
888aa372e3fSPaul Mullowney                             matrix->column_indices->data().get(),
889aa372e3fSPaul Mullowney                             matrixT->values->data().get(),
890aa372e3fSPaul Mullowney                             matrixT->column_indices->data().get(),
891aa372e3fSPaul Mullowney                             matrixT->row_offsets->data().get(),
892c41cb2e2SAlejandro Lamas Daviña                             CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat);
893aa372e3fSPaul Mullowney 
894aa372e3fSPaul Mullowney     /* assign the pointer */
895aa372e3fSPaul Mullowney     matstructT->mat = matrixT;
8964863603aSSatish Balay     ierr = PetscLogCpuToGpu(((A->rmap->n+1)+(a->nz))*sizeof(int)+(3+a->nz)*sizeof(PetscScalar));CHKERRQ(ierr);
897aa372e3fSPaul Mullowney   } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) {
8982692e278SPaul Mullowney #if CUDA_VERSION>=5000
899aa372e3fSPaul Mullowney     /* First convert HYB to CSR */
900aa372e3fSPaul Mullowney     CsrMatrix *temp= new CsrMatrix;
901aa372e3fSPaul Mullowney     temp->num_rows = A->rmap->n;
902aa372e3fSPaul Mullowney     temp->num_cols = A->cmap->n;
903aa372e3fSPaul Mullowney     temp->num_entries = a->nz;
904aa372e3fSPaul Mullowney     temp->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
905aa372e3fSPaul Mullowney     temp->column_indices = new THRUSTINTARRAY32(a->nz);
906aa372e3fSPaul Mullowney     temp->values = new THRUSTARRAY(a->nz);
907aa372e3fSPaul Mullowney 
9082692e278SPaul Mullowney 
909aa372e3fSPaul Mullowney     stat = cusparse_hyb2csr(cusparsestruct->handle,
910aa372e3fSPaul Mullowney                             matstruct->descr, (cusparseHybMat_t)matstruct->mat,
911aa372e3fSPaul Mullowney                             temp->values->data().get(),
912aa372e3fSPaul Mullowney                             temp->row_offsets->data().get(),
913c41cb2e2SAlejandro Lamas Daviña                             temp->column_indices->data().get());CHKERRCUDA(stat);
914aa372e3fSPaul Mullowney 
915aa372e3fSPaul Mullowney     /* Next, convert CSR to CSC (i.e. the matrix transpose) */
916aa372e3fSPaul Mullowney     CsrMatrix *tempT= new CsrMatrix;
917aa372e3fSPaul Mullowney     tempT->num_rows = A->rmap->n;
918aa372e3fSPaul Mullowney     tempT->num_cols = A->cmap->n;
919aa372e3fSPaul Mullowney     tempT->num_entries = a->nz;
920aa372e3fSPaul Mullowney     tempT->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
921aa372e3fSPaul Mullowney     tempT->column_indices = new THRUSTINTARRAY32(a->nz);
922aa372e3fSPaul Mullowney     tempT->values = new THRUSTARRAY(a->nz);
923aa372e3fSPaul Mullowney 
924aa372e3fSPaul Mullowney     stat = cusparse_csr2csc(cusparsestruct->handle, temp->num_rows,
925aa372e3fSPaul Mullowney                             temp->num_cols, temp->num_entries,
926aa372e3fSPaul Mullowney                             temp->values->data().get(),
927aa372e3fSPaul Mullowney                             temp->row_offsets->data().get(),
928aa372e3fSPaul Mullowney                             temp->column_indices->data().get(),
929aa372e3fSPaul Mullowney                             tempT->values->data().get(),
930aa372e3fSPaul Mullowney                             tempT->column_indices->data().get(),
931aa372e3fSPaul Mullowney                             tempT->row_offsets->data().get(),
932c41cb2e2SAlejandro Lamas Daviña                             CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat);
933aa372e3fSPaul Mullowney 
934aa372e3fSPaul Mullowney     /* Last, convert CSC to HYB */
935aa372e3fSPaul Mullowney     cusparseHybMat_t hybMat;
936c41cb2e2SAlejandro Lamas Daviña     stat = cusparseCreateHybMat(&hybMat);CHKERRCUDA(stat);
937aa372e3fSPaul Mullowney     cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ?
938aa372e3fSPaul Mullowney       CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO;
939aa372e3fSPaul Mullowney     stat = cusparse_csr2hyb(cusparsestruct->handle, A->rmap->n, A->cmap->n,
940aa372e3fSPaul Mullowney                             matstructT->descr, tempT->values->data().get(),
941aa372e3fSPaul Mullowney                             tempT->row_offsets->data().get(),
942aa372e3fSPaul Mullowney                             tempT->column_indices->data().get(),
943c41cb2e2SAlejandro Lamas Daviña                             hybMat, 0, partition);CHKERRCUDA(stat);
944aa372e3fSPaul Mullowney 
945aa372e3fSPaul Mullowney     /* assign the pointer */
946aa372e3fSPaul Mullowney     matstructT->mat = hybMat;
9474863603aSSatish Balay     ierr = PetscLogCpuToGpu((2*(((A->rmap->n+1)+(a->nz))*sizeof(int)+(a->nz)*sizeof(PetscScalar)))+3*sizeof(PetscScalar));CHKERRQ(ierr);
948aa372e3fSPaul Mullowney 
949aa372e3fSPaul Mullowney     /* delete temporaries */
950aa372e3fSPaul Mullowney     if (tempT) {
951aa372e3fSPaul Mullowney       if (tempT->values) delete (THRUSTARRAY*) tempT->values;
952aa372e3fSPaul Mullowney       if (tempT->column_indices) delete (THRUSTINTARRAY32*) tempT->column_indices;
953aa372e3fSPaul Mullowney       if (tempT->row_offsets) delete (THRUSTINTARRAY32*) tempT->row_offsets;
954aa372e3fSPaul Mullowney       delete (CsrMatrix*) tempT;
955087f3262SPaul Mullowney     }
956aa372e3fSPaul Mullowney     if (temp) {
957aa372e3fSPaul Mullowney       if (temp->values) delete (THRUSTARRAY*) temp->values;
958aa372e3fSPaul Mullowney       if (temp->column_indices) delete (THRUSTINTARRAY32*) temp->column_indices;
959aa372e3fSPaul Mullowney       if (temp->row_offsets) delete (THRUSTINTARRAY32*) temp->row_offsets;
960aa372e3fSPaul Mullowney       delete (CsrMatrix*) temp;
961aa372e3fSPaul Mullowney     }
9622692e278SPaul Mullowney #else
9632692e278SPaul Mullowney     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"ELL (Ellpack) and HYB (Hybrid) storage format for the Matrix Transpose (in MatMultTranspose) require CUDA 5.0 or later.");
9642692e278SPaul Mullowney #endif
965aa372e3fSPaul Mullowney   }
966aa372e3fSPaul Mullowney   /* assign the compressed row indices */
967aa372e3fSPaul Mullowney   matstructT->cprowIndices = new THRUSTINTARRAY;
968554b8892SKarl Rupp   matstructT->cprowIndices->resize(A->cmap->n);
969554b8892SKarl Rupp   thrust::sequence(matstructT->cprowIndices->begin(), matstructT->cprowIndices->end());
970aa372e3fSPaul Mullowney   /* assign the pointer */
971aa372e3fSPaul Mullowney   ((Mat_SeqAIJCUSPARSE*)A->spptr)->matTranspose = matstructT;
972bda325fcSPaul Mullowney   PetscFunctionReturn(0);
973bda325fcSPaul Mullowney }
974bda325fcSPaul Mullowney 
9756fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx)
976bda325fcSPaul Mullowney {
977c41cb2e2SAlejandro Lamas Daviña   PetscInt                              n = xx->map->n;
978465f34aeSAlejandro Lamas Daviña   const PetscScalar                     *barray;
979465f34aeSAlejandro Lamas Daviña   PetscScalar                           *xarray;
980465f34aeSAlejandro Lamas Daviña   thrust::device_ptr<const PetscScalar> bGPU;
981465f34aeSAlejandro Lamas Daviña   thrust::device_ptr<PetscScalar>       xGPU;
982bda325fcSPaul Mullowney   cusparseStatus_t                      stat;
983bda325fcSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors          *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
984aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct     *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
985aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct     *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
986aa372e3fSPaul Mullowney   THRUSTARRAY                           *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector;
987b175d8bbSPaul Mullowney   PetscErrorCode                        ierr;
988bda325fcSPaul Mullowney 
989bda325fcSPaul Mullowney   PetscFunctionBegin;
990aa372e3fSPaul Mullowney   /* Analyze the matrix and create the transpose ... on the fly */
991aa372e3fSPaul Mullowney   if (!loTriFactorT && !upTriFactorT) {
992bda325fcSPaul Mullowney     ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr);
993aa372e3fSPaul Mullowney     loTriFactorT       = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
994aa372e3fSPaul Mullowney     upTriFactorT       = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
995bda325fcSPaul Mullowney   }
996bda325fcSPaul Mullowney 
997bda325fcSPaul Mullowney   /* Get the GPU pointers */
998c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr);
999c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr);
1000c41cb2e2SAlejandro Lamas Daviña   xGPU = thrust::device_pointer_cast(xarray);
1001c41cb2e2SAlejandro Lamas Daviña   bGPU = thrust::device_pointer_cast(barray);
1002bda325fcSPaul Mullowney 
10037a052e47Shannah_mairs   ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
1004aa372e3fSPaul Mullowney   /* First, reorder with the row permutation */
1005c41cb2e2SAlejandro Lamas Daviña   thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()),
1006c41cb2e2SAlejandro Lamas Daviña                thrust::make_permutation_iterator(bGPU+n, cusparseTriFactors->rpermIndices->end()),
1007c41cb2e2SAlejandro Lamas Daviña                xGPU);
1008aa372e3fSPaul Mullowney 
1009aa372e3fSPaul Mullowney   /* First, solve U */
1010aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp,
10117656d835SStefano Zampini                         upTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactorT->descr,
1012aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->values->data().get(),
1013aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->row_offsets->data().get(),
1014aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->column_indices->data().get(),
1015aa372e3fSPaul Mullowney                         upTriFactorT->solveInfo,
1016c41cb2e2SAlejandro Lamas Daviña                         xarray, tempGPU->data().get());CHKERRCUDA(stat);
1017aa372e3fSPaul Mullowney 
1018aa372e3fSPaul Mullowney   /* Then, solve L */
1019aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp,
10207656d835SStefano Zampini                         loTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactorT->descr,
1021aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->values->data().get(),
1022aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->row_offsets->data().get(),
1023aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->column_indices->data().get(),
1024aa372e3fSPaul Mullowney                         loTriFactorT->solveInfo,
1025c41cb2e2SAlejandro Lamas Daviña                         tempGPU->data().get(), xarray);CHKERRCUDA(stat);
1026aa372e3fSPaul Mullowney 
1027aa372e3fSPaul Mullowney   /* Last, copy the solution, xGPU, into a temporary with the column permutation ... can't be done in place. */
1028c41cb2e2SAlejandro Lamas Daviña   thrust::copy(thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->begin()),
1029c41cb2e2SAlejandro Lamas Daviña                thrust::make_permutation_iterator(xGPU+n, cusparseTriFactors->cpermIndices->end()),
1030aa372e3fSPaul Mullowney                tempGPU->begin());
1031aa372e3fSPaul Mullowney 
1032aa372e3fSPaul Mullowney   /* Copy the temporary to the full solution. */
1033c41cb2e2SAlejandro Lamas Daviña   thrust::copy(tempGPU->begin(), tempGPU->end(), xGPU);
1034ce7cfea0Shannah_mairs   ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1035bda325fcSPaul Mullowney 
1036bda325fcSPaul Mullowney   /* restore */
1037c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr);
1038c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr);
1039c41cb2e2SAlejandro Lamas Daviña   ierr = WaitForGPU();CHKERRCUDA(ierr);
1040958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr);
1041bda325fcSPaul Mullowney   PetscFunctionReturn(0);
1042bda325fcSPaul Mullowney }
1043bda325fcSPaul Mullowney 
10446fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx)
1045bda325fcSPaul Mullowney {
1046465f34aeSAlejandro Lamas Daviña   const PetscScalar                 *barray;
1047465f34aeSAlejandro Lamas Daviña   PetscScalar                       *xarray;
1048bda325fcSPaul Mullowney   cusparseStatus_t                  stat;
1049bda325fcSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
1050aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
1051aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
1052aa372e3fSPaul Mullowney   THRUSTARRAY                       *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector;
1053b175d8bbSPaul Mullowney   PetscErrorCode                    ierr;
1054bda325fcSPaul Mullowney 
1055bda325fcSPaul Mullowney   PetscFunctionBegin;
1056aa372e3fSPaul Mullowney   /* Analyze the matrix and create the transpose ... on the fly */
1057aa372e3fSPaul Mullowney   if (!loTriFactorT && !upTriFactorT) {
1058bda325fcSPaul Mullowney     ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr);
1059aa372e3fSPaul Mullowney     loTriFactorT       = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
1060aa372e3fSPaul Mullowney     upTriFactorT       = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
1061bda325fcSPaul Mullowney   }
1062bda325fcSPaul Mullowney 
1063bda325fcSPaul Mullowney   /* Get the GPU pointers */
1064c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr);
1065c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr);
1066bda325fcSPaul Mullowney 
10677a052e47Shannah_mairs   ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
1068aa372e3fSPaul Mullowney   /* First, solve U */
1069aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp,
10707656d835SStefano Zampini                         upTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactorT->descr,
1071aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->values->data().get(),
1072aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->row_offsets->data().get(),
1073aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->column_indices->data().get(),
1074aa372e3fSPaul Mullowney                         upTriFactorT->solveInfo,
1075c41cb2e2SAlejandro Lamas Daviña                         barray, tempGPU->data().get());CHKERRCUDA(stat);
1076aa372e3fSPaul Mullowney 
1077aa372e3fSPaul Mullowney   /* Then, solve L */
1078aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp,
10797656d835SStefano Zampini                         loTriFactorT->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactorT->descr,
1080aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->values->data().get(),
1081aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->row_offsets->data().get(),
1082aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->column_indices->data().get(),
1083aa372e3fSPaul Mullowney                         loTriFactorT->solveInfo,
1084c41cb2e2SAlejandro Lamas Daviña                         tempGPU->data().get(), xarray);CHKERRCUDA(stat);
1085958c4211Shannah_mairs   ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1086bda325fcSPaul Mullowney 
1087bda325fcSPaul Mullowney   /* restore */
1088c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr);
1089c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr);
1090c41cb2e2SAlejandro Lamas Daviña   ierr = WaitForGPU();CHKERRCUDA(ierr);
1091958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr);
1092bda325fcSPaul Mullowney   PetscFunctionReturn(0);
1093bda325fcSPaul Mullowney }
1094bda325fcSPaul Mullowney 
10956fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx)
10969ae82921SPaul Mullowney {
1097465f34aeSAlejandro Lamas Daviña   const PetscScalar                     *barray;
1098465f34aeSAlejandro Lamas Daviña   PetscScalar                           *xarray;
1099465f34aeSAlejandro Lamas Daviña   thrust::device_ptr<const PetscScalar> bGPU;
1100465f34aeSAlejandro Lamas Daviña   thrust::device_ptr<PetscScalar>       xGPU;
11019ae82921SPaul Mullowney   cusparseStatus_t                      stat;
11029ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors          *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
1103aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct     *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
1104aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct     *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
1105aa372e3fSPaul Mullowney   THRUSTARRAY                           *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector;
1106b175d8bbSPaul Mullowney   PetscErrorCode                        ierr;
11079ae82921SPaul Mullowney 
11089ae82921SPaul Mullowney   PetscFunctionBegin;
1109ebc8f436SDominic Meiser 
1110e057df02SPaul Mullowney   /* Get the GPU pointers */
1111c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr);
1112c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr);
1113c41cb2e2SAlejandro Lamas Daviña   xGPU = thrust::device_pointer_cast(xarray);
1114c41cb2e2SAlejandro Lamas Daviña   bGPU = thrust::device_pointer_cast(barray);
11159ae82921SPaul Mullowney 
11167a052e47Shannah_mairs   ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
1117aa372e3fSPaul Mullowney   /* First, reorder with the row permutation */
1118c41cb2e2SAlejandro Lamas Daviña   thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()),
1119c41cb2e2SAlejandro Lamas Daviña                thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->end()),
1120c41cb2e2SAlejandro Lamas Daviña                xGPU);
1121aa372e3fSPaul Mullowney 
1122aa372e3fSPaul Mullowney   /* Next, solve L */
1123aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp,
11247656d835SStefano Zampini                         loTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactor->descr,
1125aa372e3fSPaul Mullowney                         loTriFactor->csrMat->values->data().get(),
1126aa372e3fSPaul Mullowney                         loTriFactor->csrMat->row_offsets->data().get(),
1127aa372e3fSPaul Mullowney                         loTriFactor->csrMat->column_indices->data().get(),
1128aa372e3fSPaul Mullowney                         loTriFactor->solveInfo,
1129c41cb2e2SAlejandro Lamas Daviña                         xarray, tempGPU->data().get());CHKERRCUDA(stat);
1130aa372e3fSPaul Mullowney 
1131aa372e3fSPaul Mullowney   /* Then, solve U */
1132aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp,
11337656d835SStefano Zampini                         upTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactor->descr,
1134aa372e3fSPaul Mullowney                         upTriFactor->csrMat->values->data().get(),
1135aa372e3fSPaul Mullowney                         upTriFactor->csrMat->row_offsets->data().get(),
1136aa372e3fSPaul Mullowney                         upTriFactor->csrMat->column_indices->data().get(),
1137aa372e3fSPaul Mullowney                         upTriFactor->solveInfo,
1138c41cb2e2SAlejandro Lamas Daviña                         tempGPU->data().get(), xarray);CHKERRCUDA(stat);
1139958c4211Shannah_mairs   ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1140aa372e3fSPaul Mullowney 
1141aa372e3fSPaul Mullowney   /* Last, copy the solution, xGPU, into a temporary with the column permutation ... can't be done in place. */
1142c41cb2e2SAlejandro Lamas Daviña   thrust::copy(thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->begin()),
1143c41cb2e2SAlejandro Lamas Daviña                thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->end()),
1144aa372e3fSPaul Mullowney                tempGPU->begin());
1145aa372e3fSPaul Mullowney 
1146aa372e3fSPaul Mullowney   /* Copy the temporary to the full solution. */
1147c41cb2e2SAlejandro Lamas Daviña   thrust::copy(tempGPU->begin(), tempGPU->end(), xGPU);
11489ae82921SPaul Mullowney 
1149c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr);
1150c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr);
1151c41cb2e2SAlejandro Lamas Daviña   ierr = WaitForGPU();CHKERRCUDA(ierr);
1152958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr);
11539ae82921SPaul Mullowney   PetscFunctionReturn(0);
11549ae82921SPaul Mullowney }
11559ae82921SPaul Mullowney 
11566fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx)
11579ae82921SPaul Mullowney {
1158465f34aeSAlejandro Lamas Daviña   const PetscScalar                 *barray;
1159465f34aeSAlejandro Lamas Daviña   PetscScalar                       *xarray;
11609ae82921SPaul Mullowney   cusparseStatus_t                  stat;
11619ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
1162aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
1163aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
1164aa372e3fSPaul Mullowney   THRUSTARRAY                       *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector;
1165b175d8bbSPaul Mullowney   PetscErrorCode                    ierr;
11669ae82921SPaul Mullowney 
11679ae82921SPaul Mullowney   PetscFunctionBegin;
1168e057df02SPaul Mullowney   /* Get the GPU pointers */
1169c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr);
1170c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr);
11719ae82921SPaul Mullowney 
11727a052e47Shannah_mairs   ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
1173aa372e3fSPaul Mullowney   /* First, solve L */
1174aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp,
11757656d835SStefano Zampini                         loTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, loTriFactor->descr,
1176aa372e3fSPaul Mullowney                         loTriFactor->csrMat->values->data().get(),
1177aa372e3fSPaul Mullowney                         loTriFactor->csrMat->row_offsets->data().get(),
1178aa372e3fSPaul Mullowney                         loTriFactor->csrMat->column_indices->data().get(),
1179aa372e3fSPaul Mullowney                         loTriFactor->solveInfo,
1180c41cb2e2SAlejandro Lamas Daviña                         barray, tempGPU->data().get());CHKERRCUDA(stat);
1181aa372e3fSPaul Mullowney 
1182aa372e3fSPaul Mullowney   /* Next, solve U */
1183aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp,
11847656d835SStefano Zampini                         upTriFactor->csrMat->num_rows, &PETSC_CUSPARSE_ONE, upTriFactor->descr,
1185aa372e3fSPaul Mullowney                         upTriFactor->csrMat->values->data().get(),
1186aa372e3fSPaul Mullowney                         upTriFactor->csrMat->row_offsets->data().get(),
1187aa372e3fSPaul Mullowney                         upTriFactor->csrMat->column_indices->data().get(),
1188aa372e3fSPaul Mullowney                         upTriFactor->solveInfo,
1189c41cb2e2SAlejandro Lamas Daviña                         tempGPU->data().get(), xarray);CHKERRCUDA(stat);
1190958c4211Shannah_mairs   ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
11919ae82921SPaul Mullowney 
1192c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr);
1193c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr);
1194c41cb2e2SAlejandro Lamas Daviña   ierr = WaitForGPU();CHKERRCUDA(ierr);
1195958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr);
11969ae82921SPaul Mullowney   PetscFunctionReturn(0);
11979ae82921SPaul Mullowney }
11989ae82921SPaul Mullowney 
11996fa9248bSJed Brown static PetscErrorCode MatSeqAIJCUSPARSECopyToGPU(Mat A)
12009ae82921SPaul Mullowney {
12019ae82921SPaul Mullowney 
1202aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE           *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
1203aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat;
12049ae82921SPaul Mullowney   Mat_SeqAIJ                   *a = (Mat_SeqAIJ*)A->data;
12059ae82921SPaul Mullowney   PetscInt                     m = A->rmap->n,*ii,*ridx;
12069ae82921SPaul Mullowney   PetscErrorCode               ierr;
1207aa372e3fSPaul Mullowney   cusparseStatus_t             stat;
1208b06137fdSPaul Mullowney   cudaError_t                  err;
12099ae82921SPaul Mullowney 
12109ae82921SPaul Mullowney   PetscFunctionBegin;
1211fdc842d1SBarry Smith   if (A->pinnedtocpu) PetscFunctionReturn(0);
1212b8ced49eSKarl Rupp   if (A->valid_GPU_matrix == PETSC_OFFLOAD_UNALLOCATED || A->valid_GPU_matrix == PETSC_OFFLOAD_CPU) {
12139ae82921SPaul Mullowney     ierr = PetscLogEventBegin(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr);
121434d6c7a5SJose E. Roman     if (A->assembled && A->nonzerostate == cusparsestruct->nonzerostate && cusparsestruct->format == MAT_CUSPARSE_CSR) {
121534d6c7a5SJose E. Roman       CsrMatrix *matrix = (CsrMatrix*)matstruct->mat;
121634d6c7a5SJose E. Roman       /* copy values only */
121734d6c7a5SJose E. Roman       matrix->values->assign(a->a, a->a+a->nz);
12184863603aSSatish Balay       ierr = PetscLogCpuToGpu((a->nz)*sizeof(PetscScalar));CHKERRQ(ierr);
121934d6c7a5SJose E. Roman     } else {
1220470880abSPatrick Sanan       MatSeqAIJCUSPARSEMultStruct_Destroy(&matstruct,cusparsestruct->format);
12219ae82921SPaul Mullowney       try {
1222aa372e3fSPaul Mullowney         cusparsestruct->nonzerorow=0;
1223aa372e3fSPaul Mullowney         for (int j = 0; j<m; j++) cusparsestruct->nonzerorow += ((a->i[j+1]-a->i[j])>0);
12249ae82921SPaul Mullowney 
12259ae82921SPaul Mullowney         if (a->compressedrow.use) {
12269ae82921SPaul Mullowney           m    = a->compressedrow.nrows;
12279ae82921SPaul Mullowney           ii   = a->compressedrow.i;
12289ae82921SPaul Mullowney           ridx = a->compressedrow.rindex;
12299ae82921SPaul Mullowney         } else {
1230b06137fdSPaul Mullowney           /* Forcing compressed row on the GPU */
12319ae82921SPaul Mullowney           int k=0;
1232854ce69bSBarry Smith           ierr = PetscMalloc1(cusparsestruct->nonzerorow+1, &ii);CHKERRQ(ierr);
1233854ce69bSBarry Smith           ierr = PetscMalloc1(cusparsestruct->nonzerorow, &ridx);CHKERRQ(ierr);
12349ae82921SPaul Mullowney           ii[0]=0;
12359ae82921SPaul Mullowney           for (int j = 0; j<m; j++) {
12369ae82921SPaul Mullowney             if ((a->i[j+1]-a->i[j])>0) {
12379ae82921SPaul Mullowney               ii[k]  = a->i[j];
12389ae82921SPaul Mullowney               ridx[k]= j;
12399ae82921SPaul Mullowney               k++;
12409ae82921SPaul Mullowney             }
12419ae82921SPaul Mullowney           }
1242aa372e3fSPaul Mullowney           ii[cusparsestruct->nonzerorow] = a->nz;
1243aa372e3fSPaul Mullowney           m = cusparsestruct->nonzerorow;
12449ae82921SPaul Mullowney         }
12459ae82921SPaul Mullowney 
1246aa372e3fSPaul Mullowney         /* allocate space for the triangular factor information */
1247aa372e3fSPaul Mullowney         matstruct = new Mat_SeqAIJCUSPARSEMultStruct;
1248c41cb2e2SAlejandro Lamas Daviña         stat = cusparseCreateMatDescr(&matstruct->descr);CHKERRCUDA(stat);
1249c41cb2e2SAlejandro Lamas Daviña         stat = cusparseSetMatIndexBase(matstruct->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat);
1250c41cb2e2SAlejandro Lamas Daviña         stat = cusparseSetMatType(matstruct->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUDA(stat);
12519ae82921SPaul Mullowney 
1252c41cb2e2SAlejandro Lamas Daviña         err = cudaMalloc((void **)&(matstruct->alpha),    sizeof(PetscScalar));CHKERRCUDA(err);
12537656d835SStefano Zampini         err = cudaMalloc((void **)&(matstruct->beta_zero),sizeof(PetscScalar));CHKERRCUDA(err);
12547656d835SStefano Zampini         err = cudaMalloc((void **)&(matstruct->beta_one), sizeof(PetscScalar));CHKERRCUDA(err);
12557656d835SStefano Zampini         err = cudaMemcpy(matstruct->alpha,    &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
12567656d835SStefano Zampini         err = cudaMemcpy(matstruct->beta_zero,&PETSC_CUSPARSE_ZERO,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
12577656d835SStefano Zampini         err = cudaMemcpy(matstruct->beta_one, &PETSC_CUSPARSE_ONE, sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
1258c41cb2e2SAlejandro Lamas Daviña         stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat);
1259b06137fdSPaul Mullowney 
1260aa372e3fSPaul Mullowney         /* Build a hybrid/ellpack matrix if this option is chosen for the storage */
1261aa372e3fSPaul Mullowney         if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
1262aa372e3fSPaul Mullowney           /* set the matrix */
1263aa372e3fSPaul Mullowney           CsrMatrix *matrix= new CsrMatrix;
1264a65300a6SPaul Mullowney           matrix->num_rows = m;
1265aa372e3fSPaul Mullowney           matrix->num_cols = A->cmap->n;
1266aa372e3fSPaul Mullowney           matrix->num_entries = a->nz;
1267a65300a6SPaul Mullowney           matrix->row_offsets = new THRUSTINTARRAY32(m+1);
1268a65300a6SPaul Mullowney           matrix->row_offsets->assign(ii, ii + m+1);
12699ae82921SPaul Mullowney 
1270aa372e3fSPaul Mullowney           matrix->column_indices = new THRUSTINTARRAY32(a->nz);
1271aa372e3fSPaul Mullowney           matrix->column_indices->assign(a->j, a->j+a->nz);
1272aa372e3fSPaul Mullowney 
1273aa372e3fSPaul Mullowney           matrix->values = new THRUSTARRAY(a->nz);
1274aa372e3fSPaul Mullowney           matrix->values->assign(a->a, a->a+a->nz);
1275aa372e3fSPaul Mullowney 
1276aa372e3fSPaul Mullowney           /* assign the pointer */
1277aa372e3fSPaul Mullowney           matstruct->mat = matrix;
1278aa372e3fSPaul Mullowney 
1279aa372e3fSPaul Mullowney         } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) {
12802692e278SPaul Mullowney #if CUDA_VERSION>=4020
1281aa372e3fSPaul Mullowney           CsrMatrix *matrix= new CsrMatrix;
1282a65300a6SPaul Mullowney           matrix->num_rows = m;
1283aa372e3fSPaul Mullowney           matrix->num_cols = A->cmap->n;
1284aa372e3fSPaul Mullowney           matrix->num_entries = a->nz;
1285a65300a6SPaul Mullowney           matrix->row_offsets = new THRUSTINTARRAY32(m+1);
1286a65300a6SPaul Mullowney           matrix->row_offsets->assign(ii, ii + m+1);
1287aa372e3fSPaul Mullowney 
1288aa372e3fSPaul Mullowney           matrix->column_indices = new THRUSTINTARRAY32(a->nz);
1289aa372e3fSPaul Mullowney           matrix->column_indices->assign(a->j, a->j+a->nz);
1290aa372e3fSPaul Mullowney 
1291aa372e3fSPaul Mullowney           matrix->values = new THRUSTARRAY(a->nz);
1292aa372e3fSPaul Mullowney           matrix->values->assign(a->a, a->a+a->nz);
1293aa372e3fSPaul Mullowney 
1294aa372e3fSPaul Mullowney           cusparseHybMat_t hybMat;
1295c41cb2e2SAlejandro Lamas Daviña           stat = cusparseCreateHybMat(&hybMat);CHKERRCUDA(stat);
1296aa372e3fSPaul Mullowney           cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ?
1297aa372e3fSPaul Mullowney             CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO;
1298a65300a6SPaul Mullowney           stat = cusparse_csr2hyb(cusparsestruct->handle, matrix->num_rows, matrix->num_cols,
1299aa372e3fSPaul Mullowney               matstruct->descr, matrix->values->data().get(),
1300aa372e3fSPaul Mullowney               matrix->row_offsets->data().get(),
1301aa372e3fSPaul Mullowney               matrix->column_indices->data().get(),
1302c41cb2e2SAlejandro Lamas Daviña               hybMat, 0, partition);CHKERRCUDA(stat);
1303aa372e3fSPaul Mullowney           /* assign the pointer */
1304aa372e3fSPaul Mullowney           matstruct->mat = hybMat;
1305aa372e3fSPaul Mullowney 
1306aa372e3fSPaul Mullowney           if (matrix) {
1307aa372e3fSPaul Mullowney             if (matrix->values) delete (THRUSTARRAY*)matrix->values;
1308aa372e3fSPaul Mullowney             if (matrix->column_indices) delete (THRUSTINTARRAY32*)matrix->column_indices;
1309aa372e3fSPaul Mullowney             if (matrix->row_offsets) delete (THRUSTINTARRAY32*)matrix->row_offsets;
1310aa372e3fSPaul Mullowney             delete (CsrMatrix*)matrix;
1311087f3262SPaul Mullowney           }
13122692e278SPaul Mullowney #endif
1313087f3262SPaul Mullowney         }
1314ca45077fSPaul Mullowney 
1315aa372e3fSPaul Mullowney         /* assign the compressed row indices */
1316aa372e3fSPaul Mullowney         matstruct->cprowIndices = new THRUSTINTARRAY(m);
1317aa372e3fSPaul Mullowney         matstruct->cprowIndices->assign(ridx,ridx+m);
13184863603aSSatish Balay         ierr = PetscLogCpuToGpu(((m+1)+(a->nz))*sizeof(int)+m*sizeof(PetscInt)+(3+(a->nz))*sizeof(PetscScalar));CHKERRQ(ierr);
1319aa372e3fSPaul Mullowney 
1320aa372e3fSPaul Mullowney         /* assign the pointer */
1321aa372e3fSPaul Mullowney         cusparsestruct->mat = matstruct;
1322aa372e3fSPaul Mullowney 
13239ae82921SPaul Mullowney         if (!a->compressedrow.use) {
13249ae82921SPaul Mullowney           ierr = PetscFree(ii);CHKERRQ(ierr);
13259ae82921SPaul Mullowney           ierr = PetscFree(ridx);CHKERRQ(ierr);
13269ae82921SPaul Mullowney         }
1327e65717acSKarl Rupp         cusparsestruct->workVector = new THRUSTARRAY(m);
13289ae82921SPaul Mullowney       } catch(char *ex) {
13299ae82921SPaul Mullowney         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
13309ae82921SPaul Mullowney       }
133134d6c7a5SJose E. Roman       cusparsestruct->nonzerostate = A->nonzerostate;
133234d6c7a5SJose E. Roman     }
13337d0e52d8SJose E. Roman     ierr = WaitForGPU();CHKERRCUDA(ierr);
1334b8ced49eSKarl Rupp     A->valid_GPU_matrix = PETSC_OFFLOAD_BOTH;
13359ae82921SPaul Mullowney     ierr = PetscLogEventEnd(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr);
13369ae82921SPaul Mullowney   }
13379ae82921SPaul Mullowney   PetscFunctionReturn(0);
13389ae82921SPaul Mullowney }
13399ae82921SPaul Mullowney 
1340c41cb2e2SAlejandro Lamas Daviña struct VecCUDAPlusEquals
1341aa372e3fSPaul Mullowney {
1342aa372e3fSPaul Mullowney   template <typename Tuple>
1343aa372e3fSPaul Mullowney   __host__ __device__
1344aa372e3fSPaul Mullowney   void operator()(Tuple t)
1345aa372e3fSPaul Mullowney   {
1346aa372e3fSPaul Mullowney     thrust::get<1>(t) = thrust::get<1>(t) + thrust::get<0>(t);
1347aa372e3fSPaul Mullowney   }
1348aa372e3fSPaul Mullowney };
1349aa372e3fSPaul Mullowney 
13506fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy)
13519ae82921SPaul Mullowney {
1352b175d8bbSPaul Mullowney   PetscErrorCode ierr;
13539ae82921SPaul Mullowney 
13549ae82921SPaul Mullowney   PetscFunctionBegin;
13557656d835SStefano Zampini   ierr = MatMultAdd_SeqAIJCUSPARSE(A,xx,NULL,yy);CHKERRQ(ierr);
13569ae82921SPaul Mullowney   PetscFunctionReturn(0);
13579ae82921SPaul Mullowney }
13589ae82921SPaul Mullowney 
13596fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy)
1360ca45077fSPaul Mullowney {
1361ca45077fSPaul Mullowney   Mat_SeqAIJ                   *a = (Mat_SeqAIJ*)A->data;
1362aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE           *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
13639ff858a8SKarl Rupp   Mat_SeqAIJCUSPARSEMultStruct *matstructT;
1364465f34aeSAlejandro Lamas Daviña   const PetscScalar            *xarray;
1365465f34aeSAlejandro Lamas Daviña   PetscScalar                  *yarray;
1366b175d8bbSPaul Mullowney   PetscErrorCode               ierr;
1367aa372e3fSPaul Mullowney   cusparseStatus_t             stat;
1368ca45077fSPaul Mullowney 
1369ca45077fSPaul Mullowney   PetscFunctionBegin;
137034d6c7a5SJose E. Roman   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
137134d6c7a5SJose E. Roman   ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
13729ff858a8SKarl Rupp   matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
1373aa372e3fSPaul Mullowney   if (!matstructT) {
1374bda325fcSPaul Mullowney     ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr);
1375aa372e3fSPaul Mullowney     matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
1376bda325fcSPaul Mullowney   }
1377c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr);
1378ca6ae6e6SAlejandro Lamas Daviña   ierr = VecSet(yy,0);CHKERRQ(ierr);
1379c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(yy,&yarray);CHKERRQ(ierr);
1380aa372e3fSPaul Mullowney 
13817a052e47Shannah_mairs   ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
1382aa372e3fSPaul Mullowney   if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
1383aa372e3fSPaul Mullowney     CsrMatrix *mat = (CsrMatrix*)matstructT->mat;
1384aa372e3fSPaul Mullowney     stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1385aa372e3fSPaul Mullowney                              mat->num_rows, mat->num_cols,
1386b06137fdSPaul Mullowney                              mat->num_entries, matstructT->alpha, matstructT->descr,
1387aa372e3fSPaul Mullowney                              mat->values->data().get(), mat->row_offsets->data().get(),
13887656d835SStefano Zampini                              mat->column_indices->data().get(), xarray, matstructT->beta_zero,
1389c41cb2e2SAlejandro Lamas Daviña                              yarray);CHKERRCUDA(stat);
1390aa372e3fSPaul Mullowney   } else {
13912692e278SPaul Mullowney #if CUDA_VERSION>=4020
1392aa372e3fSPaul Mullowney     cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat;
1393aa372e3fSPaul Mullowney     stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1394b06137fdSPaul Mullowney                              matstructT->alpha, matstructT->descr, hybMat,
13957656d835SStefano Zampini                              xarray, matstructT->beta_zero,
1396c41cb2e2SAlejandro Lamas Daviña                              yarray);CHKERRCUDA(stat);
13972692e278SPaul Mullowney #endif
1398ca45077fSPaul Mullowney   }
1399958c4211Shannah_mairs   ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1400c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr);
1401c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(yy,&yarray);CHKERRQ(ierr);
1402aa372e3fSPaul Mullowney   if (!cusparsestruct->stream) {
1403c41cb2e2SAlejandro Lamas Daviña     ierr = WaitForGPU();CHKERRCUDA(ierr);
1404ca45077fSPaul Mullowney   }
1405958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*a->nz - cusparsestruct->nonzerorow);CHKERRQ(ierr);
1406ca45077fSPaul Mullowney   PetscFunctionReturn(0);
1407ca45077fSPaul Mullowney }
1408ca45077fSPaul Mullowney 
1409aa372e3fSPaul Mullowney 
14106fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz)
14119ae82921SPaul Mullowney {
14129ae82921SPaul Mullowney   Mat_SeqAIJ                   *a = (Mat_SeqAIJ*)A->data;
1413aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE           *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
14149ff858a8SKarl Rupp   Mat_SeqAIJCUSPARSEMultStruct *matstruct;
1415465f34aeSAlejandro Lamas Daviña   const PetscScalar            *xarray;
14167656d835SStefano Zampini   PetscScalar                  *zarray,*dptr,*beta;
1417b175d8bbSPaul Mullowney   PetscErrorCode               ierr;
1418aa372e3fSPaul Mullowney   cusparseStatus_t             stat;
14196e111a19SKarl Rupp 
14209ae82921SPaul Mullowney   PetscFunctionBegin;
142134d6c7a5SJose E. Roman   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
142234d6c7a5SJose E. Roman   ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
14239ff858a8SKarl Rupp   matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat;
14249ae82921SPaul Mullowney   try {
1425c41cb2e2SAlejandro Lamas Daviña     ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr);
1426f2d70e9dSBarry Smith     ierr = VecCUDAGetArray(zz,&zarray);CHKERRQ(ierr);
14277656d835SStefano Zampini     dptr = cusparsestruct->workVector->size() == (thrust::detail::vector_base<PetscScalar, thrust::device_malloc_allocator<PetscScalar> >::size_type)(A->rmap->n) ? zarray : cusparsestruct->workVector->data().get();
14287656d835SStefano Zampini     beta = (yy == zz && dptr == zarray) ? matstruct->beta_one : matstruct->beta_zero;
14299ae82921SPaul Mullowney 
14307a052e47Shannah_mairs     ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
14317656d835SStefano Zampini     /* csr_spmv is multiply add */
1432aa372e3fSPaul Mullowney     if (cusparsestruct->format == MAT_CUSPARSE_CSR) {
1433b06137fdSPaul Mullowney       /* here we need to be careful to set the number of rows in the multiply to the
1434b06137fdSPaul Mullowney          number of compressed rows in the matrix ... which is equivalent to the
1435b06137fdSPaul Mullowney          size of the workVector */
14367656d835SStefano Zampini       CsrMatrix *mat = (CsrMatrix*)matstruct->mat;
1437aa372e3fSPaul Mullowney       stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1438a65300a6SPaul Mullowney                                mat->num_rows, mat->num_cols,
1439b06137fdSPaul Mullowney                                mat->num_entries, matstruct->alpha, matstruct->descr,
1440aa372e3fSPaul Mullowney                                mat->values->data().get(), mat->row_offsets->data().get(),
14417656d835SStefano Zampini                                mat->column_indices->data().get(), xarray, beta,
14427656d835SStefano Zampini                                dptr);CHKERRCUDA(stat);
1443aa372e3fSPaul Mullowney     } else {
14442692e278SPaul Mullowney #if CUDA_VERSION>=4020
1445aa372e3fSPaul Mullowney       cusparseHybMat_t hybMat = (cusparseHybMat_t)matstruct->mat;
1446a65300a6SPaul Mullowney       if (cusparsestruct->workVector->size()) {
1447aa372e3fSPaul Mullowney         stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1448b06137fdSPaul Mullowney                                  matstruct->alpha, matstruct->descr, hybMat,
14497656d835SStefano Zampini                                  xarray, beta,
14507656d835SStefano Zampini                                  dptr);CHKERRCUDA(stat);
1451a65300a6SPaul Mullowney       }
14522692e278SPaul Mullowney #endif
1453aa372e3fSPaul Mullowney     }
1454958c4211Shannah_mairs     ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1455aa372e3fSPaul Mullowney 
14567656d835SStefano Zampini     if (yy) {
14577656d835SStefano Zampini       if (dptr != zarray) {
14587656d835SStefano Zampini         ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr);
14597656d835SStefano Zampini       } else if (zz != yy) {
14607656d835SStefano Zampini         ierr = VecAXPY_SeqCUDA(zz,1.0,yy);CHKERRQ(ierr);
14617656d835SStefano Zampini       }
14627656d835SStefano Zampini     } else if (dptr != zarray) {
14637656d835SStefano Zampini       ierr = VecSet(zz,0);CHKERRQ(ierr);
14647656d835SStefano Zampini     }
1465aa372e3fSPaul Mullowney     /* scatter the data from the temporary into the full vector with a += operation */
14667a052e47Shannah_mairs     ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
14677656d835SStefano Zampini     if (dptr != zarray) {
14687656d835SStefano Zampini       thrust::device_ptr<PetscScalar> zptr;
14697656d835SStefano Zampini 
14707656d835SStefano Zampini       zptr = thrust::device_pointer_cast(zarray);
1471c41cb2e2SAlejandro Lamas Daviña       thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))),
1472c41cb2e2SAlejandro Lamas Daviña                        thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))) + cusparsestruct->workVector->size(),
1473c41cb2e2SAlejandro Lamas Daviña                        VecCUDAPlusEquals());
14747656d835SStefano Zampini     }
1475958c4211Shannah_mairs     ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1476c41cb2e2SAlejandro Lamas Daviña     ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr);
1477f2d70e9dSBarry Smith     ierr = VecCUDARestoreArray(zz,&zarray);CHKERRQ(ierr);
14789ae82921SPaul Mullowney   } catch(char *ex) {
14799ae82921SPaul Mullowney     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
14809ae82921SPaul Mullowney   }
14817656d835SStefano Zampini   if (!yy) { /* MatMult */
14827656d835SStefano Zampini     if (!cusparsestruct->stream) {
1483c41cb2e2SAlejandro Lamas Daviña       ierr = WaitForGPU();CHKERRCUDA(ierr);
14847656d835SStefano Zampini     }
14857656d835SStefano Zampini   }
1486958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr);
14879ae82921SPaul Mullowney   PetscFunctionReturn(0);
14889ae82921SPaul Mullowney }
14899ae82921SPaul Mullowney 
14906fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz)
1491ca45077fSPaul Mullowney {
1492ca45077fSPaul Mullowney   Mat_SeqAIJ                      *a = (Mat_SeqAIJ*)A->data;
1493aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE              *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
14949ff858a8SKarl Rupp   Mat_SeqAIJCUSPARSEMultStruct    *matstructT;
1495c41cb2e2SAlejandro Lamas Daviña   thrust::device_ptr<PetscScalar> zptr;
1496465f34aeSAlejandro Lamas Daviña   const PetscScalar               *xarray;
1497465f34aeSAlejandro Lamas Daviña   PetscScalar                     *zarray;
1498b175d8bbSPaul Mullowney   PetscErrorCode                  ierr;
1499aa372e3fSPaul Mullowney   cusparseStatus_t                stat;
15006e111a19SKarl Rupp 
1501ca45077fSPaul Mullowney   PetscFunctionBegin;
150234d6c7a5SJose E. Roman   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
150334d6c7a5SJose E. Roman   ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
15049ff858a8SKarl Rupp   matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
1505aa372e3fSPaul Mullowney   if (!matstructT) {
1506bda325fcSPaul Mullowney     ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr);
1507aa372e3fSPaul Mullowney     matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
1508bda325fcSPaul Mullowney   }
1509aa372e3fSPaul Mullowney 
1510ca45077fSPaul Mullowney   try {
1511c41cb2e2SAlejandro Lamas Daviña     ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr);
1512c41cb2e2SAlejandro Lamas Daviña     ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr);
1513f2d70e9dSBarry Smith     ierr = VecCUDAGetArray(zz,&zarray);CHKERRQ(ierr);
1514c41cb2e2SAlejandro Lamas Daviña     zptr = thrust::device_pointer_cast(zarray);
1515ca45077fSPaul Mullowney 
15167a052e47Shannah_mairs     ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
1517e057df02SPaul Mullowney     /* multiply add with matrix transpose */
1518aa372e3fSPaul Mullowney     if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
1519aa372e3fSPaul Mullowney       CsrMatrix *mat = (CsrMatrix*)matstructT->mat;
1520b06137fdSPaul Mullowney       /* here we need to be careful to set the number of rows in the multiply to the
1521b06137fdSPaul Mullowney          number of compressed rows in the matrix ... which is equivalent to the
1522b06137fdSPaul Mullowney          size of the workVector */
1523aa372e3fSPaul Mullowney       stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1524a65300a6SPaul Mullowney                                mat->num_rows, mat->num_cols,
1525b06137fdSPaul Mullowney                                mat->num_entries, matstructT->alpha, matstructT->descr,
1526aa372e3fSPaul Mullowney                                mat->values->data().get(), mat->row_offsets->data().get(),
15277656d835SStefano Zampini                                mat->column_indices->data().get(), xarray, matstructT->beta_zero,
1528c41cb2e2SAlejandro Lamas Daviña                                cusparsestruct->workVector->data().get());CHKERRCUDA(stat);
1529aa372e3fSPaul Mullowney     } else {
15302692e278SPaul Mullowney #if CUDA_VERSION>=4020
1531aa372e3fSPaul Mullowney       cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat;
1532a65300a6SPaul Mullowney       if (cusparsestruct->workVector->size()) {
1533aa372e3fSPaul Mullowney         stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1534b06137fdSPaul Mullowney             matstructT->alpha, matstructT->descr, hybMat,
15357656d835SStefano Zampini             xarray, matstructT->beta_zero,
1536c41cb2e2SAlejandro Lamas Daviña             cusparsestruct->workVector->data().get());CHKERRCUDA(stat);
1537a65300a6SPaul Mullowney       }
15382692e278SPaul Mullowney #endif
1539aa372e3fSPaul Mullowney     }
1540aa372e3fSPaul Mullowney 
1541aa372e3fSPaul Mullowney     /* scatter the data from the temporary into the full vector with a += operation */
1542c41cb2e2SAlejandro Lamas Daviña     thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstructT->cprowIndices->begin()))),
1543554b8892SKarl Rupp         thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstructT->cprowIndices->begin()))) + A->cmap->n,
1544c41cb2e2SAlejandro Lamas Daviña         VecCUDAPlusEquals());
1545958c4211Shannah_mairs     ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
1546c41cb2e2SAlejandro Lamas Daviña     ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr);
1547f2d70e9dSBarry Smith     ierr = VecCUDARestoreArray(zz,&zarray);CHKERRQ(ierr);
1548ca45077fSPaul Mullowney 
1549ca45077fSPaul Mullowney   } catch(char *ex) {
1550ca45077fSPaul Mullowney     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
1551ca45077fSPaul Mullowney   }
1552c41cb2e2SAlejandro Lamas Daviña   ierr = WaitForGPU();CHKERRCUDA(ierr);
1553958c4211Shannah_mairs   ierr = PetscLogGpuFlops(2.0*a->nz);CHKERRQ(ierr);
1554ca45077fSPaul Mullowney   PetscFunctionReturn(0);
1555ca45077fSPaul Mullowney }
1556ca45077fSPaul Mullowney 
15576fa9248bSJed Brown static PetscErrorCode MatAssemblyEnd_SeqAIJCUSPARSE(Mat A,MatAssemblyType mode)
15589ae82921SPaul Mullowney {
15599ae82921SPaul Mullowney   PetscErrorCode ierr;
15606e111a19SKarl Rupp 
15619ae82921SPaul Mullowney   PetscFunctionBegin;
15629ae82921SPaul Mullowney   ierr = MatAssemblyEnd_SeqAIJ(A,mode);CHKERRQ(ierr);
1563bc3f50f2SPaul Mullowney   if (A->factortype==MAT_FACTOR_NONE) {
1564e057df02SPaul Mullowney     ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
1565bc3f50f2SPaul Mullowney   }
15669ae82921SPaul Mullowney   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
1567bbf3fe20SPaul Mullowney   A->ops->mult             = MatMult_SeqAIJCUSPARSE;
1568bbf3fe20SPaul Mullowney   A->ops->multadd          = MatMultAdd_SeqAIJCUSPARSE;
1569bbf3fe20SPaul Mullowney   A->ops->multtranspose    = MatMultTranspose_SeqAIJCUSPARSE;
1570bbf3fe20SPaul Mullowney   A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE;
15719ae82921SPaul Mullowney   PetscFunctionReturn(0);
15729ae82921SPaul Mullowney }
15739ae82921SPaul Mullowney 
15749ae82921SPaul Mullowney /* --------------------------------------------------------------------------------*/
1575e057df02SPaul Mullowney /*@
15769ae82921SPaul Mullowney    MatCreateSeqAIJCUSPARSE - Creates a sparse matrix in AIJ (compressed row) format
1577e057df02SPaul Mullowney    (the default parallel PETSc format). This matrix will ultimately pushed down
1578e057df02SPaul Mullowney    to NVidia GPUs and use the CUSPARSE library for calculations. For good matrix
1579e057df02SPaul Mullowney    assembly performance the user should preallocate the matrix storage by setting
1580e057df02SPaul Mullowney    the parameter nz (or the array nnz).  By setting these parameters accurately,
1581e057df02SPaul Mullowney    performance during matrix assembly can be increased by more than a factor of 50.
15829ae82921SPaul Mullowney 
1583d083f849SBarry Smith    Collective
15849ae82921SPaul Mullowney 
15859ae82921SPaul Mullowney    Input Parameters:
15869ae82921SPaul Mullowney +  comm - MPI communicator, set to PETSC_COMM_SELF
15879ae82921SPaul Mullowney .  m - number of rows
15889ae82921SPaul Mullowney .  n - number of columns
15899ae82921SPaul Mullowney .  nz - number of nonzeros per row (same for all rows)
15909ae82921SPaul Mullowney -  nnz - array containing the number of nonzeros in the various rows
15910298fd71SBarry Smith          (possibly different for each row) or NULL
15929ae82921SPaul Mullowney 
15939ae82921SPaul Mullowney    Output Parameter:
15949ae82921SPaul Mullowney .  A - the matrix
15959ae82921SPaul Mullowney 
15969ae82921SPaul Mullowney    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
15979ae82921SPaul Mullowney    MatXXXXSetPreallocation() paradgm instead of this routine directly.
15989ae82921SPaul Mullowney    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
15999ae82921SPaul Mullowney 
16009ae82921SPaul Mullowney    Notes:
16019ae82921SPaul Mullowney    If nnz is given then nz is ignored
16029ae82921SPaul Mullowney 
16039ae82921SPaul Mullowney    The AIJ format (also called the Yale sparse matrix format or
16049ae82921SPaul Mullowney    compressed row storage), is fully compatible with standard Fortran 77
16059ae82921SPaul Mullowney    storage.  That is, the stored row and column indices can begin at
16069ae82921SPaul Mullowney    either one (as in Fortran) or zero.  See the users' manual for details.
16079ae82921SPaul Mullowney 
16089ae82921SPaul Mullowney    Specify the preallocated storage with either nz or nnz (not both).
16090298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
16109ae82921SPaul Mullowney    allocation.  For large problems you MUST preallocate memory or you
16119ae82921SPaul Mullowney    will get TERRIBLE performance, see the users' manual chapter on matrices.
16129ae82921SPaul Mullowney 
16139ae82921SPaul Mullowney    By default, this format uses inodes (identical nodes) when possible, to
16149ae82921SPaul Mullowney    improve numerical efficiency of matrix-vector products and solves. We
16159ae82921SPaul Mullowney    search for consecutive rows with the same nonzero structure, thereby
16169ae82921SPaul Mullowney    reusing matrix information to achieve increased efficiency.
16179ae82921SPaul Mullowney 
16189ae82921SPaul Mullowney    Level: intermediate
16199ae82921SPaul Mullowney 
1620e057df02SPaul Mullowney .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ(), MATSEQAIJCUSPARSE, MATAIJCUSPARSE
16219ae82921SPaul Mullowney @*/
16229ae82921SPaul Mullowney PetscErrorCode  MatCreateSeqAIJCUSPARSE(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
16239ae82921SPaul Mullowney {
16249ae82921SPaul Mullowney   PetscErrorCode ierr;
16259ae82921SPaul Mullowney 
16269ae82921SPaul Mullowney   PetscFunctionBegin;
16279ae82921SPaul Mullowney   ierr = MatCreate(comm,A);CHKERRQ(ierr);
16289ae82921SPaul Mullowney   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
16299ae82921SPaul Mullowney   ierr = MatSetType(*A,MATSEQAIJCUSPARSE);CHKERRQ(ierr);
16309ae82921SPaul Mullowney   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
16319ae82921SPaul Mullowney   PetscFunctionReturn(0);
16329ae82921SPaul Mullowney }
16339ae82921SPaul Mullowney 
16346fa9248bSJed Brown static PetscErrorCode MatDestroy_SeqAIJCUSPARSE(Mat A)
16359ae82921SPaul Mullowney {
16369ae82921SPaul Mullowney   PetscErrorCode   ierr;
1637ab25e6cbSDominic Meiser 
16389ae82921SPaul Mullowney   PetscFunctionBegin;
16399ae82921SPaul Mullowney   if (A->factortype==MAT_FACTOR_NONE) {
1640b8ced49eSKarl Rupp     if (A->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
1641470880abSPatrick Sanan       ierr = MatSeqAIJCUSPARSE_Destroy((Mat_SeqAIJCUSPARSE**)&A->spptr);CHKERRQ(ierr);
16429ae82921SPaul Mullowney     }
16439ae82921SPaul Mullowney   } else {
1644470880abSPatrick Sanan     ierr = MatSeqAIJCUSPARSETriFactors_Destroy((Mat_SeqAIJCUSPARSETriFactors**)&A->spptr);CHKERRQ(ierr);
1645aa372e3fSPaul Mullowney   }
16469ae82921SPaul Mullowney   ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr);
16479ae82921SPaul Mullowney   PetscFunctionReturn(0);
16489ae82921SPaul Mullowney }
16499ae82921SPaul Mullowney 
16509ff858a8SKarl Rupp static PetscErrorCode MatDuplicate_SeqAIJCUSPARSE(Mat A,MatDuplicateOption cpvalues,Mat *B)
16519ff858a8SKarl Rupp {
16529ff858a8SKarl Rupp   PetscErrorCode ierr;
16539ff858a8SKarl Rupp   Mat C;
16549ff858a8SKarl Rupp   cusparseStatus_t stat;
16559ff858a8SKarl Rupp   cusparseHandle_t handle=0;
16569ff858a8SKarl Rupp 
16579ff858a8SKarl Rupp   PetscFunctionBegin;
16589ff858a8SKarl Rupp   ierr = MatDuplicate_SeqAIJ(A,cpvalues,B);CHKERRQ(ierr);
16599ff858a8SKarl Rupp   C    = *B;
166034136279SStefano Zampini   ierr = PetscFree(C->defaultvectype);CHKERRQ(ierr);
166134136279SStefano Zampini   ierr = PetscStrallocpy(VECCUDA,&C->defaultvectype);CHKERRQ(ierr);
166234136279SStefano Zampini 
16639ff858a8SKarl Rupp   /* inject CUSPARSE-specific stuff */
16649ff858a8SKarl Rupp   if (C->factortype==MAT_FACTOR_NONE) {
16659ff858a8SKarl Rupp     /* you cannot check the inode.use flag here since the matrix was just created.
16669ff858a8SKarl Rupp        now build a GPU matrix data structure */
16679ff858a8SKarl Rupp     C->spptr = new Mat_SeqAIJCUSPARSE;
16689ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->mat          = 0;
16699ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->matTranspose = 0;
16709ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->workVector   = 0;
16719ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->format       = MAT_CUSPARSE_CSR;
16729ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->stream       = 0;
16739ff858a8SKarl Rupp     stat = cusparseCreate(&handle);CHKERRCUDA(stat);
16749ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->handle       = handle;
16759ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)C->spptr)->nonzerostate = 0;
16769ff858a8SKarl Rupp   } else {
16779ff858a8SKarl Rupp     /* NEXT, set the pointers to the triangular factors */
16789ff858a8SKarl Rupp     C->spptr = new Mat_SeqAIJCUSPARSETriFactors;
16799ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->loTriFactorPtr          = 0;
16809ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->upTriFactorPtr          = 0;
16819ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->loTriFactorPtrTranspose = 0;
16829ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->upTriFactorPtrTranspose = 0;
16839ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->rpermIndices            = 0;
16849ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->cpermIndices            = 0;
16859ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->workVector              = 0;
16869ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->handle                  = 0;
16879ff858a8SKarl Rupp     stat = cusparseCreate(&handle);CHKERRCUDA(stat);
16889ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->handle                  = handle;
16899ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSETriFactors*)C->spptr)->nnz                     = 0;
16909ff858a8SKarl Rupp   }
16919ff858a8SKarl Rupp 
16929ff858a8SKarl Rupp   C->ops->assemblyend      = MatAssemblyEnd_SeqAIJCUSPARSE;
16939ff858a8SKarl Rupp   C->ops->destroy          = MatDestroy_SeqAIJCUSPARSE;
16949ff858a8SKarl Rupp   C->ops->setfromoptions   = MatSetFromOptions_SeqAIJCUSPARSE;
16959ff858a8SKarl Rupp   C->ops->mult             = MatMult_SeqAIJCUSPARSE;
16969ff858a8SKarl Rupp   C->ops->multadd          = MatMultAdd_SeqAIJCUSPARSE;
16979ff858a8SKarl Rupp   C->ops->multtranspose    = MatMultTranspose_SeqAIJCUSPARSE;
16989ff858a8SKarl Rupp   C->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE;
16999ff858a8SKarl Rupp   C->ops->duplicate        = MatDuplicate_SeqAIJCUSPARSE;
17009ff858a8SKarl Rupp 
17019ff858a8SKarl Rupp   ierr = PetscObjectChangeTypeName((PetscObject)C,MATSEQAIJCUSPARSE);CHKERRQ(ierr);
17029ff858a8SKarl Rupp 
1703b8ced49eSKarl Rupp   C->valid_GPU_matrix = PETSC_OFFLOAD_UNALLOCATED;
17049ff858a8SKarl Rupp 
17059ff858a8SKarl Rupp   ierr = PetscObjectComposeFunction((PetscObject)C, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr);
17069ff858a8SKarl Rupp   PetscFunctionReturn(0);
17079ff858a8SKarl Rupp }
17089ff858a8SKarl Rupp 
170902fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat B)
17109ae82921SPaul Mullowney {
17119ae82921SPaul Mullowney   PetscErrorCode ierr;
1712aa372e3fSPaul Mullowney   cusparseStatus_t stat;
1713aa372e3fSPaul Mullowney   cusparseHandle_t handle=0;
17149ae82921SPaul Mullowney 
17159ae82921SPaul Mullowney   PetscFunctionBegin;
171634136279SStefano Zampini   ierr = PetscFree(B->defaultvectype);CHKERRQ(ierr);
171734136279SStefano Zampini   ierr = PetscStrallocpy(VECCUDA,&B->defaultvectype);CHKERRQ(ierr);
171834136279SStefano Zampini 
17199ae82921SPaul Mullowney   if (B->factortype==MAT_FACTOR_NONE) {
1720e057df02SPaul Mullowney     /* you cannot check the inode.use flag here since the matrix was just created.
1721e057df02SPaul Mullowney        now build a GPU matrix data structure */
17229ae82921SPaul Mullowney     B->spptr = new Mat_SeqAIJCUSPARSE;
17239ae82921SPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->mat          = 0;
1724aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->matTranspose = 0;
1725aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->workVector   = 0;
1726e057df02SPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->format       = MAT_CUSPARSE_CSR;
1727aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->stream       = 0;
1728c41cb2e2SAlejandro Lamas Daviña     stat = cusparseCreate(&handle);CHKERRCUDA(stat);
1729aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->handle       = handle;
17309ff858a8SKarl Rupp     ((Mat_SeqAIJCUSPARSE*)B->spptr)->nonzerostate = 0;
17319ae82921SPaul Mullowney   } else {
17329ae82921SPaul Mullowney     /* NEXT, set the pointers to the triangular factors */
1733debe9ee2SPaul Mullowney     B->spptr = new Mat_SeqAIJCUSPARSETriFactors;
17349ae82921SPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtr          = 0;
17359ae82921SPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtr          = 0;
1736aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtrTranspose = 0;
1737aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtrTranspose = 0;
1738aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->rpermIndices            = 0;
1739aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->cpermIndices            = 0;
1740aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->workVector              = 0;
1741c41cb2e2SAlejandro Lamas Daviña     stat = cusparseCreate(&handle);CHKERRCUDA(stat);
1742aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->handle                  = handle;
1743aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->nnz                     = 0;
17449ae82921SPaul Mullowney   }
1745aa372e3fSPaul Mullowney 
17469ae82921SPaul Mullowney   B->ops->assemblyend      = MatAssemblyEnd_SeqAIJCUSPARSE;
17479ae82921SPaul Mullowney   B->ops->destroy          = MatDestroy_SeqAIJCUSPARSE;
17489ae82921SPaul Mullowney   B->ops->setfromoptions   = MatSetFromOptions_SeqAIJCUSPARSE;
1749ca45077fSPaul Mullowney   B->ops->mult             = MatMult_SeqAIJCUSPARSE;
1750ca45077fSPaul Mullowney   B->ops->multadd          = MatMultAdd_SeqAIJCUSPARSE;
1751ca45077fSPaul Mullowney   B->ops->multtranspose    = MatMultTranspose_SeqAIJCUSPARSE;
1752ca45077fSPaul Mullowney   B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE;
17539ff858a8SKarl Rupp   B->ops->duplicate        = MatDuplicate_SeqAIJCUSPARSE;
17542205254eSKarl Rupp 
17559ae82921SPaul Mullowney   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJCUSPARSE);CHKERRQ(ierr);
17562205254eSKarl Rupp 
1757b8ced49eSKarl Rupp   B->valid_GPU_matrix = PETSC_OFFLOAD_UNALLOCATED;
17582205254eSKarl Rupp 
1759bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr);
17609ae82921SPaul Mullowney   PetscFunctionReturn(0);
17619ae82921SPaul Mullowney }
17629ae82921SPaul Mullowney 
176302fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJCUSPARSE(Mat B)
176402fe1965SBarry Smith {
176502fe1965SBarry Smith   PetscErrorCode ierr;
176602fe1965SBarry Smith 
176702fe1965SBarry Smith   PetscFunctionBegin;
176802fe1965SBarry Smith   ierr = MatCreate_SeqAIJ(B);CHKERRQ(ierr);
176902fe1965SBarry Smith   ierr = MatConvert_SeqAIJ_SeqAIJCUSPARSE(B);
177002fe1965SBarry Smith   PetscFunctionReturn(0);
177102fe1965SBarry Smith }
177202fe1965SBarry Smith 
17733ca39a21SBarry Smith /*MC
1774e057df02SPaul Mullowney    MATSEQAIJCUSPARSE - MATAIJCUSPARSE = "(seq)aijcusparse" - A matrix type to be used for sparse matrices.
1775e057df02SPaul Mullowney 
1776e057df02SPaul Mullowney    A matrix type type whose data resides on Nvidia GPUs. These matrices can be in either
17772692e278SPaul Mullowney    CSR, ELL, or Hybrid format. The ELL and HYB formats require CUDA 4.2 or later.
17782692e278SPaul Mullowney    All matrix calculations are performed on Nvidia GPUs using the CUSPARSE library.
1779e057df02SPaul Mullowney 
1780e057df02SPaul Mullowney    Options Database Keys:
1781e057df02SPaul Mullowney +  -mat_type aijcusparse - sets the matrix type to "seqaijcusparse" during a call to MatSetFromOptions()
1782aa372e3fSPaul 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).
1783a2b725a8SWilliam 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).
1784e057df02SPaul Mullowney 
1785e057df02SPaul Mullowney   Level: beginner
1786e057df02SPaul Mullowney 
17878468deeeSKarl Rupp .seealso: MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation
1788e057df02SPaul Mullowney M*/
17897f756511SDominic Meiser 
179042c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat,MatFactorType,Mat*);
179142c9c57cSBarry Smith 
17920f39cd5aSBarry Smith 
17933ca39a21SBarry Smith PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_CUSPARSE(void)
179442c9c57cSBarry Smith {
179542c9c57cSBarry Smith   PetscErrorCode ierr;
179642c9c57cSBarry Smith 
179742c9c57cSBarry Smith   PetscFunctionBegin;
17983ca39a21SBarry Smith   ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_LU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr);
17993ca39a21SBarry Smith   ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_CHOLESKY,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr);
18003ca39a21SBarry Smith   ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ILU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr);
18013ca39a21SBarry Smith   ierr = MatSolverTypeRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ICC,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr);
180242c9c57cSBarry Smith   PetscFunctionReturn(0);
180342c9c57cSBarry Smith }
180429b38603SBarry Smith 
180581e08676SBarry Smith 
1806470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE **cusparsestruct)
18077f756511SDominic Meiser {
18087f756511SDominic Meiser   cusparseStatus_t stat;
18097f756511SDominic Meiser   cusparseHandle_t handle;
18107f756511SDominic Meiser 
18117f756511SDominic Meiser   PetscFunctionBegin;
18127f756511SDominic Meiser   if (*cusparsestruct) {
1813470880abSPatrick Sanan     MatSeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->mat,(*cusparsestruct)->format);
1814470880abSPatrick Sanan     MatSeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->matTranspose,(*cusparsestruct)->format);
18157f756511SDominic Meiser     delete (*cusparsestruct)->workVector;
18167f756511SDominic Meiser     if (handle = (*cusparsestruct)->handle) {
1817c41cb2e2SAlejandro Lamas Daviña       stat = cusparseDestroy(handle);CHKERRCUDA(stat);
18187f756511SDominic Meiser     }
18197f756511SDominic Meiser     delete *cusparsestruct;
18207f756511SDominic Meiser     *cusparsestruct = 0;
18217f756511SDominic Meiser   }
18227f756511SDominic Meiser   PetscFunctionReturn(0);
18237f756511SDominic Meiser }
18247f756511SDominic Meiser 
18257f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix **mat)
18267f756511SDominic Meiser {
18277f756511SDominic Meiser   PetscFunctionBegin;
18287f756511SDominic Meiser   if (*mat) {
18297f756511SDominic Meiser     delete (*mat)->values;
18307f756511SDominic Meiser     delete (*mat)->column_indices;
18317f756511SDominic Meiser     delete (*mat)->row_offsets;
18327f756511SDominic Meiser     delete *mat;
18337f756511SDominic Meiser     *mat = 0;
18347f756511SDominic Meiser   }
18357f756511SDominic Meiser   PetscFunctionReturn(0);
18367f756511SDominic Meiser }
18377f756511SDominic Meiser 
1838470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct **trifactor)
18397f756511SDominic Meiser {
18407f756511SDominic Meiser   cusparseStatus_t stat;
18417f756511SDominic Meiser   PetscErrorCode   ierr;
18427f756511SDominic Meiser 
18437f756511SDominic Meiser   PetscFunctionBegin;
18447f756511SDominic Meiser   if (*trifactor) {
1845c41cb2e2SAlejandro Lamas Daviña     if ((*trifactor)->descr) { stat = cusparseDestroyMatDescr((*trifactor)->descr);CHKERRCUDA(stat); }
1846c41cb2e2SAlejandro Lamas Daviña     if ((*trifactor)->solveInfo) { stat = cusparseDestroySolveAnalysisInfo((*trifactor)->solveInfo);CHKERRCUDA(stat); }
18477f756511SDominic Meiser     ierr = CsrMatrix_Destroy(&(*trifactor)->csrMat);CHKERRQ(ierr);
18487f756511SDominic Meiser     delete *trifactor;
18497f756511SDominic Meiser     *trifactor = 0;
18507f756511SDominic Meiser   }
18517f756511SDominic Meiser   PetscFunctionReturn(0);
18527f756511SDominic Meiser }
18537f756511SDominic Meiser 
1854470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct **matstruct,MatCUSPARSEStorageFormat format)
18557f756511SDominic Meiser {
18567f756511SDominic Meiser   CsrMatrix        *mat;
18577f756511SDominic Meiser   cusparseStatus_t stat;
18587f756511SDominic Meiser   cudaError_t      err;
18597f756511SDominic Meiser 
18607f756511SDominic Meiser   PetscFunctionBegin;
18617f756511SDominic Meiser   if (*matstruct) {
18627f756511SDominic Meiser     if ((*matstruct)->mat) {
18637f756511SDominic Meiser       if (format==MAT_CUSPARSE_ELL || format==MAT_CUSPARSE_HYB) {
18647f756511SDominic Meiser         cusparseHybMat_t hybMat = (cusparseHybMat_t)(*matstruct)->mat;
1865c41cb2e2SAlejandro Lamas Daviña         stat = cusparseDestroyHybMat(hybMat);CHKERRCUDA(stat);
18667f756511SDominic Meiser       } else {
18677f756511SDominic Meiser         mat = (CsrMatrix*)(*matstruct)->mat;
18687f756511SDominic Meiser         CsrMatrix_Destroy(&mat);
18697f756511SDominic Meiser       }
18707f756511SDominic Meiser     }
1871c41cb2e2SAlejandro Lamas Daviña     if ((*matstruct)->descr) { stat = cusparseDestroyMatDescr((*matstruct)->descr);CHKERRCUDA(stat); }
18727f756511SDominic Meiser     delete (*matstruct)->cprowIndices;
1873c41cb2e2SAlejandro Lamas Daviña     if ((*matstruct)->alpha)     { err=cudaFree((*matstruct)->alpha);CHKERRCUDA(err); }
18747656d835SStefano Zampini     if ((*matstruct)->beta_zero) { err=cudaFree((*matstruct)->beta_zero);CHKERRCUDA(err); }
18757656d835SStefano Zampini     if ((*matstruct)->beta_one)  { err=cudaFree((*matstruct)->beta_one);CHKERRCUDA(err); }
18767f756511SDominic Meiser     delete *matstruct;
18777f756511SDominic Meiser     *matstruct = 0;
18787f756511SDominic Meiser   }
18797f756511SDominic Meiser   PetscFunctionReturn(0);
18807f756511SDominic Meiser }
18817f756511SDominic Meiser 
1882470880abSPatrick Sanan static PetscErrorCode MatSeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors** trifactors)
18837f756511SDominic Meiser {
18847f756511SDominic Meiser   cusparseHandle_t handle;
18857f756511SDominic Meiser   cusparseStatus_t stat;
18867f756511SDominic Meiser 
18877f756511SDominic Meiser   PetscFunctionBegin;
18887f756511SDominic Meiser   if (*trifactors) {
1889470880abSPatrick Sanan     MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->loTriFactorPtr);
1890470880abSPatrick Sanan     MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->upTriFactorPtr);
1891470880abSPatrick Sanan     MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->loTriFactorPtrTranspose);
1892470880abSPatrick Sanan     MatSeqAIJCUSPARSEMultStruct_Destroy(&(*trifactors)->upTriFactorPtrTranspose);
18937f756511SDominic Meiser     delete (*trifactors)->rpermIndices;
18947f756511SDominic Meiser     delete (*trifactors)->cpermIndices;
18957f756511SDominic Meiser     delete (*trifactors)->workVector;
18967f756511SDominic Meiser     if (handle = (*trifactors)->handle) {
1897c41cb2e2SAlejandro Lamas Daviña       stat = cusparseDestroy(handle);CHKERRCUDA(stat);
18987f756511SDominic Meiser     }
18997f756511SDominic Meiser     delete *trifactors;
19007f756511SDominic Meiser     *trifactors = 0;
19017f756511SDominic Meiser   }
19027f756511SDominic Meiser   PetscFunctionReturn(0);
19037f756511SDominic Meiser }
19047f756511SDominic Meiser 
1905