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, for debugging purpose
42 // Simulation parameters
44 volatile std::sig_atomic_t exit_request = 0;
46 // Platform and deployment
47 std::string platform_file;
48 std::string deployment_file;
50 // Automatic deployment
53 std::string topology("clique");
56 bool random_distribution = false;
57 unsigned long random_seed = 0;
60 // Load balancing algorithm
61 std::string loba_algo("simple");
62 bool bookkeeping = false;
63 bool egocentric = false;
64 double min_lb_iter_duration = 1.0; // fixme: find better defaults
65 double min_transfer_amount = 0.0;
66 double max_transfer_amount = 0.0;
67 bool integer_transfer = false;
69 // Application parameters
70 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
71 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
72 double min_comp_iter_duration = 1.0; // fixme: find better defaults
73 unsigned comp_iter_delay = 0; // fixme: find better defaults
74 double comp_time_delay = 0.0; // fixme: find better defaults
76 // Parameters for the end of the simulation
77 unsigned lb_maxiter = 0;
78 unsigned comp_maxiter = 0;
79 double time_limit = 0;
80 bool exit_on_close = true;
82 // Named parameters lists
83 loba_algorithms_type loba_algorithms;
84 loba_algorithms_type::loba_algorithms_type()
86 NOL_INSERT("2besteffort", "balance with best effort strategy (take #2)",
88 NOL_INSERT("besteffort", "balance with best effort strategy",
90 NOL_INSERT("bulk", "a multi-load-units assignation rule without ordering...",
92 NOL_INSERT("fairstrategy", "balance with fair strategy",
94 NOL_INSERT("lln", "balance with less loaded neighbors without ordering-bulk method",
96 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
98 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
100 NOL_INSERT("makhoul3", "describe your algorithm here...",
102 NOL_INSERT("none", "no load-balancing (for testing only)",
104 NOL_INSERT("simple", "balance with least loaded neighbor",
108 topologies_type topologies;
109 topologies_type::topologies_type()
111 NOL_INSERT("btree", "binary tree topology, initial load at root",
113 NOL_INSERT("clique", "all connected topology", deployment_clique);
114 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
115 NOL_INSERT("line", "line topology, initial load at one end",
117 NOL_INSERT("ring", "ring topology", deployment_ring);
118 NOL_INSERT("star", "star topology, initial load at center",
120 NOL_INSERT("torus", "torus topology", deployment_torus);
127 // local helper class
130 template <typename T>
131 static bool parse_arg(char opt, const char *arg, T& val);
132 static const char* on_off(bool b);
133 const char* descr(const char* str);
134 template <typename T>
135 const char* val_or_string(const T& val, const char* str,
137 template <typename T>
138 static bool nol_find_prefix(const T& nol, const char* descr,
142 std::string descr_str;
143 std::string val_or_string_str;
148 template <typename T>
149 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
151 std::istringstream str(arg);
152 bool result = (str >> val) && str.eof();
154 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
158 const char* opt_helper::on_off(bool b)
160 return b ? "on" : "off";
163 const char* opt_helper::descr(const char* str)
165 std::string& res = descr_str;
167 res.resize(DATA_DESCR_WIDTH, '.');
171 template <typename T>
172 const char* opt_helper::val_or_string(const T& val, const char* str,
175 std::string& res = val_or_string_str;
177 std::ostringstream oss;
186 template <typename T>
187 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
190 bool result = nol.exists(name);
192 std::stack<std::string> candidates;
193 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
194 const std::string& fullname = nol.get_name(it);
195 if (fullname.compare(0, name.length(), name) == 0)
196 candidates.push(fullname);
198 switch (candidates.size()) {
200 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
203 name = candidates.top();
206 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
209 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
210 while (!candidates.empty()) {
211 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
220 bool opt::parse_args(int* argc, char* argv[])
224 opt::program_name = argv[0];
225 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
227 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
231 while ((c = getopt(*argc, argv,
232 "a:bc:C:d:D:eEhi:I:l:L:m:M:N:r:Rs:S:t:T:vVx:Z")) != -1) {
235 opt::loba_algo = optarg;
236 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
237 "load balancing algorithm",
242 opt::bookkeeping = !opt::bookkeeping;
246 opt::comp_cost = cost_func(optarg);
248 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
255 opt::comm_cost = cost_func(optarg);
257 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
263 PARSE_ARG(opt::comp_iter_delay);
266 PARSE_ARG(opt::comp_time_delay);
269 opt::exit_on_close = !opt::exit_on_close;
272 opt::egocentric = !opt::egocentric;
275 opt::help_requested++;
278 PARSE_ARG(opt::lb_maxiter);
281 PARSE_ARG(opt::comp_maxiter);
284 PARSE_ARG(opt::log_rate);
287 PARSE_ARG(opt::auto_depl::load);
290 PARSE_ARG(opt::min_transfer_amount);
293 PARSE_ARG(opt::max_transfer_amount);
296 PARSE_ARG(opt::auto_depl::nhosts);
299 PARSE_ARG(opt::auto_depl::random_seed);
302 opt::auto_depl::random_distribution =
303 !opt::auto_depl::random_distribution;
306 PARSE_ARG(opt::min_lb_iter_duration);
309 PARSE_ARG(opt::min_comp_iter_duration);
312 PARSE_ARG(opt::time_limit);
315 opt::auto_depl::topology = optarg;
316 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
317 opt::auto_depl::topology)
321 // nothing to do: this option is checked at the very
322 // beginning of main()
325 opt::version_requested = true;
328 PARSE_ARG(opt::option_x);
329 XBT_WARN("option_x set to %d", opt::option_x);
332 opt::integer_transfer = !opt::integer_transfer;
335 XBT_ERROR("invalid option -- '%c'", optopt);
343 if (opt::version_requested || opt::help_requested)
346 if (optind < *argc) {
347 opt::platform_file = argv[optind++];
349 XBT_ERROR("missing parameter -- <plaform_file>");
352 if (optind < *argc) {
353 opt::deployment_file = argv[optind++];
355 opt::auto_depl::enabled = opt::deployment_file.empty();
357 while (optind < *argc) {
358 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
362 if (opt::max_transfer_amount &&
363 opt::max_transfer_amount < opt::min_transfer_amount) {
364 XBT_ERROR("max. data transfer amount < min. data transfer amount");
368 if (!opt::auto_depl::random_seed)
369 opt::auto_depl::random_seed = time(NULL);
378 #define DESCR(description, format, value) \
379 XBT_INFO("| %s: " format, h.descr(description), value)
381 XBT_INFO(",----[ Simulation parameters ]");
382 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
383 DESCR("platform file", "\"%s\"", platform_file.c_str());
384 if (auto_depl::enabled) {
385 XBT_INFO("| automatic deployment enabled");
386 DESCR("- topology", "%s", auto_depl::topology.c_str());
387 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
389 if (auto_depl::load < 0.0)
390 DESCR("- initial load (average)", "%g", -auto_depl::load);
392 DESCR("- initial load (total)", "%g", auto_depl::load);
393 DESCR("- random initial load distribution", "%s",
394 h.on_off(auto_depl::random_distribution));
395 DESCR("- random seed", "%s",
396 h.val_or_string(auto_depl::random_seed, "time based"));
398 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
400 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
401 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
402 DESCR("egocentric mode", "%s", h.on_off(egocentric));
403 DESCR("minimum duration between lb. iterations", "%g",
404 min_lb_iter_duration);
405 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
406 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
407 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
408 DESCR("maximum data transfer amount", "%s",
409 h.val_or_string(max_transfer_amount, "no limit"));
410 DESCR("only transfer integer data shares", "%s",
411 h.on_off(integer_transfer));
412 DESCR("minimum duration between comp. iterations", "%g",
413 min_comp_iter_duration);
414 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
415 DESCR("computations start after time", "%g", comp_time_delay);
416 DESCR("maximum number of lb. iterations", "%s",
417 h.val_or_string(lb_maxiter, "no limit"));
418 DESCR("maximum number of comp. iterations", "%s",
419 h.val_or_string(comp_maxiter, "no limit"));
420 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
421 DESCR("exit on close", "%s", h.on_off(exit_on_close));
430 #define o(opt) " " << std::setw(14) \
431 << std::left << (opt) << std::right << " "
433 #define so(subopt) std::setw(18) << (subopt) << " : "
435 #define so_list(name) do { \
436 name ## _type::iterator it; \
437 for (it = name.begin() ; it != name.end() ; ++it) \
438 std::clog << so(name.get_name(it)) \
439 << name.get_descr(it) << "\n"; \
443 std::clog << "Usage: " << opt::program_name
444 << " [options] <platform_file> [<deployment_file>]\n";
445 std::clog << "Current values for the parameters are given between square brackets.\n";
447 std::clog << "\nGlobal options:\n";
449 << "print help and exit (use -hh or -hhh for extended help)\n";
450 if (opt::help_requested < 1)
453 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
454 std::clog << o("-V") << "print version and exit\n";
456 std::clog << "\nSimulator output:\n";
457 std::clog << o("-l value")
458 << "print current load every n lb iterations, 0 to disable"
459 << " [" << opt::log_rate << "]\n";
461 << "verbose: do not override the default logging parameters\n";
463 std::clog << "\nAutomatic deployment:\n";
464 std::clog << o("-T name")
465 << "enable automatic deployment with selected topology"
466 << " [" << opt::auto_depl::topology << "]\n";
467 if (opt::help_requested > 1)
468 so_list(opt::topologies);
469 std::clog << o("-L value")
470 << "total load with auto deployment, average if negative"
471 << " [" << opt::auto_depl::load << "]\n";
472 std::clog << o("-N value")
473 << "number of hosts to use with auto deployment, 0 for max."
474 << " [" << opt::auto_depl::nhosts << "]\n";
476 << "toggle random initial load distribution"
477 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
479 std::clog << o("-r value")
480 << "random seed, 0 for a seed based on current date"
481 << " [" << opt::auto_depl::random_seed << "]\n";
483 std::clog << "\nLoad balancing algorithm:\n";
484 std::clog << o("-a name") << "load balancing algorithm"
485 << " [" << opt::loba_algo << "]\n";
486 if (opt::help_requested > 1)
487 so_list(opt::loba_algorithms);
488 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
489 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
490 std::clog << o("-E") << "toggle egocentric mode when bookkeeping"
491 << " [" << opt_helper::on_off(opt::egocentric) << "]\n";
492 if (opt::help_requested > 1)
494 << "(a not so good idea introduced by git commit f5336c5)\n";
496 std::clog << "\nLb. and comp. iterations:\n";
497 std::clog << o("-s value")
498 << "minimum duration between lb. iterations"
499 << " [" << opt::min_lb_iter_duration << "]\n";
500 std::clog << o("-S value")
501 << "minimum duration between comp. iterations"
502 << " [" << opt::min_comp_iter_duration << "]\n";
503 std::clog << o("-d value")
504 << "start comp. iterations after given number of lb. iter."
505 << " [" << opt::comp_iter_delay << "]\n";
506 std::clog << o("-D value")
507 << "start comp. iterations after given time"
508 << " [" << opt::comp_time_delay << "]\n";
510 std::clog << "\nComputations and communications:\n";
511 std::clog << o("-c [aN,...]a0")
512 << "polynomial factors for computation cost"
513 << " [" << opt::comp_cost.to_string() << "]\n";
514 std::clog << o("-C [aN,...]a0")
515 << "polynomial factors for communication cost"
516 << " [" << opt::comm_cost.to_string() << "]\n";
517 std::clog << o("-m value")
518 << "minimum data transfer amount"
519 << " [" << opt::min_transfer_amount << "]\n";
520 std::clog << o("-M value")
521 << "maximum data transfer amount, 0 for no limit"
522 << " [" << opt::max_transfer_amount << "]\n";
524 << "toggle transfer of integer data shares only"
525 << " [" << opt_helper::on_off(opt::integer_transfer) << "]\n";
527 std::clog << "\nEnd of the simulation:\n";
528 std::clog << o("-i value")
529 << "maximum number of lb. iterations, 0 for no limit"
530 << " [" << opt::lb_maxiter << "]\n";
531 std::clog << o("-I value")
532 << "maximum number of comp. iterations, 0 for no limit"
533 << " [" << opt::comp_maxiter << "]\n";
534 std::clog << o("-t value")
535 << "time limit (simulated time), 0 for no limit"
536 << " [" << opt::time_limit << "]\n";
537 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
538 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
540 if (opt::help_requested < 3)
543 std::clog << "\nLogging support:\n"
544 << " See SimGrid documentation on:\n"
545 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
546 << " Existing categories are:\n"
547 << " simu : root of following categories\n"
548 << " main : messages from global infrastructure\n"
549 << " depl : messages from auto deployment (inherited from main)\n"
550 << " comm : messages from asynchronous pipes\n"
551 << " proc : messages from base process class\n"
552 << " loba : messages from load-balancer\n"
553 << " thrd : messages from thread wrapper class\n";
555 // std::clog << "\nMiscellaneous low-level parameters\n";