1 /* Copyright (c) 2017-2020. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_S4U_EXEC_HPP
7 #define SIMGRID_S4U_EXEC_HPP
9 #include <simgrid/forward.h>
10 #include <simgrid/s4u/Activity.hpp>
11 #include <simgrid/s4u/Actor.hpp>
17 /** Computation Activity, representing the asynchronous executions.
20 * Most of them are created with :cpp:func:`simgrid::s4u::this_actor::exec_init()` or
21 * :cpp:func:`simgrid::s4u::Host::execute()`, and represent a classical (sequential) execution. This can be used to
22 * simulate some computation occuring in another thread when the calling actor is not blocked during the execution.
24 * You can also use :cpp:func:`simgrid::s4u::this_actor::parallel_execute()` to create *parallel* executions. These
25 * objects represent distributed computations involving computations on several hosts and communications between them.
26 * Such objects can for example represent a matrix multiplication done with ScaLAPACK on a real system. Once created,
27 * parallel Exec are very similar to the sequential ones. The only difference is that you cannot migrate them, and their
28 * remaining amount of work can only be defined as a ratio. See the doc of :cpp:func:`simgrid::s4u::Exec::get_remaining`
29 * and :cpp:func:`simgrid::s4u::Exec::get_remaining_ratio` for more info.
32 class XBT_PUBLIC Exec : public Activity_T<Exec> {
33 double priority_ = 1.0;
35 double timeout_ = 0.0;
41 virtual ~Exec() = default;
43 Exec(Exec const&) = delete;
44 Exec& operator=(Exec const&) = delete;
49 static xbt::signal<void(Actor const&, Exec const&)> on_start;
50 static xbt::signal<void(Actor const&, Exec const&)> on_completion;
52 Exec* start() override = 0;
53 /** @brief On sequential executions, returns the amount of flops that remain to be done; This cannot be used on
54 * parallel executions. */
55 virtual double get_remaining_ratio() const = 0;
56 virtual ExecPtr set_host(Host* host) = 0;
58 Exec* wait() override;
59 Exec* wait_for(double timeout) override;
60 /*! take a vector of s4u::ExecPtr and return when one of them is finished.
61 * The return value is the rank of the first finished ExecPtr. */
62 static int wait_any(std::vector<ExecPtr>* execs) { return wait_any_for(execs, -1); }
63 /*! Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
64 static int wait_any_for(std::vector<ExecPtr>* execs, double timeout);
68 ExecPtr set_bound(double bound);
69 ExecPtr set_priority(double priority);
70 XBT_ATTRIB_DEPRECATED_v329("Please use exec_init(...)->wait_for(timeout)") ExecPtr set_timeout(double timeout);
71 Exec* cancel() override;
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;
79 class XBT_PUBLIC ExecSeq : public Exec {
80 double flops_amount_ = 0.0;
82 explicit ExecSeq(sg_host_t host, double flops_amount);
85 friend XBT_PUBLIC ExecPtr this_actor::exec_init(double flops_amount);
89 Exec* start() override;
91 ExecPtr set_host(Host* host) override;
93 double get_remaining() const override;
94 double get_remaining_ratio() const override;
97 class XBT_PUBLIC ExecPar : public Exec {
98 std::vector<s4u::Host*> hosts_;
99 std::vector<double> flops_amounts_;
100 std::vector<double> bytes_amounts_;
101 explicit ExecPar(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
102 const std::vector<double>& bytes_amounts);
103 ExecPtr set_host(Host*) override { /* parallel exec cannot be moved */ THROW_UNIMPLEMENTED; }
106 ~ExecPar() = default;
107 friend XBT_PUBLIC ExecPtr this_actor::exec_init(const std::vector<s4u::Host*>& hosts,
108 const std::vector<double>& flops_amounts,
109 const std::vector<double>& bytes_amounts);
110 double get_remaining() const override;
111 double get_remaining_ratio() const override;
112 Exec* start() override;
116 } // namespace simgrid
118 #endif /* SIMGRID_S4U_EXEC_HPP */