Logo AND Algorithmique Numérique Distribuée

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