6 #include <unistd.h> // getopt
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
11 #include "deployment.h"
13 #include "loba_besteffort.h"
14 #include "loba_bulk.h"
15 #include "loba_fairstrategy.h"
16 #include "loba_makhoul.h"
17 #include "loba_makhoul2.h"
18 #include "loba_simple.h"
23 #define DATA_DESCR_WIDTH 42
29 // A sum of loads if considered null if it is less than
30 // load_ratio_threshold percent of the sum of loads at init.
31 const double load_ratio_threshold = 1e-4;
34 std::string program_name;
35 int help_requested = 0;
36 bool version_requested = false;
37 int option_x = 0; // hidden option, for debugging purpose
39 // Simulation parameters
41 bool exit_request = false;
43 // Platform and deployment
44 std::string platform_file;
45 std::string deployment_file;
47 // Automatic deployment
50 std::string topology("clique");
53 bool random_distribution = false;
54 unsigned long random_seed = 0;
57 // Load balancing algorithm
58 std::string loba_algo("simple");
59 bool bookkeeping = false;
60 double min_lb_iter_duration = 1.0; // fixme: find better defaults
61 double min_transfer_amount = 0.0;
62 double max_transfer_amount = 0.0;
64 // Application parameters
65 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
66 cost_func comm_cost("1e6, 0"); // fixme: find better defaults
67 double min_comp_iter_duration = 1.0; // fixme: find better defaults
68 unsigned comp_iter_delay = 0; // fixme: find better defaults
69 double comp_time_delay = 0.0; // fixme: find better defaults
71 // Parameters for the end of the simulation
72 unsigned lb_maxiter = 0;
73 unsigned comp_maxiter = 0;
74 double time_limit = 0;
75 bool exit_on_close = true;
77 // Named parameters lists
78 loba_algorithms_type loba_algorithms;
79 loba_algorithms_type::loba_algorithms_type()
81 NOL_INSERT("besteffort", "balance with best effort strategy",
83 NOL_INSERT("bulk", "describe your algorithm here...",
85 NOL_INSERT("fairstrategy", "balance with fair strategy",
87 NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
89 NOL_INSERT("makhoul2", "balance with Makhoul's source code",
91 NOL_INSERT("none", "no load-balancing (for testing only)",
93 NOL_INSERT("simple", "balance with least loaded neighbor",
97 topologies_type topologies;
98 topologies_type::topologies_type()
100 NOL_INSERT("btree", "binary tree topology, initial load at root",
102 NOL_INSERT("clique", "all connected topology", deployment_clique);
103 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
104 NOL_INSERT("line", "line topology, initial load at one end",
106 NOL_INSERT("ring", "ring topology", deployment_ring);
107 NOL_INSERT("star", "star topology, initial load at center",
109 NOL_INSERT("torus", "torus topology", deployment_torus);
116 // local helper class
119 template <typename T>
120 static bool parse_arg(char opt, const char *arg, T& val);
121 static const char* on_off(bool b);
122 const char* descr(const char* str);
123 template <typename T>
124 const char* val_or_string(const T& val, const char* str,
126 template <typename T>
127 static bool nol_find_prefix(const T& nol, const char* descr,
131 std::string descr_str;
132 std::string val_or_string_str;
137 template <typename T>
138 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
140 std::istringstream str(arg);
141 bool result = (str >> val) && str.eof();
143 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
147 const char* opt_helper::on_off(bool b)
149 return b ? "on" : "off";
152 const char* opt_helper::descr(const char* str)
154 std::string& res = descr_str;
156 res.resize(DATA_DESCR_WIDTH, '.');
160 template <typename T>
161 const char* opt_helper::val_or_string(const T& val, const char* str,
164 std::string& res = val_or_string_str;
166 std::ostringstream oss;
175 template <typename T>
176 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
179 bool result = nol.exists(name);
181 std::stack<std::string> candidates;
182 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
183 const std::string& fullname = nol.get_name(it);
184 if (fullname.compare(0, name.length(), name) == 0)
185 candidates.push(fullname);
187 switch (candidates.size()) {
189 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
192 name = candidates.top();
195 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
198 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
199 while (!candidates.empty()) {
200 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
209 bool opt::parse_args(int* argc, char* argv[])
213 opt::program_name = argv[0];
214 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
216 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
220 while ((c = getopt(*argc, argv,
221 "a:bc:C:d:D:ehi:I:l:L:m:M:N:r:Rs:S:t:T:vVx:")) != -1) {
224 opt::loba_algo = optarg;
225 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
226 "load balancing algorithm",
231 opt::bookkeeping = !opt::bookkeeping;
234 opt::exit_on_close = !opt::exit_on_close;
237 opt::help_requested++;
241 opt::comp_cost = cost_func(optarg);
243 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
250 opt::comm_cost = cost_func(optarg);
252 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
258 PARSE_ARG(opt::comp_iter_delay);
261 PARSE_ARG(opt::comp_time_delay);
264 PARSE_ARG(opt::lb_maxiter);
267 PARSE_ARG(opt::comp_maxiter);
270 PARSE_ARG(opt::log_rate);
273 PARSE_ARG(opt::auto_depl::load);
276 PARSE_ARG(opt::min_transfer_amount);
279 PARSE_ARG(opt::max_transfer_amount);
282 PARSE_ARG(opt::auto_depl::nhosts);
285 PARSE_ARG(opt::auto_depl::random_seed);
288 opt::auto_depl::random_distribution =
289 !opt::auto_depl::random_distribution;
292 PARSE_ARG(opt::min_lb_iter_duration);
295 PARSE_ARG(opt::min_comp_iter_duration);
298 PARSE_ARG(opt::time_limit);
301 opt::auto_depl::topology = optarg;
302 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
303 opt::auto_depl::topology)
307 // nothing to do: this option is checked at the very
308 // beginning of main()
311 opt::version_requested = true;
314 PARSE_ARG(opt::option_x);
315 XBT_WARN("option_x set to %d", opt::option_x);
318 XBT_ERROR("invalid option -- '%c'", optopt);
326 if (opt::version_requested || opt::help_requested)
329 if (optind < *argc) {
330 opt::platform_file = argv[optind++];
332 XBT_ERROR("missing parameter -- <plaform_file>");
335 if (optind < *argc) {
336 opt::deployment_file = argv[optind++];
338 opt::auto_depl::enabled = opt::deployment_file.empty();
340 while (optind < *argc) {
341 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
345 if (opt::max_transfer_amount &&
346 opt::max_transfer_amount < opt::min_transfer_amount) {
347 XBT_ERROR("max. data transfer amount < min. data transfer amount");
351 if (!opt::auto_depl::random_seed)
352 opt::auto_depl::random_seed = time(NULL);
361 #define DESCR(description, format, value) \
362 XBT_INFO("| %s: " format, h.descr(description), value)
364 XBT_INFO(",----[ Simulation parameters ]");
365 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
366 DESCR("platform file", "\"%s\"", platform_file.c_str());
367 if (auto_depl::enabled) {
368 XBT_INFO("| automatic deployment enabled");
369 DESCR("- topology", "%s", auto_depl::topology.c_str());
370 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
372 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
374 DESCR("- random initial load distribution", "%s",
375 h.on_off(auto_depl::random_distribution));
376 DESCR("- random seed", "%s",
377 h.val_or_string(auto_depl::random_seed, "time based"));
379 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
381 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
382 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
383 DESCR("minimum duration between lb. iterations", "%g",
384 min_lb_iter_duration);
385 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
386 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
387 DESCR("minimum data transfer amount", "%g", min_transfer_amount);
388 DESCR("maximum data transfer amount", "%s",
389 h.val_or_string(max_transfer_amount, "no limit"));
390 DESCR("minimum duration between comp. iterations", "%g",
391 min_comp_iter_duration);
392 DESCR("computations start after lb. iter", "%u", comp_iter_delay);
393 DESCR("computations start after time", "%g", comp_time_delay);
394 DESCR("maximum number of lb. iterations", "%s",
395 h.val_or_string(lb_maxiter, "no limit"));
396 DESCR("maximum number of comp. iterations", "%s",
397 h.val_or_string(comp_maxiter, "no limit"));
398 DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
399 DESCR("exit on close", "%s", h.on_off(exit_on_close));
408 #define o(opt) " " << std::setw(14) \
409 << std::left << (opt) << std::right << " "
411 #define so(subopt) std::setw(18) << (subopt) << " : "
413 #define so_list(name) do { \
414 name ## _type::iterator it; \
415 for (it = name.begin() ; it != name.end() ; ++it) \
416 std::clog << so(name.get_name(it)) \
417 << name.get_descr(it) << "\n"; \
421 std::clog << "Usage: " << opt::program_name
422 << " [options] <platform_file> [<deployment_file>]\n";
424 std::clog << "\nGlobal options\n";
426 << "print help and exit (use -hh or -hhh for extended help)\n";
427 if (opt::help_requested < 1)
430 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
431 std::clog << o("-V") << "print version and exit\n";
433 std::clog << "\nSimulation parameters\n";
434 std::clog << o("-l value")
435 << "print current load every n lb iterations, 0 to disable"
436 << " [" << opt::log_rate << "]\n";
438 << "verbose: do not override the default logging parameters\n";
440 std::clog << "\nAutomatic deployment options\n";
441 std::clog << o("-T name")
442 << "enable automatic deployment with selected topology"
443 << " [" << opt::auto_depl::topology << "]\n";
444 if (opt::help_requested > 1)
445 so_list(opt::topologies);
446 std::clog << o("-L value")
447 << "total load with auto deployment, 0 for number of hosts"
448 << " [" << opt::auto_depl::load << "]\n";
449 std::clog << o("-N value")
450 << "number of hosts to use with auto deployment, 0 for max."
451 << " [" << opt::auto_depl::nhosts << "]\n";
453 << "toggle random initial load distribution"
454 << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
456 std::clog << o("-r value")
457 << "random seed, 0 for using it on time()"
458 << " [" << opt::auto_depl::random_seed << "]\n";
460 std::clog << "\nLoad balancing algorithm\n";
461 std::clog << o("-a name") << "load balancing algorithm"
462 << " [" << opt::loba_algo << "]\n";
463 if (opt::help_requested > 1)
464 so_list(opt::loba_algorithms);
465 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
466 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
467 std::clog << o("-s value")
468 << "minimum duration between lb. iterations"
469 << " [" << opt::min_lb_iter_duration << "]\n";
471 std::clog << "\nApplication parameters\n";
472 std::clog << o("-c [aN,...]a0")
473 << "polynomial factors for computation cost"
474 << " [" << opt::comp_cost.to_string() << "]\n";
475 std::clog << o("-C [aN,...]a0")
476 << "polynomial factors for communication cost"
477 << " [" << opt::comm_cost.to_string() << "]\n";
478 std::clog << o("-m value")
479 << "minimum data transfer amount"
480 << " [" << opt::min_transfer_amount << "]\n";
481 std::clog << o("-M value")
482 << "maximum data transfer amount, 0 for no limit"
483 << " [" << opt::max_transfer_amount << "]\n";
484 std::clog << o("-S value")
485 << "minimum duration between comp. iterations"
486 << " [" << opt::min_comp_iter_duration << "]\n";
487 std::clog << o("-d value")
488 << "start computations after given number of lb iterations"
489 << " [" << opt::comp_iter_delay << "]\n";
490 std::clog << o("-D value")
491 << "start computations after given time"
492 << " [" << opt::comp_time_delay << "]\n";
494 std::clog << "\nParameters for the end of the simulation\n";
495 std::clog << o("-i value")
496 << "maximum number of lb. iterations, 0 for no limit"
497 << " [" << opt::lb_maxiter << "]\n";
498 std::clog << o("-I value")
499 << "maximum number of comp. iterations, 0 for no limit"
500 << " [" << opt::comp_maxiter << "]\n";
501 std::clog << o("-t value")
502 << "time limit (simulated time), 0 for no limit"
503 << " [" << opt::time_limit << "]\n";
504 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
505 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
507 if (opt::help_requested < 3)
510 std::clog << "\nLogging support\n"
511 << " See SimGrid documentation on:\n"
512 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
513 << " Existing categories are:\n"
514 << " simu : root of following categories\n"
515 << " main : messages from global infrastructure\n"
516 << " depl : messages from auto deployment (inherited from main)\n"
517 << " comm : messages from asynchronous pipes\n"
518 << " proc : messages from base process class\n"
519 << " loba : messages from load-balancer\n"
520 << " thrd : messages from thread wrapper class\n";
522 // std::clog << "\nMiscellaneous low-level parameters\n";