-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkernel-runner.cpp
386 lines (318 loc) · 11.4 KB
/
kernel-runner.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <algorithm>
#include <array>
#include <cstring>
#include <stdexcept>
#include <getopt.h>
#include <wordexp.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <dlfcn.h>
#include <boost/filesystem.hpp>
#include <boost/regex.hpp>
#ifdef __linux__
#include <sched.h>
#include <pthread.h>
#endif
#include "Algorithm.hpp"
#include "Backend.hpp"
#include "CUDA.hpp"
#include "ImplementationTemplate.hpp"
#include "OpenCL.hpp"
#include "options/Options.hpp"
#include "Timer.hpp"
#include "utils/Util.hpp"
#define TO_STRING(a) xstr(a)
#define xstr(a) #a
#ifndef VERSION
#define VERSION
#endif
using namespace std;
using namespace boost::filesystem;
ImplementationTemplateBase<false>::~ImplementationTemplateBase()
{}
ImplementationTemplateBase<true>::~ImplementationTemplateBase()
{}
enum class framework { cuda, opencl };
static map<string, Algorithm> algorithms;
static bool debug = false;
static bool verbose = false;
static bool warnings = false;
static bool noOutput = false;
static bool printStdOut = false;
static bool fromStdin = false;
static framework fw = framework::cuda;
static int device = 0;
static size_t platform = 0;
static string outputDir(".");
static string algorithmName = "";
static string kernelName = "";
static vector<string> libPaths = { "." };
static const char *exeName = "kernel-runner";
static Options options('h', "help", cout, [](ostream& out)
{
out << "Usage:" << endl;
out << exeName << " list platforms [-v | --verbose]" << endl;
out << exeName << " list devices [-p NUM | --platform NUM] "
<< "[-v | --verbose]" << endl;
out << exeName << " list algorithms [-v | --verbose] "
<< "[-L PATH | --lib PATH]" << endl;
out << exeName << " list implementations [-a NAME | --algorithm NAME] "
<< "[-v | --verbose] [-L PATH | --lib PATH]" << endl;
out << exeName << " query platform [-p NUM | --platform NUM] "
<< "[-v | --verbose]" << endl;
out << exeName << " query device [-p NUM | --platform NUM] "
<< "[-d NUM | --device NUM] [-v | --verbose]" << endl;
out << exeName << " query algorithm-version [-a NAME | --algorithm NAME]"
<< endl;
out << exeName << " -S" << endl;
out << exeName << " -a ALGORITHM -k KERNEL [OPTIONS] <graph file(s)>"
<< endl;
out << endl << "Options:" << endl;
});
static void __attribute__((noreturn))
usage(int exitCode = EXIT_FAILURE)
{
options.usage(cout, " ");
exit(exitCode);
}
static map<string, Algorithm>
loadAlgorithms
(const char *sym, vector<string> &paths)
{
map<string, string> libs;
map<string, Algorithm> result;
if (is_directory("./.build/kernels/")) {
paths.insert(paths.begin(), "./.build/kernels/");
}
boost::smatch match;
const char *regex = "lib(.*)kernel" TO_STRING(VERSION) "\\.so";
const char *debug_regex = "lib(.*)kerneldebug" TO_STRING(VERSION) "\\.so";
const boost::regex lib_regex(debug ? debug_regex : regex);
for (auto p_str : paths) {
if (!is_directory(p_str)) continue;
for (auto& entry : directory_iterator(p_str)) {
if (!is_regular_file(entry)) continue;
auto p = entry.path();
const string fileNameString = p.filename().string();
if (boost::regex_match(fileNameString, match, lib_regex)) {
libs.emplace(match[1], p.string());
}
}
}
for (auto lib : libs) {
void *hnd = dlopen(lib.second.c_str(), RTLD_NOW);
if (!hnd) {
if (warnings) {
cerr << "dlopen() failed: " << lib.second << endl
<< dlerror() << endl;
}
continue;
}
auto getAlgo = reinterpret_cast<register_algorithm_t*>(dlsym(hnd, sym));
if (getAlgo != nullptr) getAlgo(result[lib.first]);
else if (warnings) {
cerr << "dlsym() failed: " << sym << " (" << lib.second << ") "
<< endl << dlerror() << endl;
}
}
return result;
}
static Algorithm&
getAlgorithm(string algoName)
{
std::string errorMsg;
std::ostringstream names;
try {
return algorithms.at(algoName);
} catch (const out_of_range &) {
for (auto& algorithm : algorithms) {
names << " " << algorithm.first << endl;
}
if (algoName.empty()) {
errorMsg = "Algorithm name not specified!";
} else {
errorMsg = "No algorithm named \"" + algoName + "\"!";
}
reportError(errorMsg, "\n\nSupported algorithms:\n", names.str());
}
}
static void
handle_subcommands(Backend& backend, const vector<string>& args)
{
if (args.size() < 1) return;
if (args[0] != "list" && args[0] != "query") return;
if (args.size() < 2) usage();
if (args[0] == "list" && args[1] == "platforms") {
backend.listPlatforms(verbose);
exit(static_cast<int>(backend.platformCount()));
} else if (args[0] == "list" && args[1] == "devices") {
backend.listDevices(platform, verbose);
exit(backend.deviceCount(platform));
} else if (args[0] == "list" && args[1] == "algorithms") {
for (auto& [algoName, algorithm] : algorithms) {
cout << algoName << endl;
if (verbose) {
for (auto & [kernelName, kernel] : algorithm) {
cout << " " << kernelName << endl;
kernel->help(cout, "\t");
}
}
}
exit(EXIT_SUCCESS);
} else if (args[0] == "list" && args[1] == "implementations") {
auto& algorithm = getAlgorithm(algorithmName);
for (auto & [kernelName, kernel] : algorithm) {
cout << kernelName << endl;
if (verbose) kernel->help(cout, " ");
}
exit(EXIT_SUCCESS);
} else if (args[0] == "query" && args[1] == "platform") {
backend.queryPlatform(platform, verbose);
exit(backend.deviceCount(platform));
} else if (args[0] == "query" && args[1] == "device") {
backend.queryDevice(platform, device, verbose);
exit(EXIT_SUCCESS);
} else if (args[0] == "query" && args[1] == "algorithm-version") {
auto& algorithm = getAlgorithm(algorithmName);
cout << algorithm.commit() << endl;
exit(EXIT_SUCCESS);
} else {
usage();
}
}
static void
runJob
( std::string algoName
, std::string kernName
, vector<string> args
, const string& tag = string()
)
{
auto& algorithm = getAlgorithm(algoName);
algorithm.selectKernel(kernName);
auto graphs = algorithm.setup(args);
if (!tag.empty() && graphs.size() > 1) {
reportError("Tagged output with more than 1 graph!");
}
for (auto graph : graphs) {
auto label = tag.empty() ? path(graph).stem().string() : tag;
auto basePath = outputDir / path(label);
basePath += ".timings";
auto timeFile = basePath;
auto outputFile = basePath.replace_extension(".output");
if (noOutput) {
timeFile = "/dev/null";
outputFile = "/dev/null";
}
{
Epoch epoch(printStdOut ? "/dev/stdout" : timeFile.string(), verbose);
algorithm(graph, outputFile.string());
}
cout << algorithm.commit() << ":" << label << endl;
}
}
static void
pin_cpu()
{
#ifdef __linux__
cpu_set_t cpuset;
pthread_t thread = pthread_self();
CPU_ZERO(&cpuset);
CPU_SET(0, &cpuset);
int err = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (err != 0) reportError("pthread_setaffinity_np failed!");
err = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (err != 0) reportError("pthread_getaffinity_np failed!");
if (!CPU_ISSET(0, &cpuset)) reportError("Not set to CPU 0!");
for (int i = 1; i < CPU_SETSIZE; i++) {
if (CPU_ISSET(i, &cpuset)) reportError("Affinity for CPU #", i, "!");
}
#endif
}
int main(int argc, char * const *argv)
{
pin_cpu();
std::reference_wrapper<Backend> activeBackend(CUDA);
options.add('d', "device", "NUM", device, "Device to use.")
.add('f', "framework", fw, framework::opencl, "Use OpenCL.")
.add('L', "lib", "PATH", libPaths, "\".\"",
"Search path for algorithm libraries.")
.add('o', "output-dir", "DIR", outputDir,
"Location to use for writing algorithm output.")
.add('O', "output", printStdOut, true,
"Print timings to stdout, inhibits timings file creation.")
.add('p', "platform", "NUM", platform, "Platform to use.")
.add('g', "debug", debug, true, "Use debug kernels.")
.add('v', "verbose", verbose, true, "Verbose output.")
.add('W', "warn", warnings, true, "Verbose/debug warnings.")
.add('S', "stdin", fromStdin, true, "Read work from stdin.")
.add('q', "quiet", noOutput, true,
"Inhibit creation of output and timing files.");
Options kernelParser(options);
kernelParser.add('a', "algorithm", "NAME", algorithmName,
"Algorithm to use.")
.add('k', "kernel", "NAME", kernelName,
"Which algorithm implementation to use.");
set_new_handler(out_of_memory);
locale::global(locale(""));
cout.imbue(locale());
struct rlimit limits;
limits.rlim_cur = RLIM_INFINITY;
limits.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &limits);
auto optionResult = options.parseArgsNoUsage(argc, argv);
switch (fw) {
case framework::opencl: {
activeBackend = OpenCL;
algorithms = loadAlgorithms("registerOpenCL", libPaths);
{
//array<const char*,1> files {{&_binary_kernel_cl_start}};
//array<size_t,1> sizes {{(size_t) &_binary_kernel_cl_size}};
//cl_kernel initKernel = opencl.createKernel("init", files, sizes);
//cl_kernel runKernel = opencl.createKernel("BFS", files, sizes);
//benchmark<OpenCL>(opencl, initKernel, runKernel, graph, root, run_count, outputFile);
}
break;
}
case framework::cuda: {
algorithms = loadAlgorithms("registerCUDA", libPaths);
break;
}
}
Backend& backend = activeBackend;
handle_subcommands(backend, optionResult.remainingArgs);
bool missingArgs = !fromStdin && optionResult.remainingArgs.empty();
if (optionResult.usageRequested || missingArgs) {
options.usage(cout, " ");
if (!algorithmName.empty()) {
auto& algorithm = getAlgorithm(algorithmName);
algorithm.help(cout, " ");
}
exit(EXIT_SUCCESS);
}
if (!backend.initialised) {
cerr << "Backend not initialised: No devices?" << endl;
exit(EXIT_FAILURE);
}
backend.setDevice(platform, device);
if (fromStdin) {
string tag;
string line;
wordexp_t newArgv;
vector<string> remainingArgs;
while (getline(cin, line)) {
if (wordexp(line.c_str(), &newArgv, WRDE_NOCMD | WRDE_UNDEF)) {
reportError("Failed to expand commandline with wordexp(3)!");
}
remainingArgs = kernelParser.parseArgs
(static_cast<int>(newArgv.we_wordc), newArgv.we_wordv);
tag = newArgv.we_wordv[0];
runJob(algorithmName, kernelName, remainingArgs, tag);
kernelParser.reset();
wordfree(&newArgv);
}
} else {
runJob(algorithmName, kernelName, optionResult.remainingArgs);
}
return 0;
}