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_MemoryTraits.hpp>
54 
55 #include <impl/Kokkos_Traits.hpp>
56 #include <impl/Kokkos_Error.hpp>
57 
58 #include <impl/Kokkos_AllocationTracker.hpp>
59 #include <impl/Kokkos_BasicAllocators.hpp>
60 
61 #include <impl/KokkosExp_SharedAlloc.hpp>
62 
63 /*--------------------------------------------------------------------------*/
64 
65 namespace Kokkos {
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 } // namespace Kokkos
93 
94 namespace Kokkos {
95 
101 class HostSpace {
102 public:
103 
106  typedef size_t size_type ;
107 
114 #if defined( KOKKOS_HAVE_DEFAULT_DEVICE_TYPE_OPENMP )
115  typedef Kokkos::OpenMP execution_space ;
116 #elif defined( KOKKOS_HAVE_DEFAULT_DEVICE_TYPE_THREADS )
117  typedef Kokkos::Threads execution_space ;
118 #elif defined( KOKKOS_HAVE_OPENMP )
119  typedef Kokkos::OpenMP execution_space ;
120 #elif defined( KOKKOS_HAVE_PTHREAD )
121  typedef Kokkos::Threads execution_space ;
122 #elif defined( KOKKOS_HAVE_SERIAL )
123  typedef Kokkos::Serial execution_space ;
124 #else
125 # error "At least one of the following host execution spaces must be defined: Kokkos::OpenMP, Kokkos::Serial, or Kokkos::Threads. 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."
126 #endif
127 
130 
131  /*--------------------------------*/
132 #if ! defined( KOKKOS_USING_EXPERIMENTAL_VIEW )
133 
134 #if defined( KOKKOS_USE_PAGE_ALIGNED_HOST_MEMORY )
135  typedef Impl::PageAlignedAllocator allocator ;
136 #else
137  typedef Impl::AlignedAllocator allocator ;
138 #endif
139 
146  static Impl::AllocationTracker allocate_and_track( const std::string & label, const size_t size );
147 
148 #endif /* #if ! defined( KOKKOS_USING_EXPERIMENTAL_VIEW ) */
149 
150  /*--------------------------------*/
151  /* Functions unique to the HostSpace */
152  static int in_parallel();
153 
154  static void register_in_parallel( int (*)() );
155 
156  /*--------------------------------*/
157 
159  HostSpace();
160  HostSpace( const HostSpace & rhs ) = default ;
161  HostSpace & operator = ( const HostSpace & ) = default ;
162  ~HostSpace() = default ;
163 
166  enum AllocationMechanism { STD_MALLOC , POSIX_MEMALIGN , POSIX_MMAP , INTEL_MM_ALLOC };
167 
168  explicit
169  HostSpace( const AllocationMechanism & );
170 
172  void * allocate( const size_t arg_alloc_size ) const ;
173 
175  void deallocate( void * const arg_alloc_ptr
176  , const size_t arg_alloc_size ) const ;
177 
178 private:
179 
180  AllocationMechanism m_alloc_mech ;
181 
182  friend class Kokkos::Experimental::Impl::SharedAllocationRecord< Kokkos::HostSpace , void > ;
183 };
184 
185 } // namespace Kokkos
186 
187 //----------------------------------------------------------------------------
188 //----------------------------------------------------------------------------
189 
190 namespace Kokkos {
191 namespace Experimental {
192 namespace Impl {
193 
194 template<>
195 class SharedAllocationRecord< Kokkos::HostSpace , void >
196  : public SharedAllocationRecord< void , void >
197 {
198 private:
199 
200  friend Kokkos::HostSpace ;
201 
202  typedef SharedAllocationRecord< void , void > RecordBase ;
203 
204  SharedAllocationRecord( const SharedAllocationRecord & ) = delete ;
205  SharedAllocationRecord & operator = ( const SharedAllocationRecord & ) = delete ;
206 
207  static void deallocate( RecordBase * );
208 
210  static RecordBase s_root_record ;
211 
212  const Kokkos::HostSpace m_space ;
213 
214 protected:
215 
216  ~SharedAllocationRecord();
217  SharedAllocationRecord() = default ;
218 
219  SharedAllocationRecord( const Kokkos::HostSpace & arg_space
220  , const std::string & arg_label
221  , const size_t arg_alloc_size
222  , const RecordBase::function_type arg_dealloc = & deallocate
223  );
224 
225 public:
226 
227  inline
228  std::string get_label() const
229  {
230  return std::string( RecordBase::head()->m_label );
231  }
232 
233  KOKKOS_INLINE_FUNCTION static
234  SharedAllocationRecord * allocate( const Kokkos::HostSpace & arg_space
235  , const std::string & arg_label
236  , const size_t arg_alloc_size
237  )
238  {
239 #if defined( KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST )
240  return new SharedAllocationRecord( arg_space , arg_label , arg_alloc_size );
241 #else
242  return (SharedAllocationRecord *) 0 ;
243 #endif
244  }
245 
247  static
248  void * allocate_tracked( const Kokkos::HostSpace & arg_space
249  , const std::string & arg_label
250  , const size_t arg_alloc_size );
251 
253  static
254  void * reallocate_tracked( void * const arg_alloc_ptr
255  , const size_t arg_alloc_size );
256 
258  static
259  void deallocate_tracked( void * const arg_alloc_ptr );
260 
261 
262  static SharedAllocationRecord * get_record( void * arg_alloc_ptr );
263 
264  static void print_records( std::ostream & , const Kokkos::HostSpace & , bool detail = false );
265 };
266 
267 } // namespace Impl
268 } // namespace Experimental
269 } // namespace Kokkos
270 
271 //----------------------------------------------------------------------------
272 //----------------------------------------------------------------------------
273 
274 namespace Kokkos {
275 namespace Impl {
276 
277 template< class DstSpace, class SrcSpace, class ExecutionSpace = typename DstSpace::execution_space> struct DeepCopy ;
278 
279 template<class ExecutionSpace>
280 struct DeepCopy<HostSpace,HostSpace,ExecutionSpace> {
281  DeepCopy( void * dst , const void * src , size_t n ) {
282  memcpy( dst , src , n );
283  }
284  DeepCopy( const ExecutionSpace& exec, void * dst , const void * src , size_t n ) {
285  exec.fence();
286  memcpy( dst , src , n );
287  }
288 };
289 
290 } // namespace Impl
291 } // namespace Kokkos
292 
293 
294 #endif /* #define KOKKOS_HOSTSPACE_HPP */
295 
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.
Kokkos::Device< execution_space, memory_space > device_type
This memory space preferred device_type.
HostSpace memory_space
Tag this class as a kokkos memory space.