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_pstar.h"
22 #include "loba_simple.h"
27 #define DATA_DESCR_WIDTH 42
33 // A sum of loads if considered null if it is less than
34 // load_ratio_threshold percent of the sum of loads at init.
35 const double load_ratio_threshold = 1e-4;
38 std::string program_name;
39 int help_requested = 0;
40 bool version_requested = false;
41 int option_x = 0; // hidden option (int)
42 double option_X = 0.0; // hidden option (double)
44 // Simulation parameters
46 volatile std::sig_atomic_t exit_request = 0;
48 // Platform and deployment
49 std::string platform_file;
50 std::string deployment_file;
52 // Automatic deployment
55 std::string topology("clique");
58 bool random_distribution = false;
59 unsigned long random_seed = 0;
62 // Load balancing algorithm
63 std::string loba_algo("simple");
64 bool bookkeeping = false;
65 bool egocentric = false;
66 double min_lb_iter_duration = 1.0; // fixme: find better defaults
67 double min_transfer_amount = 0.0;
68 double max_transfer_amount = 0.0;
69 bool integer_transfer = false;
70 unsigned loba_best_divisor = 1;
72 // Application parameters
73 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
74 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
75 double min_comp_iter_duration = 1.0; // fixme: find better defaults
76 unsigned comp_iter_delay = 0; // fixme: find better defaults
77 double comp_time_delay = 0.0; // fixme: find better defaults
79 // Parameters for the end of the simulation
80 double avg_load_ratio = 0.0;
81 unsigned lb_maxiter = 0;
82 unsigned comp_maxiter = 0;
83 double time_limit = 0;
84 unsigned exit_on_convergence = 0;
85 bool exit_on_close = true;
87 // Named parameters lists
88 loba_algorithms_type loba_algorithms;
89 loba_algorithms_type::loba_algorithms_type()
91 NOL_INSERT("2besteffort", "balance with best effort strategy (take #2)",
93 NOL_INSERT("besteffort", "balance with best effort strategy",
95 NOL_INSERT("bulk", "a multi-load-units assignation rule without ordering...",
97 NOL_INSERT("fairstrategy", "balance with fair strategy",
99 NOL_INSERT("lln", "balance with less loaded neighbors without ordering-bulk method",
101 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
103 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
105 NOL_INSERT("makhoul3", "variation on Makhoul's algorithm",
107 NOL_INSERT("none", "no load-balancing (for testing only)",
109 NOL_INSERT("pstar", "balance with pstar strategy",
111 NOL_INSERT("simple", "balance with least loaded neighbor",
115 topologies_type topologies;
116 topologies_type::topologies_type()
118 NOL_INSERT("btree", "binary tree topology, initial load at root",
120 NOL_INSERT("clique", "all connected topology", deployment_clique);
121 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
122 NOL_INSERT("line", "line topology, initial load at one end",
124 NOL_INSERT("ring", "ring topology", deployment_ring);
125 NOL_INSERT("star", "star topology, initial load at center",
127 NOL_INSERT("torus", "torus topology", deployment_torus);
134 // local helper class
137 template <typename T>
138 static bool parse_arg(char opt, const char *arg, T& val);
139 static const char* on_off(bool b);
140 const char* descr(const char* str);
141 template <typename T>
142 const char* val_or_string(const T& val, const char* str,
144 template <typename T>
145 static bool nol_find_prefix(const T& nol, const char* descr,
149 std::string descr_str;
150 std::string val_or_string_str;
155 template <typename T>
156 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
158 std::istringstream str(arg);
159 bool result = (str >> val) && str.eof();
161 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
165 const char* opt_helper::on_off(bool b)
167 return b ? "on" : "off";
170 const char* opt_helper::descr(const char* str)
173 descr_str.push_back(' ');
174 descr_str.resize(DATA_DESCR_WIDTH, '.');
175 return descr_str.c_str();
178 template <typename T>
179 const char* opt_helper::val_or_string(const T& val, const char* str,
183 std::ostringstream oss;
185 val_or_string_str = oss.str();
187 val_or_string_str = str;
189 return val_or_string_str.c_str();
192 template <typename T>
193 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
196 bool result = nol.exists(name);
198 std::stack<std::string> candidates;
199 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
200 const std::string& fullname = nol.get_name(it);
201 if (fullname.compare(0, name.length(), name) == 0)
202 candidates.push(fullname);
204 switch (candidates.size()) {
206 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
209 name = candidates.top();
212 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
215 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
216 while (!candidates.empty()) {
217 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
226 bool opt::parse_args(int* argc, char* argv[])
230 opt::program_name = argv[0];
231 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
233 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
237 while ((c = getopt(*argc, argv,
239 "a:bc:C:d:D:eEg:hi:I:k:l:L:m:M:N:r:Rs:S:t:T:vVx:X:Z"))
243 PARSE_ARG(opt::avg_load_ratio);
246 opt::loba_algo = optarg;
247 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
248 "load balancing algorithm",
253 opt::bookkeeping = !opt::bookkeeping;
257 opt::comp_cost = cost_func(optarg);
259 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
266 opt::comm_cost = cost_func(optarg);
268 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
274 PARSE_ARG(opt::comp_iter_delay);
277 PARSE_ARG(opt::comp_time_delay);
280 opt::exit_on_close = !opt::exit_on_close;
283 opt::egocentric = !opt::egocentric;
286 PARSE_ARG(opt::exit_on_convergence);
289 opt::help_requested++;
292 PARSE_ARG(opt::lb_maxiter);
295 PARSE_ARG(opt::loba_best_divisor);
298 PARSE_ARG(opt::comp_maxiter);
301 PARSE_ARG(opt::log_rate);
304 PARSE_ARG(opt::auto_depl::load);
307 PARSE_ARG(opt::min_transfer_amount);
310 PARSE_ARG(opt::max_transfer_amount);
313 PARSE_ARG(opt::auto_depl::nhosts);
316 PARSE_ARG(opt::auto_depl::random_seed);
319 opt::auto_depl::random_distribution =
320 !opt::auto_depl::random_distribution;
323 PARSE_ARG(opt::min_lb_iter_duration);
326 PARSE_ARG(opt::min_comp_iter_duration);
329 PARSE_ARG(opt::time_limit);
332 opt::auto_depl::topology = optarg;
333 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
334 opt::auto_depl::topology)
338 // nothing to do: this option is checked at the very
339 // beginning of main()
342 opt::version_requested = true;
345 PARSE_ARG(opt::option_x);
346 XBT_WARN("option_x set to %d", opt::option_x);
349 PARSE_ARG(opt::option_X);
350 XBT_WARN("option_X set to %g", opt::option_X);
353 opt::integer_transfer = !opt::integer_transfer;
356 XBT_ERROR("invalid option -- '%c'", optopt);
364 if (opt::version_requested || opt::help_requested)
367 if (optind < *argc) {
368 opt::platform_file = argv[optind++];
370 XBT_ERROR("missing parameter -- <plaform_file>");
373 if (optind < *argc) {
374 opt::deployment_file = argv[optind++];
376 opt::auto_depl::enabled = opt::deployment_file.empty();
378 while (optind < *argc) {
379 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
383 if (opt::max_transfer_amount &&
384 opt::max_transfer_amount < opt::min_transfer_amount) {
385 XBT_ERROR("max. data transfer amount (%g) <"
386 " min. data transfer amount (%g)",
387 opt::max_transfer_amount, opt::min_transfer_amount);
391 if (opt::loba_best_divisor <= 0) {
392 XBT_ERROR("divisor for algorithms *best* (%u) should be positive",
393 opt::loba_best_divisor);
397 if (!opt::auto_depl::random_seed)
398 opt::auto_depl::random_seed = time(nullptr);
407 #define DESCR(description, format, value) \
408 XBT_INFO("| %s: " format, h.descr(description), value)
410 XBT_INFO(",----[ Simulation parameters ]");
411 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
412 DESCR("platform file", "\"%s\"", platform_file.c_str());
413 if (auto_depl::enabled) {
414 XBT_INFO("| automatic deployment enabled");
415 DESCR("- topology", "%s", auto_depl::topology.c_str());
416 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
418 if (auto_depl::load < 0.0)
419 DESCR("- initial load (average)", "%g", -auto_depl::load);
421 DESCR("- initial load (total)", "%g", auto_depl::load);
422 DESCR("- random initial load distribution", "%s",
423 h.on_off(auto_depl::random_distribution));
424 DESCR("- random seed", "%s", // NOTE: cannot be 0 here anymore
425 h.val_or_string(auto_depl::random_seed, "constant random", 1UL));
427 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
429 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
430 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
431 DESCR("egocentric mode", "%s", h.on_off(egocentric));
432 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
433 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
434 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
435 DESCR("maximum data transfer amount", "%s",
436 h.val_or_string(max_transfer_amount, "no limit"));
437 DESCR("only transfer integer data shares", "%s",
438 h.on_off(integer_transfer));
439 DESCR("divisor for algorithms *best*", "%u", loba_best_divisor);
440 DESCR("minimum duration between lb. iterations", "%g",
441 min_lb_iter_duration);
442 DESCR("minimum duration between comp. iterations", "%g",
443 min_comp_iter_duration);
444 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
445 DESCR("computations start after time", "%g", comp_time_delay);
446 DESCR("maximum number of lb. iterations", "%s",
447 h.val_or_string(lb_maxiter, "no limit"));
448 DESCR("maximum number of comp. iterations", "%s",
449 h.val_or_string(comp_maxiter, "no limit"));
450 DESCR("convergence is assumed within (\%)", "%g", opt::avg_load_ratio);
451 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
452 DESCR("exit on convergence", "%s",
453 h.val_or_string(exit_on_convergence, "disabled"));
454 DESCR("exit on close", "%s", h.on_off(exit_on_close));
463 #define o(opt) " " << std::setw(14) \
464 << std::left << (opt) << std::right << " "
466 #define so(subopt) std::setw(18) << (subopt) << " : "
468 #define so_list(name) do { \
469 name ## _type::iterator it; \
470 for (it = name.begin() ; it != name.end() ; ++it) \
471 std::clog << so(name.get_name(it)) \
472 << name.get_descr(it) << "\n"; \
476 std::clog << "Usage: " << opt::program_name
477 << " [options] <platform_file> [<deployment_file>]\n";
478 std::clog << "Current values for the parameters are given between square brackets.\n";
480 std::clog << "\nGlobal options:\n";
482 << "print help and exit (use -hh or -hhh for extended help)\n";
483 if (opt::help_requested < 1)
486 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
487 std::clog << o("-V") << "print version and exit\n";
489 std::clog << "\nSimulator output:\n";
490 std::clog << o("-l value")
491 << "print current load every n lb iterations, 0 to disable"
492 << " [" << opt::log_rate << "]\n";
494 << "verbose: do not override the default logging parameters\n";
496 std::clog << "\nAutomatic deployment:\n";
497 std::clog << o("-T name")
498 << "enable automatic deployment with selected topology"
499 << " [" << opt::auto_depl::topology << "]\n";
500 if (opt::help_requested > 1)
501 so_list(opt::topologies);
502 std::clog << o("-L value")
503 << "total load with auto deployment, average if negative"
504 << " [" << opt::auto_depl::load << "]\n";
505 std::clog << o("-N value")
506 << "number of hosts to use with auto deployment, 0 for max."
507 << " [" << opt::auto_depl::nhosts << "]\n";
509 << "toggle random initial load distribution"
510 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
512 std::clog << o("-r value")
513 << "random seed for initial load distribution, or 0, or 1"
514 << " [" << opt::auto_depl::random_seed << "]\n";
515 if (opt::help_requested > 1)
516 std::clog << o("") << "- use 0 for a seed based on current date\n"
517 << o("") << "- use 1 for a constant random\n";
519 std::clog << "\nLoad balancing algorithm:\n";
520 std::clog << o("-a name") << "load balancing algorithm"
521 << " [" << opt::loba_algo << "]\n";
522 if (opt::help_requested > 1)
523 so_list(opt::loba_algorithms);
524 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
525 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
526 std::clog << o("-E") << "toggle egocentric mode when bookkeeping"
527 << " [" << opt_helper::on_off(opt::egocentric) << "]\n";
528 if (opt::help_requested > 1)
530 << "(a not so good idea introduced by git commit f5336c5)\n";
531 std::clog << o("-k value") << "divisor for algorithms *best*"
532 << " [" << opt::loba_best_divisor << "]\n";
534 std::clog << "\nLb. and comp. iterations:\n";
535 std::clog << o("-s value")
536 << "minimum duration between lb. iterations"
537 << " [" << opt::min_lb_iter_duration << "]\n";
538 std::clog << o("-S value")
539 << "minimum duration between comp. iterations"
540 << " [" << opt::min_comp_iter_duration << "]\n";
541 std::clog << o("-d value")
542 << "start comp. iterations after given number of lb. iter."
543 << " [" << opt::comp_iter_delay << "]\n";
544 std::clog << o("-D value")
545 << "start comp. iterations after given time"
546 << " [" << opt::comp_time_delay << "]\n";
548 std::clog << "\nComputations and communications:\n";
549 std::clog << o("-c [aN,...]a0")
550 << "polynomial factors for computation cost"
551 << " [" << opt::comp_cost.to_string() << "]\n";
552 std::clog << o("-C [aN,...]a0")
553 << "polynomial factors for communication cost"
554 << " [" << opt::comm_cost.to_string() << "]\n";
555 std::clog << o("-m value")
556 << "minimum data transfer amount"
557 << " [" << opt::min_transfer_amount << "]\n";
558 std::clog << o("-M value")
559 << "maximum data transfer amount, 0 for no limit"
560 << " [" << opt::max_transfer_amount << "]\n";
562 << "toggle transfer of integer data shares only"
563 << " [" << opt_helper::on_off(opt::integer_transfer) << "]\n";
565 std::clog << "\nEnd of the simulation:\n";
566 std::clog << o("-i value")
567 << "maximum number of lb. iterations, 0 for no limit"
568 << " [" << opt::lb_maxiter << "]\n";
569 std::clog << o("-I value")
570 << "maximum number of comp. iterations, 0 for no limit"
571 << " [" << opt::comp_maxiter << "]\n";
572 std::clog << o("-% value")
573 << "percent of the load average to assume the convergence"
574 << " [" << opt::avg_load_ratio << "]\n";
575 if (opt::help_requested > 1)
577 << "- if negative: use 100 * (nhosts / -value) / avg_load\n";
578 std::clog << o("-g value")
579 << "number of consecutive detections of convergence to exit"
580 << " [" << opt::exit_on_convergence << "]\n";
581 if (opt::help_requested > 1)
583 << "- use 0 to disable\n";
584 std::clog << o("-t value")
585 << "time limit (simulated time), 0 for no limit"
586 << " [" << opt::time_limit << "]\n";
587 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
588 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
590 if (opt::help_requested < 3)
593 std::clog << "\nHidden options:\n";
594 std::clog << o("-x value") << "value is an integer"
595 << " [" << opt::option_x << "]\n";
596 std::clog << o("-X value") << "value is a real number"
597 << " [" << opt::option_X << "]\n";
599 std::clog << "\nLogging support:\n"
600 << " See SimGrid documentation on:\n"
601 << " http://simgrid.gforge.inria.fr/simgrid/latest/doc/outcomes_logs.html#log_user\n"
602 << " Existing categories are:\n"
603 << " simu : root of following categories\n"
604 << " main : messages from global infrastructure\n"
605 << " depl : messages from auto deployment (inherited from main)\n"
606 << " comm : messages from asynchronous pipes\n"
607 << " proc : messages from base process class\n"
608 << " loba : messages from load-balancer\n"
609 << " thrd : messages from thread wrapper class\n";
611 // std::clog << "\nMiscellaneous low-level parameters\n";