3 #include <tr1/functional>
8 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm);
13 #include "communicator.h"
15 std::string message::to_string()
17 static const char* str[] = { "INFO", "CREDIT", "LOAD",
18 "CTRL_CLOSE", "DATA_CLOSE" };
19 std::ostringstream oss;
20 oss << str[type] << ": " << amount;
24 communicator::communicator()
25 : host((hostdata* )MSG_host_get_data(MSG_host_self()))
26 , mutex(xbt_mutex_init())
27 , cond(xbt_cond_init())
33 xbt_mutex_acquire(mutex);
35 MSG_process_create("receiver", communicator::receiver_wrapper,
36 this, MSG_host_self());
37 xbt_cond_wait(cond, mutex); // wait for the receiver to be ready
38 xbt_mutex_release(mutex);
41 communicator::~communicator()
45 DEBUG0("send finalize to receiver/ctrl");
46 task = MSG_task_create("finalize", 0.0, 0, NULL);
47 MSG_task_send(task, get_ctrl_mbox());
49 DEBUG0("send finalize to receiver/data");
50 task = MSG_task_create("finalize", 0.0, 0, NULL);
51 MSG_task_send(task, get_data_mbox());
53 xbt_mutex_acquire(mutex);
54 while (receiver_process) {
55 DEBUG0("waiting for receiver to terminate");
56 xbt_cond_wait(cond, mutex);
58 xbt_mutex_release(mutex);
61 WARN0("ctrl_comm is pending!");
63 WARN0("data_comm is pending!");
64 if (!received.empty())
65 WARN2("lost %lu received message%s!",
66 (unsigned long )received.size(), ESSE(received.size()));
67 if (!sent_comm.empty())
68 WARN2("lost %lu sent message%s!",
69 (unsigned long )sent_comm.size(), ESSE(sent_comm.size()));
71 xbt_cond_destroy(cond);
72 xbt_mutex_destroy(mutex);
75 void communicator::send(const char* dest, message* msg)
77 DEBUG2("send %s to %s", msg->to_string().c_str(), dest);
78 double msg_size = sizeof *msg;
79 if (msg->get_type() == message::LOAD)
80 msg_size += opt::comm_cost(msg->get_amount());
81 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
82 msg_comm_t comm = MSG_task_isend(task, dest);
83 sent_comm.push_back(comm);
86 bool communicator::recv(message*& msg, m_host_t& from, double timeout)
89 volatile double deadline =
90 timeout > 0 ? MSG_get_clock() + timeout : 0.0;
91 xbt_mutex_acquire(mutex);
92 while (received.empty() && (!deadline || deadline > MSG_get_clock())) {
94 DEBUG0("waiting for a message to come");
97 xbt_cond_timedwait(cond, mutex, deadline - MSG_get_clock());
99 xbt_cond_wait(cond, mutex);
102 if (e.category != timeout_error)
107 xbt_mutex_release(mutex);
110 if (received.empty())
113 m_task_t task = received.front();
115 msg = (message* )MSG_task_get_data(task);
116 from = MSG_task_get_source(task);
117 MSG_task_destroy(task);
119 DEBUG2("received %s from %s",
120 msg->to_string().c_str(), MSG_host_get_name(from));
125 void communicator::flush(bool wait)
127 using std::tr1::bind;
128 using std::tr1::placeholders::_1;
130 sent_comm.remove_if(comm_test_n_destroy);
131 if (wait && !sent_comm.empty()) {
132 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
133 while (!sent_comm.empty()) {
134 std::for_each(sent_comm.begin(), sent_comm.end(),
136 comms, bind(misc::address<msg_comm_t>(), _1)));
137 MSG_comm_waitany(comms);
138 xbt_dynar_reset(comms);
139 sent_comm.remove_if(comm_test_n_destroy);
141 xbt_dynar_free(&comms);
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 = (communicator* )MSG_process_get_data(MSG_process_self());
158 int result = comm->receiver();
161 xbt_mutex_acquire(comm->mutex);
162 comm->receiver_process = NULL;
163 xbt_cond_signal(comm->cond);
164 xbt_mutex_release(comm->mutex);
169 int communicator::receiver()
171 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
172 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
173 DEBUG0("receiver ready");
174 xbt_mutex_acquire(mutex);
175 xbt_cond_signal(cond); // signal master that we are ready
176 xbt_mutex_release(mutex);
178 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
179 while (ctrl_comm || data_comm) {
182 xbt_dynar_push(comms, &ctrl_comm);
184 xbt_dynar_push(comms, &data_comm);
185 MSG_comm_waitany(comms);
186 xbt_dynar_reset(comms);
188 if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
189 if (strcmp(MSG_task_get_name(ctrl_task), "finalize")) {
190 DEBUG0("received message from ctrl");
191 xbt_mutex_acquire(mutex);
192 received.push(ctrl_task);
193 xbt_mutex_release(mutex);
195 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
197 DEBUG0("received finalize from ctrl");
198 MSG_task_destroy(ctrl_task);
204 if (data_comm && comm_test_n_destroy(data_comm)) {
205 if (strcmp(MSG_task_get_name(data_task), "finalize")) {
206 DEBUG0("received message from data");
207 xbt_mutex_acquire(mutex);
208 received.push(data_task);
209 xbt_mutex_release(mutex);
211 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
213 DEBUG0("received finalize from data");
214 MSG_task_destroy(data_task);
219 xbt_mutex_acquire(mutex);
220 if (!received.empty())
221 xbt_cond_signal(cond);
222 xbt_mutex_release(mutex);
224 xbt_dynar_free(&comms);