-
Notifications
You must be signed in to change notification settings - Fork 11
/
nvptx-run.cc
425 lines (365 loc) · 12.4 KB
/
nvptx-run.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* A tool to run PTX binaries compiled with -mmainkernel.
Copyright (C) 2014, 2015 Mentor Graphics
Copyright (C) 2016 Ivannikov Institute for System Programming of the Russian Academy of Sciences
Copyright (C) 2022, 2023 Siemens
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with nvptx-tools; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifndef NVPTX_RUN_INCLUDE_SYSTEM_CUDA_H
# include "cuda/cuda.h"
#else
# include <cuda.h>
/* On systems where installed NVIDIA driver is newer than CUDA Toolkit,
libcuda.so may have these functions even though <cuda.h> does not. */
extern "C" CUresult cuGetErrorName (CUresult, const char **);
extern "C" CUresult cuGetErrorString (CUresult, const char **);
#endif
#define HAVE_DECL_BASENAME 1
#include "libiberty.h"
#include <iostream>
#include <sstream>
#include "version.h"
#define DO_PRAGMA(x) _Pragma (#x)
#ifndef NVPTX_RUN_LINK_LIBCUDA
# include <dlfcn.h>
static struct cuda_lib_s {
# define CUDA_ONE_CALL(call) \
__typeof (::call) *call;
# define CUDA_ONE_CALL_MAYBE_NULL(call) \
CUDA_ONE_CALL (call)
# include "nvptx-run-cuda-lib.def"
# undef CUDA_ONE_CALL
# undef CUDA_ONE_CALL_MAYBE_NULL
} cuda_lib;
/* -1 if init_cuda_lib has not been called yet, false
if it has been and failed, true if it has been and succeeded. */
static signed char cuda_lib_inited = -1;
/* Dynamically load the CUDA runtime library and initialize function
pointers, return false if unsuccessful, true if successful. */
static bool
init_cuda_lib (void)
{
if (cuda_lib_inited != -1)
return cuda_lib_inited;
const char *cuda_runtime_lib = "libcuda.so.1";
void *h = dlopen (cuda_runtime_lib, RTLD_LAZY);
cuda_lib_inited = false;
if (h == NULL)
return false;
# define CUDA_ONE_CALL(call) CUDA_ONE_CALL_1 (call, false)
# define CUDA_ONE_CALL_MAYBE_NULL(call) CUDA_ONE_CALL_1 (call, true)
# define CUDA_ONE_CALL_1(call, allow_null) \
cuda_lib.call = (__typeof (call) *) dlsym (h, #call); \
if (!allow_null && cuda_lib.call == NULL) \
return false;
# include "nvptx-run-cuda-lib.def"
# undef CUDA_ONE_CALL
# undef CUDA_ONE_CALL_1
# undef CUDA_ONE_CALL_MAYBE_NULL
cuda_lib_inited = true;
return true;
}
# define CUDA_CALL_PREFIX cuda_lib.
#else
# define CUDA_ONE_CALL(call)
# define CUDA_ONE_CALL_MAYBE_NULL(call) DO_PRAGMA (weak call)
# include "nvptx-run-cuda-lib.def"
# undef CUDA_ONE_CALL_MAYBE_NULL
# undef CUDA_ONE_CALL
# define CUDA_CALL_PREFIX
# define init_cuda_lib() true
#endif
#define CUDA_CALL_NOCHECK(FN, ...) \
CUDA_CALL_PREFIX FN (__VA_ARGS__)
#define CUDA_CALL_EXISTS(FN) \
CUDA_CALL_PREFIX FN
static void
fatal_error (const std::string &error_string)
{
std::cerr << "nvptx-run: " << error_string << "\n";
exit (127);
}
static void
fatal_unless_success (CUresult r, const char *err)
{
if (r == CUDA_SUCCESS)
return;
const char *s = "[unknown]";
if (CUDA_CALL_EXISTS (cuGetErrorString))
CUDA_CALL_NOCHECK (cuGetErrorString, r, &s);
const char *n = "[unknown]";
if (CUDA_CALL_EXISTS (cuGetErrorName))
CUDA_CALL_NOCHECK (cuGetErrorName, r, &n);
std::ostringstream error_stream;
error_stream << err << ": " << s << " ("<< n << ", " << (int) r << ")";
fatal_error (error_stream.str ());
}
static size_t jitopt_lineinfo, jitopt_debuginfo, jitopt_optimize = 4;
static void
compile_file (FILE *f, CUmodule *phModule)
{
CUresult r;
char elog[8192];
CUjit_option opts[] = {
CU_JIT_ERROR_LOG_BUFFER, CU_JIT_ERROR_LOG_BUFFER_SIZE_BYTES,
CU_JIT_GENERATE_LINE_INFO,
CU_JIT_GENERATE_DEBUG_INFO,
CU_JIT_OPTIMIZATION_LEVEL,
};
void *optvals[] = {
elog, (void*) sizeof elog,
(void*) jitopt_lineinfo,
(void*) jitopt_debuginfo,
(void*) jitopt_optimize,
};
CUlinkState linkstate;
r = CUDA_CALL_NOCHECK (cuLinkCreate,
sizeof opts / sizeof *opts, opts, optvals,
&linkstate);
fatal_unless_success (r, "cuLinkCreate failed");
fseek (f, 0, SEEK_END);
int len = ftell (f);
fseek (f, 0, SEEK_SET);
char *program = new char[len + 1];
fread (program, 1, len, f);
program[len] = '\0';
int off = 0;
int count = 0;
while (off < len)
{
char namebuf[100];
int l = strlen (program + off);
sprintf (namebuf, "input file %d at offset %d", count++, off);
r = CUDA_CALL_NOCHECK (cuLinkAddData, linkstate,
CU_JIT_INPUT_PTX, program + off, l + 1,
namebuf, 0, 0, 0);
if (r != CUDA_SUCCESS)
{
#if 0
std::cerr << (program + off);
#endif
std::cerr << elog << "\n";
fatal_unless_success (r, "cuLinkAddData failed");
}
off += l;
while (off < len && program[off] == '\0')
off++;
}
delete[] program;
program = NULL;
void *linkout;
r = CUDA_CALL_NOCHECK (cuLinkComplete, linkstate, &linkout, NULL);
if (r != CUDA_SUCCESS)
{
std::cerr << elog << "\n";
fatal_unless_success (r, "cuLinkComplete failed");
}
r = CUDA_CALL_NOCHECK (cuModuleLoadData, phModule, linkout);
fatal_unless_success (r, "cuModuleLoadData failed");
r = CUDA_CALL_NOCHECK (cuLinkDestroy, linkstate);
fatal_unless_success (r, "cuLinkDestroy failed");
}
ATTRIBUTE_NORETURN static void
usage (std::ostream &out_stream, int status)
{
out_stream << "\
Usage: nvptx-none-run [option...] program [argument...]\n\
Options:\n\
-S, --stack-size N Set per-lane GPU stack size to N (default: auto)\n\
-H, --heap-size N Set GPU heap size to N (default: 256 MiB)\n\
-L, --lanes N Launch N lanes (for testing gcc -muniform-simt)\n\
-O, --optlevel N Pass PTX JIT option to set optimization level N\n\
-g, --lineinfo Pass PTX JIT option to generate line information\n\
-G, --debuginfo Pass PTX JIT option to generate debug information\n\
--help Print this help and exit\n\
--version Print version number and exit\n\
\n\
Report bugs to " << REPORT_BUGS_TO << ".\n";
exit (status);
}
static const struct option long_options[] =
{
{ "stack-size", required_argument, 0, 'S' },
{ "heap-size", required_argument, 0, 'H' },
{ "lanes", required_argument, 0, 'L' },
{ "optlevel", required_argument, 0, 'O' },
{ "lineinfo", no_argument, 0, 'g' },
{ "debuginfo", no_argument, 0, 'G' },
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'V' },
{ 0, 0, 0, 0 }
};
int
main (int argc, char **argv)
{
int o;
long stack_size = 0, heap_size = 256 * 1024 * 1024, num_lanes = 1;
while ((o = getopt_long (argc, argv, "+S:H:L:O:gGhV", long_options, 0)) != -1)
{
switch (o)
{
case 'S':
stack_size = strtol (optarg, NULL, 0);
if (stack_size <= 0)
fatal_error ("invalid stack size");
break;
case 'H':
heap_size = strtol (optarg, NULL, 0);
if (heap_size <= 0)
fatal_error ("invalid heap size");
break;
case 'L':
num_lanes = strtol (optarg, NULL, 0);
if (num_lanes < 1 || num_lanes > 32)
fatal_error ("invalid lane count");
break;
case 'O':
jitopt_optimize = (size_t) optarg[0] - '0';
if (jitopt_optimize > 4 || optarg[1] != 0)
fatal_error ("invalid optimization level");
break;
case 'g':
jitopt_lineinfo = 1;
break;
case 'G':
jitopt_debuginfo = 1;
break;
case 'h':
usage (std::cout, 0);
break;
case 'V':
std::cout << "\
nvptx-none-run " << PKGVERSION << NVPTX_TOOLS_VERSION << "\n\
Copyright (C) 2024 The nvptx-tools Developers\n\
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n\
This program is free software; you may redistribute it under the terms of\n\
the GNU General Public License version 3 or later.\n\
This program has absolutely no warranty.\n";
exit (0);
default:
usage (std::cerr, 1);
break;
}
}
if (argc < optind + 1)
fatal_error ("no program file specified");
const char *progname = argv[optind];
FILE *f = fopen (progname, "r");
if (f == NULL)
fatal_error ("program file not found");
if (!init_cuda_lib ())
fatal_error ("couldn't initialize CUDA Driver");
CUresult r;
r = CUDA_CALL_NOCHECK (cuInit, 0);
fatal_unless_success (r, "cuInit failed");
CUdevice dev;
CUcontext ctx;
r = CUDA_CALL_NOCHECK (cuDeviceGet, &dev, 0);
fatal_unless_success (r, "cuDeviceGet failed");
r = CUDA_CALL_NOCHECK (cuCtxCreate, &ctx, 0, dev);
fatal_unless_success (r, "cuCtxCreate failed");
CUdeviceptr d_retval;
r = CUDA_CALL_NOCHECK (cuMemAlloc, &d_retval, sizeof (int));
fatal_unless_success (r, "cuMemAlloc failed");
int d_argc = argc - optind;
/* The argv pointers, followed by the actual argv strings. */
CUdeviceptr d_argv;
{
size_t s_d_argv = d_argc * sizeof (char *);
for (int arg = optind; arg < argc; ++arg)
s_d_argv += strlen (argv[arg]) + 1;
r = CUDA_CALL_NOCHECK (cuMemAlloc, &d_argv, s_d_argv);
fatal_unless_success (r, "cuMemAlloc failed");
/* Skip the argv pointers. */
size_t pos = d_argc * sizeof (char *);
for (int arg = optind; arg < argc; ++arg)
{
CUdeviceptr d_arg = (CUdeviceptr) ((char *) d_argv + pos);
size_t len = strlen (argv[arg]) + 1;
r = CUDA_CALL_NOCHECK (cuMemcpyHtoD, d_arg, argv[arg], len);
fatal_unless_success (r, "cuMemcpyHtoD (d_arg) failed");
r = CUDA_CALL_NOCHECK (cuMemcpyHtoD,
(CUdeviceptr) ((char *) d_argv
+ (arg - optind) * sizeof (char *)),
&d_arg, sizeof (char *));
fatal_unless_success (r, "cuMemcpyHtoD (d_argv) failed");
pos += len;
}
}
#if 0
/* Default seems to be 1 KiB stack, 8 MiB heap. */
size_t stack, heap;
CUDA_CALL_NOCHECK (cuCtxGetLimit, &stack, CU_LIMIT_STACK_SIZE);
CUDA_CALL_NOCHECK (cuCtxGetLimit, &heap, CU_LIMIT_MALLOC_HEAP_SIZE);
std::cout << "stack " << stack << " heap " << heap << "\n";
#endif
if (!stack_size)
{
/* It appears that CUDA driver sometimes accounts memory as if stacks
were reserved for the maximum number of threads the device can host,
even if only a few are launched. Compute the default accordingly. */
int sm_count, thread_max;
r = CUDA_CALL_NOCHECK (cuDeviceGetAttribute,
&sm_count,
CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT,
dev);
fatal_unless_success (r, "could not get SM count");
r = CUDA_CALL_NOCHECK (cuDeviceGetAttribute,
&thread_max,
CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_MULTIPROCESSOR,
dev);
fatal_unless_success (r, "could not get max threads per SM count");
size_t mem;
{
size_t mem_free, mem_total;
r = CUDA_CALL_NOCHECK (cuMemGetInfo, &mem_free, &mem_total);
fatal_unless_success (r, "could not get free and total memory");
mem = mem_free;
}
/* Subtract heap size and a 128 MiB extra. */
mem -= heap_size + 128 * 1024 * 1024;
mem /= sm_count * thread_max;
/* Always limit default size to 128 KiB maximum. */
if (mem > 128 * 1024)
mem = 128 * 1024;
/* Round down to 8-byte boundary. */
stack_size = mem & -8u;
}
r = CUDA_CALL_NOCHECK (cuCtxSetLimit, CU_LIMIT_STACK_SIZE, stack_size);
fatal_unless_success (r, "could not set stack limit");
r = CUDA_CALL_NOCHECK (cuCtxSetLimit, CU_LIMIT_MALLOC_HEAP_SIZE, heap_size);
fatal_unless_success (r, "could not set heap limit");
CUmodule hModule = 0;
compile_file (f, &hModule);
fclose (f);
f = NULL;
CUfunction hKernel = 0;
r = CUDA_CALL_NOCHECK (cuModuleGetFunction, &hKernel, hModule, "__main");
fatal_unless_success (r, "could not find kernel __main");
void *args[] = { &d_retval, &d_argc, &d_argv };
r = CUDA_CALL_NOCHECK (cuLaunchKernel,
hKernel, 1, 1, 1, num_lanes, 1, 1,
1024, NULL, args, NULL);
fatal_unless_success (r, "error launching kernel");
int result;
r = CUDA_CALL_NOCHECK (cuMemcpyDtoH, &result, d_retval, sizeof (int));
fatal_unless_success (r, "error getting kernel result");
CUDA_CALL_NOCHECK (cuMemFree, d_retval);
CUDA_CALL_NOCHECK (cuMemFree, d_argv);
if (hModule)
CUDA_CALL_NOCHECK (cuModuleUnload, hModule);
CUDA_CALL_NOCHECK (cuCtxDestroy, ctx);
return result;
}