Debian Codegraph @

offload/plugins-nextgen/cuda/dynamic_cuda/cuda.cpp

raw
1 //===--- cuda/dynamic_cuda/cuda.pp ------------------------------- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implement subset of cuda api by calling into cuda library via dlopen
10 // Does the dlopen/dlsym calls as part of the call to cuInit
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/DynamicLibrary.h"
15
16 #include "Shared/Debug.h"
17
18 #include "DLWrap.h"
19 #include "cuda.h"
20
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24
25 DLWRAP_INITIALIZE()
26
27 DLWRAP_INTERNAL(cuInit, 1)
28
29 DLWRAP(cuCtxGetDevice, 1)
30 DLWRAP(cuDeviceGet, 2)
31 DLWRAP(cuDeviceGetAttribute, 3)
32 DLWRAP(cuDeviceGetCount, 1)
33 DLWRAP(cuFuncGetAttribute, 3)
34 DLWRAP(cuFuncSetAttribute, 3)
35
36 // Device info
37 DLWRAP(cuDeviceGetName, 3)
38 DLWRAP(cuDeviceGetUuid, 2)
39 DLWRAP(cuDeviceTotalMem, 2)
40 DLWRAP(cuDriverGetVersion, 1)
41
42 DLWRAP(cuGetErrorString, 2)
43 DLWRAP(cuLaunchKernel, 11)
44 DLWRAP(cuLaunchHostFunc, 3)
45
46 DLWRAP(cuMemAlloc, 2)
47 DLWRAP(cuMemAllocHost, 2)
48 DLWRAP(cuMemAllocManaged, 3)
49 DLWRAP(cuMemAllocAsync, 3)
50
51 DLWRAP(cuMemcpyDtoDAsync, 4)
52 DLWRAP(cuMemcpyDtoH, 3)
53 DLWRAP(cuMemcpyDtoHAsync, 4)
54 DLWRAP(cuMemcpyHtoD, 3)
55 DLWRAP(cuMemcpyHtoDAsync, 4)
56
57 DLWRAP(cuMemsetD8Async, 4)
58 DLWRAP(cuMemsetD16Async, 4)
59 DLWRAP(cuMemsetD32Async, 4)
60 DLWRAP(cuMemsetD2D8Async, 6)
61 DLWRAP(cuMemsetD2D16Async, 6)
62 DLWRAP(cuMemsetD2D32Async, 6)
63
64 DLWRAP(cuMemFree, 1)
65 DLWRAP(cuMemFreeHost, 1)
66 DLWRAP(cuMemFreeAsync, 2)
67
68 DLWRAP(cuModuleGetFunction, 3)
69 DLWRAP(cuModuleGetGlobal, 4)
70
71 DLWRAP(cuModuleUnload, 1)
72 DLWRAP(cuStreamCreate, 2)
73 DLWRAP(cuStreamDestroy, 1)
74 DLWRAP(cuStreamSynchronize, 1)
75 DLWRAP(cuStreamQuery, 1)
76 DLWRAP(cuStreamAddCallback, 4)
77 DLWRAP(cuCtxSetCurrent, 1)
78 DLWRAP(cuDevicePrimaryCtxRelease, 1)
79 DLWRAP(cuDevicePrimaryCtxGetState, 3)
80 DLWRAP(cuDevicePrimaryCtxSetFlags, 2)
81 DLWRAP(cuDevicePrimaryCtxRetain, 2)
82 DLWRAP(cuModuleLoadDataEx, 5)
83 DLWRAP(cuOccupancyMaxPotentialBlockSize, 6)
84 DLWRAP(cuFuncGetParamInfo, 4)
85
86 DLWRAP(cuDeviceCanAccessPeer, 3)
87 DLWRAP(cuCtxEnablePeerAccess, 2)
88 DLWRAP(cuMemcpyPeerAsync, 6)
89
90 DLWRAP(cuCtxGetLimit, 2)
91 DLWRAP(cuCtxSetLimit, 2)
92
93 DLWRAP(cuEventCreate, 2)
94 DLWRAP(cuEventRecord, 2)
95 DLWRAP(cuEventQuery, 1)
96 DLWRAP(cuStreamWaitEvent, 3)
97 DLWRAP(cuEventSynchronize, 1)
98 DLWRAP(cuEventDestroy, 1)
99
100 DLWRAP_FINALIZE()
101
102 DLWRAP(cuMemUnmap, 2)
103 DLWRAP(cuMemRelease, 1)
104 DLWRAP(cuMemAddressFree, 2)
105 DLWRAP(cuMemGetInfo, 2)
106 DLWRAP(cuMemAddressReserve, 5)
107 DLWRAP(cuMemMap, 5)
108 DLWRAP(cuMemCreate, 4)
109 DLWRAP(cuMemSetAccess, 4)
110 DLWRAP(cuMemGetAllocationGranularity, 3)
111
112 #ifndef DYNAMIC_CUDA_PATH
113 #define DYNAMIC_CUDA_PATH "libcuda.so"
114 #endif
115
116 #ifndef TARGET_NAME
117 #define TARGET_NAME CUDA
118 #endif
119 #ifndef DEBUG_PREFIX
120 #define DEBUG_PREFIX "Target " GETNAME(TARGET_NAME) " RTL"
121 #endif
122
123 static bool checkForCUDA() {
124 // return true if dlopen succeeded and all functions found
125
126 // Prefer _v2 versions of functions if found in the library
127 std::unordered_map<std::string, const char *> TryFirst = {
128 {"cuMemAlloc", "cuMemAlloc_v2"},
129 {"cuMemFree", "cuMemFree_v2"},
130 {"cuMemcpyDtoH", "cuMemcpyDtoH_v2"},
131 {"cuMemcpyHtoD", "cuMemcpyHtoD_v2"},
132 {"cuStreamDestroy", "cuStreamDestroy_v2"},
133 {"cuModuleGetGlobal", "cuModuleGetGlobal_v2"},
134 {"cuMemcpyDtoHAsync", "cuMemcpyDtoHAsync_v2"},
135 {"cuMemcpyDtoDAsync", "cuMemcpyDtoDAsync_v2"},
136 {"cuMemcpyHtoDAsync", "cuMemcpyHtoDAsync_v2"},
137 {"cuDevicePrimaryCtxRelease", "cuDevicePrimaryCtxRelease_v2"},
138 {"cuDevicePrimaryCtxSetFlags", "cuDevicePrimaryCtxSetFlags_v2"},
139 };
140
141 const char *CudaLib = DYNAMIC_CUDA_PATH;
142 std::string ErrMsg;
143 auto DynlibHandle = std::make_unique<llvm::sys::DynamicLibrary>(
144 llvm::sys::DynamicLibrary::getPermanentLibrary(CudaLib, &ErrMsg));
145 if (!DynlibHandle->isValid()) {
146 DP("Unable to load library '%s': %s!\n", CudaLib, ErrMsg.c_str());
147 return false;
148 }
149
150 for (size_t I = 0; I < dlwrap::size(); I++) {
151 const char *Sym = dlwrap::symbol(I);
152
153 auto It = TryFirst.find(Sym);
154 if (It != TryFirst.end()) {
155 const char *First = It->second;
156 void *P = DynlibHandle->getAddressOfSymbol(First);
157 if (P) {
158 DP("Implementing %s with dlsym(%s) -> %p\n", Sym, First, P);
159 *dlwrap::pointer(I) = P;
160 continue;
161 }
162 }
163
164 void *P = DynlibHandle->getAddressOfSymbol(Sym);
165 if (P == nullptr) {
166 DP("Unable to find '%s' in '%s'!\n", Sym, CudaLib);
167 return false;
168 }
169 DP("Implementing %s with dlsym(%s) -> %p\n", Sym, Sym, P);
170
171 *dlwrap::pointer(I) = P;
172 }
173
174 return true;
175 }
176
177 CUresult cuInit(unsigned X) {
178 // Note: Called exactly once from cuda rtl.cpp in a global constructor so
179 // does not need to handle being called repeatedly or concurrently
180 if (!checkForCUDA()) {
181 return CUDA_ERROR_INVALID_HANDLE;
182 }
183 return dlwrap_cuInit(X);
184 }