Logo AND Algorithmique Numérique Distribuée

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