5 #include <unistd.h> // getopt
8 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
10 #include "loba_simple.h"
11 #include "loba_fairstrategy.h"
19 // A sum of loads if considered null if it is less than
20 // load_ratio_threshold percent of the sum of loads at init.
21 const double load_ratio_threshold = 1e-4;
24 std::string program_name;
25 int help_requested = 0;
26 bool version_requested = false;
28 // Simulation parameters
29 unsigned log_rate = 1;
31 // Platform and deployment
32 std::string platform_file;
33 std::string deployment_file;
35 // Automatic deployment
38 std::string topology("clique");
43 // Load balancing algorithm
44 std::string loba_algo("simple");
45 bool bookkeeping = false;
47 // Application parameters
48 cost_func comp_cost("1e9, 0"); // fixme: find better defaults
49 cost_func comm_cost("1, 0"); // fixme: find better defaults
50 unsigned comp_maxiter = 10; // fixme: find better defaults
51 unsigned lb_maxiter = comp_maxiter; // fixme: find better defaults
52 bool exit_on_close = false;
54 // Named parameters lists
55 loba_algorithms_type loba_algorithms;
56 loba_algorithms_type::loba_algorithms_type()
58 NOL_INSERT("fairstrategy", "balance with fair strategy", loba_fairstrategy);
59 NOL_INSERT("none", "no load-balancing (for testing only)", process);
60 NOL_INSERT("simple", "balance with least loaded neighbor", loba_simple);
63 topologies_type topologies;
64 topologies_type::topologies_type()
66 NOL_INSERT("btree", "binary tree topology, initial load at root",
68 NOL_INSERT("clique", "all connected topology", deployment_clique);
69 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
70 NOL_INSERT("line", "line topology, initial load at one end",
72 NOL_INSERT("ring", "ring topology", deployment_ring);
73 NOL_INSERT("star", "star topology, initial load at center",
75 NOL_INSERT("torus", "torus topology", deployment_torus);
85 static const char* on_off(bool b);
86 const char* descr(const char* str);
88 const char* val_or_string(const T& val, const char* str,
91 static bool nol_find_prefix(const T& nol, const char* descr,
95 std::string descr_str;
96 std::string val_or_string_str;
101 const char* opt_helper::on_off(bool b)
103 return b ? "on" : "off";
106 const char* opt_helper::descr(const char* str)
108 const int descr_width = 35;
109 std::string& res = descr_str;
111 res.resize(descr_width, '.');
115 template <typename T>
116 const char* opt_helper::val_or_string(const T& val, const char* str,
119 std::string& res = val_or_string_str;
121 std::ostringstream oss;
130 template <typename T>
131 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
134 bool result = nol.exists(name);
136 std::stack<std::string> candidates;
137 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
138 const std::string& fullname = nol.get_name(it);
139 if (fullname.compare(0, name.length(), name) == 0)
140 candidates.push(fullname);
142 switch (candidates.size()) {
144 ERROR2("unknownw %s -- %s", descr, name.c_str());
147 name = candidates.top();
150 DEBUG2("infered %s -- %s", descr, name.c_str());
153 ERROR2("ambiguous %s -- %s", descr, name.c_str());
154 while (!candidates.empty()) {
155 ERROR1(" candidates are -- %s", candidates.top().c_str());
164 bool opt::parse_args(int* argc, char* argv[])
168 opt::program_name = argv[0];
169 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
173 while ((c = getopt(*argc, argv, "a:bc:C:ehi:I:l:L:N:s:T:vV")) != -1) {
176 opt::loba_algo = optarg;
177 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
178 "load balancing algorithm",
183 opt::bookkeeping = true;
186 opt::exit_on_close = true;
189 opt::help_requested++;
192 opt::comp_cost = cost_func(optarg);
195 opt::comm_cost = cost_func(optarg);
198 std::istringstream(optarg) >> opt::comp_maxiter;
201 std::istringstream(optarg) >> opt::lb_maxiter;
202 ERROR0("option -I not implemented yet");
206 std::istringstream(optarg) >> opt::log_rate;
209 std::istringstream(optarg) >> opt::auto_depl::load;
212 std::istringstream(optarg) >> opt::auto_depl::nhosts;
215 std::istringstream(optarg) >> communicator::send_count_before_flush;
218 opt::auto_depl::topology = optarg;
219 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
220 opt::auto_depl::topology)
224 // nothing to do: this option is checked at the very
225 // beginning of main()
228 opt::version_requested = true;
231 ERROR1("invalid option -- '%c'", optopt);
237 if (opt::version_requested || opt::help_requested)
240 if (optind < *argc) {
241 opt::platform_file = argv[optind++];
243 ERROR0("missing parameter -- <plaform_file>");
246 if (optind < *argc) {
247 opt::deployment_file = argv[optind++];
249 opt::auto_depl::enabled = opt::deployment_file.empty();
251 while (optind < *argc) {
252 ERROR1("unused parameter -- \"%s\"", argv[optind++]);
263 #define DESCR(description, format, value) \
264 INFO2("| %s: " format, h.descr(description), value)
266 INFO0(",----[ Simulation parameters ]");
267 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
268 DESCR("platform file", "\"%s\"", platform_file.c_str());
269 if (auto_depl::enabled) {
270 INFO0("| automatic deployment enabled");
271 DESCR("- topology", "%s", auto_depl::topology.c_str());
272 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
274 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
277 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
279 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
280 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
281 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
282 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
283 DESCR("maximum number of comp. iterations", "%s",
284 h.val_or_string(comp_maxiter, "infinity"));
285 DESCR("maximum number of lb. iterations", "%s",
286 h.val_or_string(lb_maxiter, "infinity"));
287 DESCR("exit on close", "%s", h.on_off(exit_on_close));
288 DESCR("send count before flush", "%d",
289 communicator::send_count_before_flush);
298 #define o(opt) " " << std::setw(14) \
299 << std::left << (opt) << std::right << " "
301 #define so(subopt) std::setw(18) << (subopt) << " : "
303 #define so_list(name) do { \
304 name ## _type::iterator it; \
305 for (it = name.begin() ; it != name.end() ; ++it) \
306 std::clog << so(name.get_name(it)) \
307 << name.get_descr(it) << "\n"; \
311 std::clog << "Usage: " << opt::program_name
312 << " [options] <platform_file> [<deployment_file>]\n";
314 std::clog << "\nGlobal options\n";
316 << "print help and exit (use -hh or -hhh for extended help)\n";
317 if (opt::help_requested < 1)
320 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
321 std::clog << o("-V") << "print version and exit\n";
323 std::clog << "\nSimulation parameters\n";
324 std::clog << o("-l value")
325 << "print current load every n-th iterations, 0 to disable"
326 << " (" << opt::log_rate << ")\n";
328 << "verbose: do not override the default logging parameters\n";
330 std::clog << "\nAutomatic deployment options\n";
331 std::clog << o("-T name")
332 << "enable automatic deployment with selected topology"
333 << " (" << opt::auto_depl::topology << ")\n";
334 if (opt::help_requested > 1)
335 so_list(opt::topologies);
336 std::clog << o("-L value")
337 << "total load with auto deployment, 0 for number of hosts"
338 << " (" << opt::auto_depl::load << ")\n";
339 std::clog << o("-N value")
340 << "number of hosts to use with auto deployment,"
341 << " 0 for max. (" << opt::auto_depl::nhosts << ")\n";
343 std::clog << "\nLoad balancing algorithm\n";
344 std::clog << o("-a name") << "load balancing algorithm"
345 << " (" << opt::loba_algo << ")\n";
346 if (opt::help_requested > 1)
347 so_list(opt::loba_algorithms);
348 std::clog << o("-b") << "enable bookkeeping\n";
350 std::clog << "\nApplication parameters\n";
351 std::clog << o("-c [fn,...]f0")
352 << "polynomial factors for computation cost"
353 << " (" << opt::comp_cost.to_string() << ")\n";
354 std::clog << o("-C [fn,...]f0")
355 << "polynomial factors for communication cost"
356 << " (" << opt::comm_cost.to_string() << ")\n";
357 std::clog << o("-e") << "exit on reception of \"close\" message\n";
358 std::clog << o("-i value")
359 << "maximum number of comp. iterations, 0 for infinity"
360 << " (" << opt::comp_maxiter << ")\n";
361 std::clog << o("-I value")
362 << "maximum number of lb. iterations, 0 for infinity"
363 << " (" << opt::lb_maxiter << ")\n";
365 if (opt::help_requested < 3)
368 std::clog << "\nLogging support\n"
369 << " See SimGrid documentation on:\n"
370 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
371 << " Existing categories are:\n"
372 << " simu : root of following categories\n"
373 << " main : messages from global infrastructure\n"
374 << " depl : messages from auto deployment (inherited from main)\n"
375 << " comm : messages from asynchronous pipes\n"
376 << " proc : messages from base process class\n"
377 << " loba : messages from load-balancer\n";
379 std::clog << "\nMiscellaneous low-level parameters\n";
380 std::clog << o("-s count")
381 << "check for finished comm. every `count' send operation"
382 << " (" << communicator::send_count_before_flush << ")\n";