12#ifndef RABIT_ALLREDUCE_BASE_H_
13#define RABIT_ALLREDUCE_BASE_H_
24#ifdef RABIT_CXXTESTDEFS_H
26#define protected public
35 explicit Datatype(
size_t type_size) : type_size(type_size) {}
45 static const int kMagic = 0xff99;
50 virtual bool Init(
int argc,
char* argv[]);
52 virtual bool Shutdown();
58 virtual void SetParam(
const char *name,
const char *val);
69 return ring_prev->rank;
77 if (world_size == -1)
return 1;
82 return tracker_uri !=
"NULL";
102 void Allgather(
void *sendrecvbuf_,
size_t total_size,
size_t slice_begin,
103 size_t slice_end,
size_t size_prev_slice)
override {
104 if (world_size == 1 || world_size == -1) {
108 slice_end, size_prev_slice) ==
kSuccess,
109 "AllgatherRing failed");
123 void Allreduce(
void *sendrecvbuf_,
size_t type_nbytes,
size_t count,
125 void *prepare_arg =
nullptr)
override {
126 if (prepare_fun !=
nullptr) prepare_fun(prepare_arg);
127 if (world_size == 1 || world_size == -1)
return;
141 void Broadcast(
void *sendrecvbuf_,
size_t total_size,
int root)
override {
142 if (world_size == 1 || world_size == -1)
return;
160 return version_number;
167 if (hadoop_mode != 0) {
168 LOG(CONSOLE) <<
"reporter:status:Rabit Phase[" << version_number <<
"] Operation " << seq_counter <<
"\n";
205 int errsv = xgboost::system::LastError();
206 if (errsv == EAGAIN || errsv == EWOULDBLOCK || errsv == 0)
return kSuccess;
208 if (errsv == WSAEWOULDBLOCK)
return kSuccess;
209 if (errsv == WSAECONNRESET)
return kConnReset;
226 char *buffer_head {
nullptr};
228 size_t buffer_size {0};
232 void InitBuffer(
size_t type_nbytes,
size_t count,
233 size_t reduce_buffer_size) {
234 size_t n = (type_nbytes * count + 7)/ 8;
235 auto to = Min(reduce_buffer_size, n);
239 buffer_.size() *
sizeof(uint64_t) / type_nbytes * type_nbytes;
241 "too large type_nbytes=%lu, buffer_size=%lu",
242 type_nbytes, buffer_size);
244 buffer_head =
reinterpret_cast<char*
>(BeginPtr(buffer_));
247 inline void ResetSize() {
248 size_write = size_read = 0;
259 utils::Assert(buffer_head !=
nullptr,
"ReadToRingBuffer: buffer not allocated");
260 utils::Assert(size_read <= max_size_read,
"ReadToRingBuffer: max_size_read check");
261 size_t ngap = size_read - protect_start;
262 utils::Assert(ngap <= buffer_size,
"Allreduce: boundary check");
263 size_t offset = size_read % buffer_size;
264 size_t nmax = max_size_read - size_read;
265 nmax = Min(nmax, buffer_size - ngap);
266 nmax = Min(nmax, buffer_size - offset);
268 ssize_t len = sock.
Recv(buffer_head + offset, nmax);
274 size_read +=
static_cast<size_t>(len);
285 if (max_size == size_read)
return kSuccess;
286 char *p =
static_cast<char*
>(recvbuf_);
287 ssize_t len = sock.
Recv(p + size_read, max_size - size_read);
293 size_read +=
static_cast<size_t>(len);
303 const char *p =
static_cast<const char*
>(sendbuf_);
304 ssize_t len = sock.
Send(p + size_write, max_size - size_write);
306 size_write +=
static_cast<size_t>(len);
313 std::vector<uint64_t> buffer_;
320 std::vector<LinkRecord*> plinks;
324 inline size_t Size()
const {
325 return plinks.size();
398 size_t slice_begin,
size_t slice_end,
399 size_t size_prev_slice);
439 err_link = link;
return err;
446 int version_number {0};
455 std::vector<LinkRecord> all_links;
457 LinkRecord *err_link;
459 RefLinkVector tree_links;
461 LinkRecord *ring_prev, *ring_next;
464 std::vector<std::string> env_vars;
469 std::string host_uri;
471 std::string tracker_uri;
473 std::string dmlc_role;
477 size_t reduce_buffer_size;
481 size_t reduce_ring_mincount;
483 size_t tree_reduce_minsize;
491 std::chrono::seconds timeout_sec{std::chrono::seconds{1800}};
493 bool rabit_timeout =
false;
495 bool rabit_enable_tcp_no_delay =
false;
Definition allreduce_base.h:32
implementation of basic Allreduce engine
Definition allreduce_base.h:42
int GetWorldSize() const override
get rank
Definition allreduce_base.h:76
ReturnType TryReduceScatterRing(void *sendrecvbuf_, size_t type_nbytes, size_t count, ReduceFunction reducer)
perform in-place allreduce, reduce on the sendrecvbuf,
Definition allreduce_base.cc:847
ReturnType TryBroadcast(void *sendrecvbuf_, size_t size, int root)
broadcast data from root to all nodes, this function can fail,and will return the cause of failure
Definition allreduce_base.cc:668
void ReportStatus() const
report current status to the job tracker depending on the job tracker we are in
Definition allreduce_base.h:166
void TrackerPrint(const std::string &msg) override
print the msg in the tracker, this function can be used to communicate the information of the progres...
Definition allreduce_base.cc:145
ReturnType TryAllreduceTree(void *sendrecvbuf_, size_t type_nbytes, size_t count, ReduceFunction reducer)
perform in-place allreduce, on sendrecvbuf, this function implements tree-shape reduction
Definition allreduce_base.cc:473
void CheckPoint() override
Increase internal version number. Deprecated.
Definition allreduce_base.h:153
ReturnType ReportError(LinkRecord *link, ReturnType err)
function used to report error when a link goes wrong
Definition allreduce_base.h:438
ReturnType TryAllgatherRing(void *sendrecvbuf_, size_t total_size, size_t slice_begin, size_t slice_end, size_t size_prev_slice)
internal Allgather function, each node have a segment of data in the ring of sendrecvbuf,...
Definition allreduce_base.cc:763
ReturnType TryAllreduce(void *sendrecvbuf_, size_t type_nbytes, size_t count, ReduceFunction reducer)
perform in-place allreduce, on sendrecvbuf, this function can fail, and will return the cause of fail...
Definition allreduce_base.cc:451
int VersionNumber() const override
Definition allreduce_base.h:159
std::string GetHost() const override
get rank
Definition allreduce_base.h:85
int GetRingPrevRank() const override
get rank of previous node in ring topology
Definition allreduce_base.h:68
virtual void SetParam(const char *name, const char *val)
set parameters to the engine
Definition allreduce_base.cc:182
xgboost::collective::TCPSocket ConnectTracker() const
initialize connection to the tracker
Definition allreduce_base.cc:222
void Allreduce(void *sendrecvbuf_, size_t type_nbytes, size_t count, ReduceFunction reducer, PreprocFunction prepare_fun=nullptr, void *prepare_arg=nullptr) override
perform in-place allreduce, on sendrecvbuf this function is NOT thread-safe
Definition allreduce_base.h:123
int GetRank() const override
get rank
Definition allreduce_base.h:72
void Broadcast(void *sendrecvbuf_, size_t total_size, int root) override
broadcast data from root to all nodes
Definition allreduce_base.h:141
ReturnType TryAllreduceRing(void *sendrecvbuf_, size_t type_nbytes, size_t count, ReduceFunction reducer)
perform in-place allreduce, on sendrecvbuf use a ring based algorithm, reduce-scatter + allgather
Definition allreduce_base.cc:948
static ReturnType Errno2Return()
translate errno to return type
Definition allreduce_base.h:204
int LoadCheckPoint() override
deprecated
Definition allreduce_base.h:150
bool ReConnectLinks(const char *cmd="start")
connect to the tracker to fix the the missing links this function is also used when the engine start ...
Definition allreduce_base.cc:263
bool IsDistributed() const override
whether is distributed or not
Definition allreduce_base.h:81
void Allgather(void *sendrecvbuf_, size_t total_size, size_t slice_begin, size_t slice_end, size_t size_prev_slice) override
internal Allgather function, each node have a segment of data in the ring of sendrecvbuf,...
Definition allreduce_base.h:102
ReturnTypeEnum
enumeration of possible returning results from Try functions
Definition allreduce_base.h:174
@ kRecvZeroLen
received a zero length message
Definition allreduce_base.h:180
@ kGetExcept
another node which is not my neighbor go down, get Out-of-Band exception notification from my neighbo...
Definition allreduce_base.h:187
@ kSockError
a neighbor node go down, the connection is dropped
Definition allreduce_base.h:182
@ kConnReset
a link was reset by peer
Definition allreduce_base.h:178
@ kSuccess
execution is successful
Definition allreduce_base.h:176
interface of core Allreduce engine
Definition engine.h:22
void() PreprocFunction(void *arg)
Preprocessing function, that is called before AllReduce, used to prepare the data used by AllReduce.
Definition engine.h:29
void() ReduceFunction(const void *src, void *dst, int count, const MPI::Datatype &dtype)
reduce function, the same form of MPI reduce function is used, to be compatible with MPI interface In...
Definition engine.h:41
TCP socket for simple communication.
Definition socket.h:249
void Close()
Close the socket, called automatically in destructor if the socket is not closed.
Definition socket.h:504
auto Recv(void *buf, std::size_t len, std::int32_t flags=0)
receive data using the socket
Definition socket.h:489
auto Send(const void *buf_, std::size_t len, std::int32_t flags=0)
Send data using the socket.
Definition socket.h:478
This file defines the core interface of rabit library.
void Assert(bool exp, const char *fmt,...)
assert a condition is true, use this to handle debug information
Definition utils.h:79
namespace of rabit
Definition engine.h:18
Definition allreduce_base.h:215
ReturnType ReadToArray(void *recvbuf_, size_t max_size)
read data into array, this function can not be used together with ReadToRingBuffer a link can either ...
Definition allreduce_base.h:284
ReturnType WriteFromArray(const void *sendbuf_, size_t max_size)
write data in array to sock
Definition allreduce_base.h:302
ReturnType ReadToRingBuffer(size_t protect_start, size_t max_size_read)
read data into ring-buffer, with care not to existing useful override data position after protect_sta...
Definition allreduce_base.h:258
simple data structure that works like a vector but takes reference instead of space
Definition allreduce_base.h:319
struct return type to avoid implicit conversion to int/bool
Definition allreduce_base.h:190
ReturnTypeEnum value
internal return type
Definition allreduce_base.h:192
simple utils to support the code