1 /* Copyright (c) 2007-2023. 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"
13 namespace simgrid::kernel::activity {
15 enum class CommImplType { SEND, RECEIVE };
17 using timeout_action_type = std::unique_ptr<resource::Action, std::function<void(resource::Action*)>>;
19 class XBT_PUBLIC CommImpl : public ActivityImpl_T<CommImpl> {
22 static std::function<void(CommImpl*, void*, size_t)> copy_data_callback_;
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) */
38 static void set_copy_data_callback(const std::function<void(CommImpl*, void*, size_t)>& callback);
40 CommImpl& set_type(CommImplType type);
41 CommImplType get_type() const { return type_; }
42 CommImpl& set_source(s4u::Host* from);
43 s4u::Host* get_source() const { return from_; }
44 CommImpl& set_destination(s4u::Host* to);
45 s4u::Host* get_destination() const { return to_; }
46 CommImpl& set_size(double size);
47 CommImpl& set_src_buff(unsigned char* buff, size_t size);
48 CommImpl& set_dst_buff(unsigned char* buff, size_t* size);
49 CommImpl& set_rate(double rate);
50 CommImpl& set_mailbox(MailboxImpl* mbox);
53 double get_rate() const { return rate_; }
54 MailboxImpl* get_mailbox() const { return mbox_; }
55 long get_mailbox_id() const { return mbox_id_; }
56 bool is_detached() const { return detached_; }
57 bool is_assigned() const { return (to_ != nullptr && from_ != nullptr); }
59 std::vector<s4u::Link*> get_traversed_links() const;
62 static ActivityImplPtr isend(actor::CommIsendSimcall* observer);
63 static ActivityImplPtr irecv(actor::CommIrecvSimcall* observer);
65 bool test(actor::ActorImpl* issuer) override;
66 void wait_for(actor::ActorImpl* issuer, double timeout) override;
67 static void wait_any_for(actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms, double timeout);
70 void suspend() override;
71 void resume() override;
72 void cancel() override;
73 void set_exception(actor::ActorImpl* issuer) override;
74 void finish() override;
76 std::function<void(void*)> clean_fun; /* Function to clean the detached src_buf if something goes wrong */
77 std::function<bool(void*, void*, CommImpl*)> match_fun; /* 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 std::function<void(CommImpl*, void*, size_t)> copy_data_fun;
83 timeout_action_type src_timeout_{nullptr, [](resource::Action* a) { a->unref(); }}; /* timeout set by the sender */
84 timeout_action_type dst_timeout_{nullptr, [](resource::Action* a) { a->unref(); }}; /* timeout set by the receiver */
86 actor::ActorImplPtr src_actor_ = nullptr;
87 actor::ActorImplPtr dst_actor_ = nullptr;
89 /* Data to be transferred */
90 unsigned char* src_buff_ = nullptr;
91 unsigned char* dst_buff_ = nullptr;
92 size_t src_buff_size_ = 0;
93 size_t* dst_buff_size_ = nullptr;
95 void* src_data_ = nullptr; /* User data associated to the communication */
96 void* dst_data_ = nullptr;
97 static xbt::signal<void(CommImpl const&)> on_start;
98 static xbt::signal<void(CommImpl const&)> on_completion;
100 } // namespace simgrid::kernel::activity