Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr/gitroot/simgrid/simgrid
[simgrid.git] / include / simgrid / s4u / comm.hpp
1 /* Copyright (c) 2006-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SIMGRID_S4U_COMM_HPP
8 #define SIMGRID_S4U_COMM_HPP
9
10 #include <xbt/base.h>
11
12 #include <simgrid/s4u/Activity.hpp>
13 #include <simgrid/s4u/forward.hpp>
14 #include <simgrid/s4u/mailbox.hpp>
15 #include <simgrid/forward.h>
16
17
18 namespace simgrid {
19 namespace s4u {
20
21
22 /** @brief Communication async
23  *
24  * Represents all asynchronous communications, that you can test or wait onto.
25  */
26 XBT_PUBLIC_CLASS Comm : public Activity {
27   Comm() : Activity() {}
28 public:
29   ~Comm() override;
30
31 public:
32   
33   /*! tanke a range of s4u::Comm* (last excluded) and return when one of them is finished. The return value is an iterator on the finished Comms. */
34   template<class I> static
35   I wait_any(I first, I last)
36   {
37     // Map to dynar<Synchro*>:
38     xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::simix::Synchro*), NULL);
39     for(I iter = first; iter != last; iter++) {
40       Comm& comm = **iter;
41       if (comm.state_ == inited)
42         comm.start();
43       xbt_assert(comm.state_ == started);
44       xbt_dynar_push_as(comms, simgrid::simix::Synchro*, comm.pimpl_);
45     }
46     // Call the underlying simcall:
47     int idx = simcall_comm_waitany(comms, -1);
48     xbt_dynar_free(&comms);
49     // Not found:
50     if (idx == -1)
51       return last;
52     // Lift the index to the corresponding iterator:
53     auto res = std::next(first, idx);
54     (*res)->state_ = finished;
55     return res;
56   }
57   /*! Same as wait_any, but with a timeout. If wait_any_for return because of the timeout last is returned.*/
58   template<class I> static
59   I wait_any_for(I first, I last, double timeout)
60   {
61     // Map to dynar<Synchro*>:
62     xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::simix::Synchro*), NULL);
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       xbt_dynar_push_as(comms, simgrid::simix::Synchro*, comm.pimpl_);
69     }
70     // Call the underlying simcall:
71     int idx = simcall_comm_waitany(comms, timeout);
72     xbt_dynar_free(&comms);
73     // Not found:
74     if (idx == -1)
75       return last;
76     // Lift the index to the corresponding iterator:
77     auto res = std::next(first, idx);
78     (*res)->state_ = finished;
79     return res;
80   }
81   /** Creates (but don't start) an async send to the mailbox @p dest */
82   static Comm &send_init(Mailbox &dest);
83   /** Creates and start an async send to the mailbox @p dest */
84   static Comm &send_async(Mailbox &dest, void *data, int simulatedByteAmount);
85     /** Creates (but don't start) an async recv onto the mailbox @p from */
86   static Comm &recv_init(Mailbox &from);
87   /** Creates and start an async recv to the mailbox @p from */
88   static Comm &recv_async(Mailbox &from, void **data);
89
90   void start() override;
91   void wait() override;
92   void wait(double timeout) override;
93
94   /** Sets the maximal communication rate (in byte/sec). Must be done before start */
95   void setRate(double rate);
96
97   /** Specify the data to send */
98   void setSrcData(void * buff);
99   /** Specify the size of the data to send */
100   void setSrcDataSize(size_t size);
101   /** Specify the data to send and its size */
102   void setSrcData(void * buff, size_t size);
103
104   /** Specify where to receive the data */
105   void setDstData(void ** buff);
106   /** Specify the buffer in which the data should be received */
107   void setDstData(void ** buff, size_t size);
108   /** Retrieve the size of the received data */
109   size_t getDstDataSize();
110
111   bool test();
112
113
114 private:
115   double rate_ = -1;
116   void *dstBuff_ = nullptr;
117   size_t dstBuffSize_ = 0;
118   void *srcBuff_ = nullptr;
119   size_t srcBuffSize_ = sizeof(void*);
120
121   /* FIXME: expose these elements in the API */
122   int detached_ = 0;
123   int (*matchFunction_)(void *, void *, smx_synchro_t) = nullptr;
124   void (*cleanFunction_)(void *) = nullptr;
125   void (*copyDataFunction_)(smx_synchro_t, void*, size_t) = nullptr;
126
127   smx_process_t sender_ = nullptr;
128   smx_process_t receiver_ = nullptr;
129   Mailbox *mailbox_ = nullptr;
130 };
131
132 }} // namespace simgrid::s4u
133
134 #endif /* SIMGRID_S4U_COMM_HPP */