Logo AND Algorithmique Numérique Distribuée

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