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)
41 this->children.clear();
44 for (auto const& 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::create_hierarchy(sg_netzone_t from_as)
55 if (from_as->get_children().empty()) {
57 // add hosts to jedule platform
58 std::vector<sg_host_t> table = from_as->get_all_hosts();
59 this->add_resources(table);
61 for (auto const& nz : from_as->get_children()) {
62 jed_container_t child_container = new simgrid::jedule::Container(nz->get_name());
63 this->add_child(child_container);
64 child_container->create_hierarchy(nz);
69 std::vector<int> Container::get_hierarchy()
71 if(this->parent != nullptr ) {
72 if (not this->parent->children.empty()) {
73 // we are in the last level
74 return this->parent->get_hierarchy();
79 for (auto const& child : this->parent->children) {
80 if (child.get() == this) {
87 xbt_assert( child_nb > - 1);
88 std::vector<int> heir_list = this->parent->get_hierarchy();
89 heir_list.insert(heir_list.begin(), child_nb);
94 std::vector<int> heir_list = {top_level};
99 std::string Container::get_hierarchy_as_string()
101 std::string output("");
103 std::vector<int> heir_list = this->get_hierarchy();
105 unsigned int length = heir_list.size();
107 for (auto const& id : heir_list) {
108 output += std::to_string(id);
109 if( i != length-1 ) {
117 void Container::print_resources(FILE* jed_file)
120 xbt_assert(not this->resource_list.empty());
122 unsigned int res_nb = this->resource_list.size();
123 std::string resid = this->get_hierarchy_as_string();
125 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
126 for (auto const& res : this->resource_list) {
127 const char * res_name = sg_host_get_name(res);
128 fprintf(jed_file, "%s", res_name);
129 if( i != res_nb-1 ) {
130 fprintf(jed_file, "|");
134 fprintf(jed_file, "\" />\n");
137 void Container::print(FILE* jed_file)
139 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
140 if (not this->children.empty()) {
141 for (auto const& child : this->children) {
142 child->print(jed_file);
145 this->print_resources(jed_file);
147 fprintf(jed_file, " </res>\n");
150 } // namespace jedule
151 } // namespace simgrid
153 static void add_subsets_to(std::vector<simgrid::jedule::Subset>& subset_list, std::vector<const char*> hostgroup,
154 jed_container_t parent)
156 // get ids for each host
159 // create subset for each id group
161 xbt_assert( parent != nullptr );
163 std::vector<unsigned int> id_list;
165 for (auto const& host_name : hostgroup) {
166 xbt_assert( host_name != nullptr );
167 jed_container_t parent_cont = host2_simgrid_parent_container.at(host_name);
168 unsigned int id = parent_cont->name2id.at(host_name);
169 id_list.push_back(id);
171 unsigned int nb_ids = id_list.size();
172 std::sort(id_list.begin(), id_list.end());
177 for(unsigned int i=0; i<nb_ids; i++) {
178 if( id_list[i] - id_list[pos] > 1 ) {
179 subset_list.emplace_back(id_list[start], id_list[pos], parent);
182 if( i == nb_ids-1 ) {
183 subset_list.emplace_back(id_list[i], id_list[i], parent);
186 if( i == nb_ids-1 ) {
187 subset_list.emplace_back(id_list[start], id_list[i], parent);
195 void get_resource_selection_by_hosts(std::vector<simgrid::jedule::Subset>& subset_list,
196 const std::vector<sg_host_t>& host_list)
198 // for each host name
199 // find parent container
200 // group by parent container
201 std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
202 for (auto const& host : host_list) {
203 const char *host_name = sg_host_get_name(host);
204 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
205 xbt_assert( parent != nullptr );
207 auto host_group = parent2hostgroup.find(parent->name.c_str());
208 if (host_group == parent2hostgroup.end())
209 parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
211 host_group->second.push_back(host_name);
214 for (auto const& elm : parent2hostgroup) {
215 jed_container_t parent = container_name2container.at(elm.first);
216 add_subsets_to(subset_list, elm.second, parent);