Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
All activities have their own finish method \o/
[simgrid.git] / src / simix / libsmx.cpp
1 /* libsmx.c - public interface to simix                                       */
2 /* --------                                                                   */
3 /* These functions are the only ones that are visible from the higher levels  */
4 /* (most of them simply add some documentation to the generated simcall body) */
5 /*                                                                            */
6 /* This is somehow the "libc" of SimGrid                                      */
7
8 /* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved.          */
9
10 /* This program is free software; you can redistribute it and/or modify it
11  * under the terms of the license (GNU LGPL) which comes with this package. */
12
13 #include "mc/mc.h"
14 #include "simgrid/simix/blocking_simcall.hpp"
15 #include "src/kernel/activity/CommImpl.hpp"
16 #include "src/kernel/activity/ConditionVariableImpl.hpp"
17 #include "src/kernel/activity/ExecImpl.hpp"
18 #include "src/kernel/activity/IoImpl.hpp"
19 #include "src/kernel/activity/MailboxImpl.hpp"
20 #include "src/kernel/activity/MutexImpl.hpp"
21 #include "src/mc/mc_replay.hpp"
22 #include "src/plugins/vm/VirtualMachineImpl.hpp"
23 #include "src/simix/smx_host_private.hpp"
24
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
26
27 #include "popping_bodies.cpp"
28
29 /**
30  * @ingroup simix_process_management
31  * @brief Creates a synchro that may involve parallel computation on
32  * several hosts and communication between them.
33  *
34  * @param name Name of the execution synchro to create
35  * @param host_nb Number of hosts where the synchro will be executed
36  * @param host_list Array (of size host_nb) of hosts where the synchro will be executed
37  * @param flops_amount Array (of size host_nb) of computation amount of hosts (in bytes)
38  * @param bytes_amount Array (of size host_nb * host_nb) representing the communication
39  * amount between each pair of hosts
40  * @param rate the SURF action rate
41  * @param timeout timeout
42  * @return A new SIMIX execution synchronization
43  */
44 smx_activity_t simcall_execution_parallel_start(const std::string& name, int host_nb, const sg_host_t* host_list,
45                                                 const double* flops_amount, const double* bytes_amount, double rate,
46                                                 double timeout)
47 {
48   /* checking for infinite values */
49   for (int i = 0 ; i < host_nb ; ++i) {
50     if (flops_amount != nullptr)
51       xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
52     if (bytes_amount != nullptr) {
53       for (int j = 0 ; j < host_nb ; ++j) {
54         xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
55                    "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
56       }
57     }
58   }
59
60   xbt_assert(std::isfinite(rate), "rate is not finite!");
61
62   return simgrid::simix::simcall([name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout] {
63     return SIMIX_execution_parallel_start(std::move(name), host_nb, host_list, flops_amount, bytes_amount, rate,
64                                           timeout);
65   });
66 }
67
68 /**
69  * @ingroup simix_host_management
70  * @brief Waits for the completion of an execution synchro and destroy it.
71  *
72  * @param execution The execution synchro
73  */
74 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
75 {
76   return (e_smx_state_t) simcall_BODY_execution_wait(execution);
77 }
78
79 e_smx_state_t simcall_execution_test(smx_activity_t execution)
80 {
81   return (e_smx_state_t)simcall_BODY_execution_test(execution);
82 }
83
84 void simcall_process_join(smx_actor_t process, double timeout)
85 {
86   simcall_BODY_process_join(process, timeout);
87 }
88
89 /**
90  * @ingroup simix_process_management
91  * @brief Suspends an actor
92  */
93 void simcall_process_suspend(smx_actor_t process)
94 {
95   simcall_BODY_process_suspend(process);
96 }
97
98 /**
99  * @ingroup simix_process_management
100  * @brief Creates a new sleep SIMIX synchro.
101  *
102  * This function creates a SURF action and allocates the data necessary
103  * to create the SIMIX synchro. It can raise a HostFailureException if the
104  * host crashed. The default SIMIX name of the synchro is "sleep".
105  *
106  *   @param duration Time duration of the sleep.
107  *   @return A result telling whether the sleep was successful
108  */
109 e_smx_state_t simcall_process_sleep(double duration)
110 {
111   /* checking for infinite values */
112   xbt_assert(std::isfinite(duration), "duration is not finite!");
113   return (e_smx_state_t) simcall_BODY_process_sleep(duration);
114 }
115
116 /**
117  * @ingroup simix_comm_management
118  */
119 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
120                        size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
121                        void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
122 {
123   /* checking for infinite values */
124   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
125   xbt_assert(std::isfinite(rate), "rate is not finite!");
126   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
127
128   xbt_assert(mbox, "No rendez-vous point defined for send");
129
130   if (MC_is_active() || MC_record_replay_is_active()) {
131     /* the model-checker wants two separate simcalls */
132     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
133     comm = simcall_comm_isend(sender, mbox, task_size, rate,
134         src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
135     simcall_comm_wait(comm, timeout);
136     comm = nullptr;
137   }
138   else {
139     simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
140                          match_fun, copy_data_fun, data, timeout);
141   }
142 }
143
144 /**
145  * @ingroup simix_comm_management
146  */
147 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
148                                   size_t src_buff_size,
149                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
150                                   void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
151                                   void* data, int detached)
152 {
153   /* checking for infinite values */
154   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
155   xbt_assert(std::isfinite(rate), "rate is not finite!");
156
157   xbt_assert(mbox, "No rendez-vous point defined for isend");
158
159   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
160                                  src_buff_size, match_fun,
161                                  clean_fun, copy_data_fun, data, detached);
162 }
163
164 /**
165  * @ingroup simix_comm_management
166  */
167 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
168                        int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
169                        void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
170 {
171   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
172   xbt_assert(mbox, "No rendez-vous point defined for recv");
173
174   if (MC_is_active() || MC_record_replay_is_active()) {
175     /* the model-checker wants two separate simcalls */
176     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
177     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
178                               match_fun, copy_data_fun, data, rate);
179     simcall_comm_wait(comm, timeout);
180     comm = nullptr;
181   }
182   else {
183     simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
184                            match_fun, copy_data_fun, data, timeout, rate);
185   }
186 }
187 /**
188  * @ingroup simix_comm_management
189  */
190 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
191                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
192                                   void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
193 {
194   xbt_assert(mbox, "No rendez-vous point defined for irecv");
195
196   return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
197                                  match_fun, copy_data_fun, data, rate);
198 }
199
200 /**
201  * @ingroup simix_comm_management
202  */
203 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
204                                    int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
205 {
206   xbt_assert(mbox, "No rendez-vous point defined for iprobe");
207
208   return simgrid::simix::simcall([mbox, type, match_fun, data] { return mbox->iprobe(type, match_fun, data); });
209 }
210
211 /**
212  * @ingroup simix_comm_management
213  */
214 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
215 {
216   return simcall_BODY_comm_waitany(comms, timeout);
217 }
218
219 /**
220  * @ingroup simix_comm_management
221  */
222 int simcall_comm_testany(smx_activity_t* comms, size_t count)
223 {
224   if (count == 0)
225     return -1;
226   return simcall_BODY_comm_testany(comms, count);
227 }
228
229 /**
230  * @ingroup simix_comm_management
231  */
232 void simcall_comm_wait(smx_activity_t comm, double timeout)
233 {
234   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
235   simcall_BODY_comm_wait(comm, timeout);
236 }
237
238 /**
239  * @ingroup simix_comm_management
240  *
241  */
242 int simcall_comm_test(smx_activity_t comm)
243 {
244   return simcall_BODY_comm_test(comm);
245 }
246
247 /**
248  * @ingroup simix_synchro_management
249  *
250  */
251 smx_mutex_t simcall_mutex_init()
252 {
253   if (simix_global == nullptr) {
254     fprintf(stderr, "You must initialize the SimGrid engine before using it\n"); // We can't use xbt_die since we may
255                                                                                  // get there before the initialization
256     xbt_abort();
257   }
258   return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
259 }
260
261 /**
262  * @ingroup simix_synchro_management
263  *
264  */
265 void simcall_mutex_lock(smx_mutex_t mutex)
266 {
267   simcall_BODY_mutex_lock(mutex);
268 }
269
270 /**
271  * @ingroup simix_synchro_management
272  *
273  */
274 int simcall_mutex_trylock(smx_mutex_t mutex)
275 {
276   return simcall_BODY_mutex_trylock(mutex);
277 }
278
279 /**
280  * @ingroup simix_synchro_management
281  *
282  */
283 void simcall_mutex_unlock(smx_mutex_t mutex)
284 {
285   simcall_BODY_mutex_unlock(mutex);
286 }
287
288 /**
289  * @ingroup simix_synchro_management
290  *
291  */
292 smx_cond_t simcall_cond_init()
293 {
294   return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
295 }
296
297 /**
298  * @ingroup simix_synchro_management
299  *
300  */
301 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
302 {
303   simcall_BODY_cond_wait(cond, mutex);
304 }
305
306 /**
307  * @ingroup simix_synchro_management
308  *
309  */
310 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
311 {
312   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
313   return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
314 }
315
316 /**
317  * @ingroup simix_synchro_management
318  *
319  */
320 void simcall_sem_acquire(smx_sem_t sem)
321 {
322   simcall_BODY_sem_acquire(sem);
323 }
324
325 /**
326  * @ingroup simix_synchro_management
327  *
328  */
329 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
330 {
331   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
332   return simcall_BODY_sem_acquire_timeout(sem, timeout);
333 }
334
335 e_smx_state_t simcall_io_wait(smx_activity_t io)
336 {
337   return (e_smx_state_t)simcall_BODY_io_wait(io);
338 }
339
340 void simcall_run_kernel(std::function<void()> const& code)
341 {
342   simcall_BODY_run_kernel(&code);
343 }
344
345 void simcall_run_blocking(std::function<void()> const& code)
346 {
347   simcall_BODY_run_blocking(&code);
348 }
349
350 int simcall_mc_random(int min, int max) {
351   return simcall_BODY_mc_random(min, max);
352 }
353
354 /* ************************************************************************** */
355
356 /** @brief returns a printable string representing a simcall */
357 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
358   return simcall_names[kind];
359 }
360
361 namespace simgrid {
362 namespace simix {
363
364 void unblock(smx_actor_t process)
365 {
366   xbt_assert(SIMIX_is_maestro());
367   SIMIX_simcall_answer(&process->simcall);
368 }
369 } // namespace simix
370 } // namespace simgrid
371
372 /* ****************************DEPRECATED CALLS******************************* */
373 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
374 {
375   simgrid::simix::simcall([process, kill_time] { process->set_kill_time(kill_time); });
376 }
377 void simcall_comm_cancel(smx_activity_t comm)
378 {
379   simgrid::simix::simcall([comm] { boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm)->cancel(); });
380 }
381 void simcall_execution_cancel(smx_activity_t exec)
382 {
383   simgrid::simix::simcall([exec] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->cancel(); });
384 }
385 void simcall_execution_set_priority(smx_activity_t exec, double priority)
386 {
387   simgrid::simix::simcall([exec, priority] {
388     boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_priority(priority);
389   });
390 }
391
392 void simcall_execution_set_bound(smx_activity_t exec, double bound)
393 {
394   simgrid::simix::simcall(
395       [exec, bound] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_bound(bound); });
396 }
397
398 smx_activity_t simcall_execution_start(const std::string& name, const std::string& category, double flops_amount,
399                                        double priority, double bound, sg_host_t host)
400 {
401   return simgrid::simix::simcall([name, category, flops_amount, priority, bound, host] {
402     return simgrid::kernel::activity::ExecImplPtr(
403                new simgrid::kernel::activity::ExecImpl(std::move(name), std::move(category), nullptr, host))
404         ->start(flops_amount, priority, bound);
405   });
406 }
407
408 // deprecated
409 void SIMIX_comm_finish(smx_activity_t synchro)
410 {
411   boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();
412 }