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_THREADS )
70 #include <Kokkos_Threads.hpp>
71 #endif
72 
73 #if defined( KOKKOS_ENABLE_CUDA )
74 #include <Kokkos_Cuda.hpp>
75 #endif
76 
77 #if defined( KOKKOS_ENABLE_ROCM )
78 #include <Kokkos_ROCm.hpp>
79 #endif
80 
81 #include <Kokkos_AnonymousSpace.hpp>
82 #include <Kokkos_Pair.hpp>
83 #include <Kokkos_MemoryPool.hpp>
84 #include <Kokkos_Array.hpp>
85 #include <Kokkos_View.hpp>
86 #include <Kokkos_Vectorization.hpp>
87 #include <Kokkos_Atomic.hpp>
88 #include <Kokkos_hwloc.hpp>
89 #include <Kokkos_Timer.hpp>
90 
91 #include <Kokkos_Complex.hpp>
92 
93 #include <Kokkos_CopyViews.hpp>
94 #include <functional>
95 #include <iosfwd>
96 
97 //----------------------------------------------------------------------------
98 
99 namespace Kokkos {
100 
101 struct InitArguments {
102  int num_threads;
103  int num_numa;
104  int device_id;
105  int ndevices;
106  int skip_device;
107  bool disable_warnings;
108 
109  InitArguments( int nt = -1
110  , int nn = -1
111  , int dv = -1
112  , bool dw = false
113  )
114  : num_threads{ nt }
115  , num_numa{ nn }
116  , device_id{ dv }
117  , ndevices{ -1 }
118  , skip_device{ 9999 }
119  , disable_warnings{ dw }
120  {}
121 };
122 
123 void initialize(int& narg, char* arg[]);
124 
125 void initialize(const InitArguments& args = InitArguments());
126 
127 bool is_initialized() noexcept;
128 
129 bool show_warnings() noexcept;
130 
132 void finalize();
133 
154 void push_finalize_hook(std::function<void()> f);
155 
157 void finalize_all();
158 
159 void fence();
160 
162 void print_configuration( std::ostream & , const bool detail = false );
163 
164 } // namespace Kokkos
165 
166 //----------------------------------------------------------------------------
167 //----------------------------------------------------------------------------
168 
169 namespace Kokkos {
170 
171 /* Allocate memory from a memory space.
172  * The allocation is tracked in Kokkos memory tracking system, so
173  * leaked memory can be identified.
174  */
175 template< class Space = typename Kokkos::DefaultExecutionSpace::memory_space >
176 inline
177 void * kokkos_malloc( const std::string & arg_alloc_label
178  , const size_t arg_alloc_size )
179 {
180  typedef typename Space::memory_space MemorySpace ;
181  return Impl::SharedAllocationRecord< MemorySpace >::
182  allocate_tracked( MemorySpace() , arg_alloc_label , arg_alloc_size );
183 }
184 
185 template< class Space = typename Kokkos::DefaultExecutionSpace::memory_space >
186 inline
187 void * kokkos_malloc( const size_t arg_alloc_size )
188 {
189  typedef typename Space::memory_space MemorySpace ;
190  return Impl::SharedAllocationRecord< MemorySpace >::
191  allocate_tracked( MemorySpace() , "no-label" , arg_alloc_size );
192 }
193 
194 template< class Space = typename Kokkos::DefaultExecutionSpace::memory_space >
195 inline
196 void kokkos_free( void * arg_alloc )
197 {
198  typedef typename Space::memory_space MemorySpace ;
199  return Impl::SharedAllocationRecord< MemorySpace >::
200  deallocate_tracked( arg_alloc );
201 }
202 
203 template< class Space = typename Kokkos::DefaultExecutionSpace::memory_space >
204 inline
205 void * kokkos_realloc( void * arg_alloc , const size_t arg_alloc_size )
206 {
207  typedef typename Space::memory_space MemorySpace ;
208  return Impl::SharedAllocationRecord< MemorySpace >::
209  reallocate_tracked( arg_alloc , arg_alloc_size );
210 }
211 
212 } // namespace Kokkos
213 
214 namespace Kokkos {
215 
225 class ScopeGuard {
226 public:
227  ScopeGuard ( int& narg, char* arg[] )
228  {
229  sg_init = false;
230  if ( ! Kokkos::is_initialized() ) {
231  initialize( narg, arg );
232  sg_init = true;
233  }
234  }
235 
236  ScopeGuard ( const InitArguments& args = InitArguments() )
237  {
238  sg_init = false;
239  if ( ! Kokkos::is_initialized() ) {
240  initialize( args );
241  sg_init = true;
242  }
243  }
244 
245  ~ScopeGuard( )
246  {
247  if ( Kokkos::is_initialized() && sg_init) {
248  finalize();
249  }
250  }
251 
252 //private:
253  bool sg_init;
254 
255  ScopeGuard& operator=( const ScopeGuard& ) = delete;
256  ScopeGuard( const ScopeGuard& ) = delete;
257 
258 };
259 
260 } // namespace Kokkos
261 
262 #include <Kokkos_Crs.hpp>
263 #include <Kokkos_WorkGraphPolicy.hpp>
264 
265 //----------------------------------------------------------------------------
266 //----------------------------------------------------------------------------
267 
268 #endif
269 
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...