Logo AND Algorithmique Numérique Distribuée

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