Logo AND Algorithmique Numérique Distribuée

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