-
Notifications
You must be signed in to change notification settings - Fork 9
/
CudaModularProgram.h
246 lines (183 loc) · 5.49 KB
/
CudaModularProgram.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
#pragma once
#include <string>
#include <unordered_map>
#include "unsuck.hpp"
#include "nvrtc.h"
#include <cmath>
#include "cuda.h"
using std::string;
using namespace std;
struct CudaModule{
void cu_checked(CUresult result){
if(result != CUDA_SUCCESS){
cout << "cuda error code: " << result << endl;
}
};
string path = "";
string name = "";
bool compiled = false;
bool success = false;
size_t ptxSize = 0;
char* ptx = nullptr;
size_t nvvmSize;
char *nvvm = nullptr;
CudaModule(string path, string name){
this->path = path;
this->name = name;
}
void compile(){
auto tStart = now();
cout << "================================================================================" << endl;
cout << "=== COMPILING: " << fs::path(path).filename().string() << endl;
cout << "================================================================================" << endl;
success = false;
string dir = fs::path(path).parent_path().string();
string optInclude = "-I " + dir;
string cuda_path = std::getenv("CUDA_PATH");
string cuda_include = "-I " + cuda_path + "/include";
nvrtcProgram prog;
string source = readFile(path);
nvrtcCreateProgram(&prog, source.c_str(), name.c_str(), 0, NULL, NULL);
std::vector<const char*> opts = {
"--gpu-architecture=compute_75",
// "--gpu-architecture=compute_86",
"--use_fast_math",
"--extra-device-vectorization",
"-lineinfo",
optInclude.c_str(),
cuda_include.c_str(),
"--relocatable-device-code=true",
"-default-device",
"-dlto",
// "--dopt=on",
"--std=c++17"
};
nvrtcResult res = nvrtcCompileProgram(prog, opts.size(), opts.data());
if (res != NVRTC_SUCCESS)
{
size_t logSize;
nvrtcGetProgramLogSize(prog, &logSize);
char* log = new char[logSize];
nvrtcGetProgramLog(prog, log);
std::cerr << log << std::endl;
delete[] log;
if(res != NVRTC_SUCCESS){
return;
}
}
if(nvvmSize > 0){
delete[] nvvm;
nvvmSize = 0;
}
nvrtcGetNVVMSize(prog, &nvvmSize);
nvvm = new char[nvvmSize];
nvrtcGetNVVM(prog, nvvm);
// Destroy the program.
nvrtcDestroyProgram(&prog);
compiled = true;
success = true;
printElapsedTime("compile " + name, tStart);
}
};
struct CudaModularProgram{
struct CudaModularProgramArgs{
vector<string> modules;
vector<string> kernels;
};
void cu_checked(CUresult result){
if(result != CUDA_SUCCESS){
cout << "cuda error code: " << result << endl;
}
};
vector<CudaModule*> modules;
CUmodule mod;
// CUfunction kernel = nullptr;
vector<std::function<void(void)>> compileCallbacks;
vector<string> kernelNames;
unordered_map<string, CUfunction> kernels;
CudaModularProgram(CudaModularProgramArgs args){
// CudaModularProgram(vector<string> modulePaths, vector<string> kernelNames = {}){
vector<string> modulePaths = args.modules;
vector<string> kernelNames = args.kernels;
this->kernelNames = kernelNames;
for(auto modulePath : modulePaths){
string moduleName = fs::path(modulePath).filename().string();
auto module = new CudaModule(modulePath, moduleName);
module->compile();
monitorFile(modulePath, [&, module]() {
module->compile();
link();
});
modules.push_back(module);
}
link();
}
void link(){
cout << "================================================================================" << endl;
cout << "=== LINKING" << endl;
cout << "================================================================================" << endl;
auto tStart = now();
for(auto module : modules){
if(!module->success){
return;
}
}
float walltime;
constexpr uint32_t v_optimization_level = 1;
constexpr uint32_t logSize = 8192;
char info_log[logSize];
char error_log[logSize];
vector<CUjit_option> options = {
CU_JIT_LTO,
CU_JIT_WALL_TIME,
CU_JIT_OPTIMIZATION_LEVEL,
CU_JIT_INFO_LOG_BUFFER,
CU_JIT_INFO_LOG_BUFFER_SIZE_BYTES,
CU_JIT_ERROR_LOG_BUFFER,
CU_JIT_ERROR_LOG_BUFFER_SIZE_BYTES,
CU_JIT_LOG_VERBOSE,
// CU_JIT_FAST_COMPILE // CUDA internal only (?)
};
vector<void*> optionVals = {
(void*) 1,
(void*) &walltime,
(void*) 4,
(void*) info_log,
(void*) logSize,
(void*) error_log,
(void*) logSize,
(void*) 1,
// (void*) 1
};
CUlinkState linkState;
int numOptions = options.size();
cu_checked(cuLinkCreate(numOptions, options.data(), optionVals.data(), &linkState));
for(auto module : modules){
cu_checked(cuLinkAddData(linkState, CU_JIT_INPUT_NVVM,
module->nvvm, module->nvvmSize, module->name.c_str(),
0, 0, 0));
}
size_t cubinSize;
void *cubin;
cu_checked(cuLinkComplete(linkState, &cubin, &cubinSize));
// {
// printf("link duration: %f ms \n", walltime);
printf("link error message: %s \n", error_log);
printf("link info message: %s \n", info_log);
// }
cu_checked(cuModuleLoadData(&mod, cubin));
//cu_checked(cuModuleGetFunction(&kernel, mod, "kernel"));
for(string kernelName : kernelNames){
CUfunction kernel;
cu_checked(cuModuleGetFunction(&kernel, mod, kernelName.c_str()));
kernels[kernelName] = kernel;
}
for(auto& callback : compileCallbacks){
callback();
}
// printElapsedTime("cuda link duration: ", tStart);
}
void onCompile(std::function<void(void)> callback){
compileCallbacks.push_back(callback);
}
};