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
11 #include <simgrid/forward.h>
12 #include <simgrid/s4u/Activity.hpp>
13 #include <simgrid/s4u/Mailbox.hpp> // DEPRECATED 3.20
14 #include <simgrid/s4u/forward.hpp>
20 /** @brief Communication async
22 * Represents all asynchronous communications, that you can test or wait onto.
24 XBT_PUBLIC_CLASS Comm : public Activity
26 Comm() : Activity() {}
28 friend XBT_PUBLIC void intrusive_ptr_release(simgrid::s4u::Comm * c);
29 friend XBT_PUBLIC void intrusive_ptr_add_ref(simgrid::s4u::Comm * c);
30 friend Mailbox; // Factory of comms
34 /*! take a vector s4u::CommPtr and return when one of them is finished.
35 * The return value is the rank of the first finished CommPtr. */
36 static int wait_any(std::vector<CommPtr> * comms) { return wait_any_for(comms, -1); }
37 /*! Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
38 static int wait_any_for(std::vector<CommPtr> * comms_in, double timeout)
40 // Map to dynar<Synchro*>:
41 xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), [](void*ptr){
42 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
44 for (auto const& comm : *comms_in) {
45 if (comm->state_ == inited)
47 xbt_assert(comm->state_ == started);
48 simgrid::kernel::activity::ActivityImpl* ptr = comm->pimpl_.get();
49 intrusive_ptr_add_ref(ptr);
50 xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, ptr);
52 // Call the underlying simcall:
53 int idx = simcall_comm_waitany(comms, timeout);
54 xbt_dynar_free(&comms);
58 /*! take a vector s4u::CommPtr and return when all of them is finished. */
59 static void wait_all(std::vector<CommPtr> * comms)
61 // TODO: this should be a simcall or something
62 // TODO: we are missing a version with timeout
63 for (CommPtr comm : *comms) {
67 /*! take a vector s4u::CommPtr and return the rank of the first finished one (or -1 if none is done). */
68 static int test_any(std::vector<CommPtr> * comms);
70 /** Creates (but don't start) an async send to the mailbox @p dest */
71 XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::put_init(): v3.20 will turn this warning into an error.") static CommPtr
72 send_init(MailboxPtr dest)
74 return dest->put_init();
76 /** Creates (but don't start) an async send to the mailbox @p dest */
77 XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::put_init(): v3.20 will turn this warning into an error.") static CommPtr
78 send_init(MailboxPtr dest, void* data, int simulatedByteAmount)
80 return dest->put_init(data, simulatedByteAmount);
82 /** Creates and start an async send to the mailbox @p dest */
83 XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::put_async(): v3.20 will turn this warning into an error.") static CommPtr
84 send_async(MailboxPtr dest, void* data, int simulatedByteAmount)
86 return dest->put_async(data, simulatedByteAmount);
88 /** Creates (but don't start) an async recv onto the mailbox @p from */
89 XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::get_init(): v3.20 will turn this warning into an error.") static CommPtr
90 recv_init(MailboxPtr from)
92 return from->get_init();
94 /** Creates and start an async recv to the mailbox @p from */
95 XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::get_async(): v3.20 will turn this warning into an error.") static CommPtr
96 recv_async(MailboxPtr from, void** data)
98 return from->get_async(data);
101 Activity* start() override;
102 Activity* wait() override;
103 Activity* wait(double timeout) override;
105 /** Start the comm, and ignore its result. It can be completely forgotten after that. */
107 /** Start the comm, and ignore its result. It can be completely forgotten after that. */
108 Activity* detach(void (*cleanFunction)(void*))
110 cleanFunction_ = cleanFunction;
114 /** Sets the maximal communication rate (in byte/sec). Must be done before start */
115 Activity* setRate(double rate);
117 /** Specify the data to send */
118 Activity* setSrcData(void* buff);
119 /** Specify the size of the data to send */
120 Activity* setSrcDataSize(size_t size);
121 /** Specify the data to send and its size */
122 Activity* setSrcData(void* buff, size_t size);
124 /** Specify where to receive the data */
125 Activity* setDstData(void** buff);
126 /** Specify the buffer in which the data should be received */
127 Activity* setDstData(void** buff, size_t size);
128 /** Retrieve the size of the received data */
129 size_t getDstDataSize();
134 /** Retrieve the mailbox on which this comm acts */
135 MailboxPtr getMailbox();
139 void* dstBuff_ = nullptr;
140 size_t dstBuffSize_ = 0;
141 void* srcBuff_ = nullptr;
142 size_t srcBuffSize_ = sizeof(void*);
144 /* FIXME: expose these elements in the API */
146 int (*matchFunction_)(void*, void*, simgrid::kernel::activity::CommImpl*) = nullptr;
147 void (*cleanFunction_)(void*) = nullptr;
148 void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
150 smx_actor_t sender_ = nullptr;
151 smx_actor_t receiver_ = nullptr;
152 MailboxPtr mailbox_ = nullptr;
154 std::atomic_int_fast32_t refcount_{0};
157 } // namespace simgrid::s4u
159 #endif /* SIMGRID_S4U_COMM_HPP */