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-2016. 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     switch (e.category) {
126     case cancel_error:
127       XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost."); 
128       // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
129       // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
130       // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
131       // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
132       // and did not change anythings at the C level.
133       // 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) 
134       status = MSG_TASK_CANCELED;
135       break;
136     default:
137       throw;
138     }
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(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
273     XBT_DEBUG("Got task %s from %s",(*task)->name,mailbox->name());
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*, smx_activity_t),
302                                                      void *match_data, void_f_pvoid_t cleanup, int detached)
303 {
304   simdata_task_t t_simdata = nullptr;
305   msg_process_t myself = SIMIX_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 = simcall_comm_isend(myself, mailbox->getImpl(), t_simdata->bytes_amount, t_simdata->rate,
319                                          task, sizeof(void *), match_fun, cleanup, nullptr, match_data,detached);
320   t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(act);
321
322   msg_comm_t comm = nullptr;
323   if (! detached) {
324     comm = xbt_new0(s_msg_comm_t, 1);
325     comm->task_sent = task;
326     comm->task_received = nullptr;
327     comm->status = MSG_OK;
328     comm->s_comm = act;
329   }
330
331   if (TRACE_is_enabled())
332     simcall_set_category(act, task->category);
333   if (call_end)
334     TRACE_msg_task_put_end();
335
336   return comm;
337 }
338
339 /** \ingroup msg_task_usage
340  * \brief Sends a task on a mailbox.
341  *
342  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
343  *
344  * \param task a #msg_task_t to send on another location.
345  * \param alias name of the mailbox to sent the task to
346  * \return the msg_comm_t communication created
347  */
348 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
349 {
350   return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
351 }
352
353 /** \ingroup msg_task_usage
354  * \brief Sends a task on a mailbox with a maximum rate
355  *
356  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
357  * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
358  *
359  * \param task a #msg_task_t to send on another location.
360  * \param alias name of the mailbox to sent the task to
361  * \param maxrate the maximum communication rate for sending this task .
362  * \return the msg_comm_t communication created
363  */
364 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
365 {
366   task->simdata->rate = maxrate;
367   return MSG_task_isend_internal(task, alias, nullptr, nullptr, nullptr, 0);
368 }
369
370 /** \ingroup msg_task_usage
371  * \brief Sends a task on a mailbox, with support for matching requests
372  *
373  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
374  *
375  * \param task a #msg_task_t to send on another location.
376  * \param alias name of the mailbox to sent the task to
377  * \param match_fun boolean function which parameters are:
378  *        - match_data_provided_here
379  *        - match_data_provided_by_other_side_if_any
380  *        - the_smx_synchro_describing_the_other_side
381  * \param match_data user provided data passed to match_fun
382  * \return the msg_comm_t communication created
383  */
384 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
385                                         int (*match_fun)(void*, void*, smx_activity_t), void *match_data)
386 {
387   return MSG_task_isend_internal(task, alias, match_fun, match_data, nullptr, 0);
388 }
389
390 /** \ingroup msg_task_usage
391  * \brief Sends a task on a mailbox.
392  *
393  * This is a non blocking detached send function.
394  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
395  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
396  * usual. More details on this can be obtained on
397  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
398  * in the SimGrid-user mailing list archive.
399  *
400  * \param task a #msg_task_t to send on another location.
401  * \param alias name of the mailbox to sent the task to
402  * \param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
403  * (if nullptr, no function will be called)
404  */
405 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
406 {
407   MSG_task_isend_internal(task, alias, nullptr, nullptr, cleanup, 1);
408 }
409
410 /** \ingroup msg_task_usage
411  * \brief Sends a task on a mailbox with a maximal rate.
412  *
413  * This is a non blocking detached send function.
414  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
415  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
416  * usual. More details on this can be obtained on
417  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
418  * in the SimGrid-user mailing list archive.
419  *
420  * \param task a #msg_task_t to send on another location.
421  * \param alias name of the mailbox to sent the task to
422  * \param cleanup a function to destroy the task if the
423  * communication fails, e.g. MSG_task_destroy
424  * (if nullptr, no function will be called)
425  * \param maxrate the maximum communication rate for sending this task
426  *
427  */
428 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
429 {
430   task->simdata->rate = maxrate;
431   MSG_task_dsend(task, alias, cleanup);
432 }
433
434 /** \ingroup msg_task_usage
435  * \brief Starts listening for receiving a task from an asynchronous communication.
436  *
437  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
438  *
439  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
440  * \param name of the mailbox to receive the task on
441  * \return the msg_comm_t communication created
442  */
443 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
444 {
445   return MSG_task_irecv_bounded(task, name, -1.0);
446 }
447
448 /** \ingroup msg_task_usage
449  * \brief Starts listening for receiving a task from an asynchronous communication at a given rate.
450  *
451  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
452  * \param name of the mailbox to receive the task on
453  * \param rate limit the bandwidth to the given rate
454  * \return the msg_comm_t communication created
455  */
456 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
457 {
458   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(name);
459
460   /* FIXME: these functions are not traceable */
461   /* Sanity check */
462   xbt_assert(task, "Null pointer for the task storage");
463
464   if (*task)
465     XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
466
467   /* Try to receive it by calling SIMIX network layer */
468   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
469   comm->task_sent = nullptr;
470   comm->task_received = task;
471   comm->status = MSG_OK;
472   comm->s_comm = simcall_comm_irecv(MSG_process_self(), mbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, rate);
473
474   return comm;
475 }
476
477 /** \ingroup msg_task_usage
478  * \brief Checks whether a communication is done, and if yes, finalizes it.
479  * \param comm the communication to test
480  * \return TRUE if the communication is finished
481  * (but it may have failed, use MSG_comm_get_status() to know its status)
482  * or FALSE if the communication is not finished yet
483  * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
484  */
485 int MSG_comm_test(msg_comm_t comm)
486 {
487   int finished = 0;
488
489   try {
490     finished = simcall_comm_test(comm->s_comm);
491     if (finished && comm->task_received != nullptr) {
492       /* I am the receiver */
493       (*comm->task_received)->simdata->setNotUsed();
494     }
495   }
496   catch (xbt_ex& e) {
497     switch (e.category) {
498       case network_error:
499         comm->status = MSG_TRANSFER_FAILURE;
500         finished = 1;
501         break;
502       case timeout_error:
503         comm->status = MSG_TIMEOUT;
504         finished = 1;
505         break;
506       default:
507         throw;
508     }
509   }
510
511   return finished;
512 }
513
514 /** \ingroup msg_task_usage
515  * \brief This function checks if a communication is finished.
516  * \param comms a vector of communications
517  * \return the position of the finished communication if any
518  * (but it may have failed, use MSG_comm_get_status() to know its status),
519  * or -1 if none is finished
520  */
521 int MSG_comm_testany(xbt_dynar_t comms)
522 {
523   int finished_index = -1;
524
525   /* Create the equivalent array with SIMIX objects: */
526   std::vector<simgrid::kernel::activity::ActivityImpl*> s_comms;
527   s_comms.reserve(xbt_dynar_length(comms));
528   msg_comm_t comm;
529   unsigned int cursor;
530   xbt_dynar_foreach(comms, cursor, comm) {
531     s_comms.push_back(comm->s_comm);
532   }
533
534   msg_error_t status = MSG_OK;
535   try {
536     finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
537   }
538   catch (xbt_ex& e) {
539     switch (e.category) {
540       case network_error:
541         finished_index = e.value;
542         status = MSG_TRANSFER_FAILURE;
543         break;
544       case timeout_error:
545         finished_index = e.value;
546         status = MSG_TIMEOUT;
547         break;
548       default:
549         throw;
550     }
551   }
552
553   if (finished_index != -1) {
554     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
555     /* the communication is finished */
556     comm->status = status;
557
558     if (status == MSG_OK && comm->task_received != nullptr) {
559       /* I am the receiver */
560       (*comm->task_received)->simdata->setNotUsed();
561     }
562   }
563
564   return finished_index;
565 }
566
567 /** \ingroup msg_task_usage
568  * \brief Destroys a communication.
569  * \param comm the communication to destroy.
570  */
571 void MSG_comm_destroy(msg_comm_t comm)
572 {
573   xbt_free(comm);
574 }
575
576 /** \ingroup msg_task_usage
577  * \brief Wait for the completion of a communication.
578  *
579  * It takes two parameters.
580  * \param comm the communication to wait.
581  * \param timeout Wait until the communication terminates or the timeout occurs.
582  *                You can provide a -1 timeout to obtain an infinite timeout.
583  * \return msg_error_t
584  */
585 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
586 {
587   try {
588     simcall_comm_wait(comm->s_comm, timeout);
589
590     if (comm->task_received != nullptr) {
591       /* I am the receiver */
592       (*comm->task_received)->simdata->setNotUsed();
593     }
594
595     /* FIXME: these functions are not traceable */
596   }
597   catch (xbt_ex& e) {
598     switch (e.category) {
599     case network_error:
600       comm->status = MSG_TRANSFER_FAILURE;
601       break;
602     case timeout_error:
603       comm->status = MSG_TIMEOUT;
604       break;
605     default:
606       throw;
607     }
608   }
609
610   return comm->status;
611 }
612
613 /** \ingroup msg_task_usage
614 * \brief This function is called by a sender and permit to wait for each communication
615 *
616 * \param comm a vector of communication
617 * \param nb_elem is the size of the comm vector
618 * \param timeout for each call of MSG_comm_wait
619 */
620 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
621 {
622   for (int i = 0; i < nb_elem; i++)
623     MSG_comm_wait(comm[i], timeout);
624 }
625
626 /** \ingroup msg_task_usage
627  * \brief This function waits for the first communication finished in a list.
628  * \param comms a vector of communications
629  * \return the position of the first finished communication
630  * (but it may have failed, use MSG_comm_get_status() to know its status)
631  */
632 int MSG_comm_waitany(xbt_dynar_t comms)
633 {
634   int finished_index = -1;
635
636   /* create the equivalent dynar with SIMIX objects */
637   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), nullptr);
638   msg_comm_t comm;
639   unsigned int cursor;
640   xbt_dynar_foreach(comms, cursor, comm) {
641     xbt_dynar_push(s_comms, &comm->s_comm);
642   }
643
644   msg_error_t status = MSG_OK;
645   try {
646     finished_index = simcall_comm_waitany(s_comms, -1);
647   }
648   catch(xbt_ex& e) {
649     switch (e.category) {
650       case network_error:
651         finished_index = e.value;
652         status = MSG_TRANSFER_FAILURE;
653         break;
654       case timeout_error:
655         finished_index = e.value;
656         status = MSG_TIMEOUT;
657         break;
658       default:
659         throw;
660     }
661   }
662
663   xbt_assert(finished_index != -1, "WaitAny returned -1");
664   xbt_dynar_free(&s_comms);
665
666   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
667   /* the communication is finished */
668   comm->status = status;
669
670   if (comm->task_received != nullptr) {
671     /* I am the receiver */
672     (*comm->task_received)->simdata->setNotUsed();
673   }
674
675   return finished_index;
676 }
677
678 /**
679  * \ingroup msg_task_usage
680  * \brief Returns the error (if any) that occurred during a finished communication.
681  * \param comm a finished communication
682  * \return the status of the communication, or #MSG_OK if no error occurred
683  * during the communication
684  */
685 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
686
687   return comm->status;
688 }
689
690 /** \ingroup msg_task_usage
691  * \brief Get a task (#msg_task_t) from a communication
692  *
693  * \param comm the communication where to get the task
694  * \return the task from the communication
695  */
696 msg_task_t MSG_comm_get_task(msg_comm_t comm)
697 {
698   xbt_assert(comm, "Invalid parameter");
699
700   return comm->task_received ? *comm->task_received : comm->task_sent;
701 }
702
703 /**
704  * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
705  * \param synchro the comm
706  * \param buff the data copied
707  * \param buff_size size of the buffer
708  */
709 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
710 {
711   simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
712
713   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
714
715   // notify the user callback if any
716   if (msg_global->task_copy_callback) {
717     msg_task_t task = static_cast<msg_task_t>(buff);
718     msg_global->task_copy_callback(task, comm->src_proc, comm->dst_proc);
719   }
720 }
721
722 /** \ingroup msg_task_usage
723  * \brief Sends a task to a mailbox
724  *
725  * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
726  * side if #MSG_task_receive is used).
727  * See #MSG_task_isend for sending tasks asynchronously.
728  *
729  * \param task the task to be sent
730  * \param alias the mailbox name to where the task is sent
731  *
732  * \return Returns #MSG_OK if the task was successfully sent,
733  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
734  */
735 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
736 {
737   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
738   return MSG_task_send_with_timeout(task, alias, -1);
739 }
740
741 /** \ingroup msg_task_usage
742  * \brief Sends a task to a mailbox with a maximum rate
743  *
744  * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
745  * the application to limit the bandwidth utilization of network links when sending the task.
746  *
747  * \param task the task to be sent
748  * \param alias the mailbox name to where the task is sent
749  * \param maxrate the maximum communication rate for sending this task
750  *
751  * \return Returns #MSG_OK if the task was successfully sent,
752  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
753  */
754 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
755 {
756   task->simdata->rate = maxrate;
757   return MSG_task_send(task, alias);
758 }
759
760 /** \ingroup msg_task_usage
761  * \brief Sends a task to a mailbox with a timeout
762  *
763  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
764  *
765  * \param task the task to be sent
766  * \param alias the mailbox name to where the task is sent
767  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
768  *
769  * \return Returns #MSG_OK if the task was successfully sent,
770  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
771  */
772 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
773 {
774   msg_error_t ret = MSG_OK;
775   simdata_task_t t_simdata = nullptr;
776   msg_process_t process = MSG_process_self();
777   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
778
779   int call_end = TRACE_msg_task_put_start(task);    //must be after CHECK_HOST()
780
781   /* Prepare the task to send */
782   t_simdata = task->simdata;
783   t_simdata->sender = process;
784   t_simdata->source = MSG_host_self();
785
786   t_simdata->setUsed();
787
788   t_simdata->comm = nullptr;
789   msg_global->sent_msg++;
790
791   /* Try to send it by calling SIMIX network layer */
792   try {
793     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call  */
794     comm = simcall_comm_isend(SIMIX_process_self(), mailbox->getImpl(),t_simdata->bytes_amount,
795                               t_simdata->rate, task, sizeof(void *), nullptr, nullptr, nullptr, task, 0);
796     if (TRACE_is_enabled())
797       simcall_set_category(comm, task->category);
798      t_simdata->comm = static_cast<simgrid::kernel::activity::Comm*>(comm);
799      simcall_comm_wait(comm, timeout);
800   }
801   catch (xbt_ex& e) {
802     switch (e.category) {
803     case cancel_error:
804       ret = MSG_HOST_FAILURE;
805       break;
806     case network_error:
807       ret = MSG_TRANSFER_FAILURE;
808       break;
809     case timeout_error:
810       ret = MSG_TIMEOUT;
811       break;
812     default:
813       throw;
814     }
815
816     /* If the send failed, it is not used anymore */
817     t_simdata->setNotUsed();
818   }
819
820   if (call_end)
821     TRACE_msg_task_put_end();
822   return ret;
823 }
824
825 /** \ingroup msg_task_usage
826  * \brief Sends a task to a mailbox with a timeout and with a maximum rate
827  *
828  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
829  *
830  * \param task the task to be sent
831  * \param alias the mailbox name to where the task is sent
832  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
833  * \param maxrate the maximum communication rate for sending this task
834  *
835  * \return Returns #MSG_OK if the task was successfully sent,
836  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
837  */
838 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
839 {
840   task->simdata->rate = maxrate;
841   return MSG_task_send_with_timeout(task, alias, timeout);
842 }
843
844 /** \ingroup msg_task_usage
845  * \brief Check if there is a communication going on in a mailbox.
846  *
847  * \param alias the name of the mailbox to be considered
848  *
849  * \return Returns 1 if there is a communication, 0 otherwise
850  */
851 int MSG_task_listen(const char *alias)
852 {
853   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
854   return !mbox->empty() ||
855     (mbox->getImpl()->permanent_receiver && !mbox->getImpl()->done_comm_queue.empty());
856 }
857
858 /** \ingroup msg_task_usage
859  * \brief Look if there is a communication on a mailbox and return the PID of the sender process.
860  *
861  * \param alias the name of the mailbox to be considered
862  *
863  * \return Returns the PID of sender process,
864  * -1 if there is no communication in the mailbox.
865  */
866 int MSG_task_listen_from(const char *alias)
867 {
868   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(alias);
869   simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(mbox->front());
870
871   if (!comm)
872     return -1;
873
874   return MSG_process_get_PID( static_cast<msg_task_t>(comm->src_data)->simdata->sender );
875 }
876
877 /** \ingroup msg_task_usage
878  * \brief Sets the tracing category of a task.
879  *
880  * This function should be called after the creation of a MSG task, to define the category of that task. The
881  * first parameter task must contain a task that was  created with the function #MSG_task_create. The second
882  * parameter category must contain a category that was previously declared with the function #TRACE_category
883  * (or with #TRACE_category_with_color).
884  *
885  * See \ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
886  *
887  * \param task the task that is going to be categorized
888  * \param category the name of the category to be associated to the task
889  *
890  * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
891  */
892 void MSG_task_set_category (msg_task_t task, const char *category)
893 {
894   TRACE_msg_set_task_category (task, category);
895 }
896
897 /** \ingroup msg_task_usage
898  *
899  * \brief Gets the current tracing category of a task.
900  *
901  * \param task the task to be considered
902  *
903  * \see MSG_task_set_category
904  *
905  * \return Returns the name of the tracing category of the given task, nullptr otherwise
906  */
907 const char *MSG_task_get_category (msg_task_t task)
908 {
909   return task->category;
910 }
911
912 SG_END_DECL()