Logo AND Algorithmique Numérique Distribuée

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