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

Private GIT Repository
Wip++...
[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 #include "communicator.h"
7 #include "simgrid_features.h"
8 #include "misc.h"
9
10 // XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu);
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(comm, simu,
12                                 "Messages from asynchronous pipes");
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 communicator::communicator()
24 {
25     const char* hostname = MSG_host_get_name(MSG_host_self());
26
27     ctrl_mbox = hostname;
28     ctrl_mbox += "_ctrl";
29     ctrl_task = NULL;
30     ctrl_comm = NULL;
31     ctrl_close_is_last = false;
32
33     data_mbox = hostname;
34     data_mbox += "_data";
35     data_task = NULL;
36     data_comm = NULL;
37     data_close_is_last = false;
38 }
39
40 communicator::~communicator()
41 {
42     if (ctrl_comm)
43         WARN0("ctrl_comm is pending!");
44     if (data_comm)
45         WARN0("data_comm is pending!");
46     if (!sent_comm.empty())
47         WARN2("lost %ld send communication%s!",
48               (long )sent_comm.size(), ESSE(sent_comm.size()));
49 }
50
51 void communicator::listen()
52 {
53     ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
54     data_comm = MSG_task_irecv(&data_task, get_data_mbox());
55 }
56
57 void communicator::send(const char* dest, message* msg)
58 {
59     double msg_size = sizeof *msg;
60     if (msg->get_type() == message::LOAD)
61         msg_size += msg->get_amount();
62     m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);    
63     msg_comm_t comm = MSG_task_isend(task, dest);
64     sent_comm.push_back(comm);
65 }
66
67 bool communicator::recv(message*& msg, m_host_t& from, bool wait)
68 {
69     bool restart;
70     msg = NULL;
71
72     do {
73         if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
74             msg = (message* )MSG_task_get_data(ctrl_task);
75             from = MSG_task_get_source(ctrl_task);
76             MSG_task_destroy(ctrl_task);
77             ctrl_task = NULL;
78             ctrl_comm =
79                 (!ctrl_close_is_last || msg->get_type() != message::CTRL_CLOSE)
80                 ? MSG_task_irecv(&ctrl_task, get_ctrl_mbox())
81                 : NULL;
82
83         } else if (data_comm && comm_test_n_destroy(data_comm)) {
84             msg = (message* )MSG_task_get_data(data_task);
85             from = MSG_task_get_source(data_task);
86             MSG_task_destroy(data_task);
87             data_task = NULL;
88             data_comm =
89                 (!data_close_is_last || msg->get_type() != message::DATA_CLOSE)
90                 ? MSG_task_irecv(&data_task, get_data_mbox())
91                 : NULL;
92         }
93
94         restart = wait && !msg && (ctrl_comm || data_comm);
95         if (restart) {
96             xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
97             if (ctrl_comm)
98                 xbt_dynar_push(comms, &ctrl_comm);
99             if (data_comm)
100                 xbt_dynar_push(comms, &data_comm);
101             MSG_comm_waitany(comms);
102             xbt_dynar_free(&comms);
103         }
104     } while (restart);
105
106     return msg != NULL;
107 }
108
109 void communicator::flush(bool wait)
110 {
111     sent_comm.remove_if(comm_test_n_destroy);
112     if (wait && !sent_comm.empty()) {
113         xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
114         while (!sent_comm.empty()) {
115             std::for_each(sent_comm.begin(), sent_comm.end(),
116                           std::tr1::bind(comm_push_in_dynar,
117                                          comms, std::tr1::placeholders::_1));
118             MSG_comm_waitany(comms);
119             xbt_dynar_reset(comms);
120             sent_comm.remove_if(comm_test_n_destroy);
121         }
122         xbt_dynar_free(&comms);
123     }
124 }
125
126 void communicator::next_close_on_ctrl_is_last()
127 {
128     ctrl_close_is_last = true;
129 }
130
131 void communicator::next_close_on_data_is_last()
132 {
133     data_close_is_last = true;
134 }
135
136 void communicator::comm_push_in_dynar(xbt_dynar_t dynar, msg_comm_t comm)
137 {
138     xbt_dynar_push(dynar, &comm);
139 }
140
141 bool communicator::comm_test_n_destroy(msg_comm_t comm)
142 {
143     if (MSG_comm_test(comm)) {
144         MSG_comm_destroy(comm);
145         return true;
146     } else
147         return false;
148 }
149
150 // Local variables:
151 // mode: c++
152 // End: