]> AND Private Git Repository - loba.git/blob - process.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Add option "-t" for fixing a time limit on the simulation.
[loba.git] / process.cpp
1 #include <algorithm>
2 #include <tr1/functional>
3 #include <iterator>
4 #include <numeric>
5 #include <stdexcept>
6 #include <sstream>
7 #include <xbt/log.h>
8 #include <xbt/time.h>
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc);
11
12 #include "misc.h"
13 #include "options.h"
14
15 #include "process.h"
16
17 double process::total_load_init = 0.0;
18 double process::total_load_running = 0.0;
19 double process::total_load_exit = 0.0;
20
21 process::process(int argc, char* argv[])
22 {
23     if (argc < 2 || !(std::istringstream(argv[1]) >> load))
24         throw std::invalid_argument("bad or missing initial load parameter");
25
26     neigh.assign(argv + 2, argv + argc);
27
28     pneigh.reserve(neigh.size());
29     for (unsigned i = 0 ; i < neigh.size() ; i++) {
30         neighbor* ptr = &neigh[i];
31         m_host_t host = MSG_get_host_by_name(ptr->get_name());
32         pneigh.push_back(ptr);
33         rev_neigh.insert(std::make_pair(host, ptr));
34     }
35
36     prev_load_broadcast = -1;   // force sending of load on first send()
37     expected_load = load;
38     total_load_running += load;
39     total_load_init += load;
40
41     ctrl_close_pending = data_close_pending = neigh.size();
42     close_received = false;
43     finalizing = false;
44
45     e_xbt_log_priority_t logp = xbt_log_priority_verbose;
46     if (!LOG_ISENABLED(logp))
47         return;
48     std::ostringstream oss;
49     oss << neigh.size() << " neighbor";
50     if (!neigh.empty()) {
51         oss << ESSE(neigh.size()) << ": ";
52         std::transform(neigh.begin(), neigh.end() - 1,
53                        std::ostream_iterator<const char*>(oss, ", "),
54                        std::tr1::mem_fn(&neighbor::get_name));
55         oss << neigh.back().get_name();
56     }
57     LOG1(logp, "Got %s.", oss.str().c_str());
58     print_loads(false, logp);
59 }
60
61 process::~process()
62 {
63     total_load_exit += load;
64 }
65
66 int process::run()
67 {
68     double next_iter_after_date = 0.0;
69     INFO1("Initial load: %g", load);
70     VERB0("Starting...");
71     comp_iter = lb_iter = 0;
72     while (true) {
73         if (opt::min_iter_duration) {
74             double now = MSG_get_clock();
75             if (now < next_iter_after_date){
76                 double delay = next_iter_after_date - now;
77                 DEBUG1("sleeping for %g s", delay);
78                 MSG_process_sleep(next_iter_after_date - now);
79             }
80             next_iter_after_date = MSG_get_clock() + opt::min_iter_duration;
81         }
82
83         if (load > 0.0) {
84             ++comp_iter;
85             if (opt::log_rate && comp_iter % opt::log_rate == 0) {
86                 if (opt::bookkeeping)
87                     INFO4("(%u:%u) current load: %g ; expected: %g",
88                           comp_iter, lb_iter, load, expected_load);
89                 else
90                     INFO2("(%u) current load: %g",
91                           comp_iter, load);
92             }
93
94             if (opt::bookkeeping)
95                 expected_load -= load_balance(expected_load);
96             else
97                 load -= load_balance(load);
98
99             print_loads(true, xbt_log_priority_debug);
100
101             send();
102             compute();
103
104         } else {
105             // send load information, and load when bookkeeping
106             send();
107         }
108
109         if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter)
110             break;
111         if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter)
112             break;
113         if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
114             VERB2("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
115             break;
116         }
117
118         // block on receiving unless there is something to compute or
119         // to send
120         bool wait = (load == 0 &&
121                      ((opt::bookkeeping ? expected_load : load)
122                       == prev_load_broadcast));
123         receive(wait);
124
125         // one of our neighbor is finalizing
126         if (opt::exit_on_close && close_received)
127             break;
128
129         // have no load and cannot receive anything
130         if (load == 0.0 && !may_receive())
131             break;
132
133         // fixme: this check should be implemented with a distributed
134         // algorithm, and not a shared global variable!
135         // fixme: should this chunk be moved before call to receive() ?
136         if (100.0 * total_load_running / total_load_init <=
137             opt::load_ratio_threshold) {
138             VERB0("No more load to balance in system, stopping.");
139             break;
140         }
141
142     }
143     VERB0("Going to finalize...");
144     finalize();
145
146     /* Open Questions :
147      * - definition of load on heterogeneous hosts ?
148      * - how to detect convergence ?
149      * - how to manage link failures ?
150      */
151
152     VERB0("Done.");
153     INFO3("Final load after %d iteration%s: %g",
154           comp_iter, ESSE(comp_iter), load);
155     if (opt::bookkeeping)
156         INFO1("Expected load: %g", expected_load);
157     return 0;
158 }
159
160 double process::sum_of_to_send() const
161 {
162     using std::tr1::bind;
163     using std::tr1::placeholders::_1;
164     using std::tr1::placeholders::_2;
165
166     return std::accumulate(neigh.begin(), neigh.end(), 0.0,
167                            bind(std::plus<double>(),
168                                 _1, bind(&neighbor::get_to_send, _2)));
169 }
170
171 double process::load_balance(double /*my_load*/)
172 {
173     if (lb_iter == 1)           // warn only once
174         WARN0("process::load_balance() is a no-op!");
175     return 0.0;
176 }
177
178 void process::compute()
179 {
180     if (load > 0.0) {
181         double duration = opt::comp_cost(load);
182         m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
183         DEBUG2("compute %g flop%s", duration, ESSE(duration));
184         MSG_task_execute(task);
185         MSG_task_destroy(task);
186     } else {
187         DEBUG0("nothing to compute !");
188     }
189 }
190
191 void process::send1_no_bookkeeping(neighbor& nb)
192 {
193     if (load != prev_load_broadcast)
194         comm.send(nb.get_ctrl_mbox(), new message(message::INFO, load));
195     double load_to_send = nb.get_to_send();
196     if (load_to_send > 0.0) {
197         comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
198         nb.set_to_send(0.0);
199     }
200 }
201
202 void process::send1_bookkeeping(neighbor& nb)
203 {
204     if (expected_load != prev_load_broadcast)
205         comm.send(nb.get_ctrl_mbox(),
206                   new message(message::INFO, expected_load));
207     double load_to_send;
208     double new_debt;
209     double debt_to_send = nb.get_to_send();
210     if (debt_to_send > 0.0) {
211         comm.send(nb.get_ctrl_mbox(),
212                   new message(message::CREDIT, debt_to_send));
213         nb.set_to_send(0.0);
214         new_debt = nb.get_debt() + debt_to_send;
215     } else {
216         new_debt = nb.get_debt();
217     }
218     if (load <= new_debt) {
219         load_to_send = load;
220         nb.set_debt(new_debt - load_to_send);
221         load = 0.0;
222     } else {
223         load_to_send = new_debt;
224         nb.set_debt(0.0);
225         load -= load_to_send;
226     }
227     if (load_to_send > 0.0)
228         comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
229 }
230
231 void process::send()
232 {
233     using std::tr1::bind;
234     using std::tr1::placeholders::_1;
235
236     if (opt::bookkeeping) {
237         std::for_each(neigh.begin(), neigh.end(),
238                       bind(&process::send1_bookkeeping, this, _1));
239         prev_load_broadcast = expected_load;
240     } else {
241         std::for_each(neigh.begin(), neigh.end(),
242                       bind(&process::send1_no_bookkeeping, this, _1));
243         prev_load_broadcast = load;
244     }
245     comm.flush(false);
246 }
247
248 void process::receive(bool wait)
249 {
250     message* msg;
251     m_host_t from;
252
253     while (may_receive() && comm.recv(msg, from, wait)) {
254         switch (msg->get_type()) {
255         case message::INFO: {
256             neighbor* n = rev_neigh[from];
257             n->set_load(msg->get_amount());
258             break;
259         }
260         case message::CREDIT:
261             expected_load += msg->get_amount();
262             break;
263         case message::LOAD: {
264             double ld = msg->get_amount();
265             load += ld;
266             if (finalizing)
267                 total_load_running -= ld;
268             break;
269         }
270         case message::CTRL_CLOSE:
271             ctrl_close_pending--;
272             close_received = true;
273             break;
274         case message::DATA_CLOSE:
275             data_close_pending--;
276             close_received = true;
277             break;
278         }
279         delete msg;
280         wait = false;           // only wait on first recv
281     }
282     comm.flush(false);
283 }
284
285 void process::finalize1(neighbor& nb)
286 {
287     comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
288     comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
289 }
290
291 void process::finalize()
292 {
293     using std::tr1::bind;
294     using std::tr1::placeholders::_1;
295
296     finalizing = true;
297     total_load_running -= load;
298
299     DEBUG2("send CLOSE to %lu neighbor%s",
300            (unsigned long )neigh.size(), ESSE(neigh.size()));
301     std::for_each(neigh.begin(), neigh.end(),
302                   bind(&process::finalize1, this, _1));
303
304     DEBUG2("wait for CLOSE from %lu neighbor%s",
305            (unsigned long )neigh.size(), ESSE(neigh.size()));
306     while (may_receive()) {
307         comm.flush(false);
308         receive(true);
309     }
310
311     comm.flush(true);
312 }
313
314 #define print_loads_generic(vec, verbose, logp, cat)                    \
315     if (_XBT_LOG_ISENABLEDV((*cat), logp)) {                            \
316         using std::tr1::bind;                                           \
317         using std::tr1::placeholders::_1;                               \
318         XCLOG0(cat, logp, "Neighbor loads:");                           \
319         std::for_each(vec.begin(), vec.end(),                           \
320                       bind(&neighbor::print, _1, verbose, logp, cat));  \
321     } else ((void)0)
322
323 void process::print_loads(bool verbose,
324                           e_xbt_log_priority_t logp,
325                           xbt_log_category_t cat) const
326 {
327     print_loads_generic(neigh, verbose, logp, cat);
328 }
329
330 void process::print_loads_p(bool verbose,
331                             e_xbt_log_priority_t logp,
332                             xbt_log_category_t cat) const
333 {
334     print_loads_generic(pneigh, verbose, logp, cat);
335 }
336
337 #undef print_loads_generic
338
339 // Local variables:
340 // mode: c++
341 // End: