Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[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
36 #if HAVE_PAPI
37   if (not smpi_cfg_papi_events_file().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("default");
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   xbt_os_timer_free(timer_);
57 }
58
59 /** @brief Prepares the current process for termination. */
60 void ActorExt::finalize()
61 {
62   state_ = SmpiProcessState::FINALIZED;
63   XBT_DEBUG("<%ld> Process left the game", actor_->get_pid());
64   if (info_env_ != MPI_INFO_NULL)
65     simgrid::smpi::Info::unref(info_env_);
66   if (comm_self_ != MPI_COMM_NULL)
67     simgrid::smpi::Comm::destroy(comm_self_);
68   if (comm_intra_ != MPI_COMM_NULL)
69     simgrid::smpi::Comm::destroy(comm_intra_);
70   smpi_deployment_unregister_process(instance_id_);
71 }
72
73 /** @brief Check if a process is finalized */
74 int ActorExt::finalized() const
75 {
76   return (state_ == SmpiProcessState::FINALIZED);
77 }
78
79 /** @brief Check if a process is partially initialized already */
80 int ActorExt::initializing() const
81 {
82   return (state_ == SmpiProcessState::INITIALIZING);
83 }
84
85 /** @brief Check if a process is initialized */
86 int ActorExt::initialized() const
87 {
88   // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
89   // single process ... ?
90   return (state_ == SmpiProcessState::INITIALIZED);
91 }
92
93 /** @brief Return main mailbox of the process */
94 s4u::Mailbox* ActorExt::mailbox()
95 {
96   if(mailbox_==nullptr)
97     mailbox_=s4u::Mailbox::by_name("SMPI-" + std::to_string(actor_->get_pid()));
98   return mailbox_;
99 }
100
101 /** @brief Return mailbox for small messages */
102 s4u::Mailbox* ActorExt::mailbox_small()
103 {
104   if(mailbox_small_==nullptr)
105     mailbox_small_=s4u::Mailbox::by_name("small-" + std::to_string(actor_->get_pid()));
106   return mailbox_small_;
107 }
108
109 /** @brief Mark a process as initialized (=MPI_Init called) */
110 void ActorExt::mark_as_initialized()
111 {
112   if (state_ != SmpiProcessState::FINALIZED)
113     state_ = SmpiProcessState::INITIALIZED;
114 }
115
116 /** @brief Mark a process as finalizing (=MPI_Finalize called) */
117 void ActorExt::mark_as_finalizing()
118 {
119   if (state_ != SmpiProcessState::FINALIZED)
120     state_ = SmpiProcessState::FINALIZING;
121 }
122
123 /** @brief Check if a process is finalizing */
124 int ActorExt::finalizing() const
125 {
126   return (state_ == SmpiProcessState::FINALIZING);
127 }
128
129 void ActorExt::set_replaying(bool value)
130 {
131   if (state_ != SmpiProcessState::FINALIZED)
132     replaying_ = value;
133 }
134
135 bool ActorExt::replaying() const
136 {
137   return replaying_;
138 }
139
140 s4u::ActorPtr ActorExt::get_actor()
141 {
142   return actor_;
143 }
144
145 /**
146  * @brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
147  *
148  * @see smpi_trace_set_call_location
149  */
150 smpi_trace_call_location_t* ActorExt::call_location()
151 {
152   return &trace_call_loc_;
153 }
154
155 void ActorExt::set_privatized_region(smpi_privatization_region_t region)
156 {
157   privatized_region_ = region;
158 }
159
160 smpi_privatization_region_t ActorExt::privatized_region() const
161 {
162   return privatized_region_;
163 }
164
165 MPI_Comm ActorExt::comm_world() const
166 {
167   return comm_world_ == nullptr ? MPI_COMM_NULL : *comm_world_;
168 }
169
170 s4u::MutexPtr ActorExt::mailboxes_mutex() const
171 {
172   return mailboxes_mutex_;
173 }
174
175 #if HAVE_PAPI
176 int ActorExt::papi_event_set() const
177 {
178   return papi_event_set_;
179 }
180
181 papi_counter_t& ActorExt::papi_counters()
182 {
183   return papi_counter_data_;
184 }
185 #endif
186
187 xbt_os_timer_t ActorExt::timer()
188 {
189   return timer_;
190 }
191
192 void ActorExt::simulated_start()
193 {
194   simulated_ = s4u::Engine::get_clock();
195 }
196
197 double ActorExt::simulated_elapsed() const
198 {
199   return s4u::Engine::get_clock() - simulated_;
200 }
201
202 MPI_Comm ActorExt::comm_self()
203 {
204   if (comm_self_ == MPI_COMM_NULL) {
205     auto* group = new Group(1);
206     comm_self_  = new Comm(group, nullptr);
207     comm_self_->set_name("MPI_COMM_SELF");
208     group->set_mapping(actor_->get_pid(), 0);
209   }
210   return comm_self_;
211 }
212
213 MPI_Info ActorExt::info_env()
214 {
215   if (info_env_==MPI_INFO_NULL)
216     info_env_=new Info();
217   return info_env_;
218 }
219
220 MPI_Comm ActorExt::comm_intra()
221 {
222   return comm_intra_;
223 }
224
225 void ActorExt::set_comm_intra(MPI_Comm comm)
226 {
227   comm_intra_ = comm;
228 }
229
230 void ActorExt::set_sampling(int s)
231 {
232   sampling_ = s;
233 }
234
235 int ActorExt::sampling() const
236 {
237   return sampling_;
238 }
239
240 void ActorExt::init()
241 {
242   ActorExt* ext = smpi_process();
243   // if we are in MPI_Init and argc handling has already been done.
244   if (ext->initialized())
245     return;
246
247   const simgrid::s4u::Actor* self = simgrid::s4u::Actor::self();
248   const char* id                  = self->get_property("instance_id");
249   xbt_assert(id != nullptr, "Actor '%s' seem to be calling MPI_Init(), but it was created outside of MPI, wasn't it?",
250              self->get_cname());
251   ext->instance_id_ = id;
252   const int rank = static_cast<int>(xbt_str_parse_int(self->get_property("rank"), "Cannot parse rank"));
253
254   ext->state_ = SmpiProcessState::INITIALIZING;
255   smpi_deployment_register_process(ext->instance_id_, rank, self);
256
257   ext->comm_world_ = smpi_deployment_comm_world(ext->instance_id_);
258
259   // set the process attached to the mailbox
260   ext->mailbox_small()->set_receiver(ext->actor_);
261   XBT_DEBUG("<%ld> SMPI process has been initialized: %p", ext->actor_->get_pid(), ext->actor_);
262 }
263
264 int ActorExt::get_optind() const
265 {
266   return optind_;
267 }
268
269 void ActorExt::set_optind(int new_optind)
270 {
271   optind_ = new_optind;
272 }
273
274 void ActorExt::bsend_buffer(void** buf, int* size)
275 {
276   *buf  = bsend_buffer_;
277   *size = bsend_buffer_size_;
278 }
279
280 int ActorExt::set_bsend_buffer(void* buf, int size)
281 {
282   if(buf!=nullptr && bsend_buffer_!=nullptr)
283     return MPI_ERR_BUFFER;
284   bsend_buffer_     = buf;
285   bsend_buffer_size_= size;
286   return MPI_SUCCESS;
287 }
288
289 } // namespace simgrid::smpi