-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cu
364 lines (320 loc) · 11.1 KB
/
main.cu
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
#include <cuda.h>
#include <getopt.h>
#include <string>
#include <cstdlib>
#include <cstring>
#include <strings.h>
#include <cstdio>
#include <vector>
#include <exception>
#include <stdexcept>
#include "buffer.h"
#include "bench.h"
#include "timer.h"
#include "stream.h"
#include "device.h"
using namespace std;
static void showUsage(const char* fname)
{
fprintf(stderr,
"Usage: %s [--do=<transfer specs>...] [--streams=<mode>]\n"
"\nDescription\n"
" This program uses multiple CUDA streams in an attempt at optimizing data\n"
" transfers between host and multiple CUDA devices using cudaMemcpyAsync().\n"
"\nProgram options\n"
" --do=<transfer specs> transfer specification\n"
" --streams=<mode> stream modes for transfers\n"
" --list list available CUDA devices and quit\n"
" --help show this help text and quit\n"
"\nStream modes\n"
" per-transfer one stream per transfer [default]\n"
" per-device transfers to the same device share streams\n"
" only-one all transfers share the same single stream\n"
"\nTransfer specification format\n"
" <device>[:<direction>][:<size>][:<memory options>...]\n"
"\nTransfer specification arguments\n"
" <device> CUDA device to use for transfer\n"
" <direction> transfer directions\n"
" <size> transfer size in bytes [default is 32 MiB]\n"
" <memory options> memory allocation options\n"
"\nTransfer directions\n"
" HtoD host to device transfer (RAM to GPU)\n"
" DtoH device to host transfer (GPU to RAM)\n"
" both first HtoD then DtoH [default]\n"
" reverse first DtoH then HtoD\n"
"\nMemory options format\n"
" option1,option2,option3,...\n"
"\nMemory options\n"
" mapped map host memory into CUDA address space\n"
//" portable make host memory available in all contexts\n"
" wc allocate write-combined memory on the host\n"
//" managed allocate managed memory on the device\n"
"\n"
,
fname
);
}
static void listDevices()
{
const int deviceCount = countDevices();
fprintf(stderr, "\n %2s %-15s %-9s %7s %7s %7s %8s %2s\n",
"ID", "Device name", "IO addr", "Compute", "Managed", "Unified", "Mappable", "#");
fprintf(stderr, "-------------------------------------------------------------------------------\n");
for (int i = 0; i < deviceCount; ++i)
{
cudaDeviceProp prop;
loadDeviceProperties(i, prop);
if (isDeviceValid(i))
{
fprintf(stderr, " %2d %-15s %02x:%02x.%-3x %4d.%-2d %7s %7s %8s %2d\n",
i, prop.name, prop.pciBusID, prop.pciDeviceID, prop.pciDomainID,
prop.major, prop.minor,
prop.managedMemory ? "yes" : "no",
prop.unifiedAddressing ? "yes" : "no",
prop.canMapHostMemory ? "yes" : "no",
prop.asyncEngineCount);
}
}
fprintf(stderr, "\n");
}
static void parseDevice(vector<int>& devices, const char* token)
{
if (strcasecmp("all", token) != 0)
{
char* strptr = NULL;
int device = strtol(token, &strptr, 10);
if (strptr == NULL || *strptr != '\0' || !isDeviceValid(device))
{
fprintf(stderr, "Invalid transfer specification: '%s' is not a valid device\n", token);
throw 3;
}
devices.push_back(device);
cudaDeviceProp prop;
loadDeviceProperties(device, prop);
if (prop.major < 3 || (prop.major == 3 && prop.minor < 5))
{
fprintf(stderr, "WARNING: Compute capability of device %d is lower than 3.5\n", device);
}
}
else
{
const int deviceCount = countDevices();
for (int device = 0; device < deviceCount; ++device)
{
if (isDeviceValid(device))
{
devices.push_back(device);
}
cudaDeviceProp prop;
loadDeviceProperties(device, prop);
if (prop.major < 3 || (prop.major == 3 && prop.minor < 5))
{
fprintf(stderr, "WARNING: Compute capability of device %d is lower than 3.5\n", device);
}
}
}
}
static void parseDirection(vector<cudaMemcpyKind>& directions, const char* token)
{
if (strcasecmp("dtoh", token) == 0)
{
directions.push_back(cudaMemcpyDeviceToHost);
}
else if (strcasecmp("htod", token) == 0)
{
directions.push_back(cudaMemcpyHostToDevice);
}
else if (strcasecmp("both", token) == 0)
{
directions.push_back(cudaMemcpyHostToDevice);
directions.push_back(cudaMemcpyDeviceToHost);
}
else if (strcasecmp("reverse", token) == 0)
{
directions.push_back(cudaMemcpyDeviceToHost);
directions.push_back(cudaMemcpyHostToDevice);
}
}
static void parseSize(size_t& size, const char* token)
{
char* strptr = NULL;
size = strtoul(token, &strptr, 0);
if (strptr == NULL || *strptr != '\0')
{
size = 0;
}
}
static void parseTransferSpecification(vector<TransferSpec>& transferSpecs, char* specStr)
{
vector<int> devices;
vector<cudaMemcpyKind> directions;
size_t size = 0;
unsigned int hostAllocFlags = cudaHostAllocDefault;
// unsigned int deviceAllocFlags = 0;
// bool useManagedDeviceMem = false;
// First token must be device
const char* delim = ":,";
char* token = strtok(specStr, delim);
parseDevice(devices, token);
// The remaining of the transfer specification may be in arbitrary order
// because we want to be nice
while ((token = strtok(NULL, delim)) != NULL)
{
if (directions.empty())
{
parseDirection(directions, token);
}
if (strcasecmp("mapped", token) == 0)
{
hostAllocFlags |= cudaHostAllocMapped;
}
else if (strcasecmp("write-combined", token) == 0 || strcasecmp("wc", token) == 0)
{
hostAllocFlags |= cudaHostAllocWriteCombined;
}
// else if (strcasecmp("managed", token) == 0)
// {
// useManagedDeviceMem = true;
// }
if (size == 0)
{
parseSize(size, token);
}
}
// Insert default values if necessary
if (directions.empty())
{
directions.push_back(cudaMemcpyHostToDevice);
directions.push_back(cudaMemcpyDeviceToHost);
}
if (size == 0)
{
size = 32 << 20;
}
// Try to allocate buffers and create transfer specification
try
{
fprintf(stdout, "Allocating buffers...........");
fflush(stdout);
for (cudaMemcpyKind transferMode : directions)
{
for (int device : devices)
{
TransferSpec spec;
spec.device = device;
spec.deviceBuffer = createDeviceBuffer(device, size); // FIXME: Managed memory
spec.hostBuffer = createHostBuffer(size, hostAllocFlags);
spec.length = size;
spec.direction = transferMode;
transferSpecs.push_back(spec);
}
}
fprintf(stdout, "DONE\n");
fflush(stdout);
}
catch (const runtime_error& e)
{
fprintf(stdout, "FAIL\n");
fflush(stdout);
throw e;
}
}
static void parseArguments(int argc, char** argv, StreamSharingMode& streamMode, vector<TransferSpec>& transferSpecs)
{
// Define program arguments
const option opts[] = {
{ .name = "transfer", .has_arg = 1, .flag = NULL, .val = 't' },
{ .name = "run", .has_arg = 1, .flag = NULL, .val = 't' },
{ .name = "do", .has_arg = 1, .flag = NULL, .val = 't' },
{ .name = "streams", .has_arg = 1, .flag = NULL, .val = 's' },
{ .name = "list", .has_arg = 0, .flag = NULL, .val = 'l' },
{ .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
{ .name = NULL, .has_arg = 0, .flag = NULL, .val = 0 }
};
// Parse arguments
int opt, idx;
while ((opt = getopt_long(argc, argv, "-:t:s:lh", opts, &idx)) != -1)
{
switch (opt)
{
case ':': // missing value
fprintf(stderr, "Option %s requires a value\n", argv[optind-1]);
throw 1;
case '?': // unknown option
fprintf(stderr, "Unknown option: %s\n", argv[optind-1]);
throw 1;
case 't': // transfer specification
parseTransferSpecification(transferSpecs, optarg);
break;
case 's': // stream sharing mode
if (strcasecmp("per-transfer", optarg) == 0)
{
streamMode = perTransfer;
}
else if (strcasecmp("per-device", optarg) == 0 || strcasecmp("per-gpu", optarg) == 0)
{
streamMode = perDevice;
}
else if (strcasecmp("only-one", optarg) == 0 || strcasecmp("single", optarg) == 0)
{
streamMode = singleStream;
}
else
{
fprintf(stderr, "Unknown stream mode: %s\n", optarg);
throw 2;
}
break;
case 'l': // list devices
listDevices();
throw 0;
case 'h': // show help
showUsage(argv[0]);
throw 0;
}
}
}
int main(int argc, char** argv)
{
StreamSharingMode streamMode = perTransfer;
vector<TransferSpec> transferSpecs;
// Parse program arguments
try
{
parseArguments(argc, argv, streamMode, transferSpecs);
}
catch (const runtime_error& e)
{
fprintf(stderr, "Unexpected error: %s\n", e.what());
return 1;
}
catch (const int e)
{
return e;
}
StreamManager streamManager(streamMode);
try
{
// No transfer specifications?
if (transferSpecs.empty())
{
char buffer[64];
snprintf(buffer, sizeof(buffer), "all");
parseTransferSpecification(transferSpecs, buffer);
}
// Create streams and timing events
for (TransferSpec& spec : transferSpecs)
{
spec.stream = streamManager.retrieveStream(spec.device);
spec.timer = createTimer();
}
// Run bandwidth test
runBandwidthTest(transferSpecs);
}
catch (const runtime_error& e)
{
fprintf(stderr, "Unexpected error: %s\n", e.what());
return 1;
}
return 0;
}