content_description.hpp
1 
5 #pragma once
6 #include <numeric>
7 #include <type_traits>
8 #include <utility>
9 
10 #include <rapidsmpf/memory/buffer.hpp>
11 #include <rapidsmpf/memory/memory_type.hpp>
12 
13 namespace rapidsmpf {
14 
33  public:
35  enum class Spillable : bool {
36  NO,
37  YES
38  };
39 
60  template <
61  std::ranges::input_range Range =
62  std::initializer_list<std::pair<MemoryType, std::size_t>>>
63  requires std::convertible_to<
64  std::ranges::range_value_t<Range>,
65  std::pair<MemoryType, std::size_t>>
66  constexpr explicit ContentDescription(Range&& sizes, Spillable spillable)
67  : spillable_(spillable == Spillable::YES) {
68  content_sizes_.fill(0);
69  for (auto&& [mem_type, size] : sizes) {
70  auto idx = static_cast<std::size_t>(mem_type);
71  if (idx < content_sizes_.size()) {
72  content_sizes_[idx] = size;
73  }
74  }
75  }
76 
85  constexpr ContentDescription(Spillable spillable = Spillable::NO)
86  : ContentDescription({{}}, spillable) {}
87 
94  [[nodiscard]] constexpr std::size_t& content_size(MemoryType mem_type) noexcept {
95  return content_sizes_[static_cast<std::size_t>(mem_type)];
96  }
97 
104  [[nodiscard]] constexpr std::size_t content_size(MemoryType mem_type) const noexcept {
105  return content_sizes_[static_cast<std::size_t>(mem_type)];
106  }
107 
123  [[nodiscard]] constexpr MemoryType principal_memory_type() const noexcept {
124  for (auto mem_type : MEMORY_TYPES) {
125  if (content_size(mem_type) > 0) {
126  return mem_type;
127  }
128  }
129  return MemoryType::DEVICE;
130  }
131 
141  [[nodiscard]] constexpr std::size_t content_size() const noexcept {
142  return std::accumulate(
143  content_sizes_.begin(), content_sizes_.end(), std::size_t{0}
144  );
145  }
146 
148  [[nodiscard]] constexpr bool spillable() const noexcept {
149  return spillable_;
150  }
151 
158  [[nodiscard]] constexpr bool operator==(
159  ContentDescription const& other
160  ) const noexcept {
161  return spillable_ == other.spillable_ && content_sizes_ == other.content_sizes_;
162  }
163 
164  private:
167  std::array<std::size_t, MEMORY_TYPES.size()> content_sizes_ = {};
168  bool spillable_;
169 };
170 
171 static_assert(
172  std::is_trivially_copyable_v<ContentDescription>,
173  "ContentDescription must be trivially copyable"
174 );
175 
176 } // 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 MemoryType principal_memory_type() const noexcept
Returns the principal memory type of the content.
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
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:13
@ YES
Overbooking is allowed.
@ NO
Overbooking is not allowed.
constexpr std::array< MemoryType, 3 > MEMORY_TYPES
All memory types sorted in decreasing order of preference.
Definition: memory_type.hpp:23
MemoryType
Enum representing the type of memory sorted in decreasing order of preference.
Definition: memory_type.hpp:16
@ DEVICE
Device memory.