Logo AND Algorithmique Numérique Distribuée

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