Logo AND Algorithmique Numérique Distribuée

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