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"
15 #include "loba_makhoul2.h"
20 #define DATA_DESCR_WIDTH 42
26 // A sum of loads if considered null if it is less than
27 // load_ratio_threshold percent of the sum of loads at init.
28 const double load_ratio_threshold = 1e-4;
31 std::string program_name;
32 int help_requested = 0;
33 bool version_requested = false;
35 // Simulation parameters
38 // Platform and deployment
39 std::string platform_file;
40 std::string deployment_file;
42 // Automatic deployment
45 std::string topology("clique");
50 // Load balancing algorithm
51 std::string loba_algo("simple");
52 bool bookkeeping = false;
53 double min_lb_iter_duration = 1.0; // fixme: find better defaults
54 double min_transfer_amount = 0.0;
55 double max_transfer_amount = 0.0;
57 // Application parameters
58 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
59 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
60 double min_comp_iter_duration = 1.0; // fixme: find better defaults
61 unsigned comp_iter_delay = 0; // fixme: find better defaults
62 double comp_time_delay = 0.0; // fixme: find better defaults
64 // Parameters for the end of the simulation
65 unsigned lb_maxiter = 0;
66 unsigned comp_maxiter = 0;
67 double time_limit = 0;
68 bool exit_on_close = true;
70 // Named parameters lists
71 loba_algorithms_type loba_algorithms;
72 loba_algorithms_type::loba_algorithms_type()
74 NOL_INSERT("fairstrategy", "balance with fair strategy",
76 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
78 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
80 NOL_INSERT("none", "no load-balancing (for testing only)",
82 NOL_INSERT("simple", "balance with least loaded neighbor",
86 topologies_type topologies;
87 topologies_type::topologies_type()
89 NOL_INSERT("btree", "binary tree topology, initial load at root",
91 NOL_INSERT("clique", "all connected topology", deployment_clique);
92 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
93 NOL_INSERT("line", "line topology, initial load at one end",
95 NOL_INSERT("ring", "ring topology", deployment_ring);
96 NOL_INSERT("star", "star topology, initial load at center",
98 NOL_INSERT("torus", "torus topology", deployment_torus);
105 // local helper class
108 template <typename T>
109 static bool parse_arg(char opt, const char *arg, T& val);
110 static const char* on_off(bool b);
111 const char* descr(const char* str);
112 template <typename T>
113 const char* val_or_string(const T& val, const char* str,
115 template <typename T>
116 static bool nol_find_prefix(const T& nol, const char* descr,
120 std::string descr_str;
121 std::string val_or_string_str;
126 template <typename T>
127 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
129 std::istringstream str(arg);
130 bool result = (str >> val) && str.eof();
132 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
136 const char* opt_helper::on_off(bool b)
138 return b ? "on" : "off";
141 const char* opt_helper::descr(const char* str)
143 std::string& res = descr_str;
145 res.resize(DATA_DESCR_WIDTH, '.');
149 template <typename T>
150 const char* opt_helper::val_or_string(const T& val, const char* str,
153 std::string& res = val_or_string_str;
155 std::ostringstream oss;
164 template <typename T>
165 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
168 bool result = nol.exists(name);
170 std::stack<std::string> candidates;
171 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
172 const std::string& fullname = nol.get_name(it);
173 if (fullname.compare(0, name.length(), name) == 0)
174 candidates.push(fullname);
176 switch (candidates.size()) {
178 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
181 name = candidates.top();
184 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
187 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
188 while (!candidates.empty()) {
189 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
198 bool opt::parse_args(int* argc, char* argv[])
202 opt::program_name = argv[0];
203 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
205 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
209 while ((c = getopt(*argc, argv,
210 "a:bc:C:d:D:ehi:I:l:L:m:M:N:s:S:t:T:vV")) != -1) {
213 opt::loba_algo = optarg;
214 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
215 "load balancing algorithm",
220 opt::bookkeeping = !opt::bookkeeping;
223 opt::exit_on_close = !opt::exit_on_close;
226 opt::help_requested++;
230 opt::comp_cost = cost_func(optarg);
232 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
239 opt::comm_cost = cost_func(optarg);
241 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
247 PARSE_ARG(opt::comp_iter_delay);
250 PARSE_ARG(opt::comp_time_delay);
253 PARSE_ARG(opt::lb_maxiter);
256 PARSE_ARG(opt::comp_maxiter);
259 PARSE_ARG(opt::log_rate);
262 PARSE_ARG(opt::auto_depl::load);
265 PARSE_ARG(opt::min_transfer_amount);
268 PARSE_ARG(opt::max_transfer_amount);
271 PARSE_ARG(opt::auto_depl::nhosts);
274 PARSE_ARG(opt::min_lb_iter_duration);
277 PARSE_ARG(opt::min_comp_iter_duration);
280 PARSE_ARG(opt::time_limit);
283 opt::auto_depl::topology = optarg;
284 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
285 opt::auto_depl::topology)
289 // nothing to do: this option is checked at the very
290 // beginning of main()
293 opt::version_requested = true;
296 XBT_ERROR("invalid option -- '%c'", optopt);
304 if (opt::version_requested || opt::help_requested)
307 if (optind < *argc) {
308 opt::platform_file = argv[optind++];
310 XBT_ERROR("missing parameter -- <plaform_file>");
313 if (optind < *argc) {
314 opt::deployment_file = argv[optind++];
316 opt::auto_depl::enabled = opt::deployment_file.empty();
318 while (optind < *argc) {
319 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
323 if (opt::max_transfer_amount &&
324 opt::max_transfer_amount < opt::min_transfer_amount) {
325 XBT_ERROR("max. data transfer amount < min. data transfer amount");
336 #define DESCR(description, format, value) \
337 XBT_INFO("| %s: " format, h.descr(description), value)
339 XBT_INFO(",----[ Simulation parameters ]");
340 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
341 DESCR("platform file", "\"%s\"", platform_file.c_str());
342 if (auto_depl::enabled) {
343 XBT_INFO("| automatic deployment enabled");
344 DESCR("- topology", "%s", auto_depl::topology.c_str());
345 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
347 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
350 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
352 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
353 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
354 DESCR("minimum duration between lb. iterations", "%g",
355 min_lb_iter_duration);
356 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
357 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
358 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
359 DESCR("maximum data transfer amount", "%s",
360 h.val_or_string(max_transfer_amount, "no limit"));
361 DESCR("minimum duration between comp. iterations", "%g",
362 min_comp_iter_duration);
363 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
364 DESCR("computations start after time", "%g", comp_time_delay);
365 DESCR("maximum number of lb. iterations", "%s",
366 h.val_or_string(lb_maxiter, "no limit"));
367 DESCR("maximum number of comp. iterations", "%s",
368 h.val_or_string(comp_maxiter, "no limit"));
369 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
370 DESCR("exit on close", "%s", h.on_off(exit_on_close));
379 #define o(opt) " " << std::setw(14) \
380 << std::left << (opt) << std::right << " "
382 #define so(subopt) std::setw(18) << (subopt) << " : "
384 #define so_list(name) do { \
385 name ## _type::iterator it; \
386 for (it = name.begin() ; it != name.end() ; ++it) \
387 std::clog << so(name.get_name(it)) \
388 << name.get_descr(it) << "\n"; \
392 std::clog << "Usage: " << opt::program_name
393 << " [options] <platform_file> [<deployment_file>]\n";
395 std::clog << "\nGlobal options\n";
397 << "print help and exit (use -hh or -hhh for extended help)\n";
398 if (opt::help_requested < 1)
401 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
402 std::clog << o("-V") << "print version and exit\n";
404 std::clog << "\nSimulation parameters\n";
405 std::clog << o("-l value")
406 << "print current load every n lb iterations, 0 to disable"
407 << " [" << opt::log_rate << "]\n";
409 << "verbose: do not override the default logging parameters\n";
411 std::clog << "\nAutomatic deployment options\n";
412 std::clog << o("-T name")
413 << "enable automatic deployment with selected topology"
414 << " [" << opt::auto_depl::topology << "]\n";
415 if (opt::help_requested > 1)
416 so_list(opt::topologies);
417 std::clog << o("-L value")
418 << "total load with auto deployment, 0 for number of hosts"
419 << " [" << opt::auto_depl::load << "]\n";
420 std::clog << o("-N value")
421 << "number of hosts to use with auto deployment, 0 for max."
422 << " [" << opt::auto_depl::nhosts << "]\n";
424 std::clog << "\nLoad balancing algorithm\n";
425 std::clog << o("-a name") << "load balancing algorithm"
426 << " [" << opt::loba_algo << "]\n";
427 if (opt::help_requested > 1)
428 so_list(opt::loba_algorithms);
429 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
430 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
431 std::clog << o("-s value")
432 << "minimum duration between lb. iterations"
433 << " [" << opt::min_lb_iter_duration << "]\n";
435 std::clog << "\nApplication parameters\n";
436 std::clog << o("-c [aN,...]a0")
437 << "polynomial factors for computation cost"
438 << " [" << opt::comp_cost.to_string() << "]\n";
439 std::clog << o("-C [aN,...]a0")
440 << "polynomial factors for communication cost"
441 << " [" << opt::comm_cost.to_string() << "]\n";
442 std::clog << o("-m value")
443 << "minimum data transfer amount"
444 << " [" << opt::min_transfer_amount << "]\n";
445 std::clog << o("-M value")
446 << "maximum data transfer amount, 0 for no limit"
447 << " [" << opt::max_transfer_amount << "]\n";
448 std::clog << o("-S value")
449 << "minimum duration between comp. iterations"
450 << " [" << opt::min_comp_iter_duration << "]\n";
451 std::clog << o("-d value")
452 << "start computations after given number of lb iterations"
453 << " [" << opt::comp_iter_delay << "]\n";
454 std::clog << o("-D value")
455 << "start computations after given time"
456 << " [" << opt::comp_time_delay << "]\n";
458 std::clog << "\nParameters for the end of the simulation\n";
459 std::clog << o("-i value")
460 << "maximum number of lb. iterations, 0 for no limit"
461 << " [" << opt::lb_maxiter << "]\n";
462 std::clog << o("-I value")
463 << "maximum number of comp. iterations, 0 for no limit"
464 << " [" << opt::comp_maxiter << "]\n";
465 std::clog << o("-t value")
466 << "time limit (simulated time), 0 for no limit"
467 << " [" << opt::time_limit << "]\n";
468 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
469 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
471 if (opt::help_requested < 3)
474 std::clog << "\nLogging support\n"
475 << " See SimGrid documentation on:\n"
476 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
477 << " Existing categories are:\n"
478 << " simu : root of following categories\n"
479 << " main : messages from global infrastructure\n"
480 << " depl : messages from auto deployment (inherited from main)\n"
481 << " comm : messages from asynchronous pipes\n"
482 << " proc : messages from base process class\n"
483 << " loba : messages from load-balancer\n"
484 << " thrd : messages from thread wrapper class\n";
486 // std::clog << "\nMiscellaneous low-level parameters\n";