Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_SerialLaswp.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 Yuuichi Asahi ([email protected])
17 #include <gtest/gtest.h>
18 #include <Kokkos_Core.hpp>
19 #include <Kokkos_Random.hpp>
20
21 #include "KokkosBatched_Util.hpp"
22 #include "KokkosBatched_Laswp.hpp"
23
24 using namespace KokkosBatched;
25
26 namespace Test {
27 namespace Laswp {
28
29 template <typename DeviceType, typename PivViewType, typename AViewType, typename ArgDirect>
30 struct Functor_BatchedSerialLaswp {
31 using execution_space = typename DeviceType::execution_space;
32 PivViewType m_ipiv;
33 AViewType m_a;
34
35 KOKKOS_INLINE_FUNCTION
36 Functor_BatchedSerialLaswp(const PivViewType &ipiv, const AViewType &a) : m_ipiv(ipiv), m_a(a) {}
37
38 KOKKOS_INLINE_FUNCTION
39 void operator()(const ArgDirect &, const int k, int &info) const {
40 auto sub_ipiv = Kokkos::subview(m_ipiv, k, Kokkos::ALL);
41 if constexpr (AViewType::rank == 3) {
42 auto sub_a = Kokkos::subview(m_a, k, Kokkos::ALL, Kokkos::ALL);
43 info += KokkosBatched::SerialLaswp<ArgDirect>::invoke(sub_ipiv, sub_a);
44 } else {
45 auto sub_a = Kokkos::subview(m_a, k, Kokkos::ALL);
46 info += KokkosBatched::SerialLaswp<ArgDirect>::invoke(sub_ipiv, sub_a);
47 }
48 }
49
50 inline int run() {
51 using value_type = typename AViewType::non_const_value_type;
52 std::string name_region("KokkosBatched::Test::SerialLaswp");
53 const std::string name_value_type = Test::value_type_name<value_type>();
54 std::string name = name_region + name_value_type;
55 int info_sum = 0;
56 Kokkos::Profiling::pushRegion(name.c_str());
57 Kokkos::RangePolicy<execution_space, ArgDirect> policy(0, m_a.extent(0));
58 Kokkos::parallel_reduce(name.c_str(), policy, *this, info_sum);
59 Kokkos::Profiling::popRegion();
60 return info_sum;
61 }
62 };
63
64 /// \brief Implementation details of batched laswp analytical test
65 /// Confirm A = Ref (permuted), where
66 /// A0: [[4],
67 /// [1],
68 /// [2]]
69 /// p0: [1, 2, 0]
70 /// Initial 0<->1 1<->2 2<->0
71 /// Forward: [4,1,2] -> [1,4,2] -> [1,2,4] -> [4,2,1]
72 /// Initial 2<->0 1<->2 0<->1
73 /// Backward: [4,1,2] -> [2,1,4] -> [2,4,1] -> [4,2,1]
74 ///
75 /// A1: [[4, 1, 5],
76 /// [2, 3, 7],
77 /// [6, 0, 8]]
78 /// p1: [1, 2, 0]
79 /// Initial 0<->1 1<->2 2<->0
80 /// Forward: [[4, 1, 5], -> [[2, 3, 7], -> [[2, 3, 7], -> [[4, 1, 5],
81 /// [2, 3, 7], [4, 1, 5], [6, 0, 8], [6, 0, 8],
82 /// [6, 0, 8]] [6, 0, 8]] [4, 1, 5]] [2, 3, 7]]
83 /// Initial 2<->0 1<->2 0<->1
84 /// Backward: [[4, 1, 5], -> [[6, 0, 8], -> [[6, 0, 8], -> [[4, 1, 5],
85 /// [2, 3, 7], [2, 3, 7], 4, 1, 5], [6, 0, 8],
86 /// [6, 0, 8]] [4, 1, 5]] [2, 3, 7]] [2, 3, 7]]
87 ///
88 /// A2: [[5, 1],
89 /// [2, 4],
90 /// [3, 0]]
91 /// p2: [2, 0, 1]
92 /// Initial 0<->2 1<->0 2<->1
93 /// Forward: [[5, 1], -> [[3, 0], -> [[2, 4], -> [[2, 4],
94 /// [2, 4], [2, 4] [3, 0], [5, 1],
95 /// [3, 0]] [5, 1]] [5, 1]] [3, 0]]
96 /// Initial 2<->1 1<->0 0<->2
97 /// Backward: [[5, 1], -> [[5, 1], -> [[3, 0], -> [[2, 4],
98 /// [2, 4], [3, 0], [5, 1], [5, 1],
99 /// [3, 0]] [2, 4]] [2, 4]] [3, 0]]
100 ///
101 /// \param N [in] Batch size of matrices
102 template <typename DeviceType, typename ScalarType, typename LayoutType, typename ArgDirect>
103 void impl_test_batched_laswp_analytical(const std::size_t N) {
104 using ats = typename Kokkos::ArithTraits<ScalarType>;
105 using RealType = typename ats::mag_type;
106 using View2DType = Kokkos::View<ScalarType **, LayoutType, DeviceType>;
107 using View3DType = Kokkos::View<ScalarType ***, LayoutType, DeviceType>;
108 using StridedView2DType = Kokkos::View<ScalarType **, Kokkos::LayoutStride, DeviceType>;
109 using StridedView3DType = Kokkos::View<ScalarType ***, Kokkos::LayoutStride, DeviceType>;
110 using PivView2DType = Kokkos::View<int **, LayoutType, DeviceType>;
111
112 View2DType A0("A0", N, 3), Ref0("Ref0", N, 3), A0_identity("A0_identity", N, 3), Ref0_identity("Ref0_identity", N, 3);
113 View3DType A1("A1", N, 3, 3), Ref1("Ref1", N, 3, 3), A1_identity("A1_identity", N, 3, 3),
114 Ref1_identity("Ref1_identity", N, 3, 3);
115 View3DType A2("A2", N, 3, 2), Ref2("Ref2", N, 3, 2), A2_identity("A2_identity", N, 3, 2),
116 Ref2_identity("Ref2_identity", N, 3, 2);
117 PivView2DType ipiv0("ipiv0", N, 3), ipiv1("ipiv1", N, 3), ipiv2("ipiv2", N, 3);
118
119 // Testing incx argument with strided views
120 constexpr std::size_t incx = 2;
121 Kokkos::LayoutStride layout0{N, incx, 3, N * incx};
122 StridedView2DType A0_s("A0_s", layout0), A0_s_identity("A0_s_identity", layout0);
123
124 Kokkos::LayoutStride layout1{N, incx, 3, N * incx, 3, N * incx * 3};
125 StridedView3DType A1_s("A1_s", layout1), A1_s_identity("A1_s_identity", layout1);
126
127 Kokkos::LayoutStride layout2{N, incx, 3, N * incx, 2, N * incx * 3};
128 StridedView3DType A2_s("A2_s", layout2), A2_s_identity("A2_s_identity", layout2);
129
130 // Initialize A0, A1, and A2 with random numbers
131 auto h_A0 = Kokkos::create_mirror_view(A0);
132 auto h_A1 = Kokkos::create_mirror_view(A1);
133 auto h_A2 = Kokkos::create_mirror_view(A2);
134 auto h_Ref0 = Kokkos::create_mirror_view(Ref0);
135 auto h_Ref1 = Kokkos::create_mirror_view(Ref1);
136 auto h_Ref2 = Kokkos::create_mirror_view(Ref2);
137
138 for (std::size_t ib = 0; ib < N; ib++) {
139 h_A0(ib, 0) = 4.0;
140 h_A0(ib, 1) = 1.0;
141 h_A0(ib, 2) = 2.0;
142
143 h_Ref0(ib, 0) = 4.0;
144 h_Ref0(ib, 1) = 2.0;
145 h_Ref0(ib, 2) = 1.0;
146
147 h_A1(ib, 0, 0) = 4.0;
148 h_A1(ib, 0, 1) = 1.0;
149 h_A1(ib, 0, 2) = 5.0;
150 h_A1(ib, 1, 0) = 2.0;
151 h_A1(ib, 1, 1) = 3.0;
152 h_A1(ib, 1, 2) = 7.0;
153 h_A1(ib, 2, 0) = 6.0;
154 h_A1(ib, 2, 1) = 0.0;
155 h_A1(ib, 2, 2) = 8.0;
156
157 h_Ref1(ib, 0, 0) = 4.0;
158 h_Ref1(ib, 0, 1) = 1.0;
159 h_Ref1(ib, 0, 2) = 5.0;
160 h_Ref1(ib, 1, 0) = 6.0;
161 h_Ref1(ib, 1, 1) = 0.0;
162 h_Ref1(ib, 1, 2) = 8.0;
163 h_Ref1(ib, 2, 0) = 2.0;
164 h_Ref1(ib, 2, 1) = 3.0;
165 h_Ref1(ib, 2, 2) = 7.0;
166
167 h_A2(ib, 0, 0) = 5.0;
168 h_A2(ib, 0, 1) = 1.0;
169 h_A2(ib, 1, 0) = 2.0;
170 h_A2(ib, 1, 1) = 4.0;
171 h_A2(ib, 2, 0) = 3.0;
172 h_A2(ib, 2, 1) = 0.0;
173
174 h_Ref2(ib, 0, 0) = 2.0;
175 h_Ref2(ib, 0, 1) = 4.0;
176 h_Ref2(ib, 1, 0) = 5.0;
177 h_Ref2(ib, 1, 1) = 1.0;
178 h_Ref2(ib, 2, 0) = 3.0;
179 h_Ref2(ib, 2, 1) = 0.0;
180 }
181 Kokkos::deep_copy(A0, h_A0);
182 Kokkos::deep_copy(A1, h_A1);
183 Kokkos::deep_copy(A2, h_A2);
184
185 // Strided view can be copied only on the same device
186 Kokkos::deep_copy(A0_s, A0);
187 Kokkos::deep_copy(A1_s, A1);
188 Kokkos::deep_copy(A2_s, A2);
189
190 // Copy A to Ref_identity
191 Kokkos::deep_copy(Ref0_identity, A0);
192 Kokkos::deep_copy(Ref1_identity, A1);
193 Kokkos::deep_copy(Ref2_identity, A2);
194
195 // Permute ipiv
196 auto h_ipiv0 = Kokkos::create_mirror_view(ipiv0);
197 auto h_ipiv1 = Kokkos::create_mirror_view(ipiv1);
198 auto h_ipiv2 = Kokkos::create_mirror_view(ipiv2);
199
200 for (std::size_t ib = 0; ib < N; ib++) {
201 h_ipiv0(ib, 0) = 1;
202 h_ipiv0(ib, 1) = 2;
203 h_ipiv0(ib, 2) = 0;
204
205 h_ipiv1(ib, 0) = 1;
206 h_ipiv1(ib, 1) = 2;
207 h_ipiv1(ib, 2) = 0;
208
209 h_ipiv2(ib, 0) = 2;
210 h_ipiv2(ib, 1) = 0;
211 h_ipiv2(ib, 2) = 1;
212 }
213
214 Kokkos::deep_copy(ipiv0, h_ipiv0);
215 Kokkos::deep_copy(ipiv1, h_ipiv1);
216 Kokkos::deep_copy(ipiv2, h_ipiv2);
217
218 auto info0 = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, View2DType, ArgDirect>(ipiv0, A0).run();
219 auto info1 = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, View3DType, ArgDirect>(ipiv1, A1).run();
220 auto info2 = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, View3DType, ArgDirect>(ipiv2, A2).run();
221
222 Kokkos::fence();
223 EXPECT_EQ(info0, 0);
224 EXPECT_EQ(info1, 0);
225 EXPECT_EQ(info2, 0);
226
227 // For strided views
228 info0 = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, StridedView2DType, ArgDirect>(ipiv0, A0_s).run();
229 info1 = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, StridedView3DType, ArgDirect>(ipiv1, A1_s).run();
230 info2 = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, StridedView3DType, ArgDirect>(ipiv2, A2_s).run();
231
232 Kokkos::fence();
233 EXPECT_EQ(info0, 0);
234 EXPECT_EQ(info1, 0);
235 EXPECT_EQ(info2, 0);
236
237 // Copy permuted A to A_identity which is permuted back to original A
238 Kokkos::deep_copy(A0_identity, A0);
239 Kokkos::deep_copy(A1_identity, A1);
240 Kokkos::deep_copy(A2_identity, A2);
241
242 Kokkos::deep_copy(A0_s_identity, A0_s);
243 Kokkos::deep_copy(A1_s_identity, A1_s);
244 Kokkos::deep_copy(A2_s_identity, A2_s);
245
246 using InvDirect =
247 typename std::conditional_t<std::is_same_v<ArgDirect, Direct::Forward>, Direct::Backward, Direct::Forward>;
248
249 // Permute A_identity in inverse direction to get original A
250 info0 = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, View2DType, InvDirect>(ipiv0, A0_identity).run();
251 info1 = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, View3DType, InvDirect>(ipiv1, A1_identity).run();
252 info2 = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, View3DType, InvDirect>(ipiv2, A2_identity).run();
253
254 Kokkos::fence();
255 EXPECT_EQ(info0, 0);
256 EXPECT_EQ(info1, 0);
257 EXPECT_EQ(info2, 0);
258
259 // For strided views
260 info0 =
261 Functor_BatchedSerialLaswp<DeviceType, PivView2DType, StridedView2DType, InvDirect>(ipiv0, A0_s_identity).run();
262 info1 =
263 Functor_BatchedSerialLaswp<DeviceType, PivView2DType, StridedView3DType, InvDirect>(ipiv1, A1_s_identity).run();
264 info2 =
265 Functor_BatchedSerialLaswp<DeviceType, PivView2DType, StridedView3DType, InvDirect>(ipiv2, A2_s_identity).run();
266
267 Kokkos::fence();
268 EXPECT_EQ(info0, 0);
269 EXPECT_EQ(info1, 0);
270 EXPECT_EQ(info2, 0);
271
272 RealType eps = 1.0e1 * ats::epsilon();
273
274 Kokkos::deep_copy(h_A0, A0);
275 Kokkos::deep_copy(h_A1, A1);
276 Kokkos::deep_copy(h_A2, A2);
277 auto h_A0_identity = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), A0_identity);
278 auto h_A1_identity = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), A1_identity);
279 auto h_A2_identity = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), A2_identity);
280
281 auto h_Ref0_identity = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Ref0_identity);
282 auto h_Ref1_identity = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Ref1_identity);
283 auto h_Ref2_identity = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Ref2_identity);
284 // Check if A is permuted correctly and A_identity is restored
285 for (std::size_t ib = 0; ib < N; ib++) {
286 for (std::size_t i = 0; i < 3; i++) {
287 EXPECT_NEAR_KK_REL(h_A0(ib, i), h_Ref0(ib, i), eps);
288 EXPECT_NEAR_KK_REL(h_A0_identity(ib, i), h_Ref0_identity(ib, i), eps);
289 for (std::size_t j = 0; j < 2; j++) {
290 EXPECT_NEAR_KK_REL(h_A2(ib, i, j), h_Ref2(ib, i, j), eps);
291 EXPECT_NEAR_KK_REL(h_A2_identity(ib, i, j), h_Ref2_identity(ib, i, j), eps);
292 }
293 for (std::size_t j = 0; j < 3; j++) {
294 EXPECT_NEAR_KK_REL(h_A1(ib, i, j), h_Ref1(ib, i, j), eps);
295 EXPECT_NEAR_KK_REL(h_A1_identity(ib, i, j), h_Ref1_identity(ib, i, j), eps);
296 }
297 }
298 }
299
300 // Testing for strided views, reusing A0, A1, A2, A0_identity, A1_identity, A2_identity
301 Kokkos::deep_copy(A0, A0_s);
302 Kokkos::deep_copy(h_A0, A0);
303 Kokkos::deep_copy(A1, A1_s);
304 Kokkos::deep_copy(h_A1, A1);
305 Kokkos::deep_copy(A2, A2_s);
306 Kokkos::deep_copy(h_A2, A2);
307 Kokkos::deep_copy(A0_identity, A0_s_identity);
308 Kokkos::deep_copy(h_A0_identity, A0_identity);
309 Kokkos::deep_copy(A1_identity, A1_s_identity);
310 Kokkos::deep_copy(h_A1_identity, A1_identity);
311 Kokkos::deep_copy(A2_identity, A2_s_identity);
312 Kokkos::deep_copy(h_A2_identity, A2_identity);
313 for (std::size_t ib = 0; ib < N; ib++) {
314 for (std::size_t i = 0; i < 3; i++) {
315 EXPECT_NEAR_KK_REL(h_A0(ib, i), h_Ref0(ib, i), eps);
316 EXPECT_NEAR_KK_REL(h_A0_identity(ib, i), h_Ref0_identity(ib, i), eps);
317 for (std::size_t j = 0; j < 2; j++) {
318 EXPECT_NEAR_KK_REL(h_A2(ib, i, j), h_Ref2(ib, i, j), eps);
319 EXPECT_NEAR_KK_REL(h_A2_identity(ib, i, j), h_Ref2_identity(ib, i, j), eps);
320 }
321 for (std::size_t j = 0; j < 3; j++) {
322 EXPECT_NEAR_KK_REL(h_A1(ib, i, j), h_Ref1(ib, i, j), eps);
323 EXPECT_NEAR_KK_REL(h_A1_identity(ib, i, j), h_Ref1_identity(ib, i, j), eps);
324 }
325 }
326 }
327 }
328
329 /// \brief Implementation details of batched laswp test on vectors
330 /// Apply pivot to vector
331 ///
332 /// \param N [in] Batch size of vectors
333 /// \param BlkSize [in] Length of vector b
334 template <typename DeviceType, typename ScalarType, typename LayoutType, typename ArgDirect>
335 void impl_test_batched_laswp_vector(const std::size_t N, const std::size_t BlkSize) {
336 using ats = typename Kokkos::ArithTraits<ScalarType>;
337 using RealType = typename ats::mag_type;
338 using View2DType = Kokkos::View<ScalarType **, LayoutType, DeviceType>;
339 using StridedView2DType = Kokkos::View<ScalarType **, Kokkos::LayoutStride, DeviceType>;
340 using PivView2DType = Kokkos::View<int **, LayoutType, DeviceType>;
341
342 View2DType b("b", N, BlkSize), Ref("Ref", N, BlkSize), b_identity("b_identity", N, BlkSize),
343 Ref_identity("Ref_identity", N, BlkSize);
344 PivView2DType ipiv("ipiv", N, BlkSize);
345
346 // Testing incx argument with strided views
347 constexpr std::size_t incx = 2;
348 Kokkos::LayoutStride layout{N, incx, BlkSize, N * incx};
349 StridedView2DType b_s("b_s", layout), b_s_identity("b_s_identity", layout);
350
351 // Initialize b with random numbers
352 using execution_space = typename DeviceType::execution_space;
353 Kokkos::Random_XorShift64_Pool<execution_space> rand_pool(13718);
354 ScalarType randStart, randEnd;
355
356 KokkosKernels::Impl::getRandomBounds(1.0, randStart, randEnd);
357 Kokkos::fill_random(b, rand_pool, randStart, randEnd);
358 Kokkos::deep_copy(Ref, b); // This Ref is used to store permuted b
359 Kokkos::deep_copy(Ref_identity, b);
360 Kokkos::deep_copy(b_s, b);
361
362 // Permute ipiv
363 auto h_ipiv = Kokkos::create_mirror_view(ipiv);
364 std::vector<int> ipiv_vec(BlkSize);
365 for (int i = 0; i < static_cast<int>(BlkSize); i++) {
366 ipiv_vec[i] = i;
367 }
368 auto rng = std::default_random_engine{};
369 std::shuffle(ipiv_vec.begin(), ipiv_vec.end(), rng);
370 for (std::size_t ib = 0; ib < N; ib++) {
371 for (std::size_t i = 0; i < BlkSize; i++) {
372 h_ipiv(ib, i) = ipiv_vec[i];
373 }
374 }
375 Kokkos::deep_copy(ipiv, h_ipiv);
376
377 auto info = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, View2DType, ArgDirect>(ipiv, b).run();
378
379 Kokkos::fence();
380 EXPECT_EQ(info, 0);
381
382 // For strided views
383 info = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, StridedView2DType, ArgDirect>(ipiv, b_s).run();
384
385 Kokkos::fence();
386 EXPECT_EQ(info, 0);
387
388 // Copy permuted b to b_identity which is permuted back to original b
389 Kokkos::deep_copy(b_identity, b);
390 Kokkos::deep_copy(b_s_identity, b_s);
391
392 // Permute b_identity in inverse direction to get original b
393 using InvDirect =
394 typename std::conditional_t<std::is_same_v<ArgDirect, Direct::Forward>, Direct::Backward, Direct::Forward>;
395 Functor_BatchedSerialLaswp<DeviceType, PivView2DType, View2DType, InvDirect>(ipiv, b_identity).run();
396 Functor_BatchedSerialLaswp<DeviceType, PivView2DType, StridedView2DType, InvDirect>(ipiv, b_s_identity).run();
397
398 // Make a reference
399 auto h_Ref = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Ref);
400 for (std::size_t ib = 0; ib < N; ib++) {
401 if constexpr (std::is_same_v<ArgDirect, Direct::Forward>) {
402 // Permute Ref by forward pivoting
403 for (int i = 0; i < static_cast<int>(BlkSize); i++) {
404 if (h_ipiv(ib, i) != i) {
405 Kokkos::kokkos_swap(h_Ref(ib, h_ipiv(ib, i)), h_Ref(ib, i));
406 }
407 }
408 } else {
409 // Permute Ref by backward pivoting
410 for (int i = (static_cast<int>(BlkSize) - 1); i >= 0; --i) {
411 if (h_ipiv(ib, i) != i) {
412 Kokkos::kokkos_swap(h_Ref(ib, h_ipiv(ib, i)), h_Ref(ib, i));
413 }
414 }
415 }
416 }
417
418 RealType eps = 1.0e1 * ats::epsilon();
419
420 auto h_b = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), b);
421 auto h_b_identity = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), b_identity);
422 auto h_Ref_identity = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Ref_identity);
423 // Check b is permuted correctly and b_identity is restored
424 for (std::size_t ib = 0; ib < N; ib++) {
425 for (std::size_t i = 0; i < BlkSize; i++) {
426 EXPECT_NEAR_KK_REL(h_b(ib, i), h_Ref(ib, i), eps);
427 EXPECT_NEAR_KK_REL(h_b_identity(ib, i), h_Ref_identity(ib, i), eps);
428 }
429 }
430
431 // Testing for strided views, reusing b and b_identity
432 Kokkos::deep_copy(b, b_s);
433 Kokkos::deep_copy(h_b, b);
434 Kokkos::deep_copy(b_identity, b_s_identity);
435 Kokkos::deep_copy(h_b_identity, b_identity);
436 for (std::size_t ib = 0; ib < N; ib++) {
437 for (std::size_t i = 0; i < BlkSize; i++) {
438 EXPECT_NEAR_KK_REL(h_b(ib, i), h_Ref(ib, i), eps);
439 EXPECT_NEAR_KK_REL(h_b_identity(ib, i), h_Ref_identity(ib, i), eps);
440 }
441 }
442 }
443
444 /// \brief Implementation details of batched laswp test on matrices
445 /// Apply pivot to matrix
446 ///
447 /// \param N [in] Batch size of vectors
448 /// \param BlkSize [in] Row size of matrix A
449 template <typename DeviceType, typename ScalarType, typename LayoutType, typename ArgDirect>
450 void impl_test_batched_laswp_matrix(const std::size_t N, const std::size_t BlkSize) {
451 using ats = typename Kokkos::ArithTraits<ScalarType>;
452 using RealType = typename ats::mag_type;
453 using View3DType = Kokkos::View<ScalarType ***, LayoutType, DeviceType>;
454 using StridedView3DType = Kokkos::View<ScalarType ***, Kokkos::LayoutStride, DeviceType>;
455 using PivView2DType = Kokkos::View<int **, LayoutType, DeviceType>;
456
457 // In order for the tests on non-square matrices, we fix the column size to 5
458 // and scan with the row size
459 constexpr std::size_t M = 5;
460 View3DType A("A", N, BlkSize, M), Ref("Ref", N, BlkSize, M), A_identity("A_identity", N, BlkSize, M),
461 Ref_identity("Ref_identity", N, BlkSize, M);
462 PivView2DType ipiv("ipiv", N, BlkSize);
463
464 // Testing incx argument with strided views
465 constexpr std::size_t incx = 2;
466 Kokkos::LayoutStride layout{N, incx, BlkSize, N * incx, M, N * incx * BlkSize};
467 StridedView3DType A_s("A_s", layout), A_s_identity("A_s_identity", layout);
468
469 // Initialize A with random numbers
470 using execution_space = typename DeviceType::execution_space;
471 Kokkos::Random_XorShift64_Pool<execution_space> rand_pool(13718);
472 ScalarType randStart, randEnd;
473
474 KokkosKernels::Impl::getRandomBounds(1.0, randStart, randEnd);
475 Kokkos::fill_random(A, rand_pool, randStart, randEnd);
476 Kokkos::deep_copy(Ref, A); // This Ref is used to store permuted A
477 Kokkos::deep_copy(Ref_identity, A);
478 Kokkos::deep_copy(A_s, A);
479
480 // Permute ipiv
481 auto h_ipiv = Kokkos::create_mirror_view(ipiv);
482 std::vector<int> ipiv_vec(BlkSize);
483 for (int i = 0; i < static_cast<int>(BlkSize); i++) {
484 ipiv_vec[i] = i;
485 }
486 auto rng = std::default_random_engine{};
487 std::shuffle(ipiv_vec.begin(), ipiv_vec.end(), rng);
488 for (std::size_t ib = 0; ib < N; ib++) {
489 for (std::size_t i = 0; i < BlkSize; i++) {
490 h_ipiv(ib, i) = ipiv_vec[i];
491 }
492 }
493 Kokkos::deep_copy(ipiv, h_ipiv);
494
495 auto info = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, View3DType, ArgDirect>(ipiv, A).run();
496
497 Kokkos::fence();
498 EXPECT_EQ(info, 0);
499
500 // For strided views
501 info = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, StridedView3DType, ArgDirect>(ipiv, A_s).run();
502
503 Kokkos::fence();
504 EXPECT_EQ(info, 0);
505
506 // Copy permuted A to A_identity which is permuted back to original A
507 Kokkos::deep_copy(A_identity, A);
508 Kokkos::deep_copy(A_s_identity, A_s);
509
510 // Permute A_identity in inverse direction to get original A
511 using InvDirect =
512 typename std::conditional_t<std::is_same_v<ArgDirect, Direct::Forward>, Direct::Backward, Direct::Forward>;
513 info = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, View3DType, InvDirect>(ipiv, A_identity).run();
514
515 Kokkos::fence();
516 EXPECT_EQ(info, 0);
517
518 info = Functor_BatchedSerialLaswp<DeviceType, PivView2DType, StridedView3DType, InvDirect>(ipiv, A_s_identity).run();
519
520 Kokkos::fence();
521 EXPECT_EQ(info, 0);
522
523 // permute Ref by ipiv
524 auto h_Ref = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Ref);
525 for (std::size_t ib = 0; ib < N; ib++) {
526 if constexpr (std::is_same_v<ArgDirect, Direct::Forward>) {
527 // Permute Ref by forward pivoting
528 for (int i = 0; i < static_cast<int>(BlkSize); i++) {
529 if (h_ipiv(ib, i) != i) {
530 for (int j = 0; j < static_cast<int>(M); j++) {
531 Kokkos::kokkos_swap(h_Ref(ib, h_ipiv(ib, i), j), h_Ref(ib, i, j));
532 }
533 }
534 }
535 } else {
536 // Permute Ref by backward pivoting
537 for (int i = (static_cast<int>(BlkSize) - 1); i >= 0; --i) {
538 if (h_ipiv(ib, i) != i) {
539 for (int j = 0; j < static_cast<int>(M); j++) {
540 Kokkos::kokkos_swap(h_Ref(ib, h_ipiv(ib, i), j), h_Ref(ib, i, j));
541 }
542 }
543 }
544 }
545 }
546
547 RealType eps = 1.0e1 * ats::epsilon();
548
549 auto h_A = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), A);
550 auto h_A_identity = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), A_identity);
551 auto h_Ref_identity = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Ref_identity);
552 // Check A is permuted correctly and A_identity is restored
553 for (std::size_t ib = 0; ib < N; ib++) {
554 for (std::size_t i = 0; i < BlkSize; i++) {
555 for (std::size_t j = 0; j < M; j++) {
556 EXPECT_NEAR_KK_REL(h_A(ib, i, j), h_Ref(ib, i, j), eps);
557 EXPECT_NEAR_KK_REL(h_A_identity(ib, i, j), h_Ref_identity(ib, i, j), eps);
558 }
559 }
560 }
561
562 // Testing for strided views, reusing A and A_identity
563 Kokkos::deep_copy(A, A_s);
564 Kokkos::deep_copy(h_A, A);
565 Kokkos::deep_copy(A_identity, A_s_identity);
566 Kokkos::deep_copy(h_A_identity, A_identity);
567 for (std::size_t ib = 0; ib < N; ib++) {
568 for (std::size_t i = 0; i < BlkSize; i++) {
569 for (std::size_t j = 0; j < M; j++) {
570 EXPECT_NEAR_KK_REL(h_A(ib, i, j), h_Ref(ib, i, j), eps);
571 EXPECT_NEAR_KK_REL(h_A_identity(ib, i, j), h_Ref_identity(ib, i, j), eps);
572 }
573 }
574 }
575 }
576
577 } // namespace Laswp
578 } // namespace Test
579
580 template <typename DeviceType, typename ScalarType, typename ArgDirect>
581 int test_batched_laswp() {
582 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
583 {
584 using LayoutType = Kokkos::LayoutLeft;
585 Test::Laswp::impl_test_batched_laswp_analytical<DeviceType, ScalarType, LayoutType, ArgDirect>(1);
586 Test::Laswp::impl_test_batched_laswp_analytical<DeviceType, ScalarType, LayoutType, ArgDirect>(2);
587 for (int i = 0; i < 10; i++) {
588 Test::Laswp::impl_test_batched_laswp_vector<DeviceType, ScalarType, LayoutType, ArgDirect>(1, i);
589 Test::Laswp::impl_test_batched_laswp_vector<DeviceType, ScalarType, LayoutType, ArgDirect>(2, i);
590 Test::Laswp::impl_test_batched_laswp_matrix<DeviceType, ScalarType, LayoutType, ArgDirect>(1, i);
591 Test::Laswp::impl_test_batched_laswp_matrix<DeviceType, ScalarType, LayoutType, ArgDirect>(2, i);
592 }
593 }
594 #endif
595 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
596 {
597 using LayoutType = Kokkos::LayoutRight;
598 Test::Laswp::impl_test_batched_laswp_analytical<DeviceType, ScalarType, LayoutType, ArgDirect>(1);
599 Test::Laswp::impl_test_batched_laswp_analytical<DeviceType, ScalarType, LayoutType, ArgDirect>(2);
600 for (int i = 0; i < 10; i++) {
601 Test::Laswp::impl_test_batched_laswp_vector<DeviceType, ScalarType, LayoutType, ArgDirect>(1, i);
602 Test::Laswp::impl_test_batched_laswp_vector<DeviceType, ScalarType, LayoutType, ArgDirect>(2, i);
603 Test::Laswp::impl_test_batched_laswp_matrix<DeviceType, ScalarType, LayoutType, ArgDirect>(1, i);
604 Test::Laswp::impl_test_batched_laswp_matrix<DeviceType, ScalarType, LayoutType, ArgDirect>(2, i);
605 }
606 }
607 #endif
608
609 return 0;
610 }
611
612 #if defined(KOKKOSKERNELS_INST_FLOAT)
613 TEST_F(TestCategory, test_batched_laswp_f_float) { test_batched_laswp<TestDevice, float, Direct::Forward>(); }
614 TEST_F(TestCategory, test_batched_laswp_b_float) { test_batched_laswp<TestDevice, float, Direct::Backward>(); }
615 #endif
616
617 #if defined(KOKKOSKERNELS_INST_DOUBLE)
618 TEST_F(TestCategory, test_batched_laswp_f_double) { test_batched_laswp<TestDevice, double, Direct::Forward>(); }
619 TEST_F(TestCategory, test_batched_laswp_b_double) { test_batched_laswp<TestDevice, double, Direct::Backward>(); }
620 #endif
621
622 #if defined(KOKKOSKERNELS_INST_COMPLEX_FLOAT)
623 TEST_F(TestCategory, test_batched_laswp_f_fcomplex) {
624 test_batched_laswp<TestDevice, Kokkos::complex<float>, Direct::Forward>();
625 }
626 TEST_F(TestCategory, test_batched_laswp_b_fcomplex) {
627 test_batched_laswp<TestDevice, Kokkos::complex<float>, Direct::Backward>();
628 }
629 #endif
630
631 #if defined(KOKKOSKERNELS_INST_COMPLEX_DOUBLE)
632 TEST_F(TestCategory, test_batched_laswp_f_dcomplex) {
633 test_batched_laswp<TestDevice, Kokkos::complex<double>, Direct::Forward>();
634 }
635 TEST_F(TestCategory, test_batched_laswp_b_dcomplex) {
636 test_batched_laswp<TestDevice, Kokkos::complex<double>, Direct::Backward>();
637 }
638 #endif