Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use the CMake macro #cmakedefine01 in configurable files.
[simgrid.git] / teshsuite / xbt / parmap_test / parmap_test.c
1 /* parmap_test -- test parmap                                               */
2
3 /* Copyright (c) 2007-2010, 2013-2014. The SimGrid Team.
4  * All rights 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 "simgrid/simix.h"
10 #include "xbt.h"
11 #include "xbt/ex.h"
12 #include "xbt/xbt_os_time.h"
13 #include "src/internal_config.h"
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_test, "Test for parmap");
16
17 static void fun_double(void *arg)
18 {
19   unsigned *u = arg;
20   *u = 2 * *u + 1;
21 }
22
23 static int test_parmap_basic(e_xbt_parmap_mode_t mode)
24 {
25   int ret = 0;
26   unsigned num_workers;
27   for (num_workers = 1 ; num_workers <= 16 ; num_workers *= 2) {
28     const unsigned len = 1033;
29     const unsigned num = 5;
30     unsigned *a;
31     xbt_dynar_t data;
32     xbt_parmap_t parmap;
33     unsigned i;
34
35     parmap = xbt_parmap_new(num_workers, mode);
36
37     a = xbt_malloc(len * sizeof *a);
38     data = xbt_dynar_new(sizeof a, NULL);
39     for (i = 0; i < len; i++) {
40       a[i] = i;
41       xbt_dynar_push_as(data, void *, &a[i]);
42     }
43
44     for (i = 0; i < num; i++)
45       xbt_parmap_apply(parmap, fun_double, data);
46
47     for (i = 0; i < len; i++) {
48       unsigned expected = (1U << num) * (i + 1) - 1;
49       if (a[i] != expected) {
50         XBT_CRITICAL("with %u threads, a[%u]: expected %u, got %u", num_workers, i, expected, a[i]);
51         ret = 1;
52         break;
53       }
54     }
55
56     xbt_dynar_free(&data);
57     xbt_free(a);
58     xbt_parmap_destroy(parmap);
59   }
60   return ret;
61 }
62
63 static void fun_get_id(void *arg)
64 {
65   *(uintptr_t *)arg = (uintptr_t)xbt_os_thread_self();
66   xbt_os_sleep(0.05);
67 }
68
69 static int fun_compare(const void *pa, const void *pb)
70 {
71   uintptr_t a = *(uintptr_t *)pa;
72   uintptr_t b = *(uintptr_t *)pb;
73   return a < b ? -1 : a > b ? 1 : 0;
74 }
75
76 static int test_parmap_extended(e_xbt_parmap_mode_t mode)
77 {
78   int ret = 0;
79   unsigned num_workers;
80
81   for (num_workers = 1 ; num_workers <= 16 ; num_workers *= 2) {
82     const unsigned len = 2 * num_workers;
83     uintptr_t *a;
84     xbt_parmap_t parmap;
85     xbt_dynar_t data;
86     unsigned i;
87     unsigned count;
88
89     parmap = xbt_parmap_new(num_workers, mode);
90
91     a = xbt_malloc(len * sizeof *a);
92     data = xbt_dynar_new(sizeof a, NULL);
93     for (i = 0; i < len; i++)
94       xbt_dynar_push_as(data, void *, &a[i]);
95
96     xbt_parmap_apply(parmap, fun_get_id, data);
97
98     qsort(a, len, sizeof a[0], fun_compare);
99     count = 1;
100     for (i = 1; i < len; i++)
101       if (a[i] != a[i - 1])
102         count++;
103     if (count != num_workers) {
104       XBT_CRITICAL("only %u/%u threads did some work", count, num_workers);
105       ret = 1;
106     }
107
108     xbt_dynar_free(&data);
109     xbt_free(a);
110     xbt_parmap_destroy(parmap);
111   }
112   return ret;
113 }
114
115 int main(int argc, char** argv)
116 {
117   int status = 0;
118   SIMIX_global_init(&argc, argv);
119
120   XBT_INFO("Basic testing posix");
121   status += test_parmap_basic(XBT_PARMAP_POSIX);
122   XBT_INFO("Basic testing futex");
123 #if HAVE_FUTEX_H
124   status += test_parmap_basic(XBT_PARMAP_FUTEX);
125 #endif
126   XBT_INFO("Basic testing busy wait");
127   status += test_parmap_basic(XBT_PARMAP_BUSY_WAIT);
128
129   XBT_INFO("Extended testing posix");
130   status += test_parmap_extended(XBT_PARMAP_POSIX);
131   XBT_INFO("Extended testing futex");
132 #if HAVE_FUTEX_H
133   status += test_parmap_extended(XBT_PARMAP_FUTEX);
134 #endif
135   XBT_INFO("Extended testing busy wait");
136   status += test_parmap_extended(XBT_PARMAP_BUSY_WAIT);
137
138   return status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
139 }