Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'pikachuyann/simgrid-xbt_random'
[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       jed_container_t 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 : 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> heir_list = {top_level};
80     return heir_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> heir_list = parent_->get_hierarchy();
85     heir_list.insert(heir_list.begin(), child_nb);
86     return heir_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> heir_list = this->get_hierarchy();
98
99   unsigned int length = heir_list.size();
100   unsigned int i = 0;
101   for (auto const& id : heir_list) {
102     output += std::to_string(id);
103     if( i != length-1 ) {
104       output += ".";
105     }
106   }
107
108   return output;
109 }
110
111 void Container::print_resources(FILE* jed_file)
112 {
113   unsigned int i=0;
114   xbt_assert(not this->resource_list.empty());
115
116   unsigned int res_nb = this->resource_list.size();
117   std::string resid   = this->get_hierarchy_as_string();
118
119   fprintf(jed_file, "      <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
120   for (auto const& res : this->resource_list) {
121     const char * res_name = sg_host_get_name(res);
122     fprintf(jed_file, "%s", res_name);
123     if( i != res_nb-1 ) {
124       fprintf(jed_file, "|");
125     }
126     i++;
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     jed_container_t 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   unsigned int nb_ids = id_list.size();
166   std::sort(id_list.begin(), id_list.end());
167
168   if( nb_ids > 0 ) {
169     int start = 0;
170     int pos = start;
171     for(unsigned int i=0; i<nb_ids; i++) {
172       if( id_list[i] - id_list[pos] > 1 ) {
173         subset_list.emplace_back(id_list[start], id_list[pos], parent);
174         start = i;
175
176         if( i == nb_ids-1 ) {
177           subset_list.emplace_back(id_list[i], id_list[i], parent);
178         }
179       } else {
180         if( i == nb_ids-1 ) {
181           subset_list.emplace_back(id_list[start], id_list[i], parent);
182         }
183       }
184       pos = i;
185     }
186   }
187 }
188
189 void get_resource_selection_by_hosts(std::vector<simgrid::jedule::Subset>& subset_list,
190                                      const std::vector<sg_host_t>& host_list)
191 {
192   // for each host name
193   //  find parent container
194   //  group by parent container
195   std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
196   for (auto const& host : host_list) {
197     const char *host_name = sg_host_get_name(host);
198     const simgrid::jedule::Container* parent = host2_simgrid_parent_container.at(host_name);
199     xbt_assert( parent != nullptr );
200
201     auto host_group = parent2hostgroup.find(parent->get_cname());
202     if (host_group == parent2hostgroup.end())
203       parent2hostgroup.insert({parent->get_cname(), std::vector<const char*>(1, host_name)});
204     else
205       host_group->second.push_back(host_name);
206   }
207
208   for (auto const& elm : parent2hostgroup) {
209     jed_container_t parent = container_name2container.at(elm.first);
210     add_subsets_to(subset_list, elm.second, parent);
211   }
212 }
213
214 #endif