2 #include <tr1/functional>
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc);
17 double process::total_load_init = 0.0;
18 double process::total_load_exit = 0.0;
20 process::process(int argc, char* argv[])
22 if (argc < 2 || !(std::istringstream(argv[1]) >> load))
23 throw std::invalid_argument("bad or missing initial load parameter");
25 neigh.assign(argv + 2, argv + argc);
27 pneigh.reserve(neigh.size());
28 for (unsigned i = 0 ; i < neigh.size() ; i++) {
29 neighbor* ptr = &neigh[i];
30 m_host_t host = MSG_get_host_by_name(ptr->get_name());
31 pneigh.push_back(ptr);
32 rev_neigh.insert(std::make_pair(host, ptr));
36 total_load_init += load;
38 ctrl_close_pending = data_close_pending = neigh.size();
39 if (neigh.size() == 1) {
40 comm.next_close_on_ctrl_is_last();
41 comm.next_close_on_data_is_last();
46 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
47 if (!LOG_ISENABLED(logp))
49 std::ostringstream oss;
50 oss << neigh.size() << " neighbor";
52 oss << ESSE(neigh.size()) << ": ";
53 std::transform(neigh.begin(), neigh.end() - 1,
54 std::ostream_iterator<const char*>(oss, ", "),
55 std::tr1::mem_fn(&neighbor::get_name));
56 oss << neigh.back().get_name();
58 LOG1(logp, "Got %s.", oss.str().c_str());
59 print_loads(true, logp);
64 total_load_exit += load;
69 INFO1("Initial load: %g", load);
71 // first send() to inform neighbors about our load (force it)
72 prev_load_broadcast = -1;
78 if (opt::log_rate && iter % opt::log_rate == 0) {
80 INFO3("(%u) current load: %g ; expected: %g",
81 iter, load, expected_load);
83 INFO2("(%u) current load: %g",
86 print_loads(true, xbt_log_priority_debug);
89 expected_load -= load_balance(expected_load);
91 load -= load_balance(load);
96 // NDS for Need To Send
97 #define NDS ((opt::bookkeeping ? expected_load : load) != prev_load_broadcast)
100 // General idea: do not iterate if there is nothing to
101 // compute, nor to send.
103 // fixme: review this chunk, and remove this NDS macro!
105 bool recv_wait = (load == 0 && !NDS);
106 bool close_received = !receive(recv_wait? WAIT: NO_WAIT);
108 if (opt::exit_on_close && close_received)
110 else if (opt::maxiter && iter >= opt::maxiter)
113 can_recv = (ctrl_close_pending || data_close_pending);
115 } while (one_more && can_recv && load == 0 && !NDS);
119 VERB0("Going to finalize...");
123 * - definition of load on heterogeneous hosts ?
124 * - how to detect convergence ?
125 * - how to manage link failures ?
129 INFO3("Final load after %d iteration%s: %g", iter, ESSE(iter), load);
130 if (opt::bookkeeping)
131 INFO1("Expected load: %g", expected_load);
135 double process::sum_of_to_send() const
137 using namespace std::tr1;
138 using namespace std::tr1::placeholders;
140 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
141 bind(std::plus<double>(),
142 _1, bind(&neighbor::get_to_send, _2)));
145 double process::load_balance(double /*my_load*/)
148 WARN0("process::load_balance is a no-op!");
152 void process::compute()
155 double duration = opt::comp_cost(load);
156 m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
157 DEBUG2("compute %g flop%s.", duration, ESSE(duration));
158 MSG_task_execute(task);
159 MSG_task_destroy(task);
161 DEBUG0("nothing to compute !");
165 void process::send1_no_bookkeeping(neighbor& nb)
167 if (load != prev_load_broadcast)
168 comm.send(nb.get_ctrl_mbox(), new message(message::INFO, load));
169 double load_to_send = nb.get_to_send();
170 if (load_to_send > 0.0) {
171 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
176 void process::send1_bookkeeping(neighbor& nb)
178 if (expected_load != prev_load_broadcast)
179 comm.send(nb.get_ctrl_mbox(),
180 new message(message::INFO, expected_load));
183 double debt_to_send = nb.get_to_send();
184 if (debt_to_send > 0.0) {
185 comm.send(nb.get_ctrl_mbox(),
186 new message(message::CREDIT, debt_to_send));
188 new_debt = nb.get_debt() + debt_to_send;
190 new_debt = nb.get_debt();
192 if (load <= new_debt) {
194 nb.set_debt(new_debt - load_to_send);
197 load_to_send = new_debt;
199 load -= load_to_send;
201 if (load_to_send > 0.0)
202 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
207 using namespace std::tr1;
208 using namespace std::tr1::placeholders;
210 if (opt::bookkeeping) {
211 std::for_each(neigh.begin(), neigh.end(),
212 bind(&process::send1_bookkeeping, this, _1));
213 prev_load_broadcast = expected_load;
215 std::for_each(neigh.begin(), neigh.end(),
216 bind(&process::send1_no_bookkeeping, this, _1));
217 prev_load_broadcast = load;
221 bool process::receive(recv_wait_mode wait)
223 // DEBUG1("go for receive(%s)",
224 // "NO_WAIT\0WAIT\0\0\0\0WAIT_FOR_CLOSE" + 8 * wait);
228 bool do_recv = ctrl_close_pending || data_close_pending;
229 while (do_recv && comm.recv(msg, from, wait)) {
230 switch (msg->get_type()) {
231 case message::INFO: {
232 neighbor* n = rev_neigh[from];
233 n->set_load(msg->get_amount());
236 case message::CREDIT:
237 expected_load += msg->get_amount();
240 load += msg->get_amount();
242 case message::CTRL_CLOSE:
243 if (--ctrl_close_pending == 1)
244 comm.next_close_on_ctrl_is_last();
245 DEBUG1("ctrl_close_pending = %d", ctrl_close_pending);
248 case message::DATA_CLOSE:
249 if (--data_close_pending == 1)
250 comm.next_close_on_data_is_last();
251 DEBUG1("data_close_pending = %d", data_close_pending);
256 do_recv = (wait == WAIT_FOR_CLOSE) &&
257 (ctrl_close_pending || data_close_pending);
262 void process::finalize1(neighbor& nb)
264 comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
265 comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
268 void process::finalize()
270 using namespace std::tr1;
271 using namespace std::tr1::placeholders;
273 DEBUG2("send CLOSE to %d neighbor%s.",
274 (int )neigh.size(), ESSE(neigh.size()));
275 std::for_each(neigh.begin(), neigh.end(),
276 bind(&process::finalize1, this, _1));
278 DEBUG2("wait for CLOSE from %d neighbor%s.",
279 (int )neigh.size(), ESSE(neigh.size()));
280 receive(WAIT_FOR_CLOSE);
285 #define print_loads_generic(vec, verbose, logp, cat) \
286 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
287 using namespace std::tr1; \
288 using namespace std::tr1::placeholders; \
289 XCLOG0(cat, logp, "Neighbor loads:"); \
290 std::for_each(vec.begin(), vec.end(), \
291 bind(&neighbor::print, _1, verbose, logp, cat)); \
294 void process::print_loads(bool verbose,
295 e_xbt_log_priority_t logp,
296 xbt_log_category_t cat) const
298 print_loads_generic(neigh, verbose, logp, cat);
301 void process::print_loads_p(bool verbose,
302 e_xbt_log_priority_t logp,
303 xbt_log_category_t cat) const
305 print_loads_generic(pneigh, verbose, logp, cat);
308 #undef print_loads_generic