12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(depl);
18 #include "deployment.h"
20 void MY_launch_application()
22 deployment_generator* gen;
23 gen = opt::topologies.new_instance(opt::auto_depl::topology);
25 gen->distribute_load();
30 deployment_generator::deployment_generator()
31 : hosts(opt::auto_depl::nhosts)
35 void deployment_generator::set_load(int host, double load)
37 hosts[host].load = load;
40 void deployment_generator::set_neighbor(int host, int neighbor)
42 hosts[host].neighbors.push_back(neighbor);
45 void deployment_generator::set_link(int host1, int host2)
47 set_neighbor(host1, host2);
48 set_neighbor(host2, host1);
51 void deployment_generator::distribute_load()
53 using std::placeholders::_1;
55 if (!opt::auto_depl::random_distribution) {
56 set_load(0, opt::auto_depl::load);
59 srand48(opt::auto_depl::random_seed);
60 std::vector<double> loads(hosts.size());
61 std::generate(loads.begin(), loads.end(), drand48);
62 double factor = opt::auto_depl::load /
63 std::accumulate(loads.begin(), loads.end(), 0.0);
64 std::transform(loads.begin(), loads.end(), loads.begin(),
65 std::bind(std::multiplies<double>(), _1, factor));
66 if (opt::integer_transfer) {
68 std::vector<double> iloads(hosts.size());
69 std::transform(loads.begin(), loads.end(), iloads.begin(), round);
70 // compute the differences between each load and its rounded value
71 std::vector<double> diffs(hosts.size());
72 std::transform(loads.begin(), loads.end(), iloads.begin(),
73 diffs.begin(), std::minus<double>());
74 // compute the absolute values of the diffs
75 std::vector<double> adiffs(hosts.size());
76 std::transform(diffs.begin(), diffs.end(), adiffs.begin(), fabs);
77 // find i, index of the element farthest from its rounded value
79 i = std::max_element(adiffs.begin(), adiffs.end()) - adiffs.begin();
80 // remove element i from diffs, and compute the residual part...
81 diffs[i] = diffs.back();
83 double residue = std::accumulate(diffs.begin(), diffs.end(), 0.0);
84 // ... and compute element i (rounded to avoid numerical errors)
85 iloads[i] = fabs(round(loads[i] + residue));
87 xbt_assert(opt::auto_depl::load ==
88 std::accumulate(iloads.begin(), iloads.end(), 0.0));
89 for (unsigned i = 0 ; i < hosts.size() ; ++i)
90 set_load(i, iloads[i]);
92 for (unsigned i = 0 ; i < hosts.size() ; ++i)
93 set_load(i, loads[i]);
97 void deployment_generator::deploy()
99 xbt_dynar_t args = xbt_dynar_new(sizeof(const char*), NULL);
100 for (unsigned i = 0 ; i < hosts.size() ; ++i) {
101 const char* hostname = hostdata::at(i).get_name();
102 std::ostringstream oss;
103 oss << std::setprecision(12) << hosts[i].load;
104 std::string strload = oss.str();
105 XBT_DEBUG("%s/load -> \"%s\"", hostname, strload.c_str());
106 xbt_dynar_push_as(args, const char*, strload.c_str());
107 for (unsigned j = 0 ; j < hosts[i].neighbors.size() ; ++j) {
108 int neighbor = hosts[i].neighbors[j];
109 const char* neighbor_name = hostdata::at(neighbor).get_name();
110 XBT_DEBUG("%s/neighbor -> \"%s\"", hostname, neighbor_name);
111 xbt_dynar_push_as(args, const char*, neighbor_name);
113 MSG_set_function(hostname, "compute", args);
114 xbt_dynar_reset(args);
116 xbt_dynar_free(&args);
119 void deployment_btree::generate()
121 for (unsigned i = 0 ; i < size() / 2 ; ++i) {
122 unsigned left_child = 2 * i + 1;
123 unsigned right_child = 2 * i + 2;
124 if (left_child < size()) {
125 set_link(i, left_child);
126 if (right_child < size())
127 set_link(i, right_child);
132 void deployment_clique::generate()
134 for (unsigned i = 0 ; i < size() ; ++i)
135 for (unsigned j = 0 ; j < i ; ++j)
139 void deployment_hcube::generate()
141 for (unsigned i = 0 ; i < size() ; ++i)
142 for (unsigned j = 0 ; j < i ; ++j) {
143 // Adapted from rom http://en.wikipedia.org/wiki/Hamming_distance
145 unsigned val = i ^ j;
147 // Count the number of set bits
148 while (val && dist < 2) {
157 void deployment_line::generate()
159 for (unsigned i = 0 ; i < size() - 1 ; ++i)
163 void deployment_ring::generate()
165 set_neighbor(0, size() - 1);
166 for (unsigned i = 0 ; i < size() - 1 ; ++i)
168 set_neighbor(size() - 1, 0);
171 void deployment_star::generate()
173 for (unsigned i = 1 ; i < size() ; ++i)
177 void deployment_torus::generate()
182 unsigned c = (a + b) / 2;
189 // here width == ceil(sqrt(size))
191 unsigned first_on_last_line = (size() - 1) - (size() - 1) % width;
192 XBT_DEBUG("torus size = %zu ; width = %u ; height = %zu ; foll = %u",
193 size(), width, size() / width + !!(size() % width),
195 for (unsigned i = 0; i < size(); i++) {
198 unsigned next_column;
199 unsigned prev_column;
201 next_line = i + width;
202 if (next_line >= size())
203 next_line %= width; // rewind
206 prev_line = i - width;
208 prev_line = first_on_last_line + i; // rewind
209 if (prev_line >= size())
210 prev_line -= width; // need to go at last but one line
213 if (i != size() - 1) {
215 if (next_column % width == 0)
216 next_column -= width; // rewind
218 next_column = first_on_last_line; // special case for last cell
221 if (i % width != 0) {
223 } else if (i < first_on_last_line) {
224 prev_column = i + width - 1; // rewind
226 prev_column = size() - 1; // special case for 1st cell of last line
228 if (next_line != i) {
229 set_neighbor(i, next_line);
230 if (prev_line != next_line)
231 set_neighbor(i, prev_line);
233 if (next_column != i) {
234 set_neighbor(i, next_column);
235 if (prev_column != next_column)
236 set_neighbor(i, prev_column);