2 #include <tr1/functional>
6 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm);
10 #include "simgrid_features.h"
13 #include "communicator.h"
17 void check_for_lost_messages(size_t size, const char* descr)
20 XBT_WARN("lost %zu %s message%s!", size, descr, ESSE(size));
25 communicator::communicator()
26 : host(static_cast<hostdata*>(MSG_host_get_data(MSG_host_self())))
29 receiver_thread = new_msg_thread("receiver",
30 bind(&communicator::receiver, this));
31 receiver_thread->start();
34 communicator::~communicator()
38 XBT_DEBUG("send finalize to receiver/ctrl");
39 task = MSG_task_create("finalize", 0.0, 0, NULL);
40 MSG_task_send(task, host->get_ctrl_mbox());
42 XBT_DEBUG("send finalize to receiver/data");
43 task = MSG_task_create("finalize", 0.0, 0, NULL);
44 MSG_task_send(task, host->get_data_mbox());
46 receiver_thread->wait();
47 delete receiver_thread;
49 check_for_lost_messages(ctrl_received.size(), "received ctrl");
50 check_for_lost_messages(data_received.size(), "received data");
51 check_for_lost_messages(ctrl_sent.size(), "sent ctrl");
52 check_for_lost_messages(data_sent.size(), "sent data");
55 msg_comm_t communicator::real_send(const char* dest, message* msg)
57 XBT_DEBUG("send %s to %s", msg->to_string().c_str(), dest);
58 double msg_size = sizeof *msg;
59 if (msg->get_type() == message::LOAD)
60 msg_size += opt::comm_cost(msg->get_amount());
61 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
62 TRACE_msg_set_task_category(task,
63 msg->get_type() == message::LOAD ?
64 TRACE_CAT_DATA : TRACE_CAT_CTRL);
65 return MSG_task_isend(task, dest);
68 void communicator::real_flush(sent_comm_type& sent_comm, bool wait)
70 sent_comm_type::iterator bound;
71 bound = std::remove_if(sent_comm.begin(), sent_comm.end(),
73 sent_comm.erase(bound, sent_comm.end());
74 if (wait && !sent_comm.empty()) {
75 size_t size = sent_comm.size();
76 msg_comm_t* comms = new msg_comm_t[size];
77 std::copy(sent_comm.begin(), sent_comm.end(), comms);
79 MSG_comm_waitall(comms, size, -1.0);
80 if (!MSG_WAIT_DESTROYS_COMMS)
81 std::for_each(comms, comms + size, comm_check_n_destroy);
86 void communicator::receiver()
88 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
93 message_queue& received;
95 channel chan[] = { { NULL, NULL, host->get_ctrl_mbox(), ctrl_received },
96 { NULL, NULL, host->get_data_mbox(), data_received } };
97 const int chan_size = (sizeof chan) / (sizeof chan[0]);
99 for (int i = 0 ; i < chan_size ; ++i) {
100 chan[i].comm = MSG_task_irecv(&chan[i].task, chan[i].mbox);
101 xbt_dynar_push(comms, &chan[i].comm);
104 while (!xbt_dynar_is_empty(comms)) {
106 int index = MSG_comm_waitany(comms);
107 msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
110 for (ch = chan ; ch->comm != finished_comm ; ++ch)
113 comm_check_n_destroy(ch->comm);
114 if (strcmp(MSG_task_get_name(ch->task), "finalize")) {
115 XBT_DEBUG("received message on %s", ch->mbox);
116 ch->received.push(ch->task);
118 ch->comm = MSG_task_irecv(&ch->task, ch->mbox);
119 xbt_dynar_set_as(comms, index, msg_comm_t, ch->comm);
121 XBT_DEBUG("received finalize on %s", ch->mbox);
122 MSG_task_destroy(ch->task);
125 xbt_dynar_remove_at(comms, index, NULL);
129 xbt_dynar_free(&comms);
132 void communicator::comm_check_n_destroy(msg_comm_t comm)
134 xbt_assert(MSG_comm_get_status(comm) == MSG_OK);
135 MSG_comm_destroy(comm);
138 bool communicator::comm_test_n_destroy(msg_comm_t comm)
140 if (MSG_comm_test(comm)) {
141 comm_check_n_destroy(comm);