Debian Codegraph @

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