1 /* libsmx.c - public interface to simix */
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) */
6 /* This is somehow the "libc" of SimGrid */
8 /* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved. */
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. */
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"
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
27 #include "popping_bodies.cpp"
30 * @ingroup simix_process_management
31 * @brief Creates a synchro that may involve parallel computation on
32 * several hosts and communication between them.
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
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,
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).");
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);
67 xbt_assert(std::isfinite(rate), "rate is not finite!");
68 return simgrid::simix::simcall([name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout] {
69 return SIMIX_execution_parallel_start(std::move(name), host_nb, host_list, flops_amount, bytes_amount, rate,
75 * @ingroup simix_host_management
76 * @brief Waits for the completion of an execution synchro and destroy it.
78 * @param execution The execution synchro
80 e_smx_state_t simcall_execution_wait(const smx_activity_t& execution)
82 return (e_smx_state_t)simcall_BODY_execution_wait(static_cast<simgrid::kernel::activity::ExecImpl*>(execution.get()));
85 e_smx_state_t simcall_execution_test(const smx_activity_t& execution)
87 return (e_smx_state_t)simcall_BODY_execution_test(static_cast<simgrid::kernel::activity::ExecImpl*>(execution.get()));
90 void simcall_process_join(smx_actor_t process, double timeout)
92 simcall_BODY_process_join(process, timeout);
96 * @ingroup simix_process_management
97 * @brief Suspends an actor
99 void simcall_process_suspend(smx_actor_t process)
101 simcall_BODY_process_suspend(process);
105 * @ingroup simix_process_management
106 * @brief Creates a new sleep SIMIX synchro.
108 * This function creates a SURF action and allocates the data necessary
109 * to create the SIMIX synchro. It can raise a HostFailureException if the
110 * host crashed. The default SIMIX name of the synchro is "sleep".
112 * @param duration Time duration of the sleep.
113 * @return A result telling whether the sleep was successful
115 e_smx_state_t simcall_process_sleep(double duration)
117 /* checking for infinite values */
118 xbt_assert(std::isfinite(duration), "duration is not finite!");
119 return (e_smx_state_t) simcall_BODY_process_sleep(duration);
123 * @ingroup simix_comm_management
125 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
126 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
127 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
130 /* checking for infinite values */
131 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
132 xbt_assert(std::isfinite(rate), "rate is not finite!");
133 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
135 xbt_assert(mbox, "No rendez-vous point defined for send");
137 if (MC_is_active() || MC_record_replay_is_active()) {
138 /* the model-checker wants two separate simcalls */
139 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
140 comm = simcall_comm_isend(sender, mbox, task_size, rate,
141 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
142 simcall_comm_wait(comm, timeout);
146 simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
147 match_fun, copy_data_fun, data, timeout);
152 * @ingroup simix_comm_management
154 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
155 size_t src_buff_size,
156 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
157 void (*clean_fun)(void*),
158 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
159 void* data, int detached)
161 /* checking for infinite values */
162 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
163 xbt_assert(std::isfinite(rate), "rate is not finite!");
165 xbt_assert(mbox, "No rendez-vous point defined for isend");
167 return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
168 src_buff_size, match_fun,
169 clean_fun, copy_data_fun, data, detached);
173 * @ingroup simix_comm_management
175 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
176 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
177 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
178 double timeout, double rate)
180 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
181 xbt_assert(mbox, "No rendez-vous point defined for recv");
183 if (MC_is_active() || MC_record_replay_is_active()) {
184 /* the model-checker wants two separate simcalls */
185 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
186 comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
187 match_fun, copy_data_fun, data, rate);
188 simcall_comm_wait(comm, timeout);
192 simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
193 match_fun, copy_data_fun, data, timeout, rate);
197 * @ingroup simix_comm_management
199 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
200 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
201 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
202 void* data, double rate)
204 xbt_assert(mbox, "No rendez-vous point defined for irecv");
206 return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
207 match_fun, copy_data_fun, data, rate);
211 * @ingroup simix_comm_management
213 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
214 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
216 xbt_assert(mbox, "No rendez-vous point defined for iprobe");
218 return simgrid::simix::simcall([mbox, type, match_fun, data] { return mbox->iprobe(type, match_fun, data); });
222 * @ingroup simix_comm_management
224 unsigned int simcall_comm_waitany(smx_activity_t comms[], size_t count, double timeout)
226 std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
227 std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
228 return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
230 return simcall_BODY_comm_waitany(rcomms.get(), count, timeout);
233 unsigned int simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count, double timeout)
235 return simcall_BODY_comm_waitany(comms, count, timeout);
239 * @ingroup simix_comm_management
241 int simcall_comm_testany(smx_activity_t comms[], size_t count)
245 std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
246 std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
247 return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
249 return simcall_BODY_comm_testany(rcomms.get(), count);
252 int simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count)
256 return simcall_BODY_comm_testany(comms, count);
260 * @ingroup simix_comm_management
262 void simcall_comm_wait(const smx_activity_t& comm, double timeout)
264 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
265 simcall_BODY_comm_wait(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
269 * @ingroup simix_comm_management
272 int simcall_comm_test(const smx_activity_t& comm)
274 return simcall_BODY_comm_test(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()));
278 * @ingroup simix_synchro_management
281 smx_mutex_t simcall_mutex_init()
283 if (simix_global == nullptr) {
284 fprintf(stderr, "You must initialize the SimGrid engine before using it\n"); // We can't use xbt_die since we may
285 // get there before the initialization
288 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
292 * @ingroup simix_synchro_management
295 void simcall_mutex_lock(smx_mutex_t mutex)
297 simcall_BODY_mutex_lock(mutex);
301 * @ingroup simix_synchro_management
304 int simcall_mutex_trylock(smx_mutex_t mutex)
306 return simcall_BODY_mutex_trylock(mutex);
310 * @ingroup simix_synchro_management
313 void simcall_mutex_unlock(smx_mutex_t mutex)
315 simcall_BODY_mutex_unlock(mutex);
319 * @ingroup simix_synchro_management
322 smx_cond_t simcall_cond_init()
324 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
328 * @ingroup simix_synchro_management
331 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
333 simcall_BODY_cond_wait(cond, mutex);
337 * @ingroup simix_synchro_management
340 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
342 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
343 return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
347 * @ingroup simix_synchro_management
350 void simcall_sem_acquire(smx_sem_t sem)
352 simcall_BODY_sem_acquire(sem);
356 * @ingroup simix_synchro_management
359 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
361 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
362 return simcall_BODY_sem_acquire_timeout(sem, timeout);
365 e_smx_state_t simcall_io_wait(const smx_activity_t& io)
367 return (e_smx_state_t)simcall_BODY_io_wait(static_cast<simgrid::kernel::activity::IoImpl*>(io.get()));
370 void simcall_run_kernel(std::function<void()> const& code)
372 simcall_BODY_run_kernel(&code);
375 void simcall_run_blocking(std::function<void()> const& code)
377 simcall_BODY_run_blocking(&code);
380 int simcall_mc_random(int min, int max) {
381 return simcall_BODY_mc_random(min, max);
384 /* ************************************************************************** */
386 /** @brief returns a printable string representing a simcall */
387 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
388 return simcall_names[kind];
394 void unblock(smx_actor_t process)
396 xbt_assert(SIMIX_is_maestro());
397 SIMIX_simcall_answer(&process->simcall);
400 } // namespace simgrid
402 /* ****************************DEPRECATED CALLS******************************* */
403 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
405 simgrid::simix::simcall([process, kill_time] { process->set_kill_time(kill_time); });
407 void simcall_comm_cancel(smx_activity_t comm)
409 simgrid::simix::simcall([comm] { boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm)->cancel(); });
411 void simcall_execution_cancel(smx_activity_t exec)
413 simgrid::simix::simcall([exec] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->cancel(); });
415 void simcall_execution_set_priority(smx_activity_t exec, double priority)
417 simgrid::simix::simcall([exec, priority] {
418 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_priority(priority);
422 void simcall_execution_set_bound(smx_activity_t exec, double bound)
424 simgrid::simix::simcall(
425 [exec, bound] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_bound(bound); });
428 smx_activity_t simcall_execution_start(const std::string& name, const std::string& category, double flops_amount,
429 double priority, double bound, sg_host_t host)
431 return simgrid::simix::simcall([name, category, flops_amount, priority, bound, host] {
432 return simgrid::kernel::activity::ExecImplPtr(
433 new simgrid::kernel::activity::ExecImpl(std::move(name), std::move(category)))
435 ->start(flops_amount, priority, bound);
440 void SIMIX_comm_finish(smx_activity_t synchro)
442 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();