Logo AND Algorithmique Numérique Distribuée

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