]> AND Private Git Repository - loba.git/blob - options.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Add algorithm bulk.
[loba.git] / options.cpp
1 #include <ctime>
2 #include <iomanip>
3 #include <iostream>
4 #include <sstream>
5 #include <stack>
6 #include <unistd.h>             // getopt
7 #include <xbt/log.h>
8
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
10
11 #include "deployment.h"
12 #include "process.h"
13 #include "loba_besteffort.h"
14 #include "loba_bulk.h"
15 #include "loba_fairstrategy.h"
16 #include "loba_makhoul.h"
17 #include "loba_makhoul2.h"
18 #include "loba_simple.h"
19 #include "misc.h"
20
21 #include "options.h"
22
23 #define DATA_DESCR_WIDTH 42
24
25 namespace opt {
26
27     // Constants
28
29     // A sum of loads if considered null if it is less than
30     // load_ratio_threshold percent of the sum of loads at init.
31     const double load_ratio_threshold = 1e-4;
32
33     // Global options
34     std::string program_name;
35     int help_requested = 0;
36     bool version_requested = false;
37     int option_x = 0;           // hidden option, for debugging purpose
38
39     // Simulation parameters
40     int log_rate = 1;
41     bool exit_request = false;
42
43     // Platform and deployment
44     std::string platform_file;
45     std::string deployment_file;
46
47     // Automatic deployment
48     namespace auto_depl {
49         bool        enabled = false;
50         std::string topology("clique");
51         unsigned    nhosts = 0;
52         double      load = 0.0;
53         bool        random_distribution = false;
54         unsigned long random_seed = 0;
55     }
56
57     // Load balancing algorithm
58     std::string loba_algo("simple");
59     bool bookkeeping = false;
60     double min_lb_iter_duration = 1.0;          // fixme: find better defaults
61     double min_transfer_amount = 0.0;
62     double max_transfer_amount = 0.0;
63
64     // Application parameters
65     cost_func comp_cost("1e9, 0");              // fixme: find better defaults
66     cost_func comm_cost("1e6, 0");              // fixme: find better defaults
67     double min_comp_iter_duration = 1.0;        // fixme: find better defaults
68     unsigned comp_iter_delay = 0;               // fixme: find better defaults
69     double comp_time_delay = 0.0;               // fixme: find better defaults
70
71     // Parameters for the end of the simulation
72     unsigned lb_maxiter = 0;
73     unsigned comp_maxiter = 0;
74     double time_limit = 0;
75     bool exit_on_close = true;
76
77     // Named parameters lists
78     loba_algorithms_type loba_algorithms;
79     loba_algorithms_type::loba_algorithms_type()
80     {
81         NOL_INSERT("besteffort", "balance with best effort strategy",
82                    loba_besteffort);
83         NOL_INSERT("bulk", "describe your algorithm here...",
84                    loba_bulk);
85         NOL_INSERT("fairstrategy", "balance with fair strategy",
86                    loba_fairstrategy);
87         NOL_INSERT("makhoul", "balance with Makhoul's PhD algorithm",
88                    loba_makhoul);
89         NOL_INSERT("makhoul2", "balance with Makhoul's source code",
90                    loba_makhoul2);
91         NOL_INSERT("none", "no load-balancing (for testing only)",
92                    process);
93         NOL_INSERT("simple", "balance with least loaded neighbor",
94                    loba_simple);
95     }
96
97     topologies_type topologies;
98     topologies_type::topologies_type()
99     {
100         NOL_INSERT("btree", "binary tree topology, initial load at root",
101                    deployment_btree);
102         NOL_INSERT("clique", "all connected topology", deployment_clique);
103         NOL_INSERT("hcube", "hypercube topology", deployment_hcube);
104         NOL_INSERT("line", "line topology, initial load at one end",
105                    deployment_line);
106         NOL_INSERT("ring", "ring topology", deployment_ring);
107         NOL_INSERT("star", "star topology, initial load at center",
108                    deployment_star);
109         NOL_INSERT("torus", "torus topology", deployment_torus);
110     }
111
112 } // namespace opt
113
114 namespace {
115
116     // local helper class
117     class opt_helper {
118     public:
119         template <typename T>
120         static bool parse_arg(char opt, const char *arg, T& val);
121         static const char* on_off(bool b);
122         const char* descr(const char* str);
123         template <typename T>
124         const char* val_or_string(const T& val, const char* str,
125                                   const T& deflt = 0);
126         template <typename T>
127         static bool nol_find_prefix(const T& nol, const char* descr,
128                                     std::string& name);
129
130     private:
131         std::string descr_str;
132         std::string val_or_string_str;
133     };
134
135 } // namespace
136
137 template <typename T>
138 bool opt_helper::parse_arg(char opt, const char *arg, T& val)
139 {
140     std::istringstream str(arg);
141     bool result = (str >> val) && str.eof();
142     if (!result)
143         XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
144     return result;
145 }
146
147 const char* opt_helper::on_off(bool b)
148 {
149     return b ? "on" : "off";
150 }
151
152 const char* opt_helper::descr(const char* str)
153 {
154     std::string& res = descr_str;
155     res = str;
156     res.resize(DATA_DESCR_WIDTH, '.');
157     return res.c_str();
158 }
159
160 template <typename T>
161 const char* opt_helper::val_or_string(const T& val, const char* str,
162                                       const T& deflt)
163 {
164     std::string& res = val_or_string_str;
165     if (val != deflt) {
166         std::ostringstream oss;
167         oss << val;
168         res = oss.str();
169     } else {
170         res = str;
171     }
172     return res.c_str();
173 }
174
175 template <typename T>
176 bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
177                                  std::string& name)
178 {
179     bool result = nol.exists(name);
180     if (!result) {
181         std::stack<std::string> candidates;
182         for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
183             const std::string& fullname = nol.get_name(it);
184             if (fullname.compare(0, name.length(), name) == 0)
185                 candidates.push(fullname);
186         }
187         switch (candidates.size()) {
188         case 0:
189             XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
190             break;
191         case 1:
192             name = candidates.top();
193             candidates.pop();
194             result = true;
195             XBT_DEBUG("infered %s -- %s", descr, name.c_str());
196             break;
197         default:
198             XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
199             while (!candidates.empty()) {
200                 XBT_ERROR("  candidates are -- %s", candidates.top().c_str());
201                 candidates.pop();
202             }
203             break;
204         }
205     }
206     return result;
207 }
208
209 bool opt::parse_args(int* argc, char* argv[])
210 {
211     bool result = true;
212
213     opt::program_name = argv[0];
214     opt::program_name.erase(0, 1 + opt::program_name.find_last_of('/'));
215
216 #define PARSE_ARG(x) result = opt_helper::parse_arg(c, optarg, (x)) && result
217
218     int c;
219     opterr = 0;
220     while ((c = getopt(*argc, argv,
221                        "a:bc:C:d:D:ehi:I:l:L:m:M:N:r:Rs:S:t:T:vVx:")) != -1) {
222         switch (c) {
223         case 'a':
224             opt::loba_algo = optarg;
225             result = opt_helper::nol_find_prefix(opt::loba_algorithms,
226                                                  "load balancing algorithm",
227                                                  opt::loba_algo)
228                 && result;
229             break;
230         case 'b':
231             opt::bookkeeping = !opt::bookkeeping;
232             break;
233         case 'e':
234             opt::exit_on_close = !opt::exit_on_close;
235             break;
236         case 'h':
237             opt::help_requested++;
238             break;
239         case 'c':
240             try {
241                 opt::comp_cost = cost_func(optarg);
242             } catch (...) {
243                 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
244                           c, optarg);
245                 result = false;
246             }
247             break;
248         case 'C':
249             try {
250                 opt::comm_cost = cost_func(optarg);
251             } catch (...) {
252                 XBT_ERROR("invalid argument for option '-%c' -- \"%s\"",
253                           c, optarg);
254                 result = false;
255             }
256             break;
257         case 'd':
258             PARSE_ARG(opt::comp_iter_delay);
259             break;
260         case 'D':
261             PARSE_ARG(opt::comp_time_delay);
262             break;
263         case 'i':
264             PARSE_ARG(opt::lb_maxiter);
265             break;
266         case 'I':
267             PARSE_ARG(opt::comp_maxiter);
268             break;
269         case 'l':
270             PARSE_ARG(opt::log_rate);
271             break;
272         case 'L':
273             PARSE_ARG(opt::auto_depl::load);
274             break;
275         case 'm':
276             PARSE_ARG(opt::min_transfer_amount);
277             break;
278         case 'M':
279             PARSE_ARG(opt::max_transfer_amount);
280             break;
281         case 'N':
282             PARSE_ARG(opt::auto_depl::nhosts);
283             break;
284         case 'r':
285             PARSE_ARG(opt::auto_depl::random_seed);
286             break;
287         case 'R':
288             opt::auto_depl::random_distribution =
289                 !opt::auto_depl::random_distribution;
290             break;
291         case 's':
292             PARSE_ARG(opt::min_lb_iter_duration);
293             break;
294         case 'S':
295             PARSE_ARG(opt::min_comp_iter_duration);
296             break;
297         case 't':
298             PARSE_ARG(opt::time_limit);
299             break;
300         case 'T':
301             opt::auto_depl::topology = optarg;
302             result = opt_helper::nol_find_prefix(opt::topologies, "topology",
303                                                  opt::auto_depl::topology)
304                 && result;
305             break;
306         case 'v':
307             // nothing to do: this option is checked at the very
308             // beginning of main()
309             break;
310         case 'V':
311             opt::version_requested = true;
312             break;
313         case 'x':
314             PARSE_ARG(opt::option_x);
315             XBT_WARN("option_x set to %d", opt::option_x);
316             break;
317         case '?':
318             XBT_ERROR("invalid option -- '%c'", optopt);
319             result = false;
320             break;
321         }
322     }
323
324 #undef PARSE_ARG
325
326     if (opt::version_requested || opt::help_requested)
327         return 1;
328
329     if (optind < *argc) {
330         opt::platform_file = argv[optind++];
331     } else {
332         XBT_ERROR("missing parameter -- <plaform_file>");
333         result = false;
334     }
335     if (optind < *argc) {
336         opt::deployment_file = argv[optind++];
337     }
338     opt::auto_depl::enabled = opt::deployment_file.empty();
339
340     while (optind < *argc) {
341         XBT_ERROR("unused parameter -- \"%s\"", argv[optind++]);
342         result = false;
343     }
344
345     if (opt::max_transfer_amount &&
346         opt::max_transfer_amount < opt::min_transfer_amount) {
347         XBT_ERROR("max. data transfer amount < min. data transfer amount");
348         result = false;
349     }
350
351     if (!opt::auto_depl::random_seed)
352         opt::auto_depl::random_seed = time(NULL);
353
354     return result;
355 }
356
357 void opt::print()
358 {
359     opt_helper h;
360
361 #define DESCR(description, format, value) \
362     XBT_INFO("| %s: " format, h.descr(description), value)
363
364     XBT_INFO(",----[ Simulation parameters ]");
365     DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled"));
366     DESCR("platform file", "\"%s\"", platform_file.c_str());
367     if (auto_depl::enabled) {
368         XBT_INFO("| automatic deployment enabled");
369         DESCR("- topology", "%s", auto_depl::topology.c_str());
370         DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts,
371                                                          "auto"));
372         DESCR("- initial load", "%s", h.val_or_string(auto_depl::load,
373                                                       "auto"));
374         DESCR("- random initial load distribution", "%s",
375               h.on_off(auto_depl::random_distribution));
376         DESCR("- random seed", "%s",
377               h.val_or_string(auto_depl::random_seed, "time based"));
378     } else {
379         DESCR("deployment file", "\"%s\"", deployment_file.c_str());
380     }
381     DESCR("load balancing algorithm", "%s", loba_algo.c_str());
382     DESCR("bookkeeping", "%s", h.on_off(bookkeeping));
383     DESCR("minimum duration between lb. iterations", "%g",
384           min_lb_iter_duration);
385     DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str());
386     DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str());
387     DESCR("minimum data transfer amount", "%g", min_transfer_amount);
388     DESCR("maximum data transfer amount", "%s",
389           h.val_or_string(max_transfer_amount, "no limit"));
390     DESCR("minimum duration between comp. iterations", "%g",
391           min_comp_iter_duration);
392     DESCR("computations start after lb. iter", "%u", comp_iter_delay);
393     DESCR("computations start after time", "%g", comp_time_delay);
394     DESCR("maximum number of lb. iterations", "%s",
395           h.val_or_string(lb_maxiter, "no limit"));
396     DESCR("maximum number of comp. iterations", "%s",
397           h.val_or_string(comp_maxiter, "no limit"));
398     DESCR("time limit", "%s", h.val_or_string(time_limit, "no limit"));
399     DESCR("exit on close", "%s", h.on_off(exit_on_close));
400     XBT_INFO("`----");
401
402 #undef DESCR
403 }
404
405 void opt::usage()
406 {
407     // option(...)
408 #define o(opt) "    " << std::setw(14) \
409                       << std::left << (opt) << std::right << " "
410     // sub-option(...)
411 #define so(subopt) std::setw(18) << (subopt) << " : "
412     // sub-option list
413 #define so_list(name) do {                                      \
414         name ## _type::iterator it;                             \
415         for (it = name.begin() ; it != name.end() ; ++it)       \
416             std::clog << so(name.get_name(it))                  \
417                       << name.get_descr(it) << "\n";            \
418     } while (0)
419
420
421     std::clog << "Usage: " << opt::program_name
422               << " [options] <platform_file> [<deployment_file>]\n";
423
424     std::clog << "\nGlobal options\n";
425     std::clog << o("-h")
426               << "print help and exit (use -hh or -hhh for extended help)\n";
427     if (opt::help_requested < 1)
428         return;
429
430     std::clog << o("--help") << "print help from SimGrid framework and exit\n";
431     std::clog << o("-V") << "print version and exit\n";
432
433     std::clog << "\nSimulation parameters\n";
434     std::clog << o("-l value")
435               << "print current load every n lb iterations, 0 to disable"
436               << " [" << opt::log_rate << "]\n";
437     std::clog << o("-v")
438               << "verbose: do not override the default logging parameters\n";
439
440     std::clog << "\nAutomatic deployment options\n";
441     std::clog << o("-T name")
442               << "enable automatic deployment with selected topology"
443               << " [" << opt::auto_depl::topology << "]\n";
444     if (opt::help_requested > 1)
445         so_list(opt::topologies);
446     std::clog << o("-L value")
447               << "total load with auto deployment, 0 for number of hosts"
448               << " [" << opt::auto_depl::load << "]\n";
449     std::clog << o("-N value")
450               << "number of hosts to use with auto deployment, 0 for max."
451               << " [" << opt::auto_depl::nhosts << "]\n";
452     std::clog << o("-R")
453               << "toggle random initial load distribution"
454               << " [" << opt_helper::on_off(opt::auto_depl::random_distribution)
455               << "]\n";
456     std::clog << o("-r value")
457               << "random seed, 0 for using it on time()"
458               << " [" << opt::auto_depl::random_seed << "]\n";
459
460     std::clog << "\nLoad balancing algorithm\n";
461     std::clog << o("-a name") << "load balancing algorithm"
462               << " [" << opt::loba_algo << "]\n";
463     if (opt::help_requested > 1)
464         so_list(opt::loba_algorithms);
465     std::clog << o("-b") << "toggle bookkeeping (\"virtual load\")"
466               << " [" << opt_helper::on_off(opt::bookkeeping) << "]\n";
467     std::clog << o("-s value")
468               << "minimum duration between lb. iterations"
469               << " [" << opt::min_lb_iter_duration << "]\n";
470
471     std::clog << "\nApplication parameters\n";
472     std::clog << o("-c [aN,...]a0")
473               << "polynomial factors for computation cost"
474               << " [" << opt::comp_cost.to_string() << "]\n";
475     std::clog << o("-C [aN,...]a0")
476               << "polynomial factors for communication cost"
477               << " [" << opt::comm_cost.to_string() << "]\n";
478     std::clog << o("-m value")
479               << "minimum data transfer amount"
480               << " [" << opt::min_transfer_amount << "]\n";
481     std::clog << o("-M value")
482               << "maximum data transfer amount, 0 for no limit"
483               << " [" << opt::max_transfer_amount << "]\n";
484     std::clog << o("-S value")
485               << "minimum duration between comp. iterations"
486               << " [" << opt::min_comp_iter_duration << "]\n";
487     std::clog << o("-d value")
488               << "start computations after given number of lb iterations"
489               << " [" << opt::comp_iter_delay << "]\n";
490     std::clog << o("-D value")
491               << "start computations after given time"
492               << " [" << opt::comp_time_delay << "]\n";
493
494     std::clog << "\nParameters for the end of the simulation\n";
495     std::clog << o("-i value")
496               << "maximum number of lb. iterations, 0 for no limit"
497               << " [" << opt::lb_maxiter << "]\n";
498     std::clog << o("-I value")
499               << "maximum number of comp. iterations, 0 for no limit"
500               << " [" << opt::comp_maxiter << "]\n";
501     std::clog << o("-t value")
502               << "time limit (simulated time), 0 for no limit"
503               << " [" << opt::time_limit << "]\n";
504     std::clog << o("-e") << "toggle exit on reception of \"close\" message"
505               << " [" << opt_helper::on_off(opt::exit_on_close) << "]\n";
506
507     if (opt::help_requested < 3)
508         return;
509
510     std::clog << "\nLogging support\n"
511               << "    See SimGrid documentation on:\n"
512               << "        http://simgrid.gforge.inria.fr/doc/group__XBT__log.html#log_user\n"
513               << "    Existing categories are:\n"
514               << "        simu : root of following categories\n"
515               << "        main : messages from global infrastructure\n"
516               << "        depl : messages from auto deployment (inherited from main)\n"
517               << "        comm : messages from asynchronous pipes\n"
518               << "        proc : messages from base process class\n"
519               << "        loba : messages from load-balancer\n"
520               << "        thrd : messages from thread wrapper class\n";
521
522     // std::clog << "\nMiscellaneous low-level parameters\n";
523
524 #undef so_list
525 #undef so
526 #undef o
527 }
528
529 // Local variables:
530 // mode: c++
531 // End: