Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add comments
[simgrid.git] / src / xbt / parmap.c
1 /* Copyright (c) 2004, 2005, 2007, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "gras_config.h"
7 #include <unistd.h>
8
9 #ifndef _XBT_WIN32
10 #include <sys/syscall.h>
11 #endif
12
13 #ifdef HAVE_FUTEX_H
14 #include <linux/futex.h>
15 #endif
16
17 #include "xbt/parmap.h"
18 #include "xbt/log.h"
19 #include "xbt/function_types.h"
20 #include "xbt/dynar.h"
21 #include "xbt/xbt_os_thread.h"
22 #include "xbt/sysdep.h"
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_parmap, xbt, "parmap: parallel map");
25 XBT_LOG_NEW_SUBCATEGORY(xbt_parmap_unit, xbt_parmap, "parmap unit testing");
26
27 typedef enum {
28   PARMAP_WORK = 0,
29   PARMAP_DESTROY
30 } e_xbt_parmap_flag_t;
31
32 static void xbt_parmap_start(xbt_parmap_t parmap);
33 static void xbt_parmap_signal(xbt_parmap_t parmap);
34 static void xbt_parmap_wait(xbt_parmap_t parmap);
35 static void xbt_parmap_end(xbt_parmap_t parmap);
36 static void *xbt_parmap_worker_main(void *parmap);
37
38 #ifdef HAVE_FUTEX_H
39 static void futex_wait(int *uaddr, int val);
40 static void futex_wake(int *uaddr, int val);
41 #endif
42
43 /**
44  * \brief Parallel map structure
45  */
46 typedef struct s_xbt_parmap {
47   e_xbt_parmap_flag_t status;      /* is the parmap active or being destroyed? */
48
49   int work;                        /* index of the current round (1 is the first) */
50   int done;                        /* number of rounds already done */
51   unsigned int thread_counter;     /* number of threads currently working */
52   unsigned int num_workers;        /* total number of worker threads */
53   unsigned int workers_max_id;     /* id of the next worker thread to create */
54   void_f_pvoid_t fun;              /* function to run in parallel on each element of data */
55   xbt_dynar_t data;                /* parameters to pass to fun in parallel */
56   unsigned int index;              /* index of the next element of data to pick */
57 } s_xbt_parmap_t;
58
59 /**
60  * \brief Creates a parallel map object
61  * \param num_workers number of worker threads to create
62  * \return the parmap created
63  */
64 xbt_parmap_t xbt_parmap_new(unsigned int num_workers)
65 {
66   unsigned int i;
67   xbt_os_thread_t worker = NULL;
68
69   XBT_DEBUG("Create new parmap (%u workers)", num_workers);
70
71   /* Initialize the thread pool data structure */
72   xbt_parmap_t parmap = xbt_new0(s_xbt_parmap_t, 1);
73
74   parmap->num_workers = num_workers;
75   parmap->status = PARMAP_WORK;
76
77   /* Create the pool of worker threads */
78   for (i = 0; i < num_workers; i++) {
79     worker = xbt_os_thread_create(NULL, xbt_parmap_worker_main, parmap, NULL);
80     xbt_os_thread_detach(worker);
81   }
82   xbt_parmap_start(parmap);
83   return parmap;
84 }
85
86 /**
87  * \brief Destroys a parmap
88  * \param parmap the parmap to destroy
89  */
90 void xbt_parmap_destroy(xbt_parmap_t parmap)
91 {
92   parmap->status = PARMAP_DESTROY;
93   xbt_parmap_signal(parmap);
94   xbt_free(parmap);
95 }
96
97 /**
98  * \brief Applies a list of tasks in parallel.
99  * \param parmap a parallel map object
100  * \param fun the function to call in parallel
101  * \param data each element of this dynar will be passed as an argument to fun
102  */
103 void xbt_parmap_apply(xbt_parmap_t parmap, void_f_pvoid_t fun, xbt_dynar_t data)
104 {
105   /* Assign resources to worker threads */
106   parmap->fun = fun;
107   parmap->data = data;
108   parmap->index = 0;
109   xbt_parmap_signal(parmap);
110   XBT_DEBUG("Job done");
111 }
112
113 /**
114  * \brief Returns a next task to process.
115  *
116  * Worker threads call this function to get more work.
117  *
118  * \return the next task to process, or NULL if there is no more work
119  */
120 void* xbt_parmap_next(xbt_parmap_t parmap)
121 {
122   unsigned int index = __sync_fetch_and_add(&parmap->index, 1);
123   if (index < xbt_dynar_length(parmap->data)) {
124     return xbt_dynar_get_as(parmap->data, index, void*);
125   }
126   return NULL;
127 }
128
129 /**
130  * \brief Main function of a worker thread.
131  * \param arg the parmap
132  */
133 static void *xbt_parmap_worker_main(void *arg)
134 {
135   unsigned int worker_id;
136   xbt_parmap_t parmap = (xbt_parmap_t) arg;
137
138   /* Fetch a worker id */
139   worker_id = __sync_fetch_and_add(&parmap->workers_max_id, 1);
140   xbt_os_thread_set_extra_data((void*) (unsigned long) worker_id);
141
142   XBT_DEBUG("New worker thread created (%u)", worker_id);
143
144   /* Worker's main loop */
145   while (1) {
146     xbt_parmap_wait(parmap);
147     if (parmap->status == PARMAP_WORK) {
148
149       XBT_DEBUG("Worker %u got a job", worker_id);
150
151       void* work = xbt_parmap_next(parmap);
152       if (work != NULL) {
153         parmap->fun(work);
154       }
155
156       XBT_DEBUG("Worker %u has finished", worker_id);
157
158     /* We are destroying the parmap */
159     } else {
160       xbt_parmap_end(parmap);
161       XBT_DEBUG("Shutting down worker %u", worker_id);
162       return NULL;
163     }
164   }
165 }
166
167 #ifdef HAVE_FUTEX_H
168 static void futex_wait(int *uaddr, int val)
169 {
170   XBT_VERB("Waiting on futex %p", uaddr);
171   syscall(SYS_futex, uaddr, FUTEX_WAIT_PRIVATE, val, NULL, NULL, 0);
172 }
173
174 static void futex_wake(int *uaddr, int val)
175 {
176   XBT_VERB("Waking futex %p", uaddr);
177   syscall(SYS_futex, uaddr, FUTEX_WAKE_PRIVATE, val, NULL, NULL, 0);
178 }
179 #endif
180
181 /**
182  * \brief Starts the parmap: waits for all workers to be ready and returns.
183  *
184  * This function is called by the controller thread.
185  *
186  * \param parmap a parmap
187  */
188 static void xbt_parmap_start(xbt_parmap_t parmap)
189 {
190 #ifdef HAVE_FUTEX_H
191   int myflag = parmap->done;
192   if (parmap->thread_counter < parmap->num_workers) {
193     /* wait for all workers to be ready */
194     futex_wait(&parmap->done, myflag);
195   }
196 #endif
197 }
198
199 /**
200  * \brief Wakes all workers and waits for them to finish the tasks.
201  *
202  * This function is called by the controller thread.
203  *
204  * \param parmap a parmap
205  */
206 static void xbt_parmap_signal(xbt_parmap_t parmap)
207 {
208 #ifdef HAVE_FUTEX_H
209   int myflag = parmap->done;
210   parmap->thread_counter = 0;
211   parmap->work++;
212
213   /* wake all workers */
214   futex_wake(&parmap->work, parmap->num_workers);
215
216   /* wait for all of them to finish */
217   futex_wait(&parmap->done, myflag);
218 #endif
219 }
220
221 /**
222  * \brief Waits for some work to process.
223  *
224  * This function is called by each worker when it has no more work to do.
225  *
226  * \param parmap a parmap
227  */
228 static void xbt_parmap_wait(xbt_parmap_t parmap)
229 {
230 #ifdef HAVE_FUTEX_H
231   int myflag;
232   unsigned int mycount;
233
234   myflag = parmap->work;
235   mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
236   if (mycount == parmap->num_workers) {
237     /* all workers have finished, wake the controller */
238     parmap->done++;
239     futex_wake(&parmap->done, 1);
240   }
241
242   /* wait for more work */
243   futex_wait(&parmap->work, myflag);
244 #endif
245 }
246
247 /**
248  * \brief Ends the parmap: wakes the controller thread when all workers terminate.
249  *
250  * This function is called by all worker threads when they end.
251  *
252  * \param parmap a parmap
253  */
254 static void xbt_parmap_end(xbt_parmap_t parmap)
255 {
256 #ifdef HAVE_FUTEX_H
257   unsigned int mycount;
258
259   mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
260   if (mycount == parmap->num_workers) {
261     /* all workers have finished, wake the controller */
262     parmap->done++;
263     futex_wake(&parmap->done, 1);
264   }
265 #endif
266 }
267
268 #ifdef SIMGRID_TEST
269 #include "xbt.h"
270 #include "xbt/ex.h"
271
272 XBT_TEST_SUITE("parmap", "Parallel Map");
273 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_parmap_unit);
274
275 xbt_parmap_t parmap;
276
277 void fun(void *arg);
278
279 void fun(void *arg)
280 {
281   //XBT_INFO("I'm job %lu", (unsigned long)arg);
282 }
283
284 XBT_TEST_UNIT("basic", test_parmap_basic, "Basic usage")
285 {
286   xbt_test_add("Create the parmap");
287
288   unsigned long i, j;
289   xbt_dynar_t data = xbt_dynar_new(sizeof(void *), NULL);
290
291   /* Create the parallel map */
292   parmap = xbt_parmap_new(10);
293
294   for (j = 0; j < 100; j++) {
295     xbt_dynar_push_as(data, void *, (void *)j);
296   }
297
298   for (i = 0; i < 5; i++) {
299     xbt_parmap_apply(parmap, fun, data);
300   }
301
302   /* Destroy the parmap */
303   xbt_parmap_destroy(parmap);
304   xbt_dynar_free(&data);
305 }
306
307 #endif /* SIMGRID_TEST */