Logo AND Algorithmique Numérique Distribuée

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