Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove a dependency on MSG from SMPI
[simgrid.git] / src / smpi / internals / smpi_process.cpp
1 /* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "smpi_process.hpp"
7 #include "mc/mc.h"
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"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
14
15 namespace simgrid{
16 namespace smpi{
17
18 using simgrid::s4u::Actor;
19 using simgrid::s4u::ActorPtr;
20
21 Process::Process(ActorPtr actor, simgrid::s4u::Barrier* finalization_barrier)
22     : finalization_barrier_(finalization_barrier), actor_(actor)
23 {
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;
29   if (MC_is_active())
30     MC_ignore_heap(timer_, xbt_os_timer_size());
31
32 #if HAVE_PAPI
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);
42     } else {
43       papi_event_set_ = PAPI_NULL;
44       XBT_DEBUG("No PAPI set for process %i", i);
45     }
46   }
47 #endif
48 }
49
50 Process::~Process()
51 {
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_);
58 }
59
60 void Process::set_data(int* argc, char*** argv)
61 {
62   instance_id_      = std::string((*argv)[1]);
63   comm_world_       = smpi_deployment_comm_world(instance_id_);
64   simgrid::s4u::Barrier* 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;
67
68   actor_                                                                        = simgrid::s4u::Actor::self();
69   static_cast<simgrid::msg::ActorExt*>(actor_->get_impl()->get_user_data())->data = this;
70
71   if (*argc > 3) {
72     memmove(&(*argv)[0], &(*argv)[2], sizeof(char*) * (*argc - 2));
73     (*argv)[(*argc) - 1] = nullptr;
74     (*argv)[(*argc) - 2] = nullptr;
75   }
76   (*argc) -= 2;
77   argc_ = argc;
78   argv_ = argv;
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());
82 }
83
84 /** @brief Prepares the current process for termination. */
85 void Process::finalize()
86 {
87   state_ = SmpiProcessState::FINALIZED;
88   XBT_DEBUG("<%ld> Process left the game", actor_->get_pid());
89
90   // This leads to an explosion of the search graph which cannot be reduced:
91   if(MC_is_active() || MC_record_replay_is_active())
92     return;
93   // wait for all pending asynchronous comms to finish
94   finalization_barrier_->wait();
95 }
96
97 /** @brief Check if a process is finalized */
98 int Process::finalized()
99 {
100   return (state_ == SmpiProcessState::FINALIZED);
101 }
102
103 /** @brief Check if a process is initialized */
104 int Process::initialized()
105 {
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);
109 }
110
111 /** @brief Mark a process as initialized (=MPI_Init called) */
112 void Process::mark_as_initialized()
113 {
114   if (state_ != SmpiProcessState::FINALIZED)
115     state_ = SmpiProcessState::INITIALIZED;
116 }
117
118 void Process::set_replaying(bool value){
119   if (state_ != SmpiProcessState::FINALIZED)
120     replaying_ = value;
121 }
122
123 bool Process::replaying(){
124   return replaying_;
125 }
126
127 void Process::set_user_data(void *data)
128 {
129   data_ = data;
130 }
131
132 void *Process::get_user_data()
133 {
134   return data_;
135 }
136
137 ActorPtr Process::get_actor()
138 {
139   return actor_;
140 }
141
142 /**
143  * \brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
144  *
145  * \see smpi_trace_set_call_location
146  */
147 smpi_trace_call_location_t* Process::call_location()
148 {
149   return &trace_call_loc_;
150 }
151
152 void Process::set_privatized_region(smpi_privatization_region_t region)
153 {
154   privatized_region_ = region;
155 }
156
157 smpi_privatization_region_t Process::privatized_region()
158 {
159   return privatized_region_;
160 }
161
162 MPI_Comm Process::comm_world()
163 {
164   return comm_world_==nullptr ? MPI_COMM_NULL : *comm_world_;
165 }
166
167 smx_mailbox_t Process::mailbox()
168 {
169   return mailbox_->get_impl();
170 }
171
172 smx_mailbox_t Process::mailbox_small()
173 {
174   return mailbox_small_->get_impl();
175 }
176
177 xbt_mutex_t Process::mailboxes_mutex()
178 {
179   return mailboxes_mutex_;
180 }
181
182 #if HAVE_PAPI
183 int Process::papi_event_set()
184 {
185   return papi_event_set_;
186 }
187
188 papi_counter_t& Process::papi_counters()
189 {
190   return papi_counter_data_;
191 }
192 #endif
193
194 xbt_os_timer_t Process::timer()
195 {
196   return timer_;
197 }
198
199 void Process::simulated_start()
200 {
201   simulated_ = SIMIX_get_clock();
202 }
203
204 double Process::simulated_elapsed()
205 {
206   return SIMIX_get_clock() - simulated_;
207 }
208
209 MPI_Comm Process::comm_self()
210 {
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);
215   }
216   return comm_self_;
217 }
218
219 MPI_Comm Process::comm_intra()
220 {
221   return comm_intra_;
222 }
223
224 void Process::set_comm_intra(MPI_Comm comm)
225 {
226   comm_intra_ = comm;
227 }
228
229 void Process::set_sampling(int s)
230 {
231   sampling_ = s;
232 }
233
234 int Process::sampling()
235 {
236   return sampling_;
237 }
238
239 void Process::init(int *argc, char ***argv){
240
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");
243   }
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);
247
248     char* instance_id = (*argv)[1];
249     try {
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]);
254     }
255
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);
264     }
265
266     process->set_data(argc, argv);
267   }
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.");
271 }
272
273 int Process::get_optind(){
274   return optind;
275 }
276 void Process::set_optind(int new_optind){
277   optind=new_optind;
278 }
279
280 }
281 }