-
Notifications
You must be signed in to change notification settings - Fork 11
/
cub.cu
107 lines (79 loc) · 2.78 KB
/
cub.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
#define CUB_HALF_OPTIMIZATION 1
#include <benchmark/benchmark.h>
#include "init/init.hpp"
#include "reduction/args.hpp"
#include "utils/utils.hpp"
#include <cub/cub.cuh>
using namespace cub;
static void CUB_FULL_REDUCTION(benchmark::State &state) {
const size_t num_elements = state.range(0);
half *d_in_fp16 = nullptr;
half *d_out = nullptr;
cudaEvent_t start, stop;
// Request and allocate temporary storage
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
try {
PRINT_IF_ERROR(cudaMalloc(&d_in_fp16, num_elements * sizeof(half)));
PRINT_IF_ERROR(cudaMalloc(&d_out, 1 * sizeof(half)));
cuda_memory_set(d_in_fp16, 0.001f, num_elements);
PRINT_IF_ERROR(cudaDeviceSynchronize());
PRINT_IF_ERROR(DeviceReduce::Sum(d_temp_storage, temp_storage_bytes, d_in_fp16, d_out,
num_elements));
PRINT_IF_ERROR(cudaMalloc(&d_temp_storage, temp_storage_bytes));
PRINT_IF_ERROR(cudaEventCreate(&start));
PRINT_IF_ERROR(cudaEventCreate(&stop));
defer(cudaEventDestroy(start));
defer(cudaEventDestroy(stop));
for (auto _ : state) {
PRINT_IF_ERROR(cudaEventRecord(start));
PRINT_IF_ERROR(DeviceReduce::Sum(d_temp_storage, temp_storage_bytes, d_in_fp16,
d_out, num_elements));
PRINT_IF_ERROR(cudaEventRecord(stop));
PRINT_IF_ERROR(cudaEventSynchronize(stop));
state.PauseTiming();
float msecTotal = 0.0f;
PRINT_IF_ERROR(cudaEventElapsedTime(&msecTotal, start, stop));
state.SetIterationTime(msecTotal / 1000);
state.ResumeTiming();
}
state.counters.insert({{"num_elements", num_elements},
{"flops",
{state.iterations() * 1.0 * num_elements,
benchmark::Counter::kAvgThreadsRate}}});
#if 0
half h_out;
PRINT_IF_ERROR(
cudaMemcpy(&h_out, d_out, 1 * sizeof(half), cudaMemcpyDeviceToHost));
int errors = 0;
float correct_sum = 0;
for (int i = 0; i < num_elements; i++) {
correct_sum += h_in[i];
}
if (fabs(half_to_float(h_out) - correct_sum) > 0.001) {
errors++;
if (errors < 10) {
printf("Expected %f, get h_out = %f\n", correct_sum,
half_to_float(h_out));
}
}
if (errors > 0) {
printf("CUB_FULL_REDUCTION does not agree with SEQUENTIAL! %d errors!\n",
errors);
} else {
printf("Results verified: they agree.\n\n");
}
#endif
cudaFree(d_in_fp16);
cudaFree(d_out);
cudaFree(d_temp_storage);
} catch (...) {
cudaFree(d_in_fp16);
cudaFree(d_out);
cudaFree(d_temp_storage);
cudaDeviceReset();
const auto p = std::current_exception();
std::rethrow_exception(p);
}
}
BENCHMARK(CUB_FULL_REDUCTION)->ARGS()->UseManualTime();