Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_SerialLU.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 #include "gtest/gtest.h"
19 #include "Kokkos_Core.hpp"
20 #include "Kokkos_Random.hpp"
21
22 // #include "KokkosBatched_Vector.hpp"
23
24 #include "KokkosBatched_LU_Decl.hpp"
25 #include "KokkosBatched_LU_Serial_Impl.hpp"
26
27 #include "KokkosKernels_TestUtils.hpp"
28
29 using namespace KokkosBatched;
30
31 namespace Test {
32
33 template <typename DeviceType, typename ViewType, typename AlgoTagType>
34 struct Functor_TestBatchedSerialLU {
35 using execution_space = typename DeviceType::execution_space;
36 ViewType _a;
37
38 KOKKOS_INLINE_FUNCTION
39 Functor_TestBatchedSerialLU(const ViewType &a) : _a(a) {}
40
41 KOKKOS_INLINE_FUNCTION
42 void operator()(const int k) const {
43 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
44
45 for (int i = 0; i < static_cast<int>(aa.extent(0)); ++i) aa(i, i) += 10.0;
46
47 SerialLU<AlgoTagType>::invoke(aa);
48 }
49
50 inline void run() {
51 typedef typename ViewType::value_type value_type;
52 std::string name_region("KokkosBatched::Test::SerialLU");
53 const std::string name_value_type = Test::value_type_name<value_type>();
54 std::string name = name_region + name_value_type;
55 Kokkos::Profiling::pushRegion(name.c_str());
56 Kokkos::RangePolicy<execution_space> policy(0, _a.extent(0));
57 Kokkos::parallel_for(name.c_str(), policy, *this);
58 Kokkos::Profiling::popRegion();
59 }
60 };
61
62 template <typename DeviceType, typename ViewType, typename AlgoTagType>
63 void impl_test_batched_lu(const int N, const int BlkSize) {
64 typedef typename ViewType::value_type value_type;
65 typedef Kokkos::ArithTraits<value_type> ats;
66
67 /// randomized input testing views
68 ViewType a0("a0", N, BlkSize, BlkSize), a1("a1", N, BlkSize, BlkSize);
69
70 Kokkos::Random_XorShift64_Pool<typename DeviceType::execution_space> random(13718);
71 Kokkos::fill_random(a0, random, value_type(1.0));
72
73 Kokkos::fence();
74
75 Kokkos::deep_copy(a1, a0);
76
77 Functor_TestBatchedSerialLU<DeviceType, ViewType, Algo::LU::Unblocked>(a0).run();
78 Functor_TestBatchedSerialLU<DeviceType, ViewType, AlgoTagType>(a1).run();
79
80 Kokkos::fence();
81
82 /// for comparison send it to host
83 typename ViewType::HostMirror a0_host = Kokkos::create_mirror_view(a0);
84 typename ViewType::HostMirror a1_host = Kokkos::create_mirror_view(a1);
85
86 Kokkos::deep_copy(a0_host, a0);
87 Kokkos::deep_copy(a1_host, a1);
88
89 /// check b0 = b1 ; this eps is about 10^-14
90 typedef typename ats::mag_type mag_type;
91 mag_type sum(1), diff(0);
92 const mag_type eps = 1.0e3 * ats::epsilon();
93
94 for (int k = 0; k < N; ++k)
95 for (int i = 0; i < BlkSize; ++i)
96 for (int j = 0; j < BlkSize; ++j) {
97 sum += ats::abs(a0_host(k, i, j));
98 diff += ats::abs(a0_host(k, i, j) - a1_host(k, i, j));
99 }
100 EXPECT_NEAR_KK(diff / sum, 0, eps);
101 }
102 } // namespace Test
103
104 template <typename DeviceType, typename ValueType, typename AlgoTagType>
105 int test_batched_lu() {
106 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
107 {
108 typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> ViewType;
109 Test::impl_test_batched_lu<DeviceType, ViewType, AlgoTagType>(0, 10);
110 for (int i = 0; i < 10; ++i) {
111 // printf("Testing: LayoutLeft, Blksize %d\n", i);
112 Test::impl_test_batched_lu<DeviceType, ViewType, AlgoTagType>(1024, i);
113 }
114 }
115 #endif
116 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
117 {
118 typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> ViewType;
119 Test::impl_test_batched_lu<DeviceType, ViewType, AlgoTagType>(0, 10);
120 for (int i = 0; i < 10; ++i) {
121 // printf("Testing: LayoutLeft, Blksize %d\n", i);
122 Test::impl_test_batched_lu<DeviceType, ViewType, AlgoTagType>(1024, i);
123 }
124 }
125 #endif
126
127 return 0;
128 }