Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into CRTP
[simgrid.git] / include / simgrid / s4u / Exec.hpp
1 /* Copyright (c) 2017-2019. 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_S4U_EXEC_HPP
7 #define SIMGRID_S4U_EXEC_HPP
8
9 #include <simgrid/forward.h>
10 #include <simgrid/s4u/Activity.hpp>
11 #include <simgrid/s4u/Actor.hpp>
12 #include <xbt/ex.h>
13
14 #include <atomic>
15
16 namespace simgrid {
17 namespace s4u {
18
19 /** Computation Activity, representing the asynchronous executions.
20  *
21  * They are generated from this_actor::exec_init() or Host::execute(), and can be used to model pools of threads or
22  * similar mechanisms.
23  */
24 class XBT_PUBLIC Exec : public Activity_T<Exec> {
25   double priority_              = 1.0;
26   double bound_                 = 0.0;
27   double timeout_               = 0.0;
28   std::atomic_int_fast32_t refcount_{0};
29   Host* host_ = nullptr;
30
31 protected:
32   Exec();
33   virtual ~Exec() = default;
34
35 public:
36 #ifndef DOXYGEN
37   Exec(Exec const&) = delete;
38   Exec& operator=(Exec const&) = delete;
39
40   friend ExecSeq;
41   friend ExecPar;
42   friend XBT_PUBLIC void intrusive_ptr_release(Exec* e);
43   friend XBT_PUBLIC void intrusive_ptr_add_ref(Exec* e);
44 #endif
45   static xbt::signal<void(Actor const&, Exec const&)> on_start;
46   static xbt::signal<void(Actor const&, Exec const&)> on_completion;
47
48   virtual Exec* start() override          = 0;
49   virtual double get_remaining_ratio()    = 0;
50   virtual ExecPtr set_host(Host* host)    = 0;
51
52   Exec* wait() override;
53   Exec* wait_for(double timeout) override;
54   /*! take a vector of s4u::ExecPtr and return when one of them is finished.
55    * The return value is the rank of the first finished ExecPtr. */
56   static int wait_any(std::vector<ExecPtr>* execs) { return wait_any_for(execs, -1); }
57   /*! Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
58   static int wait_any_for(std::vector<ExecPtr>* execs, double timeout);
59
60   bool test() override;
61
62   ExecPtr set_bound(double bound);
63   ExecPtr set_priority(double priority);
64   ExecPtr set_timeout(double timeout);
65   Exec* cancel() override;
66   const std::string& get_name() const { return name_; }
67   const char* get_cname() const { return name_.c_str(); }
68   Host* get_host() const;
69   unsigned int get_host_number() const;
70   double get_start_time() const;
71   double get_finish_time() const;
72   double get_cost() const;
73 };
74
75 class XBT_PUBLIC ExecSeq : public Exec {
76   double flops_amount_ = 0.0;
77
78   explicit ExecSeq(sg_host_t host, double flops_amount);
79
80 public:
81   friend XBT_PUBLIC ExecPtr this_actor::exec_init(double flops_amount);
82
83   ~ExecSeq() = default;
84
85   Exec* start() override;
86
87   ExecPtr set_host(Host* host) override;
88   Host* get_host();
89
90   double get_remaining() override;
91   double get_remaining_ratio() override;
92 };
93
94 class XBT_PUBLIC ExecPar : public Exec {
95   std::vector<s4u::Host*> hosts_;
96   std::vector<double> flops_amounts_;
97   std::vector<double> bytes_amounts_;
98   explicit ExecPar(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
99                    const std::vector<double>& bytes_amounts);
100   ExecPtr set_host(Host*) override { /* parallel exec cannot be moved */ THROW_UNIMPLEMENTED; }
101
102 public:
103   ~ExecPar() = default;
104   friend XBT_PUBLIC ExecPtr this_actor::exec_init(const std::vector<s4u::Host*>& hosts,
105                                                   const std::vector<double>& flops_amounts,
106                                                   const std::vector<double>& bytes_amounts);
107   double get_remaining() override;
108   double get_remaining_ratio() override;
109   Exec* start() override;
110 };
111
112 } // namespace s4u
113 } // namespace simgrid
114
115 #endif /* SIMGRID_S4U_EXEC_HPP */