2 #include <tr1/functional>
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc);
17 double process::total_load_init = 0.0;
18 double process::total_load_running = 0.0;
19 double process::total_load_exit = 0.0;
21 process::process(int argc, char* argv[])
23 if (argc < 2 || !(std::istringstream(argv[1]) >> real_load))
24 throw std::invalid_argument("bad or missing initial load parameter");
26 neigh.assign(argv + 2, argv + argc);
28 pneigh.reserve(neigh.size());
29 for (unsigned i = 0 ; i < neigh.size() ; i++) {
30 neighbor* ptr = &neigh[i];
31 m_host_t host = MSG_get_host_by_name(ptr->get_name());
32 pneigh.push_back(ptr);
33 rev_neigh.insert(std::make_pair(host, ptr));
38 prev_load_broadcast = -1; // force sending of load on first send_all()
39 expected_load = real_load;
40 total_load_running += real_load;
41 total_load_init += real_load;
43 ctrl_close_pending = data_close_pending = neigh.size();
44 close_received = false;
47 comp_iter = lb_iter = 0;
49 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
50 if (!LOG_ISENABLED(logp))
52 std::ostringstream oss;
53 oss << neigh.size() << " neighbor";
55 oss << ESSE(neigh.size()) << ": ";
56 std::transform(neigh.begin(), neigh.end() - 1,
57 std::ostream_iterator<const char*>(oss, ", "),
58 std::tr1::mem_fn(&neighbor::get_name));
59 oss << neigh.back().get_name();
61 LOG1(logp, "Got %s.", oss.str().c_str());
62 print_loads(false, logp);
67 total_load_exit += real_load;
68 if (opt::bookkeeping) {
69 INFO4("Final load after %d:%d iterations: %g ; expected: %g",
70 lb_iter, comp_iter, real_load, expected_load);
72 INFO2("Final load after %d iterations: %g",
74 if (lb_iter != comp_iter)
75 WARN2("lb_iter (%d) and comp_iter (%d) differ!",
78 VERB1("Total computation for this process: %g", comp);
83 double next_iter_after_date = 0.0;
84 INFO1("Initial load: %g", real_load);
87 if (get_load() > 0.0) {
88 double now = MSG_get_clock();
89 if (now < next_iter_after_date)
90 MSG_process_sleep(next_iter_after_date - now);
91 next_iter_after_date = MSG_get_clock() + opt::min_iter_duration;
95 if (opt::log_rate && lb_iter % opt::log_rate == 0) {
97 INFO4("(%u:%u) current load: %g ; expected: %g",
98 lb_iter, comp_iter, real_load, expected_load);
100 INFO2("(%u) current load: %g",
106 print_loads(true, xbt_log_priority_debug);
109 // send load information, and load (data) if any
111 if (real_load > 0.0) {
116 if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
117 VERB2("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
120 if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
121 VERB2("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
124 if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
125 VERB2("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
129 // block on receiving unless there is something to compute or
132 if (real_load != 0 || get_load() != prev_load_broadcast)
134 else if (opt::min_iter_duration)
135 timeout = opt::min_iter_duration;
140 // one of our neighbor is finalizing
141 if (opt::exit_on_close && close_received) {
142 VERB0("Close received");
146 // have no load and cannot receive anything
147 if (real_load == 0.0 && !may_receive()) {
148 VERB0("I'm a poor lonesome process, and I have no load...");
152 // fixme: this check should be implemented with a distributed
153 // algorithm, and not a shared global variable!
154 // fixme: should this chunk be moved before call to receive() ?
155 if (100.0 * total_load_running / total_load_init <=
156 opt::load_ratio_threshold) {
157 VERB0("No more load to balance in system.");
160 DEBUG1("still %g load to balance, continuing...", total_load_running);
163 VERB0("Going to finalize...");
167 * - definition of load on heterogeneous hosts ?
168 * - how to detect convergence ?
169 * - how to manage link failures ?
176 void process::load_balance()
178 if (lb_iter == 1) // warn only once
179 WARN0("process::load_balance() is a no-op!");
182 void process::compute()
184 if (real_load > 0.0) {
185 double flops = opt::comp_cost(real_load);
186 m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
187 TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
188 DEBUG2("compute %g flop%s", flops, ESSE(flops));
189 MSG_task_execute(task);
191 MSG_task_destroy(task);
193 DEBUG0("nothing to compute !");
197 void process::send(neighbor& nb, double amount)
199 set_load(get_load() - amount);
200 nb.set_to_send(nb.get_to_send() + amount);
201 nb.set_load(nb.get_load() + amount); // fixme: make this optional?
204 void process::send1_no_bookkeeping(neighbor& nb)
206 if (real_load != prev_load_broadcast)
207 comm.send(nb.get_ctrl_mbox(), new message(message::INFO, real_load));
208 double load_to_send = nb.get_to_send();
209 if (load_to_send > 0.0) {
210 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
215 void process::send1_bookkeeping(neighbor& nb)
217 if (expected_load != prev_load_broadcast)
218 comm.send(nb.get_ctrl_mbox(),
219 new message(message::INFO, expected_load));
222 double debt_to_send = nb.get_to_send();
223 if (debt_to_send > 0.0) {
224 comm.send(nb.get_ctrl_mbox(),
225 new message(message::CREDIT, debt_to_send));
227 new_debt = nb.get_debt() + debt_to_send;
229 new_debt = nb.get_debt();
231 if (real_load <= new_debt) {
232 load_to_send = real_load;
233 nb.set_debt(new_debt - load_to_send);
236 load_to_send = new_debt;
238 real_load -= load_to_send;
240 if (load_to_send > 0.0)
241 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
244 void process::send_all()
246 using std::tr1::bind;
247 using std::tr1::placeholders::_1;
249 if (opt::bookkeeping) {
250 std::for_each(neigh.begin(), neigh.end(),
251 bind(&process::send1_bookkeeping, this, _1));
252 prev_load_broadcast = expected_load;
254 std::for_each(neigh.begin(), neigh.end(),
255 bind(&process::send1_no_bookkeeping, this, _1));
256 prev_load_broadcast = real_load;
261 void process::receive(double timeout)
266 DEBUG2("%sblocking receive (%g)", "\0non-" + !timeout, timeout);
267 while (may_receive() && comm.recv(msg, from, timeout)) {
268 switch (msg->get_type()) {
269 case message::INFO: {
270 neighbor* n = rev_neigh[from];
271 n->set_load(msg->get_amount());
274 case message::CREDIT:
275 expected_load += msg->get_amount();
277 case message::LOAD: {
278 double ld = msg->get_amount();
281 total_load_running -= ld;
284 case message::CTRL_CLOSE:
285 ctrl_close_pending--;
286 close_received = true;
288 case message::DATA_CLOSE:
289 data_close_pending--;
290 close_received = true;
294 timeout = 0.0; // only wait on first recv
299 void process::finalize1(neighbor& nb)
301 comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
302 comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
305 void process::finalize()
307 using std::tr1::bind;
308 using std::tr1::placeholders::_1;
311 total_load_running -= real_load;
313 DEBUG2("send CLOSE to %lu neighbor%s",
314 (unsigned long )neigh.size(), ESSE(neigh.size()));
315 std::for_each(neigh.begin(), neigh.end(),
316 bind(&process::finalize1, this, _1));
318 DEBUG2("wait for CLOSE from %lu neighbor%s",
319 (unsigned long )neigh.size(), ESSE(neigh.size()));
320 while (may_receive()) {
328 #define print_loads_generic(vec, verbose, logp, cat) \
329 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
330 using std::tr1::bind; \
331 using std::tr1::placeholders::_1; \
332 XCLOG0(cat, logp, "Neighbor loads:"); \
333 std::for_each(vec.begin(), vec.end(), \
334 bind(&neighbor::print, _1, verbose, logp, cat)); \
337 void process::print_loads(bool verbose,
338 e_xbt_log_priority_t logp,
339 xbt_log_category_t cat) const
341 print_loads_generic(neigh, verbose, logp, cat);
344 void process::print_loads_p(bool verbose,
345 e_xbt_log_priority_t logp,
346 xbt_log_category_t cat) const
348 print_loads_generic(pneigh, verbose, logp, cat);
351 #undef print_loads_generic