Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
task can now pass tokens (void*) to other tasks. add example using tokens and apache...
[simgrid.git] / include / simgrid / plugins / task.hpp
1 #ifndef SIMGRID_PLUGINS_TASK_H_
2 #define SIMGRID_PLUGINS_TASK_H_
3
4 #include <simgrid/s4u/Activity.hpp>
5 #include <simgrid/s4u/Io.hpp>
6 #include <xbt/Extendable.hpp>
7
8 #include <atomic>
9 #include <deque>
10 #include <map>
11 #include <memory>
12 #include <set>
13
14 namespace simgrid::plugins {
15
16 class Task;
17 using TaskPtr = boost::intrusive_ptr<Task>;
18 XBT_PUBLIC void intrusive_ptr_release(Task* o);
19 XBT_PUBLIC void intrusive_ptr_add_ref(Task* o);
20 class ExecTask;
21 using ExecTaskPtr = boost::intrusive_ptr<ExecTask>;
22 class CommTask;
23 using CommTaskPtr = boost::intrusive_ptr<CommTask>;
24 class IoTask;
25 using IoTaskPtr = boost::intrusive_ptr<IoTask>;
26
27 struct ExtendedAttributeActivity {
28   static simgrid::xbt::Extension<simgrid::s4u::Activity, ExtendedAttributeActivity> EXTENSION_ID;
29   Task* task_;
30 };
31
32 class Task {
33   std::set<Task*> successors_                 = {};
34   std::map<Task*, unsigned int> predecessors_ = {};
35
36   bool ready_to_run() const;
37   void receive(Task* source);
38   void complete();
39
40 protected:
41   std::string name_;
42   double amount_;
43   std::shared_ptr<void> token_ = NULL;
44   std::deque<std::map<TaskPtr, std::shared_ptr<void>>> tokens_received_;
45   int queued_execs_ = 0;
46   int count_        = 0;
47   bool working_     = false;
48   s4u::ActivityPtr previous_activity_;
49   s4u::ActivityPtr current_activity_;
50   xbt::signal<void(Task*)> on_this_start_;
51   xbt::signal<void(Task*)> on_this_end_;
52   explicit Task(const std::string& name);
53   virtual ~Task()     = default;
54   virtual void fire() = 0;
55
56   static xbt::signal<void(Task*)> on_start;
57   static xbt::signal<void(Task*)> on_end;
58   std::atomic_int_fast32_t refcount_{0};
59
60 public:
61   static void init();
62   const std::string& get_name() const { return name_; }
63   const char* get_cname() const { return name_.c_str(); }
64   void enqueue_execs(int n);
65   void set_amount(double amount);
66   double get_amount() const { return amount_; }
67   void set_token(std::shared_ptr<void> token);
68   std::map<TaskPtr, std::shared_ptr<void>> get_tokens() const;
69   void add_successor(TaskPtr t);
70   void remove_successor(TaskPtr t);
71   void remove_all_successors();
72   const std::set<Task*>& get_successors() const { return successors_; }
73   void on_this_start_cb(const std::function<void(Task*)>& func);
74   void on_this_end_cb(const std::function<void(Task*)>& func);
75   int get_count() const;
76
77   /** Add a callback fired before a task activity start.
78    * Triggered after the on_this_start function**/
79   static void on_start_cb(const std::function<void(Task*)>& cb) { on_start.connect(cb); }
80   /** Add a callback fired after a task activity end.
81    * Triggered after the on_this_end function, but before
82    * sending tokens to successors.**/
83   static void on_end_cb(const std::function<void(Task*)>& cb) { on_end.connect(cb); }
84
85 #ifndef DOXYGEN
86   friend void intrusive_ptr_release(Task* o)
87   {
88     if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
89       std::atomic_thread_fence(std::memory_order_acquire);
90       delete o;
91     }
92   }
93   friend void intrusive_ptr_add_ref(Task* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
94 #endif
95 };
96
97 class ExecTask : public Task {
98   s4u::Host* host_;
99
100   explicit ExecTask(const std::string& name);
101   void fire() override;
102
103 public:
104   static ExecTaskPtr init(const std::string& name);
105   static ExecTaskPtr init(const std::string& name, double flops, s4u::Host* host);
106   ExecTaskPtr set_host(s4u::Host* host);
107   s4u::Host* get_host() const { return host_; }
108   ExecTaskPtr set_flops(double flops);
109   double get_flops() const { return get_amount(); }
110 };
111
112 class CommTask : public Task {
113   s4u::Host* source_;
114   s4u::Host* destination_;
115
116   explicit CommTask(const std::string& name);
117   void fire() override;
118
119 public:
120   static CommTaskPtr init(const std::string& name);
121   static CommTaskPtr init(const std::string& name, double bytes, s4u::Host* source, s4u::Host* destination);
122   CommTaskPtr set_source(s4u::Host* source);
123   s4u::Host* get_source() const { return source_; }
124   CommTaskPtr set_destination(s4u::Host* destination);
125   s4u::Host* get_destination() const { return destination_; }
126   CommTaskPtr set_bytes(double bytes);
127   double get_bytes() const { return get_amount(); }
128 };
129
130 class IoTask : public Task {
131   s4u::Disk* disk_;
132   s4u::Io::OpType type_;
133   explicit IoTask(const std::string& name);
134   void fire() override;
135
136 public:
137   static IoTaskPtr init(const std::string& name);
138   static IoTaskPtr init(const std::string& name, double bytes, s4u::Disk* disk, s4u::Io::OpType type);
139   IoTaskPtr set_disk(s4u::Disk* disk);
140   s4u::Disk* get_disk() const { return disk_; }
141   IoTaskPtr set_bytes(double bytes);
142   double get_bytes() const { return get_amount(); }
143   IoTaskPtr set_op_type(s4u::Io::OpType type);
144   s4u::Io::OpType get_op_type() const { return type_; }
145 };
146 } // namespace simgrid::plugins
147 #endif