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 <xbt/asserts.h>
11 #include <xbt/dynar.h>
13 #include <simgrid/forward.h>
14 #include <simgrid/s4u/Activity.hpp>
15 #include <simgrid/s4u/forward.hpp>
16 #include <simgrid/simix.h>
23 /** @brief Communication async
25 * Represents all asynchronous communications, that you can test or wait onto.
27 class XBT_PUBLIC Comm : public Activity {
28 Comm() : Activity() {}
30 friend XBT_PUBLIC void intrusive_ptr_release(simgrid::s4u::Comm * c);
31 friend XBT_PUBLIC void intrusive_ptr_add_ref(simgrid::s4u::Comm * c);
32 friend Mailbox; // Factory of comms
36 /*! take a vector s4u::CommPtr and return when one of them is finished.
37 * The return value is the rank of the first finished CommPtr. */
38 static int wait_any(std::vector<CommPtr> * comms) { return wait_any_for(comms, -1); }
39 /*! Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
40 static int wait_any_for(std::vector<CommPtr> * comms_in, double timeout)
42 // Map to dynar<Synchro*>:
43 xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), [](void*ptr){
44 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
46 for (auto const& comm : *comms_in) {
47 if (comm->state_ == Activity::State::inited)
49 xbt_assert(comm->state_ == Activity::State::started);
50 simgrid::kernel::activity::ActivityImpl* ptr = comm->pimpl_.get();
51 intrusive_ptr_add_ref(ptr);
52 xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, ptr);
54 // Call the underlying simcall:
55 int idx = simcall_comm_waitany(comms, timeout);
56 xbt_dynar_free(&comms);
60 /*! take a vector s4u::CommPtr and return when all of them is finished. */
61 static void wait_all(std::vector<CommPtr> * comms)
63 // TODO: this should be a simcall or something
64 // TODO: we are missing a version with timeout
65 for (CommPtr comm : *comms) {
69 /*! take a vector s4u::CommPtr and return the rank of the first finished one (or -1 if none is done). */
70 static int test_any(std::vector<CommPtr> * comms);
72 Activity* start() override;
73 Activity* wait() override;
74 Activity* wait(double timeout) override;
76 /** Start the comm, and ignore its result. It can be completely forgotten after that. */
78 /** Start the comm, and ignore its result. It can be completely forgotten after that. */
79 Activity* detach(void (*cleanFunction)(void*))
81 cleanFunction_ = cleanFunction;
85 /** Sets the maximal communication rate (in byte/sec). Must be done before start */
86 Activity* set_rate(double rate);
88 /** Specify the data to send */
89 Activity* set_src_data(void* buff);
90 /** Specify the size of the data to send */
91 Activity* set_src_data_size(size_t size);
92 /** Specify the data to send and its size */
93 Activity* set_src_data(void* buff, size_t size);
95 /** Specify where to receive the data */
96 Activity* set_dst_data(void** buff);
97 /** Specify the buffer in which the data should be received */
98 Activity* set_dst_data(void** buff, size_t size);
99 /** Retrieve the size of the received data */
100 size_t get_dst_data_size();
105 /** Retrieve the mailbox on which this comm acts */
106 MailboxPtr get_mailbox();
108 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_rate()") Activity* setRate(double rate) { return set_rate(rate); }
109 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_src_data()") Activity* setSrcData(void* buff)
111 return set_src_data(buff);
113 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_src_data()") Activity* setSrcData(void* buff, size_t size)
115 return set_src_data(buff, size);
117 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_src_data_size()") Activity* setSrcDataSize(size_t size)
119 return set_src_data_size(size);
121 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_dst_data()") Activity* setDstData(void** buff)
123 return set_dst_data(buff);
125 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_dst_data()") Activity* setDstData(void** buff, size_t size)
127 return set_dst_data(buff, size);
129 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::get_dst_data_size()") size_t getDstDataSize()
131 return get_dst_data_size();
133 XBT_ATTRIB_DEPRECATED_v323("Please use Comm::get_mailbox()") MailboxPtr getMailbox() { return get_mailbox(); }
137 void* dstBuff_ = nullptr;
138 size_t dstBuffSize_ = 0;
139 void* srcBuff_ = nullptr;
140 size_t srcBuffSize_ = sizeof(void*);
142 /* FIXME: expose these elements in the API */
144 int (*matchFunction_)(void*, void*, simgrid::kernel::activity::CommImpl*) = nullptr;
145 void (*cleanFunction_)(void*) = nullptr;
146 void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
148 smx_actor_t sender_ = nullptr;
149 smx_actor_t receiver_ = nullptr;
150 MailboxPtr mailbox_ = nullptr;
152 std::atomic_int_fast32_t refcount_{0};
155 } // namespace simgrid::s4u
157 #endif /* SIMGRID_S4U_COMM_HPP */