Logo AND Algorithmique Numérique Distribuée

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