Logo AND Algorithmique Numérique Distribuée

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