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 bool exit_on_close = true;
85 // Named parameters lists
86 loba_algorithms_type loba_algorithms;
87 loba_algorithms_type::loba_algorithms_type()
89 NOL_INSERT("2besteffort", "balance with best effort strategy (take #2)",
91 NOL_INSERT("besteffort", "balance with best effort strategy",
93 NOL_INSERT("bulk", "a multi-load-units assignation rule without ordering...",
95 NOL_INSERT("fairstrategy", "balance with fair strategy",
97 NOL_INSERT("lln", "balance with less loaded neighbors without ordering-bulk method",
99 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
101 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
103 NOL_INSERT("makhoul3", "variation on Makhoul's algorithm",
105 NOL_INSERT("none", "no load-balancing (for testing only)",
107 NOL_INSERT("simple", "balance with least loaded neighbor",
111 topologies_type topologies;
112 topologies_type::topologies_type()
114 NOL_INSERT("btree", "binary tree topology, initial load at root",
116 NOL_INSERT("clique", "all connected topology", deployment_clique);
117 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
118 NOL_INSERT("line", "line topology, initial load at one end",
120 NOL_INSERT("ring", "ring topology", deployment_ring);
121 NOL_INSERT("star", "star topology, initial load at center",
123 NOL_INSERT("torus", "torus topology", deployment_torus);
130 // local helper class
133 template <typename T>
134 static bool parse_arg(char opt, const char *arg, T& val);
135 static const char* on_off(bool b);
136 const char* descr(const char* str);
137 template <typename T>
138 const char* val_or_string(const T& val, const char* str,
140 template <typename T>
141 static bool nol_find_prefix(const T& nol, const char* descr,
145 std::string descr_str;
146 std::string val_or_string_str;
151 template <typename T>
152 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
154 std::istringstream str(arg);
155 bool result = (str >> val) && str.eof();
157 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
161 const char* opt_helper::on_off(bool b)
163 return b ? "on" : "off";
166 const char* opt_helper::descr(const char* str)
168 std::string& res = descr_str;
170 res.resize(DATA_DESCR_WIDTH, '.');
174 template <typename T>
175 const char* opt_helper::val_or_string(const T& val, const char* str,
178 std::string& res = val_or_string_str;
180 std::ostringstream oss;
189 template <typename T>
190 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
193 bool result = nol.exists(name);
195 std::stack<std::string> candidates;
196 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
197 const std::string& fullname = nol.get_name(it);
198 if (fullname.compare(0, name.length(), name) == 0)
199 candidates.push(fullname);
201 switch (candidates.size()) {
203 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
206 name = candidates.top();
209 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
212 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
213 while (!candidates.empty()) {
214 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
223 bool opt::parse_args(int* argc, char* argv[])
227 opt::program_name = argv[0];
228 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
230 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
234 while ((c = getopt(*argc, argv,
236 "a:bc:C:d:D:eEhi:I:k:l:L:m:M:N:r:Rs:S:t:T:vVx:X:Z"))
240 PARSE_ARG(opt::avg_load_ratio);
243 opt::loba_algo = optarg;
244 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
245 "load balancing algorithm",
250 opt::bookkeeping = !opt::bookkeeping;
254 opt::comp_cost = cost_func(optarg);
256 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
263 opt::comm_cost = cost_func(optarg);
265 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
271 PARSE_ARG(opt::comp_iter_delay);
274 PARSE_ARG(opt::comp_time_delay);
277 opt::exit_on_close = !opt::exit_on_close;
280 opt::egocentric = !opt::egocentric;
283 opt::help_requested++;
286 PARSE_ARG(opt::lb_maxiter);
289 PARSE_ARG(opt::loba_best_divisor);
292 PARSE_ARG(opt::comp_maxiter);
295 PARSE_ARG(opt::log_rate);
298 PARSE_ARG(opt::auto_depl::load);
301 PARSE_ARG(opt::min_transfer_amount);
304 PARSE_ARG(opt::max_transfer_amount);
307 PARSE_ARG(opt::auto_depl::nhosts);
310 PARSE_ARG(opt::auto_depl::random_seed);
313 opt::auto_depl::random_distribution =
314 !opt::auto_depl::random_distribution;
317 PARSE_ARG(opt::min_lb_iter_duration);
320 PARSE_ARG(opt::min_comp_iter_duration);
323 PARSE_ARG(opt::time_limit);
326 opt::auto_depl::topology = optarg;
327 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
328 opt::auto_depl::topology)
332 // nothing to do: this option is checked at the very
333 // beginning of main()
336 opt::version_requested = true;
339 PARSE_ARG(opt::option_x);
340 XBT_WARN("option_x set to %d", opt::option_x);
343 PARSE_ARG(opt::option_X);
344 XBT_WARN("option_X set to %g", opt::option_X);
347 opt::integer_transfer = !opt::integer_transfer;
350 XBT_ERROR("invalid option -- '%c'", optopt);
358 if (opt::option_x) { // FIXME: remove this one day...
359 opt::loba_best_divisor = opt::option_x;
360 XBT_WARN("divisor for algorithms *best* set from option -x (%d => %u),"
362 opt::option_x, opt::loba_best_divisor);
365 if (opt::version_requested || opt::help_requested)
368 if (optind < *argc) {
369 opt::platform_file = argv[optind++];
371 XBT_ERROR("missing parameter -- <plaform_file>");
374 if (optind < *argc) {
375 opt::deployment_file = argv[optind++];
377 opt::auto_depl::enabled = opt::deployment_file.empty();
379 while (optind < *argc) {
380 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
384 if (opt::max_transfer_amount &&
385 opt::max_transfer_amount < opt::min_transfer_amount) {
386 XBT_ERROR("max. data transfer amount (%g) <"
387 " min. data transfer amount (%g)",
388 opt::max_transfer_amount, opt::min_transfer_amount);
392 if (opt::loba_best_divisor <= 0) {
393 XBT_ERROR("divisor for algorithms *best* (%u) should be positive",
394 opt::loba_best_divisor);
398 if (!opt::auto_depl::random_seed)
399 opt::auto_depl::random_seed = time(NULL);
408 #define DESCR(description, format, value) \
409 XBT_INFO("| %s: " format, h.descr(description), value)
411 XBT_INFO(",----[ Simulation parameters ]");
412 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
413 DESCR("platform file", "\"%s\"", platform_file.c_str());
414 if (auto_depl::enabled) {
415 XBT_INFO("| automatic deployment enabled");
416 DESCR("- topology", "%s", auto_depl::topology.c_str());
417 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
419 if (auto_depl::load < 0.0)
420 DESCR("- initial load (average)", "%g", -auto_depl::load);
422 DESCR("- initial load (total)", "%g", auto_depl::load);
423 DESCR("- random initial load distribution", "%s",
424 h.on_off(auto_depl::random_distribution));
425 DESCR("- random seed", "%s", // NOTE: cannot be 0 here anymore
426 h.val_or_string(auto_depl::random_seed, "constant random", 1UL));
428 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
430 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
431 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
432 DESCR("egocentric mode", "%s", h.on_off(egocentric));
433 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
434 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
435 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
436 DESCR("maximum data transfer amount", "%s",
437 h.val_or_string(max_transfer_amount, "no limit"));
438 DESCR("only transfer integer data shares", "%s",
439 h.on_off(integer_transfer));
440 DESCR("divisor for algorithms *best*", "%u", loba_best_divisor);
441 DESCR("minimum duration between lb. iterations", "%g",
442 min_lb_iter_duration);
443 DESCR("minimum duration between comp. iterations", "%g",
444 min_comp_iter_duration);
445 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
446 DESCR("computations start after time", "%g", comp_time_delay);
447 DESCR("maximum number of lb. iterations", "%s",
448 h.val_or_string(lb_maxiter, "no limit"));
449 DESCR("maximum number of comp. iterations", "%s",
450 h.val_or_string(comp_maxiter, "no limit"));
451 DESCR("convergence is assumed within (\%)", "%g", opt::avg_load_ratio);
452 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
453 DESCR("exit on close", "%s", h.on_off(exit_on_close));
462 #define o(opt) " " << std::setw(14) \
463 << std::left << (opt) << std::right << " "
465 #define so(subopt) std::setw(18) << (subopt) << " : "
467 #define so_list(name) do { \
468 name ## _type::iterator it; \
469 for (it = name.begin() ; it != name.end() ; ++it) \
470 std::clog << so(name.get_name(it)) \
471 << name.get_descr(it) << "\n"; \
475 std::clog << "Usage: " << opt::program_name
476 << " [options] <platform_file> [<deployment_file>]\n";
477 std::clog << "Current values for the parameters are given between square brackets.\n";
479 std::clog << "\nGlobal options:\n";
481 << "print help and exit (use -hh or -hhh for extended help)\n";
482 if (opt::help_requested < 1)
485 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
486 std::clog << o("-V") << "print version and exit\n";
488 std::clog << "\nSimulator output:\n";
489 std::clog << o("-l value")
490 << "print current load every n lb iterations, 0 to disable"
491 << " [" << opt::log_rate << "]\n";
493 << "verbose: do not override the default logging parameters\n";
495 std::clog << "\nAutomatic deployment:\n";
496 std::clog << o("-T name")
497 << "enable automatic deployment with selected topology"
498 << " [" << opt::auto_depl::topology << "]\n";
499 if (opt::help_requested > 1)
500 so_list(opt::topologies);
501 std::clog << o("-L value")
502 << "total load with auto deployment, average if negative"
503 << " [" << opt::auto_depl::load << "]\n";
504 std::clog << o("-N value")
505 << "number of hosts to use with auto deployment, 0 for max."
506 << " [" << opt::auto_depl::nhosts << "]\n";
508 << "toggle random initial load distribution"
509 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
511 std::clog << o("-r value")
512 << "random seed for initial load distribution, or 0, or 1"
513 << " [" << opt::auto_depl::random_seed << "]\n";
514 if (opt::help_requested > 1)
515 std::clog << o("") << "- use 0 for a seed based on current date\n"
516 << o("") << "- use 1 for a constant random\n";
518 std::clog << "\nLoad balancing algorithm:\n";
519 std::clog << o("-a name") << "load balancing algorithm"
520 << " [" << opt::loba_algo << "]\n";
521 if (opt::help_requested > 1)
522 so_list(opt::loba_algorithms);
523 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
524 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
525 std::clog << o("-E") << "toggle egocentric mode when bookkeeping"
526 << " [" << opt_helper::on_off(opt::egocentric) << "]\n";
527 if (opt::help_requested > 1)
529 << "(a not so good idea introduced by git commit f5336c5)\n";
530 std::clog << o("-k value") << "divisor for algorithms *best*"
531 << " [" << opt::loba_best_divisor << "]\n";
533 std::clog << "\nLb. and comp. iterations:\n";
534 std::clog << o("-s value")
535 << "minimum duration between lb. iterations"
536 << " [" << opt::min_lb_iter_duration << "]\n";
537 std::clog << o("-S value")
538 << "minimum duration between comp. iterations"
539 << " [" << opt::min_comp_iter_duration << "]\n";
540 std::clog << o("-d value")
541 << "start comp. iterations after given number of lb. iter."
542 << " [" << opt::comp_iter_delay << "]\n";
543 std::clog << o("-D value")
544 << "start comp. iterations after given time"
545 << " [" << opt::comp_time_delay << "]\n";
547 std::clog << "\nComputations and communications:\n";
548 std::clog << o("-c [aN,...]a0")
549 << "polynomial factors for computation cost"
550 << " [" << opt::comp_cost.to_string() << "]\n";
551 std::clog << o("-C [aN,...]a0")
552 << "polynomial factors for communication cost"
553 << " [" << opt::comm_cost.to_string() << "]\n";
554 std::clog << o("-m value")
555 << "minimum data transfer amount"
556 << " [" << opt::min_transfer_amount << "]\n";
557 std::clog << o("-M value")
558 << "maximum data transfer amount, 0 for no limit"
559 << " [" << opt::max_transfer_amount << "]\n";
561 << "toggle transfer of integer data shares only"
562 << " [" << opt_helper::on_off(opt::integer_transfer) << "]\n";
564 std::clog << "\nEnd of the simulation:\n";
565 std::clog << o("-i value")
566 << "maximum number of lb. iterations, 0 for no limit"
567 << " [" << opt::lb_maxiter << "]\n";
568 std::clog << o("-I value")
569 << "maximum number of comp. iterations, 0 for no limit"
570 << " [" << opt::comp_maxiter << "]\n";
571 std::clog << o("-% value")
572 << "percent of the load average to assume the convergence"
573 << " [" << opt::avg_load_ratio << "]\n";
574 std::clog << o("-t value")
575 << "time limit (simulated time), 0 for no limit"
576 << " [" << opt::time_limit << "]\n";
577 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
578 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
580 if (opt::help_requested < 3)
583 std::clog << "\nHidden options:\n";
584 std::clog << o("-x value") << "value is an integer"
585 << " [" << opt::option_x << "]\n";
586 std::clog << o("-X value") << "value is a real number"
587 << " [" << opt::option_X << "]\n";
589 std::clog << "\nLogging support:\n"
590 << " See SimGrid documentation on:\n"
591 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
592 << " Existing categories are:\n"
593 << " simu : root of following categories\n"
594 << " main : messages from global infrastructure\n"
595 << " depl : messages from auto deployment (inherited from main)\n"
596 << " comm : messages from asynchronous pipes\n"
597 << " proc : messages from base process class\n"
598 << " loba : messages from load-balancer\n"
599 << " thrd : messages from thread wrapper class\n";
601 // std::clog << "\nMiscellaneous low-level parameters\n";