17#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
18#include <Kokkos_Macros.hpp>
20 "Including non-public Kokkos header files is not allowed.");
22#ifndef KOKKOS_EXECPOLICY_HPP
23#define KOKKOS_EXECPOLICY_HPP
25#include <Kokkos_Core_fwd.hpp>
26#include <impl/Kokkos_Traits.hpp>
27#include <impl/Kokkos_Error.hpp>
28#include <impl/Kokkos_AnalyzePolicy.hpp>
29#include <Kokkos_Concepts.hpp>
30#include <Kokkos_TypeInfo.hpp>
31#ifndef KOKKOS_ENABLE_IMPL_TYPEINFO
40struct ParallelForTag {};
41struct ParallelScanTag {};
42struct ParallelReduceTag {};
46 explicit ChunkSize(
int value_) : value(value_) {}
47#ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
48 template <
typename T =
void>
49 KOKKOS_DEPRECATED_WITH_COMMENT(
"ChunkSize should be constructed explicitly.")
50 ChunkSize(
int value_) : value(value_) {}
75template <
class... Properties>
76class RangePolicy :
public Impl::PolicyTraits<Properties...> {
78 using traits = Impl::PolicyTraits<Properties...>;
81 typename traits::execution_space m_space;
82 typename traits::index_type m_begin;
83 typename traits::index_type m_end;
84 typename traits::index_type m_granularity;
85 typename traits::index_type m_granularity_mask;
87 template <
class... OtherProperties>
88 friend class RangePolicy;
93 using member_type =
typename traits::index_type;
94 using index_type =
typename traits::index_type;
96 KOKKOS_INLINE_FUNCTION
const typename traits::execution_space& space()
const {
99 KOKKOS_INLINE_FUNCTION member_type begin()
const {
return m_begin; }
100 KOKKOS_INLINE_FUNCTION member_type end()
const {
return m_end; }
107 void operator()(
const int&)
const {}
109 template <
class... OtherProperties>
110 RangePolicy(
const RangePolicy<OtherProperties...>& p)
115 m_granularity(p.m_granularity),
116 m_granularity_mask(p.m_granularity_mask) {}
123 m_granularity_mask(0) {}
126 template <
typename IndexType1,
typename IndexType2,
127 std::enable_if_t<(std::is_convertible_v<IndexType1, member_type> &&
128 std::is_convertible_v<IndexType2, member_type>),
130 inline RangePolicy(
const IndexType1 work_begin,
const IndexType2 work_end)
131 : RangePolicy(typename traits::execution_space(), work_begin, work_end) {}
134 template <
typename IndexType1,
typename IndexType2,
135 std::enable_if_t<(std::is_convertible_v<IndexType1, member_type> &&
136 std::is_convertible_v<IndexType2, member_type>),
138 inline RangePolicy(
const typename traits::execution_space& work_space,
139 const IndexType1 work_begin,
const IndexType2 work_end)
140 : m_space(work_space),
144 m_granularity_mask(0) {
145 check_conversion_safety(work_begin);
146 check_conversion_safety(work_end);
147 check_bounds_validity();
148 set_auto_chunk_size();
151 template <
typename IndexType1,
typename IndexType2,
152 std::enable_if_t<(std::is_convertible_v<IndexType1, member_type> &&
153 std::is_convertible_v<IndexType2, member_type>),
155 RangePolicy(
const typename traits::execution_space& work_space,
156 const IndexType1 work_begin,
const IndexType2 work_end,
158 : m_space(work_space),
162 m_granularity_mask(0) {
163 check_conversion_safety(work_begin);
164 check_conversion_safety(work_end);
165 check_bounds_validity();
170 template <
typename IndexType1,
typename IndexType2,
typename... Args,
171 std::enable_if_t<(std::is_convertible_v<IndexType1, member_type> &&
172 std::is_convertible_v<IndexType2, member_type>),
174 RangePolicy(
const IndexType1 work_begin,
const IndexType2 work_end,
176 : RangePolicy(
typename traits::execution_space(), work_begin, work_end,
180#ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
181 KOKKOS_DEPRECATED_WITH_COMMENT(
"Use set_chunk_size instead")
182 inline
void set(ChunkSize chunksize) {
183 m_granularity = chunksize.value;
184 m_granularity_mask = m_granularity - 1;
190 inline member_type
chunk_size()
const {
return m_granularity; }
195 m_granularity_mask = m_granularity - 1;
201 inline void set_auto_chunk_size() {
202#ifdef KOKKOS_ENABLE_SYCL
203 if (std::is_same_v<typename traits::execution_space, Kokkos::SYCL>) {
207 m_granularity_mask = 0;
211 auto concurrency =
static_cast<int64_t
>(m_space.concurrency());
212 if (concurrency == 0) concurrency = 1;
214 if (m_granularity > 0) {
215 if (!Impl::is_integral_power_of_two(m_granularity))
216 Kokkos::abort(
"RangePolicy blocking granularity must be power of two");
219 int64_t new_chunk_size = 1;
220 while (new_chunk_size * 100 * concurrency <
221 static_cast<int64_t
>(m_end - m_begin))
223 if (new_chunk_size < 128) {
225 while ((new_chunk_size * 40 * concurrency <
226 static_cast<int64_t
>(m_end - m_begin)) &&
227 (new_chunk_size < 128))
230 m_granularity = new_chunk_size;
231 m_granularity_mask = m_granularity - 1;
234 void check_bounds_validity() {
235 if (m_end < m_begin) {
236 std::string msg =
"Kokkos::RangePolicy bounds error: The lower bound (" +
237 std::to_string(m_begin) +
238 ") is greater than the upper bound (" +
239 std::to_string(m_end) +
").\n";
240#ifndef KOKKOS_ENABLE_DEPRECATED_CODE_4
241 Kokkos::abort(msg.c_str());
245#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
246 Kokkos::Impl::log_warning(msg);
252 template <
typename IndexType>
253 static void check_conversion_safety([[maybe_unused]]
const IndexType bound) {
255 if constexpr (std::is_convertible_v<member_type, IndexType>) {
256#if !defined(KOKKOS_ENABLE_DEPRECATED_CODE_4) || \
257 defined(KOKKOS_ENABLE_DEPRECATION_WARNINGS)
260 "Kokkos::RangePolicy bound type error: an unsafe implicit conversion "
261 "is performed on a bound (" +
262 std::to_string(bound) +
264 "not preserve its original value.\n";
267 if constexpr (std::is_arithmetic_v<member_type> &&
268 (std::is_signed_v<IndexType> !=
269 std::is_signed_v<member_type>)) {
271 if constexpr (std::is_signed_v<IndexType>)
272 warn |= (bound <
static_cast<IndexType
>(
273 std::numeric_limits<member_type>::min()));
276 if constexpr (std::is_signed_v<member_type>)
277 warn |= (bound >
static_cast<IndexType
>(
278 std::numeric_limits<member_type>::max()));
283 (
static_cast<IndexType
>(
static_cast<member_type
>(bound)) != bound);
286#ifndef KOKKOS_ENABLE_DEPRECATED_CODE_4
287 Kokkos::abort(msg.c_str());
290#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
291 Kokkos::Impl::log_warning(msg);
304 using work_tag =
typename RangePolicy<Properties...>::work_tag;
305 using member_type =
typename RangePolicy<Properties...>::member_type;
307 KOKKOS_INLINE_FUNCTION member_type begin()
const {
return m_begin; }
308 KOKKOS_INLINE_FUNCTION member_type end()
const {
return m_end; }
314 KOKKOS_INLINE_FUNCTION
315 WorkRange(
const RangePolicy& range,
const int part_rank,
317 : m_begin(0), m_end(0) {
320 const member_type work_part =
321 ((((range.end() - range.begin()) + (part_size - 1)) / part_size) +
322 range.m_granularity_mask) &
323 ~member_type(range.m_granularity_mask);
325 m_begin = range.begin() + work_part * part_rank;
326 m_end = m_begin + work_part;
328 if (range.end() < m_begin) m_begin = range.end();
329 if (range.end() < m_end) m_end = range.end();
341RangePolicy() -> RangePolicy<>;
343RangePolicy(int64_t, int64_t) -> RangePolicy<>;
344RangePolicy(int64_t, int64_t, ChunkSize
const&) -> RangePolicy<>;
346RangePolicy(DefaultExecutionSpace
const&, int64_t, int64_t) -> RangePolicy<>;
347RangePolicy(DefaultExecutionSpace
const&, int64_t, int64_t, ChunkSize
const&)
350template <
typename ES,
typename = std::enable_if_t<is_execution_space_v<ES>>>
351RangePolicy(ES
const&, int64_t, int64_t) -> RangePolicy<ES>;
353template <
typename ES,
typename = std::enable_if_t<is_execution_space_v<ES>>>
354RangePolicy(ES
const&, int64_t, int64_t, ChunkSize
const&) -> RangePolicy<ES>;
365template <
class ExecSpace,
class... Properties>
366class TeamPolicyInternal :
public Impl::PolicyTraits<Properties...> {
368 using traits = Impl::PolicyTraits<Properties...>;
371 using index_type =
typename traits::index_type;
384 template <
class FunctorType>
385 static int team_size_max(
const FunctorType&);
397 template <
class FunctorType>
398 static int team_size_recommended(
const FunctorType&);
400 template <
class FunctorType>
401 static int team_size_recommended(
const FunctorType&,
const int&);
403 template <
class FunctorType>
404 int team_size_recommended(
const FunctorType& functor,
405 const int vector_length);
409 TeamPolicyInternal(
const typename traits::execution_space&,
410 int league_size_request,
int team_size_request,
411 int vector_length_request = 1);
413 TeamPolicyInternal(
const typename traits::execution_space&,
414 int league_size_request,
const Kokkos::AUTO_t&,
415 int vector_length_request = 1);
419 TeamPolicyInternal(
int league_size_request,
int team_size_request,
420 int vector_length_request = 1);
422 TeamPolicyInternal(
int league_size_request,
const Kokkos::AUTO_t&,
423 int vector_length_request = 1);
434 KOKKOS_INLINE_FUNCTION
int league_size()
const;
441 KOKKOS_INLINE_FUNCTION
int team_size()
const;
445 inline bool impl_auto_team_size()
const;
448 inline bool impl_auto_vector_length()
const;
450 static int vector_length_max();
452 KOKKOS_INLINE_FUNCTION
int impl_vector_length()
const;
454 inline typename traits::index_type chunk_size()
const;
456 inline TeamPolicyInternal& set_chunk_size(
int chunk_size);
463 KOKKOS_INLINE_FUNCTION
464 typename traits::execution_space::scratch_memory_space
team_shmem()
const;
483 template <
class JoinOp>
485 const typename JoinOp::value_type,
const JoinOp&)
const;
492 template <
typename Type>
493 KOKKOS_INLINE_FUNCTION Type
team_scan(
const Type& value)
const;
504 template <
typename Type>
505 KOKKOS_INLINE_FUNCTION Type
team_scan(
const Type& value,
506 Type*
const global_accum)
const;
512 PerTeamValue(
size_t arg);
515struct PerThreadValue {
517 PerThreadValue(
size_t arg);
520template <
class iType,
class... Args>
521struct ExtractVectorLength {
522 static inline iType value(
523 std::enable_if_t<std::is_integral_v<iType>, iType> val, Args...) {
526 static inline std::enable_if_t<!std::is_integral_v<iType>,
int> value(
527 std::enable_if_t<!std::is_integral_v<iType>, iType>, Args...) {
532template <
class iType,
class... Args>
533inline std::enable_if_t<std::is_integral_v<iType>, iType> extract_vector_length(
534 iType val, Args...) {
538template <
class iType,
class... Args>
539inline std::enable_if_t<!std::is_integral_v<iType>,
int> extract_vector_length(
546Impl::PerTeamValue PerTeam(
const size_t& arg);
547Impl::PerThreadValue PerThread(
const size_t& arg);
549struct ScratchRequest {
555 inline ScratchRequest(
const int& level_,
556 const Impl::PerTeamValue& team_value) {
558 per_team = team_value.value;
562 inline ScratchRequest(
const int& level_,
563 const Impl::PerThreadValue& thread_value) {
566 per_thread = thread_value.value;
569 inline ScratchRequest(
const int& level_,
const Impl::PerTeamValue& team_value,
570 const Impl::PerThreadValue& thread_value) {
572 per_team = team_value.value;
573 per_thread = thread_value.value;
576 inline ScratchRequest(
const int& level_,
577 const Impl::PerThreadValue& thread_value,
578 const Impl::PerTeamValue& team_value) {
580 per_team = team_value.value;
581 per_thread = thread_value.value;
586void team_policy_check_valid_storage_level_argument(
int level);
614template <
class... Properties>
616 :
public Impl::TeamPolicyInternal<
617 typename Impl::PolicyTraits<Properties...>::execution_space,
619 using internal_policy = Impl::TeamPolicyInternal<
620 typename Impl::PolicyTraits<Properties...>::execution_space,
623 template <
class... OtherProperties>
624 friend class TeamPolicy;
627 using traits = Impl::PolicyTraits<Properties...>;
629 using execution_policy = TeamPolicy<Properties...>;
631 TeamPolicy() : internal_policy(0, AUTO) {}
635 int league_size_request,
int team_size_request,
636 int vector_length_request = 1)
637 : internal_policy(space_, league_size_request, team_size_request,
638 vector_length_request) {}
640 TeamPolicy(
const typename traits::execution_space& space_,
641 int league_size_request,
const Kokkos::AUTO_t&,
642 int vector_length_request = 1)
643 : internal_policy(space_, league_size_request, Kokkos::AUTO(),
644 vector_length_request) {}
646 TeamPolicy(
const typename traits::execution_space& space_,
647 int league_size_request,
const Kokkos::AUTO_t&,
648 const Kokkos::AUTO_t&)
649 : internal_policy(space_, league_size_request, Kokkos::AUTO(),
651 TeamPolicy(
const typename traits::execution_space& space_,
652 int league_size_request,
const int team_size_request,
653 const Kokkos::AUTO_t&)
654 : internal_policy(space_, league_size_request, team_size_request,
659 int vector_length_request = 1)
660 : internal_policy(league_size_request, team_size_request,
661 vector_length_request) {}
663 TeamPolicy(
int league_size_request,
const Kokkos::AUTO_t&,
664 int vector_length_request = 1)
665 : internal_policy(league_size_request, Kokkos::AUTO(),
666 vector_length_request) {}
668 TeamPolicy(
int league_size_request,
const Kokkos::AUTO_t&,
669 const Kokkos::AUTO_t&)
670 : internal_policy(league_size_request, Kokkos::AUTO(), Kokkos::AUTO()) {}
671 TeamPolicy(
int league_size_request,
const int team_size_request,
672 const Kokkos::AUTO_t&)
673 : internal_policy(league_size_request, team_size_request,
676 template <
class... OtherProperties>
677 TeamPolicy(
const TeamPolicy<OtherProperties...> p) : internal_policy(p) {
680 internal_policy::traits::operator=(p);
684 TeamPolicy(
const internal_policy& p) : internal_policy(p) {}
687 inline TeamPolicy& set_chunk_size(
int chunk) {
688 static_assert(std::is_same<
decltype(internal_policy::set_chunk_size(chunk)),
689 internal_policy&>::value,
690 "internal set_chunk_size should return a reference");
691 return static_cast<TeamPolicy&
>(internal_policy::set_chunk_size(chunk));
694 inline TeamPolicy& set_scratch_size(
const int& level,
695 const Impl::PerTeamValue& per_team) {
696 static_assert(std::is_same<
decltype(internal_policy::set_scratch_size(
698 internal_policy&>::value,
699 "internal set_chunk_size should return a reference");
701 team_policy_check_valid_storage_level_argument(level);
702 return static_cast<TeamPolicy&
>(
703 internal_policy::set_scratch_size(level, per_team));
705 inline TeamPolicy& set_scratch_size(
const int& level,
706 const Impl::PerThreadValue& per_thread) {
707 team_policy_check_valid_storage_level_argument(level);
708 return static_cast<TeamPolicy&
>(
709 internal_policy::set_scratch_size(level, per_thread));
711 inline TeamPolicy& set_scratch_size(
const int& level,
712 const Impl::PerTeamValue& per_team,
713 const Impl::PerThreadValue& per_thread) {
714 team_policy_check_valid_storage_level_argument(level);
715 return static_cast<TeamPolicy&
>(
716 internal_policy::set_scratch_size(level, per_team, per_thread));
718 inline TeamPolicy& set_scratch_size(
const int& level,
719 const Impl::PerThreadValue& per_thread,
720 const Impl::PerTeamValue& per_team) {
721 team_policy_check_valid_storage_level_argument(level);
722 return static_cast<TeamPolicy&
>(
723 internal_policy::set_scratch_size(level, per_team, per_thread));
729TeamPolicy() -> TeamPolicy<>;
731TeamPolicy(
int,
int) -> TeamPolicy<>;
732TeamPolicy(
int,
int,
int) -> TeamPolicy<>;
733TeamPolicy(
int, Kokkos::AUTO_t
const&) -> TeamPolicy<>;
734TeamPolicy(
int, Kokkos::AUTO_t
const&,
int) -> TeamPolicy<>;
735TeamPolicy(
int, Kokkos::AUTO_t
const&, Kokkos::AUTO_t
const&) -> TeamPolicy<>;
736TeamPolicy(
int,
int, Kokkos::AUTO_t
const&) -> TeamPolicy<>;
740TeamPolicy(DefaultExecutionSpace
const&,
int,
int) -> TeamPolicy<>;
741TeamPolicy(DefaultExecutionSpace
const&,
int,
int,
int) -> TeamPolicy<>;
742TeamPolicy(DefaultExecutionSpace
const&,
int, Kokkos::AUTO_t
const&)
744TeamPolicy(DefaultExecutionSpace
const&,
int, Kokkos::AUTO_t
const&,
int)
746TeamPolicy(DefaultExecutionSpace
const&,
int, Kokkos::AUTO_t
const&,
747 Kokkos::AUTO_t
const&) -> TeamPolicy<>;
748TeamPolicy(DefaultExecutionSpace
const&,
int,
int, Kokkos::AUTO_t
const&)
753template <
typename ES,
754 typename = std::enable_if_t<Kokkos::is_execution_space_v<ES>>>
755TeamPolicy(ES
const&,
int,
int) -> TeamPolicy<ES>;
757template <
typename ES,
758 typename = std::enable_if_t<Kokkos::is_execution_space_v<ES>>>
759TeamPolicy(ES
const&,
int,
int,
int) -> TeamPolicy<ES>;
761template <
typename ES,
762 typename = std::enable_if_t<Kokkos::is_execution_space_v<ES>>>
763TeamPolicy(ES
const&,
int, Kokkos::AUTO_t
const&) -> TeamPolicy<ES>;
765template <
typename ES,
766 typename = std::enable_if_t<Kokkos::is_execution_space_v<ES>>>
767TeamPolicy(ES
const&,
int, Kokkos::AUTO_t
const&,
int) -> TeamPolicy<ES>;
769template <
typename ES,
770 typename = std::enable_if_t<Kokkos::is_execution_space_v<ES>>>
771TeamPolicy(ES
const&,
int, Kokkos::AUTO_t
const&, Kokkos::AUTO_t
const&)
774template <
typename ES,
775 typename = std::enable_if_t<Kokkos::is_execution_space_v<ES>>>
776TeamPolicy(ES
const&,
int,
int, Kokkos::AUTO_t
const&) -> TeamPolicy<ES>;
780template <
typename iType,
class TeamMemberType>
781struct TeamThreadRangeBoundariesStruct {
783 KOKKOS_INLINE_FUNCTION
static iType ibegin(
const iType& arg_begin,
784 const iType& arg_end,
785 const iType& arg_rank,
786 const iType& arg_size) {
788 ((arg_end - arg_begin + arg_size - 1) / arg_size) * arg_rank;
791 KOKKOS_INLINE_FUNCTION
static iType iend(
const iType& arg_begin,
792 const iType& arg_end,
793 const iType& arg_rank,
794 const iType& arg_size) {
797 ((arg_end - arg_begin + arg_size - 1) / arg_size) * (arg_rank + 1);
798 return end_ < arg_end ? end_ : arg_end;
802 using index_type = iType;
805 enum { increment = 1 };
806 const TeamMemberType& thread;
808 KOKKOS_INLINE_FUNCTION
809 TeamThreadRangeBoundariesStruct(
const TeamMemberType& arg_thread,
810 const iType& arg_end)
812 ibegin(0, arg_end, arg_thread.team_rank(), arg_thread.team_size())),
813 end(iend(0, arg_end, arg_thread.team_rank(), arg_thread.team_size())),
814 thread(arg_thread) {}
816 KOKKOS_INLINE_FUNCTION
817 TeamThreadRangeBoundariesStruct(
const TeamMemberType& arg_thread,
818 const iType& arg_begin,
const iType& arg_end)
819 : start(ibegin(arg_begin, arg_end, arg_thread.team_rank(),
820 arg_thread.team_size())),
821 end(iend(arg_begin, arg_end, arg_thread.team_rank(),
822 arg_thread.team_size())),
823 thread(arg_thread) {}
826template <
typename iType,
class TeamMemberType>
827struct TeamVectorRangeBoundariesStruct {
829 KOKKOS_INLINE_FUNCTION
static iType ibegin(
const iType& arg_begin,
830 const iType& arg_end,
831 const iType& arg_rank,
832 const iType& arg_size) {
834 ((arg_end - arg_begin + arg_size - 1) / arg_size) * arg_rank;
837 KOKKOS_INLINE_FUNCTION
static iType iend(
const iType& arg_begin,
838 const iType& arg_end,
839 const iType& arg_rank,
840 const iType& arg_size) {
843 ((arg_end - arg_begin + arg_size - 1) / arg_size) * (arg_rank + 1);
844 return end_ < arg_end ? end_ : arg_end;
848 using index_type = iType;
851 enum { increment = 1 };
852 const TeamMemberType& thread;
854 KOKKOS_INLINE_FUNCTION
855 TeamVectorRangeBoundariesStruct(
const TeamMemberType& arg_thread,
856 const iType& arg_end)
858 ibegin(0, arg_end, arg_thread.team_rank(), arg_thread.team_size())),
859 end(iend(0, arg_end, arg_thread.team_rank(), arg_thread.team_size())),
860 thread(arg_thread) {}
862 KOKKOS_INLINE_FUNCTION
863 TeamVectorRangeBoundariesStruct(
const TeamMemberType& arg_thread,
864 const iType& arg_begin,
const iType& arg_end)
865 : start(ibegin(arg_begin, arg_end, arg_thread.team_rank(),
866 arg_thread.team_size())),
867 end(iend(arg_begin, arg_end, arg_thread.team_rank(),
868 arg_thread.team_size())),
869 thread(arg_thread) {}
872template <
typename iType,
class TeamMemberType>
873struct ThreadVectorRangeBoundariesStruct {
874 using index_type = iType;
875 const index_type start;
876 const index_type end;
877 enum { increment = 1 };
879 KOKKOS_INLINE_FUNCTION
880 constexpr ThreadVectorRangeBoundariesStruct(
const TeamMemberType,
881 const index_type& count) noexcept
882 : start(
static_cast<index_type
>(0)), end(count) {}
884 KOKKOS_INLINE_FUNCTION
885 constexpr ThreadVectorRangeBoundariesStruct(
886 const TeamMemberType,
const index_type& arg_begin,
887 const index_type& arg_end) noexcept
888 : start(
static_cast<index_type
>(arg_begin)), end(arg_end) {}
891template <
class TeamMemberType>
892struct ThreadSingleStruct {
893 const TeamMemberType& team_member;
894 KOKKOS_INLINE_FUNCTION
895 ThreadSingleStruct(
const TeamMemberType& team_member_)
896 : team_member(team_member_) {}
899template <
class TeamMemberType>
900struct VectorSingleStruct {
901 const TeamMemberType& team_member;
902 KOKKOS_INLINE_FUNCTION
903 VectorSingleStruct(
const TeamMemberType& team_member_)
904 : team_member(team_member_) {}
916template <
typename iType,
class TeamMemberType,
class _never_use_this_overload>
917KOKKOS_INLINE_FUNCTION_DELETED
918 Impl::TeamThreadRangeBoundariesStruct<iType, TeamMemberType>
919 TeamThreadRange(
const TeamMemberType&,
const iType& count) =
delete;
928template <
typename iType1,
typename iType2,
class TeamMemberType,
929 class _never_use_this_overload>
930KOKKOS_INLINE_FUNCTION_DELETED Impl::TeamThreadRangeBoundariesStruct<
931 std::common_type_t<iType1, iType2>, TeamMemberType>
932TeamThreadRange(
const TeamMemberType&,
const iType1& begin,
933 const iType2& end) =
delete;
942template <
typename iType,
class TeamMemberType,
class _never_use_this_overload>
943KOKKOS_INLINE_FUNCTION_DELETED
944 Impl::TeamThreadRangeBoundariesStruct<iType, TeamMemberType>
945 TeamVectorRange(
const TeamMemberType&,
const iType& count) =
delete;
954template <
typename iType1,
typename iType2,
class TeamMemberType,
955 class _never_use_this_overload>
956KOKKOS_INLINE_FUNCTION_DELETED Impl::TeamThreadRangeBoundariesStruct<
957 std::common_type_t<iType1, iType2>, TeamMemberType>
958TeamVectorRange(
const TeamMemberType&,
const iType1& begin,
959 const iType2& end) =
delete;
968template <
typename iType,
class TeamMemberType,
class _never_use_this_overload>
969KOKKOS_INLINE_FUNCTION_DELETED
970 Impl::ThreadVectorRangeBoundariesStruct<iType, TeamMemberType>
971 ThreadVectorRange(
const TeamMemberType&,
const iType& count) =
delete;
973template <
typename iType1,
typename iType2,
class TeamMemberType,
974 class _never_use_this_overload>
975KOKKOS_INLINE_FUNCTION_DELETED Impl::ThreadVectorRangeBoundariesStruct<
976 std::common_type_t<iType1, iType2>, TeamMemberType>
977ThreadVectorRange(
const TeamMemberType&,
const iType1& arg_begin,
978 const iType2& arg_end) =
delete;
982enum class TeamMDRangeLastNestLevel :
bool { NotLastNestLevel, LastNestLevel };
983enum class TeamMDRangeParThread :
bool { NotParThread, ParThread };
984enum class TeamMDRangeParVector :
bool { NotParVector, ParVector };
985enum class TeamMDRangeThreadAndVector :
bool { NotBoth, Both };
987template <
typename Rank, TeamMDRangeThreadAndVector ThreadAndVector>
988struct HostBasedNestLevel;
990template <
typename Rank, TeamMDRangeThreadAndVector ThreadAndVector>
991struct AcceleratorBasedNestLevel;
1001template <
typename Rank,
typename ExecSpace,
1002 TeamMDRangeThreadAndVector ThreadAndVector>
1003struct ThreadAndVectorNestLevel;
1005struct NoReductionTag {};
1007template <
typename Rank,
typename TeamMDPolicy,
typename Lambda,
1008 typename ReductionValueType>
1009KOKKOS_INLINE_FUNCTION
void md_parallel_impl(TeamMDPolicy
const& policy,
1010 Lambda
const& lambda,
1011 ReductionValueType&& val);
1014template <
typename Rank,
typename TeamHandle>
1015struct TeamThreadMDRange;
1017template <
unsigned N, Iterate OuterDir, Iterate InnerDir,
typename TeamHandle>
1018struct TeamThreadMDRange<Rank<N, OuterDir, InnerDir>, TeamHandle> {
1019 using NestLevelType = int;
1020 using BoundaryType = int;
1021 using TeamHandleType = TeamHandle;
1022 using ExecutionSpace =
typename TeamHandleType::execution_space;
1023 using ArrayLayout =
typename ExecutionSpace::array_layout;
1025 static constexpr NestLevelType total_nest_level =
1026 Rank<N, OuterDir, InnerDir>::rank;
1027 static constexpr Iterate iter = OuterDir;
1028 static constexpr auto par_thread = Impl::TeamMDRangeParThread::ParThread;
1029 static constexpr auto par_vector = Impl::TeamMDRangeParVector::NotParVector;
1031 static constexpr Iterate direction =
1032 OuterDir == Iterate::Default ? Impl::layout_iterate_type_selector<
1033 ArrayLayout>::outer_iteration_pattern
1036 template <
class... Args>
1037 KOKKOS_FUNCTION TeamThreadMDRange(TeamHandleType
const& team_, Args&&... args)
1038 : team(team_), boundaries{static_cast<BoundaryType>(args)...} {
1039 static_assert(
sizeof...(Args) == total_nest_level);
1042 TeamHandleType
const& team;
1043 BoundaryType boundaries[total_nest_level];
1046template <
typename TeamHandle,
typename... Args>
1047KOKKOS_DEDUCTION_GUIDE TeamThreadMDRange(TeamHandle
const&, Args&&...)
1048 -> TeamThreadMDRange<Rank<
sizeof...(Args), Iterate::Default>, TeamHandle>;
1050template <
typename Rank,
typename TeamHandle>
1051struct ThreadVectorMDRange;
1053template <
unsigned N, Iterate OuterDir, Iterate InnerDir,
typename TeamHandle>
1054struct ThreadVectorMDRange<Rank<N, OuterDir, InnerDir>, TeamHandle> {
1055 using NestLevelType = int;
1056 using BoundaryType = int;
1057 using TeamHandleType = TeamHandle;
1058 using ExecutionSpace =
typename TeamHandleType::execution_space;
1059 using ArrayLayout =
typename ExecutionSpace::array_layout;
1061 static constexpr NestLevelType total_nest_level =
1062 Rank<N, OuterDir, InnerDir>::rank;
1063 static constexpr Iterate iter = OuterDir;
1064 static constexpr auto par_thread = Impl::TeamMDRangeParThread::NotParThread;
1065 static constexpr auto par_vector = Impl::TeamMDRangeParVector::ParVector;
1067 static constexpr Iterate direction =
1068 OuterDir == Iterate::Default ? Impl::layout_iterate_type_selector<
1069 ArrayLayout>::outer_iteration_pattern
1072 template <
class... Args>
1073 KOKKOS_INLINE_FUNCTION ThreadVectorMDRange(TeamHandleType
const& team_,
1075 : team(team_), boundaries{static_cast<BoundaryType>(args)...} {
1076 static_assert(
sizeof...(Args) == total_nest_level);
1079 TeamHandleType
const& team;
1080 BoundaryType boundaries[total_nest_level];
1083template <
typename TeamHandle,
typename... Args>
1084KOKKOS_DEDUCTION_GUIDE ThreadVectorMDRange(TeamHandle
const&, Args&&...)
1085 -> ThreadVectorMDRange<Rank<
sizeof...(Args), Iterate::Default>, TeamHandle>;
1087template <
typename Rank,
typename TeamHandle>
1088struct TeamVectorMDRange;
1090template <
unsigned N, Iterate OuterDir, Iterate InnerDir,
typename TeamHandle>
1091struct TeamVectorMDRange<Rank<N, OuterDir, InnerDir>, TeamHandle> {
1092 using NestLevelType = int;
1093 using BoundaryType = int;
1094 using TeamHandleType = TeamHandle;
1095 using ExecutionSpace =
typename TeamHandleType::execution_space;
1096 using ArrayLayout =
typename ExecutionSpace::array_layout;
1098 static constexpr NestLevelType total_nest_level =
1099 Rank<N, OuterDir, InnerDir>::rank;
1100 static constexpr Iterate iter = OuterDir;
1101 static constexpr auto par_thread = Impl::TeamMDRangeParThread::ParThread;
1102 static constexpr auto par_vector = Impl::TeamMDRangeParVector::ParVector;
1104 static constexpr Iterate direction =
1105 iter == Iterate::Default ? Impl::layout_iterate_type_selector<
1106 ArrayLayout>::outer_iteration_pattern
1109 template <
class... Args>
1110 KOKKOS_INLINE_FUNCTION TeamVectorMDRange(TeamHandleType
const& team_,
1112 : team(team_), boundaries{static_cast<BoundaryType>(args)...} {
1113 static_assert(
sizeof...(Args) == total_nest_level);
1116 TeamHandleType
const& team;
1117 BoundaryType boundaries[total_nest_level];
1120template <
typename TeamHandle,
typename... Args>
1121KOKKOS_DEDUCTION_GUIDE TeamVectorMDRange(TeamHandle
const&, Args&&...)
1122 -> TeamVectorMDRange<Rank<
sizeof...(Args), Iterate::Default>, TeamHandle>;
1124template <
typename Rank,
typename TeamHandle,
typename Lambda,
1125 typename ReducerValueType>
1126KOKKOS_INLINE_FUNCTION
void parallel_reduce(
1127 TeamThreadMDRange<Rank, TeamHandle>
const& policy, Lambda
const& lambda,
1128 ReducerValueType& val) {
1130 !std::is_array_v<ReducerValueType> &&
1131 !std::is_pointer_v<ReducerValueType> &&
1132 !Kokkos::is_reducer_v<ReducerValueType>,
1133 "Only scalar return types are allowed!");
1135 val = ReducerValueType{};
1136 Impl::md_parallel_impl<Rank>(policy, lambda, val);
1137 policy.team.team_reduce(
1138 Kokkos::Sum<ReducerValueType, typename TeamHandle::execution_space>{val});
1141template <
typename Rank,
typename TeamHandle,
typename Lambda>
1142KOKKOS_INLINE_FUNCTION
void parallel_for(
1143 TeamThreadMDRange<Rank, TeamHandle>
const& policy, Lambda
const& lambda) {
1144 Impl::md_parallel_impl<Rank>(policy, lambda, Impl::NoReductionTag());
1147template <
typename Rank,
typename TeamHandle,
typename Lambda,
1148 typename ReducerValueType>
1149KOKKOS_INLINE_FUNCTION
void parallel_reduce(
1150 ThreadVectorMDRange<Rank, TeamHandle>
const& policy, Lambda
const& lambda,
1151 ReducerValueType& val) {
1153 !std::is_array_v<ReducerValueType> &&
1154 !std::is_pointer_v<ReducerValueType> &&
1155 !Kokkos::is_reducer_v<ReducerValueType>,
1156 "Only a scalar return types are allowed!");
1158 val = ReducerValueType{};
1159 Impl::md_parallel_impl<Rank>(policy, lambda, val);
1161#ifdef KOKKOS_ENABLE_CUDA
1162 || std::is_same_v<
typename TeamHandle::execution_space,
1164#elif defined(KOKKOS_ENABLE_HIP)
1165 || std::is_same_v<
typename TeamHandle::execution_space,
1167#elif defined(KOKKOS_ENABLE_SYCL)
1168 || std::is_same_v<
typename TeamHandle::execution_space,
1172 policy.team.vector_reduce(
1173 Kokkos::Sum<ReducerValueType, typename TeamHandle::execution_space>{
1177template <
typename Rank,
typename TeamHandle,
typename Lambda>
1178KOKKOS_INLINE_FUNCTION
void parallel_for(
1179 ThreadVectorMDRange<Rank, TeamHandle>
const& policy, Lambda
const& lambda) {
1180 Impl::md_parallel_impl<Rank>(policy, lambda, Impl::NoReductionTag());
1183template <
typename Rank,
typename TeamHandle,
typename Lambda,
1184 typename ReducerValueType>
1185KOKKOS_INLINE_FUNCTION
void parallel_reduce(
1186 TeamVectorMDRange<Rank, TeamHandle>
const& policy, Lambda
const& lambda,
1187 ReducerValueType& val) {
1189 !std::is_array_v<ReducerValueType> &&
1190 !std::is_pointer_v<ReducerValueType> &&
1191 !Kokkos::is_reducer_v<ReducerValueType>,
1192 "Only a scalar return types are allowed!");
1194 val = ReducerValueType{};
1195 Impl::md_parallel_impl<Rank>(policy, lambda, val);
1197#ifdef KOKKOS_ENABLE_CUDA
1198 || std::is_same_v<
typename TeamHandle::execution_space,
1200#elif defined(KOKKOS_ENABLE_HIP)
1201 || std::is_same_v<
typename TeamHandle::execution_space,
1203#elif defined(KOKKOS_ENABLE_SYCL)
1204 || std::is_same_v<
typename TeamHandle::execution_space,
1208 policy.team.vector_reduce(
1209 Kokkos::Sum<ReducerValueType, typename TeamHandle::execution_space>{
1211 policy.team.team_reduce(
1212 Kokkos::Sum<ReducerValueType, typename TeamHandle::execution_space>{val});
1215template <
typename Rank,
typename TeamHandle,
typename Lambda>
1216KOKKOS_INLINE_FUNCTION
void parallel_for(
1217 TeamVectorMDRange<Rank, TeamHandle>
const& policy, Lambda
const& lambda) {
1218 Impl::md_parallel_impl<Rank>(policy, lambda, Impl::NoReductionTag());
1223template <
typename FunctorType,
typename TagType,
1224 bool HasTag = !std::is_void_v<TagType>>
1225struct ParallelConstructName;
1227template <
typename FunctorType,
typename TagType>
1228struct ParallelConstructName<FunctorType, TagType, true> {
1229 ParallelConstructName(std::string
const& label) : label_ref(label) {
1230 if (label.empty()) {
1231#ifdef KOKKOS_ENABLE_IMPL_TYPEINFO
1233 std::string(TypeInfo<std::remove_const_t<FunctorType>>::name()) +
1234 "/" + std::string(TypeInfo<TagType>::name());
1236 default_name = std::string(typeid(FunctorType).name()) +
"/" +
1237 typeid(TagType).name();
1241 std::string
const& get() {
1242 return (label_ref.empty()) ? default_name : label_ref;
1244 std::string
const& label_ref;
1245 std::string default_name;
1248template <
typename FunctorType,
typename TagType>
1249struct ParallelConstructName<FunctorType, TagType, false> {
1250 ParallelConstructName(std::string
const& label) : label_ref(label) {
1251 if (label.empty()) {
1252#ifdef KOKKOS_ENABLE_IMPL_TYPEINFO
1253 default_name = TypeInfo<std::remove_const_t<FunctorType>>::name();
1255 default_name = typeid(FunctorType).name();
1259 std::string
const& get() {
1260 return (label_ref.empty()) ? default_name : label_ref;
1262 std::string
const& label_ref;
1263 std::string default_name;
1274template <
class PatternTag,
class... Args>
1275struct PatternImplSpecializationFromTag;
1277template <
class... Args>
1278struct PatternImplSpecializationFromTag<Kokkos::ParallelForTag, Args...>
1279 : type_identity<ParallelFor<Args...>> {};
1281template <
class... Args>
1282struct PatternImplSpecializationFromTag<Kokkos::ParallelReduceTag, Args...>
1283 : type_identity<ParallelReduce<Args...>> {};
1285template <
class... Args>
1286struct PatternImplSpecializationFromTag<Kokkos::ParallelScanTag, Args...>
1287 : type_identity<ParallelScan<Args...>> {};
1289template <
class PatternImpl>
1290struct PatternTagFromImplSpecialization;
1292template <
class... Args>
1293struct PatternTagFromImplSpecialization<
ParallelFor<Args...>>
1294 : type_identity<ParallelForTag> {};
1296template <
class... Args>
1298 : type_identity<ParallelReduceTag> {};
1300template <
class... Args>
1301struct PatternTagFromImplSpecialization<
ParallelScan<Args...>>
1302 : type_identity<ParallelScanTag> {};
Implementation of the ParallelFor operator that has a partial specialization for the device.
Implementation detail of parallel_reduce.
Implementation detail of parallel_scan.
Execution policy for work over a range of an integral type.
RangePolicy(const typename traits::execution_space &work_space, const IndexType1 work_begin, const IndexType2 work_end)
Total range.
RangePolicy(const IndexType1 work_begin, const IndexType2 work_end)
Total range.
RangePolicy(const IndexType1 work_begin, const IndexType2 work_end, const ChunkSize chunk_size)
Total range.
RangePolicy & set_chunk_size(int chunk_size)
set chunk_size to a discrete value
member_type chunk_size() const
return chunk_size
RangePolicy< Properties... > execution_policy
Execution policy for parallel work over a league of teams of threads.
TeamPolicy(int league_size_request, int team_size_request, int vector_length_request=1)
Construct policy with the default instance of the execution space.
TeamPolicy(const typename traits::execution_space &space_, int league_size_request, int team_size_request, int vector_length_request=1)
Construct policy with the given instance of the execution space.
ScopeGuard Some user scope issues have been identified with some Kokkos::finalize calls; ScopeGuard a...
Parallel execution of a functor calls the functor once with each member of the execution policy.
KOKKOS_INLINE_FUNCTION JoinOp::value_type team_reduce(const typename JoinOp::value_type, const JoinOp &) const
Intra-team reduction. Returns join of all values of the team members.
KOKKOS_INLINE_FUNCTION Type team_scan(const Type &value, Type *const global_accum) const
Intra-team exclusive prefix sum with team_rank() ordering with intra-team non-deterministic ordering ...
KOKKOS_INLINE_FUNCTION int team_size() const
Number of threads in this team.
KOKKOS_INLINE_FUNCTION traits::execution_space::scratch_memory_space team_shmem() const
Handle to the currently executing team shared scratch memory.
KOKKOS_INLINE_FUNCTION int team_rank() const
Rank of this thread within this team.
KOKKOS_INLINE_FUNCTION int league_size() const
Number of teams in the league.
KOKKOS_INLINE_FUNCTION int league_rank() const
Rank of this team within the league of teams.
KOKKOS_INLINE_FUNCTION void team_barrier() const
Barrier among the threads of this team.
KOKKOS_INLINE_FUNCTION Type team_scan(const Type &value) const
Intra-team exclusive prefix sum with team_rank() ordering.
Subrange for a partition's rank and size.
KOKKOS_INLINE_FUNCTION WorkRange(const RangePolicy &range, const int part_rank, const int part_size)
Subrange for a partition's rank and size.