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

Private GIT Repository
New bug?
[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 #include "tracing.h"
15
16 #include "process.h"
17
18 double process::total_load_init = 0.0;
19 double process::total_load_running = 0.0;
20 double process::total_load_exit = 0.0;
21
22 namespace {
23
24     void sleep_until_date(double& date, double duration)
25     {
26         double sleep_duration = date - MSG_get_clock();
27         if (sleep_duration > 0.0)
28             MSG_process_sleep(sleep_duration);
29         date = MSG_get_clock() + duration;
30     }
31
32 }
33
34 process::process(int argc, char* argv[])
35 {
36     if (argc < 2 || !(std::istringstream(argv[1]) >> real_load))
37         throw std::invalid_argument("bad or missing initial load parameter");
38
39     neigh.assign(argv + 2, argv + argc);
40
41     pneigh.reserve(neigh.size());
42     for (unsigned i = 0 ; i < neigh.size() ; i++) {
43         neighbor* ptr = &neigh[i];
44         m_host_t host = MSG_get_host_by_name(ptr->get_name());
45         pneigh.push_back(ptr);
46         rev_neigh.insert(std::make_pair(host, ptr));
47     }
48
49     prev_load_broadcast = -1;   // force sending of load on first send_all()
50     expected_load = real_load;
51     total_load_running += real_load;
52     total_load_init += real_load;
53
54     ctrl_close_pending = data_close_pending = neigh.size();
55     close_received = false;
56     finalizing = false;
57
58     comp_iter = lb_iter = 0;
59
60     lb_thread = new_msg_thread("loba",
61                                std::tr1::bind(&process::load_balance_loop,
62                                               this));
63
64     e_xbt_log_priority_t logp = xbt_log_priority_verbose;
65     if (!LOG_ISENABLED(logp))
66         return;
67     std::ostringstream oss;
68     oss << neigh.size() << " neighbor";
69     if (!neigh.empty()) {
70         oss << ESSE(neigh.size()) << ": ";
71         std::transform(neigh.begin(), neigh.end() - 1,
72                        std::ostream_iterator<const char*>(oss, ", "),
73                        std::tr1::mem_fn(&neighbor::get_name));
74         oss << neigh.back().get_name();
75     }
76     XBT_LOG(logp, "Got %s.", oss.str().c_str());
77     print_loads(false, logp);
78 }
79
80 process::~process()
81 {
82     delete lb_thread;
83     total_load_exit += real_load;
84     if (opt::log_rate < 0)
85         return;
86     XBT_INFO("Final load after %d:%d iterations: %g",
87              lb_iter, comp_iter, real_load);
88     XBT_VERB("Expected load was: %g", expected_load);
89     XBT_VERB("Total computation for this process: %g", get_comp_amount());
90 }
91
92 int process::run()
93 {
94     if (opt::log_rate >= 0) {
95         XBT_INFO("Initial load: %g", real_load);
96         XBT_VERB("Initial expected load: %g", expected_load);
97     }
98     XBT_VERB("Starting...");
99     mutex.acquire();
100     lb_thread->start();
101     while (lb_iter <= opt::comp_iter_delay)
102         cond.wait(mutex);
103     mutex.release();
104     double sleep_duration = opt::comp_time_delay - MSG_get_clock();
105     if (sleep_duration > 0.0)
106         MSG_process_sleep(sleep_duration);
107     compute_loop();
108     lb_thread->wait();
109     XBT_VERB("Done.");
110     return 0;
111 }
112
113 void process::load_balance_loop()
114 {
115     using std::tr1::bind;
116     using std::tr1::placeholders::_1;
117
118     double next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
119     while (still_running()) {
120         if (lb_iter == opt::comp_iter_delay) {
121             mutex.acquire();
122             ++lb_iter;
123             cond.signal();
124             mutex.release();
125         } else {
126             ++lb_iter;
127         }
128
129         mutex.acquire();
130         if (!opt::bookkeeping)
131             expected_load = real_load - get_sum_of_to_send();
132         // nothing to do with opt::bookkeeping
133
134         if (opt::log_rate && lb_iter % opt::log_rate == 0) {
135             XBT_INFO("(%u:%u) current load: %g", lb_iter, comp_iter, real_load);
136             XBT_VERB("... expected load: %g", expected_load);
137         }
138         mutex.release();
139
140         if (expected_load > 0.0)
141             load_balance();
142
143         print_loads(true, xbt_log_priority_debug);
144
145         // send
146         std::for_each(neigh.begin(), neigh.end(),
147                       bind(&process::ctrl_send, this, _1));
148         prev_load_broadcast = expected_load;
149
150         sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
151         ctrl_receive(0.0);
152
153         comm.ctrl_flush(false);
154     }
155
156     XBT_VERB("Going to finalize for %s...", __func__);
157     XBT_DEBUG("send CTRL_CLOSE to %zu neighbor%s",
158               neigh.size(), ESSE(neigh.size()));
159     std::for_each(neigh.begin(), neigh.end(),
160                   bind(&process::ctrl_close, this, _1));
161     while (ctrl_close_pending) {
162         comm.ctrl_flush(false);
163         XBT_DEBUG("waiting for %d CTRL CLOSE", ctrl_close_pending);
164         ctrl_receive(-1.0);
165     }
166     comm.ctrl_flush(true);
167 }
168
169 void process::compute_loop()
170 {
171     using std::tr1::bind;
172     using std::tr1::placeholders::_1;
173
174     double next_iter_after_date = MSG_get_clock() + opt::min_comp_iter_duration;
175     while (still_running()) {
176         // receive
177         mutex.acquire();
178         if (real_load > 0.0)
179             data_receive(0.0);
180         else
181             data_receive(opt::min_comp_iter_duration);
182         mutex.release();
183
184         comm.data_flush(false);
185
186         // send
187         mutex.acquire();
188         std::for_each(neigh.begin(), neigh.end(),
189                       bind(&process::data_send, this, _1));
190         mutex.release();
191
192         if (real_load == 0.0)
193             continue;
194
195         // compute
196         ++comp_iter;
197         double flops = opt::comp_cost(real_load);
198         m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
199         TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
200         XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
201         MSG_task_execute(task);
202         add_comp_amount(flops);
203         MSG_task_destroy(task);
204
205         sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
206     }
207
208     XBT_VERB("Going to finalize for %s...", __func__);
209     // last send, for not losing load scheduled to be sent
210     std::for_each(neigh.begin(), neigh.end(),
211                   bind(&process::data_send, this, _1));
212     finalizing = true;
213     total_load_running -= real_load;
214     XBT_DEBUG("send DATA_CLOSE to %zu neighbor%s",
215               neigh.size(), ESSE(neigh.size()));
216     std::for_each(neigh.begin(), neigh.end(),
217                   bind(&process::data_close, this, _1));
218     while (data_close_pending) {
219         comm.data_flush(false);
220         XBT_DEBUG("waiting for %d DATA CLOSE", data_close_pending);
221         data_receive(-1.0);
222     }
223     comm.data_flush(true);
224 }
225
226 bool process::still_running()
227 {
228     static bool last_status = true;
229
230     if (!last_status) {
231         /* nop */
232
233     } else if (opt::exit_request) {
234         XBT_VERB("Global exit requested");
235         last_status = false;
236
237     } else if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
238         XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
239         last_status = false;
240
241     } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
242         XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
243         last_status = false;
244
245     } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
246         XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
247         last_status = false;
248
249     } else if (opt::exit_on_close && close_received) {
250         XBT_VERB("Close received");
251         last_status = false;
252
253     } else if (real_load == 0.0 && !data_close_pending) {
254         XBT_VERB("I'm a poor lonesome process, and I have no load...");
255         last_status = false;
256
257     } else if (100.0 * total_load_running / total_load_init <=
258                opt::load_ratio_threshold) {
259         // fixme: this check should be implemented with a distributed
260         // algorithm, and not a shared global variable!
261         XBT_VERB("No more load to balance in system.");
262         last_status = false;
263     }
264
265     return last_status;
266 }
267
268 double process::get_sum_of_to_send() const
269 {
270     using std::tr1::bind;
271     using std::tr1::placeholders::_1;
272     using std::tr1::placeholders::_2;
273
274     return std::accumulate(neigh.begin(), neigh.end(), 0.0,
275                            bind(std::plus<double>(),
276                                 _1, bind(&neighbor::get_to_send, _2)));
277 }
278
279 void process::load_balance()
280 {
281     if (lb_iter == 1)           // warn only once
282         XBT_WARN("process::load_balance() is a no-op!");
283 }
284
285 void process::send(neighbor& nb, double amount)
286 {
287     expected_load -= amount;
288     nb.set_to_send(nb.get_to_send() + amount);
289     nb.set_load(nb.get_load() + amount);
290 }
291
292 void process::ctrl_send(neighbor& nb)
293 {
294     double info_to_send = expected_load;
295     if (info_to_send != prev_load_broadcast) {
296         message* msg = new message(message::INFO, info_to_send);
297         add_ctrl_send_mesg(msg->get_size());
298         comm.ctrl_send(nb.get_ctrl_mbox(), msg);
299     }
300     if (opt::bookkeeping) {
301         double debt_to_send = nb.get_to_send();
302         if (debt_to_send > 0.0) {
303             nb.set_to_send(0.0);
304             nb.set_debt(nb.get_debt() + debt_to_send);
305             message* msg = new message(message::CREDIT, debt_to_send);
306             add_ctrl_send_mesg(msg->get_size());
307             comm.ctrl_send(nb.get_ctrl_mbox(), msg);
308         }
309     }
310 }
311
312 void process::data_send(neighbor& nb)
313 {
314     double load_to_send;
315     if (opt::bookkeeping) {
316         load_to_send = std::min(real_load, nb.get_debt());
317         if (load_to_send >= opt::min_transfer_amount) {
318             nb.set_debt(nb.get_debt() - load_to_send);
319             real_load -= load_to_send;
320         } else {
321             load_to_send = 0.0;
322         }
323     } else {
324         load_to_send = nb.get_to_send();
325         if (load_to_send >= opt::min_transfer_amount) {
326             nb.set_to_send(0.0);
327             real_load -= load_to_send;
328         } else {
329             load_to_send = 0.0;
330         }
331     }
332     while (load_to_send > 0.0) {
333         double amount;
334         if (opt::max_transfer_amount)
335             amount = std::min(load_to_send, opt::max_transfer_amount);
336         else
337             amount = load_to_send;
338         message* msg = new message(message::LOAD, amount);
339         add_data_send_mesg(msg->get_size());
340         comm.data_send(nb.get_data_mbox(), msg);
341         load_to_send -= amount;
342     }
343 }
344
345 void process::ctrl_close(neighbor& nb)
346 {
347     comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
348 }
349
350 void process::data_close(neighbor& nb)
351 {
352     comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
353 }
354
355 void process::ctrl_receive(double timeout)
356 {
357     message* msg;
358     m_host_t from;
359
360     XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
361     while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
362         if (msg->get_type() != message::CTRL_CLOSE)
363             add_ctrl_recv_mesg(msg->get_size());
364         handle_message(msg, from);
365         timeout = 0.0;
366     }
367 }
368
369 void process::data_receive(double timeout)
370 {
371     message* msg;
372     m_host_t from;
373
374     XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
375     while (data_close_pending && comm.data_recv(msg, from, timeout)) {
376         if (msg->get_type() != message::DATA_CLOSE)
377             add_data_recv_mesg(msg->get_size());
378         handle_message(msg, from);
379         timeout = 0.0;
380     }
381 }
382
383 void process::handle_message(message* msg, m_host_t from)
384 {
385     switch (msg->get_type()) {
386     case message::INFO: {
387         neighbor* n = rev_neigh[from];
388         n->set_load(msg->get_amount() + n->get_to_send());
389         break;
390     }
391     case message::CREDIT:
392         expected_load += msg->get_amount();
393         break;
394     case message::LOAD: {
395         double ld = msg->get_amount();
396         real_load += ld;
397         if (finalizing)
398             total_load_running -= ld;
399         break;
400     }
401     case message::CTRL_CLOSE:
402         ctrl_close_pending--;
403         close_received = true;
404         break;
405     case message::DATA_CLOSE:
406         data_close_pending--;
407         close_received = true;
408         break;
409     }
410     delete msg;
411 }
412
413 #define print_loads_generic(vec, verbose, logp, cat)                    \
414     if (_XBT_LOG_ISENABLEDV((*cat), logp)) {                            \
415         using std::tr1::bind;                                           \
416         using std::tr1::placeholders::_1;                               \
417         XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected).  "     \
418                   "Neighbor loads:", real_load, expected_load);         \
419         std::for_each(vec.begin(), vec.end(),                           \
420                       bind(&neighbor::print, _1, verbose, logp, cat));  \
421     } else ((void)0)
422
423 void process::print_loads(bool verbose,
424                           e_xbt_log_priority_t logp,
425                           xbt_log_category_t cat) const
426 {
427     print_loads_generic(neigh, verbose, logp, cat);
428 }
429
430 void process::print_loads_p(bool verbose,
431                             e_xbt_log_priority_t logp,
432                             xbt_log_category_t cat) const
433 {
434     print_loads_generic(pneigh, verbose, logp, cat);
435 }
436
437 #undef print_loads_generic
438
439 // Local variables:
440 // mode: c++
441 // End: