-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile_ptx.cpp
72 lines (59 loc) · 1.82 KB
/
compile_ptx.cpp
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
#include <nvrtc.h>
#include <vector>
#include "compile_ptx.h"
inline void NVRTC_CHECK(nvrtcResult result)
{
if(result != NVRTC_SUCCESS)
THError(nvrtcGetErrorString(result));
}
void compilePTX(const char* src,
const char* headers[],
const char* includeNames[],
std::vector<char>& ptx)
{
nvrtcProgram program;
NVRTC_CHECK(nvrtcCreateProgram(&program, src, NULL, 1, headers, includeNames));
nvrtcResult result = nvrtcCompileProgram(program, 0, NULL);
if(result == NVRTC_ERROR_COMPILATION)
{
size_t logsize;
nvrtcGetProgramLogSize(program, &logsize);
std::vector<char> log(logsize);
nvrtcGetProgramLog(program, log.data());
THError(log.data());
}
else
NVRTC_CHECK(result);
size_t ptx_size;
NVRTC_CHECK(nvrtcGetPTXSize(program, &ptx_size));
ptx.resize(ptx_size);
NVRTC_CHECK(nvrtcGetPTX(program, ptx.data()));
NVRTC_CHECK(nvrtcDestroyProgram(&program));
}
inline void CUDA_CHECK(CUresult result)
{
if(result != CUDA_SUCCESS)
{
const char* errstr;
cuGetErrorString(result, &errstr);
THError(errstr);
}
}
void launch(const char* ptx, const char* name, void* args[], dim3 grid, dim3 block, CUstream stream)
{
CUmodule module;
CUfunction func;
CUDA_CHECK(cuModuleLoadData(&module, ptx));
CUDA_CHECK(cuModuleGetFunction(&func, module, name));
CUDA_CHECK(cuLaunchKernel(func,
grid.x, grid.y, grid.z,
block.x, block.y, block.z,
0, stream, args, NULL));
CUDA_CHECK(cuModuleUnload(module));
}
extern "C"
void launchPTX(THCState* state, const char* ptx, const char* name, void* args[], int* grid, int* block)
{
cudaStream_t stream = THCState_getCurrentStream(state);
launch(ptx, name, args, dim3(grid[0], grid[1], grid[2]), dim3(block[0], block[1], block[2]), (CUstream)stream);
}