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));
51 prev_load_broadcast = -1; // force sending of load on first send_all()
52 expected_load = real_load;
53 total_load_running += real_load;
54 total_load_init += real_load;
56 ctrl_close_pending = data_close_pending = neigh.size();
57 close_received = false;
60 comp_iter = lb_iter = 0;
62 lb_thread = new_msg_thread("loba",
63 std::tr1::bind(&process::load_balance_loop,
66 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
67 if (!LOG_ISENABLED(logp))
69 std::ostringstream oss;
70 oss << neigh.size() << " neighbor";
72 oss << ESSE(neigh.size()) << ": ";
73 std::transform(neigh.begin(), neigh.end() - 1,
74 std::ostream_iterator<const char*>(oss, ", "),
75 std::tr1::mem_fn(&neighbor::get_name));
76 oss << neigh.back().get_name();
78 XBT_LOG(logp, "Got %s.", oss.str().c_str());
79 print_loads(false, logp);
85 total_load_exit += real_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", comp);
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::tr1::bind;
118 using std::tr1::placeholders::_1;
120 double next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
121 while (still_running()) {
122 if (lb_iter == opt::comp_iter_delay) {
131 if (opt::log_rate && lb_iter % opt::log_rate == 0) {
132 XBT_INFO("(%u:%u) current load: %g", lb_iter, comp_iter, real_load);
133 XBT_VERB("... expected load: %g", expected_load);
136 if (get_load() > 0.0)
139 print_loads(true, xbt_log_priority_debug);
142 std::for_each(neigh.begin(), neigh.end(),
143 bind(&process::ctrl_send, this, _1));
144 prev_load_broadcast = get_load();
146 sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
149 comm.ctrl_flush(false);
152 XBT_VERB("Going to finalize for %s...", __func__);
153 XBT_DEBUG("send CTRL_CLOSE to %zu neighbor%s",
154 neigh.size(), ESSE(neigh.size()));
155 std::for_each(neigh.begin(), neigh.end(),
156 bind(&process::ctrl_close, this, _1));
157 while (ctrl_close_pending) {
158 comm.ctrl_flush(false);
159 XBT_DEBUG("waiting for %d CTRL CLOSE", ctrl_close_pending);
162 comm.ctrl_flush(true);
165 void process::compute_loop()
167 using std::tr1::bind;
168 using std::tr1::placeholders::_1;
170 double next_iter_after_date = MSG_get_clock() + opt::min_comp_iter_duration;
171 while (still_running()) {
176 data_receive(opt::min_comp_iter_duration);
178 comm.data_flush(false);
180 if (real_load == 0.0)
184 std::for_each(neigh.begin(), neigh.end(),
185 bind(&process::data_send, this, _1));
189 double flops = opt::comp_cost(real_load);
190 m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
191 TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
192 XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
193 MSG_task_execute(task);
195 MSG_task_destroy(task);
197 sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
200 XBT_VERB("Going to finalize for %s...", __func__);
201 // last send, for not losing load scheduled to be sent
202 std::for_each(neigh.begin(), neigh.end(),
203 bind(&process::data_send, this, _1));
205 total_load_running -= real_load;
206 XBT_DEBUG("send DATA_CLOSE to %zu neighbor%s",
207 neigh.size(), ESSE(neigh.size()));
208 std::for_each(neigh.begin(), neigh.end(),
209 bind(&process::data_close, this, _1));
210 while (data_close_pending) {
211 comm.data_flush(false);
212 XBT_DEBUG("waiting for %d DATA CLOSE", data_close_pending);
215 comm.data_flush(true);
218 bool process::still_running()
220 static bool last_status = true;
225 } else if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
226 XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
229 } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
230 XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
233 } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
234 XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
237 } else if (opt::exit_on_close && close_received) {
238 XBT_VERB("Close received");
241 } else if (real_load == 0.0 && !data_close_pending) {
242 XBT_VERB("I'm a poor lonesome process, and I have no load...");
245 } else if (100.0 * total_load_running / total_load_init <=
246 opt::load_ratio_threshold) {
247 // fixme: this check should be implemented with a distributed
248 // algorithm, and not a shared global variable!
249 XBT_VERB("No more load to balance in system.");
256 double process::get_sum_of_to_send() const
258 using std::tr1::bind;
259 using std::tr1::placeholders::_1;
260 using std::tr1::placeholders::_2;
262 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
263 bind(std::plus<double>(),
264 _1, bind(&neighbor::get_to_send, _2)));
267 void process::load_balance()
269 if (lb_iter == 1) // warn only once
270 XBT_WARN("process::load_balance() is a no-op!");
273 void process::send(neighbor& nb, double amount)
275 set_load(get_load() - amount);
276 nb.set_to_send(nb.get_to_send() + amount);
277 nb.set_load(nb.get_load() + amount);
280 void process::ctrl_send(neighbor& nb)
282 double info_to_send = get_load();
283 if (info_to_send != prev_load_broadcast)
284 comm.ctrl_send(nb.get_ctrl_mbox(),
285 new message(message::INFO, info_to_send));
286 if (opt::bookkeeping) {
287 double debt_to_send = nb.get_to_send();
288 if (debt_to_send > 0.0) {
290 nb.set_debt(nb.get_debt() + debt_to_send);
291 comm.ctrl_send(nb.get_ctrl_mbox(),
292 new message(message::CREDIT, debt_to_send));
297 void process::data_send(neighbor& nb)
300 if (opt::bookkeeping) {
301 if (real_load <= nb.get_debt()) {
302 load_to_send = real_load;
303 nb.set_debt(nb.get_debt() - load_to_send);
306 load_to_send = nb.get_debt();
308 real_load -= load_to_send;
311 load_to_send = nb.get_to_send();
313 // do not update real_load here
315 if (load_to_send > 0.0)
316 comm.data_send(nb.get_data_mbox(),
317 new message(message::LOAD, load_to_send));
320 void process::ctrl_close(neighbor& nb)
322 comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
325 void process::data_close(neighbor& nb)
327 comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
330 void process::ctrl_receive(double timeout)
335 XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
336 while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
337 handle_message(msg, from);
342 void process::data_receive(double timeout)
347 XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
348 while (data_close_pending && comm.data_recv(msg, from, timeout)) {
349 handle_message(msg, from);
354 void process::handle_message(message* msg, m_host_t from)
356 switch (msg->get_type()) {
357 case message::INFO: {
358 neighbor* n = rev_neigh[from];
359 n->set_load(msg->get_amount());
362 case message::CREDIT:
363 expected_load += msg->get_amount();
365 case message::LOAD: {
366 double ld = msg->get_amount();
369 total_load_running -= ld;
372 case message::CTRL_CLOSE:
373 ctrl_close_pending--;
374 close_received = true;
376 case message::DATA_CLOSE:
377 data_close_pending--;
378 close_received = true;
384 #define print_loads_generic(vec, verbose, logp, cat) \
385 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
386 using std::tr1::bind; \
387 using std::tr1::placeholders::_1; \
388 XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected). " \
389 "Neighbor loads:", real_load, expected_load); \
390 std::for_each(vec.begin(), vec.end(), \
391 bind(&neighbor::print, _1, verbose, logp, cat)); \
394 void process::print_loads(bool verbose,
395 e_xbt_log_priority_t logp,
396 xbt_log_category_t cat) const
398 print_loads_generic(neigh, verbose, logp, cat);
401 void process::print_loads_p(bool verbose,
402 e_xbt_log_priority_t logp,
403 xbt_log_category_t cat) const
405 print_loads_generic(pneigh, verbose, logp, cat);
408 #undef print_loads_generic