Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
intercept getpid call in SMPI, to provide internal PID instead of same of for all...
[simgrid.git] / src / smpi / internals / smpi_actor.cpp
1 /* Copyright (c) 2009-2022. 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 "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Mutex.hpp"
10 #include "smpi_comm.hpp"
11 #include "smpi_info.hpp"
12 #include "src/mc/mc_replay.hpp"
13 #include "xbt/str.h"
14
15 #if HAVE_PAPI
16 #include "papi.h"
17 #endif
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
20
21 namespace simgrid::smpi {
22 simgrid::xbt::Extension<simgrid::s4u::Actor, ActorExt> ActorExt::EXTENSION_ID;
23
24 ActorExt::ActorExt(s4u::Actor* actor) : actor_(actor)
25 {
26   if (not simgrid::smpi::ActorExt::EXTENSION_ID.valid())
27     simgrid::smpi::ActorExt::EXTENSION_ID = simgrid::s4u::Actor::extension_create<simgrid::smpi::ActorExt>();
28
29   mailbox_         = s4u::Mailbox::by_name("SMPI-" + std::to_string(actor_->get_pid()));
30   mailbox_small_   = s4u::Mailbox::by_name("small-" + std::to_string(actor_->get_pid()));
31   mailboxes_mutex_ = s4u::Mutex::create();
32   timer_           = xbt_os_timer_new();
33   state_           = SmpiProcessState::UNINITIALIZED;
34   info_env_        = MPI_INFO_NULL;
35   if (MC_is_active())
36     MC_ignore_heap(timer_, xbt_os_timer_size());
37
38 #if HAVE_PAPI
39   if (not smpi_cfg_papi_events_file().empty()) {
40     // TODO: Implement host/process/thread based counters. This implementation
41     // just always takes the values passed via "default", like this:
42     // "default:COUNTER1:COUNTER2:COUNTER3;".
43     auto it = units2papi_setup.find("default");
44     if (it != units2papi_setup.end()) {
45       papi_event_set_    = it->second.event_set;
46       papi_counter_data_ = it->second.counter_data;
47       XBT_DEBUG("Setting PAPI set for process %li", actor->get_pid());
48     } else {
49       papi_event_set_ = PAPI_NULL;
50       XBT_DEBUG("No PAPI set for process %li", actor->get_pid());
51     }
52   }
53 #endif
54 }
55
56 ActorExt::~ActorExt()
57 {
58   xbt_os_timer_free(timer_);
59 }
60
61 /** @brief Prepares the current process for termination. */
62 void ActorExt::finalize()
63 {
64   state_ = SmpiProcessState::FINALIZED;
65   XBT_DEBUG("<%ld> Process left the game", actor_->get_pid());
66   if (info_env_ != MPI_INFO_NULL)
67     simgrid::smpi::Info::unref(info_env_);
68   if (comm_self_ != MPI_COMM_NULL)
69     simgrid::smpi::Comm::destroy(comm_self_);
70   if (comm_intra_ != MPI_COMM_NULL)
71     simgrid::smpi::Comm::destroy(comm_intra_);
72   smpi_deployment_unregister_process(instance_id_);
73 }
74
75 /** @brief Check if a process is finalized */
76 int ActorExt::finalized() const
77 {
78   return (state_ == SmpiProcessState::FINALIZED);
79 }
80
81 /** @brief Check if a process is partially initialized already */
82 int ActorExt::initializing() const
83 {
84   return (state_ == SmpiProcessState::INITIALIZING);
85 }
86
87 /** @brief Check if a process is initialized */
88 int ActorExt::initialized() const
89 {
90   // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
91   // single process ... ?
92   return (state_ == SmpiProcessState::INITIALIZED);
93 }
94
95 /** @brief Mark a process as initialized (=MPI_Init called) */
96 void ActorExt::mark_as_initialized()
97 {
98   if (state_ != SmpiProcessState::FINALIZED)
99     state_ = SmpiProcessState::INITIALIZED;
100 }
101
102 /** @brief Mark a process as finalizing (=MPI_Finalize called) */
103 void ActorExt::mark_as_finalizing()
104 {
105   if (state_ != SmpiProcessState::FINALIZED)
106     state_ = SmpiProcessState::FINALIZING;
107 }
108
109 /** @brief Check if a process is finalizing */
110 int ActorExt::finalizing() const
111 {
112   return (state_ == SmpiProcessState::FINALIZING);
113 }
114
115 void ActorExt::set_replaying(bool value)
116 {
117   if (state_ != SmpiProcessState::FINALIZED)
118     replaying_ = value;
119 }
120
121 bool ActorExt::replaying() const
122 {
123   return replaying_;
124 }
125
126 s4u::ActorPtr ActorExt::get_actor()
127 {
128   return actor_;
129 }
130
131 /**
132  * @brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
133  *
134  * @see smpi_trace_set_call_location
135  */
136 smpi_trace_call_location_t* ActorExt::call_location()
137 {
138   return &trace_call_loc_;
139 }
140
141 void ActorExt::set_privatized_region(smpi_privatization_region_t region)
142 {
143   privatized_region_ = region;
144 }
145
146 smpi_privatization_region_t ActorExt::privatized_region() const
147 {
148   return privatized_region_;
149 }
150
151 MPI_Comm ActorExt::comm_world() const
152 {
153   return comm_world_ == nullptr ? MPI_COMM_NULL : *comm_world_;
154 }
155
156 s4u::MutexPtr ActorExt::mailboxes_mutex() const
157 {
158   return mailboxes_mutex_;
159 }
160
161 #if HAVE_PAPI
162 int ActorExt::papi_event_set() const
163 {
164   return papi_event_set_;
165 }
166
167 papi_counter_t& ActorExt::papi_counters()
168 {
169   return papi_counter_data_;
170 }
171 #endif
172
173 xbt_os_timer_t ActorExt::timer()
174 {
175   return timer_;
176 }
177
178 void ActorExt::simulated_start()
179 {
180   simulated_ = s4u::Engine::get_clock();
181 }
182
183 double ActorExt::simulated_elapsed() const
184 {
185   return s4u::Engine::get_clock() - simulated_;
186 }
187
188 MPI_Comm ActorExt::comm_self()
189 {
190   if (comm_self_ == MPI_COMM_NULL) {
191     auto* group = new Group(1);
192     comm_self_  = new Comm(group, nullptr);
193     comm_self_->set_name("MPI_COMM_SELF");
194     group->set_mapping(actor_->get_pid(), 0);
195   }
196   return comm_self_;
197 }
198
199 MPI_Info ActorExt::info_env()
200 {
201   if (info_env_==MPI_INFO_NULL)
202     info_env_=new Info();
203   return info_env_;
204 }
205
206 MPI_Comm ActorExt::comm_intra()
207 {
208   return comm_intra_;
209 }
210
211 void ActorExt::set_comm_intra(MPI_Comm comm)
212 {
213   comm_intra_ = comm;
214 }
215
216 void ActorExt::set_sampling(int s)
217 {
218   sampling_ = s;
219 }
220
221 int ActorExt::sampling() const
222 {
223   return sampling_;
224 }
225
226 void ActorExt::init()
227 {
228   xbt_assert(smpi_get_universe_size() != 0, "SimGrid was not initialized properly before entering MPI_Init. "
229                                             "Aborting, please check compilation process and use smpirun.");
230
231   ActorExt* ext = smpi_process();
232   // if we are in MPI_Init and argc handling has already been done.
233   if (ext->initialized())
234     return;
235
236   const simgrid::s4u::Actor* self = simgrid::s4u::Actor::self();
237   ext->instance_id_ = self->get_property("instance_id");
238   const int rank = static_cast<int>(xbt_str_parse_int(self->get_property("rank"), "Cannot parse rank"));
239
240   ext->state_ = SmpiProcessState::INITIALIZING;
241   smpi_deployment_register_process(ext->instance_id_, rank, self);
242
243   ext->comm_world_ = smpi_deployment_comm_world(ext->instance_id_);
244
245   // set the process attached to the mailbox
246   ext->mailbox_small_->set_receiver(ext->actor_);
247   XBT_DEBUG("<%ld> SMPI process has been initialized: %p", ext->actor_->get_pid(), ext->actor_);
248 }
249
250 int ActorExt::get_optind() const
251 {
252   return optind_;
253 }
254
255 void ActorExt::set_optind(int new_optind)
256 {
257   optind_ = new_optind;
258 }
259
260 void ActorExt::bsend_buffer(void** buf, int* size)
261 {
262   *buf  = bsend_buffer_;
263   *size = bsend_buffer_size_;
264 }
265
266 int ActorExt::set_bsend_buffer(void* buf, int size)
267 {
268   if(buf!=nullptr && bsend_buffer_!=nullptr)
269     return MPI_ERR_BUFFER;
270   bsend_buffer_     = buf;
271   bsend_buffer_size_= size;
272   return MPI_SUCCESS;
273 }
274
275 } // namespace simgrid::smpi