Logo AND Algorithmique Numérique Distribuée

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