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);
21 ctrl_close_pending = data_close_pending = neigh.size();
23 e_xbt_log_priority_t logp = xbt_log_priority_verbose;
24 if (!LOG_ISENABLED(logp))
26 LOG1(logp, "My initial load is: %g", load);
27 std::ostringstream oss;
28 oss << neigh.size() << " neighbor";
30 oss << ESSE(neigh.size()) << ": ";
31 std::transform(neigh.begin(), neigh.end() - 1,
32 std::ostream_iterator<const char*>(oss, ", "),
33 std::tr1::mem_fn(&neighbor::get_name));
34 oss << neigh.back().get_name();
36 LOG1(logp, "Got %s.", oss.str().c_str());
47 INFO2("current load: %g ; expected: %g", load, expected_load);
49 INFO1("current load: %g", load);
54 xbt_sleep(100); // fixme
58 DEBUG0("going to finalize.");
61 // MSG_process_sleep(100.0); // xxx
63 * while (there is something to do) {
66 * compute load balancing;
67 * send tasks to neighbors;
70 * wait for pending messages;
74 * - definition of load on heterogeneous hosts ?
75 * - how to detect convergence ?
76 * - how to manage link failures ?
83 void process::compute()
85 double duration = opt::comp_cost(load);
86 m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
87 DEBUG2("compute %g flop%s.", duration, ESSE(duration));
88 MSG_task_execute(task);
89 MSG_task_destroy(task);
92 bool process::receive(bool wait_for_close)
97 while ((ctrl_close_pending ||
98 data_close_pending) && comm.recv(msg, from, wait_for_close)) {
99 switch (msg->get_type()) {
101 DEBUG0("received INFO");
102 // fixme: update neighbor
103 // need a map m_host_t -> neighbor&
105 case message::CREDIT:
106 DEBUG0("received CREDIT");
107 expected_load += msg->get_amount();
110 DEBUG0("received LOAD");
111 load += msg->get_amount();
113 case message::CTRL_CLOSE:
114 DEBUG0("received CTRL_CLOSE");
115 if (--ctrl_close_pending == 1)
116 comm.next_close_on_ctrl_is_last();
119 case message::DATA_CLOSE:
120 DEBUG0("received DATA_CLOSE");
121 if (--data_close_pending == 1)
122 comm.next_close_on_data_is_last();
131 void process::finalize()
133 DEBUG2("send CLOSE to %d neighbor%s.",
134 (int )neigh.size(), ESSE(neigh.size()));
135 std::vector<neighbor>::iterator n;
136 for (n = neigh.begin() ; n != neigh.end() ; ++n) {
137 comm.send(n->get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
138 comm.send(n->get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
141 DEBUG2("wait for CLOSE from %d neighbor%s.",
142 (int )neigh.size(), ESSE(neigh.size()));
145 comm.wait_for_sent();
148 void process::print_loads(e_xbt_log_priority_t logp)
150 if (!LOG_ISENABLED(logp))
153 std::ostringstream oss;
155 oss << "no neighbor!";
157 std::transform(neigh.begin(), neigh.end() - 1,
158 std::ostream_iterator<double>(oss, ", "),
159 std::tr1::mem_fn(&neighbor::get_load));
160 oss << neigh.back().get_load();
162 LOG1(logp, "Neighbor loads: %s", oss.str().c_str());