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 , mutex(xbt_mutex_init())
29 , cond(xbt_cond_init())
35 xbt_mutex_acquire(mutex);
37 MSG_process_create("receiver", communicator::receiver_wrapper,
38 this, MSG_host_self());
39 xbt_cond_wait(cond, mutex); // wait for the receiver to be ready
40 xbt_mutex_release(mutex);
43 communicator::~communicator()
47 XBT_DEBUG("send finalize to receiver/ctrl");
48 task = MSG_task_create("finalize", 0.0, 0, NULL);
49 MSG_task_send(task, get_ctrl_mbox());
51 XBT_DEBUG("send finalize to receiver/data");
52 task = MSG_task_create("finalize", 0.0, 0, NULL);
53 MSG_task_send(task, get_data_mbox());
55 xbt_mutex_acquire(mutex);
56 while (receiver_process) {
57 XBT_DEBUG("waiting for receiver to terminate");
58 xbt_cond_wait(cond, mutex);
60 xbt_mutex_release(mutex);
63 XBT_WARN("ctrl_comm is pending!");
65 XBT_WARN("data_comm is pending!");
66 if (!received.empty())
67 XBT_WARN("lost %zu received message%s!",
68 received.size(), ESSE(received.size()));
69 if (!sent_comm.empty())
70 XBT_WARN("lost %zu sent message%s!",
71 sent_comm.size(), ESSE(sent_comm.size()));
73 xbt_cond_destroy(cond);
74 xbt_mutex_destroy(mutex);
77 void communicator::send(const char* dest, message* msg)
79 XBT_DEBUG("send %s to %s", msg->to_string().c_str(), dest);
80 double msg_size = sizeof *msg;
81 if (msg->get_type() == message::LOAD)
82 msg_size += opt::comm_cost(msg->get_amount());
83 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
84 TRACE_msg_set_task_category(task,
85 msg->get_type() == message::LOAD ?
86 TRACE_CAT_DATA : TRACE_CAT_CTRL);
87 msg_comm_t comm = MSG_task_isend(task, dest);
88 sent_comm.push_back(comm);
91 bool communicator::recv(message*& msg, m_host_t& from, double timeout)
94 volatile double deadline =
95 timeout > 0 ? MSG_get_clock() + timeout : 0.0;
96 xbt_mutex_acquire(mutex);
97 while (received.empty() && (!deadline || deadline > MSG_get_clock())) {
99 XBT_DEBUG("waiting for a message to come");
102 xbt_cond_timedwait(cond, mutex, deadline - MSG_get_clock());
104 xbt_cond_wait(cond, mutex);
107 if (e.category != timeout_error)
112 xbt_mutex_release(mutex);
115 if (received.empty())
118 m_task_t task = received.front();
120 msg = static_cast<message*>(MSG_task_get_data(task));
121 from = MSG_task_get_source(task);
122 MSG_task_destroy(task);
124 XBT_DEBUG("received %s from %s",
125 msg->to_string().c_str(), MSG_host_get_name(from));
130 void communicator::flush(bool wait)
132 sent_comm.remove_if(comm_test_n_destroy);
133 if (wait && !sent_comm.empty()) {
134 msg_comm_t comms[sent_comm.size()];
135 std::copy(sent_comm.begin(), sent_comm.end(), comms);
136 MSG_comm_waitall(comms, sent_comm.size(), -1.0);
137 if (!MSG_WAIT_DESTROYS_COMMS)
138 std::for_each(sent_comm.begin(), sent_comm.end(), MSG_comm_destroy);
143 bool communicator::comm_test_n_destroy(msg_comm_t comm)
145 if (MSG_comm_test(comm)) {
146 MSG_comm_destroy(comm);
152 int communicator::receiver_wrapper(int, char* [])
155 comm = static_cast<communicator*>(MSG_process_get_data(MSG_process_self()));
156 int result = comm->receiver();
158 XBT_DEBUG("terminate");
159 xbt_mutex_acquire(comm->mutex);
160 comm->receiver_process = NULL;
161 xbt_cond_signal(comm->cond);
162 xbt_mutex_release(comm->mutex);
167 void communicator::receiver1(msg_comm_t& comm, m_task_t& task, const char* mbox)
169 MSG_comm_destroy(comm);
170 if (strcmp(MSG_task_get_name(task), "finalize")) {
171 XBT_DEBUG("received message on %s", mbox);
172 xbt_mutex_acquire(mutex);
174 xbt_cond_signal(cond);
175 xbt_mutex_release(mutex);
177 comm = MSG_task_irecv(&task, mbox);
179 XBT_DEBUG("received finalize on %s", mbox);
180 MSG_task_destroy(task);
186 int communicator::receiver()
188 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
189 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
190 XBT_DEBUG("receiver ready");
191 xbt_mutex_acquire(mutex);
192 xbt_cond_signal(cond); // signal master that we are ready
193 xbt_mutex_release(mutex);
195 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
196 while (ctrl_comm || data_comm) {
199 xbt_dynar_push(comms, &ctrl_comm);
201 xbt_dynar_push(comms, &data_comm);
202 int recvd = MSG_comm_waitany(comms);
203 msg_comm_t comm = xbt_dynar_get_as(comms, recvd, msg_comm_t);
204 xbt_dynar_reset(comms);
206 if (comm == ctrl_comm)
207 receiver1(ctrl_comm, ctrl_task, get_ctrl_mbox());
208 else if (comm == data_comm)
209 receiver1(data_comm, data_task, get_data_mbox());
211 XBT_ERROR("Handling unknown comm -- %p", comm);
212 MSG_comm_destroy(comm);
215 xbt_dynar_free(&comms);