forked from rapidsai/rapidsmpf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_pack.cpp
More file actions
329 lines (273 loc) · 10.6 KB
/
bench_pack.cpp
File metadata and controls
329 lines (273 loc) · 10.6 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES.
* SPDX-License-Identifier: Apache-2.0
*/
#include <algorithm>
#include <cstdint>
#include <memory>
#include <benchmark/benchmark.h>
#include <cudf/contiguous_split.hpp>
#include <cudf/table/table.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/span.hpp>
#include <rmm/cuda_device.hpp>
#include <rmm/cuda_stream_view.hpp>
#include <rmm/device_buffer.hpp>
#include <rmm/mr/cuda_async_memory_resource.hpp>
#include <rmm/mr/pool_memory_resource.hpp>
#include <rapidsmpf/memory/pinned_memory_resource.hpp>
#include "utils/random_data.hpp"
constexpr std::size_t MB = 1024 * 1024;
/**
* @brief Runs the cudf::pack benchmark
* @param state The benchmark state
* @param table_size_mb The size of the table in MB
* @param table_mr The memory resource for the table
* @param pack_mr The memory resource for the packed data
* @param stream The CUDA stream to use
*/
void run_pack(
benchmark::State& state,
std::size_t table_size_mb,
rmm::device_async_resource_ref table_mr,
rmm::device_async_resource_ref pack_mr,
rmm::cuda_stream_view stream
) {
auto const table_size_bytes = table_size_mb * MB;
// Calculate number of rows for a single-column table of the desired size
auto const nrows =
static_cast<cudf::size_type>(table_size_bytes / sizeof(random_data_t));
auto table = random_table(1, nrows, 0, 1000, stream, table_mr);
// Warm up
auto warm_up = cudf::pack(table.view(), stream, pack_mr);
stream.synchronize();
for (auto _ : state) {
auto packed = cudf::pack(table.view(), stream, pack_mr);
benchmark::DoNotOptimize(packed);
stream.synchronize();
}
state.SetBytesProcessed(
static_cast<std::int64_t>(state.iterations())
* static_cast<std::int64_t>(table_size_bytes)
);
state.counters["table_size_mb"] = static_cast<double>(table_size_mb);
state.counters["num_rows"] = nrows;
}
/**
* @brief Benchmark for cudf::pack with device memory
*/
static void BM_Pack_device(benchmark::State& state) {
auto const table_size_mb = static_cast<std::size_t>(state.range(0));
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
// Create memory resources
rmm::mr::cuda_async_memory_resource cuda_mr;
rmm::mr::pool_memory_resource<rmm::mr::cuda_async_memory_resource> pool_mr{
cuda_mr, rmm::percent_of_free_device_memory(40)
};
run_pack(state, table_size_mb, pool_mr, pool_mr, stream);
}
/**
* @brief Benchmark for cudf::pack with pinned memory
*/
static void BM_Pack_pinned(benchmark::State& state) {
state.SkipWithMessage("Skipping until cudf#20886 is fixed");
/* if (!rapidsmpf::is_pinned_memory_resources_supported()) {
state.SkipWithMessage("Pinned memory resources are not supported");
return;
}
auto const table_size_mb = static_cast<std::size_t>(state.range(0));
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
// Create memory resources
rmm::mr::cuda_async_memory_resource cuda_mr;
rmm::mr::pool_memory_resource<rmm::mr::cuda_async_memory_resource> pool_mr{
cuda_mr, rmm::percent_of_free_device_memory(40)
};
rapidsmpf::PinnedMemoryResource pinned_mr;
run_pack(state, table_size_mb, pool_mr, pinned_mr, stream); */
}
/**
* @brief Runs the cudf::chunked_pack benchmark
* @param state The benchmark state
* @param bounce_buffer_size The size of the bounce buffer in bytes
* @param table_size The size of the table in bytes
* @param table_mr The memory resource for the table
* @param pack_mr The memory resource for the packed data
* @param stream The CUDA stream to use
*/
void run_chunked_pack(
benchmark::State& state,
std::size_t bounce_buffer_size,
std::size_t table_size,
rmm::device_async_resource_ref table_mr,
rmm::device_async_resource_ref pack_mr,
rmm::cuda_stream_view stream
) {
// Calculate number of rows for a single-column table of the desired size
auto const nrows = static_cast<cudf::size_type>(table_size / sizeof(random_data_t));
auto table = random_table(1, nrows, 0, 1000, stream, table_mr);
// Create the chunked_pack instance to get total output size
size_t total_size;
{
cudf::chunked_pack packer(table.view(), bounce_buffer_size, stream, table_mr);
total_size = packer.get_total_contiguous_size();
}
// Allocate bounce buffer and destination buffer using the pack_mr
rmm::device_buffer bounce_buffer(bounce_buffer_size, stream, pack_mr);
rmm::device_buffer destination(total_size, stream, pack_mr);
auto run_packer = [&] {
cudf::chunked_pack packer(table.view(), bounce_buffer_size, stream, pack_mr);
std::size_t offset = 0;
while (packer.has_next()) {
auto const bytes_copied = packer.next(
cudf::device_span<std::uint8_t>(
static_cast<std::uint8_t*>(bounce_buffer.data()), bounce_buffer_size
)
);
RAPIDSMPF_CUDA_TRY(cudaMemcpyAsync(
static_cast<std::uint8_t*>(destination.data()) + offset,
bounce_buffer.data(),
bytes_copied,
cudaMemcpyDefault,
stream.value()
));
offset += bytes_copied;
}
};
{
run_packer();
stream.synchronize();
}
for (auto _ : state) {
run_packer();
benchmark::DoNotOptimize(destination);
stream.synchronize();
}
state.SetBytesProcessed(
static_cast<std::int64_t>(state.iterations())
* static_cast<std::int64_t>(table_size)
);
state.counters["table_size_mb"] =
static_cast<double>(table_size) / static_cast<double>(MB);
state.counters["num_rows"] = nrows;
state.counters["bounce_buffer_mb"] =
static_cast<double>(bounce_buffer_size) / static_cast<double>(MB);
}
/**
* @brief Benchmark for cudf::chunked_pack with device memory
*/
static void BM_ChunkedPack_device(benchmark::State& state) {
auto const table_size_mb = static_cast<std::size_t>(state.range(0));
auto const table_size_bytes = table_size_mb * MB;
// Bounce buffer size: max(1MB, table_size / 10)
auto const bounce_buffer_size = std::max(MB, table_size_bytes / 10);
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
rmm::mr::cuda_async_memory_resource cuda_mr;
rmm::mr::pool_memory_resource<rmm::mr::cuda_async_memory_resource> pool_mr{
cuda_mr, rmm::percent_of_free_device_memory(40)
};
run_chunked_pack(
state, bounce_buffer_size, table_size_bytes, pool_mr, pool_mr, stream
);
}
/**
* @brief Benchmark for cudf::chunked_pack pinned memory
*/
static void BM_ChunkedPack_pinned(benchmark::State& state) {
state.SkipWithMessage("Skipping until cudf#20886 is fixed");
/* if (!rapidsmpf::is_pinned_memory_resources_supported()) {
state.SkipWithMessage("Pinned memory resources are not supported");
return;
}
auto const table_size_mb = static_cast<std::size_t>(state.range(0));
auto const table_size_bytes = table_size_mb * MB;
// Bounce buffer size: max(1MB, table_size / 10)
auto const bounce_buffer_size = std::max(MB, table_size_bytes / 10);
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
rmm::mr::cuda_async_memory_resource cuda_mr;
rmm::mr::pool_memory_resource<rmm::mr::cuda_async_memory_resource> pool_mr{
cuda_mr, rmm::percent_of_free_device_memory(40)
};
rapidsmpf::PinnedMemoryResource pinned_mr;
run_chunked_pack(
state, bounce_buffer_size, table_size_bytes, pool_mr, pinned_mr, stream
); */
}
// Custom argument generator for the benchmark
void PackArguments(benchmark::internal::Benchmark* b) {
// Test different table sizes in MB (minimum 1MB as requested)
for (auto size_mb : {1, 10, 100, 500, 1000, 2000, 4000}) {
b->Args({size_mb});
}
}
// Register the benchmarks
BENCHMARK(BM_Pack_device)
->Apply(PackArguments)
->UseRealTime()
->Unit(benchmark::kMillisecond);
BENCHMARK(BM_Pack_pinned)
->Apply(PackArguments)
->UseRealTime()
->Unit(benchmark::kMillisecond);
BENCHMARK(BM_ChunkedPack_device)
->Apply(PackArguments)
->UseRealTime()
->Unit(benchmark::kMillisecond);
BENCHMARK(BM_ChunkedPack_pinned)
->Apply(PackArguments)
->UseRealTime()
->Unit(benchmark::kMillisecond);
/**
* @brief Benchmark for cudf::chunked_pack in device memory varying the bounce buffer size
* and keeping table size fixed at 1GB
*/
static void BM_ChunkedPack_fixed_table_device(benchmark::State& state) {
auto const bounce_buffer_size = static_cast<std::size_t>(state.range(0)) * MB;
constexpr std::size_t table_size_bytes = 1024 * MB;
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
// Create memory resources
rmm::mr::cuda_async_memory_resource cuda_mr;
rmm::mr::pool_memory_resource<rmm::mr::cuda_async_memory_resource> pool_mr{
cuda_mr, rmm::percent_of_free_device_memory(40)
};
run_chunked_pack(
state, bounce_buffer_size, table_size_bytes, pool_mr, pool_mr, stream
);
}
/**
* @brief Benchmark for cudf::chunked_pack in pinned memory varying the bounce buffer size
* and keeping table size fixed at 1GB
*/
static void BM_ChunkedPack_fixed_table_pinned(benchmark::State& state) {
state.SkipWithMessage("Skipping until cudf#20886 is fixed");
/* if (!rapidsmpf::is_pinned_memory_resources_supported()) {
state.SkipWithMessage("Pinned memory resources are not supported");
return;
}
auto const bounce_buffer_size = static_cast<std::size_t>(state.range(0)) * MB;
constexpr std::size_t table_size_bytes = 1024 * MB;
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
rmm::mr::cuda_async_memory_resource cuda_mr;
rmm::mr::pool_memory_resource<rmm::mr::cuda_async_memory_resource> pool_mr{
cuda_mr, rmm::percent_of_free_device_memory(40)
};
rapidsmpf::PinnedMemoryResource pinned_mr;
run_chunked_pack(
state, bounce_buffer_size, table_size_bytes, pool_mr, pinned_mr, stream
); */
}
// Custom argument generator for the benchmark
void ChunkedPackArguments(benchmark::internal::Benchmark* b) {
// Test different table sizes in MB (minimum 1MB as requested)
for (auto bounce_buf_sz_mb : {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}) {
b->Args({bounce_buf_sz_mb});
}
}
BENCHMARK(BM_ChunkedPack_fixed_table_device)
->Apply(ChunkedPackArguments)
->UseRealTime()
->Unit(benchmark::kMillisecond);
BENCHMARK(BM_ChunkedPack_fixed_table_pinned)
->Apply(ChunkedPackArguments)
->UseRealTime()
->Unit(benchmark::kMillisecond);
BENCHMARK_MAIN();