1 /* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/jedule/jedule.hpp"
7 #include "simgrid/host.h"
8 #include "simgrid/s4u/NetZone.hpp"
9 #include "xbt/asserts.h"
12 #if SIMGRID_HAVE_JEDULE
15 std::unordered_map<const char*, jed_container_t> host2_simgrid_parent_container;
16 std::unordered_map<std::string, jed_container_t> container_name2container;
21 Subset::Subset(int start_idx, int end_idx, Container* parent)
22 : start_idx(start_idx), parent(parent)
24 nres=end_idx-start_idx+1;
27 Container::Container(const std::string& name) : name(name)
29 container_name2container.insert({this->name, this});
32 void Container::add_child(jed_container_t child)
34 xbt_assert(child != nullptr);
35 this->children.emplace_back(child);
39 void Container::add_resources(std::vector<sg_host_t> hosts)
42 this->children.clear();
45 for (auto const& host : hosts) {
46 const char *host_name = sg_host_get_name(host);
47 this->name2id.insert({host_name, this->last_id_});
49 host2_simgrid_parent_container.insert({host_name, this});
50 this->resource_list.push_back(host);
54 void Container::create_hierarchy(sg_netzone_t from_as)
56 if (from_as->get_children().empty()) {
58 // add hosts to jedule platform
59 std::vector<sg_host_t> table = from_as->get_all_hosts();
60 this->add_resources(table);
62 for (auto const& nz : from_as->get_children()) {
63 jed_container_t child_container = new simgrid::jedule::Container(nz->get_name());
64 this->add_child(child_container);
65 child_container->create_hierarchy(nz);
70 std::vector<int> Container::get_hierarchy()
72 if(this->parent != nullptr ) {
73 if (not this->parent->children.empty()) {
74 // we are in the last level
75 return this->parent->get_hierarchy();
80 for (auto const& child : this->parent->children) {
81 if (child.get() == this) {
88 xbt_assert( child_nb > - 1);
89 std::vector<int> heir_list = this->parent->get_hierarchy();
90 heir_list.insert(heir_list.begin(), child_nb);
95 std::vector<int> heir_list = {top_level};
100 std::string Container::get_hierarchy_as_string()
102 std::string output("");
104 std::vector<int> heir_list = this->get_hierarchy();
106 unsigned int length = heir_list.size();
108 for (auto const& id : heir_list) {
109 output += std::to_string(id);
110 if( i != length-1 ) {
118 void Container::print_resources(FILE* jed_file)
121 xbt_assert(not this->resource_list.empty());
123 unsigned int res_nb = this->resource_list.size();
124 std::string resid = this->get_hierarchy_as_string();
126 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
127 for (auto const& res : this->resource_list) {
128 const char * res_name = sg_host_get_name(res);
129 fprintf(jed_file, "%s", res_name);
130 if( i != res_nb-1 ) {
131 fprintf(jed_file, "|");
135 fprintf(jed_file, "\" />\n");
138 void Container::print(FILE* jed_file)
140 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
141 if (not this->children.empty()) {
142 for (auto const& child : this->children) {
143 child->print(jed_file);
146 this->print_resources(jed_file);
148 fprintf(jed_file, " </res>\n");
151 } // namespace jedule
152 } // namespace simgrid
154 static void add_subsets_to(std::vector<simgrid::jedule::Subset>& subset_list, std::vector<const char*> hostgroup,
155 jed_container_t parent)
157 // get ids for each host
160 // create subset for each id group
162 xbt_assert( parent != nullptr );
164 std::vector<unsigned int> id_list;
166 for (auto const& host_name : hostgroup) {
167 xbt_assert( host_name != nullptr );
168 jed_container_t parent_cont = host2_simgrid_parent_container.at(host_name);
169 unsigned int id = parent_cont->name2id.at(host_name);
170 id_list.push_back(id);
172 unsigned int nb_ids = id_list.size();
173 std::sort(id_list.begin(), id_list.end());
178 for(unsigned int i=0; i<nb_ids; i++) {
179 if( id_list[i] - id_list[pos] > 1 ) {
180 subset_list.emplace_back(id_list[start], id_list[pos], parent);
183 if( i == nb_ids-1 ) {
184 subset_list.emplace_back(id_list[i], id_list[i], parent);
187 if( i == nb_ids-1 ) {
188 subset_list.emplace_back(id_list[start], id_list[i], parent);
196 void get_resource_selection_by_hosts(std::vector<simgrid::jedule::Subset>& subset_list,
197 const std::vector<sg_host_t>& host_list)
199 // for each host name
200 // find parent container
201 // group by parent container
202 std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
203 for (auto const& host : host_list) {
204 const char *host_name = sg_host_get_name(host);
205 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
206 xbt_assert( parent != nullptr );
208 auto host_group = parent2hostgroup.find(parent->name.c_str());
209 if (host_group == parent2hostgroup.end())
210 parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
212 host_group->second.push_back(host_name);
215 for (auto const& elm : parent2hostgroup) {
216 jed_container_t parent = container_name2container.at(elm.first);
217 add_subsets_to(subset_list, elm.second, parent);