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", loba_fairstrategy);
70 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm", loba_makhoul);
71 NOL_INSERT("none", "no load-balancing (for testing only)", process);
72 NOL_INSERT("simple", "balance with least loaded neighbor", loba_simple);
75 topologies_type topologies;
76 topologies_type::topologies_type()
78 NOL_INSERT("btree", "binary tree topology, initial load at root",
80 NOL_INSERT("clique", "all connected topology", deployment_clique);
81 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
82 NOL_INSERT("line", "line topology, initial load at one end",
84 NOL_INSERT("ring", "ring topology", deployment_ring);
85 NOL_INSERT("star", "star topology, initial load at center",
87 NOL_INSERT("torus", "torus topology", deployment_torus);
98 static bool parse_arg(char opt, const char *arg, T& val);
99 static const char* on_off(bool b);
100 const char* descr(const char* str);
101 template <typename T>
102 const char* val_or_string(const T& val, const char* str,
104 template <typename T>
105 static bool nol_find_prefix(const T& nol, const char* descr,
109 std::string descr_str;
110 std::string val_or_string_str;
115 template <typename T>
116 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
118 std::istringstream str(arg);
119 bool result = (str >> val) && str.eof();
121 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
125 const char* opt_helper::on_off(bool b)
127 return b ? "on" : "off";
130 const char* opt_helper::descr(const char* str)
132 const int descr_width = 40;
133 std::string& res = descr_str;
135 res.resize(descr_width, '.');
139 template <typename T>
140 const char* opt_helper::val_or_string(const T& val, const char* str,
143 std::string& res = val_or_string_str;
145 std::ostringstream oss;
154 template <typename T>
155 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
158 bool result = nol.exists(name);
160 std::stack<std::string> candidates;
161 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
162 const std::string& fullname = nol.get_name(it);
163 if (fullname.compare(0, name.length(), name) == 0)
164 candidates.push(fullname);
166 switch (candidates.size()) {
168 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
171 name = candidates.top();
174 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
177 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
178 while (!candidates.empty()) {
179 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
188 bool opt::parse_args(int* argc, char* argv[])
192 opt::program_name = argv[0];
193 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
195 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
199 while ((c = getopt(*argc, argv,
200 "a:bc:C:d:D:ehi:I:l:L:N:s:S:t:T:vV")) != -1) {
203 opt::loba_algo = optarg;
204 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
205 "load balancing algorithm",
210 opt::bookkeeping = !opt::bookkeeping;
213 opt::exit_on_close = !opt::exit_on_close;
216 opt::help_requested++;
220 opt::comp_cost = cost_func(optarg);
222 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", c, optarg);
228 opt::comm_cost = cost_func(optarg);
230 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", c, optarg);
235 PARSE_ARG(opt::comp_iter_delay);
238 PARSE_ARG(opt::comp_time_delay);
241 PARSE_ARG(opt::lb_maxiter);
244 PARSE_ARG(opt::comp_maxiter);
247 PARSE_ARG(opt::log_rate);
250 PARSE_ARG(opt::auto_depl::load);
253 PARSE_ARG(opt::auto_depl::nhosts);
256 PARSE_ARG(opt::min_lb_iter_duration);
259 PARSE_ARG(opt::min_comp_iter_duration);
262 PARSE_ARG(opt::time_limit);
265 opt::auto_depl::topology = optarg;
266 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
267 opt::auto_depl::topology)
271 // nothing to do: this option is checked at the very
272 // beginning of main()
275 opt::version_requested = true;
278 XBT_ERROR("invalid option -- '%c'", optopt);
286 if (opt::version_requested || opt::help_requested)
289 if (optind < *argc) {
290 opt::platform_file = argv[optind++];
292 XBT_ERROR("missing parameter -- <plaform_file>");
295 if (optind < *argc) {
296 opt::deployment_file = argv[optind++];
298 opt::auto_depl::enabled = opt::deployment_file.empty();
300 while (optind < *argc) {
301 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
312 #define DESCR(description, format, value) \
313 XBT_INFO("| %s: " format, h.descr(description), value)
315 XBT_INFO(",----[ Simulation parameters ]");
316 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
317 DESCR("platform file", "\"%s\"", platform_file.c_str());
318 if (auto_depl::enabled) {
319 XBT_INFO("| automatic deployment enabled");
320 DESCR("- topology", "%s", auto_depl::topology.c_str());
321 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
323 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
326 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
328 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
329 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
330 DESCR("minimum duration between lb. iterations", "%g", min_lb_iter_duration);
331 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
332 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
333 DESCR("minimum duration between comp. iterations", "%g", min_comp_iter_duration);
334 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
335 DESCR("computations start after time", "%g", comp_time_delay);
336 DESCR("maximum number of lb. iterations", "%s",
337 h.val_or_string(lb_maxiter, "infinity"));
338 DESCR("maximum number of comp. iterations", "%s",
339 h.val_or_string(comp_maxiter, "infinity"));
340 DESCR("time limit", "%s", h.val_or_string(time_limit, "infinity"));
341 DESCR("exit on close", "%s", h.on_off(exit_on_close));
350 #define o(opt) " " << std::setw(14) \
351 << std::left << (opt) << std::right << " "
353 #define so(subopt) std::setw(18) << (subopt) << " : "
355 #define so_list(name) do { \
356 name ## _type::iterator it; \
357 for (it = name.begin() ; it != name.end() ; ++it) \
358 std::clog << so(name.get_name(it)) \
359 << name.get_descr(it) << "\n"; \
363 std::clog << "Usage: " << opt::program_name
364 << " [options] <platform_file> [<deployment_file>]\n";
366 std::clog << "\nGlobal options\n";
368 << "print help and exit (use -hh or -hhh for extended help)\n";
369 if (opt::help_requested < 1)
372 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
373 std::clog << o("-V") << "print version and exit\n";
375 std::clog << "\nSimulation parameters\n";
376 std::clog << o("-l value")
377 << "print current load every n lb iterations, 0 to disable"
378 << " [" << opt::log_rate << "]\n";
380 << "verbose: do not override the default logging parameters\n";
382 std::clog << "\nAutomatic deployment options\n";
383 std::clog << o("-T name")
384 << "enable automatic deployment with selected topology"
385 << " [" << opt::auto_depl::topology << "]\n";
386 if (opt::help_requested > 1)
387 so_list(opt::topologies);
388 std::clog << o("-L value")
389 << "total load with auto deployment, 0 for number of hosts"
390 << " [" << opt::auto_depl::load << "]\n";
391 std::clog << o("-N value")
392 << "number of hosts to use with auto deployment, 0 for max."
393 << " [" << opt::auto_depl::nhosts << "]\n";
395 std::clog << "\nLoad balancing algorithm\n";
396 std::clog << o("-a name") << "load balancing algorithm"
397 << " [" << opt::loba_algo << "]\n";
398 if (opt::help_requested > 1)
399 so_list(opt::loba_algorithms);
400 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
401 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
402 std::clog << o("-s value")
403 << "minimum duration between lb. iterations"
404 << " [" << opt::min_lb_iter_duration << "]\n";
406 std::clog << "\nApplication parameters\n";
407 std::clog << o("-c [aN,...]a0")
408 << "polynomial factors for computation cost"
409 << " [" << opt::comp_cost.to_string() << "]\n";
410 std::clog << o("-C [aN,...]a0")
411 << "polynomial factors for communication cost"
412 << " [" << opt::comm_cost.to_string() << "]\n";
413 std::clog << o("-S value")
414 << "minimum duration between comp. iterations"
415 << " [" << opt::min_comp_iter_duration << "]\n";
416 std::clog << o("-d value")
417 << "start computations after given number of lb iterations"
418 << " [" << opt::comp_iter_delay << "]\n";
419 std::clog << o("-D value")
420 << "start computations after given time"
421 << " [" << opt::comp_time_delay << "]\n";
423 std::clog << "\nParameters for the end of the simulation\n";
424 std::clog << o("-i value")
425 << "maximum number of lb. iterations, 0 for infinity"
426 << " [" << opt::lb_maxiter << "]\n";
427 std::clog << o("-I value")
428 << "maximum number of comp. iterations, 0 for infinity"
429 << " [" << opt::comp_maxiter << "]\n";
430 std::clog << o("-t value")
431 << "time limit (simulated time), 0 for infinity"
432 << " [" << opt::time_limit << "]\n";
433 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
434 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
436 if (opt::help_requested < 3)
439 std::clog << "\nLogging support\n"
440 << " See SimGrid documentation on:\n"
441 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
442 << " Existing categories are:\n"
443 << " simu : root of following categories\n"
444 << " main : messages from global infrastructure\n"
445 << " depl : messages from auto deployment (inherited from main)\n"
446 << " comm : messages from asynchronous pipes\n"
447 << " proc : messages from base process class\n"
448 << " loba : messages from load-balancer\n"
449 << " thrd : messages from thread wrapper class\n";
451 // std::clog << "\nMiscellaneous low-level parameters\n";