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 std::ostringstream oss;
34 oss << neigh.size() << " neighbor";
36 oss << ESSE(neigh.size()) << ": ";
37 std::transform(neigh.begin(), neigh.end() - 1,
38 std::ostream_iterator<const char*>(oss, ", "),
39 std::tr1::mem_fn(&neighbor::get_name));
40 oss << neigh.back().get_name();
42 LOG1(logp, "Got %s.", oss.str().c_str());
51 INFO1("Initial load: %g", load);
57 if (opt::log_rate && iter % opt::log_rate == 0) {
59 INFO3("(%u) current load: %g ; expected: %g",
60 iter, load, expected_load);
62 INFO2("(%u) current load: %g",
67 close_received = !receive(false);
70 * compute load balancing;
71 * send tasks to neighbors;
76 if (opt::exit_on_close && close_received)
78 if (opt::maxiter && iter >= opt::maxiter)
81 VERB0("Going to finalize...");
85 * - definition of load on heterogeneous hosts ?
86 * - how to detect convergence ?
87 * - how to manage link failures ?
92 INFO4("Final load after %d iteration%s: %g ; expected: %g",
93 iter, ESSE(iter), load, expected_load);
95 INFO3("Final load after %d iteration%s: %g", iter, ESSE(iter), load);
99 void process::compute()
101 // fixme: shall we do something special when duration is 0 ?
102 double duration = opt::comp_cost(load);
103 m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
104 DEBUG2("compute %g flop%s.", duration, ESSE(duration));
105 MSG_task_execute(task);
106 MSG_task_destroy(task);
110 // Returns false if a CLOSE message was received.
111 bool process::receive(bool wait_for_close)
116 while ((ctrl_close_pending ||
117 data_close_pending) && comm.recv(msg, from, wait_for_close)) {
118 DEBUG2("received %s from %s",
119 msg->to_string().c_str(), MSG_host_get_name(from));
120 switch (msg->get_type()) {
122 // fixme: update neighbor
123 // need a map m_host_t -> neighbor&
125 case message::CREDIT:
126 expected_load += msg->get_amount();
129 load += msg->get_amount();
131 case message::CTRL_CLOSE:
132 if (--ctrl_close_pending == 1)
133 comm.next_close_on_ctrl_is_last();
134 DEBUG1("ctrl_close_pending = %d", ctrl_close_pending);
137 case message::DATA_CLOSE:
138 if (--data_close_pending == 1)
139 comm.next_close_on_data_is_last();
140 DEBUG1("data_close_pending = %d", data_close_pending);
149 void process::finalize()
151 DEBUG2("send CLOSE to %d neighbor%s.",
152 (int )neigh.size(), ESSE(neigh.size()));
153 std::vector<neighbor>::iterator n;
154 for (n = neigh.begin() ; n != neigh.end() ; ++n) {
155 comm.send(n->get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
156 comm.send(n->get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
159 DEBUG2("wait for CLOSE from %d neighbor%s.",
160 (int )neigh.size(), ESSE(neigh.size()));
166 void process::print_loads(e_xbt_log_priority_t logp)
168 if (!LOG_ISENABLED(logp))
171 std::ostringstream oss;
173 oss << "no neighbor!";
175 std::transform(neigh.begin(), neigh.end() - 1,
176 std::ostream_iterator<double>(oss, ", "),
177 std::tr1::mem_fn(&neighbor::get_load));
178 oss << neigh.back().get_load();
180 LOG1(logp, "Neighbor loads: %s", oss.str().c_str());