Logo AND Algorithmique Numérique Distribuée

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