Logo AND Algorithmique Numérique Distribuée

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