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

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