Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change the prototype of s4u::Comm::wait_any to mimick the C code
[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
15 #include <vector>
16
17 namespace simgrid {
18 namespace s4u {
19 /** @brief Communication async
20  *
21  * Represents all asynchronous communications, that you can test or wait onto.
22  */
23 XBT_PUBLIC_CLASS Comm : public Activity
24 {
25   Comm() : Activity() {}
26 public:
27   friend void intrusive_ptr_release(simgrid::s4u::Comm * c);
28   friend void intrusive_ptr_add_ref(simgrid::s4u::Comm * c);
29
30   virtual ~Comm();
31
32   /*! take a range of s4u::CommPtr (last excluded) and return when one of them is finished. The return value is an
33    * iterator on the finished Comms. */
34   static int wait_any(std::vector<CommPtr> * comms) { return wait_any_for(comms, -1); }
35   /*! Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
36   static int wait_any_for(std::vector<CommPtr> * comms_in, 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 (auto comm : *comms_in) {
43       if (comm->state_ == inited)
44         comm->start();
45       xbt_assert(comm->state_ == started);
46       simgrid::kernel::activity::ActivityImpl* ptr = comm->pimpl_.get();
47       intrusive_ptr_add_ref(ptr);
48       xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, ptr);
49     }
50     // Call the underlying simcall:
51     int idx = simcall_comm_waitany(comms, timeout);
52     xbt_dynar_free(&comms);
53     return idx;
54   }
55   /** Creates (but don't start) an async send to the mailbox @p dest */
56   static CommPtr send_init(MailboxPtr dest);
57   /** Creates and start an async send to the mailbox @p dest */
58   static CommPtr send_async(MailboxPtr dest, void* data, int simulatedByteAmount);
59   /** Creates (but don't start) an async recv onto the mailbox @p from */
60   static CommPtr recv_init(MailboxPtr from);
61   /** Creates and start an async recv to the mailbox @p from */
62   static CommPtr recv_async(MailboxPtr from, void** data);
63   /** Creates and start a detached send to the mailbox @p dest
64    *  TODO: make it possible to detach an already created comm */
65   static void send_detached(MailboxPtr dest, void* data, int simulatedSize);
66
67   void start() override;
68   void wait() override;
69   void wait(double timeout) override;
70
71   /** Sets the maximal communication rate (in byte/sec). Must be done before start */
72   void setRate(double rate);
73
74   /** Specify the data to send */
75   void setSrcData(void* buff);
76   /** Specify the size of the data to send */
77   void setSrcDataSize(size_t size);
78   /** Specify the data to send and its size */
79   void setSrcData(void* buff, size_t size);
80
81   /** Specify where to receive the data */
82   void setDstData(void** buff);
83   /** Specify the buffer in which the data should be received */
84   void setDstData(void** buff, size_t size);
85   /** Retrieve the size of the received data */
86   size_t getDstDataSize();
87
88   bool test();
89   void cancel();
90
91 private:
92   double rate_        = -1;
93   void* dstBuff_      = nullptr;
94   size_t dstBuffSize_ = 0;
95   void* srcBuff_      = nullptr;
96   size_t srcBuffSize_ = sizeof(void*);
97
98   /* FIXME: expose these elements in the API */
99   int detached_ = 0;
100   int (*matchFunction_)(void*, void*, simgrid::kernel::activity::CommImpl*) = nullptr;
101   void (*cleanFunction_)(void*) = nullptr;
102   void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
103
104   smx_actor_t sender_   = nullptr;
105   smx_actor_t receiver_ = nullptr;
106   MailboxPtr mailbox_   = nullptr;
107
108   std::atomic_int_fast32_t refcount_{0};
109 };
110 }
111 } // namespace simgrid::s4u
112
113 #endif /* SIMGRID_S4U_COMM_HPP */