4 #include <xbt/parmap.h>
5 #include <xbt/sysdep.h>
6 #include <internal_config.h> /* HAVE_FUTEX_H */
7 #include "simgrid/simix.h"
8 #include "xbt/xbt_os_time.h"
10 #define MODES_DEFAULT 0x7
12 #define ARRAY_SIZE 10007
15 void (*fun_to_apply)(void *);
17 static const char *parmap_mode_name(e_xbt_parmap_mode_t mode)
21 case XBT_PARMAP_POSIX:
22 snprintf(name, sizeof name, "POSIX");
24 case XBT_PARMAP_FUTEX:
25 snprintf(name, sizeof name, "FUTEX");
27 case XBT_PARMAP_BUSY_WAIT:
28 snprintf(name, sizeof name, "BUSY_WAIT");
30 case XBT_PARMAP_DEFAULT:
31 snprintf(name, sizeof name, "DEFAULT");
34 snprintf(name, sizeof name, "UNKNOWN(%d)", (int)mode);
40 static int parmap_skip_mode(e_xbt_parmap_mode_t mode)
44 case XBT_PARMAP_FUTEX:
45 printf("not available\n");
53 static unsigned fibonacci(unsigned n)
58 return fibonacci(n - 1) + fibonacci(n - 2);
61 static void fun_small_comp(void *arg)
67 static void fun_big_comp(void *arg)
70 *u = fibonacci(*u % FIBO_MAX);
73 static void array_new(unsigned **a, xbt_dynar_t *data)
76 *a = xbt_malloc(ARRAY_SIZE * sizeof **a);
77 *data = xbt_dynar_new(sizeof *a, NULL);
78 xbt_dynar_shrink(*data, ARRAY_SIZE);
79 for (i = 0 ; i < ARRAY_SIZE ; i++) {
81 xbt_dynar_push_as(*data, void*, &(*a)[i]);
85 static void bench_parmap_full(int nthreads, e_xbt_parmap_mode_t mode)
91 double start_time, elapsed_time;
93 printf("** mode = %-15s ", parmap_mode_name(mode));
96 if (parmap_skip_mode(mode))
102 start_time = xbt_os_time();
104 parmap = xbt_parmap_new(nthreads, mode);
105 xbt_parmap_apply(parmap, fun_to_apply, data);
106 xbt_parmap_destroy(parmap);
107 elapsed_time = xbt_os_time() - start_time;
109 } while (elapsed_time < TIMEOUT);
111 printf("ran %d times in %g seconds (%g/s)\n",
112 i, elapsed_time, i / elapsed_time);
114 xbt_dynar_free(&data);
118 static void bench_parmap_apply(int nthreads, e_xbt_parmap_mode_t mode)
124 double start_time, elapsed_time;
126 printf("** mode = %-15s ", parmap_mode_name(mode));
129 if (parmap_skip_mode(mode))
132 array_new(&a, &data);
134 parmap = xbt_parmap_new(nthreads, mode);
136 start_time = xbt_os_time();
138 xbt_parmap_apply(parmap, fun_to_apply, data);
139 elapsed_time = xbt_os_time() - start_time;
141 } while (elapsed_time < TIMEOUT);
142 xbt_parmap_destroy(parmap);
144 printf("ran %d times in %g seconds (%g/s)\n",
145 i, elapsed_time, i / elapsed_time);
147 xbt_dynar_free(&data);
151 static void bench_all_modes(void (*bench_fun)(int, e_xbt_parmap_mode_t),
152 int nthreads, unsigned modes)
154 e_xbt_parmap_mode_t all_modes[] = {
155 XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX,
156 XBT_PARMAP_BUSY_WAIT, XBT_PARMAP_DEFAULT
159 for (i = 0 ; i < sizeof all_modes / sizeof all_modes[0] ; i++) {
161 bench_fun(nthreads, all_modes[i]);
165 int main(int argc, char *argv[])
168 unsigned modes = MODES_DEFAULT;
170 SIMIX_global_init(&argc, argv);
172 if (argc != 2 && argc != 3) {
174 "Usage: %s nthreads [modes]\n"
175 " nthreads - number of working threads\n"
176 " modes - bitmask of modes to test\n",
180 nthreads = atoi(argv[1]);
182 fprintf(stderr, "ERROR: invalid thread count: %d\n", nthreads);
186 modes = strtol(argv[2], NULL, 0);
188 printf("Parmap benchmark with %d workers (modes = %#x)...\n\n",
191 fun_to_apply = fun_small_comp;
193 printf("Benchmark for parmap create+apply+destroy (small comp):\n");
194 bench_all_modes(bench_parmap_full, nthreads, modes);
197 printf("Benchmark for parmap apply only (small comp):\n");
198 bench_all_modes(bench_parmap_apply, nthreads, modes);
201 fun_to_apply = fun_big_comp;
203 printf("Benchmark for parmap create+apply+destroy (big comp):\n");
204 bench_all_modes(bench_parmap_full, nthreads, modes);
207 printf("Benchmark for parmap apply only (big comp):\n");
208 bench_all_modes(bench_parmap_apply, nthreads, modes);