Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further objectification of jedule bindings
[simgrid.git] / src / instr / jedule / jedule_platform.cpp
1 /* Copyright (c) 2010-2015. 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/As.hpp"
10 #include "simgrid/host.h"
11
12 #include "xbt/asserts.h"
13 #include "xbt/dynar.h"
14 #include "xbt/str.h"
15 #include <string>
16
17 #include <stdlib.h>
18 #include <stdio.h>
19
20 #if HAVE_JEDULE
21
22 namespace simgrid {
23 namespace jedule {
24
25 Container::Container(std::string name)
26 : name(name) {
27   container_name2container.insert({this->name, this});
28 }
29
30 Container::~Container(){
31   if(!this->children.empty()) {
32     for (auto child: this->children){
33       delete child;
34     }
35   }
36 }
37
38 void Container::addChild(jed_container_t child){
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   this->is_lowest = 1;
47   this->children.clear();
48   this->last_id = 0;
49
50   //FIXME do we need to sort?: xbt_dynar_sort_strings(host_names);
51
52   for (auto host : hosts) {
53     const char *host_name = sg_host_get_name(host);
54     this->name2id.insert({host_name, this->last_id});
55     (this->last_id)++;
56     host2_simgrid_parent_container.insert({host_name, this});
57     this->resource_list.push_back(host);
58   }
59 }
60
61 void Container::createHierarchy(AS_t from_as){
62   xbt_dict_cursor_t cursor = nullptr;
63   char *key;
64   AS_t elem;
65   xbt_dict_t routing_sons = from_as->children();
66
67   if (xbt_dict_is_empty(routing_sons)) {
68     // I am no AS
69     // add hosts to jedule platform
70     xbt_dynar_t table = from_as->hosts();
71     unsigned int dynar_cursor;
72     sg_host_t host;
73
74     std::vector<sg_host_t> hosts;
75
76     xbt_dynar_foreach(table, dynar_cursor, host) {
77       hosts.push_back(host);
78     }
79     this->addResources(hosts);
80     xbt_dynar_free(&table);
81   } else {
82     xbt_dict_foreach(routing_sons, cursor, key, elem) {
83       jed_container_t child_container = new simgrid::jedule::Container(std::string(elem->name()));
84       this->addChild(child_container);
85       child_container->createHierarchy(elem);
86     }
87   }
88 }
89
90 std::vector<int> Container::getHierarchy() {
91   xbt_assert( this!= nullptr );
92
93   if(this->parent != nullptr ) {
94
95     if(!this->parent->children.empty()) {
96       // we are in the last level
97       return this->parent->getHierarchy();
98     } else {
99       unsigned int i =0;
100       int child_nb = -1;
101
102       for (auto child : this->parent->children) {
103         if( child == this) {
104           child_nb = i;
105           break;
106         }
107         i++;
108       }
109
110       xbt_assert( child_nb > - 1);
111       std::vector<int> heir_list = this->parent->getHierarchy();
112       heir_list.insert(heir_list.begin(), child_nb);
113       return heir_list;
114     }
115   } else {
116     int top_level = 0;
117     std::vector<int> heir_list = {top_level};
118     return heir_list;
119   }
120 }
121
122 std::string Container::getHierarchyAsString(){
123   std::string output("");
124
125   std::vector<int> heir_list = this->getHierarchy();
126
127   unsigned int length = heir_list.size();
128   unsigned int i = 0;
129   for (auto id : heir_list) {
130     output += std::to_string(id);
131     if( i != length-1 ) {
132       output += ".";
133     }
134   }
135
136   return output;
137 }
138
139 void Container::printResources(FILE * jed_file){
140   unsigned int i=0;
141   xbt_assert(!this->resource_list.empty());
142
143   unsigned int res_nb = this->resource_list.size();
144   std::string resid = this->getHierarchyAsString();
145
146   fprintf(jed_file, "      <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
147   for (auto res: this->resource_list) {
148     const char * res_name = sg_host_get_name(res);
149     fprintf(jed_file, "%s", res_name);
150     if( i != res_nb-1 ) {
151       fprintf(jed_file, "|");
152     }
153     i++;
154   }
155   fprintf(jed_file, "\" />\n");
156 }
157
158 void Container::print(FILE* jed_file) {
159   xbt_assert( this != nullptr );
160   fprintf(jed_file, "    <res name=\"%s\">\n", this->name.c_str());
161   if( !this->children.empty()){
162     for (auto child: this->children) {
163       child->print(jed_file);
164     }
165   } else {
166     this->printResources(jed_file);
167   }
168   fprintf(jed_file, "    </res>\n");
169 }
170
171 }
172 }
173
174 static int compare_ids(const void *num1, const void *num2) {
175   return *((int*) num1) - *((int*) num2);
176 }
177
178 static void add_subset_to(xbt_dynar_t subset_list, int start, int end, jed_container_t parent) {
179   jed_res_subset_t subset;
180
181   xbt_assert( subset_list != nullptr );
182   xbt_assert( parent != nullptr );
183
184   subset = xbt_new0(s_jed_res_subset_t,1);
185   subset->start_idx = start;
186   subset->nres      = end-start+1;
187   subset->parent    = parent;
188
189   xbt_dynar_push(subset_list, &subset);
190 }
191
192 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup, jed_container_t parent) {
193   unsigned int iter;
194   char *host_name;
195   int id;
196
197   // get ids for each host
198   // sort ids
199   // compact ids
200   // create subset for each id group
201
202   xbt_assert( subset_list != nullptr );
203   xbt_assert( hostgroup != nullptr );
204   xbt_assert( parent != nullptr );
205
206   xbt_dynar_t id_list = xbt_dynar_new(sizeof(int), nullptr);
207
208   xbt_dynar_foreach(hostgroup, iter, host_name) {
209     jed_container_t parent;
210     xbt_assert( host_name != nullptr );
211     parent = (jed_container_t)host2_simgrid_parent_container.at(host_name);
212     id = parent->name2id.at(host_name);
213     xbt_dynar_push(id_list, &id);
214   }
215   int nb_ids = xbt_dynar_length(id_list);
216   xbt_dynar_sort(id_list, &compare_ids);
217
218   int *id_ar = static_cast<int*>(xbt_dynar_to_array(id_list));
219
220   if( nb_ids > 0 ) {
221     int start = 0;
222
223     int pos = start;
224     for(int i=0; i<nb_ids; i++) {
225       if( id_ar[i] - id_ar[pos] > 1 ) {
226         add_subset_to( subset_list, id_ar[start], id_ar[pos], parent );
227         start = i;
228
229         if( i == nb_ids-1 ) {
230           add_subset_to( subset_list, id_ar[i], id_ar[i], parent );
231         }
232       } else {
233         if( i == nb_ids-1 ) {
234           add_subset_to( subset_list, id_ar[start], id_ar[i], parent );
235         }
236       }
237       pos = i;
238     }
239   }
240
241   xbt_free(id_ar);
242 }
243
244 void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list, std::vector<sg_host_t> *host_list) {
245   std::unordered_map<const char*, xbt_dynar_t> parent2hostgroup;  // group hosts by parent
246
247   xbt_assert( host_list != nullptr );
248
249   // for each host name
250   //  find parent container
251   //  group by parent container
252
253   for (auto host: *host_list) {
254     const char *host_name = sg_host_get_name(host);
255     jed_container_t parent = (jed_container_t)host2_simgrid_parent_container.at(host_name);
256     xbt_assert( parent != nullptr );
257
258     auto host_group = parent2hostgroup.find(parent->name.c_str());
259     if (host_group == parent2hostgroup.end()){
260       xbt_dynar_t group = xbt_dynar_new(sizeof(char*), nullptr);
261       xbt_dynar_push(group, &host_name);
262       parent2hostgroup.insert({parent->name.c_str(), group});
263     } else {
264       xbt_dynar_push(host_group->second, &host_name);
265     }
266   }
267
268   for (auto elm: parent2hostgroup) {
269     jed_container_t parent = (jed_container_t)container_name2container.at(elm.first);
270     add_subsets_to(subset_list, elm.second, parent);
271     xbt_dynar_free_container(&elm.second);
272   }
273 }
274
275
276 #endif