Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_SerialAxpy.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_Axpy.hpp"
23
24 #include "KokkosKernels_TestUtils.hpp"
25
26 using namespace KokkosBatched;
27
28 namespace Test {
29 namespace Axpy {
30
31 template <typename DeviceType, typename ViewType, typename alphaViewType>
32 struct Functor_TestBatchedSerialAxpy {
33 using execution_space = typename DeviceType::execution_space;
34 const alphaViewType _alpha;
35 const ViewType _X;
36 const ViewType _Y;
37
38 KOKKOS_INLINE_FUNCTION
39 Functor_TestBatchedSerialAxpy(const alphaViewType &alpha, const ViewType &X, const ViewType &Y)
40 : _alpha(alpha), _X(X), _Y(Y) {}
41
42 KOKKOS_INLINE_FUNCTION
43 void operator()(const int k) const {
44 auto alpha = Kokkos::subview(_alpha, Kokkos::make_pair(k, k + 1));
45 auto x = Kokkos::subview(_X, Kokkos::make_pair(k, k + 1), Kokkos::ALL);
46 auto y = Kokkos::subview(_Y, Kokkos::make_pair(k, k + 1), Kokkos::ALL);
47
48 KokkosBatched::SerialAxpy::invoke(alpha, x, y);
49 }
50
51 inline void run() {
52 typedef typename ViewType::value_type value_type;
53 std::string name_region("KokkosBatched::Test::SerialAxpy");
54 const std::string name_value_type = Test::value_type_name<value_type>();
55 std::string name = name_region + name_value_type;
56 Kokkos::Profiling::pushRegion(name.c_str());
57 Kokkos::RangePolicy<execution_space> policy(0, _X.extent(0));
58 Kokkos::parallel_for(name.c_str(), policy, *this);
59 Kokkos::Profiling::popRegion();
60 }
61 };
62
63 template <typename DeviceType, typename ViewType, typename alphaViewType>
64 void impl_test_batched_axpy(const int N, const int BlkSize) {
65 typedef typename ViewType::value_type value_type;
66 typedef typename ViewType::const_value_type const_value_type;
67 typedef typename alphaViewType::const_value_type alpha_const_value_type;
68 typedef Kokkos::ArithTraits<value_type> ats;
69
70 ViewType X0("x0", N, BlkSize), X1("x1", N, BlkSize), Y0("y0", N, BlkSize), Y1("y1", N, BlkSize);
71
72 alphaViewType alpha("alpha", N);
73
74 Kokkos::Random_XorShift64_Pool<typename DeviceType::execution_space> random(13718);
75 Kokkos::fill_random(X0, random, const_value_type(1.0));
76 Kokkos::fill_random(Y0, random, const_value_type(1.0));
77 Kokkos::fill_random(alpha, random, alpha_const_value_type(1.0));
78
79 Kokkos::fence();
80
81 Kokkos::deep_copy(X1, X0);
82 Kokkos::deep_copy(Y1, Y0);
83
84 /// test body
85 auto alpha_host = Kokkos::create_mirror_view(alpha);
86 auto X0_host = Kokkos::create_mirror_view(X0);
87 auto Y0_host = Kokkos::create_mirror_view(Y0);
88
89 Kokkos::deep_copy(alpha_host, alpha);
90 Kokkos::deep_copy(X0_host, X0);
91 Kokkos::deep_copy(Y0_host, Y0);
92
93 for (int l = 0; l < N; ++l)
94 for (int i = 0; i < BlkSize; ++i) Y0_host(l, i) += alpha_host(l) * X0_host(l, i);
95
96 Functor_TestBatchedSerialAxpy<DeviceType, ViewType, alphaViewType>(alpha, X1, Y1).run();
97
98 Kokkos::fence();
99
100 /// for comparison send it to host
101 auto Y1_host = Kokkos::create_mirror_view(Y1);
102
103 Kokkos::deep_copy(Y1_host, Y1);
104
105 /// check c0 = c1 ; this eps is about 10^-14
106 typedef typename ats::mag_type mag_type;
107 mag_type sum(1), diff(0);
108 const mag_type eps = 1.0e3 * ats::epsilon();
109
110 for (int l = 0; l < N; ++l)
111 for (int i = 0; i < BlkSize; ++i) {
112 sum += ats::abs(Y0_host(l, i));
113 diff += ats::abs(Y0_host(l, i) - Y1_host(l, i));
114 }
115 EXPECT_NEAR_KK(diff / sum, 0, eps);
116 }
117 } // namespace Axpy
118 } // namespace Test
119
120 template <typename DeviceType, typename ValueType, typename ScalarType>
121 int test_batched_axpy() {
122 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
123 {
124 typedef Kokkos::View<ValueType **, Kokkos::LayoutLeft, DeviceType> ViewType;
125 typedef Kokkos::View<ScalarType *, Kokkos::LayoutLeft, DeviceType> alphaViewType;
126
127 for (int i = 3; i < 10; ++i) {
128 Test::Axpy::impl_test_batched_axpy<DeviceType, ViewType, alphaViewType>(1024, i);
129 }
130 }
131 #endif
132 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
133 {
134 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> ViewType;
135 typedef Kokkos::View<ScalarType *, Kokkos::LayoutRight, DeviceType> alphaViewType;
136
137 for (int i = 3; i < 10; ++i) {
138 Test::Axpy::impl_test_batched_axpy<DeviceType, ViewType, alphaViewType>(1024, i);
139 }
140 }
141 #endif
142
143 return 0;
144 }