Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #200 from Takishipp/clear_fct
[simgrid.git] / include / simgrid / s4u / Comm.hpp
1 /* Copyright (c) 2006-2017. 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 XBT_PUBLIC_CLASS Comm : public Activity
25 {
26   Comm() : Activity() {}
27 public:
28   friend void intrusive_ptr_release(simgrid::s4u::Comm * c);
29   friend void intrusive_ptr_add_ref(simgrid::s4u::Comm * c);
30   friend Mailbox; // Factory of comms
31
32   virtual ~Comm();
33
34   /*! take a range of s4u::CommPtr (last excluded) and return when one of them is finished. The return value is an
35    * iterator on the finished Comms. */
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     // 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);
43     });
44     for (auto comm : *comms_in) {
45       if (comm->state_ == inited)
46         comm->start();
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);
51     }
52     // Call the underlying simcall:
53     int idx = simcall_comm_waitany(comms, timeout);
54     xbt_dynar_free(&comms);
55     return idx;
56   }
57   /** Creates (but don't start) an async send to the mailbox @p dest */
58   static CommPtr XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::put_init(): v3.20 will remove Comm::send_init() completely.")
59       send_init(MailboxPtr dest)
60   {
61     return dest->put_init();
62   }
63   /** Creates (but don't start) an async send to the mailbox @p dest */
64   static CommPtr XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::put_init(): v3.20 will remove Comm::send_init() completely.")
65       send_init(MailboxPtr dest, void* data, int simulatedByteAmount)
66   {
67     return dest->put_init(data, simulatedByteAmount);
68   }
69   /** Creates and start an async send to the mailbox @p dest */
70   static CommPtr XBT_ATTRIB_DEPRECATED_v320(
71       "Use Mailbox::put_async(): v3.20 will remove Comm::send_async() completely.")
72       send_async(MailboxPtr dest, void* data, int simulatedByteAmount)
73   {
74     return dest->put_async(data, simulatedByteAmount);
75   }
76   /** Creates (but don't start) an async recv onto the mailbox @p from */
77   static CommPtr XBT_ATTRIB_DEPRECATED_v320("Use Mailbox::get_init(): v3.20 will remove Comm::recv_init() completely.")
78       recv_init(MailboxPtr from)
79   {
80     return from->get_init();
81   }
82   /** Creates and start an async recv to the mailbox @p from */
83   static CommPtr XBT_ATTRIB_DEPRECATED_v320(
84       "Use Mailbox::get_async(): v3.20 will remove Comm::recv_async() completely.")
85       recv_async(MailboxPtr from, void** data)
86   {
87     return from->get_async(data);
88   }
89
90   void start() override;
91   void wait() override;
92   void wait(double timeout) override;
93
94   /** Start the comm, and ignore its result. It can be completely forgotten after that. */
95   void detach();
96
97   /** Sets the maximal communication rate (in byte/sec). Must be done before start */
98   void setRate(double rate);
99
100   /** Specify the data to send */
101   void setSrcData(void* buff);
102   /** Specify the size of the data to send */
103   void setSrcDataSize(size_t size);
104   /** Specify the data to send and its size */
105   void setSrcData(void* buff, size_t size);
106
107   /** Specify where to receive the data */
108   void setDstData(void** buff);
109   /** Specify the buffer in which the data should be received */
110   void setDstData(void** buff, size_t size);
111   /** Retrieve the size of the received data */
112   size_t getDstDataSize();
113
114   bool test();
115   void cancel();
116
117   /** Retrieve the mailbox on which this comm acts */
118   MailboxPtr getMailbox();
119
120 private:
121   double rate_        = -1;
122   void* dstBuff_      = nullptr;
123   size_t dstBuffSize_ = 0;
124   void* srcBuff_      = nullptr;
125   size_t srcBuffSize_ = sizeof(void*);
126
127   /* FIXME: expose these elements in the API */
128   int detached_ = 0;
129   int (*matchFunction_)(void*, void*, simgrid::kernel::activity::CommImpl*) = nullptr;
130   void (*cleanFunction_)(void*) = nullptr;
131   void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
132
133   smx_actor_t sender_   = nullptr;
134   smx_actor_t receiver_ = nullptr;
135   MailboxPtr mailbox_   = nullptr;
136
137   std::atomic_int_fast32_t refcount_{0};
138 };
139 }
140 } // namespace simgrid::s4u
141
142 #endif /* SIMGRID_S4U_COMM_HPP */