3 #include <tr1/functional>
8 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm);
14 #include "communicator.h"
16 std::string message::to_string()
18 static const char* str[] = { "INFO", "CREDIT", "LOAD",
19 "CTRL_CLOSE", "DATA_CLOSE" };
20 std::ostringstream oss;
21 oss << str[type] << ": " << amount;
25 communicator::communicator()
26 : host((hostdata* )MSG_host_get_data(MSG_host_self()))
27 , mutex(xbt_mutex_init())
28 , cond(xbt_cond_init())
34 xbt_mutex_acquire(mutex);
36 MSG_process_create("receiver", communicator::receiver_wrapper,
37 this, MSG_host_self());
38 xbt_cond_wait(cond, mutex); // wait for the receiver to be ready
39 xbt_mutex_release(mutex);
42 communicator::~communicator()
46 DEBUG0("send finalize to receiver/ctrl");
47 task = MSG_task_create("finalize", 0.0, 0, NULL);
48 MSG_task_send(task, get_ctrl_mbox());
50 DEBUG0("send finalize to receiver/data");
51 task = MSG_task_create("finalize", 0.0, 0, NULL);
52 MSG_task_send(task, get_data_mbox());
54 xbt_mutex_acquire(mutex);
55 while (receiver_process) {
56 DEBUG0("waiting for receiver to terminate");
57 xbt_cond_wait(cond, mutex);
59 xbt_mutex_release(mutex);
62 WARN0("ctrl_comm is pending!");
64 WARN0("data_comm is pending!");
65 if (!received.empty())
66 WARN2("lost %lu received message%s!",
67 (unsigned long )received.size(), ESSE(received.size()));
68 if (!sent_comm.empty())
69 WARN2("lost %lu sent message%s!",
70 (unsigned long )sent_comm.size(), ESSE(sent_comm.size()));
72 xbt_cond_destroy(cond);
73 xbt_mutex_destroy(mutex);
76 void communicator::send(const char* dest, message* msg)
78 DEBUG2("send %s to %s", msg->to_string().c_str(), dest);
79 double msg_size = sizeof *msg;
80 if (msg->get_type() == message::LOAD)
81 msg_size += opt::comm_cost(msg->get_amount());
82 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
83 TRACE_msg_set_task_category(task,
84 msg->get_type() == message::LOAD ?
85 TRACE_CAT_DATA : TRACE_CAT_CTRL);
86 msg_comm_t comm = MSG_task_isend(task, dest);
87 sent_comm.push_back(comm);
90 bool communicator::recv(message*& msg, m_host_t& from, double timeout)
93 volatile double deadline =
94 timeout > 0 ? MSG_get_clock() + timeout : 0.0;
95 xbt_mutex_acquire(mutex);
96 while (received.empty() && (!deadline || deadline > MSG_get_clock())) {
98 DEBUG0("waiting for a message to come");
101 xbt_cond_timedwait(cond, mutex, deadline - MSG_get_clock());
103 xbt_cond_wait(cond, mutex);
106 if (e.category != timeout_error)
111 xbt_mutex_release(mutex);
114 if (received.empty())
117 m_task_t task = received.front();
119 msg = (message* )MSG_task_get_data(task);
120 from = MSG_task_get_source(task);
121 MSG_task_destroy(task);
123 DEBUG2("received %s from %s",
124 msg->to_string().c_str(), MSG_host_get_name(from));
129 void communicator::flush(bool wait)
131 using std::tr1::bind;
132 using std::tr1::placeholders::_1;
134 sent_comm.remove_if(comm_test_n_destroy);
135 if (wait && !sent_comm.empty()) {
136 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
137 while (!sent_comm.empty()) {
138 std::for_each(sent_comm.begin(), sent_comm.end(),
140 comms, bind(misc::address<msg_comm_t>(), _1)));
141 MSG_comm_waitany(comms);
142 xbt_dynar_reset(comms);
143 sent_comm.remove_if(comm_test_n_destroy);
145 xbt_dynar_free(&comms);
149 bool communicator::comm_test_n_destroy(msg_comm_t comm)
151 if (MSG_comm_test(comm)) {
152 MSG_comm_destroy(comm);
158 int communicator::receiver_wrapper(int, char* [])
161 comm = (communicator* )MSG_process_get_data(MSG_process_self());
162 int result = comm->receiver();
165 xbt_mutex_acquire(comm->mutex);
166 comm->receiver_process = NULL;
167 xbt_cond_signal(comm->cond);
168 xbt_mutex_release(comm->mutex);
173 int communicator::receiver()
175 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
176 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
177 DEBUG0("receiver ready");
178 xbt_mutex_acquire(mutex);
179 xbt_cond_signal(cond); // signal master that we are ready
180 xbt_mutex_release(mutex);
182 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
183 while (ctrl_comm || data_comm) {
186 xbt_dynar_push(comms, &ctrl_comm);
188 xbt_dynar_push(comms, &data_comm);
189 MSG_comm_waitany(comms);
190 xbt_dynar_reset(comms);
192 if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
193 if (strcmp(MSG_task_get_name(ctrl_task), "finalize")) {
194 DEBUG0("received message from ctrl");
195 xbt_mutex_acquire(mutex);
196 received.push(ctrl_task);
197 xbt_mutex_release(mutex);
199 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
201 DEBUG0("received finalize from ctrl");
202 MSG_task_destroy(ctrl_task);
208 if (data_comm && comm_test_n_destroy(data_comm)) {
209 if (strcmp(MSG_task_get_name(data_task), "finalize")) {
210 DEBUG0("received message from data");
211 xbt_mutex_acquire(mutex);
212 received.push(data_task);
213 xbt_mutex_release(mutex);
215 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
217 DEBUG0("received finalize from data");
218 MSG_task_destroy(data_task);
223 xbt_mutex_acquire(mutex);
224 if (!received.empty())
225 xbt_cond_signal(cond);
226 xbt_mutex_release(mutex);
228 xbt_dynar_free(&comms);