single.hpp
1 
5 #pragma once
6 
7 #include <cstdlib>
8 #include <memory>
9 #include <string>
10 #include <unordered_map>
11 #include <vector>
12 
13 #include <rapidsmpf/communicator/communicator.hpp>
14 #include <rapidsmpf/config.hpp>
15 #include <rapidsmpf/memory/buffer.hpp>
16 #include <rapidsmpf/memory/buffer_resource.hpp>
17 #include <rapidsmpf/progress_thread.hpp>
18 
19 namespace rapidsmpf {
20 
28 class Single final : public Communicator {
29  public:
36  class Future : public Communicator::Future {
37  friend class Single;
38 
39  public:
40  ~Future() noexcept override = default;
41  };
42 
51  std::shared_ptr<ProgressThread> progress_thread, std::shared_ptr<Logger> logger
52  );
53 
54  ~Single() noexcept override = default;
55 
59  [[nodiscard]] constexpr Rank rank() const override {
60  return 0;
61  }
62 
66  [[nodiscard]] constexpr Rank nranks() const override {
67  return 1;
68  }
69 
73  [[nodiscard]] std::unique_ptr<Communicator::Future> send(
74  std::unique_ptr<std::vector<std::uint8_t>> msg, Rank rank, Tag tag
75  ) override;
76 
77  // clang-format off
83  // clang-format on
84  [[nodiscard]] std::unique_ptr<Communicator::Future> send(
85  std::unique_ptr<Buffer> msg, Rank rank, Tag tag
86  ) override;
87 
94  [[nodiscard]] std::unique_ptr<Communicator::Future> recv(
95  Rank rank, Tag tag, std::unique_ptr<Buffer> recv_buffer
96  ) override;
97 
98  // clang-format off
105  // clang-format on
106  [[nodiscard]] std::unique_ptr<Communicator::Future> recv_sync_host_data(
107  Rank rank, Tag tag, std::unique_ptr<std::vector<std::uint8_t>> synced_buffer
108  ) override;
109 
116  [[nodiscard]] std::pair<std::unique_ptr<std::vector<std::uint8_t>>, Rank> recv_any(
117  Tag tag
118  ) override;
119 
126  [[nodiscard]] std::unique_ptr<std::vector<std::uint8_t>> recv_from(
127  Rank src, Tag tag
128  ) override;
129 
136  std::pair<
137  std::vector<std::unique_ptr<Communicator::Future>>,
138  std::vector<std::size_t>>
139  test_some(std::vector<std::unique_ptr<Communicator::Future>>& future_vector) override;
140 
141  // clang-format off
147  // clang-format on
148  std::vector<std::size_t> test_some(
149  std::unordered_map<std::size_t, std::unique_ptr<Communicator::Future>> const&
150  future_map
151  ) override;
152 
154  bool test(std::unique_ptr<Communicator::Future>& future) override;
156  std::vector<std::unique_ptr<Buffer>> wait_all(
157  std::vector<std::unique_ptr<Communicator::Future>>&& futures
158  ) override;
159 
166  [[nodiscard]] std::unique_ptr<Buffer> wait(
167  std::unique_ptr<Communicator::Future> future
168  ) override;
169 
176  [[nodiscard]] std::unique_ptr<Buffer> release_data(
177  std::unique_ptr<Communicator::Future> future
178  ) override;
179 
186  [[nodiscard]] std::unique_ptr<std::vector<std::uint8_t>> release_sync_host_data(
187  std::unique_ptr<Communicator::Future> future
188  ) override;
189 
193  [[nodiscard]] std::shared_ptr<Logger> const& logger() override {
194  return logger_;
195  }
196 
200  [[nodiscard]] std::shared_ptr<ProgressThread> const&
201  progress_thread() const override {
202  return progress_thread_;
203  }
204 
208  [[nodiscard]] std::string str() const override;
209 
210  private:
211  std::shared_ptr<Logger> logger_;
212  std::shared_ptr<ProgressThread> progress_thread_;
213 };
214 
215 
216 } // namespace rapidsmpf
Abstract base class for asynchronous operation within the communicator.
Abstract base class for a communication mechanism between nodes.
Represents the future result of an operation.
Definition: single.hpp:36
Single process communicator class that implements the Communicator interface.
Definition: single.hpp:28
std::string str() const override
Provides a string representation of the communicator.
std::unique_ptr< Communicator::Future > recv_sync_host_data(Rank rank, Tag tag, std::unique_ptr< std::vector< std::uint8_t >> synced_buffer) override
Receives a message from a specific rank to an allocated (synchronized) host buffer....
std::shared_ptr< Logger > const & logger() override
Retrieves the logger associated with this communicator.
Definition: single.hpp:193
constexpr Rank rank() const override
Retrieves the rank of the current node.
Definition: single.hpp:59
std::unique_ptr< Communicator::Future > recv(Rank rank, Tag tag, std::unique_ptr< Buffer > recv_buffer) override
Receives a message from a specific rank to a buffer. Use release_data to extract the data out of the ...
std::vector< std::unique_ptr< Buffer > > wait_all(std::vector< std::unique_ptr< Communicator::Future >> &&futures) override
Wait for completion of all futures and return their data buffers.
bool test(std::unique_ptr< Communicator::Future > &future) override
Test for completion of a single future.
constexpr Rank nranks() const override
Retrieves the total number of ranks.
Definition: single.hpp:66
std::pair< std::vector< std::unique_ptr< Communicator::Future > >, std::vector< std::size_t > > test_some(std::vector< std::unique_ptr< Communicator::Future >> &future_vector) override
Tests for completion of multiple futures.
std::unique_ptr< std::vector< std::uint8_t > > recv_from(Rank src, Tag tag) override
Receives a message from a specific rank (blocking).
std::unique_ptr< Buffer > wait(std::unique_ptr< Communicator::Future > future) override
Wait for a future to complete and return the data buffer.
std::vector< std::size_t > test_some(std::unordered_map< std::size_t, std::unique_ptr< Communicator::Future >> const &future_map) override
Tests for completion of multiple futures in a map.
std::unique_ptr< Buffer > release_data(std::unique_ptr< Communicator::Future > future) override
Retrieves data associated with a completed future.
std::unique_ptr< Communicator::Future > send(std::unique_ptr< std::vector< std::uint8_t >> msg, Rank rank, Tag tag) override
Sends a host message to a specific rank.
std::unique_ptr< Communicator::Future > send(std::unique_ptr< Buffer > msg, Rank rank, Tag tag) override
Sends a message (device or host) to a specific rank. Use release_data to obtain the data buffer again...
Single(std::shared_ptr< ProgressThread > progress_thread, std::shared_ptr< Logger > logger)
Construct a single process communicator.
std::unique_ptr< std::vector< std::uint8_t > > release_sync_host_data(std::unique_ptr< Communicator::Future > future) override
Retrieves synchronized host data associated with a completed future. When the future is completed,...
std::shared_ptr< ProgressThread > const & progress_thread() const override
Retrieves the progress thread associated with this communicator.
Definition: single.hpp:201
std::pair< std::unique_ptr< std::vector< std::uint8_t > >, Rank > recv_any(Tag tag) override
Receives a message from any rank (blocking).
A tag used for identifying messages in a communication operation.
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:14
std::int32_t Rank
The rank of a node (e.g. the rank of a MPI process), or world size (total number of ranks).