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 DEBUG0("send finalize to receiver/ctrl");
48 task = MSG_task_create("finalize", 0.0, 0, NULL);
49 MSG_task_send(task, get_ctrl_mbox());
51 DEBUG0("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 DEBUG0("waiting for receiver to terminate");
58 xbt_cond_wait(cond, mutex);
60 xbt_mutex_release(mutex);
63 WARN0("ctrl_comm is pending!");
65 WARN0("data_comm is pending!");
66 if (!received.empty())
67 WARN2("lost %lu received message%s!",
68 (unsigned long )received.size(), ESSE(received.size()));
69 if (!sent_comm.empty())
70 WARN2("lost %lu sent message%s!",
71 (unsigned long )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 DEBUG2("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 DEBUG0("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 DEBUG2("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();
159 xbt_mutex_acquire(comm->mutex);
160 comm->receiver_process = NULL;
161 xbt_cond_signal(comm->cond);
162 xbt_mutex_release(comm->mutex);
167 int communicator::receiver()
169 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
170 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
171 DEBUG0("receiver ready");
172 xbt_mutex_acquire(mutex);
173 xbt_cond_signal(cond); // signal master that we are ready
174 xbt_mutex_release(mutex);
176 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
177 while (ctrl_comm || data_comm) {
180 xbt_dynar_push(comms, &ctrl_comm);
182 xbt_dynar_push(comms, &data_comm);
183 MSG_comm_waitany(comms);
184 xbt_dynar_reset(comms);
186 if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
187 if (strcmp(MSG_task_get_name(ctrl_task), "finalize")) {
188 DEBUG0("received message from ctrl");
189 xbt_mutex_acquire(mutex);
190 received.push(ctrl_task);
191 xbt_mutex_release(mutex);
193 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
195 DEBUG0("received finalize from ctrl");
196 MSG_task_destroy(ctrl_task);
202 if (data_comm && comm_test_n_destroy(data_comm)) {
203 if (strcmp(MSG_task_get_name(data_task), "finalize")) {
204 DEBUG0("received message from data");
205 xbt_mutex_acquire(mutex);
206 received.push(data_task);
207 xbt_mutex_release(mutex);
209 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
211 DEBUG0("received finalize from data");
212 MSG_task_destroy(data_task);
217 xbt_mutex_acquire(mutex);
218 if (!received.empty())
219 xbt_cond_signal(cond);
220 xbt_mutex_release(mutex);
222 xbt_dynar_free(&comms);