Logo AND Algorithmique Numérique Distribuée

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