Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into hypervisor
[simgrid.git] / src / instr / jedule / jedule_platform.c
1 /*
2  * jed_simgrid_platform.c
3  *
4  *  Created on: Nov 30, 2010
5  *      Author: sascha
6  */
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include "xbt/asserts.h"
11 #include "xbt/dynar.h"
12
13 #include "instr/jedule/jedule_platform.h"
14
15 #ifdef HAVE_JEDULE
16
17 /********************************************************************/
18
19 static xbt_dict_t host2_simgrid_parent_container;
20 static xbt_dict_t container_name2container;
21
22 /********************************************************************/
23
24 static void add_subset_to(xbt_dynar_t subset_list, int start, int end,
25     jed_simgrid_container_t parent);
26
27 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
28     jed_simgrid_container_t parent);
29
30 static void jed_free_container(jed_simgrid_container_t container);
31
32 /********************************************************************/
33
34 static int compare_hostnames(const void *host1, const void *host2) {
35   const char *hp1 = *((const char**) host1);
36   const char *hp2 = *((const char**) host2);
37   return strcmp (hp1, hp2);
38 }
39
40 static int compare_ids(const void *num1, const void *num2) {
41   int *i1 = (int*) num1;
42   int *i2 = (int*) num2;
43   return *i1 - *i2;
44 }
45
46 static void jed_free_container(jed_simgrid_container_t container) {
47
48   xbt_dict_free(&container->name2id);
49   xbt_dynar_free(&container->resource_list);
50
51   if( container->container_children != NULL ) {
52     unsigned int iter;
53     jed_simgrid_container_t child_container;
54     xbt_dynar_foreach(container->container_children, iter, child_container) {
55       jed_free_container(child_container);
56     }
57     xbt_dynar_free(&container->container_children);
58   }
59
60   xbt_free(container->name);
61   xbt_free(container);
62 }
63
64 void jed_simgrid_create_container(jed_simgrid_container_t *container, char *name) {
65   xbt_assert( name != NULL );
66
67   *container = xbt_new0(s_jed_simgrid_container_t,1);
68   (*container)->name = xbt_strdup(name);
69   (*container)->is_lowest = 0;
70   (*container)->container_children = xbt_dynar_new(sizeof(jed_simgrid_container_t), NULL);
71   (*container)->parent = NULL;
72
73   xbt_dict_set(container_name2container, (*container)->name, *container, NULL);
74 }
75
76
77 void jed_simgrid_add_container(jed_simgrid_container_t parent,
78     jed_simgrid_container_t child) {
79   xbt_assert(parent != NULL);
80   xbt_assert(child != NULL);
81   xbt_dynar_push(parent->container_children, &child);
82   child->parent = parent;
83 }
84
85 void jed_simgrid_add_resources(jed_simgrid_container_t parent,
86     xbt_dynar_t host_names) {
87
88   unsigned int iter;
89   char *host_name;
90   char *buf;
91
92   parent->is_lowest = 1;
93   xbt_dynar_free(&parent->container_children);
94   parent->container_children = NULL;
95   parent->name2id = xbt_dict_new_homogeneous(xbt_free);
96   parent->last_id = 0;
97   parent->resource_list = xbt_dynar_new(sizeof(char *), NULL);
98
99   xbt_dynar_sort (host_names,  &compare_hostnames);
100
101   xbt_dynar_foreach(host_names, iter, host_name) {
102     buf = bprintf("%d", parent->last_id);
103     (parent->last_id)++;
104     xbt_dict_set(parent->name2id, host_name, buf, NULL);
105     xbt_dict_set(host2_simgrid_parent_container, host_name, parent, NULL);
106     xbt_dynar_push(parent->resource_list, &host_name);
107   }
108
109 }
110
111 static void add_subset_to(xbt_dynar_t subset_list, int start, int end,
112     jed_simgrid_container_t parent) {
113
114   jed_res_subset_t subset;
115
116   xbt_assert( subset_list != NULL );
117   xbt_assert( parent != NULL );
118
119   // printf(">>> start=%d end=%d\n", start, end);
120
121   subset = xbt_new0(s_jed_res_subset_t,1);
122   subset->start_idx = start;
123   subset->nres      = end-start+1;
124   subset->parent    = parent;
125
126   xbt_dynar_push(subset_list, &subset);
127
128 }
129
130 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
131     jed_simgrid_container_t parent) {
132
133   unsigned int iter;
134   char *host_name;
135   xbt_dynar_t id_list;
136   int *id_ar;
137   int nb_ids;
138   char *id_str;
139
140   // get ids for each host
141   // sort ids
142   // compact ids
143   // create subset for each id group
144
145   xbt_assert( host2_simgrid_parent_container != NULL );
146   xbt_assert( subset_list != NULL );
147   xbt_assert( hostgroup != NULL );
148   xbt_assert( parent != NULL );
149
150   id_list = xbt_dynar_new(sizeof(char *), NULL);
151
152   xbt_dynar_foreach(hostgroup, iter, host_name) {
153     jed_simgrid_container_t parent;
154     xbt_assert( host_name != NULL );
155     parent = xbt_dict_get(host2_simgrid_parent_container, host_name);
156     id_str = xbt_dict_get(parent->name2id, host_name);
157     xbt_dynar_push(id_list, &id_str);
158   }
159
160   nb_ids = xbt_dynar_length(id_list);
161   id_ar = xbt_new0(int,nb_ids);
162   xbt_dynar_foreach(id_list, iter, id_str) {
163     id_ar[iter] = atoi(id_str);
164   }
165
166   qsort (id_ar, nb_ids, sizeof(int), &compare_ids);
167
168   if( nb_ids > 0 ) {
169     int start = 0;
170     int pos;
171     int i;
172
173     pos = start;
174     for(i=0; i<nb_ids; i++) {
175
176       if( id_ar[i] - id_ar[pos] > 1 ) {
177
178         add_subset_to( subset_list, id_ar[start], id_ar[pos], parent );
179         start = i;
180
181         if( i == nb_ids-1 ) {
182           add_subset_to( subset_list, id_ar[i], id_ar[i], parent );
183         }
184
185       } else {
186         if( i == nb_ids-1 ) {
187           add_subset_to( subset_list, id_ar[start], id_ar[i], parent );
188         }
189       }
190
191       pos = i;
192     }
193   }
194
195   free(id_ar);
196   xbt_dynar_free(&id_list);
197
198
199 }
200
201 void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list,
202     xbt_dynar_t host_names) {
203
204   char *host_name;
205   unsigned int iter;
206   xbt_dict_t parent2hostgroup;  // group hosts by parent
207
208   parent2hostgroup = xbt_dict_new_homogeneous(NULL);
209
210   xbt_assert( host_names != NULL );
211
212   // for each host name
213   //  find parent container
214   //  group by parent container
215
216   xbt_dynar_foreach(host_names, iter, host_name) {
217     jed_simgrid_container_t parent;
218     xbt_dynar_t hostgroup;
219
220     //printf("checking %s \n", host_name);
221
222     parent = xbt_dict_get(host2_simgrid_parent_container, host_name);
223     xbt_assert( parent != NULL );
224
225     hostgroup = xbt_dict_get_or_null (parent2hostgroup, parent->name);
226     if( hostgroup == NULL ) {
227       hostgroup = xbt_dynar_new(sizeof(char*), NULL);
228       xbt_dict_set(parent2hostgroup, parent->name, hostgroup, NULL);
229     }
230
231     xbt_dynar_push(hostgroup, &host_name);
232   }
233
234   {
235     xbt_dict_cursor_t cursor=NULL;
236     char *parent_name;
237     xbt_dynar_t hostgroup;
238     jed_simgrid_container_t parent;
239
240     xbt_dict_foreach(parent2hostgroup,cursor,parent_name,hostgroup) {
241       parent = xbt_dict_get(container_name2container, parent_name);
242       // printf("subset parent >>> %s\n", parent->name);
243       add_subsets_to(subset_list, hostgroup, parent);
244     }
245     xbt_dynar_free(&hostgroup);
246   }
247
248   xbt_dict_free(&parent2hostgroup);
249
250 }
251
252
253 void jedule_add_meta_info(jedule_t jedule, char *key, char *value) {
254
255   char *val_cp;
256
257   xbt_assert(key != NULL);
258   xbt_assert(value != NULL);
259
260   val_cp = strdup(value);
261   xbt_dict_set(jedule->jedule_meta_info, key, val_cp, NULL);
262 }
263
264 void jed_create_jedule(jedule_t *jedule) {
265   *jedule = xbt_new0(s_jedule_t,1);
266   host2_simgrid_parent_container = xbt_dict_new_homogeneous(NULL);
267   container_name2container       = xbt_dict_new_homogeneous(NULL);
268   (*jedule)->jedule_meta_info    = xbt_dict_new_homogeneous(NULL);
269 }
270
271 void jed_free_jedule(jedule_t jedule) {
272
273   jed_free_container(jedule->root_container);
274
275   xbt_dict_free(&jedule->jedule_meta_info);
276   xbt_free(jedule);
277
278   xbt_dict_free(&host2_simgrid_parent_container);
279   xbt_dict_free(&container_name2container);
280 }
281
282 #endif