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)
32 this->parent = nullptr;
34 xbt_dict_set(container_name2container, this->name.c_str(), this, nullptr);
37 void Container::addChild(jed_container_t child){
38 xbt_assert(this != nullptr);
39 xbt_assert(child != nullptr);
40 this->children.push_back(child);
44 void Container::addResources(std::vector<sg_host_t> hosts){
46 this->children.clear();
49 //FIXME do we need to sort?: xbt_dynar_sort_strings(host_names);
51 for (auto host : hosts) {
52 const char *host_name = sg_host_get_name(host);
53 this->name2id.insert({host_name, this->last_id});
55 xbt_dict_set(host2_simgrid_parent_container, host_name, this, nullptr);
56 this->resource_list.push_back(host);
60 void Container::createHierarchy(AS_t from_as){
61 xbt_dict_cursor_t cursor = nullptr;
64 xbt_dict_t routing_sons = from_as->children();
66 if (xbt_dict_is_empty(routing_sons)) {
68 // add hosts to jedule platform
69 xbt_dynar_t table = from_as->hosts();
70 unsigned int dynar_cursor;
73 std::vector<sg_host_t> hosts;
75 xbt_dynar_foreach(table, dynar_cursor, host) {
76 hosts.push_back(host);
78 this->addResources(hosts);
79 xbt_dynar_free(&table);
81 xbt_dict_foreach(routing_sons, cursor, key, elem) {
82 jed_container_t child_container = new simgrid::jedule::Container(std::string(elem->name()));
83 this->addChild(child_container);
84 child_container->createHierarchy(elem);
89 std::vector<int> Container::getHierarchy() {
90 xbt_assert( this!= nullptr );
92 if(this->parent != nullptr ) {
94 if(!this->parent->children.empty()) {
95 // we are in the last level
96 return this->parent->getHierarchy();
101 for (auto child : this->parent->children) {
109 xbt_assert( child_nb > - 1);
110 std::vector<int> heir_list = this->parent->getHierarchy();
111 heir_list.insert(heir_list.begin(), child_nb);
116 std::vector<int> heir_list = {top_level};
121 std::string Container::getHierarchyAsString(){
122 std::string output("");
124 std::vector<int> heir_list = this->getHierarchy();
126 unsigned int length = heir_list.size();
128 for (auto id : heir_list) {
129 output += std::to_string(id);
130 if( i != length-1 ) {
138 void Container::printResources(FILE * jed_file){
141 xbt_assert(!this->resource_list.empty());
143 res_nb = this->resource_list.size();
145 std::string resid = this->getHierarchyAsString();
147 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
148 for (auto res: this->resource_list) {
149 const char * res_name = sg_host_get_name(res);
150 fprintf(jed_file, "%s", res_name);
151 if( i != res_nb-1 ) {
152 fprintf(jed_file, "|");
156 fprintf(jed_file, "\" />\n");
159 void Container::print(FILE* jed_file) {
160 xbt_assert( this != nullptr );
161 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
162 if( !this->children.empty()){
163 for (auto child: this->children) {
164 child->print(jed_file);
167 this->printResources(jed_file);
169 fprintf(jed_file, " </res>\n");
175 static int compare_ids(const void *num1, const void *num2) {
176 return *((int*) num1) - *((int*) num2);
179 static void jed_free_container(jed_container_t container) {
180 if(!container->children.empty()) {
181 for (auto child: container->children){
182 jed_free_container(child);
188 static void add_subset_to(xbt_dynar_t subset_list, int start, int end, jed_container_t parent) {
189 jed_res_subset_t subset;
191 xbt_assert( subset_list != nullptr );
192 xbt_assert( parent != nullptr );
194 subset = xbt_new0(s_jed_res_subset_t,1);
195 subset->start_idx = start;
196 subset->nres = end-start+1;
197 subset->parent = parent;
199 xbt_dynar_push(subset_list, &subset);
202 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup, jed_container_t parent) {
207 // get ids for each host
210 // create subset for each id group
212 xbt_assert( host2_simgrid_parent_container != nullptr );
213 xbt_assert( subset_list != nullptr );
214 xbt_assert( hostgroup != nullptr );
215 xbt_assert( parent != nullptr );
217 xbt_dynar_t id_list = xbt_dynar_new(sizeof(int), nullptr);
219 xbt_dynar_foreach(hostgroup, iter, host_name) {
220 jed_container_t parent;
221 xbt_assert( host_name != nullptr );
222 parent = (jed_container_t)xbt_dict_get(host2_simgrid_parent_container, host_name);
223 id = parent->name2id.at(host_name);
224 xbt_dynar_push(id_list, &id);
226 int nb_ids = xbt_dynar_length(id_list);
227 xbt_dynar_sort(id_list, &compare_ids);
229 int *id_ar = static_cast<int*>(xbt_dynar_to_array(id_list));
235 for(int i=0; i<nb_ids; i++) {
236 if( id_ar[i] - id_ar[pos] > 1 ) {
237 add_subset_to( subset_list, id_ar[start], id_ar[pos], parent );
240 if( i == nb_ids-1 ) {
241 add_subset_to( subset_list, id_ar[i], id_ar[i], parent );
244 if( i == nb_ids-1 ) {
245 add_subset_to( subset_list, id_ar[start], id_ar[i], parent );
255 void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list, std::vector<sg_host_t> *host_list) {
256 std::unordered_map<const char*, xbt_dynar_t> parent2hostgroup; // group hosts by parent
258 xbt_assert( host_list != nullptr );
260 // for each host name
261 // find parent container
262 // group by parent container
264 for (auto host: *host_list) {
265 const char *host_name = sg_host_get_name(host);
266 jed_container_t parent = (jed_container_t)xbt_dict_get(host2_simgrid_parent_container, host_name);
267 xbt_assert( parent != nullptr );
269 auto host_group = parent2hostgroup.find(parent->name.c_str());
270 if (host_group == parent2hostgroup.end()){
271 xbt_dynar_t group = xbt_dynar_new(sizeof(char*), nullptr);
272 xbt_dynar_push(group, &host_name);
273 parent2hostgroup.insert({parent->name.c_str(), group});
275 xbt_dynar_push(host_group->second, &host_name);
279 for (auto elm: parent2hostgroup) {
280 jed_container_t parent = (jed_container_t)xbt_dict_get(container_name2container, elm.first);
281 add_subsets_to(subset_list, elm.second, parent);
282 xbt_dynar_free_container(&elm.second);
286 void jedule_add_meta_info(jedule_t jedule, char *key, char *value) {
287 xbt_assert(key != nullptr);
288 xbt_assert(value != nullptr);
290 jedule->jedule_meta_info.insert({key, value});
293 void jed_create_jedule(jedule_t *jedule) {
294 *jedule = xbt_new0(s_jedule_t,1);
295 host2_simgrid_parent_container = xbt_dict_new_homogeneous(nullptr);
296 container_name2container = xbt_dict_new_homogeneous(nullptr);
299 void jed_free_jedule(jedule_t jedule) {
300 jed_free_container(jedule->root_container);
304 xbt_dict_free(&host2_simgrid_parent_container);
305 xbt_dict_free(&container_name2container);