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

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