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"
10 #include "surf/surf.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
13 "Logging specific to SMPI (benchmarking)");
15 xbt_dict_t allocs = NULL; /* Allocated on first use */
16 xbt_dict_t samples = NULL; /* Allocated on first use */
34 void smpi_bench_destroy(void)
37 xbt_dict_free(&allocs);
40 xbt_dict_free(&samples);
44 static void smpi_execute_flops(double flops)
48 host = SIMIX_host_self();
50 XBT_DEBUG("Handle real computation time: %f flops", flops);
51 action = SIMIX_req_host_execute("computation", host, flops, 1);
53 SIMIX_req_set_category (action, TRACE_internal_smpi_get_category());
55 SIMIX_req_host_execution_wait(action);
58 static void smpi_execute(double duration)
60 if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
61 XBT_DEBUG("Sleep for %f to handle real computation time", duration);
62 smpi_execute_flops(duration *
63 xbt_cfg_get_double(_surf_cfg_set,
64 "smpi/running_power"));
68 void smpi_bench_begin(void)
70 xbt_os_timer_start(smpi_process_timer());
73 void smpi_bench_end(void)
75 xbt_os_timer_t timer = smpi_process_timer();
77 xbt_os_timer_stop(timer);
78 smpi_execute(xbt_os_timer_elapsed(timer));
81 unsigned int smpi_sleep(unsigned int secs)
83 smpi_execute((double) secs);
87 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
89 double now = SIMIX_get_clock();
92 tv->tv_sec = (time_t) now;
93 tv->tv_usec = (suseconds_t) (now * 1e6);
98 static char *sample_location(int global, const char *file, int line)
101 return bprintf("%s:%d", file, line);
103 return bprintf("%s:%d:%d", file, line, smpi_process_index());
107 void smpi_sample_1(int global, const char *file, int line, int iters, double threshold)
109 char *loc = sample_location(global, file, line);
112 smpi_bench_end(); /* Take time from previous MPI call into account */
114 samples = xbt_dict_new();
116 data = xbt_dict_get_or_null(samples, loc);
118 data = (local_data_t *) xbt_new(local_data_t, 1);
121 data->sum_pow2 = 0.0;
123 data->threshold = threshold;
125 xbt_dict_set(samples, loc, data, &free);
130 int smpi_sample_2(int global, const char *file, int line)
132 char *loc = sample_location(global, file, line);
135 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
136 data = xbt_dict_get_or_null(samples, loc);
138 xbt_assert0(data, "Please, do thing in order");
140 if (!data->started) {
141 if ((data->iters > 0 && data->count >= data->iters)
142 || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
143 XBT_DEBUG("Perform some wait of %f", data->mean);
144 smpi_execute(data->mean);
154 smpi_process_simulated_start();
155 return data->started;
158 void smpi_sample_3(int global, const char *file, int line)
160 char *loc = sample_location(global, file, line);
164 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
165 data = xbt_dict_get_or_null(samples, loc);
166 if (!data || !data->started || data->count >= data->iters) {
167 xbt_assert0(data, "Please, do thing in order");
170 sample = smpi_process_simulated_elapsed();
172 data->sum_pow2 += sample * sample;
173 n = (double)data->count;
174 data->mean = data->sum / n;
175 data->relstderr = sqrt((data->sum_pow2 / n - data->mean * data->mean) / n) / data->mean;
176 XBT_DEBUG("Average mean after %d steps is %f, relative standard error is %f (sample was %f)", data->count,
177 data->mean, data->relstderr, sample);
180 void smpi_sample_flops(double flops)
182 smpi_execute_flops(flops);
185 void *smpi_shared_malloc(size_t size, const char *file, int line)
187 char *loc = bprintf("%s:%d:%zu", file, line, size);
191 allocs = xbt_dict_new();
193 data = xbt_dict_get_or_null(allocs, loc);
195 data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
197 xbt_dict_set(allocs, loc, data, &free);
205 void smpi_shared_free(void *ptr)
207 shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
211 XBT_WARN("Cannot free: nothing was allocated");
214 loc = xbt_dict_get_key(allocs, data);
216 XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
220 if (data->count <= 0) {
221 xbt_dict_remove(allocs, loc);