| 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 Vinh Dang ([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_Gemm_Decl.hpp" |
| 25 |
#include "KokkosBatched_Gemm_Team_Impl.hpp" |
| 26 |
#include "KokkosBatched_LU_Decl.hpp" |
| 27 |
#include "KokkosBatched_LU_Team_Impl.hpp" |
| 28 |
#include "KokkosBatched_SolveLU_Decl.hpp" |
| 29 |
// #include "KokkosBatched_SolveLU_Team_Impl.hpp" |
| 30 |
|
| 31 |
#include "KokkosKernels_TestUtils.hpp" |
| 32 |
|
| 33 |
using namespace KokkosBatched; |
| 34 |
|
| 35 |
namespace Test { |
| 36 |
namespace TeamSolveLU { |
| 37 |
|
| 38 |
template <typename TA, typename TB> |
| 39 |
struct ParamTag { |
| 40 |
typedef TA transA; |
| 41 |
typedef TB transB; |
| 42 |
}; |
| 43 |
|
| 44 |
template <typename DeviceType, typename ViewType, typename ScalarType, typename ParamTagType, typename AlgoTagType> |
| 45 |
struct Functor_BatchedTeamGemm { |
| 46 |
using execution_space = typename DeviceType::execution_space; |
| 47 |
ViewType _a, _b, _c; |
| 48 |
|
| 49 |
ScalarType _alpha, _beta; |
| 50 |
|
| 51 |
KOKKOS_INLINE_FUNCTION |
| 52 |
Functor_BatchedTeamGemm(const ScalarType alpha, const ViewType &a, const ViewType &b, const ScalarType beta, |
| 53 |
const ViewType &c) |
| 54 |
: _a(a), _b(b), _c(c), _alpha(alpha), _beta(beta) {} |
| 55 |
|
| 56 |
template <typename MemberType> |
| 57 |
KOKKOS_INLINE_FUNCTION void operator()(const ParamTagType &, const MemberType &member) const { |
| 58 |
const int k = member.league_rank(); |
| 59 |
|
| 60 |
auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL()); |
| 61 |
auto bb = Kokkos::subview(_b, k, Kokkos::ALL(), Kokkos::ALL()); |
| 62 |
auto cc = Kokkos::subview(_c, k, Kokkos::ALL(), Kokkos::ALL()); |
| 63 |
|
| 64 |
if (member.team_rank() == 0) { |
| 65 |
for (int i = 0; i < static_cast<int>(aa.extent(0)); ++i) aa(i, i) += 10.0; |
| 66 |
} |
| 67 |
member.team_barrier(); |
| 68 |
|
| 69 |
KokkosBatched::TeamGemm<MemberType, typename ParamTagType::transA, typename ParamTagType::transB, |
| 70 |
AlgoTagType>::invoke(member, _alpha, aa, bb, _beta, cc); |
| 71 |
} |
| 72 |
|
| 73 |
inline void run() { |
| 74 |
typedef typename ViewType::value_type value_type; |
| 75 |
std::string name_region("KokkosBatched::Test::TeamSolveLU"); |
| 76 |
const std::string name_value_type = Test::value_type_name<value_type>(); |
| 77 |
std::string name = name_region + name_value_type; |
| 78 |
Kokkos::Profiling::pushRegion(name.c_str()); |
| 79 |
const int league_size = _c.extent(0); |
| 80 |
Kokkos::TeamPolicy<execution_space, ParamTagType> policy(league_size, Kokkos::AUTO); |
| 81 |
Kokkos::parallel_for((name + "::GemmFunctor").c_str(), policy, *this); |
| 82 |
Kokkos::Profiling::popRegion(); |
| 83 |
} |
| 84 |
}; |
| 85 |
template <typename DeviceType, typename ViewType, typename AlgoTagType> |
| 86 |
struct Functor_BatchedTeamLU { |
| 87 |
using execution_space = typename DeviceType::execution_space; |
| 88 |
ViewType _a; |
| 89 |
|
| 90 |
KOKKOS_INLINE_FUNCTION |
| 91 |
Functor_BatchedTeamLU(const ViewType &a) : _a(a) {} |
| 92 |
|
| 93 |
template <typename MemberType> |
| 94 |
KOKKOS_INLINE_FUNCTION void operator()(const MemberType &member) const { |
| 95 |
const int k = member.league_rank(); |
| 96 |
auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL()); |
| 97 |
|
| 98 |
if (member.team_rank() == 0) { |
| 99 |
for (int i = 0; i < static_cast<int>(aa.extent(0)); ++i) aa(i, i) += 10.0; |
| 100 |
} |
| 101 |
member.team_barrier(); |
| 102 |
|
| 103 |
KokkosBatched::TeamLU<MemberType, AlgoTagType>::invoke(member, aa); |
| 104 |
} |
| 105 |
inline void run() { |
| 106 |
typedef typename ViewType::value_type value_type; |
| 107 |
std::string name_region("KokkosBatched::Test::TeamSolveLU"); |
| 108 |
const std::string name_value_type = Test::value_type_name<value_type>(); |
| 109 |
std::string name = name_region + name_value_type; |
| 110 |
Kokkos::Profiling::pushRegion(name.c_str()); |
| 111 |
const int league_size = _a.extent(0); |
| 112 |
Kokkos::TeamPolicy<execution_space> policy(league_size, Kokkos::AUTO); |
| 113 |
Kokkos::parallel_for((name + "::LUFunctor").c_str(), policy, *this); |
| 114 |
Kokkos::Profiling::popRegion(); |
| 115 |
} |
| 116 |
}; |
| 117 |
template <typename DeviceType, typename ViewType, typename TransType, typename AlgoTagType> |
| 118 |
struct Functor_TestBatchedTeamSolveLU { |
| 119 |
using execution_space = typename DeviceType::execution_space; |
| 120 |
ViewType _a; |
| 121 |
ViewType _b; |
| 122 |
|
| 123 |
KOKKOS_INLINE_FUNCTION |
| 124 |
Functor_TestBatchedTeamSolveLU(const ViewType &a, const ViewType &b) : _a(a), _b(b) {} |
| 125 |
|
| 126 |
template <typename MemberType> |
| 127 |
KOKKOS_INLINE_FUNCTION void operator()(const MemberType &member) const { |
| 128 |
const int k = member.league_rank(); |
| 129 |
auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL()); |
| 130 |
auto bb = Kokkos::subview(_b, k, Kokkos::ALL(), Kokkos::ALL()); |
| 131 |
|
| 132 |
KokkosBatched::TeamSolveLU<MemberType, TransType, AlgoTagType>::invoke(member, aa, bb); |
| 133 |
} |
| 134 |
|
| 135 |
inline void run() { |
| 136 |
typedef typename ViewType::value_type value_type; |
| 137 |
std::string name_region("KokkosBatched::Test::TeamSolveLU"); |
| 138 |
const std::string name_value_type = Test::value_type_name<value_type>(); |
| 139 |
std::string name = name_region + name_value_type; |
| 140 |
Kokkos::Profiling::pushRegion(name.c_str()); |
| 141 |
|
| 142 |
const int league_size = _a.extent(0); |
| 143 |
Kokkos::TeamPolicy<execution_space> policy(league_size, Kokkos::AUTO); |
| 144 |
Kokkos::parallel_for((name + "::SolveLU").c_str(), policy, *this); |
| 145 |
Kokkos::Profiling::popRegion(); |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
template <typename DeviceType, typename ViewType, typename AlgoTagType> |
| 150 |
void impl_test_batched_solvelu(const int N, const int BlkSize) { |
| 151 |
typedef typename ViewType::value_type value_type; |
| 152 |
typedef Kokkos::ArithTraits<value_type> ats; |
| 153 |
|
| 154 |
/// randomized input testing views |
| 155 |
ViewType a0("a0", N, BlkSize, BlkSize); |
| 156 |
ViewType a1("a1", N, BlkSize, BlkSize); |
| 157 |
ViewType b("b", N, BlkSize, 5); |
| 158 |
ViewType x0("x0", N, BlkSize, 5); |
| 159 |
// ViewType a0_T("a0_T", N, BlkSize, BlkSize); |
| 160 |
// ViewType b_T ("b_T", N, BlkSize, 5 ); |
| 161 |
|
| 162 |
Kokkos::Random_XorShift64_Pool<typename DeviceType::execution_space> random(13718); |
| 163 |
Kokkos::fill_random(a0, random, value_type(1.0)); |
| 164 |
Kokkos::fill_random(x0, random, value_type(1.0)); |
| 165 |
|
| 166 |
Kokkos::fence(); |
| 167 |
|
| 168 |
Kokkos::deep_copy(a1, a0); |
| 169 |
// Kokkos::deep_copy(a0_T, a0); |
| 170 |
|
| 171 |
value_type alpha = 1.0, beta = 0.0; |
| 172 |
typedef ParamTag<Trans::NoTranspose, Trans::NoTranspose> param_tag_type; |
| 173 |
|
| 174 |
Functor_BatchedTeamGemm<DeviceType, ViewType, value_type, param_tag_type, AlgoTagType>(alpha, a0, x0, beta, b).run(); |
| 175 |
|
| 176 |
Functor_BatchedTeamLU<DeviceType, ViewType, AlgoTagType>(a1).run(); |
| 177 |
|
| 178 |
Functor_TestBatchedTeamSolveLU<DeviceType, ViewType, Trans::NoTranspose, AlgoTagType>(a1, b).run(); |
| 179 |
|
| 180 |
Kokkos::fence(); |
| 181 |
|
| 182 |
// //Transpose |
| 183 |
// typedef ParamTag<Trans::Transpose,Trans::NoTranspose> param_tag_type_T; |
| 184 |
|
| 185 |
// Functor_BatchedTeamGemm<DeviceType,ViewType,value_type, |
| 186 |
// param_tag_type_T,AlgoTagType>(alpha, a0_T, x0, beta, b_T).run(); |
| 187 |
|
| 188 |
// Functor_TestBatchedTeamSolveLU<DeviceType,ViewType,AlgoTagType,Trans::Transpose>(a1,b_T).run(); |
| 189 |
|
| 190 |
// Kokkos::fence(); |
| 191 |
|
| 192 |
/// for comparison send it to host |
| 193 |
typename ViewType::HostMirror x0_host = Kokkos::create_mirror_view(x0); |
| 194 |
typename ViewType::HostMirror b_host = Kokkos::create_mirror_view(b); |
| 195 |
// typename ViewType::HostMirror b_T_host = Kokkos::create_mirror_view(b_T); |
| 196 |
|
| 197 |
Kokkos::deep_copy(x0_host, x0); |
| 198 |
Kokkos::deep_copy(b_host, b); |
| 199 |
// Kokkos::deep_copy(b_T_host, b_T); |
| 200 |
|
| 201 |
/// check x0 = b ; this eps is about 10^-14 |
| 202 |
typedef typename ats::mag_type mag_type; |
| 203 |
mag_type sum(1), diff(0); |
| 204 |
const mag_type eps = 1.0e3 * ats::epsilon(); |
| 205 |
|
| 206 |
for (int k = 0; k < N; ++k) |
| 207 |
for (int i = 0; i < BlkSize; ++i) |
| 208 |
for (int j = 0; j < 5; ++j) { |
| 209 |
sum += ats::abs(x0_host(k, i, j)); |
| 210 |
diff += ats::abs(x0_host(k, i, j) - b_host(k, i, j)); |
| 211 |
} |
| 212 |
// printf("NoTranspose -- N=%d, BlkSize=%d, sum=%f, diff=%f\n", N, BlkSize, |
| 213 |
// sum, diff); |
| 214 |
EXPECT_NEAR_KK(diff / sum, 0.0, eps); |
| 215 |
|
| 216 |
// /// check x0 = b_T ; this eps is about 10^-14 |
| 217 |
// mag_type sum_T(1), diff_T(0); |
| 218 |
|
| 219 |
// for (int k=0;k<N;++k) |
| 220 |
// for (int i=0;i<BlkSize;++i) |
| 221 |
// for (int j=0;j<5;++j) { |
| 222 |
// sum_T += ats::abs(x0_host(k,i,j)); |
| 223 |
// diff_T += ats::abs(x0_host(k,i,j)-b_T_host(k,i,j)); |
| 224 |
// } |
| 225 |
// //printf("Transpose -- N=%d, BlkSize=%d, sum=%f, diff=%f\n", N, BlkSize, |
| 226 |
// sum_T, diff_T); EXPECT_NEAR_KK( diff_T/sum_T, 0.0, eps); |
| 227 |
} |
| 228 |
} // namespace TeamSolveLU |
| 229 |
} // namespace Test |
| 230 |
|
| 231 |
template <typename DeviceType, typename ValueType, typename AlgoTagType> |
| 232 |
int test_batched_team_solvelu() { |
| 233 |
#if defined(KOKKOSKERNELS_INST_LAYOUTLEFT) |
| 234 |
{ |
| 235 |
typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> ViewType; |
| 236 |
Test::TeamSolveLU::impl_test_batched_solvelu<DeviceType, ViewType, AlgoTagType>(0, 10); |
| 237 |
for (int i = 0; i < 10; ++i) { |
| 238 |
Test::TeamSolveLU::impl_test_batched_solvelu<DeviceType, ViewType, AlgoTagType>(1024, i); |
| 239 |
} |
| 240 |
} |
| 241 |
#endif |
| 242 |
#if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT) |
| 243 |
{ |
| 244 |
typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> ViewType; |
| 245 |
Test::TeamSolveLU::impl_test_batched_solvelu<DeviceType, ViewType, AlgoTagType>(0, 10); |
| 246 |
for (int i = 0; i < 10; ++i) { |
| 247 |
Test::TeamSolveLU::impl_test_batched_solvelu<DeviceType, ViewType, AlgoTagType>(1024, i); |
| 248 |
} |
| 249 |
} |
| 250 |
#endif |
| 251 |
|
| 252 |
return 0; |
| 253 |
} |