1 /* parmap_test -- test parmap */
3 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
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. */
8 #include "src/internal_config.h" // HAVE_FUTEX_H
9 #include <simgrid/s4u/Engine.hpp>
11 #include <xbt/parmap.hpp>
15 #include <numeric> // std::iota
19 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_test, "Test for parmap");
21 static void fun_double(unsigned* arg)
26 static int test_parmap_basic(e_xbt_parmap_mode_t mode)
29 for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
30 const unsigned len = 1033;
31 const unsigned num = 5;
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]);
39 for (unsigned i = 0; i < num; i++)
40 parmap.apply(fun_double, data);
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]);
54 static void fun_get_id(std::thread::id* arg)
56 *arg = std::this_thread::get_id();
60 static int test_parmap_extended(e_xbt_parmap_mode_t mode)
64 for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
65 const unsigned len = 2 * num_workers;
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]);
72 parmap.apply(fun_get_id, data);
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);
84 int main(int argc, char** argv)
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
91 XBT_INFO("Basic testing posix");
92 status += test_parmap_basic(XBT_PARMAP_POSIX);
93 XBT_INFO("Basic testing futex");
95 status += test_parmap_basic(XBT_PARMAP_FUTEX);
97 XBT_INFO("Basic testing busy wait");
98 status += test_parmap_basic(XBT_PARMAP_BUSY_WAIT);
100 XBT_INFO("Extended testing posix");
101 status += test_parmap_extended(XBT_PARMAP_POSIX);
102 XBT_INFO("Extended testing futex");
104 status += test_parmap_extended(XBT_PARMAP_FUTEX);
106 XBT_INFO("Extended testing busy wait");
107 status += test_parmap_extended(XBT_PARMAP_BUSY_WAIT);
109 return status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;