Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try to get the refcounting right on waitany simcalls
[simgrid.git] / include / simgrid / s4u / Comm.hpp
1 /* Copyright (c) 2006-2016. 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/forward.hpp>
14 namespace simgrid {
15 namespace s4u {
16 /** @brief Communication async
17  *
18  * Represents all asynchronous communications, that you can test or wait onto.
19  */
20 XBT_PUBLIC_CLASS Comm : public Activity
21 {
22   Comm() : Activity() {}
23 public:
24   friend void intrusive_ptr_release(simgrid::s4u::Comm * c);
25   friend void intrusive_ptr_add_ref(simgrid::s4u::Comm * c);
26
27   virtual ~Comm();
28
29   /*! take a range of s4u::Comm* (last excluded) and return when one of them is finished. The return value is an
30    * iterator on the finished Comms. */
31   template <class I> static I wait_any(I first, I last)
32   {
33     // Map to dynar<Synchro*>:
34     xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), [](void*ptr){
35       intrusive_ptr_release((simgrid::kernel::activity::ActivityImpl*)ptr);
36     });
37     for (I iter = first; iter != last; iter++) {
38       Comm& comm = **iter;
39       if (comm.state_ == inited)
40         comm.start();
41       xbt_assert(comm.state_ == started);
42       intrusive_ptr_add_ref(comm.pimpl_.get());
43       xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, comm.pimpl_.get());
44     }
45     // Call the underlying simcall:
46     int idx = simcall_comm_waitany(comms, -1);
47     xbt_dynar_free(&comms);
48     // Not found:
49     if (idx == -1)
50       return last;
51     // Lift the index to the corresponding iterator:
52     auto res       = std::next(first, idx);
53     (*res)->state_ = finished;
54     return res;
55   }
56   /*! Same as wait_any, but with a timeout. If wait_any_for return because of the timeout last is returned.*/
57   template <class I> static I wait_any_for(I first, I last, double timeout)
58   {
59     // Map to dynar<Synchro*>:
60     xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), [](void*ptr){
61       intrusive_ptr_release((simgrid::kernel::activity::ActivityImpl*)ptr);
62     });
63     for (I iter = first; iter != last; iter++) {
64       Comm& comm = **iter;
65       if (comm.state_ == inited)
66         comm.start();
67       xbt_assert(comm.state_ == started);
68       intrusive_ptr_add_ref(comm.pimpl_.get());
69       xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, comm.pimpl_.get());
70     }
71     // Call the underlying simcall:
72     int idx = simcall_comm_waitany(comms, timeout);
73     xbt_dynar_free(&comms);
74     // Not found:
75     if (idx == -1)
76       return last;
77     // Lift the index to the corresponding iterator:
78     auto res       = std::next(first, idx);
79     (*res)->state_ = finished;
80     return res;
81   }
82   /** Creates (but don't start) an async send to the mailbox @p dest */
83   static CommPtr send_init(MailboxPtr dest);
84   /** Creates and start an async send to the mailbox @p dest */
85   static CommPtr send_async(MailboxPtr dest, void* data, int simulatedByteAmount);
86   /** Creates (but don't start) an async recv onto the mailbox @p from */
87   static CommPtr recv_init(MailboxPtr from);
88   /** Creates and start an async recv to the mailbox @p from */
89   static CommPtr recv_async(MailboxPtr from, void** data);
90   /** Creates and start a detached send to the mailbox @p dest
91    *  TODO: make it possible to detach an already created comm */
92   static void send_detached(MailboxPtr dest, void* data, int simulatedSize);
93
94   void start() override;
95   void wait() override;
96   void wait(double timeout) override;
97
98   /** Sets the maximal communication rate (in byte/sec). Must be done before start */
99   void setRate(double rate);
100
101   /** Specify the data to send */
102   void setSrcData(void* buff);
103   /** Specify the size of the data to send */
104   void setSrcDataSize(size_t size);
105   /** Specify the data to send and its size */
106   void setSrcData(void* buff, size_t size);
107
108   /** Specify where to receive the data */
109   void setDstData(void** buff);
110   /** Specify the buffer in which the data should be received */
111   void setDstData(void** buff, size_t size);
112   /** Retrieve the size of the received data */
113   size_t getDstDataSize();
114
115   bool test();
116   void cancel();
117
118 private:
119   double rate_        = -1;
120   void* dstBuff_      = nullptr;
121   size_t dstBuffSize_ = 0;
122   void* srcBuff_      = nullptr;
123   size_t srcBuffSize_ = sizeof(void*);
124
125   /* FIXME: expose these elements in the API */
126   int detached_ = 0;
127   int (*matchFunction_)(void*, void*, smx_activity_t) = nullptr;
128   void (*cleanFunction_)(void*) = nullptr;
129   void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
130
131   smx_actor_t sender_   = nullptr;
132   smx_actor_t receiver_ = nullptr;
133   MailboxPtr mailbox_   = nullptr;
134
135   std::atomic_int_fast32_t refcount_{0};
136 };
137 }
138 } // namespace simgrid::s4u
139
140 #endif /* SIMGRID_S4U_COMM_HPP */