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-2018. 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. */
13 #include <cmath> /* std::isfinite() */
18 #include "simgrid/s4u/VirtualMachine.hpp"
19 #include "simgrid/simix.hpp"
20 #include "simgrid/simix/blocking_simcall.hpp"
21 #include "smx_private.hpp"
22 #include "src/kernel/activity/CommImpl.hpp"
23 #include "src/kernel/activity/ConditionVariableImpl.hpp"
24 #include "src/kernel/activity/MutexImpl.hpp"
25 #include "src/mc/mc_forward.hpp"
26 #include "src/mc/mc_replay.hpp"
27 #include "src/plugins/vm/VirtualMachineImpl.hpp"
28 #include "src/simix/smx_host_private.hpp"
30 #include "xbt/functional.hpp"
32 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
34 #include "popping_bodies.cpp"
37 * \ingroup simix_process_management
38 * \brief Creates a synchro that executes some computation of an host.
40 * This function creates a SURF action and allocates the data necessary
41 * to create the SIMIX synchro. It can raise a host_error exception if the host crashed.
43 * \param name Name of the execution synchro to create
44 * \param flops_amount amount Computation amount (in flops)
45 * \param priority computation priority
47 * \param host host where the synchro will be executed
48 * \return A new SIMIX execution synchronization
50 smx_activity_t simcall_execution_start(const char* name, double flops_amount, double priority, double bound,
51 simgrid::s4u::Host* host)
53 /* checking for infinite values */
54 xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
55 xbt_assert(std::isfinite(priority), "priority is not finite!");
57 return simgrid::simix::simcall([name, flops_amount, priority, bound, host] {
58 return SIMIX_execution_start(name, flops_amount, priority, bound, host);
63 * \ingroup simix_process_management
64 * \brief Creates a synchro that may involve parallel computation on
65 * several hosts and communication between them.
67 * \param name Name of the execution synchro to create
68 * \param host_nb Number of hosts where the synchro will be executed
69 * \param host_list Array (of size host_nb) of hosts where the synchro will be executed
70 * \param flops_amount Array (of size host_nb) of computation amount of hosts (in bytes)
71 * \param bytes_amount Array (of size host_nb * host_nb) representing the communication
72 * amount between each pair of hosts
73 * \param rate the SURF action rate
74 * \param timeout timeout
75 * \return A new SIMIX execution synchronization
77 smx_activity_t simcall_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list,
78 double* flops_amount, double* bytes_amount, double rate, double timeout)
80 /* checking for infinite values */
81 for (int i = 0 ; i < host_nb ; ++i) {
82 xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
83 if (bytes_amount != nullptr) {
84 for (int j = 0 ; j < host_nb ; ++j) {
85 xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
86 "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
91 xbt_assert(std::isfinite(rate), "rate is not finite!");
93 return simgrid::simix::simcall([name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout] {
94 return SIMIX_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout);
99 * \ingroup simix_process_management
100 * \brief Cancels an execution synchro.
102 * This functions stops the execution. It calls a surf function.
103 * \param execution The execution synchro to cancel
105 void simcall_execution_cancel(smx_activity_t execution)
107 simgrid::kernel::activity::ExecImplPtr exec =
108 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
109 if (exec->surf_action_ == nullptr) // FIXME: One test fails if I remove this, but I don't get why...
111 simgrid::simix::simcall([exec] { exec->cancel(); });
115 * \ingroup simix_process_management
116 * \brief Changes the priority of an execution synchro.
118 * This functions changes the priority only. It calls a surf function.
119 * \param execution The execution synchro
120 * \param priority The new priority
122 void simcall_execution_set_priority(smx_activity_t execution, double priority)
124 /* checking for infinite values */
125 xbt_assert(std::isfinite(priority), "priority is not finite!");
126 simgrid::simix::simcall([execution, priority] {
128 simgrid::kernel::activity::ExecImplPtr exec =
129 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
130 exec->set_priority(priority);
135 * \ingroup simix_process_management
136 * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
138 * This functions changes the capping only. It calls a surf function.
139 * \param execution The execution synchro
140 * \param bound The new bound
142 void simcall_execution_set_bound(smx_activity_t execution, double bound)
144 simgrid::simix::simcall([execution, bound] {
145 simgrid::kernel::activity::ExecImplPtr exec =
146 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
147 exec->set_bound(bound);
152 * \ingroup simix_host_management
153 * \brief Waits for the completion of an execution synchro and destroy it.
155 * \param execution The execution synchro
157 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
159 return (e_smx_state_t) simcall_BODY_execution_wait(execution);
162 e_smx_state_t simcall_execution_test(smx_activity_t execution)
164 return (e_smx_state_t)simcall_BODY_execution_test(execution);
167 void simcall_process_join(smx_actor_t process, double timeout)
169 simcall_BODY_process_join(process, timeout);
173 * \ingroup simix_process_management
174 * \brief Suspends a process.
176 * This function suspends the process by suspending the synchro
177 * it was waiting for completion.
179 * \param process a SIMIX process
181 void simcall_process_suspend(smx_actor_t process)
183 simcall_BODY_process_suspend(process);
187 * \ingroup simix_process_management
188 * \brief Set the user data of a #smx_actor_t.
190 * This functions sets the user data associated to \a process.
191 * \param process SIMIX process
192 * \param data User data
194 void simcall_process_set_data(smx_actor_t process, void *data)
196 simgrid::simix::simcall([process, data] { process->setUserData(data); });
200 * \ingroup simix_process_management
201 * \brief Set the kill time of a process.
203 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
206 if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
208 XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->get_cname(), process->host->get_cname());
209 process->kill_timer = SIMIX_timer_set(kill_time, [process] {
210 simix_global->kill_process_function(process);
211 process->kill_timer=nullptr;
216 * \ingroup simix_process_management
217 * \brief Creates a new sleep SIMIX synchro.
219 * This function creates a SURF action and allocates the data necessary
220 * to create the SIMIX synchro. It can raise a host_error exception if the
221 * host crashed. The default SIMIX name of the synchro is "sleep".
223 * \param duration Time duration of the sleep.
224 * \return A result telling whether the sleep was successful
226 e_smx_state_t simcall_process_sleep(double duration)
228 /* checking for infinite values */
229 xbt_assert(std::isfinite(duration), "duration is not finite!");
230 return (e_smx_state_t) simcall_BODY_process_sleep(duration);
234 * \ingroup simix_comm_management
236 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
237 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
238 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
240 /* checking for infinite values */
241 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
242 xbt_assert(std::isfinite(rate), "rate is not finite!");
243 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
245 xbt_assert(mbox, "No rendez-vous point defined for send");
247 if (MC_is_active() || MC_record_replay_is_active()) {
248 /* the model-checker wants two separate simcalls */
249 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
250 comm = simcall_comm_isend(sender, mbox, task_size, rate,
251 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
252 simcall_comm_wait(comm, timeout);
256 simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
257 match_fun, copy_data_fun, data, timeout);
262 * \ingroup simix_comm_management
264 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
265 size_t src_buff_size,
266 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
267 void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
268 void* data, int detached)
270 /* checking for infinite values */
271 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
272 xbt_assert(std::isfinite(rate), "rate is not finite!");
274 xbt_assert(mbox, "No rendez-vous point defined for isend");
276 return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
277 src_buff_size, match_fun,
278 clean_fun, copy_data_fun, data, detached);
282 * \ingroup simix_comm_management
284 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
285 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
286 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
288 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
289 xbt_assert(mbox, "No rendez-vous point defined for recv");
291 if (MC_is_active() || MC_record_replay_is_active()) {
292 /* the model-checker wants two separate simcalls */
293 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
294 comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
295 match_fun, copy_data_fun, data, rate);
296 simcall_comm_wait(comm, timeout);
300 simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
301 match_fun, copy_data_fun, data, timeout, rate);
305 * \ingroup simix_comm_management
307 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
308 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
309 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
311 xbt_assert(mbox, "No rendez-vous point defined for irecv");
313 return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
314 match_fun, copy_data_fun, data, rate);
318 * \ingroup simix_comm_management
320 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
321 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
323 xbt_assert(mbox, "No rendez-vous point defined for iprobe");
325 return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
329 * \ingroup simix_comm_management
331 void simcall_comm_cancel(smx_activity_t synchro)
333 simgrid::simix::simcall([synchro] {
334 simgrid::kernel::activity::CommImplPtr comm =
335 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
341 * \ingroup simix_comm_management
343 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
345 return simcall_BODY_comm_waitany(comms, timeout);
349 * \ingroup simix_comm_management
351 int simcall_comm_testany(smx_activity_t* comms, size_t count)
355 return simcall_BODY_comm_testany(comms, count);
359 * \ingroup simix_comm_management
361 void simcall_comm_wait(smx_activity_t comm, double timeout)
363 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
364 simcall_BODY_comm_wait(comm, timeout);
368 * \brief Set the category of an synchro.
370 * This functions changes the category only. It calls a surf function.
371 * \param synchro The execution synchro
372 * \param category The tracing category
374 void simcall_set_category(smx_activity_t synchro, const char *category)
376 if (category == nullptr) {
379 simgrid::simix::simcall([synchro, category] { SIMIX_set_category(synchro, category); });
383 * \ingroup simix_comm_management
386 int simcall_comm_test(smx_activity_t comm)
388 return simcall_BODY_comm_test(comm);
392 * \ingroup simix_synchro_management
395 smx_mutex_t simcall_mutex_init()
397 if (not simix_global) {
398 fprintf(stderr,"You must run MSG_init before using MSG\n"); // We can't use xbt_die since we may get there before the initialization
401 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
405 * \ingroup simix_synchro_management
408 void simcall_mutex_lock(smx_mutex_t mutex)
410 simcall_BODY_mutex_lock(mutex);
414 * \ingroup simix_synchro_management
417 int simcall_mutex_trylock(smx_mutex_t mutex)
419 return simcall_BODY_mutex_trylock(mutex);
423 * \ingroup simix_synchro_management
426 void simcall_mutex_unlock(smx_mutex_t mutex)
428 simcall_BODY_mutex_unlock(mutex);
432 * \ingroup simix_synchro_management
435 smx_cond_t simcall_cond_init()
437 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
441 * \ingroup simix_synchro_management
444 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
446 simcall_BODY_cond_wait(cond, mutex);
450 * \ingroup simix_synchro_management
453 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
455 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
456 return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
460 * \ingroup simix_synchro_management
463 void simcall_sem_acquire(smx_sem_t sem)
465 simcall_BODY_sem_acquire(sem);
469 * \ingroup simix_synchro_management
472 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
474 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
475 return simcall_BODY_sem_acquire_timeout(sem, timeout);
478 sg_size_t simcall_storage_read(surf_storage_t st, sg_size_t size)
480 return simcall_BODY_storage_read(st, size);
483 sg_size_t simcall_storage_write(surf_storage_t st, sg_size_t size)
485 return simcall_BODY_storage_write(st, size);
488 void simcall_run_kernel(std::function<void()> const& code)
490 simcall_BODY_run_kernel(&code);
493 void simcall_run_blocking(std::function<void()> const& code)
495 simcall_BODY_run_blocking(&code);
498 int simcall_mc_random(int min, int max) {
499 return simcall_BODY_mc_random(min, max);
502 /* ************************************************************************** */
504 /** @brief returns a printable string representing a simcall */
505 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
506 return simcall_names[kind];
512 void unblock(smx_actor_t process)
514 xbt_assert(SIMIX_is_maestro());
515 SIMIX_simcall_answer(&process->simcall);