+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;
+
+ smpi_bench_end(); /* Take time from previous MPI call into account */
+ if (!samples) {
+ samples = xbt_dict_new();
+ }
+ data = xbt_dict_get_or_null(samples, loc);
+ if (!data) {
+ data = (local_data_t *) xbt_new(local_data_t, 1);
+ data->count = 0;
+ data->sum = 0.0;
+ data->sum_pow2 = 0.0;
+ data->iters = iters;
+ data->threshold = threshold;
+ data->started = 0;
+ xbt_dict_set(samples, loc, data, &free);
+ return 0;
+ }
+ free(loc);
+ return 1;
+}
+
+int smpi_sample_2(int global, const char *file, int line)
+{
+ char *loc = sample_location(global, file, line);
+ local_data_t *data;
+
+ xbt_assert(samples, "You did something very inconsistent, didn't you?");
+ data = xbt_dict_get_or_null(samples, loc);
+ if (!data) {
+ xbt_assert(data, "Please, do thing in order");
+ }
+ if (!data->started) {
+ if ((data->iters > 0 && data->count >= data->iters)
+ || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
+ XBT_DEBUG("Perform some wait of %f", data->mean);
+ smpi_execute(data->mean);
+ } else {
+ data->started = 1;
+ data->count++;
+ }
+ } else {
+ data->started = 0;
+ }
+ free(loc);
+ smpi_bench_begin();
+ smpi_process_simulated_start();
+ return data->started;
+}
+
+void smpi_sample_3(int global, const char *file, int line)
+{
+ char *loc = sample_location(global, file, line);
+ local_data_t *data;
+ double sample, n;
+
+ xbt_assert(samples, "You did something very inconsistent, didn't you?");
+ data = xbt_dict_get_or_null(samples, loc);
+ smpi_bench_end();
+ if(data && data->started && data->count < data->iters) {
+ sample = smpi_process_simulated_elapsed();
+ data->sum += sample;
+ data->sum_pow2 += sample * sample;
+ n = (double)data->count;
+ data->mean = data->sum / n;
+ data->relstderr = sqrt((data->sum_pow2 / n - data->mean * data->mean) / n) / data->mean;
+ XBT_DEBUG("Average mean after %d steps is %f, relative standard error is %f (sample was %f)", data->count,
+ data->mean, data->relstderr, sample);
+ }
+ free(loc);
+}
+
+void smpi_sample_flops(double flops)
+{
+ smpi_execute_flops(flops);
+}
+
+void *smpi_shared_malloc(size_t size, const char *file, int line)
+{
+ char *loc = bprintf("%s:%d:%zu", file, line, size);
+ shared_data_t *data;
+
+ if (!allocs) {
+ allocs = xbt_dict_new();
+ }
+ data = xbt_dict_get_or_null(allocs, loc);
+ if (!data) {
+ data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
+ data->count = 1;
+ xbt_dict_set(allocs, loc, data, &free);
+ } else {
+ data->count++;
+ }
+ free(loc);
+ return data->data;
+}
+
+void smpi_shared_free(void *ptr)
+{
+ shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
+ char *loc;