Kokkos Core Kernels Package  Version of the Day
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 H. Carter Edwards (hcedwar@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_HAVE_SERIAL )
53 #include <Kokkos_Serial.hpp>
54 #endif
55 
56 #if defined( KOKKOS_HAVE_OPENMP )
57 #include <Kokkos_OpenMP.hpp>
58 #endif
59 
60 #if defined( KOKKOS_HAVE_PTHREAD )
61 #include <Kokkos_Threads.hpp>
62 #endif
63 
64 #if defined( KOKKOS_HAVE_CUDA )
65 #include <Kokkos_Cuda.hpp>
66 #endif
67 
68 #include <Kokkos_Pair.hpp>
69 #include <Kokkos_Array.hpp>
70 #include <Kokkos_View.hpp>
71 #include <Kokkos_Vectorization.hpp>
72 #include <Kokkos_Atomic.hpp>
73 #include <Kokkos_hwloc.hpp>
74 
75 #ifdef KOKKOS_HAVE_CXX11
76 #include <Kokkos_Complex.hpp>
77 #endif
78 
79 
80 //----------------------------------------------------------------------------
81 
82 namespace Kokkos {
83 
84 struct InitArguments {
85  int num_threads;
86  int num_numa;
87  int device_id;
88 
89  InitArguments() {
90  num_threads = -1;
91  num_numa = -1;
92  device_id = -1;
93  }
94 };
95 
96 void initialize(int& narg, char* arg[]);
97 
98 void initialize(const InitArguments& args = InitArguments());
99 
101 void finalize();
102 
104 void finalize_all();
105 
106 void fence();
107 
108 } // namespace Kokkos
109 
110 //----------------------------------------------------------------------------
111 //----------------------------------------------------------------------------
112 
113 namespace Kokkos {
114 namespace Experimental {
115 
116 /* Allocate memory from a memory space.
117  * The allocation is tracked in Kokkos memory tracking system, so
118  * leaked memory can be identified.
119  */
120 template< class Space = typename Kokkos::DefaultExecutionSpace::memory_space >
121 inline
122 void * kokkos_malloc( const std::string & arg_alloc_label
123  , const size_t arg_alloc_size )
124 {
125  typedef typename Space::memory_space MemorySpace ;
126  return Impl::SharedAllocationRecord< MemorySpace >::
127  allocate_tracked( MemorySpace() , arg_alloc_label , arg_alloc_size );
128 }
129 
130 template< class Space = typename Kokkos::DefaultExecutionSpace::memory_space >
131 inline
132 void * kokkos_malloc( const size_t arg_alloc_size )
133 {
134  typedef typename Space::memory_space MemorySpace ;
135  return Impl::SharedAllocationRecord< MemorySpace >::
136  allocate_tracked( MemorySpace() , "no-label" , arg_alloc_size );
137 }
138 
139 template< class Space = typename Kokkos::DefaultExecutionSpace::memory_space >
140 inline
141 void kokkos_free( void * arg_alloc )
142 {
143  typedef typename Space::memory_space MemorySpace ;
144  return Impl::SharedAllocationRecord< MemorySpace >::
145  deallocate_tracked( arg_alloc );
146 }
147 
148 template< class Space = typename Kokkos::DefaultExecutionSpace::memory_space >
149 inline
150 void * kokkos_realloc( void * arg_alloc , const size_t arg_alloc_size )
151 {
152  typedef typename Space::memory_space MemorySpace ;
153  return Impl::SharedAllocationRecord< MemorySpace >::
154  reallocate_tracked( arg_alloc , arg_alloc_size );
155 }
156 
157 } // namespace Experimental
158 } // namespace Kokkos
159 
160 #if defined( KOKKOS_USING_EXPERIMENTAL_VIEW )
161 
162 namespace Kokkos {
163 
164 using Kokkos::Experimental::kokkos_malloc ;
165 using Kokkos::Experimental::kokkos_realloc ;
166 using Kokkos::Experimental::kokkos_free ;
167 
168 }
169 
170 #else
171 
172 namespace Kokkos {
173 
174 namespace Impl {
175 // should only by used by kokkos_malloc and kokkos_free
176 struct MallocHelper
177 {
178  static void increment_ref_count( AllocationTracker const & tracker )
179  {
180  tracker.increment_ref_count();
181  }
182 
183  static void decrement_ref_count( AllocationTracker const & tracker )
184  {
185  tracker.decrement_ref_count();
186  }
187 };
188 } // namespace Impl
189 
190 /* Allocate memory from a memory space.
191  * The allocation is tracked in Kokkos memory tracking system, so
192  * leaked memory can be identified.
193  */
194 template< class Arg = DefaultExecutionSpace>
195 void* kokkos_malloc(const std::string label, size_t count) {
196  if(count == 0) return NULL;
197  typedef typename Arg::memory_space MemorySpace;
198  Impl::AllocationTracker tracker = MemorySpace::allocate_and_track(label,count);;
199  Impl::MallocHelper::increment_ref_count( tracker );
200  return tracker.alloc_ptr();
201 }
202 
203 template< class Arg = DefaultExecutionSpace>
204 void* kokkos_malloc(const size_t& count) {
205  return kokkos_malloc<Arg>("DefaultLabel",count);
206 }
207 
208 
209 /* Free memory from a memory space.
210  */
211 template< class Arg = DefaultExecutionSpace>
212 void kokkos_free(const void* ptr) {
213  typedef typename Arg::memory_space MemorySpace;
214  typedef typename MemorySpace::allocator allocator;
215  Impl::AllocationTracker tracker = Impl::AllocationTracker::find<allocator>(ptr);
216  if (tracker.is_valid()) {
217  Impl::MallocHelper::decrement_ref_count( tracker );
218  }
219 }
220 
221 
222 template< class Arg = DefaultExecutionSpace>
223 void* kokkos_realloc(const void* old_ptr, size_t size) {
224  if(old_ptr == NULL)
225  return kokkos_malloc<Arg>(size);
226 
227  typedef typename Arg::memory_space MemorySpace;
228  typedef typename MemorySpace::allocator allocator;
229  Impl::AllocationTracker tracker = Impl::AllocationTracker::find<allocator>(old_ptr);
230 
231  tracker.reallocate(size);
232 
233  return tracker.alloc_ptr();
234 }
235 
236 } // namespace Kokkos
237 
238 #endif
239 
240 //----------------------------------------------------------------------------
241 //----------------------------------------------------------------------------
242 
243 #endif
244 
Declaration and definition of Kokkos::Vectorization interface.
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.
Atomic functions.