Logo AND Algorithmique Numérique Distribuée

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