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
33 unsigned log_rate = 1;
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);
96 static const char* on_off(bool b);
97 const char* descr(const char* str);
99 const char* val_or_string(const T& val, const char* str,
101 template <typename T>
102 static bool nol_find_prefix(const T& nol, const char* descr,
106 std::string descr_str;
107 std::string val_or_string_str;
112 const char* opt_helper::on_off(bool b)
114 return b ? "on" : "off";
117 const char* opt_helper::descr(const char* str)
119 const int descr_width = 40;
120 std::string& res = descr_str;
122 res.resize(descr_width, '.');
126 template <typename T>
127 const char* opt_helper::val_or_string(const T& val, const char* str,
130 std::string& res = val_or_string_str;
132 std::ostringstream oss;
141 template <typename T>
142 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
145 bool result = nol.exists(name);
147 std::stack<std::string> candidates;
148 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
149 const std::string& fullname = nol.get_name(it);
150 if (fullname.compare(0, name.length(), name) == 0)
151 candidates.push(fullname);
153 switch (candidates.size()) {
155 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
158 name = candidates.top();
161 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
164 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
165 while (!candidates.empty()) {
166 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
175 bool opt::parse_args(int* argc, char* argv[])
179 opt::program_name = argv[0];
180 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
184 while ((c = getopt(*argc, argv, "a:bc:C:ehi:I:l:L:N:s:S:t:T:vV")) != -1) {
187 opt::loba_algo = optarg;
188 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
189 "load balancing algorithm",
194 opt::bookkeeping = !opt::bookkeeping;
197 opt::exit_on_close = !opt::exit_on_close;
200 opt::help_requested++;
203 opt::comp_cost = cost_func(optarg);
206 opt::comm_cost = cost_func(optarg);
209 std::istringstream(optarg) >> opt::lb_maxiter;
212 std::istringstream(optarg) >> opt::comp_maxiter;
215 std::istringstream(optarg) >> opt::log_rate;
218 std::istringstream(optarg) >> opt::auto_depl::load;
221 std::istringstream(optarg) >> opt::auto_depl::nhosts;
224 std::istringstream(optarg) >> opt::min_lb_iter_duration;
227 std::istringstream(optarg) >> opt::min_comp_iter_duration;
230 std::istringstream(optarg) >> opt::time_limit;
233 opt::auto_depl::topology = optarg;
234 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
235 opt::auto_depl::topology)
239 // nothing to do: this option is checked at the very
240 // beginning of main()
243 opt::version_requested = true;
246 XBT_ERROR("invalid option -- '%c'", optopt);
252 if (opt::version_requested || opt::help_requested)
255 if (optind < *argc) {
256 opt::platform_file = argv[optind++];
258 XBT_ERROR("missing parameter -- <plaform_file>");
261 if (optind < *argc) {
262 opt::deployment_file = argv[optind++];
264 opt::auto_depl::enabled = opt::deployment_file.empty();
266 while (optind < *argc) {
267 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
278 #define DESCR(description, format, value) \
279 XBT_INFO("| %s: " format, h.descr(description), value)
281 XBT_INFO(",----[ Simulation parameters ]");
282 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
283 DESCR("platform file", "\"%s\"", platform_file.c_str());
284 if (auto_depl::enabled) {
285 XBT_INFO("| automatic deployment enabled");
286 DESCR("- topology", "%s", auto_depl::topology.c_str());
287 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
289 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
292 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
294 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
295 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
296 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
297 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
298 DESCR("minimum duration between lb. iterations", "%g",
299 min_lb_iter_duration);
300 DESCR("minimum duration between comp. iterations", "%g",
301 min_comp_iter_duration);
302 DESCR("maximum number of lb. iterations", "%s",
303 h.val_or_string(lb_maxiter, "infinity"));
304 DESCR("maximum number of comp. iterations", "%s",
305 h.val_or_string(comp_maxiter, "infinity"));
306 DESCR("time limit", "%s", h.val_or_string(time_limit, "infinity"));
307 DESCR("exit on close", "%s", h.on_off(exit_on_close));
316 #define o(opt) " " << std::setw(14) \
317 << std::left << (opt) << std::right << " "
319 #define so(subopt) std::setw(18) << (subopt) << " : "
321 #define so_list(name) do { \
322 name ## _type::iterator it; \
323 for (it = name.begin() ; it != name.end() ; ++it) \
324 std::clog << so(name.get_name(it)) \
325 << name.get_descr(it) << "\n"; \
329 std::clog << "Usage: " << opt::program_name
330 << " [options] <platform_file> [<deployment_file>]\n";
332 std::clog << "\nGlobal options\n";
334 << "print help and exit (use -hh or -hhh for extended help)\n";
335 if (opt::help_requested < 1)
338 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
339 std::clog << o("-V") << "print version and exit\n";
341 std::clog << "\nSimulation parameters\n";
342 std::clog << o("-l value")
343 << "print current load every n lb iterations, 0 to disable"
344 << " [" << opt::log_rate << "]\n";
346 << "verbose: do not override the default logging parameters\n";
348 std::clog << "\nAutomatic deployment options\n";
349 std::clog << o("-T name")
350 << "enable automatic deployment with selected topology"
351 << " [" << opt::auto_depl::topology << "]\n";
352 if (opt::help_requested > 1)
353 so_list(opt::topologies);
354 std::clog << o("-L value")
355 << "total load with auto deployment, 0 for number of hosts"
356 << " [" << opt::auto_depl::load << "]\n";
357 std::clog << o("-N value")
358 << "number of hosts to use with auto deployment, 0 for max."
359 << " [" << opt::auto_depl::nhosts << "]\n";
361 std::clog << "\nLoad balancing algorithm\n";
362 std::clog << o("-a name") << "load balancing algorithm"
363 << " [" << opt::loba_algo << "]\n";
364 if (opt::help_requested > 1)
365 so_list(opt::loba_algorithms);
366 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
367 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
369 std::clog << "\nApplication parameters\n";
370 std::clog << o("-c [aN,...]a0")
371 << "polynomial factors for computation cost"
372 << " [" << opt::comp_cost.to_string() << "]\n";
373 std::clog << o("-C [aN,...]a0")
374 << "polynomial factors for communication cost"
375 << " [" << opt::comm_cost.to_string() << "]\n";
376 std::clog << o("-s value")
377 << "minimum duration between lb. iterations"
378 << " [" << opt::min_lb_iter_duration << "]\n";
379 std::clog << o("-S value")
380 << "minimum duration between comp. iterations"
381 << " [" << opt::min_comp_iter_duration << "]\n";
383 std::clog << "\nParameters for the end of the simulation\n";
384 std::clog << o("-i value")
385 << "maximum number of lb. iterations, 0 for infinity"
386 << " [" << opt::lb_maxiter << "]\n";
387 std::clog << o("-I value")
388 << "maximum number of comp. iterations, 0 for infinity"
389 << " [" << opt::comp_maxiter << "]\n";
390 std::clog << o("-t value")
391 << "time limit (simulated time), 0 for infinity"
392 << " [" << opt::time_limit << "]\n";
393 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
394 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
396 if (opt::help_requested < 3)
399 std::clog << "\nLogging support\n"
400 << " See SimGrid documentation on:\n"
401 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
402 << " Existing categories are:\n"
403 << " simu : root of following categories\n"
404 << " main : messages from global infrastructure\n"
405 << " depl : messages from auto deployment (inherited from main)\n"
406 << " comm : messages from asynchronous pipes\n"
407 << " proc : messages from base process class\n"
408 << " loba : messages from load-balancer\n";
410 // std::clog << "\nMiscellaneous low-level parameters\n";