4 #include <tr1/functional>
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu);
15 process::process(int argc, char* argv[])
17 if (argc < 2 || !(std::istringstream(argv[1]) >> load))
18 throw std::invalid_argument("bad or missing initial load");
20 neigh.assign(argv + 2, argv + argc);
22 std::for_each(neigh.begin(), neigh.end(),
23 std::tr1::bind(&process::insert_neighbor_in_map,
24 this, std::tr1::placeholders::_1));
28 ctrl_close_pending = data_close_pending = neigh.size();
29 if (neigh.size() == 1) {
30 comm.next_close_on_ctrl_is_last();
31 comm.next_close_on_data_is_last();
36 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
37 if (!LOG_ISENABLED(logp))
39 std::ostringstream oss;
40 oss << neigh.size() << " neighbor";
42 oss << ESSE(neigh.size()) << ": ";
43 std::transform(neigh.begin(), neigh.end() - 1,
44 std::ostream_iterator<const char*>(oss, ", "),
45 std::tr1::mem_fn(&neighbor::get_name));
46 oss << neigh.back().get_name();
48 LOG1(logp, "Got %s.", oss.str().c_str());
56 INFO1("Initial load: %g", load);
62 if (opt::log_rate && iter % opt::log_rate == 0) {
64 INFO3("(%u) current load: %g ; expected: %g",
65 iter, load, expected_load);
67 INFO2("(%u) current load: %g",
73 bool close_received = !receive(false);
76 expected_load -= load_balance(expected_load);
78 load -= load_balance(load);
83 if (opt::exit_on_close && close_received)
85 if (opt::maxiter && iter >= opt::maxiter)
88 VERB0("Going to finalize...");
92 * - definition of load on heterogeneous hosts ?
93 * - how to detect convergence ?
94 * - how to manage link failures ?
99 INFO4("Final load after %d iteration%s: %g ; expected: %g",
100 iter, ESSE(iter), load, expected_load);
102 INFO3("Final load after %d iteration%s: %g", iter, ESSE(iter), load);
106 void process::compute()
108 // fixme: shall we do something special when duration is 0 ?
109 double duration = opt::comp_cost(load);
110 m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
111 DEBUG2("compute %g flop%s.", duration, ESSE(duration));
112 MSG_task_execute(task);
113 MSG_task_destroy(task);
116 double process::load_balance(double /*my_load*/)
121 void process::send1_no_bookkeeping(neighbor& nb)
123 comm.send(nb.get_ctrl_mbox(), new message(message::INFO, load));
124 double load_to_send = nb.get_to_send();
125 if (load_to_send > 0.0) {
126 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
131 void process::send1_bookkeeping(neighbor& nb)
133 comm.send(nb.get_ctrl_mbox(), new message(message::INFO, expected_load));
136 double debt_to_send = nb.get_to_send();
137 if (debt_to_send > 0.0) {
138 comm.send(nb.get_ctrl_mbox(),
139 new message(message::CREDIT, debt_to_send));
141 new_debt = nb.get_debt() + debt_to_send;
143 new_debt = nb.get_debt();
145 if (load <= new_debt) {
147 nb.set_debt(new_debt - load_to_send);
150 load_to_send = new_debt;
152 load -= load_to_send;
154 if (load_to_send > 0.0)
155 comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
160 // fixme: shall we send data at all iterations?
161 if (opt::bookkeeping) {
162 std::for_each(neigh.begin(), neigh.end(),
163 std::tr1::bind(&process::send1_bookkeeping,
164 this, std::tr1::placeholders::_1));
166 std::for_each(neigh.begin(), neigh.end(),
167 std::tr1::bind(&process::send1_no_bookkeeping,
168 this, std::tr1::placeholders::_1));
172 // Returns false if a CLOSE message was received.
173 bool process::receive(bool wait_for_close)
178 while ((ctrl_close_pending ||
179 data_close_pending) && comm.recv(msg, from, wait_for_close)) {
180 DEBUG2("received %s from %s",
181 msg->to_string().c_str(), MSG_host_get_name(from));
182 switch (msg->get_type()) {
183 case message::INFO: {
184 neighbor* n = rev_neigh[from];
185 n->set_load(msg->get_amount());
188 case message::CREDIT:
189 expected_load += msg->get_amount();
192 load += msg->get_amount();
194 case message::CTRL_CLOSE:
195 if (--ctrl_close_pending == 1)
196 comm.next_close_on_ctrl_is_last();
197 DEBUG1("ctrl_close_pending = %d", ctrl_close_pending);
200 case message::DATA_CLOSE:
201 if (--data_close_pending == 1)
202 comm.next_close_on_data_is_last();
203 DEBUG1("data_close_pending = %d", data_close_pending);
212 void process::finalize1(neighbor& nb)
214 comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
215 comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
218 void process::finalize()
220 DEBUG2("send CLOSE to %d neighbor%s.",
221 (int )neigh.size(), ESSE(neigh.size()));
222 std::for_each(neigh.begin(), neigh.end(),
223 std::tr1::bind(&process::finalize1,
224 this, std::tr1::placeholders::_1));
226 DEBUG2("wait for CLOSE from %d neighbor%s.",
227 (int )neigh.size(), ESSE(neigh.size()));
233 void process::print_loads(e_xbt_log_priority_t logp)
235 if (!LOG_ISENABLED(logp))
238 std::ostringstream oss;
240 oss << "no neighbor!";
242 std::transform(neigh.begin(), neigh.end() - 1,
243 std::ostream_iterator<double>(oss, ", "),
244 std::tr1::mem_fn(&neighbor::get_load));
245 oss << neigh.back().get_load();
247 LOG1(logp, "Neighbor loads: %s", oss.str().c_str());
250 void process::insert_neighbor_in_map(neighbor& nb)
253 m_host_t host = MSG_get_host_by_name(nb.get_name());
254 rev_neigh.insert(std::make_pair(host, nbp));