3 #include <tr1/functional>
8 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm);
12 #include "simgrid_features.h"
15 #include "communicator.h"
17 std::string message::to_string()
19 static const char* str[] = { "INFO", "CREDIT", "LOAD",
20 "CTRL_CLOSE", "DATA_CLOSE" };
21 std::ostringstream oss;
22 oss << str[type] << ": " << amount;
26 communicator::communicator()
27 : host(static_cast<hostdata*>(MSG_host_get_data(MSG_host_self())))
29 receiver_mutex.acquire();
31 MSG_process_create("receiver", communicator::receiver_wrapper,
32 this, MSG_host_self());
33 receiver_cond.wait(receiver_mutex); // wait for the receiver to be ready
34 receiver_mutex.release();
37 communicator::~communicator()
41 XBT_DEBUG("send finalize to receiver/ctrl");
42 task = MSG_task_create("finalize", 0.0, 0, NULL);
43 MSG_task_send(task, host->get_ctrl_mbox());
45 XBT_DEBUG("send finalize to receiver/data");
46 task = MSG_task_create("finalize", 0.0, 0, NULL);
47 MSG_task_send(task, host->get_data_mbox());
49 receiver_mutex.acquire();
50 while (receiver_thread) {
51 XBT_DEBUG("waiting for receiver to terminate");
52 receiver_cond.wait(receiver_mutex);
54 receiver_mutex.release();
56 if (!received.empty())
57 XBT_WARN("lost %zu received message%s!",
58 received.size(), ESSE(received.size()));
59 if (!sent_comm.empty())
60 XBT_WARN("lost %zu sent message%s!",
61 sent_comm.size(), ESSE(sent_comm.size()));
64 void communicator::send(const char* dest, message* msg)
66 XBT_DEBUG("send %s to %s", msg->to_string().c_str(), dest);
67 double msg_size = sizeof *msg;
68 if (msg->get_type() == message::LOAD)
69 msg_size += opt::comm_cost(msg->get_amount());
70 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
71 TRACE_msg_set_task_category(task,
72 msg->get_type() == message::LOAD ?
73 TRACE_CAT_DATA : TRACE_CAT_CTRL);
74 msg_comm_t comm = MSG_task_isend(task, dest);
75 sent_comm.push_back(comm);
78 bool communicator::recv(message*& msg, m_host_t& from, double timeout)
81 volatile double deadline =
82 timeout > 0 ? MSG_get_clock() + timeout : 0.0;
83 receiver_mutex.acquire();
84 while (received.empty() && (!deadline || deadline > MSG_get_clock())) {
86 XBT_DEBUG("waiting for a message to come");
89 receiver_cond.timedwait(receiver_mutex,
90 deadline - MSG_get_clock());
92 receiver_cond.wait(receiver_mutex);
95 if (e.category != timeout_error)
100 receiver_mutex.release();
103 if (received.empty())
106 m_task_t task = received.front();
108 msg = static_cast<message*>(MSG_task_get_data(task));
109 from = MSG_task_get_source(task);
110 MSG_task_destroy(task);
112 XBT_DEBUG("received %s from %s",
113 msg->to_string().c_str(), MSG_host_get_name(from));
118 void communicator::flush(bool wait)
120 sent_comm.remove_if(comm_test_n_destroy);
121 if (wait && !sent_comm.empty()) {
122 msg_comm_t comms[sent_comm.size()];
123 std::copy(sent_comm.begin(), sent_comm.end(), comms);
124 MSG_comm_waitall(comms, sent_comm.size(), -1.0);
125 if (!MSG_WAIT_DESTROYS_COMMS)
126 std::for_each(sent_comm.begin(), sent_comm.end(),
127 comm_check_n_destroy);
132 int communicator::receiver_wrapper(int, char* [])
135 comm = static_cast<communicator*>(MSG_process_get_data(MSG_process_self()));
138 XBT_DEBUG("terminate");
139 comm->receiver_mutex.acquire();
140 comm->receiver_thread = NULL;
141 comm->receiver_cond.signal();
142 comm->receiver_mutex.release();
147 void communicator::receiver()
149 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
155 channel chan[] = { { NULL, NULL, host->get_ctrl_mbox() },
156 { NULL, NULL, host->get_data_mbox() } };
157 const int chan_size = (sizeof chan) / (sizeof chan[0]);
159 for (int i = 0 ; i < chan_size ; ++i) {
160 chan[i].comm = MSG_task_irecv(&chan[i].task, chan[i].mbox);
161 xbt_dynar_push(comms, &chan[i].comm);
164 XBT_DEBUG("receiver ready");
165 receiver_mutex.acquire();
166 receiver_cond.signal(); // signal master that we are ready
167 receiver_mutex.release();
169 while (!xbt_dynar_is_empty(comms)) {
171 int index = MSG_comm_waitany(comms);
172 msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
175 for (ch = chan ; ch->comm != finished_comm ; ++ch)
178 comm_check_n_destroy(ch->comm);
179 if (strcmp(MSG_task_get_name(ch->task), "finalize")) {
180 XBT_DEBUG("received message on %s", ch->mbox);
181 receiver_mutex.acquire();
182 received.push(ch->task);
183 receiver_cond.signal();
184 receiver_mutex.release();
186 ch->comm = MSG_task_irecv(&ch->task, ch->mbox);
187 xbt_dynar_set_as(comms, index, msg_comm_t, ch->comm);
189 XBT_DEBUG("received finalize on %s", ch->mbox);
190 MSG_task_destroy(ch->task);
193 xbt_dynar_remove_at(comms, index, NULL);
197 xbt_dynar_free(&comms);
200 void communicator::comm_check_n_destroy(msg_comm_t comm)
202 xbt_assert(MSG_comm_get_status(comm) == MSG_OK);
203 MSG_comm_destroy(comm);
206 bool communicator::comm_test_n_destroy(msg_comm_t comm)
208 if (MSG_comm_test(comm)) {
209 comm_check_n_destroy(comm);