-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
157 lines (126 loc) · 3.75 KB
/
main.cc
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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <string>
#include <fstream>
#include <vector>
#ifdef _WIN32
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#else
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#endif
#include "clew.h"
#include "muda_runtime.h"
//#include "timerutil.h"
#include "OptionParser.h"
void usage(const char *prog) {
printf("Usage: %s <options> input.cl\n", prog);
printf(" <options>\n");
printf("\n");
printf(" --verbose Verbose mode.\n");
printf(" --platform=N Specify platform ID.\n");
printf(" --device=N Specify device ID.\n");
printf(
" --clopt=STRING Specify compiler options for OpenCL compiler.\n");
printf(" --header=FILENAME Specify custom header file to be included.\n");
printf(" -c Build kernel module.\n");
}
std::string readfile(const char *path) {
std::ifstream clsrc(path);
if (!clsrc) {
fprintf(stderr, "File not found or failed to read: %s\n", path);
return std::string();
}
std::istreambuf_iterator<char> vdataBegin(clsrc);
std::istreambuf_iterator<char> vdataEnd;
std::string clstr(vdataBegin, vdataEnd);
return clstr;
}
int main(int argc, char *const argv[]) {
if (argc < 2) {
usage(argv[0]);
exit(1);
}
optparse::OptionParser parser = optparse::OptionParser();
parser.set_defaults("verbosity", "0");
parser.add_option("--verbose").action("store_true").dest("verbosity");
parser.add_option("--platform")
.action("store")
.type("int")
.set_default(0)
.help("default: %default");
parser.add_option("--device").action("store").type("int").set_default(0).help(
"default: %default");
parser.add_option("--header").dest("header");
parser.add_option("--clopt").action("store").type("string");
parser.add_option("-c").action("store_true").dest("module");
optparse::Values &options = parser.parse_args(argc, argv);
std::vector<std::string> args = parser.args();
if (args.size() < 1) {
printf("Needs input OpenCL kernel file.\n");
usage(argv[0]);
exit(1);
}
{
int ret = clewInit();
if (ret != CLEW_SUCCESS) {
std::cerr << "Failed to find OpenCL device." << std::endl;
return EXIT_FAILURE;
}
}
bool verb = (bool)options.get("verbosity");
bool module = (bool)options.get("module");
int reqPlatformID = (int)options.get("platform");
int deviceNum = (int)options.get("device");
const char *headerfilename = options["header"].c_str();
// printf("Use platform: %d\n", reqPlatformID);
muda::MUDADeviceOCL *device = new muda::MUDADeviceOCL(muda::ocl_cpu);
assert(device);
bool ret = device->initialize(reqPlatformID, deviceNum, verb);
assert(ret);
int numDevices = device->getNumDevices();
std::string kernelfile = args.at(0);
std::string cloptions = options["clopt"];
if (verb) {
printf("clopts = %s\n", cloptions.c_str());
}
std::string headerStr;
if (headerfilename) {
if (verb)
printf("Reading header file: %s\n", headerfilename);
headerStr = readfile(headerfilename);
}
muda::MUDAProgram prog;
if (headerStr.empty()) {
prog = device->loadKernelSource(kernelfile.c_str(), 0, NULL, cloptions.c_str());
} else {
const char *headers[1];
headers[0] = headerStr.c_str();
prog = device->loadKernelSource(kernelfile.c_str(), 1, headers, cloptions.c_str());
}
if (!prog) {
return -1;
}
if (module) {
std::vector<char> bins;
bool ret = device->getModule(prog, bins);
if (!ret) {
return -1;
}
if (bins.size() == 0) {
return -1;
}
FILE* fp = fopen("module.dat", "wb");
if (!fp) {
return -1;
}
fwrite(&bins.at(0), 1, bins.size(), fp);
fclose(fp);
}
return 0;
}