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

Private GIT Repository
ajout de 3 fichiers pour lancer des expés
[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     double get_data_send_amount() const    { return acc.data_send.amount; }
38     double get_data_recv_amount() const    { return acc.data_recv.amount; }
39     unsigned get_data_send_count() const   { return acc.data_send.count;  }
40     unsigned get_data_recv_count() const   { return acc.data_recv.count;  }
41     double get_ctrl_send_amount() const    { return acc.ctrl_send.amount; }
42     double get_ctrl_recv_amount() const    { return acc.ctrl_recv.amount; }
43     unsigned get_ctrl_send_count() const   { return acc.ctrl_send.count;  }
44     unsigned get_ctrl_recv_count() const   { return acc.ctrl_recv.count;  }
45
46     int run();
47
48 protected:
49     typedef std::vector<neighbor> neigh_type;
50     typedef std::vector<neighbor*> pneigh_type;
51
52     pneigh_type pneigh;         // list of pointers to neighbors that
53                                 // we are free to reorder
54
55     // Get and set current load, which may be real load, or expected
56     // load if opt::bookkeeping is true.
57     double get_load() const                { return expected_load; }
58
59     // The load balancing algorithm comes here...
60     virtual void load_balance();
61
62     // Register some amount of load to send to given neighbor.
63     void send(neighbor& nb, double amount);
64     void send(neighbor* nb, double amount) { send(*nb, amount); }
65
66     // Sort pneigh by applying comp to their loads
67     template <typename Compare>
68     void pneigh_sort_by_load(const Compare& comp);
69
70     // Calls neighbor::print(verbose, logp, cat) for each member of neigh.
71     void print_loads(bool verbose = false,
72                      e_xbt_log_priority_t logp = xbt_log_priority_info,
73                      xbt_log_category_t cat = _XBT_LOGV(default)) const;
74
75     // Calls neighbor::print(verbose, logp, cat) for each member of pneigh.
76     void print_loads_p(bool verbose = false,
77                        e_xbt_log_priority_t logp = xbt_log_priority_info,
78                        xbt_log_category_t cat = _XBT_LOGV(default)) const;
79
80 private:
81     static double total_load_init; // sum of process loads at init
82     static double total_load_running; // sum of loads while running
83     static double total_load_exit; // sum of process loads at exit
84
85     typedef MAP_TEMPLATE<m_host_t, neighbor*> rev_neigh_type;
86     neigh_type neigh;           // list of neighbors (do not alter
87                                 // after construction!)
88     rev_neigh_type rev_neigh;   // map m_host_t -> neighbor
89
90     communicator comm;          // communicator for this process
91     int ctrl_close_pending;     // number of "close" messages to wait
92                                 // on ctrl channel
93     int data_close_pending;     // number of "close" messages to wait
94                                 // on data channel
95     bool close_received;        // true if we received a "close" message
96     bool finalizing;            // true when finalize() is running
97
98     unsigned lb_iter;           // counter of load-balancing iterations
99     unsigned comp_iter;         // counter of computation iterations
100
101     double prev_load_broadcast; // used to ensure that we do not send
102                                 // a same information messages
103     double real_load;           // current load
104     double expected_load;       // expected load in bookkeeping mode
105     double received_load;       // load received from neighbors
106
107     mutex_t mutex;              // synchronization between threads
108     condition_t cond;
109
110     struct mesg_accounting {
111         double amount;          // sum of message size
112         unsigned count;         // number of messages
113         mesg_accounting(): amount(0.0), count(0) { }
114     };
115     struct accounting {
116         double comp_amount;        // total computing done so far (flops)
117         mesg_accounting data_send; // data messages sent
118         mesg_accounting data_recv; // data messages received
119         mesg_accounting ctrl_send; // ctrl message sent
120         mesg_accounting ctrl_recv; // ctrl message received
121         accounting(): comp_amount(0.0) { }
122     };
123     accounting acc;
124
125     void add_comp_amount(double amount) { acc.comp_amount += amount; }
126     void add_data_send_mesg(double amount) {
127         ++acc.data_send.count;
128         acc.data_send.amount += amount;
129     }
130     void add_data_recv_mesg(double amount) {
131         ++acc.data_recv.count;
132         acc.data_recv.amount += amount;
133     }
134     void add_ctrl_send_mesg(double amount) {
135         ++acc.ctrl_send.count;
136         acc.ctrl_send.amount += amount;
137     }
138     void add_ctrl_recv_mesg(double amount) {
139         ++acc.ctrl_recv.count;
140         acc.ctrl_recv.amount += amount;
141     }
142
143     // Load-balancing loop
144     msg_thread* lb_thread;
145     void load_balance_loop();
146
147     // Simulate computation loop
148     void compute_loop();
149
150     // Check if we need to stop
151     bool still_running();
152
153     // Returns the sum of "to_send" for all neighbors.
154     double get_sum_of_to_send() const;
155
156     // Compute load_to_send (for data_send), subject to the execution parameters
157     static double compute_load_to_send(double desired);
158
159     // Send procedures
160     void ctrl_send(neighbor& nb);
161     void data_send(neighbor& nb);
162     void ctrl_close(neighbor& nb);
163     void data_close(neighbor& nb);
164
165     // Receive procedure
166     // Parameter "timeout" may be 0 for non-blocking operation, -1 for
167     // infinite waiting, or any positive timeout.
168     void ctrl_receive(double timeout);
169     void data_receive(double timeout);
170     void handle_message(message* msg, m_host_t from);
171 };
172
173 template <typename Compare>
174 void process::pneigh_sort_by_load(const Compare& comp)
175 {
176     using std::placeholders::_1;
177     using std::placeholders::_2;
178     std::sort(pneigh.begin(), pneigh.end(),
179               std::bind(comp,
180                         std::bind(&neighbor::get_load, _1),
181                         std::bind(&neighbor::get_load, _2)));
182 }
183
184 #endif // !PROCESS_H
185
186 // Local variables:
187 // mode: c++
188 // End: