Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into fix/execute_benched
[simgrid.git] / teshsuite / xbt / parmap_test / parmap_test.cpp
1 /* parmap_test -- test parmap                                               */
2
3 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "src/internal_config.h" // HAVE_FUTEX_H
9 #include <simgrid/msg.h>
10 #include <xbt.h>
11 #include <xbt/parmap.hpp>
12
13 #include <algorithm>
14 #include <cstdlib>
15 #include <numeric> // std::iota
16 #include <vector>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_test, "Test for parmap");
19
20 static void fun_double(unsigned* arg)
21 {
22   *arg = 2 * *arg + 1;
23 }
24
25 static int test_parmap_basic(e_xbt_parmap_mode_t mode)
26 {
27   int ret = 0;
28   for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
29     const unsigned len = 1033;
30     const unsigned num = 5;
31
32     simgrid::xbt::Parmap<unsigned*> parmap(num_workers, mode);
33     std::vector<unsigned> a(len);
34     std::vector<unsigned*> data(len);
35     std::iota(begin(a), end(a), 0);
36     std::iota(begin(data), end(data), &a[0]);
37
38     for (unsigned i = 0; i < num; i++)
39       parmap.apply(fun_double, data);
40
41     for (unsigned i = 0; i < len; i++) {
42       unsigned expected = (1U << num) * (i + 1) - 1;
43       if (a[i] != expected) {
44         XBT_CRITICAL("with %u threads, a[%u]: expected %u, got %u", num_workers, i, expected, a[i]);
45         ret = 1;
46         break;
47       }
48     }
49   }
50   return ret;
51 }
52
53 static void fun_get_id(uintptr_t* arg)
54 {
55   *arg = (uintptr_t)xbt_os_thread_self();
56   xbt_os_sleep(0.05);
57 }
58
59 static int test_parmap_extended(e_xbt_parmap_mode_t mode)
60 {
61   int ret = 0;
62
63   for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
64     const unsigned len = 2 * num_workers;
65
66     simgrid::xbt::Parmap<uintptr_t*> parmap(num_workers, mode);
67     std::vector<uintptr_t> a(len);
68     std::vector<uintptr_t*> data(len);
69     std::iota(begin(data), end(data), &a[0]);
70
71     parmap.apply(fun_get_id, data);
72
73     std::sort(begin(a), end(a));
74     unsigned count = std::distance(begin(a), std::unique(begin(a), end(a)));
75     if (count != num_workers) {
76       XBT_CRITICAL("only %u/%u threads did some work", count, num_workers);
77       ret = 1;
78     }
79   }
80   return ret;
81 }
82
83 int main(int argc, char** argv)
84 {
85   int status = 0;
86   xbt_log_control_set("parmap_test.fmt:[%c/%p]%e%m%n");
87   MSG_init(&argc, argv);
88   SIMIX_context_set_nthreads(16); // dummy value > 1
89
90   XBT_INFO("Basic testing posix");
91   status += test_parmap_basic(XBT_PARMAP_POSIX);
92   XBT_INFO("Basic testing futex");
93 #if HAVE_FUTEX_H
94   status += test_parmap_basic(XBT_PARMAP_FUTEX);
95 #endif
96   XBT_INFO("Basic testing busy wait");
97   status += test_parmap_basic(XBT_PARMAP_BUSY_WAIT);
98
99   XBT_INFO("Extended testing posix");
100   status += test_parmap_extended(XBT_PARMAP_POSIX);
101   XBT_INFO("Extended testing futex");
102 #if HAVE_FUTEX_H
103   status += test_parmap_extended(XBT_PARMAP_FUTEX);
104 #endif
105   XBT_INFO("Extended testing busy wait");
106   status += test_parmap_extended(XBT_PARMAP_BUSY_WAIT);
107
108   return status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
109 }