Logo AND Algorithmique Numérique Distribuée

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