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 unsigned lb_maxiter = 0;
57 unsigned comp_maxiter = 0;
58 double time_limit = 0;
59 bool exit_on_close = true;
61 // Named parameters lists
62 loba_algorithms_type loba_algorithms;
63 loba_algorithms_type::loba_algorithms_type()
65 NOL_INSERT("fairstrategy", "balance with fair strategy", loba_fairstrategy);
66 NOL_INSERT("none", "no load-balancing (for testing only)", process);
67 NOL_INSERT("simple", "balance with least loaded neighbor", loba_simple);
70 topologies_type topologies;
71 topologies_type::topologies_type()
73 NOL_INSERT("btree", "binary tree topology, initial load at root",
75 NOL_INSERT("clique", "all connected topology", deployment_clique);
76 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
77 NOL_INSERT("line", "line topology, initial load at one end",
79 NOL_INSERT("ring", "ring topology", deployment_ring);
80 NOL_INSERT("star", "star topology, initial load at center",
82 NOL_INSERT("torus", "torus topology", deployment_torus);
92 static const char* on_off(bool b);
93 const char* descr(const char* str);
95 const char* val_or_string(const T& val, const char* str,
98 static bool nol_find_prefix(const T& nol, const char* descr,
102 std::string descr_str;
103 std::string val_or_string_str;
108 const char* opt_helper::on_off(bool b)
110 return b ? "on" : "off";
113 const char* opt_helper::descr(const char* str)
115 const int descr_width = 40;
116 std::string& res = descr_str;
118 res.resize(descr_width, '.');
122 template <typename T>
123 const char* opt_helper::val_or_string(const T& val, const char* str,
126 std::string& res = val_or_string_str;
128 std::ostringstream oss;
137 template <typename T>
138 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
141 bool result = nol.exists(name);
143 std::stack<std::string> candidates;
144 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
145 const std::string& fullname = nol.get_name(it);
146 if (fullname.compare(0, name.length(), name) == 0)
147 candidates.push(fullname);
149 switch (candidates.size()) {
151 ERROR2("unknownw %s -- %s", descr, name.c_str());
154 name = candidates.top();
157 DEBUG2("infered %s -- %s", descr, name.c_str());
160 ERROR2("ambiguous %s -- %s", descr, name.c_str());
161 while (!candidates.empty()) {
162 ERROR1(" candidates are -- %s", candidates.top().c_str());
171 bool opt::parse_args(int* argc, char* argv[])
175 opt::program_name = argv[0];
176 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
180 while ((c = getopt(*argc, argv, "a:bc:C:ehi:I:l:L:N:s:t:T:vV")) != -1) {
183 opt::loba_algo = optarg;
184 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
185 "load balancing algorithm",
190 opt::bookkeeping = !opt::bookkeeping;
193 opt::exit_on_close = !opt::exit_on_close;
196 opt::help_requested++;
199 opt::comp_cost = cost_func(optarg);
202 opt::comm_cost = cost_func(optarg);
205 std::istringstream(optarg) >> opt::lb_maxiter;
208 std::istringstream(optarg) >> opt::comp_maxiter;
211 std::istringstream(optarg) >> opt::log_rate;
214 std::istringstream(optarg) >> opt::auto_depl::load;
217 std::istringstream(optarg) >> opt::auto_depl::nhosts;
220 std::istringstream(optarg) >> opt::min_iter_duration;
223 std::istringstream(optarg) >> opt::time_limit;
226 opt::auto_depl::topology = optarg;
227 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
228 opt::auto_depl::topology)
232 // nothing to do: this option is checked at the very
233 // beginning of main()
236 opt::version_requested = true;
239 ERROR1("invalid option -- '%c'", optopt);
245 if (opt::version_requested || opt::help_requested)
248 if (optind < *argc) {
249 opt::platform_file = argv[optind++];
251 ERROR0("missing parameter -- <plaform_file>");
254 if (optind < *argc) {
255 opt::deployment_file = argv[optind++];
257 opt::auto_depl::enabled = opt::deployment_file.empty();
259 while (optind < *argc) {
260 ERROR1("unused parameter -- \"%s\"", argv[optind++]);
271 #define DESCR(description, format, value) \
272 INFO2("| %s: " format, h.descr(description), value)
274 INFO0(",----[ Simulation parameters ]");
275 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
276 DESCR("platform file", "\"%s\"", platform_file.c_str());
277 if (auto_depl::enabled) {
278 INFO0("| automatic deployment enabled");
279 DESCR("- topology", "%s", auto_depl::topology.c_str());
280 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
282 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
285 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
287 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
288 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
289 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
290 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
291 DESCR("minimum duration between iterations", "%g", min_iter_duration);
292 DESCR("maximum number of lb. iterations", "%s",
293 h.val_or_string(lb_maxiter, "infinity"));
294 DESCR("maximum number of comp. iterations", "%s",
295 h.val_or_string(comp_maxiter, "infinity"));
296 DESCR("time limit", "%s", h.val_or_string(time_limit, "infinity"));
297 DESCR("exit on close", "%s", h.on_off(exit_on_close));
306 #define o(opt) " " << std::setw(14) \
307 << std::left << (opt) << std::right << " "
309 #define so(subopt) std::setw(18) << (subopt) << " : "
311 #define so_list(name) do { \
312 name ## _type::iterator it; \
313 for (it = name.begin() ; it != name.end() ; ++it) \
314 std::clog << so(name.get_name(it)) \
315 << name.get_descr(it) << "\n"; \
319 std::clog << "Usage: " << opt::program_name
320 << " [options] <platform_file> [<deployment_file>]\n";
322 std::clog << "\nGlobal options\n";
324 << "print help and exit (use -hh or -hhh for extended help)\n";
325 if (opt::help_requested < 1)
328 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
329 std::clog << o("-V") << "print version and exit\n";
331 std::clog << "\nSimulation parameters\n";
332 std::clog << o("-l value")
333 << "print current load every n lb iterations, 0 to disable"
334 << " [" << opt::log_rate << "]\n";
336 << "verbose: do not override the default logging parameters\n";
338 std::clog << "\nAutomatic deployment options\n";
339 std::clog << o("-T name")
340 << "enable automatic deployment with selected topology"
341 << " [" << opt::auto_depl::topology << "]\n";
342 if (opt::help_requested > 1)
343 so_list(opt::topologies);
344 std::clog << o("-L value")
345 << "total load with auto deployment, 0 for number of hosts"
346 << " [" << opt::auto_depl::load << "]\n";
347 std::clog << o("-N value")
348 << "number of hosts to use with auto deployment, 0 for max."
349 << " [" << opt::auto_depl::nhosts << "]\n";
351 std::clog << "\nLoad balancing algorithm\n";
352 std::clog << o("-a name") << "load balancing algorithm"
353 << " [" << opt::loba_algo << "]\n";
354 if (opt::help_requested > 1)
355 so_list(opt::loba_algorithms);
356 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
357 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
359 std::clog << "\nApplication parameters\n";
360 std::clog << o("-c [fn,...]f0")
361 << "polynomial factors for computation cost"
362 << " [" << opt::comp_cost.to_string() << "]\n";
363 std::clog << o("-C [fn,...]f0")
364 << "polynomial factors for communication cost"
365 << " [" << opt::comm_cost.to_string() << "]\n";
366 std::clog << o("-s value")
367 << "minimum duration between iterations"
368 << " [" << opt::min_iter_duration << "]\n";
370 std::clog << "\nParameters for the end of the simulation\n";
371 std::clog << o("-i value")
372 << "maximum number of lb. iterations, 0 for infinity"
373 << " [" << opt::lb_maxiter << "]\n";
374 std::clog << o("-I value")
375 << "maximum number of comp. iterations, 0 for infinity"
376 << " [" << opt::comp_maxiter << "]\n";
377 std::clog << o("-t value")
378 << "time limit (simulated time), 0 for infinity"
379 << " [" << opt::time_limit << "]\n";
380 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
381 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
383 if (opt::help_requested < 3)
386 std::clog << "\nLogging support\n"
387 << " See SimGrid documentation on:\n"
388 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
389 << " Existing categories are:\n"
390 << " simu : root of following categories\n"
391 << " main : messages from global infrastructure\n"
392 << " depl : messages from auto deployment (inherited from main)\n"
393 << " comm : messages from asynchronous pipes\n"
394 << " proc : messages from base process class\n"
395 << " loba : messages from load-balancer\n";
397 // std::clog << "\nMiscellaneous low-level parameters\n";