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

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