1 /* Copyright (c) 2004-2018. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/Exception.hpp"
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. */
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
16 /** @ingroup msg_task_usage
17 * @brief Executes a task and waits for its termination.
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
23 msg_error_t MSG_task_execute(msg_task_t task)
25 return MSG_parallel_task_execute(task);
28 /** @ingroup msg_task_usage
29 * @brief Executes a parallel task and waits for its termination.
31 * @param task a #msg_task_t to execute on the location on which the process is running.
33 * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
34 * or #MSG_HOST_FAILURE otherwise
36 msg_error_t MSG_parallel_task_execute(msg_task_t task)
38 return MSG_parallel_task_execute_with_timeout(task, -1);
41 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
43 simdata_task_t simdata = task->simdata;
44 e_smx_state_t comp_state;
45 msg_error_t status = MSG_OK;
47 TRACE_msg_task_execute_start(task);
49 xbt_assert((not simdata->compute) && not task->simdata->isused,
50 "This task is executed somewhere else. Go fix your code!");
52 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
54 if (simdata->flops_amount <= 0.0 && not simdata->host_nb) {
55 TRACE_msg_task_execute_end(task);
62 if (simdata->host_nb > 0) {
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 if (task->category != nullptr)
69 simcall_set_category(simdata->compute, task->category);
71 simdata->compute = boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(
72 simcall_execution_start(task->name ?: "", task->category ?: "", simdata->flops_amount, simdata->priority,
73 simdata->bound, MSG_process_get_host(MSG_process_self())));
76 comp_state = simcall_execution_wait(simdata->compute);
78 simdata->setNotUsed();
80 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
81 } catch (simgrid::HostFailureException& e) {
82 status = MSG_HOST_FAILURE;
83 } catch (simgrid::TimeoutError& e) {
86 if (e.category == cancel_error)
87 status = MSG_TASK_CANCELED;
92 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
93 simdata->flops_amount = 0.0;
94 simdata->comm = nullptr;
95 simdata->compute = nullptr;
96 TRACE_msg_task_execute_end(task);
101 /** @ingroup msg_task_usage
102 * @brief Sleep for the specified number of seconds
104 * Makes the current process sleep until @a time seconds have elapsed.
106 * @param nb_sec a number of second
108 msg_error_t MSG_process_sleep(double nb_sec)
110 msg_error_t status = MSG_OK;
113 simgrid::s4u::this_actor::sleep_for(nb_sec);
114 } catch (simgrid::HostFailureException& e) {
115 status = MSG_HOST_FAILURE;
116 } catch (xbt_ex& e) {
117 if (e.category == cancel_error) {
118 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
119 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
120 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
121 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
122 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
123 // and did not change anythings at the C level.
124 // 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)
125 status = MSG_TASK_CANCELED;
133 /** @ingroup msg_task_usage
134 * @brief Receives a task from a mailbox.
136 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
137 * for receiving tasks asynchronously.
139 * @param task a memory location for storing a #msg_task_t.
140 * @param alias name of the mailbox to receive the task from
143 * #MSG_OK if the task was successfully received,
144 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
146 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
148 return MSG_task_receive_with_timeout(task, alias, -1);
151 /** @ingroup msg_task_usage
152 * @brief Receives a task from a mailbox at a given rate.
154 * @param task a memory location for storing a #msg_task_t.
155 * @param alias name of the mailbox to receive the task from
156 * @param rate limit the reception to rate bandwidth (byte/sec)
158 * The rate parameter can be used to receive a task with a limited
159 * bandwidth (smaller than the physical available value). Use
160 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
161 * rate value do disable this feature).
164 * #MSG_OK if the task was successfully received,
165 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
167 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
169 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
172 /** @ingroup msg_task_usage
173 * @brief Receives a task from a mailbox with a given timeout.
175 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
176 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
177 * to obtain an infinite timeout.
179 * @param task a memory location for storing a #msg_task_t.
180 * @param alias name of the mailbox to receive the task from
181 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
184 * #MSG_OK if the task was successfully received,
185 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
187 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
189 return MSG_task_receive_ext(task, alias, timeout, nullptr);
192 /** @ingroup msg_task_usage
193 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
195 * @param task a memory location for storing a #msg_task_t.
196 * @param alias name of the mailbox to receive the task from
197 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
198 * @param rate limit the reception to rate bandwidth (byte/sec)
200 * The rate parameter can be used to send a task with a limited
201 * bandwidth (smaller than the physical available value). Use
202 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
203 * rate value do disable this feature).
206 * #MSG_OK if the task was successfully received,
207 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
209 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
211 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
214 /** @ingroup msg_task_usage
215 * @brief Receives a task from a mailbox from a specific host with a given timeout.
217 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
218 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
219 * to obtain an infinite timeout.
221 * @param task a memory location for storing a #msg_task_t.
222 * @param alias name of the mailbox to receive the task from
223 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
224 * @param host a #msg_host_t host from where the task was sent
227 * #MSG_OK if the task was successfully received,
228 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
230 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
232 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
233 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
236 /** @ingroup msg_task_usage
237 * @brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
239 * @param task a memory location for storing a #msg_task_t.
240 * @param alias name of the mailbox to receive the task from
241 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
242 * @param host a #msg_host_t host from where the task was sent
243 * @param rate limit the reception to rate bandwidth (byte/sec)
245 * The rate parameter can be used to receive a task with a limited
246 * bandwidth (smaller than the physical available value). Use
247 * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
248 * rate value do disable this feature).
251 * #MSG_OK if the task was successfully received,
252 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
254 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
257 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
258 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
259 msg_error_t ret = MSG_OK;
260 /* We no longer support getting a task from a specific host */
264 TRACE_msg_task_get_start();
267 xbt_assert(task, "Null pointer for the task storage");
270 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
272 /* Try to receive it by calling SIMIX network layer */
274 simcall_comm_recv(MSG_process_self()->get_impl(), mailbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr,
276 XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->get_cname());
277 (*task)->simdata->setNotUsed();
278 } catch (simgrid::HostFailureException& e) {
279 ret = MSG_HOST_FAILURE;
280 } catch (simgrid::TimeoutError& e) {
282 } catch (xbt_ex& e) {
283 switch (e.category) {
285 ret = MSG_HOST_FAILURE;
288 ret = MSG_TRANSFER_FAILURE;
295 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
296 TRACE_msg_task_get_end(*task);
301 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
302 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
303 void_f_pvoid_t cleanup, int detached)
305 simdata_task_t t_simdata = nullptr;
306 msg_process_t myself = MSG_process_self();
307 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
308 TRACE_msg_task_put_start(task);
310 /* Prepare the task to send */
311 t_simdata = task->simdata;
312 t_simdata->sender = myself;
313 t_simdata->source = MSG_host_self();
314 t_simdata->setUsed();
315 t_simdata->comm = nullptr;
316 msg_global->sent_msg++;
318 /* Send it by calling SIMIX network layer */
320 simcall_comm_isend(myself->get_impl(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
321 sizeof(void*), nullptr, cleanup, nullptr, nullptr, detached);
322 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
324 msg_comm_t comm = nullptr;
326 comm = new simgrid::msg::Comm(task, nullptr, act);
329 if (TRACE_is_enabled())
330 simcall_set_category(act, task->category);
331 TRACE_msg_task_put_end();
336 /** @ingroup msg_task_usage
337 * @brief Sends a task on a mailbox.
339 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
341 * @param task a #msg_task_t to send on another location.
342 * @param alias name of the mailbox to sent the task to
343 * @return the msg_comm_t communication created
345 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
347 return MSG_task_isend_internal(task, alias, nullptr, 0);
350 /** @ingroup msg_task_usage
351 * @brief Sends a task on a mailbox with a maximum rate
353 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
354 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
356 * @param task a #msg_task_t to send on another location.
357 * @param alias name of the mailbox to sent the task to
358 * @param maxrate the maximum communication rate for sending this task (byte/sec).
359 * @return the msg_comm_t communication created
361 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
363 task->simdata->rate = maxrate;
364 return MSG_task_isend_internal(task, alias, nullptr, 0);
367 /** @ingroup msg_task_usage
368 * @brief Sends a task on a mailbox.
370 * This is a non blocking detached send function.
371 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
372 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
373 * usual. More details on this can be obtained on
374 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
375 * in the SimGrid-user mailing list archive.
377 * @param task a #msg_task_t to send on another location.
378 * @param alias name of the mailbox to sent the task to
379 * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
380 * (if nullptr, no function will be called)
382 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
384 msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, 1);
385 xbt_assert(comm == nullptr);
388 /** @ingroup msg_task_usage
389 * @brief Sends a task on a mailbox with a maximal rate.
391 * This is a non blocking detached send function.
392 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
393 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
394 * usual. More details on this can be obtained on
395 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
396 * in the SimGrid-user mailing list archive.
398 * The rate parameter can be used to send a task with a limited
399 * bandwidth (smaller than the physical available value). Use
400 * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
401 * value do disable this feature).
403 * @param task a #msg_task_t to send on another location.
404 * @param alias name of the mailbox to sent the task to
405 * @param cleanup a function to destroy the task if the
406 * communication fails, e.g. MSG_task_destroy
407 * (if nullptr, no function will be called)
408 * @param maxrate the maximum communication rate for sending this task (byte/sec)
411 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
413 task->simdata->rate = maxrate;
414 MSG_task_dsend(task, alias, cleanup);
417 /** @ingroup msg_task_usage
418 * @brief Starts listening for receiving a task from an asynchronous communication.
420 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
422 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
423 * @param name of the mailbox to receive the task on
424 * @return the msg_comm_t communication created
426 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
428 return MSG_task_irecv_bounded(task, name, -1.0);
431 /** @ingroup msg_task_usage
432 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
434 * The rate parameter can be used to receive a task with a limited
435 * bandwidth (smaller than the physical available value). Use
436 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
437 * value do disable this feature).
439 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
440 * @param name of the mailbox to receive the task on
441 * @param rate limit the bandwidth to the given rate (byte/sec)
442 * @return the msg_comm_t communication created
444 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
446 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
448 /* FIXME: these functions are not traceable */
450 xbt_assert(task, "Null pointer for the task storage");
453 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
455 /* Try to receive it by calling SIMIX network layer */
456 msg_comm_t comm = new simgrid::msg::Comm(
458 simcall_comm_irecv(SIMIX_process_self(), mbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr, rate));
463 /** @ingroup msg_task_usage
464 * @brief Checks whether a communication is done, and if yes, finalizes it.
465 * @param comm the communication to test
466 * @return 'true' if the communication is finished
467 * (but it may have failed, use MSG_comm_get_status() to know its status)
468 * or 'false' if the communication is not finished yet
469 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
471 int MSG_comm_test(msg_comm_t comm)
473 bool finished = false;
476 finished = simcall_comm_test(comm->s_comm);
477 if (finished && comm->task_received != nullptr) {
478 /* I am the receiver */
479 (*comm->task_received)->simdata->setNotUsed();
481 } catch (simgrid::TimeoutError& e) {
482 comm->status = MSG_TIMEOUT;
486 if (e.category == network_error) {
487 comm->status = MSG_TRANSFER_FAILURE;
497 /** @ingroup msg_task_usage
498 * @brief This function checks if a communication is finished.
499 * @param comms a vector of communications
500 * @return the position of the finished communication if any
501 * (but it may have failed, use MSG_comm_get_status() to know its status),
502 * or -1 if none is finished
504 int MSG_comm_testany(xbt_dynar_t comms)
506 int finished_index = -1;
508 /* Create the equivalent array with SIMIX objects: */
509 std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
510 s_comms.reserve(xbt_dynar_length(comms));
513 xbt_dynar_foreach(comms, cursor, comm) {
514 s_comms.push_back(comm->s_comm);
517 msg_error_t status = MSG_OK;
519 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
520 } catch (simgrid::TimeoutError& e) {
521 finished_index = e.value;
522 status = MSG_TIMEOUT;
525 switch (e.category) {
527 finished_index = e.value;
528 status = MSG_TRANSFER_FAILURE;
535 if (finished_index != -1) {
536 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
537 /* the communication is finished */
538 comm->status = status;
540 if (status == MSG_OK && comm->task_received != nullptr) {
541 /* I am the receiver */
542 (*comm->task_received)->simdata->setNotUsed();
546 return finished_index;
549 /** @ingroup msg_task_usage
550 * @brief Destroys a communication.
551 * @param comm the communication to destroy.
553 void MSG_comm_destroy(msg_comm_t comm)
558 /** @ingroup msg_task_usage
559 * @brief Wait for the completion of a communication.
561 * It takes two parameters.
562 * @param comm the communication to wait.
563 * @param timeout Wait until the communication terminates or the timeout occurs.
564 * You can provide a -1 timeout to obtain an infinite timeout.
565 * @return msg_error_t
567 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
570 simcall_comm_wait(comm->s_comm, timeout);
572 if (comm->task_received != nullptr) {
573 /* I am the receiver */
574 (*comm->task_received)->simdata->setNotUsed();
577 /* FIXME: these functions are not traceable */
578 } catch (simgrid::TimeoutError& e) {
579 comm->status = MSG_TIMEOUT;
582 if (e.category == network_error)
583 comm->status = MSG_TRANSFER_FAILURE;
591 /** @ingroup msg_task_usage
592 * @brief This function is called by a sender and permit to wait for each communication
594 * @param comm a vector of communication
595 * @param nb_elem is the size of the comm vector
596 * @param timeout for each call of MSG_comm_wait
598 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
600 for (int i = 0; i < nb_elem; i++)
601 MSG_comm_wait(comm[i], timeout);
604 /** @ingroup msg_task_usage
605 * @brief This function waits for the first communication finished in a list.
606 * @param comms a vector of communications
607 * @return the position of the first finished communication
608 * (but it may have failed, use MSG_comm_get_status() to know its status)
610 int MSG_comm_waitany(xbt_dynar_t comms)
612 int finished_index = -1;
614 /* create the equivalent dynar with SIMIX objects */
615 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
616 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
620 xbt_dynar_foreach(comms, cursor, comm) {
621 intrusive_ptr_add_ref(comm->s_comm.get());
622 xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
625 msg_error_t status = MSG_OK;
627 finished_index = simcall_comm_waitany(s_comms, -1);
628 } catch (simgrid::TimeoutError& e) {
629 finished_index = e.value;
630 status = MSG_TIMEOUT;
633 if (e.category == network_error) {
634 finished_index = e.value;
635 status = MSG_TRANSFER_FAILURE;
641 xbt_assert(finished_index != -1, "WaitAny returned -1");
642 xbt_dynar_free(&s_comms);
644 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
645 /* the communication is finished */
646 comm->status = status;
648 if (comm->task_received != nullptr) {
649 /* I am the receiver */
650 (*comm->task_received)->simdata->setNotUsed();
653 return finished_index;
657 * @ingroup msg_task_usage
658 * @brief Returns the error (if any) that occurred during a finished communication.
659 * @param comm a finished communication
660 * @return the status of the communication, or #MSG_OK if no error occurred
661 * during the communication
663 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
668 /** @ingroup msg_task_usage
669 * @brief Get a task (#msg_task_t) from a communication
671 * @param comm the communication where to get the task
672 * @return the task from the communication
674 msg_task_t MSG_comm_get_task(msg_comm_t comm)
676 xbt_assert(comm, "Invalid parameter");
678 return comm->task_received ? *comm->task_received : comm->task_sent;
682 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
683 * @param synchro the comm
684 * @param buff the data copied
685 * @param buff_size size of the buffer
687 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
689 simgrid::kernel::activity::CommImplPtr comm =
690 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
692 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
694 // notify the user callback if any
695 if (msg_global->task_copy_callback) {
696 msg_task_t task = static_cast<msg_task_t>(buff);
697 msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
701 /** @ingroup msg_task_usage
702 * @brief Sends a task to a mailbox
704 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
705 * side if #MSG_task_receive is used).
706 * See #MSG_task_isend for sending tasks asynchronously.
708 * @param task the task to be sent
709 * @param alias the mailbox name to where the task is sent
711 * @return Returns #MSG_OK if the task was successfully sent,
712 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
714 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
716 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
717 return MSG_task_send_with_timeout(task, alias, -1);
720 /** @ingroup msg_task_usage
721 * @brief Sends a task to a mailbox with a maximum rate
723 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
724 * the application to limit the bandwidth utilization of network links when sending the task.
726 * The maxrate parameter can be used to send a task with a limited
727 * bandwidth (smaller than the physical available value). Use
728 * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
729 * value do disable this feature).
731 * @param task the task to be sent
732 * @param alias the mailbox name to where the task is sent
733 * @param maxrate the maximum communication rate for sending this task (byte/sec)
735 * @return Returns #MSG_OK if the task was successfully sent,
736 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
738 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
740 task->simdata->rate = maxrate;
741 return MSG_task_send(task, alias);
744 /** @ingroup msg_task_usage
745 * @brief Sends a task to a mailbox with a timeout
747 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
749 * @param task the task to be sent
750 * @param alias the mailbox name to where the task is sent
751 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
753 * @return Returns #MSG_OK if the task was successfully sent,
754 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
756 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
758 msg_error_t ret = MSG_OK;
759 simdata_task_t t_simdata = nullptr;
760 msg_process_t process = MSG_process_self();
761 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
763 TRACE_msg_task_put_start(task);
765 /* Prepare the task to send */
766 t_simdata = task->simdata;
767 t_simdata->sender = process;
768 t_simdata->source = MSG_host_self();
770 t_simdata->setUsed();
772 t_simdata->comm = nullptr;
773 msg_global->sent_msg++;
775 /* Try to send it by calling SIMIX network layer */
777 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
778 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
779 sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
780 if (TRACE_is_enabled() && task->category != nullptr)
781 simcall_set_category(comm, task->category);
782 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
783 simcall_comm_wait(comm, timeout);
784 } catch (simgrid::TimeoutError& e) {
788 switch (e.category) {
790 ret = MSG_HOST_FAILURE;
793 ret = MSG_TRANSFER_FAILURE;
799 /* If the send failed, it is not used anymore */
800 t_simdata->setNotUsed();
803 TRACE_msg_task_put_end();
807 /** @ingroup msg_task_usage
808 * @brief Sends a task to a mailbox with a timeout and with a maximum rate
810 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
812 * The maxrate parameter can be used to send a task with a limited
813 * bandwidth (smaller than the physical available value). Use
814 * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
815 * value do disable this feature).
817 * @param task the task to be sent
818 * @param alias the mailbox name to where the task is sent
819 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
820 * @param maxrate the maximum communication rate for sending this task (byte/sec)
822 * @return Returns #MSG_OK if the task was successfully sent,
823 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
825 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
827 task->simdata->rate = maxrate;
828 return MSG_task_send_with_timeout(task, alias, timeout);
831 /** @ingroup msg_task_usage
832 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
834 * @param alias the name of the mailbox to be considered
836 * @return Returns the PID of sender process,
837 * -1 if there is no communication in the mailbox.
839 int MSG_task_listen_from(const char *alias)
841 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(alias);
842 simgrid::kernel::activity::CommImplPtr comm =
843 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
848 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
851 /** @ingroup msg_task_usage
852 * @brief Sets the tracing category of a task.
854 * This function should be called after the creation of a MSG task, to define the category of that task. The
855 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
856 * parameter category must contain a category that was previously declared with the function #TRACE_category
857 * (or with #TRACE_category_with_color).
859 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
861 * @param task the task that is going to be categorized
862 * @param category the name of the category to be associated to the task
864 * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
866 void MSG_task_set_category (msg_task_t task, const char *category)
868 TRACE_msg_set_task_category (task, category);
871 /** @ingroup msg_task_usage
873 * @brief Gets the current tracing category of a task.
875 * @param task the task to be considered
877 * @see MSG_task_set_category
879 * @return Returns the name of the tracing category of the given task, nullptr otherwise
881 const char *MSG_task_get_category (msg_task_t task)
883 return task->category;