Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
75b0c806af1595e61a5d019a73bcbbcc6bb02ee2
[simgrid.git] / src / smpi / internals / smpi_actor.cpp
1 /* Copyright (c) 2009-2021. 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 "smpi_info.hpp"
10 #include "src/mc/mc_replay.hpp"
11 #include "src/simix/smx_private.hpp"
12 #include "simgrid/s4u/Mutex.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_ = SIMIX_get_clock();
168 }
169
170 double ActorExt::simulated_elapsed() const
171 {
172   return SIMIX_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     group->set_mapping(actor_, 0);
181   }
182   return comm_self_;
183 }
184
185 MPI_Info ActorExt::info_env()
186 {
187   if (info_env_==MPI_INFO_NULL)
188     info_env_=new Info();
189   return info_env_;
190 }
191
192 MPI_Comm ActorExt::comm_intra()
193 {
194   return comm_intra_;
195 }
196
197 void ActorExt::set_comm_intra(MPI_Comm comm)
198 {
199   comm_intra_ = comm;
200 }
201
202 void ActorExt::set_sampling(int s)
203 {
204   sampling_ = s;
205 }
206
207 int ActorExt::sampling() const
208 {
209   return sampling_;
210 }
211
212 void ActorExt::init()
213 {
214   xbt_assert(smpi_get_universe_size() != 0, "SimGrid was not initialized properly before entering MPI_Init. "
215                                             "Aborting, please check compilation process and use smpirun.");
216
217   simgrid::s4u::Actor* self = simgrid::s4u::Actor::self();
218   // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
219   // this up here so that I can set the privatized region before the switch.
220   ActorExt* ext = smpi_process();
221   // if we are in MPI_Init and argc handling has already been done.
222   if (ext->initialized())
223     return;
224
225   if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
226     /* Now using the segment index of this process  */
227     ext->set_privatized_region(smpi_init_global_memory_segment_process());
228     /* Done at the process's creation */
229     SMPI_switch_data_segment(self);
230   }
231
232   ext->instance_id_ = self->get_property("instance_id");
233   const int rank    = xbt_str_parse_int(self->get_property("rank"), "Cannot parse rank");
234
235   ext->state_ = SmpiProcessState::INITIALIZING;
236   smpi_deployment_register_process(ext->instance_id_, rank, self);
237
238   ext->comm_world_ = smpi_deployment_comm_world(ext->instance_id_);
239
240   // set the process attached to the mailbox
241   ext->mailbox_small_->set_receiver(ext->actor_);
242   XBT_DEBUG("<%ld> SMPI process has been initialized: %p", ext->actor_->get_pid(), ext->actor_);
243 }
244
245 int ActorExt::get_optind() const
246 {
247   return optind_;
248 }
249
250 void ActorExt::set_optind(int new_optind)
251 {
252   optind_ = new_optind;
253 }
254
255 void ActorExt::bsend_buffer(void** buf, int* size)
256 {
257   *buf  = bsend_buffer_;
258   *size = bsend_buffer_size_;
259 }
260
261 int ActorExt::set_bsend_buffer(void* buf, int size)
262 {
263   if(buf!=nullptr && bsend_buffer_!=nullptr)
264     return MPI_ERR_BUFFER;
265   bsend_buffer_     = buf;
266   bsend_buffer_size_= size;
267   return MPI_SUCCESS;
268 }
269
270 } // namespace smpi
271 } // namespace simgrid