45 #ifndef KOKKOS_PARALLEL_REDUCE_HPP
46 #define KOKKOS_PARALLEL_REDUCE_HPP
48 #include <Kokkos_NumericTraits.hpp>
52 template <
class T,
class Enable =
void>
53 struct is_reducer_type {
58 struct is_reducer_type<
59 T, typename std::enable_if<std::is_same<
60 typename std::remove_cv<T>::type,
61 typename std::remove_cv<typename T::reducer>::type>::value>::type> {
65 template <
class Scalar,
class Space>
70 typedef typename std::remove_cv<Scalar>::type value_type;
75 result_view_type value;
76 bool references_scalar_v;
79 KOKKOS_INLINE_FUNCTION
80 Sum(value_type& value_) : value(&value_), references_scalar_v(true) {}
82 KOKKOS_INLINE_FUNCTION
83 Sum(
const result_view_type& value_)
84 : value(value_), references_scalar_v(false) {}
87 KOKKOS_INLINE_FUNCTION
88 void join(value_type& dest,
const value_type& src)
const { dest += src; }
90 KOKKOS_INLINE_FUNCTION
91 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
95 KOKKOS_INLINE_FUNCTION
96 void init(value_type& val)
const {
97 val = reduction_identity<value_type>::sum();
100 KOKKOS_INLINE_FUNCTION
101 value_type& reference()
const {
return *value.data(); }
103 KOKKOS_INLINE_FUNCTION
104 result_view_type view()
const {
return value; }
106 KOKKOS_INLINE_FUNCTION
107 bool references_scalar()
const {
return references_scalar_v; }
110 template <
class Scalar,
class Space>
114 typedef Prod reducer;
115 typedef typename std::remove_cv<Scalar>::type value_type;
120 result_view_type value;
121 bool references_scalar_v;
124 KOKKOS_INLINE_FUNCTION
125 Prod(value_type& value_) : value(&value_), references_scalar_v(true) {}
127 KOKKOS_INLINE_FUNCTION
128 Prod(
const result_view_type& value_)
129 : value(value_), references_scalar_v(false) {}
132 KOKKOS_INLINE_FUNCTION
133 void join(value_type& dest,
const value_type& src)
const { dest *= src; }
135 KOKKOS_INLINE_FUNCTION
136 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
140 KOKKOS_INLINE_FUNCTION
141 void init(value_type& val)
const {
142 val = reduction_identity<value_type>::prod();
145 KOKKOS_INLINE_FUNCTION
146 value_type& reference()
const {
return *value.data(); }
148 KOKKOS_INLINE_FUNCTION
149 result_view_type view()
const {
return value; }
151 KOKKOS_INLINE_FUNCTION
152 bool references_scalar()
const {
return references_scalar_v; }
155 template <
class Scalar,
class Space>
160 typedef typename std::remove_cv<Scalar>::type value_type;
165 result_view_type value;
166 bool references_scalar_v;
169 KOKKOS_INLINE_FUNCTION
170 Min(value_type& value_) : value(&value_), references_scalar_v(true) {}
172 KOKKOS_INLINE_FUNCTION
173 Min(
const result_view_type& value_)
174 : value(value_), references_scalar_v(false) {}
177 KOKKOS_INLINE_FUNCTION
178 void join(value_type& dest,
const value_type& src)
const {
179 if (src < dest) dest = src;
182 KOKKOS_INLINE_FUNCTION
183 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
184 if (src < dest) dest = src;
187 KOKKOS_INLINE_FUNCTION
188 void init(value_type& val)
const {
189 val = reduction_identity<value_type>::min();
192 KOKKOS_INLINE_FUNCTION
193 value_type& reference()
const {
return *value.data(); }
195 KOKKOS_INLINE_FUNCTION
196 result_view_type view()
const {
return value; }
198 KOKKOS_INLINE_FUNCTION
199 bool references_scalar()
const {
return references_scalar_v; }
202 template <
class Scalar,
class Space>
207 typedef typename std::remove_cv<Scalar>::type value_type;
212 result_view_type value;
213 bool references_scalar_v;
216 KOKKOS_INLINE_FUNCTION
217 Max(value_type& value_) : value(&value_), references_scalar_v(true) {}
219 KOKKOS_INLINE_FUNCTION
220 Max(
const result_view_type& value_)
221 : value(value_), references_scalar_v(false) {}
224 KOKKOS_INLINE_FUNCTION
225 void join(value_type& dest,
const value_type& src)
const {
226 if (src > dest) dest = src;
229 KOKKOS_INLINE_FUNCTION
230 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
231 if (src > dest) dest = src;
235 KOKKOS_INLINE_FUNCTION
236 void init(value_type& val)
const {
237 val = reduction_identity<value_type>::max();
240 KOKKOS_INLINE_FUNCTION
241 value_type& reference()
const {
return *value.data(); }
243 KOKKOS_INLINE_FUNCTION
244 result_view_type view()
const {
return value; }
246 KOKKOS_INLINE_FUNCTION
247 bool references_scalar()
const {
return references_scalar_v; }
250 template <
class Scalar,
class Space>
254 typedef LAnd reducer;
255 typedef typename std::remove_cv<Scalar>::type value_type;
260 result_view_type value;
261 bool references_scalar_v;
264 KOKKOS_INLINE_FUNCTION
265 LAnd(value_type& value_) : value(&value_), references_scalar_v(true) {}
267 KOKKOS_INLINE_FUNCTION
268 LAnd(
const result_view_type& value_)
269 : value(value_), references_scalar_v(false) {}
271 KOKKOS_INLINE_FUNCTION
272 void join(value_type& dest,
const value_type& src)
const {
276 KOKKOS_INLINE_FUNCTION
277 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
281 KOKKOS_INLINE_FUNCTION
282 void init(value_type& val)
const {
283 val = reduction_identity<value_type>::land();
286 KOKKOS_INLINE_FUNCTION
287 value_type& reference()
const {
return *value.data(); }
289 KOKKOS_INLINE_FUNCTION
290 result_view_type view()
const {
return value; }
292 KOKKOS_INLINE_FUNCTION
293 bool references_scalar()
const {
return references_scalar_v; }
296 template <
class Scalar,
class Space>
301 typedef typename std::remove_cv<Scalar>::type value_type;
306 result_view_type value;
307 bool references_scalar_v;
310 KOKKOS_INLINE_FUNCTION
311 LOr(value_type& value_) : value(&value_), references_scalar_v(true) {}
313 KOKKOS_INLINE_FUNCTION
314 LOr(
const result_view_type& value_)
315 : value(value_), references_scalar_v(false) {}
318 KOKKOS_INLINE_FUNCTION
319 void join(value_type& dest,
const value_type& src)
const {
323 KOKKOS_INLINE_FUNCTION
324 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
328 KOKKOS_INLINE_FUNCTION
329 void init(value_type& val)
const {
330 val = reduction_identity<value_type>::lor();
333 KOKKOS_INLINE_FUNCTION
334 value_type& reference()
const {
return *value.data(); }
336 KOKKOS_INLINE_FUNCTION
337 result_view_type view()
const {
return value; }
339 KOKKOS_INLINE_FUNCTION
340 bool references_scalar()
const {
return references_scalar_v; }
343 template <
class Scalar,
class Space>
347 typedef BAnd reducer;
348 typedef typename std::remove_cv<Scalar>::type value_type;
353 result_view_type value;
354 bool references_scalar_v;
357 KOKKOS_INLINE_FUNCTION
358 BAnd(value_type& value_) : value(&value_), references_scalar_v(true) {}
360 KOKKOS_INLINE_FUNCTION
361 BAnd(
const result_view_type& value_)
362 : value(value_), references_scalar_v(false) {}
365 KOKKOS_INLINE_FUNCTION
366 void join(value_type& dest,
const value_type& src)
const {
370 KOKKOS_INLINE_FUNCTION
371 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
375 KOKKOS_INLINE_FUNCTION
376 void init(value_type& val)
const {
377 val = reduction_identity<value_type>::band();
380 KOKKOS_INLINE_FUNCTION
381 value_type& reference()
const {
return *value.data(); }
383 KOKKOS_INLINE_FUNCTION
384 result_view_type view()
const {
return value; }
386 KOKKOS_INLINE_FUNCTION
387 bool references_scalar()
const {
return references_scalar_v; }
390 template <
class Scalar,
class Space>
395 typedef typename std::remove_cv<Scalar>::type value_type;
400 result_view_type value;
401 bool references_scalar_v;
404 KOKKOS_INLINE_FUNCTION
405 BOr(value_type& value_) : value(&value_), references_scalar_v(true) {}
407 KOKKOS_INLINE_FUNCTION
408 BOr(
const result_view_type& value_)
409 : value(value_), references_scalar_v(false) {}
412 KOKKOS_INLINE_FUNCTION
413 void join(value_type& dest,
const value_type& src)
const {
417 KOKKOS_INLINE_FUNCTION
418 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
422 KOKKOS_INLINE_FUNCTION
423 void init(value_type& val)
const {
424 val = reduction_identity<value_type>::bor();
427 KOKKOS_INLINE_FUNCTION
428 value_type& reference()
const {
return *value.data(); }
430 KOKKOS_INLINE_FUNCTION
431 result_view_type view()
const {
return value; }
433 KOKKOS_INLINE_FUNCTION
434 bool references_scalar()
const {
return references_scalar_v; }
437 template <
class Scalar,
class Index>
438 struct ValLocScalar {
442 KOKKOS_INLINE_FUNCTION
443 void operator=(
const ValLocScalar& rhs) {
448 KOKKOS_INLINE_FUNCTION
449 void operator=(
const volatile ValLocScalar& rhs)
volatile {
455 template <
class Scalar,
class Index,
class Space>
458 typedef typename std::remove_cv<Scalar>::type scalar_type;
459 typedef typename std::remove_cv<Index>::type index_type;
463 typedef MinLoc reducer;
464 typedef ValLocScalar<scalar_type, index_type> value_type;
469 result_view_type value;
470 bool references_scalar_v;
473 KOKKOS_INLINE_FUNCTION
474 MinLoc(value_type& value_) : value(&value_), references_scalar_v(true) {}
476 KOKKOS_INLINE_FUNCTION
477 MinLoc(
const result_view_type& value_)
478 : value(value_), references_scalar_v(false) {}
481 KOKKOS_INLINE_FUNCTION
482 void join(value_type& dest,
const value_type& src)
const {
483 if (src.val < dest.val) dest = src;
486 KOKKOS_INLINE_FUNCTION
487 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
488 if (src.val < dest.val) dest = src;
491 KOKKOS_INLINE_FUNCTION
492 void init(value_type& val)
const {
493 val.val = reduction_identity<scalar_type>::min();
494 val.loc = reduction_identity<index_type>::min();
497 KOKKOS_INLINE_FUNCTION
498 value_type& reference()
const {
return *value.data(); }
500 KOKKOS_INLINE_FUNCTION
501 result_view_type view()
const {
return value; }
503 KOKKOS_INLINE_FUNCTION
504 bool references_scalar()
const {
return references_scalar_v; }
507 template <
class Scalar,
class Index,
class Space>
510 typedef typename std::remove_cv<Scalar>::type scalar_type;
511 typedef typename std::remove_cv<Index>::type index_type;
515 typedef MaxLoc reducer;
516 typedef ValLocScalar<scalar_type, index_type> value_type;
521 result_view_type value;
522 bool references_scalar_v;
525 KOKKOS_INLINE_FUNCTION
526 MaxLoc(value_type& value_) : value(&value_), references_scalar_v(true) {}
528 KOKKOS_INLINE_FUNCTION
529 MaxLoc(
const result_view_type& value_)
530 : value(value_), references_scalar_v(false) {}
533 KOKKOS_INLINE_FUNCTION
534 void join(value_type& dest,
const value_type& src)
const {
535 if (src.val > dest.val) dest = src;
538 KOKKOS_INLINE_FUNCTION
539 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
540 if (src.val > dest.val) dest = src;
543 KOKKOS_INLINE_FUNCTION
544 void init(value_type& val)
const {
545 val.val = reduction_identity<scalar_type>::max();
546 val.loc = reduction_identity<index_type>::min();
549 KOKKOS_INLINE_FUNCTION
550 value_type& reference()
const {
return *value.data(); }
552 KOKKOS_INLINE_FUNCTION
553 result_view_type view()
const {
return value; }
555 KOKKOS_INLINE_FUNCTION
556 bool references_scalar()
const {
return references_scalar_v; }
559 template <
class Scalar>
560 struct MinMaxScalar {
561 Scalar min_val, max_val;
563 KOKKOS_INLINE_FUNCTION
564 void operator=(
const MinMaxScalar& rhs) {
565 min_val = rhs.min_val;
566 max_val = rhs.max_val;
569 KOKKOS_INLINE_FUNCTION
570 void operator=(
const volatile MinMaxScalar& rhs)
volatile {
571 min_val = rhs.min_val;
572 max_val = rhs.max_val;
576 template <
class Scalar,
class Space>
579 typedef typename std::remove_cv<Scalar>::type scalar_type;
583 typedef MinMax reducer;
584 typedef MinMaxScalar<scalar_type> value_type;
589 result_view_type value;
590 bool references_scalar_v;
593 KOKKOS_INLINE_FUNCTION
594 MinMax(value_type& value_) : value(&value_), references_scalar_v(true) {}
596 KOKKOS_INLINE_FUNCTION
597 MinMax(
const result_view_type& value_)
598 : value(value_), references_scalar_v(false) {}
601 KOKKOS_INLINE_FUNCTION
602 void join(value_type& dest,
const value_type& src)
const {
603 if (src.min_val < dest.min_val) {
604 dest.min_val = src.min_val;
606 if (src.max_val > dest.max_val) {
607 dest.max_val = src.max_val;
611 KOKKOS_INLINE_FUNCTION
612 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
613 if (src.min_val < dest.min_val) {
614 dest.min_val = src.min_val;
616 if (src.max_val > dest.max_val) {
617 dest.max_val = src.max_val;
621 KOKKOS_INLINE_FUNCTION
622 void init(value_type& val)
const {
623 val.max_val = reduction_identity<scalar_type>::max();
624 val.min_val = reduction_identity<scalar_type>::min();
627 KOKKOS_INLINE_FUNCTION
628 value_type& reference()
const {
return *value.data(); }
630 KOKKOS_INLINE_FUNCTION
631 result_view_type view()
const {
return value; }
633 KOKKOS_INLINE_FUNCTION
634 bool references_scalar()
const {
return references_scalar_v; }
637 template <
class Scalar,
class Index>
638 struct MinMaxLocScalar {
639 Scalar min_val, max_val;
640 Index min_loc, max_loc;
642 KOKKOS_INLINE_FUNCTION
643 void operator=(
const MinMaxLocScalar& rhs) {
644 min_val = rhs.min_val;
645 min_loc = rhs.min_loc;
646 max_val = rhs.max_val;
647 max_loc = rhs.max_loc;
650 KOKKOS_INLINE_FUNCTION
651 void operator=(
const volatile MinMaxLocScalar& rhs)
volatile {
652 min_val = rhs.min_val;
653 min_loc = rhs.min_loc;
654 max_val = rhs.max_val;
655 max_loc = rhs.max_loc;
659 template <
class Scalar,
class Index,
class Space>
662 typedef typename std::remove_cv<Scalar>::type scalar_type;
663 typedef typename std::remove_cv<Index>::type index_type;
667 typedef MinMaxLoc reducer;
668 typedef MinMaxLocScalar<scalar_type, index_type> value_type;
673 result_view_type value;
674 bool references_scalar_v;
677 KOKKOS_INLINE_FUNCTION
678 MinMaxLoc(value_type& value_) : value(&value_), references_scalar_v(true) {}
680 KOKKOS_INLINE_FUNCTION
681 MinMaxLoc(
const result_view_type& value_)
682 : value(value_), references_scalar_v(false) {}
685 KOKKOS_INLINE_FUNCTION
686 void join(value_type& dest,
const value_type& src)
const {
687 if (src.min_val < dest.min_val) {
688 dest.min_val = src.min_val;
689 dest.min_loc = src.min_loc;
691 if (src.max_val > dest.max_val) {
692 dest.max_val = src.max_val;
693 dest.max_loc = src.max_loc;
697 KOKKOS_INLINE_FUNCTION
698 void join(
volatile value_type& dest,
const volatile value_type& src)
const {
699 if (src.min_val < dest.min_val) {
700 dest.min_val = src.min_val;
701 dest.min_loc = src.min_loc;
703 if (src.max_val > dest.max_val) {
704 dest.max_val = src.max_val;
705 dest.max_loc = src.max_loc;
709 KOKKOS_INLINE_FUNCTION
710 void init(value_type& val)
const {
711 val.max_val = reduction_identity<scalar_type>::max();
712 val.min_val = reduction_identity<scalar_type>::min();
713 val.max_loc = reduction_identity<index_type>::min();
714 val.min_loc = reduction_identity<index_type>::min();
717 KOKKOS_INLINE_FUNCTION
718 value_type& reference()
const {
return *value.data(); }
720 KOKKOS_INLINE_FUNCTION
721 result_view_type view()
const {
return value; }
723 KOKKOS_INLINE_FUNCTION
724 bool references_scalar()
const {
return references_scalar_v; }
730 template <
class T,
class ReturnType,
class ValueTraits>
731 struct ParallelReduceReturnValue;
733 template <
class ReturnType,
class FunctorType>
734 struct ParallelReduceReturnValue<
735 typename std::enable_if<Kokkos::is_view<ReturnType>::value>::type,
738 typedef InvalidType reducer_type;
740 typedef typename return_type::value_type value_type_scalar;
741 typedef typename return_type::value_type*
const value_type_array;
743 typedef typename if_c<return_type::rank == 0, value_type_scalar,
744 value_type_array>::type value_type;
746 static return_type& return_value(
ReturnType& return_val,
const FunctorType&) {
751 template <
class ReturnType,
class FunctorType>
752 struct ParallelReduceReturnValue<
753 typename std::enable_if<!Kokkos::is_view<ReturnType>::value &&
754 (!std::is_array<ReturnType>::value &&
755 !std::is_pointer<ReturnType>::value) &&
756 !Kokkos::is_reducer_type<ReturnType>::value>::type,
761 typedef InvalidType reducer_type;
763 typedef typename return_type::value_type value_type;
765 static return_type return_value(
ReturnType& return_val,
const FunctorType&) {
766 return return_type(&return_val);
770 template <
class ReturnType,
class FunctorType>
771 struct ParallelReduceReturnValue<
772 typename std::enable_if<(std::is_array<ReturnType>::value ||
773 std::is_pointer<ReturnType>::value)>::type,
779 typedef InvalidType reducer_type;
781 typedef typename return_type::value_type value_type[];
783 static return_type return_value(
ReturnType& return_val,
784 const FunctorType& functor) {
785 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
786 return return_type(return_val, functor.value_count);
788 if (std::is_array<ReturnType>::value)
789 return return_type(return_val);
791 return return_type(return_val, functor.value_count);
796 template <
class ReturnType,
class FunctorType>
797 struct ParallelReduceReturnValue<
798 typename std::enable_if<Kokkos::is_reducer_type<ReturnType>::value>::type,
802 typedef typename return_type::value_type value_type;
804 static return_type return_value(
ReturnType& return_val,
const FunctorType&) {
809 template <
class T,
class ReturnType,
class FunctorType>
810 struct ParallelReducePolicyType;
812 template <
class PolicyType,
class FunctorType>
813 struct ParallelReducePolicyType<
814 typename std::enable_if<
815 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type,
816 PolicyType, FunctorType> {
817 typedef PolicyType policy_type;
818 static PolicyType policy(
const PolicyType& policy_) {
return policy_; }
821 template <
class PolicyType,
class FunctorType>
822 struct ParallelReducePolicyType<
823 typename std::enable_if<std::is_integral<PolicyType>::value>::type,
824 PolicyType, FunctorType> {
825 typedef typename Impl::FunctorPolicyExecutionSpace<
826 FunctorType,
void>::execution_space 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 typedef FunctorType functor_type;
839 static const functor_type& functor(
const functor_type& functor) {
844 template <
class PolicyType,
class FunctorType,
class ReturnType>
845 struct ParallelReduceAdaptor {
846 typedef Impl::ParallelReduceReturnValue<void, ReturnType, FunctorType>
847 return_value_adapter;
848 #ifdef KOKKOS_IMPL_NEED_FUNCTOR_WRAPPER
849 typedef Impl::ParallelReduceFunctorType<
850 FunctorType, PolicyType,
typename return_value_adapter::value_type,
851 typename PolicyType::execution_space>
854 static inline void execute(
const std::string& label,
const PolicyType& policy,
855 const FunctorType& functor,
857 #if defined(KOKKOS_ENABLE_PROFILING)
859 if (Kokkos::Profiling::profileLibraryLoaded()) {
860 Kokkos::Impl::ParallelConstructName<FunctorType,
861 typename PolicyType::work_tag>
863 Kokkos::Profiling::beginParallelReduce(name.get(), 0, &kpID);
869 Kokkos::Impl::shared_allocation_tracking_disable();
870 #ifdef KOKKOS_IMPL_NEED_FUNCTOR_WRAPPER
871 Impl::ParallelReduce<
typename functor_adaptor::functor_type, PolicyType,
872 typename return_value_adapter::reducer_type>
873 closure(functor_adaptor::functor(functor), policy,
874 return_value_adapter::return_value(return_value, functor));
876 Impl::ParallelReduce<FunctorType, PolicyType,
877 typename return_value_adapter::reducer_type>
878 closure(functor, policy,
879 return_value_adapter::return_value(return_value, functor));
881 Kokkos::Impl::shared_allocation_tracking_enable();
884 #if defined(KOKKOS_ENABLE_PROFILING)
885 if (Kokkos::Profiling::profileLibraryLoaded()) {
886 Kokkos::Profiling::endParallelReduce(kpID);
909 template <
typename T>
910 struct ReducerHasTestReferenceFunction {
911 template <
typename E>
912 static std::true_type test_func(decltype(&E::references_scalar));
913 template <
typename E>
914 static std::false_type test_func(...);
917 value = std::is_same<std::true_type, decltype(test_func<T>(
nullptr))>::value
921 template <
class ExecutionSpace,
class T,
922 bool is_reducer = ReducerHasTestReferenceFunction<T>::value>
923 struct ParallelReduceFence {
924 static void fence(
const ExecutionSpace& execution_space,
const T&) {
925 execution_space.fence();
928 template <
class ExecutionSpace,
class... Args>
929 struct ParallelReduceFence<ExecutionSpace,
View<Args...>, false> {
930 static void fence(
const ExecutionSpace&,
const View<Args...>){};
932 template <
class ExecutionSpace,
class T>
933 struct ParallelReduceFence<ExecutionSpace, T, true> {
934 static void fence(
const ExecutionSpace& execution_space,
const T& reducer) {
935 if (reducer.references_scalar()) execution_space.fence();
977 template <
class PolicyType,
class FunctorType,
class ReturnType>
979 const std::string& label,
const PolicyType& policy,
980 const FunctorType& functor,
ReturnType& return_value,
981 typename std::enable_if<
982 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type* =
984 Impl::ParallelReduceAdaptor<PolicyType, FunctorType, ReturnType>::execute(
985 label, policy, functor, return_value);
986 Impl::ParallelReduceFence<
typename PolicyType::execution_space,
987 ReturnType>::fence(policy.space(), return_value);
990 template <
class PolicyType,
class FunctorType,
class ReturnType>
992 const PolicyType& policy,
const FunctorType& functor,
994 typename std::enable_if<
995 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type* =
997 Impl::ParallelReduceAdaptor<PolicyType, FunctorType, ReturnType>::execute(
998 "", policy, functor, return_value);
999 Impl::ParallelReduceFence<
typename PolicyType::execution_space,
1000 ReturnType>::fence(policy.space(), return_value);
1003 template <
class FunctorType,
class ReturnType>
1004 inline void parallel_reduce(
const size_t& policy,
const FunctorType& functor,
1006 typedef typename Impl::ParallelReducePolicyType<
1007 void, size_t, FunctorType>::policy_type policy_type;
1008 Impl::ParallelReduceAdaptor<policy_type, FunctorType, ReturnType>::execute(
1009 "", policy_type(0, policy), functor, return_value);
1010 Impl::ParallelReduceFence<typename policy_type::execution_space, ReturnType>::
1011 fence(
typename policy_type::execution_space(), return_value);
1014 template <
class FunctorType,
class ReturnType>
1015 inline void parallel_reduce(
const std::string& label,
const size_t& policy,
1016 const FunctorType& functor,
1018 typedef typename Impl::ParallelReducePolicyType<
1019 void, size_t, FunctorType>::policy_type policy_type;
1020 Impl::ParallelReduceAdaptor<policy_type, FunctorType, ReturnType>::execute(
1021 label, policy_type(0, policy), functor, return_value);
1022 Impl::ParallelReduceFence<typename policy_type::execution_space, ReturnType>::
1023 fence(
typename policy_type::execution_space(), return_value);
1028 template <
class PolicyType,
class FunctorType,
class ReturnType>
1030 const std::string& label,
const PolicyType& policy,
1031 const FunctorType& functor,
const ReturnType& return_value,
1032 typename std::enable_if<
1033 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type* =
1036 Impl::ParallelReduceAdaptor<PolicyType, FunctorType, ReturnType>::execute(
1037 label, policy, functor, return_value_impl);
1038 Impl::ParallelReduceFence<
typename PolicyType::execution_space,
1039 ReturnType>::fence(policy.space(), return_value);
1042 template <
class PolicyType,
class FunctorType,
class ReturnType>
1044 const PolicyType& policy,
const FunctorType& functor,
1046 typename std::enable_if<
1047 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type* =
1050 Impl::ParallelReduceAdaptor<PolicyType, FunctorType, ReturnType>::execute(
1051 "", policy, functor, return_value_impl);
1052 Impl::ParallelReduceFence<
typename PolicyType::execution_space,
1053 ReturnType>::fence(policy.space(), return_value);
1056 template <
class FunctorType,
class ReturnType>
1057 inline void parallel_reduce(
const size_t& policy,
const FunctorType& functor,
1059 typedef typename Impl::ParallelReducePolicyType<
1060 void, size_t, FunctorType>::policy_type policy_type;
1062 Impl::ParallelReduceAdaptor<policy_type, FunctorType, ReturnType>::execute(
1063 "", policy_type(0, policy), functor, return_value_impl);
1064 Impl::ParallelReduceFence<typename policy_type::execution_space, ReturnType>::
1065 fence(
typename policy_type::execution_space(), return_value);
1068 template <
class FunctorType,
class ReturnType>
1069 inline void parallel_reduce(
const std::string& label,
const size_t& policy,
1070 const FunctorType& functor,
1072 typedef typename Impl::ParallelReducePolicyType<
1073 void, size_t, FunctorType>::policy_type policy_type;
1075 Impl::ParallelReduceAdaptor<policy_type, FunctorType, ReturnType>::execute(
1076 label, policy_type(0, policy), functor, return_value_impl);
1077 Impl::ParallelReduceFence<typename policy_type::execution_space, ReturnType>::
1078 fence(
typename policy_type::execution_space(), return_value);
1083 template <
class PolicyType,
class FunctorType>
1085 const std::string& label,
const PolicyType& policy,
1086 const FunctorType& functor,
1087 typename std::enable_if<
1088 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type* =
1090 typedef Kokkos::Impl::FunctorValueTraits<FunctorType, void> ValueTraits;
1091 typedef typename Kokkos::Impl::if_c<
1092 (ValueTraits::StaticValueSize != 0),
typename ValueTraits::value_type,
1093 typename ValueTraits::pointer_type>::type value_type;
1096 Impl::FunctorAnalysis<Impl::FunctorPatternInterface::REDUCE, PolicyType,
1097 FunctorType>::has_final_member_function,
1098 "Calling parallel_reduce without either return value or final function.");
1102 result_view_type result_view;
1104 Impl::ParallelReduceAdaptor<PolicyType, FunctorType,
1105 result_view_type>::execute(label, policy, functor,
1109 template <
class PolicyType,
class FunctorType>
1111 const PolicyType& policy,
const FunctorType& functor,
1112 typename std::enable_if<
1113 Kokkos::Impl::is_execution_policy<PolicyType>::value>::type* =
1115 typedef Kokkos::Impl::FunctorValueTraits<FunctorType, void> ValueTraits;
1116 typedef typename Kokkos::Impl::if_c<
1117 (ValueTraits::StaticValueSize != 0),
typename ValueTraits::value_type,
1118 typename ValueTraits::pointer_type>::type value_type;
1121 Impl::FunctorAnalysis<Impl::FunctorPatternInterface::REDUCE, PolicyType,
1122 FunctorType>::has_final_member_function,
1123 "Calling parallel_reduce without either return value or final function.");
1127 result_view_type result_view;
1129 Impl::ParallelReduceAdaptor<PolicyType, FunctorType,
1130 result_view_type>::execute(
"", policy, functor,
1134 template <
class FunctorType>
1135 inline void parallel_reduce(
const size_t& policy,
const FunctorType& functor) {
1136 typedef typename Impl::ParallelReducePolicyType<
1137 void, size_t, FunctorType>::policy_type policy_type;
1138 typedef Kokkos::Impl::FunctorValueTraits<FunctorType, void> ValueTraits;
1139 typedef typename Kokkos::Impl::if_c<
1140 (ValueTraits::StaticValueSize != 0),
typename ValueTraits::value_type,
1141 typename ValueTraits::pointer_type>::type value_type;
1144 Impl::FunctorAnalysis<Impl::FunctorPatternInterface::REDUCE,
1146 FunctorType>::has_final_member_function,
1147 "Calling parallel_reduce without either return value or final function.");
1151 result_view_type result_view;
1153 Impl::ParallelReduceAdaptor<policy_type, FunctorType,
1154 result_view_type>::execute(
"",
1155 policy_type(0, policy),
1156 functor, result_view);
1159 template <
class FunctorType>
1160 inline void parallel_reduce(
const std::string& label,
const size_t& policy,
1161 const FunctorType& functor) {
1162 typedef typename Impl::ParallelReducePolicyType<
1163 void, size_t, FunctorType>::policy_type policy_type;
1164 typedef Kokkos::Impl::FunctorValueTraits<FunctorType, void> ValueTraits;
1165 typedef typename Kokkos::Impl::if_c<
1166 (ValueTraits::StaticValueSize != 0),
typename ValueTraits::value_type,
1167 typename ValueTraits::pointer_type>::type value_type;
1170 Impl::FunctorAnalysis<Impl::FunctorPatternInterface::REDUCE,
1172 FunctorType>::has_final_member_function,
1173 "Calling parallel_reduce without either return value or final function.");
1177 result_view_type result_view;
1179 Impl::ParallelReduceAdaptor<policy_type, FunctorType,
1180 result_view_type>::execute(label,
1181 policy_type(0, policy),
1182 functor, result_view);
1187 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
1190 namespace Experimental {
1196 using Kokkos::MaxLoc;
1198 using Kokkos::MinLoc;
1199 using Kokkos::MinMax;
1200 using Kokkos::MinMaxLoc;
1201 using Kokkos::MinMaxLocScalar;
1202 using Kokkos::MinMaxScalar;
1205 using Kokkos::ValLocScalar;
1210 #endif // KOKKOS_PARALLEL_REDUCE_HPP
Memory management for host memory.
void parallel_reduce(const std::string &label, const PolicyType &policy, const FunctorType &functor, ReturnType &return_value, typename std::enable_if< Kokkos::Impl::is_execution_policy< PolicyType >::value >::type *=nullptr)
Parallel reduction.
Execution policy for work over a range of an integral type.