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  inline
111  static void fence( OpenMP const& = OpenMP() ) noexcept;
112 
117  inline
118  static bool is_asynchronous( OpenMP const& = OpenMP() ) noexcept;
119 
120 
126  static std::vector<OpenMP> partition(...);
127 
132  static OpenMP create_instance(...);
133 
138  template <typename F>
139  static void partition_master( F const& f
140  , int requested_num_partitions = 0
141  , int requested_partition_size = 0
142  );
143 
144  // use UniqueToken
145  static int concurrency();
146 
147 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
148  static void initialize( int thread_count,
150  int use_numa_count,
151  int use_cores_per_numa = 0);
152 
163  static void initialize( int thread_count = -1 );
164 
166  static bool is_initialized() noexcept;
167 
169  static void finalize();
170 
171  inline
172  static int thread_pool_size() noexcept;
173 
175  KOKKOS_INLINE_FUNCTION
176  static int thread_pool_rank() noexcept;
177 
178  inline
179  static int thread_pool_size( int depth );
180 
181  static void sleep() {};
182  static void wake() {};
183 
184  // Using omp_get_max_threads(); is problematic
185  // On Intel (essentially an initial call to the OpenMP runtime
186  // without a parallel region before will set a process mask for a single core
187  // The runtime will than bind threads for a parallel region to other cores on the
188  // entering the first parallel region and make the process mask the aggregate of
189  // the thread masks. The intend seems to be to make serial code run fast, if you
190  // compile with OpenMP enabled but don't actually use parallel regions or so
191  // static int omp_max_threads = omp_get_max_threads();
192  static int get_current_max_threads() noexcept;
193 
194  // use UniqueToken
195  inline
196  static int max_hardware_threads() noexcept;
197 
198  // use UniqueToken
199  KOKKOS_INLINE_FUNCTION
200  static int hardware_thread_id() noexcept;
201 #else
202  static void impl_initialize( int thread_count = -1 );
203 
205  static bool impl_is_initialized() noexcept;
206 
208  static void impl_finalize();
209 
210  inline
211  static int impl_thread_pool_size() noexcept;
212 
214  KOKKOS_INLINE_FUNCTION
215  static int impl_thread_pool_rank() noexcept;
216 
217  inline
218  static int impl_thread_pool_size( int depth );
219 
220  // use UniqueToken
221  inline
222  static int impl_max_hardware_threads() noexcept;
223 
224  // use UniqueToken
225  KOKKOS_INLINE_FUNCTION
226  static int impl_hardware_thread_id() noexcept;
227 
228  static int impl_get_current_max_threads() noexcept;
229 #endif
230 
231  static constexpr const char* name() noexcept { return "OpenMP"; }
232 };
233 
234 } // namespace Kokkos
235 
236 /*--------------------------------------------------------------------------*/
237 /*--------------------------------------------------------------------------*/
238 
239 namespace Kokkos {
240 namespace Impl {
241 
242 template<>
243 struct MemorySpaceAccess
244  < Kokkos::OpenMP::memory_space
245  , Kokkos::OpenMP::scratch_memory_space
246  >
247 {
248  enum { assignable = false };
249  enum { accessible = true };
250  enum { deepcopy = false };
251 };
252 
253 template<>
254 struct VerifyExecutionCanAccessMemorySpace
255  < Kokkos::OpenMP::memory_space
256  , Kokkos::OpenMP::scratch_memory_space
257  >
258 {
259  enum { value = true };
260  inline static void verify( void ) { }
261  inline static void verify( const void * ) { }
262 };
263 
264 } // namespace Impl
265 } // namespace Kokkos
266 
267 /*--------------------------------------------------------------------------*/
268 /*--------------------------------------------------------------------------*/
269 
270 #include <OpenMP/Kokkos_OpenMP_Exec.hpp>
271 #include <OpenMP/Kokkos_OpenMP_Team.hpp>
272 #include <OpenMP/Kokkos_OpenMP_Parallel.hpp>
273 #include <OpenMP/Kokkos_OpenMP_Task.hpp>
274 
275 #include <KokkosExp_MDRangePolicy.hpp>
276 /*--------------------------------------------------------------------------*/
277 
278 #endif /* #if defined( KOKKOS_ENABLE_OPENMP ) && defined( _OPENMP ) */
279 #endif /* #ifndef KOKKOS_OPENMP_HPP */
280 
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.