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/NetZone.hpp"
10 #include "xbt/asserts.h"
11 #include "xbt/dynar.h"
18 Subset::Subset(int start_idx, int end_idx, Container* parent)
19 : start_idx(start_idx), parent(parent)
21 nres=end_idx-start_idx+1;
25 Container::Container(std::string name): name(name)
27 container_name2container.insert({this->name, this});
30 Container::~Container()
32 if(!this->children.empty())
33 for (auto child: this->children)
37 void Container::addChild(jed_container_t child)
39 xbt_assert(this != nullptr);
40 xbt_assert(child != nullptr);
41 this->children.push_back(child);
45 void Container::addResources(std::vector<sg_host_t> hosts)
48 this->children.clear();
51 //FIXME do we need to sort?: xbt_dynar_sort_strings(host_names);
53 for (auto host : hosts) {
54 const char *host_name = sg_host_get_name(host);
55 this->name2id.insert({host_name, this->last_id});
57 host2_simgrid_parent_container.insert({host_name, this});
58 this->resource_list.push_back(host);
62 void Container::createHierarchy(sg_netzone_t from_as)
64 xbt_dict_cursor_t cursor = nullptr;
67 xbt_dict_t routing_sons = from_as->children();
69 if (xbt_dict_is_empty(routing_sons)) {
71 // add hosts to jedule platform
72 this->addResources(*from_as->hosts());
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()
84 xbt_assert( this!= nullptr );
86 if(this->parent != nullptr ) {
88 if(!this->parent->children.empty()) {
89 // we are in the last level
90 return this->parent->getHierarchy();
95 for (auto child : this->parent->children) {
103 xbt_assert( child_nb > - 1);
104 std::vector<int> heir_list = this->parent->getHierarchy();
105 heir_list.insert(heir_list.begin(), child_nb);
110 std::vector<int> heir_list = {top_level};
115 std::string Container::getHierarchyAsString()
117 std::string output("");
119 std::vector<int> heir_list = this->getHierarchy();
121 unsigned int length = heir_list.size();
123 for (auto id : heir_list) {
124 output += std::to_string(id);
125 if( i != length-1 ) {
133 void Container::printResources(FILE * jed_file)
136 xbt_assert(!this->resource_list.empty());
138 unsigned int res_nb = this->resource_list.size();
139 std::string resid = this->getHierarchyAsString();
141 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
142 for (auto res: this->resource_list) {
143 const char * res_name = sg_host_get_name(res);
144 fprintf(jed_file, "%s", res_name);
145 if( i != res_nb-1 ) {
146 fprintf(jed_file, "|");
150 fprintf(jed_file, "\" />\n");
153 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 void add_subsets_to(std::vector<jed_subset_t> *subset_list, std::vector<const char*> hostgroup, jed_container_t parent)
172 // get ids for each host
175 // create subset for each id group
177 xbt_assert( parent != nullptr );
179 std::vector<unsigned int> id_list;
181 for (auto host_name : hostgroup) {
182 xbt_assert( host_name != nullptr );
183 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
184 unsigned int id = parent->name2id.at(host_name);
185 id_list.push_back(id);
187 unsigned int nb_ids = id_list.size();
188 std::sort(id_list.begin(), id_list.end());
193 for(unsigned int i=0; i<nb_ids; i++) {
194 if( id_list[i] - id_list[pos] > 1 ) {
195 subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[pos], parent));
198 if( i == nb_ids-1 ) {
199 subset_list->push_back(new simgrid::jedule::Subset(id_list[i], id_list[i], parent));
202 if( i == nb_ids-1 ) {
203 subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[i], parent));
212 void get_resource_selection_by_hosts(std::vector<jed_subset_t> *subset_list, std::vector<sg_host_t> *host_list)
214 xbt_assert( host_list != nullptr );
215 // for each host name
216 // find parent container
217 // group by parent container
218 std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
219 for (auto host: *host_list) {
220 const char *host_name = sg_host_get_name(host);
221 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
222 xbt_assert( parent != nullptr );
224 auto host_group = parent2hostgroup.find(parent->name.c_str());
225 if (host_group == parent2hostgroup.end())
226 parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
228 host_group->second.push_back(host_name);
231 for (auto elm: parent2hostgroup) {
232 jed_container_t parent = container_name2container.at(elm.first);
233 add_subsets_to(subset_list, elm.second, parent);