Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_SerialSolveLU.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_SolveLU_Decl.hpp"
29 // #include "KokkosBatched_SolveLU_Serial_Impl.hpp"
30
31 #include "KokkosKernels_TestUtils.hpp"
32
33 using namespace KokkosBatched;
34
35 namespace Test {
36 namespace SerialSolveLU {
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::SerialSolveLU");
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::SerialSolveLU");
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 ViewType, typename TransType, typename AlgoTagType>
110 struct Functor_TestBatchedSerialSolveLU {
111 using execution_space = typename DeviceType::execution_space;
112 ViewType _a;
113 ViewType _b;
114
115 KOKKOS_INLINE_FUNCTION
116 Functor_TestBatchedSerialSolveLU(const ViewType &a, const ViewType &b) : _a(a), _b(b) {}
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 bb = Kokkos::subview(_b, k, Kokkos::ALL(), Kokkos::ALL());
122
123 KokkosBatched::SerialSolveLU<TransType, AlgoTagType>::invoke(aa, bb);
124 }
125
126 inline void run() {
127 typedef typename ViewType::value_type value_type;
128 std::string name_region("KokkosBatched::Test::SerialSolveLU");
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 + "::SolveLUFunctor").c_str(), policy, *this);
134 Kokkos::Profiling::popRegion();
135 }
136 };
137
138 template <typename DeviceType, typename ViewType, typename AlgoTagType>
139 void impl_test_batched_solvelu(const int N, const int BlkSize) {
140 typedef typename ViewType::value_type value_type;
141 typedef Kokkos::ArithTraits<value_type> ats;
142
143 /// randomized input testing views
144 ViewType a0("a0", N, BlkSize, BlkSize);
145 ViewType a1("a1", N, BlkSize, BlkSize);
146 ViewType b("b", N, BlkSize, 5);
147 ViewType x0("x0", N, BlkSize, 5);
148 // ViewType a0_T("a0_T", N, BlkSize, BlkSize);
149 // ViewType b_T ("b_T", N, BlkSize, 5 );
150
151 Kokkos::Random_XorShift64_Pool<typename DeviceType::execution_space> random(13718);
152 Kokkos::fill_random(a0, random, value_type(1.0));
153 Kokkos::fill_random(x0, random, value_type(1.0));
154
155 Kokkos::fence();
156
157 Kokkos::deep_copy(a1, a0);
158 // Kokkos::deep_copy(a0_T, a0);
159
160 value_type alpha = 1.0, beta = 0.0;
161 typedef ParamTag<Trans::NoTranspose, Trans::NoTranspose> param_tag_type;
162
163 Functor_BatchedSerialGemm<DeviceType, ViewType, value_type, param_tag_type, AlgoTagType>(alpha, a0, x0, beta, b)
164 .run();
165
166 Functor_BatchedSerialLU<DeviceType, ViewType, AlgoTagType>(a1).run();
167
168 Functor_TestBatchedSerialSolveLU<DeviceType, ViewType, Trans::NoTranspose, AlgoTagType>(a1, b).run();
169
170 Kokkos::fence();
171
172 // //Transpose
173 // typedef ParamTag<Trans::Transpose,Trans::NoTranspose> param_tag_type_T;
174
175 // Functor_BatchedSerialGemm<DeviceType,ViewType,value_type,
176 // param_tag_type_T,AlgoTagType>(alpha, a0_T, x0, beta, b_T).run();
177
178 // Functor_TestBatchedSerialSolveLU<DeviceType,ViewType,Trans::Transpose,AlgoTagType>(a1,b_T).run();
179
180 // Kokkos::fence();
181
182 /// for comparison send it to host
183 typename ViewType::HostMirror x0_host = Kokkos::create_mirror_view(x0);
184 typename ViewType::HostMirror b_host = Kokkos::create_mirror_view(b);
185 // typename ViewType::HostMirror b_T_host = Kokkos::create_mirror_view(b_T);
186
187 Kokkos::deep_copy(x0_host, x0);
188 Kokkos::deep_copy(b_host, b);
189 // Kokkos::deep_copy(b_T_host, b_T);
190
191 /// check x0 = b ; this eps is about 10^-14
192 typedef typename ats::mag_type mag_type;
193 mag_type sum(1), diff(0);
194 const mag_type eps = 1.0e3 * ats::epsilon();
195
196 for (int k = 0; k < N; ++k)
197 for (int i = 0; i < BlkSize; ++i)
198 for (int j = 0; j < 5; ++j) {
199 sum += ats::abs(x0_host(k, i, j));
200 diff += ats::abs(x0_host(k, i, j) - b_host(k, i, j));
201 }
202 // printf("NoTranspose -- N=%d, BlkSize=%d, sum=%f, diff=%f\n", N, BlkSize,
203 // sum, diff);
204 EXPECT_NEAR_KK(diff / sum, 0.0, eps);
205
206 /// check x0 = b_T ; this eps is about 10^-14
207 // mag_type sum_T(1), diff_T(0);
208
209 // for (int k=0;k<N;++k)
210 // for (int i=0;i<BlkSize;++i)
211 // for (int j=0;j<5;++j) {
212 // sum_T += ats::abs(x0_host(k,i,j));
213 // diff_T += ats::abs(x0_host(k,i,j)-b_T_host(k,i,j));
214 // }
215 // //printf("Transpose -- N=%d, BlkSize=%d, sum=%f, diff=%f\n", N, BlkSize,
216 // sum_T, diff_T); EXPECT_NEAR_KK( diff_T/sum_T, 0.0, eps);
217 }
218 } // namespace SerialSolveLU
219 } // namespace Test
220
221 template <typename DeviceType, typename ValueType, typename AlgoTagType>
222 int test_batched_solvelu() {
223 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
224 {
225 typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> ViewType;
226 Test::SerialSolveLU::impl_test_batched_solvelu<DeviceType, ViewType, AlgoTagType>(0, 10);
227 for (int i = 0; i < 10; ++i) {
228 Test::SerialSolveLU::impl_test_batched_solvelu<DeviceType, ViewType, AlgoTagType>(1024, i);
229 }
230 }
231 #endif
232 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
233 {
234 typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> ViewType;
235 Test::SerialSolveLU::impl_test_batched_solvelu<DeviceType, ViewType, AlgoTagType>(0, 10);
236 for (int i = 0; i < 10; ++i) {
237 Test::SerialSolveLU::impl_test_batched_solvelu<DeviceType, ViewType, AlgoTagType>(1024, i);
238 }
239 }
240 #endif
241
242 return 0;
243 }