Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / teshsuite / xbt / parmap_bench / parmap_bench.cpp
1 /* Copyright (c) 2012-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/internal_config.h" // HAVE_FUTEX_H
7 #include "xbt/parmap.hpp"
8 #include <simgrid/s4u/Engine.hpp>
9 #include <xbt.h>
10
11 #include <cstdlib>
12 #include <numeric> // std::iota
13 #include <string>
14 #include <vector>
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_bench, "Bench for parmap");
17
18 constexpr unsigned MODES_DEFAULT = 0x7;
19 constexpr unsigned ARRAY_SIZE    = 10007;
20 constexpr unsigned FIBO_MAX      = 25;
21
22 static std::string parmap_mode_name(e_xbt_parmap_mode_t mode)
23 {
24   std::string name;
25   switch (mode) {
26     case XBT_PARMAP_POSIX:
27       name = "POSIX";
28       break;
29     case XBT_PARMAP_FUTEX:
30       name = "FUTEX";
31       break;
32     case XBT_PARMAP_BUSY_WAIT:
33       name = "BUSY_WAIT";
34       break;
35     case XBT_PARMAP_DEFAULT:
36       name = "DEFAULT";
37       break;
38     default:
39       name = "UNKNOWN(" + std::to_string(mode) + ")";
40       break;
41   }
42   return name;
43 }
44
45 static unsigned fibonacci(unsigned n)
46 {
47   if (n < 2)
48     return n;
49   else
50     return fibonacci(n - 1) + fibonacci(n - 2);
51 }
52
53 static void fun_small_comp(unsigned* arg)
54 {
55   *arg = 2 * *arg + 1;
56 }
57
58 static void fun_big_comp(unsigned* arg)
59 {
60   *arg = fibonacci(*arg % FIBO_MAX);
61 }
62
63 template <class F>
64 void bench_parmap(int nthreads, double timeout, e_xbt_parmap_mode_t mode, bool full_bench, F func_to_apply)
65 {
66   std::string mode_name = parmap_mode_name(mode);
67   XBT_INFO("** mode = %s", mode_name.c_str());
68
69   if (mode == XBT_PARMAP_FUTEX && not HAVE_FUTEX_H) {
70     XBT_INFO("   not available");
71     return;
72   }
73
74   std::vector<unsigned> a(ARRAY_SIZE);
75   std::vector<unsigned*> data(ARRAY_SIZE);
76   std::iota(begin(a), end(a), 0);
77   std::iota(begin(data), end(data), &a[0]);
78
79   auto* parmap      = new simgrid::xbt::Parmap<unsigned*>(nthreads, mode);
80   int i             = 0;
81   double start_time = xbt_os_time();
82   double elapsed_time;
83   do {
84     if (full_bench) {
85       delete parmap;
86       parmap = new simgrid::xbt::Parmap<unsigned*>(nthreads, mode);
87     }
88     parmap->apply(func_to_apply, data);
89     elapsed_time = xbt_os_time() - start_time;
90     i++;
91   } while (elapsed_time < timeout);
92   delete parmap;
93
94   XBT_INFO("   ran %d times in %g seconds (%g/s)", i, elapsed_time, i / elapsed_time);
95 }
96
97 template <class F> void bench_all_modes(int nthreads, double timeout, unsigned modes, bool full_bench, F func_to_apply)
98 {
99   std::vector<e_xbt_parmap_mode_t> all_modes = {XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX, XBT_PARMAP_BUSY_WAIT,
100                                                 XBT_PARMAP_DEFAULT};
101
102   for (unsigned i = 0; i < all_modes.size(); i++) {
103     if (1U << i & modes)
104       bench_parmap(nthreads, timeout, all_modes[i], full_bench, func_to_apply);
105   }
106 }
107
108 int main(int argc, char* argv[])
109 {
110   int nthreads;
111   double timeout;
112   unsigned modes = MODES_DEFAULT;
113
114   xbt_log_control_set("parmap_bench.fmt:[%c/%p]%e%m%n");
115   simgrid::s4u::Engine e(&argc, argv);
116
117   if (argc != 3 && argc != 4) {
118     XBT_INFO("Usage: %s nthreads timeout [modes]", argv[0]);
119     XBT_INFO("    nthreads - number of working threads");
120     XBT_INFO("    timeout  - max duration for each test");
121     XBT_INFO("    modes    - bitmask of modes to test");
122     return EXIT_FAILURE;
123   }
124   nthreads = atoi(argv[1]);
125   if (nthreads < 1) {
126     XBT_ERROR("Invalid thread count: %d", nthreads);
127     return EXIT_FAILURE;
128   }
129   timeout = atof(argv[2]);
130   if (argc == 4)
131     modes = static_cast<unsigned>(strtoul(argv[2], nullptr, 0));
132
133   XBT_INFO("Parmap benchmark with %d workers (modes = %#x)...", nthreads, modes);
134   XBT_INFO("%s", "");
135
136   simgrid::kernel::context::set_nthreads(nthreads);
137
138   XBT_INFO("Benchmark for parmap create+apply+destroy (small comp):");
139   bench_all_modes(nthreads, timeout, modes, true, &fun_small_comp);
140   XBT_INFO("%s", "");
141
142   XBT_INFO("Benchmark for parmap apply only (small comp):");
143   bench_all_modes(nthreads, timeout, modes, false, &fun_small_comp);
144   XBT_INFO("%s", "");
145
146   XBT_INFO("Benchmark for parmap create+apply+destroy (big comp):");
147   bench_all_modes(nthreads, timeout, modes, true, &fun_big_comp);
148   XBT_INFO("%s", "");
149
150   XBT_INFO("Benchmark for parmap apply only (big comp):");
151   bench_all_modes(nthreads, timeout, modes, false, &fun_big_comp);
152   XBT_INFO("%s", "");
153
154   return EXIT_SUCCESS;
155 }