1519f805aSKarl Rupp #if !defined(__CUSPARSEMATIMPL) 29ae82921SPaul Mullowney #define __CUSPARSEMATIMPL 39ae82921SPaul Mullowney 4*c41cb2e2SAlejandro Lamas Daviña #include <../src/vec/vec/impls/seq/seqcuda/cudavecimpl.h> 59ae82921SPaul Mullowney 62692e278SPaul Mullowney #if CUDA_VERSION>=4020 79ae82921SPaul Mullowney #include <cusparse_v2.h> 82692e278SPaul Mullowney #else 92692e278SPaul Mullowney #include <cusparse.h> 102692e278SPaul Mullowney #endif 119ae82921SPaul Mullowney 129ae82921SPaul Mullowney #include <algorithm> 139ae82921SPaul Mullowney #include <vector> 149ae82921SPaul Mullowney 15*c41cb2e2SAlejandro Lamas Daviña #include <thrust/device_vector.h> 16*c41cb2e2SAlejandro Lamas Daviña #include <thrust/device_ptr.h> 17*c41cb2e2SAlejandro Lamas Daviña #include <thrust/transform.h> 18*c41cb2e2SAlejandro Lamas Daviña #include <thrust/functional.h> 19*c41cb2e2SAlejandro Lamas Daviña 20aa372e3fSPaul Mullowney #if defined(PETSC_USE_COMPLEX) 21aa372e3fSPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE) 22aa372e3fSPaul Mullowney #define cusparse_solve cusparseCcsrsv_solve 23aa372e3fSPaul Mullowney #define cusparse_analysis cusparseCcsrsv_analysis 24aa372e3fSPaul Mullowney #define cusparse_csr_spmv cusparseCcsrmv 25aa372e3fSPaul Mullowney #define cusparse_csr2csc cusparseCcsr2csc 26aa372e3fSPaul Mullowney #define cusparse_hyb_spmv cusparseChybmv 27aa372e3fSPaul Mullowney #define cusparse_csr2hyb cusparseCcsr2hyb 28aa372e3fSPaul Mullowney #define cusparse_hyb2csr cusparseChyb2csr 29aa372e3fSPaul Mullowney cuFloatComplex ALPHA = {1.0f, 0.0f}; 30aa372e3fSPaul Mullowney cuFloatComplex BETA = {0.0f, 0.0f}; 31aa372e3fSPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE) 32aa372e3fSPaul Mullowney #define cusparse_solve cusparseZcsrsv_solve 33aa372e3fSPaul Mullowney #define cusparse_analysis cusparseZcsrsv_analysis 34aa372e3fSPaul Mullowney #define cusparse_csr_spmv cusparseZcsrmv 35aa372e3fSPaul Mullowney #define cusparse_csr2csc cusparseZcsr2csc 36aa372e3fSPaul Mullowney #define cusparse_hyb_spmv cusparseZhybmv 37aa372e3fSPaul Mullowney #define cusparse_csr2hyb cusparseZcsr2hyb 38aa372e3fSPaul Mullowney #define cusparse_hyb2csr cusparseZhyb2csr 39aa372e3fSPaul Mullowney cuDoubleComplex ALPHA = {1.0, 0.0}; 40aa372e3fSPaul Mullowney cuDoubleComplex BETA = {0.0, 0.0}; 41aa372e3fSPaul Mullowney #endif 42aa372e3fSPaul Mullowney #else 43aa372e3fSPaul Mullowney PetscScalar ALPHA = 1.0; 44aa372e3fSPaul Mullowney PetscScalar BETA = 0.0; 45aa372e3fSPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE) 46aa372e3fSPaul Mullowney #define cusparse_solve cusparseScsrsv_solve 47aa372e3fSPaul Mullowney #define cusparse_analysis cusparseScsrsv_analysis 48aa372e3fSPaul Mullowney #define cusparse_csr_spmv cusparseScsrmv 49aa372e3fSPaul Mullowney #define cusparse_csr2csc cusparseScsr2csc 50aa372e3fSPaul Mullowney #define cusparse_hyb_spmv cusparseShybmv 51aa372e3fSPaul Mullowney #define cusparse_csr2hyb cusparseScsr2hyb 52aa372e3fSPaul Mullowney #define cusparse_hyb2csr cusparseShyb2csr 53aa372e3fSPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE) 54aa372e3fSPaul Mullowney #define cusparse_solve cusparseDcsrsv_solve 55aa372e3fSPaul Mullowney #define cusparse_analysis cusparseDcsrsv_analysis 56aa372e3fSPaul Mullowney #define cusparse_csr_spmv cusparseDcsrmv 57aa372e3fSPaul Mullowney #define cusparse_csr2csc cusparseDcsr2csc 58aa372e3fSPaul Mullowney #define cusparse_hyb_spmv cusparseDhybmv 59aa372e3fSPaul Mullowney #define cusparse_csr2hyb cusparseDcsr2hyb 60aa372e3fSPaul Mullowney #define cusparse_hyb2csr cusparseDhyb2csr 61aa372e3fSPaul Mullowney #endif 62aa372e3fSPaul Mullowney #endif 63aa372e3fSPaul Mullowney 64aa372e3fSPaul Mullowney #define THRUSTINTARRAY32 thrust::device_vector<int> 65aa372e3fSPaul Mullowney #define THRUSTINTARRAY thrust::device_vector<PetscInt> 66aa372e3fSPaul Mullowney #define THRUSTARRAY thrust::device_vector<PetscScalar> 67aa372e3fSPaul Mullowney 68aa372e3fSPaul Mullowney /* A CSR matrix structure */ 69aa372e3fSPaul Mullowney struct CsrMatrix { 70aa372e3fSPaul Mullowney PetscInt num_rows; 71aa372e3fSPaul Mullowney PetscInt num_cols; 72aa372e3fSPaul Mullowney PetscInt num_entries; 73aa372e3fSPaul Mullowney THRUSTINTARRAY32 *row_offsets; 74aa372e3fSPaul Mullowney THRUSTINTARRAY32 *column_indices; 75aa372e3fSPaul Mullowney THRUSTARRAY *values; 769ae82921SPaul Mullowney }; 779ae82921SPaul Mullowney 78aa372e3fSPaul Mullowney //#define CUSPMATRIXCSR32 cusp::csr_matrix<int,PetscScalar,cusp::device_memory> 79aa372e3fSPaul Mullowney 80aa372e3fSPaul Mullowney /* This is struct holding the relevant data needed to a MatSolve */ 81aa372e3fSPaul Mullowney struct Mat_SeqAIJCUSPARSETriFactorStruct { 82aa372e3fSPaul Mullowney /* Data needed for triangular solve */ 83aa372e3fSPaul Mullowney cusparseMatDescr_t descr; 84aa372e3fSPaul Mullowney cusparseSolveAnalysisInfo_t solveInfo; 85aa372e3fSPaul Mullowney cusparseOperation_t solveOp; 86aa372e3fSPaul Mullowney CsrMatrix *csrMat; 87aa372e3fSPaul Mullowney }; 88aa372e3fSPaul Mullowney 89aa372e3fSPaul Mullowney /* This is struct holding the relevant data needed to a MatMult */ 90aa372e3fSPaul Mullowney struct Mat_SeqAIJCUSPARSEMultStruct { 91aa372e3fSPaul Mullowney void *mat; /* opaque pointer to a matrix. This could be either a cusparseHybMat_t or a CsrMatrix */ 92aa372e3fSPaul Mullowney cusparseMatDescr_t descr; /* Data needed to describe the matrix for a multiply */ 93aa372e3fSPaul Mullowney THRUSTINTARRAY *cprowIndices; /* compressed row indices used in the parallel SpMV */ 94b06137fdSPaul Mullowney PetscScalar *alpha; /* pointer to a device "scalar" storing the alpha parameter in the SpMV */ 95b06137fdSPaul Mullowney PetscScalar *beta; /* pointer to a device "scalar" storing the beta parameter in the SpMV */ 96aa372e3fSPaul Mullowney }; 97aa372e3fSPaul Mullowney 98aa372e3fSPaul Mullowney /* This is a larger struct holding all the triangular factors for a solve, transpose solve, and 99aa372e3fSPaul Mullowney any indices used in a reordering */ 100aa372e3fSPaul Mullowney struct Mat_SeqAIJCUSPARSETriFactors { 101aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorPtr; /* pointer for lower triangular (factored matrix) on GPU */ 102aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorPtr; /* pointer for upper triangular (factored matrix) on GPU */ 103aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *loTriFactorPtrTranspose; /* pointer for lower triangular (factored matrix) on GPU for the transpose (useful for BiCG) */ 104aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSETriFactorStruct *upTriFactorPtrTranspose; /* pointer for upper triangular (factored matrix) on GPU for the transpose (useful for BiCG)*/ 105aa372e3fSPaul Mullowney THRUSTINTARRAY *rpermIndices; /* indices used for any reordering */ 106aa372e3fSPaul Mullowney THRUSTINTARRAY *cpermIndices; /* indices used for any reordering */ 107aa372e3fSPaul Mullowney THRUSTARRAY *workVector; 108aa372e3fSPaul Mullowney cusparseHandle_t handle; /* a handle to the cusparse library */ 109aa372e3fSPaul Mullowney PetscInt nnz; /* number of nonzeros ... need this for accurate logging between ICC and ILU */ 110aa372e3fSPaul Mullowney }; 111aa372e3fSPaul Mullowney 112aa372e3fSPaul Mullowney /* This is a larger struct holding all the matrices for a SpMV, and SpMV Tranpose */ 1139ae82921SPaul Mullowney struct Mat_SeqAIJCUSPARSE { 114aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *mat; /* pointer to the matrix on the GPU */ 115aa372e3fSPaul Mullowney Mat_SeqAIJCUSPARSEMultStruct *matTranspose; /* pointer to the matrix on the GPU (for the transpose ... useful for BiCG) */ 116aa372e3fSPaul Mullowney THRUSTARRAY *workVector; /*pointer to a workvector to which we can copy the relevant indices of a vector we want to multiply */ 1179ae82921SPaul Mullowney PetscInt nonzerorow; /* number of nonzero rows ... used in the flop calculations */ 118e057df02SPaul Mullowney MatCUSPARSEStorageFormat format; /* the storage format for the matrix on the device */ 119aa372e3fSPaul Mullowney cudaStream_t stream; /* a stream for the parallel SpMV ... this is not owned and should not be deleted */ 120aa372e3fSPaul Mullowney cusparseHandle_t handle; /* a handle to the cusparse library ... this may not be owned (if we're working in parallel i.e. multiGPUs) */ 1219ae82921SPaul Mullowney }; 1229ae82921SPaul Mullowney 1235a576424SJed Brown PETSC_INTERN PetscErrorCode MatCUSPARSECopyToGPU(Mat); 124b06137fdSPaul Mullowney PETSC_INTERN PetscErrorCode MatCUSPARSESetStream(Mat, const cudaStream_t stream); 125b06137fdSPaul Mullowney PETSC_INTERN PetscErrorCode MatCUSPARSESetHandle(Mat, const cusparseHandle_t handle); 126b06137fdSPaul Mullowney PETSC_INTERN PetscErrorCode MatCUSPARSEClearHandle(Mat); 1279ae82921SPaul Mullowney #endif 128