Logo AND Algorithmique Numérique Distribuée

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