Logo AND Algorithmique Numérique Distribuée

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