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_mutex_release(mutex);
40 communicator::~communicator()
44 DEBUG0("send finalize to receiver/ctrl");
45 task = MSG_task_create("finalize", 0.0, 0, NULL);
46 MSG_task_send(task, get_ctrl_mbox());
48 DEBUG0("send finalize to receiver/data");
49 task = MSG_task_create("finalize", 0.0, 0, NULL);
50 MSG_task_send(task, get_data_mbox());
52 xbt_mutex_acquire(mutex);
53 while (receiver_process) {
54 DEBUG0("waiting for receiver to terminate");
55 xbt_cond_wait(cond, mutex);
57 xbt_mutex_release(mutex);
60 WARN0("ctrl_comm is pending!");
62 WARN0("data_comm is pending!");
63 if (!received.empty())
64 WARN2("lost %lu received message%s!",
65 (unsigned long )received.size(), ESSE(received.size()));
66 if (!sent_comm.empty())
67 WARN2("lost %lu sent message%s!",
68 (unsigned long )sent_comm.size(), ESSE(sent_comm.size()));
70 xbt_cond_destroy(cond);
71 xbt_mutex_destroy(mutex);
74 void communicator::send(const char* dest, message* msg)
76 DEBUG2("send %s to %s", msg->to_string().c_str(), dest);
77 double msg_size = sizeof *msg;
78 if (msg->get_type() == message::LOAD)
79 msg_size += opt::comm_cost(msg->get_amount());
80 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
81 msg_comm_t comm = MSG_task_isend(task, dest);
82 sent_comm.push_back(comm);
85 bool communicator::recv(message*& msg, m_host_t& from, bool wait)
88 xbt_mutex_acquire(mutex);
89 while (received.empty()) {
90 DEBUG0("waiting for a message to come");
91 xbt_cond_wait(cond, mutex);
93 xbt_mutex_release(mutex);
99 m_task_t task = received.front();
101 msg = (message* )MSG_task_get_data(task);
102 from = MSG_task_get_source(task);
103 MSG_task_destroy(task);
105 DEBUG2("received %s from %s",
106 msg->to_string().c_str(), MSG_host_get_name(from));
111 void communicator::flush(bool wait)
113 using std::tr1::bind;
114 using std::tr1::placeholders::_1;
116 sent_comm.remove_if(comm_test_n_destroy);
117 if (wait && !sent_comm.empty()) {
118 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
119 while (!sent_comm.empty()) {
120 std::for_each(sent_comm.begin(), sent_comm.end(),
122 comms, bind(misc::address<msg_comm_t>(), _1)));
123 MSG_comm_waitany(comms);
124 xbt_dynar_reset(comms);
125 sent_comm.remove_if(comm_test_n_destroy);
127 xbt_dynar_free(&comms);
131 bool communicator::comm_test_n_destroy(msg_comm_t comm)
133 if (MSG_comm_test(comm)) {
134 MSG_comm_destroy(comm);
140 int communicator::receiver_wrapper(int, char* [])
143 comm = (communicator* )MSG_process_get_data(MSG_process_self());
144 int result = comm->receiver();
147 xbt_mutex_acquire(comm->mutex);
148 comm->receiver_process = NULL;
149 xbt_cond_signal(comm->cond);
150 xbt_mutex_release(comm->mutex);
155 int communicator::receiver()
157 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
158 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
159 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
160 while (ctrl_comm || data_comm) {
163 xbt_dynar_push(comms, &ctrl_comm);
165 xbt_dynar_push(comms, &data_comm);
166 MSG_comm_waitany(comms);
167 xbt_dynar_reset(comms);
169 if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
170 if (strcmp(MSG_task_get_name(ctrl_task), "finalize")) {
171 DEBUG0("received message from ctrl");
172 received.push(ctrl_task);
174 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
176 DEBUG0("received finalize from ctrl");
177 MSG_task_destroy(ctrl_task);
183 if (data_comm && comm_test_n_destroy(data_comm)) {
184 if (strcmp(MSG_task_get_name(data_task), "finalize")) {
185 DEBUG0("received message from data");
186 received.push(data_task);
188 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
190 DEBUG0("received finalize from data");
191 MSG_task_destroy(data_task);
196 xbt_mutex_acquire(mutex);
197 xbt_cond_signal(cond);
198 xbt_mutex_release(mutex);
200 xbt_dynar_free(&comms);