Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
source is sender's host. One field is enough
[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   xbt_assert((not simdata->compute) && not task->simdata->is_used,
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->set_used();
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 ?: "", host));
77       });
78       /* checking for infinite values */
79       xbt_assert(std::isfinite(simdata->flops_amount), "flops_amount is not finite!");
80
81       simdata->compute->start(simdata->flops_amount, simdata->priority, simdata->bound);
82     }
83
84     comp_state = simcall_execution_wait(simdata->compute);
85
86     simdata->set_not_used();
87
88     XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
89   } catch (simgrid::HostFailureException& e) {
90     status = MSG_HOST_FAILURE;
91   } catch (simgrid::TimeoutError& e) {
92     status = MSG_TIMEOUT;
93   } catch (simgrid::CancelException& e) {
94     status = MSG_TASK_CANCELED;
95   }
96
97   /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
98   simdata->flops_amount = 0.0;
99   simdata->comm = nullptr;
100   simdata->compute = nullptr;
101
102   if (TRACE_actor_is_enabled())
103     simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->pop_event();
104
105   return status;
106 }
107
108 /**
109  * @brief Receives a task from a mailbox.
110  *
111  * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
112  * for receiving tasks asynchronously.
113  *
114  * @param task a memory location for storing a #msg_task_t.
115  * @param alias name of the mailbox to receive the task from
116  *
117  * @return Returns
118  * #MSG_OK if the task was successfully received,
119  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
120  */
121 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
122 {
123   return MSG_task_receive_with_timeout(task, alias, -1);
124 }
125
126 /**
127  * @brief Receives a task from a mailbox at a given rate.
128  *
129  * @param task a memory location for storing a #msg_task_t.
130  * @param alias name of the mailbox to receive the task from
131  * @param rate limit the reception to rate bandwidth (byte/sec)
132  *
133  * The rate parameter can be used to receive a task with a limited
134  * bandwidth (smaller than the physical available value). Use
135  * MSG_task_receive() if you don't limit the rate (or pass -1 as a
136  * rate value do disable this feature).
137  *
138  * @return Returns
139  * #MSG_OK if the task was successfully received,
140  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
141  */
142 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
143 {
144   return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
145 }
146
147 /**
148  * @brief Receives a task from a mailbox with a given timeout.
149  *
150  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
151  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously.  You can provide a -1 timeout
152  * to obtain an infinite timeout.
153  *
154  * @param task a memory location for storing a #msg_task_t.
155  * @param alias name of the mailbox to receive the task from
156  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
157  *
158  * @return Returns
159  * #MSG_OK if the task was successfully received,
160  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
161  */
162 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
163 {
164   return MSG_task_receive_ext(task, alias, timeout, nullptr);
165 }
166
167 /**
168  * @brief Receives a task from a mailbox with a given timeout and at a given rate.
169  *
170  * @param task a memory location for storing a #msg_task_t.
171  * @param alias name of the mailbox to receive the task from
172  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
173  * @param rate limit the reception to rate bandwidth (byte/sec)
174  *
175  * The rate parameter can be used to send a task with a limited
176  * bandwidth (smaller than the physical available value). Use
177  * MSG_task_receive() if you don't limit the rate (or pass -1 as a
178  * rate value do disable this feature).
179  *
180  * @return Returns
181  * #MSG_OK if the task was successfully received,
182  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
183  */
184 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
185 {
186   return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
187 }
188
189 /**
190  * @brief Receives a task from a mailbox from a specific host with a given timeout.
191  *
192  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
193  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
194  * to obtain an infinite timeout.
195  *
196  * @param task a memory location for storing a #msg_task_t.
197  * @param alias name of the mailbox to receive the task from
198  * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
199  * @param host a #msg_host_t host from where the task was sent
200  *
201  * @return Returns
202  * #MSG_OK if the task was successfully received,
203  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
204  */
205 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
206 {
207   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
208   return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
209 }
210
211 /**
212  * @brief Receives a task from a mailbox from a specific host with a given timeout  and at a given rate.
213  *
214  * @param task a memory location for storing a #msg_task_t.
215  * @param alias name of the mailbox to receive the task from
216  * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
217  * @param host a #msg_host_t host from where the task was sent
218  * @param rate limit the reception to rate bandwidth (byte/sec)
219  *
220  * The rate parameter can be used to receive a task with a limited
221  * bandwidth (smaller than the physical available value). Use
222  * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
223  * rate value do disable this feature).
224  *
225  * @return Returns
226  * #MSG_OK if the task was successfully received,
227  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
228  */
229 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
230                                          double rate)
231 {
232   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
233   msg_error_t ret = MSG_OK;
234   /* We no longer support getting a task from a specific host */
235   if (host)
236     THROW_UNIMPLEMENTED;
237
238   /* Sanity check */
239   xbt_assert(task, "Null pointer for the task storage");
240
241   if (*task)
242     XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
243
244   /* Try to receive it by calling SIMIX network layer */
245   try {
246     void* payload;
247     simgrid::s4u::Mailbox::by_name(alias)
248         ->get_init()
249         ->set_dst_data(&payload, sizeof(msg_task_t*))
250         ->set_rate(rate)
251         ->wait_for(timeout);
252     *task = static_cast<msg_task_t>(payload);
253     XBT_DEBUG("Got task %s from %s", (*task)->name, alias);
254     (*task)->simdata->set_not_used();
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_TASK_CANCELED;
261   } catch (xbt_ex& e) {
262     if (e.category == network_error)
263       ret = MSG_TRANSFER_FAILURE;
264     else
265       throw;
266   }
267
268   if (TRACE_actor_is_enabled() && ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
269     container_t process_container = simgrid::instr::Container::by_name(instr_pid(MSG_process_self()));
270
271     std::string key = std::string("p") + std::to_string((*task)->counter);
272     simgrid::instr::Container::get_root()->get_link("ACTOR_TASK_LINK")->end_event(process_container, "SR", key);
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, void_f_pvoid_t cleanup,
279                                                  bool 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->set_used();
290   t_simdata->comm = nullptr;
291   msg_global->sent_msg++;
292
293   simgrid::s4u::CommPtr comm = mailbox->put_init(task, t_simdata->bytes_amount)->set_rate(t_simdata->rate);
294   t_simdata->comm            = comm;
295   if (detached)
296     comm->detach(cleanup);
297   else
298     comm->start();
299
300   msg_comm_t msg_comm = nullptr;
301   if (not detached) {
302     msg_comm = new simgrid::msg::Comm(task, nullptr, comm);
303   }
304
305   if (TRACE_is_enabled() && task->category != nullptr)
306     simgrid::simix::simcall([comm, task] { comm->get_impl()->set_category(task->category); });
307
308   return msg_comm;
309 }
310
311 /**
312  * @brief Sends a task on a mailbox.
313  *
314  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
315  *
316  * @param task a #msg_task_t to send on another location.
317  * @param alias name of the mailbox to sent the task to
318  * @return the msg_comm_t communication created
319  */
320 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
321 {
322   return MSG_task_isend_internal(task, alias, nullptr, false);
323 }
324
325 /**
326  * @brief Sends a task on a mailbox with a maximum rate
327  *
328  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
329  * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
330  *
331  * @param task a #msg_task_t to send on another location.
332  * @param alias name of the mailbox to sent the task to
333  * @param maxrate the maximum communication rate for sending this task (byte/sec).
334  * @return the msg_comm_t communication created
335  */
336 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
337 {
338   task->simdata->rate = maxrate;
339   return MSG_task_isend_internal(task, alias, nullptr, false);
340 }
341
342 /**
343  * @brief Sends a task on a mailbox.
344  *
345  * This is a non blocking detached send function.
346  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
347  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
348  * usual. More details on this can be obtained on
349  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
350  * in the SimGrid-user mailing list archive.
351  *
352  * @param task a #msg_task_t to send on another location.
353  * @param alias name of the mailbox to sent the task to
354  * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
355  * (if nullptr, no function will be called)
356  */
357 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
358 {
359   msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, true);
360   xbt_assert(comm == nullptr);
361 }
362
363 /**
364  * @brief Sends a task on a mailbox with a maximal rate.
365  *
366  * This is a non blocking detached send function.
367  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
368  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
369  * usual. More details on this can be obtained on
370  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
371  * in the SimGrid-user mailing list archive.
372  *
373  * The rate parameter can be used to send a task with a limited
374  * bandwidth (smaller than the physical available value). Use
375  * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
376  * value do disable this feature).
377  *
378  * @param task a #msg_task_t to send on another location.
379  * @param alias name of the mailbox to sent the task to
380  * @param cleanup a function to destroy the task if the
381  * communication fails, e.g. MSG_task_destroy
382  * (if nullptr, no function will be called)
383  * @param maxrate the maximum communication rate for sending this task (byte/sec)
384  *
385  */
386 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
387 {
388   task->simdata->rate = maxrate;
389   MSG_task_dsend(task, alias, cleanup);
390 }
391
392 /**
393  * @brief Starts listening for receiving a task from an asynchronous communication.
394  *
395  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
396  *
397  * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
398  * @param name of the mailbox to receive the task on
399  * @return the msg_comm_t communication created
400  */
401 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
402 {
403   return MSG_task_irecv_bounded(task, name, -1.0);
404 }
405
406 /**
407  * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
408  *
409  * The rate parameter can be used to receive a task with a limited
410  * bandwidth (smaller than the physical available value). Use
411  * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
412  * value do disable this feature).
413  *
414  * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
415  * @param name of the mailbox to receive the task on
416  * @param rate limit the bandwidth to the given rate (byte/sec)
417  * @return the msg_comm_t communication created
418  */
419 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
420 {
421   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
422
423   /* FIXME: these functions are not traceable */
424   /* Sanity check */
425   xbt_assert(task, "Null pointer for the task storage");
426
427   if (*task)
428     XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
429
430   /* Try to receive it by calling SIMIX network layer */
431   msg_comm_t comm = new simgrid::msg::Comm(
432       nullptr, task, mbox->get_init()->set_dst_data((void**)task, sizeof(msg_task_t*))->set_rate(rate)->start());
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 = comm->s_comm->test();
451     if (finished && comm->task_received != nullptr) {
452       /* I am the receiver */
453       (*comm->task_received)->simdata->set_not_used();
454     }
455   } catch (simgrid::TimeoutError& e) {
456     comm->status = MSG_TIMEOUT;
457     finished     = true;
458   } catch (simgrid::CancelException& e) {
459     comm->status = MSG_TASK_CANCELED;
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_impl().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   } catch (simgrid::CancelException& e) {
501     finished_index = e.value;
502     status         = MSG_TASK_CANCELED;
503   }
504   catch (xbt_ex& e) {
505     if (e.category != network_error)
506       throw;
507     finished_index = e.value;
508     status         = MSG_TRANSFER_FAILURE;
509   }
510
511   if (finished_index != -1) {
512     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
513     /* the communication is finished */
514     comm->status = status;
515
516     if (status == MSG_OK && comm->task_received != nullptr) {
517       /* I am the receiver */
518       (*comm->task_received)->simdata->set_not_used();
519     }
520   }
521
522   return finished_index;
523 }
524
525 /** @brief Destroys the provided communication. */
526 void MSG_comm_destroy(msg_comm_t comm)
527 {
528   delete comm;
529 }
530
531 /** @brief Wait for the completion of a communication.
532  *
533  * It takes two parameters.
534  * @param comm the communication to wait.
535  * @param timeout Wait until the communication terminates or the timeout occurs.
536  *                You can provide a -1 timeout to obtain an infinite timeout.
537  * @return msg_error_t
538  */
539 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
540 {
541   try {
542     comm->s_comm->wait_for(timeout);
543
544     if (comm->task_received != nullptr) {
545       /* I am the receiver */
546       (*comm->task_received)->simdata->set_not_used();
547     }
548
549     /* FIXME: these functions are not traceable */
550   } catch (simgrid::TimeoutError& e) {
551     comm->status = MSG_TIMEOUT;
552   } catch (simgrid::CancelException& e) {
553     comm->status = MSG_TASK_CANCELED;
554   }
555   catch (xbt_ex& e) {
556     if (e.category == network_error)
557       comm->status = MSG_TRANSFER_FAILURE;
558     else
559       throw;
560   }
561
562   return comm->status;
563 }
564
565 /** @brief This function is called by a sender and permit to wait for each communication
566  *
567  * @param comm a vector of communication
568  * @param nb_elem is the size of the comm vector
569  * @param timeout for each call of MSG_comm_wait
570  */
571 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
572 {
573   for (int i = 0; i < nb_elem; i++)
574     MSG_comm_wait(comm[i], timeout);
575 }
576
577 /** @brief This function waits for the first communication finished in a list.
578  * @param comms a vector of communications
579  * @return the position of the first finished communication
580  * (but it may have failed, use MSG_comm_get_status() to know its status)
581  */
582 int MSG_comm_waitany(xbt_dynar_t comms)
583 {
584   int finished_index = -1;
585
586   /* Create the equivalent array with SIMIX objects: */
587   std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
588   s_comms.reserve(xbt_dynar_length(comms));
589   msg_comm_t comm;
590   unsigned int cursor;
591   xbt_dynar_foreach(comms, cursor, comm) {
592     s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
593   }
594
595   msg_error_t status = MSG_OK;
596   try {
597     finished_index = simcall_comm_waitany(s_comms.data(), s_comms.size(), -1);
598   } catch (simgrid::TimeoutError& e) {
599     finished_index = e.value;
600     status         = MSG_TIMEOUT;
601   } catch (simgrid::CancelException& e) {
602     finished_index = e.value;
603     status         = MSG_TASK_CANCELED;
604   }
605   catch(xbt_ex& e) {
606     if (e.category == network_error) {
607       finished_index = e.value;
608       status         = MSG_TRANSFER_FAILURE;
609     } else {
610       throw;
611     }
612   }
613
614   xbt_assert(finished_index != -1, "WaitAny returned -1");
615
616   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
617   /* the communication is finished */
618   comm->status = status;
619
620   if (comm->task_received != nullptr) {
621     /* I am the receiver */
622     (*comm->task_received)->simdata->set_not_used();
623   }
624
625   return finished_index;
626 }
627
628 /**
629  * @brief Returns the error (if any) that occurred during a finished communication.
630  * @param comm a finished communication
631  * @return the status of the communication, or #MSG_OK if no error occurred
632  * during the communication
633  */
634 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
635
636   return comm->status;
637 }
638
639 /** @brief Get a task (#msg_task_t) from a communication
640  *
641  * @param comm the communication where to get the task
642  * @return the task from the communication
643  */
644 msg_task_t MSG_comm_get_task(msg_comm_t comm)
645 {
646   xbt_assert(comm, "Invalid parameter");
647
648   return comm->task_received ? *comm->task_received : comm->task_sent;
649 }
650
651 /**
652  * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
653  * @param comm the comm
654  * @param buff the data copied
655  * @param buff_size size of the buffer
656  */
657 void MSG_comm_copy_data_from_SIMIX(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
658 {
659   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
660
661   // notify the user callback if any
662   if (msg_global->task_copy_callback) {
663     msg_task_t task = static_cast<msg_task_t>(buff);
664     msg_global->task_copy_callback(task, comm->src_actor_->ciface(), comm->dst_actor_->ciface());
665   }
666 }
667
668 /**
669  * @brief Sends a task to a mailbox
670  *
671  * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
672  * side if #MSG_task_receive is used).
673  * See #MSG_task_isend for sending tasks asynchronously.
674  *
675  * @param task the task to be sent
676  * @param alias the mailbox name to where the task is sent
677  *
678  * @return Returns #MSG_OK if the task was successfully sent,
679  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
680  */
681 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
682 {
683   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
684   return MSG_task_send_with_timeout(task, alias, -1);
685 }
686
687 /**
688  * @brief Sends a task to a mailbox with a maximum rate
689  *
690  * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
691  * the application to limit the bandwidth utilization of network links when sending the task.
692  *
693  * The maxrate parameter can be used to send a task with a limited
694  * bandwidth (smaller than the physical available value). Use
695  * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
696  * value do disable this feature).
697  *
698  * @param task the task to be sent
699  * @param alias the mailbox name to where the task is sent
700  * @param maxrate the maximum communication rate for sending this task (byte/sec)
701  *
702  * @return Returns #MSG_OK if the task was successfully sent,
703  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
704  */
705 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
706 {
707   task->simdata->rate = maxrate;
708   return MSG_task_send(task, alias);
709 }
710
711 /**
712  * @brief Sends a task to a mailbox with a timeout
713  *
714  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
715  *
716  * @param task the task to be sent
717  * @param alias the mailbox name to where the task is sent
718  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
719  *
720  * @return Returns #MSG_OK if the task was successfully sent,
721  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
722  */
723 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
724 {
725   msg_error_t ret = MSG_OK;
726
727   TRACE_msg_task_put_start(task);
728
729   /* Prepare the task to send */
730   simdata_task_t t_simdata = task->simdata;
731   t_simdata->sender        = MSG_process_self();
732   t_simdata->set_used();
733
734   msg_global->sent_msg++;
735
736   /* Try to send it */
737   try {
738     simgrid::s4u::CommPtr comm =
739         simgrid::s4u::Mailbox::by_name(alias)->put_init(task, t_simdata->bytes_amount)->set_rate(t_simdata->rate);
740     t_simdata->comm = comm;
741     comm->start();
742     if (TRACE_is_enabled() && task->category != nullptr)
743       simgrid::simix::simcall([comm, task] { comm->get_impl()->set_category(task->category); });
744     comm->wait_for(timeout);
745   } catch (simgrid::TimeoutError& e) {
746     ret = MSG_TIMEOUT;
747   } catch (simgrid::CancelException& e) {
748     ret = MSG_HOST_FAILURE;
749   } catch (xbt_ex& e) {
750     if (e.category == network_error)
751       ret = MSG_TRANSFER_FAILURE;
752     else
753       throw;
754
755     /* If the send failed, it is not used anymore */
756     t_simdata->set_not_used();
757   }
758
759   return ret;
760 }
761
762 /**
763  * @brief Sends a task to a mailbox with a timeout and with a maximum rate
764  *
765  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
766  *
767  * The maxrate parameter can be used to send a task with a limited
768  * bandwidth (smaller than the physical available value). Use
769  * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
770  * value do disable this feature).
771  *
772  * @param task the task to be sent
773  * @param alias the mailbox name to where the task is sent
774  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
775  * @param maxrate the maximum communication rate for sending this task (byte/sec)
776  *
777  * @return Returns #MSG_OK if the task was successfully sent,
778  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
779  */
780 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
781 {
782   task->simdata->rate = maxrate;
783   return MSG_task_send_with_timeout(task, alias, timeout);
784 }
785
786 /**
787  * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
788  *
789  * @param alias the name of the mailbox to be considered
790  *
791  * @return Returns the PID of sender process,
792  * -1 if there is no communication in the mailbox.
793  */
794 int MSG_task_listen_from(const char *alias)
795 {
796   simgrid::kernel::activity::CommImplPtr comm = simgrid::s4u::Mailbox::by_name(alias)->front();
797
798   return comm ? MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff_)->simdata->sender) : -1;
799 }
800
801 /**
802  * @brief Sets the tracing category of a task.
803  *
804  * This function should be called after the creation of a MSG task, to define the category of that task. The
805  * first parameter task must contain a task that was  created with the function #MSG_task_create. The second
806  * parameter category must contain a category that was previously declared with the function #TRACE_category
807  * (or with #TRACE_category_with_color).
808  *
809  * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
810  *
811  * @param task the task that is going to be categorized
812  * @param category the name of the category to be associated to the task
813  *
814  * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
815  */
816 void MSG_task_set_category (msg_task_t task, const char *category)
817 {
818   xbt_assert(task->category == nullptr, "Task %p(%s) already has a category (%s).", task, task->name, task->category);
819
820   // if user provides a nullptr category, task is no longer traced
821   if (category == nullptr) {
822     xbt_free(task->category);
823     task->category = nullptr;
824     XBT_DEBUG("MSG task %p(%s), category removed", task, task->name);
825   } else {
826     // set task category
827     task->category = xbt_strdup(category);
828     XBT_DEBUG("MSG task %p(%s), category %s", task, task->name, task->category);
829   }
830 }
831
832 /**
833  * @brief Gets the current tracing category of a task.
834  *
835  * @param task the task to be considered
836  *
837  * @see MSG_task_set_category
838  *
839  * @return Returns the name of the tracing category of the given task, nullptr otherwise
840  */
841 const char *MSG_task_get_category (msg_task_t task)
842 {
843   return task->category;
844 }