Logo AND Algorithmique Numérique Distribuée

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