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"
16 class XBT_PUBLIC CommImpl : public ActivityImpl_T<CommImpl> {
20 static void (*copy_data_callback_)(CommImpl*, void*, size_t);
24 bool detached_ = false; /* If detached or not */
25 bool copied_ = false; /* whether the data were already copied */
26 MailboxImpl* mbox_ = nullptr; /* Rendez-vous where the comm is queued */
27 s4u::Host* from_ = nullptr; /* Pre-determined only for direct host-to-host communications */
28 s4u::Host* to_ = nullptr; /* Otherwise, computed at start() time from the actors */
31 enum class Type { SEND, RECEIVE };
33 static void set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t));
35 explicit CommImpl(Type type) : type_(type) {}
36 CommImpl(s4u::Host* from, s4u::Host* to, double bytes);
38 CommImpl& set_size(double size);
39 CommImpl& set_src_buff(unsigned char* buff, size_t size);
40 CommImpl& set_dst_buff(unsigned char* buff, size_t* size);
41 CommImpl& set_rate(double rate);
42 CommImpl& set_mailbox(MailboxImpl* mbox);
45 double get_rate() const { return rate_; }
46 MailboxImpl* get_mailbox() const { return mbox_; }
47 bool detached() const { return detached_; }
49 std::vector<s4u::Link*> get_traversed_links() const;
52 static ActivityImplPtr
53 isend(actor::ActorImpl* src, MailboxImpl* mbox, double task_size, double rate, unsigned char* src_buff,
54 size_t src_buff_size, bool (*match_fun)(void*, void*, CommImpl*),
55 void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
56 void (*copy_data_fun)(CommImpl*, void*, size_t), // used to copy data if not default one
57 void* data, bool detached);
58 static ActivityImplPtr irecv(actor::ActorImpl* receiver, MailboxImpl* mbox, unsigned char* dst_buff,
59 size_t* dst_buff_size, bool (*match_fun)(void*, void*, CommImpl*),
60 void (*copy_data_fun)(CommImpl*, void*, size_t), void* data, double rate);
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;
74 const Type type_ = Type::SEND; /* Type of the communication (SEND or RECEIVE) */
77 MailboxImpl* mbox_cpy = nullptr; /* Copy of the rendez-vous where the comm is queued, MC needs it for DPOR
78 (comm.mbox set to nullptr when the communication is removed from the mailbox
79 (used as garbage collector)) */
82 void (*clean_fun)(void*) = nullptr; /* Function to clean the detached src_buf if something goes wrong */
83 bool (*match_fun)(void*, void*, CommImpl*) = nullptr; /* Filter function used by the other side. It is used when
84 looking if a given communication matches my needs. For that, myself must match the
85 expectations of the other side, too. See */
86 void (*copy_data_fun)(CommImpl*, void*, size_t) = nullptr;
88 /* Surf action data */
89 resource::Action* src_timeout_ = nullptr; /* Surf's actions to instrument the timeouts */
90 resource::Action* dst_timeout_ = nullptr; /* Surf's actions to instrument the timeouts */
91 actor::ActorImplPtr src_actor_ = nullptr;
92 actor::ActorImplPtr dst_actor_ = nullptr;
94 /* Data to be transferred */
95 unsigned char* src_buff_ = nullptr;
96 unsigned char* dst_buff_ = nullptr;
97 size_t src_buff_size_ = 0;
98 size_t* dst_buff_size_ = nullptr;
100 void* src_data_ = nullptr; /* User data associated to the communication */
101 void* dst_data_ = nullptr;
102 static xbt::signal<void(CommImpl const&)> on_start;
103 static xbt::signal<void(CommImpl const&)> on_completion;
105 } // namespace activity
106 } // namespace kernel
107 } // namespace simgrid