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  private:
117  void* impl_allocate(const char* arg_label, const size_t arg_alloc_size,
118  const size_t arg_logical_size = 0,
119  const Kokkos::Tools::SpaceHandle =
120  Kokkos::Tools::make_space_handle(name())) const;
121  void impl_deallocate(const char* arg_label, void* const arg_alloc_ptr,
122  const size_t arg_alloc_size,
123  const size_t arg_logical_size = 0,
124  const Kokkos::Tools::SpaceHandle =
125  Kokkos::Tools::make_space_handle(name())) const;
126 
127  public:
129  static constexpr const char* name() { return m_name; }
130 
131  private:
132  static constexpr const char* m_name = "Host";
133 };
134 
135 } // namespace Kokkos
136 
137 //----------------------------------------------------------------------------
138 
139 namespace Kokkos {
140 
141 namespace Impl {
142 
144  Kokkos::HostSpace>::assignable);
145 
146 template <typename S>
147 struct HostMirror {
148  private:
149  // If input execution space can access HostSpace then keep it.
150  // Example: Kokkos::OpenMP can access, Kokkos::Cuda cannot
151  enum {
153  typename S::execution_space::memory_space,
154  Kokkos::HostSpace>::accessible
155  };
156 
157  // If HostSpace can access memory space then keep it.
158  // Example: Cannot access Kokkos::CudaSpace, can access Kokkos::CudaUVMSpace
159  enum {
160  keep_mem =
162  typename S::memory_space>::accessible
163  };
164 
165  public:
166  using Space = std::conditional_t<
167  keep_exe && keep_mem, S,
168  std::conditional_t<keep_mem,
169  Kokkos::Device<Kokkos::HostSpace::execution_space,
170  typename S::memory_space>,
172 };
173 
174 } // namespace Impl
175 
176 } // namespace Kokkos
177 
178 //----------------------------------------------------------------------------
179 
180 KOKKOS_IMPL_SHARED_ALLOCATION_SPECIALIZATION(Kokkos::HostSpace);
181 
182 //----------------------------------------------------------------------------
183 
184 namespace Kokkos {
185 
186 namespace Impl {
187 
188 template <>
189 struct DeepCopy<HostSpace, HostSpace, DefaultHostExecutionSpace> {
190  DeepCopy(void* dst, const void* src, size_t n) {
191  hostspace_parallel_deepcopy(dst, src, n);
192  }
193 
194  DeepCopy(const DefaultHostExecutionSpace& exec, void* dst, const void* src,
195  size_t n) {
196  hostspace_parallel_deepcopy_async(exec, dst, src, n);
197  }
198 };
199 
200 template <class ExecutionSpace>
201 struct DeepCopy<HostSpace, HostSpace, ExecutionSpace> {
202  DeepCopy(void* dst, const void* src, size_t n) {
203  hostspace_parallel_deepcopy(dst, src, n);
204  }
205 
206  DeepCopy(const ExecutionSpace& exec, void* dst, const void* src, size_t n) {
207  exec.fence(
208  "Kokkos::Impl::DeepCopy<HostSpace, HostSpace, "
209  "ExecutionSpace>::DeepCopy: fence before copy");
210  hostspace_parallel_deepcopy_async(dst, src, n);
211  }
212 };
213 
214 } // namespace Impl
215 
216 } // namespace Kokkos
217 
218 #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.