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_QTHREADS )
119 // typedef Kokkos::Qthreads execution_space;
120 #elif defined( KOKKOS_ENABLE_OPENMP )
121  typedef Kokkos::OpenMP execution_space;
122 #elif defined( KOKKOS_ENABLE_THREADS )
123  typedef Kokkos::Threads execution_space;
124 //#elif defined( KOKKOS_ENABLE_QTHREADS )
125 // typedef Kokkos::Qthreads execution_space;
126 #elif defined( KOKKOS_ENABLE_SERIAL )
127  typedef Kokkos::Serial execution_space;
128 #else
129 # 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."
130 #endif
131 
133  typedef Kokkos::Device< execution_space, memory_space > device_type;
134 
136  HostSpace();
137  HostSpace( HostSpace && rhs ) = default;
138  HostSpace( const HostSpace & rhs ) = default;
139  HostSpace & operator = ( HostSpace && ) = default;
140  HostSpace & operator = ( const HostSpace & ) = default;
141  ~HostSpace() = default;
142 
145  enum AllocationMechanism { STD_MALLOC, POSIX_MEMALIGN, POSIX_MMAP, INTEL_MM_ALLOC };
146 
147  explicit
148  HostSpace( const AllocationMechanism & );
149 
151  void * allocate( const size_t arg_alloc_size ) const;
152 
154  void deallocate( void * const arg_alloc_ptr
155  , const size_t arg_alloc_size ) const;
156 
158  static constexpr const char* name() { return m_name; }
159 
160 private:
161  AllocationMechanism m_alloc_mech;
162  static constexpr const char* m_name = "Host";
163  friend class Kokkos::Impl::SharedAllocationRecord< Kokkos::HostSpace, void >;
164 };
165 
166 } // namespace Kokkos
167 
168 //----------------------------------------------------------------------------
169 
170 namespace Kokkos {
171 
172 namespace Impl {
173 
175 
176 template< typename S >
177 struct HostMirror {
178 private:
179  // If input execution space can access HostSpace then keep it.
180  // Example: Kokkos::OpenMP can access, Kokkos::Cuda cannot
181  enum { keep_exe = Kokkos::Impl::MemorySpaceAccess
182  < typename S::execution_space::memory_space, Kokkos::HostSpace >::accessible };
183 
184  // If HostSpace can access memory space then keep it.
185  // Example: Cannot access Kokkos::CudaSpace, can access Kokkos::CudaUVMSpace
186  enum { keep_mem = Kokkos::Impl::MemorySpaceAccess
187  < Kokkos::HostSpace, typename S::memory_space >::accessible };
188 
189 public:
190 
191  typedef typename std::conditional
192  < keep_exe && keep_mem /* Can keep whole space */
193  , S
194  , typename std::conditional
195  < keep_mem /* Can keep memory space, use default Host execution space */
196  , Kokkos::Device< Kokkos::HostSpace::execution_space
197  , typename S::memory_space >
199  >::type
200  >::type Space;
201 };
202 
203 } // namespace Impl
204 
205 } // namespace Kokkos
206 
207 //----------------------------------------------------------------------------
208 
209 namespace Kokkos {
210 
211 namespace Impl {
212 
213 template<>
214 class SharedAllocationRecord< Kokkos::HostSpace, void >
215  : public SharedAllocationRecord< void, void >
216 {
217 private:
218  friend Kokkos::HostSpace;
219 
220  typedef SharedAllocationRecord< void, void > RecordBase;
221 
222  SharedAllocationRecord( const SharedAllocationRecord & ) = delete;
223  SharedAllocationRecord & operator = ( const SharedAllocationRecord & ) = delete;
224 
225  static void deallocate( RecordBase * );
226 
227 #ifdef KOKKOS_DEBUG
228 
229  static RecordBase s_root_record;
230 #endif
231 
232  const Kokkos::HostSpace m_space;
233 
234 protected:
235  ~SharedAllocationRecord();
236  SharedAllocationRecord() = default;
237 
238  SharedAllocationRecord( const Kokkos::HostSpace & arg_space
239  , const std::string & arg_label
240  , const size_t arg_alloc_size
241  , const RecordBase::function_type arg_dealloc = & deallocate
242  );
243 
244 public:
245 
246  inline
247  std::string get_label() const
248  {
249  return std::string( RecordBase::head()->m_label );
250  }
251 
252  KOKKOS_INLINE_FUNCTION static
253  SharedAllocationRecord * allocate( const Kokkos::HostSpace & arg_space
254  , const std::string & arg_label
255  , const size_t arg_alloc_size
256  )
257  {
258 #if defined( KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST )
259  return new SharedAllocationRecord( arg_space, arg_label, arg_alloc_size );
260 #else
261  return (SharedAllocationRecord *) 0;
262 #endif
263  }
264 
265 
267  static
268  void * allocate_tracked( const Kokkos::HostSpace & arg_space
269  , const std::string & arg_label
270  , const size_t arg_alloc_size );
271 
273  static
274  void * reallocate_tracked( void * const arg_alloc_ptr
275  , const size_t arg_alloc_size );
276 
278  static
279  void deallocate_tracked( void * const arg_alloc_ptr );
280 
281  static SharedAllocationRecord * get_record( void * arg_alloc_ptr );
282 
283  static void print_records( std::ostream &, const Kokkos::HostSpace &, bool detail = false );
284 };
285 
286 } // namespace Impl
287 
288 } // namespace Kokkos
289 
290 //----------------------------------------------------------------------------
291 
292 namespace Kokkos {
293 
294 namespace Impl {
295 
296 #define PAR_DEEP_COPY_USE_MEMCPY
297 
298 template< class ExecutionSpace >
299 struct DeepCopy< HostSpace, HostSpace, ExecutionSpace > {
300  DeepCopy( void * dst, const void * src, size_t n ) {
301  hostspace_parallel_deepcopy(dst,src,n);
302  }
303 
304  DeepCopy( const ExecutionSpace& exec, void * dst, const void * src, size_t n ) {
305  exec.fence();
306  hostspace_parallel_deepcopy(dst,src,n);
307  }
308 };
309 
310 } // namespace Impl
311 
312 } // namespace Kokkos
313 
314 #endif // #define KOKKOS_HOSTSPACE_HPP
315 
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.