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
52 // Application parameters
53 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
54 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
55 double min_comp_iter_duration = 1.0; // fixme: find better defaults
56 unsigned comp_iter_delay = 0; // fixme: find better defaults
57 double comp_time_delay = 0.0; // fixme: find better defaults
59 // Parameters for the end of the simulation
60 unsigned lb_maxiter = 0;
61 unsigned comp_maxiter = 0;
62 double time_limit = 0;
63 bool exit_on_close = true;
65 // Named parameters lists
66 loba_algorithms_type loba_algorithms;
67 loba_algorithms_type::loba_algorithms_type()
69 NOL_INSERT("fairstrategy", "balance with fair strategy",
71 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
73 NOL_INSERT("none", "no load-balancing (for testing only)",
75 NOL_INSERT("simple", "balance with least loaded neighbor",
79 topologies_type topologies;
80 topologies_type::topologies_type()
82 NOL_INSERT("btree", "binary tree topology, initial load at root",
84 NOL_INSERT("clique", "all connected topology", deployment_clique);
85 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
86 NOL_INSERT("line", "line topology, initial load at one end",
88 NOL_INSERT("ring", "ring topology", deployment_ring);
89 NOL_INSERT("star", "star topology, initial load at center",
91 NOL_INSERT("torus", "torus topology", deployment_torus);
101 template <typename T>
102 static bool parse_arg(char opt, const char *arg, T& val);
103 static const char* on_off(bool b);
104 const char* descr(const char* str);
105 template <typename T>
106 const char* val_or_string(const T& val, const char* str,
108 template <typename T>
109 static bool nol_find_prefix(const T& nol, const char* descr,
113 std::string descr_str;
114 std::string val_or_string_str;
119 template <typename T>
120 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
122 std::istringstream str(arg);
123 bool result = (str >> val) && str.eof();
125 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
129 const char* opt_helper::on_off(bool b)
131 return b ? "on" : "off";
134 const char* opt_helper::descr(const char* str)
136 const int descr_width = 40;
137 std::string& res = descr_str;
139 res.resize(descr_width, '.');
143 template <typename T>
144 const char* opt_helper::val_or_string(const T& val, const char* str,
147 std::string& res = val_or_string_str;
149 std::ostringstream oss;
158 template <typename T>
159 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
162 bool result = nol.exists(name);
164 std::stack<std::string> candidates;
165 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
166 const std::string& fullname = nol.get_name(it);
167 if (fullname.compare(0, name.length(), name) == 0)
168 candidates.push(fullname);
170 switch (candidates.size()) {
172 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
175 name = candidates.top();
178 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
181 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
182 while (!candidates.empty()) {
183 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
192 bool opt::parse_args(int* argc, char* argv[])
196 opt::program_name = argv[0];
197 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
199 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
203 while ((c = getopt(*argc, argv,
204 "a:bc:C:d:D:ehi:I:l:L:N:s:S:t:T:vV")) != -1) {
207 opt::loba_algo = optarg;
208 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
209 "load balancing algorithm",
214 opt::bookkeeping = !opt::bookkeeping;
217 opt::exit_on_close = !opt::exit_on_close;
220 opt::help_requested++;
224 opt::comp_cost = cost_func(optarg);
226 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
233 opt::comm_cost = cost_func(optarg);
235 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
241 PARSE_ARG(opt::comp_iter_delay);
244 PARSE_ARG(opt::comp_time_delay);
247 PARSE_ARG(opt::lb_maxiter);
250 PARSE_ARG(opt::comp_maxiter);
253 PARSE_ARG(opt::log_rate);
256 PARSE_ARG(opt::auto_depl::load);
259 PARSE_ARG(opt::auto_depl::nhosts);
262 PARSE_ARG(opt::min_lb_iter_duration);
265 PARSE_ARG(opt::min_comp_iter_duration);
268 PARSE_ARG(opt::time_limit);
271 opt::auto_depl::topology = optarg;
272 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
273 opt::auto_depl::topology)
277 // nothing to do: this option is checked at the very
278 // beginning of main()
281 opt::version_requested = true;
284 XBT_ERROR("invalid option -- '%c'", optopt);
292 if (opt::version_requested || opt::help_requested)
295 if (optind < *argc) {
296 opt::platform_file = argv[optind++];
298 XBT_ERROR("missing parameter -- <plaform_file>");
301 if (optind < *argc) {
302 opt::deployment_file = argv[optind++];
304 opt::auto_depl::enabled = opt::deployment_file.empty();
306 while (optind < *argc) {
307 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
318 #define DESCR(description, format, value) \
319 XBT_INFO("| %s: " format, h.descr(description), value)
321 XBT_INFO(",----[ Simulation parameters ]");
322 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
323 DESCR("platform file", "\"%s\"", platform_file.c_str());
324 if (auto_depl::enabled) {
325 XBT_INFO("| automatic deployment enabled");
326 DESCR("- topology", "%s", auto_depl::topology.c_str());
327 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
329 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
332 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
334 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
335 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
336 DESCR("minimum duration between lb. iterations", "%g",
337 min_lb_iter_duration);
338 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
339 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
340 DESCR("minimum duration between comp. iterations", "%g",
341 min_comp_iter_duration);
342 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
343 DESCR("computations start after time", "%g", comp_time_delay);
344 DESCR("maximum number of lb. iterations", "%s",
345 h.val_or_string(lb_maxiter, "infinity"));
346 DESCR("maximum number of comp. iterations", "%s",
347 h.val_or_string(comp_maxiter, "infinity"));
348 DESCR("time limit", "%s", h.val_or_string(time_limit, "infinity"));
349 DESCR("exit on close", "%s", h.on_off(exit_on_close));
358 #define o(opt) " " << std::setw(14) \
359 << std::left << (opt) << std::right << " "
361 #define so(subopt) std::setw(18) << (subopt) << " : "
363 #define so_list(name) do { \
364 name ## _type::iterator it; \
365 for (it = name.begin() ; it != name.end() ; ++it) \
366 std::clog << so(name.get_name(it)) \
367 << name.get_descr(it) << "\n"; \
371 std::clog << "Usage: " << opt::program_name
372 << " [options] <platform_file> [<deployment_file>]\n";
374 std::clog << "\nGlobal options\n";
376 << "print help and exit (use -hh or -hhh for extended help)\n";
377 if (opt::help_requested < 1)
380 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
381 std::clog << o("-V") << "print version and exit\n";
383 std::clog << "\nSimulation parameters\n";
384 std::clog << o("-l value")
385 << "print current load every n lb iterations, 0 to disable"
386 << " [" << opt::log_rate << "]\n";
388 << "verbose: do not override the default logging parameters\n";
390 std::clog << "\nAutomatic deployment options\n";
391 std::clog << o("-T name")
392 << "enable automatic deployment with selected topology"
393 << " [" << opt::auto_depl::topology << "]\n";
394 if (opt::help_requested > 1)
395 so_list(opt::topologies);
396 std::clog << o("-L value")
397 << "total load with auto deployment, 0 for number of hosts"
398 << " [" << opt::auto_depl::load << "]\n";
399 std::clog << o("-N value")
400 << "number of hosts to use with auto deployment, 0 for max."
401 << " [" << opt::auto_depl::nhosts << "]\n";
403 std::clog << "\nLoad balancing algorithm\n";
404 std::clog << o("-a name") << "load balancing algorithm"
405 << " [" << opt::loba_algo << "]\n";
406 if (opt::help_requested > 1)
407 so_list(opt::loba_algorithms);
408 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
409 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
410 std::clog << o("-s value")
411 << "minimum duration between lb. iterations"
412 << " [" << opt::min_lb_iter_duration << "]\n";
414 std::clog << "\nApplication parameters\n";
415 std::clog << o("-c [aN,...]a0")
416 << "polynomial factors for computation cost"
417 << " [" << opt::comp_cost.to_string() << "]\n";
418 std::clog << o("-C [aN,...]a0")
419 << "polynomial factors for communication cost"
420 << " [" << opt::comm_cost.to_string() << "]\n";
421 std::clog << o("-S value")
422 << "minimum duration between comp. iterations"
423 << " [" << opt::min_comp_iter_duration << "]\n";
424 std::clog << o("-d value")
425 << "start computations after given number of lb iterations"
426 << " [" << opt::comp_iter_delay << "]\n";
427 std::clog << o("-D value")
428 << "start computations after given time"
429 << " [" << opt::comp_time_delay << "]\n";
431 std::clog << "\nParameters for the end of the simulation\n";
432 std::clog << o("-i value")
433 << "maximum number of lb. iterations, 0 for infinity"
434 << " [" << opt::lb_maxiter << "]\n";
435 std::clog << o("-I value")
436 << "maximum number of comp. iterations, 0 for infinity"
437 << " [" << opt::comp_maxiter << "]\n";
438 std::clog << o("-t value")
439 << "time limit (simulated time), 0 for infinity"
440 << " [" << opt::time_limit << "]\n";
441 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
442 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
444 if (opt::help_requested < 3)
447 std::clog << "\nLogging support\n"
448 << " See SimGrid documentation on:\n"
449 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
450 << " Existing categories are:\n"
451 << " simu : root of following categories\n"
452 << " main : messages from global infrastructure\n"
453 << " depl : messages from auto deployment (inherited from main)\n"
454 << " comm : messages from asynchronous pipes\n"
455 << " proc : messages from base process class\n"
456 << " loba : messages from load-balancer\n"
457 << " thrd : messages from thread wrapper class\n";
459 // std::clog << "\nMiscellaneous low-level parameters\n";