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 "smpi_process.hpp"
8 #include "smpi_comm.hpp"
9 #include "src/mc/mc_replay.hpp"
10 #include "src/msg/msg_private.hpp"
11 #include "src/simix/smx_private.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
18 using simgrid::s4u::Actor;
19 using simgrid::s4u::ActorPtr;
21 Process::Process(ActorPtr actor, msg_bar_t finalization_barrier)
22 : finalization_barrier_(finalization_barrier), actor_(actor)
24 mailbox_ = simgrid::s4u::Mailbox::by_name("SMPI-" + std::to_string(actor_->get_pid()));
25 mailbox_small_ = simgrid::s4u::Mailbox::by_name("small-" + std::to_string(actor_->get_pid()));
26 mailboxes_mutex_ = xbt_mutex_init();
27 timer_ = xbt_os_timer_new();
28 state_ = SmpiProcessState::UNINITIALIZED;
30 MC_ignore_heap(timer_, xbt_os_timer_size());
33 if (simgrid::config::get_value<std::string>("smpi/papi-events")[0] != '\0') {
34 // TODO: Implement host/process/thread based counters. This implementation
35 // just always takes the values passed via "default", like this:
36 // "default:COUNTER1:COUNTER2:COUNTER3;".
37 auto it = units2papi_setup.find(papi_default_config_name);
38 if (it != units2papi_setup.end()) {
39 papi_event_set_ = it->second.event_set;
40 papi_counter_data_ = it->second.counter_data;
41 XBT_DEBUG("Setting PAPI set for process %i", i);
43 papi_event_set_ = PAPI_NULL;
44 XBT_DEBUG("No PAPI set for process %i", i);
52 if (comm_self_ != MPI_COMM_NULL)
53 simgrid::smpi::Comm::destroy(comm_self_);
54 if (comm_intra_ != MPI_COMM_NULL)
55 simgrid::smpi::Comm::destroy(comm_intra_);
56 xbt_os_timer_free(timer_);
57 xbt_mutex_destroy(mailboxes_mutex_);
60 void Process::set_data(int* argc, char*** argv)
62 instance_id_ = std::string((*argv)[1]);
63 comm_world_ = smpi_deployment_comm_world(instance_id_);
64 msg_bar_t barrier = smpi_deployment_finalization_barrier(instance_id_);
65 if (barrier != nullptr) // don't overwrite the current one if the instance has none
66 finalization_barrier_ = barrier;
68 actor_ = simgrid::s4u::Actor::self();
69 static_cast<simgrid::msg::ActorExt*>(actor_->get_impl()->get_user_data())->data = this;
72 memmove(&(*argv)[0], &(*argv)[2], sizeof(char*) * (*argc - 2));
73 (*argv)[(*argc) - 1] = nullptr;
74 (*argv)[(*argc) - 2] = nullptr;
79 // set the process attached to the mailbox
80 mailbox_small_->set_receiver(actor_);
81 XBT_DEBUG("<%ld> SMPI process has been initialized: %p", actor_->get_pid(), actor_.get());
84 /** @brief Prepares the current process for termination. */
85 void Process::finalize()
87 state_ = SmpiProcessState::FINALIZED;
88 XBT_DEBUG("<%ld> Process left the game", actor_->get_pid());
90 // This leads to an explosion of the search graph which cannot be reduced:
91 if(MC_is_active() || MC_record_replay_is_active())
93 // wait for all pending asynchronous comms to finish
94 MSG_barrier_wait(finalization_barrier_);
97 /** @brief Check if a process is finalized */
98 int Process::finalized()
100 return (state_ == SmpiProcessState::FINALIZED);
103 /** @brief Check if a process is initialized */
104 int Process::initialized()
106 // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
107 // single process ... ?
108 return (state_ == SmpiProcessState::INITIALIZED);
111 /** @brief Mark a process as initialized (=MPI_Init called) */
112 void Process::mark_as_initialized()
114 if (state_ != SmpiProcessState::FINALIZED)
115 state_ = SmpiProcessState::INITIALIZED;
118 void Process::set_replaying(bool value){
119 if (state_ != SmpiProcessState::FINALIZED)
123 bool Process::replaying(){
127 void Process::set_user_data(void *data)
132 void *Process::get_user_data()
137 ActorPtr Process::get_actor()
143 * \brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
145 * \see smpi_trace_set_call_location
147 smpi_trace_call_location_t* Process::call_location()
149 return &trace_call_loc_;
152 void Process::set_privatized_region(smpi_privatization_region_t region)
154 privatized_region_ = region;
157 smpi_privatization_region_t Process::privatized_region()
159 return privatized_region_;
162 MPI_Comm Process::comm_world()
164 return comm_world_==nullptr ? MPI_COMM_NULL : *comm_world_;
167 smx_mailbox_t Process::mailbox()
169 return mailbox_->get_impl();
172 smx_mailbox_t Process::mailbox_small()
174 return mailbox_small_->get_impl();
177 xbt_mutex_t Process::mailboxes_mutex()
179 return mailboxes_mutex_;
183 int Process::papi_event_set()
185 return papi_event_set_;
188 papi_counter_t& Process::papi_counters()
190 return papi_counter_data_;
194 xbt_os_timer_t Process::timer()
199 void Process::simulated_start()
201 simulated_ = SIMIX_get_clock();
204 double Process::simulated_elapsed()
206 return SIMIX_get_clock() - simulated_;
209 MPI_Comm Process::comm_self()
211 if(comm_self_==MPI_COMM_NULL){
212 MPI_Group group = new Group(1);
213 comm_self_ = new Comm(group, nullptr);
214 group->set_mapping(actor_, 0);
219 MPI_Comm Process::comm_intra()
224 void Process::set_comm_intra(MPI_Comm comm)
229 void Process::set_sampling(int s)
234 int Process::sampling()
239 void Process::init(int *argc, char ***argv){
241 if (smpi_process_count() == 0) {
242 xbt_die("SimGrid was not initialized properly before entering MPI_Init. Aborting, please check compilation process and use smpirun\n");
244 if (argc != nullptr && argv != nullptr) {
245 simgrid::s4u::ActorPtr proc = simgrid::s4u::Actor::self();
246 proc->get_impl()->context_->set_cleanup(&MSG_process_cleanup_from_SIMIX);
248 char* instance_id = (*argv)[1];
250 int rank = std::stoi(std::string((*argv)[2]));
251 smpi_deployment_register_process(instance_id, rank, proc);
252 } catch (std::invalid_argument& ia) {
253 throw std::invalid_argument(std::string("Invalid rank: ") + (*argv)[2]);
256 // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
257 // this up here so that I can set the privatized region before the switch.
258 Process* process = smpi_process_remote(proc);
259 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
260 /* Now using the segment index of this process */
261 process->set_privatized_region(smpi_init_global_memory_segment_process());
262 /* Done at the process's creation */
263 SMPI_switch_data_segment(proc);
266 process->set_data(argc, argv);
268 xbt_assert(smpi_process(), "smpi_process() returned nullptr. You probably gave a nullptr parameter to MPI_Init. "
269 "Although it's required by MPI-2, this is currently not supported by SMPI. "
270 "Please use MPI_Init(&argc, &argv) as usual instead.");
273 int Process::get_optind(){
276 void Process::set_optind(int new_optind){