Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please codacy: use long form of negation in C++
[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 child: this->children)
31       delete child;
32 }
33
34 void Container::addChild(jed_container_t child)
35 {
36   xbt_assert(this != nullptr);
37   xbt_assert(child != nullptr);
38   this->children.push_back(child);
39   child->parent = this;
40 }
41
42 void Container::addResources(std::vector<sg_host_t> hosts)
43 {
44   this->is_lowest = 1;
45   this->children.clear();
46   this->last_id = 0;
47
48   //FIXME do we need to sort?: xbt_dynar_sort_strings(host_names);
49
50   for (auto host : hosts) {
51     const char *host_name = sg_host_get_name(host);
52     this->name2id.insert({host_name, this->last_id});
53     (this->last_id)++;
54     host2_simgrid_parent_container.insert({host_name, this});
55     this->resource_list.push_back(host);
56   }
57 }
58
59 void Container::createHierarchy(sg_netzone_t from_as)
60 {
61
62   if (from_as->children()->empty()) {
63     // I am no AS
64     // add hosts to jedule platform
65     std::vector<sg_host_t> table;
66     from_as->hosts(&table);
67     this->addResources(table);
68   } else {
69     for (auto nz : *from_as->children()) {
70       jed_container_t child_container = new simgrid::jedule::Container(std::string(nz->name()));
71       this->addChild(child_container);
72       child_container->createHierarchy(nz);
73     }
74   }
75 }
76
77 std::vector<int> Container::getHierarchy()
78 {
79   xbt_assert( this!= nullptr );
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->getHierarchy();
86     } else {
87       unsigned int i =0;
88       int child_nb = -1;
89
90       for (auto 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->getHierarchy();
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::getHierarchyAsString()
111 {
112   std::string output("");
113
114   std::vector<int> heir_list = this->getHierarchy();
115
116   unsigned int length = heir_list.size();
117   unsigned int i = 0;
118   for (auto 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::printResources(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->getHierarchyAsString();
135
136   fprintf(jed_file, "      <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
137   for (auto 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   xbt_assert( this != nullptr );
151   fprintf(jed_file, "    <res name=\"%s\">\n", this->name.c_str());
152   if (not this->children.empty()) {
153     for (auto child: this->children) {
154       child->print(jed_file);
155     }
156   } else {
157     this->printResources(jed_file);
158   }
159   fprintf(jed_file, "    </res>\n");
160 }
161
162 }
163 }
164
165 static void add_subsets_to(std::vector<jed_subset_t> *subset_list, std::vector<const char*> hostgroup, jed_container_t parent)
166 {
167   // get ids for each host
168   // sort ids
169   // compact ids
170   // create subset for each id group
171
172   xbt_assert( parent != nullptr );
173
174   std::vector<unsigned int> id_list;
175
176   for (auto host_name : hostgroup) {
177     xbt_assert( host_name != nullptr );
178     jed_container_t parent = host2_simgrid_parent_container.at(host_name);
179     unsigned int id = parent->name2id.at(host_name);
180     id_list.push_back(id);
181   }
182   unsigned int nb_ids = id_list.size();
183   std::sort(id_list.begin(), id_list.end());
184
185   if( nb_ids > 0 ) {
186     int start = 0;
187     int pos = start;
188     for(unsigned int i=0; i<nb_ids; i++) {
189       if( id_list[i] - id_list[pos] > 1 ) {
190         subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[pos], parent));
191         start = i;
192
193         if( i == nb_ids-1 ) {
194          subset_list->push_back(new simgrid::jedule::Subset(id_list[i], id_list[i], parent));
195         }
196       } else {
197         if( i == nb_ids-1 ) {
198           subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[i], parent));
199         }
200       }
201       pos = i;
202     }
203   }
204
205 }
206
207 void get_resource_selection_by_hosts(std::vector<jed_subset_t> *subset_list, std::vector<sg_host_t> *host_list)
208 {
209   xbt_assert( host_list != nullptr );
210   // for each host name
211   //  find parent container
212   //  group by parent container
213   std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
214   for (auto host: *host_list) {
215     const char *host_name = sg_host_get_name(host);
216     jed_container_t parent = host2_simgrid_parent_container.at(host_name);
217     xbt_assert( parent != nullptr );
218
219     auto host_group = parent2hostgroup.find(parent->name.c_str());
220     if (host_group == parent2hostgroup.end())
221       parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
222     else
223       host_group->second.push_back(host_name);
224   }
225
226   for (auto elm: parent2hostgroup) {
227     jed_container_t parent = container_name2container.at(elm.first);
228     add_subsets_to(subset_list, elm.second, parent);
229   }
230 }
231
232 #endif