| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#pragma once |
| 7 |
#include <xgboost/collective/result.h> |
| 8 |
#include <xgboost/collective/socket.h> |
| 9 |
|
| 10 |
#if defined(_WIN32) |
| 11 |
#include <xgboost/windefs.h> |
| 12 |
|
| 13 |
#include <winsock2.h> |
| 14 |
#include <ws2tcpip.h> |
| 15 |
#else |
| 16 |
|
| 17 |
#include <arpa/inet.h> |
| 18 |
#include <fcntl.h> |
| 19 |
#include <netdb.h> |
| 20 |
#include <netinet/in.h> |
| 21 |
#include <sys/ioctl.h> |
| 22 |
#include <sys/socket.h> |
| 23 |
#include <unistd.h> |
| 24 |
|
| 25 |
#include <cerrno> |
| 26 |
|
| 27 |
#endif |
| 28 |
|
| 29 |
#include <chrono> |
| 30 |
#include <cstring> |
| 31 |
#include <string> |
| 32 |
#include <system_error> |
| 33 |
#include <unordered_map> |
| 34 |
#include <vector> |
| 35 |
|
| 36 |
#if !defined(_WIN32) |
| 37 |
|
| 38 |
#include <poll.h> |
| 39 |
|
| 40 |
using SOCKET = int; |
| 41 |
using sock_size_t = size_t; |
| 42 |
#endif |
| 43 |
|
| 44 |
#define IS_MINGW() defined(__MINGW32__) |
| 45 |
|
| 46 |
#if IS_MINGW() && !defined(POLLRDNORM) && !defined(POLLRDBAND) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#pragma message("Distributed training on mingw is not supported.") |
| 62 |
typedef struct pollfd { |
| 63 |
SOCKET fd; |
| 64 |
short events; |
| 65 |
short revents; |
| 66 |
} WSAPOLLFD, *PWSAPOLLFD, *LPWSAPOLLFD; |
| 67 |
|
| 68 |
|
| 69 |
#define POLLIN (0x0100 | 0x0200) |
| 70 |
#define POLLPRI 0x0400 |
| 71 |
|
| 72 |
#define POLLOUT 0x0010 |
| 73 |
|
| 74 |
#endif |
| 75 |
|
| 76 |
namespace rabit { |
| 77 |
namespace utils { |
| 78 |
|
| 79 |
template <typename PollFD> |
| 80 |
int PollImpl(PollFD* pfd, int nfds, std::chrono::seconds timeout) noexcept(true) { |
| 81 |
|
| 82 |
|
| 83 |
#if defined(_WIN32) |
| 84 |
|
| 85 |
#if IS_MINGW() |
| 86 |
xgboost::MingWError(); |
| 87 |
return -1; |
| 88 |
#else |
| 89 |
return WSAPoll(pfd, nfds, std::chrono::milliseconds(timeout).count()); |
| 90 |
#endif |
| 91 |
|
| 92 |
#else |
| 93 |
return poll(pfd, nfds, timeout.count() < 0 ? -1 : std::chrono::milliseconds(timeout).count()); |
| 94 |
#endif |
| 95 |
} |
| 96 |
|
| 97 |
template <typename E> |
| 98 |
std::enable_if_t<std::is_integral_v<E>, xgboost::collective::Result> PollError(E const& revents) { |
| 99 |
if ((revents & POLLERR) != 0) { |
| 100 |
auto err = errno; |
| 101 |
auto str = strerror(err); |
| 102 |
return xgboost::system::FailWithCode(std::string{"Poll error condition:"} + |
| 103 |
std::string{str} + |
| 104 |
" code:" + std::to_string(err)); |
| 105 |
} |
| 106 |
if ((revents & POLLNVAL) != 0) { |
| 107 |
return xgboost::system::FailWithCode("Invalid polling request."); |
| 108 |
} |
| 109 |
if ((revents & POLLHUP) != 0) { |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
return xgboost::collective::Success(); |
| 120 |
} |
| 121 |
#if defined(POLLRDHUP) |
| 122 |
|
| 123 |
if ((revents & POLLRDHUP) != 0) { |
| 124 |
return xgboost::system::FailWithCode("Poll hung up on the other end."); |
| 125 |
} |
| 126 |
#endif |
| 127 |
return xgboost::collective::Success(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
struct PollHelper { |
| 132 |
public: |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
inline void WatchRead(SOCKET fd) { |
| 138 |
auto& pfd = fds[fd]; |
| 139 |
pfd.fd = fd; |
| 140 |
pfd.events |= POLLIN; |
| 141 |
} |
| 142 |
void WatchRead(xgboost::collective::TCPSocket const &socket) { this->WatchRead(socket.Handle()); } |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
inline void WatchWrite(SOCKET fd) { |
| 149 |
auto& pfd = fds[fd]; |
| 150 |
pfd.fd = fd; |
| 151 |
pfd.events |= POLLOUT; |
| 152 |
} |
| 153 |
void WatchWrite(xgboost::collective::TCPSocket const &socket) { |
| 154 |
this->WatchWrite(socket.Handle()); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
inline void WatchException(SOCKET fd) { |
| 162 |
auto& pfd = fds[fd]; |
| 163 |
pfd.fd = fd; |
| 164 |
pfd.events |= POLLPRI; |
| 165 |
} |
| 166 |
void WatchException(xgboost::collective::TCPSocket const &socket) { |
| 167 |
this->WatchException(socket.Handle()); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
[[nodiscard]] bool CheckRead(SOCKET fd) const { |
| 174 |
const auto& pfd = fds.find(fd); |
| 175 |
return pfd != fds.end() && ((pfd->second.events & POLLIN) != 0); |
| 176 |
} |
| 177 |
[[nodiscard]] bool CheckRead(xgboost::collective::TCPSocket const& socket) const { |
| 178 |
return this->CheckRead(socket.Handle()); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
[[nodiscard]] bool CheckWrite(SOCKET fd) const { |
| 186 |
const auto& pfd = fds.find(fd); |
| 187 |
return pfd != fds.end() && ((pfd->second.events & POLLOUT) != 0); |
| 188 |
} |
| 189 |
[[nodiscard]] bool CheckWrite(xgboost::collective::TCPSocket const& socket) const { |
| 190 |
return this->CheckWrite(socket.Handle()); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
[[nodiscard]] xgboost::collective::Result Poll(std::chrono::seconds timeout, |
| 198 |
bool check_error = true) { |
| 199 |
std::vector<pollfd> fdset; |
| 200 |
fdset.reserve(fds.size()); |
| 201 |
for (auto kv : fds) { |
| 202 |
fdset.push_back(kv.second); |
| 203 |
} |
| 204 |
std::int32_t ret = PollImpl(fdset.data(), fdset.size(), timeout); |
| 205 |
if (ret == 0) { |
| 206 |
return xgboost::collective::Fail( |
| 207 |
"Poll timeout:" + std::to_string(timeout.count()) + " seconds.", |
| 208 |
std::make_error_code(std::errc::timed_out)); |
| 209 |
} else if (ret < 0) { |
| 210 |
return xgboost::system::FailWithCode("Poll failed, nfds:" + std::to_string(fdset.size())); |
| 211 |
} |
| 212 |
|
| 213 |
for (auto& pfd : fdset) { |
| 214 |
auto result = PollError(pfd.revents); |
| 215 |
if (check_error && !result.OK()) { |
| 216 |
return result; |
| 217 |
} |
| 218 |
|
| 219 |
auto revents = pfd.revents & pfd.events; |
| 220 |
fds[pfd.fd].events = revents; |
| 221 |
} |
| 222 |
return xgboost::collective::Success(); |
| 223 |
} |
| 224 |
|
| 225 |
std::unordered_map<SOCKET, pollfd> fds; |
| 226 |
}; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
#if IS_MINGW() && !defined(POLLRDNORM) && !defined(POLLRDBAND) |
| 231 |
#undef POLLIN |
| 232 |
#undef POLLPRI |
| 233 |
#undef POLLOUT |
| 234 |
#endif |