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
478
479
480
481
482
483
484
485
486
487
488
489
|
/*
* Created on Tuesday, September 14 2010 17:19
*/
package com.jogamp.opencl.demos.bandwidth;
import com.jogamp.common.nio.Buffers;
import com.jogamp.opencl.CLBuffer;
import com.jogamp.opencl.CLCommandQueue;
import com.jogamp.opencl.CLContext;
import com.jogamp.opencl.CLDevice;
import com.jogamp.opencl.CLPlatform;
import static com.jogamp.opencl.CLMemory.Map.*;
import com.jogamp.opencl.CLMemory.Mem;
import static com.jogamp.opencl.CLMemory.Mem.*;
import java.nio.ByteBuffer;
/**
* Port of Nvidia's BandwidthTest to JOCL HLB.
* @author Michael Bien
*/
public class BandwidthBenchmark {
// defines, project
private static int MEMCOPY_ITERATIONS = 100;
private static int DEFAULT_SIZE = (32 * (1 << 20)); //32 M
private static int DEFAULT_INCREMENT = (1 << 22); //4 M
private static int CACHE_CLEAR_SIZE = (1 << 24); //16 M
//shmoo mode defines
private static int SHMOO_MEMSIZE_MAX = (1 << 26); //64 M
private static int SHMOO_MEMSIZE_START = (1 << 10); //1 KB
private static int SHMOO_INCREMENT_1KB = (1 << 10); //1 KB
private static int SHMOO_INCREMENT_2KB = (1 << 11); //2 KB
private static int SHMOO_INCREMENT_10KB = (10 * (1 << 10)); //10KB
private static int SHMOO_INCREMENT_100KB = (100 * (1 << 10)); //100 KB
private static int SHMOO_INCREMENT_1MB = (1 << 20); //1 MB
private static int SHMOO_INCREMENT_2MB = (1 << 21); //2 MB
private static int SHMOO_INCREMENT_4MB = (1 << 22); //4 MB
private static int SHMOO_LIMIT_20KB = (20 * (1 << 10)); //20 KB
private static int SHMOO_LIMIT_50KB = (50 * (1 << 10)); //50 KB
private static int SHMOO_LIMIT_100KB = (100 * (1 << 10)); //100 KB
private static int SHMOO_LIMIT_1MB = (1 << 20); //1 MB
private static int SHMOO_LIMIT_16MB = (1 << 24); //16 MB
private static int SHMOO_LIMIT_32MB = (1 << 25); //32 MB
private enum TEST_MODE { QUICK, RANGE, SHMOO };
private enum COPY { DEVICE_TO_HOST, HOST_TO_DEVICE, DEVICE_TO_DEVICE };
private enum MEMORY { PAGEABLE, PINNED };
private enum ACCESS { MAPPED, DIRECT };
public static void main(String[] args) {
int start = DEFAULT_SIZE;
int end = DEFAULT_SIZE;
int increment = DEFAULT_INCREMENT;
TEST_MODE mode = TEST_MODE.QUICK;
MEMORY memMode = MEMORY.PAGEABLE;
ACCESS accMode = ACCESS.DIRECT;
CLPlatform[] platforms = CLPlatform.listCLPlatforms();
CLPlatform platform = platforms[0];
// prefere NV
for (CLPlatform p : platforms) {
if(p.getICDSuffix().equals("NV")) {
platform = p;
break;
}
}
CLDevice device = platform.getMaxFlopsDevice();
int deviceIndex = -1;
for (String arg : args) {
if(arg.startsWith("--access=")) {
accMode = ACCESS.valueOf(arg.substring(9).toUpperCase());
}else if(arg.startsWith("--memory=")) {
memMode = MEMORY.valueOf(arg.substring(9).toUpperCase());
}else if(arg.startsWith("--device=")) {
deviceIndex = Integer.parseInt(arg.substring(9).toUpperCase());
}else if(arg.startsWith("--mode=")) {
mode = TEST_MODE.valueOf(arg.substring(7).toUpperCase());
}else if(arg.startsWith("--platform=")) {
platform = platforms[Integer.parseInt(arg.substring(11))];
}else{
System.out.println("unknown arg: "+arg);
System.exit(1);
}
}
if(deviceIndex != -1) {
device = platform.listCLDevices()[deviceIndex];
}
CLContext context = CLContext.create(device);
System.out.println();
System.out.println(platform);
System.out.println(context);
System.out.println();
// Run tests
testBandwidth(context, start, end, increment, mode, COPY.HOST_TO_DEVICE, accMode, memMode);
testBandwidth(context, start, end, increment, mode, COPY.DEVICE_TO_HOST, accMode, memMode);
testBandwidth(context, start, end, increment, mode, COPY.DEVICE_TO_DEVICE, accMode, memMode);
context.release();
}
private static void testBandwidth(CLContext context, int start, int end, int increment, TEST_MODE mode, COPY kind, ACCESS accMode, MEMORY memMode) {
switch (mode) {
case QUICK:
testBandwidthQuick(context, DEFAULT_SIZE, kind, accMode, memMode);
break;
case RANGE:
testBandwidthRange(context, start, end, increment, kind, accMode, memMode);
break;
case SHMOO:
testBandwidthShmoo(context, kind, accMode, memMode);
break;
default:
break;
}
}
/**
* Run a quick mode bandwidth test
*/
private static void testBandwidthQuick(CLContext context, int size, COPY kind, ACCESS accMode, MEMORY memMode) {
testBandwidthRange(context, size, size, DEFAULT_INCREMENT, kind, accMode, memMode);
}
/**
* Run a range mode bandwidth test
*/
private static void testBandwidthRange(CLContext context, int start, int end, int increment, COPY kind, ACCESS accMode, MEMORY memMode) {
//count the number of copies we're going to run
int count = 1 + ((end - start) / increment);
int[] memSizes = new int[count];
double[] bandwidths = new double[count];
// Use the device asked by the user
CLDevice[] devices = context.getDevices();
for (CLDevice device : devices) {
CLCommandQueue queue = device.createCommandQueue();
//run each of the copies
for (int i = 0; i < count; i++) {
memSizes[i] = start + i * increment;
switch (kind) {
case DEVICE_TO_HOST:
bandwidths[i] += testDeviceToHostTransfer(queue, memSizes[i], accMode, memMode);
break;
case HOST_TO_DEVICE:
bandwidths[i] += testHostToDeviceTransfer(queue, memSizes[i], accMode, memMode);
break;
case DEVICE_TO_DEVICE:
bandwidths[i] += testDeviceToDeviceTransfer(queue, memSizes[i]);
break;
}
}
queue.release();
}
//print results
printResultsReadable(memSizes, bandwidths, count, kind, accMode, memMode, count);
}
/**
* Intense shmoo mode - covers a large range of values with varying increments
*/
private static void testBandwidthShmoo(CLContext context, COPY kind, ACCESS accMode, MEMORY memMode) {
//count the number of copies to make
int count = 1 + (SHMOO_LIMIT_20KB / SHMOO_INCREMENT_1KB)
+ ((SHMOO_LIMIT_50KB - SHMOO_LIMIT_20KB) / SHMOO_INCREMENT_2KB)
+ ((SHMOO_LIMIT_100KB - SHMOO_LIMIT_50KB) / SHMOO_INCREMENT_10KB)
+ ((SHMOO_LIMIT_1MB - SHMOO_LIMIT_100KB) / SHMOO_INCREMENT_100KB)
+ ((SHMOO_LIMIT_16MB - SHMOO_LIMIT_1MB) / SHMOO_INCREMENT_1MB)
+ ((SHMOO_LIMIT_32MB - SHMOO_LIMIT_16MB) / SHMOO_INCREMENT_2MB)
+ ((SHMOO_MEMSIZE_MAX - SHMOO_LIMIT_32MB) / SHMOO_INCREMENT_4MB);
int[] memSizes = new int[count];
double[] bandwidths = new double[count];
// Use the device asked by the user
CLDevice[] devices = context.getDevices();
for (CLDevice device : devices) {
// Allocate command queue for the device
CLCommandQueue queue = device.createCommandQueue();
//Run the shmoo
int iteration = 0;
int memSize = 0;
while (memSize <= SHMOO_MEMSIZE_MAX) {
if (memSize < SHMOO_LIMIT_20KB) {
memSize += SHMOO_INCREMENT_1KB;
} else if (memSize < SHMOO_LIMIT_50KB) {
memSize += SHMOO_INCREMENT_2KB;
} else if (memSize < SHMOO_LIMIT_100KB) {
memSize += SHMOO_INCREMENT_10KB;
} else if (memSize < SHMOO_LIMIT_1MB) {
memSize += SHMOO_INCREMENT_100KB;
} else if (memSize < SHMOO_LIMIT_16MB) {
memSize += SHMOO_INCREMENT_1MB;
} else if (memSize < SHMOO_LIMIT_32MB) {
memSize += SHMOO_INCREMENT_2MB;
} else {
memSize += SHMOO_INCREMENT_4MB;
}
memSizes[iteration] = memSize;
switch (kind) {
case DEVICE_TO_HOST:
bandwidths[iteration] += testDeviceToHostTransfer(queue, memSizes[iteration], accMode, memMode);
break;
case HOST_TO_DEVICE:
bandwidths[iteration] += testHostToDeviceTransfer(queue, memSizes[iteration], accMode, memMode);
break;
case DEVICE_TO_DEVICE:
bandwidths[iteration] += testDeviceToDeviceTransfer(queue, memSizes[iteration]);
break;
}
iteration++;
}
queue.release();
}
//print results
printResultsReadable(memSizes, bandwidths, count, kind, accMode, memMode, count);
}
/**
* test the bandwidth of a device to host memcopy of a specific size
*/
private static double testDeviceToHostTransfer(CLCommandQueue queue, int memSize, ACCESS accMode, MEMORY memMode) {
ByteBuffer h_data = null;
CLBuffer<?> cmPinnedData = null;
CLBuffer<?> cmDevData;
CLContext context = queue.getContext();
//allocate and init host memory, pinned or conventional
if (memMode == memMode.PINNED) {
// Create a host buffer
cmPinnedData = context.createBuffer(memSize, Mem.READ_WRITE, Mem.ALLOCATE_BUFFER);
// Get a mapped pointer
h_data = queue.putMapBuffer(cmPinnedData, WRITE, true);
fill(h_data);
// unmap and make data in the host buffer valid
cmPinnedData = cmPinnedData.cloneWith(h_data);
queue.putUnmapMemory(cmPinnedData);
} else { // PAGED
// standard host alloc
h_data = Buffers.newDirectByteBuffer(memSize);
fill(h_data);
}
// allocate device memory
cmDevData = context.createBuffer(memSize, Mem.READ_WRITE);
// initialize device memory
if (memMode == memMode.PINNED) {
// Get a mapped pointer
h_data = queue.putMapBuffer(cmPinnedData, WRITE, true);
cmDevData = cmDevData.cloneWith(h_data);
queue.putWriteBuffer(cmDevData, false);
} else { // PAGED
cmDevData = cmDevData.cloneWith(h_data);
queue.putWriteBuffer(cmDevData, false);
}
// Sync queue to host, start timer 0, and copy data from GPU to Host
queue.finish();
long delta = System.nanoTime();
if (accMode == accMode.DIRECT) {
// DIRECT: API access to device buffer
cmDevData = cmDevData.cloneWith(h_data);
for (int i = 0; i < MEMCOPY_ITERATIONS; i++) {
queue.putReadBuffer(cmDevData, false);
}
queue.finish();
} else {
// MAPPED: mapped pointers to device buffer for conventional pointer access
ByteBuffer dm_idata = queue.putMapBuffer(cmDevData, WRITE, true);
for (int i = 0; i < MEMCOPY_ITERATIONS; i++) {
h_data.put(dm_idata).rewind();
dm_idata.rewind();
}
cmDevData = cmDevData.cloneWith(dm_idata);
queue.putUnmapMemory(cmDevData);
}
//get the the elapsed time in seconds
delta = System.nanoTime() - delta;
//clean up memory
cmDevData.release();
if (cmPinnedData != null) {
cmPinnedData = cmPinnedData.cloneWith(h_data);
queue.putUnmapMemory(cmPinnedData);
cmPinnedData.release();
}
//calculate bandwidth in MB/s
double elapsedTime = delta/1000000000.0;
return ((double) memSize * (double) MEMCOPY_ITERATIONS) / (elapsedTime*(double)(1 << 20));
}
/**
* test the bandwidth of a device to host memcopy of a specific size
*/
private static double testHostToDeviceTransfer(CLCommandQueue queue, int memSize, ACCESS accMode, MEMORY memMode) {
ByteBuffer h_data;
CLBuffer<?> cmPinnedData = null;
CLBuffer<?> cmDevData;
CLContext context = queue.getContext();
// Allocate and init host memory, pinned or conventional
if (memMode == memMode.PINNED) {
// Create a host buffer
cmPinnedData = context.createBuffer(memSize, Mem.READ_WRITE, Mem.ALLOCATE_BUFFER);
// Get a mapped pointer
h_data = queue.putMapBuffer(cmPinnedData, WRITE, true);
//initialize
fill(h_data);
// unmap and make data in the host buffer valid
cmPinnedData = cmPinnedData.cloneWith(h_data);
queue.putUnmapMemory(cmPinnedData);
} else { // PAGED
// standard host alloc
h_data = Buffers.newDirectByteBuffer(memSize);
fill(h_data);
}
// allocate device memory
cmDevData = context.createBuffer(memSize, Mem.READ_WRITE);
// Sync queue to host, start timer 0, and copy data from Host to GPU
queue.finish();
long delta = System.nanoTime();
if (accMode == accMode.DIRECT) {
if (memMode == memMode.PINNED) {
// Get a mapped pointer
h_data = queue.putMapBuffer(cmPinnedData, WRITE, true);
}
// DIRECT: API access to device buffer
cmDevData = cmDevData.cloneWith(h_data);
for (int i = 0; i < MEMCOPY_ITERATIONS; i++) {
queue.putWriteBuffer(cmDevData, false);
}
queue.finish();
} else {
// MAPPED: mapped pointers to device buffer and conventional pointer access
ByteBuffer dm_idata = queue.putMapBuffer(cmDevData, READ, true);
for (int i = 0; i < MEMCOPY_ITERATIONS; i++) {
dm_idata.put(h_data).rewind();
h_data.rewind();
}
cmDevData = cmDevData.cloneWith(dm_idata);
queue.putUnmapMemory(cmDevData);
}
//get the the elapsed time in ms
delta = System.nanoTime() - delta;
//clean up memory
cmDevData.release();
if (cmPinnedData != null) {
// cmPinnedData = cmPinnedData.cloneWith(h_data);
// queue.putUnmapMemory(cmPinnedData);
cmPinnedData.release();
}
//calculate bandwidth in MB/s
double elapsedTime = delta/1000000000.0;
return ((double) memSize * (double) MEMCOPY_ITERATIONS) / (elapsedTime*(double)(1 << 20));
}
/**
* test the bandwidth of a device to host memcopy of a specific size
*/
private static double testDeviceToDeviceTransfer(CLCommandQueue queue, int memSize) {
CLContext context = queue.getContext();
//allocate host memory
ByteBuffer h_idata = Buffers.newDirectByteBuffer(memSize);
fill(h_idata);
// allocate device input and output memory and initialize the device input memory
CLBuffer<?> d_idata = context.createBuffer(memSize, READ_ONLY);
CLBuffer<?> d_odata = context.createBuffer(memSize, WRITE_ONLY);
d_idata = d_idata.cloneWith(h_idata);
queue.putWriteBuffer(d_idata, true);
// Sync queue to host, start timer 0, and copy data from one GPU buffer to another GPU bufffer
queue.finish();
long delta = System.nanoTime();
for (int i = 0; i < MEMCOPY_ITERATIONS; i++) {
queue.putCopyBuffer(d_idata, d_odata);
}
// Sync with GPU
queue.finish();
//get the the elapsed time in ms
delta = System.nanoTime() - delta;
//clean up memory on host and device
d_idata.release();
d_odata.release();
// Calculate bandwidth in MB/s
// This is for kernels that read and write GMEM simultaneously
// Obtained Throughput for unidirectional block copies will be 1/2 of this #
double elapsedTime = delta/1000000000.0;
return 2.0 * ((double) memSize * (double) MEMCOPY_ITERATIONS) / (elapsedTime*(double)(1 << 20));
}
private static void fill(ByteBuffer buffer) {
int i = 0;
while(buffer.remaining() > 0) {
buffer.putChar((char) (i++ & 0xff));
}
buffer.rewind();
}
/**
* print results in an easily read format
*/
private static void printResultsReadable(int[] memSizes, double[] bandwidths, int count, COPY kind, ACCESS accMode, MEMORY memMode, int iNumDevs) {
// log config information
if (kind == COPY.DEVICE_TO_DEVICE) {
System.out.print("Device to Device Bandwidth, "+iNumDevs+" Device(s), ");
} else {
if (kind == COPY.DEVICE_TO_HOST) {
System.out.print("Device to Host Bandwidth, "+iNumDevs+" Device(s), ");
} else if (kind == COPY.HOST_TO_DEVICE) {
System.out.print("Host to Device Bandwidth, "+iNumDevs+" Device(s), ");
}
if (memMode == memMode.PAGEABLE) {
System.out.print("Paged memory");
} else if (memMode == memMode.PINNED) {
System.out.print("Pinned memory");
}
if (accMode == accMode.DIRECT) {
System.out.println(", direct access");
} else if (accMode == accMode.MAPPED) {
System.out.println(", mapped access");
}
}
System.out.println();
System.out.println(" Transfer Size (Bytes)\tBandwidth(MB/s)\n");
int i;
for (i = 0; i < (count - 1); i++) {
System.out.printf(" %s\t\t\t%s%.1f\n", memSizes[i], (memSizes[i] < 10000) ? "\t" : "", bandwidths[i]);
}
System.out.printf(" %s\t\t\t%s%.1f\n\n", memSizes[i], (memSizes[i] < 10000) ? "\t" : "", bandwidths[i]);
}
}
|