Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix for manually crafted clusters.
[simgrid.git] / src / msg / msg_gos.cpp
1 /* Copyright (c) 2004-2016. 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 #include <xbt/ex.hpp>
7
8 #include "src/simix/smx_private.h" /* MSG_task_listen looks inside the rdv directly. Not clean. */
9 #include "msg_private.h"
10 #include "mc/mc.h"
11 #include "xbt/log.h"
12 #include "xbt/sysdep.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
15                                 "Logging specific to MSG (gos)");
16
17 /** \ingroup msg_task_usage
18  * \brief Executes a task and waits for its termination.
19  *
20  * This function is used for describing the behavior of a process. It takes only one parameter.
21  * \param task a #msg_task_t to execute on the location on which the process is running.
22  * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
23  */
24 msg_error_t MSG_task_execute(msg_task_t task)
25 {
26   /* TODO: add this to other locations */
27   msg_host_t host = MSG_process_get_host(MSG_process_self());
28   MSG_host_add_task(host, task);
29
30   msg_error_t ret = MSG_parallel_task_execute(task);
31
32   MSG_host_del_task(host, task);
33
34   return ret;
35 }
36
37 /** \ingroup msg_task_usage
38  * \brief Executes a parallel task and waits for its termination.
39  *
40  * \param task a #msg_task_t to execute on the location on which the process is running.
41  *
42  * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
43  * or #MSG_HOST_FAILURE otherwise
44  */
45 msg_error_t MSG_parallel_task_execute(msg_task_t task)
46 {
47   simdata_task_t simdata = task->simdata;
48   simdata_process_t p_simdata = (simdata_process_t) SIMIX_process_self_get_data();
49   e_smx_state_t comp_state;
50   msg_error_t status = MSG_OK;
51
52   TRACE_msg_task_execute_start(task);
53
54   xbt_assert((!simdata->compute) && !task->simdata->isused,
55              "This task is executed somewhere else. Go fix your code!");
56
57   XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
58
59   if (simdata->flops_amount == 0 && !simdata->host_nb) {
60     TRACE_msg_task_execute_end(task);
61     return MSG_OK;
62   }
63
64   try {
65     simdata->setUsed();
66
67     if (simdata->host_nb > 0) {
68       simdata->compute = static_cast<simgrid::simix::Exec*>(
69           simcall_execution_parallel_start(task->name, simdata->host_nb,simdata->host_list,
70                                                        simdata->flops_parallel_amount, simdata->bytes_parallel_amount,
71                                                        1.0, -1.0));
72       XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
73     } else {
74       unsigned long affinity_mask =
75          (unsigned long)(uintptr_t) xbt_dict_get_or_null_ext(simdata->affinity_mask_db, (char *) p_simdata->m_host,
76                                                              sizeof(msg_host_t));
77       XBT_DEBUG("execute %s@%s with affinity(0x%04lx)",
78                 MSG_task_get_name(task), MSG_host_get_name(p_simdata->m_host), affinity_mask);
79
80           simdata->compute = static_cast<simgrid::simix::Exec*>(
81               simcall_execution_start(task->name, simdata->flops_amount, simdata->priority,
82                                                  simdata->bound, affinity_mask));
83     }
84     simcall_set_category(simdata->compute, task->category);
85     p_simdata->waiting_action = simdata->compute;
86     comp_state = simcall_execution_wait(simdata->compute);
87
88     p_simdata->waiting_action = nullptr;
89     simdata->setNotUsed();
90
91     XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
92   }
93   catch (xbt_ex& e) {
94     switch (e.category) {
95     case cancel_error:
96       status = MSG_TASK_CANCELED;
97       break;
98     case host_error:
99       status = MSG_HOST_FAILURE;
100       break;
101     default:
102       throw;
103     }
104   }
105
106   /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
107   simdata->flops_amount = 0.0;
108   simdata->comm = nullptr;
109   simdata->compute = nullptr;
110   TRACE_msg_task_execute_end(task);
111
112   MSG_RETURN(status);
113 }
114
115 /** \ingroup msg_task_usage
116  * \brief Sleep for the specified number of seconds
117  *
118  * Makes the current process sleep until \a time seconds have elapsed.
119  *
120  * \param nb_sec a number of second
121  */
122 msg_error_t MSG_process_sleep(double nb_sec)
123 {
124   msg_error_t status = MSG_OK;
125   /*msg_process_t proc = MSG_process_self();*/
126
127   TRACE_msg_process_sleep_in(MSG_process_self());
128
129   try {
130     simcall_process_sleep(nb_sec);
131   }
132   catch(xbt_ex& e) {
133     switch (e.category) {
134     case cancel_error:
135       XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, WTF here ?"); 
136       // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
137       // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
138       // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
139       // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
140       // and did not change anythings at the C level.
141       // See comment in the jmsg_process.c file, function JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos) 
142       status = MSG_TASK_CANCELED;
143       break;
144     default:
145       throw;
146     }
147   }
148
149   TRACE_msg_process_sleep_out(MSG_process_self());
150   MSG_RETURN(status);
151 }
152
153 /** \ingroup msg_task_usage
154  * \brief Receives a task from a mailbox.
155  *
156  * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
157  * for receiving tasks asynchronously.
158  *
159  * \param task a memory location for storing a #msg_task_t.
160  * \param alias name of the mailbox to receive the task from
161  *
162  * \return Returns
163  * #MSG_OK if the task was successfully received,
164  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
165  */
166 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
167 {
168   return MSG_task_receive_with_timeout(task, alias, -1);
169 }
170
171 /** \ingroup msg_task_usage
172  * \brief Receives a task from a mailbox at a given rate.
173  *
174  * \param task a memory location for storing a #msg_task_t.
175  * \param alias name of the mailbox to receive the task from
176  * \param rate limit the reception to rate bandwidth
177  *
178  * \return Returns
179  * #MSG_OK if the task was successfully received,
180  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
181  */
182 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
183 {
184   return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
185 }
186
187 /** \ingroup msg_task_usage
188  * \brief Receives a task from a mailbox with a given timeout.
189  *
190  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
191  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously.  You can provide a -1 timeout
192  * to obtain an infinite timeout.
193  *
194  * \param task a memory location for storing a #msg_task_t.
195  * \param alias name of the mailbox to receive the task from
196  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
197  *
198  * \return Returns
199  * #MSG_OK if the task was successfully received,
200  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
201  */
202 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
203 {
204   return MSG_task_receive_ext(task, alias, timeout, nullptr);
205 }
206
207 /** \ingroup msg_task_usage
208  * \brief Receives a task from a mailbox with a given timeout and at a given rate.
209  *
210  * \param task a memory location for storing a #msg_task_t.
211  * \param alias name of the mailbox to receive the task from
212  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
213  *  \param rate limit the reception to rate bandwidth
214  *
215  * \return Returns
216  * #MSG_OK if the task was successfully received,
217  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
218  */
219 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
220 {
221   return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
222 }
223
224 /** \ingroup msg_task_usage
225  * \brief Receives a task from a mailbox from a specific host with a given timeout.
226  *
227  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
228  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
229  * to obtain an infinite timeout.
230  *
231  * \param task a memory location for storing a #msg_task_t.
232  * \param alias name of the mailbox to receive the task from
233  * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
234  * \param host a #msg_host_t host from where the task was sent
235  *
236  * \return Returns
237  * #MSG_OK if the task was successfully received,
238 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
239  */
240 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
241 {
242   msg_error_t ret = MSG_OK;
243   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
244   try {
245     ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task, host, timeout);
246   }
247   catch(xbt_ex& e) {
248     switch (e.category) {
249     case cancel_error:          /* may be thrown by MSG_mailbox_get_by_alias */
250       ret = MSG_HOST_FAILURE;
251       break;
252     default:
253       throw;
254     }
255   }
256   return ret;
257 }
258
259 /** \ingroup msg_task_usage
260  * \brief Receives a task from a mailbox from a specific host with a given timeout  and at a given rate.
261  *
262  * \param task a memory location for storing a #msg_task_t.
263  * \param alias name of the mailbox to receive the task from
264  * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
265  * \param host a #msg_host_t host from where the task was sent
266  * \param rate limit the reception to rate bandwidth
267  *
268  * \return Returns
269  * #MSG_OK if the task was successfully received,
270 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
271  */
272 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
273                                          double rate)
274 {
275   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
276   return MSG_mailbox_get_task_ext_bounded(MSG_mailbox_get_by_alias(alias), task, host, timeout, rate);
277 }
278
279 /* Internal function used to factorize code between MSG_task_isend_with_matching() and MSG_task_dsend(). */
280 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
281                                                      int (*match_fun)(void*,void*, smx_synchro_t),
282                                                      void *match_data, void_f_pvoid_t cleanup, int detached)
283 {
284   simdata_task_t t_simdata = nullptr;
285   msg_process_t process = MSG_process_self();
286   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
287   int call_end = TRACE_msg_task_put_start(task);
288
289   /* Prepare the task to send */
290   t_simdata = task->simdata;
291   t_simdata->sender = process;
292   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data())->m_host;
293   t_simdata->setUsed();
294   t_simdata->comm = nullptr;
295   msg_global->sent_msg++;
296
297   /* Send it by calling SIMIX network layer */
298   smx_synchro_t act = simcall_comm_isend(SIMIX_process_self(), mailbox, t_simdata->bytes_amount, t_simdata->rate,
299                                          task, sizeof(void *), match_fun, cleanup, nullptr, match_data,detached);
300   t_simdata->comm = static_cast<simgrid::simix::Comm*>(act); /* FIXME: is the field t_simdata->comm still useful? */
301
302   msg_comm_t comm;
303   if (detached) {
304     comm = nullptr;
305   } else {
306     comm = xbt_new0(s_msg_comm_t, 1);
307     comm->task_sent = task;
308     comm->task_received = nullptr;
309     comm->status = MSG_OK;
310     comm->s_comm = act;
311   }
312
313   if (TRACE_is_enabled())
314     simcall_set_category(act, task->category);
315   if (call_end)
316     TRACE_msg_task_put_end();
317
318   return comm;
319 }
320
321 /** \ingroup msg_task_usage
322  * \brief Sends a task on a mailbox.
323  *
324  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
325  *
326  * \param task a #msg_task_t to send on another location.
327  * \param alias name of the mailbox to sent the task to
328  * \return the msg_comm_t communication created
329  */
330 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
331 {
332   return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
333 }
334
335 /** \ingroup msg_task_usage
336  * \brief Sends a task on a mailbox with a maximum rate
337  *
338  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
339  * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
340  *
341  * \param task a #msg_task_t to send on another location.
342  * \param alias name of the mailbox to sent the task to
343  * \param maxrate the maximum communication rate for sending this task .
344  * \return the msg_comm_t communication created
345  */
346 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
347 {
348   task->simdata->rate = maxrate;
349   return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
350 }
351
352 /** \ingroup msg_task_usage
353  * \brief Sends a task on a mailbox, with support for matching requests
354  *
355  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
356  *
357  * \param task a #msg_task_t to send on another location.
358  * \param alias name of the mailbox to sent the task to
359  * \param match_fun boolean function which parameters are:
360  *        - match_data_provided_here
361  *        - match_data_provided_by_other_side_if_any
362  *        - the_smx_synchro_describing_the_other_side
363  * \param match_data user provided data passed to match_fun
364  * \return the msg_comm_t communication created
365  */
366 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
367                                         int (*match_fun)(void*, void*, smx_synchro_t), void *match_data)
368 {
369   return MSG_task_isend_internal(task, alias, match_fun, match_data, nullptr, 0);
370 }
371
372 /** \ingroup msg_task_usage
373  * \brief Sends a task on a mailbox.
374  *
375  * This is a non blocking detached send function.
376  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
377  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
378  * usual. More details on this can be obtained on
379  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
380  * in the SimGrid-user mailing list archive.
381  *
382  * \param task a #msg_task_t to send on another location.
383  * \param alias name of the mailbox to sent the task to
384  * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
385  * (if nullptr, no function will be called)
386  */
387 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
388 {
389   MSG_task_isend_internal(task, alias, nullptr, nullptr, cleanup, 1);
390 }
391
392 /** \ingroup msg_task_usage
393  * \brief Sends a task on a mailbox with a maximal rate.
394  *
395  * This is a non blocking detached send function.
396  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
397  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
398  * usual. More details on this can be obtained on
399  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
400  * in the SimGrid-user mailing list archive.
401  *
402  * \param task a #msg_task_t to send on another location.
403  * \param alias name of the mailbox to sent the task to
404  * \param cleanup a function to destroy the task if the
405  * communication fails, e.g. MSG_task_destroy
406  * (if nullptr, no function will be called)
407  * \param maxrate the maximum communication rate for sending this task
408  *
409  */
410 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
411 {
412   task->simdata->rate = maxrate;
413   MSG_task_dsend(task, alias, cleanup);
414 }
415
416 /** \ingroup msg_task_usage
417  * \brief Starts listening for receiving a task from an asynchronous communication.
418  *
419  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
420  *
421  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
422  * \param name of the mailbox to receive the task on
423  * \return the msg_comm_t communication created
424  */
425 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
426 {
427   return MSG_task_irecv_bounded(task, name, -1.0);
428 }
429
430 /** \ingroup msg_task_usage
431  * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
432  *
433  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
434  * \param name of the mailbox to receive the task on
435  * \param rate limit the bandwidth to the given rate
436  * \return the msg_comm_t communication created
437  */
438 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
439 {
440   smx_mailbox_t rdv = MSG_mailbox_get_by_alias(name);
441
442   /* FIXME: these functions are not traceable */
443   /* Sanity check */
444   xbt_assert(task, "Null pointer for the task storage");
445
446   if (*task)
447     XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
448
449   /* Try to receive it by calling SIMIX network layer */
450   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
451   comm->task_sent = nullptr;
452   comm->task_received = task;
453   comm->status = MSG_OK;
454   comm->s_comm = simcall_comm_irecv(MSG_process_self(), rdv, task, nullptr, nullptr, nullptr, nullptr, rate);
455
456   return comm;
457 }
458
459 /** \ingroup msg_task_usage
460  * \brief Checks whether a communication is done, and if yes, finalizes it.
461  * \param comm the communication to test
462  * \return TRUE if the communication is finished
463  * (but it may have failed, use MSG_comm_get_status() to know its status)
464  * or FALSE if the communication is not finished yet
465  * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
466  */
467 int MSG_comm_test(msg_comm_t comm)
468 {
469   int finished = 0;
470
471   try {
472     finished = simcall_comm_test(comm->s_comm);
473     if (finished && comm->task_received != nullptr) {
474       /* I am the receiver */
475       (*comm->task_received)->simdata->setNotUsed();
476     }
477   }
478   catch (xbt_ex& e) {
479     switch (e.category) {
480       case network_error:
481         comm->status = MSG_TRANSFER_FAILURE;
482         finished = 1;
483         break;
484       case timeout_error:
485         comm->status = MSG_TIMEOUT;
486         finished = 1;
487         break;
488       default:
489         throw;
490     }
491   }
492
493   return finished;
494 }
495
496 /** \ingroup msg_task_usage
497  * \brief This function checks if a communication is finished.
498  * \param comms a vector of communications
499  * \return the position of the finished communication if any
500  * (but it may have failed, use MSG_comm_get_status() to know its status),
501  * or -1 if none is finished
502  */
503 int MSG_comm_testany(xbt_dynar_t comms)
504 {
505   int finished_index = -1;
506
507   /* create the equivalent dynar with SIMIX objects */
508   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), nullptr);
509   msg_comm_t comm;
510   unsigned int cursor;
511   xbt_dynar_foreach(comms, cursor, comm) {
512     xbt_dynar_push(s_comms, &comm->s_comm);
513   }
514
515   msg_error_t status = MSG_OK;
516   try {
517     finished_index = simcall_comm_testany(s_comms);
518   }
519   catch (xbt_ex& e) {
520     switch (e.category) {
521       case network_error:
522         finished_index = e.value;
523         status = MSG_TRANSFER_FAILURE;
524         break;
525       case timeout_error:
526         finished_index = e.value;
527         status = MSG_TIMEOUT;
528         break;
529       default:
530         throw;
531     }
532   }
533   xbt_dynar_free(&s_comms);
534
535   if (finished_index != -1) {
536     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
537     /* the communication is finished */
538     comm->status = status;
539
540     if (status == MSG_OK && comm->task_received != nullptr) {
541       /* I am the receiver */
542       (*comm->task_received)->simdata->setNotUsed();
543     }
544   }
545
546   return finished_index;
547 }
548
549 /** \ingroup msg_task_usage
550  * \brief Destroys a communication.
551  * \param comm the communication to destroy.
552  */
553 void MSG_comm_destroy(msg_comm_t comm)
554 {
555   xbt_free(comm);
556 }
557
558 /** \ingroup msg_task_usage
559  * \brief Wait for the completion of a communication.
560  *
561  * It takes two parameters.
562  * \param comm the communication to wait.
563  * \param timeout Wait until the communication terminates or the timeout occurs.
564  *                You can provide a -1 timeout to obtain an infinite timeout.
565  * \return msg_error_t
566  */
567 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
568 {
569   try {
570     simcall_comm_wait(comm->s_comm, timeout);
571
572     if (comm->task_received != nullptr) {
573       /* I am the receiver */
574       (*comm->task_received)->simdata->setNotUsed();
575     }
576
577     /* FIXME: these functions are not traceable */
578   }
579   catch (xbt_ex& e) {
580     switch (e.category) {
581     case network_error:
582       comm->status = MSG_TRANSFER_FAILURE;
583       break;
584     case timeout_error:
585       comm->status = MSG_TIMEOUT;
586       break;
587     default:
588       throw;
589     }
590   }
591
592   return comm->status;
593 }
594
595 /** \ingroup msg_task_usage
596 * \brief This function is called by a sender and permit to wait for each communication
597 *
598 * \param comm a vector of communication
599 * \param nb_elem is the size of the comm vector
600 * \param timeout for each call of MSG_comm_wait
601 */
602 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
603 {
604   int i = 0;
605   for (i = 0; i < nb_elem; i++) {
606     MSG_comm_wait(comm[i], timeout);
607   }
608 }
609
610 /** \ingroup msg_task_usage
611  * \brief This function waits for the first communication finished in a list.
612  * \param comms a vector of communications
613  * \return the position of the first finished communication
614  * (but it may have failed, use MSG_comm_get_status() to know its status)
615  */
616 int MSG_comm_waitany(xbt_dynar_t comms)
617 {
618   int finished_index = -1;
619
620   /* create the equivalent dynar with SIMIX objects */
621   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), nullptr);
622   msg_comm_t comm;
623   unsigned int cursor;
624   xbt_dynar_foreach(comms, cursor, comm) {
625     xbt_dynar_push(s_comms, &comm->s_comm);
626   }
627
628   msg_error_t status = MSG_OK;
629   try {
630     finished_index = simcall_comm_waitany(s_comms);
631   }
632   catch(xbt_ex& e) {
633     switch (e.category) {
634       case network_error:
635         finished_index = e.value;
636         status = MSG_TRANSFER_FAILURE;
637         break;
638       case timeout_error:
639         finished_index = e.value;
640         status = MSG_TIMEOUT;
641         break;
642       default:
643         throw;
644     }
645   }
646
647   xbt_assert(finished_index != -1, "WaitAny returned -1");
648   xbt_dynar_free(&s_comms);
649
650   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
651   /* the communication is finished */
652   comm->status = status;
653
654   if (comm->task_received != nullptr) {
655     /* I am the receiver */
656     (*comm->task_received)->simdata->setNotUsed();
657   }
658
659   return finished_index;
660 }
661
662 /**
663  * \ingroup msg_task_usage
664  * \brief Returns the error (if any) that occurred during a finished communication.
665  * \param comm a finished communication
666  * \return the status of the communication, or #MSG_OK if no error occurred
667  * during the communication
668  */
669 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
670
671   return comm->status;
672 }
673
674 /** \ingroup msg_task_usage
675  * \brief Get a task (#msg_task_t) from a communication
676  *
677  * \param comm the communication where to get the task
678  * \return the task from the communication
679  */
680 msg_task_t MSG_comm_get_task(msg_comm_t comm)
681 {
682   xbt_assert(comm, "Invalid parameter");
683
684   return comm->task_received ? *comm->task_received : comm->task_sent;
685 }
686
687 /**
688  * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
689  * \param comm the comm
690  * \param buff the data copied
691  * \param buff_size size of the buffer
692  */
693 void MSG_comm_copy_data_from_SIMIX(smx_synchro_t synchro, void* buff, size_t buff_size)
694 {
695   simgrid::simix::Comm *comm = static_cast<simgrid::simix::Comm*>(synchro);
696
697   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
698
699   // notify the user callback if any
700   if (msg_global->task_copy_callback) {
701     msg_task_t task = (msg_task_t) buff;
702     msg_global->task_copy_callback(task, comm->src_proc, comm->dst_proc);
703   }
704 }
705
706 /** \ingroup msg_task_usage
707  * \brief Sends a task to a mailbox
708  *
709  * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
710  * side if #MSG_task_receive is used).
711  * See #MSG_task_isend for sending tasks asynchronously.
712  *
713  * \param task the task to be sent
714  * \param alias the mailbox name to where the task is sent
715  *
716  * \return Returns #MSG_OK if the task was successfully sent,
717  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
718  */
719 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
720 {
721   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
722   return MSG_task_send_with_timeout(task, alias, -1);
723 }
724
725 /** \ingroup msg_task_usage
726  * \brief Sends a task to a mailbox with a maximum rate
727  *
728  * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
729  * the application to limit the bandwidth utilization of network links when sending the task.
730  *
731  * \param task the task to be sent
732  * \param alias the mailbox name to where the task is sent
733  * \param maxrate the maximum communication rate for sending this task
734  *
735  * \return Returns #MSG_OK if the task was successfully sent,
736  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
737  */
738 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
739 {
740   task->simdata->rate = maxrate;
741   return MSG_task_send(task, alias);
742 }
743
744 /** \ingroup msg_task_usage
745  * \brief Sends a task to a mailbox with a timeout
746  *
747  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
748  *
749  * \param task the task to be sent
750  * \param alias the mailbox name to where the task is sent
751  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
752  *
753  * \return Returns #MSG_OK if the task was successfully sent,
754  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
755  */
756 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
757 {
758   msg_error_t ret = MSG_OK;
759   simdata_task_t t_simdata = nullptr;
760   msg_process_t process = MSG_process_self();
761   simdata_process_t p_simdata = (simdata_process_t) SIMIX_process_self_get_data();
762   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
763
764   int call_end = TRACE_msg_task_put_start(task);    //must be after CHECK_HOST()
765
766   /* Prepare the task to send */
767   t_simdata = task->simdata;
768   t_simdata->sender = process;
769   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data())->m_host;
770
771   t_simdata->setUsed();
772
773   t_simdata->comm = nullptr;
774   msg_global->sent_msg++;
775
776   p_simdata->waiting_task = task;
777
778   /* Try to send it by calling SIMIX network layer */
779   try {
780     smx_synchro_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call  */
781     comm = simcall_comm_isend(SIMIX_process_self(), mailbox,t_simdata->bytes_amount,
782                               t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
783     if (TRACE_is_enabled())
784       simcall_set_category(comm, task->category);
785      t_simdata->comm = static_cast<simgrid::simix::Comm*>(comm);
786      simcall_comm_wait(comm, timeout);
787   }
788   catch (xbt_ex& e) {
789     switch (e.category) {
790     case cancel_error:
791       ret = MSG_HOST_FAILURE;
792       break;
793     case network_error:
794       ret = MSG_TRANSFER_FAILURE;
795       break;
796     case timeout_error:
797       ret = MSG_TIMEOUT;
798       break;
799     default:
800       throw;
801     }
802
803     /* If the send failed, it is not used anymore */
804     t_simdata->setNotUsed();
805   }
806
807   p_simdata->waiting_task = nullptr;
808   if (call_end)
809     TRACE_msg_task_put_end();
810   MSG_RETURN(ret);
811 }
812
813 /** \ingroup msg_task_usage
814  * \brief Sends a task to a mailbox with a timeout and with a maximum rate
815  *
816  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
817  *
818  * \param task the task to be sent
819  * \param alias the mailbox name to where the task is sent
820  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
821  * \param maxrate the maximum communication rate for sending this task
822  *
823  * \return Returns #MSG_OK if the task was successfully sent,
824  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
825  */
826 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
827 {
828   task->simdata->rate = maxrate;
829   return MSG_task_send_with_timeout(task, alias, timeout);
830 }
831
832 /** \ingroup msg_task_usage
833  * \brief Check if there is a communication going on in a mailbox.
834  *
835  * \param alias the name of the mailbox to be considered
836  *
837  * \return Returns 1 if there is a communication, 0 otherwise
838  */
839 int MSG_task_listen(const char *alias)
840 {
841   smx_mailbox_t mbox = MSG_mailbox_get_by_alias(alias);
842   return !MSG_mailbox_is_empty(mbox) ||
843     (mbox->permanent_receiver && !mbox->done_comm_queue.empty());
844 }
845
846 /** \ingroup msg_task_usage
847  * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
848  *
849  * \param alias the name of the mailbox to be considered
850  *
851  * \return Returns the PID of sender process,
852  * -1 if there is no communication in the mailbox.
853  */
854 int MSG_task_listen_from(const char *alias)
855 {
856   msg_task_t task;
857
858   if (nullptr == (task = MSG_mailbox_front(MSG_mailbox_get_by_alias(alias))))
859     return -1;
860
861   return MSG_process_get_PID(task->simdata->sender);
862 }
863
864 /** \ingroup msg_task_usage
865  * \brief Sets the tracing category of a task.
866  *
867  * This function should be called after the creation of a MSG task, to define the category of that task. The
868  * first parameter task must contain a task that was  created with the function #MSG_task_create. The second
869  * parameter category must contain a category that was previously declared with the function #TRACE_category
870  * (or with #TRACE_category_with_color).
871  *
872  * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
873  *
874  * \param task the task that is going to be categorized
875  * \param category the name of the category to be associated to the task
876  *
877  * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
878  */
879 void MSG_task_set_category (msg_task_t task, const char *category)
880 {
881   TRACE_msg_set_task_category (task, category);
882 }
883
884 /** \ingroup msg_task_usage
885  *
886  * \brief Gets the current tracing category of a task.
887  *
888  * \param task the task to be considered
889  *
890  * \see MSG_task_set_category
891  *
892  * \return Returns the name of the tracing category of the given task, nullptr otherwise
893  */
894 const char *MSG_task_get_category (msg_task_t task)
895 {
896   return task->category;
897 }
898
899 /**
900  * \brief Returns the value of a given AS or router property
901  *
902  * \param asr the name of a router or AS
903  * \param name a property name
904  * \return value of a property (or nullptr if property not set)
905  */
906 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
907 {
908   return (char*) xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
909 }
910
911 /**
912  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
913  * a the AS or router
914  *
915  * \param asr the name of a router or AS
916  * \return a dict containing the properties
917  */
918 xbt_dict_t MSG_as_router_get_properties(const char* asr)
919 {
920   return (simcall_asr_get_properties(asr));
921 }
922
923 /**
924  * \brief Change the value of a given AS or router
925  *
926  * \param asr the name of a router or AS
927  * \param name a property name
928  * \param value what to change the property to
929  * \param free_ctn the freeing function to use to kill the value on need
930  */
931 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
932   xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
933 }