-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathCCUDAHandler.h
292 lines (261 loc) · 9.13 KB
/
CCUDAHandler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine".
// For conditions of distribution and use, see copyright notice in nabla.h
#ifndef _NBL_VIDEO_C_CUDA_HANDLER_H_
#define _NBL_VIDEO_C_CUDA_HANDLER_H_
#include "nbl/core/declarations.h"
#include "nbl/core/definitions.h"
#include "nbl/system/declarations.h"
#include "nbl/video/CCUDADevice.h"
#ifdef _NBL_COMPILE_WITH_CUDA_
namespace nbl::video
{
class CCUDAHandler : public core::IReferenceCounted
{
public:
static bool defaultHandleResult(CUresult result, const system::logger_opt_ptr& logger=nullptr);
inline bool defaultHandleResult(CUresult result)
{
core::smart_refctd_ptr<system::ILogger> logger = m_logger.get();
return defaultHandleResult(result,logger.get());
}
//
bool defaultHandleResult(nvrtcResult result);
//
template<typename T>
static T* cast_CUDA_ptr(CUdeviceptr ptr) { return reinterpret_cast<T*>(ptr); }
//
static core::smart_refctd_ptr<CCUDAHandler> create(system::ISystem* system, core::smart_refctd_ptr<system::ILogger>&& _logger);
//
using LibLoader = system::DefaultFuncPtrLoader;
NBL_SYSTEM_DECLARE_DYNAMIC_FUNCTION_CALLER_CLASS(CUDA,LibLoader
,cuCtxCreate_v2
,cuDevicePrimaryCtxRetain
,cuDevicePrimaryCtxRelease
,cuDevicePrimaryCtxSetFlags
,cuDevicePrimaryCtxGetState
,cuCtxDestroy_v2
,cuCtxEnablePeerAccess
,cuCtxGetApiVersion
,cuCtxGetCurrent
,cuCtxGetDevice
,cuCtxGetSharedMemConfig
,cuCtxPopCurrent_v2
,cuCtxPushCurrent_v2
,cuCtxSetCacheConfig
,cuCtxSetCurrent
,cuCtxSetSharedMemConfig
,cuCtxSynchronize
,cuDeviceComputeCapability
,cuDeviceCanAccessPeer
,cuDeviceGetCount
,cuDeviceGet
,cuDeviceGetAttribute
,cuDeviceGetLuid
,cuDeviceGetUuid
,cuDeviceTotalMem_v2
,cuDeviceGetName
,cuDriverGetVersion
,cuEventCreate
,cuEventDestroy_v2
,cuEventElapsedTime
,cuEventQuery
,cuEventRecord
,cuEventSynchronize
,cuFuncGetAttribute
,cuFuncSetCacheConfig
,cuGetErrorName
,cuGetErrorString
,cuGraphicsMapResources
,cuGraphicsResourceGetMappedPointer_v2
,cuGraphicsResourceGetMappedMipmappedArray
,cuGraphicsSubResourceGetMappedArray
,cuGraphicsUnmapResources
,cuGraphicsUnregisterResource
,cuInit
,cuLaunchKernel
,cuMemAlloc_v2
,cuMemcpyDtoD_v2
,cuMemcpyDtoH_v2
,cuMemcpyHtoD_v2
,cuMemcpyDtoDAsync_v2
,cuMemcpyDtoHAsync_v2
,cuMemcpyHtoDAsync_v2
,cuMemGetAddressRange_v2
,cuMemFree_v2
,cuMemFreeHost
,cuMemGetInfo_v2
,cuMemHostAlloc
,cuMemHostRegister_v2
,cuMemHostUnregister
,cuMemsetD32_v2
,cuMemsetD32Async
,cuMemsetD8_v2
,cuMemsetD8Async
,cuModuleGetFunction
,cuModuleGetGlobal_v2
,cuModuleLoadDataEx
,cuModuleLoadFatBinary
,cuModuleUnload
,cuOccupancyMaxActiveBlocksPerMultiprocessor
,cuPointerGetAttribute
,cuStreamAddCallback
,cuStreamCreate
,cuStreamDestroy_v2
,cuStreamQuery
,cuStreamSynchronize
,cuStreamWaitEvent
,cuSurfObjectCreate
,cuSurfObjectDestroy
,cuTexObjectCreate
,cuTexObjectDestroy
,cuImportExternalMemory
,cuDestroyExternalMemory
,cuExternalMemoryGetMappedBuffer
,cuMemUnmap
,cuMemAddressFree
,cuMemGetAllocationGranularity
,cuMemAddressReserve
,cuMemCreate
,cuMemExportToShareableHandle
,cuMemMap
,cuMemRelease
,cuMemSetAccess
,cuMemImportFromShareableHandle
,cuLaunchHostFunc
,cuDestroyExternalSemaphore
,cuImportExternalSemaphore
,cuSignalExternalSemaphoresAsync
);
const CUDA& getCUDAFunctionTable() const {return m_cuda;}
NBL_SYSTEM_DECLARE_DYNAMIC_FUNCTION_CALLER_CLASS(NVRTC,LibLoader,
nvrtcGetErrorString,
nvrtcVersion,
nvrtcAddNameExpression,
nvrtcCompileProgram,
nvrtcCreateProgram,
nvrtcDestroyProgram,
nvrtcGetLoweredName,
nvrtcGetPTX,
nvrtcGetPTXSize,
nvrtcGetProgramLog,
nvrtcGetProgramLogSize
);
const NVRTC& getNVRTCFunctionTable() const {return m_nvrtc;}
//
inline core::SRange<system::IFile* const> getSTDHeaders()
{
auto begin = m_headers.empty() ? nullptr:(&m_headers[0].get());
return {begin,begin+m_headers.size()};
}
inline const auto& getSTDHeaderContents() { return m_headerContents; }
inline const auto& getSTDHeaderNames() { return m_headerNames; }
//
nvrtcResult createProgram(nvrtcProgram* prog, std::string&& source, const char* name, const int headerCount=0, const char* const* headerContents=nullptr, const char* const* includeNames=nullptr);
inline nvrtcResult createProgram(nvrtcProgram* prog, const char* source, const char* name, const int headerCount=0, const char* const* headerContents=nullptr, const char* const* includeNames=nullptr)
{
return createProgram(prog,std::string(source),name,headerCount,headerContents,includeNames);
}
inline nvrtcResult createProgram(nvrtcProgram* prog, system::IFile* file, const int headerCount=0, const char* const* headerContents=nullptr, const char* const* includeNames=nullptr)
{
const auto filesize = file->getSize();
std::string source(filesize+1u,'0');
system::IFile::success_t bytesRead;
file->read(bytesRead,source.data(),0u,file->getSize());
source.resize(bytesRead.getBytesProcessed());
return createProgram(prog,std::move(source),file->getFileName().string().c_str(),headerCount,headerContents,includeNames);
}
//
inline nvrtcResult compileProgram(nvrtcProgram prog, core::SRange<const char* const> options)
{
return m_nvrtc.pnvrtcCompileProgram(prog,options.size(),options.begin());
}
//
nvrtcResult getProgramLog(nvrtcProgram prog, std::string& log);
//
struct ptx_and_nvrtcResult_t
{
core::smart_refctd_ptr<asset::ICPUBuffer> ptx;
nvrtcResult result;
};
ptx_and_nvrtcResult_t getPTX(nvrtcProgram prog);
//
inline ptx_and_nvrtcResult_t compileDirectlyToPTX(
std::string&& source, const char* filename, core::SRange<const char* const> nvrtcOptions,
const int headerCount=0, const char* const* headerContents=nullptr, const char* const* includeNames=nullptr,
std::string* log=nullptr
)
{
nvrtcProgram program = nullptr;
nvrtcResult result = NVRTC_ERROR_PROGRAM_CREATION_FAILURE;
auto cleanup = core::makeRAIIExiter([&]() -> void
{
if (result!=NVRTC_SUCCESS && program)
m_nvrtc.pnvrtcDestroyProgram(&program); // TODO: do we need to destroy the program if we successfully get PTX?
});
result = createProgram(&program,std::move(source),filename,headerCount,headerContents,includeNames);
return compileDirectlyToPTX_impl(result,program,nvrtcOptions,log);
}
inline ptx_and_nvrtcResult_t compileDirectlyToPTX(
const char* source, const char* filename, core::SRange<const char* const> nvrtcOptions,
const int headerCount=0, const char* const* headerContents=nullptr, const char* const* includeNames=nullptr,
std::string* log=nullptr
)
{
return compileDirectlyToPTX(std::string(source),filename,nvrtcOptions,headerCount,headerContents,includeNames,log);
}
inline ptx_and_nvrtcResult_t compileDirectlyToPTX(
system::IFile* file, core::SRange<const char* const> nvrtcOptions,
const int headerCount=0, const char* const* headerContents=nullptr, const char* const* includeNames=nullptr,
std::string* log=nullptr
)
{
nvrtcProgram program = nullptr;
nvrtcResult result = NVRTC_ERROR_PROGRAM_CREATION_FAILURE;
auto cleanup = core::makeRAIIExiter([&]() -> void
{
if (result!=NVRTC_SUCCESS && program)
m_nvrtc.pnvrtcDestroyProgram(&program); // TODO: do we need to destroy the program if we successfully get PTX?
});
result = createProgram(&program,file,headerCount,headerContents,includeNames);
return compileDirectlyToPTX_impl(result,program,nvrtcOptions,log);
}
core::smart_refctd_ptr<CCUDADevice> createDevice(core::smart_refctd_ptr<CVulkanConnection>&& vulkanConnection, IPhysicalDevice* physicalDevice);
protected:
CCUDAHandler(CUDA&& _cuda, NVRTC&& _nvrtc, core::vector<core::smart_refctd_ptr<system::IFile>>&& _headers, core::smart_refctd_ptr<system::ILogger>&& _logger, int _version)
: m_cuda(std::move(_cuda)), m_nvrtc(std::move(_nvrtc)), m_headers(std::move(_headers)), m_logger(std::move(_logger)), m_version(_version)
{
for (auto& header : m_headers)
{
m_headerContents.push_back(reinterpret_cast<const char*>(header->getMappedPointer()));
m_headerNamesStorage.push_back(header->getFileName().string());
m_headerNames.push_back(m_headerNamesStorage.back().c_str());
}
}
~CCUDAHandler() = default;
//
inline ptx_and_nvrtcResult_t compileDirectlyToPTX_impl(nvrtcResult result, nvrtcProgram program, core::SRange<const char* const> nvrtcOptions, std::string* log)
{
if (result!=NVRTC_SUCCESS)
return {nullptr,result};
result = compileProgram(program,nvrtcOptions);
if (log)
getProgramLog(program,*log);
if (result!=NVRTC_SUCCESS)
return {nullptr,result};
return getPTX(program);
}
// function tables
CUDA m_cuda;
NVRTC m_nvrtc;
//
core::vector<core::smart_refctd_ptr<system::IFile>> m_headers;
core::vector<const char*> m_headerContents;
core::vector<std::string> m_headerNamesStorage;
core::vector<const char*> m_headerNames;
system::logger_opt_smart_ptr m_logger;
int m_version;
};
}
#endif // _NBL_COMPILE_WITH_CUDA_
#endif