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. 3.0
6 // Copyright (2020) National Technology & Engineering
7 // Solutions of Sandia, LLC (NTESS).
8 //
9 // Under the terms of Contract DE-NA0003525 with NTESS,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Christian R. Trott (crtrott@sandia.gov)
40 //
41 // ************************************************************************
42 //@HEADER
43 */
44 
45 #ifndef KOKKOS_OPENMP_HPP
46 #define KOKKOS_OPENMP_HPP
47 
48 #include <Kokkos_Macros.hpp>
49 #if defined(KOKKOS_ENABLE_OPENMP)
50 
51 #include <Kokkos_Core_fwd.hpp>
52 
53 #include <cstddef>
54 #include <iosfwd>
55 #include <Kokkos_HostSpace.hpp>
56 
57 #ifdef KOKKOS_ENABLE_HBWSPACE
58 #include <Kokkos_HBWSpace.hpp>
59 #endif
60 
61 #include <Kokkos_ScratchSpace.hpp>
62 #include <Kokkos_Parallel.hpp>
63 #include <Kokkos_TaskScheduler.hpp>
64 #include <Kokkos_Layout.hpp>
65 #include <impl/Kokkos_Tags.hpp>
66 
67 #include <vector>
68 
69 /*--------------------------------------------------------------------------*/
70 
71 namespace Kokkos {
72 
73 namespace Impl {
74 class OpenMPExec;
75 }
76 
79 class OpenMP {
80  public:
82  using execution_space = OpenMP;
83 
84  using memory_space =
85 #ifdef KOKKOS_ENABLE_HBWSPACE
86  Experimental::HBWSpace;
87 #else
88  HostSpace;
89 #endif
90 
92  using device_type = Kokkos::Device<execution_space, memory_space>;
93  using array_layout = LayoutRight;
94  using size_type = memory_space::size_type;
95  using scratch_memory_space = ScratchMemorySpace<OpenMP>;
96 
98  static void print_configuration(std::ostream&, const bool verbose = false);
99 
101  inline static bool in_parallel(OpenMP const& = OpenMP()) noexcept;
102 
106  static void impl_static_fence(OpenMP const& = OpenMP()) noexcept;
107 
108 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
109  static void fence(OpenMP const& = OpenMP()) noexcept;
110 #else
111  void fence() const;
112 #endif
113 
118  inline static bool is_asynchronous(OpenMP const& = OpenMP()) noexcept;
119 
125  static std::vector<OpenMP> partition(...);
126 
131  static OpenMP create_instance(...);
132 
138  template <typename F>
139  static void partition_master(F const& f, int requested_num_partitions = 0,
140  int requested_partition_size = 0);
141 
142  // use UniqueToken
143  static int concurrency();
144 
145 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
146  static void initialize(int thread_count, int use_numa_count,
148  int use_cores_per_numa = 0);
149 
160  static void initialize(int thread_count = -1);
161 
164  static bool is_initialized() noexcept;
165 
167  static void finalize();
168 
169  inline static int thread_pool_size() noexcept;
170 
172  KOKKOS_INLINE_FUNCTION
173  static int thread_pool_rank() noexcept;
174 
175  inline static int thread_pool_size(int depth);
176 
177  static void sleep(){};
178  static void wake(){};
179 
180  // Using omp_get_max_threads(); is problematic
181  // On Intel (essentially an initial call to the OpenMP runtime
182  // without a parallel region before will set a process mask for a single core
183  // The runtime will than bind threads for a parallel region to other cores on
184  // the entering the first parallel region and make the process mask the
185  // aggregate of the thread masks. The intend seems to be to make serial code
186  // run fast, if you compile with OpenMP enabled but don't actually use
187  // parallel regions or so static int omp_max_threads = omp_get_max_threads();
188  static int get_current_max_threads() noexcept;
189 
190  // use UniqueToken
191  inline static int max_hardware_threads() noexcept;
192 
193  // use UniqueToken
194  KOKKOS_INLINE_FUNCTION
195  static int hardware_thread_id() noexcept;
196 #else
197  static void impl_initialize(int thread_count = -1);
198 
201  static bool impl_is_initialized() noexcept;
202 
204  static void impl_finalize();
205 
206  inline static int impl_thread_pool_size() noexcept;
207 
209  KOKKOS_INLINE_FUNCTION
210  static int impl_thread_pool_rank() noexcept;
211 
212  inline static int impl_thread_pool_size(int depth);
213 
214  // use UniqueToken
215  inline static int impl_max_hardware_threads() noexcept;
216 
217  // use UniqueToken
218  KOKKOS_INLINE_FUNCTION
219  static int impl_hardware_thread_id() noexcept;
220 
221  static int impl_get_current_max_threads() noexcept;
222 #endif
223 
224  static constexpr const char* name() noexcept { return "OpenMP"; }
225  uint32_t impl_instance_id() const noexcept { return 0; }
226 };
227 
228 namespace Profiling {
229 namespace Experimental {
230 template <>
231 struct DeviceTypeTraits<OpenMP> {
232  static constexpr DeviceType id = DeviceType::OpenMP;
233 };
234 } // namespace Experimental
235 } // namespace Profiling
236 } // namespace Kokkos
237 
238 /*--------------------------------------------------------------------------*/
239 /*--------------------------------------------------------------------------*/
240 
241 namespace Kokkos {
242 namespace Impl {
243 
244 template <>
245 struct MemorySpaceAccess<Kokkos::OpenMP::memory_space,
246  Kokkos::OpenMP::scratch_memory_space> {
247  enum { assignable = false };
248  enum { accessible = true };
249  enum { deepcopy = false };
250 };
251 
252 template <>
253 struct VerifyExecutionCanAccessMemorySpace<
254  Kokkos::OpenMP::memory_space, Kokkos::OpenMP::scratch_memory_space> {
255  enum { value = true };
256  inline static void verify(void) {}
257  inline static void verify(const void*) {}
258 };
259 
260 } // namespace Impl
261 } // namespace Kokkos
262 
263 /*--------------------------------------------------------------------------*/
264 /*--------------------------------------------------------------------------*/
265 
266 #include <OpenMP/Kokkos_OpenMP_Exec.hpp>
267 #include <OpenMP/Kokkos_OpenMP_Team.hpp>
268 #include <OpenMP/Kokkos_OpenMP_Parallel.hpp>
269 #include <OpenMP/Kokkos_OpenMP_Task.hpp>
270 
271 #include <KokkosExp_MDRangePolicy.hpp>
272 /*--------------------------------------------------------------------------*/
273 
274 #endif /* #if defined( KOKKOS_ENABLE_OPENMP ) && defined( _OPENMP ) */
275 #endif /* #ifndef KOKKOS_OPENMP_HPP */
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.