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

Private GIT Repository
Print my own loads too with process:print_loads.
[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 <tr1/functional>
9 #ifdef USE_UNORDERED_MAP
10 #  include <tr1/unordered_map>
11 #  define MAP_TEMPLATE std::tr1::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_comp() const                { return comp; }
36     double get_real_load() const           { return real_load; }
37
38     int run();
39
40 protected:
41     typedef std::vector<neighbor> neigh_type;
42     typedef std::vector<neighbor*> pneigh_type;
43
44     pneigh_type pneigh;         // list of pointers to neighbors that
45                                 // we are free to reorder
46
47     // Get and set current load, which may be real load, or expected
48     // load if opt::bookkeeping is true.
49     double get_load() const;
50     void set_load(double load);
51
52     // The load balancing algorithm comes here...
53     virtual void load_balance();
54
55     // Register some amount of load to send to given neighbor.
56     void send(neighbor& nb, double amount);
57     void send(neighbor* nb, double amount) { send(*nb, amount); }
58
59     // Sort pneigh by applying comp to their loads
60     template <typename Compare>
61     void pneigh_sort_by_load(const Compare& comp);
62
63     // Calls neighbor::print(verbose, logp, cat) for each member of neigh.
64     void print_loads(bool verbose = false,
65                      e_xbt_log_priority_t logp = xbt_log_priority_info,
66                      xbt_log_category_t cat = _XBT_LOGV(default)) const;
67
68     // Calls neighbor::print(verbose, logp, cat) for each member of pneigh.
69     void print_loads_p(bool verbose = false,
70                        e_xbt_log_priority_t logp = xbt_log_priority_info,
71                        xbt_log_category_t cat = _XBT_LOGV(default)) const;
72
73 private:
74     static double total_load_init; // sum of process loads at init
75     static double total_load_running; // sum of loads while running
76     static double total_load_exit; // sum of process loads at exit
77
78     typedef MAP_TEMPLATE<m_host_t, neighbor*> rev_neigh_type;
79     neigh_type neigh;           // list of neighbors (do not alter
80                                 // after construction!)
81     rev_neigh_type rev_neigh;   // map m_host_t -> neighbor
82
83     communicator comm;          // communicator for this process
84     int ctrl_close_pending;     // number of "close" messages to wait
85                                 // on ctrl channel
86     int data_close_pending;     // number of "close" messages to wait
87                                 // on data channel
88     bool close_received;        // true if we received a "close" message
89     bool finalizing;            // true when finalize() is running
90
91     unsigned lb_iter;           // counter of load-balancing iterations
92     unsigned comp_iter;         // counter of computation iterations
93
94     double comp;                // total computing done so far (flops)
95
96     double prev_load_broadcast; // used to ensure that we do not send
97                                 // a same information messages
98     double real_load;           // current load
99     double expected_load;       // expected load in bookkeeping mode
100
101     mutex_t mutex;              // synchronization between threads
102     condition_t cond;
103
104     // Load-balancing loop
105     msg_thread* lb_thread;
106     void load_balance_loop();
107
108     // Simulate computation loop
109     void compute_loop();
110
111     bool still_running();
112
113     // Send procedures
114     void ctrl_send(neighbor& nb);
115     void data_send(neighbor& nb);
116     void ctrl_close(neighbor& nb);
117     void data_close(neighbor& nb);
118
119     // Receive procedure
120     // Parameter "timeout" may be 0 for non-blocking operation, -1 for
121     // infinite waiting, or any positive timeout.
122     void ctrl_receive(double timeout);
123     void data_receive(double timeout);
124     void handle_message(message* msg, m_host_t from);
125 };
126
127 inline
128 double process::get_load() const
129 {
130     if (opt::bookkeeping)
131         return expected_load;
132     else
133         return real_load;
134 }
135
136 inline
137 void process::set_load(double load)
138 {
139     if (opt::bookkeeping)
140         expected_load = load;
141     else
142         real_load = load;
143 }
144
145 template <typename Compare>
146 void process::pneigh_sort_by_load(const Compare& comp)
147 {
148     using std::tr1::bind;
149     using std::tr1::placeholders::_1;
150     using std::tr1::placeholders::_2;
151     std::sort(pneigh.begin(), pneigh.end(),
152               bind(comp,
153                    bind(&neighbor::get_load, _1),
154                    bind(&neighbor::get_load, _2)));
155 }
156
157 #endif // !PROCESS_H
158
159 // Local variables:
160 // mode: c++
161 // End: