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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
//@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
#include <Kokkos_Array.hpp>
#include <Kokkos_DetectionIdiom.hpp>
namespace {
// nvcc errors on variables only used in static_asserts
// Passing those variables to this function should eliminate the warning
template <typename... Ts>
KOKKOS_FUNCTION constexpr void maybe_unused(Ts&&...) {}
template <typename T, typename U = T>
using equality_comparable =
decltype(std::declval<T const&>() == std::declval<U const&>());
KOKKOS_FUNCTION constexpr bool test_array() {
constexpr Kokkos::Array<int, 3> a{{1, 2}};
static_assert(!a.empty());
static_assert(a.size() == 3);
static_assert(a.max_size() == 3);
static_assert(*a.data() == 1);
static_assert(a[1] == 2);
return true;
}
static_assert(test_array());
KOKKOS_FUNCTION constexpr bool test_array_structured_binding_support() {
constexpr Kokkos::Array<float, 2> a{};
auto& [xr, yr] = a;
(void)xr;
(void)yr;
auto [x, y] = a;
(void)x;
(void)y;
auto const& [xcr, ycr] = a;
(void)xcr;
(void)ycr;
return true;
}
static_assert(test_array_structured_binding_support());
KOKKOS_FUNCTION constexpr bool test_array_ctad() {
constexpr int x = 10;
constexpr Kokkos::Array a{1, 2, 3, 5, x};
constexpr Kokkos::Array<int, 5> b{1, 2, 3, 5, x};
return std::is_same_v<decltype(a), decltype(b)> && a == b;
}
static_assert(test_array_ctad());
KOKKOS_FUNCTION constexpr bool test_array_aggregate_initialization() {
// Initialize arrays from brace-init-list as for std::array.
Kokkos::Array<float, 2> aggregate_initialization_syntax_1 = {1.41f, 3.14f};
if ((aggregate_initialization_syntax_1[0] != 1.41f) ||
(aggregate_initialization_syntax_1[1] != 3.14f))
return false;
Kokkos::Array<int, 3> aggregate_initialization_syntax_2{
{0, 1, 2}}; // since C++11
if ((aggregate_initialization_syntax_2[0] != 0) ||
(aggregate_initialization_syntax_2[1] != 1) ||
(aggregate_initialization_syntax_2[2] != 2))
return false;
// Note that this is a valid initialization.
Kokkos::Array<double, 3> initialized_with_one_argument_missing = {{255, 255}};
if ((initialized_with_one_argument_missing[0] != 255) ||
(initialized_with_one_argument_missing[1] != 255) ||
(initialized_with_one_argument_missing[2] != 0))
return false;
// But the following line would not compile
// Kokkos::Array< double, 3 > initialized_with_too_many{ { 1, 2, 3, 4 } };
return true;
}
static_assert(test_array_aggregate_initialization());
// A few compilers, such as GCC 8.4, were erroring out when the function below
// appeared in a constant expression because
// Kokkos::Array<T, 0, Proxy>::operator[] is non-constexpr. The issue
// disappears with GCC 9.1 (https://godbolt.org/z/TG4TEef1b). As a workaround,
// the static_assert was dropped and the [[maybe_unused]] is used as an attempt
// to silent warnings that the function is never used.
[[maybe_unused]] KOKKOS_FUNCTION void test_array_zero_sized() {
using T = float;
// The code below must compile for zero-sized arrays.
constexpr int N = 0;
Kokkos::Array<T, N> a;
for (int i = 0; i < N; ++i) {
a[i] = T();
}
}
constexpr bool test_array_const_qualified_element_type() {
Kokkos::Array<int const, 1> a{255};
return a[0] == 255;
}
static_assert(test_array_const_qualified_element_type());
// User-defined type providing a sepcialization of kokkos_swap
struct MyInt {
int i;
private:
friend constexpr KOKKOS_FUNCTION void kokkos_swap(MyInt& lhs,
MyInt& rhs) noexcept {
lhs.i = 255;
rhs.i = 127;
}
};
constexpr bool test_array_specialization_kokkos_swap() {
Kokkos::Array<MyInt, 2> a{MyInt{1}, MyInt{2}};
Kokkos::Array<MyInt, 2> b{MyInt{11}, MyInt{22}};
// sanity check
if (a[0].i != 1 || a[1].i != 2 || b[0].i != 11 || b[1].i != 22) {
return false;
}
using Kokkos::kokkos_swap;
kokkos_swap(a, b);
// check that the user-definied kokkos_swap(MyInt) overload was called
if (a[0].i != 255 || a[1].i != 255 || b[0].i != 127 || b[1].i != 127) {
return false;
}
return true;
}
static_assert(test_array_specialization_kokkos_swap());
constexpr bool test_to_array() {
// copies a string literal
[[maybe_unused]] auto a1 = Kokkos::to_array("foo");
static_assert(a1.size() == 4);
maybe_unused(a1);
// deduces both element type and length
[[maybe_unused]] auto a2 = Kokkos::to_array({0, 2, 1, 3});
static_assert(std::is_same_v<decltype(a2), Kokkos::Array<int, 4>>);
maybe_unused(a2);
// gcc8, icc, and nvcc 11.3 do not support the implicit conversion
#if !(defined(KOKKOS_COMPILER_GNU) && (KOKKOS_COMPILER_GNU < 910)) && \
!(defined(KOKKOS_COMPILER_NVCC) && (KOKKOS_COMPILER_NVCC < 1140))
// deduces length with element type specified
// implicit conversion happens
[[maybe_unused]] auto a3 = Kokkos::to_array<long>({0, 1, 3});
static_assert(std::is_same_v<decltype(a3), Kokkos::Array<long, 3>>);
maybe_unused(a3);
#endif
return true;
}
static_assert(test_to_array());
// making sure we cover both const and non-const cases by having a function that
// writes to an array and another one that reads from it
// also checking that it supports host device annotations
template <class T, size_t N>
KOKKOS_FUNCTION constexpr void iota(Kokkos::Array<T, N>& a, T value) {
for (auto& e : a) {
e = value++;
}
}
template <class T, size_t N>
KOKKOS_FUNCTION constexpr T accumulate(Kokkos::Array<T, N> const& a, T init) {
T acc = init;
for (auto const& e : a) {
acc = acc + e;
}
return acc;
}
constexpr bool test_range_based_for_loop() {
// making sure zero-sized arrays are supported
constexpr Kokkos::Array<int, 0> a0 = [] {
Kokkos::Array<int, 0> a{};
iota(a, 1);
return a;
}();
static_assert(accumulate(a0, 0) == 0);
constexpr Kokkos::Array<int, 1> a1 = [] {
Kokkos::Array<int, 1> a{};
iota(a, 1);
return a;
}();
static_assert(accumulate(a1, 0) == 1);
constexpr Kokkos::Array<int, 2> a2 = [] {
Kokkos::Array<int, 2> a{};
iota(a, 1);
return a;
}();
static_assert(accumulate(a2, 0) == 3);
constexpr Kokkos::Array<int, 3> a3 = [] {
Kokkos::Array<int, 3> a{};
iota(a, 1);
return a;
}();
static_assert(accumulate(a3, 0) == 6);
return true;
}
static_assert(test_range_based_for_loop());
constexpr bool test_begin_end() {
constexpr Kokkos::Array<float, 0> a0{};
static_assert(begin(a0) == nullptr);
static_assert(end(a0) == nullptr);
constexpr Kokkos::Array<float, 1> a1{};
static_assert(begin(a1) == &a1[0]);
static_assert(end(a1) == &a1[0] + a1.size());
[[maybe_unused]] Kokkos::Array<double, 0> n0{};
static_assert(std::is_same_v<decltype(begin(n0)), double*>);
static_assert(std::is_same_v<decltype(end(n0)), double*>);
static_assert(std::is_same_v<double*, decltype(n0)::pointer>);
static_assert(noexcept(begin(n0)));
static_assert(noexcept(end(n0)));
[[maybe_unused]] Kokkos::Array<double, 0> const c0{};
static_assert(std::is_same_v<decltype(begin(c0)), double const*>);
static_assert(std::is_same_v<decltype(end(c0)), double const*>);
static_assert(std::is_same_v<double const*, decltype(c0)::const_pointer>);
static_assert(noexcept(begin(c0)));
static_assert(noexcept(end(c0)));
[[maybe_unused]] Kokkos::Array<double, 1> n1{};
static_assert(std::is_same_v<decltype(begin(n1)), double*>);
static_assert(std::is_same_v<decltype(end(n1)), double*>);
static_assert(std::is_same_v<double*, decltype(n1)::pointer>);
static_assert(noexcept(begin(n1)));
static_assert(noexcept(end(n1)));
[[maybe_unused]] Kokkos::Array<double, 1> const c1{};
static_assert(std::is_same_v<decltype(begin(c1)), double const*>);
static_assert(std::is_same_v<decltype(end(c1)), double const*>);
static_assert(std::is_same_v<double const*, decltype(c1)::const_pointer>);
static_assert(noexcept(begin(c1)));
static_assert(noexcept(end(c1)));
return true;
}
static_assert(test_begin_end());
constexpr bool test_array_equality_comparable() {
using C0 = Kokkos::Array<char, 0>;
using C2 = Kokkos::Array<char, 2>;
using C3 = Kokkos::Array<char, 3>;
using I0 = Kokkos::Array<int, 0>;
using I2 = Kokkos::Array<int, 2>;
using I3 = Kokkos::Array<int, 3>;
static_assert(Kokkos::is_detected_v<equality_comparable, C0, C0>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C0, C2>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C0, C3>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C0, I0>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C0, I2>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C0, I3>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C2, C0>);
static_assert(Kokkos::is_detected_v<equality_comparable, C2, C2>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C2, C3>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C2, I0>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C2, I2>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C2, I3>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C3, C0>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C3, C2>);
static_assert(Kokkos::is_detected_v<equality_comparable, C3, C3>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C3, I0>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C3, I2>);
static_assert(!Kokkos::is_detected_v<equality_comparable, C3, I3>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I0, C0>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I0, C2>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I0, C3>);
static_assert(Kokkos::is_detected_v<equality_comparable, I0, I0>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I0, I2>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I0, I3>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I2, C0>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I2, C2>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I2, C3>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I2, I0>);
static_assert(Kokkos::is_detected_v<equality_comparable, I2, I2>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I2, I3>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I3, C0>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I3, C2>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I3, C3>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I3, I0>);
static_assert(!Kokkos::is_detected_v<equality_comparable, I3, I2>);
static_assert(Kokkos::is_detected_v<equality_comparable, I3, I3>);
return true;
}
static_assert(test_array_equality_comparable());
} // namespace
|