-
Notifications
You must be signed in to change notification settings - Fork 11
/
seg_wmma_16n.cu
223 lines (190 loc) · 7.74 KB
/
seg_wmma_16n.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
#include <benchmark/benchmark.h>
#include "init/init.hpp"
#include "prefixsum/args.hpp"
#include "utils/utils.hpp"
#include "kernel.cuh"
using namespace wmma_prefixsum;
template <int SEGMENT_SIZE, int WARPS_PER_BLOCK>
void tryCUDA_WMMA_SEGMENTED_PREFIXSUM_16N(benchmark::State &state) {
const size_t num_segments = state.range(0);
const size_t segment_size = state.range(1);
if (segment_size != SEGMENT_SIZE) {
state.SkipWithError(fmt::format("segment_size={} must be equal to SEGMENT_SIZE={} ",
segment_size, SEGMENT_SIZE)
.c_str());
return;
}
const int BLOCK_DIM = WARPS_PER_BLOCK * WARP_SIZE;
const size_t num_elements = num_segments * segment_size;
const int segments_per_block = WARPS_PER_BLOCK * 16;
defer(cudaDeviceReset());
half *d_in_fp16 = nullptr;
half *d_out = nullptr;
cudaEvent_t start, stop;
try {
PRINT_IF_ERROR(cudaMalloc(&d_in_fp16, num_elements * sizeof(half)));
PRINT_IF_ERROR(cudaMalloc(&d_out, num_elements * sizeof(half)));
cuda_memory_set(d_in_fp16, 0.001f, num_elements);
dim3 gridDim, blockDim;
blockDim.x = BLOCK_DIM;
gridDim.x = (num_segments + segments_per_block - 1) / segments_per_block;
if (gridDim.x >= CUDA_MAX_GRID_SIZE) {
state.SkipWithError(
fmt::format("gridDim.x={} is greater than CUDA_MAX_GRID_SIZE", gridDim.x)
.c_str());
return;
}
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_wmma_segmented_prefixsum_16n<SEGMENT_SIZE, WARPS_PER_BLOCK, BLOCK_DIM>
<<<gridDim, blockDim>>>(d_in_fp16, d_out, num_segments);
PRINT_IF_ERROR(cudaEventRecord(stop));
PRINT_IF_ERROR(cudaEventSynchronize(stop));
/* state.SkipWithError("break"); */
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},
{"warps_per_block", WARPS_PER_BLOCK},
{"flops",
{state.iterations() * 1.0 * num_segments * segment_size,
benchmark::Counter::kAvgThreadsRate}}});
#if 0
half *h_out = new half[num_elements];
PRINT_IF_ERROR(cudaMemcpy(h_out, d_out, num_elements * sizeof(half),
cudaMemcpyDeviceToHost));
int errors = 0;
for (int j = 0; j < num_segments; j++) {
float correct_segment_sum = 0;
for (int i = 0; i < segment_size; i++) {
correct_segment_sum += h_in[j * segment_size + i];
if (fabs(half_to_float(h_out[j * segment_size + i]) -
correct_segment_sum) > 0.01) {
errors++;
if (errors < 10) {
printf("Expected %f, get h_out[%d] = %f\n", correct_segment_sum, i,
half_to_float(h_out[j * segment_size + i]));
}
}
}
}
if (errors > 0) {
printf("CUDA_WMMA_SEGMENTED_PREFIXSUM_16N 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);
}
}
template <size_t SEGMENT_SIZE, int WARPS_PER_BLOCK>
static void iCUDA_WMMA_SEGMENTED_PREFIXSUM_16N(benchmark::State &state) {
cudaDeviceReset();
try {
tryCUDA_WMMA_SEGMENTED_PREFIXSUM_16N<SEGMENT_SIZE, WARPS_PER_BLOCK>(state);
} catch (const std::exception &e) {
state.SkipWithError(e.what());
} catch (const std::string &e) {
state.SkipWithError(e.c_str());
} catch (...) {
state.SkipWithError("unknown exception");
}
}
template <int WARPS_PER_BLOCK>
static void CUDA_WMMA_SEGMENTED_PREFIXSUM_16N(benchmark::State &state) {
const int segment_size = state.range(1);
switch (segment_size) {
#define Dispatch(N) \
case N: \
iCUDA_WMMA_SEGMENTED_PREFIXSUM_16N<N, WARPS_PER_BLOCK>(state); \
break
Dispatch(16);
Dispatch(32);
Dispatch(64);
Dispatch(128);
Dispatch(256);
Dispatch(512);
Dispatch(1024);
Dispatch(2048);
Dispatch(4096);
Dispatch(8192);
Dispatch(16384);
Dispatch(32768);
Dispatch(65536);
Dispatch(131072);
Dispatch(262144);
Dispatch(524288);
Dispatch(1048576);
Dispatch(2097152);
Dispatch(4194304);
Dispatch(8388608);
Dispatch(16777216);
Dispatch(33554432);
Dispatch(67108864);
Dispatch(134217728);
Dispatch(268435456);
Dispatch(536870912);
Dispatch(1073741824);
default:
static_assert(true, "invalid segment size");
state.SkipWithError("invalid segment size");
#undef DISPATCH
}
}
template <int WARPS_PER_BLOCK>
static void CUDA_WMMA_TUNE_SEGMENTED_PREFIXSUM_16N(benchmark::State &state) {
CUDA_WMMA_SEGMENTED_PREFIXSUM_16N<WARPS_PER_BLOCK>(state);
}
#define RUN_CUDA_WMMA_TUNE(TUNE_ARGS) \
BENCHMARK_TEMPLATE(CUDA_WMMA_TUNE_SEGMENTED_PREFIXSUM_16N, 1) \
->Apply(TUNE_ARGS) \
->UseManualTime(); \
BENCHMARK_TEMPLATE(CUDA_WMMA_TUNE_SEGMENTED_PREFIXSUM_16N, 2) \
->Apply(TUNE_ARGS) \
->UseManualTime(); \
BENCHMARK_TEMPLATE(CUDA_WMMA_TUNE_SEGMENTED_PREFIXSUM_16N, 4) \
->Apply(TUNE_ARGS) \
->UseManualTime(); \
BENCHMARK_TEMPLATE(CUDA_WMMA_TUNE_SEGMENTED_PREFIXSUM_16N, 8) \
->Apply(TUNE_ARGS) \
->UseManualTime(); \
BENCHMARK_TEMPLATE(CUDA_WMMA_TUNE_SEGMENTED_PREFIXSUM_16N, 16) \
->Apply(TUNE_ARGS) \
->UseManualTime();
// RUN_CUDA_WMMA_TUNE(Tuning16_x_14);
// RUN_CUDA_WMMA_TUNE(Tuning16_x_18);
// RUN_CUDA_WMMA_TUNE(Tuning16_x_22);
// RUN_CUDA_WMMA_TUNE(Tuning16_x_26);
RUN_CUDA_WMMA_TUNE(Tuning16_x_30);
#define RUN_CUDA_WMMA(Args) \
BENCHMARK_TEMPLATE(CUDA_WMMA_SEGMENTED_PREFIXSUM_16N, 1)->Args()->UseManualTime(); \
BENCHMARK_TEMPLATE(CUDA_WMMA_SEGMENTED_PREFIXSUM_16N, 2)->Args()->UseManualTime(); \
BENCHMARK_TEMPLATE(CUDA_WMMA_SEGMENTED_PREFIXSUM_16N, 4)->Args()->UseManualTime(); \
BENCHMARK_TEMPLATE(CUDA_WMMA_SEGMENTED_PREFIXSUM_16N, 8)->Args()->UseManualTime(); \
BENCHMARK_TEMPLATE(CUDA_WMMA_SEGMENTED_PREFIXSUM_16N, 16)->Args()->UseManualTime();
RUN_CUDA_WMMA(SEG_16_ARGS);
RUN_CUDA_WMMA(SEG_32_ARGS);
RUN_CUDA_WMMA(SEG_64_ARGS);
RUN_CUDA_WMMA(SEG_128_ARGS);
RUN_CUDA_WMMA(SEG_256_ARGS);
RUN_CUDA_WMMA(SEG_512_ARGS);
RUN_CUDA_WMMA(SEG_1024_ARGS);