11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc);
19 double process::total_load_init = 0.0;
20 double process::total_load_running = 0.0;
21 double process::total_load_exit = 0.0;
25 void sleep_until_date(double& date, double duration)
27 double sleep_duration = date - MSG_get_clock();
28 if (sleep_duration > 0.0)
29 MSG_process_sleep(sleep_duration);
30 date = MSG_get_clock() + duration;
35 process::process(int argc, char* argv[])
37 if (argc < 2 || !(std::istringstream(argv[1]) >> real_load))
38 throw std::invalid_argument("bad or missing initial load parameter");
40 double iload = std::trunc(real_load);
41 if (opt::integer_transfer && real_load != iload) {
42 XBT_WARN("Initial load %g is not an integer. Truncate it.",
47 neigh.assign(argv + 2, argv + argc);
49 pneigh.reserve(neigh.size());
50 for (unsigned i = 0 ; i < neigh.size() ; i++) {
51 neighbor* ptr = &neigh[i];
52 m_host_t host = MSG_get_host_by_name(ptr->get_name());
53 pneigh.push_back(ptr);
54 rev_neigh.insert(std::make_pair(host, ptr));
57 prev_load_broadcast = -1; // force sending of load on first send_all()
58 expected_load = real_load;
59 total_load_running += real_load;
60 total_load_init += real_load;
63 ctrl_close_pending = data_close_pending = neigh.size();
64 close_received = false;
67 all_comp_iter = comp_iter = lb_iter = 0;
69 lb_thread = new_msg_thread("loba",
70 std::bind(&process::load_balance_loop, this));
72 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
73 if (!LOG_ISENABLED(logp))
75 std::ostringstream oss;
76 oss << neigh.size() << " neighbor";
78 oss << ESSE(neigh.size()) << ": ";
79 std::transform(neigh.begin(), neigh.end() - 1,
80 std::ostream_iterator<const char*>(oss, ", "),
81 std::mem_fn(&neighbor::get_name));
82 oss << neigh.back().get_name();
84 XBT_LOG(logp, "Got %s.", oss.str().c_str());
85 print_loads(false, logp);
91 total_load_exit += real_load;
92 xbt_assert(received_load == 0.0,
93 "received_load is %g, but should be 0.0 !", received_load);
94 if (opt::log_rate < 0)
96 XBT_INFO("Final load after %d:%d:%d iterations: %g",
97 lb_iter, comp_iter, all_comp_iter, real_load);
98 XBT_VERB("Expected load was: %g", expected_load);
99 XBT_VERB("Total computation for this process: %g", get_comp_amount());
100 print_loads(true, xbt_log_priority_debug);
105 if (opt::log_rate >= 0) {
106 XBT_INFO("Initial load: %g", real_load);
107 XBT_VERB("Initial expected load: %g", expected_load);
109 XBT_VERB("Starting...");
112 while (lb_iter <= opt::comp_iter_delay)
115 double sleep_duration = opt::comp_time_delay - MSG_get_clock();
116 if (sleep_duration > 0.0)
117 MSG_process_sleep(sleep_duration);
124 void process::load_balance_loop()
126 using std::placeholders::_1;
128 double next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
129 while (still_running()) {
130 if (lb_iter == opt::comp_iter_delay) {
142 if (!opt::bookkeeping)
143 expected_load = real_load - get_sum_of_to_send();
144 // nothing to do with opt::bookkeeping
146 if (opt::log_rate && lb_iter % opt::log_rate == 0) {
147 XBT_INFO("(%u:%u:%u) current load: %g",
148 lb_iter, comp_iter, all_comp_iter, real_load);
149 XBT_VERB("... expected load: %g", expected_load);
152 if (expected_load > 0.0)
155 print_loads(true, xbt_log_priority_debug);
158 comm.ctrl_flush(false);
159 std::for_each(neigh.begin(), neigh.end(),
160 std::bind(&process::ctrl_send, this, _1));
161 prev_load_broadcast = expected_load;
164 sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
167 XBT_VERB("Going to finalize for %s...", __func__);
168 XBT_DEBUG("send CTRL_CLOSE to %zu neighbor%s",
169 neigh.size(), ESSE(neigh.size()));
170 std::for_each(neigh.begin(), neigh.end(),
171 std::bind(&process::ctrl_close, this, _1));
172 while (ctrl_close_pending) {
173 comm.ctrl_flush(false);
174 XBT_DEBUG("waiting for %d CTRL_CLOSE", ctrl_close_pending);
177 comm.ctrl_flush(true);
180 void process::compute_loop()
182 using std::placeholders::_1;
184 double next_iter_after_date = MSG_get_clock() + opt::min_comp_iter_duration;
185 while (still_running()) {
186 // receive (do not block if there is something to compute)
187 data_receive(real_load > 0.0 ? 0.0 : opt::min_comp_iter_duration);
190 comm.data_flush(false);
192 real_load += received_load;
194 std::for_each(neigh.begin(), neigh.end(),
195 std::bind(&process::data_send, this, _1));
199 if (real_load == 0.0)
204 double flops = opt::comp_cost(real_load);
205 m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
206 TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
207 XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
208 MSG_task_execute(task);
209 add_comp_amount(flops);
210 MSG_task_destroy(task);
212 sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
215 XBT_VERB("Going to finalize for %s...", __func__);
217 XBT_DEBUG("send DATA_CLOSE to %zu neighbor%s",
218 neigh.size(), ESSE(neigh.size()));
219 std::for_each(neigh.begin(), neigh.end(),
220 std::bind(&process::data_close, this, _1));
221 while (data_close_pending) {
222 comm.data_flush(false);
223 XBT_DEBUG("waiting for %d DATA_CLOSE", data_close_pending);
226 real_load += received_load;
228 total_load_running -= real_load;
229 comm.data_flush(true);
232 bool process::still_running()
234 static bool last_status = true;
239 } else if (opt::exit_request) {
240 XBT_VERB("Global exit requested");
243 } else if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
244 XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
247 } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
248 XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
251 } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
252 XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
255 } else if (opt::exit_on_close && close_received) {
256 XBT_VERB("Close received");
259 } else if (real_load == 0.0 && !data_close_pending) {
260 XBT_VERB("I'm a poor lonesome process, and I have no load...");
263 } else if (100.0 * total_load_running / total_load_init <=
264 opt::load_ratio_threshold) {
265 // fixme: this check should be implemented with a distributed
266 // algorithm, and not a shared global variable!
267 XBT_VERB("No more load to balance in system.");
274 double process::get_sum_of_to_send() const
276 using std::placeholders::_1;
277 using std::placeholders::_2;
279 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
280 std::bind(std::plus<double>(), _1,
281 std::bind(&neighbor::get_to_send, _2)));
284 void process::load_balance()
286 if (lb_iter == 1) // warn only once
287 XBT_WARN("process::load_balance() is a no-op!");
290 void process::send(neighbor& nb, double amount)
292 expected_load -= amount;
293 nb.set_to_send(nb.get_to_send() + amount);
294 nb.set_load(nb.get_load() + amount);
297 void process::ctrl_send(neighbor& nb)
299 double info_to_send = expected_load;
301 if (opt::bookkeeping) { // bookkeeping
302 debt_to_send = nb.get_to_send();
303 if (debt_to_send > 0.0) {
305 nb.set_debt(nb.get_debt() + debt_to_send);
307 } else { // !bookkeeping
310 if (info_to_send != prev_load_broadcast || debt_to_send > 0.0) {
311 message* msg = new message(message::CTRL, info_to_send, debt_to_send);
312 add_ctrl_send_mesg(msg->get_size());
313 comm.ctrl_send(nb.get_ctrl_mbox(), msg);
317 double process::compute_load_to_send(double desired)
319 if (opt::integer_transfer)
320 desired = std::floor(desired);
321 return desired >= opt::min_transfer_amount ? desired : 0.0;
324 void process::data_send(neighbor& nb)
327 if (opt::bookkeeping) { // bookkeeping
328 double excess_load; // load amount we are able to send
330 excess_load = std::max(0.0, real_load - expected_load);
332 excess_load = real_load;
334 double balance = nb.get_debt() - nb.get_credit();
335 load_to_send = std::min(excess_load,
336 std::max(0.0, balance));
338 // adjust load to send (rounding, truncation, etc.)
339 load_to_send = compute_load_to_send(load_to_send);
340 if (load_to_send > 0.0)
341 nb.set_debt(nb.get_debt() - load_to_send);
342 } else { // !bookkeeping
343 load_to_send = compute_load_to_send(nb.get_to_send());
344 if (load_to_send > 0.0)
345 nb.set_to_send(nb.get_to_send() - load_to_send);
347 real_load -= load_to_send;
348 while (load_to_send > 0.0) {
350 if (opt::max_transfer_amount)
351 amount = std::min(load_to_send, opt::max_transfer_amount);
353 amount = load_to_send;
354 message* msg = new message(message::DATA, amount);
355 add_data_send_mesg(msg->get_size());
356 comm.data_send(nb.get_data_mbox(), msg);
357 load_to_send -= amount;
361 void process::ctrl_close(neighbor& nb)
363 comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
366 void process::data_close(neighbor& nb)
368 comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
371 void process::ctrl_receive(double timeout)
376 XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
377 while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
378 if (msg->get_type() != message::CTRL_CLOSE)
379 add_ctrl_recv_mesg(msg->get_size());
380 handle_message(msg, from);
385 void process::data_receive(double timeout)
390 XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
391 while (data_close_pending && comm.data_recv(msg, from, timeout)) {
392 if (msg->get_type() != message::DATA_CLOSE)
393 add_data_recv_mesg(msg->get_size());
394 handle_message(msg, from);
399 void process::handle_message(message* msg, m_host_t from)
401 switch (msg->get_type()) {
402 case message::CTRL: {
403 neighbor* n = rev_neigh[from];
404 n->set_load(msg->get_amount() + n->get_to_send());
405 if (opt::bookkeeping) {
406 double credit = msg->get_credit();
407 expected_load += credit;
408 n->set_credit(n->get_credit() + credit);
412 case message::DATA: {
413 neighbor* n = rev_neigh[from];
414 double ld = msg->get_amount();
416 n->set_credit(n->get_credit() - ld);
419 case message::CTRL_CLOSE:
420 ctrl_close_pending--;
421 close_received = true;
423 case message::DATA_CLOSE:
424 data_close_pending--;
425 close_received = true;
431 #define print_loads_generic(vec, verbose, logp, cat) \
432 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
433 using std::placeholders::_1; \
434 XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected). " \
435 "Neighbor loads:", real_load, expected_load); \
436 std::for_each(vec.begin(), vec.end(), \
437 std::bind(&neighbor::print, _1, verbose, logp, cat)); \
440 void process::print_loads(bool verbose,
441 e_xbt_log_priority_t logp,
442 xbt_log_category_t cat) const
444 print_loads_generic(neigh, verbose, logp, cat);
447 void process::print_loads_p(bool verbose,
448 e_xbt_log_priority_t logp,
449 xbt_log_category_t cat) const
451 print_loads_generic(pneigh, verbose, logp, cat);
454 #undef print_loads_generic