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"
19 #define DATA_DESCR_WIDTH 42
25 // A sum of loads if considered null if it is less than
26 // load_ratio_threshold percent of the sum of loads at init.
27 const double load_ratio_threshold = 1e-4;
30 std::string program_name;
31 int help_requested = 0;
32 bool version_requested = false;
34 // Simulation parameters
37 // Platform and deployment
38 std::string platform_file;
39 std::string deployment_file;
41 // Automatic deployment
44 std::string topology("clique");
49 // Load balancing algorithm
50 std::string loba_algo("simple");
51 bool bookkeeping = false;
52 double min_lb_iter_duration = 1.0; // fixme: find better defaults
53 double min_transfer_amount = 0.0;
54 double max_transfer_amount = 0.0;
56 // Application parameters
57 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
58 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
59 double min_comp_iter_duration = 1.0; // fixme: find better defaults
60 unsigned comp_iter_delay = 0; // fixme: find better defaults
61 double comp_time_delay = 0.0; // fixme: find better defaults
63 // Parameters for the end of the simulation
64 unsigned lb_maxiter = 0;
65 unsigned comp_maxiter = 0;
66 double time_limit = 0;
67 bool exit_on_close = true;
69 // Named parameters lists
70 loba_algorithms_type loba_algorithms;
71 loba_algorithms_type::loba_algorithms_type()
73 NOL_INSERT("fairstrategy", "balance with fair strategy",
75 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
77 NOL_INSERT("none", "no load-balancing (for testing only)",
79 NOL_INSERT("simple", "balance with least loaded neighbor",
83 topologies_type topologies;
84 topologies_type::topologies_type()
86 NOL_INSERT("btree", "binary tree topology, initial load at root",
88 NOL_INSERT("clique", "all connected topology", deployment_clique);
89 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
90 NOL_INSERT("line", "line topology, initial load at one end",
92 NOL_INSERT("ring", "ring topology", deployment_ring);
93 NOL_INSERT("star", "star topology, initial load at center",
95 NOL_INSERT("torus", "torus topology", deployment_torus);
102 // local helper class
105 template <typename T>
106 static bool parse_arg(char opt, const char *arg, T& val);
107 static const char* on_off(bool b);
108 const char* descr(const char* str);
109 template <typename T>
110 const char* val_or_string(const T& val, const char* str,
112 template <typename T>
113 static bool nol_find_prefix(const T& nol, const char* descr,
117 std::string descr_str;
118 std::string val_or_string_str;
123 template <typename T>
124 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
126 std::istringstream str(arg);
127 bool result = (str >> val) && str.eof();
129 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
133 const char* opt_helper::on_off(bool b)
135 return b ? "on" : "off";
138 const char* opt_helper::descr(const char* str)
140 std::string& res = descr_str;
142 res.resize(DATA_DESCR_WIDTH, '.');
146 template <typename T>
147 const char* opt_helper::val_or_string(const T& val, const char* str,
150 std::string& res = val_or_string_str;
152 std::ostringstream oss;
161 template <typename T>
162 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
165 bool result = nol.exists(name);
167 std::stack<std::string> candidates;
168 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
169 const std::string& fullname = nol.get_name(it);
170 if (fullname.compare(0, name.length(), name) == 0)
171 candidates.push(fullname);
173 switch (candidates.size()) {
175 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
178 name = candidates.top();
181 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
184 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
185 while (!candidates.empty()) {
186 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
195 bool opt::parse_args(int* argc, char* argv[])
199 opt::program_name = argv[0];
200 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
202 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
206 while ((c = getopt(*argc, argv,
207 "a:bc:C:d:D:ehi:I:l:L:m:M:N:s:S:t:T:vV")) != -1) {
210 opt::loba_algo = optarg;
211 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
212 "load balancing algorithm",
217 opt::bookkeeping = !opt::bookkeeping;
220 opt::exit_on_close = !opt::exit_on_close;
223 opt::help_requested++;
227 opt::comp_cost = cost_func(optarg);
229 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
236 opt::comm_cost = cost_func(optarg);
238 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
244 PARSE_ARG(opt::comp_iter_delay);
247 PARSE_ARG(opt::comp_time_delay);
250 PARSE_ARG(opt::lb_maxiter);
253 PARSE_ARG(opt::comp_maxiter);
256 PARSE_ARG(opt::log_rate);
259 PARSE_ARG(opt::auto_depl::load);
262 PARSE_ARG(opt::min_transfer_amount);
265 PARSE_ARG(opt::max_transfer_amount);
268 PARSE_ARG(opt::auto_depl::nhosts);
271 PARSE_ARG(opt::min_lb_iter_duration);
274 PARSE_ARG(opt::min_comp_iter_duration);
277 PARSE_ARG(opt::time_limit);
280 opt::auto_depl::topology = optarg;
281 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
282 opt::auto_depl::topology)
286 // nothing to do: this option is checked at the very
287 // beginning of main()
290 opt::version_requested = true;
293 XBT_ERROR("invalid option -- '%c'", optopt);
301 if (opt::version_requested || opt::help_requested)
304 if (optind < *argc) {
305 opt::platform_file = argv[optind++];
307 XBT_ERROR("missing parameter -- <plaform_file>");
310 if (optind < *argc) {
311 opt::deployment_file = argv[optind++];
313 opt::auto_depl::enabled = opt::deployment_file.empty();
315 while (optind < *argc) {
316 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
320 if (opt::max_transfer_amount &&
321 opt::max_transfer_amount < opt::min_transfer_amount) {
322 XBT_ERROR("max. data transfer amount < min. data transfer amount");
333 #define DESCR(description, format, value) \
334 XBT_INFO("| %s: " format, h.descr(description), value)
336 XBT_INFO(",----[ Simulation parameters ]");
337 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
338 DESCR("platform file", "\"%s\"", platform_file.c_str());
339 if (auto_depl::enabled) {
340 XBT_INFO("| automatic deployment enabled");
341 DESCR("- topology", "%s", auto_depl::topology.c_str());
342 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
344 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
347 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
349 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
350 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
351 DESCR("minimum duration between lb. iterations", "%g",
352 min_lb_iter_duration);
353 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
354 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
355 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
356 DESCR("maximum data transfer amount", "%s",
357 h.val_or_string(max_transfer_amount, "no limit"));
358 DESCR("minimum duration between comp. iterations", "%g",
359 min_comp_iter_duration);
360 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
361 DESCR("computations start after time", "%g", comp_time_delay);
362 DESCR("maximum number of lb. iterations", "%s",
363 h.val_or_string(lb_maxiter, "no limit"));
364 DESCR("maximum number of comp. iterations", "%s",
365 h.val_or_string(comp_maxiter, "no limit"));
366 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
367 DESCR("exit on close", "%s", h.on_off(exit_on_close));
376 #define o(opt) " " << std::setw(14) \
377 << std::left << (opt) << std::right << " "
379 #define so(subopt) std::setw(18) << (subopt) << " : "
381 #define so_list(name) do { \
382 name ## _type::iterator it; \
383 for (it = name.begin() ; it != name.end() ; ++it) \
384 std::clog << so(name.get_name(it)) \
385 << name.get_descr(it) << "\n"; \
389 std::clog << "Usage: " << opt::program_name
390 << " [options] <platform_file> [<deployment_file>]\n";
392 std::clog << "\nGlobal options\n";
394 << "print help and exit (use -hh or -hhh for extended help)\n";
395 if (opt::help_requested < 1)
398 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
399 std::clog << o("-V") << "print version and exit\n";
401 std::clog << "\nSimulation parameters\n";
402 std::clog << o("-l value")
403 << "print current load every n lb iterations, 0 to disable"
404 << " [" << opt::log_rate << "]\n";
406 << "verbose: do not override the default logging parameters\n";
408 std::clog << "\nAutomatic deployment options\n";
409 std::clog << o("-T name")
410 << "enable automatic deployment with selected topology"
411 << " [" << opt::auto_depl::topology << "]\n";
412 if (opt::help_requested > 1)
413 so_list(opt::topologies);
414 std::clog << o("-L value")
415 << "total load with auto deployment, 0 for number of hosts"
416 << " [" << opt::auto_depl::load << "]\n";
417 std::clog << o("-N value")
418 << "number of hosts to use with auto deployment, 0 for max."
419 << " [" << opt::auto_depl::nhosts << "]\n";
421 std::clog << "\nLoad balancing algorithm\n";
422 std::clog << o("-a name") << "load balancing algorithm"
423 << " [" << opt::loba_algo << "]\n";
424 if (opt::help_requested > 1)
425 so_list(opt::loba_algorithms);
426 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
427 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
428 std::clog << o("-s value")
429 << "minimum duration between lb. iterations"
430 << " [" << opt::min_lb_iter_duration << "]\n";
432 std::clog << "\nApplication parameters\n";
433 std::clog << o("-c [aN,...]a0")
434 << "polynomial factors for computation cost"
435 << " [" << opt::comp_cost.to_string() << "]\n";
436 std::clog << o("-C [aN,...]a0")
437 << "polynomial factors for communication cost"
438 << " [" << opt::comm_cost.to_string() << "]\n";
439 std::clog << o("-m value")
440 << "minimum data transfer amount"
441 << " [" << opt::min_transfer_amount << "]\n";
442 std::clog << o("-M value")
443 << "maximum data transfer amount, 0 for no limit"
444 << " [" << opt::max_transfer_amount << "]\n";
445 std::clog << o("-S value")
446 << "minimum duration between comp. iterations"
447 << " [" << opt::min_comp_iter_duration << "]\n";
448 std::clog << o("-d value")
449 << "start computations after given number of lb iterations"
450 << " [" << opt::comp_iter_delay << "]\n";
451 std::clog << o("-D value")
452 << "start computations after given time"
453 << " [" << opt::comp_time_delay << "]\n";
455 std::clog << "\nParameters for the end of the simulation\n";
456 std::clog << o("-i value")
457 << "maximum number of lb. iterations, 0 for no limit"
458 << " [" << opt::lb_maxiter << "]\n";
459 std::clog << o("-I value")
460 << "maximum number of comp. iterations, 0 for no limit"
461 << " [" << opt::comp_maxiter << "]\n";
462 std::clog << o("-t value")
463 << "time limit (simulated time), 0 for no limit"
464 << " [" << opt::time_limit << "]\n";
465 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
466 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
468 if (opt::help_requested < 3)
471 std::clog << "\nLogging support\n"
472 << " See SimGrid documentation on:\n"
473 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
474 << " Existing categories are:\n"
475 << " simu : root of following categories\n"
476 << " main : messages from global infrastructure\n"
477 << " depl : messages from auto deployment (inherited from main)\n"
478 << " comm : messages from asynchronous pipes\n"
479 << " proc : messages from base process class\n"
480 << " loba : messages from load-balancer\n"
481 << " thrd : messages from thread wrapper class\n";
483 // std::clog << "\nMiscellaneous low-level parameters\n";