2 * jed_simgrid_platform.c
4 * Created on: Nov 30, 2010
10 #include "xbt/asserts.h"
11 #include "xbt/dynar.h"
13 #include "instr/jedule/jedule_platform.h"
17 /********************************************************************/
19 static xbt_dict_t host2_simgrid_parent_container;
20 static xbt_dict_t container_name2container;
22 /********************************************************************/
24 static void add_subset_to(xbt_dynar_t subset_list, int start, int end,
25 jed_simgrid_container_t parent);
27 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
28 jed_simgrid_container_t parent);
30 static void jed_free_container(jed_simgrid_container_t container);
32 /********************************************************************/
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);
40 static int compare_ids(const void *num1, const void *num2) {
41 int *i1 = (int*) num1;
42 int *i2 = (int*) num2;
46 static void jed_free_container(jed_simgrid_container_t container) {
48 xbt_dict_free(&container->name2id);
49 if( container->resource_list != NULL ) {
50 xbt_dynar_free(&container->resource_list);
53 if( container->container_children != NULL ) {
55 jed_simgrid_container_t child_container;
56 xbt_dynar_foreach(container->container_children, iter, child_container) {
57 jed_free_container(child_container);
61 free(container->name);
65 void jed_simgrid_create_container(jed_simgrid_container_t *container, char *name) {
66 xbt_assert( name != NULL );
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;
74 xbt_dict_set(container_name2container, (*container)->name, *container, NULL);
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;
86 void jed_simgrid_add_resources(jed_simgrid_container_t parent,
87 xbt_dynar_t host_names) {
94 parent->is_lowest = 1;
95 xbt_dynar_free(&parent->container_children);
96 parent->container_children = NULL;
97 parent->name2id = xbt_dict_new();
99 parent->resource_list = xbt_dynar_new(sizeof(char *), NULL);
101 xbt_dynar_sort (host_names, &compare_hostnames);
103 xbt_dynar_foreach(host_names, iter, host_name) {
104 buf_copy = strdup(buf);
105 sprintf(buf_copy, "%d", 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);
114 static void add_subset_to(xbt_dynar_t subset_list, int start, int end,
115 jed_simgrid_container_t parent) {
117 jed_res_subset_t subset;
119 xbt_assert( subset_list != NULL );
120 xbt_assert( parent != NULL );
122 // printf(">>> start=%d end=%d\n", start, end);
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;
129 xbt_dynar_push(subset_list, &subset);
133 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
134 jed_simgrid_container_t parent) {
143 // get ids for each host
146 // create subset for each id group
148 xbt_assert( host2_simgrid_parent_container != NULL );
149 xbt_assert( subset_list != NULL );
150 xbt_assert( hostgroup != NULL );
151 xbt_assert( parent != NULL );
153 id_list = xbt_dynar_new(sizeof(char *), NULL);
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);
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);
169 qsort (id_ar, nb_ids, sizeof(int), &compare_ids);
177 for(i=0; i<nb_ids; i++) {
179 if( id_ar[i] - id_ar[pos] > 1 ) {
181 add_subset_to( subset_list, id_ar[start], id_ar[pos], parent );
184 if( i == nb_ids-1 ) {
185 add_subset_to( subset_list, id_ar[i], id_ar[i], parent );
189 if( i == nb_ids-1 ) {
190 add_subset_to( subset_list, id_ar[start], id_ar[i], parent );
199 xbt_dynar_free(&id_list);
204 void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list,
205 xbt_dynar_t host_names) {
209 xbt_dict_t parent2hostgroup; // group hosts by parent
211 parent2hostgroup = xbt_dict_new();
213 xbt_assert( host_names != NULL );
215 // for each host name
216 // find parent container
217 // group by parent container
219 xbt_dynar_foreach(host_names, iter, host_name) {
220 jed_simgrid_container_t parent;
221 xbt_dynar_t hostgroup;
223 //printf("checking %s \n", host_name);
225 parent = xbt_dict_get(host2_simgrid_parent_container, host_name);
226 xbt_assert( parent != NULL );
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);
234 xbt_dynar_push(hostgroup, &host_name);
238 xbt_dict_cursor_t cursor=NULL;
240 xbt_dynar_t hostgroup;
241 jed_simgrid_container_t parent;
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);
251 xbt_dict_free(&parent2hostgroup);
256 void jedule_add_meta_info(jedule_t jedule, char *key, char *value) {
260 xbt_assert(key != NULL);
261 xbt_assert(value != NULL);
263 val_cp = strdup(value);
264 xbt_dict_set(jedule->jedule_meta_info, key, val_cp, NULL);
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();
274 void jed_free_jedule(jedule_t jedule) {
276 jed_free_container(jedule->root_container);
278 xbt_dict_free(&jedule->jedule_meta_info);
281 xbt_dict_free(&host2_simgrid_parent_container);
282 xbt_dict_free(&container_name2container);