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, bool wait)
89 xbt_mutex_acquire(mutex);
90 while (received.empty()) {
91 DEBUG0("waiting for a message to come");
92 xbt_cond_wait(cond, mutex);
94 xbt_mutex_release(mutex);
100 m_task_t task = received.front();
102 msg = (message* )MSG_task_get_data(task);
103 from = MSG_task_get_source(task);
104 MSG_task_destroy(task);
106 DEBUG2("received %s from %s",
107 msg->to_string().c_str(), MSG_host_get_name(from));
112 void communicator::flush(bool wait)
114 using std::tr1::bind;
115 using std::tr1::placeholders::_1;
117 sent_comm.remove_if(comm_test_n_destroy);
118 if (wait && !sent_comm.empty()) {
119 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
120 while (!sent_comm.empty()) {
121 std::for_each(sent_comm.begin(), sent_comm.end(),
123 comms, bind(misc::address<msg_comm_t>(), _1)));
124 MSG_comm_waitany(comms);
125 xbt_dynar_reset(comms);
126 sent_comm.remove_if(comm_test_n_destroy);
128 xbt_dynar_free(&comms);
132 bool communicator::comm_test_n_destroy(msg_comm_t comm)
134 if (MSG_comm_test(comm)) {
135 MSG_comm_destroy(comm);
141 int communicator::receiver_wrapper(int, char* [])
144 comm = (communicator* )MSG_process_get_data(MSG_process_self());
145 int result = comm->receiver();
148 xbt_mutex_acquire(comm->mutex);
149 comm->receiver_process = NULL;
150 xbt_cond_signal(comm->cond);
151 xbt_mutex_release(comm->mutex);
156 int communicator::receiver()
158 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
159 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
160 DEBUG0("receiver ready");
161 xbt_mutex_acquire(mutex);
162 xbt_cond_signal(cond); // signal master that we are ready
163 xbt_mutex_release(mutex);
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 xbt_mutex_acquire(mutex);
179 received.push(ctrl_task);
180 xbt_mutex_release(mutex);
182 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
184 DEBUG0("received finalize from ctrl");
185 MSG_task_destroy(ctrl_task);
191 if (data_comm && comm_test_n_destroy(data_comm)) {
192 if (strcmp(MSG_task_get_name(data_task), "finalize")) {
193 DEBUG0("received message from data");
194 xbt_mutex_acquire(mutex);
195 received.push(data_task);
196 xbt_mutex_release(mutex);
198 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
200 DEBUG0("received finalize from data");
201 MSG_task_destroy(data_task);
206 xbt_mutex_acquire(mutex);
207 if (!received.empty())
208 xbt_cond_signal(cond);
209 xbt_mutex_release(mutex);
211 xbt_dynar_free(&comms);