1 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_KERNEL_ACTIVITY_COMM_HPP
7 #define SIMGRID_KERNEL_ACTIVITY_COMM_HPP
9 #include "src/kernel/activity/ActivityImpl.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11 #include "src/kernel/actor/CommObserver.hpp"
17 enum class CommImplType { SEND, RECEIVE };
19 class XBT_PUBLIC CommImpl : public ActivityImpl_T<CommImpl> {
23 static void (*copy_data_callback_)(CommImpl*, void*, size_t);
27 bool detached_ = false; /* If detached or not */
28 bool copied_ = false; /* whether the data were already copied */
29 MailboxImpl* mbox_ = nullptr; /* Rendez-vous where the comm is queued. nullptr once the comm is matched with both a
30 sender and receiver */
31 long mbox_id_ = -1; /* ID of the rendez-vous where the comm was first queued (for MC) */
32 s4u::Host* from_ = nullptr; /* Pre-determined only for direct host-to-host communications */
33 s4u::Host* to_ = nullptr; /* Otherwise, computed at start() time from the actors */
34 CommImplType type_ = CommImplType::SEND; /* Type of the communication (SEND or RECEIVE) */
37 static void set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t));
40 CommImpl(s4u::Host* from, s4u::Host* to, double bytes);
42 CommImpl& set_type(CommImplType type);
43 CommImplType get_type() const { return type_; }
44 CommImpl& set_size(double size);
45 CommImpl& set_src_buff(unsigned char* buff, size_t size);
46 CommImpl& set_dst_buff(unsigned char* buff, size_t* size);
47 CommImpl& set_rate(double rate);
48 CommImpl& set_mailbox(MailboxImpl* mbox);
51 double get_rate() const { return rate_; }
52 MailboxImpl* get_mailbox() const { return mbox_; }
53 long get_mailbox_id() const { return mbox_id_; }
54 bool detached() const { return detached_; }
56 std::vector<s4u::Link*> get_traversed_links() const;
59 static ActivityImplPtr isend(actor::CommIsendSimcall* observer);
60 static ActivityImplPtr irecv(actor::CommIrecvSimcall* observer);
62 bool test(actor::ActorImpl* issuer) override;
63 void wait_for(actor::ActorImpl* issuer, double timeout) override;
64 static void wait_any_for(actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms, double timeout);
67 void suspend() override;
68 void resume() override;
69 void cancel() override;
71 void set_exception(actor::ActorImpl* issuer) override;
72 void finish() override;
76 MailboxImpl* mbox_cpy = nullptr; /* Copy of the rendez-vous where the comm is queued, MC needs it for DPOR
77 (comm.mbox set to nullptr when the communication is removed from the mailbox
78 (used as garbage collector)) */
81 void (*clean_fun)(void*) = nullptr; /* Function to clean the detached src_buf if something goes wrong */
82 bool (*match_fun)(void*, void*, CommImpl*) = nullptr; /* Filter function used by the other side. It is used when
83 looking if a given communication matches my needs. For that, myself must match the
84 expectations of the other side, too. See */
85 void (*copy_data_fun)(CommImpl*, void*, size_t) = nullptr;
87 /* Surf action data */
88 resource::Action* src_timeout_ = nullptr; /* Surf's actions to instrument the timeouts */
89 resource::Action* dst_timeout_ = nullptr; /* Surf's actions to instrument the timeouts */
90 actor::ActorImplPtr src_actor_ = nullptr;
91 actor::ActorImplPtr dst_actor_ = nullptr;
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;
99 void* src_data_ = nullptr; /* User data associated to the communication */
100 void* dst_data_ = nullptr;
101 static xbt::signal<void(CommImpl const&)> on_start;
102 static xbt::signal<void(CommImpl const&)> on_completion;
104 } // namespace activity
105 } // namespace kernel
106 } // namespace simgrid