Logo AND Algorithmique Numérique Distribuée

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