1 /* A thread pool (C++ version). */
3 /* Copyright (c) 2004-2017 The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #define XBT_PARMAP_HPP
12 #include "src/internal_config.h" // HAVE_FUTEX_H
13 #include "src/kernel/context/Context.hpp"
15 #include <boost/optional.hpp>
16 #include <simgrid/simix.h>
19 #include <xbt/parmap.h>
20 #include <xbt/xbt_os_thread.h>
24 #include <linux/futex.h>
25 #include <sys/syscall.h>
28 XBT_LOG_EXTERNAL_CATEGORY(xbt_parmap);
33 /** \addtogroup XBT_parmap
35 * \brief Parallel map class
38 template <typename T> class Parmap {
40 Parmap(unsigned num_workers, e_xbt_parmap_mode_t mode);
42 void apply(void (*fun)(T), const std::vector<T>& data);
43 boost::optional<T> next();
46 enum Flag { PARMAP_WORK, PARMAP_DESTROY };
49 * \brief Thread data transmission structure
53 ThreadData(Parmap<T>& parmap, int id) : parmap(parmap), worker_id(id) {}
59 * \brief Synchronization object (different specializations).
63 explicit Synchro(Parmap<T>& parmap) : parmap(parmap) {}
66 * \brief Wakes all workers and waits for them to finish the tasks.
68 * This function is called by the controller thread.
70 virtual void master_signal() = 0;
72 * \brief Starts the parmap: waits for all workers to be ready and returns.
74 * This function is called by the controller thread.
76 virtual void master_wait() = 0;
78 * \brief Ends the parmap: wakes the controller thread when all workers terminate.
80 * This function is called by all worker threads when they end (not including the controller).
82 virtual void worker_signal() = 0;
84 * \brief Waits for some work to process.
86 * This function is called by each worker thread (not including the controller) when it has no more work to do.
88 * \param round the expected round number
90 virtual void worker_wait(unsigned) = 0;
96 class PosixSynchro : public Synchro {
98 explicit PosixSynchro(Parmap<T>& parmap);
100 void master_signal();
102 void worker_signal();
103 void worker_wait(unsigned round);
106 xbt_os_cond_t ready_cond;
107 xbt_os_mutex_t ready_mutex;
108 xbt_os_cond_t done_cond;
109 xbt_os_mutex_t done_mutex;
113 class FutexSynchro : public Synchro {
115 explicit FutexSynchro(Parmap<T>& parmap) : Synchro(parmap) {}
116 void master_signal();
118 void worker_signal();
119 void worker_wait(unsigned);
122 static void futex_wait(unsigned* uaddr, unsigned val);
123 static void futex_wake(unsigned* uaddr, unsigned val);
127 class BusyWaitSynchro : public Synchro {
129 explicit BusyWaitSynchro(Parmap<T>& parmap) : Synchro(parmap) {}
130 void master_signal();
132 void worker_signal();
133 void worker_wait(unsigned);
136 static void* worker_main(void* arg);
137 Synchro* new_synchro(e_xbt_parmap_mode_t mode);
140 Flag status; /**< is the parmap active or being destroyed? */
141 unsigned work_round; /**< index of the current round */
142 unsigned thread_counter; /**< number of workers that have done the work */
143 unsigned num_workers; /**< total number of worker threads including the controller */
144 xbt_os_thread_t* workers; /**< worker thread handlers */
145 void (*fun)(const T); /**< function to run in parallel on each element of data */
146 const std::vector<T>* data; /**< parameters to pass to fun in parallel */
147 std::atomic<unsigned> index; /**< index of the next element of data to pick */
148 Synchro* synchro; /**< synchronization object */
152 * \brief Creates a parallel map object
153 * \param num_workers number of worker threads to create
154 * \param mode how to synchronize the worker threads
156 template <typename T> Parmap<T>::Parmap(unsigned num_workers, e_xbt_parmap_mode_t mode)
158 XBT_CDEBUG(xbt_parmap, "Create new parmap (%u workers)", num_workers);
160 /* Initialize the thread pool data structure */
161 this->status = PARMAP_WORK;
162 this->work_round = 0;
163 this->workers = new xbt_os_thread_t[num_workers];
164 this->num_workers = num_workers;
165 this->synchro = new_synchro(mode);
167 /* Create the pool of worker threads */
168 this->workers[0] = nullptr;
169 #if HAVE_PTHREAD_SETAFFINITY
172 for (unsigned i = 1; i < num_workers; i++) {
173 ThreadData* data = new ThreadData(*this, i);
174 this->workers[i] = xbt_os_thread_create(nullptr, worker_main, data, nullptr);
175 #if HAVE_PTHREAD_SETAFFINITY
176 xbt_os_thread_bind(this->workers[i], core_bind);
177 if (core_bind != xbt_os_get_numcores() - 1)
186 * \brief Destroys a parmap
188 template <typename T> Parmap<T>::~Parmap()
190 status = PARMAP_DESTROY;
191 synchro->master_signal();
193 for (unsigned i = 1; i < num_workers; i++)
194 xbt_os_thread_join(workers[i], nullptr);
201 * \brief Applies a list of tasks in parallel.
202 * \param fun the function to call in parallel
203 * \param data each element of this vector will be passed as an argument to fun
205 template <typename T> void Parmap<T>::apply(void (*fun)(T), const std::vector<T>& data)
207 /* Assign resources to worker threads (we are maestro here)*/
211 this->synchro->master_signal(); // maestro runs futex_wait to wake all the minions (the working threads)
212 this->work(); // maestro works with its minions
213 this->synchro->master_wait(); // When there is no more work to do, then maestro waits for the last minion to stop
214 XBT_CDEBUG(xbt_parmap, "Job done"); // ... and proceeds
218 * \brief Returns a next task to process.
220 * Worker threads call this function to get more work.
222 * \return the next task to process, or throws a std::out_of_range exception if there is no more work
224 template <typename T> boost::optional<T> Parmap<T>::next()
226 unsigned index = this->index++;
227 if (index < this->data->size())
228 return (*this->data)[index];
234 * \brief Main work loop: applies fun to elements in turn.
236 template <typename T> void Parmap<T>::work()
238 unsigned index = this->index++;
239 unsigned length = this->data->size();
240 while (index < length) {
241 this->fun((*this->data)[index]);
242 index = this->index++;
247 * Get a synchronization object for given mode.
248 * \param mode the synchronization mode
250 template <typename T> typename Parmap<T>::Synchro* Parmap<T>::new_synchro(e_xbt_parmap_mode_t mode)
252 if (mode == XBT_PARMAP_DEFAULT) {
254 mode = XBT_PARMAP_FUTEX;
256 mode = XBT_PARMAP_POSIX;
261 case XBT_PARMAP_POSIX:
262 res = new PosixSynchro(*this);
264 case XBT_PARMAP_FUTEX:
266 res = new FutexSynchro(*this);
268 xbt_die("Fute is not available on this OS.");
271 case XBT_PARMAP_BUSY_WAIT:
272 res = new BusyWaitSynchro(*this);
281 * \brief Main function of a worker thread.
283 template <typename T> void* Parmap<T>::worker_main(void* arg)
285 ThreadData* data = static_cast<ThreadData*>(arg);
286 Parmap<T>& parmap = data->parmap;
288 smx_context_t context = SIMIX_context_new(std::function<void()>(), nullptr, nullptr);
289 SIMIX_context_set_current(context);
291 XBT_CDEBUG(xbt_parmap, "New worker thread created");
293 /* Worker's main loop */
296 parmap.synchro->worker_wait(round);
297 if (parmap.status == PARMAP_DESTROY)
300 XBT_CDEBUG(xbt_parmap, "Worker %d got a job", data->worker_id);
302 parmap.synchro->worker_signal();
303 XBT_CDEBUG(xbt_parmap, "Worker %d has finished", data->worker_id);
305 /* We are destroying the parmap */
311 template <typename T> Parmap<T>::PosixSynchro::PosixSynchro(Parmap<T>& parmap) : Synchro(parmap)
313 ready_cond = xbt_os_cond_init();
314 ready_mutex = xbt_os_mutex_init();
315 done_cond = xbt_os_cond_init();
316 done_mutex = xbt_os_mutex_init();
319 template <typename T> Parmap<T>::PosixSynchro::~PosixSynchro()
321 xbt_os_cond_destroy(ready_cond);
322 xbt_os_mutex_destroy(ready_mutex);
323 xbt_os_cond_destroy(done_cond);
324 xbt_os_mutex_destroy(done_mutex);
327 template <typename T> void Parmap<T>::PosixSynchro::master_signal()
329 xbt_os_mutex_acquire(ready_mutex);
330 this->parmap.thread_counter = 1;
331 this->parmap.work_round++;
332 /* wake all workers */
333 xbt_os_cond_broadcast(ready_cond);
334 xbt_os_mutex_release(ready_mutex);
337 template <typename T> void Parmap<T>::PosixSynchro::master_wait()
339 xbt_os_mutex_acquire(done_mutex);
340 if (this->parmap.thread_counter < this->parmap.num_workers) {
341 /* wait for all workers to be ready */
342 xbt_os_cond_wait(done_cond, done_mutex);
344 xbt_os_mutex_release(done_mutex);
347 template <typename T> void Parmap<T>::PosixSynchro::worker_signal()
349 xbt_os_mutex_acquire(done_mutex);
350 this->parmap.thread_counter++;
351 if (this->parmap.thread_counter == this->parmap.num_workers) {
352 /* all workers have finished, wake the controller */
353 xbt_os_cond_signal(done_cond);
355 xbt_os_mutex_release(done_mutex);
358 template <typename T> void Parmap<T>::PosixSynchro::worker_wait(unsigned round)
360 xbt_os_mutex_acquire(ready_mutex);
361 /* wait for more work */
362 if (this->parmap.work_round != round) {
363 xbt_os_cond_wait(ready_cond, ready_mutex);
365 xbt_os_mutex_release(ready_mutex);
369 template <typename T> inline void Parmap<T>::FutexSynchro::futex_wait(unsigned* uaddr, unsigned val)
371 XBT_CVERB(xbt_parmap, "Waiting on futex %p", uaddr);
372 syscall(SYS_futex, uaddr, FUTEX_WAIT_PRIVATE, val, nullptr, nullptr, 0);
375 template <typename T> inline void Parmap<T>::FutexSynchro::futex_wake(unsigned* uaddr, unsigned val)
377 XBT_CVERB(xbt_parmap, "Waking futex %p", uaddr);
378 syscall(SYS_futex, uaddr, FUTEX_WAKE_PRIVATE, val, nullptr, nullptr, 0);
381 template <typename T> void Parmap<T>::FutexSynchro::master_signal()
383 this->parmap.thread_counter = 1;
384 __sync_add_and_fetch(&this->parmap.work_round, 1);
385 /* wake all workers */
386 futex_wake(&this->parmap.work_round, std::numeric_limits<int>::max());
389 template <typename T> void Parmap<T>::FutexSynchro::master_wait()
391 unsigned count = this->parmap.thread_counter;
392 while (count < this->parmap.num_workers) {
393 /* wait for all workers to be ready */
394 futex_wait(&this->parmap.thread_counter, count);
395 count = this->parmap.thread_counter;
399 template <typename T> void Parmap<T>::FutexSynchro::worker_signal()
401 unsigned count = __sync_add_and_fetch(&this->parmap.thread_counter, 1);
402 if (count == this->parmap.num_workers) {
403 /* all workers have finished, wake the controller */
404 futex_wake(&this->parmap.thread_counter, std::numeric_limits<int>::max());
408 template <typename T> void Parmap<T>::FutexSynchro::worker_wait(unsigned round)
410 unsigned work_round = this->parmap.work_round;
411 /* wait for more work */
412 while (work_round != round) {
413 futex_wait(&this->parmap.work_round, work_round);
414 work_round = this->parmap.work_round;
419 template <typename T> void Parmap<T>::BusyWaitSynchro::master_signal()
421 this->parmap.thread_counter = 1;
422 __sync_add_and_fetch(&this->parmap.work_round, 1);
425 template <typename T> void Parmap<T>::BusyWaitSynchro::master_wait()
427 while (this->parmap.thread_counter < this->parmap.num_workers) {
428 xbt_os_thread_yield();
432 template <typename T> void Parmap<T>::BusyWaitSynchro::worker_signal()
434 __sync_add_and_fetch(&this->parmap.thread_counter, 1);
437 template <typename T> void Parmap<T>::BusyWaitSynchro::worker_wait(unsigned round)
439 /* wait for more work */
440 while (this->parmap.work_round != round) {
441 xbt_os_thread_yield();