Logo AND Algorithmique Numérique Distribuée

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