xref: /petsc/src/mat/impls/aij/seq/seqcusparse/aijcusparse.cu (revision ca6ae6e6368cd311feda6a11de4b5c9e1f1aea8e)
19ae82921SPaul Mullowney /*
29ae82921SPaul Mullowney   Defines the basic matrix operations for the AIJ (compressed row)
3bc3f50f2SPaul Mullowney   matrix storage format using the CUSPARSE library,
49ae82921SPaul Mullowney */
5dced61a5SBarry Smith #define PETSC_SKIP_SPINLOCK
69ae82921SPaul Mullowney 
73d13b8fdSMatthew G. Knepley #include <petscconf.h>
83d13b8fdSMatthew G. Knepley #include <../src/mat/impls/aij/seq/aij.h>          /*I "petscmat.h" I*/
9087f3262SPaul Mullowney #include <../src/mat/impls/sbaij/seq/sbaij.h>
103d13b8fdSMatthew G. Knepley #include <../src/vec/vec/impls/dvecimpl.h>
11af0996ceSBarry Smith #include <petsc/private/vecimpl.h>
129ae82921SPaul Mullowney #undef VecType
133d13b8fdSMatthew G. Knepley #include <../src/mat/impls/aij/seq/seqcusparse/cusparsematimpl.h>
14bc3f50f2SPaul Mullowney 
15e057df02SPaul Mullowney const char *const MatCUSPARSEStorageFormats[] = {"CSR","ELL","HYB","MatCUSPARSEStorageFormat","MAT_CUSPARSE_",0};
169ae82921SPaul Mullowney 
17087f3262SPaul Mullowney static PetscErrorCode MatICCFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,const MatFactorInfo*);
18087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,const MatFactorInfo*);
19087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat,Mat,const MatFactorInfo*);
20087f3262SPaul Mullowney 
216fa9248bSJed Brown static PetscErrorCode MatILUFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,IS,const MatFactorInfo*);
226fa9248bSJed Brown static PetscErrorCode MatLUFactorSymbolic_SeqAIJCUSPARSE(Mat,Mat,IS,IS,const MatFactorInfo*);
236fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat,Mat,const MatFactorInfo*);
24087f3262SPaul Mullowney 
256fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat,Vec,Vec);
266fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat,Vec,Vec);
276fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat,Vec,Vec);
286fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat,Vec,Vec);
294416b707SBarry Smith static PetscErrorCode MatSetFromOptions_SeqAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat);
306fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat,Vec,Vec);
316fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat,Vec,Vec,Vec);
326fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat,Vec,Vec);
336fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat,Vec,Vec,Vec);
349ae82921SPaul Mullowney 
357f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix**);
367f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSETriFactorStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct**);
377f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct**,MatCUSPARSEStorageFormat);
387f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors**);
397f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE**);
407f756511SDominic Meiser 
41b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSESetStream(Mat A,const cudaStream_t stream)
42b06137fdSPaul Mullowney {
43b06137fdSPaul Mullowney   cusparseStatus_t   stat;
44b06137fdSPaul Mullowney   Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
45b06137fdSPaul Mullowney 
46b06137fdSPaul Mullowney   PetscFunctionBegin;
47b06137fdSPaul Mullowney   cusparsestruct->stream = stream;
48c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetStream(cusparsestruct->handle,cusparsestruct->stream);CHKERRCUDA(stat);
49b06137fdSPaul Mullowney   PetscFunctionReturn(0);
50b06137fdSPaul Mullowney }
51b06137fdSPaul Mullowney 
52b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSESetHandle(Mat A,const cusparseHandle_t handle)
53b06137fdSPaul Mullowney {
54b06137fdSPaul Mullowney   cusparseStatus_t   stat;
55b06137fdSPaul Mullowney   Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
56b06137fdSPaul Mullowney 
57b06137fdSPaul Mullowney   PetscFunctionBegin;
5816a2e217SAlejandro Lamas Daviña   if (cusparsestruct->handle) {
59c41cb2e2SAlejandro Lamas Daviña     stat = cusparseDestroy(cusparsestruct->handle);CHKERRCUDA(stat);
6016a2e217SAlejandro Lamas Daviña   }
61b06137fdSPaul Mullowney   cusparsestruct->handle = handle;
62c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat);
63b06137fdSPaul Mullowney   PetscFunctionReturn(0);
64b06137fdSPaul Mullowney }
65b06137fdSPaul Mullowney 
66b06137fdSPaul Mullowney PetscErrorCode MatCUSPARSEClearHandle(Mat A)
67b06137fdSPaul Mullowney {
68b06137fdSPaul Mullowney   Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
69b06137fdSPaul Mullowney   PetscFunctionBegin;
70b06137fdSPaul Mullowney   if (cusparsestruct->handle)
71b06137fdSPaul Mullowney     cusparsestruct->handle = 0;
72b06137fdSPaul Mullowney   PetscFunctionReturn(0);
73b06137fdSPaul Mullowney }
74b06137fdSPaul Mullowney 
759ae82921SPaul Mullowney PetscErrorCode MatFactorGetSolverPackage_seqaij_cusparse(Mat A,const MatSolverPackage *type)
769ae82921SPaul Mullowney {
779ae82921SPaul Mullowney   PetscFunctionBegin;
789ae82921SPaul Mullowney   *type = MATSOLVERCUSPARSE;
799ae82921SPaul Mullowney   PetscFunctionReturn(0);
809ae82921SPaul Mullowney }
819ae82921SPaul Mullowney 
82c708e6cdSJed Brown /*MC
83087f3262SPaul Mullowney   MATSOLVERCUSPARSE = "cusparse" - A matrix type providing triangular solvers for seq matrices
84087f3262SPaul Mullowney   on a single GPU of type, seqaijcusparse, aijcusparse, or seqaijcusp, aijcusp. Currently supported
85087f3262SPaul Mullowney   algorithms are ILU(k) and ICC(k). Typically, deeper factorizations (larger k) results in poorer
86087f3262SPaul Mullowney   performance in the triangular solves. Full LU, and Cholesky decompositions can be solved through the
87087f3262SPaul Mullowney   CUSPARSE triangular solve algorithm. However, the performance can be quite poor and thus these
88087f3262SPaul Mullowney   algorithms are not recommended. This class does NOT support direct solver operations.
89c708e6cdSJed Brown 
909ae82921SPaul Mullowney   Level: beginner
91c708e6cdSJed Brown 
92c708e6cdSJed Brown .seealso: PCFactorSetMatSolverPackage(), MatSolverPackage, MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation
93c708e6cdSJed Brown M*/
949ae82921SPaul Mullowney 
9542c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat A,MatFactorType ftype,Mat *B)
969ae82921SPaul Mullowney {
979ae82921SPaul Mullowney   PetscErrorCode ierr;
98bc3f50f2SPaul Mullowney   PetscInt       n = A->rmap->n;
999ae82921SPaul Mullowney 
1009ae82921SPaul Mullowney   PetscFunctionBegin;
101bc3f50f2SPaul Mullowney   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
102404133a2SPaul Mullowney   (*B)->factortype = ftype;
103bc3f50f2SPaul Mullowney   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
1049ae82921SPaul Mullowney   ierr = MatSetType(*B,MATSEQAIJCUSPARSE);CHKERRQ(ierr);
1052205254eSKarl Rupp 
106087f3262SPaul Mullowney   if (ftype == MAT_FACTOR_LU || ftype == MAT_FACTOR_ILU || ftype == MAT_FACTOR_ILUDT) {
10733d57670SJed Brown     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
1089ae82921SPaul Mullowney     (*B)->ops->ilufactorsymbolic = MatILUFactorSymbolic_SeqAIJCUSPARSE;
1099ae82921SPaul Mullowney     (*B)->ops->lufactorsymbolic  = MatLUFactorSymbolic_SeqAIJCUSPARSE;
110087f3262SPaul Mullowney   } else if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
111087f3262SPaul Mullowney     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqAIJCUSPARSE;
112087f3262SPaul Mullowney     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqAIJCUSPARSE;
1139ae82921SPaul Mullowney   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported for CUSPARSE Matrix Types");
114bc3f50f2SPaul Mullowney 
115fa03d054SJed Brown   ierr = MatSeqAIJSetPreallocation(*B,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr);
11662a20339SJed Brown   ierr = PetscObjectComposeFunction((PetscObject)(*B),"MatFactorGetSolverPackage_C",MatFactorGetSolverPackage_seqaij_cusparse);CHKERRQ(ierr);
1179ae82921SPaul Mullowney   PetscFunctionReturn(0);
1189ae82921SPaul Mullowney }
1199ae82921SPaul Mullowney 
120bc3f50f2SPaul Mullowney PETSC_INTERN PetscErrorCode MatCUSPARSESetFormat_SeqAIJCUSPARSE(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format)
121ca45077fSPaul Mullowney {
122aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
1236e111a19SKarl Rupp 
124ca45077fSPaul Mullowney   PetscFunctionBegin;
1252692e278SPaul Mullowney #if CUDA_VERSION>=4020
126ca45077fSPaul Mullowney   switch (op) {
127e057df02SPaul Mullowney   case MAT_CUSPARSE_MULT:
128aa372e3fSPaul Mullowney     cusparsestruct->format = format;
129ca45077fSPaul Mullowney     break;
130e057df02SPaul Mullowney   case MAT_CUSPARSE_ALL:
131aa372e3fSPaul Mullowney     cusparsestruct->format = format;
132ca45077fSPaul Mullowney     break;
133ca45077fSPaul Mullowney   default:
13436d62e41SPaul Mullowney     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unsupported operation %d for MatCUSPARSEFormatOperation. MAT_CUSPARSE_MULT and MAT_CUSPARSE_ALL are currently supported.",op);
135ca45077fSPaul Mullowney   }
1362692e278SPaul Mullowney #else
1376c4ed002SBarry 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.");
1382692e278SPaul Mullowney #endif
139ca45077fSPaul Mullowney   PetscFunctionReturn(0);
140ca45077fSPaul Mullowney }
1419ae82921SPaul Mullowney 
142e057df02SPaul Mullowney /*@
143e057df02SPaul Mullowney    MatCUSPARSESetFormat - Sets the storage format of CUSPARSE matrices for a particular
144e057df02SPaul Mullowney    operation. Only the MatMult operation can use different GPU storage formats
145aa372e3fSPaul Mullowney    for MPIAIJCUSPARSE matrices.
146e057df02SPaul Mullowney    Not Collective
147e057df02SPaul Mullowney 
148e057df02SPaul Mullowney    Input Parameters:
1498468deeeSKarl Rupp +  A - Matrix of type SEQAIJCUSPARSE
15036d62e41SPaul Mullowney .  op - MatCUSPARSEFormatOperation. SEQAIJCUSPARSE matrices support MAT_CUSPARSE_MULT and MAT_CUSPARSE_ALL. MPIAIJCUSPARSE matrices support MAT_CUSPARSE_MULT_DIAG, MAT_CUSPARSE_MULT_OFFDIAG, and MAT_CUSPARSE_ALL.
1512692e278SPaul Mullowney -  format - MatCUSPARSEStorageFormat (one of MAT_CUSPARSE_CSR, MAT_CUSPARSE_ELL, MAT_CUSPARSE_HYB. The latter two require CUDA 4.2)
152e057df02SPaul Mullowney 
153e057df02SPaul Mullowney    Output Parameter:
154e057df02SPaul Mullowney 
155e057df02SPaul Mullowney    Level: intermediate
156e057df02SPaul Mullowney 
1578468deeeSKarl Rupp .seealso: MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation
158e057df02SPaul Mullowney @*/
159e057df02SPaul Mullowney PetscErrorCode MatCUSPARSESetFormat(Mat A,MatCUSPARSEFormatOperation op,MatCUSPARSEStorageFormat format)
160e057df02SPaul Mullowney {
161e057df02SPaul Mullowney   PetscErrorCode ierr;
1626e111a19SKarl Rupp 
163e057df02SPaul Mullowney   PetscFunctionBegin;
164e057df02SPaul Mullowney   PetscValidHeaderSpecific(A, MAT_CLASSID,1);
165e057df02SPaul Mullowney   ierr = PetscTryMethod(A, "MatCUSPARSESetFormat_C",(Mat,MatCUSPARSEFormatOperation,MatCUSPARSEStorageFormat),(A,op,format));CHKERRQ(ierr);
166e057df02SPaul Mullowney   PetscFunctionReturn(0);
167e057df02SPaul Mullowney }
168e057df02SPaul Mullowney 
1694416b707SBarry Smith static PetscErrorCode MatSetFromOptions_SeqAIJCUSPARSE(PetscOptionItems *PetscOptionsObject,Mat A)
1709ae82921SPaul Mullowney {
1719ae82921SPaul Mullowney   PetscErrorCode           ierr;
172e057df02SPaul Mullowney   MatCUSPARSEStorageFormat format;
1739ae82921SPaul Mullowney   PetscBool                flg;
174a183c035SDominic Meiser   Mat_SeqAIJCUSPARSE       *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
1756e111a19SKarl Rupp 
1769ae82921SPaul Mullowney   PetscFunctionBegin;
177e55864a3SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"SeqAIJCUSPARSE options");CHKERRQ(ierr);
178e057df02SPaul Mullowney   ierr = PetscObjectOptionsBegin((PetscObject)A);
1799ae82921SPaul Mullowney   if (A->factortype==MAT_FACTOR_NONE) {
180e057df02SPaul Mullowney     ierr = PetscOptionsEnum("-mat_cusparse_mult_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV",
181a183c035SDominic Meiser                             "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr);
182e057df02SPaul Mullowney     if (flg) {
183e057df02SPaul Mullowney       ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_MULT,format);CHKERRQ(ierr);
184045c96e1SPaul Mullowney     }
1859ae82921SPaul Mullowney   }
1864c87dfd4SPaul Mullowney   ierr = PetscOptionsEnum("-mat_cusparse_storage_format","sets storage format of (seq)aijcusparse gpu matrices for SpMV and TriSolve",
187a183c035SDominic Meiser                           "MatCUSPARSESetFormat",MatCUSPARSEStorageFormats,(PetscEnum)cusparsestruct->format,(PetscEnum*)&format,&flg);CHKERRQ(ierr);
1884c87dfd4SPaul Mullowney   if (flg) {
1894c87dfd4SPaul Mullowney     ierr = MatCUSPARSESetFormat(A,MAT_CUSPARSE_ALL,format);CHKERRQ(ierr);
1904c87dfd4SPaul Mullowney   }
1919ae82921SPaul Mullowney   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1929ae82921SPaul Mullowney   PetscFunctionReturn(0);
1939ae82921SPaul Mullowney 
1949ae82921SPaul Mullowney }
1959ae82921SPaul Mullowney 
1966fa9248bSJed Brown static PetscErrorCode MatILUFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
1979ae82921SPaul Mullowney {
1989ae82921SPaul Mullowney   PetscErrorCode ierr;
1999ae82921SPaul Mullowney 
2009ae82921SPaul Mullowney   PetscFunctionBegin;
2019ae82921SPaul Mullowney   ierr = MatILUFactorSymbolic_SeqAIJ(B,A,isrow,iscol,info);CHKERRQ(ierr);
2029ae82921SPaul Mullowney   B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJCUSPARSE;
2039ae82921SPaul Mullowney   PetscFunctionReturn(0);
2049ae82921SPaul Mullowney }
2059ae82921SPaul Mullowney 
2066fa9248bSJed Brown static PetscErrorCode MatLUFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
2079ae82921SPaul Mullowney {
2089ae82921SPaul Mullowney   PetscErrorCode ierr;
2099ae82921SPaul Mullowney 
2109ae82921SPaul Mullowney   PetscFunctionBegin;
2119ae82921SPaul Mullowney   ierr = MatLUFactorSymbolic_SeqAIJ(B,A,isrow,iscol,info);CHKERRQ(ierr);
2129ae82921SPaul Mullowney   B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJCUSPARSE;
2139ae82921SPaul Mullowney   PetscFunctionReturn(0);
2149ae82921SPaul Mullowney }
2159ae82921SPaul Mullowney 
216087f3262SPaul Mullowney static PetscErrorCode MatICCFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS perm,const MatFactorInfo *info)
217087f3262SPaul Mullowney {
218087f3262SPaul Mullowney   PetscErrorCode ierr;
219087f3262SPaul Mullowney 
220087f3262SPaul Mullowney   PetscFunctionBegin;
221087f3262SPaul Mullowney   ierr = MatICCFactorSymbolic_SeqAIJ(B,A,perm,info);CHKERRQ(ierr);
222087f3262SPaul Mullowney   B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJCUSPARSE;
223087f3262SPaul Mullowney   PetscFunctionReturn(0);
224087f3262SPaul Mullowney }
225087f3262SPaul Mullowney 
226087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJCUSPARSE(Mat B,Mat A,IS perm,const MatFactorInfo *info)
227087f3262SPaul Mullowney {
228087f3262SPaul Mullowney   PetscErrorCode ierr;
229087f3262SPaul Mullowney 
230087f3262SPaul Mullowney   PetscFunctionBegin;
231087f3262SPaul Mullowney   ierr = MatCholeskyFactorSymbolic_SeqAIJ(B,A,perm,info);CHKERRQ(ierr);
232087f3262SPaul Mullowney   B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJCUSPARSE;
233087f3262SPaul Mullowney   PetscFunctionReturn(0);
234087f3262SPaul Mullowney }
235087f3262SPaul Mullowney 
236087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILULowerTriMatrix(Mat A)
2379ae82921SPaul Mullowney {
2389ae82921SPaul Mullowney   Mat_SeqAIJ                        *a = (Mat_SeqAIJ*)A->data;
2399ae82921SPaul Mullowney   PetscInt                          n = A->rmap->n;
2409ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
241aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
2429ae82921SPaul Mullowney   cusparseStatus_t                  stat;
2439ae82921SPaul Mullowney   const PetscInt                    *ai = a->i,*aj = a->j,*vi;
2449ae82921SPaul Mullowney   const MatScalar                   *aa = a->a,*v;
2459ae82921SPaul Mullowney   PetscInt                          *AiLo, *AjLo;
2469ae82921SPaul Mullowney   PetscScalar                       *AALo;
2479ae82921SPaul Mullowney   PetscInt                          i,nz, nzLower, offset, rowOffset;
248b175d8bbSPaul Mullowney   PetscErrorCode                    ierr;
2499ae82921SPaul Mullowney 
2509ae82921SPaul Mullowney   PetscFunctionBegin;
251c41cb2e2SAlejandro Lamas Daviña   if (A->valid_GPU_matrix == PETSC_CUDA_UNALLOCATED || A->valid_GPU_matrix == PETSC_CUDA_CPU) {
2529ae82921SPaul Mullowney     try {
2539ae82921SPaul Mullowney       /* first figure out the number of nonzeros in the lower triangular matrix including 1's on the diagonal. */
2549ae82921SPaul Mullowney       nzLower=n+ai[n]-ai[1];
2559ae82921SPaul Mullowney 
2569ae82921SPaul Mullowney       /* Allocate Space for the lower triangular matrix */
257c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AiLo, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr);
258c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AjLo, nzLower*sizeof(PetscInt));CHKERRCUDA(ierr);
259c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AALo, nzLower*sizeof(PetscScalar));CHKERRCUDA(ierr);
2609ae82921SPaul Mullowney 
2619ae82921SPaul Mullowney       /* Fill the lower triangular matrix */
2629ae82921SPaul Mullowney       AiLo[0]  = (PetscInt) 0;
2639ae82921SPaul Mullowney       AiLo[n]  = nzLower;
2649ae82921SPaul Mullowney       AjLo[0]  = (PetscInt) 0;
2659ae82921SPaul Mullowney       AALo[0]  = (MatScalar) 1.0;
2669ae82921SPaul Mullowney       v        = aa;
2679ae82921SPaul Mullowney       vi       = aj;
2689ae82921SPaul Mullowney       offset   = 1;
2699ae82921SPaul Mullowney       rowOffset= 1;
2709ae82921SPaul Mullowney       for (i=1; i<n; i++) {
2719ae82921SPaul Mullowney         nz = ai[i+1] - ai[i];
272e057df02SPaul Mullowney         /* additional 1 for the term on the diagonal */
2739ae82921SPaul Mullowney         AiLo[i]    = rowOffset;
2749ae82921SPaul Mullowney         rowOffset += nz+1;
2759ae82921SPaul Mullowney 
2769ae82921SPaul Mullowney         ierr = PetscMemcpy(&(AjLo[offset]), vi, nz*sizeof(PetscInt));CHKERRQ(ierr);
2779ae82921SPaul Mullowney         ierr = PetscMemcpy(&(AALo[offset]), v, nz*sizeof(PetscScalar));CHKERRQ(ierr);
2789ae82921SPaul Mullowney 
2799ae82921SPaul Mullowney         offset      += nz;
2809ae82921SPaul Mullowney         AjLo[offset] = (PetscInt) i;
2819ae82921SPaul Mullowney         AALo[offset] = (MatScalar) 1.0;
2829ae82921SPaul Mullowney         offset      += 1;
2839ae82921SPaul Mullowney 
2849ae82921SPaul Mullowney         v  += nz;
2859ae82921SPaul Mullowney         vi += nz;
2869ae82921SPaul Mullowney       }
2872205254eSKarl Rupp 
288aa372e3fSPaul Mullowney       /* allocate space for the triangular factor information */
289aa372e3fSPaul Mullowney       loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct;
2902205254eSKarl Rupp 
291aa372e3fSPaul Mullowney       /* Create the matrix description */
292c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUDA(stat);
293c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat);
294c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat);
295c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_LOWER);CHKERRCUDA(stat);
296c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUDA(stat);
297aa372e3fSPaul Mullowney 
298aa372e3fSPaul Mullowney       /* Create the solve analysis information */
299c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUDA(stat);
300aa372e3fSPaul Mullowney 
301aa372e3fSPaul Mullowney       /* set the operation */
302aa372e3fSPaul Mullowney       loTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
303aa372e3fSPaul Mullowney 
304aa372e3fSPaul Mullowney       /* set the matrix */
305aa372e3fSPaul Mullowney       loTriFactor->csrMat = new CsrMatrix;
306aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_rows = n;
307aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_cols = n;
308aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_entries = nzLower;
309aa372e3fSPaul Mullowney 
310aa372e3fSPaul Mullowney       loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1);
311aa372e3fSPaul Mullowney       loTriFactor->csrMat->row_offsets->assign(AiLo, AiLo+n+1);
312aa372e3fSPaul Mullowney 
313aa372e3fSPaul Mullowney       loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzLower);
314aa372e3fSPaul Mullowney       loTriFactor->csrMat->column_indices->assign(AjLo, AjLo+nzLower);
315aa372e3fSPaul Mullowney 
316aa372e3fSPaul Mullowney       loTriFactor->csrMat->values = new THRUSTARRAY(nzLower);
317aa372e3fSPaul Mullowney       loTriFactor->csrMat->values->assign(AALo, AALo+nzLower);
318aa372e3fSPaul Mullowney 
319aa372e3fSPaul Mullowney       /* perform the solve analysis */
320aa372e3fSPaul Mullowney       stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp,
321aa372e3fSPaul Mullowney                                loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr,
322aa372e3fSPaul Mullowney                                loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(),
323c41cb2e2SAlejandro Lamas Daviña                                loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUDA(stat);
324aa372e3fSPaul Mullowney 
325aa372e3fSPaul Mullowney       /* assign the pointer. Is this really necessary? */
326aa372e3fSPaul Mullowney       ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor;
3272205254eSKarl Rupp 
328c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AiLo);CHKERRCUDA(ierr);
329c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AjLo);CHKERRCUDA(ierr);
330c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AALo);CHKERRCUDA(ierr);
3319ae82921SPaul Mullowney     } catch(char *ex) {
3329ae82921SPaul Mullowney       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
3339ae82921SPaul Mullowney     }
3349ae82921SPaul Mullowney   }
3359ae82921SPaul Mullowney   PetscFunctionReturn(0);
3369ae82921SPaul Mullowney }
3379ae82921SPaul Mullowney 
338087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(Mat A)
3399ae82921SPaul Mullowney {
3409ae82921SPaul Mullowney   Mat_SeqAIJ                        *a = (Mat_SeqAIJ*)A->data;
3419ae82921SPaul Mullowney   PetscInt                          n = A->rmap->n;
3429ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
343aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
3449ae82921SPaul Mullowney   cusparseStatus_t                  stat;
3459ae82921SPaul Mullowney   const PetscInt                    *aj = a->j,*adiag = a->diag,*vi;
3469ae82921SPaul Mullowney   const MatScalar                   *aa = a->a,*v;
3479ae82921SPaul Mullowney   PetscInt                          *AiUp, *AjUp;
3489ae82921SPaul Mullowney   PetscScalar                       *AAUp;
3499ae82921SPaul Mullowney   PetscInt                          i,nz, nzUpper, offset;
3509ae82921SPaul Mullowney   PetscErrorCode                    ierr;
3519ae82921SPaul Mullowney 
3529ae82921SPaul Mullowney   PetscFunctionBegin;
353c41cb2e2SAlejandro Lamas Daviña   if (A->valid_GPU_matrix == PETSC_CUDA_UNALLOCATED || A->valid_GPU_matrix == PETSC_CUDA_CPU) {
3549ae82921SPaul Mullowney     try {
3559ae82921SPaul Mullowney       /* next, figure out the number of nonzeros in the upper triangular matrix. */
3569ae82921SPaul Mullowney       nzUpper = adiag[0]-adiag[n];
3579ae82921SPaul Mullowney 
3589ae82921SPaul Mullowney       /* Allocate Space for the upper triangular matrix */
359c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr);
360c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(ierr);
361c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr);
3629ae82921SPaul Mullowney 
3639ae82921SPaul Mullowney       /* Fill the upper triangular matrix */
3649ae82921SPaul Mullowney       AiUp[0]=(PetscInt) 0;
3659ae82921SPaul Mullowney       AiUp[n]=nzUpper;
3669ae82921SPaul Mullowney       offset = nzUpper;
3679ae82921SPaul Mullowney       for (i=n-1; i>=0; i--) {
3689ae82921SPaul Mullowney         v  = aa + adiag[i+1] + 1;
3699ae82921SPaul Mullowney         vi = aj + adiag[i+1] + 1;
3709ae82921SPaul Mullowney 
371e057df02SPaul Mullowney         /* number of elements NOT on the diagonal */
3729ae82921SPaul Mullowney         nz = adiag[i] - adiag[i+1]-1;
3739ae82921SPaul Mullowney 
374e057df02SPaul Mullowney         /* decrement the offset */
3759ae82921SPaul Mullowney         offset -= (nz+1);
3769ae82921SPaul Mullowney 
377e057df02SPaul Mullowney         /* first, set the diagonal elements */
3789ae82921SPaul Mullowney         AjUp[offset] = (PetscInt) i;
37909f51544SAlejandro Lamas Daviña         AAUp[offset] = (MatScalar)1./v[nz];
3809ae82921SPaul Mullowney         AiUp[i]      = AiUp[i+1] - (nz+1);
3819ae82921SPaul Mullowney 
3829ae82921SPaul Mullowney         ierr = PetscMemcpy(&(AjUp[offset+1]), vi, nz*sizeof(PetscInt));CHKERRQ(ierr);
3839ae82921SPaul Mullowney         ierr = PetscMemcpy(&(AAUp[offset+1]), v, nz*sizeof(PetscScalar));CHKERRQ(ierr);
3849ae82921SPaul Mullowney       }
3852205254eSKarl Rupp 
386aa372e3fSPaul Mullowney       /* allocate space for the triangular factor information */
387aa372e3fSPaul Mullowney       upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct;
3882205254eSKarl Rupp 
389aa372e3fSPaul Mullowney       /* Create the matrix description */
390c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUDA(stat);
391c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat);
392c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat);
393c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat);
394c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUDA(stat);
395aa372e3fSPaul Mullowney 
396aa372e3fSPaul Mullowney       /* Create the solve analysis information */
397c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUDA(stat);
398aa372e3fSPaul Mullowney 
399aa372e3fSPaul Mullowney       /* set the operation */
400aa372e3fSPaul Mullowney       upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
401aa372e3fSPaul Mullowney 
402aa372e3fSPaul Mullowney       /* set the matrix */
403aa372e3fSPaul Mullowney       upTriFactor->csrMat = new CsrMatrix;
404aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_rows = n;
405aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_cols = n;
406aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_entries = nzUpper;
407aa372e3fSPaul Mullowney 
408aa372e3fSPaul Mullowney       upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(n+1);
409aa372e3fSPaul Mullowney       upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+n+1);
410aa372e3fSPaul Mullowney 
411aa372e3fSPaul Mullowney       upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(nzUpper);
412aa372e3fSPaul Mullowney       upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+nzUpper);
413aa372e3fSPaul Mullowney 
414aa372e3fSPaul Mullowney       upTriFactor->csrMat->values = new THRUSTARRAY(nzUpper);
415aa372e3fSPaul Mullowney       upTriFactor->csrMat->values->assign(AAUp, AAUp+nzUpper);
416aa372e3fSPaul Mullowney 
417aa372e3fSPaul Mullowney       /* perform the solve analysis */
418aa372e3fSPaul Mullowney       stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp,
419aa372e3fSPaul Mullowney                                upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr,
420aa372e3fSPaul Mullowney                                upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(),
421c41cb2e2SAlejandro Lamas Daviña                                upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUDA(stat);
422aa372e3fSPaul Mullowney 
423aa372e3fSPaul Mullowney       /* assign the pointer. Is this really necessary? */
424aa372e3fSPaul Mullowney       ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor;
4252205254eSKarl Rupp 
426c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AiUp);CHKERRCUDA(ierr);
427c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AjUp);CHKERRCUDA(ierr);
428c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AAUp);CHKERRCUDA(ierr);
4299ae82921SPaul Mullowney     } catch(char *ex) {
4309ae82921SPaul Mullowney       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
4319ae82921SPaul Mullowney     }
4329ae82921SPaul Mullowney   }
4339ae82921SPaul Mullowney   PetscFunctionReturn(0);
4349ae82921SPaul Mullowney }
4359ae82921SPaul Mullowney 
436087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(Mat A)
4379ae82921SPaul Mullowney {
4389ae82921SPaul Mullowney   PetscErrorCode               ierr;
4399ae82921SPaul Mullowney   Mat_SeqAIJ                   *a                  = (Mat_SeqAIJ*)A->data;
4409ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
4419ae82921SPaul Mullowney   IS                           isrow = a->row,iscol = a->icol;
4429ae82921SPaul Mullowney   PetscBool                    row_identity,col_identity;
4439ae82921SPaul Mullowney   const PetscInt               *r,*c;
4449ae82921SPaul Mullowney   PetscInt                     n = A->rmap->n;
4459ae82921SPaul Mullowney 
4469ae82921SPaul Mullowney   PetscFunctionBegin;
447087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEBuildILULowerTriMatrix(A);CHKERRQ(ierr);
448087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEBuildILUUpperTriMatrix(A);CHKERRQ(ierr);
4492205254eSKarl Rupp 
450aa372e3fSPaul Mullowney   cusparseTriFactors->workVector = new THRUSTARRAY;
451aa372e3fSPaul Mullowney   cusparseTriFactors->workVector->resize(n);
452aa372e3fSPaul Mullowney   cusparseTriFactors->nnz=a->nz;
4539ae82921SPaul Mullowney 
454c41cb2e2SAlejandro Lamas Daviña   A->valid_GPU_matrix = PETSC_CUDA_BOTH;
455e057df02SPaul Mullowney   /*lower triangular indices */
4569ae82921SPaul Mullowney   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
4579ae82921SPaul Mullowney   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
4582205254eSKarl Rupp   if (!row_identity) {
459aa372e3fSPaul Mullowney     cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n);
460aa372e3fSPaul Mullowney     cusparseTriFactors->rpermIndices->assign(r, r+n);
4612205254eSKarl Rupp   }
4629ae82921SPaul Mullowney   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
4639ae82921SPaul Mullowney 
464e057df02SPaul Mullowney   /*upper triangular indices */
4659ae82921SPaul Mullowney   ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr);
4669ae82921SPaul Mullowney   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
4672205254eSKarl Rupp   if (!col_identity) {
468aa372e3fSPaul Mullowney     cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n);
469aa372e3fSPaul Mullowney     cusparseTriFactors->cpermIndices->assign(c, c+n);
4702205254eSKarl Rupp   }
4719ae82921SPaul Mullowney   ierr = ISRestoreIndices(iscol,&c);CHKERRQ(ierr);
4729ae82921SPaul Mullowney   PetscFunctionReturn(0);
4739ae82921SPaul Mullowney }
4749ae82921SPaul Mullowney 
475087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEBuildICCTriMatrices(Mat A)
476087f3262SPaul Mullowney {
477087f3262SPaul Mullowney   Mat_SeqAIJ                        *a = (Mat_SeqAIJ*)A->data;
478087f3262SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
479aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
480aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
481087f3262SPaul Mullowney   cusparseStatus_t                  stat;
482087f3262SPaul Mullowney   PetscErrorCode                    ierr;
483087f3262SPaul Mullowney   PetscInt                          *AiUp, *AjUp;
484087f3262SPaul Mullowney   PetscScalar                       *AAUp;
485087f3262SPaul Mullowney   PetscScalar                       *AALo;
486087f3262SPaul Mullowney   PetscInt                          nzUpper = a->nz,n = A->rmap->n,i,offset,nz,j;
487087f3262SPaul Mullowney   Mat_SeqSBAIJ                      *b = (Mat_SeqSBAIJ*)A->data;
488087f3262SPaul Mullowney   const PetscInt                    *ai = b->i,*aj = b->j,*vj;
489087f3262SPaul Mullowney   const MatScalar                   *aa = b->a,*v;
490087f3262SPaul Mullowney 
491087f3262SPaul Mullowney   PetscFunctionBegin;
492c41cb2e2SAlejandro Lamas Daviña   if (A->valid_GPU_matrix == PETSC_CUDA_UNALLOCATED || A->valid_GPU_matrix == PETSC_CUDA_CPU) {
493087f3262SPaul Mullowney     try {
494087f3262SPaul Mullowney       /* Allocate Space for the upper triangular matrix */
495c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AiUp, (n+1)*sizeof(PetscInt));CHKERRCUDA(ierr);
496c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AjUp, nzUpper*sizeof(PetscInt));CHKERRCUDA(ierr);
497c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AAUp, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr);
498c41cb2e2SAlejandro Lamas Daviña       ierr = cudaMallocHost((void**) &AALo, nzUpper*sizeof(PetscScalar));CHKERRCUDA(ierr);
499087f3262SPaul Mullowney 
500087f3262SPaul Mullowney       /* Fill the upper triangular matrix */
501087f3262SPaul Mullowney       AiUp[0]=(PetscInt) 0;
502087f3262SPaul Mullowney       AiUp[n]=nzUpper;
503087f3262SPaul Mullowney       offset = 0;
504087f3262SPaul Mullowney       for (i=0; i<n; i++) {
505087f3262SPaul Mullowney         /* set the pointers */
506087f3262SPaul Mullowney         v  = aa + ai[i];
507087f3262SPaul Mullowney         vj = aj + ai[i];
508087f3262SPaul Mullowney         nz = ai[i+1] - ai[i] - 1; /* exclude diag[i] */
509087f3262SPaul Mullowney 
510087f3262SPaul Mullowney         /* first, set the diagonal elements */
511087f3262SPaul Mullowney         AjUp[offset] = (PetscInt) i;
51209f51544SAlejandro Lamas Daviña         AAUp[offset] = (MatScalar)1.0/v[nz];
513087f3262SPaul Mullowney         AiUp[i]      = offset;
51409f51544SAlejandro Lamas Daviña         AALo[offset] = (MatScalar)1.0/v[nz];
515087f3262SPaul Mullowney 
516087f3262SPaul Mullowney         offset+=1;
517087f3262SPaul Mullowney         if (nz>0) {
518087f3262SPaul Mullowney           ierr = PetscMemcpy(&(AjUp[offset]), vj, nz*sizeof(PetscInt));CHKERRQ(ierr);
519087f3262SPaul Mullowney           ierr = PetscMemcpy(&(AAUp[offset]), v, nz*sizeof(PetscScalar));CHKERRQ(ierr);
520087f3262SPaul Mullowney           for (j=offset; j<offset+nz; j++) {
521087f3262SPaul Mullowney             AAUp[j] = -AAUp[j];
522087f3262SPaul Mullowney             AALo[j] = AAUp[j]/v[nz];
523087f3262SPaul Mullowney           }
524087f3262SPaul Mullowney           offset+=nz;
525087f3262SPaul Mullowney         }
526087f3262SPaul Mullowney       }
527087f3262SPaul Mullowney 
528aa372e3fSPaul Mullowney       /* allocate space for the triangular factor information */
529aa372e3fSPaul Mullowney       upTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct;
530087f3262SPaul Mullowney 
531aa372e3fSPaul Mullowney       /* Create the matrix description */
532c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateMatDescr(&upTriFactor->descr);CHKERRCUDA(stat);
533c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatIndexBase(upTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat);
534c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatType(upTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat);
535c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatFillMode(upTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat);
536c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatDiagType(upTriFactor->descr, CUSPARSE_DIAG_TYPE_UNIT);CHKERRCUDA(stat);
537087f3262SPaul Mullowney 
538aa372e3fSPaul Mullowney       /* Create the solve analysis information */
539c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateSolveAnalysisInfo(&upTriFactor->solveInfo);CHKERRCUDA(stat);
540aa372e3fSPaul Mullowney 
541aa372e3fSPaul Mullowney       /* set the operation */
542aa372e3fSPaul Mullowney       upTriFactor->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
543aa372e3fSPaul Mullowney 
544aa372e3fSPaul Mullowney       /* set the matrix */
545aa372e3fSPaul Mullowney       upTriFactor->csrMat = new CsrMatrix;
546aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_rows = A->rmap->n;
547aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_cols = A->cmap->n;
548aa372e3fSPaul Mullowney       upTriFactor->csrMat->num_entries = a->nz;
549aa372e3fSPaul Mullowney 
550aa372e3fSPaul Mullowney       upTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
551aa372e3fSPaul Mullowney       upTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1);
552aa372e3fSPaul Mullowney 
553aa372e3fSPaul Mullowney       upTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz);
554aa372e3fSPaul Mullowney       upTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz);
555aa372e3fSPaul Mullowney 
556aa372e3fSPaul Mullowney       upTriFactor->csrMat->values = new THRUSTARRAY(a->nz);
557aa372e3fSPaul Mullowney       upTriFactor->csrMat->values->assign(AAUp, AAUp+a->nz);
558aa372e3fSPaul Mullowney 
559aa372e3fSPaul Mullowney       /* perform the solve analysis */
560aa372e3fSPaul Mullowney       stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactor->solveOp,
561aa372e3fSPaul Mullowney                                upTriFactor->csrMat->num_rows, upTriFactor->csrMat->num_entries, upTriFactor->descr,
562aa372e3fSPaul Mullowney                                upTriFactor->csrMat->values->data().get(), upTriFactor->csrMat->row_offsets->data().get(),
563c41cb2e2SAlejandro Lamas Daviña                                upTriFactor->csrMat->column_indices->data().get(), upTriFactor->solveInfo);CHKERRCUDA(stat);
564aa372e3fSPaul Mullowney 
565aa372e3fSPaul Mullowney       /* assign the pointer. Is this really necessary? */
566aa372e3fSPaul Mullowney       ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtr = upTriFactor;
567aa372e3fSPaul Mullowney 
568aa372e3fSPaul Mullowney       /* allocate space for the triangular factor information */
569aa372e3fSPaul Mullowney       loTriFactor = new Mat_SeqAIJCUSPARSETriFactorStruct;
570aa372e3fSPaul Mullowney 
571aa372e3fSPaul Mullowney       /* Create the matrix description */
572c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateMatDescr(&loTriFactor->descr);CHKERRCUDA(stat);
573c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatIndexBase(loTriFactor->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat);
574c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatType(loTriFactor->descr, CUSPARSE_MATRIX_TYPE_TRIANGULAR);CHKERRCUDA(stat);
575c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatFillMode(loTriFactor->descr, CUSPARSE_FILL_MODE_UPPER);CHKERRCUDA(stat);
576c41cb2e2SAlejandro Lamas Daviña       stat = cusparseSetMatDiagType(loTriFactor->descr, CUSPARSE_DIAG_TYPE_NON_UNIT);CHKERRCUDA(stat);
577aa372e3fSPaul Mullowney 
578aa372e3fSPaul Mullowney       /* Create the solve analysis information */
579c41cb2e2SAlejandro Lamas Daviña       stat = cusparseCreateSolveAnalysisInfo(&loTriFactor->solveInfo);CHKERRCUDA(stat);
580aa372e3fSPaul Mullowney 
581aa372e3fSPaul Mullowney       /* set the operation */
582aa372e3fSPaul Mullowney       loTriFactor->solveOp = CUSPARSE_OPERATION_TRANSPOSE;
583aa372e3fSPaul Mullowney 
584aa372e3fSPaul Mullowney       /* set the matrix */
585aa372e3fSPaul Mullowney       loTriFactor->csrMat = new CsrMatrix;
586aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_rows = A->rmap->n;
587aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_cols = A->cmap->n;
588aa372e3fSPaul Mullowney       loTriFactor->csrMat->num_entries = a->nz;
589aa372e3fSPaul Mullowney 
590aa372e3fSPaul Mullowney       loTriFactor->csrMat->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
591aa372e3fSPaul Mullowney       loTriFactor->csrMat->row_offsets->assign(AiUp, AiUp+A->rmap->n+1);
592aa372e3fSPaul Mullowney 
593aa372e3fSPaul Mullowney       loTriFactor->csrMat->column_indices = new THRUSTINTARRAY32(a->nz);
594aa372e3fSPaul Mullowney       loTriFactor->csrMat->column_indices->assign(AjUp, AjUp+a->nz);
595aa372e3fSPaul Mullowney 
596aa372e3fSPaul Mullowney       loTriFactor->csrMat->values = new THRUSTARRAY(a->nz);
597aa372e3fSPaul Mullowney       loTriFactor->csrMat->values->assign(AALo, AALo+a->nz);
598aa372e3fSPaul Mullowney 
599aa372e3fSPaul Mullowney       /* perform the solve analysis */
600aa372e3fSPaul Mullowney       stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactor->solveOp,
601aa372e3fSPaul Mullowney                                loTriFactor->csrMat->num_rows, loTriFactor->csrMat->num_entries, loTriFactor->descr,
602aa372e3fSPaul Mullowney                                loTriFactor->csrMat->values->data().get(), loTriFactor->csrMat->row_offsets->data().get(),
603c41cb2e2SAlejandro Lamas Daviña                                loTriFactor->csrMat->column_indices->data().get(), loTriFactor->solveInfo);CHKERRCUDA(stat);
604aa372e3fSPaul Mullowney 
605aa372e3fSPaul Mullowney       /* assign the pointer. Is this really necessary? */
606aa372e3fSPaul Mullowney       ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtr = loTriFactor;
607087f3262SPaul Mullowney 
608c41cb2e2SAlejandro Lamas Daviña       A->valid_GPU_matrix = PETSC_CUDA_BOTH;
609c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AiUp);CHKERRCUDA(ierr);
610c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AjUp);CHKERRCUDA(ierr);
611c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AAUp);CHKERRCUDA(ierr);
612c41cb2e2SAlejandro Lamas Daviña       ierr = cudaFreeHost(AALo);CHKERRCUDA(ierr);
613087f3262SPaul Mullowney     } catch(char *ex) {
614087f3262SPaul Mullowney       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
615087f3262SPaul Mullowney     }
616087f3262SPaul Mullowney   }
617087f3262SPaul Mullowney   PetscFunctionReturn(0);
618087f3262SPaul Mullowney }
619087f3262SPaul Mullowney 
620087f3262SPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(Mat A)
6219ae82921SPaul Mullowney {
6229ae82921SPaul Mullowney   PetscErrorCode               ierr;
623087f3262SPaul Mullowney   Mat_SeqAIJ                   *a                  = (Mat_SeqAIJ*)A->data;
624087f3262SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
625087f3262SPaul Mullowney   IS                           ip = a->row;
626087f3262SPaul Mullowney   const PetscInt               *rip;
627087f3262SPaul Mullowney   PetscBool                    perm_identity;
628087f3262SPaul Mullowney   PetscInt                     n = A->rmap->n;
629087f3262SPaul Mullowney 
630087f3262SPaul Mullowney   PetscFunctionBegin;
631087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEBuildICCTriMatrices(A);CHKERRQ(ierr);
632aa372e3fSPaul Mullowney   cusparseTriFactors->workVector = new THRUSTARRAY;
633aa372e3fSPaul Mullowney   cusparseTriFactors->workVector->resize(n);
634aa372e3fSPaul Mullowney   cusparseTriFactors->nnz=(a->nz-n)*2 + n;
635aa372e3fSPaul Mullowney 
636087f3262SPaul Mullowney   /*lower triangular indices */
637087f3262SPaul Mullowney   ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr);
638087f3262SPaul Mullowney   ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr);
639087f3262SPaul Mullowney   if (!perm_identity) {
640aa372e3fSPaul Mullowney     cusparseTriFactors->rpermIndices = new THRUSTINTARRAY(n);
641aa372e3fSPaul Mullowney     cusparseTriFactors->rpermIndices->assign(rip, rip+n);
642aa372e3fSPaul Mullowney     cusparseTriFactors->cpermIndices = new THRUSTINTARRAY(n);
643aa372e3fSPaul Mullowney     cusparseTriFactors->cpermIndices->assign(rip, rip+n);
644087f3262SPaul Mullowney   }
645087f3262SPaul Mullowney   ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr);
646087f3262SPaul Mullowney   PetscFunctionReturn(0);
647087f3262SPaul Mullowney }
648087f3262SPaul Mullowney 
6496fa9248bSJed Brown static PetscErrorCode MatLUFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info)
6509ae82921SPaul Mullowney {
6519ae82921SPaul Mullowney   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
6529ae82921SPaul Mullowney   IS             isrow = b->row,iscol = b->col;
6539ae82921SPaul Mullowney   PetscBool      row_identity,col_identity;
654b175d8bbSPaul Mullowney   PetscErrorCode ierr;
6559ae82921SPaul Mullowney 
6569ae82921SPaul Mullowney   PetscFunctionBegin;
6579ae82921SPaul Mullowney   ierr = MatLUFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr);
658e057df02SPaul Mullowney   /* determine which version of MatSolve needs to be used. */
6599ae82921SPaul Mullowney   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
6609ae82921SPaul Mullowney   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
661bda325fcSPaul Mullowney   if (row_identity && col_identity) {
662bda325fcSPaul Mullowney     B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering;
663bda325fcSPaul Mullowney     B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering;
664bda325fcSPaul Mullowney   } else {
665bda325fcSPaul Mullowney     B->ops->solve = MatSolve_SeqAIJCUSPARSE;
666bda325fcSPaul Mullowney     B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE;
667bda325fcSPaul Mullowney   }
6688dc1d2a3SPaul Mullowney 
669e057df02SPaul Mullowney   /* get the triangular factors */
670087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEILUAnalysisAndCopyToGPU(B);CHKERRQ(ierr);
6719ae82921SPaul Mullowney   PetscFunctionReturn(0);
6729ae82921SPaul Mullowney }
6739ae82921SPaul Mullowney 
674087f3262SPaul Mullowney static PetscErrorCode MatCholeskyFactorNumeric_SeqAIJCUSPARSE(Mat B,Mat A,const MatFactorInfo *info)
675087f3262SPaul Mullowney {
676087f3262SPaul Mullowney   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
677087f3262SPaul Mullowney   IS             ip = b->row;
678087f3262SPaul Mullowney   PetscBool      perm_identity;
679b175d8bbSPaul Mullowney   PetscErrorCode ierr;
680087f3262SPaul Mullowney 
681087f3262SPaul Mullowney   PetscFunctionBegin;
682087f3262SPaul Mullowney   ierr = MatCholeskyFactorNumeric_SeqAIJ(B,A,info);CHKERRQ(ierr);
683087f3262SPaul Mullowney 
684087f3262SPaul Mullowney   /* determine which version of MatSolve needs to be used. */
685087f3262SPaul Mullowney   ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr);
686087f3262SPaul Mullowney   if (perm_identity) {
687087f3262SPaul Mullowney     B->ops->solve = MatSolve_SeqAIJCUSPARSE_NaturalOrdering;
688087f3262SPaul Mullowney     B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering;
689087f3262SPaul Mullowney   } else {
690087f3262SPaul Mullowney     B->ops->solve = MatSolve_SeqAIJCUSPARSE;
691087f3262SPaul Mullowney     B->ops->solvetranspose = MatSolveTranspose_SeqAIJCUSPARSE;
692087f3262SPaul Mullowney   }
693087f3262SPaul Mullowney 
694087f3262SPaul Mullowney   /* get the triangular factors */
695087f3262SPaul Mullowney   ierr = MatSeqAIJCUSPARSEICCAnalysisAndCopyToGPU(B);CHKERRQ(ierr);
696087f3262SPaul Mullowney   PetscFunctionReturn(0);
697087f3262SPaul Mullowney }
6989ae82921SPaul Mullowney 
699b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(Mat A)
700bda325fcSPaul Mullowney {
701bda325fcSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
702aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
703aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
704aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
705aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
706bda325fcSPaul Mullowney   cusparseStatus_t                  stat;
707aa372e3fSPaul Mullowney   cusparseIndexBase_t               indexBase;
708aa372e3fSPaul Mullowney   cusparseMatrixType_t              matrixType;
709aa372e3fSPaul Mullowney   cusparseFillMode_t                fillMode;
710aa372e3fSPaul Mullowney   cusparseDiagType_t                diagType;
711b175d8bbSPaul Mullowney 
712bda325fcSPaul Mullowney   PetscFunctionBegin;
713bda325fcSPaul Mullowney 
714aa372e3fSPaul Mullowney   /*********************************************/
715aa372e3fSPaul Mullowney   /* Now the Transpose of the Lower Tri Factor */
716aa372e3fSPaul Mullowney   /*********************************************/
717aa372e3fSPaul Mullowney 
718aa372e3fSPaul Mullowney   /* allocate space for the transpose of the lower triangular factor */
719aa372e3fSPaul Mullowney   loTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct;
720aa372e3fSPaul Mullowney 
721aa372e3fSPaul Mullowney   /* set the matrix descriptors of the lower triangular factor */
722aa372e3fSPaul Mullowney   matrixType = cusparseGetMatType(loTriFactor->descr);
723aa372e3fSPaul Mullowney   indexBase = cusparseGetMatIndexBase(loTriFactor->descr);
724aa372e3fSPaul Mullowney   fillMode = cusparseGetMatFillMode(loTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ?
725aa372e3fSPaul Mullowney     CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER;
726aa372e3fSPaul Mullowney   diagType = cusparseGetMatDiagType(loTriFactor->descr);
727aa372e3fSPaul Mullowney 
728aa372e3fSPaul Mullowney   /* Create the matrix description */
729c41cb2e2SAlejandro Lamas Daviña   stat = cusparseCreateMatDescr(&loTriFactorT->descr);CHKERRCUDA(stat);
730c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatIndexBase(loTriFactorT->descr, indexBase);CHKERRCUDA(stat);
731c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatType(loTriFactorT->descr, matrixType);CHKERRCUDA(stat);
732c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatFillMode(loTriFactorT->descr, fillMode);CHKERRCUDA(stat);
733c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatDiagType(loTriFactorT->descr, diagType);CHKERRCUDA(stat);
734aa372e3fSPaul Mullowney 
735aa372e3fSPaul Mullowney   /* Create the solve analysis information */
736c41cb2e2SAlejandro Lamas Daviña   stat = cusparseCreateSolveAnalysisInfo(&loTriFactorT->solveInfo);CHKERRCUDA(stat);
737aa372e3fSPaul Mullowney 
738aa372e3fSPaul Mullowney   /* set the operation */
739aa372e3fSPaul Mullowney   loTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
740aa372e3fSPaul Mullowney 
741aa372e3fSPaul Mullowney   /* allocate GPU space for the CSC of the lower triangular factor*/
742aa372e3fSPaul Mullowney   loTriFactorT->csrMat = new CsrMatrix;
743aa372e3fSPaul Mullowney   loTriFactorT->csrMat->num_rows = loTriFactor->csrMat->num_rows;
744aa372e3fSPaul Mullowney   loTriFactorT->csrMat->num_cols = loTriFactor->csrMat->num_cols;
745aa372e3fSPaul Mullowney   loTriFactorT->csrMat->num_entries = loTriFactor->csrMat->num_entries;
746aa372e3fSPaul Mullowney   loTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(loTriFactor->csrMat->num_rows+1);
747aa372e3fSPaul Mullowney   loTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(loTriFactor->csrMat->num_entries);
748aa372e3fSPaul Mullowney   loTriFactorT->csrMat->values = new THRUSTARRAY(loTriFactor->csrMat->num_entries);
749aa372e3fSPaul Mullowney 
750aa372e3fSPaul Mullowney   /* compute the transpose of the lower triangular factor, i.e. the CSC */
751aa372e3fSPaul Mullowney   stat = cusparse_csr2csc(cusparseTriFactors->handle, loTriFactor->csrMat->num_rows,
752aa372e3fSPaul Mullowney                           loTriFactor->csrMat->num_cols, loTriFactor->csrMat->num_entries,
753aa372e3fSPaul Mullowney                           loTriFactor->csrMat->values->data().get(),
754aa372e3fSPaul Mullowney                           loTriFactor->csrMat->row_offsets->data().get(),
755aa372e3fSPaul Mullowney                           loTriFactor->csrMat->column_indices->data().get(),
756aa372e3fSPaul Mullowney                           loTriFactorT->csrMat->values->data().get(),
757aa372e3fSPaul Mullowney                           loTriFactorT->csrMat->column_indices->data().get(),
758aa372e3fSPaul Mullowney                           loTriFactorT->csrMat->row_offsets->data().get(),
759c41cb2e2SAlejandro Lamas Daviña                           CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat);
760aa372e3fSPaul Mullowney 
761aa372e3fSPaul Mullowney   /* perform the solve analysis on the transposed matrix */
762aa372e3fSPaul Mullowney   stat = cusparse_analysis(cusparseTriFactors->handle, loTriFactorT->solveOp,
763aa372e3fSPaul Mullowney                            loTriFactorT->csrMat->num_rows, loTriFactorT->csrMat->num_entries,
764aa372e3fSPaul Mullowney                            loTriFactorT->descr, loTriFactorT->csrMat->values->data().get(),
765aa372e3fSPaul Mullowney                            loTriFactorT->csrMat->row_offsets->data().get(), loTriFactorT->csrMat->column_indices->data().get(),
766c41cb2e2SAlejandro Lamas Daviña                            loTriFactorT->solveInfo);CHKERRCUDA(stat);
767aa372e3fSPaul Mullowney 
768aa372e3fSPaul Mullowney   /* assign the pointer. Is this really necessary? */
769aa372e3fSPaul Mullowney   ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->loTriFactorPtrTranspose = loTriFactorT;
770aa372e3fSPaul Mullowney 
771aa372e3fSPaul Mullowney   /*********************************************/
772aa372e3fSPaul Mullowney   /* Now the Transpose of the Upper Tri Factor */
773aa372e3fSPaul Mullowney   /*********************************************/
774aa372e3fSPaul Mullowney 
775aa372e3fSPaul Mullowney   /* allocate space for the transpose of the upper triangular factor */
776aa372e3fSPaul Mullowney   upTriFactorT = new Mat_SeqAIJCUSPARSETriFactorStruct;
777aa372e3fSPaul Mullowney 
778aa372e3fSPaul Mullowney   /* set the matrix descriptors of the upper triangular factor */
779aa372e3fSPaul Mullowney   matrixType = cusparseGetMatType(upTriFactor->descr);
780aa372e3fSPaul Mullowney   indexBase = cusparseGetMatIndexBase(upTriFactor->descr);
781aa372e3fSPaul Mullowney   fillMode = cusparseGetMatFillMode(upTriFactor->descr)==CUSPARSE_FILL_MODE_UPPER ?
782aa372e3fSPaul Mullowney     CUSPARSE_FILL_MODE_LOWER : CUSPARSE_FILL_MODE_UPPER;
783aa372e3fSPaul Mullowney   diagType = cusparseGetMatDiagType(upTriFactor->descr);
784aa372e3fSPaul Mullowney 
785aa372e3fSPaul Mullowney   /* Create the matrix description */
786c41cb2e2SAlejandro Lamas Daviña   stat = cusparseCreateMatDescr(&upTriFactorT->descr);CHKERRCUDA(stat);
787c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatIndexBase(upTriFactorT->descr, indexBase);CHKERRCUDA(stat);
788c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatType(upTriFactorT->descr, matrixType);CHKERRCUDA(stat);
789c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatFillMode(upTriFactorT->descr, fillMode);CHKERRCUDA(stat);
790c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatDiagType(upTriFactorT->descr, diagType);CHKERRCUDA(stat);
791aa372e3fSPaul Mullowney 
792aa372e3fSPaul Mullowney   /* Create the solve analysis information */
793c41cb2e2SAlejandro Lamas Daviña   stat = cusparseCreateSolveAnalysisInfo(&upTriFactorT->solveInfo);CHKERRCUDA(stat);
794aa372e3fSPaul Mullowney 
795aa372e3fSPaul Mullowney   /* set the operation */
796aa372e3fSPaul Mullowney   upTriFactorT->solveOp = CUSPARSE_OPERATION_NON_TRANSPOSE;
797aa372e3fSPaul Mullowney 
798aa372e3fSPaul Mullowney   /* allocate GPU space for the CSC of the upper triangular factor*/
799aa372e3fSPaul Mullowney   upTriFactorT->csrMat = new CsrMatrix;
800aa372e3fSPaul Mullowney   upTriFactorT->csrMat->num_rows = upTriFactor->csrMat->num_rows;
801aa372e3fSPaul Mullowney   upTriFactorT->csrMat->num_cols = upTriFactor->csrMat->num_cols;
802aa372e3fSPaul Mullowney   upTriFactorT->csrMat->num_entries = upTriFactor->csrMat->num_entries;
803aa372e3fSPaul Mullowney   upTriFactorT->csrMat->row_offsets = new THRUSTINTARRAY32(upTriFactor->csrMat->num_rows+1);
804aa372e3fSPaul Mullowney   upTriFactorT->csrMat->column_indices = new THRUSTINTARRAY32(upTriFactor->csrMat->num_entries);
805aa372e3fSPaul Mullowney   upTriFactorT->csrMat->values = new THRUSTARRAY(upTriFactor->csrMat->num_entries);
806aa372e3fSPaul Mullowney 
807aa372e3fSPaul Mullowney   /* compute the transpose of the upper triangular factor, i.e. the CSC */
808aa372e3fSPaul Mullowney   stat = cusparse_csr2csc(cusparseTriFactors->handle, upTriFactor->csrMat->num_rows,
809aa372e3fSPaul Mullowney                           upTriFactor->csrMat->num_cols, upTriFactor->csrMat->num_entries,
810aa372e3fSPaul Mullowney                           upTriFactor->csrMat->values->data().get(),
811aa372e3fSPaul Mullowney                           upTriFactor->csrMat->row_offsets->data().get(),
812aa372e3fSPaul Mullowney                           upTriFactor->csrMat->column_indices->data().get(),
813aa372e3fSPaul Mullowney                           upTriFactorT->csrMat->values->data().get(),
814aa372e3fSPaul Mullowney                           upTriFactorT->csrMat->column_indices->data().get(),
815aa372e3fSPaul Mullowney                           upTriFactorT->csrMat->row_offsets->data().get(),
816c41cb2e2SAlejandro Lamas Daviña                           CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat);
817aa372e3fSPaul Mullowney 
818aa372e3fSPaul Mullowney   /* perform the solve analysis on the transposed matrix */
819aa372e3fSPaul Mullowney   stat = cusparse_analysis(cusparseTriFactors->handle, upTriFactorT->solveOp,
820aa372e3fSPaul Mullowney                            upTriFactorT->csrMat->num_rows, upTriFactorT->csrMat->num_entries,
821aa372e3fSPaul Mullowney                            upTriFactorT->descr, upTriFactorT->csrMat->values->data().get(),
822aa372e3fSPaul Mullowney                            upTriFactorT->csrMat->row_offsets->data().get(), upTriFactorT->csrMat->column_indices->data().get(),
823c41cb2e2SAlejandro Lamas Daviña                            upTriFactorT->solveInfo);CHKERRCUDA(stat);
824aa372e3fSPaul Mullowney 
825aa372e3fSPaul Mullowney   /* assign the pointer. Is this really necessary? */
826aa372e3fSPaul Mullowney   ((Mat_SeqAIJCUSPARSETriFactors*)A->spptr)->upTriFactorPtrTranspose = upTriFactorT;
827bda325fcSPaul Mullowney   PetscFunctionReturn(0);
828bda325fcSPaul Mullowney }
829bda325fcSPaul Mullowney 
830b175d8bbSPaul Mullowney static PetscErrorCode MatSeqAIJCUSPARSEGenerateTransposeForMult(Mat A)
831bda325fcSPaul Mullowney {
832aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE           *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
833aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat;
834aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
835bda325fcSPaul Mullowney   Mat_SeqAIJ                   *a = (Mat_SeqAIJ*)A->data;
836bda325fcSPaul Mullowney   cusparseStatus_t             stat;
837aa372e3fSPaul Mullowney   cusparseIndexBase_t          indexBase;
838b06137fdSPaul Mullowney   cudaError_t                  err;
839b175d8bbSPaul Mullowney 
840bda325fcSPaul Mullowney   PetscFunctionBegin;
841aa372e3fSPaul Mullowney 
842aa372e3fSPaul Mullowney   /* allocate space for the triangular factor information */
843aa372e3fSPaul Mullowney   matstructT = new Mat_SeqAIJCUSPARSEMultStruct;
844c41cb2e2SAlejandro Lamas Daviña   stat = cusparseCreateMatDescr(&matstructT->descr);CHKERRCUDA(stat);
845aa372e3fSPaul Mullowney   indexBase = cusparseGetMatIndexBase(matstruct->descr);
846c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatIndexBase(matstructT->descr, indexBase);CHKERRCUDA(stat);
847c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetMatType(matstructT->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUDA(stat);
848aa372e3fSPaul Mullowney 
849b06137fdSPaul Mullowney   /* set alpha and beta */
850c41cb2e2SAlejandro Lamas Daviña   err = cudaMalloc((void **)&(matstructT->alpha),sizeof(PetscScalar));CHKERRCUDA(err);
851c41cb2e2SAlejandro Lamas Daviña   err = cudaMemcpy(matstructT->alpha,&ALPHA,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
852c41cb2e2SAlejandro Lamas Daviña   err = cudaMalloc((void **)&(matstructT->beta),sizeof(PetscScalar));CHKERRCUDA(err);
853c41cb2e2SAlejandro Lamas Daviña   err = cudaMemcpy(matstructT->beta,&BETA,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
854c41cb2e2SAlejandro Lamas Daviña   stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat);
855b06137fdSPaul Mullowney 
856aa372e3fSPaul Mullowney   if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
857aa372e3fSPaul Mullowney     CsrMatrix *matrix = (CsrMatrix*)matstruct->mat;
858aa372e3fSPaul Mullowney     CsrMatrix *matrixT= new CsrMatrix;
859aa372e3fSPaul Mullowney     matrixT->num_rows = A->rmap->n;
860aa372e3fSPaul Mullowney     matrixT->num_cols = A->cmap->n;
861aa372e3fSPaul Mullowney     matrixT->num_entries = a->nz;
862aa372e3fSPaul Mullowney     matrixT->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
863aa372e3fSPaul Mullowney     matrixT->column_indices = new THRUSTINTARRAY32(a->nz);
864aa372e3fSPaul Mullowney     matrixT->values = new THRUSTARRAY(a->nz);
865aa372e3fSPaul Mullowney 
866aa372e3fSPaul Mullowney     /* compute the transpose of the upper triangular factor, i.e. the CSC */
867aa372e3fSPaul Mullowney     indexBase = cusparseGetMatIndexBase(matstruct->descr);
868aa372e3fSPaul Mullowney     stat = cusparse_csr2csc(cusparsestruct->handle, matrix->num_rows,
869aa372e3fSPaul Mullowney                             matrix->num_cols, matrix->num_entries,
870aa372e3fSPaul Mullowney                             matrix->values->data().get(),
871aa372e3fSPaul Mullowney                             matrix->row_offsets->data().get(),
872aa372e3fSPaul Mullowney                             matrix->column_indices->data().get(),
873aa372e3fSPaul Mullowney                             matrixT->values->data().get(),
874aa372e3fSPaul Mullowney                             matrixT->column_indices->data().get(),
875aa372e3fSPaul Mullowney                             matrixT->row_offsets->data().get(),
876c41cb2e2SAlejandro Lamas Daviña                             CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat);
877aa372e3fSPaul Mullowney 
878aa372e3fSPaul Mullowney     /* assign the pointer */
879aa372e3fSPaul Mullowney     matstructT->mat = matrixT;
880aa372e3fSPaul Mullowney 
881aa372e3fSPaul Mullowney   } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) {
8822692e278SPaul Mullowney #if CUDA_VERSION>=5000
883aa372e3fSPaul Mullowney     /* First convert HYB to CSR */
884aa372e3fSPaul Mullowney     CsrMatrix *temp= new CsrMatrix;
885aa372e3fSPaul Mullowney     temp->num_rows = A->rmap->n;
886aa372e3fSPaul Mullowney     temp->num_cols = A->cmap->n;
887aa372e3fSPaul Mullowney     temp->num_entries = a->nz;
888aa372e3fSPaul Mullowney     temp->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
889aa372e3fSPaul Mullowney     temp->column_indices = new THRUSTINTARRAY32(a->nz);
890aa372e3fSPaul Mullowney     temp->values = new THRUSTARRAY(a->nz);
891aa372e3fSPaul Mullowney 
8922692e278SPaul Mullowney 
893aa372e3fSPaul Mullowney     stat = cusparse_hyb2csr(cusparsestruct->handle,
894aa372e3fSPaul Mullowney                             matstruct->descr, (cusparseHybMat_t)matstruct->mat,
895aa372e3fSPaul Mullowney                             temp->values->data().get(),
896aa372e3fSPaul Mullowney                             temp->row_offsets->data().get(),
897c41cb2e2SAlejandro Lamas Daviña                             temp->column_indices->data().get());CHKERRCUDA(stat);
898aa372e3fSPaul Mullowney 
899aa372e3fSPaul Mullowney     /* Next, convert CSR to CSC (i.e. the matrix transpose) */
900aa372e3fSPaul Mullowney     CsrMatrix *tempT= new CsrMatrix;
901aa372e3fSPaul Mullowney     tempT->num_rows = A->rmap->n;
902aa372e3fSPaul Mullowney     tempT->num_cols = A->cmap->n;
903aa372e3fSPaul Mullowney     tempT->num_entries = a->nz;
904aa372e3fSPaul Mullowney     tempT->row_offsets = new THRUSTINTARRAY32(A->rmap->n+1);
905aa372e3fSPaul Mullowney     tempT->column_indices = new THRUSTINTARRAY32(a->nz);
906aa372e3fSPaul Mullowney     tempT->values = new THRUSTARRAY(a->nz);
907aa372e3fSPaul Mullowney 
908aa372e3fSPaul Mullowney     stat = cusparse_csr2csc(cusparsestruct->handle, temp->num_rows,
909aa372e3fSPaul Mullowney                             temp->num_cols, temp->num_entries,
910aa372e3fSPaul Mullowney                             temp->values->data().get(),
911aa372e3fSPaul Mullowney                             temp->row_offsets->data().get(),
912aa372e3fSPaul Mullowney                             temp->column_indices->data().get(),
913aa372e3fSPaul Mullowney                             tempT->values->data().get(),
914aa372e3fSPaul Mullowney                             tempT->column_indices->data().get(),
915aa372e3fSPaul Mullowney                             tempT->row_offsets->data().get(),
916c41cb2e2SAlejandro Lamas Daviña                             CUSPARSE_ACTION_NUMERIC, indexBase);CHKERRCUDA(stat);
917aa372e3fSPaul Mullowney 
918aa372e3fSPaul Mullowney     /* Last, convert CSC to HYB */
919aa372e3fSPaul Mullowney     cusparseHybMat_t hybMat;
920c41cb2e2SAlejandro Lamas Daviña     stat = cusparseCreateHybMat(&hybMat);CHKERRCUDA(stat);
921aa372e3fSPaul Mullowney     cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ?
922aa372e3fSPaul Mullowney       CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO;
923aa372e3fSPaul Mullowney     stat = cusparse_csr2hyb(cusparsestruct->handle, A->rmap->n, A->cmap->n,
924aa372e3fSPaul Mullowney                             matstructT->descr, tempT->values->data().get(),
925aa372e3fSPaul Mullowney                             tempT->row_offsets->data().get(),
926aa372e3fSPaul Mullowney                             tempT->column_indices->data().get(),
927c41cb2e2SAlejandro Lamas Daviña                             hybMat, 0, partition);CHKERRCUDA(stat);
928aa372e3fSPaul Mullowney 
929aa372e3fSPaul Mullowney     /* assign the pointer */
930aa372e3fSPaul Mullowney     matstructT->mat = hybMat;
931aa372e3fSPaul Mullowney 
932aa372e3fSPaul Mullowney     /* delete temporaries */
933aa372e3fSPaul Mullowney     if (tempT) {
934aa372e3fSPaul Mullowney       if (tempT->values) delete (THRUSTARRAY*) tempT->values;
935aa372e3fSPaul Mullowney       if (tempT->column_indices) delete (THRUSTINTARRAY32*) tempT->column_indices;
936aa372e3fSPaul Mullowney       if (tempT->row_offsets) delete (THRUSTINTARRAY32*) tempT->row_offsets;
937aa372e3fSPaul Mullowney       delete (CsrMatrix*) tempT;
938087f3262SPaul Mullowney     }
939aa372e3fSPaul Mullowney     if (temp) {
940aa372e3fSPaul Mullowney       if (temp->values) delete (THRUSTARRAY*) temp->values;
941aa372e3fSPaul Mullowney       if (temp->column_indices) delete (THRUSTINTARRAY32*) temp->column_indices;
942aa372e3fSPaul Mullowney       if (temp->row_offsets) delete (THRUSTINTARRAY32*) temp->row_offsets;
943aa372e3fSPaul Mullowney       delete (CsrMatrix*) temp;
944aa372e3fSPaul Mullowney     }
9452692e278SPaul Mullowney #else
9462692e278SPaul 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.");
9472692e278SPaul Mullowney #endif
948aa372e3fSPaul Mullowney   }
949aa372e3fSPaul Mullowney   /* assign the compressed row indices */
950aa372e3fSPaul Mullowney   matstructT->cprowIndices = new THRUSTINTARRAY;
951aa372e3fSPaul Mullowney 
952aa372e3fSPaul Mullowney   /* assign the pointer */
953aa372e3fSPaul Mullowney   ((Mat_SeqAIJCUSPARSE*)A->spptr)->matTranspose = matstructT;
954bda325fcSPaul Mullowney   PetscFunctionReturn(0);
955bda325fcSPaul Mullowney }
956bda325fcSPaul Mullowney 
9576fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx)
958bda325fcSPaul Mullowney {
959c41cb2e2SAlejandro Lamas Daviña   PetscInt                              n = xx->map->n;
960465f34aeSAlejandro Lamas Daviña   const PetscScalar                     *barray;
961465f34aeSAlejandro Lamas Daviña   PetscScalar                           *xarray;
962465f34aeSAlejandro Lamas Daviña   thrust::device_ptr<const PetscScalar> bGPU;
963465f34aeSAlejandro Lamas Daviña   thrust::device_ptr<PetscScalar>       xGPU;
964bda325fcSPaul Mullowney   cusparseStatus_t                      stat;
965bda325fcSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors          *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
966aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct     *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
967aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct     *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
968aa372e3fSPaul Mullowney   THRUSTARRAY                           *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector;
969b175d8bbSPaul Mullowney   PetscErrorCode                        ierr;
970bda325fcSPaul Mullowney 
971bda325fcSPaul Mullowney   PetscFunctionBegin;
972aa372e3fSPaul Mullowney   /* Analyze the matrix and create the transpose ... on the fly */
973aa372e3fSPaul Mullowney   if (!loTriFactorT && !upTriFactorT) {
974bda325fcSPaul Mullowney     ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr);
975aa372e3fSPaul Mullowney     loTriFactorT       = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
976aa372e3fSPaul Mullowney     upTriFactorT       = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
977bda325fcSPaul Mullowney   }
978bda325fcSPaul Mullowney 
979bda325fcSPaul Mullowney   /* Get the GPU pointers */
980c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr);
981c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr);
982c41cb2e2SAlejandro Lamas Daviña   xGPU = thrust::device_pointer_cast(xarray);
983c41cb2e2SAlejandro Lamas Daviña   bGPU = thrust::device_pointer_cast(barray);
984bda325fcSPaul Mullowney 
985aa372e3fSPaul Mullowney   /* First, reorder with the row permutation */
986c41cb2e2SAlejandro Lamas Daviña   thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()),
987c41cb2e2SAlejandro Lamas Daviña                thrust::make_permutation_iterator(bGPU+n, cusparseTriFactors->rpermIndices->end()),
988c41cb2e2SAlejandro Lamas Daviña                xGPU);
989aa372e3fSPaul Mullowney 
990aa372e3fSPaul Mullowney   /* First, solve U */
991aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp,
992aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->num_rows, &ALPHA, upTriFactorT->descr,
993aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->values->data().get(),
994aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->row_offsets->data().get(),
995aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->column_indices->data().get(),
996aa372e3fSPaul Mullowney                         upTriFactorT->solveInfo,
997c41cb2e2SAlejandro Lamas Daviña                         xarray, tempGPU->data().get());CHKERRCUDA(stat);
998aa372e3fSPaul Mullowney 
999aa372e3fSPaul Mullowney   /* Then, solve L */
1000aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp,
1001aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->num_rows, &ALPHA, loTriFactorT->descr,
1002aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->values->data().get(),
1003aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->row_offsets->data().get(),
1004aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->column_indices->data().get(),
1005aa372e3fSPaul Mullowney                         loTriFactorT->solveInfo,
1006c41cb2e2SAlejandro Lamas Daviña                         tempGPU->data().get(), xarray);CHKERRCUDA(stat);
1007aa372e3fSPaul Mullowney 
1008aa372e3fSPaul Mullowney   /* Last, copy the solution, xGPU, into a temporary with the column permutation ... can't be done in place. */
1009c41cb2e2SAlejandro Lamas Daviña   thrust::copy(thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->begin()),
1010c41cb2e2SAlejandro Lamas Daviña                thrust::make_permutation_iterator(xGPU+n, cusparseTriFactors->cpermIndices->end()),
1011aa372e3fSPaul Mullowney                tempGPU->begin());
1012aa372e3fSPaul Mullowney 
1013aa372e3fSPaul Mullowney   /* Copy the temporary to the full solution. */
1014c41cb2e2SAlejandro Lamas Daviña   thrust::copy(tempGPU->begin(), tempGPU->end(), xGPU);
1015bda325fcSPaul Mullowney 
1016bda325fcSPaul Mullowney   /* restore */
1017c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr);
1018c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr);
1019c41cb2e2SAlejandro Lamas Daviña   ierr = WaitForGPU();CHKERRCUDA(ierr);
1020087f3262SPaul Mullowney 
1021aa372e3fSPaul Mullowney   ierr = PetscLogFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr);
1022bda325fcSPaul Mullowney   PetscFunctionReturn(0);
1023bda325fcSPaul Mullowney }
1024bda325fcSPaul Mullowney 
10256fa9248bSJed Brown static PetscErrorCode MatSolveTranspose_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx)
1026bda325fcSPaul Mullowney {
1027465f34aeSAlejandro Lamas Daviña   const PetscScalar                 *barray;
1028465f34aeSAlejandro Lamas Daviña   PetscScalar                       *xarray;
1029bda325fcSPaul Mullowney   cusparseStatus_t                  stat;
1030bda325fcSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
1031aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
1032aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorT = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
1033aa372e3fSPaul Mullowney   THRUSTARRAY                       *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector;
1034b175d8bbSPaul Mullowney   PetscErrorCode                    ierr;
1035bda325fcSPaul Mullowney 
1036bda325fcSPaul Mullowney   PetscFunctionBegin;
1037aa372e3fSPaul Mullowney   /* Analyze the matrix and create the transpose ... on the fly */
1038aa372e3fSPaul Mullowney   if (!loTriFactorT && !upTriFactorT) {
1039bda325fcSPaul Mullowney     ierr = MatSeqAIJCUSPARSEAnalyzeTransposeForSolve(A);CHKERRQ(ierr);
1040aa372e3fSPaul Mullowney     loTriFactorT       = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtrTranspose;
1041aa372e3fSPaul Mullowney     upTriFactorT       = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtrTranspose;
1042bda325fcSPaul Mullowney   }
1043bda325fcSPaul Mullowney 
1044bda325fcSPaul Mullowney   /* Get the GPU pointers */
1045c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr);
1046c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr);
1047bda325fcSPaul Mullowney 
1048aa372e3fSPaul Mullowney   /* First, solve U */
1049aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, upTriFactorT->solveOp,
1050aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->num_rows, &ALPHA, upTriFactorT->descr,
1051aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->values->data().get(),
1052aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->row_offsets->data().get(),
1053aa372e3fSPaul Mullowney                         upTriFactorT->csrMat->column_indices->data().get(),
1054aa372e3fSPaul Mullowney                         upTriFactorT->solveInfo,
1055c41cb2e2SAlejandro Lamas Daviña                         barray, tempGPU->data().get());CHKERRCUDA(stat);
1056aa372e3fSPaul Mullowney 
1057aa372e3fSPaul Mullowney   /* Then, solve L */
1058aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, loTriFactorT->solveOp,
1059aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->num_rows, &ALPHA, loTriFactorT->descr,
1060aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->values->data().get(),
1061aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->row_offsets->data().get(),
1062aa372e3fSPaul Mullowney                         loTriFactorT->csrMat->column_indices->data().get(),
1063aa372e3fSPaul Mullowney                         loTriFactorT->solveInfo,
1064c41cb2e2SAlejandro Lamas Daviña                         tempGPU->data().get(), xarray);CHKERRCUDA(stat);
1065bda325fcSPaul Mullowney 
1066bda325fcSPaul Mullowney   /* restore */
1067c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr);
1068c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr);
1069c41cb2e2SAlejandro Lamas Daviña   ierr = WaitForGPU();CHKERRCUDA(ierr);
1070aa372e3fSPaul Mullowney   ierr = PetscLogFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr);
1071bda325fcSPaul Mullowney   PetscFunctionReturn(0);
1072bda325fcSPaul Mullowney }
1073bda325fcSPaul Mullowney 
10746fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE(Mat A,Vec bb,Vec xx)
10759ae82921SPaul Mullowney {
1076465f34aeSAlejandro Lamas Daviña   const PetscScalar                     *barray;
1077465f34aeSAlejandro Lamas Daviña   PetscScalar                           *xarray;
1078465f34aeSAlejandro Lamas Daviña   thrust::device_ptr<const PetscScalar> bGPU;
1079465f34aeSAlejandro Lamas Daviña   thrust::device_ptr<PetscScalar>       xGPU;
10809ae82921SPaul Mullowney   cusparseStatus_t                      stat;
10819ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors          *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
1082aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct     *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
1083aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct     *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
1084aa372e3fSPaul Mullowney   THRUSTARRAY                           *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector;
1085b175d8bbSPaul Mullowney   PetscErrorCode                        ierr;
1086ebc8f436SDominic Meiser   VecType                               t;
1087ebc8f436SDominic Meiser   PetscBool                             flg;
10889ae82921SPaul Mullowney 
10899ae82921SPaul Mullowney   PetscFunctionBegin;
1090ebc8f436SDominic Meiser   ierr = VecGetType(bb,&t);CHKERRQ(ierr);
1091c41cb2e2SAlejandro Lamas Daviña   ierr = PetscStrcmp(t,VECSEQCUDA,&flg);CHKERRQ(ierr);
1092c41cb2e2SAlejandro Lamas Daviña   if (!flg) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Vector of type %s passed into MatSolve_SeqAIJCUSPARSE (Arg #2). Can only deal with %s\n.",t,VECSEQCUDA);
1093ebc8f436SDominic Meiser   ierr = VecGetType(xx,&t);CHKERRQ(ierr);
1094c41cb2e2SAlejandro Lamas Daviña   ierr = PetscStrcmp(t,VECSEQCUDA,&flg);CHKERRQ(ierr);
1095c41cb2e2SAlejandro Lamas Daviña   if (!flg) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Vector of type %s passed into MatSolve_SeqAIJCUSPARSE (Arg #3). Can only deal with %s\n.",t,VECSEQCUDA);
1096ebc8f436SDominic Meiser 
1097e057df02SPaul Mullowney   /* Get the GPU pointers */
1098c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr);
1099c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr);
1100c41cb2e2SAlejandro Lamas Daviña   xGPU = thrust::device_pointer_cast(xarray);
1101c41cb2e2SAlejandro Lamas Daviña   bGPU = thrust::device_pointer_cast(barray);
11029ae82921SPaul Mullowney 
1103aa372e3fSPaul Mullowney   /* First, reorder with the row permutation */
1104c41cb2e2SAlejandro Lamas Daviña   thrust::copy(thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->begin()),
1105c41cb2e2SAlejandro Lamas Daviña                thrust::make_permutation_iterator(bGPU, cusparseTriFactors->rpermIndices->end()),
1106c41cb2e2SAlejandro Lamas Daviña                xGPU);
1107aa372e3fSPaul Mullowney 
1108aa372e3fSPaul Mullowney   /* Next, solve L */
1109aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp,
1110aa372e3fSPaul Mullowney                         loTriFactor->csrMat->num_rows, &ALPHA, loTriFactor->descr,
1111aa372e3fSPaul Mullowney                         loTriFactor->csrMat->values->data().get(),
1112aa372e3fSPaul Mullowney                         loTriFactor->csrMat->row_offsets->data().get(),
1113aa372e3fSPaul Mullowney                         loTriFactor->csrMat->column_indices->data().get(),
1114aa372e3fSPaul Mullowney                         loTriFactor->solveInfo,
1115c41cb2e2SAlejandro Lamas Daviña                         xarray, tempGPU->data().get());CHKERRCUDA(stat);
1116aa372e3fSPaul Mullowney 
1117aa372e3fSPaul Mullowney   /* Then, solve U */
1118aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp,
1119aa372e3fSPaul Mullowney                         upTriFactor->csrMat->num_rows, &ALPHA, upTriFactor->descr,
1120aa372e3fSPaul Mullowney                         upTriFactor->csrMat->values->data().get(),
1121aa372e3fSPaul Mullowney                         upTriFactor->csrMat->row_offsets->data().get(),
1122aa372e3fSPaul Mullowney                         upTriFactor->csrMat->column_indices->data().get(),
1123aa372e3fSPaul Mullowney                         upTriFactor->solveInfo,
1124c41cb2e2SAlejandro Lamas Daviña                         tempGPU->data().get(), xarray);CHKERRCUDA(stat);
1125aa372e3fSPaul Mullowney 
1126aa372e3fSPaul Mullowney   /* Last, copy the solution, xGPU, into a temporary with the column permutation ... can't be done in place. */
1127c41cb2e2SAlejandro Lamas Daviña   thrust::copy(thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->begin()),
1128c41cb2e2SAlejandro Lamas Daviña                thrust::make_permutation_iterator(xGPU, cusparseTriFactors->cpermIndices->end()),
1129aa372e3fSPaul Mullowney                tempGPU->begin());
1130aa372e3fSPaul Mullowney 
1131aa372e3fSPaul Mullowney   /* Copy the temporary to the full solution. */
1132c41cb2e2SAlejandro Lamas Daviña   thrust::copy(tempGPU->begin(), tempGPU->end(), xGPU);
11339ae82921SPaul Mullowney 
1134c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr);
1135c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr);
1136c41cb2e2SAlejandro Lamas Daviña   ierr = WaitForGPU();CHKERRCUDA(ierr);
1137aa372e3fSPaul Mullowney   ierr = PetscLogFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr);
11389ae82921SPaul Mullowney   PetscFunctionReturn(0);
11399ae82921SPaul Mullowney }
11409ae82921SPaul Mullowney 
11416fa9248bSJed Brown static PetscErrorCode MatSolve_SeqAIJCUSPARSE_NaturalOrdering(Mat A,Vec bb,Vec xx)
11429ae82921SPaul Mullowney {
1143465f34aeSAlejandro Lamas Daviña   const PetscScalar                 *barray;
1144465f34aeSAlejandro Lamas Daviña   PetscScalar                       *xarray;
11459ae82921SPaul Mullowney   cusparseStatus_t                  stat;
11469ae82921SPaul Mullowney   Mat_SeqAIJCUSPARSETriFactors      *cusparseTriFactors = (Mat_SeqAIJCUSPARSETriFactors*)A->spptr;
1147aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->loTriFactorPtr;
1148aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactor = (Mat_SeqAIJCUSPARSETriFactorStruct*)cusparseTriFactors->upTriFactorPtr;
1149aa372e3fSPaul Mullowney   THRUSTARRAY                       *tempGPU = (THRUSTARRAY*)cusparseTriFactors->workVector;
1150b175d8bbSPaul Mullowney   PetscErrorCode                    ierr;
11519ae82921SPaul Mullowney 
11529ae82921SPaul Mullowney   PetscFunctionBegin;
1153e057df02SPaul Mullowney   /* Get the GPU pointers */
1154c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(xx,&xarray);CHKERRQ(ierr);
1155c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(bb,&barray);CHKERRQ(ierr);
11569ae82921SPaul Mullowney 
1157aa372e3fSPaul Mullowney   /* First, solve L */
1158aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, loTriFactor->solveOp,
1159aa372e3fSPaul Mullowney                         loTriFactor->csrMat->num_rows, &ALPHA, loTriFactor->descr,
1160aa372e3fSPaul Mullowney                         loTriFactor->csrMat->values->data().get(),
1161aa372e3fSPaul Mullowney                         loTriFactor->csrMat->row_offsets->data().get(),
1162aa372e3fSPaul Mullowney                         loTriFactor->csrMat->column_indices->data().get(),
1163aa372e3fSPaul Mullowney                         loTriFactor->solveInfo,
1164c41cb2e2SAlejandro Lamas Daviña                         barray, tempGPU->data().get());CHKERRCUDA(stat);
1165aa372e3fSPaul Mullowney 
1166aa372e3fSPaul Mullowney   /* Next, solve U */
1167aa372e3fSPaul Mullowney   stat = cusparse_solve(cusparseTriFactors->handle, upTriFactor->solveOp,
1168aa372e3fSPaul Mullowney                         upTriFactor->csrMat->num_rows, &ALPHA, upTriFactor->descr,
1169aa372e3fSPaul Mullowney                         upTriFactor->csrMat->values->data().get(),
1170aa372e3fSPaul Mullowney                         upTriFactor->csrMat->row_offsets->data().get(),
1171aa372e3fSPaul Mullowney                         upTriFactor->csrMat->column_indices->data().get(),
1172aa372e3fSPaul Mullowney                         upTriFactor->solveInfo,
1173c41cb2e2SAlejandro Lamas Daviña                         tempGPU->data().get(), xarray);CHKERRCUDA(stat);
11749ae82921SPaul Mullowney 
1175c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(bb,&barray);CHKERRQ(ierr);
1176c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(xx,&xarray);CHKERRQ(ierr);
1177c41cb2e2SAlejandro Lamas Daviña   ierr = WaitForGPU();CHKERRCUDA(ierr);
1178aa372e3fSPaul Mullowney   ierr = PetscLogFlops(2.0*cusparseTriFactors->nnz - A->cmap->n);CHKERRQ(ierr);
11799ae82921SPaul Mullowney   PetscFunctionReturn(0);
11809ae82921SPaul Mullowney }
11819ae82921SPaul Mullowney 
11826fa9248bSJed Brown static PetscErrorCode MatSeqAIJCUSPARSECopyToGPU(Mat A)
11839ae82921SPaul Mullowney {
11849ae82921SPaul Mullowney 
1185aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE           *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
1186aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat;
11879ae82921SPaul Mullowney   Mat_SeqAIJ                   *a = (Mat_SeqAIJ*)A->data;
11889ae82921SPaul Mullowney   PetscInt                     m = A->rmap->n,*ii,*ridx;
11899ae82921SPaul Mullowney   PetscErrorCode               ierr;
1190aa372e3fSPaul Mullowney   cusparseStatus_t             stat;
1191b06137fdSPaul Mullowney   cudaError_t                  err;
11929ae82921SPaul Mullowney 
11939ae82921SPaul Mullowney   PetscFunctionBegin;
1194c41cb2e2SAlejandro Lamas Daviña   if (A->valid_GPU_matrix == PETSC_CUDA_UNALLOCATED || A->valid_GPU_matrix == PETSC_CUDA_CPU) {
11959ae82921SPaul Mullowney     ierr = PetscLogEventBegin(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr);
119634d6c7a5SJose E. Roman     if (A->assembled && A->nonzerostate == cusparsestruct->nonzerostate && cusparsestruct->format == MAT_CUSPARSE_CSR) {
119734d6c7a5SJose E. Roman       CsrMatrix *matrix = (CsrMatrix*)matstruct->mat;
119834d6c7a5SJose E. Roman       /* copy values only */
119934d6c7a5SJose E. Roman       matrix->values->assign(a->a, a->a+a->nz);
120034d6c7a5SJose E. Roman     } else {
1201ce814652SDominic Meiser       Mat_SeqAIJCUSPARSEMultStruct_Destroy(&matstruct,cusparsestruct->format);
12029ae82921SPaul Mullowney       try {
1203aa372e3fSPaul Mullowney         cusparsestruct->nonzerorow=0;
1204aa372e3fSPaul Mullowney         for (int j = 0; j<m; j++) cusparsestruct->nonzerorow += ((a->i[j+1]-a->i[j])>0);
12059ae82921SPaul Mullowney 
12069ae82921SPaul Mullowney         if (a->compressedrow.use) {
12079ae82921SPaul Mullowney           m    = a->compressedrow.nrows;
12089ae82921SPaul Mullowney           ii   = a->compressedrow.i;
12099ae82921SPaul Mullowney           ridx = a->compressedrow.rindex;
12109ae82921SPaul Mullowney         } else {
1211b06137fdSPaul Mullowney           /* Forcing compressed row on the GPU */
12129ae82921SPaul Mullowney           int k=0;
1213854ce69bSBarry Smith           ierr = PetscMalloc1(cusparsestruct->nonzerorow+1, &ii);CHKERRQ(ierr);
1214854ce69bSBarry Smith           ierr = PetscMalloc1(cusparsestruct->nonzerorow, &ridx);CHKERRQ(ierr);
12159ae82921SPaul Mullowney           ii[0]=0;
12169ae82921SPaul Mullowney           for (int j = 0; j<m; j++) {
12179ae82921SPaul Mullowney             if ((a->i[j+1]-a->i[j])>0) {
12189ae82921SPaul Mullowney               ii[k]  = a->i[j];
12199ae82921SPaul Mullowney               ridx[k]= j;
12209ae82921SPaul Mullowney               k++;
12219ae82921SPaul Mullowney             }
12229ae82921SPaul Mullowney           }
1223aa372e3fSPaul Mullowney           ii[cusparsestruct->nonzerorow] = a->nz;
1224aa372e3fSPaul Mullowney           m = cusparsestruct->nonzerorow;
12259ae82921SPaul Mullowney         }
12269ae82921SPaul Mullowney 
1227aa372e3fSPaul Mullowney         /* allocate space for the triangular factor information */
1228aa372e3fSPaul Mullowney         matstruct = new Mat_SeqAIJCUSPARSEMultStruct;
1229c41cb2e2SAlejandro Lamas Daviña         stat = cusparseCreateMatDescr(&matstruct->descr);CHKERRCUDA(stat);
1230c41cb2e2SAlejandro Lamas Daviña         stat = cusparseSetMatIndexBase(matstruct->descr, CUSPARSE_INDEX_BASE_ZERO);CHKERRCUDA(stat);
1231c41cb2e2SAlejandro Lamas Daviña         stat = cusparseSetMatType(matstruct->descr, CUSPARSE_MATRIX_TYPE_GENERAL);CHKERRCUDA(stat);
12329ae82921SPaul Mullowney 
1233c41cb2e2SAlejandro Lamas Daviña         err = cudaMalloc((void **)&(matstruct->alpha),sizeof(PetscScalar));CHKERRCUDA(err);
1234c41cb2e2SAlejandro Lamas Daviña         err = cudaMemcpy(matstruct->alpha,&ALPHA,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
1235c41cb2e2SAlejandro Lamas Daviña         err = cudaMalloc((void **)&(matstruct->beta),sizeof(PetscScalar));CHKERRCUDA(err);
1236c41cb2e2SAlejandro Lamas Daviña         err = cudaMemcpy(matstruct->beta,&BETA,sizeof(PetscScalar),cudaMemcpyHostToDevice);CHKERRCUDA(err);
1237c41cb2e2SAlejandro Lamas Daviña         stat = cusparseSetPointerMode(cusparsestruct->handle, CUSPARSE_POINTER_MODE_DEVICE);CHKERRCUDA(stat);
1238b06137fdSPaul Mullowney 
1239aa372e3fSPaul Mullowney         /* Build a hybrid/ellpack matrix if this option is chosen for the storage */
1240aa372e3fSPaul Mullowney         if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
1241aa372e3fSPaul Mullowney           /* set the matrix */
1242aa372e3fSPaul Mullowney           CsrMatrix *matrix= new CsrMatrix;
1243a65300a6SPaul Mullowney           matrix->num_rows = m;
1244aa372e3fSPaul Mullowney           matrix->num_cols = A->cmap->n;
1245aa372e3fSPaul Mullowney           matrix->num_entries = a->nz;
1246a65300a6SPaul Mullowney           matrix->row_offsets = new THRUSTINTARRAY32(m+1);
1247a65300a6SPaul Mullowney           matrix->row_offsets->assign(ii, ii + m+1);
12489ae82921SPaul Mullowney 
1249aa372e3fSPaul Mullowney           matrix->column_indices = new THRUSTINTARRAY32(a->nz);
1250aa372e3fSPaul Mullowney           matrix->column_indices->assign(a->j, a->j+a->nz);
1251aa372e3fSPaul Mullowney 
1252aa372e3fSPaul Mullowney           matrix->values = new THRUSTARRAY(a->nz);
1253aa372e3fSPaul Mullowney           matrix->values->assign(a->a, a->a+a->nz);
1254aa372e3fSPaul Mullowney 
1255aa372e3fSPaul Mullowney           /* assign the pointer */
1256aa372e3fSPaul Mullowney           matstruct->mat = matrix;
1257aa372e3fSPaul Mullowney 
1258aa372e3fSPaul Mullowney         } else if (cusparsestruct->format==MAT_CUSPARSE_ELL || cusparsestruct->format==MAT_CUSPARSE_HYB) {
12592692e278SPaul Mullowney #if CUDA_VERSION>=4020
1260aa372e3fSPaul Mullowney           CsrMatrix *matrix= new CsrMatrix;
1261a65300a6SPaul Mullowney           matrix->num_rows = m;
1262aa372e3fSPaul Mullowney           matrix->num_cols = A->cmap->n;
1263aa372e3fSPaul Mullowney           matrix->num_entries = a->nz;
1264a65300a6SPaul Mullowney           matrix->row_offsets = new THRUSTINTARRAY32(m+1);
1265a65300a6SPaul Mullowney           matrix->row_offsets->assign(ii, ii + m+1);
1266aa372e3fSPaul Mullowney 
1267aa372e3fSPaul Mullowney           matrix->column_indices = new THRUSTINTARRAY32(a->nz);
1268aa372e3fSPaul Mullowney           matrix->column_indices->assign(a->j, a->j+a->nz);
1269aa372e3fSPaul Mullowney 
1270aa372e3fSPaul Mullowney           matrix->values = new THRUSTARRAY(a->nz);
1271aa372e3fSPaul Mullowney           matrix->values->assign(a->a, a->a+a->nz);
1272aa372e3fSPaul Mullowney 
1273aa372e3fSPaul Mullowney           cusparseHybMat_t hybMat;
1274c41cb2e2SAlejandro Lamas Daviña           stat = cusparseCreateHybMat(&hybMat);CHKERRCUDA(stat);
1275aa372e3fSPaul Mullowney           cusparseHybPartition_t partition = cusparsestruct->format==MAT_CUSPARSE_ELL ?
1276aa372e3fSPaul Mullowney             CUSPARSE_HYB_PARTITION_MAX : CUSPARSE_HYB_PARTITION_AUTO;
1277a65300a6SPaul Mullowney           stat = cusparse_csr2hyb(cusparsestruct->handle, matrix->num_rows, matrix->num_cols,
1278aa372e3fSPaul Mullowney               matstruct->descr, matrix->values->data().get(),
1279aa372e3fSPaul Mullowney               matrix->row_offsets->data().get(),
1280aa372e3fSPaul Mullowney               matrix->column_indices->data().get(),
1281c41cb2e2SAlejandro Lamas Daviña               hybMat, 0, partition);CHKERRCUDA(stat);
1282aa372e3fSPaul Mullowney           /* assign the pointer */
1283aa372e3fSPaul Mullowney           matstruct->mat = hybMat;
1284aa372e3fSPaul Mullowney 
1285aa372e3fSPaul Mullowney           if (matrix) {
1286aa372e3fSPaul Mullowney             if (matrix->values) delete (THRUSTARRAY*)matrix->values;
1287aa372e3fSPaul Mullowney             if (matrix->column_indices) delete (THRUSTINTARRAY32*)matrix->column_indices;
1288aa372e3fSPaul Mullowney             if (matrix->row_offsets) delete (THRUSTINTARRAY32*)matrix->row_offsets;
1289aa372e3fSPaul Mullowney             delete (CsrMatrix*)matrix;
1290087f3262SPaul Mullowney           }
12912692e278SPaul Mullowney #endif
1292087f3262SPaul Mullowney         }
1293ca45077fSPaul Mullowney 
1294aa372e3fSPaul Mullowney         /* assign the compressed row indices */
1295aa372e3fSPaul Mullowney         matstruct->cprowIndices = new THRUSTINTARRAY(m);
1296aa372e3fSPaul Mullowney         matstruct->cprowIndices->assign(ridx,ridx+m);
1297aa372e3fSPaul Mullowney 
1298aa372e3fSPaul Mullowney         /* assign the pointer */
1299aa372e3fSPaul Mullowney         cusparsestruct->mat = matstruct;
1300aa372e3fSPaul Mullowney 
13019ae82921SPaul Mullowney         if (!a->compressedrow.use) {
13029ae82921SPaul Mullowney           ierr = PetscFree(ii);CHKERRQ(ierr);
13039ae82921SPaul Mullowney           ierr = PetscFree(ridx);CHKERRQ(ierr);
13049ae82921SPaul Mullowney         }
1305aa372e3fSPaul Mullowney         cusparsestruct->workVector = new THRUSTARRAY;
1306aa372e3fSPaul Mullowney         cusparsestruct->workVector->resize(m);
13079ae82921SPaul Mullowney       } catch(char *ex) {
13089ae82921SPaul Mullowney         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
13099ae82921SPaul Mullowney       }
131034d6c7a5SJose E. Roman       cusparsestruct->nonzerostate = A->nonzerostate;
131134d6c7a5SJose E. Roman     }
13127d0e52d8SJose E. Roman     ierr = WaitForGPU();CHKERRCUDA(ierr);
13137d0e52d8SJose E. Roman     A->valid_GPU_matrix = PETSC_CUDA_BOTH;
13149ae82921SPaul Mullowney     ierr = PetscLogEventEnd(MAT_CUSPARSECopyToGPU,A,0,0,0);CHKERRQ(ierr);
13159ae82921SPaul Mullowney   }
13169ae82921SPaul Mullowney   PetscFunctionReturn(0);
13179ae82921SPaul Mullowney }
13189ae82921SPaul Mullowney 
13192a7a6963SBarry Smith static PetscErrorCode MatCreateVecs_SeqAIJCUSPARSE(Mat mat, Vec *right, Vec *left)
13209ae82921SPaul Mullowney {
13219ae82921SPaul Mullowney   PetscErrorCode ierr;
132233d57670SJed Brown   PetscInt rbs,cbs;
13239ae82921SPaul Mullowney 
13249ae82921SPaul Mullowney   PetscFunctionBegin;
132533d57670SJed Brown   ierr = MatGetBlockSizes(mat,&rbs,&cbs);CHKERRQ(ierr);
13269ae82921SPaul Mullowney   if (right) {
1327ce94432eSBarry Smith     ierr = VecCreate(PetscObjectComm((PetscObject)mat),right);CHKERRQ(ierr);
13289ae82921SPaul Mullowney     ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
132933d57670SJed Brown     ierr = VecSetBlockSize(*right,cbs);CHKERRQ(ierr);
1330c41cb2e2SAlejandro Lamas Daviña     ierr = VecSetType(*right,VECSEQCUDA);CHKERRQ(ierr);
13319ae82921SPaul Mullowney     ierr = PetscLayoutReference(mat->cmap,&(*right)->map);CHKERRQ(ierr);
13329ae82921SPaul Mullowney   }
13339ae82921SPaul Mullowney   if (left) {
1334ce94432eSBarry Smith     ierr = VecCreate(PetscObjectComm((PetscObject)mat),left);CHKERRQ(ierr);
13359ae82921SPaul Mullowney     ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
133633d57670SJed Brown     ierr = VecSetBlockSize(*left,rbs);CHKERRQ(ierr);
1337c41cb2e2SAlejandro Lamas Daviña     ierr = VecSetType(*left,VECSEQCUDA);CHKERRQ(ierr);
13389ae82921SPaul Mullowney     ierr = PetscLayoutReference(mat->rmap,&(*left)->map);CHKERRQ(ierr);
13399ae82921SPaul Mullowney   }
13409ae82921SPaul Mullowney   PetscFunctionReturn(0);
13419ae82921SPaul Mullowney }
13429ae82921SPaul Mullowney 
1343c41cb2e2SAlejandro Lamas Daviña struct VecCUDAPlusEquals
1344aa372e3fSPaul Mullowney {
1345aa372e3fSPaul Mullowney   template <typename Tuple>
1346aa372e3fSPaul Mullowney   __host__ __device__
1347aa372e3fSPaul Mullowney   void operator()(Tuple t)
1348aa372e3fSPaul Mullowney   {
1349aa372e3fSPaul Mullowney     thrust::get<1>(t) = thrust::get<1>(t) + thrust::get<0>(t);
1350aa372e3fSPaul Mullowney   }
1351aa372e3fSPaul Mullowney };
1352aa372e3fSPaul Mullowney 
13536fa9248bSJed Brown static PetscErrorCode MatMult_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy)
13549ae82921SPaul Mullowney {
13559ae82921SPaul Mullowney   Mat_SeqAIJ                   *a = (Mat_SeqAIJ*)A->data;
1356aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE           *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
1357aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat;
1358465f34aeSAlejandro Lamas Daviña   const PetscScalar            *xarray;
1359465f34aeSAlejandro Lamas Daviña   PetscScalar                  *yarray;
1360b175d8bbSPaul Mullowney   PetscErrorCode               ierr;
1361aa372e3fSPaul Mullowney   cusparseStatus_t             stat;
13629ae82921SPaul Mullowney 
13639ae82921SPaul Mullowney   PetscFunctionBegin;
136434d6c7a5SJose E. Roman   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
136534d6c7a5SJose E. Roman   ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
1366c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr);
1367*ca6ae6e6SAlejandro Lamas Daviña   ierr = VecSet(yy,0);CHKERRQ(ierr);
1368c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(yy,&yarray);CHKERRQ(ierr);
1369aa372e3fSPaul Mullowney   if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
1370aa372e3fSPaul Mullowney     CsrMatrix *mat = (CsrMatrix*)matstruct->mat;
1371aa372e3fSPaul Mullowney     stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1372aa372e3fSPaul Mullowney                              mat->num_rows, mat->num_cols, mat->num_entries,
1373b06137fdSPaul Mullowney                              matstruct->alpha, matstruct->descr, mat->values->data().get(), mat->row_offsets->data().get(),
1374c41cb2e2SAlejandro Lamas Daviña                              mat->column_indices->data().get(), xarray, matstruct->beta,
1375c41cb2e2SAlejandro Lamas Daviña                              yarray);CHKERRCUDA(stat);
1376aa372e3fSPaul Mullowney   } else {
13772692e278SPaul Mullowney #if CUDA_VERSION>=4020
1378aa372e3fSPaul Mullowney     cusparseHybMat_t hybMat = (cusparseHybMat_t)matstruct->mat;
1379aa372e3fSPaul Mullowney     stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1380b06137fdSPaul Mullowney                              matstruct->alpha, matstruct->descr, hybMat,
1381c41cb2e2SAlejandro Lamas Daviña                              xarray, matstruct->beta,
1382c41cb2e2SAlejandro Lamas Daviña                              yarray);CHKERRCUDA(stat);
13832692e278SPaul Mullowney #endif
13849ae82921SPaul Mullowney   }
1385c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr);
1386c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(yy,&yarray);CHKERRQ(ierr);
1387aa372e3fSPaul Mullowney   if (!cusparsestruct->stream) {
1388c41cb2e2SAlejandro Lamas Daviña     ierr = WaitForGPU();CHKERRCUDA(ierr);
1389ca45077fSPaul Mullowney   }
1390aa372e3fSPaul Mullowney   ierr = PetscLogFlops(2.0*a->nz - cusparsestruct->nonzerorow);CHKERRQ(ierr);
13919ae82921SPaul Mullowney   PetscFunctionReturn(0);
13929ae82921SPaul Mullowney }
13939ae82921SPaul Mullowney 
13946fa9248bSJed Brown static PetscErrorCode MatMultTranspose_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy)
1395ca45077fSPaul Mullowney {
1396ca45077fSPaul Mullowney   Mat_SeqAIJ                   *a = (Mat_SeqAIJ*)A->data;
1397aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE           *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
1398aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
1399465f34aeSAlejandro Lamas Daviña   const PetscScalar            *xarray;
1400465f34aeSAlejandro Lamas Daviña   PetscScalar                  *yarray;
1401b175d8bbSPaul Mullowney   PetscErrorCode               ierr;
1402aa372e3fSPaul Mullowney   cusparseStatus_t             stat;
1403ca45077fSPaul Mullowney 
1404ca45077fSPaul Mullowney   PetscFunctionBegin;
140534d6c7a5SJose E. Roman   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
140634d6c7a5SJose E. Roman   ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
1407aa372e3fSPaul Mullowney   if (!matstructT) {
1408bda325fcSPaul Mullowney     ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr);
1409aa372e3fSPaul Mullowney     matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
1410bda325fcSPaul Mullowney   }
1411c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr);
1412*ca6ae6e6SAlejandro Lamas Daviña   ierr = VecSet(yy,0);CHKERRQ(ierr);
1413c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDAGetArrayWrite(yy,&yarray);CHKERRQ(ierr);
1414aa372e3fSPaul Mullowney 
1415aa372e3fSPaul Mullowney   if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
1416aa372e3fSPaul Mullowney     CsrMatrix *mat = (CsrMatrix*)matstructT->mat;
1417aa372e3fSPaul Mullowney     stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1418aa372e3fSPaul Mullowney                              mat->num_rows, mat->num_cols,
1419b06137fdSPaul Mullowney                              mat->num_entries, matstructT->alpha, matstructT->descr,
1420aa372e3fSPaul Mullowney                              mat->values->data().get(), mat->row_offsets->data().get(),
1421c41cb2e2SAlejandro Lamas Daviña                              mat->column_indices->data().get(), xarray, matstructT->beta,
1422c41cb2e2SAlejandro Lamas Daviña                              yarray);CHKERRCUDA(stat);
1423aa372e3fSPaul Mullowney   } else {
14242692e278SPaul Mullowney #if CUDA_VERSION>=4020
1425aa372e3fSPaul Mullowney     cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat;
1426aa372e3fSPaul Mullowney     stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1427b06137fdSPaul Mullowney                              matstructT->alpha, matstructT->descr, hybMat,
1428c41cb2e2SAlejandro Lamas Daviña                              xarray, matstructT->beta,
1429c41cb2e2SAlejandro Lamas Daviña                              yarray);CHKERRCUDA(stat);
14302692e278SPaul Mullowney #endif
1431ca45077fSPaul Mullowney   }
1432c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr);
1433c41cb2e2SAlejandro Lamas Daviña   ierr = VecCUDARestoreArrayWrite(yy,&yarray);CHKERRQ(ierr);
1434aa372e3fSPaul Mullowney   if (!cusparsestruct->stream) {
1435c41cb2e2SAlejandro Lamas Daviña     ierr = WaitForGPU();CHKERRCUDA(ierr);
1436ca45077fSPaul Mullowney   }
1437aa372e3fSPaul Mullowney   ierr = PetscLogFlops(2.0*a->nz - cusparsestruct->nonzerorow);CHKERRQ(ierr);
1438ca45077fSPaul Mullowney   PetscFunctionReturn(0);
1439ca45077fSPaul Mullowney }
1440ca45077fSPaul Mullowney 
1441aa372e3fSPaul Mullowney 
14426fa9248bSJed Brown static PetscErrorCode MatMultAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz)
14439ae82921SPaul Mullowney {
14449ae82921SPaul Mullowney   Mat_SeqAIJ                      *a = (Mat_SeqAIJ*)A->data;
1445aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE              *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
1446aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct    *matstruct = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->mat;
1447c41cb2e2SAlejandro Lamas Daviña   thrust::device_ptr<PetscScalar> zptr;
1448465f34aeSAlejandro Lamas Daviña   const PetscScalar               *xarray;
1449465f34aeSAlejandro Lamas Daviña   PetscScalar                     *zarray;
1450b175d8bbSPaul Mullowney   PetscErrorCode                  ierr;
1451aa372e3fSPaul Mullowney   cusparseStatus_t                stat;
14526e111a19SKarl Rupp 
14539ae82921SPaul Mullowney   PetscFunctionBegin;
145434d6c7a5SJose E. Roman   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
145534d6c7a5SJose E. Roman   ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
14569ae82921SPaul Mullowney   try {
1457c41cb2e2SAlejandro Lamas Daviña     ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr);
1458c41cb2e2SAlejandro Lamas Daviña     ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr);
145934d6c7a5SJose E. Roman     ierr = VecCUDAGetArrayReadWrite(zz,&zarray);CHKERRQ(ierr);
1460c41cb2e2SAlejandro Lamas Daviña     zptr = thrust::device_pointer_cast(zarray);
14619ae82921SPaul Mullowney 
1462e057df02SPaul Mullowney     /* multiply add */
1463aa372e3fSPaul Mullowney     if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
1464aa372e3fSPaul Mullowney       CsrMatrix *mat = (CsrMatrix*)matstruct->mat;
1465b06137fdSPaul Mullowney     /* here we need to be careful to set the number of rows in the multiply to the
1466b06137fdSPaul Mullowney        number of compressed rows in the matrix ... which is equivalent to the
1467b06137fdSPaul Mullowney        size of the workVector */
1468aa372e3fSPaul Mullowney       stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1469a65300a6SPaul Mullowney                                mat->num_rows, mat->num_cols,
1470b06137fdSPaul Mullowney                                mat->num_entries, matstruct->alpha, matstruct->descr,
1471aa372e3fSPaul Mullowney                                mat->values->data().get(), mat->row_offsets->data().get(),
1472c41cb2e2SAlejandro Lamas Daviña                                mat->column_indices->data().get(), xarray, matstruct->beta,
1473c41cb2e2SAlejandro Lamas Daviña                                cusparsestruct->workVector->data().get());CHKERRCUDA(stat);
1474aa372e3fSPaul Mullowney     } else {
14752692e278SPaul Mullowney #if CUDA_VERSION>=4020
1476aa372e3fSPaul Mullowney       cusparseHybMat_t hybMat = (cusparseHybMat_t)matstruct->mat;
1477a65300a6SPaul Mullowney       if (cusparsestruct->workVector->size()) {
1478aa372e3fSPaul Mullowney         stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1479b06137fdSPaul Mullowney             matstruct->alpha, matstruct->descr, hybMat,
1480c41cb2e2SAlejandro Lamas Daviña             xarray, matstruct->beta,
1481c41cb2e2SAlejandro Lamas Daviña             cusparsestruct->workVector->data().get());CHKERRCUDA(stat);
1482a65300a6SPaul Mullowney       }
14832692e278SPaul Mullowney #endif
1484aa372e3fSPaul Mullowney     }
1485aa372e3fSPaul Mullowney 
1486aa372e3fSPaul Mullowney     /* scatter the data from the temporary into the full vector with a += operation */
1487c41cb2e2SAlejandro Lamas Daviña     thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))),
1488c41cb2e2SAlejandro Lamas Daviña         thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstruct->cprowIndices->begin()))) + cusparsestruct->workVector->size(),
1489c41cb2e2SAlejandro Lamas Daviña         VecCUDAPlusEquals());
1490c41cb2e2SAlejandro Lamas Daviña     ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr);
149134d6c7a5SJose E. Roman     ierr = VecCUDARestoreArrayReadWrite(zz,&zarray);CHKERRQ(ierr);
14929ae82921SPaul Mullowney 
14939ae82921SPaul Mullowney   } catch(char *ex) {
14949ae82921SPaul Mullowney     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
14959ae82921SPaul Mullowney   }
1496c41cb2e2SAlejandro Lamas Daviña   ierr = WaitForGPU();CHKERRCUDA(ierr);
14979ae82921SPaul Mullowney   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
14989ae82921SPaul Mullowney   PetscFunctionReturn(0);
14999ae82921SPaul Mullowney }
15009ae82921SPaul Mullowney 
15016fa9248bSJed Brown static PetscErrorCode MatMultTransposeAdd_SeqAIJCUSPARSE(Mat A,Vec xx,Vec yy,Vec zz)
1502ca45077fSPaul Mullowney {
1503ca45077fSPaul Mullowney   Mat_SeqAIJ                      *a = (Mat_SeqAIJ*)A->data;
1504aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSE              *cusparsestruct = (Mat_SeqAIJCUSPARSE*)A->spptr;
1505aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct    *matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
1506c41cb2e2SAlejandro Lamas Daviña   thrust::device_ptr<PetscScalar> zptr;
1507465f34aeSAlejandro Lamas Daviña   const PetscScalar               *xarray;
1508465f34aeSAlejandro Lamas Daviña   PetscScalar                     *zarray;
1509b175d8bbSPaul Mullowney   PetscErrorCode                  ierr;
1510aa372e3fSPaul Mullowney   cusparseStatus_t                stat;
15116e111a19SKarl Rupp 
1512ca45077fSPaul Mullowney   PetscFunctionBegin;
151334d6c7a5SJose E. Roman   /* The line below is necessary due to the operations that modify the matrix on the CPU (axpy, scale, etc) */
151434d6c7a5SJose E. Roman   ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
1515aa372e3fSPaul Mullowney   if (!matstructT) {
1516bda325fcSPaul Mullowney     ierr = MatSeqAIJCUSPARSEGenerateTransposeForMult(A);CHKERRQ(ierr);
1517aa372e3fSPaul Mullowney     matstructT = (Mat_SeqAIJCUSPARSEMultStruct*)cusparsestruct->matTranspose;
1518bda325fcSPaul Mullowney   }
1519aa372e3fSPaul Mullowney 
1520ca45077fSPaul Mullowney   try {
1521c41cb2e2SAlejandro Lamas Daviña     ierr = VecCopy_SeqCUDA(yy,zz);CHKERRQ(ierr);
1522c41cb2e2SAlejandro Lamas Daviña     ierr = VecCUDAGetArrayRead(xx,&xarray);CHKERRQ(ierr);
152334d6c7a5SJose E. Roman     ierr = VecCUDAGetArrayReadWrite(zz,&zarray);CHKERRQ(ierr);
1524c41cb2e2SAlejandro Lamas Daviña     zptr = thrust::device_pointer_cast(zarray);
1525ca45077fSPaul Mullowney 
1526e057df02SPaul Mullowney     /* multiply add with matrix transpose */
1527aa372e3fSPaul Mullowney     if (cusparsestruct->format==MAT_CUSPARSE_CSR) {
1528aa372e3fSPaul Mullowney       CsrMatrix *mat = (CsrMatrix*)matstructT->mat;
1529b06137fdSPaul Mullowney       /* here we need to be careful to set the number of rows in the multiply to the
1530b06137fdSPaul Mullowney          number of compressed rows in the matrix ... which is equivalent to the
1531b06137fdSPaul Mullowney          size of the workVector */
1532aa372e3fSPaul Mullowney       stat = cusparse_csr_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1533a65300a6SPaul Mullowney                                mat->num_rows, mat->num_cols,
1534b06137fdSPaul Mullowney                                mat->num_entries, matstructT->alpha, matstructT->descr,
1535aa372e3fSPaul Mullowney                                mat->values->data().get(), mat->row_offsets->data().get(),
1536c41cb2e2SAlejandro Lamas Daviña                                mat->column_indices->data().get(), xarray, matstructT->beta,
1537c41cb2e2SAlejandro Lamas Daviña                                cusparsestruct->workVector->data().get());CHKERRCUDA(stat);
1538aa372e3fSPaul Mullowney     } else {
15392692e278SPaul Mullowney #if CUDA_VERSION>=4020
1540aa372e3fSPaul Mullowney       cusparseHybMat_t hybMat = (cusparseHybMat_t)matstructT->mat;
1541a65300a6SPaul Mullowney       if (cusparsestruct->workVector->size()) {
1542aa372e3fSPaul Mullowney         stat = cusparse_hyb_spmv(cusparsestruct->handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
1543b06137fdSPaul Mullowney             matstructT->alpha, matstructT->descr, hybMat,
1544c41cb2e2SAlejandro Lamas Daviña             xarray, matstructT->beta,
1545c41cb2e2SAlejandro Lamas Daviña             cusparsestruct->workVector->data().get());CHKERRCUDA(stat);
1546a65300a6SPaul Mullowney       }
15472692e278SPaul Mullowney #endif
1548aa372e3fSPaul Mullowney     }
1549aa372e3fSPaul Mullowney 
1550aa372e3fSPaul Mullowney     /* scatter the data from the temporary into the full vector with a += operation */
1551c41cb2e2SAlejandro Lamas Daviña     thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstructT->cprowIndices->begin()))),
1552c41cb2e2SAlejandro Lamas Daviña         thrust::make_zip_iterator(thrust::make_tuple(cusparsestruct->workVector->begin(), thrust::make_permutation_iterator(zptr, matstructT->cprowIndices->begin()))) + cusparsestruct->workVector->size(),
1553c41cb2e2SAlejandro Lamas Daviña         VecCUDAPlusEquals());
1554ca45077fSPaul Mullowney 
1555c41cb2e2SAlejandro Lamas Daviña     ierr = VecCUDARestoreArrayRead(xx,&xarray);CHKERRQ(ierr);
155634d6c7a5SJose E. Roman     ierr = VecCUDARestoreArrayReadWrite(zz,&zarray);CHKERRQ(ierr);
1557ca45077fSPaul Mullowney 
1558ca45077fSPaul Mullowney   } catch(char *ex) {
1559ca45077fSPaul Mullowney     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSPARSE error: %s", ex);
1560ca45077fSPaul Mullowney   }
1561c41cb2e2SAlejandro Lamas Daviña   ierr = WaitForGPU();CHKERRCUDA(ierr);
1562ca45077fSPaul Mullowney   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1563ca45077fSPaul Mullowney   PetscFunctionReturn(0);
1564ca45077fSPaul Mullowney }
1565ca45077fSPaul Mullowney 
15666fa9248bSJed Brown static PetscErrorCode MatAssemblyEnd_SeqAIJCUSPARSE(Mat A,MatAssemblyType mode)
15679ae82921SPaul Mullowney {
15689ae82921SPaul Mullowney   PetscErrorCode ierr;
15696e111a19SKarl Rupp 
15709ae82921SPaul Mullowney   PetscFunctionBegin;
15719ae82921SPaul Mullowney   ierr = MatAssemblyEnd_SeqAIJ(A,mode);CHKERRQ(ierr);
1572bc3f50f2SPaul Mullowney   if (A->factortype==MAT_FACTOR_NONE) {
1573e057df02SPaul Mullowney     ierr = MatSeqAIJCUSPARSECopyToGPU(A);CHKERRQ(ierr);
1574bc3f50f2SPaul Mullowney   }
15759ae82921SPaul Mullowney   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
1576bbf3fe20SPaul Mullowney   A->ops->mult             = MatMult_SeqAIJCUSPARSE;
1577bbf3fe20SPaul Mullowney   A->ops->multadd          = MatMultAdd_SeqAIJCUSPARSE;
1578bbf3fe20SPaul Mullowney   A->ops->multtranspose    = MatMultTranspose_SeqAIJCUSPARSE;
1579bbf3fe20SPaul Mullowney   A->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE;
15809ae82921SPaul Mullowney   PetscFunctionReturn(0);
15819ae82921SPaul Mullowney }
15829ae82921SPaul Mullowney 
15839ae82921SPaul Mullowney /* --------------------------------------------------------------------------------*/
1584e057df02SPaul Mullowney /*@
15859ae82921SPaul Mullowney    MatCreateSeqAIJCUSPARSE - Creates a sparse matrix in AIJ (compressed row) format
1586e057df02SPaul Mullowney    (the default parallel PETSc format). This matrix will ultimately pushed down
1587e057df02SPaul Mullowney    to NVidia GPUs and use the CUSPARSE library for calculations. For good matrix
1588e057df02SPaul Mullowney    assembly performance the user should preallocate the matrix storage by setting
1589e057df02SPaul Mullowney    the parameter nz (or the array nnz).  By setting these parameters accurately,
1590e057df02SPaul Mullowney    performance during matrix assembly can be increased by more than a factor of 50.
15919ae82921SPaul Mullowney 
15929ae82921SPaul Mullowney    Collective on MPI_Comm
15939ae82921SPaul Mullowney 
15949ae82921SPaul Mullowney    Input Parameters:
15959ae82921SPaul Mullowney +  comm - MPI communicator, set to PETSC_COMM_SELF
15969ae82921SPaul Mullowney .  m - number of rows
15979ae82921SPaul Mullowney .  n - number of columns
15989ae82921SPaul Mullowney .  nz - number of nonzeros per row (same for all rows)
15999ae82921SPaul Mullowney -  nnz - array containing the number of nonzeros in the various rows
16000298fd71SBarry Smith          (possibly different for each row) or NULL
16019ae82921SPaul Mullowney 
16029ae82921SPaul Mullowney    Output Parameter:
16039ae82921SPaul Mullowney .  A - the matrix
16049ae82921SPaul Mullowney 
16059ae82921SPaul Mullowney    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
16069ae82921SPaul Mullowney    MatXXXXSetPreallocation() paradgm instead of this routine directly.
16079ae82921SPaul Mullowney    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
16089ae82921SPaul Mullowney 
16099ae82921SPaul Mullowney    Notes:
16109ae82921SPaul Mullowney    If nnz is given then nz is ignored
16119ae82921SPaul Mullowney 
16129ae82921SPaul Mullowney    The AIJ format (also called the Yale sparse matrix format or
16139ae82921SPaul Mullowney    compressed row storage), is fully compatible with standard Fortran 77
16149ae82921SPaul Mullowney    storage.  That is, the stored row and column indices can begin at
16159ae82921SPaul Mullowney    either one (as in Fortran) or zero.  See the users' manual for details.
16169ae82921SPaul Mullowney 
16179ae82921SPaul Mullowney    Specify the preallocated storage with either nz or nnz (not both).
16180298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
16199ae82921SPaul Mullowney    allocation.  For large problems you MUST preallocate memory or you
16209ae82921SPaul Mullowney    will get TERRIBLE performance, see the users' manual chapter on matrices.
16219ae82921SPaul Mullowney 
16229ae82921SPaul Mullowney    By default, this format uses inodes (identical nodes) when possible, to
16239ae82921SPaul Mullowney    improve numerical efficiency of matrix-vector products and solves. We
16249ae82921SPaul Mullowney    search for consecutive rows with the same nonzero structure, thereby
16259ae82921SPaul Mullowney    reusing matrix information to achieve increased efficiency.
16269ae82921SPaul Mullowney 
16279ae82921SPaul Mullowney    Level: intermediate
16289ae82921SPaul Mullowney 
1629e057df02SPaul Mullowney .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ(), MATSEQAIJCUSPARSE, MATAIJCUSPARSE
16309ae82921SPaul Mullowney @*/
16319ae82921SPaul Mullowney PetscErrorCode  MatCreateSeqAIJCUSPARSE(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
16329ae82921SPaul Mullowney {
16339ae82921SPaul Mullowney   PetscErrorCode ierr;
16349ae82921SPaul Mullowney 
16359ae82921SPaul Mullowney   PetscFunctionBegin;
16369ae82921SPaul Mullowney   ierr = MatCreate(comm,A);CHKERRQ(ierr);
16379ae82921SPaul Mullowney   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
16389ae82921SPaul Mullowney   ierr = MatSetType(*A,MATSEQAIJCUSPARSE);CHKERRQ(ierr);
16399ae82921SPaul Mullowney   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
16409ae82921SPaul Mullowney   PetscFunctionReturn(0);
16419ae82921SPaul Mullowney }
16429ae82921SPaul Mullowney 
16436fa9248bSJed Brown static PetscErrorCode MatDestroy_SeqAIJCUSPARSE(Mat A)
16449ae82921SPaul Mullowney {
16459ae82921SPaul Mullowney   PetscErrorCode   ierr;
1646ab25e6cbSDominic Meiser 
16479ae82921SPaul Mullowney   PetscFunctionBegin;
16489ae82921SPaul Mullowney   if (A->factortype==MAT_FACTOR_NONE) {
1649c41cb2e2SAlejandro Lamas Daviña     if (A->valid_GPU_matrix != PETSC_CUDA_UNALLOCATED) {
1650ab25e6cbSDominic Meiser       ierr = Mat_SeqAIJCUSPARSE_Destroy((Mat_SeqAIJCUSPARSE**)&A->spptr);CHKERRQ(ierr);
16519ae82921SPaul Mullowney     }
16529ae82921SPaul Mullowney   } else {
1653ab25e6cbSDominic Meiser     ierr = Mat_SeqAIJCUSPARSETriFactors_Destroy((Mat_SeqAIJCUSPARSETriFactors**)&A->spptr);CHKERRQ(ierr);
1654aa372e3fSPaul Mullowney   }
16559ae82921SPaul Mullowney   ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr);
16569ae82921SPaul Mullowney   PetscFunctionReturn(0);
16579ae82921SPaul Mullowney }
16589ae82921SPaul Mullowney 
16598cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJCUSPARSE(Mat B)
16609ae82921SPaul Mullowney {
16619ae82921SPaul Mullowney   PetscErrorCode ierr;
1662aa372e3fSPaul Mullowney   cusparseStatus_t stat;
1663aa372e3fSPaul Mullowney   cusparseHandle_t handle=0;
16649ae82921SPaul Mullowney 
16659ae82921SPaul Mullowney   PetscFunctionBegin;
16669ae82921SPaul Mullowney   ierr = MatCreate_SeqAIJ(B);CHKERRQ(ierr);
16679ae82921SPaul Mullowney   if (B->factortype==MAT_FACTOR_NONE) {
1668e057df02SPaul Mullowney     /* you cannot check the inode.use flag here since the matrix was just created.
1669e057df02SPaul Mullowney        now build a GPU matrix data structure */
16709ae82921SPaul Mullowney     B->spptr = new Mat_SeqAIJCUSPARSE;
16719ae82921SPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->mat          = 0;
1672aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->matTranspose = 0;
1673aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->workVector   = 0;
1674e057df02SPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->format       = MAT_CUSPARSE_CSR;
1675aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->stream       = 0;
1676aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->handle       = 0;
1677c41cb2e2SAlejandro Lamas Daviña     stat = cusparseCreate(&handle);CHKERRCUDA(stat);
1678aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->handle       = handle;
1679aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSE*)B->spptr)->stream       = 0;
16809ae82921SPaul Mullowney   } else {
16819ae82921SPaul Mullowney     /* NEXT, set the pointers to the triangular factors */
1682debe9ee2SPaul Mullowney     B->spptr = new Mat_SeqAIJCUSPARSETriFactors;
16839ae82921SPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtr          = 0;
16849ae82921SPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtr          = 0;
1685aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->loTriFactorPtrTranspose = 0;
1686aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->upTriFactorPtrTranspose = 0;
1687aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->rpermIndices            = 0;
1688aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->cpermIndices            = 0;
1689aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->workVector              = 0;
1690aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->handle                  = 0;
1691c41cb2e2SAlejandro Lamas Daviña     stat = cusparseCreate(&handle);CHKERRCUDA(stat);
1692aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->handle                  = handle;
1693aa372e3fSPaul Mullowney     ((Mat_SeqAIJCUSPARSETriFactors*)B->spptr)->nnz                     = 0;
16949ae82921SPaul Mullowney   }
1695aa372e3fSPaul Mullowney 
16969ae82921SPaul Mullowney   B->ops->assemblyend      = MatAssemblyEnd_SeqAIJCUSPARSE;
16979ae82921SPaul Mullowney   B->ops->destroy          = MatDestroy_SeqAIJCUSPARSE;
16982a7a6963SBarry Smith   B->ops->getvecs          = MatCreateVecs_SeqAIJCUSPARSE;
16999ae82921SPaul Mullowney   B->ops->setfromoptions   = MatSetFromOptions_SeqAIJCUSPARSE;
1700ca45077fSPaul Mullowney   B->ops->mult             = MatMult_SeqAIJCUSPARSE;
1701ca45077fSPaul Mullowney   B->ops->multadd          = MatMultAdd_SeqAIJCUSPARSE;
1702ca45077fSPaul Mullowney   B->ops->multtranspose    = MatMultTranspose_SeqAIJCUSPARSE;
1703ca45077fSPaul Mullowney   B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJCUSPARSE;
17042205254eSKarl Rupp 
17059ae82921SPaul Mullowney   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJCUSPARSE);CHKERRQ(ierr);
17062205254eSKarl Rupp 
1707c41cb2e2SAlejandro Lamas Daviña   B->valid_GPU_matrix = PETSC_CUDA_UNALLOCATED;
17082205254eSKarl Rupp 
1709bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B, "MatCUSPARSESetFormat_C", MatCUSPARSESetFormat_SeqAIJCUSPARSE);CHKERRQ(ierr);
17109ae82921SPaul Mullowney   PetscFunctionReturn(0);
17119ae82921SPaul Mullowney }
17129ae82921SPaul Mullowney 
1713e057df02SPaul Mullowney /*M
1714e057df02SPaul Mullowney    MATSEQAIJCUSPARSE - MATAIJCUSPARSE = "(seq)aijcusparse" - A matrix type to be used for sparse matrices.
1715e057df02SPaul Mullowney 
1716e057df02SPaul Mullowney    A matrix type type whose data resides on Nvidia GPUs. These matrices can be in either
17172692e278SPaul Mullowney    CSR, ELL, or Hybrid format. The ELL and HYB formats require CUDA 4.2 or later.
17182692e278SPaul Mullowney    All matrix calculations are performed on Nvidia GPUs using the CUSPARSE library.
1719e057df02SPaul Mullowney 
1720e057df02SPaul Mullowney    Options Database Keys:
1721e057df02SPaul Mullowney +  -mat_type aijcusparse - sets the matrix type to "seqaijcusparse" during a call to MatSetFromOptions()
1722aa372e3fSPaul 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).
1723aa372e3fSPaul Mullowney .  -mat_cusparse_mult_storage_format csr - sets the storage format of matrices (for MatMult) during a call to MatSetFromOptions(). Other options include ell (ellpack) or hyb (hybrid).
1724e057df02SPaul Mullowney 
1725e057df02SPaul Mullowney   Level: beginner
1726e057df02SPaul Mullowney 
17278468deeeSKarl Rupp .seealso: MatCreateSeqAIJCUSPARSE(), MATAIJCUSPARSE, MatCreateAIJCUSPARSE(), MatCUSPARSESetFormat(), MatCUSPARSEStorageFormat, MatCUSPARSEFormatOperation
1728e057df02SPaul Mullowney M*/
17297f756511SDominic Meiser 
173042c9c57cSBarry Smith PETSC_EXTERN PetscErrorCode MatGetFactor_seqaijcusparse_cusparse(Mat,MatFactorType,Mat*);
173142c9c57cSBarry Smith 
17320f39cd5aSBarry Smith 
173329b38603SBarry Smith PETSC_EXTERN PetscErrorCode MatSolverPackageRegister_CUSPARSE(void)
173442c9c57cSBarry Smith {
173542c9c57cSBarry Smith   PetscErrorCode ierr;
173642c9c57cSBarry Smith 
173742c9c57cSBarry Smith   PetscFunctionBegin;
173842c9c57cSBarry Smith   ierr = MatSolverPackageRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_LU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr);
173942c9c57cSBarry Smith   ierr = MatSolverPackageRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_CHOLESKY,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr);
174042c9c57cSBarry Smith   ierr = MatSolverPackageRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ILU,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr);
174142c9c57cSBarry Smith   ierr = MatSolverPackageRegister(MATSOLVERCUSPARSE,MATSEQAIJCUSPARSE,MAT_FACTOR_ICC,MatGetFactor_seqaijcusparse_cusparse);CHKERRQ(ierr);
174242c9c57cSBarry Smith   PetscFunctionReturn(0);
174342c9c57cSBarry Smith }
174429b38603SBarry Smith 
174581e08676SBarry Smith 
17467f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSE_Destroy(Mat_SeqAIJCUSPARSE **cusparsestruct)
17477f756511SDominic Meiser {
17487f756511SDominic Meiser   cusparseStatus_t stat;
17497f756511SDominic Meiser   cusparseHandle_t handle;
17507f756511SDominic Meiser 
17517f756511SDominic Meiser   PetscFunctionBegin;
17527f756511SDominic Meiser   if (*cusparsestruct) {
17537f756511SDominic Meiser     Mat_SeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->mat,(*cusparsestruct)->format);
17547f756511SDominic Meiser     Mat_SeqAIJCUSPARSEMultStruct_Destroy(&(*cusparsestruct)->matTranspose,(*cusparsestruct)->format);
17557f756511SDominic Meiser     delete (*cusparsestruct)->workVector;
17567f756511SDominic Meiser     if (handle = (*cusparsestruct)->handle) {
1757c41cb2e2SAlejandro Lamas Daviña       stat = cusparseDestroy(handle);CHKERRCUDA(stat);
17587f756511SDominic Meiser     }
17597f756511SDominic Meiser     delete *cusparsestruct;
17607f756511SDominic Meiser     *cusparsestruct = 0;
17617f756511SDominic Meiser   }
17627f756511SDominic Meiser   PetscFunctionReturn(0);
17637f756511SDominic Meiser }
17647f756511SDominic Meiser 
17657f756511SDominic Meiser static PetscErrorCode CsrMatrix_Destroy(CsrMatrix **mat)
17667f756511SDominic Meiser {
17677f756511SDominic Meiser   PetscFunctionBegin;
17687f756511SDominic Meiser   if (*mat) {
17697f756511SDominic Meiser     delete (*mat)->values;
17707f756511SDominic Meiser     delete (*mat)->column_indices;
17717f756511SDominic Meiser     delete (*mat)->row_offsets;
17727f756511SDominic Meiser     delete *mat;
17737f756511SDominic Meiser     *mat = 0;
17747f756511SDominic Meiser   }
17757f756511SDominic Meiser   PetscFunctionReturn(0);
17767f756511SDominic Meiser }
17777f756511SDominic Meiser 
17787f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSETriFactorStruct_Destroy(Mat_SeqAIJCUSPARSETriFactorStruct **trifactor)
17797f756511SDominic Meiser {
17807f756511SDominic Meiser   cusparseStatus_t stat;
17817f756511SDominic Meiser   PetscErrorCode   ierr;
17827f756511SDominic Meiser 
17837f756511SDominic Meiser   PetscFunctionBegin;
17847f756511SDominic Meiser   if (*trifactor) {
1785c41cb2e2SAlejandro Lamas Daviña     if ((*trifactor)->descr) { stat = cusparseDestroyMatDescr((*trifactor)->descr);CHKERRCUDA(stat); }
1786c41cb2e2SAlejandro Lamas Daviña     if ((*trifactor)->solveInfo) { stat = cusparseDestroySolveAnalysisInfo((*trifactor)->solveInfo);CHKERRCUDA(stat); }
17877f756511SDominic Meiser     ierr = CsrMatrix_Destroy(&(*trifactor)->csrMat);CHKERRQ(ierr);
17887f756511SDominic Meiser     delete *trifactor;
17897f756511SDominic Meiser     *trifactor = 0;
17907f756511SDominic Meiser   }
17917f756511SDominic Meiser   PetscFunctionReturn(0);
17927f756511SDominic Meiser }
17937f756511SDominic Meiser 
17947f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSEMultStruct_Destroy(Mat_SeqAIJCUSPARSEMultStruct **matstruct,MatCUSPARSEStorageFormat format)
17957f756511SDominic Meiser {
17967f756511SDominic Meiser   CsrMatrix        *mat;
17977f756511SDominic Meiser   cusparseStatus_t stat;
17987f756511SDominic Meiser   cudaError_t      err;
17997f756511SDominic Meiser 
18007f756511SDominic Meiser   PetscFunctionBegin;
18017f756511SDominic Meiser   if (*matstruct) {
18027f756511SDominic Meiser     if ((*matstruct)->mat) {
18037f756511SDominic Meiser       if (format==MAT_CUSPARSE_ELL || format==MAT_CUSPARSE_HYB) {
18047f756511SDominic Meiser         cusparseHybMat_t hybMat = (cusparseHybMat_t)(*matstruct)->mat;
1805c41cb2e2SAlejandro Lamas Daviña         stat = cusparseDestroyHybMat(hybMat);CHKERRCUDA(stat);
18067f756511SDominic Meiser       } else {
18077f756511SDominic Meiser         mat = (CsrMatrix*)(*matstruct)->mat;
18087f756511SDominic Meiser         CsrMatrix_Destroy(&mat);
18097f756511SDominic Meiser       }
18107f756511SDominic Meiser     }
1811c41cb2e2SAlejandro Lamas Daviña     if ((*matstruct)->descr) { stat = cusparseDestroyMatDescr((*matstruct)->descr);CHKERRCUDA(stat); }
18127f756511SDominic Meiser     delete (*matstruct)->cprowIndices;
1813c41cb2e2SAlejandro Lamas Daviña     if ((*matstruct)->alpha) { err=cudaFree((*matstruct)->alpha);CHKERRCUDA(err); }
1814c41cb2e2SAlejandro Lamas Daviña     if ((*matstruct)->beta) { err=cudaFree((*matstruct)->beta);CHKERRCUDA(err); }
18157f756511SDominic Meiser     delete *matstruct;
18167f756511SDominic Meiser     *matstruct = 0;
18177f756511SDominic Meiser   }
18187f756511SDominic Meiser   PetscFunctionReturn(0);
18197f756511SDominic Meiser }
18207f756511SDominic Meiser 
18217f756511SDominic Meiser static PetscErrorCode Mat_SeqAIJCUSPARSETriFactors_Destroy(Mat_SeqAIJCUSPARSETriFactors** trifactors)
18227f756511SDominic Meiser {
18237f756511SDominic Meiser   cusparseHandle_t handle;
18247f756511SDominic Meiser   cusparseStatus_t stat;
18257f756511SDominic Meiser 
18267f756511SDominic Meiser   PetscFunctionBegin;
18277f756511SDominic Meiser   if (*trifactors) {
18287f756511SDominic Meiser     Mat_SeqAIJCUSPARSETriFactorStruct_Destroy(&(*trifactors)->loTriFactorPtr);
18297f756511SDominic Meiser     Mat_SeqAIJCUSPARSETriFactorStruct_Destroy(&(*trifactors)->upTriFactorPtr);
18307f756511SDominic Meiser     Mat_SeqAIJCUSPARSETriFactorStruct_Destroy(&(*trifactors)->loTriFactorPtrTranspose);
18317f756511SDominic Meiser     Mat_SeqAIJCUSPARSETriFactorStruct_Destroy(&(*trifactors)->upTriFactorPtrTranspose);
18327f756511SDominic Meiser     delete (*trifactors)->rpermIndices;
18337f756511SDominic Meiser     delete (*trifactors)->cpermIndices;
18347f756511SDominic Meiser     delete (*trifactors)->workVector;
18357f756511SDominic Meiser     if (handle = (*trifactors)->handle) {
1836c41cb2e2SAlejandro Lamas Daviña       stat = cusparseDestroy(handle);CHKERRCUDA(stat);
18377f756511SDominic Meiser     }
18387f756511SDominic Meiser     delete *trifactors;
18397f756511SDominic Meiser     *trifactors = 0;
18407f756511SDominic Meiser   }
18417f756511SDominic Meiser   PetscFunctionReturn(0);
18427f756511SDominic Meiser }
18437f756511SDominic Meiser 
1844