Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
xbt_trp and xbt_datadesc were removed.
[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_is_empty(datas))
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   XBT_DEBUG("Dopar for %lu 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_DEFAULT_CATEGORY(xbt_dyn);
70
71 static void add100(int rank, void *data)
72 {
73   //XBT_INFO("Thread%d: Add 100 to %d",rank,*(int*)data);
74   *(int *) data += 100;
75 }
76
77 XBT_TEST_UNIT("dopar", test_dynar_dopar, "do parallel on dynars of integers")
78 {
79   xbt_dynar_t d;
80   int i, cpt;
81   unsigned int cursor;
82
83   xbt_test_add
84       ("==== Push %d int, add 100 to each of them in parallel and check the results",
85        NB_ELEM);
86   d = xbt_dynar_new(sizeof(int), NULL);
87   for (cpt = 0; cpt < NB_ELEM; cpt++) {
88     xbt_dynar_push_as(d, int, cpt);     /* This is faster (and possible only with scalars) */
89     xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
90   }
91   xbt_dynar_dopar(d, add100);
92   cpt = 100;
93   xbt_dynar_foreach(d, cursor, i) {
94     xbt_test_assert(i == cpt,
95                      "The retrieved value is not the expected one (%d!=%d)",
96                      i, cpt);
97     cpt++;
98   }
99   xbt_dynar_free(&d);
100 }
101
102 #endif                          /* SIMGRID_TEST */