5 #include <unistd.h> // getopt
8 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
10 #include "deployment.h"
12 #include "loba_besteffort.h"
13 #include "loba_fairstrategy.h"
14 #include "loba_makhoul.h"
15 #include "loba_makhoul2.h"
16 #include "loba_simple.h"
21 #define DATA_DESCR_WIDTH 42
27 // A sum of loads if considered null if it is less than
28 // load_ratio_threshold percent of the sum of loads at init.
29 const double load_ratio_threshold = 1e-4;
32 std::string program_name;
33 int help_requested = 0;
34 bool version_requested = false;
36 // Simulation parameters
38 bool exit_request = false;
40 // Platform and deployment
41 std::string platform_file;
42 std::string deployment_file;
44 // Automatic deployment
47 std::string topology("clique");
52 // Load balancing algorithm
53 std::string loba_algo("simple");
54 bool bookkeeping = false;
55 double min_lb_iter_duration = 1.0; // fixme: find better defaults
56 double min_transfer_amount = 0.0;
57 double max_transfer_amount = 0.0;
59 // Application parameters
60 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
61 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
62 double min_comp_iter_duration = 1.0; // fixme: find better defaults
63 unsigned comp_iter_delay = 0; // fixme: find better defaults
64 double comp_time_delay = 0.0; // fixme: find better defaults
66 // Parameters for the end of the simulation
67 unsigned lb_maxiter = 0;
68 unsigned comp_maxiter = 0;
69 double time_limit = 0;
70 bool exit_on_close = true;
72 // Named parameters lists
73 loba_algorithms_type loba_algorithms;
74 loba_algorithms_type::loba_algorithms_type()
76 NOL_INSERT("besteffort", "balance with best effort strategy",
78 NOL_INSERT("fairstrategy", "balance with fair strategy",
80 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
82 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
84 NOL_INSERT("none", "no load-balancing (for testing only)",
86 NOL_INSERT("simple", "balance with least loaded neighbor",
90 topologies_type topologies;
91 topologies_type::topologies_type()
93 NOL_INSERT("btree", "binary tree topology, initial load at root",
95 NOL_INSERT("clique", "all connected topology", deployment_clique);
96 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
97 NOL_INSERT("line", "line topology, initial load at one end",
99 NOL_INSERT("ring", "ring topology", deployment_ring);
100 NOL_INSERT("star", "star topology, initial load at center",
102 NOL_INSERT("torus", "torus topology", deployment_torus);
109 // local helper class
112 template <typename T>
113 static bool parse_arg(char opt, const char *arg, T& val);
114 static const char* on_off(bool b);
115 const char* descr(const char* str);
116 template <typename T>
117 const char* val_or_string(const T& val, const char* str,
119 template <typename T>
120 static bool nol_find_prefix(const T& nol, const char* descr,
124 std::string descr_str;
125 std::string val_or_string_str;
130 template <typename T>
131 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
133 std::istringstream str(arg);
134 bool result = (str >> val) && str.eof();
136 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
140 const char* opt_helper::on_off(bool b)
142 return b ? "on" : "off";
145 const char* opt_helper::descr(const char* str)
147 std::string& res = descr_str;
149 res.resize(DATA_DESCR_WIDTH, '.');
153 template <typename T>
154 const char* opt_helper::val_or_string(const T& val, const char* str,
157 std::string& res = val_or_string_str;
159 std::ostringstream oss;
168 template <typename T>
169 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
172 bool result = nol.exists(name);
174 std::stack<std::string> candidates;
175 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
176 const std::string& fullname = nol.get_name(it);
177 if (fullname.compare(0, name.length(), name) == 0)
178 candidates.push(fullname);
180 switch (candidates.size()) {
182 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
185 name = candidates.top();
188 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
191 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
192 while (!candidates.empty()) {
193 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
202 bool opt::parse_args(int* argc, char* argv[])
206 opt::program_name = argv[0];
207 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
209 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
213 while ((c = getopt(*argc, argv,
214 "a:bc:C:d:D:ehi:I:l:L:m:M:N:s:S:t:T:vV")) != -1) {
217 opt::loba_algo = optarg;
218 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
219 "load balancing algorithm",
224 opt::bookkeeping = !opt::bookkeeping;
227 opt::exit_on_close = !opt::exit_on_close;
230 opt::help_requested++;
234 opt::comp_cost = cost_func(optarg);
236 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
243 opt::comm_cost = cost_func(optarg);
245 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
251 PARSE_ARG(opt::comp_iter_delay);
254 PARSE_ARG(opt::comp_time_delay);
257 PARSE_ARG(opt::lb_maxiter);
260 PARSE_ARG(opt::comp_maxiter);
263 PARSE_ARG(opt::log_rate);
266 PARSE_ARG(opt::auto_depl::load);
269 PARSE_ARG(opt::min_transfer_amount);
272 PARSE_ARG(opt::max_transfer_amount);
275 PARSE_ARG(opt::auto_depl::nhosts);
278 PARSE_ARG(opt::min_lb_iter_duration);
281 PARSE_ARG(opt::min_comp_iter_duration);
284 PARSE_ARG(opt::time_limit);
287 opt::auto_depl::topology = optarg;
288 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
289 opt::auto_depl::topology)
293 // nothing to do: this option is checked at the very
294 // beginning of main()
297 opt::version_requested = true;
300 XBT_ERROR("invalid option -- '%c'", optopt);
308 if (opt::version_requested || opt::help_requested)
311 if (optind < *argc) {
312 opt::platform_file = argv[optind++];
314 XBT_ERROR("missing parameter -- <plaform_file>");
317 if (optind < *argc) {
318 opt::deployment_file = argv[optind++];
320 opt::auto_depl::enabled = opt::deployment_file.empty();
322 while (optind < *argc) {
323 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
327 if (opt::max_transfer_amount &&
328 opt::max_transfer_amount < opt::min_transfer_amount) {
329 XBT_ERROR("max. data transfer amount < min. data transfer amount");
340 #define DESCR(description, format, value) \
341 XBT_INFO("| %s: " format, h.descr(description), value)
343 XBT_INFO(",----[ Simulation parameters ]");
344 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
345 DESCR("platform file", "\"%s\"", platform_file.c_str());
346 if (auto_depl::enabled) {
347 XBT_INFO("| automatic deployment enabled");
348 DESCR("- topology", "%s", auto_depl::topology.c_str());
349 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
351 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
354 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
356 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
357 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
358 DESCR("minimum duration between lb. iterations", "%g",
359 min_lb_iter_duration);
360 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
361 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
362 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
363 DESCR("maximum data transfer amount", "%s",
364 h.val_or_string(max_transfer_amount, "no limit"));
365 DESCR("minimum duration between comp. iterations", "%g",
366 min_comp_iter_duration);
367 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
368 DESCR("computations start after time", "%g", comp_time_delay);
369 DESCR("maximum number of lb. iterations", "%s",
370 h.val_or_string(lb_maxiter, "no limit"));
371 DESCR("maximum number of comp. iterations", "%s",
372 h.val_or_string(comp_maxiter, "no limit"));
373 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
374 DESCR("exit on close", "%s", h.on_off(exit_on_close));
383 #define o(opt) " " << std::setw(14) \
384 << std::left << (opt) << std::right << " "
386 #define so(subopt) std::setw(18) << (subopt) << " : "
388 #define so_list(name) do { \
389 name ## _type::iterator it; \
390 for (it = name.begin() ; it != name.end() ; ++it) \
391 std::clog << so(name.get_name(it)) \
392 << name.get_descr(it) << "\n"; \
396 std::clog << "Usage: " << opt::program_name
397 << " [options] <platform_file> [<deployment_file>]\n";
399 std::clog << "\nGlobal options\n";
401 << "print help and exit (use -hh or -hhh for extended help)\n";
402 if (opt::help_requested < 1)
405 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
406 std::clog << o("-V") << "print version and exit\n";
408 std::clog << "\nSimulation parameters\n";
409 std::clog << o("-l value")
410 << "print current load every n lb iterations, 0 to disable"
411 << " [" << opt::log_rate << "]\n";
413 << "verbose: do not override the default logging parameters\n";
415 std::clog << "\nAutomatic deployment options\n";
416 std::clog << o("-T name")
417 << "enable automatic deployment with selected topology"
418 << " [" << opt::auto_depl::topology << "]\n";
419 if (opt::help_requested > 1)
420 so_list(opt::topologies);
421 std::clog << o("-L value")
422 << "total load with auto deployment, 0 for number of hosts"
423 << " [" << opt::auto_depl::load << "]\n";
424 std::clog << o("-N value")
425 << "number of hosts to use with auto deployment, 0 for max."
426 << " [" << opt::auto_depl::nhosts << "]\n";
428 std::clog << "\nLoad balancing algorithm\n";
429 std::clog << o("-a name") << "load balancing algorithm"
430 << " [" << opt::loba_algo << "]\n";
431 if (opt::help_requested > 1)
432 so_list(opt::loba_algorithms);
433 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
434 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
435 std::clog << o("-s value")
436 << "minimum duration between lb. iterations"
437 << " [" << opt::min_lb_iter_duration << "]\n";
439 std::clog << "\nApplication parameters\n";
440 std::clog << o("-c [aN,...]a0")
441 << "polynomial factors for computation cost"
442 << " [" << opt::comp_cost.to_string() << "]\n";
443 std::clog << o("-C [aN,...]a0")
444 << "polynomial factors for communication cost"
445 << " [" << opt::comm_cost.to_string() << "]\n";
446 std::clog << o("-m value")
447 << "minimum data transfer amount"
448 << " [" << opt::min_transfer_amount << "]\n";
449 std::clog << o("-M value")
450 << "maximum data transfer amount, 0 for no limit"
451 << " [" << opt::max_transfer_amount << "]\n";
452 std::clog << o("-S value")
453 << "minimum duration between comp. iterations"
454 << " [" << opt::min_comp_iter_duration << "]\n";
455 std::clog << o("-d value")
456 << "start computations after given number of lb iterations"
457 << " [" << opt::comp_iter_delay << "]\n";
458 std::clog << o("-D value")
459 << "start computations after given time"
460 << " [" << opt::comp_time_delay << "]\n";
462 std::clog << "\nParameters for the end of the simulation\n";
463 std::clog << o("-i value")
464 << "maximum number of lb. iterations, 0 for no limit"
465 << " [" << opt::lb_maxiter << "]\n";
466 std::clog << o("-I value")
467 << "maximum number of comp. iterations, 0 for no limit"
468 << " [" << opt::comp_maxiter << "]\n";
469 std::clog << o("-t value")
470 << "time limit (simulated time), 0 for no limit"
471 << " [" << opt::time_limit << "]\n";
472 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
473 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
475 if (opt::help_requested < 3)
478 std::clog << "\nLogging support\n"
479 << " See SimGrid documentation on:\n"
480 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
481 << " Existing categories are:\n"
482 << " simu : root of following categories\n"
483 << " main : messages from global infrastructure\n"
484 << " depl : messages from auto deployment (inherited from main)\n"
485 << " comm : messages from asynchronous pipes\n"
486 << " proc : messages from base process class\n"
487 << " loba : messages from load-balancer\n"
488 << " thrd : messages from thread wrapper class\n";
490 // std::clog << "\nMiscellaneous low-level parameters\n";