Logo AND Algorithmique Numérique Distribuée

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