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 (TRACE_actor_is_enabled())
39 simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->push_event("execute");
44 task->compute = boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(simcall_execution_parallel_start(
45 std::move(task->get_name()), task->hosts_.size(), task->hosts_.data(),
46 (task->flops_parallel_amount.empty() ? nullptr : task->flops_parallel_amount.data()),
47 (task->bytes_parallel_amount.empty() ? nullptr : task->bytes_parallel_amount.data()), -1.0, timeout));
48 XBT_DEBUG("Parallel execution action created: %p", task->compute.get());
49 if (task->has_tracing_category())
50 simgrid::simix::simcall([task] { task->compute->set_category(std::move(task->get_tracing_category())); });
52 comp_state = simcall_execution_wait(task->compute);
56 XBT_DEBUG("Execution task '%s' finished in state %d", task->get_cname(), (int)comp_state);
57 } catch (simgrid::HostFailureException& e) {
58 status = MSG_HOST_FAILURE;
59 } catch (simgrid::TimeoutError& e) {
61 } catch (simgrid::CancelException& e) {
62 status = MSG_TASK_CANCELED;
65 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
66 task->flops_amount = 0.0;
68 task->compute = nullptr;
70 if (TRACE_actor_is_enabled())
71 simgrid::instr::Container::by_name(instr_pid(MSG_process_self()))->get_state("ACTOR_STATE")->pop_event();
77 * @brief Receives a task from a mailbox.
79 * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
80 * for receiving tasks asynchronously.
82 * @param task a memory location for storing a #msg_task_t.
83 * @param alias name of the mailbox to receive the task from
86 * #MSG_OK if the task was successfully received,
87 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
89 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
91 return MSG_task_receive_with_timeout(task, alias, -1);
95 * @brief Receives a task from a mailbox at a given rate.
97 * @param task a memory location for storing a #msg_task_t.
98 * @param alias name of the mailbox to receive the task from
99 * @param rate limit the reception to rate bandwidth (byte/sec)
101 * The rate parameter can be used to receive a task with a limited bandwidth (smaller than the physical available
102 * value). Use MSG_task_receive() if you don't limit the rate (or pass -1 as a rate value do disable this feature).
105 * #MSG_OK if the task was successfully received,
106 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
108 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
110 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
114 * @brief Receives a task from a mailbox with a given timeout.
116 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
117 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
118 * to obtain an infinite timeout.
120 * @param task a memory location for storing a #msg_task_t.
121 * @param alias name of the mailbox to receive the task from
122 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
125 * #MSG_OK if the task was successfully received,
126 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
128 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
130 return MSG_task_receive_ext(task, alias, timeout, nullptr);
134 * @brief Receives a task from a mailbox with a given timeout and at a given rate.
136 * @param task a memory location for storing a #msg_task_t.
137 * @param alias name of the mailbox to receive the task from
138 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
139 * @param rate limit the reception to rate bandwidth (byte/sec)
141 * The rate parameter can be used to send a task with a limited
142 * bandwidth (smaller than the physical available value). Use
143 * MSG_task_receive() if you don't limit the rate (or pass -1 as a
144 * rate value do disable this feature).
147 * #MSG_OK if the task was successfully received,
148 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
150 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t* task, const char* alias, double timeout, double rate)
152 return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
156 * @brief Receives a task from a mailbox from a specific host with a given timeout.
158 * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
159 * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
160 * to obtain an infinite timeout.
162 * @param task a memory location for storing a #msg_task_t.
163 * @param alias name of the mailbox to receive the task from
164 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
165 * @param host a #msg_host_t host from where the task was sent
168 * #MSG_OK if the task was successfully received,
169 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
171 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
173 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
174 return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
178 * @brief Receives a task from a mailbox from a specific host with a given timeout and at a given rate.
180 * @param task a memory location for storing a #msg_task_t.
181 * @param alias name of the mailbox to receive the task from
182 * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
183 * @param host a #msg_host_t host from where the task was sent
184 * @param rate limit the reception to rate bandwidth (byte/sec)
186 * The rate parameter can be used to receive a task with a limited bandwidth (smaller than the physical available
187 * value). Use MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a rate value do disable this feature).
190 * #MSG_OK if the task was successfully received,
191 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
193 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
196 XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
197 msg_error_t ret = MSG_OK;
198 /* We no longer support getting a task from a specific host */
203 xbt_assert(task, "Null pointer for the task storage");
206 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
208 /* Try to receive it by calling SIMIX network layer */
211 simgrid::s4u::Mailbox::by_name(alias)
213 ->set_dst_data(&payload, sizeof(msg_task_t*))
216 *task = static_cast<msg_task_t>(payload);
217 XBT_DEBUG("Got task %s from %s", (*task)->get_cname(), alias);
218 (*task)->set_not_used();
219 } catch (simgrid::HostFailureException& e) {
220 ret = MSG_HOST_FAILURE;
221 } catch (simgrid::TimeoutError& e) {
223 } catch (simgrid::CancelException& e) {
224 ret = MSG_TASK_CANCELED;
225 } catch (xbt_ex& e) {
226 if (e.category == network_error)
227 ret = MSG_TRANSFER_FAILURE;
232 if (TRACE_actor_is_enabled() && ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
233 container_t process_container = simgrid::instr::Container::by_name(instr_pid(MSG_process_self()));
235 std::string key = std::string("p") + std::to_string((*task)->get_id());
236 simgrid::instr::Container::get_root()->get_link("ACTOR_TASK_LINK")->end_event(process_container, "SR", key);
243 * @brief Starts listening for receiving a task from an asynchronous communication.
245 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
247 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
248 * @param name of the mailbox to receive the task on
249 * @return the msg_comm_t communication created
251 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
253 return MSG_task_irecv_bounded(task, name, -1.0);
257 * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
259 * The rate parameter can be used to receive a task with a limited
260 * bandwidth (smaller than the physical available value). Use
261 * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
262 * value do disable this feature).
264 * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
265 * @param name of the mailbox to receive the task on
266 * @param rate limit the bandwidth to the given rate (byte/sec)
267 * @return the msg_comm_t communication created
269 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
271 simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
273 /* FIXME: these functions are not traceable */
275 xbt_assert(task, "Null pointer for the task storage");
278 XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
280 /* Try to receive it by calling SIMIX network layer */
281 simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name(name)
283 ->set_dst_data((void**)task, sizeof(msg_task_t*))
287 return new simgrid::msg::Comm(nullptr, task, comm);
291 * @brief Checks whether a communication is done, and if yes, finalizes it.
292 * @param comm the communication to test
293 * @return 'true' if the communication is finished
294 * (but it may have failed, use MSG_comm_get_status() to know its status)
295 * or 'false' if the communication is not finished yet
296 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
298 int MSG_comm_test(msg_comm_t comm)
300 bool finished = false;
303 finished = comm->s_comm->test();
304 if (finished && comm->task_received != nullptr) {
305 /* I am the receiver */
306 (*comm->task_received)->set_not_used();
308 } catch (simgrid::TimeoutError& e) {
309 comm->status = MSG_TIMEOUT;
311 } catch (simgrid::CancelException& e) {
312 comm->status = MSG_TASK_CANCELED;
316 if (e.category == network_error) {
317 comm->status = MSG_TRANSFER_FAILURE;
328 * @brief This function checks if a communication is finished.
329 * @param comms a vector of communications
330 * @return the position of the finished communication if any
331 * (but it may have failed, use MSG_comm_get_status() to know its status), or -1 if none is finished
333 int MSG_comm_testany(xbt_dynar_t comms)
335 int finished_index = -1;
337 /* Create the equivalent array with SIMIX objects: */
338 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
339 s_comms.reserve(xbt_dynar_length(comms));
342 xbt_dynar_foreach(comms, cursor, comm) {
343 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
346 msg_error_t status = MSG_OK;
348 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
349 } catch (simgrid::TimeoutError& e) {
350 finished_index = e.value;
351 status = MSG_TIMEOUT;
352 } catch (simgrid::CancelException& e) {
353 finished_index = e.value;
354 status = MSG_TASK_CANCELED;
357 if (e.category != network_error)
359 finished_index = e.value;
360 status = MSG_TRANSFER_FAILURE;
363 if (finished_index != -1) {
364 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
365 /* the communication is finished */
366 comm->status = status;
368 if (status == MSG_OK && comm->task_received != nullptr) {
369 /* I am the receiver */
370 (*comm->task_received)->set_not_used();
374 return finished_index;
377 /** @brief Destroys the provided communication. */
378 void MSG_comm_destroy(msg_comm_t comm)
383 /** @brief Wait for the completion of a communication.
385 * It takes two parameters.
386 * @param comm the communication to wait.
387 * @param timeout Wait until the communication terminates or the timeout occurs.
388 * You can provide a -1 timeout to obtain an infinite timeout.
389 * @return msg_error_t
391 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
394 comm->s_comm->wait_for(timeout);
396 if (comm->task_received != nullptr) {
397 /* I am the receiver */
398 (*comm->task_received)->set_not_used();
401 /* FIXME: these functions are not traceable */
402 } catch (simgrid::TimeoutError& e) {
403 comm->status = MSG_TIMEOUT;
404 } catch (simgrid::CancelException& e) {
405 comm->status = MSG_TASK_CANCELED;
408 if (e.category == network_error)
409 comm->status = MSG_TRANSFER_FAILURE;
417 /** @brief This function is called by a sender and permit to wait for each communication
419 * @param comm a vector of communication
420 * @param nb_elem is the size of the comm vector
421 * @param timeout for each call of MSG_comm_wait
423 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
425 for (int i = 0; i < nb_elem; i++)
426 MSG_comm_wait(comm[i], timeout);
429 /** @brief This function waits for the first communication finished in a list.
430 * @param comms a vector of communications
431 * @return the position of the first finished communication
432 * (but it may have failed, use MSG_comm_get_status() to know its status)
434 int MSG_comm_waitany(xbt_dynar_t comms)
436 int finished_index = -1;
438 /* Create the equivalent array with SIMIX objects: */
439 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
440 s_comms.reserve(xbt_dynar_length(comms));
443 xbt_dynar_foreach(comms, cursor, comm) {
444 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl().get()));
447 msg_error_t status = MSG_OK;
449 finished_index = simcall_comm_waitany(s_comms.data(), s_comms.size(), -1);
450 } catch (simgrid::TimeoutError& e) {
451 finished_index = e.value;
452 status = MSG_TIMEOUT;
453 } catch (simgrid::CancelException& e) {
454 finished_index = e.value;
455 status = MSG_TASK_CANCELED;
458 if (e.category == network_error) {
459 finished_index = e.value;
460 status = MSG_TRANSFER_FAILURE;
466 xbt_assert(finished_index != -1, "WaitAny returned -1");
468 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
469 /* the communication is finished */
470 comm->status = status;
472 if (comm->task_received != nullptr) {
473 /* I am the receiver */
474 (*comm->task_received)->set_not_used();
477 return finished_index;
481 * @brief Returns the error (if any) that occurred during a finished communication.
482 * @param comm a finished communication
483 * @return the status of the communication, or #MSG_OK if no error occurred during the communication
485 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
490 /** @brief Get a task (#msg_task_t) from a communication
492 * @param comm the communication where to get the task
493 * @return the task from the communication
495 msg_task_t MSG_comm_get_task(msg_comm_t comm)
497 xbt_assert(comm, "Invalid parameter");
499 return comm->task_received ? *comm->task_received : comm->task_sent;
503 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
504 * @param comm the comm
505 * @param buff the data copied
506 * @param buff_size size of the buffer
508 void MSG_comm_copy_data_from_SIMIX(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
510 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
512 // notify the user callback if any
513 if (msg_global->task_copy_callback) {
514 msg_task_t task = static_cast<msg_task_t>(buff);
515 msg_global->task_copy_callback(task, comm->src_actor_->ciface(), comm->dst_actor_->ciface());
520 * @brief Sends a task to a mailbox
522 * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
523 * side if #MSG_task_receive is used).
524 * See #MSG_task_isend for sending tasks asynchronously.
526 * @param task the task to be sent
527 * @param alias the mailbox name to where the task is sent
529 * @return Returns #MSG_OK if the task was successfully sent,
530 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
532 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
534 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
535 return MSG_task_send_with_timeout(task, alias, -1);
539 * @brief Sends a task to a mailbox with a maximum rate
541 * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
542 * the application to limit the bandwidth utilization of network links when sending the task.
544 * The maxrate parameter can be used to send a task with a limited bandwidth (smaller than the physical available
545 * value). Use MSG_task_send() if you don't limit the rate (or pass -1 as a rate value do disable this feature).
547 * @param task the task to be sent
548 * @param alias the mailbox name to where the task is sent
549 * @param maxrate the maximum communication rate for sending this task (byte/sec)
551 * @return Returns #MSG_OK if the task was successfully sent,
552 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
554 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
556 task->set_rate(maxrate);
557 return MSG_task_send(task, alias);
561 * @brief Sends a task to a mailbox with a timeout
563 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
565 * @param task the task to be sent
566 * @param alias the mailbox name to where the task is sent
567 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
569 * @return Returns #MSG_OK if the task was successfully sent,
570 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
572 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
574 msg_error_t ret = MSG_OK;
577 simgrid::s4u::CommPtr comm = task->send_async(alias, nullptr, false);
579 comm->wait_for(timeout);
580 } catch (simgrid::TimeoutError& e) {
582 } catch (simgrid::CancelException& e) {
583 ret = MSG_HOST_FAILURE;
584 } catch (xbt_ex& e) {
585 if (e.category == network_error)
586 ret = MSG_TRANSFER_FAILURE;
590 /* If the send failed, it is not used anymore */
591 task->set_not_used();
598 * @brief Sends a task to a mailbox with a timeout and with a maximum rate
600 * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
602 * The maxrate parameter can be used to send a task with a limited bandwidth (smaller than the physical available
603 * value). Use MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate value do disable this
606 * @param task the task to be sent
607 * @param alias the mailbox name to where the task is sent
608 * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
609 * @param maxrate the maximum communication rate for sending this task (byte/sec)
611 * @return Returns #MSG_OK if the task was successfully sent,
612 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
614 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
616 task->set_rate(maxrate);
617 return MSG_task_send_with_timeout(task, alias, timeout);
621 * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
623 * @param alias the name of the mailbox to be considered
625 * @return Returns the PID of sender process,
626 * -1 if there is no communication in the mailbox.#include <cmath>
629 int MSG_task_listen_from(const char *alias)
631 /* looks inside the rdv directly. Not clean. */
632 simgrid::kernel::activity::CommImplPtr comm = simgrid::s4u::Mailbox::by_name(alias)->front();
634 if (comm && comm->src_actor_)
635 return comm->src_actor_->get_pid();