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
57 // Parameters for the end of the simulation
58 unsigned lb_maxiter = 0;
59 unsigned comp_maxiter = 0;
60 double time_limit = 0;
61 bool exit_on_close = true;
63 // Named parameters lists
64 loba_algorithms_type loba_algorithms;
65 loba_algorithms_type::loba_algorithms_type()
67 NOL_INSERT("fairstrategy", "balance with fair strategy", loba_fairstrategy);
68 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm", loba_makhoul);
69 NOL_INSERT("none", "no load-balancing (for testing only)", process);
70 NOL_INSERT("simple", "balance with least loaded neighbor", loba_simple);
73 topologies_type topologies;
74 topologies_type::topologies_type()
76 NOL_INSERT("btree", "binary tree topology, initial load at root",
78 NOL_INSERT("clique", "all connected topology", deployment_clique);
79 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
80 NOL_INSERT("line", "line topology, initial load at one end",
82 NOL_INSERT("ring", "ring topology", deployment_ring);
83 NOL_INSERT("star", "star topology, initial load at center",
85 NOL_INSERT("torus", "torus topology", deployment_torus);
96 static bool parse_arg(char opt, const char *arg, T& val);
97 static const char* on_off(bool b);
98 const char* descr(const char* str);
100 const char* val_or_string(const T& val, const char* str,
102 template <typename T>
103 static bool nol_find_prefix(const T& nol, const char* descr,
107 std::string descr_str;
108 std::string val_or_string_str;
113 template <typename T>
114 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
116 std::istringstream str(arg);
117 bool result = (str >> val) && str.eof();
119 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
123 const char* opt_helper::on_off(bool b)
125 return b ? "on" : "off";
128 const char* opt_helper::descr(const char* str)
130 const int descr_width = 40;
131 std::string& res = descr_str;
133 res.resize(descr_width, '.');
137 template <typename T>
138 const char* opt_helper::val_or_string(const T& val, const char* str,
141 std::string& res = val_or_string_str;
143 std::ostringstream oss;
152 template <typename T>
153 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
156 bool result = nol.exists(name);
158 std::stack<std::string> candidates;
159 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
160 const std::string& fullname = nol.get_name(it);
161 if (fullname.compare(0, name.length(), name) == 0)
162 candidates.push(fullname);
164 switch (candidates.size()) {
166 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
169 name = candidates.top();
172 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
175 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
176 while (!candidates.empty()) {
177 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
186 bool opt::parse_args(int* argc, char* argv[])
190 opt::program_name = argv[0];
191 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
193 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
197 while ((c = getopt(*argc, argv, "a:bc:C:ehi:I:l:L:N:s:S:t:T:vV")) != -1) {
200 opt::loba_algo = optarg;
201 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
202 "load balancing algorithm",
207 opt::bookkeeping = !opt::bookkeeping;
210 opt::exit_on_close = !opt::exit_on_close;
213 opt::help_requested++;
217 opt::comp_cost = cost_func(optarg);
219 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", c, optarg);
225 opt::comm_cost = cost_func(optarg);
227 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", c, optarg);
232 PARSE_ARG(opt::lb_maxiter);
235 PARSE_ARG(opt::comp_maxiter);
238 PARSE_ARG(opt::log_rate);
241 PARSE_ARG(opt::auto_depl::load);
244 PARSE_ARG(opt::auto_depl::nhosts);
247 PARSE_ARG(opt::min_lb_iter_duration);
250 PARSE_ARG(opt::min_comp_iter_duration);
253 PARSE_ARG(opt::time_limit);
256 opt::auto_depl::topology = optarg;
257 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
258 opt::auto_depl::topology)
262 // nothing to do: this option is checked at the very
263 // beginning of main()
266 opt::version_requested = true;
269 XBT_ERROR("invalid option -- '%c'", optopt);
277 if (opt::version_requested || opt::help_requested)
280 if (optind < *argc) {
281 opt::platform_file = argv[optind++];
283 XBT_ERROR("missing parameter -- <plaform_file>");
286 if (optind < *argc) {
287 opt::deployment_file = argv[optind++];
289 opt::auto_depl::enabled = opt::deployment_file.empty();
291 while (optind < *argc) {
292 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
303 #define DESCR(description, format, value) \
304 XBT_INFO("| %s: " format, h.descr(description), value)
306 XBT_INFO(",----[ Simulation parameters ]");
307 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
308 DESCR("platform file", "\"%s\"", platform_file.c_str());
309 if (auto_depl::enabled) {
310 XBT_INFO("| automatic deployment enabled");
311 DESCR("- topology", "%s", auto_depl::topology.c_str());
312 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
314 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
317 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
319 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
320 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
321 DESCR("minimum duration between lb. iterations", "%g", min_lb_iter_duration);
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 comp. iterations", "%g", min_comp_iter_duration);
325 DESCR("maximum number of lb. iterations", "%s",
326 h.val_or_string(lb_maxiter, "infinity"));
327 DESCR("maximum number of comp. iterations", "%s",
328 h.val_or_string(comp_maxiter, "infinity"));
329 DESCR("time limit", "%s", h.val_or_string(time_limit, "infinity"));
330 DESCR("exit on close", "%s", h.on_off(exit_on_close));
339 #define o(opt) " " << std::setw(14) \
340 << std::left << (opt) << std::right << " "
342 #define so(subopt) std::setw(18) << (subopt) << " : "
344 #define so_list(name) do { \
345 name ## _type::iterator it; \
346 for (it = name.begin() ; it != name.end() ; ++it) \
347 std::clog << so(name.get_name(it)) \
348 << name.get_descr(it) << "\n"; \
352 std::clog << "Usage: " << opt::program_name
353 << " [options] <platform_file> [<deployment_file>]\n";
355 std::clog << "\nGlobal options\n";
357 << "print help and exit (use -hh or -hhh for extended help)\n";
358 if (opt::help_requested < 1)
361 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
362 std::clog << o("-V") << "print version and exit\n";
364 std::clog << "\nSimulation parameters\n";
365 std::clog << o("-l value")
366 << "print current load every n lb iterations, 0 to disable"
367 << " [" << opt::log_rate << "]\n";
369 << "verbose: do not override the default logging parameters\n";
371 std::clog << "\nAutomatic deployment options\n";
372 std::clog << o("-T name")
373 << "enable automatic deployment with selected topology"
374 << " [" << opt::auto_depl::topology << "]\n";
375 if (opt::help_requested > 1)
376 so_list(opt::topologies);
377 std::clog << o("-L value")
378 << "total load with auto deployment, 0 for number of hosts"
379 << " [" << opt::auto_depl::load << "]\n";
380 std::clog << o("-N value")
381 << "number of hosts to use with auto deployment, 0 for max."
382 << " [" << opt::auto_depl::nhosts << "]\n";
384 std::clog << "\nLoad balancing algorithm\n";
385 std::clog << o("-a name") << "load balancing algorithm"
386 << " [" << opt::loba_algo << "]\n";
387 if (opt::help_requested > 1)
388 so_list(opt::loba_algorithms);
389 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
390 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
391 std::clog << o("-s value")
392 << "minimum duration between lb. iterations"
393 << " [" << opt::min_lb_iter_duration << "]\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 comp. iterations"
404 << " [" << opt::min_comp_iter_duration << "]\n";
406 std::clog << "\nParameters for the end of the simulation\n";
407 std::clog << o("-i value")
408 << "maximum number of lb. iterations, 0 for infinity"
409 << " [" << opt::lb_maxiter << "]\n";
410 std::clog << o("-I value")
411 << "maximum number of comp. iterations, 0 for infinity"
412 << " [" << opt::comp_maxiter << "]\n";
413 std::clog << o("-t value")
414 << "time limit (simulated time), 0 for infinity"
415 << " [" << opt::time_limit << "]\n";
416 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
417 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
419 if (opt::help_requested < 3)
422 std::clog << "\nLogging support\n"
423 << " See SimGrid documentation on:\n"
424 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
425 << " Existing categories are:\n"
426 << " simu : root of following categories\n"
427 << " main : messages from global infrastructure\n"
428 << " depl : messages from auto deployment (inherited from main)\n"
429 << " comm : messages from asynchronous pipes\n"
430 << " proc : messages from base process class\n"
431 << " loba : messages from load-balancer\n"
432 << " thrd : messages from thread wrapper class\n";
434 // std::clog << "\nMiscellaneous low-level parameters\n";