forked from alpaka-group/alpaka3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.cpp
More file actions
294 lines (250 loc) · 11.5 KB
/
transform.cpp
File metadata and controls
294 lines (250 loc) · 11.5 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
/* Copyright 2024 René Widera
* SPDX-License-Identifier: MPL-2.0
*/
#include <alpaka/alpaka.hpp>
#include <alpaka/meta/CartesianProduct.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>
#include <chrono>
#include <functional>
#include <iostream>
#include <type_traits>
using namespace alpaka;
using TestBackends = std::decay_t<decltype(onHost::allBackends(onHost::enabledApis, exec::enabledExecutors))>;
/** Stencil SIMD functor
*
* The stencil functor is getting a SimdPtr as input which allows to call the operator[] to change the index.
* In this test we do not shift the memory to avoid out of memory access.
* We cannot use generic lambdas with CUDA therefor we need to write a functor
*/
struct StencilAdd
{
constexpr auto operator()(concepts::SimdPtr auto const& a, concepts::SimdPtr auto const& b) const
{
return a.load() + b.load();
}
};
struct StencilAddWithAcc
{
constexpr auto operator()(
onAcc::concepts::Acc auto const&,
concepts::SimdPtr auto const& a,
concepts::SimdPtr auto const& b) const
{
return a.load() + b.load();
}
};
struct AddUpCastWithAcc
{
constexpr auto operator()(
onAcc::concepts::Acc auto const&,
concepts::Simd auto const& a,
concepts::Simd auto const& b) const
{
return pCast<size_t>(a + b);
}
};
struct ScalarOpWithAcc
{
constexpr auto operator()(onAcc::concepts::Acc auto const&, auto const& a, auto const& b) const
{
using ValueType = trait::GetValueType_t<ALPAKA_TYPEOF(a)>;
// The algorithm is using SIMD internally, forwarding single lanes to this functor can result in forwarding
// reference wrappers instead of values. By calling operator + we enforce casting to the value type.
return math::min(a + ValueType{1}, +b);
}
};
struct TestWithMdSpan
{
template<typename T_DataType>
static void executeTest(
concepts::Executor auto exec,
auto const& computeQueue,
auto const setup,
concepts::Vector auto extentMd)
{
std::cout << "run func : " << onHost::demangledName(std::get<1>(setup)) << std::endl;
auto computeDev = computeQueue.getDevice();
using DataType = T_DataType;
using OutDataType = decltype(std::get<1>(setup)(std::declval<DataType>(), std::declval<DataType>()));
onHost::SharedBuffer computeBufferOut = onHost::allocDeferred<OutDataType>(computeQueue, extentMd);
onHost::SharedBuffer computeBufferIn0 = onHost::allocDeferred<DataType>(computeQueue, extentMd);
onHost::SharedBuffer computeBufferIn1 = onHost::allocLikeDeferred(computeQueue, computeBufferIn0);
onHost::SharedBuffer hostBufferIota = onHost::allocLike(onHost::makeHostDevice(), computeBufferIn0);
onHost::SharedBuffer hostBufferOut = onHost::allocLike(onHost::makeHostDevice(), computeBufferOut);
// initialize with the linearized index
DataType iotaCounter = 0;
for(auto& value : hostBufferIota)
{
value = iotaCounter;
++iotaCounter;
}
onHost::memcpy(computeQueue, computeBufferIn0, hostBufferIota);
onHost::memcpy(computeQueue, computeBufferIn1, hostBufferIota);
onHost::wait(computeQueue);
auto const beginT = std::chrono::high_resolution_clock::now();
onHost::transform(
computeQueue,
exec,
computeBufferOut,
std::get<0>(setup),
computeBufferIn0,
computeBufferIn1);
onHost::wait(computeQueue);
auto const endT = std::chrono::high_resolution_clock::now();
std::cout << "Time for transform: " << std::chrono::duration<double>(endT - beginT).count() << 's'
<< " data size: " << computeBufferOut.getExtents() << std::endl;
onHost::memcpy(computeQueue, hostBufferOut, computeBufferOut);
onHost::wait(computeQueue);
// validate without using the forward iterator
DataType refIotaCounter = 0;
meta::ndLoopIncIdx(
extentMd,
[&](auto idx)
{
CHECK(hostBufferOut[idx] == std::get<1>(setup)(refIotaCounter, refIotaCounter));
++refIotaCounter;
});
};
};
template<typename T_DataType>
void prepareTest(auto cfg, concepts::Vector auto extentMd, auto const& setupTuple)
{
using DataType = T_DataType;
auto deviceSpec = cfg[object::deviceSpec];
alpaka::concepts::Executor auto exec = cfg[object::exec];
auto computeDevSelector = onHost::makeDeviceSelector(deviceSpec);
if(!computeDevSelector.isAvailable())
{
std::cout << "No device available for " << deviceSpec.getName() << std::endl;
return;
}
onHost::Device computeDev = computeDevSelector.makeDevice(0);
std::cout << "device spec: " << getName(deviceSpec) << std::endl;
std::cout << "device name: " << computeDev.getName() << std::endl;
std::cout << "executor : " << exec.getName() << std::endl;
onHost::Queue computeQueue = computeDev.makeQueue();
// execute for each functor
std::apply(
[&](auto const&... setup)
{ (std::get<2>(setup).template executeTest<DataType>(exec, computeQueue, setup, extentMd), ...); },
setupTuple);
}
TEMPLATE_LIST_TEST_CASE("transform", "", TestBackends)
{
auto cfg = TestType::makeDict();
using DataType = int;
// This list is not directly defined within the function `prepareTest()` due to nvcc compile issues.
auto functorList = std::make_tuple(
std::make_tuple(std::minus{}, std::minus{}, TestWithMdSpan{}),
std::make_tuple(std::plus{}, std::plus{}, TestWithMdSpan{}),
// we use variable.load() in the functor therefore we need to wrap the functor as StencilFunc
std::make_tuple(StencilFunc{StencilAdd{}}, std::plus{}, TestWithMdSpan{}),
std::make_tuple(StencilFunc{StencilAddWithAcc{}}, std::plus{}, TestWithMdSpan{}),
/* We can use a lambda function because the types are explicitly defined.
* Generic lambdas would not be supported for CUDA/HIP
* Wrapp the functor as ScalarFunc because math::min() cannot be executed on a Simd pack.
* This enforces that the functor is evaluated on scalar values and not SIMD packs.
* Memory loads and stores will be vectorized.
*/
std::make_tuple(
ScalarFunc{[] ALPAKA_FN_ACC(DataType const& a, DataType const& b)
{ return math::min(a + DataType{1}, b); }},
[](DataType const& a, DataType const& b) { return math::min(a + DataType{1}, b); },
TestWithMdSpan{}),
std::make_tuple(
ScalarFunc{ScalarOpWithAcc{}},
[](DataType const& a, DataType const& b) { return math::min(a + DataType{1}, b); },
TestWithMdSpan{}),
// different output type
std::make_tuple(
AddUpCastWithAcc{},
[](DataType const& a, DataType const& b) { return static_cast<size_t>(a + b); },
TestWithMdSpan{}));
// different extents for testing
auto extentMdList
= std::make_tuple(Vec{5, 7, 3, 11}, Vec{93, 7, 123}, Vec{5, 7, 4111}, Vec{5, 7, 3}, Vec{7, 3}, Vec{3});
std::apply([&](auto... extents) { (prepareTest<DataType>(cfg, extents, functorList), ...); }, extentMdList);
}
struct TestWithGenerator
{
template<typename T_DataType>
static void executeTest(
concepts::Executor auto exec,
auto const& computeQueue,
auto const setup,
concepts::Vector auto extentMd)
{
std::cout << "run func : " << onHost::demangledName(std::get<1>(setup)) << std::endl;
auto computeDev = computeQueue.getDevice();
using DataType = T_DataType;
using OutDataType = decltype(std::get<1>(setup)(std::declval<DataType>(), std::declval<DataType>()));
onHost::SharedBuffer computeBufferOut = onHost::allocDeferred<OutDataType>(computeQueue, extentMd);
onHost::SharedBuffer computeBufferIn0 = onHost::allocDeferred<DataType>(computeQueue, extentMd);
auto generator = LinearizedIdxGenerator{extentMd};
onHost::SharedBuffer hostBufferIota = onHost::allocLike(onHost::makeHostDevice(), computeBufferIn0);
onHost::SharedBuffer hostBufferOut = onHost::allocLike(onHost::makeHostDevice(), computeBufferOut);
// initialize with the linearized index
DataType iotaCounter = 0;
for(auto& value : hostBufferIota)
{
value = iotaCounter;
++iotaCounter;
}
onHost::memcpy(computeQueue, computeBufferIn0, hostBufferIota);
onHost::wait(computeQueue);
auto const beginT = std::chrono::high_resolution_clock::now();
onHost::transform(computeQueue, exec, computeBufferOut, std::get<0>(setup), computeBufferIn0, generator);
onHost::wait(computeQueue);
auto const endT = std::chrono::high_resolution_clock::now();
std::cout << "Time for transform: " << std::chrono::duration<double>(endT - beginT).count() << 's'
<< " data size: " << computeBufferOut.getExtents() << std::endl;
onHost::memcpy(computeQueue, hostBufferOut, computeBufferOut);
onHost::wait(computeQueue);
// validate without using the forward iterator
DataType refIotaCounter = 0;
meta::ndLoopIncIdx(
extentMd,
[&](auto idx)
{
CHECK(hostBufferOut[idx] == std::get<1>(setup)(refIotaCounter, generator[idx]));
++refIotaCounter;
});
};
};
TEMPLATE_LIST_TEST_CASE("transform generator", "", TestBackends)
{
auto cfg = TestType::makeDict();
using DataType = int;
// This list is not directly defined within the function `prepareTest()` due to nvcc compile issues.
auto functorList = std::make_tuple(
std::make_tuple(std::minus{}, std::minus{}, TestWithGenerator{}),
std::make_tuple(std::plus{}, std::plus{}, TestWithGenerator{}),
// we use variable.load() in the functor therefore we need to wrap the functor as StencilFunc
std::make_tuple(StencilFunc{StencilAdd{}}, std::plus{}, TestWithGenerator{}),
std::make_tuple(StencilFunc{StencilAddWithAcc{}}, std::plus{}, TestWithGenerator{}),
/* We can use a lambda function because the types are explicitly defined.
* Generic lambdas would not be supported for CUDA/HIP
* Wrapp the functor as ScalarFunc because math::min() cannot be executed on a Simd pack.
* This enforces that the functor is evaluated on scalar values and not SIMD packs.
* Memory loads and stores will be vectorized.
*/
std::make_tuple(
ScalarFunc{[] ALPAKA_FN_ACC(DataType const& a, DataType const& b)
{ return math::min(a + DataType{1}, b); }},
[](DataType const& a, DataType const& b) { return math::min(a + DataType{1}, b); },
TestWithGenerator{}),
std::make_tuple(
ScalarFunc{ScalarOpWithAcc{}},
[](DataType const& a, DataType const& b) { return math::min(a + DataType{1}, b); },
TestWithGenerator{}),
// different output type
std::make_tuple(
AddUpCastWithAcc{},
[](DataType const& a, DataType const& b) { return static_cast<size_t>(a + b); },
TestWithGenerator{}));
// different extents for testing
auto extentMdList
= std::make_tuple(Vec{5, 7, 3, 11}, Vec{93, 7, 123}, Vec{5, 7, 4111}, Vec{5, 7, 3}, Vec{7, 3}, Vec{3});
std::apply([&](auto... extents) { (prepareTest<DataType>(cfg, extents, functorList), ...); }, extentMdList);
}