Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_SerialInverseLU.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 Vinh Dang ([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_Gemm_Decl.hpp"
25 #include "KokkosBatched_Gemm_Serial_Impl.hpp"
26 #include "KokkosBatched_LU_Decl.hpp"
27 #include "KokkosBatched_LU_Serial_Impl.hpp"
28 #include "KokkosBatched_InverseLU_Decl.hpp"
29 // #include "KokkosBatched_InverseLU_Serial_Impl.hpp"
30
31 #include "KokkosKernels_TestUtils.hpp"
32
33 using namespace KokkosBatched;
34
35 namespace Test {
36 namespace SerialInverseLU {
37
38 template <typename TA, typename TB>
39 struct ParamTag {
40 typedef TA transA;
41 typedef TB transB;
42 };
43
44 template <typename DeviceType, typename ViewType, typename ScalarType, typename ParamTagType, typename AlgoTagType>
45 struct Functor_BatchedSerialGemm {
46 using execution_space = typename DeviceType::execution_space;
47 ViewType _a, _b, _c;
48
49 ScalarType _alpha, _beta;
50
51 KOKKOS_INLINE_FUNCTION
52 Functor_BatchedSerialGemm(const ScalarType alpha, const ViewType &a, const ViewType &b, const ScalarType beta,
53 const ViewType &c)
54 : _a(a), _b(b), _c(c), _alpha(alpha), _beta(beta) {}
55
56 KOKKOS_INLINE_FUNCTION
57 void operator()(const ParamTagType &, const int k) const {
58 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
59 auto bb = Kokkos::subview(_b, k, Kokkos::ALL(), Kokkos::ALL());
60 auto cc = Kokkos::subview(_c, k, Kokkos::ALL(), Kokkos::ALL());
61
62 for (int i = 0; i < static_cast<int>(aa.extent(0)); ++i) aa(i, i) += 10.0;
63
64 SerialGemm<typename ParamTagType::transA, typename ParamTagType::transB, AlgoTagType>::invoke(_alpha, aa, bb, _beta,
65 cc);
66 }
67
68 inline void run() {
69 typedef typename ViewType::value_type value_type;
70 std::string name_region("KokkosBatched::Test::SerialInverseLU");
71 const std::string name_value_type = Test::value_type_name<value_type>();
72 std::string name = name_region + name_value_type;
73 Kokkos::Profiling::pushRegion(name.c_str());
74 Kokkos::RangePolicy<execution_space, ParamTagType> policy(0, _c.extent(0));
75 Kokkos::parallel_for((name + "::GemmFunctor").c_str(), policy, *this);
76 Kokkos::Profiling::popRegion();
77 }
78 };
79
80 template <typename DeviceType, typename ViewType, typename AlgoTagType>
81 struct Functor_BatchedSerialLU {
82 using execution_space = typename DeviceType::execution_space;
83 ViewType _a;
84
85 KOKKOS_INLINE_FUNCTION
86 Functor_BatchedSerialLU(const ViewType &a) : _a(a) {}
87
88 KOKKOS_INLINE_FUNCTION
89 void operator()(const int k) const {
90 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
91
92 for (int i = 0; i < static_cast<int>(aa.extent(0)); ++i) aa(i, i) += 10.0;
93
94 SerialLU<AlgoTagType>::invoke(aa);
95 }
96
97 inline void run() {
98 typedef typename ViewType::value_type value_type;
99 std::string name_region("KokkosBatched::Test::SerialInverseLU");
100 const std::string name_value_type = Test::value_type_name<value_type>();
101 std::string name = name_region + name_value_type;
102 Kokkos::Profiling::pushRegion(name.c_str());
103 Kokkos::RangePolicy<execution_space> policy(0, _a.extent(0));
104 Kokkos::parallel_for((name + "::LUFunctor").c_str(), policy, *this);
105 Kokkos::Profiling::popRegion();
106 }
107 };
108
109 template <typename DeviceType, typename AViewType, typename WViewType, typename AlgoTagType>
110 struct Functor_TestBatchedSerialInverseLU {
111 using execution_space = typename DeviceType::execution_space;
112 AViewType _a;
113 WViewType _w;
114
115 KOKKOS_INLINE_FUNCTION
116 Functor_TestBatchedSerialInverseLU(const AViewType &a, const WViewType &w) : _a(a), _w(w) {}
117
118 KOKKOS_INLINE_FUNCTION
119 void operator()(const int k) const {
120 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
121 auto ww = Kokkos::subview(_w, k, Kokkos::ALL());
122
123 KokkosBatched::SerialInverseLU<AlgoTagType>::invoke(aa, ww);
124 }
125
126 inline void run() {
127 typedef typename AViewType::value_type value_type;
128 std::string name_region("KokkosBatched::Test::SerialInverseLU");
129 const std::string name_value_type = Test::value_type_name<value_type>();
130 std::string name = name_region + name_value_type;
131 Kokkos::Profiling::pushRegion(name.c_str());
132 Kokkos::RangePolicy<execution_space> policy(0, _a.extent(0));
133 Kokkos::parallel_for((name + "::InverseLUFunctor").c_str(), policy, *this);
134 Kokkos::Profiling::popRegion();
135 }
136 };
137
138 template <typename DeviceType, typename AViewType, typename WViewType, typename AlgoTagType>
139 void impl_test_batched_inverselu(const int N, const int BlkSize) {
140 typedef typename AViewType::value_type value_type;
141 typedef Kokkos::ArithTraits<value_type> ats;
142
143 /// randomized input testing views
144 AViewType a0("a0", N, BlkSize, BlkSize);
145 AViewType a1("a1", N, BlkSize, BlkSize);
146 WViewType w("w", N, BlkSize * static_cast<size_t>(BlkSize));
147 AViewType c0("c0", N, BlkSize, BlkSize);
148
149 Kokkos::Random_XorShift64_Pool<typename DeviceType::execution_space> random(13718);
150 Kokkos::fill_random(a0, random, value_type(1.0));
151
152 Kokkos::fence();
153
154 Kokkos::deep_copy(a1, a0);
155 Kokkos::deep_copy(w, value_type(0.0));
156
157 Functor_BatchedSerialLU<DeviceType, AViewType, AlgoTagType>(a1).run();
158
159 Functor_TestBatchedSerialInverseLU<DeviceType, AViewType, WViewType, AlgoTagType>(a1, w).run();
160
161 value_type alpha = 1.0, beta = 0.0;
162 typedef SerialInverseLU::ParamTag<Trans::NoTranspose, Trans::NoTranspose> param_tag_type;
163
164 Functor_BatchedSerialGemm<DeviceType, AViewType, value_type, param_tag_type, AlgoTagType>(alpha, a0, a1, beta, c0)
165 .run();
166
167 Kokkos::fence();
168
169 /// for comparison send it to host
170 typename AViewType::HostMirror c0_host = Kokkos::create_mirror_view(c0);
171
172 Kokkos::deep_copy(c0_host, c0);
173
174 /// check identity matrix ; this eps is about 10^-14
175 typedef typename ats::mag_type mag_type;
176 mag_type sum_diag(0), sum_all(0), sum_diag_ref(N * BlkSize);
177 const mag_type eps = 1.0e3 * ats::epsilon();
178
179 for (int k = 0; k < N; ++k)
180 for (int i = 0; i < BlkSize; ++i)
181 for (int j = 0; j < BlkSize; ++j) {
182 sum_all += ats::abs(c0_host(k, i, j));
183 if (i == j) sum_diag += ats::abs(c0_host(k, i, j));
184 }
185 EXPECT_NEAR_KK(sum_all - sum_diag, 0, eps);
186 EXPECT_NEAR_KK(sum_diag - sum_diag_ref, 0, eps);
187 }
188 } // namespace SerialInverseLU
189 } // namespace Test
190
191 template <typename DeviceType, typename ValueType, typename AlgoTagType>
192 int test_batched_inverselu() {
193 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
194 {
195 typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> AViewType;
196 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> WViewType;
197 Test::SerialInverseLU::impl_test_batched_inverselu<DeviceType, AViewType, WViewType, AlgoTagType>(0, 10);
198 for (int i = 0; i < 10; ++i) {
199 Test::SerialInverseLU::impl_test_batched_inverselu<DeviceType, AViewType, WViewType, AlgoTagType>(1024, i);
200 }
201 }
202 #endif
203 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
204 {
205 typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> AViewType;
206 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> WViewType;
207 Test::SerialInverseLU::impl_test_batched_inverselu<DeviceType, AViewType, WViewType, AlgoTagType>(0, 10);
208 for (int i = 0; i < 10; ++i) {
209 Test::SerialInverseLU::impl_test_batched_inverselu<DeviceType, AViewType, WViewType, AlgoTagType>(1024, i);
210 }
211 }
212 #endif
213
214 return 0;
215 }