Logo AND Algorithmique Numérique Distribuée

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