4 #define USE_UNORDERED_MAP 1
5 //#undef USE_UNORDERED_MAP
9 #ifdef USE_UNORDERED_MAP
10 # include <unordered_map>
11 # define MAP_TEMPLATE std::unordered_map
14 # define MAP_TEMPLATE std::map
19 #include "communicator.h"
21 #include "msg_thread.h"
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; }
32 process(int argc, char* argv[]);
35 double get_real_load() const { return real_load; }
36 double get_comp_amount() const { return acc.comp_amount; }
37 unsigned get_comp_iter() const { return comp_iter; }
38 unsigned get_all_comp_iter() const { return all_comp_iter; }
39 double get_data_send_amount() const { return acc.data_send.amount; }
40 double get_data_recv_amount() const { return acc.data_recv.amount; }
41 unsigned get_data_send_count() const { return acc.data_send.count; }
42 unsigned get_data_recv_count() const { return acc.data_recv.count; }
43 double get_ctrl_send_amount() const { return acc.ctrl_send.amount; }
44 double get_ctrl_recv_amount() const { return acc.ctrl_recv.amount; }
45 unsigned get_ctrl_send_count() const { return acc.ctrl_send.count; }
46 unsigned get_ctrl_recv_count() const { return acc.ctrl_recv.count; }
47 double get_idle_duration() const { return idle_duration; }
48 double get_convergence() const { return convergence; }
53 typedef std::vector<neighbor> neigh_type;
54 typedef std::vector<neighbor*> pneigh_type;
56 pneigh_type pneigh; // list of pointers to neighbors that
57 // we are free to reorder
59 // Get and set current load, which may be real load, or expected
60 // load if opt::bookkeeping is true.
61 double get_load() const { return expected_load; }
63 // The load balancing algorithm comes here...
64 virtual void load_balance();
66 // Register some amount of load to send to given neighbor.
67 void send(neighbor& nb, double amount);
68 void send(neighbor* nb, double amount) { send(*nb, amount); }
70 // Sort pneigh by applying comp to their loads
71 template <typename Compare>
72 void pneigh_sort_by_load(const Compare& comp);
74 // Calls neighbor::print(verbose, logp, cat) for each member of neigh.
75 void print_loads(bool verbose = false,
76 e_xbt_log_priority_t logp = xbt_log_priority_info,
77 xbt_log_category_t cat = _XBT_LOGV(default)) const;
79 // Calls neighbor::print(verbose, logp, cat) for each member of pneigh.
80 void print_loads_p(bool verbose = false,
81 e_xbt_log_priority_t logp = xbt_log_priority_info,
82 xbt_log_category_t cat = _XBT_LOGV(default)) const;
85 static double total_load_init; // sum of process loads at init
86 static double total_load_running; // sum of loads while running
87 static double total_load_exit; // sum of process loads at exit
89 static int process_counter;
90 static double total_load_average;
91 static double load_diff_threshold;
93 typedef MAP_TEMPLATE<m_host_t, neighbor*> rev_neigh_type;
94 neigh_type neigh; // list of neighbors (do not alter
95 // after construction!)
96 rev_neigh_type rev_neigh; // map m_host_t -> neighbor
98 communicator comm; // communicator for this process
99 int ctrl_close_pending; // number of "close" messages to wait
101 int data_close_pending; // number of "close" messages to wait
103 bool close_received; // true if we received a "close" message
104 bool finalizing; // true when finalize() is running
106 unsigned lb_iter; // counter of load-balancing iterations
107 unsigned comp_iter; // counter of computation iterations
108 unsigned all_comp_iter; // counter of computation iterations
109 // (counting empty iterations too)
111 double prev_load_broadcast; // used to ensure that we do not send
112 // a same information messages
113 double real_load; // current load
114 double expected_load; // expected load in bookkeeping mode
115 double received_load; // load received from neighbors
117 double idle_duration; // how long we had nothing to compute
118 double convergence; // date when convergence was achieved, or -1.0
120 mutex_t mutex; // synchronization between threads
123 struct mesg_accounting {
124 double amount; // sum of message size
125 unsigned count; // number of messages
126 mesg_accounting(): amount(0.0), count(0) { }
129 double comp_amount; // total computing done so far (flops)
130 mesg_accounting data_send; // data messages sent
131 mesg_accounting data_recv; // data messages received
132 mesg_accounting ctrl_send; // ctrl message sent
133 mesg_accounting ctrl_recv; // ctrl message received
134 accounting(): comp_amount(0.0) { }
136 accounting acc; // use a structure so that it is
137 // automatically initialized a
140 void add_comp_amount(double amount) { acc.comp_amount += amount; }
141 void add_data_send_mesg(double amount) {
142 ++acc.data_send.count;
143 acc.data_send.amount += amount;
145 void add_data_recv_mesg(double amount) {
146 ++acc.data_recv.count;
147 acc.data_recv.amount += amount;
149 void add_ctrl_send_mesg(double amount) {
150 ++acc.ctrl_send.count;
151 acc.ctrl_send.amount += amount;
153 void add_ctrl_recv_mesg(double amount) {
154 ++acc.ctrl_recv.count;
155 acc.ctrl_recv.amount += amount;
158 // Load-balancing loop
159 msg_thread* lb_thread;
160 void load_balance_loop();
162 // Simulate computation loop
165 // Check for convergence
166 void convergence_check();
168 // Check if we need to stop
169 bool still_running();
171 // Returns the sum of "to_send" for all neighbors.
172 double get_sum_of_to_send() const;
174 // Compute load_to_send (for data_send), subject to the execution parameters
175 static double compute_load_to_send(double desired);
178 void ctrl_send(neighbor& nb);
179 void data_send(neighbor& nb);
180 void ctrl_close(neighbor& nb);
181 void data_close(neighbor& nb);
184 // Parameter "timeout" may be 0 for non-blocking operation, -1 for
185 // infinite waiting, or any positive timeout.
186 void ctrl_receive(double timeout);
187 void data_receive(double timeout);
188 void handle_message(message* msg, m_host_t from);
191 template <typename Compare>
192 void process::pneigh_sort_by_load(const Compare& comp)
194 using std::placeholders::_1;
195 using std::placeholders::_2;
196 std::sort(pneigh.begin(), pneigh.end(),
198 std::bind(&neighbor::get_load, _1),
199 std::bind(&neighbor::get_load, _2)));