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 = (char*)calloc((strlen(name)+1), sizeof(char));
70 strcpy((*container)->name, name);
71 (*container)->is_lowest = 0;
72 (*container)->container_children = xbt_dynar_new(sizeof(jed_simgrid_container_t), NULL);
73 (*container)->parent = NULL;
75 xbt_dict_set(container_name2container, (*container)->name, *container, NULL);
79 void jed_simgrid_add_container(jed_simgrid_container_t parent,
80 jed_simgrid_container_t child) {
81 xbt_assert(parent != NULL);
82 xbt_assert(child != NULL);
83 xbt_dynar_push(parent->container_children, &child);
84 child->parent = parent;
87 void jed_simgrid_add_resources(jed_simgrid_container_t parent,
88 xbt_dynar_t host_names) {
95 parent->is_lowest = 1;
96 xbt_dynar_free(&parent->container_children);
97 parent->container_children = NULL;
98 parent->name2id = xbt_dict_new();
100 parent->resource_list = xbt_dynar_new(sizeof(char *), NULL);
102 xbt_dynar_sort (host_names, &compare_hostnames);
104 xbt_dynar_foreach(host_names, iter, host_name) {
105 buf_copy = strdup(buf);
106 sprintf(buf_copy, "%d", parent->last_id);
108 xbt_dict_set(parent->name2id, host_name, buf_copy, NULL);
109 xbt_dict_set(host2_simgrid_parent_container, host_name, parent, NULL);
110 xbt_dynar_push(parent->resource_list, &host_name);
115 static void add_subset_to(xbt_dynar_t subset_list, int start, int end,
116 jed_simgrid_container_t parent) {
118 jed_res_subset_t subset;
120 xbt_assert( subset_list != NULL );
121 xbt_assert( parent != NULL );
123 // printf(">>> start=%d end=%d\n", start, end);
125 subset = (jed_res_subset_t)calloc(1,sizeof(s_jed_res_subset_t));
126 subset->start_idx = start;
127 subset->nres = end-start+1;
128 subset->parent = parent;
130 xbt_dynar_push(subset_list, &subset);
134 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
135 jed_simgrid_container_t parent) {
144 // get ids for each host
147 // create subset for each id group
149 xbt_assert( host2_simgrid_parent_container != NULL );
150 xbt_assert( subset_list != NULL );
151 xbt_assert( hostgroup != NULL );
152 xbt_assert( parent != NULL );
154 id_list = xbt_dynar_new(sizeof(char *), NULL);
156 xbt_dynar_foreach(hostgroup, iter, host_name) {
157 jed_simgrid_container_t parent;
158 xbt_assert( host_name != NULL );
159 parent = xbt_dict_get(host2_simgrid_parent_container, host_name);
160 id_str = xbt_dict_get(parent->name2id, host_name);
161 xbt_dynar_push(id_list, &id_str);
164 nb_ids = xbt_dynar_length(id_list);
165 id_ar = (int*)calloc(nb_ids, sizeof(int));
166 xbt_dynar_foreach(id_list, iter, id_str) {
167 id_ar[iter] = atoi(id_str);
170 qsort (id_ar, nb_ids, sizeof(int), &compare_ids);
178 for(i=0; i<nb_ids; i++) {
180 if( id_ar[i] - id_ar[pos] > 1 ) {
182 add_subset_to( subset_list, id_ar[start], id_ar[pos], parent );
185 if( i == nb_ids-1 ) {
186 add_subset_to( subset_list, id_ar[i], id_ar[i], parent );
190 if( i == nb_ids-1 ) {
191 add_subset_to( subset_list, id_ar[start], id_ar[i], parent );
200 xbt_dynar_free(&id_list);
205 void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list,
206 xbt_dynar_t host_names) {
210 xbt_dict_t parent2hostgroup; // group hosts by parent
212 parent2hostgroup = xbt_dict_new();
214 xbt_assert( host_names != NULL );
216 // for each host name
217 // find parent container
218 // group by parent container
220 xbt_dynar_foreach(host_names, iter, host_name) {
221 jed_simgrid_container_t parent;
222 xbt_dynar_t hostgroup;
224 //printf("checking %s \n", host_name);
226 parent = xbt_dict_get(host2_simgrid_parent_container, host_name);
227 xbt_assert( parent != NULL );
229 hostgroup = xbt_dict_get_or_null (parent2hostgroup, parent->name);
230 if( hostgroup == NULL ) {
231 hostgroup = xbt_dynar_new(sizeof(char*), NULL);
232 xbt_dict_set(parent2hostgroup, parent->name, hostgroup, NULL);
235 xbt_dynar_push(hostgroup, &host_name);
239 xbt_dict_cursor_t cursor=NULL;
241 xbt_dynar_t hostgroup;
242 jed_simgrid_container_t parent;
244 xbt_dict_foreach(parent2hostgroup,cursor,parent_name,hostgroup) {
245 parent = xbt_dict_get(container_name2container, parent_name);
246 // printf("subset parent >>> %s\n", parent->name);
247 add_subsets_to(subset_list, hostgroup, parent);
252 xbt_dict_free(&parent2hostgroup);
257 void jedule_add_meta_info(jedule_t jedule, char *key, char *value) {
261 xbt_assert(key != NULL);
262 xbt_assert(value != NULL);
264 val_cp = strdup(value);
265 xbt_dict_set(jedule->jedule_meta_info, key, val_cp, NULL);
268 void jed_create_jedule(jedule_t *jedule) {
269 *jedule = (jedule_t)calloc(1,sizeof(s_jedule_t));
270 host2_simgrid_parent_container = xbt_dict_new();
271 container_name2container = xbt_dict_new();
272 (*jedule)->jedule_meta_info = xbt_dict_new();
275 void jed_free_jedule(jedule_t jedule) {
277 jed_free_container(jedule->root_container);
279 xbt_dict_free(&jedule->jedule_meta_info);
282 xbt_dict_free(&host2_simgrid_parent_container);
283 xbt_dict_free(&container_name2container);