bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
communicator.h
Go to the documentation of this file.
1#ifndef BITRL_RL_DISTRIBUTED_COMMUNICATOR_H
2#define BITRL_RL_DISTRIBUTED_COMMUNICATOR_H
3
5
6#include <chrono>
7#include <cstring>
8#include <memory>
9#include <stdexcept>
10#include <string>
11#include <type_traits>
12#include <vector>
13
14namespace bitrl
15{
17{
18
32{
33 public:
34 virtual ~Communicator() = default;
35
36 virtual uint_t rank() const noexcept = 0;
37
38 virtual uint_t world_size() const noexcept = 0;
39
43 virtual std::vector<std::string> all_gather(const std::string &mine) = 0;
44
48 virtual std::string broadcast(const std::string &bytes, uint_t root) = 0;
49
53 template <typename T> void all_reduce_sum(std::vector<T> &values);
54
58 template <typename T> void broadcast(std::vector<T> &values, uint_t root);
59
63 void barrier() { all_gather(std::string()); }
64};
65
79{
80 public:
86 TCPStoreCommunicator(const std::string &host, uint_t port, uint_t rank, uint_t world_size,
87 std::chrono::milliseconds timeout = std::chrono::seconds(60));
88
90
91 uint_t rank() const noexcept override { return rank_; }
92
93 uint_t world_size() const noexcept override { return world_size_; }
94
95 std::vector<std::string> all_gather(const std::string &mine) override;
96
97 std::string broadcast(const std::string &bytes, uint_t root) override;
98
99 private:
100 struct Store;
101 std::string key_(uint_t call, const std::string &who) const;
102 void forget_(uint_t call);
103
104 std::unique_ptr<Store> store_;
105 uint_t rank_;
106 uint_t world_size_;
107 uint_t n_calls_{0};
108};
109
110template <typename T> void Communicator::all_reduce_sum(std::vector<T> &values)
111{
112 static_assert(std::is_arithmetic_v<T>, "all_reduce_sum needs numbers");
113 const std::string mine(reinterpret_cast<const char *>(values.data()),
114 values.size() * sizeof(T));
115 const auto all = all_gather(mine);
116 std::vector<T> sum(values.size(), T(0));
117 for (const auto &bytes : all)
118 {
119 if (bytes.size() != mine.size())
120 {
121 throw std::runtime_error("Communicator: the ranks sent values of different sizes");
122 }
123 std::vector<T> theirs(values.size());
124 std::memcpy(theirs.data(), bytes.data(), bytes.size());
125 for (std::size_t i = 0; i < sum.size(); ++i)
126 {
127 sum[i] += theirs[i];
128 }
129 }
130 values = std::move(sum);
131}
132
133template <typename T> void Communicator::broadcast(std::vector<T> &values, uint_t root)
134{
135 static_assert(std::is_arithmetic_v<T>, "broadcast needs numbers");
136 const std::string mine(reinterpret_cast<const char *>(values.data()),
137 values.size() * sizeof(T));
138 const auto bytes = broadcast(mine, root);
139 if (bytes.size() % sizeof(T) != 0)
140 {
141 throw std::runtime_error("Communicator: broadcast received a partial value");
142 }
143 values.resize(bytes.size() / sizeof(T));
144 std::memcpy(values.data(), bytes.data(), bytes.size());
145}
146
147} // namespace rl::distributed
148} // namespace bitrl
149
150#endif // BITRL_RL_DISTRIBUTED_COMMUNICATOR_H
How the ranks of a distributed training exchange numbers.
Definition communicator.h:32
virtual std::string broadcast(const std::string &bytes, uint_t root)=0
What root gave; the other ranks' bytes are ignored.
virtual uint_t rank() const noexcept=0
void all_reduce_sum(std::vector< T > &values)
Replace values with the sum of every rank's values.
Definition communicator.h:110
virtual uint_t world_size() const noexcept=0
virtual std::vector< std::string > all_gather(const std::string &mine)=0
What every rank gave, in the order of the ranks.
void barrier()
Return once every rank has called it.
Definition communicator.h:63
uint_t rank() const noexcept override
Definition communicator.h:91
std::vector< std::string > all_gather(const std::string &mine) override
What every rank gave, in the order of the ranks.
TCPStoreCommunicator(const std::string &host, uint_t port, uint_t rank, uint_t world_size, std::chrono::milliseconds timeout=std::chrono::seconds(60))
std::string broadcast(const std::string &bytes, uint_t root) override
What root gave; the other ranks' bytes are ignored.
uint_t world_size() const noexcept override
Definition communicator.h:93
Definition communicator.h:17
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
std::size_t uint_t
uint_t
Definition bitsim_types.h:40