Logo AND Algorithmique Numérique Distribuée

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