Logo AND Algorithmique Numérique Distribuée

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