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
|
//@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 <gtest/gtest.h>
#include <Kokkos_Core.hpp>
namespace Test {
template <int rank, int dynrank, class RankType, std::size_t... Is>
void test_matching_arguments_rank_helper(std::index_sequence<Is...>) {
constexpr int nargs = sizeof...(Is);
using view_type = Kokkos::View<RankType>;
if (nargs == rank || nargs == dynrank) {
{ // does not throw
view_type v("v", ((Is * 0) + 1)...);
}
{ // does not throw
view_type v(nullptr, ((Is * 0) + 1)...);
}
} else {
ASSERT_DEATH(
{ view_type v("v", ((Is * 0) + 1)...); },
"Constructor for Kokkos::View 'v' has mismatched number of arguments. "
"The number of arguments = " +
std::to_string(nargs) +
" neither matches the dynamic rank = " + std::to_string(dynrank) +
" nor the total rank = " + std::to_string(rank));
ASSERT_DEATH(
{ view_type v(nullptr, ((Is * 0) + 1)...); },
"Constructor for Kokkos::View 'UNMANAGED' has mismatched number of "
"arguments. "
"The number of arguments = " +
std::to_string(nargs) +
" neither matches the dynamic rank = " + std::to_string(dynrank) +
" nor the total rank = " + std::to_string(rank));
}
}
template <int rank, int dynrank, template <int> class RankType>
void test_matching_arguments_rank() {
test_matching_arguments_rank_helper<rank, dynrank,
typename RankType<rank>::type>(
std::make_index_sequence<0>());
test_matching_arguments_rank_helper<rank, dynrank,
typename RankType<rank>::type>(
std::make_index_sequence<1>());
test_matching_arguments_rank_helper<rank, dynrank,
typename RankType<rank>::type>(
std::make_index_sequence<2>());
test_matching_arguments_rank_helper<rank, dynrank,
typename RankType<rank>::type>(
std::make_index_sequence<3>());
test_matching_arguments_rank_helper<rank, dynrank,
typename RankType<rank>::type>(
std::make_index_sequence<4>());
test_matching_arguments_rank_helper<rank, dynrank,
typename RankType<rank>::type>(
std::make_index_sequence<5>());
test_matching_arguments_rank_helper<rank, dynrank,
typename RankType<rank>::type>(
std::make_index_sequence<6>());
test_matching_arguments_rank_helper<rank, dynrank,
typename RankType<rank>::type>(
std::make_index_sequence<7>());
test_matching_arguments_rank_helper<rank, dynrank,
typename RankType<rank>::type>(
std::make_index_sequence<8>());
}
template <int rank>
struct DynamicRank {
using type = typename DynamicRank<rank - 1>::type*;
};
template <>
struct DynamicRank<0> {
using type = int;
};
#ifdef KOKKOS_COMPILER_NVHPC
#define VIEW_CTOR_TEST_UNREACHABLE() __builtin_unreachable()
#else
#define VIEW_CTOR_TEST_UNREACHABLE() static_assert(true)
#endif
// Skip test execution when KOKKOS_ENABLE_OPENMPTARGET is enabled until
// Kokkos::abort() aborts properly on that backend
#ifndef KOKKOS_ENABLE_OPENMPTARGET
TEST(TEST_CATEGORY_DEATH, view_construction_with_wrong_params_dyn) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
#ifndef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
GTEST_SKIP() << "only enforced when debug bound checks is enabled";
VIEW_CTOR_TEST_UNREACHABLE();
#endif
test_matching_arguments_rank<0, 0, DynamicRank>(); // dim = 0, dynamic = 0
test_matching_arguments_rank<1, 1, DynamicRank>(); // dim = 1, dynamic = 1
test_matching_arguments_rank<2, 2, DynamicRank>(); // dim = 2, dynamic = 2
test_matching_arguments_rank<3, 3, DynamicRank>(); // dim = 3, dynamic = 3
test_matching_arguments_rank<4, 4, DynamicRank>(); // dim = 4, dynamic = 4
test_matching_arguments_rank<5, 5, DynamicRank>(); // dim = 5, dynamic = 5
test_matching_arguments_rank<6, 6, DynamicRank>(); // dim = 6, dynamic = 6
test_matching_arguments_rank<7, 7, DynamicRank>(); // dim = 7, dynamic = 7
test_matching_arguments_rank<8, 8, DynamicRank>(); // dim = 8, dynamic = 8
}
template <int rank>
struct StaticRank {
using type = typename StaticRank<rank - 1>::type[1];
};
template <>
struct StaticRank<0> {
using type = int;
};
TEST(TEST_CATEGORY_DEATH, view_construction_with_wrong_params_stat) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
#ifndef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
GTEST_SKIP() << "only enforced when debug bound checks is enabled";
VIEW_CTOR_TEST_UNREACHABLE();
#endif
test_matching_arguments_rank<0, 0, StaticRank>(); // dim = 0, dynamic = 0
test_matching_arguments_rank<1, 0, StaticRank>(); // dim = 1, dynamic = 0
test_matching_arguments_rank<2, 0, StaticRank>(); // dim = 2, dynamic = 0
test_matching_arguments_rank<3, 0, StaticRank>(); // dim = 3, dynamic = 0
test_matching_arguments_rank<4, 0, StaticRank>(); // dim = 4, dynamic = 0
test_matching_arguments_rank<5, 0, StaticRank>(); // dim = 5, dynamic = 0
test_matching_arguments_rank<6, 0, StaticRank>(); // dim = 6, dynamic = 0
test_matching_arguments_rank<7, 0, StaticRank>(); // dim = 7, dynamic = 0
test_matching_arguments_rank<8, 0, StaticRank>(); // dim = 8, dynamic = 0
}
template <int rank>
struct MixedRank {
using type = typename DynamicRank<rank - 1>::type[1];
};
template <>
struct MixedRank<0> {
using type = int;
};
TEST(TEST_CATEGORY_DEATH, view_construction_with_wrong_params_mix) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
#ifndef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
GTEST_SKIP() << "only enforced when debug bound checks is enabled";
VIEW_CTOR_TEST_UNREACHABLE();
#endif
test_matching_arguments_rank<0, 0, MixedRank>(); // dim = 0, dynamic = 0
test_matching_arguments_rank<1, 0, MixedRank>(); // dim = 1, dynamic = 0
test_matching_arguments_rank<2, 1, MixedRank>(); // dim = 2, dynamic = 1
test_matching_arguments_rank<3, 2, MixedRank>(); // dim = 3, dynamic = 2
test_matching_arguments_rank<4, 3, MixedRank>(); // dim = 4, dynamic = 3
test_matching_arguments_rank<5, 4, MixedRank>(); // dim = 5, dynamic = 4
test_matching_arguments_rank<6, 5, MixedRank>(); // dim = 6, dynamic = 5
test_matching_arguments_rank<7, 6, MixedRank>(); // dim = 7, dynamic = 6
test_matching_arguments_rank<8, 7, MixedRank>(); // dim = 8, dynamic = 7
}
#define CHECK_DEATH(EXPR) \
ASSERT_DEATH(EXPR, \
"The specified run-time extent for Kokkos::View 'v' does not " \
"match the compile-time extent in dimension 0. The given " \
"extent is 2 but should be 1.")
#define CHECK_DEATH_UNMANAGED(EXPR) \
ASSERT_DEATH( \
EXPR, \
"The specified run-time extent for Kokkos::View 'UNMANAGED' does not " \
"match the compile-time extent in dimension 0. The given " \
"extent is 2 but should be 1.")
TEST(TEST_CATEGORY_DEATH, view_construction_with_wrong_static_extents) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
#ifndef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
GTEST_SKIP() << "only enforced when debug bound checks is enabled";
VIEW_CTOR_TEST_UNREACHABLE();
#endif
// clang-format off
CHECK_DEATH({ Kokkos::View<int[1]> v("v", 2); });
CHECK_DEATH({ Kokkos::View<int[1][1]> v("v", 2, 1); });
CHECK_DEATH({ Kokkos::View<int[1][1][1]> v("v", 2, 1, 1); });
CHECK_DEATH({ Kokkos::View<int[1][1][1][1]> v("v", 2, 1, 1, 1); });
CHECK_DEATH({ Kokkos::View<int[1][1][1][1][1]> v("v", 2, 1, 1, 1, 1); });
CHECK_DEATH({ Kokkos::View<int[1][1][1][1][1][1]> v("v", 2, 1, 1, 1, 1, 1); });
CHECK_DEATH({ Kokkos::View<int[1][1][1][1][1][1][1]> v("v", 2, 1, 1, 1, 1, 1, 1); });
CHECK_DEATH({ Kokkos::View<int[1][1][1][1][1][1][1][1]> v("v", 2, 1, 1, 1, 1, 1, 1, 1); });
CHECK_DEATH_UNMANAGED({ Kokkos::View<int[1]> v(nullptr, 2); });
CHECK_DEATH_UNMANAGED({ Kokkos::View<int[1][1]> v(nullptr, 2, 1); });
CHECK_DEATH_UNMANAGED({ Kokkos::View<int[1][1][1]> v(nullptr, 2, 1, 1); });
CHECK_DEATH_UNMANAGED({ Kokkos::View<int[1][1][1][1]> v(nullptr, 2, 1, 1, 1); });
CHECK_DEATH_UNMANAGED({ Kokkos::View<int[1][1][1][1][1]> v(nullptr, 2, 1, 1, 1, 1); });
CHECK_DEATH_UNMANAGED({ Kokkos::View<int[1][1][1][1][1][1]> v(nullptr, 2, 1, 1, 1, 1, 1); });
CHECK_DEATH_UNMANAGED({ Kokkos::View<int[1][1][1][1][1][1][1]> v(nullptr, 2, 1, 1, 1, 1, 1, 1); });
CHECK_DEATH_UNMANAGED({ Kokkos::View<int[1][1][1][1][1][1][1][1]> v(nullptr, 2, 1, 1, 1, 1, 1, 1, 1); });
// clang-format on
}
#undef CHECK_DEATH
#undef CHECK_DEATH_UNMANAGED
#endif // KOKKOS_ENABLE_OPENMPTARGET
#undef VIEW_CTOR_TEST_UNREACHABLE
} // namespace Test
|