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), nres(end_idx - start_idx + 1), parent(parent)
26 Container::Container(const std::string& name) : name(name)
28 container_name2container.insert({this->name, this});
31 void Container::add_child(jed_container_t child)
33 xbt_assert(child != nullptr);
34 this->children.emplace_back(child);
38 void Container::add_resources(std::vector<sg_host_t> hosts)
40 this->children.clear();
43 for (auto const& host : hosts) {
44 const char *host_name = sg_host_get_name(host);
45 this->name2id.insert({host_name, this->last_id_});
47 host2_simgrid_parent_container.insert({host_name, this});
48 this->resource_list.push_back(host);
52 void Container::create_hierarchy(sg_netzone_t from_as)
54 if (from_as->get_children().empty()) {
56 // add hosts to jedule platform
57 std::vector<sg_host_t> table = from_as->get_all_hosts();
58 this->add_resources(table);
60 for (auto const& nz : from_as->get_children()) {
61 jed_container_t child_container = new simgrid::jedule::Container(nz->get_name());
62 this->add_child(child_container);
63 child_container->create_hierarchy(nz);
68 std::vector<int> Container::get_hierarchy()
70 if(this->parent != nullptr ) {
71 if (not this->parent->children.empty()) {
72 // we are in the last level
73 return this->parent->get_hierarchy();
78 for (auto const& child : this->parent->children) {
79 if (child.get() == this) {
86 xbt_assert( child_nb > - 1);
87 std::vector<int> heir_list = this->parent->get_hierarchy();
88 heir_list.insert(heir_list.begin(), child_nb);
93 std::vector<int> heir_list = {top_level};
98 std::string Container::get_hierarchy_as_string()
100 std::string output("");
102 std::vector<int> heir_list = this->get_hierarchy();
104 unsigned int length = heir_list.size();
106 for (auto const& id : heir_list) {
107 output += std::to_string(id);
108 if( i != length-1 ) {
116 void Container::print_resources(FILE* jed_file)
119 xbt_assert(not this->resource_list.empty());
121 unsigned int res_nb = this->resource_list.size();
122 std::string resid = this->get_hierarchy_as_string();
124 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
125 for (auto const& res : this->resource_list) {
126 const char * res_name = sg_host_get_name(res);
127 fprintf(jed_file, "%s", res_name);
128 if( i != res_nb-1 ) {
129 fprintf(jed_file, "|");
133 fprintf(jed_file, "\" />\n");
136 void Container::print(FILE* jed_file)
138 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
139 if (not this->children.empty()) {
140 for (auto const& child : this->children) {
141 child->print(jed_file);
144 this->print_resources(jed_file);
146 fprintf(jed_file, " </res>\n");
149 } // namespace jedule
150 } // namespace simgrid
152 static void add_subsets_to(std::vector<simgrid::jedule::Subset>& subset_list, std::vector<const char*> hostgroup,
153 jed_container_t parent)
155 // get ids for each host
158 // create subset for each id group
160 xbt_assert( parent != nullptr );
162 std::vector<unsigned int> id_list;
164 for (auto const& host_name : hostgroup) {
165 xbt_assert( host_name != nullptr );
166 jed_container_t parent_cont = host2_simgrid_parent_container.at(host_name);
167 unsigned int id = parent_cont->name2id.at(host_name);
168 id_list.push_back(id);
170 unsigned int nb_ids = id_list.size();
171 std::sort(id_list.begin(), id_list.end());
176 for(unsigned int i=0; i<nb_ids; i++) {
177 if( id_list[i] - id_list[pos] > 1 ) {
178 subset_list.emplace_back(id_list[start], id_list[pos], parent);
181 if( i == nb_ids-1 ) {
182 subset_list.emplace_back(id_list[i], id_list[i], parent);
185 if( i == nb_ids-1 ) {
186 subset_list.emplace_back(id_list[start], id_list[i], parent);
194 void get_resource_selection_by_hosts(std::vector<simgrid::jedule::Subset>& subset_list,
195 const std::vector<sg_host_t>& host_list)
197 // for each host name
198 // find parent container
199 // group by parent container
200 std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
201 for (auto const& host : host_list) {
202 const char *host_name = sg_host_get_name(host);
203 const simgrid::jedule::Container* parent = host2_simgrid_parent_container.at(host_name);
204 xbt_assert( parent != nullptr );
206 auto host_group = parent2hostgroup.find(parent->name.c_str());
207 if (host_group == parent2hostgroup.end())
208 parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
210 host_group->second.push_back(host_name);
213 for (auto const& elm : parent2hostgroup) {
214 jed_container_t parent = container_name2container.at(elm.first);
215 add_subsets_to(subset_list, elm.second, parent);