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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright Contributors to the Kokkos project
#include <cstdio>
#include <Kokkos_Macros.hpp>
#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
import kokkos.core;
#else
#include <Kokkos_Core.hpp>
#endif
// This file is largely duplicating TestRange.hpp but it applies
// Kokkos::Experimental require at every place where a parallel
// operation is executed.
namespace Test {
namespace {
template <class ExecSpace, class ScheduleType, class Property>
struct TestRangeRequire {
using value_type = int; ///< alias required for the parallel_reduce
using view_type = Kokkos::View<int *, ExecSpace>;
view_type m_flags;
struct VerifyInitTag {};
struct ResetTag {};
struct VerifyResetTag {};
struct OffsetTag {};
struct VerifyOffsetTag {};
int N;
static const int offset = 13;
TestRangeRequire(const size_t N_)
: m_flags(Kokkos::view_alloc(Kokkos::WithoutInitializing, "flags"), N_),
N(N_) {}
void test_for() {
typename view_type::host_mirror_type host_flags =
Kokkos::create_mirror_view(m_flags);
Kokkos::parallel_for(
Kokkos::Experimental::require(
Kokkos::RangePolicy<ExecSpace, ScheduleType>(0, N), Property()),
*this);
Kokkos::parallel_for(
Kokkos::Experimental::require(
Kokkos::RangePolicy<ExecSpace, ScheduleType, VerifyInitTag>(0, N),
Property()),
*this);
Kokkos::deep_copy(host_flags, m_flags);
int error_count = 0;
for (int i = 0; i < N; ++i) {
if (int(i) != host_flags(i)) ++error_count;
}
ASSERT_EQ(error_count, int(0));
Kokkos::parallel_for(
Kokkos::Experimental::require(
Kokkos::RangePolicy<ExecSpace, ScheduleType, ResetTag>(0, N),
Property()),
*this);
Kokkos::parallel_for(
std::string("TestKernelFor"),
Kokkos::Experimental::require(
Kokkos::RangePolicy<ExecSpace, ScheduleType, VerifyResetTag>(0, N),
Property()),
*this);
Kokkos::deep_copy(host_flags, m_flags);
error_count = 0;
for (int i = 0; i < N; ++i) {
if (int(2 * i) != host_flags(i)) ++error_count;
}
ASSERT_EQ(error_count, int(0));
Kokkos::parallel_for(
Kokkos::Experimental::require(
Kokkos::RangePolicy<ExecSpace, ScheduleType, OffsetTag>(offset,
N + offset),
Property()),
*this);
Kokkos::parallel_for(
std::string("TestKernelFor"),
Kokkos::Experimental::require(
Kokkos::RangePolicy<ExecSpace, ScheduleType,
Kokkos::IndexType<unsigned int>,
VerifyOffsetTag>(0, N),
Property()),
*this);
Kokkos::deep_copy(host_flags, m_flags);
error_count = 0;
for (int i = 0; i < N; ++i) {
if (i + offset != host_flags(i)) ++error_count;
}
ASSERT_EQ(error_count, int(0));
}
KOKKOS_INLINE_FUNCTION
void operator()(const int i) const { m_flags(i) = i; }
KOKKOS_INLINE_FUNCTION
void operator()(const VerifyInitTag &, const int i) const {
if (i != m_flags(i)) {
Kokkos::printf("TestRangeRequire::test_for error at %d != %d\n", i,
m_flags(i));
}
}
KOKKOS_INLINE_FUNCTION
void operator()(const ResetTag &, const int i) const {
m_flags(i) = 2 * m_flags(i);
}
KOKKOS_INLINE_FUNCTION
void operator()(const VerifyResetTag &, const int i) const {
if (2 * i != m_flags(i)) {
Kokkos::printf("TestRangeRequire::test_for error at %d != %d\n", i,
m_flags(i));
}
}
KOKKOS_INLINE_FUNCTION
void operator()(const OffsetTag &, const int i) const {
m_flags(i - offset) = i;
}
KOKKOS_INLINE_FUNCTION
void operator()(const VerifyOffsetTag &, const int i) const {
if (i + offset != m_flags(i)) {
Kokkos::printf("TestRangeRequire::test_for error at %d != %d\n",
i + offset, m_flags(i));
}
}
//----------------------------------------
void test_reduce() {
int total = 0;
Kokkos::parallel_for(
Kokkos::Experimental::require(
Kokkos::RangePolicy<ExecSpace, ScheduleType>(0, N), Property()),
*this);
Kokkos::parallel_reduce(
"TestKernelReduce",
Kokkos::Experimental::require(
Kokkos::RangePolicy<ExecSpace, ScheduleType>(0, N), Property()),
*this, total);
// sum( 0 .. N-1 )
ASSERT_EQ(size_t((N - 1) * (N) / 2), size_t(total));
Kokkos::parallel_reduce(
Kokkos::Experimental::require(
Kokkos::RangePolicy<ExecSpace, ScheduleType, OffsetTag>(offset,
N + offset),
Property()),
*this, total);
// sum( 1 .. N )
ASSERT_EQ(size_t((N) * (N + 1) / 2), size_t(total));
}
KOKKOS_INLINE_FUNCTION
void operator()(const int i, value_type &update) const {
update += m_flags(i);
}
KOKKOS_INLINE_FUNCTION
void operator()(const OffsetTag &, const int i, value_type &update) const {
update += 1 + m_flags(i - offset);
}
//----------------------------------------
void test_dynamic_policy() {
auto const N_no_implicit_capture = N;
using policy_t =
Kokkos::RangePolicy<ExecSpace, Kokkos::Schedule<Kokkos::Dynamic> >;
int const concurrency = ExecSpace().concurrency();
{
Kokkos::View<size_t *, ExecSpace, Kokkos::MemoryTraits<Kokkos::Atomic> >
count("Count", concurrency);
Kokkos::View<int *, ExecSpace> a("A", N);
Kokkos::parallel_for(
policy_t(0, N), KOKKOS_LAMBDA(const int &i) {
for (int k = 0; k < (i < N_no_implicit_capture / 2 ? 1 : 10000);
k++) {
a(i)++;
}
count(ExecSpace::impl_hardware_thread_id())++;
});
int error = 0;
Kokkos::parallel_reduce(
Kokkos::RangePolicy<ExecSpace>(0, N),
KOKKOS_LAMBDA(const int &i, int &lsum) {
lsum += (a(i) != (i < N_no_implicit_capture / 2 ? 1 : 10000));
},
error);
ASSERT_EQ(error, 0);
if ((concurrency > 1) && (N > 4 * concurrency)) {
size_t min = N;
size_t max = 0;
for (int t = 0; t < concurrency; t++) {
if (count(t) < min) min = count(t);
if (count(t) > max) max = count(t);
}
ASSERT_LT(min, max);
// if ( concurrency > 2 ) {
// ASSERT_LT( 2 * min, max );
//}
}
}
{
Kokkos::View<size_t *, ExecSpace, Kokkos::MemoryTraits<Kokkos::Atomic> >
count("Count", concurrency);
Kokkos::View<int *, ExecSpace> a("A", N);
int sum = 0;
Kokkos::parallel_reduce(
policy_t(0, N),
KOKKOS_LAMBDA(const int &i, int &lsum) {
for (int k = 0; k < (i < N_no_implicit_capture / 2 ? 1 : 10000);
k++) {
a(i)++;
}
count(ExecSpace::impl_hardware_thread_id())++;
lsum++;
},
sum);
ASSERT_EQ(sum, N);
int error = 0;
Kokkos::parallel_reduce(
Kokkos::RangePolicy<ExecSpace>(0, N),
KOKKOS_LAMBDA(const int &i, int &lsum) {
lsum += (a(i) != (i < N_no_implicit_capture / 2 ? 1 : 10000));
},
error);
ASSERT_EQ(error, 0);
if ((concurrency > 1) && (N > 4 * concurrency)) {
size_t min = N;
size_t max = 0;
for (int t = 0; t < concurrency; t++) {
if (count(t) < min) min = count(t);
if (count(t) > max) max = count(t);
}
ASSERT_LT(min, max);
// if ( concurrency > 2 ) {
// ASSERT_LT( 2 * min, max );
//}
}
}
}
};
} // namespace
TEST(TEST_CATEGORY, range_for_require) {
using Property = Kokkos::Experimental::WorkItemProperty::HintLightWeight_t;
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Static>, Property>
f(0);
f.test_for();
}
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Dynamic>,
Property>
f(0);
f.test_for();
}
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Static>, Property>
f(2);
f.test_for();
}
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Dynamic>,
Property>
f(3);
f.test_for();
}
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Static>, Property>
f(1000);
f.test_for();
}
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Dynamic>,
Property>
f(1001);
f.test_for();
}
}
TEST(TEST_CATEGORY, range_reduce_require) {
using Property = Kokkos::Experimental::WorkItemProperty::HintLightWeight_t;
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Static>, Property>
f(0);
f.test_reduce();
}
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Dynamic>,
Property>
f(0);
f.test_reduce();
}
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Static>, Property>
f(2);
f.test_reduce();
}
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Dynamic>,
Property>
f(3);
f.test_reduce();
}
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Static>, Property>
f(1000);
f.test_reduce();
}
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Dynamic>,
Property>
f(1001);
f.test_reduce();
}
}
#ifndef KOKKOS_ENABLE_OPENMPTARGET
TEST(TEST_CATEGORY, range_dynamic_policy_require) {
#if !defined(KOKKOS_ENABLE_CUDA) && !defined(KOKKOS_ENABLE_HIP) && \
!defined(KOKKOS_ENABLE_SYCL)
using Property = Kokkos::Experimental::WorkItemProperty::HintLightWeight_t;
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Dynamic>,
Property>
f(0);
f.test_dynamic_policy();
}
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Dynamic>,
Property>
f(3);
f.test_dynamic_policy();
}
{
TestRangeRequire<TEST_EXECSPACE, Kokkos::Schedule<Kokkos::Dynamic>,
Property>
f(1001);
f.test_dynamic_policy();
}
#endif
}
#endif
} // namespace Test
|