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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
|
//@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 <sstream>
#include <iostream>
#include <limits>
#include <Kokkos_Core.hpp>
namespace Test {
namespace ReduceCombinatorical {
template <class Scalar, class Space = Kokkos::HostSpace>
struct AddPlus {
public:
// Required.
using reducer = AddPlus;
using value_type = Scalar;
using result_view_type =
Kokkos::View<value_type, Space, Kokkos::MemoryTraits<Kokkos::Unmanaged> >;
private:
result_view_type result;
public:
AddPlus(value_type& result_) : result(&result_) {}
// Required.
KOKKOS_INLINE_FUNCTION
void join(value_type& dest, const value_type& src) const { dest += src + 1; }
// Optional.
KOKKOS_INLINE_FUNCTION
void init(value_type& val) const { val = value_type(); }
KOKKOS_INLINE_FUNCTION
value_type& reference() const { return result(); }
KOKKOS_INLINE_FUNCTION
result_view_type view() const { return result; }
};
template <int ISTEAM>
struct FunctorScalar;
template <>
struct FunctorScalar<0> {
Kokkos::View<double> result;
FunctorScalar(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const int& i, double& update) const { update += i; }
};
template <>
struct FunctorScalar<1> {
using team_type = Kokkos::TeamPolicy<>::member_type;
Kokkos::View<double> result;
FunctorScalar(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const team_type& team, double& update) const {
update += 1.0 / team.team_size() * team.league_rank();
}
};
template <int ISTEAM>
struct FunctorScalarInit;
template <>
struct FunctorScalarInit<0> {
Kokkos::View<double> result;
FunctorScalarInit(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const int& i, double& update) const { update += i; }
KOKKOS_INLINE_FUNCTION
void init(double& update) const { update = 0.0; }
};
template <>
struct FunctorScalarInit<1> {
using team_type = Kokkos::TeamPolicy<>::member_type;
Kokkos::View<double> result;
FunctorScalarInit(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const team_type& team, double& update) const {
update += 1.0 / team.team_size() * team.league_rank();
}
KOKKOS_INLINE_FUNCTION
void init(double& update) const { update = 0.0; }
};
template <int ISTEAM>
struct FunctorScalarFinal;
template <>
struct FunctorScalarFinal<0> {
Kokkos::View<double> result;
FunctorScalarFinal(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const int& i, double& update) const { update += i; }
KOKKOS_INLINE_FUNCTION
void final(double& update) const { result() = update; }
};
template <>
struct FunctorScalarFinal<1> {
using team_type = Kokkos::TeamPolicy<>::member_type;
Kokkos::View<double> result;
FunctorScalarFinal(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const team_type& team, double& update) const {
update += 1.0 / team.team_size() * team.league_rank();
}
KOKKOS_INLINE_FUNCTION
void final(double& update) const { result() = update; }
};
template <int ISTEAM>
struct FunctorScalarJoin;
template <>
struct FunctorScalarJoin<0> {
Kokkos::View<double> result;
FunctorScalarJoin(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const int& i, double& update) const { update += i; }
KOKKOS_INLINE_FUNCTION
void join(double& dst, const double& update) const { dst += update; }
};
template <>
struct FunctorScalarJoin<1> {
using team_type = Kokkos::TeamPolicy<>::member_type;
Kokkos::View<double> result;
FunctorScalarJoin(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const team_type& team, double& update) const {
update += 1.0 / team.team_size() * team.league_rank();
}
KOKKOS_INLINE_FUNCTION
void join(double& dst, const double& update) const { dst += update; }
};
template <int ISTEAM>
struct FunctorScalarJoinFinal;
template <>
struct FunctorScalarJoinFinal<0> {
Kokkos::View<double> result;
FunctorScalarJoinFinal(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const int& i, double& update) const { update += i; }
KOKKOS_INLINE_FUNCTION
void join(double& dst, const double& update) const { dst += update; }
KOKKOS_INLINE_FUNCTION
void final(double& update) const { result() = update; }
};
template <>
struct FunctorScalarJoinFinal<1> {
using team_type = Kokkos::TeamPolicy<>::member_type;
Kokkos::View<double> result;
FunctorScalarJoinFinal(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const team_type& team, double& update) const {
update += 1.0 / team.team_size() * team.league_rank();
}
KOKKOS_INLINE_FUNCTION
void join(double& dst, const double& update) const { dst += update; }
KOKKOS_INLINE_FUNCTION
void final(double& update) const { result() = update; }
};
template <int ISTEAM>
struct FunctorScalarJoinInit;
template <>
struct FunctorScalarJoinInit<0> {
Kokkos::View<double> result;
FunctorScalarJoinInit(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const int& i, double& update) const { update += i; }
KOKKOS_INLINE_FUNCTION
void join(double& dst, const double& update) const { dst += update; }
KOKKOS_INLINE_FUNCTION
void init(double& update) const { update = 0.0; }
};
template <>
struct FunctorScalarJoinInit<1> {
using team_type = Kokkos::TeamPolicy<>::member_type;
Kokkos::View<double> result;
FunctorScalarJoinInit(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const team_type& team, double& update) const {
update += 1.0 / team.team_size() * team.league_rank();
}
KOKKOS_INLINE_FUNCTION
void join(double& dst, const double& update) const { dst += update; }
KOKKOS_INLINE_FUNCTION
void init(double& update) const { update = 0.0; }
};
template <int ISTEAM>
struct FunctorScalarJoinFinalInit;
template <>
struct FunctorScalarJoinFinalInit<0> {
Kokkos::View<double> result;
FunctorScalarJoinFinalInit(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const int& i, double& update) const { update += i; }
KOKKOS_INLINE_FUNCTION
void join(double& dst, const double& update) const { dst += update; }
KOKKOS_INLINE_FUNCTION
void final(double& update) const { result() = update; }
KOKKOS_INLINE_FUNCTION
void init(double& update) const { update = 0.0; }
};
template <>
struct FunctorScalarJoinFinalInit<1> {
using team_type = Kokkos::TeamPolicy<>::member_type;
Kokkos::View<double> result;
FunctorScalarJoinFinalInit(Kokkos::View<double> r) : result(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const team_type& team, double& update) const {
update += 1.0 / team.team_size() * team.league_rank();
}
KOKKOS_INLINE_FUNCTION
void join(double& dst, const double& update) const { dst += update; }
KOKKOS_INLINE_FUNCTION
void final(double& update) const { result() = update; }
KOKKOS_INLINE_FUNCTION
void init(double& update) const { update = 0.0; }
};
struct Functor1 {
KOKKOS_INLINE_FUNCTION
void operator()(const int& i, double& update) const { update += i; }
};
struct Functor2 {
using value_type = double[];
const unsigned value_count;
Functor2(unsigned n) : value_count(n) {}
KOKKOS_INLINE_FUNCTION
void operator()(const unsigned& i, double update[]) const {
for (unsigned j = 0; j < value_count; j++) {
update[j] += i;
}
}
KOKKOS_INLINE_FUNCTION
void init(double dst[]) const {
for (unsigned i = 0; i < value_count; ++i) dst[i] = 0;
}
KOKKOS_INLINE_FUNCTION
void join(double dst[], const double src[]) const {
for (unsigned i = 0; i < value_count; ++i) dst[i] += src[i];
}
};
} // namespace ReduceCombinatorical
template <class ExecSpace = Kokkos::DefaultExecutionSpace>
struct TestReduceCombinatoricalInstantiation {
template <class... Args>
static void CallParallelReduce(Args... args) {
Kokkos::parallel_reduce(args...);
}
template <class... Args>
static void AddReturnArgument(int N, Args... args) {
Kokkos::View<double, Kokkos::HostSpace> result_view("ResultViewHost");
Kokkos::View<double, ExecSpace> result_view_device("ResultViewDevice");
double expected_result = (1.0 * N) * (1.0 * N - 1.0) / 2.0;
double value = 99;
Kokkos::parallel_reduce(args..., value);
ASSERT_EQ(expected_result, value);
result_view() = 99;
CallParallelReduce(args..., result_view);
Kokkos::fence();
ASSERT_EQ(expected_result, result_view());
#ifndef KOKKOS_ENABLE_OPENMPTARGET
result_view() = 99;
CallParallelReduce(args..., result_view_device);
Kokkos::fence();
Kokkos::deep_copy(result_view, result_view_device);
ASSERT_EQ(expected_result, result_view());
#endif
value = 99;
CallParallelReduce(
args...,
Kokkos::View<double, Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged> >(&value));
Kokkos::fence();
ASSERT_EQ(expected_result, value);
result_view() = 99;
const Kokkos::View<double, Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged> >
result_view_const_um = result_view;
CallParallelReduce(args..., result_view_const_um);
Kokkos::fence();
ASSERT_EQ(expected_result, result_view_const_um());
value = 99;
// WORKAROUND OPENMPTARGET Custom Reducers not implemented
#ifndef KOKKOS_ENABLE_OPENMPTARGET
CallParallelReduce(args...,
Test::ReduceCombinatorical::AddPlus<double>(value));
if ((Kokkos::DefaultExecutionSpace().concurrency() > 1) &&
(ExecSpace().concurrency() > 1) && (expected_result > 0)) {
ASSERT_LT(expected_result, value);
} else if (((Kokkos::DefaultExecutionSpace().concurrency() > 1) ||
(ExecSpace().concurrency() > 1)) &&
(expected_result > 0)) {
ASSERT_LE(expected_result, value);
} else {
ASSERT_EQ(expected_result, value);
}
value = 99;
Test::ReduceCombinatorical::AddPlus<double> add(value);
CallParallelReduce(args..., add);
if ((Kokkos::DefaultExecutionSpace().concurrency() > 1) &&
(ExecSpace().concurrency() > 1) && (expected_result > 0)) {
ASSERT_LT(expected_result, value);
} else if (((Kokkos::DefaultExecutionSpace().concurrency() > 1) ||
(ExecSpace().concurrency() > 1)) &&
(expected_result > 0)) {
ASSERT_LE(expected_result, value);
} else {
ASSERT_EQ(expected_result, value);
}
#endif
}
template <class... Args>
static void AddLambdaRange(int N, void*, Args... args) {
AddReturnArgument(
N, args..., KOKKOS_LAMBDA(const int& i, double& lsum) { lsum += i; });
}
template <class... Args>
static void AddLambdaTeam(int N, void*, Args... args) {
AddReturnArgument(
N, args...,
KOKKOS_LAMBDA(const Kokkos::TeamPolicy<>::member_type& team,
double& update) {
update += 1.0 / team.team_size() * team.league_rank();
});
}
template <class... Args>
static void AddLambdaRange(int, Kokkos::InvalidType, Args... /*args*/) {}
template <class... Args>
static void AddLambdaTeam(int, Kokkos::InvalidType, Args... /*args*/) {}
template <int ISTEAM, class... Args>
static void AddFunctor(int N, Args... args) {
Kokkos::View<double, ExecSpace> result_view("FunctorView");
auto h_r = Kokkos::create_mirror_view(result_view);
Test::ReduceCombinatorical::FunctorScalar<ISTEAM> functor(result_view);
AddReturnArgument(N, args..., functor);
AddReturnArgument(
N, args...,
Test::ReduceCombinatorical::FunctorScalar<ISTEAM>(result_view));
// WORKAROUND OPENMPTARGET: reductions with functor join/init/final
// not implemented
#if !defined(KOKKOS_ENABLE_OPENMPTARGET)
AddReturnArgument(
N, args...,
Test::ReduceCombinatorical::FunctorScalarInit<ISTEAM>(result_view));
AddReturnArgument(
N, args...,
Test::ReduceCombinatorical::FunctorScalarJoin<ISTEAM>(result_view));
AddReturnArgument(
N, args...,
Test::ReduceCombinatorical::FunctorScalarJoinInit<ISTEAM>(result_view));
double expected_result = (1.0 * N) * (1.0 * N - 1.0) / 2.0;
h_r() = 0;
Kokkos::deep_copy(result_view, h_r);
CallParallelReduce(
args...,
Test::ReduceCombinatorical::FunctorScalarFinal<ISTEAM>(result_view));
Kokkos::fence();
Kokkos::deep_copy(h_r, result_view);
ASSERT_EQ(expected_result, h_r());
h_r() = 0;
Kokkos::deep_copy(result_view, h_r);
CallParallelReduce(
args..., Test::ReduceCombinatorical::FunctorScalarJoinFinal<ISTEAM>(
result_view));
Kokkos::fence();
Kokkos::deep_copy(h_r, result_view);
ASSERT_EQ(expected_result, h_r());
h_r() = 0;
Kokkos::deep_copy(result_view, h_r);
CallParallelReduce(
args..., Test::ReduceCombinatorical::FunctorScalarJoinFinalInit<ISTEAM>(
result_view));
Kokkos::fence();
Kokkos::deep_copy(h_r, result_view);
ASSERT_EQ(expected_result, h_r());
#endif
}
template <class... Args>
static void AddFunctorLambdaRange(int N, Args... args) {
AddFunctor<0, Args...>(N, args...);
AddLambdaRange(N,
std::conditional_t<
std::is_same_v<ExecSpace, Kokkos::DefaultExecutionSpace>,
void*, Kokkos::InvalidType>(),
args...);
}
template <class... Args>
static void AddFunctorLambdaTeam(int N, Args... args) {
AddFunctor<1, Args...>(N, args...);
AddLambdaTeam(N,
std::conditional_t<
std::is_same_v<ExecSpace, Kokkos::DefaultExecutionSpace>,
void*, Kokkos::InvalidType>(),
args...);
}
template <class... Args>
static void AddPolicy_1(int N, Args... args) {
Kokkos::RangePolicy<ExecSpace> policy(0, N);
AddFunctorLambdaRange(1000, args..., 1000);
AddFunctorLambdaRange(N, args..., N);
AddFunctorLambdaRange(N, args..., policy);
}
template <class... Args>
static void AddPolicy_2(int N, Args... args) {
AddFunctorLambdaRange(N, args..., Kokkos::RangePolicy<ExecSpace>(0, N));
AddFunctorLambdaRange(
N, args...,
Kokkos::RangePolicy<ExecSpace, Kokkos::Schedule<Kokkos::Dynamic> >(0,
N));
AddFunctorLambdaRange(
N, args...,
Kokkos::RangePolicy<ExecSpace, Kokkos::Schedule<Kokkos::Static> >(0, N)
.set_chunk_size(16));
AddFunctorLambdaRange(
N, args...,
Kokkos::RangePolicy<ExecSpace, Kokkos::Schedule<Kokkos::Dynamic> >(0, N)
.set_chunk_size(16));
}
template <class... Args>
static void AddPolicy_3(int N, Args... args) {
AddFunctorLambdaTeam(N, args...,
Kokkos::TeamPolicy<ExecSpace>(N, Kokkos::AUTO));
AddFunctorLambdaTeam(
N, args...,
Kokkos::TeamPolicy<ExecSpace, Kokkos::Schedule<Kokkos::Dynamic> >(
N, Kokkos::AUTO));
AddFunctorLambdaTeam(
N, args...,
Kokkos::TeamPolicy<ExecSpace, Kokkos::Schedule<Kokkos::Static> >(
N, Kokkos::AUTO)
.set_chunk_size(16));
AddFunctorLambdaTeam(
N, args...,
Kokkos::TeamPolicy<ExecSpace, Kokkos::Schedule<Kokkos::Dynamic> >(
N, Kokkos::AUTO)
.set_chunk_size(16));
}
static void execute_a1() { AddPolicy_1(1000); }
static void execute_b1() {
std::string s("Std::String");
AddPolicy_1(1000, s.c_str());
AddPolicy_1(1000, "Char Constant");
#ifndef KOKKOS_ENABLE_OPENMPTARGET
AddPolicy_1(0, "Char Constant");
#endif
}
static void execute_c1() {
std::string s("Std::String");
AddPolicy_1(1000, s);
}
static void execute_a2() { AddPolicy_2(1000); }
static void execute_b2() {
std::string s("Std::String");
AddPolicy_2(1000, s.c_str());
AddPolicy_2(1000, "Char Constant");
#ifndef KOKKOS_ENABLE_OPENMPTARGET
AddPolicy_2(0, "Char Constant");
#endif
}
static void execute_c2() {
std::string s("Std::String");
AddPolicy_2(1000, s);
}
static void execute_a3() {
#ifndef KOKKOS_ENABLE_OPENMPTARGET
AddPolicy_3(1000);
#endif
}
static void execute_b3() {
#ifndef KOKKOS_ENABLE_OPENMPTARGET
std::string s("Std::String");
AddPolicy_3(1000, s.c_str());
AddPolicy_3(1000, "Char Constant");
AddPolicy_3(0, "Char Constant");
#endif
}
static void execute_c3() {
#ifndef KOKKOS_ENABLE_OPENMPTARGET
std::string s("Std::String");
AddPolicy_3(1000, s);
#endif
}
};
} // namespace Test
|