Logo AND Algorithmique Numérique Distribuée

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