forked from rapidsai/rapidsmpf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_memory_resources.cpp
More file actions
344 lines (278 loc) · 11 KB
/
bench_memory_resources.cpp
File metadata and controls
344 lines (278 loc) · 11 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES.
* SPDX-License-Identifier: Apache-2.0
*/
#include <cstring>
#include <benchmark/benchmark.h>
#include <rmm/cuda_stream_view.hpp>
#include <rmm/device_buffer.hpp>
#include <rmm/mr/cuda_memory_resource.hpp>
#include <rmm/mr/pinned_host_memory_resource.hpp>
#include <rapidsmpf/error.hpp>
#include <rapidsmpf/memory/pinned_memory_resource.hpp>
enum ResourceType : int {
NEW_DELETE = 0,
HOST_MEMORY_RESOURCE = 1,
PINNED_MEMORY_RESOURCE = 2,
};
constexpr std::array<ResourceType, 3> RESOURCE_TYPES{
ResourceType::NEW_DELETE,
ResourceType::HOST_MEMORY_RESOURCE,
ResourceType::PINNED_MEMORY_RESOURCE
};
std::array<std::string, 3> const ResourceTypeStr{
"NewDelete", "HostMemoryResource", "PinnedMemoryResource"
};
class NewDelete final : public rapidsmpf::HostMemoryResource {
public:
void* allocate(
rmm::cuda_stream_view,
std::size_t size,
std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT
) override {
return ::operator new(size, std::align_val_t{alignment});
}
void deallocate(
rmm::cuda_stream_view,
void* ptr,
std::size_t,
std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT
) noexcept override {
::operator delete(ptr, std::align_val_t{alignment});
}
};
// Helper function to create a memory resource based on type
std::unique_ptr<rapidsmpf::HostMemoryResource> create_host_memory_resource(
ResourceType const& resource_type
) {
switch (resource_type) {
case ResourceType::NEW_DELETE:
return std::make_unique<NewDelete>();
case ResourceType::HOST_MEMORY_RESOURCE:
return std::make_unique<rapidsmpf::HostMemoryResource>();
case ResourceType::PINNED_MEMORY_RESOURCE:
return std::make_unique<rapidsmpf::PinnedMemoryResource>();
default:
RAPIDSMPF_FAIL("Unknown memory resource type");
}
}
void BM_Allocate(benchmark::State& state) {
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
auto const allocation_size = static_cast<size_t>(state.range(0));
auto const resource_type = static_cast<ResourceType>(state.range(1));
if (resource_type == ResourceType::PINNED_MEMORY_RESOURCE
&& !rapidsmpf::is_pinned_memory_resources_supported())
{
state.SkipWithMessage("pinned memory not supported on system");
return;
}
auto mr = create_host_memory_resource(resource_type);
for (auto _ : state) {
void* ptr = mr->allocate(stream, allocation_size);
benchmark::DoNotOptimize(ptr);
stream.synchronize();
state.PauseTiming();
mr->deallocate(stream, ptr, allocation_size);
stream.synchronize();
state.ResumeTiming();
}
state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(allocation_size));
state.SetLabel(
"allocate: " + ResourceTypeStr[static_cast<std::size_t>(resource_type)]
);
}
void BM_Deallocate(benchmark::State& state) {
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
auto const allocation_size = static_cast<size_t>(state.range(0));
auto const resource_type = static_cast<ResourceType>(state.range(1));
if (resource_type == ResourceType::PINNED_MEMORY_RESOURCE
&& !rapidsmpf::is_pinned_memory_resources_supported())
{
state.SkipWithMessage("pinned memory not supported on system");
return;
}
auto mr = create_host_memory_resource(resource_type);
for (auto _ : state) {
state.PauseTiming();
void* ptr = mr->allocate(stream, allocation_size);
stream.synchronize();
state.ResumeTiming();
mr->deallocate(stream, ptr, allocation_size);
stream.synchronize();
}
state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(allocation_size));
state.SetLabel(
"deallocate: " + ResourceTypeStr[static_cast<std::size_t>(resource_type)]
);
}
void BM_DeviceToHostCopyInclAlloc(benchmark::State& state) {
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
auto const transfer_size = static_cast<size_t>(state.range(0));
auto const resource_type = static_cast<ResourceType>(state.range(1));
if (resource_type == ResourceType::PINNED_MEMORY_RESOURCE
&& !rapidsmpf::is_pinned_memory_resources_supported())
{
state.SkipWithMessage("pinned memory not supported on system");
return;
}
auto host_mr = create_host_memory_resource(resource_type);
auto device_mr = std::make_unique<rmm::mr::cuda_memory_resource>();
// Allocate device memory
auto src = rmm::device_buffer(transfer_size, stream, device_mr.get());
// Initialize src to avoid optimization removal
RAPIDSMPF_CUDA_TRY(cudaMemsetAsync(src.data(), 0xAB, transfer_size, stream));
stream.synchronize();
for (auto _ : state) {
void* dst = host_mr->allocate(stream, transfer_size);
RAPIDSMPF_CUDA_TRY(
cudaMemcpyAsync(dst, src.data(), transfer_size, cudaMemcpyDefault, stream)
);
stream.synchronize();
state.PauseTiming();
host_mr->deallocate(stream, dst, transfer_size);
state.ResumeTiming();
}
state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(transfer_size));
state.SetLabel(
"memcpy device to host (incl. alloc): "
+ ResourceTypeStr[static_cast<std::size_t>(resource_type)]
);
}
template <typename T>
void bench_copy(
benchmark::State& state,
T& mr,
void const* src,
std::size_t size,
rmm::cuda_stream_view stream
) {
for (auto _ : state) {
state.PauseTiming();
void* dst = mr->allocate(stream, size);
stream.synchronize();
state.ResumeTiming();
RAPIDSMPF_CUDA_TRY(cudaMemcpyAsync(dst, src, size, cudaMemcpyDefault, stream));
stream.synchronize();
state.PauseTiming();
mr->deallocate(stream, dst, size);
state.ResumeTiming();
}
}
void BM_DeviceToHostCopy(benchmark::State& state) {
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
auto const transfer_size = static_cast<size_t>(state.range(0));
auto const resource_type = static_cast<ResourceType>(state.range(1));
if (resource_type == ResourceType::PINNED_MEMORY_RESOURCE
&& !rapidsmpf::is_pinned_memory_resources_supported())
{
state.SkipWithMessage("pinned memory not supported on system");
return;
}
auto host_mr = create_host_memory_resource(resource_type);
auto device_mr = std::make_unique<rmm::mr::cuda_memory_resource>();
auto src = rmm::device_buffer(transfer_size, stream, device_mr.get());
// Initialize src to avoid optimization removal
RAPIDSMPF_CUDA_TRY(cudaMemsetAsync(src.data(), 0xAB, transfer_size, stream));
bench_copy(state, host_mr, src.data(), transfer_size, stream);
state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(transfer_size));
state.SetLabel(
"memcpy device to host: "
+ ResourceTypeStr[static_cast<std::size_t>(resource_type)]
);
}
void BM_HostToDeviceCopy(benchmark::State& state) {
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
auto const transfer_size = static_cast<size_t>(state.range(0));
auto const resource_type = static_cast<ResourceType>(state.range(1));
if (resource_type == ResourceType::PINNED_MEMORY_RESOURCE
&& !rapidsmpf::is_pinned_memory_resources_supported())
{
state.SkipWithMessage("pinned memory not supported on system");
return;
}
auto host_mr = create_host_memory_resource(resource_type);
auto device_mr = std::make_unique<rmm::mr::cuda_memory_resource>();
// Allocate host memory and initialize
void* host_ptr = host_mr->allocate(stream, transfer_size);
memset(host_ptr, 0, transfer_size);
// Allocate device memory and copy from host
auto src = rmm::device_buffer(transfer_size, stream, device_mr.get());
bench_copy(state, host_mr, src.data(), transfer_size, stream);
state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(transfer_size));
state.SetLabel(
"memcpy host to device: "
+ ResourceTypeStr[static_cast<std::size_t>(resource_type)]
);
}
void BM_HostToHostCopy(benchmark::State& state) {
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
auto const transfer_size = static_cast<size_t>(state.range(0));
auto const resource_type = static_cast<ResourceType>(state.range(1));
if (resource_type == ResourceType::PINNED_MEMORY_RESOURCE
&& !rapidsmpf::is_pinned_memory_resources_supported())
{
state.SkipWithMessage("pinned memory not supported on system");
return;
}
auto host_mr = create_host_memory_resource(resource_type);
void* src = host_mr->allocate(stream, transfer_size);
// Initialize src to avoid optimization elimination
std::memset(src, 0xAB, transfer_size);
bench_copy(state, host_mr, src, transfer_size, stream);
state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(transfer_size));
state.SetLabel(
"memcpy host to host: " + ResourceTypeStr[static_cast<std::size_t>(resource_type)]
);
}
void BM_DeviceToDeviceCopy(benchmark::State& state) {
rmm::cuda_stream_view stream = rmm::cuda_stream_default;
auto const transfer_size = static_cast<size_t>(state.range(0));
// Device MR, independent of host resource type
auto device_mr = std::make_unique<rmm::mr::cuda_memory_resource>();
rmm::device_buffer src(transfer_size, stream, device_mr.get());
// Initialize src to avoid optimization removal
RAPIDSMPF_CUDA_TRY(cudaMemsetAsync(src.data(), 0xAB, transfer_size, stream));
bench_copy(state, device_mr, src.data(), transfer_size, stream);
state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(transfer_size));
state.SetLabel("memcpy device to device: rmm::mr::cuda_memory_resource");
}
// Custom argument generator for the benchmark
void CustomArguments(benchmark::internal::Benchmark* b) {
// Test different allocation sizes
for (auto size : {1 << 10, 500 << 10, 1 << 20, 500 << 20, 1 << 30}) {
// Test all memory resource types
for (auto resource_type : RESOURCE_TYPES) {
b->Args({size, resource_type});
}
}
}
// Register the benchmarks
BENCHMARK(BM_DeviceToHostCopyInclAlloc)
->Apply(CustomArguments)
->UseRealTime()
->Unit(benchmark::kMicrosecond);
BENCHMARK(BM_DeviceToHostCopy)
->Apply(CustomArguments)
->UseRealTime()
->Unit(benchmark::kMicrosecond);
BENCHMARK(BM_HostToDeviceCopy)
->Apply(CustomArguments)
->UseRealTime()
->Unit(benchmark::kMicrosecond);
BENCHMARK(BM_Allocate)
->Apply(CustomArguments)
->UseRealTime()
->Unit(benchmark::kMicrosecond);
BENCHMARK(BM_Deallocate)
->Apply(CustomArguments)
->UseRealTime()
->Unit(benchmark::kMicrosecond);
BENCHMARK(BM_HostToHostCopy)
->Apply(CustomArguments)
->UseRealTime()
->Unit(benchmark::kMicrosecond);
BENCHMARK(BM_DeviceToDeviceCopy)
->Apply(CustomArguments)
->UseRealTime()
->Unit(benchmark::kMicrosecond);
BENCHMARK_MAIN();