1 /* Copyright (c) 2009-2021. 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"
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 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 void ActorExt::set_replaying(bool value)
104 if (state_ != SmpiProcessState::FINALIZED)
108 bool ActorExt::replaying() const
113 s4u::ActorPtr ActorExt::get_actor()
119 * @brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
121 * @see smpi_trace_set_call_location
123 smpi_trace_call_location_t* ActorExt::call_location()
125 return &trace_call_loc_;
128 void ActorExt::set_privatized_region(smpi_privatization_region_t region)
130 privatized_region_ = region;
133 smpi_privatization_region_t ActorExt::privatized_region() const
135 return privatized_region_;
138 MPI_Comm ActorExt::comm_world() const
140 return comm_world_ == nullptr ? MPI_COMM_NULL : *comm_world_;
143 s4u::MutexPtr ActorExt::mailboxes_mutex() const
145 return mailboxes_mutex_;
149 int ActorExt::papi_event_set() const
151 return papi_event_set_;
154 papi_counter_t& ActorExt::papi_counters()
156 return papi_counter_data_;
160 xbt_os_timer_t ActorExt::timer()
165 void ActorExt::simulated_start()
167 simulated_ = s4u::Engine::get_clock();
170 double ActorExt::simulated_elapsed() const
172 return s4u::Engine::get_clock() - simulated_;
175 MPI_Comm ActorExt::comm_self()
177 if (comm_self_ == MPI_COMM_NULL) {
178 auto* group = new Group(1);
179 comm_self_ = new Comm(group, nullptr);
180 comm_self_->set_name("MPI_COMM_SELF");
181 group->set_mapping(actor_->get_pid(), 0);
186 MPI_Info ActorExt::info_env()
188 if (info_env_==MPI_INFO_NULL)
189 info_env_=new Info();
193 MPI_Comm ActorExt::comm_intra()
198 void ActorExt::set_comm_intra(MPI_Comm comm)
203 void ActorExt::set_sampling(int s)
208 int ActorExt::sampling() const
213 void ActorExt::init()
215 xbt_assert(smpi_get_universe_size() != 0, "SimGrid was not initialized properly before entering MPI_Init. "
216 "Aborting, please check compilation process and use smpirun.");
218 ActorExt* ext = smpi_process();
219 // if we are in MPI_Init and argc handling has already been done.
220 if (ext->initialized())
223 const simgrid::s4u::Actor* self = simgrid::s4u::Actor::self();
224 ext->instance_id_ = self->get_property("instance_id");
225 const int rank = xbt_str_parse_int(self->get_property("rank"), "Cannot parse rank");
227 ext->state_ = SmpiProcessState::INITIALIZING;
228 smpi_deployment_register_process(ext->instance_id_, rank, self);
230 ext->comm_world_ = smpi_deployment_comm_world(ext->instance_id_);
232 // set the process attached to the mailbox
233 ext->mailbox_small_->set_receiver(ext->actor_);
234 XBT_DEBUG("<%ld> SMPI process has been initialized: %p", ext->actor_->get_pid(), ext->actor_);
237 int ActorExt::get_optind() const
242 void ActorExt::set_optind(int new_optind)
244 optind_ = new_optind;
247 void ActorExt::bsend_buffer(void** buf, int* size)
249 *buf = bsend_buffer_;
250 *size = bsend_buffer_size_;
253 int ActorExt::set_bsend_buffer(void* buf, int size)
255 if(buf!=nullptr && bsend_buffer_!=nullptr)
256 return MPI_ERR_BUFFER;
258 bsend_buffer_size_= size;
263 } // namespace simgrid