chrono_batch_render/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 5
0.0%
Branches:
0 / 6
0.0%
Functions:
0 / 1
0.0%

device/cuda/api.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2 #pragma once
3
4 #include <cstddef>
5 #include <cstdint>
6 #include <string>
7
8 #include "chrono_batch_render/device/compute/runtime_compiler.h"
9 #include "chrono_batch_render/util/version.h"
10
11 namespace chrono::batch_render::device::cuda {
12
13 using CUresult = int;
14 using CUdevice = int;
15 using CUdeviceptr = uint64_t;
16 using CUcontext = struct CUctx_st*;
17 using CUstream = struct CUstream_st*;
18 using CUevent = struct CUevent_st*;
19 using CUmodule = struct CUmod_st*;
20 using CUfunction = struct CUfunc_st*;
21 using CUmemoryPool = struct CUmemPoolHandle_st*;
22
23 struct CUmemLocation {
24 int type;
25 int id;
26 };
27
28 struct CUmemPoolProps {
29 int allocType;
30 int handleTypes;
31 CUmemLocation location;
32 void* win32SecurityAttributes;
33 size_t maxSize;
34 unsigned char reserved[56];
35 };
36
37 using CUgraph = struct CUgraph_st*;
38 using CUgraphExec = struct CUgraphExec_st*;
39
40 struct CUuuid {
41 unsigned char bytes[16];
42 };
43
44 using CUexternalSemaphore = struct CUextSemaphore_st*;
45 constexpr int CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_FD = 9;
46
47 struct CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC {
48 int type;
49
50 union {
51 int fd;
52
53 struct {
54 void* handle;
55 const void* name;
56 } win32;
57
58 const void* nvSciSyncObj;
59 } handle;
60
61 unsigned flags;
62 unsigned reserved[16];
63 };
64
65 struct CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS {
66 struct {
67 struct {
68 unsigned long long value;
69 } fence;
70
71 union {
72 void* fence;
73 unsigned long long reserved;
74 } nvSciSync;
75
76 struct {
77 unsigned long long key;
78 } keyedMutex;
79
80 unsigned reserved[12];
81 } params;
82
83 unsigned flags;
84 unsigned reserved[16];
85 };
86
87 struct CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS {
88 struct {
89 struct {
90 unsigned long long value;
91 } fence;
92
93 union {
94 void* fence;
95 unsigned long long reserved;
96 } nvSciSync;
97
98 struct {
99 unsigned long long key;
100 unsigned timeoutMs;
101 } keyedMutex;
102
103 unsigned reserved[10];
104 } params;
105
106 unsigned flags;
107 unsigned reserved[16];
108 };
109
110 using CUdevice_attribute = int;
111
112 using CUexternalMemory = struct CUextMemory_st*;
113 constexpr int CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD = 1;
114 constexpr unsigned CUDA_EXTERNAL_MEMORY_DEDICATED = 0x1;
115
116 struct CUDA_EXTERNAL_MEMORY_HANDLE_DESC {
117 int type;
118
119 union {
120 int fd;
121
122 struct {
123 void* handle;
124 const void* name;
125 } win32;
126
127 const void* nvSciBufObject;
128 } handle;
129
130 unsigned long long size;
131 unsigned flags;
132 unsigned reserved[16];
133 };
134
135 static_assert(sizeof(CUDA_EXTERNAL_MEMORY_HANDLE_DESC) == 104);
136
137 struct CUDA_EXTERNAL_MEMORY_BUFFER_DESC {
138 unsigned long long offset;
139 unsigned long long size;
140 unsigned flags;
141 unsigned reserved[16];
142 };
143
144 static_assert(sizeof(CUDA_EXTERNAL_MEMORY_BUFFER_DESC) == 88);
145
146 constexpr CUresult CUDA_SUCCESS = 0;
147 constexpr CUresult CUDA_ERROR_OUT_OF_MEMORY = 2;
148 constexpr CUresult CUDA_ERROR_NO_DEVICE = 100;
149 constexpr CUresult CUDA_ERROR_NOT_FOUND = 500;
150 constexpr CUresult CUDA_ERROR_NOT_READY = 600;
151
152 constexpr unsigned CU_MEMHOSTALLOC_DEVICEMAP = 2;
153 constexpr unsigned CU_MEMHOSTALLOC_WRITECOMBINED = 4;
154 constexpr unsigned CU_STREAM_NON_BLOCKING = 1;
155 constexpr unsigned CU_EVENT_DISABLE_TIMING = 2;
156 constexpr int CU_MEM_ALLOCATION_TYPE_PINNED = 1;
157 constexpr int CU_MEM_LOCATION_TYPE_DEVICE = 1;
158 constexpr int CU_MEMPOOL_ATTR_RELEASE_THRESHOLD = 4;
159 constexpr unsigned CU_EVENT_RECORD_EXTERNAL = 1;
160 constexpr int CU_STREAM_CAPTURE_MODE_THREAD_LOCAL = 1;
161
162 constexpr int CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES = 1;
163 constexpr int CU_FUNC_ATTRIBUTE_LOCAL_SIZE_BYTES = 3;
164 constexpr int CU_FUNC_ATTRIBUTE_NUM_REGS = 4;
165
166 constexpr uintptr_t CU_LAUNCH_PARAM_END = 0;
167 constexpr uintptr_t CU_LAUNCH_PARAM_BUFFER_POINTER = 1;
168 constexpr uintptr_t CU_LAUNCH_PARAM_BUFFER_SIZE = 2;
169
170 ✗ constexpr bool LostDevice(CUresult result) {
171
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ return result == 214 || result == 700 || result == 702 || (result >= 714 && result <= 719);
172 }
173
174 constexpr CUdevice_attribute CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_BLOCK = 1;
175 constexpr CUdevice_attribute CU_DEVICE_ATTRIBUTE_WARP_SIZE = 10;
176 constexpr CUdevice_attribute CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT = 16;
177 constexpr CUdevice_attribute CU_DEVICE_ATTRIBUTE_INTEGRATED = 18;
178 constexpr CUdevice_attribute CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR = 75;
179 constexpr CUdevice_attribute CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR = 76;
180 constexpr CUdevice_attribute CU_DEVICE_ATTRIBUTE_MEMORY_POOLS_SUPPORTED = 115;
181
182 struct CUDA_ARRAY3D_DESCRIPTOR {
183 size_t Width;
184 size_t Height;
185 size_t Depth;
186 int Format;
187 unsigned NumChannels;
188 unsigned Flags;
189 };
190
191 struct CUDA_RESOURCE_DESC {
192 int resType;
193
194 union {
195 struct {
196 void* hMipmappedArray;
197 } mipmap;
198
199 int reserved[32];
200 } res;
201
202 unsigned flags;
203 };
204
205 struct CUDA_TEXTURE_DESC {
206 int addressMode[3];
207 int filterMode;
208 unsigned flags;
209 unsigned maxAnisotropy;
210 int mipmapFilterMode;
211 float mipmapLevelBias;
212 float minMipmapLevelClamp;
213 float maxMipmapLevelClamp;
214 float borderColor[4];
215 int reserved[12];
216 };
217
218 struct CUDA_MEMCPY2D {
219 size_t srcXInBytes;
220 size_t srcY;
221 int srcMemoryType;
222 const void* srcHost;
223 CUdeviceptr srcDevice;
224 void* srcArray;
225 size_t srcPitch;
226 size_t dstXInBytes;
227 size_t dstY;
228 int dstMemoryType;
229 void* dstHost;
230 CUdeviceptr dstDevice;
231 void* dstArray;
232 size_t dstPitch;
233 size_t WidthInBytes;
234 size_t Height;
235 };
236
237 constexpr int CU_MEMORYTYPE_DEVICE = 2;
238 constexpr int CU_MEMORYTYPE_ARRAY = 3;
239
240 constexpr Version kMinimumDriverVersion{12, 0};
241
242 ✗ constexpr Version DecodeVersion(int packed) {
243
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return {packed / 1000, packed % 1000 / 10};
244 }
245
246 ✗ struct Api {
247 std::string library;
248 Version driverVersion;
249
250 CUresult (*cuInit)(unsigned flags) = nullptr;
251 CUresult (*cuDriverGetVersion)(int* version) = nullptr;
252 CUresult (*cuDeviceGetCount)(int* count) = nullptr;
253 CUresult (*cuGetErrorName)(CUresult result, const char** name) = nullptr;
254 CUresult (*cuGetErrorString)(CUresult result, const char** text) = nullptr;
255 CUresult (*cuDeviceGet)(CUdevice* device, int ordinal) = nullptr;
256 CUresult (*cuDeviceGetName)(char* name, int length, CUdevice device) = nullptr;
257 CUresult (*cuDeviceGetUuid)(CUuuid* uuid, CUdevice device) = nullptr;
258 CUresult (*cuDeviceTotalMem)(size_t* bytes, CUdevice device) = nullptr;
259 CUresult (*cuDeviceGetAttribute)(int* value, CUdevice_attribute attribute,
260 CUdevice device) = nullptr;
261 CUresult (*cuDevicePrimaryCtxRetain)(CUcontext* context, CUdevice device) = nullptr;
262 CUresult (*cuDevicePrimaryCtxRelease)(CUdevice device) = nullptr;
263 CUresult (*cuCtxPushCurrent)(CUcontext context) = nullptr;
264 CUresult (*cuCtxPopCurrent)(CUcontext* context) = nullptr;
265 CUresult (*cuMemAlloc)(CUdeviceptr* pointer, size_t bytes) = nullptr;
266 CUresult (*cuMemFree)(CUdeviceptr pointer) = nullptr;
267 CUresult (*cuMemGetInfo)(size_t* free, size_t* total) = nullptr;
268 CUresult (*cuMemPoolCreate)(CUmemoryPool* pool, const CUmemPoolProps* properties) = nullptr;
269 CUresult (*cuMemPoolDestroy)(CUmemoryPool pool) = nullptr;
270 CUresult (*cuMemPoolSetAttribute)(CUmemoryPool pool, int attribute, void* value) = nullptr;
271 CUresult (*cuMemAllocFromPoolAsync)(CUdeviceptr* pointer, size_t bytes, CUmemoryPool pool,
272 CUstream stream) = nullptr;
273 CUresult (*cuMemFreeAsync)(CUdeviceptr pointer, CUstream stream) = nullptr;
274 CUresult (*cuMemHostAlloc)(void** pointer, size_t bytes, unsigned flags) = nullptr;
275 CUresult (*cuMemFreeHost)(void* pointer) = nullptr;
276 CUresult (*cuMemHostGetDevicePointer)(CUdeviceptr* device, void* host,
277 unsigned flags) = nullptr;
278 CUresult (*cuStreamCreate)(CUstream* stream, unsigned flags) = nullptr;
279 CUresult (*cuStreamDestroy)(CUstream stream) = nullptr;
280 CUresult (*cuStreamSynchronize)(CUstream stream) = nullptr;
281 CUresult (*cuEventCreate)(CUevent* event, unsigned flags) = nullptr;
282 CUresult (*cuEventDestroy)(CUevent event) = nullptr;
283 CUresult (*cuEventQuery)(CUevent event) = nullptr;
284 CUresult (*cuEventSynchronize)(CUevent event) = nullptr;
285 CUresult (*cuEventRecordWithFlags)(CUevent event, CUstream stream, unsigned flags) = nullptr;
286 CUresult (*cuStreamWaitEvent)(CUstream stream, CUevent event, unsigned flags) = nullptr;
287 CUresult (*cuLaunchHostFunc)(CUstream stream, void (*function)(void*), void* data) = nullptr;
288 CUresult (*cuImportExternalSemaphore)(
289 CUexternalSemaphore* semaphore, const CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC* desc) = nullptr;
290 CUresult (*cuDestroyExternalSemaphore)(CUexternalSemaphore semaphore) = nullptr;
291 CUresult (*cuImportExternalMemory)(CUexternalMemory* memory,
292 const CUDA_EXTERNAL_MEMORY_HANDLE_DESC* desc) = nullptr;
293 CUresult (*cuExternalMemoryGetMappedBuffer)(CUdeviceptr* pointer, CUexternalMemory memory,
294 const CUDA_EXTERNAL_MEMORY_BUFFER_DESC* desc) =
295 nullptr;
296 CUresult (*cuDestroyExternalMemory)(CUexternalMemory memory) = nullptr;
297 CUresult (*cuMemcpyDtoH)(void* dst, CUdeviceptr src, size_t bytes) = nullptr;
298 CUresult (*cuSignalExternalSemaphoresAsync)(const CUexternalSemaphore* semaphores,
299 const CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS* params,
300 unsigned count, CUstream stream) = nullptr;
301 CUresult (*cuWaitExternalSemaphoresAsync)(const CUexternalSemaphore* semaphores,
302 const CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS* params,
303 unsigned count, CUstream stream) = nullptr;
304 CUresult (*cuEventElapsedTime)(float* milliseconds, CUevent from, CUevent to) = nullptr;
305 CUresult (*cuMemcpyAsync)(CUdeviceptr dst, CUdeviceptr src, size_t bytes,
306 CUstream stream) = nullptr;
307 CUresult (*cuMemsetD32Async)(CUdeviceptr dst, unsigned value, size_t count,
308 CUstream stream) = nullptr;
309 CUresult (*cuMemcpyHtoDAsync)(CUdeviceptr dst, const void* src, size_t bytes,
310 CUstream stream) = nullptr;
311 CUresult (*cuModuleLoadData)(CUmodule* module, const void* image) = nullptr;
312 CUresult (*cuModuleUnload)(CUmodule module) = nullptr;
313 CUresult (*cuModuleGetGlobal)(CUdeviceptr* pointer, size_t* bytes, CUmodule module,
314 const char* name) = nullptr;
315 CUresult (*cuMemcpyHtoD)(CUdeviceptr dst, const void* src, size_t bytes) = nullptr;
316 CUresult (*cuModuleGetFunction)(CUfunction* function, CUmodule module,
317 const char* name) = nullptr;
318 CUresult (*cuFuncGetAttribute)(int* value, int attribute, CUfunction function) = nullptr;
319 CUresult (*cuLaunchKernel)(CUfunction function, unsigned groupsX, unsigned groupsY,
320 unsigned groupsZ, unsigned threadsX, unsigned threadsY,
321 unsigned threadsZ, unsigned sharedBytes, CUstream stream,
322 void** params, void** extra) = nullptr;
323 CUresult (*cuStreamBeginCapture)(CUstream stream, int mode) = nullptr;
324 CUresult (*cuStreamEndCapture)(CUstream stream, CUgraph* graph) = nullptr;
325 CUresult (*cuGraphInstantiateWithFlags)(CUgraphExec* exec, CUgraph graph,
326 unsigned long long flags) = nullptr;
327 CUresult (*cuGraphLaunch)(CUgraphExec exec, CUstream stream) = nullptr;
328 CUresult (*cuGraphExecDestroy)(CUgraphExec exec) = nullptr;
329 CUresult (*cuGraphDestroy)(CUgraph graph) = nullptr;
330 CUresult (*cuMipmappedArrayCreate)(void** array, const CUDA_ARRAY3D_DESCRIPTOR* desc,
331 unsigned levels) = nullptr;
332 CUresult (*cuMipmappedArrayGetLevel)(void** level, void* array, unsigned index) = nullptr;
333 CUresult (*cuMipmappedArrayDestroy)(void* array) = nullptr;
334 CUresult (*cuMemcpy2DAsync)(const CUDA_MEMCPY2D* copy, CUstream stream) = nullptr;
335 CUresult (*cuTexObjectCreate)(unsigned long long* object, const CUDA_RESOURCE_DESC* resource,
336 const CUDA_TEXTURE_DESC* sampler, const void* view) = nullptr;
337 CUresult (*cuTexObjectDestroy)(unsigned long long object) = nullptr;
338
339 const char* ErrorName(CUresult result) const;
340
341 void Warn(CUresult result, const char* what) const;
342
343 void Check(CUresult result, const char* what) const;
344 };
345
346 bool Available();
347
348 const Api& GetApi();
349
350 const compute::RuntimeCompiler& GetNvrtc();
351
352 }
353