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;
28 void sleep_until_date(double& date, double duration)
30 double sleep_duration = date - MSG_get_clock();
31 if (sleep_duration > 0.0)
32 MSG_process_sleep(sleep_duration);
33 date = MSG_get_clock() + duration;
38 process::process(int argc, char* argv[])
40 if (argc < 2 || !(std::istringstream(argv[1]) >> real_load))
41 throw std::invalid_argument("bad or missing initial load parameter");
43 double iload = std::trunc(real_load);
44 if (opt::integer_transfer && real_load != iload) {
45 XBT_WARN("Initial load %g is not an integer. Truncate it.",
50 neigh.assign(argv + 2, argv + argc);
52 pneigh.reserve(neigh.size());
53 for (unsigned i = 0 ; i < neigh.size() ; i++) {
54 neighbor* ptr = &neigh[i];
55 m_host_t host = MSG_get_host_by_name(ptr->get_name());
56 pneigh.push_back(ptr);
57 rev_neigh.insert(std::make_pair(host, ptr));
60 // Note: there should not be race condition with the current
61 // version of Simgrid, when updating the global variables.
63 prev_load_broadcast = -1; // force sending of load on first send_all()
64 expected_load = real_load;
65 total_load_running += real_load;
66 total_load_init += real_load;
72 total_load_average = total_load_running / process_counter;
74 ctrl_close_pending = data_close_pending = neigh.size();
75 close_received = false;
78 all_comp_iter = comp_iter = lb_iter = 0;
80 lb_thread = new_msg_thread("loba",
81 std::bind(&process::load_balance_loop, this));
83 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
84 if (!LOG_ISENABLED(logp))
86 std::ostringstream oss;
87 oss << neigh.size() << " neighbor";
89 oss << ESSE(neigh.size()) << ": ";
90 std::transform(neigh.begin(), neigh.end() - 1,
91 std::ostream_iterator<const char*>(oss, ", "),
92 std::mem_fn(&neighbor::get_name));
93 oss << neigh.back().get_name();
95 XBT_LOG(logp, "Got %s.", oss.str().c_str());
96 print_loads(false, logp);
102 total_load_exit += real_load;
103 xbt_assert(received_load == 0.0,
104 "received_load is %g, but should be 0.0 !", received_load);
105 if (opt::log_rate < 0)
107 XBT_INFO("Final load after %d:%d:%d iterations: %g",
108 lb_iter, comp_iter, all_comp_iter, real_load);
109 if (convergence >= 0.0)
110 XBT_INFO("Convergence within %g%% was achieved at time %g",
111 opt::avg_load_ratio, convergence);
113 XBT_INFO("Convergence within %g%% was not achieved",
114 opt::avg_load_ratio);
115 XBT_VERB("Expected load was: %g", expected_load);
116 XBT_VERB("Total computation for this process: %g", get_comp_amount());
117 print_loads(true, xbt_log_priority_debug);
122 if (opt::log_rate >= 0) {
123 XBT_INFO("Initial load: %g", real_load);
124 XBT_VERB("Initial expected load: %g", expected_load);
126 XBT_VERB("Starting...");
129 while (lb_iter <= opt::comp_iter_delay)
132 double sleep_duration = opt::comp_time_delay - MSG_get_clock();
133 if (sleep_duration > 0.0)
134 MSG_process_sleep(sleep_duration);
141 void process::load_balance_loop()
143 using std::placeholders::_1;
145 double next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
146 while (still_running()) {
147 if (lb_iter == opt::comp_iter_delay) {
159 if (!opt::bookkeeping)
160 expected_load = real_load - get_sum_of_to_send();
161 // nothing to do with opt::bookkeeping
163 if (opt::log_rate && lb_iter % opt::log_rate == 0) {
164 XBT_INFO("(%u:%u:%u) current load: %g",
165 lb_iter, comp_iter, all_comp_iter, real_load);
166 XBT_VERB("... expected load: %g", expected_load);
169 if (expected_load > 0.0)
172 print_loads(true, xbt_log_priority_debug);
175 comm.ctrl_flush(false);
176 std::for_each(neigh.begin(), neigh.end(),
177 std::bind(&process::ctrl_send, this, _1));
178 prev_load_broadcast = expected_load;
181 sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
184 XBT_VERB("Going to finalize for %s...", __func__);
185 XBT_DEBUG("send CTRL_CLOSE to %zu neighbor%s",
186 neigh.size(), ESSE(neigh.size()));
187 std::for_each(neigh.begin(), neigh.end(),
188 std::bind(&process::ctrl_close, this, _1));
189 while (ctrl_close_pending) {
190 comm.ctrl_flush(false);
191 XBT_DEBUG("waiting for %d CTRL_CLOSE", ctrl_close_pending);
194 comm.ctrl_flush(true);
197 void process::compute_loop()
199 using std::placeholders::_1;
201 double next_iter_after_date = MSG_get_clock() + opt::min_comp_iter_duration;
202 while (still_running()) {
203 // receive (do not block if there is something to compute)
204 data_receive(real_load > 0.0 ? 0.0 : opt::min_comp_iter_duration);
207 comm.data_flush(false);
209 real_load += received_load;
211 std::for_each(neigh.begin(), neigh.end(),
212 std::bind(&process::data_send, this, _1));
216 if (real_load == 0.0)
220 100.0 * std::fabs(real_load / total_load_average - 1.0);
221 if (convergence >= 0.0) {
222 if (load_ratio > opt::avg_load_ratio) {
223 XBT_VERB("current load has diverged: %g (%.4g%%)",
224 real_load, load_ratio);
228 if (load_ratio <= opt::avg_load_ratio) {
229 XBT_VERB("current load has converged: %g (%.4g%%)",
230 real_load, load_ratio);
231 convergence = MSG_get_clock();
237 double flops = opt::comp_cost(real_load);
238 m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
239 TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
240 XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
241 MSG_task_execute(task);
242 add_comp_amount(flops);
243 MSG_task_destroy(task);
245 sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
248 XBT_VERB("Going to finalize for %s...", __func__);
250 XBT_DEBUG("send DATA_CLOSE to %zu neighbor%s",
251 neigh.size(), ESSE(neigh.size()));
252 std::for_each(neigh.begin(), neigh.end(),
253 std::bind(&process::data_close, this, _1));
254 while (data_close_pending) {
255 comm.data_flush(false);
256 XBT_DEBUG("waiting for %d DATA_CLOSE", data_close_pending);
259 real_load += received_load;
261 total_load_running -= real_load;
262 comm.data_flush(true);
265 bool process::still_running()
267 static bool last_status = true;
272 } else if (opt::exit_request) {
273 XBT_VERB("Global exit requested");
276 } else if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
277 XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
280 } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
281 XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
284 } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
285 XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
288 } else if (opt::exit_on_close && close_received) {
289 XBT_VERB("Close received");
292 } else if (real_load == 0.0 && !data_close_pending) {
293 XBT_VERB("I'm a poor lonesome process, and I have no load...");
296 } else if (100.0 * total_load_running / total_load_init <=
297 opt::load_ratio_threshold) {
298 // fixme: this check should be implemented with a distributed
299 // algorithm, and not a shared global variable!
300 XBT_VERB("No more load to balance in system.");
307 double process::get_sum_of_to_send() const
309 using std::placeholders::_1;
310 using std::placeholders::_2;
312 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
313 std::bind(std::plus<double>(), _1,
314 std::bind(&neighbor::get_to_send, _2)));
317 void process::load_balance()
319 if (lb_iter == 1) // warn only once
320 XBT_WARN("process::load_balance() is a no-op!");
323 void process::send(neighbor& nb, double amount)
325 expected_load -= amount;
326 nb.set_to_send(nb.get_to_send() + amount);
327 nb.set_load(nb.get_load() + amount);
330 void process::ctrl_send(neighbor& nb)
332 double info_to_send = expected_load;
334 if (opt::bookkeeping) { // bookkeeping
335 debt_to_send = nb.get_to_send();
336 if (debt_to_send > 0.0) {
338 nb.set_debt(nb.get_debt() + debt_to_send);
340 } else { // !bookkeeping
343 if (info_to_send != prev_load_broadcast || debt_to_send > 0.0) {
344 message* msg = new message(message::CTRL, info_to_send, debt_to_send);
345 add_ctrl_send_mesg(msg->get_size());
346 comm.ctrl_send(nb.get_ctrl_mbox(), msg);
350 double process::compute_load_to_send(double desired)
352 if (opt::integer_transfer)
353 desired = std::floor(desired);
354 return desired >= opt::min_transfer_amount ? desired : 0.0;
357 void process::data_send(neighbor& nb)
360 if (opt::bookkeeping) { // bookkeeping
361 double excess_load; // load amount we are able to send
363 excess_load = std::max(0.0, real_load - expected_load);
365 excess_load = real_load;
367 double balance = nb.get_debt() - nb.get_credit();
368 load_to_send = std::min(excess_load,
369 std::max(0.0, balance));
371 // adjust load to send (rounding, truncation, etc.)
372 load_to_send = compute_load_to_send(load_to_send);
373 if (load_to_send > 0.0)
374 nb.set_debt(nb.get_debt() - load_to_send);
375 } else { // !bookkeeping
376 load_to_send = compute_load_to_send(nb.get_to_send());
377 if (load_to_send > 0.0)
378 nb.set_to_send(nb.get_to_send() - load_to_send);
380 real_load -= load_to_send;
381 while (load_to_send > 0.0) {
383 if (opt::max_transfer_amount)
384 amount = std::min(load_to_send, opt::max_transfer_amount);
386 amount = load_to_send;
387 message* msg = new message(message::DATA, amount);
388 add_data_send_mesg(msg->get_size());
389 comm.data_send(nb.get_data_mbox(), msg);
390 load_to_send -= amount;
394 void process::ctrl_close(neighbor& nb)
396 comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
399 void process::data_close(neighbor& nb)
401 comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
404 void process::ctrl_receive(double timeout)
409 XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
410 while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
411 if (msg->get_type() != message::CTRL_CLOSE)
412 add_ctrl_recv_mesg(msg->get_size());
413 handle_message(msg, from);
418 void process::data_receive(double timeout)
423 XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
424 while (data_close_pending && comm.data_recv(msg, from, timeout)) {
425 if (msg->get_type() != message::DATA_CLOSE)
426 add_data_recv_mesg(msg->get_size());
427 handle_message(msg, from);
432 void process::handle_message(message* msg, m_host_t from)
434 switch (msg->get_type()) {
435 case message::CTRL: {
436 neighbor* n = rev_neigh[from];
437 n->set_load(msg->get_amount() + n->get_to_send());
438 if (opt::bookkeeping) {
439 double credit = msg->get_credit();
440 expected_load += credit;
441 n->set_credit(n->get_credit() + credit);
445 case message::DATA: {
446 neighbor* n = rev_neigh[from];
447 double ld = msg->get_amount();
449 n->set_credit(n->get_credit() - ld);
452 case message::CTRL_CLOSE:
453 ctrl_close_pending--;
454 close_received = true;
456 case message::DATA_CLOSE:
457 data_close_pending--;
458 close_received = true;
464 #define print_loads_generic(vec, verbose, logp, cat) \
465 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
466 using std::placeholders::_1; \
467 XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected). " \
468 "Neighbor loads:", real_load, expected_load); \
469 std::for_each(vec.begin(), vec.end(), \
470 std::bind(&neighbor::print, _1, verbose, logp, cat)); \
473 void process::print_loads(bool verbose,
474 e_xbt_log_priority_t logp,
475 xbt_log_category_t cat) const
477 print_loads_generic(neigh, verbose, logp, cat);
480 void process::print_loads_p(bool verbose,
481 e_xbt_log_priority_t logp,
482 xbt_log_category_t cat) const
484 print_loads_generic(pneigh, verbose, logp, cat);
487 #undef print_loads_generic