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

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