Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 (byte/sec)
163  *
164  * The rate parameter can be used to receive a task with a limited
165  * bandwidth (smaller than the physical available value). Use
166  * MSG_task_receive() if you don't limit the rate (or pass -1 as a
167  * rate value do disable this feature).
168  *
169  * \return Returns
170  * #MSG_OK if the task was successfully received,
171  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
172  */
173 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
174 {
175   return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
176 }
177
178 /** \ingroup msg_task_usage
179  * \brief Receives a task from a mailbox with a given timeout.
180  *
181  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
182  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously.  You can provide a -1 timeout
183  * to obtain an infinite timeout.
184  *
185  * \param task a memory location for storing a #msg_task_t.
186  * \param alias name of the mailbox to receive the task from
187  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
188  *
189  * \return Returns
190  * #MSG_OK if the task was successfully received,
191  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
192  */
193 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
194 {
195   return MSG_task_receive_ext(task, alias, timeout, nullptr);
196 }
197
198 /** \ingroup msg_task_usage
199  * \brief Receives a task from a mailbox with a given timeout and at a given rate.
200  *
201  * \param task a memory location for storing a #msg_task_t.
202  * \param alias name of the mailbox to receive the task from
203  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
204  * \param rate limit the reception to rate bandwidth (byte/sec)
205  *
206  * The rate parameter can be used to send a task with a limited
207  * bandwidth (smaller than the physical available value). Use
208  * MSG_task_receive() if you don't limit the rate (or pass -1 as a
209  * rate value do disable this feature).
210  *
211  * \return Returns
212  * #MSG_OK if the task was successfully received,
213  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
214  */
215 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
216 {
217   return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
218 }
219
220 /** \ingroup msg_task_usage
221  * \brief Receives a task from a mailbox from a specific host with a given timeout.
222  *
223  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
224  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
225  * to obtain an infinite timeout.
226  *
227  * \param task a memory location for storing a #msg_task_t.
228  * \param alias name of the mailbox to receive the task from
229  * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
230  * \param host a #msg_host_t host from where the task was sent
231  *
232  * \return Returns
233  * #MSG_OK if the task was successfully received,
234 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
235  */
236 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
237 {
238   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
239   return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
240 }
241
242 /** \ingroup msg_task_usage
243  * \brief Receives a task from a mailbox from a specific host with a given timeout  and at a given rate.
244  *
245  * \param task a memory location for storing a #msg_task_t.
246  * \param alias name of the mailbox to receive the task from
247  * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
248  * \param host a #msg_host_t host from where the task was sent
249  * \param rate limit the reception to rate bandwidth (byte/sec)
250  *
251  * The rate parameter can be used to receive a task with a limited
252  * bandwidth (smaller than the physical available value). Use
253  * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
254  * rate value do disable this feature).
255  *
256  * \return Returns
257  * #MSG_OK if the task was successfully received,
258  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
259  */
260 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
261                                          double rate)
262 {
263   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
264   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
265   msg_error_t ret = MSG_OK;
266   /* We no longer support getting a task from a specific host */
267   if (host)
268     THROW_UNIMPLEMENTED;
269
270   TRACE_msg_task_get_start();
271
272   /* Sanity check */
273   xbt_assert(task, "Null pointer for the task storage");
274
275   if (*task)
276     XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
277
278   /* Try to receive it by calling SIMIX network layer */
279   try {
280     simcall_comm_recv(MSG_process_self()->getImpl(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
281     XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->getCname());
282     (*task)->simdata->setNotUsed();
283   }
284   catch (xbt_ex& e) {
285     switch (e.category) {
286     case host_error:
287     case cancel_error:
288       ret = MSG_HOST_FAILURE;
289       break;
290     case network_error:
291       ret = MSG_TRANSFER_FAILURE;
292       break;
293     case timeout_error:
294       ret = MSG_TIMEOUT;
295       break;
296     default:
297       throw;
298     }
299   }
300
301   if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
302     TRACE_msg_task_get_end(*task);
303   }
304   return ret;
305 }
306
307 /* Internal function used to factorize code between MSG_task_isend_with_matching() and MSG_task_dsend(). */
308 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
309                                                  int (*match_fun)(void*, void*, void*), void* match_data,
310                                                  void_f_pvoid_t cleanup, int detached)
311 {
312   simdata_task_t t_simdata = nullptr;
313   msg_process_t myself = MSG_process_self();
314   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
315   int call_end = TRACE_msg_task_put_start(task);
316
317   /* Prepare the task to send */
318   t_simdata = task->simdata;
319   t_simdata->sender = myself;
320   t_simdata->source = MSG_host_self();
321   t_simdata->setUsed();
322   t_simdata->comm = nullptr;
323   msg_global->sent_msg++;
324
325   /* Send it by calling SIMIX network layer */
326   smx_activity_t act =
327       simcall_comm_isend(myself->getImpl(), mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate, task,
328                          sizeof(void*), (simix_match_func_t)match_fun, cleanup, nullptr, match_data, detached);
329   t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
330
331   msg_comm_t comm = nullptr;
332   if (not detached) {
333     comm = new simgrid::msg::Comm(task, nullptr, act);
334   }
335
336   if (TRACE_is_enabled())
337     simcall_set_category(act, task->category);
338   if (call_end)
339     TRACE_msg_task_put_end();
340
341   return comm;
342 }
343
344 /** \ingroup msg_task_usage
345  * \brief Sends a task on a mailbox.
346  *
347  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
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  * \return the msg_comm_t communication created
352  */
353 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
354 {
355   return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
356 }
357
358 /** \ingroup msg_task_usage
359  * \brief Sends a task on a mailbox with a maximum rate
360  *
361  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
362  * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
363  *
364  * \param task a #msg_task_t to send on another location.
365  * \param alias name of the mailbox to sent the task to
366  * \param maxrate the maximum communication rate for sending this task (byte/sec).
367  * \return the msg_comm_t communication created
368  */
369 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
370 {
371   task->simdata->rate = maxrate;
372   return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
373 }
374
375 /** \ingroup msg_task_usage
376  * \brief Sends a task on a mailbox, with support for matching requests
377  *
378  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
379  *
380  * \param task a #msg_task_t to send on another location.
381  * \param alias name of the mailbox to sent the task to
382  * \param match_fun boolean function which parameters are:
383  *        - match_data_provided_here
384  *        - match_data_provided_by_other_side_if_any
385  *        - the_smx_synchro_describing_the_other_side
386  * \param match_data user provided data passed to match_fun
387  * \return the msg_comm_t communication created
388  */
389 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char* alias, int (*match_fun)(void*, void*, void*),
390                                         void* match_data)
391 {
392   return MSG_task_isend_internal(task, alias, match_fun, match_data, nullptr, 0);
393 }
394
395 /** \ingroup msg_task_usage
396  * \brief Sends a task on a mailbox.
397  *
398  * This is a non blocking detached send function.
399  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
400  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
401  * usual. More details on this can be obtained on
402  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
403  * in the SimGrid-user mailing list archive.
404  *
405  * \param task a #msg_task_t to send on another location.
406  * \param alias name of the mailbox to sent the task to
407  * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
408  * (if nullptr, no function will be called)
409  */
410 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
411 {
412   MSG_task_isend_internal(task, alias, nullptr, nullptr, cleanup, 1);
413 }
414
415 /** \ingroup msg_task_usage
416  * \brief Sends a task on a mailbox with a maximal rate.
417  *
418  * This is a non blocking detached send function.
419  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
420  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
421  * usual. More details on this can be obtained on
422  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
423  * in the SimGrid-user mailing list archive.
424  *
425  * The rate parameter can be used to send a task with a limited
426  * bandwidth (smaller than the physical available value). Use
427  * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
428  * value do disable this feature).
429  *
430  * \param task a #msg_task_t to send on another location.
431  * \param alias name of the mailbox to sent the task to
432  * \param cleanup a function to destroy the task if the
433  * communication fails, e.g. MSG_task_destroy
434  * (if nullptr, no function will be called)
435  * \param maxrate the maximum communication rate for sending this task (byte/sec)
436  *
437  */
438 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
439 {
440   task->simdata->rate = maxrate;
441   MSG_task_dsend(task, alias, cleanup);
442 }
443
444 /** \ingroup msg_task_usage
445  * \brief Starts listening for receiving a task from an asynchronous communication.
446  *
447  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
448  *
449  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
450  * \param name of the mailbox to receive the task on
451  * \return the msg_comm_t communication created
452  */
453 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
454 {
455   return MSG_task_irecv_bounded(task, name, -1.0);
456 }
457
458 /** \ingroup msg_task_usage
459  * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
460  *
461  * The rate parameter can be used to receive a task with a limited
462  * bandwidth (smaller than the physical available value). Use
463  * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
464  * value do disable this feature).
465  *
466  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
467  * \param name of the mailbox to receive the task on
468  * \param rate limit the bandwidth to the given rate (byte/sec)
469  * \return the msg_comm_t communication created
470  */
471 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
472 {
473   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(name);
474
475   /* FIXME: these functions are not traceable */
476   /* Sanity check */
477   xbt_assert(task, "Null pointer for the task storage");
478
479   if (*task)
480     XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
481
482   /* Try to receive it by calling SIMIX network layer */
483   msg_comm_t comm =
484       new simgrid::msg::Comm(nullptr, task, simcall_comm_irecv(SIMIX_process_self(), mbox->getImpl(), task, nullptr,
485                                                                nullptr, nullptr, nullptr, rate));
486
487   return comm;
488 }
489
490 /** \ingroup msg_task_usage
491  * \brief Checks whether a communication is done, and if yes, finalizes it.
492  * \param comm the communication to test
493  * \return TRUE if the communication is finished
494  * (but it may have failed, use MSG_comm_get_status() to know its status)
495  * or FALSE if the communication is not finished yet
496  * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
497  */
498 int MSG_comm_test(msg_comm_t comm)
499 {
500   int finished = 0;
501
502   try {
503     finished = simcall_comm_test(comm->s_comm);
504     if (finished && comm->task_received != nullptr) {
505       /* I am the receiver */
506       (*comm->task_received)->simdata->setNotUsed();
507     }
508   }
509   catch (xbt_ex& e) {
510     switch (e.category) {
511       case network_error:
512         comm->status = MSG_TRANSFER_FAILURE;
513         finished = 1;
514         break;
515       case timeout_error:
516         comm->status = MSG_TIMEOUT;
517         finished = 1;
518         break;
519       default:
520         throw;
521     }
522   }
523
524   return finished;
525 }
526
527 /** \ingroup msg_task_usage
528  * \brief This function checks if a communication is finished.
529  * \param comms a vector of communications
530  * \return the position of the finished communication if any
531  * (but it may have failed, use MSG_comm_get_status() to know its status),
532  * or -1 if none is finished
533  */
534 int MSG_comm_testany(xbt_dynar_t comms)
535 {
536   int finished_index = -1;
537
538   /* Create the equivalent array with SIMIX objects: */
539   std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
540   s_comms.reserve(xbt_dynar_length(comms));
541   msg_comm_t comm;
542   unsigned int cursor;
543   xbt_dynar_foreach(comms, cursor, comm) {
544     s_comms.push_back(comm->s_comm);
545   }
546
547   msg_error_t status = MSG_OK;
548   try {
549     finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
550   }
551   catch (xbt_ex& e) {
552     switch (e.category) {
553       case network_error:
554         finished_index = e.value;
555         status = MSG_TRANSFER_FAILURE;
556         break;
557       case timeout_error:
558         finished_index = e.value;
559         status = MSG_TIMEOUT;
560         break;
561       default:
562         throw;
563     }
564   }
565
566   if (finished_index != -1) {
567     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
568     /* the communication is finished */
569     comm->status = status;
570
571     if (status == MSG_OK && comm->task_received != nullptr) {
572       /* I am the receiver */
573       (*comm->task_received)->simdata->setNotUsed();
574     }
575   }
576
577   return finished_index;
578 }
579
580 /** \ingroup msg_task_usage
581  * \brief Destroys a communication.
582  * \param comm the communication to destroy.
583  */
584 void MSG_comm_destroy(msg_comm_t comm)
585 {
586   delete comm;
587 }
588
589 /** \ingroup msg_task_usage
590  * \brief Wait for the completion of a communication.
591  *
592  * It takes two parameters.
593  * \param comm the communication to wait.
594  * \param timeout Wait until the communication terminates or the timeout occurs.
595  *                You can provide a -1 timeout to obtain an infinite timeout.
596  * \return msg_error_t
597  */
598 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
599 {
600   try {
601     simcall_comm_wait(comm->s_comm, timeout);
602
603     if (comm->task_received != nullptr) {
604       /* I am the receiver */
605       (*comm->task_received)->simdata->setNotUsed();
606     }
607
608     /* FIXME: these functions are not traceable */
609   }
610   catch (xbt_ex& e) {
611     switch (e.category) {
612     case network_error:
613       comm->status = MSG_TRANSFER_FAILURE;
614       break;
615     case timeout_error:
616       comm->status = MSG_TIMEOUT;
617       break;
618     default:
619       throw;
620     }
621   }
622
623   return comm->status;
624 }
625
626 /** \ingroup msg_task_usage
627 * \brief This function is called by a sender and permit to wait for each communication
628 *
629 * \param comm a vector of communication
630 * \param nb_elem is the size of the comm vector
631 * \param timeout for each call of MSG_comm_wait
632 */
633 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
634 {
635   for (int i = 0; i < nb_elem; i++)
636     MSG_comm_wait(comm[i], timeout);
637 }
638
639 /** \ingroup msg_task_usage
640  * \brief This function waits for the first communication finished in a list.
641  * \param comms a vector of communications
642  * \return the position of the first finished communication
643  * (but it may have failed, use MSG_comm_get_status() to know its status)
644  */
645 int MSG_comm_waitany(xbt_dynar_t comms)
646 {
647   int finished_index = -1;
648
649   /* create the equivalent dynar with SIMIX objects */
650   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
651     intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
652   });
653   msg_comm_t comm;
654   unsigned int cursor;
655   xbt_dynar_foreach(comms, cursor, comm) {
656     intrusive_ptr_add_ref(comm->s_comm.get());
657     xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
658   }
659
660   msg_error_t status = MSG_OK;
661   try {
662     finished_index = simcall_comm_waitany(s_comms, -1);
663   }
664   catch(xbt_ex& e) {
665     switch (e.category) {
666       case network_error:
667         finished_index = e.value;
668         status = MSG_TRANSFER_FAILURE;
669         break;
670       case timeout_error:
671         finished_index = e.value;
672         status = MSG_TIMEOUT;
673         break;
674       default:
675         throw;
676     }
677   }
678
679   xbt_assert(finished_index != -1, "WaitAny returned -1");
680   xbt_dynar_free(&s_comms);
681
682   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
683   /* the communication is finished */
684   comm->status = status;
685
686   if (comm->task_received != nullptr) {
687     /* I am the receiver */
688     (*comm->task_received)->simdata->setNotUsed();
689   }
690
691   return finished_index;
692 }
693
694 /**
695  * \ingroup msg_task_usage
696  * \brief Returns the error (if any) that occurred during a finished communication.
697  * \param comm a finished communication
698  * \return the status of the communication, or #MSG_OK if no error occurred
699  * during the communication
700  */
701 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
702
703   return comm->status;
704 }
705
706 /** \ingroup msg_task_usage
707  * \brief Get a task (#msg_task_t) from a communication
708  *
709  * \param comm the communication where to get the task
710  * \return the task from the communication
711  */
712 msg_task_t MSG_comm_get_task(msg_comm_t comm)
713 {
714   xbt_assert(comm, "Invalid parameter");
715
716   return comm->task_received ? *comm->task_received : comm->task_sent;
717 }
718
719 /**
720  * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
721  * \param synchro the comm
722  * \param buff the data copied
723  * \param buff_size size of the buffer
724  */
725 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
726 {
727   simgrid::kernel::activity::CommImplPtr comm =
728       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
729
730   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
731
732   // notify the user callback if any
733   if (msg_global->task_copy_callback) {
734     msg_task_t task = static_cast<msg_task_t>(buff);
735     msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
736   }
737 }
738
739 /** \ingroup msg_task_usage
740  * \brief Sends a task to a mailbox
741  *
742  * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
743  * side if #MSG_task_receive is used).
744  * See #MSG_task_isend for sending tasks asynchronously.
745  *
746  * \param task the task to be sent
747  * \param alias the mailbox name to where the task is sent
748  *
749  * \return Returns #MSG_OK if the task was successfully sent,
750  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
751  */
752 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
753 {
754   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
755   return MSG_task_send_with_timeout(task, alias, -1);
756 }
757
758 /** \ingroup msg_task_usage
759  * \brief Sends a task to a mailbox with a maximum rate
760  *
761  * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
762  * the application to limit the bandwidth utilization of network links when sending the task.
763  *
764  * The maxrate parameter can be used to send a task with a limited
765  * bandwidth (smaller than the physical available value). Use
766  * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
767  * value do disable this feature).
768  *
769  * \param task the task to be sent
770  * \param alias the mailbox name to where the task is sent
771  * \param maxrate the maximum communication rate for sending this task (byte/sec)
772  *
773  * \return Returns #MSG_OK if the task was successfully sent,
774  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
775  */
776 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
777 {
778   task->simdata->rate = maxrate;
779   return MSG_task_send(task, alias);
780 }
781
782 /** \ingroup msg_task_usage
783  * \brief Sends a task to a mailbox with a timeout
784  *
785  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
786  *
787  * \param task the task to be sent
788  * \param alias the mailbox name to where the task is sent
789  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
790  *
791  * \return Returns #MSG_OK if the task was successfully sent,
792  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
793  */
794 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
795 {
796   msg_error_t ret = MSG_OK;
797   simdata_task_t t_simdata = nullptr;
798   msg_process_t process = MSG_process_self();
799   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
800
801   int call_end = TRACE_msg_task_put_start(task);
802
803   /* Prepare the task to send */
804   t_simdata = task->simdata;
805   t_simdata->sender = process;
806   t_simdata->source = MSG_host_self();
807
808   t_simdata->setUsed();
809
810   t_simdata->comm = nullptr;
811   msg_global->sent_msg++;
812
813   /* Try to send it by calling SIMIX network layer */
814   try {
815     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call  */
816     comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
817                               t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
818     if (TRACE_is_enabled())
819       simcall_set_category(comm, task->category);
820     t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
821     simcall_comm_wait(comm, timeout);
822   }
823   catch (xbt_ex& e) {
824     switch (e.category) {
825     case cancel_error:
826       ret = MSG_HOST_FAILURE;
827       break;
828     case network_error:
829       ret = MSG_TRANSFER_FAILURE;
830       break;
831     case timeout_error:
832       ret = MSG_TIMEOUT;
833       break;
834     default:
835       throw;
836     }
837
838     /* If the send failed, it is not used anymore */
839     t_simdata->setNotUsed();
840   }
841
842   if (call_end)
843     TRACE_msg_task_put_end();
844   return ret;
845 }
846
847 /** \ingroup msg_task_usage
848  * \brief Sends a task to a mailbox with a timeout and with a maximum rate
849  *
850  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
851  *
852  * The maxrate parameter can be used to send a task with a limited
853  * bandwidth (smaller than the physical available value). Use
854  * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
855  * value do disable this feature).
856  *
857  * \param task the task to be sent
858  * \param alias the mailbox name to where the task is sent
859  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
860  * \param maxrate the maximum communication rate for sending this task (byte/sec)
861  *
862  * \return Returns #MSG_OK if the task was successfully sent,
863  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
864  */
865 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
866 {
867   task->simdata->rate = maxrate;
868   return MSG_task_send_with_timeout(task, alias, timeout);
869 }
870
871 /** \ingroup msg_task_usage
872  * \brief Check if there is a communication going on in a mailbox.
873  *
874  * \param alias the name of the mailbox to be considered
875  *
876  * \return Returns 1 if there is a communication, 0 otherwise
877  */
878 int MSG_task_listen(const char *alias)
879 {
880   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
881   return mbox->listen() ? 1 : 0;
882 }
883
884 /** \ingroup msg_task_usage
885  * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
886  *
887  * \param alias the name of the mailbox to be considered
888  *
889  * \return Returns the PID of sender process,
890  * -1 if there is no communication in the mailbox.
891  */
892 int MSG_task_listen_from(const char *alias)
893 {
894   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
895   simgrid::kernel::activity::CommImplPtr comm =
896       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
897
898   if (not comm)
899     return -1;
900
901   return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
902 }
903
904 /** \ingroup msg_task_usage
905  * \brief Sets the tracing category of a task.
906  *
907  * This function should be called after the creation of a MSG task, to define the category of that task. The
908  * first parameter task must contain a task that was  created with the function #MSG_task_create. The second
909  * parameter category must contain a category that was previously declared with the function #TRACE_category
910  * (or with #TRACE_category_with_color).
911  *
912  * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
913  *
914  * \param task the task that is going to be categorized
915  * \param category the name of the category to be associated to the task
916  *
917  * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
918  */
919 void MSG_task_set_category (msg_task_t task, const char *category)
920 {
921   TRACE_msg_set_task_category (task, category);
922 }
923
924 /** \ingroup msg_task_usage
925  *
926  * \brief Gets the current tracing category of a task.
927  *
928  * \param task the task to be considered
929  *
930  * \see MSG_task_set_category
931  *
932  * \return Returns the name of the tracing category of the given task, nullptr otherwise
933  */
934 const char *MSG_task_get_category (msg_task_t task)
935 {
936   return task->category;
937 }
938 }