Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
commiting some more automatic source patching stuff, and renamed some of the
[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   DEBUG1("Handle real computation time: %f flops", flops);
51   action = SIMIX_req_host_execute("computation", host, flops);
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   SIMIX_req_host_execution_destroy(action);
57 }
58
59 static void smpi_execute(double duration)
60 {
61   if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
62     DEBUG1("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 void 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();
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, &free);
127   }
128   free(loc);
129 }
130
131 int smpi_sample_2(int global, const char *file, int line)
132 {
133   char *loc = sample_location(global, file, line);
134   local_data_t *data;
135
136   xbt_assert0(samples, "You did something very inconsistent, didn't you?");
137   data = xbt_dict_get_or_null(samples, loc);
138   if (!data) {
139     xbt_assert0(data, "Please, do thing in order");
140   }
141   if (!data->started) {
142     if ((data->iters > 0 && data->count >= data->iters)
143         || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
144       DEBUG1("Perform some wait of %f", data->mean);
145       smpi_execute(data->mean);
146     } else {
147       data->started = 1;
148       data->count++;
149     }
150   } else {
151     data->started = 0;
152   }
153   free(loc);
154   smpi_bench_begin();
155   smpi_process_simulated_start();
156   return data->started;
157 }
158
159 void smpi_sample_3(int global, const char *file, int line)
160 {
161   char *loc = sample_location(global, file, line);
162   local_data_t *data;
163   double sample, n;
164
165   xbt_assert0(samples, "You did something very inconsistent, didn't you?");
166   data = xbt_dict_get_or_null(samples, loc);
167   if (!data || !data->started || data->count >= data->iters) {
168     xbt_assert0(data, "Please, do thing in order");
169   }
170   smpi_bench_end();
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   DEBUG4("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
181 void smpi_sample_flops(double flops)
182 {
183   smpi_execute_flops(flops);
184 }
185
186 void *smpi_shared_malloc(size_t size, const char *file, int line)
187 {
188   char *loc = bprintf("%s:%d:%zu", file, line, size);
189   shared_data_t *data;
190
191   if (!allocs) {
192     allocs = xbt_dict_new();
193   }
194   data = xbt_dict_get_or_null(allocs, loc);
195   if (!data) {
196     data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
197     data->count = 1;
198     xbt_dict_set(allocs, loc, data, &free);
199   } else {
200     data->count++;
201   }
202   free(loc);
203   return data->data;
204 }
205
206 void smpi_shared_free(void *ptr)
207 {
208   shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
209   char *loc;
210
211   if (!allocs) {
212     WARN0("Cannot free: nothing was allocated");
213     return;
214   }
215   loc = xbt_dict_get_key(allocs, data);
216   if (!loc) {
217     WARN1("Cannot free: %p was not shared-allocated by SMPI", ptr);
218     return;
219   }
220   data->count--;
221   if (data->count <= 0) {
222     xbt_dict_remove(allocs, loc);
223   }
224 }