Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
start threads only if I have more than 1 element to handle
[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 2009 Da SimGrid Team. All right reserved.                      */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/sysdep.h"
10 #include "xbt/dynar.h"
11 #include "xbt/synchro.h"
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_sync);
13
14 typedef struct {
15   xbt_dynar_t data;
16   int rank;
17   void_f_int_pvoid_t function;
18   xbt_thread_t worker;
19 } s_worker_data_t,*worker_data_t;
20
21 static void worker_wait_n_free(void*w) {
22   worker_data_t worker=*(worker_data_t*)w;
23   xbt_thread_join(worker->worker);
24   xbt_free(worker);
25 }
26 static void worker_wrapper(void *w) {
27   worker_data_t me=(worker_data_t)w;
28   (*me->function)(me->rank,xbt_dynar_get_ptr(me->data,me->rank));
29 }
30
31 void xbt_dynar_dopar(xbt_dynar_t datas, void_f_int_pvoid_t function) {
32   xbt_dynar_t workers = xbt_dynar_new(sizeof(worker_data_t),worker_wait_n_free);
33   unsigned int cursor;
34   void *data;
35   if (xbt_dynar_length(datas)==0)
36     return; /* nothing to do */
37   if (xbt_dynar_length(datas)==1) {
38     /* don't start any new thread, do it directly */
39     (*function)(0,xbt_dynar_get_ptr(datas,0));
40     return;
41   }
42   /* Start all workers */
43   INFO1("Dopar for %ld elements",xbt_dynar_length(datas));
44   xbt_dynar_foreach(datas,cursor,data){
45     worker_data_t w = xbt_new0(s_worker_data_t,1);
46     w->data = datas;
47     w->function = function;
48     w->rank=cursor;
49     xbt_dynar_push(workers,&w);
50     w->worker = xbt_thread_create("dopar worker",worker_wrapper,w,1/*joinable*/);
51   }
52   /* wait them all */
53   xbt_dynar_free(&workers);
54 }
55
56 #ifdef SIMGRID_TEST
57 #define NB_ELEM 50
58 #include "xbt/synchro.h"
59
60 XBT_TEST_SUITE("synchro", "Advanced synchronization mecanisms");
61 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
62 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
63
64 static void add100(int rank,void *data) {
65   //INFO2("Thread%d: Add 100 to %d",rank,*(int*)data);
66   *(int*)data +=100;
67 }
68
69 XBT_TEST_UNIT("dopar", test_dynar_dopar, "do parallel on dynars of integers")
70 {
71   xbt_dynar_t d;
72   int i, cpt;
73   unsigned int cursor;
74
75   xbt_test_add1("==== Push %d int, add 100 to each of them in parallel and check the results", NB_ELEM);
76   d = xbt_dynar_new(sizeof(int), NULL);
77   for (cpt = 0; cpt < NB_ELEM; cpt++) {
78     xbt_dynar_push_as(d, int, cpt);     /* This is faster (and possible only with scalars) */
79     xbt_test_log2("Push %d, length=%lu", cpt, xbt_dynar_length(d));
80   }
81   xbt_dynar_dopar(d,add100);
82   cpt = 100;
83   xbt_dynar_foreach(d, cursor, i) {
84     xbt_test_assert2(i == cpt,
85                      "The retrieved value is not the expected one (%d!=%d)",
86                      i, cpt);
87     cpt++;
88   }
89   xbt_dynar_free(&d);
90 }
91
92 #endif /* SIMGRID_TEST */
93
94