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)
43 host = SIMIX_host_self();
45 DEBUG1("Handle real computation time: %f flops", flops);
46 action = SIMIX_req_host_execute("computation", host, flops);
47 SIMIX_req_host_execution_wait(action);
48 SIMIX_req_host_execution_destroy(action);
51 static void smpi_execute(double duration)
53 if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
54 DEBUG1("Sleep for %f to handle real computation time", duration);
55 smpi_execute_flops(duration *
56 xbt_cfg_get_double(_surf_cfg_set,
57 "smpi/running_power"));
61 void smpi_bench_begin(void)
63 xbt_os_timer_start(smpi_process_timer());
66 void smpi_bench_end(void)
68 xbt_os_timer_t timer = smpi_process_timer();
70 xbt_os_timer_stop(timer);
71 smpi_execute(xbt_os_timer_elapsed(timer));
74 unsigned int smpi_sleep(unsigned int secs)
76 smpi_execute((double) secs);
80 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
82 double now = SIMIX_get_clock();
85 tv->tv_sec = (time_t) now;
86 tv->tv_usec = (suseconds_t) (now * 1e6);
91 static char *sample_location(int global, const char *file, int line)
94 return bprintf("%s:%d", file, line);
96 return bprintf("%s:%d:%d", file, line, smpi_process_index());
100 void smpi_sample_1(int global, const char *file, int line, int max)
102 char *loc = sample_location(global, file, line);
105 smpi_bench_end(); /* Take time from previous MPI call into account */
107 samples = xbt_dict_new();
109 data = xbt_dict_get_or_null(samples, loc);
111 data = (local_data_t *) xbt_new(local_data_t, 1);
116 xbt_dict_set(samples, loc, data, &free);
121 int smpi_sample_2(int global, const char *file, int line)
123 char *loc = sample_location(global, file, line);
126 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
127 data = xbt_dict_get_or_null(samples, loc);
129 xbt_assert0(data, "Please, do thing in order");
131 if (!data->started) {
132 if (data->count < data->max) {
136 DEBUG1("Perform some wait of %f", data->time / (double) data->count);
137 smpi_execute(data->time / (double) data->count);
144 smpi_process_simulated_start();
145 return data->started;
148 void smpi_sample_3(int global, const char *file, int line)
150 char *loc = sample_location(global, file, line);
153 xbt_assert0(samples, "You did something very inconsistent, didn't you?");
154 data = xbt_dict_get_or_null(samples, loc);
155 if (!data || !data->started || data->count >= data->max) {
156 xbt_assert0(data, "Please, do thing in order");
159 data->time += smpi_process_simulated_elapsed();
160 DEBUG2("Average mean after %d steps is %f", data->count,
161 data->time / (double) data->count);
164 void smpi_sample_flops(double flops)
166 smpi_execute_flops(flops);
169 void *smpi_shared_malloc(size_t size, const char *file, int line)
171 char *loc = bprintf("%s:%d:%zu", file, line, size);
175 allocs = xbt_dict_new();
177 data = xbt_dict_get_or_null(allocs, loc);
179 data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
181 xbt_dict_set(allocs, loc, data, &free);
189 void smpi_shared_free(void *ptr)
191 shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
195 WARN0("Cannot free: nothing was allocated");
198 loc = xbt_dict_get_key(allocs, data);
200 WARN1("Cannot free: %p was not shared-allocated by SMPI", ptr);
204 if (data->count <= 0) {
205 xbt_dict_remove(allocs, loc);