10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc);
18 mutex_t *process::proc_mutex;
20 double process::total_load_init = 0.0;
21 double process::total_load_running = 0.0;
22 double process::total_load_exit = 0.0;
24 int process::process_counter = 0;
25 double process::total_load_average;
26 double process::average_load_ratio;
27 double process::load_diff_threshold;
29 std::atomic<int> process::convergence_counter(0);
33 void sleep_until_date(double& date, double duration)
35 double sleep_duration = date - MSG_get_clock();
36 if (sleep_duration > 0.0)
37 MSG_process_sleep(sleep_duration);
38 date = MSG_get_clock() + duration;
43 process::process(int argc, char* argv[])
45 if (argc < 2 || !(std::istringstream(argv[1]) >> real_load))
46 throw std::invalid_argument("bad or missing initial load parameter");
48 double iload = std::trunc(real_load);
49 if (opt::integer_transfer && real_load != iload) {
50 XBT_WARN("Initial load %g is not an integer. Truncate it.",
55 neigh.assign(argv + 2, argv + argc);
57 pneigh.reserve(neigh.size());
58 for (unsigned i = 0 ; i < neigh.size() ; i++) {
59 neighbor* ptr = &neigh[i];
60 msg_host_t host = MSG_get_host_by_name(ptr->get_name());
61 pneigh.push_back(ptr);
62 rev_neigh.insert(std::make_pair(host, ptr));
65 prev_load_broadcast = -1; // force sending of load on first send_all()
66 expected_load = real_load;
72 proc_mutex->acquire();
74 convergence_counter++;
75 total_load_init += real_load;
76 total_load_running += real_load;
77 total_load_average = total_load_running / process_counter;
78 if (opt::avg_load_ratio >= 0.0)
79 average_load_ratio = opt::avg_load_ratio;
81 average_load_ratio = 100.0 *
82 (process_counter / -opt::avg_load_ratio) / total_load_average;
83 load_diff_threshold = (opt::load_ratio_threshold +
84 average_load_ratio * total_load_average) / 100.0;
85 proc_mutex->release();
87 ctrl_close_pending = data_close_pending = neigh.size();
88 close_received = false;
91 all_comp_iter = comp_iter = lb_iter = 0;
93 lb_thread = new_msg_thread("loba",
94 std::bind(&process::load_balance_loop, this));
96 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
97 if (!LOG_ISENABLED(logp))
99 std::ostringstream oss;
100 oss << neigh.size() << " neighbor";
101 if (!neigh.empty()) {
102 oss << ESSE(neigh.size()) << ": ";
103 std::transform(neigh.begin(), neigh.end() - 1,
104 std::ostream_iterator<const char*>(oss, ", "),
105 std::mem_fn(&neighbor::get_name));
106 oss << neigh.back().get_name();
108 XBT_LOG(logp, "Got %s.", oss.str().c_str());
109 print_loads(false, logp);
115 proc_mutex->acquire();
116 total_load_exit += real_load;
117 proc_mutex->release();
118 xbt_assert(received_load == 0.0,
119 "received_load is %g, but should be 0.0 !", received_load);
120 if (opt::log_rate < 0)
122 XBT_INFO("Final load after %d:%d:%d iterations: %g",
123 lb_iter, comp_iter, all_comp_iter, real_load);
124 if (convergence >= 0.0)
125 XBT_INFO("Convergence within %g%% was achieved at time %g",
126 average_load_ratio, convergence);
128 XBT_INFO("Convergence within %g%% was not achieved",
130 XBT_VERB("Expected load was: %g", expected_load);
131 XBT_VERB("Total computation for this process: %g", get_comp_amount());
132 print_loads(true, xbt_log_priority_debug);
135 double process::get_iter_deviation() const
137 double average_cost = opt::comp_cost(total_load_average); // fixme: get locked?
138 // Do not count idle periods
139 double comp_iter_opt = acc.comp_amount / average_cost;
141 // Add iterations that could have been achieved while beeing idle
142 // (kept for documentation)
143 double self_speed = MSG_get_host_speed(MSG_host_self());
144 double average_duration = average_cost / self_speed;
145 comp_iter_opt += idle_duration / average_duration;
147 return comp_iter - comp_iter_opt;
152 if (opt::log_rate >= 0) {
153 XBT_INFO("Initial load: %g", real_load);
154 XBT_VERB("Initial expected load: %g", expected_load);
156 XBT_VERB("Starting...");
159 while (lb_iter <= opt::comp_iter_delay)
162 double sleep_duration = opt::comp_time_delay - MSG_get_clock();
163 if (sleep_duration > 0.0)
164 MSG_process_sleep(sleep_duration);
171 void process::load_balance_loop()
173 using std::placeholders::_1;
175 double next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
176 while (still_running()) {
177 if (lb_iter == opt::comp_iter_delay) {
189 if (!opt::bookkeeping)
190 expected_load = real_load - get_sum_of_to_send();
191 // nothing to do with opt::bookkeeping
193 if (opt::log_rate && lb_iter % opt::log_rate == 0) {
194 XBT_INFO("(%u:%u:%u) current load: %g",
195 lb_iter, comp_iter, all_comp_iter, real_load);
196 XBT_VERB("... expected load: %g", expected_load);
199 if (expected_load > 0.0)
202 print_loads(true, xbt_log_priority_debug);
205 comm.ctrl_flush(false);
206 std::for_each(neigh.begin(), neigh.end(),
207 std::bind(&process::ctrl_send, this, _1));
208 prev_load_broadcast = expected_load;
211 sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
214 XBT_VERB("Going to finalize for %s...", __func__);
215 XBT_DEBUG("send CTRL_CLOSE to %zu neighbor%s",
216 neigh.size(), ESSE(neigh.size()));
217 std::for_each(neigh.begin(), neigh.end(),
218 std::bind(&process::ctrl_close, this, _1));
219 while (ctrl_close_pending) {
220 comm.ctrl_flush(false);
221 XBT_DEBUG("waiting for %d CTRL_CLOSE", ctrl_close_pending);
224 comm.ctrl_flush(true);
227 void process::compute_loop()
229 using std::placeholders::_1;
231 double next_iter_after_date = MSG_get_clock() + opt::min_comp_iter_duration;
232 double idle_since_date = 0.0;
233 while (still_running()) {
235 // if there is something to compute, do not block
236 // else, block the duration of an *lb* iteration
237 data_receive(real_load > 0.0 ? 0.0 : opt::min_lb_iter_duration);
240 comm.data_flush(false);
242 real_load += received_load;
244 std::for_each(neigh.begin(), neigh.end(),
245 std::bind(&process::data_send, this, _1));
249 if (real_load == 0.0)
255 idle_duration += MSG_get_clock() - idle_since_date;
257 double flops = opt::comp_cost(real_load);
258 msg_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
259 // MSG_task_set_category(task, TRACE_CAT_COMP);
260 XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
261 MSG_task_execute(task);
262 add_comp_amount(flops);
263 MSG_task_destroy(task);
265 idle_since_date = MSG_get_clock();
267 sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
270 XBT_VERB("Going to finalize for %s...", __func__);
271 // Note: idle duration is not counted during finalization
273 XBT_DEBUG("send DATA_CLOSE to %zu neighbor%s",
274 neigh.size(), ESSE(neigh.size()));
275 std::for_each(neigh.begin(), neigh.end(),
276 std::bind(&process::data_close, this, _1));
277 while (data_close_pending) {
278 comm.data_flush(false);
279 XBT_DEBUG("waiting for %d DATA_CLOSE", data_close_pending);
282 real_load += received_load;
284 proc_mutex->acquire();
285 total_load_running -= real_load;
286 proc_mutex->release();
288 comm.data_flush(true);
291 void process::convergence_check()
293 double average = total_load_average; // fixme: get locked?
294 double load_diff = std::fabs(real_load - average);
295 bool converged = load_diff <= load_diff_threshold;
298 if (convergence < 0) {
299 XBT_VERB("current load has converged: %g (%.4g%%)",
300 real_load, 100.0 * load_diff / average);
301 convergence = MSG_get_clock();
302 local_convergence_counter = opt::exit_on_convergence;
304 if (local_convergence_counter > 0 && --local_convergence_counter == 0)
305 --convergence_counter;
307 if (convergence >= 0.0) {
308 XBT_VERB("current load has diverged: %g (%.4g%%)",
309 real_load, 100.0 * load_diff / average);
311 if (local_convergence_counter == 0)
312 ++convergence_counter;
317 bool process::still_running()
319 static bool last_status = true;
324 } else if (opt::exit_request) {
325 XBT_VERB("Global exit requested");
328 } else if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
329 XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
332 } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
333 XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
336 } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
337 XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
340 } else if (opt::exit_on_convergence && convergence_counter == 0) {
341 XBT_VERB("Global convergence detected");
344 } else if (opt::exit_on_close && close_received) {
345 XBT_VERB("Close received");
348 } else if (real_load == 0.0 && !data_close_pending) {
349 XBT_VERB("I'm a poor lonesome process, and I have no load...");
352 } else if (100.0 * total_load_running / total_load_init <=
353 opt::load_ratio_threshold) { // fixme: get locked?
354 // fixme: this check should be implemented with a distributed
355 // algorithm, and not a shared global variable!
356 XBT_VERB("No more load to balance in system.");
363 double process::get_sum_of_to_send() const
365 using std::placeholders::_1;
366 using std::placeholders::_2;
368 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
369 std::bind(std::plus<double>(), _1,
370 std::bind(&neighbor::get_to_send, _2)));
373 void process::load_balance()
375 if (lb_iter == 1) // warn only once
376 XBT_WARN("process::load_balance() is a no-op!");
379 void process::send(neighbor& nb, double amount)
381 expected_load -= amount;
382 nb.set_to_send(nb.get_to_send() + amount);
383 nb.set_load(nb.get_load() + amount);
386 void process::ctrl_send(neighbor& nb)
388 double info_to_send = expected_load;
390 if (opt::bookkeeping) { // bookkeeping
391 debt_to_send = nb.get_to_send();
392 if (debt_to_send > 0.0) {
394 nb.set_debt(nb.get_debt() + debt_to_send);
396 } else { // !bookkeeping
399 if (info_to_send != prev_load_broadcast || debt_to_send > 0.0) {
400 message* msg = new message(message::CTRL, info_to_send, debt_to_send);
401 add_ctrl_send_mesg(msg->get_size());
402 comm.ctrl_send(nb.get_ctrl_mbox(), msg);
406 double process::compute_load_to_send(double desired)
408 if (opt::integer_transfer)
409 desired = std::floor(desired);
410 return desired >= opt::min_transfer_amount ? desired : 0.0;
413 void process::data_send(neighbor& nb)
416 if (opt::bookkeeping) { // bookkeeping
417 double excess_load; // load amount we are able to send
419 excess_load = std::max(0.0, real_load - expected_load);
421 excess_load = real_load;
424 if (nb.get_credit() > 0.0)
425 balance = nb.get_debt() - nb.get_credit();
427 balance = nb.get_debt();
428 load_to_send = std::min(excess_load,
429 std::max(0.0, balance));
431 // adjust load to send (rounding, truncation, etc.)
432 load_to_send = compute_load_to_send(load_to_send);
433 if (load_to_send > 0.0)
434 nb.set_debt(nb.get_debt() - load_to_send);
435 } else { // !bookkeeping
436 load_to_send = compute_load_to_send(nb.get_to_send());
437 if (load_to_send > 0.0)
438 nb.set_to_send(nb.get_to_send() - load_to_send);
440 real_load -= load_to_send;
441 while (load_to_send > 0.0) {
443 if (opt::max_transfer_amount)
444 amount = std::min(load_to_send, opt::max_transfer_amount);
446 amount = load_to_send;
447 message* msg = new message(message::DATA, amount);
448 add_data_send_mesg(msg->get_size());
449 comm.data_send(nb.get_data_mbox(), msg);
450 load_to_send -= amount;
454 void process::ctrl_close(neighbor& nb)
456 comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
459 void process::data_close(neighbor& nb)
461 comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
464 void process::ctrl_receive(double timeout)
469 XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
470 while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
471 if (msg->get_type() != message::CTRL_CLOSE)
472 add_ctrl_recv_mesg(msg->get_size());
473 handle_message(msg, from);
478 void process::data_receive(double timeout)
483 XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
484 while (data_close_pending && comm.data_recv(msg, from, timeout)) {
485 if (msg->get_type() != message::DATA_CLOSE)
486 add_data_recv_mesg(msg->get_size());
487 handle_message(msg, from);
492 void process::handle_message(message* msg, msg_host_t from)
494 switch (msg->get_type()) {
495 case message::CTRL: {
496 neighbor* n = rev_neigh[from];
497 n->set_load(msg->get_amount() + n->get_to_send());
498 if (opt::bookkeeping) {
499 double credit = msg->get_credit();
500 expected_load += credit;
501 n->set_credit(n->get_credit() + credit);
505 case message::DATA: {
506 neighbor* n = rev_neigh[from];
507 double ld = msg->get_amount();
509 n->set_credit(n->get_credit() - ld);
512 case message::CTRL_CLOSE:
513 ctrl_close_pending--;
514 close_received = true;
516 case message::DATA_CLOSE:
517 data_close_pending--;
518 close_received = true;
524 #define print_loads_generic(vec, verbose, logp, cat) \
525 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
526 using std::placeholders::_1; \
527 XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected). " \
528 "Neighbor loads:", real_load, expected_load); \
529 std::for_each(vec.begin(), vec.end(), \
530 std::bind(&neighbor::print, _1, verbose, logp, cat)); \
533 void process::print_loads(bool verbose,
534 e_xbt_log_priority_t logp,
535 xbt_log_category_t cat) const
537 print_loads_generic(neigh, verbose, logp, cat);
540 void process::print_loads_p(bool verbose,
541 e_xbt_log_priority_t logp,
542 xbt_log_category_t cat) const
544 print_loads_generic(pneigh, verbose, logp, cat);
547 #undef print_loads_generic