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 const int communicator::send_count_before_flush = 4;
26 communicator::communicator()
27 : host((hostdata* )MSG_host_get_data(MSG_host_self()))
28 , mutex(xbt_mutex_init())
29 , cond(xbt_cond_init())
36 xbt_mutex_acquire(mutex);
38 MSG_process_create("receiver", communicator::receiver_wrapper,
39 this, MSG_host_self());
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 DEBUG0("wait for receiver to terminate");
56 xbt_mutex_acquire(mutex);
57 while (receiver_process)
58 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 msg_comm_t comm = MSG_task_isend(task, dest);
84 sent_comm.push_back(comm);
86 if (++send_counter >= send_count_before_flush) {
92 bool communicator::recv(message*& msg, m_host_t& from, bool wait)
95 DEBUG0("suspend main process on recv");
96 xbt_mutex_acquire(mutex);
97 while (received.empty())
98 xbt_cond_wait(cond, mutex);
99 xbt_mutex_release(mutex);
102 if (received.empty())
105 m_task_t task = received.front();
107 msg = (message* )MSG_task_get_data(task);
108 from = MSG_task_get_source(task);
109 MSG_task_destroy(task);
111 DEBUG2("received %s from %s",
112 msg->to_string().c_str(), MSG_host_get_name(from));
117 void communicator::flush(bool wait)
119 using std::tr1::bind;
120 using std::tr1::placeholders::_1;
122 sent_comm.remove_if(comm_test_n_destroy);
123 if (wait && !sent_comm.empty()) {
124 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
125 while (!sent_comm.empty()) {
126 std::for_each(sent_comm.begin(), sent_comm.end(),
128 comms, bind(misc::address<msg_comm_t>(), _1)));
129 MSG_comm_waitany(comms);
130 xbt_dynar_reset(comms);
131 sent_comm.remove_if(comm_test_n_destroy);
133 xbt_dynar_free(&comms);
137 bool communicator::comm_test_n_destroy(msg_comm_t comm)
139 if (MSG_comm_test(comm)) {
140 MSG_comm_destroy(comm);
146 int communicator::receiver_wrapper(int, char* [])
149 comm = (communicator* )MSG_process_get_data(MSG_process_self());
150 int result = comm->receiver();
153 xbt_mutex_acquire(comm->mutex);
154 comm->receiver_process = NULL;
155 xbt_cond_signal(comm->cond);
156 xbt_mutex_release(comm->mutex);
161 int communicator::receiver()
163 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
164 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
165 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
166 while (ctrl_comm || data_comm) {
169 xbt_dynar_push(comms, &ctrl_comm);
171 xbt_dynar_push(comms, &data_comm);
172 MSG_comm_waitany(comms);
173 xbt_dynar_reset(comms);
175 if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
176 if (strcmp(MSG_task_get_name(ctrl_task), "finalize")) {
177 DEBUG0("received message from ctrl");
178 received.push(ctrl_task);
180 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
182 DEBUG0("received finalize from ctrl");
183 MSG_task_destroy(ctrl_task);
189 if (data_comm && comm_test_n_destroy(data_comm)) {
190 if (strcmp(MSG_task_get_name(data_task), "finalize")) {
191 DEBUG0("received message from data");
192 received.push(data_task);
194 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
196 DEBUG0("received finalize from data");
197 MSG_task_destroy(data_task);
202 xbt_mutex_acquire(mutex);
203 xbt_cond_signal(cond);
204 xbt_mutex_release(mutex);
206 xbt_dynar_free(&comms);