Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill useless code
[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 smx_actor_t Process::process(){
149   return process_;
150 }
151
152
153 /**
154  * \brief Returns a structure that stores the location (filename + linenumber)
155  *        of the last calls to MPI_* functions.
156  *
157  * \see smpi_trace_set_call_location
158  */
159 smpi_trace_call_location_t* Process::call_location()
160 {
161   return &trace_call_loc_;
162 }
163
164 int Process::index()
165 {
166   return index_;
167 }
168
169 MPI_Comm Process::comm_world()
170 {
171   return comm_world_==nullptr ? MPI_COMM_NULL : *comm_world_;
172 }
173
174 smx_mailbox_t Process::mailbox()
175 {
176   return mailbox_->getImpl();
177 }
178
179 smx_mailbox_t Process::mailbox_small()
180 {
181   return mailbox_small_->getImpl();
182 }
183
184 xbt_mutex_t Process::mailboxes_mutex()
185 {
186   return mailboxes_mutex_;
187 }
188
189 #if HAVE_PAPI
190 int Process::papi_event_set(void)
191 {
192   return papi_event_set_;
193 }
194
195 papi_counter_t& smpi_process_papi_counters(void)
196 {
197   return papi_counter_data_;
198 }
199 #endif
200
201 xbt_os_timer_t Process::timer()
202 {
203   return timer_;
204 }
205
206 void Process::simulated_start()
207 {
208   simulated_ = SIMIX_get_clock();
209 }
210
211 double Process::simulated_elapsed()
212 {
213   return SIMIX_get_clock() - simulated_;
214 }
215
216 MPI_Comm Process::comm_self()
217 {
218   if(comm_self_==MPI_COMM_NULL){
219     MPI_Group group = new  Group(1);
220     comm_self_ = new  Comm(group, nullptr);
221     group->set_mapping(index_, 0);
222   }
223   return comm_self_;
224 }
225
226 MPI_Comm Process::comm_intra()
227 {
228   return comm_intra_;
229 }
230
231 void Process::set_comm_intra(MPI_Comm comm)
232 {
233   comm_intra_ = comm;
234 }
235
236 void Process::set_sampling(int s)
237 {
238   sampling_ = s;
239 }
240
241 int Process::sampling()
242 {
243   return sampling_;
244 }
245
246 void Process::set_finalization_barrier(msg_bar_t bar){
247   finalization_barrier_=bar;
248 }
249
250 msg_bar_t Process::finalization_barrier(){
251   return finalization_barrier_;
252 }
253
254 int Process::return_value(){
255   return return_value_;
256 }
257
258 void Process::set_return_value(int val){
259   return_value_=val;
260 }
261
262 void Process::init(int *argc, char ***argv){
263
264   if (process_data == nullptr){
265     printf("SimGrid was not initialized properly before entering MPI_Init. Aborting, please check compilation process and use smpirun\n");
266     exit(1);
267   }
268   if (argc != nullptr && argv != nullptr) {
269     smx_actor_t proc = SIMIX_process_self();
270     proc->context->set_cleanup(&MSG_process_cleanup_from_SIMIX);
271
272     int index = smpi_process_index_of_smx_process(proc);
273
274     if(index_to_process_data == nullptr){
275       index_to_process_data=static_cast<int*>(xbt_malloc(SIMIX_process_count()*sizeof(int)));
276     }
277
278     char* instance_id = (*argv)[1];
279     int rank = xbt_str_parse_int((*argv)[2], "Invalid rank: %s");
280     smpi_deployment_register_process(instance_id, rank, index);
281
282     if(smpi_privatize_global_variables){
283       /* Now using segment index of the process  */
284       index = proc->segment_index;
285       /* Done at the process's creation */
286       SMPI_switch_data_segment(index);
287     }
288
289     Process* process = smpi_process_remote(index);
290     process->set_data(argc, argv);
291   }
292   xbt_assert(smpi_process(),
293       "smpi_process() returned nullptr. You probably gave a nullptr parameter to MPI_Init. "
294       "Although it's required by MPI-2, this is currently not supported by SMPI.");
295 }
296
297 }
298 }