Logo AND Algorithmique Numérique Distribuée

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