Logo AND Algorithmique Numérique Distribuée

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