| Taskflow: A General-purpose Task-parallel Programming System

Loading...
Searching...
No Matches
worker.hpp
1#pragma once
2
3#include "declarations.hpp"
4#include "wsq.hpp"
5#include "nonblocking_notifier.hpp"
6#include "atomic_notifier.hpp"
7
12
13 namespace tf {
14
15// ----------------------------------------------------------------------------
16// Default Notifier
17// Our experiments show that NonblockingNotifier has the most stable performance
18// ----------------------------------------------------------------------------
19
35#ifdef TF_ENABLE_ATOMIC_NOTIFIER
36 using DefaultNotifier = AtomicNotifier;
37#else
38 using DefaultNotifier = NonblockingNotifier;
39#endif
40
41// ----------------------------------------------------------------------------
42// Class Definition: Worker
43// ----------------------------------------------------------------------------
44
55 class Worker {
56
57 friend class Executor;
58 friend class Runtime;
59 friend class WorkerView;
60
61#ifdef TF_ENABLE_TASK_PRIORITY
62 using wsq_type = BoundedPriorityWSQ<BoundedWSQ<Node*>, 3>;
63#else
64 using wsq_type = BoundedWSQ<Node*>;
65#endif
66
67 public:
68
76 inline size_t id() const { return _id; }
77
82 inline size_t queue_size() const { return _wsq.size(); }
83
87 inline size_t queue_capacity() const { return static_cast<size_t>(_wsq.capacity()); }
88
92 std::thread& thread() { return _thread; }
93
94 private:
95
96 alignas(TF_CACHELINE_SIZE) std::atomic_flag _done = ATOMIC_FLAG_INIT;
97
98 size_t _id;
99 size_t _sticky_victim;
100 Xorshift<uint32_t> _rdgen;
101 std::thread _thread;
102 wsq_type _wsq;
103};
104
105// ----------------------------------------------------------------------------
106// Class Definition: WorkerView
107// ----------------------------------------------------------------------------
108
119 class WorkerView {
120
121 friend class Executor;
122
123 public:
124
132 size_t id() const;
133
138 size_t queue_size() const;
139
143 size_t queue_capacity() const;
144
145 private:
146
147 WorkerView(const Worker&);
148 WorkerView(const WorkerView&) = default;
149
150 const Worker& _worker;
151};
152
153// Constructor
154 inline WorkerView::WorkerView(const Worker& w) : _worker{w} {
155}
156
157// function: id
158 inline size_t WorkerView::id() const {
159 return _worker._id;
160}
161
162// Function: queue_size
163 inline size_t WorkerView::queue_size() const {
164 return _worker.queue_size();
165}
166
167// Function: queue_capacity
168 inline size_t WorkerView::queue_capacity() const {
169 return _worker.queue_capacity();
170}
171
172// ----------------------------------------------------------------------------
173// Class Definition: WorkerInterface
174// ----------------------------------------------------------------------------
175
277 class WorkerInterface {
278
279 public:
280
284 virtual ~WorkerInterface() = default;
285
292 virtual void scheduler_prologue(Worker& worker) = 0;
293
303 virtual void scheduler_epilogue(Worker& worker, std::exception_ptr ptr) = 0;
304
305};
306
315 template <typename T, typename... ArgsT>
316std::shared_ptr<T> make_worker_interface(ArgsT&&... args) {
317 static_assert(
318 std::is_base_of_v<WorkerInterface, T>,
319 "T must be derived from WorkerInterface"
320 );
321 return std::make_shared<T>(std::forward<ArgsT>(args)...);
322}
323
324
325
326
327} // end of namespact tf ------------------------------------------------------
328
329
tf::BoundedPriorityWSQ
class to create a lock-free bounded work-stealing queue with priority support
Definition wsq.hpp:983
tf::BoundedWSQ
class to create a lock-free bounded work-stealing queue
Definition wsq.hpp:559
tf::NonblockingNotifier
class to create a non-blocking notifier
Definition nonblocking_notifier.hpp:84
tf::WorkerInterface
class to configure worker behavior in an executor
Definition worker.hpp:277
tf::WorkerInterface::scheduler_epilogue
virtual void scheduler_epilogue(Worker &worker, std::exception_ptr ptr)=0
method to call after a worker leaves the scheduling loop
tf::WorkerInterface::scheduler_prologue
virtual void scheduler_prologue(Worker &worker)=0
method to call before a worker enters the scheduling loop
tf::WorkerInterface::~WorkerInterface
virtual ~WorkerInterface()=default
default destructor
tf::WorkerView::id
size_t id() const
queries the worker id associated with its parent executor
Definition worker.hpp:158
tf::WorkerView::queue_capacity
size_t queue_capacity() const
queries the current capacity of the queue
Definition worker.hpp:168
tf::WorkerView::queue_size
size_t queue_size() const
queries the size of the queue (i.e., number of pending tasks to run) associated with the worker
Definition worker.hpp:163
tf::Worker
class to create a worker in an executor
Definition worker.hpp:55
tf::Worker::id
size_t id() const
queries the worker id associated with its parent executor
Definition worker.hpp:76
tf::Worker::queue_capacity
size_t queue_capacity() const
queries the current capacity of the queue
Definition worker.hpp:87
tf::Worker::queue_size
size_t queue_size() const
queries the size of the queue (i.e., number of enqueued tasks to run) associated with the worker
Definition worker.hpp:82
tf::Worker::thread
std::thread & thread()
acquires the associated thread
Definition worker.hpp:92
tf::Xorshift
class to create a fast xorshift-based pseudo-random number generator
Definition math.hpp:412
tf
taskflow namespace
Definition small_vector.hpp:20
tf::make_worker_interface
std::shared_ptr< T > make_worker_interface(ArgsT &&... args)
helper function to create an instance derived from tf::WorkerInterface
Definition worker.hpp:316
tf::DefaultNotifier
NonblockingNotifier DefaultNotifier
the default notifier type used by Taskflow
Definition worker.hpp:38