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)
51 if (mode == XBT_PARMAP_FUTEX) {
52 std::cout << "not available\n";
59 static unsigned fibonacci(unsigned n)
64 return fibonacci(n - 1) + fibonacci(n - 2);
67 static void fun_small_comp(unsigned* arg)
72 static void fun_big_comp(unsigned* arg)
74 *arg = fibonacci(*arg % FIBO_MAX);
77 static void bench_parmap_full(int nthreads, e_xbt_parmap_mode_t mode)
79 std::cout << "** mode = " << std::left << std::setw(15) << parmap_mode_name(mode) << " ";
82 if (parmap_skip_mode(mode))
85 std::vector<unsigned> a(ARRAY_SIZE);
86 std::vector<unsigned*> data(ARRAY_SIZE);
87 std::iota(begin(a), end(a), 0);
88 std::iota(begin(data), end(data), &a[0]);
91 double start_time = xbt_os_time();
95 simgrid::xbt::Parmap<unsigned*> parmap(nthreads, mode);
96 parmap.apply(fun_to_apply, data);
97 } // enclosing block to ensure that the parmap is destroyed here.
98 elapsed_time = xbt_os_time() - start_time;
100 } while (elapsed_time < TIMEOUT);
102 std::cout << "ran " << i << " times in " << elapsed_time << " seconds (" << (i / elapsed_time) << "/s)\n";
105 static void bench_parmap_apply(int nthreads, e_xbt_parmap_mode_t mode)
107 std::cout << "** mode = " << std::left << std::setw(15) << parmap_mode_name(mode) << " ";
110 if (parmap_skip_mode(mode))
113 std::vector<unsigned> a(ARRAY_SIZE);
114 std::vector<unsigned*> data(ARRAY_SIZE);
115 std::iota(begin(a), end(a), 0);
116 std::iota(begin(data), end(data), &a[0]);
118 simgrid::xbt::Parmap<unsigned*> parmap(nthreads, mode);
120 double start_time = xbt_os_time();
123 parmap.apply(fun_to_apply, data);
124 elapsed_time = xbt_os_time() - start_time;
126 } while (elapsed_time < TIMEOUT);
128 std::cout << "ran " << i << " times in " << elapsed_time << " seconds (" << (i / elapsed_time) << "/s)\n";
131 static void bench_all_modes(void (*bench_fun)(int, e_xbt_parmap_mode_t), int nthreads, unsigned modes)
133 std::vector<e_xbt_parmap_mode_t> all_modes = {XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX, XBT_PARMAP_BUSY_WAIT,
136 for (unsigned i = 0; i < all_modes.size(); i++) {
138 bench_fun(nthreads, all_modes[i]);
142 int main(int argc, char* argv[])
145 unsigned modes = MODES_DEFAULT;
147 MSG_init(&argc, argv);
149 if (argc != 2 && argc != 3) {
150 std::cerr << "Usage: " << argv[0] << " nthreads [modes]\n"
151 << " nthreads - number of working threads\n"
152 << " modes - bitmask of modes to test\n";
155 nthreads = atoi(argv[1]);
157 std::cerr << "ERROR: invalid thread count: " << nthreads << "\n";
161 modes = strtol(argv[2], NULL, 0);
163 std::cout << "Parmap benchmark with " << nthreads << " workers (modes = " << std::hex << modes << std::dec
166 fun_to_apply = &fun_small_comp;
168 std::cout << "Benchmark for parmap create+apply+destroy (small comp):\n";
169 bench_all_modes(bench_parmap_full, nthreads, modes);
170 std::cout << std::endl;
172 std::cout << "Benchmark for parmap apply only (small comp):\n";
173 bench_all_modes(bench_parmap_apply, nthreads, modes);
174 std::cout << std::endl;
176 fun_to_apply = &fun_big_comp;
178 std::cout << "Benchmark for parmap create+apply+destroy (big comp):\n";
179 bench_all_modes(bench_parmap_full, nthreads, modes);
180 std::cout << std::endl;
182 std::cout << "Benchmark for parmap apply only (big comp):\n";
183 bench_all_modes(bench_parmap_apply, nthreads, modes);
184 std::cout << std::endl;