xref: /petsc/src/mat/impls/aij/seq/seqcusparse/cusparsematimpl.h (revision ddea5d601081b8d3ba49f4b67516b76abb728e3b)
1042217e8SBarry Smith #if !defined(CUSPARSEMATIMPL)
2042217e8SBarry Smith #define CUSPARSEMATIMPL
39ae82921SPaul Mullowney 
4afb2bd1cSJunchao Zhang #include <petscpkg_version.h>
5303a667bSSatish Balay #include <petsc/private/cudavecimpl.h>
6042217e8SBarry Smith #include <petscaijdevice.h>
79ae82921SPaul Mullowney 
89ae82921SPaul Mullowney #include <cusparse_v2.h>
99ae82921SPaul Mullowney 
109ae82921SPaul Mullowney #include <algorithm>
119ae82921SPaul Mullowney #include <vector>
129ae82921SPaul Mullowney 
13c41cb2e2SAlejandro Lamas Daviña #include <thrust/device_vector.h>
14c41cb2e2SAlejandro Lamas Daviña #include <thrust/device_ptr.h>
1550ab121bSSatish Balay #include <thrust/device_malloc_allocator.h>
16c41cb2e2SAlejandro Lamas Daviña #include <thrust/transform.h>
17c41cb2e2SAlejandro Lamas Daviña #include <thrust/functional.h>
18554b8892SKarl Rupp #include <thrust/sequence.h>
197eaca502SStefano Zampini #include <thrust/system/system_error.h>
20c41cb2e2SAlejandro Lamas Daviña 
21185e5899SJunchao Zhang #if (CUSPARSE_VER_MAJOR > 10 || CUSPARSE_VER_MAJOR == 10 && CUSPARSE_VER_MINOR >= 2) /* According to cuda/10.1.168 on OLCF Summit */
2257d48284SJunchao Zhang #define CHKERRCUSPARSE(stat)\
2357d48284SJunchao Zhang do {\
2457d48284SJunchao Zhang   if (PetscUnlikely(stat)) {\
2557d48284SJunchao Zhang     const char *name  = cusparseGetErrorName(stat);\
2657d48284SJunchao Zhang     const char *descr = cusparseGetErrorString(stat);\
27a4b895e1SBarry Smith     if ((stat == CUSPARSE_STATUS_NOT_INITIALIZED) || (stat == CUSPARSE_STATUS_ALLOC_FAILED)) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_GPU_RESOURCE,"cuSPARSE error %d (%s) : %s. Reports not initialized or alloc failed; this indicates the GPU has run out resources",(int)stat,name,descr); \
28a952e0d5SBarry Smith     else SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_GPU,"cuSPARSE error %d (%s) : %s",(int)stat,name,descr);\
2957d48284SJunchao Zhang   }\
3057d48284SJunchao Zhang } while (0)
316d22fe3dSJunchao Zhang #else
32589f383fSBarry Smith #define CHKERRCUSPARSE(stat) do {if (PetscUnlikely(stat)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_GPU,"cusparse error %d",(int)stat);} while (0)
336d22fe3dSJunchao Zhang #endif
3457d48284SJunchao Zhang 
357eaca502SStefano Zampini #define PetscStackCallThrust(body) do {                                     \
367eaca502SStefano Zampini     try {                                                                   \
377eaca502SStefano Zampini       body;                                                                 \
387eaca502SStefano Zampini     } catch(thrust::system_error& e) {                                      \
397eaca502SStefano Zampini       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in Thrust %s",e.what());\
407eaca502SStefano Zampini     }                                                                       \
417eaca502SStefano Zampini   } while (0)
427eaca502SStefano Zampini 
43aa372e3fSPaul Mullowney #if defined(PETSC_USE_COMPLEX)
44aa372e3fSPaul Mullowney   #if defined(PETSC_USE_REAL_SINGLE)
45ccdfe979SStefano Zampini     const cuComplex PETSC_CUSPARSE_ONE        = {1.0f, 0.0f};
46ccdfe979SStefano Zampini     const cuComplex PETSC_CUSPARSE_ZERO       = {0.0f, 0.0f};
47afb2bd1cSJunchao Zhang   #elif defined(PETSC_USE_REAL_DOUBLE)
48afb2bd1cSJunchao Zhang     const cuDoubleComplex PETSC_CUSPARSE_ONE  = {1.0, 0.0};
49afb2bd1cSJunchao Zhang     const cuDoubleComplex PETSC_CUSPARSE_ZERO = {0.0, 0.0};
50afb2bd1cSJunchao Zhang   #endif
51afb2bd1cSJunchao Zhang #else
52afb2bd1cSJunchao Zhang   const PetscScalar PETSC_CUSPARSE_ONE        = 1.0;
53afb2bd1cSJunchao Zhang   const PetscScalar PETSC_CUSPARSE_ZERO       = 0.0;
54afb2bd1cSJunchao Zhang #endif
55afb2bd1cSJunchao Zhang 
561b0a6780SStefano Zampini #if PETSC_PKG_CUDA_VERSION_GE(9,0,0)
57afb2bd1cSJunchao Zhang   #define cusparse_create_analysis_info  cusparseCreateCsrsv2Info
58afb2bd1cSJunchao Zhang   #define cusparse_destroy_analysis_info cusparseDestroyCsrsv2Info
59afb2bd1cSJunchao Zhang   #if defined(PETSC_USE_COMPLEX)
60afb2bd1cSJunchao Zhang     #if defined(PETSC_USE_REAL_SINGLE)
61afb2bd1cSJunchao Zhang       #define cusparse_get_svbuffsize(a,b,c,d,e,f,g,h,i,j) cusparseCcsrsv2_bufferSize(a,b,c,d,e,(cuComplex*)(f),g,h,i,j)
62afb2bd1cSJunchao Zhang       #define cusparse_analysis(a,b,c,d,e,f,g,h,i,j,k)     cusparseCcsrsv2_analysis(a,b,c,d,e,(const cuComplex*)(f),g,h,i,j,k)
63afb2bd1cSJunchao Zhang       #define cusparse_solve(a,b,c,d,e,f,g,h,i,j,k,l,m,n)  cusparseCcsrsv2_solve(a,b,c,d,(const cuComplex*)(e),f,(const cuComplex*)(g),h,i,j,(const cuComplex*)(k),(cuComplex*)(l),m,n)
64afb2bd1cSJunchao Zhang     #elif defined(PETSC_USE_REAL_DOUBLE)
65afb2bd1cSJunchao Zhang       #define cusparse_get_svbuffsize(a,b,c,d,e,f,g,h,i,j) cusparseZcsrsv2_bufferSize(a,b,c,d,e,(cuDoubleComplex*)(f),g,h,i,j)
66afb2bd1cSJunchao Zhang       #define cusparse_analysis(a,b,c,d,e,f,g,h,i,j,k)     cusparseZcsrsv2_analysis(a,b,c,d,e,(const cuDoubleComplex*)(f),g,h,i,j,k)
67afb2bd1cSJunchao Zhang       #define cusparse_solve(a,b,c,d,e,f,g,h,i,j,k,l,m,n)  cusparseZcsrsv2_solve(a,b,c,d,(const cuDoubleComplex*)(e),f,(const cuDoubleComplex*)(g),h,i,j,(const cuDoubleComplex*)(k),(cuDoubleComplex*)(l),m,n)
68afb2bd1cSJunchao Zhang     #endif
69afb2bd1cSJunchao Zhang   #else /* not complex */
70afb2bd1cSJunchao Zhang     #if defined(PETSC_USE_REAL_SINGLE)
71afb2bd1cSJunchao Zhang       #define cusparse_get_svbuffsize cusparseScsrsv2_bufferSize
72afb2bd1cSJunchao Zhang       #define cusparse_analysis       cusparseScsrsv2_analysis
73afb2bd1cSJunchao Zhang       #define cusparse_solve          cusparseScsrsv2_solve
74afb2bd1cSJunchao Zhang     #elif defined(PETSC_USE_REAL_DOUBLE)
75afb2bd1cSJunchao Zhang       #define cusparse_get_svbuffsize cusparseDcsrsv2_bufferSize
76afb2bd1cSJunchao Zhang       #define cusparse_analysis       cusparseDcsrsv2_analysis
77afb2bd1cSJunchao Zhang       #define cusparse_solve          cusparseDcsrsv2_solve
78afb2bd1cSJunchao Zhang     #endif
79afb2bd1cSJunchao Zhang   #endif
80afb2bd1cSJunchao Zhang #else
81afb2bd1cSJunchao Zhang   #define cusparse_create_analysis_info  cusparseCreateSolveAnalysisInfo
82afb2bd1cSJunchao Zhang   #define cusparse_destroy_analysis_info cusparseDestroySolveAnalysisInfo
83afb2bd1cSJunchao Zhang   #if defined(PETSC_USE_COMPLEX)
84afb2bd1cSJunchao Zhang     #if defined(PETSC_USE_REAL_SINGLE)
85ec42abe4SAlejandro Lamas Daviña       #define cusparse_solve(a,b,c,d,e,f,g,h,i,j,k) cusparseCcsrsv_solve((a),(b),(c),(cuComplex*)(d),(e),(cuComplex*)(f),(g),(h),(i),(cuComplex*)(j),(cuComplex*)(k))
86ec42abe4SAlejandro Lamas Daviña       #define cusparse_analysis(a,b,c,d,e,f,g,h,i)  cusparseCcsrsv_analysis((a),(b),(c),(d),(e),(cuComplex*)(f),(g),(h),(i))
871b0a6780SStefano Zampini     #elif defined(PETSC_USE_REAL_DOUBLE)
881b0a6780SStefano Zampini       #define cusparse_solve(a,b,c,d,e,f,g,h,i,j,k) cusparseZcsrsv_solve((a),(b),(c),(cuDoubleComplex*)(d),(e),(cuDoubleComplex*)(f),(g),(h),(i),(cuDoubleComplex*)(j),(cuDoubleComplex*)(k))
891b0a6780SStefano Zampini       #define cusparse_analysis(a,b,c,d,e,f,g,h,i)  cusparseZcsrsv_analysis((a),(b),(c),(d),(e),(cuDoubleComplex*)(f),(g),(h),(i))
901b0a6780SStefano Zampini     #endif
911b0a6780SStefano Zampini   #else /* not complex */
921b0a6780SStefano Zampini     #if defined(PETSC_USE_REAL_SINGLE)
931b0a6780SStefano Zampini       #define cusparse_solve    cusparseScsrsv_solve
941b0a6780SStefano Zampini       #define cusparse_analysis cusparseScsrsv_analysis
951b0a6780SStefano Zampini     #elif defined(PETSC_USE_REAL_DOUBLE)
961b0a6780SStefano Zampini       #define cusparse_solve    cusparseDcsrsv_solve
971b0a6780SStefano Zampini       #define cusparse_analysis cusparseDcsrsv_analysis
981b0a6780SStefano Zampini     #endif
991b0a6780SStefano Zampini   #endif
1001b0a6780SStefano Zampini #endif
1011b0a6780SStefano Zampini 
1021b0a6780SStefano Zampini #if PETSC_PKG_CUDA_VERSION_GE(11,0,0)
1031b0a6780SStefano Zampini   #define cusparse_csr2csc cusparseCsr2cscEx2
1041b0a6780SStefano Zampini   #if defined(PETSC_USE_COMPLEX)
1051b0a6780SStefano Zampini     #if defined(PETSC_USE_REAL_SINGLE)
1061b0a6780SStefano Zampini       #define cusparse_scalartype CUDA_C_32F
107039c6fbaSStefano Zampini       #define cusparse_csr_spgeam(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t)            cusparseCcsrgeam2(a,b,c,(cuComplex*)d,e,f,(cuComplex*)g,h,i,(cuComplex*)j,k,l,(cuComplex*)m,n,o,p,(cuComplex*)q,r,s,t)
108039c6fbaSStefano Zampini       #define cusparse_csr_spgeam_bufferSize(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) cusparseCcsrgeam2_bufferSizeExt(a,b,c,(cuComplex*)d,e,f,(cuComplex*)g,h,i,(cuComplex*)j,k,l,(cuComplex*)m,n,o,p,(cuComplex*)q,r,s,t)
1091b0a6780SStefano Zampini     #elif defined(PETSC_USE_REAL_DOUBLE)
1101b0a6780SStefano Zampini       #define cusparse_scalartype CUDA_C_64F
111039c6fbaSStefano Zampini       #define cusparse_csr_spgeam(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t)            cusparseZcsrgeam2(a,b,c,(cuDoubleComplex*)d,e,f,(cuDoubleComplex*)g,h,i,(cuDoubleComplex*)j,k,l,(cuDoubleComplex*)m,n,o,p,(cuDoubleComplex*)q,r,s,t)
112039c6fbaSStefano Zampini       #define cusparse_csr_spgeam_bufferSize(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) cusparseZcsrgeam2_bufferSizeExt(a,b,c,(cuDoubleComplex*)d,e,f,(cuDoubleComplex*)g,h,i,(cuDoubleComplex*)j,k,l,(cuDoubleComplex*)m,n,o,p,(cuDoubleComplex*)q,r,s,t)
1131b0a6780SStefano Zampini     #endif
1141b0a6780SStefano Zampini   #else /* not complex */
1151b0a6780SStefano Zampini     #if defined(PETSC_USE_REAL_SINGLE)
1161b0a6780SStefano Zampini       #define cusparse_scalartype CUDA_R_32F
117039c6fbaSStefano Zampini       #define cusparse_csr_spgeam            cusparseScsrgeam2
118039c6fbaSStefano Zampini       #define cusparse_csr_spgeam_bufferSize cusparseScsrgeam2_bufferSizeExt
1191b0a6780SStefano Zampini     #elif defined(PETSC_USE_REAL_DOUBLE)
1201b0a6780SStefano Zampini       #define cusparse_scalartype CUDA_R_64F
121039c6fbaSStefano Zampini       #define cusparse_csr_spgeam            cusparseDcsrgeam2
122039c6fbaSStefano Zampini       #define cusparse_csr_spgeam_bufferSize cusparseDcsrgeam2_bufferSizeExt
1231b0a6780SStefano Zampini     #endif
1241b0a6780SStefano Zampini   #endif
1251b0a6780SStefano Zampini #else
1261b0a6780SStefano Zampini   #if defined(PETSC_USE_COMPLEX)
1271b0a6780SStefano Zampini     #if defined(PETSC_USE_REAL_SINGLE)
128ec42abe4SAlejandro Lamas Daviña       #define cusparse_csr_spmv(a,b,c,d,e,f,g,h,i,j,k,l,m)       cusparseCcsrmv((a),(b),(c),(d),(e),(cuComplex*)(f),(g),(cuComplex*)(h),(i),(j),(cuComplex*)(k),(cuComplex*)(l),(cuComplex*)(m))
129ccdfe979SStefano Zampini       #define cusparse_csr_spmm(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) cusparseCcsrmm((a),(b),(c),(d),(e),(f),(cuComplex*)(g),(h),(cuComplex*)(i),(j),(k),(cuComplex*)(l),(m),(cuComplex*)(n),(cuComplex*)(o),(p))
130ec42abe4SAlejandro Lamas Daviña       #define cusparse_csr2csc(a,b,c,d,e,f,g,h,i,j,k,l)          cusparseCcsr2csc((a),(b),(c),(d),(cuComplex*)(e),(f),(g),(cuComplex*)(h),(i),(j),(k),(l))
131ec42abe4SAlejandro Lamas Daviña       #define cusparse_hyb_spmv(a,b,c,d,e,f,g,h)                 cusparseChybmv((a),(b),(cuComplex*)(c),(d),(e),(cuComplex*)(f),(cuComplex*)(g),(cuComplex*)(h))
132ec42abe4SAlejandro Lamas Daviña       #define cusparse_csr2hyb(a,b,c,d,e,f,g,h,i,j)              cusparseCcsr2hyb((a),(b),(c),(d),(cuComplex*)(e),(f),(g),(h),(i),(j))
133ec42abe4SAlejandro Lamas Daviña       #define cusparse_hyb2csr(a,b,c,d,e,f)                      cusparseChyb2csr((a),(b),(c),(cuComplex*)(d),(e),(f))
134fcdce8c4SStefano Zampini       #define cusparse_csr_spgemm(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) cusparseCcsrgemm(a,b,c,d,e,f,g,h,(cuComplex*)i,j,k,l,m,(cuComplex*)n,o,p,q,(cuComplex*)r,s,t)
135039c6fbaSStefano Zampini       #define cusparse_csr_spgeam(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s)   cusparseCcsrgeam(a,b,c,(cuComplex*)d,e,f,(cuComplex*)g,h,i,(cuComplex*)j,k,l,(cuComplex*)m,n,o,p,(cuComplex*)q,r,s)
136aa372e3fSPaul Mullowney     #elif defined(PETSC_USE_REAL_DOUBLE)
137ec42abe4SAlejandro Lamas Daviña       #define cusparse_csr_spmv(a,b,c,d,e,f,g,h,i,j,k,l,m)       cusparseZcsrmv((a),(b),(c),(d),(e),(cuDoubleComplex*)(f),(g),(cuDoubleComplex*)(h),(i),(j),(cuDoubleComplex*)(k),(cuDoubleComplex*)(l),(cuDoubleComplex*)(m))
138ccdfe979SStefano Zampini       #define cusparse_csr_spmm(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) cusparseZcsrmm((a),(b),(c),(d),(e),(f),(cuDoubleComplex*)(g),(h),(cuDoubleComplex*)(i),(j),(k),(cuDoubleComplex*)(l),(m),(cuDoubleComplex*)(n),(cuDoubleComplex*)(o),(p))
139ec42abe4SAlejandro Lamas Daviña       #define cusparse_csr2csc(a,b,c,d,e,f,g,h,i,j,k,l)          cusparseZcsr2csc((a),(b),(c),(d),(cuDoubleComplex*)(e),(f),(g),(cuDoubleComplex*)(h),(i),(j),(k),(l))
140ec42abe4SAlejandro Lamas Daviña       #define cusparse_hyb_spmv(a,b,c,d,e,f,g,h)                 cusparseZhybmv((a),(b),(cuDoubleComplex*)(c),(d),(e),(cuDoubleComplex*)(f),(cuDoubleComplex*)(g),(cuDoubleComplex*)(h))
141ec42abe4SAlejandro Lamas Daviña       #define cusparse_csr2hyb(a,b,c,d,e,f,g,h,i,j)              cusparseZcsr2hyb((a),(b),(c),(d),(cuDoubleComplex*)(e),(f),(g),(h),(i),(j))
142ec42abe4SAlejandro Lamas Daviña       #define cusparse_hyb2csr(a,b,c,d,e,f)                      cusparseZhyb2csr((a),(b),(c),(cuDoubleComplex*)(d),(e),(f))
143fcdce8c4SStefano Zampini       #define cusparse_csr_spgemm(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) cusparseZcsrgemm(a,b,c,d,e,f,g,h,(cuDoubleComplex*)i,j,k,l,m,(cuDoubleComplex*)n,o,p,q,(cuDoubleComplex*)r,s,t)
144039c6fbaSStefano Zampini       #define cusparse_csr_spgeam(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s)   cusparseZcsrgeam(a,b,c,(cuDoubleComplex*)d,e,f,(cuDoubleComplex*)g,h,i,(cuDoubleComplex*)j,k,l,(cuDoubleComplex*)m,n,o,p,(cuDoubleComplex*)q,r,s)
145aa372e3fSPaul Mullowney     #endif
146aa372e3fSPaul Mullowney   #else
147aa372e3fSPaul Mullowney     #if defined(PETSC_USE_REAL_SINGLE)
148aa372e3fSPaul Mullowney       #define cusparse_csr_spmv cusparseScsrmv
149ccdfe979SStefano Zampini       #define cusparse_csr_spmm cusparseScsrmm
150aa372e3fSPaul Mullowney       #define cusparse_csr2csc  cusparseScsr2csc
151aa372e3fSPaul Mullowney       #define cusparse_hyb_spmv cusparseShybmv
152aa372e3fSPaul Mullowney       #define cusparse_csr2hyb  cusparseScsr2hyb
153aa372e3fSPaul Mullowney       #define cusparse_hyb2csr  cusparseShyb2csr
154fcdce8c4SStefano Zampini       #define cusparse_csr_spgemm cusparseScsrgemm
155039c6fbaSStefano Zampini       #define cusparse_csr_spgeam cusparseScsrgeam
156aa372e3fSPaul Mullowney     #elif defined(PETSC_USE_REAL_DOUBLE)
157aa372e3fSPaul Mullowney       #define cusparse_csr_spmv cusparseDcsrmv
158ccdfe979SStefano Zampini       #define cusparse_csr_spmm cusparseDcsrmm
159aa372e3fSPaul Mullowney       #define cusparse_csr2csc  cusparseDcsr2csc
160aa372e3fSPaul Mullowney       #define cusparse_hyb_spmv cusparseDhybmv
161aa372e3fSPaul Mullowney       #define cusparse_csr2hyb  cusparseDcsr2hyb
162aa372e3fSPaul Mullowney       #define cusparse_hyb2csr  cusparseDhyb2csr
163fcdce8c4SStefano Zampini       #define cusparse_csr_spgemm cusparseDcsrgemm
164039c6fbaSStefano Zampini       #define cusparse_csr_spgeam cusparseDcsrgeam
165aa372e3fSPaul Mullowney     #endif
166aa372e3fSPaul Mullowney   #endif
167afb2bd1cSJunchao Zhang #endif
168aa372e3fSPaul Mullowney 
169aa372e3fSPaul Mullowney #define THRUSTINTARRAY32 thrust::device_vector<int>
170aa372e3fSPaul Mullowney #define THRUSTINTARRAY thrust::device_vector<PetscInt>
171aa372e3fSPaul Mullowney #define THRUSTARRAY thrust::device_vector<PetscScalar>
172aa372e3fSPaul Mullowney 
173aa372e3fSPaul Mullowney /* A CSR matrix structure */
174aa372e3fSPaul Mullowney struct CsrMatrix {
175aa372e3fSPaul Mullowney   PetscInt         num_rows;
176aa372e3fSPaul Mullowney   PetscInt         num_cols;
177aa372e3fSPaul Mullowney   PetscInt         num_entries;
178aa372e3fSPaul Mullowney   THRUSTINTARRAY32 *row_offsets;
179aa372e3fSPaul Mullowney   THRUSTINTARRAY32 *column_indices;
180aa372e3fSPaul Mullowney   THRUSTARRAY      *values;
1819ae82921SPaul Mullowney };
1829ae82921SPaul Mullowney 
183aa372e3fSPaul Mullowney /* This is struct holding the relevant data needed to a MatSolve */
184aa372e3fSPaul Mullowney struct Mat_SeqAIJCUSPARSETriFactorStruct {
185aa372e3fSPaul Mullowney   /* Data needed for triangular solve */
186aa372e3fSPaul Mullowney   cusparseMatDescr_t          descr;
187aa372e3fSPaul Mullowney   cusparseOperation_t         solveOp;
188aa372e3fSPaul Mullowney   CsrMatrix                   *csrMat;
1891b0a6780SStefano Zampini  #if PETSC_PKG_CUDA_VERSION_GE(9,0,0)
190afb2bd1cSJunchao Zhang   csrsv2Info_t                solveInfo;
191afb2bd1cSJunchao Zhang  #else
192afb2bd1cSJunchao Zhang   cusparseSolveAnalysisInfo_t solveInfo;
193afb2bd1cSJunchao Zhang  #endif
1941b0a6780SStefano Zampini   cusparseSolvePolicy_t       solvePolicy;     /* whether level information is generated and used */
1951b0a6780SStefano Zampini   int                         solveBufferSize;
1961b0a6780SStefano Zampini   void                        *solveBuffer;
1971b0a6780SStefano Zampini   size_t                      csr2cscBufferSize; /* to transpose the triangular factor (only used for CUDA >= 11.0) */
1981b0a6780SStefano Zampini   void                        *csr2cscBuffer;
1992cbc15d9SMark   PetscScalar                 *AA_h; /* managed host buffer for moving values to the GPU */
200aa372e3fSPaul Mullowney };
201aa372e3fSPaul Mullowney 
202afb2bd1cSJunchao Zhang /* This is a larger struct holding all the triangular factors for a solve, transpose solve, and any indices used in a reordering */
203aa372e3fSPaul Mullowney struct Mat_SeqAIJCUSPARSETriFactors {
204aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorPtr; /* pointer for lower triangular (factored matrix) on GPU */
205aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorPtr; /* pointer for upper triangular (factored matrix) on GPU */
206aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorPtrTranspose; /* pointer for lower triangular (factored matrix) on GPU for the transpose (useful for BiCG) */
207aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorPtrTranspose; /* pointer for upper triangular (factored matrix) on GPU for the transpose (useful for BiCG)*/
208aa372e3fSPaul Mullowney   THRUSTINTARRAY                    *rpermIndices;  /* indices used for any reordering */
209aa372e3fSPaul Mullowney   THRUSTINTARRAY                    *cpermIndices;  /* indices used for any reordering */
210aa372e3fSPaul Mullowney   THRUSTARRAY                       *workVector;
211aa372e3fSPaul Mullowney   cusparseHandle_t                  handle;   /* a handle to the cusparse library */
212aa372e3fSPaul Mullowney   PetscInt                          nnz;      /* number of nonzeros ... need this for accurate logging between ICC and ILU */
213bddcd29dSMark Adams   PetscScalar                       *a_band_d; /* GPU data for banded CSR LU factorization matrix diag(L)=1 */
214bddcd29dSMark Adams   int                               *i_band_d; /* this could be optimized away */
215e8d2b73aSMark Adams   cudaDeviceProp                    dev_prop;
216e8d2b73aSMark Adams   PetscBool                         init_dev_prop;
217aa372e3fSPaul Mullowney };
218aa372e3fSPaul Mullowney 
219afb2bd1cSJunchao Zhang struct Mat_CusparseSpMV {
220afb2bd1cSJunchao Zhang   PetscBool             initialized;    /* Don't rely on spmvBuffer != NULL to test if the struct is initialized, */
221afb2bd1cSJunchao Zhang   size_t                spmvBufferSize; /* since I'm not sure if smvBuffer can be NULL even after cusparseSpMV_bufferSize() */
222afb2bd1cSJunchao Zhang   void                  *spmvBuffer;
2239db3cbf9SStefano Zampini  #if PETSC_PKG_CUDA_VERSION_GE(11,0,0)  /* these are present from CUDA 10.1, but PETSc code makes use of them from CUDA 11 on */
224afb2bd1cSJunchao Zhang   cusparseDnVecDescr_t  vecXDescr,vecYDescr; /* descriptor for the dense vectors in y=op(A)x */
2259db3cbf9SStefano Zampini  #endif
226afb2bd1cSJunchao Zhang };
227afb2bd1cSJunchao Zhang 
228afb2bd1cSJunchao Zhang /* This is struct holding the relevant data needed to a MatMult */
229afb2bd1cSJunchao Zhang struct Mat_SeqAIJCUSPARSEMultStruct {
230afb2bd1cSJunchao Zhang   void               *mat;  /* opaque pointer to a matrix. This could be either a cusparseHybMat_t or a CsrMatrix */
231afb2bd1cSJunchao Zhang   cusparseMatDescr_t descr; /* Data needed to describe the matrix for a multiply */
232afb2bd1cSJunchao Zhang   THRUSTINTARRAY     *cprowIndices;   /* compressed row indices used in the parallel SpMV */
233afb2bd1cSJunchao Zhang   PetscScalar        *alpha_one; /* pointer to a device "scalar" storing the alpha parameter in the SpMV */
234afb2bd1cSJunchao Zhang   PetscScalar        *beta_zero; /* pointer to a device "scalar" storing the beta parameter in the SpMV as zero*/
235afb2bd1cSJunchao Zhang   PetscScalar        *beta_one; /* pointer to a device "scalar" storing the beta parameter in the SpMV as one */
236afb2bd1cSJunchao Zhang  #if PETSC_PKG_CUDA_VERSION_GE(11,0,0)
237afb2bd1cSJunchao Zhang   cusparseSpMatDescr_t  matDescr;  /* descriptor for the matrix, used by SpMV and SpMM */
238afb2bd1cSJunchao Zhang   Mat_CusparseSpMV      cuSpMV[3]; /* different Mat_CusparseSpMV structs for non-transpose, transpose, conj-transpose */
239afb2bd1cSJunchao Zhang   Mat_SeqAIJCUSPARSEMultStruct() : matDescr(NULL) {
240afb2bd1cSJunchao Zhang     for (int i=0; i<3; i++) cuSpMV[i].initialized = PETSC_FALSE;
241afb2bd1cSJunchao Zhang   }
242afb2bd1cSJunchao Zhang  #endif
243afb2bd1cSJunchao Zhang };
244afb2bd1cSJunchao Zhang 
245aa372e3fSPaul Mullowney /* This is a larger struct holding all the matrices for a SpMV, and SpMV Tranpose */
2469ae82921SPaul Mullowney struct Mat_SeqAIJCUSPARSE {
247aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *mat;            /* pointer to the matrix on the GPU */
248aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *matTranspose;   /* pointer to the matrix on the GPU (for the transpose ... useful for BiCG) */
249aa372e3fSPaul Mullowney   THRUSTARRAY                  *workVector;     /* pointer to a workvector to which we can copy the relevant indices of a vector we want to multiply */
250029808eeSJunchao Zhang   THRUSTINTARRAY32             *rowoffsets_gpu; /* rowoffsets on GPU in non-compressed-row format. It is used to convert CSR to CSC */
251213423ffSJunchao Zhang   PetscInt                     nrows;           /* number of rows of the matrix seen by GPU */
252e057df02SPaul Mullowney   MatCUSPARSEStorageFormat     format;          /* the storage format for the matrix on the device */
253aa372e3fSPaul Mullowney   cudaStream_t                 stream;          /* a stream for the parallel SpMV ... this is not owned and should not be deleted */
254aa372e3fSPaul Mullowney   cusparseHandle_t             handle;          /* a handle to the cusparse library ... this may not be owned (if we're working in parallel i.e. multiGPUs) */
25554da937aSStefano Zampini   PetscObjectState             nonzerostate;    /* track nonzero state to possibly recreate the GPU matrix */
256afb2bd1cSJunchao Zhang  #if PETSC_PKG_CUDA_VERSION_GE(11,0,0)
257afb2bd1cSJunchao Zhang   size_t                       csr2cscBufferSize; /* stuff used to compute the matTranspose above */
258afb2bd1cSJunchao Zhang   void                         *csr2cscBuffer;    /* This is used as a C struct and is calloc'ed by PetscNewLog() */
259afb2bd1cSJunchao Zhang   cusparseCsr2CscAlg_t         csr2cscAlg;        /* algorithms can be selected from command line options */
260afb2bd1cSJunchao Zhang   cusparseSpMVAlg_t            spmvAlg;
261afb2bd1cSJunchao Zhang   cusparseSpMMAlg_t            spmmAlg;
262afb2bd1cSJunchao Zhang  #endif
263a49f1ed0SStefano Zampini   THRUSTINTARRAY               *csr2csc_i;
264042217e8SBarry Smith   PetscSplitCSRDataStructure   deviceMat;       /* Matrix on device for, eg, assembly */
265*ddea5d60SJunchao Zhang   THRUSTINTARRAY               *cooPerm;        /* permutation array that sorts the input coo entris by row and col */
266*ddea5d60SJunchao Zhang   THRUSTINTARRAY               *cooPerm_a;      /* ordered array that indicate i-th nonzero (after sorting) is the j-th unique nonzero */
2679ae82921SPaul Mullowney };
2689ae82921SPaul Mullowney 
2695a576424SJed Brown PETSC_INTERN PetscErrorCode MatCUSPARSECopyToGPU(Mat);
270b06137fdSPaul Mullowney PETSC_INTERN PetscErrorCode MatCUSPARSESetStream(Mat, const cudaStream_t stream);
271b06137fdSPaul Mullowney PETSC_INTERN PetscErrorCode MatCUSPARSESetHandle(Mat, const cusparseHandle_t handle);
272b06137fdSPaul Mullowney PETSC_INTERN PetscErrorCode MatCUSPARSEClearHandle(Mat);
273e61fc153SStefano Zampini PETSC_INTERN PetscErrorCode MatSetPreallocationCOO_SeqAIJCUSPARSE(Mat,PetscInt,const PetscInt[],const PetscInt[]);
274e61fc153SStefano Zampini PETSC_INTERN PetscErrorCode MatSetValuesCOO_SeqAIJCUSPARSE(Mat,const PetscScalar[],InsertMode);
275ed502f03SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSEGetArrayRead(Mat,const PetscScalar**);
276ed502f03SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSERestoreArrayRead(Mat,const PetscScalar**);
277ed502f03SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSEGetArrayWrite(Mat,PetscScalar**);
278ed502f03SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSERestoreArrayWrite(Mat,PetscScalar**);
279039c6fbaSStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSEGetArray(Mat,PetscScalar**);
280039c6fbaSStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSERestoreArray(Mat,PetscScalar**);
281ed502f03SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSEMergeMats(Mat,Mat,MatReuse,Mat*);
282ed502f03SStefano Zampini 
28308391a17SStefano Zampini PETSC_STATIC_INLINE bool isCudaMem(const void *data)
28408391a17SStefano Zampini {
28508391a17SStefano Zampini   cudaError_t                  cerr;
28608391a17SStefano Zampini   struct cudaPointerAttributes attr;
28708391a17SStefano Zampini   enum cudaMemoryType          mtype;
28808391a17SStefano Zampini   cerr = cudaPointerGetAttributes(&attr,data); /* Do not check error since before CUDA 11.0, passing a host pointer returns cudaErrorInvalidValue */
28908391a17SStefano Zampini   cudaGetLastError(); /* Reset the last error */
29008391a17SStefano Zampini   #if (CUDART_VERSION < 10000)
29108391a17SStefano Zampini     mtype = attr.memoryType;
29208391a17SStefano Zampini   #else
29308391a17SStefano Zampini     mtype = attr.type;
29408391a17SStefano Zampini   #endif
29508391a17SStefano Zampini   if (cerr == cudaSuccess && mtype == cudaMemoryTypeDevice) return true;
29608391a17SStefano Zampini   else return false;
29708391a17SStefano Zampini }
29808391a17SStefano Zampini 
2999ae82921SPaul Mullowney #endif
300