1 /* Copyright (c) 2009-2019. 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 ActorExt::ActorExt(s4u::ActorPtr actor) : actor_(actor)
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;
31 MC_ignore_heap(timer_, xbt_os_timer_size());
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());
44 papi_event_set_ = PAPI_NULL;
45 XBT_DEBUG("No PAPI set for process %li", actor->get_pid());
53 TRACE_smpi_finalize(actor_->get_pid());
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_);
62 /** @brief Prepares the current process for termination. */
63 void ActorExt::finalize()
65 state_ = SmpiProcessState::FINALIZED;
66 XBT_DEBUG("<%ld> Process left the game", actor_->get_pid());
68 smpi_deployment_unregister_process(instance_id_);
71 /** @brief Check if a process is finalized */
72 int ActorExt::finalized()
74 return (state_ == SmpiProcessState::FINALIZED);
77 /** @brief Check if a process is partially initialized already */
78 int ActorExt::initializing()
80 return (state_ == SmpiProcessState::INITIALIZING);
83 /** @brief Check if a process is initialized */
84 int ActorExt::initialized()
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);
91 /** @brief Mark a process as initialized (=MPI_Init called) */
92 void ActorExt::mark_as_initialized()
94 if (state_ != SmpiProcessState::FINALIZED)
95 state_ = SmpiProcessState::INITIALIZED;
98 void ActorExt::set_replaying(bool value)
100 if (state_ != SmpiProcessState::FINALIZED)
104 bool ActorExt::replaying()
109 s4u::ActorPtr ActorExt::get_actor()
115 * @brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
117 * @see smpi_trace_set_call_location
119 smpi_trace_call_location_t* ActorExt::call_location()
121 return &trace_call_loc_;
124 void ActorExt::set_privatized_region(smpi_privatization_region_t region)
126 privatized_region_ = region;
129 smpi_privatization_region_t ActorExt::privatized_region()
131 return privatized_region_;
134 MPI_Comm ActorExt::comm_world()
136 return comm_world_ == nullptr ? MPI_COMM_NULL : *comm_world_;
139 s4u::MutexPtr ActorExt::mailboxes_mutex()
141 return mailboxes_mutex_;
145 int ActorExt::papi_event_set()
147 return papi_event_set_;
150 papi_counter_t& ActorExt::papi_counters()
152 return papi_counter_data_;
156 xbt_os_timer_t ActorExt::timer()
161 void ActorExt::simulated_start()
163 simulated_ = SIMIX_get_clock();
166 double ActorExt::simulated_elapsed()
168 return SIMIX_get_clock() - simulated_;
171 MPI_Comm ActorExt::comm_self()
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);
181 MPI_Info ActorExt::info_env()
186 MPI_Comm ActorExt::comm_intra()
191 void ActorExt::set_comm_intra(MPI_Comm comm)
196 void ActorExt::set_sampling(int s)
201 int ActorExt::sampling()
206 void ActorExt::init()
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.");
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())
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);
226 ext->instance_id_ = self->get_property("instance_id");
227 const int rank = xbt_str_parse_int(self->get_property("rank"), "Cannot parse rank");
229 ext->state_ = SmpiProcessState::INITIALIZING;
230 smpi_deployment_register_process(ext->instance_id_, rank, self);
232 ext->comm_world_ = smpi_deployment_comm_world(ext->instance_id_);
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());
239 int ActorExt::get_optind()
244 void ActorExt::set_optind(int new_optind)
246 optind_ = new_optind;
250 } // namespace simgrid