allgather.hpp
1 
5 #pragma once
6 
7 #include <atomic>
8 #include <chrono>
9 #include <condition_variable>
10 #include <cstdint>
11 #include <functional>
12 #include <memory>
13 #include <mutex>
14 #include <optional>
15 #include <vector>
16 
17 #include <rmm/cuda_stream_view.hpp>
18 
19 #include <rapidsmpf/coll/utils.hpp>
20 #include <rapidsmpf/communicator/communicator.hpp>
21 #include <rapidsmpf/memory/buffer.hpp>
22 #include <rapidsmpf/memory/buffer_resource.hpp>
23 #include <rapidsmpf/memory/packed_data.hpp>
24 #include <rapidsmpf/memory/spill_manager.hpp>
25 #include <rapidsmpf/progress_thread.hpp>
26 
34 namespace rapidsmpf::coll {
35 
57 class AllGather {
58  public:
65  void insert(std::uint64_t sequence_number, PackedData&& packed_data);
66 
71 
73  enum class Ordered : bool {
74  NO,
75  YES,
76  };
77 
92  [[nodiscard]] std::vector<PackedData> wait_and_extract(
93  Ordered ordered = Ordered::YES,
94  std::chrono::milliseconds timeout = std::chrono::milliseconds{-1}
95  );
96 
115  std::shared_ptr<Communicator> comm,
116  OpID op_id,
117  BufferResource* br,
118  std::function<void(void)>&& finished_callback = nullptr
119  );
120 
122  AllGather(AllGather const&) = delete;
124  AllGather& operator=(AllGather const&) = delete;
126  AllGather(AllGather&&) = delete;
129 
135  [[nodiscard]] std::shared_ptr<Communicator> const& comm() const noexcept {
136  return comm_;
137  }
138 
146  ~AllGather() noexcept;
147 
156  ProgressThread::ProgressState event_loop();
157 
158  private:
164  void insert(std::unique_ptr<detail::Chunk> chunk);
165 
172  void mark_finish(std::uint64_t expected_chunks) noexcept;
173 
181  void wait(std::chrono::milliseconds timeout = std::chrono::milliseconds{-1});
182 
190  std::size_t spill(std::optional<std::size_t> amount = std::nullopt);
191 
192  std::shared_ptr<Communicator> comm_;
193  BufferResource* br_;
194  std::function<void(void)> finished_callback_{
195  nullptr
196  };
197  std::atomic<Rank> finish_counter_;
198  std::atomic<std::uint32_t> nlocal_insertions_;
199  std::atomic<std::uint64_t> extraction_goalpost_{
200  0
201  };
202  OpID op_id_;
203  std::atomic<bool> locally_finished_{false};
204  bool can_extract_{false};
205  mutable std::mutex mutex_;
206  std::condition_variable cv_;
207  detail::PostBox inserted_{};
208  detail::PostBox for_extraction_{};
209  ProgressThread::FunctionID function_id_{};
210  SpillManager::SpillFunctionID spill_function_id_{};
211  // We track remote finishes separately from the finish_counter_ above since the path
212  // through the event loop state machine for a local finish marker is slightly
213  // different from a remote finish marker.
215  Rank remote_finish_counter_;
217  std::uint64_t num_expected_messages_{0};
219  std::uint64_t num_received_messages_{0};
221  std::vector<std::unique_ptr<detail::Chunk>> to_receive_{};
223  std::vector<std::unique_ptr<Communicator::Future>> fire_and_forget_{};
225  std::vector<std::unique_ptr<detail::Chunk>> sent_posted_{};
227  std::vector<std::unique_ptr<Communicator::Future>> sent_futures_{};
229  std::vector<std::unique_ptr<detail::Chunk>> receive_posted_{};
231  std::vector<std::unique_ptr<Communicator::Future>> receive_futures_{};
232 };
233 
234 } // namespace rapidsmpf::coll
Class managing buffer resources.
A progress thread that can execute arbitrary functions.
std::size_t SpillFunctionID
Represents a unique identifier for a registered spill function.
AllGather communication service.
Definition: allgather.hpp:57
AllGather & operator=(AllGather const &)=delete
Deleted copy assignment operator.
AllGather(AllGather &&)=delete
Deleted move constructor.
~AllGather() noexcept
Destructor.
Ordered
Tag requesting ordering for extraction.
Definition: allgather.hpp:73
@ YES
Extraction is ordered.
@ NO
Extraction is unordered.
void insert(std::uint64_t sequence_number, PackedData &&packed_data)
Insert packed data into the allgather operation.
AllGather & operator=(AllGather &&)=delete
Deleted move assignment operator.
std::shared_ptr< Communicator > const & comm() const noexcept
Gets the communicator associated with this AllGather.
Definition: allgather.hpp:135
std::vector< PackedData > wait_and_extract(Ordered ordered=Ordered::YES, std::chrono::milliseconds timeout=std::chrono::milliseconds{-1})
Wait for completion and extract all gathered data.
ProgressThread::ProgressState event_loop()
Main event loop for processing allgather operations.
AllGather(std::shared_ptr< Communicator > comm, OpID op_id, BufferResource *br, std::function< void(void)> &&finished_callback=nullptr)
Construct a new allgather operation.
AllGather(AllGather const &)=delete
Deleted copy constructor.
void insert_finished()
Mark that this rank has finished contributing data.
Collective communication interfaces.
std::int32_t Rank
The rank of a node (e.g. the rank of a MPI process), or world size (total number of ranks).
std::int32_t OpID
Operation ID defined by the user. This allows users to concurrently execute multiple operations,...
Bag of bytes with metadata suitable for sending over the wire.
Definition: packed_data.hpp:26