Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename the plugins from the command line, and document it
[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-2018. 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 <cmath>         /* std::isfinite() */
14
15 #include <functional>
16
17 #include "mc/mc.h"
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"
29 #include "xbt/ex.h"
30 #include "xbt/functional.hpp"
31
32 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
33
34 #include "popping_bodies.cpp"
35
36 /**
37  * \ingroup simix_process_management
38  * \brief Creates a synchro that executes some computation of an host.
39  *
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.
42  *
43  * \param name Name of the execution synchro to create
44  * \param flops_amount amount Computation amount (in flops)
45  * \param priority computation priority
46  * \param bound
47  * \param host host where the synchro will be executed
48  * \return A new SIMIX execution synchronization
49  */
50 smx_activity_t simcall_execution_start(const char* name, double flops_amount, double priority, double bound,
51                                        simgrid::s4u::Host* host)
52 {
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!");
56
57   return simgrid::simix::simcall([name, flops_amount, priority, bound, host] {
58     return SIMIX_execution_start(name, flops_amount, priority, bound, host);
59   });
60 }
61
62 /**
63  * \ingroup simix_process_management
64  * \brief Creates a synchro that may involve parallel computation on
65  * several hosts and communication between them.
66  *
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
76  */
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)
79 {
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);
87       }
88     }
89   }
90
91   xbt_assert(std::isfinite(rate), "rate is not finite!");
92
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);
95   });
96 }
97
98 /**
99  * \ingroup simix_process_management
100  * \brief Cancels an execution synchro.
101  *
102  * This functions stops the execution. It calls a surf function.
103  * \param execution The execution synchro to cancel
104  */
105 void simcall_execution_cancel(smx_activity_t execution)
106 {
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...
110     return;
111   simgrid::simix::simcall([exec] { exec->cancel(); });
112 }
113
114 /**
115  * \ingroup simix_process_management
116  * \brief Changes the priority of an execution synchro.
117  *
118  * This functions changes the priority only. It calls a surf function.
119  * \param execution The execution synchro
120  * \param priority The new priority
121  */
122 void simcall_execution_set_priority(smx_activity_t execution, double priority)
123 {
124   /* checking for infinite values */
125   xbt_assert(std::isfinite(priority), "priority is not finite!");
126   simgrid::simix::simcall([execution, priority] {
127
128     simgrid::kernel::activity::ExecImplPtr exec =
129         boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
130     exec->set_priority(priority);
131   });
132 }
133
134 /**
135  * \ingroup simix_process_management
136  * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
137  *
138  * This functions changes the capping only. It calls a surf function.
139  * \param execution The execution synchro
140  * \param bound The new bound
141  */
142 void simcall_execution_set_bound(smx_activity_t execution, double bound)
143 {
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);
148   });
149 }
150
151 /**
152  * \ingroup simix_host_management
153  * \brief Waits for the completion of an execution synchro and destroy it.
154  *
155  * \param execution The execution synchro
156  */
157 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
158 {
159   return (e_smx_state_t) simcall_BODY_execution_wait(execution);
160 }
161
162 e_smx_state_t simcall_execution_test(smx_activity_t execution)
163 {
164   return (e_smx_state_t)simcall_BODY_execution_test(execution);
165 }
166
167 void simcall_process_join(smx_actor_t process, double timeout)
168 {
169   simcall_BODY_process_join(process, timeout);
170 }
171
172 /**
173  * \ingroup simix_process_management
174  * \brief Suspends a process.
175  *
176  * This function suspends the process by suspending the synchro
177  * it was waiting for completion.
178  *
179  * \param process a SIMIX process
180  */
181 void simcall_process_suspend(smx_actor_t process)
182 {
183   simcall_BODY_process_suspend(process);
184 }
185
186 /**
187  * \ingroup simix_process_management
188  * \brief Set the user data of a #smx_actor_t.
189  *
190  * This functions sets the user data associated to \a process.
191  * \param process SIMIX process
192  * \param data User data
193  */
194 void simcall_process_set_data(smx_actor_t process, void *data)
195 {
196   simgrid::simix::simcall([process, data] { process->setUserData(data); });
197 }
198
199 /**
200  * \ingroup simix_process_management
201  * \brief Set the kill time of a process.
202  */
203 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
204 {
205
206   if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
207     return;
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;
212   });
213 }
214
215 /**
216  * \ingroup simix_process_management
217  * \brief Creates a new sleep SIMIX synchro.
218  *
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".
222  *
223  *   \param duration Time duration of the sleep.
224  *   \return A result telling whether the sleep was successful
225  */
226 e_smx_state_t simcall_process_sleep(double duration)
227 {
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);
231 }
232
233 /**
234  * \ingroup simix_comm_management
235  */
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)
239 {
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!");
244
245   xbt_assert(mbox, "No rendez-vous point defined for send");
246
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);
253     comm = nullptr;
254   }
255   else {
256     simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
257                          match_fun, copy_data_fun, data, timeout);
258   }
259 }
260
261 /**
262  * \ingroup simix_comm_management
263  */
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)
269 {
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!");
273
274   xbt_assert(mbox, "No rendez-vous point defined for isend");
275
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);
279 }
280
281 /**
282  * \ingroup simix_comm_management
283  */
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)
287 {
288   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
289   xbt_assert(mbox, "No rendez-vous point defined for recv");
290
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);
297     comm = nullptr;
298   }
299   else {
300     simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
301                            match_fun, copy_data_fun, data, timeout, rate);
302   }
303 }
304 /**
305  * \ingroup simix_comm_management
306  */
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)
310 {
311   xbt_assert(mbox, "No rendez-vous point defined for irecv");
312
313   return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
314                                  match_fun, copy_data_fun, data, rate);
315 }
316
317 /**
318  * \ingroup simix_comm_management
319  */
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)
322 {
323   xbt_assert(mbox, "No rendez-vous point defined for iprobe");
324
325   return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
326 }
327
328 /**
329  * \ingroup simix_comm_management
330  */
331 void simcall_comm_cancel(smx_activity_t synchro)
332 {
333   simgrid::simix::simcall([synchro] {
334     simgrid::kernel::activity::CommImplPtr comm =
335         boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
336     comm->cancel();
337   });
338 }
339
340 /**
341  * \ingroup simix_comm_management
342  */
343 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
344 {
345   return simcall_BODY_comm_waitany(comms, timeout);
346 }
347
348 /**
349  * \ingroup simix_comm_management
350  */
351 int simcall_comm_testany(smx_activity_t* comms, size_t count)
352 {
353   if (count == 0)
354     return -1;
355   return simcall_BODY_comm_testany(comms, count);
356 }
357
358 /**
359  * \ingroup simix_comm_management
360  */
361 void simcall_comm_wait(smx_activity_t comm, double timeout)
362 {
363   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
364   simcall_BODY_comm_wait(comm, timeout);
365 }
366
367 /**
368  * \brief Set the category of an synchro.
369  *
370  * This functions changes the category only. It calls a surf function.
371  * \param synchro The execution synchro
372  * \param category The tracing category
373  */
374 void simcall_set_category(smx_activity_t synchro, const char *category)
375 {
376   if (category == nullptr) {
377     return;
378   }
379   simgrid::simix::simcall([synchro, category] { SIMIX_set_category(synchro, category); });
380 }
381
382 /**
383  * \ingroup simix_comm_management
384  *
385  */
386 int simcall_comm_test(smx_activity_t comm)
387 {
388   return simcall_BODY_comm_test(comm);
389 }
390
391 /**
392  * \ingroup simix_synchro_management
393  *
394  */
395 smx_mutex_t simcall_mutex_init()
396 {
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
399     xbt_abort();
400   }
401   return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
402 }
403
404 /**
405  * \ingroup simix_synchro_management
406  *
407  */
408 void simcall_mutex_lock(smx_mutex_t mutex)
409 {
410   simcall_BODY_mutex_lock(mutex);
411 }
412
413 /**
414  * \ingroup simix_synchro_management
415  *
416  */
417 int simcall_mutex_trylock(smx_mutex_t mutex)
418 {
419   return simcall_BODY_mutex_trylock(mutex);
420 }
421
422 /**
423  * \ingroup simix_synchro_management
424  *
425  */
426 void simcall_mutex_unlock(smx_mutex_t mutex)
427 {
428   simcall_BODY_mutex_unlock(mutex);
429 }
430
431 /**
432  * \ingroup simix_synchro_management
433  *
434  */
435 smx_cond_t simcall_cond_init()
436 {
437   return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
438 }
439
440 /**
441  * \ingroup simix_synchro_management
442  *
443  */
444 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
445 {
446   simcall_BODY_cond_wait(cond, mutex);
447 }
448
449 /**
450  * \ingroup simix_synchro_management
451  *
452  */
453 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
454 {
455   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
456   return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
457 }
458
459 /**
460  * \ingroup simix_synchro_management
461  *
462  */
463 void simcall_sem_acquire(smx_sem_t sem)
464 {
465   simcall_BODY_sem_acquire(sem);
466 }
467
468 /**
469  * \ingroup simix_synchro_management
470  *
471  */
472 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
473 {
474   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
475   return simcall_BODY_sem_acquire_timeout(sem, timeout);
476 }
477
478 sg_size_t simcall_storage_read(surf_storage_t st, sg_size_t size)
479 {
480   return simcall_BODY_storage_read(st, size);
481 }
482
483 sg_size_t simcall_storage_write(surf_storage_t st, sg_size_t size)
484 {
485   return simcall_BODY_storage_write(st, size);
486 }
487
488 void simcall_run_kernel(std::function<void()> const& code)
489 {
490   simcall_BODY_run_kernel(&code);
491 }
492
493 void simcall_run_blocking(std::function<void()> const& code)
494 {
495   simcall_BODY_run_blocking(&code);
496 }
497
498 int simcall_mc_random(int min, int max) {
499   return simcall_BODY_mc_random(min, max);
500 }
501
502 /* ************************************************************************** */
503
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];
507 }
508
509 namespace simgrid {
510 namespace simix {
511
512 void unblock(smx_actor_t process)
513 {
514   xbt_assert(SIMIX_is_maestro());
515   SIMIX_simcall_answer(&process->simcall);
516 }
517
518 }
519 }