Logo AND Algorithmique Numérique Distribuée

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