Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_OpenMP.hpp
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 Christian R. Trott (crtrott@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
44 #ifndef KOKKOS_OPENMP_HPP
45 #define KOKKOS_OPENMP_HPP
46 
47 #include <Kokkos_Macros.hpp>
48 #if defined( KOKKOS_ENABLE_OPENMP)
49 
50 #include <Kokkos_Core_fwd.hpp>
51 
52 #include <cstddef>
53 #include <iosfwd>
54 #include <Kokkos_HostSpace.hpp>
55 
56 #ifdef KOKKOS_ENABLE_HBWSPACE
57 #include <Kokkos_HBWSpace.hpp>
58 #endif
59 
60 #include <Kokkos_ScratchSpace.hpp>
61 #include <Kokkos_Parallel.hpp>
62 #include <Kokkos_TaskScheduler.hpp>
63 #include <Kokkos_Layout.hpp>
64 #include <impl/Kokkos_Tags.hpp>
65 
66 #include <vector>
67 
68 /*--------------------------------------------------------------------------*/
69 
70 namespace Kokkos {
71 
72 namespace Impl {
73 class OpenMPExec;
74 }
75 
78 class OpenMP {
79 public:
81  using execution_space = OpenMP;
82 
83  using memory_space =
84  #ifdef KOKKOS_ENABLE_HBWSPACE
85  Experimental::HBWSpace;
86  #else
87  HostSpace;
88  #endif
89 
91  using device_type = Kokkos::Device< execution_space, memory_space >;
92  using array_layout = LayoutRight;
93  using size_type = memory_space::size_type;
94  using scratch_memory_space = ScratchMemorySpace< OpenMP >;
95 
97  inline
98  OpenMP() noexcept;
99 
101  static void print_configuration( std::ostream & , const bool verbose = false );
102 
104  inline
105  static bool in_parallel( OpenMP const& = OpenMP() ) noexcept;
106 
110  static void impl_static_fence( OpenMP const& = OpenMP() ) noexcept;
111 
112  #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
113  static void fence( OpenMP const& = OpenMP() ) noexcept;
114  #else
115  void fence() const;
116  #endif
117 
118 
123  inline
124  static bool is_asynchronous( OpenMP const& = OpenMP() ) noexcept;
125 
126 
132  static std::vector<OpenMP> partition(...);
133 
138  static OpenMP create_instance(...);
139 
144  template <typename F>
145  static void partition_master( F const& f
146  , int requested_num_partitions = 0
147  , int requested_partition_size = 0
148  );
149 
150  // use UniqueToken
151  static int concurrency();
152 
153 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
154  static void initialize( int thread_count,
156  int use_numa_count,
157  int use_cores_per_numa = 0);
158 
169  static void initialize( int thread_count = -1 );
170 
172  static bool is_initialized() noexcept;
173 
175  static void finalize();
176 
177  inline
178  static int thread_pool_size() noexcept;
179 
181  KOKKOS_INLINE_FUNCTION
182  static int thread_pool_rank() noexcept;
183 
184  inline
185  static int thread_pool_size( int depth );
186 
187  static void sleep() {};
188  static void wake() {};
189 
190  // Using omp_get_max_threads(); is problematic
191  // On Intel (essentially an initial call to the OpenMP runtime
192  // without a parallel region before will set a process mask for a single core
193  // The runtime will than bind threads for a parallel region to other cores on the
194  // entering the first parallel region and make the process mask the aggregate of
195  // the thread masks. The intend seems to be to make serial code run fast, if you
196  // compile with OpenMP enabled but don't actually use parallel regions or so
197  // static int omp_max_threads = omp_get_max_threads();
198  static int get_current_max_threads() noexcept;
199 
200  // use UniqueToken
201  inline
202  static int max_hardware_threads() noexcept;
203 
204  // use UniqueToken
205  KOKKOS_INLINE_FUNCTION
206  static int hardware_thread_id() noexcept;
207 #else
208  static void impl_initialize( int thread_count = -1 );
209 
211  static bool impl_is_initialized() noexcept;
212 
214  static void impl_finalize();
215 
216  inline
217  static int impl_thread_pool_size() noexcept;
218 
220  KOKKOS_INLINE_FUNCTION
221  static int impl_thread_pool_rank() noexcept;
222 
223  inline
224  static int impl_thread_pool_size( int depth );
225 
226  // use UniqueToken
227  inline
228  static int impl_max_hardware_threads() noexcept;
229 
230  // use UniqueToken
231  KOKKOS_INLINE_FUNCTION
232  static int impl_hardware_thread_id() noexcept;
233 
234  static int impl_get_current_max_threads() noexcept;
235 #endif
236 
237  static constexpr const char* name() noexcept { return "OpenMP"; }
238 };
239 
240 } // namespace Kokkos
241 
242 /*--------------------------------------------------------------------------*/
243 /*--------------------------------------------------------------------------*/
244 
245 namespace Kokkos {
246 namespace Impl {
247 
248 template<>
249 struct MemorySpaceAccess
250  < Kokkos::OpenMP::memory_space
251  , Kokkos::OpenMP::scratch_memory_space
252  >
253 {
254  enum { assignable = false };
255  enum { accessible = true };
256  enum { deepcopy = false };
257 };
258 
259 template<>
260 struct VerifyExecutionCanAccessMemorySpace
261  < Kokkos::OpenMP::memory_space
262  , Kokkos::OpenMP::scratch_memory_space
263  >
264 {
265  enum { value = true };
266  inline static void verify( void ) { }
267  inline static void verify( const void * ) { }
268 };
269 
270 } // namespace Impl
271 } // namespace Kokkos
272 
273 /*--------------------------------------------------------------------------*/
274 /*--------------------------------------------------------------------------*/
275 
276 #include <OpenMP/Kokkos_OpenMP_Exec.hpp>
277 #include <OpenMP/Kokkos_OpenMP_Team.hpp>
278 #include <OpenMP/Kokkos_OpenMP_Parallel.hpp>
279 #include <OpenMP/Kokkos_OpenMP_Task.hpp>
280 
281 #include <KokkosExp_MDRangePolicy.hpp>
282 /*--------------------------------------------------------------------------*/
283 
284 #endif /* #if defined( KOKKOS_ENABLE_OPENMP ) && defined( _OPENMP ) */
285 #endif /* #ifndef KOKKOS_OPENMP_HPP */
286 
void print_configuration(std::ostream &, const bool detail=false)
Print &quot;Bill of Materials&quot;.
Declaration of various MemoryLayout options.
Declaration of parallel operators.
void finalize()
Finalize the spaces that were initialized via Kokkos::initialize.