4 #define USE_UNORDERED_MAP 1
5 //#undef USE_UNORDERED_MAP
8 #include <tr1/functional>
9 #ifdef USE_UNORDERED_MAP
10 # include <tr1/unordered_map>
11 # define MAP_TEMPLATE std::tr1::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_comp() const { return comp; }
36 double get_real_load() const { return real_load; }
41 typedef std::vector<neighbor> neigh_type;
42 typedef std::vector<neighbor*> pneigh_type;
44 pneigh_type pneigh; // list of pointers to neighbors that
45 // we are free to reorder
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);
52 // The load balancing algorithm comes here...
53 virtual void load_balance();
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); }
59 // Sort pneigh by applying comp to their loads
60 template <typename Compare>
61 void pneigh_sort_by_load(const Compare& comp);
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;
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;
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
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
83 communicator comm; // communicator for this process
84 int ctrl_close_pending; // number of "close" messages to wait
86 int data_close_pending; // number of "close" messages to wait
88 bool close_received; // true if we received a "close" message
89 bool finalizing; // true when finalize() is running
91 unsigned lb_iter; // counter of load-balancing iterations
92 unsigned comp_iter; // counter of computation iterations
94 double comp; // total computing done so far (flops)
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
101 mutex_t mutex; // synchronization between threads
104 // Load-balancing loop
105 msg_thread* lb_thread;
106 void load_balance_loop();
108 // Simulate computation loop
111 bool still_running();
113 // Returns the sum of "to_send" for all neighbors.
114 double get_sum_of_to_send() const;
117 void ctrl_send(neighbor& nb);
118 void data_send(neighbor& nb);
119 void ctrl_close(neighbor& nb);
120 void data_close(neighbor& nb);
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);
131 double process::get_load() const
133 if (opt::bookkeeping)
134 return expected_load;
140 void process::set_load(double load)
142 if (opt::bookkeeping)
143 expected_load = load;
148 template <typename Compare>
149 void process::pneigh_sort_by_load(const Compare& comp)
151 using std::tr1::bind;
152 using std::tr1::placeholders::_1;
153 using std::tr1::placeholders::_2;
154 std::sort(pneigh.begin(), pneigh.end(),
156 bind(&neighbor::get_load, _1),
157 bind(&neighbor::get_load, _2)));