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