6 #include <unistd.h> // getopt
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
11 #include "deployment.h"
13 #include "loba_besteffort.h"
14 #include "loba_fairstrategy.h"
15 #include "loba_makhoul.h"
16 #include "loba_makhoul2.h"
17 #include "loba_simple.h"
22 #define DATA_DESCR_WIDTH 42
28 // A sum of loads if considered null if it is less than
29 // load_ratio_threshold percent of the sum of loads at init.
30 const double load_ratio_threshold = 1e-4;
33 std::string program_name;
34 int help_requested = 0;
35 bool version_requested = false;
36 int option_x = 0; // hidden option, for debugging purpose
38 // Simulation parameters
40 bool exit_request = false;
42 // Platform and deployment
43 std::string platform_file;
44 std::string deployment_file;
46 // Automatic deployment
49 std::string topology("clique");
52 bool random_distribution = false;
53 unsigned long random_seed = 0;
56 // Load balancing algorithm
57 std::string loba_algo("simple");
58 bool bookkeeping = false;
59 double min_lb_iter_duration = 1.0; // fixme: find better defaults
60 double min_transfer_amount = 0.0;
61 double max_transfer_amount = 0.0;
63 // Application parameters
64 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
65 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
66 double min_comp_iter_duration = 1.0; // fixme: find better defaults
67 unsigned comp_iter_delay = 0; // fixme: find better defaults
68 double comp_time_delay = 0.0; // fixme: find better defaults
70 // Parameters for the end of the simulation
71 unsigned lb_maxiter = 0;
72 unsigned comp_maxiter = 0;
73 double time_limit = 0;
74 bool exit_on_close = true;
76 // Named parameters lists
77 loba_algorithms_type loba_algorithms;
78 loba_algorithms_type::loba_algorithms_type()
80 NOL_INSERT("besteffort", "balance with best effort strategy",
82 NOL_INSERT("fairstrategy", "balance with fair strategy",
84 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
86 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
88 NOL_INSERT("none", "no load-balancing (for testing only)",
90 NOL_INSERT("simple", "balance with least loaded neighbor",
94 topologies_type topologies;
95 topologies_type::topologies_type()
97 NOL_INSERT("btree", "binary tree topology, initial load at root",
99 NOL_INSERT("clique", "all connected topology", deployment_clique);
100 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
101 NOL_INSERT("line", "line topology, initial load at one end",
103 NOL_INSERT("ring", "ring topology", deployment_ring);
104 NOL_INSERT("star", "star topology, initial load at center",
106 NOL_INSERT("torus", "torus topology", deployment_torus);
113 // local helper class
116 template <typename T>
117 static bool parse_arg(char opt, const char *arg, T& val);
118 static const char* on_off(bool b);
119 const char* descr(const char* str);
120 template <typename T>
121 const char* val_or_string(const T& val, const char* str,
123 template <typename T>
124 static bool nol_find_prefix(const T& nol, const char* descr,
128 std::string descr_str;
129 std::string val_or_string_str;
134 template <typename T>
135 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
137 std::istringstream str(arg);
138 bool result = (str >> val) && str.eof();
140 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
144 const char* opt_helper::on_off(bool b)
146 return b ? "on" : "off";
149 const char* opt_helper::descr(const char* str)
151 std::string& res = descr_str;
153 res.resize(DATA_DESCR_WIDTH, '.');
157 template <typename T>
158 const char* opt_helper::val_or_string(const T& val, const char* str,
161 std::string& res = val_or_string_str;
163 std::ostringstream oss;
172 template <typename T>
173 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
176 bool result = nol.exists(name);
178 std::stack<std::string> candidates;
179 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
180 const std::string& fullname = nol.get_name(it);
181 if (fullname.compare(0, name.length(), name) == 0)
182 candidates.push(fullname);
184 switch (candidates.size()) {
186 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
189 name = candidates.top();
192 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
195 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
196 while (!candidates.empty()) {
197 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
206 bool opt::parse_args(int* argc, char* argv[])
210 opt::program_name = argv[0];
211 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
213 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
217 while ((c = getopt(*argc, argv,
218 "a:bc:C:d:D:ehi:I:l:L:m:M:N:r:Rs:S:t:T:vVx:")) != -1) {
221 opt::loba_algo = optarg;
222 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
223 "load balancing algorithm",
228 opt::bookkeeping = !opt::bookkeeping;
231 opt::exit_on_close = !opt::exit_on_close;
234 opt::help_requested++;
238 opt::comp_cost = cost_func(optarg);
240 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
247 opt::comm_cost = cost_func(optarg);
249 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
255 PARSE_ARG(opt::comp_iter_delay);
258 PARSE_ARG(opt::comp_time_delay);
261 PARSE_ARG(opt::lb_maxiter);
264 PARSE_ARG(opt::comp_maxiter);
267 PARSE_ARG(opt::log_rate);
270 PARSE_ARG(opt::auto_depl::load);
273 PARSE_ARG(opt::min_transfer_amount);
276 PARSE_ARG(opt::max_transfer_amount);
279 PARSE_ARG(opt::auto_depl::nhosts);
282 PARSE_ARG(opt::auto_depl::random_seed);
285 opt::auto_depl::random_distribution =
286 !opt::auto_depl::random_distribution;
289 PARSE_ARG(opt::min_lb_iter_duration);
292 PARSE_ARG(opt::min_comp_iter_duration);
295 PARSE_ARG(opt::time_limit);
298 opt::auto_depl::topology = optarg;
299 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
300 opt::auto_depl::topology)
304 // nothing to do: this option is checked at the very
305 // beginning of main()
308 opt::version_requested = true;
311 PARSE_ARG(opt::option_x);
312 XBT_WARN("option_x set to %d", opt::option_x);
315 XBT_ERROR("invalid option -- '%c'", optopt);
323 if (opt::version_requested || opt::help_requested)
326 if (optind < *argc) {
327 opt::platform_file = argv[optind++];
329 XBT_ERROR("missing parameter -- <plaform_file>");
332 if (optind < *argc) {
333 opt::deployment_file = argv[optind++];
335 opt::auto_depl::enabled = opt::deployment_file.empty();
337 while (optind < *argc) {
338 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
342 if (opt::max_transfer_amount &&
343 opt::max_transfer_amount < opt::min_transfer_amount) {
344 XBT_ERROR("max. data transfer amount < min. data transfer amount");
348 if (!opt::auto_depl::random_seed)
349 opt::auto_depl::random_seed = time(NULL);
358 #define DESCR(description, format, value) \
359 XBT_INFO("| %s: " format, h.descr(description), value)
361 XBT_INFO(",----[ Simulation parameters ]");
362 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
363 DESCR("platform file", "\"%s\"", platform_file.c_str());
364 if (auto_depl::enabled) {
365 XBT_INFO("| automatic deployment enabled");
366 DESCR("- topology", "%s", auto_depl::topology.c_str());
367 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
369 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
371 DESCR("- random initial load distribution", "%s",
372 h.on_off(auto_depl::random_distribution));
373 DESCR("- random seed", "%s",
374 h.val_or_string(auto_depl::random_seed, "time based"));
376 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
378 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
379 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
380 DESCR("minimum duration between lb. iterations", "%g",
381 min_lb_iter_duration);
382 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
383 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
384 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
385 DESCR("maximum data transfer amount", "%s",
386 h.val_or_string(max_transfer_amount, "no limit"));
387 DESCR("minimum duration between comp. iterations", "%g",
388 min_comp_iter_duration);
389 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
390 DESCR("computations start after time", "%g", comp_time_delay);
391 DESCR("maximum number of lb. iterations", "%s",
392 h.val_or_string(lb_maxiter, "no limit"));
393 DESCR("maximum number of comp. iterations", "%s",
394 h.val_or_string(comp_maxiter, "no limit"));
395 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
396 DESCR("exit on close", "%s", h.on_off(exit_on_close));
405 #define o(opt) " " << std::setw(14) \
406 << std::left << (opt) << std::right << " "
408 #define so(subopt) std::setw(18) << (subopt) << " : "
410 #define so_list(name) do { \
411 name ## _type::iterator it; \
412 for (it = name.begin() ; it != name.end() ; ++it) \
413 std::clog << so(name.get_name(it)) \
414 << name.get_descr(it) << "\n"; \
418 std::clog << "Usage: " << opt::program_name
419 << " [options] <platform_file> [<deployment_file>]\n";
421 std::clog << "\nGlobal options\n";
423 << "print help and exit (use -hh or -hhh for extended help)\n";
424 if (opt::help_requested < 1)
427 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
428 std::clog << o("-V") << "print version and exit\n";
430 std::clog << "\nSimulation parameters\n";
431 std::clog << o("-l value")
432 << "print current load every n lb iterations, 0 to disable"
433 << " [" << opt::log_rate << "]\n";
435 << "verbose: do not override the default logging parameters\n";
437 std::clog << "\nAutomatic deployment options\n";
438 std::clog << o("-T name")
439 << "enable automatic deployment with selected topology"
440 << " [" << opt::auto_depl::topology << "]\n";
441 if (opt::help_requested > 1)
442 so_list(opt::topologies);
443 std::clog << o("-L value")
444 << "total load with auto deployment, 0 for number of hosts"
445 << " [" << opt::auto_depl::load << "]\n";
446 std::clog << o("-N value")
447 << "number of hosts to use with auto deployment, 0 for max."
448 << " [" << opt::auto_depl::nhosts << "]\n";
450 << "toggle random initial load distribution"
451 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
453 std::clog << o("-r value")
454 << "random seed, 0 for using it on time()"
455 << " [" << opt::auto_depl::random_seed << "]\n";
457 std::clog << "\nLoad balancing algorithm\n";
458 std::clog << o("-a name") << "load balancing algorithm"
459 << " [" << opt::loba_algo << "]\n";
460 if (opt::help_requested > 1)
461 so_list(opt::loba_algorithms);
462 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
463 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
464 std::clog << o("-s value")
465 << "minimum duration between lb. iterations"
466 << " [" << opt::min_lb_iter_duration << "]\n";
468 std::clog << "\nApplication parameters\n";
469 std::clog << o("-c [aN,...]a0")
470 << "polynomial factors for computation cost"
471 << " [" << opt::comp_cost.to_string() << "]\n";
472 std::clog << o("-C [aN,...]a0")
473 << "polynomial factors for communication cost"
474 << " [" << opt::comm_cost.to_string() << "]\n";
475 std::clog << o("-m value")
476 << "minimum data transfer amount"
477 << " [" << opt::min_transfer_amount << "]\n";
478 std::clog << o("-M value")
479 << "maximum data transfer amount, 0 for no limit"
480 << " [" << opt::max_transfer_amount << "]\n";
481 std::clog << o("-S value")
482 << "minimum duration between comp. iterations"
483 << " [" << opt::min_comp_iter_duration << "]\n";
484 std::clog << o("-d value")
485 << "start computations after given number of lb iterations"
486 << " [" << opt::comp_iter_delay << "]\n";
487 std::clog << o("-D value")
488 << "start computations after given time"
489 << " [" << opt::comp_time_delay << "]\n";
491 std::clog << "\nParameters for the end of the simulation\n";
492 std::clog << o("-i value")
493 << "maximum number of lb. iterations, 0 for no limit"
494 << " [" << opt::lb_maxiter << "]\n";
495 std::clog << o("-I value")
496 << "maximum number of comp. iterations, 0 for no limit"
497 << " [" << opt::comp_maxiter << "]\n";
498 std::clog << o("-t value")
499 << "time limit (simulated time), 0 for no limit"
500 << " [" << opt::time_limit << "]\n";
501 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
502 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
504 if (opt::help_requested < 3)
507 std::clog << "\nLogging support\n"
508 << " See SimGrid documentation on:\n"
509 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
510 << " Existing categories are:\n"
511 << " simu : root of following categories\n"
512 << " main : messages from global infrastructure\n"
513 << " depl : messages from auto deployment (inherited from main)\n"
514 << " comm : messages from asynchronous pipes\n"
515 << " proc : messages from base process class\n"
516 << " loba : messages from load-balancer\n"
517 << " thrd : messages from thread wrapper class\n";
519 // std::clog << "\nMiscellaneous low-level parameters\n";