Logo AND Algorithmique Numérique Distribuée

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