1 #include "communicator.h"
4 #include <tr1/functional>
8 #include "simgrid_features.h"
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm);
14 std::string message::to_string()
16 static const char* str[] = { "INFO", "CREDIT", "LOAD",
17 "CTRL_CLOSE", "DATA_CLOSE" };
18 std::ostringstream oss;
19 oss << str[type] << " (" << amount << ")";
23 const int communicator::send_count_before_flush = 128;
25 communicator::communicator()
26 : host((hostdata* )MSG_host_get_data(MSG_host_self()))
30 , ctrl_close_is_last(false)
33 , data_close_is_last(false)
37 communicator::~communicator()
40 WARN0("ctrl_comm is pending!");
42 WARN0("data_comm is pending!");
43 if (!sent_comm.empty())
44 WARN2("lost %ld send communication%s!",
45 (long )sent_comm.size(), ESSE(sent_comm.size()));
48 void communicator::listen()
50 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
51 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
54 void communicator::send(const char* dest, message* msg)
56 double msg_size = sizeof *msg;
57 if (msg->get_type() == message::LOAD)
58 msg_size += opt::comm_cost(msg->get_amount());
59 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
60 msg_comm_t comm = MSG_task_isend(task, dest);
61 sent_comm.push_back(comm);
63 if (++send_counter >= send_count_before_flush) {
69 bool communicator::recv(message*& msg, m_host_t& from, bool wait)
75 if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
76 msg = (message* )MSG_task_get_data(ctrl_task);
77 from = MSG_task_get_source(ctrl_task);
78 MSG_task_destroy(ctrl_task);
81 (!ctrl_close_is_last || msg->get_type() != message::CTRL_CLOSE)
82 ? MSG_task_irecv(&ctrl_task, get_ctrl_mbox())
85 } else if (data_comm && comm_test_n_destroy(data_comm)) {
86 msg = (message* )MSG_task_get_data(data_task);
87 from = MSG_task_get_source(data_task);
88 MSG_task_destroy(data_task);
91 (!data_close_is_last || msg->get_type() != message::DATA_CLOSE)
92 ? MSG_task_irecv(&data_task, get_data_mbox())
96 restart = wait && !msg && (ctrl_comm || data_comm);
98 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
100 xbt_dynar_push(comms, &ctrl_comm);
102 xbt_dynar_push(comms, &data_comm);
103 MSG_comm_waitany(comms);
104 xbt_dynar_free(&comms);
111 void communicator::flush(bool wait)
113 using namespace std::tr1;
114 using namespace std::tr1::placeholders;
116 sent_comm.remove_if(comm_test_n_destroy);
117 if (wait && !sent_comm.empty()) {
118 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
119 while (!sent_comm.empty()) {
120 std::for_each(sent_comm.begin(), sent_comm.end(),
122 comms, bind(misc::address<msg_comm_t>(), _1)));
123 MSG_comm_waitany(comms);
124 xbt_dynar_reset(comms);
125 sent_comm.remove_if(comm_test_n_destroy);
127 xbt_dynar_free(&comms);
131 void communicator::next_close_on_ctrl_is_last()
133 ctrl_close_is_last = true;
136 void communicator::next_close_on_data_is_last()
138 data_close_is_last = true;
141 bool communicator::comm_test_n_destroy(msg_comm_t comm)
143 if (MSG_comm_test(comm)) {
144 MSG_comm_destroy(comm);