4#ifndef XGBOOST_COMMON_REF_RESOURCE_VIEW_H_
5#define XGBOOST_COMMON_REF_RESOURCE_VIEW_H_
16#include "xgboost/span.h"
27 static_assert(!std::is_reference_v<T>);
31 using size_type = std::uint64_t;
34 value_type* ptr_{
nullptr};
36 std::shared_ptr<common::ResourceHandler> mem_{
nullptr};
39 void Init(value_type* ptr, size_type size, std::shared_ptr<common::ResourceHandler> mem) {
42 mem_ = std::move(mem);
46 RefResourceView(value_type* ptr, size_type n, std::shared_ptr<common::ResourceHandler> mem)
47 : ptr_{ptr}, size_{n}, mem_{std::move(mem)} {
48 CHECK_GE(mem_->Size(), n);
58 RefResourceView(value_type* ptr, size_type n, std::shared_ptr<common::ResourceHandler> mem,
62 std::fill_n(ptr_, n, init);
77 [[nodiscard]] size_type size()
const {
return size_; }
78 [[nodiscard]] size_type size_bytes()
const {
81 [[nodiscard]] value_type* data() {
return ptr_; };
82 [[nodiscard]] value_type
const* data()
const {
return ptr_; };
83 [[nodiscard]]
bool empty()
const {
return size() == 0; }
85 [[nodiscard]]
auto cbegin()
const {
return data(); }
86 [[nodiscard]]
auto begin() {
return data(); }
87 [[nodiscard]]
auto begin()
const {
return cbegin(); }
88 [[nodiscard]]
auto cend()
const {
return data() + size(); }
89 [[nodiscard]]
auto end() {
return data() + size(); }
90 [[nodiscard]]
auto end()
const {
return cend(); }
92 [[nodiscard]]
auto const& front()
const {
return data()[0]; }
93 [[nodiscard]]
auto& front() {
return data()[0]; }
94 [[nodiscard]]
auto const& back()
const {
return data()[size() - 1]; }
95 [[nodiscard]]
auto& back() {
return data()[size() - 1]; }
97 [[nodiscard]] value_type& operator[](size_type i) {
return ptr_[i]; }
98 [[nodiscard]] value_type
const& operator[](size_type i)
const {
return ptr_[i]; }
111template <
typename Vec>
121 using T =
typename Vec::value_type;
122 auto expected_bytes =
sizeof(T) * n;
124 auto [ptr, n_bytes] = fi->Consume(expected_bytes);
125 if (n_bytes != expected_bytes) {
129 if constexpr (std::is_same_v<Vec, RefResourceView<T>>) {
133 std::memcpy(vec->data(), ptr, n_bytes);
141template <
typename Vec>
143 std::size_t bytes{0};
144 auto n =
static_cast<std::uint64_t
>(vec.size());
145 bytes += fo->Write(n);
150 using T =
typename std::remove_reference_t<
decltype(vec)>::value_type;
151 bytes += fo->Write(vec.data(), vec.size() *
sizeof(T));
161 auto resource = std::make_shared<common::MallocResource>(n_elements *
sizeof(T));
162 return RefResourceView{resource->DataAs<T>(), n_elements, resource, init};
167 static_assert(!std::is_reference_v<T>);
168 static_assert(!std::is_const_v<T>);
169 static_assert(std::is_trivially_copyable_v<T>);
172 using size_type =
typename Upper::size_type;
173 using value_type =
typename Upper::value_type;
185 void Resize(
typename Upper::size_type new_size) {
186 auto resource = std::dynamic_pointer_cast<common::MallocResource>(this->
Resource());
188 resource->Resize(new_size *
sizeof(T));
189 this->Init(resource->template DataAs<T>(), new_size, resource);
Output stream backed by a file.
Definition io.h:426
Wrap resource into a dmlc stream.
Definition io.h:282
Definition ref_resource_view.h:166
A vector-like type that holds a reference counted resource.
Definition ref_resource_view.h:26
RefResourceView(RefResourceView &&that)=default
We allow move assignment for lazy initialization.
auto Resource() const
Get the underlying resource.
Definition ref_resource_view.h:103
RefResourceView(value_type *ptr, size_type n, std::shared_ptr< common::ResourceHandler > mem, T const &init)
Construct a view on ptr with length n.
Definition ref_resource_view.h:58
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition span.h:424
defines console logging options for xgboost. Use to enforce unified print behavior.
Copyright 2017-2023, XGBoost Contributors.
Definition span.h:77
std::size_t WriteVec(AlignedFileWriteStream *fo, Vec const &vec)
Write a vector to stream.
Definition ref_resource_view.h:142
bool ReadVec(common::AlignedResourceReadStream *fi, Vec *vec)
Read a vector from stream.
Definition ref_resource_view.h:112
RefResourceView< T > MakeFixedVecWithMalloc(std::size_t n_elements, T const &init)
Make a fixed size RefResourceView with malloc resource.
Definition ref_resource_view.h:160
Copyright 2014-2023, XGBoost Contributors.