Logo AND Algorithmique Numérique Distribuée

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