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 "smpi_info.hpp"
10 #include "src/mc/mc_replay.hpp"
11 #include "src/simix/smx_private.hpp"
15 extern std::string papi_default_config_name;
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
22 simgrid::xbt::Extension<simgrid::s4u::Actor, ActorExt> ActorExt::EXTENSION_ID;
24 ActorExt::ActorExt(s4u::Actor* actor) : actor_(actor)
26 if (not simgrid::smpi::ActorExt::EXTENSION_ID.valid())
27 simgrid::smpi::ActorExt::EXTENSION_ID = simgrid::s4u::Actor::extension_create<simgrid::smpi::ActorExt>();
29 mailbox_ = s4u::Mailbox::by_name("SMPI-" + std::to_string(actor_->get_pid()));
30 mailbox_small_ = s4u::Mailbox::by_name("small-" + std::to_string(actor_->get_pid()));
31 mailboxes_mutex_ = s4u::Mutex::create();
32 timer_ = xbt_os_timer_new();
33 state_ = SmpiProcessState::UNINITIALIZED;
34 info_env_ = MPI_INFO_NULL;
36 MC_ignore_heap(timer_, xbt_os_timer_size());
39 if (not simgrid::config::get_value<std::string>("smpi/papi-events").empty()) {
40 // TODO: Implement host/process/thread based counters. This implementation
41 // just always takes the values passed via "default", like this:
42 // "default:COUNTER1:COUNTER2:COUNTER3;".
43 auto it = units2papi_setup.find(papi_default_config_name);
44 if (it != units2papi_setup.end()) {
45 papi_event_set_ = it->second.event_set;
46 papi_counter_data_ = it->second.counter_data;
47 XBT_DEBUG("Setting PAPI set for process %li", actor->get_pid());
49 papi_event_set_ = PAPI_NULL;
50 XBT_DEBUG("No PAPI set for process %li", actor->get_pid());
58 if (comm_self_ != MPI_COMM_NULL)
59 simgrid::smpi::Comm::destroy(comm_self_);
60 if (comm_intra_ != MPI_COMM_NULL)
61 simgrid::smpi::Comm::destroy(comm_intra_);
62 xbt_os_timer_free(timer_);
65 /** @brief Prepares the current process for termination. */
66 void ActorExt::finalize()
68 state_ = SmpiProcessState::FINALIZED;
69 XBT_DEBUG("<%ld> Process left the game", actor_->get_pid());
71 smpi_deployment_unregister_process(instance_id_);
74 /** @brief Check if a process is finalized */
75 int ActorExt::finalized()
77 return (state_ == SmpiProcessState::FINALIZED);
80 /** @brief Check if a process is partially initialized already */
81 int ActorExt::initializing()
83 return (state_ == SmpiProcessState::INITIALIZING);
86 /** @brief Check if a process is initialized */
87 int ActorExt::initialized()
89 // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
90 // single process ... ?
91 return (state_ == SmpiProcessState::INITIALIZED);
94 /** @brief Mark a process as initialized (=MPI_Init called) */
95 void ActorExt::mark_as_initialized()
97 if (state_ != SmpiProcessState::FINALIZED)
98 state_ = SmpiProcessState::INITIALIZED;
101 void ActorExt::set_replaying(bool value)
103 if (state_ != SmpiProcessState::FINALIZED)
107 bool ActorExt::replaying()
112 s4u::ActorPtr ActorExt::get_actor()
118 * @brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
120 * @see smpi_trace_set_call_location
122 smpi_trace_call_location_t* ActorExt::call_location()
124 return &trace_call_loc_;
127 void ActorExt::set_privatized_region(smpi_privatization_region_t region)
129 privatized_region_ = region;
132 smpi_privatization_region_t ActorExt::privatized_region()
134 return privatized_region_;
137 MPI_Comm ActorExt::comm_world()
139 return comm_world_ == nullptr ? MPI_COMM_NULL : *comm_world_;
142 s4u::MutexPtr ActorExt::mailboxes_mutex()
144 return mailboxes_mutex_;
148 int ActorExt::papi_event_set()
150 return papi_event_set_;
153 papi_counter_t& ActorExt::papi_counters()
155 return papi_counter_data_;
159 xbt_os_timer_t ActorExt::timer()
164 void ActorExt::simulated_start()
166 simulated_ = SIMIX_get_clock();
169 double ActorExt::simulated_elapsed()
171 return SIMIX_get_clock() - simulated_;
174 MPI_Comm ActorExt::comm_self()
176 if (comm_self_ == MPI_COMM_NULL) {
177 MPI_Group group = new Group(1);
178 comm_self_ = new Comm(group, nullptr);
179 group->set_mapping(actor_, 0);
184 MPI_Info ActorExt::info_env()
186 if (info_env_==MPI_INFO_NULL)
187 info_env_=new Info();
191 MPI_Comm ActorExt::comm_intra()
196 void ActorExt::set_comm_intra(MPI_Comm comm)
201 void ActorExt::set_sampling(int s)
206 int ActorExt::sampling()
211 void ActorExt::init()
213 xbt_assert(smpi_get_universe_size() != 0, "SimGrid was not initialized properly before entering MPI_Init. "
214 "Aborting, please check compilation process and use smpirun.");
216 simgrid::s4u::ActorPtr self = simgrid::s4u::Actor::self();
217 // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
218 // this up here so that I can set the privatized region before the switch.
219 ActorExt* ext = smpi_process();
220 // if we are in MPI_Init and argc handling has already been done.
221 if (ext->initialized())
224 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
225 /* Now using the segment index of this process */
226 ext->set_privatized_region(smpi_init_global_memory_segment_process());
227 /* Done at the process's creation */
228 SMPI_switch_data_segment(self);
231 ext->instance_id_ = self->get_property("instance_id");
232 const int rank = xbt_str_parse_int(self->get_property("rank"), "Cannot parse rank");
234 ext->state_ = SmpiProcessState::INITIALIZING;
235 smpi_deployment_register_process(ext->instance_id_, rank, self);
237 ext->comm_world_ = smpi_deployment_comm_world(ext->instance_id_);
239 // set the process attached to the mailbox
240 ext->mailbox_small_->set_receiver(ext->actor_);
241 XBT_DEBUG("<%ld> SMPI process has been initialized: %p", ext->actor_->get_pid(), ext->actor_);
244 int ActorExt::get_optind()
249 void ActorExt::set_optind(int new_optind)
251 optind_ = new_optind;
254 void ActorExt::bsend_buffer(void** buf, int* size)
256 *buf = bsend_buffer_;
257 *size = bsend_buffer_size_;
260 void ActorExt::set_bsend_buffer(void* buf, int size)
263 bsend_buffer_size_= size;
267 } // namespace simgrid