Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
387fd23dc60f94ccf4f0b60d13600e88c1abf82b
[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) // XBT_DEPRECATED_v328
48 {
49   SIMIX_process_self()->join(process, timeout);
50 }
51
52 void simcall_process_suspend(smx_actor_t process) // XBT_DEPRECATED_v328
53 {
54   process->iface()->suspend();
55 }
56
57 e_smx_state_t simcall_process_sleep(double duration) // XBT_DEPRECATED_v329
58 {
59   SIMIX_process_self()->sleep(duration);
60   return SIMIX_DONE;
61 }
62
63 /**
64  * @ingroup simix_comm_management
65  */
66 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
67                        size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
68                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
69                        double timeout)
70 {
71   /* checking for infinite values */
72   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
73   xbt_assert(std::isfinite(rate), "rate is not finite!");
74   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
75
76   xbt_assert(mbox, "No rendez-vous point defined for send");
77
78   if (MC_is_active() || MC_record_replay_is_active()) {
79     /* the model-checker wants two separate simcalls */
80     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
81     comm = simcall_comm_isend(sender, mbox, task_size, rate,
82         src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
83     simcall_comm_wait(comm, timeout);
84     comm = nullptr;
85   }
86   else {
87     simcall_BODY_comm_send(sender, mbox, task_size, rate, static_cast<unsigned char*>(src_buff), src_buff_size,
88                            match_fun, copy_data_fun, data, timeout);
89   }
90 }
91
92 /**
93  * @ingroup simix_comm_management
94  */
95 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
96                                   size_t src_buff_size,
97                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
98                                   void (*clean_fun)(void*),
99                                   void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
100                                   void* data, bool detached)
101 {
102   /* checking for infinite values */
103   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
104   xbt_assert(std::isfinite(rate), "rate is not finite!");
105
106   xbt_assert(mbox, "No rendez-vous point defined for isend");
107
108   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, static_cast<unsigned char*>(src_buff), src_buff_size,
109                                  match_fun, clean_fun, copy_data_fun, data, detached);
110 }
111
112 /**
113  * @ingroup simix_comm_management
114  */
115 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
116                        int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
117                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
118                        double timeout, double rate)
119 {
120   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
121   xbt_assert(mbox, "No rendez-vous point defined for recv");
122
123   if (MC_is_active() || MC_record_replay_is_active()) {
124     /* the model-checker wants two separate simcalls */
125     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
126     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
127                               match_fun, copy_data_fun, data, rate);
128     simcall_comm_wait(comm, timeout);
129     comm = nullptr;
130   }
131   else {
132     simcall_BODY_comm_recv(receiver, mbox, static_cast<unsigned char*>(dst_buff), dst_buff_size, match_fun,
133                            copy_data_fun, data, timeout, rate);
134   }
135 }
136 /**
137  * @ingroup simix_comm_management
138  */
139 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
140                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
141                                   void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
142                                   void* data, double rate)
143 {
144   xbt_assert(mbox, "No rendez-vous point defined for irecv");
145
146   return simcall_BODY_comm_irecv(receiver, mbox, static_cast<unsigned char*>(dst_buff), dst_buff_size, match_fun,
147                                  copy_data_fun, data, rate);
148 }
149
150 /**
151  * @ingroup simix_comm_management
152  */
153 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
154                                    int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
155 {
156   xbt_assert(mbox, "No rendez-vous point defined for iprobe");
157
158   return simgrid::kernel::actor::simcall([mbox, type, match_fun, data] { return mbox->iprobe(type, match_fun, data); });
159 }
160
161 /**
162  * @ingroup simix_comm_management
163  */
164 unsigned int simcall_comm_waitany(smx_activity_t comms[], size_t count, double timeout)
165 {
166   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
167   std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
168     return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
169   });
170   return simcall_BODY_comm_waitany(rcomms.get(), count, timeout);
171 }
172
173 unsigned int simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count, double timeout)
174 {
175   return simcall_BODY_comm_waitany(comms, count, timeout);
176 }
177
178 /**
179  * @ingroup simix_comm_management
180  */
181 int simcall_comm_testany(smx_activity_t comms[], size_t count)
182 {
183   if (count == 0)
184     return -1;
185   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
186   std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
187     return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
188   });
189   return simcall_BODY_comm_testany(rcomms.get(), count);
190 }
191
192 int simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count)
193 {
194   if (count == 0)
195     return -1;
196   return simcall_BODY_comm_testany(comms, count);
197 }
198
199 /**
200  * @ingroup simix_comm_management
201  */
202 void simcall_comm_wait(const smx_activity_t& comm, double timeout)
203 {
204   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
205   simcall_BODY_comm_wait(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
206 }
207
208 /**
209  * @ingroup simix_comm_management
210  *
211  */
212 bool simcall_comm_test(const smx_activity_t& comm)
213 {
214   return simcall_BODY_comm_test(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()));
215 }
216
217 /**
218  * @ingroup simix_synchro_management
219  *
220  */
221 smx_mutex_t simcall_mutex_init()
222 {
223   if (simix_global == nullptr) {
224     fprintf(stderr, "You must initialize the SimGrid engine before using it\n"); // We can't use xbt_die since we may
225                                                                                  // get there before the initialization
226     xbt_abort();
227   }
228   return simgrid::kernel::actor::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
229 }
230
231 /**
232  * @ingroup simix_synchro_management
233  *
234  */
235 void simcall_mutex_lock(smx_mutex_t mutex)
236 {
237   simcall_BODY_mutex_lock(mutex);
238 }
239
240 /**
241  * @ingroup simix_synchro_management
242  *
243  */
244 int simcall_mutex_trylock(smx_mutex_t mutex)
245 {
246   return simcall_BODY_mutex_trylock(mutex);
247 }
248
249 /**
250  * @ingroup simix_synchro_management
251  *
252  */
253 void simcall_mutex_unlock(smx_mutex_t mutex)
254 {
255   simcall_BODY_mutex_unlock(mutex);
256 }
257
258 /**
259  * @ingroup simix_synchro_management
260  *
261  */
262 smx_cond_t simcall_cond_init()
263 {
264   return simgrid::kernel::actor::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
265 }
266
267 /**
268  * @ingroup simix_synchro_management
269  *
270  */
271 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
272 {
273   simcall_BODY_cond_wait(cond, mutex);
274 }
275
276 /**
277  * @ingroup simix_synchro_management
278  *
279  */
280 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
281 {
282   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
283   return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
284 }
285
286 /**
287  * @ingroup simix_synchro_management
288  *
289  */
290 void simcall_sem_acquire(smx_sem_t sem)
291 {
292   simcall_BODY_sem_acquire(sem);
293 }
294
295 /**
296  * @ingroup simix_synchro_management
297  *
298  */
299 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
300 {
301   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
302   return simcall_BODY_sem_acquire_timeout(sem, timeout);
303 }
304
305 e_smx_state_t simcall_io_wait(const smx_activity_t& io)
306 {
307   return (e_smx_state_t)simcall_BODY_io_wait(static_cast<simgrid::kernel::activity::IoImpl*>(io.get()));
308 }
309
310 void simcall_run_kernel(std::function<void()> const& code, simgrid::mc::SimcallInspector* t)
311 {
312   SIMIX_process_self()->simcall.inspector_ = t;
313   simcall_BODY_run_kernel(&code);
314 }
315
316 void simcall_run_blocking(std::function<void()> const& code, simgrid::mc::SimcallInspector* t = nullptr)
317 {
318   SIMIX_process_self()->simcall.inspector_ = t;
319   simcall_BODY_run_blocking(&code);
320 }
321
322 int simcall_mc_random(int min, int max) {
323   return simcall_BODY_mc_random(min, max);
324 }
325
326 /* ************************************************************************** */
327
328 /** @brief returns a printable string representing a simcall */
329 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
330   return simcall_names[kind];
331 }
332
333 namespace simgrid {
334 namespace simix {
335
336 void unblock(smx_actor_t actor)
337 {
338   xbt_assert(SIMIX_is_maestro());
339   actor->simcall_answer();
340 }
341 } // namespace simix
342 } // namespace simgrid