Logo AND Algorithmique Numérique Distribuée

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