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

Private GIT Repository
Move declarations at proper place, and fix indentation.
[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                { return expected_load; }
50
51     // The load balancing algorithm comes here...
52     virtual void load_balance();
53
54     // Register some amount of load to send to given neighbor.
55     void send(neighbor& nb, double amount);
56     void send(neighbor* nb, double amount) { send(*nb, amount); }
57
58     // Sort pneigh by applying comp to their loads
59     template <typename Compare>
60     void pneigh_sort_by_load(const Compare& comp);
61
62     // Calls neighbor::print(verbose, logp, cat) for each member of neigh.
63     void print_loads(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     // Calls neighbor::print(verbose, logp, cat) for each member of pneigh.
68     void print_loads_p(bool verbose = false,
69                        e_xbt_log_priority_t logp = xbt_log_priority_info,
70                        xbt_log_category_t cat = _XBT_LOGV(default)) const;
71
72 private:
73     static double total_load_init; // sum of process loads at init
74     static double total_load_running; // sum of loads while running
75     static double total_load_exit; // sum of process loads at exit
76
77     typedef MAP_TEMPLATE<m_host_t, neighbor*> rev_neigh_type;
78     neigh_type neigh;           // list of neighbors (do not alter
79                                 // after construction!)
80     rev_neigh_type rev_neigh;   // map m_host_t -> neighbor
81
82     communicator comm;          // communicator for this process
83     int ctrl_close_pending;     // number of "close" messages to wait
84                                 // on ctrl channel
85     int data_close_pending;     // number of "close" messages to wait
86                                 // on data channel
87     bool close_received;        // true if we received a "close" message
88     bool finalizing;            // true when finalize() is running
89
90     unsigned lb_iter;           // counter of load-balancing iterations
91     unsigned comp_iter;         // counter of computation iterations
92
93     double comp;                // total computing done so far (flops)
94
95     double prev_load_broadcast; // used to ensure that we do not send
96                                 // a same information messages
97     double real_load;           // current load
98     double expected_load;       // expected load in bookkeeping mode
99
100     mutex_t mutex;              // synchronization between threads
101     condition_t cond;
102
103     // Load-balancing loop
104     msg_thread* lb_thread;
105     void load_balance_loop();
106
107     // Simulate computation loop
108     void compute_loop();
109
110     // Check if we need to stop
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 template <typename Compare>
131 void process::pneigh_sort_by_load(const Compare& comp)
132 {
133     using std::tr1::bind;
134     using std::tr1::placeholders::_1;
135     using std::tr1::placeholders::_2;
136     std::sort(pneigh.begin(), pneigh.end(),
137               bind(comp,
138                    bind(&neighbor::get_load, _1),
139                    bind(&neighbor::get_load, _2)));
140 }
141
142 #endif // !PROCESS_H
143
144 // Local variables:
145 // mode: c++
146 // End: