Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
13b9cc1bbd4b9d2fffa2cfe7a8529359bf8d0ee6
[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 void simcall_call(smx_actor_t actor)
37 {
38   if (actor != simix_global->maestro_process) {
39     XBT_DEBUG("Yield actor '%s' on simcall %s (%d)", actor->get_cname(), SIMIX_simcall_name(actor->simcall.call),
40               (int)actor->simcall.call);
41     SIMIX_process_yield(actor);
42   } else {
43     SIMIX_simcall_handle(&actor->simcall, 0);
44   }
45 }
46
47 /**
48  * \ingroup simix_process_management
49  * \brief Creates a synchro that executes some computation of an host.
50  *
51  * This function creates a SURF action and allocates the data necessary
52  * to create the SIMIX synchro. It can raise a host_error exception if the host crashed.
53  *
54  * \param name Name of the execution synchro to create
55  * \param flops_amount amount Computation amount (in flops)
56  * \param priority computation priority
57  * \param bound
58  * \param host host where the synchro will be executed
59  * \return A new SIMIX execution synchronization
60  */
61 smx_activity_t simcall_execution_start(const char* name, double flops_amount, double priority, double bound,
62                                        simgrid::s4u::Host* host)
63 {
64   /* checking for infinite values */
65   xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
66   xbt_assert(std::isfinite(priority), "priority is not finite!");
67
68   return simgrid::simix::simcall([name, flops_amount, priority, bound, host] {
69     return SIMIX_execution_start(name, flops_amount, priority, bound, host);
70   });
71 }
72
73 /**
74  * \ingroup simix_process_management
75  * \brief Creates a synchro that may involve parallel computation on
76  * several hosts and communication between them.
77  *
78  * \param name Name of the execution synchro to create
79  * \param host_nb Number of hosts where the synchro will be executed
80  * \param host_list Array (of size host_nb) of hosts where the synchro will be executed
81  * \param flops_amount Array (of size host_nb) of computation amount of hosts (in bytes)
82  * \param bytes_amount Array (of size host_nb * host_nb) representing the communication
83  * amount between each pair of hosts
84  * \param rate the SURF action rate
85  * \param timeout timeout
86  * \return A new SIMIX execution synchronization
87  */
88 smx_activity_t simcall_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list,
89                                                 double* flops_amount, double* bytes_amount, double rate, double timeout)
90 {
91   /* checking for infinite values */
92   for (int i = 0 ; i < host_nb ; ++i) {
93     xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
94     if (bytes_amount != nullptr) {
95       for (int j = 0 ; j < host_nb ; ++j) {
96         xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
97                    "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
98       }
99     }
100   }
101
102   xbt_assert(std::isfinite(rate), "rate is not finite!");
103
104   return simgrid::simix::simcall([name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout] {
105     return SIMIX_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout);
106   });
107 }
108
109 /**
110  * \ingroup simix_process_management
111  * \brief Cancels an execution synchro.
112  *
113  * This functions stops the execution. It calls a surf function.
114  * \param execution The execution synchro to cancel
115  */
116 void simcall_execution_cancel(smx_activity_t execution)
117 {
118   simgrid::kernel::activity::ExecImplPtr exec =
119       boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
120   if (exec->surf_action_ == nullptr) // FIXME: One test fails if I remove this, but I don't get why...
121     return;
122   simgrid::simix::simcall([exec] { exec->cancel(); });
123 }
124
125 /**
126  * \ingroup simix_process_management
127  * \brief Changes the priority of an execution synchro.
128  *
129  * This functions changes the priority only. It calls a surf function.
130  * \param execution The execution synchro
131  * \param priority The new priority
132  */
133 void simcall_execution_set_priority(smx_activity_t execution, double priority)
134 {
135   /* checking for infinite values */
136   xbt_assert(std::isfinite(priority), "priority is not finite!");
137   simgrid::simix::simcall([execution, priority] {
138
139     simgrid::kernel::activity::ExecImplPtr exec =
140         boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
141     exec->set_priority(priority);
142   });
143 }
144
145 /**
146  * \ingroup simix_process_management
147  * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
148  *
149  * This functions changes the capping only. It calls a surf function.
150  * \param execution The execution synchro
151  * \param bound The new bound
152  */
153 void simcall_execution_set_bound(smx_activity_t execution, double bound)
154 {
155   simgrid::simix::simcall([execution, bound] {
156     simgrid::kernel::activity::ExecImplPtr exec =
157         boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
158     exec->set_bound(bound);
159   });
160 }
161
162 /**
163  * \ingroup simix_host_management
164  * \brief Waits for the completion of an execution synchro and destroy it.
165  *
166  * \param execution The execution synchro
167  */
168 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
169 {
170   return (e_smx_state_t) simcall_BODY_execution_wait(execution);
171 }
172
173 e_smx_state_t simcall_execution_test(smx_activity_t execution)
174 {
175   return (e_smx_state_t)simcall_BODY_execution_test(execution);
176 }
177
178 void simcall_process_join(smx_actor_t process, double timeout)
179 {
180   simcall_BODY_process_join(process, timeout);
181 }
182
183 /**
184  * \ingroup simix_process_management
185  * \brief Suspends a process.
186  *
187  * This function suspends the process by suspending the synchro
188  * it was waiting for completion.
189  *
190  * \param process a SIMIX process
191  */
192 void simcall_process_suspend(smx_actor_t process)
193 {
194   simcall_BODY_process_suspend(process);
195 }
196
197 /**
198  * \ingroup simix_process_management
199  * \brief Set the user data of a #smx_actor_t.
200  *
201  * This functions sets the user data associated to \a process.
202  * \param process SIMIX process
203  * \param data User data
204  */
205 void simcall_process_set_data(smx_actor_t process, void *data)
206 {
207   simgrid::simix::simcall([process, data] { process->setUserData(data); });
208 }
209
210 /**
211  * \ingroup simix_process_management
212  * \brief Set the kill time of a process.
213  */
214 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
215 {
216
217   if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
218     return;
219   XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->get_cname(), process->host->get_cname());
220   process->kill_timer = SIMIX_timer_set(kill_time, [process] {
221     simix_global->kill_process_function(process);
222     process->kill_timer=nullptr;
223   });
224 }
225
226 /**
227  * \ingroup simix_process_management
228  * \brief Creates a new sleep SIMIX synchro.
229  *
230  * This function creates a SURF action and allocates the data necessary
231  * to create the SIMIX synchro. It can raise a host_error exception if the
232  * host crashed. The default SIMIX name of the synchro is "sleep".
233  *
234  *   \param duration Time duration of the sleep.
235  *   \return A result telling whether the sleep was successful
236  */
237 e_smx_state_t simcall_process_sleep(double duration)
238 {
239   /* checking for infinite values */
240   xbt_assert(std::isfinite(duration), "duration is not finite!");
241   return (e_smx_state_t) simcall_BODY_process_sleep(duration);
242 }
243
244 /**
245  * \ingroup simix_comm_management
246  */
247 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
248                        size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
249                        void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
250 {
251   /* checking for infinite values */
252   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
253   xbt_assert(std::isfinite(rate), "rate is not finite!");
254   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
255
256   xbt_assert(mbox, "No rendez-vous point defined for send");
257
258   if (MC_is_active() || MC_record_replay_is_active()) {
259     /* the model-checker wants two separate simcalls */
260     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
261     comm = simcall_comm_isend(sender, mbox, task_size, rate,
262         src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
263     simcall_comm_wait(comm, timeout);
264     comm = nullptr;
265   }
266   else {
267     simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
268                          match_fun, copy_data_fun, data, timeout);
269   }
270 }
271
272 /**
273  * \ingroup simix_comm_management
274  */
275 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
276                                   size_t src_buff_size,
277                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
278                                   void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
279                                   void* data, int detached)
280 {
281   /* checking for infinite values */
282   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
283   xbt_assert(std::isfinite(rate), "rate is not finite!");
284
285   xbt_assert(mbox, "No rendez-vous point defined for isend");
286
287   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
288                                  src_buff_size, match_fun,
289                                  clean_fun, copy_data_fun, data, detached);
290 }
291
292 /**
293  * \ingroup simix_comm_management
294  */
295 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
296                        int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
297                        void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
298 {
299   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
300   xbt_assert(mbox, "No rendez-vous point defined for recv");
301
302   if (MC_is_active() || MC_record_replay_is_active()) {
303     /* the model-checker wants two separate simcalls */
304     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
305     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
306                               match_fun, copy_data_fun, data, rate);
307     simcall_comm_wait(comm, timeout);
308     comm = nullptr;
309   }
310   else {
311     simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
312                            match_fun, copy_data_fun, data, timeout, rate);
313   }
314 }
315 /**
316  * \ingroup simix_comm_management
317  */
318 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
319                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
320                                   void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
321 {
322   xbt_assert(mbox, "No rendez-vous point defined for irecv");
323
324   return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
325                                  match_fun, copy_data_fun, data, rate);
326 }
327
328 /**
329  * \ingroup simix_comm_management
330  */
331 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
332                                    int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
333 {
334   xbt_assert(mbox, "No rendez-vous point defined for iprobe");
335
336   return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
337 }
338
339 /**
340  * \ingroup simix_comm_management
341  */
342 void simcall_comm_cancel(smx_activity_t synchro)
343 {
344   simgrid::simix::simcall([synchro] {
345     simgrid::kernel::activity::CommImplPtr comm =
346         boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
347     comm->cancel();
348   });
349 }
350
351 /**
352  * \ingroup simix_comm_management
353  */
354 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
355 {
356   return simcall_BODY_comm_waitany(comms, timeout);
357 }
358
359 /**
360  * \ingroup simix_comm_management
361  */
362 int simcall_comm_testany(smx_activity_t* comms, size_t count)
363 {
364   if (count == 0)
365     return -1;
366   return simcall_BODY_comm_testany(comms, count);
367 }
368
369 /**
370  * \ingroup simix_comm_management
371  */
372 void simcall_comm_wait(smx_activity_t comm, double timeout)
373 {
374   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
375   simcall_BODY_comm_wait(comm, timeout);
376 }
377
378 /**
379  * \brief Set the category of an synchro.
380  *
381  * This functions changes the category only. It calls a surf function.
382  * \param synchro The execution synchro
383  * \param category The tracing category
384  */
385 void simcall_set_category(smx_activity_t synchro, const char *category)
386 {
387   if (category == nullptr) {
388     return;
389   }
390   simgrid::simix::simcall([synchro, category] { SIMIX_set_category(synchro, category); });
391 }
392
393 /**
394  * \ingroup simix_comm_management
395  *
396  */
397 int simcall_comm_test(smx_activity_t comm)
398 {
399   return simcall_BODY_comm_test(comm);
400 }
401
402 /**
403  * \ingroup simix_synchro_management
404  *
405  */
406 smx_mutex_t simcall_mutex_init()
407 {
408   if (not simix_global) {
409     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
410     xbt_abort();
411   }
412   return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
413 }
414
415 /**
416  * \ingroup simix_synchro_management
417  *
418  */
419 void simcall_mutex_lock(smx_mutex_t mutex)
420 {
421   simcall_BODY_mutex_lock(mutex);
422 }
423
424 /**
425  * \ingroup simix_synchro_management
426  *
427  */
428 int simcall_mutex_trylock(smx_mutex_t mutex)
429 {
430   return simcall_BODY_mutex_trylock(mutex);
431 }
432
433 /**
434  * \ingroup simix_synchro_management
435  *
436  */
437 void simcall_mutex_unlock(smx_mutex_t mutex)
438 {
439   simcall_BODY_mutex_unlock(mutex);
440 }
441
442 /**
443  * \ingroup simix_synchro_management
444  *
445  */
446 smx_cond_t simcall_cond_init()
447 {
448   return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
449 }
450
451 /**
452  * \ingroup simix_synchro_management
453  *
454  */
455 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
456 {
457   simcall_BODY_cond_wait(cond, mutex);
458 }
459
460 /**
461  * \ingroup simix_synchro_management
462  *
463  */
464 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
465 {
466   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
467   return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
468 }
469
470 /**
471  * \ingroup simix_synchro_management
472  *
473  */
474 void simcall_sem_acquire(smx_sem_t sem)
475 {
476   simcall_BODY_sem_acquire(sem);
477 }
478
479 /**
480  * \ingroup simix_synchro_management
481  *
482  */
483 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
484 {
485   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
486   return simcall_BODY_sem_acquire_timeout(sem, timeout);
487 }
488
489 sg_size_t simcall_storage_read(surf_storage_t st, sg_size_t size)
490 {
491   return simcall_BODY_storage_read(st, size);
492 }
493
494 sg_size_t simcall_storage_write(surf_storage_t st, sg_size_t size)
495 {
496   return simcall_BODY_storage_write(st, size);
497 }
498
499 void simcall_run_kernel(std::function<void()> const& code)
500 {
501   simcall_BODY_run_kernel(&code);
502 }
503
504 void simcall_run_blocking(std::function<void()> const& code)
505 {
506   simcall_BODY_run_blocking(&code);
507 }
508
509 int simcall_mc_random(int min, int max) {
510   return simcall_BODY_mc_random(min, max);
511 }
512
513 /* ************************************************************************** */
514
515 /** @brief returns a printable string representing a simcall */
516 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
517   return simcall_names[kind];
518 }
519
520 namespace simgrid {
521 namespace simix {
522
523 void unblock(smx_actor_t process)
524 {
525   xbt_assert(SIMIX_is_maestro());
526   SIMIX_simcall_answer(&process->simcall);
527 }
528
529 }
530 }