Logo AND Algorithmique Numérique Distribuée

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