Logo AND Algorithmique Numérique Distribuée

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