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> 90 typedef Serial execution_space ;
92 typedef HostSpace::size_type size_type ;
94 typedef HostSpace memory_space ;
99 typedef LayoutRight array_layout ;
102 typedef ScratchMemorySpace< Kokkos::Serial > scratch_memory_space ;
112 inline static int in_parallel() {
return false ; }
140 static void fence() {}
142 static void initialize(
unsigned threads_count = 1 ,
143 unsigned use_numa_count = 0 ,
144 unsigned use_cores_per_numa = 0 ,
145 bool allow_asynchronous_threadpool =
false);
147 static int is_initialized();
150 static int concurrency() {
return 1;};
160 inline static int thread_pool_size(
int = 0 ) {
return 1 ; }
161 KOKKOS_INLINE_FUNCTION
static int thread_pool_rank() {
return 0 ; }
165 KOKKOS_INLINE_FUNCTION
static unsigned hardware_thread_id() {
return thread_pool_rank(); }
166 inline static unsigned max_hardware_threads() {
return thread_pool_size(0); }
168 static const char* name();
181 struct MemorySpaceAccess
182 <
Kokkos::Serial::memory_space
183 , Kokkos::Serial::scratch_memory_space
186 enum { assignable =
false };
187 enum { accessible =
true };
188 enum { deepcopy =
false };
192 struct VerifyExecutionCanAccessMemorySpace
193 <
Kokkos::Serial::memory_space
194 , Kokkos::Serial::scratch_memory_space
197 enum { value =
true };
198 inline static void verify(
void ) { }
199 inline static void verify(
const void * ) { }
212 void serial_resize_thread_team_data(
size_t pool_reduce_bytes
213 ,
size_t team_reduce_bytes
214 ,
size_t team_shared_bytes
215 ,
size_t thread_local_bytes );
217 HostThreadTeamData * serial_get_thread_team_data();
231 template<
class ... Properties >
232 class TeamPolicyInternal<
Kokkos::Serial , Properties ... >:
public PolicyTraits<Properties...>
236 size_t m_team_scratch_size[2] ;
237 size_t m_thread_scratch_size[2] ;
244 typedef TeamPolicyInternal execution_policy ;
246 typedef PolicyTraits<Properties ... > traits;
249 typedef Kokkos::Serial execution_space ;
251 TeamPolicyInternal& operator = (
const TeamPolicyInternal& p) {
252 m_league_size = p.m_league_size;
253 m_team_scratch_size[0] = p.m_team_scratch_size[0];
254 m_thread_scratch_size[0] = p.m_thread_scratch_size[0];
255 m_team_scratch_size[1] = p.m_team_scratch_size[1];
256 m_thread_scratch_size[1] = p.m_thread_scratch_size[1];
257 m_chunk_size = p.m_chunk_size;
263 template<
class FunctorType >
265 int team_size_max(
const FunctorType & ) {
return 1 ; }
267 template<
class FunctorType >
269 int team_size_recommended(
const FunctorType & ) {
return 1 ; }
271 template<
class FunctorType >
273 int team_size_recommended(
const FunctorType & ,
const int& ) {
return 1 ; }
277 inline int team_size()
const {
return 1 ; }
278 inline int league_size()
const {
return m_league_size ; }
279 inline size_t scratch_size(
const int& level,
int = 0)
const {
return m_team_scratch_size[level] + m_thread_scratch_size[level]; }
282 TeamPolicyInternal( execution_space &
283 ,
int league_size_request
286 : m_team_scratch_size { 0 , 0 }
287 , m_thread_scratch_size { 0 , 0 }
288 , m_league_size( league_size_request )
289 , m_chunk_size ( 32 )
292 TeamPolicyInternal( execution_space &
293 ,
int league_size_request
294 ,
const Kokkos::AUTO_t &
296 : m_team_scratch_size { 0 , 0 }
297 , m_thread_scratch_size { 0 , 0 }
298 , m_league_size( league_size_request )
299 , m_chunk_size ( 32 )
302 TeamPolicyInternal(
int league_size_request
305 : m_team_scratch_size { 0 , 0 }
306 , m_thread_scratch_size { 0 , 0 }
307 , m_league_size( league_size_request )
308 , m_chunk_size ( 32 )
311 TeamPolicyInternal(
int league_size_request
312 ,
const Kokkos::AUTO_t &
314 : m_team_scratch_size { 0 , 0 }
315 , m_thread_scratch_size { 0 , 0 }
316 , m_league_size( league_size_request )
317 , m_chunk_size ( 32 )
320 inline int chunk_size()
const {
return m_chunk_size ; }
323 inline TeamPolicyInternal set_chunk_size(
typename traits::index_type chunk_size_)
const {
324 TeamPolicyInternal p = *
this;
325 p.m_chunk_size = chunk_size_;
330 inline TeamPolicyInternal set_scratch_size(
const int& level,
const PerTeamValue& per_team)
const {
331 TeamPolicyInternal p = *
this;
332 p.m_team_scratch_size[level] = per_team.value;
337 inline TeamPolicyInternal set_scratch_size(
const int& level,
const PerThreadValue& per_thread)
const {
338 TeamPolicyInternal p = *
this;
339 p.m_thread_scratch_size[level] = per_thread.value;
344 inline TeamPolicyInternal set_scratch_size(
const int& level,
const PerTeamValue& per_team,
const PerThreadValue& per_thread)
const {
345 TeamPolicyInternal p = *
this;
346 p.m_team_scratch_size[level] = per_team.value;
347 p.m_thread_scratch_size[level] = per_thread.value;
351 typedef Impl::HostThreadTeamMember< Kokkos::Serial > member_type ;
363 template<
class FunctorType ,
class ... Traits >
364 class ParallelFor< FunctorType ,
365 Kokkos::RangePolicy< Traits ... > ,
373 const FunctorType m_functor ;
374 const Policy m_policy ;
376 template<
class TagType >
377 typename std::enable_if< std::is_same< TagType , void >::value >::type
380 const typename Policy::member_type e = m_policy.end();
381 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
386 template<
class TagType >
387 typename std::enable_if< ! std::is_same< TagType , void >::value >::type
391 const typename Policy::member_type e = m_policy.end();
392 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
401 { this->
template exec< typename Policy::work_tag >(); }
404 ParallelFor(
const FunctorType & arg_functor
405 ,
const Policy & arg_policy )
406 : m_functor( arg_functor )
407 , m_policy( arg_policy )
413 template<
class FunctorType ,
class ReducerType ,
class ... Traits >
414 class ParallelReduce< FunctorType
415 ,
Kokkos::RangePolicy< Traits ... >
423 typedef typename Policy::work_tag WorkTag ;
425 typedef Kokkos::Impl::if_c< std::is_same<InvalidType,ReducerType>::value, FunctorType, ReducerType> ReducerConditional;
426 typedef typename ReducerConditional::type ReducerTypeFwd;
428 typedef FunctorAnalysis< FunctorPatternInterface::REDUCE , Policy , FunctorType > Analysis ;
430 typedef Kokkos::Impl::FunctorValueInit< ReducerTypeFwd , WorkTag > ValueInit ;
432 typedef typename Analysis::pointer_type pointer_type ;
433 typedef typename Analysis::reference_type reference_type ;
435 const FunctorType m_functor ;
436 const Policy m_policy ;
437 const ReducerType m_reducer ;
438 const pointer_type m_result_ptr ;
440 template<
class TagType >
442 typename std::enable_if< std::is_same< TagType , void >::value >::type
443 exec( reference_type update )
const 445 const typename Policy::member_type e = m_policy.end();
446 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
447 m_functor( i , update );
451 template<
class TagType >
453 typename std::enable_if< ! std::is_same< TagType , void >::value >::type
454 exec( reference_type update )
const 458 const typename Policy::member_type e = m_policy.end();
459 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
460 m_functor( t , i , update );
469 const size_t pool_reduce_size =
470 Analysis::value_size( ReducerConditional::select(m_functor , m_reducer) );
471 const size_t team_reduce_size = 0 ;
472 const size_t team_shared_size = 0 ;
473 const size_t thread_local_size = 0 ;
475 serial_resize_thread_team_data( pool_reduce_size
478 , thread_local_size );
480 HostThreadTeamData & data = *serial_get_thread_team_data();
483 m_result_ptr ? m_result_ptr : pointer_type(data.pool_reduce_local());
485 reference_type update =
486 ValueInit::init( ReducerConditional::select(m_functor , m_reducer) , ptr );
488 this->
template exec< WorkTag >( update );
490 Kokkos::Impl::FunctorFinal< ReducerTypeFwd , WorkTag >::
491 final( ReducerConditional::select(m_functor , m_reducer) , ptr );
494 template<
class HostViewType >
495 ParallelReduce(
const FunctorType & arg_functor ,
496 const Policy & arg_policy ,
497 const HostViewType & arg_result_view ,
498 typename std::enable_if<
499 Kokkos::is_view< HostViewType >::value &&
500 !Kokkos::is_reducer_type<ReducerType>::value
501 ,
void*>::type = NULL)
502 : m_functor( arg_functor )
503 , m_policy( arg_policy )
504 , m_reducer( InvalidType() )
505 , m_result_ptr( arg_result_view.data() )
507 static_assert( Kokkos::is_view< HostViewType >::value
508 ,
"Kokkos::Serial reduce result must be a View" );
510 static_assert( std::is_same< typename HostViewType::memory_space , HostSpace >::value
511 ,
"Kokkos::Serial reduce result must be a View in HostSpace" );
515 ParallelReduce(
const FunctorType & arg_functor
517 ,
const ReducerType& reducer )
518 : m_functor( arg_functor )
519 , m_policy( arg_policy )
520 , m_reducer( reducer )
521 , m_result_ptr( reducer.view().data() )
531 template<
class FunctorType ,
class ... Traits >
532 class ParallelScan< FunctorType
533 ,
Kokkos::RangePolicy< Traits ... >
540 typedef typename Policy::work_tag WorkTag ;
542 typedef FunctorAnalysis< FunctorPatternInterface::SCAN , Policy , FunctorType > Analysis ;
544 typedef Kokkos::Impl::FunctorValueInit< FunctorType , WorkTag > ValueInit ;
546 typedef typename Analysis::pointer_type pointer_type ;
547 typedef typename Analysis::reference_type reference_type ;
549 const FunctorType m_functor ;
550 const Policy m_policy ;
552 template<
class TagType >
554 typename std::enable_if< std::is_same< TagType , void >::value >::type
555 exec( reference_type update )
const 557 const typename Policy::member_type e = m_policy.end();
558 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
559 m_functor( i , update ,
true );
563 template<
class TagType >
565 typename std::enable_if< ! std::is_same< TagType , void >::value >::type
566 exec( reference_type update )
const 569 const typename Policy::member_type e = m_policy.end();
570 for (
typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
571 m_functor( t , i , update ,
true );
580 const size_t pool_reduce_size = Analysis::value_size( m_functor );
581 const size_t team_reduce_size = 0 ;
582 const size_t team_shared_size = 0 ;
583 const size_t thread_local_size = 0 ;
585 serial_resize_thread_team_data( pool_reduce_size
588 , thread_local_size );
590 HostThreadTeamData & data = *serial_get_thread_team_data();
592 reference_type update =
593 ValueInit::init( m_functor , pointer_type(data.pool_reduce_local()) );
595 this->
template exec< WorkTag >( update );
599 ParallelScan(
const FunctorType & arg_functor
600 ,
const Policy & arg_policy
602 : m_functor( arg_functor )
603 , m_policy( arg_policy )
617 template<
class FunctorType ,
class ... Properties >
618 class ParallelFor< FunctorType
619 ,
Kokkos::TeamPolicy< Properties ... >
625 enum { TEAM_REDUCE_SIZE = 512 };
627 typedef TeamPolicyInternal< Kokkos::Serial , Properties ...> Policy ;
628 typedef typename Policy::member_type Member ;
630 const FunctorType m_functor ;
634 template<
class TagType >
636 typename std::enable_if< std::is_same< TagType , void >::value >::type
637 exec( HostThreadTeamData & data )
const 639 for (
int ileague = 0 ; ileague < m_league ; ++ileague ) {
640 m_functor( Member(data,ileague,m_league) );
644 template<
class TagType >
646 typename std::enable_if< ! std::is_same< TagType , void >::value >::type
647 exec( HostThreadTeamData & data )
const 650 for (
int ileague = 0 ; ileague < m_league ; ++ileague ) {
651 m_functor( t , Member(data,ileague,m_league) );
660 const size_t pool_reduce_size = 0 ;
661 const size_t team_reduce_size = TEAM_REDUCE_SIZE ;
662 const size_t team_shared_size = m_shared ;
663 const size_t thread_local_size = 0 ;
665 serial_resize_thread_team_data( pool_reduce_size
668 , thread_local_size );
670 HostThreadTeamData & data = *serial_get_thread_team_data();
672 this->
template exec< typename Policy::work_tag >( data );
675 ParallelFor(
const FunctorType & arg_functor
676 ,
const Policy & arg_policy )
677 : m_functor( arg_functor )
678 , m_league( arg_policy.league_size() )
679 , m_shared( arg_policy.scratch_size(0) +
680 arg_policy.scratch_size(1) +
681 FunctorTeamShmemSize< FunctorType >::value( arg_functor , 1 ) )
687 template<
class FunctorType ,
class ReducerType ,
class ... Properties >
688 class ParallelReduce< FunctorType
689 ,
Kokkos::TeamPolicy< Properties ... >
696 enum { TEAM_REDUCE_SIZE = 512 };
698 typedef TeamPolicyInternal< Kokkos::Serial, Properties ... > Policy ;
700 typedef FunctorAnalysis< FunctorPatternInterface::REDUCE , Policy , FunctorType > Analysis ;
702 typedef typename Policy::member_type Member ;
703 typedef typename Policy::work_tag WorkTag ;
705 typedef Kokkos::Impl::if_c< std::is_same<InvalidType,ReducerType>::value, FunctorType, ReducerType> ReducerConditional;
706 typedef typename ReducerConditional::type ReducerTypeFwd;
708 typedef Kokkos::Impl::FunctorValueInit< ReducerTypeFwd , WorkTag > ValueInit ;
710 typedef typename Analysis::pointer_type pointer_type ;
711 typedef typename Analysis::reference_type reference_type ;
713 const FunctorType m_functor ;
715 const ReducerType m_reducer ;
716 pointer_type m_result_ptr ;
719 template<
class TagType >
721 typename std::enable_if< std::is_same< TagType , void >::value >::type
722 exec( HostThreadTeamData & data , reference_type update )
const 724 for (
int ileague = 0 ; ileague < m_league ; ++ileague ) {
725 m_functor( Member(data,ileague,m_league) , update );
729 template<
class TagType >
731 typename std::enable_if< ! std::is_same< TagType , void >::value >::type
732 exec( HostThreadTeamData & data , reference_type update )
const 736 for (
int ileague = 0 ; ileague < m_league ; ++ileague ) {
737 m_functor( t , Member(data,ileague,m_league) , update );
746 const size_t pool_reduce_size =
747 Analysis::value_size( ReducerConditional::select(m_functor, m_reducer));
749 const size_t team_reduce_size = TEAM_REDUCE_SIZE ;
750 const size_t team_shared_size = m_shared ;
751 const size_t thread_local_size = 0 ;
753 serial_resize_thread_team_data( pool_reduce_size
756 , thread_local_size );
759 HostThreadTeamData & data = *serial_get_thread_team_data();
762 m_result_ptr ? m_result_ptr : pointer_type(data.pool_reduce_local());
764 reference_type update =
765 ValueInit::init( ReducerConditional::select(m_functor , m_reducer) , ptr );
767 this->
template exec< WorkTag >( data , update );
769 Kokkos::Impl::FunctorFinal< ReducerTypeFwd , WorkTag >::
770 final( ReducerConditional::select(m_functor , m_reducer) , ptr );
773 template<
class ViewType >
774 ParallelReduce(
const FunctorType & arg_functor
775 ,
const Policy & arg_policy
776 ,
const ViewType & arg_result ,
777 typename std::enable_if<
778 Kokkos::is_view< ViewType >::value &&
779 !Kokkos::is_reducer_type<ReducerType>::value
780 ,
void*>::type = NULL)
781 : m_functor( arg_functor )
782 , m_league( arg_policy.league_size() )
783 , m_reducer( InvalidType() )
784 , m_result_ptr( arg_result.data() )
785 , m_shared( arg_policy.scratch_size(0) +
786 arg_policy.scratch_size(1) +
787 FunctorTeamShmemSize< FunctorType >::value( m_functor , 1 ) )
789 static_assert( Kokkos::is_view< ViewType >::value
790 ,
"Reduction result on Kokkos::Serial must be a Kokkos::View" );
792 static_assert( std::is_same<
typename ViewType::memory_space
794 ,
"Reduction result on Kokkos::Serial must be a Kokkos::View in HostSpace" );
798 ParallelReduce(
const FunctorType & arg_functor
800 ,
const ReducerType& reducer )
801 : m_functor( arg_functor )
802 , m_league( arg_policy.league_size() )
803 , m_reducer( reducer )
804 , m_result_ptr( reducer.view().data() )
805 , m_shared( arg_policy.scratch_size(0) +
806 arg_policy.scratch_size(1) +
807 FunctorTeamShmemSize< FunctorType >::value( arg_functor , 1 ) )
822 #include <impl/Kokkos_Serial_Task.hpp> 824 #endif // defined( KOKKOS_ENABLE_SERIAL ) void print_configuration(std::ostream &, const bool detail=false)
Print "Bill of Materials".
Memory space for main process and CPU execution spaces.
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.