Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Variable may be unused.
[simgrid.git] / src / smpi / internals / smpi_global.cpp
index a8f461b..e75aef9 100644 (file)
@@ -13,6 +13,7 @@
 #include "src/smpi/include/smpi_actor.hpp"
 #include "xbt/config.hpp"
 
+#include <algorithm>
 #include <cfloat> /* DBL_MAX */
 #include <dlfcn.h>
 #include <fcntl.h>
@@ -42,6 +43,16 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi, "Logging specific to SMPI (ke
 #include <boost/tokenizer.hpp>
 #include <boost/algorithm/string.hpp> /* trim_right / trim_left */
 
+#if SMPI_IFORT
+  extern "C" void for_rtl_init_ (int *, char **);
+  extern "C" void for_rtl_finish_ ();
+#elif SMPI_FLANG
+  extern "C" void __io_set_argc(int);
+  extern "C" void __io_set_argv(char **);
+#elif SMPI_GFORTRAN
+  extern "C" void _gfortran_set_args(int, char **);
+#endif
+
 #ifndef RTLD_DEEPBIND
 /* RTLD_DEEPBIND is a bad idea of GNU ld that obviously does not exist on other platforms
  * See https://www.akkadia.org/drepper/dsohowto.pdf
@@ -416,14 +427,37 @@ typedef void (*smpi_fortran_entry_point_type)();
 
 static int smpi_run_entry_point(smpi_entry_point_type entry_point, std::vector<std::string> args)
 {
-  char noarg[]   = {'\0'};
-  const int argc = args.size();
-  std::unique_ptr<char*[]> argv(new char*[argc + 1]);
-  for (int i = 0; i != argc; ++i)
-    argv[i] = args[i].empty() ? noarg : &args[i].front();
-  argv[argc] = nullptr;
-
-  int res = entry_point(argc, argv.get());
+  // copy C strings, we need them writable
+  std::vector<char*>* args4argv = new std::vector<char*>(args.size());
+  std::transform(begin(args), end(args), begin(*args4argv), [](const std::string& s) { return xbt_strdup(s.c_str()); });
+
+#if !SMPI_IFORT
+  // take a copy of args4argv to keep reference of the allocated strings
+  const std::vector<char*> args2str(*args4argv);
+#endif
+  int argc = args4argv->size();
+  args4argv->push_back(nullptr);
+  char** argv = args4argv->data();
+
+  simgrid::smpi::ActorExt::init(&argc, &argv);
+#if SMPI_IFORT
+  for_rtl_init_ (&argc, argv);
+#elif SMPI_FLANG
+  __io_set_argc(argc);
+  __io_set_argv(argv);
+#elif SMPI_GFORTRAN
+  _gfortran_set_args(argc, argv);
+#endif 
+  int res = entry_point(argc, argv);
+
+#if SMPI_IFORT
+  for_rtl_finish_ ();
+#else
+  for (char* s : args2str)
+    xbt_free(s);
+  delete args4argv;
+#endif
+
   if (res != 0){
     XBT_WARN("SMPI process did not return 0. Return value : %d", res);
     if (smpi_exit_status == 0)
@@ -439,7 +473,6 @@ static smpi_entry_point_type smpi_resolve_function(void* handle)
   smpi_fortran_entry_point_type entry_point_fortran = (smpi_fortran_entry_point_type)dlsym(handle, "user_main_");
   if (entry_point_fortran != nullptr) {
     return [entry_point_fortran](int argc, char** argv) {
-      smpi_process_init(&argc, &argv);
       entry_point_fortran();
       return 0;
     };