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

Private GIT Repository
Add a VERSION file, for the distrib.
[loba.git] / process.cpp
1 #include <algorithm>
2 #include <cmath>
3 #include <functional>
4 #include <iterator>
5 #include <numeric>
6 #include <stdexcept>
7 #include <sstream>
8 #include <xbt/log.h>
9 #include <xbt/time.h>
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc);
12
13 #include "misc.h"
14 #include "options.h"
15 #include "tracing.h"
16
17 #include "process.h"
18
19 mutex_t *process::proc_mutex;
20
21 double process::total_load_init = 0.0;
22 double process::total_load_running = 0.0;
23 double process::total_load_exit = 0.0;
24
25 int process::process_counter = 0;
26 double process::total_load_average;
27 double process::average_load_ratio;
28 double process::load_diff_threshold;
29
30 std::atomic<int> process::convergence_counter(0);
31
32 namespace {
33
34     void sleep_until_date(double& date, double duration)
35     {
36         double sleep_duration = date - MSG_get_clock();
37         if (sleep_duration > 0.0)
38             MSG_process_sleep(sleep_duration);
39         date = MSG_get_clock() + duration;
40     }
41
42 }
43
44 process::process(int argc, char* argv[])
45 {
46     if (argc < 2 || !(std::istringstream(argv[1]) >> real_load))
47         throw std::invalid_argument("bad or missing initial load parameter");
48
49     double iload = std::trunc(real_load);
50     if (opt::integer_transfer && real_load != iload) {
51         XBT_WARN("Initial load %g is not an integer.  Truncate it.",
52                  real_load);
53         real_load = iload;
54     }
55
56     neigh.assign(argv + 2, argv + argc);
57
58     pneigh.reserve(neigh.size());
59     for (unsigned i = 0 ; i < neigh.size() ; i++) {
60         neighbor* ptr = &neigh[i];
61         msg_host_t host = MSG_get_host_by_name(ptr->get_name());
62         pneigh.push_back(ptr);
63         rev_neigh.insert(std::make_pair(host, ptr));
64     }
65
66     prev_load_broadcast = -1;   // force sending of load on first send_all()
67     expected_load = real_load;
68     received_load = 0.0;
69
70     idle_duration = 0.0;
71     convergence = -1.0;
72
73     proc_mutex->acquire();
74     process_counter++;
75     convergence_counter++;
76     total_load_init += real_load;
77     total_load_running += real_load;
78     total_load_average = total_load_running / process_counter;
79     if (opt::avg_load_ratio >= 0.0)
80         average_load_ratio = opt::avg_load_ratio;
81     else
82         average_load_ratio = 100.0 *
83             (process_counter / -opt::avg_load_ratio) / total_load_average;
84     load_diff_threshold = (opt::load_ratio_threshold +
85                            average_load_ratio * total_load_average) / 100.0;
86     proc_mutex->release();
87
88     ctrl_close_pending = data_close_pending = neigh.size();
89     close_received = false;
90     finalizing = false;
91
92     all_comp_iter = comp_iter = lb_iter = 0;
93
94     lb_thread = new_msg_thread("loba",
95                                std::bind(&process::load_balance_loop, this));
96
97     e_xbt_log_priority_t logp = xbt_log_priority_verbose;
98     if (!LOG_ISENABLED(logp))
99         return;
100     std::ostringstream oss;
101     oss << neigh.size() << " neighbor";
102     if (!neigh.empty()) {
103         oss << ESSE(neigh.size()) << ": ";
104         std::transform(neigh.begin(), neigh.end() - 1,
105                        std::ostream_iterator<const char*>(oss, ", "),
106                        std::mem_fn(&neighbor::get_name));
107         oss << neigh.back().get_name();
108     }
109     XBT_LOG(logp, "Got %s.", oss.str().c_str());
110     print_loads(false, logp);
111 }
112
113 process::~process()
114 {
115     delete lb_thread;
116     proc_mutex->acquire();
117     total_load_exit += real_load;
118     proc_mutex->release();
119     xbt_assert(received_load == 0.0,
120                "received_load is %g, but should be 0.0 !", received_load);
121     if (opt::log_rate < 0)
122         return;
123     XBT_INFO("Final load after %d:%d:%d iterations: %g",
124              lb_iter, comp_iter, all_comp_iter, real_load);
125     if (convergence >= 0.0)
126         XBT_INFO("Convergence within %g%% was achieved at time %g",
127                  average_load_ratio, convergence);
128     else
129         XBT_INFO("Convergence within %g%% was not achieved",
130                  average_load_ratio);
131     XBT_VERB("Expected load was: %g", expected_load);
132     XBT_VERB("Total computation for this process: %g", get_comp_amount());
133     print_loads(true, xbt_log_priority_debug);
134 }
135
136 double process::get_iter_deviation() const
137 {
138     double average_cost = opt::comp_cost(total_load_average); // fixme: get locked?
139     // Do not count idle periods
140     double comp_iter_opt = acc.comp_amount / average_cost;
141 /*
142     // Add iterations that could have been achieved while beeing idle
143     // (kept for documentation)
144     double self_speed = MSG_get_host_speed(MSG_host_self());
145     double average_duration = average_cost / self_speed;
146     comp_iter_opt += idle_duration / average_duration;
147 */
148     return comp_iter - comp_iter_opt;
149 }
150
151 int process::run()
152 {
153     if (opt::log_rate >= 0) {
154         XBT_INFO("Initial load: %g", real_load);
155         XBT_VERB("Initial expected load: %g", expected_load);
156     }
157     XBT_VERB("Starting...");
158     mutex.acquire();
159     lb_thread->start();
160     while (lb_iter <= opt::comp_iter_delay)
161         cond.wait(mutex);
162     mutex.release();
163     double sleep_duration = opt::comp_time_delay - MSG_get_clock();
164     if (sleep_duration > 0.0)
165         MSG_process_sleep(sleep_duration);
166     compute_loop();
167     lb_thread->wait();
168     XBT_VERB("Done.");
169     return 0;
170 }
171
172 void process::load_balance_loop()
173 {
174     using std::placeholders::_1;
175
176     double next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
177     while (still_running()) {
178         if (lb_iter == opt::comp_iter_delay) {
179             mutex.acquire();
180             ++lb_iter;
181             cond.signal();
182             mutex.release();
183         } else {
184             ++lb_iter;
185         }
186
187         ctrl_receive(0.0);
188
189         mutex.acquire();
190         if (!opt::bookkeeping)
191             expected_load = real_load - get_sum_of_to_send();
192         // nothing to do with opt::bookkeeping
193
194         if (opt::log_rate && lb_iter % opt::log_rate == 0) {
195             XBT_INFO("(%u:%u:%u) current load: %g",
196                      lb_iter, comp_iter, all_comp_iter, real_load);
197             XBT_VERB("... expected load: %g", expected_load);
198         }
199
200         if (expected_load > 0.0)
201             load_balance();
202
203         print_loads(true, xbt_log_priority_debug);
204
205         // send
206         comm.ctrl_flush(false);
207         std::for_each(neigh.begin(), neigh.end(),
208                       std::bind(&process::ctrl_send, this, _1));
209         prev_load_broadcast = expected_load;
210         mutex.release();
211
212         sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
213     }
214
215     XBT_VERB("Going to finalize for %s...", __func__);
216     XBT_DEBUG("send CTRL_CLOSE to %zu neighbor%s",
217               neigh.size(), ESSE(neigh.size()));
218     std::for_each(neigh.begin(), neigh.end(),
219                   std::bind(&process::ctrl_close, this, _1));
220     while (ctrl_close_pending) {
221         comm.ctrl_flush(false);
222         XBT_DEBUG("waiting for %d CTRL_CLOSE", ctrl_close_pending);
223         ctrl_receive(-1.0);
224     }
225     comm.ctrl_flush(true);
226 }
227
228 void process::compute_loop()
229 {
230     using std::placeholders::_1;
231
232     double next_iter_after_date = MSG_get_clock() + opt::min_comp_iter_duration;
233     double idle_since_date = 0.0;
234     while (still_running()) {
235         // receive
236         // if there is something to compute, do not block
237         // else, block the duration of an *lb* iteration
238         data_receive(real_load > 0.0 ? 0.0 : opt::min_lb_iter_duration);
239
240         // send
241         comm.data_flush(false);
242         mutex.acquire();
243         real_load += received_load;
244         received_load = 0.0;
245         std::for_each(neigh.begin(), neigh.end(),
246                       std::bind(&process::data_send, this, _1));
247         mutex.release();
248
249         ++all_comp_iter;
250         if (real_load == 0.0)
251             continue;
252
253         convergence_check();
254
255         // compute
256         idle_duration += MSG_get_clock() - idle_since_date;
257         ++comp_iter;
258         double flops = opt::comp_cost(real_load);
259         msg_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
260         // MSG_task_set_category(task, TRACE_CAT_COMP);
261         XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
262         MSG_task_execute(task);
263         add_comp_amount(flops);
264         MSG_task_destroy(task);
265
266         idle_since_date = MSG_get_clock();
267
268         sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
269     }
270
271     XBT_VERB("Going to finalize for %s...", __func__);
272     // Note: idle duration is not counted during finalization
273     finalizing = true;
274     XBT_DEBUG("send DATA_CLOSE to %zu neighbor%s",
275               neigh.size(), ESSE(neigh.size()));
276     std::for_each(neigh.begin(), neigh.end(),
277                   std::bind(&process::data_close, this, _1));
278     while (data_close_pending) {
279         comm.data_flush(false);
280         XBT_DEBUG("waiting for %d DATA_CLOSE", data_close_pending);
281         data_receive(-1.0);
282     }
283     real_load += received_load;
284     received_load = 0.0;
285     proc_mutex->acquire();
286     total_load_running -= real_load;
287     proc_mutex->release();
288     convergence_check();
289     comm.data_flush(true);
290 }
291
292 void process::convergence_check()
293 {
294     double average = total_load_average; // fixme: get locked?
295     double load_diff = std::fabs(real_load - average);
296     bool converged = load_diff <= load_diff_threshold;
297
298     if (converged) {
299         if (convergence < 0) {
300             XBT_VERB("current load has converged: %g (%.4g%%)",
301                      real_load,  100.0 * load_diff / average);
302             convergence = MSG_get_clock();
303             local_convergence_counter = opt::exit_on_convergence;
304         }
305         if (local_convergence_counter > 0 && --local_convergence_counter == 0)
306                 --convergence_counter;
307     } else {
308         if (convergence >= 0.0) {
309             XBT_VERB("current load has diverged: %g (%.4g%%)",
310                      real_load, 100.0 * load_diff / average);
311             convergence = -1.0;
312             if (local_convergence_counter == 0)
313                 ++convergence_counter;
314         }
315     }
316 }
317
318 bool process::still_running()
319 {
320     static bool last_status = true;
321
322     if (!last_status) {
323         /* nop */
324
325     } else if (opt::exit_request) {
326         XBT_VERB("Global exit requested");
327         last_status = false;
328
329     } else if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
330         XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
331         last_status = false;
332
333     } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
334         XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
335         last_status = false;
336
337     } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
338         XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
339         last_status = false;
340
341     } else if (opt::exit_on_convergence && convergence_counter == 0) {
342         XBT_VERB("Global convergence detected");
343         last_status = false;
344
345     } else if (opt::exit_on_close && close_received) {
346         XBT_VERB("Close received");
347         last_status = false;
348
349     } else if (real_load == 0.0 && !data_close_pending) {
350         XBT_VERB("I'm a poor lonesome process, and I have no load...");
351         last_status = false;
352
353     } else if (100.0 * total_load_running / total_load_init <=
354                opt::load_ratio_threshold) { // fixme: get locked?
355         // fixme: this check should be implemented with a distributed
356         // algorithm, and not a shared global variable!
357         XBT_VERB("No more load to balance in system.");
358         last_status = false;
359     }
360
361     return last_status;
362 }
363
364 double process::get_sum_of_to_send() const
365 {
366     using std::placeholders::_1;
367     using std::placeholders::_2;
368
369     return std::accumulate(neigh.begin(), neigh.end(), 0.0,
370                            std::bind(std::plus<double>(), _1,
371                                      std::bind(&neighbor::get_to_send, _2)));
372 }
373
374 void process::load_balance()
375 {
376     if (lb_iter == 1)           // warn only once
377         XBT_WARN("process::load_balance() is a no-op!");
378 }
379
380 void process::send(neighbor& nb, double amount)
381 {
382     expected_load -= amount;
383     nb.set_to_send(nb.get_to_send() + amount);
384     nb.set_load(nb.get_load() + amount);
385 }
386
387 void process::ctrl_send(neighbor& nb)
388 {
389     double info_to_send = expected_load;
390     double debt_to_send;
391     if (opt::bookkeeping) {     // bookkeeping
392         debt_to_send = nb.get_to_send();
393         if (debt_to_send > 0.0) {
394             nb.set_to_send(0.0);
395             nb.set_debt(nb.get_debt() + debt_to_send);
396         }
397     } else {                    // !bookkeeping
398         debt_to_send = 0.0;
399     }
400     if (info_to_send != prev_load_broadcast || debt_to_send > 0.0) {
401         message* msg = new message(message::CTRL, info_to_send, debt_to_send);
402         add_ctrl_send_mesg(msg->get_size());
403         comm.ctrl_send(nb.get_ctrl_mbox(), msg);
404     }
405 }
406
407 double process::compute_load_to_send(double desired)
408 {
409     if (opt::integer_transfer)
410         desired = std::floor(desired);
411     return desired >= opt::min_transfer_amount ? desired : 0.0;
412 }
413
414 void process::data_send(neighbor& nb)
415 {
416     double load_to_send;
417     if (opt::bookkeeping) {     // bookkeeping
418         double excess_load;     // load amount we are able to send
419         if (opt::egocentric)
420             excess_load = std::max(0.0, real_load - expected_load);
421         else
422             excess_load = real_load;
423
424         double balance;
425         if (nb.get_credit() > 0.0)
426             balance = nb.get_debt() - nb.get_credit();
427         else
428             balance = nb.get_debt();
429         load_to_send = std::min(excess_load,
430                                 std::max(0.0, balance));
431
432         // adjust load to send (rounding, truncation, etc.)
433         load_to_send = compute_load_to_send(load_to_send);
434         if (load_to_send > 0.0)
435             nb.set_debt(nb.get_debt() - load_to_send);
436     } else {                    // !bookkeeping
437         load_to_send = compute_load_to_send(nb.get_to_send());
438         if (load_to_send > 0.0)
439             nb.set_to_send(nb.get_to_send() - load_to_send);
440     }
441     real_load -= load_to_send;
442     while (load_to_send > 0.0) {
443         double amount;
444         if (opt::max_transfer_amount)
445             amount = std::min(load_to_send, opt::max_transfer_amount);
446         else
447             amount = load_to_send;
448         message* msg = new message(message::DATA, amount);
449         add_data_send_mesg(msg->get_size());
450         comm.data_send(nb.get_data_mbox(), msg);
451         load_to_send -= amount;
452     }
453 }
454
455 void process::ctrl_close(neighbor& nb)
456 {
457     comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
458 }
459
460 void process::data_close(neighbor& nb)
461 {
462     comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
463 }
464
465 void process::ctrl_receive(double timeout)
466 {
467     message* msg;
468     msg_host_t from;
469
470     XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
471     while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
472         if (msg->get_type() != message::CTRL_CLOSE)
473             add_ctrl_recv_mesg(msg->get_size());
474         handle_message(msg, from);
475         timeout = 0.0;
476     }
477 }
478
479 void process::data_receive(double timeout)
480 {
481     message* msg;
482     msg_host_t from;
483
484     XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
485     while (data_close_pending && comm.data_recv(msg, from, timeout)) {
486         if (msg->get_type() != message::DATA_CLOSE)
487             add_data_recv_mesg(msg->get_size());
488         handle_message(msg, from);
489         timeout = 0.0;
490     }
491 }
492
493 void process::handle_message(message* msg, msg_host_t from)
494 {
495     switch (msg->get_type()) {
496     case message::CTRL: {
497         neighbor* n = rev_neigh[from];
498         n->set_load(msg->get_amount() + n->get_to_send());
499         if (opt::bookkeeping) {
500             double credit = msg->get_credit();
501             expected_load += credit;
502             n->set_credit(n->get_credit() + credit);
503         }
504         break;
505     }
506     case message::DATA: {
507         neighbor* n = rev_neigh[from];
508         double ld = msg->get_amount();
509         received_load += ld;
510         n->set_credit(n->get_credit() - ld);
511         break;
512     }
513     case message::CTRL_CLOSE:
514         ctrl_close_pending--;
515         close_received = true;
516         break;
517     case message::DATA_CLOSE:
518         data_close_pending--;
519         close_received = true;
520         break;
521     }
522     delete msg;
523 }
524
525 #define print_loads_generic(vec, verbose, logp, cat)                    \
526     if (_XBT_LOG_ISENABLEDV((*cat), logp)) {                            \
527         using std::placeholders::_1;                                    \
528         XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected).  "     \
529                   "Neighbor loads:", real_load, expected_load);         \
530         std::for_each(vec.begin(), vec.end(),                           \
531                       std::bind(&neighbor::print, _1, verbose, logp, cat)); \
532     } else ((void)0)
533
534 void process::print_loads(bool verbose,
535                           e_xbt_log_priority_t logp,
536                           xbt_log_category_t cat) const
537 {
538     print_loads_generic(neigh, verbose, logp, cat);
539 }
540
541 void process::print_loads_p(bool verbose,
542                             e_xbt_log_priority_t logp,
543                             xbt_log_category_t cat) const
544 {
545     print_loads_generic(pneigh, verbose, logp, cat);
546 }
547
548 #undef print_loads_generic
549
550 // Local variables:
551 // mode: c++
552 // End: