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/MutexImpl.hpp"
20 #include "src/mc/mc_replay.hpp"
21 #include "src/plugins/vm/VirtualMachineImpl.hpp"
22 #include "src/simix/smx_host_private.hpp"
23 #include "src/simix/smx_io_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(std::string name, int host_nb, sg_host_t* host_list,
45 double* flops_amount, double* bytes_amount, double rate, double timeout)
47 /* checking for infinite values */
48 for (int i = 0 ; i < host_nb ; ++i) {
49 if (flops_amount != nullptr)
50 xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
51 if (bytes_amount != nullptr) {
52 for (int j = 0 ; j < host_nb ; ++j) {
53 xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
54 "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
59 xbt_assert(std::isfinite(rate), "rate is not finite!");
61 return simgrid::simix::simcall([name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout] {
62 return SIMIX_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout);
67 * @ingroup simix_host_management
68 * @brief Waits for the completion of an execution synchro and destroy it.
70 * @param execution The execution synchro
72 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
74 return (e_smx_state_t) simcall_BODY_execution_wait(execution);
77 e_smx_state_t simcall_execution_test(smx_activity_t execution)
79 return (e_smx_state_t)simcall_BODY_execution_test(execution);
82 void simcall_process_join(smx_actor_t process, double timeout)
84 simcall_BODY_process_join(process, timeout);
88 * @ingroup simix_process_management
89 * @brief Suspends an actor
91 void simcall_process_suspend(smx_actor_t process)
93 simcall_BODY_process_suspend(process);
97 * @ingroup simix_process_management
98 * @brief Creates a new sleep SIMIX synchro.
100 * This function creates a SURF action and allocates the data necessary
101 * to create the SIMIX synchro. It can raise a HostFailureException if the
102 * host crashed. The default SIMIX name of the synchro is "sleep".
104 * @param duration Time duration of the sleep.
105 * @return A result telling whether the sleep was successful
107 e_smx_state_t simcall_process_sleep(double duration)
109 /* checking for infinite values */
110 xbt_assert(std::isfinite(duration), "duration is not finite!");
111 return (e_smx_state_t) simcall_BODY_process_sleep(duration);
115 * @ingroup simix_comm_management
117 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
118 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
119 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
121 /* checking for infinite values */
122 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
123 xbt_assert(std::isfinite(rate), "rate is not finite!");
124 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
126 xbt_assert(mbox, "No rendez-vous point defined for send");
128 if (MC_is_active() || MC_record_replay_is_active()) {
129 /* the model-checker wants two separate simcalls */
130 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
131 comm = simcall_comm_isend(sender, mbox, task_size, rate,
132 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
133 simcall_comm_wait(comm, timeout);
137 simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
138 match_fun, copy_data_fun, data, timeout);
143 * @ingroup simix_comm_management
145 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
146 size_t src_buff_size,
147 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
148 void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
149 void* data, int detached)
151 /* checking for infinite values */
152 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
153 xbt_assert(std::isfinite(rate), "rate is not finite!");
155 xbt_assert(mbox, "No rendez-vous point defined for isend");
157 return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
158 src_buff_size, match_fun,
159 clean_fun, copy_data_fun, data, detached);
163 * @ingroup simix_comm_management
165 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
166 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
167 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
169 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
170 xbt_assert(mbox, "No rendez-vous point defined for recv");
172 if (MC_is_active() || MC_record_replay_is_active()) {
173 /* the model-checker wants two separate simcalls */
174 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
175 comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
176 match_fun, copy_data_fun, data, rate);
177 simcall_comm_wait(comm, timeout);
181 simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
182 match_fun, copy_data_fun, data, timeout, rate);
186 * @ingroup simix_comm_management
188 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
189 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
190 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
192 xbt_assert(mbox, "No rendez-vous point defined for irecv");
194 return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
195 match_fun, copy_data_fun, data, rate);
199 * @ingroup simix_comm_management
201 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
202 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
204 xbt_assert(mbox, "No rendez-vous point defined for iprobe");
206 return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
210 * @ingroup simix_comm_management
212 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
214 return simcall_BODY_comm_waitany(comms, timeout);
218 * @ingroup simix_comm_management
220 int simcall_comm_testany(smx_activity_t* comms, size_t count)
224 return simcall_BODY_comm_testany(comms, count);
228 * @ingroup simix_comm_management
230 void simcall_comm_wait(smx_activity_t comm, double timeout)
232 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
233 simcall_BODY_comm_wait(comm, timeout);
237 * @ingroup simix_comm_management
240 int simcall_comm_test(smx_activity_t comm)
242 return simcall_BODY_comm_test(comm);
246 * @ingroup simix_synchro_management
249 smx_mutex_t simcall_mutex_init()
251 if (simix_global == nullptr) {
252 fprintf(stderr, "You must initialize the SimGrid engine before using it\n"); // We can't use xbt_die since we may
253 // get there before the initialization
256 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
260 * @ingroup simix_synchro_management
263 void simcall_mutex_lock(smx_mutex_t mutex)
265 simcall_BODY_mutex_lock(mutex);
269 * @ingroup simix_synchro_management
272 int simcall_mutex_trylock(smx_mutex_t mutex)
274 return simcall_BODY_mutex_trylock(mutex);
278 * @ingroup simix_synchro_management
281 void simcall_mutex_unlock(smx_mutex_t mutex)
283 simcall_BODY_mutex_unlock(mutex);
287 * @ingroup simix_synchro_management
290 smx_cond_t simcall_cond_init()
292 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
296 * @ingroup simix_synchro_management
299 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
301 simcall_BODY_cond_wait(cond, mutex);
305 * @ingroup simix_synchro_management
308 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
310 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
311 return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
315 * @ingroup simix_synchro_management
318 void simcall_sem_acquire(smx_sem_t sem)
320 simcall_BODY_sem_acquire(sem);
324 * @ingroup simix_synchro_management
327 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
329 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
330 return simcall_BODY_sem_acquire_timeout(sem, timeout);
333 e_smx_state_t simcall_io_wait(smx_activity_t io)
335 return (e_smx_state_t)simcall_BODY_io_wait(io);
338 void simcall_run_kernel(std::function<void()> const& code)
340 simcall_BODY_run_kernel(&code);
343 void simcall_run_blocking(std::function<void()> const& code)
345 simcall_BODY_run_blocking(&code);
348 int simcall_mc_random(int min, int max) {
349 return simcall_BODY_mc_random(min, max);
352 /* ************************************************************************** */
354 /** @brief returns a printable string representing a simcall */
355 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
356 return simcall_names[kind];
362 void unblock(smx_actor_t process)
364 xbt_assert(SIMIX_is_maestro());
365 SIMIX_simcall_answer(&process->simcall);
368 } // namespace simgrid
370 /* ****************************DEPRECATED CALLS******************************* */
371 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
373 simgrid::simix::simcall([process, kill_time] { process->set_kill_time(kill_time); });
375 void simcall_comm_cancel(smx_activity_t comm)
377 simgrid::simix::simcall([comm] { boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm)->cancel(); });
379 void simcall_execution_cancel(smx_activity_t exec)
381 simgrid::simix::simcall([exec] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->cancel(); });
383 void simcall_execution_set_priority(smx_activity_t exec, double priority)
385 simgrid::simix::simcall([exec, priority] {
386 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_priority(priority);
390 void simcall_execution_set_bound(smx_activity_t exec, double bound)
392 simgrid::simix::simcall(
393 [exec, bound] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_bound(bound); });
396 smx_activity_t simcall_execution_start(std::string name, std::string category, double flops_amount, double priority,
397 double bound, sg_host_t host)
399 return simgrid::simix::simcall([name, category, flops_amount, priority, bound, host] {
400 return SIMIX_execution_start(name, category, flops_amount, priority, bound, host);