Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
13d1ef6ca61eebf52eb2c39da9cd136276d58610
[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), parent(parent)
23 {
24   nres=end_idx-start_idx+1;
25 }
26
27 Container::Container(const std::string& name) : name(name)
28 {
29   container_name2container.insert({this->name, this});
30 }
31
32 Container::~Container()
33 {
34   for (auto const& child : this->children)
35     delete child;
36 }
37
38 void Container::add_child(jed_container_t child)
39 {
40   xbt_assert(child != nullptr);
41   this->children.push_back(child);
42   child->parent = this;
43 }
44
45 void Container::add_resources(std::vector<sg_host_t> hosts)
46 {
47   this->is_lowest_ = 1;
48   this->children.clear();
49   this->last_id_ = 0;
50
51   for (auto const& host : hosts) {
52     const char *host_name = sg_host_get_name(host);
53     this->name2id.insert({host_name, this->last_id_});
54     (this->last_id_)++;
55     host2_simgrid_parent_container.insert({host_name, this});
56     this->resource_list.push_back(host);
57   }
58 }
59
60 void Container::create_hierarchy(sg_netzone_t from_as)
61 {
62
63   if (from_as->get_children().empty()) {
64     // I am no AS
65     // add hosts to jedule platform
66     std::vector<sg_host_t> table = from_as->get_all_hosts();
67     this->add_resources(table);
68   } else {
69     for (auto const& nz : from_as->get_children()) {
70       jed_container_t child_container = new simgrid::jedule::Container(nz->get_name());
71       this->add_child(child_container);
72       child_container->create_hierarchy(nz);
73     }
74   }
75 }
76
77 std::vector<int> Container::get_hierarchy()
78 {
79   if(this->parent != nullptr ) {
80
81     if (not this->parent->children.empty()) {
82       // we are in the last level
83       return this->parent->get_hierarchy();
84     } else {
85       unsigned int i =0;
86       int child_nb = -1;
87
88       for (auto const& child : this->parent->children) {
89         if( child == this) {
90           child_nb = i;
91           break;
92         }
93         i++;
94       }
95
96       xbt_assert( child_nb > - 1);
97       std::vector<int> heir_list = this->parent->get_hierarchy();
98       heir_list.insert(heir_list.begin(), child_nb);
99       return heir_list;
100     }
101   } else {
102     int top_level = 0;
103     std::vector<int> heir_list = {top_level};
104     return heir_list;
105   }
106 }
107
108 std::string Container::get_hierarchy_as_string()
109 {
110   std::string output("");
111
112   std::vector<int> heir_list = this->get_hierarchy();
113
114   unsigned int length = heir_list.size();
115   unsigned int i = 0;
116   for (auto const& id : heir_list) {
117     output += std::to_string(id);
118     if( i != length-1 ) {
119       output += ".";
120     }
121   }
122
123   return output;
124 }
125
126 void Container::print_resources(FILE* jed_file)
127 {
128   unsigned int i=0;
129   xbt_assert(not this->resource_list.empty());
130
131   unsigned int res_nb = this->resource_list.size();
132   std::string resid   = this->get_hierarchy_as_string();
133
134   fprintf(jed_file, "      <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
135   for (auto const& res : this->resource_list) {
136     const char * res_name = sg_host_get_name(res);
137     fprintf(jed_file, "%s", res_name);
138     if( i != res_nb-1 ) {
139       fprintf(jed_file, "|");
140     }
141     i++;
142   }
143   fprintf(jed_file, "\" />\n");
144 }
145
146 void Container::print(FILE* jed_file)
147 {
148   fprintf(jed_file, "    <res name=\"%s\">\n", this->name.c_str());
149   if (not this->children.empty()) {
150     for (auto const& child : this->children) {
151       child->print(jed_file);
152     }
153   } else {
154     this->print_resources(jed_file);
155   }
156   fprintf(jed_file, "    </res>\n");
157 }
158
159 }
160 }
161
162 static void add_subsets_to(std::vector<simgrid::jedule::Subset>& subset_list, std::vector<const char*> hostgroup,
163                            jed_container_t parent)
164 {
165   // get ids for each host
166   // sort ids
167   // compact ids
168   // create subset for each id group
169
170   xbt_assert( parent != nullptr );
171
172   std::vector<unsigned int> id_list;
173
174   for (auto const& host_name : hostgroup) {
175     xbt_assert( host_name != nullptr );
176     jed_container_t parent_cont = host2_simgrid_parent_container.at(host_name);
177     unsigned int id             = parent_cont->name2id.at(host_name);
178     id_list.push_back(id);
179   }
180   unsigned int nb_ids = id_list.size();
181   std::sort(id_list.begin(), id_list.end());
182
183   if( nb_ids > 0 ) {
184     int start = 0;
185     int pos = start;
186     for(unsigned int i=0; i<nb_ids; i++) {
187       if( id_list[i] - id_list[pos] > 1 ) {
188         subset_list.emplace_back(id_list[start], id_list[pos], parent);
189         start = i;
190
191         if( i == nb_ids-1 ) {
192           subset_list.emplace_back(id_list[i], id_list[i], parent);
193         }
194       } else {
195         if( i == nb_ids-1 ) {
196           subset_list.emplace_back(id_list[start], id_list[i], parent);
197         }
198       }
199       pos = i;
200     }
201   }
202
203 }
204
205 void get_resource_selection_by_hosts(std::vector<simgrid::jedule::Subset>& subset_list,
206                                      const std::vector<sg_host_t>& host_list)
207 {
208   // for each host name
209   //  find parent container
210   //  group by parent container
211   std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
212   for (auto const& host : host_list) {
213     const char *host_name = sg_host_get_name(host);
214     jed_container_t parent = host2_simgrid_parent_container.at(host_name);
215     xbt_assert( parent != nullptr );
216
217     auto host_group = parent2hostgroup.find(parent->name.c_str());
218     if (host_group == parent2hostgroup.end())
219       parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
220     else
221       host_group->second.push_back(host_name);
222   }
223
224   for (auto const& elm : parent2hostgroup) {
225     jed_container_t parent = container_name2container.at(elm.first);
226     add_subsets_to(subset_list, elm.second, parent);
227   }
228 }
229
230 #endif