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"
14 #include "loba_makhoul.h"
23 // A sum of loads if considered null if it is less than
24 // load_ratio_threshold percent of the sum of loads at init.
25 const double load_ratio_threshold = 1e-4;
28 std::string program_name;
29 int help_requested = 0;
30 bool version_requested = false;
32 // Simulation parameters
35 // Platform and deployment
36 std::string platform_file;
37 std::string deployment_file;
39 // Automatic deployment
42 std::string topology("clique");
47 // Load balancing algorithm
48 std::string loba_algo("simple");
49 bool bookkeeping = false;
50 double min_lb_iter_duration = 1.0; // fixme: find better defaults
51 double min_transfer_amount = 0.0;
52 double max_transfer_amount = 0.0;
54 // Application parameters
55 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
56 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
57 double min_comp_iter_duration = 1.0; // fixme: find better defaults
58 unsigned comp_iter_delay = 0; // fixme: find better defaults
59 double comp_time_delay = 0.0; // fixme: find better defaults
61 // Parameters for the end of the simulation
62 unsigned lb_maxiter = 0;
63 unsigned comp_maxiter = 0;
64 double time_limit = 0;
65 bool exit_on_close = true;
67 // Named parameters lists
68 loba_algorithms_type loba_algorithms;
69 loba_algorithms_type::loba_algorithms_type()
71 NOL_INSERT("fairstrategy", "balance with fair strategy",
73 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
75 NOL_INSERT("none", "no load-balancing (for testing only)",
77 NOL_INSERT("simple", "balance with least loaded neighbor",
81 topologies_type topologies;
82 topologies_type::topologies_type()
84 NOL_INSERT("btree", "binary tree topology, initial load at root",
86 NOL_INSERT("clique", "all connected topology", deployment_clique);
87 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
88 NOL_INSERT("line", "line topology, initial load at one end",
90 NOL_INSERT("ring", "ring topology", deployment_ring);
91 NOL_INSERT("star", "star topology, initial load at center",
93 NOL_INSERT("torus", "torus topology", deployment_torus);
100 // local helper class
103 template <typename T>
104 static bool parse_arg(char opt, const char *arg, T& val);
105 static const char* on_off(bool b);
106 const char* descr(const char* str);
107 template <typename T>
108 const char* val_or_string(const T& val, const char* str,
110 template <typename T>
111 static bool nol_find_prefix(const T& nol, const char* descr,
115 std::string descr_str;
116 std::string val_or_string_str;
121 template <typename T>
122 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
124 std::istringstream str(arg);
125 bool result = (str >> val) && str.eof();
127 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
131 const char* opt_helper::on_off(bool b)
133 return b ? "on" : "off";
136 const char* opt_helper::descr(const char* str)
138 const int descr_width = 40;
139 std::string& res = descr_str;
141 res.resize(descr_width, '.');
145 template <typename T>
146 const char* opt_helper::val_or_string(const T& val, const char* str,
149 std::string& res = val_or_string_str;
151 std::ostringstream oss;
160 template <typename T>
161 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
164 bool result = nol.exists(name);
166 std::stack<std::string> candidates;
167 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
168 const std::string& fullname = nol.get_name(it);
169 if (fullname.compare(0, name.length(), name) == 0)
170 candidates.push(fullname);
172 switch (candidates.size()) {
174 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
177 name = candidates.top();
180 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
183 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
184 while (!candidates.empty()) {
185 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
194 bool opt::parse_args(int* argc, char* argv[])
198 opt::program_name = argv[0];
199 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
201 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
205 while ((c = getopt(*argc, argv,
206 "a:bc:C:d:D:ehi:I:l:L:m:M:N:s:S:t:T:vV")) != -1) {
209 opt::loba_algo = optarg;
210 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
211 "load balancing algorithm",
216 opt::bookkeeping = !opt::bookkeeping;
219 opt::exit_on_close = !opt::exit_on_close;
222 opt::help_requested++;
226 opt::comp_cost = cost_func(optarg);
228 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
235 opt::comm_cost = cost_func(optarg);
237 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
243 PARSE_ARG(opt::comp_iter_delay);
246 PARSE_ARG(opt::comp_time_delay);
249 PARSE_ARG(opt::lb_maxiter);
252 PARSE_ARG(opt::comp_maxiter);
255 PARSE_ARG(opt::log_rate);
258 PARSE_ARG(opt::auto_depl::load);
261 PARSE_ARG(opt::min_transfer_amount);
264 PARSE_ARG(opt::max_transfer_amount);
267 PARSE_ARG(opt::auto_depl::nhosts);
270 PARSE_ARG(opt::min_lb_iter_duration);
273 PARSE_ARG(opt::min_comp_iter_duration);
276 PARSE_ARG(opt::time_limit);
279 opt::auto_depl::topology = optarg;
280 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
281 opt::auto_depl::topology)
285 // nothing to do: this option is checked at the very
286 // beginning of main()
289 opt::version_requested = true;
292 XBT_ERROR("invalid option -- '%c'", optopt);
300 if (opt::version_requested || opt::help_requested)
303 if (optind < *argc) {
304 opt::platform_file = argv[optind++];
306 XBT_ERROR("missing parameter -- <plaform_file>");
309 if (optind < *argc) {
310 opt::deployment_file = argv[optind++];
312 opt::auto_depl::enabled = opt::deployment_file.empty();
314 while (optind < *argc) {
315 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
319 if (opt::max_transfer_amount &&
320 opt::max_transfer_amount < opt::min_transfer_amount) {
321 XBT_ERROR("max. data transfer amount < min. data transfer amount");
332 #define DESCR(description, format, value) \
333 XBT_INFO("| %s: " format, h.descr(description), value)
335 XBT_INFO(",----[ Simulation parameters ]");
336 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
337 DESCR("platform file", "\"%s\"", platform_file.c_str());
338 if (auto_depl::enabled) {
339 XBT_INFO("| automatic deployment enabled");
340 DESCR("- topology", "%s", auto_depl::topology.c_str());
341 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
343 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
346 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
348 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
349 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
350 DESCR("minimum duration between lb. iterations", "%g",
351 min_lb_iter_duration);
352 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
353 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
354 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
355 DESCR("maximum data transfer amount", "%s",
356 h.val_or_string(max_transfer_amount, "no limit"));
357 DESCR("minimum duration between comp. iterations", "%g",
358 min_comp_iter_duration);
359 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
360 DESCR("computations start after time", "%g", comp_time_delay);
361 DESCR("maximum number of lb. iterations", "%s",
362 h.val_or_string(lb_maxiter, "no limit"));
363 DESCR("maximum number of comp. iterations", "%s",
364 h.val_or_string(comp_maxiter, "no limit"));
365 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
366 DESCR("exit on close", "%s", h.on_off(exit_on_close));
375 #define o(opt) " " << std::setw(14) \
376 << std::left << (opt) << std::right << " "
378 #define so(subopt) std::setw(18) << (subopt) << " : "
380 #define so_list(name) do { \
381 name ## _type::iterator it; \
382 for (it = name.begin() ; it != name.end() ; ++it) \
383 std::clog << so(name.get_name(it)) \
384 << name.get_descr(it) << "\n"; \
388 std::clog << "Usage: " << opt::program_name
389 << " [options] <platform_file> [<deployment_file>]\n";
391 std::clog << "\nGlobal options\n";
393 << "print help and exit (use -hh or -hhh for extended help)\n";
394 if (opt::help_requested < 1)
397 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
398 std::clog << o("-V") << "print version and exit\n";
400 std::clog << "\nSimulation parameters\n";
401 std::clog << o("-l value")
402 << "print current load every n lb iterations, 0 to disable"
403 << " [" << opt::log_rate << "]\n";
405 << "verbose: do not override the default logging parameters\n";
407 std::clog << "\nAutomatic deployment options\n";
408 std::clog << o("-T name")
409 << "enable automatic deployment with selected topology"
410 << " [" << opt::auto_depl::topology << "]\n";
411 if (opt::help_requested > 1)
412 so_list(opt::topologies);
413 std::clog << o("-L value")
414 << "total load with auto deployment, 0 for number of hosts"
415 << " [" << opt::auto_depl::load << "]\n";
416 std::clog << o("-N value")
417 << "number of hosts to use with auto deployment, 0 for max."
418 << " [" << opt::auto_depl::nhosts << "]\n";
420 std::clog << "\nLoad balancing algorithm\n";
421 std::clog << o("-a name") << "load balancing algorithm"
422 << " [" << opt::loba_algo << "]\n";
423 if (opt::help_requested > 1)
424 so_list(opt::loba_algorithms);
425 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
426 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
427 std::clog << o("-s value")
428 << "minimum duration between lb. iterations"
429 << " [" << opt::min_lb_iter_duration << "]\n";
431 std::clog << "\nApplication parameters\n";
432 std::clog << o("-c [aN,...]a0")
433 << "polynomial factors for computation cost"
434 << " [" << opt::comp_cost.to_string() << "]\n";
435 std::clog << o("-C [aN,...]a0")
436 << "polynomial factors for communication cost"
437 << " [" << opt::comm_cost.to_string() << "]\n";
438 std::clog << o("-m value")
439 << "minimum data transfer amount"
440 << " [" << opt::min_transfer_amount << "]\n";
441 std::clog << o("-M value")
442 << "maximum data transfer amount, 0 for no limit"
443 << " [" << opt::max_transfer_amount << "]\n";
444 std::clog << o("-S value")
445 << "minimum duration between comp. iterations"
446 << " [" << opt::min_comp_iter_duration << "]\n";
447 std::clog << o("-d value")
448 << "start computations after given number of lb iterations"
449 << " [" << opt::comp_iter_delay << "]\n";
450 std::clog << o("-D value")
451 << "start computations after given time"
452 << " [" << opt::comp_time_delay << "]\n";
454 std::clog << "\nParameters for the end of the simulation\n";
455 std::clog << o("-i value")
456 << "maximum number of lb. iterations, 0 for no limit"
457 << " [" << opt::lb_maxiter << "]\n";
458 std::clog << o("-I value")
459 << "maximum number of comp. iterations, 0 for no limit"
460 << " [" << opt::comp_maxiter << "]\n";
461 std::clog << o("-t value")
462 << "time limit (simulated time), 0 for no limit"
463 << " [" << opt::time_limit << "]\n";
464 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
465 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
467 if (opt::help_requested < 3)
470 std::clog << "\nLogging support\n"
471 << " See SimGrid documentation on:\n"
472 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
473 << " Existing categories are:\n"
474 << " simu : root of following categories\n"
475 << " main : messages from global infrastructure\n"
476 << " depl : messages from auto deployment (inherited from main)\n"
477 << " comm : messages from asynchronous pipes\n"
478 << " proc : messages from base process class\n"
479 << " loba : messages from load-balancer\n"
480 << " thrd : messages from thread wrapper class\n";
482 // std::clog << "\nMiscellaneous low-level parameters\n";