1 /* Copyright (c) 2017-2019. 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>
18 /** Computation Activity, representing the asynchronous executions.
20 * They are generated from this_actor::exec_init() or Host::execute(), and can be used to model pools of threads or
23 class XBT_PUBLIC Exec : public Activity {
24 std::string name_ = "";
25 double priority_ = 1.0;
27 double timeout_ = 0.0;
28 std::string tracing_category_ = "";
29 std::atomic_int_fast32_t refcount_{0};
30 Host* host_ = nullptr;
34 virtual ~Exec() = default;
38 Exec(Exec const&) = delete;
39 Exec& operator=(Exec const&) = delete;
43 friend XBT_PUBLIC void intrusive_ptr_release(Exec* e);
44 friend XBT_PUBLIC void intrusive_ptr_add_ref(Exec* e);
46 static xbt::signal<void(Actor const&)> on_start;
47 static xbt::signal<void(Actor const&)> on_completion;
49 virtual Exec* start() override = 0;
50 virtual double get_remaining_ratio() = 0;
51 virtual ExecPtr set_host(Host* host) = 0;
53 Exec* wait() override;
54 Exec* wait_for(double timeout) override;
55 /*! take a vector of s4u::ExecPtr and return when one of them is finished.
56 * The return value is the rank of the first finished ExecPtr. */
57 static int wait_any(std::vector<ExecPtr>* execs) { return wait_any_for(execs, -1); }
58 /*! Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
59 static int wait_any_for(std::vector<ExecPtr>* execs, double timeout);
63 ExecPtr set_bound(double bound);
64 ExecPtr set_name(const std::string& name);
65 ExecPtr set_priority(double priority);
66 ExecPtr set_tracing_category(const std::string& category);
67 ExecPtr set_timeout(double timeout);
68 Exec* cancel() override;
69 const std::string& get_name() const { return name_; }
70 const char* get_cname() const { return name_.c_str(); }
73 class XBT_PUBLIC ExecSeq : public Exec {
74 double flops_amount_ = 0.0;
76 explicit ExecSeq(sg_host_t host, double flops_amount);
79 friend XBT_PUBLIC ExecPtr this_actor::exec_init(double flops_amount);
83 Exec* start() override;
85 ExecPtr set_host(Host* host) override;
88 double get_remaining() override;
89 double get_remaining_ratio() override;
92 class XBT_PUBLIC ExecPar : public Exec {
93 std::vector<s4u::Host*> hosts_;
94 std::vector<double> flops_amounts_;
95 std::vector<double> bytes_amounts_;
96 explicit ExecPar(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
97 const std::vector<double>& bytes_amounts);
98 ExecPtr set_host(Host*) override { /* parallel exec cannot be moved */ THROW_UNIMPLEMENTED; }
101 ~ExecPar() = default;
102 friend XBT_PUBLIC ExecPtr this_actor::exec_init(const std::vector<s4u::Host*>& hosts,
103 const std::vector<double>& flops_amounts,
104 const std::vector<double>& bytes_amounts);
105 double get_remaining() override;
106 double get_remaining_ratio() override;
107 Exec* start() override;
111 } // namespace simgrid
113 #endif /* SIMGRID_S4U_EXEC_HPP */