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) {
31 xbt_dict_free(&allocs);
34 xbt_dict_free(&samples);
38 static void smpi_execute_flops(double flops) {
43 e_surf_action_state_t state;
45 host = SIMIX_host_self();
46 mutex = SIMIX_mutex_init();
47 cond = SIMIX_cond_init();
48 DEBUG1("Handle real computation time: %f flops", flops);
49 action = SIMIX_action_execute(host, "computation", flops);
50 SIMIX_mutex_lock(mutex);
51 SIMIX_register_action_to_condition(action, cond);
52 for(state = SIMIX_action_get_state(action);
53 state == SURF_ACTION_READY ||
54 state == SURF_ACTION_RUNNING; state = SIMIX_action_get_state(action)) {
55 SIMIX_cond_wait(cond, mutex);
57 SIMIX_unregister_action_to_condition(action, cond);
58 SIMIX_mutex_unlock(mutex);
59 SIMIX_action_destroy(action);
60 SIMIX_cond_destroy(cond);
61 SIMIX_mutex_destroy(mutex);
64 static void smpi_execute(double duration) {
65 if(duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
66 DEBUG1("Sleep for %f to handle real computation time", duration);
67 smpi_execute_flops(duration * xbt_cfg_get_double(_surf_cfg_set, "smpi/running_power"));
71 void smpi_bench_begin(int rank, const char* mpi_call) {
72 if(mpi_call && rank >= 0 && xbt_cfg_get_int(_surf_cfg_set, "smpi/log_events")) {
73 INFO3("SMPE: ts=%f rank=%d type=end et=%s", SIMIX_get_clock(), rank, mpi_call);
75 xbt_os_timer_start(smpi_process_timer());
78 void smpi_bench_end(int rank, const char* mpi_call) {
79 xbt_os_timer_t timer = smpi_process_timer();
81 xbt_os_timer_stop(timer);
82 smpi_execute(xbt_os_timer_elapsed(timer));
83 if(mpi_call && rank >= 0 && xbt_cfg_get_int(_surf_cfg_set, "smpi/log_events")) {
84 INFO3("SMPE: ts=%f rank=%d type=begin et=%s", SIMIX_get_clock(), rank, mpi_call);
88 unsigned int smpi_sleep(unsigned int secs) {
89 smpi_execute((double)secs);
93 int smpi_gettimeofday(struct timeval* tv, struct timezone* tz) {
94 double now = SIMIX_get_clock();
97 tv->tv_sec = (time_t)now;
98 tv->tv_usec = (suseconds_t)(now * 1e6);
103 static char* sample_location(int global, const char* file, int line) {
105 return bprintf("%s:%d", file, line);
107 return bprintf("%s:%d:%d", file, line, smpi_process_index());
111 void smpi_sample_1(int global, const char* file, int line, int max) {
112 char* loc = sample_location(global, file, line);
115 smpi_bench_end(-1, NULL); /* Take time from previous MPI call into account */
117 samples = xbt_dict_new();
119 data = xbt_dict_get_or_null(samples, loc);
121 data = (local_data_t*)xbt_new(local_data_t, 1);
126 xbt_dict_set(samples, loc, data, &free);
131 int smpi_sample_2(int global, const char* file, int line) {
132 char* loc = sample_location(global, file, line);
135 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
136 data = xbt_dict_get_or_null(samples, loc);
138 xbt_assert0(data, "Please, do thing in order");
140 if (!data->started) {
141 if (data->count < data->max) {
145 DEBUG1("Perform some wait of %f", data->time / (double)data->count);
146 smpi_execute(data->time / (double)data->count);
152 smpi_bench_begin(-1, NULL);
153 smpi_process_simulated_start();
154 return data->started;
157 void smpi_sample_3(int global, const char* file, int line) {
158 char* loc = sample_location(global, file, line);
161 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
162 data = xbt_dict_get_or_null(samples, loc);
163 if (!data || !data->started || data->count >= data->max) {
164 xbt_assert0(data, "Please, do thing in order");
166 smpi_bench_end(-1, NULL);
167 data->time += smpi_process_simulated_elapsed();
168 DEBUG2("Average mean after %d steps is %f", data->count, data->time / (double)data->count);
171 void smpi_sample_flops(double flops) {
172 smpi_execute_flops(flops);
175 void* smpi_shared_malloc(size_t size, const char* file, int line) {
176 char* loc = bprintf("%s:%d:%zu", file, line, size);
180 allocs = xbt_dict_new();
182 data = xbt_dict_get_or_null(allocs, loc);
184 data = (shared_data_t*)xbt_malloc0(sizeof(int) + size);
186 xbt_dict_set(allocs, loc, data, &free);
194 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);