1 /* Copyright (c) 2012-2017. 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 <simgrid/msg.h>
9 #include <xbt/parmap.hpp>
14 #include <numeric> // std::iota
18 #define MODES_DEFAULT 0x7
20 #define ARRAY_SIZE 10007
23 void (*fun_to_apply)(unsigned*);
25 static std::string parmap_mode_name(e_xbt_parmap_mode_t mode)
29 case XBT_PARMAP_POSIX:
32 case XBT_PARMAP_FUTEX:
35 case XBT_PARMAP_BUSY_WAIT:
38 case XBT_PARMAP_DEFAULT:
42 name = "UNKNOWN(" + std::to_string(mode) + ")";
48 static bool parmap_skip_mode(e_xbt_parmap_mode_t mode)
50 if (mode == XBT_PARMAP_FUTEX && not HAVE_FUTEX_H) {
51 std::cout << "not available\n";
58 static unsigned fibonacci(unsigned n)
63 return fibonacci(n - 1) + fibonacci(n - 2);
66 static void fun_small_comp(unsigned* arg)
71 static void fun_big_comp(unsigned* arg)
73 *arg = fibonacci(*arg % FIBO_MAX);
76 static void bench_parmap_full(int nthreads, e_xbt_parmap_mode_t mode)
78 std::cout << "** mode = " << std::left << std::setw(15) << parmap_mode_name(mode) << " ";
81 if (parmap_skip_mode(mode))
84 std::vector<unsigned> a(ARRAY_SIZE);
85 std::vector<unsigned*> data(ARRAY_SIZE);
86 std::iota(begin(a), end(a), 0);
87 std::iota(begin(data), end(data), &a[0]);
90 double start_time = xbt_os_time();
94 simgrid::xbt::Parmap<unsigned*> parmap(nthreads, mode);
95 parmap.apply(fun_to_apply, data);
96 } // enclosing block to ensure that the parmap is destroyed here.
97 elapsed_time = xbt_os_time() - start_time;
99 } while (elapsed_time < TIMEOUT);
101 std::cout << "ran " << i << " times in " << elapsed_time << " seconds (" << (i / elapsed_time) << "/s)\n";
104 static void bench_parmap_apply(int nthreads, e_xbt_parmap_mode_t mode)
106 std::cout << "** mode = " << std::left << std::setw(15) << parmap_mode_name(mode) << " ";
109 if (parmap_skip_mode(mode))
112 std::vector<unsigned> a(ARRAY_SIZE);
113 std::vector<unsigned*> data(ARRAY_SIZE);
114 std::iota(begin(a), end(a), 0);
115 std::iota(begin(data), end(data), &a[0]);
117 simgrid::xbt::Parmap<unsigned*> parmap(nthreads, mode);
119 double start_time = xbt_os_time();
122 parmap.apply(fun_to_apply, data);
123 elapsed_time = xbt_os_time() - start_time;
125 } while (elapsed_time < TIMEOUT);
127 std::cout << "ran " << i << " times in " << elapsed_time << " seconds (" << (i / elapsed_time) << "/s)\n";
130 static void bench_all_modes(void (*bench_fun)(int, e_xbt_parmap_mode_t), int nthreads, unsigned modes)
132 std::vector<e_xbt_parmap_mode_t> all_modes = {XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX, XBT_PARMAP_BUSY_WAIT,
135 for (unsigned i = 0; i < all_modes.size(); i++) {
137 bench_fun(nthreads, all_modes[i]);
141 int main(int argc, char* argv[])
144 unsigned modes = MODES_DEFAULT;
146 MSG_init(&argc, argv);
148 if (argc != 2 && argc != 3) {
149 std::cerr << "Usage: " << argv[0] << " nthreads [modes]\n"
150 << " nthreads - number of working threads\n"
151 << " modes - bitmask of modes to test\n";
154 nthreads = atoi(argv[1]);
156 std::cerr << "ERROR: invalid thread count: " << nthreads << "\n";
160 modes = strtol(argv[2], NULL, 0);
162 std::cout << "Parmap benchmark with " << nthreads << " workers (modes = " << std::hex << modes << std::dec
165 fun_to_apply = &fun_small_comp;
167 std::cout << "Benchmark for parmap create+apply+destroy (small comp):\n";
168 bench_all_modes(bench_parmap_full, nthreads, modes);
169 std::cout << std::endl;
171 std::cout << "Benchmark for parmap apply only (small comp):\n";
172 bench_all_modes(bench_parmap_apply, nthreads, modes);
173 std::cout << std::endl;
175 fun_to_apply = &fun_big_comp;
177 std::cout << "Benchmark for parmap create+apply+destroy (big comp):\n";
178 bench_all_modes(bench_parmap_full, nthreads, modes);
179 std::cout << std::endl;
181 std::cout << "Benchmark for parmap apply only (big comp):\n";
182 bench_all_modes(bench_parmap_apply, nthreads, modes);
183 std::cout << std::endl;