4#include <uni20/config.hpp>
45 : data_(allocate(
size, initialization)), size_(
size)
49 this->initialize_elements(data_, size_, initialization);
53 release(data_, size_);
61 requires has_runtime_precision_v<value_type>
64 precision_.value = precision;
65 this->initialize_elements(data_, size_, initialization);
69 Precision default_precision() const
72 return precision_.get();
74 std::optional<Precision> default_precision_if_set() const noexcept
77 return precision_.value;
80 void default_precision(Precision precision)
81 requires has_runtime_precision_v<value_type>
83 precision_.value = precision;
93 if constexpr (has_runtime_precision_v<value_type>) precision_.value =
value.precision();
95 std::fill_n(data_, size_,
value);
99 release(data_, size_);
106 HostBuffer(
HostBuffer const& other) : data_(allocate(other.size_)), size_(other.size_), precision_(other.precision_)
110 this->copy_from(other.data_, other.size_);
114 release(data_, size_);
122 : data_(std::exchange(other.data_,
nullptr)), size_(std::exchange(other.size_, 0)),
123 precision_(std::move(other.precision_))
130 if (
this == &other)
return *
this;
132 this->
swap(replacement);
138 if (
this == &other)
return *
this;
139 release(data_, size_);
140 data_ = std::exchange(other.data_,
nullptr);
141 size_ = std::exchange(other.size_, 0);
142 precision_ = std::move(other.precision_);
153 if (
size == size_)
return;
159 copy_values(replacement, data_, copied_size);
160 if (
size > copied_size)
161 this->initialize_elements(replacement + copied_size,
size - copied_size, initialization);
165 release(replacement,
size);
169 release(data_, size_);
176 release(data_, size_);
184 swap(data_, other.data_);
185 swap(size_, other.size_);
186 swap(precision_, other.precision_);
192 [[nodiscard]]
bool empty() const noexcept {
return size_ == 0; }
196 [[nodiscard]]
auto end() noexcept ->
iterator {
return size_ == 0 ? data_ : data_ + size_; }
197 [[nodiscard]]
auto end() const noexcept ->
const_iterator {
return size_ == 0 ? data_ : data_ + size_; }
206 if constexpr (has_runtime_precision_v<value_type>)
215 auto p = this->default_precision();
222 detail::initialize_host_elements(
data, count, initialization);
228 if (
size == 0)
return nullptr;
229 if (
size > std::numeric_limits<size_type>::max() /
sizeof(
value_type))
throw std::bad_array_new_length{};
233 if constexpr (!uninitialized_ok<value_type>)
237 if constexpr (has_runtime_precision_v<value_type>)
240 std::uninitialized_value_construct_n(result,
size);
244 detail::aligned_deleter<value_type>{}(result);
248 else if constexpr (!std::is_assignable_v<value_type&, value_type>)
255 if constexpr (detail::zero_initializable_v<value_type>)
258 ::new (
static_cast<void*
>(result + i))
value_type(detail::zero_value<value_type>());
262 throw std::invalid_argument(
"Zero initialization requires an element type with a zero/default value");
267 detail::aligned_deleter<value_type>{}(result);
277 if (
data ==
nullptr)
return;
278 if constexpr (!uninitialized_ok<value_type>)
282 detail::aligned_deleter<value_type>{}(
data);
287 if (
size == 0)
return;
288 if constexpr (uninitialized_ok<value_type>)
292 std::memcpy(
static_cast<void*
>(output), input,
size *
sizeof(
value_type));
296 std::copy_n(input,
size, output);
304 [[no_unique_address]] detail::precision_default<value_type> precision_{};
323 template <
typename ElementType>
331 template <
typename ElementType>
332 requires has_runtime_precision_v<ElementType>
336 return storage_t<ElementType>(size, initialization,
p);
338 template <
typename ElementType>
339 requires has_runtime_precision_v<ElementType>
340 [[nodiscard]]
static auto make_storage_like(storage_t<ElementType>
const& source, std::size_t size,
343 if (
auto p = source.default_precision_if_set())
return storage_t<ElementType>(size, initialization, *
p);
344 return storage_t<ElementType>(size, initialization);
350 return storage.data();
353 template <
typename ElementType>
356 return storage.data();
360#if UNI20_BACKEND_BLAS || UNI20_HAS_MPLAPACK_MPREAL || UNI20_HAS_MPLAPACK_FLOAT80
369#if UNI20_BACKEND_BLAS || UNI20_HAS_MPLAPACK_MPREAL || UNI20_HAS_MPLAPACK_FLOAT80
Aligned, (un)initialized and temporary buffer allocation utilities.
Ordered backend selector values shared by tensor storage and linalg dispatch.
Owning contiguous host buffer used by HostStorage.
Definition host_storage.hpp:26
HostBuffer(HostBuffer &&other) noexcept
Definition host_storage.hpp:121
auto begin() const noexcept -> const_iterator
Definition host_storage.hpp:195
HostBuffer() noexcept=default
void resize(size_type size)
Replace the allocation while preserving the common prefix.
Definition host_storage.hpp:148
auto operator[](size_type index) noexcept -> value_type &
Definition host_storage.hpp:199
auto size() const noexcept -> size_type
Definition host_storage.hpp:191
value_type const * const_iterator
Definition host_storage.hpp:31
auto end() noexcept -> iterator
Definition host_storage.hpp:196
auto data() noexcept -> value_type *
Definition host_storage.hpp:189
HostBuffer(HostBuffer const &other)
Definition host_storage.hpp:106
value_type * iterator
Definition host_storage.hpp:30
HostBuffer(size_type size, StorageInitialization initialization)
Allocate elements with an explicit numerical initialization policy.
Definition host_storage.hpp:44
void clear() noexcept
Definition host_storage.hpp:174
void swap(HostBuffer &other) noexcept
Definition host_storage.hpp:181
~HostBuffer()
Definition host_storage.hpp:126
ElementType value_type
Definition host_storage.hpp:28
auto end() const noexcept -> const_iterator
Definition host_storage.hpp:197
auto data() const noexcept -> value_type const *
Definition host_storage.hpp:190
auto operator[](size_type index) const noexcept -> value_type const &
Definition host_storage.hpp:200
bool empty() const noexcept
Definition host_storage.hpp:192
auto operator=(HostBuffer const &other) -> HostBuffer &
Definition host_storage.hpp:128
static constexpr bool initializes_elements
Definition host_storage.hpp:33
auto begin() noexcept -> iterator
Definition host_storage.hpp:194
HostBuffer(size_type size, value_type const &value)
Allocate and fill storage with one value.
Definition host_storage.hpp:88
auto operator=(HostBuffer &&other) noexcept -> HostBuffer &
Definition host_storage.hpp:136
void resize(size_type size, StorageInitialization initialization)
Preserve the common prefix and initialize only newly added elements.
Definition host_storage.hpp:151
std::size_t size_type
Definition host_storage.hpp:29
Exact arithmetic or an explicit binary working precision.
Definition core/precision.hpp:13
Whether raw allocation may establish storage for T without construction.
Definition common/initialization.hpp:46
locale_t value
Definition data_table.cpp:49
Configured gateway to Uni20's mdspan implementation.
Definition data_table.cpp:8
Definition async/async.hpp:26
constexpr uninitialized_t uninitialized
Explicit allocation tag for overwrite outputs and scratch space.
Definition common/initialization.hpp:22
constexpr bool has_runtime_precision_v
Whether numerical values of this scalar family carry runtime precision.
Definition runtime_precision.hpp:10
void swap(HostBuffer< ElementType > &lhs, HostBuffer< ElementType > &rhs) noexcept
Definition host_storage.hpp:307
std::ptrdiff_t size_type
Signed size and index type, used for tensor extents and indexing.
Definition types.hpp:18
StorageInitialization
Numerical initialization requested from a storage implementation.
Definition common/initialization.hpp:26
table_alignment alignment
Definition presentation.cpp:2018
Default pageable-host tensor storage policy.
Definition host_storage.hpp:316
static constexpr std::size_t allocation_alignment
Guaranteed byte alignment of every HostBuffer allocation.
Definition host_storage.hpp:318
static auto make_handle(storage_t< ElementType > const &storage) noexcept -> ElementType const *
Definition host_storage.hpp:354
static auto make_storage(std::size_t size, StorageInitialization initialization) -> storage_t< ElementType >
Allocate host elements with the requested numerical initialization.
Definition host_storage.hpp:324
static constexpr auto backend_selector() noexcept -> backend_selector_type
Return the default ordered linalg backends for pageable host storage.
Definition host_storage.hpp:366
static auto make_handle(storage_t< ElementType > &storage) noexcept -> ElementType *
Definition host_storage.hpp:348
Backend family for BLAS dense linalg kernels, including MPLAPACK scalar adapters.
Definition backend_selector.hpp:27
Backend value for the reference host CPU dense linalg oracle.
Definition backend_selector.hpp:55
Backend family for configured dense LAPACK kernels, including MPLAPACK scalar adapters.
Definition backend_selector.hpp:20
Ordered list of backend values tried by the dispatch walk.
Definition backend_selector.hpp:62