Debian Codegraph @

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