2#ifndef TTG_MADNESS_BUFFER_H
3#define TTG_MADNESS_BUFFER_H
13template<
typename T,
typename Allocator>
21 static_assert(std::is_trivially_copyable_v<element_type>,
22 "Only trivially copyable types are supported for devices.");
23 static_assert(std::is_default_constructible_v<element_type>,
24 "Only default constructible types are supported for devices.");
28 using host_data_ptr = std::add_pointer_t<element_type>;
29 std::shared_ptr<element_type[]> m_sptr;
30 host_data_ptr m_host_data =
nullptr;
31 std::size_t m_count = 0;
41 return allocator_traits::allocate(get_allocator_reference(), n);
45 allocator_traits::deallocate(get_allocator_reference(), m_host_data, m_count);
55 , m_host_data(allocate(n))
63 template<
typename Deleter>
66 , m_sptr(
std::move(ptr))
67 , m_host_data(m_sptr.get())
77 , m_sptr(
std::move(ptr))
78 , m_host_data(m_sptr.get())
94 , m_host_data(db.m_host_data)
98 db.m_host_data =
nullptr;
110 allocator_type::operator=(std::move(db));
111 std::swap(m_host_data, db.m_host_data);
112 std::swap(m_count, db.m_count);
113 std::swap(m_owned, db.m_owned);
126 if (!device.
is_host())
throw std::runtime_error(
"MADNESS backend does not support non-host memory!");
173 if (device.
is_device())
throw std::runtime_error(
"MADNESS missing support for non-host memory!");
181 if (device.
is_device())
throw std::runtime_error(
"MADNESS missing support for non-host memory!");
195 if (device.
is_device())
throw std::runtime_error(
"MADNESS missing support for non-host memory!");
201 throw std::runtime_error(
"not implemented yet");
205 return (m_host_data ==
nullptr);
236 operator bool()
const {
253 m_host_data =
nullptr;
256 m_host_data = allocate(n);
270 m_sptr = std::move(ptr);
271 m_host_data = m_sptr.get();
284#if defined(TTG_SERIALIZATION_SUPPORTS_MADNESS)
285 template <
typename Archive>
286 std::enable_if_t<std::is_base_of_v<madness::archive::BufferInputArchive, Archive> ||
287 std::is_base_of_v<madness::archive::BufferOutputArchive, Archive>>
288 serialize(Archive& ar) {
289 if constexpr (ttg::detail::is_output_archive_v<Archive>) {
290 std::size_t s =
size();
292 ar << madness::archive::wrap(
host_ptr(), s);
297 ar >> madness::archive::wrap(
host_ptr(), s);
Represents a device in a specific execution space.
this contains MADNESS-based TTG functionality
A runtime-managed buffer mirrored between host and device memory.
const element_type * host_ptr() const
ttg::device::Device get_owner_device() const
void reset(std::shared_ptr< element_type[]> ptr, std::size_t n, ttg::scope scope=ttg::scope::SyncIn)
const element_type * owner_device_ptr() const
Buffer & operator=(const Buffer &db)=delete
bool is_current_on(ttg::device::Device dev) const
void unpin_on(int device_id)
std::decay_t< T > element_type
void reset_scope(ttg::scope scope)
void reset(std::size_t n, ttg::scope scope=ttg::scope::SyncIn)
Buffer(const Buffer &db)=delete
Buffer(std::shared_ptr< element_type[]> ptr, std::size_t n, ttg::scope scope=ttg::scope::SyncIn)
const element_type * device_ptr_on(const ttg::device::Device &device) const
element_type * device_ptr_on(const ttg::device::Device &device)
element_type * current_device_ptr()
bool is_valid_on(const ttg::device::Device &device) const
element_type * host_ptr()
void allocate_on(const ttg::device::Device &device_id)
typename allocator_traits::allocator_type allocator_type
element_type * owner_device_ptr()
void pin_on(int device_id)
std::allocator_traits< Allocator > allocator_traits
Buffer & operator=(Buffer &&db)
Buffer(std::unique_ptr< element_type[], Deleter > ptr, std::size_t n, ttg::scope scope=ttg::scope::SyncIn)
Buffer(std::size_t n, ttg::scope scope=ttg::scope::SyncIn)
const element_type * current_device_ptr() const
void set_current_device(const ttg::device::Device &device)