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 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
384 DESCR("- random initial load distribution", "%s",
385 h.on_off(auto_depl::random_distribution));
386 DESCR("- random seed", "%s",
387 h.val_or_string(auto_depl::random_seed, "time based"));
389 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
391 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
392 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
393 DESCR("minimum duration between lb. iterations", "%g",
394 min_lb_iter_duration);
395 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
396 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
397 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
398 DESCR("maximum data transfer amount", "%s",
399 h.val_or_string(max_transfer_amount, "no limit"));
400 DESCR("only transfer integer data shares", "%s",
401 h.on_off(integer_transfer));
402 DESCR("minimum duration between comp. iterations", "%g",
403 min_comp_iter_duration);
404 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
405 DESCR("computations start after time", "%g", comp_time_delay);
406 DESCR("maximum number of lb. iterations", "%s",
407 h.val_or_string(lb_maxiter, "no limit"));
408 DESCR("maximum number of comp. iterations", "%s",
409 h.val_or_string(comp_maxiter, "no limit"));
410 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
411 DESCR("exit on close", "%s", h.on_off(exit_on_close));
420 #define o(opt) " " << std::setw(14) \
421 << std::left << (opt) << std::right << " "
423 #define so(subopt) std::setw(18) << (subopt) << " : "
425 #define so_list(name) do { \
426 name ## _type::iterator it; \
427 for (it = name.begin() ; it != name.end() ; ++it) \
428 std::clog << so(name.get_name(it)) \
429 << name.get_descr(it) << "\n"; \
433 std::clog << "Usage: " << opt::program_name
434 << " [options] <platform_file> [<deployment_file>]\n";
435 std::clog << "Current values for the parameters are given between square brackets.\n";
437 std::clog << "\nGlobal options:\n";
439 << "print help and exit (use -hh or -hhh for extended help)\n";
440 if (opt::help_requested < 1)
443 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
444 std::clog << o("-V") << "print version and exit\n";
446 std::clog << "\nSimulator output:\n";
447 std::clog << o("-l value")
448 << "print current load every n lb iterations, 0 to disable"
449 << " [" << opt::log_rate << "]\n";
451 << "verbose: do not override the default logging parameters\n";
453 std::clog << "\nAutomatic deployment:\n";
454 std::clog << o("-T name")
455 << "enable automatic deployment with selected topology"
456 << " [" << opt::auto_depl::topology << "]\n";
457 if (opt::help_requested > 1)
458 so_list(opt::topologies);
459 std::clog << o("-L value")
460 << "total load with auto deployment, 0 for number of hosts"
461 << " [" << opt::auto_depl::load << "]\n";
462 std::clog << o("-N value")
463 << "number of hosts to use with auto deployment, 0 for max."
464 << " [" << opt::auto_depl::nhosts << "]\n";
466 << "toggle random initial load distribution"
467 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
469 std::clog << o("-r value")
470 << "random seed, 0 for a seed based on current date"
471 << " [" << opt::auto_depl::random_seed << "]\n";
473 std::clog << "\nLoad balancing algorithm:\n";
474 std::clog << o("-a name") << "load balancing algorithm"
475 << " [" << opt::loba_algo << "]\n";
476 if (opt::help_requested > 1)
477 so_list(opt::loba_algorithms);
478 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
479 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
481 std::clog << "\nLb. and comp. iterations:\n";
482 std::clog << o("-s value")
483 << "minimum duration between lb. iterations"
484 << " [" << opt::min_lb_iter_duration << "]\n";
485 std::clog << o("-S value")
486 << "minimum duration between comp. iterations"
487 << " [" << opt::min_comp_iter_duration << "]\n";
488 std::clog << o("-d value")
489 << "start comp. iterations after given number of lb. iter."
490 << " [" << opt::comp_iter_delay << "]\n";
491 std::clog << o("-D value")
492 << "start comp. iterations after given time"
493 << " [" << opt::comp_time_delay << "]\n";
495 std::clog << "\nComputations and communications:\n";
496 std::clog << o("-c [aN,...]a0")
497 << "polynomial factors for computation cost"
498 << " [" << opt::comp_cost.to_string() << "]\n";
499 std::clog << o("-C [aN,...]a0")
500 << "polynomial factors for communication cost"
501 << " [" << opt::comm_cost.to_string() << "]\n";
502 std::clog << o("-m value")
503 << "minimum data transfer amount"
504 << " [" << opt::min_transfer_amount << "]\n";
505 std::clog << o("-M value")
506 << "maximum data transfer amount, 0 for no limit"
507 << " [" << opt::max_transfer_amount << "]\n";
509 << "toggle transfer integer of integer data shares only"
510 << " [" << opt_helper::on_off(opt::integer_transfer) << "]\n";
512 std::clog << "\nEnd of the simulation:\n";
513 std::clog << o("-i value")
514 << "maximum number of lb. iterations, 0 for no limit"
515 << " [" << opt::lb_maxiter << "]\n";
516 std::clog << o("-I value")
517 << "maximum number of comp. iterations, 0 for no limit"
518 << " [" << opt::comp_maxiter << "]\n";
519 std::clog << o("-t value")
520 << "time limit (simulated time), 0 for no limit"
521 << " [" << opt::time_limit << "]\n";
522 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
523 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
525 if (opt::help_requested < 3)
528 std::clog << "\nLogging support:\n"
529 << " See SimGrid documentation on:\n"
530 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
531 << " Existing categories are:\n"
532 << " simu : root of following categories\n"
533 << " main : messages from global infrastructure\n"
534 << " depl : messages from auto deployment (inherited from main)\n"
535 << " comm : messages from asynchronous pipes\n"
536 << " proc : messages from base process class\n"
537 << " loba : messages from load-balancer\n"
538 << " thrd : messages from thread wrapper class\n";
540 // std::clog << "\nMiscellaneous low-level parameters\n";