Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Enable SIMIX parallel mode for parmap_{bench,text}.
[simgrid.git] / teshsuite / xbt / parmap_bench / parmap_bench.cpp
index 9f74d1c..4fbead4 100644 (file)
@@ -9,12 +9,12 @@
 #include <xbt/parmap.hpp>
 
 #include <cstdlib>
-#include <iomanip>
-#include <iostream>
 #include <numeric> // std::iota
 #include <string>
 #include <vector>
 
+XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_bench, "Bench for parmap");
+
 #define MODES_DEFAULT 0x7
 #define TIMEOUT 10.0
 #define ARRAY_SIZE 10007
@@ -45,16 +45,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 (mode == XBT_PARMAP_FUTEX && not HAVE_FUTEX_H) {
-    std::cout << "not available\n";
-    return true;
-  } else {
-    return false;
-  }
-}
-
 static unsigned fibonacci(unsigned n)
 {
   if (n < 2)
@@ -73,68 +63,46 @@ 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)
+static void bench_parmap(int nthreads, e_xbt_parmap_mode_t mode, bool full_bench)
 {
-  std::cout << "** mode = " << std::left << std::setw(15) << parmap_mode_name(mode) << " ";
-  std::cout.flush();
-
-  if (parmap_skip_mode(mode))
-    return;
+  XBT_INFO("** mode = %s", parmap_mode_name(mode).c_str());
 
-  std::vector<unsigned> a(ARRAY_SIZE);
-  std::vector<unsigned*> 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<unsigned*> 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<unsigned> a(ARRAY_SIZE);
   std::vector<unsigned*> data(ARRAY_SIZE);
   std::iota(begin(a), end(a), 0);
   std::iota(begin(data), end(data), &a[0]);
 
-  simgrid::xbt::Parmap<unsigned*> parmap(nthreads, mode);
+  auto* parmap      = new simgrid::xbt::Parmap<unsigned*>(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<unsigned*>(nthreads, mode);
+    }
+    parmap->apply(fun_to_apply, data);
     elapsed_time = xbt_os_time() - start_time;
     i++;
   } 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)
+static void bench_all_modes(int nthreads, unsigned modes, bool full_bench)
 {
   std::vector<e_xbt_parmap_mode_t> 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, all_modes[i], full_bench);
   }
 }
 
@@ -143,44 +111,46 @@ int main(int argc, char* argv[])
   int nthreads;
   unsigned modes = MODES_DEFAULT;
 
+  xbt_log_control_set("parmap_bench.fmt:[%c/%p]%e%m%n");
   MSG_init(&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";
+    XBT_INFO("Usage: %s nthreads [modes]", argv[0]);
+    XBT_INFO("    nthreads - number of working threads");
+    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)
     modes = strtol(argv[2], NULL, 0);
 
-  std::cout << "Parmap benchmark with " << nthreads << " workers (modes = " << std::hex << modes << std::dec
-            << ")...\n\n";
+  XBT_INFO("Parmap benchmark with %d workers (modes = %#x)...", nthreads, modes);
+  XBT_INFO("%s", "");
 
+  SIMIX_context_set_nthreads(nthreads);
   fun_to_apply = &fun_small_comp;
 
-  std::cout << "Benchmark for parmap create+apply+destroy (small comp):\n";
-  bench_all_modes(bench_parmap_full, nthreads, modes);
-  std::cout << std::endl;
+  XBT_INFO("Benchmark for parmap create+apply+destroy (small comp):");
+  bench_all_modes(nthreads, modes, true);
+  XBT_INFO("%s", "");
 
-  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 apply only (small comp):");
+  bench_all_modes(nthreads, modes, false);
+  XBT_INFO("%s", "");
 
   fun_to_apply = &fun_big_comp;
 
-  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, modes, true);
+  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, modes, false);
+  XBT_INFO("%s", "");
 
   return EXIT_SUCCESS;
 }