-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduler.hpp
680 lines (603 loc) · 22.6 KB
/
Scheduler.hpp
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// DU3sch.hpp
// Jaroslav Jindrak NPRG051 2014/2015
#pragma once
#include <cstddef>
#include <random>
#include <limits>
#include <deque>
#include <map>
#include <memory>
#include <thread>
#include <atomic>
#include <future>
/**
* Note: This solution uses N asyncs and futures and N threads for non-void tasks.
* (2 * N thread and no asyncs for void tasks)
* Void task specializations are at the bottom of the source file.
*/
/**
* Brief: Forward declaration of the scheduler class.
*/
template<typename T, typename TASK>
class Scheduler;
/**
* Brief: Simple struct used to hold the result of a task and the
* information if the task has been completed already.
*/
template<typename T>
struct task_register
{
bool done{false};
T result;
};
/**
* Brief: Simple struct used to hold the task and it's id, so that the
* core executing the task knows where to store the result.
*/
template<typename TASK>
struct task_pack
{
TASK task;
std::size_t id;
};
/**
* Brief: Class representing a processor core in this program, has it's
* queue of tasks and is called by the Scheduler to execute them.
*/
template<typename T, typename TASK>
class core
{
public:
/**
* Brief: Default constructor of the core class.
*/
core()
: running{false}
{ /* DUMMY BODY */ }
/**
* Brief: Adds a task to the core's queue in a thread safe manner.
* Param: Task pack to be added, containing the task and it's id.
*/
void add_task(task_pack<TASK>&& task)
{
que_mutex_.lock();
queue_.push_back(task);
que_mutex_.unlock();
}
/**
* Brief: Returns the size of the queue of this core, used for
* choosing a core for a task and for choosing steal target
* in the Scheduler class.
*/
std::size_t queue_size()
{
return queue_.size();
}
/**
* Brief: Executes the task thats sits first in the queue and sets
* the result in the appropriate task register.
* Param: Pointer to the scheduler so the core can easily set the
* task result in the task register container.
*/
void execute(Scheduler<T, TASK>* sched)
{
task_pack<TASK> tmp_pack;
try
{
tmp_pack = pop_front();
}
catch(...)
{
// The queue got emptied after the call to this method.
return; // No need to continue here.
}
current_task_id = tmp_pack.id;
// Execute the task itself and store it's result in the Scheduler.
auto fut = std::async(std::move(tmp_pack.task));
sched->set_result(current_task_id, fut.get(), true);
}
/**
* Brief: Returns the first element in the queue and removes
* it from said queue in a thread safe manner.
*/
task_pack<TASK> pop_front()
{
// This method will be used for stealing and thus has to be
// thread safe.
que_mutex_.lock();
if(queue_.empty())
{
// The queue got emptied before this method locked the mutex,
// throw to signal the problem to the caller.
que_mutex_.unlock(); // But don't create a deadlock...
throw 0;
}
auto tmp = queue_.front();
queue_.pop_front();
que_mutex_.unlock();
return tmp;
}
std::atomic<bool> running; // Will be changed from the Scheduler so has
// to be atomic.
private:
std::deque<task_pack<TASK>> queue_; // The task queue.
std::mutex que_mutex_; // Mutex used to lock the queue.
std::size_t current_task_id; // ID of the current task in execution.
};
/**
* Brief: The main template version of the Scheduler class.
*/
template<typename T, typename TASK>
class Scheduler
{
public:
/**
* Brief: Main constructor for this class.
* Param: core_count Number of cores that can be used.
*/
Scheduler(std::size_t core_count)
: core_count_{core_count},
cores_{new core<T, TASK>[core_count_]},
core_threads_{new std::unique_ptr<std::thread>[core_count_]},
rd_device_{}, rd_engine_{rd_device_()},
rd_dist_{0,core_count - 1}
{ /* DUMMY BODY */ }
/**
* Brief: Copy constructor, created because of std::random_device
* and std::mutex because they have explicitly deleted
* copy constructor, making this class also non-copyable.
* Param: The other instance of this class. (const ref)
*/
Scheduler(const Scheduler<T, TASK>& o)
: core_count_{o.core_count_},
task_registers_{o.task_registers_},
rd_device_{},
rd_engine_{o.rd_engine_},
rd_dist_{o.rd_dist_},
cores_{new core<T, TASK>[core_count_]},
core_threads_{new std::unique_ptr<std::thread>[core_count_]},
register_mutex_{}
{ /* DUMMY BODY */ }
/**
* Brief: Destructor, deallocates heap objects.
*/
~Scheduler()
{
delete[] cores_;
delete[] core_threads_;
}
/**
* Brief: Adds task to a chosen core and if the core isn't running,
* resets it's thread to start the execution.
* Param: The task to be executed.
*/
std::size_t add_task(TASK task)
{
std::size_t core_id = get_core_id(); // Get a suitable core.
std::size_t task_id = get_task_id(); // Find the smallest id.
task_pack<TASK> tmp_pack;
tmp_pack.id = task_id;
tmp_pack.task = std::move(task);
set_result(task_id, T(), false); // Create a record for this task.
cores_[core_id].add_task(std::move(tmp_pack));
if(!cores_[core_id].running)
{ // Re-start the core thread if it's not running.
cores_[core_id].running = true;
core_threads_[core_id].reset(
new std::thread(&Scheduler<T, TASK>::run_core, this, core_id)
);
core_threads_[core_id]->detach();
}
return task_id;
}
/**
* Brief: Checks if a given task has completed it's execution.
* Param: ID of the task returned by the add_task method.
*/
bool is_task_ready(std::size_t index)
{
return task_registers_[index].done;
}
/**
* Brief: Returns the result of a given task and blocks (actively) if
* the task hasn't finished it's execution.
* Param: ID of the task returned by the add_task method.
* Note: Due to it's blocking nature, it is advised to call
* is_task_ready(std::size_t) before calling this method.
*/
T get_task_result(std::size_t index)
{
while(!task_registers_[index].done); // Active block.
T tmp = task_registers_[index].result;
/**
* Note: This mechanism will free indexes of tasks that have
* already returned their results for another use,
* theoretically increasing the number of overall tasks
* completed by this scheduler. It will, though, create
* small stall time when another thread want's to access
* the task register, because of this lock.
* So for a scheduler with small number of tasks expected,
* commenting out these three lines might improve the
* performance.
*/
register_mutex_.lock();
task_registers_.erase(task_registers_.find(index));
register_mutex_.unlock();
return std::move(tmp);
}
/**
* Brief: Sets the result of a given task.
* Param: ID of the task.
* Param: Result of the task.
* Param: Boolean indicating if the task has already completed.
* (Used as false for initial value.)
*/
void set_result(std::size_t id, T res, bool done)
{
register_mutex_.lock();
task_registers_[id].result = res;
task_registers_[id].done = done;
register_mutex_.unlock();
}
private:
/**
* Brief: Finds the core with the smallest queue
* and returns it's id (index in the core array).
*/
std::size_t get_core_id()
{
std::size_t id{0};
std::size_t min_queue_size{std::numeric_limits<std::size_t>::max()};
for(std::size_t i = 0; i < core_count_; ++i)
{
if(cores_[i].queue_size() < min_queue_size)
{ // Store the new minimum.
id = i;
min_queue_size = cores_[i].queue_size();
}
}
return id;
}
/**
* Brief: Method to be run in the core thread, if the core
* is running, executes it's oldest task in the queue
* (or steals a task if neccessary) and if the core has
* no tasks and none can be stolen, stops the core.
* Param: ID of the core to be run.
*/
void run_core(std::size_t core_id)
{
task_pack<TASK> tmp_task;
while(cores_[core_id].running)
{
if(cores_[core_id].queue_size() > 0)
{ // Still has unfinished tasks.
cores_[core_id].execute(this);
}
else if(can_steal())
{ // Steal from other cores (at random).
try
{
tmp_task = steal_task();
}
catch(...)
{
// The queue got emptied before steal_task() could
// finnish.
cores_[core_id].running = false;
return;
}
cores_[core_id].add_task(std::move(tmp_task));
cores_[core_id].execute(this);
}
else
{ // No task in the entire schedulet that could be run
// by this core.
cores_[core_id].running = false;
return;
}
}
}
/**
* Brief: Checks if there is a core that has non-empty queue,
* returns true if there is, false otherwise.
* NOTE: No need to check id against that of the caller, this function
* will be called only if the caller has empty queue.
*/
bool can_steal()
{
for(std::size_t i = 0; i < core_count_; ++i)
{
if(cores_[i].queue_size() > 0)
return true; // Found a core with free tasks.
}
return false;
}
/**
* Brief: Will steal a tank from a randomly chosen core that
* has non-empty queue of tasks.
* NOTE: Will be called after can_steal(), otherwise
* this function would block until there is
* a core with non-empty queue and a different id.
*/
task_pack<TASK> steal_task()
{
std::size_t id{get_rand()};
// It is not needed to check if the id is different than
// that of the caller, because the caller will call this
// function only when his queue is empty.
while(cores_[id].queue_size() == 0)
id = get_rand(); // Find a random core from those that
// have non-empty queue.
return cores_[id].pop_front();
}
/**
* Brief: Returns a random number in the range [0..core_count - 1],
* used to find a target for task stealing.
*/
std::size_t get_rand()
{
return rd_dist_(rd_engine_);
}
/**
* Brief: Finds the lowest key that is not used for a task. Although
* keeping and incremental counter for the ids is possible,
* if the scheduler would run longer the std::size_t would
* sometime reach it's maximum and in the worst case scenario
* the first task added (with id = 0) would still be running so
* simply using modulo is not good.
*/
std::size_t get_task_id()
{
std::size_t tmp_id = 0;
for(auto it = task_registers_.begin(); it != task_registers_.end();
++it)
{
// The map is ordered by it's keys, so this will find the first
// free key.
if(it->first == tmp_id)
++tmp_id; // Used, skip.
else
break; // Found!
}
return tmp_id;
}
std::size_t core_count_; // Number of cores available.
std::map<std::size_t, task_register<T>> task_registers_; // Results.
std::mutex register_mutex_; // Mutex that locks the result container.
/* Variables for the super simple random system from C++11! */
std::random_device rd_device_;
std::default_random_engine rd_engine_;
std::uniform_int_distribution<std::size_t> rd_dist_;
/* Will have fixed size (number of cores). */
core<T, TASK>* cores_;
std::unique_ptr<std::thread>* core_threads_;
};
/*****
* vvvvv SPECIALIZATION AREA BELOW vvvvv *
*****/
/**
* Brief: Void specialization of the core class.
* Note: Ommiting method description comments due to the similarities
* between this version and the main templated one.
*/
template<typename TASK>
class core<void, TASK>
{
public:
core()
: running{false}
{ /* DUMMY BODY */ }
void add_task(task_pack<TASK>&& task)
{
que_mutex_.lock();
queue_.push_back(task);
que_mutex_.unlock();
}
std::size_t queue_size()
{
return queue_.size();
}
void execute(Scheduler<void, TASK>* sched)
{
task_pack<TASK> tmp_pack;
try
{
tmp_pack = pop_front();
}
catch(...)
{
// The queue got emptied after the call to this method.
return; // No need to continue here.
}
current_task_id = tmp_pack.id;
std::thread th(std::move(tmp_pack.task));
th.join(); // Just wait for the procedure to finnish.
sched->set_result(current_task_id, 0, true);
}
task_pack<TASK> pop_front()
{
// This method will be used for stealing and thus has to be
// thread safe.
que_mutex_.lock();
if(queue_.empty())
{
// The queue got emptied before this method locked the mutex,
// throw to signal the problem to the caller.
que_mutex_.unlock();
throw 0;
}
auto tmp = queue_.front();
queue_.pop_front();
que_mutex_.unlock();
return tmp;
}
std::atomic<bool> running;
private:
std::deque<task_pack<TASK>> queue_;
std::mutex que_mutex_;
std::size_t current_task_id;
};
/**
* Brief: Void specialization of the scheduler class.
* Note: Ommiting method description comments due to the similarities
* between this version and the main templated one.
*/
template<typename TASK>
class Scheduler<void, TASK>
{
public:
Scheduler(std::size_t core_count)
: core_count_{core_count},
rd_device_{}, rd_engine_{rd_device_()},
rd_dist_{0, core_count_ - 1},
cores_{new core<void, TASK>[core_count_]},
core_threads_{new std::unique_ptr<std::thread>[core_count_]}
{ /* DUMMY BODY */ }
Scheduler(const Scheduler<void, TASK>& o)
: core_count_{o.core_count_},
task_registers_{o.task_registers_},
rd_device_{}, rd_engine_{o.rd_engine_},
rd_dist_{o.rd_dist_},
cores_{new core<void, TASK>[core_count_]},
core_threads_{new std::unique_ptr<std::thread>[core_count_]}
{ /* DUMMY BODY */ }
~Scheduler()
{
delete[] cores_;
delete[] core_threads_;
}
std::size_t add_task(TASK task)
{
std::size_t core_id = get_core_id(); // Get a suitable core.
std::size_t task_id = get_task_id(); // Find the smallest id.
task_pack<TASK> tmp_pack;
tmp_pack.id = task_id;
tmp_pack.task = std::move(task);
set_result(task_id, 0, false); // Create a record for this task.
cores_[core_id].add_task(std::move(tmp_pack));
if(!cores_[core_id].running)
{ // If the core is available, execute the task immedietly.
// The mutex will be unlocked at the end of the run_core method.
cores_[core_id].running = true;
core_threads_[core_id].reset(
new std::thread(&Scheduler<void, TASK>::run_core, this, core_id)
);
core_threads_[core_id]->detach();
}
return task_id;
}
bool is_task_ready(std::size_t index)
{
return task_registers_[index];
}
void get_task_result(std::size_t index)
{
// Since this will be a procedure, just block till it's done.
while(!task_registers_[index]);
return;
}
void set_result(std::size_t id, int dummy, bool done)
{ // Dummy will be zero, ignore it.
//std::cerr << "[" << id << "] Done." << std::endl;
register_mutex_.lock();
task_registers_[id] = done;
register_mutex_.unlock();
}
private:
std::size_t get_core_id()
{
std::size_t id{0};
std::size_t min_queue_size{std::numeric_limits<std::size_t>::max()};
for(std::size_t i = 0; i < core_count_; ++i)
{
if(cores_[i].queue_size() < min_queue_size)
{
id = i;
min_queue_size = cores_[i].queue_size();
}
}
return id;
}
void run_core(std::size_t core_id)
{
task_pack<TASK> tmp_task;
while(cores_[core_id].running)
{
if(cores_[core_id].queue_size() > 0)
{ // Still has unfinished tasks.
cores_[core_id].execute(this);
}
else if(can_steal())
{ // Steal from other cores (at random).
try
{
tmp_task = steal_task();
}
catch(...)
{
// The queue got emptied before steal_task() could
// finnish.
cores_[core_id].running = false;
break;
}
cores_[core_id].add_task(std::move(tmp_task));
cores_[core_id].execute(this);
}
else
{ // No task in the entire schedulet that could be run
// by this core.
cores_[core_id].running = false;
}
}
}
bool can_steal()
{
for(std::size_t i = 0; i < core_count_; ++i)
{
if(cores_[i].queue_size() > 0)
return true; // Found a core with free tasks.
}
return false;
}
task_pack<TASK> steal_task()
{
std::size_t id{get_rand()};
// It is not needed to check if the id is different than
// that of the caller, because the caller will call this
// function only when his queue is empty.
while(cores_[id].queue_size() == 0)
id = get_rand(); // Find a random core from those that
// have non-empty queue.
return cores_[id].pop_front();
}
std::size_t get_rand()
{
return rd_dist_(rd_engine_);
}
std::size_t get_task_id()
{
std::size_t tmp_id = 0;
for(auto it = task_registers_.begin(); it != task_registers_.end();
++it)
{
// The map is ordered by it's keys, so this will find the first
// free key.
if(it->first == tmp_id)
++tmp_id; // Used, skip.
else
break; // Found!
}
return tmp_id;
}
std::size_t core_count_;
std::map<std::size_t, bool> task_registers_;
std::mutex register_mutex_;
/* Variables for the super simple random system from C++11! */
std::random_device rd_device_;
std::default_random_engine rd_engine_;
std::uniform_int_distribution<std::size_t> rd_dist_;
/* Will have fixed size (number of cores). */
core<void, TASK>* cores_;
std::unique_ptr<std::thread>* core_threads_;
};