Logo AND Algorithmique Numérique Distribuée

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