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;
37 // Simulation parameters
39 bool exit_request = false;
41 // Platform and deployment
42 std::string platform_file;
43 std::string deployment_file;
45 // Automatic deployment
48 std::string topology("clique");
51 bool random_distribution = false;
52 unsigned long random_seed = 0;
55 // Load balancing algorithm
56 std::string loba_algo("simple");
57 bool bookkeeping = false;
58 double min_lb_iter_duration = 1.0; // fixme: find better defaults
59 double min_transfer_amount = 0.0;
60 double max_transfer_amount = 0.0;
62 // Application parameters
63 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
64 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
65 double min_comp_iter_duration = 1.0; // fixme: find better defaults
66 unsigned comp_iter_delay = 0; // fixme: find better defaults
67 double comp_time_delay = 0.0; // fixme: find better defaults
69 // Parameters for the end of the simulation
70 unsigned lb_maxiter = 0;
71 unsigned comp_maxiter = 0;
72 double time_limit = 0;
73 bool exit_on_close = true;
75 // Named parameters lists
76 loba_algorithms_type loba_algorithms;
77 loba_algorithms_type::loba_algorithms_type()
79 NOL_INSERT("besteffort", "balance with best effort strategy",
81 NOL_INSERT("fairstrategy", "balance with fair strategy",
83 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
85 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
87 NOL_INSERT("none", "no load-balancing (for testing only)",
89 NOL_INSERT("simple", "balance with least loaded neighbor",
93 topologies_type topologies;
94 topologies_type::topologies_type()
96 NOL_INSERT("btree", "binary tree topology, initial load at root",
98 NOL_INSERT("clique", "all connected topology", deployment_clique);
99 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
100 NOL_INSERT("line", "line topology, initial load at one end",
102 NOL_INSERT("ring", "ring topology", deployment_ring);
103 NOL_INSERT("star", "star topology, initial load at center",
105 NOL_INSERT("torus", "torus topology", deployment_torus);
112 // local helper class
115 template <typename T>
116 static bool parse_arg(char opt, const char *arg, T& val);
117 static const char* on_off(bool b);
118 const char* descr(const char* str);
119 template <typename T>
120 const char* val_or_string(const T& val, const char* str,
122 template <typename T>
123 static bool nol_find_prefix(const T& nol, const char* descr,
127 std::string descr_str;
128 std::string val_or_string_str;
133 template <typename T>
134 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
136 std::istringstream str(arg);
137 bool result = (str >> val) && str.eof();
139 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
143 const char* opt_helper::on_off(bool b)
145 return b ? "on" : "off";
148 const char* opt_helper::descr(const char* str)
150 std::string& res = descr_str;
152 res.resize(DATA_DESCR_WIDTH, '.');
156 template <typename T>
157 const char* opt_helper::val_or_string(const T& val, const char* str,
160 std::string& res = val_or_string_str;
162 std::ostringstream oss;
171 template <typename T>
172 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
175 bool result = nol.exists(name);
177 std::stack<std::string> candidates;
178 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
179 const std::string& fullname = nol.get_name(it);
180 if (fullname.compare(0, name.length(), name) == 0)
181 candidates.push(fullname);
183 switch (candidates.size()) {
185 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
188 name = candidates.top();
191 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
194 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
195 while (!candidates.empty()) {
196 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
205 bool opt::parse_args(int* argc, char* argv[])
209 opt::program_name = argv[0];
210 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
212 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
216 while ((c = getopt(*argc, argv,
217 "a:bc:C:d:D:ehi:I:l:L:m:M:N:r:Rs:S:t:T:vV")) != -1) {
220 opt::loba_algo = optarg;
221 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
222 "load balancing algorithm",
227 opt::bookkeeping = !opt::bookkeeping;
230 opt::exit_on_close = !opt::exit_on_close;
233 opt::help_requested++;
237 opt::comp_cost = cost_func(optarg);
239 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
246 opt::comm_cost = cost_func(optarg);
248 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
254 PARSE_ARG(opt::comp_iter_delay);
257 PARSE_ARG(opt::comp_time_delay);
260 PARSE_ARG(opt::lb_maxiter);
263 PARSE_ARG(opt::comp_maxiter);
266 PARSE_ARG(opt::log_rate);
269 PARSE_ARG(opt::auto_depl::load);
272 PARSE_ARG(opt::min_transfer_amount);
275 PARSE_ARG(opt::max_transfer_amount);
278 PARSE_ARG(opt::auto_depl::nhosts);
281 PARSE_ARG(opt::auto_depl::random_seed);
284 opt::auto_depl::random_distribution =
285 !opt::auto_depl::random_distribution;
288 PARSE_ARG(opt::min_lb_iter_duration);
291 PARSE_ARG(opt::min_comp_iter_duration);
294 PARSE_ARG(opt::time_limit);
297 opt::auto_depl::topology = optarg;
298 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
299 opt::auto_depl::topology)
303 // nothing to do: this option is checked at the very
304 // beginning of main()
307 opt::version_requested = true;
310 XBT_ERROR("invalid option -- '%c'", optopt);
318 if (opt::version_requested || opt::help_requested)
321 if (optind < *argc) {
322 opt::platform_file = argv[optind++];
324 XBT_ERROR("missing parameter -- <plaform_file>");
327 if (optind < *argc) {
328 opt::deployment_file = argv[optind++];
330 opt::auto_depl::enabled = opt::deployment_file.empty();
332 while (optind < *argc) {
333 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
337 if (opt::max_transfer_amount &&
338 opt::max_transfer_amount < opt::min_transfer_amount) {
339 XBT_ERROR("max. data transfer amount < min. data transfer amount");
343 if (!opt::auto_depl::random_seed)
344 opt::auto_depl::random_seed = time(NULL);
353 #define DESCR(description, format, value) \
354 XBT_INFO("| %s: " format, h.descr(description), value)
356 XBT_INFO(",----[ Simulation parameters ]");
357 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
358 DESCR("platform file", "\"%s\"", platform_file.c_str());
359 if (auto_depl::enabled) {
360 XBT_INFO("| automatic deployment enabled");
361 DESCR("- topology", "%s", auto_depl::topology.c_str());
362 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
364 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
366 DESCR("- random initial load distribution", "%s",
367 h.on_off(auto_depl::random_distribution));
368 DESCR("- random seed", "%s",
369 h.val_or_string(auto_depl::random_seed, "time based"));
371 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
373 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
374 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
375 DESCR("minimum duration between lb. iterations", "%g",
376 min_lb_iter_duration);
377 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
378 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
379 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
380 DESCR("maximum data transfer amount", "%s",
381 h.val_or_string(max_transfer_amount, "no limit"));
382 DESCR("minimum duration between comp. iterations", "%g",
383 min_comp_iter_duration);
384 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
385 DESCR("computations start after time", "%g", comp_time_delay);
386 DESCR("maximum number of lb. iterations", "%s",
387 h.val_or_string(lb_maxiter, "no limit"));
388 DESCR("maximum number of comp. iterations", "%s",
389 h.val_or_string(comp_maxiter, "no limit"));
390 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
391 DESCR("exit on close", "%s", h.on_off(exit_on_close));
400 #define o(opt) " " << std::setw(14) \
401 << std::left << (opt) << std::right << " "
403 #define so(subopt) std::setw(18) << (subopt) << " : "
405 #define so_list(name) do { \
406 name ## _type::iterator it; \
407 for (it = name.begin() ; it != name.end() ; ++it) \
408 std::clog << so(name.get_name(it)) \
409 << name.get_descr(it) << "\n"; \
413 std::clog << "Usage: " << opt::program_name
414 << " [options] <platform_file> [<deployment_file>]\n";
416 std::clog << "\nGlobal options\n";
418 << "print help and exit (use -hh or -hhh for extended help)\n";
419 if (opt::help_requested < 1)
422 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
423 std::clog << o("-V") << "print version and exit\n";
425 std::clog << "\nSimulation parameters\n";
426 std::clog << o("-l value")
427 << "print current load every n lb iterations, 0 to disable"
428 << " [" << opt::log_rate << "]\n";
430 << "verbose: do not override the default logging parameters\n";
432 std::clog << "\nAutomatic deployment options\n";
433 std::clog << o("-T name")
434 << "enable automatic deployment with selected topology"
435 << " [" << opt::auto_depl::topology << "]\n";
436 if (opt::help_requested > 1)
437 so_list(opt::topologies);
438 std::clog << o("-L value")
439 << "total load with auto deployment, 0 for number of hosts"
440 << " [" << opt::auto_depl::load << "]\n";
441 std::clog << o("-N value")
442 << "number of hosts to use with auto deployment, 0 for max."
443 << " [" << opt::auto_depl::nhosts << "]\n";
445 << "toggle random initial load distribution"
446 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
448 std::clog << o("-r value")
449 << "random seed, 0 for using it on time()"
450 << " [" << opt::auto_depl::random_seed << "]\n";
452 std::clog << "\nLoad balancing algorithm\n";
453 std::clog << o("-a name") << "load balancing algorithm"
454 << " [" << opt::loba_algo << "]\n";
455 if (opt::help_requested > 1)
456 so_list(opt::loba_algorithms);
457 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
458 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
459 std::clog << o("-s value")
460 << "minimum duration between lb. iterations"
461 << " [" << opt::min_lb_iter_duration << "]\n";
463 std::clog << "\nApplication parameters\n";
464 std::clog << o("-c [aN,...]a0")
465 << "polynomial factors for computation cost"
466 << " [" << opt::comp_cost.to_string() << "]\n";
467 std::clog << o("-C [aN,...]a0")
468 << "polynomial factors for communication cost"
469 << " [" << opt::comm_cost.to_string() << "]\n";
470 std::clog << o("-m value")
471 << "minimum data transfer amount"
472 << " [" << opt::min_transfer_amount << "]\n";
473 std::clog << o("-M value")
474 << "maximum data transfer amount, 0 for no limit"
475 << " [" << opt::max_transfer_amount << "]\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 computations after given number of lb iterations"
481 << " [" << opt::comp_iter_delay << "]\n";
482 std::clog << o("-D value")
483 << "start computations after given time"
484 << " [" << opt::comp_time_delay << "]\n";
486 std::clog << "\nParameters for the end of the simulation\n";
487 std::clog << o("-i value")
488 << "maximum number of lb. iterations, 0 for no limit"
489 << " [" << opt::lb_maxiter << "]\n";
490 std::clog << o("-I value")
491 << "maximum number of comp. iterations, 0 for no limit"
492 << " [" << opt::comp_maxiter << "]\n";
493 std::clog << o("-t value")
494 << "time limit (simulated time), 0 for no limit"
495 << " [" << opt::time_limit << "]\n";
496 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
497 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
499 if (opt::help_requested < 3)
502 std::clog << "\nLogging support\n"
503 << " See SimGrid documentation on:\n"
504 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
505 << " Existing categories are:\n"
506 << " simu : root of following categories\n"
507 << " main : messages from global infrastructure\n"
508 << " depl : messages from auto deployment (inherited from main)\n"
509 << " comm : messages from asynchronous pipes\n"
510 << " proc : messages from base process class\n"
511 << " loba : messages from load-balancer\n"
512 << " thrd : messages from thread wrapper class\n";
514 // std::clog << "\nMiscellaneous low-level parameters\n";