Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
CommWaitTransition mailbox is now valid
[simgrid.git] / src / kernel / activity / CommImpl.hpp
1 /* Copyright (c) 2007-2022. 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_KERNEL_ACTIVITY_COMM_HPP
7 #define SIMGRID_KERNEL_ACTIVITY_COMM_HPP
8
9 #include "src/kernel/activity/ActivityImpl.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11 #include "src/kernel/actor/SimcallObserver.hpp"
12
13 namespace simgrid {
14 namespace kernel {
15 namespace activity {
16
17 class XBT_PUBLIC CommImpl : public ActivityImpl_T<CommImpl> {
18   ~CommImpl() override;
19   void cleanup_surf();
20
21   static void (*copy_data_callback_)(CommImpl*, void*, size_t);
22
23   double rate_       = 0.0;
24   double size_       = 0.0;
25   bool detached_     = false;   /* If detached or not */
26   bool copied_       = false;   /* whether the data were already copied */
27   MailboxImpl* mbox_ = nullptr; /* Rendez-vous where the comm is queued. nullptr once the comm is matched with both a
28                                    sender and receiver */
29   long mbox_id_      = -1;      /* ID of the rendez-vous where the comm was first queued (for MC) */
30   s4u::Host* from_   = nullptr; /* Pre-determined only for direct host-to-host communications */
31   s4u::Host* to_     = nullptr; /* Otherwise, computed at start() time from the actors */
32
33 public:
34   enum class Type { SEND, RECEIVE };
35
36   static void set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t));
37
38   explicit CommImpl(Type type) : type_(type) {}
39   CommImpl(s4u::Host* from, s4u::Host* to, double bytes);
40
41   CommImpl& set_size(double size);
42   CommImpl& set_src_buff(unsigned char* buff, size_t size);
43   CommImpl& set_dst_buff(unsigned char* buff, size_t* size);
44   CommImpl& set_rate(double rate);
45   CommImpl& set_mailbox(MailboxImpl* mbox);
46   CommImpl& detach();
47
48   double get_rate() const { return rate_; }
49   MailboxImpl* get_mailbox() const { return mbox_; }
50   long get_mailbox_id() const { return mbox_id_; }
51   bool detached() const { return detached_; }
52
53   std::vector<s4u::Link*> get_traversed_links() const;
54   void copy_data();
55
56   static ActivityImplPtr isend(actor::CommIsendSimcall* observer);
57   static ActivityImplPtr irecv(actor::CommIrecvSimcall* observer);
58
59   bool test(actor::ActorImpl* issuer) override;
60   void wait_for(actor::ActorImpl* issuer, double timeout) override;
61   static void wait_any_for(actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms, double timeout);
62
63   CommImpl* start();
64   void suspend() override;
65   void resume() override;
66   void cancel() override;
67   void post() override;
68   void set_exception(actor::ActorImpl* issuer) override;
69   void finish() override;
70
71   const Type type_ = Type::SEND; /* Type of the communication (SEND or RECEIVE) */
72
73 #if SIMGRID_HAVE_MC
74   MailboxImpl* mbox_cpy = nullptr; /* Copy of the rendez-vous where the comm is queued, MC needs it for DPOR
75                                      (comm.mbox set to nullptr when the communication is removed from the mailbox
76                                      (used as garbage collector)) */
77 #endif
78
79   void (*clean_fun)(void*) = nullptr; /* Function to clean the detached src_buf if something goes wrong */
80   bool (*match_fun)(void*, void*, CommImpl*) = nullptr; /* Filter function used by the other side. It is used when
81 looking if a given communication matches my needs. For that, myself must match the
82 expectations of the other side, too. See  */
83   void (*copy_data_fun)(CommImpl*, void*, size_t) = nullptr;
84
85   /* Surf action data */
86   resource::Action* src_timeout_ = nullptr; /* Surf's actions to instrument the timeouts */
87   resource::Action* dst_timeout_ = nullptr; /* Surf's actions to instrument the timeouts */
88   actor::ActorImplPtr src_actor_ = nullptr;
89   actor::ActorImplPtr dst_actor_ = nullptr;
90
91   /* Data to be transferred */
92   unsigned char* src_buff_ = nullptr;
93   unsigned char* dst_buff_ = nullptr;
94   size_t src_buff_size_    = 0;
95   size_t* dst_buff_size_   = nullptr;
96
97   void* src_data_ = nullptr; /* User data associated to the communication */
98   void* dst_data_ = nullptr;
99   static xbt::signal<void(CommImpl const&)> on_start;
100   static xbt::signal<void(CommImpl const&)> on_completion;
101 };
102 } // namespace activity
103 } // namespace kernel
104 } // namespace simgrid
105
106 #endif