Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Introduce smpi::Process
[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 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     XBT_DEBUG("<%d> New process in the game: %p", index, SIMIX_process_self());
98 }
99
100 void Process::destroy()
101 {
102   if(smpi_privatize_global_variables){
103     smpi_switch_data_segment(index_);
104   }
105   state_ = SMPI_FINALIZED;
106   XBT_DEBUG("<%d> Process left the game", index_);
107 }
108
109 /** @brief Prepares the current process for termination. */
110 void Process::finalize()
111 {
112     // This leads to an explosion of the search graph which cannot be reduced:
113     if(MC_is_active() || MC_record_replay_is_active())
114       return;
115     // wait for all pending asynchronous comms to finish
116     MSG_barrier_wait(finalization_barrier_);
117 }
118
119 /** @brief Check if a process is finalized */
120 int Process::finalized()
121 {
122     if (index_ != MPI_UNDEFINED)
123       return (state_ == SMPI_FINALIZED);
124     else
125       return 0;
126 }
127
128 /** @brief Check if a process is initialized */
129 int Process::initialized()
130 {
131   if (index_to_process_data == nullptr){
132     return false;
133   } else{
134     return ((index_ != MPI_UNDEFINED) && (state_ == SMPI_INITIALIZED));
135   }
136 }
137
138 /** @brief Mark a process as initialized (=MPI_Init called) */
139 void Process::mark_as_initialized()
140 {
141   if ((index_ != MPI_UNDEFINED) && (state_ != SMPI_FINALIZED))
142     state_ = SMPI_INITIALIZED;
143 }
144
145 void Process::set_replaying(bool value){
146   if ((index_ != MPI_UNDEFINED) && (state_ != SMPI_FINALIZED))
147     replaying_ = value;
148 }
149
150 bool Process::replaying(){
151   if (index_ != MPI_UNDEFINED)
152     return replaying_;
153   else
154     return false;
155 }
156
157 void Process::set_user_data(void *data)
158 {
159   data_ = data;
160 }
161
162 void *Process::get_user_data()
163 {
164   return data_;
165 }
166
167
168 /**
169  * \brief Returns a structure that stores the location (filename + linenumber)
170  *        of the last calls to MPI_* functions.
171  *
172  * \see smpi_trace_set_call_location
173  */
174 smpi_trace_call_location_t* Process::call_location()
175 {
176   return &trace_call_loc_;
177 }
178
179 int Process::index()
180 {
181   return index_;
182 }
183
184 MPI_Comm Process::comm_world()
185 {
186   return comm_world_==nullptr ? MPI_COMM_NULL : *comm_world_;
187 }
188
189 smx_mailbox_t Process::mailbox()
190 {
191   return mailbox_->getImpl();
192 }
193
194 smx_mailbox_t Process::mailbox_small()
195 {
196   return mailbox_small_->getImpl();
197 }
198
199 xbt_mutex_t Process::mailboxes_mutex()
200 {
201   return mailboxes_mutex_;
202 }
203
204 #if HAVE_PAPI
205 int Process::papi_event_set(void)
206 {
207   return papi_event_set_;
208 }
209
210 papi_counter_t& smpi_process_papi_counters(void)
211 {
212   return papi_counter_data_;
213 }
214 #endif
215
216 xbt_os_timer_t Process::timer()
217 {
218   return timer_;
219 }
220
221 void Process::simulated_start()
222 {
223   simulated_ = SIMIX_get_clock();
224 }
225
226 double Process::simulated_elapsed()
227 {
228   return SIMIX_get_clock() - simulated_;
229 }
230
231 MPI_Comm Process::comm_self()
232 {
233   if(comm_self_==MPI_COMM_NULL){
234     MPI_Group group = new  Group(1);
235     comm_self_ = new  Comm(group, nullptr);
236     group->set_mapping(index_, 0);
237   }
238   return comm_self_;
239 }
240
241 MPI_Comm Process::comm_intra()
242 {
243   return comm_intra_;
244 }
245
246 void Process::set_comm_intra(MPI_Comm comm)
247 {
248   comm_intra_ = comm;
249 }
250
251 void Process::set_sampling(int s)
252 {
253   sampling_ = s;
254 }
255
256 int Process::sampling()
257 {
258   return sampling_;
259 }
260
261 void Process::set_finalization_barrier(msg_bar_t bar){
262   finalization_barrier_=bar;
263 }
264
265 msg_bar_t Process::finalization_barrier(){
266   return finalization_barrier_;
267 }
268
269 int Process::return_value(){
270   return return_value_;
271 }
272
273 void Process::set_return_value(int val){
274   return_value_=val;
275 }
276
277 void Process::init(int *argc, char ***argv){
278
279   if (process_data == nullptr){
280     printf("SimGrid was not initialized properly before entering MPI_Init. Aborting, please check compilation process and use smpirun\n");
281     exit(1);
282   }
283   if (argc != nullptr && argv != nullptr) {
284     smx_actor_t proc = SIMIX_process_self();
285     proc->context->set_cleanup(&MSG_process_cleanup_from_SIMIX);
286
287     int index = smpi_process_index_of_smx_process(proc);
288
289     if(index_to_process_data == nullptr){
290       index_to_process_data=static_cast<int*>(xbt_malloc(SIMIX_process_count()*sizeof(int)));
291     }
292
293     char* instance_id = (*argv)[1];
294     int rank = xbt_str_parse_int((*argv)[2], "Invalid rank: %s");
295     smpi_deployment_register_process(instance_id, rank, index);
296
297     if(smpi_privatize_global_variables){
298       /* Now using segment index of the process  */
299       index = proc->segment_index;
300       /* Done at the process's creation */
301       SMPI_switch_data_segment(index);
302     }
303
304     Process* process = smpi_process_remote(index);
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 }