Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Squeeze any block with a given delay (in flops).
[simgrid.git] / src / smpi / smpi_bench.c
1 /* Copyright (c) 2007, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "private.h"
8 #include "xbt/dict.h"
9 #include "xbt/sysdep.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
12                                 "Logging specific to SMPI (benchmarking)");
13
14 xbt_dict_t allocs = NULL; /* Allocated on first use */
15 xbt_dict_t samples = NULL; /* Allocated on first use */
16
17 typedef struct {
18    int count;
19    char data[];
20 } shared_data_t;
21
22 typedef struct {
23    double time;
24    int count;
25    int max;
26    int started;
27 } local_data_t;
28
29 void smpi_bench_destroy(void) {
30    if (allocs) {
31       xbt_dict_free(&allocs);
32    }
33    if (samples) {
34       xbt_dict_free(&samples);
35    }
36 }
37
38 static void smpi_execute_flops(double flops) {
39   smx_host_t host;
40   smx_action_t action;
41   smx_mutex_t mutex;
42   smx_cond_t cond;
43   e_surf_action_state_t state;
44
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);
56   }
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);
62 }
63
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"));
68   }
69 }
70
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);
74   }
75   xbt_os_timer_start(smpi_process_timer());
76 }
77
78 void smpi_bench_end(int rank, const char* mpi_call) {
79   xbt_os_timer_t timer = smpi_process_timer();
80
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);
85   }
86 }
87
88 unsigned int smpi_sleep(unsigned int secs) {
89    smpi_execute((double)secs);
90    return secs;
91 }
92
93 int smpi_gettimeofday(struct timeval* tv, struct timezone* tz) {
94    double now = SIMIX_get_clock();
95
96    if(tv) {
97       tv->tv_sec = (time_t)now;
98       tv->tv_usec = (suseconds_t)(now * 1e6);
99    }
100    return 0;
101 }
102
103 static char* sample_location(int global, const char* file, int line) {
104    if(global) {
105       return bprintf("%s:%d", file, line);
106    } else {
107       return bprintf("%s:%d:%d", file, line, smpi_process_index());
108    }
109 }
110
111 void smpi_sample_1(int global, const char* file, int line, int max) {
112    char* loc = sample_location(global, file, line);
113    local_data_t* data;
114
115    smpi_bench_end(-1, NULL); /* Take time from previous MPI call into account */
116    if (!samples) {
117       samples = xbt_dict_new();
118    }
119    data = xbt_dict_get_or_null(samples, loc);
120    if (!data) {
121       data = (local_data_t*)xbt_new(local_data_t, 1);
122       data->time = 0.0;
123       data->count = 0;
124       data->max = max;
125       data->started = 0;
126       xbt_dict_set(samples, loc, data, &free);
127    }
128    free(loc);
129 }
130
131 int smpi_sample_2(int global, const char* file, int line) {
132    char* loc = sample_location(global, file, line);
133    local_data_t* data;
134
135    xbt_assert0(samples, "You did something very inconsistent, didn't you?");
136    data = xbt_dict_get_or_null(samples, loc);
137    if (!data) {
138       xbt_assert0(data, "Please, do thing in order");
139    }
140    if (!data->started) {
141       if (data->count < data->max) {
142          data->started = 1;
143          data->count++;
144       } else {
145          DEBUG1("Perform some wait of %f", data->time / (double)data->count);
146          smpi_execute(data->time / (double)data->count);
147       }
148    } else {
149       data->started = 0;
150    }
151    free(loc);
152    smpi_bench_begin(-1, NULL);
153    smpi_process_simulated_start();
154    return data->started;
155 }
156
157 void smpi_sample_3(int global, const char* file, int line) {
158    char* loc = sample_location(global, file, line);
159    local_data_t* data;
160
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");
165    }
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);
169 }
170
171 void smpi_sample_flops(double flops) {
172    smpi_execute_flops(flops);
173 }
174
175 void* smpi_shared_malloc(size_t size, const char* file, int line) {
176    char* loc = bprintf("%s:%d:%zu", file, line, size);
177    shared_data_t* data;
178
179    if (!allocs) {
180       allocs = xbt_dict_new();
181    }
182    data = xbt_dict_get_or_null(allocs, loc);
183    if (!data) {
184       data = (shared_data_t*)xbt_malloc0(sizeof(int) + size);
185       data->count = 1;
186       xbt_dict_set(allocs, loc, data, &free);
187    } else {
188       data->count++;
189    }
190    free(loc);
191    return data->data;
192 }
193
194 void smpi_shared_free(void* ptr) {
195    shared_data_t* data = (shared_data_t*)((int*)ptr - 1);
196    char* loc;
197
198    if (!allocs) {
199       WARN0("Cannot free: nothing was allocated");
200       return;
201    }
202    loc = xbt_dict_get_key(allocs, data);
203    if (!loc) {
204       WARN1("Cannot free: %p was not shared-allocated by SMPI", ptr);
205       return;
206    }
207    data->count--;
208    if (data->count <= 0) {
209       xbt_dict_remove(allocs, loc);
210    }
211 }