47 #ifndef KOKKOS_SERIAL_HPP 48 #define KOKKOS_SERIAL_HPP 50 #include <Kokkos_Macros.hpp> 51 #if defined( KOKKOS_ENABLE_SERIAL ) 56 #include <Kokkos_TaskScheduler.hpp> 58 #include <Kokkos_HostSpace.hpp> 59 #include <Kokkos_ScratchSpace.hpp> 60 #include <Kokkos_MemoryTraits.hpp> 61 #include <impl/Kokkos_Tags.hpp> 62 #include <impl/Kokkos_HostThreadTeam.hpp> 63 #include <impl/Kokkos_FunctorAnalysis.hpp> 64 #include <impl/Kokkos_FunctorAdapter.hpp> 65 #include <impl/Kokkos_Profiling_Interface.hpp> 67 #include <KokkosExp_MDRangePolicy.hpp> 69 #include <Kokkos_UniqueToken.hpp> 91 typedef Serial execution_space ;
93 typedef HostSpace::size_type size_type ;
95 typedef HostSpace memory_space ;
97 typedef Kokkos::Device<execution_space,memory_space> device_type;
100 typedef LayoutRight array_layout ;
103 typedef ScratchMemorySpace< Kokkos::Serial > scratch_memory_space ;
113 inline static int in_parallel() {
return false ; }
121 static void fence() {}
124 static int concurrency() {
return 1;};
129 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE 133 static void initialize(
unsigned threads_count = 1 ,
134 unsigned use_numa_count = 0 ,
135 unsigned use_cores_per_numa = 0 ,
136 bool allow_asynchronous_threadpool =
false);
138 static bool is_initialized();
145 inline static int thread_pool_size(
int = 0 ) {
return 1 ; }
146 KOKKOS_INLINE_FUNCTION
static int thread_pool_rank() {
return 0 ; }
150 KOKKOS_INLINE_FUNCTION
static unsigned hardware_thread_id() {
return thread_pool_rank(); }
151 inline static unsigned max_hardware_threads() {
return thread_pool_size(0); }
153 static void impl_initialize();
155 static bool impl_is_initialized();
158 static void impl_finalize();
162 inline static int impl_thread_pool_size(
int = 0 ) {
return 1 ; }
163 KOKKOS_INLINE_FUNCTION
static int impl_thread_pool_rank() {
return 0 ; }
167 KOKKOS_INLINE_FUNCTION
static unsigned impl_hardware_thread_id() {
return impl_thread_pool_rank(); }
168 inline static unsigned impl_max_hardware_threads() {
return impl_thread_pool_size(0); }
171 static const char* name();
184 struct MemorySpaceAccess
185 <
Kokkos::Serial::memory_space
186 , Kokkos::Serial::scratch_memory_space
189 enum { assignable =
false };
190 enum { accessible =
true };
191 enum { deepcopy =
false };
195 struct VerifyExecutionCanAccessMemorySpace
196 <
Kokkos::Serial::memory_space
197 , Kokkos::Serial::scratch_memory_space
200 enum { value =
true };
201 inline static void verify(
void ) { }
202 inline static void verify(
const void * ) { }
215 void serial_resize_thread_team_data(
size_t pool_reduce_bytes
216 ,
size_t team_reduce_bytes
217 ,
size_t team_shared_bytes
218 ,
size_t thread_local_bytes );
220 HostThreadTeamData * serial_get_thread_team_data();
234 template<
class ... Properties >
235 class TeamPolicyInternal<
Kokkos::Serial , Properties ... >:
public PolicyTraits<Properties...>
239 size_t m_team_scratch_size[2] ;
240 size_t m_thread_scratch_size[2] ;
247 typedef TeamPolicyInternal execution_policy ;
249 typedef PolicyTraits<Properties ... > traits;
252 typedef Kokkos::Serial execution_space ;
254 TeamPolicyInternal& operator = (
const TeamPolicyInternal& p) {
255 m_league_size = p.m_league_size;
256 m_team_scratch_size[0] = p.m_team_scratch_size[0];
257 m_thread_scratch_size[0] = p.m_thread_scratch_size[0];
258 m_team_scratch_size[1] = p.m_team_scratch_size[1];
259 m_thread_scratch_size[1] = p.m_thread_scratch_size[1];
260 m_chunk_size = p.m_chunk_size;
266 template<
class FunctorType >
268 int team_size_max(
const FunctorType & ) {
return 1 ; }
270 template<
class FunctorType >
272 int team_size_recommended(
const FunctorType & ) {
return 1 ; }
274 template<
class FunctorType >
276 int team_size_recommended(
const FunctorType & ,
const int& ) {
return 1 ; }
280 inline int team_size()
const {
return 1 ; }
281 inline int league_size()
const {
return m_league_size ; }
282 inline size_t scratch_size(
const int& level,
int = 0)
const {
return m_team_scratch_size[level] + m_thread_scratch_size[level]; }
285 TeamPolicyInternal( execution_space &
286 ,
int league_size_request
287 #ifndef KOKKOS_ENABLE_DEPRECATED_CODE
288 ,
int team_size_request
293 : m_team_scratch_size { 0 , 0 }
294 , m_thread_scratch_size { 0 , 0 }
295 , m_league_size( league_size_request )
296 , m_chunk_size ( 32 )
298 #ifndef KOKKOS_ENABLE_DEPRECATED_CODE 299 if(team_size_request > 1) Kokkos::abort(
"Kokkos::abort: Requested Team Size is too large!");
303 TeamPolicyInternal( execution_space &
304 ,
int league_size_request
305 ,
const Kokkos::AUTO_t &
307 : m_team_scratch_size { 0 , 0 }
308 , m_thread_scratch_size { 0 , 0 }
309 , m_league_size( league_size_request )
310 , m_chunk_size ( 32 )
313 TeamPolicyInternal(
int league_size_request
314 #ifndef KOKKOS_ENABLE_DEPRECATED_CODE
315 ,
int team_size_request
320 : m_team_scratch_size { 0 , 0 }
321 , m_thread_scratch_size { 0 , 0 }
322 , m_league_size( league_size_request )
323 , m_chunk_size ( 32 )
325 #ifndef KOKKOS_ENABLE_DEPRECATED_CODE 326 if(team_size_request > 1) Kokkos::abort(
"Kokkos::abort: Requested Team Size is too large!");
330 TeamPolicyInternal(
int league_size_request
331 ,
const Kokkos::AUTO_t &
333 : m_team_scratch_size { 0 , 0 }
334 , m_thread_scratch_size { 0 , 0 }
335 , m_league_size( league_size_request )
336 , m_chunk_size ( 32 )
339 inline int chunk_size()
const {
return m_chunk_size ; }
341 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE 343 inline TeamPolicyInternal set_chunk_size(
typename traits::index_type chunk_size_)
const {
344 TeamPolicyInternal p = *
this;
345 p.m_chunk_size = chunk_size_;
350 inline TeamPolicyInternal set_scratch_size(
const int& level,
const PerTeamValue& per_team)
const {
351 TeamPolicyInternal p = *
this;
352 p.m_team_scratch_size[level] = per_team.value;
357 inline TeamPolicyInternal set_scratch_size(
const int& level,
const PerThreadValue& per_thread)
const {
358 TeamPolicyInternal p = *
this;
359 p.m_thread_scratch_size[level] = per_thread.value;
364 inline TeamPolicyInternal set_scratch_size(
const int& level,
const PerTeamValue& per_team,
const PerThreadValue& per_thread)
const {
365 TeamPolicyInternal p = *
this;
366 p.m_team_scratch_size[level] = per_team.value;
367 p.m_thread_scratch_size[level] = per_thread.value;
372 inline TeamPolicyInternal& set_chunk_size(
typename traits::index_type chunk_size_) {
373 m_chunk_size = chunk_size_;
378 inline TeamPolicyInternal& set_scratch_size(
const int& level,
const PerTeamValue& per_team) {
379 m_team_scratch_size[level] = per_team.value;
384 inline TeamPolicyInternal& set_scratch_size(
const int& level,
const PerThreadValue& per_thread) {
385 m_thread_scratch_size[level] = per_thread.value;
390 inline TeamPolicyInternal& set_scratch_size(
const int& level,
const PerTeamValue& per_team,
const PerThreadValue& per_thread) {
391 m_team_scratch_size[level] = per_team.value;
392 m_thread_scratch_size[level] = per_thread.value;
397 typedef Impl::HostThreadTeamMember< Kokkos::Serial > member_type ;
400 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE 402 inline TeamPolicyInternal internal_set_chunk_size(
typename traits::index_type chunk_size_) {
403 m_chunk_size = chunk_size_;
408 inline TeamPolicyInternal internal_set_scratch_size(
const int& level,
const PerTeamValue& per_team) {
409 m_team_scratch_size[level] = per_team.value;
414 inline TeamPolicyInternal internal_set_scratch_size(
const int& level,
const PerThreadValue& per_thread) {
415 m_thread_scratch_size[level] = per_thread.value;
420 inline TeamPolicyInternal internal_set_scratch_size(
const int& level,
const PerTeamValue& per_team,
const PerThreadValue& per_thread) {
421 m_team_scratch_size[level] = per_team.value;
422 m_thread_scratch_size[level] = per_thread.value;
437 template<
class FunctorType ,
class ... Traits >
438 class ParallelFor< FunctorType ,
439 Kokkos::RangePolicy< Traits ... > ,
447 const FunctorType m_functor ;
448 const Policy m_policy ;
450 template<
class TagType >
451 typename std::enable_if< std::is_same< TagType , void >::value >::type
454 const typename Policy::member_type e = m_policy.end();
455 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
460 template<
class TagType >
461 typename std::enable_if< ! std::is_same< TagType , void >::value >::type
465 const typename Policy::member_type e = m_policy.end();
466 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
475 { this->
template exec< typename Policy::work_tag >(); }
478 ParallelFor(
const FunctorType & arg_functor
479 ,
const Policy & arg_policy )
480 : m_functor( arg_functor )
481 , m_policy( arg_policy )
487 template<
class FunctorType ,
class ReducerType ,
class ... Traits >
488 class ParallelReduce< FunctorType
489 ,
Kokkos::RangePolicy< Traits ... >
497 typedef typename Policy::work_tag WorkTag ;
499 typedef Kokkos::Impl::if_c< std::is_same<InvalidType,ReducerType>::value, FunctorType, ReducerType> ReducerConditional;
501 typedef typename ReducerConditional::type ReducerTypeFwd;
502 typedef typename Kokkos::Impl::if_c< std::is_same<InvalidType,ReducerType>::value, WorkTag,
void>::type WorkTagFwd;
504 typedef FunctorAnalysis< FunctorPatternInterface::REDUCE , Policy , FunctorType > Analysis ;
506 typedef Kokkos::Impl::FunctorValueInit< ReducerTypeFwd , WorkTagFwd > ValueInit ;
508 typedef typename Analysis::pointer_type pointer_type ;
509 typedef typename Analysis::reference_type reference_type ;
511 const FunctorType m_functor ;
512 const Policy m_policy ;
513 const ReducerType m_reducer ;
514 const pointer_type m_result_ptr ;
516 template<
class TagType >
518 typename std::enable_if< std::is_same< TagType , void >::value >::type
519 exec( reference_type update )
const 521 const typename Policy::member_type e = m_policy.end();
522 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
523 m_functor( i , update );
527 template<
class TagType >
529 typename std::enable_if< ! std::is_same< TagType , void >::value >::type
530 exec( reference_type update )
const 534 const typename Policy::member_type e = m_policy.end();
535 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
536 m_functor( t , i , update );
545 const size_t pool_reduce_size =
546 Analysis::value_size( ReducerConditional::select(m_functor , m_reducer) );
547 const size_t team_reduce_size = 0 ;
548 const size_t team_shared_size = 0 ;
549 const size_t thread_local_size = 0 ;
551 serial_resize_thread_team_data( pool_reduce_size
554 , thread_local_size );
556 HostThreadTeamData & data = *serial_get_thread_team_data();
559 m_result_ptr ? m_result_ptr : pointer_type(data.pool_reduce_local());
561 reference_type update =
562 ValueInit::init( ReducerConditional::select(m_functor , m_reducer) , ptr );
564 this->
template exec< WorkTag >( update );
566 Kokkos::Impl::FunctorFinal< ReducerTypeFwd , WorkTagFwd >::
567 final( ReducerConditional::select(m_functor , m_reducer) , ptr );
570 template<
class HostViewType >
571 ParallelReduce(
const FunctorType & arg_functor ,
572 const Policy & arg_policy ,
573 const HostViewType & arg_result_view ,
574 typename std::enable_if<
575 Kokkos::is_view< HostViewType >::value &&
576 !Kokkos::is_reducer_type<ReducerType>::value
577 ,
void*>::type = NULL)
578 : m_functor( arg_functor )
579 , m_policy( arg_policy )
580 , m_reducer( InvalidType() )
581 , m_result_ptr( arg_result_view.data() )
583 static_assert( Kokkos::is_view< HostViewType >::value
584 ,
"Kokkos::Serial reduce result must be a View" );
586 static_assert( std::is_same< typename HostViewType::memory_space , HostSpace >::value
587 ,
"Kokkos::Serial reduce result must be a View in HostSpace" );
591 ParallelReduce(
const FunctorType & arg_functor
593 ,
const ReducerType& reducer )
594 : m_functor( arg_functor )
595 , m_policy( arg_policy )
596 , m_reducer( reducer )
597 , m_result_ptr( reducer.view().data() )
608 template<
class FunctorType ,
class ... Traits >
609 class ParallelScan< FunctorType
610 ,
Kokkos::RangePolicy< Traits ... >
617 typedef typename Policy::work_tag WorkTag ;
619 typedef FunctorAnalysis< FunctorPatternInterface::SCAN , Policy , FunctorType > Analysis ;
621 typedef Kokkos::Impl::FunctorValueInit< FunctorType , WorkTag > ValueInit ;
623 typedef typename Analysis::pointer_type pointer_type ;
624 typedef typename Analysis::reference_type reference_type ;
626 const FunctorType m_functor ;
627 const Policy m_policy ;
629 template<
class TagType >
631 typename std::enable_if< std::is_same< TagType , void >::value >::type
632 exec( reference_type update )
const 634 const typename Policy::member_type e = m_policy.end();
635 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
636 m_functor( i , update ,
true );
640 template<
class TagType >
642 typename std::enable_if< ! std::is_same< TagType , void >::value >::type
643 exec( reference_type update )
const 646 const typename Policy::member_type e = m_policy.end();
647 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
648 m_functor( t , i , update ,
true );
657 const size_t pool_reduce_size = Analysis::value_size( m_functor );
658 const size_t team_reduce_size = 0 ;
659 const size_t team_shared_size = 0 ;
660 const size_t thread_local_size = 0 ;
662 serial_resize_thread_team_data( pool_reduce_size
665 , thread_local_size );
667 HostThreadTeamData & data = *serial_get_thread_team_data();
669 reference_type update =
670 ValueInit::init( m_functor , pointer_type(data.pool_reduce_local()) );
672 this->
template exec< WorkTag >( update );
676 ParallelScan(
const FunctorType & arg_functor
677 ,
const Policy & arg_policy
679 : m_functor( arg_functor )
680 , m_policy( arg_policy )
685 template<
class FunctorType ,
class ReturnType,
class ... Traits >
686 class ParallelScanWithTotal< FunctorType
687 ,
Kokkos::RangePolicy< Traits ... >
695 typedef typename Policy::work_tag WorkTag ;
697 typedef FunctorAnalysis< FunctorPatternInterface::SCAN , Policy , FunctorType > Analysis ;
699 typedef Kokkos::Impl::FunctorValueInit< FunctorType , WorkTag > ValueInit ;
701 typedef typename Analysis::pointer_type pointer_type ;
702 typedef typename Analysis::reference_type reference_type ;
704 const FunctorType m_functor ;
705 const Policy m_policy ;
706 ReturnType & m_returnvalue;
708 template<
class TagType >
710 typename std::enable_if< std::is_same< TagType , void >::value >::type
711 exec( reference_type update )
const 713 const typename Policy::member_type e = m_policy.end();
714 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
715 m_functor( i , update ,
true );
719 template<
class TagType >
721 typename std::enable_if< ! std::is_same< TagType , void >::value >::type
722 exec( reference_type update )
const 725 const typename Policy::member_type e = m_policy.end();
726 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
727 m_functor( t , i , update ,
true );
736 const size_t pool_reduce_size = Analysis::value_size( m_functor );
737 const size_t team_reduce_size = 0 ;
738 const size_t team_shared_size = 0 ;
739 const size_t thread_local_size = 0 ;
741 serial_resize_thread_team_data( pool_reduce_size
744 , thread_local_size );
746 HostThreadTeamData & data = *serial_get_thread_team_data();
748 reference_type update =
749 ValueInit::init( m_functor , pointer_type(data.pool_reduce_local()) );
751 this->
template exec< WorkTag >( update );
753 m_returnvalue = update;
757 ParallelScanWithTotal(
const FunctorType & arg_functor
758 ,
const Policy & arg_policy
759 , ReturnType & arg_returnvalue
761 : m_functor( arg_functor )
762 , m_policy( arg_policy )
763 , m_returnvalue( arg_returnvalue )
778 template<
class FunctorType ,
class ... Traits >
779 class ParallelFor< FunctorType ,
780 Kokkos::MDRangePolicy< Traits ... > ,
786 typedef Kokkos::MDRangePolicy< Traits ... > MDRangePolicy ;
787 typedef typename MDRangePolicy::impl_range_policy Policy ;
789 typedef typename Kokkos::Impl::HostIterateTile< MDRangePolicy, FunctorType, typename MDRangePolicy::work_tag, void > iterate_type;
791 const FunctorType m_functor ;
792 const MDRangePolicy m_mdr_policy ;
793 const Policy m_policy ;
798 const typename Policy::member_type e = m_policy.end();
799 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
800 iterate_type( m_mdr_policy, m_functor )( i );
811 ParallelFor(
const FunctorType & arg_functor
812 ,
const MDRangePolicy & arg_policy )
813 : m_functor( arg_functor )
814 , m_mdr_policy( arg_policy )
815 , m_policy( Policy(0, m_mdr_policy.m_num_tiles).set_chunk_size(1) )
820 template<
class FunctorType ,
class ReducerType ,
class ... Traits >
821 class ParallelReduce< FunctorType
822 ,
Kokkos::MDRangePolicy< Traits ... >
829 typedef Kokkos::MDRangePolicy< Traits ... > MDRangePolicy ;
830 typedef typename MDRangePolicy::impl_range_policy Policy ;
832 typedef typename MDRangePolicy::work_tag WorkTag ;
834 typedef Kokkos::Impl::if_c< std::is_same<InvalidType,ReducerType>::value, FunctorType, ReducerType> ReducerConditional;
835 typedef typename ReducerConditional::type ReducerTypeFwd;
836 typedef typename Kokkos::Impl::if_c< std::is_same<InvalidType,ReducerType>::value, WorkTag,
void>::type WorkTagFwd;
838 typedef FunctorAnalysis< FunctorPatternInterface::REDUCE , MDRangePolicy , FunctorType > Analysis ;
840 typedef Kokkos::Impl::FunctorValueInit< ReducerTypeFwd , WorkTagFwd > ValueInit ;
842 typedef typename Analysis::pointer_type pointer_type ;
843 typedef typename Analysis::value_type value_type ;
844 typedef typename Analysis::reference_type reference_type ;
847 using iterate_type =
typename Kokkos::Impl::HostIterateTile< MDRangePolicy
854 const FunctorType m_functor ;
855 const MDRangePolicy m_mdr_policy ;
856 const Policy m_policy ;
857 const ReducerType m_reducer ;
858 const pointer_type m_result_ptr ;
862 exec( reference_type update )
const 864 const typename Policy::member_type e = m_policy.end();
865 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
866 iterate_type( m_mdr_policy, m_functor, update )( i );
875 const size_t pool_reduce_size =
876 Analysis::value_size( ReducerConditional::select(m_functor , m_reducer) );
877 const size_t team_reduce_size = 0 ;
878 const size_t team_shared_size = 0 ;
879 const size_t thread_local_size = 0 ;
881 serial_resize_thread_team_data( pool_reduce_size
884 , thread_local_size );
886 HostThreadTeamData & data = *serial_get_thread_team_data();
889 m_result_ptr ? m_result_ptr : pointer_type(data.pool_reduce_local());
891 reference_type update =
892 ValueInit::init( ReducerConditional::select(m_functor , m_reducer) , ptr );
894 this-> exec( update );
896 Kokkos::Impl::FunctorFinal< ReducerTypeFwd , WorkTagFwd >::
897 final( ReducerConditional::select(m_functor , m_reducer) , ptr );
900 template<
class HostViewType >
901 ParallelReduce(
const FunctorType & arg_functor ,
902 const MDRangePolicy & arg_policy ,
903 const HostViewType & arg_result_view ,
904 typename std::enable_if<
905 Kokkos::is_view< HostViewType >::value &&
906 !Kokkos::is_reducer_type<ReducerType>::value
907 ,
void*>::type = NULL)
908 : m_functor( arg_functor )
909 , m_mdr_policy( arg_policy )
910 , m_policy( Policy(0, m_mdr_policy.m_num_tiles).set_chunk_size(1) )
911 , m_reducer( InvalidType() )
912 , m_result_ptr( arg_result_view.data() )
914 static_assert( Kokkos::is_view< HostViewType >::value
915 ,
"Kokkos::Serial reduce result must be a View" );
917 static_assert( std::is_same< typename HostViewType::memory_space , HostSpace >::value
918 ,
"Kokkos::Serial reduce result must be a View in HostSpace" );
922 ParallelReduce(
const FunctorType & arg_functor
923 , MDRangePolicy arg_policy
924 ,
const ReducerType& reducer )
925 : m_functor( arg_functor )
926 , m_mdr_policy( arg_policy )
927 , m_policy( Policy(0, m_mdr_policy.m_num_tiles).set_chunk_size(1) )
928 , m_reducer( reducer )
929 , m_result_ptr( reducer.view().data() )
949 template<
class FunctorType ,
class ... Properties >
950 class ParallelFor< FunctorType
951 ,
Kokkos::TeamPolicy< Properties ... >
957 enum { TEAM_REDUCE_SIZE = 512 };
959 typedef TeamPolicyInternal< Kokkos::Serial , Properties ...> Policy ;
960 typedef typename Policy::member_type Member ;
962 const FunctorType m_functor ;
966 template<
class TagType >
968 typename std::enable_if< std::is_same< TagType , void >::value >::type
969 exec( HostThreadTeamData & data )
const 971 for (
int ileague = 0 ; ileague < m_league ; ++ileague ) {
972 m_functor( Member(data,ileague,m_league) );
976 template<
class TagType >
978 typename std::enable_if< ! std::is_same< TagType , void >::value >::type
979 exec( HostThreadTeamData & data )
const 982 for (
int ileague = 0 ; ileague < m_league ; ++ileague ) {
983 m_functor( t , Member(data,ileague,m_league) );
992 const size_t pool_reduce_size = 0 ;
993 const size_t team_reduce_size = TEAM_REDUCE_SIZE ;
994 const size_t team_shared_size = m_shared ;
995 const size_t thread_local_size = 0 ;
997 serial_resize_thread_team_data( pool_reduce_size
1000 , thread_local_size );
1002 HostThreadTeamData & data = *serial_get_thread_team_data();
1004 this->
template exec< typename Policy::work_tag >( data );
1007 ParallelFor(
const FunctorType & arg_functor
1008 ,
const Policy & arg_policy )
1009 : m_functor( arg_functor )
1010 , m_league( arg_policy.league_size() )
1011 , m_shared( arg_policy.scratch_size(0) +
1012 arg_policy.scratch_size(1) +
1013 FunctorTeamShmemSize< FunctorType >::value( arg_functor , 1 ) )
1019 template<
class FunctorType ,
class ReducerType ,
class ... Properties >
1020 class ParallelReduce< FunctorType
1021 ,
Kokkos::TeamPolicy< Properties ... >
1028 enum { TEAM_REDUCE_SIZE = 512 };
1030 typedef TeamPolicyInternal< Kokkos::Serial, Properties ... > Policy ;
1032 typedef FunctorAnalysis< FunctorPatternInterface::REDUCE , Policy , FunctorType > Analysis ;
1034 typedef typename Policy::member_type Member ;
1035 typedef typename Policy::work_tag WorkTag ;
1037 typedef Kokkos::Impl::if_c< std::is_same<InvalidType,ReducerType>::value, FunctorType, ReducerType> ReducerConditional;
1038 typedef typename ReducerConditional::type ReducerTypeFwd;
1039 typedef typename Kokkos::Impl::if_c< std::is_same<InvalidType,ReducerType>::value, WorkTag,
void>::type WorkTagFwd;
1041 typedef Kokkos::Impl::FunctorValueInit< ReducerTypeFwd , WorkTagFwd > ValueInit ;
1043 typedef typename Analysis::pointer_type pointer_type ;
1044 typedef typename Analysis::reference_type reference_type ;
1046 const FunctorType m_functor ;
1047 const int m_league ;
1048 const ReducerType m_reducer ;
1049 pointer_type m_result_ptr ;
1050 const int m_shared ;
1052 template<
class TagType >
1054 typename std::enable_if< std::is_same< TagType , void >::value >::type
1055 exec( HostThreadTeamData & data , reference_type update )
const 1057 for (
int ileague = 0 ; ileague < m_league ; ++ileague ) {
1058 m_functor( Member(data,ileague,m_league) , update );
1062 template<
class TagType >
1064 typename std::enable_if< ! std::is_same< TagType , void >::value >::type
1065 exec( HostThreadTeamData & data , reference_type update )
const 1069 for (
int ileague = 0 ; ileague < m_league ; ++ileague ) {
1070 m_functor( t , Member(data,ileague,m_league) , update );
1077 void execute()
const 1079 const size_t pool_reduce_size =
1080 Analysis::value_size( ReducerConditional::select(m_functor, m_reducer));
1082 const size_t team_reduce_size = TEAM_REDUCE_SIZE ;
1083 const size_t team_shared_size = m_shared ;
1084 const size_t thread_local_size = 0 ;
1086 serial_resize_thread_team_data( pool_reduce_size
1089 , thread_local_size );
1092 HostThreadTeamData & data = *serial_get_thread_team_data();
1095 m_result_ptr ? m_result_ptr : pointer_type(data.pool_reduce_local());
1097 reference_type update =
1098 ValueInit::init( ReducerConditional::select(m_functor , m_reducer) , ptr );
1100 this->
template exec< WorkTag >( data , update );
1102 Kokkos::Impl::FunctorFinal< ReducerTypeFwd , WorkTagFwd >::
1103 final( ReducerConditional::select(m_functor , m_reducer) , ptr );
1106 template<
class ViewType >
1107 ParallelReduce(
const FunctorType & arg_functor
1108 ,
const Policy & arg_policy
1109 ,
const ViewType & arg_result ,
1110 typename std::enable_if<
1111 Kokkos::is_view< ViewType >::value &&
1112 !Kokkos::is_reducer_type<ReducerType>::value
1113 ,
void*>::type = NULL)
1114 : m_functor( arg_functor )
1115 , m_league( arg_policy.league_size() )
1116 , m_reducer( InvalidType() )
1117 , m_result_ptr( arg_result.data() )
1118 , m_shared( arg_policy.scratch_size(0) +
1119 arg_policy.scratch_size(1) +
1120 FunctorTeamShmemSize< FunctorType >::value( m_functor , 1 ) )
1122 static_assert( Kokkos::is_view< ViewType >::value
1123 ,
"Reduction result on Kokkos::Serial must be a Kokkos::View" );
1125 static_assert( std::is_same<
typename ViewType::memory_space
1127 ,
"Reduction result on Kokkos::Serial must be a Kokkos::View in HostSpace" );
1131 ParallelReduce(
const FunctorType & arg_functor
1133 ,
const ReducerType& reducer )
1134 : m_functor( arg_functor )
1135 , m_league( arg_policy.league_size() )
1136 , m_reducer( reducer )
1137 , m_result_ptr( reducer.view().data() )
1138 , m_shared( arg_policy.scratch_size(0) +
1139 arg_policy.scratch_size(1) +
1140 FunctorTeamShmemSize< FunctorType >::value( arg_functor , 1 ) )
1155 namespace Kokkos {
namespace Experimental {
1158 class UniqueToken< Serial, UniqueTokenScope::Instance>
1161 using execution_space = Serial;
1162 using size_type = int;
1167 UniqueToken( execution_space
const& = execution_space() ) noexcept {}
1170 KOKKOS_INLINE_FUNCTION
1171 int size() const noexcept {
return 1; }
1174 KOKKOS_INLINE_FUNCTION
1175 int acquire() const noexcept {
return 0; }
1178 KOKKOS_INLINE_FUNCTION
1179 void release(
int )
const noexcept {}
1183 class UniqueToken< Serial, UniqueTokenScope::Global>
1186 using execution_space = Serial;
1187 using size_type = int;
1192 UniqueToken( execution_space
const& = execution_space() ) noexcept {}
1195 KOKKOS_INLINE_FUNCTION
1196 int size() const noexcept {
return 1; }
1199 KOKKOS_INLINE_FUNCTION
1200 int acquire() const noexcept {
return 0; }
1203 KOKKOS_INLINE_FUNCTION
1204 void release(
int )
const noexcept {}
1209 #include <impl/Kokkos_Serial_Task.hpp> 1211 #endif // defined( KOKKOS_ENABLE_SERIAL ) void print_configuration(std::ostream &, const bool detail=false)
Print "Bill of Materials".
Memory management for host memory.
Declaration of various MemoryLayout options.
Declaration of parallel operators.
void finalize()
Finalize the spaces that were initialized via Kokkos::initialize.
Execution policy for work over a range of an integral type.