Logo AND Algorithmique Numérique Distribuée

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