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. */
9 #include "xbt/sysdep.h"
11 #include "surf/surf.h"
15 #include <sys/types.h>
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
23 "Logging specific to SMPI (benchmarking)");
25 /* Shared allocations are handled through shared memory segments.
26 * Associated data and metadata are used as follows:
29 * `allocs' dict ---- -.
30 * ---------- shared_data_t shared_metadata_t / | | |
31 * .->| <name> | ---> -------------------- <--. ----------------- | | | |
32 * | ---------- | fd of <name> | | | size of mmap | --| | | |
33 * | | count (2) | |-- | data | \ | | |
34 * `----------------- | <name> | | ----------------- ---- |
35 * -------------------- | ^ |
37 * | | `allocs_metadata' dict |
38 * | | ---------------------- |
39 * | `-- | <addr of mmap #1> |<-'
40 * | .-- | <addr of mmap #2> |<-.
41 * | | ---------------------- |
47 * | shared_metadata_t / | |
48 * | ----------------- | | |
49 * | | size of mmap | --| | |
51 * ----------------- | | |
56 #define PTR_STRLEN (2 + 2 * sizeof(void*) + 1)
58 xbt_dict_t allocs = NULL; /* Allocated on first use */
59 xbt_dict_t allocs_metadata = NULL; /* Allocated on first use */
60 xbt_dict_t samples = NULL; /* Allocated on first use */
61 xbt_dict_t calls = NULL; /* Allocated on first use */
62 __thread int smpi_current_rank = 0; /* Updated after each MPI call */
75 static size_t shm_size(int fd) {
78 if(fstat(fd, &st) < 0) {
79 xbt_die("Could not stat fd %d: %s", fd, strerror(errno));
81 return (size_t)st.st_size;
84 static void* shm_map(int fd, size_t size, shared_data_t* data) {
87 shared_metadata_t* meta;
89 if(size > shm_size(fd)) {
90 if(ftruncate(fd, (off_t)size) < 0) {
91 xbt_die("Could not truncate fd %d to %zu: %s", fd, size, strerror(errno));
94 mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
95 if(mem == MAP_FAILED) {
96 xbt_die("Could not map fd %d: %s", fd, strerror(errno));
98 if(!allocs_metadata) {
99 allocs_metadata = xbt_dict_new();
101 snprintf(loc, PTR_STRLEN, "%p", mem);
102 meta = xbt_new(shared_metadata_t, 1);
105 xbt_dict_set(allocs_metadata, loc, meta, &free);
106 XBT_DEBUG("MMAP %zu to %p", size, mem);
121 void smpi_bench_destroy(void)
124 xbt_dict_free(&allocs);
127 xbt_dict_free(&samples);
130 xbt_dict_free(&calls);
134 static void smpi_execute_flops(double flops)
138 host = SIMIX_host_self();
140 XBT_DEBUG("Handle real computation time: %f flops", flops);
141 action = SIMIX_req_host_execute("computation", host, flops, 1);
143 SIMIX_req_set_category (action, TRACE_internal_smpi_get_category());
145 SIMIX_req_host_execution_wait(action);
148 static void smpi_execute(double duration)
150 if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
151 XBT_DEBUG("Sleep for %f to handle real computation time", duration);
152 smpi_execute_flops(duration *
153 xbt_cfg_get_double(_surf_cfg_set,
154 "smpi/running_power"));
158 void smpi_bench_begin(void)
160 xbt_os_timer_start(smpi_process_timer());
161 smpi_current_rank = smpi_process_index();
164 void smpi_bench_end(void)
166 xbt_os_timer_t timer = smpi_process_timer();
168 xbt_os_timer_stop(timer);
169 smpi_execute(xbt_os_timer_elapsed(timer));
172 unsigned int smpi_sleep(unsigned int secs)
175 smpi_execute((double) secs);
180 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
184 now = SIMIX_get_clock();
186 tv->tv_sec = (time_t)now;
187 tv->tv_usec = (suseconds_t)((now - tv->tv_sec) * 1e6);
193 static char *sample_location(int global, const char *file, int line)
196 return bprintf("%s:%d", file, line);
198 return bprintf("%s:%d:%d", file, line, smpi_process_index());
202 int smpi_sample_1(int global, const char *file, int line, int iters, double threshold)
204 char *loc = sample_location(global, file, line);
207 smpi_bench_end(); /* Take time from previous MPI call into account */
209 samples = xbt_dict_new();
211 data = xbt_dict_get_or_null(samples, loc);
213 data = (local_data_t *) xbt_new(local_data_t, 1);
216 data->sum_pow2 = 0.0;
218 data->threshold = threshold;
220 xbt_dict_set(samples, loc, data, &free);
227 int smpi_sample_2(int global, const char *file, int line)
229 char *loc = sample_location(global, file, line);
232 xbt_assert(samples, "You did something very inconsistent, didn't you?");
233 data = xbt_dict_get_or_null(samples, loc);
235 xbt_assert(data, "Please, do thing in order");
237 if (!data->started) {
238 if ((data->iters > 0 && data->count >= data->iters)
239 || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
240 XBT_DEBUG("Perform some wait of %f", data->mean);
241 smpi_execute(data->mean);
251 smpi_process_simulated_start();
252 return data->started;
255 void smpi_sample_3(int global, const char *file, int line)
257 char *loc = sample_location(global, file, line);
261 xbt_assert(samples, "You did something very inconsistent, didn't you?");
262 data = xbt_dict_get_or_null(samples, loc);
264 if(data && data->started && data->count < data->iters) {
265 sample = smpi_process_simulated_elapsed();
267 data->sum_pow2 += sample * sample;
268 n = (double)data->count;
269 data->mean = data->sum / n;
270 data->relstderr = sqrt((data->sum_pow2 / n - data->mean * data->mean) / n) / data->mean;
271 XBT_DEBUG("Average mean after %d steps is %f, relative standard error is %f (sample was %f)", data->count,
272 data->mean, data->relstderr, sample);
277 void smpi_sample_flops(double flops)
279 smpi_execute_flops(flops);
282 void *smpi_shared_malloc(size_t size, const char *file, int line)
284 char *loc = bprintf("%zu_%s_%d", (size_t)getpid(), file, line);
285 size_t len = strlen(loc);
291 for(i = 0; i < len; i++) {
292 /* Make the 'loc' ID be a flat filename */
298 allocs = xbt_dict_new();
300 data = xbt_dict_get_or_null(allocs, loc);
302 fd = shm_open(loc, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
306 xbt_die("Please cleanup /dev/shm/%s", loc);
308 xbt_die("An unhandled error occured while opening %s: %s", loc, strerror(errno));
311 data = xbt_new(shared_data_t, 1);
315 mem = shm_map(fd, size, data);
316 if(shm_unlink(loc) < 0) {
317 XBT_WARN("Could not early unlink %s: %s", loc, strerror(errno));
319 xbt_dict_set(allocs, loc, data, &free);
320 XBT_DEBUG("Mapping %s at %p through %d", loc, mem, fd);
322 mem = shm_map(data->fd, size, data);
325 XBT_DEBUG("Malloc %zu in %p (metadata at %p)", size, mem, data);
329 void smpi_shared_free(void *ptr)
331 char loc[PTR_STRLEN];
332 shared_metadata_t* meta;
336 XBT_WARN("Cannot free: nothing was allocated");
339 if(!allocs_metadata) {
340 XBT_WARN("Cannot free: no metadata was allocated");
342 snprintf(loc, PTR_STRLEN, "%p", ptr);
343 meta = (shared_metadata_t*)xbt_dict_get_or_null(allocs_metadata, loc);
345 XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
350 XBT_WARN("Cannot free: something is broken in the metadata link");
353 if(munmap(ptr, meta->size) < 0) {
354 XBT_WARN("Unmapping of fd %d failed: %s", data->fd, strerror(errno));
357 if (data->count <= 0) {
359 xbt_dict_remove(allocs, data->loc);
364 int smpi_shared_known_call(const char* func, const char* input) {
365 char* loc = bprintf("%s:%s", func, input);
370 calls = xbt_dict_new();
373 xbt_dict_get(calls, loc); /* Succeed or throw */
377 if(ex.category == not_found_error) {
388 void* smpi_shared_get_call(const char* func, const char* input) {
389 char* loc = bprintf("%s:%s", func, input);
393 calls = xbt_dict_new();
395 data = xbt_dict_get(calls, loc);
400 void* smpi_shared_set_call(const char* func, const char* input, void* data) {
401 char* loc = bprintf("%s:%s", func, input);
404 calls = xbt_dict_new();
406 xbt_dict_set(calls, loc, data, NULL);