Logo AND Algorithmique Numérique Distribuée

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