Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[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
14 #if HAVE_PAPI
15 #include "papi.h"
16 #endif
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
19
20 namespace simgrid {
21 namespace 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 void ActorExt::set_replaying(bool value)
103 {
104   if (state_ != SmpiProcessState::FINALIZED)
105     replaying_ = value;
106 }
107
108 bool ActorExt::replaying() const
109 {
110   return replaying_;
111 }
112
113 s4u::ActorPtr ActorExt::get_actor()
114 {
115   return actor_;
116 }
117
118 /**
119  * @brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
120  *
121  * @see smpi_trace_set_call_location
122  */
123 smpi_trace_call_location_t* ActorExt::call_location()
124 {
125   return &trace_call_loc_;
126 }
127
128 void ActorExt::set_privatized_region(smpi_privatization_region_t region)
129 {
130   privatized_region_ = region;
131 }
132
133 smpi_privatization_region_t ActorExt::privatized_region() const
134 {
135   return privatized_region_;
136 }
137
138 MPI_Comm ActorExt::comm_world() const
139 {
140   return comm_world_ == nullptr ? MPI_COMM_NULL : *comm_world_;
141 }
142
143 s4u::MutexPtr ActorExt::mailboxes_mutex() const
144 {
145   return mailboxes_mutex_;
146 }
147
148 #if HAVE_PAPI
149 int ActorExt::papi_event_set() const
150 {
151   return papi_event_set_;
152 }
153
154 papi_counter_t& ActorExt::papi_counters()
155 {
156   return papi_counter_data_;
157 }
158 #endif
159
160 xbt_os_timer_t ActorExt::timer()
161 {
162   return timer_;
163 }
164
165 void ActorExt::simulated_start()
166 {
167   simulated_ = s4u::Engine::get_clock();
168 }
169
170 double ActorExt::simulated_elapsed() const
171 {
172   return s4u::Engine::get_clock() - simulated_;
173 }
174
175 MPI_Comm ActorExt::comm_self()
176 {
177   if (comm_self_ == MPI_COMM_NULL) {
178     auto* group = new Group(1);
179     comm_self_  = new Comm(group, nullptr);
180     comm_self_->set_name("MPI_COMM_SELF");
181     group->set_mapping(actor_->get_pid(), 0);
182   }
183   return comm_self_;
184 }
185
186 MPI_Info ActorExt::info_env()
187 {
188   if (info_env_==MPI_INFO_NULL)
189     info_env_=new Info();
190   return info_env_;
191 }
192
193 MPI_Comm ActorExt::comm_intra()
194 {
195   return comm_intra_;
196 }
197
198 void ActorExt::set_comm_intra(MPI_Comm comm)
199 {
200   comm_intra_ = comm;
201 }
202
203 void ActorExt::set_sampling(int s)
204 {
205   sampling_ = s;
206 }
207
208 int ActorExt::sampling() const
209 {
210   return sampling_;
211 }
212
213 void ActorExt::init()
214 {
215   xbt_assert(smpi_get_universe_size() != 0, "SimGrid was not initialized properly before entering MPI_Init. "
216                                             "Aborting, please check compilation process and use smpirun.");
217
218   ActorExt* ext = smpi_process();
219   // if we are in MPI_Init and argc handling has already been done.
220   if (ext->initialized())
221     return;
222
223   const simgrid::s4u::Actor* self = simgrid::s4u::Actor::self();
224   ext->instance_id_ = self->get_property("instance_id");
225   const int rank    = xbt_str_parse_int(self->get_property("rank"), "Cannot parse rank");
226
227   ext->state_ = SmpiProcessState::INITIALIZING;
228   smpi_deployment_register_process(ext->instance_id_, rank, self);
229
230   ext->comm_world_ = smpi_deployment_comm_world(ext->instance_id_);
231
232   // set the process attached to the mailbox
233   ext->mailbox_small_->set_receiver(ext->actor_);
234   XBT_DEBUG("<%ld> SMPI process has been initialized: %p", ext->actor_->get_pid(), ext->actor_);
235 }
236
237 int ActorExt::get_optind() const
238 {
239   return optind_;
240 }
241
242 void ActorExt::set_optind(int new_optind)
243 {
244   optind_ = new_optind;
245 }
246
247 void ActorExt::bsend_buffer(void** buf, int* size)
248 {
249   *buf  = bsend_buffer_;
250   *size = bsend_buffer_size_;
251 }
252
253 int ActorExt::set_bsend_buffer(void* buf, int size)
254 {
255   if(buf!=nullptr && bsend_buffer_!=nullptr)
256     return MPI_ERR_BUFFER;
257   bsend_buffer_     = buf;
258   bsend_buffer_size_= size;
259   return MPI_SUCCESS;
260 }
261
262 } // namespace smpi
263 } // namespace simgrid