Logo AND Algorithmique Numérique Distribuée

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