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())))
32 , receiver_mutex(xbt_mutex_init())
33 , receiver_cond(xbt_cond_init())
35 xbt_mutex_acquire(receiver_mutex);
37 MSG_process_create("receiver", communicator::receiver_wrapper,
38 this, MSG_host_self());
39 // wait for the receiver to be ready
40 xbt_cond_wait(receiver_cond, receiver_mutex);
41 xbt_mutex_release(receiver_mutex);
44 communicator::~communicator()
48 XBT_DEBUG("send finalize to receiver/ctrl");
49 task = MSG_task_create("finalize", 0.0, 0, NULL);
50 MSG_task_send(task, get_ctrl_mbox());
52 XBT_DEBUG("send finalize to receiver/data");
53 task = MSG_task_create("finalize", 0.0, 0, NULL);
54 MSG_task_send(task, get_data_mbox());
56 xbt_mutex_acquire(receiver_mutex);
57 while (receiver_thread) {
58 XBT_DEBUG("waiting for receiver to terminate");
59 xbt_cond_wait(receiver_cond, receiver_mutex);
61 xbt_mutex_release(receiver_mutex);
64 XBT_WARN("ctrl_comm is pending!");
66 XBT_WARN("data_comm is pending!");
67 if (!received.empty())
68 XBT_WARN("lost %zu received message%s!",
69 received.size(), ESSE(received.size()));
70 if (!sent_comm.empty())
71 XBT_WARN("lost %zu sent message%s!",
72 sent_comm.size(), ESSE(sent_comm.size()));
74 xbt_cond_destroy(receiver_cond);
75 xbt_mutex_destroy(receiver_mutex);
78 void communicator::send(const char* dest, message* msg)
80 XBT_DEBUG("send %s to %s", msg->to_string().c_str(), dest);
81 double msg_size = sizeof *msg;
82 if (msg->get_type() == message::LOAD)
83 msg_size += opt::comm_cost(msg->get_amount());
84 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
85 TRACE_msg_set_task_category(task,
86 msg->get_type() == message::LOAD ?
87 TRACE_CAT_DATA : TRACE_CAT_CTRL);
88 msg_comm_t comm = MSG_task_isend(task, dest);
89 sent_comm.push_back(comm);
92 bool communicator::recv(message*& msg, m_host_t& from, double timeout)
95 volatile double deadline =
96 timeout > 0 ? MSG_get_clock() + timeout : 0.0;
97 xbt_mutex_acquire(receiver_mutex);
98 while (received.empty() && (!deadline || deadline > MSG_get_clock())) {
100 XBT_DEBUG("waiting for a message to come");
103 xbt_cond_timedwait(receiver_cond, receiver_mutex,
104 deadline - MSG_get_clock());
106 xbt_cond_wait(receiver_cond, receiver_mutex);
109 if (e.category != timeout_error)
114 xbt_mutex_release(receiver_mutex);
117 if (received.empty())
120 m_task_t task = received.front();
122 msg = static_cast<message*>(MSG_task_get_data(task));
123 from = MSG_task_get_source(task);
124 MSG_task_destroy(task);
126 XBT_DEBUG("received %s from %s",
127 msg->to_string().c_str(), MSG_host_get_name(from));
132 void communicator::flush(bool wait)
134 sent_comm.remove_if(comm_test_n_destroy);
135 if (wait && !sent_comm.empty()) {
136 msg_comm_t comms[sent_comm.size()];
137 std::copy(sent_comm.begin(), sent_comm.end(), comms);
138 MSG_comm_waitall(comms, sent_comm.size(), -1.0);
139 if (!MSG_WAIT_DESTROYS_COMMS)
140 std::for_each(sent_comm.begin(), sent_comm.end(),
141 comm_check_n_destroy);
146 int communicator::receiver_wrapper(int, char* [])
149 comm = static_cast<communicator*>(MSG_process_get_data(MSG_process_self()));
152 XBT_DEBUG("terminate");
153 xbt_mutex_acquire(comm->receiver_mutex);
154 comm->receiver_thread = NULL;
155 xbt_cond_signal(comm->receiver_cond);
156 xbt_mutex_release(comm->receiver_mutex);
161 void communicator::receiver1(msg_comm_t& comm, m_task_t& task, const char* mbox)
163 comm_check_n_destroy(comm);
164 if (strcmp(MSG_task_get_name(task), "finalize")) {
165 XBT_DEBUG("received message on %s", mbox);
166 xbt_mutex_acquire(receiver_mutex);
168 xbt_cond_signal(receiver_cond);
169 xbt_mutex_release(receiver_mutex);
171 comm = MSG_task_irecv(&task, mbox);
173 XBT_DEBUG("received finalize on %s", mbox);
174 MSG_task_destroy(task);
180 void communicator::receiver()
182 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
183 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
184 XBT_DEBUG("receiver ready");
185 xbt_mutex_acquire(receiver_mutex);
186 xbt_cond_signal(receiver_cond); // signal master that we are ready
187 xbt_mutex_release(receiver_mutex);
189 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
190 while (ctrl_comm || data_comm) {
193 xbt_dynar_push(comms, &ctrl_comm);
195 xbt_dynar_push(comms, &data_comm);
196 int index = MSG_comm_waitany(comms);
197 msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
198 xbt_dynar_reset(comms);
200 if (finished_comm == ctrl_comm)
201 receiver1(ctrl_comm, ctrl_task, get_ctrl_mbox());
202 else if (finished_comm == data_comm)
203 receiver1(data_comm, data_task, get_data_mbox());
205 THROW1(0, 0, "Cannot handle unknown comm -- %p", finished_comm);
207 xbt_dynar_free(&comms);
210 void communicator::comm_check_n_destroy(msg_comm_t comm)
212 xbt_assert(MSG_comm_get_status(comm) == MSG_OK);
213 MSG_comm_destroy(comm);
216 bool communicator::comm_test_n_destroy(msg_comm_t comm)
218 if (MSG_comm_test(comm)) {
219 comm_check_n_destroy(comm);