Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add smpi/pedantic flag to avoiding reporting controversial errors that may or may...
[simgrid.git] / src / smpi / internals / smpi_config.cpp
1 /* Copyright (c) 2008-2021. 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 #include "smpi_config.hpp"
6 #include "include/xbt/config.hpp"
7 #include "mc/mc.h"
8 #include "private.hpp"
9 #include "smpi_coll.hpp"
10 #include "src/simix/smx_private.hpp"
11 #include "xbt/parse_units.hpp"
12
13 #include <cfloat> /* DBL_MAX */
14 #include <boost/algorithm/string.hpp> /* trim */
15 #include <boost/tokenizer.hpp>
16
17 #if SIMGRID_HAVE_MC
18 #include "src/mc/mc_config.hpp"
19 #endif
20
21 #if defined(__APPLE__)
22 # include <AvailabilityMacros.h>
23 # ifndef MAC_OS_X_VERSION_10_12
24 #   define MAC_OS_X_VERSION_10_12 101200
25 # endif
26 constexpr bool HAVE_WORKING_MMAP = (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12);
27 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__sun) || defined(__HAIKU__)
28 constexpr bool HAVE_WORKING_MMAP = false;
29 #else
30 constexpr bool HAVE_WORKING_MMAP = true;
31 #endif
32
33 SharedMallocType _smpi_cfg_shared_malloc = SharedMallocType::GLOBAL;
34 SmpiPrivStrategies _smpi_cfg_privatization = SmpiPrivStrategies::NONE;
35 double _smpi_cfg_host_speed;
36
37 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_config, smpi, "Logging specific to SMPI (config)");
38
39 simgrid::config::Flag<std::string> _smpi_cfg_host_speed_string{
40     "smpi/host-speed", "Speed of the host running the simulation (in flop/s). Used to bench the operations.", "20000f",
41     [](const std::string& str) {
42       _smpi_cfg_host_speed = xbt_parse_get_speed("smpi/host-speed", 1, str, "option smpi/host-speed");
43       xbt_assert(_smpi_cfg_host_speed > 0.0, "Invalid value (%s) for 'smpi/host-speed': it must be positive.",
44                  _smpi_cfg_host_speed_string.get().c_str());
45     }};
46
47 simgrid::config::Flag<bool> _smpi_cfg_simulate_computation{
48   "smpi/simulate-computation", "Whether the computational part of the simulated application should be simulated.",
49    true};
50 simgrid::config::Flag<std::string> _smpi_cfg_shared_malloc_string{
51   "smpi/shared-malloc", "Whether SMPI_SHARED_MALLOC is enabled. Disable it for debugging purposes.", "global", 
52   [](const std::string& val) {   
53     if ((val == "yes") || (val == "1") || (val == "on") || (val == "global")) {
54       _smpi_cfg_shared_malloc = SharedMallocType::GLOBAL;
55     } else if (val == "local") {
56       _smpi_cfg_shared_malloc = SharedMallocType::LOCAL;
57     } else if ((val == "no") || (val == "0") || (val == "off")) {
58       _smpi_cfg_shared_malloc = SharedMallocType::NONE;
59     } else {
60       xbt_die("Invalid value '%s' for option smpi/shared-malloc. Possible values: 'on' or 'global', 'local', 'off'",
61       val.c_str());
62     } 
63   } };
64
65 simgrid::config::Flag<double> _smpi_cfg_cpu_threshold{
66   "smpi/cpu-threshold", "Minimal computation time (in seconds) not discarded, or -1 for infinity.", 1e-6,
67   [](const double& val){
68     if (val < 0)
69       _smpi_cfg_cpu_threshold = DBL_MAX;
70   }};
71
72 simgrid::config::Flag<int> _smpi_cfg_async_small_thresh{"smpi/async-small-thresh",
73                                                         "Maximal size of messages that are to be sent asynchronously, without waiting for the receiver",
74                                                         0};
75 simgrid::config::Flag<int> _smpi_cfg_detached_send_thresh{"smpi/send-is-detached-thresh",
76                                                           "Threshold of message size where MPI_Send stops behaving like MPI_Isend and becomes MPI_Ssend", 
77                                                           65536};
78 simgrid::config::Flag<bool> _smpi_cfg_grow_injected_times{"smpi/grow-injected-times",
79                                                           "Whether we want to make the injected time in MPI_Iprobe and MPI_Test grow, to "
80                                                           "allow faster simulation. This can make simulation less precise, though.",
81                                                           true};
82 simgrid::config::Flag<double> _smpi_cfg_iprobe_cpu_usage{"smpi/iprobe-cpu-usage",
83                                                         "Maximum usage of CPUs by MPI_Iprobe() calls. We've observed that MPI_Iprobes "
84                                                         "consume significantly less power than the maximum of a specific application. "
85                                                         "This value is then (Iprobe_Usage/Max_Application_Usage).",
86                                                         1.0};
87
88 simgrid::config::Flag<bool>  _smpi_cfg_trace_call_location{"smpi/trace-call-location",
89                                                            "Should filename and linenumber of MPI calls be traced?", false};
90 simgrid::config::Flag<bool> _smpi_cfg_trace_call_use_absolute_path{"smpi/trace-call-use-absolute-path",
91                                                                    "Should filenames for trace-call tracing be absolute or not?", false};
92 simgrid::config::Flag<std::string> _smpi_cfg_comp_adjustment_file{"smpi/comp-adjustment-file",
93     "A file containing speedups or slowdowns for some parts of the code.", 
94     "", [](const std::string& filename){
95       if (not filename.empty()) {
96         std::ifstream fstream(filename);
97         xbt_assert(fstream.is_open(), "Could not open file %s. Does it exist?", filename.c_str());
98         std::string line;
99         using Tokenizer = boost::tokenizer<boost::escaped_list_separator<char>>;
100         std::getline(fstream, line); // Skip the header line
101         while (std::getline(fstream, line)) {
102           Tokenizer tok(line);
103           Tokenizer::iterator it  = tok.begin();
104           Tokenizer::iterator end = std::next(tok.begin());
105           std::string location = *it;
106           boost::trim(location);
107           location2speedup.insert(std::pair<std::string, double>(location, std::stod(*end)));
108         }
109       }
110     }};
111
112 simgrid::config::Flag<bool> _smpi_cfg_default_errhandler_is_error{
113   "smpi/errors-are-fatal", "Whether MPI errors are fatal or just return. Default is true", true };
114 simgrid::config::Flag<bool> _smpi_cfg_pedantic{
115   "smpi/pedantic", "Activate extra checks that may crash slightly incorrect codes which would not crash on actual implementations", true };
116 #if HAVE_PAPI
117   simgrid::config::Flag<std::string> _smpi_cfg_papi_events_file{"smpi/papi-events",
118                                                                 "This switch enables tracking the specified counters with PAPI", ""};
119 #endif
120
121 simgrid::config::Flag<double> _smpi_cfg_auto_shared_malloc_thresh("smpi/auto-shared-malloc-thresh",
122                                                                   "Threshold size for the automatic sharing of memory",
123                                                                   0);
124
125 simgrid::config::Flag<bool> _smpi_cfg_display_alloc("smpi/display-allocs",
126                                                     "Whether we should display a memory allocations analysis after simulation.",
127                                                      false);
128
129 simgrid::config::Flag<int> _smpi_cfg_list_leaks("smpi/list-leaks",
130                                                 "Whether we should display the n first MPI handle leaks (addresses and type only) after simulation",
131                                                 -1);
132
133 double smpi_cfg_host_speed(){
134   return _smpi_cfg_host_speed;
135 }
136
137 bool smpi_cfg_simulate_computation(){
138   return _smpi_cfg_simulate_computation;
139 }
140
141 SharedMallocType smpi_cfg_shared_malloc(){
142   return _smpi_cfg_shared_malloc;
143 }
144
145 double smpi_cfg_cpu_thresh(){
146   return _smpi_cfg_cpu_threshold;
147 }
148
149 SmpiPrivStrategies smpi_cfg_privatization(){
150   return _smpi_cfg_privatization;
151 }
152
153 int smpi_cfg_async_small_thresh(){
154   return _smpi_cfg_async_small_thresh;
155 }
156
157 int smpi_cfg_detached_send_thresh(){
158   return _smpi_cfg_detached_send_thresh;
159 }
160
161 bool smpi_cfg_grow_injected_times(){
162   return _smpi_cfg_grow_injected_times;
163 }
164
165 double smpi_cfg_iprobe_cpu_usage(){
166   return _smpi_cfg_iprobe_cpu_usage;
167 }
168
169 bool smpi_cfg_trace_call_location(){
170   return _smpi_cfg_trace_call_location;
171 }
172
173 bool smpi_cfg_trace_call_use_absolute_path(){
174   return _smpi_cfg_trace_call_use_absolute_path;
175 }
176
177 bool smpi_cfg_display_alloc(){
178   return _smpi_cfg_list_leaks != -1 ? true : _smpi_cfg_display_alloc;
179 }
180
181 std::string smpi_cfg_comp_adjustment_file(){
182   return _smpi_cfg_comp_adjustment_file;
183 }
184 #if HAVE_PAPI
185 std::string smpi_cfg_papi_events_file(){
186   return _smpi_cfg_papi_events_file;
187 }
188 #endif
189 double smpi_cfg_auto_shared_malloc_thresh(){
190   return _smpi_cfg_auto_shared_malloc_thresh;
191 }
192
193 // public version declared in smpi.h (without parameter, and with C linkage)
194 void smpi_init_options()
195 {
196   smpi_init_options_internal(false);
197 }
198
199 void smpi_init_options_internal(bool called_by_smpi_main)
200 {
201   static bool smpi_options_initialized = false;
202   static bool running_with_smpi_main   = false;
203
204   if (called_by_smpi_main)
205     running_with_smpi_main = true;
206
207   // return if already called
208   if (smpi_options_initialized)
209     return;
210   simgrid::config::declare_flag<bool>("smpi/display-timing", "Whether we should display the timing after simulation.", false);
211   simgrid::config::declare_flag<bool>("smpi/keep-temps", "Whether we should keep the generated temporary files.", false);
212   simgrid::config::declare_flag<std::string>("smpi/tmpdir", "tmp dir for dlopen files", "/tmp");
213
214   simgrid::config::declare_flag<std::string>("smpi/coll-selector", "Which collective selector to use", "default");
215   simgrid::config::declare_flag<std::string>("smpi/gather", "Which collective to use for gather", "");
216   simgrid::config::declare_flag<std::string>("smpi/allgather", "Which collective to use for allgather", "");
217   simgrid::config::declare_flag<std::string>("smpi/barrier", "Which collective to use for barrier", "");
218   simgrid::config::declare_flag<std::string>("smpi/reduce_scatter", "Which collective to use for reduce_scatter", "");
219   simgrid::config::declare_flag<std::string>("smpi/scatter", "Which collective to use for scatter", "");
220   simgrid::config::declare_flag<std::string>("smpi/allgatherv", "Which collective to use for allgatherv", "");
221   simgrid::config::declare_flag<std::string>("smpi/allreduce", "Which collective to use for allreduce", "");
222   simgrid::config::declare_flag<std::string>("smpi/alltoall", "Which collective to use for alltoall", "");
223   simgrid::config::declare_flag<std::string>("smpi/alltoallv", "Which collective to use for alltoallv", "");
224   simgrid::config::declare_flag<std::string>("smpi/bcast", "Which collective to use for bcast", "");
225   simgrid::config::declare_flag<std::string>("smpi/reduce", "Which collective to use for reduce", "");
226
227   const char* default_privatization = std::getenv("SMPI_PRIVATIZATION");
228   if (default_privatization == nullptr)
229     default_privatization = "no";
230
231   simgrid::config::declare_flag<std::string>(
232       "smpi/privatization", "How we should privatize global variable at runtime (no, yes, mmap, dlopen).",
233       default_privatization, [](const std::string& smpi_privatize_option) {
234         if (smpi_privatize_option == "no" || smpi_privatize_option == "0")
235           _smpi_cfg_privatization = SmpiPrivStrategies::NONE;
236         else if (smpi_privatize_option == "yes" || smpi_privatize_option == "1")
237           _smpi_cfg_privatization = SmpiPrivStrategies::DEFAULT;
238         else if (smpi_privatize_option == "mmap")
239           _smpi_cfg_privatization = SmpiPrivStrategies::MMAP;
240         else if (smpi_privatize_option == "dlopen")
241           _smpi_cfg_privatization = SmpiPrivStrategies::DLOPEN;
242         else
243           xbt_die("Invalid value for smpi/privatization: '%s'", smpi_privatize_option.c_str());
244
245         if (not running_with_smpi_main) {
246           XBT_DEBUG("Running without smpi_main(); disable smpi/privatization.");
247           _smpi_cfg_privatization = SmpiPrivStrategies::NONE;
248         }
249         if (not HAVE_WORKING_MMAP && _smpi_cfg_privatization == SmpiPrivStrategies::MMAP) {
250           XBT_INFO("mmap privatization is broken on this platform, switching to dlopen privatization instead.");
251           _smpi_cfg_privatization = SmpiPrivStrategies::DLOPEN;
252         }
253       });
254
255   simgrid::config::declare_flag<std::string>("smpi/privatize-libs", 
256                                             "Add libraries (; separated) to privatize (libgfortran for example)."
257                                             "You need to provide the full names of the files (libgfortran.so.4), or its full path", 
258                                             "");
259   simgrid::config::declare_flag<double>("smpi/shared-malloc-blocksize",
260                                         "Size of the bogus file which will be created for global shared allocations", 
261                                         1UL << 20);
262   simgrid::config::declare_flag<std::string>("smpi/shared-malloc-hugepage",
263                                              "Path to a mounted hugetlbfs, to use huge pages with shared malloc.", 
264                                              "");
265
266   simgrid::config::declare_flag<std::string>(
267       "smpi/os", "Small messages timings (MPI_Send minimum time for small messages)", "0:0:0:0:0");
268   simgrid::config::declare_flag<std::string>(
269       "smpi/ois", "Small messages timings (MPI_Isend minimum time for small messages)", "0:0:0:0:0");
270   simgrid::config::declare_flag<std::string>(
271       "smpi/or", "Small messages timings (MPI_Recv minimum time for small messages)", "0:0:0:0:0");
272
273   simgrid::config::declare_flag<bool>("smpi/finalization-barrier", "Do we add a barrier in MPI_Finalize or not", false);
274
275   smpi_options_initialized = true;
276 }
277
278 void smpi_check_options()
279 {
280 #if SIMGRID_HAVE_MC
281   if (MC_is_active()) {
282     if (_sg_mc_buffering == "zero")
283       simgrid::config::set_value<int>("smpi/send-is-detached-thresh", 0);
284     else if (_sg_mc_buffering == "infty")
285       simgrid::config::set_value<int>("smpi/send-is-detached-thresh", INT_MAX);
286     else
287       THROW_IMPOSSIBLE;
288   }
289 #endif
290
291   xbt_assert(smpi_cfg_async_small_thresh() <= smpi_cfg_detached_send_thresh(),
292              "smpi/async-small-thresh (=%d) should be smaller or equal to smpi/send-is-detached-thresh (=%d)",
293              smpi_cfg_async_small_thresh(),
294              smpi_cfg_detached_send_thresh());
295
296   if (simgrid::config::is_default("smpi/host-speed") && not MC_is_active()) {
297     XBT_INFO("You did not set the power of the host running the simulation.  "
298              "The timings will certainly not be accurate.  "
299              "Use the option \"--cfg=smpi/host-speed:<flops>\" to set its value.  "
300              "Check "
301              "https://simgrid.org/doc/latest/Configuring_SimGrid.html#automatic-benchmarking-of-smpi-code for more "
302              "information.");
303   }
304
305   simgrid::smpi::colls::set_collectives();
306   simgrid::smpi::colls::smpi_coll_cleanup_callback = nullptr;
307 }
308