Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename the plugins from the command line, and document it
[simgrid.git] / src / instr / jedule / jedule_platform.cpp
1 /* Copyright (c) 2010-2018. 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/host.h"
8 #include "simgrid/s4u/NetZone.hpp"
9 #include "xbt/asserts.h"
10 #include <algorithm>
11
12 #if SIMGRID_HAVE_JEDULE
13
14 namespace {
15 std::unordered_map<const char*, jed_container_t> host2_simgrid_parent_container;
16 std::unordered_map<std::string, jed_container_t> container_name2container;
17 }
18
19 namespace simgrid {
20 namespace jedule {
21 Subset::Subset(int start_idx, int end_idx, Container* parent)
22 : start_idx(start_idx), parent(parent)
23 {
24   nres=end_idx-start_idx+1;
25 }
26
27
28 Container::Container(std::string name): name(name)
29 {
30   container_name2container.insert({this->name, this});
31 }
32
33 Container::~Container()
34 {
35   if (not this->children.empty())
36     for (auto const& child : this->children)
37       delete child;
38 }
39
40 void Container::addChild(jed_container_t child)
41 {
42   xbt_assert(child != nullptr);
43   this->children.push_back(child);
44   child->parent = this;
45 }
46
47 void Container::addResources(std::vector<sg_host_t> hosts)
48 {
49   this->is_lowest = 1;
50   this->children.clear();
51   this->last_id = 0;
52
53   //FIXME do we need to sort?: xbt_dynar_sort_strings(host_names);
54
55   for (auto const& host : hosts) {
56     const char *host_name = sg_host_get_name(host);
57     this->name2id.insert({host_name, this->last_id});
58     (this->last_id)++;
59     host2_simgrid_parent_container.insert({host_name, this});
60     this->resource_list.push_back(host);
61   }
62 }
63
64 void Container::createHierarchy(sg_netzone_t from_as)
65 {
66
67   if (from_as->getChildren()->empty()) {
68     // I am no AS
69     // add hosts to jedule platform
70     std::vector<sg_host_t> table;
71     from_as->getHosts(&table);
72     this->addResources(table);
73   } else {
74     for (auto const& nz : *from_as->getChildren()) {
75       jed_container_t child_container = new simgrid::jedule::Container(std::string(nz->get_cname()));
76       this->addChild(child_container);
77       child_container->createHierarchy(nz);
78     }
79   }
80 }
81
82 std::vector<int> Container::getHierarchy()
83 {
84   if(this->parent != nullptr ) {
85
86     if (not 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 const& 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 const& 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(not 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 const& 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   fprintf(jed_file, "    <res name=\"%s\">\n", this->name.c_str());
154   if (not this->children.empty()) {
155     for (auto const& child : this->children) {
156       child->print(jed_file);
157     }
158   } else {
159     this->printResources(jed_file);
160   }
161   fprintf(jed_file, "    </res>\n");
162 }
163
164 }
165 }
166
167 static void add_subsets_to(std::vector<jed_subset_t> *subset_list, std::vector<const char*> hostgroup, jed_container_t parent)
168 {
169   // get ids for each host
170   // sort ids
171   // compact ids
172   // create subset for each id group
173
174   xbt_assert( parent != nullptr );
175
176   std::vector<unsigned int> id_list;
177
178   for (auto const& host_name : hostgroup) {
179     xbt_assert( host_name != nullptr );
180     jed_container_t parent = host2_simgrid_parent_container.at(host_name);
181     unsigned int id = parent->name2id.at(host_name);
182     id_list.push_back(id);
183   }
184   unsigned int nb_ids = id_list.size();
185   std::sort(id_list.begin(), id_list.end());
186
187   if( nb_ids > 0 ) {
188     int start = 0;
189     int pos = start;
190     for(unsigned int i=0; i<nb_ids; i++) {
191       if( id_list[i] - id_list[pos] > 1 ) {
192         subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[pos], parent));
193         start = i;
194
195         if( i == nb_ids-1 ) {
196          subset_list->push_back(new simgrid::jedule::Subset(id_list[i], id_list[i], parent));
197         }
198       } else {
199         if( i == nb_ids-1 ) {
200           subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[i], parent));
201         }
202       }
203       pos = i;
204     }
205   }
206
207 }
208
209 void get_resource_selection_by_hosts(std::vector<jed_subset_t> *subset_list, std::vector<sg_host_t> *host_list)
210 {
211   xbt_assert( host_list != nullptr );
212   // for each host name
213   //  find parent container
214   //  group by parent container
215   std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
216   for (auto const& host : *host_list) {
217     const char *host_name = sg_host_get_name(host);
218     jed_container_t parent = host2_simgrid_parent_container.at(host_name);
219     xbt_assert( parent != nullptr );
220
221     auto host_group = parent2hostgroup.find(parent->name.c_str());
222     if (host_group == parent2hostgroup.end())
223       parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
224     else
225       host_group->second.push_back(host_name);
226   }
227
228   for (auto const& elm : parent2hostgroup) {
229     jed_container_t parent = container_name2container.at(elm.first);
230     add_subsets_to(subset_list, elm.second, parent);
231   }
232 }
233
234 #endif