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 217eaca502SStefano Zampini #define PetscStackCallThrust(body) do { \ 227eaca502SStefano Zampini try { \ 237eaca502SStefano Zampini body; \ 247eaca502SStefano Zampini } catch(thrust::system_error& e) { \ 2598921bdaSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in Thrust %s",e.what());\ 267eaca502SStefano Zampini } \ 277eaca502SStefano Zampini } while (0) 287eaca502SStefano Zampini 29aa372e3fSPaul Mullowney #if defined(PETSC_USE_COMPLEX) 30aa372e3fSPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE) 31ccdfe979SStefano Zampini const cuComplex PETSC_CUSPARSE_ONE = {1.0f, 0.0f}; 32ccdfe979SStefano Zampini const cuComplex PETSC_CUSPARSE_ZERO = {0.0f, 0.0f}; 33afb2bd1cSJunchao Zhang #elif defined(PETSC_USE_REAL_DOUBLE) 34afb2bd1cSJunchao Zhang const cuDoubleComplex PETSC_CUSPARSE_ONE = {1.0, 0.0}; 35afb2bd1cSJunchao Zhang const cuDoubleComplex PETSC_CUSPARSE_ZERO = {0.0, 0.0}; 36afb2bd1cSJunchao Zhang #endif 37afb2bd1cSJunchao Zhang #else 38afb2bd1cSJunchao Zhang const PetscScalar PETSC_CUSPARSE_ONE = 1.0; 39afb2bd1cSJunchao Zhang const PetscScalar PETSC_CUSPARSE_ZERO = 0.0; 40afb2bd1cSJunchao Zhang #endif 41afb2bd1cSJunchao Zhang 421b0a6780SStefano Zampini #if PETSC_PKG_CUDA_VERSION_GE(9,0,0) 43*261a78b4SJunchao Zhang #define csrsvInfo_t csrsv2Info_t 44*261a78b4SJunchao Zhang #define cusparseCreateCsrsvInfo cusparseCreateCsrsv2Info 45*261a78b4SJunchao Zhang #define cusparseDestroyCsrsvInfo cusparseDestroyCsrsv2Info 46afb2bd1cSJunchao Zhang #if defined(PETSC_USE_COMPLEX) 47afb2bd1cSJunchao Zhang #if defined(PETSC_USE_REAL_SINGLE) 48*261a78b4SJunchao 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) 49*261a78b4SJunchao 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) 50*261a78b4SJunchao 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) 51afb2bd1cSJunchao Zhang #elif defined(PETSC_USE_REAL_DOUBLE) 52*261a78b4SJunchao 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) 53*261a78b4SJunchao 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) 54*261a78b4SJunchao 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) 55afb2bd1cSJunchao Zhang #endif 56afb2bd1cSJunchao Zhang #else /* not complex */ 57afb2bd1cSJunchao Zhang #if defined(PETSC_USE_REAL_SINGLE) 58*261a78b4SJunchao Zhang #define cusparseXcsrsv_buffsize cusparseScsrsv2_bufferSize 59*261a78b4SJunchao Zhang #define cusparseXcsrsv_analysis cusparseScsrsv2_analysis 60*261a78b4SJunchao Zhang #define cusparseXcsrsv_solve cusparseScsrsv2_solve 61afb2bd1cSJunchao Zhang #elif defined(PETSC_USE_REAL_DOUBLE) 62*261a78b4SJunchao Zhang #define cusparseXcsrsv_buffsize cusparseDcsrsv2_bufferSize 63*261a78b4SJunchao Zhang #define cusparseXcsrsv_analysis cusparseDcsrsv2_analysis 64*261a78b4SJunchao Zhang #define cusparseXcsrsv_solve cusparseDcsrsv2_solve 65afb2bd1cSJunchao Zhang #endif 66afb2bd1cSJunchao Zhang #endif 67afb2bd1cSJunchao Zhang #else 68*261a78b4SJunchao Zhang #define csrsvInfo_t cusparseSolveAnalysisInfo_t 69*261a78b4SJunchao Zhang #define cusparseCreateCsrsvInfo cusparseCreateSolveAnalysisInfo 70*261a78b4SJunchao Zhang #define cusparseDestroyCsrsvInfo cusparseDestroySolveAnalysisInfo 71afb2bd1cSJunchao Zhang #if defined(PETSC_USE_COMPLEX) 72afb2bd1cSJunchao Zhang #if defined(PETSC_USE_REAL_SINGLE) 73*261a78b4SJunchao Zhang #define cusparseXcsrsv_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)) 74*261a78b4SJunchao Zhang #define cusparseXcsrsv_analysis(a,b,c,d,e,f,g,h,i) cusparseCcsrsv_analysis((a),(b),(c),(d),(e),(cuComplex*)(f),(g),(h),(i)) 751b0a6780SStefano Zampini #elif defined(PETSC_USE_REAL_DOUBLE) 76*261a78b4SJunchao Zhang #define cusparseXcsrsv_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)) 77*261a78b4SJunchao Zhang #define cusparseXcsrsv_analysis(a,b,c,d,e,f,g,h,i) cusparseZcsrsv_analysis((a),(b),(c),(d),(e),(cuDoubleComplex*)(f),(g),(h),(i)) 781b0a6780SStefano Zampini #endif 791b0a6780SStefano Zampini #else /* not complex */ 801b0a6780SStefano Zampini #if defined(PETSC_USE_REAL_SINGLE) 81*261a78b4SJunchao Zhang #define cusparseXcsrsv_solve cusparseScsrsv_solve 82*261a78b4SJunchao Zhang #define cusparseXcsrsv_analysis cusparseScsrsv_analysis 831b0a6780SStefano Zampini #elif defined(PETSC_USE_REAL_DOUBLE) 84*261a78b4SJunchao Zhang #define cusparseXcsrsv_solve cusparseDcsrsv_solve 85*261a78b4SJunchao Zhang #define cusparseXcsrsv_analysis cusparseDcsrsv_analysis 861b0a6780SStefano Zampini #endif 871b0a6780SStefano Zampini #endif 881b0a6780SStefano Zampini #endif 891b0a6780SStefano Zampini 901b0a6780SStefano Zampini #if PETSC_PKG_CUDA_VERSION_GE(11,0,0) 911b0a6780SStefano Zampini #define cusparse_csr2csc cusparseCsr2cscEx2 921b0a6780SStefano Zampini #if defined(PETSC_USE_COMPLEX) 931b0a6780SStefano Zampini #if defined(PETSC_USE_REAL_SINGLE) 941b0a6780SStefano Zampini #define cusparse_scalartype CUDA_C_32F 95039c6fbaSStefano 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) 96039c6fbaSStefano 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) 971b0a6780SStefano Zampini #elif defined(PETSC_USE_REAL_DOUBLE) 981b0a6780SStefano Zampini #define cusparse_scalartype CUDA_C_64F 99039c6fbaSStefano 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) 100039c6fbaSStefano 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) 1011b0a6780SStefano Zampini #endif 1021b0a6780SStefano Zampini #else /* not complex */ 1031b0a6780SStefano Zampini #if defined(PETSC_USE_REAL_SINGLE) 1041b0a6780SStefano Zampini #define cusparse_scalartype CUDA_R_32F 105039c6fbaSStefano Zampini #define cusparse_csr_spgeam cusparseScsrgeam2 106039c6fbaSStefano Zampini #define cusparse_csr_spgeam_bufferSize cusparseScsrgeam2_bufferSizeExt 1071b0a6780SStefano Zampini #elif defined(PETSC_USE_REAL_DOUBLE) 1081b0a6780SStefano Zampini #define cusparse_scalartype CUDA_R_64F 109039c6fbaSStefano Zampini #define cusparse_csr_spgeam cusparseDcsrgeam2 110039c6fbaSStefano Zampini #define cusparse_csr_spgeam_bufferSize cusparseDcsrgeam2_bufferSizeExt 1111b0a6780SStefano Zampini #endif 1121b0a6780SStefano Zampini #endif 1131b0a6780SStefano Zampini #else 1141b0a6780SStefano Zampini #if defined(PETSC_USE_COMPLEX) 1151b0a6780SStefano Zampini #if defined(PETSC_USE_REAL_SINGLE) 116ec42abe4SAlejandro 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)) 117ccdfe979SStefano 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)) 118ec42abe4SAlejandro 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)) 119ec42abe4SAlejandro 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)) 120ec42abe4SAlejandro 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)) 121ec42abe4SAlejandro Lamas Daviña #define cusparse_hyb2csr(a,b,c,d,e,f) cusparseChyb2csr((a),(b),(c),(cuComplex*)(d),(e),(f)) 122fcdce8c4SStefano 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) 123039c6fbaSStefano 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) 124aa372e3fSPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE) 125ec42abe4SAlejandro 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)) 126ccdfe979SStefano 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)) 127ec42abe4SAlejandro 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)) 128ec42abe4SAlejandro 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)) 129ec42abe4SAlejandro 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)) 130ec42abe4SAlejandro Lamas Daviña #define cusparse_hyb2csr(a,b,c,d,e,f) cusparseZhyb2csr((a),(b),(c),(cuDoubleComplex*)(d),(e),(f)) 131fcdce8c4SStefano 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) 132039c6fbaSStefano 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) 133aa372e3fSPaul Mullowney #endif 134aa372e3fSPaul Mullowney #else 135aa372e3fSPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE) 136aa372e3fSPaul Mullowney #define cusparse_csr_spmv cusparseScsrmv 137ccdfe979SStefano Zampini #define cusparse_csr_spmm cusparseScsrmm 138aa372e3fSPaul Mullowney #define cusparse_csr2csc cusparseScsr2csc 139aa372e3fSPaul Mullowney #define cusparse_hyb_spmv cusparseShybmv 140aa372e3fSPaul Mullowney #define cusparse_csr2hyb cusparseScsr2hyb 141aa372e3fSPaul Mullowney #define cusparse_hyb2csr cusparseShyb2csr 142fcdce8c4SStefano Zampini #define cusparse_csr_spgemm cusparseScsrgemm 143039c6fbaSStefano Zampini #define cusparse_csr_spgeam cusparseScsrgeam 144aa372e3fSPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE) 145aa372e3fSPaul Mullowney #define cusparse_csr_spmv cusparseDcsrmv 146ccdfe979SStefano Zampini #define cusparse_csr_spmm cusparseDcsrmm 147aa372e3fSPaul Mullowney #define cusparse_csr2csc cusparseDcsr2csc 148aa372e3fSPaul Mullowney #define cusparse_hyb_spmv cusparseDhybmv 149aa372e3fSPaul Mullowney #define cusparse_csr2hyb cusparseDcsr2hyb 150aa372e3fSPaul Mullowney #define cusparse_hyb2csr cusparseDhyb2csr 151fcdce8c4SStefano Zampini #define cusparse_csr_spgemm cusparseDcsrgemm 152039c6fbaSStefano Zampini #define cusparse_csr_spgeam cusparseDcsrgeam 153aa372e3fSPaul Mullowney #endif 154aa372e3fSPaul Mullowney #endif 155afb2bd1cSJunchao Zhang #endif 156aa372e3fSPaul Mullowney 157aa372e3fSPaul Mullowney #define THRUSTINTARRAY32 thrust::device_vector<int> 158aa372e3fSPaul Mullowney #define THRUSTINTARRAY thrust::device_vector<PetscInt> 159aa372e3fSPaul Mullowney #define THRUSTARRAY thrust::device_vector<PetscScalar> 160aa372e3fSPaul Mullowney 161aa372e3fSPaul Mullowney /* A CSR matrix structure */ 162aa372e3fSPaul Mullowney struct CsrMatrix { 163aa372e3fSPaul Mullowney PetscInt num_rows; 164aa372e3fSPaul Mullowney PetscInt num_cols; 165aa372e3fSPaul Mullowney PetscInt num_entries; 166aa372e3fSPaul Mullowney THRUSTINTARRAY32 *row_offsets; 167aa372e3fSPaul Mullowney THRUSTINTARRAY32 *column_indices; 168aa372e3fSPaul Mullowney THRUSTARRAY *values; 1699ae82921SPaul Mullowney }; 1709ae82921SPaul Mullowney 171aa372e3fSPaul Mullowney /* This is struct holding the relevant data needed to a MatSolve */ 172aa372e3fSPaul Mullowney struct Mat_SeqAIJCUSPARSETriFactorStruct { 173aa372e3fSPaul Mullowney /* Data needed for triangular solve */ 174aa372e3fSPaul Mullowney cusparseMatDescr_t descr; 175aa372e3fSPaul Mullowney cusparseOperation_t solveOp; 176aa372e3fSPaul Mullowney CsrMatrix *csrMat; 177*261a78b4SJunchao Zhang csrsvInfo_t solveInfo; 1781b0a6780SStefano Zampini cusparseSolvePolicy_t solvePolicy; /* whether level information is generated and used */ 1791b0a6780SStefano Zampini int solveBufferSize; 1801b0a6780SStefano Zampini void *solveBuffer; 1811b0a6780SStefano Zampini size_t csr2cscBufferSize; /* to transpose the triangular factor (only used for CUDA >= 11.0) */ 1821b0a6780SStefano Zampini void *csr2cscBuffer; 1832cbc15d9SMark PetscScalar *AA_h; /* managed host buffer for moving values to the GPU */ 184aa372e3fSPaul Mullowney }; 185aa372e3fSPaul Mullowney 186afb2bd1cSJunchao Zhang /* This is a larger struct holding all the triangular factors for a solve, transpose solve, and any indices used in a reordering */ 187aa372e3fSPaul Mullowney struct Mat_SeqAIJCUSPARSETriFactors { 188aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorPtr; /* pointer for lower triangular (factored matrix) on GPU */ 189aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorPtr; /* pointer for upper triangular (factored matrix) on GPU */ 190aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorPtrTranspose; /* pointer for lower triangular (factored matrix) on GPU for the transpose (useful for BiCG) */ 191aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorPtrTranspose; /* pointer for upper triangular (factored matrix) on GPU for the transpose (useful for BiCG)*/ 192aa372e3fSPaul Mullowney THRUSTINTARRAY *rpermIndices; /* indices used for any reordering */ 193aa372e3fSPaul Mullowney THRUSTINTARRAY *cpermIndices; /* indices used for any reordering */ 194aa372e3fSPaul Mullowney THRUSTARRAY *workVector; 195aa372e3fSPaul Mullowney cusparseHandle_t handle; /* a handle to the cusparse library */ 196aa372e3fSPaul Mullowney PetscInt nnz; /* number of nonzeros ... need this for accurate logging between ICC and ILU */ 197bddcd29dSMark Adams PetscScalar *a_band_d; /* GPU data for banded CSR LU factorization matrix diag(L)=1 */ 198bddcd29dSMark Adams int *i_band_d; /* this could be optimized away */ 199e8d2b73aSMark Adams cudaDeviceProp dev_prop; 200e8d2b73aSMark Adams PetscBool init_dev_prop; 201aa372e3fSPaul Mullowney }; 202aa372e3fSPaul Mullowney 203afb2bd1cSJunchao Zhang struct Mat_CusparseSpMV { 204afb2bd1cSJunchao Zhang PetscBool initialized; /* Don't rely on spmvBuffer != NULL to test if the struct is initialized, */ 205afb2bd1cSJunchao Zhang size_t spmvBufferSize; /* since I'm not sure if smvBuffer can be NULL even after cusparseSpMV_bufferSize() */ 206afb2bd1cSJunchao Zhang void *spmvBuffer; 2079db3cbf9SStefano 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 */ 208afb2bd1cSJunchao Zhang cusparseDnVecDescr_t vecXDescr,vecYDescr; /* descriptor for the dense vectors in y=op(A)x */ 2099db3cbf9SStefano Zampini #endif 210afb2bd1cSJunchao Zhang }; 211afb2bd1cSJunchao Zhang 212afb2bd1cSJunchao Zhang /* This is struct holding the relevant data needed to a MatMult */ 213afb2bd1cSJunchao Zhang struct Mat_SeqAIJCUSPARSEMultStruct { 214afb2bd1cSJunchao Zhang void *mat; /* opaque pointer to a matrix. This could be either a cusparseHybMat_t or a CsrMatrix */ 215afb2bd1cSJunchao Zhang cusparseMatDescr_t descr; /* Data needed to describe the matrix for a multiply */ 216afb2bd1cSJunchao Zhang THRUSTINTARRAY *cprowIndices; /* compressed row indices used in the parallel SpMV */ 217afb2bd1cSJunchao Zhang PetscScalar *alpha_one; /* pointer to a device "scalar" storing the alpha parameter in the SpMV */ 218afb2bd1cSJunchao Zhang PetscScalar *beta_zero; /* pointer to a device "scalar" storing the beta parameter in the SpMV as zero*/ 219afb2bd1cSJunchao Zhang PetscScalar *beta_one; /* pointer to a device "scalar" storing the beta parameter in the SpMV as one */ 220afb2bd1cSJunchao Zhang #if PETSC_PKG_CUDA_VERSION_GE(11,0,0) 221afb2bd1cSJunchao Zhang cusparseSpMatDescr_t matDescr; /* descriptor for the matrix, used by SpMV and SpMM */ 222afb2bd1cSJunchao Zhang Mat_CusparseSpMV cuSpMV[3]; /* different Mat_CusparseSpMV structs for non-transpose, transpose, conj-transpose */ 223afb2bd1cSJunchao Zhang Mat_SeqAIJCUSPARSEMultStruct() : matDescr(NULL) { 224afb2bd1cSJunchao Zhang for (int i=0; i<3; i++) cuSpMV[i].initialized = PETSC_FALSE; 225afb2bd1cSJunchao Zhang } 226afb2bd1cSJunchao Zhang #endif 227afb2bd1cSJunchao Zhang }; 228afb2bd1cSJunchao Zhang 2297301b172SPierre Jolivet /* This is a larger struct holding all the matrices for a SpMV, and SpMV Transpose */ 2309ae82921SPaul Mullowney struct Mat_SeqAIJCUSPARSE { 231aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *mat; /* pointer to the matrix on the GPU */ 232aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matTranspose; /* pointer to the matrix on the GPU (for the transpose ... useful for BiCG) */ 233aa372e3fSPaul Mullowney THRUSTARRAY *workVector; /* pointer to a workvector to which we can copy the relevant indices of a vector we want to multiply */ 234029808eeSJunchao Zhang THRUSTINTARRAY32 *rowoffsets_gpu; /* rowoffsets on GPU in non-compressed-row format. It is used to convert CSR to CSC */ 235213423ffSJunchao Zhang PetscInt nrows; /* number of rows of the matrix seen by GPU */ 236e057df02SPaul Mullowney MatCUSPARSEStorageFormat format; /* the storage format for the matrix on the device */ 237365b711fSMark Adams PetscBool use_cpu_solve; /* Use AIJ_Seq (I)LU solve */ 238aa372e3fSPaul Mullowney cudaStream_t stream; /* a stream for the parallel SpMV ... this is not owned and should not be deleted */ 239aa372e3fSPaul Mullowney cusparseHandle_t handle; /* a handle to the cusparse library ... this may not be owned (if we're working in parallel i.e. multiGPUs) */ 24054da937aSStefano Zampini PetscObjectState nonzerostate; /* track nonzero state to possibly recreate the GPU matrix */ 241afb2bd1cSJunchao Zhang #if PETSC_PKG_CUDA_VERSION_GE(11,0,0) 242afb2bd1cSJunchao Zhang size_t csr2cscBufferSize; /* stuff used to compute the matTranspose above */ 243afb2bd1cSJunchao Zhang void *csr2cscBuffer; /* This is used as a C struct and is calloc'ed by PetscNewLog() */ 244afb2bd1cSJunchao Zhang cusparseCsr2CscAlg_t csr2cscAlg; /* algorithms can be selected from command line options */ 245afb2bd1cSJunchao Zhang cusparseSpMVAlg_t spmvAlg; 246afb2bd1cSJunchao Zhang cusparseSpMMAlg_t spmmAlg; 247afb2bd1cSJunchao Zhang #endif 248a49f1ed0SStefano Zampini THRUSTINTARRAY *csr2csc_i; 249042217e8SBarry Smith PetscSplitCSRDataStructure deviceMat; /* Matrix on device for, eg, assembly */ 250219fbbafSJunchao Zhang 251219fbbafSJunchao Zhang /* Stuff for basic COO support */ 252ddea5d60SJunchao Zhang THRUSTINTARRAY *cooPerm; /* permutation array that sorts the input coo entris by row and col */ 253ddea5d60SJunchao Zhang THRUSTINTARRAY *cooPerm_a; /* ordered array that indicate i-th nonzero (after sorting) is the j-th unique nonzero */ 254219fbbafSJunchao Zhang 255219fbbafSJunchao Zhang /* Stuff for extended COO support */ 256219fbbafSJunchao Zhang PetscBool use_extended_coo; /* Use extended COO format */ 257219fbbafSJunchao Zhang PetscCount *jmap_d; /* perm[disp+jmap[i]..disp+jmap[i+1]) gives indices of entries in v[] associated with i-th nonzero of the matrix */ 258219fbbafSJunchao Zhang PetscCount *perm_d; 259219fbbafSJunchao Zhang 260219fbbafSJunchao Zhang Mat_SeqAIJCUSPARSE() : use_extended_coo(PETSC_FALSE), perm_d(NULL), jmap_d(NULL) {} 2619ae82921SPaul Mullowney }; 2629ae82921SPaul Mullowney 263d3ecb6c7SJed Brown PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSECopyToGPU(Mat); 264219fbbafSJunchao Zhang PETSC_INTERN PetscErrorCode MatSetPreallocationCOO_SeqAIJCUSPARSE_Basic(Mat,PetscCount,const PetscInt[],const PetscInt[]); 265219fbbafSJunchao Zhang PETSC_INTERN PetscErrorCode MatSetValuesCOO_SeqAIJCUSPARSE_Basic(Mat,const PetscScalar[],InsertMode); 266ed502f03SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSEMergeMats(Mat,Mat,MatReuse,Mat*); 2675f101d05SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJCUSPARSETriFactors_Reset(Mat_SeqAIJCUSPARSETriFactors_p*); 268ed502f03SStefano Zampini 2699fbee547SJacob Faibussowitsch static inline bool isCudaMem(const void *data) 27008391a17SStefano Zampini { 27108391a17SStefano Zampini cudaError_t cerr; 27208391a17SStefano Zampini struct cudaPointerAttributes attr; 27308391a17SStefano Zampini enum cudaMemoryType mtype; 27408391a17SStefano Zampini cerr = cudaPointerGetAttributes(&attr,data); /* Do not check error since before CUDA 11.0, passing a host pointer returns cudaErrorInvalidValue */ 27508391a17SStefano Zampini cudaGetLastError(); /* Reset the last error */ 27608391a17SStefano Zampini #if (CUDART_VERSION < 10000) 27708391a17SStefano Zampini mtype = attr.memoryType; 27808391a17SStefano Zampini #else 27908391a17SStefano Zampini mtype = attr.type; 28008391a17SStefano Zampini #endif 28108391a17SStefano Zampini if (cerr == cudaSuccess && mtype == cudaMemoryTypeDevice) return true; 28208391a17SStefano Zampini else return false; 28308391a17SStefano Zampini } 28408391a17SStefano Zampini 2859ae82921SPaul Mullowney #endif 286