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"
21 // A sum of loads if considered null if it is less than
22 // load_ratio_threshold percent of the sum of loads at init.
23 const double load_ratio_threshold = 1e-4;
26 std::string program_name;
27 int help_requested = 0;
28 bool version_requested = false;
30 // Simulation parameters
31 unsigned log_rate = 1;
33 // Platform and deployment
34 std::string platform_file;
35 std::string deployment_file;
37 // Automatic deployment
40 std::string topology("clique");
45 // Load balancing algorithm
46 std::string loba_algo("simple");
47 bool bookkeeping = false;
49 // Application parameters
50 // fixme: find better defaults
51 cost_func comp_cost("1e9, 0");
52 cost_func comm_cost("1, 0");
54 // Parameters for the end of the simulation
55 // fixme: find better defaults
56 unsigned comp_maxiter = 10;
57 unsigned lb_maxiter = comp_maxiter;
58 bool exit_on_close = false;
60 // Named parameters lists
61 loba_algorithms_type loba_algorithms;
62 loba_algorithms_type::loba_algorithms_type()
64 NOL_INSERT("fairstrategy", "balance with fair strategy", loba_fairstrategy);
65 NOL_INSERT("none", "no load-balancing (for testing only)", process);
66 NOL_INSERT("simple", "balance with least loaded neighbor", loba_simple);
69 topologies_type topologies;
70 topologies_type::topologies_type()
72 NOL_INSERT("btree", "binary tree topology, initial load at root",
74 NOL_INSERT("clique", "all connected topology", deployment_clique);
75 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
76 NOL_INSERT("line", "line topology, initial load at one end",
78 NOL_INSERT("ring", "ring topology", deployment_ring);
79 NOL_INSERT("star", "star topology, initial load at center",
81 NOL_INSERT("torus", "torus topology", deployment_torus);
91 static const char* on_off(bool b);
92 const char* descr(const char* str);
94 const char* val_or_string(const T& val, const char* str,
97 static bool nol_find_prefix(const T& nol, const char* descr,
101 std::string descr_str;
102 std::string val_or_string_str;
107 const char* opt_helper::on_off(bool b)
109 return b ? "on" : "off";
112 const char* opt_helper::descr(const char* str)
114 const int descr_width = 35;
115 std::string& res = descr_str;
117 res.resize(descr_width, '.');
121 template <typename T>
122 const char* opt_helper::val_or_string(const T& val, const char* str,
125 std::string& res = val_or_string_str;
127 std::ostringstream oss;
136 template <typename T>
137 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
140 bool result = nol.exists(name);
142 std::stack<std::string> candidates;
143 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
144 const std::string& fullname = nol.get_name(it);
145 if (fullname.compare(0, name.length(), name) == 0)
146 candidates.push(fullname);
148 switch (candidates.size()) {
150 ERROR2("unknownw %s -- %s", descr, name.c_str());
153 name = candidates.top();
156 DEBUG2("infered %s -- %s", descr, name.c_str());
159 ERROR2("ambiguous %s -- %s", descr, name.c_str());
160 while (!candidates.empty()) {
161 ERROR1(" candidates are -- %s", candidates.top().c_str());
170 bool opt::parse_args(int* argc, char* argv[])
174 opt::program_name = argv[0];
175 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
179 while ((c = getopt(*argc, argv, "a:bc:C:ehi:I:l:L:N:T:vV")) != -1) {
182 opt::loba_algo = optarg;
183 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
184 "load balancing algorithm",
189 opt::bookkeeping = true;
192 opt::exit_on_close = true;
195 opt::help_requested++;
198 opt::comp_cost = cost_func(optarg);
201 opt::comm_cost = cost_func(optarg);
204 std::istringstream(optarg) >> opt::comp_maxiter;
207 std::istringstream(optarg) >> opt::lb_maxiter;
208 ERROR0("option -I not implemented yet");
212 std::istringstream(optarg) >> opt::log_rate;
215 std::istringstream(optarg) >> opt::auto_depl::load;
218 std::istringstream(optarg) >> opt::auto_depl::nhosts;
221 opt::auto_depl::topology = optarg;
222 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
223 opt::auto_depl::topology)
227 // nothing to do: this option is checked at the very
228 // beginning of main()
231 opt::version_requested = true;
234 ERROR1("invalid option -- '%c'", optopt);
240 if (opt::version_requested || opt::help_requested)
243 if (optind < *argc) {
244 opt::platform_file = argv[optind++];
246 ERROR0("missing parameter -- <plaform_file>");
249 if (optind < *argc) {
250 opt::deployment_file = argv[optind++];
252 opt::auto_depl::enabled = opt::deployment_file.empty();
254 while (optind < *argc) {
255 ERROR1("unused parameter -- \"%s\"", argv[optind++]);
266 #define DESCR(description, format, value) \
267 INFO2("| %s: " format, h.descr(description), value)
269 INFO0(",----[ Simulation parameters ]");
270 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
271 DESCR("platform file", "\"%s\"", platform_file.c_str());
272 if (auto_depl::enabled) {
273 INFO0("| automatic deployment enabled");
274 DESCR("- topology", "%s", auto_depl::topology.c_str());
275 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
277 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
280 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
282 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
283 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
284 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
285 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
286 DESCR("maximum number of comp. iterations", "%s",
287 h.val_or_string(comp_maxiter, "infinity"));
288 DESCR("maximum number of lb. iterations", "%s",
289 h.val_or_string(lb_maxiter, "infinity"));
290 DESCR("exit on close", "%s", h.on_off(exit_on_close));
299 #define o(opt) " " << std::setw(14) \
300 << std::left << (opt) << std::right << " "
302 #define so(subopt) std::setw(18) << (subopt) << " : "
304 #define so_list(name) do { \
305 name ## _type::iterator it; \
306 for (it = name.begin() ; it != name.end() ; ++it) \
307 std::clog << so(name.get_name(it)) \
308 << name.get_descr(it) << "\n"; \
312 std::clog << "Usage: " << opt::program_name
313 << " [options] <platform_file> [<deployment_file>]\n";
315 std::clog << "\nGlobal options\n";
317 << "print help and exit (use -hh or -hhh for extended help)\n";
318 if (opt::help_requested < 1)
321 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
322 std::clog << o("-V") << "print version and exit\n";
324 std::clog << "\nSimulation parameters\n";
325 std::clog << o("-l value")
326 << "print current load every n-th iterations, 0 to disable"
327 << " (" << opt::log_rate << ")\n";
329 << "verbose: do not override the default logging parameters\n";
331 std::clog << "\nAutomatic deployment options\n";
332 std::clog << o("-T name")
333 << "enable automatic deployment with selected topology"
334 << " (" << opt::auto_depl::topology << ")\n";
335 if (opt::help_requested > 1)
336 so_list(opt::topologies);
337 std::clog << o("-L value")
338 << "total load with auto deployment, 0 for number of hosts"
339 << " (" << opt::auto_depl::load << ")\n";
340 std::clog << o("-N value")
341 << "number of hosts to use with auto deployment,"
342 << " 0 for max. (" << opt::auto_depl::nhosts << ")\n";
344 std::clog << "\nLoad balancing algorithm\n";
345 std::clog << o("-a name") << "load balancing algorithm"
346 << " (" << opt::loba_algo << ")\n";
347 if (opt::help_requested > 1)
348 so_list(opt::loba_algorithms);
349 std::clog << o("-b") << "enable bookkeeping (\"virtual load\")\n";
351 std::clog << "\nApplication parameters\n";
352 std::clog << o("-c [fn,...]f0")
353 << "polynomial factors for computation cost"
354 << " (" << opt::comp_cost.to_string() << ")\n";
355 std::clog << o("-C [fn,...]f0")
356 << "polynomial factors for communication cost"
357 << " (" << opt::comm_cost.to_string() << ")\n";
359 std::clog << "\nParameters for the end of the simulation\n";
360 std::clog << o("-i value")
361 << "maximum number of comp. iterations, 0 for infinity"
362 << " (" << opt::comp_maxiter << ")\n";
363 std::clog << o("-I value")
364 << "maximum number of lb. iterations, 0 for infinity"
365 << " (" << opt::lb_maxiter << ")\n";
366 std::clog << o("-e") << "exit on reception of \"close\" message\n";
368 if (opt::help_requested < 3)
371 std::clog << "\nLogging support\n"
372 << " See SimGrid documentation on:\n"
373 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
374 << " Existing categories are:\n"
375 << " simu : root of following categories\n"
376 << " main : messages from global infrastructure\n"
377 << " depl : messages from auto deployment (inherited from main)\n"
378 << " comm : messages from asynchronous pipes\n"
379 << " proc : messages from base process class\n"
380 << " loba : messages from load-balancer\n";
382 // std::clog << "\nMiscellaneous low-level parameters\n";