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"
9 #include "simgrid/s4u/forward.hpp"
10 #include "smpi_comm.hpp"
11 #include "smpi_group.hpp"
12 #include "src/mc/mc_replay.hpp"
13 #include "src/msg/msg_private.hpp"
14 #include "src/simix/smx_private.hpp"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
21 using simgrid::s4u::Actor;
22 using simgrid::s4u::ActorPtr;
24 Process::Process(ActorPtr actor, msg_bar_t finalization_barrier)
25 : finalization_barrier_(finalization_barrier), process_(actor)
27 mailbox_ = simgrid::s4u::Mailbox::byName("SMPI-" + std::to_string(process_->getPid()));
28 mailbox_small_ = simgrid::s4u::Mailbox::byName("small-" + std::to_string(process_->getPid()));
29 mailboxes_mutex_ = xbt_mutex_init();
30 timer_ = xbt_os_timer_new();
31 state_ = SMPI_UNINITIALIZED;
33 MC_ignore_heap(timer_, xbt_os_timer_size());
36 if (xbt_cfg_get_string("smpi/papi-events")[0] != '\0') {
37 // TODO: Implement host/process/thread based counters. This implementation
38 // just always takes the values passed via "default", like this:
39 // "default:COUNTER1:COUNTER2:COUNTER3;".
40 auto it = units2papi_setup.find(papi_default_config_name);
41 if (it != units2papi_setup.end()) {
42 papi_event_set_ = it->second.event_set;
43 papi_counter_data_ = it->second.counter_data;
44 XBT_DEBUG("Setting PAPI set for process %i", i);
46 papi_event_set_ = PAPI_NULL;
47 XBT_DEBUG("No PAPI set for process %i", i);
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_);
60 xbt_mutex_destroy(mailboxes_mutex_);
63 void Process::set_data(int* argc, char*** argv)
65 instance_id_ = std::string((*argv)[1]);
66 comm_world_ = smpi_deployment_comm_world(instance_id_);
67 msg_bar_t barrier = smpi_deployment_finalization_barrier(instance_id_);
68 if (barrier != nullptr) // don't overwrite the current one if the instance has none
69 finalization_barrier_ = barrier;
71 process_ = simgrid::s4u::Actor::self();
72 static_cast<simgrid::msg::ActorExt*>(process_->getImpl()->userdata)->data = this;
75 memmove(&(*argv)[0], &(*argv)[2], sizeof(char*) * (*argc - 2));
76 (*argv)[(*argc) - 1] = nullptr;
77 (*argv)[(*argc) - 2] = nullptr;
82 // set the process attached to the mailbox
83 mailbox_small_->setReceiver(process_);
84 XBT_DEBUG("<%ld> SMPI process has been initialized: %p", process_->getPid(), process_.get());
87 /** @brief Prepares the current process for termination. */
88 void Process::finalize()
90 state_ = SMPI_FINALIZED;
91 XBT_DEBUG("<%ld> Process left the game", process_->getPid());
93 // This leads to an explosion of the search graph which cannot be reduced:
94 if(MC_is_active() || MC_record_replay_is_active())
96 // wait for all pending asynchronous comms to finish
97 MSG_barrier_wait(finalization_barrier_);
100 /** @brief Check if a process is finalized */
101 int Process::finalized()
103 return (state_ == SMPI_FINALIZED);
106 /** @brief Check if a process is initialized */
107 int Process::initialized()
109 // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
110 // single process ... ?
111 return (state_ == SMPI_INITIALIZED);
114 /** @brief Mark a process as initialized (=MPI_Init called) */
115 void Process::mark_as_initialized()
117 if (state_ != SMPI_FINALIZED)
118 state_ = SMPI_INITIALIZED;
121 void Process::set_replaying(bool value){
122 if (state_ != SMPI_FINALIZED)
126 bool Process::replaying(){
130 void Process::set_user_data(void *data)
135 void *Process::get_user_data()
140 ActorPtr Process::process(){
145 * \brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
147 * \see smpi_trace_set_call_location
149 smpi_trace_call_location_t* Process::call_location()
151 return &trace_call_loc_;
154 void Process::set_privatized_region(smpi_privatization_region_t region)
156 privatized_region_ = region;
159 smpi_privatization_region_t Process::privatized_region()
161 return privatized_region_;
164 MPI_Comm Process::comm_world()
166 return comm_world_==nullptr ? MPI_COMM_NULL : *comm_world_;
169 smx_mailbox_t Process::mailbox()
171 return mailbox_->getImpl();
174 smx_mailbox_t Process::mailbox_small()
176 return mailbox_small_->getImpl();
179 xbt_mutex_t Process::mailboxes_mutex()
181 return mailboxes_mutex_;
185 int Process::papi_event_set()
187 return papi_event_set_;
190 papi_counter_t& Process::papi_counters()
192 return papi_counter_data_;
196 xbt_os_timer_t Process::timer()
201 void Process::simulated_start()
203 simulated_ = SIMIX_get_clock();
206 double Process::simulated_elapsed()
208 return SIMIX_get_clock() - simulated_;
211 MPI_Comm Process::comm_self()
213 if(comm_self_==MPI_COMM_NULL){
214 MPI_Group group = new Group(1);
215 comm_self_ = new Comm(group, nullptr);
216 group->set_mapping(process_, 0);
221 MPI_Comm Process::comm_intra()
226 void Process::set_comm_intra(MPI_Comm comm)
231 void Process::set_sampling(int s)
236 int Process::sampling()
241 msg_bar_t Process::finalization_barrier(){
242 return finalization_barrier_;
245 void Process::init(int *argc, char ***argv){
247 if (smpi_process_count() == 0) {
248 xbt_die("SimGrid was not initialized properly before entering MPI_Init. Aborting, please check compilation process and use smpirun\n");
250 if (argc != nullptr && argv != nullptr) {
251 simgrid::s4u::ActorPtr proc = simgrid::s4u::Actor::self();
252 proc->getImpl()->context->set_cleanup(&MSG_process_cleanup_from_SIMIX);
254 char* instance_id = (*argv)[1];
256 int rank = std::stoi(std::string((*argv)[2]));
257 smpi_deployment_register_process(instance_id, rank, proc);
258 } catch (std::invalid_argument& ia) {
259 throw std::invalid_argument(std::string("Invalid rank: ") + (*argv)[2]);
262 // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
263 // this up here so that I can set the privatized region before the switch.
264 Process* process = smpi_process_remote(proc);
265 if (smpi_privatize_global_variables == SmpiPrivStrategies::Mmap) {
266 /* Now using the segment index of this process */
267 process->set_privatized_region(smpi_init_global_memory_segment_process());
268 /* Done at the process's creation */
269 SMPI_switch_data_segment(proc);
272 process->set_data(argc, argv);
274 xbt_assert(smpi_process(), "smpi_process() returned nullptr. You probably gave a nullptr parameter to MPI_Init. "
275 "Although it's required by MPI-2, this is currently not supported by SMPI. "
276 "Please use MPI_Init(&argc, &argv) as usual instead.");