10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc);
18 double process::total_load_init = 0.0;
19 double process::total_load_running = 0.0;
20 double process::total_load_exit = 0.0;
24 void sleep_until_date(double& date, double duration)
26 double sleep_duration = date - MSG_get_clock();
27 if (sleep_duration > 0.0)
28 MSG_process_sleep(sleep_duration);
29 date = MSG_get_clock() + duration;
34 process::process(int argc, char* argv[])
36 if (argc < 2 || !(std::istringstream(argv[1]) >> real_load))
37 throw std::invalid_argument("bad or missing initial load parameter");
39 neigh.assign(argv + 2, argv + argc);
41 pneigh.reserve(neigh.size());
42 for (unsigned i = 0 ; i < neigh.size() ; i++) {
43 neighbor* ptr = &neigh[i];
44 m_host_t host = MSG_get_host_by_name(ptr->get_name());
45 pneigh.push_back(ptr);
46 rev_neigh.insert(std::make_pair(host, ptr));
49 prev_load_broadcast = -1; // force sending of load on first send_all()
50 expected_load = real_load;
51 total_load_running += real_load;
52 total_load_init += real_load;
55 ctrl_close_pending = data_close_pending = neigh.size();
56 close_received = false;
59 comp_iter = lb_iter = 0;
61 lb_thread = new_msg_thread("loba",
62 std::bind(&process::load_balance_loop, this));
64 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
65 if (!LOG_ISENABLED(logp))
67 std::ostringstream oss;
68 oss << neigh.size() << " neighbor";
70 oss << ESSE(neigh.size()) << ": ";
71 std::transform(neigh.begin(), neigh.end() - 1,
72 std::ostream_iterator<const char*>(oss, ", "),
73 std::mem_fn(&neighbor::get_name));
74 oss << neigh.back().get_name();
76 XBT_LOG(logp, "Got %s.", oss.str().c_str());
77 print_loads(false, logp);
83 total_load_exit += real_load;
84 xbt_assert(received_load == 0.0,
85 "received_load is %g, but should be 0.0 !", received_load);
86 if (opt::log_rate < 0)
88 XBT_INFO("Final load after %d:%d iterations: %g",
89 lb_iter, comp_iter, real_load);
90 XBT_VERB("Expected load was: %g", expected_load);
91 XBT_VERB("Total computation for this process: %g", get_comp_amount());
96 if (opt::log_rate >= 0) {
97 XBT_INFO("Initial load: %g", real_load);
98 XBT_VERB("Initial expected load: %g", expected_load);
100 XBT_VERB("Starting...");
103 while (lb_iter <= opt::comp_iter_delay)
106 double sleep_duration = opt::comp_time_delay - MSG_get_clock();
107 if (sleep_duration > 0.0)
108 MSG_process_sleep(sleep_duration);
115 void process::load_balance_loop()
117 using std::placeholders::_1;
119 double next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
120 while (still_running()) {
121 if (lb_iter == opt::comp_iter_delay) {
131 if (!opt::bookkeeping)
132 expected_load = real_load - get_sum_of_to_send();
133 // nothing to do with opt::bookkeeping
135 if (opt::log_rate && lb_iter % opt::log_rate == 0) {
136 XBT_INFO("(%u:%u) current load: %g", lb_iter, comp_iter, real_load);
137 XBT_VERB("... expected load: %g", expected_load);
140 if (expected_load > 0.0)
143 print_loads(true, xbt_log_priority_debug);
146 comm.ctrl_flush(false);
147 std::for_each(neigh.begin(), neigh.end(),
148 std::bind(&process::ctrl_send, this, _1));
149 prev_load_broadcast = expected_load;
152 sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
156 XBT_VERB("Going to finalize for %s...", __func__);
157 XBT_DEBUG("send CTRL_CLOSE to %zu neighbor%s",
158 neigh.size(), ESSE(neigh.size()));
159 std::for_each(neigh.begin(), neigh.end(),
160 std::bind(&process::ctrl_close, this, _1));
161 while (ctrl_close_pending) {
162 comm.ctrl_flush(false);
163 XBT_DEBUG("waiting for %d CTRL_CLOSE", ctrl_close_pending);
166 comm.ctrl_flush(true);
169 void process::compute_loop()
171 using std::placeholders::_1;
173 double next_iter_after_date = MSG_get_clock() + opt::min_comp_iter_duration;
174 while (still_running()) {
175 // receive (do not block if there is something to compute)
176 data_receive(real_load > 0.0 ? 0.0 : opt::min_comp_iter_duration);
179 comm.data_flush(false);
181 real_load += received_load;
183 std::for_each(neigh.begin(), neigh.end(),
184 std::bind(&process::data_send, this, _1));
187 if (real_load == 0.0)
192 double flops = opt::comp_cost(real_load);
193 m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
194 TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
195 XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
196 MSG_task_execute(task);
197 add_comp_amount(flops);
198 MSG_task_destroy(task);
200 sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
203 XBT_VERB("Going to finalize for %s...", __func__);
205 XBT_DEBUG("send DATA_CLOSE to %zu neighbor%s",
206 neigh.size(), ESSE(neigh.size()));
207 std::for_each(neigh.begin(), neigh.end(),
208 std::bind(&process::data_close, this, _1));
209 while (data_close_pending) {
210 comm.data_flush(false);
211 XBT_DEBUG("waiting for %d DATA_CLOSE", data_close_pending);
214 real_load += received_load;
216 total_load_running -= real_load;
217 comm.data_flush(true);
220 bool process::still_running()
222 static bool last_status = true;
227 } else if (opt::exit_request) {
228 XBT_VERB("Global exit requested");
231 } else if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
232 XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
235 } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
236 XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
239 } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
240 XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
243 } else if (opt::exit_on_close && close_received) {
244 XBT_VERB("Close received");
247 } else if (real_load == 0.0 && !data_close_pending) {
248 XBT_VERB("I'm a poor lonesome process, and I have no load...");
251 } else if (100.0 * total_load_running / total_load_init <=
252 opt::load_ratio_threshold) {
253 // fixme: this check should be implemented with a distributed
254 // algorithm, and not a shared global variable!
255 XBT_VERB("No more load to balance in system.");
262 double process::get_sum_of_to_send() const
264 using std::placeholders::_1;
265 using std::placeholders::_2;
267 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
268 std::bind(std::plus<double>(), _1,
269 std::bind(&neighbor::get_to_send, _2)));
272 void process::load_balance()
274 if (lb_iter == 1) // warn only once
275 XBT_WARN("process::load_balance() is a no-op!");
278 void process::send(neighbor& nb, double amount)
280 expected_load -= amount;
281 nb.set_to_send(nb.get_to_send() + amount);
282 nb.set_load(nb.get_load() + amount);
285 void process::ctrl_send(neighbor& nb)
287 double info_to_send = expected_load;
289 if (opt::bookkeeping) { // bookkeeping
290 debt_to_send = nb.get_to_send();
291 if (debt_to_send > 0.0) {
293 nb.set_debt(nb.get_debt() + debt_to_send);
295 } else { // !bookkeeping
298 if (info_to_send != prev_load_broadcast || debt_to_send > 0.0) {
299 message* msg = new message(message::CTRL, info_to_send, debt_to_send);
300 add_ctrl_send_mesg(msg->get_size());
301 comm.ctrl_send(nb.get_ctrl_mbox(), msg);
305 void process::data_send(neighbor& nb)
308 if (opt::bookkeeping) {
309 load_to_send = std::min(real_load, nb.get_debt());
310 if (load_to_send >= opt::min_transfer_amount) {
311 nb.set_debt(nb.get_debt() - load_to_send);
312 real_load -= load_to_send;
317 load_to_send = nb.get_to_send();
318 if (load_to_send >= opt::min_transfer_amount) {
320 real_load -= load_to_send;
325 while (load_to_send > 0.0) {
327 if (opt::max_transfer_amount)
328 amount = std::min(load_to_send, opt::max_transfer_amount);
330 amount = load_to_send;
331 message* msg = new message(message::DATA, amount);
332 add_data_send_mesg(msg->get_size());
333 comm.data_send(nb.get_data_mbox(), msg);
334 load_to_send -= amount;
338 void process::ctrl_close(neighbor& nb)
340 comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
343 void process::data_close(neighbor& nb)
345 comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
348 void process::ctrl_receive(double timeout)
353 XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
354 while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
355 if (msg->get_type() != message::CTRL_CLOSE)
356 add_ctrl_recv_mesg(msg->get_size());
357 handle_message(msg, from);
362 void process::data_receive(double timeout)
367 XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
368 while (data_close_pending && comm.data_recv(msg, from, timeout)) {
369 if (msg->get_type() != message::DATA_CLOSE)
370 add_data_recv_mesg(msg->get_size());
371 handle_message(msg, from);
376 void process::handle_message(message* msg, m_host_t from)
378 switch (msg->get_type()) {
379 case message::CTRL: {
380 neighbor* n = rev_neigh[from];
381 n->set_load(msg->get_amount() + n->get_to_send());
382 expected_load += msg->get_credit(); // may be 0.0 if !opt::bookkeeping
385 case message::DATA: {
386 double ld = msg->get_amount();
390 case message::CTRL_CLOSE:
391 ctrl_close_pending--;
392 close_received = true;
394 case message::DATA_CLOSE:
395 data_close_pending--;
396 close_received = true;
402 #define print_loads_generic(vec, verbose, logp, cat) \
403 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
404 using std::placeholders::_1; \
405 XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected). " \
406 "Neighbor loads:", real_load, expected_load); \
407 std::for_each(vec.begin(), vec.end(), \
408 std::bind(&neighbor::print, _1, verbose, logp, cat)); \
411 void process::print_loads(bool verbose,
412 e_xbt_log_priority_t logp,
413 xbt_log_category_t cat) const
415 print_loads_generic(neigh, verbose, logp, cat);
418 void process::print_loads_p(bool verbose,
419 e_xbt_log_priority_t logp,
420 xbt_log_category_t cat) const
422 print_loads_generic(pneigh, verbose, logp, cat);
425 #undef print_loads_generic