Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
port a blocking simcall to the modernity
[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-2019. 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/MailboxImpl.hpp"
20 #include "src/kernel/activity/MutexImpl.hpp"
21 #include "src/mc/mc_replay.hpp"
22 #include "src/plugins/vm/VirtualMachineImpl.hpp"
23
24 #include "popping_bodies.cpp"
25
26 /**
27  * @ingroup simix_host_management
28  * @brief Waits for the completion of an execution synchro and destroy it.
29  *
30  * @param execution The execution synchro
31  */
32 e_smx_state_t simcall_execution_wait(const smx_activity_t& execution)
33 {
34   return (e_smx_state_t)simcall_BODY_execution_wait(static_cast<simgrid::kernel::activity::ExecImpl*>(execution.get()));
35 }
36
37 bool simcall_execution_test(const smx_activity_t& execution)
38 {
39   return simcall_BODY_execution_test(static_cast<simgrid::kernel::activity::ExecImpl*>(execution.get()));
40 }
41
42 unsigned int simcall_execution_waitany_for(simgrid::kernel::activity::ExecImpl* execs[], size_t count, double timeout)
43 {
44   return simcall_BODY_execution_waitany_for(execs, count, timeout);
45 }
46
47 void simcall_process_join(smx_actor_t process, double timeout)
48 {
49   SIMIX_process_self()->join(process, timeout);
50 }
51
52 /**
53  * @ingroup simix_process_management
54  * @brief Suspends an actor
55  */
56 void simcall_process_suspend(smx_actor_t process)
57 {
58   simcall_BODY_process_suspend(process);
59 }
60
61 /**
62  * @ingroup simix_process_management
63  * @brief Creates a new sleep SIMIX synchro.
64  *
65  * This function creates a SURF action and allocates the data necessary
66  * to create the SIMIX synchro. It can raise a HostFailureException if the
67  * host crashed. The default SIMIX name of the synchro is "sleep".
68  *
69  *   @param duration Time duration of the sleep.
70  *   @return A result telling whether the sleep was successful
71  */
72 e_smx_state_t simcall_process_sleep(double duration)
73 {
74   /* checking for infinite values */
75   xbt_assert(std::isfinite(duration), "duration is not finite!");
76   return (e_smx_state_t) simcall_BODY_process_sleep(duration);
77 }
78
79 /**
80  * @ingroup simix_comm_management
81  */
82 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
83                        size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
84                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
85                        double timeout)
86 {
87   /* checking for infinite values */
88   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
89   xbt_assert(std::isfinite(rate), "rate is not finite!");
90   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
91
92   xbt_assert(mbox, "No rendez-vous point defined for send");
93
94   if (MC_is_active() || MC_record_replay_is_active()) {
95     /* the model-checker wants two separate simcalls */
96     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
97     comm = simcall_comm_isend(sender, mbox, task_size, rate,
98         src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
99     simcall_comm_wait(comm, timeout);
100     comm = nullptr;
101   }
102   else {
103     simcall_BODY_comm_send(sender, mbox, task_size, rate, static_cast<unsigned char*>(src_buff), src_buff_size,
104                            match_fun, copy_data_fun, data, timeout);
105   }
106 }
107
108 /**
109  * @ingroup simix_comm_management
110  */
111 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
112                                   size_t src_buff_size,
113                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
114                                   void (*clean_fun)(void*),
115                                   void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
116                                   void* data, bool detached)
117 {
118   /* checking for infinite values */
119   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
120   xbt_assert(std::isfinite(rate), "rate is not finite!");
121
122   xbt_assert(mbox, "No rendez-vous point defined for isend");
123
124   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, static_cast<unsigned char*>(src_buff), src_buff_size,
125                                  match_fun, clean_fun, copy_data_fun, data, detached);
126 }
127
128 /**
129  * @ingroup simix_comm_management
130  */
131 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
132                        int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
133                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
134                        double timeout, double rate)
135 {
136   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
137   xbt_assert(mbox, "No rendez-vous point defined for recv");
138
139   if (MC_is_active() || MC_record_replay_is_active()) {
140     /* the model-checker wants two separate simcalls */
141     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
142     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
143                               match_fun, copy_data_fun, data, rate);
144     simcall_comm_wait(comm, timeout);
145     comm = nullptr;
146   }
147   else {
148     simcall_BODY_comm_recv(receiver, mbox, static_cast<unsigned char*>(dst_buff), dst_buff_size, match_fun,
149                            copy_data_fun, data, timeout, rate);
150   }
151 }
152 /**
153  * @ingroup simix_comm_management
154  */
155 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
156                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
157                                   void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
158                                   void* data, double rate)
159 {
160   xbt_assert(mbox, "No rendez-vous point defined for irecv");
161
162   return simcall_BODY_comm_irecv(receiver, mbox, static_cast<unsigned char*>(dst_buff), dst_buff_size, match_fun,
163                                  copy_data_fun, data, rate);
164 }
165
166 /**
167  * @ingroup simix_comm_management
168  */
169 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
170                                    int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
171 {
172   xbt_assert(mbox, "No rendez-vous point defined for iprobe");
173
174   return simgrid::simix::simcall([mbox, type, match_fun, data] { return mbox->iprobe(type, match_fun, data); });
175 }
176
177 /**
178  * @ingroup simix_comm_management
179  */
180 unsigned int simcall_comm_waitany(smx_activity_t comms[], size_t count, double timeout)
181 {
182   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
183   std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
184     return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
185   });
186   return simcall_BODY_comm_waitany(rcomms.get(), count, timeout);
187 }
188
189 unsigned int simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count, double timeout)
190 {
191   return simcall_BODY_comm_waitany(comms, count, timeout);
192 }
193
194 /**
195  * @ingroup simix_comm_management
196  */
197 int simcall_comm_testany(smx_activity_t comms[], size_t count)
198 {
199   if (count == 0)
200     return -1;
201   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
202   std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
203     return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
204   });
205   return simcall_BODY_comm_testany(rcomms.get(), count);
206 }
207
208 int simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count)
209 {
210   if (count == 0)
211     return -1;
212   return simcall_BODY_comm_testany(comms, count);
213 }
214
215 /**
216  * @ingroup simix_comm_management
217  */
218 void simcall_comm_wait(const smx_activity_t& comm, double timeout)
219 {
220   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
221   simcall_BODY_comm_wait(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
222 }
223
224 /**
225  * @ingroup simix_comm_management
226  *
227  */
228 bool simcall_comm_test(const smx_activity_t& comm)
229 {
230   return simcall_BODY_comm_test(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()));
231 }
232
233 /**
234  * @ingroup simix_synchro_management
235  *
236  */
237 smx_mutex_t simcall_mutex_init()
238 {
239   if (simix_global == nullptr) {
240     fprintf(stderr, "You must initialize the SimGrid engine before using it\n"); // We can't use xbt_die since we may
241                                                                                  // get there before the initialization
242     xbt_abort();
243   }
244   return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
245 }
246
247 /**
248  * @ingroup simix_synchro_management
249  *
250  */
251 void simcall_mutex_lock(smx_mutex_t mutex)
252 {
253   simcall_BODY_mutex_lock(mutex);
254 }
255
256 /**
257  * @ingroup simix_synchro_management
258  *
259  */
260 int simcall_mutex_trylock(smx_mutex_t mutex)
261 {
262   return simcall_BODY_mutex_trylock(mutex);
263 }
264
265 /**
266  * @ingroup simix_synchro_management
267  *
268  */
269 void simcall_mutex_unlock(smx_mutex_t mutex)
270 {
271   simcall_BODY_mutex_unlock(mutex);
272 }
273
274 /**
275  * @ingroup simix_synchro_management
276  *
277  */
278 smx_cond_t simcall_cond_init()
279 {
280   return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
281 }
282
283 /**
284  * @ingroup simix_synchro_management
285  *
286  */
287 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
288 {
289   simcall_BODY_cond_wait(cond, mutex);
290 }
291
292 /**
293  * @ingroup simix_synchro_management
294  *
295  */
296 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
297 {
298   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
299   return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
300 }
301
302 /**
303  * @ingroup simix_synchro_management
304  *
305  */
306 void simcall_sem_acquire(smx_sem_t sem)
307 {
308   simcall_BODY_sem_acquire(sem);
309 }
310
311 /**
312  * @ingroup simix_synchro_management
313  *
314  */
315 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
316 {
317   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
318   return simcall_BODY_sem_acquire_timeout(sem, timeout);
319 }
320
321 e_smx_state_t simcall_io_wait(const smx_activity_t& io)
322 {
323   return (e_smx_state_t)simcall_BODY_io_wait(static_cast<simgrid::kernel::activity::IoImpl*>(io.get()));
324 }
325
326 void simcall_run_kernel(std::function<void()> const& code)
327 {
328   simcall_BODY_run_kernel(&code);
329 }
330
331 void simcall_run_blocking(std::function<void()> const& code)
332 {
333   simcall_BODY_run_blocking(&code);
334 }
335
336 int simcall_mc_random(int min, int max) {
337   return simcall_BODY_mc_random(min, max);
338 }
339
340 /* ************************************************************************** */
341
342 /** @brief returns a printable string representing a simcall */
343 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
344   return simcall_names[kind];
345 }
346
347 namespace simgrid {
348 namespace simix {
349
350 void unblock(smx_actor_t actor)
351 {
352   xbt_assert(SIMIX_is_maestro());
353   actor->simcall_answer();
354 }
355 } // namespace simix
356 } // namespace simgrid
357
358 /* ****************************DEPRECATED CALLS******************************* */
359 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
360 {
361   simgrid::simix::simcall([process, kill_time] { process->set_kill_time(kill_time); });
362 }
363 void simcall_comm_cancel(smx_activity_t comm)
364 {
365   simgrid::simix::simcall([comm] { boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm)->cancel(); });
366 }
367 void simcall_execution_cancel(smx_activity_t exec)
368 {
369   simgrid::simix::simcall([exec] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->cancel(); });
370 }
371
372 void simcall_execution_set_bound(smx_activity_t exec, double bound)
373 {
374   simgrid::simix::simcall(
375       [exec, bound] { boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(exec)->set_bound(bound); });
376 }
377
378 // deprecated
379 smx_activity_t simcall_execution_start(const std::string& name, const std::string& category, double flops_amount,
380                                        double sharing_penalty, double bound, sg_host_t host)
381 {
382   return simgrid::simix::simcall([name, category, flops_amount, sharing_penalty, bound, host] {
383     simgrid::kernel::activity::ExecImpl* exec = new simgrid::kernel::activity::ExecImpl();
384     (*exec)
385         .set_name(name)
386         .set_tracing_category(category)
387         .set_host(host)
388         .set_sharing_penalty(sharing_penalty)
389         .set_bound(bound)
390         .set_flops_amount(flops_amount)
391         .start();
392     return simgrid::kernel::activity::ExecImplPtr(exec);
393   });
394 }
395
396 // deprecated
397 smx_activity_t simcall_execution_parallel_start(const std::string& name, int host_nb, const sg_host_t* host_list,
398                                                 const double* flops_amount, const double* bytes_amount, double rate,
399                                                 double timeout)
400 {
401   /* Check that we are not mixing VMs and PMs in the parallel task */
402   bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
403   for (int i = 1; i < host_nb; i++) {
404     bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
405     xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
406   }
407
408   /* checking for infinite values */
409   for (int i = 0; i < host_nb; ++i) {
410     if (flops_amount != nullptr)
411       xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
412     if (bytes_amount != nullptr) {
413       for (int j = 0; j < host_nb; ++j) {
414         xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]), "bytes_amount[%d+%d*%d] is not finite!", i, host_nb,
415                    j);
416       }
417     }
418   }
419   xbt_assert(std::isfinite(rate), "rate is not finite!");
420
421   std::vector<simgrid::s4u::Host*> hosts(host_list, host_list + host_nb);
422   std::vector<double> flops_parallel_amount;
423   std::vector<double> bytes_parallel_amount;
424   if (flops_amount != nullptr)
425     flops_parallel_amount = std::vector<double>(flops_amount, flops_amount + host_nb);
426   if (bytes_amount != nullptr)
427     bytes_parallel_amount = std::vector<double>(bytes_amount, bytes_amount + host_nb * host_nb);
428   return simgrid::simix::simcall([name, hosts, flops_parallel_amount, bytes_parallel_amount, timeout] {
429     simgrid::kernel::activity::ExecImpl* exec = new simgrid::kernel::activity::ExecImpl();
430     (*exec)
431         .set_name(name)
432         .set_hosts(hosts)
433         .set_timeout(timeout)
434         .set_flops_amounts(flops_parallel_amount)
435         .set_bytes_amounts(bytes_parallel_amount)
436         .start();
437     return simgrid::kernel::activity::ExecImplPtr(exec);
438   });
439 }
440
441 // deprecated
442 void SIMIX_comm_finish(smx_activity_t synchro)
443 {
444   boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();
445 }