Logo AND Algorithmique Numérique Distribuée

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