Logo AND Algorithmique Numérique Distribuée

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