Kokkos Core Kernels Package  Version of the Day
Kokkos_Serial.hpp
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Kokkos v. 2.0
6 // Copyright (2014) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact H. Carter Edwards (hcedwar@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
46 
47 #ifndef KOKKOS_SERIAL_HPP
48 #define KOKKOS_SERIAL_HPP
49 
50 #include <cstddef>
51 #include <iosfwd>
52 #include <Kokkos_Parallel.hpp>
53 #include <Kokkos_Layout.hpp>
54 #include <Kokkos_HostSpace.hpp>
55 #include <Kokkos_ScratchSpace.hpp>
56 #include <Kokkos_MemoryTraits.hpp>
57 #include <impl/Kokkos_Tags.hpp>
58 #include <impl/Kokkos_FunctorAdapter.hpp>
59 
60 #if defined( KOKKOS_HAVE_SERIAL )
61 
62 namespace Kokkos {
63 
76 class Serial {
77 public:
79 
80 
82  typedef Serial execution_space ;
84  typedef HostSpace::size_type size_type ;
86  typedef HostSpace memory_space ;
89 
91  typedef LayoutRight array_layout ;
92 
94  typedef ScratchMemorySpace< Kokkos::Serial > scratch_memory_space ;
95 
97 
104  inline static int in_parallel() { return false ; }
105 
117  static bool sleep();
118 
124  static bool wake();
125 
132  static void fence() {}
133 
134  static void initialize( unsigned threads_count = 1 ,
135  unsigned use_numa_count = 0 ,
136  unsigned use_cores_per_numa = 0 ,
137  bool allow_asynchronous_threadpool = false) {
138  (void) threads_count;
139  (void) use_numa_count;
140  (void) use_cores_per_numa;
141  (void) allow_asynchronous_threadpool;
142 
143  // Init the array of locks used for arbitrarily sized atomics
144  Impl::init_lock_array_host_space();
145 
146  }
147 
148  static int is_initialized() { return 1 ; }
149 
151  static void finalize() {}
152 
154  static void print_configuration( std::ostream & , const bool /* detail */ = false ) {}
155 
156  //--------------------------------------------------------------------------
157 
158  inline static int thread_pool_size( int = 0 ) { return 1 ; }
159  KOKKOS_INLINE_FUNCTION static int thread_pool_rank() { return 0 ; }
160 
161  //--------------------------------------------------------------------------
162 
163  KOKKOS_INLINE_FUNCTION static unsigned hardware_thread_id() { return thread_pool_rank(); }
164  inline static unsigned max_hardware_threads() { return thread_pool_size(0); }
165 
166  //--------------------------------------------------------------------------
167 
168  static void * scratch_memory_resize( unsigned reduce_size , unsigned shared_size );
169 
170  //--------------------------------------------------------------------------
171 };
172 
173 } // namespace Kokkos
174 
175 /*--------------------------------------------------------------------------*/
176 /*--------------------------------------------------------------------------*/
177 
178 namespace Kokkos {
179 namespace Impl {
180 
181 template<>
182 struct VerifyExecutionCanAccessMemorySpace
183  < Kokkos::Serial::memory_space
184  , Kokkos::Serial::scratch_memory_space
185  >
186 {
187  enum { value = true };
188  inline static void verify( void ) { }
189  inline static void verify( const void * ) { }
190 };
191 
192 namespace SerialImpl {
193 
194 struct Sentinel {
195 
196  void * m_scratch ;
197  unsigned m_reduce_end ;
198  unsigned m_shared_end ;
199 
200  Sentinel();
201  ~Sentinel();
202  static Sentinel & singleton();
203 };
204 
205 inline
206 unsigned align( unsigned n );
207 }
208 } // namespace Impl
209 } // namespace Kokkos
210 
211 /*--------------------------------------------------------------------------*/
212 /*--------------------------------------------------------------------------*/
213 
214 namespace Kokkos {
215 namespace Impl {
216 
217 class SerialTeamMember {
218 private:
219  typedef Kokkos::ScratchMemorySpace< Kokkos::Serial > scratch_memory_space ;
220  const scratch_memory_space m_space ;
221  const int m_league_rank ;
222  const int m_league_size ;
223 
224  SerialTeamMember & operator = ( const SerialTeamMember & );
225 
226 public:
227 
228  KOKKOS_INLINE_FUNCTION
229  const scratch_memory_space & team_shmem() const { return m_space ; }
230 
231  KOKKOS_INLINE_FUNCTION int league_rank() const { return m_league_rank ; }
232  KOKKOS_INLINE_FUNCTION int league_size() const { return m_league_size ; }
233  KOKKOS_INLINE_FUNCTION int team_rank() const { return 0 ; }
234  KOKKOS_INLINE_FUNCTION int team_size() const { return 1 ; }
235 
236  KOKKOS_INLINE_FUNCTION void team_barrier() const {}
237 
238  template<class ValueType>
239  KOKKOS_INLINE_FUNCTION
240  void team_broadcast(const ValueType& , const int& ) const {}
241 
242  template< class ValueType, class JoinOp >
243  KOKKOS_INLINE_FUNCTION
244  ValueType team_reduce( const ValueType & value , const JoinOp & ) const
245  {
246  return value ;
247  }
248 
258  template< typename Type >
259  KOKKOS_INLINE_FUNCTION Type team_scan( const Type & value , Type * const global_accum ) const
260  {
261  const Type tmp = global_accum ? *global_accum : Type(0) ;
262  if ( global_accum ) { *global_accum += value ; }
263  return tmp ;
264  }
265 
271  template< typename Type >
272  KOKKOS_INLINE_FUNCTION Type team_scan( const Type & ) const
273  { return Type(0); }
274 
275  //----------------------------------------
276  // Execution space specific:
277 
278  SerialTeamMember( int arg_league_rank
279  , int arg_league_size
280  , int arg_shared_size
281  );
282 };
283 
284 } // namespace Impl
285 
286 
287 /*
288  * < Kokkos::Serial , WorkArgTag >
289  * < WorkArgTag , Impl::enable_if< Impl::is_same< Kokkos::Serial , Kokkos::DefaultExecutionSpace >::value >::type >
290  *
291  */
292 template< class Arg0 , class Arg1 >
293 class TeamPolicy< Arg0 , Arg1 , Kokkos::Serial >
294 {
295 private:
296 
297  const int m_league_size ;
298  const int m_scratch_size ;
299 
300 public:
301 
303  typedef TeamPolicy execution_policy ;
304 
306  typedef Kokkos::Serial execution_space ;
307 
308  typedef typename
309  Impl::if_c< ! Impl::is_same< Kokkos::Serial , Arg0 >::value , Arg0 , Arg1 >::type
310  work_tag ;
311 
312  //----------------------------------------
313 
314  template< class FunctorType >
315  static
316  int team_size_max( const FunctorType & ) { return 1 ; }
317 
318  template< class FunctorType >
319  static
320  int team_size_recommended( const FunctorType & ) { return 1 ; }
321 
322  template< class FunctorType >
323  static
324  int team_size_recommended( const FunctorType & , const int& ) { return 1 ; }
325 
326  //----------------------------------------
327 
328  inline int team_size() const { return 1 ; }
329  inline int league_size() const { return m_league_size ; }
330  inline size_t scratch_size() const { return m_scratch_size ; }
331 
333  TeamPolicy( execution_space &
334  , int league_size_request
335  , int /* team_size_request */
336  , int /* vector_length_request */ = 1 )
337  : m_league_size( league_size_request )
338  , m_scratch_size ( 0 )
339  {}
340 
341  TeamPolicy( execution_space &
342  , int league_size_request
343  , const Kokkos::AUTO_t & /* team_size_request */
344  , int /* vector_length_request */ = 1 )
345  : m_league_size( league_size_request )
346  , m_scratch_size ( 0 )
347  {}
348 
349  TeamPolicy( int league_size_request
350  , int /* team_size_request */
351  , int /* vector_length_request */ = 1 )
352  : m_league_size( league_size_request )
353  , m_scratch_size ( 0 )
354  {}
355 
356  TeamPolicy( int league_size_request
357  , const Kokkos::AUTO_t & /* team_size_request */
358  , int /* vector_length_request */ = 1 )
359  : m_league_size( league_size_request )
360  , m_scratch_size ( 0 )
361  {}
362 
363  template<class MemorySpace>
364  TeamPolicy( int league_size_request
365  , int /* team_size_request */
366  , const Experimental::TeamScratchRequest<MemorySpace> & scratch_request )
367  : m_league_size(league_size_request)
368  , m_scratch_size(scratch_request.total(1))
369  {}
370 
371 
372  template<class MemorySpace>
373  TeamPolicy( int league_size_request
374  , const Kokkos::AUTO_t & /* team_size_request */
375  , const Experimental::TeamScratchRequest<MemorySpace> & scratch_request )
376  : m_league_size(league_size_request)
377  , m_scratch_size(scratch_request.total(1))
378  {}
379 
380  typedef Impl::SerialTeamMember member_type ;
381 };
382 
383 } /* namespace Kokkos */
384 
385 /*--------------------------------------------------------------------------*/
386 /*--------------------------------------------------------------------------*/
387 
388 /*--------------------------------------------------------------------------*/
389 /*--------------------------------------------------------------------------*/
390 /* Parallel patterns for Kokkos::Serial with RangePolicy */
391 
392 namespace Kokkos {
393 namespace Impl {
394 
395 template< class FunctorType , class Arg0 , class Arg1 , class Arg2 >
396 class ParallelFor< FunctorType
397  , Kokkos::RangePolicy< Arg0 , Arg1 , Arg2 , Kokkos::Serial >
398  >
399 {
400 private:
401 
403 
404  const FunctorType m_functor ;
405  const Policy m_policy ;
406 
407  template< class TagType >
408  typename std::enable_if< std::is_same< TagType , void >::value >::type
409  exec() const
410  {
411  const typename Policy::member_type e = m_policy.end();
412  for ( typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
413  m_functor( i );
414  }
415  }
416 
417  template< class TagType >
418  typename std::enable_if< ! std::is_same< TagType , void >::value >::type
419  exec() const
420  {
421  const TagType t{} ;
422  const typename Policy::member_type e = m_policy.end();
423  for ( typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
424  m_functor( t , i );
425  }
426  }
427 
428 public:
429 
430  inline
431  void execute() const
432  { this-> template exec< typename Policy::work_tag >(); }
433 
434  inline
435  ParallelFor( const FunctorType & arg_functor
436  , const Policy & arg_policy )
437  : m_functor( arg_functor )
438  , m_policy( arg_policy )
439  {}
440 };
441 
442 /*--------------------------------------------------------------------------*/
443 
444 template< class FunctorType , class Arg0 , class Arg1 , class Arg2 >
445 class ParallelReduce< FunctorType
447  >
448 {
449 private:
450 
452  typedef typename Policy::work_tag WorkTag ;
453  typedef Kokkos::Impl::FunctorValueTraits< FunctorType , WorkTag > ValueTraits ;
454  typedef Kokkos::Impl::FunctorValueInit< FunctorType , WorkTag > ValueInit ;
455 
456  typedef typename ValueTraits::pointer_type pointer_type ;
457  typedef typename ValueTraits::reference_type reference_type ;
458 
459  const FunctorType m_functor ;
460  const Policy m_policy ;
461  const pointer_type m_result_ptr ;
462 
463 
464  template< class TagType >
465  inline
466  typename std::enable_if< std::is_same< TagType , void >::value >::type
467  exec( pointer_type ptr ) const
468  {
469  reference_type update = ValueInit::init( m_functor , ptr );
470 
471  const typename Policy::member_type e = m_policy.end();
472  for ( typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
473  m_functor( i , update );
474  }
475 
476  Kokkos::Impl::FunctorFinal< FunctorType , TagType >::
477  final( m_functor , ptr );
478  }
479 
480  template< class TagType >
481  inline
482  typename std::enable_if< ! std::is_same< TagType , void >::value >::type
483  exec( pointer_type ptr ) const
484  {
485  const TagType t{} ;
486  reference_type update = ValueInit::init( m_functor , ptr );
487 
488  const typename Policy::member_type e = m_policy.end();
489  for ( typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
490  m_functor( t , i , update );
491  }
492 
493  Kokkos::Impl::FunctorFinal< FunctorType , TagType >::
494  final( m_functor , ptr );
495  }
496 
497 public:
498 
499  inline
500  void execute() const
501  {
502  pointer_type ptr = (pointer_type) Kokkos::Serial::scratch_memory_resize
503  ( ValueTraits::value_size( m_functor ) , 0 );
504 
505  this-> template exec< WorkTag >( m_result_ptr ? m_result_ptr : ptr );
506  }
507 
508  template< class ViewType >
509  ParallelReduce( const FunctorType & arg_functor
510  , const Policy & arg_policy
511  , const ViewType & arg_result )
512  : m_functor( arg_functor )
513  , m_policy( arg_policy )
514  , m_result_ptr( arg_result.ptr_on_device() )
515  {
516  static_assert( Kokkos::is_view< ViewType >::value
517  , "Reduction result on Kokkos::Serial must be a Kokkos::View" );
518 
519  static_assert( std::is_same< typename ViewType::memory_space
520  , Kokkos::HostSpace >::value
521  , "Reduction result on Kokkos::Serial must be a Kokkos::View in HostSpace" );
522  }
523 };
524 
525 /*--------------------------------------------------------------------------*/
526 
527 template< class FunctorType , class Arg0 , class Arg1 , class Arg2 >
528 class ParallelScan< FunctorType
530  >
531 {
532 private:
533 
535  typedef typename Policy::work_tag WorkTag ;
536  typedef Kokkos::Impl::FunctorValueTraits< FunctorType , WorkTag > ValueTraits ;
537  typedef Kokkos::Impl::FunctorValueInit< FunctorType , WorkTag > ValueInit ;
538 
539  typedef typename ValueTraits::pointer_type pointer_type ;
540  typedef typename ValueTraits::reference_type reference_type ;
541 
542  const FunctorType m_functor ;
543  const Policy m_policy ;
544 
545  template< class TagType >
546  inline
547  typename std::enable_if< std::is_same< TagType , void >::value >::type
548  exec( pointer_type ptr ) const
549  {
550  reference_type update = ValueInit::init( m_functor , ptr );
551 
552  const typename Policy::member_type e = m_policy.end();
553  for ( typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
554  m_functor( i , update , true );
555  }
556  }
557 
558  template< class TagType >
559  inline
560  typename std::enable_if< ! std::is_same< TagType , void >::value >::type
561  exec( pointer_type ptr ) const
562  {
563  const TagType t{} ;
564  reference_type update = ValueInit::init( m_functor , ptr );
565 
566  const typename Policy::member_type e = m_policy.end();
567  for ( typename Policy::member_type i = m_policy.begin() ; i < e ; ++i ) {
568  m_functor( t , i , update , true );
569  }
570  }
571 
572 public:
573 
574  inline
575  void execute() const
576  {
577  pointer_type ptr = (pointer_type)
578  Kokkos::Serial::scratch_memory_resize( ValueTraits::value_size( m_functor ) , 0 );
579  this-> template exec< WorkTag >( ptr );
580  }
581 
582  inline
583  ParallelScan( const FunctorType & arg_functor
584  , const Policy & arg_policy
585  )
586  : m_functor( arg_functor )
587  , m_policy( arg_policy )
588  {}
589 };
590 
591 } // namespace Impl
592 } // namespace Kokkos
593 
594 /*--------------------------------------------------------------------------*/
595 /*--------------------------------------------------------------------------*/
596 /* Parallel patterns for Kokkos::Serial with TeamPolicy */
597 
598 namespace Kokkos {
599 namespace Impl {
600 
601 template< class FunctorType , class Arg0 , class Arg1 >
602 class ParallelFor< FunctorType
604  >
605 {
606 private:
607 
609  typedef typename Policy::member_type Member ;
610 
611  const FunctorType m_functor ;
612  const int m_league ;
613  const int m_shared ;
614 
615  template< class TagType >
616  inline
617  typename std::enable_if< std::is_same< TagType , void >::value >::type
618  exec() const
619  {
620  for ( int ileague = 0 ; ileague < m_league ; ++ileague ) {
621  m_functor( Member(ileague,m_league,m_shared) );
622  }
623  }
624 
625  template< class TagType >
626  inline
627  typename std::enable_if< ! std::is_same< TagType , void >::value >::type
628  exec() const
629  {
630  const TagType t{} ;
631  for ( int ileague = 0 ; ileague < m_league ; ++ileague ) {
632  m_functor( t , Member(ileague,m_league,m_shared) );
633  }
634  }
635 
636 public:
637 
638  inline
639  void execute() const
640  {
641  Kokkos::Serial::scratch_memory_resize( 0 , m_shared );
642  this-> template exec< typename Policy::work_tag >();
643  }
644 
645  ParallelFor( const FunctorType & arg_functor
646  , const Policy & arg_policy )
647  : m_functor( arg_functor )
648  , m_league( arg_policy.league_size() )
649  , m_shared( arg_policy.scratch_size() + FunctorTeamShmemSize< FunctorType >::value( arg_functor , 1 ) )
650  { }
651 };
652 
653 /*--------------------------------------------------------------------------*/
654 
655 template< class FunctorType , class Arg0 , class Arg1 >
656 class ParallelReduce< FunctorType
658  >
659 {
660 private:
661 
663  typedef typename Policy::member_type Member ;
664  typedef typename Policy::work_tag WorkTag ;
665  typedef Kokkos::Impl::FunctorValueTraits< FunctorType , WorkTag > ValueTraits ;
666  typedef Kokkos::Impl::FunctorValueInit< FunctorType , WorkTag > ValueInit ;
667 
668  typedef typename ValueTraits::pointer_type pointer_type ;
669  typedef typename ValueTraits::reference_type reference_type ;
670 
671  const FunctorType m_functor ;
672  const int m_league ;
673  const int m_shared ;
674  pointer_type m_result_ptr ;
675 
676  template< class TagType >
677  inline
678  typename std::enable_if< std::is_same< TagType , void >::value >::type
679  exec( pointer_type ptr ) const
680  {
681  reference_type update = ValueInit::init( m_functor , ptr );
682 
683  for ( int ileague = 0 ; ileague < m_league ; ++ileague ) {
684  m_functor( Member(ileague,m_league,m_shared) , update );
685  }
686 
687  Kokkos::Impl::FunctorFinal< FunctorType , TagType >::
688  final( m_functor , ptr );
689  }
690 
691  template< class TagType >
692  inline
693  typename std::enable_if< ! std::is_same< TagType , void >::value >::type
694  exec( pointer_type ptr ) const
695  {
696  const TagType t{} ;
697 
698  reference_type update = ValueInit::init( m_functor , ptr );
699 
700  for ( int ileague = 0 ; ileague < m_league ; ++ileague ) {
701  m_functor( t , Member(ileague,m_league,m_shared) , update );
702  }
703 
704  Kokkos::Impl::FunctorFinal< FunctorType , TagType >::
705  final( m_functor , ptr );
706  }
707 
708 public:
709 
710  inline
711  void execute() const
712  {
713  pointer_type ptr = (pointer_type) Kokkos::Serial::scratch_memory_resize
714  ( ValueTraits::value_size( m_functor ) , m_shared );
715 
716  this-> template exec< WorkTag >( m_result_ptr ? m_result_ptr : ptr );
717  }
718 
719  template< class ViewType >
720  ParallelReduce( const FunctorType & arg_functor
721  , const Policy & arg_policy
722  , const ViewType & arg_result
723  )
724  : m_functor( arg_functor )
725  , m_league( arg_policy.league_size() )
726  , m_shared( arg_policy.scratch_size() + FunctorTeamShmemSize< FunctorType >::value( m_functor , 1 ) )
727  , m_result_ptr( arg_result.ptr_on_device() )
728  {
729  static_assert( Kokkos::is_view< ViewType >::value
730  , "Reduction result on Kokkos::Serial must be a Kokkos::View" );
731 
732  static_assert( std::is_same< typename ViewType::memory_space
733  , Kokkos::HostSpace >::value
734  , "Reduction result on Kokkos::Serial must be a Kokkos::View in HostSpace" );
735  }
736 
737 };
738 
739 } // namespace Impl
740 } // namespace Kokkos
741 
742 /*--------------------------------------------------------------------------*/
743 /*--------------------------------------------------------------------------*/
744 /* Nested parallel patterns for Kokkos::Serial with TeamPolicy */
745 
746 namespace Kokkos {
747 namespace Impl {
748 
749 template<typename iType>
750 struct TeamThreadRangeBoundariesStruct<iType,SerialTeamMember> {
751  typedef iType index_type;
752  const iType begin ;
753  const iType end ;
754  enum {increment = 1};
755  const SerialTeamMember& thread;
756 
757  KOKKOS_INLINE_FUNCTION
758  TeamThreadRangeBoundariesStruct (const SerialTeamMember& arg_thread, const iType& arg_count)
759  : begin(0)
760  , end(arg_count)
761  , thread(arg_thread)
762  {}
763 
764  KOKKOS_INLINE_FUNCTION
765  TeamThreadRangeBoundariesStruct (const SerialTeamMember& arg_thread, const iType& arg_begin, const iType & arg_end )
766  : begin( arg_begin )
767  , end( arg_end)
768  , thread( arg_thread )
769  {}
770 };
771 
772  template<typename iType>
773  struct ThreadVectorRangeBoundariesStruct<iType,SerialTeamMember> {
774  typedef iType index_type;
775  enum {start = 0};
776  const iType end;
777  enum {increment = 1};
778 
779  KOKKOS_INLINE_FUNCTION
780  ThreadVectorRangeBoundariesStruct (const SerialTeamMember& thread, const iType& count):
781  end( count )
782  {}
783  };
784 
785 } // namespace Impl
786 
787 template<typename iType>
788 KOKKOS_INLINE_FUNCTION
789 Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>
790 TeamThreadRange( const Impl::SerialTeamMember& thread, const iType & count )
791 {
792  return Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>(thread,count);
793 }
794 
795 template<typename iType>
796 KOKKOS_INLINE_FUNCTION
797 Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>
798 TeamThreadRange( const Impl::SerialTeamMember& thread, const iType & begin , const iType & end )
799 {
800  return Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>(thread,begin,end);
801 }
802 
803 template<typename iType>
804 KOKKOS_INLINE_FUNCTION
805 Impl::ThreadVectorRangeBoundariesStruct<iType,Impl::SerialTeamMember >
806  ThreadVectorRange(const Impl::SerialTeamMember& thread, const iType& count) {
807  return Impl::ThreadVectorRangeBoundariesStruct<iType,Impl::SerialTeamMember >(thread,count);
808 }
809 
810 KOKKOS_INLINE_FUNCTION
811 Impl::ThreadSingleStruct<Impl::SerialTeamMember> PerTeam(const Impl::SerialTeamMember& thread) {
812  return Impl::ThreadSingleStruct<Impl::SerialTeamMember>(thread);
813 }
814 
815 KOKKOS_INLINE_FUNCTION
816 Impl::VectorSingleStruct<Impl::SerialTeamMember> PerThread(const Impl::SerialTeamMember& thread) {
817  return Impl::VectorSingleStruct<Impl::SerialTeamMember>(thread);
818 }
819 
820 } // namespace Kokkos
821 
822 namespace Kokkos {
823 
828 template<typename iType, class Lambda>
829 KOKKOS_INLINE_FUNCTION
830 void parallel_for(const Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>& loop_boundaries, const Lambda& lambda) {
831  for( iType i = loop_boundaries.begin; i < loop_boundaries.end; i+=loop_boundaries.increment)
832  lambda(i);
833 }
834 
839 template< typename iType, class Lambda, typename ValueType >
840 KOKKOS_INLINE_FUNCTION
841 void parallel_reduce(const Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>& loop_boundaries,
842  const Lambda & lambda, ValueType& result) {
843 
844  result = ValueType();
845 
846  for( iType i = loop_boundaries.begin; i < loop_boundaries.end; i+=loop_boundaries.increment) {
847  ValueType tmp = ValueType();
848  lambda(i,tmp);
849  result+=tmp;
850  }
851 
852  result = loop_boundaries.thread.team_reduce(result,Impl::JoinAdd<ValueType>());
853 }
854 
862 template< typename iType, class Lambda, typename ValueType, class JoinType >
863 KOKKOS_INLINE_FUNCTION
864 void parallel_reduce(const Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>& loop_boundaries,
865  const Lambda & lambda, const JoinType& join, ValueType& init_result) {
866 
867  ValueType result = init_result;
868 
869  for( iType i = loop_boundaries.begin; i < loop_boundaries.end; i+=loop_boundaries.increment) {
870  ValueType tmp = ValueType();
871  lambda(i,tmp);
872  join(result,tmp);
873  }
874 
875  init_result = loop_boundaries.thread.team_reduce(result,Impl::JoinLambdaAdapter<ValueType,JoinType>(join));
876 }
877 
878 } //namespace Kokkos
879 
880 namespace Kokkos {
885 template<typename iType, class Lambda>
886 KOKKOS_INLINE_FUNCTION
887 void parallel_for(const Impl::ThreadVectorRangeBoundariesStruct<iType,Impl::SerialTeamMember >&
888  loop_boundaries, const Lambda& lambda) {
889  #ifdef KOKKOS_HAVE_PRAGMA_IVDEP
890  #pragma ivdep
891  #endif
892  for( iType i = loop_boundaries.start; i < loop_boundaries.end; i+=loop_boundaries.increment)
893  lambda(i);
894 }
895 
900 template< typename iType, class Lambda, typename ValueType >
901 KOKKOS_INLINE_FUNCTION
902 void parallel_reduce(const Impl::ThreadVectorRangeBoundariesStruct<iType,Impl::SerialTeamMember >&
903  loop_boundaries, const Lambda & lambda, ValueType& result) {
904  result = ValueType();
905 #ifdef KOKKOS_HAVE_PRAGMA_IVDEP
906 #pragma ivdep
907 #endif
908  for( iType i = loop_boundaries.start; i < loop_boundaries.end; i+=loop_boundaries.increment) {
909  ValueType tmp = ValueType();
910  lambda(i,tmp);
911  result+=tmp;
912  }
913 }
914 
922 template< typename iType, class Lambda, typename ValueType, class JoinType >
923 KOKKOS_INLINE_FUNCTION
924 void parallel_reduce(const Impl::ThreadVectorRangeBoundariesStruct<iType,Impl::SerialTeamMember >&
925  loop_boundaries, const Lambda & lambda, const JoinType& join, ValueType& init_result) {
926 
927  ValueType result = init_result;
928 #ifdef KOKKOS_HAVE_PRAGMA_IVDEP
929 #pragma ivdep
930 #endif
931  for( iType i = loop_boundaries.start; i < loop_boundaries.end; i+=loop_boundaries.increment) {
932  ValueType tmp = ValueType();
933  lambda(i,tmp);
934  join(result,tmp);
935  }
936  init_result = result;
937 }
938 
949 template< typename iType, class FunctorType >
950 KOKKOS_INLINE_FUNCTION
951 void parallel_scan(const Impl::ThreadVectorRangeBoundariesStruct<iType,Impl::SerialTeamMember >&
952  loop_boundaries, const FunctorType & lambda) {
953 
954  typedef Kokkos::Impl::FunctorValueTraits< FunctorType , void > ValueTraits ;
955  typedef typename ValueTraits::value_type value_type ;
956 
957  value_type scan_val = value_type();
958 
959 #ifdef KOKKOS_HAVE_PRAGMA_IVDEP
960 #pragma ivdep
961 #endif
962  for( iType i = loop_boundaries.start; i < loop_boundaries.end; i+=loop_boundaries.increment) {
963  lambda(i,scan_val,true);
964  }
965 }
966 
967 } // namespace Kokkos
968 
969 namespace Kokkos {
970 
971 template<class FunctorType>
972 KOKKOS_INLINE_FUNCTION
973 void single(const Impl::VectorSingleStruct<Impl::SerialTeamMember>& , const FunctorType& lambda) {
974  lambda();
975 }
976 
977 template<class FunctorType>
978 KOKKOS_INLINE_FUNCTION
979 void single(const Impl::ThreadSingleStruct<Impl::SerialTeamMember>& , const FunctorType& lambda) {
980  lambda();
981 }
982 
983 template<class FunctorType, class ValueType>
984 KOKKOS_INLINE_FUNCTION
985 void single(const Impl::VectorSingleStruct<Impl::SerialTeamMember>& , const FunctorType& lambda, ValueType& val) {
986  lambda(val);
987 }
988 
989 template<class FunctorType, class ValueType>
990 KOKKOS_INLINE_FUNCTION
991 void single(const Impl::ThreadSingleStruct<Impl::SerialTeamMember>& , const FunctorType& lambda, ValueType& val) {
992  lambda(val);
993 }
994 }
995 
996 #endif // defined( KOKKOS_HAVE_SERIAL )
997 #endif /* #define KOKKOS_SERIAL_HPP */
998 
999 //----------------------------------------------------------------------------
1000 //----------------------------------------------------------------------------
1001 
Scratch memory space associated with an execution space.
KOKKOS_INLINE_FUNCTION Impl::TeamThreadRangeBoundariesStruct< iType, TeamMemberType > TeamThreadRange(const TeamMemberType &, const iType &count)
Execution policy for parallel work over a threads within a team.
void parallel_reduce(const ExecPolicy &policy, const FunctorType &functor, const std::string &str="", typename Impl::enable_if< !Impl::is_integral< ExecPolicy >::value >::type *=0)
Parallel reduction.
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.
KOKKOS_INLINE_FUNCTION Impl::ThreadVectorRangeBoundariesStruct< iType, TeamMemberType > ThreadVectorRange(const TeamMemberType &, const iType &count)
Execution policy for a vector parallel loop.
Execution policy for work over a range of an integral type.
void parallel_for(const ExecPolicy &policy, const FunctorType &functor, const std::string &str="", typename Impl::enable_if< !Impl::is_integral< ExecPolicy >::value >::type *=0)
Execute functor in parallel according to the execution policy.
Execution policy for parallel work over a league of teams of threads.