X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/fcd7ccb546f1381f883b7522b41bf47c63d745b3..0122fe3fb5f5610a68eacdaabc4dec96ca408516:/teshsuite/xbt/parmap_bench/parmap_bench.cpp diff --git a/teshsuite/xbt/parmap_bench/parmap_bench.cpp b/teshsuite/xbt/parmap_bench/parmap_bench.cpp index f7d0fcce86..c57a9b1877 100644 --- a/teshsuite/xbt/parmap_bench/parmap_bench.cpp +++ b/teshsuite/xbt/parmap_bench/parmap_bench.cpp @@ -1,26 +1,23 @@ -/* Copyright (c) 2012-2017. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2012-2020. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ #include "src/internal_config.h" // HAVE_FUTEX_H -#include +#include "xbt/parmap.hpp" +#include #include -#include #include -#include -#include #include // std::iota #include #include -#define MODES_DEFAULT 0x7 -#define TIMEOUT 10.0 -#define ARRAY_SIZE 10007 -#define FIBO_MAX 25 +XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_bench, "Bench for parmap"); -void (*fun_to_apply)(unsigned*); +constexpr unsigned MODES_DEFAULT = 0x7; +constexpr unsigned ARRAY_SIZE = 10007; +constexpr unsigned FIBO_MAX = 25; static std::string parmap_mode_name(e_xbt_parmap_mode_t mode) { @@ -45,17 +42,6 @@ static std::string parmap_mode_name(e_xbt_parmap_mode_t mode) return name; } -static bool parmap_skip_mode(e_xbt_parmap_mode_t mode) -{ -#if !HAVE_FUTEX_H - if (mode == XBT_PARMAP_FUTEX) { - std::cout << "not available\n"; - return true; - } else -#endif - return false; -} - static unsigned fibonacci(unsigned n) { if (n < 2) @@ -74,114 +60,96 @@ static void fun_big_comp(unsigned* arg) *arg = fibonacci(*arg % FIBO_MAX); } -static void bench_parmap_full(int nthreads, e_xbt_parmap_mode_t mode) +template +void bench_parmap(int nthreads, double timeout, e_xbt_parmap_mode_t mode, bool full_bench, F func_to_apply) { - std::cout << "** mode = " << std::left << std::setw(15) << parmap_mode_name(mode) << " "; - std::cout.flush(); + std::string mode_name = parmap_mode_name(mode); + XBT_INFO("** mode = %s", mode_name.c_str()); - if (parmap_skip_mode(mode)) - return; - - std::vector a(ARRAY_SIZE); - std::vector data(ARRAY_SIZE); - std::iota(begin(a), end(a), 0); - std::iota(begin(data), end(data), &a[0]); - - int i = 0; - double start_time = xbt_os_time(); - double elapsed_time; - do { - { - simgrid::xbt::Parmap parmap(nthreads, mode); - parmap.apply(fun_to_apply, data); - } // enclosing block to ensure that the parmap is destroyed here. - elapsed_time = xbt_os_time() - start_time; - i++; - } while (elapsed_time < TIMEOUT); - - std::cout << "ran " << i << " times in " << elapsed_time << " seconds (" << (i / elapsed_time) << "/s)\n"; -} - -static void bench_parmap_apply(int nthreads, e_xbt_parmap_mode_t mode) -{ - std::cout << "** mode = " << std::left << std::setw(15) << parmap_mode_name(mode) << " "; - std::cout.flush(); - - if (parmap_skip_mode(mode)) + if (mode == XBT_PARMAP_FUTEX && not HAVE_FUTEX_H) { + XBT_INFO(" not available"); return; + } std::vector a(ARRAY_SIZE); std::vector data(ARRAY_SIZE); std::iota(begin(a), end(a), 0); std::iota(begin(data), end(data), &a[0]); - simgrid::xbt::Parmap parmap(nthreads, mode); + auto* parmap = new simgrid::xbt::Parmap(nthreads, mode); int i = 0; double start_time = xbt_os_time(); double elapsed_time; do { - parmap.apply(fun_to_apply, data); + if (full_bench) { + delete parmap; + parmap = new simgrid::xbt::Parmap(nthreads, mode); + } + parmap->apply(func_to_apply, data); elapsed_time = xbt_os_time() - start_time; i++; - } while (elapsed_time < TIMEOUT); + } while (elapsed_time < timeout); + delete parmap; - std::cout << "ran " << i << " times in " << elapsed_time << " seconds (" << (i / elapsed_time) << "/s)\n"; + XBT_INFO(" ran %d times in %g seconds (%g/s)", i, elapsed_time, i / elapsed_time); } -static void bench_all_modes(void (*bench_fun)(int, e_xbt_parmap_mode_t), int nthreads, unsigned modes) +template void bench_all_modes(int nthreads, double timeout, unsigned modes, bool full_bench, F func_to_apply) { std::vector all_modes = {XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX, XBT_PARMAP_BUSY_WAIT, XBT_PARMAP_DEFAULT}; for (unsigned i = 0; i < all_modes.size(); i++) { if (1U << i & modes) - bench_fun(nthreads, all_modes[i]); + bench_parmap(nthreads, timeout, all_modes[i], full_bench, func_to_apply); } } int main(int argc, char* argv[]) { int nthreads; + double timeout; unsigned modes = MODES_DEFAULT; - MSG_init(&argc, argv); + xbt_log_control_set("parmap_bench.fmt:[%c/%p]%e%m%n"); + simgrid::s4u::Engine e(&argc, argv); - if (argc != 2 && argc != 3) { - std::cerr << "Usage: " << argv[0] << " nthreads [modes]\n" - << " nthreads - number of working threads\n" - << " modes - bitmask of modes to test\n"; + if (argc != 3 && argc != 4) { + XBT_INFO("Usage: %s nthreads timeout [modes]", argv[0]); + XBT_INFO(" nthreads - number of working threads"); + XBT_INFO(" timeout - max duration for each test"); + XBT_INFO(" modes - bitmask of modes to test"); return EXIT_FAILURE; } nthreads = atoi(argv[1]); if (nthreads < 1) { - std::cerr << "ERROR: invalid thread count: " << nthreads << "\n"; + XBT_ERROR("Invalid thread count: %d", nthreads); return EXIT_FAILURE; } - if (argc == 3) + timeout = atof(argv[2]); + if (argc == 4) modes = strtol(argv[2], NULL, 0); - std::cout << "Parmap benchmark with " << nthreads << " workers (modes = " << std::hex << modes << std::dec - << ")...\n\n"; - - fun_to_apply = &fun_small_comp; + XBT_INFO("Parmap benchmark with %d workers (modes = %#x)...", nthreads, modes); + XBT_INFO("%s", ""); - std::cout << "Benchmark for parmap create+apply+destroy (small comp):\n"; - bench_all_modes(bench_parmap_full, nthreads, modes); - std::cout << std::endl; + SIMIX_context_set_nthreads(nthreads); - std::cout << "Benchmark for parmap apply only (small comp):\n"; - bench_all_modes(bench_parmap_apply, nthreads, modes); - std::cout << std::endl; + XBT_INFO("Benchmark for parmap create+apply+destroy (small comp):"); + bench_all_modes(nthreads, timeout, modes, true, &fun_small_comp); + XBT_INFO("%s", ""); - fun_to_apply = &fun_big_comp; + XBT_INFO("Benchmark for parmap apply only (small comp):"); + bench_all_modes(nthreads, timeout, modes, false, &fun_small_comp); + XBT_INFO("%s", ""); - std::cout << "Benchmark for parmap create+apply+destroy (big comp):\n"; - bench_all_modes(bench_parmap_full, nthreads, modes); - std::cout << std::endl; + XBT_INFO("Benchmark for parmap create+apply+destroy (big comp):"); + bench_all_modes(nthreads, timeout, modes, true, &fun_big_comp); + XBT_INFO("%s", ""); - std::cout << "Benchmark for parmap apply only (big comp):\n"; - bench_all_modes(bench_parmap_apply, nthreads, modes); - std::cout << std::endl; + XBT_INFO("Benchmark for parmap apply only (big comp):"); + bench_all_modes(nthreads, timeout, modes, false, &fun_big_comp); + XBT_INFO("%s", ""); return EXIT_SUCCESS; }