Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
50950970dc4e7623d2adcdf18492c09c4fd9bedb
[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 using simgrid::s4u::Actor;
23 using simgrid::s4u::ActorPtr;
24
25 ActorExt::ActorExt(ActorPtr actor, simgrid::s4u::Barrier* finalization_barrier)
26     : finalization_barrier_(finalization_barrier), actor_(actor)
27 {
28   mailbox_         = simgrid::s4u::Mailbox::by_name("SMPI-" + std::to_string(actor_->get_pid()));
29   mailbox_small_   = simgrid::s4u::Mailbox::by_name("small-" + std::to_string(actor_->get_pid()));
30   mailboxes_mutex_ = xbt_mutex_init();
31   timer_           = xbt_os_timer_new();
32   state_           = SmpiProcessState::UNINITIALIZED;
33   if (MC_is_active())
34     MC_ignore_heap(timer_, xbt_os_timer_size());
35
36 #if HAVE_PAPI
37   if (not simgrid::config::get_value<std::string>("smpi/papi-events").empty()) {
38     // TODO: Implement host/process/thread based counters. This implementation
39     // just always takes the values passed via "default", like this:
40     // "default:COUNTER1:COUNTER2:COUNTER3;".
41     auto it = units2papi_setup.find(papi_default_config_name);
42     if (it != units2papi_setup.end()) {
43       papi_event_set_    = it->second.event_set;
44       papi_counter_data_ = it->second.counter_data;
45       XBT_DEBUG("Setting PAPI set for process %li", actor->get_pid());
46     } else {
47       papi_event_set_ = PAPI_NULL;
48       XBT_DEBUG("No PAPI set for process %li", actor->get_pid());
49     }
50   }
51 #endif
52 }
53
54 ActorExt::~ActorExt()
55 {
56   if (comm_self_ != MPI_COMM_NULL)
57     simgrid::smpi::Comm::destroy(comm_self_);
58   if (comm_intra_ != MPI_COMM_NULL)
59     simgrid::smpi::Comm::destroy(comm_intra_);
60   xbt_os_timer_free(timer_);
61   xbt_mutex_destroy(mailboxes_mutex_);
62 }
63
64 void ActorExt::set_data(const char* instance_id)
65 {
66   instance_id_                   = std::string(instance_id);
67   comm_world_                    = smpi_deployment_comm_world(instance_id_);
68   simgrid::s4u::Barrier* barrier = smpi_deployment_finalization_barrier(instance_id_);
69   if (barrier != nullptr) // don't overwrite the current one if the instance has none
70     finalization_barrier_ = barrier;
71
72   // set the process attached to the mailbox
73   mailbox_small_->set_receiver(actor_);
74   XBT_DEBUG("<%ld> SMPI process has been initialized: %p", actor_->get_pid(), actor_.get());
75 }
76
77 /** @brief Prepares the current process for termination. */
78 void ActorExt::finalize()
79 {
80   state_ = SmpiProcessState::FINALIZED;
81   XBT_DEBUG("<%ld> Process left the game", actor_->get_pid());
82
83   // This leads to an explosion of the search graph which cannot be reduced:
84   if (MC_is_active() || MC_record_replay_is_active())
85     return;
86   // wait for all pending asynchronous comms to finish
87   finalization_barrier_->wait();
88 }
89
90 /** @brief Check if a process is finalized */
91 int ActorExt::finalized()
92 {
93   return (state_ == SmpiProcessState::FINALIZED);
94 }
95
96 /** @brief Check if a process is partially initialized already */
97 int ActorExt::initializing()
98 {
99   return (state_ == SmpiProcessState::INITIALIZING);
100 }
101
102 /** @brief Check if a process is initialized */
103 int ActorExt::initialized()
104 {
105   // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
106   // single process ... ?
107   return (state_ == SmpiProcessState::INITIALIZED);
108 }
109
110 /** @brief Mark a process as initialized (=MPI_Init called) */
111 void ActorExt::mark_as_initialized()
112 {
113   if (state_ != SmpiProcessState::FINALIZED)
114     state_ = SmpiProcessState::INITIALIZED;
115 }
116
117 void ActorExt::set_replaying(bool value)
118 {
119   if (state_ != SmpiProcessState::FINALIZED)
120     replaying_ = value;
121 }
122
123 bool ActorExt::replaying()
124 {
125   return replaying_;
126 }
127
128 ActorPtr ActorExt::get_actor()
129 {
130   return actor_;
131 }
132
133 /**
134  * @brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
135  *
136  * @see smpi_trace_set_call_location
137  */
138 smpi_trace_call_location_t* ActorExt::call_location()
139 {
140   return &trace_call_loc_;
141 }
142
143 void ActorExt::set_privatized_region(smpi_privatization_region_t region)
144 {
145   privatized_region_ = region;
146 }
147
148 smpi_privatization_region_t ActorExt::privatized_region()
149 {
150   return privatized_region_;
151 }
152
153 MPI_Comm ActorExt::comm_world()
154 {
155   return comm_world_ == nullptr ? MPI_COMM_NULL : *comm_world_;
156 }
157
158 smx_mailbox_t ActorExt::mailbox()
159 {
160   return mailbox_->get_impl();
161 }
162
163 smx_mailbox_t ActorExt::mailbox_small()
164 {
165   return mailbox_small_->get_impl();
166 }
167
168 xbt_mutex_t ActorExt::mailboxes_mutex()
169 {
170   return mailboxes_mutex_;
171 }
172
173 #if HAVE_PAPI
174 int ActorExt::papi_event_set()
175 {
176   return papi_event_set_;
177 }
178
179 papi_counter_t& ActorExt::papi_counters()
180 {
181   return papi_counter_data_;
182 }
183 #endif
184
185 xbt_os_timer_t ActorExt::timer()
186 {
187   return timer_;
188 }
189
190 void ActorExt::simulated_start()
191 {
192   simulated_ = SIMIX_get_clock();
193 }
194
195 double ActorExt::simulated_elapsed()
196 {
197   return SIMIX_get_clock() - simulated_;
198 }
199
200 MPI_Comm ActorExt::comm_self()
201 {
202   if (comm_self_ == MPI_COMM_NULL) {
203     MPI_Group group = new Group(1);
204     comm_self_      = new Comm(group, nullptr);
205     group->set_mapping(actor_, 0);
206   }
207   return comm_self_;
208 }
209
210 MPI_Comm ActorExt::comm_intra()
211 {
212   return comm_intra_;
213 }
214
215 void ActorExt::set_comm_intra(MPI_Comm comm)
216 {
217   comm_intra_ = comm;
218 }
219
220 void ActorExt::set_sampling(int s)
221 {
222   sampling_ = s;
223 }
224
225 int ActorExt::sampling()
226 {
227   return sampling_;
228 }
229
230 void ActorExt::init()
231 {
232   if (smpi_process_count() == 0) {
233     xbt_die("SimGrid was not initialized properly before entering MPI_Init. Aborting, please check compilation process "
234             "and use smpirun\n");
235   }
236
237   simgrid::s4u::ActorPtr proc = simgrid::s4u::Actor::self();
238   proc->get_impl()->context_->set_cleanup(&SIMIX_process_cleanup);
239   // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
240   // this up here so that I can set the privatized region before the switch.
241   ActorExt* process = smpi_process_remote(proc);
242   // if we are in MPI_Init and argc handling has already been done.
243   if (process->initialized())
244     return;
245
246   if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
247     /* Now using the segment index of this process  */
248     process->set_privatized_region(smpi_init_global_memory_segment_process());
249     /* Done at the process's creation */
250     SMPI_switch_data_segment(proc);
251   }
252
253   const char* instance_id = simgrid::s4u::Actor::self()->get_property("instance_id");
254   const int rank          = xbt_str_parse_int(simgrid::s4u::Actor::self()->get_property("rank"), "Cannot parse rank");
255
256   process->state_ = SmpiProcessState::INITIALIZING;
257   smpi_deployment_register_process(instance_id, rank, proc);
258
259   process->set_data(instance_id);
260 }
261
262 int ActorExt::get_optind()
263 {
264   return optind;
265 }
266 void ActorExt::set_optind(int new_optind)
267 {
268   optind = new_optind;
269 }
270
271 } // namespace smpi
272 } // namespace simgrid