Logo AND Algorithmique Numérique Distribuée

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