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::load_diff_threshold;
31 void sleep_until_date(double& date, double duration)
33 double sleep_duration = date - MSG_get_clock();
34 if (sleep_duration > 0.0)
35 MSG_process_sleep(sleep_duration);
36 date = MSG_get_clock() + duration;
41 process::process(int argc, char* argv[])
43 if (argc < 2 || !(std::istringstream(argv[1]) >> real_load))
44 throw std::invalid_argument("bad or missing initial load parameter");
46 double iload = std::trunc(real_load);
47 if (opt::integer_transfer && real_load != iload) {
48 XBT_WARN("Initial load %g is not an integer. Truncate it.",
53 neigh.assign(argv + 2, argv + argc);
55 pneigh.reserve(neigh.size());
56 for (unsigned i = 0 ; i < neigh.size() ; i++) {
57 neighbor* ptr = &neigh[i];
58 m_host_t host = MSG_get_host_by_name(ptr->get_name());
59 pneigh.push_back(ptr);
60 rev_neigh.insert(std::make_pair(host, ptr));
63 prev_load_broadcast = -1; // force sending of load on first send_all()
64 expected_load = real_load;
70 proc_mutex->acquire();
72 total_load_init += real_load;
73 total_load_running += real_load;
74 total_load_average = total_load_running / process_counter;
75 load_diff_threshold = (opt::load_ratio_threshold +
76 opt::avg_load_ratio * total_load_average) / 100.0;
77 proc_mutex->release();
79 ctrl_close_pending = data_close_pending = neigh.size();
80 close_received = false;
83 all_comp_iter = comp_iter = lb_iter = 0;
85 lb_thread = new_msg_thread("loba",
86 std::bind(&process::load_balance_loop, this));
88 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
89 if (!LOG_ISENABLED(logp))
91 std::ostringstream oss;
92 oss << neigh.size() << " neighbor";
94 oss << ESSE(neigh.size()) << ": ";
95 std::transform(neigh.begin(), neigh.end() - 1,
96 std::ostream_iterator<const char*>(oss, ", "),
97 std::mem_fn(&neighbor::get_name));
98 oss << neigh.back().get_name();
100 XBT_LOG(logp, "Got %s.", oss.str().c_str());
101 print_loads(false, logp);
107 proc_mutex->acquire();
108 total_load_exit += real_load;
109 proc_mutex->release();
110 xbt_assert(received_load == 0.0,
111 "received_load is %g, but should be 0.0 !", received_load);
112 if (opt::log_rate < 0)
114 XBT_INFO("Final load after %d:%d:%d iterations: %g",
115 lb_iter, comp_iter, all_comp_iter, real_load);
116 if (convergence >= 0.0)
117 XBT_INFO("Convergence within %g%% was achieved at time %g",
118 opt::avg_load_ratio, convergence);
120 XBT_INFO("Convergence within %g%% was not achieved",
121 opt::avg_load_ratio);
122 XBT_VERB("Expected load was: %g", expected_load);
123 XBT_VERB("Total computation for this process: %g", get_comp_amount());
124 print_loads(true, xbt_log_priority_debug);
127 double process::get_iter_deviation() const
129 double average_cost = opt::comp_cost(total_load_average); // fixme: get locked?
130 // Do not count idle periods
131 double comp_iter_opt = acc.comp_amount / average_cost;
133 // Add iterations that could have been achieved while beeing idle
134 // (kept for documentation)
135 double self_speed = MSG_get_host_speed(MSG_host_self());
136 double average_duration = average_cost / self_speed;
137 comp_iter_opt += idle_duration / average_duration;
139 return comp_iter - comp_iter_opt;
144 if (opt::log_rate >= 0) {
145 XBT_INFO("Initial load: %g", real_load);
146 XBT_VERB("Initial expected load: %g", expected_load);
148 XBT_VERB("Starting...");
151 while (lb_iter <= opt::comp_iter_delay)
154 double sleep_duration = opt::comp_time_delay - MSG_get_clock();
155 if (sleep_duration > 0.0)
156 MSG_process_sleep(sleep_duration);
163 void process::load_balance_loop()
165 using std::placeholders::_1;
167 double next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
168 while (still_running()) {
169 if (lb_iter == opt::comp_iter_delay) {
181 if (!opt::bookkeeping)
182 expected_load = real_load - get_sum_of_to_send();
183 // nothing to do with opt::bookkeeping
185 if (opt::log_rate && lb_iter % opt::log_rate == 0) {
186 XBT_INFO("(%u:%u:%u) current load: %g",
187 lb_iter, comp_iter, all_comp_iter, real_load);
188 XBT_VERB("... expected load: %g", expected_load);
191 if (expected_load > 0.0)
194 print_loads(true, xbt_log_priority_debug);
197 comm.ctrl_flush(false);
198 std::for_each(neigh.begin(), neigh.end(),
199 std::bind(&process::ctrl_send, this, _1));
200 prev_load_broadcast = expected_load;
203 sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
206 XBT_VERB("Going to finalize for %s...", __func__);
207 XBT_DEBUG("send CTRL_CLOSE to %zu neighbor%s",
208 neigh.size(), ESSE(neigh.size()));
209 std::for_each(neigh.begin(), neigh.end(),
210 std::bind(&process::ctrl_close, this, _1));
211 while (ctrl_close_pending) {
212 comm.ctrl_flush(false);
213 XBT_DEBUG("waiting for %d CTRL_CLOSE", ctrl_close_pending);
216 comm.ctrl_flush(true);
219 void process::compute_loop()
221 using std::placeholders::_1;
223 double next_iter_after_date = MSG_get_clock() + opt::min_comp_iter_duration;
224 double idle_since_date = 0.0;
225 while (still_running()) {
227 // if there is something to compute, do not block
228 // else, block the duration of an *lb* iteration
229 data_receive(real_load > 0.0 ? 0.0 : opt::min_lb_iter_duration);
232 comm.data_flush(false);
234 real_load += received_load;
236 std::for_each(neigh.begin(), neigh.end(),
237 std::bind(&process::data_send, this, _1));
241 if (real_load == 0.0)
247 idle_duration += MSG_get_clock() - idle_since_date;
249 double flops = opt::comp_cost(real_load);
250 m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
251 TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
252 XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
253 MSG_task_execute(task);
254 add_comp_amount(flops);
255 MSG_task_destroy(task);
257 idle_since_date = MSG_get_clock();
259 sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
262 XBT_VERB("Going to finalize for %s...", __func__);
263 // Note: idle duration is not counted during finalization
265 XBT_DEBUG("send DATA_CLOSE to %zu neighbor%s",
266 neigh.size(), ESSE(neigh.size()));
267 std::for_each(neigh.begin(), neigh.end(),
268 std::bind(&process::data_close, this, _1));
269 while (data_close_pending) {
270 comm.data_flush(false);
271 XBT_DEBUG("waiting for %d DATA_CLOSE", data_close_pending);
274 real_load += received_load;
276 proc_mutex->acquire();
277 total_load_running -= real_load;
278 proc_mutex->release();
280 comm.data_flush(true);
283 void process::convergence_check()
285 double average = total_load_average; // fixme: get locked?
286 double load_diff = std::fabs(real_load - average);
287 bool converged = load_diff <= load_diff_threshold;
289 if (convergence >= 0.0) {
291 XBT_VERB("current load has diverged: %g (%.4g%%)",
292 real_load, 100.0 * load_diff / average);
297 XBT_VERB("current load has converged: %g (%.4g%%)",
298 real_load, 100.0 * load_diff / average);
299 convergence = MSG_get_clock();
304 bool process::still_running()
306 static bool last_status = true;
311 } else if (opt::exit_request) {
312 XBT_VERB("Global exit requested");
315 } else if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
316 XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
319 } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
320 XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
323 } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
324 XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
327 } else if (opt::exit_on_close && close_received) {
328 XBT_VERB("Close received");
331 } else if (real_load == 0.0 && !data_close_pending) {
332 XBT_VERB("I'm a poor lonesome process, and I have no load...");
335 } else if (100.0 * total_load_running / total_load_init <=
336 opt::load_ratio_threshold) { // fixme: get locked?
337 // fixme: this check should be implemented with a distributed
338 // algorithm, and not a shared global variable!
339 XBT_VERB("No more load to balance in system.");
346 double process::get_sum_of_to_send() const
348 using std::placeholders::_1;
349 using std::placeholders::_2;
351 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
352 std::bind(std::plus<double>(), _1,
353 std::bind(&neighbor::get_to_send, _2)));
356 void process::load_balance()
358 if (lb_iter == 1) // warn only once
359 XBT_WARN("process::load_balance() is a no-op!");
362 void process::send(neighbor& nb, double amount)
364 expected_load -= amount;
365 nb.set_to_send(nb.get_to_send() + amount);
366 nb.set_load(nb.get_load() + amount);
369 void process::ctrl_send(neighbor& nb)
371 double info_to_send = expected_load;
373 if (opt::bookkeeping) { // bookkeeping
374 debt_to_send = nb.get_to_send();
375 if (debt_to_send > 0.0) {
377 nb.set_debt(nb.get_debt() + debt_to_send);
379 } else { // !bookkeeping
382 if (info_to_send != prev_load_broadcast || debt_to_send > 0.0) {
383 message* msg = new message(message::CTRL, info_to_send, debt_to_send);
384 add_ctrl_send_mesg(msg->get_size());
385 comm.ctrl_send(nb.get_ctrl_mbox(), msg);
389 double process::compute_load_to_send(double desired)
391 if (opt::integer_transfer)
392 desired = std::floor(desired);
393 return desired >= opt::min_transfer_amount ? desired : 0.0;
396 void process::data_send(neighbor& nb)
399 if (opt::bookkeeping) { // bookkeeping
400 double excess_load; // load amount we are able to send
402 excess_load = std::max(0.0, real_load - expected_load);
404 excess_load = real_load;
406 double balance = nb.get_debt() - nb.get_credit();
407 load_to_send = std::min(excess_load,
408 std::max(0.0, balance));
410 // adjust load to send (rounding, truncation, etc.)
411 load_to_send = compute_load_to_send(load_to_send);
412 if (load_to_send > 0.0)
413 nb.set_debt(nb.get_debt() - load_to_send);
414 } else { // !bookkeeping
415 load_to_send = compute_load_to_send(nb.get_to_send());
416 if (load_to_send > 0.0)
417 nb.set_to_send(nb.get_to_send() - load_to_send);
419 real_load -= load_to_send;
420 while (load_to_send > 0.0) {
422 if (opt::max_transfer_amount)
423 amount = std::min(load_to_send, opt::max_transfer_amount);
425 amount = load_to_send;
426 message* msg = new message(message::DATA, amount);
427 add_data_send_mesg(msg->get_size());
428 comm.data_send(nb.get_data_mbox(), msg);
429 load_to_send -= amount;
433 void process::ctrl_close(neighbor& nb)
435 comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
438 void process::data_close(neighbor& nb)
440 comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
443 void process::ctrl_receive(double timeout)
448 XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
449 while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
450 if (msg->get_type() != message::CTRL_CLOSE)
451 add_ctrl_recv_mesg(msg->get_size());
452 handle_message(msg, from);
457 void process::data_receive(double timeout)
462 XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
463 while (data_close_pending && comm.data_recv(msg, from, timeout)) {
464 if (msg->get_type() != message::DATA_CLOSE)
465 add_data_recv_mesg(msg->get_size());
466 handle_message(msg, from);
471 void process::handle_message(message* msg, m_host_t from)
473 switch (msg->get_type()) {
474 case message::CTRL: {
475 neighbor* n = rev_neigh[from];
476 n->set_load(msg->get_amount() + n->get_to_send());
477 if (opt::bookkeeping) {
478 double credit = msg->get_credit();
479 expected_load += credit;
480 n->set_credit(n->get_credit() + credit);
484 case message::DATA: {
485 neighbor* n = rev_neigh[from];
486 double ld = msg->get_amount();
488 n->set_credit(n->get_credit() - ld);
491 case message::CTRL_CLOSE:
492 ctrl_close_pending--;
493 close_received = true;
495 case message::DATA_CLOSE:
496 data_close_pending--;
497 close_received = true;
503 #define print_loads_generic(vec, verbose, logp, cat) \
504 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
505 using std::placeholders::_1; \
506 XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected). " \
507 "Neighbor loads:", real_load, expected_load); \
508 std::for_each(vec.begin(), vec.end(), \
509 std::bind(&neighbor::print, _1, verbose, logp, cat)); \
512 void process::print_loads(bool verbose,
513 e_xbt_log_priority_t logp,
514 xbt_log_category_t cat) const
516 print_loads_generic(neigh, verbose, logp, cat);
519 void process::print_loads_p(bool verbose,
520 e_xbt_log_priority_t logp,
521 xbt_log_category_t cat) const
523 print_loads_generic(pneigh, verbose, logp, cat);
526 #undef print_loads_generic