1 /* Copyright (c) 2010-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "simgrid/jedule/jedule_platform.hpp"
8 #include "simgrid/jedule/jedule_output.hpp"
9 #include "simgrid/s4u/As.hpp"
10 #include "simgrid/host.h"
12 #include "xbt/asserts.h"
13 #include "xbt/dynar.h"
22 static xbt_dict_t host2_simgrid_parent_container;
23 static xbt_dict_t container_name2container;
28 Container::Container(std::string name)
31 xbt_dict_set(container_name2container, this->name.c_str(), this, nullptr);
34 void Container::addChild(jed_container_t child){
35 xbt_assert(this != nullptr);
36 xbt_assert(child != nullptr);
37 this->children.push_back(child);
41 void Container::addResources(std::vector<sg_host_t> hosts){
43 this->children.clear();
46 //FIXME do we need to sort?: xbt_dynar_sort_strings(host_names);
48 for (auto host : hosts) {
49 const char *host_name = sg_host_get_name(host);
50 this->name2id.insert({host_name, this->last_id});
52 xbt_dict_set(host2_simgrid_parent_container, host_name, this, nullptr);
53 this->resource_list.push_back(host);
57 void Container::createHierarchy(AS_t from_as){
58 xbt_dict_cursor_t cursor = nullptr;
61 xbt_dict_t routing_sons = from_as->children();
63 if (xbt_dict_is_empty(routing_sons)) {
65 // add hosts to jedule platform
66 xbt_dynar_t table = from_as->hosts();
67 unsigned int dynar_cursor;
70 std::vector<sg_host_t> hosts;
72 xbt_dynar_foreach(table, dynar_cursor, host) {
73 hosts.push_back(host);
75 this->addResources(hosts);
76 xbt_dynar_free(&table);
78 xbt_dict_foreach(routing_sons, cursor, key, elem) {
79 jed_container_t child_container = new simgrid::jedule::Container(std::string(elem->name()));
80 this->addChild(child_container);
81 child_container->createHierarchy(elem);
86 std::vector<int> Container::getHierarchy() {
87 xbt_assert( this!= nullptr );
89 if(this->parent != nullptr ) {
91 if(!this->parent->children.empty()) {
92 // we are in the last level
93 return this->parent->getHierarchy();
98 for (auto child : this->parent->children) {
106 xbt_assert( child_nb > - 1);
107 std::vector<int> heir_list = this->parent->getHierarchy();
108 heir_list.insert(heir_list.begin(), child_nb);
113 std::vector<int> heir_list = {top_level};
118 std::string Container::getHierarchyAsString(){
119 std::string output("");
121 std::vector<int> heir_list = this->getHierarchy();
123 unsigned int length = heir_list.size();
125 for (auto id : heir_list) {
126 output += std::to_string(id);
127 if( i != length-1 ) {
135 void Container::printResources(FILE * jed_file){
137 xbt_assert(!this->resource_list.empty());
139 unsigned int res_nb = this->resource_list.size();
140 std::string resid = this->getHierarchyAsString();
142 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
143 for (auto res: this->resource_list) {
144 const char * res_name = sg_host_get_name(res);
145 fprintf(jed_file, "%s", res_name);
146 if( i != res_nb-1 ) {
147 fprintf(jed_file, "|");
151 fprintf(jed_file, "\" />\n");
154 void Container::print(FILE* jed_file) {
155 xbt_assert( this != nullptr );
156 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
157 if( !this->children.empty()){
158 for (auto child: this->children) {
159 child->print(jed_file);
162 this->printResources(jed_file);
164 fprintf(jed_file, " </res>\n");
170 static int compare_ids(const void *num1, const void *num2) {
171 return *((int*) num1) - *((int*) num2);
174 /* FIXME: That should be the destructor, shouldnt it? */
175 static void jed_free_container(jed_container_t container) {
176 if(!container->children.empty()) {
177 for (auto child: container->children){
178 jed_free_container(child);
184 static void add_subset_to(xbt_dynar_t subset_list, int start, int end, jed_container_t parent) {
185 jed_res_subset_t subset;
187 xbt_assert( subset_list != nullptr );
188 xbt_assert( parent != nullptr );
190 subset = xbt_new0(s_jed_res_subset_t,1);
191 subset->start_idx = start;
192 subset->nres = end-start+1;
193 subset->parent = parent;
195 xbt_dynar_push(subset_list, &subset);
198 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup, jed_container_t parent) {
203 // get ids for each host
206 // create subset for each id group
208 xbt_assert( host2_simgrid_parent_container != nullptr );
209 xbt_assert( subset_list != nullptr );
210 xbt_assert( hostgroup != nullptr );
211 xbt_assert( parent != nullptr );
213 xbt_dynar_t id_list = xbt_dynar_new(sizeof(int), nullptr);
215 xbt_dynar_foreach(hostgroup, iter, host_name) {
216 jed_container_t parent;
217 xbt_assert( host_name != nullptr );
218 parent = (jed_container_t)xbt_dict_get(host2_simgrid_parent_container, host_name);
219 id = parent->name2id.at(host_name);
220 xbt_dynar_push(id_list, &id);
222 int nb_ids = xbt_dynar_length(id_list);
223 xbt_dynar_sort(id_list, &compare_ids);
225 int *id_ar = static_cast<int*>(xbt_dynar_to_array(id_list));
231 for(int i=0; i<nb_ids; i++) {
232 if( id_ar[i] - id_ar[pos] > 1 ) {
233 add_subset_to( subset_list, id_ar[start], id_ar[pos], parent );
236 if( i == nb_ids-1 ) {
237 add_subset_to( subset_list, id_ar[i], id_ar[i], parent );
240 if( i == nb_ids-1 ) {
241 add_subset_to( subset_list, id_ar[start], id_ar[i], parent );
251 void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list, std::vector<sg_host_t> *host_list) {
252 std::unordered_map<const char*, xbt_dynar_t> parent2hostgroup; // group hosts by parent
254 xbt_assert( host_list != nullptr );
256 // for each host name
257 // find parent container
258 // group by parent container
260 for (auto host: *host_list) {
261 const char *host_name = sg_host_get_name(host);
262 jed_container_t parent = (jed_container_t)xbt_dict_get(host2_simgrid_parent_container, host_name);
263 xbt_assert( parent != nullptr );
265 auto host_group = parent2hostgroup.find(parent->name.c_str());
266 if (host_group == parent2hostgroup.end()){
267 xbt_dynar_t group = xbt_dynar_new(sizeof(char*), nullptr);
268 xbt_dynar_push(group, &host_name);
269 parent2hostgroup.insert({parent->name.c_str(), group});
271 xbt_dynar_push(host_group->second, &host_name);
275 for (auto elm: parent2hostgroup) {
276 jed_container_t parent = (jed_container_t)xbt_dict_get(container_name2container, elm.first);
277 add_subsets_to(subset_list, elm.second, parent);
278 xbt_dynar_free_container(&elm.second);
282 void jedule_add_meta_info(jedule_t jedule, char *key, char *value) {
283 xbt_assert(key != nullptr);
284 xbt_assert(value != nullptr);
286 jedule->jedule_meta_info.insert({key, value});
289 void jed_create_jedule(jedule_t *jedule) {
290 *jedule = xbt_new0(s_jedule_t,1);
291 host2_simgrid_parent_container = xbt_dict_new_homogeneous(nullptr);
292 container_name2container = xbt_dict_new_homogeneous(nullptr);
295 void jed_free_jedule(jedule_t jedule) {
296 jed_free_container(jedule->root_container);
300 xbt_dict_free(&host2_simgrid_parent_container);
301 xbt_dict_free(&container_name2container);