17#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
18#include <Kokkos_Macros.hpp>
20 "Including non-public Kokkos header files is not allowed.");
22#ifndef KOKKOS_HOSTSPACE_HPP
23#define KOKKOS_HOSTSPACE_HPP
30#include <Kokkos_Core_fwd.hpp>
31#include <Kokkos_Concepts.hpp>
32#include <Kokkos_MemoryTraits.hpp>
34#include <impl/Kokkos_Traits.hpp>
35#include <impl/Kokkos_Error.hpp>
36#include <impl/Kokkos_SharedAlloc.hpp>
37#include <impl/Kokkos_Tools.hpp>
39#include "impl/Kokkos_HostSpace_deepcopy.hpp"
40#include <impl/Kokkos_MemorySpace.hpp>
85 using size_type = size_t;
96 using device_type = Kokkos::Device<execution_space, memory_space>;
120 void*
allocate(
const char* arg_label,
const size_t arg_alloc_size,
121 const size_t arg_logical_size = 0)
const;
124 void deallocate(
void*
const arg_alloc_ptr,
const size_t arg_alloc_size)
const;
125 void deallocate(
const char* arg_label,
void*
const arg_alloc_ptr,
126 const size_t arg_alloc_size,
127 const size_t arg_logical_size = 0)
const;
130 template <
class,
class,
class,
class>
133 void* impl_allocate(
const char* arg_label,
const size_t arg_alloc_size,
134 const size_t arg_logical_size = 0,
135 const Kokkos::Tools::SpaceHandle =
136 Kokkos::Tools::make_space_handle(
name()))
const;
137 void impl_deallocate(
const char* arg_label,
void*
const arg_alloc_ptr,
138 const size_t arg_alloc_size,
139 const size_t arg_logical_size = 0,
140 const Kokkos::Tools::SpaceHandle =
141 Kokkos::Tools::make_space_handle(
name()))
const;
145 static constexpr const char*
name() {
return m_name; }
149 static constexpr const char* m_name =
"Host";
150 friend class Kokkos::Impl::SharedAllocationRecord<Kokkos::
HostSpace, void>;
172 typename S::execution_space::memory_space,
181 typename S::memory_space>::accessible
185 using Space = std::conditional_t<
186 keep_exe && keep_mem, S,
187 std::conditional_t<keep_mem,
189 typename S::memory_space>,
204class SharedAllocationRecord<Kokkos::HostSpace, void>
205 :
public SharedAllocationRecordCommon<Kokkos::HostSpace> {
208 friend class SharedAllocationRecordCommon<Kokkos::HostSpace>;
210 using base_t = SharedAllocationRecordCommon<Kokkos::HostSpace>;
211 using RecordBase = SharedAllocationRecord<void, void>;
213 SharedAllocationRecord(const SharedAllocationRecord&) = delete;
214 SharedAllocationRecord& operator=(const SharedAllocationRecord&) = delete;
216#ifdef KOKKOS_ENABLE_DEBUG
218 static RecordBase s_root_record;
224 ~SharedAllocationRecord();
225 SharedAllocationRecord() = default;
231 template <typename ExecutionSpace>
232 SharedAllocationRecord(
233 const ExecutionSpace& , const Kokkos::HostSpace& arg_space,
234 const std::string& arg_label, const size_t arg_alloc_size,
235 const RecordBase::function_type arg_dealloc = &deallocate)
237#ifdef KOKKOS_ENABLE_DEBUG
238 &SharedAllocationRecord<Kokkos::HostSpace, void>::s_root_record,
240 Impl::checked_allocation_with_header(arg_space, arg_label,
242 sizeof(SharedAllocationHeader) + arg_alloc_size, arg_dealloc,
245 this->base_t::_fill_host_accessible_header_info(*RecordBase::m_alloc_ptr,
249 SharedAllocationRecord(
251 const size_t arg_alloc_size,
252 const RecordBase::function_type arg_dealloc = &deallocate);
255 KOKKOS_INLINE_FUNCTION
static SharedAllocationRecord* allocate(
257 const size_t arg_alloc_size) {
258 KOKKOS_IF_ON_HOST((
return new SharedAllocationRecord(arg_space, arg_label,
260 KOKKOS_IF_ON_DEVICE(((
void)arg_space; (void)arg_label; (void)arg_alloc_size;
275template <
class DT,
class... DP>
276struct ZeroMemset<typename HostSpace::execution_space, DT, DP...> {
277 ZeroMemset(
const typename HostSpace::execution_space& exec,
278 const View<DT, DP...>& dst,
279 typename View<DT, DP...>::const_value_type&) {
284 hostspace_fence(exec);
285 using ValueType =
typename View<DT, DP...>::value_type;
286 std::memset(dst.data(), 0,
sizeof(ValueType) * dst.size());
289 ZeroMemset(
const View<DT, DP...>& dst,
290 typename View<DT, DP...>::const_value_type&) {
291 using ValueType =
typename View<DT, DP...>::value_type;
292 std::memset(dst.data(), 0,
sizeof(ValueType) * dst.size());
297struct DeepCopy<HostSpace, HostSpace, DefaultHostExecutionSpace> {
298 DeepCopy(
void* dst,
const void* src,
size_t n) {
299 hostspace_parallel_deepcopy(dst, src, n);
302 DeepCopy(
const DefaultHostExecutionSpace& exec,
void* dst,
const void* src,
304 hostspace_parallel_deepcopy_async(exec, dst, src, n);
308template <
class ExecutionSpace>
309struct DeepCopy<HostSpace, HostSpace, ExecutionSpace> {
310 DeepCopy(
void* dst,
const void* src,
size_t n) {
311 hostspace_parallel_deepcopy(dst, src, n);
314 DeepCopy(
const ExecutionSpace& exec,
void* dst,
const void* src,
size_t n) {
316 "Kokkos::Impl::DeepCopy<HostSpace, HostSpace, "
317 "ExecutionSpace>::DeepCopy: fence before copy");
318 hostspace_parallel_deepcopy_async(dst, src, n);
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.
static constexpr const char * name()
Return Name of the MemorySpace.
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.
DefaultHostExecutionSpace execution_space
Default execution space for this memory space.
void unlock_address_host_space(void *ptr)
Release lock for the address.
void init_lock_array_host_space()
Initialize lock array for arbitrary size atomics.
bool lock_address_host_space(void *ptr)
Acquire a lock for the address.
Access relationship between DstMemorySpace and SrcMemorySpace.