Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
get sg_instr_new_router through a signal
[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*), NULL);
35     for (I iter = first; iter != last; iter++) {
36       Comm& comm = **iter;
37       if (comm.state_ == inited)
38         comm.start();
39       xbt_assert(comm.state_ == started);
40       xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, comm.pimpl_);
41     }
42     // Call the underlying simcall:
43     int idx = simcall_comm_waitany(comms, -1);
44     xbt_dynar_free(&comms);
45     // Not found:
46     if (idx == -1)
47       return last;
48     // Lift the index to the corresponding iterator:
49     auto res       = std::next(first, idx);
50     (*res)->state_ = finished;
51     return res;
52   }
53   /*! Same as wait_any, but with a timeout. If wait_any_for return because of the timeout last is returned.*/
54   template <class I> static I wait_any_for(I first, I last, double timeout)
55   {
56     // Map to dynar<Synchro*>:
57     xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), NULL);
58     for (I iter = first; iter != last; iter++) {
59       Comm& comm = **iter;
60       if (comm.state_ == inited)
61         comm.start();
62       xbt_assert(comm.state_ == started);
63       xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, comm.pimpl_);
64     }
65     // Call the underlying simcall:
66     int idx = simcall_comm_waitany(comms, timeout);
67     xbt_dynar_free(&comms);
68     // Not found:
69     if (idx == -1)
70       return last;
71     // Lift the index to the corresponding iterator:
72     auto res       = std::next(first, idx);
73     (*res)->state_ = finished;
74     return res;
75   }
76   /** Creates (but don't start) an async send to the mailbox @p dest */
77   static CommPtr send_init(MailboxPtr dest);
78   /** Creates and start an async send to the mailbox @p dest */
79   static CommPtr send_async(MailboxPtr dest, void* data, int simulatedByteAmount);
80   /** Creates (but don't start) an async recv onto the mailbox @p from */
81   static CommPtr recv_init(MailboxPtr from);
82   /** Creates and start an async recv to the mailbox @p from */
83   static CommPtr recv_async(MailboxPtr from, void** data);
84   /** Creates and start a detached send to the mailbox @p dest
85    *  TODO: make it possible to detach an already created comm */
86   static void send_detached(MailboxPtr dest, void* data, int simulatedSize);
87
88   void start() override;
89   void wait() override;
90   void wait(double timeout) override;
91
92   /** Sets the maximal communication rate (in byte/sec). Must be done before start */
93   void setRate(double rate);
94
95   /** Specify the data to send */
96   void setSrcData(void* buff);
97   /** Specify the size of the data to send */
98   void setSrcDataSize(size_t size);
99   /** Specify the data to send and its size */
100   void setSrcData(void* buff, size_t size);
101
102   /** Specify where to receive the data */
103   void setDstData(void** buff);
104   /** Specify the buffer in which the data should be received */
105   void setDstData(void** buff, size_t size);
106   /** Retrieve the size of the received data */
107   size_t getDstDataSize();
108
109   bool test();
110   void cancel();
111
112 private:
113   double rate_        = -1;
114   void* dstBuff_      = nullptr;
115   size_t dstBuffSize_ = 0;
116   void* srcBuff_      = nullptr;
117   size_t srcBuffSize_ = sizeof(void*);
118
119   /* FIXME: expose these elements in the API */
120   int detached_ = 0;
121   int (*matchFunction_)(void*, void*, smx_activity_t) = nullptr;
122   void (*cleanFunction_)(void*) = nullptr;
123   void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
124
125   smx_actor_t sender_   = nullptr;
126   smx_actor_t receiver_ = nullptr;
127   MailboxPtr mailbox_   = nullptr;
128
129   std::atomic_int_fast32_t refcount_{0};
130 };
131 }
132 } // namespace simgrid::s4u
133
134 #endif /* SIMGRID_S4U_COMM_HPP */