Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b5f61229b150903924317b5371b213927a6149b4
[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 bool parmap_skip_mode(e_xbt_parmap_mode_t mode)
49 {
50   return mode == XBT_PARMAP_FUTEX && not HAVE_FUTEX_H;
51 }
52
53 static unsigned fibonacci(unsigned n)
54 {
55   if (n < 2)
56     return n;
57   else
58     return fibonacci(n - 1) + fibonacci(n - 2);
59 }
60
61 static void fun_small_comp(unsigned* arg)
62 {
63   *arg = 2 * *arg + 1;
64 }
65
66 static void fun_big_comp(unsigned* arg)
67 {
68   *arg = fibonacci(*arg % FIBO_MAX);
69 }
70
71 static void bench_parmap_full(int nthreads, e_xbt_parmap_mode_t mode)
72 {
73   XBT_INFO("** mode = %s", parmap_mode_name(mode).c_str());
74
75   if (parmap_skip_mode(mode)) {
76     XBT_INFO("   not available");
77     return;
78   }
79
80   std::vector<unsigned> a(ARRAY_SIZE);
81   std::vector<unsigned*> data(ARRAY_SIZE);
82   std::iota(begin(a), end(a), 0);
83   std::iota(begin(data), end(data), &a[0]);
84
85   int i             = 0;
86   double start_time = xbt_os_time();
87   double elapsed_time;
88   do {
89     {
90       simgrid::xbt::Parmap<unsigned*> parmap(nthreads, mode);
91       parmap.apply(fun_to_apply, data);
92     } // enclosing block to ensure that the parmap is destroyed here.
93     elapsed_time = xbt_os_time() - start_time;
94     i++;
95   } while (elapsed_time < TIMEOUT);
96
97   XBT_INFO("   ran %d times in %g seconds (%g/s)", i, elapsed_time, i / elapsed_time);
98 }
99
100 static void bench_parmap_apply(int nthreads, e_xbt_parmap_mode_t mode)
101 {
102   XBT_INFO("** mode = %s", parmap_mode_name(mode).c_str());
103
104   if (parmap_skip_mode(mode)) {
105     XBT_INFO("   not available");
106     return;
107   }
108
109   std::vector<unsigned> a(ARRAY_SIZE);
110   std::vector<unsigned*> data(ARRAY_SIZE);
111   std::iota(begin(a), end(a), 0);
112   std::iota(begin(data), end(data), &a[0]);
113
114   simgrid::xbt::Parmap<unsigned*> parmap(nthreads, mode);
115   int i             = 0;
116   double start_time = xbt_os_time();
117   double elapsed_time;
118   do {
119     parmap.apply(fun_to_apply, data);
120     elapsed_time = xbt_os_time() - start_time;
121     i++;
122   } while (elapsed_time < TIMEOUT);
123
124   XBT_INFO("   ran %d times in %g seconds (%g/s)", i, elapsed_time, i / elapsed_time);
125 }
126
127 static void bench_all_modes(void (*bench_fun)(int, e_xbt_parmap_mode_t), int nthreads, unsigned modes)
128 {
129   std::vector<e_xbt_parmap_mode_t> all_modes = {XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX, XBT_PARMAP_BUSY_WAIT,
130                                                 XBT_PARMAP_DEFAULT};
131
132   for (unsigned i = 0; i < all_modes.size(); i++) {
133     if (1U << i & modes)
134       bench_fun(nthreads, all_modes[i]);
135   }
136 }
137
138 int main(int argc, char* argv[])
139 {
140   int nthreads;
141   unsigned modes = MODES_DEFAULT;
142
143   xbt_log_control_set("parmap_bench.fmt:[%c/%p]%e%m%n");
144   MSG_init(&argc, argv);
145
146   if (argc != 2 && argc != 3) {
147     XBT_INFO("Usage: %s nthreads [modes]", argv[0]);
148     XBT_INFO("    nthreads - number of working threads");
149     XBT_INFO("    modes    - bitmask of modes to test");
150     return EXIT_FAILURE;
151   }
152   nthreads = atoi(argv[1]);
153   if (nthreads < 1) {
154     XBT_ERROR("Invalid thread count: %d", nthreads);
155     return EXIT_FAILURE;
156   }
157   if (argc == 3)
158     modes = strtol(argv[2], NULL, 0);
159
160   XBT_INFO("Parmap benchmark with %d workers (modes = %#x)...", nthreads, modes);
161   XBT_INFO("%s", "");
162
163   fun_to_apply = &fun_small_comp;
164
165   XBT_INFO("Benchmark for parmap create+apply+destroy (small comp):");
166   bench_all_modes(bench_parmap_full, nthreads, modes);
167   XBT_INFO("%s", "");
168
169   XBT_INFO("Benchmark for parmap apply only (small comp):");
170   bench_all_modes(bench_parmap_apply, nthreads, modes);
171   XBT_INFO("%s", "");
172
173   fun_to_apply = &fun_big_comp;
174
175   XBT_INFO("Benchmark for parmap create+apply+destroy (big comp):");
176   bench_all_modes(bench_parmap_full, nthreads, modes);
177   XBT_INFO("%s", "");
178
179   XBT_INFO("Benchmark for parmap apply only (big comp):");
180   bench_all_modes(bench_parmap_apply, nthreads, modes);
181   XBT_INFO("%s", "");
182
183   return EXIT_SUCCESS;
184 }