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 DEBUG1("Handle real computation time: %f flops", flops);
51 action = SIMIX_req_host_execute("computation", host, flops);
53 SIMIX_req_set_category (action, TRACE_internal_smpi_get_category());
55 SIMIX_req_host_execution_wait(action);
56 SIMIX_req_host_execution_destroy(action);
59 static void smpi_execute(double duration)
61 if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
62 DEBUG1("Sleep for %f to handle real computation time", duration);
63 smpi_execute_flops(duration *
64 xbt_cfg_get_double(_surf_cfg_set,
65 "smpi/running_power"));
69 void smpi_bench_begin(void)
71 xbt_os_timer_start(smpi_process_timer());
74 void smpi_bench_end(void)
76 xbt_os_timer_t timer = smpi_process_timer();
78 xbt_os_timer_stop(timer);
79 smpi_execute(xbt_os_timer_elapsed(timer));
82 unsigned int smpi_sleep(unsigned int secs)
84 smpi_execute((double) secs);
88 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
90 double now = SIMIX_get_clock();
93 tv->tv_sec = (time_t) now;
94 tv->tv_usec = (suseconds_t) (now * 1e6);
99 static char *sample_location(int global, const char *file, int line)
102 return bprintf("%s:%d", file, line);
104 return bprintf("%s:%d:%d", file, line, smpi_process_index());
108 void smpi_sample_1(int global, const char *file, int line, int iters, double threshold)
110 char *loc = sample_location(global, file, line);
113 smpi_bench_end(); /* Take time from previous MPI call into account */
115 samples = xbt_dict_new();
117 data = xbt_dict_get_or_null(samples, loc);
119 data = (local_data_t *) xbt_new(local_data_t, 1);
122 data->sum_pow2 = 0.0;
124 data->threshold = threshold;
126 xbt_dict_set(samples, loc, data, &free);
131 int smpi_sample_2(int global, const char *file, int line)
133 char *loc = sample_location(global, file, line);
136 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
137 data = xbt_dict_get_or_null(samples, loc);
139 xbt_assert0(data, "Please, do thing in order");
141 if (!data->started) {
142 if ((data->iters > 0 && data->count >= data->iters)
143 || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
144 DEBUG1("Perform some wait of %f", data->mean);
145 smpi_execute(data->mean);
155 smpi_process_simulated_start();
156 return data->started;
159 void smpi_sample_3(int global, const char *file, int line)
161 char *loc = sample_location(global, file, line);
165 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
166 data = xbt_dict_get_or_null(samples, loc);
167 if (!data || !data->started || data->count >= data->iters) {
168 xbt_assert0(data, "Please, do thing in order");
171 sample = smpi_process_simulated_elapsed();
173 data->sum_pow2 += sample * sample;
174 n = (double)data->count;
175 data->mean = data->sum / n;
176 data->relstderr = sqrt((data->sum_pow2 / n - data->mean * data->mean) / n) / data->mean;
177 DEBUG4("Average mean after %d steps is %f, relative standard error is %f (sample was %f)", data->count,
178 data->mean, data->relstderr, sample);
181 void smpi_sample_flops(double flops)
183 smpi_execute_flops(flops);
186 void *smpi_shared_malloc(size_t size, const char *file, int line)
188 char *loc = bprintf("%s:%d:%zu", file, line, size);
192 allocs = xbt_dict_new();
194 data = xbt_dict_get_or_null(allocs, loc);
196 data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
198 xbt_dict_set(allocs, loc, data, &free);
206 void smpi_shared_free(void *ptr)
208 shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
212 WARN0("Cannot free: nothing was allocated");
215 loc = xbt_dict_get_key(allocs, data);
217 WARN1("Cannot free: %p was not shared-allocated by SMPI", ptr);
221 if (data->count <= 0) {
222 xbt_dict_remove(allocs, loc);