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(), MSG_comm_destroy);
145 bool communicator::comm_test_n_destroy(msg_comm_t comm)
147 if (MSG_comm_test(comm)) {
148 MSG_comm_destroy(comm);
154 int communicator::receiver_wrapper(int, char* [])
157 comm = static_cast<communicator*>(MSG_process_get_data(MSG_process_self()));
158 int result = comm->receiver();
160 XBT_DEBUG("terminate");
161 xbt_mutex_acquire(comm->receiver_mutex);
162 comm->receiver_thread = NULL;
163 xbt_cond_signal(comm->receiver_cond);
164 xbt_mutex_release(comm->receiver_mutex);
169 void communicator::receiver1(msg_comm_t& comm, m_task_t& task, const char* mbox)
171 MSG_comm_destroy(comm);
172 if (strcmp(MSG_task_get_name(task), "finalize")) {
173 XBT_DEBUG("received message on %s", mbox);
174 xbt_mutex_acquire(receiver_mutex);
176 xbt_cond_signal(receiver_cond);
177 xbt_mutex_release(receiver_mutex);
179 comm = MSG_task_irecv(&task, mbox);
181 XBT_DEBUG("received finalize on %s", mbox);
182 MSG_task_destroy(task);
188 int communicator::receiver()
190 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
191 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
192 XBT_DEBUG("receiver ready");
193 xbt_mutex_acquire(receiver_mutex);
194 xbt_cond_signal(receiver_cond); // signal master that we are ready
195 xbt_mutex_release(receiver_mutex);
197 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
198 while (ctrl_comm || data_comm) {
201 xbt_dynar_push(comms, &ctrl_comm);
203 xbt_dynar_push(comms, &data_comm);
204 int index = MSG_comm_waitany(comms);
205 msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
206 xbt_dynar_reset(comms);
208 if (finished_comm == ctrl_comm)
209 receiver1(ctrl_comm, ctrl_task, get_ctrl_mbox());
210 else if (finished_comm == data_comm)
211 receiver1(data_comm, data_task, get_data_mbox());
213 XBT_ERROR("Handling unknown comm -- %p", finished_comm);
214 MSG_comm_destroy(finished_comm);
217 xbt_dynar_free(&comms);