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;
23 int process::process_counter = 0;
24 double process::total_load_average;
25 double process::load_diff_threshold;
29 void sleep_until_date(double& date, double duration)
31 double sleep_duration = date - MSG_get_clock();
32 if (sleep_duration > 0.0)
33 MSG_process_sleep(sleep_duration);
34 date = MSG_get_clock() + duration;
39 process::process(int argc, char* argv[])
41 if (argc < 2 || !(std::istringstream(argv[1]) >> real_load))
42 throw std::invalid_argument("bad or missing initial load parameter");
44 double iload = std::trunc(real_load);
45 if (opt::integer_transfer && real_load != iload) {
46 XBT_WARN("Initial load %g is not an integer. Truncate it.",
51 neigh.assign(argv + 2, argv + argc);
53 pneigh.reserve(neigh.size());
54 for (unsigned i = 0 ; i < neigh.size() ; i++) {
55 neighbor* ptr = &neigh[i];
56 m_host_t host = MSG_get_host_by_name(ptr->get_name());
57 pneigh.push_back(ptr);
58 rev_neigh.insert(std::make_pair(host, ptr));
61 // Note: there should not be race condition with the current
62 // version of Simgrid, when updating the global variables.
64 prev_load_broadcast = -1; // force sending of load on first send_all()
65 expected_load = real_load;
66 total_load_running += real_load;
67 total_load_init += real_load;
73 total_load_average = total_load_running / process_counter;
74 load_diff_threshold = (opt::load_ratio_threshold +
75 opt::avg_load_ratio * total_load_average) / 100.0;
77 ctrl_close_pending = data_close_pending = neigh.size();
78 close_received = false;
81 all_comp_iter = comp_iter = lb_iter = 0;
83 lb_thread = new_msg_thread("loba",
84 std::bind(&process::load_balance_loop, this));
86 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
87 if (!LOG_ISENABLED(logp))
89 std::ostringstream oss;
90 oss << neigh.size() << " neighbor";
92 oss << ESSE(neigh.size()) << ": ";
93 std::transform(neigh.begin(), neigh.end() - 1,
94 std::ostream_iterator<const char*>(oss, ", "),
95 std::mem_fn(&neighbor::get_name));
96 oss << neigh.back().get_name();
98 XBT_LOG(logp, "Got %s.", oss.str().c_str());
99 print_loads(false, logp);
105 total_load_exit += real_load;
106 xbt_assert(received_load == 0.0,
107 "received_load is %g, but should be 0.0 !", received_load);
108 if (opt::log_rate < 0)
110 XBT_INFO("Final load after %d:%d:%d iterations: %g",
111 lb_iter, comp_iter, all_comp_iter, real_load);
112 if (convergence >= 0.0)
113 XBT_INFO("Convergence within %g%% was achieved at time %g",
114 opt::avg_load_ratio, convergence);
116 XBT_INFO("Convergence within %g%% was not achieved",
117 opt::avg_load_ratio);
118 XBT_VERB("Expected load was: %g", expected_load);
119 XBT_VERB("Total computation for this process: %g", get_comp_amount());
120 print_loads(true, xbt_log_priority_debug);
125 if (opt::log_rate >= 0) {
126 XBT_INFO("Initial load: %g", real_load);
127 XBT_VERB("Initial expected load: %g", expected_load);
129 XBT_VERB("Starting...");
132 while (lb_iter <= opt::comp_iter_delay)
135 double sleep_duration = opt::comp_time_delay - MSG_get_clock();
136 if (sleep_duration > 0.0)
137 MSG_process_sleep(sleep_duration);
144 void process::load_balance_loop()
146 using std::placeholders::_1;
148 double next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
149 while (still_running()) {
150 if (lb_iter == opt::comp_iter_delay) {
162 if (!opt::bookkeeping)
163 expected_load = real_load - get_sum_of_to_send();
164 // nothing to do with opt::bookkeeping
166 if (opt::log_rate && lb_iter % opt::log_rate == 0) {
167 XBT_INFO("(%u:%u:%u) current load: %g",
168 lb_iter, comp_iter, all_comp_iter, real_load);
169 XBT_VERB("... expected load: %g", expected_load);
172 if (expected_load > 0.0)
175 print_loads(true, xbt_log_priority_debug);
178 comm.ctrl_flush(false);
179 std::for_each(neigh.begin(), neigh.end(),
180 std::bind(&process::ctrl_send, this, _1));
181 prev_load_broadcast = expected_load;
184 sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
187 XBT_VERB("Going to finalize for %s...", __func__);
188 XBT_DEBUG("send CTRL_CLOSE to %zu neighbor%s",
189 neigh.size(), ESSE(neigh.size()));
190 std::for_each(neigh.begin(), neigh.end(),
191 std::bind(&process::ctrl_close, this, _1));
192 while (ctrl_close_pending) {
193 comm.ctrl_flush(false);
194 XBT_DEBUG("waiting for %d CTRL_CLOSE", ctrl_close_pending);
197 comm.ctrl_flush(true);
200 void process::compute_loop()
202 using std::placeholders::_1;
204 double next_iter_after_date = MSG_get_clock() + opt::min_comp_iter_duration;
205 while (still_running()) {
209 // if there is something to compute, do not block
210 // else, block the duration of an *lb* iteration
211 data_receive(real_load > 0.0 ? 0.0 : opt::min_lb_iter_duration);
214 comm.data_flush(false);
216 real_load += received_load;
218 std::for_each(neigh.begin(), neigh.end(),
219 std::bind(&process::data_send, this, _1));
224 } while (real_load == 0.0);
230 double flops = opt::comp_cost(real_load);
231 m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
232 TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
233 XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
234 MSG_task_execute(task);
235 add_comp_amount(flops);
236 MSG_task_destroy(task);
238 sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
241 XBT_VERB("Going to finalize for %s...", __func__);
243 XBT_DEBUG("send DATA_CLOSE to %zu neighbor%s",
244 neigh.size(), ESSE(neigh.size()));
245 std::for_each(neigh.begin(), neigh.end(),
246 std::bind(&process::data_close, this, _1));
247 while (data_close_pending) {
248 comm.data_flush(false);
249 XBT_DEBUG("waiting for %d DATA_CLOSE", data_close_pending);
252 real_load += received_load;
254 total_load_running -= real_load;
256 comm.data_flush(true);
259 void process::convergence_check()
261 double load_diff = std::fabs(real_load - total_load_average);
262 bool converged = load_diff <= load_diff_threshold;
264 if (convergence >= 0.0) {
266 XBT_VERB("current load has diverged: %g (%.4g%%)",
267 real_load, 100.0 * load_diff / total_load_average);
272 XBT_VERB("current load has converged: %g (%.4g%%)",
273 real_load, 100.0 * load_diff / total_load_average);
274 convergence = MSG_get_clock();
279 bool process::still_running()
281 static bool last_status = true;
286 } else if (opt::exit_request) {
287 XBT_VERB("Global exit requested");
290 } else if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
291 XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
294 } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
295 XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
298 } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
299 XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
302 } else if (opt::exit_on_close && close_received) {
303 XBT_VERB("Close received");
306 } else if (real_load == 0.0 && !data_close_pending) {
307 XBT_VERB("I'm a poor lonesome process, and I have no load...");
310 } else if (100.0 * total_load_running / total_load_init <=
311 opt::load_ratio_threshold) {
312 // fixme: this check should be implemented with a distributed
313 // algorithm, and not a shared global variable!
314 XBT_VERB("No more load to balance in system.");
321 double process::get_sum_of_to_send() const
323 using std::placeholders::_1;
324 using std::placeholders::_2;
326 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
327 std::bind(std::plus<double>(), _1,
328 std::bind(&neighbor::get_to_send, _2)));
331 void process::load_balance()
333 if (lb_iter == 1) // warn only once
334 XBT_WARN("process::load_balance() is a no-op!");
337 void process::send(neighbor& nb, double amount)
339 expected_load -= amount;
340 nb.set_to_send(nb.get_to_send() + amount);
341 nb.set_load(nb.get_load() + amount);
344 void process::ctrl_send(neighbor& nb)
346 double info_to_send = expected_load;
348 if (opt::bookkeeping) { // bookkeeping
349 debt_to_send = nb.get_to_send();
350 if (debt_to_send > 0.0) {
352 nb.set_debt(nb.get_debt() + debt_to_send);
354 } else { // !bookkeeping
357 if (info_to_send != prev_load_broadcast || debt_to_send > 0.0) {
358 message* msg = new message(message::CTRL, info_to_send, debt_to_send);
359 add_ctrl_send_mesg(msg->get_size());
360 comm.ctrl_send(nb.get_ctrl_mbox(), msg);
364 double process::compute_load_to_send(double desired)
366 if (opt::integer_transfer)
367 desired = std::floor(desired);
368 return desired >= opt::min_transfer_amount ? desired : 0.0;
371 void process::data_send(neighbor& nb)
374 if (opt::bookkeeping) { // bookkeeping
375 double excess_load; // load amount we are able to send
377 excess_load = std::max(0.0, real_load - expected_load);
379 excess_load = real_load;
381 double balance = nb.get_debt() - nb.get_credit();
382 load_to_send = std::min(excess_load,
383 std::max(0.0, balance));
385 // adjust load to send (rounding, truncation, etc.)
386 load_to_send = compute_load_to_send(load_to_send);
387 if (load_to_send > 0.0)
388 nb.set_debt(nb.get_debt() - load_to_send);
389 } else { // !bookkeeping
390 load_to_send = compute_load_to_send(nb.get_to_send());
391 if (load_to_send > 0.0)
392 nb.set_to_send(nb.get_to_send() - load_to_send);
394 real_load -= load_to_send;
395 while (load_to_send > 0.0) {
397 if (opt::max_transfer_amount)
398 amount = std::min(load_to_send, opt::max_transfer_amount);
400 amount = load_to_send;
401 message* msg = new message(message::DATA, amount);
402 add_data_send_mesg(msg->get_size());
403 comm.data_send(nb.get_data_mbox(), msg);
404 load_to_send -= amount;
408 void process::ctrl_close(neighbor& nb)
410 comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
413 void process::data_close(neighbor& nb)
415 comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
418 void process::ctrl_receive(double timeout)
423 XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
424 while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
425 if (msg->get_type() != message::CTRL_CLOSE)
426 add_ctrl_recv_mesg(msg->get_size());
427 handle_message(msg, from);
432 void process::data_receive(double timeout)
437 XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
438 while (data_close_pending && comm.data_recv(msg, from, timeout)) {
439 if (msg->get_type() != message::DATA_CLOSE)
440 add_data_recv_mesg(msg->get_size());
441 handle_message(msg, from);
446 void process::handle_message(message* msg, m_host_t from)
448 switch (msg->get_type()) {
449 case message::CTRL: {
450 neighbor* n = rev_neigh[from];
451 n->set_load(msg->get_amount() + n->get_to_send());
452 if (opt::bookkeeping) {
453 double credit = msg->get_credit();
454 expected_load += credit;
455 n->set_credit(n->get_credit() + credit);
459 case message::DATA: {
460 neighbor* n = rev_neigh[from];
461 double ld = msg->get_amount();
463 n->set_credit(n->get_credit() - ld);
466 case message::CTRL_CLOSE:
467 ctrl_close_pending--;
468 close_received = true;
470 case message::DATA_CLOSE:
471 data_close_pending--;
472 close_received = true;
478 #define print_loads_generic(vec, verbose, logp, cat) \
479 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
480 using std::placeholders::_1; \
481 XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected). " \
482 "Neighbor loads:", real_load, expected_load); \
483 std::for_each(vec.begin(), vec.end(), \
484 std::bind(&neighbor::print, _1, verbose, logp, cat)); \
487 void process::print_loads(bool verbose,
488 e_xbt_log_priority_t logp,
489 xbt_log_category_t cat) const
491 print_loads_generic(neigh, verbose, logp, cat);
494 void process::print_loads_p(bool verbose,
495 e_xbt_log_priority_t logp,
496 xbt_log_category_t cat) const
498 print_loads_generic(pneigh, verbose, logp, cat);
501 #undef print_loads_generic