Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e55b407a2579a7af4b8b199c47cd64b32596f104
[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
30 protected:
31   Exec();
32   virtual ~Exec() = default;
33
34 public:
35 #ifndef DOXYGEN
36   Exec(Exec const&) = delete;
37   Exec& operator=(Exec const&) = delete;
38
39   friend ExecSeq;
40   friend ExecPar;
41   friend XBT_PUBLIC void intrusive_ptr_release(Exec* e);
42   friend XBT_PUBLIC void intrusive_ptr_add_ref(Exec* e);
43 #endif
44   static xbt::signal<void(Actor const&, Exec const&)> on_start;
45   static xbt::signal<void(Actor const&, Exec const&)> on_completion;
46
47   virtual Exec* start() override          = 0;
48   virtual double get_remaining_ratio()    = 0;
49   virtual ExecPtr set_host(Host* host)    = 0;
50
51   Exec* wait() override;
52   Exec* wait_for(double timeout) override;
53   /*! take a vector of s4u::ExecPtr and return when one of them is finished.
54    * The return value is the rank of the first finished ExecPtr. */
55   static int wait_any(std::vector<ExecPtr>* execs) { return wait_any_for(execs, -1); }
56   /*! Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
57   static int wait_any_for(std::vector<ExecPtr>* execs, double timeout);
58
59   bool test() override;
60
61   ExecPtr set_bound(double bound);
62   ExecPtr set_priority(double priority);
63   ExecPtr set_timeout(double timeout);
64   Exec* cancel() override;
65   Host* get_host() const;
66   unsigned int get_host_number() const;
67   double get_start_time() const;
68   double get_finish_time() const;
69   double get_cost() const;
70 };
71
72 class XBT_PUBLIC ExecSeq : public Exec {
73   double flops_amount_ = 0.0;
74
75   explicit ExecSeq(sg_host_t host, double flops_amount);
76
77 public:
78   friend XBT_PUBLIC ExecPtr this_actor::exec_init(double flops_amount);
79
80   ~ExecSeq() = default;
81
82   Exec* start() override;
83
84   ExecPtr set_host(Host* host) override;
85
86   double get_remaining() override;
87   double get_remaining_ratio() override;
88 };
89
90 class XBT_PUBLIC ExecPar : public Exec {
91   std::vector<s4u::Host*> hosts_;
92   std::vector<double> flops_amounts_;
93   std::vector<double> bytes_amounts_;
94   explicit ExecPar(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
95                    const std::vector<double>& bytes_amounts);
96   ExecPtr set_host(Host*) override { /* parallel exec cannot be moved */ THROW_UNIMPLEMENTED; }
97
98 public:
99   ~ExecPar() = default;
100   friend XBT_PUBLIC ExecPtr this_actor::exec_init(const std::vector<s4u::Host*>& hosts,
101                                                   const std::vector<double>& flops_amounts,
102                                                   const std::vector<double>& bytes_amounts);
103   double get_remaining() override;
104   double get_remaining_ratio() override;
105   Exec* start() override;
106 };
107
108 } // namespace s4u
109 } // namespace simgrid
110
111 #endif /* SIMGRID_S4U_EXEC_HPP */