Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_Serial.hpp
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16 
19 
20 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
21 #include <Kokkos_Macros.hpp>
22 static_assert(false,
23  "Including non-public Kokkos header files is not allowed.");
24 #endif
25 #ifndef KOKKOS_SERIAL_HPP
26 #define KOKKOS_SERIAL_HPP
27 
28 #include <Kokkos_Macros.hpp>
29 #if defined(KOKKOS_ENABLE_SERIAL)
30 
31 #include <cstddef>
32 #include <iosfwd>
33 #include <mutex>
34 #include <thread>
35 #include <Kokkos_Core_fwd.hpp>
36 #include <Kokkos_TaskScheduler.hpp>
37 #include <Kokkos_Layout.hpp>
38 #include <Kokkos_HostSpace.hpp>
39 #include <Kokkos_ScratchSpace.hpp>
40 #include <Kokkos_MemoryTraits.hpp>
41 #include <impl/Kokkos_HostThreadTeam.hpp>
42 #include <impl/Kokkos_FunctorAnalysis.hpp>
43 #include <impl/Kokkos_Tools.hpp>
44 #include <impl/Kokkos_HostSharedPtr.hpp>
45 #include <impl/Kokkos_InitializationSettings.hpp>
46 
47 namespace Kokkos {
48 
49 namespace Impl {
50 class SerialInternal {
51  public:
52  SerialInternal() = default;
53 
54  bool is_initialized();
55 
56  void initialize();
57 
58  void finalize();
59 
60  static SerialInternal& singleton();
61 
62  std::mutex m_thread_team_data_mutex;
63 
64  // Resize thread team data scratch memory
65  void resize_thread_team_data(size_t pool_reduce_bytes,
66  size_t team_reduce_bytes,
67  size_t team_shared_bytes,
68  size_t thread_local_bytes);
69 
70  HostThreadTeamData m_thread_team_data;
71  bool m_is_initialized = false;
72 };
73 } // namespace Impl
74 
87 class Serial {
88  public:
90 
91 
93  using execution_space = Serial;
95  using memory_space = Kokkos::HostSpace;
97  using size_type = memory_space::size_type;
99  using device_type = Kokkos::Device<execution_space, memory_space>;
100 
102  using array_layout = LayoutRight;
103 
105  using scratch_memory_space = ScratchMemorySpace<Kokkos::Serial>;
106 
108 
109  Serial();
110 
117  inline static int in_parallel() { return false; }
118 
125  static void impl_static_fence(const std::string& name) {
126  Kokkos::Tools::Experimental::Impl::profile_fence_event<Kokkos::Serial>(
127  name,
128  Kokkos::Tools::Experimental::SpecialSynchronizationCases::
129  GlobalDeviceSynchronization,
130  []() {}); // TODO: correct device ID
131  Kokkos::memory_fence();
132  }
133 
134  void fence(const std::string& name =
135  "Kokkos::Serial::fence: Unnamed Instance Fence") const {
136  Kokkos::Tools::Experimental::Impl::profile_fence_event<Kokkos::Serial>(
137  name, Kokkos::Tools::Experimental::Impl::DirectFenceIDHandle{1},
138  []() {}); // TODO: correct device ID
139  Kokkos::memory_fence();
140  }
141 
143 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
144  static int concurrency() { return 1; }
145 #else
146  int concurrency() const { return 1; }
147 #endif
148 
150  void print_configuration(std::ostream& os, bool verbose = false) const;
151 
152  static void impl_initialize(InitializationSettings const&);
153 
154  static bool impl_is_initialized();
155 
157  static void impl_finalize();
158 
159  //--------------------------------------------------------------------------
160 
161  inline static int impl_thread_pool_size(int = 0) { return 1; }
162  KOKKOS_INLINE_FUNCTION static int impl_thread_pool_rank() { return 0; }
163 
164  //--------------------------------------------------------------------------
165 
166  KOKKOS_INLINE_FUNCTION static unsigned impl_hardware_thread_id() {
167  return impl_thread_pool_rank();
168  }
169  inline static unsigned impl_max_hardware_threads() {
170  return impl_thread_pool_size(0);
171  }
172 
173  uint32_t impl_instance_id() const noexcept { return 1; }
174 
175  static const char* name();
176 
177  Impl::SerialInternal* impl_internal_space_instance() const {
178  return m_space_instance.get();
179  }
180 
181  private:
182  Kokkos::Impl::HostSharedPtr<Impl::SerialInternal> m_space_instance;
183  friend bool operator==(Serial const& lhs, Serial const& rhs) {
184  return lhs.impl_internal_space_instance() ==
185  rhs.impl_internal_space_instance();
186  }
187  friend bool operator!=(Serial const& lhs, Serial const& rhs) {
188  return !(lhs == rhs);
189  }
190  //--------------------------------------------------------------------------
191 };
192 
193 namespace Tools {
194 namespace Experimental {
195 template <>
196 struct DeviceTypeTraits<Serial> {
197  static constexpr DeviceType id = DeviceType::Serial;
198  static int device_id(const Serial&) { return 0; }
199 };
200 } // namespace Experimental
201 } // namespace Tools
202 } // namespace Kokkos
203 
204 /*--------------------------------------------------------------------------*/
205 /*--------------------------------------------------------------------------*/
206 
207 namespace Kokkos {
208 namespace Impl {
209 
210 // We only need to provide a specialization for Serial if there is a host
211 // parallel execution space since the specialization for
212 // DefaultHostExecutionSpace is defined elsewhere.
213 struct DummyExecutionSpace;
214 template <class DT, class... DP>
215 struct ZeroMemset<
216  std::conditional_t<!std::is_same<Serial, DefaultHostExecutionSpace>::value,
217  Serial, DummyExecutionSpace>,
218  DT, DP...> : public ZeroMemset<DefaultHostExecutionSpace, DT, DP...> {
219  using Base = ZeroMemset<DefaultHostExecutionSpace, DT, DP...>;
220  using Base::Base;
221 
222  ZeroMemset(const Serial&, const View<DT, DP...>& dst,
223  typename View<DT, DP...>::const_value_type& value)
224  : Base(dst, value) {}
225 };
226 
227 template <>
228 struct MemorySpaceAccess<Kokkos::Serial::memory_space,
229  Kokkos::Serial::scratch_memory_space> {
230  enum : bool { assignable = false };
231  enum : bool { accessible = true };
232  enum : bool { deepcopy = false };
233 };
234 
235 } // namespace Impl
236 } // namespace Kokkos
237 
238 #include <Serial/Kokkos_Serial_Parallel_Range.hpp>
239 #include <Serial/Kokkos_Serial_Parallel_MDRange.hpp>
240 #include <Serial/Kokkos_Serial_Parallel_Team.hpp>
241 #include <Serial/Kokkos_Serial_Task.hpp>
242 #include <Serial/Kokkos_Serial_UniqueToken.hpp>
243 
244 #endif // defined( KOKKOS_ENABLE_SERIAL )
245 #endif /* #define KOKKOS_SERIAL_HPP */
Memory management for host memory.