Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix broken link to releases
[simgrid.git] / include / simgrid / msg.h
1 /* Copyright (c) 2004-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MSG_H
7 #define SIMGRID_MSG_H
8
9 #include <simgrid/actor.h>
10 #include <simgrid/barrier.h>
11 #include <simgrid/engine.h>
12 #include <simgrid/forward.h>
13 #include <simgrid/host.h>
14 #include <simgrid/instr.h>
15 #include <simgrid/mailbox.h>
16 #include <simgrid/mutex.h>
17 #include <simgrid/plugins/live_migration.h>
18 #include <simgrid/semaphore.h>
19 #include <simgrid/version.h>
20 #include <simgrid/vm.h>
21 #include <simgrid/zone.h>
22 #include <xbt.h>
23
24 #ifndef MIN
25 #define MIN(a, b) ((a) < (b) ? (a) : (b))
26 #endif
27 #ifndef MAX
28 #define MAX(a, b) ((a) > (b) ? (a) : (b))
29 #endif
30
31 #ifdef __cplusplus
32 #include <map>
33 #include <simgrid/simix.h>
34 namespace simgrid {
35 namespace msg {
36 class Comm;
37 class Task;
38 }
39 }
40 using sg_msg_Comm = simgrid::msg::Comm;
41 using sg_msg_Task = simgrid::msg::Task;
42 #else
43 typedef struct msg_Comm sg_msg_Comm;
44 typedef struct msg_Task sg_msg_Task;
45 #endif
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 /** @brief Return code of most MSG functions */
52 /* Keep these code as binary values: java bindings manipulate | of these values */
53 typedef enum {
54   MSG_OK               = 0, /**< @brief Everything is right. Keep on going this way ! */
55   MSG_TIMEOUT          = 1, /**< @brief nothing good happened before the timer you provided elapsed */
56   MSG_TRANSFER_FAILURE = 2, /**< @brief There has been a problem during you task
57     transfer. Either the network is down or the remote host has been
58     shutdown. */
59   MSG_HOST_FAILURE = 4,     /**< @brief System shutdown. The host on which you are
60     running has just been rebooted. Free your datastructures and
61     return now !*/
62   MSG_TASK_CANCELED = 8     /**< @brief Canceled task. This task has been canceled by somebody!*/
63 } msg_error_t;
64
65 /* *************************** Network Zones ******************************** */
66 typedef sg_netzone_t msg_netzone_t;
67
68 XBT_PUBLIC msg_netzone_t MSG_zone_get_root();
69 XBT_PUBLIC const char* MSG_zone_get_name(const_sg_netzone_t zone);
70 XBT_PUBLIC msg_netzone_t MSG_zone_get_by_name(const char* name);
71 XBT_PUBLIC void MSG_zone_get_sons(const_sg_netzone_t zone, xbt_dict_t whereto);
72 XBT_PUBLIC const char* MSG_zone_get_property_value(const_sg_netzone_t zone, const char* name);
73 XBT_PUBLIC void MSG_zone_set_property_value(msg_netzone_t zone, const char* name, const char* value);
74 XBT_PUBLIC void MSG_zone_get_hosts(const_sg_netzone_t zone, xbt_dynar_t whereto);
75
76 /* ******************************** Hosts ************************************ */
77 /** @brief Host datatype.
78  *
79  * A <em>location</em> (or <em>host</em>) is any possible place where a process may run. Thus it is represented as a
80  * <em>physical resource with computing capabilities</em>, some <em>mailboxes</em> to enable running process to
81  * communicate with remote ones, and some <em>private data</em> that can be only accessed by local process.
82  */
83 typedef sg_host_t msg_host_t;
84
85 /** @brief Finds a msg_host_t using its name. */
86 XBT_PUBLIC sg_host_t MSG_get_host_by_name(const char* name);
87 /** @brief Finds a msg_host_t using its name. */
88 XBT_PUBLIC sg_host_t MSG_host_by_name(const char* name);
89
90 /** @brief Returns the amount of host found in the platform */
91 XBT_PUBLIC size_t MSG_get_host_number();
92 /** @brief Returns a dynar with all existing hosts
93  *
94  * The host order in the returned array is generally different from the host creation/declaration order in the XML
95  * platform (we use a hash table internally).
96  */
97 XBT_ATTRIB_DEPRECATED_v330("Please use sg_host_list()") XBT_PUBLIC xbt_dynar_t MSG_hosts_as_dynar();
98
99 /** @brief Returns the name of this host */
100 XBT_PUBLIC const char* MSG_host_get_name(const_sg_host_t host);
101 /** @brief Returns the user data of this host */
102 XBT_PUBLIC void* MSG_host_get_data(const_sg_host_t host);
103 /** @brief Sets the user data of this host */
104 XBT_PUBLIC void MSG_host_set_data(sg_host_t host, void* data);
105
106 XBT_PUBLIC double MSG_host_get_speed(const_sg_host_t host);
107 XBT_PUBLIC double MSG_host_get_power_peak_at(const_sg_host_t host, int pstate_index);
108 XBT_PUBLIC int MSG_host_get_core_number(const_sg_host_t host);
109 XBT_PUBLIC int MSG_host_get_nb_pstates(const_sg_host_t host);
110 XBT_PUBLIC int MSG_host_get_pstate(const_sg_host_t host);
111 XBT_PUBLIC void MSG_host_set_pstate(sg_host_t host, int pstate);
112 /** @brief Start the host if it is off
113  *
114  * @beginrst
115  * See also :cpp:func:`MSG_host_is_on()` to test the current state of the host, and :ref:`plugin_host_energy`
116  * for more info on DVFS.
117  * @endrst
118  */
119 XBT_PUBLIC void MSG_host_on(sg_host_t h);
120 /** @brief Stop the host if it is on
121  *
122  * @beginrst
123  * See also :cpp:func:`MSG_host_is_on()` to test the current state of the host, and :ref:`plugin_host_energy`
124  * for more info on DVFS.
125  * @endrst
126  */
127 XBT_PUBLIC void MSG_host_off(sg_host_t h);
128 XBT_PUBLIC int MSG_host_is_on(const_sg_host_t h);
129 XBT_PUBLIC xbt_dict_t MSG_host_get_properties(const_sg_host_t host);
130 XBT_PUBLIC const char* MSG_host_get_property_value(const_sg_host_t host, const char* name);
131 XBT_PUBLIC void MSG_host_set_property_value(sg_host_t host, const char* name, const char* value);
132 XBT_PUBLIC void MSG_host_get_process_list(const_sg_host_t host, xbt_dynar_t whereto);
133
134 /** @brief Return the location on which the current process is executed */
135 XBT_PUBLIC sg_host_t MSG_host_self();
136 XBT_PUBLIC double MSG_host_get_load(const_sg_host_t host);
137
138 /* ******************************** VMs ************************************* */
139 typedef sg_vm_t msg_vm_t;
140
141 XBT_PUBLIC msg_vm_t MSG_vm_create_core(msg_host_t pm, const char* name);
142 XBT_PUBLIC msg_vm_t MSG_vm_create_multicore(msg_host_t pm, const char* name, int coreAmount);
143
144 XBT_PUBLIC int MSG_vm_is_created(const_sg_vm_t vm);
145 XBT_PUBLIC int MSG_vm_is_running(const_sg_vm_t vm);
146 XBT_PUBLIC int MSG_vm_is_suspended(const_sg_vm_t vm);
147
148 XBT_PUBLIC const char* MSG_vm_get_name(const_sg_vm_t vm);
149 XBT_PUBLIC void MSG_vm_set_ramsize(msg_vm_t vm, size_t size);
150 XBT_PUBLIC size_t MSG_vm_get_ramsize(const_sg_vm_t vm);
151 XBT_PUBLIC msg_host_t MSG_vm_get_pm(const_sg_vm_t vm);
152 XBT_PUBLIC void MSG_vm_set_bound(msg_vm_t vm, double bound);
153
154 XBT_PUBLIC void MSG_vm_start(msg_vm_t vm);
155 XBT_PUBLIC void MSG_vm_suspend(msg_vm_t vm);
156 XBT_PUBLIC void MSG_vm_resume(msg_vm_t vm);
157 XBT_PUBLIC void MSG_vm_shutdown(msg_vm_t vm);
158 XBT_PUBLIC void MSG_vm_destroy(msg_vm_t vm);
159
160 /* ******************************** Actor/process *************************** */
161 /** Processes are independent agents that can do stuff on their own.
162  *  They are in charge of executing your code interacting with the simulated world.
163  *  A process may be defined as a <em>code</em> with some <em>private data</em>.
164  *  Processes must be located on <em>hosts</em> (#msg_host_t), and they exchange data by sending tasks (#msg_task_t)
165  *  that are similar to envelops containing data.
166  *
167  *  @hideinitializer
168  */
169 typedef sg_actor_t msg_process_t;
170
171 XBT_PUBLIC int MSG_process_get_PID(const_sg_actor_t process);
172 XBT_PUBLIC int MSG_process_get_PPID(const_sg_actor_t process);
173 /** @brief Return a process from its PID (or NULL if not found).
174  *
175  * Note that the PID are unique in the whole simulation, not only on a given host.
176  */
177 XBT_PUBLIC sg_actor_t MSG_process_from_PID(int pid);
178 XBT_PUBLIC const char* MSG_process_get_name(const_sg_actor_t process);
179 XBT_PUBLIC sg_host_t MSG_process_get_host(const_sg_actor_t process);
180
181 /*property handlers*/
182 XBT_PUBLIC xbt_dict_t MSG_process_get_properties(const_sg_actor_t process);
183 XBT_PUBLIC const char* MSG_process_get_property_value(const_sg_actor_t process, const char* name);
184
185 XBT_PUBLIC void MSG_process_suspend(msg_process_t process);
186 XBT_PUBLIC void MSG_process_resume(msg_process_t process);
187 XBT_PUBLIC int MSG_process_is_suspended(const_sg_actor_t process);
188 XBT_PUBLIC void MSG_process_restart(msg_process_t process);
189 /** @brief Sets the "auto-restart" flag of the process.
190  *
191  * If the flag is set, the process will be automatically restarted when its host comes back up.
192  */
193 XBT_PUBLIC void MSG_process_auto_restart_set(msg_process_t process, int auto_restart);
194 /** @brief Indicates that this process should not prevent the simulation from ending
195  *
196  * SimGrid simulations run until all non-daemon processes are stopped.
197  */
198 XBT_PUBLIC void MSG_process_daemonize(msg_process_t process);
199 /** @brief Immediately changes the host on which this process runs */
200 XBT_PUBLIC void MSG_process_migrate(msg_process_t process, msg_host_t host);
201 /** @brief Wait for the completion of a process.
202  *
203  * @param process the process to wait for
204  * @param timeout wait until the process is over, or the timeout occurs
205  */
206 XBT_PUBLIC void MSG_process_join(const_sg_actor_t process, double timeout);
207 /** @brief Kills a process */
208 XBT_PUBLIC void MSG_process_kill(msg_process_t process);
209 /** @brief Kill all running process */
210 XBT_PUBLIC void MSG_process_killall();
211 /** @brief Specifies the time at which the process should be automatically killed */
212 XBT_PUBLIC void MSG_process_set_kill_time(msg_process_t process, double kill_time);
213 /** @brief Yield the current actor; let the other actors execute first */
214 XBT_PUBLIC void MSG_process_yield();
215 /*** @brief Sleep for the specified number of seconds */
216 XBT_PUBLIC msg_error_t MSG_process_sleep(double nb_sec);
217 XBT_PUBLIC msg_process_t MSG_process_self();
218 XBT_PUBLIC aid_t MSG_process_self_PID();
219 XBT_PUBLIC aid_t MSG_process_self_PPID();
220 XBT_PUBLIC const char* MSG_process_self_name();
221 XBT_PUBLIC void MSG_process_ref(const_sg_actor_t process);
222 XBT_PUBLIC void MSG_process_unref(const_sg_actor_t process);
223
224 /** @brief Object representing an ongoing communication between processes.
225  *
226  * @beginrst
227  * Such beast is usually obtained by using :cpp:func:`MSG_task_isend`, :cpp:func:`MSG_task_irecv` or friends.
228  * @endrst
229  */
230 typedef sg_msg_Comm* msg_comm_t;
231 typedef const sg_msg_Comm* const_msg_comm_t;
232
233 /** @brief Task datatype.
234  *
235  *  Since most scheduling algorithms rely on a concept of task  that can be either <em>computed</em> locally or
236  *  <em>transferred</em> on another processor, it seems to be the right level of abstraction for our purposes.
237  *  A <em>task</em> may then be defined by a <em>computing amount</em>, a <em>message size</em> and
238  *  some <em>private data</em>.
239  */
240 typedef sg_msg_Task* msg_task_t;
241 typedef const sg_msg_Task* const_msg_task_t;
242
243 /* ******************************** Task ************************************ */
244
245
246 /** @brief Default value for an uninitialized #msg_task_t. */
247 #define MSG_TASK_UNINITIALIZED NULL
248
249 /************************** Global ******************************************/
250 /** @brief set a configuration variable
251  *
252  * @beginrst
253  * Do --help on any simgrid binary to see the list of currently existing configuration variables, and see Section
254  * :ref:`options`.
255  * @endrst
256  *
257  * Example:
258  * MSG_config("host/model","ptask_L07");
259  */
260 XBT_PUBLIC void MSG_config(const char* key, const char* value);
261 /** @brief Initialize the MSG internal data.
262  *  @hideinitializer
263  *
264  *  It also checks that the link-time and compile-time versions of SimGrid do
265  *  match, so you should use this version instead of the #MSG_init_nocheck
266  *  function that does the same initializations, but without this check.
267  *
268  *  We allow linking against compiled versions that differ in the patch level.
269  */
270 #define MSG_init(argc, argv)                                                                                           \
271   do {                                                                                                                 \
272     sg_version_check(SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR, SIMGRID_VERSION_PATCH);                             \
273     MSG_init_nocheck((argc), (argv));                                                                                  \
274   } while (0)
275
276 XBT_PUBLIC void MSG_init_nocheck(int* argc, char** argv);
277 /** @brief Launch the MSG simulation */
278 XBT_PUBLIC msg_error_t MSG_main();
279 /** @brief Registers the main function of a process in a global table.
280  *
281  * This table is then used by #MSG_launch_application.
282  * @param name the reference name of the function.
283  * @param code the function (must have the same prototype than the main function of any C program: int ..(int argc, char
284  * *argv[]))
285  */
286 XBT_PUBLIC void MSG_function_register(const char* name, int (*code)(int, char**));
287 /** @brief Registers a code function as being the default value.
288  *
289  * This function will get used by MSG_launch_application() when there is no registered function of the requested name
290  * in.
291  *
292  * @param code the function (must have the same prototype than the main function of any C program: int ..(int argc, char
293  * *argv[]))
294  */
295 XBT_PUBLIC void MSG_function_register_default(int (*code)(int, char**));
296 /** @brief Creates a new platform, including hosts, links and the routing_table */
297 XBT_PUBLIC void MSG_create_environment(const char* file);
298 /** @brief Creates the application described in the provided file */
299 XBT_PUBLIC void MSG_launch_application(const char* file);
300
301 /** @brief A clock (in second). */
302 XBT_PUBLIC double MSG_get_clock();
303 /** @brief Returns the amount of messages sent since the simulation start */
304 XBT_PUBLIC unsigned long int MSG_get_sent_msg();
305
306 /************************** Process handling *********************************/
307 XBT_PUBLIC msg_process_t MSG_process_create(const char* name, int (*code)(int, char**), void* data, msg_host_t host);
308 XBT_PUBLIC msg_process_t MSG_process_create_with_arguments(const char* name, int (*code)(int, char**), void* data,
309                                                            msg_host_t host, int argc, char** argv);
310 XBT_PUBLIC msg_process_t MSG_process_create_with_environment(const char* name, int (*code)(int, char**), void* data,
311                                                              msg_host_t host, int argc, char** argv,
312                                                              xbt_dict_t properties);
313
314 XBT_PUBLIC msg_process_t MSG_process_attach(const char* name, void* data, msg_host_t host, xbt_dict_t properties);
315 XBT_PUBLIC void MSG_process_detach();
316
317 XBT_PUBLIC void MSG_process_set_data_cleanup(void_f_pvoid_t data_cleanup);
318 XBT_ATTRIB_DEPRECATED_v330("Please use sg_actor_list()") XBT_PUBLIC xbt_dynar_t MSG_processes_as_dynar();
319 XBT_ATTRIB_DEPRECATED_v330("Please use sg_actor_count()") XBT_PUBLIC int MSG_process_get_number();
320
321 XBT_PUBLIC void* MSG_process_get_data(const_sg_actor_t process);
322 XBT_PUBLIC msg_error_t MSG_process_set_data(msg_process_t process, void* data);
323
324 XBT_PUBLIC void MSG_process_on_exit(int_f_int_pvoid_t fun, void* data);
325
326 /************************** Task handling ************************************/
327 XBT_PUBLIC msg_task_t MSG_task_create(const char* name, double flops_amount, double bytes_amount, void* data);
328 XBT_PUBLIC msg_task_t MSG_parallel_task_create(const char* name, int host_nb, const msg_host_t* host_list,
329                                                double* flops_amount, double* bytes_amount, void* data);
330 XBT_PUBLIC void* MSG_task_get_data(const_msg_task_t task);
331 XBT_PUBLIC void MSG_task_set_data(msg_task_t task, void* data);
332 XBT_PUBLIC msg_process_t MSG_task_get_sender(const_msg_task_t task);
333 XBT_PUBLIC msg_host_t MSG_task_get_source(const_msg_task_t task);
334 XBT_PUBLIC const char* MSG_task_get_name(const_msg_task_t task);
335 XBT_PUBLIC void MSG_task_set_name(msg_task_t task, const char* name);
336 XBT_PUBLIC msg_error_t MSG_task_cancel(msg_task_t task);
337 XBT_PUBLIC msg_error_t MSG_task_destroy(msg_task_t task);
338
339 XBT_PUBLIC msg_error_t MSG_task_execute(msg_task_t task);
340 XBT_PUBLIC msg_error_t MSG_parallel_task_execute(msg_task_t task);
341 XBT_PUBLIC msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout);
342 XBT_PUBLIC void MSG_task_set_priority(msg_task_t task, double priority);
343 XBT_PUBLIC void MSG_task_set_bound(msg_task_t task, double bound);
344
345 XBT_PUBLIC void MSG_task_set_flops_amount(msg_task_t task, double flops_amount);
346 XBT_PUBLIC double MSG_task_get_flops_amount(const_msg_task_t task);
347 XBT_PUBLIC double MSG_task_get_remaining_work_ratio(const_msg_task_t task);
348 XBT_PUBLIC void MSG_task_set_bytes_amount(msg_task_t task, double bytes_amount);
349
350 XBT_PUBLIC double MSG_task_get_remaining_communication(const_msg_task_t task);
351 XBT_PUBLIC double MSG_task_get_bytes_amount(const_msg_task_t task);
352
353 XBT_PUBLIC msg_error_t MSG_task_receive_with_timeout(msg_task_t* task, const char* alias, double timeout);
354
355 XBT_PUBLIC msg_error_t MSG_task_receive(msg_task_t* task, const char* alias);
356 #define MSG_task_recv(t, a) MSG_task_receive((t), (a))
357
358 XBT_PUBLIC msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t* task, const char* alias, double timeout,
359                                                              double rate);
360 XBT_PUBLIC msg_error_t MSG_task_receive_bounded(msg_task_t* task, const char* alias, double rate);
361 #define MSG_task_recv_bounded(t, a, r) MSG_task_receive_bounded((t), (a), (r))
362
363 XBT_PUBLIC msg_comm_t MSG_task_isend(msg_task_t task, const char* alias);
364 XBT_PUBLIC msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char* alias, double maxrate);
365
366 XBT_PUBLIC void MSG_task_dsend(msg_task_t task, const char* alias, void_f_pvoid_t cleanup);
367 XBT_PUBLIC void MSG_task_dsend_bounded(msg_task_t task, const char* alias, void_f_pvoid_t cleanup, double maxrate);
368 XBT_PUBLIC msg_comm_t MSG_task_irecv(msg_task_t* task, const char* alias);
369 XBT_PUBLIC msg_comm_t MSG_task_irecv_bounded(msg_task_t* task, const char* alias, double rate);
370 XBT_PUBLIC int MSG_comm_test(msg_comm_t comm);
371 XBT_PUBLIC int MSG_comm_testany(const_xbt_dynar_t comms);
372 XBT_PUBLIC void MSG_comm_destroy(const_msg_comm_t comm);
373 XBT_PUBLIC msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout);
374 XBT_PUBLIC void MSG_comm_waitall(msg_comm_t* comm, int nb_elem, double timeout);
375 XBT_PUBLIC int MSG_comm_waitany(const_xbt_dynar_t comms);
376 XBT_PUBLIC msg_task_t MSG_comm_get_task(const_msg_comm_t comm);
377 XBT_PUBLIC msg_error_t MSG_comm_get_status(const_msg_comm_t comm);
378
379 /** @brief Check if there is a communication going on in a mailbox.
380  *
381  * @param alias the name of the mailbox to be considered
382  *
383  * @return Returns 1 if there is a communication, 0 otherwise
384  */
385 XBT_PUBLIC int MSG_task_listen(const char* alias);
386 XBT_PUBLIC msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char* alias, double timeout);
387 XBT_PUBLIC msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char* alias, double timeout,
388                                                           double maxrate);
389 XBT_PUBLIC msg_error_t MSG_task_send(msg_task_t task, const char* alias);
390 XBT_PUBLIC msg_error_t MSG_task_send_bounded(msg_task_t task, const char* alias, double rate);
391 XBT_PUBLIC int MSG_task_listen_from(const char* alias);
392 XBT_PUBLIC void MSG_task_set_category(msg_task_t task, const char* category);
393 XBT_PUBLIC const char* MSG_task_get_category(const_msg_task_t task);
394
395 /************************** Mailbox handling ************************************/
396
397 /* @brief set a mailbox in eager mode.
398  * All messages sent to this mailbox will be transferred to the receiver without waiting for the receive call.
399  * The receive call will still be necessary to use the received data.
400  * If there is a need to receive some messages asynchronously, and some not, two different mailboxes should be used.
401  *
402  * This call should be done before issuing any receive, and on the receiver's side only
403  */
404 XBT_PUBLIC void MSG_mailbox_set_async(const char* alias);
405
406 /** @brief Opaque type representing a semaphore */
407 typedef sg_sem_t msg_sem_t;
408 XBT_PUBLIC msg_sem_t MSG_sem_init(int initial_value);
409 XBT_PUBLIC void MSG_sem_acquire(msg_sem_t sem);
410 XBT_PUBLIC int MSG_sem_acquire_timeout(msg_sem_t sem, double timeout);
411 XBT_PUBLIC void MSG_sem_release(msg_sem_t sem);
412 XBT_PUBLIC int MSG_sem_get_capacity(const_sg_sem_t sem);
413 XBT_PUBLIC void MSG_sem_destroy(const_sg_sem_t sem);
414 XBT_PUBLIC int MSG_sem_would_block(const_sg_sem_t sem);
415
416 /** @brief Opaque type representing a barrier identifier */
417 typedef sg_bar_t msg_bar_t;
418 /** @brief Initializes a barrier, with count elements */
419 XBT_PUBLIC msg_bar_t MSG_barrier_init(unsigned int count);
420 /** @brief Destroys barrier */
421 XBT_PUBLIC void MSG_barrier_destroy(const_sg_bar_t bar);
422 /** @brief Performs a barrier already initialized */
423 XBT_PUBLIC int MSG_barrier_wait(msg_bar_t bar);
424
425 /* ****************************************************************************************** */
426 #ifdef __cplusplus
427 }
428 #endif
429
430 #endif