-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
92 lines (76 loc) · 2.87 KB
/
test.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
#include <iostream>
#include <fstream>
#include <cassert>
#include "cuda.h"
#include "nvvm.h"
void checkCudaErrors(CUresult err) {
assert(err == CUDA_SUCCESS);
}
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("/home/magkanar/spack_software/linux-ubuntu20.04-skylake/gcc-11.1.0/nvhpc-22.1-zsmejw/Linux_x86_64/22.1/cuda/11.5/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
nvvmCreateProgram(&prog);
// Add libdevice module to program
nvvmAddModuleToProgram(prog, libdeviceMod, libdeviceModSize, "libdevice");
// Add custom IR to program
nvvmAddModuleToProgram(prog, myIr, myIrSize, "myIr");
// Declare compile options
const char *options[] = { "-ftz=1" };
// Compile the program
nvvmCompileProgram(prog, 1, options);
// Get compiled module
char* compiled_module;
size_t compiled_module_size;
nvvmGetCompiledResultSize(prog, &compiled_module_size);
std::cout << "Compiled module size: " << compiled_module_size << "\n";
compiled_module = (char*)malloc(compiled_module_size);
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();
}