4 #include <tr1/functional>
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc);
16 double process::total_load_init = 0.0;
17 double process::total_load_exit = 0.0;
19 process::process(int argc, char* argv[])
21 if (argc < 2 || !(std::istringstream(argv[1]) >> load))
22 throw std::invalid_argument("bad or missing initial load parameter");
24 neigh.assign(argv + 2, argv + argc);
26 pneigh.reserve(neigh.size());
27 for (unsigned i = 0 ; i < neigh.size() ; i++) {
28 neighbor* ptr = &neigh[i];
29 m_host_t host = MSG_get_host_by_name(ptr->get_name());
30 pneigh.push_back(ptr);
31 rev_neigh.insert(std::make_pair(host, ptr));
35 total_load_init += load;
37 ctrl_close_pending = data_close_pending = neigh.size();
38 if (neigh.size() == 1) {
39 comm.next_close_on_ctrl_is_last();
40 comm.next_close_on_data_is_last();
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());
63 total_load_exit += load;
68 INFO1("Initial load: %g", load);
70 // first send() to inform neighbors about our load (force it)
71 prev_load_broadcast = -1;
77 if (opt::log_rate && iter % opt::log_rate == 0) {
79 INFO3("(%u) current load: %g ; expected: %g",
80 iter, load, expected_load);
82 INFO2("(%u) current load: %g",
85 print_loads(xbt_log_priority_debug);
88 expected_load -= load_balance(expected_load);
90 load -= load_balance(load);
95 // NDS for Need To Send
96 #define NDS ((opt::bookkeeping ? expected_load : load) != prev_load_broadcast)
99 // General idea: do not iterate if there is nothing to
100 // compute, nor to send.
102 // fixme: review this chunk, and remove this NDS macro!
104 bool recv_wait = (load == 0 && !NDS);
105 bool close_received = !receive(recv_wait? WAIT: NO_WAIT);
107 if (opt::exit_on_close && close_received)
109 else if (opt::maxiter && iter >= opt::maxiter)
112 can_recv = (ctrl_close_pending || data_close_pending);
114 } while (one_more && can_recv && load == 0 && !NDS);
118 VERB0("Going to finalize...");
122 * - definition of load on heterogeneous hosts ?
123 * - how to detect convergence ?
124 * - how to manage link failures ?
128 INFO3("Final load after %d iteration%s: %g", iter, ESSE(iter), load);
129 if (opt::bookkeeping)
130 INFO1("Expected load: %g", expected_load);
134 double process::sum_of_to_send() const
136 using namespace std::tr1;
137 using namespace std::tr1::placeholders;
139 return std::accumulate(neigh.begin(), neigh.end(), 0.0,
140 bind(std::plus<double>(),
141 _1, bind(&neighbor::get_to_send, _2)));
144 double process::load_balance(double /*my_load*/)
147 WARN0("process::load_balance is a no-op!");
151 void process::compute()
154 double duration = opt::comp_cost(load);
155 m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
156 DEBUG2("compute %g flop%s.", duration, ESSE(duration));
157 MSG_task_execute(task);
158 MSG_task_destroy(task);
160 DEBUG0("nothing to compute !");
164 void process::send1_no_bookkeeping(neighbor& nb)
166 if (load != prev_load_broadcast)
167 comm.send(nb.get_ctrl_mbox(), new message(message::INFO, load));
168 double load_to_send = nb.get_to_send();
169 if (load_to_send > 0.0) {
170 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
175 void process::send1_bookkeeping(neighbor& nb)
177 if (expected_load != prev_load_broadcast)
178 comm.send(nb.get_ctrl_mbox(),
179 new message(message::INFO, expected_load));
182 double debt_to_send = nb.get_to_send();
183 if (debt_to_send > 0.0) {
184 comm.send(nb.get_ctrl_mbox(),
185 new message(message::CREDIT, debt_to_send));
187 new_debt = nb.get_debt() + debt_to_send;
189 new_debt = nb.get_debt();
191 if (load <= new_debt) {
193 nb.set_debt(new_debt - load_to_send);
196 load_to_send = new_debt;
198 load -= load_to_send;
200 if (load_to_send > 0.0)
201 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
206 using namespace std::tr1;
207 using namespace std::tr1::placeholders;
209 if (opt::bookkeeping) {
210 std::for_each(neigh.begin(), neigh.end(),
211 bind(&process::send1_bookkeeping, this, _1));
212 prev_load_broadcast = expected_load;
214 std::for_each(neigh.begin(), neigh.end(),
215 bind(&process::send1_no_bookkeeping, this, _1));
216 prev_load_broadcast = load;
220 bool process::receive(recv_wait_mode wait)
222 // DEBUG1("go for receive(%s)",
223 // "NO_WAIT\0WAIT\0\0\0\0WAIT_FOR_CLOSE" + 8 * wait);
227 bool do_recv = ctrl_close_pending || data_close_pending;
228 while (do_recv && comm.recv(msg, from, wait)) {
229 switch (msg->get_type()) {
230 case message::INFO: {
231 neighbor* n = rev_neigh[from];
232 n->set_load(msg->get_amount());
235 case message::CREDIT:
236 expected_load += msg->get_amount();
239 load += msg->get_amount();
241 case message::CTRL_CLOSE:
242 if (--ctrl_close_pending == 1)
243 comm.next_close_on_ctrl_is_last();
244 DEBUG1("ctrl_close_pending = %d", ctrl_close_pending);
247 case message::DATA_CLOSE:
248 if (--data_close_pending == 1)
249 comm.next_close_on_data_is_last();
250 DEBUG1("data_close_pending = %d", data_close_pending);
255 do_recv = (wait == WAIT_FOR_CLOSE) &&
256 (ctrl_close_pending || data_close_pending);
261 void process::finalize1(neighbor& nb)
263 comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
264 comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
267 void process::finalize()
269 using namespace std::tr1;
270 using namespace std::tr1::placeholders;
272 DEBUG2("send CLOSE to %d neighbor%s.",
273 (int )neigh.size(), ESSE(neigh.size()));
274 std::for_each(neigh.begin(), neigh.end(),
275 bind(&process::finalize1, this, _1));
277 DEBUG2("wait for CLOSE from %d neighbor%s.",
278 (int )neigh.size(), ESSE(neigh.size()));
279 receive(WAIT_FOR_CLOSE);
284 void process::print_loads(e_xbt_log_priority_t logp)
286 if (!LOG_ISENABLED(logp))
289 std::ostringstream oss;
291 oss << "no neighbor!";
293 std::transform(neigh.begin(), neigh.end() - 1,
294 std::ostream_iterator<double>(oss, ", "),
295 std::tr1::mem_fn(&neighbor::get_load));
296 oss << neigh.back().get_load();
298 LOG1(logp, "Neighbor loads: %s", oss.str().c_str());