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"
24 #include "popping_bodies.cpp"
27 * @ingroup simix_host_management
28 * @brief Waits for the completion of an execution synchro and destroy it.
30 * @param execution The execution synchro
32 e_smx_state_t simcall_execution_wait(const smx_activity_t& execution)
34 return (e_smx_state_t)simcall_BODY_execution_wait(static_cast<simgrid::kernel::activity::ExecImpl*>(execution.get()));
37 bool simcall_execution_test(const smx_activity_t& execution)
39 return simcall_BODY_execution_test(static_cast<simgrid::kernel::activity::ExecImpl*>(execution.get()));
42 void simcall_process_join(smx_actor_t process, double timeout)
44 simcall_BODY_process_join(process, timeout);
48 * @ingroup simix_process_management
49 * @brief Suspends an actor
51 void simcall_process_suspend(smx_actor_t process)
53 simcall_BODY_process_suspend(process);
57 * @ingroup simix_process_management
58 * @brief Creates a new sleep SIMIX synchro.
60 * This function creates a SURF action and allocates the data necessary
61 * to create the SIMIX synchro. It can raise a HostFailureException if the
62 * host crashed. The default SIMIX name of the synchro is "sleep".
64 * @param duration Time duration of the sleep.
65 * @return A result telling whether the sleep was successful
67 e_smx_state_t simcall_process_sleep(double duration)
69 /* checking for infinite values */
70 xbt_assert(std::isfinite(duration), "duration is not finite!");
71 return (e_smx_state_t) simcall_BODY_process_sleep(duration);
75 * @ingroup simix_comm_management
77 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
78 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
79 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
82 /* checking for infinite values */
83 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
84 xbt_assert(std::isfinite(rate), "rate is not finite!");
85 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
87 xbt_assert(mbox, "No rendez-vous point defined for send");
89 if (MC_is_active() || MC_record_replay_is_active()) {
90 /* the model-checker wants two separate simcalls */
91 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
92 comm = simcall_comm_isend(sender, mbox, task_size, rate,
93 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
94 simcall_comm_wait(comm, timeout);
98 simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
99 match_fun, copy_data_fun, data, timeout);
104 * @ingroup simix_comm_management
106 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
107 size_t src_buff_size,
108 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
109 void (*clean_fun)(void*),
110 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
111 void* data, bool detached)
113 /* checking for infinite values */
114 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
115 xbt_assert(std::isfinite(rate), "rate is not finite!");
117 xbt_assert(mbox, "No rendez-vous point defined for isend");
119 return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
120 src_buff_size, match_fun,
121 clean_fun, copy_data_fun, data, detached);
125 * @ingroup simix_comm_management
127 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
128 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
129 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
130 double timeout, double rate)
132 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
133 xbt_assert(mbox, "No rendez-vous point defined for recv");
135 if (MC_is_active() || MC_record_replay_is_active()) {
136 /* the model-checker wants two separate simcalls */
137 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
138 comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
139 match_fun, copy_data_fun, data, rate);
140 simcall_comm_wait(comm, timeout);
144 simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
145 match_fun, copy_data_fun, data, timeout, rate);
149 * @ingroup simix_comm_management
151 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
152 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
153 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
154 void* data, double rate)
156 xbt_assert(mbox, "No rendez-vous point defined for irecv");
158 return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
159 match_fun, copy_data_fun, data, rate);
163 * @ingroup simix_comm_management
165 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
166 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
168 xbt_assert(mbox, "No rendez-vous point defined for iprobe");
170 return simgrid::simix::simcall([mbox, type, match_fun, data] { return mbox->iprobe(type, match_fun, data); });
174 * @ingroup simix_comm_management
176 unsigned int simcall_comm_waitany(smx_activity_t comms[], size_t count, double timeout)
178 std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
179 std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
180 return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
182 return simcall_BODY_comm_waitany(rcomms.get(), count, timeout);
185 unsigned int simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count, double timeout)
187 return simcall_BODY_comm_waitany(comms, count, timeout);
191 * @ingroup simix_comm_management
193 int simcall_comm_testany(smx_activity_t comms[], size_t count)
197 std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
198 std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
199 return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
201 return simcall_BODY_comm_testany(rcomms.get(), count);
204 int simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count)
208 return simcall_BODY_comm_testany(comms, count);
212 * @ingroup simix_comm_management
214 void simcall_comm_wait(const smx_activity_t& comm, double timeout)
216 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
217 simcall_BODY_comm_wait(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
221 * @ingroup simix_comm_management
224 bool simcall_comm_test(const smx_activity_t& comm)
226 return simcall_BODY_comm_test(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()));
230 * @ingroup simix_synchro_management
233 smx_mutex_t simcall_mutex_init()
235 if (simix_global == nullptr) {
236 fprintf(stderr, "You must initialize the SimGrid engine before using it\n"); // We can't use xbt_die since we may
237 // get there before the initialization
240 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
244 * @ingroup simix_synchro_management
247 void simcall_mutex_lock(smx_mutex_t mutex)
249 simcall_BODY_mutex_lock(mutex);
253 * @ingroup simix_synchro_management
256 int simcall_mutex_trylock(smx_mutex_t mutex)
258 return simcall_BODY_mutex_trylock(mutex);
262 * @ingroup simix_synchro_management
265 void simcall_mutex_unlock(smx_mutex_t mutex)
267 simcall_BODY_mutex_unlock(mutex);
271 * @ingroup simix_synchro_management
274 smx_cond_t simcall_cond_init()
276 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
280 * @ingroup simix_synchro_management
283 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
285 simcall_BODY_cond_wait(cond, mutex);
289 * @ingroup simix_synchro_management
292 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
294 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
295 return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
299 * @ingroup simix_synchro_management
302 void simcall_sem_acquire(smx_sem_t sem)
304 simcall_BODY_sem_acquire(sem);
308 * @ingroup simix_synchro_management
311 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
313 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
314 return simcall_BODY_sem_acquire_timeout(sem, timeout);
317 e_smx_state_t simcall_io_wait(const smx_activity_t& io)
319 return (e_smx_state_t)simcall_BODY_io_wait(static_cast<simgrid::kernel::activity::IoImpl*>(io.get()));
322 void simcall_run_kernel(std::function<void()> const& code)
324 simcall_BODY_run_kernel(&code);
327 void simcall_run_blocking(std::function<void()> const& code)
329 simcall_BODY_run_blocking(&code);
332 int simcall_mc_random(int min, int max) {
333 return simcall_BODY_mc_random(min, max);
336 /* ************************************************************************** */
338 /** @brief returns a printable string representing a simcall */
339 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
340 return simcall_names[kind];
346 void unblock(smx_actor_t process)
348 xbt_assert(SIMIX_is_maestro());
349 SIMIX_simcall_answer(&process->simcall);
352 } // namespace simgrid
354 /* ****************************DEPRECATED CALLS******************************* */
355 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
357 simgrid::simix::simcall([process, kill_time] { process->set_kill_time(kill_time); });
359 void simcall_comm_cancel(smx_activity_t comm)
361 simgrid::simix::simcall([comm] { boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm)->cancel(); });
363 void simcall_execution_cancel(smx_activity_t exec)
365 simgrid::simix::simcall([exec] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->cancel(); });
367 void simcall_execution_set_priority(smx_activity_t exec, double priority)
369 simgrid::simix::simcall([exec, priority] {
370 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_priority(priority);
374 void simcall_execution_set_bound(smx_activity_t exec, double bound)
376 simgrid::simix::simcall(
377 [exec, bound] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_bound(bound); });
381 smx_activity_t simcall_execution_start(const std::string& name, const std::string& category, double flops_amount,
382 double priority, double bound, sg_host_t host)
384 return simgrid::simix::simcall([name, category, flops_amount, priority, bound, host] {
385 simgrid::kernel::activity::ExecImpl* exec = new simgrid::kernel::activity::ExecImpl();
388 .set_tracing_category(category)
390 .set_priority(priority)
392 .set_flops_amount(flops_amount)
394 return simgrid::kernel::activity::ExecImplPtr(exec);
399 smx_activity_t simcall_execution_parallel_start(const std::string& name, int host_nb, const sg_host_t* host_list,
400 const double* flops_amount, const double* bytes_amount, double rate,
403 /* Check that we are not mixing VMs and PMs in the parallel task */
404 bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
405 for (int i = 1; i < host_nb; i++) {
406 bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
407 xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
410 /* checking for infinite values */
411 for (int i = 0; i < host_nb; ++i) {
412 if (flops_amount != nullptr)
413 xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
414 if (bytes_amount != nullptr) {
415 for (int j = 0; j < host_nb; ++j) {
416 xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]), "bytes_amount[%d+%d*%d] is not finite!", i, host_nb,
421 xbt_assert(std::isfinite(rate), "rate is not finite!");
423 std::vector<simgrid::s4u::Host*> hosts(host_list, host_list + host_nb);
424 std::vector<double> flops_parallel_amount;
425 std::vector<double> bytes_parallel_amount;
426 if (flops_amount != nullptr)
427 flops_parallel_amount = std::vector<double>(flops_amount, flops_amount + host_nb);
428 if (bytes_amount != nullptr)
429 bytes_parallel_amount = std::vector<double>(bytes_amount, bytes_amount + host_nb * host_nb);
430 return simgrid::simix::simcall([name, hosts, flops_parallel_amount, bytes_parallel_amount, timeout] {
431 simgrid::kernel::activity::ExecImpl* exec = new simgrid::kernel::activity::ExecImpl();
435 .set_timeout(timeout)
436 .set_flops_amounts(flops_parallel_amount)
437 .set_bytes_amounts(bytes_parallel_amount)
439 return simgrid::kernel::activity::ExecImplPtr(exec);
444 void SIMIX_comm_finish(smx_activity_t synchro)
446 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();