Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / smpi / internals / smpi_actor.cpp
1 /* Copyright (c) 2009-2018. 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 using simgrid::s4u::Actor;
23 using simgrid::s4u::ActorPtr;
24
25 ActorExt::ActorExt(ActorPtr actor, simgrid::s4u::Barrier* finalization_barrier)
26     : finalization_barrier_(finalization_barrier), actor_(actor)
27 {
28   mailbox_         = simgrid::s4u::Mailbox::by_name("SMPI-" + std::to_string(actor_->get_pid()));
29   mailbox_small_   = simgrid::s4u::Mailbox::by_name("small-" + std::to_string(actor_->get_pid()));
30   mailboxes_mutex_ = xbt_mutex_init();
31   timer_           = xbt_os_timer_new();
32   state_           = SmpiProcessState::UNINITIALIZED;
33   if (MC_is_active())
34     MC_ignore_heap(timer_, xbt_os_timer_size());
35
36 #if HAVE_PAPI
37   if (not simgrid::config::get_value<std::string>("smpi/papi-events").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(papi_default_config_name);
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   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   xbt_mutex_destroy(mailboxes_mutex_);
62 }
63
64 void ActorExt::set_data(int* argc, char*** argv)
65 {
66   instance_id_                   = std::string((*argv)[1]);
67   comm_world_                    = smpi_deployment_comm_world(instance_id_);
68   simgrid::s4u::Barrier* barrier = smpi_deployment_finalization_barrier(instance_id_);
69   if (barrier != nullptr) // don't overwrite the current one if the instance has none
70     finalization_barrier_ = barrier;
71
72   if (*argc > 3) {
73     memmove(&(*argv)[0], &(*argv)[2], sizeof(char*) * (*argc - 2));
74     (*argv)[(*argc) - 1] = nullptr;
75     (*argv)[(*argc) - 2] = nullptr;
76   }
77   (*argc) -= 2;
78   // set the process attached to the mailbox
79   mailbox_small_->set_receiver(actor_);
80   XBT_DEBUG("<%ld> SMPI process has been initialized: %p", actor_->get_pid(), actor_.get());
81 }
82
83 /** @brief Prepares the current process for termination. */
84 void ActorExt::finalize()
85 {
86   state_ = SmpiProcessState::FINALIZED;
87   XBT_DEBUG("<%ld> Process left the game", actor_->get_pid());
88
89   // This leads to an explosion of the search graph which cannot be reduced:
90   if (MC_is_active() || MC_record_replay_is_active())
91     return;
92   // wait for all pending asynchronous comms to finish
93   finalization_barrier_->wait();
94 }
95
96 /** @brief Check if a process is finalized */
97 int ActorExt::finalized()
98 {
99   return (state_ == SmpiProcessState::FINALIZED);
100 }
101
102 /** @brief Check if a process is partially initialized already */
103 int ActorExt::initializing()
104 {
105   return (state_ == SmpiProcessState::INITIALIZING);
106 }
107
108 /** @brief Check if a process is initialized */
109 int ActorExt::initialized()
110 {
111   // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
112   // single process ... ?
113   return (state_ == SmpiProcessState::INITIALIZED);
114 }
115
116 /** @brief Mark a process as initialized (=MPI_Init called) */
117 void ActorExt::mark_as_initialized()
118 {
119   if (state_ != SmpiProcessState::FINALIZED)
120     state_ = SmpiProcessState::INITIALIZED;
121 }
122
123 void ActorExt::set_replaying(bool value)
124 {
125   if (state_ != SmpiProcessState::FINALIZED)
126     replaying_ = value;
127 }
128
129 bool ActorExt::replaying()
130 {
131   return replaying_;
132 }
133
134 ActorPtr ActorExt::get_actor()
135 {
136   return actor_;
137 }
138
139 /**
140  * @brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
141  *
142  * @see smpi_trace_set_call_location
143  */
144 smpi_trace_call_location_t* ActorExt::call_location()
145 {
146   return &trace_call_loc_;
147 }
148
149 void ActorExt::set_privatized_region(smpi_privatization_region_t region)
150 {
151   privatized_region_ = region;
152 }
153
154 smpi_privatization_region_t ActorExt::privatized_region()
155 {
156   return privatized_region_;
157 }
158
159 MPI_Comm ActorExt::comm_world()
160 {
161   return comm_world_ == nullptr ? MPI_COMM_NULL : *comm_world_;
162 }
163
164 smx_mailbox_t ActorExt::mailbox()
165 {
166   return mailbox_->get_impl();
167 }
168
169 smx_mailbox_t ActorExt::mailbox_small()
170 {
171   return mailbox_small_->get_impl();
172 }
173
174 xbt_mutex_t ActorExt::mailboxes_mutex()
175 {
176   return mailboxes_mutex_;
177 }
178
179 #if HAVE_PAPI
180 int ActorExt::papi_event_set()
181 {
182   return papi_event_set_;
183 }
184
185 papi_counter_t& ActorExt::papi_counters()
186 {
187   return papi_counter_data_;
188 }
189 #endif
190
191 xbt_os_timer_t ActorExt::timer()
192 {
193   return timer_;
194 }
195
196 void ActorExt::simulated_start()
197 {
198   simulated_ = SIMIX_get_clock();
199 }
200
201 double ActorExt::simulated_elapsed()
202 {
203   return SIMIX_get_clock() - simulated_;
204 }
205
206 MPI_Comm ActorExt::comm_self()
207 {
208   if (comm_self_ == MPI_COMM_NULL) {
209     MPI_Group group = new Group(1);
210     comm_self_      = new Comm(group, nullptr);
211     group->set_mapping(actor_, 0);
212   }
213   return comm_self_;
214 }
215
216 MPI_Comm ActorExt::comm_intra()
217 {
218   return comm_intra_;
219 }
220
221 void ActorExt::set_comm_intra(MPI_Comm comm)
222 {
223   comm_intra_ = comm;
224 }
225
226 void ActorExt::set_sampling(int s)
227 {
228   sampling_ = s;
229 }
230
231 int ActorExt::sampling()
232 {
233   return sampling_;
234 }
235
236 void ActorExt::init(int* argc, char*** argv)
237 {
238
239   if (smpi_process_count() == 0) {
240     xbt_die("SimGrid was not initialized properly before entering MPI_Init. Aborting, please check compilation process "
241             "and use smpirun\n");
242   }
243   if (argc != nullptr && argv != nullptr) {
244     simgrid::s4u::ActorPtr proc = simgrid::s4u::Actor::self();
245     proc->get_impl()->context_->set_cleanup(&SIMIX_process_cleanup);
246     // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
247     // this up here so that I can set the privatized region before the switch.
248     ActorExt* process = smpi_process_remote(proc);
249     //if we are in MPI_Init and argc handling has already been done.
250     if (process->initialized())
251       return;
252       
253     process->state_ = SmpiProcessState::INITIALIZING;
254     
255     char* instance_id = (*argv)[1];
256     try {
257       int rank = std::stoi(std::string((*argv)[2]));
258       smpi_deployment_register_process(instance_id, rank, proc);
259     } catch (std::invalid_argument& ia) {
260       throw std::invalid_argument(std::string("Invalid rank: ") + (*argv)[2]);
261     }
262
263     if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
264       /* Now using the segment index of this process  */
265       process->set_privatized_region(smpi_init_global_memory_segment_process());
266       /* Done at the process's creation */
267       SMPI_switch_data_segment(proc);
268     }
269
270     process->set_data(argc, argv);
271   } 
272 }
273
274 int ActorExt::get_optind()
275 {
276   return optind;
277 }
278 void ActorExt::set_optind(int new_optind)
279 {
280   optind = new_optind;
281 }
282
283 } // namespace smpi
284 } // namespace simgrid