Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9998aff42cba65b1fdaa0022d0935106eccc7c91
[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   if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
61     XBT_DEBUG("Sleep for %f to handle real computation time", duration);
62     smpi_execute_flops(duration *
63                        xbt_cfg_get_double(_surf_cfg_set,
64                                           "smpi/running_power"));
65   }
66 }
67
68 void smpi_bench_begin(void)
69 {
70   xbt_os_timer_start(smpi_process_timer());
71 }
72
73 void smpi_bench_end(void)
74 {
75   xbt_os_timer_t timer = smpi_process_timer();
76
77   xbt_os_timer_stop(timer);
78   smpi_execute(xbt_os_timer_elapsed(timer));
79 }
80
81 unsigned int smpi_sleep(unsigned int secs)
82 {
83   smpi_execute((double) secs);
84   return secs;
85 }
86
87 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
88 {
89   double now = SIMIX_get_clock();
90
91   if (tv) {
92     tv->tv_sec = (time_t) now;
93     tv->tv_usec = (suseconds_t) (now * 1e6);
94   }
95   return 0;
96 }
97
98 static char *sample_location(int global, const char *file, int line)
99 {
100   if (global) {
101     return bprintf("%s:%d", file, line);
102   } else {
103     return bprintf("%s:%d:%d", file, line, smpi_process_index());
104   }
105 }
106
107 int smpi_sample_1(int global, const char *file, int line, int iters, double threshold)
108 {
109   char *loc = sample_location(global, file, line);
110   local_data_t *data;
111
112   smpi_bench_end();     /* Take time from previous MPI call into account */
113   if (!samples) {
114     samples = xbt_dict_new_homogeneous(free);
115   }
116   data = xbt_dict_get_or_null(samples, loc);
117   if (!data) {
118     data = (local_data_t *) xbt_new(local_data_t, 1);
119     data->count = 0;
120     data->sum = 0.0;
121     data->sum_pow2 = 0.0;
122     data->iters = iters;
123     data->threshold = threshold;
124     data->started = 0;
125     xbt_dict_set(samples, loc, data, NULL);
126     return 0;
127   }
128   free(loc);
129   return 1;
130 }
131
132 int smpi_sample_2(int global, const char *file, int line)
133 {
134   char *loc = sample_location(global, file, line);
135   local_data_t *data;
136
137   xbt_assert(samples, "You did something very inconsistent, didn't you?");
138   data = xbt_dict_get_or_null(samples, loc);
139   if (!data) {
140     xbt_assert(data, "Please, do thing in order");
141   }
142   if (!data->started) {
143     if ((data->iters > 0 && data->count >= data->iters)
144         || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
145       XBT_DEBUG("Perform some wait of %f", data->mean);
146       smpi_execute(data->mean);
147     } else {
148       data->started = 1;
149       data->count++;
150     }
151   } else {
152     data->started = 0;
153   }
154   free(loc);
155   smpi_bench_begin();
156   smpi_process_simulated_start();
157   return data->started;
158 }
159
160 void smpi_sample_3(int global, const char *file, int line)
161 {
162   char *loc = sample_location(global, file, line);
163   local_data_t *data;
164   double sample, n;
165
166   xbt_assert(samples, "You did something very inconsistent, didn't you?");
167   data = xbt_dict_get_or_null(samples, loc);
168   smpi_bench_end();
169   if(data && data->started && data->count < data->iters) {
170     sample = smpi_process_simulated_elapsed();
171     data->sum += sample;
172     data->sum_pow2 += sample * sample;
173     n = (double)data->count;
174     data->mean = data->sum / n;
175     data->relstderr = sqrt((data->sum_pow2 / n - data->mean * data->mean) / n) / data->mean;
176     XBT_DEBUG("Average mean after %d steps is %f, relative standard error is %f (sample was %f)", data->count,
177            data->mean, data->relstderr, sample);
178   }
179   free(loc);
180 }
181
182 void smpi_sample_flops(double flops)
183 {
184   smpi_execute_flops(flops);
185 }
186
187 void *smpi_shared_malloc(size_t size, const char *file, int line)
188 {
189   char *loc = bprintf("%s:%d:%zu", file, line, size);
190   shared_data_t *data;
191
192   if (!allocs) {
193     allocs = xbt_dict_new_homogeneous(free);
194   }
195   data = xbt_dict_get_or_null(allocs, loc);
196   if (!data) {
197     data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
198     data->count = 1;
199     xbt_dict_set(allocs, loc, data, NULL);
200   } else {
201     data->count++;
202   }
203   free(loc);
204   return data->data;
205 }
206
207 void smpi_shared_free(void *ptr)
208 {
209   shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
210   char *loc;
211
212   if (!allocs) {
213     XBT_WARN("Cannot free: nothing was allocated");
214     return;
215   }
216   loc = xbt_dict_get_key(allocs, data);
217   if (!loc) {
218     XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
219     return;
220   }
221   data->count--;
222   if (data->count <= 0) {
223     xbt_dict_remove(allocs, loc);
224   }
225 }
226
227 int smpi_shared_known_call(const char* func, const char* input) {
228    char* loc = bprintf("%s:%s", func, input);
229    xbt_ex_t ex;
230    int known;
231
232    if(!calls) {
233       calls = xbt_dict_new_homogeneous(NULL);
234    }
235    TRY {
236       xbt_dict_get(calls, loc); /* Succeed or throw */
237       known = 1;
238    }
239    CATCH(ex) {
240       if(ex.category == not_found_error) {
241          known = 0;
242          xbt_ex_free(ex);
243       } else {
244          RETHROW;
245       }
246    }
247    free(loc);
248    return known;
249 }
250
251 void* smpi_shared_get_call(const char* func, const char* input) {
252    char* loc = bprintf("%s:%s", func, input);
253    void* data;
254
255    if(!calls) {
256       calls = xbt_dict_new_homogeneous(NULL);
257    }
258    data = xbt_dict_get(calls, loc);
259    free(loc);
260    return data;
261 }
262
263 void* smpi_shared_set_call(const char* func, const char* input, void* data) {
264    char* loc = bprintf("%s:%s", func, input);
265
266    if(!calls) {
267       calls = xbt_dict_new_homogeneous(NULL);
268    }
269    xbt_dict_set(calls, loc, data, NULL);
270    free(loc);
271    return data;
272 }