forked from alpaka-group/alpaka3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifetime.cpp
More file actions
235 lines (196 loc) · 7.6 KB
/
lifetime.cpp
File metadata and controls
235 lines (196 loc) · 7.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
/* Copyright 2025 Simeon Ehrig
* SPDX-License-Identifier: MPL-2.0
*/
#include <alpaka/alpaka.hpp>
#include <catch2/catch_test_macros.hpp>
#include <vector>
using namespace alpaka;
TEST_CASE("move MdSpan", "[mem][mdspan][lifetime]")
{
constexpr size_t size = 10;
std::vector<int> data(size);
int* ptr = data.data();
concepts::Vector auto extents = Vec<uint32_t, 1>{}.fill(size);
concepts::Vector auto pitches = alpaka::calculatePitchesFromExtents<int>(extents);
using MutMdSpan = MdSpan<int, decltype(extents), decltype(pitches)>;
using ConstMdSpan = MdSpan<int const, decltype(extents), decltype(pitches)>;
// mdspan needs to be trivial copyable, otherwise it cannot be passed to the kernel
static_assert(std::is_trivially_copyable_v<MutMdSpan>);
static_assert(std::is_trivially_copyable_v<ConstMdSpan>);
MutMdSpan mdspan = MdSpan(ptr, extents, pitches);
REQUIRE(mdspan);
REQUIRE(mdspan.data() != nullptr);
[[maybe_unused]] ConstMdSpan other_mdspan = std::move(mdspan);
REQUIRE(mdspan);
REQUIRE(mdspan.data() != nullptr);
REQUIRE(other_mdspan);
REQUIRE(other_mdspan.data() != nullptr);
// because moving a mdspan does a copy, the original mdspan should be still valid after the move
REQUIRE(mdspan.data() == other_mdspan.data());
REQUIRE(mdspan.getExtents() == other_mdspan.getExtents());
REQUIRE(mdspan.getPitches() == other_mdspan.getPitches());
}
TEST_CASE("move View", "[mem][view][lifetime]")
{
constexpr size_t size = 10;
std::vector<int> data(size);
int* ptr = data.data();
concepts::Vector auto extents = Vec<uint32_t, 1>{}.fill(size);
concepts::Vector auto pitches = alpaka::calculatePitchesFromExtents<int>(extents);
using MutView = View<alpaka::api::Host, int, decltype(extents)>;
using ConstView = View<alpaka::api::Host, int const, decltype(extents)>;
MutView view(api::host, ptr, extents, pitches);
REQUIRE(view);
REQUIRE(view.data() != nullptr);
[[maybe_unused]] ConstView const_view = std::move(view);
REQUIRE(view);
REQUIRE(view.data() != nullptr);
REQUIRE(const_view);
REQUIRE(const_view.data() != nullptr);
// because moving a view does a copy, the original view should be still valid after the move
REQUIRE(view.data() == const_view.data());
REQUIRE(view.getExtents() == const_view.getExtents());
REQUIRE(view.getPitches() == const_view.getPitches());
}
TEST_CASE("copy SharedBuffer", "[mem][sharedBuffer][lifetime]")
{
onHost::SharedBuffer buffer = onHost::allocHost<int>(Vec<unsigned int, 1>{10});
REQUIRE(buffer.getUseCount() == 1);
onHost::SharedBuffer buffer2 = buffer;
REQUIRE(buffer.getUseCount() == 2);
REQUIRE(buffer);
REQUIRE(buffer2.getUseCount() == 2);
REQUIRE(buffer2);
}
TEST_CASE("move SharedBuffer", "[mem][sharedBuffer][lifetime]")
{
onHost::SharedBuffer buffer = onHost::allocHost<int>(Vec<unsigned int, 1>{10});
REQUIRE(buffer.getUseCount() == 1);
onHost::SharedBuffer buffer2 = std::move(buffer);
REQUIRE_FALSE(buffer);
REQUIRE(buffer2.getUseCount() == 1);
REQUIRE(buffer2);
onHost::SharedBuffer buffer3(std::move(buffer2));
REQUIRE_FALSE(buffer);
REQUIRE_FALSE(buffer2);
REQUIRE(buffer3.getUseCount() == 1);
REQUIRE(buffer3);
}
struct LivingMemory
{
// live counter:
// 1 -> alive
// 0 -> freed
// negative -> double free
int live_counter = 1;
};
TEST_CASE("lifetime of shared memory", "[mem][sharedBuffer][lifetime]")
{
concepts::Vector auto extents = Vec<uint32_t, 2>{}.fill(1);
concepts::Vector auto pitches = alpaka::calculatePitchesFromExtents<int>(extents);
LivingMemory lv_mem1;
auto lv_mem_deleter1 = [&lv_mem1] { lv_mem1.live_counter -= 1; };
{
onHost::SharedBuffer buffer(api::host, &lv_mem1, extents, pitches, lv_mem_deleter1);
REQUIRE(lv_mem1.live_counter == 1);
}
REQUIRE(lv_mem1.live_counter == 0);
LivingMemory lv_mem2;
auto lv_mem_deleter2 = [&lv_mem2] { lv_mem2.live_counter -= 1; };
onHost::SharedBuffer buffer2(api::host, &lv_mem2, extents, pitches, lv_mem_deleter2);
{
onHost::SharedBuffer buffer = buffer2;
REQUIRE(buffer2.getUseCount() == 2);
REQUIRE(lv_mem2.live_counter == 1);
}
REQUIRE(lv_mem2.live_counter == 1);
REQUIRE(buffer2.getUseCount() == 1);
LivingMemory lv_mem3;
auto lv_mem_deleter3 = [&lv_mem3] { lv_mem3.live_counter -= 1; };
onHost::SharedBuffer buffer3(api::host, &lv_mem3, extents, pitches, lv_mem_deleter3);
{
onHost::SharedBuffer buffer(std::move(buffer3));
REQUIRE(buffer.getUseCount() == 1);
REQUIRE(lv_mem3.live_counter == 1);
}
REQUIRE(lv_mem3.live_counter == 0);
REQUIRE_FALSE(buffer3);
}
void funcCopyByValue(auto buffer, long const expected_use_count)
{
REQUIRE(buffer);
REQUIRE(buffer.getUseCount() == expected_use_count);
}
void funcReference(auto& buffer, long const expected_use_count)
{
REQUIRE(buffer);
REQUIRE(buffer.getUseCount() == expected_use_count);
}
void funcConstReference(auto const& buffer, long const expected_use_count)
{
REQUIRE(buffer);
REQUIRE(buffer.getUseCount() == expected_use_count);
}
/** Takes the buffer as universal reference but do not assign to a lvalue.
* Therefore, the buffer is still valid after the function call.
*
* see: https://alagner.github.io/2024/03/14/Passing-arguments-by-move.html
*/
void funcUniversalRefBorrow(auto&& buffer, long const expected_use_count)
{
REQUIRE(buffer);
REQUIRE(buffer.getUseCount() == expected_use_count);
}
/** Takes the buffer as universal reference and assign it to a lvalue.
* Therefore, the buffer is not valid after the function call anymore.
*
*/
void funcUniversalRefMoved(auto&& buffer, long const expected_use_count)
{
REQUIRE(buffer);
auto tmp_buffer = std::move(buffer);
REQUIRE_FALSE(buffer);
REQUIRE(tmp_buffer);
REQUIRE(tmp_buffer.getUseCount() == expected_use_count);
}
TEST_CASE("pass shared memory to function", "[mem][sharedBuffer][lifetime]")
{
concepts::Vector auto extents = Vec<uint32_t, 2>{}.fill(1);
concepts::Vector auto pitches = alpaka::calculatePitchesFromExtents<int>(extents);
LivingMemory lv_mem1;
auto lv_mem_deleter1 = [&lv_mem1] { lv_mem1.live_counter -= 1; };
onHost::SharedBuffer buffer1(api::host, &lv_mem1, extents, pitches, lv_mem_deleter1);
REQUIRE(buffer1.getUseCount() == 1);
funcCopyByValue(buffer1, 2);
REQUIRE(buffer1);
REQUIRE(buffer1.getUseCount() == 1);
REQUIRE(lv_mem1.live_counter == 1);
REQUIRE(buffer1.getUseCount() == 1);
funcReference(buffer1, 1);
REQUIRE(buffer1);
REQUIRE(buffer1.getUseCount() == 1);
REQUIRE(lv_mem1.live_counter == 1);
REQUIRE(buffer1.getUseCount() == 1);
funcConstReference(buffer1, 1);
REQUIRE(buffer1);
REQUIRE(buffer1.getUseCount() == 1);
REQUIRE(lv_mem1.live_counter == 1);
REQUIRE(buffer1.getUseCount() == 1);
funcUniversalRefBorrow(buffer1, 1);
REQUIRE(buffer1);
REQUIRE(buffer1.getUseCount() == 1);
REQUIRE(lv_mem1.live_counter == 1);
REQUIRE(buffer1.getUseCount() == 1);
funcUniversalRefBorrow(std::move(buffer1), 1);
REQUIRE(buffer1);
REQUIRE(buffer1.getUseCount() == 1);
REQUIRE(lv_mem1.live_counter == 1);
REQUIRE(buffer1.getUseCount() == 1);
funcUniversalRefMoved(std::move(buffer1), 1);
REQUIRE_FALSE(buffer1);
REQUIRE(lv_mem1.live_counter == 0);
LivingMemory lv_mem2;
auto lv_mem_deleter2 = [&lv_mem2] { lv_mem2.live_counter -= 1; };
funcUniversalRefMoved(onHost::SharedBuffer{api::host, &lv_mem2, extents, pitches, lv_mem_deleter2}, 1);
REQUIRE(lv_mem2.live_counter == 0);
}