Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Strings are overkill for parmap_test.
[simgrid.git] / teshsuite / xbt / parmap_test / parmap_test.cpp
1 /* parmap_test -- test parmap                                               */
2
3 /* Copyright (c) 2007-2021. 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/s4u/Engine.hpp>
10 #include <xbt.h>
11 #include <xbt/parmap.hpp>
12
13 #include <algorithm>
14 #include <cstdlib>
15 #include <numeric> // std::iota
16 #include <thread>
17 #include <vector>
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_test, "Test for parmap");
20
21 static void fun_double(unsigned* arg)
22 {
23   *arg = 2 * *arg + 1;
24 }
25
26 static int test_parmap_basic(e_xbt_parmap_mode_t mode)
27 {
28   int ret = 0;
29   for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
30     const unsigned len = 1033;
31     const unsigned num = 5;
32
33     simgrid::xbt::Parmap<unsigned*> parmap(num_workers, mode);
34     std::vector<unsigned> a(len);
35     std::vector<unsigned*> data(len);
36     std::iota(begin(a), end(a), 0);
37     std::iota(begin(data), end(data), &a[0]);
38
39     for (unsigned i = 0; i < num; i++)
40       parmap.apply(fun_double, data);
41
42     for (unsigned i = 0; i < len; i++) {
43       unsigned expected = (1U << num) * (i + 1) - 1;
44       if (a[i] != expected) {
45         XBT_CRITICAL("with %u threads, a[%u]: expected %u, got %u", num_workers, i, expected, a[i]);
46         ret = 1;
47         break;
48       }
49     }
50   }
51   return ret;
52 }
53
54 static void fun_get_id(std::thread::id* arg)
55 {
56   *arg = std::this_thread::get_id();
57   xbt_os_sleep(0.05);
58 }
59
60 static int test_parmap_extended(e_xbt_parmap_mode_t mode)
61 {
62   int ret = 0;
63
64   for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
65     const unsigned len = 2 * num_workers;
66
67     simgrid::xbt::Parmap<std::thread::id*> parmap(num_workers, mode);
68     std::vector<std::thread::id> a(len);
69     std::vector<std::thread::id*> data(len);
70     std::iota(begin(data), end(data), &a[0]);
71
72     parmap.apply(fun_get_id, data);
73
74     std::sort(begin(a), end(a));
75     unsigned count = static_cast<unsigned>(std::distance(begin(a), std::unique(begin(a), end(a))));
76     if (count != num_workers) {
77       XBT_CRITICAL("only %u/%u threads did some work", count, num_workers);
78       ret = 1;
79     }
80   }
81   return ret;
82 }
83
84 int main(int argc, char** argv)
85 {
86   int status = 0;
87   xbt_log_control_set("parmap_test.fmt:[%c/%p]%e%m%n");
88   simgrid::s4u::Engine e(&argc, argv);
89   SIMIX_context_set_nthreads(16); // dummy value > 1
90
91   XBT_INFO("Basic testing posix");
92   status += test_parmap_basic(XBT_PARMAP_POSIX);
93   XBT_INFO("Basic testing futex");
94 #if HAVE_FUTEX_H
95   status += test_parmap_basic(XBT_PARMAP_FUTEX);
96 #endif
97   XBT_INFO("Basic testing busy wait");
98   status += test_parmap_basic(XBT_PARMAP_BUSY_WAIT);
99
100   XBT_INFO("Extended testing posix");
101   status += test_parmap_extended(XBT_PARMAP_POSIX);
102   XBT_INFO("Extended testing futex");
103 #if HAVE_FUTEX_H
104   status += test_parmap_extended(XBT_PARMAP_FUTEX);
105 #endif
106   XBT_INFO("Extended testing busy wait");
107   status += test_parmap_extended(XBT_PARMAP_BUSY_WAIT);
108
109   return status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
110 }