Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 #include "impl/Kokkos_HostSpace_deepcopy.hpp"
61 
62 /*--------------------------------------------------------------------------*/
63 
64 namespace Kokkos {
65 
66 namespace Impl {
67 
74 void init_lock_array_host_space();
75 
81 bool lock_address_host_space(void* ptr);
82 
89 void unlock_address_host_space( void* ptr );
90 
91 } // namespace Impl
92 
93 } // namespace Kokkos
94 
95 namespace Kokkos {
96 
102 class HostSpace {
103 public:
106  typedef size_t size_type;
107 
114 #if defined( KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_OPENMP )
115  typedef Kokkos::OpenMP execution_space;
116 #elif defined( KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_THREADS )
117  typedef Kokkos::Threads execution_space;
118 #elif defined( KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_HPX )
119  typedef Kokkos::Experimental::HPX execution_space;
120 //#elif defined( KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_QTHREADS )
121 // typedef Kokkos::Qthreads execution_space;
122 #elif defined( KOKKOS_ENABLE_OPENMP )
123  typedef Kokkos::OpenMP execution_space;
124 #elif defined( KOKKOS_ENABLE_THREADS )
125  typedef Kokkos::Threads execution_space;
126 //#elif defined( KOKKOS_ENABLE_QTHREADS )
127 // typedef Kokkos::Qthreads execution_space;
128 #elif defined( KOKKOS_ENABLE_HPX )
129  typedef Kokkos::Experimental::HPX execution_space;
130 #elif defined( KOKKOS_ENABLE_SERIAL )
131  typedef Kokkos::Serial execution_space;
132 #else
133 # 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."
134 #endif
135 
137  typedef Kokkos::Device< execution_space, memory_space > device_type;
138 
140  HostSpace();
141  HostSpace( HostSpace && rhs ) = default;
142  HostSpace( const HostSpace & rhs ) = default;
143  HostSpace & operator = ( HostSpace && ) = default;
144  HostSpace & operator = ( const HostSpace & ) = default;
145  ~HostSpace() = default;
146 
149  enum AllocationMechanism { STD_MALLOC, POSIX_MEMALIGN, POSIX_MMAP, INTEL_MM_ALLOC };
150 
151  explicit
152  HostSpace( const AllocationMechanism & );
153 
155  void * allocate( const size_t arg_alloc_size ) const;
156 
158  void deallocate( void * const arg_alloc_ptr
159  , const size_t arg_alloc_size ) const;
160 
162  static constexpr const char* name() { return m_name; }
163 
164 private:
165  AllocationMechanism m_alloc_mech;
166  static constexpr const char* m_name = "Host";
167  friend class Kokkos::Impl::SharedAllocationRecord< Kokkos::HostSpace, void >;
168 };
169 
170 } // namespace Kokkos
171 
172 //----------------------------------------------------------------------------
173 
174 namespace Kokkos {
175 
176 namespace Impl {
177 
179 
180 template< typename S >
181 struct HostMirror {
182 private:
183  // If input execution space can access HostSpace then keep it.
184  // Example: Kokkos::OpenMP can access, Kokkos::Cuda cannot
185  enum { keep_exe = Kokkos::Impl::MemorySpaceAccess
186  < typename S::execution_space::memory_space, Kokkos::HostSpace >::accessible };
187 
188  // If HostSpace can access memory space then keep it.
189  // Example: Cannot access Kokkos::CudaSpace, can access Kokkos::CudaUVMSpace
190  enum { keep_mem = Kokkos::Impl::MemorySpaceAccess
191  < Kokkos::HostSpace, typename S::memory_space >::accessible };
192 
193 public:
194 
195  typedef typename std::conditional
196  < keep_exe && keep_mem /* Can keep whole space */
197  , S
198  , typename std::conditional
199  < keep_mem /* Can keep memory space, use default Host execution space */
200  , Kokkos::Device< Kokkos::HostSpace::execution_space
201  , typename S::memory_space >
203  >::type
204  >::type Space;
205 };
206 
207 } // namespace Impl
208 
209 } // namespace Kokkos
210 
211 //----------------------------------------------------------------------------
212 
213 namespace Kokkos {
214 
215 namespace Impl {
216 
217 template<>
218 class SharedAllocationRecord< Kokkos::HostSpace, void >
219  : public SharedAllocationRecord< void, void >
220 {
221 private:
222  friend Kokkos::HostSpace;
223 
224  typedef SharedAllocationRecord< void, void > RecordBase;
225 
226  SharedAllocationRecord( const SharedAllocationRecord & ) = delete;
227  SharedAllocationRecord & operator = ( const SharedAllocationRecord & ) = delete;
228 
229  static void deallocate( RecordBase * );
230 
231 #ifdef KOKKOS_DEBUG
232 
233  static RecordBase s_root_record;
234 #endif
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 #define PAR_DEEP_COPY_USE_MEMCPY
301 
302 template< class ExecutionSpace >
303 struct DeepCopy< HostSpace, HostSpace, ExecutionSpace > {
304  DeepCopy( void * dst, const void * src, size_t n ) {
305  hostspace_parallel_deepcopy(dst,src,n);
306  }
307 
308  DeepCopy( const ExecutionSpace& exec, void * dst, const void * src, size_t n ) {
309  exec.fence();
310  hostspace_parallel_deepcopy(dst,src,n);
311  exec.fence();
312  }
313 };
314 
315 } // namespace Impl
316 
317 } // namespace Kokkos
318 
319 #endif // #define KOKKOS_HOSTSPACE_HPP
320 
void * allocate(const size_t arg_alloc_size) const
Allocate untracked memory in the space.
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 deallocate(void *const arg_alloc_ptr, const size_t arg_alloc_size) const
Deallocate 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.
Access relationship between DstMemorySpace and SrcMemorySpace.