This repository has been archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_impl.h
269 lines (238 loc) · 7.77 KB
/
worker_impl.h
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#pragma once
#include <cstdint>
#include <iostream>
#include <list>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
#include <absl/synchronization/mutex.h>
#include <absl/synchronization/notification.h>
#include "communicator.h"
#include "controller.h"
#include "cuda_helper.h"
#include "kvs.h"
#include "lkvs_impl.h"
#include "nccl_communicator.h"
#include "operator.h"
#include "thread_pool_impl.h"
namespace elf {
class WorkerConf {
public:
const int gpu;
const int64_t id;
const int64_t rank;
const int64_t size;
private:
ThreadPool pool;
absl::Mutex comms_mux;
std::unordered_map<std::string, std::unique_ptr<Communicator>> communicators;
std::shared_ptr<KeyValueStore> kvs;
int64_t todo;
absl::Notification ready_;
public:
WorkerConf(int gpu,
int64_t id,
int64_t rank,
int64_t size,
std::shared_ptr<KeyValueStore> kvs,
const std::vector<std::string> &identifiers)
: gpu(gpu), id(id), rank(rank), size(size), pool(8), kvs(kvs), todo(identifiers.size()) {
for (auto &identifier : identifiers) {
pool.Schedule([this, identifier]() { create_communicator_for_variable(identifier); });
}
if (!todo) {
ready_.Notify();
}
}
WorkerConf(const WorkerConf &) = delete;
bool ready() {
return ready_.HasBeenNotified();
}
void wait_ready() {
ready_.WaitForNotification();
}
bool schedule_allreduce(const std::string &identifier,
const void *in,
void *out,
size_t count,
Communicator::DataType type,
std::function<void()> done_callback) {
assert(ready());
auto communicator = communicators.at(identifier).get();
assert(communicator);
pool.Schedule([=]() {
CUDA_CHECK(cudaSetDevice(gpu));
communicator->allreduce(in, out, count, type);
done_callback();
});
return true;
}
bool schedule_broadcast(const std::string &identifier,
const void *in,
void *out,
size_t count,
Communicator::DataType type,
std::function<void()> done_callback) {
assert(ready());
auto communicator = communicators.at(identifier).get();
assert(communicator);
pool.Schedule([=]() {
CUDA_CHECK(cudaSetDevice(gpu));
communicator->broadcast(in, out, 0, count, type);
done_callback();
});
return true;
}
private:
void create_communicator_for_variable(std::string variable) {
CUDA_CHECK(cudaSetDevice(gpu));
auto comm = create_nccl_communicator(kvs.get(), variable, rank, size);
{
absl::MutexLock l(&comms_mux);
communicators[variable] = std::move(comm);
--todo;
if (todo == 0) {
ready_.Notify();
}
}
}
void teardown_communicators() {}
};
class ControllerKVSAdapter : public KeyValueStore {
Controller *ctrl;
int64_t conf_id;
public:
ControllerKVSAdapter(Controller *ctrl, int64_t conf_id) : ctrl(ctrl), conf_id(conf_id) {}
~ControllerKVSAdapter() override {}
void set(const std::string &k, const std::string &v) override {
ctrl->kv_set(conf_id, k, v);
}
std::shared_future<std::string> get(const std::string &k) override {
return ctrl->kv_get(conf_id, k);
}
};
class Worker {
Controller *ctrl;
int64_t id = -2222;
int gpu;
std::vector<std::string> global_variables;
std::vector<std::string> weight_variables;
std::list<WorkerConf> confs;
// XXX: the callbacks may stil be triggered after this object is deallocated before the worker
// is unregistered from the controller.
// find a proper way to fix it
absl::Mutex &conf_mux;
bool &deleted;
std::vector<std::string> identifiers;
decltype(confs)::iterator current_conf;
decltype(confs)::iterator ready_conf;
absl::Notification first_conf_pushed;
public:
Worker(Controller *ctrl)
: ctrl(ctrl), conf_mux(*(new absl::Mutex)), deleted(*(new bool(false))) {
CUDA_CHECK(cudaGetDevice(&gpu));
}
Worker(const Worker &) = delete;
~Worker() {
absl::MutexLock l(&conf_mux);
deleted = true;
}
void commit_and_join(const std::string &name = "") {
for (auto &gv : global_variables) {
identifiers.push_back(gv);
}
for (auto &wv : weight_variables) {
identifiers.push_back(wv);
}
id = ctrl->join(name, [this](Controller::UpdateData data) {
absl::MutexLock l(&conf_mux);
if (deleted) {
std::cerr << "[elf warning] calling callback of deleted Worker\n";
return;
}
confs.emplace_back(gpu, data.conf_id, data.rank, data.size,
std::make_shared<ControllerKVSAdapter>(ctrl, data.conf_id), identifiers);
if (!first_conf_pushed.HasBeenNotified()) {
first_conf_pushed.Notify();
}
});
{
first_conf_pushed.WaitForNotification();
confs.front().wait_ready();
ready_conf = confs.begin();
current_conf = confs.begin();
}
}
void leave() {
ctrl->leave(id);
}
class Allreduce : public Operator {
Worker &worker;
const std::string identifier;
public:
Allreduce(Worker &worker, const std::string &identifier)
: worker(worker), identifier(identifier) {}
~Allreduce() {}
bool execute_async(const void *in,
void *out,
size_t count,
Communicator::DataType type,
std::function<void()> done_callback) {
return worker.current_conf->schedule_allreduce(
identifier, in, out, count, type, done_callback);
}
};
class Broadcast : public Operator {
Worker &worker;
const std::string identifier;
public:
Broadcast(Worker &worker, const std::string &identifier)
: worker(worker), identifier(identifier) {}
~Broadcast() {}
bool execute_async(const void *in,
void *out,
size_t count,
Communicator::DataType type,
std::function<void()> done_callback) {
return worker.current_conf->schedule_broadcast(
identifier, in, out, count, type, done_callback);
}
};
std::unique_ptr<Operator> add_global_variable(std::string identifier) {
identifier = "[g]" + identifier;
global_variables.push_back(identifier);
return std::make_unique<Broadcast>(*this, identifier);
}
std::unique_ptr<Operator> add_weight_variable(std::string identifier) {
identifier = "[w]" + identifier;
weight_variables.push_back(identifier);
return std::make_unique<Allreduce>(*this, identifier);
}
// should_continue, requires_broadcast, shard_number
std::tuple<bool, bool, int64_t> begin_batch() {
assert(first_conf_pushed.HasBeenNotified());
{
absl::MutexLock l(&conf_mux);
for (auto next = ready_conf; next != confs.end(); ++next) {
if (next->ready()) {
ready_conf = next;
} else {
break;
}
}
}
int64_t batch_conf_id;
bool requires_broadcast;
std::tie(batch_conf_id, requires_broadcast) = ctrl->begin_batch(id, ready_conf->id).get();
if (batch_conf_id == -1) {
return {false, false, 0};
}
while (current_conf->id != batch_conf_id) {
current_conf++;
}
// TODO: gc
return {true, requires_broadcast, ctrl->get_shard()};
}
};
} // namespace elf