Logo AND Algorithmique Numérique Distribuée

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