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"
26 static double get_total_load_init() { return total_load_init; }
27 static double get_total_load_running() { return total_load_running; }
28 static double get_total_load_exit() { return total_load_exit; }
30 process(int argc, char* argv[]);
33 double get_comp() const { return comp; }
34 double get_real_load() const { return real_load; }
39 typedef std::vector<neighbor> neigh_type;
40 typedef std::vector<neighbor*> pneigh_type;
42 pneigh_type pneigh; // list of pointers to neighbors that
43 // we are free to reorder
45 template <typename Compare>
46 void pneigh_sort_by_load(const Compare& comp);
48 // Get and set current load, which may be real load, or expected
49 // load if opt::bookkeeping is true.
50 double get_load() const;
51 void set_load(double load);
53 // Register some amount of load to send to given neighbor.
54 void send(neighbor& nb, double amount);
55 void send(neighbor* nb, double amount) { send(*nb, amount); }
57 // Calls neighbor::print(verbose, logp, cat) for each member of neigh.
58 void print_loads(bool verbose = false,
59 e_xbt_log_priority_t logp = xbt_log_priority_info,
60 xbt_log_category_t cat = _XBT_LOGV(default)) const;
62 // Calls neighbor::print(verbose, logp, cat) for each member of pneigh.
63 void print_loads_p(bool verbose = false,
64 e_xbt_log_priority_t logp = xbt_log_priority_info,
65 xbt_log_category_t cat = _XBT_LOGV(default)) const;
68 static double total_load_init; // sum of process loads at init
69 static double total_load_running; // sum of loads while running
70 static double total_load_exit; // sum of process loads at exit
72 typedef MAP_TEMPLATE<m_host_t, neighbor*> rev_neigh_type;
73 neigh_type neigh; // list of neighbors (do not alter
74 // after construction!)
75 rev_neigh_type rev_neigh; // map m_host_t -> neighbor
77 communicator comm; // communicator for this process
78 int ctrl_close_pending; // number of "close" messages to wait
80 int data_close_pending; // number of "close" messages to wait
82 bool close_received; // true if we received a "close" message
83 bool finalizing; // true when finalize() is running
85 unsigned lb_iter; // counter of load-balancing iterations
86 unsigned comp_iter; // counter of computation iterations
88 double comp; // total computing done so far (flops)
90 double prev_load_broadcast; // used to ensure that we do not send
91 // a same information messages
92 double real_load; // current load
93 double expected_load; // expected load in bookkeeping mode
95 // The load balancing algorithm comes here...
96 virtual void load_balance();
98 // Virtually do some computation
101 // Send procedures, with helpers for bookkeeping mode or not
102 void send1_no_bookkeeping(neighbor& nb);
103 void send1_bookkeeping(neighbor& nb);
106 // Returns true if there remains neighbors to listen for
107 bool may_receive() const {
108 return ctrl_close_pending || data_close_pending;
112 // Parameter "timeout" may be 0 for non-blocking operation, -1 for
113 // infinite waiting, or any positive timeout.
114 void receive(double timeout);
116 // Finalize sends a "close" message to each neighbor and wait for
117 // all of them to answer.
118 void finalize1(neighbor& nb);
123 double process::get_load() const
125 if (opt::bookkeeping)
126 return expected_load;
132 void process::set_load(double load)
134 if (opt::bookkeeping)
135 expected_load = load;
140 template <typename Compare>
141 void process::pneigh_sort_by_load(const Compare& comp)
143 using std::tr1::bind;
144 using std::tr1::placeholders::_1;
145 using std::tr1::placeholders::_2;
146 std::sort(pneigh.begin(), pneigh.end(),
148 bind(&neighbor::get_load, _1),
149 bind(&neighbor::get_load, _2)));