Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Restore parmap unitTesting as part of teshsuite.
[simgrid.git] / teshsuite / xbt / parmap_test.c
1 /* parmap_test -- test parmap                                               */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. 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 void test_parmap_basic(e_xbt_parmap_mode_t mode)
24 {
25   unsigned num_workers;
26   for (num_workers = 1 ; num_workers <= 16 ; num_workers *= 2) {
27     const unsigned len = 1033;
28     const unsigned num = 5;
29     unsigned *a;
30     xbt_dynar_t data;
31     xbt_parmap_t parmap;
32     unsigned i;
33
34     parmap = xbt_parmap_new(num_workers, mode);
35
36     a = xbt_malloc(len * sizeof *a);
37     data = xbt_dynar_new(sizeof a, NULL);
38     for (i = 0; i < len; i++) {
39       a[i] = i;
40       xbt_dynar_push_as(data, void *, &a[i]);
41     }
42
43     for (i = 0; i < num; i++)
44       xbt_parmap_apply(parmap, fun_double, data);
45
46     for (i = 0; i < len; i++) {
47       unsigned expected = (1U << num) * (i + 1) - 1;
48       xbt_test_assert(a[i] == expected,
49                       "a[%u]: expected %u, got %u", i, expected, a[i]);
50     }
51
52     xbt_dynar_free(&data);
53     xbt_free(a);
54     xbt_parmap_destroy(parmap);
55   }
56 }
57
58 static void fun_get_id(void *arg)
59 {
60   *(uintptr_t *)arg = (uintptr_t)xbt_os_thread_self();
61   xbt_os_sleep(0.5);
62 }
63
64 static int fun_compare(const void *pa, const void *pb)
65 {
66   uintptr_t a = *(uintptr_t *)pa;
67   uintptr_t b = *(uintptr_t *)pb;
68   return a < b ? -1 : a > b ? 1 : 0;
69 }
70
71 static void test_parmap_extended(e_xbt_parmap_mode_t mode)
72 {
73   unsigned num_workers;
74
75   for (num_workers = 1 ; num_workers <= 16 ; num_workers *= 2) {
76     const unsigned len = 2 * num_workers;
77     uintptr_t *a;
78     xbt_parmap_t parmap;
79     xbt_dynar_t data;
80     unsigned i;
81     unsigned count;
82
83     parmap = xbt_parmap_new(num_workers, mode);
84
85     a = xbt_malloc(len * sizeof *a);
86     data = xbt_dynar_new(sizeof a, NULL);
87     for (i = 0; i < len; i++)
88       xbt_dynar_push_as(data, void *, &a[i]);
89
90     xbt_parmap_apply(parmap, fun_get_id, data);
91
92     qsort(a, len, sizeof a[0], fun_compare);
93     count = 1;
94     for (i = 1; i < len; i++)
95       if (a[i] != a[i - 1])
96         count++;
97     xbt_test_assert(count == num_workers,
98                     "only %u/%u threads did some work", count, num_workers);
99
100     xbt_dynar_free(&data);
101     xbt_free(a);
102     xbt_parmap_destroy(parmap);
103   }
104 }
105
106 int main(int argc, char** argv)
107 {
108   SIMIX_global_init(&argc, argv);
109
110   XBT_INFO("Basic testing posix");
111   test_parmap_basic(XBT_PARMAP_POSIX);
112   XBT_INFO("Basic testing futex");
113 #ifdef HAVE_FUTEX_H
114   test_parmap_basic(XBT_PARMAP_FUTEX);
115 #endif
116   XBT_INFO("Basic testing busy wait");
117   test_parmap_basic(XBT_PARMAP_BUSY_WAIT);
118
119   XBT_INFO("Extended testing posix");
120   test_parmap_extended(XBT_PARMAP_POSIX);
121   XBT_INFO("Extended testing futex");
122 #ifdef HAVE_FUTEX_H
123   test_parmap_extended(XBT_PARMAP_FUTEX);
124 #endif
125   XBT_INFO("Extended testing busy wait");
126   test_parmap_extended(XBT_PARMAP_BUSY_WAIT);
127
128   return 0;
129 }