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_type::iterator bound;
64 bound = std::remove_if(sent_comm.begin(), sent_comm.end(),
66 sent_comm.erase(bound, sent_comm.end());
67 if (wait && !sent_comm.empty()) {
68 msg_comm_t comms[sent_comm.size()];
69 std::copy(sent_comm.begin(), sent_comm.end(), comms);
70 MSG_comm_waitall(comms, sent_comm.size(), -1.0);
71 if (!MSG_WAIT_DESTROYS_COMMS)
72 std::for_each(sent_comm.begin(), sent_comm.end(),
73 comm_check_n_destroy);
78 void communicator::receiver()
80 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
86 channel chan[] = { { NULL, NULL, host->get_ctrl_mbox() },
87 { NULL, NULL, host->get_data_mbox() } };
88 const int chan_size = (sizeof chan) / (sizeof chan[0]);
90 for (int i = 0 ; i < chan_size ; ++i) {
91 chan[i].comm = MSG_task_irecv(&chan[i].task, chan[i].mbox);
92 xbt_dynar_push(comms, &chan[i].comm);
95 while (!xbt_dynar_is_empty(comms)) {
97 int index = MSG_comm_waitany(comms);
98 msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
101 for (ch = chan ; ch->comm != finished_comm ; ++ch)
104 comm_check_n_destroy(ch->comm);
105 if (strcmp(MSG_task_get_name(ch->task), "finalize")) {
106 XBT_DEBUG("received message on %s", ch->mbox);
107 received.push(ch->task);
109 ch->comm = MSG_task_irecv(&ch->task, ch->mbox);
110 xbt_dynar_set_as(comms, index, msg_comm_t, ch->comm);
112 XBT_DEBUG("received finalize on %s", ch->mbox);
113 MSG_task_destroy(ch->task);
116 xbt_dynar_remove_at(comms, index, NULL);
120 xbt_dynar_free(&comms);
123 void communicator::comm_check_n_destroy(msg_comm_t comm)
125 xbt_assert(MSG_comm_get_status(comm) == MSG_OK);
126 MSG_comm_destroy(comm);
129 bool communicator::comm_test_n_destroy(msg_comm_t comm)
131 if (MSG_comm_test(comm)) {
132 comm_check_n_destroy(comm);