5 #include <unistd.h> // getopt
8 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
10 #include "deployment.h"
12 #include "loba_simple.h"
13 #include "loba_fairstrategy.h"
14 #include "loba_makhoul.h"
23 // A sum of loads if considered null if it is less than
24 // load_ratio_threshold percent of the sum of loads at init.
25 const double load_ratio_threshold = 1e-4;
28 std::string program_name;
29 int help_requested = 0;
30 bool version_requested = false;
32 // Simulation parameters
35 // Platform and deployment
36 std::string platform_file;
37 std::string deployment_file;
39 // Automatic deployment
42 std::string topology("clique");
47 // Load balancing algorithm
48 std::string loba_algo("simple");
49 bool bookkeeping = false;
50 double min_lb_iter_duration = 1.0; // fixme: find better defaults
51 double min_transfer_amount = 0.0;
52 double max_transfer_amount = 0.0;
54 // Application parameters
55 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
56 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
57 double min_comp_iter_duration = 1.0; // fixme: find better defaults
58 unsigned comp_iter_delay = 0; // fixme: find better defaults
59 double comp_time_delay = 0.0; // fixme: find better defaults
61 // Parameters for the end of the simulation
62 unsigned lb_maxiter = 0;
63 unsigned comp_maxiter = 0;
64 double time_limit = 0;
65 bool exit_on_close = true;
67 // Named parameters lists
68 loba_algorithms_type loba_algorithms;
69 loba_algorithms_type::loba_algorithms_type()
71 NOL_INSERT("fairstrategy", "balance with fair strategy",
73 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
75 NOL_INSERT("none", "no load-balancing (for testing only)",
77 NOL_INSERT("simple", "balance with least loaded neighbor",
81 topologies_type topologies;
82 topologies_type::topologies_type()
84 NOL_INSERT("btree", "binary tree topology, initial load at root",
86 NOL_INSERT("clique", "all connected topology", deployment_clique);
87 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
88 NOL_INSERT("line", "line topology, initial load at one end",
90 NOL_INSERT("ring", "ring topology", deployment_ring);
91 NOL_INSERT("star", "star topology, initial load at center",
93 NOL_INSERT("torus", "torus topology", deployment_torus);
100 // local helper class
103 template <typename T>
104 static bool parse_arg(char opt, const char *arg, T& val);
105 static const char* on_off(bool b);
106 const char* descr(const char* str);
107 template <typename T>
108 const char* val_or_string(const T& val, const char* str,
110 template <typename T>
111 static bool nol_find_prefix(const T& nol, const char* descr,
115 std::string descr_str;
116 std::string val_or_string_str;
121 template <typename T>
122 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
124 std::istringstream str(arg);
125 bool result = (str >> val) && str.eof();
127 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
131 const char* opt_helper::on_off(bool b)
133 return b ? "on" : "off";
136 const char* opt_helper::descr(const char* str)
138 const int descr_width = 40;
139 std::string& res = descr_str;
141 res.resize(descr_width, '.');
145 template <typename T>
146 const char* opt_helper::val_or_string(const T& val, const char* str,
149 std::string& res = val_or_string_str;
151 std::ostringstream oss;
160 template <typename T>
161 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
164 bool result = nol.exists(name);
166 std::stack<std::string> candidates;
167 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
168 const std::string& fullname = nol.get_name(it);
169 if (fullname.compare(0, name.length(), name) == 0)
170 candidates.push(fullname);
172 switch (candidates.size()) {
174 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
177 name = candidates.top();
180 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
183 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
184 while (!candidates.empty()) {
185 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
194 bool opt::parse_args(int* argc, char* argv[])
198 opt::program_name = argv[0];
199 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
201 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
205 while ((c = getopt(*argc, argv,
206 "a:bc:C:d:D:ehi:I:l:L:m:M:N:s:S:t:T:vV")) != -1) {
209 opt::loba_algo = optarg;
210 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
211 "load balancing algorithm",
216 opt::bookkeeping = !opt::bookkeeping;
219 opt::exit_on_close = !opt::exit_on_close;
222 opt::help_requested++;
226 opt::comp_cost = cost_func(optarg);
228 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
235 opt::comm_cost = cost_func(optarg);
237 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
243 PARSE_ARG(opt::comp_iter_delay);
246 PARSE_ARG(opt::comp_time_delay);
249 PARSE_ARG(opt::lb_maxiter);
252 PARSE_ARG(opt::comp_maxiter);
255 PARSE_ARG(opt::log_rate);
258 PARSE_ARG(opt::auto_depl::load);
261 PARSE_ARG(opt::min_transfer_amount);
264 PARSE_ARG(opt::max_transfer_amount);
267 PARSE_ARG(opt::auto_depl::nhosts);
270 PARSE_ARG(opt::min_lb_iter_duration);
273 PARSE_ARG(opt::min_comp_iter_duration);
276 PARSE_ARG(opt::time_limit);
279 opt::auto_depl::topology = optarg;
280 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
281 opt::auto_depl::topology)
285 // nothing to do: this option is checked at the very
286 // beginning of main()
289 opt::version_requested = true;
292 XBT_ERROR("invalid option -- '%c'", optopt);
300 if (opt::version_requested || opt::help_requested)
303 if (optind < *argc) {
304 opt::platform_file = argv[optind++];
306 XBT_ERROR("missing parameter -- <plaform_file>");
309 if (optind < *argc) {
310 opt::deployment_file = argv[optind++];
312 opt::auto_depl::enabled = opt::deployment_file.empty();
314 while (optind < *argc) {
315 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
319 if (opt::max_transfer_amount &&
320 opt::max_transfer_amount < opt::min_transfer_amount) {
321 XBT_ERROR("max. data transfer amount < min. data transfer amount");
332 #define DESCR(description, format, value) \
333 XBT_INFO("| %s: " format, h.descr(description), value)
335 XBT_INFO(",----[ Simulation parameters ]");
336 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
337 DESCR("platform file", "\"%s\"", platform_file.c_str());
338 if (auto_depl::enabled) {
339 XBT_INFO("| automatic deployment enabled");
340 DESCR("- topology", "%s", auto_depl::topology.c_str());
341 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
343 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
346 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
348 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
349 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
350 DESCR("minimum duration between lb. iterations", "%g",
351 min_lb_iter_duration);
352 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
353 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
354 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
355 DESCR("maximum data transfer amount", "%g", max_transfer_amount);
356 DESCR("minimum duration between comp. iterations", "%g",
357 min_comp_iter_duration);
358 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
359 DESCR("computations start after time", "%g", comp_time_delay);
360 DESCR("maximum number of lb. iterations", "%s",
361 h.val_or_string(lb_maxiter, "infinity"));
362 DESCR("maximum number of comp. iterations", "%s",
363 h.val_or_string(comp_maxiter, "infinity"));
364 DESCR("time limit", "%s", h.val_or_string(time_limit, "infinity"));
365 DESCR("exit on close", "%s", h.on_off(exit_on_close));
374 #define o(opt) " " << std::setw(14) \
375 << std::left << (opt) << std::right << " "
377 #define so(subopt) std::setw(18) << (subopt) << " : "
379 #define so_list(name) do { \
380 name ## _type::iterator it; \
381 for (it = name.begin() ; it != name.end() ; ++it) \
382 std::clog << so(name.get_name(it)) \
383 << name.get_descr(it) << "\n"; \
387 std::clog << "Usage: " << opt::program_name
388 << " [options] <platform_file> [<deployment_file>]\n";
390 std::clog << "\nGlobal options\n";
392 << "print help and exit (use -hh or -hhh for extended help)\n";
393 if (opt::help_requested < 1)
396 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
397 std::clog << o("-V") << "print version and exit\n";
399 std::clog << "\nSimulation parameters\n";
400 std::clog << o("-l value")
401 << "print current load every n lb iterations, 0 to disable"
402 << " [" << opt::log_rate << "]\n";
404 << "verbose: do not override the default logging parameters\n";
406 std::clog << "\nAutomatic deployment options\n";
407 std::clog << o("-T name")
408 << "enable automatic deployment with selected topology"
409 << " [" << opt::auto_depl::topology << "]\n";
410 if (opt::help_requested > 1)
411 so_list(opt::topologies);
412 std::clog << o("-L value")
413 << "total load with auto deployment, 0 for number of hosts"
414 << " [" << opt::auto_depl::load << "]\n";
415 std::clog << o("-N value")
416 << "number of hosts to use with auto deployment, 0 for max."
417 << " [" << opt::auto_depl::nhosts << "]\n";
419 std::clog << "\nLoad balancing algorithm\n";
420 std::clog << o("-a name") << "load balancing algorithm"
421 << " [" << opt::loba_algo << "]\n";
422 if (opt::help_requested > 1)
423 so_list(opt::loba_algorithms);
424 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
425 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
426 std::clog << o("-s value")
427 << "minimum duration between lb. iterations"
428 << " [" << opt::min_lb_iter_duration << "]\n";
430 std::clog << "\nApplication parameters\n";
431 std::clog << o("-c [aN,...]a0")
432 << "polynomial factors for computation cost"
433 << " [" << opt::comp_cost.to_string() << "]\n";
434 std::clog << o("-C [aN,...]a0")
435 << "polynomial factors for communication cost"
436 << " [" << opt::comm_cost.to_string() << "]\n";
437 std::clog << o("-m value")
438 << "minimum data transfer amount"
439 << " [" << opt::min_transfer_amount << "]\n";
440 std::clog << o("-M value")
441 << "maximum data transfer amount (0 for no limit)"
442 << " [" << opt::max_transfer_amount << "]\n";
443 std::clog << o("-S value")
444 << "minimum duration between comp. iterations"
445 << " [" << opt::min_comp_iter_duration << "]\n";
446 std::clog << o("-d value")
447 << "start computations after given number of lb iterations"
448 << " [" << opt::comp_iter_delay << "]\n";
449 std::clog << o("-D value")
450 << "start computations after given time"
451 << " [" << opt::comp_time_delay << "]\n";
453 std::clog << "\nParameters for the end of the simulation\n";
454 std::clog << o("-i value")
455 << "maximum number of lb. iterations, 0 for infinity"
456 << " [" << opt::lb_maxiter << "]\n";
457 std::clog << o("-I value")
458 << "maximum number of comp. iterations, 0 for infinity"
459 << " [" << opt::comp_maxiter << "]\n";
460 std::clog << o("-t value")
461 << "time limit (simulated time), 0 for infinity"
462 << " [" << opt::time_limit << "]\n";
463 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
464 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
466 if (opt::help_requested < 3)
469 std::clog << "\nLogging support\n"
470 << " See SimGrid documentation on:\n"
471 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
472 << " Existing categories are:\n"
473 << " simu : root of following categories\n"
474 << " main : messages from global infrastructure\n"
475 << " depl : messages from auto deployment (inherited from main)\n"
476 << " comm : messages from asynchronous pipes\n"
477 << " proc : messages from base process class\n"
478 << " loba : messages from load-balancer\n"
479 << " thrd : messages from thread wrapper class\n";
481 // std::clog << "\nMiscellaneous low-level parameters\n";