1 /* Copyright (c) 2012-2020. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/internal_config.h" // HAVE_FUTEX_H
7 #include "xbt/parmap.hpp"
8 #include <simgrid/s4u/Engine.hpp>
12 #include <numeric> // std::iota
16 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_bench, "Bench for parmap");
18 constexpr unsigned MODES_DEFAULT = 0x7;
19 constexpr unsigned ARRAY_SIZE = 10007;
20 constexpr unsigned FIBO_MAX = 25;
22 void (*fun_to_apply)(unsigned*);
24 static std::string parmap_mode_name(e_xbt_parmap_mode_t mode)
28 case XBT_PARMAP_POSIX:
31 case XBT_PARMAP_FUTEX:
34 case XBT_PARMAP_BUSY_WAIT:
37 case XBT_PARMAP_DEFAULT:
41 name = "UNKNOWN(" + std::to_string(mode) + ")";
47 static unsigned fibonacci(unsigned n)
52 return fibonacci(n - 1) + fibonacci(n - 2);
55 static void fun_small_comp(unsigned* arg)
60 static void fun_big_comp(unsigned* arg)
62 *arg = fibonacci(*arg % FIBO_MAX);
65 static void bench_parmap(int nthreads, double timeout, e_xbt_parmap_mode_t mode, bool full_bench)
67 std::string mode_name = parmap_mode_name(mode);
68 XBT_INFO("** mode = %s", mode_name.c_str());
70 if (mode == XBT_PARMAP_FUTEX && not HAVE_FUTEX_H) {
71 XBT_INFO(" not available");
75 std::vector<unsigned> a(ARRAY_SIZE);
76 std::vector<unsigned*> data(ARRAY_SIZE);
77 std::iota(begin(a), end(a), 0);
78 std::iota(begin(data), end(data), &a[0]);
80 auto* parmap = new simgrid::xbt::Parmap<unsigned*>(nthreads, mode);
82 double start_time = xbt_os_time();
87 parmap = new simgrid::xbt::Parmap<unsigned*>(nthreads, mode);
89 parmap->apply(fun_to_apply, data);
90 elapsed_time = xbt_os_time() - start_time;
92 } while (elapsed_time < timeout);
95 XBT_INFO(" ran %d times in %g seconds (%g/s)", i, elapsed_time, i / elapsed_time);
98 static void bench_all_modes(int nthreads, double timeout, unsigned modes, bool full_bench)
100 std::vector<e_xbt_parmap_mode_t> all_modes = {XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX, XBT_PARMAP_BUSY_WAIT,
103 for (unsigned i = 0; i < all_modes.size(); i++) {
105 bench_parmap(nthreads, timeout, all_modes[i], full_bench);
109 int main(int argc, char* argv[])
113 unsigned modes = MODES_DEFAULT;
115 xbt_log_control_set("parmap_bench.fmt:[%c/%p]%e%m%n");
116 simgrid::s4u::Engine e(&argc, argv);
118 if (argc != 3 && argc != 4) {
119 XBT_INFO("Usage: %s nthreads timeout [modes]", argv[0]);
120 XBT_INFO(" nthreads - number of working threads");
121 XBT_INFO(" timeout - max duration for each test");
122 XBT_INFO(" modes - bitmask of modes to test");
125 nthreads = atoi(argv[1]);
127 XBT_ERROR("Invalid thread count: %d", nthreads);
130 timeout = atof(argv[2]);
132 modes = strtol(argv[2], NULL, 0);
134 XBT_INFO("Parmap benchmark with %d workers (modes = %#x)...", nthreads, modes);
137 SIMIX_context_set_nthreads(nthreads);
138 fun_to_apply = &fun_small_comp;
140 XBT_INFO("Benchmark for parmap create+apply+destroy (small comp):");
141 bench_all_modes(nthreads, timeout, modes, true);
144 XBT_INFO("Benchmark for parmap apply only (small comp):");
145 bench_all_modes(nthreads, timeout, modes, false);
148 fun_to_apply = &fun_big_comp;
150 XBT_INFO("Benchmark for parmap create+apply+destroy (big comp):");
151 bench_all_modes(nthreads, timeout, modes, true);
154 XBT_INFO("Benchmark for parmap apply only (big comp):");
155 bench_all_modes(nthreads, timeout, modes, false);