-
Notifications
You must be signed in to change notification settings - Fork 0
/
osu_coll.c
478 lines (406 loc) · 10.9 KB
/
osu_coll.c
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Copyright (C) 2002-2013 the Network-Based Computing Laboratory
* (NBCL), The Ohio State University.
*
* Contact: Dr. D. K. Panda (panda@cse.ohio-state.edu)
*
* For detailed copyright and licensing information, please refer to the
* copyright file COPYRIGHT in the top level directory.
*/
/*
* HEADER FILES
*/
#include "osu_coll.h"
#ifdef _ENABLE_OPENACC_
#include <openacc.h>
#endif
#ifdef _ENABLE_CUDA_
#include <cuda.h>
#include <cuda_runtime.h>
#endif
/*
* GLOBAL VARIABLES
*/
#ifdef _ENABLE_CUDA_
static CUcontext cuContext;
#endif
static char const * benchmark_header = NULL;
static char const * benchmark_name = NULL;
static int accel_enabled = 0;
static struct {
char const * message;
char const * optarg;
char opt;
} bad_usage;
static int
set_max_message_size (int value)
{
if (0 > value) {
return -1;
}
options.max_message_size = value;
return 0;
}
static int
set_num_iterations (int value)
{
if (1 > value) {
return -1;
}
options.iterations = value;
options.iterations_large = value;
return 0;
}
static int
set_max_memlimit (int value)
{
options.max_mem_limit = value;
if (value < MAX_MEM_LOWER_LIMIT) {
options.max_mem_limit = MAX_MEM_LOWER_LIMIT;
fprintf(stderr,"Requested memory limit too low, using [%d] instead.",
MAX_MEM_LOWER_LIMIT);
}
return 0;
}
void
set_header (const char * header)
{
benchmark_header = header;
}
void
set_benchmark_name (const char * name)
{
benchmark_name = name;
}
void
enable_accel_support (void)
{
accel_enabled = (CUDA_ENABLED || OPENACC_ENABLED);
}
enum po_ret_type
process_options (int argc, char *argv[])
{
extern char * optarg;
extern int optind, optopt;
char const * optstring = (accel_enabled) ? "+:d:hvfm:i:M:" : "+:hvfm:i:M:";
char c;
/*
* SET DEFAULT OPTIONS
*/
options.accel = none;
options.show_size = 1;
options.show_full = 0;
options.max_message_size = DEFAULT_MAX_MESSAGE_SIZE;
options.max_mem_limit = MAX_MEM_LIMIT;
options.iterations = 1000;
options.iterations_large = 100;
while ((c = getopt(argc, argv, optstring)) != -1) {
bad_usage.opt = c;
bad_usage.optarg = NULL;
bad_usage.message = NULL;
switch (c) {
case 'h':
return po_help_message;
case 'v':
return po_version_message;
case 'm':
if (set_max_message_size(atoi(optarg))) {
bad_usage.message = "Invalid Message Size";
bad_usage.optarg = optarg;
return po_bad_usage;
}
break;
case 'i':
if (set_num_iterations(atoi(optarg))) {
bad_usage.message = "Invalid Number of Iterations";
bad_usage.optarg = optarg;
return po_bad_usage;
}
break;
case 'f':
options.show_full = 1;
break;
case 'M':
/*
* This function does not error but prints a warning message if
* the value is too low.
*/
set_max_memlimit(atoll(optarg));
break;
case 'd':
if (!accel_enabled) {
bad_usage.message = "Benchmark Does Not Support "
"Accelerator Transfers";
bad_usage.optarg = optarg;
return po_bad_usage;
}
else if (0 == strncasecmp(optarg, "cuda", 10)) {
if (CUDA_ENABLED) {
options.accel = cuda;
}
else {
bad_usage.message = "CUDA Support Not Enabled\n"
"Please recompile benchmark with CUDA support";
bad_usage.optarg = optarg;
return po_bad_usage;
}
}
else if (0 == strncasecmp(optarg, "openacc", 10)) {
if (OPENACC_ENABLED) {
options.accel = openacc;
}
else {
bad_usage.message = "OpenACC Support Not Enabled\n"
"Please recompile benchmark with OpenACC support";
bad_usage.optarg = optarg;
return po_bad_usage;
}
}
else {
bad_usage.message = "Invalid Accel Type Specified";
bad_usage.optarg = optarg;
return po_bad_usage;
}
break;
case ':':
bad_usage.message = "Option Missing Required Argument";
bad_usage.opt = optopt;
return po_bad_usage;
default:
bad_usage.message = "Invalid Option";
bad_usage.opt = optopt;
return po_bad_usage;
}
}
return po_okay;
}
void
print_bad_usage_message (int rank)
{
if (rank) return;
if (bad_usage.optarg) {
fprintf(stderr, "%s [-%c %s]\n\n", bad_usage.message, bad_usage.opt,
bad_usage.optarg);
}
else {
fprintf(stderr, "%s [-%c]\n\n", bad_usage.message, bad_usage.opt);
}
print_help_message(rank);
}
void
print_help_message (int rank)
{
if (rank) return;
printf("Usage: %s [options]\n", benchmark_name);
printf("options:\n");
if (accel_enabled) {
printf(" -d TYPE use accelerator device buffers which can be of TYPE `cuda' or\n");
printf(" `openacc' (uses standard host buffers if not specified)\n");
}
if (options.show_size) {
printf(" -m SIZE maximum message size set to SIZE (default 1MB\n");
printf(" -M SIZE per process maximum memory consumption set to SIZE (default\n");
printf(" %dMB)\n", MAX_MEM_LIMIT / (1 << 20));
}
printf(" -i ITER iterations per message size set to ITER (default 1000 for small\n");
printf(" messages, 100 for large messages)\n");
printf(" -f print full format listing (MIN/MAX latency and ITERATIONS\n");
printf(" displayed in addition to AVERAGE latency)\n");
printf(" -h print this help\n");
printf(" -v print version info\n");
printf("\n");
fflush(stdout);
}
void
print_version_message (int rank)
{
if (rank) return;
switch (options.accel) {
case cuda:
printf(benchmark_header, "-CUDA");
break;
case openacc:
printf(benchmark_header, "-OPENACC");
break;
default:
printf(benchmark_header, "");
break;
}
fflush(stdout);
}
void
print_preamble (int rank)
{
if (rank) return;
switch (options.accel) {
case cuda:
printf(benchmark_header, "-CUDA");
break;
case openacc:
printf(benchmark_header, "-OPENACC");
break;
default:
printf(benchmark_header, "");
break;
}
if (options.show_size) {
fprintf(stdout, "%-*s", 10, "# Size");
fprintf(stdout, "%*s", FIELD_WIDTH, "Avg Latency(us)");
}
else {
fprintf(stdout, "# Avg Latency(us)");
}
if (options.show_full) {
fprintf(stdout, "%*s", FIELD_WIDTH, "Min Latency(us)");
fprintf(stdout, "%*s", FIELD_WIDTH, "Max Latency(us)");
fprintf(stdout, "%*s\n", 12, "Iterations");
}
else {
fprintf(stdout, "\n");
}
fflush(stdout);
}
void
print_stats (int rank, int size, double avg_time, double min_time, double
max_time)
{
if (rank) return;
if (options.show_size) {
fprintf(stdout, "%-*d", 10, size);
fprintf(stdout, "%*.*f", FIELD_WIDTH, FLOAT_PRECISION, avg_time);
}
else {
fprintf(stdout, "%*.*f", 17, FLOAT_PRECISION, avg_time);
}
if (options.show_full) {
fprintf(stdout, "%*.*f%*.*f%*lu\n",
FIELD_WIDTH, FLOAT_PRECISION, min_time,
FIELD_WIDTH, FLOAT_PRECISION, max_time,
12, options.iterations);
}
else {
fprintf(stdout, "\n");
}
fflush(stdout);
}
void
set_buffer (void * buffer, enum accel_type type, int data, size_t size)
{
#ifdef _ENABLE_OPENACC_
size_t i;
char * p = (char *)buffer;
#endif
switch (type) {
case none:
memset(buffer, data, size);
break;
case cuda:
#ifdef _ENABLE_CUDA_
cudaMemset(buffer, data, size);
#endif
break;
case openacc:
#ifdef _ENABLE_OPENACC_
#pragma acc parallel loop deviceptr(p)
for(i = 0; i < size; i++) {
p[i] = data;
}
#endif
break;
}
}
int
allocate_buffer (void ** buffer, size_t size, enum accel_type type)
{
size_t alignment = sysconf(_SC_PAGESIZE);
#ifdef _ENABLE_CUDA_
cudaError_t cuerr = cudaSuccess;
#endif
switch (type) {
case none:
return posix_memalign(buffer, alignment, size);
#ifdef _ENABLE_CUDA_
case cuda:
cuerr = cudaMalloc(buffer, size);
if (cudaSuccess != cuerr) {
return 1;
}
else {
return 0;
}
#endif
#ifdef _ENABLE_OPENACC_
case openacc:
*buffer = acc_malloc(size);
if (NULL == *buffer) {
return 1;
}
else {
return 0;
}
#endif
default:
return 1;
}
}
void
free_buffer (void * buffer, enum accel_type type)
{
switch (type) {
case none:
free(buffer);
break;
case cuda:
#ifdef _ENABLE_CUDA_
cudaFree(buffer);
#endif
break;
case openacc:
#ifdef _ENABLE_OPENACC_
acc_free(buffer);
#endif
break;
}
}
int
init_cuda_context (void)
{
#ifdef _ENABLE_CUDA_
CUresult curesult = CUDA_SUCCESS;
CUdevice cuDevice;
int local_rank, dev_count;
int dev_id = 0;
char * str;
if ((str = getenv("LOCAL_RANK")) != NULL) {
cudaGetDeviceCount(&dev_count);
local_rank = atoi(str);
dev_id = local_rank % dev_count;
}
curesult = cuInit(0);
if (curesult != CUDA_SUCCESS) {
return 1;
}
curesult = cuDeviceGet(&cuDevice, dev_id);
if (curesult != CUDA_SUCCESS) {
return 1;
}
curesult = cuCtxCreate(&cuContext, 0, cuDevice);
if (curesult != CUDA_SUCCESS) {
return 1;
}
#endif
return 0;
}
int
destroy_cuda_context (void)
{
#ifdef _ENABLE_CUDA_
CUresult curesult = CUDA_SUCCESS;
curesult = cuCtxDestroy(cuContext);
if (curesult != CUDA_SUCCESS) {
return 1;
}
#endif
return 0;
}
/* vi:set sw=4 sts=4 tw=80: */