Logo AND Algorithmique Numérique Distribuée

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