Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d2f6966f03abf5307bca91b5c66a13c01397e670
[simgrid.git] / src / surf / surf_interface.cpp
1 /* Copyright (c) 2004-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 #include "surf_interface.hpp"
7 #include "mc/mc.h"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/sg_config.hpp"
10 #include "src/internal_config.h"
11 #include "src/simgrid/version.h"
12 #include "src/surf/HostImpl.hpp"
13 #include "src/surf/xml/platf.hpp"
14 #include "surf/surf.hpp"
15 #include "xbt/module.h"
16
17 #include <fstream>
18 #include <string>
19
20 #ifdef _WIN32
21 #include <windows.h>
22 #endif
23
24 XBT_LOG_NEW_CATEGORY(surf, "All SURF categories");
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_kernel, surf, "Logging specific to SURF (kernel)");
26
27 /*********
28  * Utils *
29  *********/
30
31 std::vector<simgrid::kernel::resource::Model*> all_existing_models; /* to destroy models correctly */
32
33 simgrid::kernel::profile::FutureEvtSet future_evt_set;
34 std::vector<std::string> surf_path;
35 /**  set of hosts for which one want to be notified if they ever restart. */
36 std::set<std::string> watched_hosts;
37 extern std::map<std::string, simgrid::kernel::resource::StorageType*> storage_types;
38
39 s_surf_model_description_t* surf_plugin_description = nullptr;
40 static void XBT_ATTRIB_DESTRUCTOR(800) simgrid_free_plugin_description()
41 {
42   xbt_free(surf_plugin_description);
43 }
44
45 XBT_PUBLIC void simgrid_add_plugin_description(const char* name, const char* description, void_f_void_t init_fun)
46 {
47   static int plugin_amount = 0;
48
49   /* no need to check for plugin name conflict: the compiler already ensures that the generated
50    * simgrid_##id##_plugin_register() is unique */
51
52   plugin_amount++;
53   surf_plugin_description = static_cast<s_surf_model_description_t*>(
54       xbt_realloc(surf_plugin_description, sizeof(s_surf_model_description_t) * (plugin_amount + 2)));
55
56   surf_plugin_description[plugin_amount - 1] = {name, description, init_fun};
57   surf_plugin_description[plugin_amount]     = {nullptr, nullptr, nullptr}; // this array must be null terminated
58 }
59
60 /* Don't forget to update the option description in smx_config when you change this */
61 s_surf_model_description_t surf_network_model_description[] = {
62     {"LV08", "Realistic network analytic model (slow-start modeled by multiplying latency by 13.01, bandwidth by .97; "
63              "bottleneck sharing uses a payload of S=20537 for evaluating RTT). ",
64      &surf_network_model_init_LegrandVelho},
65     {"Constant", "Simplistic network model where all communication take a constant time (one second). This model "
66                  "provides the lowest realism, but is (marginally) faster.",
67      &surf_network_model_init_Constant},
68     {"SMPI", "Realistic network model specifically tailored for HPC settings (accurate modeling of slow start with "
69              "correction factors on three intervals: < 1KiB, < 64 KiB, >= 64 KiB)",
70      &surf_network_model_init_SMPI},
71     {"IB", "Realistic network model specifically tailored for HPC settings, with Infiniband contention model",
72      &surf_network_model_init_IB},
73     {"CM02", "Legacy network analytic model (Very similar to LV08, but without corrective factors. The timings of "
74              "small messages are thus poorly modeled).",
75      &surf_network_model_init_CM02},
76     {"NS3", "Network pseudo-model using the NS3 tcp model instead of an analytic model", &surf_network_model_init_NS3},
77     {"Reno",
78      "Model from Steven H. Low using lagrange_solve instead of lmm_solve (experts only; check the code for more info).",
79      &surf_network_model_init_Reno},
80     {"Reno2",
81      "Model from Steven H. Low using lagrange_solve instead of lmm_solve (experts only; check the code for more info).",
82      &surf_network_model_init_Reno2},
83     {"Vegas",
84      "Model from Steven H. Low using lagrange_solve instead of lmm_solve (experts only; check the code for more info).",
85      &surf_network_model_init_Vegas},
86     {nullptr, nullptr, nullptr} /* this array must be nullptr terminated */
87 };
88
89 #if ! HAVE_SMPI
90 void surf_network_model_init_SMPI() {
91   xbt_die("Please activate SMPI support in cmake to use the SMPI network model.");
92 }
93 void surf_network_model_init_IB() {
94   xbt_die("Please activate SMPI support in cmake to use the IB network model.");
95 }
96 #endif
97 #if !SIMGRID_HAVE_NS3
98 void surf_network_model_init_NS3() {
99   xbt_die("Please activate NS3 support in cmake and install the dependencies to use the NS3 network model.");
100 }
101 #endif
102
103 s_surf_model_description_t surf_cpu_model_description[] = {
104   {"Cas01", "Simplistic CPU model (time=size/power).", &surf_cpu_model_init_Cas01},
105   {nullptr, nullptr,  nullptr}      /* this array must be nullptr terminated */
106 };
107
108 s_surf_model_description_t surf_host_model_description[] = {
109   {"default",   "Default host model. Currently, CPU:Cas01 and network:LV08 (with cross traffic enabled)", &surf_host_model_init_current_default},
110   {"compound",  "Host model that is automatically chosen if you change the network and CPU models", &surf_host_model_init_compound},
111   {"ptask_L07", "Host model somehow similar to Cas01+CM02 but allowing parallel tasks", &surf_host_model_init_ptask_L07},
112   {nullptr, nullptr, nullptr}      /* this array must be nullptr terminated */
113 };
114
115 s_surf_model_description_t surf_optimization_mode_description[] = {
116   {"Lazy", "Lazy action management (partial invalidation in lmm + heap in action remaining).", nullptr},
117   {"TI",   "Trace integration. Highly optimized mode when using availability traces (only available for the Cas01 CPU model for now).", nullptr},
118   {"Full", "Full update of remaining and variables. Slow but may be useful when debugging.", nullptr},
119   {nullptr, nullptr, nullptr}      /* this array must be nullptr terminated */
120 };
121
122 s_surf_model_description_t surf_storage_model_description[] = {
123   {"default", "Simplistic storage model.", &surf_storage_model_init_default},
124   {nullptr, nullptr,  nullptr}      /* this array must be nullptr terminated */
125 };
126
127 double NOW = 0;
128
129 double surf_get_clock()
130 {
131   return NOW;
132 }
133
134 /* returns whether #file_path is a absolute file path. Surprising, isn't it ? */
135 static bool is_absolute_file_path(const std::string& file_path)
136 {
137 #ifdef _WIN32
138   WIN32_FIND_DATA wfd = {0};
139   HANDLE hFile        = FindFirstFile(file_path.c_str(), &wfd);
140
141   if (INVALID_HANDLE_VALUE == hFile)
142     return false;
143
144   FindClose(hFile);
145   return true;
146 #else
147   return (file_path.c_str()[0] == '/');
148 #endif
149 }
150
151 std::ifstream* surf_ifsopen(const std::string& name)
152 {
153   xbt_assert(not name.empty());
154
155   std::ifstream* fs = new std::ifstream();
156   if (is_absolute_file_path(name)) { /* don't mess with absolute file names */
157     fs->open(name.c_str(), std::ifstream::in);
158   }
159
160   /* search relative files in the path */
161   for (auto const& path_elm : surf_path) {
162     std::string buff = path_elm + "/" + name;
163     fs->open(buff.c_str(), std::ifstream::in);
164
165     if (not fs->fail()) {
166       XBT_DEBUG("Found file at %s", buff.c_str());
167       return fs;
168     }
169   }
170
171   return fs;
172 }
173
174 FILE* surf_fopen(const std::string& name, const char* mode)
175 {
176   FILE *file = nullptr;
177
178   if (is_absolute_file_path(name)) /* don't mess with absolute file names */
179     return fopen(name.c_str(), mode);
180
181   /* search relative files in the path */
182   for (auto const& path_elm : surf_path) {
183     std::string buff = path_elm + "/" + name;
184     file             = fopen(buff.c_str(), mode);
185
186     if (file)
187       return file;
188   }
189   return nullptr;
190 }
191
192 /** Displays the long description of all registered models, and quit */
193 void model_help(const char *category, s_surf_model_description_t * table)
194 {
195   printf("Long description of the %s models accepted by this simulator:\n", category);
196   for (int i = 0; table[i].name; i++)
197     printf("  %s: %s\n", table[i].name, table[i].description);
198 }
199
200 int find_model_description(s_surf_model_description_t* table, const std::string& name)
201 {
202   for (int i = 0; table[i].name; i++)
203     if (name == table[i].name)
204       return i;
205
206   if (not table[0].name)
207     xbt_die("No model is valid! This is a bug.");
208
209   std::string name_list = std::string(table[0].name);
210   for (int i = 1; table[i].name; i++)
211     name_list = name_list + ", " + table[i].name;
212
213   xbt_die("Model '%s' is invalid! Valid models are: %s.", name.c_str(), name_list.c_str());
214   return -1;
215 }
216
217 void sg_version_check(int lib_version_major, int lib_version_minor, int lib_version_patch)
218 {
219   if ((lib_version_major != SIMGRID_VERSION_MAJOR) || (lib_version_minor != SIMGRID_VERSION_MINOR)) {
220     fprintf(stderr, "FATAL ERROR: Your program was compiled with SimGrid version %d.%d.%d, "
221                     "and then linked against SimGrid %d.%d.%d. Please fix this.\n",
222             lib_version_major, lib_version_minor, lib_version_patch, SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR,
223             SIMGRID_VERSION_PATCH);
224     abort();
225   }
226   if (lib_version_patch != SIMGRID_VERSION_PATCH) {
227     if (SIMGRID_VERSION_PATCH > 89 || lib_version_patch > 89) {
228       fprintf(
229           stderr,
230           "FATAL ERROR: Your program was compiled with SimGrid version %d.%d.%d, "
231           "and then linked against SimGrid %d.%d.%d. \n"
232           "One of them is a development version, and should not be mixed with the stable release. Please fix this.\n",
233           lib_version_major, lib_version_minor, lib_version_patch, SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR,
234           SIMGRID_VERSION_PATCH);
235       abort();
236     }
237     fprintf(stderr, "Warning: Your program was compiled with SimGrid version %d.%d.%d, "
238                     "and then linked against SimGrid %d.%d.%d. Proceeding anyway.\n",
239             lib_version_major, lib_version_minor, lib_version_patch, SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR,
240             SIMGRID_VERSION_PATCH);
241   }
242 }
243
244 void sg_version_get(int* ver_major, int* ver_minor, int* ver_patch)
245 {
246   *ver_major = SIMGRID_VERSION_MAJOR;
247   *ver_minor = SIMGRID_VERSION_MINOR;
248   *ver_patch = SIMGRID_VERSION_PATCH;
249 }
250
251 void sg_version()
252 {
253   std::printf("This program was linked against %s (git: %s), found in %s.\n",
254               SIMGRID_VERSION_STRING, SIMGRID_GIT_VERSION, SIMGRID_INSTALL_PREFIX);
255
256 #if SIMGRID_HAVE_MC
257   std::printf("   Model-checking support compiled in.\n");
258 #else
259   std::printf("   Model-checking support disabled at compilation.\n");
260 #endif
261
262 #if SIMGRID_HAVE_NS3
263   std::printf("   NS3 support compiled in.\n");
264 #else
265   std::printf("   NS3 support disabled at compilation.\n");
266 #endif
267
268 #if SIMGRID_HAVE_JEDULE
269   std::printf("   Jedule support compiled in.\n");
270 #else
271   std::printf("   Jedule support disabled at compilation.\n");
272 #endif
273
274 #if SIMGRID_HAVE_LUA
275   std::printf("   Lua support compiled in.\n");
276 #else
277   std::printf("   Lua support disabled at compilation.\n");
278 #endif
279
280 #if SIMGRID_HAVE_MALLOCATOR
281   std::printf("   Mallocator support compiled in.\n");
282 #else
283   std::printf("   Mallocator support disabled at compilation.\n");
284 #endif
285
286   std::printf("\nTo cite SimGrid in a publication, please use:\n"
287               "   Henri Casanova, Arnaud Giersch, Arnaud Legrand, Martin Quinson, Frédéric Suter. \n"
288               "   Versatile, Scalable, and Accurate Simulation of Distributed Applications and Platforms. \n"
289               "   Journal of Parallel and Distributed Computing, Elsevier, 2014, 74 (10), pp.2899-2917.\n");
290   std::printf("The pdf file and a BibTeX entry for LaTeX users can be found at http://hal.inria.fr/hal-01017319\n");
291 }
292
293 void surf_init(int *argc, char **argv)
294 {
295   if (USER_HOST_LEVEL != -1) // Already initialized
296     return;
297
298   XBT_DEBUG("Create all Libs");
299   USER_HOST_LEVEL = simgrid::s4u::Host::extension_create(nullptr);
300
301   xbt_init(argc, argv);
302
303   sg_config_init(argc, argv);
304
305   if (MC_is_active())
306     MC_memory_init();
307 }
308
309 void surf_exit()
310 {
311   simgrid::s4u::Engine::shutdown();
312   for (auto const& e : storage_types) {
313     simgrid::kernel::resource::StorageType* stype = e.second;
314     delete stype->properties;
315     delete stype->model_properties;
316     delete stype;
317   }
318
319   for (auto const& model : all_existing_models)
320     delete model;
321
322   tmgr_finalize();
323   sg_platf_exit();
324
325   NOW = 0;                      /* Just in case the user plans to restart the simulation afterward */
326 }