Logo AND Algorithmique Numérique Distribuée

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