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. 2.0
6 // Copyright (2014) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Christian R. Trott (crtrott@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
44 #ifndef KOKKOS_CORE_HPP
45 #define KOKKOS_CORE_HPP
46 
47 //----------------------------------------------------------------------------
48 // Include the execution space header files for the enabled execution spaces.
49 
50 #include <Kokkos_Core_fwd.hpp>
51 
52 #if defined( KOKKOS_ENABLE_SERIAL )
53 #include <Kokkos_Serial.hpp>
54 #endif
55 
56 #if defined( KOKKOS_ENABLE_OPENMP )
57 #include <Kokkos_OpenMP.hpp>
58 #endif
59 
60 //#if defined( KOKKOS_ENABLE_OPENMPTARGET )
61 #include <Kokkos_OpenMPTarget.hpp>
62 #include <Kokkos_OpenMPTargetSpace.hpp>
63 //#endif
64 
65 #if defined( KOKKOS_ENABLE_QTHREADS )
66 #include <Kokkos_Qthreads.hpp>
67 #endif
68 
69 #if defined( KOKKOS_ENABLE_HPX )
70 #include <Kokkos_HPX.hpp>
71 #endif
72 
73 #if defined( KOKKOS_ENABLE_THREADS )
74 #include <Kokkos_Threads.hpp>
75 #endif
76 
77 #if defined( KOKKOS_ENABLE_CUDA )
78 #include <Kokkos_Cuda.hpp>
79 #endif
80 
81 #if defined( KOKKOS_ENABLE_ROCM )
82 #include <Kokkos_ROCm.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 
96 #include <Kokkos_Complex.hpp>
97 
98 #include <Kokkos_CopyViews.hpp>
99 #include <functional>
100 #include <iosfwd>
101 
102 //----------------------------------------------------------------------------
103 
104 namespace Kokkos {
105 
106 struct InitArguments {
107  int num_threads;
108  int num_numa;
109  int device_id;
110  int ndevices;
111  int skip_device;
112  bool disable_warnings;
113 
114  InitArguments( int nt = -1
115  , int nn = -1
116  , int dv = -1
117  , bool dw = false
118  )
119  : num_threads{ nt }
120  , num_numa{ nn }
121  , device_id{ dv }
122  , ndevices{ -1 }
123  , skip_device{ 9999 }
124  , disable_warnings{ dw }
125  {}
126 };
127 
128 void initialize(int& narg, char* arg[]);
129 
130 void initialize(const InitArguments& args = InitArguments());
131 
132 bool is_initialized() noexcept;
133 
134 bool show_warnings() noexcept;
135 
137 void finalize();
138 
159 void push_finalize_hook(std::function<void()> f);
160 
162 void finalize_all();
163 
164 void fence();
165 
167 void print_configuration( std::ostream & , const bool detail = false );
168 
169 } // namespace Kokkos
170 
171 //----------------------------------------------------------------------------
172 //----------------------------------------------------------------------------
173 
174 namespace Kokkos {
175 
176 /* Allocate memory from a memory space.
177  * The allocation is tracked in Kokkos memory tracking system, so
178  * leaked memory can be identified.
179  */
180 template< class Space = typename Kokkos::DefaultExecutionSpace::memory_space >
181 inline
182 void * kokkos_malloc( const std::string & arg_alloc_label
183  , const size_t arg_alloc_size )
184 {
185  typedef typename Space::memory_space MemorySpace ;
186  return Impl::SharedAllocationRecord< MemorySpace >::
187  allocate_tracked( MemorySpace() , arg_alloc_label , arg_alloc_size );
188 }
189 
190 template< class Space = typename Kokkos::DefaultExecutionSpace::memory_space >
191 inline
192 void * kokkos_malloc( const size_t arg_alloc_size )
193 {
194  typedef typename Space::memory_space MemorySpace ;
195  return Impl::SharedAllocationRecord< MemorySpace >::
196  allocate_tracked( MemorySpace() , "no-label" , arg_alloc_size );
197 }
198 
199 template< class Space = typename Kokkos::DefaultExecutionSpace::memory_space >
200 inline
201 void kokkos_free( void * arg_alloc )
202 {
203  typedef typename Space::memory_space MemorySpace ;
204  return Impl::SharedAllocationRecord< MemorySpace >::
205  deallocate_tracked( arg_alloc );
206 }
207 
208 template< class Space = typename Kokkos::DefaultExecutionSpace::memory_space >
209 inline
210 void * kokkos_realloc( void * arg_alloc , const size_t arg_alloc_size )
211 {
212  typedef typename Space::memory_space MemorySpace ;
213  return Impl::SharedAllocationRecord< MemorySpace >::
214  reallocate_tracked( arg_alloc , arg_alloc_size );
215 }
216 
217 } // namespace Kokkos
218 
219 namespace Kokkos {
220 
230 class ScopeGuard {
231 public:
232  ScopeGuard ( int& narg, char* arg[] )
233  {
234  sg_init = false;
235  if ( ! Kokkos::is_initialized() ) {
236  initialize( narg, arg );
237  sg_init = true;
238  }
239  }
240 
241  ScopeGuard ( const InitArguments& args = InitArguments() )
242  {
243  sg_init = false;
244  if ( ! Kokkos::is_initialized() ) {
245  initialize( args );
246  sg_init = true;
247  }
248  }
249 
250  ~ScopeGuard( )
251  {
252  if ( Kokkos::is_initialized() && sg_init) {
253  finalize();
254  }
255  }
256 
257 //private:
258  bool sg_init;
259 
260  ScopeGuard& operator=( const ScopeGuard& ) = delete;
261  ScopeGuard( const ScopeGuard& ) = delete;
262 
263 };
264 
265 } // namespace Kokkos
266 
267 #include <Kokkos_Crs.hpp>
268 #include <Kokkos_WorkGraphPolicy.hpp>
269 
270 //----------------------------------------------------------------------------
271 //----------------------------------------------------------------------------
272 
273 #endif
274 
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...