Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4ecf4ebbb4df1c9c517189d70f8ceaade9e090cf
[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 #include "surf/surf.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
13                                 "Logging specific to SMPI (benchmarking)");
14
15 xbt_dict_t allocs = NULL;       /* Allocated on first use */
16 xbt_dict_t samples = NULL;      /* Allocated on first use */
17
18 typedef struct {
19   int count;
20   char data[];
21 } shared_data_t;
22
23 typedef struct {
24   double time;
25   int count;
26   int max;
27   int started;
28 } local_data_t;
29
30 void smpi_bench_destroy(void)
31 {
32   if (allocs) {
33     xbt_dict_free(&allocs);
34   }
35   if (samples) {
36     xbt_dict_free(&samples);
37   }
38 }
39
40 static void smpi_execute_flops(double flops)
41 {
42   smx_action_t action;
43   smx_host_t host;
44   host = SIMIX_host_self();
45
46   DEBUG1("Handle real computation time: %f flops", flops);
47   action = SIMIX_req_host_execute("computation", host, flops);
48   SIMIX_req_host_execution_wait(action);
49   SIMIX_req_host_execution_destroy(action);
50 }
51
52 static void smpi_execute(double duration)
53 {
54   if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
55     DEBUG1("Sleep for %f to handle real computation time", duration);
56     smpi_execute_flops(duration *
57                        xbt_cfg_get_double(_surf_cfg_set,
58                                           "smpi/running_power"));
59   }
60 }
61
62 void smpi_bench_begin(void)
63 {
64   xbt_os_timer_start(smpi_process_timer());
65 }
66
67 void smpi_bench_end(void)
68 {
69   xbt_os_timer_t timer = smpi_process_timer();
70
71   xbt_os_timer_stop(timer);
72   smpi_execute(xbt_os_timer_elapsed(timer));
73 }
74
75 unsigned int smpi_sleep(unsigned int secs)
76 {
77   smpi_execute((double) secs);
78   return secs;
79 }
80
81 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
82 {
83   double now = SIMIX_get_clock();
84
85   if (tv) {
86     tv->tv_sec = (time_t) now;
87     tv->tv_usec = (suseconds_t) (now * 1e6);
88   }
89   return 0;
90 }
91
92 static char *sample_location(int global, const char *file, int line)
93 {
94   if (global) {
95     return bprintf("%s:%d", file, line);
96   } else {
97     return bprintf("%s:%d:%d", file, line, smpi_process_index());
98   }
99 }
100
101 void smpi_sample_1(int global, const char *file, int line, int max)
102 {
103   char *loc = sample_location(global, file, line);
104   local_data_t *data;
105
106   smpi_bench_end();     /* Take time from previous MPI call into account */
107   if (!samples) {
108     samples = xbt_dict_new();
109   }
110   data = xbt_dict_get_or_null(samples, loc);
111   if (!data) {
112     data = (local_data_t *) xbt_new(local_data_t, 1);
113     data->time = 0.0;
114     data->count = 0;
115     data->max = max;
116     data->started = 0;
117     xbt_dict_set(samples, loc, data, &free);
118   }
119   free(loc);
120 }
121
122 int smpi_sample_2(int global, const char *file, int line)
123 {
124   char *loc = sample_location(global, file, line);
125   local_data_t *data;
126
127   xbt_assert0(samples, "You did something very inconsistent, didn't you?");
128   data = xbt_dict_get_or_null(samples, loc);
129   if (!data) {
130     xbt_assert0(data, "Please, do thing in order");
131   }
132   if (!data->started) {
133     if (data->count < data->max) {
134       data->started = 1;
135       data->count++;
136     } else {
137       DEBUG1("Perform some wait of %f", data->time / (double) data->count);
138       smpi_execute(data->time / (double) data->count);
139     }
140   } else {
141     data->started = 0;
142   }
143   free(loc);
144   smpi_bench_begin();
145   smpi_process_simulated_start();
146   return data->started;
147 }
148
149 void smpi_sample_3(int global, const char *file, int line)
150 {
151   char *loc = sample_location(global, file, line);
152   local_data_t *data;
153
154   xbt_assert0(samples, "You did something very inconsistent, didn't you?");
155   data = xbt_dict_get_or_null(samples, loc);
156   if (!data || !data->started || data->count >= data->max) {
157     xbt_assert0(data, "Please, do thing in order");
158   }
159   smpi_bench_end();
160   data->time += smpi_process_simulated_elapsed();
161   DEBUG2("Average mean after %d steps is %f", data->count,
162          data->time / (double) data->count);
163 }
164
165 void smpi_sample_flops(double flops)
166 {
167   smpi_execute_flops(flops);
168 }
169
170 void *smpi_shared_malloc(size_t size, const char *file, int line)
171 {
172   char *loc = bprintf("%s:%d:%zu", file, line, size);
173   shared_data_t *data;
174
175   if (!allocs) {
176     allocs = xbt_dict_new();
177   }
178   data = xbt_dict_get_or_null(allocs, loc);
179   if (!data) {
180     data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
181     data->count = 1;
182     xbt_dict_set(allocs, loc, data, &free);
183   } else {
184     data->count++;
185   }
186   free(loc);
187   return data->data;
188 }
189
190 void smpi_shared_free(void *ptr)
191 {
192   shared_data_t *data = (shared_data_t *) ((int *) ptr - 1);
193   char *loc;
194
195   if (!allocs) {
196     WARN0("Cannot free: nothing was allocated");
197     return;
198   }
199   loc = xbt_dict_get_key(allocs, data);
200   if (!loc) {
201     WARN1("Cannot free: %p was not shared-allocated by SMPI", ptr);
202     return;
203   }
204   data->count--;
205   if (data->count <= 0) {
206     xbt_dict_remove(allocs, loc);
207   }
208 }