45 #ifndef KOKKOS_PARALLEL_REDUCE_HPP 46 #define KOKKOS_PARALLEL_REDUCE_HPP 48 #include <Kokkos_NumericTraits.hpp> 49 #include <Kokkos_View.hpp> 50 #include <impl/Kokkos_FunctorAnalysis.hpp> 51 #include <impl/Kokkos_FunctorAdapter.hpp> 52 #include <type_traits> 56 template <
class T,
class Enable =
void>
57 struct is_reducer_type {
62 struct is_reducer_type<
63 T, typename std::enable_if<std::is_same<
64 typename std::remove_cv<T>::type,
65 typename std::remove_cv<typename T::reducer>::type>::value>::type> {
69 template <
class Scalar,
class Space>
73 using reducer = Sum<Scalar, Space>;
74 using value_type =
typename std::remove_cv<Scalar>::type;
79 result_view_type value;
80 bool references_scalar_v;
83 KOKKOS_INLINE_FUNCTION
84 Sum(value_type& value_) : value(&value_), references_scalar_v(true) {}
86 KOKKOS_INLINE_FUNCTION
87 Sum(
const result_view_type& value_)
88 : value(value_), references_scalar_v(false) {}
91 KOKKOS_INLINE_FUNCTION
92 void join(value_type& dest,
const value_type& src)
const { dest += src; }
94 KOKKOS_INLINE_FUNCTION
95 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
99 KOKKOS_INLINE_FUNCTION
100 void init(value_type& val)
const {
101 val = reduction_identity<value_type>::sum();
104 KOKKOS_INLINE_FUNCTION
105 value_type& reference()
const {
return *value.data(); }
107 KOKKOS_INLINE_FUNCTION
108 result_view_type view()
const {
return value; }
110 KOKKOS_INLINE_FUNCTION
111 bool references_scalar()
const {
return references_scalar_v; }
114 template <
class Scalar,
class Space>
118 using reducer = Prod<Scalar, Space>;
119 using value_type =
typename std::remove_cv<Scalar>::type;
124 result_view_type value;
125 bool references_scalar_v;
128 KOKKOS_INLINE_FUNCTION
129 Prod(value_type& value_) : value(&value_), references_scalar_v(true) {}
131 KOKKOS_INLINE_FUNCTION
132 Prod(
const result_view_type& value_)
133 : value(value_), references_scalar_v(false) {}
136 KOKKOS_INLINE_FUNCTION
137 void join(value_type& dest,
const value_type& src)
const { dest *= src; }
139 KOKKOS_INLINE_FUNCTION
140 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
144 KOKKOS_INLINE_FUNCTION
145 void init(value_type& val)
const {
146 val = reduction_identity<value_type>::prod();
149 KOKKOS_INLINE_FUNCTION
150 value_type& reference()
const {
return *value.data(); }
152 KOKKOS_INLINE_FUNCTION
153 result_view_type view()
const {
return value; }
155 KOKKOS_INLINE_FUNCTION
156 bool references_scalar()
const {
return references_scalar_v; }
159 template <
class Scalar,
class Space>
163 using reducer = Min<Scalar, Space>;
164 using value_type =
typename std::remove_cv<Scalar>::type;
169 result_view_type value;
170 bool references_scalar_v;
173 KOKKOS_INLINE_FUNCTION
174 Min(value_type& value_) : value(&value_), references_scalar_v(true) {}
176 KOKKOS_INLINE_FUNCTION
177 Min(
const result_view_type& value_)
178 : value(value_), references_scalar_v(false) {}
181 KOKKOS_INLINE_FUNCTION
182 void join(value_type& dest,
const value_type& src)
const {
183 if (src < dest) dest = src;
186 KOKKOS_INLINE_FUNCTION
187 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
188 if (src < dest) dest = src;
191 KOKKOS_INLINE_FUNCTION
192 void init(value_type& val)
const {
193 val = reduction_identity<value_type>::min();
196 KOKKOS_INLINE_FUNCTION
197 value_type& reference()
const {
return *value.data(); }
199 KOKKOS_INLINE_FUNCTION
200 result_view_type view()
const {
return value; }
202 KOKKOS_INLINE_FUNCTION
203 bool references_scalar()
const {
return references_scalar_v; }
206 template <
class Scalar,
class Space>
210 using reducer = Max<Scalar, Space>;
211 using value_type =
typename std::remove_cv<Scalar>::type;
216 result_view_type value;
217 bool references_scalar_v;
220 KOKKOS_INLINE_FUNCTION
221 Max(value_type& value_) : value(&value_), references_scalar_v(true) {}
223 KOKKOS_INLINE_FUNCTION
224 Max(
const result_view_type& value_)
225 : value(value_), references_scalar_v(false) {}
228 KOKKOS_INLINE_FUNCTION
229 void join(value_type& dest,
const value_type& src)
const {
230 if (src > dest) dest = src;
233 KOKKOS_INLINE_FUNCTION
234 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
235 if (src > dest) dest = src;
239 KOKKOS_INLINE_FUNCTION
240 void init(value_type& val)
const {
241 val = reduction_identity<value_type>::max();
244 KOKKOS_INLINE_FUNCTION
245 value_type& reference()
const {
return *value.data(); }
247 KOKKOS_INLINE_FUNCTION
248 result_view_type view()
const {
return value; }
250 KOKKOS_INLINE_FUNCTION
251 bool references_scalar()
const {
return references_scalar_v; }
254 template <
class Scalar,
class Space>
258 using reducer = LAnd<Scalar, Space>;
259 using value_type =
typename std::remove_cv<Scalar>::type;
264 result_view_type value;
265 bool references_scalar_v;
268 KOKKOS_INLINE_FUNCTION
269 LAnd(value_type& value_) : value(&value_), references_scalar_v(true) {}
271 KOKKOS_INLINE_FUNCTION
272 LAnd(
const result_view_type& value_)
273 : value(value_), references_scalar_v(false) {}
275 KOKKOS_INLINE_FUNCTION
276 void join(value_type& dest,
const value_type& src)
const {
280 KOKKOS_INLINE_FUNCTION
281 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
285 KOKKOS_INLINE_FUNCTION
286 void init(value_type& val)
const {
287 val = reduction_identity<value_type>::land();
290 KOKKOS_INLINE_FUNCTION
291 value_type& reference()
const {
return *value.data(); }
293 KOKKOS_INLINE_FUNCTION
294 result_view_type view()
const {
return value; }
296 KOKKOS_INLINE_FUNCTION
297 bool references_scalar()
const {
return references_scalar_v; }
300 template <
class Scalar,
class Space>
304 using reducer = LOr<Scalar, Space>;
305 using value_type =
typename std::remove_cv<Scalar>::type;
310 result_view_type value;
311 bool references_scalar_v;
314 KOKKOS_INLINE_FUNCTION
315 LOr(value_type& value_) : value(&value_), references_scalar_v(true) {}
317 KOKKOS_INLINE_FUNCTION
318 LOr(
const result_view_type& value_)
319 : value(value_), references_scalar_v(false) {}
322 KOKKOS_INLINE_FUNCTION
323 void join(value_type& dest,
const value_type& src)
const {
327 KOKKOS_INLINE_FUNCTION
328 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
332 KOKKOS_INLINE_FUNCTION
333 void init(value_type& val)
const {
334 val = reduction_identity<value_type>::lor();
337 KOKKOS_INLINE_FUNCTION
338 value_type& reference()
const {
return *value.data(); }
340 KOKKOS_INLINE_FUNCTION
341 result_view_type view()
const {
return value; }
343 KOKKOS_INLINE_FUNCTION
344 bool references_scalar()
const {
return references_scalar_v; }
347 template <
class Scalar,
class Space>
351 using reducer = BAnd<Scalar, Space>;
352 using value_type =
typename std::remove_cv<Scalar>::type;
357 result_view_type value;
358 bool references_scalar_v;
361 KOKKOS_INLINE_FUNCTION
362 BAnd(value_type& value_) : value(&value_), references_scalar_v(true) {}
364 KOKKOS_INLINE_FUNCTION
365 BAnd(
const result_view_type& value_)
366 : value(value_), references_scalar_v(false) {}
369 KOKKOS_INLINE_FUNCTION
370 void join(value_type& dest,
const value_type& src)
const {
374 KOKKOS_INLINE_FUNCTION
375 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
379 KOKKOS_INLINE_FUNCTION
380 void init(value_type& val)
const {
381 val = reduction_identity<value_type>::band();
384 KOKKOS_INLINE_FUNCTION
385 value_type& reference()
const {
return *value.data(); }
387 KOKKOS_INLINE_FUNCTION
388 result_view_type view()
const {
return value; }
390 KOKKOS_INLINE_FUNCTION
391 bool references_scalar()
const {
return references_scalar_v; }
394 template <
class Scalar,
class Space>
398 using reducer = BOr<Scalar, Space>;
399 using value_type =
typename std::remove_cv<Scalar>::type;
404 result_view_type value;
405 bool references_scalar_v;
408 KOKKOS_INLINE_FUNCTION
409 BOr(value_type& value_) : value(&value_), references_scalar_v(true) {}
411 KOKKOS_INLINE_FUNCTION
412 BOr(
const result_view_type& value_)
413 : value(value_), references_scalar_v(false) {}
416 KOKKOS_INLINE_FUNCTION
417 void join(value_type& dest,
const value_type& src)
const {
421 KOKKOS_INLINE_FUNCTION
422 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
426 KOKKOS_INLINE_FUNCTION
427 void init(value_type& val)
const {
428 val = reduction_identity<value_type>::bor();
431 KOKKOS_INLINE_FUNCTION
432 value_type& reference()
const {
return *value.data(); }
434 KOKKOS_INLINE_FUNCTION
435 result_view_type view()
const {
return value; }
437 KOKKOS_INLINE_FUNCTION
438 bool references_scalar()
const {
return references_scalar_v; }
441 template <
class Scalar,
class Index>
442 struct ValLocScalar {
446 KOKKOS_INLINE_FUNCTION
447 void operator=(
const ValLocScalar& rhs) {
452 KOKKOS_INLINE_FUNCTION
453 void operator=(
const volatile ValLocScalar& rhs)
volatile {
459 template <
class Scalar,
class Index,
class Space>
462 using scalar_type =
typename std::remove_cv<Scalar>::type;
463 using index_type =
typename std::remove_cv<Index>::type;
467 using reducer = MinLoc<Scalar, Index, Space>;
468 using value_type = ValLocScalar<scalar_type, index_type>;
473 result_view_type value;
474 bool references_scalar_v;
477 KOKKOS_INLINE_FUNCTION
478 MinLoc(value_type& value_) : value(&value_), references_scalar_v(true) {}
480 KOKKOS_INLINE_FUNCTION
481 MinLoc(
const result_view_type& value_)
482 : value(value_), references_scalar_v(false) {}
485 KOKKOS_INLINE_FUNCTION
486 void join(value_type& dest,
const value_type& src)
const {
487 if (src.val < dest.val) dest = src;
490 KOKKOS_INLINE_FUNCTION
491 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
492 if (src.val < dest.val) dest = src;
495 KOKKOS_INLINE_FUNCTION
496 void init(value_type& val)
const {
497 val.val = reduction_identity<scalar_type>::min();
498 val.loc = reduction_identity<index_type>::min();
501 KOKKOS_INLINE_FUNCTION
502 value_type& reference()
const {
return *value.data(); }
504 KOKKOS_INLINE_FUNCTION
505 result_view_type view()
const {
return value; }
507 KOKKOS_INLINE_FUNCTION
508 bool references_scalar()
const {
return references_scalar_v; }
511 template <
class Scalar,
class Index,
class Space>
514 using scalar_type =
typename std::remove_cv<Scalar>::type;
515 using index_type =
typename std::remove_cv<Index>::type;
519 using reducer = MaxLoc<Scalar, Index, Space>;
520 using value_type = ValLocScalar<scalar_type, index_type>;
525 result_view_type value;
526 bool references_scalar_v;
529 KOKKOS_INLINE_FUNCTION
530 MaxLoc(value_type& value_) : value(&value_), references_scalar_v(true) {}
532 KOKKOS_INLINE_FUNCTION
533 MaxLoc(
const result_view_type& value_)
534 : value(value_), references_scalar_v(false) {}
537 KOKKOS_INLINE_FUNCTION
538 void join(value_type& dest,
const value_type& src)
const {
539 if (src.val > dest.val) dest = src;
542 KOKKOS_INLINE_FUNCTION
543 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
544 if (src.val > dest.val) dest = src;
547 KOKKOS_INLINE_FUNCTION
548 void init(value_type& val)
const {
549 val.val = reduction_identity<scalar_type>::max();
550 val.loc = reduction_identity<index_type>::min();
553 KOKKOS_INLINE_FUNCTION
554 value_type& reference()
const {
return *value.data(); }
556 KOKKOS_INLINE_FUNCTION
557 result_view_type view()
const {
return value; }
559 KOKKOS_INLINE_FUNCTION
560 bool references_scalar()
const {
return references_scalar_v; }
563 template <
class Scalar>
564 struct MinMaxScalar {
565 Scalar min_val, max_val;
567 KOKKOS_INLINE_FUNCTION
568 void operator=(
const MinMaxScalar& rhs) {
569 min_val = rhs.min_val;
570 max_val = rhs.max_val;
573 KOKKOS_INLINE_FUNCTION
574 void operator=(
const volatile MinMaxScalar& rhs)
volatile {
575 min_val = rhs.min_val;
576 max_val = rhs.max_val;
580 template <
class Scalar,
class Space>
583 using scalar_type =
typename std::remove_cv<Scalar>::type;
587 using reducer = MinMax<Scalar, Space>;
588 using value_type = MinMaxScalar<scalar_type>;
593 result_view_type value;
594 bool references_scalar_v;
597 KOKKOS_INLINE_FUNCTION
598 MinMax(value_type& value_) : value(&value_), references_scalar_v(true) {}
600 KOKKOS_INLINE_FUNCTION
601 MinMax(
const result_view_type& value_)
602 : value(value_), references_scalar_v(false) {}
605 KOKKOS_INLINE_FUNCTION
606 void join(value_type& dest,
const value_type& src)
const {
607 if (src.min_val < dest.min_val) {
608 dest.min_val = src.min_val;
610 if (src.max_val > dest.max_val) {
611 dest.max_val = src.max_val;
615 KOKKOS_INLINE_FUNCTION
616 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
617 if (src.min_val < dest.min_val) {
618 dest.min_val = src.min_val;
620 if (src.max_val > dest.max_val) {
621 dest.max_val = src.max_val;
625 KOKKOS_INLINE_FUNCTION
626 void init(value_type& val)
const {
627 val.max_val = reduction_identity<scalar_type>::max();
628 val.min_val = reduction_identity<scalar_type>::min();
631 KOKKOS_INLINE_FUNCTION
632 value_type& reference()
const {
return *value.data(); }
634 KOKKOS_INLINE_FUNCTION
635 result_view_type view()
const {
return value; }
637 KOKKOS_INLINE_FUNCTION
638 bool references_scalar()
const {
return references_scalar_v; }
641 template <
class Scalar,
class Index>
642 struct MinMaxLocScalar {
643 Scalar min_val, max_val;
644 Index min_loc, max_loc;
646 KOKKOS_INLINE_FUNCTION
647 void operator=(
const MinMaxLocScalar& rhs) {
648 min_val = rhs.min_val;
649 min_loc = rhs.min_loc;
650 max_val = rhs.max_val;
651 max_loc = rhs.max_loc;
654 KOKKOS_INLINE_FUNCTION
655 void operator=(
const volatile MinMaxLocScalar& rhs)
volatile {
656 min_val = rhs.min_val;
657 min_loc = rhs.min_loc;
658 max_val = rhs.max_val;
659 max_loc = rhs.max_loc;
663 template <
class Scalar,
class Index,
class Space>
666 using scalar_type =
typename std::remove_cv<Scalar>::type;
667 using index_type =
typename std::remove_cv<Index>::type;
671 using reducer = MinMaxLoc<Scalar, Index, Space>;
672 using value_type = MinMaxLocScalar<scalar_type, index_type>;
677 result_view_type value;
678 bool references_scalar_v;
681 KOKKOS_INLINE_FUNCTION
682 MinMaxLoc(value_type& value_) : value(&value_), references_scalar_v(true) {}
684 KOKKOS_INLINE_FUNCTION
685 MinMaxLoc(
const result_view_type& value_)
686 : value(value_), references_scalar_v(false) {}
689 KOKKOS_INLINE_FUNCTION
690 void join(value_type& dest,
const value_type& src)
const {
691 if (src.min_val < dest.min_val) {
692 dest.min_val = src.min_val;
693 dest.min_loc = src.min_loc;
695 if (src.max_val > dest.max_val) {
696 dest.max_val = src.max_val;
697 dest.max_loc = src.max_loc;
701 KOKKOS_INLINE_FUNCTION
702 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
703 if (src.min_val < dest.min_val) {
704 dest.min_val = src.min_val;
705 dest.min_loc = src.min_loc;
707 if (src.max_val > dest.max_val) {
708 dest.max_val = src.max_val;
709 dest.max_loc = src.max_loc;
713 KOKKOS_INLINE_FUNCTION
714 void init(value_type& val)
const {
715 val.max_val = reduction_identity<scalar_type>::max();
716 val.min_val = reduction_identity<scalar_type>::min();
717 val.max_loc = reduction_identity<index_type>::min();
718 val.min_loc = reduction_identity<index_type>::min();
721 KOKKOS_INLINE_FUNCTION
722 value_type& reference()
const {
return *value.data(); }
724 KOKKOS_INLINE_FUNCTION
725 result_view_type view()
const {
return value; }
727 KOKKOS_INLINE_FUNCTION
728 bool references_scalar()
const {
return references_scalar_v; }
734 template <
class T,
class ReturnType,
class ValueTraits>
735 struct ParallelReduceReturnValue;
737 template <
class ReturnType,
class FunctorType>
738 struct ParallelReduceReturnValue<
739 typename std::enable_if<Kokkos::is_view<ReturnType>::value>::type,
742 using reducer_type = InvalidType;
744 using value_type_scalar =
typename return_type::value_type;
745 using value_type_array =
typename return_type::value_type*
const;
747 using value_type = std::conditional_t<return_type::rank == 0,
748 value_type_scalar, value_type_array>;
750 static return_type& return_value(
ReturnType& return_val,
const FunctorType&) {
755 template <
class ReturnType,
class FunctorType>
756 struct ParallelReduceReturnValue<
757 typename std::enable_if<!Kokkos::is_view<ReturnType>::value &&
758 (!std::is_array<ReturnType>::value &&
759 !std::is_pointer<ReturnType>::value) &&
760 !Kokkos::is_reducer_type<ReturnType>::value>::type,
765 using reducer_type = InvalidType;
767 using value_type =
typename return_type::value_type;
769 static return_type return_value(
ReturnType& return_val,
const FunctorType&) {
770 return return_type(&return_val);
774 template <
class ReturnType,
class FunctorType>
775 struct ParallelReduceReturnValue<
776 typename std::enable_if<(std::is_array<ReturnType>::value ||
777 std::is_pointer<ReturnType>::value)>::type,
782 using reducer_type = InvalidType;
784 using value_type =
typename return_type::value_type[];
786 static return_type return_value(
ReturnType& return_val,
787 const FunctorType& functor) {
788 if (std::is_array<ReturnType>::value)
789 return return_type(return_val);
791 return return_type(return_val, functor.value_count);
795 template <
class ReturnType,
class FunctorType>
796 struct ParallelReduceReturnValue<
797 typename std::enable_if<Kokkos::is_reducer_type<ReturnType>::value>::type,
801 using value_type =
typename return_type::value_type;
803 static return_type return_value(
ReturnType& return_val,
const FunctorType&) {
808 template <
class T,
class ReturnType,
class FunctorType>
809 struct ParallelReducePolicyType;
811 template <
class PolicyType,
class FunctorType>
812 struct ParallelReducePolicyType<
813 typename std::enable_if<
814 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type,
815 PolicyType, FunctorType> {
816 using policy_type = PolicyType;
817 static PolicyType policy(
const PolicyType& policy_) {
return policy_; }
820 template <
class PolicyType,
class FunctorType>
821 struct ParallelReducePolicyType<
822 typename std::enable_if<std::is_integral<PolicyType>::value>::type,
823 PolicyType, FunctorType> {
824 using execution_space =
825 typename Impl::FunctorPolicyExecutionSpace<FunctorType,
826 void>::execution_space;
830 static policy_type policy(
const PolicyType& policy_) {
831 return policy_type(0, policy_);
835 template <
class FunctorType,
class ExecPolicy,
class ValueType,
836 class ExecutionSpace>
837 struct ParallelReduceFunctorType {
838 using functor_type = FunctorType;
839 static const functor_type& functor(
const functor_type& functor) {
844 template <
class PolicyType,
class FunctorType,
class ReturnType>
845 struct ParallelReduceAdaptor {
846 using return_value_adapter =
847 Impl::ParallelReduceReturnValue<void, ReturnType, FunctorType>;
848 #ifdef KOKKOS_IMPL_NEED_FUNCTOR_WRAPPER 849 using functor_adaptor =
850 Impl::ParallelReduceFunctorType<FunctorType, PolicyType,
851 typename return_value_adapter::value_type,
852 typename PolicyType::execution_space>;
854 static inline void execute(
const std::string& label,
const PolicyType& policy,
855 const FunctorType& functor,
859 PolicyType inner_policy = policy;
860 Kokkos::Tools::Impl::begin_parallel_reduce<
861 typename return_value_adapter::reducer_type>(inner_policy, functor,
864 Kokkos::Impl::shared_allocation_tracking_disable();
865 #ifdef KOKKOS_IMPL_NEED_FUNCTOR_WRAPPER 866 Impl::ParallelReduce<
typename functor_adaptor::functor_type, PolicyType,
867 typename return_value_adapter::reducer_type>
868 closure(functor_adaptor::functor(functor), inner_policy,
869 return_value_adapter::return_value(return_value, functor));
871 Impl::ParallelReduce<FunctorType, PolicyType,
872 typename return_value_adapter::reducer_type>
873 closure(functor, inner_policy,
874 return_value_adapter::return_value(return_value, functor));
876 Kokkos::Impl::shared_allocation_tracking_enable();
879 Kokkos::Tools::Impl::end_parallel_reduce<
880 typename return_value_adapter::reducer_type>(inner_policy, functor,
902 template <
typename T>
903 struct ReducerHasTestReferenceFunction {
904 template <
typename E>
905 static std::true_type test_func(decltype(&E::references_scalar));
906 template <
typename E>
907 static std::false_type test_func(...);
910 value = std::is_same<std::true_type, decltype(test_func<T>(
nullptr))>::value
914 template <
class ExecutionSpace,
class Arg>
915 constexpr std::enable_if_t<
917 !ReducerHasTestReferenceFunction<Arg>::value &&
918 !Kokkos::is_view<Arg>::value,
921 parallel_reduce_needs_fence(ExecutionSpace
const&, Arg
const&) {
925 template <
class ExecutionSpace,
class Reducer>
926 constexpr std::enable_if_t<
931 ReducerHasTestReferenceFunction<Reducer>::value,
934 parallel_reduce_needs_fence(ExecutionSpace
const&, Reducer
const& reducer) {
935 return reducer.references_scalar();
938 template <
class ExecutionSpace,
class ViewLike>
939 constexpr std::enable_if_t<
941 Kokkos::is_view<ViewLike>::value,
944 parallel_reduce_needs_fence(ExecutionSpace
const&, ViewLike
const&) {
948 template <
class ExecutionSpace,
class... Args>
949 struct ParallelReduceFence {
950 template <
class... ArgsDeduced>
951 static void fence(
const ExecutionSpace& ex, ArgsDeduced&&... args) {
952 if (Impl::parallel_reduce_needs_fence(ex, (ArgsDeduced &&) args...)) {
1002 template <
class PolicyType,
class FunctorType,
class ReturnType>
1003 inline typename std::enable_if<
1004 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type
1005 parallel_reduce(
const std::string& label,
const PolicyType& policy,
1006 const FunctorType& functor,
ReturnType& return_value) {
1007 Impl::ParallelReduceAdaptor<PolicyType, FunctorType, ReturnType>::execute(
1008 label, policy, functor, return_value);
1009 Impl::ParallelReduceFence<
typename PolicyType::execution_space,
1010 ReturnType>::fence(policy.space(), return_value);
1013 template <
class PolicyType,
class FunctorType,
class ReturnType>
1014 inline typename std::enable_if<
1015 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type
1016 parallel_reduce(
const PolicyType& policy,
const FunctorType& functor,
1018 Impl::ParallelReduceAdaptor<PolicyType, FunctorType, ReturnType>::execute(
1019 "", policy, functor, return_value);
1020 Impl::ParallelReduceFence<
typename PolicyType::execution_space,
1021 ReturnType>::fence(policy.space(), return_value);
1024 template <
class FunctorType,
class ReturnType>
1025 inline void parallel_reduce(
const size_t& policy,
const FunctorType& functor,
1028 typename Impl::ParallelReducePolicyType<void, size_t,
1029 FunctorType>::policy_type;
1030 Impl::ParallelReduceAdaptor<policy_type, FunctorType, ReturnType>::execute(
1031 "", policy_type(0, policy), functor, return_value);
1032 Impl::ParallelReduceFence<typename policy_type::execution_space, ReturnType>::
1033 fence(
typename policy_type::execution_space(), return_value);
1036 template <
class FunctorType,
class ReturnType>
1037 inline void parallel_reduce(
const std::string& label,
const size_t& policy,
1038 const FunctorType& functor,
1041 typename Impl::ParallelReducePolicyType<void, size_t,
1042 FunctorType>::policy_type;
1043 Impl::ParallelReduceAdaptor<policy_type, FunctorType, ReturnType>::execute(
1044 label, policy_type(0, policy), functor, return_value);
1045 Impl::ParallelReduceFence<typename policy_type::execution_space, ReturnType>::
1046 fence(
typename policy_type::execution_space(), return_value);
1051 template <
class PolicyType,
class FunctorType,
class ReturnType>
1052 inline typename std::enable_if<
1053 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type
1054 parallel_reduce(
const std::string& label,
const PolicyType& policy,
1055 const FunctorType& functor,
const ReturnType& return_value) {
1057 Impl::ParallelReduceAdaptor<PolicyType, FunctorType, ReturnType>::execute(
1058 label, policy, functor, return_value_impl);
1059 Impl::ParallelReduceFence<
typename PolicyType::execution_space,
1060 ReturnType>::fence(policy.space(), return_value);
1063 template <
class PolicyType,
class FunctorType,
class ReturnType>
1064 inline typename std::enable_if<
1065 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type
1066 parallel_reduce(
const PolicyType& policy,
const FunctorType& functor,
1069 Impl::ParallelReduceAdaptor<PolicyType, FunctorType, ReturnType>::execute(
1070 "", policy, functor, return_value_impl);
1071 Impl::ParallelReduceFence<
typename PolicyType::execution_space,
1072 ReturnType>::fence(policy.space(), return_value);
1075 template <
class FunctorType,
class ReturnType>
1076 inline void parallel_reduce(
const size_t& policy,
const FunctorType& functor,
1079 typename Impl::ParallelReducePolicyType<void, size_t,
1080 FunctorType>::policy_type;
1082 Impl::ParallelReduceAdaptor<policy_type, FunctorType, ReturnType>::execute(
1083 "", policy_type(0, policy), functor, return_value_impl);
1084 Impl::ParallelReduceFence<typename policy_type::execution_space, ReturnType>::
1085 fence(
typename policy_type::execution_space(), return_value);
1088 template <
class FunctorType,
class ReturnType>
1089 inline void parallel_reduce(
const std::string& label,
const size_t& policy,
1090 const FunctorType& functor,
1093 typename Impl::ParallelReducePolicyType<void, size_t,
1094 FunctorType>::policy_type;
1096 Impl::ParallelReduceAdaptor<policy_type, FunctorType, ReturnType>::execute(
1097 label, policy_type(0, policy), functor, return_value_impl);
1098 Impl::ParallelReduceFence<typename policy_type::execution_space, ReturnType>::
1099 fence(
typename policy_type::execution_space(), return_value);
1104 template <
class PolicyType,
class FunctorType>
1105 inline void parallel_reduce(
1106 const std::string& label,
const PolicyType& policy,
1107 const FunctorType& functor,
1108 typename std::enable_if<
1109 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type* =
1111 using ValueTraits = Kokkos::Impl::FunctorValueTraits<FunctorType, void>;
1112 using value_type = std::conditional_t<(ValueTraits::StaticValueSize != 0),
1113 typename ValueTraits::value_type,
1114 typename ValueTraits::pointer_type>;
1117 Impl::FunctorAnalysis<Impl::FunctorPatternInterface::REDUCE, PolicyType,
1118 FunctorType>::has_final_member_function,
1119 "Calling parallel_reduce without either return value or final function.");
1121 using result_view_type =
1123 result_view_type result_view;
1125 Impl::ParallelReduceAdaptor<PolicyType, FunctorType,
1126 result_view_type>::execute(label, policy, functor,
1130 template <
class PolicyType,
class FunctorType>
1131 inline void parallel_reduce(
1132 const PolicyType& policy,
const FunctorType& functor,
1133 typename std::enable_if<
1134 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type* =
1136 using ValueTraits = Kokkos::Impl::FunctorValueTraits<FunctorType, void>;
1137 using value_type = std::conditional_t<(ValueTraits::StaticValueSize != 0),
1138 typename ValueTraits::value_type,
1139 typename ValueTraits::pointer_type>;
1142 Impl::FunctorAnalysis<Impl::FunctorPatternInterface::REDUCE, PolicyType,
1143 FunctorType>::has_final_member_function,
1144 "Calling parallel_reduce without either return value or final function.");
1146 using result_view_type =
1148 result_view_type result_view;
1150 Impl::ParallelReduceAdaptor<PolicyType, FunctorType,
1151 result_view_type>::execute(
"", policy, functor,
1155 template <
class FunctorType>
1156 inline void parallel_reduce(
const size_t& policy,
const FunctorType& functor) {
1158 typename Impl::ParallelReducePolicyType<void, size_t,
1159 FunctorType>::policy_type;
1160 using ValueTraits = Kokkos::Impl::FunctorValueTraits<FunctorType, void>;
1161 using value_type = std::conditional_t<(ValueTraits::StaticValueSize != 0),
1162 typename ValueTraits::value_type,
1163 typename ValueTraits::pointer_type>;
1166 Impl::FunctorAnalysis<Impl::FunctorPatternInterface::REDUCE,
1168 FunctorType>::has_final_member_function,
1169 "Calling parallel_reduce without either return value or final function.");
1171 using result_view_type =
1173 result_view_type result_view;
1175 Impl::ParallelReduceAdaptor<policy_type, FunctorType,
1176 result_view_type>::execute(
"",
1177 policy_type(0, policy),
1178 functor, result_view);
1181 template <
class FunctorType>
1182 inline void parallel_reduce(
const std::string& label,
const size_t& policy,
1183 const FunctorType& functor) {
1185 typename Impl::ParallelReducePolicyType<void, size_t,
1186 FunctorType>::policy_type;
1187 using ValueTraits = Kokkos::Impl::FunctorValueTraits<FunctorType, void>;
1188 using value_type = std::conditional_t<(ValueTraits::StaticValueSize != 0),
1189 typename ValueTraits::value_type,
1190 typename ValueTraits::pointer_type>;
1193 Impl::FunctorAnalysis<Impl::FunctorPatternInterface::REDUCE,
1195 FunctorType>::has_final_member_function,
1196 "Calling parallel_reduce without either return value or final function.");
1198 using result_view_type =
1200 result_view_type result_view;
1202 Impl::ParallelReduceAdaptor<policy_type, FunctorType,
1203 result_view_type>::execute(label,
1204 policy_type(0, policy),
1205 functor, result_view);
1210 #endif // KOKKOS_PARALLEL_REDUCE_HPP
Memory management for host memory.
Execution policy for work over a range of an integral type.