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

Private GIT Repository
ff8b7517d5b3d062881388859ec53d4fc4f60882
[loba.git] / process.h
1 #ifndef PROCESS_H
2 #define PROCESS_H
3
4 #define USE_UNORDERED_MAP 1
5 //#undef USE_UNORDERED_MAP
6
7 #include <algorithm>
8 #include <functional>
9 #ifdef USE_UNORDERED_MAP
10 #  include <unordered_map>
11 #  define MAP_TEMPLATE std::unordered_map
12 #else
13 #  include <map>
14 #  define MAP_TEMPLATE std::map
15 #endif
16 #include <vector>
17 #include <msg/msg.h>
18 #include <xbt/log.h>
19 #include "communicator.h"
20 #include "misc.h"
21 #include "msg_thread.h"
22 #include "neighbor.h"
23 #include "options.h"
24 #include "synchro.h"
25
26 class process {
27 public:
28     static double get_total_load_init()    { return total_load_init;    }
29     static double get_total_load_running() { return total_load_running; }
30     static double get_total_load_exit()    { return total_load_exit;    }
31
32     process(int argc, char* argv[]);
33     virtual ~process();
34
35     double get_real_load() const           { return real_load;            }
36     double get_comp_amount() const         { return acc.comp_amount;      }
37     unsigned get_comp_iter() const         { return comp_iter;            }
38     unsigned get_all_comp_iter() const     { return all_comp_iter;        }
39     double get_data_send_amount() const    { return acc.data_send.amount; }
40     double get_data_recv_amount() const    { return acc.data_recv.amount; }
41     unsigned get_data_send_count() const   { return acc.data_send.count;  }
42     unsigned get_data_recv_count() const   { return acc.data_recv.count;  }
43     double get_ctrl_send_amount() const    { return acc.ctrl_send.amount; }
44     double get_ctrl_recv_amount() const    { return acc.ctrl_recv.amount; }
45     unsigned get_ctrl_send_count() const   { return acc.ctrl_send.count;  }
46     unsigned get_ctrl_recv_count() const   { return acc.ctrl_recv.count;  }
47     double get_convergence() const         { return convergence;          }
48
49     int run();
50
51 protected:
52     typedef std::vector<neighbor> neigh_type;
53     typedef std::vector<neighbor*> pneigh_type;
54
55     pneigh_type pneigh;         // list of pointers to neighbors that
56                                 // we are free to reorder
57
58     // Get and set current load, which may be real load, or expected
59     // load if opt::bookkeeping is true.
60     double get_load() const                { return expected_load; }
61
62     // The load balancing algorithm comes here...
63     virtual void load_balance();
64
65     // Register some amount of load to send to given neighbor.
66     void send(neighbor& nb, double amount);
67     void send(neighbor* nb, double amount) { send(*nb, amount); }
68
69     // Sort pneigh by applying comp to their loads
70     template <typename Compare>
71     void pneigh_sort_by_load(const Compare& comp);
72
73     // Calls neighbor::print(verbose, logp, cat) for each member of neigh.
74     void print_loads(bool verbose = false,
75                      e_xbt_log_priority_t logp = xbt_log_priority_info,
76                      xbt_log_category_t cat = _XBT_LOGV(default)) const;
77
78     // Calls neighbor::print(verbose, logp, cat) for each member of pneigh.
79     void print_loads_p(bool verbose = false,
80                        e_xbt_log_priority_t logp = xbt_log_priority_info,
81                        xbt_log_category_t cat = _XBT_LOGV(default)) const;
82
83 private:
84     static double total_load_init; // sum of process loads at init
85     static double total_load_running; // sum of loads while running
86     static double total_load_exit; // sum of process loads at exit
87
88     static int process_counter;
89     static double total_load_average;
90
91     typedef MAP_TEMPLATE<m_host_t, neighbor*> rev_neigh_type;
92     neigh_type neigh;           // list of neighbors (do not alter
93                                 // after construction!)
94     rev_neigh_type rev_neigh;   // map m_host_t -> neighbor
95
96     communicator comm;          // communicator for this process
97     int ctrl_close_pending;     // number of "close" messages to wait
98                                 // on ctrl channel
99     int data_close_pending;     // number of "close" messages to wait
100                                 // on data channel
101     bool close_received;        // true if we received a "close" message
102     bool finalizing;            // true when finalize() is running
103
104     unsigned lb_iter;           // counter of load-balancing iterations
105     unsigned comp_iter;         // counter of computation iterations
106     unsigned all_comp_iter;     // counter of computation iterations
107                                 // (counting empty iterations too)
108
109     double prev_load_broadcast; // used to ensure that we do not send
110                                 // a same information messages
111     double real_load;           // current load
112     double expected_load;       // expected load in bookkeeping mode
113     double received_load;       // load received from neighbors
114
115     double convergence;         // date when convergence was achieved, or -1.0
116
117     mutex_t mutex;              // synchronization between threads
118     condition_t cond;
119
120     struct mesg_accounting {
121         double amount;          // sum of message size
122         unsigned count;         // number of messages
123         mesg_accounting(): amount(0.0), count(0) { }
124     };
125     struct accounting {
126         double comp_amount;        // total computing done so far (flops)
127         mesg_accounting data_send; // data messages sent
128         mesg_accounting data_recv; // data messages received
129         mesg_accounting ctrl_send; // ctrl message sent
130         mesg_accounting ctrl_recv; // ctrl message received
131         accounting(): comp_amount(0.0) { }
132     };
133     accounting acc;             // use a structure so that it is
134                                 // automatically initialized a
135                                 // construction
136
137     void add_comp_amount(double amount) { acc.comp_amount += amount; }
138     void add_data_send_mesg(double amount) {
139         ++acc.data_send.count;
140         acc.data_send.amount += amount;
141     }
142     void add_data_recv_mesg(double amount) {
143         ++acc.data_recv.count;
144         acc.data_recv.amount += amount;
145     }
146     void add_ctrl_send_mesg(double amount) {
147         ++acc.ctrl_send.count;
148         acc.ctrl_send.amount += amount;
149     }
150     void add_ctrl_recv_mesg(double amount) {
151         ++acc.ctrl_recv.count;
152         acc.ctrl_recv.amount += amount;
153     }
154
155     // Load-balancing loop
156     msg_thread* lb_thread;
157     void load_balance_loop();
158
159     // Simulate computation loop
160     void compute_loop();
161
162     // Check if we need to stop
163     bool still_running();
164
165     // Returns the sum of "to_send" for all neighbors.
166     double get_sum_of_to_send() const;
167
168     // Compute load_to_send (for data_send), subject to the execution parameters
169     static double compute_load_to_send(double desired);
170
171     // Send procedures
172     void ctrl_send(neighbor& nb);
173     void data_send(neighbor& nb);
174     void ctrl_close(neighbor& nb);
175     void data_close(neighbor& nb);
176
177     // Receive procedure
178     // Parameter "timeout" may be 0 for non-blocking operation, -1 for
179     // infinite waiting, or any positive timeout.
180     void ctrl_receive(double timeout);
181     void data_receive(double timeout);
182     void handle_message(message* msg, m_host_t from);
183 };
184
185 template <typename Compare>
186 void process::pneigh_sort_by_load(const Compare& comp)
187 {
188     using std::placeholders::_1;
189     using std::placeholders::_2;
190     std::sort(pneigh.begin(), pneigh.end(),
191               std::bind(comp,
192                         std::bind(&neighbor::get_load, _1),
193                         std::bind(&neighbor::get_load, _2)));
194 }
195
196 #endif // !PROCESS_H
197
198 // Local variables:
199 // mode: c++
200 // End: