Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_VectorArithmatic.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16 /// \author Kyungjoo Kim ([email protected])
17
18 // Note: Luc Berger-Vergiat 04/14/21
19 // This test does not run on cuda or HIP
20 // backends so the whole file is guarded
21 // to ensure it is not included in these
22 // backends unit-test
23
24 #if !defined(TEST_CUDA_BATCHED_DENSE_CPP) && !defined(TEST_HIP_BATCHED_DENSE_CPP) && \
25 !defined(TEST_SYCL_BATCHED_DENSE_CPP) && !defined(TEST_OPENMPTARGET_BATCHED_DENSE_CPP)
26
27 #include "gtest/gtest.h"
28 #include "Kokkos_Core.hpp"
29 #include "Kokkos_Random.hpp"
30
31 #include "KokkosBatched_Vector.hpp"
32
33 #include "KokkosKernels_TestUtils.hpp"
34
35 using namespace KokkosBatched;
36
37 namespace Test {
38
39 template <typename VectorTagType, int VectorLength>
40 void impl_test_complex_real_imag_value() {
41 typedef Vector<VectorTagType, VectorLength> vector_type;
42
43 typedef typename vector_type::value_type value_type;
44 const int vector_length = vector_type::vector_length;
45
46 vector_type a;
47
48 for (int k = 0; k < vector_length; ++k) {
49 a[k].real() = k * 3 + 1;
50 a[k].imag() = k * 5 + 4;
51 }
52
53 const auto a_real = Kokkos::ArithTraits<vector_type>::real(a);
54 const auto a_imag = Kokkos::ArithTraits<vector_type>::imag(a);
55
56 typedef Kokkos::ArithTraits<value_type> ats;
57 const typename ats::mag_type eps = 1.0e3 * ats::epsilon();
58 for (int k = 0; k < vector_length; ++k) {
59 EXPECT_NEAR(a[k].real(), a_real[k], eps);
60 EXPECT_NEAR(a[k].imag(), a_imag[k], eps);
61 }
62 }
63
64 template <typename VectorTagType, int VectorLength>
65 void impl_test_batched_vector_arithmatic() {
66 /// random data initialization
67 typedef Vector<VectorTagType, VectorLength> vector_type;
68
69 typedef typename vector_type::value_type value_type;
70 const int vector_length = vector_type::vector_length;
71
72 typedef Kokkos::ArithTraits<value_type> ats;
73 typedef typename ats::mag_type mag_type;
74
75 vector_type a, b, c;
76 value_type alpha;
77 mag_type beta;
78 const value_type zero(0);
79
80 Random<value_type> a_random;
81 Random<mag_type> b_random;
82 for (int iter = 0; iter < 100; ++iter) {
83 for (int k = 0; k < vector_length; ++k) {
84 a[k] = a_random.value();
85 b[k] = a_random.value();
86 c[k] = zero;
87 }
88 alpha = a_random.value();
89 beta = b_random.value();
90
91 const mag_type eps = 1.0e3 * ats::epsilon();
92
93 {
94 /// test : vec + vec
95 c = a + b;
96 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(a[k] + b[k]), eps * ats::abs(c[k]));
97
98 /// test : value + vec
99 c = alpha + b;
100 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(alpha + b[k]), eps * ats::abs(c[k]));
101
102 /// test : vec + value
103 c = b + alpha;
104 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(b[k] + alpha), eps * ats::abs(c[k]));
105
106 /// test : vec + mag
107 c = a + beta;
108 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(a[k] + beta), eps * ats::abs(c[k]));
109
110 /// test : mag + vec
111 c = beta + a;
112 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(beta + a[k]), eps * ats::abs(c[k]));
113 }
114 {
115 /// test : vec - vec
116 c = a - b;
117 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(a[k] - b[k]), eps * ats::abs(c[k]));
118
119 /// test : value - vec
120 c = alpha - b;
121 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(alpha - b[k]), eps * ats::abs(c[k]));
122
123 /// test : vec + value
124 c = b - alpha;
125 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(b[k] - alpha), eps * ats::abs(c[k]));
126
127 /// test : vec - mag
128 c = a - beta;
129 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(a[k] - beta), eps * ats::abs(c[k]));
130
131 /// test : mag - vec
132 c = beta - a;
133 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(beta - a[k]), eps * ats::abs(c[k]));
134 }
135 {
136 /// test : vec * vec
137 c = a * b;
138 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(a[k] * b[k]), eps * ats::abs(c[k]));
139
140 /// test : value * vec
141 c = alpha * b;
142 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(alpha * b[k]), eps * ats::abs(c[k]));
143
144 /// test : vec + value
145 c = b * alpha;
146 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(b[k] * alpha), eps * ats::abs(c[k]));
147
148 /// test : vec * mag
149 c = a * beta;
150 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(a[k] * beta), eps * ats::abs(c[k]));
151
152 /// test : mag * vec
153 c = beta * a;
154 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(beta * a[k]), eps * ats::abs(c[k]));
155 }
156 {
157 /// test : vec / vec
158 c = a / b;
159 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(a[k] / b[k]), eps * ats::abs(c[k]));
160
161 /// test : value / vec
162 c = alpha / b;
163 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(alpha / b[k]), eps * ats::abs(c[k]));
164
165 /// test : vec / value
166 c = b / alpha;
167 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(b[k] / alpha), eps * ats::abs(c[k]));
168
169 /// test : mag / vec
170 c = beta / a;
171 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(beta / a[k]), eps * ats::abs(c[k]));
172
173 /// test : vec / value
174 c = a / beta;
175 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(a[k] / beta), eps * ats::abs(c[k]));
176 }
177 {
178 /// test : vec -vec
179 c = -a;
180 for (int k = 0; k < vector_length; ++k) EXPECT_NEAR(ats::abs(c[k]), ats::abs(-a[k]), eps * ats::abs(c[k]));
181 }
182 #if defined(__DO_NOT_TEST__)
183 {
184 /// test : add radial
185 const mag_type tiny = 1.0;
186
187 c = vector_type(0);
188 c += -vector_type(tiny) * vector_type(a < 0);
189 c += vector_type(tiny) * vector_type(a >= 0);
190
191 for (int k = 0; k < vector_length; ++k)
192 EXPECT_NEAR(ats::abs(c[k]), ats::abs(a[k] < 0 ? -tiny : tiny), eps * ats::abs(c[k]));
193 }
194 #endif
195 }
196 }
197 } // namespace Test
198
199 template <typename DeviceType, typename VectorTagType, int VectorLength>
200 int test_batched_vector_arithmatic() {
201 static_assert(Kokkos::SpaceAccessibility<DeviceType, Kokkos::HostSpace>::accessible,
202 "vector datatype is only tested on host space");
203 Test::impl_test_batched_vector_arithmatic<VectorTagType, VectorLength>();
204
205 return 0;
206 }
207 template <typename DeviceType, typename VectorTagType, int VectorLength>
208 int test_batched_complex_real_imag_value() {
209 static_assert(Kokkos::SpaceAccessibility<DeviceType, Kokkos::HostSpace>::accessible,
210 "vector datatype is only tested on host space");
211 Test::impl_test_complex_real_imag_value<VectorTagType, VectorLength>();
212
213 return 0;
214 }
215
216 ///
217 /// SIMD
218 ///
219
220 #if defined(KOKKOSKERNELS_INST_FLOAT)
221 TEST_F(TestCategory, batched_vector_arithmatic_simd_float3) {
222 test_batched_vector_arithmatic<TestDevice, SIMD<float>, 3>();
223 }
224 TEST_F(TestCategory, batched_vector_arithmatic_simd_float4) {
225 test_batched_vector_arithmatic<TestDevice, SIMD<float>, 4>();
226 }
227 // avx
228 TEST_F(TestCategory, batched_vector_arithmatic_simd_float8) {
229 test_batched_vector_arithmatic<TestDevice, SIMD<float>, 8>();
230 }
231 // avx 512
232 TEST_F(TestCategory, batched_vector_arithmatic_simd_float16) {
233 test_batched_vector_arithmatic<TestDevice, SIMD<float>, 16>();
234 }
235 #endif
236
237 #if defined(KOKKOSKERNELS_INST_DOUBLE)
238 TEST_F(TestCategory, batched_vector_arithmatic_simd_double3) {
239 test_batched_vector_arithmatic<TestDevice, SIMD<double>, 3>();
240 }
241 // avx
242 TEST_F(TestCategory, batched_vector_arithmatic_simd_double4) {
243 test_batched_vector_arithmatic<TestDevice, SIMD<double>, 4>();
244 }
245 // avx 512
246 TEST_F(TestCategory, batched_vector_arithmatic_simd_double8) {
247 test_batched_vector_arithmatic<TestDevice, SIMD<double>, 8>();
248 }
249 #endif
250
251 #define __DO_NOT_TEST__
252 #if defined(KOKKOSKERNELS_INST_COMPLEX_FLOAT)
253 TEST_F(TestCategory, batched_vector_arithmatic_simd_scomplex3) {
254 test_batched_vector_arithmatic<TestDevice, SIMD<Kokkos::complex<float> >, 3>();
255 }
256 // avx
257 TEST_F(TestCategory, batched_vector_arithmatic_simd_scomplex4) {
258 test_batched_vector_arithmatic<TestDevice, SIMD<Kokkos::complex<float> >, 4>();
259 }
260 // avx 512
261 TEST_F(TestCategory, batched_vector_arithmatic_simd_scomplex8) {
262 test_batched_vector_arithmatic<TestDevice, SIMD<Kokkos::complex<float> >, 8>();
263 }
264
265 TEST_F(TestCategory, batched_vector_scomplex_real_imag_value3) {
266 test_batched_complex_real_imag_value<TestDevice, SIMD<Kokkos::complex<float> >, 3>();
267 }
268 // avx
269 TEST_F(TestCategory, batched_vector_scomplex_real_imag_value2) {
270 test_batched_complex_real_imag_value<TestDevice, SIMD<Kokkos::complex<float> >, 2>();
271 }
272 // avx 512
273 TEST_F(TestCategory, batched_vector_scomplex_real_imag_value4) {
274 test_batched_complex_real_imag_value<TestDevice, SIMD<Kokkos::complex<float> >, 4>();
275 }
276 #endif
277
278 #if defined(KOKKOSKERNELS_INST_COMPLEX_DOUBLE)
279 TEST_F(TestCategory, batched_vector_arithmatic_simd_dcomplex3) {
280 test_batched_vector_arithmatic<TestDevice, SIMD<Kokkos::complex<double> >, 3>();
281 }
282 // avx
283 TEST_F(TestCategory, batched_vector_arithmatic_simd_dcomplex2) {
284 test_batched_vector_arithmatic<TestDevice, SIMD<Kokkos::complex<double> >, 2>();
285 }
286 // avx 512
287 TEST_F(TestCategory, batched_vector_arithmatic_simd_dcomplex4) {
288 test_batched_vector_arithmatic<TestDevice, SIMD<Kokkos::complex<double> >, 4>();
289 }
290
291 TEST_F(TestCategory, batched_vector_dcomplex_real_imag_value3) {
292 test_batched_complex_real_imag_value<TestDevice, SIMD<Kokkos::complex<double> >, 3>();
293 }
294 // avx
295 TEST_F(TestCategory, batched_vector_dcomplex_real_imag_value2) {
296 test_batched_complex_real_imag_value<TestDevice, SIMD<Kokkos::complex<double> >, 2>();
297 }
298 // avx 512
299 TEST_F(TestCategory, batched_vector_dcomplex_real_imag_value4) {
300 test_batched_complex_real_imag_value<TestDevice, SIMD<Kokkos::complex<double> >, 4>();
301 }
302 #endif
303 #undef __DO_NOT_TEST__
304
305 #endif // check to not include this in a device test