Logo AND Algorithmique Numérique Distribuée

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