Back to Taskflow

Taskflow: A General

docs/worker_8hpp_source.html

4.1.09.2 KB
Original Source

| | 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

13namespace 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

36using DefaultNotifier = AtomicNotifier;

37#else

38using DefaultNotifier = NonblockingNotifier;

39#endif

40

41// ----------------------------------------------------------------------------

42// Class Definition: Worker

43// ----------------------------------------------------------------------------

44

55class Worker {

56

57friend class Executor;

58friend class Runtime;

59friend class WorkerView;

60

61using wsq_type = BoundedWSQ<Node*>;

62

63public:

64

72inline size_t id() const { return _id; }

73

78inline size_t queue_size() const { return _wsq.size(); }

79

83inline size_t queue_capacity() const { return static_cast<size_t>(_wsq.capacity()); }

84

88 std::thread& thread() { return _thread; }

89

90private:

91

92alignas(TF_CACHELINE_SIZE) std::atomic_flag _done = ATOMIC_FLAG_INIT;

93

94size_t _id;

95size_t _sticky_victim;

96Xorshift<uint32_t> _rdgen;

97 std::thread _thread;

98 wsq_type _wsq;

99//std::vector<Node*> _pool;

100};

101

102// ----------------------------------------------------------------------------

103// Class Definition: WorkerView

104// ----------------------------------------------------------------------------

105

116class WorkerView {

117

118friend class Executor;

119

120public:

121

129size_t id() const;

130

135size_t queue_size() const;

136

140size_t queue_capacity() const;

141

142private:

143

144 WorkerView(const Worker&);

145 WorkerView(const WorkerView&) = default;

146

147const Worker& _worker;

148

149};

150

151// Constructor

152inline WorkerView::WorkerView(const Worker& w) : _worker{w} {

153}

154

155// function: id

156inline size_t WorkerView::id() const {

157return _worker._id;

158}

159

160// Function: queue_size

161inline size_t WorkerView::queue_size() const {

162return _worker._wsq.size();

163}

164

165// Function: queue_capacity

166inline size_t WorkerView::queue_capacity() const {

167return static_cast<size_t>(_worker._wsq.capacity());

168}

169

170// ----------------------------------------------------------------------------

171// Class Definition: WorkerInterface

172// ----------------------------------------------------------------------------

173

275class WorkerInterface {

276

277public:

278

282virtual ~WorkerInterface() = default;

283

290virtual void scheduler_prologue(Worker& worker) = 0;

291

301virtual void scheduler_epilogue(Worker& worker, std::exception_ptr ptr) = 0;

302

303};

304

313template <typename T, typename... ArgsT>

314std::shared_ptr<T> make_worker_interface(ArgsT&&... args) {

315static_assert(

316 std::is_base_of_v<WorkerInterface, T>,

317"T must be derived from WorkerInterface"

318 );

319return std::make_shared<T>(std::forward<ArgsT>(args)...);

320}

321

322

323

324

325} // end of namespact tf ------------------------------------------------------

326

327

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:275

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:156

tf::WorkerView::queue_capacity

size_t queue_capacity() const

queries the current capacity of the queue

Definition worker.hpp:166

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:161

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:72

tf::Worker::queue_capacity

size_t queue_capacity() const

queries the current capacity of the queue

Definition worker.hpp:83

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:78

tf::Worker::thread

std::thread & thread()

acquires the associated thread

Definition worker.hpp:88

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:314

tf::DefaultNotifier

NonblockingNotifier DefaultNotifier

the default notifier type used by Taskflow

Definition worker.hpp:38