| 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_Util.hpp" |
| 23 |
#include "KokkosBatched_Trsv_Decl.hpp" |
| 24 |
|
| 25 |
// #include "KokkosKernels_TestUtils.hpp" |
| 26 |
|
| 27 |
using namespace KokkosBatched; |
| 28 |
|
| 29 |
namespace Test { |
| 30 |
namespace Trsv { |
| 31 |
|
| 32 |
template <typename U, typename T, typename D> |
| 33 |
struct ParamTag { |
| 34 |
typedef U uplo; |
| 35 |
typedef T trans; |
| 36 |
typedef D diag; |
| 37 |
}; |
| 38 |
|
| 39 |
template <typename DeviceType, typename ViewType, typename ScalarType, typename ParamTagType, typename AlgoTagType> |
| 40 |
struct Functor_TestBatchedSerialTrsv { |
| 41 |
using execution_space = typename DeviceType::execution_space; |
| 42 |
ViewType _a, _b; |
| 43 |
|
| 44 |
ScalarType _alpha; |
| 45 |
|
| 46 |
KOKKOS_INLINE_FUNCTION |
| 47 |
Functor_TestBatchedSerialTrsv(const ScalarType alpha, const ViewType &a, const ViewType &b) |
| 48 |
: _a(a), _b(b), _alpha(alpha) {} |
| 49 |
|
| 50 |
KOKKOS_INLINE_FUNCTION |
| 51 |
void operator()(const ParamTagType &, const int k) const { |
| 52 |
auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL()); |
| 53 |
auto bb = Kokkos::subview(_b, k, Kokkos::ALL(), 0); |
| 54 |
|
| 55 |
SerialTrsv<typename ParamTagType::uplo, typename ParamTagType::trans, typename ParamTagType::diag, |
| 56 |
AlgoTagType>::invoke(_alpha, aa, bb); |
| 57 |
} |
| 58 |
|
| 59 |
inline void run() { |
| 60 |
typedef typename ViewType::value_type value_type; |
| 61 |
std::string name_region("KokkosBatched::Test::SerialTrsv"); |
| 62 |
const std::string name_value_type = Test::value_type_name<value_type>(); |
| 63 |
std::string name = name_region + name_value_type; |
| 64 |
Kokkos::Profiling::pushRegion(name.c_str()); |
| 65 |
Kokkos::RangePolicy<execution_space, ParamTagType> policy(0, _b.extent(0)); |
| 66 |
Kokkos::parallel_for(name.c_str(), policy, *this); |
| 67 |
Kokkos::Profiling::popRegion(); |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
template <typename DeviceType, typename ViewType, typename ScalarType, typename ParamTagType, typename AlgoTagType> |
| 72 |
void impl_test_batched_trsv(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 |
ScalarType alpha(1.5); |
| 78 |
|
| 79 |
ViewType a0("a0", N, BlkSize, BlkSize), a1("a1", N, BlkSize, BlkSize), b0("b0", N, BlkSize, 1), |
| 80 |
b1("b1", N, BlkSize, 1); |
| 81 |
|
| 82 |
Kokkos::Random_XorShift64_Pool<typename DeviceType::execution_space> random(13718); |
| 83 |
Kokkos::fill_random(a0, random, value_type(1.0)); |
| 84 |
Kokkos::fill_random(b0, random, value_type(1.0)); |
| 85 |
|
| 86 |
Kokkos::fence(); |
| 87 |
|
| 88 |
Kokkos::deep_copy(b0, 1.0); |
| 89 |
|
| 90 |
Kokkos::deep_copy(a1, a0); |
| 91 |
Kokkos::deep_copy(b1, b0); |
| 92 |
|
| 93 |
Functor_TestBatchedSerialTrsv<DeviceType, ViewType, ScalarType, ParamTagType, Algo::Trsv::Unblocked>(alpha, a0, b0) |
| 94 |
.run(); |
| 95 |
Functor_TestBatchedSerialTrsv<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(alpha, a1, b1).run(); |
| 96 |
|
| 97 |
Kokkos::fence(); |
| 98 |
|
| 99 |
/// for comparison send it to host |
| 100 |
typename ViewType::HostMirror a0_host = Kokkos::create_mirror_view(a0); |
| 101 |
typename ViewType::HostMirror b0_host = Kokkos::create_mirror_view(b0); |
| 102 |
typename ViewType::HostMirror b1_host = Kokkos::create_mirror_view(b1); |
| 103 |
|
| 104 |
Kokkos::deep_copy(a0_host, a0); |
| 105 |
Kokkos::deep_copy(b0_host, b0); |
| 106 |
Kokkos::deep_copy(b1_host, b1); |
| 107 |
|
| 108 |
/// 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 |
/// check b0 and b1 are correct |
| 114 |
const value_type one(1); |
| 115 |
const bool is_unit_diag = std::is_same<typename ParamTagType::diag, Diag::Unit>::value; |
| 116 |
for (int k = 0; k < N; ++k) { |
| 117 |
if (std::is_same<typename ParamTagType::trans, Trans::NoTranspose>::value) { |
| 118 |
if (std::is_same<typename ParamTagType::uplo, Uplo::Lower>::value) { |
| 119 |
for (int i = 0; i < BlkSize; ++i) { |
| 120 |
value_type tmp(0); |
| 121 |
for (int j = 0; j <= i; ++j) { |
| 122 |
const value_type aval = (i == j && is_unit_diag ? one : a0_host(k, i, j)); |
| 123 |
const value_type bval = b0_host(k, j, 0); |
| 124 |
tmp += aval * bval; |
| 125 |
} |
| 126 |
EXPECT_NEAR(ats::abs(tmp), ats::abs(alpha), eps); |
| 127 |
} |
| 128 |
for (int i = 0; i < BlkSize; ++i) { |
| 129 |
value_type tmp(0); |
| 130 |
for (int j = 0; j <= i; ++j) { |
| 131 |
const value_type aval = (i == j && is_unit_diag ? one : a0_host(k, i, j)); |
| 132 |
const value_type bval = b1_host(k, j, 0); |
| 133 |
tmp += aval * bval; |
| 134 |
} |
| 135 |
EXPECT_NEAR(ats::abs(tmp), ats::abs(alpha), eps); |
| 136 |
} |
| 137 |
} else if (std::is_same<typename ParamTagType::uplo, Uplo::Upper>::value) { |
| 138 |
for (int i = 0; i < BlkSize; ++i) { |
| 139 |
value_type tmp(0); |
| 140 |
for (int j = i; j < BlkSize; ++j) { |
| 141 |
const value_type aval = (i == j && is_unit_diag ? one : a0_host(k, i, j)); |
| 142 |
const value_type bval = b0_host(k, j, 0); |
| 143 |
tmp += aval * bval; |
| 144 |
} |
| 145 |
EXPECT_NEAR(ats::abs(tmp), ats::abs(alpha), eps); |
| 146 |
} |
| 147 |
for (int i = 0; i < BlkSize; ++i) { |
| 148 |
value_type tmp(0); |
| 149 |
for (int j = i; j < BlkSize; ++j) { |
| 150 |
const value_type aval = (i == j && is_unit_diag ? one : a0_host(k, i, j)); |
| 151 |
const value_type bval = b1_host(k, j, 0); |
| 152 |
tmp += aval * bval; |
| 153 |
} |
| 154 |
EXPECT_NEAR(ats::abs(tmp), ats::abs(alpha), eps); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/// check b0 = b1 ; |
| 161 |
for (int k = 0; k < N; ++k) |
| 162 |
for (int i = 0; i < BlkSize; ++i) |
| 163 |
for (int j = 0; j < 1; ++j) { |
| 164 |
sum += ats::abs(b0_host(k, i, j)); |
| 165 |
diff += ats::abs(b0_host(k, i, j) - b1_host(k, i, j)); |
| 166 |
} |
| 167 |
EXPECT_NEAR(diff / sum, 0.0, eps); |
| 168 |
} |
| 169 |
} // namespace Trsv |
| 170 |
} // namespace Test |
| 171 |
|
| 172 |
template <typename DeviceType, typename ValueType, typename ScalarType, typename ParamTagType, typename AlgoTagType> |
| 173 |
int test_batched_trsv() { |
| 174 |
#if defined(KOKKOSKERNELS_INST_LAYOUTLEFT) |
| 175 |
{ |
| 176 |
typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> ViewType; |
| 177 |
Test::Trsv::impl_test_batched_trsv<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(0, 10); |
| 178 |
for (int i = 0; i < 10; ++i) { |
| 179 |
// printf("Testing: LayoutLeft, Blksize %d, Uplo %d, Trans %d, Diag |
| 180 |
// %d\n", |
| 181 |
// i, |
| 182 |
// std::is_same<typename ParamTagType::uplo, Uplo::Lower>::value, |
| 183 |
// std::is_same<typename ParamTagType::trans, |
| 184 |
// Trans::NoTranspose>::value, std::is_same<typename |
| 185 |
// ParamTagType::diag, Diag::Unit>::value); |
| 186 |
Test::Trsv::impl_test_batched_trsv<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(1, i); |
| 187 |
} |
| 188 |
} |
| 189 |
#endif |
| 190 |
#if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT) |
| 191 |
{ |
| 192 |
typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> ViewType; |
| 193 |
Test::Trsv::impl_test_batched_trsv<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(0, 10); |
| 194 |
for (int i = 0; i < 10; ++i) { |
| 195 |
// printf("Testing: LayoutRight, Blksize %d, Uplo %d, Trans %d, Diag |
| 196 |
// %d\n", |
| 197 |
// i, |
| 198 |
// std::is_same<typename ParamTagType::uplo, Uplo::Lower>::value, |
| 199 |
// std::is_same<typename ParamTagType::trans, |
| 200 |
// Trans::NoTranspose>::value, std::is_same<typename |
| 201 |
// ParamTagType::diag, Diag::Unit>::value); |
| 202 |
Test::Trsv::impl_test_batched_trsv<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(1, i); |
| 203 |
} |
| 204 |
} |
| 205 |
#endif |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |