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