Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
16505df21e68ab914d14a0c1d9d90369993493fd
[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 #if !HAVE_FUTEX_H
49   if (mode == XBT_PARMAP_FUTEX) {
50     printf("not available\n");
51     return 1;
52   } else
53 #endif
54     return 0;
55 }
56
57 static unsigned fibonacci(unsigned n)
58 {
59   if (n < 2)
60     return n;
61   else
62     return fibonacci(n - 1) + fibonacci(n - 2);
63 }
64
65 static void fun_small_comp(void *arg)
66 {
67   unsigned *u = arg;
68   *u = 2 * *u + 1;
69 }
70
71 static void fun_big_comp(void *arg)
72 {
73   unsigned *u = arg;
74   *u = fibonacci(*u % FIBO_MAX);
75 }
76
77 static void array_new(unsigned **a, xbt_dynar_t *data)
78 {
79   *a = xbt_malloc(ARRAY_SIZE * sizeof **a);
80   *data = xbt_dynar_new(sizeof *a, NULL);
81   xbt_dynar_shrink(*data, ARRAY_SIZE);
82   for (int i = 0 ; i < ARRAY_SIZE ; i++) {
83     (*a)[i] = i;
84     xbt_dynar_push_as(*data, void*, &(*a)[i]);
85   }
86 }
87
88 static void bench_parmap_full(int nthreads, e_xbt_parmap_mode_t mode)
89 {
90   unsigned *a;
91   xbt_dynar_t data;
92   xbt_parmap_t parmap;
93   double elapsed_time;
94
95   printf("** mode = %-15s ", parmap_mode_name(mode));
96   fflush(stdout);
97
98   if (parmap_skip_mode(mode))
99     return;
100
101   array_new(&a, &data);
102
103   int i = 0;
104   double start_time = xbt_os_time();
105   do {
106     parmap = xbt_parmap_new(nthreads, mode);
107     xbt_parmap_apply(parmap, fun_to_apply, data);
108     xbt_parmap_destroy(parmap);
109     elapsed_time = xbt_os_time() - start_time;
110     i++;
111   } while (elapsed_time < TIMEOUT);
112
113   printf("ran %d times in %g seconds (%g/s)\n", i, elapsed_time, i / elapsed_time);
114
115   xbt_dynar_free(&data);
116   xbt_free(a);
117 }
118
119 static void bench_parmap_apply(int nthreads, e_xbt_parmap_mode_t mode)
120 {
121   unsigned *a;
122   xbt_dynar_t data;
123   double elapsed_time;
124
125   printf("** mode = %-15s ", parmap_mode_name(mode));
126   fflush(stdout);
127
128   if (parmap_skip_mode(mode))
129     return;
130
131   array_new(&a, &data);
132
133   xbt_parmap_t parmap = xbt_parmap_new(nthreads, mode);
134   int i = 0;
135   double start_time = xbt_os_time();
136   do {
137     xbt_parmap_apply(parmap, fun_to_apply, data);
138     elapsed_time = xbt_os_time() - start_time;
139     i++;
140   } while (elapsed_time < TIMEOUT);
141   xbt_parmap_destroy(parmap);
142
143   printf("ran %d times in %g seconds (%g/s)\n", i, elapsed_time, i / elapsed_time);
144
145   xbt_dynar_free(&data);
146   xbt_free(a);
147 }
148
149 static void bench_all_modes(void (*bench_fun)(int, e_xbt_parmap_mode_t),
150                             int nthreads, unsigned modes)
151 {
152   e_xbt_parmap_mode_t all_modes[] = {XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX, XBT_PARMAP_BUSY_WAIT, XBT_PARMAP_DEFAULT};
153
154   for (unsigned i = 0 ; i < sizeof all_modes / sizeof all_modes[0] ; i++) {
155     if (1U << i & modes)
156       bench_fun(nthreads, all_modes[i]);
157   }
158 }
159
160 int main(int argc, char *argv[])
161 {
162   int nthreads;
163   unsigned modes = MODES_DEFAULT;
164
165   SIMIX_global_init(&argc, argv);
166
167   if (argc != 2 && argc != 3) {
168     fprintf(stderr, "Usage: %s nthreads [modes]\n"
169             "    nthreads - number of working threads\n"
170             "    modes    - bitmask of modes to test\n",
171             argv[0]);
172     return EXIT_FAILURE;
173   }
174   nthreads = atoi(argv[1]);
175   if (nthreads < 1) {
176     fprintf(stderr, "ERROR: invalid thread count: %d\n", nthreads);
177     return EXIT_FAILURE;
178   }
179   if (argc == 3)
180     modes = strtol(argv[2], NULL, 0);
181
182   printf("Parmap benchmark with %d workers (modes = %#x)...\n\n", nthreads, modes);
183
184   fun_to_apply = &fun_small_comp;
185
186   printf("Benchmark for parmap create+apply+destroy (small comp):\n");
187   bench_all_modes(bench_parmap_full, nthreads, modes);
188   printf("\n");
189
190   printf("Benchmark for parmap apply only (small comp):\n");
191   bench_all_modes(bench_parmap_apply, nthreads, modes);
192   printf("\n");
193
194   fun_to_apply = &fun_big_comp;
195
196   printf("Benchmark for parmap create+apply+destroy (big comp):\n");
197   bench_all_modes(bench_parmap_full, nthreads, modes);
198   printf("\n");
199
200   printf("Benchmark for parmap apply only (big comp):\n");
201   bench_all_modes(bench_parmap_apply, nthreads, modes);
202   printf("\n");
203
204   return EXIT_SUCCESS;
205 }