Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d5eb6b3e9f9da9e363b27353018b9f40b4007b99
[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 "private.h"
8 #include "xbt/dict.h"
9 #include "xbt/sysdep.h"
10 #include "surf/surf.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
13                                 "Logging specific to SMPI (benchmarking)");
14
15 xbt_dict_t allocs = NULL;       /* Allocated on first use */
16 xbt_dict_t samples = NULL;      /* Allocated on first use */
17
18 typedef struct {
19   int count;
20   char data[];
21 } shared_data_t;
22
23 typedef struct {
24   int count;
25   double sum;
26   double sum_pow2;
27   double mean;
28   double relstderr;
29   int iters;
30   double threshold;
31   int started;
32 } local_data_t;
33
34 void smpi_bench_destroy(void)
35 {
36   if (allocs) {
37     xbt_dict_free(&allocs);
38   }
39   if (samples) {
40     xbt_dict_free(&samples);
41   }
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 void 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();
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, &free);
126   }
127   free(loc);
128 }
129
130 int smpi_sample_2(int global, const char *file, int line)
131 {
132   char *loc = sample_location(global, file, line);
133   local_data_t *data;
134
135   xbt_assert0(samples, "You did something very inconsistent, didn't you?");
136   data = xbt_dict_get_or_null(samples, loc);
137   if (!data) {
138     xbt_assert0(data, "Please, do thing in order");
139   }
140   if (!data->started) {
141     if ((data->iters > 0 && data->count >= data->iters)
142         || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
143       XBT_DEBUG("Perform some wait of %f", data->mean);
144       smpi_execute(data->mean);
145     } else {
146       data->started = 1;
147       data->count++;
148     }
149   } else {
150     data->started = 0;
151   }
152   free(loc);
153   smpi_bench_begin();
154   smpi_process_simulated_start();
155   return data->started;
156 }
157
158 void smpi_sample_3(int global, const char *file, int line)
159 {
160   char *loc = sample_location(global, file, line);
161   local_data_t *data;
162   double sample, n;
163
164   xbt_assert0(samples, "You did something very inconsistent, didn't you?");
165   data = xbt_dict_get_or_null(samples, loc);
166   if (!data || !data->started || data->count >= data->iters) {
167     xbt_assert0(data, "Please, do thing in order");
168   }
169   smpi_bench_end();
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
180 void smpi_sample_flops(double flops)
181 {
182   smpi_execute_flops(flops);
183 }
184
185 void *smpi_shared_malloc(size_t size, const char *file, int line)
186 {
187   char *loc = bprintf("%s:%d:%zu", file, line, size);
188   shared_data_t *data;
189
190   if (!allocs) {
191     allocs = xbt_dict_new();
192   }
193   data = xbt_dict_get_or_null(allocs, loc);
194   if (!data) {
195     data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
196     data->count = 1;
197     xbt_dict_set(allocs, loc, data, &free);
198   } else {
199     data->count++;
200   }
201   free(loc);
202   return data->data;
203 }
204
205 void smpi_shared_free(void *ptr)
206 {
207   shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
208   char *loc;
209
210   if (!allocs) {
211     XBT_WARN("Cannot free: nothing was allocated");
212     return;
213   }
214   loc = xbt_dict_get_key(allocs, data);
215   if (!loc) {
216     XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
217     return;
218   }
219   data->count--;
220   if (data->count <= 0) {
221     xbt_dict_remove(allocs, loc);
222   }
223 }