+#include "xbt/dict.h"
+#include "xbt/sysdep.h"
+#include "xbt/ex.h"
+#include "surf/surf.h"
+
+#ifndef WIN32
+#include <sys/mman.h>
+#endif
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <math.h> // sqrt
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
+ "Logging specific to SMPI (benchmarking)");
+
+/* Shared allocations are handled through shared memory segments.
+ * Associated data and metadata are used as follows:
+ *
+ * mmap #1
+ * `allocs' dict ---- -.
+ * ---------- shared_data_t shared_metadata_t / | | |
+ * .->| <name> | ---> -------------------- <--. ----------------- | | | |
+ * | ---------- | fd of <name> | | | size of mmap | --| | | |
+ * | | count (2) | |-- | data | \ | | |
+ * `----------------- | <name> | | ----------------- ---- |
+ * -------------------- | ^ |
+ * | | |
+ * | | `allocs_metadata' dict |
+ * | | ---------------------- |
+ * | `-- | <addr of mmap #1> |<-'
+ * | .-- | <addr of mmap #2> |<-.
+ * | | ---------------------- |
+ * | | |
+ * | | |
+ * | | |
+ * | | mmap #2 |
+ * | v ---- -'
+ * | shared_metadata_t / | |
+ * | ----------------- | | |
+ * | | size of mmap | --| | |
+ * `-- | data | | | |
+ * ----------------- | | |
+ * \ | |
+ * ----
+ */
+
+#define PTR_STRLEN (2 + 2 * sizeof(void*) + 1)
+
+xbt_dict_t allocs = NULL; /* Allocated on first use */
+xbt_dict_t allocs_metadata = NULL; /* Allocated on first use */
+xbt_dict_t samples = NULL; /* Allocated on first use */
+xbt_dict_t calls = NULL; /* Allocated on first use */
+__thread int smpi_current_rank = 0; /* Updated after each MPI call */
+
+typedef struct {
+ int fd;
+ int count;
+ char* loc;
+} shared_data_t;
+
+typedef struct {
+ size_t size;
+ shared_data_t* data;
+} shared_metadata_t;
+
+static size_t shm_size(int fd) {
+ struct stat st;
+
+ if(fstat(fd, &st) < 0) {
+ xbt_die("Could not stat fd %d: %s", fd, strerror(errno));
+ }
+ return (size_t)st.st_size;
+}
+
+#ifndef WIN32
+static void* shm_map(int fd, size_t size, shared_data_t* data) {
+ void* mem;
+ char loc[PTR_STRLEN];
+ shared_metadata_t* meta;
+
+ if(size > shm_size(fd)) {
+ if(ftruncate(fd, (off_t)size) < 0) {
+ xbt_die("Could not truncate fd %d to %zu: %s", fd, size, strerror(errno));
+ }
+ }
+
+ mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if(mem == MAP_FAILED) {
+ xbt_die("Could not map fd %d: %s", fd, strerror(errno));
+ }
+ if(!allocs_metadata) {
+ allocs_metadata = xbt_dict_new();
+ }
+ snprintf(loc, PTR_STRLEN, "%p", mem);
+ meta = xbt_new(shared_metadata_t, 1);
+ meta->size = size;
+ meta->data = data;
+ xbt_dict_set(allocs_metadata, loc, meta, &free);
+ XBT_DEBUG("MMAP %zu to %p", size, mem);
+ return mem;
+}
+#endif
+
+void smpi_bench_destroy(void)
+{
+ xbt_dict_free(&allocs);
+ xbt_dict_free(&samples);
+ xbt_dict_free(&calls);
+}
+
+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 = simcall_host_execute("computation", host, flops, 1);
+#ifdef HAVE_TRACING
+ simcall_set_category (action, TRACE_internal_smpi_get_category());
+#endif
+ simcall_host_execution_wait(action);
+}
+
+static void smpi_execute(double duration)
+{
+ /* FIXME: a global variable would be less expensive to consult than a call to xbt_cfg_get_double() right on the critical path */
+ if (duration >= surf_cfg_get_double("smpi/cpu_threshold")) {
+ XBT_DEBUG("Sleep for %f to handle real computation time", duration);
+ smpi_execute_flops(duration *
+ surf_cfg_get_double("smpi/running_power"));
+ } else {
+ XBT_DEBUG("Real computation took %f while option smpi/cpu_threshold is set to %f => ignore it",
+ duration, surf_cfg_get_double("smpi/cpu_threshold"));
+ }
+}
+
+void smpi_bench_begin(void)
+{
+ xbt_os_timer_start(smpi_process_timer());
+ smpi_current_rank = smpi_process_index();
+}