Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a3f1b0fd42b52915d3d5bf31754c4d622e63cd5a
[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 #if !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   *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++) {
85     (*a)[i] = i;
86     xbt_dynar_push_as(*data, void*, &(*a)[i]);
87   }
88 }
89
90 static void bench_parmap_full(int nthreads, e_xbt_parmap_mode_t mode)
91 {
92   unsigned *a;
93   xbt_dynar_t data;
94   xbt_parmap_t parmap;
95   double elapsed_time;
96
97   printf("** mode = %-15s ", parmap_mode_name(mode));
98   fflush(stdout);
99
100   if (parmap_skip_mode(mode))
101     return;
102
103   array_new(&a, &data);
104
105   int i = 0;
106   double start_time = xbt_os_time();
107   do {
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;
112     i++;
113   } while (elapsed_time < TIMEOUT);
114
115   printf("ran %d times in %g seconds (%g/s)\n", i, elapsed_time, i / elapsed_time);
116
117   xbt_dynar_free(&data);
118   xbt_free(a);
119 }
120
121 static void bench_parmap_apply(int nthreads, e_xbt_parmap_mode_t mode)
122 {
123   unsigned *a;
124   xbt_dynar_t data;
125   double elapsed_time;
126
127   printf("** mode = %-15s ", parmap_mode_name(mode));
128   fflush(stdout);
129
130   if (parmap_skip_mode(mode))
131     return;
132
133   array_new(&a, &data);
134
135   xbt_parmap_t parmap = xbt_parmap_new(nthreads, mode);
136   int i = 0;
137   double start_time = xbt_os_time();
138   do {
139     xbt_parmap_apply(parmap, fun_to_apply, data);
140     elapsed_time = xbt_os_time() - start_time;
141     i++;
142   } while (elapsed_time < TIMEOUT);
143   xbt_parmap_destroy(parmap);
144
145   printf("ran %d times in %g seconds (%g/s)\n", i, elapsed_time, i / elapsed_time);
146
147   xbt_dynar_free(&data);
148   xbt_free(a);
149 }
150
151 static void bench_all_modes(void (*bench_fun)(int, e_xbt_parmap_mode_t),
152                             int nthreads, unsigned modes)
153 {
154   e_xbt_parmap_mode_t all_modes[] = {XBT_PARMAP_POSIX, XBT_PARMAP_FUTEX, XBT_PARMAP_BUSY_WAIT, XBT_PARMAP_DEFAULT};
155
156   for (unsigned i = 0 ; i < sizeof all_modes / sizeof all_modes[0] ; i++) {
157     if (1U << i & modes)
158       bench_fun(nthreads, all_modes[i]);
159   }
160 }
161
162 int main(int argc, char *argv[])
163 {
164   int nthreads;
165   unsigned modes = MODES_DEFAULT;
166
167   SIMIX_global_init(&argc, argv);
168
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",
173             argv[0]);
174     return EXIT_FAILURE;
175   }
176   nthreads = atoi(argv[1]);
177   if (nthreads < 1) {
178     fprintf(stderr, "ERROR: invalid thread count: %d\n", nthreads);
179     return EXIT_FAILURE;
180   }
181   if (argc == 3)
182     modes = strtol(argv[2], NULL, 0);
183
184   printf("Parmap benchmark with %d workers (modes = %#x)...\n\n", nthreads, modes);
185
186   fun_to_apply = &fun_small_comp;
187
188   printf("Benchmark for parmap create+apply+destroy (small comp):\n");
189   bench_all_modes(bench_parmap_full, nthreads, modes);
190   printf("\n");
191
192   printf("Benchmark for parmap apply only (small comp):\n");
193   bench_all_modes(bench_parmap_apply, nthreads, modes);
194   printf("\n");
195
196   fun_to_apply = &fun_big_comp;
197
198   printf("Benchmark for parmap create+apply+destroy (big comp):\n");
199   bench_all_modes(bench_parmap_full, nthreads, modes);
200   printf("\n");
201
202   printf("Benchmark for parmap apply only (big comp):\n");
203   bench_all_modes(bench_parmap_apply, nthreads, modes);
204   printf("\n");
205
206   return EXIT_SUCCESS;
207 }