+#include "xbt/dict.h"
+#include "xbt/sysdep.h"
+#include "xbt/ex.h"
+#include "surf/surf.h"
+
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
+ "Logging specific to SMPI (benchmarking)");
+
+xbt_dict_t allocs = NULL; /* Allocated on first use */
+xbt_dict_t samples = NULL; /* Allocated on first use */
+xbt_dict_t calls = NULL; /* Allocated on first use */
+
+typedef struct {
+ int count;
+ char data[];
+} shared_data_t;
+
+typedef struct {
+ int count;
+ double sum;
+ double sum_pow2;
+ double mean;
+ double relstderr;
+ int iters;
+ double threshold;
+ int started;
+} local_data_t;
+
+void smpi_bench_destroy(void)
+{
+ if (allocs) {
+ xbt_dict_free(&allocs);
+ }
+ if (samples) {
+ xbt_dict_free(&samples);
+ }
+ if(calls) {
+ xbt_dict_free(&calls);
+ }
+}
+
+static void smpi_execute_flops(double flops)
+{
+ smx_action_t action;
+ smx_host_t host;
+ host = SIMIX_host_self();
+
+ XBT_DEBUG("Handle real computation time: %f flops", flops);
+ action = SIMIX_req_host_execute("computation", host, flops, 1);
+#ifdef HAVE_TRACING
+ SIMIX_req_set_category (action, TRACE_internal_smpi_get_category());
+#endif
+ SIMIX_req_host_execution_wait(action);
+}
+
+static void smpi_execute(double duration)
+{
+ if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
+ XBT_DEBUG("Sleep for %f to handle real computation time", duration);
+ smpi_execute_flops(duration *
+ xbt_cfg_get_double(_surf_cfg_set,
+ "smpi/running_power"));
+ }
+}
+
+void smpi_bench_begin(void)
+{
+ xbt_os_timer_start(smpi_process_timer());
+}
+
+void smpi_bench_end(void)
+{
+ xbt_os_timer_t timer = smpi_process_timer();
+
+ xbt_os_timer_stop(timer);
+ smpi_execute(xbt_os_timer_elapsed(timer));
+}
+
+unsigned int smpi_sleep(unsigned int secs)
+{
+ smpi_execute((double) secs);
+ return secs;
+}
+
+int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+ double now = SIMIX_get_clock();
+
+ if (tv) {
+ tv->tv_sec = (time_t) now;
+ tv->tv_usec = (suseconds_t) (now * 1e6);
+ }
+ return 0;
+}
+
+static char *sample_location(int global, const char *file, int line)
+{
+ if (global) {
+ return bprintf("%s:%d", file, line);
+ } else {
+ return bprintf("%s:%d:%d", file, line, smpi_process_index());
+ }
+}
+
+int smpi_sample_1(int global, const char *file, int line, int iters, double threshold)
+{
+ char *loc = sample_location(global, file, line);
+ local_data_t *data;