imate
C++/CUDA Reference
cudart_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 
17 #include "./cudart_symbols.h"
18 #include <cuda_runtime_api.h> // cudaError_t, cudaEvent_t, cudaStream_t,
19  // cudaDeviceProp, cudaMemcpyKind
20 #include <cstdlib> // NULL
21 #include <sstream> // std::ostringstream
22 #include "./dynamic_loading.h" // dynamic_loading
23 
24 
25 // =========================
26 // Initialize static members
27 // =========================
28 
43  NULL;
45 
46 
47 // ============
48 // get lib name
49 // ============
50 
53 
55 {
56  // Get the extension name of a shared library depending on OS
57  std::string lib_extension;
58 
59  #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || \
60  defined(__NT__)
61  lib_extension = "lib";
62  #elif __APPLE__
63  lib_extension = "dylib";
64  #elif __linux__
65  lib_extension = "so";
66  #else
67  #error "Unknown compiler"
68  #endif
69 
70  // Check cudart version
71  // #ifndef CUDART_VERSION
72  // #error "CUDART_VERSION is not defined."
73  // #endif
74 
75  // CUDART_VERSION is something like 11020, which means major version 11
76  // and minor version 2. To get the major version, strip off last three
77  // digits.
78  // int cuda_version_major = CUDART_VERSION / 1000;
79 
80  // cudart shared library base name
81  std::string lib_base_name = "libcudart";
82 
83  // Construct the lib name
84  std::ostringstream oss;
85  oss << lib_base_name << "." << lib_extension;
86  // oss << lib_base_name << "." << lib_extension << "."
87  // << std::to_string(cuda_version_major);
88 
89  std::string lib_name = oss.str();
90  return lib_name;
91 }
92 
93 
94 #ifdef __cplusplus
95  extern "C" {
96 #endif
97 
98 
99 // =================
100 // cuda Event Create
101 // =================
102 
105 
106 cudaError_t cudaEventCreate(cudaEvent_t* event)
107 {
108  if (cudartSymbols::cudaEventCreate == NULL)
109  {
110  std::string lib_name = cudartSymbols::get_lib_name();
111  const char* symbol_name = "cudaEventCreate";
112 
114  dynamic_loading::load_symbol<cudaEventCreate_type>(
115  lib_name.c_str(),
116  symbol_name);
117  }
118 
119  return cudartSymbols::cudaEventCreate(event);
120 }
121 
122 
123 // ==================
124 // cuda Event Destroy
125 // ==================
126 
129 
130 cudaError_t cudaEventDestroy(cudaEvent_t event)
131 {
133  {
134  std::string lib_name = cudartSymbols::get_lib_name();
135  const char* symbol_name = "cudaEventDestroy";
136 
138  dynamic_loading::load_symbol<cudaEventDestroy_type>(
139  lib_name.c_str(),
140  symbol_name);
141  }
142 
143  return cudartSymbols::cudaEventDestroy(event);
144 }
145 
146 
147 // =======================
148 // cuda Event Elapsed Time
149 // =======================
150 
153 
155  float* ms,
156  cudaEvent_t start,
157  cudaEvent_t end)
158 {
160  {
161  std::string lib_name = cudartSymbols::get_lib_name();
162  const char* symbol_name = "cudaEventElapsedTime";
163 
165  dynamic_loading::load_symbol<cudaEventElapsedTime_type>(
166  lib_name.c_str(),
167  symbol_name);
168  }
169 
170  return cudartSymbols::cudaEventElapsedTime(ms, start, end);
171 }
172 
173 
174 // =================
175 // cuda Event Record
176 // =================
177 
180 
181 cudaError_t cudaEventRecord(cudaEvent_t event, cudaStream_t stream)
182 {
183  if (cudartSymbols::cudaEventRecord == NULL)
184  {
185  std::string lib_name = cudartSymbols::get_lib_name();
186  const char* symbol_name = "cudaEventRecord";
187 
189  dynamic_loading::load_symbol<cudaEventRecord_type>(
190  lib_name.c_str(),
191  symbol_name);
192  }
193 
194  return cudartSymbols::cudaEventRecord(event, stream);
195 }
196 
197 
198 // ======================
199 // cuda Event Synchronize
200 // ======================
201 
204 
205 cudaError_t cudaEventSynchronize(cudaEvent_t event)
206 {
208  {
209  std::string lib_name = cudartSymbols::get_lib_name();
210  const char* symbol_name = "cudaEventSynchronize";
211 
213  dynamic_loading::load_symbol<cudaEventSynchronize_type>(
214  lib_name.c_str(),
215  symbol_name);
216  }
217 
219 }
220 
221 
222 // ===============
223 // cuda Get Device
224 // ===============
225 
228 
229 cudaError_t cudaGetDevice(int* device)
230 {
231  if (cudartSymbols::cudaGetDevice == NULL)
232  {
233  std::string lib_name = cudartSymbols::get_lib_name();
234  const char* symbol_name = "cudaGetDevice";
235 
237  dynamic_loading::load_symbol<cudaGetDevice_type>(
238  lib_name.c_str(),
239  symbol_name);
240  }
241 
242  return cudartSymbols::cudaGetDevice(device);
243 }
244 
245 
246 // =====================
247 // cuda Get Device Count
248 // =====================
249 
252 
253 cudaError_t cudaGetDeviceCount(int* count)
254 {
256  {
257  std::string lib_name = cudartSymbols::get_lib_name();
258  const char* symbol_name = "cudaGetDeviceCount";
259 
261  dynamic_loading::load_symbol<cudaGetDeviceCount_type>(
262  lib_name.c_str(),
263  symbol_name);
264  }
265 
266  return cudartSymbols::cudaGetDeviceCount(count);
267 }
268 
269 
270 // ==========================
271 // cuda Get Device Properties
272 // ==========================
273 
276 
278  cudaDeviceProp* prop,
279  int device)
280 {
282  {
283  std::string lib_name = cudartSymbols::get_lib_name();
284  const char* symbol_name = "cudaGetDeviceProperties";
285 
287  dynamic_loading::load_symbol<cudaGetDeviceProperties_type>(
288  lib_name.c_str(),
289  symbol_name);
290  }
291 
292  return cudartSymbols::cudaGetDeviceProperties(prop, device);
293 }
294 
295 
296 // =========
297 // cuda Free
298 // =========
299 
302 
303 cudaError_t cudaFree(void* devPtr)
304 {
305  if (cudartSymbols::cudaFree == NULL)
306  {
307  std::string lib_name = cudartSymbols::get_lib_name();
308  const char* symbol_name = "cudaFree";
309 
310  cudartSymbols::cudaFree = dynamic_loading::load_symbol<cudaFree_type>(
311  lib_name.c_str(),
312  symbol_name);
313  }
314 
315  return cudartSymbols::cudaFree(devPtr);
316 }
317 
318 
319 // ===========
320 // cuda Malloc
321 // ===========
322 
325 
326 cudaError_t cudaMalloc(void** devPtr, size_t size)
327 {
328  if (cudartSymbols::cudaMalloc == NULL)
329  {
330  std::string lib_name = cudartSymbols::get_lib_name();
331  const char* symbol_name = "cudaMalloc";
332 
334  dynamic_loading::load_symbol<cudaMalloc_type>(
335  lib_name.c_str(),
336  symbol_name);
337  }
338 
339  return cudartSymbols::cudaMalloc(devPtr, size);
340 }
341 
342 
343 // ===========
344 // cuda Memcpy
345 // ===========
346 
349 
350 cudaError_t cudaMemcpy(
351  void* dst,
352  const void* src,
353  size_t count,
354  cudaMemcpyKind kind)
355 {
356  if (cudartSymbols::cudaMemcpy == NULL)
357  {
358  std::string lib_name = cudartSymbols::get_lib_name();
359  const char* symbol_name = "cudaMemcpy";
360 
362  dynamic_loading::load_symbol<cudaMemcpy_type>(
363  lib_name.c_str(),
364  symbol_name);
365  }
366 
367  return cudartSymbols::cudaMemcpy(dst, src, count, kind);
368 }
369 
370 
371 // ===========
372 // cuda Memcpy
373 // ===========
374 
377 
378 cudaError_t cudaSetDevice(int device)
379 {
380  if (cudartSymbols::cudaSetDevice == NULL)
381  {
382  std::string lib_name = cudartSymbols::get_lib_name();
383  const char* symbol_name = "cudaSetDevice";
384 
386  dynamic_loading::load_symbol<cudaSetDevice_type>(
387  lib_name.c_str(),
388  symbol_name);
389  }
390 
391  return cudartSymbols::cudaSetDevice(device);
392 }
393 
394 
395 // ========================
396 // cuda Register Fat Binary
397 // ========================
398 
401 
402 void** __cudaRegisterFatBinary(void *fatCubin)
403 {
405  {
406  std::string lib_name = cudartSymbols::get_lib_name();
407  const char* symbol_name = "__cudaRegisterFatBinary";
408 
410  dynamic_loading::load_symbol<__cudaRegisterFatBinary_type>(
411  lib_name.c_str(),
412  symbol_name);
413  }
414 
416 }
417 
418 
419 // ============================
420 // cuda Register Fat Binary End
421 // ============================
422 
425 
426 void __cudaRegisterFatBinaryEnd(void **fatCubinHandle)
427 {
429  {
430  std::string lib_name = cudartSymbols::get_lib_name();
431  const char* symbol_name = "__cudaRegisterFatBinaryEnd";
432 
434  dynamic_loading::load_symbol<__cudaRegisterFatBinaryEnd_type>(
435  lib_name.c_str(),
436  symbol_name);
437  }
438 
439  return cudartSymbols::__cudaRegisterFatBinaryEnd(fatCubinHandle);
440 }
441 
442 
443 // ==========================
444 // cuda Unregister Fat Binary
445 // ==========================
446 
449 
450 void __cudaUnregisterFatBinary(void **fatCubinHandle)
451 {
453  {
454  std::string lib_name = cudartSymbols::get_lib_name();
455  const char* symbol_name = "__cudaUnregisterFatBinary";
456 
458  dynamic_loading::load_symbol<__cudaUnregisterFatBinary_type>(
459  lib_name.c_str(),
460  symbol_name);
461  }
462 
463  return cudartSymbols::__cudaUnregisterFatBinary(fatCubinHandle);
464 }
465 
466 
467 #ifdef __cplusplus
468  }
469 #endif
static cudaEventCreate_type cudaEventCreate
static cudaEventSynchronize_type cudaEventSynchronize
static __cudaRegisterFatBinaryEnd_type __cudaRegisterFatBinaryEnd
static __cudaUnregisterFatBinary_type __cudaUnregisterFatBinary
static cudaMalloc_type cudaMalloc
static cudaMemcpy_type cudaMemcpy
static __cudaRegisterFatBinary_type __cudaRegisterFatBinary
static cudaGetDevice_type cudaGetDevice
static cudaEventRecord_type cudaEventRecord
static cudaEventDestroy_type cudaEventDestroy
static cudaGetDeviceCount_type cudaGetDeviceCount
static std::string get_lib_name()
Returns the name of cudart shared library.
static cudaSetDevice_type cudaSetDevice
static cudaFree_type cudaFree
static cudaEventElapsedTime_type cudaEventElapsedTime
static cudaGetDeviceProperties_type cudaGetDeviceProperties
cudaError_t cudaEventSynchronize(cudaEvent_t event)
Definition of CUDA's cudaEventSynchronize function using dynamically loaded cudart library.
void ** __cudaRegisterFatBinary(void *fatCubin)
Definition of CUDA's __cudaRegisterFatBinary function using dynamically loaded cudart library.
cudaError_t cudaEventElapsedTime(float *ms, cudaEvent_t start, cudaEvent_t end)
Definition of CUDA's cudaEventElapsedTime function using dynamically loaded cudart library.
cudaError_t cudaGetDevice(int *device)
Definition of CUDA's cudaGetDevice function using dynamically loaded cudart library.
cudaError_t cudaEventCreate(cudaEvent_t *event)
Definition of CUDA's cudaEventCreate function using dynamically loaded cudart library.
cudaError_t cudaSetDevice(int device)
Definition of CUDA's cudaSetDevice function using dynamically loaded cudart library.
void __cudaRegisterFatBinaryEnd(void **fatCubinHandle)
Definition of CUDA's __cudaRegisterFatBinaryEnd function using dynamically loaded cudart library.
cudaError_t cudaEventDestroy(cudaEvent_t event)
Definition of CUDA's cudaEventDestroy function using dynamically loaded cudart library.
cudaError_t cudaGetDeviceCount(int *count)
Definition of CUDA's cudaGetDeviceCount function using dynamically loaded cudart library.
cudaError_t cudaEventRecord(cudaEvent_t event, cudaStream_t stream)
Definition of CUDA's cudaEventRecord function using dynamically loaded cudart library.
cudaError_t cudaFree(void *devPtr)
Definition of CUDA's cudaFree function using dynamically loaded cudart library.
cudaError_t cudaMemcpy(void *dst, const void *src, size_t count, cudaMemcpyKind kind)
Definition of CUDA's cudaMemcpy function using dynamically loaded cudart library.
cudaError_t cudaMalloc(void **devPtr, size_t size)
Definition of CUDA's cudaMalloc function using dynamically loaded cudart library.
void __cudaUnregisterFatBinary(void **fatCubinHandle)
Definition of CUDA's __cudaUnregisterFatBinary function using dynamically loaded cudart library.
cudaError_t cudaGetDeviceProperties(cudaDeviceProp *prop, int device)
Definition of CUDA's cudaGetDeviceProperties function using dynamically loaded cudart library.
void(* __cudaRegisterFatBinaryEnd_type)(void **fatCubinHandle)
Definition: cudart_types.h:45
cudaError_t(* cudaGetDeviceCount_type)(int *count)
Definition: cudart_types.h:36
cudaError_t(* cudaEventSynchronize_type)(cudaEvent_t event)
Definition: cudart_types.h:34
cudaError_t(* cudaGetDevice_type)(int *device)
Definition: cudart_types.h:35
cudaError_t(* cudaSetDevice_type)(int device)
Definition: cudart_types.h:43
cudaError_t(* cudaEventRecord_type)(cudaEvent_t event, cudaStream_t stream)
Definition: cudart_types.h:32
cudaError_t(* cudaFree_type)(void *devPtr)
Definition: cudart_types.h:39
cudaError_t(* cudaGetDeviceProperties_type)(cudaDeviceProp *prop, int device)
Definition: cudart_types.h:37
void **(* __cudaRegisterFatBinary_type)(void *fatCubin)
Definition: cudart_types.h:44
void(* __cudaUnregisterFatBinary_type)(void **fatCubinHandle)
Definition: cudart_types.h:46
cudaError_t(* cudaEventDestroy_type)(cudaEvent_t event)
Definition: cudart_types.h:29
cudaError_t(* cudaEventCreate_type)(cudaEvent_t *event)
Definition: cudart_types.h:28
cudaError_t(* cudaMemcpy_type)(void *dst, const void *src, size_t count, cudaMemcpyKind kind)
Definition: cudart_types.h:41
cudaError_t(* cudaMalloc_type)(void **devPtr, size_t size)
Definition: cudart_types.h:40
cudaError_t(* cudaEventElapsedTime_type)(float *ms, cudaEvent_t start, cudaEvent_t end)
Definition: cudart_types.h:30