1 /* Copyright (c) 2012-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include <xbt/parmap.h>
11 #include <xbt/sysdep.h>
12 #include "src/internal_config.h" /* HAVE_FUTEX_H */
13 #include "simgrid/simix.h"
14 #include "xbt/xbt_os_time.h"
16 #define MODES_DEFAULT 0x7
18 #define ARRAY_SIZE 10007
21 void (*fun_to_apply)(void *);
23 static const char *parmap_mode_name(e_xbt_parmap_mode_t mode)
27 case XBT_PARMAP_POSIX:
28 snprintf(name, sizeof name, "POSIX");
30 case XBT_PARMAP_FUTEX:
31 snprintf(name, sizeof name, "FUTEX");
33 case XBT_PARMAP_BUSY_WAIT:
34 snprintf(name, sizeof name, "BUSY_WAIT");
36 case XBT_PARMAP_DEFAULT:
37 snprintf(name, sizeof name, "DEFAULT");
40 snprintf(name, sizeof name, "UNKNOWN(%d)", (int)mode);
46 static int parmap_skip_mode(e_xbt_parmap_mode_t mode)
50 case XBT_PARMAP_FUTEX:
51 printf("not available\n");
59 static unsigned fibonacci(unsigned n)
64 return fibonacci(n - 1) + fibonacci(n - 2);
67 static void fun_small_comp(void *arg)
73 static void fun_big_comp(void *arg)
76 *u = fibonacci(*u % FIBO_MAX);
79 static void array_new(unsigned **a, xbt_dynar_t *data)
81 *a = xbt_malloc(ARRAY_SIZE * sizeof **a);
82 *data = xbt_dynar_new(sizeof *a, NULL);
83 xbt_dynar_shrink(*data, ARRAY_SIZE);
84 for (int i = 0 ; i < ARRAY_SIZE ; i++) {
86 xbt_dynar_push_as(*data, void*, &(*a)[i]);
90 static void bench_parmap_full(int nthreads, e_xbt_parmap_mode_t mode)
97 printf("** mode = %-15s ", parmap_mode_name(mode));
100 if (parmap_skip_mode(mode))
103 array_new(&a, &data);
106 double start_time = xbt_os_time();
108 parmap = xbt_parmap_new(nthreads, mode);
109 xbt_parmap_apply(parmap, fun_to_apply, data);
110 xbt_parmap_destroy(parmap);
111 elapsed_time = xbt_os_time() - start_time;
113 } while (elapsed_time < TIMEOUT);
115 printf("ran %d times in %g seconds (%g/s)\n", i, elapsed_time, i / elapsed_time);
117 xbt_dynar_free(&data);
121 static void bench_parmap_apply(int nthreads, e_xbt_parmap_mode_t mode)
127 printf("** mode = %-15s ", parmap_mode_name(mode));
130 if (parmap_skip_mode(mode))
133 array_new(&a, &data);
135 xbt_parmap_t parmap = xbt_parmap_new(nthreads, mode);
137 double start_time = xbt_os_time();
139 xbt_parmap_apply(parmap, fun_to_apply, data);
140 elapsed_time = xbt_os_time() - start_time;
142 } while (elapsed_time < TIMEOUT);
143 xbt_parmap_destroy(parmap);
145 printf("ran %d times in %g seconds (%g/s)\n", 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[] = {XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX, XBT_PARMAP_BUSY_WAIT, XBT_PARMAP_DEFAULT};
156 for (unsigned i = 0 ; i < sizeof all_modes / sizeof all_modes[0] ; i++) {
158 bench_fun(nthreads, all_modes[i]);
162 int main(int argc, char *argv[])
165 unsigned modes = MODES_DEFAULT;
167 SIMIX_global_init(&argc, argv);
169 if (argc != 2 && argc != 3) {
170 fprintf(stderr, "Usage: %s nthreads [modes]\n"
171 " nthreads - number of working threads\n"
172 " modes - bitmask of modes to test\n",
176 nthreads = atoi(argv[1]);
178 fprintf(stderr, "ERROR: invalid thread count: %d\n", nthreads);
182 modes = strtol(argv[2], NULL, 0);
184 printf("Parmap benchmark with %d workers (modes = %#x)...\n\n", nthreads, modes);
186 fun_to_apply = &fun_small_comp;
188 printf("Benchmark for parmap create+apply+destroy (small comp):\n");
189 bench_all_modes(bench_parmap_full, nthreads, modes);
192 printf("Benchmark for parmap apply only (small comp):\n");
193 bench_all_modes(bench_parmap_apply, nthreads, modes);
196 fun_to_apply = &fun_big_comp;
198 printf("Benchmark for parmap create+apply+destroy (big comp):\n");
199 bench_all_modes(bench_parmap_full, nthreads, modes);
202 printf("Benchmark for parmap apply only (big comp):\n");
203 bench_all_modes(bench_parmap_apply, nthreads, modes);