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 { return expected_load; }
51 // The load balancing algorithm comes here...
52 virtual void load_balance();
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); }
58 // Sort pneigh by applying comp to their loads
59 template <typename Compare>
60 void pneigh_sort_by_load(const Compare& comp);
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;
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;
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
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
82 communicator comm; // communicator for this process
83 int ctrl_close_pending; // number of "close" messages to wait
85 int data_close_pending; // number of "close" messages to wait
87 bool close_received; // true if we received a "close" message
88 bool finalizing; // true when finalize() is running
90 unsigned lb_iter; // counter of load-balancing iterations
91 unsigned comp_iter; // counter of computation iterations
93 double comp; // total computing done so far (flops)
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
100 mutex_t mutex; // synchronization between threads
103 // Load-balancing loop
104 msg_thread* lb_thread;
105 void load_balance_loop();
107 // Simulate computation loop
110 // Check if we need to stop
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);
130 template <typename Compare>
131 void process::pneigh_sort_by_load(const Compare& comp)
133 using std::tr1::bind;
134 using std::tr1::placeholders::_1;
135 using std::tr1::placeholders::_2;
136 std::sort(pneigh.begin(), pneigh.end(),
138 bind(&neighbor::get_load, _1),
139 bind(&neighbor::get_load, _2)));