forked from alpaka-group/alpaka3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimd.cpp
More file actions
197 lines (160 loc) · 7.07 KB
/
simd.cpp
File metadata and controls
197 lines (160 loc) · 7.07 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
/* Copyright 2024 René Widera
* SPDX-License-Identifier: MPL-2.0
*/
#include <alpaka/alpaka.hpp>
#include <catch2/catch_test_macros.hpp>
#include <cstdint>
#include <iostream>
#include <string>
#include <tuple>
/** @file
*
* This file is testing simd functionality.
* We do not use constexpr because depending on the implementation the underlying native types are not constexpr.
*/
/** define one dimensional simd vector run-time test cases for operator +,-,*,/ */
TEST_CASE("simd 1D", "[simd vector]")
{
using namespace alpaka;
auto simd = Simd{3};
CHECK(simd.width() == 1);
CHECK(simd.x() == 3);
CHECK((simd == Simd{3}).reduce(std::logical_and{}));
auto typeLambda = [](auto const typeDummy)
{
using type = std::decay_t<decltype(typeDummy)>;
auto inputData = std::make_tuple(
std::make_tuple(std::plus{}, Simd(type{3}), Simd(type{7}), Simd(type{10})),
std::make_tuple(std::plus{}, Simd(type{3}), type{7}, Simd(type{10})),
std::make_tuple(std::plus{}, type{3}, Simd(type{7}), Simd(type{10})),
std::make_tuple(std::minus{}, Simd(type{17}), Simd(type{7}), Simd(type{10})),
std::make_tuple(std::minus{}, Simd(type{17}), type{7}, Simd(type{10})),
std::make_tuple(std::minus{}, type{17}, Simd(type{7}), Simd(type{10})),
std::make_tuple(std::multiplies{}, Simd(type{3}), Simd(type{7}), Simd(type{21})),
std::make_tuple(std::multiplies{}, Simd(type{3}), type{7}, Simd(type{21})),
std::make_tuple(std::multiplies{}, type{3}, Simd(type{7}), Simd(type{21})),
std::make_tuple(std::divides{}, Simd(type{21}), Simd(type{7}), Simd(type{3})),
std::make_tuple(std::divides{}, Simd(type{21}), type{7}, Simd(type{3})),
std::make_tuple(std::divides{}, type{21}, Simd(type{7}), Simd(type{3})));
bool x = std::apply(
[&](auto... args)
{
return (
(std::get<0>(args)(std::get<1>(args), std::get<2>(args)) == std::get<3>(args))
.reduce(std::logical_and{})
&& ...);
},
inputData);
return x;
};
auto inputTypes = std::tuple<int, uint32_t, uint64_t, float, double>{};
bool x = std::apply([&](auto... args) { return (typeLambda(args) && ...); }, inputTypes);
CHECK(x);
}
/** define two dimensional simd vector run-time test cases for operator +,-,*,/ */
TEST_CASE("simd 2D", "[simd vector]")
{
using namespace alpaka;
auto simd = Simd{3, 7};
CHECK(simd.width() == 2);
CHECK((simd.y() == 3 && simd.x() == 7));
CHECK((simd == Simd{3, 7}).reduce(std::logical_and{}));
CHECK((simd != Simd{7, 3}).reduce(std::logical_and{}));
CHECK((Simd{7} == Simd{7, 3}.eraseBack()).reduce(std::logical_and{}));
CHECK((Simd{3} == Simd{7, 3}.rshrink<1u>()).reduce(std::logical_and{}));
CHECK((Simd{3} == Simd{7, 3}.rshrink<1u>(1u)).reduce(std::logical_and{}));
CHECK((Simd{7} == Simd{7, 3}.rshrink<1u>(0u)).reduce(std::logical_and{}));
CHECK((Simd{7} == Simd{7, 3}.remove<1u>()).reduce(std::logical_and{}));
CHECK((Simd{3} == Simd{7, 3}.remove<0u>()).reduce(std::logical_and{}));
auto ssimd = Simd<ALPAKA_TYPEOF(simd), 4>{simd, simd, simd, simd};
auto reduceResult = ssimd.reduce(std::plus{});
CHECK((reduceResult == Simd{12, 28}).reduce(std::logical_and{}));
auto typeLambda = [](auto const typeDummy)
{
using type = std::decay_t<decltype(typeDummy)>;
auto inputData = std::make_tuple(
std::make_tuple(std::plus{}, Simd(type{3}, type{7}), Simd(type{7}, type{9}), Simd(type{10}, type{16})),
std::make_tuple(std::plus{}, Simd(type{3}, type{9}), type{7}, Simd(type{10}, type{16})),
std::make_tuple(std::plus{}, type{3}, Simd(type{7}, type{9}), Simd(type{10}, type{12})),
std::make_tuple(std::minus{}, Simd(type{17}, type{7}), Simd(type{7}, type{3}), Simd(type{10}, type{4})),
std::make_tuple(std::minus{}, Simd(type{17}, type{7}), type{7}, Simd(type{10}, type{0})),
std::make_tuple(std::minus{}, type{17}, Simd(type{7}, type{3}), Simd(type{10}, type{14})),
std::make_tuple(
std::multiplies{},
Simd(type{3}, type{7}),
Simd(type{7}, type{11}),
Simd(type{21}, type{77})),
std::make_tuple(std::multiplies{}, Simd(type{3}, type{7}), type{7}, Simd(type{21}, type{49})),
std::make_tuple(std::multiplies{}, type{3}, Simd(type{7}, type{3}), Simd(type{21}, type{9})),
std::make_tuple(std::divides{}, Simd(type{21}, type{3}), Simd(type{7}, type{3}), Simd(type{3}, type{1})),
std::make_tuple(std::divides{}, Simd(type{21}, type{14}), type{7}, Simd(type{3}, type{2})),
std::make_tuple(std::divides{}, type{21}, Simd(type{7}, type{3}), Simd(type{3}, type{7})));
bool x = std::apply(
[&](auto... args)
{
return (
(std::get<0>(args)(std::get<1>(args), std::get<2>(args)) == std::get<3>(args))
.reduce(std::logical_and{})
&& ...);
},
inputData);
return x;
};
auto inputTypes = std::tuple<int, uint32_t, uint64_t, float, double>{};
bool x = std::apply([&](auto... args) { return (typeLambda(args) && ...); }, inputTypes);
CHECK(x);
}
/** define two dimensional simd vector run-time test cases for operator >,>=,<,<= */
TEST_CASE("simd 3D", "[simd vector]")
{
using namespace alpaka;
auto typeLambda = [](auto const typeDummy)
{
using type = std::decay_t<decltype(typeDummy)>;
auto inputData = std::make_tuple(
std::make_tuple(
std::greater{},
Simd(type{3}, type{7}),
Simd(type{7}, type{9}),
makeSimdMask<type>(false, false))
);
bool x = std::apply(
[&](auto... args)
{
return (
(std::get<0>(args)(std::get<1>(args), std::get<2>(args)) == std::get<3>(args))
.reduce(std::logical_and{})
&& ...);
},
inputData);
return x;
};
auto inputTypes = std::tuple<int, uint32_t, uint64_t, float, double>{};
bool x = std::apply([&](auto... args) { return (typeLambda(args) && ...); }, inputTypes);
CHECK(x);
}
struct MinValue
{
auto operator()(auto const& a, auto const& b) const
{
return a < b ? a : b;
}
};
TEST_CASE("simd reduce", "[simd vector]")
{
using namespace alpaka;
auto s1 = Simd{3, 7, 4};
CHECK(14 == s1.reduce(std::plus{}));
CHECK(14 == s1.sum());
auto s2 = Simd{1, 2, 3, 4, 5};
CHECK(15 == s2.reduce(std::plus{}));
CHECK(15 == s2.sum());
auto s3 = Simd{1, 2, 3, 4, 5};
CHECK(120 == s3.reduce(std::multiplies{}));
CHECK(120 == s3.product());
// check user provided functor
auto s4 = Simd{1, 2, 3, 4, 5};
CHECK(1 == s4.reduce(MinValue{}));
auto s5 = Simd{1, 2, 3, 4, 5, -10};
CHECK(-10 == s5.reduce(MinValue{}));
}