Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_TeamVectorUTV.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_Copy_Decl.hpp"
23 #include "KokkosBatched_ApplyPivot_Decl.hpp"
24 #include "KokkosBlas2_team_gemv_spec.hpp"
25 #include "KokkosBatched_Trsv_Decl.hpp"
26 #include "KokkosBatched_UTV_Decl.hpp"
27
28 #include "KokkosKernels_TestUtils.hpp"
29
30 using namespace KokkosBatched;
31
32 namespace Test {
33
34 template <typename DeviceType, typename MatrixViewType, typename VectorViewType, typename PivViewType,
35 typename WorkViewType, typename AlgoTagType>
36 struct Functor_TestBatchedTeamVectorUTV {
37 using execution_space = typename DeviceType::execution_space;
38 MatrixViewType _r, _a, _acopy, _u, _v;
39 PivViewType _p;
40 VectorViewType _x, _b;
41 WorkViewType _w;
42
43 KOKKOS_INLINE_FUNCTION
44 Functor_TestBatchedTeamVectorUTV(const MatrixViewType &r, const MatrixViewType &a, const MatrixViewType &acopy,
45 const MatrixViewType &u, const MatrixViewType &v, const PivViewType &p,
46 const VectorViewType &x, const VectorViewType &b, const WorkViewType &w)
47 : _r(r), _a(a), _acopy(acopy), _u(u), _v(v), _p(p), _x(x), _b(b), _w(w) {}
48
49 template <typename MemberType>
50 KOKKOS_INLINE_FUNCTION void operator()(const MemberType &member) const {
51 typedef typename MatrixViewType::non_const_value_type value_type;
52 const value_type one(1), zero(0), add_this(10);
53
54 const int k = member.league_rank();
55 auto rr = Kokkos::subview(_r, k, Kokkos::ALL(), Kokkos::ALL());
56 auto ac = Kokkos::subview(_acopy, k, Kokkos::ALL(), Kokkos::ALL());
57
58 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
59 auto uu = Kokkos::subview(_u, k, Kokkos::ALL(), Kokkos::ALL());
60 auto vv = Kokkos::subview(_v, k, Kokkos::ALL(), Kokkos::ALL());
61
62 auto pp = Kokkos::subview(_p, k, Kokkos::ALL());
63
64 auto bb = Kokkos::subview(_b, k, Kokkos::ALL());
65 auto xx = Kokkos::subview(_x, k, Kokkos::ALL());
66
67 auto ww = Kokkos::subview(_w, k, Kokkos::ALL());
68
69 // make diagonal dominant and set xx = 1,2,3,4,5
70 const int m = aa.extent(0), r = rr.extent(1);
71 if (m <= r) {
72 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, m), [&](const int &i) {
73 aa(i, i) += add_this;
74 xx(i) = (i + 1);
75 });
76 } else {
77 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, m * m), [=](const int &ij) {
78 const int i = ij / m, j = ij % m;
79 value_type tmp(0);
80 for (int l = 0; l < r; ++l) tmp += rr(i, l) * rr(j, l);
81 aa(i, j) = tmp;
82 });
83 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, m), [&](const int &i) { xx(i) = (i + 1); });
84 }
85 member.team_barrier(); // finish writing aa, xx
86
87 /// copy for verification
88 TeamVectorCopy<MemberType, Trans::NoTranspose>::invoke(member, aa, ac);
89
90 /// bb = AA*xx
91 KokkosBlas::TeamVectorGemv<MemberType, Trans::NoTranspose, Algo::Gemv::Unblocked>::invoke(member, one, aa, xx, zero,
92 bb);
93 member.team_barrier();
94
95 /// Solving Ax = b using UTV transformation
96 /// A P^T P x = b
97 /// UTV P x = b;
98
99 /// UTV = A P^T
100 int matrix_rank(0);
101 TeamVectorUTV<MemberType, AlgoTagType>::invoke(member, aa, pp, uu, vv, ww, matrix_rank);
102 member.team_barrier();
103 const auto range_upto_rank = Kokkos::pair<int, int>(0, matrix_rank);
104 auto um = Kokkos::subview(uu, Kokkos::ALL(), range_upto_rank);
105 auto am = Kokkos::subview(aa, range_upto_rank, range_upto_rank);
106 auto vm = Kokkos::subview(vv, range_upto_rank, Kokkos::ALL());
107 if (matrix_rank < m) {
108 /// w = U^T b
109 KokkosBlas::TeamVectorGemv<MemberType, Trans::Transpose, Algo::Gemv::Unblocked>::invoke(member, one, um, bb, zero,
110 ww);
111 member.team_barrier();
112
113 /// w = T^{-1} w
114 TeamVectorTrsv<MemberType, Uplo::Lower, Trans::NoTranspose, Diag::NonUnit, Algo::Trsv::Unblocked>::invoke(
115 member, one, am, ww);
116 member.team_barrier();
117
118 /// x = V^T w
119 KokkosBlas::TeamVectorGemv<MemberType, Trans::Transpose, Algo::Gemv::Unblocked>::invoke(member, one, vm, ww, zero,
120 xx);
121 member.team_barrier();
122 } else {
123 /// x = U^T b
124 KokkosBlas::TeamVectorGemv<MemberType, Trans::Transpose, Algo::Gemv::Unblocked>::invoke(member, one, um, bb, zero,
125 xx);
126 member.team_barrier();
127
128 /// x = T^{-1} x
129 TeamVectorTrsv<MemberType, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, Algo::Trsv::Unblocked>::invoke(
130 member, one, am, xx);
131 member.team_barrier();
132 }
133
134 /// x = P^T x
135 TeamVectorApplyPivot<MemberType, Side::Left, Direct::Backward>::invoke(member, pp, xx);
136 member.team_barrier();
137 }
138
139 inline void run() {
140 typedef typename MatrixViewType::non_const_value_type value_type;
141 std::string name_region("KokkosBatched::Test::TeamVectorUTV");
142 const std::string name_value_type = Test::value_type_name<value_type>();
143 std::string name = name_region + name_value_type;
144 Kokkos::Profiling::pushRegion(name.c_str());
145
146 const int league_size = _a.extent(0);
147 Kokkos::TeamPolicy<execution_space> policy(league_size, Kokkos::AUTO);
148
149 Kokkos::parallel_for(name.c_str(), policy, *this);
150 Kokkos::Profiling::popRegion();
151 }
152 };
153
154 template <typename DeviceType, typename MatrixViewType, typename VectorViewType, typename PivViewType,
155 typename WorkViewType, typename AlgoTagType>
156 void impl_test_batched_utv(const int N, const int BlkSize) {
157 typedef typename MatrixViewType::non_const_value_type value_type;
158 typedef Kokkos::ArithTraits<value_type> ats;
159 // const value_type one(1);
160 /// randomized input testing views
161 MatrixViewType r("r", N, BlkSize, 3);
162 MatrixViewType a("a", N, BlkSize, BlkSize);
163 MatrixViewType acopy("copy", N, BlkSize, BlkSize);
164 MatrixViewType u("u", N, BlkSize, BlkSize);
165 MatrixViewType v("v", N, BlkSize, BlkSize);
166 PivViewType p("p", N, BlkSize);
167 VectorViewType x("x", N, BlkSize);
168 VectorViewType b("b", N, BlkSize);
169 WorkViewType w("w", N, 3 * BlkSize);
170
171 Kokkos::fence();
172
173 Kokkos::Random_XorShift64_Pool<typename DeviceType::execution_space> random(13718);
174 if (BlkSize <= 3)
175 Kokkos::fill_random(a, random, value_type(1.0));
176 else
177 Kokkos::fill_random(r, random, value_type(1.0));
178
179 Kokkos::fence();
180
181 Functor_TestBatchedTeamVectorUTV<DeviceType, MatrixViewType, VectorViewType, PivViewType, WorkViewType, AlgoTagType>(
182 r, a, acopy, u, v, p, x, b, w)
183 .run();
184
185 Kokkos::fence();
186
187 /// for comparison send it to host
188 auto a_host = Kokkos::create_mirror_view(acopy);
189 auto x_host = Kokkos::create_mirror_view(x);
190 auto b_host = Kokkos::create_mirror_view(b);
191 auto w_host = Kokkos::create_mirror_view(w);
192
193 Kokkos::deep_copy(a_host, acopy);
194 Kokkos::deep_copy(x_host, x);
195 Kokkos::deep_copy(b_host, b);
196
197 /// this is least square; we cannot expect high
198 typedef typename ats::mag_type mag_type;
199 const mag_type eps = 1e3 * ats::epsilon();
200
201 for (int k = 0; k < N; ++k) {
202 mag_type residual(0), norm(0);
203 for (int i = 0; i < BlkSize; ++i) {
204 value_type tmp(0);
205 for (int j = 0; j < BlkSize; ++j) {
206 tmp += a_host(k, i, j) * x_host(k, j);
207 }
208 w_host(k, i) = tmp - b_host(k, i);
209 }
210 for (int i = 0; i < BlkSize; ++i) {
211 value_type tmp(0);
212 for (int j = 0; j < BlkSize; ++j) {
213 tmp += a_host(k, j, i) * w_host(k, j);
214 }
215 residual += ats::abs(tmp) * ats::abs(tmp);
216 norm += ats::abs(b_host(k, i)) * ats::abs(b_host(k, i));
217 }
218 // printf("norm %e, residual %e, rel res %e\n", norm, residual,
219 // residual/norm);
220 EXPECT_NEAR_KK(residual / norm, mag_type(0), eps);
221 }
222 }
223 } // namespace Test
224
225 template <typename DeviceType, typename ValueType, typename IntType, typename AlgoTagType>
226 int test_batched_utv() {
227 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
228 {
229 typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> MatrixViewType;
230 typedef Kokkos::View<ValueType **, Kokkos::LayoutLeft, DeviceType> VectorViewType;
231 typedef Kokkos::View<IntType **, Kokkos::LayoutLeft, DeviceType> PivViewType;
232 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> WorkViewType;
233 Test::impl_test_batched_utv<DeviceType, MatrixViewType, VectorViewType, PivViewType, WorkViewType, AlgoTagType>(0,
234 10);
235 for (int i = 1; i < 10; ++i) {
236 // printf("Testing: LayoutLeft, Blksize %d\n", i);
237 Test::impl_test_batched_utv<DeviceType, MatrixViewType, VectorViewType, PivViewType, WorkViewType, AlgoTagType>(
238 1024, i);
239 }
240 }
241 #endif
242 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
243 {
244 typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> MatrixViewType;
245 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> VectorViewType;
246 typedef Kokkos::View<IntType **, Kokkos::LayoutRight, DeviceType> PivViewType;
247 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> WorkViewType;
248 Test::impl_test_batched_utv<DeviceType, MatrixViewType, VectorViewType, PivViewType, WorkViewType, AlgoTagType>(0,
249 10);
250 for (int i = 1; i < 10; ++i) {
251 // printf("Testing: LayoutRight, Blksize %d\n", i);
252 Test::impl_test_batched_utv<DeviceType, MatrixViewType, VectorViewType, PivViewType, WorkViewType, AlgoTagType>(
253 1024, i);
254 }
255 }
256 #endif
257
258 return 0;
259 }