Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
30327d64bbed248bf30a7ec8b1b9a111d207b4c2
[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 xbt_mutex_t ActorExt::mailboxes_mutex()
159 {
160   return mailboxes_mutex_;
161 }
162
163 #if HAVE_PAPI
164 int ActorExt::papi_event_set()
165 {
166   return papi_event_set_;
167 }
168
169 papi_counter_t& ActorExt::papi_counters()
170 {
171   return papi_counter_data_;
172 }
173 #endif
174
175 xbt_os_timer_t ActorExt::timer()
176 {
177   return timer_;
178 }
179
180 void ActorExt::simulated_start()
181 {
182   simulated_ = SIMIX_get_clock();
183 }
184
185 double ActorExt::simulated_elapsed()
186 {
187   return SIMIX_get_clock() - simulated_;
188 }
189
190 MPI_Comm ActorExt::comm_self()
191 {
192   if (comm_self_ == MPI_COMM_NULL) {
193     MPI_Group group = new Group(1);
194     comm_self_      = new Comm(group, nullptr);
195     group->set_mapping(actor_, 0);
196   }
197   return comm_self_;
198 }
199
200 MPI_Comm ActorExt::comm_intra()
201 {
202   return comm_intra_;
203 }
204
205 void ActorExt::set_comm_intra(MPI_Comm comm)
206 {
207   comm_intra_ = comm;
208 }
209
210 void ActorExt::set_sampling(int s)
211 {
212   sampling_ = s;
213 }
214
215 int ActorExt::sampling()
216 {
217   return sampling_;
218 }
219
220 void ActorExt::init()
221 {
222   if (smpi_process_count() == 0) {
223     xbt_die("SimGrid was not initialized properly before entering MPI_Init. Aborting, please check compilation process "
224             "and use smpirun\n");
225   }
226
227   simgrid::s4u::ActorPtr proc = simgrid::s4u::Actor::self();
228   // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
229   // this up here so that I can set the privatized region before the switch.
230   ActorExt* process = smpi_process_remote(proc);
231   // if we are in MPI_Init and argc handling has already been done.
232   if (process->initialized())
233     return;
234
235   if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
236     /* Now using the segment index of this process  */
237     process->set_privatized_region(smpi_init_global_memory_segment_process());
238     /* Done at the process's creation */
239     SMPI_switch_data_segment(proc);
240   }
241
242   const char* instance_id = simgrid::s4u::Actor::self()->get_property("instance_id");
243   const int rank          = xbt_str_parse_int(simgrid::s4u::Actor::self()->get_property("rank"), "Cannot parse rank");
244
245   process->state_ = SmpiProcessState::INITIALIZING;
246   smpi_deployment_register_process(instance_id, rank, proc);
247
248   process->set_data(instance_id);
249 }
250
251 int ActorExt::get_optind()
252 {
253   return optind;
254 }
255 void ActorExt::set_optind(int new_optind)
256 {
257   optind = new_optind;
258 }
259
260 } // namespace smpi
261 } // namespace simgrid