10 #include <xbt/dynar.h>
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(depl);
19 #include "deployment.h"
21 void MY_launch_application()
23 deployment_generator* gen;
24 gen = opt::topologies.new_instance(opt::auto_depl::topology);
26 gen->distribute_load();
31 deployment_generator::deployment_generator()
32 : hosts(opt::auto_depl::nhosts)
36 void deployment_generator::set_load(int host, double load)
38 hosts[host].load = load;
41 void deployment_generator::set_neighbor(int host, int neighbor)
43 hosts[host].neighbors.push_back(neighbor);
46 void deployment_generator::set_link(int host1, int host2)
48 set_neighbor(host1, host2);
49 set_neighbor(host2, host1);
52 void deployment_generator::distribute_load()
54 using std::placeholders::_1;
56 if (!opt::auto_depl::random_distribution) {
57 set_load(0, opt::auto_depl::load);
60 std::vector<double> loads(hosts.size());
61 if (opt::auto_depl::random_seed != 1) {
62 srand48(opt::auto_depl::random_seed);
63 std::generate(loads.begin(), loads.end(), drand48);
65 std::fill(loads.begin(), loads.end(), 1.0);
67 double factor = opt::auto_depl::load /
68 std::accumulate(loads.begin(), loads.end(), 0.0);
69 std::transform(loads.begin(), loads.end(), loads.begin(),
70 std::bind(std::multiplies<double>(), _1, factor));
71 if (opt::integer_transfer) {
75 for (i = 0 ; i < hosts.size() - 1; ++i) {
77 iload = std::floor(loads[i]);
78 else if (residue > 0.0)
79 iload = std::ceil(loads[i]);
80 else // residue == 0.0
81 iload = std::round(loads[i]);
82 residue += (loads[i] - iload);
85 // abs(round(...)) to avoid rounding errors, or a value of -0
86 // Note: i == hosts.size() - 1
87 iload = std::abs(std::round(loads[i] + residue));
90 xbt_assert(opt::auto_depl::load ==
91 std::accumulate(loads.begin(), loads.end(), 0.0));
93 for (unsigned i = 0 ; i < hosts.size() ; ++i)
94 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 == std::ceil(std::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);