imate
C++/CUDA Reference
cusparse_symbols.cpp
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_symbols.h"
17 #include <cusparse.h> // CUSPARSE_VER_MAJOR
18 #include <cstdlib> // NULL
19 #include <sstream> // std::ostringstream
20 #include "./dynamic_loading.h" // dynamic_loading
21 
22 
23 // =========================
24 // Initialize static members
25 // =========================
26 
35 
36 
37 // ============
38 // get lib name
39 // ============
40 
43 
45 {
46  // Get the extension name of a shared library depending on OS
47  std::string lib_extension;
48 
49  #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || \
50  defined(__NT__)
51  lib_extension = "lib";
52  #elif __APPLE__
53  lib_extension = "dylib";
54  #elif __linux__
55  lib_extension = "so";
56  #else
57  #error "Unknown compiler"
58  #endif
59 
60  // Check cusparse version
61  #ifndef CUSPARSE_VER_MAJOR
62  #error "CUSPARSE_VER_MAJOR is not defined."
63  #endif
64 
65  // cusparse shared library base name
66  std::string lib_base_name = "libcusparse";
67 
68  // Construct the lib name
69  std::ostringstream oss;
70  // oss << lib_base_name << "." << lib_extension;
71  oss << lib_base_name << "." << lib_extension << "." \
72  << CUSPARSE_VER_MAJOR;
73 
74  std::string lib_name = oss.str();
75 
76  return lib_name;
77 }
78 
79 
80 #ifdef __cplusplus
81  extern "C" {
82 #endif
83 
84 
85 // ===============
86 // cusparse Create
87 // ===============
88 
91 
92 cusparseStatus_t cusparseCreate(cusparseHandle_t* handle)
93 {
95  {
96  std::string lib_name = cusparseSymbols::get_lib_name();
97  const char* symbol_name = "cusparseCreate";
98 
100  dynamic_loading::load_symbol<cusparseCreate_type>(
101  lib_name.c_str(),
102  symbol_name);
103  }
104 
105  return cusparseSymbols::cusparseCreate(handle);
106 }
107 
108 
109 // ================
110 // cusparse Destroy
111 // ================
112 
115 
116 cusparseStatus_t cusparseDestroy(cusparseHandle_t handle)
117 {
119  {
120  std::string lib_name = cusparseSymbols::get_lib_name();
121  const char* symbol_name = "cusparseDestroy";
122 
124  dynamic_loading::load_symbol<cusparseDestroy_type>(
125  lib_name.c_str(),
126  symbol_name);
127  }
128 
129  return cusparseSymbols::cusparseDestroy(handle);
130 }
131 
132 
133 // =================
134 // cusparseCreateCsr
135 // =================
136 
139 
140 cusparseStatus_t cusparseCreateCsr(
141  cusparseSpMatDescr_t* spMatDescr,
142  int64_t rows,
143  int64_t cols,
144  int64_t nnz,
145  void* csrRowOffsets,
146  void* csrColInd,
147  void* csrValues,
148  cusparseIndexType_t csrRowOffsetsType,
149  cusparseIndexType_t csrColIndType,
150  cusparseIndexBase_t idxBase,
151  cudaDataType valueType)
152 {
154  {
155  std::string lib_name = cusparseSymbols::get_lib_name();
156  const char* symbol_name = "cusparseCreateCsr";
157 
159  dynamic_loading::load_symbol<cusparseCreateCsr_type>(
160  lib_name.c_str(),
161  symbol_name);
162  }
163 
165  spMatDescr, rows, cols, nnz, csrRowOffsets, csrColInd, csrValues,
166  csrRowOffsetsType, csrColIndType, idxBase, valueType);
167 }
168 
169 
170 // ===================
171 // cusparseCreateDnVec
172 // ===================
173 
176 
177 cusparseStatus_t cusparseCreateDnVec(
178  cusparseDnVecDescr_t* dnVecDescr,
179  int64_t size,
180  void* values,
181  cudaDataType valueType)
182 {
184  {
185  std::string lib_name = cusparseSymbols::get_lib_name();
186  const char* symbol_name = "cusparseCreateDnVec";
187 
189  dynamic_loading::load_symbol<cusparseCreateDnVec_type>(
190  lib_name.c_str(),
191  symbol_name);
192  }
193 
195  dnVecDescr, size, values, valueType);
196 }
197 
198 
199 // ====================
200 // cusparseDestroySpMat
201 // ====================
202 
205 
206 cusparseStatus_t cusparseDestroySpMat(
207  cusparseConstSpMatDescr_t spMatDescr)
208 {
210  {
211  std::string lib_name = cusparseSymbols::get_lib_name();
212  const char* symbol_name = "cusparseDestroySpMat";
213 
215  dynamic_loading::load_symbol<cusparseDestroySpMat_type>(
216  lib_name.c_str(),
217  symbol_name);
218  }
219 
220  return cusparseSymbols::cusparseDestroySpMat(spMatDescr);
221 }
222 
223 
224 // ====================
225 // cusparseDestroyDnVec
226 // ====================
227 
230 
231 cusparseStatus_t cusparseDestroyDnVec(
232  cusparseConstDnVecDescr_t dnVecDescr)
233 {
235  {
236  std::string lib_name = cusparseSymbols::get_lib_name();
237  const char* symbol_name = "cusparseDestroyDnVec";
238 
240  dynamic_loading::load_symbol<cusparseDestroyDnVec_type>(
241  lib_name.c_str(),
242  symbol_name);
243  }
244 
245  return cusparseSymbols::cusparseDestroyDnVec(dnVecDescr);
246 }
247 
248 
249 // =======================
250 // cusparseSpMV_bufferSize
251 // =======================
252 
255 
256 cusparseStatus_t cusparseSpMV_bufferSize(
257  cusparseHandle_t handle,
258  cusparseOperation_t opA,
259  const void* alpha,
262  const void* beta,
263  cusparseDnVecDescr_t vecY,
264  cudaDataType computeType,
265  cusparseSpMVAlg_t alg,
266  size_t* bufferSize)
267 {
269  {
270  std::string lib_name = cusparseSymbols::get_lib_name();
271  const char* symbol_name = "cusparseSpMV_bufferSize";
272 
274  dynamic_loading::load_symbol<cusparseSpMV_bufferSize_type>(
275  lib_name.c_str(),
276  symbol_name);
277  }
278 
280  handle, opA, alpha, matA, vecX, beta, vecY, computeType, alg,
281  bufferSize);
282 }
283 
284 
285 // ============
286 // cusparseSpMV
287 // ============
288 
291 
292 cusparseStatus_t cusparseSpMV(
293  cusparseHandle_t handle,
294  cusparseOperation_t opA,
295  const void* alpha,
298  const void* beta,
299  cusparseDnVecDescr_t vecY,
300  cudaDataType computeType,
301  cusparseSpMVAlg_t alg,
302  void* externalBuffer)
303 {
304  if (cusparseSymbols::cusparseSpMV == NULL)
305  {
306  std::string lib_name = cusparseSymbols::get_lib_name();
307  const char* symbol_name = "cusparseSpMV";
308 
310  dynamic_loading::load_symbol<cusparseSpMV_type>(
311  lib_name.c_str(),
312  symbol_name);
313  }
314 
316  handle, opA, alpha, matA, vecX, beta, vecY, computeType, alg,
317  externalBuffer);
318 }
319 
320 
321 #ifdef __cplusplus
322  }
323 #endif
static cusparseCreateDnVec_type cusparseCreateDnVec
static cusparseCreateCsr_type cusparseCreateCsr
static cusparseDestroyDnVec_type cusparseDestroyDnVec
static cusparseSpMV_type cusparseSpMV
static cusparseDestroy_type cusparseDestroy
static std::string get_lib_name()
Returns the name of cusparse shared library.
static cusparseSpMV_bufferSize_type cusparseSpMV_bufferSize
static cusparseDestroySpMat_type cusparseDestroySpMat
static cusparseCreate_type cusparseCreate
cusparseStatus_t cusparseDestroy(cusparseHandle_t handle)
Definition of CUDA's cusparseDestroy function using dynamically loaded cublas library.
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 cusparseCreate(cusparseHandle_t *handle)
Definition of CUDA's cusparseCreate 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.
cusparseStatus_t(* cusparseSpMV_type)(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)
cusparseStatus_t(* cusparseCreate_type)(cusparseHandle_t *handle)
cusparseStatus_t(* cusparseDestroy_type)(cusparseHandle_t handle)
cusparseStatus_t(* cusparseCreateDnVec_type)(cusparseDnVecDescr_t *dnVecDescr, int64_t size, void *values, cudaDataType valueType)
#define cusparseConstDnVecDescr_t
cusparseStatus_t(* cusparseDestroyDnVec_type)(cusparseConstDnVecDescr_t dnVecDescr)
#define cusparseConstSpMatDescr_t
cusparseStatus_t(* cusparseSpMV_bufferSize_type)(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)
cusparseStatus_t(* cusparseDestroySpMat_type)(cusparseConstSpMatDescr_t spMatDescr)
cusparseStatus_t(* cusparseCreateCsr_type)(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)