2 #include <tr1/functional>
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 = 0.0)
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;
54 ctrl_close_pending = data_close_pending = neigh.size();
55 close_received = false;
58 comp_iter = lb_iter = 0;
60 lb_thread = new_msg_thread("loba",
61 std::tr1::bind(&process::load_balance_loop,
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::tr1::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 if (opt::log_rate < 0)
86 XBT_INFO("Final load after %d:%d iterations: %g",
87 lb_iter, comp_iter, real_load);
88 XBT_VERB("Expected load was: %g", expected_load);
89 XBT_VERB("Total computation for this process: %g", get_comp_amount());
94 if (opt::log_rate >= 0) {
95 XBT_INFO("Initial load: %g", real_load);
96 XBT_VERB("Initial expected load: %g", expected_load);
98 XBT_VERB("Starting...");
101 while (lb_iter <= opt::comp_iter_delay)
104 double sleep_duration = opt::comp_time_delay - MSG_get_clock();
105 if (sleep_duration > 0.0)
106 MSG_process_sleep(sleep_duration);
113 void process::load_balance_loop()
115 using std::tr1::bind;
116 using std::tr1::placeholders::_1;
118 double next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
119 while (still_running()) {
120 if (lb_iter == opt::comp_iter_delay) {
129 if (!opt::bookkeeping)
130 expected_load = real_load - get_sum_of_to_send();
131 // nothing to do with opt::bookkeeping
133 if (opt::log_rate && lb_iter % opt::log_rate == 0) {
134 XBT_INFO("(%u:%u) current load: %g", lb_iter, comp_iter, real_load);
135 XBT_VERB("... expected load: %g", expected_load);
138 if (expected_load > 0.0)
141 print_loads(true, xbt_log_priority_debug);
144 std::for_each(neigh.begin(), neigh.end(),
145 bind(&process::ctrl_send, this, _1));
146 prev_load_broadcast = expected_load;
148 sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
151 comm.ctrl_flush(false);
154 XBT_VERB("Going to finalize for %s...", __func__);
155 XBT_DEBUG("send CTRL_CLOSE to %zu neighbor%s",
156 neigh.size(), ESSE(neigh.size()));
157 std::for_each(neigh.begin(), neigh.end(),
158 bind(&process::ctrl_close, this, _1));
159 while (ctrl_close_pending) {
160 comm.ctrl_flush(false);
161 XBT_DEBUG("waiting for %d CTRL CLOSE", ctrl_close_pending);
164 comm.ctrl_flush(true);
167 void process::compute_loop()
169 using std::tr1::bind;
170 using std::tr1::placeholders::_1;
172 double next_iter_after_date = MSG_get_clock() + opt::min_comp_iter_duration;
173 while (still_running()) {
178 data_receive(opt::min_comp_iter_duration);
180 comm.data_flush(false);
182 if (real_load == 0.0)
186 std::for_each(neigh.begin(), neigh.end(),
187 bind(&process::data_send, this, _1));
191 double flops = opt::comp_cost(real_load);
192 m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
193 TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
194 XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
195 MSG_task_execute(task);
196 add_comp_amount(flops);
197 MSG_task_destroy(task);
199 sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
202 XBT_VERB("Going to finalize for %s...", __func__);
203 // last send, for not losing load scheduled to be sent
204 std::for_each(neigh.begin(), neigh.end(),
205 bind(&process::data_send, this, _1));
207 total_load_running -= real_load;
208 XBT_DEBUG("send DATA_CLOSE to %zu neighbor%s",
209 neigh.size(), ESSE(neigh.size()));
210 std::for_each(neigh.begin(), neigh.end(),
211 bind(&process::data_close, this, _1));
212 while (data_close_pending) {
213 comm.data_flush(false);
214 XBT_DEBUG("waiting for %d DATA CLOSE", data_close_pending);
217 comm.data_flush(true);
220 bool process::still_running()
222 static bool last_status = true;
227 } else if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
228 XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
231 } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
232 XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
235 } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
236 XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
239 } else if (opt::exit_on_close && close_received) {
240 XBT_VERB("Close received");
243 } else if (real_load == 0.0 && !data_close_pending) {
244 XBT_VERB("I'm a poor lonesome process, and I have no load...");
247 } else if (100.0 * total_load_running / total_load_init <=
248 opt::load_ratio_threshold) {
249 // fixme: this check should be implemented with a distributed
250 // algorithm, and not a shared global variable!
251 XBT_VERB("No more load to balance in system.");
258 double process::get_sum_of_to_send() const
260 using std::tr1::bind;
261 using std::tr1::placeholders::_1;
262 using std::tr1::placeholders::_2;
264 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
265 bind(std::plus<double>(),
266 _1, bind(&neighbor::get_to_send, _2)));
269 void process::load_balance()
271 if (lb_iter == 1) // warn only once
272 XBT_WARN("process::load_balance() is a no-op!");
275 void process::send(neighbor& nb, double amount)
277 expected_load -= amount;
278 nb.set_to_send(nb.get_to_send() + amount);
279 nb.set_load(nb.get_load() + amount);
282 void process::ctrl_send(neighbor& nb)
284 double info_to_send = expected_load;
285 if (info_to_send != prev_load_broadcast) {
286 message* msg = new message(message::INFO, info_to_send);
287 add_ctrl_send_mesg(msg->get_size());
288 comm.ctrl_send(nb.get_ctrl_mbox(), msg);
290 if (opt::bookkeeping) {
291 double debt_to_send = nb.get_to_send();
292 if (debt_to_send > 0.0) {
294 nb.set_debt(nb.get_debt() + debt_to_send);
295 message* msg = new message(message::CREDIT, debt_to_send);
296 add_ctrl_send_mesg(msg->get_size());
297 comm.ctrl_send(nb.get_ctrl_mbox(), msg);
302 void process::data_send(neighbor& nb)
305 if (opt::bookkeeping) {
306 load_to_send = std::min(real_load, nb.get_debt());
307 if (load_to_send >= opt::min_transfer_amount) {
308 nb.set_debt(nb.get_debt() - load_to_send);
309 real_load -= load_to_send;
314 load_to_send = nb.get_to_send();
315 if (load_to_send >= opt::min_transfer_amount) {
317 real_load -= load_to_send;
322 while (load_to_send > 0.0) {
324 if (opt::max_transfer_amount)
325 amount = std::min(load_to_send, opt::max_transfer_amount);
327 amount = load_to_send;
328 message* msg = new message(message::LOAD, amount);
329 add_data_send_mesg(msg->get_size());
330 comm.data_send(nb.get_data_mbox(), msg);
331 load_to_send -= amount;
335 void process::ctrl_close(neighbor& nb)
337 comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
340 void process::data_close(neighbor& nb)
342 comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
345 void process::ctrl_receive(double timeout)
350 XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
351 while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
352 if (msg->get_type() != message::CTRL_CLOSE)
353 add_ctrl_recv_mesg(msg->get_size());
354 handle_message(msg, from);
359 void process::data_receive(double timeout)
364 XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
365 while (data_close_pending && comm.data_recv(msg, from, timeout)) {
366 if (msg->get_type() != message::DATA_CLOSE)
367 add_data_recv_mesg(msg->get_size());
368 handle_message(msg, from);
373 void process::handle_message(message* msg, m_host_t from)
375 switch (msg->get_type()) {
376 case message::INFO: {
377 neighbor* n = rev_neigh[from];
378 n->set_load(msg->get_amount() + n->get_to_send());
381 case message::CREDIT:
382 expected_load += msg->get_amount();
384 case message::LOAD: {
385 double ld = msg->get_amount();
388 total_load_running -= ld;
391 case message::CTRL_CLOSE:
392 ctrl_close_pending--;
393 close_received = true;
395 case message::DATA_CLOSE:
396 data_close_pending--;
397 close_received = true;
403 #define print_loads_generic(vec, verbose, logp, cat) \
404 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
405 using std::tr1::bind; \
406 using std::tr1::placeholders::_1; \
407 XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected). " \
408 "Neighbor loads:", real_load, expected_load); \
409 std::for_each(vec.begin(), vec.end(), \
410 bind(&neighbor::print, _1, verbose, logp, cat)); \
413 void process::print_loads(bool verbose,
414 e_xbt_log_priority_t logp,
415 xbt_log_category_t cat) const
417 print_loads_generic(neigh, verbose, logp, cat);
420 void process::print_loads_p(bool verbose,
421 e_xbt_log_priority_t logp,
422 xbt_log_category_t cat) const
424 print_loads_generic(pneigh, verbose, logp, cat);
427 #undef print_loads_generic