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");
53 double min_iter_duration = 1.0;
55 // Parameters for the end of the simulation
56 // fixme: find better defaults
57 unsigned comp_maxiter = 10;
58 unsigned lb_maxiter = comp_maxiter;
59 bool exit_on_close = false;
61 // Named parameters lists
62 loba_algorithms_type loba_algorithms;
63 loba_algorithms_type::loba_algorithms_type()
65 NOL_INSERT("fairstrategy", "balance with fair strategy", loba_fairstrategy);
66 NOL_INSERT("none", "no load-balancing (for testing only)", process);
67 NOL_INSERT("simple", "balance with least loaded neighbor", loba_simple);
70 topologies_type topologies;
71 topologies_type::topologies_type()
73 NOL_INSERT("btree", "binary tree topology, initial load at root",
75 NOL_INSERT("clique", "all connected topology", deployment_clique);
76 NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
77 NOL_INSERT("line", "line topology, initial load at one end",
79 NOL_INSERT("ring", "ring topology", deployment_ring);
80 NOL_INSERT("star", "star topology, initial load at center",
82 NOL_INSERT("torus", "torus topology", deployment_torus);
92 static const char* on_off(bool b);
93 const char* descr(const char* str);
95 const char* val_or_string(const T& val, const char* str,
98 static bool nol_find_prefix(const T& nol, const char* descr,
102 std::string descr_str;
103 std::string val_or_string_str;
108 const char* opt_helper::on_off(bool b)
110 return b ? "on" : "off";
113 const char* opt_helper::descr(const char* str)
115 const int descr_width = 40;
116 std::string& res = descr_str;
118 res.resize(descr_width, '.');
122 template <typename T>
123 const char* opt_helper::val_or_string(const T& val, const char* str,
126 std::string& res = val_or_string_str;
128 std::ostringstream oss;
137 template <typename T>
138 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
141 bool result = nol.exists(name);
143 std::stack<std::string> candidates;
144 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
145 const std::string& fullname = nol.get_name(it);
146 if (fullname.compare(0, name.length(), name) == 0)
147 candidates.push(fullname);
149 switch (candidates.size()) {
151 ERROR2("unknownw %s -- %s", descr, name.c_str());
154 name = candidates.top();
157 DEBUG2("infered %s -- %s", descr, name.c_str());
160 ERROR2("ambiguous %s -- %s", descr, name.c_str());
161 while (!candidates.empty()) {
162 ERROR1(" candidates are -- %s", candidates.top().c_str());
171 bool opt::parse_args(int* argc, char* argv[])
175 opt::program_name = argv[0];
176 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
180 while ((c = getopt(*argc, argv, "a:bc:C:ehi:I:l:L:N:s:T:vV")) != -1) {
183 opt::loba_algo = optarg;
184 result = opt_helper::nol_find_prefix(opt::loba_algorithms,
185 "load balancing algorithm",
190 opt::bookkeeping = true;
193 opt::exit_on_close = true;
196 opt::help_requested++;
199 opt::comp_cost = cost_func(optarg);
202 opt::comm_cost = cost_func(optarg);
205 std::istringstream(optarg) >> opt::comp_maxiter;
208 std::istringstream(optarg) >> opt::lb_maxiter;
209 ERROR0("option -I not implemented yet");
213 std::istringstream(optarg) >> opt::log_rate;
216 std::istringstream(optarg) >> opt::auto_depl::load;
219 std::istringstream(optarg) >> opt::auto_depl::nhosts;
222 std::istringstream(optarg) >> opt::min_iter_duration;
225 opt::auto_depl::topology = optarg;
226 result = opt_helper::nol_find_prefix(opt::topologies, "topology",
227 opt::auto_depl::topology)
231 // nothing to do: this option is checked at the very
232 // beginning of main()
235 opt::version_requested = true;
238 ERROR1("invalid option -- '%c'", optopt);
244 if (opt::version_requested || opt::help_requested)
247 if (optind < *argc) {
248 opt::platform_file = argv[optind++];
250 ERROR0("missing parameter -- <plaform_file>");
253 if (optind < *argc) {
254 opt::deployment_file = argv[optind++];
256 opt::auto_depl::enabled = opt::deployment_file.empty();
258 while (optind < *argc) {
259 ERROR1("unused parameter -- \"%s\"", argv[optind++]);
270 #define DESCR(description, format, value) \
271 INFO2("| %s: " format, h.descr(description), value)
273 INFO0(",----[ Simulation parameters ]");
274 DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
275 DESCR("platform file", "\"%s\"", platform_file.c_str());
276 if (auto_depl::enabled) {
277 INFO0("| automatic deployment enabled");
278 DESCR("- topology", "%s", auto_depl::topology.c_str());
279 DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
281 DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
284 DESCR("deployment file", "\"%s\"", deployment_file.c_str());
286 DESCR("load balancing algorithm", "%s", loba_algo.c_str());
287 DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
288 DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
289 DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
290 DESCR("minimum duration between iterations", "%g", min_iter_duration);
291 DESCR("maximum number of comp. iterations", "%s",
292 h.val_or_string(comp_maxiter, "infinity"));
293 DESCR("maximum number of lb. iterations", "%s",
294 h.val_or_string(lb_maxiter, "infinity"));
295 DESCR("exit on close", "%s", h.on_off(exit_on_close));
304 #define o(opt) " " << std::setw(14) \
305 << std::left << (opt) << std::right << " "
307 #define so(subopt) std::setw(18) << (subopt) << " : "
309 #define so_list(name) do { \
310 name ## _type::iterator it; \
311 for (it = name.begin() ; it != name.end() ; ++it) \
312 std::clog << so(name.get_name(it)) \
313 << name.get_descr(it) << "\n"; \
317 std::clog << "Usage: " << opt::program_name
318 << " [options] <platform_file> [<deployment_file>]\n";
320 std::clog << "\nGlobal options\n";
322 << "print help and exit (use -hh or -hhh for extended help)\n";
323 if (opt::help_requested < 1)
326 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
327 std::clog << o("-V") << "print version and exit\n";
329 std::clog << "\nSimulation parameters\n";
330 std::clog << o("-l value")
331 << "print current load every n-th iterations, 0 to disable"
332 << " (" << opt::log_rate << ")\n";
334 << "verbose: do not override the default logging parameters\n";
336 std::clog << "\nAutomatic deployment options\n";
337 std::clog << o("-T name")
338 << "enable automatic deployment with selected topology"
339 << " (" << opt::auto_depl::topology << ")\n";
340 if (opt::help_requested > 1)
341 so_list(opt::topologies);
342 std::clog << o("-L value")
343 << "total load with auto deployment, 0 for number of hosts"
344 << " (" << opt::auto_depl::load << ")\n";
345 std::clog << o("-N value")
346 << "number of hosts to use with auto deployment,"
347 << " 0 for max. (" << opt::auto_depl::nhosts << ")\n";
349 std::clog << "\nLoad balancing algorithm\n";
350 std::clog << o("-a name") << "load balancing algorithm"
351 << " (" << opt::loba_algo << ")\n";
352 if (opt::help_requested > 1)
353 so_list(opt::loba_algorithms);
354 std::clog << o("-b") << "enable bookkeeping (\"virtual load\")\n";
356 std::clog << "\nApplication parameters\n";
357 std::clog << o("-c [fn,...]f0")
358 << "polynomial factors for computation cost"
359 << " (" << opt::comp_cost.to_string() << ")\n";
360 std::clog << o("-C [fn,...]f0")
361 << "polynomial factors for communication cost"
362 << " (" << opt::comm_cost.to_string() << ")\n";
363 std::clog << o("-s value")
364 << "minimum duration between iterations"
365 << " (" << opt::min_iter_duration << ")\n";
367 std::clog << "\nParameters for the end of the simulation\n";
368 std::clog << o("-i value")
369 << "maximum number of comp. iterations, 0 for infinity"
370 << " (" << opt::comp_maxiter << ")\n";
371 std::clog << o("-I value")
372 << "maximum number of lb. iterations, 0 for infinity"
373 << " (" << opt::lb_maxiter << ")\n";
374 std::clog << o("-e") << "exit on reception of \"close\" message\n";
376 if (opt::help_requested < 3)
379 std::clog << "\nLogging support\n"
380 << " See SimGrid documentation on:\n"
381 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
382 << " Existing categories are:\n"
383 << " simu : root of following categories\n"
384 << " main : messages from global infrastructure\n"
385 << " depl : messages from auto deployment (inherited from main)\n"
386 << " comm : messages from asynchronous pipes\n"
387 << " proc : messages from base process class\n"
388 << " loba : messages from load-balancer\n";
390 // std::clog << "\nMiscellaneous low-level parameters\n";