-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathceed-cuda-compile.cpp
More file actions
301 lines (257 loc) · 12.9 KB
/
Copy pathceed-cuda-compile.cpp
File metadata and controls
301 lines (257 loc) · 12.9 KB
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
293
294
295
296
297
298
299
300
301
// Copyright (c) 2017-2025, Lawrence Livermore National Security, LLC and other CEED contributors.
// All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
//
// SPDX-License-Identifier: BSD-2-Clause
//
// This file is part of CEED: http://github.com/ceed
#include "ceed-cuda-compile.h"
#include <ceed.h>
#include <ceed/backend.h>
#include <ceed/jit-tools.h>
#include <cuda_runtime.h>
#include <nvrtc.h>
#include <stdarg.h>
#include <string.h>
#include <sstream>
#include "ceed-cuda-common.h"
#define CeedChk_Nvrtc(ceed, x) \
do { \
nvrtcResult result = static_cast<nvrtcResult>(x); \
if (result != NVRTC_SUCCESS) return CeedError((ceed), CEED_ERROR_BACKEND, nvrtcGetErrorString(result)); \
} while (0)
#define CeedCallNvrtc(ceed, ...) \
do { \
int ierr_q_ = __VA_ARGS__; \
CeedChk_Nvrtc(ceed, ierr_q_); \
} while (0)
//------------------------------------------------------------------------------
// Compile CUDA kernel
//------------------------------------------------------------------------------
static int CeedCompileCore_Cuda(Ceed ceed, const char *source, const bool throw_error, bool *is_compile_good, CUmodule *module,
const CeedInt num_defines, va_list args) {
size_t ptx_size;
char *ptx;
const int num_opts = 4;
CeedInt num_jit_source_dirs = 0, num_jit_defines = 0;
const char **opts;
nvrtcProgram prog;
struct cudaDeviceProp prop;
Ceed_Cuda *ceed_data;
cudaFree(0); // Make sure a Context exists for nvrtc
std::ostringstream code;
// Get kernel specific options, such as kernel constants
if (num_defines > 0) {
char *name;
for (int i = 0; i < num_defines; i++) {
name = va_arg(args, char *);
if (!strcmp(name, "CEED_JIT_PRECISION")) {
char *val;
val = va_arg(args, char *);
code << "#define " << name << " " << val << "\n";
} else {
int val;
val = va_arg(args, int);
code << "#define " << name << " " << val << "\n";
}
}
}
// Standard libCEED definitions for CUDA backends
code << "#include <ceed/jit-source/cuda/cuda-jit.h>\n\n";
// Non-macro options
CeedCallBackend(CeedCalloc(num_opts, &opts));
opts[0] = "-default-device";
CeedCallBackend(CeedGetData(ceed, &ceed_data));
CeedCallCuda(ceed, cudaGetDeviceProperties(&prop, ceed_data->device_id));
std::string arch_arg =
#if CUDA_VERSION >= 11010
// NVRTC used to support only virtual architectures through the option
// -arch, since it was only emitting PTX. It will now support actual
// architectures as well to emit SASS.
// https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/index.html#dynamic-code-generation
"-arch=sm_"
#else
"-arch=compute_"
#endif
+ std::to_string(prop.major) + std::to_string(prop.minor);
opts[1] = arch_arg.c_str();
opts[2] = "-Dint32_t=int";
opts[3] = "-DCEED_RUNNING_JIT_PASS=1";
// Additional include dirs
{
const char **jit_source_dirs;
CeedCallBackend(CeedGetJitSourceRoots(ceed, &num_jit_source_dirs, &jit_source_dirs));
CeedCallBackend(CeedRealloc(num_opts + num_jit_source_dirs, &opts));
for (CeedInt i = 0; i < num_jit_source_dirs; i++) {
std::ostringstream include_dir_arg;
include_dir_arg << "-I" << jit_source_dirs[i];
CeedCallBackend(CeedStringAllocCopy(include_dir_arg.str().c_str(), (char **)&opts[num_opts + i]));
}
CeedCallBackend(CeedRestoreJitSourceRoots(ceed, &jit_source_dirs));
}
// User defines
{
const char **jit_defines;
CeedCallBackend(CeedGetJitDefines(ceed, &num_jit_defines, &jit_defines));
CeedCallBackend(CeedRealloc(num_opts + num_jit_source_dirs + num_jit_defines, &opts));
for (CeedInt i = 0; i < num_jit_defines; i++) {
std::ostringstream define_arg;
define_arg << "-D" << jit_defines[i];
CeedCallBackend(CeedStringAllocCopy(define_arg.str().c_str(), (char **)&opts[num_opts + num_jit_source_dirs + i]));
}
CeedCallBackend(CeedRestoreJitDefines(ceed, &jit_defines));
}
// Add string source argument provided in call
code << source;
// Create Program
CeedCallNvrtc(ceed, nvrtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL));
// Compile kernel
CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- ATTEMPTING TO COMPILE JIT SOURCE ----------\n");
CeedDebug(ceed, "Source:\n%s\n", code.str().c_str());
CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- END OF JIT SOURCE ----------\n");
if (CeedDebugFlag(ceed)) {
// LCOV_EXCL_START
CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- JiT COMPILER OPTIONS ----------\n");
for (CeedInt i = 0; i < num_opts + num_jit_source_dirs + num_jit_defines; i++) {
CeedDebug(ceed, "Option %d: %s", i, opts[i]);
}
CeedDebug(ceed, "");
CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- END OF JiT COMPILER OPTIONS ----------\n");
// LCOV_EXCL_STOP
}
nvrtcResult result = nvrtcCompileProgram(prog, num_opts + num_jit_source_dirs + num_jit_defines, opts);
for (CeedInt i = 0; i < num_jit_source_dirs; i++) {
CeedCallBackend(CeedFree(&opts[num_opts + i]));
}
for (CeedInt i = 0; i < num_jit_defines; i++) {
CeedCallBackend(CeedFree(&opts[num_opts + num_jit_source_dirs + i]));
}
CeedCallBackend(CeedFree(&opts));
*is_compile_good = result == NVRTC_SUCCESS;
if (!*is_compile_good) {
char *log;
size_t log_size;
CeedCallNvrtc(ceed, nvrtcGetProgramLogSize(prog, &log_size));
CeedCallBackend(CeedMalloc(log_size, &log));
CeedCallNvrtc(ceed, nvrtcGetProgramLog(prog, log));
if (throw_error) {
return CeedError(ceed, CEED_ERROR_BACKEND, "%s\n%s", nvrtcGetErrorString(result), log);
} else {
// LCOV_EXCL_START
CeedDebug256(ceed, CEED_DEBUG_COLOR_ERROR, "---------- COMPILE ERROR DETECTED ----------\n");
CeedDebug(ceed, "Error: %s\nCompile log:\n%s\n", nvrtcGetErrorString(result), log);
CeedDebug256(ceed, CEED_DEBUG_COLOR_WARNING, "---------- BACKEND MAY FALLBACK ----------\n");
CeedCallBackend(CeedFree(&log));
CeedCallNvrtc(ceed, nvrtcDestroyProgram(&prog));
return CEED_ERROR_SUCCESS;
// LCOV_EXCL_STOP
}
}
#if CUDA_VERSION >= 11010
CeedCallNvrtc(ceed, nvrtcGetCUBINSize(prog, &ptx_size));
CeedCallBackend(CeedMalloc(ptx_size, &ptx));
CeedCallNvrtc(ceed, nvrtcGetCUBIN(prog, ptx));
#else
CeedCallNvrtc(ceed, nvrtcGetPTXSize(prog, &ptx_size));
CeedCallBackend(CeedMalloc(ptx_size, &ptx));
CeedCallNvrtc(ceed, nvrtcGetPTX(prog, ptx));
#endif
CeedCallNvrtc(ceed, nvrtcDestroyProgram(&prog));
CeedCallCuda(ceed, cuModuleLoadData(module, ptx));
CeedCallBackend(CeedFree(&ptx));
return CEED_ERROR_SUCCESS;
}
int CeedCompile_Cuda(Ceed ceed, const char *source, CUmodule *module, const CeedInt num_defines, ...) {
bool is_compile_good = true;
va_list args;
va_start(args, num_defines);
const CeedInt ierr = CeedCompileCore_Cuda(ceed, source, true, &is_compile_good, module, num_defines, args);
va_end(args);
CeedCallBackend(ierr);
return CEED_ERROR_SUCCESS;
}
int CeedTryCompile_Cuda(Ceed ceed, const char *source, bool *is_compile_good, CUmodule *module, const CeedInt num_defines, ...) {
va_list args;
va_start(args, num_defines);
const CeedInt ierr = CeedCompileCore_Cuda(ceed, source, false, is_compile_good, module, num_defines, args);
va_end(args);
CeedCallBackend(ierr);
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Get CUDA kernel
//------------------------------------------------------------------------------
int CeedGetKernel_Cuda(Ceed ceed, CUmodule module, const char *name, CUfunction *kernel) {
CeedCallCuda(ceed, cuModuleGetFunction(kernel, module, name));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Run CUDA kernel with block size selected automatically based on the kernel
// (which may use enough registers to require a smaller block size than the
// hardware is capable)
//------------------------------------------------------------------------------
int CeedRunKernelAutoblockCuda(Ceed ceed, CUfunction kernel, size_t points, void **args) {
int min_grid_size, max_block_size;
CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_block_size, kernel, NULL, 0, 0x10000));
CeedCallBackend(CeedRunKernel_Cuda(ceed, kernel, CeedDivUpInt(points, max_block_size), max_block_size, args));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Run CUDA kernel
//------------------------------------------------------------------------------
int CeedRunKernel_Cuda(Ceed ceed, CUfunction kernel, const int grid_size, const int block_size, void **args) {
CeedCallBackend(CeedRunKernelDimShared_Cuda(ceed, kernel, NULL, grid_size, block_size, 1, 1, 0, args));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Run CUDA kernel for spatial dimension
//------------------------------------------------------------------------------
int CeedRunKernelDim_Cuda(Ceed ceed, CUfunction kernel, const int grid_size, const int block_size_x, const int block_size_y, const int block_size_z,
void **args) {
CeedCallBackend(CeedRunKernelDimShared_Cuda(ceed, kernel, NULL, grid_size, block_size_x, block_size_y, block_size_z, 0, args));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Run CUDA kernel for spatial dimension with shared memory
//------------------------------------------------------------------------------
static int CeedRunKernelDimSharedCore_Cuda(Ceed ceed, CUfunction kernel, CUstream stream, const int grid_size, const int block_size_x,
const int block_size_y, const int block_size_z, const int shared_mem_size, const bool throw_error,
bool *is_good_run, void **args) {
#if CUDA_VERSION >= 9000
cuFuncSetAttribute(kernel, CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, shared_mem_size);
#endif
CUresult result = cuLaunchKernel(kernel, grid_size, 1, 1, block_size_x, block_size_y, block_size_z, shared_mem_size, stream, args, NULL);
if (result == CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES) {
int max_threads_per_block, shared_size_bytes, num_regs;
cuFuncGetAttribute(&max_threads_per_block, CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK, kernel);
cuFuncGetAttribute(&shared_size_bytes, CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES, kernel);
cuFuncGetAttribute(&num_regs, CU_FUNC_ATTRIBUTE_NUM_REGS, kernel);
if (throw_error) {
return CeedError(ceed, CEED_ERROR_BACKEND,
"CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES: max_threads_per_block %d on block size (%d,%d,%d), shared_size %d, num_regs %d",
max_threads_per_block, block_size_x, block_size_y, block_size_z, shared_size_bytes, num_regs);
} else {
// LCOV_EXCL_START
CeedDebug256(ceed, CEED_DEBUG_COLOR_ERROR, "---------- LAUNCH ERROR DETECTED ----------\n");
CeedDebug(ceed, "CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES: max_threads_per_block %d on block size (%d,%d,%d), shared_size %d, num_regs %d\n",
max_threads_per_block, block_size_x, block_size_y, block_size_z, shared_size_bytes, num_regs);
CeedDebug256(ceed, CEED_DEBUG_COLOR_WARNING, "---------- BACKEND MAY FALLBACK ----------\n");
// LCOV_EXCL_STOP
}
*is_good_run = false;
} else CeedChk_Cu(ceed, result);
return CEED_ERROR_SUCCESS;
}
int CeedRunKernelDimShared_Cuda(Ceed ceed, CUfunction kernel, CUstream stream, const int grid_size, const int block_size_x, const int block_size_y,
const int block_size_z, const int shared_mem_size, void **args) {
bool is_good_run = true;
CeedCallBackend(CeedRunKernelDimSharedCore_Cuda(ceed, kernel, stream, grid_size, block_size_x, block_size_y, block_size_z, shared_mem_size, true,
&is_good_run, args));
return CEED_ERROR_SUCCESS;
}
int CeedTryRunKernelDimShared_Cuda(Ceed ceed, CUfunction kernel, CUstream stream, const int grid_size, const int block_size_x, const int block_size_y,
const int block_size_z, const int shared_mem_size, bool *is_good_run, void **args) {
CeedCallBackend(CeedRunKernelDimSharedCore_Cuda(ceed, kernel, stream, grid_size, block_size_x, block_size_y, block_size_z, shared_mem_size, false,
is_good_run, args));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------