1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#ifndef KOKKOS_TEST_SIMD_REDUCTIONS_HPP
#define KOKKOS_TEST_SIMD_REDUCTIONS_HPP
#include <Kokkos_SIMD.hpp>
#include <SIMDTesting_Utilities.hpp>
template <typename T, typename ReductionOp>
struct get_identity {
KOKKOS_INLINE_FUNCTION T operator()() { return T(); }
};
template <typename T, typename BinaryOp>
struct get_identity<T, masked_reduce<BinaryOp>> {
KOKKOS_INLINE_FUNCTION T operator()() {
return Kokkos::Experimental::Impl::Identity<T, BinaryOp>();
}
};
template <typename Abi, typename Loader, typename ReductionOp, typename T>
inline void host_check_reduction_one_loader(ReductionOp reduce_op,
std::size_t n, T const* args) {
Loader loader;
using simd_type = Kokkos::Experimental::basic_simd<T, Abi>;
using mask_type =
typename Kokkos::Experimental::basic_simd<T, Abi>::mask_type;
constexpr std::size_t width = simd_type::size();
for (std::size_t i = 0; i < n; i += width) {
std::size_t const nremaining = n - i;
std::size_t const nlanes = Kokkos::min(nremaining, width);
simd_type arg;
bool const loaded_arg = loader.host_load(args + i, nlanes, arg);
if (!loaded_arg) continue;
T true_identity = get_identity<T, ReductionOp>{}();
T test_identity = 12;
if constexpr (std::is_same_v<Abi, Kokkos::Experimental::simd_abi::scalar>) {
mask_type mask_false(false);
auto expected = reduce_op.on_host_serial(arg, test_identity, mask_false);
auto computed = reduce_op.on_host(arg, test_identity, mask_false);
gtest_checker().equality(expected, computed);
mask_type mask_true(true);
expected = reduce_op.on_host_serial(arg, true_identity, mask_true);
computed = reduce_op.on_host(arg, true_identity, mask_true);
gtest_checker().equality(expected, computed);
} else {
mask_type mask_false(false);
auto expected = reduce_op.on_host_serial(arg, test_identity, mask_false);
auto computed = reduce_op.on_host(arg, test_identity, mask_false);
gtest_checker().equality(expected, computed);
for (std::size_t j = 0; j < mask_type::size(); ++j) {
mask_type mask([=](std::size_t idx) { return idx >= j; });
expected = reduce_op.on_host_serial(arg, true_identity, mask);
computed = reduce_op.on_host(arg, true_identity, mask);
gtest_checker().equality(expected, computed);
}
}
}
}
template <typename Abi, typename ReductionOp, typename T>
inline void host_check_reduction_all_loaders(ReductionOp reduce_op,
std::size_t n, T const* args) {
host_check_reduction_one_loader<Abi, load_element_aligned>(reduce_op, n,
args);
host_check_reduction_one_loader<Abi, load_masked>(reduce_op, n, args);
host_check_reduction_one_loader<Abi, load_as_scalars>(reduce_op, n, args);
}
template <typename Abi, typename DataType, size_t n>
inline void host_check_all_reductions(const DataType (&args)[n]) {
host_check_reduction_all_loaders<Abi>(reduce_min(), n, args);
host_check_reduction_all_loaders<Abi>(reduce_max(), n, args);
host_check_reduction_all_loaders<Abi>(reduce<std::plus<>>(), n, args);
host_check_reduction_all_loaders<Abi>(reduce<std::multiplies<>>(), n, args);
host_check_reduction_all_loaders<Abi>(masked_reduce_min(), n, args);
host_check_reduction_all_loaders<Abi>(masked_reduce_max(), n, args);
host_check_reduction_all_loaders<Abi>(masked_reduce<std::plus<>>(), n, args);
host_check_reduction_all_loaders<Abi>(masked_reduce<std::multiplies<>>(), n,
args);
}
template <typename Abi, typename DataType>
inline void host_check_reductions() {
if constexpr (is_simd_avail_v<DataType, Abi>) {
constexpr size_t n = 16;
if constexpr (std::is_signed_v<DataType>) {
DataType const args[n] = {1, 2, -1, 10, 0, 1, -2, 10,
0, 1, -2, -15, 5, 17, -22, 20};
host_check_all_reductions<Abi>(args);
} else {
DataType const args[n] = {1, 2, 1, 10, 0, 1, 2, 10,
0, 1, 2, 15, 5, 17, 22, 20};
host_check_all_reductions<Abi>(args);
}
}
}
template <typename Abi, typename... DataTypes>
inline void host_check_reductions_all_types(
Kokkos::Experimental::Impl::data_types<DataTypes...>) {
(host_check_reductions<Abi, DataTypes>(), ...);
}
template <typename... Abis>
inline void host_check_reductions_all_abis(
Kokkos::Experimental::Impl::abi_set<Abis...>) {
using DataTypes = Kokkos::Experimental::Impl::data_type_set;
(host_check_reductions_all_types<Abis>(DataTypes()), ...);
}
template <typename Abi, typename Loader, typename ReductionOp, typename T>
KOKKOS_INLINE_FUNCTION void device_check_reduction_one_loader(
ReductionOp reduce_op, std::size_t n, T const* args) {
Loader loader;
using simd_type = Kokkos::Experimental::basic_simd<T, Abi>;
using mask_type =
typename Kokkos::Experimental::basic_simd<T, Abi>::mask_type;
constexpr std::size_t width = simd_type::size();
T true_identity = get_identity<T, ReductionOp>{}();
T test_identity = 12;
for (std::size_t i = 0; i < n; i += width) {
std::size_t const nremaining = n - i;
std::size_t const nlanes = Kokkos::min(nremaining, width);
simd_type arg;
bool const loaded_arg = loader.device_load(args + i, nlanes, arg);
if (!loaded_arg) continue;
mask_type mask_false(false);
auto expected = reduce_op.on_device_serial(arg, test_identity, mask_false);
auto computed = reduce_op.on_device(arg, test_identity, mask_false);
kokkos_checker().equality(expected, computed);
for (std::size_t j = 0; j < mask_type::size(); ++j) {
mask_type mask(KOKKOS_LAMBDA(std::size_t idx) { return idx >= j; });
expected = reduce_op.on_device_serial(arg, true_identity, mask);
computed = reduce_op.on_device(arg, true_identity, mask);
kokkos_checker().equality(expected, computed);
}
}
}
template <typename Abi, typename ReductionOp, typename T>
KOKKOS_INLINE_FUNCTION void device_check_reduction_all_loaders(
ReductionOp reduce_op, std::size_t n, T const* args) {
device_check_reduction_one_loader<Abi, load_element_aligned>(reduce_op, n,
args);
device_check_reduction_one_loader<Abi, load_masked>(reduce_op, n, args);
device_check_reduction_one_loader<Abi, load_as_scalars>(reduce_op, n, args);
}
template <typename Abi, typename DataType, size_t n>
KOKKOS_INLINE_FUNCTION void device_check_all_reductions(
const DataType (&args)[n]) {
device_check_reduction_all_loaders<Abi>(reduce_min(), n, args);
device_check_reduction_all_loaders<Abi>(reduce_max(), n, args);
device_check_reduction_all_loaders<Abi>(reduce<std::plus<>>(), n, args);
device_check_reduction_all_loaders<Abi>(reduce<std::multiplies<>>(), n, args);
device_check_reduction_all_loaders<Abi>(masked_reduce_min(), n, args);
device_check_reduction_all_loaders<Abi>(masked_reduce_max(), n, args);
device_check_reduction_all_loaders<Abi>(masked_reduce<std::plus<>>(), n,
args);
device_check_reduction_all_loaders<Abi>(masked_reduce<std::multiplies<>>(), n,
args);
}
template <typename Abi, typename DataType>
KOKKOS_INLINE_FUNCTION void device_check_reductions() {
if constexpr (is_type_v<Kokkos::Experimental::basic_simd<DataType, Abi>>) {
constexpr size_t n = 16;
if constexpr (std::is_signed_v<DataType>) {
DataType const args[n] = {1, 2, -1, 10, 0, 1, -2, 10,
0, 1, -2, -15, 5, 17, -22, 20};
device_check_all_reductions<Abi>(args);
} else {
DataType const args[n] = {1, 2, 1, 10, 0, 1, 2, 10,
0, 1, 2, 15, 5, 17, 22, 20};
device_check_all_reductions<Abi>(args);
}
}
}
template <typename Abi, typename... DataTypes>
KOKKOS_INLINE_FUNCTION void device_check_reductions_all_types(
Kokkos::Experimental::Impl::data_types<DataTypes...>) {
(device_check_reductions<Abi, DataTypes>(), ...);
}
template <typename... Abis>
KOKKOS_INLINE_FUNCTION void device_check_reductions_all_abis(
Kokkos::Experimental::Impl::abi_set<Abis...>) {
using DataTypes = Kokkos::Experimental::Impl::data_type_set;
(device_check_reductions_all_types<Abis>(DataTypes()), ...);
}
class simd_device_reduction_functor {
public:
KOKKOS_INLINE_FUNCTION void operator()(int) const {
device_check_reductions_all_abis(
Kokkos::Experimental::Impl::device_abi_set());
}
};
TEST(simd, host_reductions) {
host_check_reductions_all_abis(Kokkos::Experimental::Impl::host_abi_set());
}
TEST(simd, device_reductions) {
#ifdef KOKKOS_ENABLE_OPENMPTARGET // FIXME_OPENMPTARGET
GTEST_SKIP()
<< "skipping because of a non-deterministic failure reporting: "
"Failure to synchronize stream (nil): Error in "
"cuStreamSynchronize: an illegal memory access was encountered";
#endif
#if defined(KOKKOS_ENABLE_OPENACC) && \
defined(KOKKOS_COMPILER_CLANG) // FIXME_CLACC
GTEST_SKIP()
<< "skipping because of a non-deterministic failure reporting: "
"Failure to synchronize stream (nil): Error in "
"cuStreamSynchronize: an illegal memory access was encountered";
#endif
Kokkos::parallel_for(1, simd_device_reduction_functor());
}
#endif
|