Logo AND Algorithmique Numérique Distribuée

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