Logo AND Algorithmique Numérique Distribuée

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