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_simple.h"
25 #define DATA_DESCR_WIDTH 42
31 // A sum of loads if considered null if it is less than
32 // load_ratio_threshold percent of the sum of loads at init.
33 const double load_ratio_threshold = 1e-4;
36 std::string program_name;
37 int help_requested = 0;
38 bool version_requested = false;
39 int option_x = 0; // hidden option, for debugging purpose
41 // Simulation parameters
43 volatile std::sig_atomic_t exit_request = 0;
45 // Platform and deployment
46 std::string platform_file;
47 std::string deployment_file;
49 // Automatic deployment
52 std::string topology("clique");
55 bool random_distribution = false;
56 unsigned long random_seed = 0;
59 // Load balancing algorithm
60 std::string loba_algo("simple");
61 bool bookkeeping = false;
62 bool egocentric = false;
63 double min_lb_iter_duration = 1.0; // fixme: find better defaults
64 double min_transfer_amount = 0.0;
65 double max_transfer_amount = 0.0;
66 bool integer_transfer = false;
68 // Application parameters
69 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
70 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
71 double min_comp_iter_duration = 1.0; // fixme: find better defaults
72 unsigned comp_iter_delay = 0; // fixme: find better defaults
73 double comp_time_delay = 0.0; // fixme: find better defaults
75 // Parameters for the end of the simulation
76 unsigned lb_maxiter = 0;
77 unsigned comp_maxiter = 0;
78 double time_limit = 0;
79 bool exit_on_close = true;
81 // Named parameters lists
82 loba_algorithms_type loba_algorithms;
83 loba_algorithms_type::loba_algorithms_type()
85 NOL_INSERT("2besteffort", "balance with best effort strategy (take #2)",
87 NOL_INSERT("besteffort", "balance with best effort strategy",
89 NOL_INSERT("bulk", "a multi-load-units assignation rule without ordering...",
91 NOL_INSERT("fairstrategy", "balance with fair strategy",
93 NOL_INSERT("lln", "balance with less loaded neighbors without ordering-bulk method",
95 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
97 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
99 NOL_INSERT("none", "no load-balancing (for testing only)",
101 NOL_INSERT("simple", "balance with least loaded neighbor",
105 topologies_type topologies;
106 topologies_type::topologies_type()
108 NOL_INSERT("btree", "binary tree topology, initial load at root",
110 NOL_INSERT("clique", "all connected topology", deployment_clique);
111 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
112 NOL_INSERT("line", "line topology, initial load at one end",
114 NOL_INSERT("ring", "ring topology", deployment_ring);
115 NOL_INSERT("star", "star topology, initial load at center",
117 NOL_INSERT("torus", "torus topology", deployment_torus);
124 // local helper class
127 template <typename T>
128 static bool parse_arg(char opt, const char *arg, T& val);
129 static const char* on_off(bool b);
130 const char* descr(const char* str);
131 template <typename T>
132 const char* val_or_string(const T& val, const char* str,
134 template <typename T>
135 static bool nol_find_prefix(const T& nol, const char* descr,
139 std::string descr_str;
140 std::string val_or_string_str;
145 template <typename T>
146 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
148 std::istringstream str(arg);
149 bool result = (str >> val) && str.eof();
151 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
155 const char* opt_helper::on_off(bool b)
157 return b ? "on" : "off";
160 const char* opt_helper::descr(const char* str)
162 std::string& res = descr_str;
164 res.resize(DATA_DESCR_WIDTH, '.');
168 template <typename T>
169 const char* opt_helper::val_or_string(const T& val, const char* str,
172 std::string& res = val_or_string_str;
174 std::ostringstream oss;
183 template <typename T>
184 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
187 bool result = nol.exists(name);
189 std::stack<std::string> candidates;
190 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
191 const std::string& fullname = nol.get_name(it);
192 if (fullname.compare(0, name.length(), name) == 0)
193 candidates.push(fullname);
195 switch (candidates.size()) {
197 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
200 name = candidates.top();
203 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
206 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
207 while (!candidates.empty()) {
208 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
217 bool opt::parse_args(int* argc, char* argv[])
221 opt::program_name = argv[0];
222 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
224 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
228 while ((c = getopt(*argc, argv,
229 "a:bc:C:d:D:eEhi:I:l:L:m:M:N:r:Rs:S:t:T:vVx:Z")) != -1) {
232 opt::loba_algo = optarg;
233 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
234 "load balancing algorithm",
239 opt::bookkeeping = !opt::bookkeeping;
243 opt::comp_cost = cost_func(optarg);
245 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
252 opt::comm_cost = cost_func(optarg);
254 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
260 PARSE_ARG(opt::comp_iter_delay);
263 PARSE_ARG(opt::comp_time_delay);
266 opt::exit_on_close = !opt::exit_on_close;
269 opt::egocentric = !opt::egocentric;
272 opt::help_requested++;
275 PARSE_ARG(opt::lb_maxiter);
278 PARSE_ARG(opt::comp_maxiter);
281 PARSE_ARG(opt::log_rate);
284 PARSE_ARG(opt::auto_depl::load);
287 PARSE_ARG(opt::min_transfer_amount);
290 PARSE_ARG(opt::max_transfer_amount);
293 PARSE_ARG(opt::auto_depl::nhosts);
296 PARSE_ARG(opt::auto_depl::random_seed);
299 opt::auto_depl::random_distribution =
300 !opt::auto_depl::random_distribution;
303 PARSE_ARG(opt::min_lb_iter_duration);
306 PARSE_ARG(opt::min_comp_iter_duration);
309 PARSE_ARG(opt::time_limit);
312 opt::auto_depl::topology = optarg;
313 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
314 opt::auto_depl::topology)
318 // nothing to do: this option is checked at the very
319 // beginning of main()
322 opt::version_requested = true;
325 PARSE_ARG(opt::option_x);
326 XBT_WARN("option_x set to %d", opt::option_x);
329 opt::integer_transfer = !opt::integer_transfer;
332 XBT_ERROR("invalid option -- '%c'", optopt);
340 if (opt::version_requested || opt::help_requested)
343 if (optind < *argc) {
344 opt::platform_file = argv[optind++];
346 XBT_ERROR("missing parameter -- <plaform_file>");
349 if (optind < *argc) {
350 opt::deployment_file = argv[optind++];
352 opt::auto_depl::enabled = opt::deployment_file.empty();
354 while (optind < *argc) {
355 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
359 if (opt::max_transfer_amount &&
360 opt::max_transfer_amount < opt::min_transfer_amount) {
361 XBT_ERROR("max. data transfer amount < min. data transfer amount");
365 if (!opt::auto_depl::random_seed)
366 opt::auto_depl::random_seed = time(NULL);
375 #define DESCR(description, format, value) \
376 XBT_INFO("| %s: " format, h.descr(description), value)
378 XBT_INFO(",----[ Simulation parameters ]");
379 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
380 DESCR("platform file", "\"%s\"", platform_file.c_str());
381 if (auto_depl::enabled) {
382 XBT_INFO("| automatic deployment enabled");
383 DESCR("- topology", "%s", auto_depl::topology.c_str());
384 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
386 if (auto_depl::load < 0.0)
387 DESCR("- initial load (average)", "%g", -auto_depl::load);
389 DESCR("- initial load (total)", "%g", auto_depl::load);
390 DESCR("- random initial load distribution", "%s",
391 h.on_off(auto_depl::random_distribution));
392 DESCR("- random seed", "%s",
393 h.val_or_string(auto_depl::random_seed, "time based"));
395 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
397 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
398 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
399 DESCR("egocentric mode", "%s", h.on_off(egocentric));
400 DESCR("minimum duration between lb. iterations", "%g",
401 min_lb_iter_duration);
402 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
403 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
404 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
405 DESCR("maximum data transfer amount", "%s",
406 h.val_or_string(max_transfer_amount, "no limit"));
407 DESCR("only transfer integer data shares", "%s",
408 h.on_off(integer_transfer));
409 DESCR("minimum duration between comp. iterations", "%g",
410 min_comp_iter_duration);
411 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
412 DESCR("computations start after time", "%g", comp_time_delay);
413 DESCR("maximum number of lb. iterations", "%s",
414 h.val_or_string(lb_maxiter, "no limit"));
415 DESCR("maximum number of comp. iterations", "%s",
416 h.val_or_string(comp_maxiter, "no limit"));
417 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
418 DESCR("exit on close", "%s", h.on_off(exit_on_close));
427 #define o(opt) " " << std::setw(14) \
428 << std::left << (opt) << std::right << " "
430 #define so(subopt) std::setw(18) << (subopt) << " : "
432 #define so_list(name) do { \
433 name ## _type::iterator it; \
434 for (it = name.begin() ; it != name.end() ; ++it) \
435 std::clog << so(name.get_name(it)) \
436 << name.get_descr(it) << "\n"; \
440 std::clog << "Usage: " << opt::program_name
441 << " [options] <platform_file> [<deployment_file>]\n";
442 std::clog << "Current values for the parameters are given between square brackets.\n";
444 std::clog << "\nGlobal options:\n";
446 << "print help and exit (use -hh or -hhh for extended help)\n";
447 if (opt::help_requested < 1)
450 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
451 std::clog << o("-V") << "print version and exit\n";
453 std::clog << "\nSimulator output:\n";
454 std::clog << o("-l value")
455 << "print current load every n lb iterations, 0 to disable"
456 << " [" << opt::log_rate << "]\n";
458 << "verbose: do not override the default logging parameters\n";
460 std::clog << "\nAutomatic deployment:\n";
461 std::clog << o("-T name")
462 << "enable automatic deployment with selected topology"
463 << " [" << opt::auto_depl::topology << "]\n";
464 if (opt::help_requested > 1)
465 so_list(opt::topologies);
466 std::clog << o("-L value")
467 << "total load with auto deployment, average if negative"
468 << " [" << opt::auto_depl::load << "]\n";
469 std::clog << o("-N value")
470 << "number of hosts to use with auto deployment, 0 for max."
471 << " [" << opt::auto_depl::nhosts << "]\n";
473 << "toggle random initial load distribution"
474 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
476 std::clog << o("-r value")
477 << "random seed, 0 for a seed based on current date"
478 << " [" << opt::auto_depl::random_seed << "]\n";
480 std::clog << "\nLoad balancing algorithm:\n";
481 std::clog << o("-a name") << "load balancing algorithm"
482 << " [" << opt::loba_algo << "]\n";
483 if (opt::help_requested > 1)
484 so_list(opt::loba_algorithms);
485 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
486 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
487 std::clog << o("-E") << "toggle egocentric mode when bookkeeping"
488 << " [" << opt_helper::on_off(opt::egocentric) << "]\n";
489 if (opt::help_requested > 1)
491 << "(a not so good idea introduced by git commit f5336c5)\n";
493 std::clog << "\nLb. and comp. iterations:\n";
494 std::clog << o("-s value")
495 << "minimum duration between lb. iterations"
496 << " [" << opt::min_lb_iter_duration << "]\n";
497 std::clog << o("-S value")
498 << "minimum duration between comp. iterations"
499 << " [" << opt::min_comp_iter_duration << "]\n";
500 std::clog << o("-d value")
501 << "start comp. iterations after given number of lb. iter."
502 << " [" << opt::comp_iter_delay << "]\n";
503 std::clog << o("-D value")
504 << "start comp. iterations after given time"
505 << " [" << opt::comp_time_delay << "]\n";
507 std::clog << "\nComputations and communications:\n";
508 std::clog << o("-c [aN,...]a0")
509 << "polynomial factors for computation cost"
510 << " [" << opt::comp_cost.to_string() << "]\n";
511 std::clog << o("-C [aN,...]a0")
512 << "polynomial factors for communication cost"
513 << " [" << opt::comm_cost.to_string() << "]\n";
514 std::clog << o("-m value")
515 << "minimum data transfer amount"
516 << " [" << opt::min_transfer_amount << "]\n";
517 std::clog << o("-M value")
518 << "maximum data transfer amount, 0 for no limit"
519 << " [" << opt::max_transfer_amount << "]\n";
521 << "toggle transfer of integer data shares only"
522 << " [" << opt_helper::on_off(opt::integer_transfer) << "]\n";
524 std::clog << "\nEnd of the simulation:\n";
525 std::clog << o("-i value")
526 << "maximum number of lb. iterations, 0 for no limit"
527 << " [" << opt::lb_maxiter << "]\n";
528 std::clog << o("-I value")
529 << "maximum number of comp. iterations, 0 for no limit"
530 << " [" << opt::comp_maxiter << "]\n";
531 std::clog << o("-t value")
532 << "time limit (simulated time), 0 for no limit"
533 << " [" << opt::time_limit << "]\n";
534 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
535 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
537 if (opt::help_requested < 3)
540 std::clog << "\nLogging support:\n"
541 << " See SimGrid documentation on:\n"
542 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
543 << " Existing categories are:\n"
544 << " simu : root of following categories\n"
545 << " main : messages from global infrastructure\n"
546 << " depl : messages from auto deployment (inherited from main)\n"
547 << " comm : messages from asynchronous pipes\n"
548 << " proc : messages from base process class\n"
549 << " loba : messages from load-balancer\n"
550 << " thrd : messages from thread wrapper class\n";
552 // std::clog << "\nMiscellaneous low-level parameters\n";