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 "smpi_comm.hpp"
10 #include "smpi_group.hpp"
11 #include "src/mc/mc_replay.hpp"
12 #include "src/msg/msg_private.hpp"
13 #include "src/simix/smx_private.hpp"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
17 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
19 static char *get_mailbox_name(char *str, int index)
21 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", static_cast<int>(sizeof(int) * 2), static_cast<unsigned>(index));
25 static char *get_mailbox_name_small(char *str, int index)
27 snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", static_cast<int>(sizeof(int) * 2), static_cast<unsigned>(index));
34 using simgrid::s4u::ActorPtr;
36 Process::Process(ActorPtr actor, msg_bar_t finalization_barrier)
37 : finalization_barrier_(finalization_barrier)
39 char name[MAILBOX_NAME_MAXLEN];
41 int index = actor->getPid() - 1; // TODO cheinrich: This needs to be removed! Just a quick hack to make the following 2 lines work
42 mailbox_ = simgrid::s4u::Mailbox::byName(get_mailbox_name(name, index));
43 mailbox_small_ = simgrid::s4u::Mailbox::byName(get_mailbox_name_small(name, index));
44 mailboxes_mutex_ = xbt_mutex_init();
45 timer_ = xbt_os_timer_new();
46 state_ = SMPI_UNINITIALIZED;
48 MC_ignore_heap(timer_, xbt_os_timer_size());
51 if (xbt_cfg_get_string("smpi/papi-events")[0] != '\0') {
52 // TODO: Implement host/process/thread based counters. This implementation
53 // just always takes the values passed via "default", like this:
54 // "default:COUNTER1:COUNTER2:COUNTER3;".
55 auto it = units2papi_setup.find(papi_default_config_name);
56 if (it != units2papi_setup.end()) {
57 papi_event_set_ = it->second.event_set;
58 papi_counter_data_ = it->second.counter_data;
59 XBT_DEBUG("Setting PAPI set for process %i", i);
61 papi_event_set_ = PAPI_NULL;
62 XBT_DEBUG("No PAPI set for process %i", i);
68 void Process::set_data(int index, int* argc, char*** argv)
70 char* instance_id = (*argv)[1];
71 comm_world_ = smpi_deployment_comm_world(instance_id);
72 msg_bar_t barrier = smpi_deployment_finalization_barrier(instance_id);
73 if (barrier != nullptr) // don't overwrite the current one if the instance has none
74 finalization_barrier_ = barrier;
75 instance_id_ = instance_id;
78 process_ = simgrid::s4u::Actor::self();
79 static_cast<simgrid::msg::ActorExt*>(process_->getImpl()->userdata)->data = this;
82 memmove(&(*argv)[0], &(*argv)[2], sizeof(char *) * (*argc - 2));
83 (*argv)[(*argc) - 1] = nullptr;
84 (*argv)[(*argc) - 2] = nullptr;
89 // set the process attached to the mailbox
90 mailbox_small_->setReceiver(simgrid::s4u::Actor::self());
91 XBT_DEBUG("<%d> New process in the game: %p", index_, process_);
94 /** @brief Prepares the current process for termination. */
95 void Process::finalize()
97 state_ = SMPI_FINALIZED;
98 XBT_DEBUG("<%d> Process left the game", index_);
100 // This leads to an explosion of the search graph which cannot be reduced:
101 if(MC_is_active() || MC_record_replay_is_active())
103 // wait for all pending asynchronous comms to finish
104 MSG_barrier_wait(finalization_barrier_);
107 /** @brief Check if a process is finalized */
108 int Process::finalized()
110 if (index_ != MPI_UNDEFINED)
111 return (state_ == SMPI_FINALIZED);
116 /** @brief Check if a process is initialized */
117 int Process::initialized()
119 // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
120 // single process ... ?
121 return ((index_ != MPI_UNDEFINED) && (state_ == SMPI_INITIALIZED));
124 /** @brief Mark a process as initialized (=MPI_Init called) */
125 void Process::mark_as_initialized()
127 if ((index_ != MPI_UNDEFINED) && (state_ != SMPI_FINALIZED))
128 state_ = SMPI_INITIALIZED;
131 void Process::set_replaying(bool value){
132 if ((index_ != MPI_UNDEFINED) && (state_ != SMPI_FINALIZED))
136 bool Process::replaying(){
137 if (index_ != MPI_UNDEFINED)
143 void Process::set_user_data(void *data)
148 void *Process::get_user_data()
153 ActorPtr Process::process(){
158 * \brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
160 * \see smpi_trace_set_call_location
162 smpi_trace_call_location_t* Process::call_location()
164 return &trace_call_loc_;
167 void Process::set_privatized_region(smpi_privatization_region_t region)
169 privatized_region_ = region;
172 smpi_privatization_region_t Process::privatized_region()
174 return privatized_region_;
182 MPI_Comm Process::comm_world()
184 return comm_world_==nullptr ? MPI_COMM_NULL : *comm_world_;
187 smx_mailbox_t Process::mailbox()
189 return mailbox_->getImpl();
192 smx_mailbox_t Process::mailbox_small()
194 return mailbox_small_->getImpl();
197 xbt_mutex_t Process::mailboxes_mutex()
199 return mailboxes_mutex_;
203 int Process::papi_event_set()
205 return papi_event_set_;
208 papi_counter_t& smpi_process_papi_counters()
210 return papi_counter_data_;
214 xbt_os_timer_t Process::timer()
219 void Process::simulated_start()
221 simulated_ = SIMIX_get_clock();
224 double Process::simulated_elapsed()
226 return SIMIX_get_clock() - simulated_;
229 MPI_Comm Process::comm_self()
231 if(comm_self_==MPI_COMM_NULL){
232 MPI_Group group = new Group(1);
233 comm_self_ = new Comm(group, nullptr);
234 group->set_mapping(process_, 0);
239 MPI_Comm Process::comm_intra()
244 void Process::set_comm_intra(MPI_Comm comm)
249 void Process::set_sampling(int s)
254 int Process::sampling()
259 msg_bar_t Process::finalization_barrier(){
260 return finalization_barrier_;
263 int Process::return_value(){
264 return return_value_;
267 void Process::set_return_value(int val){
271 void Process::init(int *argc, char ***argv){
273 if (smpi_process_count() == 0) {
274 xbt_die("SimGrid was not initialized properly before entering MPI_Init. Aborting, please check compilation process and use smpirun\n");
276 if (argc != nullptr && argv != nullptr) {
277 simgrid::s4u::ActorPtr proc = simgrid::s4u::Actor::self();
278 proc->getImpl()->context->set_cleanup(&MSG_process_cleanup_from_SIMIX);
280 int index = proc->getPid() - 1; // The maestro process has always ID 0 but we don't need that process here
282 char* instance_id = (*argv)[1];
284 int rank = std::stoi(std::string((*argv)[2]));
285 smpi_deployment_register_process(instance_id, rank, index);
286 } catch (std::invalid_argument& ia) {
287 throw std::invalid_argument(std::string("Invalid rank: ") + (*argv)[2]);
290 // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
291 // this up here so that I can set the privatized region before the switch.
292 Process* process = smpi_process_remote(index);
293 if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){
294 /* Now using the segment index of this process */
295 index = proc->getImpl()->segment_index;
296 process->set_privatized_region(smpi_init_global_memory_segment_process());
297 /* Done at the process's creation */
298 SMPI_switch_data_segment(index);
301 process->set_data(index, argc, argv);
303 xbt_assert(smpi_process(),
304 "smpi_process() returned nullptr. You probably gave a nullptr parameter to MPI_Init. "
305 "Although it's required by MPI-2, this is currently not supported by SMPI.");