Logo AND Algorithmique Numérique Distribuée

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