Logo AND Algorithmique Numérique Distribuée

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