Debian Codegraph @

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