Logo AND Algorithmique Numérique Distribuée

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