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())))
28 , receiver_mutex(xbt_mutex_init())
29 , receiver_cond(xbt_cond_init())
31 xbt_mutex_acquire(receiver_mutex);
33 MSG_process_create("receiver", communicator::receiver_wrapper,
34 this, MSG_host_self());
35 // wait for the receiver to be ready
36 xbt_cond_wait(receiver_cond, receiver_mutex);
37 xbt_mutex_release(receiver_mutex);
40 communicator::~communicator()
44 XBT_DEBUG("send finalize to receiver/ctrl");
45 task = MSG_task_create("finalize", 0.0, 0, NULL);
46 MSG_task_send(task, host->get_ctrl_mbox());
48 XBT_DEBUG("send finalize to receiver/data");
49 task = MSG_task_create("finalize", 0.0, 0, NULL);
50 MSG_task_send(task, host->get_data_mbox());
52 xbt_mutex_acquire(receiver_mutex);
53 while (receiver_thread) {
54 XBT_DEBUG("waiting for receiver to terminate");
55 xbt_cond_wait(receiver_cond, receiver_mutex);
57 xbt_mutex_release(receiver_mutex);
59 if (!received.empty())
60 XBT_WARN("lost %zu received message%s!",
61 received.size(), ESSE(received.size()));
62 if (!sent_comm.empty())
63 XBT_WARN("lost %zu sent message%s!",
64 sent_comm.size(), ESSE(sent_comm.size()));
66 xbt_cond_destroy(receiver_cond);
67 xbt_mutex_destroy(receiver_mutex);
70 void communicator::send(const char* dest, message* msg)
72 XBT_DEBUG("send %s to %s", msg->to_string().c_str(), dest);
73 double msg_size = sizeof *msg;
74 if (msg->get_type() == message::LOAD)
75 msg_size += opt::comm_cost(msg->get_amount());
76 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
77 TRACE_msg_set_task_category(task,
78 msg->get_type() == message::LOAD ?
79 TRACE_CAT_DATA : TRACE_CAT_CTRL);
80 msg_comm_t comm = MSG_task_isend(task, dest);
81 sent_comm.push_back(comm);
84 bool communicator::recv(message*& msg, m_host_t& from, double timeout)
87 volatile double deadline =
88 timeout > 0 ? MSG_get_clock() + timeout : 0.0;
89 xbt_mutex_acquire(receiver_mutex);
90 while (received.empty() && (!deadline || deadline > MSG_get_clock())) {
92 XBT_DEBUG("waiting for a message to come");
95 xbt_cond_timedwait(receiver_cond, receiver_mutex,
96 deadline - MSG_get_clock());
98 xbt_cond_wait(receiver_cond, receiver_mutex);
101 if (e.category != timeout_error)
106 xbt_mutex_release(receiver_mutex);
109 if (received.empty())
112 m_task_t task = received.front();
114 msg = static_cast<message*>(MSG_task_get_data(task));
115 from = MSG_task_get_source(task);
116 MSG_task_destroy(task);
118 XBT_DEBUG("received %s from %s",
119 msg->to_string().c_str(), MSG_host_get_name(from));
124 void communicator::flush(bool wait)
126 sent_comm.remove_if(comm_test_n_destroy);
127 if (wait && !sent_comm.empty()) {
128 msg_comm_t comms[sent_comm.size()];
129 std::copy(sent_comm.begin(), sent_comm.end(), comms);
130 MSG_comm_waitall(comms, sent_comm.size(), -1.0);
131 if (!MSG_WAIT_DESTROYS_COMMS)
132 std::for_each(sent_comm.begin(), sent_comm.end(),
133 comm_check_n_destroy);
138 int communicator::receiver_wrapper(int, char* [])
141 comm = static_cast<communicator*>(MSG_process_get_data(MSG_process_self()));
144 XBT_DEBUG("terminate");
145 xbt_mutex_acquire(comm->receiver_mutex);
146 comm->receiver_thread = NULL;
147 xbt_cond_signal(comm->receiver_cond);
148 xbt_mutex_release(comm->receiver_mutex);
153 void communicator::receiver()
155 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
161 channel chan[] = { { NULL, NULL, host->get_ctrl_mbox() },
162 { NULL, NULL, host->get_data_mbox() } };
163 const int chan_size = (sizeof chan) / (sizeof chan[0]);
165 for (int i = 0 ; i < chan_size ; ++i) {
166 chan[i].comm = MSG_task_irecv(&chan[i].task, chan[i].mbox);
167 xbt_dynar_push(comms, &chan[i].comm);
170 XBT_DEBUG("receiver ready");
171 xbt_mutex_acquire(receiver_mutex);
172 xbt_cond_signal(receiver_cond); // signal master that we are ready
173 xbt_mutex_release(receiver_mutex);
175 while (!xbt_dynar_is_empty(comms)) {
177 int index = MSG_comm_waitany(comms);
178 msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
181 for (ch = chan ; ch->comm != finished_comm ; ++ch)
184 comm_check_n_destroy(ch->comm);
185 if (strcmp(MSG_task_get_name(ch->task), "finalize")) {
186 XBT_DEBUG("received message on %s", ch->mbox);
187 xbt_mutex_acquire(receiver_mutex);
188 received.push(ch->task);
189 xbt_cond_signal(receiver_cond);
190 xbt_mutex_release(receiver_mutex);
192 ch->comm = MSG_task_irecv(&ch->task, ch->mbox);
193 xbt_dynar_set_as(comms, index, msg_comm_t, ch->comm);
195 XBT_DEBUG("received finalize on %s", ch->mbox);
196 MSG_task_destroy(ch->task);
199 xbt_dynar_remove_at(comms, index, NULL);
203 xbt_dynar_free(&comms);
206 void communicator::comm_check_n_destroy(msg_comm_t comm)
208 xbt_assert(MSG_comm_get_status(comm) == MSG_OK);
209 MSG_comm_destroy(comm);
212 bool communicator::comm_test_n_destroy(msg_comm_t comm)
214 if (MSG_comm_test(comm)) {
215 comm_check_n_destroy(comm);