Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change mmalloc.h into a public header
[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   worker_data_t worker=*(worker_data_t*)w;
24   xbt_thread_join(worker->worker);
25   xbt_free(worker);
26 }
27 static void worker_wrapper(void *w) {
28   worker_data_t me=(worker_data_t)w;
29   (*me->function)(me->rank,xbt_dynar_get_ptr(me->data,me->rank));
30 }
31
32 void xbt_dynar_dopar(xbt_dynar_t datas, void_f_int_pvoid_t function) {
33   xbt_dynar_t workers = xbt_dynar_new(sizeof(worker_data_t),worker_wait_n_free);
34   unsigned int cursor;
35   void *data;
36   if (xbt_dynar_length(datas)==0)
37     return; /* nothing to do */
38   if (xbt_dynar_length(datas)==1) {
39     /* don't start any new thread, do it directly */
40     (*function)(0,xbt_dynar_get_ptr(datas,0));
41     return;
42   }
43   /* Start all workers */
44   INFO1("Dopar for %ld elements",xbt_dynar_length(datas));
45   xbt_dynar_foreach(datas,cursor,data){
46     worker_data_t w = xbt_new0(s_worker_data_t,1);
47     w->data = datas;
48     w->function = function;
49     w->rank=cursor;
50     xbt_dynar_push(workers,&w);
51     w->worker = xbt_thread_create("dopar worker",worker_wrapper,w,1/*joinable*/);
52   }
53   /* wait them all */
54   xbt_dynar_free(&workers);
55 }
56
57 #ifdef SIMGRID_TEST
58 #define NB_ELEM 50
59 #include "xbt/synchro.h"
60
61 XBT_TEST_SUITE("synchro", "Advanced synchronization mecanisms");
62 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
63 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
64
65 static void add100(int rank,void *data) {
66   //INFO2("Thread%d: Add 100 to %d",rank,*(int*)data);
67   *(int*)data +=100;
68 }
69
70 XBT_TEST_UNIT("dopar", test_dynar_dopar, "do parallel on dynars of integers")
71 {
72   xbt_dynar_t d;
73   int i, cpt;
74   unsigned int cursor;
75
76   xbt_test_add1("==== Push %d int, add 100 to each of them in parallel and check the results", NB_ELEM);
77   d = xbt_dynar_new(sizeof(int), NULL);
78   for (cpt = 0; cpt < NB_ELEM; cpt++) {
79     xbt_dynar_push_as(d, int, cpt);     /* This is faster (and possible only with scalars) */
80     xbt_test_log2("Push %d, length=%lu", cpt, xbt_dynar_length(d));
81   }
82   xbt_dynar_dopar(d,add100);
83   cpt = 100;
84   xbt_dynar_foreach(d, cursor, i) {
85     xbt_test_assert2(i == cpt,
86                      "The retrieved value is not the expected one (%d!=%d)",
87                      i, cpt);
88     cpt++;
89   }
90   xbt_dynar_free(&d);
91 }
92
93 #endif /* SIMGRID_TEST */
94
95