forked from SixTrack/sixtracklib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvrtc_wrap.h
214 lines (179 loc) · 6.3 KB
/
nvrtc_wrap.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
#pragma once
#include <cuda.h>
#include <cuda_runtime.h>
#include <nvrtc.h>
#include <vector>
#include <iostream>
#include "utils.h"
// GPU occupancy tuning
//const size_t NUM_THREADS = 256;
const size_t NUM_THREADS = 1024;
//const size_t NUM_BLOCKS = 512;
const size_t NUM_BLOCKS = 1024;
// Headers for the cuda code
std::vector<std::string> my_headers_names {
"track.h"
};
std::vector<std::string> my_headers {{
#include "track.xxd"
}};
static void CUDA_info() {
using namespace std;
const int kb = 1024;
const int mb = kb * kb;
cout << "NBody.GPU" << endl << "=========" << endl << endl;
cout << "CUDA version: v" << CUDART_VERSION << endl;
int devCount;
cudaGetDeviceCount(&devCount);
cout << "CUDA Devices: " << endl << endl;
for(int i = 0; i < devCount; ++i) {
cudaDeviceProp props;
cudaGetDeviceProperties(&props, i);
cout << i << ": " << props.name << ": " << props.major << "." << props.minor << endl;
cout << " Global memory: " << props.totalGlobalMem / mb << "mb" << endl;
cout << " Shared memory: " << props.sharedMemPerBlock / kb << "kb" << endl;
cout << " Constant memory: " << props.totalConstMem / kb << "kb" << endl;
cout << " Block registers: " << props.regsPerBlock << endl << endl;
cout << " Warp size: " << props.warpSize << endl;
cout << " Threads per block: " << props.maxThreadsPerBlock << endl;
cout << " Max block dimensions: [ " << props.maxThreadsDim[0] << ", " << props.maxThreadsDim[1] << ", " << props.maxThreadsDim[2] << " ]" << endl;
cout << " Max grid dimensions: [ " << props.maxGridSize[0] << ", " << props.maxGridSize[1] << ", " << props.maxGridSize[2] << " ]" << endl;
cout << endl;
}
}
std::string CUDA_get_architecture(int device) {
cudaDeviceProp props;
cudaGetDeviceProperties(&props, device);
return std::to_string(props.major) + std::to_string(props.minor);
}
template <typename T>
static void CUDA_SAFE_CALL(const T & x) {
CUresult result = x;
if (result != CUDA_SUCCESS) {
const char *msg;
cuGetErrorName(result, &msg);
std::cerr << "\nerror: " << x << " failed with error "
<< msg << '\n';
exit(1);
}
}
class CUDA {
public:
CUdevice cuDevice;
CUcontext context;
CUDA() {
CUDA_SAFE_CALL(cuInit(0));
CUDA_SAFE_CALL(cuDeviceGet(&cuDevice, 0));
CUDA_SAFE_CALL(cuCtxCreate(&context, 0, cuDevice));
}
~CUDA() {
CUDA_SAFE_CALL(cuCtxDestroy(context));
}
} cuda;
class NVRTC {
CUmodule module;
CUfunction kernel;
std::vector<std::string> compilation_opts;
std::vector<char> ptx;
public:
template <typename T>
static void SAFE_CALL(const T & x) {
nvrtcResult result = x;
if (result != NVRTC_SUCCESS) {
std::cerr << "\nerror: " << x << " failed with error "
<< nvrtcGetErrorString(result) << '\n';
exit(1);
}
}
NVRTC() {
compilation_opts.emplace_back("--gpu-architecture=compute_"+CUDA_get_architecture(0));
// compilation_opts.emplace_back("-I .");
// compilation_opts.emplace_back("-I ./include");
compilation_opts.emplace_back("--fmad=false");
compilation_opts.emplace_back("--std=c++11");
}
NVRTC(const std::string & ptx_path): NVRTC() {
read_ptx(ptx_path);
std::cout << "loaded ptx " << ptx_path << std::endl;
}
NVRTC(const NVRTC & o) = delete;
NVRTC & operator=(const NVRTC & o) = delete;
NVRTC(NVRTC && o) = default;
NVRTC & operator=(NVRTC && o) = default;
~NVRTC() {
clear();
}
void compile_ptx(std::string buffer) {
//cuCtxSetCurrent(cuda.context);
auto ptr_headers = vecStr2vec<const char*>(my_headers);
auto ptr_headers_names = vecStr2vec<const char*>(my_headers_names);
nvrtcProgram prog;
NVRTC::SAFE_CALL(
nvrtcCreateProgram(&prog, // prog
buffer.c_str(), // buffer
"lattice.cu", // name
ptr_headers.size(), // numHeaders
ptr_headers.data(), // headers
ptr_headers_names.data())); // includeNames
// Compile the program
nvrtcResult compileResult = nvrtcCompileProgram(prog, // prog
compilation_opts.size(), // numOptions
vecStr2vec<const char *>(compilation_opts).data()); // options
// Obtain compilation log from the program.
size_t logSize;
NVRTC::SAFE_CALL(nvrtcGetProgramLogSize(prog, &logSize));
if (logSize > 1) {
char *log = new char[logSize];
NVRTC::SAFE_CALL(nvrtcGetProgramLog(prog, log));
std::cout << log << '\n';
delete[] log;
}
if (compileResult != NVRTC_SUCCESS) {
exit(1);
}
// Obtain PTX from the program.
size_t ptxSize;
NVRTC::SAFE_CALL(nvrtcGetPTXSize(prog, &ptxSize));
ptx.resize(ptxSize);
NVRTC::SAFE_CALL(nvrtcGetPTX(prog, ptx.data()));
// Destroy the program.
NVRTC::SAFE_CALL(nvrtcDestroyProgram(&prog));
load_ptx();
}
void load_ptx() {
CUDA_SAFE_CALL(cuModuleLoadDataEx(&module, ptx.data(), 0, 0, 0));
CUDA_SAFE_CALL(cuModuleGetFunction(&kernel, module, "track"));
}
void write_ptx(const std::string & path) {
std::ofstream file(path, std::ios::out | std::ofstream::binary | std::ofstream::trunc);
std::copy(ptx.begin(), ptx.end(), std::ostreambuf_iterator<char>(file));
}
void read_ptx(const std::string & path) {
std::ifstream file(path, std::ios::in | std::ifstream::binary);
if ( file.is_open() ) {
clear();
std::istreambuf_iterator<char> iter(file);
std::copy(iter, std::istreambuf_iterator<char>(), std::back_inserter(ptx));
load_ptx();
} else {
throw std::runtime_error("cannot find "+path);
}
}
void run(void** args) {
if (ptx.empty()) throw std::runtime_error("Compile or load the ptx before trying to run it!");
//cuCtxSetCurrent(cuda.context);
CUDA_SAFE_CALL(
cuLaunchKernel(kernel,
NUM_THREADS, 1, 1, // grid dim
NUM_BLOCKS, 1, 1, // block dim
0, NULL, // shared mem and stream
args, 0)); // arguments
CUDA_SAFE_CALL(cuCtxSynchronize());
}
void clear() {
if (!ptx.empty()) {
CUDA_SAFE_CALL(cuModuleUnload(module));
ptx.clear();
}
}
};