Logo AND Algorithmique Numérique Distribuée

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