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;
66 // Application parameters
67 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
68 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
69 double min_comp_iter_duration = 1.0; // fixme: find better defaults
70 unsigned comp_iter_delay = 0; // fixme: find better defaults
71 double comp_time_delay = 0.0; // fixme: find better defaults
73 // Parameters for the end of the simulation
74 unsigned lb_maxiter = 0;
75 unsigned comp_maxiter = 0;
76 double time_limit = 0;
77 bool exit_on_close = true;
79 // Named parameters lists
80 loba_algorithms_type loba_algorithms;
81 loba_algorithms_type::loba_algorithms_type()
83 NOL_INSERT("2besteffort", "balance with best effort strategy (take #2)",
85 NOL_INSERT("besteffort", "balance with best effort strategy",
87 NOL_INSERT("bulk", "A multi-load-units assignation rule without ordering...",
89 NOL_INSERT("fairstrategy", "balance with fair strategy",
91 NOL_INSERT("lln", "Balance with less loaded neighbors without ordering-bulk method",
93 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
95 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
97 NOL_INSERT("none", "no load-balancing (for testing only)",
99 NOL_INSERT("simple", "balance with least loaded neighbor",
103 topologies_type topologies;
104 topologies_type::topologies_type()
106 NOL_INSERT("btree", "binary tree topology, initial load at root",
108 NOL_INSERT("clique", "all connected topology", deployment_clique);
109 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
110 NOL_INSERT("line", "line topology, initial load at one end",
112 NOL_INSERT("ring", "ring topology", deployment_ring);
113 NOL_INSERT("star", "star topology, initial load at center",
115 NOL_INSERT("torus", "torus topology", deployment_torus);
122 // local helper class
125 template <typename T>
126 static bool parse_arg(char opt, const char *arg, T& val);
127 static const char* on_off(bool b);
128 const char* descr(const char* str);
129 template <typename T>
130 const char* val_or_string(const T& val, const char* str,
132 template <typename T>
133 static bool nol_find_prefix(const T& nol, const char* descr,
137 std::string descr_str;
138 std::string val_or_string_str;
143 template <typename T>
144 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
146 std::istringstream str(arg);
147 bool result = (str >> val) && str.eof();
149 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
153 const char* opt_helper::on_off(bool b)
155 return b ? "on" : "off";
158 const char* opt_helper::descr(const char* str)
160 std::string& res = descr_str;
162 res.resize(DATA_DESCR_WIDTH, '.');
166 template <typename T>
167 const char* opt_helper::val_or_string(const T& val, const char* str,
170 std::string& res = val_or_string_str;
172 std::ostringstream oss;
181 template <typename T>
182 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
185 bool result = nol.exists(name);
187 std::stack<std::string> candidates;
188 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
189 const std::string& fullname = nol.get_name(it);
190 if (fullname.compare(0, name.length(), name) == 0)
191 candidates.push(fullname);
193 switch (candidates.size()) {
195 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
198 name = candidates.top();
201 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
204 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
205 while (!candidates.empty()) {
206 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
215 bool opt::parse_args(int* argc, char* argv[])
219 opt::program_name = argv[0];
220 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
222 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
226 while ((c = getopt(*argc, argv,
227 "a:bc:C:d:D:ehi:I:l:L:m:M:N:r:Rs:S:t:T:vVx:")) != -1) {
230 opt::loba_algo = optarg;
231 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
232 "load balancing algorithm",
237 opt::bookkeeping = !opt::bookkeeping;
240 opt::exit_on_close = !opt::exit_on_close;
243 opt::help_requested++;
247 opt::comp_cost = cost_func(optarg);
249 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
256 opt::comm_cost = cost_func(optarg);
258 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
264 PARSE_ARG(opt::comp_iter_delay);
267 PARSE_ARG(opt::comp_time_delay);
270 PARSE_ARG(opt::lb_maxiter);
273 PARSE_ARG(opt::comp_maxiter);
276 PARSE_ARG(opt::log_rate);
279 PARSE_ARG(opt::auto_depl::load);
282 PARSE_ARG(opt::min_transfer_amount);
285 PARSE_ARG(opt::max_transfer_amount);
288 PARSE_ARG(opt::auto_depl::nhosts);
291 PARSE_ARG(opt::auto_depl::random_seed);
294 opt::auto_depl::random_distribution =
295 !opt::auto_depl::random_distribution;
298 PARSE_ARG(opt::min_lb_iter_duration);
301 PARSE_ARG(opt::min_comp_iter_duration);
304 PARSE_ARG(opt::time_limit);
307 opt::auto_depl::topology = optarg;
308 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
309 opt::auto_depl::topology)
313 // nothing to do: this option is checked at the very
314 // beginning of main()
317 opt::version_requested = true;
320 PARSE_ARG(opt::option_x);
321 XBT_WARN("option_x set to %d", opt::option_x);
324 XBT_ERROR("invalid option -- '%c'", optopt);
332 if (opt::version_requested || opt::help_requested)
335 if (optind < *argc) {
336 opt::platform_file = argv[optind++];
338 XBT_ERROR("missing parameter -- <plaform_file>");
341 if (optind < *argc) {
342 opt::deployment_file = argv[optind++];
344 opt::auto_depl::enabled = opt::deployment_file.empty();
346 while (optind < *argc) {
347 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
351 if (opt::max_transfer_amount &&
352 opt::max_transfer_amount < opt::min_transfer_amount) {
353 XBT_ERROR("max. data transfer amount < min. data transfer amount");
357 if (!opt::auto_depl::random_seed)
358 opt::auto_depl::random_seed = time(NULL);
367 #define DESCR(description, format, value) \
368 XBT_INFO("| %s: " format, h.descr(description), value)
370 XBT_INFO(",----[ Simulation parameters ]");
371 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
372 DESCR("platform file", "\"%s\"", platform_file.c_str());
373 if (auto_depl::enabled) {
374 XBT_INFO("| automatic deployment enabled");
375 DESCR("- topology", "%s", auto_depl::topology.c_str());
376 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
378 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
380 DESCR("- random initial load distribution", "%s",
381 h.on_off(auto_depl::random_distribution));
382 DESCR("- random seed", "%s",
383 h.val_or_string(auto_depl::random_seed, "time based"));
385 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
387 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
388 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
389 DESCR("minimum duration between lb. iterations", "%g",
390 min_lb_iter_duration);
391 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
392 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
393 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
394 DESCR("maximum data transfer amount", "%s",
395 h.val_or_string(max_transfer_amount, "no limit"));
396 DESCR("minimum duration between comp. iterations", "%g",
397 min_comp_iter_duration);
398 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
399 DESCR("computations start after time", "%g", comp_time_delay);
400 DESCR("maximum number of lb. iterations", "%s",
401 h.val_or_string(lb_maxiter, "no limit"));
402 DESCR("maximum number of comp. iterations", "%s",
403 h.val_or_string(comp_maxiter, "no limit"));
404 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
405 DESCR("exit on close", "%s", h.on_off(exit_on_close));
414 #define o(opt) " " << std::setw(14) \
415 << std::left << (opt) << std::right << " "
417 #define so(subopt) std::setw(18) << (subopt) << " : "
419 #define so_list(name) do { \
420 name ## _type::iterator it; \
421 for (it = name.begin() ; it != name.end() ; ++it) \
422 std::clog << so(name.get_name(it)) \
423 << name.get_descr(it) << "\n"; \
427 std::clog << "Usage: " << opt::program_name
428 << " [options] <platform_file> [<deployment_file>]\n";
429 std::clog << "Current values for the parameters are given between square brackets.\n";
431 std::clog << "\nGlobal options:\n";
433 << "print help and exit (use -hh or -hhh for extended help)\n";
434 if (opt::help_requested < 1)
437 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
438 std::clog << o("-V") << "print version and exit\n";
440 std::clog << "\nSimulator output:\n";
441 std::clog << o("-l value")
442 << "print current load every n lb iterations, 0 to disable"
443 << " [" << opt::log_rate << "]\n";
445 << "verbose: do not override the default logging parameters\n";
447 std::clog << "\nAutomatic deployment:\n";
448 std::clog << o("-T name")
449 << "enable automatic deployment with selected topology"
450 << " [" << opt::auto_depl::topology << "]\n";
451 if (opt::help_requested > 1)
452 so_list(opt::topologies);
453 std::clog << o("-L value")
454 << "total load with auto deployment, 0 for number of hosts"
455 << " [" << opt::auto_depl::load << "]\n";
456 std::clog << o("-N value")
457 << "number of hosts to use with auto deployment, 0 for max."
458 << " [" << opt::auto_depl::nhosts << "]\n";
460 << "toggle random initial load distribution"
461 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
463 std::clog << o("-r value")
464 << "random seed, 0 for a seed based on current date"
465 << " [" << opt::auto_depl::random_seed << "]\n";
467 std::clog << "\nLoad balancing algorithm:\n";
468 std::clog << o("-a name") << "load balancing algorithm"
469 << " [" << opt::loba_algo << "]\n";
470 if (opt::help_requested > 1)
471 so_list(opt::loba_algorithms);
472 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
473 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
475 std::clog << "\nLb. and comp. iterations:\n";
476 std::clog << o("-s value")
477 << "minimum duration between lb. iterations"
478 << " [" << opt::min_lb_iter_duration << "]\n";
479 std::clog << o("-S value")
480 << "minimum duration between comp. iterations"
481 << " [" << opt::min_comp_iter_duration << "]\n";
482 std::clog << o("-d value")
483 << "start comp. iterations after given number of lb. iter."
484 << " [" << opt::comp_iter_delay << "]\n";
485 std::clog << o("-D value")
486 << "start comp. iterations after given time"
487 << " [" << opt::comp_time_delay << "]\n";
489 std::clog << "\nComputations and communications:\n";
490 std::clog << o("-c [aN,...]a0")
491 << "polynomial factors for computation cost"
492 << " [" << opt::comp_cost.to_string() << "]\n";
493 std::clog << o("-C [aN,...]a0")
494 << "polynomial factors for communication cost"
495 << " [" << opt::comm_cost.to_string() << "]\n";
496 std::clog << o("-m value")
497 << "minimum data transfer amount"
498 << " [" << opt::min_transfer_amount << "]\n";
499 std::clog << o("-M value")
500 << "maximum data transfer amount, 0 for no limit"
501 << " [" << opt::max_transfer_amount << "]\n";
503 std::clog << "\nEnd of the simulation:\n";
504 std::clog << o("-i value")
505 << "maximum number of lb. iterations, 0 for no limit"
506 << " [" << opt::lb_maxiter << "]\n";
507 std::clog << o("-I value")
508 << "maximum number of comp. iterations, 0 for no limit"
509 << " [" << opt::comp_maxiter << "]\n";
510 std::clog << o("-t value")
511 << "time limit (simulated time), 0 for no limit"
512 << " [" << opt::time_limit << "]\n";
513 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
514 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
516 if (opt::help_requested < 3)
519 std::clog << "\nLogging support:\n"
520 << " See SimGrid documentation on:\n"
521 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
522 << " Existing categories are:\n"
523 << " simu : root of following categories\n"
524 << " main : messages from global infrastructure\n"
525 << " depl : messages from auto deployment (inherited from main)\n"
526 << " comm : messages from asynchronous pipes\n"
527 << " proc : messages from base process class\n"
528 << " loba : messages from load-balancer\n"
529 << " thrd : messages from thread wrapper class\n";
531 // std::clog << "\nMiscellaneous low-level parameters\n";