postbox.hpp
1 
5 #pragma once
6 
7 #include <functional>
8 #include <mutex>
9 #include <string>
10 #include <unordered_map>
11 #include <vector>
12 
13 #include <rapidsmpf/error.hpp>
14 #include <rapidsmpf/shuffler/chunk.hpp>
15 
17 
24 template <typename KeyType>
25 class PostBox {
26  public:
27  using key_type = KeyType;
28 
39  template <typename Fn>
40  PostBox(Fn&& key_map_fn, size_t num_keys_hint = 0)
41  : key_map_fn_(std::move(key_map_fn)) {
42  if (num_keys_hint > 0) {
43  pigeonhole_.reserve(num_keys_hint);
44  }
45  }
46 
52  void insert(Chunk&& chunk);
53 
63  bool is_empty(PartID pid) const;
64 
74  [[nodiscard]] Chunk extract(PartID pid, ChunkID cid);
75 
84  std::unordered_map<ChunkID, Chunk> extract(PartID pid);
85 
94  std::unordered_map<ChunkID, Chunk> extract_by_key(KeyType key);
95 
101  std::vector<Chunk> extract_all_ready();
102 
108  [[nodiscard]] bool empty() const;
109 
117  [[nodiscard]] std::vector<std::tuple<key_type, ChunkID, std::size_t>> search(
118  MemoryType mem_type
119  ) const;
120 
125  [[nodiscard]] std::string str() const;
126 
127  private:
128  // TODO: more fine-grained locking e.g. by locking each partition individually.
129  mutable std::mutex mutex_;
130  std::function<key_type(PartID)>
131  key_map_fn_;
132  std::unordered_map<key_type, std::unordered_map<ChunkID, Chunk>>
133  pigeonhole_;
134 };
135 
145 template <typename KeyType>
146 inline std::ostream& operator<<(std::ostream& os, PostBox<KeyType> const& obj) {
147  os << obj.str();
148  return os;
149 }
150 
151 } // namespace rapidsmpf::shuffler::detail
Chunk with multiple messages. This class contains two buffers for concatenated metadata and data.
Definition: chunk.hpp:58
A thread-safe container for managing and retrieving data chunks by partition and chunk ID.
Definition: postbox.hpp:25
bool is_empty(PartID pid) const
Check whether the specified partition contains any chunks.
std::vector< std::tuple< key_type, ChunkID, std::size_t > > search(MemoryType mem_type) const
Searches for chunks of the specified memory type.
KeyType key_type
The type of the key used to map chunks.
Definition: postbox.hpp:27
std::unordered_map< ChunkID, Chunk > extract_by_key(KeyType key)
Extracts all chunks associated with a specific key.
std::string str() const
Returns a description of this instance.
bool empty() const
Checks if the PostBox is empty.
void insert(Chunk &&chunk)
Inserts a chunk into the PostBox.
PostBox(Fn &&key_map_fn, size_t num_keys_hint=0)
Construct a new PostBox.
Definition: postbox.hpp:40
std::unordered_map< ChunkID, Chunk > extract(PartID pid)
Extracts all chunks associated with a specific partition.
Chunk extract(PartID pid, ChunkID cid)
Extracts a specific chunk from the PostBox.
std::vector< Chunk > extract_all_ready()
Extracts all ready chunks from the PostBox.
Shuffler private interfaces.
Definition: chunk.hpp:24
std::ostream & operator<<(std::ostream &os, Chunk const &obj)
Overloads the stream insertion operator for the Chunk class.
Definition: chunk.hpp:450
std::uint64_t ChunkID
The globally unique ID of a chunk.
Definition: chunk.hpp:29
std::uint32_t PartID
Partition ID, which goes from 0 to the total number of partitions.
Definition: chunk.hpp:22