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