Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d470ebdb34040571882f9c7b0c6faaf5b4e7150e
[simgrid.git] / src / smpi / internals / smpi_deployment.cpp
1 /* Copyright (c) 2004-2017. 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 "SmpiHost.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 char* name, int max_no_processes, int process_count, MPI_Comm comm, msg_bar_t finalization_barrier)
21       : name(name)
22       , size(max_no_processes)
23       , present_processes(0)
24       , index(process_count)
25       , comm_world(comm)
26       , finalization_barrier(finalization_barrier)
27   {
28   }
29
30   const char* name;
31   int size;
32   int present_processes;
33   int index; // Badly named. This should be "no_processes_when_registering" ;)
34   MPI_Comm comm_world;
35   msg_bar_t finalization_barrier;
36 };
37 }
38 }
39 }
40
41 using simgrid::smpi::app::Instance;
42
43 static std::map<std::string, Instance> smpi_instances;
44 extern int process_count; // How many processes have been allocated over all instances?
45 extern int* index_to_process_data;
46
47 /** \ingroup smpi_simulation
48  * \brief Registers a running instance of a MPI program.
49  *
50  * FIXME : remove MSG from the loop at some point.
51  * \param name the reference name of the function.
52  * \param code the main mpi function (must have a int ..(int argc, char *argv[])) prototype
53  * \param num_processes the size of the instance we want to deploy
54  */
55 void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_processes)
56 {
57   if (code != nullptr) { // When started with smpirun, we will not execute a function
58     SIMIX_function_register(name, code);
59   }
60
61   static int already_called = 0;
62   if (not already_called) {
63     already_called = 1;
64     std::vector<simgrid::s4u::Host*> list;
65     simgrid::s4u::Engine::getInstance()->getHostList(&list);
66     for (auto const& host : list) {
67       host->extension_set(new simgrid::smpi::SmpiHost(host));
68     }
69   }
70
71   Instance instance(name, num_processes, process_count, MPI_COMM_NULL, MSG_barrier_init(num_processes));
72   MPI_Group group     = new simgrid::smpi::Group(instance.size);
73   instance.comm_world = new simgrid::smpi::Comm(group, nullptr);
74   MPI_Attr_put(instance.comm_world, MPI_UNIVERSE_SIZE, reinterpret_cast<void*>(instance.size));
75
76   process_count+=num_processes;
77
78   smpi_instances.insert(std::pair<std::string, Instance>(name, instance));
79 }
80
81 //get the index of the process in the process_data array
82 void smpi_deployment_register_process(const char* instance_id, int rank, int index)
83 {
84   if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
85     index_to_process_data[index]=index;
86     return;
87   }
88
89   Instance& instance = smpi_instances.at(instance_id);
90
91   instance.present_processes++;
92   index_to_process_data[index] = instance.index + rank;
93   instance.comm_world->group()->set_mapping(index, rank);
94 }
95
96 //get the index of the process in the process_data array
97 MPI_Comm* smpi_deployment_comm_world(const char* instance_id)
98 {
99   if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
100     return nullptr;
101   }
102   Instance& instance = smpi_instances.at(instance_id);
103   return &instance.comm_world;
104 }
105
106 msg_bar_t smpi_deployment_finalization_barrier(const char* instance_id)
107 {
108   if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
109     return nullptr;
110   }
111   Instance& instance = smpi_instances.at(instance_id);
112   return instance.finalization_barrier;
113 }
114
115 void smpi_deployment_cleanup_instances(){
116   for (auto const& item : smpi_instances) {
117     Instance instance = item.second;
118     MSG_barrier_destroy(instance.finalization_barrier);
119     simgrid::smpi::Comm::destroy(instance.comm_world);
120   }
121   smpi_instances.clear();
122 }