Logo AND Algorithmique Numérique Distribuée

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