Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove a dependency on MSG from SMPI
[simgrid.git] / src / smpi / internals / smpi_deployment.cpp
1 /* Copyright (c) 2004-2018. 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, int process_count, MPI_Comm comm,
20            simgrid::s4u::Barrier* finalization_barrier)
21       : name(name)
22       , size(max_no_processes)
23       , present_processes(0)
24       , comm_world(comm)
25       , finalization_barrier(finalization_barrier)
26   { }
27
28   const std::string name;
29   int size;
30   int present_processes;
31   MPI_Comm comm_world;
32   simgrid::s4u::Barrier* finalization_barrier;
33 };
34 }
35 }
36 }
37
38 using simgrid::smpi::app::Instance;
39
40 static std::map<std::string, Instance> smpi_instances;
41 extern int process_count; // How many processes have been allocated over all instances?
42
43 /** \ingroup smpi_simulation
44  * \brief Registers a running instance of a MPI program.
45  *
46  * \param name the reference name of the function.
47  * \param code the main mpi function (must have a int ..(int argc, char *argv[])) prototype
48  * \param num_processes the size of the instance we want to deploy
49  */
50 void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_processes)
51 {
52   if (code != nullptr) { // When started with smpirun, we will not execute a function
53     SIMIX_function_register(name, code);
54   }
55
56   static int already_called = 0;
57   if (not already_called) {
58     already_called = 1;
59     std::vector<simgrid::s4u::Host*> list = simgrid::s4u::Engine::get_instance()->get_all_hosts();
60     for (auto const& host : list) {
61       host->extension_set(new simgrid::smpi::Host(host));
62     }
63   }
64
65   Instance instance(std::string(name), num_processes, process_count, MPI_COMM_NULL,
66                     new simgrid::s4u::Barrier(num_processes));
67   MPI_Group group     = new simgrid::smpi::Group(instance.size);
68   instance.comm_world = new simgrid::smpi::Comm(group, nullptr);
69   MPI_Attr_put(instance.comm_world, MPI_UNIVERSE_SIZE, reinterpret_cast<void*>(instance.size));
70
71   process_count+=num_processes;
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
80   instance.present_processes++;
81   instance.comm_world->group()->set_mapping(actor, rank);
82 }
83
84 MPI_Comm* smpi_deployment_comm_world(const std::string instance_id)
85 {
86   if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
87     return nullptr;
88   }
89   Instance& instance = smpi_instances.at(instance_id);
90   return &instance.comm_world;
91 }
92
93 simgrid::s4u::Barrier* smpi_deployment_finalization_barrier(const std::string instance_id)
94 {
95   if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
96     return nullptr;
97   }
98   Instance& instance = smpi_instances.at(instance_id);
99   return instance.finalization_barrier;
100 }
101
102 void smpi_deployment_cleanup_instances(){
103   for (auto const& item : smpi_instances) {
104     Instance instance = item.second;
105     delete instance.finalization_barrier;
106     simgrid::smpi::Comm::destroy(instance.comm_world);
107   }
108   smpi_instances.clear();
109 }