-
Notifications
You must be signed in to change notification settings - Fork 11
/
wmma_cg.cu
211 lines (172 loc) · 6.66 KB
/
wmma_cg.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
#if 0
#define CUB_HALF_OPTIMIZATION 1
#include <benchmark/benchmark.h>
#include <type_traits>
#include <utility>
#include <cooperative_groups.h>
#include "init/init.hpp"
#include "prefixsum/args.hpp"
#include "utils/utils.hpp"
#include "kernel.cuh"
using namespace wmma_prefixsum;
template <typename Fun>
struct is_function_ptr
: std::integral_constant<
bool, std::is_pointer<Fun>::value and
std::is_function<typename std::remove_pointer<Fun>::type>::value> {};
template <typename Arg, typename... Args>
static inline void collect_argument_addresses(void **collected_addresses, Arg &&arg,
Args &&... args) {
collected_addresses[0] = static_cast<void *>(&arg);
collect_argument_addresses(collected_addresses + 1, std::forward<Args>(args)...);
}
template <typename... Args>
static inline void **collect_arguments(Args &&... args) {
void **argument_ptrs = (void **) malloc((sizeof...(Args)) * sizeof(void *));
collect_argument_addresses(argument_ptrs, std::forward<Args>(args)...);
return argument_ptrs;
}
template <int SEGMENT_SIZE, int WARPS_PER_BLOCK>
void tryCUDA_WMMA_FULL_PREFIXSUM_CG(benchmark::State &state) {
const int num_elements = state.range(0);
int num_segments = (num_elements + SEGMENT_SIZE - 1) / SEGMENT_SIZE;
const int BLOCK_DIM = WARPS_PER_BLOCK * WARP_SIZE;
if (num_elements % SEGMENT_SIZE) {
state.SkipWithError("num_elements must be multiples of SEGMENT_SIZE");
return;
}
auto h_in = new float[num_elements];
std::fill(h_in, h_in + num_elements, 0.01f);
float *d_in_fp32 = nullptr;
half *d_in_fp16 = nullptr;
half *d_out = nullptr;
half *partial_sums = nullptr;
PRINT_IF_ERROR(cudaMalloc(&d_in_fp32, num_elements * sizeof(float)));
PRINT_IF_ERROR(cudaMalloc(&d_in_fp16, num_elements * sizeof(half)));
PRINT_IF_ERROR(cudaMalloc(&d_out, num_elements * sizeof(half)));
PRINT_IF_ERROR(cudaMalloc(&partial_sums, num_segments * sizeof(half)));
PRINT_IF_ERROR(
cudaMemcpy(d_in_fp32, h_in, num_elements * sizeof(float), cudaMemcpyHostToDevice));
PRINT_IF_LAUNCH_ERROR((convertFp32ToFp16<<<(num_elements + 1023) / 1024, 1024>>>(
d_in_fp16, d_in_fp32, num_elements)));
dim3 gridDim, blockDim;
blockDim.x = BLOCK_DIM;
gridDim.x = (num_segments + WARPS_PER_BLOCK - 1) / WARPS_PER_BLOCK;
cudaEvent_t start, stop;
PRINT_IF_ERROR(cudaEventCreate(&start));
PRINT_IF_ERROR(cudaEventCreate(&stop));
#if 0
const auto params =
collect_arguments(d_in_fp16, d_out, num_segments, SEGMENT_SIZE);
defer(free(params));
#else
const auto segment_size = SEGMENT_SIZE;
void *params[] = {(void *) &d_in_fp16, (void *) &d_out, (void *) &partial_sums,
(void *) &num_segments, (void *) &segment_size};
#endif
int maxActiveBlocks;
cudaOccupancyMaxActiveBlocksPerMultiprocessor(
&maxActiveBlocks,
(void *) &compute_wmma_prefixsum_cg<SEGMENT_SIZE, WARPS_PER_BLOCK, BLOCK_DIM>,
blockDim.x, 0);
/* printf("gridDim = %d maxActiveBlocks = %d\n", gridDim.x,
* maxActiveBlocks);
*/
try {
for (auto _ : state) {
PRINT_IF_ERROR(cudaEventRecord(start));
cudaLaunchCooperativeKernel(
(const void
*) &compute_wmma_prefixsum_cg<SEGMENT_SIZE, WARPS_PER_BLOCK, BLOCK_DIM>,
gridDim, blockDim, params);
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_elements", num_elements},
{"segment_size", SEGMENT_SIZE},
{"warps_per_block", WARPS_PER_BLOCK},
{"flops",
{state.iterations() * 1.0 * num_elements,
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;
float correct_sum = 0;
for (int i = 0; i < num_elements; i++) {
correct_sum += h_in[i];
if (fabs(half_to_float(h_out[i]) - correct_sum) > 0.1) {
errors++;
if (errors < 10) {
printf("Expected %f, get h_out[%d] = %f\n", correct_sum, i,
half_to_float(h_out[i]));
}
}
}
if (errors > 0) {
printf("CUDA_PREFIXSUM_WMM does not agree with SEQUENTIAL! %d errors!\n",
errors);
} else {
printf("Results verified: they agree.\n\n");
}
delete h_out;
#endif
PRINT_IF_ERROR(cudaEventDestroy(start));
PRINT_IF_ERROR(cudaEventDestroy(stop));
} catch (...) {
delete[] h_in;
cudaFree(d_in_fp32);
cudaFree(d_in_fp16);
cudaFree(d_out);
cudaFree(partial_sums);
const auto p = std::current_exception();
std::rethrow_exception(p);
}
}
template <int SEGMENT_SIZE, int WARPS_PER_BLOCK>
void CUDA_WMMA_FULL_PREFIXSUM_CG(benchmark::State &state) {
cudaDeviceReset();
try {
tryCUDA_WMMA_FULL_PREFIXSUM_CG<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");
}
}
#define BENCHMARK_PRIFIXSUM0(SEGMENT_SIZE, WARPS_PER_BLOCK) \
BENCHMARK_TEMPLATE(CUDA_WMMA_FULL_PREFIXSUM_CG, SEGMENT_SIZE, WARPS_PER_BLOCK) \
->ARGS() \
->UseManualTime()
#define BENCHMARK_PRIFIXSUM(SEGMENT_SIZE) \
BENCHMARK_PRIFIXSUM0(SEGMENT_SIZE, 1); \
BENCHMARK_PRIFIXSUM0(SEGMENT_SIZE, 2); \
BENCHMARK_PRIFIXSUM0(SEGMENT_SIZE, 4); \
BENCHMARK_PRIFIXSUM0(SEGMENT_SIZE, 8); \
BENCHMARK_PRIFIXSUM0(SEGMENT_SIZE, 16)
// disabled
#if 0
BENCHMARK_PRIFIXSUM(256);
BENCHMARK_PRIFIXSUM(2 * 256);
BENCHMARK_PRIFIXSUM(4 * 256);
BENCHMARK_PRIFIXSUM(8 * 256);
#if 0 // use too much shared data
BENCHMARK_PRIFIXSUM(16 * 256);
BENCHMARK_PRIFIXSUM(32 * 256);
BENCHMARK_PRIFIXSUM(64 * 256);
BENCHMARK_PRIFIXSUM(128 * 256);
BENCHMARK_PRIFIXSUM(256 * 256);
BENCHMARK_PRIFIXSUM(512 * 256);
BENCHMARK_PRIFIXSUM(1024 * 256);
#endif
#endif
#endif