Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into MC_LTL
[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         }
58
59         free(container->name);
60         free(container);
61 }
62
63 void jed_simgrid_create_container(jed_simgrid_container_t *container, char *name) {
64         xbt_assert( name != NULL );
65
66         *container = (jed_simgrid_container_t)calloc(1,sizeof(s_jed_simgrid_container_t));
67         (*container)->name = xbt_strdup(name);
68         (*container)->is_lowest = 0;
69         (*container)->container_children = xbt_dynar_new(sizeof(jed_simgrid_container_t), NULL);
70         (*container)->parent = NULL;
71
72         xbt_dict_set(container_name2container, (*container)->name, *container, NULL);
73 }
74
75
76 void jed_simgrid_add_container(jed_simgrid_container_t parent,
77                 jed_simgrid_container_t child) {
78         xbt_assert(parent != NULL);
79         xbt_assert(child != NULL);
80         xbt_dynar_push(parent->container_children, &child);
81         child->parent = parent;
82 }
83
84 void jed_simgrid_add_resources(jed_simgrid_container_t parent,
85                 xbt_dynar_t host_names) {
86
87         unsigned int iter;
88         char *host_name;
89         char *buf;
90
91         parent->is_lowest = 1;
92         xbt_dynar_free(&parent->container_children);
93         parent->container_children = NULL;
94         parent->name2id = xbt_dict_new();
95         parent->last_id = 0;
96         parent->resource_list = xbt_dynar_new(sizeof(char *), NULL);
97
98         xbt_dynar_sort (host_names,     &compare_hostnames);
99
100         xbt_dynar_foreach(host_names, iter, host_name) {
101                 buf = bprintf("%d", parent->last_id);
102                 (parent->last_id)++;
103                 xbt_dict_set(parent->name2id, host_name, buf, xbt_free);
104                 xbt_dict_set(host2_simgrid_parent_container, host_name, parent, NULL);
105                 xbt_dynar_push(parent->resource_list, &host_name);
106         }
107
108 }
109
110 static void add_subset_to(xbt_dynar_t subset_list, int start, int end,
111                 jed_simgrid_container_t parent) {
112
113         jed_res_subset_t subset;
114
115         xbt_assert( subset_list != NULL );
116         xbt_assert( parent != NULL );
117
118         // printf(">>> start=%d end=%d\n", start, end);
119
120         subset = (jed_res_subset_t)calloc(1,sizeof(s_jed_res_subset_t));
121         subset->start_idx = start;
122         subset->nres      = end-start+1;
123         subset->parent    = parent;
124
125         xbt_dynar_push(subset_list, &subset);
126
127 }
128
129 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
130                 jed_simgrid_container_t parent) {
131
132         unsigned int iter;
133         char *host_name;
134         xbt_dynar_t id_list;
135         int *id_ar;
136         int nb_ids;
137         char *id_str;
138
139         // get ids for each host
140         // sort ids
141         // compact ids
142         // create subset for each id group
143
144         xbt_assert( host2_simgrid_parent_container != NULL );
145         xbt_assert( subset_list != NULL );
146         xbt_assert( hostgroup != NULL );
147         xbt_assert( parent != NULL );
148
149         id_list = xbt_dynar_new(sizeof(char *), NULL);
150
151         xbt_dynar_foreach(hostgroup, iter, host_name) {
152                 jed_simgrid_container_t parent;
153                 xbt_assert( host_name != NULL );
154                 parent = xbt_dict_get(host2_simgrid_parent_container, host_name);
155                 id_str = xbt_dict_get(parent->name2id, host_name);
156                 xbt_dynar_push(id_list, &id_str);
157         }
158
159         nb_ids = xbt_dynar_length(id_list);
160         id_ar = (int*)calloc(nb_ids, sizeof(int));
161         xbt_dynar_foreach(id_list, iter, id_str) {
162                 id_ar[iter] = atoi(id_str);
163         }
164
165         qsort (id_ar, nb_ids, sizeof(int), &compare_ids);
166
167         if( nb_ids > 0 ) {
168                 int start = 0;
169                 int pos;
170                 int i;
171
172                 pos = start;
173                 for(i=0; i<nb_ids; i++) {
174
175                         if( id_ar[i] - id_ar[pos] > 1 ) {
176
177                                 add_subset_to( subset_list, id_ar[start], id_ar[pos], parent );
178                                 start = i;
179
180                                 if( i == nb_ids-1 ) {
181                                         add_subset_to( subset_list, id_ar[i], id_ar[i], parent );
182                                 }
183
184                         } else {
185                                 if( i == nb_ids-1 ) {
186                                         add_subset_to( subset_list, id_ar[start], id_ar[i], parent );
187                                 }
188                         }
189
190                         pos = i;
191                 }
192         }
193
194         free(id_ar);
195         xbt_dynar_free(&id_list);
196
197
198 }
199
200 void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list,
201                 xbt_dynar_t host_names) {
202
203         char *host_name;
204         unsigned int iter;
205         xbt_dict_t parent2hostgroup;  // group hosts by parent
206
207         parent2hostgroup = xbt_dict_new();
208
209         xbt_assert( host_names != NULL );
210
211         // for each host name
212         //  find parent container
213         //  group by parent container
214
215         xbt_dynar_foreach(host_names, iter, host_name) {
216                 jed_simgrid_container_t parent;
217                 xbt_dynar_t hostgroup;
218
219                 //printf("checking %s \n", host_name);
220
221                 parent = xbt_dict_get(host2_simgrid_parent_container, host_name);
222                 xbt_assert( parent != NULL );
223
224                 hostgroup = xbt_dict_get_or_null (parent2hostgroup, parent->name);
225                 if( hostgroup == NULL ) {
226                         hostgroup = xbt_dynar_new(sizeof(char*), NULL);
227                         xbt_dict_set(parent2hostgroup, parent->name, hostgroup, NULL);
228                 }
229
230                 xbt_dynar_push(hostgroup, &host_name);
231         }
232
233         {
234                 xbt_dict_cursor_t cursor=NULL;
235                 char *parent_name;
236                 xbt_dynar_t hostgroup;
237                 jed_simgrid_container_t parent;
238
239                 xbt_dict_foreach(parent2hostgroup,cursor,parent_name,hostgroup) {
240                         parent = xbt_dict_get(container_name2container, parent_name);
241                         // printf("subset parent >>> %s\n", parent->name);
242                         add_subsets_to(subset_list, hostgroup, parent);
243                 }
244
245         }
246
247         xbt_dict_free(&parent2hostgroup);
248
249 }
250
251
252 void jedule_add_meta_info(jedule_t jedule, char *key, char *value) {
253
254         char *val_cp;
255
256         xbt_assert(key != NULL);
257         xbt_assert(value != NULL);
258
259         val_cp = strdup(value);
260         xbt_dict_set(jedule->jedule_meta_info, key, val_cp, NULL);
261 }
262
263 void jed_create_jedule(jedule_t *jedule) {
264         *jedule = (jedule_t)calloc(1,sizeof(s_jedule_t));
265         host2_simgrid_parent_container = xbt_dict_new();
266         container_name2container       = xbt_dict_new();
267         (*jedule)->jedule_meta_info    = xbt_dict_new();
268 }
269
270 void jed_free_jedule(jedule_t jedule) {
271
272         jed_free_container(jedule->root_container);
273
274         xbt_dict_free(&jedule->jedule_meta_info);
275         free(jedule);
276
277         xbt_dict_free(&host2_simgrid_parent_container);
278         xbt_dict_free(&container_name2container);
279 }
280
281 #endif