Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simplify calls to CommImpl::isend and CommImpl::irecv
[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 */
28   s4u::Host* from_   = nullptr; /* Pre-determined only for direct host-to-host communications */
29   s4u::Host* to_     = nullptr; /* Otherwise, computed at start() time from the actors */
30
31 public:
32   enum class Type { SEND, RECEIVE };
33
34   static void set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t));
35
36   explicit CommImpl(Type type) : type_(type) {}
37   CommImpl(s4u::Host* from, s4u::Host* to, double bytes);
38
39   CommImpl& set_size(double size);
40   CommImpl& set_src_buff(unsigned char* buff, size_t size);
41   CommImpl& set_dst_buff(unsigned char* buff, size_t* size);
42   CommImpl& set_rate(double rate);
43   CommImpl& set_mailbox(MailboxImpl* mbox);
44   CommImpl& detach();
45
46   double get_rate() const { return rate_; }
47   MailboxImpl* get_mailbox() const { return mbox_; }
48   bool detached() const { return detached_; }
49
50   std::vector<s4u::Link*> get_traversed_links() const;
51   void copy_data();
52
53   static ActivityImplPtr isend(actor::CommIsendSimcall* observer);
54   static ActivityImplPtr irecv(actor::CommIrecvSimcall* observer);
55
56   bool test(actor::ActorImpl* issuer) override;
57   void wait_for(actor::ActorImpl* issuer, double timeout) override;
58   static void wait_any_for(actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms, double timeout);
59
60   CommImpl* start();
61   void suspend() override;
62   void resume() override;
63   void cancel() override;
64   void post() override;
65   void set_exception(actor::ActorImpl* issuer) override;
66   void finish() override;
67
68   const Type type_ = Type::SEND; /* Type of the communication (SEND or RECEIVE) */
69
70 #if SIMGRID_HAVE_MC
71   MailboxImpl* mbox_cpy = nullptr; /* Copy of the rendez-vous where the comm is queued, MC needs it for DPOR
72                                      (comm.mbox set to nullptr when the communication is removed from the mailbox
73                                      (used as garbage collector)) */
74 #endif
75
76   void (*clean_fun)(void*) = nullptr; /* Function to clean the detached src_buf if something goes wrong */
77   bool (*match_fun)(void*, void*, CommImpl*) = nullptr; /* Filter function used by the other side. It is used when
78 looking if a given communication matches my needs. For that, myself must match the
79 expectations of the other side, too. See  */
80   void (*copy_data_fun)(CommImpl*, void*, size_t) = nullptr;
81
82   /* Surf action data */
83   resource::Action* src_timeout_ = nullptr; /* Surf's actions to instrument the timeouts */
84   resource::Action* dst_timeout_ = nullptr; /* Surf's actions to instrument the timeouts */
85   actor::ActorImplPtr src_actor_ = nullptr;
86   actor::ActorImplPtr dst_actor_ = nullptr;
87
88   /* Data to be transferred */
89   unsigned char* src_buff_ = nullptr;
90   unsigned char* dst_buff_ = nullptr;
91   size_t src_buff_size_    = 0;
92   size_t* dst_buff_size_   = nullptr;
93
94   void* src_data_ = nullptr; /* User data associated to the communication */
95   void* dst_data_ = nullptr;
96   static xbt::signal<void(CommImpl const&)> on_start;
97   static xbt::signal<void(CommImpl const&)> on_completion;
98 };
99 } // namespace activity
100 } // namespace kernel
101 } // namespace simgrid
102
103 #endif