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_ == inited)
49 xbt_assert(comm->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* setRate(double rate);
88 /** Specify the data to send */
89 Activity* setSrcData(void* buff);
90 /** Specify the size of the data to send */
91 Activity* setSrcDataSize(size_t size);
92 /** Specify the data to send and its size */
93 Activity* setSrcData(void* buff, size_t size);
95 /** Specify where to receive the data */
96 Activity* setDstData(void** buff);
97 /** Specify the buffer in which the data should be received */
98 Activity* setDstData(void** buff, size_t size);
99 /** Retrieve the size of the received data */
100 size_t getDstDataSize();
105 /** Retrieve the mailbox on which this comm acts */
106 MailboxPtr getMailbox();
110 void* dstBuff_ = nullptr;
111 size_t dstBuffSize_ = 0;
112 void* srcBuff_ = nullptr;
113 size_t srcBuffSize_ = sizeof(void*);
115 /* FIXME: expose these elements in the API */
117 int (*matchFunction_)(void*, void*, simgrid::kernel::activity::CommImpl*) = nullptr;
118 void (*cleanFunction_)(void*) = nullptr;
119 void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
121 smx_actor_t sender_ = nullptr;
122 smx_actor_t receiver_ = nullptr;
123 MailboxPtr mailbox_ = nullptr;
125 std::atomic_int_fast32_t refcount_{0};
128 } // namespace simgrid::s4u
130 #endif /* SIMGRID_S4U_COMM_HPP */