Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use real threads for xbt_dynar_dopar.
[simgrid.git] / src / xbt / xbt_synchro.c
1 /* xbt_synchro -- advanced multithreaded features                           */
2 /* Working on top of real threads in RL and of simulated processes in SG    */
3
4 /* Copyright (c) 2009, 2010. The SimGrid Team.
5  * All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/sysdep.h"
11 #include "xbt/dynar.h"
12 #include "xbt/synchro.h"
13 #include "xbt/xbt_os_thread.h"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_sync);
15
16 typedef struct {
17   xbt_dynar_t data;
18   int rank;
19   void_f_int_pvoid_t function;
20   xbt_os_thread_t worker;
21 } s_worker_data_t, *worker_data_t;
22
23 static void worker_wait_n_free(void *w)
24 {
25   worker_data_t worker = *(worker_data_t *) w;
26   xbt_os_thread_join(worker->worker, NULL);
27   xbt_free(worker);
28 }
29
30 static void *worker_wrapper(void *w)
31 {
32   worker_data_t me = (worker_data_t) w;
33   me->function(me->rank, xbt_dynar_get_ptr(me->data, me->rank));
34   return NULL;
35 }
36
37 void xbt_dynar_dopar(xbt_dynar_t datas, void_f_int_pvoid_t function)
38 {
39   xbt_dynar_t workers =
40       xbt_dynar_new(sizeof(worker_data_t), worker_wait_n_free);
41   unsigned int cursor;
42   void *data;
43   if (xbt_dynar_is_empty(datas))
44     return;                     /* nothing to do */
45   if (xbt_dynar_length(datas) == 1) {
46     /* don't start any new thread, do it directly */
47     function(0, xbt_dynar_get_ptr(datas, 0));
48     return;
49   }
50   /* Start all workers */
51   XBT_DEBUG("Dopar for %lu elements", xbt_dynar_length(datas));
52   xbt_dynar_foreach(datas, cursor, data) {
53     worker_data_t w = xbt_new0(s_worker_data_t, 1);
54     w->data = datas;
55     w->function = function;
56     w->rank = cursor;
57     xbt_dynar_push(workers, &w);
58     w->worker =
59         xbt_os_thread_create("dopar worker", worker_wrapper, w, NULL);
60   }
61   /* wait them all */
62   xbt_dynar_free(&workers);
63 }
64
65 #ifdef SIMGRID_TEST
66 #define NB_ELEM 50
67 #include "xbt/synchro.h"
68
69 XBT_TEST_SUITE("synchro", "Advanced synchronization mecanisms");
70 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dyn);
71
72 static void add100(int rank, void *data)
73 {
74   //XBT_INFO("Thread%d: Add 100 to %d",rank,*(int*)data);
75   *(int *) data += 100;
76 }
77
78 XBT_TEST_UNIT("dopar", test_dynar_dopar, "do parallel on dynars of integers")
79 {
80   xbt_dynar_t d;
81   int i, cpt;
82   unsigned int cursor;
83
84   xbt_test_add
85       ("==== Push %d int, add 100 to each of them in parallel and check the results",
86        NB_ELEM);
87   d = xbt_dynar_new(sizeof(int), NULL);
88   for (cpt = 0; cpt < NB_ELEM; cpt++) {
89     xbt_dynar_push_as(d, int, cpt);     /* This is faster (and possible only with scalars) */
90     xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
91   }
92   xbt_dynar_dopar(d, add100);
93   cpt = 100;
94   xbt_dynar_foreach(d, cursor, i) {
95     xbt_test_assert(i == cpt,
96                      "The retrieved value is not the expected one (%d!=%d)",
97                      i, cpt);
98     cpt++;
99   }
100   xbt_dynar_free(&d);
101 }
102
103 #endif                          /* SIMGRID_TEST */