Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid
[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 <simgrid/forward.h>
10 #include <simgrid/s4u/Activity.hpp>
11
12 #include <atomic>
13 #include <vector>
14
15 namespace simgrid {
16 namespace s4u {
17 /** @brief Communication async
18  *
19  * Represents all asynchronous communications, that you can test or wait onto.
20  */
21 class XBT_PUBLIC Comm : public Activity {
22   Comm() : Activity() {}
23 public:
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 simgrid::s4u::Mailbox; // Factory of comms
27
28   virtual ~Comm();
29
30   static simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> on_sender_start;
31   static simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> on_receiver_start;
32   static simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> on_completion;
33
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);
39
40   /*! take a vector s4u::CommPtr and return when all of them is finished. */
41   static void wait_all(std::vector<CommPtr>* comms);
42   /*! take a vector s4u::CommPtr and return the rank of the first finished one (or -1 if none is done). */
43   static int test_any(std::vector<CommPtr> * comms);
44
45   Comm* start() override;
46   Comm* wait() override;
47   Comm* wait_for(double timeout) override;
48   bool test() override;
49
50   /** Start the comm, and ignore its result. It can be completely forgotten after that. */
51   Comm* detach();
52   /** Start the comm, and ignore its result. It can be completely forgotten after that. */
53   Comm* detach(void (*clean_function)(void*))
54   {
55     clean_fun_ = clean_function;
56     return detach();
57   }
58
59   /** Sets the maximal communication rate (in byte/sec). Must be done before start */
60   Comm* set_rate(double rate);
61
62   /** Specify the data to send.
63    *
64    * This is way will get actually copied over to the receiver.
65    * That's completely unrelated from the simulated size (given with @ref Activity::set_remaining()):
66    * you can send a short buffer in your simulator, that represents a very large message
67    * in the simulated world, or the opposite.
68    */
69   Comm* set_src_data(void* buff);
70   /** Specify the size of the data to send. Not to be mixed with @ref Activity::set_remaining()
71    *
72    * That's the size of the data to actually copy in the simulator (ie, the data passed with Activity::set_src_data()).
73    * That's completely unrelated from the simulated size (given with @ref Activity::set_remaining()):
74    * you can send a short buffer in your simulator, that represents a very large message
75    * in the simulated world, or the opposite.
76    */
77   Comm* set_src_data_size(size_t size);
78   /** Specify the data to send and its size. Don't mix the size with @ref Activity::set_remaining()
79    *
80    * This is way will get actually copied over to the receiver.
81    * That's completely unrelated from the simulated size (given with @ref Activity::set_remaining()):
82    * you can send a short buffer in your simulator, that represents a very large message
83    * in the simulated world, or the opposite.
84    */
85   Comm* set_src_data(void* buff, size_t size);
86
87   /** Specify where to receive the data.
88    *
89    * That's a buffer where the sent data will be copied */
90   Comm* set_dst_data(void** buff);
91   /** Specify the buffer in which the data should be received
92    *
93    * That's a buffer where the sent data will be copied  */
94   Comm* set_dst_data(void** buff, size_t size);
95   /** Retrieve the size of the received data. Not to be mixed with @ref Activity::set_remaining()  */
96   size_t get_dst_data_size();
97
98   Comm* cancel() override;
99
100   /** Retrieve the mailbox on which this comm acts */
101   MailboxPtr get_mailbox();
102
103 #ifndef DOXYGEN
104   XBT_ATTRIB_DEPRECATED_v324("Please use Comm::wait_for()") void wait(double t) override { wait_for(t); }
105   XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_rate()") Activity* setRate(double rate) { return set_rate(rate); }
106   XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_src_data()") Activity* setSrcData(void* buff)
107   {
108     return set_src_data(buff);
109   }
110   XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_src_data()") Activity* setSrcData(void* buff, size_t size)
111   {
112     return set_src_data(buff, size);
113   }
114   XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_src_data_size()") Activity* setSrcDataSize(size_t size)
115   {
116     return set_src_data_size(size);
117   }
118   XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_dst_data()") Activity* setDstData(void** buff)
119   {
120     return set_dst_data(buff);
121   }
122   XBT_ATTRIB_DEPRECATED_v323("Please use Comm::set_dst_data()") Activity* setDstData(void** buff, size_t size)
123   {
124     return set_dst_data(buff, size);
125   }
126   XBT_ATTRIB_DEPRECATED_v323("Please use Comm::get_dst_data_size()") size_t getDstDataSize()
127   {
128     return get_dst_data_size();
129   }
130   XBT_ATTRIB_DEPRECATED_v323("Please use Comm::get_mailbox()") MailboxPtr getMailbox() { return get_mailbox(); }
131 #endif
132
133 private:
134   double rate_        = -1;
135   void* dst_buff_       = nullptr;
136   size_t dst_buff_size_ = 0;
137   void* src_buff_       = nullptr;
138   size_t src_buff_size_ = sizeof(void*);
139
140   /* FIXME: expose these elements in the API */
141   int detached_ = 0;
142   int (*match_fun_)(void*, void*, simgrid::kernel::activity::CommImpl*) = nullptr;
143   void (*clean_fun_)(void*)                                             = nullptr;
144   void (*copy_data_function_)(smx_activity_t, void*, size_t)            = nullptr;
145
146   smx_actor_t sender_   = nullptr;
147   smx_actor_t receiver_ = nullptr;
148   MailboxPtr mailbox_   = nullptr;
149
150   std::atomic_int_fast32_t refcount_{0};
151 };
152 } // namespace s4u
153 } // namespace simgrid
154
155 #endif /* SIMGRID_S4U_COMM_HPP */