1 /* parmap_test -- test parmap */
3 /* Copyright (c) 2007-2019. 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/msg.h>
11 #include <xbt/parmap.hpp>
15 #include <numeric> // std::iota
20 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_test, "Test for parmap");
22 static void fun_double(unsigned* arg)
27 static int test_parmap_basic(e_xbt_parmap_mode_t mode)
30 for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
31 const unsigned len = 1033;
32 const unsigned num = 5;
34 simgrid::xbt::Parmap<unsigned*> parmap(num_workers, mode);
35 std::vector<unsigned> a(len);
36 std::vector<unsigned*> data(len);
37 std::iota(begin(a), end(a), 0);
38 std::iota(begin(data), end(data), &a[0]);
40 for (unsigned i = 0; i < num; i++)
41 parmap.apply(fun_double, data);
43 for (unsigned i = 0; i < len; i++) {
44 unsigned expected = (1U << num) * (i + 1) - 1;
45 if (a[i] != expected) {
46 XBT_CRITICAL("with %u threads, a[%u]: expected %u, got %u", num_workers, i, expected, a[i]);
55 static void fun_get_id(std::string* arg)
58 ss << std::this_thread::get_id();
63 static int test_parmap_extended(e_xbt_parmap_mode_t mode)
67 for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
68 const unsigned len = 2 * num_workers;
70 simgrid::xbt::Parmap<std::string*> parmap(num_workers, mode);
71 std::vector<std::string> a(len);
72 std::vector<std::string*> data(len);
73 std::iota(begin(data), end(data), &a[0]);
75 parmap.apply(fun_get_id, data);
77 std::sort(begin(a), end(a));
78 unsigned count = std::distance(begin(a), std::unique(begin(a), end(a)));
79 if (count != num_workers) {
80 XBT_CRITICAL("only %u/%u threads did some work", count, num_workers);
87 int main(int argc, char** argv)
90 xbt_log_control_set("parmap_test.fmt:[%c/%p]%e%m%n");
91 MSG_init(&argc, argv);
92 SIMIX_context_set_nthreads(16); // dummy value > 1
94 XBT_INFO("Basic testing posix");
95 status += test_parmap_basic(XBT_PARMAP_POSIX);
96 XBT_INFO("Basic testing futex");
98 status += test_parmap_basic(XBT_PARMAP_FUTEX);
100 XBT_INFO("Basic testing busy wait");
101 status += test_parmap_basic(XBT_PARMAP_BUSY_WAIT);
103 XBT_INFO("Extended testing posix");
104 status += test_parmap_extended(XBT_PARMAP_POSIX);
105 XBT_INFO("Extended testing futex");
107 status += test_parmap_extended(XBT_PARMAP_FUTEX);
109 XBT_INFO("Extended testing busy wait");
110 status += test_parmap_extended(XBT_PARMAP_BUSY_WAIT);
112 return status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;