Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Offer the possibility to change smpi bandwidth and latency factor into tag config...
[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 #include <sys/mman.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <stdio.h>
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
24                                 "Logging specific to SMPI (benchmarking)");
25
26 /* Shared allocations are handled through shared memory segments.
27  * Associated data and metadata are used as follows:
28  *
29  *                                                                    mmap #1
30  *    `allocs' dict                                                     ---- -.
31  *    ----------      shared_data_t               shared_metadata_t   / |  |  |
32  * .->| <name> | ---> -------------------- <--.   -----------------   | |  |  |
33  * |  ----------      | fd of <name>     |    |   | size of mmap  | --| |  |  |
34  * |                  | count (2)        |    |-- | data          |   \ |  |  |
35  * `----------------- | <name>           |    |   -----------------     ----  |
36  *                    --------------------    |   ^                           |
37  *                                            |   |                           |
38  *                                            |   |   `allocs_metadata' dict  |
39  *                                            |   |   ----------------------  |
40  *                                            |   `-- | <addr of mmap #1>  |<-'
41  *                                            |   .-- | <addr of mmap #2>  |<-.
42  *                                            |   |   ----------------------  |
43  *                                            |   |                           |
44  *                                            |   |                           |
45  *                                            |   |                           |
46  *                                            |   |                   mmap #2 |
47  *                                            |   v                     ---- -'
48  *                                            |   shared_metadata_t   / |  |
49  *                                            |   -----------------   | |  |
50  *                                            |   | size of mmap  | --| |  |
51  *                                            `-- | data          |   | |  |
52  *                                                -----------------   | |  |
53  *                                                                    \ |  |
54  *                                                                      ----
55  */
56
57 #define PTR_STRLEN (2 + 2 * sizeof(void*) + 1)
58
59 xbt_dict_t allocs = NULL;          /* Allocated on first use */
60 xbt_dict_t allocs_metadata = NULL; /* Allocated on first use */
61 xbt_dict_t samples = NULL;         /* Allocated on first use */
62 xbt_dict_t calls = NULL;           /* Allocated on first use */
63 __thread int smpi_current_rank = 0;      /* Updated after each MPI call */
64
65 typedef struct {
66   int fd;
67   int count;
68   char* loc;
69 } shared_data_t;
70
71 typedef struct  {
72   size_t size;
73   shared_data_t* data;
74 } shared_metadata_t;
75
76 static size_t shm_size(int fd) {
77   struct stat st;
78
79   if(fstat(fd, &st) < 0) {
80     xbt_die("Could not stat fd %d: %s", fd, strerror(errno));
81   }
82   return (size_t)st.st_size;
83 }
84
85 static void* shm_map(int fd, size_t size, shared_data_t* data) {
86   void* mem;
87   char loc[PTR_STRLEN];
88   shared_metadata_t* meta;
89
90   if(size > shm_size(fd)) {
91     if(ftruncate(fd, (off_t)size) < 0) {
92       xbt_die("Could not truncate fd %d to %zu: %s", fd, size, strerror(errno));
93     }
94   }
95   mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
96   if(mem == MAP_FAILED) {
97     xbt_die("Could not map fd %d: %s", fd, strerror(errno));
98   }
99   if(!allocs_metadata) {
100     allocs_metadata = xbt_dict_new();
101   }
102   snprintf(loc, PTR_STRLEN, "%p", mem);
103   meta = xbt_new(shared_metadata_t, 1);
104   meta->size = size;
105   meta->data = data;
106   xbt_dict_set(allocs_metadata, loc, meta, &free);
107   XBT_DEBUG("MMAP %zu to %p", size, mem);
108   return mem;
109 }
110
111 typedef struct {
112   int count;
113   double sum;
114   double sum_pow2;
115   double mean;
116   double relstderr;
117   int iters;
118   double threshold;
119   int started;
120 } local_data_t;
121
122 void smpi_bench_destroy(void)
123 {
124   xbt_dict_free(&allocs);
125   xbt_dict_free(&samples);
126   xbt_dict_free(&calls);
127 }
128
129 static void smpi_execute_flops(double flops)
130 {
131   smx_action_t action;
132   smx_host_t host;
133   host = SIMIX_host_self();
134
135   XBT_DEBUG("Handle real computation time: %f flops", flops);
136   action = SIMIX_req_host_execute("computation", host, flops, 1);
137 #ifdef HAVE_TRACING
138   SIMIX_req_set_category (action, TRACE_internal_smpi_get_category());
139 #endif
140   SIMIX_req_host_execution_wait(action);
141 }
142
143 static void smpi_execute(double duration)
144 {
145   /* FIXME: a global variable would be less expensive to consult than a call to xbt_cfg_get_double() right on the critical path */
146   if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
147     XBT_DEBUG("Sleep for %f to handle real computation time", duration);
148     smpi_execute_flops(duration *
149                        xbt_cfg_get_double(_surf_cfg_set,
150                                           "smpi/running_power"));
151   } else {
152           XBT_DEBUG("Real computation took %f while threshold is set to %f; ignore it",
153                           duration, xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold"));
154   }
155 }
156
157 void smpi_bench_begin(void)
158 {
159   xbt_os_timer_start(smpi_process_timer());
160   smpi_current_rank = smpi_process_index();
161 }
162
163 void smpi_bench_end(void)
164 {
165   xbt_os_timer_t timer = smpi_process_timer();
166
167   xbt_os_timer_stop(timer);
168   smpi_execute(xbt_os_timer_elapsed(timer));
169 }
170
171 unsigned int smpi_sleep(unsigned int secs)
172 {
173   smpi_bench_end();
174   smpi_execute((double) secs);
175   smpi_bench_begin();
176   return secs;
177 }
178
179 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
180 {
181   double now;
182   smpi_bench_end();
183   now = SIMIX_get_clock();
184   if (tv) {
185     tv->tv_sec = (time_t)now;
186     tv->tv_usec = (suseconds_t)((now - tv->tv_sec) * 1e6);
187   }
188   smpi_bench_begin();
189   return 0;
190 }
191
192 static char *sample_location(int global, const char *file, int line)
193 {
194   if (global) {
195     return bprintf("%s:%d", file, line);
196   } else {
197     return bprintf("%s:%d:%d", file, line, smpi_process_index());
198   }
199 }
200
201 int smpi_sample_1(int global, const char *file, int line, int iters, double threshold)
202 {
203   char *loc = sample_location(global, file, line);
204   local_data_t *data;
205
206   smpi_bench_end();     /* Take time from previous MPI call into account */
207   if (!samples) {
208     samples = xbt_dict_new_homogeneous(free);
209   }
210   data = xbt_dict_get_or_null(samples, loc);
211   if (!data) {
212     data = (local_data_t *) xbt_new(local_data_t, 1);
213     data->count = 0;
214     data->sum = 0.0;
215     data->sum_pow2 = 0.0;
216     data->iters = iters;
217     data->threshold = threshold;
218     data->started = 0;
219     xbt_dict_set(samples, loc, data, NULL);
220     return 0;
221   }
222   free(loc);
223   return 1;
224 }
225
226 int smpi_sample_2(int global, const char *file, int line)
227 {
228   char *loc = sample_location(global, file, line);
229   local_data_t *data;
230
231   xbt_assert(samples, "You did something very inconsistent, didn't you?");
232   data = xbt_dict_get_or_null(samples, loc);
233   if (!data) {
234     xbt_assert(data, "Please, do thing in order");
235   }
236   if (!data->started) {
237     if ((data->iters > 0 && data->count >= data->iters)
238         || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
239       XBT_DEBUG("Perform some wait of %f", data->mean);
240       smpi_execute(data->mean);
241     } else {
242       data->started = 1;
243       data->count++;
244     }
245   } else {
246     data->started = 0;
247   }
248   free(loc);
249   smpi_bench_begin();
250   smpi_process_simulated_start();
251   return data->started;
252 }
253
254 void smpi_sample_3(int global, const char *file, int line)
255 {
256   char *loc = sample_location(global, file, line);
257   local_data_t *data;
258   double sample, n;
259
260   xbt_assert(samples, "You did something very inconsistent, didn't you?");
261   data = xbt_dict_get_or_null(samples, loc);
262   smpi_bench_end();
263   if(data && data->started && data->count < data->iters) {
264     sample = smpi_process_simulated_elapsed();
265     data->sum += sample;
266     data->sum_pow2 += sample * sample;
267     n = (double)data->count;
268     data->mean = data->sum / n;
269     data->relstderr = sqrt((data->sum_pow2 / n - data->mean * data->mean) / n) / data->mean;
270     XBT_DEBUG("Average mean after %d steps is %f, relative standard error is %f (sample was %f)", data->count,
271            data->mean, data->relstderr, sample);
272   }
273   free(loc);
274 }
275
276 void smpi_sample_flops(double flops)
277 {
278   smpi_execute_flops(flops);
279 }
280
281 void *smpi_shared_malloc(size_t size, const char *file, int line)
282 {
283   char *loc = bprintf("%zu_%s_%d", (size_t)getpid(), file, line);
284   size_t len = strlen(loc);
285   size_t i;
286   int fd;
287   void* mem;
288   shared_data_t *data;
289
290   for(i = 0; i < len; i++) {
291     /* Make the 'loc' ID be a flat filename */
292     if(loc[i] == '/') {
293       loc[i] = '_';
294     }
295   }
296   if (!allocs) {
297     allocs = xbt_dict_new_homogeneous(free);
298   }
299   data = xbt_dict_get_or_null(allocs, loc);
300   if(!data) {
301     fd = shm_open(loc, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
302     if(fd < 0) {
303       switch(errno) {
304         case EEXIST:
305           xbt_die("Please cleanup /dev/shm/%s", loc);
306         default:
307           xbt_die("An unhandled error occured while opening %s: %s", loc, strerror(errno));
308       }
309     }
310     data = xbt_new(shared_data_t, 1);
311     data->fd = fd;
312     data->count = 1;
313     data->loc = loc;
314     mem = shm_map(fd, size, data);
315     if(shm_unlink(loc) < 0) {
316       XBT_WARN("Could not early unlink %s: %s", loc, strerror(errno));
317     }
318     xbt_dict_set(allocs, loc, data, NULL);
319     XBT_DEBUG("Mapping %s at %p through %d", loc, mem, fd);
320   } else {
321     mem = shm_map(data->fd, size, data);
322     data->count++;
323   }
324   XBT_DEBUG("Malloc %zu in %p (metadata at %p)", size, mem, data);
325   return mem;
326 }
327
328 void smpi_shared_free(void *ptr)
329 {
330   char loc[PTR_STRLEN];
331   shared_metadata_t* meta;
332   shared_data_t* data;
333
334   if (!allocs) {
335     XBT_WARN("Cannot free: nothing was allocated");
336     return;
337   }
338   if(!allocs_metadata) {
339     XBT_WARN("Cannot free: no metadata was allocated");
340   }
341   snprintf(loc, PTR_STRLEN, "%p", ptr);
342   meta = (shared_metadata_t*)xbt_dict_get_or_null(allocs_metadata, loc);
343   if (!meta) {
344     XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
345     return;
346   }
347   data = meta->data;
348   if(!data) {
349     XBT_WARN("Cannot free: something is broken in the metadata link");
350     return;
351   }
352   if(munmap(ptr, meta->size) < 0) {
353     XBT_WARN("Unmapping of fd %d failed: %s", data->fd, strerror(errno));
354   }
355   data->count--;
356   if (data->count <= 0) {
357     close(data->fd);
358     xbt_dict_remove(allocs, data->loc);
359     free(data->loc);
360   }
361 }
362
363 int smpi_shared_known_call(const char* func, const char* input) {
364    char* loc = bprintf("%s:%s", func, input);
365    xbt_ex_t ex;
366    int known;
367
368    if(!calls) {
369       calls = xbt_dict_new_homogeneous(NULL);
370    }
371    TRY {
372       xbt_dict_get(calls, loc); /* Succeed or throw */
373       known = 1;
374    }
375    CATCH(ex) {
376       if(ex.category == not_found_error) {
377          known = 0;
378          xbt_ex_free(ex);
379       } else {
380          RETHROW;
381       }
382    }
383    free(loc);
384    return known;
385 }
386
387 void* smpi_shared_get_call(const char* func, const char* input) {
388    char* loc = bprintf("%s:%s", func, input);
389    void* data;
390
391    if(!calls) {
392       calls = xbt_dict_new_homogeneous(NULL);
393    }
394    data = xbt_dict_get(calls, loc);
395    free(loc);
396    return data;
397 }
398
399 void* smpi_shared_set_call(const char* func, const char* input, void* data) {
400    char* loc = bprintf("%s:%s", func, input);
401
402    if(!calls) {
403       calls = xbt_dict_new_homogeneous(NULL);
404    }
405    xbt_dict_set(calls, loc, data, NULL);
406    free(loc);
407    return data;
408 }