Kokkos Core Kernels Package  Version of the Day
Kokkos_HostSpace.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_HOSTSPACE_HPP
45 #define KOKKOS_HOSTSPACE_HPP
46 
47 #include <cstring>
48 #include <string>
49 #include <iosfwd>
50 #include <typeinfo>
51 
52 #include <Kokkos_Core_fwd.hpp>
53 #include <Kokkos_Concepts.hpp>
54 #include <Kokkos_MemoryTraits.hpp>
55 
56 #include <impl/Kokkos_Traits.hpp>
57 #include <impl/Kokkos_Error.hpp>
58 #include <impl/Kokkos_SharedAlloc.hpp>
59 
60 /*--------------------------------------------------------------------------*/
61 
62 namespace Kokkos {
63 
64 namespace Impl {
65 
72 void init_lock_array_host_space();
73 
79 bool lock_address_host_space(void* ptr);
80 
87 void unlock_address_host_space( void* ptr );
88 
89 } // namespace Impl
90 
91 } // namespace Kokkos
92 
93 namespace Kokkos {
94 
100 class HostSpace {
101 public:
104  typedef size_t size_type;
105 
112 #if defined( KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_OPENMP )
113  typedef Kokkos::OpenMP execution_space;
114 #elif defined( KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_THREADS )
115  typedef Kokkos::Threads execution_space;
116 //#elif defined( KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_QTHREADS )
117 // typedef Kokkos::Qthreads execution_space;
118 #elif defined( KOKKOS_ENABLE_OPENMP )
119  typedef Kokkos::OpenMP execution_space;
120 #elif defined( KOKKOS_ENABLE_THREADS )
121  typedef Kokkos::Threads execution_space;
122 //#elif defined( KOKKOS_ENABLE_QTHREADS )
123 // typedef Kokkos::Qthreads execution_space;
124 #elif defined( KOKKOS_ENABLE_SERIAL )
125  typedef Kokkos::Serial execution_space;
126 #else
127 # error "At least one of the following host execution spaces must be defined: Kokkos::OpenMP, Kokkos::Threads, Kokkos::Qthreads, or Kokkos::Serial. You might be seeing this message if you disabled the Kokkos::Serial device explicitly using the Kokkos_ENABLE_Serial:BOOL=OFF CMake option, but did not enable any of the other host execution space devices."
128 #endif
129 
132 
133  /*--------------------------------*/
134  /* Functions unique to the HostSpace */
135  static int in_parallel();
136 
137  static void register_in_parallel( int (*)() );
138 
139  /*--------------------------------*/
140 
142  HostSpace();
143  HostSpace( HostSpace && rhs ) = default;
144  HostSpace( const HostSpace & rhs ) = default;
145  HostSpace & operator = ( HostSpace && ) = default;
146  HostSpace & operator = ( const HostSpace & ) = default;
147  ~HostSpace() = default;
148 
151  enum AllocationMechanism { STD_MALLOC, POSIX_MEMALIGN, POSIX_MMAP, INTEL_MM_ALLOC };
152 
153  explicit
154  HostSpace( const AllocationMechanism & );
155 
157  void * allocate( const size_t arg_alloc_size ) const;
158 
160  void deallocate( void * const arg_alloc_ptr
161  , const size_t arg_alloc_size ) const;
162 
164  static constexpr const char* name();
165 
166 private:
167  AllocationMechanism m_alloc_mech;
168  static constexpr const char* m_name = "Host";
169  friend class Kokkos::Impl::SharedAllocationRecord< Kokkos::HostSpace, void >;
170 };
171 
172 } // namespace Kokkos
173 
174 //----------------------------------------------------------------------------
175 
176 namespace Kokkos {
177 
178 namespace Impl {
179 
181 
182 template< typename S >
183 struct HostMirror {
184 private:
185  // If input execution space can access HostSpace then keep it.
186  // Example: Kokkos::OpenMP can access, Kokkos::Cuda cannot
187  enum { keep_exe = Kokkos::Impl::MemorySpaceAccess
188  < typename S::execution_space::memory_space, Kokkos::HostSpace >::accessible };
189 
190  // If HostSpace can access memory space then keep it.
191  // Example: Cannot access Kokkos::CudaSpace, can access Kokkos::CudaUVMSpace
192  enum { keep_mem = Kokkos::Impl::MemorySpaceAccess
193  < Kokkos::HostSpace, typename S::memory_space >::accessible };
194 
195 public:
196 
197  typedef typename std::conditional
198  < keep_exe && keep_mem /* Can keep whole space */
199  , S
200  , typename std::conditional
201  < keep_mem /* Can keep memory space, use default Host execution space */
202  , Kokkos::Device< Kokkos::HostSpace::execution_space
203  , typename S::memory_space >
205  >::type
206  >::type Space;
207 };
208 
209 } // namespace Impl
210 
211 } // namespace Kokkos
212 
213 //----------------------------------------------------------------------------
214 
215 namespace Kokkos {
216 
217 namespace Impl {
218 
219 template<>
220 class SharedAllocationRecord< Kokkos::HostSpace, void >
221  : public SharedAllocationRecord< void, void >
222 {
223 private:
224  friend Kokkos::HostSpace;
225 
226  typedef SharedAllocationRecord< void, void > RecordBase;
227 
228  SharedAllocationRecord( const SharedAllocationRecord & ) = delete;
229  SharedAllocationRecord & operator = ( const SharedAllocationRecord & ) = delete;
230 
231  static void deallocate( RecordBase * );
232 
234  static RecordBase s_root_record;
235 
236  const Kokkos::HostSpace m_space;
237 
238 protected:
239  ~SharedAllocationRecord();
240  SharedAllocationRecord() = default;
241 
242  SharedAllocationRecord( const Kokkos::HostSpace & arg_space
243  , const std::string & arg_label
244  , const size_t arg_alloc_size
245  , const RecordBase::function_type arg_dealloc = & deallocate
246  );
247 
248 public:
249 
250  inline
251  std::string get_label() const
252  {
253  return std::string( RecordBase::head()->m_label );
254  }
255 
256  KOKKOS_INLINE_FUNCTION static
257  SharedAllocationRecord * allocate( const Kokkos::HostSpace & arg_space
258  , const std::string & arg_label
259  , const size_t arg_alloc_size
260  )
261  {
262 #if defined( KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST )
263  return new SharedAllocationRecord( arg_space, arg_label, arg_alloc_size );
264 #else
265  return (SharedAllocationRecord *) 0;
266 #endif
267  }
268 
269 
271  static
272  void * allocate_tracked( const Kokkos::HostSpace & arg_space
273  , const std::string & arg_label
274  , const size_t arg_alloc_size );
275 
277  static
278  void * reallocate_tracked( void * const arg_alloc_ptr
279  , const size_t arg_alloc_size );
280 
282  static
283  void deallocate_tracked( void * const arg_alloc_ptr );
284 
285  static SharedAllocationRecord * get_record( void * arg_alloc_ptr );
286 
287  static void print_records( std::ostream &, const Kokkos::HostSpace &, bool detail = false );
288 };
289 
290 } // namespace Impl
291 
292 } // namespace Kokkos
293 
294 //----------------------------------------------------------------------------
295 
296 namespace Kokkos {
297 
298 namespace Impl {
299 
300 template< class DstSpace, class SrcSpace, class ExecutionSpace = typename DstSpace::execution_space > struct DeepCopy;
301 
302 template< class ExecutionSpace >
303 struct DeepCopy< HostSpace, HostSpace, ExecutionSpace > {
304  DeepCopy( void * dst, const void * src, size_t n ) {
305  memcpy( dst, src, n );
306  }
307 
308  DeepCopy( const ExecutionSpace& exec, void * dst, const void * src, size_t n ) {
309  exec.fence();
310  memcpy( dst, src, n );
311  }
312 };
313 
314 } // namespace Impl
315 
316 } // namespace Kokkos
317 
318 #endif // #define KOKKOS_HOSTSPACE_HPP
319 
Kokkos::Device< execution_space, memory_space > device_type
This memory space preferred device_type.
AllocationMechanism
Non-default memory space instance to choose allocation mechansim, if available.
Memory space for main process and CPU execution spaces.
Memory management for host memory.
HostSpace memory_space
Tag this class as a kokkos memory space.
Access relationship between DstMemorySpace and SrcMemorySpace.