Logo AND Algorithmique Numérique Distribuée

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