Logo AND Algorithmique Numérique Distribuée

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