Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_TeamTrsm.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_Vector.hpp"
23
24 #include "KokkosBatched_Trsm_Decl.hpp"
25 #include "KokkosBatched_Trsm_Serial_Impl.hpp"
26 #include "KokkosBatched_Trsm_Team_Impl.hpp"
27
28 #include "KokkosKernels_TestUtils.hpp"
29
30 using namespace KokkosBatched;
31
32 namespace Test {
33 namespace TeamTrsm {
34
35 template <typename S, typename U, typename T, typename D>
36 struct ParamTag {
37 typedef S side;
38 typedef U uplo;
39 typedef T trans;
40 typedef D diag;
41 };
42
43 template <typename DeviceType, typename ViewType, typename ScalarType, typename ParamTagType, typename AlgoTagType>
44 struct Functor_TestBatchedTeamTrsm {
45 using execution_space = typename DeviceType::execution_space;
46 ViewType _a, _b;
47
48 ScalarType _alpha;
49
50 KOKKOS_INLINE_FUNCTION
51 Functor_TestBatchedTeamTrsm(const ScalarType alpha, const ViewType &a, const ViewType &b)
52 : _a(a), _b(b), _alpha(alpha) {}
53
54 template <typename MemberType>
55 KOKKOS_INLINE_FUNCTION void operator()(const ParamTagType &, const MemberType &member) const {
56 const int k = member.league_rank();
57
58 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
59 auto bb = Kokkos::subview(_b, k, Kokkos::ALL(), Kokkos::ALL());
60
61 KokkosBatched::TeamTrsm<MemberType, typename ParamTagType::side, typename ParamTagType::uplo,
62 typename ParamTagType::trans, typename ParamTagType::diag, AlgoTagType>::invoke(member,
63 _alpha, aa,
64 bb);
65 }
66
67 inline void run() {
68 typedef typename ViewType::value_type value_type;
69 std::string name_region("KokkosBatched::Test::TeamTrsm");
70 const std::string name_value_type = Test::value_type_name<value_type>();
71 std::string name = name_region + name_value_type;
72 Kokkos::Profiling::pushRegion(name.c_str());
73
74 const int league_size = _b.extent(0);
75 Kokkos::TeamPolicy<execution_space, ParamTagType> policy(league_size, Kokkos::AUTO);
76 Kokkos::parallel_for(name.c_str(), policy, *this);
77 Kokkos::Profiling::popRegion();
78 }
79 };
80
81 template <typename DeviceType, typename ViewType, typename ScalarType, typename ParamTagType, typename AlgoTagType>
82 void impl_test_batched_trsm(const int N, const int BlkSize, const int NumCols) {
83 typedef typename ViewType::value_type value_type;
84 typedef Kokkos::ArithTraits<value_type> ats;
85
86 /// randomized input testing views
87 ScalarType alpha(1.0);
88
89 const bool is_side_right = std::is_same<typename ParamTagType::side, Side::Right>::value;
90 const int b_nrows = is_side_right ? NumCols : BlkSize;
91 const int b_ncols = is_side_right ? BlkSize : NumCols;
92 ViewType a0("a0", N, BlkSize, BlkSize), a1("a1", N, BlkSize, BlkSize), b0("b0", N, b_nrows, b_ncols),
93 b1("b1", N, b_nrows, b_ncols);
94
95 Kokkos::Random_XorShift64_Pool<typename DeviceType::execution_space> random(13718);
96 Kokkos::fill_random(a0, random, value_type(1.0));
97 Kokkos::fill_random(b0, random, value_type(1.0));
98
99 Kokkos::fence();
100
101 Kokkos::deep_copy(a1, a0);
102 Kokkos::deep_copy(b1, b0);
103
104 Functor_TestBatchedTeamTrsm<DeviceType, ViewType, ScalarType, ParamTagType, Algo::Trsm::Unblocked>(alpha, a0, b0)
105 .run();
106 Functor_TestBatchedTeamTrsm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(alpha, a1, b1).run();
107
108 Kokkos::fence();
109
110 /// for comparison send it to host
111 typename ViewType::HostMirror b0_host = Kokkos::create_mirror_view(b0);
112 typename ViewType::HostMirror b1_host = Kokkos::create_mirror_view(b1);
113
114 Kokkos::deep_copy(b0_host, b0);
115 Kokkos::deep_copy(b1_host, b1);
116
117 /// check b0 = b1 ; this eps is about 10^-14
118 typedef typename ats::mag_type mag_type;
119 mag_type sum(1), diff(0);
120 const mag_type eps = 1.0e3 * ats::epsilon();
121
122 for (int k = 0; k < N; ++k)
123 for (int i = 0; i < b_nrows; ++i)
124 for (int j = 0; j < b_ncols; ++j) {
125 sum += ats::abs(b0_host(k, i, j));
126 diff += ats::abs(b0_host(k, i, j) - b1_host(k, i, j));
127 }
128 EXPECT_NEAR_KK(diff / sum, 0.0, eps);
129 }
130 } // namespace TeamTrsm
131 } // namespace Test
132
133 template <typename DeviceType, typename ValueType, typename ScalarType, typename ParamTagType, typename AlgoTagType>
134 int test_batched_team_trsm() {
135 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
136 {
137 typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> ViewType;
138 Test::TeamTrsm::impl_test_batched_trsm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(0, 10, 4);
139 for (int i = 0; i < 10; ++i) {
140 // printf("Testing: LayoutLeft, Blksize %d\n", i);
141 Test::TeamTrsm::impl_test_batched_trsm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(1024, i, 4);
142 Test::TeamTrsm::impl_test_batched_trsm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(1024, i, 1);
143 }
144 }
145 #endif
146 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
147 {
148 typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> ViewType;
149 Test::TeamTrsm::impl_test_batched_trsm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(0, 10, 4);
150 for (int i = 0; i < 10; ++i) {
151 // printf("Testing: LayoutRight, Blksize %d\n", i);
152 Test::TeamTrsm::impl_test_batched_trsm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(1024, i, 4);
153 Test::TeamTrsm::impl_test_batched_trsm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(1024, i, 1);
154 }
155 }
156 #endif
157
158 return 0;
159 }