Logo AND Algorithmique Numérique Distribuée

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