Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c40fa0d94172cc5e9745d84a070c2ca16ca840e9
[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 Returns the worker id of the current thread.
131  * \param parmap a parmap
132  * \return the worker id
133  */
134 unsigned long xbt_parmap_get_worker_id(xbt_parmap_t parmap)
135 {
136   return (unsigned long) xbt_os_thread_get_extra_data();
137 }
138
139 /**
140  * \brief Main function of a worker thread.
141  * \param arg the parmap
142  */
143 static void *xbt_parmap_worker_main(void *arg)
144 {
145   unsigned int worker_id;
146   xbt_parmap_t parmap = (xbt_parmap_t) arg;
147
148   /* Fetch a worker id */
149   worker_id = __sync_fetch_and_add(&parmap->workers_max_id, 1);
150   xbt_os_thread_set_extra_data((void*) (unsigned long) worker_id);
151
152   XBT_DEBUG("New worker thread created (%u)", worker_id);
153
154   /* Worker's main loop */
155   while (1) {
156     xbt_parmap_wait(parmap);
157     if (parmap->status == PARMAP_WORK) {
158
159       XBT_DEBUG("Worker %u got a job", worker_id);
160
161       void* work = xbt_parmap_next(parmap);
162       if (work != NULL) {
163         parmap->fun(work);
164       }
165
166       XBT_DEBUG("Worker %u has finished", worker_id);
167
168     /* We are destroying the parmap */
169     } else {
170       xbt_parmap_end(parmap);
171       XBT_DEBUG("Shutting down worker %u", worker_id);
172       return NULL;
173     }
174   }
175 }
176
177 #ifdef HAVE_FUTEX_H
178 static void futex_wait(int *uaddr, int val)
179 {
180   XBT_VERB("Waiting on futex %p", uaddr);
181   syscall(SYS_futex, uaddr, FUTEX_WAIT_PRIVATE, val, NULL, NULL, 0);
182 }
183
184 static void futex_wake(int *uaddr, int val)
185 {
186   XBT_VERB("Waking futex %p", uaddr);
187   syscall(SYS_futex, uaddr, FUTEX_WAKE_PRIVATE, val, NULL, NULL, 0);
188 }
189 #endif
190
191 /**
192  * \brief Starts the parmap: waits for all workers to be ready and returns.
193  *
194  * This function is called by the controller thread.
195  *
196  * \param parmap a parmap
197  */
198 static void xbt_parmap_start(xbt_parmap_t parmap)
199 {
200 #ifdef HAVE_FUTEX_H
201   int myflag = parmap->done;
202   if (parmap->thread_counter < parmap->num_workers) {
203     /* wait for all workers to be ready */
204     futex_wait(&parmap->done, myflag);
205   }
206 #endif
207 }
208
209 /**
210  * \brief Wakes all workers and waits for them to finish the tasks.
211  *
212  * This function is called by the controller thread.
213  *
214  * \param parmap a parmap
215  */
216 static void xbt_parmap_signal(xbt_parmap_t parmap)
217 {
218 #ifdef HAVE_FUTEX_H
219   int myflag = parmap->done;
220   parmap->thread_counter = 0;
221   parmap->work++;
222
223   /* wake all workers */
224   futex_wake(&parmap->work, parmap->num_workers);
225
226   /* wait for all of them to finish */
227   futex_wait(&parmap->done, myflag);
228 #endif
229 }
230
231 /**
232  * \brief Waits for some work to process.
233  *
234  * This function is called by each worker when it has no more work to do.
235  *
236  * \param parmap a parmap
237  */
238 static void xbt_parmap_wait(xbt_parmap_t parmap)
239 {
240 #ifdef HAVE_FUTEX_H
241   int myflag;
242   unsigned int mycount;
243
244   myflag = parmap->work;
245   mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
246   if (mycount == parmap->num_workers) {
247     /* all workers have finished, wake the controller */
248     parmap->done++;
249     futex_wake(&parmap->done, 1);
250   }
251
252   /* wait for more work */
253   futex_wait(&parmap->work, myflag);
254 #endif
255 }
256
257 /**
258  * \brief Ends the parmap: wakes the controller thread when all workers terminate.
259  *
260  * This function is called by all worker threads when they end.
261  *
262  * \param parmap a parmap
263  */
264 static void xbt_parmap_end(xbt_parmap_t parmap)
265 {
266 #ifdef HAVE_FUTEX_H
267   unsigned int mycount;
268
269   mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
270   if (mycount == parmap->num_workers) {
271     /* all workers have finished, wake the controller */
272     parmap->done++;
273     futex_wake(&parmap->done, 1);
274   }
275 #endif
276 }
277
278 #ifdef SIMGRID_TEST
279 #include "xbt.h"
280 #include "xbt/ex.h"
281
282 XBT_TEST_SUITE("parmap", "Parallel Map");
283 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_parmap_unit);
284
285 xbt_parmap_t parmap;
286
287 void fun(void *arg);
288
289 void fun(void *arg)
290 {
291   //XBT_INFO("I'm job %lu", (unsigned long)arg);
292 }
293
294 XBT_TEST_UNIT("basic", test_parmap_basic, "Basic usage")
295 {
296   xbt_test_add("Create the parmap");
297
298   unsigned long i, j;
299   xbt_dynar_t data = xbt_dynar_new(sizeof(void *), NULL);
300
301   /* Create the parallel map */
302   parmap = xbt_parmap_new(10);
303
304   for (j = 0; j < 100; j++) {
305     xbt_dynar_push_as(data, void *, (void *)j);
306   }
307
308   for (i = 0; i < 5; i++) {
309     xbt_parmap_apply(parmap, fun, data);
310   }
311
312   /* Destroy the parmap */
313   xbt_parmap_destroy(parmap);
314   xbt_dynar_free(&data);
315 }
316
317 #endif /* SIMGRID_TEST */