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 const char* on_off(bool b)
85 return b ? "on" : "off";
89 const char* val_or_string(const T& val, const char* str, const T& deflt = 0)
91 static std::string res;
93 std::ostringstream oss;
102 // helper function for opt::parse_args()
104 template <typename T>
105 bool nol_find_prefix(const T& nol, const char* descr, std::string& name)
107 bool result = nol.exists(name);
109 std::stack<std::string> candidates;
110 for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
111 const std::string& fullname = nol.get_name(it);
112 if (fullname.compare(0, name.length(), name) == 0)
113 candidates.push(fullname);
115 switch (candidates.size()) {
117 ERROR2("unknownw %s -- %s", descr, name.c_str());
120 name = candidates.top();
123 DEBUG2("infered %s -- %s", descr, name.c_str());
126 ERROR2("ambiguous %s -- %s", descr, name.c_str());
127 while (!candidates.empty()) {
128 ERROR1(" candidates are -- %s", candidates.top().c_str());
139 bool opt::parse_args(int* argc, char* argv[])
143 opt::program_name = argv[0];
144 opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
148 while ((c = getopt(*argc, argv, "a:bc:C:ehi:l:L:N:T:vV")) != -1) {
151 opt::loba_algo = optarg;
152 result = nol_find_prefix(opt::loba_algorithms,
153 "load balancing algorithm",
157 opt::bookkeeping = true;
160 opt::exit_on_close = true;
163 opt::help_requested++;
166 opt::comp_cost = cost_func(optarg);
169 opt::comm_cost = cost_func(optarg);
172 std::istringstream(optarg) >> opt::maxiter;
175 std::istringstream(optarg) >> opt::log_rate;
178 std::istringstream(optarg) >> opt::auto_depl::load;
181 std::istringstream(optarg) >> opt::auto_depl::nhosts;
184 opt::auto_depl::topology = optarg;
185 result = nol_find_prefix(opt::topologies, "topology",
186 opt::auto_depl::topology);
189 // nothing to do: this option is checked at the very
190 // beginning of main()
193 opt::version_requested = true;
196 ERROR1("invalid option -- '%c'", optopt);
202 if (opt::version_requested || opt::help_requested)
205 if (optind < *argc) {
206 opt::platform_file = argv[optind++];
208 ERROR0("missing parameter -- <plaform_file>");
211 if (optind < *argc) {
212 opt::deployment_file = argv[optind++];
214 opt::auto_depl::enabled = opt::deployment_file.empty();
216 while (optind < *argc) {
217 ERROR1("unused parameter -- \"%s\"", argv[optind++]);
226 INFO0(",----[ Simulation parameters ]");
227 INFO1("| log rate.....................: %s",
228 val_or_string(opt::log_rate, "disabled"));
229 INFO1("| platform file................: \"%s\"", opt::platform_file.c_str());
230 if (opt::auto_depl::enabled) {
231 INFO0("| automatic deployment enabled");
232 INFO1("| - topology...................: %s", opt::auto_depl::topology.c_str());
233 INFO1("| - number of hosts............: %s",
234 val_or_string(opt::auto_depl::nhosts, "auto"));
235 INFO1("| - initial load...............: %s",
236 val_or_string(opt::auto_depl::load, "auto"));
238 INFO1("| deployment file..............: \"%s\"", opt::deployment_file.c_str());
240 INFO1("| load balancing algorithm.....: %s", opt::loba_algo.c_str());
241 INFO1("| bookkeeping..................: %s", on_off(opt::bookkeeping));
242 INFO1("| computation cost factors.....: [%s]", opt::comp_cost.to_string().c_str());
243 INFO1("| communication cost factors...: [%s]", opt::comm_cost.to_string().c_str());
244 INFO1("| maximum number of iterations.: %s",
245 val_or_string(opt::maxiter, "infinity"));
246 INFO1("| exit on close................: %s", on_off(opt::exit_on_close));
253 #define o(opt) " " << std::setw(14) \
254 << std::left << (opt) << std::right << " "
256 #define so(subopt) std::setw(18) << (subopt) << " : "
258 #define so_list(name) do { \
259 name ## _type::iterator it; \
260 for (it = name.begin() ; it != name.end() ; ++it) \
261 std::clog << so(name.get_name(it)) \
262 << name.get_descr(it) << "\n"; \
266 std::clog << "Usage: " << opt::program_name
267 << " [options] <platform_file> [<deployment_file>]\n";
269 std::clog << "\nGlobal options\n";
271 << "print help and exit (use -hh or -hhh for extended help)\n";
272 if (opt::help_requested < 1)
275 std::clog << o("--help") << "print help from SimGrid framework and exit\n";
276 std::clog << o("-V") << "print version and exit\n";
278 std::clog << "\nSimulation parameters\n";
279 std::clog << o("-l value")
280 << "print current load every n-th iterations, 0 to disable"
281 << " (" << opt::log_rate << ")\n";
283 << "verbose: do not override the default logging parameters\n";
285 std::clog << "\nAutomatic deployment options\n";
286 std::clog << o("-T name")
287 << "enable automatic deployment with selected topology"
288 << " (" << opt::auto_depl::topology << ")\n";
289 if (opt::help_requested > 1)
290 so_list(opt::topologies);
291 std::clog << o("-L value")
292 << "total load with auto deployment, 0 for number of hosts"
293 << " (" << opt::auto_depl::load << ")\n";
294 std::clog << o("-N value")
295 << "number of hosts to use with auto deployment,"
296 << " 0 for max. (" << opt::auto_depl::nhosts << ")\n";
298 std::clog << "\nLoad balancing algorithm\n";
299 std::clog << o("-a name") << "load balancing algorithm"
300 << " (" << opt::loba_algo << ")\n";
301 if (opt::help_requested > 1)
302 so_list(opt::loba_algorithms);
303 std::clog << o("-b") << "enable bookkeeping\n";
305 std::clog << "\nApplication parameters\n";
306 std::clog << o("-c [fn,...]f0")
307 << "polynomial factors for computation cost"
308 << " (" << opt::comp_cost.to_string() << ")\n";
309 std::clog << o("-C [fn,...]f0")
310 << "polynomial factors for communication cost"
311 << " (" << opt::comm_cost.to_string() << ")\n";
312 std::clog << o("-e") << "exit on reception of \"close\" message\n";
313 std::clog << o("-i value")
314 << "maximum number of iterations, 0 for infinity"
315 << " (" << opt::maxiter << ")\n";
317 if (opt::help_requested < 3)
320 std::clog << "\nLogging support\n"
321 << " See SimGrid documentation on:\n"
322 << " http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
323 << " Existing categories are:\n"
324 << " simu : root of following categories\n"
325 << " main : messages from global infrastructure\n"
326 << " depl : messages from auto deployment (inherited from main)\n"
327 << " comm : messages from asynchronous pipes\n"
328 << " proc : messages from base process class\n"
329 << " loba : messages from load-balancer\n";