Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill smpi_process_count(), use smpi_get_universe_size() instead
[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 namespace simgrid {
14 namespace smpi {
15 namespace app {
16
17 static int universe_size = 0;
18
19 class Instance {
20 public:
21   Instance(const std::string& name, int max_no_processes, MPI_Comm comm, simgrid::s4u::Barrier* finalization_barrier)
22       : name(name)
23       , size(max_no_processes)
24       , present_processes(0)
25       , comm_world(comm)
26       , finalization_barrier(finalization_barrier)
27   {
28     MPI_Group group = new simgrid::smpi::Group(size);
29     comm_world      = new simgrid::smpi::Comm(group, nullptr, 0, -1);
30     //  FIXME : using MPI_Attr_put with MPI_UNIVERSE_SIZE is forbidden and we make it a no-op (which triggers a warning
31     //  as MPI_ERR_ARG is returned). Directly calling Comm::attr_put breaks for now, as MPI_UNIVERSE_SIZE,is <0
32     //  instance.comm_world->attr_put<simgrid::smpi::Comm>(MPI_UNIVERSE_SIZE, reinterpret_cast<void*>(instance.size));
33
34     universe_size += max_no_processes;
35   }
36
37   const std::string name;
38   int size;
39   int present_processes;
40   MPI_Comm comm_world;
41   simgrid::s4u::Barrier* finalization_barrier;
42 };
43 }
44 }
45 }
46
47 using simgrid::smpi::app::Instance;
48
49 static std::map<std::string, Instance> smpi_instances;
50
51 /** @ingroup smpi_simulation
52  * @brief Registers a running instance of a MPI program.
53  *
54  * @param name the reference name of the function.
55  * @param code either the main mpi function
56  *             (must have a int ..(int argc, char *argv[]) prototype) or nullptr
57  *             (if the function deployment is managed somewhere else —
58  *              e.g., when deploying manually or using smpirun)
59  * @param num_processes the size of the instance we want to deploy
60  */
61 void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_processes)
62 {
63   if (code != nullptr) // When started with smpirun, we will not execute a function
64     simgrid::s4u::Engine::get_instance()->register_function(name, code);
65
66   static bool already_called = false;
67   if (not already_called) {
68     already_called = true;
69     for (auto const& host : simgrid::s4u::Engine::get_instance()->get_all_hosts())
70       host->extension_set(new simgrid::smpi::Host(host));
71   }
72
73   Instance instance(std::string(name), num_processes, MPI_COMM_NULL, new simgrid::s4u::Barrier(num_processes));
74
75   smpi_instances.insert(std::pair<std::string, Instance>(name, instance));
76 }
77
78 void smpi_deployment_register_process(const std::string& instance_id, int rank, simgrid::s4u::ActorPtr actor)
79 {
80   Instance& instance = smpi_instances.at(instance_id);
81
82   instance.present_processes++;
83   instance.comm_world->group()->set_mapping(actor, rank);
84 }
85
86 MPI_Comm* smpi_deployment_comm_world(const std::string& instance_id)
87 {
88   if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
89     return nullptr;
90   }
91   Instance& instance = smpi_instances.at(instance_id);
92   return &instance.comm_world;
93 }
94
95 simgrid::s4u::Barrier* smpi_deployment_finalization_barrier(const std::string& instance_id)
96 {
97   if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
98     return nullptr;
99   }
100   Instance& instance = smpi_instances.at(instance_id);
101   return instance.finalization_barrier;
102 }
103
104 void smpi_deployment_cleanup_instances(){
105   for (auto const& item : smpi_instances) {
106     Instance instance = item.second;
107     delete instance.finalization_barrier;
108     simgrid::smpi::Comm::destroy(instance.comm_world);
109   }
110   smpi_instances.clear();
111 }
112
113 int smpi_get_universe_size()
114 {
115   return simgrid::smpi::app::universe_size;
116 }