Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f21e9f70b7a3ca7e460fcfd3c48698a2016b2cc1
[simgrid.git] / teshsuite / xbt / 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 "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",
51                      num_workers, i, expected, a[i]);
52         ret = 1;
53         break;
54       }
55     }
56
57     xbt_dynar_free(&data);
58     xbt_free(a);
59     xbt_parmap_destroy(parmap);
60   }
61   return ret;
62 }
63
64 static void fun_get_id(void *arg)
65 {
66   *(uintptr_t *)arg = (uintptr_t)xbt_os_thread_self();
67   xbt_os_sleep(0.05);
68 }
69
70 static int fun_compare(const void *pa, const void *pb)
71 {
72   uintptr_t a = *(uintptr_t *)pa;
73   uintptr_t b = *(uintptr_t *)pb;
74   return a < b ? -1 : a > b ? 1 : 0;
75 }
76
77 static int test_parmap_extended(e_xbt_parmap_mode_t mode)
78 {
79   int ret = 0;
80   unsigned num_workers;
81
82   for (num_workers = 1 ; num_workers <= 16 ; num_workers *= 2) {
83     const unsigned len = 2 * num_workers;
84     uintptr_t *a;
85     xbt_parmap_t parmap;
86     xbt_dynar_t data;
87     unsigned i;
88     unsigned count;
89
90     parmap = xbt_parmap_new(num_workers, mode);
91
92     a = xbt_malloc(len * sizeof *a);
93     data = xbt_dynar_new(sizeof a, NULL);
94     for (i = 0; i < len; i++)
95       xbt_dynar_push_as(data, void *, &a[i]);
96
97     xbt_parmap_apply(parmap, fun_get_id, data);
98
99     qsort(a, len, sizeof a[0], fun_compare);
100     count = 1;
101     for (i = 1; i < len; i++)
102       if (a[i] != a[i - 1])
103         count++;
104     if (count != num_workers) {
105       XBT_CRITICAL("only %u/%u threads did some work", count, num_workers);
106       ret = 1;
107     }
108
109     xbt_dynar_free(&data);
110     xbt_free(a);
111     xbt_parmap_destroy(parmap);
112   }
113   return ret;
114 }
115
116 int main(int argc, char** argv)
117 {
118   int status = 0;
119   SIMIX_global_init(&argc, argv);
120
121   XBT_INFO("Basic testing posix");
122   status += test_parmap_basic(XBT_PARMAP_POSIX);
123   XBT_INFO("Basic testing futex");
124 #ifdef HAVE_FUTEX_H
125   status += test_parmap_basic(XBT_PARMAP_FUTEX);
126 #endif
127   XBT_INFO("Basic testing busy wait");
128   status += test_parmap_basic(XBT_PARMAP_BUSY_WAIT);
129
130   XBT_INFO("Extended testing posix");
131   status += test_parmap_extended(XBT_PARMAP_POSIX);
132   XBT_INFO("Extended testing futex");
133 #ifdef HAVE_FUTEX_H
134   status += test_parmap_extended(XBT_PARMAP_FUTEX);
135 #endif
136   XBT_INFO("Extended testing busy wait");
137   status += test_parmap_extended(XBT_PARMAP_BUSY_WAIT);
138
139   return status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
140 }