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"
11 #include "surf/surf.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
14 "Logging specific to SMPI (benchmarking)");
16 xbt_dict_t allocs = NULL; /* Allocated on first use */
17 xbt_dict_t samples = NULL; /* Allocated on first use */
18 xbt_dict_t calls = NULL; /* Allocated on first use */
19 __thread int smpi_current_rank = 0; /* Updated after each MPI call */
37 void smpi_bench_destroy(void)
40 xbt_dict_free(&allocs);
43 xbt_dict_free(&samples);
46 xbt_dict_free(&calls);
50 static void smpi_execute_flops(double flops)
54 host = SIMIX_host_self();
56 XBT_DEBUG("Handle real computation time: %f flops", flops);
57 action = SIMIX_req_host_execute("computation", host, flops, 1);
59 SIMIX_req_set_category (action, TRACE_internal_smpi_get_category());
61 SIMIX_req_host_execution_wait(action);
64 static void smpi_execute(double duration)
66 if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
67 XBT_DEBUG("Sleep for %f to handle real computation time", duration);
68 smpi_execute_flops(duration *
69 xbt_cfg_get_double(_surf_cfg_set,
70 "smpi/running_power"));
74 void smpi_bench_begin(void)
76 xbt_os_timer_start(smpi_process_timer());
77 smpi_current_rank = smpi_process_index();
80 void smpi_bench_end(void)
82 xbt_os_timer_t timer = smpi_process_timer();
84 xbt_os_timer_stop(timer);
85 smpi_execute(xbt_os_timer_elapsed(timer));
88 unsigned int smpi_sleep(unsigned int secs)
90 smpi_execute((double) secs);
94 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
96 double now = SIMIX_get_clock();
99 tv->tv_sec = (time_t) now;
100 tv->tv_usec = (suseconds_t) (now * 1e6);
105 static char *sample_location(int global, const char *file, int line)
108 return bprintf("%s:%d", file, line);
110 return bprintf("%s:%d:%d", file, line, smpi_process_index());
114 int smpi_sample_1(int global, const char *file, int line, int iters, double threshold)
116 char *loc = sample_location(global, file, line);
119 smpi_bench_end(); /* Take time from previous MPI call into account */
121 samples = xbt_dict_new();
123 data = xbt_dict_get_or_null(samples, loc);
125 data = (local_data_t *) xbt_new(local_data_t, 1);
128 data->sum_pow2 = 0.0;
130 data->threshold = threshold;
132 xbt_dict_set(samples, loc, data, &free);
139 int smpi_sample_2(int global, const char *file, int line)
141 char *loc = sample_location(global, file, line);
144 xbt_assert(samples, "You did something very inconsistent, didn't you?");
145 data = xbt_dict_get_or_null(samples, loc);
147 xbt_assert(data, "Please, do thing in order");
149 if (!data->started) {
150 if ((data->iters > 0 && data->count >= data->iters)
151 || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
152 XBT_DEBUG("Perform some wait of %f", data->mean);
153 smpi_execute(data->mean);
163 smpi_process_simulated_start();
164 return data->started;
167 void smpi_sample_3(int global, const char *file, int line)
169 char *loc = sample_location(global, file, line);
173 xbt_assert(samples, "You did something very inconsistent, didn't you?");
174 data = xbt_dict_get_or_null(samples, loc);
176 if(data && data->started && data->count < data->iters) {
177 sample = smpi_process_simulated_elapsed();
179 data->sum_pow2 += sample * sample;
180 n = (double)data->count;
181 data->mean = data->sum / n;
182 data->relstderr = sqrt((data->sum_pow2 / n - data->mean * data->mean) / n) / data->mean;
183 XBT_DEBUG("Average mean after %d steps is %f, relative standard error is %f (sample was %f)", data->count,
184 data->mean, data->relstderr, sample);
189 void smpi_sample_flops(double flops)
191 smpi_execute_flops(flops);
194 void *smpi_shared_malloc(size_t size, const char *file, int line)
196 char *loc = bprintf("%s:%d:%zu", file, line, size);
200 allocs = xbt_dict_new();
202 data = xbt_dict_get_or_null(allocs, loc);
204 data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
206 xbt_dict_set(allocs, loc, data, &free);
214 void smpi_shared_free(void *ptr)
216 shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
220 XBT_WARN("Cannot free: nothing was allocated");
223 loc = xbt_dict_get_key(allocs, data);
225 XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
229 if (data->count <= 0) {
230 xbt_dict_remove(allocs, loc);
234 int smpi_shared_known_call(const char* func, const char* input) {
235 char* loc = bprintf("%s:%s", func, input);
240 calls = xbt_dict_new();
243 xbt_dict_get(calls, loc); /* Succeed or throw */
246 if(ex.category == not_found_error) {
257 void* smpi_shared_get_call(const char* func, const char* input) {
258 char* loc = bprintf("%s:%s", func, input);
262 calls = xbt_dict_new();
264 data = xbt_dict_get(calls, loc);
269 void* smpi_shared_set_call(const char* func, const char* input, void* data) {
270 char* loc = bprintf("%s:%s", func, input);
273 calls = xbt_dict_new();
275 xbt_dict_set(calls, loc, data, NULL);