Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / include / simgrid / s4u / Exec.hpp
1 /* Copyright (c) 2017-2023. 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::s4u {
15
16 /** Computation Activity, representing the asynchronous executions.
17  *
18  * @beginrst
19  * Most of them are created with :cpp:func:`simgrid::s4u::this_actor::exec_init()` or
20  * :cpp:func:`simgrid::s4u::Host::execute()`, and represent a classical (sequential) execution. This can be used to
21  * simulate some computation occurring in another thread when the calling actor is not blocked during the execution.
22  *
23  * You can also use :cpp:func:`simgrid::s4u::this_actor::parallel_execute()` to create *parallel* executions. These
24  * objects represent distributed computations involving computations on several hosts and communications between them.
25  * Such objects can for example represent a matrix multiplication done with ScaLAPACK on a real system. Once created,
26  * parallel Exec are very similar to the sequential ones. The only difference is that you cannot migrate them, and their
27  * remaining amount of work can only be defined as a ratio. See the doc of :cpp:func:`simgrid::s4u::Exec::get_remaining`
28  * and :cpp:func:`simgrid::s4u::Exec::get_remaining_ratio` for more info.
29  * @endrst
30  */
31 class XBT_PUBLIC Exec : public Activity_T<Exec> {
32 #ifndef DOXYGEN
33   friend kernel::activity::ExecImpl;
34   friend kernel::EngineImpl; // Auto-completes the execs of maestro (in simDAG)
35 #endif
36
37   bool parallel_ = false;
38
39   inline static xbt::signal<void(Exec const&)> on_start;
40   xbt::signal<void(Exec const&)> on_this_start;
41
42 protected:
43   explicit Exec(kernel::activity::ExecImplPtr pimpl);
44   Exec* do_start() override;
45
46   void reset() const;
47
48   void fire_on_completion() const override { on_completion(*this); }
49   void fire_on_this_completion() const override { on_this_completion(*this); }
50   void fire_on_suspend() const override { on_suspend(*this); }
51   void fire_on_this_suspend() const override { on_this_suspend(*this); }
52   void fire_on_resume() const override { on_resume(*this); }
53   void fire_on_this_resume() const override { on_this_resume(*this); }
54   void fire_on_veto() const override { on_veto(const_cast<Exec&>(*this)); }
55   void fire_on_this_veto() const override { on_this_veto(const_cast<Exec&>(*this)); }
56
57 public:
58 #ifndef DOXYGEN
59   Exec(Exec const&) = delete;
60   Exec& operator=(Exec const&) = delete;
61 #endif
62   /*! \static Signal fired each time that any execution actually starts (no veto) */
63   static void on_start_cb(const std::function<void(Exec const&)>& cb) { on_start.connect(cb); }
64   /*! Signal fired each time that this specific execution actually starts (no veto) */
65   void on_this_start_cb(const std::function<void(Exec const&)>& cb) { on_this_start.connect(cb); }
66
67   /*! \static Initiate the creation of an Exec. Setters have to be called afterwards */
68   static ExecPtr init();
69
70   /*! \static take a vector of s4u::ExecPtr and return when one of them is finished.
71    * The return value is the rank of the first finished ExecPtr. */
72   static ssize_t wait_any(const std::vector<ExecPtr>& execs) { return wait_any_for(execs, -1); }
73   /*! \static Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
74   static ssize_t wait_any_for(const std::vector<ExecPtr>& execs, double timeout);
75
76   /** @brief On sequential executions, returns the amount of flops that remain to be done; This cannot be used on
77    * parallel executions. */
78   double get_remaining() const override;
79   double get_remaining_ratio() const;
80   ExecPtr set_host(Host* host);
81   ExecPtr set_hosts(const std::vector<Host*>& hosts);
82   ExecPtr unset_host();
83   ExecPtr unset_hosts() { return unset_host(); }
84
85   ExecPtr set_flops_amount(double flops_amount);
86   ExecPtr set_flops_amounts(const std::vector<double>& flops_amounts);
87   ExecPtr set_bytes_amounts(const std::vector<double>& bytes_amounts);
88
89   ExecPtr set_thread_count(int thread_count);
90
91   ExecPtr set_bound(double bound);
92   ExecPtr set_priority(double priority);
93   ExecPtr update_priority(double priority);
94
95   Host* get_host() const;
96   unsigned int get_host_number() const;
97   int get_thread_count() const;
98   double get_cost() const;
99   bool is_parallel() const { return parallel_; }
100   bool is_assigned() const override;
101 };
102
103 } // namespace simgrid::s4u
104
105 #endif /* SIMGRID_S4U_EXEC_HPP */