5 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm);
9 #include "simgrid_features.h"
12 #include "communicator.h"
14 communicator::communicator()
15 : host(static_cast<hostdata*>(MSG_host_get_data(MSG_host_self())))
17 receiver_mutex.acquire();
19 MSG_process_create("receiver", communicator::receiver_wrapper,
20 this, MSG_host_self());
21 receiver_cond.wait(receiver_mutex); // wait for the receiver to be ready
22 receiver_mutex.release();
25 communicator::~communicator()
29 XBT_DEBUG("send finalize to receiver/ctrl");
30 task = MSG_task_create("finalize", 0.0, 0, NULL);
31 MSG_task_send(task, host->get_ctrl_mbox());
33 XBT_DEBUG("send finalize to receiver/data");
34 task = MSG_task_create("finalize", 0.0, 0, NULL);
35 MSG_task_send(task, host->get_data_mbox());
37 receiver_mutex.acquire();
38 while (receiver_thread) {
39 XBT_DEBUG("waiting for receiver to terminate");
40 receiver_cond.wait(receiver_mutex);
42 receiver_mutex.release();
44 if (!received.empty())
45 XBT_WARN("lost %zu received message%s!",
46 received.size(), ESSE(received.size()));
47 if (!sent_comm.empty())
48 XBT_WARN("lost %zu sent message%s!",
49 sent_comm.size(), ESSE(sent_comm.size()));
52 void communicator::send(const char* dest, message* msg)
54 XBT_DEBUG("send %s to %s", msg->to_string().c_str(), dest);
55 double msg_size = sizeof *msg;
56 if (msg->get_type() == message::LOAD)
57 msg_size += opt::comm_cost(msg->get_amount());
58 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
59 TRACE_msg_set_task_category(task,
60 msg->get_type() == message::LOAD ?
61 TRACE_CAT_DATA : TRACE_CAT_CTRL);
62 msg_comm_t comm = MSG_task_isend(task, dest);
63 sent_comm.push_back(comm);
66 void communicator::flush(bool wait)
68 sent_comm.remove_if(comm_test_n_destroy);
69 if (wait && !sent_comm.empty()) {
70 msg_comm_t comms[sent_comm.size()];
71 std::copy(sent_comm.begin(), sent_comm.end(), comms);
72 MSG_comm_waitall(comms, sent_comm.size(), -1.0);
73 if (!MSG_WAIT_DESTROYS_COMMS)
74 std::for_each(sent_comm.begin(), sent_comm.end(),
75 comm_check_n_destroy);
80 bool communicator::recv(message*& msg, m_host_t& from, double timeout)
82 XBT_DEBUG("waiting for a message to come");
83 bool recvd = received.pop(msg, from, timeout);
85 XBT_DEBUG("received %s from %s",
86 msg->to_string().c_str(), MSG_host_get_name(from));
90 int communicator::receiver_wrapper(int, char* [])
93 comm = static_cast<communicator*>(MSG_process_get_data(MSG_process_self()));
96 XBT_DEBUG("terminate");
97 comm->receiver_mutex.acquire();
98 comm->receiver_thread = NULL;
99 comm->receiver_cond.signal();
100 comm->receiver_mutex.release();
105 void communicator::receiver()
107 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
113 channel chan[] = { { NULL, NULL, host->get_ctrl_mbox() },
114 { NULL, NULL, host->get_data_mbox() } };
115 const int chan_size = (sizeof chan) / (sizeof chan[0]);
117 for (int i = 0 ; i < chan_size ; ++i) {
118 chan[i].comm = MSG_task_irecv(&chan[i].task, chan[i].mbox);
119 xbt_dynar_push(comms, &chan[i].comm);
122 XBT_DEBUG("receiver ready");
123 receiver_mutex.acquire();
124 receiver_cond.signal(); // signal master that we are ready
125 receiver_mutex.release();
127 while (!xbt_dynar_is_empty(comms)) {
129 int index = MSG_comm_waitany(comms);
130 msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
133 for (ch = chan ; ch->comm != finished_comm ; ++ch)
136 comm_check_n_destroy(ch->comm);
137 if (strcmp(MSG_task_get_name(ch->task), "finalize")) {
138 XBT_DEBUG("received message on %s", ch->mbox);
139 received.push(ch->task);
141 ch->comm = MSG_task_irecv(&ch->task, ch->mbox);
142 xbt_dynar_set_as(comms, index, msg_comm_t, ch->comm);
144 XBT_DEBUG("received finalize on %s", ch->mbox);
145 MSG_task_destroy(ch->task);
148 xbt_dynar_remove_at(comms, index, NULL);
152 xbt_dynar_free(&comms);
155 void communicator::comm_check_n_destroy(msg_comm_t comm)
157 xbt_assert(MSG_comm_get_status(comm) == MSG_OK);
158 MSG_comm_destroy(comm);
161 bool communicator::comm_test_n_destroy(msg_comm_t comm)
163 if (MSG_comm_test(comm)) {
164 comm_check_n_destroy(comm);