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
39 // Platform and deployment
40 std::string platform_file;
41 std::string deployment_file;
43 // Automatic deployment
46 std::string topology("clique");
51 // Load balancing algorithm
52 std::string loba_algo("simple");
53 bool bookkeeping = false;
54 double min_lb_iter_duration = 1.0; // fixme: find better defaults
55 double min_transfer_amount = 0.0;
56 double max_transfer_amount = 0.0;
58 // Application parameters
59 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
60 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
61 double min_comp_iter_duration = 1.0; // fixme: find better defaults
62 unsigned comp_iter_delay = 0; // fixme: find better defaults
63 double comp_time_delay = 0.0; // fixme: find better defaults
65 // Parameters for the end of the simulation
66 unsigned lb_maxiter = 0;
67 unsigned comp_maxiter = 0;
68 double time_limit = 0;
69 bool exit_on_close = true;
71 // Named parameters lists
72 loba_algorithms_type loba_algorithms;
73 loba_algorithms_type::loba_algorithms_type()
75 NOL_INSERT("besteffort", "balance with best effort strategy",
77 NOL_INSERT("fairstrategy", "balance with fair strategy",
79 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
81 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
83 NOL_INSERT("none", "no load-balancing (for testing only)",
85 NOL_INSERT("simple", "balance with least loaded neighbor",
89 topologies_type topologies;
90 topologies_type::topologies_type()
92 NOL_INSERT("btree", "binary tree topology, initial load at root",
94 NOL_INSERT("clique", "all connected topology", deployment_clique);
95 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
96 NOL_INSERT("line", "line topology, initial load at one end",
98 NOL_INSERT("ring", "ring topology", deployment_ring);
99 NOL_INSERT("star", "star topology, initial load at center",
101 NOL_INSERT("torus", "torus topology", deployment_torus);
108 // local helper class
111 template <typename T>
112 static bool parse_arg(char opt, const char *arg, T& val);
113 static const char* on_off(bool b);
114 const char* descr(const char* str);
115 template <typename T>
116 const char* val_or_string(const T& val, const char* str,
118 template <typename T>
119 static bool nol_find_prefix(const T& nol, const char* descr,
123 std::string descr_str;
124 std::string val_or_string_str;
129 template <typename T>
130 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
132 std::istringstream str(arg);
133 bool result = (str >> val) && str.eof();
135 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
139 const char* opt_helper::on_off(bool b)
141 return b ? "on" : "off";
144 const char* opt_helper::descr(const char* str)
146 std::string& res = descr_str;
148 res.resize(DATA_DESCR_WIDTH, '.');
152 template <typename T>
153 const char* opt_helper::val_or_string(const T& val, const char* str,
156 std::string& res = val_or_string_str;
158 std::ostringstream oss;
167 template <typename T>
168 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
171 bool result = nol.exists(name);
173 std::stack<std::string> candidates;
174 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
175 const std::string& fullname = nol.get_name(it);
176 if (fullname.compare(0, name.length(), name) == 0)
177 candidates.push(fullname);
179 switch (candidates.size()) {
181 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
184 name = candidates.top();
187 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
190 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
191 while (!candidates.empty()) {
192 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
201 bool opt::parse_args(int* argc, char* argv[])
205 opt::program_name = argv[0];
206 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
208 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
212 while ((c = getopt(*argc, argv,
213 "a:bc:C:d:D:ehi:I:l:L:m:M:N:s:S:t:T:vV")) != -1) {
216 opt::loba_algo = optarg;
217 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
218 "load balancing algorithm",
223 opt::bookkeeping = !opt::bookkeeping;
226 opt::exit_on_close = !opt::exit_on_close;
229 opt::help_requested++;
233 opt::comp_cost = cost_func(optarg);
235 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
242 opt::comm_cost = cost_func(optarg);
244 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
250 PARSE_ARG(opt::comp_iter_delay);
253 PARSE_ARG(opt::comp_time_delay);
256 PARSE_ARG(opt::lb_maxiter);
259 PARSE_ARG(opt::comp_maxiter);
262 PARSE_ARG(opt::log_rate);
265 PARSE_ARG(opt::auto_depl::load);
268 PARSE_ARG(opt::min_transfer_amount);
271 PARSE_ARG(opt::max_transfer_amount);
274 PARSE_ARG(opt::auto_depl::nhosts);
277 PARSE_ARG(opt::min_lb_iter_duration);
280 PARSE_ARG(opt::min_comp_iter_duration);
283 PARSE_ARG(opt::time_limit);
286 opt::auto_depl::topology = optarg;
287 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
288 opt::auto_depl::topology)
292 // nothing to do: this option is checked at the very
293 // beginning of main()
296 opt::version_requested = true;
299 XBT_ERROR("invalid option -- '%c'", optopt);
307 if (opt::version_requested || opt::help_requested)
310 if (optind < *argc) {
311 opt::platform_file = argv[optind++];
313 XBT_ERROR("missing parameter -- <plaform_file>");
316 if (optind < *argc) {
317 opt::deployment_file = argv[optind++];
319 opt::auto_depl::enabled = opt::deployment_file.empty();
321 while (optind < *argc) {
322 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
326 if (opt::max_transfer_amount &&
327 opt::max_transfer_amount < opt::min_transfer_amount) {
328 XBT_ERROR("max. data transfer amount < min. data transfer amount");
339 #define DESCR(description, format, value) \
340 XBT_INFO("| %s: " format, h.descr(description), value)
342 XBT_INFO(",----[ Simulation parameters ]");
343 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
344 DESCR("platform file", "\"%s\"", platform_file.c_str());
345 if (auto_depl::enabled) {
346 XBT_INFO("| automatic deployment enabled");
347 DESCR("- topology", "%s", auto_depl::topology.c_str());
348 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
350 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
353 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
355 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
356 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
357 DESCR("minimum duration between lb. iterations", "%g",
358 min_lb_iter_duration);
359 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
360 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
361 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
362 DESCR("maximum data transfer amount", "%s",
363 h.val_or_string(max_transfer_amount, "no limit"));
364 DESCR("minimum duration between comp. iterations", "%g",
365 min_comp_iter_duration);
366 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
367 DESCR("computations start after time", "%g", comp_time_delay);
368 DESCR("maximum number of lb. iterations", "%s",
369 h.val_or_string(lb_maxiter, "no limit"));
370 DESCR("maximum number of comp. iterations", "%s",
371 h.val_or_string(comp_maxiter, "no limit"));
372 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
373 DESCR("exit on close", "%s", h.on_off(exit_on_close));
382 #define o(opt) " " << std::setw(14) \
383 << std::left << (opt) << std::right << " "
385 #define so(subopt) std::setw(18) << (subopt) << " : "
387 #define so_list(name) do { \
388 name ## _type::iterator it; \
389 for (it = name.begin() ; it != name.end() ; ++it) \
390 std::clog << so(name.get_name(it)) \
391 << name.get_descr(it) << "\n"; \
395 std::clog << "Usage: " << opt::program_name
396 << " [options] <platform_file> [<deployment_file>]\n";
398 std::clog << "\nGlobal options\n";
400 << "print help and exit (use -hh or -hhh for extended help)\n";
401 if (opt::help_requested < 1)
404 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
405 std::clog << o("-V") << "print version and exit\n";
407 std::clog << "\nSimulation parameters\n";
408 std::clog << o("-l value")
409 << "print current load every n lb iterations, 0 to disable"
410 << " [" << opt::log_rate << "]\n";
412 << "verbose: do not override the default logging parameters\n";
414 std::clog << "\nAutomatic deployment options\n";
415 std::clog << o("-T name")
416 << "enable automatic deployment with selected topology"
417 << " [" << opt::auto_depl::topology << "]\n";
418 if (opt::help_requested > 1)
419 so_list(opt::topologies);
420 std::clog << o("-L value")
421 << "total load with auto deployment, 0 for number of hosts"
422 << " [" << opt::auto_depl::load << "]\n";
423 std::clog << o("-N value")
424 << "number of hosts to use with auto deployment, 0 for max."
425 << " [" << opt::auto_depl::nhosts << "]\n";
427 std::clog << "\nLoad balancing algorithm\n";
428 std::clog << o("-a name") << "load balancing algorithm"
429 << " [" << opt::loba_algo << "]\n";
430 if (opt::help_requested > 1)
431 so_list(opt::loba_algorithms);
432 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
433 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
434 std::clog << o("-s value")
435 << "minimum duration between lb. iterations"
436 << " [" << opt::min_lb_iter_duration << "]\n";
438 std::clog << "\nApplication parameters\n";
439 std::clog << o("-c [aN,...]a0")
440 << "polynomial factors for computation cost"
441 << " [" << opt::comp_cost.to_string() << "]\n";
442 std::clog << o("-C [aN,...]a0")
443 << "polynomial factors for communication cost"
444 << " [" << opt::comm_cost.to_string() << "]\n";
445 std::clog << o("-m value")
446 << "minimum data transfer amount"
447 << " [" << opt::min_transfer_amount << "]\n";
448 std::clog << o("-M value")
449 << "maximum data transfer amount, 0 for no limit"
450 << " [" << opt::max_transfer_amount << "]\n";
451 std::clog << o("-S value")
452 << "minimum duration between comp. iterations"
453 << " [" << opt::min_comp_iter_duration << "]\n";
454 std::clog << o("-d value")
455 << "start computations after given number of lb iterations"
456 << " [" << opt::comp_iter_delay << "]\n";
457 std::clog << o("-D value")
458 << "start computations after given time"
459 << " [" << opt::comp_time_delay << "]\n";
461 std::clog << "\nParameters for the end of the simulation\n";
462 std::clog << o("-i value")
463 << "maximum number of lb. iterations, 0 for no limit"
464 << " [" << opt::lb_maxiter << "]\n";
465 std::clog << o("-I value")
466 << "maximum number of comp. iterations, 0 for no limit"
467 << " [" << opt::comp_maxiter << "]\n";
468 std::clog << o("-t value")
469 << "time limit (simulated time), 0 for no limit"
470 << " [" << opt::time_limit << "]\n";
471 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
472 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
474 if (opt::help_requested < 3)
477 std::clog << "\nLogging support\n"
478 << " See SimGrid documentation on:\n"
479 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
480 << " Existing categories are:\n"
481 << " simu : root of following categories\n"
482 << " main : messages from global infrastructure\n"
483 << " depl : messages from auto deployment (inherited from main)\n"
484 << " comm : messages from asynchronous pipes\n"
485 << " proc : messages from base process class\n"
486 << " loba : messages from load-balancer\n"
487 << " thrd : messages from thread wrapper class\n";
489 // std::clog << "\nMiscellaneous low-level parameters\n";