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 
299 public:
300 
302  typedef TeamPolicy execution_policy ;
303 
305  typedef Kokkos::Serial execution_space ;
306 
307  typedef typename
308  Impl::if_c< ! Impl::is_same< Kokkos::Serial , Arg0 >::value , Arg0 , Arg1 >::type
309  work_tag ;
310 
311  //----------------------------------------
312 
313  template< class FunctorType >
314  static
315  int team_size_max( const FunctorType & ) { return 1 ; }
316 
317  template< class FunctorType >
318  static
319  int team_size_recommended( const FunctorType & ) { return 1 ; }
320 
321  template< class FunctorType >
322  static
323  int team_size_recommended( const FunctorType & , const int& ) { return 1 ; }
324 
325  //----------------------------------------
326 
327  inline int team_size() const { return 1 ; }
328  inline int league_size() const { return m_league_size ; }
329 
331  TeamPolicy( execution_space &
332  , int league_size_request
333  , int /* team_size_request */
334  , int /* vector_length_request */ = 1 )
335  : m_league_size( league_size_request )
336  {}
337 
338  TeamPolicy( execution_space &
339  , int league_size_request
340  , const Kokkos::AUTO_t & /* team_size_request */
341  , int /* vector_length_request */ = 1 )
342  : m_league_size( league_size_request )
343  {}
344 
345  TeamPolicy( int league_size_request
346  , int /* team_size_request */
347  , int /* vector_length_request */ = 1 )
348  : m_league_size( league_size_request )
349  {}
350 
351  TeamPolicy( int league_size_request
352  , const Kokkos::AUTO_t & /* team_size_request */
353  , int /* vector_length_request */ = 1 )
354  : m_league_size( league_size_request )
355  {}
356 
357  typedef Impl::SerialTeamMember member_type ;
358 };
359 
360 } /* namespace Kokkos */
361 
362 /*--------------------------------------------------------------------------*/
363 /*--------------------------------------------------------------------------*/
364 
365 /*--------------------------------------------------------------------------*/
366 /*--------------------------------------------------------------------------*/
367 
368 namespace Kokkos {
369 namespace Impl {
370 
371 template< class FunctorType , class Arg0 , class Arg1 , class Arg2 >
372 class ParallelFor< FunctorType , Kokkos::RangePolicy< Arg0 , Arg1 , Arg2 , Kokkos::Serial > >
373 {
374 private:
375 
377 
378 public:
379  // work tag is void
380  template< class PType >
381  inline
382  ParallelFor( typename Impl::enable_if<
383  ( Impl::is_same< PType , Policy >::value &&
384  Impl::is_same< typename PType::work_tag , void >::value
385  ), const FunctorType & >::type functor
386  , const PType & policy )
387  {
388  const typename PType::member_type e = policy.end();
389  for ( typename PType::member_type i = policy.begin() ; i < e ; ++i ) {
390  functor( i );
391  }
392  }
393 
394  // work tag is non-void
395  template< class PType >
396  inline
397  ParallelFor( typename Impl::enable_if<
398  ( Impl::is_same< PType , Policy >::value &&
399  ! Impl::is_same< typename PType::work_tag , void >::value
400  ), const FunctorType & >::type functor
401  , const PType & policy )
402  {
403  const typename PType::member_type e = policy.end();
404  for ( typename PType::member_type i = policy.begin() ; i < e ; ++i ) {
405  functor( typename PType::work_tag() , i );
406  }
407  }
408 };
409 
410 template< class FunctorType , class Arg0 , class Arg1 , class Arg2 >
411 class ParallelReduce< FunctorType , Kokkos::RangePolicy< Arg0 , Arg1 , Arg2 , Kokkos::Serial > >
412 {
413 public:
415  typedef typename Policy::work_tag WorkTag ;
416  typedef Kokkos::Impl::FunctorValueTraits< FunctorType , WorkTag > ValueTraits ;
417  typedef Kokkos::Impl::FunctorValueInit< FunctorType , WorkTag > ValueInit ;
418 
419  typedef typename ValueTraits::pointer_type pointer_type ;
420  typedef typename ValueTraits::reference_type reference_type ;
421 
422  // Work tag is void
423  template< class ViewType , class PType >
424  ParallelReduce( typename Impl::enable_if<
425  ( Impl::is_view< ViewType >::value &&
426  Impl::is_same< typename ViewType::memory_space , HostSpace >::value &&
427  Impl::is_same< PType , Policy >::value &&
428  Impl::is_same< typename PType::work_tag , void >::value
429  ), const FunctorType & >::type functor
430  , const PType & policy
431  , const ViewType & result
432  )
433  {
434  pointer_type result_ptr = result.ptr_on_device();
435 
436  if ( ! result_ptr ) {
437  result_ptr = (pointer_type)
438  Kokkos::Serial::scratch_memory_resize( ValueTraits::value_size( functor ) , 0 );
439  }
440 
441  reference_type update = ValueInit::init( functor , result_ptr );
442 
443  const typename PType::member_type e = policy.end();
444  for ( typename PType::member_type i = policy.begin() ; i < e ; ++i ) {
445  functor( i , update );
446  }
447 
448  Kokkos::Impl::FunctorFinal< FunctorType , WorkTag >::final( functor , result_ptr );
449  }
450 
451  // Work tag is non-void
452  template< class ViewType , class PType >
453  ParallelReduce( typename Impl::enable_if<
454  ( Impl::is_view< ViewType >::value &&
455  Impl::is_same< typename ViewType::memory_space , HostSpace >::value &&
456  Impl::is_same< PType , Policy >::value &&
457  ! Impl::is_same< typename PType::work_tag , void >::value
458  ), const FunctorType & >::type functor
459  , const PType & policy
460  , const ViewType & result
461  )
462  {
463  pointer_type result_ptr = result.ptr_on_device();
464 
465  if ( ! result_ptr ) {
466  result_ptr = (pointer_type)
467  Kokkos::Serial::scratch_memory_resize( ValueTraits::value_size( functor ) , 0 );
468  }
469 
470  typename ValueTraits::reference_type update = ValueInit::init( functor , result_ptr );
471 
472  const typename PType::member_type e = policy.end();
473  for ( typename PType::member_type i = policy.begin() ; i < e ; ++i ) {
474  functor( typename PType::work_tag() , i , update );
475  }
476 
477  Kokkos::Impl::FunctorFinal< FunctorType , WorkTag >::final( functor , result_ptr );
478  }
479 };
480 
481 template< class FunctorType , class Arg0 , class Arg1 , class Arg2 >
482 class ParallelScan< FunctorType , Kokkos::RangePolicy< Arg0 , Arg1 , Arg2 , Kokkos::Serial > >
483 {
484 private:
485 
487 
488  typedef Kokkos::Impl::FunctorValueTraits< FunctorType , typename Policy::work_tag > ValueTraits ;
489  typedef Kokkos::Impl::FunctorValueInit< FunctorType , typename Policy::work_tag > ValueInit ;
490 
491 public:
492 
493  typedef typename ValueTraits::pointer_type pointer_type ;
494  typedef typename ValueTraits::reference_type reference_type ;
495 
496  // work tag is void
497  template< class PType >
498  inline
499  ParallelScan( typename Impl::enable_if<
500  ( Impl::is_same< PType , Policy >::value &&
501  Impl::is_same< typename PType::work_tag , void >::value
502  ), const FunctorType & >::type functor
503  , const PType & policy )
504  {
505  pointer_type result_ptr = (pointer_type)
506  Kokkos::Serial::scratch_memory_resize( ValueTraits::value_size( functor ) , 0 );
507 
508  reference_type update = ValueInit::init( functor , result_ptr );
509 
510  const typename PType::member_type e = policy.end();
511  for ( typename PType::member_type i = policy.begin() ; i < e ; ++i ) {
512  functor( i , update , true );
513  }
514 
515  Kokkos::Impl::FunctorFinal< FunctorType , typename Policy::work_tag >::final( functor , result_ptr );
516  }
517 
518  // work tag is non-void
519  template< class PType >
520  inline
521  ParallelScan( typename Impl::enable_if<
522  ( Impl::is_same< PType , Policy >::value &&
523  ! Impl::is_same< typename PType::work_tag , void >::value
524  ), const FunctorType & >::type functor
525  , const PType & policy )
526  {
527  pointer_type result_ptr = (pointer_type)
528  Kokkos::Serial::scratch_memory_resize( ValueTraits::value_size( functor ) , 0 );
529 
530  reference_type update = ValueInit::init( functor , result_ptr );
531 
532  const typename PType::member_type e = policy.end();
533  for ( typename PType::member_type i = policy.begin() ; i < e ; ++i ) {
534  functor( typename PType::work_tag() , i , update , true );
535  }
536 
537  Kokkos::Impl::FunctorFinal< FunctorType , typename Policy::work_tag >::final( functor , result_ptr );
538  }
539 };
540 
541 } // namespace Impl
542 } // namespace Kokkos
543 
544 /*--------------------------------------------------------------------------*/
545 /*--------------------------------------------------------------------------*/
546 
547 namespace Kokkos {
548 namespace Impl {
549 
550 template< class FunctorType , class Arg0 , class Arg1 >
551 class ParallelFor< FunctorType , Kokkos::TeamPolicy< Arg0 , Arg1 , Kokkos::Serial > >
552 {
553 private:
554 
556 
557  template< class TagType >
558  KOKKOS_FORCEINLINE_FUNCTION static
559  void driver( typename Impl::enable_if< Impl::is_same< TagType , void >::value ,
560  const FunctorType & >::type functor
561  , const typename Policy::member_type & member )
562  { functor( member ); }
563 
564  template< class TagType >
565  KOKKOS_FORCEINLINE_FUNCTION static
566  void driver( typename Impl::enable_if< ! Impl::is_same< TagType , void >::value ,
567  const FunctorType & >::type functor
568  , const typename Policy::member_type & member )
569  { functor( TagType() , member ); }
570 
571 public:
572 
573  ParallelFor( const FunctorType & functor
574  , const Policy & policy )
575  {
576  const int shared_size = FunctorTeamShmemSize< FunctorType >::value( functor , policy.team_size() );
577 
578  Kokkos::Serial::scratch_memory_resize( 0 , shared_size );
579 
580  for ( int ileague = 0 ; ileague < policy.league_size() ; ++ileague ) {
581  ParallelFor::template driver< typename Policy::work_tag >
582  ( functor , typename Policy::member_type(ileague,policy.league_size(),shared_size) );
583  // functor( typename Policy::member_type(ileague,policy.league_size(),shared_size) );
584  }
585  }
586 };
587 
588 template< class FunctorType , class Arg0 , class Arg1 >
589 class ParallelReduce< FunctorType , Kokkos::TeamPolicy< Arg0 , Arg1 , Kokkos::Serial > >
590 {
591 private:
592 
594  typedef Kokkos::Impl::FunctorValueTraits< FunctorType , typename Policy::work_tag > ValueTraits ;
595  typedef Kokkos::Impl::FunctorValueInit< FunctorType , typename Policy::work_tag > ValueInit ;
596 
597 public:
598 
599  typedef typename ValueTraits::pointer_type pointer_type ;
600  typedef typename ValueTraits::reference_type reference_type ;
601 
602 private:
603 
604  template< class TagType >
605  KOKKOS_FORCEINLINE_FUNCTION static
606  void driver( typename Impl::enable_if< Impl::is_same< TagType , void >::value ,
607  const FunctorType & >::type functor
608  , const typename Policy::member_type & member
609  , reference_type update )
610  { functor( member , update ); }
611 
612  template< class TagType >
613  KOKKOS_FORCEINLINE_FUNCTION static
614  void driver( typename Impl::enable_if< ! Impl::is_same< TagType , void >::value ,
615  const FunctorType & >::type functor
616  , const typename Policy::member_type & member
617  , reference_type update )
618  { functor( TagType() , member , update ); }
619 
620 public:
621 
622  template< class ViewType >
623  ParallelReduce( const FunctorType & functor
624  , const Policy & policy
625  , const ViewType & result
626  )
627  {
628  const int reduce_size = ValueTraits::value_size( functor );
629  const int shared_size = FunctorTeamShmemSize< FunctorType >::value( functor , policy.team_size() );
630  void * const scratch_reduce = Kokkos::Serial::scratch_memory_resize( reduce_size , shared_size );
631 
632  const pointer_type result_ptr =
633  result.ptr_on_device() ? result.ptr_on_device()
634  : (pointer_type) scratch_reduce ;
635 
636  reference_type update = ValueInit::init( functor , result_ptr );
637 
638  for ( int ileague = 0 ; ileague < policy.league_size() ; ++ileague ) {
639  ParallelReduce::template driver< typename Policy::work_tag >
640  ( functor , typename Policy::member_type(ileague,policy.league_size(),shared_size) , update );
641  }
642 
643  Kokkos::Impl::FunctorFinal< FunctorType , typename Policy::work_tag >::final( functor , result_ptr );
644  }
645 };
646 
647 } // namespace Impl
648 } // namespace Kokkos
649 
650 namespace Kokkos {
651 
652 namespace Impl {
653 
654 template<typename iType>
655 struct TeamThreadRangeBoundariesStruct<iType,SerialTeamMember> {
656  typedef iType index_type;
657  const iType begin ;
658  const iType end ;
659  enum {increment = 1};
660  const SerialTeamMember& thread;
661 
662  KOKKOS_INLINE_FUNCTION
663  TeamThreadRangeBoundariesStruct (const SerialTeamMember& arg_thread, const iType& arg_count)
664  : begin(0)
665  , end(arg_count)
666  , thread(arg_thread)
667  {}
668 
669  KOKKOS_INLINE_FUNCTION
670  TeamThreadRangeBoundariesStruct (const SerialTeamMember& arg_thread, const iType& arg_begin, const iType & arg_end )
671  : begin( arg_begin )
672  , end( arg_end)
673  , thread( arg_thread )
674  {}
675 };
676 
677  template<typename iType>
678  struct ThreadVectorRangeBoundariesStruct<iType,SerialTeamMember> {
679  typedef iType index_type;
680  enum {start = 0};
681  const iType end;
682  enum {increment = 1};
683 
684  KOKKOS_INLINE_FUNCTION
685  ThreadVectorRangeBoundariesStruct (const SerialTeamMember& thread, const iType& count):
686  end( count )
687  {}
688  };
689 
690 } // namespace Impl
691 
692 template<typename iType>
693 KOKKOS_INLINE_FUNCTION
694 Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>
695 TeamThreadRange( const Impl::SerialTeamMember& thread, const iType & count )
696 {
697  return Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>(thread,count);
698 }
699 
700 template<typename iType>
701 KOKKOS_INLINE_FUNCTION
702 Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>
703 TeamThreadRange( const Impl::SerialTeamMember& thread, const iType & begin , const iType & end )
704 {
705  return Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>(thread,begin,end);
706 }
707 
708 template<typename iType>
709 KOKKOS_INLINE_FUNCTION
710 Impl::ThreadVectorRangeBoundariesStruct<iType,Impl::SerialTeamMember >
711  ThreadVectorRange(const Impl::SerialTeamMember& thread, const iType& count) {
712  return Impl::ThreadVectorRangeBoundariesStruct<iType,Impl::SerialTeamMember >(thread,count);
713 }
714 
715 KOKKOS_INLINE_FUNCTION
716 Impl::ThreadSingleStruct<Impl::SerialTeamMember> PerTeam(const Impl::SerialTeamMember& thread) {
717  return Impl::ThreadSingleStruct<Impl::SerialTeamMember>(thread);
718 }
719 
720 KOKKOS_INLINE_FUNCTION
721 Impl::VectorSingleStruct<Impl::SerialTeamMember> PerThread(const Impl::SerialTeamMember& thread) {
722  return Impl::VectorSingleStruct<Impl::SerialTeamMember>(thread);
723 }
724 
725 } // namespace Kokkos
726 
727 namespace Kokkos {
728 
733 template<typename iType, class Lambda>
734 KOKKOS_INLINE_FUNCTION
735 void parallel_for(const Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>& loop_boundaries, const Lambda& lambda) {
736  for( iType i = loop_boundaries.begin; i < loop_boundaries.end; i+=loop_boundaries.increment)
737  lambda(i);
738 }
739 
744 template< typename iType, class Lambda, typename ValueType >
745 KOKKOS_INLINE_FUNCTION
746 void parallel_reduce(const Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>& loop_boundaries,
747  const Lambda & lambda, ValueType& result) {
748 
749  result = ValueType();
750 
751  for( iType i = loop_boundaries.begin; i < loop_boundaries.end; i+=loop_boundaries.increment) {
752  ValueType tmp = ValueType();
753  lambda(i,tmp);
754  result+=tmp;
755  }
756 
757  result = loop_boundaries.thread.team_reduce(result,Impl::JoinAdd<ValueType>());
758 }
759 
760 #ifdef KOKKOS_HAVE_CXX11
761 
769 template< typename iType, class Lambda, typename ValueType, class JoinType >
770 KOKKOS_INLINE_FUNCTION
771 void parallel_reduce(const Impl::TeamThreadRangeBoundariesStruct<iType,Impl::SerialTeamMember>& loop_boundaries,
772  const Lambda & lambda, const JoinType& join, ValueType& init_result) {
773 
774  ValueType result = init_result;
775 
776  for( iType i = loop_boundaries.begin; i < loop_boundaries.end; i+=loop_boundaries.increment) {
777  ValueType tmp = ValueType();
778  lambda(i,tmp);
779  join(result,tmp);
780  }
781 
782  init_result = loop_boundaries.thread.team_reduce(result,Impl::JoinLambdaAdapter<ValueType,JoinType>(join));
783 }
784 
785 #endif // KOKKOS_HAVE_CXX11
786 
787 } //namespace Kokkos
788 
789 namespace Kokkos {
794 template<typename iType, class Lambda>
795 KOKKOS_INLINE_FUNCTION
796 void parallel_for(const Impl::ThreadVectorRangeBoundariesStruct<iType,Impl::SerialTeamMember >&
797  loop_boundaries, const Lambda& lambda) {
798  #ifdef KOKKOS_HAVE_PRAGMA_IVDEP
799  #pragma ivdep
800  #endif
801  for( iType i = loop_boundaries.start; i < loop_boundaries.end; i+=loop_boundaries.increment)
802  lambda(i);
803 }
804 
809 template< typename iType, class Lambda, typename ValueType >
810 KOKKOS_INLINE_FUNCTION
811 void parallel_reduce(const Impl::ThreadVectorRangeBoundariesStruct<iType,Impl::SerialTeamMember >&
812  loop_boundaries, const Lambda & lambda, ValueType& result) {
813  result = ValueType();
814 #ifdef KOKKOS_HAVE_PRAGMA_IVDEP
815 #pragma ivdep
816 #endif
817  for( iType i = loop_boundaries.start; i < loop_boundaries.end; i+=loop_boundaries.increment) {
818  ValueType tmp = ValueType();
819  lambda(i,tmp);
820  result+=tmp;
821  }
822 }
823 
831 template< typename iType, class Lambda, typename ValueType, class JoinType >
832 KOKKOS_INLINE_FUNCTION
833 void parallel_reduce(const Impl::ThreadVectorRangeBoundariesStruct<iType,Impl::SerialTeamMember >&
834  loop_boundaries, const Lambda & lambda, const JoinType& join, ValueType& init_result) {
835 
836  ValueType result = init_result;
837 #ifdef KOKKOS_HAVE_PRAGMA_IVDEP
838 #pragma ivdep
839 #endif
840  for( iType i = loop_boundaries.start; i < loop_boundaries.end; i+=loop_boundaries.increment) {
841  ValueType tmp = ValueType();
842  lambda(i,tmp);
843  join(result,tmp);
844  }
845  init_result = result;
846 }
847 
858 template< typename iType, class FunctorType >
859 KOKKOS_INLINE_FUNCTION
860 void parallel_scan(const Impl::ThreadVectorRangeBoundariesStruct<iType,Impl::SerialTeamMember >&
861  loop_boundaries, const FunctorType & lambda) {
862 
863  typedef Kokkos::Impl::FunctorValueTraits< FunctorType , void > ValueTraits ;
864  typedef typename ValueTraits::value_type value_type ;
865 
866  value_type scan_val = value_type();
867 
868 #ifdef KOKKOS_HAVE_PRAGMA_IVDEP
869 #pragma ivdep
870 #endif
871  for( iType i = loop_boundaries.start; i < loop_boundaries.end; i+=loop_boundaries.increment) {
872  lambda(i,scan_val,true);
873  }
874 }
875 
876 } // namespace Kokkos
877 
878 namespace Kokkos {
879 
880 template<class FunctorType>
881 KOKKOS_INLINE_FUNCTION
882 void single(const Impl::VectorSingleStruct<Impl::SerialTeamMember>& , const FunctorType& lambda) {
883  lambda();
884 }
885 
886 template<class FunctorType>
887 KOKKOS_INLINE_FUNCTION
888 void single(const Impl::ThreadSingleStruct<Impl::SerialTeamMember>& , const FunctorType& lambda) {
889  lambda();
890 }
891 
892 template<class FunctorType, class ValueType>
893 KOKKOS_INLINE_FUNCTION
894 void single(const Impl::VectorSingleStruct<Impl::SerialTeamMember>& , const FunctorType& lambda, ValueType& val) {
895  lambda(val);
896 }
897 
898 template<class FunctorType, class ValueType>
899 KOKKOS_INLINE_FUNCTION
900 void single(const Impl::ThreadSingleStruct<Impl::SerialTeamMember>& , const FunctorType& lambda, ValueType& val) {
901  lambda(val);
902 }
903 }
904 
905 #endif // defined( KOKKOS_HAVE_SERIAL )
906 #endif /* #define KOKKOS_SERIAL_HPP */
907 
908 //----------------------------------------------------------------------------
909 //----------------------------------------------------------------------------
910 
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.
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.