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 Christian R. Trott (crtrott@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 
131  typedef Kokkos::Device< execution_space, memory_space > device_type;
132 
134  HostSpace();
135  HostSpace( HostSpace && rhs ) = default;
136  HostSpace( const HostSpace & rhs ) = default;
137  HostSpace & operator = ( HostSpace && ) = default;
138  HostSpace & operator = ( const HostSpace & ) = default;
139  ~HostSpace() = default;
140 
143  enum AllocationMechanism { STD_MALLOC, POSIX_MEMALIGN, POSIX_MMAP, INTEL_MM_ALLOC };
144 
145  explicit
146  HostSpace( const AllocationMechanism & );
147 
149  void * allocate( const size_t arg_alloc_size ) const;
150 
152  void deallocate( void * const arg_alloc_ptr
153  , const size_t arg_alloc_size ) const;
154 
156  static constexpr const char* name() { return m_name; }
157 
158 private:
159  AllocationMechanism m_alloc_mech;
160  static constexpr const char* m_name = "Host";
161  friend class Kokkos::Impl::SharedAllocationRecord< Kokkos::HostSpace, void >;
162 };
163 
164 } // namespace Kokkos
165 
166 //----------------------------------------------------------------------------
167 
168 namespace Kokkos {
169 
170 namespace Impl {
171 
173 
174 template< typename S >
175 struct HostMirror {
176 private:
177  // If input execution space can access HostSpace then keep it.
178  // Example: Kokkos::OpenMP can access, Kokkos::Cuda cannot
179  enum { keep_exe = Kokkos::Impl::MemorySpaceAccess
180  < typename S::execution_space::memory_space, Kokkos::HostSpace >::accessible };
181 
182  // If HostSpace can access memory space then keep it.
183  // Example: Cannot access Kokkos::CudaSpace, can access Kokkos::CudaUVMSpace
184  enum { keep_mem = Kokkos::Impl::MemorySpaceAccess
185  < Kokkos::HostSpace, typename S::memory_space >::accessible };
186 
187 public:
188 
189  typedef typename std::conditional
190  < keep_exe && keep_mem /* Can keep whole space */
191  , S
192  , typename std::conditional
193  < keep_mem /* Can keep memory space, use default Host execution space */
194  , Kokkos::Device< Kokkos::HostSpace::execution_space
195  , typename S::memory_space >
197  >::type
198  >::type Space;
199 };
200 
201 } // namespace Impl
202 
203 } // namespace Kokkos
204 
205 //----------------------------------------------------------------------------
206 
207 namespace Kokkos {
208 
209 namespace Impl {
210 
211 template<>
212 class SharedAllocationRecord< Kokkos::HostSpace, void >
213  : public SharedAllocationRecord< void, void >
214 {
215 private:
216  friend Kokkos::HostSpace;
217 
218  typedef SharedAllocationRecord< void, void > RecordBase;
219 
220  SharedAllocationRecord( const SharedAllocationRecord & ) = delete;
221  SharedAllocationRecord & operator = ( const SharedAllocationRecord & ) = delete;
222 
223  static void deallocate( RecordBase * );
224 
225 #ifdef KOKKOS_DEBUG
226 
227  static RecordBase s_root_record;
228 #endif
229 
230  const Kokkos::HostSpace m_space;
231 
232 protected:
233  ~SharedAllocationRecord();
234  SharedAllocationRecord() = default;
235 
236  SharedAllocationRecord( const Kokkos::HostSpace & arg_space
237  , const std::string & arg_label
238  , const size_t arg_alloc_size
239  , const RecordBase::function_type arg_dealloc = & deallocate
240  );
241 
242 public:
243 
244  inline
245  std::string get_label() const
246  {
247  return std::string( RecordBase::head()->m_label );
248  }
249 
250  KOKKOS_INLINE_FUNCTION static
251  SharedAllocationRecord * allocate( const Kokkos::HostSpace & arg_space
252  , const std::string & arg_label
253  , const size_t arg_alloc_size
254  )
255  {
256 #if defined( KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST )
257  return new SharedAllocationRecord( arg_space, arg_label, arg_alloc_size );
258 #else
259  return (SharedAllocationRecord *) 0;
260 #endif
261  }
262 
263 
265  static
266  void * allocate_tracked( const Kokkos::HostSpace & arg_space
267  , const std::string & arg_label
268  , const size_t arg_alloc_size );
269 
271  static
272  void * reallocate_tracked( void * const arg_alloc_ptr
273  , const size_t arg_alloc_size );
274 
276  static
277  void deallocate_tracked( void * const arg_alloc_ptr );
278 
279  static SharedAllocationRecord * get_record( void * arg_alloc_ptr );
280 
281  static void print_records( std::ostream &, const Kokkos::HostSpace &, bool detail = false );
282 };
283 
284 } // namespace Impl
285 
286 } // namespace Kokkos
287 
288 //----------------------------------------------------------------------------
289 
290 namespace Kokkos {
291 
292 namespace Impl {
293 
294 template< class ExecutionSpace >
295 struct DeepCopy< HostSpace, HostSpace, ExecutionSpace > {
296  DeepCopy( void * dst, const void * src, size_t n ) {
297  memcpy( dst, src, n );
298  }
299 
300  DeepCopy( const ExecutionSpace& exec, void * dst, const void * src, size_t n ) {
301  exec.fence();
302  memcpy( dst, src, n );
303  }
304 };
305 
306 } // namespace Impl
307 
308 } // namespace Kokkos
309 
310 #endif // #define KOKKOS_HOSTSPACE_HPP
311 
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.
void * allocate(const size_t arg_alloc_size) const
Allocate untracked memory in the space.
Memory management for host memory.
static constexpr const char * name()
Return Name of the MemorySpace.
HostSpace()
Default memory space instance.
HostSpace memory_space
Tag this class as a kokkos memory space.
void deallocate(void *const arg_alloc_ptr, const size_t arg_alloc_size) const
Deallocate untracked memory in the space.
Access relationship between DstMemorySpace and SrcMemorySpace.