forked from microsoft/Multiverso
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
233 lines (210 loc) · 7.31 KB
/
server.cpp
File metadata and controls
233 lines (210 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "multiverso/server.h"
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include "multiverso/actor.h"
#include "multiverso/dashboard.h"
#include "multiverso/multiverso.h"
#include "multiverso/table_interface.h"
#include "multiverso/io/io.h"
#include "multiverso/util/configure.h"
#include "multiverso/util/mt_queue.h"
#include "multiverso/zoo.h"
namespace multiverso {
MV_DEFINE_bool(sync, false, "sync or async");
MV_DEFINE_int(backup_worker_ratio, 0, "ratio% of backup workers, set 20 means 20%");
Server::Server() : Actor(actor::kServer) {
RegisterHandler(MsgType::Request_Get, std::bind(
&Server::ProcessGet, this, std::placeholders::_1));
RegisterHandler(MsgType::Request_Add, std::bind(
&Server::ProcessAdd, this, std::placeholders::_1));
}
int Server::RegisterTable(ServerTable* server_table) {
int id = static_cast<int>(store_.size());
store_.push_back(server_table);
return id;
}
void Server::ProcessGet(MessagePtr& msg) {
MONITOR_BEGIN(SERVER_PROCESS_GET);
if (msg->data().size() != 0) {
MessagePtr reply(msg->CreateReplyMessage());
int table_id = msg->table_id();
CHECK(table_id >= 0 && table_id < static_cast<int>(store_.size()));
store_[table_id]->ProcessGet(msg->data(), &reply->data());
SendTo(actor::kCommunicator, reply);
}
MONITOR_END(SERVER_PROCESS_GET);
}
void Server::ProcessAdd(MessagePtr& msg) {
MONITOR_BEGIN(SERVER_PROCESS_ADD)
if (msg->data().size() != 0) {
MessagePtr reply(msg->CreateReplyMessage());
int table_id = msg->table_id();
CHECK(table_id >= 0 && table_id < static_cast<int>(store_.size()));
store_[table_id]->ProcessAdd(msg->data());
SendTo(actor::kCommunicator, reply);
}
MONITOR_END(SERVER_PROCESS_ADD)
}
// The Sync Server implement logic to support Sync SGD training
// The implementation assumes all the workers will call same number
// of Add and/or Get requests
// The server promise all workers i-th Get will get the same parameters
// If worker k has add delta to server j times when its i-th Get
// then the server will return the parameter after all K
// workers finished their j-th update
class SyncServer : public Server {
public:
SyncServer() : Server() {
RegisterHandler(MsgType::Server_Finish_Train, std::bind(
&SyncServer::ProcessFinishTrain, this, std::placeholders::_1));
int num_worker = Zoo::Get()->num_workers();
worker_get_clocks_.reset(new VectorClock(num_worker));
worker_add_clocks_.reset(new VectorClock(num_worker));
num_waited_add_.resize(num_worker, 0);
}
// make some modification to suit to the sync server
// please not use in other place, may different with the general vector clock
class VectorClock {
public:
explicit VectorClock(int n) :
local_clock_(n, 0), global_clock_(0), size_(0) {}
// Return true when all clock reach a same number
virtual bool Update(int i) {
++local_clock_[i];
if (global_clock_ < *(std::min_element(std::begin(local_clock_),
std::end(local_clock_)))) {
++global_clock_;
if (global_clock_ == max_element()) {
return true;
}
}
return false;
}
virtual bool FinishTrain(int i) {
local_clock_[i] = std::numeric_limits<int>::max();
if (global_clock_ < min_element()) {
global_clock_ = min_element();
if (global_clock_ == max_element()) {
return true;
}
}
return false;
}
std::string DebugString() {
std::string rank = "rank :" + std::to_string(MV_Rank());
std::string os = rank + " global ";
os += std::to_string(global_clock_) + " local: ";
for (auto i : local_clock_) {
if (i == std::numeric_limits<int>::max()) os += "-1 ";
else os += std::to_string(i) + " ";
}
return os;
}
int local_clock(int i) const { return local_clock_[i]; }
int global_clock() const { return global_clock_; }
private:
int max_element() const {
int max = global_clock_;
for (auto val : local_clock_) {
max = (val != std::numeric_limits<int>::max() && val > max) ? val : max;
}
return max;
}
int min_element() const {
return *std::min_element(std::begin(local_clock_), std::end(local_clock_));
}
protected:
std::vector<int> local_clock_;
int global_clock_;
int size_;
};
protected:
void ProcessAdd(MessagePtr& msg) override {
// 1. Before add: cache faster worker
int worker = Zoo::Get()->rank_to_worker_id(msg->src());
if (worker_get_clocks_->local_clock(worker) >
worker_get_clocks_->global_clock()) {
msg_add_cache_.Push(msg);
++num_waited_add_[worker];
return;
}
// 2. Process Add
Server::ProcessAdd(msg);
// 3. After add: process cached process get if necessary
if (worker_add_clocks_->Update(worker)) {
CHECK(msg_add_cache_.Empty());
while (!msg_get_cache_.Empty()) {
MessagePtr get_msg;
CHECK(msg_get_cache_.TryPop(get_msg));
int get_worker = Zoo::Get()->rank_to_worker_id(get_msg->src());
Server::ProcessGet(get_msg);
CHECK(!worker_get_clocks_->Update(get_worker));
}
}
}
void ProcessGet(MessagePtr& msg) override {
// 1. Before get: cache faster worker
int worker = Zoo::Get()->rank_to_worker_id(msg->src());
if (worker_add_clocks_->local_clock(worker) >
worker_add_clocks_->global_clock() ||
num_waited_add_[worker] > 0) {
// Will wait for other worker finished Add
msg_get_cache_.Push(msg);
return;
}
// 2. Process Get
Server::ProcessGet(msg);
// 3. After get: process cached process add if necessary
if (worker_get_clocks_->Update(worker)) {
while (!msg_add_cache_.Empty()) {
MessagePtr add_msg;
CHECK(msg_add_cache_.TryPop(add_msg));
int add_worker = Zoo::Get()->rank_to_worker_id(add_msg->src());
Server::ProcessAdd(add_msg);
CHECK(!worker_add_clocks_->Update(add_worker));
--num_waited_add_[add_worker];
}
}
}
void ProcessFinishTrain(MessagePtr& msg) {
int worker = Zoo::Get()->rank_to_worker_id(msg->src());
if (worker_add_clocks_->FinishTrain(worker)) {
CHECK(msg_add_cache_.Empty());
while (!msg_get_cache_.Empty()) {
MessagePtr get_msg;
CHECK(msg_get_cache_.TryPop(get_msg));
int get_worker = Zoo::Get()->rank_to_worker_id(get_msg->src());
Server::ProcessGet(get_msg);
CHECK(!worker_get_clocks_->Update(get_worker));
}
}
if (worker_get_clocks_->FinishTrain(worker)) {
CHECK(msg_get_cache_.Empty());
while (!msg_add_cache_.Empty()) {
MessagePtr add_msg;
CHECK(msg_add_cache_.TryPop(add_msg));
int add_worker = Zoo::Get()->rank_to_worker_id(add_msg->src());
Server::ProcessAdd(add_msg);
CHECK(!worker_add_clocks_->Update(add_worker));
--num_waited_add_[add_worker];
}
}
}
private:
std::unique_ptr<VectorClock> worker_get_clocks_;
std::unique_ptr<VectorClock> worker_add_clocks_;
std::vector<int> num_waited_add_;
MtQueue<MessagePtr> msg_add_cache_;
MtQueue<MessagePtr> msg_get_cache_;
};
Server* Server::GetServer() {
if (!MV_CONFIG_sync) {
Log::Info("Create a async server\n");
return new Server();
}
Log::Info("Create a sync server\n");
return new SyncServer();
}
} // namespace multiverso