Kokkos Core Kernels Package  Version of the Day
Kokkos_HostSpace.hpp
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Kokkos v. 3.0
6 // Copyright (2020) National Technology & Engineering
7 // Solutions of Sandia, LLC (NTESS).
8 //
9 // Under the terms of Contract DE-NA0003525 with NTESS,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Christian R. Trott (crtrott@sandia.gov)
40 //
41 // ************************************************************************
42 //@HEADER
43 */
44 
45 #ifndef KOKKOS_HOSTSPACE_HPP
46 #define KOKKOS_HOSTSPACE_HPP
47 
48 #include <cstring>
49 #include <string>
50 #include <iosfwd>
51 #include <typeinfo>
52 
53 #include <Kokkos_Core_fwd.hpp>
54 #include <Kokkos_Concepts.hpp>
55 #include <Kokkos_MemoryTraits.hpp>
56 
57 #include <impl/Kokkos_Traits.hpp>
58 #include <impl/Kokkos_Error.hpp>
59 #include <impl/Kokkos_SharedAlloc.hpp>
60 #include <impl/Kokkos_Tools.hpp>
61 
62 #include "impl/Kokkos_HostSpace_deepcopy.hpp"
63 
64 /*--------------------------------------------------------------------------*/
65 
66 namespace Kokkos {
67 
68 namespace Impl {
69 
76 void init_lock_array_host_space();
77 
83 bool lock_address_host_space(void* ptr);
84 
91 void unlock_address_host_space(void* ptr);
92 
93 } // namespace Impl
94 
95 } // namespace Kokkos
96 
97 namespace Kokkos {
103 class HostSpace {
104  public:
107  using size_type = size_t;
108 
115 #if defined(KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_OPENMP)
116  using execution_space = Kokkos::OpenMP;
117 #elif defined(KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_THREADS)
118  using execution_space = Kokkos::Threads;
119 #elif defined(KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_HPX)
120  using execution_space = Kokkos::Experimental::HPX;
121 #elif defined(KOKKOS_ENABLE_OPENMP)
122  using execution_space = Kokkos::OpenMP;
123 #elif defined(KOKKOS_ENABLE_THREADS)
124  using execution_space = Kokkos::Threads;
125 #elif defined(KOKKOS_ENABLE_HPX)
126  using execution_space = Kokkos::Experimental::HPX;
127 #elif defined(KOKKOS_ENABLE_SERIAL)
128  using execution_space = Kokkos::Serial;
129 #else
130 #error \
131  "At least one of the following host execution spaces must be defined: Kokkos::OpenMP, Kokkos::Threads, 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."
132 #endif
133 
135  using device_type = Kokkos::Device<execution_space, memory_space>;
136 
138  HostSpace();
139  HostSpace(HostSpace&& rhs) = default;
140  HostSpace(const HostSpace& rhs) = default;
141  HostSpace& operator=(HostSpace&&) = default;
142  HostSpace& operator=(const HostSpace&) = default;
143  ~HostSpace() = default;
144 
149  STD_MALLOC,
150  POSIX_MEMALIGN,
151  POSIX_MMAP,
152  INTEL_MM_ALLOC
153  };
154 
155  explicit HostSpace(const AllocationMechanism&);
156 
158  void* allocate(const size_t arg_alloc_size) const;
159  void* allocate(const char* arg_label, const size_t arg_alloc_size,
160  const size_t arg_logical_size = 0) const;
161 
163  void deallocate(void* const arg_alloc_ptr, const size_t arg_alloc_size) const;
164  void deallocate(const char* arg_label, void* const arg_alloc_ptr,
165  const size_t arg_alloc_size,
166  const size_t arg_logical_size = 0) const;
167 
168  private:
169  template <class, class, class, class>
171 
172  void* impl_allocate(const char* arg_label, const size_t arg_alloc_size,
173  const size_t arg_logical_size = 0,
174  const Kokkos::Tools::SpaceHandle =
175  Kokkos::Tools::make_space_handle(name())) const;
176  void impl_deallocate(const char* arg_label, void* const arg_alloc_ptr,
177  const size_t arg_alloc_size,
178  const size_t arg_logical_size = 0,
179  const Kokkos::Tools::SpaceHandle =
180  Kokkos::Tools::make_space_handle(name())) const;
181 
182  public:
184  static constexpr const char* name() { return m_name; }
185 
186  private:
187  AllocationMechanism m_alloc_mech;
188  static constexpr const char* m_name = "Host";
189  friend class Kokkos::Impl::SharedAllocationRecord<Kokkos::HostSpace, void>;
190 };
191 
192 } // namespace Kokkos
193 
194 //----------------------------------------------------------------------------
195 
196 namespace Kokkos {
197 
198 namespace Impl {
199 
201  Kokkos::HostSpace>::assignable,
202  "");
203 
204 template <typename S>
205 struct HostMirror {
206  private:
207  // If input execution space can access HostSpace then keep it.
208  // Example: Kokkos::OpenMP can access, Kokkos::Cuda cannot
209  enum {
211  typename S::execution_space::memory_space,
212  Kokkos::HostSpace>::accessible
213  };
214 
215  // If HostSpace can access memory space then keep it.
216  // Example: Cannot access Kokkos::CudaSpace, can access Kokkos::CudaUVMSpace
217  enum {
218  keep_mem =
220  typename S::memory_space>::accessible
221  };
222 
223  public:
224  using Space = typename std::conditional<
225  keep_exe && keep_mem, S,
226  typename std::conditional<
227  keep_mem,
228  Kokkos::Device<Kokkos::HostSpace::execution_space,
229  typename S::memory_space>,
230  Kokkos::HostSpace>::type>::type;
231 };
232 
233 } // namespace Impl
234 
235 } // namespace Kokkos
236 
237 //----------------------------------------------------------------------------
238 
239 namespace Kokkos {
240 
241 namespace Impl {
242 
243 template <>
244 class SharedAllocationRecord<Kokkos::HostSpace, void>
245  : public SharedAllocationRecordCommon<Kokkos::HostSpace> {
246  private:
247  friend Kokkos::HostSpace;
248  friend class SharedAllocationRecordCommon<Kokkos::HostSpace>;
249 
250  using base_t = SharedAllocationRecordCommon<Kokkos::HostSpace>;
251  using RecordBase = SharedAllocationRecord<void, void>;
252 
253  SharedAllocationRecord(const SharedAllocationRecord&) = delete;
254  SharedAllocationRecord& operator=(const SharedAllocationRecord&) = delete;
255 
256 #ifdef KOKKOS_ENABLE_DEBUG
257 
258  static RecordBase s_root_record;
259 #endif
260 
261  const Kokkos::HostSpace m_space;
262 
263  protected:
264  ~SharedAllocationRecord()
265 #if defined( \
266  KOKKOS_IMPL_INTEL_WORKAROUND_NOEXCEPT_SPECIFICATION_VIRTUAL_FUNCTION)
267  noexcept
268 #endif
269  ;
270  SharedAllocationRecord() = default;
271 
272  SharedAllocationRecord(
273  const Kokkos::HostSpace& arg_space, const std::string& arg_label,
274  const size_t arg_alloc_size,
275  const RecordBase::function_type arg_dealloc = &deallocate);
276 
277  public:
278  KOKKOS_INLINE_FUNCTION static SharedAllocationRecord* allocate(
279  const Kokkos::HostSpace& arg_space, const std::string& arg_label,
280  const size_t arg_alloc_size) {
281 #if defined(KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST)
282  return new SharedAllocationRecord(arg_space, arg_label, arg_alloc_size);
283 #else
284  (void)arg_space;
285  (void)arg_label;
286  (void)arg_alloc_size;
287  return (SharedAllocationRecord*)0;
288 #endif
289  }
290 };
291 
292 } // namespace Impl
293 
294 } // namespace Kokkos
295 
296 //----------------------------------------------------------------------------
297 
298 namespace Kokkos {
299 
300 namespace Impl {
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
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.
Kokkos::Device< execution_space, memory_space > device_type
This memory space preferred device_type.
HostSpace()
Default memory space instance.
void deallocate(void *const arg_alloc_ptr, const size_t arg_alloc_size) const
Deallocate untracked memory in the space.
LogicalMemorySpace is a space that is identical to another space, but differentiable by name and temp...
Definition: dummy.cpp:3
Access relationship between DstMemorySpace and SrcMemorySpace.