imate
C++/CUDA Reference
cublas_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 "./cublas_symbols.h"
17 #include <cublas_api.h> // CUBLAS_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 
41 
42 
43 // ============
44 // get lib name
45 // ============
46 
49 
51 {
52  // Get the extension name of a shared library depending on OS
53  std::string lib_extension;
54 
55  #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || \
56  defined(__NT__)
57  lib_extension = "lib";
58  #elif __APPLE__
59  lib_extension = "dylib";
60  #elif __linux__
61  lib_extension = "so";
62  #else
63  #error "Unknown compiler"
64  #endif
65 
66  // Check cublas version
67  #ifndef CUBLAS_VER_MAJOR
68  #error "CUBLAS_VER_MAJOR is not defined."
69  #endif
70 
71  // cublas shared library base name
72  std::string lib_base_name = "libcublas";
73 
74  // Construct the lib name
75  std::ostringstream oss;
76  // oss << lib_base_name << "." << lib_extension;
77  oss << lib_base_name << "." << lib_extension << "." \
78  << CUBLAS_VER_MAJOR;
79 
80  std::string lib_name = oss.str();
81  return lib_name;
82 }
83 
84 
85 #ifdef __cplusplus
86  extern "C" {
87 #endif
88 
89 
90 // =============
91 // cublas Create
92 // =============
93 
96 
97 cublasStatus_t cublasCreate_v2(cublasHandle_t* handle)
98 {
99  if (cublasSymbols::cublasCreate == NULL)
100  {
101  std::string lib_name = cublasSymbols::get_lib_name();
102  const char* symbol_name = "cublasCreate_v2";
103 
105  dynamic_loading::load_symbol<cublasCreate_type>(
106  lib_name.c_str(),
107  symbol_name);
108  }
109 
110  return cublasSymbols::cublasCreate(handle);
111 }
112 
113 
114 // ==============
115 // cublas Destroy
116 // ==============
117 
118 
121 
122 cublasStatus_t cublasDestroy_v2(cublasHandle_t handle)
123 {
124  if (cublasSymbols::cublasDestroy == NULL)
125  {
126  std::string lib_name = cublasSymbols::get_lib_name();
127  const char* symbol_name = "cublasDestroy_v2";
128 
130  dynamic_loading::load_symbol<cublasDestroy_type>(
131  lib_name.c_str(),
132  symbol_name);
133  }
134 
135  return cublasSymbols::cublasDestroy(handle);
136 }
137 
138 // ===========
139 // cublasSgemv
140 // ===========
141 
144 
145 cublasStatus_t cublasSgemv_v2(
146  cublasHandle_t handle,
147  cublasOperation_t trans,
148  int m,
149  int n,
150  const float* alpha,
151  const float* A,
152  int lda,
153  const float* x,
154  int incx,
155  const float* beta,
156  float* y,
157  int incy)
158 {
159  if (cublasSymbols::cublasSgemv == NULL)
160  {
161  std::string lib_name = cublasSymbols::get_lib_name();
162  const char* symbol_name = "cublasSgemv_v2";
163 
165  dynamic_loading::load_symbol<cublasSgemv_type>(
166  lib_name.c_str(),
167  symbol_name);
168  }
169 
170  return cublasSymbols::cublasSgemv(handle, trans, m, n, alpha, A, lda, x,
171  incx, beta, y, incy);
172 }
173 
174 
175 // ===========
176 // cublasDgemv
177 // ===========
178 
181 
182 cublasStatus_t cublasDgemv_v2(
183  cublasHandle_t handle,
184  cublasOperation_t trans,
185  int m,
186  int n,
187  const double* alpha,
188  const double* A,
189  int lda,
190  const double* x,
191  int incx,
192  const double* beta,
193  double* y,
194  int incy)
195 {
196  if (cublasSymbols::cublasDgemv == NULL)
197  {
198  std::string lib_name = cublasSymbols::get_lib_name();
199  const char* symbol_name = "cublasDgemv_v2";
200 
202  dynamic_loading::load_symbol<cublasDgemv_type>(
203  lib_name.c_str(),
204  symbol_name);
205  }
206 
207  return cublasSymbols::cublasDgemv(handle, trans, m, n, alpha, A, lda, x,
208  incx, beta, y, incy);
209 }
210 
211 
212 // ===========
213 // cublasScopy
214 // ===========
215 
218 
219 cublasStatus_t cublasScopy(
220  cublasHandle_t handle, int n,
221  const float* x,
222  int incx,
223  float* y,
224  int incy)
225 {
226  if (cublasSymbols::cublasScopy == NULL)
227  {
228  std::string lib_name = cublasSymbols::get_lib_name();
229  const char* symbol_name = "cublasScopy_v2";
230 
232  dynamic_loading::load_symbol<cublasScopy_type>(
233  lib_name.c_str(),
234  symbol_name);
235  }
236 
237  return cublasSymbols::cublasScopy(handle, n, x, incx, y, incy);
238 }
239 
240 
241 // ===========
242 // cublasDcopy
243 // ===========
244 
247 
248 cublasStatus_t cublasDcopy(
249  cublasHandle_t handle, int n,
250  const double* x,
251  int incx,
252  double* y,
253  int incy)
254 {
255  if (cublasSymbols::cublasDcopy == NULL)
256  {
257  std::string lib_name = cublasSymbols::get_lib_name();
258  const char* symbol_name = "cublasDcopy_v2";
259 
261  dynamic_loading::load_symbol<cublasDcopy_type>(
262  lib_name.c_str(),
263  symbol_name);
264  }
265 
266  return cublasSymbols::cublasDcopy(handle, n, x, incx, y, incy);
267 }
268 
269 
270 // ===========
271 // cublasSaxpy
272 // ===========
273 
276 
277 cublasStatus_t cublasSaxpy(
278  cublasHandle_t handle,
279  int n,
280  const float *alpha,
281  const float *x,
282  int incx,
283  float *y,
284  int incy)
285 {
286  if (cublasSymbols::cublasSaxpy == NULL)
287  {
288  std::string lib_name = cublasSymbols::get_lib_name();
289  const char* symbol_name = "cublasSaxpy_v2";
290 
292  dynamic_loading::load_symbol<cublasSaxpy_type>(
293  lib_name.c_str(),
294  symbol_name);
295  }
296 
297  return cublasSymbols::cublasSaxpy(handle, n, alpha, x, incx, y, incy);
298 }
299 
300 
301 // ===========
302 // cublasDaxpy
303 // ===========
304 
307 
308 cublasStatus_t cublasDaxpy(
309  cublasHandle_t handle,
310  int n,
311  const double *alpha,
312  const double *x,
313  int incx,
314  double *y,
315  int incy)
316 {
317  if (cublasSymbols::cublasDaxpy == NULL)
318  {
319  std::string lib_name = cublasSymbols::get_lib_name();
320  const char* symbol_name = "cublasDaxpy_v2";
321 
323  dynamic_loading::load_symbol<cublasDaxpy_type>(
324  lib_name.c_str(),
325  symbol_name);
326  }
327 
328  return cublasSymbols::cublasDaxpy(handle, n, alpha, x, incx, y, incy);
329 }
330 
331 
332 // ==========
333 // cublasSdot
334 // ==========
335 
338 
339 cublasStatus_t cublasSdot(
340  cublasHandle_t handle,
341  int n,
342  const float *x,
343  int incx,
344  const float *y,
345  int incy,
346  float *result)
347 {
348  if (cublasSymbols::cublasSdot == NULL)
349  {
350  std::string lib_name = cublasSymbols::get_lib_name();
351  const char* symbol_name = "cublasSdot_v2";
352 
354  dynamic_loading::load_symbol<cublasSdot_type>(
355  lib_name.c_str(),
356  symbol_name);
357  }
358 
359  return cublasSymbols::cublasSdot(handle, n, x, incx, y, incy, result);
360 }
361 
362 
363 // ==========
364 // cublasDdot
365 // ==========
366 
369 
370 cublasStatus_t cublasDdot(
371  cublasHandle_t handle,
372  int n,
373  const double *x,
374  int incx,
375  const double *y,
376  int incy,
377  double *result)
378 {
379  if (cublasSymbols::cublasDdot == NULL)
380  {
381  std::string lib_name = cublasSymbols::get_lib_name();
382  const char* symbol_name = "cublasDdot_v2";
383 
385  dynamic_loading::load_symbol<cublasDdot_type>(
386  lib_name.c_str(),
387  symbol_name);
388  }
389 
390  return cublasSymbols::cublasDdot(handle, n, x, incx, y, incy, result);
391 }
392 
393 
394 // ===========
395 // cublasSnrm2
396 // ===========
397 
400 
401 cublasStatus_t cublasSnrm2(
402  cublasHandle_t handle,
403  int n,
404  const float *x,
405  int incx,
406  float *result)
407 {
408  if (cublasSymbols::cublasSnrm2 == NULL)
409  {
410  std::string lib_name = cublasSymbols::get_lib_name();
411  const char* symbol_name = "cublasSnrm2_v2";
412 
414  dynamic_loading::load_symbol<cublasSnrm2_type>(
415  lib_name.c_str(),
416  symbol_name);
417  }
418 
419  return cublasSymbols::cublasSnrm2(handle, n, x, incx, result);
420 }
421 
422 
423 // ===========
424 // cublasDnrm2
425 // ===========
426 
429 
430 cublasStatus_t cublasDnrm2(
431  cublasHandle_t handle,
432  int n,
433  const double *x,
434  int incx,
435  double *result)
436 {
437  if (cublasSymbols::cublasDnrm2 == NULL)
438  {
439  std::string lib_name = cublasSymbols::get_lib_name();
440  const char* symbol_name = "cublasDnrm2_v2";
441 
443  dynamic_loading::load_symbol<cublasDnrm2_type>(
444  lib_name.c_str(),
445  symbol_name);
446  }
447 
448  return cublasSymbols::cublasDnrm2(handle, n, x, incx, result);
449 }
450 
451 
452 // ===========
453 // cublasSscal
454 // ===========
455 
458 
459 cublasStatus_t cublasSscal(
460  cublasHandle_t handle,
461  int n,
462  const float *alpha,
463  float *x,
464  int incx)
465 {
466  if (cublasSymbols::cublasSscal == NULL)
467  {
468  std::string lib_name = cublasSymbols::get_lib_name();
469  const char* symbol_name = "cublasSscal_v2";
470 
472  dynamic_loading::load_symbol<cublasSscal_type>(
473  lib_name.c_str(),
474  symbol_name);
475  }
476 
477  return cublasSymbols::cublasSscal(handle, n, alpha, x, incx);
478 }
479 
480 
481 // ===========
482 // cublasDscal
483 // ===========
484 
487 
488 cublasStatus_t cublasDscal(
489  cublasHandle_t handle,
490  int n,
491  const double *alpha,
492  double *x,
493  int incx)
494 {
495  if (cublasSymbols::cublasDscal == NULL)
496  {
497  std::string lib_name = cublasSymbols::get_lib_name();
498  const char* symbol_name = "cublasDscal_v2";
499 
501  dynamic_loading::load_symbol<cublasDscal_type>(
502  lib_name.c_str(),
503  symbol_name);
504  }
505 
506  return cublasSymbols::cublasDscal(handle, n, alpha, x, incx);
507 }
508 
509 #ifdef __cplusplus
510  }
511 #endif
static cublasSaxpy_type cublasSaxpy
static cublasDdot_type cublasDdot
static cublasScopy_type cublasScopy
static cublasSscal_type cublasSscal
static cublasDaxpy_type cublasDaxpy
static cublasCreate_type cublasCreate
static cublasDestroy_type cublasDestroy
static cublasDnrm2_type cublasDnrm2
static cublasSdot_type cublasSdot
static cublasDcopy_type cublasDcopy
static cublasSnrm2_type cublasSnrm2
static cublasDscal_type cublasDscal
static cublasSgemv_type cublasSgemv
static std::string get_lib_name()
Returns the name of cublas shared library.
static cublasDgemv_type cublasDgemv
cublasStatus_t cublasDcopy(cublasHandle_t handle, int n, const double *x, int incx, double *y, int incy)
Definition of CUDA's cublasDcopy function using dynamically loaded cublas library.
cublasStatus_t cublasSgemv_v2(cublasHandle_t handle, cublasOperation_t trans, int m, int n, const float *alpha, const float *A, int lda, const float *x, int incx, const float *beta, float *y, int incy)
Definition of CUDA's cublasSgemv function using dynamically loaded cublas library.
cublasStatus_t cublasSscal(cublasHandle_t handle, int n, const float *alpha, float *x, int incx)
Definition of CUDA's cublasSscal function using dynamically loaded cublas library.
cublasStatus_t cublasDscal(cublasHandle_t handle, int n, const double *alpha, double *x, int incx)
Definition of CUDA's cublasDscal function using dynamically loaded cublas library.
cublasStatus_t cublasSdot(cublasHandle_t handle, int n, const float *x, int incx, const float *y, int incy, float *result)
Definition of CUDA's cublasSdot function using dynamically loaded cublas library.
cublasStatus_t cublasSnrm2(cublasHandle_t handle, int n, const float *x, int incx, float *result)
Definition of CUDA's cublasSnrm2 function using dynamically loaded cublas library.
cublasStatus_t cublasSaxpy(cublasHandle_t handle, int n, const float *alpha, const float *x, int incx, float *y, int incy)
Definition of CUDA's cublasSaxpy function using dynamically loaded cublas library.
cublasStatus_t cublasDaxpy(cublasHandle_t handle, int n, const double *alpha, const double *x, int incx, double *y, int incy)
Definition of CUDA's cublasDaxpy function using dynamically loaded cublas library.
cublasStatus_t cublasScopy(cublasHandle_t handle, int n, const float *x, int incx, float *y, int incy)
Definition of CUDA's cublasScopy function using dynamically loaded cublas library.
cublasStatus_t cublasDestroy_v2(cublasHandle_t handle)
Definition of CUDA's cublasDestroy function using dynamically loaded cublas library.
cublasStatus_t cublasDnrm2(cublasHandle_t handle, int n, const double *x, int incx, double *result)
Definition of CUDA's cublasDnrm2 function using dynamically loaded cublas library.
cublasStatus_t cublasDgemv_v2(cublasHandle_t handle, cublasOperation_t trans, int m, int n, const double *alpha, const double *A, int lda, const double *x, int incx, const double *beta, double *y, int incy)
Definition of CUDA's cublasDgemv function using dynamically loaded cublas library.
cublasStatus_t cublasDdot(cublasHandle_t handle, int n, const double *x, int incx, const double *y, int incy, double *result)
Definition of CUDA's cublasDdot function using dynamically loaded cublas library.
cublasStatus_t cublasCreate_v2(cublasHandle_t *handle)
Definition of CUDA's cublasCreate function using dynamically loaded cublas library.
cublasStatus_t(* cublasDestroy_type)(cublasHandle_t handle)
Definition: cublas_types.h:32
cublasStatus_t(* cublasDgemv_type)(cublasHandle_t handle, cublasOperation_t trans, int m, int n, const double *alpha, const double *A, int lda, const double *x, int incx, const double *beta, double *y, int incy)
Definition: cublas_types.h:50
cublasStatus_t(* cublasDcopy_type)(cublasHandle_t handle, int n, const double *x, int incx, double *y, int incy)
Definition: cublas_types.h:73
cublasStatus_t(* cublasDaxpy_type)(cublasHandle_t handle, int n, const double *alpha, const double *x, int incx, double *y, int incy)
Definition: cublas_types.h:91
cublasStatus_t(* cublasDscal_type)(cublasHandle_t handle, int n, const double *alpha, double *x, int incx)
Definition: cublas_types.h:145
cublasStatus_t(* cublasSaxpy_type)(cublasHandle_t handle, int n, const float *alpha, const float *x, int incx, float *y, int incy)
Definition: cublas_types.h:81
cublasStatus_t(* cublasCreate_type)(cublasHandle_t *handle)
Definition: cublas_types.h:29
cublasStatus_t(* cublasSscal_type)(cublasHandle_t handle, int n, const float *alpha, float *x, int incx)
Definition: cublas_types.h:137
cublasStatus_t(* cublasSgemv_type)(cublasHandle_t handle, cublasOperation_t trans, int m, int n, const float *alpha, const float *A, int lda, const float *x, int incx, const float *beta, float *y, int incy)
Definition: cublas_types.h:35
cublasStatus_t(* cublasSdot_type)(cublasHandle_t handle, int n, const float *x, int incx, const float *y, int incy, float *result)
Definition: cublas_types.h:101
cublasStatus_t(* cublasScopy_type)(cublasHandle_t handle, int n, const float *x, int incx, float *y, int incy)
Definition: cublas_types.h:65
cublasStatus_t(* cublasSnrm2_type)(cublasHandle_t handle, int n, const float *x, int incx, float *result)
Definition: cublas_types.h:121
cublasStatus_t(* cublasDdot_type)(cublasHandle_t handle, int n, const double *x, int incx, const double *y, int incy, double *result)
Definition: cublas_types.h:111
cublasStatus_t(* cublasDnrm2_type)(cublasHandle_t handle, int n, const double *x, int incx, double *result)
Definition: cublas_types.h:129