Debian Codegraph @

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