Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_HBWSpace.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_HBWSPACE_HPP
45 #define KOKKOS_HBWSPACE_HPP
46 
47 #include <Kokkos_Macros.hpp>
48 #ifdef KOKKOS_ENABLE_HBWSPACE
49 
50 #include <Kokkos_HostSpace.hpp>
51 
52 namespace Kokkos {
53 
54 namespace Experimental {
55 
56 namespace Impl {
57 
64 void init_lock_array_hbw_space();
65 
71 bool lock_address_hbw_space( void* ptr );
72 
79 void unlock_address_hbw_space( void* ptr );
80 
81 } // namespace Impl
82 
83 } // namespace Experimental
84 
85 } // namespace Kokkos
86 
87 namespace Kokkos {
88 
89 namespace Experimental {
90 
96 class HBWSpace {
97 public:
99  typedef HBWSpace memory_space;
100  typedef size_t size_type;
101 
108 #if defined( KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_OPENMP )
109  typedef Kokkos::OpenMP execution_space;
110 #elif defined( KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_THREADS )
111  typedef Kokkos::Threads execution_space;
112 //#elif defined( KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_QTHREADS )
113 // typedef Kokkos::Qthreads execution_space;
114 #elif defined( KOKKOS_ENABLE_OPENMP )
115  typedef Kokkos::OpenMP execution_space;
116 #elif defined( KOKKOS_ENABLE_THREADS )
117  typedef Kokkos::Threads execution_space;
118 //#elif defined( KOKKOS_ENABLE_QTHREADS )
119 // typedef Kokkos::Qthreads execution_space;
120 #elif defined( KOKKOS_ENABLE_SERIAL )
121  typedef Kokkos::Serial execution_space;
122 #else
123 # error "At least one of the following host execution spaces must be defined: Kokkos::OpenMP, Kokkos::Threads, Kokkos::Qhreads, 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."
124 #endif
125 
127  typedef Kokkos::Device< execution_space, memory_space > device_type;
128 
130  HBWSpace();
131  HBWSpace( const HBWSpace & rhs ) = default;
132  HBWSpace & operator = ( const HBWSpace & ) = default;
133  ~HBWSpace() = default;
134 
137  enum AllocationMechanism { STD_MALLOC, POSIX_MEMALIGN, POSIX_MMAP, INTEL_MM_ALLOC };
138 
139  explicit
140  HBWSpace( const AllocationMechanism & );
141 
143  void * allocate( const size_t arg_alloc_size ) const;
144 
146  void deallocate( void * const arg_alloc_ptr
147  , const size_t arg_alloc_size ) const;
148 
150  static constexpr const char* name() { return "HBW"; }
151 
152 private:
153 
154  AllocationMechanism m_alloc_mech;
155  friend class Kokkos::Impl::SharedAllocationRecord< Kokkos::Experimental::HBWSpace, void >;
156 };
157 
158 } // namespace Experimental
159 
160 } // namespace Kokkos
161 
162 //----------------------------------------------------------------------------
163 
164 namespace Kokkos {
165 
166 namespace Impl {
167 
168 template<>
169 class SharedAllocationRecord< Kokkos::Experimental::HBWSpace, void >
170  : public SharedAllocationRecord< void, void >
171 {
172 private:
173 
174  friend Kokkos::Experimental::HBWSpace;
175 
176  typedef SharedAllocationRecord< void, void > RecordBase;
177 
178  SharedAllocationRecord( const SharedAllocationRecord & ) = delete;
179  SharedAllocationRecord & operator = ( const SharedAllocationRecord & ) = delete;
180 
181  static void deallocate( RecordBase * );
182 
183 #ifdef KOKKOS_DEBUG
184 
185  static RecordBase s_root_record;
186 #endif
187 
188  const Kokkos::Experimental::HBWSpace m_space;
189 
190 protected:
191 
192  ~SharedAllocationRecord();
193  SharedAllocationRecord() = default;
194 
195  SharedAllocationRecord( const Kokkos::Experimental::HBWSpace & arg_space
196  , const std::string & arg_label
197  , const size_t arg_alloc_size
198  , const RecordBase::function_type arg_dealloc = & deallocate
199  );
200 
201 public:
202 
203  inline
204  std::string get_label() const
205  {
206  return std::string( RecordBase::head()->m_label );
207  }
208 
209  KOKKOS_INLINE_FUNCTION static
210  SharedAllocationRecord * allocate( const Kokkos::Experimental::HBWSpace & arg_space
211  , const std::string & arg_label
212  , const size_t arg_alloc_size
213  )
214  {
215 #if defined( KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST )
216  return new SharedAllocationRecord( arg_space, arg_label, arg_alloc_size );
217 #else
218  return (SharedAllocationRecord *) 0;
219 #endif
220  }
221 
223  static
224  void * allocate_tracked( const Kokkos::Experimental::HBWSpace & arg_space
225  , const std::string & arg_label
226  , const size_t arg_alloc_size );
227 
229  static
230  void * reallocate_tracked( void * const arg_alloc_ptr
231  , const size_t arg_alloc_size );
232 
234  static
235  void deallocate_tracked( void * const arg_alloc_ptr );
236 
237  static SharedAllocationRecord * get_record( void * arg_alloc_ptr );
238 
239  static void print_records( std::ostream &, const Kokkos::Experimental::HBWSpace &, bool detail = false );
240 };
241 
242 } // namespace Impl
243 
244 } // namespace Kokkos
245 
246 //----------------------------------------------------------------------------
247 
248 namespace Kokkos {
249 
250 namespace Impl {
251 
253 
254 template<>
255 struct MemorySpaceAccess< Kokkos::HostSpace, Kokkos::Experimental::HBWSpace > {
256  enum { assignable = true };
257  enum { accessible = true };
258  enum { deepcopy = true };
259 };
260 
261 template<>
262 struct MemorySpaceAccess< Kokkos::Experimental::HBWSpace, Kokkos::HostSpace > {
263  enum { assignable = false };
264  enum { accessible = true };
265  enum { deepcopy = true };
266 };
267 
268 } // namespace Impl
269 
270 } // namespace Kokkos
271 
272 //----------------------------------------------------------------------------
273 
274 namespace Kokkos {
275 
276 namespace Impl {
277 
278 template< class ExecutionSpace >
279 struct DeepCopy< Experimental::HBWSpace, Experimental::HBWSpace, ExecutionSpace > {
280  DeepCopy( void * dst, const void * src, size_t n ) {
281  memcpy( dst, src, n );
282  }
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 template< class ExecutionSpace >
291 struct DeepCopy< HostSpace, Experimental::HBWSpace, ExecutionSpace > {
292  DeepCopy( void * dst, const void * src, size_t n ) {
293  memcpy( dst, src, n );
294  }
295 
296  DeepCopy( const ExecutionSpace& exec, void * dst, const void * src, size_t n ) {
297  exec.fence();
298  memcpy( dst, src, n );
299  }
300 };
301 
302 template< class ExecutionSpace >
303 struct DeepCopy< Experimental::HBWSpace, 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 namespace Kokkos {
319 
320 namespace Impl {
321 
322 template<>
323 struct VerifyExecutionCanAccessMemorySpace< Kokkos::HostSpace, Kokkos::Experimental::HBWSpace >
324 {
325  enum { value = true };
326  inline static void verify( void ) { }
327  inline static void verify( const void * ) { }
328 };
329 
330 template<>
331 struct VerifyExecutionCanAccessMemorySpace< Kokkos::Experimental::HBWSpace, Kokkos::HostSpace >
332 {
333  enum { value = true };
334  inline static void verify( void ) { }
335  inline static void verify( const void * ) { }
336 };
337 
338 } // namespace Impl
339 
340 } // namespace Kokkos
341 
342 #endif
343 #endif // #define KOKKOS_HBWSPACE_HPP
344 
Memory management for host memory.
Access relationship between DstMemorySpace and SrcMemorySpace.