6 #include <unistd.h> // getopt
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
11 #include "deployment.h"
13 #include "loba_2besteffort.h"
14 #include "loba_besteffort.h"
15 #include "loba_bulk.h"
16 #include "loba_fairstrategy.h"
18 #include "loba_makhoul.h"
19 #include "loba_makhoul2.h"
20 #include "loba_makhoul3.h"
21 #include "loba_simple.h"
26 #define DATA_DESCR_WIDTH 42
32 // A sum of loads if considered null if it is less than
33 // load_ratio_threshold percent of the sum of loads at init.
34 const double load_ratio_threshold = 1e-4;
37 std::string program_name;
38 int help_requested = 0;
39 bool version_requested = false;
40 int option_x = 0; // hidden option (int)
41 double option_X = 0.0; // hidden option (double)
43 // Simulation parameters
45 volatile std::sig_atomic_t exit_request = 0;
47 // Platform and deployment
48 std::string platform_file;
49 std::string deployment_file;
51 // Automatic deployment
54 std::string topology("clique");
57 bool random_distribution = false;
58 unsigned long random_seed = 0;
61 // Load balancing algorithm
62 std::string loba_algo("simple");
63 bool bookkeeping = false;
64 bool egocentric = false;
65 double min_lb_iter_duration = 1.0; // fixme: find better defaults
66 double min_transfer_amount = 0.0;
67 double max_transfer_amount = 0.0;
68 bool integer_transfer = false;
69 unsigned loba_best_divisor = 1;
71 // Application parameters
72 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
73 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
74 double min_comp_iter_duration = 1.0; // fixme: find better defaults
75 unsigned comp_iter_delay = 0; // fixme: find better defaults
76 double comp_time_delay = 0.0; // fixme: find better defaults
78 // Parameters for the end of the simulation
79 double avg_load_ratio = 0.0;
80 unsigned lb_maxiter = 0;
81 unsigned comp_maxiter = 0;
82 double time_limit = 0;
83 unsigned exit_on_convergence = 0;
84 bool exit_on_close = true;
86 // Named parameters lists
87 loba_algorithms_type loba_algorithms;
88 loba_algorithms_type::loba_algorithms_type()
90 NOL_INSERT("2besteffort", "balance with best effort strategy (take #2)",
92 NOL_INSERT("besteffort", "balance with best effort strategy",
94 NOL_INSERT("bulk", "a multi-load-units assignation rule without ordering...",
96 NOL_INSERT("fairstrategy", "balance with fair strategy",
98 NOL_INSERT("lln", "balance with less loaded neighbors without ordering-bulk method",
100 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
102 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
104 NOL_INSERT("makhoul3", "variation on Makhoul's algorithm",
106 NOL_INSERT("none", "no load-balancing (for testing only)",
108 NOL_INSERT("simple", "balance with least loaded neighbor",
112 topologies_type topologies;
113 topologies_type::topologies_type()
115 NOL_INSERT("btree", "binary tree topology, initial load at root",
117 NOL_INSERT("clique", "all connected topology", deployment_clique);
118 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
119 NOL_INSERT("line", "line topology, initial load at one end",
121 NOL_INSERT("ring", "ring topology", deployment_ring);
122 NOL_INSERT("star", "star topology, initial load at center",
124 NOL_INSERT("torus", "torus topology", deployment_torus);
131 // local helper class
134 template <typename T>
135 static bool parse_arg(char opt, const char *arg, T& val);
136 static const char* on_off(bool b);
137 const char* descr(const char* str);
138 template <typename T>
139 const char* val_or_string(const T& val, const char* str,
141 template <typename T>
142 static bool nol_find_prefix(const T& nol, const char* descr,
146 std::string descr_str;
147 std::string val_or_string_str;
152 template <typename T>
153 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
155 std::istringstream str(arg);
156 bool result = (str >> val) && str.eof();
158 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
162 const char* opt_helper::on_off(bool b)
164 return b ? "on" : "off";
167 const char* opt_helper::descr(const char* str)
170 descr_str.resize(DATA_DESCR_WIDTH, '.');
171 return descr_str.c_str();
174 template <typename T>
175 const char* opt_helper::val_or_string(const T& val, const char* str,
179 std::ostringstream oss;
181 val_or_string_str = oss.str();
183 val_or_string_str = str;
185 return val_or_string_str.c_str();
188 template <typename T>
189 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
192 bool result = nol.exists(name);
194 std::stack<std::string> candidates;
195 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
196 const std::string& fullname = nol.get_name(it);
197 if (fullname.compare(0, name.length(), name) == 0)
198 candidates.push(fullname);
200 switch (candidates.size()) {
202 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
205 name = candidates.top();
208 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
211 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
212 while (!candidates.empty()) {
213 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
222 bool opt::parse_args(int* argc, char* argv[])
226 opt::program_name = argv[0];
227 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
229 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
233 while ((c = getopt(*argc, argv,
235 "a:bc:C:d:D:eEg:hi:I:k:l:L:m:M:N:r:Rs:S:t:T:vVx:X:Z"))
239 PARSE_ARG(opt::avg_load_ratio);
242 opt::loba_algo = optarg;
243 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
244 "load balancing algorithm",
249 opt::bookkeeping = !opt::bookkeeping;
253 opt::comp_cost = cost_func(optarg);
255 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
262 opt::comm_cost = cost_func(optarg);
264 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
270 PARSE_ARG(opt::comp_iter_delay);
273 PARSE_ARG(opt::comp_time_delay);
276 opt::exit_on_close = !opt::exit_on_close;
279 opt::egocentric = !opt::egocentric;
282 PARSE_ARG(opt::exit_on_convergence);
285 opt::help_requested++;
288 PARSE_ARG(opt::lb_maxiter);
291 PARSE_ARG(opt::loba_best_divisor);
294 PARSE_ARG(opt::comp_maxiter);
297 PARSE_ARG(opt::log_rate);
300 PARSE_ARG(opt::auto_depl::load);
303 PARSE_ARG(opt::min_transfer_amount);
306 PARSE_ARG(opt::max_transfer_amount);
309 PARSE_ARG(opt::auto_depl::nhosts);
312 PARSE_ARG(opt::auto_depl::random_seed);
315 opt::auto_depl::random_distribution =
316 !opt::auto_depl::random_distribution;
319 PARSE_ARG(opt::min_lb_iter_duration);
322 PARSE_ARG(opt::min_comp_iter_duration);
325 PARSE_ARG(opt::time_limit);
328 opt::auto_depl::topology = optarg;
329 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
330 opt::auto_depl::topology)
334 // nothing to do: this option is checked at the very
335 // beginning of main()
338 opt::version_requested = true;
341 PARSE_ARG(opt::option_x);
342 XBT_WARN("option_x set to %d", opt::option_x);
345 PARSE_ARG(opt::option_X);
346 XBT_WARN("option_X set to %g", opt::option_X);
349 opt::integer_transfer = !opt::integer_transfer;
352 XBT_ERROR("invalid option -- '%c'", optopt);
360 if (opt::version_requested || opt::help_requested)
363 if (optind < *argc) {
364 opt::platform_file = argv[optind++];
366 XBT_ERROR("missing parameter -- <plaform_file>");
369 if (optind < *argc) {
370 opt::deployment_file = argv[optind++];
372 opt::auto_depl::enabled = opt::deployment_file.empty();
374 while (optind < *argc) {
375 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
379 if (opt::max_transfer_amount &&
380 opt::max_transfer_amount < opt::min_transfer_amount) {
381 XBT_ERROR("max. data transfer amount (%g) <"
382 " min. data transfer amount (%g)",
383 opt::max_transfer_amount, opt::min_transfer_amount);
387 if (opt::loba_best_divisor <= 0) {
388 XBT_ERROR("divisor for algorithms *best* (%u) should be positive",
389 opt::loba_best_divisor);
393 if (!opt::auto_depl::random_seed)
394 opt::auto_depl::random_seed = time(NULL);
403 #define DESCR(description, format, value) \
404 XBT_INFO("| %s: " format, h.descr(description), value)
406 XBT_INFO(",----[ Simulation parameters ]");
407 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
408 DESCR("platform file", "\"%s\"", platform_file.c_str());
409 if (auto_depl::enabled) {
410 XBT_INFO("| automatic deployment enabled");
411 DESCR("- topology", "%s", auto_depl::topology.c_str());
412 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
414 if (auto_depl::load < 0.0)
415 DESCR("- initial load (average)", "%g", -auto_depl::load);
417 DESCR("- initial load (total)", "%g", auto_depl::load);
418 DESCR("- random initial load distribution", "%s",
419 h.on_off(auto_depl::random_distribution));
420 DESCR("- random seed", "%s", // NOTE: cannot be 0 here anymore
421 h.val_or_string(auto_depl::random_seed, "constant random", 1UL));
423 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
425 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
426 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
427 DESCR("egocentric mode", "%s", h.on_off(egocentric));
428 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
429 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
430 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
431 DESCR("maximum data transfer amount", "%s",
432 h.val_or_string(max_transfer_amount, "no limit"));
433 DESCR("only transfer integer data shares", "%s",
434 h.on_off(integer_transfer));
435 DESCR("divisor for algorithms *best*", "%u", loba_best_divisor);
436 DESCR("minimum duration between lb. iterations", "%g",
437 min_lb_iter_duration);
438 DESCR("minimum duration between comp. iterations", "%g",
439 min_comp_iter_duration);
440 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
441 DESCR("computations start after time", "%g", comp_time_delay);
442 DESCR("maximum number of lb. iterations", "%s",
443 h.val_or_string(lb_maxiter, "no limit"));
444 DESCR("maximum number of comp. iterations", "%s",
445 h.val_or_string(comp_maxiter, "no limit"));
446 DESCR("convergence is assumed within (\%)", "%g", opt::avg_load_ratio);
447 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
448 DESCR("exit on convergence", "%s",
449 h.val_or_string(exit_on_convergence, "disabled"));
450 DESCR("exit on close", "%s", h.on_off(exit_on_close));
459 #define o(opt) " " << std::setw(14) \
460 << std::left << (opt) << std::right << " "
462 #define so(subopt) std::setw(18) << (subopt) << " : "
464 #define so_list(name) do { \
465 name ## _type::iterator it; \
466 for (it = name.begin() ; it != name.end() ; ++it) \
467 std::clog << so(name.get_name(it)) \
468 << name.get_descr(it) << "\n"; \
472 std::clog << "Usage: " << opt::program_name
473 << " [options] <platform_file> [<deployment_file>]\n";
474 std::clog << "Current values for the parameters are given between square brackets.\n";
476 std::clog << "\nGlobal options:\n";
478 << "print help and exit (use -hh or -hhh for extended help)\n";
479 if (opt::help_requested < 1)
482 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
483 std::clog << o("-V") << "print version and exit\n";
485 std::clog << "\nSimulator output:\n";
486 std::clog << o("-l value")
487 << "print current load every n lb iterations, 0 to disable"
488 << " [" << opt::log_rate << "]\n";
490 << "verbose: do not override the default logging parameters\n";
492 std::clog << "\nAutomatic deployment:\n";
493 std::clog << o("-T name")
494 << "enable automatic deployment with selected topology"
495 << " [" << opt::auto_depl::topology << "]\n";
496 if (opt::help_requested > 1)
497 so_list(opt::topologies);
498 std::clog << o("-L value")
499 << "total load with auto deployment, average if negative"
500 << " [" << opt::auto_depl::load << "]\n";
501 std::clog << o("-N value")
502 << "number of hosts to use with auto deployment, 0 for max."
503 << " [" << opt::auto_depl::nhosts << "]\n";
505 << "toggle random initial load distribution"
506 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
508 std::clog << o("-r value")
509 << "random seed for initial load distribution, or 0, or 1"
510 << " [" << opt::auto_depl::random_seed << "]\n";
511 if (opt::help_requested > 1)
512 std::clog << o("") << "- use 0 for a seed based on current date\n"
513 << o("") << "- use 1 for a constant random\n";
515 std::clog << "\nLoad balancing algorithm:\n";
516 std::clog << o("-a name") << "load balancing algorithm"
517 << " [" << opt::loba_algo << "]\n";
518 if (opt::help_requested > 1)
519 so_list(opt::loba_algorithms);
520 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
521 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
522 std::clog << o("-E") << "toggle egocentric mode when bookkeeping"
523 << " [" << opt_helper::on_off(opt::egocentric) << "]\n";
524 if (opt::help_requested > 1)
526 << "(a not so good idea introduced by git commit f5336c5)\n";
527 std::clog << o("-k value") << "divisor for algorithms *best*"
528 << " [" << opt::loba_best_divisor << "]\n";
530 std::clog << "\nLb. and comp. iterations:\n";
531 std::clog << o("-s value")
532 << "minimum duration between lb. iterations"
533 << " [" << opt::min_lb_iter_duration << "]\n";
534 std::clog << o("-S value")
535 << "minimum duration between comp. iterations"
536 << " [" << opt::min_comp_iter_duration << "]\n";
537 std::clog << o("-d value")
538 << "start comp. iterations after given number of lb. iter."
539 << " [" << opt::comp_iter_delay << "]\n";
540 std::clog << o("-D value")
541 << "start comp. iterations after given time"
542 << " [" << opt::comp_time_delay << "]\n";
544 std::clog << "\nComputations and communications:\n";
545 std::clog << o("-c [aN,...]a0")
546 << "polynomial factors for computation cost"
547 << " [" << opt::comp_cost.to_string() << "]\n";
548 std::clog << o("-C [aN,...]a0")
549 << "polynomial factors for communication cost"
550 << " [" << opt::comm_cost.to_string() << "]\n";
551 std::clog << o("-m value")
552 << "minimum data transfer amount"
553 << " [" << opt::min_transfer_amount << "]\n";
554 std::clog << o("-M value")
555 << "maximum data transfer amount, 0 for no limit"
556 << " [" << opt::max_transfer_amount << "]\n";
558 << "toggle transfer of integer data shares only"
559 << " [" << opt_helper::on_off(opt::integer_transfer) << "]\n";
561 std::clog << "\nEnd of the simulation:\n";
562 std::clog << o("-i value")
563 << "maximum number of lb. iterations, 0 for no limit"
564 << " [" << opt::lb_maxiter << "]\n";
565 std::clog << o("-I value")
566 << "maximum number of comp. iterations, 0 for no limit"
567 << " [" << opt::comp_maxiter << "]\n";
568 std::clog << o("-% value")
569 << "percent of the load average to assume the convergence"
570 << " [" << opt::avg_load_ratio << "]\n";
571 if (opt::help_requested > 1)
573 << "- if negative: use 100 * (nhosts / -value) / avg_load\n";
574 std::clog << o("-g value")
575 << "number of consecutive detections of convergence to exit"
576 << " [" << opt::exit_on_convergence << "]\n";
577 if (opt::help_requested > 1)
579 << "- use 0 to disable\n";
580 std::clog << o("-t value")
581 << "time limit (simulated time), 0 for no limit"
582 << " [" << opt::time_limit << "]\n";
583 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
584 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
586 if (opt::help_requested < 3)
589 std::clog << "\nHidden options:\n";
590 std::clog << o("-x value") << "value is an integer"
591 << " [" << opt::option_x << "]\n";
592 std::clog << o("-X value") << "value is a real number"
593 << " [" << opt::option_X << "]\n";
595 std::clog << "\nLogging support:\n"
596 << " See SimGrid documentation on:\n"
597 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
598 << " Existing categories are:\n"
599 << " simu : root of following categories\n"
600 << " main : messages from global infrastructure\n"
601 << " depl : messages from auto deployment (inherited from main)\n"
602 << " comm : messages from asynchronous pipes\n"
603 << " proc : messages from base process class\n"
604 << " loba : messages from load-balancer\n"
605 << " thrd : messages from thread wrapper class\n";
607 // std::clog << "\nMiscellaneous low-level parameters\n";