Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This variable has no reason to be global
[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 including the controller */
53   void_f_pvoid_t fun;              /* function to run in parallel on each element of data */
54   xbt_dynar_t data;                /* parameters to pass to fun in parallel */
55   unsigned int index;              /* index of the next element of data to pick */
56 } s_xbt_parmap_t;
57
58 /**
59  * \brief Creates a parallel map object
60  * \param num_workers number of worker threads to create
61  * \return the parmap created
62  */
63 xbt_parmap_t xbt_parmap_new(unsigned int num_workers)
64 {
65   unsigned int i;
66   xbt_os_thread_t worker = NULL;
67
68   XBT_DEBUG("Create new parmap (%u workers)", num_workers);
69
70   /* Initialize the thread pool data structure */
71   xbt_parmap_t parmap = xbt_new0(s_xbt_parmap_t, 1);
72
73   parmap->num_workers = num_workers;
74   parmap->status = PARMAP_WORK;
75
76   /* Create the pool of worker threads */
77   for (i = 0; i < num_workers - 1; i++) {
78     worker = xbt_os_thread_create(NULL, xbt_parmap_worker_main, parmap, NULL);
79     xbt_os_thread_detach(worker);
80   }
81   xbt_parmap_start(parmap);
82   return parmap;
83 }
84
85 /**
86  * \brief Destroys a parmap
87  * \param parmap the parmap to destroy
88  */
89 void xbt_parmap_destroy(xbt_parmap_t parmap)
90 {
91   parmap->status = PARMAP_DESTROY;
92   xbt_parmap_signal(parmap);
93   xbt_free(parmap);
94 }
95
96 /**
97  * \brief Applies a list of tasks in parallel.
98  * \param parmap a parallel map object
99  * \param fun the function to call in parallel
100  * \param data each element of this dynar will be passed as an argument to fun
101  */
102 void xbt_parmap_apply(xbt_parmap_t parmap, void_f_pvoid_t fun, xbt_dynar_t data)
103 {
104   /* Assign resources to worker threads */
105   parmap->fun = fun;
106   parmap->data = data;
107   parmap->index = 0;
108   xbt_parmap_signal(parmap);
109   XBT_DEBUG("Job done");
110 }
111
112 /**
113  * \brief Returns a next task to process.
114  *
115  * Worker threads call this function to get more work.
116  *
117  * \return the next task to process, or NULL if there is no more work
118  */
119 void* xbt_parmap_next(xbt_parmap_t parmap)
120 {
121   unsigned int index = __sync_fetch_and_add(&parmap->index, 1);
122   if (index < xbt_dynar_length(parmap->data)) {
123     return xbt_dynar_get_as(parmap->data, index, void*);
124   }
125   return NULL;
126 }
127
128 /**
129  * \brief Main function of a worker thread.
130  * \param arg the parmap
131  */
132 static void *xbt_parmap_worker_main(void *arg)
133 {
134   xbt_parmap_t parmap = (xbt_parmap_t) arg;
135
136   XBT_DEBUG("New worker thread created");
137
138   /* Worker's main loop */
139   while (1) {
140     xbt_parmap_wait(parmap);
141     if (parmap->status == PARMAP_WORK) {
142
143       XBT_DEBUG("Worker got a job");
144
145       void* work = xbt_parmap_next(parmap);
146       if (work != NULL) {
147         parmap->fun(work);
148       }
149
150       XBT_DEBUG("Worker has finished");
151
152     /* We are destroying the parmap */
153     } else {
154       xbt_parmap_end(parmap);
155       XBT_DEBUG("Shutting down worker");
156       return NULL;
157     }
158   }
159 }
160
161 #ifdef HAVE_FUTEX_H
162 static void futex_wait(int *uaddr, int val)
163 {
164   XBT_VERB("Waiting on futex %p", uaddr);
165   syscall(SYS_futex, uaddr, FUTEX_WAIT_PRIVATE, val, NULL, NULL, 0);
166 }
167
168 static void futex_wake(int *uaddr, int val)
169 {
170   XBT_VERB("Waking futex %p", uaddr);
171   syscall(SYS_futex, uaddr, FUTEX_WAKE_PRIVATE, val, NULL, NULL, 0);
172 }
173 #endif
174
175 /**
176  * \brief Starts the parmap: waits for all workers to be ready and returns.
177  *
178  * This function is called by the controller thread.
179  *
180  * \param parmap a parmap
181  */
182 static void xbt_parmap_start(xbt_parmap_t parmap)
183 {
184 #ifdef HAVE_FUTEX_H
185   int myflag = parmap->done;
186   __sync_fetch_and_add(&parmap->thread_counter, 1);
187   if (parmap->thread_counter < parmap->num_workers) {
188     /* wait for all workers to be ready */
189     futex_wait(&parmap->done, myflag);
190   }
191 #endif
192 }
193
194 /**
195  * \brief Wakes all workers and waits for them to finish the tasks.
196  *
197  * This function is called by the controller thread.
198  *
199  * \param parmap a parmap
200  */
201 static void xbt_parmap_signal(xbt_parmap_t parmap)
202 {
203 #ifdef HAVE_FUTEX_H
204   int myflag = parmap->done;
205   parmap->thread_counter = 0;
206   parmap->work++;
207
208   /* wake all workers */
209   futex_wake(&parmap->work, parmap->num_workers);
210
211   if (parmap->status == PARMAP_WORK) {
212     /* also work myself */
213     void* work = xbt_parmap_next(parmap);
214     if (work != NULL) {
215       parmap->fun(work);
216     }
217   }
218
219   unsigned int mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
220   if (mycount < parmap->num_workers) {
221     /* some workers have not finished yet */
222     futex_wait(&parmap->done, myflag);
223   }
224
225 #endif
226 }
227
228 /**
229  * \brief Waits for some work to process.
230  *
231  * This function is called by each worker when it has no more work to do.
232  *
233  * \param parmap a parmap
234  */
235 static void xbt_parmap_wait(xbt_parmap_t parmap)
236 {
237 #ifdef HAVE_FUTEX_H
238   int myflag;
239   unsigned int mycount;
240
241   myflag = parmap->work;
242   mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
243   if (mycount == parmap->num_workers) {
244     /* all workers have finished, wake the controller */
245     parmap->done++;
246     futex_wake(&parmap->done, 1);
247   }
248
249   /* wait for more work */
250   futex_wait(&parmap->work, myflag);
251 #endif
252 }
253
254 /**
255  * \brief Ends the parmap: wakes the controller thread when all workers terminate.
256  *
257  * This function is called by all worker threads when they end.
258  *
259  * \param parmap a parmap
260  */
261 static void xbt_parmap_end(xbt_parmap_t parmap)
262 {
263 #ifdef HAVE_FUTEX_H
264   unsigned int mycount;
265
266   mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
267   if (mycount == parmap->num_workers) {
268     /* all workers have finished, wake the controller */
269     parmap->done++;
270     futex_wake(&parmap->done, 1);
271   }
272 #endif
273 }
274
275 #ifdef SIMGRID_TEST
276 #include "xbt.h"
277 #include "xbt/ex.h"
278
279 XBT_TEST_SUITE("parmap", "Parallel Map");
280 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_parmap_unit);
281
282 xbt_parmap_t parmap;
283
284 void fun(void *arg);
285
286 void fun(void *arg)
287 {
288   //XBT_INFO("I'm job %lu", (unsigned long)arg);
289 }
290
291 XBT_TEST_UNIT("basic", test_parmap_basic, "Basic usage")
292 {
293   xbt_test_add("Create the parmap");
294
295   unsigned long i, j;
296   xbt_dynar_t data = xbt_dynar_new(sizeof(void *), NULL);
297
298   /* Create the parallel map */
299   parmap = xbt_parmap_new(10);
300
301   for (j = 0; j < 100; j++) {
302     xbt_dynar_push_as(data, void *, (void *)j);
303   }
304
305   for (i = 0; i < 5; i++) {
306     xbt_parmap_apply(parmap, fun, data);
307   }
308
309   /* Destroy the parmap */
310   xbt_parmap_destroy(parmap);
311   xbt_dynar_free(&data);
312 }
313
314 #endif /* SIMGRID_TEST */