6 #include <unistd.h> // getopt
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
11 #include "deployment.h"
13 #include "loba_besteffort.h"
14 #include "loba_bulk.h"
15 #include "loba_fairstrategy.h"
17 #include "loba_makhoul.h"
18 #include "loba_makhoul2.h"
19 #include "loba_simple.h"
24 #define DATA_DESCR_WIDTH 42
30 // A sum of loads if considered null if it is less than
31 // load_ratio_threshold percent of the sum of loads at init.
32 const double load_ratio_threshold = 1e-4;
35 std::string program_name;
36 int help_requested = 0;
37 bool version_requested = false;
38 int option_x = 0; // hidden option, for debugging purpose
40 // Simulation parameters
42 volatile sig_atomic_t exit_request = 0;
44 // Platform and deployment
45 std::string platform_file;
46 std::string deployment_file;
48 // Automatic deployment
51 std::string topology("clique");
54 bool random_distribution = false;
55 unsigned long random_seed = 0;
58 // Load balancing algorithm
59 std::string loba_algo("simple");
60 bool bookkeeping = false;
61 double min_lb_iter_duration = 1.0; // fixme: find better defaults
62 double min_transfer_amount = 0.0;
63 double max_transfer_amount = 0.0;
65 // Application parameters
66 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
67 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
68 double min_comp_iter_duration = 1.0; // fixme: find better defaults
69 unsigned comp_iter_delay = 0; // fixme: find better defaults
70 double comp_time_delay = 0.0; // fixme: find better defaults
72 // Parameters for the end of the simulation
73 unsigned lb_maxiter = 0;
74 unsigned comp_maxiter = 0;
75 double time_limit = 0;
76 bool exit_on_close = true;
78 // Named parameters lists
79 loba_algorithms_type loba_algorithms;
80 loba_algorithms_type::loba_algorithms_type()
82 NOL_INSERT("besteffort", "balance with best effort strategy",
84 NOL_INSERT("bulk", "A multi-load-units assignation rule without ordering...",
86 NOL_INSERT("fairstrategy", "balance with fair strategy",
88 NOL_INSERT("lln", "Balance with less loaded neighbors without ordering-bulk method",
90 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
92 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
94 NOL_INSERT("none", "no load-balancing (for testing only)",
96 NOL_INSERT("simple", "balance with least loaded neighbor",
100 topologies_type topologies;
101 topologies_type::topologies_type()
103 NOL_INSERT("btree", "binary tree topology, initial load at root",
105 NOL_INSERT("clique", "all connected topology", deployment_clique);
106 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
107 NOL_INSERT("line", "line topology, initial load at one end",
109 NOL_INSERT("ring", "ring topology", deployment_ring);
110 NOL_INSERT("star", "star topology, initial load at center",
112 NOL_INSERT("torus", "torus topology", deployment_torus);
119 // local helper class
122 template <typename T>
123 static bool parse_arg(char opt, const char *arg, T& val);
124 static const char* on_off(bool b);
125 const char* descr(const char* str);
126 template <typename T>
127 const char* val_or_string(const T& val, const char* str,
129 template <typename T>
130 static bool nol_find_prefix(const T& nol, const char* descr,
134 std::string descr_str;
135 std::string val_or_string_str;
140 template <typename T>
141 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
143 std::istringstream str(arg);
144 bool result = (str >> val) && str.eof();
146 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
150 const char* opt_helper::on_off(bool b)
152 return b ? "on" : "off";
155 const char* opt_helper::descr(const char* str)
157 std::string& res = descr_str;
159 res.resize(DATA_DESCR_WIDTH, '.');
163 template <typename T>
164 const char* opt_helper::val_or_string(const T& val, const char* str,
167 std::string& res = val_or_string_str;
169 std::ostringstream oss;
178 template <typename T>
179 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
182 bool result = nol.exists(name);
184 std::stack<std::string> candidates;
185 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
186 const std::string& fullname = nol.get_name(it);
187 if (fullname.compare(0, name.length(), name) == 0)
188 candidates.push(fullname);
190 switch (candidates.size()) {
192 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
195 name = candidates.top();
198 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
201 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
202 while (!candidates.empty()) {
203 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
212 bool opt::parse_args(int* argc, char* argv[])
216 opt::program_name = argv[0];
217 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
219 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
223 while ((c = getopt(*argc, argv,
224 "a:bc:C:d:D:ehi:I:l:L:m:M:N:r:Rs:S:t:T:vVx:")) != -1) {
227 opt::loba_algo = optarg;
228 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
229 "load balancing algorithm",
234 opt::bookkeeping = !opt::bookkeeping;
237 opt::exit_on_close = !opt::exit_on_close;
240 opt::help_requested++;
244 opt::comp_cost = cost_func(optarg);
246 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
253 opt::comm_cost = cost_func(optarg);
255 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
261 PARSE_ARG(opt::comp_iter_delay);
264 PARSE_ARG(opt::comp_time_delay);
267 PARSE_ARG(opt::lb_maxiter);
270 PARSE_ARG(opt::comp_maxiter);
273 PARSE_ARG(opt::log_rate);
276 PARSE_ARG(opt::auto_depl::load);
279 PARSE_ARG(opt::min_transfer_amount);
282 PARSE_ARG(opt::max_transfer_amount);
285 PARSE_ARG(opt::auto_depl::nhosts);
288 PARSE_ARG(opt::auto_depl::random_seed);
291 opt::auto_depl::random_distribution =
292 !opt::auto_depl::random_distribution;
295 PARSE_ARG(opt::min_lb_iter_duration);
298 PARSE_ARG(opt::min_comp_iter_duration);
301 PARSE_ARG(opt::time_limit);
304 opt::auto_depl::topology = optarg;
305 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
306 opt::auto_depl::topology)
310 // nothing to do: this option is checked at the very
311 // beginning of main()
314 opt::version_requested = true;
317 PARSE_ARG(opt::option_x);
318 XBT_WARN("option_x set to %d", opt::option_x);
321 XBT_ERROR("invalid option -- '%c'", optopt);
329 if (opt::version_requested || opt::help_requested)
332 if (optind < *argc) {
333 opt::platform_file = argv[optind++];
335 XBT_ERROR("missing parameter -- <plaform_file>");
338 if (optind < *argc) {
339 opt::deployment_file = argv[optind++];
341 opt::auto_depl::enabled = opt::deployment_file.empty();
343 while (optind < *argc) {
344 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
348 if (opt::max_transfer_amount &&
349 opt::max_transfer_amount < opt::min_transfer_amount) {
350 XBT_ERROR("max. data transfer amount < min. data transfer amount");
354 if (!opt::auto_depl::random_seed)
355 opt::auto_depl::random_seed = time(NULL);
364 #define DESCR(description, format, value) \
365 XBT_INFO("| %s: " format, h.descr(description), value)
367 XBT_INFO(",----[ Simulation parameters ]");
368 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
369 DESCR("platform file", "\"%s\"", platform_file.c_str());
370 if (auto_depl::enabled) {
371 XBT_INFO("| automatic deployment enabled");
372 DESCR("- topology", "%s", auto_depl::topology.c_str());
373 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
375 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
377 DESCR("- random initial load distribution", "%s",
378 h.on_off(auto_depl::random_distribution));
379 DESCR("- random seed", "%s",
380 h.val_or_string(auto_depl::random_seed, "time based"));
382 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
384 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
385 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
386 DESCR("minimum duration between lb. iterations", "%g",
387 min_lb_iter_duration);
388 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
389 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
390 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
391 DESCR("maximum data transfer amount", "%s",
392 h.val_or_string(max_transfer_amount, "no limit"));
393 DESCR("minimum duration between comp. iterations", "%g",
394 min_comp_iter_duration);
395 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
396 DESCR("computations start after time", "%g", comp_time_delay);
397 DESCR("maximum number of lb. iterations", "%s",
398 h.val_or_string(lb_maxiter, "no limit"));
399 DESCR("maximum number of comp. iterations", "%s",
400 h.val_or_string(comp_maxiter, "no limit"));
401 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
402 DESCR("exit on close", "%s", h.on_off(exit_on_close));
411 #define o(opt) " " << std::setw(14) \
412 << std::left << (opt) << std::right << " "
414 #define so(subopt) std::setw(18) << (subopt) << " : "
416 #define so_list(name) do { \
417 name ## _type::iterator it; \
418 for (it = name.begin() ; it != name.end() ; ++it) \
419 std::clog << so(name.get_name(it)) \
420 << name.get_descr(it) << "\n"; \
424 std::clog << "Usage: " << opt::program_name
425 << " [options] <platform_file> [<deployment_file>]\n";
426 std::clog << "Current values for the parameters are given between square brackets.\n";
428 std::clog << "\nGlobal options:\n";
430 << "print help and exit (use -hh or -hhh for extended help)\n";
431 if (opt::help_requested < 1)
434 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
435 std::clog << o("-V") << "print version and exit\n";
437 std::clog << "\nSimulator output:\n";
438 std::clog << o("-l value")
439 << "print current load every n lb iterations, 0 to disable"
440 << " [" << opt::log_rate << "]\n";
442 << "verbose: do not override the default logging parameters\n";
444 std::clog << "\nAutomatic deployment:\n";
445 std::clog << o("-T name")
446 << "enable automatic deployment with selected topology"
447 << " [" << opt::auto_depl::topology << "]\n";
448 if (opt::help_requested > 1)
449 so_list(opt::topologies);
450 std::clog << o("-L value")
451 << "total load with auto deployment, 0 for number of hosts"
452 << " [" << opt::auto_depl::load << "]\n";
453 std::clog << o("-N value")
454 << "number of hosts to use with auto deployment, 0 for max."
455 << " [" << opt::auto_depl::nhosts << "]\n";
457 << "toggle random initial load distribution"
458 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
460 std::clog << o("-r value")
461 << "random seed, 0 for a seed based on current date"
462 << " [" << opt::auto_depl::random_seed << "]\n";
464 std::clog << "\nLoad balancing algorithm:\n";
465 std::clog << o("-a name") << "load balancing algorithm"
466 << " [" << opt::loba_algo << "]\n";
467 if (opt::help_requested > 1)
468 so_list(opt::loba_algorithms);
469 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
470 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
472 std::clog << "\nLb. and comp. iterations:\n";
473 std::clog << o("-s value")
474 << "minimum duration between lb. iterations"
475 << " [" << opt::min_lb_iter_duration << "]\n";
476 std::clog << o("-S value")
477 << "minimum duration between comp. iterations"
478 << " [" << opt::min_comp_iter_duration << "]\n";
479 std::clog << o("-d value")
480 << "start comp. iterations after given number of lb. iter."
481 << " [" << opt::comp_iter_delay << "]\n";
482 std::clog << o("-D value")
483 << "start comp. iterations after given time"
484 << " [" << opt::comp_time_delay << "]\n";
486 std::clog << "\nComputations and communications:\n";
487 std::clog << o("-c [aN,...]a0")
488 << "polynomial factors for computation cost"
489 << " [" << opt::comp_cost.to_string() << "]\n";
490 std::clog << o("-C [aN,...]a0")
491 << "polynomial factors for communication cost"
492 << " [" << opt::comm_cost.to_string() << "]\n";
493 std::clog << o("-m value")
494 << "minimum data transfer amount"
495 << " [" << opt::min_transfer_amount << "]\n";
496 std::clog << o("-M value")
497 << "maximum data transfer amount, 0 for no limit"
498 << " [" << opt::max_transfer_amount << "]\n";
500 std::clog << "\nEnd of the simulation:\n";
501 std::clog << o("-i value")
502 << "maximum number of lb. iterations, 0 for no limit"
503 << " [" << opt::lb_maxiter << "]\n";
504 std::clog << o("-I value")
505 << "maximum number of comp. iterations, 0 for no limit"
506 << " [" << opt::comp_maxiter << "]\n";
507 std::clog << o("-t value")
508 << "time limit (simulated time), 0 for no limit"
509 << " [" << opt::time_limit << "]\n";
510 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
511 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
513 if (opt::help_requested < 3)
516 std::clog << "\nLogging support:\n"
517 << " See SimGrid documentation on:\n"
518 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
519 << " Existing categories are:\n"
520 << " simu : root of following categories\n"
521 << " main : messages from global infrastructure\n"
522 << " depl : messages from auto deployment (inherited from main)\n"
523 << " comm : messages from asynchronous pipes\n"
524 << " proc : messages from base process class\n"
525 << " loba : messages from load-balancer\n"
526 << " thrd : messages from thread wrapper class\n";
528 // std::clog << "\nMiscellaneous low-level parameters\n";