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;
70 // Application parameters
71 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
72 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
73 double min_comp_iter_duration = 1.0; // fixme: find better defaults
74 unsigned comp_iter_delay = 0; // fixme: find better defaults
75 double comp_time_delay = 0.0; // fixme: find better defaults
77 // Parameters for the end of the simulation
78 unsigned lb_maxiter = 0;
79 unsigned comp_maxiter = 0;
80 double time_limit = 0;
81 bool exit_on_close = true;
83 // Named parameters lists
84 loba_algorithms_type loba_algorithms;
85 loba_algorithms_type::loba_algorithms_type()
87 NOL_INSERT("2besteffort", "balance with best effort strategy (take #2)",
89 NOL_INSERT("besteffort", "balance with best effort strategy",
91 NOL_INSERT("bulk", "a multi-load-units assignation rule without ordering...",
93 NOL_INSERT("fairstrategy", "balance with fair strategy",
95 NOL_INSERT("lln", "balance with less loaded neighbors without ordering-bulk method",
97 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
99 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
101 NOL_INSERT("makhoul3", "variation on Makhoul's algorithm",
103 NOL_INSERT("none", "no load-balancing (for testing only)",
105 NOL_INSERT("simple", "balance with least loaded neighbor",
109 topologies_type topologies;
110 topologies_type::topologies_type()
112 NOL_INSERT("btree", "binary tree topology, initial load at root",
114 NOL_INSERT("clique", "all connected topology", deployment_clique);
115 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
116 NOL_INSERT("line", "line topology, initial load at one end",
118 NOL_INSERT("ring", "ring topology", deployment_ring);
119 NOL_INSERT("star", "star topology, initial load at center",
121 NOL_INSERT("torus", "torus topology", deployment_torus);
128 // local helper class
131 template <typename T>
132 static bool parse_arg(char opt, const char *arg, T& val);
133 static const char* on_off(bool b);
134 const char* descr(const char* str);
135 template <typename T>
136 const char* val_or_string(const T& val, const char* str,
138 template <typename T>
139 static bool nol_find_prefix(const T& nol, const char* descr,
143 std::string descr_str;
144 std::string val_or_string_str;
149 template <typename T>
150 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
152 std::istringstream str(arg);
153 bool result = (str >> val) && str.eof();
155 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
159 const char* opt_helper::on_off(bool b)
161 return b ? "on" : "off";
164 const char* opt_helper::descr(const char* str)
166 std::string& res = descr_str;
168 res.resize(DATA_DESCR_WIDTH, '.');
172 template <typename T>
173 const char* opt_helper::val_or_string(const T& val, const char* str,
176 std::string& res = val_or_string_str;
178 std::ostringstream oss;
187 template <typename T>
188 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
191 bool result = nol.exists(name);
193 std::stack<std::string> candidates;
194 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
195 const std::string& fullname = nol.get_name(it);
196 if (fullname.compare(0, name.length(), name) == 0)
197 candidates.push(fullname);
199 switch (candidates.size()) {
201 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
204 name = candidates.top();
207 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
210 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
211 while (!candidates.empty()) {
212 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
221 bool opt::parse_args(int* argc, char* argv[])
225 opt::program_name = argv[0];
226 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
228 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
232 while ((c = getopt(*argc, argv,
233 "a:bc:C:d:D:eEhi:I:l:L:m:M:N:r:Rs:S:t:T:vVx:X:Z"))
237 opt::loba_algo = optarg;
238 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
239 "load balancing algorithm",
244 opt::bookkeeping = !opt::bookkeeping;
248 opt::comp_cost = cost_func(optarg);
250 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
257 opt::comm_cost = cost_func(optarg);
259 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
265 PARSE_ARG(opt::comp_iter_delay);
268 PARSE_ARG(opt::comp_time_delay);
271 opt::exit_on_close = !opt::exit_on_close;
274 opt::egocentric = !opt::egocentric;
277 opt::help_requested++;
280 PARSE_ARG(opt::lb_maxiter);
283 PARSE_ARG(opt::comp_maxiter);
286 PARSE_ARG(opt::log_rate);
289 PARSE_ARG(opt::auto_depl::load);
292 PARSE_ARG(opt::min_transfer_amount);
295 PARSE_ARG(opt::max_transfer_amount);
298 PARSE_ARG(opt::auto_depl::nhosts);
301 PARSE_ARG(opt::auto_depl::random_seed);
304 opt::auto_depl::random_distribution =
305 !opt::auto_depl::random_distribution;
308 PARSE_ARG(opt::min_lb_iter_duration);
311 PARSE_ARG(opt::min_comp_iter_duration);
314 PARSE_ARG(opt::time_limit);
317 opt::auto_depl::topology = optarg;
318 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
319 opt::auto_depl::topology)
323 // nothing to do: this option is checked at the very
324 // beginning of main()
327 opt::version_requested = true;
330 PARSE_ARG(opt::option_x);
331 XBT_WARN("option_x set to %d", opt::option_x);
334 PARSE_ARG(opt::option_X);
335 XBT_WARN("option_X set to %g", opt::option_X);
338 opt::integer_transfer = !opt::integer_transfer;
341 XBT_ERROR("invalid option -- '%c'", optopt);
349 if (opt::version_requested || opt::help_requested)
352 if (optind < *argc) {
353 opt::platform_file = argv[optind++];
355 XBT_ERROR("missing parameter -- <plaform_file>");
358 if (optind < *argc) {
359 opt::deployment_file = argv[optind++];
361 opt::auto_depl::enabled = opt::deployment_file.empty();
363 while (optind < *argc) {
364 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
368 if (opt::max_transfer_amount &&
369 opt::max_transfer_amount < opt::min_transfer_amount) {
370 XBT_ERROR("max. data transfer amount (%g) <"
371 " min. data transfer amount (%g)",
372 opt::max_transfer_amount, opt::min_transfer_amount);
376 if (!opt::auto_depl::random_seed)
377 opt::auto_depl::random_seed = time(NULL);
386 #define DESCR(description, format, value) \
387 XBT_INFO("| %s: " format, h.descr(description), value)
389 XBT_INFO(",----[ Simulation parameters ]");
390 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
391 DESCR("platform file", "\"%s\"", platform_file.c_str());
392 if (auto_depl::enabled) {
393 XBT_INFO("| automatic deployment enabled");
394 DESCR("- topology", "%s", auto_depl::topology.c_str());
395 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
397 if (auto_depl::load < 0.0)
398 DESCR("- initial load (average)", "%g", -auto_depl::load);
400 DESCR("- initial load (total)", "%g", auto_depl::load);
401 DESCR("- random initial load distribution", "%s",
402 h.on_off(auto_depl::random_distribution));
403 DESCR("- random seed", "%s", // NOTE: cannot be 0 here anymore
404 h.val_or_string(auto_depl::random_seed, "constant random", 1UL));
406 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
408 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
409 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
410 DESCR("egocentric mode", "%s", h.on_off(egocentric));
411 DESCR("minimum duration between lb. iterations", "%g",
412 min_lb_iter_duration);
413 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
414 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
415 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
416 DESCR("maximum data transfer amount", "%s",
417 h.val_or_string(max_transfer_amount, "no limit"));
418 DESCR("only transfer integer data shares", "%s",
419 h.on_off(integer_transfer));
420 DESCR("minimum duration between comp. iterations", "%g",
421 min_comp_iter_duration);
422 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
423 DESCR("computations start after time", "%g", comp_time_delay);
424 DESCR("maximum number of lb. iterations", "%s",
425 h.val_or_string(lb_maxiter, "no limit"));
426 DESCR("maximum number of comp. iterations", "%s",
427 h.val_or_string(comp_maxiter, "no limit"));
428 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
429 DESCR("exit on close", "%s", h.on_off(exit_on_close));
438 #define o(opt) " " << std::setw(14) \
439 << std::left << (opt) << std::right << " "
441 #define so(subopt) std::setw(18) << (subopt) << " : "
443 #define so_list(name) do { \
444 name ## _type::iterator it; \
445 for (it = name.begin() ; it != name.end() ; ++it) \
446 std::clog << so(name.get_name(it)) \
447 << name.get_descr(it) << "\n"; \
451 std::clog << "Usage: " << opt::program_name
452 << " [options] <platform_file> [<deployment_file>]\n";
453 std::clog << "Current values for the parameters are given between square brackets.\n";
455 std::clog << "\nGlobal options:\n";
457 << "print help and exit (use -hh or -hhh for extended help)\n";
458 if (opt::help_requested < 1)
461 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
462 std::clog << o("-V") << "print version and exit\n";
464 std::clog << "\nSimulator output:\n";
465 std::clog << o("-l value")
466 << "print current load every n lb iterations, 0 to disable"
467 << " [" << opt::log_rate << "]\n";
469 << "verbose: do not override the default logging parameters\n";
471 std::clog << "\nAutomatic deployment:\n";
472 std::clog << o("-T name")
473 << "enable automatic deployment with selected topology"
474 << " [" << opt::auto_depl::topology << "]\n";
475 if (opt::help_requested > 1)
476 so_list(opt::topologies);
477 std::clog << o("-L value")
478 << "total load with auto deployment, average if negative"
479 << " [" << opt::auto_depl::load << "]\n";
480 std::clog << o("-N value")
481 << "number of hosts to use with auto deployment, 0 for max."
482 << " [" << opt::auto_depl::nhosts << "]\n";
484 << "toggle random initial load distribution"
485 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
487 std::clog << o("-r value")
488 << "random seed for initial load distribution, or 0, or 1"
489 << " [" << opt::auto_depl::random_seed << "]\n";
490 if (opt::help_requested > 1)
491 std::clog << o("") << "- use 0 for a seed based on current date\n"
492 << o("") << "- use 1 for a constant random\n";
494 std::clog << "\nLoad balancing algorithm:\n";
495 std::clog << o("-a name") << "load balancing algorithm"
496 << " [" << opt::loba_algo << "]\n";
497 if (opt::help_requested > 1)
498 so_list(opt::loba_algorithms);
499 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
500 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
501 std::clog << o("-E") << "toggle egocentric mode when bookkeeping"
502 << " [" << opt_helper::on_off(opt::egocentric) << "]\n";
503 if (opt::help_requested > 1)
505 << "(a not so good idea introduced by git commit f5336c5)\n";
507 std::clog << "\nLb. and comp. iterations:\n";
508 std::clog << o("-s value")
509 << "minimum duration between lb. iterations"
510 << " [" << opt::min_lb_iter_duration << "]\n";
511 std::clog << o("-S value")
512 << "minimum duration between comp. iterations"
513 << " [" << opt::min_comp_iter_duration << "]\n";
514 std::clog << o("-d value")
515 << "start comp. iterations after given number of lb. iter."
516 << " [" << opt::comp_iter_delay << "]\n";
517 std::clog << o("-D value")
518 << "start comp. iterations after given time"
519 << " [" << opt::comp_time_delay << "]\n";
521 std::clog << "\nComputations and communications:\n";
522 std::clog << o("-c [aN,...]a0")
523 << "polynomial factors for computation cost"
524 << " [" << opt::comp_cost.to_string() << "]\n";
525 std::clog << o("-C [aN,...]a0")
526 << "polynomial factors for communication cost"
527 << " [" << opt::comm_cost.to_string() << "]\n";
528 std::clog << o("-m value")
529 << "minimum data transfer amount"
530 << " [" << opt::min_transfer_amount << "]\n";
531 std::clog << o("-M value")
532 << "maximum data transfer amount, 0 for no limit"
533 << " [" << opt::max_transfer_amount << "]\n";
535 << "toggle transfer of integer data shares only"
536 << " [" << opt_helper::on_off(opt::integer_transfer) << "]\n";
538 std::clog << "\nEnd of the simulation:\n";
539 std::clog << o("-i value")
540 << "maximum number of lb. iterations, 0 for no limit"
541 << " [" << opt::lb_maxiter << "]\n";
542 std::clog << o("-I value")
543 << "maximum number of comp. iterations, 0 for no limit"
544 << " [" << opt::comp_maxiter << "]\n";
545 std::clog << o("-t value")
546 << "time limit (simulated time), 0 for no limit"
547 << " [" << opt::time_limit << "]\n";
548 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
549 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
551 if (opt::help_requested < 3)
554 std::clog << "\nHidden options:\n";
555 std::clog << o("-x value") << "value is an integer"
556 << " [" << opt::option_x << "]\n";
557 std::clog << o("-X value") << "value is a real number"
558 << " [" << opt::option_X << "]\n";
560 std::clog << "\nLogging support:\n"
561 << " See SimGrid documentation on:\n"
562 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
563 << " Existing categories are:\n"
564 << " simu : root of following categories\n"
565 << " main : messages from global infrastructure\n"
566 << " depl : messages from auto deployment (inherited from main)\n"
567 << " comm : messages from asynchronous pipes\n"
568 << " proc : messages from base process class\n"
569 << " loba : messages from load-balancer\n"
570 << " thrd : messages from thread wrapper class\n";
572 // std::clog << "\nMiscellaneous low-level parameters\n";