Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Field is not used (and apparently has never been used).
[simgrid.git] / src / instr / jedule / jedule_platform.cpp
1 /* Copyright (c) 2010-2019. 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   this->children.emplace_back(child);
35   child->parent = this;
36 }
37
38 void Container::add_resources(std::vector<sg_host_t> hosts)
39 {
40   this->children.clear();
41   this->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(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 std::vector<int> Container::get_hierarchy()
69 {
70   if(this->parent != nullptr ) {
71     if (not this->parent->children.empty()) {
72       // we are in the last level
73       return this->parent->get_hierarchy();
74     } else {
75       unsigned int i =0;
76       int child_nb = -1;
77
78       for (auto const& child : this->parent->children) {
79         if (child.get() == this) {
80           child_nb = i;
81           break;
82         }
83         i++;
84       }
85
86       xbt_assert( child_nb > - 1);
87       std::vector<int> heir_list = this->parent->get_hierarchy();
88       heir_list.insert(heir_list.begin(), child_nb);
89       return heir_list;
90     }
91   } else {
92     int top_level = 0;
93     std::vector<int> heir_list = {top_level};
94     return heir_list;
95   }
96 }
97
98 std::string Container::get_hierarchy_as_string()
99 {
100   std::string output("");
101
102   std::vector<int> heir_list = this->get_hierarchy();
103
104   unsigned int length = heir_list.size();
105   unsigned int i = 0;
106   for (auto const& id : heir_list) {
107     output += std::to_string(id);
108     if( i != length-1 ) {
109       output += ".";
110     }
111   }
112
113   return output;
114 }
115
116 void Container::print_resources(FILE* jed_file)
117 {
118   unsigned int i=0;
119   xbt_assert(not this->resource_list.empty());
120
121   unsigned int res_nb = this->resource_list.size();
122   std::string resid   = this->get_hierarchy_as_string();
123
124   fprintf(jed_file, "      <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
125   for (auto const& res : this->resource_list) {
126     const char * res_name = sg_host_get_name(res);
127     fprintf(jed_file, "%s", res_name);
128     if( i != res_nb-1 ) {
129       fprintf(jed_file, "|");
130     }
131     i++;
132   }
133   fprintf(jed_file, "\" />\n");
134 }
135
136 void Container::print(FILE* jed_file)
137 {
138   fprintf(jed_file, "    <res name=\"%s\">\n", this->name.c_str());
139   if (not this->children.empty()) {
140     for (auto const& child : this->children) {
141       child->print(jed_file);
142     }
143   } else {
144     this->print_resources(jed_file);
145   }
146   fprintf(jed_file, "    </res>\n");
147 }
148
149 } // namespace jedule
150 } // namespace simgrid
151
152 static void add_subsets_to(std::vector<simgrid::jedule::Subset>& subset_list, std::vector<const char*> hostgroup,
153                            jed_container_t parent)
154 {
155   // get ids for each host
156   // sort ids
157   // compact ids
158   // create subset for each id group
159
160   xbt_assert( parent != nullptr );
161
162   std::vector<unsigned int> id_list;
163
164   for (auto const& host_name : hostgroup) {
165     xbt_assert( host_name != nullptr );
166     jed_container_t parent_cont = host2_simgrid_parent_container.at(host_name);
167     unsigned int id             = parent_cont->name2id.at(host_name);
168     id_list.push_back(id);
169   }
170   unsigned int nb_ids = id_list.size();
171   std::sort(id_list.begin(), id_list.end());
172
173   if( nb_ids > 0 ) {
174     int start = 0;
175     int pos = start;
176     for(unsigned int i=0; i<nb_ids; i++) {
177       if( id_list[i] - id_list[pos] > 1 ) {
178         subset_list.emplace_back(id_list[start], id_list[pos], parent);
179         start = i;
180
181         if( i == nb_ids-1 ) {
182           subset_list.emplace_back(id_list[i], id_list[i], parent);
183         }
184       } else {
185         if( i == nb_ids-1 ) {
186           subset_list.emplace_back(id_list[start], id_list[i], parent);
187         }
188       }
189       pos = i;
190     }
191   }
192 }
193
194 void get_resource_selection_by_hosts(std::vector<simgrid::jedule::Subset>& subset_list,
195                                      const std::vector<sg_host_t>& host_list)
196 {
197   // for each host name
198   //  find parent container
199   //  group by parent container
200   std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
201   for (auto const& host : host_list) {
202     const char *host_name = sg_host_get_name(host);
203     const simgrid::jedule::Container* parent = host2_simgrid_parent_container.at(host_name);
204     xbt_assert( parent != nullptr );
205
206     auto host_group = parent2hostgroup.find(parent->name.c_str());
207     if (host_group == parent2hostgroup.end())
208       parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
209     else
210       host_group->second.push_back(host_name);
211   }
212
213   for (auto const& elm : parent2hostgroup) {
214     jed_container_t parent = container_name2container.at(elm.first);
215     add_subsets_to(subset_list, elm.second, parent);
216   }
217 }
218
219 #endif