Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix sem_init testing.
[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 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_sync);
14
15 typedef struct {
16   xbt_dynar_t data;
17   int rank;
18   void_f_int_pvoid_t function;
19   xbt_thread_t worker;
20 } s_worker_data_t, *worker_data_t;
21
22 static void worker_wait_n_free(void *w)
23 {
24   worker_data_t worker = *(worker_data_t *) w;
25   xbt_thread_join(worker->worker);
26   xbt_free(worker);
27 }
28
29 static void worker_wrapper(void *w)
30 {
31   worker_data_t me = (worker_data_t) w;
32   (*me->function) (me->rank, xbt_dynar_get_ptr(me->data, me->rank));
33 }
34
35 void xbt_dynar_dopar(xbt_dynar_t datas, void_f_int_pvoid_t function)
36 {
37   xbt_dynar_t workers =
38       xbt_dynar_new(sizeof(worker_data_t), worker_wait_n_free);
39   unsigned int cursor;
40   void *data;
41   if (xbt_dynar_length(datas) == 0)
42     return;                     /* nothing to do */
43   if (xbt_dynar_length(datas) == 1) {
44     /* don't start any new thread, do it directly */
45     (*function) (0, xbt_dynar_get_ptr(datas, 0));
46     return;
47   }
48   /* Start all workers */
49   INFO1("Dopar for %ld elements", xbt_dynar_length(datas));
50   xbt_dynar_foreach(datas, cursor, data) {
51     worker_data_t w = xbt_new0(s_worker_data_t, 1);
52     w->data = datas;
53     w->function = function;
54     w->rank = cursor;
55     xbt_dynar_push(workers, &w);
56     w->worker =
57         xbt_thread_create("dopar worker", worker_wrapper, w,
58                           1 /*joinable */ );
59   }
60   /* wait them all */
61   xbt_dynar_free(&workers);
62 }
63
64 #ifdef SIMGRID_TEST
65 #define NB_ELEM 50
66 #include "xbt/synchro.h"
67
68 XBT_TEST_SUITE("synchro", "Advanced synchronization mecanisms");
69 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
70 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
71
72 static void add100(int rank, void *data)
73 {
74   //INFO2("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_add1
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_log2("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_assert2(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 */