Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f7d0fcce862ed11cbe4c286f700cc88fac171971
[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 <iomanip>
13 #include <iostream>
14 #include <numeric> // std::iota
15 #include <string>
16 #include <vector>
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 #if !HAVE_FUTEX_H
51   if (mode == XBT_PARMAP_FUTEX) {
52     std::cout << "not available\n";
53     return true;
54   } else
55 #endif
56     return false;
57 }
58
59 static unsigned fibonacci(unsigned n)
60 {
61   if (n < 2)
62     return n;
63   else
64     return fibonacci(n - 1) + fibonacci(n - 2);
65 }
66
67 static void fun_small_comp(unsigned* arg)
68 {
69   *arg = 2 * *arg + 1;
70 }
71
72 static void fun_big_comp(unsigned* arg)
73 {
74   *arg = fibonacci(*arg % FIBO_MAX);
75 }
76
77 static void bench_parmap_full(int nthreads, e_xbt_parmap_mode_t mode)
78 {
79   std::cout << "** mode = " << std::left << std::setw(15) << parmap_mode_name(mode) << " ";
80   std::cout.flush();
81
82   if (parmap_skip_mode(mode))
83     return;
84
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]);
89
90   int i             = 0;
91   double start_time = xbt_os_time();
92   double elapsed_time;
93   do {
94     {
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;
99     i++;
100   } while (elapsed_time < TIMEOUT);
101
102   std::cout << "ran " << i << " times in " << elapsed_time << " seconds (" << (i / elapsed_time) << "/s)\n";
103 }
104
105 static void bench_parmap_apply(int nthreads, e_xbt_parmap_mode_t mode)
106 {
107   std::cout << "** mode = " << std::left << std::setw(15) << parmap_mode_name(mode) << " ";
108   std::cout.flush();
109
110   if (parmap_skip_mode(mode))
111     return;
112
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]);
117
118   simgrid::xbt::Parmap<unsigned*> parmap(nthreads, mode);
119   int i             = 0;
120   double start_time = xbt_os_time();
121   double elapsed_time;
122   do {
123     parmap.apply(fun_to_apply, data);
124     elapsed_time = xbt_os_time() - start_time;
125     i++;
126   } while (elapsed_time < TIMEOUT);
127
128   std::cout << "ran " << i << " times in " << elapsed_time << " seconds (" << (i / elapsed_time) << "/s)\n";
129 }
130
131 static void bench_all_modes(void (*bench_fun)(int, e_xbt_parmap_mode_t), int nthreads, unsigned modes)
132 {
133   std::vector<e_xbt_parmap_mode_t> all_modes = {XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX, XBT_PARMAP_BUSY_WAIT,
134                                                 XBT_PARMAP_DEFAULT};
135
136   for (unsigned i = 0; i < all_modes.size(); i++) {
137     if (1U << i & modes)
138       bench_fun(nthreads, all_modes[i]);
139   }
140 }
141
142 int main(int argc, char* argv[])
143 {
144   int nthreads;
145   unsigned modes = MODES_DEFAULT;
146
147   MSG_init(&argc, argv);
148
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";
153     return EXIT_FAILURE;
154   }
155   nthreads = atoi(argv[1]);
156   if (nthreads < 1) {
157     std::cerr << "ERROR: invalid thread count: " << nthreads << "\n";
158     return EXIT_FAILURE;
159   }
160   if (argc == 3)
161     modes = strtol(argv[2], NULL, 0);
162
163   std::cout << "Parmap benchmark with " << nthreads << " workers (modes = " << std::hex << modes << std::dec
164             << ")...\n\n";
165
166   fun_to_apply = &fun_small_comp;
167
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;
171
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;
175
176   fun_to_apply = &fun_big_comp;
177
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;
181
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;
185
186   return EXIT_SUCCESS;
187 }