Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
play with (parallel) execs. still not satisfying
[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   /* Check that we are not mixing VMs and PMs in the parallel task */
49   bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
50   for (int i = 1; i < host_nb; i++) {
51     bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
52     xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
53   }
54
55   /* checking for infinite values */
56   for (int i = 0 ; i < host_nb ; ++i) {
57     if (flops_amount != nullptr)
58       xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
59     if (bytes_amount != nullptr) {
60       for (int j = 0 ; j < host_nb ; ++j) {
61         xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
62                    "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
63       }
64     }
65   }
66
67   xbt_assert(std::isfinite(rate), "rate is not finite!");
68
69   return simgrid::simix::simcall([name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout] {
70     return SIMIX_execution_parallel_start(std::move(name), host_nb, host_list, flops_amount, bytes_amount, rate,
71                                           timeout);
72   });
73 }
74
75 /**
76  * @ingroup simix_host_management
77  * @brief Waits for the completion of an execution synchro and destroy it.
78  *
79  * @param execution The execution synchro
80  */
81 e_smx_state_t simcall_execution_wait(const smx_activity_t& execution)
82 {
83   return (e_smx_state_t)simcall_BODY_execution_wait(static_cast<simgrid::kernel::activity::ExecImpl*>(execution.get()));
84 }
85
86 e_smx_state_t simcall_execution_test(const smx_activity_t& execution)
87 {
88   return (e_smx_state_t)simcall_BODY_execution_test(static_cast<simgrid::kernel::activity::ExecImpl*>(execution.get()));
89 }
90
91 void simcall_process_join(smx_actor_t process, double timeout)
92 {
93   simcall_BODY_process_join(process, timeout);
94 }
95
96 /**
97  * @ingroup simix_process_management
98  * @brief Suspends an actor
99  */
100 void simcall_process_suspend(smx_actor_t process)
101 {
102   simcall_BODY_process_suspend(process);
103 }
104
105 /**
106  * @ingroup simix_process_management
107  * @brief Creates a new sleep SIMIX synchro.
108  *
109  * This function creates a SURF action and allocates the data necessary
110  * to create the SIMIX synchro. It can raise a HostFailureException if the
111  * host crashed. The default SIMIX name of the synchro is "sleep".
112  *
113  *   @param duration Time duration of the sleep.
114  *   @return A result telling whether the sleep was successful
115  */
116 e_smx_state_t simcall_process_sleep(double duration)
117 {
118   /* checking for infinite values */
119   xbt_assert(std::isfinite(duration), "duration is not finite!");
120   return (e_smx_state_t) simcall_BODY_process_sleep(duration);
121 }
122
123 /**
124  * @ingroup simix_comm_management
125  */
126 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
127                        size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
128                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
129                        double timeout)
130 {
131   /* checking for infinite values */
132   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
133   xbt_assert(std::isfinite(rate), "rate is not finite!");
134   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
135
136   xbt_assert(mbox, "No rendez-vous point defined for send");
137
138   if (MC_is_active() || MC_record_replay_is_active()) {
139     /* the model-checker wants two separate simcalls */
140     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
141     comm = simcall_comm_isend(sender, mbox, task_size, rate,
142         src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
143     simcall_comm_wait(comm, timeout);
144     comm = nullptr;
145   }
146   else {
147     simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
148                          match_fun, copy_data_fun, data, timeout);
149   }
150 }
151
152 /**
153  * @ingroup simix_comm_management
154  */
155 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
156                                   size_t src_buff_size,
157                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
158                                   void (*clean_fun)(void*),
159                                   void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
160                                   void* data, int detached)
161 {
162   /* checking for infinite values */
163   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
164   xbt_assert(std::isfinite(rate), "rate is not finite!");
165
166   xbt_assert(mbox, "No rendez-vous point defined for isend");
167
168   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
169                                  src_buff_size, match_fun,
170                                  clean_fun, copy_data_fun, data, detached);
171 }
172
173 /**
174  * @ingroup simix_comm_management
175  */
176 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
177                        int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
178                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
179                        double timeout, double rate)
180 {
181   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
182   xbt_assert(mbox, "No rendez-vous point defined for recv");
183
184   if (MC_is_active() || MC_record_replay_is_active()) {
185     /* the model-checker wants two separate simcalls */
186     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
187     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
188                               match_fun, copy_data_fun, data, rate);
189     simcall_comm_wait(comm, timeout);
190     comm = nullptr;
191   }
192   else {
193     simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
194                            match_fun, copy_data_fun, data, timeout, rate);
195   }
196 }
197 /**
198  * @ingroup simix_comm_management
199  */
200 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
201                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
202                                   void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
203                                   void* data, double rate)
204 {
205   xbt_assert(mbox, "No rendez-vous point defined for irecv");
206
207   return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
208                                  match_fun, copy_data_fun, data, rate);
209 }
210
211 /**
212  * @ingroup simix_comm_management
213  */
214 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
215                                    int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
216 {
217   xbt_assert(mbox, "No rendez-vous point defined for iprobe");
218
219   return simgrid::simix::simcall([mbox, type, match_fun, data] { return mbox->iprobe(type, match_fun, data); });
220 }
221
222 /**
223  * @ingroup simix_comm_management
224  */
225 unsigned int simcall_comm_waitany(smx_activity_t comms[], size_t count, double timeout)
226 {
227   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
228   std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
229     return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
230   });
231   return simcall_BODY_comm_waitany(rcomms.get(), count, timeout);
232 }
233
234 unsigned int simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count, double timeout)
235 {
236   return simcall_BODY_comm_waitany(comms, count, timeout);
237 }
238
239 /**
240  * @ingroup simix_comm_management
241  */
242 int simcall_comm_testany(smx_activity_t comms[], size_t count)
243 {
244   if (count == 0)
245     return -1;
246   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
247   std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
248     return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
249   });
250   return simcall_BODY_comm_testany(rcomms.get(), count);
251 }
252
253 int simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count)
254 {
255   if (count == 0)
256     return -1;
257   return simcall_BODY_comm_testany(comms, count);
258 }
259
260 /**
261  * @ingroup simix_comm_management
262  */
263 void simcall_comm_wait(const smx_activity_t& comm, double timeout)
264 {
265   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
266   simcall_BODY_comm_wait(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
267 }
268
269 /**
270  * @ingroup simix_comm_management
271  *
272  */
273 int simcall_comm_test(const smx_activity_t& comm)
274 {
275   return simcall_BODY_comm_test(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()));
276 }
277
278 /**
279  * @ingroup simix_synchro_management
280  *
281  */
282 smx_mutex_t simcall_mutex_init()
283 {
284   if (simix_global == nullptr) {
285     fprintf(stderr, "You must initialize the SimGrid engine before using it\n"); // We can't use xbt_die since we may
286                                                                                  // get there before the initialization
287     xbt_abort();
288   }
289   return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
290 }
291
292 /**
293  * @ingroup simix_synchro_management
294  *
295  */
296 void simcall_mutex_lock(smx_mutex_t mutex)
297 {
298   simcall_BODY_mutex_lock(mutex);
299 }
300
301 /**
302  * @ingroup simix_synchro_management
303  *
304  */
305 int simcall_mutex_trylock(smx_mutex_t mutex)
306 {
307   return simcall_BODY_mutex_trylock(mutex);
308 }
309
310 /**
311  * @ingroup simix_synchro_management
312  *
313  */
314 void simcall_mutex_unlock(smx_mutex_t mutex)
315 {
316   simcall_BODY_mutex_unlock(mutex);
317 }
318
319 /**
320  * @ingroup simix_synchro_management
321  *
322  */
323 smx_cond_t simcall_cond_init()
324 {
325   return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
326 }
327
328 /**
329  * @ingroup simix_synchro_management
330  *
331  */
332 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
333 {
334   simcall_BODY_cond_wait(cond, mutex);
335 }
336
337 /**
338  * @ingroup simix_synchro_management
339  *
340  */
341 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
342 {
343   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
344   return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
345 }
346
347 /**
348  * @ingroup simix_synchro_management
349  *
350  */
351 void simcall_sem_acquire(smx_sem_t sem)
352 {
353   simcall_BODY_sem_acquire(sem);
354 }
355
356 /**
357  * @ingroup simix_synchro_management
358  *
359  */
360 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
361 {
362   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
363   return simcall_BODY_sem_acquire_timeout(sem, timeout);
364 }
365
366 e_smx_state_t simcall_io_wait(const smx_activity_t& io)
367 {
368   return (e_smx_state_t)simcall_BODY_io_wait(static_cast<simgrid::kernel::activity::IoImpl*>(io.get()));
369 }
370
371 void simcall_run_kernel(std::function<void()> const& code)
372 {
373   simcall_BODY_run_kernel(&code);
374 }
375
376 void simcall_run_blocking(std::function<void()> const& code)
377 {
378   simcall_BODY_run_blocking(&code);
379 }
380
381 int simcall_mc_random(int min, int max) {
382   return simcall_BODY_mc_random(min, max);
383 }
384
385 /* ************************************************************************** */
386
387 /** @brief returns a printable string representing a simcall */
388 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
389   return simcall_names[kind];
390 }
391
392 namespace simgrid {
393 namespace simix {
394
395 void unblock(smx_actor_t process)
396 {
397   xbt_assert(SIMIX_is_maestro());
398   SIMIX_simcall_answer(&process->simcall);
399 }
400 } // namespace simix
401 } // namespace simgrid
402
403 /* ****************************DEPRECATED CALLS******************************* */
404 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
405 {
406   simgrid::simix::simcall([process, kill_time] { process->set_kill_time(kill_time); });
407 }
408 void simcall_comm_cancel(smx_activity_t comm)
409 {
410   simgrid::simix::simcall([comm] { boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm)->cancel(); });
411 }
412 void simcall_execution_cancel(smx_activity_t exec)
413 {
414   simgrid::simix::simcall([exec] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->cancel(); });
415 }
416 void simcall_execution_set_priority(smx_activity_t exec, double priority)
417 {
418   simgrid::simix::simcall([exec, priority] {
419     boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_priority(priority);
420   });
421 }
422
423 void simcall_execution_set_bound(smx_activity_t exec, double bound)
424 {
425   simgrid::simix::simcall(
426       [exec, bound] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_bound(bound); });
427 }
428
429 smx_activity_t simcall_execution_start(const std::string& name, const std::string& category, double flops_amount,
430                                        double priority, double bound, sg_host_t host)
431 {
432   return simgrid::simix::simcall([name, category, flops_amount, priority, bound, host] {
433     return simgrid::kernel::activity::ExecImplPtr(
434                new simgrid::kernel::activity::ExecImpl(std::move(name), std::move(category), host))
435         ->start(flops_amount, priority, bound);
436   });
437 }
438
439 // deprecated
440 void SIMIX_comm_finish(smx_activity_t synchro)
441 {
442   boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();
443 }