Logo AND Algorithmique Numérique Distribuée

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