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
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright Contributors to the Kokkos project
#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
#include <Kokkos_Macros.hpp>
static_assert(false,
"Including non-public Kokkos header files is not allowed.");
#endif
#ifndef KOKKOS_PARALLEL_REDUCE_HPP
#define KOKKOS_PARALLEL_REDUCE_HPP
#include <impl/Kokkos_BuiltinReducers.hpp>
#include <Kokkos_CheckUsage.hpp>
#include <Kokkos_ExecPolicy.hpp>
#include <Kokkos_View.hpp>
#include <impl/Kokkos_FunctorAnalysis.hpp>
#include <impl/Kokkos_Tools_Generic.hpp>
#include <type_traits>
namespace Kokkos {
namespace Impl {
template <typename FunctorType, typename FunctorAnalysisReducerType,
typename Enable>
class CombinedFunctorReducer {
public:
using functor_type = FunctorType;
using reducer_type = FunctorAnalysisReducerType;
CombinedFunctorReducer(const FunctorType& functor,
const FunctorAnalysisReducerType& reducer)
: m_functor(functor), m_reducer(reducer) {}
KOKKOS_FUNCTION const FunctorType& get_functor() const { return m_functor; }
KOKKOS_FUNCTION const FunctorAnalysisReducerType& get_reducer() const {
return m_reducer;
}
private:
FunctorType m_functor;
FunctorAnalysisReducerType m_reducer;
};
template <typename FunctorType, typename FunctorAnalysisReducerType>
class CombinedFunctorReducer<
FunctorType, FunctorAnalysisReducerType,
std::enable_if_t<std::is_same_v<
FunctorType, typename FunctorAnalysisReducerType::functor_type>>> {
public:
using functor_type = FunctorType;
using reducer_type = FunctorAnalysisReducerType;
CombinedFunctorReducer(const FunctorType& functor,
const FunctorAnalysisReducerType&)
: m_reducer(functor) {}
KOKKOS_FUNCTION const FunctorType& get_functor() const {
return m_reducer.get_functor();
}
KOKKOS_FUNCTION const FunctorAnalysisReducerType& get_reducer() const {
return m_reducer;
}
private:
FunctorAnalysisReducerType m_reducer;
};
template <class T, class ReturnType, class ValueTraits>
struct ParallelReduceReturnValue;
template <class ReturnType, class FunctorType>
struct ParallelReduceReturnValue<
std::enable_if_t<Kokkos::is_view<ReturnType>::value>, ReturnType,
FunctorType> {
using return_type = ReturnType;
using reducer_type = InvalidType;
using value_type_scalar = typename return_type::value_type;
using value_type_array = typename return_type::value_type* const;
using value_type = std::conditional_t<return_type::rank == 0,
value_type_scalar, value_type_array>;
static return_type& return_value(ReturnType& return_val, const FunctorType&) {
return return_val; // NOLINT(bugprone-return-const-ref-from-parameter)
}
};
template <class ReturnType, class FunctorType>
struct ParallelReduceReturnValue<
std::enable_if_t<!Kokkos::is_view<ReturnType>::value &&
(!std::is_array_v<ReturnType> &&
!std::is_pointer_v<
ReturnType>)&&!Kokkos::is_reducer<ReturnType>::value>,
ReturnType, FunctorType> {
using return_type =
Kokkos::View<ReturnType, Kokkos::HostSpace, Kokkos::MemoryUnmanaged>;
using reducer_type = InvalidType;
using value_type = typename return_type::value_type;
static return_type return_value(ReturnType& return_val, const FunctorType&) {
return return_type(&return_val);
}
};
template <class ReturnType, class FunctorType>
struct ParallelReduceReturnValue<
std::enable_if_t<(std::is_array_v<ReturnType> ||
std::is_pointer_v<ReturnType>)>,
ReturnType, FunctorType> {
using return_type = Kokkos::View<std::remove_const_t<ReturnType>,
Kokkos::HostSpace, Kokkos::MemoryUnmanaged>;
using reducer_type = InvalidType;
using value_type = typename return_type::value_type[];
static return_type return_value(ReturnType& return_val,
const FunctorType& functor) {
if (std::is_array_v<ReturnType>)
return return_type(return_val);
else
return return_type(return_val, functor.value_count);
}
};
template <class ReturnType, class FunctorType>
struct ParallelReduceReturnValue<
std::enable_if_t<Kokkos::is_reducer<ReturnType>::value>, ReturnType,
FunctorType> {
using return_type = typename ReturnType::result_view_type;
using reducer_type = ReturnType;
using value_type = typename return_type::value_type;
static auto return_value(ReturnType& return_val, const FunctorType&) {
return return_val.view();
}
};
template <class T, class ReturnType, class FunctorType>
struct ParallelReducePolicyType;
template <class PolicyType, class FunctorType>
struct ParallelReducePolicyType<
std::enable_if_t<Kokkos::is_execution_policy<PolicyType>::value>,
PolicyType, FunctorType> {
using policy_type = PolicyType;
static PolicyType policy(const PolicyType& policy_) { return policy_; }
};
template <class PolicyType, class FunctorType>
struct ParallelReducePolicyType<
std::enable_if_t<std::is_integral_v<PolicyType>>, PolicyType, FunctorType> {
using execution_space =
typename Impl::FunctorPolicyExecutionSpace<FunctorType,
void>::execution_space;
using policy_type = Kokkos::RangePolicy<execution_space>;
static policy_type policy(const PolicyType& policy_) {
return policy_type(0, policy_);
}
};
template <class PolicyType, class FunctorType, class ReturnType>
struct ParallelReduceAdaptor {
using return_value_adapter =
Impl::ParallelReduceReturnValue<void, ReturnType, FunctorType>;
// Equivalent to std::get<I>(std::tuple) but callable on the device.
template <bool B, class T1, class T2>
static KOKKOS_FUNCTION std::conditional_t<B, T1&&, T2&&> forwarding_switch(
T1&& v1, T2&& v2) {
if constexpr (B)
return static_cast<T1&&>(v1);
else
return static_cast<T2&&>(v2);
}
static inline void execute_impl(const std::string& label,
const PolicyType& policy,
const FunctorType& functor,
ReturnType& return_value) {
using PassedReducerType = typename return_value_adapter::reducer_type;
uint64_t kpID = 0;
constexpr bool passed_reducer_type_is_invalid =
std::is_same_v<InvalidType, PassedReducerType>;
using TheReducerType = std::conditional_t<passed_reducer_type_is_invalid,
FunctorType, PassedReducerType>;
using Analysis = FunctorAnalysis<FunctorPatternInterface::REDUCE,
PolicyType, TheReducerType,
typename return_value_adapter::value_type>;
using CombinedFunctorReducerType =
CombinedFunctorReducer<FunctorType, typename Analysis::Reducer>;
CombinedFunctorReducerType functor_reducer(
functor, typename Analysis::Reducer(
forwarding_switch<passed_reducer_type_is_invalid>(
functor, return_value)));
const auto& response = Kokkos::Tools::Impl::begin_parallel_reduce<
typename return_value_adapter::reducer_type>(policy, functor_reducer,
label, kpID);
const auto& inner_policy = response.policy;
auto closure = construct_with_shared_allocation_tracking_disabled<
Impl::ParallelReduce<CombinedFunctorReducerType, PolicyType,
typename Impl::FunctorPolicyExecutionSpace<
FunctorType, PolicyType>::execution_space>>(
functor_reducer, inner_policy,
return_value_adapter::return_value(return_value, functor));
closure.execute();
Kokkos::Tools::Impl::end_parallel_reduce<PassedReducerType>(
inner_policy, functor, label, kpID);
}
static constexpr bool is_array_reduction =
Impl::FunctorAnalysis<
Impl::FunctorPatternInterface::REDUCE, PolicyType, FunctorType,
typename return_value_adapter::value_type>::StaticValueSize == 0;
template <typename Dummy = ReturnType>
static inline std::enable_if_t<!(is_array_reduction &&
std::is_pointer_v<Dummy>)>
execute(const std::string& label, const PolicyType& policy,
const FunctorType& functor, ReturnType& return_value) {
execute_impl(label, policy, functor, return_value);
}
};
} // namespace Impl
//----------------------------------------------------------------------------
/*! \fn void parallel_reduce(label,policy,functor,return_argument)
\brief Perform a parallel reduction.
\param label An optional Label giving the call name. Must be able to
construct a std::string from the argument. \param policy A Kokkos Execution
Policy, such as an integer, a RangePolicy or a TeamPolicy. \param functor A
functor with a reduction operator, and optional init, join and final
functions. \param return_argument A return argument which can be a scalar, a
View, or a ReducerStruct. This argument can be left out if the functor has a
final function.
*/
// Parallel Reduce Blocking behavior
namespace Impl {
template <typename T>
struct ReducerHasTestReferenceFunction {
template <typename E>
static std::true_type test_func(decltype(&E::references_scalar));
template <typename E>
static std::false_type test_func(...);
enum {
value = std::is_same_v<std::true_type, decltype(test_func<T>(nullptr))>
};
};
template <class ExecutionSpace, class Arg>
constexpr std::enable_if_t<
// constraints only necessary because SFINAE lacks subsumption
!ReducerHasTestReferenceFunction<Arg>::value &&
!Kokkos::is_view<Arg>::value,
// return type:
bool>
parallel_reduce_needs_fence(ExecutionSpace const&, Arg const&) {
return true;
}
template <class ExecutionSpace, class Reducer>
constexpr std::enable_if_t<
// equivalent to:
// (requires (Reducer const& r) {
// { reducer.references_scalar() } -> std::convertible_to<bool>;
// })
ReducerHasTestReferenceFunction<Reducer>::value,
// return type:
bool>
parallel_reduce_needs_fence(ExecutionSpace const&, Reducer const& reducer) {
return reducer.references_scalar();
}
template <class ExecutionSpace, class ViewLike>
constexpr std::enable_if_t<
// requires Kokkos::ViewLike<ViewLike>
Kokkos::is_view<ViewLike>::value,
// return type:
bool>
parallel_reduce_needs_fence(ExecutionSpace const&, ViewLike const&) {
return false;
}
template <class ExecutionSpace, class... Args>
struct ParallelReduceFence {
template <class... ArgsDeduced>
static void fence(const ExecutionSpace& ex, const std::string& name,
ArgsDeduced&&... args) {
if (Impl::parallel_reduce_needs_fence(ex, (ArgsDeduced&&)args...)) {
ex.fence(name);
}
}
};
} // namespace Impl
/** \brief Parallel reduction
*
* parallel_reduce performs parallel reductions with arbitrary functions - i.e.
* it is not solely data based. The call expects up to 4 arguments:
*
*
* Example of a parallel_reduce functor for a POD (plain old data) value type:
* \code
* class FunctorType { // For POD value type
* public:
* using execution_space = ...;
* using value_type = <podType>;
* void operator()( <intType> iwork , <podType> & update ) const ;
* void init( <podType> & update ) const ;
* void join( <podType> & update ,
* const <podType> & input ) const ;
*
* void final( <podType> & update ) const ;
* };
* \endcode
*
* Example of a parallel_reduce functor for an array of POD (plain old data)
* values:
* \code
* class FunctorType { // For array of POD value
* public:
* using execution_space = ...;
* using value_type = <podType>[];
* void operator()( <intType> , <podType> update[] ) const ;
* void init( <podType> update[] ) const ;
* void join( <podType> update[] ,
* const <podType> input[] ) const ;
*
* void final( <podType> update[] ) const ;
* };
* \endcode
*/
// ReturnValue is scalar or array: take by reference
template <class PolicyType, class FunctorType, class ReturnType>
inline std::enable_if_t<Kokkos::is_execution_policy<PolicyType>::value &&
!(Kokkos::is_view<ReturnType>::value ||
Kokkos::is_reducer<ReturnType>::value ||
std::is_pointer_v<ReturnType>)>
parallel_reduce(const std::string& label, const PolicyType& policy,
const FunctorType& functor, ReturnType& return_value) {
/** Enforce correct use **/
Impl::CheckUsage<Impl::UsageRequires::insideExecEnv>::check(
"parallel_reduce", policy, label.c_str());
static_assert(
!std::is_const_v<ReturnType>,
"A const reduction result type is only allowed for a View, pointer or "
"reducer return type!");
Impl::ParallelReduceAdaptor<PolicyType, FunctorType, ReturnType>::execute(
label, policy, functor, return_value);
Impl::ParallelReduceFence<typename PolicyType::execution_space, ReturnType>::
fence(
policy.space(),
"Kokkos::parallel_reduce: fence due to result being value, not view",
return_value);
}
template <class PolicyType, class FunctorType, class ReturnType>
inline std::enable_if_t<Kokkos::is_execution_policy<PolicyType>::value &&
!(Kokkos::is_view<ReturnType>::value ||
Kokkos::is_reducer<ReturnType>::value ||
std::is_pointer_v<ReturnType>)>
parallel_reduce(const PolicyType& policy, const FunctorType& functor,
ReturnType& return_value) {
/** Enforce correct use **/
Impl::CheckUsage<Impl::UsageRequires::insideExecEnv>::check("parallel_reduce",
policy);
parallel_reduce("", policy, functor, return_value);
}
template <class FunctorType, class ReturnType>
inline std::enable_if_t<!(Kokkos::is_view<ReturnType>::value ||
Kokkos::is_reducer<ReturnType>::value ||
std::is_pointer_v<ReturnType>)>
parallel_reduce(const std::string& label, const size_t& work_count,
const FunctorType& functor, ReturnType& return_value) {
/** Enforce correct use **/
Impl::CheckUsage<Impl::UsageRequires::insideExecEnv>::check(
"parallel_reduce", work_count, label.c_str());
using policy_type =
typename Impl::ParallelReducePolicyType<void, size_t,
FunctorType>::policy_type;
parallel_reduce(label, policy_type(0, work_count), functor, return_value);
}
template <class FunctorType, class ReturnType>
inline std::enable_if_t<!(Kokkos::is_view<ReturnType>::value ||
Kokkos::is_reducer<ReturnType>::value ||
std::is_pointer_v<ReturnType>)>
parallel_reduce(const size_t& work_count, const FunctorType& functor,
ReturnType& return_value) {
/** Enforce correct use **/
Impl::CheckUsage<Impl::UsageRequires::insideExecEnv>::check("parallel_reduce",
work_count);
parallel_reduce("", work_count, functor, return_value);
}
// ReturnValue as View or Reducer: take by copy to allow for inline construction
template <class PolicyType, class FunctorType, class ReturnType>
inline std::enable_if_t<Kokkos::is_execution_policy<PolicyType>::value &&
(Kokkos::is_view<ReturnType>::value ||
Kokkos::is_reducer<ReturnType>::value ||
std::is_pointer_v<ReturnType>)>
parallel_reduce(const std::string& label, const PolicyType& policy,
const FunctorType& functor, const ReturnType& return_value) {
/** Enforce correct use **/
Impl::CheckUsage<Impl::UsageRequires::insideExecEnv>::check(
"parallel_reduce", policy, label.c_str());
ReturnType return_value_impl = return_value;
Impl::ParallelReduceAdaptor<PolicyType, FunctorType, ReturnType>::execute(
label, policy, functor, return_value_impl);
Impl::ParallelReduceFence<typename PolicyType::execution_space, ReturnType>::
fence(policy.space(),
"Kokkos::parallel_reduce: fence" /*FIXME: describe correct reason*/,
return_value);
}
template <class PolicyType, class FunctorType, class ReturnType>
inline std::enable_if_t<Kokkos::is_execution_policy<PolicyType>::value &&
(Kokkos::is_view<ReturnType>::value ||
Kokkos::is_reducer<ReturnType>::value ||
std::is_pointer_v<ReturnType>)>
parallel_reduce(const PolicyType& policy, const FunctorType& functor,
const ReturnType& return_value) {
/** Enforce correct use **/
Impl::CheckUsage<Impl::UsageRequires::insideExecEnv>::check("parallel_reduce",
policy);
parallel_reduce("", policy, functor, return_value);
}
template <class FunctorType, class ReturnType>
inline std::enable_if_t<Kokkos::is_view<ReturnType>::value ||
Kokkos::is_reducer<ReturnType>::value ||
std::is_pointer_v<ReturnType>>
parallel_reduce(const std::string& label, const size_t& work_count,
const FunctorType& functor, const ReturnType& return_value) {
/** Enforce correct use **/
Impl::CheckUsage<Impl::UsageRequires::insideExecEnv>::check(
"parallel_reduce", work_count, label.c_str());
using policy_type =
typename Impl::ParallelReducePolicyType<void, size_t,
FunctorType>::policy_type;
parallel_reduce(label, policy_type(0, work_count), functor, return_value);
}
template <class FunctorType, class ReturnType>
inline std::enable_if_t<Kokkos::is_view<ReturnType>::value ||
Kokkos::is_reducer<ReturnType>::value ||
std::is_pointer_v<ReturnType>>
parallel_reduce(const size_t& work_count, const FunctorType& functor,
const ReturnType& return_value) {
/** Enforce correct use **/
Impl::CheckUsage<Impl::UsageRequires::insideExecEnv>::check("parallel_reduce",
work_count);
parallel_reduce("", work_count, functor, return_value);
}
// No Return Argument
template <class PolicyType, class FunctorType>
inline void parallel_reduce(
const std::string& label, const PolicyType& policy,
const FunctorType& functor,
std::enable_if_t<Kokkos::is_execution_policy<PolicyType>::value>* =
nullptr) {
/** Enforce correct use **/
Impl::CheckUsage<Impl::UsageRequires::insideExecEnv>::check(
"parallel_reduce", policy, label.c_str());
using FunctorAnalysis =
Impl::FunctorAnalysis<Impl::FunctorPatternInterface::REDUCE, PolicyType,
FunctorType, void>;
using value_type = std::conditional_t<(FunctorAnalysis::StaticValueSize != 0),
typename FunctorAnalysis::value_type,
typename FunctorAnalysis::pointer_type>;
static_assert(
FunctorAnalysis::has_final_member_function,
"Calling parallel_reduce without either return value or final function.");
using result_view_type =
Kokkos::View<value_type, Kokkos::HostSpace, Kokkos::MemoryUnmanaged>;
result_view_type result_view;
Impl::ParallelReduceAdaptor<PolicyType, FunctorType,
result_view_type>::execute(label, policy, functor,
result_view);
}
template <class PolicyType, class FunctorType>
inline void parallel_reduce(
const PolicyType& policy, const FunctorType& functor,
std::enable_if_t<Kokkos::is_execution_policy<PolicyType>::value>* =
nullptr) {
/** Enforce correct use **/
Impl::CheckUsage<Impl::UsageRequires::insideExecEnv>::check("parallel_reduce",
policy);
parallel_reduce("", policy, functor);
}
template <class FunctorType>
inline void parallel_reduce(const std::string& label, const size_t& work_count,
const FunctorType& functor) {
/** Enforce correct use **/
Impl::CheckUsage<Impl::UsageRequires::insideExecEnv>::check(
"parallel_reduce", work_count, label.c_str());
using policy_type =
typename Impl::ParallelReducePolicyType<void, size_t,
FunctorType>::policy_type;
parallel_reduce(label, policy_type(0, work_count), functor);
}
template <class FunctorType>
inline void parallel_reduce(const size_t& work_count,
const FunctorType& functor) {
/** Enforce correct use **/
Impl::CheckUsage<Impl::UsageRequires::insideExecEnv>::check("parallel_reduce",
work_count);
parallel_reduce("", work_count, functor);
}
} // namespace Kokkos
#endif // KOKKOS_PARALLEL_REDUCE_HPP
|