Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_VectorMath.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_batched_vector_math() {
41 /// random data initialization
42 typedef Vector<VectorTagType, VectorLength> vector_type;
43
44 typedef typename vector_type::value_type value_type;
45 const int vector_length = vector_type::vector_length;
46
47 typedef Kokkos::ArithTraits<value_type> ats;
48 typedef typename ats::mag_type mag_type;
49
50 vector_type a, b, aref, bref;
51 const value_type one(1.0);
52 const value_type half(0.5);
53 const value_type maxmin(1.0e-7);
54 const mag_type eps = 1.0e3 * ats::epsilon();
55
56 Random<value_type> random;
57 for (int iter = 0; iter < 100; ++iter) {
58 for (int k = 0; k < vector_length; ++k) {
59 const auto aval = (random.value() + half);
60 const auto bval = (random.value() + half);
61
62 aref[k] = max(min(aval, one), maxmin);
63 bref[k] = max(min(bval, one), maxmin);
64 }
65
66 {
67 #undef CHECK
68 #define CHECK(op) \
69 { \
70 a = op(aref); \
71 for (int i = 0; i < vector_length; ++i) EXPECT_NEAR_KK(a[i], ats::op(aref[i]), eps* a[i]); \
72 }
73
74 CHECK(sqrt);
75 CHECK(cbrt);
76 CHECK(log);
77 CHECK(exp);
78 CHECK(sin);
79 CHECK(cos);
80 CHECK(tan);
81 CHECK(sinh);
82 CHECK(cosh);
83 CHECK(tanh);
84 CHECK(asin);
85 CHECK(acos);
86 CHECK(atan);
87
88 #undef CHECK
89 #define CHECK \
90 { \
91 a = pow(aref, bref); \
92 for (int i = 0; i < vector_length; ++i) EXPECT_NEAR_KK(a[i], ats::pow(aref[i], bref[i]), eps* a[i]); \
93 } \
94 CHECK;
95
96 #undef CHECK
97 #define CHECK(op) \
98 { \
99 mag_type beta = mag_type(3.2); \
100 a = op(aref, beta); \
101 for (int i = 0; i < vector_length; ++i) EXPECT_NEAR_KK(a[i], ats::op(aref[i], beta), eps* a[i]); \
102 }
103
104 CHECK(pow);
105
106 #undef CHECK
107 #define CHECK(op) \
108 { \
109 value_type alpha = random.value() + 2.0; \
110 a = op(alpha, bref); \
111 for (int i = 0; i < vector_length; ++i) EXPECT_NEAR_KK(a[i], ats::op(alpha, bref[i]), eps* a[i]); \
112 }
113
114 CHECK(pow);
115 #undef CHECK
116 } // end test body
117 } // end for
118 } // impl
119 } // namespace Test
120
121 template <typename DeviceType, typename VectorTagType, int VectorLength>
122 int test_batched_vector_math() {
123 static_assert(Kokkos::SpaceAccessibility<DeviceType, Kokkos::HostSpace>::accessible,
124 "vector datatype is only tested on host space");
125 Test::impl_test_batched_vector_math<VectorTagType, VectorLength>();
126
127 return 0;
128 }
129
130 // template<typename ValueType>
131 // int test_complex_pow() {
132 // typedef Kokkos::ArithTraits<Kokkos::complex<ValueType> > ats;
133 // typedef typename ats::mag_type mag_type;
134
135 // const mag_type eps = 1.0e3 * ats::epsilon();
136
137 // mag_type a2 = 4.5;
138 // Kokkos::complex<mag_type> a0(3.2, -1.4), a1(1.2, 2.3);
139 // std::complex<mag_type> b0(3.2, -1.4), b1(1.2, 2.3);
140
141 // Test::EXPECT_NEAR_KK( ats::pow(a0,a1), std::pow(b0,b1), eps );
142 // Test::EXPECT_NEAR_KK( ats::pow(a0,a2), std::pow(b0,a2), eps );
143
144 // return 0;
145 // }
146
147 ///
148 /// SIMD
149 ///
150
151 #if defined(KOKKOSKERNELS_INST_FLOAT)
152 TEST_F(TestCategory, batched_vector_math_simd_float3) { test_batched_vector_math<TestDevice, SIMD<float>, 3>(); }
153 TEST_F(TestCategory, batched_vector_math_simd_float8) { test_batched_vector_math<TestDevice, SIMD<float>, 8>(); }
154 #endif
155
156 #if defined(KOKKOSKERNELS_INST_DOUBLE)
157 TEST_F(TestCategory, batched_vector_math_simd_double3) { test_batched_vector_math<TestDevice, SIMD<double>, 3>(); }
158 TEST_F(TestCategory, batched_vector_math_simd_double4) { test_batched_vector_math<TestDevice, SIMD<double>, 4>(); }
159 #endif
160
161 // using namespace Test;
162
163 // #if defined(KOKKOSKERNELS_INST_COMPLEX_FLOAT)
164 // TEST_F( TestCategory, batched_vector_math_simd_scomplex3 ) {
165 // test_complex_pow<float>();
166 // test_batched_vector_math<TestDevice,SIMD<Kokkos::complex<float> >,3>();
167 // }
168 // TEST_F( TestCategory, batched_vector_math_simd_scomplex4 ) {
169 // test_batched_vector_math<TestDevice,SIMD<Kokkos::complex<float> >,4>();
170 // }
171 // #endif
172
173 // #if defined(KOKKOSKERNELS_INST_COMPLEX_DOUBLE)
174 // TEST_F( TestCategory, batched_vector_math_simd_dcomplex3 ) {
175 // test_complex_pow<double>();
176 // test_batched_vector_math<TestDevice,SIMD<Kokkos::complex<double> >,3>();
177 // }
178 // TEST_F( TestCategory, batched_vector_math_simd_dcomplex2 ) {
179 // test_batched_vector_math<TestDevice,SIMD<Kokkos::complex<double> >,2>();
180 // }
181 // #endif
182
183 #endif // check to not include this in a device test