-/* Copyright (c) 2004, 2005, 2007, 2009, 2010. The SimGrid Team.
+/* Copyright (c) 2004-2005, 2007, 2009-2014. The SimGrid Team.
* All rights reserved. */
/* 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 "internal_config.h"
#include <unistd.h>
unsigned thread_counter; /**< number of workers that have done the work */
unsigned int num_workers; /**< total number of worker threads including the controller */
+ xbt_os_thread_t *workers; /**< worker thread handlers */
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 */
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;
XBT_DEBUG("Create new parmap (%u workers)", num_workers);
/* Initialize the thread pool data structure */
xbt_parmap_t parmap = xbt_new0(s_xbt_parmap_t, 1);
+ parmap->workers = xbt_new(xbt_os_thread_t, num_workers);
parmap->num_workers = num_workers;
parmap->status = XBT_PARMAP_WORK;
/* Create the pool of worker threads */
xbt_parmap_thread_data_t data;
+ parmap->workers[0] = NULL;
for (i = 1; i < num_workers; i++) {
data = xbt_new0(s_xbt_parmap_thread_data_t, 1);
data->parmap = parmap;
data->worker_id = i;
- worker = xbt_os_thread_create(NULL, xbt_parmap_worker_main, data, NULL);
- xbt_os_thread_detach(worker);
+ parmap->workers[i] = xbt_os_thread_create(NULL, xbt_parmap_worker_main,
+ data, NULL);
}
return parmap;
}
xbt_parmap_t xbt_parmap_mc_new(unsigned int num_workers, e_xbt_parmap_mode_t mode)
{
unsigned int i;
- xbt_os_thread_t worker = NULL;
XBT_DEBUG("Create new parmap (%u workers)", num_workers);
/* Initialize the thread pool data structure */
xbt_parmap_t parmap = xbt_new0(s_xbt_parmap_t, 1);
+ parmap->workers = xbt_new(xbt_os_thread_t, num_workers);
parmap->num_workers = num_workers;
parmap->status = XBT_PARMAP_WORK;
/* Create the pool of worker threads */
xbt_parmap_thread_data_t data;
+ parmap->workers[0] = NULL;
for (i = 1; i < num_workers; i++) {
data = xbt_new0(s_xbt_parmap_thread_data_t, 1);
data->parmap = parmap;
data->worker_id = i;
- worker = xbt_os_thread_create(NULL, xbt_parmap_mc_worker_main, data, NULL);
- xbt_os_thread_detach(worker);
+ parmap->workers[i] = xbt_os_thread_create(NULL, xbt_parmap_mc_worker_main,
+ data, NULL);
}
return parmap;
}
parmap->status = XBT_PARMAP_DESTROY;
parmap->master_signal_f(parmap);
- parmap->master_wait_f(parmap);
+
+ unsigned int i;
+ for (i = 1; i < parmap->num_workers; i++)
+ xbt_os_thread_join(parmap->workers[i], NULL);
xbt_os_cond_destroy(parmap->ready_cond);
xbt_os_mutex_destroy(parmap->ready_mutex);
xbt_os_cond_destroy(parmap->done_cond);
xbt_os_mutex_destroy(parmap->done_mutex);
+ xbt_free(parmap->workers);
xbt_free(parmap);
}
*/
void xbt_parmap_apply(xbt_parmap_t parmap, void_f_pvoid_t fun, xbt_dynar_t data)
{
- /* Assign resources to worker threads */
+ /* Assign resources to worker threads (we are maestro here)*/
parmap->fun = fun;
parmap->data = data;
parmap->index = 0;
- parmap->master_signal_f(parmap);
- xbt_parmap_work(parmap);
- parmap->master_wait_f(parmap);
- XBT_DEBUG("Job done");
+ parmap->master_signal_f(parmap); // maestro runs futex_wait to wake all the minions (the working threads)
+ xbt_parmap_work(parmap); // maestro works with its minions
+ parmap->master_wait_f(parmap); // When there is no more work to do, then maestro waits for the last minion to stop
+ XBT_DEBUG("Job done"); // ... and proceeds
}
/**
/* We are destroying the parmap */
} else {
+ SIMIX_context_free(context);
xbt_free(data);
- parmap->worker_signal_f(parmap);
return NULL;
}
}
/* We are destroying the parmap */
} else {
xbt_free(data);
- parmap->worker_signal_f(parmap);
return NULL;
}
}