+ SIMIX_comm_set_copy_data_callback(smpi_comm_copy_buffer_callback);
+
+ static std::size_t rank = 0;
+
+ if (smpi_privatize_global_variables == SMPI_PRIVATIZE_DLOPEN) {
+
+ std::string executable_copy = executable;
+ simix_global->default_function = [executable_copy](std::vector<std::string> args) {
+ return std::function<void()>([executable_copy, args] {
+
+ // Copy the dynamic library:
+ std::string target_executable = executable_copy
+ + "_" + std::to_string(getpid())
+ + "_" + std::to_string(rank++) + ".so";
+ // TODO, execute directly instead of relying on cp
+ const char* command1 [] = {
+ "cp", "--reflink=auto", "--", executable_copy.c_str(), target_executable.c_str(),
+ nullptr
+ };
+ const char* command2 [] = {
+ "cp", "--", executable_copy.c_str(), target_executable.c_str(),
+ nullptr
+ };
+ if (execute_command(command1) != 0 && execute_command(command2) != 0)
+ xbt_die("copy failed");
+
+ // Load the copy and resolve the entry point:
+ void* handle = dlopen(target_executable.c_str(), RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
+ unlink(target_executable.c_str());
+ if (handle == nullptr)
+ xbt_die("dlopen failed");
+ smpi_entry_point_type entry_point = smpi_resolve_function(handle);
+ if (!entry_point)
+ xbt_die("Could not resolve entry point");
+
+ smpi_run_entry_point(entry_point, args);
+ });
+ };
+
+ }
+ else {
+
+ // Load the dynamic library and resolve the entry point:
+ void* handle = dlopen(executable, RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
+ if (handle == nullptr)
+ xbt_die("dlopen failed for %s", executable);
+ smpi_entry_point_type entry_point = smpi_resolve_function(handle);
+ if (!entry_point)
+ xbt_die("main not found in %s", executable);
+ // TODO, register the executable for SMPI privatization
+
+ // Execute the same entry point for each simulated process:
+ simix_global->default_function = [entry_point](std::vector<std::string> args) {
+ return std::function<void()>([entry_point, args] {
+ smpi_run_entry_point(entry_point, args);
+ });
+ };
+
+ }
+