content_description.hpp
1 
5 #pragma once
6 #include <numeric>
7 #include <type_traits>
8 #include <utility>
9 
10 #include <rapidsmpf/buffer/buffer.hpp>
11 
12 namespace rapidsmpf {
13 
32  public:
34  enum class Spillable : bool {
35  NO,
36  YES
37  };
38 
59  template <
60  std::ranges::input_range Range =
61  std::initializer_list<std::pair<MemoryType, std::size_t>>>
62  requires std::convertible_to<
63  std::ranges::range_value_t<Range>,
64  std::pair<MemoryType, std::size_t>>
65  constexpr explicit ContentDescription(Range&& sizes, Spillable spillable)
66  : spillable_(spillable == Spillable::YES) {
67  content_sizes_.fill(0);
68  for (auto&& [mem_type, size] : sizes) {
69  auto idx = static_cast<std::size_t>(mem_type);
70  if (idx < content_sizes_.size()) {
71  content_sizes_[idx] = size;
72  }
73  }
74  }
75 
84  constexpr ContentDescription(Spillable spillable = Spillable::NO)
85  : ContentDescription({{}}, spillable) {}
86 
93  [[nodiscard]] constexpr std::size_t& content_size(MemoryType mem_type) noexcept {
94  return content_sizes_[static_cast<std::size_t>(mem_type)];
95  }
96 
103  [[nodiscard]] constexpr std::size_t content_size(MemoryType mem_type) const noexcept {
104  return content_sizes_[static_cast<std::size_t>(mem_type)];
105  }
106 
116  [[nodiscard]] constexpr std::size_t content_size() const noexcept {
117  return std::accumulate(
118  content_sizes_.begin(), content_sizes_.end(), std::size_t{0}
119  );
120  }
121 
123  [[nodiscard]] constexpr bool spillable() const noexcept {
124  return spillable_;
125  }
126 
133  [[nodiscard]] constexpr bool operator==(
134  ContentDescription const& other
135  ) const noexcept {
136  return spillable_ == other.spillable_ && content_sizes_ == other.content_sizes_;
137  }
138 
139  private:
142  std::array<std::size_t, MEMORY_TYPES.size()> content_sizes_ = {};
143  bool spillable_;
144 };
145 
146 static_assert(
147  std::is_trivially_copyable_v<ContentDescription>,
148  "ContentDescription must be trivially copyable"
149 );
150 
151 } // namespace rapidsmpf
Description of an object's content.
Spillable
Indicates whether the content is spillable.
constexpr ContentDescription(Spillable spillable=Spillable::NO)
Construct a description with all sizes zero and a given spillability.
requires constexpr std::convertible_to< std::ranges::range_value_t< Range >, std::pair< MemoryType, std::size_t > > ContentDescription(Range &&sizes, Spillable spillable)
Construct a content description from a range of (MemoryType, size) pairs.
constexpr std::size_t content_size(MemoryType mem_type) const noexcept
Get the size for a specific memory type.
constexpr std::size_t & content_size(MemoryType mem_type) noexcept
Access (read/write) the size for a specific memory type.
constexpr std::size_t content_size() const noexcept
Get the total content size across all memory types.
constexpr bool operator==(ContentDescription const &other) const noexcept
Equality comparison.
constexpr bool spillable() const noexcept