Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / examples / cpp / dag-scheduling / s4u-dag-scheduling.cpp
1 /* Copyright (c) 2009-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 /* simple test to schedule a DAX file with the Min-Min algorithm.           */
7 #include <algorithm>
8 #include <simgrid/host.h>
9 #include <simgrid/s4u.hpp>
10 #include <string.h>
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(dag_scheduling, "Logging specific to this example");
13 namespace sg4 = simgrid::s4u;
14
15 static std::vector<sg4::Exec*> get_ready_tasks(const std::vector<sg4::ActivityPtr>& dax)
16 {
17   std::vector<sg4::Exec*> ready_tasks;
18   std::map<sg4::Exec*, unsigned int> candidate_execs;
19
20   for (const auto& a : dax) {
21     // Only look at activity that have their dependencies solved but are not assigned
22     if (a->dependencies_solved() && not a->is_assigned()) {
23       // if it is an exec, it's ready
24       if (auto* exec = dynamic_cast<sg4::Exec*>(a.get()))
25         ready_tasks.push_back(exec);
26       // if it a comm, we consider its successor as a candidate. If a candidate solves all its dependencies,
27       // i.e., get all its input data, it's ready
28       if (const auto* comm = dynamic_cast<sg4::Comm*>(a.get())) {
29         auto* next_exec = static_cast<sg4::Exec*>(comm->get_successors().front().get());
30         candidate_execs[next_exec]++;
31         if (next_exec->get_dependencies().size() == candidate_execs[next_exec])
32           ready_tasks.push_back(next_exec);
33       }
34     }
35   }
36   XBT_DEBUG("There are %zu ready tasks", ready_tasks.size());
37   return ready_tasks;
38 }
39
40 static sg4::Host* get_best_host(const sg4::ExecPtr exec, double* min_finish_time)
41 {
42   sg4::Host* best_host = nullptr;
43   *min_finish_time = std::numeric_limits<double>::max();
44
45   for (const auto& host : sg4::Engine::get_instance()->get_all_hosts()) {
46     double data_available      = 0.;
47     double last_data_available = -1.0;
48     /* compute last_data_available */
49     for (const auto& parent : exec->get_dependencies()) {
50       /* normal case */
51       if (const auto* comm = dynamic_cast<sg4::Comm*>(parent.get())) {
52         const auto* source = comm->get_source();
53         XBT_DEBUG("transfer from %s to %s", source->get_cname(), host->get_cname());
54         /* Estimate the redistribution time from this parent */
55         double redist_time;
56         if (comm->get_remaining() <= 1e-6) {
57           redist_time = 0;
58         } else {
59           double bandwidth      = std::numeric_limits<double>::max();
60           auto [links, latency] = source->route_to(host);
61           for (auto const& link : links)
62             bandwidth = std::min(bandwidth, link->get_bandwidth());
63
64           redist_time = latency + comm->get_remaining() / bandwidth;
65         }
66         // We use the user data field to store the finish time of the predecessor of the comm, i.e., its potential
67         // start time
68         data_available = *comm->get_data<double>() + redist_time;
69       }
70
71       /* no transfer, control dependency */
72       if (const auto* parent_exec = dynamic_cast<sg4::Exec*>(parent.get()))
73         data_available = parent_exec->get_finish_time();
74
75       if (last_data_available < data_available)
76         last_data_available = data_available;
77     }
78
79     double finish_time = std::max(*host->get_data<double>(), last_data_available) +
80                          exec->get_remaining() / host->get_speed();
81
82     XBT_DEBUG("%s finishes on %s at %f", exec->get_cname(), host->get_cname(), finish_time);
83
84     if (finish_time < *min_finish_time) {
85       *min_finish_time = finish_time;
86       best_host        = host;
87     }
88   }
89
90   return best_host;
91 }
92
93 static void schedule_on(sg4::ExecPtr exec, sg4::Host* host, double busy_until = 0.0)
94 {
95   exec->set_host(host);
96   // We use the user data field to store up to when the host is busy
97   delete host->get_data<double>(); // In case we're erasing a previous value
98   host->set_data(new double(busy_until));
99   // we can also set the destination of all the input comms of this exec
100   for (const auto& pred : exec->get_dependencies()) {
101     auto* comm = dynamic_cast<sg4::Comm*>(pred.get());
102     if (comm != nullptr) {
103       comm->set_destination(host);
104       delete comm->get_data<double>();
105     }
106   }
107   // we can also set the source of all the output comms of this exec
108   for (const auto& succ : exec->get_successors()) {
109     auto* comm = dynamic_cast<sg4::Comm*>(succ.get());
110     if (comm != nullptr)
111       comm->set_source(host);
112   }
113 }
114
115 int main(int argc, char** argv)
116 {
117   sg4::Engine e(&argc, argv);
118   std::set<sg4::Activity*> vetoed;
119   e.track_vetoed_activities(&vetoed);
120
121   sg4::Exec::on_completion_cb([](sg4::Exec const& exec) {
122     // when an Exec completes, we need to set the potential start time of all its ouput comms
123     for (const auto& succ : exec.get_successors()) {
124       auto* comm = dynamic_cast<sg4::Comm*>(succ.get());
125       if (comm != nullptr) {
126         auto* finish_time = new double(exec.get_finish_time());
127         // We use the user data field to store the finish time of the predecessor of the comm, i.e., its potential start
128         // time
129         comm->set_data(finish_time);
130       }
131     }
132   });
133
134   e.load_platform(argv[1]);
135
136   /* Mark all hosts as sequential, as it ought to be in such a scheduling example.
137    *
138    * It means that the hosts can only compute one thing at a given time. If an execution already takes place on a given
139    * host, any subsequently started execution will be queued until after the first execution terminates */
140   for (auto const& host : e.get_all_hosts()) {
141     host->set_concurrency_limit(1);
142     host->set_data(new double(0.0));
143   }
144   /* load the DAX file */
145   auto dax = sg4::create_DAG_from_DAX(argv[2]);
146
147   /* Schedule the root first */
148   double root_finish_time;
149   auto* root = static_cast<sg4::Exec*>(dax.front().get());
150   auto* host = get_best_host(root, &root_finish_time);
151   schedule_on(root, host);
152
153   e.run();
154
155   while (not vetoed.empty()) {
156     XBT_DEBUG("Start new scheduling round");
157     /* Get the set of ready tasks */
158     auto ready_tasks = get_ready_tasks(dax);
159     vetoed.clear();
160
161     if (ready_tasks.empty()) {
162       /* there is no ready exec, let advance the simulation */
163       e.run();
164       continue;
165     }
166     /* For each ready exec:
167      * get the host that minimizes the completion time.
168      * select the exec that has the minimum completion time on its best host.
169      */
170     double min_finish_time   = std::numeric_limits<double>::max();
171     sg4::Exec* selected_task = nullptr;
172     sg4::Host* selected_host = nullptr;
173
174     for (auto* exec : ready_tasks) {
175       XBT_DEBUG("%s is ready", exec->get_cname());
176       double finish_time;
177       host = get_best_host(exec, &finish_time);
178       if (finish_time < min_finish_time) {
179         min_finish_time = finish_time;
180         selected_task   = exec;
181         selected_host   = host;
182       }
183     }
184
185     XBT_INFO("Schedule %s on %s", selected_task->get_cname(), selected_host->get_cname());
186     schedule_on(selected_task, selected_host, min_finish_time);
187
188     ready_tasks.clear();
189     e.run();
190   }
191
192   /* Cleanup memory */
193   for (auto const* h : e.get_all_hosts())
194     delete h->get_data<double>();
195
196   XBT_INFO("Simulation Time: %f", simgrid_get_clock());
197
198   return 0;
199 }