2 #include <tr1/functional>
6 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm);
10 #include "simgrid_features.h"
13 #include "communicator.h"
15 communicator::communicator()
16 : host(static_cast<hostdata*>(MSG_host_get_data(MSG_host_self())))
19 receiver_thread = new_msg_thread("receiver",
20 bind(&communicator::receiver, this));
21 receiver_thread->start();
24 communicator::~communicator()
28 XBT_DEBUG("send finalize to receiver/ctrl");
29 task = MSG_task_create("finalize", 0.0, 0, NULL);
30 MSG_task_send(task, host->get_ctrl_mbox());
32 XBT_DEBUG("send finalize to receiver/data");
33 task = MSG_task_create("finalize", 0.0, 0, NULL);
34 MSG_task_send(task, host->get_data_mbox());
36 receiver_thread->wait();
37 delete receiver_thread;
39 if (!received.empty())
40 XBT_WARN("lost %zu received message%s!",
41 received.size(), ESSE(received.size()));
42 if (!sent_comm.empty())
43 XBT_WARN("lost %zu sent message%s!",
44 sent_comm.size(), ESSE(sent_comm.size()));
47 void communicator::send(const char* dest, message* msg)
49 XBT_DEBUG("send %s to %s", msg->to_string().c_str(), dest);
50 double msg_size = sizeof *msg;
51 if (msg->get_type() == message::LOAD)
52 msg_size += opt::comm_cost(msg->get_amount());
53 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
54 TRACE_msg_set_task_category(task,
55 msg->get_type() == message::LOAD ?
56 TRACE_CAT_DATA : TRACE_CAT_CTRL);
57 msg_comm_t comm = MSG_task_isend(task, dest);
58 sent_comm.push_back(comm);
61 void communicator::flush(bool wait)
63 sent_comm.remove_if(comm_test_n_destroy);
64 if (wait && !sent_comm.empty()) {
65 msg_comm_t comms[sent_comm.size()];
66 std::copy(sent_comm.begin(), sent_comm.end(), comms);
67 MSG_comm_waitall(comms, sent_comm.size(), -1.0);
68 if (!MSG_WAIT_DESTROYS_COMMS)
69 std::for_each(sent_comm.begin(), sent_comm.end(),
70 comm_check_n_destroy);
75 bool communicator::recv(message*& msg, m_host_t& from, double timeout)
77 XBT_DEBUG("waiting for a message to come");
78 bool recvd = received.pop(msg, from, timeout);
80 XBT_DEBUG("received %s from %s",
81 msg->to_string().c_str(), MSG_host_get_name(from));
85 void communicator::receiver()
87 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
93 channel chan[] = { { NULL, NULL, host->get_ctrl_mbox() },
94 { NULL, NULL, host->get_data_mbox() } };
95 const int chan_size = (sizeof chan) / (sizeof chan[0]);
97 for (int i = 0 ; i < chan_size ; ++i) {
98 chan[i].comm = MSG_task_irecv(&chan[i].task, chan[i].mbox);
99 xbt_dynar_push(comms, &chan[i].comm);
102 while (!xbt_dynar_is_empty(comms)) {
104 int index = MSG_comm_waitany(comms);
105 msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
108 for (ch = chan ; ch->comm != finished_comm ; ++ch)
111 comm_check_n_destroy(ch->comm);
112 if (strcmp(MSG_task_get_name(ch->task), "finalize")) {
113 XBT_DEBUG("received message on %s", ch->mbox);
114 received.push(ch->task);
116 ch->comm = MSG_task_irecv(&ch->task, ch->mbox);
117 xbt_dynar_set_as(comms, index, msg_comm_t, ch->comm);
119 XBT_DEBUG("received finalize on %s", ch->mbox);
120 MSG_task_destroy(ch->task);
123 xbt_dynar_remove_at(comms, index, NULL);
127 xbt_dynar_free(&comms);
130 void communicator::comm_check_n_destroy(msg_comm_t comm)
132 xbt_assert(MSG_comm_get_status(comm) == MSG_OK);
133 MSG_comm_destroy(comm);
136 bool communicator::comm_test_n_destroy(msg_comm_t comm)
138 if (MSG_comm_test(comm)) {
139 comm_check_n_destroy(comm);