Logo AND Algorithmique Numérique Distribuée

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