Logo AND Algorithmique Numérique Distribuée

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