1 /* Copyright (c) 2009-2022. 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 "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Mutex.hpp"
10 #include "smpi_comm.hpp"
11 #include "smpi_info.hpp"
12 #include "src/mc/mc_replay.hpp"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
21 namespace simgrid::smpi {
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 smpi_cfg_papi_events_file().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("default");
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 xbt_os_timer_free(timer_);
61 /** @brief Prepares the current process for termination. */
62 void ActorExt::finalize()
64 state_ = SmpiProcessState::FINALIZED;
65 XBT_DEBUG("<%ld> Process left the game", actor_->get_pid());
66 if (info_env_ != MPI_INFO_NULL)
67 simgrid::smpi::Info::unref(info_env_);
68 if (comm_self_ != MPI_COMM_NULL)
69 simgrid::smpi::Comm::destroy(comm_self_);
70 if (comm_intra_ != MPI_COMM_NULL)
71 simgrid::smpi::Comm::destroy(comm_intra_);
72 smpi_deployment_unregister_process(instance_id_);
75 /** @brief Check if a process is finalized */
76 int ActorExt::finalized() const
78 return (state_ == SmpiProcessState::FINALIZED);
81 /** @brief Check if a process is partially initialized already */
82 int ActorExt::initializing() const
84 return (state_ == SmpiProcessState::INITIALIZING);
87 /** @brief Check if a process is initialized */
88 int ActorExt::initialized() const
90 // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
91 // single process ... ?
92 return (state_ == SmpiProcessState::INITIALIZED);
95 /** @brief Mark a process as initialized (=MPI_Init called) */
96 void ActorExt::mark_as_initialized()
98 if (state_ != SmpiProcessState::FINALIZED)
99 state_ = SmpiProcessState::INITIALIZED;
102 /** @brief Mark a process as finalizing (=MPI_Finalize called) */
103 void ActorExt::mark_as_finalizing()
105 if (state_ != SmpiProcessState::FINALIZED)
106 state_ = SmpiProcessState::FINALIZING;
109 /** @brief Check if a process is finalizing */
110 int ActorExt::finalizing() const
112 return (state_ == SmpiProcessState::FINALIZING);
115 void ActorExt::set_replaying(bool value)
117 if (state_ != SmpiProcessState::FINALIZED)
121 bool ActorExt::replaying() const
126 s4u::ActorPtr ActorExt::get_actor()
132 * @brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
134 * @see smpi_trace_set_call_location
136 smpi_trace_call_location_t* ActorExt::call_location()
138 return &trace_call_loc_;
141 void ActorExt::set_privatized_region(smpi_privatization_region_t region)
143 privatized_region_ = region;
146 smpi_privatization_region_t ActorExt::privatized_region() const
148 return privatized_region_;
151 MPI_Comm ActorExt::comm_world() const
153 return comm_world_ == nullptr ? MPI_COMM_NULL : *comm_world_;
156 s4u::MutexPtr ActorExt::mailboxes_mutex() const
158 return mailboxes_mutex_;
162 int ActorExt::papi_event_set() const
164 return papi_event_set_;
167 papi_counter_t& ActorExt::papi_counters()
169 return papi_counter_data_;
173 xbt_os_timer_t ActorExt::timer()
178 void ActorExt::simulated_start()
180 simulated_ = s4u::Engine::get_clock();
183 double ActorExt::simulated_elapsed() const
185 return s4u::Engine::get_clock() - simulated_;
188 MPI_Comm ActorExt::comm_self()
190 if (comm_self_ == MPI_COMM_NULL) {
191 auto* group = new Group(1);
192 comm_self_ = new Comm(group, nullptr);
193 comm_self_->set_name("MPI_COMM_SELF");
194 group->set_mapping(actor_->get_pid(), 0);
199 MPI_Info ActorExt::info_env()
201 if (info_env_==MPI_INFO_NULL)
202 info_env_=new Info();
206 MPI_Comm ActorExt::comm_intra()
211 void ActorExt::set_comm_intra(MPI_Comm comm)
216 void ActorExt::set_sampling(int s)
221 int ActorExt::sampling() const
226 void ActorExt::init()
228 xbt_assert(smpi_get_universe_size() != 0, "SimGrid was not initialized properly before entering MPI_Init. "
229 "Aborting, please check compilation process and use smpirun.");
231 ActorExt* ext = smpi_process();
232 // if we are in MPI_Init and argc handling has already been done.
233 if (ext->initialized())
236 const simgrid::s4u::Actor* self = simgrid::s4u::Actor::self();
237 ext->instance_id_ = self->get_property("instance_id");
238 const int rank = static_cast<int>(xbt_str_parse_int(self->get_property("rank"), "Cannot parse rank"));
240 ext->state_ = SmpiProcessState::INITIALIZING;
241 smpi_deployment_register_process(ext->instance_id_, rank, self);
243 ext->comm_world_ = smpi_deployment_comm_world(ext->instance_id_);
245 // set the process attached to the mailbox
246 ext->mailbox_small_->set_receiver(ext->actor_);
247 XBT_DEBUG("<%ld> SMPI process has been initialized: %p", ext->actor_->get_pid(), ext->actor_);
250 int ActorExt::get_optind() const
255 void ActorExt::set_optind(int new_optind)
257 optind_ = new_optind;
260 void ActorExt::bsend_buffer(void** buf, int* size)
262 *buf = bsend_buffer_;
263 *size = bsend_buffer_size_;
266 int ActorExt::set_bsend_buffer(void* buf, int size)
268 if(buf!=nullptr && bsend_buffer_!=nullptr)
269 return MPI_ERR_BUFFER;
271 bsend_buffer_size_= size;
275 } // namespace simgrid::smpi