Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_HostSpace.hpp
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16 
17 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
18 #include <Kokkos_Macros.hpp>
19 static_assert(false,
20  "Including non-public Kokkos header files is not allowed.");
21 #endif
22 #ifndef KOKKOS_HOSTSPACE_HPP
23 #define KOKKOS_HOSTSPACE_HPP
24 
25 #include <cstring>
26 #include <string>
27 #include <iosfwd>
28 #include <typeinfo>
29 
30 #include <Kokkos_Core_fwd.hpp>
31 #include <Kokkos_Concepts.hpp>
32 #include <Kokkos_MemoryTraits.hpp>
33 
34 #include <impl/Kokkos_Traits.hpp>
35 #include <impl/Kokkos_Error.hpp>
36 #include <impl/Kokkos_SharedAlloc.hpp>
37 #include <impl/Kokkos_Tools.hpp>
38 
39 #include "impl/Kokkos_HostSpace_deepcopy.hpp"
40 
41 /*--------------------------------------------------------------------------*/
42 
43 namespace Kokkos {
49 class HostSpace {
50  public:
53  using size_type = size_t;
54 
61  using execution_space = DefaultHostExecutionSpace;
62 
64  using device_type = Kokkos::Device<execution_space, memory_space>;
65 
66  HostSpace() = default;
67  HostSpace(HostSpace&& rhs) = default;
68  HostSpace(const HostSpace& rhs) = default;
69  HostSpace& operator=(HostSpace&&) = default;
70  HostSpace& operator=(const HostSpace&) = default;
71  ~HostSpace() = default;
72 
73 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
74 
77 #if defined(KOKKOS_COMPILER_GNU) && KOKKOS_COMPILER_GNU < 1100
78  // We see deprecation warnings even when not using the deprecated
79  // HostSpace constructor below when using gcc before release 11.
80  enum
81 #else
82  enum KOKKOS_DEPRECATED
83 #endif
84  AllocationMechanism {
85  STD_MALLOC,
86  POSIX_MEMALIGN,
87  POSIX_MMAP,
88  INTEL_MM_ALLOC
89  };
90 
91  KOKKOS_DEPRECATED
92  explicit HostSpace(const AllocationMechanism&);
93 #endif
94 
96  template <typename ExecutionSpace>
97  void* allocate(const ExecutionSpace&, const size_t arg_alloc_size) const {
98  return allocate(arg_alloc_size);
99  }
100  template <typename ExecutionSpace>
101  void* allocate(const ExecutionSpace&, const char* arg_label,
102  const size_t arg_alloc_size,
103  const size_t arg_logical_size = 0) const {
104  return allocate(arg_label, arg_alloc_size, arg_logical_size);
105  }
106  void* allocate(const size_t arg_alloc_size) const;
107  void* allocate(const char* arg_label, const size_t arg_alloc_size,
108  const size_t arg_logical_size = 0) const;
109 
111  void deallocate(void* const arg_alloc_ptr, const size_t arg_alloc_size) const;
112  void deallocate(const char* arg_label, void* const arg_alloc_ptr,
113  const size_t arg_alloc_size,
114  const size_t arg_logical_size = 0) const;
115 
116  void* impl_allocate(const char* arg_label, const size_t arg_alloc_size,
117  const size_t arg_logical_size = 0,
118  const Kokkos::Tools::SpaceHandle =
119  Kokkos::Tools::make_space_handle(name())) const;
120  void impl_deallocate(const char* arg_label, void* const arg_alloc_ptr,
121  const size_t arg_alloc_size,
122  const size_t arg_logical_size = 0,
123  const Kokkos::Tools::SpaceHandle =
124  Kokkos::Tools::make_space_handle(name())) const;
125 
127  static constexpr const char* name() { return m_name; }
128 
129  private:
130  static constexpr const char* m_name = "Host";
131 };
132 
133 } // namespace Kokkos
134 
135 //----------------------------------------------------------------------------
136 
137 namespace Kokkos {
138 
139 namespace Impl {
140 
142  Kokkos::HostSpace>::assignable);
143 
144 template <typename S>
145 struct HostMirror {
146  private:
147  // If input execution space can access HostSpace then keep it.
148  // Example: Kokkos::OpenMP can access, Kokkos::Cuda cannot
149  enum {
151  typename S::execution_space::memory_space,
152  Kokkos::HostSpace>::accessible
153  };
154 
155  // If HostSpace can access memory space then keep it.
156  // Example: Cannot access Kokkos::CudaSpace, can access Kokkos::CudaUVMSpace
157  enum {
158  keep_mem =
160  typename S::memory_space>::accessible
161  };
162 
163  public:
164  using Space = std::conditional_t<
165  keep_exe && keep_mem, S,
166  std::conditional_t<keep_mem,
167  Kokkos::Device<Kokkos::HostSpace::execution_space,
168  typename S::memory_space>,
170 };
171 
172 } // namespace Impl
173 
174 } // namespace Kokkos
175 
176 //----------------------------------------------------------------------------
177 
178 KOKKOS_IMPL_SHARED_ALLOCATION_SPECIALIZATION(Kokkos::HostSpace);
179 
180 //----------------------------------------------------------------------------
181 
182 namespace Kokkos {
183 
184 namespace Impl {
185 
186 template <>
187 struct DeepCopy<HostSpace, HostSpace, DefaultHostExecutionSpace> {
188  DeepCopy(void* dst, const void* src, size_t n) {
189  hostspace_parallel_deepcopy(dst, src, n);
190  }
191 
192  DeepCopy(const DefaultHostExecutionSpace& exec, void* dst, const void* src,
193  size_t n) {
194  hostspace_parallel_deepcopy_async(exec, dst, src, n);
195  }
196 };
197 
198 template <class ExecutionSpace>
199 struct DeepCopy<HostSpace, HostSpace, ExecutionSpace> {
200  DeepCopy(void* dst, const void* src, size_t n) {
201  hostspace_parallel_deepcopy(dst, src, n);
202  }
203 
204  DeepCopy(const ExecutionSpace& exec, void* dst, const void* src, size_t n) {
205  exec.fence(
206  "Kokkos::Impl::DeepCopy<HostSpace, HostSpace, "
207  "ExecutionSpace>::DeepCopy: fence before copy");
208  hostspace_parallel_deepcopy_async(dst, src, n);
209  }
210 };
211 
212 } // namespace Impl
213 
214 } // namespace Kokkos
215 
216 #endif // #define KOKKOS_HOSTSPACE_HPP
void * allocate(const ExecutionSpace &, const size_t arg_alloc_size) const
Allocate untracked memory in the space.
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.
Kokkos::Device< execution_space, memory_space > device_type
This memory space preferred device_type.
DefaultHostExecutionSpace execution_space
Default execution space for this memory space.
Access relationship between DstMemorySpace and SrcMemorySpace.