Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move declarations for xbt_abort and xbt_die to xbt/asserts.h.
[simgrid.git] / src / instr / jedule / jedule_platform.cpp
1 /* Copyright (c) 2010-2021. 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 #include "simgrid/jedule/jedule.hpp"
7 #include "simgrid/config.h"
8 #include "simgrid/host.h"
9 #include "simgrid/s4u/NetZone.hpp"
10 #include "xbt/asserts.h"
11 #include <algorithm>
12
13 #if SIMGRID_HAVE_JEDULE
14
15 namespace {
16 std::unordered_map<const char*, jed_container_t> host2_simgrid_parent_container;
17 std::unordered_map<std::string, jed_container_t> container_name2container;
18 }
19
20 namespace simgrid {
21 namespace jedule {
22 Subset::Subset(int start_idx, int end_idx, Container* parent)
23     : start_idx(start_idx), nres(end_idx - start_idx + 1), parent(parent)
24 {
25 }
26
27 Container::Container(const std::string& name) : name(name)
28 {
29   container_name2container.insert({this->name, this});
30 }
31
32 void Container::add_child(jed_container_t child)
33 {
34   xbt_assert(child != nullptr);
35   children_.emplace_back(child);
36   child->set_parent(this);
37 }
38
39 void Container::add_resources(std::vector<sg_host_t> hosts)
40 {
41   children_.clear();
42   last_id_ = 0;
43
44   for (auto const& host : hosts) {
45     const char *host_name = sg_host_get_name(host);
46     this->name2id.insert({host_name, this->last_id_});
47     (this->last_id_)++;
48     host2_simgrid_parent_container.insert({host_name, this});
49     this->resource_list.push_back(host);
50   }
51 }
52
53 void Container::create_hierarchy(const_sg_netzone_t from_as)
54 {
55   if (from_as->get_children().empty()) {
56     // I am no AS
57     // add hosts to jedule platform
58     std::vector<sg_host_t> table = from_as->get_all_hosts();
59     this->add_resources(table);
60   } else {
61     for (auto const& nz : from_as->get_children()) {
62       auto* child_container = new simgrid::jedule::Container(nz->get_name());
63       this->add_child(child_container);
64       child_container->create_hierarchy(nz);
65     }
66   }
67 }
68
69 int Container::get_child_position(const Container* child) const
70 {
71   auto it = std::find_if(begin(children_), end(children_),
72                          [&child](const std::unique_ptr<Container>& c) { return c.get() == child; });
73   return it == end(children_) ? -1 : static_cast<int>(std::distance(begin(children_), it));
74 }
75
76 std::vector<int> Container::get_hierarchy()
77 {
78   if (parent_ == nullptr) {
79     int top_level = 0;
80     std::vector<int> hier_list = {top_level};
81     return hier_list;
82   } else if (parent_->has_children()) {
83     int child_nb = parent_->get_child_position(this);
84     xbt_assert(child_nb > -1);
85     std::vector<int> hier_list = parent_->get_hierarchy();
86     hier_list.insert(hier_list.begin(), child_nb);
87     return hier_list;
88   } else {
89     // we are in the last level
90     return parent_->get_hierarchy();
91   }
92 }
93
94 std::string Container::get_hierarchy_as_string()
95 {
96   std::string output("");
97
98   std::vector<int> hier_list = this->get_hierarchy();
99
100   bool sep = false;
101   for (auto const& id : hier_list) {
102     if (sep)
103       output += '.';
104     else
105       sep = true;
106     output += std::to_string(id);
107   }
108
109   return output;
110 }
111
112 void Container::print_resources(FILE* jed_file)
113 {
114   xbt_assert(not this->resource_list.empty());
115
116   std::string resid = this->get_hierarchy_as_string();
117
118   fprintf(jed_file, "      <rset id=\"%s\" nb=\"%zu\" names=\"", resid.c_str(), this->resource_list.size());
119   bool sep = false;
120   for (auto const& res : this->resource_list) {
121     if (sep)
122       putc('|', jed_file);
123     else
124       sep = true;
125     const char * res_name = sg_host_get_name(res);
126     fprintf(jed_file, "%s", res_name);
127   }
128   fprintf(jed_file, "\" />\n");
129 }
130
131 void Container::print(FILE* jed_file)
132 {
133   fprintf(jed_file, "    <res name=\"%s\">\n", this->name.c_str());
134   if (not children_.empty()) {
135     for (auto const& child : children_) {
136       child->print(jed_file);
137     }
138   } else {
139     this->print_resources(jed_file);
140   }
141   fprintf(jed_file, "    </res>\n");
142 }
143
144 } // namespace jedule
145 } // namespace simgrid
146
147 static void add_subsets_to(std::vector<simgrid::jedule::Subset>& subset_list, std::vector<const char*> hostgroup,
148                            jed_container_t parent)
149 {
150   // get ids for each host
151   // sort ids
152   // compact ids
153   // create subset for each id group
154
155   xbt_assert( parent != nullptr );
156
157   std::vector<unsigned int> id_list;
158
159   for (auto const& host_name : hostgroup) {
160     xbt_assert( host_name != nullptr );
161     const simgrid::jedule::Container* parent_cont = host2_simgrid_parent_container.at(host_name);
162     unsigned int id             = parent_cont->get_id_by_name(host_name);
163     id_list.push_back(id);
164   }
165   std::sort(id_list.begin(), id_list.end());
166
167   size_t nb_ids = id_list.size();
168   size_t start  = 0;
169   size_t pos    = start;
170   for (size_t i = 0; i < nb_ids; i++) {
171     if (id_list[i] - id_list[pos] > 1) {
172       subset_list.emplace_back(id_list[start], id_list[pos], parent);
173       start = i;
174
175       if (i == nb_ids - 1) {
176         subset_list.emplace_back(id_list[i], id_list[i], parent);
177       }
178     } else {
179       if (i == nb_ids - 1) {
180         subset_list.emplace_back(id_list[start], id_list[i], parent);
181       }
182     }
183     pos = i;
184   }
185 }
186
187 void get_resource_selection_by_hosts(std::vector<simgrid::jedule::Subset>& subset_list,
188                                      const std::vector<sg_host_t>& host_list)
189 {
190   // for each host name
191   //  find parent container
192   //  group by parent container
193   std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
194   for (auto const& host : host_list) {
195     const char *host_name = sg_host_get_name(host);
196     const simgrid::jedule::Container* parent = host2_simgrid_parent_container.at(host_name);
197     xbt_assert( parent != nullptr );
198
199     auto host_group = parent2hostgroup.find(parent->get_cname());
200     if (host_group == parent2hostgroup.end())
201       parent2hostgroup.insert({parent->get_cname(), std::vector<const char*>(1, host_name)});
202     else
203       host_group->second.push_back(host_name);
204   }
205
206   for (auto const& elm : parent2hostgroup) {
207     jed_container_t parent = container_name2container.at(elm.first);
208     add_subsets_to(subset_list, elm.second, parent);
209   }
210 }
211
212 #endif