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 int 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);
132 int smpi_sample_2(int global, const char *file, int line)
134 char *loc = sample_location(global, file, line);
137 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
138 data = xbt_dict_get_or_null(samples, loc);
140 xbt_assert0(data, "Please, do thing in order");
142 if (!data->started) {
143 if ((data->iters > 0 && data->count >= data->iters)
144 || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
145 XBT_DEBUG("Perform some wait of %f", data->mean);
146 smpi_execute(data->mean);
156 smpi_process_simulated_start();
157 return data->started;
160 void smpi_sample_3(int global, const char *file, int line)
162 char *loc = sample_location(global, file, line);
166 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
167 data = xbt_dict_get_or_null(samples, loc);
169 if(data && data->started && data->count < data->iters) {
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);
182 void smpi_sample_flops(double flops)
184 smpi_execute_flops(flops);
187 void *smpi_shared_malloc(size_t size, const char *file, int line)
189 char *loc = bprintf("%s:%d:%zu", file, line, size);
193 allocs = xbt_dict_new();
195 data = xbt_dict_get_or_null(allocs, loc);
197 data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
199 xbt_dict_set(allocs, loc, data, &free);
207 void smpi_shared_free(void *ptr)
209 shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
213 XBT_WARN("Cannot free: nothing was allocated");
216 loc = xbt_dict_get_key(allocs, data);
218 XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
222 if (data->count <= 0) {
223 xbt_dict_remove(allocs, loc);