-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_full.cpp
184 lines (146 loc) · 5.54 KB
/
test_full.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
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
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include "cuda.h"
#include "nvvm.h"
void checkCudaErrors(CUresult err) {
if (err != CUDA_SUCCESS) {
const char *ret = NULL;
cuGetErrorName(err, &ret);
std::cerr << "CUDA error: " << ret << "\n";
exit(1);
}
}
void checkNVVMErrors(nvvmResult err) {
if (err != NVVM_SUCCESS) {
std::cerr << "NVVM Error: " << nvvmGetErrorString(err) << "\n";
exit(1);
}
}
std::string loadFile(const std::string& filename) {
std::ifstream t(filename);
if (!t.is_open()) {
std::cerr << filename << " not found\n";
return "";
}
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return str;
}
/// main - Program entry point
int main(int argc, char **argv) {
CUdevice device;
CUmodule cudaModule;
CUcontext context;
CUfunction function;
CUlinkState linker;
int devCount;
// CUDA initialization
checkCudaErrors(cuInit(0));
checkCudaErrors(cuDeviceGetCount(&devCount));
checkCudaErrors(cuDeviceGet(&device, 0));
char name[128];
checkCudaErrors(cuDeviceGetName(name, 128, device));
std::cout << "Using CUDA Device [0]: " << name << "\n";
int devMajor, devMinor;
checkCudaErrors(cuDeviceGetAttribute(&devMajor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, device));
checkCudaErrors(cuDeviceGetAttribute(&devMinor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, device));
std::cout << "Device Compute Capability: "
<< devMajor << "." << devMinor << "\n";
if (devMajor < 2) {
std::cerr << "ERROR: Device 0 is not SM 2.0 or greater\n";
return 1;
}
auto str = loadFile("t2.ll");
//print str
// std::cout << str << "\n";
nvvmProgram prog;
auto libdevice_str = loadFile(std::string(std::getenv("CUDA_ROOT")) + "/nvvm/libdevice/libdevice.10.bc");
const char *libdeviceMod = libdevice_str.c_str();
size_t libdeviceModSize = libdevice_str.size();
std::cout << "libdeviceModSize = " << libdeviceModSize << std::endl;
const char *myIr = str.c_str();
size_t myIrSize = str.size();
// Create NVVM program object
checkNVVMErrors(nvvmCreateProgram(&prog));
// Add libdevice module to program
checkNVVMErrors(nvvmAddModuleToProgram(prog, libdeviceMod, libdeviceModSize, "libdevice"));
// Add custom IR to program
checkNVVMErrors(nvvmAddModuleToProgram(prog, myIr, myIrSize, "myIr"));
// Declare compile options
const char *options[] = { "-arch=compute_80" };
// Compile the program
checkNVVMErrors(nvvmCompileProgram(prog, 1, options));
// Get compiled module
char* compiled_module;
size_t compiled_module_size;
checkNVVMErrors(nvvmGetCompiledResultSize(prog, &compiled_module_size));
std::cout << "Compiled module size: " << compiled_module_size << "\n";
compiled_module = (char*)malloc(compiled_module_size);
checkNVVMErrors(nvvmGetCompiledResult(prog, compiled_module));
std::cout << "Compiled module\n";
// print compiled_module to file
std::ofstream outfile;
outfile.open("t2_test.ptx");
outfile << compiled_module;
outfile.close();
std::string compiled_ptx_module{compiled_module};
// Create driver context
checkCudaErrors(cuCtxCreate(&context, 0, device));
std::cout << "Created context" << std::endl;
// Create module for object
checkCudaErrors(cuModuleLoadDataEx(&cudaModule, compiled_module, 0, 0, 0));
std::cout << "Loaded my LLVM IR module" << std::endl;
// Get kernel function
checkCudaErrors(cuModuleGetFunction(&function, cudaModule, "kernel"));
// Device data
CUdeviceptr devBufferA;
CUdeviceptr devBufferB;
CUdeviceptr devBufferC;
checkCudaErrors(cuMemAlloc(&devBufferA, sizeof(float)*16));
checkCudaErrors(cuMemAlloc(&devBufferB, sizeof(float)*16));
checkCudaErrors(cuMemAlloc(&devBufferC, sizeof(float)*16));
float* hostA = new float[16];
float* hostB = new float[16];
float* hostC = new float[16];
// Populate input
for (unsigned i = 0; i != 16; ++i) {
hostA[i] = (float)i;
hostB[i] = (float)(2*i);
hostC[i] = 0.0f;
}
checkCudaErrors(cuMemcpyHtoD(devBufferA, &hostA[0], sizeof(float)*16));
checkCudaErrors(cuMemcpyHtoD(devBufferB, &hostB[0], sizeof(float)*16));
unsigned blockSizeX = 16;
unsigned blockSizeY = 1;
unsigned blockSizeZ = 1;
unsigned gridSizeX = 1;
unsigned gridSizeY = 1;
unsigned gridSizeZ = 1;
// Kernel parameters
void *KernelParams[] = { &devBufferA, &devBufferB, &devBufferC };
std::cout << "Launching kernel\n";
// Kernel launch
checkCudaErrors(cuLaunchKernel(function, gridSizeX, gridSizeY, gridSizeZ,
blockSizeX, blockSizeY, blockSizeZ,
0, NULL, KernelParams, NULL));
// Retrieve device data
checkCudaErrors(cuMemcpyDtoH(&hostC[0], devBufferC, sizeof(float)*16));
std::cout << "Results:\n";
for (unsigned i = 0; i != 16; ++i) {
std::cout << "pow(" << hostA[i] << ", " << hostB[i] << ") = " << hostC[i] << "\n";
}
// Clean up after ourselves
delete [] hostA;
delete [] hostB;
delete [] hostC;
// Clean-up
checkCudaErrors(cuMemFree(devBufferA));
checkCudaErrors(cuMemFree(devBufferB));
checkCudaErrors(cuMemFree(devBufferC));
checkCudaErrors(cuModuleUnload(cudaModule));
checkCudaErrors(cuCtxDestroy(context));
return 0;
}