2 #include <tr1/functional>
10 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]) >> 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()
40 total_load_running += load;
41 total_load_init += load;
43 ctrl_close_pending = data_close_pending = neigh.size();
44 close_received = false;
47 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
48 if (!LOG_ISENABLED(logp))
50 std::ostringstream oss;
51 oss << neigh.size() << " neighbor";
53 oss << ESSE(neigh.size()) << ": ";
54 std::transform(neigh.begin(), neigh.end() - 1,
55 std::ostream_iterator<const char*>(oss, ", "),
56 std::tr1::mem_fn(&neighbor::get_name));
57 oss << neigh.back().get_name();
59 LOG1(logp, "Got %s.", oss.str().c_str());
60 print_loads(false, logp);
65 total_load_exit += load;
70 double next_iter_after_date = 0.0;
71 INFO1("Initial load: %g", load);
73 comp_iter = lb_iter = 0;
75 if (opt::min_iter_duration) {
76 double now = MSG_get_clock();
77 if (now < next_iter_after_date){
78 double delay = next_iter_after_date - now;
79 DEBUG1("sleeping for %g s", delay);
80 MSG_process_sleep(delay);
82 next_iter_after_date = MSG_get_clock() + opt::min_iter_duration;
85 double ld = lb_load();
89 if (opt::log_rate && lb_iter % opt::log_rate == 0) {
91 INFO4("(%u:%u) current load: %g ; expected: %g",
92 lb_iter, comp_iter, load, expected_load);
94 INFO2("(%u) current load: %g",
98 ld -= load_balance(ld);
100 print_loads(true, xbt_log_priority_debug);
104 // send load information, and load (data) if any
111 if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
112 VERB2("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
115 if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
116 VERB2("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
119 if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
120 VERB2("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
124 // block on receiving unless there is something to compute or
126 bool wait = (load == 0 && lb_load() == prev_load_broadcast);
129 // one of our neighbor is finalizing
130 if (opt::exit_on_close && close_received) {
131 VERB0("Close received");
135 // have no load and cannot receive anything
136 if (load == 0.0 && !may_receive()) {
137 VERB0("I'm a poor lonesome process, and I have no load...");
141 // fixme: this check should be implemented with a distributed
142 // algorithm, and not a shared global variable!
143 // fixme: should this chunk be moved before call to receive() ?
144 if (100.0 * total_load_running / total_load_init <=
145 opt::load_ratio_threshold) {
146 VERB0("No more load to balance in system.");
149 DEBUG1("still %g load to balance, continuing...", total_load_running);
152 VERB0("Going to finalize...");
156 * - definition of load on heterogeneous hosts ?
157 * - how to detect convergence ?
158 * - how to manage link failures ?
162 if (opt::bookkeeping) {
163 INFO4("Final load after %d:%d iterations: %g ; expected: %g",
164 lb_iter, comp_iter, load, expected_load);
166 INFO2("Final load after %d iterations: %g",
168 if (lb_iter != comp_iter)
169 WARN2("lb_iter (%d) and comp_iter (%d) differ!",
175 double process::sum_of_to_send() const
177 using std::tr1::bind;
178 using std::tr1::placeholders::_1;
179 using std::tr1::placeholders::_2;
181 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
182 bind(std::plus<double>(),
183 _1, bind(&neighbor::get_to_send, _2)));
186 double process::load_balance(double /*my_load*/)
188 if (lb_iter == 1) // warn only once
189 WARN0("process::load_balance() is a no-op!");
193 void process::compute()
196 double flops = opt::comp_cost(load);
197 m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
198 DEBUG2("compute %g flop%s", flops, ESSE(flops));
199 MSG_task_execute(task);
201 MSG_task_destroy(task);
203 DEBUG0("nothing to compute !");
207 void process::send1_no_bookkeeping(neighbor& nb)
209 if (load != prev_load_broadcast)
210 comm.send(nb.get_ctrl_mbox(), new message(message::INFO, load));
211 double load_to_send = nb.get_to_send();
212 if (load_to_send > 0.0) {
213 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
218 void process::send1_bookkeeping(neighbor& nb)
220 if (expected_load != prev_load_broadcast)
221 comm.send(nb.get_ctrl_mbox(),
222 new message(message::INFO, expected_load));
225 double debt_to_send = nb.get_to_send();
226 if (debt_to_send > 0.0) {
227 comm.send(nb.get_ctrl_mbox(),
228 new message(message::CREDIT, debt_to_send));
230 new_debt = nb.get_debt() + debt_to_send;
232 new_debt = nb.get_debt();
234 if (load <= new_debt) {
236 nb.set_debt(new_debt - load_to_send);
239 load_to_send = new_debt;
241 load -= load_to_send;
243 if (load_to_send > 0.0)
244 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
249 using std::tr1::bind;
250 using std::tr1::placeholders::_1;
252 if (opt::bookkeeping) {
253 std::for_each(neigh.begin(), neigh.end(),
254 bind(&process::send1_bookkeeping, this, _1));
255 prev_load_broadcast = expected_load;
257 std::for_each(neigh.begin(), neigh.end(),
258 bind(&process::send1_no_bookkeeping, this, _1));
259 prev_load_broadcast = load;
264 void process::receive(bool wait)
269 DEBUG1("%sblocking receive", "\0non-" + !wait);
270 while (may_receive() && comm.recv(msg, from, wait)) {
271 switch (msg->get_type()) {
272 case message::INFO: {
273 neighbor* n = rev_neigh[from];
274 n->set_load(msg->get_amount());
277 case message::CREDIT:
278 expected_load += msg->get_amount();
280 case message::LOAD: {
281 double ld = msg->get_amount();
284 total_load_running -= ld;
287 case message::CTRL_CLOSE:
288 ctrl_close_pending--;
289 close_received = true;
291 case message::DATA_CLOSE:
292 data_close_pending--;
293 close_received = true;
297 wait = false; // only wait on first recv
302 void process::finalize1(neighbor& nb)
304 comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
305 comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
308 void process::finalize()
310 using std::tr1::bind;
311 using std::tr1::placeholders::_1;
314 total_load_running -= load;
316 DEBUG2("send CLOSE to %lu neighbor%s",
317 (unsigned long )neigh.size(), ESSE(neigh.size()));
318 std::for_each(neigh.begin(), neigh.end(),
319 bind(&process::finalize1, this, _1));
321 DEBUG2("wait for CLOSE from %lu neighbor%s",
322 (unsigned long )neigh.size(), ESSE(neigh.size()));
323 while (may_receive()) {
331 #define print_loads_generic(vec, verbose, logp, cat) \
332 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
333 using std::tr1::bind; \
334 using std::tr1::placeholders::_1; \
335 XCLOG0(cat, logp, "Neighbor loads:"); \
336 std::for_each(vec.begin(), vec.end(), \
337 bind(&neighbor::print, _1, verbose, logp, cat)); \
340 void process::print_loads(bool verbose,
341 e_xbt_log_priority_t logp,
342 xbt_log_category_t cat) const
344 print_loads_generic(neigh, verbose, logp, cat);
347 void process::print_loads_p(bool verbose,
348 e_xbt_log_priority_t logp,
349 xbt_log_category_t cat) const
351 print_loads_generic(pneigh, verbose, logp, cat);
354 #undef print_loads_generic