-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_scheduler.h
More file actions
307 lines (266 loc) · 8.68 KB
/
task_scheduler.h
File metadata and controls
307 lines (266 loc) · 8.68 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* Centralized scheduler that distribute tasks to all workers.
* The scheduler thread is dedicated to launching the task only.
* For each update task, a worker will be assigned to do the work.
* For each query task, all worker will be assigned to write back the result to the scheduler.
*/
#ifndef TASK_SCHEDULER_H
#define TASK_SCHEDULER_H
#include <iostream>
#include <thread>
#include <vector>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <functional>
#include <pthread.h>
#include <sched.h>
#include "fenwick.h"
#include "readerwriterqueue.h"
#include "generator.h"
using namespace moodycamel;
enum class TaskType { Update, Query, Sync, Finish };
struct Task {
TaskType type;
int index;
int value; // used only for updates
};
struct TaskQueue {
std::queue<Task> queue;
std::mutex mutex;
std::condition_variable cv;
};
void pin_thread_to_core(int core_id) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
int rc = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
if (rc != 0) {
std::cerr << "Error calling pthread_setaffinity_np: " << rc << std::endl;
}
}
class Scheduler {
public:
Scheduler(int num_workers, int tree_size, int batch_size)
: num_workers_(num_workers), tree_size_(tree_size), batch_size_(batch_size), results_(batch_size) {
local_trees_.reserve(num_workers);
task_queues_.reserve(num_workers);
for (int i = 0; i < num_workers_; ++i) {
local_trees_.emplace_back(FenwickTreeSequential(tree_size_));
workers_.emplace_back(&Scheduler::worker_loop, this, i, i+1);
task_queues_.emplace_back(std::make_unique<TaskQueue>());
}
}
void init() {
for (auto& val : results_) {
val.store(0, std::memory_order_relaxed);
}
sync_ = 0;
}
void submit_update(int index, int value) {
enqueue_task({TaskType::Update, index, value});
}
void submit_query(int index, int batch_id) {
broadcast_task({TaskType::Query, index, batch_id});
}
void sync() {
broadcast_task({TaskType::Sync, 0, 0});
while (sync_.load() != num_workers_);
return;
}
void shutdown() {
broadcast_task({TaskType::Finish, 0, 0});
for (auto& t : workers_) {
t.join();
}
}
int validate_sum() {
int res = 0;
for (int i = 0; i < batch_size_; i++) {
res += results_[i].load();
}
return res;
}
private:
int num_workers_;
int tree_size_;
int batch_size_;
int counter_ = 0;
std::vector<std::thread> workers_;
std::vector<std::unique_ptr<TaskQueue>> task_queues_;
std::vector<std::atomic<int>> results_;
std::vector<FenwickTreeSequential> local_trees_;
std::atomic<int> sync_ = 0;
void enqueue_task(Task task) {
int worker_id = counter_++ % num_workers_;
auto& q = *task_queues_[worker_id];
{
std::lock_guard<std::mutex> lock(q.mutex);
q.queue.push(task);
}
q.cv.notify_one();
}
void broadcast_task(Task task) {
for (int i = 0; i < num_workers_; ++i) {
auto& q = *task_queues_[i];
{
std::lock_guard<std::mutex> lock(q.mutex);
q.queue.push(task);
}
q.cv.notify_one();
}
}
void worker_loop(int worker_id, int core_id) {
pin_thread_to_core(core_id);
TaskQueue& q = *task_queues_[worker_id];
while (true) {
Task task;
{
std::unique_lock<std::mutex> lock(q.mutex);
q.cv.wait(lock, [&]() { return !q.queue.empty(); });
task = q.queue.front();
q.queue.pop();
}
if (task.type == TaskType::Update) {
local_trees_[worker_id].add(task.index, task.value);
} else if (task.type == TaskType::Query) {
int result = local_trees_[worker_id].sum(task.index);
results_[task.value].fetch_add(result, std::memory_order_relaxed);
} else if (task.type == TaskType::Sync) {
sync_++;
} else if (task.type == TaskType::Finish) {
return;
}
}
}
};
class LockFreeScheduler {
public:
LockFreeScheduler(int num_workers, int tree_size, int batch_size)
: num_workers_(num_workers), tree_size_(tree_size), batch_size_(batch_size), results_(batch_size) {
local_trees_.reserve(num_workers);
task_queues_.reserve(num_workers);
for (int i = 0; i < num_workers_; ++i) {
local_trees_.emplace_back(FenwickTreeSequential(tree_size_));
workers_.emplace_back(&LockFreeScheduler::worker_loop, this, i, i+1);
task_queues_.emplace_back(BlockingReaderWriterQueue<Task>(100));
}
}
void init() {
for (auto& val : results_) {
val.store(0, std::memory_order_relaxed);
}
sync_ = 0;
}
void submit_update(int index, int value) {
enqueue_task({TaskType::Update, index, value});
}
void submit_query(int index, int batch_id) {
broadcast_task({TaskType::Query, index, batch_id});
}
void sync() {
broadcast_task({TaskType::Sync, 0, 0});
while (sync_.load() != num_workers_);
return;
}
void shutdown() {
broadcast_task({TaskType::Finish, 0, 0});
for (auto& t : workers_) {
t.join();
}
}
int validate_sum() {
int res = 0;
for (int i = 0; i < batch_size_; i++) {
res += results_[i].load();
}
return res;
}
private:
int num_workers_;
int tree_size_;
int batch_size_;
int counter_ = 0;
std::vector<std::thread> workers_;
std::vector<BlockingReaderWriterQueue<Task>> task_queues_;
std::vector<std::atomic<int>> results_;
std::vector<FenwickTreeSequential> local_trees_;
std::atomic<int> sync_ = 0;
void enqueue_task(Task task) {
int worker_id = counter_++ % num_workers_;
auto& q = task_queues_[worker_id];
q.enqueue(task);
}
void broadcast_task(Task task) {
for (int i = 0; i < num_workers_; ++i) {
auto& q = task_queues_[i];
q.enqueue(task);
}
}
void worker_loop(int worker_id, int core_id) {
pin_thread_to_core(core_id);
auto& q = task_queues_[worker_id];
while (true) {
Task task;
q.wait_dequeue(task);
if (task.type == TaskType::Update) {
local_trees_[worker_id].add(task.index, task.value);
} else if (task.type == TaskType::Query) {
int result = local_trees_[worker_id].sum(task.index);
results_[task.value].fetch_add(result, std::memory_order_relaxed);
} else if (task.type == TaskType::Sync) {
sync_++;
} else if (task.type == TaskType::Finish) {
return;
}
}
}
};
class DecentralizedScheduler {
public:
DecentralizedScheduler(int num_workers, int batch_size,
std::vector<Operation>& operations, std::vector<FenwickTreeSequential>& local_trees)
: num_workers_(num_workers), batch_size_(batch_size), results_(batch_size) {
for (int i = 0; i < num_workers_; ++i) {
results_[i] = std::vector<int>(batch_size_);
}
for (int i = 0; i < num_workers_; ++i) {
workers_.emplace_back(&DecentralizedScheduler::worker_loop, this, i, i+1, std::ref(operations), std::ref(local_trees[i]));
}
}
void sync() {
for (auto& t : workers_) {
t.join();
}
}
int validate_sum() {
int res = 0;
for (int i = 0; i < num_workers_; i++) {
for (int j = 0; j < batch_size_; j++) {
res += results_[i][j];
}
}
return res;
}
private:
int num_workers_;
int batch_size_;
std::vector<std::thread> workers_;
std::vector<std::vector<int>> results_;
void worker_loop(int worker_id, int core_id, std::vector<Operation>& operations, FenwickTreeSequential& local_tree) {
pin_thread_to_core(core_id);
int counter = 0;
for (size_t i = 0; i < (size_t)batch_size_; ++i) {
const auto& op = operations[i];
if (op.command == 'a') {
if (counter++ % num_workers_ == worker_id) {
local_tree.add(op.index, op.value);
}
} else {
results_[worker_id][i] = local_tree.sum(op.index);
}
}
}
};
#endif