Kokkos Core Kernels Package  Version of the Day
Kokkos_Core.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_CORE_HPP
46 #define KOKKOS_CORE_HPP
47 
48 //----------------------------------------------------------------------------
49 // Include the execution space header files for the enabled execution spaces.
50 
51 #include <Kokkos_Core_fwd.hpp>
52 
53 // Fundamental type description for half precision
54 // Should not rely on other backend infrastructure
55 #include <Kokkos_Half.hpp>
56 #include <KokkosCore_Config_DeclareBackend.hpp>
57 
58 #include <Kokkos_AnonymousSpace.hpp>
59 #include <Kokkos_LogicalSpaces.hpp>
60 #include <Kokkos_Pair.hpp>
61 #include <Kokkos_MathematicalFunctions.hpp>
62 #include <Kokkos_MemoryPool.hpp>
63 #include <Kokkos_Array.hpp>
64 #include <Kokkos_View.hpp>
65 #include <Kokkos_Vectorization.hpp>
66 #include <Kokkos_Atomic.hpp>
67 #include <Kokkos_hwloc.hpp>
68 #include <Kokkos_Timer.hpp>
69 #include <Kokkos_Tuners.hpp>
70 #include <Kokkos_TaskScheduler.hpp>
71 #include <Kokkos_Complex.hpp>
72 #include <Kokkos_CopyViews.hpp>
73 #include <functional>
74 #include <iosfwd>
75 #include <map>
76 #include <memory>
77 
78 //----------------------------------------------------------------------------
79 
80 namespace Kokkos {
81 
82 struct InitArguments {
83  int num_threads;
84  int num_numa;
85  int device_id;
86  int ndevices;
87  int skip_device;
88  bool disable_warnings;
89  bool tune_internals;
90  bool tool_help = false;
91  std::string tool_lib = {};
92  std::string tool_args = {};
93 
94  InitArguments(int nt = -1, int nn = -1, int dv = -1, bool dw = false,
95  bool ti = false)
96  : num_threads{nt},
97  num_numa{nn},
98  device_id{dv},
99  ndevices{-1},
100  skip_device{9999},
101  disable_warnings{dw},
102  tune_internals{ti} {}
103 };
104 
105 namespace Impl {
106 
107 /* ExecSpaceManager - Responsible for initializing all of the registered
108  * backends. Backends are registered using the register_space_initializer()
109  * function which should be called from a global context so that it is called
110  * prior to initialize_spaces() which is called from Kokkos::initialize()
111  */
112 class ExecSpaceManager {
113  std::map<std::string, std::unique_ptr<ExecSpaceInitializerBase>>
114  exec_space_factory_list;
115 
116  public:
117  ExecSpaceManager() = default;
118 
119  void register_space_factory(std::string name,
120  std::unique_ptr<ExecSpaceInitializerBase> ptr);
121  void initialize_spaces(const Kokkos::InitArguments& args);
122  void finalize_spaces(const bool all_spaces);
123  void static_fence();
124  void print_configuration(std::ostream& msg, const bool detail);
125  static ExecSpaceManager& get_instance();
126 };
127 
128 template <class SpaceInitializerType>
129 int initialize_space_factory(std::string name) {
130  auto space_ptr = std::make_unique<SpaceInitializerType>();
131  ExecSpaceManager::get_instance().register_space_factory(name,
132  std::move(space_ptr));
133  return 1;
134 }
135 
136 } // namespace Impl
137 void initialize(int& narg, char* arg[]);
138 
139 void initialize(InitArguments args = InitArguments());
140 
141 namespace Impl {
142 
143 void pre_initialize(const InitArguments& args);
144 
145 void post_initialize(const InitArguments& args);
146 
147 void declare_configuration_metadata(const std::string& category,
148  const std::string& key,
149  const std::string& value);
150 
151 } // namespace Impl
152 
153 bool is_initialized() noexcept;
154 
155 bool show_warnings() noexcept;
156 bool tune_internals() noexcept;
157 
159 void finalize();
160 
181 void push_finalize_hook(std::function<void()> f);
182 
184 void finalize_all();
185 
186 void fence();
187 
189 void print_configuration(std::ostream&, const bool detail = false);
190 
191 } // namespace Kokkos
192 
193 //----------------------------------------------------------------------------
194 //----------------------------------------------------------------------------
195 
196 namespace Kokkos {
197 
198 /* Allocate memory from a memory space.
199  * The allocation is tracked in Kokkos memory tracking system, so
200  * leaked memory can be identified.
201  */
202 template <class Space = typename Kokkos::DefaultExecutionSpace::memory_space>
203 inline void* kokkos_malloc(const std::string& arg_alloc_label,
204  const size_t arg_alloc_size) {
205  using MemorySpace = typename Space::memory_space;
206  return Impl::SharedAllocationRecord<MemorySpace>::allocate_tracked(
207  MemorySpace(), arg_alloc_label, arg_alloc_size);
208 }
209 
210 template <class Space = typename Kokkos::DefaultExecutionSpace::memory_space>
211 inline void* kokkos_malloc(const size_t arg_alloc_size) {
212  using MemorySpace = typename Space::memory_space;
213  return Impl::SharedAllocationRecord<MemorySpace>::allocate_tracked(
214  MemorySpace(), "no-label", arg_alloc_size);
215 }
216 
217 template <class Space = typename Kokkos::DefaultExecutionSpace::memory_space>
218 inline void kokkos_free(void* arg_alloc) {
219  using MemorySpace = typename Space::memory_space;
220  return Impl::SharedAllocationRecord<MemorySpace>::deallocate_tracked(
221  arg_alloc);
222 }
223 
224 template <class Space = typename Kokkos::DefaultExecutionSpace::memory_space>
225 inline void* kokkos_realloc(void* arg_alloc, const size_t arg_alloc_size) {
226  using MemorySpace = typename Space::memory_space;
227  return Impl::SharedAllocationRecord<MemorySpace>::reallocate_tracked(
228  arg_alloc, arg_alloc_size);
229 }
230 
231 } // namespace Kokkos
232 
233 namespace Kokkos {
234 
244 class ScopeGuard {
245  public:
246  ScopeGuard(int& narg, char* arg[]) {
247  sg_init = false;
248  if (!Kokkos::is_initialized()) {
249  initialize(narg, arg);
250  sg_init = true;
251  }
252  }
253 
254  ScopeGuard(const InitArguments& args = InitArguments()) {
255  sg_init = false;
256  if (!Kokkos::is_initialized()) {
257  initialize(args);
258  sg_init = true;
259  }
260  }
261 
262  ~ScopeGuard() {
263  if (Kokkos::is_initialized() && sg_init) {
264  finalize();
265  }
266  }
267 
268  // private:
269  bool sg_init;
270 
271  ScopeGuard& operator=(const ScopeGuard&) = delete;
272  ScopeGuard(const ScopeGuard&) = delete;
273 };
274 
275 } // namespace Kokkos
276 
277 #include <Kokkos_Crs.hpp>
278 #include <Kokkos_WorkGraphPolicy.hpp>
279 // Including this in Kokkos_Parallel_Reduce.hpp led to a circular dependency
280 // because Kokkos::Sum is used in Kokkos_Combined_Reducer.hpp and the default.
281 // The real answer is to finally break up Kokkos_Parallel_Reduce.hpp into
282 // smaller parts...
283 #include <impl/Kokkos_Combined_Reducer.hpp>
284 // Yet another workaround to deal with circular dependency issues because the
285 // implementation of the RAII wrapper is using Kokkos::single.
286 #include <Kokkos_AcquireUniqueTokenImpl.hpp>
287 
288 // Specializations requires after core definitions
289 #include <KokkosCore_Config_PostInclude.hpp>
290 //----------------------------------------------------------------------------
291 //----------------------------------------------------------------------------
292 
293 #endif
Declaration and definition of Kokkos::Vectorization interface.
Declaration and definition of Kokkos::pair.
Atomic functions.
ScopeGuard Some user scope issues have been identified with some Kokkos::finalize calls; ScopeGuard a...
Definition: dummy.cpp:3