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