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));
35 prev_load_broadcast = -1; // force sending of load on first send()
37 total_load_init += load;
39 ctrl_close_pending = data_close_pending = neigh.size();
40 if (neigh.size() == 1) {
41 comm.next_close_on_ctrl_is_last();
42 comm.next_close_on_data_is_last();
44 close_received = false;
45 may_receive = (neigh.size() > 0); // the same as (ctrl_close_pending ||
46 // data_close_pending)
50 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
51 if (!LOG_ISENABLED(logp))
53 std::ostringstream oss;
54 oss << neigh.size() << " neighbor";
56 oss << ESSE(neigh.size()) << ": ";
57 std::transform(neigh.begin(), neigh.end() - 1,
58 std::ostream_iterator<const char*>(oss, ", "),
59 std::tr1::mem_fn(&neighbor::get_name));
60 oss << neigh.back().get_name();
62 LOG1(logp, "Got %s.", oss.str().c_str());
63 print_loads(true, logp);
68 total_load_exit += load;
73 INFO1("Initial load: %g", load);
80 if (opt::log_rate && iter % opt::log_rate == 0) {
82 INFO3("(%u) current load: %g ; expected: %g",
83 iter, load, expected_load);
85 INFO2("(%u) current load: %g",
88 print_loads(true, xbt_log_priority_debug);
91 expected_load -= load_balance(expected_load);
93 load -= load_balance(load);
98 // NDS for Need To Send
99 #define NDS ((opt::bookkeeping ? expected_load : load) != prev_load_broadcast)
101 // General idea: block on receiving unless there is
102 // something to compute, or to send, or we must exit.
104 // fixme: review this chunk, and remove this NDS macro!
106 // FIXME: HAD A DEADLOCK HERE...
108 bool recv_wait = (load == 0 && !NDS);
109 DEBUG1("CALL RECEIVE(%s)", recv_wait? "WAIT": "NO_WAIT");
110 receive(recv_wait? WAIT: NO_WAIT);
112 if (opt::exit_on_close && close_received)
114 else if (opt::maxiter && iter >= opt::maxiter)
117 } while (one_more && may_receive && load == 0 && !NDS);
118 DEBUG0("RECEIVE LOOP ENDED");
122 VERB0("Going to finalize...");
126 * - definition of load on heterogeneous hosts ?
127 * - how to detect convergence ?
128 * - how to manage link failures ?
132 INFO3("Final load after %d iteration%s: %g", iter, ESSE(iter), load);
133 if (opt::bookkeeping)
134 INFO1("Expected load: %g", expected_load);
138 double process::sum_of_to_send() const
140 using namespace std::tr1;
141 using namespace std::tr1::placeholders;
143 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
144 bind(std::plus<double>(),
145 _1, bind(&neighbor::get_to_send, _2)));
148 double process::load_balance(double /*my_load*/)
151 WARN0("process::load_balance is a no-op!");
155 void process::compute()
158 double duration = opt::comp_cost(load);
159 m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
160 DEBUG2("compute %g flop%s.", duration, ESSE(duration));
161 MSG_task_execute(task);
162 MSG_task_destroy(task);
164 DEBUG0("nothing to compute !");
168 void process::send1_no_bookkeeping(neighbor& nb)
170 if (load != prev_load_broadcast)
171 comm.send(nb.get_ctrl_mbox(), new message(message::INFO, load));
172 double load_to_send = nb.get_to_send();
173 if (load_to_send > 0.0) {
174 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
179 void process::send1_bookkeeping(neighbor& nb)
181 if (expected_load != prev_load_broadcast)
182 comm.send(nb.get_ctrl_mbox(),
183 new message(message::INFO, expected_load));
186 double debt_to_send = nb.get_to_send();
187 if (debt_to_send > 0.0) {
188 comm.send(nb.get_ctrl_mbox(),
189 new message(message::CREDIT, debt_to_send));
191 new_debt = nb.get_debt() + debt_to_send;
193 new_debt = nb.get_debt();
195 if (load <= new_debt) {
197 nb.set_debt(new_debt - load_to_send);
200 load_to_send = new_debt;
202 load -= load_to_send;
204 if (load_to_send > 0.0)
205 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
210 using namespace std::tr1;
211 using namespace std::tr1::placeholders;
213 if (opt::bookkeeping) {
214 std::for_each(neigh.begin(), neigh.end(),
215 bind(&process::send1_bookkeeping, this, _1));
216 prev_load_broadcast = expected_load;
218 std::for_each(neigh.begin(), neigh.end(),
219 bind(&process::send1_no_bookkeeping, this, _1));
220 prev_load_broadcast = load;
224 void process::receive(recv_wait_mode wait)
226 // DEBUG1("go for receive(%s)",
227 // "NO_WAIT\0WAIT\0\0\0\0WAIT_FOR_CLOSE" + 8 * wait);
230 bool do_loop = may_receive;
231 while (do_loop && comm.recv(msg, from, wait)) {
232 switch (msg->get_type()) {
233 case message::INFO: {
234 neighbor* n = rev_neigh[from];
235 n->set_load(msg->get_amount());
238 case message::CREDIT:
239 expected_load += msg->get_amount();
242 load += msg->get_amount();
244 case message::CTRL_CLOSE:
245 if (--ctrl_close_pending == 1)
246 comm.next_close_on_ctrl_is_last();
247 // DEBUG1("ctrl_close_pending = %d", ctrl_close_pending);
248 close_received = true;
250 case message::DATA_CLOSE:
251 if (--data_close_pending == 1)
252 comm.next_close_on_data_is_last();
253 // DEBUG1("data_close_pending = %d", data_close_pending);
254 close_received = true;
258 may_receive = (ctrl_close_pending || data_close_pending);
259 do_loop = (wait == WAIT_FOR_CLOSE) && may_receive;
263 void process::finalize1(neighbor& nb)
265 comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
266 comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
269 void process::finalize()
271 using namespace std::tr1;
272 using namespace std::tr1::placeholders;
274 DEBUG2("send CLOSE to %d neighbor%s.",
275 (int )neigh.size(), ESSE(neigh.size()));
276 std::for_each(neigh.begin(), neigh.end(),
277 bind(&process::finalize1, this, _1));
279 DEBUG2("wait for CLOSE from %d neighbor%s.",
280 (int )neigh.size(), ESSE(neigh.size()));
281 receive(WAIT_FOR_CLOSE);
286 #define print_loads_generic(vec, verbose, logp, cat) \
287 if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \
288 using namespace std::tr1; \
289 using namespace std::tr1::placeholders; \
290 XCLOG0(cat, logp, "Neighbor loads:"); \
291 std::for_each(vec.begin(), vec.end(), \
292 bind(&neighbor::print, _1, verbose, logp, cat)); \
295 void process::print_loads(bool verbose,
296 e_xbt_log_priority_t logp,
297 xbt_log_category_t cat) const
299 print_loads_generic(neigh, verbose, logp, cat);
302 void process::print_loads_p(bool verbose,
303 e_xbt_log_priority_t logp,
304 xbt_log_category_t cat) const
306 print_loads_generic(pneigh, verbose, logp, cat);
309 #undef print_loads_generic