6 #include <unistd.h> // getopt
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
11 #include "deployment.h"
13 #include "loba_besteffort.h"
14 #include "loba_fairstrategy.h"
15 #include "loba_makhoul.h"
16 #include "loba_makhoul2.h"
17 #include "loba_simple.h"
22 #define DATA_DESCR_WIDTH 42
28 // A sum of loads if considered null if it is less than
29 // load_ratio_threshold percent of the sum of loads at init.
30 const double load_ratio_threshold = 1e-4;
33 std::string program_name;
34 int help_requested = 0;
35 bool version_requested = false;
36 int option_x = 0; // hidden option, for debugging purpose
38 // Simulation parameters
40 bool exit_request = false;
42 // Platform and deployment
43 std::string platform_file;
44 std::string deployment_file;
46 // Automatic deployment
49 std::string topology("clique");
52 bool random_distribution = false;
53 unsigned long random_seed = 0;
56 // Load balancing algorithm
57 std::string loba_algo("simple");
58 bool bookkeeping = false;
59 double min_lb_iter_duration = 1.0; // fixme: find better defaults
60 double min_transfer_amount = 0.0;
61 double max_transfer_amount = 0.0;
63 // Application parameters
64 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
65 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
66 double min_comp_iter_duration = 1.0; // fixme: find better defaults
67 unsigned comp_iter_delay = 0; // fixme: find better defaults
68 double comp_time_delay = 0.0; // fixme: find better defaults
70 // Parameters for the end of the simulation
71 unsigned lb_maxiter = 0;
72 unsigned comp_maxiter = 0;
73 double time_limit = 0;
74 bool exit_on_close = true;
76 // Named parameters lists
77 loba_algorithms_type loba_algorithms;
78 loba_algorithms_type::loba_algorithms_type()
80 NOL_INSERT("besteffort", "balance with best effort strategy",
82 NOL_INSERT("fairstrategy", "balance with fair strategy",
84 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
86 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
88 NOL_INSERT("none", "no load-balancing (for testing only)",
90 NOL_INSERT("simple", "balance with least loaded neighbor",
94 topologies_type topologies;
95 topologies_type::topologies_type()
97 NOL_INSERT("btree", "binary tree topology, initial load at root",
99 NOL_INSERT("clique", "all connected topology", deployment_clique);
100 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
101 NOL_INSERT("line", "line topology, initial load at one end",
103 NOL_INSERT("ring", "ring topology", deployment_ring);
104 NOL_INSERT("star", "star topology, initial load at center",
106 NOL_INSERT("torus", "torus topology", deployment_torus);
113 // local helper class
116 template <typename T>
117 static bool parse_arg(char opt, const char *arg, T& val);
118 static const char* on_off(bool b);
119 const char* descr(const char* str);
120 template <typename T>
121 const char* val_or_string(const T& val, const char* str,
123 template <typename T>
124 static bool nol_find_prefix(const T& nol, const char* descr,
128 std::string descr_str;
129 std::string val_or_string_str;
134 template <typename T>
135 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
137 std::istringstream str(arg);
138 bool result = (str >> val) && str.eof();
140 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
144 const char* opt_helper::on_off(bool b)
146 return b ? "on" : "off";
149 const char* opt_helper::descr(const char* str)
151 std::string& res = descr_str;
153 res.resize(DATA_DESCR_WIDTH, '.');
157 template <typename T>
158 const char* opt_helper::val_or_string(const T& val, const char* str,
161 std::string& res = val_or_string_str;
163 std::ostringstream oss;
172 template <typename T>
173 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
176 bool result = nol.exists(name);
178 std::stack<std::string> candidates;
179 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
180 const std::string& fullname = nol.get_name(it);
181 if (fullname.compare(0, name.length(), name) == 0)
182 candidates.push(fullname);
184 switch (candidates.size()) {
186 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
189 name = candidates.top();
192 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
195 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
196 while (!candidates.empty()) {
197 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
206 bool opt::parse_args(int* argc, char* argv[])
210 opt::program_name = argv[0];
211 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
213 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
217 while ((c = getopt(*argc, argv,
218 "a:bc:C:d:D:ehi:I:l:L:m:M:N:r:Rs:S:t:T:vVx:")) != -1) {
221 opt::loba_algo = optarg;
222 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
223 "load balancing algorithm",
228 opt::bookkeeping = !opt::bookkeeping;
231 opt::exit_on_close = !opt::exit_on_close;
234 opt::help_requested++;
238 opt::comp_cost = cost_func(optarg);
240 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
247 opt::comm_cost = cost_func(optarg);
249 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
255 PARSE_ARG(opt::comp_iter_delay);
258 PARSE_ARG(opt::comp_time_delay);
261 PARSE_ARG(opt::lb_maxiter);
264 PARSE_ARG(opt::comp_maxiter);
267 PARSE_ARG(opt::log_rate);
270 PARSE_ARG(opt::auto_depl::load);
273 PARSE_ARG(opt::min_transfer_amount);
276 PARSE_ARG(opt::max_transfer_amount);
279 PARSE_ARG(opt::auto_depl::nhosts);
282 PARSE_ARG(opt::auto_depl::random_seed);
285 opt::auto_depl::random_distribution =
286 !opt::auto_depl::random_distribution;
289 PARSE_ARG(opt::min_lb_iter_duration);
292 PARSE_ARG(opt::min_comp_iter_duration);
295 PARSE_ARG(opt::time_limit);
298 opt::auto_depl::topology = optarg;
299 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
300 opt::auto_depl::topology)
304 // nothing to do: this option is checked at the very
305 // beginning of main()
308 opt::version_requested = true;
311 PARSE_ARG(opt::option_x);
312 XBT_WARN("option_x set to %d", opt::option_x);
315 XBT_ERROR("invalid option -- '%c'", optopt);
323 if (opt::version_requested || opt::help_requested)
326 if (optind < *argc) {
327 opt::platform_file = argv[optind++];
329 XBT_ERROR("missing parameter -- <plaform_file>");
332 if (optind < *argc) {
333 opt::deployment_file = argv[optind++];
335 opt::auto_depl::enabled = opt::deployment_file.empty();
337 while (optind < *argc) {
338 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
342 if (opt::max_transfer_amount &&
343 opt::max_transfer_amount < opt::min_transfer_amount) {
344 XBT_ERROR("max. data transfer amount < min. data transfer amount");
348 if (!opt::auto_depl::random_seed)
349 opt::auto_depl::random_seed = time(NULL);
358 #define DESCR(description, format, value) \
359 XBT_INFO("| %s: " format, h.descr(description), value)
361 XBT_INFO(",----[ Simulation parameters ]");
362 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
363 DESCR("platform file", "\"%s\"", platform_file.c_str());
364 if (auto_depl::enabled) {
365 XBT_INFO("| automatic deployment enabled");
366 DESCR("- topology", "%s", auto_depl::topology.c_str());
367 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
369 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
371 DESCR("- random initial load distribution", "%s",
372 h.on_off(auto_depl::random_distribution));
373 DESCR("- random seed", "%s",
374 h.val_or_string(auto_depl::random_seed, "time based"));
376 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
378 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
379 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
380 DESCR("minimum duration between lb. iterations", "%g",
381 min_lb_iter_duration);
382 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
383 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
384 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
385 DESCR("maximum data transfer amount", "%s",
386 h.val_or_string(max_transfer_amount, "no limit"));
387 DESCR("minimum duration between comp. iterations", "%g",
388 min_comp_iter_duration);
389 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
390 DESCR("computations start after time", "%g", comp_time_delay);
391 DESCR("maximum number of lb. iterations", "%s",
392 h.val_or_string(lb_maxiter, "no limit"));
393 DESCR("maximum number of comp. iterations", "%s",
394 h.val_or_string(comp_maxiter, "no limit"));
395 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
396 DESCR("exit on close", "%s", h.on_off(exit_on_close));
405 #define o(opt) " " << std::setw(14) \
406 << std::left << (opt) << std::right << " "
408 #define so(subopt) std::setw(18) << (subopt) << " : "
410 #define so_list(name) do { \
411 name ## _type::iterator it; \
412 for (it = name.begin() ; it != name.end() ; ++it) \
413 std::clog << so(name.get_name(it)) \
414 << name.get_descr(it) << "\n"; \
418 std::clog << "Usage: " << opt::program_name
419 << " [options] <platform_file> [<deployment_file>]\n";
420 std::clog << "Current values for the parameters are given between square brackets.\n";
422 std::clog << "\nGlobal options:\n";
424 << "print help and exit (use -hh or -hhh for extended help)\n";
425 if (opt::help_requested < 1)
428 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
429 std::clog << o("-V") << "print version and exit\n";
431 std::clog << "\nSimulator output:\n";
432 std::clog << o("-l value")
433 << "print current load every n lb iterations, 0 to disable"
434 << " [" << opt::log_rate << "]\n";
436 << "verbose: do not override the default logging parameters\n";
438 std::clog << "\nAutomatic deployment:\n";
439 std::clog << o("-T name")
440 << "enable automatic deployment with selected topology"
441 << " [" << opt::auto_depl::topology << "]\n";
442 if (opt::help_requested > 1)
443 so_list(opt::topologies);
444 std::clog << o("-L value")
445 << "total load with auto deployment, 0 for number of hosts"
446 << " [" << opt::auto_depl::load << "]\n";
447 std::clog << o("-N value")
448 << "number of hosts to use with auto deployment, 0 for max."
449 << " [" << opt::auto_depl::nhosts << "]\n";
451 << "toggle random initial load distribution"
452 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
454 std::clog << o("-r value")
455 << "random seed, 0 for a seed based on current date"
456 << " [" << opt::auto_depl::random_seed << "]\n";
458 std::clog << "\nLoad balancing algorithm:\n";
459 std::clog << o("-a name") << "load balancing algorithm"
460 << " [" << opt::loba_algo << "]\n";
461 if (opt::help_requested > 1)
462 so_list(opt::loba_algorithms);
463 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
464 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
466 std::clog << "\nLb. and comp. iterations:\n";
467 std::clog << o("-s value")
468 << "minimum duration between lb. iterations"
469 << " [" << opt::min_lb_iter_duration << "]\n";
470 std::clog << o("-S value")
471 << "minimum duration between comp. iterations"
472 << " [" << opt::min_comp_iter_duration << "]\n";
473 std::clog << o("-d value")
474 << "start comp. iterations after given number of lb. iter."
475 << " [" << opt::comp_iter_delay << "]\n";
476 std::clog << o("-D value")
477 << "start comp. iterations after given time"
478 << " [" << opt::comp_time_delay << "]\n";
480 std::clog << "\nComputations and communications:\n";
481 std::clog << o("-c [aN,...]a0")
482 << "polynomial factors for computation cost"
483 << " [" << opt::comp_cost.to_string() << "]\n";
484 std::clog << o("-C [aN,...]a0")
485 << "polynomial factors for communication cost"
486 << " [" << opt::comm_cost.to_string() << "]\n";
487 std::clog << o("-m value")
488 << "minimum data transfer amount"
489 << " [" << opt::min_transfer_amount << "]\n";
490 std::clog << o("-M value")
491 << "maximum data transfer amount, 0 for no limit"
492 << " [" << opt::max_transfer_amount << "]\n";
494 std::clog << "\nEnd of the simulation:\n";
495 std::clog << o("-i value")
496 << "maximum number of lb. iterations, 0 for no limit"
497 << " [" << opt::lb_maxiter << "]\n";
498 std::clog << o("-I value")
499 << "maximum number of comp. iterations, 0 for no limit"
500 << " [" << opt::comp_maxiter << "]\n";
501 std::clog << o("-t value")
502 << "time limit (simulated time), 0 for no limit"
503 << " [" << opt::time_limit << "]\n";
504 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
505 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
507 if (opt::help_requested < 3)
510 std::clog << "\nLogging support:\n"
511 << " See SimGrid documentation on:\n"
512 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
513 << " Existing categories are:\n"
514 << " simu : root of following categories\n"
515 << " main : messages from global infrastructure\n"
516 << " depl : messages from auto deployment (inherited from main)\n"
517 << " comm : messages from asynchronous pipes\n"
518 << " proc : messages from base process class\n"
519 << " loba : messages from load-balancer\n"
520 << " thrd : messages from thread wrapper class\n";
522 // std::clog << "\nMiscellaneous low-level parameters\n";