Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
08fd7e73a8e872f5c5171abb428721845ec12ee7
[simgrid.git] / src / instr / jedule / jedule_platform.c
1 /* Copyright (c) 2010-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include "xbt/asserts.h"
10 #include "xbt/dynar.h"
11
12 #include "simgrid/jedule/jedule_platform.h"
13
14 #ifdef HAVE_JEDULE
15
16 /********************************************************************/
17
18 static xbt_dict_t host2_simgrid_parent_container;
19 static xbt_dict_t container_name2container;
20
21 /********************************************************************/
22
23 static void add_subset_to(xbt_dynar_t subset_list, int start, int end,
24     jed_simgrid_container_t parent);
25
26 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
27     jed_simgrid_container_t parent);
28
29 static void jed_free_container(jed_simgrid_container_t container);
30
31 /********************************************************************/
32
33 static int compare_hostnames(const void *host1, const void *host2) {
34   const char *hp1 = *((const char**) host1);
35   const char *hp2 = *((const char**) host2);
36   return strcmp (hp1, hp2);
37 }
38
39 static int compare_ids(const void *num1, const void *num2) {
40   int *i1 = (int*) num1;
41   int *i2 = (int*) num2;
42   return *i1 - *i2;
43 }
44
45 static void jed_free_container(jed_simgrid_container_t container) {
46
47   xbt_dict_free(&container->name2id);
48   xbt_dynar_free(&container->resource_list);
49
50   if( container->container_children != NULL ) {
51     unsigned int iter;
52     jed_simgrid_container_t child_container;
53     xbt_dynar_foreach(container->container_children, iter, child_container) {
54       jed_free_container(child_container);
55     }
56     xbt_dynar_free(&container->container_children);
57   }
58
59   xbt_free(container->name);
60   xbt_free(container);
61 }
62
63 void jed_simgrid_create_container(jed_simgrid_container_t *container,
64                                   const char *name)
65 {
66   xbt_assert( name != NULL );
67
68   *container = xbt_new0(s_jed_simgrid_container_t,1);
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;
92
93   parent->is_lowest = 1;
94   xbt_dynar_free(&parent->container_children);
95   parent->container_children = NULL;
96   parent->name2id = xbt_dict_new_homogeneous(xbt_free_f);
97   parent->last_id = 0;
98   parent->resource_list = xbt_dynar_new(sizeof(char *), NULL);
99
100   xbt_dynar_sort (host_names,  &compare_hostnames);
101
102   xbt_dynar_foreach(host_names, iter, host_name) {
103     buf = bprintf("%d", parent->last_id);
104     (parent->last_id)++;
105     xbt_dict_set(parent->name2id, host_name, buf, 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 = xbt_new0(s_jed_res_subset_t,1);
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 = xbt_new0(int,nb_ids);
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_homogeneous(NULL);
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     xbt_dynar_free(&hostgroup);
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 = xbt_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 = xbt_new0(s_jedule_t,1);
267   host2_simgrid_parent_container = xbt_dict_new_homogeneous(NULL);
268   container_name2container       = xbt_dict_new_homogeneous(NULL);
269   (*jedule)->jedule_meta_info    = xbt_dict_new_homogeneous(NULL);
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   xbt_free(jedule);
278
279   xbt_dict_free(&host2_simgrid_parent_container);
280   xbt_dict_free(&container_name2container);
281 }
282
283 #endif