1 /* Copyright (c) 2007, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include <math.h> // sqrt
10 #include "xbt/sysdep.h"
12 #include "surf/surf.h"
16 #include <sys/types.h>
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
24 "Logging specific to SMPI (benchmarking)");
26 /* Shared allocations are handled through shared memory segments.
27 * Associated data and metadata are used as follows:
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 * -------------------- | ^ |
38 * | | `allocs_metadata' dict |
39 * | | ---------------------- |
40 * | `-- | <addr of mmap #1> |<-'
41 * | .-- | <addr of mmap #2> |<-.
42 * | | ---------------------- |
48 * | shared_metadata_t / | |
49 * | ----------------- | | |
50 * | | size of mmap | --| | |
52 * ----------------- | | |
57 #define PTR_STRLEN (2 + 2 * sizeof(void*) + 1)
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 */
76 static size_t shm_size(int fd) {
79 if(fstat(fd, &st) < 0) {
80 xbt_die("Could not stat fd %d: %s", fd, strerror(errno));
82 return (size_t)st.st_size;
85 static void* shm_map(int fd, size_t size, shared_data_t* data) {
88 shared_metadata_t* meta;
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));
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));
99 if(!allocs_metadata) {
100 allocs_metadata = xbt_dict_new();
102 snprintf(loc, PTR_STRLEN, "%p", mem);
103 meta = xbt_new(shared_metadata_t, 1);
106 xbt_dict_set(allocs_metadata, loc, meta, &free);
107 XBT_DEBUG("MMAP %zu to %p", size, mem);
122 void smpi_bench_destroy(void)
124 xbt_dict_free(&allocs);
125 xbt_dict_free(&samples);
126 xbt_dict_free(&calls);
129 static void smpi_execute_flops(double flops)
133 host = SIMIX_host_self();
135 XBT_DEBUG("Handle real computation time: %f flops", flops);
136 action = SIMIX_req_host_execute("computation", host, flops, 1);
138 SIMIX_req_set_category (action, TRACE_internal_smpi_get_category());
140 SIMIX_req_host_execution_wait(action);
143 static void smpi_execute(double duration)
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"));
154 void smpi_bench_begin(void)
156 xbt_os_timer_start(smpi_process_timer());
157 smpi_current_rank = smpi_process_index();
160 void smpi_bench_end(void)
162 xbt_os_timer_t timer = smpi_process_timer();
164 xbt_os_timer_stop(timer);
165 smpi_execute(xbt_os_timer_elapsed(timer));
168 unsigned int smpi_sleep(unsigned int secs)
171 smpi_execute((double) secs);
176 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
180 now = SIMIX_get_clock();
182 tv->tv_sec = (time_t)now;
183 tv->tv_usec = (suseconds_t)((now - tv->tv_sec) * 1e6);
189 static char *sample_location(int global, const char *file, int line)
192 return bprintf("%s:%d", file, line);
194 return bprintf("%s:%d:%d", file, line, smpi_process_index());
198 int smpi_sample_1(int global, const char *file, int line, int iters, double threshold)
200 char *loc = sample_location(global, file, line);
203 smpi_bench_end(); /* Take time from previous MPI call into account */
205 samples = xbt_dict_new_homogeneous(free);
207 data = xbt_dict_get_or_null(samples, loc);
209 data = (local_data_t *) xbt_new(local_data_t, 1);
212 data->sum_pow2 = 0.0;
214 data->threshold = threshold;
216 xbt_dict_set(samples, loc, data, NULL);
223 int smpi_sample_2(int global, const char *file, int line)
225 char *loc = sample_location(global, file, line);
228 xbt_assert(samples, "You did something very inconsistent, didn't you?");
229 data = xbt_dict_get_or_null(samples, loc);
231 xbt_assert(data, "Please, do thing in order");
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);
247 smpi_process_simulated_start();
248 return data->started;
251 void smpi_sample_3(int global, const char *file, int line)
253 char *loc = sample_location(global, file, line);
257 xbt_assert(samples, "You did something very inconsistent, didn't you?");
258 data = xbt_dict_get_or_null(samples, loc);
260 if(data && data->started && data->count < data->iters) {
261 sample = smpi_process_simulated_elapsed();
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);
273 void smpi_sample_flops(double flops)
275 smpi_execute_flops(flops);
278 void *smpi_shared_malloc(size_t size, const char *file, int line)
280 char *loc = bprintf("%zu_%s_%d", (size_t)getpid(), file, line);
281 size_t len = strlen(loc);
287 for(i = 0; i < len; i++) {
288 /* Make the 'loc' ID be a flat filename */
294 allocs = xbt_dict_new_homogeneous(free);
296 data = xbt_dict_get_or_null(allocs, loc);
298 fd = shm_open(loc, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
302 xbt_die("Please cleanup /dev/shm/%s", loc);
304 xbt_die("An unhandled error occured while opening %s: %s", loc, strerror(errno));
307 data = xbt_new(shared_data_t, 1);
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));
315 xbt_dict_set(allocs, loc, data, NULL);
316 XBT_DEBUG("Mapping %s at %p through %d", loc, mem, fd);
318 mem = shm_map(data->fd, size, data);
321 XBT_DEBUG("Malloc %zu in %p (metadata at %p)", size, mem, data);
325 void smpi_shared_free(void *ptr)
327 char loc[PTR_STRLEN];
328 shared_metadata_t* meta;
332 XBT_WARN("Cannot free: nothing was allocated");
335 if(!allocs_metadata) {
336 XBT_WARN("Cannot free: no metadata was allocated");
338 snprintf(loc, PTR_STRLEN, "%p", ptr);
339 meta = (shared_metadata_t*)xbt_dict_get_or_null(allocs_metadata, loc);
341 XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
346 XBT_WARN("Cannot free: something is broken in the metadata link");
349 if(munmap(ptr, meta->size) < 0) {
350 XBT_WARN("Unmapping of fd %d failed: %s", data->fd, strerror(errno));
353 if (data->count <= 0) {
355 xbt_dict_remove(allocs, data->loc);
360 int smpi_shared_known_call(const char* func, const char* input) {
361 char* loc = bprintf("%s:%s", func, input);
366 calls = xbt_dict_new_homogeneous(NULL);
369 xbt_dict_get(calls, loc); /* Succeed or throw */
373 if(ex.category == not_found_error) {
384 void* smpi_shared_get_call(const char* func, const char* input) {
385 char* loc = bprintf("%s:%s", func, input);
389 calls = xbt_dict_new_homogeneous(NULL);
391 data = xbt_dict_get(calls, loc);
396 void* smpi_shared_set_call(const char* func, const char* input, void* data) {
397 char* loc = bprintf("%s:%s", func, input);
400 calls = xbt_dict_new_homogeneous(NULL);
402 xbt_dict_set(calls, loc, data, NULL);