1 /* Copyright (c) 2010-2016. 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.hpp"
8 #include "simgrid/jedule/jedule_platform.hpp"
9 #include "simgrid/s4u/As.hpp"
11 #include "xbt/asserts.h"
12 #include "xbt/dynar.h"
19 Container::Container(std::string name)
21 container_name2container.insert({this->name, this});
24 Container::~Container(){
25 if(!this->children.empty())
26 for (auto child: this->children)
30 void Container::addChild(jed_container_t child){
31 xbt_assert(this != nullptr);
32 xbt_assert(child != nullptr);
33 this->children.push_back(child);
37 void Container::addResources(std::vector<sg_host_t> hosts){
39 this->children.clear();
42 //FIXME do we need to sort?: xbt_dynar_sort_strings(host_names);
44 for (auto host : hosts) {
45 const char *host_name = sg_host_get_name(host);
46 this->name2id.insert({host_name, this->last_id});
48 host2_simgrid_parent_container.insert({host_name, this});
49 this->resource_list.push_back(host);
53 void Container::createHierarchy(AS_t from_as){
54 xbt_dict_cursor_t cursor = nullptr;
57 xbt_dict_t routing_sons = from_as->children();
59 if (xbt_dict_is_empty(routing_sons)) {
61 // add hosts to jedule platform
62 xbt_dynar_t table = from_as->hosts();
63 unsigned int dynar_cursor;
66 std::vector<sg_host_t> hosts;
68 xbt_dynar_foreach(table, dynar_cursor, host) {
69 hosts.push_back(host);
71 this->addResources(hosts);
72 xbt_dynar_free(&table);
74 xbt_dict_foreach(routing_sons, cursor, key, elem) {
75 jed_container_t child_container = new simgrid::jedule::Container(std::string(elem->name()));
76 this->addChild(child_container);
77 child_container->createHierarchy(elem);
82 std::vector<int> Container::getHierarchy() {
83 xbt_assert( this!= nullptr );
85 if(this->parent != nullptr ) {
87 if(!this->parent->children.empty()) {
88 // we are in the last level
89 return this->parent->getHierarchy();
94 for (auto child : this->parent->children) {
102 xbt_assert( child_nb > - 1);
103 std::vector<int> heir_list = this->parent->getHierarchy();
104 heir_list.insert(heir_list.begin(), child_nb);
109 std::vector<int> heir_list = {top_level};
114 std::string Container::getHierarchyAsString(){
115 std::string output("");
117 std::vector<int> heir_list = this->getHierarchy();
119 unsigned int length = heir_list.size();
121 for (auto id : heir_list) {
122 output += std::to_string(id);
123 if( i != length-1 ) {
131 void Container::printResources(FILE * jed_file){
133 xbt_assert(!this->resource_list.empty());
135 unsigned int res_nb = this->resource_list.size();
136 std::string resid = this->getHierarchyAsString();
138 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
139 for (auto res: this->resource_list) {
140 const char * res_name = sg_host_get_name(res);
141 fprintf(jed_file, "%s", res_name);
142 if( i != res_nb-1 ) {
143 fprintf(jed_file, "|");
147 fprintf(jed_file, "\" />\n");
150 void Container::print(FILE* jed_file) {
151 xbt_assert( this != nullptr );
152 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
153 if( !this->children.empty()){
154 for (auto child: this->children) {
155 child->print(jed_file);
158 this->printResources(jed_file);
160 fprintf(jed_file, " </res>\n");
166 static int compare_ids(const void *num1, const void *num2) {
167 return *((int*) num1) - *((int*) num2);
170 static void add_subset_to(xbt_dynar_t subset_list, int start, int end, jed_container_t parent) {
171 jed_res_subset_t subset;
173 xbt_assert( subset_list != nullptr );
174 xbt_assert( parent != nullptr );
176 subset = xbt_new0(s_jed_res_subset_t,1);
177 subset->start_idx = start;
178 subset->nres = end-start+1;
179 subset->parent = parent;
181 xbt_dynar_push(subset_list, &subset);
184 static void add_subsets_to(xbt_dynar_t subset_list, std::vector<const char*> hostgroup, jed_container_t parent) {
187 // get ids for each host
190 // create subset for each id group
192 xbt_assert( subset_list != nullptr );
193 xbt_assert( parent != nullptr );
195 xbt_dynar_t id_list = xbt_dynar_new(sizeof(int), nullptr);
197 for (auto host_name : hostgroup) {
198 xbt_assert( host_name != nullptr );
199 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
200 id = parent->name2id.at(host_name);
201 xbt_dynar_push(id_list, &id);
203 int nb_ids = xbt_dynar_length(id_list);
204 xbt_dynar_sort(id_list, &compare_ids);
206 int *id_ar = static_cast<int*>(xbt_dynar_to_array(id_list));
212 for(int i=0; i<nb_ids; i++) {
213 if( id_ar[i] - id_ar[pos] > 1 ) {
214 add_subset_to( subset_list, id_ar[start], id_ar[pos], parent );
217 if( i == nb_ids-1 ) {
218 add_subset_to( subset_list, id_ar[i], id_ar[i], parent );
221 if( i == nb_ids-1 ) {
222 add_subset_to( subset_list, id_ar[start], id_ar[i], parent );
232 void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list, std::vector<sg_host_t> *host_list) {
233 xbt_assert( host_list != nullptr );
234 // for each host name
235 // find parent container
236 // group by parent container
237 std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
238 for (auto host: *host_list) {
239 const char *host_name = sg_host_get_name(host);
240 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
241 xbt_assert( parent != nullptr );
243 auto host_group = parent2hostgroup.find(parent->name.c_str());
244 if (host_group == parent2hostgroup.end())
245 parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
247 host_group->second.push_back(host_name);
250 for (auto elm: parent2hostgroup) {
251 jed_container_t parent = container_name2container.at(elm.first);
252 add_subsets_to(subset_list, elm.second, parent);