From: Christophe ThiƩry Date: Fri, 9 Dec 2011 15:31:11 +0000 (+0100) Subject: Alternative implementation or parmap with busy waits instead of futexes X-Git-Tag: exp_20120216~237^2~40 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/8f3905e6e13d9511eb3002f9bdeb30e147e76056 Alternative implementation or parmap with busy waits instead of futexes When the user contexts are run in parallel, you can choose the synchronization mode of their parmap with: --cfg=contexts/parallel_mode:{posix|futex|busy_wait}. The default is futex. Posix synchronization is not implemented yet. --- diff --git a/include/simix/context.h b/include/simix/context.h index 71d1e6dac2..367acf27ad 100644 --- a/include/simix/context.h +++ b/include/simix/context.h @@ -10,6 +10,7 @@ #define _SIMIX_CONTEXT_H #include "xbt/swag.h" +#include "xbt/parmap.h" #include "simix/datatypes.h" #include "simgrid_config.h" @@ -50,8 +51,6 @@ typedef struct s_smx_context_factory { smx_pfn_context_get_data_t get_data; } s_smx_context_factory_t; - - /* Hack: let msg load directly the right factory */ typedef void (*smx_ctx_factory_initializer_t)(smx_context_factory_t*); extern smx_ctx_factory_initializer_t smx_factory_initializer_to_use; @@ -97,11 +96,13 @@ smx_context_t smx_ctx_base_self(void); void *smx_ctx_base_get_data(smx_context_t context); /* parallelism */ -XBT_INLINE void SIMIX_context_set_nthreads(int nb_threads); -XBT_INLINE int SIMIX_context_get_nthreads(void); XBT_INLINE int SIMIX_context_is_parallel(void); -XBT_INLINE void SIMIX_context_set_parallel_threshold(int threshold); +XBT_INLINE int SIMIX_context_get_nthreads(void); +XBT_INLINE void SIMIX_context_set_nthreads(int nb_threads); XBT_INLINE int SIMIX_context_get_parallel_threshold(void); +XBT_INLINE void SIMIX_context_set_parallel_threshold(int threshold); +XBT_INLINE e_xbt_parmap_mode_t SIMIX_context_get_parallel_mode(void); +XBT_INLINE void SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode); SG_END_DECL() diff --git a/include/xbt/parmap.h b/include/xbt/parmap.h index 7bf69c0208..b2e66dc7a4 100644 --- a/include/xbt/parmap.h +++ b/include/xbt/parmap.h @@ -32,7 +32,17 @@ SG_BEGIN_DECL() /** \brief Parallel map data type (opaque type) */ typedef struct s_xbt_parmap *xbt_parmap_t; -XBT_PUBLIC(xbt_parmap_t) xbt_parmap_new(unsigned int num_workers); +/** + * \brief Synchronization mode of the worker threads of a parmap. + */ +typedef enum { + XBT_PARMAP_POSIX, /**< use POSIX synchronization primitives */ + XBT_PARMAP_FUTEX, /**< use Linux futex system call */ + XBT_PARMAP_BUSY_WAIT /**< busy waits (no system calls, maximum CPU usage) */ +} e_xbt_parmap_mode_t; + +XBT_PUBLIC(xbt_parmap_t) xbt_parmap_new(unsigned int num_workers, + e_xbt_parmap_mode_t mode); XBT_PUBLIC(void) xbt_parmap_destroy(xbt_parmap_t parmap); XBT_PUBLIC(void) xbt_parmap_apply(xbt_parmap_t parmap, diff --git a/src/simix/smx_context.c b/src/simix/smx_context.c index 4926372722..6423cd6146 100644 --- a/src/simix/smx_context.c +++ b/src/simix/smx_context.c @@ -29,6 +29,7 @@ static xbt_os_thread_key_t smx_current_context_key = 0; static smx_context_t smx_current_context_serial; static int smx_parallel_contexts = 1; static int smx_parallel_threshold = 2; +static e_xbt_parmap_mode_t smx_parallel_synchronization_mode = XBT_PARMAP_FUTEX; /** * This function is called by SIMIX_global_init() to initialize the context module. @@ -108,6 +109,24 @@ void SIMIX_context_mod_exit(void) xbt_dict_remove((xbt_dict_t) _surf_cfg_set,"contexts/factory"); } +/** + * \brief Returns whether some parallel threads are used + * for the user contexts. + * \return 1 if parallelism is used + */ +XBT_INLINE int SIMIX_context_is_parallel(void) { + return smx_parallel_contexts > 1; +} + +/** + * \brief Returns the number of parallel threads used + * for the user contexts. + * \return the number of threads (1 means no parallelism) + */ +XBT_INLINE int SIMIX_context_get_nthreads(void) { + return smx_parallel_contexts; +} + /** * \brief Sets the number of parallel threads to use * for the user contexts. @@ -132,21 +151,16 @@ XBT_INLINE void SIMIX_context_set_nthreads(int nb_threads) { } /** - * \brief Returns the number of parallel threads used - * for the user contexts. - * \return the number of threads (1 means no parallelism) - */ -XBT_INLINE int SIMIX_context_get_nthreads(void) { - return smx_parallel_contexts; -} - -/** - * \brief Returns whether some parallel threads are used - * for the user contexts. - * \return 1 if parallelism is used + * \brief Returns the threshold above which user processes are run in parallel. + * + * If the number of threads is set to 1, there is no parallelism and this + * threshold has no effect. + * + * \return when the number of user processes ready to run is above + * this threshold, they are run in parallel */ -XBT_INLINE int SIMIX_context_is_parallel(void) { - return smx_parallel_contexts > 1; +XBT_INLINE int SIMIX_context_get_parallel_threshold(void) { + return smx_parallel_threshold; } /** @@ -163,16 +177,21 @@ XBT_INLINE void SIMIX_context_set_parallel_threshold(int threshold) { } /** - * \brief Returns the threshold above which user processes are run in parallel. - * - * If the number of threads is set to 1, there is no parallelism and this - * threshold has no effect. - * - * \return when the number of user processes ready to run is above - * this threshold, they are run in parallel + * \brief Returns the synchronization mode used when processes are run in + * parallel. + * \return how threads are synchronized if processes are run in parallel */ -XBT_INLINE int SIMIX_context_get_parallel_threshold(void) { - return smx_parallel_threshold; +XBT_INLINE e_xbt_parmap_mode_t SIMIX_context_get_parallel_mode(void) { + return smx_parallel_synchronization_mode; +} + +/** + * \brief Sets the synchronization mode to use when processes are run in + * parallel. + * \param mode how to synchronize threads if processes are run in parallel + */ +XBT_INLINE void SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode) { + smx_parallel_synchronization_mode = mode; } /** diff --git a/src/simix/smx_context_raw.c b/src/simix/smx_context_raw.c index c431cc30c2..c053cff81c 100644 --- a/src/simix/smx_context_raw.c +++ b/src/simix/smx_context_raw.c @@ -168,7 +168,7 @@ __asm__ ( ); #else -/* If you implement raw contextes for other processors, don't forget to +/* If you implement raw contexts for other processors, don't forget to update the definition of HAVE_RAWCTX in buildtools/Cmake/CompleteInFiles.cmake */ raw_stack_t raw_makecontext(char* malloced_stack, int stack_size, @@ -231,7 +231,7 @@ void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory) if (SIMIX_context_is_parallel()) { #ifdef CONTEXT_THREADS int nthreads = SIMIX_context_get_nthreads(); - raw_parmap = xbt_parmap_new(nthreads); + raw_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode()); raw_workers_stacks = xbt_new(raw_stack_t, nthreads); xbt_os_thread_key_create(&raw_worker_id_key); #endif @@ -327,7 +327,6 @@ smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv, */ static void smx_ctx_raw_free(smx_context_t context) { - if (context) { #ifdef HAVE_VALGRIND_VALGRIND_H diff --git a/src/simix/smx_context_sysv.c b/src/simix/smx_context_sysv.c index 7f2c5c34c7..437db9f5d8 100644 --- a/src/simix/smx_context_sysv.c +++ b/src/simix/smx_context_sysv.c @@ -98,7 +98,7 @@ void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory) if (SIMIX_context_is_parallel()) { #ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */ int nthreads = SIMIX_context_get_nthreads(); - sysv_parmap = xbt_parmap_new(nthreads); + sysv_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode()); sysv_workers_stacks = xbt_new(ucontext_t, nthreads); xbt_os_thread_key_create(&sysv_worker_id_key); (*factory)->stop = smx_ctx_sysv_stop_parallel; diff --git a/src/surf/surf_config.c b/src/surf/surf_config.c index a6dda50fb1..7b25a55143 100644 --- a/src/surf/surf_config.c +++ b/src/surf/surf_config.c @@ -215,6 +215,24 @@ static void _surf_cfg_cb_contexts_parallel_threshold(const char *name, int pos) SIMIX_context_set_parallel_threshold(xbt_cfg_get_int(_surf_cfg_set, name)); } +static void _surf_cfg_cb_contexts_parallel_mode(const char *name, int pos) +{ + const char* mode_name = xbt_cfg_get_string(_surf_cfg_set, name); + if (!strcmp(mode_name, "posix")) { + SIMIX_context_set_parallel_mode(XBT_PARMAP_POSIX); + } + else if (!strcmp(mode_name, "futex")) { + SIMIX_context_set_parallel_mode(XBT_PARMAP_FUTEX); + } + else if (!strcmp(mode_name, "busy_wait")) { + SIMIX_context_set_parallel_mode(XBT_PARMAP_BUSY_WAIT); + } + else { + XBT_WARN("Command line setting of the parallel synchronization mode should " + "be one of \"posix\", \"futex\" or \"busy_wait\""); + } +} + static void _surf_cfg_cb__surf_network_coordinates(const char *name, int pos) { @@ -409,6 +427,13 @@ void surf_config_init(int *argc, char **argv) xbt_cfgelm_int, &default_value_int, 1, 1, _surf_cfg_cb_contexts_parallel_threshold, NULL); + /* minimal number of user contexts to be run in parallel */ + default_value = xbt_strdup("futex"); + xbt_cfg_register(&_surf_cfg_set, "contexts/parallel_mode", + "Synchronization mode to use when running contexts in parallel", + xbt_cfgelm_string, &default_value, 1, 1, + _surf_cfg_cb_contexts_parallel_mode, NULL); + default_value = xbt_strdup("no"); xbt_cfg_register(&_surf_cfg_set, "coordinates", "\"yes\" or \"no\" (FIXME: document)", diff --git a/src/xbt/parmap.c b/src/xbt/parmap.c index 8a86b36384..d9f5a9d5b1 100644 --- a/src/xbt/parmap.c +++ b/src/xbt/parmap.c @@ -25,42 +25,61 @@ 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 { - PARMAP_WORK = 0, - PARMAP_DESTROY + XBT_PARMAP_WORK = 0, + XBT_PARMAP_DESTROY } e_xbt_parmap_flag_t; -static void xbt_parmap_start(xbt_parmap_t parmap); -static void xbt_parmap_signal(xbt_parmap_t parmap); -static void xbt_parmap_wait(xbt_parmap_t parmap); -static void xbt_parmap_end(xbt_parmap_t parmap); +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 */ - 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 */ + 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) +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; @@ -71,14 +90,15 @@ xbt_parmap_t xbt_parmap_new(unsigned int num_workers) xbt_parmap_t parmap = xbt_new0(s_xbt_parmap_t, 1); parmap->num_workers = num_workers; - parmap->status = PARMAP_WORK; + parmap->status = XBT_PARMAP_WORK; + xbt_parmap_set_mode(parmap, mode); /* 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); } - xbt_parmap_start(parmap); + parmap->start_f(parmap); return parmap; } @@ -88,11 +108,45 @@ xbt_parmap_t xbt_parmap_new(unsigned int num_workers) */ void xbt_parmap_destroy(xbt_parmap_t parmap) { - parmap->status = PARMAP_DESTROY; - xbt_parmap_signal(parmap); + parmap->status = XBT_PARMAP_DESTROY; + parmap->signal_f(parmap); xbt_free(parmap); } +/** + * \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: + 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; + 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 @@ -105,7 +159,7 @@ void xbt_parmap_apply(xbt_parmap_t parmap, void_f_pvoid_t fun, xbt_dynar_t data) parmap->fun = fun; parmap->data = data; parmap->index = 0; - xbt_parmap_signal(parmap); + parmap->signal_f(parmap); XBT_DEBUG("Job done"); } @@ -137,8 +191,8 @@ static void *xbt_parmap_worker_main(void *arg) /* Worker's main loop */ while (1) { - xbt_parmap_wait(parmap); - if (parmap->status == PARMAP_WORK) { + parmap->wait_f(parmap); + if (parmap->status == XBT_PARMAP_WORK) { XBT_DEBUG("Worker got a job"); @@ -152,7 +206,7 @@ static void *xbt_parmap_worker_main(void *arg) /* We are destroying the parmap */ } else { - xbt_parmap_end(parmap); + parmap->end_f(parmap); XBT_DEBUG("Shutting down worker"); return NULL; } @@ -173,6 +227,27 @@ static void futex_wake(int *uaddr, int val) } #endif +static void xbt_parmap_posix_start(xbt_parmap_t parmap) +{ + THROW_UNIMPLEMENTED; +} + +static void xbt_parmap_posix_end(xbt_parmap_t parmap) +{ + THROW_UNIMPLEMENTED; +} + +static void xbt_parmap_posix_signal(xbt_parmap_t parmap) +{ + THROW_UNIMPLEMENTED; +} + +static void xbt_parmap_posix_wait(xbt_parmap_t parmap) +{ + THROW_UNIMPLEMENTED; +} + +#ifdef HAVE_FUTEX_H /** * \brief Starts the parmap: waits for all workers to be ready and returns. * @@ -180,16 +255,34 @@ static void futex_wake(int *uaddr, int val) * * \param parmap a parmap */ -static void xbt_parmap_start(xbt_parmap_t parmap) +static void xbt_parmap_futex_start(xbt_parmap_t parmap) { -#ifdef HAVE_FUTEX_H 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); } -#endif +} + +/** + * \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; + + 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); + } } /** @@ -199,9 +292,8 @@ static void xbt_parmap_start(xbt_parmap_t parmap) * * \param parmap a parmap */ -static void xbt_parmap_signal(xbt_parmap_t parmap) +static void xbt_parmap_futex_signal(xbt_parmap_t parmap) { -#ifdef HAVE_FUTEX_H int myflag = parmap->done; parmap->thread_counter = 0; parmap->work++; @@ -209,11 +301,12 @@ static void xbt_parmap_signal(xbt_parmap_t parmap) /* wake all workers */ futex_wake(&parmap->work, parmap->num_workers); - if (parmap->status == PARMAP_WORK) { + if (parmap->status == XBT_PARMAP_WORK) { /* also work myself */ void* work = xbt_parmap_next(parmap); - if (work != NULL) { + while (work != NULL) { parmap->fun(work); + work = xbt_parmap_next(parmap); } } @@ -222,20 +315,18 @@ static void xbt_parmap_signal(xbt_parmap_t parmap) /* some workers have not finished yet */ futex_wait(&parmap->done, myflag); } - -#endif } /** * \brief Waits for some work to process. * - * This function is called by each worker when it has no more work to do. + * 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_wait(xbt_parmap_t parmap) +static void xbt_parmap_futex_wait(xbt_parmap_t parmap) { -#ifdef HAVE_FUTEX_H int myflag; unsigned int mycount; @@ -249,7 +340,22 @@ static void xbt_parmap_wait(xbt_parmap_t parmap) /* 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(); + } } /** @@ -259,18 +365,56 @@ static void xbt_parmap_wait(xbt_parmap_t parmap) * * \param parmap a parmap */ -static void xbt_parmap_end(xbt_parmap_t parmap) +static void xbt_parmap_busy_end(xbt_parmap_t parmap) { -#ifdef HAVE_FUTEX_H - unsigned int mycount; + __sync_add_and_fetch(&parmap->thread_counter, 1); +} - 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); +/** + * \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); + } + } + + /* 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(); + } +} + +/** + * \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); + + /* wait for more work */ + while (parmap->work == work) { + xbt_os_thread_yield(); } -#endif } #ifdef SIMGRID_TEST