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 maxiter = 10; // fixme: find better defaults
51 bool exit_on_close = false;
53 // Named parameters lists
54 loba_algorithms_type loba_algorithms;
55 loba_algorithms_type::loba_algorithms_type()
57 NOL_INSERT("fairstrategy", "balance with fair strategy", loba_fairstrategy);
58 NOL_INSERT("none", "no load-balancing (for testing only)", process);
59 NOL_INSERT("simple", "balance with least loaded neighbor", loba_simple);
62 topologies_type topologies;
63 topologies_type::topologies_type()
65 NOL_INSERT("btree", "binary tree topology, initial load at root",
67 NOL_INSERT("clique", "all connected topology", deployment_clique);
68 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
69 NOL_INSERT("line", "line topology, initial load at one end",
71 NOL_INSERT("ring", "ring topology", deployment_ring);
72 NOL_INSERT("star", "star topology, initial load at center",
74 NOL_INSERT("torus", "torus topology", deployment_torus);
81 // some helper functions for opt::print()
83 std::string* descr_str;
84 std::string* val_or_string_str;
86 void print_helper_init()
88 descr_str = new std::string;
89 val_or_string_str = new std::string;
92 void print_helper_destroy()
94 delete val_or_string_str;
98 const char* descr(const char* str)
100 const int descr_width = 35;
101 std::string* res = descr_str;
103 res->resize(descr_width, '.');
107 const char* on_off(bool b)
109 return b ? "on" : "off";
112 template <typename T>
113 const char* val_or_string(const T& val, const char* str, const T& deflt = 0)
115 std::string* res = val_or_string_str;
117 std::ostringstream oss;
126 // helper function for opt::parse_args()
128 template <typename T>
129 bool nol_find_prefix(const T& nol, const char* descr, std::string& name)
131 bool result = nol.exists(name);
133 std::stack<std::string> candidates;
134 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
135 const std::string& fullname = nol.get_name(it);
136 if (fullname.compare(0, name.length(), name) == 0)
137 candidates.push(fullname);
139 switch (candidates.size()) {
141 ERROR2("unknownw %s -- %s", descr, name.c_str());
144 name = candidates.top();
147 DEBUG2("infered %s -- %s", descr, name.c_str());
150 ERROR2("ambiguous %s -- %s", descr, name.c_str());
151 while (!candidates.empty()) {
152 ERROR1(" candidates are -- %s", candidates.top().c_str());
163 bool opt::parse_args(int* argc, char* argv[])
167 opt::program_name = argv[0];
168 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
172 while ((c = getopt(*argc, argv, "a:bc:C:ehi:l:L:N:T:vV")) != -1) {
175 opt::loba_algo = optarg;
176 result = nol_find_prefix(opt::loba_algorithms,
177 "load balancing algorithm",
181 opt::bookkeeping = true;
184 opt::exit_on_close = true;
187 opt::help_requested++;
190 opt::comp_cost = cost_func(optarg);
193 opt::comm_cost = cost_func(optarg);
196 std::istringstream(optarg) >> opt::maxiter;
199 std::istringstream(optarg) >> opt::log_rate;
202 std::istringstream(optarg) >> opt::auto_depl::load;
205 std::istringstream(optarg) >> opt::auto_depl::nhosts;
208 opt::auto_depl::topology = optarg;
209 result = nol_find_prefix(opt::topologies, "topology",
210 opt::auto_depl::topology);
213 // nothing to do: this option is checked at the very
214 // beginning of main()
217 opt::version_requested = true;
220 ERROR1("invalid option -- '%c'", optopt);
226 if (opt::version_requested || opt::help_requested)
229 if (optind < *argc) {
230 opt::platform_file = argv[optind++];
232 ERROR0("missing parameter -- <plaform_file>");
235 if (optind < *argc) {
236 opt::deployment_file = argv[optind++];
238 opt::auto_depl::enabled = opt::deployment_file.empty();
240 while (optind < *argc) {
241 ERROR1("unused parameter -- \"%s\"", argv[optind++]);
251 #define DESCR(description, format, value) \
252 INFO2("| %s: " format, descr(description), value)
253 INFO0(",----[ Simulation parameters ]");
254 DESCR("log rate", "%s", val_or_string(log_rate, "disabled"));
255 DESCR("platform file", "\"%s\"", platform_file.c_str());
256 if (auto_depl::enabled) {
257 INFO0("| automatic deployment enabled");
258 DESCR("- topology", "%s", auto_depl::topology.c_str());
259 DESCR("- number of hosts", "%s", val_or_string(auto_depl::nhosts,
261 DESCR("- initial load", "%s", val_or_string(auto_depl::load,
264 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
266 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
267 DESCR("bookkeeping", "%s", on_off(bookkeeping));
268 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
269 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
270 DESCR("maximum number of iterations", "%s",
271 val_or_string(maxiter, "infinity"));
272 DESCR("exit on close", "%s", on_off(exit_on_close));
275 print_helper_destroy();
281 #define o(opt) " " << std::setw(14) \
282 << std::left << (opt) << std::right << " "
284 #define so(subopt) std::setw(18) << (subopt) << " : "
286 #define so_list(name) do { \
287 name ## _type::iterator it; \
288 for (it = name.begin() ; it != name.end() ; ++it) \
289 std::clog << so(name.get_name(it)) \
290 << name.get_descr(it) << "\n"; \
294 std::clog << "Usage: " << opt::program_name
295 << " [options] <platform_file> [<deployment_file>]\n";
297 std::clog << "\nGlobal options\n";
299 << "print help and exit (use -hh or -hhh for extended help)\n";
300 if (opt::help_requested < 1)
303 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
304 std::clog << o("-V") << "print version and exit\n";
306 std::clog << "\nSimulation parameters\n";
307 std::clog << o("-l value")
308 << "print current load every n-th iterations, 0 to disable"
309 << " (" << opt::log_rate << ")\n";
311 << "verbose: do not override the default logging parameters\n";
313 std::clog << "\nAutomatic deployment options\n";
314 std::clog << o("-T name")
315 << "enable automatic deployment with selected topology"
316 << " (" << opt::auto_depl::topology << ")\n";
317 if (opt::help_requested > 1)
318 so_list(opt::topologies);
319 std::clog << o("-L value")
320 << "total load with auto deployment, 0 for number of hosts"
321 << " (" << opt::auto_depl::load << ")\n";
322 std::clog << o("-N value")
323 << "number of hosts to use with auto deployment,"
324 << " 0 for max. (" << opt::auto_depl::nhosts << ")\n";
326 std::clog << "\nLoad balancing algorithm\n";
327 std::clog << o("-a name") << "load balancing algorithm"
328 << " (" << opt::loba_algo << ")\n";
329 if (opt::help_requested > 1)
330 so_list(opt::loba_algorithms);
331 std::clog << o("-b") << "enable bookkeeping\n";
333 std::clog << "\nApplication parameters\n";
334 std::clog << o("-c [fn,...]f0")
335 << "polynomial factors for computation cost"
336 << " (" << opt::comp_cost.to_string() << ")\n";
337 std::clog << o("-C [fn,...]f0")
338 << "polynomial factors for communication cost"
339 << " (" << opt::comm_cost.to_string() << ")\n";
340 std::clog << o("-e") << "exit on reception of \"close\" message\n";
341 std::clog << o("-i value")
342 << "maximum number of iterations, 0 for infinity"
343 << " (" << opt::maxiter << ")\n";
345 if (opt::help_requested < 3)
348 std::clog << "\nLogging support\n"
349 << " See SimGrid documentation on:\n"
350 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
351 << " Existing categories are:\n"
352 << " simu : root of following categories\n"
353 << " main : messages from global infrastructure\n"
354 << " depl : messages from auto deployment (inherited from main)\n"
355 << " comm : messages from asynchronous pipes\n"
356 << " proc : messages from base process class\n"
357 << " loba : messages from load-balancer\n";