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

Private GIT Repository
Use tr1/functional for sorting the hosts.
[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 <vector>
8 #ifdef USE_UNORDERED_MAP
9 #  include <tr1/unordered_map>
10 #  define MAP_TEMPLATE std::tr1::unordered_map
11 #else
12 #  include <map>
13 #  define MAP_TEMPLATE std::map
14 #endif
15 #include <msg/msg.h>
16 #include <xbt/log.h>
17 #include "communicator.h"
18 #include "misc.h"
19 #include "neighbor.h"
20 #include "options.h"
21
22 class process {
23 public:
24     static double get_total_load_init()    { return total_load_init;    }
25     static double get_total_load_running() { return total_load_running; }
26     static double get_total_load_exit()    { return total_load_exit;    }
27
28     process(int argc, char* argv[]);
29     virtual ~process();
30
31     double get_comp() const                { return comp; }
32     double get_real_load() const           { return real_load; }
33
34     int run();
35
36 protected:
37     typedef std::vector<neighbor> neigh_type;
38     typedef std::vector<neighbor*> pneigh_type;
39
40     pneigh_type pneigh;         // list of pointers to neighbors that
41                                 // we are free to reorder
42
43     // Get and set current load, which may be real load, or expected
44     // load if opt::bookkeeping is true.
45     double get_load() const;
46     void set_load(double load);
47
48     // Register some amount of load to send to given neighbor.
49     void send(neighbor& nb, double amount);
50     void send(neighbor* nb, double amount) { send(*nb, amount); }
51
52     // Calls neighbor::print(verbose, logp, cat) for each member of neigh.
53     void print_loads(bool verbose = false,
54                      e_xbt_log_priority_t logp = xbt_log_priority_info,
55                      xbt_log_category_t cat = _XBT_LOGV(default)) const;
56
57     // Calls neighbor::print(verbose, logp, cat) for each member of pneigh.
58     void print_loads_p(bool verbose = false,
59                        e_xbt_log_priority_t logp = xbt_log_priority_info,
60                        xbt_log_category_t cat = _XBT_LOGV(default)) const;
61
62 private:
63     static double total_load_init; // sum of process loads at init
64     static double total_load_running; // sum of loads while running
65     static double total_load_exit; // sum of process loads at exit
66
67     typedef MAP_TEMPLATE<m_host_t, neighbor*> rev_neigh_type;
68     neigh_type neigh;           // list of neighbors (do not alter
69                                 // after construction!)
70     rev_neigh_type rev_neigh;   // map m_host_t -> neighbor
71
72     communicator comm;          // communicator for this process
73     int ctrl_close_pending;     // number of "close" messages to wait
74                                 // on ctrl channel
75     int data_close_pending;     // number of "close" messages to wait
76                                 // on data channel
77     bool close_received;        // true if we received a "close" message
78     bool finalizing;            // true when finalize() is running
79
80     unsigned lb_iter;           // counter of load-balancing iterations
81     unsigned comp_iter;         // counter of computation iterations
82
83     double comp;                // total computing done so far (flops)
84
85     double prev_load_broadcast; // used to ensure that we do not send
86                                 // a same information messages
87     double real_load;           // current load
88     double expected_load;       // expected load in bookkeeping mode
89
90     // The load balancing algorithm comes here...
91     virtual void load_balance();
92
93     // Virtually do some computation
94     void compute();
95
96     // Send procedures, with helpers for bookkeeping mode or not
97     void send1_no_bookkeeping(neighbor& nb);
98     void send1_bookkeeping(neighbor& nb);
99     void send_all();
100
101     // Returns true if there remains neighbors to listen for
102     bool may_receive() const {
103         return ctrl_close_pending || data_close_pending;
104     }
105
106     // Receive procedure
107     // Parameter "timeout" may be 0 for non-blocking operation, -1 for
108     // infinite waiting, or any positive timeout.
109     void receive(double timeout);
110
111     // Finalize sends a "close" message to each neighbor and wait for
112     // all of them to answer.
113     void finalize1(neighbor& nb);
114     void finalize();
115 };
116
117 inline
118 double process::get_load() const
119 {
120     if (opt::bookkeeping)
121         return expected_load;
122     else
123         return real_load;
124 }
125
126 inline
127 void process::set_load(double load)
128 {
129     if (opt::bookkeeping)
130         expected_load = load;
131     else
132         real_load = load;
133 }
134
135 struct pneigh_load_less {
136     bool operator()(const neighbor* a, const neighbor* b)
137     {
138         return a->get_load() < b->get_load();
139     }
140 };
141
142 struct pneigh_load_greater {
143     bool operator()(const neighbor* a, const neighbor* b)
144     {
145         return a->get_load() > b->get_load();
146     }
147 };
148
149 #endif // !PROCESS_H
150
151 // Local variables:
152 // mode: c++
153 // End: