Logo AND Algorithmique Numérique Distribuée

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