1 /* Copyright (c) 2006-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 #ifndef SIMGRID_S4U_COMM_HPP
7 #define SIMGRID_S4U_COMM_HPP
9 #include <simgrid/forward.h>
10 #include <simgrid/s4u/Activity.hpp>
17 /** @brief Communication async
19 * Represents all asynchronous communications, that you can test or wait onto.
21 class XBT_PUBLIC Comm : public Activity {
22 Comm() : Activity() {}
24 friend XBT_PUBLIC void intrusive_ptr_release(simgrid::s4u::Comm * c);
25 friend XBT_PUBLIC void intrusive_ptr_add_ref(simgrid::s4u::Comm * c);
26 friend Mailbox; // Factory of comms
30 /*! take a vector s4u::CommPtr and return when one of them is finished.
31 * The return value is the rank of the first finished CommPtr. */
32 static int wait_any(std::vector<CommPtr> * comms) { return wait_any_for(comms, -1); }
33 /*! Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
34 static int wait_any_for(std::vector<CommPtr>* comms_in, double timeout);
36 /*! take a vector s4u::CommPtr and return when all of them is finished. */
37 static void wait_all(std::vector<CommPtr>* comms);
38 /*! take a vector s4u::CommPtr and return the rank of the first finished one (or -1 if none is done). */
39 static int test_any(std::vector<CommPtr> * comms);
41 Activity* start() override;
42 Activity* wait() override;
43 Activity* wait(double timeout) override;
45 /** Start the comm, and ignore its result. It can be completely forgotten after that. */
47 /** Start the comm, and ignore its result. It can be completely forgotten after that. */
48 Activity* detach(void (*clean_function)(void*))
50 clean_fun_ = clean_function;
54 /** Sets the maximal communication rate (in byte/sec). Must be done before start */
55 Activity* set_rate(double rate);
57 /** Specify the data to send */
58 Activity* set_src_data(void* buff);
59 /** Specify the size of the data to send */
60 Activity* set_src_data_size(size_t size);
61 /** Specify the data to send and its size */
62 Activity* set_src_data(void* buff, size_t size);
64 /** Specify where to receive the data */
65 Activity* set_dst_data(void** buff);
66 /** Specify the buffer in which the data should be received */
67 Activity* set_dst_data(void** buff, size_t size);
68 /** Retrieve the size of the received data */
69 size_t get_dst_data_size();
74 /** Retrieve the mailbox on which this comm acts */
75 MailboxPtr get_mailbox();
77 /** @deprecated See Comm::set_rate() */
78 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_rate()") Activity* setRate(double rate) { return set_rate(rate); }
79 /** @deprecated See Comm::set_src_data() */
80 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_src_data()") Activity* setSrcData(void* buff)
82 return set_src_data(buff);
84 /** @deprecated See Comm::set_src_data() */
85 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_src_data()") Activity* setSrcData(void* buff, size_t size)
87 return set_src_data(buff, size);
89 /** @deprecated See Comm::set_src_data_size() */
90 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_src_data_size()") Activity* setSrcDataSize(size_t size)
92 return set_src_data_size(size);
94 /** @deprecated See Comm::set_dst_data() */
95 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_dst_data()") Activity* setDstData(void** buff)
97 return set_dst_data(buff);
99 /** @deprecated See Comm::set_dst_data() */
100 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_dst_data()") Activity* setDstData(void** buff, size_t size)
102 return set_dst_data(buff, size);
104 /** @deprecated See Comm::get_dst_data_size() */
105 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::get_dst_data_size()") size_t getDstDataSize()
107 return get_dst_data_size();
109 /** @deprecated See Comm::get_mailbox() */
110 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::get_mailbox()") MailboxPtr getMailbox() { return get_mailbox(); }
114 void* dst_buff_ = nullptr;
115 size_t dst_buff_size_ = 0;
116 void* src_buff_ = nullptr;
117 size_t src_buff_size_ = sizeof(void*);
119 /* FIXME: expose these elements in the API */
121 int (*match_fun_)(void*, void*, simgrid::kernel::activity::CommImpl*) = nullptr;
122 void (*clean_fun_)(void*) = nullptr;
123 void (*copy_data_function_)(smx_activity_t, void*, size_t) = nullptr;
125 smx_actor_t sender_ = nullptr;
126 smx_actor_t receiver_ = nullptr;
127 MailboxPtr mailbox_ = nullptr;
129 std::atomic_int_fast32_t refcount_{0};
132 } // namespace simgrid
134 #endif /* SIMGRID_S4U_COMM_HPP */