Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_Threads.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_THREADS_HPP
46 #define KOKKOS_THREADS_HPP
47 
48 #include <Kokkos_Macros.hpp>
49 #if defined(KOKKOS_ENABLE_THREADS)
50 
51 #include <Kokkos_Core_fwd.hpp>
52 
53 #include <cstddef>
54 #include <iosfwd>
55 #include <Kokkos_HostSpace.hpp>
56 #include <Kokkos_ScratchSpace.hpp>
57 #include <Kokkos_Layout.hpp>
58 #include <Kokkos_MemoryTraits.hpp>
59 #include <impl/Kokkos_Profiling_Interface.hpp>
60 #include <impl/Kokkos_Tags.hpp>
61 
62 /*--------------------------------------------------------------------------*/
63 
64 namespace Kokkos {
65 namespace Impl {
66 class ThreadsExec;
67 } // namespace Impl
68 } // namespace Kokkos
69 
70 /*--------------------------------------------------------------------------*/
71 
72 namespace Kokkos {
73 
75 class Threads {
76  public:
78 
79  typedef Threads execution_space;
81  typedef Kokkos::HostSpace memory_space;
82 
84  typedef Kokkos::Device<execution_space, memory_space> device_type;
85 
86  typedef Kokkos::LayoutRight array_layout;
87  typedef memory_space::size_type size_type;
88 
89  typedef ScratchMemorySpace<Threads> scratch_memory_space;
90 
92  /*------------------------------------------------------------------------*/
94 
95 
98  static int in_parallel();
99 
101  static void print_configuration(std::ostream&, const bool detail = false);
102 
109  static void impl_static_fence();
110 
111 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
112  static void fence();
113 #else
114  void fence() const;
115 #endif
116 
118  static int concurrency();
119 
120 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
121  static bool sleep();
122 
123  static bool wake();
124 
125  static void finalize();
126 
127  static void initialize(unsigned threads_count = 0,
128  unsigned use_numa_count = 0,
129  unsigned use_cores_per_numa = 0,
130  bool allow_asynchronous_threadpool = false);
131 
132  static int is_initialized();
133 
134  static Threads& instance(int = 0);
135 
136  //----------------------------------------
137 
138  static int thread_pool_size(int depth = 0);
139 #if defined(KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST)
140  static int thread_pool_rank();
141 #else
142  KOKKOS_INLINE_FUNCTION static int thread_pool_rank() { return 0; }
143 #endif
144 
145  inline static unsigned max_hardware_threads() { return thread_pool_size(0); }
146  KOKKOS_INLINE_FUNCTION static unsigned hardware_thread_id() {
147  return thread_pool_rank();
148  }
149 #else
150  static void impl_finalize();
154 
156  /*------------------------------------------------------------------------*/
157  /*------------------------------------------------------------------------*/
159 
160 
178  static void impl_initialize(unsigned threads_count = 0,
179  unsigned use_numa_count = 0,
180  unsigned use_cores_per_numa = 0,
181  bool allow_asynchronous_threadpool = false);
182 
183  static int impl_is_initialized();
184 
185  static Threads& impl_instance(int = 0);
186 
187  //----------------------------------------
188 
189  static int impl_thread_pool_size(int depth = 0);
190 #if defined(KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST)
191  static int impl_thread_pool_rank();
192 #else
193  KOKKOS_INLINE_FUNCTION static int impl_thread_pool_rank() { return 0; }
194 #endif
195 
196  inline static unsigned impl_max_hardware_threads() {
197  return impl_thread_pool_size(0);
198  }
199  KOKKOS_INLINE_FUNCTION static unsigned impl_hardware_thread_id() {
200  return impl_thread_pool_rank();
201  }
202 #endif
203 
204  uint32_t impl_instance_id() const noexcept { return 0; }
205 
206  static const char* name();
208  //----------------------------------------
209 };
210 
211 namespace Profiling {
212 namespace Experimental {
213 template <>
214 struct DeviceTypeTraits<Threads> {
215  static constexpr DeviceType id = DeviceType::Threads;
216 };
217 } // namespace Experimental
218 } // namespace Profiling
219 } // namespace Kokkos
220 
221 /*--------------------------------------------------------------------------*/
222 
223 namespace Kokkos {
224 namespace Impl {
225 
226 template <>
227 struct MemorySpaceAccess<Kokkos::Threads::memory_space,
228  Kokkos::Threads::scratch_memory_space> {
229  enum { assignable = false };
230  enum { accessible = true };
231  enum { deepcopy = false };
232 };
233 
234 template <>
235 struct VerifyExecutionCanAccessMemorySpace<
236  Kokkos::Threads::memory_space, Kokkos::Threads::scratch_memory_space> {
237  enum { value = true };
238  inline static void verify(void) {}
239  inline static void verify(const void*) {}
240 };
241 
242 } // namespace Impl
243 } // namespace Kokkos
244 
245 /*--------------------------------------------------------------------------*/
246 
247 #include <Kokkos_ExecPolicy.hpp>
248 #include <Kokkos_Parallel.hpp>
249 #include <Threads/Kokkos_ThreadsExec.hpp>
250 #include <Threads/Kokkos_ThreadsTeam.hpp>
251 #include <Threads/Kokkos_Threads_Parallel.hpp>
252 
253 #include <KokkosExp_MDRangePolicy.hpp>
254 
255 //----------------------------------------------------------------------------
256 //----------------------------------------------------------------------------
257 
258 #endif /* #if defined( KOKKOS_ENABLE_THREADS ) */
259 #endif /* #define KOKKOS_THREADS_HPP */
void print_configuration(std::ostream &, const bool detail=false)
Print &quot;Bill of Materials&quot;.
Memory management for host memory.
Memory layout tag indicating right-to-left (C or lexigraphical scheme) striding of multi-indices...
Declaration of various MemoryLayout options.
Declaration of parallel operators.
void finalize()
Finalize the spaces that were initialized via Kokkos::initialize.