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 xbt_dynar_free(&container->resource_list);
51 if( container->container_children != NULL ) {
53 jed_simgrid_container_t child_container;
54 xbt_dynar_foreach(container->container_children, iter, child_container) {
55 jed_free_container(child_container);
57 xbt_dynar_free(&container->container_children);
60 xbt_free(container->name);
64 void jed_simgrid_create_container(jed_simgrid_container_t *container, char *name) {
65 xbt_assert( name != NULL );
67 *container = xbt_new0(s_jed_simgrid_container_t,1);
68 (*container)->name = xbt_strdup(name);
69 (*container)->is_lowest = 0;
70 (*container)->container_children = xbt_dynar_new(sizeof(jed_simgrid_container_t), NULL);
71 (*container)->parent = NULL;
73 xbt_dict_set(container_name2container, (*container)->name, *container, NULL);
77 void jed_simgrid_add_container(jed_simgrid_container_t parent,
78 jed_simgrid_container_t child) {
79 xbt_assert(parent != NULL);
80 xbt_assert(child != NULL);
81 xbt_dynar_push(parent->container_children, &child);
82 child->parent = parent;
85 void jed_simgrid_add_resources(jed_simgrid_container_t parent,
86 xbt_dynar_t host_names) {
92 parent->is_lowest = 1;
93 xbt_dynar_free(&parent->container_children);
94 parent->container_children = NULL;
95 parent->name2id = xbt_dict_new_homogeneous(xbt_free);
97 parent->resource_list = xbt_dynar_new(sizeof(char *), NULL);
99 xbt_dynar_sort (host_names, &compare_hostnames);
101 xbt_dynar_foreach(host_names, iter, host_name) {
102 buf = bprintf("%d", parent->last_id);
104 xbt_dict_set(parent->name2id, host_name, buf, NULL);
105 xbt_dict_set(host2_simgrid_parent_container, host_name, parent, NULL);
106 xbt_dynar_push(parent->resource_list, &host_name);
111 static void add_subset_to(xbt_dynar_t subset_list, int start, int end,
112 jed_simgrid_container_t parent) {
114 jed_res_subset_t subset;
116 xbt_assert( subset_list != NULL );
117 xbt_assert( parent != NULL );
119 // printf(">>> start=%d end=%d\n", start, end);
121 subset = xbt_new0(s_jed_res_subset_t,1);
122 subset->start_idx = start;
123 subset->nres = end-start+1;
124 subset->parent = parent;
126 xbt_dynar_push(subset_list, &subset);
130 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
131 jed_simgrid_container_t parent) {
140 // get ids for each host
143 // create subset for each id group
145 xbt_assert( host2_simgrid_parent_container != NULL );
146 xbt_assert( subset_list != NULL );
147 xbt_assert( hostgroup != NULL );
148 xbt_assert( parent != NULL );
150 id_list = xbt_dynar_new(sizeof(char *), NULL);
152 xbt_dynar_foreach(hostgroup, iter, host_name) {
153 jed_simgrid_container_t parent;
154 xbt_assert( host_name != NULL );
155 parent = xbt_dict_get(host2_simgrid_parent_container, host_name);
156 id_str = xbt_dict_get(parent->name2id, host_name);
157 xbt_dynar_push(id_list, &id_str);
160 nb_ids = xbt_dynar_length(id_list);
161 id_ar = xbt_new0(int,nb_ids);
162 xbt_dynar_foreach(id_list, iter, id_str) {
163 id_ar[iter] = atoi(id_str);
166 qsort (id_ar, nb_ids, sizeof(int), &compare_ids);
174 for(i=0; i<nb_ids; i++) {
176 if( id_ar[i] - id_ar[pos] > 1 ) {
178 add_subset_to( subset_list, id_ar[start], id_ar[pos], parent );
181 if( i == nb_ids-1 ) {
182 add_subset_to( subset_list, id_ar[i], id_ar[i], parent );
186 if( i == nb_ids-1 ) {
187 add_subset_to( subset_list, id_ar[start], id_ar[i], parent );
196 xbt_dynar_free(&id_list);
201 void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list,
202 xbt_dynar_t host_names) {
206 xbt_dict_t parent2hostgroup; // group hosts by parent
208 parent2hostgroup = xbt_dict_new_homogeneous(NULL);
210 xbt_assert( host_names != NULL );
212 // for each host name
213 // find parent container
214 // group by parent container
216 xbt_dynar_foreach(host_names, iter, host_name) {
217 jed_simgrid_container_t parent;
218 xbt_dynar_t hostgroup;
220 //printf("checking %s \n", host_name);
222 parent = xbt_dict_get(host2_simgrid_parent_container, host_name);
223 xbt_assert( parent != NULL );
225 hostgroup = xbt_dict_get_or_null (parent2hostgroup, parent->name);
226 if( hostgroup == NULL ) {
227 hostgroup = xbt_dynar_new(sizeof(char*), NULL);
228 xbt_dict_set(parent2hostgroup, parent->name, hostgroup, NULL);
231 xbt_dynar_push(hostgroup, &host_name);
235 xbt_dict_cursor_t cursor=NULL;
237 xbt_dynar_t hostgroup;
238 jed_simgrid_container_t parent;
240 xbt_dict_foreach(parent2hostgroup,cursor,parent_name,hostgroup) {
241 parent = xbt_dict_get(container_name2container, parent_name);
242 // printf("subset parent >>> %s\n", parent->name);
243 add_subsets_to(subset_list, hostgroup, parent);
245 xbt_dynar_free(&hostgroup);
248 xbt_dict_free(&parent2hostgroup);
253 void jedule_add_meta_info(jedule_t jedule, char *key, char *value) {
257 xbt_assert(key != NULL);
258 xbt_assert(value != NULL);
260 val_cp = strdup(value);
261 xbt_dict_set(jedule->jedule_meta_info, key, val_cp, NULL);
264 void jed_create_jedule(jedule_t *jedule) {
265 *jedule = xbt_new0(s_jedule_t,1);
266 host2_simgrid_parent_container = xbt_dict_new_homogeneous(NULL);
267 container_name2container = xbt_dict_new_homogeneous(NULL);
268 (*jedule)->jedule_meta_info = xbt_dict_new_homogeneous(NULL);
271 void jed_free_jedule(jedule_t jedule) {
273 jed_free_container(jedule->root_container);
275 xbt_dict_free(&jedule->jedule_meta_info);
278 xbt_dict_free(&host2_simgrid_parent_container);
279 xbt_dict_free(&container_name2container);