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) {
88 status = MSG_TASK_CANCELED;
95 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
96 simdata->flops_amount = 0.0;
97 simdata->comm = nullptr;
98 simdata->compute = nullptr;
99 TRACE_msg_task_execute_end(task);
104 /** @ingroup msg_task_usage
105 * @brief Sleep for the specified number of seconds
107 * Makes the current process sleep until @a time seconds have elapsed.
109 * @param nb_sec a number of second
111 msg_error_t MSG_process_sleep(double nb_sec)
113 msg_error_t status = MSG_OK;
116 simgrid::s4u::this_actor::sleep_for(nb_sec);
117 } catch (simgrid::HostFailureException& e) {
118 status = MSG_HOST_FAILURE;
119 } catch (xbt_ex& e) {
120 if (e.category == cancel_error) {
121 XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
122 // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
123 // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
124 // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
125 // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
126 // and did not change anythings at the C level.
127 // See comment in the jmsg_process.c file, function JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
128 status = MSG_TASK_CANCELED;
136 /** @ingroup msg_task_usage
137 * @brief Receives a task from a mailbox.
139 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
140 * for receiving tasks asynchronously.
142 * @param task a memory location for storing a #msg_task_t.
143 * @param alias name of the mailbox to receive the task from
146 * #MSG_OK if the task was successfully received,
147 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
149 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
151 return MSG_task_receive_with_timeout(task, alias, -1);
154 /** @ingroup msg_task_usage
155 * @brief Receives a task from a mailbox at a given rate.
157 * @param task a memory location for storing a #msg_task_t.
158 * @param alias name of the mailbox to receive the task from
159 * @param rate limit the reception to rate bandwidth (byte/sec)
161 * The rate parameter can be used to receive a task with a limited
162 * bandwidth (smaller than the physical available value). Use
163 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
164 * rate value do disable this feature).
167 * #MSG_OK if the task was successfully received,
168 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
170 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
172 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
175 /** @ingroup msg_task_usage
176 * @brief Receives a task from a mailbox with a given timeout.
178 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
179 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
180 * to obtain an infinite timeout.
182 * @param task a memory location for storing a #msg_task_t.
183 * @param alias name of the mailbox to receive the task from
184 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
187 * #MSG_OK if the task was successfully received,
188 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
190 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
192 return MSG_task_receive_ext(task, alias, timeout, nullptr);
195 /** @ingroup msg_task_usage
196 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
198 * @param task a memory location for storing a #msg_task_t.
199 * @param alias name of the mailbox to receive the task from
200 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
201 * @param rate limit the reception to rate bandwidth (byte/sec)
203 * The rate parameter can be used to send a task with a limited
204 * bandwidth (smaller than the physical available value). Use
205 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
206 * rate value do disable this feature).
209 * #MSG_OK if the task was successfully received,
210 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
212 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
214 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
217 /** @ingroup msg_task_usage
218 * @brief Receives a task from a mailbox from a specific host with a given timeout.
220 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
221 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
222 * to obtain an infinite timeout.
224 * @param task a memory location for storing a #msg_task_t.
225 * @param alias name of the mailbox to receive the task from
226 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
227 * @param host a #msg_host_t host from where the task was sent
230 * #MSG_OK if the task was successfully received,
231 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
233 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
235 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
236 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
239 /** @ingroup msg_task_usage
240 * @brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
242 * @param task a memory location for storing a #msg_task_t.
243 * @param alias name of the mailbox to receive the task from
244 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
245 * @param host a #msg_host_t host from where the task was sent
246 * @param rate limit the reception to rate bandwidth (byte/sec)
248 * The rate parameter can be used to receive a task with a limited
249 * bandwidth (smaller than the physical available value). Use
250 * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
251 * rate value do disable this feature).
254 * #MSG_OK if the task was successfully received,
255 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
257 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
260 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
261 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
262 msg_error_t ret = MSG_OK;
263 /* We no longer support getting a task from a specific host */
267 TRACE_msg_task_get_start();
270 xbt_assert(task, "Null pointer for the task storage");
273 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
275 /* Try to receive it by calling SIMIX network layer */
277 simcall_comm_recv(MSG_process_self()->get_impl(), mailbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr,
279 XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->get_cname());
280 (*task)->simdata->setNotUsed();
281 } catch (simgrid::HostFailureException& e) {
282 ret = MSG_HOST_FAILURE;
283 } catch (simgrid::TimeoutError& e) {
285 } catch (xbt_ex& e) {
286 switch (e.category) {
288 ret = MSG_HOST_FAILURE;
291 ret = MSG_TRANSFER_FAILURE;
298 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
299 TRACE_msg_task_get_end(*task);
304 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
305 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
306 void_f_pvoid_t cleanup, int detached)
308 simdata_task_t t_simdata = nullptr;
309 msg_process_t myself = MSG_process_self();
310 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
311 TRACE_msg_task_put_start(task);
313 /* Prepare the task to send */
314 t_simdata = task->simdata;
315 t_simdata->sender = myself;
316 t_simdata->source = MSG_host_self();
317 t_simdata->setUsed();
318 t_simdata->comm = nullptr;
319 msg_global->sent_msg++;
321 /* Send it by calling SIMIX network layer */
323 simcall_comm_isend(myself->get_impl(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
324 sizeof(void*), nullptr, cleanup, nullptr, nullptr, detached);
325 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
327 msg_comm_t comm = nullptr;
329 comm = new simgrid::msg::Comm(task, nullptr, act);
332 if (TRACE_is_enabled())
333 simcall_set_category(act, task->category);
334 TRACE_msg_task_put_end();
339 /** @ingroup msg_task_usage
340 * @brief Sends a task on a mailbox.
342 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
344 * @param task a #msg_task_t to send on another location.
345 * @param alias name of the mailbox to sent the task to
346 * @return the msg_comm_t communication created
348 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
350 return MSG_task_isend_internal(task, alias, nullptr, 0);
353 /** @ingroup msg_task_usage
354 * @brief Sends a task on a mailbox with a maximum rate
356 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
357 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
359 * @param task a #msg_task_t to send on another location.
360 * @param alias name of the mailbox to sent the task to
361 * @param maxrate the maximum communication rate for sending this task (byte/sec).
362 * @return the msg_comm_t communication created
364 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
366 task->simdata->rate = maxrate;
367 return MSG_task_isend_internal(task, alias, nullptr, 0);
370 /** @ingroup msg_task_usage
371 * @brief Sends a task on a mailbox.
373 * This is a non blocking detached send function.
374 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
375 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
376 * usual. More details on this can be obtained on
377 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
378 * in the SimGrid-user mailing list archive.
380 * @param task a #msg_task_t to send on another location.
381 * @param alias name of the mailbox to sent the task to
382 * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
383 * (if nullptr, no function will be called)
385 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
387 msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, 1);
388 xbt_assert(comm == nullptr);
391 /** @ingroup msg_task_usage
392 * @brief Sends a task on a mailbox with a maximal rate.
394 * This is a non blocking detached send function.
395 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
396 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
397 * usual. More details on this can be obtained on
398 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
399 * in the SimGrid-user mailing list archive.
401 * The rate parameter can be used to send a task with a limited
402 * bandwidth (smaller than the physical available value). Use
403 * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
404 * value do disable this feature).
406 * @param task a #msg_task_t to send on another location.
407 * @param alias name of the mailbox to sent the task to
408 * @param cleanup a function to destroy the task if the
409 * communication fails, e.g. MSG_task_destroy
410 * (if nullptr, no function will be called)
411 * @param maxrate the maximum communication rate for sending this task (byte/sec)
414 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
416 task->simdata->rate = maxrate;
417 MSG_task_dsend(task, alias, cleanup);
420 /** @ingroup msg_task_usage
421 * @brief Starts listening for receiving a task from an asynchronous communication.
423 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
425 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
426 * @param name of the mailbox to receive the task on
427 * @return the msg_comm_t communication created
429 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
431 return MSG_task_irecv_bounded(task, name, -1.0);
434 /** @ingroup msg_task_usage
435 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
437 * The rate parameter can be used to receive a task with a limited
438 * bandwidth (smaller than the physical available value). Use
439 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
440 * value do disable this feature).
442 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
443 * @param name of the mailbox to receive the task on
444 * @param rate limit the bandwidth to the given rate (byte/sec)
445 * @return the msg_comm_t communication created
447 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
449 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
451 /* FIXME: these functions are not traceable */
453 xbt_assert(task, "Null pointer for the task storage");
456 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
458 /* Try to receive it by calling SIMIX network layer */
459 msg_comm_t comm = new simgrid::msg::Comm(
461 simcall_comm_irecv(SIMIX_process_self(), mbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr, rate));
466 /** @ingroup msg_task_usage
467 * @brief Checks whether a communication is done, and if yes, finalizes it.
468 * @param comm the communication to test
469 * @return 'true' if the communication is finished
470 * (but it may have failed, use MSG_comm_get_status() to know its status)
471 * or 'false' if the communication is not finished yet
472 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
474 int MSG_comm_test(msg_comm_t comm)
476 bool finished = false;
479 finished = simcall_comm_test(comm->s_comm);
480 if (finished && comm->task_received != nullptr) {
481 /* I am the receiver */
482 (*comm->task_received)->simdata->setNotUsed();
484 } catch (simgrid::TimeoutError& e) {
485 comm->status = MSG_TIMEOUT;
489 switch (e.category) {
491 comm->status = MSG_TRANSFER_FAILURE;
502 /** @ingroup msg_task_usage
503 * @brief This function checks if a communication is finished.
504 * @param comms a vector of communications
505 * @return the position of the finished communication if any
506 * (but it may have failed, use MSG_comm_get_status() to know its status),
507 * or -1 if none is finished
509 int MSG_comm_testany(xbt_dynar_t comms)
511 int finished_index = -1;
513 /* Create the equivalent array with SIMIX objects: */
514 std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
515 s_comms.reserve(xbt_dynar_length(comms));
518 xbt_dynar_foreach(comms, cursor, comm) {
519 s_comms.push_back(comm->s_comm);
522 msg_error_t status = MSG_OK;
524 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
525 } catch (simgrid::TimeoutError& e) {
526 finished_index = e.value;
527 status = MSG_TIMEOUT;
530 switch (e.category) {
532 finished_index = e.value;
533 status = MSG_TRANSFER_FAILURE;
540 if (finished_index != -1) {
541 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
542 /* the communication is finished */
543 comm->status = status;
545 if (status == MSG_OK && comm->task_received != nullptr) {
546 /* I am the receiver */
547 (*comm->task_received)->simdata->setNotUsed();
551 return finished_index;
554 /** @ingroup msg_task_usage
555 * @brief Destroys a communication.
556 * @param comm the communication to destroy.
558 void MSG_comm_destroy(msg_comm_t comm)
563 /** @ingroup msg_task_usage
564 * @brief Wait for the completion of a communication.
566 * It takes two parameters.
567 * @param comm the communication to wait.
568 * @param timeout Wait until the communication terminates or the timeout occurs.
569 * You can provide a -1 timeout to obtain an infinite timeout.
570 * @return msg_error_t
572 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
575 simcall_comm_wait(comm->s_comm, timeout);
577 if (comm->task_received != nullptr) {
578 /* I am the receiver */
579 (*comm->task_received)->simdata->setNotUsed();
582 /* FIXME: these functions are not traceable */
583 } catch (simgrid::TimeoutError& e) {
584 comm->status = MSG_TIMEOUT;
587 switch (e.category) {
589 comm->status = MSG_TRANSFER_FAILURE;
599 /** @ingroup msg_task_usage
600 * @brief This function is called by a sender and permit to wait for each communication
602 * @param comm a vector of communication
603 * @param nb_elem is the size of the comm vector
604 * @param timeout for each call of MSG_comm_wait
606 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
608 for (int i = 0; i < nb_elem; i++)
609 MSG_comm_wait(comm[i], timeout);
612 /** @ingroup msg_task_usage
613 * @brief This function waits for the first communication finished in a list.
614 * @param comms a vector of communications
615 * @return the position of the first finished communication
616 * (but it may have failed, use MSG_comm_get_status() to know its status)
618 int MSG_comm_waitany(xbt_dynar_t comms)
620 int finished_index = -1;
622 /* create the equivalent dynar with SIMIX objects */
623 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
624 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
628 xbt_dynar_foreach(comms, cursor, comm) {
629 intrusive_ptr_add_ref(comm->s_comm.get());
630 xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
633 msg_error_t status = MSG_OK;
635 finished_index = simcall_comm_waitany(s_comms, -1);
636 } catch (simgrid::TimeoutError& e) {
637 finished_index = e.value;
638 status = MSG_TIMEOUT;
641 switch (e.category) {
643 finished_index = e.value;
644 status = MSG_TRANSFER_FAILURE;
651 xbt_assert(finished_index != -1, "WaitAny returned -1");
652 xbt_dynar_free(&s_comms);
654 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
655 /* the communication is finished */
656 comm->status = status;
658 if (comm->task_received != nullptr) {
659 /* I am the receiver */
660 (*comm->task_received)->simdata->setNotUsed();
663 return finished_index;
667 * @ingroup msg_task_usage
668 * @brief Returns the error (if any) that occurred during a finished communication.
669 * @param comm a finished communication
670 * @return the status of the communication, or #MSG_OK if no error occurred
671 * during the communication
673 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
678 /** @ingroup msg_task_usage
679 * @brief Get a task (#msg_task_t) from a communication
681 * @param comm the communication where to get the task
682 * @return the task from the communication
684 msg_task_t MSG_comm_get_task(msg_comm_t comm)
686 xbt_assert(comm, "Invalid parameter");
688 return comm->task_received ? *comm->task_received : comm->task_sent;
692 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
693 * @param synchro the comm
694 * @param buff the data copied
695 * @param buff_size size of the buffer
697 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
699 simgrid::kernel::activity::CommImplPtr comm =
700 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
702 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
704 // notify the user callback if any
705 if (msg_global->task_copy_callback) {
706 msg_task_t task = static_cast<msg_task_t>(buff);
707 msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
711 /** @ingroup msg_task_usage
712 * @brief Sends a task to a mailbox
714 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
715 * side if #MSG_task_receive is used).
716 * See #MSG_task_isend for sending tasks asynchronously.
718 * @param task the task to be sent
719 * @param alias the mailbox name to where the task is sent
721 * @return Returns #MSG_OK if the task was successfully sent,
722 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
724 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
726 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
727 return MSG_task_send_with_timeout(task, alias, -1);
730 /** @ingroup msg_task_usage
731 * @brief Sends a task to a mailbox with a maximum rate
733 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
734 * the application to limit the bandwidth utilization of network links when sending the task.
736 * The maxrate parameter can be used to send a task with a limited
737 * bandwidth (smaller than the physical available value). Use
738 * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
739 * value do disable this feature).
741 * @param task the task to be sent
742 * @param alias the mailbox name to where the task is sent
743 * @param maxrate the maximum communication rate for sending this task (byte/sec)
745 * @return Returns #MSG_OK if the task was successfully sent,
746 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
748 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
750 task->simdata->rate = maxrate;
751 return MSG_task_send(task, alias);
754 /** @ingroup msg_task_usage
755 * @brief Sends a task to a mailbox with a timeout
757 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
759 * @param task the task to be sent
760 * @param alias the mailbox name to where the task is sent
761 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
763 * @return Returns #MSG_OK if the task was successfully sent,
764 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
766 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
768 msg_error_t ret = MSG_OK;
769 simdata_task_t t_simdata = nullptr;
770 msg_process_t process = MSG_process_self();
771 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
773 TRACE_msg_task_put_start(task);
775 /* Prepare the task to send */
776 t_simdata = task->simdata;
777 t_simdata->sender = process;
778 t_simdata->source = MSG_host_self();
780 t_simdata->setUsed();
782 t_simdata->comm = nullptr;
783 msg_global->sent_msg++;
785 /* Try to send it by calling SIMIX network layer */
787 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call */
788 comm = simcall_comm_isend(SIMIX_process_self(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
789 sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
790 if (TRACE_is_enabled() && task->category != nullptr)
791 simcall_set_category(comm, task->category);
792 t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
793 simcall_comm_wait(comm, timeout);
794 } catch (simgrid::TimeoutError& e) {
798 switch (e.category) {
800 ret = MSG_HOST_FAILURE;
803 ret = MSG_TRANSFER_FAILURE;
809 /* If the send failed, it is not used anymore */
810 t_simdata->setNotUsed();
813 TRACE_msg_task_put_end();
817 /** @ingroup msg_task_usage
818 * @brief Sends a task to a mailbox with a timeout and with a maximum rate
820 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
822 * The maxrate parameter can be used to send a task with a limited
823 * bandwidth (smaller than the physical available value). Use
824 * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
825 * value do disable this feature).
827 * @param task the task to be sent
828 * @param alias the mailbox name to where the task is sent
829 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
830 * @param maxrate the maximum communication rate for sending this task (byte/sec)
832 * @return Returns #MSG_OK if the task was successfully sent,
833 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
835 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
837 task->simdata->rate = maxrate;
838 return MSG_task_send_with_timeout(task, alias, timeout);
841 /** @ingroup msg_task_usage
842 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
844 * @param alias the name of the mailbox to be considered
846 * @return Returns the PID of sender process,
847 * -1 if there is no communication in the mailbox.
849 int MSG_task_listen_from(const char *alias)
851 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(alias);
852 simgrid::kernel::activity::CommImplPtr comm =
853 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
858 return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
861 /** @ingroup msg_task_usage
862 * @brief Sets the tracing category of a task.
864 * This function should be called after the creation of a MSG task, to define the category of that task. The
865 * first parameter task must contain a task that was created with the function #MSG_task_create. The second
866 * parameter category must contain a category that was previously declared with the function #TRACE_category
867 * (or with #TRACE_category_with_color).
869 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
871 * @param task the task that is going to be categorized
872 * @param category the name of the category to be associated to the task
874 * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
876 void MSG_task_set_category (msg_task_t task, const char *category)
878 TRACE_msg_set_task_category (task, category);
881 /** @ingroup msg_task_usage
883 * @brief Gets the current tracing category of a task.
885 * @param task the task to be considered
887 * @see MSG_task_set_category
889 * @return Returns the name of the tracing category of the given task, nullptr otherwise
891 const char *MSG_task_get_category (msg_task_t task)
893 return task->category;