Logo AND Algorithmique Numérique Distribuée

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