5 #include <unistd.h> // getopt
8 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
10 #include "deployment.h"
12 #include "loba_simple.h"
13 #include "loba_fairstrategy.h"
21 // A sum of loads if considered null if it is less than
22 // load_ratio_threshold percent of the sum of loads at init.
23 const double load_ratio_threshold = 1e-4;
26 std::string program_name;
27 int help_requested = 0;
28 bool version_requested = false;
30 // Simulation parameters
31 unsigned log_rate = 1;
33 // Platform and deployment
34 std::string platform_file;
35 std::string deployment_file;
37 // Automatic deployment
40 std::string topology("clique");
45 // Load balancing algorithm
46 std::string loba_algo("simple");
47 bool bookkeeping = false;
49 // Application parameters
50 // fixme: find better defaults
51 cost_func comp_cost("1e9, 0");
52 cost_func comm_cost("1, 0");
53 double min_iter_duration = 1.0;
55 // Parameters for the end of the simulation
56 // fixme: find better defaults
57 unsigned lb_maxiter = 10;
58 unsigned comp_maxiter = 0;
59 double time_limit = 0;
60 bool exit_on_close = false;
62 // Named parameters lists
63 loba_algorithms_type loba_algorithms;
64 loba_algorithms_type::loba_algorithms_type()
66 NOL_INSERT("fairstrategy", "balance with fair strategy", loba_fairstrategy);
67 NOL_INSERT("none", "no load-balancing (for testing only)", process);
68 NOL_INSERT("simple", "balance with least loaded neighbor", loba_simple);
71 topologies_type topologies;
72 topologies_type::topologies_type()
74 NOL_INSERT("btree", "binary tree topology, initial load at root",
76 NOL_INSERT("clique", "all connected topology", deployment_clique);
77 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
78 NOL_INSERT("line", "line topology, initial load at one end",
80 NOL_INSERT("ring", "ring topology", deployment_ring);
81 NOL_INSERT("star", "star topology, initial load at center",
83 NOL_INSERT("torus", "torus topology", deployment_torus);
93 static const char* on_off(bool b);
94 const char* descr(const char* str);
96 const char* val_or_string(const T& val, const char* str,
99 static bool nol_find_prefix(const T& nol, const char* descr,
103 std::string descr_str;
104 std::string val_or_string_str;
109 const char* opt_helper::on_off(bool b)
111 return b ? "on" : "off";
114 const char* opt_helper::descr(const char* str)
116 const int descr_width = 40;
117 std::string& res = descr_str;
119 res.resize(descr_width, '.');
123 template <typename T>
124 const char* opt_helper::val_or_string(const T& val, const char* str,
127 std::string& res = val_or_string_str;
129 std::ostringstream oss;
138 template <typename T>
139 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
142 bool result = nol.exists(name);
144 std::stack<std::string> candidates;
145 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
146 const std::string& fullname = nol.get_name(it);
147 if (fullname.compare(0, name.length(), name) == 0)
148 candidates.push(fullname);
150 switch (candidates.size()) {
152 ERROR2("unknownw %s -- %s", descr, name.c_str());
155 name = candidates.top();
158 DEBUG2("infered %s -- %s", descr, name.c_str());
161 ERROR2("ambiguous %s -- %s", descr, name.c_str());
162 while (!candidates.empty()) {
163 ERROR1(" candidates are -- %s", candidates.top().c_str());
172 bool opt::parse_args(int* argc, char* argv[])
176 opt::program_name = argv[0];
177 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
181 while ((c = getopt(*argc, argv, "a:bc:C:ehi:I:l:L:N:s:t:T:vV")) != -1) {
184 opt::loba_algo = optarg;
185 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
186 "load balancing algorithm",
191 opt::bookkeeping = !opt::bookkeeping;
194 opt::exit_on_close = !opt::exit_on_close;
197 opt::help_requested++;
200 opt::comp_cost = cost_func(optarg);
203 opt::comm_cost = cost_func(optarg);
206 std::istringstream(optarg) >> opt::lb_maxiter;
209 std::istringstream(optarg) >> opt::comp_maxiter;
212 std::istringstream(optarg) >> opt::log_rate;
215 std::istringstream(optarg) >> opt::auto_depl::load;
218 std::istringstream(optarg) >> opt::auto_depl::nhosts;
221 std::istringstream(optarg) >> opt::min_iter_duration;
224 std::istringstream(optarg) >> opt::time_limit;
227 opt::auto_depl::topology = optarg;
228 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
229 opt::auto_depl::topology)
233 // nothing to do: this option is checked at the very
234 // beginning of main()
237 opt::version_requested = true;
240 ERROR1("invalid option -- '%c'", optopt);
246 if (opt::version_requested || opt::help_requested)
249 if (optind < *argc) {
250 opt::platform_file = argv[optind++];
252 ERROR0("missing parameter -- <plaform_file>");
255 if (optind < *argc) {
256 opt::deployment_file = argv[optind++];
258 opt::auto_depl::enabled = opt::deployment_file.empty();
260 while (optind < *argc) {
261 ERROR1("unused parameter -- \"%s\"", argv[optind++]);
272 #define DESCR(description, format, value) \
273 INFO2("| %s: " format, h.descr(description), value)
275 INFO0(",----[ Simulation parameters ]");
276 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
277 DESCR("platform file", "\"%s\"", platform_file.c_str());
278 if (auto_depl::enabled) {
279 INFO0("| automatic deployment enabled");
280 DESCR("- topology", "%s", auto_depl::topology.c_str());
281 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
283 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
286 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
288 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
289 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
290 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
291 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
292 DESCR("minimum duration between iterations", "%g", min_iter_duration);
293 DESCR("maximum number of lb. iterations", "%s",
294 h.val_or_string(lb_maxiter, "infinity"));
295 DESCR("maximum number of comp. iterations", "%s",
296 h.val_or_string(comp_maxiter, "infinity"));
297 DESCR("time limit", "%s", h.val_or_string(time_limit, "infinity"));
298 DESCR("exit on close", "%s", h.on_off(exit_on_close));
307 #define o(opt) " " << std::setw(14) \
308 << std::left << (opt) << std::right << " "
310 #define so(subopt) std::setw(18) << (subopt) << " : "
312 #define so_list(name) do { \
313 name ## _type::iterator it; \
314 for (it = name.begin() ; it != name.end() ; ++it) \
315 std::clog << so(name.get_name(it)) \
316 << name.get_descr(it) << "\n"; \
320 std::clog << "Usage: " << opt::program_name
321 << " [options] <platform_file> [<deployment_file>]\n";
323 std::clog << "\nGlobal options\n";
325 << "print help and exit (use -hh or -hhh for extended help)\n";
326 if (opt::help_requested < 1)
329 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
330 std::clog << o("-V") << "print version and exit\n";
332 std::clog << "\nSimulation parameters\n";
333 std::clog << o("-l value")
334 << "print current load every n lb iterations, 0 to disable"
335 << " [" << opt::log_rate << "]\n";
337 << "verbose: do not override the default logging parameters\n";
339 std::clog << "\nAutomatic deployment options\n";
340 std::clog << o("-T name")
341 << "enable automatic deployment with selected topology"
342 << " [" << opt::auto_depl::topology << "]\n";
343 if (opt::help_requested > 1)
344 so_list(opt::topologies);
345 std::clog << o("-L value")
346 << "total load with auto deployment, 0 for number of hosts"
347 << " [" << opt::auto_depl::load << "]\n";
348 std::clog << o("-N value")
349 << "number of hosts to use with auto deployment, 0 for max."
350 << " [" << opt::auto_depl::nhosts << "]\n";
352 std::clog << "\nLoad balancing algorithm\n";
353 std::clog << o("-a name") << "load balancing algorithm"
354 << " [" << opt::loba_algo << "]\n";
355 if (opt::help_requested > 1)
356 so_list(opt::loba_algorithms);
357 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
358 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
360 std::clog << "\nApplication parameters\n";
361 std::clog << o("-c [fn,...]f0")
362 << "polynomial factors for computation cost"
363 << " [" << opt::comp_cost.to_string() << "]\n";
364 std::clog << o("-C [fn,...]f0")
365 << "polynomial factors for communication cost"
366 << " [" << opt::comm_cost.to_string() << "]\n";
367 std::clog << o("-s value")
368 << "minimum duration between iterations"
369 << " [" << opt::min_iter_duration << "]\n";
371 std::clog << "\nParameters for the end of the simulation\n";
372 std::clog << o("-i value")
373 << "maximum number of lb. iterations, 0 for infinity"
374 << " [" << opt::lb_maxiter << "]\n";
375 std::clog << o("-I value")
376 << "maximum number of comp. iterations, 0 for infinity"
377 << " [" << opt::comp_maxiter << "]\n";
378 std::clog << o("-t value")
379 << "time limit (simulated time), 0 for infinity"
380 << " [" << opt::time_limit << "]\n";
381 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
382 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
384 if (opt::help_requested < 3)
387 std::clog << "\nLogging support\n"
388 << " See SimGrid documentation on:\n"
389 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
390 << " Existing categories are:\n"
391 << " simu : root of following categories\n"
392 << " main : messages from global infrastructure\n"
393 << " depl : messages from auto deployment (inherited from main)\n"
394 << " comm : messages from asynchronous pipes\n"
395 << " proc : messages from base process class\n"
396 << " loba : messages from load-balancer\n";
398 // std::clog << "\nMiscellaneous low-level parameters\n";