xref: /petsc/src/mat/impls/aij/seq/seqcusparse/cusparsematimpl.h (revision cd871708d6ae82bd70cc1a9e2138f9b57839fe75)
1a4963045SJacob Faibussowitsch #pragma once
29ae82921SPaul Mullowney 
3afb2bd1cSJunchao Zhang #include <petscpkg_version.h>
46d54fb17SJacob Faibussowitsch #include <../src/vec/vec/impls/seq/cupm/vecseqcupm.hpp> /* for VecSeq_CUPM */
547f8145dSJacob Faibussowitsch #include <../src/sys/objects/device/impls/cupm/cupmthrustutility.hpp>
66d54fb17SJacob Faibussowitsch #include <petsc/private/petsclegacycupmblas.h>
79ae82921SPaul Mullowney 
89ae82921SPaul Mullowney #include <algorithm>
99ae82921SPaul Mullowney #include <vector>
109ae82921SPaul Mullowney 
11c41cb2e2SAlejandro Lamas Daviña #include <thrust/device_vector.h>
12c41cb2e2SAlejandro Lamas Daviña #include <thrust/device_ptr.h>
1350ab121bSSatish Balay #include <thrust/device_malloc_allocator.h>
14c41cb2e2SAlejandro Lamas Daviña #include <thrust/transform.h>
15c41cb2e2SAlejandro Lamas Daviña #include <thrust/functional.h>
16554b8892SKarl Rupp #include <thrust/sequence.h>
177eaca502SStefano Zampini #include <thrust/system/system_error.h>
18c41cb2e2SAlejandro Lamas Daviña 
19aa372e3fSPaul Mullowney #if defined(PETSC_USE_COMPLEX)
20aa372e3fSPaul Mullowney   #if defined(PETSC_USE_REAL_SINGLE)
21ccdfe979SStefano Zampini const cuComplex PETSC_CUSPARSE_ONE  = {1.0f, 0.0f};
22ccdfe979SStefano Zampini const cuComplex PETSC_CUSPARSE_ZERO = {0.0f, 0.0f};
23da112707SJunchao Zhang     #define cusparseXcsrilu02_bufferSize(a, b, c, d, e, f, g, h, i)  cusparseCcsrilu02_bufferSize(a, b, c, d, (cuComplex *)e, f, g, h, i)
24da112707SJunchao Zhang     #define cusparseXcsrilu02_analysis(a, b, c, d, e, f, g, h, i, j) cusparseCcsrilu02_analysis(a, b, c, d, (cuComplex *)e, f, g, h, i, j)
25da112707SJunchao Zhang     #define cusparseXcsrilu02(a, b, c, d, e, f, g, h, i, j)          cusparseCcsrilu02(a, b, c, d, (cuComplex *)e, f, g, h, i, j)
26da112707SJunchao Zhang     #define cusparseXcsric02_bufferSize(a, b, c, d, e, f, g, h, i)   cusparseCcsric02_bufferSize(a, b, c, d, (cuComplex *)e, f, g, h, i)
27da112707SJunchao Zhang     #define cusparseXcsric02_analysis(a, b, c, d, e, f, g, h, i, j)  cusparseCcsric02_analysis(a, b, c, d, (cuComplex *)e, f, g, h, i, j)
28da112707SJunchao Zhang     #define cusparseXcsric02(a, b, c, d, e, f, g, h, i, j)           cusparseCcsric02(a, b, c, d, (cuComplex *)e, f, g, h, i, j)
29afb2bd1cSJunchao Zhang   #elif defined(PETSC_USE_REAL_DOUBLE)
30afb2bd1cSJunchao Zhang const cuDoubleComplex PETSC_CUSPARSE_ONE  = {1.0, 0.0};
31afb2bd1cSJunchao Zhang const cuDoubleComplex PETSC_CUSPARSE_ZERO = {0.0, 0.0};
32da112707SJunchao Zhang     #define cusparseXcsrilu02_bufferSize(a, b, c, d, e, f, g, h, i)  cusparseZcsrilu02_bufferSize(a, b, c, d, (cuDoubleComplex *)e, f, g, h, i)
33da112707SJunchao Zhang     #define cusparseXcsrilu02_analysis(a, b, c, d, e, f, g, h, i, j) cusparseZcsrilu02_analysis(a, b, c, d, (cuDoubleComplex *)e, f, g, h, i, j)
34da112707SJunchao Zhang     #define cusparseXcsrilu02(a, b, c, d, e, f, g, h, i, j)          cusparseZcsrilu02(a, b, c, d, (cuDoubleComplex *)e, f, g, h, i, j)
35da112707SJunchao Zhang     #define cusparseXcsric02_bufferSize(a, b, c, d, e, f, g, h, i)   cusparseZcsric02_bufferSize(a, b, c, d, (cuDoubleComplex *)e, f, g, h, i)
36da112707SJunchao Zhang     #define cusparseXcsric02_analysis(a, b, c, d, e, f, g, h, i, j)  cusparseZcsric02_analysis(a, b, c, d, (cuDoubleComplex *)e, f, g, h, i, j)
37da112707SJunchao Zhang     #define cusparseXcsric02(a, b, c, d, e, f, g, h, i, j)           cusparseZcsric02(a, b, c, d, (cuDoubleComplex *)e, f, g, h, i, j)
38afb2bd1cSJunchao Zhang   #endif
39afb2bd1cSJunchao Zhang #else
40afb2bd1cSJunchao Zhang const PetscScalar PETSC_CUSPARSE_ONE  = 1.0;
41afb2bd1cSJunchao Zhang const PetscScalar PETSC_CUSPARSE_ZERO = 0.0;
42da112707SJunchao Zhang   #if defined(PETSC_USE_REAL_SINGLE)
43da112707SJunchao Zhang     #define cusparseXcsrilu02_bufferSize cusparseScsrilu02_bufferSize
44da112707SJunchao Zhang     #define cusparseXcsrilu02_analysis   cusparseScsrilu02_analysis
45da112707SJunchao Zhang     #define cusparseXcsrilu02            cusparseScsrilu02
46da112707SJunchao Zhang     #define cusparseXcsric02_bufferSize  cusparseScsric02_bufferSize
47da112707SJunchao Zhang     #define cusparseXcsric02_analysis    cusparseScsric02_analysis
48da112707SJunchao Zhang     #define cusparseXcsric02             cusparseScsric02
49da112707SJunchao Zhang   #elif defined(PETSC_USE_REAL_DOUBLE)
50da112707SJunchao Zhang     #define cusparseXcsrilu02_bufferSize cusparseDcsrilu02_bufferSize
51da112707SJunchao Zhang     #define cusparseXcsrilu02_analysis   cusparseDcsrilu02_analysis
52da112707SJunchao Zhang     #define cusparseXcsrilu02            cusparseDcsrilu02
53da112707SJunchao Zhang     #define cusparseXcsric02_bufferSize  cusparseDcsric02_bufferSize
54da112707SJunchao Zhang     #define cusparseXcsric02_analysis    cusparseDcsric02_analysis
55da112707SJunchao Zhang     #define cusparseXcsric02             cusparseDcsric02
56da112707SJunchao Zhang   #endif
57afb2bd1cSJunchao Zhang #endif
58afb2bd1cSJunchao Zhang 
591b0a6780SStefano Zampini #if PETSC_PKG_CUDA_VERSION_GE(9, 0, 0)
60261a78b4SJunchao Zhang   #define csrsvInfo_t              csrsv2Info_t
61261a78b4SJunchao Zhang   #define cusparseCreateCsrsvInfo  cusparseCreateCsrsv2Info
62261a78b4SJunchao Zhang   #define cusparseDestroyCsrsvInfo cusparseDestroyCsrsv2Info
63afb2bd1cSJunchao Zhang   #if defined(PETSC_USE_COMPLEX)
64afb2bd1cSJunchao Zhang     #if defined(PETSC_USE_REAL_SINGLE)
65261a78b4SJunchao Zhang       #define cusparseXcsrsv_buffsize(a, b, c, d, e, f, g, h, i, j)          cusparseCcsrsv2_bufferSize(a, b, c, d, e, (cuComplex *)(f), g, h, i, j)
66261a78b4SJunchao Zhang       #define cusparseXcsrsv_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)
67261a78b4SJunchao Zhang       #define cusparseXcsrsv_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)
68afb2bd1cSJunchao Zhang     #elif defined(PETSC_USE_REAL_DOUBLE)
69261a78b4SJunchao Zhang       #define cusparseXcsrsv_buffsize(a, b, c, d, e, f, g, h, i, j)          cusparseZcsrsv2_bufferSize(a, b, c, d, e, (cuDoubleComplex *)(f), g, h, i, j)
70261a78b4SJunchao Zhang       #define cusparseXcsrsv_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)
71261a78b4SJunchao Zhang       #define cusparseXcsrsv_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)
72afb2bd1cSJunchao Zhang     #endif
73afb2bd1cSJunchao Zhang   #else /* not complex */
74afb2bd1cSJunchao Zhang     #if defined(PETSC_USE_REAL_SINGLE)
75261a78b4SJunchao Zhang       #define cusparseXcsrsv_buffsize cusparseScsrsv2_bufferSize
76261a78b4SJunchao Zhang       #define cusparseXcsrsv_analysis cusparseScsrsv2_analysis
77261a78b4SJunchao Zhang       #define cusparseXcsrsv_solve    cusparseScsrsv2_solve
78afb2bd1cSJunchao Zhang     #elif defined(PETSC_USE_REAL_DOUBLE)
79261a78b4SJunchao Zhang       #define cusparseXcsrsv_buffsize cusparseDcsrsv2_bufferSize
80261a78b4SJunchao Zhang       #define cusparseXcsrsv_analysis cusparseDcsrsv2_analysis
81261a78b4SJunchao Zhang       #define cusparseXcsrsv_solve    cusparseDcsrsv2_solve
82afb2bd1cSJunchao Zhang     #endif
83afb2bd1cSJunchao Zhang   #endif
849f7ba44dSJacob Faibussowitsch #else /* PETSC_PKG_CUDA_VERSION_GE(9, 0, 0) */
85261a78b4SJunchao Zhang   #define csrsvInfo_t              cusparseSolveAnalysisInfo_t
86261a78b4SJunchao Zhang   #define cusparseCreateCsrsvInfo  cusparseCreateSolveAnalysisInfo
87261a78b4SJunchao Zhang   #define cusparseDestroyCsrsvInfo cusparseDestroySolveAnalysisInfo
88afb2bd1cSJunchao Zhang   #if defined(PETSC_USE_COMPLEX)
89afb2bd1cSJunchao Zhang     #if defined(PETSC_USE_REAL_SINGLE)
909f7ba44dSJacob Faibussowitsch       #define cusparseXcsrsv_solve(a, b, c, d_IGNORED, e, f, g, h, i, j, k, l, m_IGNORED, n_IGNORED) cusparseCcsrsv_solve((a), (b), (c), (cuComplex *)(e), (f), (cuComplex *)(g), (h), (i), (j), (cuComplex *)(k), (cuComplex *)(l))
919f7ba44dSJacob Faibussowitsch       #define cusparseXcsrsv_analysis(a, b, c, d, e, f, g, h, i, j_IGNORED, k_IGNORED)               cusparseCcsrsv_analysis((a), (b), (c), (d), (e), (cuComplex *)(f), (g), (h), (i))
921b0a6780SStefano Zampini     #elif defined(PETSC_USE_REAL_DOUBLE)
939f7ba44dSJacob Faibussowitsch       #define cusparseXcsrsv_solve(a, b, c, d_IGNORED, e, f, g, h, i, j, k, l, m_IGNORED, n_IGNORED) \
949f7ba44dSJacob Faibussowitsch         cusparseZcsrsv_solve((a), (b), (c), (cuDoubleComplex *)(e), (f), (cuDoubleComplex *)(g), (h), (i), (j), (cuDoubleComplex *)(k), (cuDoubleComplex *)(l))
959f7ba44dSJacob Faibussowitsch       #define cusparseXcsrsv_analysis(a, b, c, d, e, f, g, h, i, j_IGNORED, k_IGNORED) cusparseZcsrsv_analysis((a), (b), (c), (d), (e), (cuDoubleComplex *)(f), (g), (h), (i))
961b0a6780SStefano Zampini     #endif
971b0a6780SStefano Zampini   #else /* not complex */
981b0a6780SStefano Zampini     #if defined(PETSC_USE_REAL_SINGLE)
99261a78b4SJunchao Zhang       #define cusparseXcsrsv_solve                                     cusparseScsrsv_solve
1009f7ba44dSJacob Faibussowitsch       #define cusparseXcsrsv_analysis(a, b, c, d, e, f, g, h, i, j, k) cusparseScsrsv_analysis(a, b, c, d, e, f, g, h, i)
1011b0a6780SStefano Zampini     #elif defined(PETSC_USE_REAL_DOUBLE)
102261a78b4SJunchao Zhang       #define cusparseXcsrsv_solve                                     cusparseDcsrsv_solve
1039f7ba44dSJacob Faibussowitsch       #define cusparseXcsrsv_analysis(a, b, c, d, e, f, g, h, i, j, k) cusparseDcsrsv_analysis(a, b, c, d, e, f, g, h, i)
1041b0a6780SStefano Zampini     #endif
1051b0a6780SStefano Zampini   #endif
1069f7ba44dSJacob Faibussowitsch #endif /* PETSC_PKG_CUDA_VERSION_GE(9, 0, 0) */
1071b0a6780SStefano Zampini 
1081b0a6780SStefano Zampini #if PETSC_PKG_CUDA_VERSION_GE(11, 0, 0)
1091b0a6780SStefano Zampini   #define cusparse_csr2csc cusparseCsr2cscEx2
1101b0a6780SStefano Zampini   #if defined(PETSC_USE_COMPLEX)
1111b0a6780SStefano Zampini     #if defined(PETSC_USE_REAL_SINGLE)
1121b0a6780SStefano Zampini       #define cusparse_scalartype                                                             CUDA_C_32F
113039c6fbaSStefano 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)
1149371c9d4SSatish Balay       #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) \
1159371c9d4SSatish Balay         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)
1161b0a6780SStefano Zampini     #elif defined(PETSC_USE_REAL_DOUBLE)
1171b0a6780SStefano Zampini       #define cusparse_scalartype CUDA_C_64F
1189371c9d4SSatish Balay       #define cusparse_csr_spgeam(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t) \
1199371c9d4SSatish Balay         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)
1209371c9d4SSatish Balay       #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) \
1219371c9d4SSatish Balay         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)
1221b0a6780SStefano Zampini     #endif
1231b0a6780SStefano Zampini   #else /* not complex */
1241b0a6780SStefano Zampini     #if defined(PETSC_USE_REAL_SINGLE)
1251b0a6780SStefano Zampini       #define cusparse_scalartype            CUDA_R_32F
126039c6fbaSStefano Zampini       #define cusparse_csr_spgeam            cusparseScsrgeam2
127039c6fbaSStefano Zampini       #define cusparse_csr_spgeam_bufferSize cusparseScsrgeam2_bufferSizeExt
1281b0a6780SStefano Zampini     #elif defined(PETSC_USE_REAL_DOUBLE)
1291b0a6780SStefano Zampini       #define cusparse_scalartype            CUDA_R_64F
130039c6fbaSStefano Zampini       #define cusparse_csr_spgeam            cusparseDcsrgeam2
131039c6fbaSStefano Zampini       #define cusparse_csr_spgeam_bufferSize cusparseDcsrgeam2_bufferSizeExt
1321b0a6780SStefano Zampini     #endif
1331b0a6780SStefano Zampini   #endif
1349f7ba44dSJacob Faibussowitsch #else /* PETSC_PKG_CUDA_VERSION_GE(11, 0, 0) */
1351b0a6780SStefano Zampini   #if defined(PETSC_USE_COMPLEX)
1361b0a6780SStefano Zampini     #if defined(PETSC_USE_REAL_SINGLE)
137ec42abe4SAlejandro 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))
138ccdfe979SStefano 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))
139ec42abe4SAlejandro 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))
140ec42abe4SAlejandro 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))
141ec42abe4SAlejandro 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))
142ec42abe4SAlejandro Lamas Daviña       #define cusparse_hyb2csr(a, b, c, d, e, f)                                              cusparseChyb2csr((a), (b), (c), (cuComplex *)(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) cusparseCcsrgemm(a, b, c, d, e, f, g, h, (cuComplex *)i, j, k, l, m, (cuComplex *)n, o, p, q, (cuComplex *)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)    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)
145aa372e3fSPaul Mullowney     #elif defined(PETSC_USE_REAL_DOUBLE)
146ec42abe4SAlejandro 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))
1479371c9d4SSatish Balay       #define cusparse_csr_spmm(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
1489371c9d4SSatish Balay         cusparseZcsrmm((a), (b), (c), (d), (e), (f), (cuDoubleComplex *)(g), (h), (cuDoubleComplex *)(i), (j), (k), (cuDoubleComplex *)(l), (m), (cuDoubleComplex *)(n), (cuDoubleComplex *)(o), (p))
149ec42abe4SAlejandro 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))
150ec42abe4SAlejandro 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))
151ec42abe4SAlejandro 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))
152ec42abe4SAlejandro Lamas Daviña       #define cusparse_hyb2csr(a, b, c, d, e, f)                                              cusparseZhyb2csr((a), (b), (c), (cuDoubleComplex *)(d), (e), (f))
153fcdce8c4SStefano 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)
1549371c9d4SSatish Balay       #define cusparse_csr_spgeam(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s) \
1559371c9d4SSatish Balay         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)
156aa372e3fSPaul Mullowney     #endif
157aa372e3fSPaul Mullowney   #else
158aa372e3fSPaul Mullowney     #if defined(PETSC_USE_REAL_SINGLE)
159aa372e3fSPaul Mullowney       #define cusparse_csr_spmv   cusparseScsrmv
160ccdfe979SStefano Zampini       #define cusparse_csr_spmm   cusparseScsrmm
161aa372e3fSPaul Mullowney       #define cusparse_csr2csc    cusparseScsr2csc
162aa372e3fSPaul Mullowney       #define cusparse_hyb_spmv   cusparseShybmv
163aa372e3fSPaul Mullowney       #define cusparse_csr2hyb    cusparseScsr2hyb
164aa372e3fSPaul Mullowney       #define cusparse_hyb2csr    cusparseShyb2csr
165fcdce8c4SStefano Zampini       #define cusparse_csr_spgemm cusparseScsrgemm
166039c6fbaSStefano Zampini       #define cusparse_csr_spgeam cusparseScsrgeam
167aa372e3fSPaul Mullowney     #elif defined(PETSC_USE_REAL_DOUBLE)
168aa372e3fSPaul Mullowney       #define cusparse_csr_spmv   cusparseDcsrmv
169ccdfe979SStefano Zampini       #define cusparse_csr_spmm   cusparseDcsrmm
170aa372e3fSPaul Mullowney       #define cusparse_csr2csc    cusparseDcsr2csc
171aa372e3fSPaul Mullowney       #define cusparse_hyb_spmv   cusparseDhybmv
172aa372e3fSPaul Mullowney       #define cusparse_csr2hyb    cusparseDcsr2hyb
173aa372e3fSPaul Mullowney       #define cusparse_hyb2csr    cusparseDhyb2csr
174fcdce8c4SStefano Zampini       #define cusparse_csr_spgemm cusparseDcsrgemm
175039c6fbaSStefano Zampini       #define cusparse_csr_spgeam cusparseDcsrgeam
176aa372e3fSPaul Mullowney     #endif
177aa372e3fSPaul Mullowney   #endif
1789f7ba44dSJacob Faibussowitsch #endif /* PETSC_PKG_CUDA_VERSION_GE(11, 0, 0) */
179aa372e3fSPaul Mullowney 
180aa372e3fSPaul Mullowney #define THRUSTINTARRAY32 thrust::device_vector<int>
181aa372e3fSPaul Mullowney #define THRUSTINTARRAY   thrust::device_vector<PetscInt>
182aa372e3fSPaul Mullowney #define THRUSTARRAY      thrust::device_vector<PetscScalar>
183aa372e3fSPaul Mullowney 
1840b4b7b1cSBarry Smith /* A CSR matrix nonzero structure */
185aa372e3fSPaul Mullowney struct CsrMatrix {
186aa372e3fSPaul Mullowney   PetscInt          num_rows;
187aa372e3fSPaul Mullowney   PetscInt          num_cols;
188aa372e3fSPaul Mullowney   PetscInt          num_entries;
189aa372e3fSPaul Mullowney   THRUSTINTARRAY32 *row_offsets;
190aa372e3fSPaul Mullowney   THRUSTINTARRAY32 *column_indices;
191aa372e3fSPaul Mullowney   THRUSTARRAY      *values;
1929ae82921SPaul Mullowney };
1939ae82921SPaul Mullowney 
194aa372e3fSPaul Mullowney /* This is struct holding the relevant data needed to a MatSolve */
195aa372e3fSPaul Mullowney struct Mat_SeqAIJCUSPARSETriFactorStruct {
196aa372e3fSPaul Mullowney   /* Data needed for triangular solve */
197aa372e3fSPaul Mullowney   cusparseMatDescr_t  descr;
198aa372e3fSPaul Mullowney   cusparseOperation_t solveOp;
199aa372e3fSPaul Mullowney   CsrMatrix          *csrMat;
200b917901dSJunchao Zhang #if PETSC_PKG_CUDA_VERSION_LT(11, 4, 0)
201261a78b4SJunchao Zhang   csrsvInfo_t           solveInfo;
2021b0a6780SStefano Zampini   cusparseSolvePolicy_t solvePolicy; /* whether level information is generated and used */
203d460d7bfSJunchao Zhang #endif
2041b0a6780SStefano Zampini   int          solveBufferSize;
2051b0a6780SStefano Zampini   void        *solveBuffer;
2061b0a6780SStefano Zampini   size_t       csr2cscBufferSize; /* to transpose the triangular factor (only used for CUDA >= 11.0) */
2071b0a6780SStefano Zampini   void        *csr2cscBuffer;
2082cbc15d9SMark   PetscScalar *AA_h; /* managed host buffer for moving values to the GPU */
209aa372e3fSPaul Mullowney };
210aa372e3fSPaul Mullowney 
211afb2bd1cSJunchao Zhang /* This is a larger struct holding all the triangular factors for a solve, transpose solve, and any indices used in a reordering */
212aa372e3fSPaul Mullowney struct Mat_SeqAIJCUSPARSETriFactors {
213b917901dSJunchao Zhang #if PETSC_PKG_CUDA_VERSION_LT(11, 4, 0)
214aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorPtr;          /* pointer for lower triangular (factored matrix) on GPU */
215aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorPtr;          /* pointer for upper triangular (factored matrix) on GPU */
216aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorPtrTranspose; /* pointer for lower triangular (factored matrix) on GPU for the transpose (useful for BiCG) */
217aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorPtrTranspose; /* pointer for upper triangular (factored matrix) on GPU for the transpose (useful for BiCG)*/
218d460d7bfSJunchao Zhang #endif
219d460d7bfSJunchao Zhang 
220aa372e3fSPaul Mullowney   THRUSTINTARRAY  *rpermIndices; /* indices used for any reordering */
221aa372e3fSPaul Mullowney   THRUSTINTARRAY  *cpermIndices; /* indices used for any reordering */
222aa372e3fSPaul Mullowney   THRUSTARRAY     *workVector;
223aa372e3fSPaul Mullowney   cusparseHandle_t handle; /* a handle to the cusparse library */
224aa372e3fSPaul Mullowney   PetscInt         nnz;    /* number of nonzeros ... need this for accurate logging between ICC and ILU */
225e8d2b73aSMark Adams   cudaDeviceProp   dev_prop;
226e8d2b73aSMark Adams   PetscBool        init_dev_prop;
227da112707SJunchao Zhang 
228b917901dSJunchao Zhang #if PETSC_PKG_CUDA_VERSION_GE(11, 4, 0)
229da112707SJunchao Zhang   /* csrilu0/csric0 appeared in cusparse-8.0, but we use it along with cusparseSpSV,
230da112707SJunchao Zhang      which first appeared in cusparse-11.5 with cuda-11.3.
231da112707SJunchao Zhang   */
232d460d7bfSJunchao Zhang   PetscScalar *csrVal, *diag;             // the diagonal D in UtDU of Cholesky
233d460d7bfSJunchao Zhang   int         *csrRowPtr32, *csrColIdx32; // i,j of M. cusparseScsrilu02/ic02() etc require 32-bit indices
234d460d7bfSJunchao Zhang 
235d460d7bfSJunchao Zhang   PetscInt    *csrRowPtr, *csrColIdx; // i, j of M on device for CUDA APIs that support 64-bit indices
236d460d7bfSJunchao Zhang   PetscScalar *csrVal_h, *diag_h;     // Since LU is done on host, we prepare a factored matrix in regular csr format on host and then copy it to device
237d460d7bfSJunchao Zhang   PetscInt    *csrRowPtr_h;           // csrColIdx_h is temporary, so it is not here
238da112707SJunchao Zhang 
239da112707SJunchao Zhang   /* Mixed mat descriptor types? yes, different cusparse APIs use different types */
240da112707SJunchao Zhang   cusparseMatDescr_t   matDescr_M;
241da112707SJunchao Zhang   cusparseSpMatDescr_t spMatDescr_L, spMatDescr_U;
242da112707SJunchao Zhang   cusparseSpSVDescr_t  spsvDescr_L, spsvDescr_Lt, spsvDescr_U, spsvDescr_Ut;
243da112707SJunchao Zhang 
244da112707SJunchao Zhang   cusparseDnVecDescr_t dnVecDescr_X, dnVecDescr_Y;
245da112707SJunchao Zhang   PetscScalar         *X, *Y; /* data array of dnVec X and Y */
246da112707SJunchao Zhang 
247da112707SJunchao Zhang   /* Mixed size types? yes, CUDA-11.7.0 declared cusparseDcsrilu02_bufferSizeExt() that returns size_t but did not implement it! */
24812ba2bc6SJunchao Zhang   int    factBufferSize_M; /* M ~= LU or LLt */
249da112707SJunchao Zhang   size_t spsvBufferSize_L, spsvBufferSize_Lt, spsvBufferSize_U, spsvBufferSize_Ut;
25012ba2bc6SJunchao Zhang   /* cusparse needs various buffers for factorization and solve of L, U, Lt, or Ut.
25112ba2bc6SJunchao Zhang      So save memory, we share the factorization buffer with one of spsvBuffer_L/U.
25212ba2bc6SJunchao Zhang   */
25312ba2bc6SJunchao Zhang   void *factBuffer_M, *spsvBuffer_L, *spsvBuffer_U, *spsvBuffer_Lt, *spsvBuffer_Ut;
254da112707SJunchao Zhang 
255da112707SJunchao Zhang   csrilu02Info_t        ilu0Info_M;
256da112707SJunchao Zhang   csric02Info_t         ic0Info_M;
257da112707SJunchao Zhang   int                   structural_zero, numerical_zero;
258da112707SJunchao Zhang   cusparseSolvePolicy_t policy_M;
259da112707SJunchao Zhang 
26012ba2bc6SJunchao Zhang   /* In MatSolveTranspose() for ILU0, we use the two flags to do on-demand solve */
26112ba2bc6SJunchao Zhang   PetscBool createdTransposeSpSVDescr;    /* Have we created SpSV descriptors for Lt, Ut? */
262*204a0e31SJunchao Zhang   PetscBool updatedTransposeSpSVAnalysis; /* Have we ever updated (done) SpSV analysis for Lt, Ut */
263*204a0e31SJunchao Zhang   PetscBool updatedSpSVAnalysis;          /* Have we ever updated (done) SpSV Analysis for L, U? */
264da112707SJunchao Zhang 
265da112707SJunchao Zhang   PetscLogDouble numericFactFlops; /* Estimated FLOPs in ILU0/ICC0 numeric factorization */
266da112707SJunchao Zhang #endif
267aa372e3fSPaul Mullowney };
268aa372e3fSPaul Mullowney 
269afb2bd1cSJunchao Zhang struct Mat_CusparseSpMV {
270afb2bd1cSJunchao Zhang   PetscBool initialized;    /* Don't rely on spmvBuffer != NULL to test if the struct is initialized, */
271afb2bd1cSJunchao Zhang   size_t    spmvBufferSize; /* since I'm not sure if smvBuffer can be NULL even after cusparseSpMV_bufferSize() */
272afb2bd1cSJunchao Zhang   void     *spmvBuffer;
2739db3cbf9SStefano 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 */
274afb2bd1cSJunchao Zhang   cusparseDnVecDescr_t vecXDescr, vecYDescr; /* descriptor for the dense vectors in y=op(A)x */
2759db3cbf9SStefano Zampini #endif
276afb2bd1cSJunchao Zhang };
277afb2bd1cSJunchao Zhang 
278afb2bd1cSJunchao Zhang /* This is struct holding the relevant data needed to a MatMult */
279afb2bd1cSJunchao Zhang struct Mat_SeqAIJCUSPARSEMultStruct {
280afb2bd1cSJunchao Zhang   void              *mat;          /* opaque pointer to a matrix. This could be either a cusparseHybMat_t or a CsrMatrix */
281afb2bd1cSJunchao Zhang   cusparseMatDescr_t descr;        /* Data needed to describe the matrix for a multiply */
282afb2bd1cSJunchao Zhang   THRUSTINTARRAY    *cprowIndices; /* compressed row indices used in the parallel SpMV */
283afb2bd1cSJunchao Zhang   PetscScalar       *alpha_one;    /* pointer to a device "scalar" storing the alpha parameter in the SpMV */
284afb2bd1cSJunchao Zhang   PetscScalar       *beta_zero;    /* pointer to a device "scalar" storing the beta parameter in the SpMV as zero*/
285afb2bd1cSJunchao Zhang   PetscScalar       *beta_one;     /* pointer to a device "scalar" storing the beta parameter in the SpMV as one */
286afb2bd1cSJunchao Zhang #if PETSC_PKG_CUDA_VERSION_GE(11, 0, 0)
287afb2bd1cSJunchao Zhang   cusparseSpMatDescr_t matDescr;          /* descriptor for the matrix, used by SpMV and SpMM */
288fe5544b9SJunchao Zhang   #if PETSC_PKG_CUDA_VERSION_GE(12, 4, 0) // tested up to 12.6.0
289fe5544b9SJunchao Zhang   cusparseSpMatDescr_t matDescr_SpMV[3];  // Use separate MatDescr for opA's, to workaround cusparse bugs after 12.4, see https://github.com/NVIDIA/CUDALibrarySamples/issues/212,
290fe5544b9SJunchao Zhang   cusparseSpMatDescr_t matDescr_SpMM[3];  // and known issues https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html#cusparse-release-12-6
291fe5544b9SJunchao Zhang   #endif
292afb2bd1cSJunchao Zhang   Mat_CusparseSpMV cuSpMV[3]; /* different Mat_CusparseSpMV structs for non-transpose, transpose, conj-transpose */
Mat_SeqAIJCUSPARSEMultStructMat_SeqAIJCUSPARSEMultStruct293d71ae5a4SJacob Faibussowitsch   Mat_SeqAIJCUSPARSEMultStruct() : matDescr(NULL)
294d71ae5a4SJacob Faibussowitsch   {
295fe5544b9SJunchao Zhang     for (int i = 0; i < 3; i++) {
296fe5544b9SJunchao Zhang       cuSpMV[i].initialized = PETSC_FALSE;
297fe5544b9SJunchao Zhang   #if PETSC_PKG_CUDA_VERSION_GE(12, 4, 0)
298fe5544b9SJunchao Zhang       matDescr_SpMV[i] = NULL;
299fe5544b9SJunchao Zhang       matDescr_SpMM[i] = NULL;
300fe5544b9SJunchao Zhang   #endif
301fe5544b9SJunchao Zhang     }
302afb2bd1cSJunchao Zhang   }
303afb2bd1cSJunchao Zhang #endif
304afb2bd1cSJunchao Zhang };
305afb2bd1cSJunchao Zhang 
3067301b172SPierre Jolivet /* This is a larger struct holding all the matrices for a SpMV, and SpMV Transpose */
3079ae82921SPaul Mullowney struct Mat_SeqAIJCUSPARSE {
308aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *mat;            /* pointer to the matrix on the GPU */
309aa372e3fSPaul Mullowney   Mat_SeqAIJCUSPARSEMultStruct *matTranspose;   /* pointer to the matrix on the GPU (for the transpose ... useful for BiCG) */
310aa372e3fSPaul Mullowney   THRUSTARRAY                  *workVector;     /* pointer to a workvector to which we can copy the relevant indices of a vector we want to multiply */
311029808eeSJunchao Zhang   THRUSTINTARRAY32             *rowoffsets_gpu; /* rowoffsets on GPU in non-compressed-row format. It is used to convert CSR to CSC */
312213423ffSJunchao Zhang   PetscInt                      nrows;          /* number of rows of the matrix seen by GPU */
313e057df02SPaul Mullowney   MatCUSPARSEStorageFormat      format;         /* the storage format for the matrix on the device */
314365b711fSMark Adams   PetscBool                     use_cpu_solve;  /* Use AIJ_Seq (I)LU solve */
315aa372e3fSPaul Mullowney   cudaStream_t                  stream;         /* a stream for the parallel SpMV ... this is not owned and should not be deleted */
316aa372e3fSPaul Mullowney   cusparseHandle_t              handle;         /* a handle to the cusparse library ... this may not be owned (if we're working in parallel i.e. multiGPUs) */
31754da937aSStefano Zampini   PetscObjectState              nonzerostate;   /* track nonzero state to possibly recreate the GPU matrix */
318afb2bd1cSJunchao Zhang #if PETSC_PKG_CUDA_VERSION_GE(11, 0, 0)
319afb2bd1cSJunchao Zhang   size_t               csr2cscBufferSize; /* stuff used to compute the matTranspose above */
320fea72eb4SStefano Zampini   void                *csr2cscBuffer;     /* This is used as a C struct and is calloc'ed by PetscNew() */
321afb2bd1cSJunchao Zhang   cusparseCsr2CscAlg_t csr2cscAlg;        /* algorithms can be selected from command line options */
322afb2bd1cSJunchao Zhang   cusparseSpMVAlg_t    spmvAlg;
323afb2bd1cSJunchao Zhang   cusparseSpMMAlg_t    spmmAlg;
324afb2bd1cSJunchao Zhang #endif
325a49f1ed0SStefano Zampini   THRUSTINTARRAY *csr2csc_i;
3262c4ab24aSJunchao Zhang   THRUSTINTARRAY *coords; /* permutation array used in MatSeqAIJCUSPARSEMergeMats */
3279ae82921SPaul Mullowney };
3289ae82921SPaul Mullowney 
32947d993e7Ssuyashtn typedef struct Mat_SeqAIJCUSPARSETriFactors *Mat_SeqAIJCUSPARSETriFactors_p;
33047d993e7Ssuyashtn 
331d3ecb6c7SJed Brown PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSECopyToGPU(Mat);
332ed502f03SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSEMergeMats(Mat, Mat, MatReuse, Mat *);
3335f101d05SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSETriFactors_Reset(Mat_SeqAIJCUSPARSETriFactors_p *);
334ed502f03SStefano Zampini 
3356d54fb17SJacob Faibussowitsch using VecSeq_CUDA = Petsc::vec::cupm::impl::VecSeq_CUPM<Petsc::device::cupm::DeviceType::CUDA>;
3366d54fb17SJacob Faibussowitsch 
isCudaMem(const void * data)337d71ae5a4SJacob Faibussowitsch static inline bool isCudaMem(const void *data)
338d71ae5a4SJacob Faibussowitsch {
3396d54fb17SJacob Faibussowitsch   using namespace Petsc::device::cupm;
3406d54fb17SJacob Faibussowitsch   auto mtype = PETSC_MEMTYPE_HOST;
3416d54fb17SJacob Faibussowitsch 
3426d54fb17SJacob Faibussowitsch   PetscFunctionBegin;
3436d54fb17SJacob Faibussowitsch   PetscCallAbort(PETSC_COMM_SELF, impl::Interface<DeviceType::CUDA>::PetscCUPMGetMemType(data, &mtype));
3446d54fb17SJacob Faibussowitsch   PetscFunctionReturn(PetscMemTypeDevice(mtype));
34508391a17SStefano Zampini }
346