Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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 static int compare_ids(const void *num1, const void *num2) {
176   return *((int*) num1) - *((int*) num2);
177 }
178
179 static void jed_free_container(jed_container_t container) {
180   if(!container->children.empty()) {
181     for (auto child: container->children){
182       jed_free_container(child);
183     }
184   }
185   delete container;
186 }
187
188 static void add_subset_to(xbt_dynar_t subset_list, int start, int end, jed_container_t parent) {
189   jed_res_subset_t subset;
190
191   xbt_assert( subset_list != nullptr );
192   xbt_assert( parent != nullptr );
193
194   subset = xbt_new0(s_jed_res_subset_t,1);
195   subset->start_idx = start;
196   subset->nres      = end-start+1;
197   subset->parent    = parent;
198
199   xbt_dynar_push(subset_list, &subset);
200 }
201
202 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup, jed_container_t parent) {
203   unsigned int iter;
204   char *host_name;
205   int id;
206
207   // get ids for each host
208   // sort ids
209   // compact ids
210   // create subset for each id group
211
212   xbt_assert( host2_simgrid_parent_container != nullptr );
213   xbt_assert( subset_list != nullptr );
214   xbt_assert( hostgroup != nullptr );
215   xbt_assert( parent != nullptr );
216
217   xbt_dynar_t id_list = xbt_dynar_new(sizeof(int), nullptr);
218
219   xbt_dynar_foreach(hostgroup, iter, host_name) {
220     jed_container_t parent;
221     xbt_assert( host_name != nullptr );
222     parent = (jed_container_t)xbt_dict_get(host2_simgrid_parent_container, host_name);
223     id = parent->name2id.at(host_name);
224     xbt_dynar_push(id_list, &id);
225   }
226   int nb_ids = xbt_dynar_length(id_list);
227   xbt_dynar_sort(id_list, &compare_ids);
228
229   int *id_ar = static_cast<int*>(xbt_dynar_to_array(id_list));
230
231   if( nb_ids > 0 ) {
232     int start = 0;
233
234     int pos = start;
235     for(int i=0; i<nb_ids; i++) {
236       if( id_ar[i] - id_ar[pos] > 1 ) {
237         add_subset_to( subset_list, id_ar[start], id_ar[pos], parent );
238         start = i;
239
240         if( i == nb_ids-1 ) {
241           add_subset_to( subset_list, id_ar[i], id_ar[i], parent );
242         }
243       } else {
244         if( i == nb_ids-1 ) {
245           add_subset_to( subset_list, id_ar[start], id_ar[i], parent );
246         }
247       }
248       pos = i;
249     }
250   }
251
252   xbt_free(id_ar);
253 }
254
255 void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list, std::vector<sg_host_t> *host_list) {
256   std::unordered_map<const char*, xbt_dynar_t> parent2hostgroup;  // group hosts by parent
257
258   xbt_assert( host_list != nullptr );
259
260   // for each host name
261   //  find parent container
262   //  group by parent container
263
264   for (auto host: *host_list) {
265     const char *host_name = sg_host_get_name(host);
266     jed_container_t parent = (jed_container_t)xbt_dict_get(host2_simgrid_parent_container, host_name);
267     xbt_assert( parent != nullptr );
268
269     auto host_group = parent2hostgroup.find(parent->name.c_str());
270     if (host_group == parent2hostgroup.end()){
271       xbt_dynar_t group = xbt_dynar_new(sizeof(char*), nullptr);
272       xbt_dynar_push(group, &host_name);
273       parent2hostgroup.insert({parent->name.c_str(), group});
274     } else {
275       xbt_dynar_push(host_group->second, &host_name);
276     }
277   }
278
279   for (auto elm: parent2hostgroup) {
280     jed_container_t parent = (jed_container_t)xbt_dict_get(container_name2container, elm.first);
281     add_subsets_to(subset_list, elm.second, parent);
282     xbt_dynar_free_container(&elm.second);
283   }
284 }
285
286 void jedule_add_meta_info(jedule_t jedule, char *key, char *value) {
287   xbt_assert(key != nullptr);
288   xbt_assert(value != nullptr);
289
290   jedule->jedule_meta_info.insert({key, value});
291 }
292
293 void jed_create_jedule(jedule_t *jedule) {
294   *jedule = xbt_new0(s_jedule_t,1);
295   host2_simgrid_parent_container = xbt_dict_new_homogeneous(nullptr);
296   container_name2container       = xbt_dict_new_homogeneous(nullptr);
297 }
298
299 void jed_free_jedule(jedule_t jedule) {
300   jed_free_container(jedule->root_container);
301
302   xbt_free(jedule);
303
304   xbt_dict_free(&host2_simgrid_parent_container);
305   xbt_dict_free(&container_name2container);
306 }
307 #endif