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;
22 process::process(int argc, char* argv[])
24 if (argc < 2 || !(std::istringstream(argv[1]) >> load))
25 throw std::invalid_argument("bad or missing initial load parameter");
27 neigh.assign(argv + 2, argv + argc);
29 pneigh.reserve(neigh.size());
30 for (unsigned i = 0 ; i < neigh.size() ; i++) {
31 neighbor* ptr = &neigh[i];
32 m_host_t host = MSG_get_host_by_name(ptr->get_name());
33 pneigh.push_back(ptr);
34 rev_neigh.insert(std::make_pair(host, ptr));
39 prev_load_broadcast = -1; // force sending of load on first send()
41 total_load_running += load;
42 total_load_init += load;
44 ctrl_close_pending = data_close_pending = neigh.size();
45 close_received = false;
48 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
49 if (!LOG_ISENABLED(logp))
51 std::ostringstream oss;
52 oss << neigh.size() << " neighbor";
54 oss << ESSE(neigh.size()) << ": ";
55 std::transform(neigh.begin(), neigh.end() - 1,
56 std::ostream_iterator<const char*>(oss, ", "),
57 std::tr1::mem_fn(&neighbor::get_name));
58 oss << neigh.back().get_name();
60 LOG1(logp, "Got %s.", oss.str().c_str());
61 print_loads(false, logp);
66 total_load_exit += load;
71 double next_iter_after_date = 0.0;
72 INFO1("Initial load: %g", load);
74 comp_iter = lb_iter = 0;
76 double ld = lb_load();
78 double now = MSG_get_clock();
79 if (now < next_iter_after_date)
80 MSG_process_sleep(next_iter_after_date - now);
81 next_iter_after_date = MSG_get_clock() + opt::min_iter_duration;
85 if (opt::log_rate && lb_iter % opt::log_rate == 0) {
87 INFO4("(%u:%u) current load: %g ; expected: %g",
88 lb_iter, comp_iter, load, expected_load);
90 INFO2("(%u) current load: %g",
94 ld -= load_balance(ld);
96 print_loads(true, xbt_log_priority_debug);
100 // send load information, and load (data) if any
107 if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
108 VERB2("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
111 if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
112 VERB2("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
115 if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
116 VERB2("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
120 // block on receiving unless there is something to compute or
123 if (load != 0 || lb_load() != prev_load_broadcast)
125 else if (opt::min_iter_duration)
126 timeout = opt::min_iter_duration;
131 // one of our neighbor is finalizing
132 if (opt::exit_on_close && close_received) {
133 VERB0("Close received");
137 // have no load and cannot receive anything
138 if (load == 0.0 && !may_receive()) {
139 VERB0("I'm a poor lonesome process, and I have no load...");
143 // fixme: this check should be implemented with a distributed
144 // algorithm, and not a shared global variable!
145 // fixme: should this chunk be moved before call to receive() ?
146 if (100.0 * total_load_running / total_load_init <=
147 opt::load_ratio_threshold) {
148 VERB0("No more load to balance in system.");
151 DEBUG1("still %g load to balance, continuing...", total_load_running);
154 VERB0("Going to finalize...");
158 * - definition of load on heterogeneous hosts ?
159 * - how to detect convergence ?
160 * - how to manage link failures ?
164 if (opt::bookkeeping) {
165 INFO4("Final load after %d:%d iterations: %g ; expected: %g",
166 lb_iter, comp_iter, load, expected_load);
168 INFO2("Final load after %d iterations: %g",
170 if (lb_iter != comp_iter)
171 WARN2("lb_iter (%d) and comp_iter (%d) differ!",
177 double process::sum_of_to_send() const
179 using std::tr1::bind;
180 using std::tr1::placeholders::_1;
181 using std::tr1::placeholders::_2;
183 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
184 bind(std::plus<double>(),
185 _1, bind(&neighbor::get_to_send, _2)));
188 double process::load_balance(double /*my_load*/)
190 if (lb_iter == 1) // warn only once
191 WARN0("process::load_balance() is a no-op!");
195 void process::compute()
198 double flops = opt::comp_cost(load);
199 m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
200 TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
201 DEBUG2("compute %g flop%s", flops, ESSE(flops));
202 MSG_task_execute(task);
204 MSG_task_destroy(task);
206 DEBUG0("nothing to compute !");
210 void process::send1_no_bookkeeping(neighbor& nb)
212 if (load != prev_load_broadcast)
213 comm.send(nb.get_ctrl_mbox(), new message(message::INFO, load));
214 double load_to_send = nb.get_to_send();
215 if (load_to_send > 0.0) {
216 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
221 void process::send1_bookkeeping(neighbor& nb)
223 if (expected_load != prev_load_broadcast)
224 comm.send(nb.get_ctrl_mbox(),
225 new message(message::INFO, expected_load));
228 double debt_to_send = nb.get_to_send();
229 if (debt_to_send > 0.0) {
230 comm.send(nb.get_ctrl_mbox(),
231 new message(message::CREDIT, debt_to_send));
233 new_debt = nb.get_debt() + debt_to_send;
235 new_debt = nb.get_debt();
237 if (load <= new_debt) {
239 nb.set_debt(new_debt - load_to_send);
242 load_to_send = new_debt;
244 load -= load_to_send;
246 if (load_to_send > 0.0)
247 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
252 using std::tr1::bind;
253 using std::tr1::placeholders::_1;
255 if (opt::bookkeeping) {
256 std::for_each(neigh.begin(), neigh.end(),
257 bind(&process::send1_bookkeeping, this, _1));
258 prev_load_broadcast = expected_load;
260 std::for_each(neigh.begin(), neigh.end(),
261 bind(&process::send1_no_bookkeeping, this, _1));
262 prev_load_broadcast = load;
267 void process::receive(double timeout)
272 DEBUG2("%sblocking receive (%g)", "\0non-" + !timeout, timeout);
273 while (may_receive() && comm.recv(msg, from, timeout)) {
274 switch (msg->get_type()) {
275 case message::INFO: {
276 neighbor* n = rev_neigh[from];
277 n->set_load(msg->get_amount());
280 case message::CREDIT:
281 expected_load += msg->get_amount();
283 case message::LOAD: {
284 double ld = msg->get_amount();
287 total_load_running -= ld;
290 case message::CTRL_CLOSE:
291 ctrl_close_pending--;
292 close_received = true;
294 case message::DATA_CLOSE:
295 data_close_pending--;
296 close_received = true;
300 timeout = 0.0; // only wait on first recv
305 void process::finalize1(neighbor& nb)
307 comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
308 comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
311 void process::finalize()
313 using std::tr1::bind;
314 using std::tr1::placeholders::_1;
317 total_load_running -= load;
319 DEBUG2("send CLOSE to %lu neighbor%s",
320 (unsigned long )neigh.size(), ESSE(neigh.size()));
321 std::for_each(neigh.begin(), neigh.end(),
322 bind(&process::finalize1, this, _1));
324 DEBUG2("wait for CLOSE from %lu neighbor%s",
325 (unsigned long )neigh.size(), ESSE(neigh.size()));
326 while (may_receive()) {
334 #define print_loads_generic(vec, verbose, logp, cat) \
335 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
336 using std::tr1::bind; \
337 using std::tr1::placeholders::_1; \
338 XCLOG0(cat, logp, "Neighbor loads:"); \
339 std::for_each(vec.begin(), vec.end(), \
340 bind(&neighbor::print, _1, verbose, logp, cat)); \
343 void process::print_loads(bool verbose,
344 e_xbt_log_priority_t logp,
345 xbt_log_category_t cat) const
347 print_loads_generic(neigh, verbose, logp, cat);
350 void process::print_loads_p(bool verbose,
351 e_xbt_log_priority_t logp,
352 xbt_log_category_t cat) const
354 print_loads_generic(pneigh, verbose, logp, cat);
357 #undef print_loads_generic