Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement thread factory with std::thread, and cleanups
[simgrid.git] / src / simgrid / sg_config.cpp
1 /* Copyright (c) 2009-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* sg_config: configuration infrastructure for the simulation world         */
7
8 #include "simgrid/sg_config.hpp"
9 #include "simgrid/instr.h"
10 #include "src/instr/instr_private.hpp"
11 #include "src/internal_config.h"
12 #include "src/kernel/lmm/maxmin.hpp"
13 #include "src/mc/mc_config.hpp"
14 #include "src/mc/mc_replay.hpp"
15 #include "src/surf/surf_interface.hpp"
16 #include "surf/surf.hpp"
17 #include "xbt/config.hpp"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_config, surf, "About the configuration of SimGrid");
20
21 static simgrid::config::Flag<bool> cfg_continue_after_help
22   {"help-nostop", "Do not stop the execution when --help is found", false};
23
24 /** @brief Allow other libraries to react to the --help flag, too
25  *
26  * When finding --help on the command line, simgrid usually stops right after displaying its help message.
27  * If you are writing a library using simgrid, you may want to display your own help message before everything stops.
28  * If so, just call this function before having simgrid parsing the command line, and you will be given the control
29  * even if the user is asking for help.
30  */
31 void sg_config_continue_after_help()
32 {
33   cfg_continue_after_help = true;
34 }
35
36 /* 0: beginning of time (config cannot be changed yet)
37  * 1: initialized: cfg_set created (config can now be changed)
38  * 2: configured: command line parsed and config part of platform file was
39  *    integrated also, platform construction ongoing or done.
40  *    (Config cannot be changed anymore!)
41  */
42 int _sg_cfg_init_status = 0;
43
44 /* instruct the upper layer (simix or simdag) to exit as soon as possible */
45 bool _sg_cfg_exit_asap = false;
46
47 #define sg_cfg_exit_early()                                                                                            \
48   do {                                                                                                                 \
49     _sg_cfg_exit_asap = true;                                                                                          \
50     return;                                                                                                            \
51   } while (0)
52
53 /* Parse the command line, looking for options */
54 static void sg_config_cmd_line(int *argc, char **argv)
55 {
56   bool shall_exit = false;
57   int i;
58   int j;
59   bool parse_args = true; // Stop parsing the parameters once we found '--'
60
61   for (j = i = 1; i < *argc; i++) {
62     if (not strcmp("--", argv[i])) {
63       parse_args = false;
64       // Remove that '--' from the arguments
65     } else if (parse_args && not strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
66       char *opt = strchr(argv[i], '=');
67       opt++;
68
69       simgrid::config::set_parse(opt);
70       XBT_DEBUG("Did apply '%s' as config setting", opt);
71     } else if (parse_args && not strcmp(argv[i], "--version")) {
72       printf("%s\n", SIMGRID_VERSION_STRING);
73       shall_exit = true;
74     } else if (parse_args && (not strcmp(argv[i], "--cfg-help") || not strcmp(argv[i], "--help"))) {
75       printf("Description of the configuration accepted by this simulator:\n");
76       simgrid::config::help();
77       printf(
78           "\n"
79           "Each of these configurations can be used by adding\n"
80           "    --cfg=<option name>:<option value>\n"
81           "to the command line.\n"
82           "\n"
83           "For more information, please refer to:\n"
84           "   --help-aliases for the list of all option aliases.\n"
85           "   --help-logs and --help-log-categories for the details of logging output.\n"
86           "   --help-models for a list of all models known by this simulator.\n"
87           "   --help-tracing for the details of all tracing options known by this simulator.\n"
88           "   --version to get SimGrid version information.\n"
89           "\n"
90         );
91       shall_exit = not cfg_continue_after_help;
92       argv[j++]  = argv[i]; // Preserve the --help in argv just in case someone else wants to see it
93     } else if (parse_args && not strcmp(argv[i], "--help-aliases")) {
94       printf("Here is a list of all deprecated option names, with their replacement.\n");
95       simgrid::config::show_aliases();
96       printf("Please consider using the recent names\n");
97       shall_exit = true;
98     } else if (parse_args && not strcmp(argv[i], "--help-models")) {
99       model_help("host", surf_host_model_description);
100       printf("\n");
101       model_help("CPU", surf_cpu_model_description);
102       printf("\n");
103       model_help("network", surf_network_model_description);
104       printf("\nLong description of all optimization levels accepted by the models of this simulator:\n");
105       for (int k = 0; surf_optimization_mode_description[k].name; k++)
106         printf("  %s: %s\n",
107                surf_optimization_mode_description[k].name,
108                surf_optimization_mode_description[k].description);
109       printf("Both network and CPU models have 'Lazy' as default optimization level\n\n");
110       shall_exit = true;
111     } else if (parse_args && not strcmp(argv[i], "--help-tracing")) {
112       TRACE_help();
113       shall_exit = true;
114     } else {
115       argv[j++] = argv[i];
116     }
117   }
118   if (j < *argc) {
119     argv[j] = nullptr;
120     *argc = j;
121   }
122   if (shall_exit)
123     sg_cfg_exit_early();
124 }
125
126 /* callback of the plugin variable */
127 static void _sg_cfg_cb__plugin(const std::string& value)
128 {
129   xbt_assert(_sg_cfg_init_status < 2, "Cannot load a plugin after the initialization");
130
131   if (value.empty())
132     return;
133
134   if (value == "help") {
135     model_help("plugin", surf_plugin_description);
136     sg_cfg_exit_early();
137   }
138
139   int plugin_id = find_model_description(surf_plugin_description, value);
140   surf_plugin_description[plugin_id].model_init_preparse();
141 }
142
143 /* callback of the host/model variable */
144 static void _sg_cfg_cb__host_model(const std::string& value)
145 {
146   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
147
148   if (value == "help") {
149     model_help("host", surf_host_model_description);
150     sg_cfg_exit_early();
151   }
152
153   /* Make sure that the model exists */
154   find_model_description(surf_host_model_description, value);
155 }
156
157 /* callback of the cpu/model variable */
158 static void _sg_cfg_cb__cpu_model(const std::string& value)
159 {
160   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
161
162   if (value == "help") {
163     model_help("CPU", surf_cpu_model_description);
164     sg_cfg_exit_early();
165   }
166
167   /* New Module missing */
168   find_model_description(surf_cpu_model_description, value);
169 }
170
171 /* callback of the cpu/model variable */
172 static void _sg_cfg_cb__optimization_mode(const std::string& value)
173 {
174   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
175
176   if (value == "help") {
177     model_help("optimization", surf_optimization_mode_description);
178     sg_cfg_exit_early();
179   }
180
181   /* New Module missing */
182   find_model_description(surf_optimization_mode_description, value);
183 }
184
185 /* callback of the cpu/model variable */
186 static void _sg_cfg_cb__storage_mode(const std::string& value)
187 {
188   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
189
190   if (value == "help") {
191     model_help("storage", surf_storage_model_description);
192     sg_cfg_exit_early();
193   }
194
195   find_model_description(surf_storage_model_description, value);
196 }
197
198 /* callback of the network_model variable */
199 static void _sg_cfg_cb__network_model(const std::string& value)
200 {
201   xbt_assert(_sg_cfg_init_status < 2, "Cannot change the model after the initialization");
202
203   if (value == "help") {
204     model_help("network", surf_network_model_description);
205     sg_cfg_exit_early();
206   }
207
208   /* New Module missing */
209   find_model_description(surf_network_model_description, value);
210 }
211
212 static void _sg_cfg_cb_contexts_parallel_mode(const std::string& mode_name)
213 {
214   if (mode_name == "posix") {
215     SIMIX_context_set_parallel_mode(XBT_PARMAP_POSIX);
216   } else if (mode_name == "futex") {
217     SIMIX_context_set_parallel_mode(XBT_PARMAP_FUTEX);
218   } else if (mode_name == "busy_wait") {
219     SIMIX_context_set_parallel_mode(XBT_PARMAP_BUSY_WAIT);
220   } else {
221     xbt_die("Command line setting of the parallel synchronization mode should "
222             "be one of \"posix\", \"futex\" or \"busy_wait\"");
223   }
224 }
225
226 /* build description line with possible values */
227 static void describe_model(char *result,int resultsize,
228                            const s_surf_model_description_t model_description[],
229                            const char *name,
230                            const char *description)
231 {
232   result[0] = '\0';
233   char *p = result;
234   p += snprintf(result,resultsize-1, "%s. Possible values: %s", description,
235             model_description[0].name ? model_description[0].name : "n/a");
236   for (int i = 1; model_description[i].name; i++)
237     p += snprintf(p,resultsize-(p-result)-1, ", %s", model_description[i].name);
238   p += snprintf(p,resultsize-(p-result)-1, ".\n       (use 'help' as a value to see the long description of each %s)", name);
239
240   xbt_assert(p<result+resultsize-1,"Buffer too small to display the model description of %s",name);
241 }
242
243 /* create the config set, register what should be and parse the command line*/
244 void sg_config_init(int *argc, char **argv)
245 {
246   const int descsize = 1024;
247   char description[descsize];
248
249   /* Create the configuration support */
250   if (_sg_cfg_init_status != 0) { /* Only create stuff if not already inited */
251     XBT_WARN("Call to sg_config_init() after initialization ignored");
252     return;
253   }
254
255   /* Plugins configuration */
256   describe_model(description, descsize, surf_plugin_description, "plugin", "The plugins");
257   simgrid::config::declare_flag<std::string>("plugin", description, "", &_sg_cfg_cb__plugin);
258
259   describe_model(description, descsize, surf_cpu_model_description, "model", "The model to use for the CPU");
260   simgrid::config::declare_flag<std::string>("cpu/model", description, "Cas01", &_sg_cfg_cb__cpu_model);
261
262   describe_model(description, descsize, surf_storage_model_description, "model", "The model to use for the storage");
263   simgrid::config::declare_flag<std::string>("storage/model", description, "default", &_sg_cfg_cb__storage_mode);
264
265   describe_model(description, descsize, surf_network_model_description, "model", "The model to use for the network");
266   simgrid::config::declare_flag<std::string>("network/model", description, "LV08", &_sg_cfg_cb__network_model);
267
268   describe_model(description, descsize, surf_optimization_mode_description, "optimization mode",
269                  "The optimization modes to use for the network");
270   simgrid::config::declare_flag<std::string>("network/optim", description, "Lazy", &_sg_cfg_cb__optimization_mode);
271
272   describe_model(description, descsize, surf_host_model_description, "model", "The model to use for the host");
273   simgrid::config::declare_flag<std::string>("host/model", description, "default", &_sg_cfg_cb__host_model);
274
275   simgrid::config::bind_flag(sg_surf_precision, "surf/precision",
276                              "Numerical precision used when updating simulation times (in seconds)");
277
278   simgrid::config::bind_flag(sg_maxmin_precision, "maxmin/precision",
279                              "Numerical precision used when computing resource sharing (in flops/sec or bytes/sec)");
280
281   simgrid::config::bind_flag(sg_concurrency_limit, "maxmin/concurrency-limit", {"maxmin/concurrency_limit"},
282                              "Maximum number of concurrent variables in the maxmim system. Also limits the number of "
283                              "processes on each host, at higher level. (default: -1 means no such limitation)");
284
285   /* The parameters of network models */
286
287   sg_latency_factor = 13.01; // comes from the default LV08 network model
288   simgrid::config::bind_flag(sg_latency_factor, "network/latency-factor", {"network/latency_factor"},
289                              "Correction factor to apply to the provided latency (default value set by network model)");
290
291   sg_bandwidth_factor = 0.97; // comes from the default LV08 network model
292   simgrid::config::bind_flag(
293       sg_bandwidth_factor, "network/bandwidth-factor", {"network/bandwidth_factor"},
294       "Correction factor to apply to the provided bandwidth (default value set by network model)");
295
296   sg_weight_S_parameter = 20537; // comes from the default LV08 network model
297   simgrid::config::bind_flag(
298       sg_weight_S_parameter, "network/weight-S", {"network/weight_S"},
299       "Correction factor to apply to the weight of competing streams (default value set by network model)");
300
301   /* Inclusion path */
302   simgrid::config::declare_flag<std::string>("path", "Lookup path for inclusions in platform and deployment XML files",
303                                              "", [](std::string const& path) {
304                                                if (not path.empty())
305                                                  surf_path.push_back(path);
306                                              });
307
308   simgrid::config::declare_flag<bool>("cpu/maxmin-selective-update",
309                                       "Update the constraint set propagating recursively to others constraints "
310                                       "(off by default unless optim is set to lazy)",
311                                       "no");
312   simgrid::config::alias("cpu/maxmin-selective-update", {"cpu/maxmin_selective_update"});
313   simgrid::config::declare_flag<bool>("network/maxmin-selective-update", "Update the constraint set propagating "
314                                                                          "recursively to others constraints (off by "
315                                                                          "default unless optim is set to lazy)",
316                                       "no");
317   simgrid::config::alias("network/maxmin-selective-update", {"network/maxmin_selective_update"});
318
319   extern bool _sg_do_verbose_exit;
320   simgrid::config::bind_flag(_sg_do_verbose_exit, "verbose-exit", "Activate the \"do nothing\" mode in Ctrl-C");
321
322   simgrid::config::declare_flag<int>("contexts/stack-size", "Stack size of contexts in KiB (not with threads)",
323                                      8 * 1024, [](int value) { smx_context_stack_size = value * 1024; });
324   simgrid::config::alias("contexts/stack-size", {"contexts/stack_size"});
325
326   /* guard size for contexts stacks in memory pages */
327 #if defined(_WIN32) || (PTH_STACKGROWTH != -1)
328   int default_guard_size = 0;
329 #else
330   int default_guard_size = 1;
331 #endif
332   simgrid::config::declare_flag<int>("contexts/guard-size", "Guard size for contexts stacks in memory pages",
333                                      default_guard_size,
334                                      [](int value) { smx_context_guard_size = value * xbt_pagesize; });
335   simgrid::config::alias("contexts/guard-size", {"contexts/guard_size"});
336   simgrid::config::declare_flag<int>("contexts/nthreads", "Number of parallel threads used to execute user contexts", 1,
337                                      &SIMIX_context_set_nthreads);
338
339   simgrid::config::declare_flag<int>("contexts/parallel-threshold",
340                                      "Minimal number of user contexts to be run in parallel (raw contexts only)", 2,
341                                      &SIMIX_context_set_parallel_threshold);
342   simgrid::config::alias("contexts/parallel-threshold", {"contexts/parallel_threshold"});
343
344   /* synchronization mode for parallel user contexts */
345 #if HAVE_FUTEX_H
346   std::string default_synchro_mode = "futex";
347 #else // No futex on mac and posix is unimplemented yet
348   std::string default_synchro_mode = "busy_wait";
349 #endif
350   simgrid::config::declare_flag<std::string>("contexts/synchro", "Synchronization mode to use when running contexts in "
351                                                                  "parallel (either futex, posix or busy_wait)",
352                                              default_synchro_mode, &_sg_cfg_cb_contexts_parallel_mode);
353
354   // For smpi/bw-factor and smpi/lat-factor
355   // SMPI model can be used without enable_smpi, so keep this out of the ifdef.
356   simgrid::config::declare_flag<std::string>("smpi/bw-factor",
357                                              "Bandwidth factors for smpi. Format: "
358                                              "'threshold0:value0;threshold1:value1;...;thresholdN:valueN', "
359                                              "meaning if(size >=thresholdN ) return valueN.",
360                                              "65472:0.940694;15424:0.697866;9376:0.58729;5776:1.08739;3484:0.77493;"
361                                              "1426:0.608902;732:0.341987;257:0.338112;0:0.812084");
362   simgrid::config::alias("smpi/bw-factor", {"smpi/bw_factor"});
363
364   simgrid::config::declare_flag<std::string>("smpi/lat-factor", "Latency factors for smpi.",
365                                              "65472:11.6436;15424:3.48845;9376:2.59299;5776:2.18796;3484:1.88101;"
366                                              "1426:1.61075;732:1.9503;257:1.95341;0:2.01467");
367   simgrid::config::alias("smpi/lat-factor", {"smpi/lat_factor"});
368   simgrid::config::declare_flag<std::string>("smpi/IB-penalty-factors",
369                                              "Correction factor to communications using Infiniband model with "
370                                              "contention (default value based on Stampede cluster profiling)",
371                                              "0.965;0.925;1.35");
372   simgrid::config::alias("smpi/IB-penalty-factors", {"smpi/IB_penalty_factors"});
373
374 #if HAVE_SMPI
375   simgrid::config::declare_flag<double>("smpi/host-speed", "Speed of the host running the simulation (in flop/s). "
376                                                            "Used to bench the operations.",
377                                         20000.0);
378   simgrid::config::alias("smpi/host-speed", {"smpi/running_power", "smpi/running-power"});
379
380   simgrid::config::declare_flag<bool>("smpi/keep-temps", "Whether we should keep the generated temporary files.",
381                                       false);
382
383   simgrid::config::declare_flag<bool>("smpi/display-timing", "Whether we should display the timing after simulation.",
384                                       false);
385   simgrid::config::alias("smpi/display-timing", {"smpi/display_timing"});
386
387   simgrid::config::declare_flag<bool>(
388       "smpi/simulate-computation", "Whether the computational part of the simulated application should be simulated.",
389       true);
390   simgrid::config::alias("smpi/simulate-computation", {"smpi/simulate_computation"});
391
392   simgrid::config::declare_flag<std::string>(
393       "smpi/shared-malloc", "Whether SMPI_SHARED_MALLOC is enabled. Disable it for debugging purposes.", "global");
394   simgrid::config::alias("smpi/shared-malloc", {"smpi/use_shared_malloc", "smpi/use-shared-malloc"});
395   simgrid::config::declare_flag<double>("smpi/shared-malloc-blocksize",
396                                         "Size of the bogus file which will be created for global shared allocations",
397                                         1UL << 20);
398   simgrid::config::declare_flag<std::string>("smpi/shared-malloc-hugepage",
399                                              "Path to a mounted hugetlbfs, to use huge pages with shared malloc.", "");
400
401   simgrid::config::declare_flag<double>(
402       "smpi/cpu-threshold", "Minimal computation time (in seconds) not discarded, or -1 for infinity.", 1e-6);
403   simgrid::config::alias("smpi/cpu-threshold", {"smpi/cpu_threshold"});
404
405   simgrid::config::declare_flag<int>(
406       "smpi/async-small-thresh",
407       "Maximal size of messages that are to be sent asynchronously, without waiting for the receiver", 0);
408   simgrid::config::alias("smpi/async-small-thresh", {"smpi/async_small_thres", "smpi/async_small_thresh"});
409
410   simgrid::config::declare_flag<bool>("smpi/trace-call-location",
411                                       "Should filename and linenumber of MPI calls be traced?", false);
412
413   simgrid::config::declare_flag<int>(
414       "smpi/send-is-detached-thresh",
415       "Threshold of message size where MPI_Send stops behaving like MPI_Isend and becomes MPI_Ssend", 65536);
416   simgrid::config::alias("smpi/send-is-detached-thresh",
417                          {"smpi/send_is_detached_thres", "smpi/send_is_detached_thresh"});
418
419   const char* default_privatization = std::getenv("SMPI_PRIVATIZATION");
420   if (default_privatization == nullptr)
421     default_privatization = "no";
422
423   simgrid::config::declare_flag<std::string>(
424       "smpi/privatization", "How we should privatize global variable at runtime (no, yes, mmap, dlopen).",
425       default_privatization);
426   simgrid::config::alias("smpi/privatization", {"smpi/privatize_global_variables", "smpi/privatize-global-variables"});
427
428   simgrid::config::declare_flag<std::string>(
429       "smpi/privatize-libs", "Add libraries (; separated) to privatize (libgfortran for example). You need to provide the full names of the files (libgfortran.so.4), or its full path", "");
430
431   simgrid::config::declare_flag<bool>("smpi/grow-injected-times",
432                                       "Whether we want to make the injected time in MPI_Iprobe and MPI_Test grow, to "
433                                       "allow faster simulation. This can make simulation less precise, though.",
434                                       true);
435
436 #if HAVE_PAPI
437   simgrid::config::declare_flag<std::string>("smpi/papi-events",
438                                              "This switch enables tracking the specified counters with PAPI", "");
439 #endif
440   simgrid::config::declare_flag<std::string>("smpi/comp-adjustment-file",
441                                              "A file containing speedups or slowdowns for some parts of the code.", "");
442   simgrid::config::declare_flag<std::string>(
443       "smpi/os", "Small messages timings (MPI_Send minimum time for small messages)", "0:0:0:0:0");
444   simgrid::config::declare_flag<std::string>(
445       "smpi/ois", "Small messages timings (MPI_Isend minimum time for small messages)", "0:0:0:0:0");
446   simgrid::config::declare_flag<std::string>(
447       "smpi/or", "Small messages timings (MPI_Recv minimum time for small messages)", "0:0:0:0:0");
448
449   simgrid::config::declare_flag<double>("smpi/iprobe-cpu-usage",
450                                         "Maximum usage of CPUs by MPI_Iprobe() calls. We've observed that MPI_Iprobes "
451                                         "consume significantly less power than the maximum of a specific application. "
452                                         "This value is then (Iprobe_Usage/Max_Application_Usage).",
453                                         1.0);
454
455   simgrid::config::declare_flag<std::string>("smpi/coll-selector", "Which collective selector to use", "default");
456   simgrid::config::alias("smpi/coll-selector", {"smpi/coll_selector"});
457   simgrid::config::declare_flag<std::string>("smpi/gather", "Which collective to use for gather", "");
458   simgrid::config::declare_flag<std::string>("smpi/allgather", "Which collective to use for allgather", "");
459   simgrid::config::declare_flag<std::string>("smpi/barrier", "Which collective to use for barrier", "");
460   simgrid::config::declare_flag<std::string>("smpi/reduce_scatter", "Which collective to use for reduce_scatter", "");
461   simgrid::config::alias("smpi/reduce_scatter", {"smpi/reduce-scatter"});
462   simgrid::config::declare_flag<std::string>("smpi/scatter", "Which collective to use for scatter", "");
463   simgrid::config::declare_flag<std::string>("smpi/allgatherv", "Which collective to use for allgatherv", "");
464   simgrid::config::declare_flag<std::string>("smpi/allreduce", "Which collective to use for allreduce", "");
465   simgrid::config::declare_flag<std::string>("smpi/alltoall", "Which collective to use for alltoall", "");
466   simgrid::config::declare_flag<std::string>("smpi/alltoallv", "Which collective to use for alltoallv", "");
467   simgrid::config::declare_flag<std::string>("smpi/bcast", "Which collective to use for bcast", "");
468   simgrid::config::declare_flag<std::string>("smpi/reduce", "Which collective to use for reduce", "");
469 #endif // HAVE_SMPI
470
471   /* Others */
472
473   simgrid::config::declare_flag<bool>(
474       "exception/cutpath", "Whether to cut all path information from call traces, used e.g. in exceptions.", false);
475
476   extern bool _sg_do_clean_atexit;
477   simgrid::config::bind_flag(_sg_do_clean_atexit, "clean-atexit", {"clean_atexit"},
478                              "Whether to cleanup SimGrid at exit. Disable it if your code segfaults after its end.");
479
480   if (surf_path.empty())
481     simgrid::config::set_default<std::string>("path", "./");
482
483   _sg_cfg_init_status = 1;
484
485   sg_config_cmd_line(argc, argv);
486
487   xbt_mallocator_initialization_is_done(SIMIX_context_is_parallel());
488 }
489
490 void sg_config_finalize()
491 {
492   if (not _sg_cfg_init_status)
493     return;                     /* Not initialized yet. Nothing to do */
494
495   simgrid::config::finalize();
496   _sg_cfg_init_status = 0;
497 }