Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Actor: make the refcount observable, and improve debug messages
[simgrid.git] / src / smpi / internals / smpi_deployment.cpp
1 /* Copyright (c) 2004-2019. 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_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(const std::string& name, int max_no_processes, MPI_Comm comm)
24       : name_(name), size_(max_no_processes), comm_world_(comm)
25   {
26     MPI_Group group = new simgrid::smpi::Group(size_);
27     comm_world_     = new simgrid::smpi::Comm(group, nullptr, 0, -1);
28     //  FIXME : using MPI_Attr_put with MPI_UNIVERSE_SIZE is forbidden and we make it a no-op (which triggers a warning
29     //  as MPI_ERR_ARG is returned). Directly calling Comm::attr_put breaks for now, as MPI_UNIVERSE_SIZE,is <0
30     //  instance.comm_world->attr_put<simgrid::smpi::Comm>(MPI_UNIVERSE_SIZE, reinterpret_cast<void*>(instance.size));
31
32     universe_size += max_no_processes;
33   }
34
35   const std::string name_;
36   unsigned int size_;
37   std::vector<simgrid::s4u::ActorPtr> present_processes_;
38   unsigned int finalized_ranks_ = 0;
39   MPI_Comm comm_world_;
40 };
41 }
42 }
43 }
44
45 using simgrid::smpi::app::Instance;
46
47 static std::map<std::string, Instance> smpi_instances;
48
49 /** @ingroup smpi_simulation
50  * @brief Registers a running instance of a MPI program.
51  *
52  * @param name the reference name of the function.
53  * @param code either the main mpi function
54  *             (must have a int ..(int argc, char *argv[]) prototype) or nullptr
55  *             (if the function deployment is managed somewhere else —
56  *              e.g., when deploying manually or using smpirun)
57  * @param num_processes the size of the instance we want to deploy
58  */
59 void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_processes)
60 {
61   if (code != nullptr) // When started with smpirun, we will not execute a function
62     simgrid::s4u::Engine::get_instance()->register_function(name, code);
63
64   static bool already_called = false;
65   if (not already_called) {
66     already_called = true;
67     for (auto const& host : simgrid::s4u::Engine::get_instance()->get_all_hosts())
68       host->extension_set(new simgrid::smpi::Host(host));
69   }
70
71   Instance instance(std::string(name), num_processes, MPI_COMM_NULL);
72
73   smpi_instances.insert(std::pair<std::string, Instance>(name, instance));
74 }
75
76 void smpi_deployment_register_process(const std::string& instance_id, int rank, simgrid::s4u::ActorPtr actor)
77 {
78   Instance& instance = smpi_instances.at(instance_id);
79   instance.present_processes_.push_back(actor);
80   instance.comm_world_->group()->set_mapping(actor, rank);
81 }
82
83 void smpi_deployment_unregister_process(const std::string& instance_id)
84 {
85   Instance& instance = smpi_instances.at(instance_id);
86   instance.finalized_ranks_++;
87
88   if (instance.finalized_ranks_ == instance.size_) {
89     instance.present_processes_.clear();
90     simgrid::smpi::Comm::destroy(instance.comm_world_);
91     smpi_instances.erase(instance_id);
92   }
93 }
94
95 MPI_Comm* smpi_deployment_comm_world(const std::string& instance_id)
96 {
97   if (smpi_instances
98           .empty()) { // no instance registered, we probably used smpirun. (FIXME: I guess this never happens for real)
99     return nullptr;
100   }
101   Instance& instance = smpi_instances.at(instance_id);
102   return &instance.comm_world_;
103 }
104
105 void smpi_deployment_cleanup_instances(){
106   for (auto const& item : smpi_instances) {
107     XBT_CINFO(smpi, "Stalling SMPI instance: %s. Do all your MPI ranks call MPI_Finalize()?", item.first.c_str());
108     Instance instance = item.second;
109     instance.present_processes_.clear();
110     simgrid::smpi::Comm::destroy(instance.comm_world_);
111   }
112   smpi_instances.clear();
113 }
114
115 int smpi_get_universe_size()
116 {
117   return simgrid::smpi::app::universe_size;
118 }