-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvrtc_wrapper.h
296 lines (264 loc) · 5.73 KB
/
nvrtc_wrapper.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
293
294
295
296
#pragma once
#include <string>
#include <fstream>
#include <streambuf>
#include <libgen.h>
#include <vector>
#include <iostream>
#include "cuda_env.h"
using namespace std;
std::string read_file(const char *path)
{
std::ifstream t(path);
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return str;
}
class cuSource
{
protected:
string source;
string name;
public:
cuSource(){}
int LoadSource(const char *path)
{
source = read_file(path);
if(source.size()==0) return -1;
char *p = (char *)malloc(sizeof(char) * 4096);
memset(p, 0, sizeof(char) * 4096);
memcpy(p, path, strlen(path));
name = basename(p);
free(p);
return 0;
}
void ClearSource(){
source = "";
name = "";
}
const char *GetSource() const { return source.c_str(); }
const char *GetName() const { return name.c_str(); }
};
class cuModule
{
protected:
CUmodule module;
CUfunction kernel; // entry point
vector<void *> launch_param;
public:
cuModule() : module(nullptr), kernel(nullptr) {}
~cuModule()
{
Unload();
}
void LoadPTX(const char* ptx,const char* entry_func_name){
cuSafeCall(cuModuleLoadDataEx(&module, ptx, 0, 0, 0));
cuSafeCall(cuModuleGetFunction(&kernel, module, entry_func_name));
}
int GetRegisterUsage(){
int nreg=-1;
cuSafeCall(cuFuncGetAttribute(&nreg,CU_FUNC_ATTRIBUTE_NUM_REGS,kernel));
return nreg;
}
int GetSharedMemUsage(){
int shmem_usg=-1;
cuSafeCall(cuFuncGetAttribute(&shmem_usg,CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES,kernel));
return shmem_usg;
}
int GetConstMemUsage(){
int n=-1;
cuSafeCall(cuFuncGetAttribute(&n,CU_FUNC_ATTRIBUTE_CONST_SIZE_BYTES,kernel));
return n;
}
int GetLocalMemUsage(){
int n=-1;
cuSafeCall(cuFuncGetAttribute(&n,CU_FUNC_ATTRIBUTE_LOCAL_SIZE_BYTES,kernel));
return n;
}
void Unload()
{
if (module)
{
cuSafeCall(cuModuleUnload(module));
module = 0;
}
}
void AddParamPtr(void *param_ptr)
{
launch_param.push_back(param_ptr);
}
void ClearParams()
{
launch_param.clear();
}
void Launch(int gridDimX, int gridDimY, int gridDimZ,
int blockDimX, int blockDimY, int blockDimZ)
{
cuSafeCall(cuLaunchKernel(kernel,
gridDimX, gridDimY, gridDimZ,
blockDimX, blockDimY, blockDimZ,
0, NULL, // shared mem and stream
launch_param.data(), 0)); // arguments
ClearParams();
}
};
class cuDevice
{
friend class cuContext;
protected:
CUdevice device;
string name;
size_t totalMemBytes;
public:
cuDevice() : device(-1), totalMemBytes(0) {}
void GetDevice(int index)
{
char buf[1024] = {0};
cuSafeCall(cuDeviceGet(&device, index));
cuSafeCall(cuDeviceGetName(buf, 1024, device));
cuSafeCall(cuDeviceTotalMem(&totalMemBytes, device));
name = buf;
}
cuDevice(const cuDevice &dev)
{
this->device = dev.device;
this->name = dev.name;
this->totalMemBytes = dev.totalMemBytes;
}
int GetHandle() { return device; }
size_t GetTotalMemBytes() { return totalMemBytes; }
static vector<cuDevice> GetAllDevices()
{
vector<cuDevice> devices;
int count = 0;
cuSafeCall(cuDeviceGetCount(&count));
for (int i = 0; i < count; i++){
cuDevice dev;
dev.GetDevice(i);
devices.push_back(dev);
}
return devices;
}
};
class cuContext
{
protected:
CUcontext context;
public:
cuContext() : context(0) {}
~cuContext()
{
Destroy();
}
void CreateInDevice(cuDevice *device)
{
cuSafeCall(cuCtxCreate(&context, 0, device->device));
}
void Destroy()
{
if (context)
{
cuSafeCall(cuCtxDestroy(context));
context = 0;
}
}
static void GlobalSync()
{
cuSafeCall(cuCtxSynchronize());
}
};
typedef CUdeviceptr cuDevicePtr;
class cuModuleCompiler
{
protected:
cuSource src;
string ptx;
string log;
vector<string> options;
vector<void *> launch_param;
public:
cuModuleCompiler() {}
~cuModuleCompiler()
{
}
void ClearStatus()
{
ptx = "";
log = "";
options.clear();
src.ClearSource();
}
// return 0 if success
// return -1 if error
int LoadSource(const char *path)
{
int b = src.LoadSource(path);
if(b < 0){
return -1;
}
return 0;
}
// return 0 if success
// return -1 if error
int Compile()
{
// create program
nvrtcProgram prog = 0;
if (strlen(src.GetSource()) == 0)
{
// no source files added.
log = "== Error : Please add one source file before compiling... ==\n";
log += "== Compilation terminated. ==";
return -1;
}
string str = src.GetSource();
string nam = src.GetName();
const char *buffer = str.c_str();
const char *name = nam.c_str();
nvrtcSafeCall(
nvrtcCreateProgram(&prog, // prog
buffer, // buffer
name, // name
0, // numHeaders
NULL, // headers
NULL) // includeNames
);
vector<const char *> options_arr;
for (size_t i = 0; i < options.size(); i++)
{
options_arr.push_back(options[i].c_str());
}
nvrtcResult compileResult = nvrtcCompileProgram(prog, options.size(), options_arr.data());
// Obtain compilation log from the program.
size_t logSize;
nvrtcSafeCall(nvrtcGetProgramLogSize(prog, &logSize));
char *szlog = new char[logSize];
nvrtcSafeCall(nvrtcGetProgramLog(prog, szlog));
log = szlog;
delete[] szlog;
if (compileResult != NVRTC_SUCCESS)
{
return -1;
}
// Obtain PTX from the program.
size_t ptxSize;
nvrtcSafeCall(nvrtcGetPTXSize(prog, &ptxSize));
char *szptx = new char[ptxSize];
nvrtcSafeCall(nvrtcGetPTX(prog, szptx));
ptx = szptx;
delete[] szptx;
// Destroy the program.
nvrtcSafeCall(nvrtcDestroyProgram(&prog));
return 0;
}
const char *GetCompileLog()
{
return log.c_str();
}
const char *GetPTX() { return ptx.c_str(); }
void AddCompileOption(const char *option)
{
string opt = option;
options.push_back(opt);
}
};