Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_VectorMisc.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_misc() {
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, c;
51 const mag_type eps = 1.0e3 * ats::epsilon();
52
53 Random<value_type> random;
54 for (int iter = 0; iter < 100; ++iter) {
55 for (int k = 0; k < vector_length; ++k) {
56 a[k] = random.value();
57 b[k] = random.value();
58 }
59
60 {
61 c = conditional_assign(a < b, a, b);
62 for (int i = 0; i < vector_length; ++i) {
63 const auto cc = a[i] < b[i] ? a[i] : b[i];
64 EXPECT_NEAR_KK(c[i], cc, eps * c[i]);
65 }
66
67 c = 0;
68 conditional_assign(c, a < b, a, b);
69 for (int i = 0; i < vector_length; ++i) {
70 const auto cc = a[i] < b[i] ? a[i] : b[i];
71 EXPECT_NEAR_KK(c[i], cc, eps * c[i]);
72 }
73 }
74 {
75 c = conditional_assign(a < b, a, value_type(0));
76 for (int i = 0; i < vector_length; ++i) {
77 const auto cc = a[i] < b[i] ? a[i] : 0;
78 EXPECT_NEAR_KK(c[i], cc, eps * c[i]);
79 }
80
81 c = 0;
82 conditional_assign(c, a < b, a, value_type(0));
83 for (int i = 0; i < vector_length; ++i) {
84 const auto cc = a[i] < b[i] ? a[i] : 0;
85 EXPECT_NEAR_KK(c[i], cc, eps * c[i]);
86 }
87 }
88 {
89 c = conditional_assign(a < b, value_type(0), b);
90 for (int i = 0; i < vector_length; ++i) {
91 const auto cc = a[i] < b[i] ? 0 : b[i];
92 EXPECT_NEAR_KK(c[i], cc, eps * c[i]);
93 }
94
95 c = 0;
96 conditional_assign(c, a < b, value_type(0), b);
97 for (int i = 0; i < vector_length; ++i) {
98 const auto cc = a[i] < b[i] ? 0 : b[i];
99 EXPECT_NEAR_KK(c[i], cc, eps * c[i]);
100 }
101 }
102
103 {
104 typedef Vector<SIMD<bool>, VectorLength> vector_bool_type;
105 vector_bool_type cond_all_true, cond_all_false, cond_alternate;
106
107 for (int i = 0; i < vector_length; ++i) {
108 cond_all_true[i] = true;
109 cond_all_false[i] = false;
110 cond_alternate[i] = i % 2;
111 }
112 bool all_true, any_true;
113
114 all_true = true;
115 any_true = false;
116 for (int i = 0; i < vector_length; ++i) {
117 all_true &= cond_all_true[i];
118 any_true |= cond_all_true[i];
119 }
120 EXPECT_EQ(all_true, true);
121 EXPECT_EQ(any_true, true);
122
123 all_true = true;
124 any_true = false;
125 for (int i = 0; i < vector_length; ++i) {
126 all_true &= cond_all_false[i];
127 any_true |= cond_all_false[i];
128 }
129 EXPECT_EQ(all_true, false);
130 EXPECT_EQ(any_true, false);
131
132 all_true = true;
133 any_true = false;
134 for (int i = 0; i < vector_length; ++i) {
135 all_true &= cond_alternate[i];
136 any_true |= cond_alternate[i];
137 }
138 EXPECT_EQ(all_true, false);
139 EXPECT_EQ(any_true, true);
140 }
141 {
142 value_type min_a = a[0], max_a = a[0], sum_a = 0, prod_a = 1;
143 for (int i = 0; i < vector_length; ++i) {
144 min_a = min(min_a, a[i]);
145 max_a = max(max_a, a[i]);
146 sum_a += a[i];
147 prod_a *= a[i];
148 }
149 EXPECT_NEAR_KK(min_a, min(a), eps * min_a);
150 EXPECT_NEAR_KK(max_a, max(a), eps * max_a);
151 EXPECT_NEAR_KK(sum_a, sum(a), eps * sum_a);
152 EXPECT_NEAR_KK(prod_a, prod(a), eps * prod_a);
153 } // end test body
154 } // end for
155 } // impl
156 } // namespace Test
157
158 template <typename DeviceType, typename VectorTagType, int VectorLength>
159 int test_batched_vector_misc() {
160 static_assert(Kokkos::SpaceAccessibility<DeviceType, Kokkos::HostSpace>::accessible,
161 "vector datatype is only tested on host space");
162 Test::impl_test_batched_vector_misc<VectorTagType, VectorLength>();
163
164 return 0;
165 }
166
167 ///
168 /// SIMD
169 ///
170
171 #if defined(KOKKOSKERNELS_INST_FLOAT)
172 TEST_F(TestCategory, batched_vector_misc_simd_float3) { test_batched_vector_misc<TestDevice, SIMD<float>, 3>(); }
173 TEST_F(TestCategory, batched_vector_misc_simd_float8) { test_batched_vector_misc<TestDevice, SIMD<float>, 8>(); }
174 #endif
175
176 #if defined(KOKKOSKERNELS_INST_DOUBLE)
177 TEST_F(TestCategory, batched_vector_misc_simd_double3) { test_batched_vector_misc<TestDevice, SIMD<double>, 3>(); }
178 TEST_F(TestCategory, batched_vector_misc_simd_double4) { test_batched_vector_misc<TestDevice, SIMD<double>, 4>(); }
179 #endif
180
181 // #if defined(KOKKOSKERNELS_INST_COMPLEX_FLOAT)
182 // TEST_F( TestCategory, batched_vector_misc_simd_scomplex3 ) {
183 // test_batched_vector_misc<TestDevice,SIMD<Kokkos::complex<float> >,3>();
184 // }
185 // TEST_F( TestCategory, batched_vector_misc_simd_scomplex4 ) {
186 // test_batched_vector_misc<TestDevice,SIMD<Kokkos::complex<float> >,4>();
187 // }
188 // #endif
189
190 // #if defined(KOKKOSKERNELS_INST_COMPLEX_DOUBLE)
191 // TEST_F( TestCategory, batched_vector_misc_simd_dcomplex3 ) {
192 // test_batched_vector_misc<TestDevice,SIMD<Kokkos::complex<double> >,3>();
193 // }
194 // TEST_F( TestCategory, batched_vector_misc_simd_dcomplex2 ) {
195 // test_batched_vector_misc<TestDevice,SIMD<Kokkos::complex<double> >,2>();
196 // }
197 // #endif
198
199 #endif // check to not include this in a device test