1 /* Copyright (c) 2004-2019. 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"
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. */
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
18 * @brief Executes a task and waits for its termination.
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
24 msg_error_t MSG_task_execute(msg_task_t task)
26 return MSG_parallel_task_execute(task);
30 * @brief Executes a parallel task and waits for its termination.
32 * @param task a #msg_task_t to execute on the location on which the process is running.
34 * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
35 * or #MSG_HOST_FAILURE otherwise
37 msg_error_t MSG_parallel_task_execute(msg_task_t task)
39 return MSG_parallel_task_execute_with_timeout(task, -1);
42 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
44 simdata_task_t simdata = task->simdata;
45 e_smx_state_t comp_state;
46 msg_error_t status = MSG_OK;
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) {
58 if (TRACE_actor_is_enabled())
59 simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->push_event("execute");
64 if (simdata->host_nb > 0) {
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); });
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));
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!");
83 simdata->compute->start(simdata->flops_amount, simdata->priority, simdata->bound);
86 comp_state = simcall_execution_wait(simdata->compute);
88 simdata->setNotUsed();
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) {
96 if (e.category == cancel_error)
97 status = MSG_TASK_CANCELED;
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;
107 if (TRACE_actor_is_enabled())
108 simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->pop_event();
114 * @brief Receives a task from a mailbox.
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.
119 * @param task a memory location for storing a #msg_task_t.
120 * @param alias name of the mailbox to receive the task from
123 * #MSG_OK if the task was successfully received,
124 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
126 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
128 return MSG_task_receive_with_timeout(task, alias, -1);
132 * @brief Receives a task from a mailbox at a given rate.
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)
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).
144 * #MSG_OK if the task was successfully received,
145 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
147 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
149 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
153 * @brief Receives a task from a mailbox with a given timeout.
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.
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)
164 * #MSG_OK if the task was successfully received,
165 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
167 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
169 return MSG_task_receive_ext(task, alias, timeout, nullptr);
173 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
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)
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).
186 * #MSG_OK if the task was successfully received,
187 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
189 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
191 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
195 * @brief Receives a task from a mailbox from a specific host with a given timeout.
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.
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
207 * #MSG_OK if the task was successfully received,
208 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
210 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
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);
217 * @brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
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)
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).
231 * #MSG_OK if the task was successfully received,
232 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
234 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
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 */
244 TRACE_msg_task_get_start();
247 xbt_assert(task, "Null pointer for the task storage");
250 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
252 /* Try to receive it by calling SIMIX network layer */
254 simcall_comm_recv(MSG_process_self()->get_impl(), mailbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr,
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) {
262 } catch (xbt_ex& e) {
263 switch (e.category) {
265 ret = MSG_HOST_FAILURE;
268 ret = MSG_TRANSFER_FAILURE;
275 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
276 TRACE_msg_task_get_end(*task);
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)
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);
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++;
298 /* Send it by calling SIMIX network layer */
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);
304 msg_comm_t comm = nullptr;
306 comm = new simgrid::msg::Comm(task, nullptr, act);
309 if (TRACE_is_enabled() && task->category != nullptr)
310 simgrid::simix::simcall([act, task] { act->set_category(task->category); });
312 TRACE_msg_task_put_end();
318 * @brief Sends a task on a mailbox.
320 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
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
326 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
328 return MSG_task_isend_internal(task, alias, nullptr, 0);
332 * @brief Sends a task on a mailbox with a maximum rate
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.
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
342 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
344 task->simdata->rate = maxrate;
345 return MSG_task_isend_internal(task, alias, nullptr, 0);
349 * @brief Sends a task on a mailbox.
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.
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)
363 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
365 msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, 1);
366 xbt_assert(comm == nullptr);
370 * @brief Sends a task on a mailbox with a maximal rate.
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.
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).
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)
392 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
394 task->simdata->rate = maxrate;
395 MSG_task_dsend(task, alias, cleanup);
399 * @brief Starts listening for receiving a task from an asynchronous communication.
401 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
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
407 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
409 return MSG_task_irecv_bounded(task, name, -1.0);
413 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
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).
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
425 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
427 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
429 /* FIXME: these functions are not traceable */
431 xbt_assert(task, "Null pointer for the task storage");
434 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
436 /* Try to receive it by calling SIMIX network layer */
437 msg_comm_t comm = new simgrid::msg::Comm(
439 simcall_comm_irecv(SIMIX_process_self(), mbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr, rate));
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.
452 int MSG_comm_test(msg_comm_t comm)
454 bool finished = false;
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();
462 } catch (simgrid::TimeoutError& e) {
463 comm->status = MSG_TIMEOUT;
467 if (e.category == network_error) {
468 comm->status = MSG_TRANSFER_FAILURE;
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
485 int MSG_comm_testany(xbt_dynar_t comms)
487 int finished_index = -1;
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));
494 xbt_dynar_foreach(comms, cursor, comm) {
495 s_comms.push_back(comm->s_comm);
498 msg_error_t status = MSG_OK;
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;
506 if (e.category != network_error)
508 finished_index = e.value;
509 status = MSG_TRANSFER_FAILURE;
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;
517 if (status == MSG_OK && comm->task_received != nullptr) {
518 /* I am the receiver */
519 (*comm->task_received)->simdata->setNotUsed();
523 return finished_index;
526 /** @brief Destroys the provided communication. */
527 void MSG_comm_destroy(msg_comm_t comm)
532 /** @brief Wait for the completion of a communication.
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
540 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
543 simcall_comm_wait(comm->s_comm, timeout);
545 if (comm->task_received != nullptr) {
546 /* I am the receiver */
547 (*comm->task_received)->simdata->setNotUsed();
550 /* FIXME: these functions are not traceable */
551 } catch (simgrid::TimeoutError& e) {
552 comm->status = MSG_TIMEOUT;
555 if (e.category == network_error)
556 comm->status = MSG_TRANSFER_FAILURE;
564 /** @brief This function is called by a sender and permit to wait for each communication
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
570 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
572 for (int i = 0; i < nb_elem; i++)
573 MSG_comm_wait(comm[i], timeout);
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)
581 int MSG_comm_waitany(xbt_dynar_t comms)
583 int finished_index = -1;
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);
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());
596 msg_error_t status = MSG_OK;
598 finished_index = simcall_comm_waitany(s_comms, -1);
599 } catch (simgrid::TimeoutError& e) {
600 finished_index = e.value;
601 status = MSG_TIMEOUT;
604 if (e.category == network_error) {
605 finished_index = e.value;
606 status = MSG_TRANSFER_FAILURE;
612 xbt_assert(finished_index != -1, "WaitAny returned -1");
613 xbt_dynar_free(&s_comms);
615 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
616 /* the communication is finished */
617 comm->status = status;
619 if (comm->task_received != nullptr) {
620 /* I am the receiver */
621 (*comm->task_received)->simdata->setNotUsed();
624 return finished_index;
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
633 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
638 /** @brief Get a task (#msg_task_t) from a communication
640 * @param comm the communication where to get the task
641 * @return the task from the communication
643 msg_task_t MSG_comm_get_task(msg_comm_t comm)
645 xbt_assert(comm, "Invalid parameter");
647 return comm->task_received ? *comm->task_received : comm->task_sent;
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
656 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
658 simgrid::kernel::activity::CommImplPtr comm =
659 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
661 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
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());
671 * @brief Sends a task to a mailbox
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.
677 * @param task the task to be sent
678 * @param alias the mailbox name to where the task is sent
680 * @return Returns #MSG_OK if the task was successfully sent,
681 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
683 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
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);
690 * @brief Sends a task to a mailbox with a maximum rate
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.
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).
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)
704 * @return Returns #MSG_OK if the task was successfully sent,
705 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
707 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
709 task->simdata->rate = maxrate;
710 return MSG_task_send(task, alias);
714 * @brief Sends a task to a mailbox with a timeout
716 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
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)
722 * @return Returns #MSG_OK if the task was successfully sent,
723 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
725 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
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);
732 TRACE_msg_task_put_start(task);
734 /* Prepare the task to send */
735 t_simdata = task->simdata;
736 t_simdata->sender = process;
737 t_simdata->source = MSG_host_self();
739 t_simdata->setUsed();
741 t_simdata->comm = nullptr;
742 msg_global->sent_msg++;
744 /* Try to send it by calling SIMIX network layer */
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) {
757 switch (e.category) {
759 ret = MSG_HOST_FAILURE;
762 ret = MSG_TRANSFER_FAILURE;
768 /* If the send failed, it is not used anymore */
769 t_simdata->setNotUsed();
772 TRACE_msg_task_put_end();
777 * @brief Sends a task to a mailbox with a timeout and with a maximum rate
779 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
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).
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)
791 * @return Returns #MSG_OK if the task was successfully sent,
792 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
794 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
796 task->simdata->rate = maxrate;
797 return MSG_task_send_with_timeout(task, alias, timeout);
801 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
803 * @param alias the name of the mailbox to be considered
805 * @return Returns the PID of sender process,
806 * -1 if there is no communication in the mailbox.
808 int MSG_task_listen_from(const char *alias)
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());
817 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff_)->simdata->sender);
821 * @brief Sets the tracing category of a task.
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).
828 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
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
833 * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
835 void MSG_task_set_category (msg_task_t task, const char *category)
837 TRACE_msg_set_task_category (task, category);
841 * @brief Gets the current tracing category of a task.
843 * @param task the task to be considered
845 * @see MSG_task_set_category
847 * @return Returns the name of the tracing category of the given task, nullptr otherwise
849 const char *MSG_task_get_category (msg_task_t task)
851 return task->category;