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. */
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"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
18 * @brief Executes a parallel task and waits for its termination.
20 * @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_parallel_task_execute(msg_task_t task)
26 return MSG_parallel_task_execute_with_timeout(task, -1);
29 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
31 e_smx_state_t comp_state;
32 msg_error_t status = MSG_OK;
34 xbt_assert((not task->compute) && not task->is_used(), "This task is executed somewhere else. Go fix your code!");
36 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
38 if (task->flops_amount <= 0.0 && not task->hosts_.empty()) {
42 if (TRACE_actor_is_enabled())
43 simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->push_event("execute");
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())); });
56 comp_state = simcall_execution_wait(task->compute);
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) {
65 } catch (simgrid::CancelException& e) {
66 status = MSG_TASK_CANCELED;
69 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
70 task->flops_amount = 0.0;
72 task->compute = nullptr;
74 if (TRACE_actor_is_enabled())
75 simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->pop_event();
81 * @brief Receives a task from a mailbox.
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.
86 * @param task a memory location for storing a #msg_task_t.
87 * @param alias name of the mailbox to receive the task from
90 * #MSG_OK if the task was successfully received,
91 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
93 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
95 return MSG_task_receive_with_timeout(task, alias, -1);
99 * @brief Receives a task from a mailbox at a given rate.
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)
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).
109 * #MSG_OK if the task was successfully received,
110 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
112 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
114 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
118 * @brief Receives a task from a mailbox with a given timeout.
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.
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)
129 * #MSG_OK if the task was successfully received,
130 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
132 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
134 return MSG_task_receive_ext(task, alias, timeout, nullptr);
138 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
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)
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).
151 * #MSG_OK if the task was successfully received,
152 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
154 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t* task, const char* alias, double timeout, double rate)
156 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
160 * @brief Receives a task from a mailbox from a specific host with a given timeout.
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.
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
172 * #MSG_OK if the task was successfully received,
173 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
175 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
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);
182 * @brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
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)
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).
194 * #MSG_OK if the task was successfully received,
195 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
197 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
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 */
207 xbt_assert(task, "Null pointer for the task storage");
210 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
212 /* Try to receive it by calling SIMIX network layer */
215 simgrid::s4u::Mailbox::by_name(alias)
217 ->set_dst_data(&payload, sizeof(msg_task_t*))
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) {
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;
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()));
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);
247 * @brief Starts listening for receiving a task from an asynchronous communication.
249 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
251 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
252 * @param name of the mailbox to receive the task on
253 * @return the msg_comm_t communication created
255 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
257 return MSG_task_irecv_bounded(task, name, -1.0);
261 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
263 * The rate parameter can be used to receive a task with a limited
264 * bandwidth (smaller than the physical available value). Use
265 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
266 * value do disable this feature).
268 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
269 * @param name of the mailbox to receive the task on
270 * @param rate limit the bandwidth to the given rate (byte/sec)
271 * @return the msg_comm_t communication created
273 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
275 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
277 /* FIXME: these functions are not traceable */
279 xbt_assert(task, "Null pointer for the task storage");
282 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
284 /* Try to receive it by calling SIMIX network layer */
285 simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name(name)
287 ->set_dst_data((void**)task, sizeof(msg_task_t*))
291 return new simgrid::msg::Comm(nullptr, task, comm);
295 * @brief Checks whether a communication is done, and if yes, finalizes it.
296 * @param comm the communication to test
297 * @return 'true' if the communication is finished
298 * (but it may have failed, use MSG_comm_get_status() to know its status)
299 * or 'false' if the communication is not finished yet
300 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
302 int MSG_comm_test(msg_comm_t comm)
304 bool finished = false;
307 finished = comm->s_comm->test();
308 if (finished && comm->task_received != nullptr) {
309 /* I am the receiver */
310 (*comm->task_received)->set_not_used();
312 } catch (simgrid::TimeoutError& e) {
313 comm->status = MSG_TIMEOUT;
315 } catch (simgrid::CancelException& e) {
316 comm->status = MSG_TASK_CANCELED;
320 if (e.category == network_error) {
321 comm->status = MSG_TRANSFER_FAILURE;
332 * @brief This function checks if a communication is finished.
333 * @param comms a vector of communications
334 * @return the position of the finished communication if any
335 * (but it may have failed, use MSG_comm_get_status() to know its status), or -1 if none is finished
337 int MSG_comm_testany(xbt_dynar_t comms)
339 int finished_index = -1;
341 /* Create the equivalent array with SIMIX objects: */
342 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
343 s_comms.reserve(xbt_dynar_length(comms));
346 xbt_dynar_foreach(comms, cursor, comm) {
347 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
350 msg_error_t status = MSG_OK;
352 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
353 } catch (simgrid::TimeoutError& e) {
354 finished_index = e.value;
355 status = MSG_TIMEOUT;
356 } catch (simgrid::CancelException& e) {
357 finished_index = e.value;
358 status = MSG_TASK_CANCELED;
361 if (e.category != network_error)
363 finished_index = e.value;
364 status = MSG_TRANSFER_FAILURE;
367 if (finished_index != -1) {
368 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
369 /* the communication is finished */
370 comm->status = status;
372 if (status == MSG_OK && comm->task_received != nullptr) {
373 /* I am the receiver */
374 (*comm->task_received)->set_not_used();
378 return finished_index;
381 /** @brief Destroys the provided communication. */
382 void MSG_comm_destroy(msg_comm_t comm)
387 /** @brief Wait for the completion of a communication.
389 * It takes two parameters.
390 * @param comm the communication to wait.
391 * @param timeout Wait until the communication terminates or the timeout occurs.
392 * You can provide a -1 timeout to obtain an infinite timeout.
393 * @return msg_error_t
395 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
398 comm->s_comm->wait_for(timeout);
400 if (comm->task_received != nullptr) {
401 /* I am the receiver */
402 (*comm->task_received)->set_not_used();
405 /* FIXME: these functions are not traceable */
406 } catch (simgrid::TimeoutError& e) {
407 comm->status = MSG_TIMEOUT;
408 } catch (simgrid::CancelException& e) {
409 comm->status = MSG_TASK_CANCELED;
412 if (e.category == network_error)
413 comm->status = MSG_TRANSFER_FAILURE;
421 /** @brief This function is called by a sender and permit to wait for each communication
423 * @param comm a vector of communication
424 * @param nb_elem is the size of the comm vector
425 * @param timeout for each call of MSG_comm_wait
427 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
429 for (int i = 0; i < nb_elem; i++)
430 MSG_comm_wait(comm[i], timeout);
433 /** @brief This function waits for the first communication finished in a list.
434 * @param comms a vector of communications
435 * @return the position of the first finished communication
436 * (but it may have failed, use MSG_comm_get_status() to know its status)
438 int MSG_comm_waitany(xbt_dynar_t comms)
440 int finished_index = -1;
442 /* Create the equivalent array with SIMIX objects: */
443 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
444 s_comms.reserve(xbt_dynar_length(comms));
447 xbt_dynar_foreach(comms, cursor, comm) {
448 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
451 msg_error_t status = MSG_OK;
453 finished_index = simcall_comm_waitany(s_comms.data(), s_comms.size(), -1);
454 } catch (simgrid::TimeoutError& e) {
455 finished_index = e.value;
456 status = MSG_TIMEOUT;
457 } catch (simgrid::CancelException& e) {
458 finished_index = e.value;
459 status = MSG_TASK_CANCELED;
462 if (e.category == network_error) {
463 finished_index = e.value;
464 status = MSG_TRANSFER_FAILURE;
470 xbt_assert(finished_index != -1, "WaitAny returned -1");
472 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
473 /* the communication is finished */
474 comm->status = status;
476 if (comm->task_received != nullptr) {
477 /* I am the receiver */
478 (*comm->task_received)->set_not_used();
481 return finished_index;
485 * @brief Returns the error (if any) that occurred during a finished communication.
486 * @param comm a finished communication
487 * @return the status of the communication, or #MSG_OK if no error occurred during the communication
489 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
494 /** @brief Get a task (#msg_task_t) from a communication
496 * @param comm the communication where to get the task
497 * @return the task from the communication
499 msg_task_t MSG_comm_get_task(msg_comm_t comm)
501 xbt_assert(comm, "Invalid parameter");
503 return comm->task_received ? *comm->task_received : comm->task_sent;
507 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
508 * @param comm the comm
509 * @param buff the data copied
510 * @param buff_size size of the buffer
512 void MSG_comm_copy_data_from_SIMIX(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
514 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
516 // notify the user callback if any
517 if (msg_global->task_copy_callback) {
518 msg_task_t task = static_cast<msg_task_t>(buff);
519 msg_global->task_copy_callback(task, comm->src_actor_->ciface(), comm->dst_actor_->ciface());
524 * @brief Sends a task to a mailbox
526 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
527 * side if #MSG_task_receive is used).
528 * See #MSG_task_isend for sending tasks asynchronously.
530 * @param task the task to be sent
531 * @param alias the mailbox name to where the task is sent
533 * @return Returns #MSG_OK if the task was successfully sent,
534 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
536 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
538 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
539 return MSG_task_send_with_timeout(task, alias, -1);
543 * @brief Sends a task to a mailbox with a maximum rate
545 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
546 * the application to limit the bandwidth utilization of network links when sending the task.
548 * The maxrate parameter can be used to send a task with a limited bandwidth (smaller than the physical available
549 * value). Use MSG_task_send() if you don't limit the rate (or pass -1 as a rate value do disable this feature).
551 * @param task the task to be sent
552 * @param alias the mailbox name to where the task is sent
553 * @param maxrate the maximum communication rate for sending this task (byte/sec)
555 * @return Returns #MSG_OK if the task was successfully sent,
556 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
558 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
560 task->set_rate(maxrate);
561 return MSG_task_send(task, alias);
565 * @brief Sends a task to a mailbox with a timeout
567 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
569 * @param task the task to be sent
570 * @param alias the mailbox name to where the task is sent
571 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
573 * @return Returns #MSG_OK if the task was successfully sent,
574 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
576 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
578 msg_error_t ret = MSG_OK;
581 simgrid::s4u::CommPtr comm = task->send_async(alias, nullptr, false);
583 comm->wait_for(timeout);
584 } catch (simgrid::TimeoutError& e) {
586 } catch (simgrid::CancelException& e) {
587 ret = MSG_HOST_FAILURE;
588 } catch (xbt_ex& e) {
589 if (e.category == network_error)
590 ret = MSG_TRANSFER_FAILURE;
594 /* If the send failed, it is not used anymore */
595 task->set_not_used();
602 * @brief Sends a task to a mailbox with a timeout and with a maximum rate
604 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
606 * The maxrate parameter can be used to send a task with a limited bandwidth (smaller than the physical available
607 * value). Use MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate value do disable this
610 * @param task the task to be sent
611 * @param alias the mailbox name to where the task is sent
612 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
613 * @param maxrate the maximum communication rate for sending this task (byte/sec)
615 * @return Returns #MSG_OK if the task was successfully sent,
616 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
618 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
620 task->set_rate(maxrate);
621 return MSG_task_send_with_timeout(task, alias, timeout);
625 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
627 * @param alias the name of the mailbox to be considered
629 * @return Returns the PID of sender process,
630 * -1 if there is no communication in the mailbox.#include <cmath>
633 int MSG_task_listen_from(const char *alias)
635 /* looks inside the rdv directly. Not clean. */
636 simgrid::kernel::activity::CommImplPtr comm = simgrid::s4u::Mailbox::by_name(alias)->front();
638 if (comm && comm->src_actor_)
639 return comm->src_actor_->get_pid();