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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
//@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_SHIFT_OPS_HPP
#define KOKKOS_TEST_SIMD_SHIFT_OPS_HPP
#include <Kokkos_SIMD.hpp>
#include <SIMDTesting_Utilities.hpp>
template <typename Abi, typename Loader, typename ShiftOp, typename DataType>
inline void host_check_shift_on_one_loader(ShiftOp shift_op,
DataType test_vals[],
DataType shift_by[], std::size_t n) {
using simd_type = Kokkos::Experimental::basic_simd<DataType, Abi>;
constexpr std::size_t width = simd_type::size();
Loader loader;
for (std::size_t i = 0; i < n; ++i) {
simd_type simd_vals;
bool const loaded_arg = loader.host_load(test_vals, width, simd_vals);
if (!loaded_arg) {
continue;
}
DataType expected_val[width];
for (std::size_t lane = 0; lane < width; ++lane) {
expected_val[lane] =
shift_op.on_host(simd_vals[lane], static_cast<int>(shift_by[i]));
}
simd_type expected_result;
expected_result.copy_from(expected_val,
Kokkos::Experimental::simd_flag_default);
simd_type const computed_result =
shift_op.on_host(simd_vals, static_cast<int>(shift_by[i]));
host_check_equality(expected_result, computed_result, width);
}
}
template <typename Abi, typename Loader, typename ShiftOp, typename DataType>
inline void host_check_shift_by_lanes_on_one_loader(
ShiftOp shift_op, DataType test_vals[],
Kokkos::Experimental::basic_simd<DataType, Abi>& shift_by) {
using simd_type = Kokkos::Experimental::basic_simd<DataType, Abi>;
constexpr std::size_t width = simd_type::size();
Loader loader;
simd_type simd_vals;
bool const loaded_arg = loader.host_load(test_vals, width, simd_vals);
ASSERT_TRUE(loaded_arg);
DataType expected_val[width];
for (std::size_t lane = 0; lane < width; ++lane) {
expected_val[lane] =
shift_op.on_host(simd_vals[lane], static_cast<int>(shift_by[lane]));
}
simd_type expected_result;
expected_result.copy_from(expected_val,
Kokkos::Experimental::simd_flag_default);
simd_type const computed_result = shift_op.on_host(simd_vals, shift_by);
host_check_equality(expected_result, computed_result, width);
}
template <typename Abi, typename ShiftOp, typename DataType>
inline void host_check_shift_op_all_loaders(ShiftOp shift_op,
DataType test_vals[],
DataType shift_by[],
std::size_t n) {
host_check_shift_on_one_loader<Abi, load_element_aligned>(shift_op, test_vals,
shift_by, n);
host_check_shift_on_one_loader<Abi, load_masked>(shift_op, test_vals,
shift_by, n);
host_check_shift_on_one_loader<Abi, load_as_scalars>(shift_op, test_vals,
shift_by, n);
host_check_shift_on_one_loader<Abi, load_vector_aligned>(shift_op, test_vals,
shift_by, n);
Kokkos::Experimental::basic_simd<DataType, Abi> shift_by_lanes;
shift_by_lanes.copy_from(shift_by, Kokkos::Experimental::simd_flag_default);
host_check_shift_by_lanes_on_one_loader<Abi, load_element_aligned>(
shift_op, test_vals, shift_by_lanes);
host_check_shift_by_lanes_on_one_loader<Abi, load_masked>(shift_op, test_vals,
shift_by_lanes);
host_check_shift_by_lanes_on_one_loader<Abi, load_as_scalars>(
shift_op, test_vals, shift_by_lanes);
host_check_shift_by_lanes_on_one_loader<Abi, load_vector_aligned>(
shift_op, test_vals, shift_by_lanes);
}
template <typename Abi, typename DataType>
inline void host_check_shift_ops() {
if constexpr (is_simd_avail_v<DataType, Abi>) {
if constexpr (std::is_integral_v<DataType>) {
using simd_type = Kokkos::Experimental::basic_simd<DataType, Abi>;
constexpr std::size_t width = simd_type::size();
constexpr std::size_t num_cases = 16;
constexpr size_t alignment =
Kokkos::Experimental::basic_simd<DataType, Abi>::size() *
sizeof(DataType);
DataType max = std::numeric_limits<DataType>::max();
alignas(alignment) DataType shift_by[num_cases] = {
0, 1, 3, width / 2, width / 2 + 1, width - 1, width, width + 1,
0, 1, 3, width / 2, width / 2 + 1, width - 1, width, width + 1};
alignas(alignment) DataType test_vals[width];
for (std::size_t i = 0; i < width; ++i) {
DataType inc = max / width;
test_vals[i] = i * inc + 1;
}
host_check_shift_op_all_loaders<Abi>(shift_right(), test_vals, shift_by,
num_cases);
host_check_shift_op_all_loaders<Abi>(shift_right_eq(), test_vals,
shift_by, num_cases);
host_check_shift_op_all_loaders<Abi>(shift_left(), test_vals, shift_by,
num_cases);
host_check_shift_op_all_loaders<Abi>(shift_left_eq(), test_vals, shift_by,
num_cases);
if constexpr (std::is_signed_v<DataType>) {
for (std::size_t i = 0; i < width; ++i) test_vals[i] *= -1;
host_check_shift_op_all_loaders<Abi>(shift_right(), test_vals, shift_by,
num_cases);
host_check_shift_op_all_loaders<Abi>(shift_right_eq(), test_vals,
shift_by, num_cases);
}
}
}
}
template <typename Abi, typename... DataTypes>
inline void host_check_shift_ops_all_types(
Kokkos::Experimental::Impl::data_types<DataTypes...>) {
(host_check_shift_ops<Abi, DataTypes>(), ...);
}
template <typename... Abis>
inline void host_check_shift_ops_all_abis(
Kokkos::Experimental::Impl::abi_set<Abis...>) {
using DataTypes = Kokkos::Experimental::Impl::data_type_set;
(host_check_shift_ops_all_types<Abis>(DataTypes()), ...);
}
template <typename Abi, typename Loader, typename ShiftOp, typename DataType>
KOKKOS_INLINE_FUNCTION void device_check_shift_on_one_loader(
ShiftOp shift_op, DataType test_vals[], DataType shift_by[],
std::size_t n) {
using simd_type = Kokkos::Experimental::basic_simd<DataType, Abi>;
constexpr std::size_t width = simd_type::size();
Loader loader;
for (std::size_t i = 0; i < n; ++i) {
simd_type simd_vals;
bool const loaded_arg = loader.device_load(test_vals, width, simd_vals);
if (!loaded_arg) {
continue;
}
simd_type expected_result(KOKKOS_LAMBDA(std::size_t lane) {
return shift_op.on_device(simd_vals[lane], static_cast<int>(shift_by[i]));
});
simd_type const computed_result =
shift_op.on_device(simd_vals, static_cast<int>(shift_by[i]));
device_check_equality(expected_result, computed_result, width);
}
}
template <typename Abi, typename Loader, typename ShiftOp, typename DataType>
KOKKOS_INLINE_FUNCTION void device_check_shift_by_lanes_on_one_loader(
ShiftOp shift_op, DataType test_vals[],
Kokkos::Experimental::basic_simd<DataType, Abi>& shift_by) {
using simd_type = Kokkos::Experimental::basic_simd<DataType, Abi>;
constexpr std::size_t width = simd_type::size();
Loader loader;
simd_type simd_vals;
loader.device_load(test_vals, width, simd_vals);
simd_type expected_result(KOKKOS_LAMBDA(std::size_t lane) {
return shift_op.on_device(simd_vals[lane],
static_cast<int>(shift_by[lane]));
});
simd_type const computed_result = shift_op.on_device(simd_vals, shift_by);
device_check_equality(expected_result, computed_result, width);
}
template <typename Abi, typename ShiftOp, typename DataType>
KOKKOS_INLINE_FUNCTION void device_check_shift_op_all_loaders(
ShiftOp shift_op, DataType test_vals[], DataType shift_by[],
std::size_t n) {
device_check_shift_on_one_loader<Abi, load_element_aligned>(
shift_op, test_vals, shift_by, n);
device_check_shift_on_one_loader<Abi, load_masked>(shift_op, test_vals,
shift_by, n);
device_check_shift_on_one_loader<Abi, load_as_scalars>(shift_op, test_vals,
shift_by, n);
device_check_shift_on_one_loader<Abi, load_vector_aligned>(
shift_op, test_vals, shift_by, n);
Kokkos::Experimental::basic_simd<DataType, Abi> shift_by_lanes;
shift_by_lanes.copy_from(shift_by, Kokkos::Experimental::simd_flag_default);
device_check_shift_by_lanes_on_one_loader<Abi, load_element_aligned>(
shift_op, test_vals, shift_by_lanes);
device_check_shift_by_lanes_on_one_loader<Abi, load_masked>(
shift_op, test_vals, shift_by_lanes);
device_check_shift_by_lanes_on_one_loader<Abi, load_as_scalars>(
shift_op, test_vals, shift_by_lanes);
device_check_shift_by_lanes_on_one_loader<Abi, load_vector_aligned>(
shift_op, test_vals, shift_by_lanes);
}
template <typename Abi, typename DataType>
KOKKOS_INLINE_FUNCTION void device_check_shift_ops() {
if constexpr (is_type_v<Kokkos::Experimental::basic_simd<DataType, Abi>>) {
if constexpr (std::is_integral_v<DataType>) {
using simd_type = Kokkos::Experimental::basic_simd<DataType, Abi>;
constexpr std::size_t width = simd_type::size();
constexpr std::size_t num_cases = 16;
DataType max = Kokkos::reduction_identity<DataType>::max();
DataType shift_by[num_cases] = {
0, 1, 3, width / 2, width / 2 + 1, width - 1, width, width + 1,
0, 1, 3, width / 2, width / 2 + 1, width - 1, width, width + 1};
DataType test_vals[width];
for (std::size_t i = 0; i < width; ++i) {
DataType inc = max / width;
test_vals[i] = i * inc + 1;
}
device_check_shift_op_all_loaders<Abi>(shift_right(), test_vals, shift_by,
num_cases);
device_check_shift_op_all_loaders<Abi>(shift_right_eq(), test_vals,
shift_by, num_cases);
device_check_shift_op_all_loaders<Abi>(shift_left(), test_vals, shift_by,
num_cases);
device_check_shift_op_all_loaders<Abi>(shift_left_eq(), test_vals,
shift_by, num_cases);
if constexpr (std::is_signed_v<DataType>) {
for (std::size_t i = 0; i < width; ++i) test_vals[i] *= -1;
device_check_shift_op_all_loaders<Abi>(shift_right(), test_vals,
shift_by, num_cases);
device_check_shift_op_all_loaders<Abi>(shift_right_eq(), test_vals,
shift_by, num_cases);
}
}
}
}
template <typename Abi, typename... DataTypes>
KOKKOS_INLINE_FUNCTION void device_check_shift_ops_all_types(
Kokkos::Experimental::Impl::data_types<DataTypes...>) {
(device_check_shift_ops<Abi, DataTypes>(), ...);
}
template <typename... Abis>
KOKKOS_INLINE_FUNCTION void device_check_shift_ops_all_abis(
Kokkos::Experimental::Impl::abi_set<Abis...>) {
using DataTypes = Kokkos::Experimental::Impl::data_type_set;
(device_check_shift_ops_all_types<Abis>(DataTypes()), ...);
}
class simd_device_shift_ops_functor {
public:
KOKKOS_INLINE_FUNCTION void operator()(int) const {
device_check_shift_ops_all_abis(
Kokkos::Experimental::Impl::device_abi_set());
}
};
TEST(simd, host_shift_ops) {
host_check_shift_ops_all_abis(Kokkos::Experimental::Impl::host_abi_set());
}
TEST(simd, device_shift_ops) {
Kokkos::parallel_for(Kokkos::RangePolicy<Kokkos::IndexType<int>>(0, 1),
simd_device_shift_ops_functor());
}
#endif
|