Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use sensible process names (their rank) in SMPI, and get simix ignore that
[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   }
152 }
153
154 void smpi_bench_begin(void)
155 {
156   xbt_os_timer_start(smpi_process_timer());
157   smpi_current_rank = smpi_process_index();
158 }
159
160 void smpi_bench_end(void)
161 {
162   xbt_os_timer_t timer = smpi_process_timer();
163
164   xbt_os_timer_stop(timer);
165   smpi_execute(xbt_os_timer_elapsed(timer));
166 }
167
168 unsigned int smpi_sleep(unsigned int secs)
169 {
170   smpi_bench_end();
171   smpi_execute((double) secs);
172   smpi_bench_begin();
173   return secs;
174 }
175
176 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
177 {
178   double now;
179   smpi_bench_end();
180   now = SIMIX_get_clock();
181   if (tv) {
182     tv->tv_sec = (time_t)now;
183     tv->tv_usec = (suseconds_t)((now - tv->tv_sec) * 1e6);
184   }
185   smpi_bench_begin();
186   return 0;
187 }
188
189 static char *sample_location(int global, const char *file, int line)
190 {
191   if (global) {
192     return bprintf("%s:%d", file, line);
193   } else {
194     return bprintf("%s:%d:%d", file, line, smpi_process_index());
195   }
196 }
197
198 int smpi_sample_1(int global, const char *file, int line, int iters, double threshold)
199 {
200   char *loc = sample_location(global, file, line);
201   local_data_t *data;
202
203   smpi_bench_end();     /* Take time from previous MPI call into account */
204   if (!samples) {
205     samples = xbt_dict_new_homogeneous(free);
206   }
207   data = xbt_dict_get_or_null(samples, loc);
208   if (!data) {
209     data = (local_data_t *) xbt_new(local_data_t, 1);
210     data->count = 0;
211     data->sum = 0.0;
212     data->sum_pow2 = 0.0;
213     data->iters = iters;
214     data->threshold = threshold;
215     data->started = 0;
216     xbt_dict_set(samples, loc, data, NULL);
217     return 0;
218   }
219   free(loc);
220   return 1;
221 }
222
223 int smpi_sample_2(int global, const char *file, int line)
224 {
225   char *loc = sample_location(global, file, line);
226   local_data_t *data;
227
228   xbt_assert(samples, "You did something very inconsistent, didn't you?");
229   data = xbt_dict_get_or_null(samples, loc);
230   if (!data) {
231     xbt_assert(data, "Please, do thing in order");
232   }
233   if (!data->started) {
234     if ((data->iters > 0 && data->count >= data->iters)
235         || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
236       XBT_DEBUG("Perform some wait of %f", data->mean);
237       smpi_execute(data->mean);
238     } else {
239       data->started = 1;
240       data->count++;
241     }
242   } else {
243     data->started = 0;
244   }
245   free(loc);
246   smpi_bench_begin();
247   smpi_process_simulated_start();
248   return data->started;
249 }
250
251 void smpi_sample_3(int global, const char *file, int line)
252 {
253   char *loc = sample_location(global, file, line);
254   local_data_t *data;
255   double sample, n;
256
257   xbt_assert(samples, "You did something very inconsistent, didn't you?");
258   data = xbt_dict_get_or_null(samples, loc);
259   smpi_bench_end();
260   if(data && data->started && data->count < data->iters) {
261     sample = smpi_process_simulated_elapsed();
262     data->sum += sample;
263     data->sum_pow2 += sample * sample;
264     n = (double)data->count;
265     data->mean = data->sum / n;
266     data->relstderr = sqrt((data->sum_pow2 / n - data->mean * data->mean) / n) / data->mean;
267     XBT_DEBUG("Average mean after %d steps is %f, relative standard error is %f (sample was %f)", data->count,
268            data->mean, data->relstderr, sample);
269   }
270   free(loc);
271 }
272
273 void smpi_sample_flops(double flops)
274 {
275   smpi_execute_flops(flops);
276 }
277
278 void *smpi_shared_malloc(size_t size, const char *file, int line)
279 {
280   char *loc = bprintf("%zu_%s_%d", (size_t)getpid(), file, line);
281   size_t len = strlen(loc);
282   size_t i;
283   int fd;
284   void* mem;
285   shared_data_t *data;
286
287   for(i = 0; i < len; i++) {
288     /* Make the 'loc' ID be a flat filename */
289     if(loc[i] == '/') {
290       loc[i] = '_';
291     }
292   }
293   if (!allocs) {
294     allocs = xbt_dict_new_homogeneous(free);
295   }
296   data = xbt_dict_get_or_null(allocs, loc);
297   if(!data) {
298     fd = shm_open(loc, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
299     if(fd < 0) {
300       switch(errno) {
301         case EEXIST:
302           xbt_die("Please cleanup /dev/shm/%s", loc);
303         default:
304           xbt_die("An unhandled error occured while opening %s: %s", loc, strerror(errno));
305       }
306     }
307     data = xbt_new(shared_data_t, 1);
308     data->fd = fd;
309     data->count = 1;
310     data->loc = loc;
311     mem = shm_map(fd, size, data);
312     if(shm_unlink(loc) < 0) {
313       XBT_WARN("Could not early unlink %s: %s", loc, strerror(errno));
314     }
315     xbt_dict_set(allocs, loc, data, NULL);
316     XBT_DEBUG("Mapping %s at %p through %d", loc, mem, fd);
317   } else {
318     mem = shm_map(data->fd, size, data);
319     data->count++;
320   }
321   XBT_DEBUG("Malloc %zu in %p (metadata at %p)", size, mem, data);
322   return mem;
323 }
324
325 void smpi_shared_free(void *ptr)
326 {
327   char loc[PTR_STRLEN];
328   shared_metadata_t* meta;
329   shared_data_t* data;
330
331   if (!allocs) {
332     XBT_WARN("Cannot free: nothing was allocated");
333     return;
334   }
335   if(!allocs_metadata) {
336     XBT_WARN("Cannot free: no metadata was allocated");
337   }
338   snprintf(loc, PTR_STRLEN, "%p", ptr);
339   meta = (shared_metadata_t*)xbt_dict_get_or_null(allocs_metadata, loc);
340   if (!meta) {
341     XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
342     return;
343   }
344   data = meta->data;
345   if(!data) {
346     XBT_WARN("Cannot free: something is broken in the metadata link");
347     return;
348   }
349   if(munmap(ptr, meta->size) < 0) {
350     XBT_WARN("Unmapping of fd %d failed: %s", data->fd, strerror(errno));
351   }
352   data->count--;
353   if (data->count <= 0) {
354     close(data->fd);
355     xbt_dict_remove(allocs, data->loc);
356     free(data->loc);
357   }
358 }
359
360 int smpi_shared_known_call(const char* func, const char* input) {
361    char* loc = bprintf("%s:%s", func, input);
362    xbt_ex_t ex;
363    int known;
364
365    if(!calls) {
366       calls = xbt_dict_new_homogeneous(NULL);
367    }
368    TRY {
369       xbt_dict_get(calls, loc); /* Succeed or throw */
370       known = 1;
371    }
372    CATCH(ex) {
373       if(ex.category == not_found_error) {
374          known = 0;
375          xbt_ex_free(ex);
376       } else {
377          RETHROW;
378       }
379    }
380    free(loc);
381    return known;
382 }
383
384 void* smpi_shared_get_call(const char* func, const char* input) {
385    char* loc = bprintf("%s:%s", func, input);
386    void* data;
387
388    if(!calls) {
389       calls = xbt_dict_new_homogeneous(NULL);
390    }
391    data = xbt_dict_get(calls, loc);
392    free(loc);
393    return data;
394 }
395
396 void* smpi_shared_set_call(const char* func, const char* input, void* data) {
397    char* loc = bprintf("%s:%s", func, input);
398
399    if(!calls) {
400       calls = xbt_dict_new_homogeneous(NULL);
401    }
402    xbt_dict_set(calls, loc, data, NULL);
403    free(loc);
404    return data;
405 }