Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f783e8cd6647502115d9cc4c1e4576b75ee008ca
[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/msg.h" /* barrier */
10 #include "simgrid/s4u/Engine.hpp"
11 #include "smpi_comm.hpp"
12 #include <map>
13
14 namespace simgrid {
15 namespace smpi {
16 namespace app {
17
18 class Instance {
19 public:
20   Instance(const std::string name, int max_no_processes, int process_count, MPI_Comm comm,
21            msg_bar_t 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
29   const std::string name;
30   int size;
31   int present_processes;
32   MPI_Comm comm_world;
33   msg_bar_t finalization_barrier;
34 };
35 }
36 }
37 }
38
39 using simgrid::smpi::app::Instance;
40
41 static std::map<std::string, Instance> smpi_instances;
42 extern int process_count; // How many processes have been allocated over all instances?
43
44 /** \ingroup smpi_simulation
45  * \brief Registers a running instance of a MPI program.
46  *
47  * FIXME : remove MSG from the loop at some point.
48  * \param name the reference name of the function.
49  * \param code the main mpi function (must have a int ..(int argc, char *argv[])) prototype
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     SIMIX_function_register(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, process_count, MPI_COMM_NULL, MSG_barrier_init(num_processes));
68   MPI_Group group     = new simgrid::smpi::Group(instance.size);
69   instance.comm_world = new simgrid::smpi::Comm(group, nullptr);
70   MPI_Attr_put(instance.comm_world, MPI_UNIVERSE_SIZE, reinterpret_cast<void*>(instance.size));
71
72   process_count+=num_processes;
73
74   smpi_instances.insert(std::pair<std::string, Instance>(name, instance));
75 }
76
77 void smpi_deployment_register_process(const std::string instance_id, int rank, simgrid::s4u::ActorPtr actor)
78 {
79   Instance& instance = smpi_instances.at(instance_id);
80
81   instance.present_processes++;
82   instance.comm_world->group()->set_mapping(actor, rank);
83 }
84
85 MPI_Comm* smpi_deployment_comm_world(const std::string instance_id)
86 {
87   if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
88     return nullptr;
89   }
90   Instance& instance = smpi_instances.at(instance_id);
91   return &instance.comm_world;
92 }
93
94 msg_bar_t smpi_deployment_finalization_barrier(const std::string instance_id)
95 {
96   if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
97     return nullptr;
98   }
99   Instance& instance = smpi_instances.at(instance_id);
100   return instance.finalization_barrier;
101 }
102
103 void smpi_deployment_cleanup_instances(){
104   for (auto const& item : smpi_instances) {
105     Instance instance = item.second;
106     MSG_barrier_destroy(instance.finalization_barrier);
107     simgrid::smpi::Comm::destroy(instance.comm_world);
108   }
109   smpi_instances.clear();
110 }