]> AND Private Git Repository - loba.git/blob - communicator.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Remove redundant includes.
[loba.git] / communicator.cpp
1 #include "communicator.h"
2
3 #include <algorithm>
4 #include <tr1/functional>
5 #include <sstream>
6 #include <msg/msg.h>
7 #include <xbt/log.h>
8 #include "misc.h"
9 #include "options.h"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm);
12
13 std::string message::to_string()
14 {
15     static const char* str[] = { "INFO", "CREDIT", "LOAD",
16                                  "CTRL_CLOSE", "DATA_CLOSE" };
17     std::ostringstream oss;
18     oss << str[type] << " (" << amount << ")";
19     return oss.str();
20 }
21
22 const int communicator::send_count_before_flush = 128;
23
24 communicator::communicator()
25     : host((hostdata* )MSG_host_get_data(MSG_host_self()))
26     , send_counter(0)
27     , ctrl_task(NULL)
28     , ctrl_comm(NULL)
29     , ctrl_close_is_last(false)
30     , data_task(NULL)
31     , data_comm(NULL)
32     , data_close_is_last(false)
33 {
34 }
35
36 communicator::~communicator()
37 {
38     if (ctrl_comm)
39         WARN0("ctrl_comm is pending!");
40     if (data_comm)
41         WARN0("data_comm is pending!");
42     if (!sent_comm.empty())
43         WARN2("lost %ld send communication%s!",
44               (long )sent_comm.size(), ESSE(sent_comm.size()));
45 }
46
47 void communicator::listen()
48 {
49     ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
50     data_comm = MSG_task_irecv(&data_task, get_data_mbox());
51 }
52
53 void communicator::send(const char* dest, message* msg)
54 {
55     DEBUG2("send %s to %s", msg->to_string().c_str(), dest);
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);
62
63     if (++send_counter >= send_count_before_flush) {
64         flush(false);
65         send_counter = 0;
66     }
67 }
68
69 bool communicator::recv(message*& msg, m_host_t& from, bool wait)
70 {
71     bool restart;
72     msg = NULL;
73
74     do {
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);
79             ctrl_task = NULL;
80             ctrl_comm =
81                 (!ctrl_close_is_last || msg->get_type() != message::CTRL_CLOSE)
82                 ? MSG_task_irecv(&ctrl_task, get_ctrl_mbox())
83                 : NULL;
84
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);
89             data_task = NULL;
90             data_comm =
91                 (!data_close_is_last || msg->get_type() != message::DATA_CLOSE)
92                 ? MSG_task_irecv(&data_task, get_data_mbox())
93                 : NULL;
94         }
95
96         restart = wait && !msg && (ctrl_comm || data_comm);
97         if (restart) {
98             xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
99             if (ctrl_comm)
100                 xbt_dynar_push(comms, &ctrl_comm);
101             if (data_comm)
102                 xbt_dynar_push(comms, &data_comm);
103             MSG_comm_waitany(comms);
104             xbt_dynar_free(&comms);
105         }
106     } while (restart);
107
108     if (msg)
109         DEBUG2("received %s from %s",
110                msg->to_string().c_str(), MSG_host_get_name(from));
111
112     return msg != NULL;
113 }
114
115 void communicator::flush(bool wait)
116 {
117     using namespace std::tr1;
118     using namespace std::tr1::placeholders;
119
120     sent_comm.remove_if(comm_test_n_destroy);
121     if (wait && !sent_comm.empty()) {
122         xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
123         while (!sent_comm.empty()) {
124             std::for_each(sent_comm.begin(), sent_comm.end(),
125                           bind(xbt_dynar_push,
126                                comms, bind(misc::address<msg_comm_t>(), _1)));
127             MSG_comm_waitany(comms);
128             xbt_dynar_reset(comms);
129             sent_comm.remove_if(comm_test_n_destroy);
130         }
131         xbt_dynar_free(&comms);
132     }
133 }
134
135 void communicator::next_close_on_ctrl_is_last()
136 {
137     ctrl_close_is_last = true;
138 }
139
140 void communicator::next_close_on_data_is_last()
141 {
142     data_close_is_last = true;
143 }
144
145 bool communicator::comm_test_n_destroy(msg_comm_t comm)
146 {
147     if (MSG_comm_test(comm)) {
148         MSG_comm_destroy(comm);
149         return true;
150     } else
151         return false;
152 }
153
154 // Local variables:
155 // mode: c++
156 // End: