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;
51 // Application parameters
52 // fixme: find better defaults
53 cost_func comp_cost("1e9, 0");
54 cost_func comm_cost("1e6, 0");
55 double min_lb_iter_duration = 1.0;
56 double min_comp_iter_duration = 1.0;
58 // Parameters for the end of the simulation
59 unsigned lb_maxiter = 0;
60 unsigned comp_maxiter = 0;
61 double time_limit = 0;
62 bool exit_on_close = true;
64 // Named parameters lists
65 loba_algorithms_type loba_algorithms;
66 loba_algorithms_type::loba_algorithms_type()
68 NOL_INSERT("fairstrategy", "balance with fair strategy", loba_fairstrategy);
69 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm", loba_makhoul);
70 NOL_INSERT("none", "no load-balancing (for testing only)", process);
71 NOL_INSERT("simple", "balance with least loaded neighbor", loba_simple);
74 topologies_type topologies;
75 topologies_type::topologies_type()
77 NOL_INSERT("btree", "binary tree topology, initial load at root",
79 NOL_INSERT("clique", "all connected topology", deployment_clique);
80 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
81 NOL_INSERT("line", "line topology, initial load at one end",
83 NOL_INSERT("ring", "ring topology", deployment_ring);
84 NOL_INSERT("star", "star topology, initial load at center",
86 NOL_INSERT("torus", "torus topology", deployment_torus);
97 static bool parse_arg(char opt, const char *arg, T& val);
98 static const char* on_off(bool b);
99 const char* descr(const char* str);
100 template <typename T>
101 const char* val_or_string(const T& val, const char* str,
103 template <typename T>
104 static bool nol_find_prefix(const T& nol, const char* descr,
108 std::string descr_str;
109 std::string val_or_string_str;
114 template <typename T>
115 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
117 std::istringstream str(arg);
118 bool result = (str >> val) && str.eof();
120 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
124 const char* opt_helper::on_off(bool b)
126 return b ? "on" : "off";
129 const char* opt_helper::descr(const char* str)
131 const int descr_width = 40;
132 std::string& res = descr_str;
134 res.resize(descr_width, '.');
138 template <typename T>
139 const char* opt_helper::val_or_string(const T& val, const char* str,
142 std::string& res = val_or_string_str;
144 std::ostringstream oss;
153 template <typename T>
154 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
157 bool result = nol.exists(name);
159 std::stack<std::string> candidates;
160 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
161 const std::string& fullname = nol.get_name(it);
162 if (fullname.compare(0, name.length(), name) == 0)
163 candidates.push(fullname);
165 switch (candidates.size()) {
167 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
170 name = candidates.top();
173 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
176 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
177 while (!candidates.empty()) {
178 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
187 bool opt::parse_args(int* argc, char* argv[])
191 opt::program_name = argv[0];
192 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
194 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
198 while ((c = getopt(*argc, argv, "a:bc:C:ehi:I:l:L:N:s:S:t:T:vV")) != -1) {
201 opt::loba_algo = optarg;
202 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
203 "load balancing algorithm",
208 opt::bookkeeping = !opt::bookkeeping;
211 opt::exit_on_close = !opt::exit_on_close;
214 opt::help_requested++;
218 opt::comp_cost = cost_func(optarg);
220 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", c, optarg);
226 opt::comm_cost = cost_func(optarg);
228 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", c, optarg);
233 PARSE_ARG(opt::lb_maxiter);
236 PARSE_ARG(opt::comp_maxiter);
239 PARSE_ARG(opt::log_rate);
242 PARSE_ARG(opt::auto_depl::load);
245 PARSE_ARG(opt::auto_depl::nhosts);
248 PARSE_ARG(opt::min_lb_iter_duration);
251 PARSE_ARG(opt::min_comp_iter_duration);
254 PARSE_ARG(opt::time_limit);
257 opt::auto_depl::topology = optarg;
258 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
259 opt::auto_depl::topology)
263 // nothing to do: this option is checked at the very
264 // beginning of main()
267 opt::version_requested = true;
270 XBT_ERROR("invalid option -- '%c'", optopt);
278 if (opt::version_requested || opt::help_requested)
281 if (optind < *argc) {
282 opt::platform_file = argv[optind++];
284 XBT_ERROR("missing parameter -- <plaform_file>");
287 if (optind < *argc) {
288 opt::deployment_file = argv[optind++];
290 opt::auto_depl::enabled = opt::deployment_file.empty();
292 while (optind < *argc) {
293 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
304 #define DESCR(description, format, value) \
305 XBT_INFO("| %s: " format, h.descr(description), value)
307 XBT_INFO(",----[ Simulation parameters ]");
308 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
309 DESCR("platform file", "\"%s\"", platform_file.c_str());
310 if (auto_depl::enabled) {
311 XBT_INFO("| automatic deployment enabled");
312 DESCR("- topology", "%s", auto_depl::topology.c_str());
313 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
315 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
318 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
320 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
321 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
322 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
323 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
324 DESCR("minimum duration between lb. iterations", "%g",
325 min_lb_iter_duration);
326 DESCR("minimum duration between comp. iterations", "%g",
327 min_comp_iter_duration);
328 DESCR("maximum number of lb. iterations", "%s",
329 h.val_or_string(lb_maxiter, "infinity"));
330 DESCR("maximum number of comp. iterations", "%s",
331 h.val_or_string(comp_maxiter, "infinity"));
332 DESCR("time limit", "%s", h.val_or_string(time_limit, "infinity"));
333 DESCR("exit on close", "%s", h.on_off(exit_on_close));
342 #define o(opt) " " << std::setw(14) \
343 << std::left << (opt) << std::right << " "
345 #define so(subopt) std::setw(18) << (subopt) << " : "
347 #define so_list(name) do { \
348 name ## _type::iterator it; \
349 for (it = name.begin() ; it != name.end() ; ++it) \
350 std::clog << so(name.get_name(it)) \
351 << name.get_descr(it) << "\n"; \
355 std::clog << "Usage: " << opt::program_name
356 << " [options] <platform_file> [<deployment_file>]\n";
358 std::clog << "\nGlobal options\n";
360 << "print help and exit (use -hh or -hhh for extended help)\n";
361 if (opt::help_requested < 1)
364 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
365 std::clog << o("-V") << "print version and exit\n";
367 std::clog << "\nSimulation parameters\n";
368 std::clog << o("-l value")
369 << "print current load every n lb iterations, 0 to disable"
370 << " [" << opt::log_rate << "]\n";
372 << "verbose: do not override the default logging parameters\n";
374 std::clog << "\nAutomatic deployment options\n";
375 std::clog << o("-T name")
376 << "enable automatic deployment with selected topology"
377 << " [" << opt::auto_depl::topology << "]\n";
378 if (opt::help_requested > 1)
379 so_list(opt::topologies);
380 std::clog << o("-L value")
381 << "total load with auto deployment, 0 for number of hosts"
382 << " [" << opt::auto_depl::load << "]\n";
383 std::clog << o("-N value")
384 << "number of hosts to use with auto deployment, 0 for max."
385 << " [" << opt::auto_depl::nhosts << "]\n";
387 std::clog << "\nLoad balancing algorithm\n";
388 std::clog << o("-a name") << "load balancing algorithm"
389 << " [" << opt::loba_algo << "]\n";
390 if (opt::help_requested > 1)
391 so_list(opt::loba_algorithms);
392 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
393 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
395 std::clog << "\nApplication parameters\n";
396 std::clog << o("-c [aN,...]a0")
397 << "polynomial factors for computation cost"
398 << " [" << opt::comp_cost.to_string() << "]\n";
399 std::clog << o("-C [aN,...]a0")
400 << "polynomial factors for communication cost"
401 << " [" << opt::comm_cost.to_string() << "]\n";
402 std::clog << o("-s value")
403 << "minimum duration between lb. iterations"
404 << " [" << opt::min_lb_iter_duration << "]\n";
405 std::clog << o("-S value")
406 << "minimum duration between comp. iterations"
407 << " [" << opt::min_comp_iter_duration << "]\n";
409 std::clog << "\nParameters for the end of the simulation\n";
410 std::clog << o("-i value")
411 << "maximum number of lb. iterations, 0 for infinity"
412 << " [" << opt::lb_maxiter << "]\n";
413 std::clog << o("-I value")
414 << "maximum number of comp. iterations, 0 for infinity"
415 << " [" << opt::comp_maxiter << "]\n";
416 std::clog << o("-t value")
417 << "time limit (simulated time), 0 for infinity"
418 << " [" << opt::time_limit << "]\n";
419 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
420 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
422 if (opt::help_requested < 3)
425 std::clog << "\nLogging support\n"
426 << " See SimGrid documentation on:\n"
427 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
428 << " Existing categories are:\n"
429 << " simu : root of following categories\n"
430 << " main : messages from global infrastructure\n"
431 << " depl : messages from auto deployment (inherited from main)\n"
432 << " comm : messages from asynchronous pipes\n"
433 << " proc : messages from base process class\n"
434 << " loba : messages from load-balancer\n"
435 << " thrd : messages from thread wrapper class\n";
437 // std::clog << "\nMiscellaneous low-level parameters\n";