Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
document SMPI configuration items
[simgrid.git] / src / smpi / smpi_bench.c
1 /* Copyright (c) 2007, 2009, 2010. 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 <math.h> // sqrt
8 #include "private.h"
9 #include "xbt/dict.h"
10 #include "xbt/sysdep.h"
11 #include "xbt/ex.h"
12 #include "surf/surf.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
15                                 "Logging specific to SMPI (benchmarking)");
16
17 xbt_dict_t allocs = NULL;       /* Allocated on first use */
18 xbt_dict_t samples = NULL;      /* Allocated on first use */
19 xbt_dict_t calls = NULL;        /* Allocated on first use */
20
21 typedef struct {
22   int count;
23   char data[];
24 } shared_data_t;
25
26 typedef struct {
27   int count;
28   double sum;
29   double sum_pow2;
30   double mean;
31   double relstderr;
32   int iters;
33   double threshold;
34   int started;
35 } local_data_t;
36
37 void smpi_bench_destroy(void)
38 {
39   xbt_dict_free(&allocs);
40   xbt_dict_free(&samples);
41   xbt_dict_free(&calls);
42 }
43
44 static void smpi_execute_flops(double flops)
45 {
46   smx_action_t action;
47   smx_host_t host;
48   host = SIMIX_host_self();
49
50   XBT_DEBUG("Handle real computation time: %f flops", flops);
51   action = SIMIX_req_host_execute("computation", host, flops, 1);
52 #ifdef HAVE_TRACING
53   SIMIX_req_set_category (action, TRACE_internal_smpi_get_category());
54 #endif
55   SIMIX_req_host_execution_wait(action);
56 }
57
58 static void smpi_execute(double duration)
59 {
60   /* FIXME: a global variable would be less expensive to consult than a call to xbt_cfg_get_double() right on the critical path */
61   if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
62     XBT_DEBUG("Sleep for %f to handle real computation time", duration);
63     smpi_execute_flops(duration *
64                        xbt_cfg_get_double(_surf_cfg_set,
65                                           "smpi/running_power"));
66   }
67 }
68
69 void smpi_bench_begin(void)
70 {
71   xbt_os_timer_start(smpi_process_timer());
72 }
73
74 void smpi_bench_end(void)
75 {
76   xbt_os_timer_t timer = smpi_process_timer();
77
78   xbt_os_timer_stop(timer);
79   smpi_execute(xbt_os_timer_elapsed(timer));
80 }
81
82 unsigned int smpi_sleep(unsigned int secs)
83 {
84   smpi_execute((double) secs);
85   return secs;
86 }
87
88 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
89 {
90   double now = SIMIX_get_clock();
91
92   if (tv) {
93     tv->tv_sec = (time_t) now;
94     tv->tv_usec = (suseconds_t) (now * 1e6);
95   }
96   return 0;
97 }
98
99 static char *sample_location(int global, const char *file, int line)
100 {
101   if (global) {
102     return bprintf("%s:%d", file, line);
103   } else {
104     return bprintf("%s:%d:%d", file, line, smpi_process_index());
105   }
106 }
107
108 int smpi_sample_1(int global, const char *file, int line, int iters, double threshold)
109 {
110   char *loc = sample_location(global, file, line);
111   local_data_t *data;
112
113   smpi_bench_end();     /* Take time from previous MPI call into account */
114   if (!samples) {
115     samples = xbt_dict_new_homogeneous(free);
116   }
117   data = xbt_dict_get_or_null(samples, loc);
118   if (!data) {
119     data = (local_data_t *) xbt_new(local_data_t, 1);
120     data->count = 0;
121     data->sum = 0.0;
122     data->sum_pow2 = 0.0;
123     data->iters = iters;
124     data->threshold = threshold;
125     data->started = 0;
126     xbt_dict_set(samples, loc, data, NULL);
127     return 0;
128   }
129   free(loc);
130   return 1;
131 }
132
133 int smpi_sample_2(int global, const char *file, int line)
134 {
135   char *loc = sample_location(global, file, line);
136   local_data_t *data;
137
138   xbt_assert(samples, "You did something very inconsistent, didn't you?");
139   data = xbt_dict_get_or_null(samples, loc);
140   if (!data) {
141     xbt_assert(data, "Please, do thing in order");
142   }
143   if (!data->started) {
144     if ((data->iters > 0 && data->count >= data->iters)
145         || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
146       XBT_DEBUG("Perform some wait of %f", data->mean);
147       smpi_execute(data->mean);
148     } else {
149       data->started = 1;
150       data->count++;
151     }
152   } else {
153     data->started = 0;
154   }
155   free(loc);
156   smpi_bench_begin();
157   smpi_process_simulated_start();
158   return data->started;
159 }
160
161 void smpi_sample_3(int global, const char *file, int line)
162 {
163   char *loc = sample_location(global, file, line);
164   local_data_t *data;
165   double sample, n;
166
167   xbt_assert(samples, "You did something very inconsistent, didn't you?");
168   data = xbt_dict_get_or_null(samples, loc);
169   smpi_bench_end();
170   if(data && data->started && data->count < data->iters) {
171     sample = smpi_process_simulated_elapsed();
172     data->sum += sample;
173     data->sum_pow2 += sample * sample;
174     n = (double)data->count;
175     data->mean = data->sum / n;
176     data->relstderr = sqrt((data->sum_pow2 / n - data->mean * data->mean) / n) / data->mean;
177     XBT_DEBUG("Average mean after %d steps is %f, relative standard error is %f (sample was %f)", data->count,
178            data->mean, data->relstderr, sample);
179   }
180   free(loc);
181 }
182
183 void smpi_sample_flops(double flops)
184 {
185   smpi_execute_flops(flops);
186 }
187
188 void *smpi_shared_malloc(size_t size, const char *file, int line)
189 {
190   char *loc = bprintf("%s:%d:%zu", file, line, size);
191   shared_data_t *data;
192
193   if (!allocs) {
194     allocs = xbt_dict_new_homogeneous(free);
195   }
196   data = xbt_dict_get_or_null(allocs, loc);
197   if (!data) {
198     data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
199     data->count = 1;
200     xbt_dict_set(allocs, loc, data, NULL);
201   } else {
202     data->count++;
203   }
204   free(loc);
205   return data->data;
206 }
207
208 void smpi_shared_free(void *ptr)
209 {
210   shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
211   char *loc;
212
213   if (!allocs) {
214     XBT_WARN("Cannot free: nothing was allocated");
215     return;
216   }
217   loc = xbt_dict_get_key(allocs, data);
218   if (!loc) {
219     XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
220     return;
221   }
222   data->count--;
223   if (data->count <= 0) {
224     xbt_dict_remove(allocs, loc);
225   }
226 }
227
228 int smpi_shared_known_call(const char* func, const char* input) {
229    char* loc = bprintf("%s:%s", func, input);
230    xbt_ex_t ex;
231    int known;
232
233    if(!calls) {
234       calls = xbt_dict_new_homogeneous(NULL);
235    }
236    TRY {
237       xbt_dict_get(calls, loc); /* Succeed or throw */
238       known = 1;
239    }
240    CATCH(ex) {
241       if(ex.category == not_found_error) {
242          known = 0;
243          xbt_ex_free(ex);
244       } else {
245          RETHROW;
246       }
247    }
248    free(loc);
249    return known;
250 }
251
252 void* smpi_shared_get_call(const char* func, const char* input) {
253    char* loc = bprintf("%s:%s", func, input);
254    void* data;
255
256    if(!calls) {
257       calls = xbt_dict_new_homogeneous(NULL);
258    }
259    data = xbt_dict_get(calls, loc);
260    free(loc);
261    return data;
262 }
263
264 void* smpi_shared_set_call(const char* func, const char* input, void* data) {
265    char* loc = bprintf("%s:%s", func, input);
266
267    if(!calls) {
268       calls = xbt_dict_new_homogeneous(NULL);
269    }
270    xbt_dict_set(calls, loc, data, NULL);
271    free(loc);
272    return data;
273 }