1 /* Copyright (c) 2009-2017. 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)");
18 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
20 static char* get_mailbox_name(char* str, int process_id)
22 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", static_cast<int>(sizeof(int) * 2), static_cast<unsigned>(process_id));
26 static char* get_mailbox_name_small(char* str, int process_id)
28 snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", static_cast<int>(sizeof(int) * 2), static_cast<unsigned>(process_id));
35 using simgrid::s4u::Actor;
36 using simgrid::s4u::ActorPtr;
38 Process::Process(ActorPtr actor, msg_bar_t finalization_barrier)
39 : finalization_barrier_(finalization_barrier)
41 char name[MAILBOX_NAME_MAXLEN];
43 mailbox_ = simgrid::s4u::Mailbox::byName(get_mailbox_name(name, process_->getPid()));
44 mailbox_small_ = simgrid::s4u::Mailbox::byName(get_mailbox_name_small(name, process_->getPid()));
45 mailboxes_mutex_ = xbt_mutex_init();
46 timer_ = xbt_os_timer_new();
47 state_ = SMPI_UNINITIALIZED;
49 MC_ignore_heap(timer_, xbt_os_timer_size());
52 if (xbt_cfg_get_string("smpi/papi-events")[0] != '\0') {
53 // TODO: Implement host/process/thread based counters. This implementation
54 // just always takes the values passed via "default", like this:
55 // "default:COUNTER1:COUNTER2:COUNTER3;".
56 auto it = units2papi_setup.find(papi_default_config_name);
57 if (it != units2papi_setup.end()) {
58 papi_event_set_ = it->second.event_set;
59 papi_counter_data_ = it->second.counter_data;
60 XBT_DEBUG("Setting PAPI set for process %i", i);
62 papi_event_set_ = PAPI_NULL;
63 XBT_DEBUG("No PAPI set for process %i", i);
69 void Process::set_data(int* argc, char*** argv)
71 instance_id_ = std::string((*argv)[1]);
72 comm_world_ = smpi_deployment_comm_world(instance_id_.c_str());
73 msg_bar_t barrier = smpi_deployment_finalization_barrier(instance_id_.c_str());
74 if (barrier != nullptr) // don't overwrite the current one if the instance has none
75 finalization_barrier_ = barrier;
77 process_ = simgrid::s4u::Actor::self();
78 static_cast<simgrid::msg::ActorExt*>(process_->getImpl()->userdata)->data = this;
81 memmove(&(*argv)[0], &(*argv)[2], sizeof(char*) * (*argc - 2));
82 (*argv)[(*argc) - 1] = nullptr;
83 (*argv)[(*argc) - 2] = nullptr;
88 // set the process attached to the mailbox
89 mailbox_small_->setReceiver(simgrid::s4u::Actor::self());
90 XBT_DEBUG("<%lu> New process in the game: %p", process_->getPid(), process_);
93 /** @brief Prepares the current process for termination. */
94 void Process::finalize()
96 state_ = SMPI_FINALIZED;
97 XBT_DEBUG("<%lu> Process left the game", process_->getPid());
99 // This leads to an explosion of the search graph which cannot be reduced:
100 if(MC_is_active() || MC_record_replay_is_active())
102 // wait for all pending asynchronous comms to finish
103 MSG_barrier_wait(finalization_barrier_);
106 /** @brief Check if a process is finalized */
107 int Process::finalized()
109 return (state_ == SMPI_FINALIZED);
112 /** @brief Check if a process is initialized */
113 int Process::initialized()
115 // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
116 // single process ... ?
117 return (state_ == SMPI_INITIALIZED);
120 /** @brief Mark a process as initialized (=MPI_Init called) */
121 void Process::mark_as_initialized()
123 if (state_ != SMPI_FINALIZED)
124 state_ = SMPI_INITIALIZED;
127 void Process::set_replaying(bool value){
128 if (state_ != SMPI_FINALIZED)
132 bool Process::replaying(){
136 void Process::set_user_data(void *data)
141 void *Process::get_user_data()
146 ActorPtr Process::process(){
151 * \brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
153 * \see smpi_trace_set_call_location
155 smpi_trace_call_location_t* Process::call_location()
157 return &trace_call_loc_;
160 void Process::set_privatized_region(smpi_privatization_region_t region)
162 privatized_region_ = region;
165 smpi_privatization_region_t Process::privatized_region()
167 return privatized_region_;
170 MPI_Comm Process::comm_world()
172 return comm_world_==nullptr ? MPI_COMM_NULL : *comm_world_;
175 smx_mailbox_t Process::mailbox()
177 return mailbox_->getImpl();
180 smx_mailbox_t Process::mailbox_small()
182 return mailbox_small_->getImpl();
185 xbt_mutex_t Process::mailboxes_mutex()
187 return mailboxes_mutex_;
191 int Process::papi_event_set()
193 return papi_event_set_;
196 papi_counter_t& smpi_process_papi_counters()
198 return papi_counter_data_;
202 xbt_os_timer_t Process::timer()
207 void Process::simulated_start()
209 simulated_ = SIMIX_get_clock();
212 double Process::simulated_elapsed()
214 return SIMIX_get_clock() - simulated_;
217 MPI_Comm Process::comm_self()
219 if(comm_self_==MPI_COMM_NULL){
220 MPI_Group group = new Group(1);
221 comm_self_ = new Comm(group, nullptr);
222 group->set_mapping(process_, 0);
227 MPI_Comm Process::comm_intra()
232 void Process::set_comm_intra(MPI_Comm comm)
237 void Process::set_sampling(int s)
242 int Process::sampling()
247 msg_bar_t Process::finalization_barrier(){
248 return finalization_barrier_;
251 int Process::return_value(){
252 return return_value_;
255 void Process::set_return_value(int val){
259 void Process::init(int *argc, char ***argv){
261 if (smpi_process_count() == 0) {
262 xbt_die("SimGrid was not initialized properly before entering MPI_Init. Aborting, please check compilation process and use smpirun\n");
264 if (argc != nullptr && argv != nullptr) {
265 simgrid::s4u::ActorPtr proc = simgrid::s4u::Actor::self();
266 proc->getImpl()->context->set_cleanup(&MSG_process_cleanup_from_SIMIX);
268 char* instance_id = (*argv)[1];
270 int rank = std::stoi(std::string((*argv)[2]));
271 smpi_deployment_register_process(instance_id, rank, proc);
272 } catch (std::invalid_argument& ia) {
273 throw std::invalid_argument(std::string("Invalid rank: ") + (*argv)[2]);
276 // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
277 // this up here so that I can set the privatized region before the switch.
278 Process* process = smpi_process_remote(proc);
279 int my_proc_id = proc->getPid();
280 if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){
281 /* Now using the segment index of this process */
282 process->set_privatized_region(smpi_init_global_memory_segment_process());
283 /* Done at the process's creation */
284 SMPI_switch_data_segment(my_proc_id);
287 process->set_data(argc, argv);
289 xbt_assert(smpi_process(),
290 "smpi_process() returned nullptr. You probably gave a nullptr parameter to MPI_Init. "
291 "Although it's required by MPI-2, this is currently not supported by SMPI.");