11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc);
19 mutex_t *process::proc_mutex;
21 double process::total_load_init = 0.0;
22 double process::total_load_running = 0.0;
23 double process::total_load_exit = 0.0;
25 int process::process_counter = 0;
26 double process::total_load_average;
27 double process::average_load_ratio;
28 double process::load_diff_threshold;
30 std::atomic<int> process::convergence_counter(0);
34 void sleep_until_date(double& date, double duration)
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;
44 process::process(int argc, char* argv[])
46 if (argc < 2 || !(std::istringstream(argv[1]) >> real_load))
47 throw std::invalid_argument("bad or missing initial load parameter");
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.",
56 neigh.assign(argv + 2, argv + argc);
58 pneigh.reserve(neigh.size());
59 for (unsigned i = 0 ; i < neigh.size() ; i++) {
60 neighbor* ptr = &neigh[i];
61 m_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));
66 prev_load_broadcast = -1; // force sending of load on first send_all()
67 expected_load = real_load;
73 proc_mutex->acquire();
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;
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();
88 ctrl_close_pending = data_close_pending = neigh.size();
89 close_received = false;
92 all_comp_iter = comp_iter = lb_iter = 0;
94 lb_thread = new_msg_thread("loba",
95 std::bind(&process::load_balance_loop, this));
97 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
98 if (!LOG_ISENABLED(logp))
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();
109 XBT_LOG(logp, "Got %s.", oss.str().c_str());
110 print_loads(false, logp);
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)
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);
129 XBT_INFO("Convergence within %g%% was not achieved",
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);
136 double process::get_iter_deviation() const
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;
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;
148 return comp_iter - comp_iter_opt;
153 if (opt::log_rate >= 0) {
154 XBT_INFO("Initial load: %g", real_load);
155 XBT_VERB("Initial expected load: %g", expected_load);
157 XBT_VERB("Starting...");
160 while (lb_iter <= opt::comp_iter_delay)
163 double sleep_duration = opt::comp_time_delay - MSG_get_clock();
164 if (sleep_duration > 0.0)
165 MSG_process_sleep(sleep_duration);
172 void process::load_balance_loop()
174 using std::placeholders::_1;
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) {
190 if (!opt::bookkeeping)
191 expected_load = real_load - get_sum_of_to_send();
192 // nothing to do with opt::bookkeeping
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);
200 if (expected_load > 0.0)
203 print_loads(true, xbt_log_priority_debug);
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;
212 sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
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);
225 comm.ctrl_flush(true);
228 void process::compute_loop()
230 using std::placeholders::_1;
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()) {
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);
241 comm.data_flush(false);
243 real_load += received_load;
245 std::for_each(neigh.begin(), neigh.end(),
246 std::bind(&process::data_send, this, _1));
250 if (real_load == 0.0)
256 idle_duration += MSG_get_clock() - idle_since_date;
258 double flops = opt::comp_cost(real_load);
259 m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
260 TRACE_msg_set_task_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);
266 idle_since_date = MSG_get_clock();
268 sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
271 XBT_VERB("Going to finalize for %s...", __func__);
272 // Note: idle duration is not counted during finalization
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);
283 real_load += received_load;
285 proc_mutex->acquire();
286 total_load_running -= real_load;
287 proc_mutex->release();
289 comm.data_flush(true);
292 void process::convergence_check()
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;
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;
305 if (local_convergence_counter > 0 && --local_convergence_counter == 0)
306 --convergence_counter;
308 if (convergence >= 0.0) {
309 XBT_VERB("current load has diverged: %g (%.4g%%)",
310 real_load, 100.0 * load_diff / average);
312 if (local_convergence_counter == 0)
313 ++convergence_counter;
318 bool process::still_running()
320 static bool last_status = true;
325 } else if (opt::exit_request) {
326 XBT_VERB("Global exit requested");
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);
333 } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
334 XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
337 } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
338 XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
341 } else if (opt::exit_on_convergence && convergence_counter == 0) {
342 XBT_VERB("Global convergence detected");
345 } else if (opt::exit_on_close && close_received) {
346 XBT_VERB("Close received");
349 } else if (real_load == 0.0 && !data_close_pending) {
350 XBT_VERB("I'm a poor lonesome process, and I have no load...");
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.");
364 double process::get_sum_of_to_send() const
366 using std::placeholders::_1;
367 using std::placeholders::_2;
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)));
374 void process::load_balance()
376 if (lb_iter == 1) // warn only once
377 XBT_WARN("process::load_balance() is a no-op!");
380 void process::send(neighbor& nb, double amount)
382 expected_load -= amount;
383 nb.set_to_send(nb.get_to_send() + amount);
384 nb.set_load(nb.get_load() + amount);
387 void process::ctrl_send(neighbor& nb)
389 double info_to_send = expected_load;
391 if (opt::bookkeeping) { // bookkeeping
392 debt_to_send = nb.get_to_send();
393 if (debt_to_send > 0.0) {
395 nb.set_debt(nb.get_debt() + debt_to_send);
397 } else { // !bookkeeping
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);
407 double process::compute_load_to_send(double desired)
409 if (opt::integer_transfer)
410 desired = std::floor(desired);
411 return desired >= opt::min_transfer_amount ? desired : 0.0;
414 void process::data_send(neighbor& nb)
417 if (opt::bookkeeping) { // bookkeeping
418 double excess_load; // load amount we are able to send
420 excess_load = std::max(0.0, real_load - expected_load);
422 excess_load = real_load;
424 double balance = nb.get_debt() - nb.get_credit();
425 load_to_send = std::min(excess_load,
426 std::max(0.0, balance));
428 // adjust load to send (rounding, truncation, etc.)
429 load_to_send = compute_load_to_send(load_to_send);
430 if (load_to_send > 0.0)
431 nb.set_debt(nb.get_debt() - load_to_send);
432 } else { // !bookkeeping
433 load_to_send = compute_load_to_send(nb.get_to_send());
434 if (load_to_send > 0.0)
435 nb.set_to_send(nb.get_to_send() - load_to_send);
437 real_load -= load_to_send;
438 while (load_to_send > 0.0) {
440 if (opt::max_transfer_amount)
441 amount = std::min(load_to_send, opt::max_transfer_amount);
443 amount = load_to_send;
444 message* msg = new message(message::DATA, amount);
445 add_data_send_mesg(msg->get_size());
446 comm.data_send(nb.get_data_mbox(), msg);
447 load_to_send -= amount;
451 void process::ctrl_close(neighbor& nb)
453 comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
456 void process::data_close(neighbor& nb)
458 comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
461 void process::ctrl_receive(double timeout)
466 XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
467 while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
468 if (msg->get_type() != message::CTRL_CLOSE)
469 add_ctrl_recv_mesg(msg->get_size());
470 handle_message(msg, from);
475 void process::data_receive(double timeout)
480 XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
481 while (data_close_pending && comm.data_recv(msg, from, timeout)) {
482 if (msg->get_type() != message::DATA_CLOSE)
483 add_data_recv_mesg(msg->get_size());
484 handle_message(msg, from);
489 void process::handle_message(message* msg, m_host_t from)
491 switch (msg->get_type()) {
492 case message::CTRL: {
493 neighbor* n = rev_neigh[from];
494 n->set_load(msg->get_amount() + n->get_to_send());
495 if (opt::bookkeeping) {
496 double credit = msg->get_credit();
497 expected_load += credit;
498 n->set_credit(n->get_credit() + credit);
502 case message::DATA: {
503 neighbor* n = rev_neigh[from];
504 double ld = msg->get_amount();
506 n->set_credit(n->get_credit() - ld);
509 case message::CTRL_CLOSE:
510 ctrl_close_pending--;
511 close_received = true;
513 case message::DATA_CLOSE:
514 data_close_pending--;
515 close_received = true;
521 #define print_loads_generic(vec, verbose, logp, cat) \
522 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
523 using std::placeholders::_1; \
524 XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected). " \
525 "Neighbor loads:", real_load, expected_load); \
526 std::for_each(vec.begin(), vec.end(), \
527 std::bind(&neighbor::print, _1, verbose, logp, cat)); \
530 void process::print_loads(bool verbose,
531 e_xbt_log_priority_t logp,
532 xbt_log_category_t cat) const
534 print_loads_generic(neigh, verbose, logp, cat);
537 void process::print_loads_p(bool verbose,
538 e_xbt_log_priority_t logp,
539 xbt_log_category_t cat) const
541 print_loads_generic(pneigh, verbose, logp, cat);
544 #undef print_loads_generic