Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reduce the amount of includes
[simgrid.git] / src / smpi / 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 "simgrid/msg.h" /* barrier */
8 #include "src/smpi/SmpiHost.hpp"
9 #include "src/smpi/smpi_comm.hpp"
10
11 namespace simgrid {
12 namespace smpi {
13 namespace app {
14
15 class Instance {
16 public:
17   Instance(const char* name, int max_no_processes, int process_count, MPI_Comm comm, msg_bar_t finalization_barrier)
18       : name(name)
19       , size(max_no_processes)
20       , present_processes(0)
21       , index(process_count)
22       , comm_world(comm)
23       , finalization_barrier(finalization_barrier)
24   {
25   }
26
27   const char* name;
28   int size;
29   int present_processes;
30   int index; // Badly named. This should be "no_processes_when_registering" ;)
31   MPI_Comm comm_world;
32   msg_bar_t finalization_barrier;
33 };
34 }
35 }
36 namespace s4u {
37 extern std::map<std::string, simgrid::s4u::Host*> host_list;
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   SIMIX_function_register(name, code);
58
59   static int already_called = 0;
60   if (!already_called) {
61     already_called = 1;
62     for (auto& item : simgrid::s4u::host_list) {
63       simgrid::s4u::Host* host = item.second;
64       host->extension_set(new simgrid::smpi::SmpiHost(host));
65     }
66   }
67
68   Instance instance(name, num_processes, process_count, MPI_COMM_NULL, MSG_barrier_init(num_processes));
69
70   process_count+=num_processes;
71
72   smpi_instances.insert(std::pair<std::string, Instance>(name, instance));
73 }
74
75 //get the index of the process in the process_data array
76 void smpi_deployment_register_process(const char* instance_id, int rank, int index)
77 {
78   if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
79     index_to_process_data[index]=index;
80     return;
81   }
82
83   Instance& instance = smpi_instances.at(instance_id);
84
85   if (instance.comm_world == MPI_COMM_NULL) {
86     MPI_Group group     = new simgrid::smpi::Group(instance.size);
87     instance.comm_world = new simgrid::smpi::Comm(group, nullptr);
88   }
89   instance.present_processes++;
90   index_to_process_data[index] = instance.index + rank;
91   instance.comm_world->group()->set_mapping(index, rank);
92 }
93
94 //get the index of the process in the process_data array
95 MPI_Comm* smpi_deployment_comm_world(const char* 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.comm_world;
102 }
103
104 msg_bar_t smpi_deployment_finalization_barrier(const char* instance_id)
105 {
106   if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
107     return nullptr;
108   }
109   Instance& instance = smpi_instances.at(instance_id);
110   return instance.finalization_barrier;
111 }
112
113 void smpi_deployment_cleanup_instances(){
114   for (auto& item : smpi_instances) {
115     Instance instance = item.second;
116     if (instance.comm_world != MPI_COMM_NULL)
117       delete instance.comm_world->group();
118     delete instance.comm_world;
119     MSG_barrier_destroy(instance.finalization_barrier);
120   }
121 }