Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
jedule binding to SD started
[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 "jedule_platform.h"
14
15 /********************************************************************/
16
17 static xbt_dict_t host2_simgrid_parent_container;
18 static xbt_dict_t container_name2container;
19
20 /********************************************************************/
21
22 static void add_subset_to(xbt_dynar_t subset_list, int start, int end,
23                 jed_simgrid_container_t parent);
24
25 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
26                 jed_simgrid_container_t parent);
27
28 static void jed_free_container(jed_simgrid_container_t container);
29
30 /********************************************************************/
31
32 static int compare_hostnames(const void *host1, const void *host2) {
33         return strcmp ((char*)host1, (char*)host2);
34 }
35
36 static int compare_ids(const void *num1, const void *num2) {
37         int *i1 = (int*) num1;
38         int *i2 = (int*) num2;
39         return *i1 - *i2;
40 }
41
42 static void jed_free_container(jed_simgrid_container_t container) {
43
44         xbt_dict_free(&container->name2id);
45         if( container->resource_list != NULL ) {
46                 xbt_dynar_free(&container->resource_list);
47         }
48
49         if( container->container_children != NULL ) {
50                 unsigned int iter;
51                 jed_simgrid_container_t child_container;
52                 xbt_dynar_foreach(container->container_children, iter, child_container) {
53                         jed_free_container(child_container);
54                 }
55         }
56
57         free(container->name);
58         free(container);
59 }
60
61 void jed_simgrid_create_container(jed_simgrid_container_t *container, char *name) {
62         xbt_assert( name != NULL );
63
64         *container = (jed_simgrid_container_t)calloc(1,sizeof(s_jed_simgrid_container_t));
65         (*container)->name = (char*)calloc((strlen(name)+1), sizeof(char));
66         strcpy((*container)->name, name);
67         (*container)->is_lowest = 0;
68         (*container)->container_children = xbt_dynar_new(sizeof(jed_simgrid_container_t), NULL);
69         (*container)->parent = NULL;
70
71         xbt_dict_set(container_name2container, (*container)->name, *container, NULL);
72 }
73
74
75 void jed_simgrid_add_container(jed_simgrid_container_t parent,
76                 jed_simgrid_container_t child) {
77         xbt_assert(parent != NULL);
78         xbt_assert(child != NULL);
79         xbt_dynar_push(parent->container_children, &child);
80         child->parent = parent;
81 }
82
83 void jed_simgrid_add_resources(jed_simgrid_container_t parent,
84                 xbt_dynar_t host_names) {
85
86         unsigned int iter;
87         char *host_name;
88         char buf[16];
89         char *buf_copy;
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
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