Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
36f01638bbd56c0e5fa4c356eac82e3bb2d456a1
[simgrid.git] / src / smpi / internals / smpi_process.cpp
1 /* Copyright (c) 2009-2017. 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 "private.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"
15 #include <sstream>
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
18
19 namespace simgrid{
20 namespace smpi{
21
22 using simgrid::s4u::Actor;
23 using simgrid::s4u::ActorPtr;
24
25 Process::Process(ActorPtr actor, msg_bar_t finalization_barrier)
26     : finalization_barrier_(finalization_barrier), process_(actor)
27 {
28   std::stringstream mailboxname;
29   std::stringstream mailboxname_small;
30
31   mailboxname           << std::string("SMPI-")  << process_->getPid();
32   mailboxname_small     << std::string("small-") << process_->getPid();
33   mailbox_              = simgrid::s4u::Mailbox::byName(mailboxname.str());
34   mailbox_small_        = simgrid::s4u::Mailbox::byName(mailboxname_small.str());
35   mailboxes_mutex_      = xbt_mutex_init();
36   timer_                = xbt_os_timer_new();
37   state_                = SMPI_UNINITIALIZED;
38   if (MC_is_active())
39     MC_ignore_heap(timer_, xbt_os_timer_size());
40
41 #if HAVE_PAPI
42   if (xbt_cfg_get_string("smpi/papi-events")[0] != '\0') {
43     // TODO: Implement host/process/thread based counters. This implementation
44     // just always takes the values passed via "default", like this:
45     // "default:COUNTER1:COUNTER2:COUNTER3;".
46     auto it = units2papi_setup.find(papi_default_config_name);
47     if (it != units2papi_setup.end()) {
48       papi_event_set_    = it->second.event_set;
49       papi_counter_data_ = it->second.counter_data;
50       XBT_DEBUG("Setting PAPI set for process %i", i);
51     } else {
52       papi_event_set_ = PAPI_NULL;
53       XBT_DEBUG("No PAPI set for process %i", i);
54     }
55   }
56 #endif
57 }
58
59 Process::~Process()
60 {
61   xbt_os_timer_free(timer_);
62   xbt_mutex_destroy(mailboxes_mutex_);
63 }
64
65 void Process::set_data(int* argc, char*** argv)
66 {
67   instance_id_      = std::string((*argv)[1]);
68   comm_world_       = smpi_deployment_comm_world(instance_id_.c_str());
69   msg_bar_t barrier = smpi_deployment_finalization_barrier(instance_id_.c_str());
70   if (barrier != nullptr) // don't overwrite the current one if the instance has none
71     finalization_barrier_ = barrier;
72
73   process_                                                                  = simgrid::s4u::Actor::self();
74   static_cast<simgrid::msg::ActorExt*>(process_->getImpl()->userdata)->data = this;
75
76   if (*argc > 3) {
77     memmove(&(*argv)[0], &(*argv)[2], sizeof(char*) * (*argc - 2));
78     (*argv)[(*argc) - 1] = nullptr;
79     (*argv)[(*argc) - 2] = nullptr;
80   }
81   (*argc) -= 2;
82   argc_ = argc;
83   argv_ = argv;
84   // set the process attached to the mailbox
85   mailbox_small_->setReceiver(process_);
86   XBT_DEBUG("<%lu> New process in the game: %p", process_->getPid(), process_.get());
87 }
88
89 /** @brief Prepares the current process for termination. */
90 void Process::finalize()
91 {
92   state_ = SMPI_FINALIZED;
93   XBT_DEBUG("<%lu> Process left the game", process_->getPid());
94
95   // This leads to an explosion of the search graph which cannot be reduced:
96   if(MC_is_active() || MC_record_replay_is_active())
97     return;
98   // wait for all pending asynchronous comms to finish
99   MSG_barrier_wait(finalization_barrier_);
100 }
101
102 /** @brief Check if a process is finalized */
103 int Process::finalized()
104 {
105   return (state_ == SMPI_FINALIZED);
106 }
107
108 /** @brief Check if a process is initialized */
109 int Process::initialized()
110 {
111   // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
112   // single process ... ?
113   return (state_ == SMPI_INITIALIZED);
114 }
115
116 /** @brief Mark a process as initialized (=MPI_Init called) */
117 void Process::mark_as_initialized()
118 {
119   if (state_ != SMPI_FINALIZED)
120     state_ = SMPI_INITIALIZED;
121 }
122
123 void Process::set_replaying(bool value){
124   if (state_ != SMPI_FINALIZED)
125     replaying_ = value;
126 }
127
128 bool Process::replaying(){
129   return replaying_;
130 }
131
132 void Process::set_user_data(void *data)
133 {
134   data_ = data;
135 }
136
137 void *Process::get_user_data()
138 {
139   return data_;
140 }
141
142 ActorPtr Process::process(){
143   return process_;
144 }
145
146 /**
147  * \brief Returns a structure that stores the location (filename + linenumber) of the last calls to MPI_* functions.
148  *
149  * \see smpi_trace_set_call_location
150  */
151 smpi_trace_call_location_t* Process::call_location()
152 {
153   return &trace_call_loc_;
154 }
155
156 void Process::set_privatized_region(smpi_privatization_region_t region)
157 {
158   privatized_region_ = region;
159 }
160
161 smpi_privatization_region_t Process::privatized_region()
162 {
163   return privatized_region_;
164 }
165
166 MPI_Comm Process::comm_world()
167 {
168   return comm_world_==nullptr ? MPI_COMM_NULL : *comm_world_;
169 }
170
171 smx_mailbox_t Process::mailbox()
172 {
173   return mailbox_->getImpl();
174 }
175
176 smx_mailbox_t Process::mailbox_small()
177 {
178   return mailbox_small_->getImpl();
179 }
180
181 xbt_mutex_t Process::mailboxes_mutex()
182 {
183   return mailboxes_mutex_;
184 }
185
186 #if HAVE_PAPI
187 int Process::papi_event_set()
188 {
189   return papi_event_set_;
190 }
191
192 papi_counter_t& smpi_process_papi_counters()
193 {
194   return papi_counter_data_;
195 }
196 #endif
197
198 xbt_os_timer_t Process::timer()
199 {
200   return timer_;
201 }
202
203 void Process::simulated_start()
204 {
205   simulated_ = SIMIX_get_clock();
206 }
207
208 double Process::simulated_elapsed()
209 {
210   return SIMIX_get_clock() - simulated_;
211 }
212
213 MPI_Comm Process::comm_self()
214 {
215   if(comm_self_==MPI_COMM_NULL){
216     MPI_Group group = new  Group(1);
217     comm_self_ = new  Comm(group, nullptr);
218     group->set_mapping(process_, 0);
219   }
220   return comm_self_;
221 }
222
223 MPI_Comm Process::comm_intra()
224 {
225   return comm_intra_;
226 }
227
228 void Process::set_comm_intra(MPI_Comm comm)
229 {
230   comm_intra_ = comm;
231 }
232
233 void Process::set_sampling(int s)
234 {
235   sampling_ = s;
236 }
237
238 int Process::sampling()
239 {
240   return sampling_;
241 }
242
243 msg_bar_t Process::finalization_barrier(){
244   return finalization_barrier_;
245 }
246
247 int Process::return_value(){
248   return return_value_;
249 }
250
251 void Process::set_return_value(int val){
252   return_value_=val;
253 }
254
255 void Process::init(int *argc, char ***argv){
256
257   if (smpi_process_count() == 0) {
258     xbt_die("SimGrid was not initialized properly before entering MPI_Init. Aborting, please check compilation process and use smpirun\n");
259   }
260   if (argc != nullptr && argv != nullptr) {
261     simgrid::s4u::ActorPtr proc = simgrid::s4u::Actor::self();
262     proc->getImpl()->context->set_cleanup(&MSG_process_cleanup_from_SIMIX);
263
264     char* instance_id = (*argv)[1];
265     try {
266       int rank = std::stoi(std::string((*argv)[2]));
267       smpi_deployment_register_process(instance_id, rank, proc);
268     } catch (std::invalid_argument& ia) {
269       throw std::invalid_argument(std::string("Invalid rank: ") + (*argv)[2]);
270     }
271
272     // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
273     // this up here so that I can set the privatized region before the switch.
274     Process* process = smpi_process_remote(proc);
275     int my_proc_id   = proc->getPid();
276     if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){
277       /* Now using the segment index of this process  */
278       process->set_privatized_region(smpi_init_global_memory_segment_process());
279       /* Done at the process's creation */
280       SMPI_switch_data_segment(my_proc_id);
281     }
282
283     process->set_data(argc, argv);
284   }
285   xbt_assert(smpi_process(),
286       "smpi_process() returned nullptr. You probably gave a nullptr parameter to MPI_Init. "
287       "Although it's required by MPI-2, this is currently not supported by SMPI.");
288 }
289
290 }
291 }