4 #include <xbt/parmap.h>
5 #include <xbt/sysdep.h>
6 #include <gras_config.h> /* HAVE_FUTEX_H */
7 #include "xbt/xbt_os_time.h"
9 #define MODES_DEFAULT 0x7
11 #define ARRAY_SIZE 10007
14 void (*fun_to_apply)(void *);
16 static const char *parmap_mode_name(e_xbt_parmap_mode_t mode)
20 case XBT_PARMAP_POSIX:
21 snprintf(name, sizeof name, "POSIX");
23 case XBT_PARMAP_FUTEX:
24 snprintf(name, sizeof name, "FUTEX");
26 case XBT_PARMAP_BUSY_WAIT:
27 snprintf(name, sizeof name, "BUSY_WAIT");
29 case XBT_PARMAP_DEFAULT:
30 snprintf(name, sizeof name, "DEFAULT");
33 snprintf(name, sizeof name, "UNKNOWN(%d)", mode);
39 static int parmap_skip_mode(e_xbt_parmap_mode_t mode)
43 case XBT_PARMAP_FUTEX:
44 printf("not available\n");
52 static unsigned fibonacci(unsigned n)
57 return fibonacci(n - 1) + fibonacci(n - 2);
60 static void fun_small_comp(void *arg)
66 static void fun_big_comp(void *arg)
69 *u = fibonacci(*u % FIBO_MAX);
72 static void array_new(unsigned **a, xbt_dynar_t *data)
75 *a = xbt_malloc(ARRAY_SIZE * sizeof **a);
76 *data = xbt_dynar_new(sizeof *a, NULL);
77 xbt_dynar_shrink(*data, ARRAY_SIZE);
78 for (i = 0 ; i < ARRAY_SIZE ; i++) {
80 xbt_dynar_push_as(*data, void*, &(*a)[i]);
84 static void bench_parmap_full(int nthreads, e_xbt_parmap_mode_t mode)
90 double start_time, elapsed_time;
92 printf("** mode = %-15s ", parmap_mode_name(mode));
95 if (parmap_skip_mode(mode))
101 start_time = xbt_os_time();
103 parmap = xbt_parmap_new(nthreads, mode);
104 xbt_parmap_apply(parmap, fun_to_apply, data);
105 xbt_parmap_destroy(parmap);
106 elapsed_time = xbt_os_time() - start_time;
108 } while (elapsed_time < TIMEOUT);
110 printf("ran %d times in %g seconds (%g/s)\n",
111 i, elapsed_time, i / elapsed_time);
113 xbt_dynar_free(&data);
117 static void bench_parmap_apply(int nthreads, e_xbt_parmap_mode_t mode)
123 double start_time, elapsed_time;
125 printf("** mode = %-15s ", parmap_mode_name(mode));
128 if (parmap_skip_mode(mode))
131 array_new(&a, &data);
133 parmap = xbt_parmap_new(nthreads, mode);
135 start_time = xbt_os_time();
137 xbt_parmap_apply(parmap, fun_to_apply, data);
138 elapsed_time = xbt_os_time() - start_time;
140 } while (elapsed_time < TIMEOUT);
141 xbt_parmap_destroy(parmap);
143 printf("ran %d times in %g seconds (%g/s)\n",
144 i, elapsed_time, i / elapsed_time);
146 xbt_dynar_free(&data);
150 static void bench_all_modes(void (*bench_fun)(int, e_xbt_parmap_mode_t),
151 int nthreads, unsigned modes)
153 e_xbt_parmap_mode_t all_modes[] = {
154 XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX,
155 XBT_PARMAP_BUSY_WAIT, XBT_PARMAP_DEFAULT
158 for (i = 0 ; i < sizeof all_modes / sizeof all_modes[0] ; i++) {
160 bench_fun(nthreads, all_modes[i]);
164 int main(int argc, char *argv[])
167 unsigned modes = MODES_DEFAULT;
169 if (argc != 2 && argc != 3) {
171 "Usage: %s nthreads [modes]\n"
172 " nthreads - number of working threads\n"
173 " modes - bitmask of modes to test\n",
177 nthreads = atoi(argv[1]);
179 fprintf(stderr, "ERROR: invalid thread count: %d\n", nthreads);
183 modes = atoi(argv[2]);
185 printf("Parmap benchmark with %d workers (modes = %#x)...\n\n",
188 fun_to_apply = fun_small_comp;
190 printf("Benchmark for parmap create+apply+destroy (small comp):\n");
191 bench_all_modes(bench_parmap_full, nthreads, modes);
194 printf("Benchmark for parmap apply only (small comp):\n");
195 bench_all_modes(bench_parmap_apply, nthreads, modes);
198 fun_to_apply = fun_big_comp;
200 printf("Benchmark for parmap create+apply+destroy (big comp):\n");
201 bench_all_modes(bench_parmap_full, nthreads, modes);
204 printf("Benchmark for parmap apply only (big comp):\n");
205 bench_all_modes(bench_parmap_apply, nthreads, modes);