Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 #if defined(KOKKOS_ENABLE_SERIAL)
54 #include <Kokkos_Serial.hpp>
55 #endif
56 
57 #if defined(KOKKOS_ENABLE_OPENMP)
58 #include <Kokkos_OpenMP.hpp>
59 #endif
60 
61 #if defined(KOKKOS_ENABLE_OPENMPTARGET)
62 #include <Kokkos_OpenMPTarget.hpp>
63 #include <Kokkos_OpenMPTargetSpace.hpp>
64 #endif
65 
66 #if defined(KOKKOS_ENABLE_HPX)
67 #include <Kokkos_HPX.hpp>
68 #endif
69 
70 #if defined(KOKKOS_ENABLE_THREADS)
71 #include <Kokkos_Threads.hpp>
72 #endif
73 
74 #if defined(KOKKOS_ENABLE_CUDA)
75 #include <Kokkos_Cuda.hpp>
76 #endif
77 
78 #if defined(KOKKOS_ENABLE_ROCM)
79 #include <Kokkos_ROCm.hpp>
80 #endif
81 #if defined(KOKKOS_ENABLE_HIP)
82 #include <Kokkos_HIP.hpp>
83 #endif
84 
85 #include <Kokkos_AnonymousSpace.hpp>
86 #include <Kokkos_Pair.hpp>
87 #include <Kokkos_MemoryPool.hpp>
88 #include <Kokkos_Array.hpp>
89 #include <Kokkos_View.hpp>
90 #include <Kokkos_Vectorization.hpp>
91 #include <Kokkos_Atomic.hpp>
92 #include <Kokkos_hwloc.hpp>
93 #include <Kokkos_Timer.hpp>
94 #include <Kokkos_TaskScheduler.hpp>
95 #include <Kokkos_Complex.hpp>
96 #include <Kokkos_CopyViews.hpp>
97 #include <functional>
98 #include <iosfwd>
99 
100 //----------------------------------------------------------------------------
101 
102 namespace Kokkos {
103 
104 struct InitArguments {
105  int num_threads;
106  int num_numa;
107  int device_id;
108  int ndevices;
109  int skip_device;
110  bool disable_warnings;
111 
112  InitArguments(int nt = -1, int nn = -1, int dv = -1, bool dw = false)
113  : num_threads{nt},
114  num_numa{nn},
115  device_id{dv},
116  ndevices{-1},
117  skip_device{9999},
118  disable_warnings{dw} {}
119 };
120 
121 void initialize(int& narg, char* arg[]);
122 
123 void initialize(InitArguments args = InitArguments());
124 
125 namespace Impl {
126 
127 void pre_initialize(const InitArguments& args);
128 
129 void post_initialize(const InitArguments& args);
130 
131 } // namespace Impl
132 
133 bool is_initialized() noexcept;
134 
135 bool show_warnings() noexcept;
136 
138 void finalize();
139 
160 void push_finalize_hook(std::function<void()> f);
161 
163 void finalize_all();
164 
165 void fence();
166 
168 void print_configuration(std::ostream&, const bool detail = false);
169 
170 } // namespace Kokkos
171 
172 //----------------------------------------------------------------------------
173 //----------------------------------------------------------------------------
174 
175 namespace Kokkos {
176 
177 /* Allocate memory from a memory space.
178  * The allocation is tracked in Kokkos memory tracking system, so
179  * leaked memory can be identified.
180  */
181 template <class Space = typename Kokkos::DefaultExecutionSpace::memory_space>
182 inline void* kokkos_malloc(const std::string& arg_alloc_label,
183  const size_t arg_alloc_size) {
184  typedef typename Space::memory_space MemorySpace;
185  return Impl::SharedAllocationRecord<MemorySpace>::allocate_tracked(
186  MemorySpace(), arg_alloc_label, arg_alloc_size);
187 }
188 
189 template <class Space = typename Kokkos::DefaultExecutionSpace::memory_space>
190 inline void* kokkos_malloc(const size_t arg_alloc_size) {
191  typedef typename Space::memory_space MemorySpace;
192  return Impl::SharedAllocationRecord<MemorySpace>::allocate_tracked(
193  MemorySpace(), "no-label", arg_alloc_size);
194 }
195 
196 template <class Space = typename Kokkos::DefaultExecutionSpace::memory_space>
197 inline void kokkos_free(void* arg_alloc) {
198  typedef typename Space::memory_space MemorySpace;
199  return Impl::SharedAllocationRecord<MemorySpace>::deallocate_tracked(
200  arg_alloc);
201 }
202 
203 template <class Space = typename Kokkos::DefaultExecutionSpace::memory_space>
204 inline void* kokkos_realloc(void* arg_alloc, const size_t arg_alloc_size) {
205  typedef typename Space::memory_space MemorySpace;
206  return Impl::SharedAllocationRecord<MemorySpace>::reallocate_tracked(
207  arg_alloc, arg_alloc_size);
208 }
209 
210 } // namespace Kokkos
211 
212 namespace Kokkos {
213 
223 class ScopeGuard {
224  public:
225  ScopeGuard(int& narg, char* arg[]) {
226  sg_init = false;
227  if (!Kokkos::is_initialized()) {
228  initialize(narg, arg);
229  sg_init = true;
230  }
231  }
232 
233  ScopeGuard(const InitArguments& args = InitArguments()) {
234  sg_init = false;
235  if (!Kokkos::is_initialized()) {
236  initialize(args);
237  sg_init = true;
238  }
239  }
240 
241  ~ScopeGuard() {
242  if (Kokkos::is_initialized() && sg_init) {
243  finalize();
244  }
245  }
246 
247  // private:
248  bool sg_init;
249 
250  ScopeGuard& operator=(const ScopeGuard&) = delete;
251  ScopeGuard(const ScopeGuard&) = delete;
252 };
253 
254 } // namespace Kokkos
255 
256 #include <Kokkos_Crs.hpp>
257 #include <Kokkos_WorkGraphPolicy.hpp>
258 
259 //----------------------------------------------------------------------------
260 //----------------------------------------------------------------------------
261 
262 #endif
Declaration and definition of Kokkos::Vectorization interface.
void print_configuration(std::ostream &, const bool detail=false)
Print &quot;Bill of Materials&quot;.
Declaration and definition of Kokkos::pair.
Declaration and definition of Kokkos::Serial device.
void finalize_all()
Finalize all known execution spaces.
void finalize()
Finalize the spaces that were initialized via Kokkos::initialize.
void push_finalize_hook(std::function< void()> f)
Push a user-defined function to be called in Kokkos::finalize, before any Kokkos state is finalized...
Atomic functions.
ScopeGuard Some user scope issues have been identified with some Kokkos::finalize calls; ScopeGuard a...