Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use a global variable to cache the rank of currently running process.
[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 int smpi_current_rank = 0;      /* Updated after each MPI call */
18
19 typedef struct {
20   int count;
21   char data[];
22 } shared_data_t;
23
24 typedef struct {
25   int count;
26   double sum;
27   double sum_pow2;
28   double mean;
29   double relstderr;
30   int iters;
31   double threshold;
32   int started;
33 } local_data_t;
34
35 void smpi_bench_destroy(void)
36 {
37   if (allocs) {
38     xbt_dict_free(&allocs);
39   }
40   if (samples) {
41     xbt_dict_free(&samples);
42   }
43 }
44
45 static void smpi_execute_flops(double flops)
46 {
47   smx_action_t action;
48   smx_host_t host;
49   host = SIMIX_host_self();
50
51   XBT_DEBUG("Handle real computation time: %f flops", flops);
52   action = SIMIX_req_host_execute("computation", host, flops, 1);
53 #ifdef HAVE_TRACING
54   SIMIX_req_set_category (action, TRACE_internal_smpi_get_category());
55 #endif
56   SIMIX_req_host_execution_wait(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     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   smpi_current_rank = smpi_process_index();
73 }
74
75 void smpi_bench_end(void)
76 {
77   xbt_os_timer_t timer = smpi_process_timer();
78
79   xbt_os_timer_stop(timer);
80   smpi_execute(xbt_os_timer_elapsed(timer));
81 }
82
83 unsigned int smpi_sleep(unsigned int secs)
84 {
85   smpi_execute((double) secs);
86   return secs;
87 }
88
89 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
90 {
91   double now = SIMIX_get_clock();
92
93   if (tv) {
94     tv->tv_sec = (time_t) now;
95     tv->tv_usec = (suseconds_t) (now * 1e6);
96   }
97   return 0;
98 }
99
100 static char *sample_location(int global, const char *file, int line)
101 {
102   if (global) {
103     return bprintf("%s:%d", file, line);
104   } else {
105     return bprintf("%s:%d:%d", file, line, smpi_process_index());
106   }
107 }
108
109 int smpi_sample_1(int global, const char *file, int line, int iters, double threshold)
110 {
111   char *loc = sample_location(global, file, line);
112   local_data_t *data;
113
114   smpi_bench_end();     /* Take time from previous MPI call into account */
115   if (!samples) {
116     samples = xbt_dict_new();
117   }
118   data = xbt_dict_get_or_null(samples, loc);
119   if (!data) {
120     data = (local_data_t *) xbt_new(local_data_t, 1);
121     data->count = 0;
122     data->sum = 0.0;
123     data->sum_pow2 = 0.0;
124     data->iters = iters;
125     data->threshold = threshold;
126     data->started = 0;
127     xbt_dict_set(samples, loc, data, &free);
128     return 0;
129   }
130   free(loc);
131   return 1;
132 }
133
134 int smpi_sample_2(int global, const char *file, int line)
135 {
136   char *loc = sample_location(global, file, line);
137   local_data_t *data;
138
139   xbt_assert(samples, "You did something very inconsistent, didn't you?");
140   data = xbt_dict_get_or_null(samples, loc);
141   if (!data) {
142     xbt_assert(data, "Please, do thing in order");
143   }
144   if (!data->started) {
145     if ((data->iters > 0 && data->count >= data->iters)
146         || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
147       XBT_DEBUG("Perform some wait of %f", data->mean);
148       smpi_execute(data->mean);
149     } else {
150       data->started = 1;
151       data->count++;
152     }
153   } else {
154     data->started = 0;
155   }
156   free(loc);
157   smpi_bench_begin();
158   smpi_process_simulated_start();
159   return data->started;
160 }
161
162 void smpi_sample_3(int global, const char *file, int line)
163 {
164   char *loc = sample_location(global, file, line);
165   local_data_t *data;
166   double sample, n;
167
168   xbt_assert(samples, "You did something very inconsistent, didn't you?");
169   data = xbt_dict_get_or_null(samples, loc);
170   smpi_bench_end();
171   if(data && data->started && data->count < data->iters) {
172     sample = smpi_process_simulated_elapsed();
173     data->sum += sample;
174     data->sum_pow2 += sample * sample;
175     n = (double)data->count;
176     data->mean = data->sum / n;
177     data->relstderr = sqrt((data->sum_pow2 / n - data->mean * data->mean) / n) / data->mean;
178     XBT_DEBUG("Average mean after %d steps is %f, relative standard error is %f (sample was %f)", data->count,
179            data->mean, data->relstderr, sample);
180   }
181   free(loc);
182 }
183
184 void smpi_sample_flops(double flops)
185 {
186   smpi_execute_flops(flops);
187 }
188
189 void *smpi_shared_malloc(size_t size, const char *file, int line)
190 {
191   char *loc = bprintf("%s:%d:%zu", file, line, size);
192   shared_data_t *data;
193
194   if (!allocs) {
195     allocs = xbt_dict_new();
196   }
197   data = xbt_dict_get_or_null(allocs, loc);
198   if (!data) {
199     data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
200     data->count = 1;
201     xbt_dict_set(allocs, loc, data, &free);
202   } else {
203     data->count++;
204   }
205   free(loc);
206   return data->data;
207 }
208
209 void smpi_shared_free(void *ptr)
210 {
211   shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
212   char *loc;
213
214   if (!allocs) {
215     XBT_WARN("Cannot free: nothing was allocated");
216     return;
217   }
218   loc = xbt_dict_get_key(allocs, data);
219   if (!loc) {
220     XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
221     return;
222   }
223   data->count--;
224   if (data->count <= 0) {
225     xbt_dict_remove(allocs, loc);
226   }
227 }