Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / teshsuite / xbt / parmap_test / parmap_test.cpp
1 /* parmap_test -- test parmap                                               */
2
3 /* Copyright (c) 2007-2019. 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 <sstream>
17 #include <thread>
18 #include <vector>
19
20 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_test, "Test for parmap");
21
22 static void fun_double(unsigned* arg)
23 {
24   *arg = 2 * *arg + 1;
25 }
26
27 static int test_parmap_basic(e_xbt_parmap_mode_t mode)
28 {
29   int ret = 0;
30   for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
31     const unsigned len = 1033;
32     const unsigned num = 5;
33
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]);
39
40     for (unsigned i = 0; i < num; i++)
41       parmap.apply(fun_double, data);
42
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]);
47         ret = 1;
48         break;
49       }
50     }
51   }
52   return ret;
53 }
54
55 static void fun_get_id(std::string* arg)
56 {
57   std::stringstream ss;
58   ss << std::this_thread::get_id();
59   *arg = ss.str();
60   xbt_os_sleep(0.05);
61 }
62
63 static int test_parmap_extended(e_xbt_parmap_mode_t mode)
64 {
65   int ret = 0;
66
67   for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
68     const unsigned len = 2 * num_workers;
69
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]);
74
75     parmap.apply(fun_get_id, data);
76
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);
81       ret = 1;
82     }
83   }
84   return ret;
85 }
86
87 int main(int argc, char** argv)
88 {
89   int status = 0;
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
93
94   XBT_INFO("Basic testing posix");
95   status += test_parmap_basic(XBT_PARMAP_POSIX);
96   XBT_INFO("Basic testing futex");
97 #if HAVE_FUTEX_H
98   status += test_parmap_basic(XBT_PARMAP_FUTEX);
99 #endif
100   XBT_INFO("Basic testing busy wait");
101   status += test_parmap_basic(XBT_PARMAP_BUSY_WAIT);
102
103   XBT_INFO("Extended testing posix");
104   status += test_parmap_extended(XBT_PARMAP_POSIX);
105   XBT_INFO("Extended testing futex");
106 #if HAVE_FUTEX_H
107   status += test_parmap_extended(XBT_PARMAP_FUTEX);
108 #endif
109   XBT_INFO("Extended testing busy wait");
110   status += test_parmap_extended(XBT_PARMAP_BUSY_WAIT);
111
112   return status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
113 }