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

Private GIT Repository
Change format for log messages.
[loba.git] / communicator.cpp
1 #include <algorithm>
2 #include <tr1/functional>
3 #include <msg/msg.h>
4 #include <xbt/log.h>
5
6 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm);
7
8 #include "misc.h"
9 #include "options.h"
10 #include "simgrid_features.h"
11 #include "tracing.h"
12
13 #include "communicator.h"
14
15 namespace {
16
17     void check_for_lost_messages(size_t size, const char* descr)
18     {
19         if (size)
20             XBT_WARN("lost %zu %s message%s!", size, descr, ESSE(size));
21     }
22
23 }
24
25 communicator::communicator()
26     : host(static_cast<hostdata*>(MSG_host_get_data(MSG_host_self())))
27 {
28     using std::tr1::bind;
29     receiver_thread = new_msg_thread("receiver",
30                                      bind(&communicator::receiver, this));
31     receiver_thread->start();
32 }
33
34 communicator::~communicator()
35 {
36     m_task_t task;
37
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());
41
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());
45
46     receiver_thread->wait();
47     delete receiver_thread;
48
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");
53 }
54
55 msg_comm_t communicator::real_send(const char* dest, message* msg)
56 {
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);
66 }
67
68 void communicator::real_flush(sent_comm_type& sent_comm, bool wait)
69 {
70     sent_comm_type::iterator bound;
71     bound = std::remove_if(sent_comm.begin(), sent_comm.end(),
72                            comm_test_n_destroy);
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);
78         sent_comm.clear();
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);
82         delete[] comms;
83     }
84 }
85
86 void communicator::receiver()
87 {
88     xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
89     struct channel {
90         msg_comm_t comm;
91         m_task_t task;
92         const char* mbox;
93         message_queue& received;
94     };
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]);
98
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);
102     }
103
104     while (!xbt_dynar_is_empty(comms)) {
105
106         int index = MSG_comm_waitany(comms);
107         msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
108         channel* ch;
109
110         for (ch = chan ; ch->comm != finished_comm ; ++ch)
111             /* nop */;
112
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);
117             ch->task = NULL;
118             ch->comm = MSG_task_irecv(&ch->task, ch->mbox);
119             xbt_dynar_set_as(comms, index, msg_comm_t, ch->comm);
120         } else {
121             XBT_DEBUG("received finalize on %s", ch->mbox);
122             MSG_task_destroy(ch->task);
123             ch->task = NULL;
124             ch->comm = NULL;
125             xbt_dynar_remove_at(comms, index, NULL);
126         }
127
128     }
129     xbt_dynar_free(&comms);
130 }
131
132 void communicator::comm_check_n_destroy(msg_comm_t comm)
133 {
134     xbt_assert(MSG_comm_get_status(comm) == MSG_OK);
135     MSG_comm_destroy(comm);
136 }
137
138 bool communicator::comm_test_n_destroy(msg_comm_t comm)
139 {
140     if (MSG_comm_test(comm)) {
141         comm_check_n_destroy(comm);
142         return true;
143     } else
144         return false;
145 }
146
147 // Local variables:
148 // mode: c++
149 // End: