1 /* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/smpi/include/smpi_actor.hpp"
8 #include "smpi_comm.hpp"
9 #include "src/mc/mc_replay.hpp"
10 #include "src/simix/smx_private.hpp"
14 extern std::string papi_default_config_name;
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
22 using simgrid::s4u::Actor;
23 using simgrid::s4u::ActorPtr;
25 ActorExt::ActorExt(ActorPtr actor, simgrid::s4u::Barrier* finalization_barrier)
26 : finalization_barrier_(finalization_barrier), actor_(actor)
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;
34 MC_ignore_heap(timer_, xbt_os_timer_size());
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());
47 papi_event_set_ = PAPI_NULL;
48 XBT_DEBUG("No PAPI set for process %li", actor->get_pid());
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_);
64 void ActorExt::set_data(const char* instance_id)
66 instance_id_ = std::string(instance_id);
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;
72 // set the process attached to the mailbox
73 mailbox_small_->set_receiver(actor_);
74 XBT_DEBUG("<%ld> SMPI process has been initialized: %p", actor_->get_pid(), actor_.get());
77 /** @brief Prepares the current process for termination. */
78 void ActorExt::finalize()
80 state_ = SmpiProcessState::FINALIZED;
81 XBT_DEBUG("<%ld> Process left the game", actor_->get_pid());
83 // This leads to an explosion of the search graph which cannot be reduced:
84 if (MC_is_active() || MC_record_replay_is_active())
86 // wait for all pending asynchronous comms to finish
87 finalization_barrier_->wait();
90 /** @brief Check if a process is finalized */
91 int ActorExt::finalized()
93 return (state_ == SmpiProcessState::FINALIZED);
96 /** @brief Check if a process is partially initialized already */
97 int ActorExt::initializing()
99 return (state_ == SmpiProcessState::INITIALIZING);
102 /** @brief Check if a process is initialized */
103 int ActorExt::initialized()
105 // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
106 // single process ... ?
107 return (state_ == SmpiProcessState::INITIALIZED);
110 /** @brief Mark a process as initialized (=MPI_Init called) */
111 void ActorExt::mark_as_initialized()
113 if (state_ != SmpiProcessState::FINALIZED)
114 state_ = SmpiProcessState::INITIALIZED;
117 void ActorExt::set_replaying(bool value)
119 if (state_ != SmpiProcessState::FINALIZED)
123 bool ActorExt::replaying()
128 ActorPtr ActorExt::get_actor()
134 * @brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
136 * @see smpi_trace_set_call_location
138 smpi_trace_call_location_t* ActorExt::call_location()
140 return &trace_call_loc_;
143 void ActorExt::set_privatized_region(smpi_privatization_region_t region)
145 privatized_region_ = region;
148 smpi_privatization_region_t ActorExt::privatized_region()
150 return privatized_region_;
153 MPI_Comm ActorExt::comm_world()
155 return comm_world_ == nullptr ? MPI_COMM_NULL : *comm_world_;
158 smx_mailbox_t ActorExt::mailbox()
160 return mailbox_->get_impl();
163 smx_mailbox_t ActorExt::mailbox_small()
165 return mailbox_small_->get_impl();
168 xbt_mutex_t ActorExt::mailboxes_mutex()
170 return mailboxes_mutex_;
174 int ActorExt::papi_event_set()
176 return papi_event_set_;
179 papi_counter_t& ActorExt::papi_counters()
181 return papi_counter_data_;
185 xbt_os_timer_t ActorExt::timer()
190 void ActorExt::simulated_start()
192 simulated_ = SIMIX_get_clock();
195 double ActorExt::simulated_elapsed()
197 return SIMIX_get_clock() - simulated_;
200 MPI_Comm ActorExt::comm_self()
202 if (comm_self_ == MPI_COMM_NULL) {
203 MPI_Group group = new Group(1);
204 comm_self_ = new Comm(group, nullptr);
205 group->set_mapping(actor_, 0);
210 MPI_Comm ActorExt::comm_intra()
215 void ActorExt::set_comm_intra(MPI_Comm comm)
220 void ActorExt::set_sampling(int s)
225 int ActorExt::sampling()
230 void ActorExt::init()
232 if (smpi_process_count() == 0) {
233 xbt_die("SimGrid was not initialized properly before entering MPI_Init. Aborting, please check compilation process "
234 "and use smpirun\n");
237 simgrid::s4u::ActorPtr proc = simgrid::s4u::Actor::self();
238 proc->get_impl()->context_->set_cleanup(&SIMIX_process_cleanup);
239 // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
240 // this up here so that I can set the privatized region before the switch.
241 ActorExt* process = smpi_process_remote(proc);
242 // if we are in MPI_Init and argc handling has already been done.
243 if (process->initialized())
246 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
247 /* Now using the segment index of this process */
248 process->set_privatized_region(smpi_init_global_memory_segment_process());
249 /* Done at the process's creation */
250 SMPI_switch_data_segment(proc);
253 const char* instance_id = simgrid::s4u::Actor::self()->get_property("instance_id");
254 const int rank = xbt_str_parse_int(simgrid::s4u::Actor::self()->get_property("rank"), "Cannot parse rank");
256 process->state_ = SmpiProcessState::INITIALIZING;
257 smpi_deployment_register_process(instance_id, rank, proc);
259 process->set_data(instance_id);
262 int ActorExt::get_optind()
266 void ActorExt::set_optind(int new_optind)
272 } // namespace simgrid