Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_SerialGesv.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 Kim Liegeois ([email protected])
17
18 #include "gtest/gtest.h"
19 #include "Kokkos_Core.hpp"
20 #include "Kokkos_Random.hpp"
21
22 #include "KokkosBatched_Gesv.hpp"
23 #include "KokkosBatched_Dot.hpp"
24 #include "KokkosBlas2_serial_gemv_impl.hpp"
25
26 #include "KokkosKernels_TestUtils.hpp"
27
28 #include "Test_Batched_DenseUtils.hpp"
29
30 using namespace KokkosBatched;
31
32 namespace Test {
33 namespace Gesv {
34
35 template <typename DeviceType, typename MatrixType, typename VectorType, typename AlgoTagType>
36 struct Functor_TestBatchedSerialGesv {
37 using execution_space = typename DeviceType::execution_space;
38 const MatrixType _A;
39 const MatrixType _tmp;
40 const VectorType _X;
41 const VectorType _B;
42
43 KOKKOS_INLINE_FUNCTION
44 Functor_TestBatchedSerialGesv(const MatrixType &A, const MatrixType &tmp, const VectorType &X, const VectorType &B)
45 : _A(A), _tmp(tmp), _X(X), _B(B) {}
46
47 KOKKOS_INLINE_FUNCTION
48 void operator()(const int k) const {
49 auto A = Kokkos::subview(_A, k, Kokkos::ALL, Kokkos::ALL);
50 auto x = Kokkos::subview(_X, k, Kokkos::ALL);
51 auto b = Kokkos::subview(_B, k, Kokkos::ALL);
52 auto tmp = Kokkos::subview(_tmp, k, Kokkos::ALL, Kokkos::ALL);
53
54 KokkosBatched::SerialGesv<AlgoTagType>::invoke(A, x, b, tmp);
55 }
56
57 inline void run() {
58 typedef typename VectorType::value_type value_type;
59 std::string name_region("KokkosBatched::Test::SerialGesv");
60 const std::string name_value_type = Test::value_type_name<value_type>();
61 std::string name = name_region + name_value_type;
62 Kokkos::Profiling::pushRegion(name.c_str());
63 Kokkos::RangePolicy<execution_space> policy(0, _X.extent(0));
64 Kokkos::parallel_for(name.c_str(), policy, *this);
65 Kokkos::Profiling::popRegion();
66 }
67 };
68
69 template <typename DeviceType, typename MatrixType, typename VectorType, typename AlgoTagType>
70 void impl_test_batched_gesv(const int N, const int BlkSize) {
71 typedef typename MatrixType::value_type value_type;
72 typedef Kokkos::ArithTraits<value_type> ats;
73
74 using MagnitudeType = typename Kokkos::ArithTraits<value_type>::mag_type;
75 using NormViewType = Kokkos::View<MagnitudeType *, Kokkos::LayoutLeft, DeviceType>;
76
77 NormViewType sqr_norm_j("sqr_norm_j", N);
78 auto sqr_norm_j_host = Kokkos::create_mirror_view(sqr_norm_j);
79
80 MatrixType A("A", N, BlkSize, BlkSize), A2("A", N, BlkSize, BlkSize), tmp("tmp", N, BlkSize, BlkSize + 4);
81 VectorType B("b", N, BlkSize), B2("b", N, BlkSize), X("x", N, BlkSize);
82
83 create_tridiagonal_batched_matrices(A, B);
84 Kokkos::deep_copy(A2, A);
85 Kokkos::deep_copy(B2, B);
86
87 auto A_host = Kokkos::create_mirror_view(A2);
88 auto B_host = Kokkos::create_mirror_view(B2);
89 auto X_host = Kokkos::create_mirror_view(X);
90
91 Kokkos::deep_copy(A_host, A2);
92 Kokkos::deep_copy(B_host, B2);
93
94 Kokkos::fence();
95
96 Functor_TestBatchedSerialGesv<DeviceType, MatrixType, VectorType, AlgoTagType>(A, tmp, X, B).run();
97
98 Kokkos::fence();
99
100 Kokkos::deep_copy(X_host, X);
101
102 for (int l = 0; l < N; ++l)
103 KokkosBlas::SerialGemv<Trans::NoTranspose, KokkosBlas::Algo::Gemv::Unblocked>::invoke(
104 -1, Kokkos::subview(A_host, l, Kokkos::ALL, Kokkos::ALL), Kokkos::subview(X_host, l, Kokkos::ALL), 1,
105 Kokkos::subview(B_host, l, Kokkos::ALL));
106
107 KokkosBatched::SerialDot<Trans::NoTranspose>::invoke(B_host, B_host, sqr_norm_j_host);
108
109 const MagnitudeType eps = 1.0e3 * ats::epsilon();
110
111 for (int l = 0; l < N; ++l) EXPECT_NEAR_KK(sqr_norm_j_host(l), 0, eps);
112 }
113 } // namespace Gesv
114 } // namespace Test
115
116 template <typename DeviceType, typename ValueType, typename AlgoTagType>
117 int test_batched_gesv() {
118 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
119 {
120 typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> MatrixType;
121 typedef Kokkos::View<ValueType **, Kokkos::LayoutLeft, DeviceType> VectorType;
122
123 for (int i = 3; i < 10; ++i) {
124 Test::Gesv::impl_test_batched_gesv<DeviceType, MatrixType, VectorType, AlgoTagType>(1024, i);
125 }
126 }
127 #endif
128 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
129 {
130 typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> MatrixType;
131 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> VectorType;
132
133 for (int i = 3; i < 10; ++i) {
134 Test::Gesv::impl_test_batched_gesv<DeviceType, MatrixType, VectorType, AlgoTagType>(1024, i);
135 }
136 }
137 #endif
138
139 return 0;
140 }