Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[project-description] Fix extraction of the ns-3 version.
[simgrid.git] / src / kernel / actor / CommObserver.hpp
1 /* Copyright (c) 2019-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_MC_SIMCALL_COMM_OBSERVER_HPP
7 #define SIMGRID_MC_SIMCALL_COMM_OBSERVER_HPP
8
9 #include "simgrid/forward.h"
10 #include "src/kernel/actor/SimcallObserver.hpp"
11 #include "src/mc/transition/Transition.hpp"
12 #include "xbt/asserts.h"
13
14 #include <string>
15
16 namespace simgrid::kernel::actor {
17
18 class ActivityTestSimcall final : public ResultingSimcall<bool> {
19   activity::ActivityImpl* const activity_;
20
21 public:
22   ActivityTestSimcall(ActorImpl* actor, activity::ActivityImpl* activity)
23       : ResultingSimcall(actor, true), activity_(activity)
24   {
25   }
26   bool is_visible() const override { return true; }
27   activity::ActivityImpl* get_activity() const { return activity_; }
28   void serialize(std::stringstream& stream) const override;
29 };
30
31 class ActivityTestanySimcall final : public ResultingSimcall<ssize_t> {
32   const std::vector<activity::ActivityImpl*>& activities_;
33   std::vector<int> indexes_; // indexes in activities_ pointing to ready activities (=whose test() is positive)
34   int next_value_ = 0;
35
36 public:
37   ActivityTestanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities);
38   bool is_visible() const override { return true; }
39   bool is_enabled() override { return true; /* can return -1 if no activity is ready */ }
40   void serialize(std::stringstream& stream) const override;
41   int get_max_consider() override;
42   void prepare(int times_considered) override;
43   const std::vector<activity::ActivityImpl*>& get_activities() const { return activities_; }
44   int get_value() const { return next_value_; }
45 };
46
47 class ActivityWaitSimcall final : public ResultingSimcall<bool> {
48   activity::ActivityImpl* activity_;
49   const double timeout_;
50
51 public:
52   ActivityWaitSimcall(ActorImpl* actor, activity::ActivityImpl* activity, double timeout)
53       : ResultingSimcall(actor, false), activity_(activity), timeout_(timeout)
54   {
55   }
56   void serialize(std::stringstream& stream) const override;
57   bool is_visible() const override { return true; }
58   bool is_enabled() override;
59   activity::ActivityImpl* get_activity() const { return activity_; }
60   void set_activity(activity::ActivityImpl* activity) { activity_ = activity; }
61   double get_timeout() const { return timeout_; }
62 };
63
64 class ActivityWaitanySimcall final : public ResultingSimcall<ssize_t> {
65   const std::vector<activity::ActivityImpl*>& activities_;
66   std::vector<int> indexes_; // indexes in activities_ pointing to ready activities (=whose test() is positive)
67   const double timeout_;
68   int next_value_ = 0;
69
70 public:
71   ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities, double timeout);
72   bool is_enabled() override;
73   void serialize(std::stringstream& stream) const override;
74   bool is_visible() const override { return true; }
75   void prepare(int times_considered) override;
76   int get_max_consider() override;
77   const std::vector<activity::ActivityImpl*>& get_activities() const { return activities_; }
78   double get_timeout() const { return timeout_; }
79   int get_value() const { return next_value_; }
80 };
81
82 class CommIsendSimcall final : public SimcallObserver {
83   activity::MailboxImpl* mbox_;
84   double payload_size_;
85   double rate_;
86   unsigned char* src_buff_;
87   size_t src_buff_size_;
88   void* payload_;
89   bool detached_;
90   activity::CommImpl* comm_ = {};
91   int tag_                  = {};
92
93   std::function<bool(void*, void*, activity::CommImpl*)> match_fun_;
94   std::function<void(void*)> clean_fun_; // used to free the synchro in case of problem after a detached send
95   std::function<void(activity::CommImpl*, void*, size_t)> copy_data_fun_; // used to copy data if not default one
96
97 public:
98   CommIsendSimcall(
99       ActorImpl* actor, activity::MailboxImpl* mbox, double payload_size, double rate, unsigned char* src_buff,
100       size_t src_buff_size, const std::function<bool(void*, void*, activity::CommImpl*)>& match_fun,
101       const std::function<void(void*)>& clean_fun, // used to free the synchro in case of problem after a detached send
102       const std::function<void(activity::CommImpl*, void*, size_t)>&
103           copy_data_fun, // used to copy data if not default one
104       void* payload, bool detached)
105       : SimcallObserver(actor)
106       , mbox_(mbox)
107       , payload_size_(payload_size)
108       , rate_(rate)
109       , src_buff_(src_buff)
110       , src_buff_size_(src_buff_size)
111       , payload_(payload)
112       , detached_(detached)
113       , match_fun_(match_fun)
114       , clean_fun_(clean_fun)
115       , copy_data_fun_(copy_data_fun)
116   {
117   }
118   void serialize(std::stringstream& stream) const override;
119   bool is_visible() const override { return true; }
120   activity::MailboxImpl* get_mailbox() const { return mbox_; }
121   double get_payload_size() const { return payload_size_; }
122   double get_rate() const { return rate_; }
123   unsigned char* get_src_buff() const { return src_buff_; }
124   size_t get_src_buff_size() const { return src_buff_size_; }
125   void* get_payload() const { return payload_; }
126   bool is_detached() const { return detached_; }
127   void set_comm(activity::CommImpl* comm) { comm_ = comm; }
128   void set_tag(int tag) { tag_ = tag; }
129
130   auto const& get_match_fun() const { return match_fun_; }
131   auto const& get_clean_fun() const { return clean_fun_; }
132   auto const& get_copy_data_fun() const { return copy_data_fun_; }
133 };
134
135 class CommIrecvSimcall final : public SimcallObserver {
136   activity::MailboxImpl* mbox_;
137   unsigned char* dst_buff_;
138   size_t* dst_buff_size_;
139   void* payload_;
140   double rate_;
141   activity::CommImpl* comm_ = {};
142   int tag_                  = {};
143
144   std::function<bool(void*, void*, activity::CommImpl*)> match_fun_;
145   std::function<void(activity::CommImpl*, void*, size_t)> copy_data_fun_; // used to copy data if not default one
146
147 public:
148   CommIrecvSimcall(ActorImpl* actor, activity::MailboxImpl* mbox, unsigned char* dst_buff, size_t* dst_buff_size,
149                    const std::function<bool(void*, void*, activity::CommImpl*)>& match_fun,
150                    const std::function<void(activity::CommImpl*, void*, size_t)>& copy_data_fun, void* payload,
151                    double rate)
152       : SimcallObserver(actor)
153       , mbox_(mbox)
154       , dst_buff_(dst_buff)
155       , dst_buff_size_(dst_buff_size)
156       , payload_(payload)
157       , rate_(rate)
158       , match_fun_(match_fun)
159       , copy_data_fun_(copy_data_fun)
160   {
161   }
162   void serialize(std::stringstream& stream) const override;
163   bool is_visible() const override { return true; }
164   activity::MailboxImpl* get_mailbox() const { return mbox_; }
165   double get_rate() const { return rate_; }
166   unsigned char* get_dst_buff() const { return dst_buff_; }
167   size_t* get_dst_buff_size() const { return dst_buff_size_; }
168   void* get_payload() const { return payload_; }
169   void set_comm(activity::CommImpl* comm) { comm_ = comm; }
170   void set_tag(int tag) { tag_ = tag; }
171
172   auto const& get_match_fun() const { return match_fun_; };
173   auto const& get_copy_data_fun() const { return copy_data_fun_; }
174 };
175
176 } // namespace simgrid::kernel::actor
177
178 #endif