Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill features marked deprecated until v3.20.
[simgrid.git] / include / simgrid / s4u / Comm.hpp
1 /* Copyright (c) 2006-2018. 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/asserts.h>
10 #include <xbt/base.h>
11 #include <xbt/dynar.h>
12
13 #include <simgrid/forward.h>
14 #include <simgrid/s4u/Activity.hpp>
15 #include <simgrid/s4u/forward.hpp>
16 #include <simgrid/simix.h>
17
18 #include <atomic>
19 #include <vector>
20
21 namespace simgrid {
22 namespace s4u {
23 /** @brief Communication async
24  *
25  * Represents all asynchronous communications, that you can test or wait onto.
26  */
27 class XBT_PUBLIC Comm : public Activity {
28   Comm() : Activity() {}
29 public:
30   friend XBT_PUBLIC void intrusive_ptr_release(simgrid::s4u::Comm * c);
31   friend XBT_PUBLIC void intrusive_ptr_add_ref(simgrid::s4u::Comm * c);
32   friend Mailbox; // Factory of comms
33
34   virtual ~Comm();
35
36   /*! take a vector s4u::CommPtr and return when one of them is finished.
37    * The return value is the rank of the first finished CommPtr. */
38   static int wait_any(std::vector<CommPtr> * comms) { return wait_any_for(comms, -1); }
39   /*! Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
40   static int wait_any_for(std::vector<CommPtr> * comms_in, double timeout)
41   {
42     // Map to dynar<Synchro*>:
43     xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), [](void*ptr){
44       intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
45     });
46     for (auto const& comm : *comms_in) {
47       if (comm->state_ == inited)
48         comm->start();
49       xbt_assert(comm->state_ == started);
50       simgrid::kernel::activity::ActivityImpl* ptr = comm->pimpl_.get();
51       intrusive_ptr_add_ref(ptr);
52       xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, ptr);
53     }
54     // Call the underlying simcall:
55     int idx = simcall_comm_waitany(comms, timeout);
56     xbt_dynar_free(&comms);
57     return idx;
58   }
59
60   /*! take a vector s4u::CommPtr and return when all of them is finished. */
61   static void wait_all(std::vector<CommPtr> * comms)
62   {
63     // TODO: this should be a simcall or something
64     // TODO: we are missing a version with timeout
65     for (CommPtr comm : *comms) {
66       comm->wait();
67     }
68   }
69   /*! take a vector s4u::CommPtr and return the rank of the first finished one (or -1 if none is done). */
70   static int test_any(std::vector<CommPtr> * comms);
71
72   Activity* start() override;
73   Activity* wait() override;
74   Activity* wait(double timeout) override;
75
76   /** Start the comm, and ignore its result. It can be completely forgotten after that. */
77   Activity* detach();
78   /** Start the comm, and ignore its result. It can be completely forgotten after that. */
79   Activity* detach(void (*cleanFunction)(void*))
80   {
81     cleanFunction_ = cleanFunction;
82     return detach();
83   }
84
85   /** Sets the maximal communication rate (in byte/sec). Must be done before start */
86   Activity* setRate(double rate);
87
88   /** Specify the data to send */
89   Activity* setSrcData(void* buff);
90   /** Specify the size of the data to send */
91   Activity* setSrcDataSize(size_t size);
92   /** Specify the data to send and its size */
93   Activity* setSrcData(void* buff, size_t size);
94
95   /** Specify where to receive the data */
96   Activity* setDstData(void** buff);
97   /** Specify the buffer in which the data should be received */
98   Activity* setDstData(void** buff, size_t size);
99   /** Retrieve the size of the received data */
100   size_t getDstDataSize();
101
102   bool test();
103   Activity* cancel();
104
105   /** Retrieve the mailbox on which this comm acts */
106   MailboxPtr getMailbox();
107
108 private:
109   double rate_        = -1;
110   void* dstBuff_      = nullptr;
111   size_t dstBuffSize_ = 0;
112   void* srcBuff_      = nullptr;
113   size_t srcBuffSize_ = sizeof(void*);
114
115   /* FIXME: expose these elements in the API */
116   int detached_ = 0;
117   int (*matchFunction_)(void*, void*, simgrid::kernel::activity::CommImpl*) = nullptr;
118   void (*cleanFunction_)(void*) = nullptr;
119   void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
120
121   smx_actor_t sender_   = nullptr;
122   smx_actor_t receiver_ = nullptr;
123   MailboxPtr mailbox_   = nullptr;
124
125   std::atomic_int_fast32_t refcount_{0};
126 };
127 }
128 } // namespace simgrid::s4u
129
130 #endif /* SIMGRID_S4U_COMM_HPP */