-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathgpu_stream.cu
More file actions
702 lines (618 loc) · 26 KB
/
gpu_stream.cu
File metadata and controls
702 lines (618 loc) · 26 KB
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// GPU stream benchmark
// This benchmark is based on the STREAM benchmark, which is a simple benchmark program that measures
// sustainable memory bandwidth (in MB/s) and the corresponding computation rate for simple (COPY, SCALE, ADD, TRIAD)
// kernels.
#include "gpu_stream.hpp"
#include <cassert>
#include <iostream>
#include <nvml.h>
/**
* @brief Destroys the CUDA events used for benchmarking.
*
* @details This function cleans up and releases the resources associated with the CUDA events
* used for benchmarking based on the provided arguments. It ensures that all allocated resources
* are properly freed.
*
* @param[in,out] args A unique pointer to a BenchArgs structure
*
* @return int The status code indicating success or failure of the destruction process.
*/
template <typename T> int GpuStream::DestroyEvent(std::unique_ptr<BenchArgs<T>> &args) {
cudaError_t cuda_err = cudaSuccess;
if (SetGpu(args->gpu_id)) {
return -1;
}
cuda_err = cudaEventDestroy(args->sub.start_event);
if (cuda_err != cudaSuccess) {
std::cerr << "DestroyEvent::cudaEventDestroy error: " << cuda_err << std::endl;
return -1;
}
cuda_err = cudaEventDestroy(args->sub.end_event);
if (cuda_err != cudaSuccess) {
std::cerr << "DestroyEvent::cudaEventDestroy error: " << cuda_err << std::endl;
return -1;
}
return 0;
}
/**
* @brief Constructor for the GpuStream class.
*
* This constructor initializes the GpuStream opts with the given parameters.
*
* @param opts parsed command line options..
*/
GpuStream::GpuStream(Opts &opts) noexcept : opts_(opts) { PrintInputInfo(opts_); }
/**
* @brief Sets the active GPU.
*
* @details This function sets the active GPU to the specified GPU ID.
*
* @param[in] gpu_id The ID of the GPU to set as active.
*
* @return int The status code indicating success or failure.
*/
int GpuStream::SetGpu(int gpu_id) {
cudaError_t cuda_err = cudaSetDevice(gpu_id);
if (cuda_err != cudaSuccess) {
std::cerr << "SetGpu::cudaSetDevice " << gpu_id << "error: " << cuda_err << std::endl;
return -1;
}
return 0;
}
/**
* @brief Retrieves the number of GPUs available.
*
* @details This function retrieves the number of GPUs available on the system and stores the count
* in the provided pointer.
*
* @param[out] gpu_count Pointer to an integer where the GPU count will be stored.
*
* @return int The status code indicating success or failure.
*/
int GpuStream::GetGpuCount(int *gpu_count) {
cudaError_t cuda_err = cudaGetDeviceCount(gpu_count);
if (cuda_err != cudaSuccess) {
std::cerr << "GetGpuCount::cudaGetDeviceCount error: " << cuda_err << std::endl;
return -1;
}
return 0;
}
/**
* @brief destroys buff and stream resources.
*
* @details This method cleans up and releases the resources associated with the buffer and stream
*
* @param[in,out] args A unique pointer to a BenchArgs structure containing the necessary arguments.
*
* @return int The status code indicating success or failure.
*/
template <typename T> int GpuStream::Destroy(std::unique_ptr<BenchArgs<T>> &args) {
int ret = DestroyBufAndStream(args);
if (ret == 0) {
ret = DestroyEvent(args);
}
return ret;
}
/**
* @brief Gets the memory clock rate for a CUDA device.
*
* @details This function gets the memory clock rate using the appropriate method
* based on CUDA version: CUDA 12.0+ uses NVML and cudaDeviceGetAttribute as a fallback;
* older CUDA versions use cudaDeviceProp.
*
* @param[in] device_id The ID of the CUDA device.
* @param[in] prop The properties of the CUDA device.
* @return float The memory clock rate in MHz, or -1.0f if retrieval fails.
*/
float GpuStream::GetMemoryClockRate(int device_id, const cudaDeviceProp &prop) {
float memory_clock_mhz = 0.0f;
// Set the device before getting attributes
if (SetGpu(device_id)) {
return -1.0f;
}
#if CUDA_VERSION >= 12000
// For CUDA 12.0+, first try NVML for actual clock rate
memory_clock_mhz = GetActualMemoryClockRate(device_id);
// If NVML fails, fall back to cudaDeviceGetAttribute
if (memory_clock_mhz < 0.0f) {
int memory_clock_khz = 0;
cudaError_t cuda_err = cudaDeviceGetAttribute(&memory_clock_khz, cudaDevAttrMemoryClockRate, device_id);
if (cuda_err != cudaSuccess) {
std::cerr << "GetMemoryClockRate::cudaDeviceGetAttribute error: " << cuda_err << std::endl;
return -1.0f;
}
// Convert kHz to MHz
memory_clock_mhz = memory_clock_khz / 1000.0f;
}
#else
// For CUDA < 12.0, use memoryClockRate from cudaDeviceProp
// Note: memoryClockRate is in kHz, need to convert to MHz first
memory_clock_mhz = prop.memoryClockRate / 1000.0f; // Convert kHz to MHz
#endif
return memory_clock_mhz;
}
/**
* @brief Prints CUDA device information.
*
* @details This function prints the properties of a CUDA device specified by the device ID.
*
* @param[in] device_id The ID of the CUDA device.
* @param[in] prop The properties of the CUDA device.
* @param[in] memory_clock_mhz The memory clock rate in MHz (or -1.0f if unavailable).
* @param[in] peak_bw The theoretical peak bandwidth in GB/s (or -1.0f if unavailable).
*/
void GpuStream::PrintCudaDeviceInfo(int device_id, const cudaDeviceProp &prop, float memory_clock_mhz, float peak_bw) {
std::cout << "\nDevice " << device_id << ": \"" << prop.name << "\"";
std::cout << " " << prop.multiProcessorCount << " SMs(" << prop.major << "." << prop.minor << ")";
if (peak_bw < 0.0f) {
// Bandwidth computation failed (memory_clock_mhz will also be < 0)
std::cout << " Memory: " << prop.memoryBusWidth << "-bit bus, " << prop.totalGlobalMem / (1024 * 1024 * 1024)
<< " GB total (peak BW unavailable)";
} else {
// Both memory_clock_mhz and peak_bw are valid
std::cout << " Memory: " << memory_clock_mhz << "MHz x " << prop.memoryBusWidth << "-bit = " << peak_bw
<< " GB/s PEAK ";
}
std::cout << " ECC is " << (prop.ECCEnabled ? "ON" : "OFF") << std::endl;
}
/**
* @brief Allocates a buffer on the GPU.
*
* @details This function allocates a buffer of the specified size on the GPU and returns a pointer
* to the allocated memory.
*
* @param[out] ptr Pointer to a pointer where the address of the allocated buffer will be stored.
* @param[in] size The size of the buffer to allocate in bytes.
*
* @return cudaError_t The status code indicating success or failure of the memory allocation.
*/
template <typename T> cudaError_t GpuStream::GpuMallocDataBuf(T **ptr, uint64_t size) { return cudaMalloc(ptr, size); }
/**
* @brief Prepares validation buffers for GPU stream benchmark.
*
* @details This function allocates and initializes validation buffers for different
* kernels (copy, scale, add, and triad) used in the GPU stream benchmark. The buffer order
* matches the kernel order in the Kernel enum.
*
* @param args A unique pointer to a BenchArgs structure containing the necessary arguments
* for preparing the buffer and stream.
*
* @return int The status code indicating success or failure of the preparation.
*/
template <typename T> int GpuStream::PrepareValidationBuf(std::unique_ptr<BenchArgs<T>> &args) {
args->sub.validation_buf_ptrs.resize(kNumValidationBuffers);
// Compute and allocate validation buffers for add, scale and triad
uint64_t size = args->size / sizeof(T);
for (auto &buf_ptr : args->sub.validation_buf_ptrs) {
buf_ptr.resize(size);
}
// Initialize validation buffer
T s = static_cast<T>(scalar);
for (size_t j = 0; j < size; j++) {
args->sub.validation_buf_ptrs[0][j] = static_cast<T>(j % kUInt8Mod);
args->sub.validation_buf_ptrs[1][j] = static_cast<T>(j % kUInt8Mod) * s;
args->sub.validation_buf_ptrs[2][j] = static_cast<T>(j % kUInt8Mod) + static_cast<T>(j % kUInt8Mod);
args->sub.validation_buf_ptrs[3][j] = static_cast<T>(j % kUInt8Mod) + static_cast<T>(j % kUInt8Mod) * s;
}
return 0;
}
/**
* @brief Prepares the buffer and stream for benchmarking.
*
* @details This function prepares the necessary buffer and stream for benchmarking based on the
* provided arguments. It initializes and configures the buffer and stream as required.
*
* @param[in,out] args A unique pointer to a BenchArgs structure containing the necessary arguments
* for preparing the buffer and stream.
*
* @return int The status code indicating success or failure of the preparation.
*/
template <typename T> int GpuStream::PrepareBufAndStream(std::unique_ptr<BenchArgs<T>> &args) {
cudaError_t cuda_err = cudaSuccess;
if (args->check_data) {
// Generate data to copy - use local NUMA node for best CPU access
args->sub.data_buf = static_cast<T *>(numa_alloc_local(args->size));
for (int j = 0; j < args->size / sizeof(T); j++) {
args->sub.data_buf[j] = static_cast<T>(j % kUInt8Mod);
}
// Allocate check buffer on local NUMA node
args->sub.check_buf = static_cast<T *>(numa_alloc_local(args->size));
}
// Allocate buffers
args->sub.gpu_buf_ptrs.resize(kNumBuffers);
// Set to buffer device for GPU buffer
if (SetGpu(args->gpu_id)) {
return -1;
}
// Allocate buffers
for (auto &buf_ptr : args->sub.gpu_buf_ptrs) {
T *raw_ptr = nullptr;
cuda_err = GpuMallocDataBuf(&raw_ptr, args->size);
if (cuda_err != cudaSuccess) {
std::cerr << "PrepareBufAndStream::cudaMalloc error: " << cuda_err << std::endl;
return -1;
}
buf_ptr.reset(raw_ptr); // Transfer ownership to the smart pointer
}
// Initialize source buffer
if (args->check_data) {
cuda_err = cudaMemcpy(args->sub.gpu_buf_ptrs[0].get(), args->sub.data_buf, args->size, cudaMemcpyDefault);
if (cuda_err != cudaSuccess) {
std::cerr << "PrepareBufAndStream::cudaMemcpy error: " << cuda_err << std::endl;
return -1;
}
cuda_err = cudaMemcpy(args->sub.gpu_buf_ptrs[1].get(), args->sub.data_buf, args->size, cudaMemcpyDefault);
if (cuda_err != cudaSuccess) {
std::cerr << "PrepareBufAndStream::cudaMemcpy error: " << cuda_err << std::endl;
return -1;
}
PrepareValidationBuf<T>(args);
}
cuda_err = cudaStreamCreateWithFlags(&(args->sub.stream), cudaStreamNonBlocking);
if (cuda_err != cudaSuccess) {
std::cerr << "PrepareBufAndStream::cudaStreamCreate error: " << cuda_err << std::endl;
return -1;
}
return 0;
}
/**
* @brief Prepares CUDA events for benchmarking.
*
* @details This function creates the necessary CUDA events for benchmarking based on the
* provided arguments. It initializes and configures the events as required.
*
* @param[in,out] args A unique pointer to a BenchArgs structure containing the necessary arguments
* for preparing the CUDA events.
*
* @return int The status code indicating success or failure of the preparation.
*/
template <typename T> int GpuStream::PrepareEvent(std::unique_ptr<BenchArgs<T>> &args) {
cudaError_t cuda_err = cudaSuccess;
if (SetGpu(args->gpu_id)) {
return -1;
}
cuda_err = cudaEventCreate(&(args->sub.start_event));
if (cuda_err != cudaSuccess) {
std::cerr << "PrepareEvent::cudaEventCreate error: " << cuda_err << std::endl;
return -1;
}
cuda_err = cudaEventCreate(&(args->sub.end_event));
if (cuda_err != cudaSuccess) {
std::cerr << "PrepareEvent::cudaEventCreate error: " << cuda_err << std::endl;
return -1;
}
return 0;
}
/**
* @brief Validates the result of data transfer.
*
* @details This function checks the buffer to validate the result of a data transfer operation
* based on the provided arguments. It ensures that the data transfer was successful and that
* the buffer contains the expected data.
*
* @param[in,out] args A unique pointer to a BenchArgs structure containing the necessary arguments
* for validating the buffer.
*
* @return int The status code indicating success or failure of the validation.
*/
template <typename T> int GpuStream::CheckBuf(std::unique_ptr<BenchArgs<T>> &args, int kernel_idx) {
cudaError_t cuda_err = cudaSuccess;
int memcmp_result = 0;
if (SetGpu(args->gpu_id)) {
return -1;
}
// Copy buffer output from stream kernel to local buffer
cuda_err = cudaMemcpy(args->sub.check_buf, args->sub.gpu_buf_ptrs[2].get(), args->size, cudaMemcpyDefault);
if (cuda_err != cudaSuccess) {
std::cerr << "CheckBuf::cudaMemcpy error: " << cuda_err << std::endl;
return -1;
}
// Validate result by comparing the data buffer and check buffer
memcmp_result = memcmp(args->sub.validation_buf_ptrs[kernel_idx].data(), args->sub.check_buf, args->size);
if (memcmp_result) {
std::cerr << "CheckBuf::Memory check failed for kernel index " << kernel_idx << std::endl;
return -1;
}
return 0;
}
/**
* @brief Destroys the buffer and stream used for benchmarking.
*
* @details This function cleans up and releases the resources associated with the buffer and stream
* used for benchmarking based on the provided arguments. It ensures that all allocated buffers and streams
* resources are properly freed.
*
* @param[in,out] args A unique pointer to a BenchArgs structure containing the necessary arguments
* for destroying the buffer and stream.
*
* @return int The status code indicating success or failure of the destruction process.
*/
template <typename T> int GpuStream::DestroyBufAndStream(std::unique_ptr<BenchArgs<T>> &args) {
int ret = 0;
cudaError_t cuda_err = cudaSuccess;
// Destroy original data buffer and check buffer
if (args->sub.data_buf != nullptr) {
numa_free(args->sub.data_buf, args->size);
}
if (args->sub.check_buf != nullptr) {
numa_free(args->sub.check_buf, args->size);
}
// Set to buffer device for GPU buffer
if (SetGpu(args->gpu_id)) {
return -1;
}
cuda_err = cudaStreamDestroy(args->sub.stream);
if (cuda_err != cudaSuccess) {
std::cerr << "DestroyBufAndStream::cudaStreamDestroy error: " << cuda_err << std::endl;
return -1;
}
return ret;
}
/**
* @brief Runs the STREAM benchmark.
*
* @details This function runs the STREAM benchmark using the specified kernel and number of threads per block.
* It prepares the necessary arguments and configurations for the benchmark execution.
*
* @param[in,out] args A unique pointer to a BenchArgs structure containing the necessary arguments for the
benchmark.
* @param[in] kernel The kernel function to be used for the benchmark.
* @param[in] num_threads_per_block The number of threads per block to be used in the kernel execution.
*
* @return int The status code indicating success or failure of the benchmark execution.
*/
template <typename T>
int GpuStream::RunStreamKernel(std::unique_ptr<BenchArgs<T>> &args, Kernel kernel, int num_threads_per_block) {
cudaError_t cuda_err = cudaSuccess;
uint64_t num_thread_blocks;
int size_factor = 2;
// Validate data size
// Each thread processes 128 bits (16 bytes) for optimal memory bandwidth.
// For double: uses double2 (16 bytes). For float: would use float4 (16 bytes).
constexpr uint64_t kBytesPerThread = 16; // 128-bit aligned access
uint64_t num_bytes_in_thread_block = num_threads_per_block * kBytesPerThread;
if (args->size % num_bytes_in_thread_block) {
std::cerr << "RunStreamKernel: Data size should be multiple of " << num_bytes_in_thread_block << std::endl;
return -1;
}
num_thread_blocks = args->size / num_bytes_in_thread_block;
args->sub.times_in_ms.resize(static_cast<int>(Kernel::kCount));
if (SetGpu(args->gpu_id)) {
return -1;
}
// Launch jobs and collect running time
for (int i = 0; i < args->num_loops + args->num_warm_up; i++) {
// Record start event once warm up iterations are done
if (i == args->num_warm_up) {
cuda_err = cudaEventRecord(args->sub.start_event, args->sub.stream);
if (cuda_err != cudaSuccess) {
std::cerr << "RunStreamKernel::cudaEventRecord error: " << cuda_err << std::endl;
return -1;
}
}
switch (kernel) {
case Kernel::kCopy:
CopyKernel<T><<<num_thread_blocks, num_threads_per_block, 0, args->sub.stream>>>(
reinterpret_cast<VecT<T> *>(args->sub.gpu_buf_ptrs[2].get()),
reinterpret_cast<const VecT<T> *>(args->sub.gpu_buf_ptrs[0].get()));
args->sub.kernel_name = "COPY";
break;
case Kernel::kScale:
ScaleKernel<T><<<num_thread_blocks, num_threads_per_block, 0, args->sub.stream>>>(
reinterpret_cast<VecT<T> *>(args->sub.gpu_buf_ptrs[2].get()),
reinterpret_cast<const VecT<T> *>(args->sub.gpu_buf_ptrs[0].get()), static_cast<T>(scalar));
args->sub.kernel_name = "SCALE";
break;
case Kernel::kAdd:
AddKernel<T><<<num_thread_blocks, num_threads_per_block, 0, args->sub.stream>>>(
reinterpret_cast<VecT<T> *>(args->sub.gpu_buf_ptrs[2].get()),
reinterpret_cast<const VecT<T> *>(args->sub.gpu_buf_ptrs[0].get()),
reinterpret_cast<const VecT<T> *>(args->sub.gpu_buf_ptrs[1].get()));
size_factor = 3;
args->sub.kernel_name = "ADD";
break;
case Kernel::kTriad:
TriadKernel<T><<<num_thread_blocks, num_threads_per_block, 0, args->sub.stream>>>(
reinterpret_cast<VecT<T> *>(args->sub.gpu_buf_ptrs[2].get()),
reinterpret_cast<const VecT<T> *>(args->sub.gpu_buf_ptrs[0].get()),
reinterpret_cast<const VecT<T> *>(args->sub.gpu_buf_ptrs[1].get()), static_cast<T>(scalar));
size_factor = 3;
args->sub.kernel_name = "TRIAD";
break;
default:
std::cerr << "RunStreamKernel::Invalid kernel: " << std::endl;
break;
}
// Record end event at the end of iterations
if (i + 1 == args->num_loops + args->num_warm_up) {
cuda_err = cudaEventRecord(args->sub.end_event, args->sub.stream);
if (cuda_err != cudaSuccess) {
std::cerr << "RunStreamKernel::cudaEventRecord error: " << cuda_err << std::endl;
return -1;
}
}
}
// Wait for the stream to finish
cuda_err = cudaStreamSynchronize(args->sub.stream);
if (cuda_err != cudaSuccess) {
std::cerr << "RunStreamKernel::cudaStreamSynchronize error: " << cuda_err << std::endl;
return -1;
}
// Calculate time
float time_in_ms = 0;
cuda_err = cudaEventElapsedTime(&time_in_ms, args->sub.start_event, args->sub.end_event);
if (cuda_err != cudaSuccess) {
std::cerr << "RunStreamKernel::cudaEventElapsedTime error: " << cuda_err << std::endl;
return -1;
}
args->sub.times_in_ms[static_cast<int>(kernel)].push_back(time_in_ms / size_factor);
return 0;
}
float GpuStream::GetActualMemoryClockRate(int gpu_id) {
nvmlReturn_t result;
nvmlDevice_t device;
unsigned int clock_mhz;
// Initialize NVML
result = nvmlInit();
if (result != NVML_SUCCESS) {
std::cerr << "Failed to initialize NVML: " << nvmlErrorString(result) << std::endl;
return -1.0f;
}
// Get device handle
result = nvmlDeviceGetHandleByIndex(gpu_id, &device);
if (result != NVML_SUCCESS) {
std::cerr << "Failed to get device handle: " << nvmlErrorString(result) << std::endl;
nvmlShutdown();
return -1.0f;
}
// Get memory clock rate
result = nvmlDeviceGetClockInfo(device, NVML_CLOCK_MEM, &clock_mhz);
if (result != NVML_SUCCESS) {
std::cerr << "Failed to get memory clock: " << nvmlErrorString(result) << std::endl;
nvmlShutdown();
return -1.0f;
}
nvmlShutdown();
return static_cast<float>(clock_mhz);
}
/**
* @brief Runs the benchmark for various kernels and processes the results for a BenchArgs config.
*
* @details This function prepares the necessary buffers and streams, runs the benchmark for each kernel
* with different thread per block configurations, checks the results, and processes the benchmark results.
* It also handles cleanup of resources in case of errors.
*
* @param[in,out] args A unique pointer to a BenchArgs structure containing the necessary arguments for the
benchmark.
*
* @return int The status code indicating success or failure of the benchmark execution.
* */
template <typename T>
int GpuStream::RunStream(std::unique_ptr<BenchArgs<T>> &args, const std::string &data_type, float peak_bw) {
int ret = 0;
ret = PrepareBufAndStream<T>(args);
if (ret != 0) {
return DestroyBufAndStream(args);
}
ret = PrepareEvent(args);
if (ret != 0) {
return DestroyEvent(args);
}
// benchmark over the kThreadsPerBlock array
for (const int num_threads_in_block : kThreadsPerBlock) {
// run the stream benchmark over the stream kernels
for (int i = 0; i < static_cast<int>(Kernel::kCount); ++i) {
Kernel kernel = static_cast<Kernel>(i);
int ret = RunStreamKernel<T>(args, kernel, num_threads_in_block);
if (ret == 0 && args->check_data) {
// Compare buffer based on the kernel
ret = CheckBuf(args, i);
}
}
}
// output formatted results to stdout
// Tags are of format:
// STREAM_<Kernelname>_datatype_buffer_<buffer_size>_block_<block_size>
for (int i = 0; i < args->sub.times_in_ms.size(); i++) {
std::string tag = "STREAM_" + KernelToString(i) + "_" + data_type + "_buffer_" + std::to_string(args->size);
for (int j = 0; j < args->sub.times_in_ms[i].size(); j++) {
// Calculate and display bandwidth
double bw = args->size * args->num_loops / args->sub.times_in_ms[i][j] / 1e6;
std::cout << tag << "_block_" << kThreadsPerBlock[j] << "\t" << bw << "\t";
if (peak_bw < 0) { // cannot get peak_bw -> prints -1 for efficiency
std::cout << "-1" << std::endl;
} else {
std::cout << std::fixed << std::setprecision(2) << bw / peak_bw * 100 << std::endl;
}
}
}
// cleanup buffer and streams for the curr arg
Destroy(args);
return ret;
}
/**
* @brief Runs the Stream benchmark.
*
* @details This function processes the input args, validates and composes the BenchArgs structure for
* the first visible GPU (CUDA device 0). When running under Superbench's default_local_mode,
* CUDA_VISIBLE_DEVICES is set per process, so device 0 maps to the assigned physical GPU.
*
* @return int The status code indicating success or failure of the benchmark execution.
* */
int GpuStream::Run() {
int ret = 0;
int gpu_count = 0;
// Get number of NUMA nodes
if (numa_available()) {
std::cerr << "main::numa_available error" << std::endl;
return -1;
}
// Get number of GPUs
ret = GetGpuCount(&gpu_count);
if (ret != 0) {
return ret;
}
if (gpu_count < 1) {
std::cerr << "Run::No GPU available" << std::endl;
return -1;
}
// Run on CUDA device 0 (the visible GPU assigned by CUDA_VISIBLE_DEVICES).
if (opts_.data_type == "float") {
auto args = std::make_unique<BenchArgs<float>>();
args->gpu_id = 0;
cudaGetDeviceProperties(&args->gpu_device_prop, 0);
args->num_warm_up = opts_.num_warm_up;
args->num_loops = opts_.num_loops;
args->size = opts_.size;
args->check_data = opts_.check_data;
bench_args_.emplace_back(std::move(args));
} else {
auto args = std::make_unique<BenchArgs<double>>();
args->gpu_id = 0;
cudaGetDeviceProperties(&args->gpu_device_prop, 0);
args->num_warm_up = opts_.num_warm_up;
args->num_loops = opts_.num_loops;
args->size = opts_.size;
args->check_data = opts_.check_data;
bench_args_.emplace_back(std::move(args));
}
bool has_error = false;
// Run the benchmark for all the configured data
for (auto &variant_args : bench_args_) {
std::visit(
[&](auto &curr_args) {
// Get memory clock rate once for both bandwidth computation and display
float memory_clock_mhz = GetMemoryClockRate(curr_args->gpu_id, curr_args->gpu_device_prop);
// Compute theoretical bandwidth using the memory clock rate
float peak_bw = -1.0f;
if (memory_clock_mhz > 0.0f) {
// Calculate theoretical bandwidth: memory_clock_mhz * bus_width_bytes * 2 (DDR) / 1000 (convert to
// GB/s)
peak_bw = memory_clock_mhz * (curr_args->gpu_device_prop.memoryBusWidth / 8) * 2 / 1000.0;
}
// Print device info with both the memory clock and peak bandwidth
PrintCudaDeviceInfo(curr_args->gpu_id, curr_args->gpu_device_prop, memory_clock_mhz, peak_bw);
// Run the stream benchmark for the configured data, passing the peak bandwidth
if constexpr (std::is_same_v<std::decay_t<decltype(*curr_args)>, BenchArgs<float>>) {
ret = RunStream<float>(curr_args, "float", peak_bw);
} else if constexpr (std::is_same_v<std::decay_t<decltype(*curr_args)>, BenchArgs<double>>) {
ret = RunStream<double>(curr_args, "double", peak_bw);
} else {
std::cerr << "Run::Unknown type error" << std::endl;
has_error = true;
return;
}
if (ret != 0) {
std::cerr << "Run::RunStream error: " << errno << std::endl;
has_error = true;
}
},
variant_args);
}
if (has_error) {
return -1;
}
return ret;
}