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

Private GIT Repository
926bf1f9d064e343cd87273bdbdec41f41879cf9
[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     // Returns the sum of "to_send" for all neighbors.
114     double get_sum_of_to_send() const;
115
116     // Send procedures
117     void ctrl_send(neighbor& nb);
118     void data_send(neighbor& nb);
119     void ctrl_close(neighbor& nb);
120     void data_close(neighbor& nb);
121
122     // Receive procedure
123     // Parameter "timeout" may be 0 for non-blocking operation, -1 for
124     // infinite waiting, or any positive timeout.
125     void ctrl_receive(double timeout);
126     void data_receive(double timeout);
127     void handle_message(message* msg, m_host_t from);
128 };
129
130 inline
131 double process::get_load() const
132 {
133     if (opt::bookkeeping)
134         return expected_load;
135     else
136         return real_load;
137 }
138
139 inline
140 void process::set_load(double load)
141 {
142     if (opt::bookkeeping)
143         expected_load = load;
144     else
145         real_load = load;
146 }
147
148 template <typename Compare>
149 void process::pneigh_sort_by_load(const Compare& comp)
150 {
151     using std::tr1::bind;
152     using std::tr1::placeholders::_1;
153     using std::tr1::placeholders::_2;
154     std::sort(pneigh.begin(), pneigh.end(),
155               bind(comp,
156                    bind(&neighbor::get_load, _1),
157                    bind(&neighbor::get_load, _2)));
158 }
159
160 #endif // !PROCESS_H
161
162 // Local variables:
163 // mode: c++
164 // End: