forked from alpaka-group/alpaka3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharedMem.cpp
More file actions
277 lines (229 loc) · 8.84 KB
/
sharedMem.cpp
File metadata and controls
277 lines (229 loc) · 8.84 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
/* Copyright 2024 René Widera
* SPDX-License-Identifier: MPL-2.0
*/
#include <alpaka/alpaka.hpp>
#include <alpakaTest/testMacros.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>
#include <type_traits>
using namespace alpaka;
using TestApis = std::decay_t<decltype(onHost::allBackends(onHost::enabledApis, exec::enabledExecutors))>;
template<uint32_t T_blockSize>
struct SharedBlockIotaKernel
{
template<typename T>
ALPAKA_FN_ACC void operator()(T const& acc, auto out, auto numBlocks) const
{
auto& shared = declareSharedVar<uint32_t[T_blockSize], uniqueId()>(acc);
for(auto blockIdx : onAcc::makeIdxMap(acc, onAcc::worker::blocksInGrid, IdxRange{numBlocks}))
{
auto const numDataElemInBlock = acc[frame::extent];
auto blockOffset = blockIdx * numDataElemInBlock;
for(auto inBlockOffset : onAcc::makeIdxMap(acc, onAcc::worker::threadsInBlock, onAcc::range::frameExtent))
{
uint32_t id = (T_blockSize - 1u - inBlockOffset).x();
shared[id] = id;
}
alpaka::onAcc::syncBlockThreads(acc);
for(auto inBlockOffset : onAcc::makeIdxMap(acc, onAcc::worker::threadsInBlock, onAcc::range::frameExtent))
{
out[blockOffset + inBlockOffset] = (blockOffset + shared[inBlockOffset.x()]).x();
}
}
}
};
TEMPLATE_LIST_TEST_CASE("block shared iota", "[sharedMem]", TestApis)
{
auto cfg = TestType::makeDict();
auto deviceSpec = cfg[object::deviceSpec];
auto exec = cfg[object::exec];
auto devSelector = onHost::makeDeviceSelector(deviceSpec);
if(!devSelector.isAvailable())
{
WARN("No device available for " << deviceSpec.getName());
return;
}
onHost::Device device = devSelector.makeDevice(0);
INFO(deviceSpec.getApi().getName() << " " << device.getName());
onHost::Queue queue = device.makeQueue();
constexpr Vec numBlocks = Vec{2u};
constexpr Vec blockExtent = Vec{128u};
constexpr Vec dataExtent = numBlocks * blockExtent;
INFO("block shared iota exec=" << onHost::demangledName(exec));
auto dBuff = onHost::alloc<uint32_t>(device, dataExtent);
auto hBuff = onHost::allocHostLike(dBuff);
alpaka::onHost::wait(queue);
queue.enqueue(
exec,
onHost::FrameSpec{numBlocks / 2u, blockExtent},
KernelBundle{SharedBlockIotaKernel<blockExtent.x()>{}, dBuff, numBlocks});
alpaka::onHost::memcpy(queue, hBuff, dBuff);
alpaka::onHost::wait(queue);
auto* ptr = onHost::data(hBuff);
for(uint32_t i = 0u; i < dataExtent; ++i)
{
CHECK(i == ptr[i]);
}
}
/** Validate shared memory aliasing and uniqueness.
*
* If a id during the shared memory declaration is used twice the same memory should be returned.
* Different id's should produce independant shared memory.
*/
struct SharedMemAlias
{
template<typename T>
ALPAKA_FN_ACC void operator()(T const& acc, auto result) const
{
bool test = true;
auto& s0 = declareSharedVar<uint32_t, uniqueId()>(acc);
auto& s1 = declareSharedVar<uint32_t, uniqueId()>(acc);
test = test && &s0 != &s1;
auto& s2 = declareSharedVar<uint32_t, 42>(acc);
auto& s3 = declareSharedVar<uint32_t, 42>(acc);
test = test && &s2 == &s3;
// check that we not create an alias to the first two variables
test = test && &s2 != &s0;
test = test && &s2 != &s1;
auto a0 = declareSharedMdArray<uint32_t, uniqueId()>(acc, CVec<uint32_t, 2u>{});
auto a1 = declareSharedMdArray<uint32_t, uniqueId()>(acc, CVec<uint32_t, 2u>{});
test = test && a0 != a1;
auto a2 = declareSharedMdArray<uint32_t, 42>(acc, CVec<uint32_t, 2u>{});
auto a3 = declareSharedMdArray<uint32_t, 42>(acc, CVec<uint32_t, 2u>{});
test = test && a2 == a3;
// check that we not create an alias to the first two arrays
test = test && a2 != a0;
test = test && a2 != a1;
// the address of a shared memory array and normal shared variable is not allowed to be equal even if the same
// id is used.
test = test && &s2 != &a2[0u];
result[0] = test;
}
};
struct DynSharedMemMember
{
uint32_t dynSharedMemBytes = 32u;
template<typename T>
ALPAKA_FN_ACC void operator()(T const& acc, auto result) const
{
bool test = true;
// set dynamic shared memory
auto* dynS0 = getDynSharedMem<uint32_t>(acc);
auto* dynS1 = getDynSharedMem<uint32_t>(acc);
test = test && dynS0 == dynS1;
result[0] = test;
}
};
struct DynSharedMemTrait
{
template<typename T>
ALPAKA_FN_ACC void operator()(T const& acc, auto result) const
{
bool test = true;
// set dynamic shared memory
auto* dynS0 = getDynSharedMem<uint32_t>(acc);
auto* dynS1 = getDynSharedMem<uint32_t>(acc);
test = test && dynS0 == dynS1;
result[0] = test;
}
};
namespace alpaka::onHost::trait
{
template<typename T_Spec>
struct BlockDynSharedMemBytes<DynSharedMemTrait, T_Spec>
{
BlockDynSharedMemBytes(DynSharedMemTrait const& kernel, T_Spec const& spec)
{
alpaka::unused(kernel, spec);
}
uint32_t operator()(auto const executor, auto const&... args) const
{
alpaka::unused(executor, args...);
return 32;
}
};
} // namespace alpaka::onHost::trait
TEMPLATE_LIST_TEST_CASE("block shared alias", "[SharedMem]", TestApis)
{
auto cfg = TestType::makeDict();
auto deviceSpec = cfg[object::deviceSpec];
auto exec = cfg[object::exec];
auto devSelector = onHost::makeDeviceSelector(deviceSpec);
if(!devSelector.isAvailable())
{
WARN("No device available for " << deviceSpec.getName());
return;
}
onHost::Device device = devSelector.makeDevice(0);
INFO(deviceSpec.getApi().getName() << " " << device.getName());
onHost::Queue queue = device.makeQueue();
constexpr Vec numBlocks = Vec{1u};
constexpr Vec blockExtent = Vec{1u};
auto dBuff = onHost::alloc<bool>(device, Vec{1u});
auto hBuff = onHost::allocHostLike(dBuff);
alpaka::onHost::wait(queue);
{
queue.enqueue(exec, onHost::FrameSpec{numBlocks, blockExtent}, KernelBundle{SharedMemAlias{}, dBuff});
alpaka::onHost::memcpy(queue, hBuff, dBuff);
alpaka::onHost::wait(queue);
CHECK(hBuff[0] == true);
}
{
queue.enqueue(exec, onHost::FrameSpec{numBlocks, blockExtent}, KernelBundle{DynSharedMemMember{}, dBuff});
alpaka::onHost::memcpy(queue, hBuff, dBuff);
alpaka::onHost::wait(queue);
CHECK(hBuff[0] == true);
}
{
queue.enqueue(exec, onHost::FrameSpec{numBlocks, blockExtent}, KernelBundle{DynSharedMemTrait{}, dBuff});
alpaka::onHost::memcpy(queue, hBuff, dBuff);
alpaka::onHost::wait(queue);
CHECK(hBuff[0] == true);
}
}
template<typename T_Idx>
struct IndexTypeKernel
{
constexpr void operator()(alpaka::onAcc::concepts::Acc auto const& acc, alpaka::concepts::IMdSpan auto result)
const
{
auto a = declareSharedMdArray<float, uniqueId()>(acc, CVec<T_Idx, 2u>{});
using SharedMdArrayType = decltype(a);
result[0u] = std::is_same_v<typename SharedMdArrayType::index_type, T_Idx>;
result[1u] = std::is_same_v<typename ALPAKA_TYPEOF(a.getExtents())::type, T_Idx>;
}
};
template<typename T_Idx>
void test_index_type(auto& queue, auto const& exec, auto name)
{
auto dBuff = onHost::alloc<bool>(queue.getDevice(), Vec{2u});
auto hBuff = onHost::allocHostLike(dBuff);
onHost::wait(queue);
queue.enqueue(
exec,
onHost::FrameSpec{alpaka::Vec{1u}, alpaka::Vec{1u}},
KernelBundle{IndexTypeKernel<T_Idx>{}, dBuff});
alpaka::onHost::memcpy(queue, hBuff, dBuff);
onHost::wait(queue);
REQUIRE_MESSAGE(hBuff[0] == true, "SharedMdArrayType::index_type failed with index type: " << name);
REQUIRE_MESSAGE(hBuff[1] == true, "getExtents()::type failed with index type: " << name);
}
TEMPLATE_LIST_TEST_CASE("test shared memory index type", "[sharedMem]", TestApis)
{
auto cfg = TestType::makeDict();
auto deviceSpec = cfg[object::deviceSpec];
auto exec = cfg[object::exec];
auto devSelector = onHost::makeDeviceSelector(deviceSpec);
if(!devSelector.isAvailable())
{
WARN("No device available for " << deviceSpec.getName());
return;
}
onHost::Device device = devSelector.makeDevice(0);
INFO(deviceSpec.getApi().getName() << " " << device.getName());
alpaka::onHost::Queue queue = device.makeQueue();
test_index_type<uint32_t>(queue, exec, "uint32_t");
test_index_type<int64_t>(queue, exec, "int64_t");
test_index_type<int>(queue, exec, "int");
test_index_type<size_t>(queue, exec, "size_t");
}