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 double min_lb_iter_duration = 1.0; // fixme: find better defaults
63 double min_transfer_amount = 0.0;
64 double max_transfer_amount = 0.0;
65 bool integer_transfer = false;
67 // Application parameters
68 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
69 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
70 double min_comp_iter_duration = 1.0; // fixme: find better defaults
71 unsigned comp_iter_delay = 0; // fixme: find better defaults
72 double comp_time_delay = 0.0; // fixme: find better defaults
74 // Parameters for the end of the simulation
75 unsigned lb_maxiter = 0;
76 unsigned comp_maxiter = 0;
77 double time_limit = 0;
78 bool exit_on_close = true;
80 // Named parameters lists
81 loba_algorithms_type loba_algorithms;
82 loba_algorithms_type::loba_algorithms_type()
84 NOL_INSERT("2besteffort", "balance with best effort strategy (take #2)",
86 NOL_INSERT("besteffort", "balance with best effort strategy",
88 NOL_INSERT("bulk", "A multi-load-units assignation rule without ordering...",
90 NOL_INSERT("fairstrategy", "balance with fair strategy",
92 NOL_INSERT("lln", "Balance with less loaded neighbors without ordering-bulk method",
94 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
96 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
98 NOL_INSERT("none", "no load-balancing (for testing only)",
100 NOL_INSERT("simple", "balance with least loaded neighbor",
104 topologies_type topologies;
105 topologies_type::topologies_type()
107 NOL_INSERT("btree", "binary tree topology, initial load at root",
109 NOL_INSERT("clique", "all connected topology", deployment_clique);
110 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
111 NOL_INSERT("line", "line topology, initial load at one end",
113 NOL_INSERT("ring", "ring topology", deployment_ring);
114 NOL_INSERT("star", "star topology, initial load at center",
116 NOL_INSERT("torus", "torus topology", deployment_torus);
123 // local helper class
126 template <typename T>
127 static bool parse_arg(char opt, const char *arg, T& val);
128 static const char* on_off(bool b);
129 const char* descr(const char* str);
130 template <typename T>
131 const char* val_or_string(const T& val, const char* str,
133 template <typename T>
134 static bool nol_find_prefix(const T& nol, const char* descr,
138 std::string descr_str;
139 std::string val_or_string_str;
144 template <typename T>
145 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
147 std::istringstream str(arg);
148 bool result = (str >> val) && str.eof();
150 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
154 const char* opt_helper::on_off(bool b)
156 return b ? "on" : "off";
159 const char* opt_helper::descr(const char* str)
161 std::string& res = descr_str;
163 res.resize(DATA_DESCR_WIDTH, '.');
167 template <typename T>
168 const char* opt_helper::val_or_string(const T& val, const char* str,
171 std::string& res = val_or_string_str;
173 std::ostringstream oss;
182 template <typename T>
183 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
186 bool result = nol.exists(name);
188 std::stack<std::string> candidates;
189 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
190 const std::string& fullname = nol.get_name(it);
191 if (fullname.compare(0, name.length(), name) == 0)
192 candidates.push(fullname);
194 switch (candidates.size()) {
196 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
199 name = candidates.top();
202 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
205 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
206 while (!candidates.empty()) {
207 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
216 bool opt::parse_args(int* argc, char* argv[])
220 opt::program_name = argv[0];
221 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
223 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
227 while ((c = getopt(*argc, argv,
228 "a:bc:C:d:D:ehi:I:l:L:m:M:N:r:Rs:S:t:T:vVx:Z")) != -1) {
231 opt::loba_algo = optarg;
232 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
233 "load balancing algorithm",
238 opt::bookkeeping = !opt::bookkeeping;
241 opt::exit_on_close = !opt::exit_on_close;
244 opt::help_requested++;
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 PARSE_ARG(opt::lb_maxiter);
274 PARSE_ARG(opt::comp_maxiter);
277 PARSE_ARG(opt::log_rate);
280 PARSE_ARG(opt::auto_depl::load);
283 PARSE_ARG(opt::min_transfer_amount);
286 PARSE_ARG(opt::max_transfer_amount);
289 PARSE_ARG(opt::auto_depl::nhosts);
292 PARSE_ARG(opt::auto_depl::random_seed);
295 opt::auto_depl::random_distribution =
296 !opt::auto_depl::random_distribution;
299 PARSE_ARG(opt::min_lb_iter_duration);
302 PARSE_ARG(opt::min_comp_iter_duration);
305 PARSE_ARG(opt::time_limit);
308 opt::auto_depl::topology = optarg;
309 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
310 opt::auto_depl::topology)
314 // nothing to do: this option is checked at the very
315 // beginning of main()
318 opt::version_requested = true;
321 PARSE_ARG(opt::option_x);
322 XBT_WARN("option_x set to %d", opt::option_x);
325 opt::integer_transfer = !opt::integer_transfer;
328 XBT_ERROR("invalid option -- '%c'", optopt);
336 if (opt::version_requested || opt::help_requested)
339 if (optind < *argc) {
340 opt::platform_file = argv[optind++];
342 XBT_ERROR("missing parameter -- <plaform_file>");
345 if (optind < *argc) {
346 opt::deployment_file = argv[optind++];
348 opt::auto_depl::enabled = opt::deployment_file.empty();
350 while (optind < *argc) {
351 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
355 if (opt::max_transfer_amount &&
356 opt::max_transfer_amount < opt::min_transfer_amount) {
357 XBT_ERROR("max. data transfer amount < min. data transfer amount");
361 if (!opt::auto_depl::random_seed)
362 opt::auto_depl::random_seed = time(NULL);
371 #define DESCR(description, format, value) \
372 XBT_INFO("| %s: " format, h.descr(description), value)
374 XBT_INFO(",----[ Simulation parameters ]");
375 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
376 DESCR("platform file", "\"%s\"", platform_file.c_str());
377 if (auto_depl::enabled) {
378 XBT_INFO("| automatic deployment enabled");
379 DESCR("- topology", "%s", auto_depl::topology.c_str());
380 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
382 if (auto_depl::load < 0.0)
383 DESCR("- initial load (average)", "%g", -auto_depl::load);
385 DESCR("- initial load (total)", "%g", auto_depl::load);
386 DESCR("- random initial load distribution", "%s",
387 h.on_off(auto_depl::random_distribution));
388 DESCR("- random seed", "%s",
389 h.val_or_string(auto_depl::random_seed, "time based"));
391 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
393 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
394 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
395 DESCR("minimum duration between lb. iterations", "%g",
396 min_lb_iter_duration);
397 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
398 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
399 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
400 DESCR("maximum data transfer amount", "%s",
401 h.val_or_string(max_transfer_amount, "no limit"));
402 DESCR("only transfer integer data shares", "%s",
403 h.on_off(integer_transfer));
404 DESCR("minimum duration between comp. iterations", "%g",
405 min_comp_iter_duration);
406 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
407 DESCR("computations start after time", "%g", comp_time_delay);
408 DESCR("maximum number of lb. iterations", "%s",
409 h.val_or_string(lb_maxiter, "no limit"));
410 DESCR("maximum number of comp. iterations", "%s",
411 h.val_or_string(comp_maxiter, "no limit"));
412 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
413 DESCR("exit on close", "%s", h.on_off(exit_on_close));
422 #define o(opt) " " << std::setw(14) \
423 << std::left << (opt) << std::right << " "
425 #define so(subopt) std::setw(18) << (subopt) << " : "
427 #define so_list(name) do { \
428 name ## _type::iterator it; \
429 for (it = name.begin() ; it != name.end() ; ++it) \
430 std::clog << so(name.get_name(it)) \
431 << name.get_descr(it) << "\n"; \
435 std::clog << "Usage: " << opt::program_name
436 << " [options] <platform_file> [<deployment_file>]\n";
437 std::clog << "Current values for the parameters are given between square brackets.\n";
439 std::clog << "\nGlobal options:\n";
441 << "print help and exit (use -hh or -hhh for extended help)\n";
442 if (opt::help_requested < 1)
445 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
446 std::clog << o("-V") << "print version and exit\n";
448 std::clog << "\nSimulator output:\n";
449 std::clog << o("-l value")
450 << "print current load every n lb iterations, 0 to disable"
451 << " [" << opt::log_rate << "]\n";
453 << "verbose: do not override the default logging parameters\n";
455 std::clog << "\nAutomatic deployment:\n";
456 std::clog << o("-T name")
457 << "enable automatic deployment with selected topology"
458 << " [" << opt::auto_depl::topology << "]\n";
459 if (opt::help_requested > 1)
460 so_list(opt::topologies);
461 std::clog << o("-L value")
462 << "total load with auto deployment, average if negative"
463 << " [" << opt::auto_depl::load << "]\n";
464 std::clog << o("-N value")
465 << "number of hosts to use with auto deployment, 0 for max."
466 << " [" << opt::auto_depl::nhosts << "]\n";
468 << "toggle random initial load distribution"
469 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
471 std::clog << o("-r value")
472 << "random seed, 0 for a seed based on current date"
473 << " [" << opt::auto_depl::random_seed << "]\n";
475 std::clog << "\nLoad balancing algorithm:\n";
476 std::clog << o("-a name") << "load balancing algorithm"
477 << " [" << opt::loba_algo << "]\n";
478 if (opt::help_requested > 1)
479 so_list(opt::loba_algorithms);
480 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
481 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
483 std::clog << "\nLb. and comp. iterations:\n";
484 std::clog << o("-s value")
485 << "minimum duration between lb. iterations"
486 << " [" << opt::min_lb_iter_duration << "]\n";
487 std::clog << o("-S value")
488 << "minimum duration between comp. iterations"
489 << " [" << opt::min_comp_iter_duration << "]\n";
490 std::clog << o("-d value")
491 << "start comp. iterations after given number of lb. iter."
492 << " [" << opt::comp_iter_delay << "]\n";
493 std::clog << o("-D value")
494 << "start comp. iterations after given time"
495 << " [" << opt::comp_time_delay << "]\n";
497 std::clog << "\nComputations and communications:\n";
498 std::clog << o("-c [aN,...]a0")
499 << "polynomial factors for computation cost"
500 << " [" << opt::comp_cost.to_string() << "]\n";
501 std::clog << o("-C [aN,...]a0")
502 << "polynomial factors for communication cost"
503 << " [" << opt::comm_cost.to_string() << "]\n";
504 std::clog << o("-m value")
505 << "minimum data transfer amount"
506 << " [" << opt::min_transfer_amount << "]\n";
507 std::clog << o("-M value")
508 << "maximum data transfer amount, 0 for no limit"
509 << " [" << opt::max_transfer_amount << "]\n";
511 << "toggle transfer of integer data shares only"
512 << " [" << opt_helper::on_off(opt::integer_transfer) << "]\n";
514 std::clog << "\nEnd of the simulation:\n";
515 std::clog << o("-i value")
516 << "maximum number of lb. iterations, 0 for no limit"
517 << " [" << opt::lb_maxiter << "]\n";
518 std::clog << o("-I value")
519 << "maximum number of comp. iterations, 0 for no limit"
520 << " [" << opt::comp_maxiter << "]\n";
521 std::clog << o("-t value")
522 << "time limit (simulated time), 0 for no limit"
523 << " [" << opt::time_limit << "]\n";
524 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
525 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
527 if (opt::help_requested < 3)
530 std::clog << "\nLogging support:\n"
531 << " See SimGrid documentation on:\n"
532 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
533 << " Existing categories are:\n"
534 << " simu : root of following categories\n"
535 << " main : messages from global infrastructure\n"
536 << " depl : messages from auto deployment (inherited from main)\n"
537 << " comm : messages from asynchronous pipes\n"
538 << " proc : messages from base process class\n"
539 << " loba : messages from load-balancer\n"
540 << " thrd : messages from thread wrapper class\n";
542 // std::clog << "\nMiscellaneous low-level parameters\n";