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

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