Logo AND Algorithmique Numérique Distribuée

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