Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b9db0756a4b91ef02fe064f1d35aa48113f1e584
[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   xbt_parmap_t parmap = (xbt_parmap_t) arg;
136
137   XBT_DEBUG("New worker thread created");
138
139   /* Worker's main loop */
140   while (1) {
141     xbt_parmap_wait(parmap);
142     if (parmap->status == PARMAP_WORK) {
143
144       XBT_DEBUG("Worker got a job");
145
146       void* work = xbt_parmap_next(parmap);
147       if (work != NULL) {
148         parmap->fun(work);
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   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   /* wait for all of them to finish */
212   futex_wait(&parmap->done, myflag);
213 #endif
214 }
215
216 /**
217  * \brief Waits for some work to process.
218  *
219  * This function is called by each worker when it has no more work to do.
220  *
221  * \param parmap a parmap
222  */
223 static void xbt_parmap_wait(xbt_parmap_t parmap)
224 {
225 #ifdef HAVE_FUTEX_H
226   int myflag;
227   unsigned int mycount;
228
229   myflag = parmap->work;
230   mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
231   if (mycount == parmap->num_workers) {
232     /* all workers have finished, wake the controller */
233     parmap->done++;
234     futex_wake(&parmap->done, 1);
235   }
236
237   /* wait for more work */
238   futex_wait(&parmap->work, myflag);
239 #endif
240 }
241
242 /**
243  * \brief Ends the parmap: wakes the controller thread when all workers terminate.
244  *
245  * This function is called by all worker threads when they end.
246  *
247  * \param parmap a parmap
248  */
249 static void xbt_parmap_end(xbt_parmap_t parmap)
250 {
251 #ifdef HAVE_FUTEX_H
252   unsigned int mycount;
253
254   mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
255   if (mycount == parmap->num_workers) {
256     /* all workers have finished, wake the controller */
257     parmap->done++;
258     futex_wake(&parmap->done, 1);
259   }
260 #endif
261 }
262
263 #ifdef SIMGRID_TEST
264 #include "xbt.h"
265 #include "xbt/ex.h"
266
267 XBT_TEST_SUITE("parmap", "Parallel Map");
268 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_parmap_unit);
269
270 xbt_parmap_t parmap;
271
272 void fun(void *arg);
273
274 void fun(void *arg)
275 {
276   //XBT_INFO("I'm job %lu", (unsigned long)arg);
277 }
278
279 XBT_TEST_UNIT("basic", test_parmap_basic, "Basic usage")
280 {
281   xbt_test_add("Create the parmap");
282
283   unsigned long i, j;
284   xbt_dynar_t data = xbt_dynar_new(sizeof(void *), NULL);
285
286   /* Create the parallel map */
287   parmap = xbt_parmap_new(10);
288
289   for (j = 0; j < 100; j++) {
290     xbt_dynar_push_as(data, void *, (void *)j);
291   }
292
293   for (i = 0; i < 5; i++) {
294     xbt_parmap_apply(parmap, fun, data);
295   }
296
297   /* Destroy the parmap */
298   xbt_parmap_destroy(parmap);
299   xbt_dynar_free(&data);
300 }
301
302 #endif /* SIMGRID_TEST */