-
Notifications
You must be signed in to change notification settings - Fork 11
/
seg_cub_warp_original.cu
175 lines (141 loc) · 5.48 KB
/
seg_cub_warp_original.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
#include <benchmark/benchmark.h>
#include "init/init.hpp"
#include "prefixsum/args.hpp"
#include "utils/utils.hpp"
#include <cub/cub.cuh>
using namespace cub;
#ifndef WARP_SIZE
#define WARP_SIZE (32)
#endif // WARP_SIZE
template <int THREADS_PER_BLOCK, int LOGICAL_THREADS_PER_WARP>
__global__ void compute_cub_warp_segmented_prefixsum(half *d_in, half *d_out) {
constexpr int num_warps = THREADS_PER_BLOCK / LOGICAL_THREADS_PER_WARP;
const int offset = blockIdx.x * blockDim.x + threadIdx.x;
const int warp_id = offset / WARP_SIZE;
typedef WarpScan<half, LOGICAL_THREADS_PER_WARP> WarpScanT;
__shared__ typename WarpScanT::TempStorage temp_storage[num_warps];
WarpScanT(temp_storage[warp_id]).InclusiveSum(d_in[offset], d_out[offset]);
}
template <int THREADS_PER_BLOCK, int LOGICAL_THREADS_PER_WARP>
static void ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM(benchmark::State &state) {
const size_t num_segments = state.range(0);
const size_t segment_size = state.range(1);
if (segment_size != LOGICAL_THREADS_PER_WARP) {
state.SkipWithError("segment size must be LOGICAL_THREADS_PER_WARP");
}
const size_t num_elements = num_segments * segment_size;
const int segments_per_block = THREADS_PER_BLOCK / LOGICAL_THREADS_PER_WARP;
dim3 gridDim, blockDim;
blockDim.x = THREADS_PER_BLOCK;
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;
}
half *d_in_fp16 = nullptr;
half *d_out = nullptr;
cudaEvent_t start, stop;
defer(cudaDeviceReset());
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);
PRINT_IF_ERROR(cudaDeviceSynchronize());
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_warp_segmented_prefixsum<THREADS_PER_BLOCK, LOGICAL_THREADS_PER_WARP>
<<<gridDim, blockDim>>>(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},
{"logical_threads_per_block", LOGICAL_THREADS_PER_WARP},
{"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.001) {
errors++;
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("ORIGINAL_CUB_SEGMENTED_PREFIXSUM_16 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);
}
}
BENCHMARK_TEMPLATE(ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM, 32, 16)
->SEG_16_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM, 64, 16)
->SEG_16_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM, 128, 16)
->SEG_16_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM, 256, 16)
->SEG_16_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM, 512, 16)
->SEG_16_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM, 1024, 16)
->SEG_16_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM, 32, 32)
->SEG_32_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM, 64, 32)
->SEG_32_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM, 128, 32)
->SEG_32_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM, 256, 32)
->SEG_32_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM, 512, 32)
->SEG_32_ARGS()
->UseManualTime();
BENCHMARK_TEMPLATE(ORIGINAL_CUB_WARP_SEGMENTED_PREFIXSUM, 1024, 32)
->SEG_32_ARGS()
->UseManualTime();