Logo AND Algorithmique Numérique Distribuée

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