Logo AND Algorithmique Numérique Distribuée

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