Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI: redesign the end of actors/ranks' lifetime
[simgrid.git] / src / smpi / internals / smpi_actor.cpp
1 /* Copyright (c) 2009-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/smpi/include/smpi_actor.hpp"
7 #include "mc/mc.h"
8 #include "smpi_comm.hpp"
9 #include "src/mc/mc_replay.hpp"
10 #include "src/simix/smx_private.hpp"
11
12 #if HAVE_PAPI
13 #include "papi.h"
14 extern std::string papi_default_config_name;
15 #endif
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
18
19 namespace simgrid {
20 namespace smpi {
21
22 ActorExt::ActorExt(s4u::ActorPtr actor) : actor_(actor)
23 {
24   mailbox_         = s4u::Mailbox::by_name("SMPI-" + std::to_string(actor_->get_pid()));
25   mailbox_small_   = s4u::Mailbox::by_name("small-" + std::to_string(actor_->get_pid()));
26   mailboxes_mutex_ = s4u::Mutex::create();
27   timer_           = xbt_os_timer_new();
28   state_           = SmpiProcessState::UNINITIALIZED;
29   info_env_        = MPI_INFO_NULL;
30   if (MC_is_active())
31     MC_ignore_heap(timer_, xbt_os_timer_size());
32
33 #if HAVE_PAPI
34   if (not simgrid::config::get_value<std::string>("smpi/papi-events").empty()) {
35     // TODO: Implement host/process/thread based counters. This implementation
36     // just always takes the values passed via "default", like this:
37     // "default:COUNTER1:COUNTER2:COUNTER3;".
38     auto it = units2papi_setup.find(papi_default_config_name);
39     if (it != units2papi_setup.end()) {
40       papi_event_set_    = it->second.event_set;
41       papi_counter_data_ = it->second.counter_data;
42       XBT_DEBUG("Setting PAPI set for process %li", actor->get_pid());
43     } else {
44       papi_event_set_ = PAPI_NULL;
45       XBT_DEBUG("No PAPI set for process %li", actor->get_pid());
46     }
47   }
48 #endif
49 }
50
51 ActorExt::~ActorExt()
52 {
53   if (comm_self_ != MPI_COMM_NULL)
54     simgrid::smpi::Comm::destroy(comm_self_);
55   if (comm_intra_ != MPI_COMM_NULL)
56     simgrid::smpi::Comm::destroy(comm_intra_);
57   xbt_os_timer_free(timer_);
58 }
59
60 /** @brief Prepares the current process for termination. */
61 void ActorExt::finalize()
62 {
63   state_ = SmpiProcessState::FINALIZED;
64   XBT_DEBUG("<%ld> Process left the game", actor_->get_pid());
65
66   smpi_deployment_unregister_process(instance_id_);
67 }
68
69 /** @brief Check if a process is finalized */
70 int ActorExt::finalized()
71 {
72   return (state_ == SmpiProcessState::FINALIZED);
73 }
74
75 /** @brief Check if a process is partially initialized already */
76 int ActorExt::initializing()
77 {
78   return (state_ == SmpiProcessState::INITIALIZING);
79 }
80
81 /** @brief Check if a process is initialized */
82 int ActorExt::initialized()
83 {
84   // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
85   // single process ... ?
86   return (state_ == SmpiProcessState::INITIALIZED);
87 }
88
89 /** @brief Mark a process as initialized (=MPI_Init called) */
90 void ActorExt::mark_as_initialized()
91 {
92   if (state_ != SmpiProcessState::FINALIZED)
93     state_ = SmpiProcessState::INITIALIZED;
94 }
95
96 void ActorExt::set_replaying(bool value)
97 {
98   if (state_ != SmpiProcessState::FINALIZED)
99     replaying_ = value;
100 }
101
102 bool ActorExt::replaying()
103 {
104   return replaying_;
105 }
106
107 s4u::ActorPtr ActorExt::get_actor()
108 {
109   return actor_;
110 }
111
112 /**
113  * @brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
114  *
115  * @see smpi_trace_set_call_location
116  */
117 smpi_trace_call_location_t* ActorExt::call_location()
118 {
119   return &trace_call_loc_;
120 }
121
122 void ActorExt::set_privatized_region(smpi_privatization_region_t region)
123 {
124   privatized_region_ = region;
125 }
126
127 smpi_privatization_region_t ActorExt::privatized_region()
128 {
129   return privatized_region_;
130 }
131
132 MPI_Comm ActorExt::comm_world()
133 {
134   return comm_world_ == nullptr ? MPI_COMM_NULL : *comm_world_;
135 }
136
137 s4u::MutexPtr ActorExt::mailboxes_mutex()
138 {
139   return mailboxes_mutex_;
140 }
141
142 #if HAVE_PAPI
143 int ActorExt::papi_event_set()
144 {
145   return papi_event_set_;
146 }
147
148 papi_counter_t& ActorExt::papi_counters()
149 {
150   return papi_counter_data_;
151 }
152 #endif
153
154 xbt_os_timer_t ActorExt::timer()
155 {
156   return timer_;
157 }
158
159 void ActorExt::simulated_start()
160 {
161   simulated_ = SIMIX_get_clock();
162 }
163
164 double ActorExt::simulated_elapsed()
165 {
166   return SIMIX_get_clock() - simulated_;
167 }
168
169 MPI_Comm ActorExt::comm_self()
170 {
171   if (comm_self_ == MPI_COMM_NULL) {
172     MPI_Group group = new Group(1);
173     comm_self_      = new Comm(group, nullptr);
174     group->set_mapping(actor_, 0);
175   }
176   return comm_self_;
177 }
178
179 MPI_Info ActorExt::info_env()
180 {
181   return info_env_;
182 }
183
184 MPI_Comm ActorExt::comm_intra()
185 {
186   return comm_intra_;
187 }
188
189 void ActorExt::set_comm_intra(MPI_Comm comm)
190 {
191   comm_intra_ = comm;
192 }
193
194 void ActorExt::set_sampling(int s)
195 {
196   sampling_ = s;
197 }
198
199 int ActorExt::sampling()
200 {
201   return sampling_;
202 }
203
204 void ActorExt::init()
205 {
206   xbt_assert(smpi_get_universe_size() != 0, "SimGrid was not initialized properly before entering MPI_Init. "
207                                             "Aborting, please check compilation process and use smpirun.");
208
209   simgrid::s4u::ActorPtr self = simgrid::s4u::Actor::self();
210   // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
211   // this up here so that I can set the privatized region before the switch.
212   ActorExt* ext = smpi_process();
213   // if we are in MPI_Init and argc handling has already been done.
214   if (ext->initialized())
215     return;
216
217   if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
218     /* Now using the segment index of this process  */
219     ext->set_privatized_region(smpi_init_global_memory_segment_process());
220     /* Done at the process's creation */
221     SMPI_switch_data_segment(self);
222   }
223
224   ext->instance_id_ = self->get_property("instance_id");
225   const int rank    = xbt_str_parse_int(self->get_property("rank"), "Cannot parse rank");
226
227   ext->state_ = SmpiProcessState::INITIALIZING;
228   smpi_deployment_register_process(ext->instance_id_, rank, self);
229
230   ext->comm_world_ = smpi_deployment_comm_world(ext->instance_id_);
231
232   // set the process attached to the mailbox
233   ext->mailbox_small_->set_receiver(ext->actor_);
234   XBT_DEBUG("<%ld> SMPI process has been initialized: %p", ext->actor_->get_pid(), ext->actor_.get());
235 }
236
237 int ActorExt::get_optind()
238 {
239   return optind_;
240 }
241
242 void ActorExt::set_optind(int new_optind)
243 {
244   optind_ = new_optind;
245 }
246
247 } // namespace smpi
248 } // namespace simgrid