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