Logo AND Algorithmique Numérique Distribuée

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