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 */
30 void smpi_bench_destroy(void)
33 xbt_dict_free(&allocs);
36 xbt_dict_free(&samples);
40 static void smpi_execute_flops(double flops)
44 host = SIMIX_host_self();
46 DEBUG1("Handle real computation time: %f flops", flops);
47 action = SIMIX_req_host_execute("computation", host, flops);
49 SIMIX_req_set_category (action, TRACE_internal_smpi_get_category());
51 SIMIX_req_host_execution_wait(action);
52 SIMIX_req_host_execution_destroy(action);
55 static void smpi_execute(double duration)
57 if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
58 DEBUG1("Sleep for %f to handle real computation time", duration);
59 smpi_execute_flops(duration *
60 xbt_cfg_get_double(_surf_cfg_set,
61 "smpi/running_power"));
65 void smpi_bench_begin(void)
67 xbt_os_timer_start(smpi_process_timer());
70 void smpi_bench_end(void)
72 xbt_os_timer_t timer = smpi_process_timer();
74 xbt_os_timer_stop(timer);
75 smpi_execute(xbt_os_timer_elapsed(timer));
78 unsigned int smpi_sleep(unsigned int secs)
80 smpi_execute((double) secs);
84 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
86 double now = SIMIX_get_clock();
89 tv->tv_sec = (time_t) now;
90 tv->tv_usec = (suseconds_t) (now * 1e6);
95 static char *sample_location(int global, const char *file, int line)
98 return bprintf("%s:%d", file, line);
100 return bprintf("%s:%d:%d", file, line, smpi_process_index());
104 void smpi_sample_1(int global, const char *file, int line, int max)
106 char *loc = sample_location(global, file, line);
109 smpi_bench_end(); /* Take time from previous MPI call into account */
111 samples = xbt_dict_new();
113 data = xbt_dict_get_or_null(samples, loc);
115 data = (local_data_t *) xbt_new(local_data_t, 1);
120 xbt_dict_set(samples, loc, data, &free);
125 int smpi_sample_2(int global, const char *file, int line)
127 char *loc = sample_location(global, file, line);
130 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
131 data = xbt_dict_get_or_null(samples, loc);
133 xbt_assert0(data, "Please, do thing in order");
135 if (!data->started) {
136 if (data->count < data->max) {
140 DEBUG1("Perform some wait of %f", data->time / (double) data->count);
141 smpi_execute(data->time / (double) data->count);
148 smpi_process_simulated_start();
149 return data->started;
152 void smpi_sample_3(int global, const char *file, int line)
154 char *loc = sample_location(global, file, line);
157 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
158 data = xbt_dict_get_or_null(samples, loc);
159 if (!data || !data->started || data->count >= data->max) {
160 xbt_assert0(data, "Please, do thing in order");
163 data->time += smpi_process_simulated_elapsed();
164 DEBUG2("Average mean after %d steps is %f", data->count,
165 data->time / (double) data->count);
168 void smpi_sample_flops(double flops)
170 smpi_execute_flops(flops);
173 void *smpi_shared_malloc(size_t size, const char *file, int line)
175 char *loc = bprintf("%s:%d:%zu", file, line, size);
179 allocs = xbt_dict_new();
181 data = xbt_dict_get_or_null(allocs, loc);
183 data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
185 xbt_dict_set(allocs, loc, data, &free);
193 void smpi_shared_free(void *ptr)
195 shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
199 WARN0("Cannot free: nothing was allocated");
202 loc = xbt_dict_get_key(allocs, data);
204 WARN1("Cannot free: %p was not shared-allocated by SMPI", ptr);
208 if (data->count <= 0) {
209 xbt_dict_remove(allocs, loc);