Logo AND Algorithmique Numérique Distribuée

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