Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
230e30efdc25fa7f77a61012aabbebcf4dcb5828
[simgrid.git] / include / simgrid / s4u / Comm.hpp
1 /* Copyright (c) 2006-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_S4U_COMM_HPP
7 #define SIMGRID_S4U_COMM_HPP
8
9 #include <xbt/base.h>
10
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>
15
16 #include <vector>
17
18 namespace simgrid {
19 namespace s4u {
20 /** @brief Communication async
21  *
22  * Represents all asynchronous communications, that you can test or wait onto.
23  */
24 class XBT_PUBLIC Comm : public Activity {
25   Comm() : Activity() {}
26 public:
27   friend XBT_PUBLIC void intrusive_ptr_release(simgrid::s4u::Comm * c);
28   friend XBT_PUBLIC void intrusive_ptr_add_ref(simgrid::s4u::Comm * c);
29   friend Mailbox; // Factory of comms
30
31   virtual ~Comm();
32
33   /*! take a vector s4u::CommPtr and return when one of them is finished.
34    * The return value is the rank of the first finished CommPtr. */
35   static int wait_any(std::vector<CommPtr> * comms) { return wait_any_for(comms, -1); }
36   /*! Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
37   static int wait_any_for(std::vector<CommPtr> * comms_in, double timeout)
38   {
39     // Map to dynar<Synchro*>:
40     xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), [](void*ptr){
41       intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
42     });
43     for (auto const& comm : *comms_in) {
44       if (comm->state_ == inited)
45         comm->start();
46       xbt_assert(comm->state_ == started);
47       simgrid::kernel::activity::ActivityImpl* ptr = comm->pimpl_.get();
48       intrusive_ptr_add_ref(ptr);
49       xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, ptr);
50     }
51     // Call the underlying simcall:
52     int idx = simcall_comm_waitany(comms, timeout);
53     xbt_dynar_free(&comms);
54     return idx;
55   }
56
57   /*! take a vector s4u::CommPtr and return when all of them is finished. */
58   static void wait_all(std::vector<CommPtr> * comms)
59   {
60     // TODO: this should be a simcall or something
61     // TODO: we are missing a version with timeout
62     for (CommPtr comm : *comms) {
63       comm->wait();
64     }
65   }
66   /*! take a vector s4u::CommPtr and return the rank of the first finished one (or -1 if none is done). */
67   static int test_any(std::vector<CommPtr> * comms);
68
69   /** Creates (but don't start) an async send to the mailbox @p dest */
70   XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::put_init(): v3.20 will turn this warning into an error.") static CommPtr
71   send_init(MailboxPtr dest)
72   {
73     return dest->put_init();
74   }
75   /** Creates (but don't start) an async send to the mailbox @p dest */
76   XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::put_init(): v3.20 will turn this warning into an error.") static CommPtr
77   send_init(MailboxPtr dest, void* data, int simulatedByteAmount)
78   {
79     return dest->put_init(data, simulatedByteAmount);
80   }
81   /** Creates and start an async send to the mailbox @p dest */
82   XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::put_async(): v3.20 will turn this warning into an error.") static CommPtr
83   send_async(MailboxPtr dest, void* data, int simulatedByteAmount)
84   {
85     return dest->put_async(data, simulatedByteAmount);
86   }
87   /** Creates (but don't start) an async recv onto the mailbox @p from */
88   XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::get_init(): v3.20 will turn this warning into an error.") static CommPtr
89   recv_init(MailboxPtr from)
90   {
91     return from->get_init();
92   }
93   /** Creates and start an async recv to the mailbox @p from */
94   XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::get_async(): v3.20 will turn this warning into an error.") static CommPtr
95   recv_async(MailboxPtr from, void** data)
96   {
97     return from->get_async(data);
98   }
99
100   Activity* start() override;
101   Activity* wait() override;
102   Activity* wait(double timeout) override;
103
104   /** Start the comm, and ignore its result. It can be completely forgotten after that. */
105   Activity* detach();
106   /** Start the comm, and ignore its result. It can be completely forgotten after that. */
107   Activity* detach(void (*cleanFunction)(void*))
108   {
109     cleanFunction_ = cleanFunction;
110     return detach();
111   }
112
113   /** Sets the maximal communication rate (in byte/sec). Must be done before start */
114   Activity* setRate(double rate);
115
116   /** Specify the data to send */
117   Activity* setSrcData(void* buff);
118   /** Specify the size of the data to send */
119   Activity* setSrcDataSize(size_t size);
120   /** Specify the data to send and its size */
121   Activity* setSrcData(void* buff, size_t size);
122
123   /** Specify where to receive the data */
124   Activity* setDstData(void** buff);
125   /** Specify the buffer in which the data should be received */
126   Activity* setDstData(void** buff, size_t size);
127   /** Retrieve the size of the received data */
128   size_t getDstDataSize();
129
130   bool test();
131   Activity* cancel();
132
133   /** Retrieve the mailbox on which this comm acts */
134   MailboxPtr getMailbox();
135
136 private:
137   double rate_        = -1;
138   void* dstBuff_      = nullptr;
139   size_t dstBuffSize_ = 0;
140   void* srcBuff_      = nullptr;
141   size_t srcBuffSize_ = sizeof(void*);
142
143   /* FIXME: expose these elements in the API */
144   int detached_ = 0;
145   int (*matchFunction_)(void*, void*, simgrid::kernel::activity::CommImpl*) = nullptr;
146   void (*cleanFunction_)(void*) = nullptr;
147   void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
148
149   smx_actor_t sender_   = nullptr;
150   smx_actor_t receiver_ = nullptr;
151   MailboxPtr mailbox_   = nullptr;
152
153   std::atomic_int_fast32_t refcount_{0};
154 };
155 }
156 } // namespace simgrid::s4u
157
158 #endif /* SIMGRID_S4U_COMM_HPP */