forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestMDRangePolicyConstructors.hpp
199 lines (169 loc) · 7.37 KB
/
TestMDRangePolicyConstructors.hpp
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
//@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>
#include <regex>
namespace {
template <class IndexType>
void construct_mdrange_policy_variable_type() {
(void)Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>>{
Kokkos::Array<IndexType, 2>{}, Kokkos::Array<IndexType, 2>{}};
(void)Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>>{
{{IndexType(0), IndexType(0)}}, {{IndexType(2), IndexType(2)}}};
(void)Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>>{
{IndexType(0), IndexType(0)}, {IndexType(2), IndexType(2)}};
}
TEST(TEST_CATEGORY, md_range_policy_construction_from_arrays) {
{
// Check that construction from Kokkos::Array of the specified index type
// works.
using IndexType = unsigned long long;
Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>,
Kokkos::IndexType<IndexType>>
p1(Kokkos::Array<IndexType, 2>{{0, 1}},
Kokkos::Array<IndexType, 2>{{2, 3}});
Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>,
Kokkos::IndexType<IndexType>>
p2(Kokkos::Array<IndexType, 2>{{0, 1}},
Kokkos::Array<IndexType, 2>{{2, 3}});
Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>,
Kokkos::IndexType<IndexType>>
p3(Kokkos::Array<IndexType, 2>{{0, 1}},
Kokkos::Array<IndexType, 2>{{2, 3}},
Kokkos::Array<IndexType, 1>{{4}});
}
{
// Check that construction from double-braced initializer list
// works.
using index_type = unsigned long long;
Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>> p1({{0, 1}},
{{2, 3}});
Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>,
Kokkos::IndexType<index_type>>
p2({{0, 1}}, {{2, 3}});
}
{
// Check that construction from Kokkos::Array of long compiles for backwards
// compability. This was broken in
// https://github.com/kokkos/kokkos/pull/3527/commits/88ea8eec6567c84739d77bdd25fdbc647fae28bb#r512323639
Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>> p1(
Kokkos::Array<long, 2>{{0, 1}}, Kokkos::Array<long, 2>{{2, 3}});
Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>> p2(
Kokkos::Array<long, 2>{{0, 1}}, Kokkos::Array<long, 2>{{2, 3}});
Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>> p3(
Kokkos::Array<long, 2>{{0, 1}}, Kokkos::Array<long, 2>{{2, 3}},
Kokkos::Array<long, 1>{{4}});
}
// Check that construction from various index types works.
construct_mdrange_policy_variable_type<char>();
construct_mdrange_policy_variable_type<int>();
construct_mdrange_policy_variable_type<unsigned long>();
construct_mdrange_policy_variable_type<std::int64_t>();
}
#ifndef KOKKOS_ENABLE_OPENMPTARGET // FIXME_OPENMPTARGET
TEST(TEST_CATEGORY_DEATH, policy_bounds_unsafe_narrowing_conversions) {
using Policy = Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>,
Kokkos::IndexType<unsigned>>;
::testing::FLAGS_gtest_death_test_style = "threadsafe";
ASSERT_DEATH(
{
(void)Policy({-1, 0}, {2, 3});
},
"unsafe narrowing conversion");
}
TEST(TEST_CATEGORY_DEATH, policy_invalid_bounds) {
using Policy = Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>>;
::testing::FLAGS_gtest_death_test_style = "threadsafe";
auto [dim0, dim1] = (Policy::inner_direction == Kokkos::Iterate::Right)
? std::make_pair(1, 0)
: std::make_pair(0, 1);
std::string msg1 =
"Kokkos::MDRangePolicy bounds error: The lower bound (100) is greater "
"than its upper bound (90) in dimension " +
std::to_string(dim0) + ".\n";
std::string msg2 =
"Kokkos::MDRangePolicy bounds error: The lower bound (100) is greater "
"than its upper bound (90) in dimension " +
std::to_string(dim1) + ".\n";
#if !defined(KOKKOS_ENABLE_DEPRECATED_CODE_4)
// escape the parentheses in the regex to match the error message
msg1 = std::regex_replace(msg1, std::regex("\\(|\\)"), "\\$&");
(void)msg2;
ASSERT_DEATH({ (void)Policy({100, 100}, {90, 90}); }, msg1);
#else
if (!Kokkos::show_warnings()) {
GTEST_SKIP() << "Kokkos warning messages are disabled";
}
::testing::internal::CaptureStderr();
(void)Policy({100, 100}, {90, 90});
#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
ASSERT_EQ(::testing::internal::GetCapturedStderr(), msg1 + msg2);
#else
ASSERT_TRUE(::testing::internal::GetCapturedStderr().empty());
(void)msg1;
(void)msg2;
#endif
#endif
}
#endif
TEST(TEST_CATEGORY, policy_get_tile_size) {
constexpr int rank = 3;
using Policy = Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<rank>>;
using tile_type = typename Policy::tile_type;
std::size_t last_rank =
(Policy::inner_direction == Kokkos::Iterate::Right) ? rank - 1 : 0;
auto default_size_properties =
Kokkos::Impl::get_tile_size_properties(TEST_EXECSPACE());
{
int dim_length = 100;
Policy policy({0, 0, 0}, {dim_length, dim_length, dim_length},
tile_type{{2, 4, 16}});
auto rec_tile_sizes = policy.tile_size_recommended();
auto rec_largest_tile_sizes = policy.largest_tile_size_recommended();
EXPECT_EQ(default_size_properties.max_total_tile_size,
policy.max_total_tile_size());
int prod_rec_tile_size = 1;
for (std::size_t i = 0; i < rank; ++i) {
EXPECT_GT(rec_tile_sizes[i], 0)
<< " invalid default tile size for rank " << i;
EXPECT_LE(rec_tile_sizes[i], rec_largest_tile_sizes[i])
<< " invalid default tile size for rank " << i;
if (default_size_properties.default_largest_tile_size == 0) {
EXPECT_EQ(dim_length, rec_largest_tile_sizes[i])
<< " incorrect recommended largest tile size returned for rank "
<< i;
auto expected_rec_tile_size =
(i == last_rank) ? dim_length
: default_size_properties.default_tile_size;
EXPECT_EQ(expected_rec_tile_size, rec_tile_sizes[i])
<< " incorrect recommended tile size returned for rank " << i;
} else {
EXPECT_EQ(default_size_properties.default_largest_tile_size,
rec_largest_tile_sizes[i])
<< " incorrect recommended largest tile size returned for rank "
<< i;
auto expected_rec_tile_size =
(i == last_rank) ? default_size_properties.default_largest_tile_size
: default_size_properties.default_tile_size;
EXPECT_EQ(expected_rec_tile_size, rec_tile_sizes[i])
<< " incorrect recommended tile size returned for rank " << i;
}
prod_rec_tile_size *= rec_tile_sizes[i];
}
EXPECT_LT(prod_rec_tile_size, policy.max_total_tile_size());
}
}
} // namespace