Kokkos Core Kernels Package  Version of the Day
Kokkos_Core.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 
17 #ifndef KOKKOS_CORE_HPP
18 #define KOKKOS_CORE_HPP
19 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
20 #define KOKKOS_IMPL_PUBLIC_INCLUDE
21 #define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
22 #endif
23 
24 //----------------------------------------------------------------------------
25 // In the case windows.h is included before Kokkos_Core.hpp there might be
26 // errors due to the potentially defined macros with name "min" and "max" in
27 // windows.h. These collide with the use of "min" and "max" in names inside
28 // Kokkos. The macros will be redefined at the end of Kokkos_Core.hpp
29 #if defined(min)
30 #pragma push_macro("min")
31 #undef min
32 #define KOKKOS_IMPL_PUSH_MACRO_MIN
33 #endif
34 #if defined(max)
35 #pragma push_macro("max")
36 #undef max
37 #define KOKKOS_IMPL_PUSH_MACRO_MAX
38 #endif
39 
40 //----------------------------------------------------------------------------
41 // Include the execution space header files for the enabled execution spaces.
42 
43 #include <Kokkos_Core_fwd.hpp>
44 
45 #include <KokkosCore_Config_DeclareBackend.hpp>
46 
47 #include <Kokkos_Half.hpp>
48 #include <Kokkos_AnonymousSpace.hpp>
49 #include <Kokkos_LogicalSpaces.hpp>
50 #include <Kokkos_Pair.hpp>
51 #include <Kokkos_MinMaxClamp.hpp>
52 #include <Kokkos_MathematicalConstants.hpp>
53 #include <Kokkos_MathematicalFunctions.hpp>
54 #include <Kokkos_MathematicalSpecialFunctions.hpp>
55 #include <Kokkos_NumericTraits.hpp>
56 #include <Kokkos_MemoryPool.hpp>
57 #include <Kokkos_Array.hpp>
58 #include <Kokkos_View.hpp>
59 #include <Kokkos_Vectorization.hpp>
60 #include <Kokkos_Atomic.hpp>
61 #include <Kokkos_hwloc.hpp>
62 #include <Kokkos_Timer.hpp>
63 #include <Kokkos_Tuners.hpp>
64 #include <Kokkos_TaskScheduler.hpp>
65 #include <Kokkos_Complex.hpp>
66 #include <Kokkos_CopyViews.hpp>
67 #include <impl/Kokkos_TeamMDPolicy.hpp>
68 #include <impl/Kokkos_InitializationSettings.hpp>
69 #include <functional>
70 #include <iosfwd>
71 #include <memory>
72 #include <vector>
73 
74 //----------------------------------------------------------------------------
75 
76 namespace Kokkos {
77 
78 void initialize(int& argc, char* argv[]);
79 
80 void initialize(
81  InitializationSettings const& settings = InitializationSettings());
82 
83 namespace Impl {
84 
85 void pre_initialize(const InitializationSettings& settings);
86 
87 void post_initialize(const InitializationSettings& settings);
88 
89 void pre_finalize();
90 
91 void post_finalize();
92 
93 void declare_configuration_metadata(const std::string& category,
94  const std::string& key,
95  const std::string& value);
96 
97 } // namespace Impl
98 
99 [[nodiscard]] bool is_initialized() noexcept;
100 [[nodiscard]] bool is_finalized() noexcept;
101 
102 bool show_warnings() noexcept;
103 bool tune_internals() noexcept;
104 
106 void finalize();
107 
128 void push_finalize_hook(std::function<void()> f);
129 
130 void fence(const std::string& name /*= "Kokkos::fence: Unnamed Global Fence"*/);
131 
133 void print_configuration(std::ostream& os, bool verbose = false);
134 
135 } // namespace Kokkos
136 
137 //----------------------------------------------------------------------------
138 //----------------------------------------------------------------------------
139 
140 namespace Kokkos {
141 
142 /* Allocate memory from a memory space.
143  * The allocation is tracked in Kokkos memory tracking system, so
144  * leaked memory can be identified.
145  */
146 template <class Space = Kokkos::DefaultExecutionSpace::memory_space>
147 inline void* kokkos_malloc(const std::string& arg_alloc_label,
148  const size_t arg_alloc_size) {
149  using MemorySpace = typename Space::memory_space;
150  return Impl::SharedAllocationRecord<MemorySpace>::allocate_tracked(
151  MemorySpace(), arg_alloc_label, arg_alloc_size);
152 }
153 
154 template <class Space = Kokkos::DefaultExecutionSpace::memory_space>
155 inline void* kokkos_malloc(const size_t arg_alloc_size) {
156  using MemorySpace = typename Space::memory_space;
157  return Impl::SharedAllocationRecord<MemorySpace>::allocate_tracked(
158  MemorySpace(), "no-label", arg_alloc_size);
159 }
160 
161 template <class Space = Kokkos::DefaultExecutionSpace::memory_space>
162 inline void kokkos_free(void* arg_alloc) {
163  using MemorySpace = typename Space::memory_space;
164  return Impl::SharedAllocationRecord<MemorySpace>::deallocate_tracked(
165  arg_alloc);
166 }
167 
168 template <class Space = Kokkos::DefaultExecutionSpace::memory_space>
169 inline void* kokkos_realloc(void* arg_alloc, const size_t arg_alloc_size) {
170  using MemorySpace = typename Space::memory_space;
171  return Impl::SharedAllocationRecord<MemorySpace>::reallocate_tracked(
172  arg_alloc, arg_alloc_size);
173 }
174 
175 } // namespace Kokkos
176 
177 namespace Kokkos {
178 
187 namespace Impl {
188 
189 inline std::string scopeguard_correct_usage() {
190  return std::string(
191  "Do instead:\n"
192  " std::unique_ptr<Kokkos::ScopeGuard> guard =\n"
193  " !Kokkos::is_initialized() && !Kokkos::is_finalized()?\n"
194  " new ScopeGuard(argc,argv) : nullptr;\n");
195 }
196 
197 inline std::string scopeguard_create_while_initialized_warning() {
198  return std::string(
199  "Kokkos Error: Creating a ScopeGuard while Kokkos is initialized "
200  "is illegal.\n")
201  .append(scopeguard_correct_usage());
202 }
203 
204 inline std::string scopeguard_create_after_finalize_warning() {
205  return std::string(
206  "Kokkos Error: Creating a ScopeGuard after Kokkos was finalized "
207  "is illegal.\n")
208  .append(scopeguard_correct_usage());
209 }
210 
211 inline std::string scopeguard_destruct_after_finalize_warning() {
212  return std::string(
213  "Kokkos Error: Destroying a ScopeGuard after Kokkos was finalized "
214  "is illegal.\n")
215  .append(scopeguard_correct_usage());
216 }
217 
218 } // namespace Impl
219 
220 class KOKKOS_ATTRIBUTE_NODISCARD ScopeGuard {
221  public:
222  template <class... Args>
223 #if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) >= 201907
224  [[nodiscard]]
225 #endif
226  ScopeGuard(Args&&... args) {
227  if (is_initialized()) {
228  Kokkos::abort(
229  Impl::scopeguard_create_while_initialized_warning().c_str());
230  }
231  if (is_finalized()) {
232  Kokkos::abort(Impl::scopeguard_create_after_finalize_warning().c_str());
233  }
234  initialize(static_cast<Args&&>(args)...);
235  }
236 
237  ~ScopeGuard() {
238  if (is_finalized()) {
239  Kokkos::abort(Impl::scopeguard_destruct_after_finalize_warning().c_str());
240  }
241  finalize();
242  }
243 
244  ScopeGuard& operator=(const ScopeGuard&) = delete;
245  ScopeGuard& operator=(ScopeGuard&&) = delete;
246  ScopeGuard(const ScopeGuard&) = delete;
247  ScopeGuard(ScopeGuard&&) = delete;
248 };
249 
250 } // namespace Kokkos
251 
252 namespace Kokkos {
253 namespace Experimental {
254 // Partitioning an Execution Space: expects space and integer arguments for
255 // relative weight
256 // Customization point for backends
257 // Default behavior is to return the passed in instance
258 template <class ExecSpace, class... Args>
259 std::vector<ExecSpace> partition_space(ExecSpace const& space, Args...) {
260  static_assert(is_execution_space<ExecSpace>::value,
261  "Kokkos Error: partition_space expects an Execution Space as "
262  "first argument");
263  static_assert(
264  (... && std::is_arithmetic_v<Args>),
265  "Kokkos Error: partitioning arguments must be integers or floats");
266  std::vector<ExecSpace> instances(sizeof...(Args));
267  for (int s = 0; s < int(sizeof...(Args)); s++) instances[s] = space;
268  return instances;
269 }
270 
271 template <class ExecSpace, class T>
272 std::vector<ExecSpace> partition_space(ExecSpace const& space,
273  std::vector<T>& weights) {
274  static_assert(is_execution_space<ExecSpace>::value,
275  "Kokkos Error: partition_space expects an Execution Space as "
276  "first argument");
277  static_assert(
278  std::is_arithmetic<T>::value,
279  "Kokkos Error: partitioning arguments must be integers or floats");
280 
281  std::vector<ExecSpace> instances(weights.size());
282  for (int s = 0; s < int(weights.size()); s++) instances[s] = space;
283  return instances;
284 }
285 } // namespace Experimental
286 } // namespace Kokkos
287 
288 #include <Kokkos_Crs.hpp>
289 #include <Kokkos_WorkGraphPolicy.hpp>
290 // Including this in Kokkos_Parallel_Reduce.hpp led to a circular dependency
291 // because Kokkos::Sum is used in Kokkos_Combined_Reducer.hpp and the default.
292 // The real answer is to finally break up Kokkos_Parallel_Reduce.hpp into
293 // smaller parts...
294 #include <impl/Kokkos_Combined_Reducer.hpp>
295 // Yet another workaround to deal with circular dependency issues because the
296 // implementation of the RAII wrapper is using Kokkos::single.
297 #include <Kokkos_AcquireUniqueTokenImpl.hpp>
298 
299 // Specializations required after core definitions
300 #include <KokkosCore_Config_PostInclude.hpp>
301 
302 //----------------------------------------------------------------------------
303 // Redefinition of the macros min and max if we pushed them at entry of
304 // Kokkos_Core.hpp
305 #if defined(KOKKOS_IMPL_PUSH_MACRO_MIN)
306 #pragma pop_macro("min")
307 #undef KOKKOS_IMPL_PUSH_MACRO_MIN
308 #endif
309 #if defined(KOKKOS_IMPL_PUSH_MACRO_MAX)
310 #pragma pop_macro("max")
311 #undef KOKKOS_IMPL_PUSH_MACRO_MAX
312 #endif
313 
314 //----------------------------------------------------------------------------
315 //----------------------------------------------------------------------------
316 
317 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
318 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
319 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
320 #endif
321 #endif
Declaration and definition of Kokkos::Vectorization interface.
Declaration and definition of Kokkos::pair.
Atomic functions.
Definition: dummy.cpp:17