Kokkos Core Kernels Package Version of the Day
Loading...
Searching...
No Matches
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>
19static_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#include <impl/Kokkos_MemorySpace.hpp>
41
42/*--------------------------------------------------------------------------*/
43
44namespace Kokkos {
50class HostSpace {
51 public:
54 using size_type = size_t;
55
62 using execution_space = DefaultHostExecutionSpace;
63
65 using device_type = Kokkos::Device<execution_space, memory_space>;
66
69 HostSpace(HostSpace&& rhs) = default;
70 HostSpace(const HostSpace& rhs) = default;
71 HostSpace& operator=(HostSpace&&) = default;
72 HostSpace& operator=(const HostSpace&) = default;
73 ~HostSpace() = default;
74
77
79 STD_MALLOC,
80 POSIX_MEMALIGN,
81 POSIX_MMAP,
82 INTEL_MM_ALLOC
83 };
84
85 explicit HostSpace(const AllocationMechanism&);
86
88 void* allocate(const size_t arg_alloc_size) const;
89 void* allocate(const char* arg_label, const size_t arg_alloc_size,
90 const size_t arg_logical_size = 0) const;
91
93 void deallocate(void* const arg_alloc_ptr, const size_t arg_alloc_size) const;
94 void deallocate(const char* arg_label, void* const arg_alloc_ptr,
95 const size_t arg_alloc_size,
96 const size_t arg_logical_size = 0) const;
97
98 private:
99 template <class, class, class, class>
101
102 void* impl_allocate(const char* arg_label, const size_t arg_alloc_size,
103 const size_t arg_logical_size = 0,
104 const Kokkos::Tools::SpaceHandle =
105 Kokkos::Tools::make_space_handle(name())) const;
106 void impl_deallocate(const char* arg_label, void* const arg_alloc_ptr,
107 const size_t arg_alloc_size,
108 const size_t arg_logical_size = 0,
109 const Kokkos::Tools::SpaceHandle =
110 Kokkos::Tools::make_space_handle(name())) const;
111
112 public:
114 static constexpr const char* name() { return m_name; }
115
116 private:
117 AllocationMechanism m_alloc_mech;
118 static constexpr const char* m_name = "Host";
119 friend class Kokkos::Impl::SharedAllocationRecord<Kokkos::HostSpace, void>;
120};
121
122} // namespace Kokkos
123
124//----------------------------------------------------------------------------
125
126namespace Kokkos {
127
128namespace Impl {
129
131 Kokkos::HostSpace>::assignable,
132 "");
133
134template <typename S>
135struct HostMirror {
136 private:
137 // If input execution space can access HostSpace then keep it.
138 // Example: Kokkos::OpenMP can access, Kokkos::Cuda cannot
139 enum {
141 typename S::execution_space::memory_space,
142 Kokkos::HostSpace>::accessible
143 };
144
145 // If HostSpace can access memory space then keep it.
146 // Example: Cannot access Kokkos::CudaSpace, can access Kokkos::CudaUVMSpace
147 enum {
148 keep_mem =
150 typename S::memory_space>::accessible
151 };
152
153 public:
154 using Space = std::conditional_t<
155 keep_exe && keep_mem, S,
156 std::conditional_t<keep_mem,
158 typename S::memory_space>,
159 Kokkos::HostSpace>>;
160};
161
162} // namespace Impl
163
164} // namespace Kokkos
165
166//----------------------------------------------------------------------------
167
168namespace Kokkos {
169
170namespace Impl {
171
172template <>
173class SharedAllocationRecord<Kokkos::HostSpace, void>
174 : public SharedAllocationRecordCommon<Kokkos::HostSpace> {
175 private:
176 friend Kokkos::HostSpace;
177 friend class SharedAllocationRecordCommon<Kokkos::HostSpace>;
178
179 using base_t = SharedAllocationRecordCommon<Kokkos::HostSpace>;
180 using RecordBase = SharedAllocationRecord<void, void>;
181
182 SharedAllocationRecord(const SharedAllocationRecord&) = delete;
183 SharedAllocationRecord& operator=(const SharedAllocationRecord&) = delete;
184
185#ifdef KOKKOS_ENABLE_DEBUG
187 static RecordBase s_root_record;
188#endif
189
190 const Kokkos::HostSpace m_space;
191
192 protected:
193 ~SharedAllocationRecord();
194 SharedAllocationRecord() = default;
195
196 // This constructor does not forward to the one without exec_space arg
197 // in order to work around https://github.com/kokkos/kokkos/issues/5258
198 // This constructor is templated so I can't just put it into the cpp file
199 // like the other constructor.
200 template <typename ExecutionSpace>
201 SharedAllocationRecord(
202 const ExecutionSpace& /* exec_space*/, const Kokkos::HostSpace& arg_space,
203 const std::string& arg_label, const size_t arg_alloc_size,
204 const RecordBase::function_type arg_dealloc = &deallocate)
205 : base_t(
206#ifdef KOKKOS_ENABLE_DEBUG
207 &SharedAllocationRecord<Kokkos::HostSpace, void>::s_root_record,
208#endif
209 Impl::checked_allocation_with_header(arg_space, arg_label,
210 arg_alloc_size),
211 sizeof(SharedAllocationHeader) + arg_alloc_size, arg_dealloc,
212 arg_label),
213 m_space(arg_space) {
214 this->base_t::_fill_host_accessible_header_info(*RecordBase::m_alloc_ptr,
215 arg_label);
216 }
217
218 SharedAllocationRecord(
219 const Kokkos::HostSpace& arg_space, const std::string& arg_label,
220 const size_t arg_alloc_size,
221 const RecordBase::function_type arg_dealloc = &deallocate);
222
223 public:
224 KOKKOS_INLINE_FUNCTION static SharedAllocationRecord* allocate(
225 const Kokkos::HostSpace& arg_space, const std::string& arg_label,
226 const size_t arg_alloc_size) {
227 KOKKOS_IF_ON_HOST((return new SharedAllocationRecord(arg_space, arg_label,
228 arg_alloc_size);))
229 KOKKOS_IF_ON_DEVICE(((void)arg_space; (void)arg_label; (void)arg_alloc_size;
230 return nullptr;))
231 }
232};
233
234} // namespace Impl
235
236} // namespace Kokkos
237
238//----------------------------------------------------------------------------
239
240namespace Kokkos {
241
242namespace Impl {
243
244template <>
245struct DeepCopy<HostSpace, HostSpace, DefaultHostExecutionSpace> {
246 DeepCopy(void* dst, const void* src, size_t n) {
247 hostspace_parallel_deepcopy(dst, src, n);
248 }
249
250 DeepCopy(const DefaultHostExecutionSpace& exec, void* dst, const void* src,
251 size_t n) {
252 hostspace_parallel_deepcopy_async(exec, dst, src, n);
253 }
254};
255
256template <class ExecutionSpace>
257struct DeepCopy<HostSpace, HostSpace, ExecutionSpace> {
258 DeepCopy(void* dst, const void* src, size_t n) {
259 hostspace_parallel_deepcopy(dst, src, n);
260 }
261
262 DeepCopy(const ExecutionSpace& exec, void* dst, const void* src, size_t n) {
263 exec.fence(
264 "Kokkos::Impl::DeepCopy<HostSpace, HostSpace, "
265 "ExecutionSpace>::DeepCopy: fence before copy");
266 hostspace_parallel_deepcopy_async(dst, src, n);
267 }
268};
269
270} // namespace Impl
271
272} // namespace Kokkos
273
274#endif // #define KOKKOS_HOSTSPACE_HPP
LogicalMemorySpace is a space that is identical to another space, but differentiable by name and temp...
Memory management for host memory.
HostSpace()
Default memory space instance.
HostSpace memory_space
Tag this class as a kokkos memory space.
static constexpr const char * name()
Return Name of the MemorySpace.
DefaultHostExecutionSpace execution_space
Default execution space for this memory space.
void deallocate(void *const arg_alloc_ptr, const size_t arg_alloc_size) const
Deallocate untracked memory in the space.
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.
Kokkos::Device< execution_space, memory_space > device_type
This memory space preferred device_type.
ScopeGuard Some user scope issues have been identified with some Kokkos::finalize calls; ScopeGuard a...
Access relationship between DstMemorySpace and SrcMemorySpace.