Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f1656e5caef6778c5356f066f883b4be532c610b
[simgrid.git] / src / smpi / internals / smpi_deployment.cpp
1 /* Copyright (c) 2004-2021. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "smpi_host.hpp"
8 #include "private.hpp"
9 #include "simgrid/s4u/Engine.hpp"
10 #include "simgrid/s4u/Barrier.hpp"
11 #include "smpi_comm.hpp"
12 #include <map>
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smpi);
15
16 namespace simgrid {
17 namespace smpi {
18 namespace app {
19
20 static int universe_size = 0;
21
22 class Instance {
23 public:
24   explicit Instance(int max_no_processes) : size_(max_no_processes)
25   {
26     auto* group = new simgrid::smpi::Group(size_);
27     comm_world_ = new simgrid::smpi::Comm(group, nullptr, false, -1);
28     universe_size += max_no_processes;
29     bar_ = new s4u::Barrier(size_);
30   }
31   s4u::Barrier* bar_;
32   unsigned int size_;
33   unsigned int finalized_ranks_ = 0;
34   MPI_Comm comm_world_;
35 };
36 }
37 }
38 }
39
40 using simgrid::smpi::app::Instance;
41
42 static std::map<std::string, Instance, std::less<>> smpi_instances;
43
44 /** @ingroup smpi_simulation
45  * @brief Registers a running instance of an MPI program.
46  *
47  * @param name the reference name of the function.
48  * @param code either the main mpi function
49  *             (must have a int ..(int argc, char *argv[]) prototype) or nullptr
50  *             (if the function deployment is managed somewhere else —
51  *              e.g., when deploying manually or using smpirun)
52  * @param num_processes the size of the instance we want to deploy
53  */
54 void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_processes)
55 {
56   if (code != nullptr) // When started with smpirun, we will not execute a function
57     simgrid::s4u::Engine::get_instance()->register_function(name, code);
58
59   Instance instance(num_processes);
60
61   smpi_instances.insert(std::pair<std::string, Instance>(name, instance));
62 }
63
64 void smpi_deployment_register_process(const std::string& instance_id, int rank, const simgrid::s4u::Actor* actor)
65 {
66   const Instance& instance = smpi_instances.at(instance_id);
67   instance.comm_world_->group()->set_mapping(actor->get_pid(), rank);
68
69 }
70
71 void smpi_deployment_startup_barrier(const std::string& instance_id)
72 {
73   const Instance& instance = smpi_instances.at(instance_id);
74   instance.bar_->wait();
75 }
76
77 void smpi_deployment_unregister_process(const std::string& instance_id)
78 {
79   Instance& instance = smpi_instances.at(instance_id);
80   instance.finalized_ranks_++;
81
82   if (instance.finalized_ranks_ == instance.size_) {
83     simgrid::smpi::Comm::destroy(instance.comm_world_);
84     delete instance.bar_;
85     smpi_instances.erase(instance_id);
86   }
87 }
88
89 MPI_Comm* smpi_deployment_comm_world(const std::string& instance_id)
90 {
91   if (smpi_instances
92           .empty()) { // no instance registered, we probably used smpirun. (FIXME: I guess this never happens for real)
93     return nullptr;
94   }
95   Instance& instance = smpi_instances.at(instance_id);
96   return &instance.comm_world_;
97 }
98
99 void smpi_deployment_cleanup_instances(){
100   for (auto const& item : smpi_instances) {
101     XBT_INFO("Stalling SMPI instance: %s. Do all your MPI ranks call MPI_Finalize()?", item.first.c_str());
102     Instance instance = item.second;
103     simgrid::smpi::Comm::destroy(instance.comm_world_);
104   }
105   smpi_instances.clear();
106 }
107
108 int smpi_get_universe_size()
109 {
110   return simgrid::smpi::app::universe_size;
111 }
112
113 /** @brief Auxiliary method to get list of hosts to deploy app */
114 static std::vector<simgrid::s4u::Host*> smpi_get_hosts(const simgrid::s4u::Engine* e, const std::string& hostfile)
115 {
116   if (hostfile == "") {
117     return e->get_all_hosts();
118   }
119   std::vector<simgrid::s4u::Host*> hosts;
120   std::ifstream in(hostfile.c_str());
121   xbt_assert(in, "smpirun: Cannot open the host file: %s", hostfile.c_str());
122   std::string str;
123   while (std::getline(in, str)) {
124     if (not str.empty())
125       hosts.emplace_back(e->host_by_name(str));
126   }
127   xbt_assert(not hosts.empty(), "smpirun: the hostfile '%s' is empty", hostfile.c_str());
128   return hosts;
129 }
130
131 /** @brief Read replay configuration from file */
132 static std::vector<std::string> smpi_read_replay(const std::string& replayfile)
133 {
134   std::vector<std::string> replay;
135   if (replayfile == "")
136     return replay;
137
138   std::ifstream in(replayfile.c_str());
139   xbt_assert(in, "smpirun: Cannot open the replay file: %s", replayfile.c_str());
140   std::string str;
141   while (std::getline(in, str)) {
142     if (not str.empty())
143       replay.emplace_back(str);
144   }
145
146   return replay;
147 }
148
149 /** @brief Build argument vector to pass to process */
150 static std::vector<std::string> smpi_deployment_get_args(int rank_id, const std::vector<std::string>& replay,
151                                                          const std::vector<const char*>& run_args)
152 {
153   std::vector<std::string> args{std::to_string(rank_id)};
154   // pass arguments to process only if not a replay execution
155   if (replay.empty())
156     args.insert(args.end(), begin(run_args), end(run_args));
157   /* one trace per process */
158   if (replay.size() > 1)
159     args.emplace_back(replay[rank_id]);
160   return args;
161 }
162
163 /**
164  * @brief Deploy an SMPI application from a smpirun call
165  *
166  * This used to be done at smpirun script, parsing either the hostfile or the platform XML.
167  * If hostfile isn't provided, get the list of hosts from engine.
168  */
169 int smpi_deployment_smpirun(const simgrid::s4u::Engine* e, const std::string& hostfile, int np,
170                             const std::string& replayfile, int map, const std::vector<const char*>& run_args)
171 {
172   auto hosts     = smpi_get_hosts(e, hostfile);
173   auto replay    = smpi_read_replay(replayfile);
174   int hosts_size = static_cast<int>(hosts.size());
175   if (np == 0)
176     np = hosts_size;
177
178   xbt_assert(np > 0, "Invalid number of process (np must be > 0). Check your np parameter, platform or hostfile");
179
180   if (np > hosts_size) {
181     XBT_INFO("You requested to use %d ranks, but there is only %d processes in your hostfile...", np, hosts_size);
182   }
183
184   for (int i = 0; i < np; i++) {
185     simgrid::s4u::Host* host = hosts[i % hosts_size];
186     std::string rank_id      = std::to_string(i);
187     auto args                = smpi_deployment_get_args(i, replay, run_args);
188     auto actor               = simgrid::s4u::Actor::create(rank_id, host, rank_id, args);
189     /* keeping the same behavior as done in smpirun script, print mapping rank/process */
190     if (map != 0) {
191       XBT_INFO("[rank %d] -> %s", i, host->get_cname());
192     }
193     actor->set_property("instance_id", "smpirun");
194     actor->set_property("rank", rank_id);
195     if (not replay.empty())
196       actor->set_property("smpi_replay", "true");
197     /* shared trace file, set it to rank 0 */
198     if (i == 0 && replay.size() == 1)
199       actor->set_property("tracefile", replay[0]);
200   }
201   return np;
202 }