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));
36 prev_load_broadcast = -1; // force sending of load on first send()
38 total_load_running += load;
39 total_load_init += load;
41 ctrl_close_pending = data_close_pending = neigh.size();
42 close_received = false;
45 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
46 if (!LOG_ISENABLED(logp))
48 std::ostringstream oss;
49 oss << neigh.size() << " neighbor";
51 oss << ESSE(neigh.size()) << ": ";
52 std::transform(neigh.begin(), neigh.end() - 1,
53 std::ostream_iterator<const char*>(oss, ", "),
54 std::tr1::mem_fn(&neighbor::get_name));
55 oss << neigh.back().get_name();
57 LOG1(logp, "Got %s.", oss.str().c_str());
58 print_loads(false, logp);
63 total_load_exit += load;
68 double next_iter_after_date = 0.0;
69 INFO1("Initial load: %g", load);
71 comp_iter = lb_iter = 0;
73 if (opt::min_iter_duration) {
74 double now = MSG_get_clock();
75 if (now < next_iter_after_date){
76 double delay = next_iter_after_date - now;
77 DEBUG1("sleeping for %g s", delay);
78 MSG_process_sleep(next_iter_after_date - now);
80 next_iter_after_date = MSG_get_clock() + opt::min_iter_duration;
85 if (opt::log_rate && comp_iter % opt::log_rate == 0) {
87 INFO4("(%u:%u) current load: %g ; expected: %g",
88 comp_iter, lb_iter, load, expected_load);
90 INFO2("(%u) current load: %g",
95 expected_load -= load_balance(expected_load);
97 load -= load_balance(load);
99 print_loads(true, xbt_log_priority_debug);
105 // send load information, and load when bookkeeping
109 if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
110 VERB2("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
113 if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
114 VERB2("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
117 if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
118 VERB2("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
122 // block on receiving unless there is something to compute or
124 bool wait = (load == 0 &&
125 ((opt::bookkeeping ? expected_load : load)
126 == 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.");
150 VERB0("Going to finalize...");
154 * - definition of load on heterogeneous hosts ?
155 * - how to detect convergence ?
156 * - how to manage link failures ?
160 if (opt::bookkeeping) {
161 INFO4("Final load after %d:%d iterations: %g ; expected: %g",
162 comp_iter, lb_iter, load, expected_load);
164 INFO2("Final load after %d iterations: %g",
166 if (comp_iter != lb_iter)
167 WARN2("comp_iter (%d) and lb_iter (%d) differ!",
173 double process::sum_of_to_send() const
175 using std::tr1::bind;
176 using std::tr1::placeholders::_1;
177 using std::tr1::placeholders::_2;
179 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
180 bind(std::plus<double>(),
181 _1, bind(&neighbor::get_to_send, _2)));
184 double process::load_balance(double /*my_load*/)
186 if (lb_iter == 1) // warn only once
187 WARN0("process::load_balance() is a no-op!");
191 void process::compute()
194 double duration = opt::comp_cost(load);
195 m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
196 DEBUG2("compute %g flop%s", duration, ESSE(duration));
197 MSG_task_execute(task);
198 MSG_task_destroy(task);
200 DEBUG0("nothing to compute !");
204 void process::send1_no_bookkeeping(neighbor& nb)
206 if (load != prev_load_broadcast)
207 comm.send(nb.get_ctrl_mbox(), new message(message::INFO, 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 (load <= new_debt) {
233 nb.set_debt(new_debt - load_to_send);
236 load_to_send = new_debt;
238 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));
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 = load;
261 void process::receive(bool wait)
266 while (may_receive() && comm.recv(msg, from, wait)) {
267 switch (msg->get_type()) {
268 case message::INFO: {
269 neighbor* n = rev_neigh[from];
270 n->set_load(msg->get_amount());
273 case message::CREDIT:
274 expected_load += msg->get_amount();
276 case message::LOAD: {
277 double ld = msg->get_amount();
280 total_load_running -= ld;
283 case message::CTRL_CLOSE:
284 ctrl_close_pending--;
285 close_received = true;
287 case message::DATA_CLOSE:
288 data_close_pending--;
289 close_received = true;
293 wait = false; // only wait on first recv
298 void process::finalize1(neighbor& nb)
300 comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
301 comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
304 void process::finalize()
306 using std::tr1::bind;
307 using std::tr1::placeholders::_1;
310 total_load_running -= load;
312 DEBUG2("send CLOSE to %lu neighbor%s",
313 (unsigned long )neigh.size(), ESSE(neigh.size()));
314 std::for_each(neigh.begin(), neigh.end(),
315 bind(&process::finalize1, this, _1));
317 DEBUG2("wait for CLOSE from %lu neighbor%s",
318 (unsigned long )neigh.size(), ESSE(neigh.size()));
319 while (may_receive()) {
327 #define print_loads_generic(vec, verbose, logp, cat) \
328 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
329 using std::tr1::bind; \
330 using std::tr1::placeholders::_1; \
331 XCLOG0(cat, logp, "Neighbor loads:"); \
332 std::for_each(vec.begin(), vec.end(), \
333 bind(&neighbor::print, _1, verbose, logp, cat)); \
336 void process::print_loads(bool verbose,
337 e_xbt_log_priority_t logp,
338 xbt_log_category_t cat) const
340 print_loads_generic(neigh, verbose, logp, cat);
343 void process::print_loads_p(bool verbose,
344 e_xbt_log_priority_t logp,
345 xbt_log_category_t cat) const
347 print_loads_generic(pneigh, verbose, logp, cat);
350 #undef print_loads_generic