Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename smx_synchro_t to smx_activity_t
[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/s4u/Activity.hpp>
12 #include <simgrid/s4u/forward.hpp>
13 #include <simgrid/s4u/mailbox.hpp>
14 #include <simgrid/forward.h>
15
16
17 namespace simgrid {
18 namespace s4u {
19
20
21 /** @brief Communication async
22  *
23  * Represents all asynchronous communications, that you can test or wait onto.
24  */
25 XBT_PUBLIC_CLASS Comm : public Activity {
26   Comm() : Activity() {}
27 public:
28   ~Comm() override;
29
30 public:
31   
32   /*! take 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. */
33   template<class I> static
34   I wait_any(I first, I last)
35   {
36     // Map to dynar<Synchro*>:
37     xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), NULL);
38     for(I iter = first; iter != last; iter++) {
39       Comm& comm = **iter;
40       if (comm.state_ == inited)
41         comm.start();
42       xbt_assert(comm.state_ == started);
43       xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, comm.pimpl_);
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
58   I wait_any_for(I first, I last, double timeout)
59   {
60     // Map to dynar<Synchro*>:
61     xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), NULL);
62     for(I iter = first; iter != last; iter++) {
63       Comm& comm = **iter;
64       if (comm.state_ == inited)
65         comm.start();
66       xbt_assert(comm.state_ == started);
67       xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, comm.pimpl_);
68     }
69     // Call the underlying simcall:
70     int idx = simcall_comm_waitany(comms, timeout);
71     xbt_dynar_free(&comms);
72     // Not found:
73     if (idx == -1)
74       return last;
75     // Lift the index to the corresponding iterator:
76     auto res = std::next(first, idx);
77     (*res)->state_ = finished;
78     return res;
79   }
80   /** Creates (but don't start) an async send to the mailbox @p dest */
81   static Comm &send_init(Mailbox &dest);
82   /** Creates and start an async send to the mailbox @p dest */
83   static Comm &send_async(Mailbox &dest, void *data, int simulatedByteAmount);
84     /** Creates (but don't start) an async recv onto the mailbox @p from */
85   static Comm &recv_init(Mailbox &from);
86   /** Creates and start an async recv to the mailbox @p from */
87   static Comm &recv_async(Mailbox &from, void **data);
88
89   void start() override;
90   void wait() override;
91   void wait(double timeout) override;
92
93   /** Sets the maximal communication rate (in byte/sec). Must be done before start */
94   void setRate(double rate);
95
96   /** Specify the data to send */
97   void setSrcData(void * buff);
98   /** Specify the size of the data to send */
99   void setSrcDataSize(size_t size);
100   /** Specify the data to send and its size */
101   void setSrcData(void * buff, size_t size);
102
103   /** Specify where to receive the data */
104   void setDstData(void ** buff);
105   /** Specify the buffer in which the data should be received */
106   void setDstData(void ** buff, size_t size);
107   /** Retrieve the size of the received data */
108   size_t getDstDataSize();
109
110   bool test();
111
112
113 private:
114   double rate_ = -1;
115   void *dstBuff_ = nullptr;
116   size_t dstBuffSize_ = 0;
117   void *srcBuff_ = nullptr;
118   size_t srcBuffSize_ = sizeof(void*);
119
120   /* FIXME: expose these elements in the API */
121   int detached_ = 0;
122   int (*matchFunction_)(void *, void *, smx_activity_t) = nullptr;
123   void (*cleanFunction_)(void *) = nullptr;
124   void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
125
126   smx_process_t sender_ = nullptr;
127   smx_process_t receiver_ = nullptr;
128   Mailbox *mailbox_ = nullptr;
129 };
130
131 }} // namespace simgrid::s4u
132
133 #endif /* SIMGRID_S4U_COMM_HPP */