Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics: rename a function, deprecate old name
[simgrid.git] / teshsuite / xbt / parmap_bench / parmap_bench.cpp
1 /* Copyright (c) 2012-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/internal_config.h" // HAVE_FUTEX_H
7 #include <simgrid/msg.h>
8 #include <xbt.h>
9 #include <xbt/parmap.hpp>
10
11 #include <cstdlib>
12 #include <numeric> // std::iota
13 #include <string>
14 #include <vector>
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_bench, "Bench for parmap");
17
18 #define MODES_DEFAULT 0x7
19 #define TIMEOUT 10.0
20 #define ARRAY_SIZE 10007
21 #define FIBO_MAX 25
22
23 void (*fun_to_apply)(unsigned*);
24
25 static std::string parmap_mode_name(e_xbt_parmap_mode_t mode)
26 {
27   std::string name;
28   switch (mode) {
29     case XBT_PARMAP_POSIX:
30       name = "POSIX";
31       break;
32     case XBT_PARMAP_FUTEX:
33       name = "FUTEX";
34       break;
35     case XBT_PARMAP_BUSY_WAIT:
36       name = "BUSY_WAIT";
37       break;
38     case XBT_PARMAP_DEFAULT:
39       name = "DEFAULT";
40       break;
41     default:
42       name = "UNKNOWN(" + std::to_string(mode) + ")";
43       break;
44   }
45   return name;
46 }
47
48 static unsigned fibonacci(unsigned n)
49 {
50   if (n < 2)
51     return n;
52   else
53     return fibonacci(n - 1) + fibonacci(n - 2);
54 }
55
56 static void fun_small_comp(unsigned* arg)
57 {
58   *arg = 2 * *arg + 1;
59 }
60
61 static void fun_big_comp(unsigned* arg)
62 {
63   *arg = fibonacci(*arg % FIBO_MAX);
64 }
65
66 static void bench_parmap(int nthreads, e_xbt_parmap_mode_t mode, bool full_bench)
67 {
68   XBT_INFO("** mode = %s", parmap_mode_name(mode).c_str());
69
70   if (mode == XBT_PARMAP_FUTEX && not HAVE_FUTEX_H) {
71     XBT_INFO("   not available");
72     return;
73   }
74
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]);
79
80   auto* parmap      = new simgrid::xbt::Parmap<unsigned*>(nthreads, mode);
81   int i             = 0;
82   double start_time = xbt_os_time();
83   double elapsed_time;
84   do {
85     if (full_bench) {
86       delete parmap;
87       parmap = new simgrid::xbt::Parmap<unsigned*>(nthreads, mode);
88     }
89     parmap->apply(fun_to_apply, data);
90     elapsed_time = xbt_os_time() - start_time;
91     i++;
92   } while (elapsed_time < TIMEOUT);
93   delete parmap;
94
95   XBT_INFO("   ran %d times in %g seconds (%g/s)", i, elapsed_time, i / elapsed_time);
96 }
97
98 static void bench_all_modes(int nthreads, unsigned modes, bool full_bench)
99 {
100   std::vector<e_xbt_parmap_mode_t> all_modes = {XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX, XBT_PARMAP_BUSY_WAIT,
101                                                 XBT_PARMAP_DEFAULT};
102
103   for (unsigned i = 0; i < all_modes.size(); i++) {
104     if (1U << i & modes)
105       bench_parmap(nthreads, all_modes[i], full_bench);
106   }
107 }
108
109 int main(int argc, char* argv[])
110 {
111   int nthreads;
112   unsigned modes = MODES_DEFAULT;
113
114   xbt_log_control_set("parmap_bench.fmt:[%c/%p]%e%m%n");
115   MSG_init(&argc, argv);
116
117   if (argc != 2 && argc != 3) {
118     XBT_INFO("Usage: %s nthreads [modes]", argv[0]);
119     XBT_INFO("    nthreads - number of working threads");
120     XBT_INFO("    modes    - bitmask of modes to test");
121     return EXIT_FAILURE;
122   }
123   nthreads = atoi(argv[1]);
124   if (nthreads < 1) {
125     XBT_ERROR("Invalid thread count: %d", nthreads);
126     return EXIT_FAILURE;
127   }
128   if (argc == 3)
129     modes = strtol(argv[2], NULL, 0);
130
131   XBT_INFO("Parmap benchmark with %d workers (modes = %#x)...", nthreads, modes);
132   XBT_INFO("%s", "");
133
134   SIMIX_context_set_nthreads(nthreads);
135   fun_to_apply = &fun_small_comp;
136
137   XBT_INFO("Benchmark for parmap create+apply+destroy (small comp):");
138   bench_all_modes(nthreads, modes, true);
139   XBT_INFO("%s", "");
140
141   XBT_INFO("Benchmark for parmap apply only (small comp):");
142   bench_all_modes(nthreads, modes, false);
143   XBT_INFO("%s", "");
144
145   fun_to_apply = &fun_big_comp;
146
147   XBT_INFO("Benchmark for parmap create+apply+destroy (big comp):");
148   bench_all_modes(nthreads, modes, true);
149   XBT_INFO("%s", "");
150
151   XBT_INFO("Benchmark for parmap apply only (big comp):");
152   bench_all_modes(nthreads, modes, false);
153   XBT_INFO("%s", "");
154
155   return EXIT_SUCCESS;
156 }