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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
12 "Logging specific to SMPI (benchmarking)");
14 xbt_dict_t allocs = NULL; /* Allocated on first use */
15 xbt_dict_t samples = NULL; /* Allocated on first use */
29 void smpi_bench_destroy(void)
32 xbt_dict_free(&allocs);
35 xbt_dict_free(&samples);
39 static void smpi_execute_flops(double flops)
45 e_surf_action_state_t state;
47 host = SIMIX_host_self();
48 mutex = SIMIX_mutex_init();
49 cond = SIMIX_cond_init();
50 DEBUG1("Handle real computation time: %f flops", flops);
51 action = SIMIX_action_execute(host, "computation", flops);
52 SIMIX_mutex_lock(mutex);
53 SIMIX_register_action_to_condition(action, cond);
54 for (state = SIMIX_action_get_state(action);
55 state == SURF_ACTION_READY ||
56 state == SURF_ACTION_RUNNING;
57 state = SIMIX_action_get_state(action)) {
58 SIMIX_cond_wait(cond, mutex);
60 SIMIX_unregister_action_to_condition(action, cond);
61 SIMIX_mutex_unlock(mutex);
62 SIMIX_action_destroy(action);
63 SIMIX_cond_destroy(cond);
64 SIMIX_mutex_destroy(mutex);
67 static void smpi_execute(double duration)
69 if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
70 DEBUG1("Sleep for %f to handle real computation time", duration);
71 smpi_execute_flops(duration *
72 xbt_cfg_get_double(_surf_cfg_set,
73 "smpi/running_power"));
77 void smpi_bench_begin(int rank, const char *mpi_call)
79 if (mpi_call && rank >= 0
80 && xbt_cfg_get_int(_surf_cfg_set, "smpi/log_events")) {
81 INFO3("SMPE: ts=%f rank=%d type=end et=%s", SIMIX_get_clock(), rank,
84 xbt_os_timer_start(smpi_process_timer());
87 void smpi_bench_end(int rank, const char *mpi_call)
89 xbt_os_timer_t timer = smpi_process_timer();
91 xbt_os_timer_stop(timer);
92 smpi_execute(xbt_os_timer_elapsed(timer));
93 if (mpi_call && rank >= 0
94 && xbt_cfg_get_int(_surf_cfg_set, "smpi/log_events")) {
95 INFO3("SMPE: ts=%f rank=%d type=begin et=%s", SIMIX_get_clock(), rank,
100 unsigned int smpi_sleep(unsigned int secs)
102 smpi_execute((double) secs);
106 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
108 double now = SIMIX_get_clock();
111 tv->tv_sec = (time_t) now;
112 tv->tv_usec = (suseconds_t) (now * 1e6);
117 static char *sample_location(int global, const char *file, int line)
120 return bprintf("%s:%d", file, line);
122 return bprintf("%s:%d:%d", file, line, smpi_process_index());
126 void smpi_sample_1(int global, const char *file, int line, int max)
128 char *loc = sample_location(global, file, line);
131 smpi_bench_end(-1, NULL); /* Take time from previous MPI call into account */
133 samples = xbt_dict_new();
135 data = xbt_dict_get_or_null(samples, loc);
137 data = (local_data_t *) xbt_new(local_data_t, 1);
142 xbt_dict_set(samples, loc, data, &free);
147 int smpi_sample_2(int global, const char *file, int line)
149 char *loc = sample_location(global, file, line);
152 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
153 data = xbt_dict_get_or_null(samples, loc);
155 xbt_assert0(data, "Please, do thing in order");
157 if (!data->started) {
158 if (data->count < data->max) {
162 DEBUG1("Perform some wait of %f", data->time / (double) data->count);
163 smpi_execute(data->time / (double) data->count);
169 smpi_bench_begin(-1, NULL);
170 smpi_process_simulated_start();
171 return data->started;
174 void smpi_sample_3(int global, const char *file, int line)
176 char *loc = sample_location(global, file, line);
179 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
180 data = xbt_dict_get_or_null(samples, loc);
181 if (!data || !data->started || data->count >= data->max) {
182 xbt_assert0(data, "Please, do thing in order");
184 smpi_bench_end(-1, NULL);
185 data->time += smpi_process_simulated_elapsed();
186 DEBUG2("Average mean after %d steps is %f", data->count,
187 data->time / (double) data->count);
190 void smpi_sample_flops(double flops)
192 smpi_execute_flops(flops);
195 void *smpi_shared_malloc(size_t size, const char *file, int line)
197 char *loc = bprintf("%s:%d:%zu", file, line, size);
201 allocs = xbt_dict_new();
203 data = xbt_dict_get_or_null(allocs, loc);
205 data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
207 xbt_dict_set(allocs, loc, data, &free);
215 void smpi_shared_free(void *ptr)
217 shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
221 WARN0("Cannot free: nothing was allocated");
224 loc = xbt_dict_get_key(allocs, data);
226 WARN1("Cannot free: %p was not shared-allocated by SMPI", ptr);
230 if (data->count <= 0) {
231 xbt_dict_remove(allocs, loc);