Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / teshsuite / xbt / parmap_bench / parmap_bench.c
1 /* Copyright (c) 2012-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <xbt/dynar.h>
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"
15
16 #define MODES_DEFAULT 0x7
17 #define TIMEOUT 10.0
18 #define ARRAY_SIZE 10007
19 #define FIBO_MAX 25
20
21 void (*fun_to_apply)(void *);
22
23 static const char *parmap_mode_name(e_xbt_parmap_mode_t mode)
24 {
25   static char name[80];
26   switch (mode) {
27   case XBT_PARMAP_POSIX:
28     snprintf(name, sizeof name, "POSIX");
29     break;
30   case XBT_PARMAP_FUTEX:
31     snprintf(name, sizeof name, "FUTEX");
32     break;
33   case XBT_PARMAP_BUSY_WAIT:
34     snprintf(name, sizeof name, "BUSY_WAIT");
35     break;
36   case XBT_PARMAP_DEFAULT:
37     snprintf(name, sizeof name, "DEFAULT");
38     break;
39   default:
40     snprintf(name, sizeof name, "UNKNOWN(%d)", (int)mode);
41     break;
42   }
43   return name;
44 }
45
46 static int parmap_skip_mode(e_xbt_parmap_mode_t mode)
47 {
48   switch (mode) {
49 #ifndef HAVE_FUTEX_H
50   case XBT_PARMAP_FUTEX:
51     printf("not available\n");
52     return 1;
53 #endif
54   default:
55     return 0;
56   }
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(void *arg)
68 {
69   unsigned *u = arg;
70   *u = 2 * *u + 1;
71 }
72
73 static void fun_big_comp(void *arg)
74 {
75   unsigned *u = arg;
76   *u = fibonacci(*u % FIBO_MAX);
77 }
78
79 static void array_new(unsigned **a, xbt_dynar_t *data)
80 {
81   int i;
82   *a = xbt_malloc(ARRAY_SIZE * sizeof **a);
83   *data = xbt_dynar_new(sizeof *a, NULL);
84   xbt_dynar_shrink(*data, ARRAY_SIZE);
85   for (i = 0 ; i < ARRAY_SIZE ; i++) {
86     (*a)[i] = i;
87     xbt_dynar_push_as(*data, void*, &(*a)[i]);
88   }
89 }
90
91 static void bench_parmap_full(int nthreads, e_xbt_parmap_mode_t mode)
92 {
93   unsigned *a;
94   xbt_dynar_t data;
95   xbt_parmap_t parmap;
96   int i;
97   double start_time, elapsed_time;
98
99   printf("** mode = %-15s ", parmap_mode_name(mode));
100   fflush(stdout);
101
102   if (parmap_skip_mode(mode))
103     return;
104
105   array_new(&a, &data);
106
107   i = 0;
108   start_time = xbt_os_time();
109   do {
110     parmap = xbt_parmap_new(nthreads, mode);
111     xbt_parmap_apply(parmap, fun_to_apply, data);
112     xbt_parmap_destroy(parmap);
113     elapsed_time = xbt_os_time() - start_time;
114     i++;
115   } while (elapsed_time < TIMEOUT);
116
117   printf("ran %d times in %g seconds (%g/s)\n", i, elapsed_time, i / elapsed_time);
118
119   xbt_dynar_free(&data);
120   xbt_free(a);
121 }
122
123 static void bench_parmap_apply(int nthreads, e_xbt_parmap_mode_t mode)
124 {
125   unsigned *a;
126   xbt_dynar_t data;
127   double start_time, elapsed_time;
128
129   printf("** mode = %-15s ", parmap_mode_name(mode));
130   fflush(stdout);
131
132   if (parmap_skip_mode(mode))
133     return;
134
135   array_new(&a, &data);
136
137   xbt_parmap_t parmap = xbt_parmap_new(nthreads, mode);
138   int i = 0;
139   start_time = xbt_os_time();
140   do {
141     xbt_parmap_apply(parmap, fun_to_apply, data);
142     elapsed_time = xbt_os_time() - start_time;
143     i++;
144   } while (elapsed_time < TIMEOUT);
145   xbt_parmap_destroy(parmap);
146
147   printf("ran %d times in %g seconds (%g/s)\n",
148          i, elapsed_time, i / elapsed_time);
149
150   xbt_dynar_free(&data);
151   xbt_free(a);
152 }
153
154 static void bench_all_modes(void (*bench_fun)(int, e_xbt_parmap_mode_t),
155                             int nthreads, unsigned modes)
156 {
157   e_xbt_parmap_mode_t all_modes[] = {XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX, XBT_PARMAP_BUSY_WAIT, XBT_PARMAP_DEFAULT};
158
159   for (unsigned i = 0 ; i < sizeof all_modes / sizeof all_modes[0] ; i++) {
160     if (1U << i & modes)
161       bench_fun(nthreads, all_modes[i]);
162   }
163 }
164
165 int main(int argc, char *argv[])
166 {
167   int nthreads;
168   unsigned modes = MODES_DEFAULT;
169
170   SIMIX_global_init(&argc, argv);
171
172   if (argc != 2 && argc != 3) {
173     fprintf(stderr, "Usage: %s nthreads [modes]\n"
174             "    nthreads - number of working threads\n"
175             "    modes    - bitmask of modes to test\n",
176             argv[0]);
177     return EXIT_FAILURE;
178   }
179   nthreads = atoi(argv[1]);
180   if (nthreads < 1) {
181     fprintf(stderr, "ERROR: invalid thread count: %d\n", nthreads);
182     return EXIT_FAILURE;
183   }
184   if (argc == 3)
185     modes = strtol(argv[2], NULL, 0);
186
187   printf("Parmap benchmark with %d workers (modes = %#x)...\n\n", nthreads, modes);
188
189   fun_to_apply = fun_small_comp;
190
191   printf("Benchmark for parmap create+apply+destroy (small comp):\n");
192   bench_all_modes(bench_parmap_full, nthreads, modes);
193   printf("\n");
194
195   printf("Benchmark for parmap apply only (small comp):\n");
196   bench_all_modes(bench_parmap_apply, nthreads, modes);
197   printf("\n");
198
199   fun_to_apply = fun_big_comp;
200
201   printf("Benchmark for parmap create+apply+destroy (big comp):\n");
202   bench_all_modes(bench_parmap_full, nthreads, modes);
203   printf("\n");
204
205   printf("Benchmark for parmap apply only (big comp):\n");
206   bench_all_modes(bench_parmap_apply, nthreads, modes);
207   printf("\n");
208
209   return EXIT_SUCCESS;
210 }