2 #include <tr1/functional>
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu);
14 process::process(int argc, char* argv[])
16 if (argc < 2 || !(std::istringstream(argv[1]) >> load))
17 throw std::invalid_argument("bad or missing initial load");
19 neigh.assign(argv + 2, argv + argc);
22 ctrl_close_pending = data_close_pending = neigh.size();
23 if (neigh.size() == 1) {
24 comm.next_close_on_ctrl_is_last();
25 comm.next_close_on_data_is_last();
30 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
31 if (!LOG_ISENABLED(logp))
33 LOG1(logp, "My initial load is: %g", load);
34 std::ostringstream oss;
35 oss << neigh.size() << " neighbor";
37 oss << ESSE(neigh.size()) << ": ";
38 std::transform(neigh.begin(), neigh.end() - 1,
39 std::ostream_iterator<const char*>(oss, ", "),
40 std::tr1::mem_fn(&neighbor::get_name));
41 oss << neigh.back().get_name();
43 LOG1(logp, "Got %s.", oss.str().c_str());
56 INFO3("(%u) current load: %g ; expected: %g",
57 iter, load, expected_load);
59 INFO2("(%u) current load: %g",
63 close_received = !receive(false);
66 * compute load balancing;
67 * send tasks to neighbors;
73 if (opt::exit_on_close && close_received)
75 if (opt::maxiter && iter >= opt::maxiter)
78 VERB0("Going to finalize...");
82 * - definition of load on heterogeneous hosts ?
83 * - how to detect convergence ?
84 * - how to manage link failures ?
91 void process::compute()
93 // fixme: shall we do something special when duration is 0 ?
94 double duration = opt::comp_cost(load);
95 m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
96 DEBUG2("compute %g flop%s.", duration, ESSE(duration));
97 MSG_task_execute(task);
98 MSG_task_destroy(task);
102 // Returns false if a CLOSE message was received.
103 bool process::receive(bool wait_for_close)
108 while ((ctrl_close_pending ||
109 data_close_pending) && comm.recv(msg, from, wait_for_close)) {
110 DEBUG2("received %s from %s",
111 msg->to_string().c_str(), MSG_host_get_name(from));
112 switch (msg->get_type()) {
114 // fixme: update neighbor
115 // need a map m_host_t -> neighbor&
117 case message::CREDIT:
118 expected_load += msg->get_amount();
121 load += msg->get_amount();
123 case message::CTRL_CLOSE:
124 if (--ctrl_close_pending == 1)
125 comm.next_close_on_ctrl_is_last();
126 DEBUG1("ctrl_close_pending = %d", ctrl_close_pending);
129 case message::DATA_CLOSE:
130 if (--data_close_pending == 1)
131 comm.next_close_on_data_is_last();
132 DEBUG1("data_close_pending = %d", data_close_pending);
141 void process::finalize()
143 DEBUG2("send CLOSE to %d neighbor%s.",
144 (int )neigh.size(), ESSE(neigh.size()));
145 std::vector<neighbor>::iterator n;
146 for (n = neigh.begin() ; n != neigh.end() ; ++n) {
147 comm.send(n->get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
148 comm.send(n->get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
151 DEBUG2("wait for CLOSE from %d neighbor%s.",
152 (int )neigh.size(), ESSE(neigh.size()));
158 void process::print_loads(e_xbt_log_priority_t logp)
160 if (!LOG_ISENABLED(logp))
163 std::ostringstream oss;
165 oss << "no neighbor!";
167 std::transform(neigh.begin(), neigh.end() - 1,
168 std::ostream_iterator<double>(oss, ", "),
169 std::tr1::mem_fn(&neighbor::get_load));
170 oss << neigh.back().get_load();
172 LOG1(logp, "Neighbor loads: %s", oss.str().c_str());