Logo AND Algorithmique Numérique Distribuée

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