Logo AND Algorithmique Numérique Distribuée

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