ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
KokkosExecutor.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File KokkosExecutor.h --------------------------*/
3/*--------------------------------------------------------------------------*/
50/*--------------------------------------------------------------------------*/
51/*----------------------------- DEFINITIONS --------------------------------*/
52/*--------------------------------------------------------------------------*/
53
54#ifndef __ADS_KokkosExecutor
55 #define __ADS_KokkosExecutor
56/* self-identification: #endif constant */
57
58#if defined( ADS_WITH_KOKKOS ) || __has_include( <Kokkos_Core.hpp> )
59
60/*--------------------------------------------------------------------------*/
61/*------------------------------ INCLUDES ----------------------------------*/
62/*--------------------------------------------------------------------------*/
63
64#include "ADS/Contract.h"
66
67#include <Kokkos_Core.hpp>
68
69#include <cstddef>
70#include <type_traits>
71#include <utility>
72
73/*--------------------------------------------------------------------------*/
74/*--------------------------- NAMESPACE ------------------------------------*/
75/*--------------------------------------------------------------------------*/
76
77namespace ADS
78{
79
80/*--------------------------------------------------------------------------*/
81/*------------------------- CLASS KokkosExecutor ---------------------------*/
82/*--------------------------------------------------------------------------*/
83
85template< class Space = Kokkos::DefaultHostExecutionSpace >
87{
88
89/*--------------------------------------------------------------------------*/
90 public:
93 explicit KokkosExecutor( Space s = Space() ) : f_s( std::move( s ) ) { }
94
96 [[nodiscard]] std::size_t concurrency( void ) const {
97 const int c = f_s.concurrency();
98 return( std::size_t( c > 0 ? c : 1 ) );
99 }
100
118 template< class F >
119 void bulk( std::size_t n , F && f ) {
120 if( ( n <= 1 ) || ( concurrency() == 1 ) || Inside::active() ||
121 inKernel( f_s ) ) {
122 for( std::size_t i = 0 ; i < n ; ++i )
123 f( i );
124 return;
125 }
126 detail::FirstError error;
127 auto * pe = &error;
128 auto * pf = &f;
129 Kokkos::parallel_for(
130 "ADS::KokkosExecutor::bulk" ,
131 Kokkos::RangePolicy< Space , Kokkos::Schedule< Kokkos::Dynamic > ,
132 Kokkos::IndexType< std::size_t > >( f_s , 0 , n )
133 .set_chunk_size( 1 ) ,
134 [ = ]( std::size_t i ) {
135 if( pe->failed() )
136 return;
137 Inside mark;
138 try {
139 ( *pf )( i );
140 }
141 catch( ... ) {
142 pe->keep();
143 }
144 } );
145 f_s.fence( "ADS::KokkosExecutor::bulk" );
146 error.rethrow();
147 }
148
149/*--------------------------------------------------------------------------*/
150 private:
151 struct Tag;
152 using Inside = detail::InsideCall< Tag >;
153
160 static bool inKernel( [[maybe_unused]] const Space & s ) {
161#if defined( KOKKOS_ENABLE_OPENMP )
162 if constexpr( std::is_same_v< Space , Kokkos::OpenMP > )
163 return( omp_get_level() > 0 );
164 else
165#endif
166 return( false );
167 }
168
169 Space f_s;
170 }; // end( class KokkosExecutor )
171
172/*--------------------------------------------------------------------------*/
173
174} // end( namespace ADS )
175
176#endif /* ADS_WITH_KOKKOS */
177
178/*--------------------------------------------------------------------------*/
179
180#endif /* KokkosExecutor.h included */
181
182/*--------------------------------------------------------------------------*/
183/*----------------------- End File KokkosExecutor.h ------------------------*/
184/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
std::size_t concurrency(void) const
the concurrency of the space
Definition KokkosExecutor.h:96
KokkosExecutor(Space s=Space())
the executor on the instance of the space (the default one if none is given)
Definition KokkosExecutor.h:93
void bulk(std::size_t n, F &&f)
the calls by a parallel_for of the space, in order on the calling thread inside a call
Definition KokkosExecutor.h:119
Definition ADT.h:96