Debian Codegraph @

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