Logo AND Algorithmique Numérique Distribuée

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