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_iter_duration = 1.0;
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);
95 static const char* on_off(bool b);
96 const char* descr(const char* str);
98 const char* val_or_string(const T& val, const char* str,
100 template <typename T>
101 static bool nol_find_prefix(const T& nol, const char* descr,
105 std::string descr_str;
106 std::string val_or_string_str;
111 const char* opt_helper::on_off(bool b)
113 return b ? "on" : "off";
116 const char* opt_helper::descr(const char* str)
118 const int descr_width = 40;
119 std::string& res = descr_str;
121 res.resize(descr_width, '.');
125 template <typename T>
126 const char* opt_helper::val_or_string(const T& val, const char* str,
129 std::string& res = val_or_string_str;
131 std::ostringstream oss;
140 template <typename T>
141 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
144 bool result = nol.exists(name);
146 std::stack<std::string> candidates;
147 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
148 const std::string& fullname = nol.get_name(it);
149 if (fullname.compare(0, name.length(), name) == 0)
150 candidates.push(fullname);
152 switch (candidates.size()) {
154 XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
157 name = candidates.top();
160 XBT_DEBUG("infered %s -- %s", descr, name.c_str());
163 XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
164 while (!candidates.empty()) {
165 XBT_ERROR(" candidates are -- %s", candidates.top().c_str());
174 bool opt::parse_args(int* argc, char* argv[])
178 opt::program_name = argv[0];
179 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
183 while ((c = getopt(*argc, argv, "a:bc:C:ehi:I:l:L:N:s:t:T:vV")) != -1) {
186 opt::loba_algo = optarg;
187 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
188 "load balancing algorithm",
193 opt::bookkeeping = !opt::bookkeeping;
196 opt::exit_on_close = !opt::exit_on_close;
199 opt::help_requested++;
202 opt::comp_cost = cost_func(optarg);
205 opt::comm_cost = cost_func(optarg);
208 std::istringstream(optarg) >> opt::lb_maxiter;
211 std::istringstream(optarg) >> opt::comp_maxiter;
214 std::istringstream(optarg) >> opt::log_rate;
217 std::istringstream(optarg) >> opt::auto_depl::load;
220 std::istringstream(optarg) >> opt::auto_depl::nhosts;
223 std::istringstream(optarg) >> opt::min_iter_duration;
226 std::istringstream(optarg) >> opt::time_limit;
229 opt::auto_depl::topology = optarg;
230 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
231 opt::auto_depl::topology)
235 // nothing to do: this option is checked at the very
236 // beginning of main()
239 opt::version_requested = true;
242 XBT_ERROR("invalid option -- '%c'", optopt);
248 if (opt::version_requested || opt::help_requested)
251 if (optind < *argc) {
252 opt::platform_file = argv[optind++];
254 XBT_ERROR("missing parameter -- <plaform_file>");
257 if (optind < *argc) {
258 opt::deployment_file = argv[optind++];
260 opt::auto_depl::enabled = opt::deployment_file.empty();
262 while (optind < *argc) {
263 XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
274 #define DESCR(description, format, value) \
275 XBT_INFO("| %s: " format, h.descr(description), value)
277 XBT_INFO(",----[ Simulation parameters ]");
278 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
279 DESCR("platform file", "\"%s\"", platform_file.c_str());
280 if (auto_depl::enabled) {
281 XBT_INFO("| automatic deployment enabled");
282 DESCR("- topology", "%s", auto_depl::topology.c_str());
283 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
285 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
288 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
290 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
291 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
292 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
293 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
294 DESCR("minimum duration between iterations", "%g", min_iter_duration);
295 DESCR("maximum number of lb. iterations", "%s",
296 h.val_or_string(lb_maxiter, "infinity"));
297 DESCR("maximum number of comp. iterations", "%s",
298 h.val_or_string(comp_maxiter, "infinity"));
299 DESCR("time limit", "%s", h.val_or_string(time_limit, "infinity"));
300 DESCR("exit on close", "%s", h.on_off(exit_on_close));
309 #define o(opt) " " << std::setw(14) \
310 << std::left << (opt) << std::right << " "
312 #define so(subopt) std::setw(18) << (subopt) << " : "
314 #define so_list(name) do { \
315 name ## _type::iterator it; \
316 for (it = name.begin() ; it != name.end() ; ++it) \
317 std::clog << so(name.get_name(it)) \
318 << name.get_descr(it) << "\n"; \
322 std::clog << "Usage: " << opt::program_name
323 << " [options] <platform_file> [<deployment_file>]\n";
325 std::clog << "\nGlobal options\n";
327 << "print help and exit (use -hh or -hhh for extended help)\n";
328 if (opt::help_requested < 1)
331 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
332 std::clog << o("-V") << "print version and exit\n";
334 std::clog << "\nSimulation parameters\n";
335 std::clog << o("-l value")
336 << "print current load every n lb iterations, 0 to disable"
337 << " [" << opt::log_rate << "]\n";
339 << "verbose: do not override the default logging parameters\n";
341 std::clog << "\nAutomatic deployment options\n";
342 std::clog << o("-T name")
343 << "enable automatic deployment with selected topology"
344 << " [" << opt::auto_depl::topology << "]\n";
345 if (opt::help_requested > 1)
346 so_list(opt::topologies);
347 std::clog << o("-L value")
348 << "total load with auto deployment, 0 for number of hosts"
349 << " [" << opt::auto_depl::load << "]\n";
350 std::clog << o("-N value")
351 << "number of hosts to use with auto deployment, 0 for max."
352 << " [" << opt::auto_depl::nhosts << "]\n";
354 std::clog << "\nLoad balancing algorithm\n";
355 std::clog << o("-a name") << "load balancing algorithm"
356 << " [" << opt::loba_algo << "]\n";
357 if (opt::help_requested > 1)
358 so_list(opt::loba_algorithms);
359 std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
360 << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
362 std::clog << "\nApplication parameters\n";
363 std::clog << o("-c [fn,...]f0")
364 << "polynomial factors for computation cost"
365 << " [" << opt::comp_cost.to_string() << "]\n";
366 std::clog << o("-C [fn,...]f0")
367 << "polynomial factors for communication cost"
368 << " [" << opt::comm_cost.to_string() << "]\n";
369 std::clog << o("-s value")
370 << "minimum duration between iterations"
371 << " [" << opt::min_iter_duration << "]\n";
373 std::clog << "\nParameters for the end of the simulation\n";
374 std::clog << o("-i value")
375 << "maximum number of lb. iterations, 0 for infinity"
376 << " [" << opt::lb_maxiter << "]\n";
377 std::clog << o("-I value")
378 << "maximum number of comp. iterations, 0 for infinity"
379 << " [" << opt::comp_maxiter << "]\n";
380 std::clog << o("-t value")
381 << "time limit (simulated time), 0 for infinity"
382 << " [" << opt::time_limit << "]\n";
383 std::clog << o("-e") << "toggle exit on reception of \"close\" message"
384 << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
386 if (opt::help_requested < 3)
389 std::clog << "\nLogging support\n"
390 << " See SimGrid documentation on:\n"
391 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
392 << " Existing categories are:\n"
393 << " simu : root of following categories\n"
394 << " main : messages from global infrastructure\n"
395 << " depl : messages from auto deployment (inherited from main)\n"
396 << " comm : messages from asynchronous pipes\n"
397 << " proc : messages from base process class\n"
398 << " loba : messages from load-balancer\n";
400 // std::clog << "\nMiscellaneous low-level parameters\n";