imate
C++/CUDA Reference
cusparse_interface.cu
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright 2021, Siavash Ameli <sameli@berkeley.edu>
3  * SPDX-License-Identifier: BSD-3-Clause
4  * SPDX-FileType: SOURCE
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the license found in the LICENSE.txt file in the root
8  * directory of this source tree.
9  */
10 
11 
12 // =======
13 // Headers
14 // =======
15 
16 #include "./cusparse_interface.h"
17 #include <cassert> // assert
18 
19 // ==================
20 // cusparse interface
21 // ==================
22 
28 
30 {
31 
32  // ======================
33  // create cusparse matrix (float)
34  // ======================
35 
38 
39  template<>
41  cusparseSpMatDescr_t& cusparse_matrix,
42  const LongIndexType num_rows,
43  const LongIndexType num_columns,
44  const LongIndexType nnz,
45  float* device_A_data,
46  LongIndexType* device_A_indices,
47  LongIndexType* device_A_index_pointer)
48  {
49  cusparseStatus_t status = cusparseCreateCsr(
50  &cusparse_matrix, num_rows, num_columns, nnz,
51  device_A_index_pointer, device_A_indices, device_A_data,
52  CUSPARSE_INDEX_32I, CUSPARSE_INDEX_32I,
53  CUSPARSE_INDEX_BASE_ZERO, CUDA_R_32F);
54 
55  assert(status == CUSPARSE_STATUS_SUCCESS);
56  }
57 
58 
59  // ======================
60  // create cusparse matrix (double)
61  // ======================
62 
65 
66  template<>
68  cusparseSpMatDescr_t& cusparse_matrix,
69  const LongIndexType num_rows,
70  const LongIndexType num_columns,
71  const LongIndexType nnz,
72  double* device_A_data,
73  LongIndexType* device_A_indices,
74  LongIndexType* device_A_index_pointer)
75  {
76  cusparseStatus_t status = cusparseCreateCsr(
77  &cusparse_matrix, num_rows, num_columns, nnz,
78  device_A_index_pointer, device_A_indices, device_A_data,
79  CUSPARSE_INDEX_32I, CUSPARSE_INDEX_32I,
80  CUSPARSE_INDEX_BASE_ZERO, CUDA_R_64F);
81 
82  assert(status == CUSPARSE_STATUS_SUCCESS);
83  }
84 
85 
86  // ======================
87  // create cusparse vector (float)
88  // ======================
89 
96 
97  template<>
99  cusparseDnVecDescr_t& cusparse_vector,
100  const LongIndexType vector_size,
101  float* device_vector)
102  {
103  cusparseStatus_t status = cusparseCreateDnVec(
104  &cusparse_vector, vector_size, device_vector, CUDA_R_32F);
105 
106  assert(status == CUSPARSE_STATUS_SUCCESS);
107  }
108 
109 
110  // ======================
111  // create cusparse vector (double)
112  // ======================
113 
120 
121  template<>
123  cusparseDnVecDescr_t& cusparse_vector,
124  const LongIndexType vector_size,
125  double* device_vector)
126  {
127  cusparseStatus_t status = cusparseCreateDnVec(
128  &cusparse_vector, vector_size, device_vector, CUDA_R_64F);
129 
130  assert(status == CUSPARSE_STATUS_SUCCESS);
131  }
132 
133 
134  // =======================
135  // destroy cusparse matrix
136  // =======================
137 
140 
142  cusparseSpMatDescr_t& cusparse_matrix)
143  {
144  cusparseStatus_t status = cusparseDestroySpMat(cusparse_matrix);
145  assert(status == CUSPARSE_STATUS_SUCCESS);
146  }
147 
148 
149  // =======================
150  // destroy cusparse vector
151  // =======================
152 
155 
157  cusparseDnVecDescr_t& cusparse_vector)
158  {
159  cusparseStatus_t status = cusparseDestroyDnVec(cusparse_vector);
160  assert(status == CUSPARSE_STATUS_SUCCESS);
161  }
162 
163 
164  // ===========================
165  // cusparse matrix buffer size (float)
166  // ===========================
167 
172 
173  template<>
175  cusparseHandle_t cusparse_handle,
176  cusparseOperation_t cusparse_operation,
177  const float alpha,
178  cusparseSpMatDescr_t cusparse_matrix,
179  cusparseDnVecDescr_t cusparse_input_vector,
180  const float beta,
181  cusparseDnVecDescr_t cusparse_output_vector,
182  cusparseSpMVAlg_t algorithm,
183  size_t* buffer_size)
184  {
185  cusparseStatus_t status = cusparseSpMV_bufferSize(
186  cusparse_handle, cusparse_operation, &alpha, cusparse_matrix,
187  cusparse_input_vector, &beta, cusparse_output_vector,
188  CUDA_R_32F, algorithm, buffer_size);
189 
190  assert(status == CUSPARSE_STATUS_SUCCESS);
191  }
192 
193 
194  // ===========================
195  // cusparse matrix buffer size (double)
196  // ===========================
197 
202 
203  template<>
205  cusparseHandle_t cusparse_handle,
206  cusparseOperation_t cusparse_operation,
207  const double alpha,
208  cusparseSpMatDescr_t cusparse_matrix,
209  cusparseDnVecDescr_t cusparse_input_vector,
210  const double beta,
211  cusparseDnVecDescr_t cusparse_output_vector,
212  cusparseSpMVAlg_t algorithm,
213  size_t* buffer_size)
214  {
215  cusparseStatus_t status = cusparseSpMV_bufferSize(
216  cusparse_handle, cusparse_operation, &alpha, cusparse_matrix,
217  cusparse_input_vector, &beta, cusparse_output_vector,
218  CUDA_R_64F, algorithm, buffer_size);
219 
220  assert(status == CUSPARSE_STATUS_SUCCESS);
221  }
222 
223 
224  // ===============
225  // cusparse matvec (float)
226  // ===============
227 
230 
231  template<>
233  cusparseHandle_t cusparse_handle,
234  cusparseOperation_t cusparse_operation,
235  const float alpha,
236  cusparseSpMatDescr_t cusparse_matrix,
237  cusparseDnVecDescr_t cusparse_input_vector,
238  const float beta,
239  cusparseDnVecDescr_t cusparse_output_vector,
240  cusparseSpMVAlg_t algorithm,
241  void* external_buffer)
242  {
243  cusparseStatus_t status = cusparseSpMV(cusparse_handle,
244  cusparse_operation, &alpha,
245  cusparse_matrix,
246  cusparse_input_vector, &beta,
247  cusparse_output_vector,
248  CUDA_R_32F, algorithm,
249  external_buffer);
250 
251  assert(status == CUSPARSE_STATUS_SUCCESS);
252  }
253 
254 
255  // ===============
256  // cusparse matvec (double)
257  // ===============
258 
261 
262  template<>
264  cusparseHandle_t cusparse_handle,
265  cusparseOperation_t cusparse_operation,
266  const double alpha,
267  cusparseSpMatDescr_t cusparse_matrix,
268  cusparseDnVecDescr_t cusparse_input_vector,
269  const double beta,
270  cusparseDnVecDescr_t cusparse_output_vector,
271  cusparseSpMVAlg_t algorithm,
272  void* external_buffer)
273  {
274  cusparseStatus_t status = cusparseSpMV(cusparse_handle,
275  cusparse_operation, &alpha,
276  cusparse_matrix,
277  cusparse_input_vector, &beta,
278  cusparse_output_vector,
279  CUDA_R_64F, algorithm,
280  external_buffer);
281 
282  assert(status == CUSPARSE_STATUS_SUCCESS);
283  }
284 } // namespace cusparse_interface
cusparseStatus_t cusparseSpMV(cusparseHandle_t handle, cusparseOperation_t opA, const void *alpha, cusparseConstSpMatDescr_t matA, cusparseConstDnVecDescr_t vecX, const void *beta, cusparseDnVecDescr_t vecY, cudaDataType computeType, cusparseSpMVAlg_t alg, void *externalBuffer)
Definition of CUDA's cusparseSmMV function using dynamically loaded cublas library.
cusparseStatus_t cusparseDestroySpMat(cusparseConstSpMatDescr_t spMatDescr)
Definition of CUDA's cusparseDestroySpMat function using dynamically loaded cublas library.
cusparseStatus_t cusparseCreateDnVec(cusparseDnVecDescr_t *dnVecDescr, int64_t size, void *values, cudaDataType valueType)
Definition of CUDA's cusparseCreateDnVec function using dynamically loaded cublas library.
cusparseStatus_t cusparseSpMV_bufferSize(cusparseHandle_t handle, cusparseOperation_t opA, const void *alpha, cusparseConstSpMatDescr_t matA, cusparseConstDnVecDescr_t vecX, const void *beta, cusparseDnVecDescr_t vecY, cudaDataType computeType, cusparseSpMVAlg_t alg, size_t *bufferSize)
Definition of CUDA's cusparseSpMV_bufferSize function using dynamically loaded cublas library.
cusparseStatus_t cusparseCreateCsr(cusparseSpMatDescr_t *spMatDescr, int64_t rows, int64_t cols, int64_t nnz, void *csrRowOffsets, void *csrColInd, void *csrValues, cusparseIndexType_t csrRowOffsetsType, cusparseIndexType_t csrColIndType, cusparseIndexBase_t idxBase, cudaDataType valueType)
Definition of CUDA's cusparseCreateCsr function using dynamically loaded cublas library.
cusparseStatus_t cusparseDestroyDnVec(cusparseConstDnVecDescr_t dnVecDescr)
Definition of CUDA's cusparseDestroyDnVec function using dynamically loaded cublas library.
A collection of templates to wrapper cusparse functions.
void cusparse_matvec< double >(cusparseHandle_t cusparse_handle, cusparseOperation_t cusparse_operation, const double alpha, cusparseSpMatDescr_t cusparse_matrix, cusparseDnVecDescr_t cusparse_input_vector, const double beta, cusparseDnVecDescr_t cusparse_output_vector, cusparseSpMVAlg_t algorithm, void *external_buffer)
A wrapper for cusparseSpMV to perform sparse matrix-vector multiplication uasing double precision dat...
void cusparse_matrix_buffer_size< float >(cusparseHandle_t cusparse_handle, cusparseOperation_t cusparse_operation, const float alpha, cusparseSpMatDescr_t cusparse_matrix, cusparseDnVecDescr_t cusparse_input_vector, const float beta, cusparseDnVecDescr_t cusparse_output_vector, cusparseSpMVAlg_t algorithm, size_t *buffer_size)
A template wrapper for cusparseSpMat_buffersize for float precision data. This function determines th...
void create_cusparse_matrix< double >(cusparseSpMatDescr_t &cusparse_matrix, const LongIndexType num_rows, const LongIndexType num_columns, const LongIndexType nnz, double *device_A_data, LongIndexType *device_A_indices, LongIndexType *device_A_index_pointer)
A template wrapper for cusparseSpMatDescr_t for the double precision data.
void create_cusparse_vector< double >(cusparseDnVecDescr_t &cusparse_vector, const LongIndexType vector_size, double *device_vector)
A template wrapper for cusparseDnVecDescr_t for the double precision data.
void destroy_cusparse_matrix(cusparseSpMatDescr_t &cusparse_matrix)
Destroys cusparse matrix.
void destroy_cusparse_vector(cusparseDnVecDescr_t &cusparse_vector)
Destroys cusparse vector.
void cusparse_matvec< float >(cusparseHandle_t cusparse_handle, cusparseOperation_t cusparse_operation, const float alpha, cusparseSpMatDescr_t cusparse_matrix, cusparseDnVecDescr_t cusparse_input_vector, const float beta, cusparseDnVecDescr_t cusparse_output_vector, cusparseSpMVAlg_t algorithm, void *external_buffer)
A wrapper for cusparseSpMV to perform sparse matrix-vector multiplication uasing float precision data...
void create_cusparse_vector< float >(cusparseDnVecDescr_t &cusparse_vector, const LongIndexType vector_size, float *device_vector)
A template wrapper for cusparseDnVecDescr_t for the float precision data.
void create_cusparse_matrix< float >(cusparseSpMatDescr_t &cusparse_matrix, const LongIndexType num_rows, const LongIndexType num_columns, const LongIndexType nnz, float *device_A_data, LongIndexType *device_A_indices, LongIndexType *device_A_index_pointer)
A template wrapper for cusparseSpMatDescr_t for the float precision data.
void cusparse_matrix_buffer_size< double >(cusparseHandle_t cusparse_handle, cusparseOperation_t cusparse_operation, const double alpha, cusparseSpMatDescr_t cusparse_matrix, cusparseDnVecDescr_t cusparse_input_vector, const double beta, cusparseDnVecDescr_t cusparse_output_vector, cusparseSpMVAlg_t algorithm, size_t *buffer_size)
A template wrapper for cusparseSpMat_buffersize for double precision data. This function determines t...
int LongIndexType
Definition: types.h:60