/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
+#include "gras_config.h"
+#include <unistd.h>
+
+#ifndef _XBT_WIN32
+#include <sys/syscall.h>
+#endif
+
+#ifdef HAVE_FUTEX_H
+#include <linux/futex.h>
+#endif
+
+#include "xbt/parmap.h"
+#include "xbt/log.h"
+#include "xbt/function_types.h"
+#include "xbt/dynar.h"
+#include "xbt/xbt_os_thread.h"
+#include "xbt/sysdep.h"
+
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_parmap, xbt, "parmap: parallel map");
+XBT_LOG_NEW_SUBCATEGORY(xbt_parmap_unit, xbt_parmap, "parmap unit testing");
+
+typedef enum {
+ XBT_PARMAP_WORK = 0,
+ XBT_PARMAP_DESTROY
+} e_xbt_parmap_flag_t;
+
+static void xbt_parmap_set_mode(xbt_parmap_t parmap, e_xbt_parmap_mode_t mode);
+static void *xbt_parmap_worker_main(void *parmap);
+
+static void xbt_parmap_posix_start(xbt_parmap_t parmap);
+static void xbt_parmap_posix_end(xbt_parmap_t parmap);
+static void xbt_parmap_posix_signal(xbt_parmap_t parmap);
+static void xbt_parmap_posix_wait(xbt_parmap_t parmap);
+
+#ifdef HAVE_FUTEX_H
+static void xbt_parmap_futex_start(xbt_parmap_t parmap);
+static void xbt_parmap_futex_end(xbt_parmap_t parmap);
+static void xbt_parmap_futex_signal(xbt_parmap_t parmap);
+static void xbt_parmap_futex_wait(xbt_parmap_t parmap);
+static void futex_wait(int *uaddr, int val);
+static void futex_wake(int *uaddr, int val);
+#endif
+
+static void xbt_parmap_busy_start(xbt_parmap_t parmap);
+static void xbt_parmap_busy_end(xbt_parmap_t parmap);
+static void xbt_parmap_busy_signal(xbt_parmap_t parmap);
+static void xbt_parmap_busy_wait(xbt_parmap_t parmap);
+
+
+/**
+ * \brief Parallel map structure
+ */
+typedef struct s_xbt_parmap {
+ e_xbt_parmap_flag_t status; /**< is the parmap active or being destroyed? */
+ int work; /**< index of the current round (1 is the first) */
+ int done; /**< number of rounds already done (futexes only) */
+ unsigned int thread_counter; /**< number of threads currently working */
+ unsigned int num_workers; /**< total number of worker threads including the controller */
+ void_f_pvoid_t fun; /**< function to run in parallel on each element of data */
+ xbt_dynar_t data; /**< parameters to pass to fun in parallel */
+ unsigned int index; /**< index of the next element of data to pick */
+
+ /* fields that depend on the synchronization mode */
+ e_xbt_parmap_mode_t mode; /**< synchronization mode */
+ void (*start_f)(xbt_parmap_t); /**< initializes the worker threads */
+ void (*end_f)(xbt_parmap_t); /**< finalizes the worker threads */
+ void (*signal_f)(xbt_parmap_t); /**< wakes the workers threads to process tasks */
+ void (*wait_f)(xbt_parmap_t); /**< waits for more work */
+} s_xbt_parmap_t;
+
+/**
+ * \brief Creates a parallel map object
+ * \param num_workers number of worker threads to create
+ * \param mode how to synchronize the worker threads
+ * \return the parmap created
+ */
+xbt_parmap_t xbt_parmap_new(unsigned int num_workers, e_xbt_parmap_mode_t mode)
+{
+ unsigned int i;
+ xbt_os_thread_t worker = NULL;
-#include "threadpool_private.h"
+ XBT_DEBUG("Create new parmap (%u workers)", num_workers);
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_threadpool, xbt,
- "threadpool: pool of worker threads");
+ /* Initialize the thread pool data structure */
+ xbt_parmap_t parmap = xbt_new0(s_xbt_parmap_t, 1);
-static void *_xbt_tpool_worker_main(void *tpool);
+ parmap->num_workers = num_workers;
+ parmap->status = XBT_PARMAP_WORK;
+ xbt_parmap_set_mode(parmap, mode);
-unsigned long tpoolcounter = 0; /* Debug purposes */
+ /* Create the pool of worker threads */
+ for (i = 0; i < num_workers - 1; i++) {
+ worker = xbt_os_thread_create(NULL, xbt_parmap_worker_main, parmap, NULL);
+ xbt_os_thread_detach(worker);
+ }
+ parmap->start_f(parmap);
+ return parmap;
+}
-xbt_tpool_t xbt_tpool_new(unsigned int num_workers, unsigned int max_jobs)
+/**
+ * \brief Destroys a parmap
+ * \param parmap the parmap to destroy
+ */
+void xbt_parmap_destroy(xbt_parmap_t parmap)
{
- unsigned int i;
- xbt_os_thread_t worker = NULL;
+ parmap->status = XBT_PARMAP_DESTROY;
+ parmap->signal_f(parmap);
+ xbt_free(parmap);
+}
- DEBUG2("Create new thread pool (%u, %u)", num_workers, max_jobs);
-
- /* Initialize thread pool data structure */
- xbt_tpool_t tpool = xbt_new0(s_xbt_tpool_t, 1);
- tpool->mutex = xbt_os_mutex_init();
- tpool->job_posted = xbt_os_cond_init();
- tpool->job_taken = xbt_os_cond_init();
- tpool->idle_worker = xbt_os_cond_init();
- tpool->jobs_queue = xbt_dynar_new(sizeof(s_xbt_tpool_job_t), NULL);
- tpool->num_workers = num_workers;
- tpool->num_idle_workers = 0;
- tpool->max_jobs = max_jobs;
-
- /* Create the pool of worker threads */
- for(i=0; i < num_workers; i++){
- worker = xbt_os_thread_create(NULL, _xbt_tpool_worker_main, tpool, NULL);
- xbt_os_thread_detach(worker);
+/**
+ * \brief Sets the synchronization mode of a parmap.
+ * \param parmap a parallel map object
+ * \param mode the synchronization mode
+ */
+static void xbt_parmap_set_mode(xbt_parmap_t parmap, e_xbt_parmap_mode_t mode)
+{
+ parmap->mode = mode;
+
+ switch (mode) {
+
+ case XBT_PARMAP_POSIX:
+ parmap->start_f = xbt_parmap_posix_start;
+ parmap->end_f = xbt_parmap_posix_end;
+ parmap->signal_f = xbt_parmap_posix_signal;
+ parmap->wait_f = xbt_parmap_posix_wait;
+ break;
+
+
+ case XBT_PARMAP_FUTEX:
+#ifdef HAVE_FUTEX_H
+ parmap->start_f = xbt_parmap_futex_start;
+ parmap->end_f = xbt_parmap_futex_end;
+ parmap->signal_f = xbt_parmap_futex_signal;
+ parmap->wait_f = xbt_parmap_futex_wait;
+#endif
+ xbt_die("Impossible to use futex on Mac");
+ break;
+
+ case XBT_PARMAP_BUSY_WAIT:
+ parmap->start_f = xbt_parmap_busy_start;
+ parmap->end_f = xbt_parmap_busy_end;
+ parmap->signal_f = xbt_parmap_busy_signal;
+ parmap->wait_f = xbt_parmap_busy_wait;
+ break;
+ }
+}
+
+/**
+ * \brief Applies a list of tasks in parallel.
+ * \param parmap a parallel map object
+ * \param fun the function to call in parallel
+ * \param data each element of this dynar will be passed as an argument to fun
+ */
+void xbt_parmap_apply(xbt_parmap_t parmap, void_f_pvoid_t fun, xbt_dynar_t data)
+{
+ /* Assign resources to worker threads */
+ parmap->fun = fun;
+ parmap->data = data;
+ parmap->index = 0;
+ parmap->signal_f(parmap);
+ XBT_DEBUG("Job done");
+}
+
+/**
+ * \brief Returns a next task to process.
+ *
+ * Worker threads call this function to get more work.
+ *
+ * \return the next task to process, or NULL if there is no more work
+ */
+void* xbt_parmap_next(xbt_parmap_t parmap)
+{
+ unsigned int index = __sync_fetch_and_add(&parmap->index, 1);
+ if (index < xbt_dynar_length(parmap->data)) {
+ return xbt_dynar_get_as(parmap->data, index, void*);
}
-
- return tpool;
+ return NULL;
}
-void xbt_tpool_destroy(xbt_tpool_t tpool)
-{
- DEBUG1("Destroy thread pool %p", tpool);
+/**
+ * \brief Main function of a worker thread.
+ * \param arg the parmap
+ */
+static void *xbt_parmap_worker_main(void *arg)
+{
+ xbt_parmap_t parmap = (xbt_parmap_t) arg;
+
+ XBT_DEBUG("New worker thread created");
+
+ /* Worker's main loop */
+ while (1) {
+ parmap->wait_f(parmap);
+ if (parmap->status == XBT_PARMAP_WORK) {
+
+ XBT_DEBUG("Worker got a job");
+
+ void* work = xbt_parmap_next(parmap);
+ while (work != NULL) {
+ parmap->fun(work);
+ work = xbt_parmap_next(parmap);
+ }
- /* Lock the pool, then signal every worker an wait for each to finish */
- xbt_os_mutex_acquire(tpool->mutex);
- tpool->flags = TPOOL_DESTROY;
+ XBT_DEBUG("Worker has finished");
- while(tpool->num_workers){
- DEBUG1("Still %u workers, waiting...", tpool->num_workers);
- xbt_os_cond_signal(tpool->job_posted);
- xbt_os_cond_wait(tpool->job_taken, tpool->mutex);
+ /* We are destroying the parmap */
+ } else {
+ parmap->end_f(parmap);
+ XBT_DEBUG("Shutting down worker");
+ return NULL;
+ }
}
+}
+
+#ifdef HAVE_FUTEX_H
+static void futex_wait(int *uaddr, int val)
+{
+ XBT_VERB("Waiting on futex %p", uaddr);
+ syscall(SYS_futex, uaddr, FUTEX_WAIT_PRIVATE, val, NULL, NULL, 0);
+}
- /* Destroy pool's data structures */
- xbt_os_cond_destroy(tpool->job_posted);
- xbt_os_cond_destroy(tpool->job_taken);
- xbt_os_cond_destroy(tpool->idle_worker);
- xbt_os_mutex_release(tpool->mutex);
- xbt_os_mutex_destroy(tpool->mutex);
- xbt_free(tpool);
+static void futex_wake(int *uaddr, int val)
+{
+ XBT_VERB("Waking futex %p", uaddr);
+ syscall(SYS_futex, uaddr, FUTEX_WAKE_PRIVATE, val, NULL, NULL, 0);
}
+#endif
-void xbt_tpool_queue_job(xbt_tpool_t tpool, void_f_pvoid_t fun, void* fun_arg)
+static void xbt_parmap_posix_start(xbt_parmap_t parmap)
{
- s_xbt_tpool_job_t job;
- job.fun = fun;
- job.fun_arg = fun_arg;
+ THROW_UNIMPLEMENTED;
+}
- /* Wait until we can lock on the pool with some space on it for the job */
- xbt_os_mutex_acquire(tpool->mutex);
- while(xbt_dynar_length(tpool->jobs_queue) == tpool->max_jobs)
- xbt_os_cond_wait(tpool->job_taken, tpool->mutex);
+static void xbt_parmap_posix_end(xbt_parmap_t parmap)
+{
+ THROW_UNIMPLEMENTED;
+}
- DEBUG3("Queue job %p (%p) to thread pool %p", fun, fun_arg, tpool);
+static void xbt_parmap_posix_signal(xbt_parmap_t parmap)
+{
+ THROW_UNIMPLEMENTED;
+}
- /* Push the job in the queue, signal the workers and unlock the pool */
- xbt_dynar_push_as(tpool->jobs_queue, s_xbt_tpool_job_t, job);
- xbt_os_cond_signal(tpool->job_posted);
- xbt_os_mutex_release(tpool->mutex);
- return;
+static void xbt_parmap_posix_wait(xbt_parmap_t parmap)
+{
+ THROW_UNIMPLEMENTED;
}
-void xbt_tpool_wait_all(xbt_tpool_t tpool)
+#ifdef HAVE_FUTEX_H
+/**
+ * \brief Starts the parmap: waits for all workers to be ready and returns.
+ *
+ * This function is called by the controller thread.
+ *
+ * \param parmap a parmap
+ */
+static void xbt_parmap_futex_start(xbt_parmap_t parmap)
{
- DEBUG1("Wait all workers in thread pool %p", tpool);
- xbt_os_mutex_acquire(tpool->mutex);
+ int myflag = parmap->done;
+ __sync_fetch_and_add(&parmap->thread_counter, 1);
+ if (parmap->thread_counter < parmap->num_workers) {
+ /* wait for all workers to be ready */
+ futex_wait(&parmap->done, myflag);
+ }
+}
- while(tpool->num_idle_workers < tpool->num_workers
- || xbt_dynar_length(tpool->jobs_queue) > 0)
- xbt_os_cond_wait(tpool->idle_worker, tpool->mutex);
+/**
+ * \brief Ends the parmap: wakes the controller thread when all workers terminate.
+ *
+ * This function is called by all worker threads when they end (not including
+ * the controller).
+ *
+ * \param parmap a parmap
+ */
+static void xbt_parmap_futex_end(xbt_parmap_t parmap)
+{
+ unsigned int mycount;
- xbt_os_mutex_release(tpool->mutex);
- DEBUG1("Wait all workers done in thread pool %p", tpool);
- return;
+ mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
+ if (mycount == parmap->num_workers) {
+ /* all workers have finished, wake the controller */
+ parmap->done++;
+ futex_wake(&parmap->done, 1);
+ }
}
-static void *_xbt_tpool_worker_main(void *arg)
+/**
+ * \brief Wakes all workers and waits for them to finish the tasks.
+ *
+ * This function is called by the controller thread.
+ *
+ * \param parmap a parmap
+ */
+static void xbt_parmap_futex_signal(xbt_parmap_t parmap)
{
- s_xbt_tpool_job_t job;
- xbt_tpool_t tpool = (xbt_tpool_t)arg;
+ int myflag = parmap->done;
+ parmap->thread_counter = 0;
+ parmap->work++;
+
+ /* wake all workers */
+ futex_wake(&parmap->work, parmap->num_workers);
+
+ if (parmap->status == XBT_PARMAP_WORK) {
+ /* also work myself */
+ void* work = xbt_parmap_next(parmap);
+ while (work != NULL) {
+ parmap->fun(work);
+ work = xbt_parmap_next(parmap);
+ }
+ }
+
+ unsigned int mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
+ if (mycount < parmap->num_workers) {
+ /* some workers have not finished yet */
+ futex_wait(&parmap->done, myflag);
+ }
+}
- unsigned long i = tpoolcounter++; /* Debug purposes */
- DEBUG1("New worker thread created (%lu)", i);
-
+/**
+ * \brief Waits for some work to process.
+ *
+ * This function is called by each worker thread (not including the controller)
+ * when it has no more work to do.
+ *
+ * \param parmap a parmap
+ */
+static void xbt_parmap_futex_wait(xbt_parmap_t parmap)
+{
+ int myflag;
+ unsigned int mycount;
+
+ myflag = parmap->work;
+ mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
+ if (mycount == parmap->num_workers) {
+ /* all workers have finished, wake the controller */
+ parmap->done++;
+ futex_wake(&parmap->done, 1);
+ }
- /* Worker's main loop */
- while(1){
- xbt_os_mutex_acquire(tpool->mutex);
+ /* wait for more work */
+ futex_wait(&parmap->work, myflag);
+}
+#endif
+
+/**
+ * \brief Starts the parmap: waits for all workers to be ready and returns.
+ *
+ * This function is called by the controller thread.
+ *
+ * \param parmap a parmap
+ */
+static void xbt_parmap_busy_start(xbt_parmap_t parmap)
+{
+ __sync_fetch_and_add(&parmap->thread_counter, 1);
+ while (parmap->thread_counter < parmap->num_workers) {
+ xbt_os_thread_yield();
+ }
+}
- tpool->num_idle_workers++;
- xbt_os_cond_signal(tpool->idle_worker);
+/**
+ * \brief Ends the parmap: wakes the controller thread when all workers terminate.
+ *
+ * This function is called by all worker threads when they end.
+ *
+ * \param parmap a parmap
+ */
+static void xbt_parmap_busy_end(xbt_parmap_t parmap)
+{
+ __sync_add_and_fetch(&parmap->thread_counter, 1);
+}
- /* If there are no jobs in the queue wait for one */
- while(xbt_dynar_length(tpool->jobs_queue) == 0
- && tpool->flags != TPOOL_DESTROY){
- DEBUG1("Worker %lu waiting for a job", i);
- xbt_os_cond_wait(tpool->job_posted, tpool->mutex);
+/**
+ * \brief Wakes all workers and waits for them to finish the tasks.
+ *
+ * This function is called by the controller thread.
+ *
+ * \param parmap a parmap
+ */
+static void xbt_parmap_busy_signal(xbt_parmap_t parmap)
+{
+ parmap->thread_counter = 0;
+ parmap->work++;
+
+ if (parmap->status == XBT_PARMAP_WORK) {
+ /* also work myself */
+ void* work = xbt_parmap_next(parmap);
+ while (work != NULL) {
+ parmap->fun(work);
+ work = xbt_parmap_next(parmap);
}
+ }
- DEBUG1("Worker %lu got a job", i);
+ /* I have finished, wait for the others */
+ __sync_add_and_fetch(&parmap->thread_counter, 1);
+ while (parmap->thread_counter < parmap->num_workers) {
+ xbt_os_thread_yield();
+ }
+}
- /* If we are shutting down, signal the destroyer so it can kill the other */
- /* workers, unlock the pool and return */
- if(tpool->flags == TPOOL_DESTROY){
- DEBUG1("Shutting down worker %lu", i);
- tpool->num_idle_workers--;
- tpool->num_workers--;
- xbt_os_cond_signal(tpool->job_taken);
- xbt_os_mutex_release(tpool->mutex);
- return NULL;
- }
+/**
+ * \brief Waits for some work to process.
+ *
+ * This function is called by each worker thread (not including the controller)
+ * when it has no more work to do.
+ *
+ * \param parmap a parmap
+ */
+static void xbt_parmap_busy_wait(xbt_parmap_t parmap)
+{
+ int work = parmap->work;
+ __sync_add_and_fetch(&parmap->thread_counter, 1);
- /* Get a job, signal the pool to inform jobs submitters and unlock it */
- job = xbt_dynar_pop_as(tpool->jobs_queue, s_xbt_tpool_job_t);
- xbt_os_cond_signal(tpool->job_taken);
- tpool->num_idle_workers--;
- xbt_os_mutex_release(tpool->mutex);
-
- /* Run the job and loop again ... */
- job.fun(job.fun_arg);
+ /* wait for more work */
+ while (parmap->work == work) {
+ xbt_os_thread_yield();
}
}
#include "xbt.h"
#include "xbt/ex.h"
-XBT_TEST_SUITE("tpool", "Thread pool");
+XBT_TEST_SUITE("parmap", "Parallel Map");
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_parmap_unit);
-xbt_tpool_t tpool;
+xbt_parmap_t parmap;
-void job(void *arg);
+void fun(void *arg);
-void job (void *arg)
+void fun(void *arg)
{
- xbt_test_log1("I'm job %lu", (unsigned long)arg);
+ //XBT_INFO("I'm job %lu", (unsigned long)arg);
}
-XBT_TEST_UNIT("basic", test_tpool_basic, "Basic usage")
+XBT_TEST_UNIT("basic", test_parmap_basic, "Basic usage")
{
- xbt_test_add0("Create thread pool");
+ xbt_test_add("Create the parmap");
- unsigned long i,j;
- /* Create thread pool */
- tpool = xbt_tpool_new(5, 10);
+ unsigned long i, j;
+ xbt_dynar_t data = xbt_dynar_new(sizeof(void *), NULL);
- for(j=0; j < 10; j++){
- /* Queue some work */
- for(i=0; i < 20; i++){
- xbt_tpool_queue_job(tpool, job, (void*)i);
- }
- /* Wait for everyone */
- xbt_tpool_wait_all(tpool);
+ /* Create the parallel map */
+ parmap = xbt_parmap_new(10, XBT_PARMAP_FUTEX);
+
+ for (j = 0; j < 100; j++) {
+ xbt_dynar_push_as(data, void *, (void *)j);
+ }
+
+ for (i = 0; i < 5; i++) {
+ xbt_parmap_apply(parmap, fun, data);
}
- /* Destroy thread pool */
- xbt_tpool_destroy(tpool);
+ /* Destroy the parmap */
+ xbt_parmap_destroy(parmap);
+ xbt_dynar_free(&data);
}
#endif /* SIMGRID_TEST */