-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathseg_cub_block.cu
289 lines (252 loc) · 9.91 KB
/
seg_cub_block.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
#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;
template <int THREADS_PER_BLOCK, int ITEMS_PER_THREAD, BlockReduceAlgorithm ALGORITHM>
__global__ void compute_cub_block_segmented_reduction(half *d_in, half *d_out) {
// Specialize BlockReduce type for our thread block
typedef BlockReduce<half, THREADS_PER_BLOCK, ALGORITHM> BlockReduceT;
// Shared memory
__shared__ typename BlockReduceT::TempStorage temp_storage;
// Per-thread tile data
half data[ITEMS_PER_THREAD];
LoadDirectStriped<THREADS_PER_BLOCK, half, ITEMS_PER_THREAD>(
threadIdx.x, d_in + blockIdx.x * THREADS_PER_BLOCK * ITEMS_PER_THREAD, data);
// Compute sum
half aggregate = BlockReduceT(temp_storage).Sum(data);
// Store aggregate
if (threadIdx.x == 0) {
d_out[blockIdx.x] = aggregate;
}
}
template <int THREADS_PER_BLOCK, int ITEMS_PER_THREAD, BlockReduceAlgorithm ALGORITHM>
static void CUB_BLOCK_SEGMENTED_REDUCTION(benchmark::State &state) {
const size_t num_segments = state.range(0);
const size_t segment_size = state.range(1);
if (segment_size != THREADS_PER_BLOCK * ITEMS_PER_THREAD) {
state.SkipWithError("segment size must be THREADS_PER_BLOCK x ITEMS_PER_THREAD");
return;
}
if (num_segments >= CUDA_MAX_GRID_SIZE) {
state.SkipWithError(
fmt::format("gridDim.x={} is greater than CUDA_MAX_GRID_SIZE", num_segments)
.c_str());
return;
}
const size_t num_elements = num_segments * segment_size;
half *d_in_fp16 = nullptr;
half *d_out = nullptr;
try {
PRINT_IF_ERROR(cudaMalloc(&d_in_fp16, num_elements * sizeof(half)));
PRINT_IF_ERROR(cudaMalloc(&d_out, num_segments * sizeof(half)));
cuda_memory_set(d_in_fp16, 0.001f, num_elements);
PRINT_IF_ERROR(cudaDeviceSynchronize());
cudaEvent_t start, stop;
PRINT_IF_ERROR(cudaEventCreate(&start));
PRINT_IF_ERROR(cudaEventCreate(&stop));
defer(cudaEventDestroy(start));
defer(cudaEventDestroy(stop));
for (auto _ : state) {
PRINT_IF_ERROR(cudaEventRecord(start));
compute_cub_block_segmented_reduction<THREADS_PER_BLOCK,
ITEMS_PER_THREAD,
ALGORITHM>
<<<num_segments, THREADS_PER_BLOCK>>>(d_in_fp16, d_out);
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_segments", num_segments},
{"segment_size", segment_size},
{"num_elements", num_segments * segment_size},
{"threads_per_block", THREADS_PER_BLOCK},
{"items_per_thread", ITEMS_PER_THREAD},
{"block_reduce_algorithm", (int) ALGORITHM},
{"flops",
{state.iterations() * 1.0 * num_segments * segment_size,
benchmark::Counter::kAvgThreadsRate}}});
#if 0
half *h_out = new half[num_segments];
PRINT_IF_ERROR(cudaMemcpy(h_out, d_out, num_segments * sizeof(half),
cudaMemcpyDeviceToHost));
float correct_segment_sum = 0;
for (int i = 0; i < segment_size; i++) {
correct_segment_sum += h_in[i];
}
int errors = 0;
for (int i = 0; i < num_segments; i++) {
if (fabs(half_to_float(h_out[i]) - correct_segment_sum) > 0.001) {
errors++;
if (errors < 10) {
printf("segment %d has sum %f (expected %f)\n", i,
half_to_float(h_out[i]), correct_segment_sum);
}
}
}
if (errors > 0) {
printf("CUB_BLOCK_SEGMENTED_REDUCTION does not agree with SEQUENTIAL! %d "
"errors!\n",
errors);
} else {
printf("Results verified: they agree.\n\n");
}
delete h_out;
#endif
cudaFree(d_in_fp16);
cudaFree(d_out);
} catch (...) {
cudaFree(d_in_fp16);
cudaFree(d_out);
cudaDeviceReset();
const auto p = std::current_exception();
std::rethrow_exception(p);
}
}
// BlockReduceAlgorithm are BLOCK_REDUCE_RAKING,
// BLOCK_REDUCE_WARP_REDUCTIONS and
// BLOCK_REDUCE_RAKING_COMMUTATIVE_ONLY
// BLOCK_REDUCE_RAKING_COMMUTATIVE_ONLY
#if 1
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 32, 1, BLOCK_REDUCE_RAKING)
->SEG_32_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 32, 2, BLOCK_REDUCE_RAKING)
->SEG_64_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 64, 1, BLOCK_REDUCE_RAKING)
->SEG_64_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 32, 4, BLOCK_REDUCE_RAKING)
->SEG_128_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 64, 2, BLOCK_REDUCE_RAKING)
->SEG_128_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 128, 1, BLOCK_REDUCE_RAKING)
->SEG_128_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 32, 8, BLOCK_REDUCE_RAKING)
->SEG_256_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 64, 4, BLOCK_REDUCE_RAKING)
->SEG_256_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 128, 2, BLOCK_REDUCE_RAKING)
->SEG_256_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 256, 1, BLOCK_REDUCE_RAKING)
->SEG_256_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 32, 16, BLOCK_REDUCE_RAKING)
->SEG_512_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 64, 8, BLOCK_REDUCE_RAKING)
->SEG_512_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 128, 4, BLOCK_REDUCE_RAKING)
->SEG_512_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 256, 2, BLOCK_REDUCE_RAKING)
->SEG_512_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 512, 1, BLOCK_REDUCE_RAKING)
->SEG_512_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 32, 32, BLOCK_REDUCE_RAKING)
->SEG_1024_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 64, 16, BLOCK_REDUCE_RAKING)
->SEG_1024_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 128, 8, BLOCK_REDUCE_RAKING)
->SEG_1024_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 256, 4, BLOCK_REDUCE_RAKING)
->SEG_1024_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 512, 2, BLOCK_REDUCE_RAKING)
->SEG_1024_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 1024, 1, BLOCK_REDUCE_RAKING)
->SEG_1024_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 32, 64, BLOCK_REDUCE_RAKING)
->SEG_2048_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 64, 32, BLOCK_REDUCE_RAKING)
->SEG_2048_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 128, 16, BLOCK_REDUCE_RAKING)
->SEG_2048_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 256, 8, BLOCK_REDUCE_RAKING)
->SEG_2048_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 512, 4, BLOCK_REDUCE_RAKING)
->SEG_2048_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 1024, 2, BLOCK_REDUCE_RAKING)
->SEG_2048_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 32, 128, BLOCK_REDUCE_RAKING)
->SEG_4096_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 64, 64, BLOCK_REDUCE_RAKING)
->SEG_4096_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 128, 32, BLOCK_REDUCE_RAKING)
->SEG_4096_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 256, 16, BLOCK_REDUCE_RAKING)
->SEG_4096_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 512, 8, BLOCK_REDUCE_RAKING)
->SEG_4096_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 1024, 4, BLOCK_REDUCE_RAKING)
->SEG_4096_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 32, 256, BLOCK_REDUCE_RAKING)
->SEG_8192_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 64, 128, BLOCK_REDUCE_RAKING)
->SEG_8192_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 128, 64, BLOCK_REDUCE_RAKING)
->SEG_8192_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 256, 32, BLOCK_REDUCE_RAKING)
->SEG_8192_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 512, 16, BLOCK_REDUCE_RAKING)
->SEG_8192_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 1024, 8, BLOCK_REDUCE_RAKING)
->SEG_8192_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 32, 512, BLOCK_REDUCE_RAKING)
->SEG_16384_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 64, 256, BLOCK_REDUCE_RAKING)
->SEG_16384_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 128, 128, BLOCK_REDUCE_RAKING)
->SEG_16384_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 256, 64, BLOCK_REDUCE_RAKING)
->SEG_16384_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 512, 32, BLOCK_REDUCE_RAKING)
->SEG_16384_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(CUB_BLOCK_SEGMENTED_REDUCTION, 1024, 16, BLOCK_REDUCE_RAKING)
->SEG_16384_ARGS()
->UseManualTime();
#endif