+void process::load_balance()
+{
+ if (lb_iter == 1) // warn only once
+ XBT_WARN("process::load_balance() is a no-op!");
+}
+
+void process::send(neighbor& nb, double amount)
+{
+ expected_load -= amount;
+ nb.set_to_send(nb.get_to_send() + amount);
+ nb.set_load(nb.get_load() + amount);
+}
+
+void process::ctrl_send(neighbor& nb)
+{
+ double info_to_send = expected_load;
+ double debt_to_send;
+ if (opt::bookkeeping) { // bookkeeping
+ debt_to_send = nb.get_to_send();
+ if (debt_to_send > 0.0) {
+ nb.set_to_send(0.0);
+ nb.set_debt(nb.get_debt() + debt_to_send);
+ }
+ } else { // !bookkeeping
+ debt_to_send = 0.0;
+ }
+ if (info_to_send != prev_load_broadcast || debt_to_send > 0.0) {
+ message* msg =
+ new message(message::message_type::CTRL, info_to_send, debt_to_send);
+ add_ctrl_send_mesg(msg->get_size());
+ comm.ctrl_send(nb.get_ctrl_mbox(), msg);
+ }
+}
+
+double process::compute_load_to_send(double desired)
+{
+ if (opt::integer_transfer)
+ desired = std::floor(desired);
+ return desired >= opt::min_transfer_amount ? desired : 0.0;
+}
+
+void process::data_send(neighbor& nb)
+{
+ double load_to_send;
+ if (opt::bookkeeping) { // bookkeeping
+ double excess_load; // load amount we are able to send
+ if (opt::egocentric)
+ excess_load = std::max(0.0, real_load - expected_load);
+ else
+ excess_load = real_load;
+
+ double balance;
+ if (nb.get_credit() > 0.0)
+ balance = nb.get_debt() - nb.get_credit();
+ else
+ balance = nb.get_debt();
+ load_to_send = std::min(excess_load,
+ std::max(0.0, balance));
+
+ // adjust load to send (rounding, truncation, etc.)
+ load_to_send = compute_load_to_send(load_to_send);
+ if (load_to_send > 0.0)
+ nb.set_debt(nb.get_debt() - load_to_send);
+ } else { // !bookkeeping
+ load_to_send = compute_load_to_send(nb.get_to_send());
+ if (load_to_send > 0.0)
+ nb.set_to_send(nb.get_to_send() - load_to_send);
+ }
+ real_load -= load_to_send;
+ while (load_to_send > 0.0) {
+ double amount;
+ if (opt::max_transfer_amount)
+ amount = std::min(load_to_send, opt::max_transfer_amount);
+ else
+ amount = load_to_send;
+ message* msg = new message(message::message_type::DATA, amount);
+ add_data_send_mesg(msg->get_size());
+ comm.data_send(nb.get_data_mbox(), msg);
+ load_to_send -= amount;
+ }
+}
+
+void process::ctrl_close(neighbor& nb)
+{
+ comm.ctrl_send(nb.get_ctrl_mbox(),
+ new message(message::message_type::CTRL_CLOSE, 0.0));
+}
+
+void process::data_close(neighbor& nb)
+{
+ comm.data_send(nb.get_data_mbox(),
+ new message(message::message_type::DATA_CLOSE, 0.0));
+}
+
+void process::ctrl_receive(double timeout)
+{
+ message* msg;
+ msg_host_t from;
+
+ XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
+ while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
+ if (msg->get_type() != message::message_type::CTRL_CLOSE)
+ add_ctrl_recv_mesg(msg->get_size());
+ handle_message(msg, from);
+ timeout = 0.0;
+ }
+}
+
+void process::data_receive(double timeout)
+{
+ message* msg;
+ msg_host_t from;
+
+ XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
+ while (data_close_pending && comm.data_recv(msg, from, timeout)) {
+ if (msg->get_type() != message::message_type::DATA_CLOSE)
+ add_data_recv_mesg(msg->get_size());
+ handle_message(msg, from);
+ timeout = 0.0;
+ }
+}
+
+void process::handle_message(message* msg, msg_host_t from)
+{
+ switch (msg->get_type()) {
+ case message::message_type::CTRL: {
+ neighbor* n = rev_neigh[from];
+ n->set_load(msg->get_amount() + n->get_to_send());
+ if (opt::bookkeeping) {
+ double credit = msg->get_credit();
+ expected_load += credit;
+ n->set_credit(n->get_credit() + credit);
+ }
+ break;
+ }
+ case message::message_type::DATA: {
+ neighbor* n = rev_neigh[from];
+ double ld = msg->get_amount();
+ received_load += ld;
+ n->set_credit(n->get_credit() - ld);
+ break;
+ }
+ case message::message_type::CTRL_CLOSE:
+ ctrl_close_pending--;
+ close_received = true;
+ break;
+ case message::message_type::DATA_CLOSE:
+ data_close_pending--;
+ close_received = true;
+ break;
+ }
+ delete msg;
+}
+
+void process::print_loads(bool verbose,
+ e_xbt_log_priority_t logp,
+ xbt_log_category_t cat) const
+{
+ if (!_XBT_LOG_ISENABLEDV((*cat), logp))
+ return;
+ XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected). Neighbor loads:",
+ real_load, expected_load);
+ for (const neighbor& n : neigh)
+ n.print(verbose, logp, cat);
+}
+
+void process::print_loads_p(bool verbose,
+ e_xbt_log_priority_t logp,
+ xbt_log_category_t cat) const
+{
+ if (!_XBT_LOG_ISENABLEDV((*cat), logp))
+ return;
+ XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected). Neighbor loads:",
+ real_load, expected_load);
+ for (const neighbor* n : pneigh)
+ n->print(verbose, logp, cat);
+}
+
+#undef print_loads_generic
+