1 /* Copyright (c) 2010-2021. 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/config.h"
8 #include "simgrid/host.h"
9 #include "simgrid/s4u/NetZone.hpp"
10 #include "xbt/asserts.h"
13 #if SIMGRID_HAVE_JEDULE
16 std::unordered_map<const char*, jed_container_t> host2_simgrid_parent_container;
17 std::unordered_map<std::string, jed_container_t> container_name2container;
22 Subset::Subset(int start_idx, int end_idx, Container* parent)
23 : start_idx(start_idx), nres(end_idx - start_idx + 1), parent(parent)
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 children_.emplace_back(child);
36 child->set_parent(this);
39 void Container::add_resources(const std::vector<sg_host_t>& hosts)
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(const_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 auto* child_container = new simgrid::jedule::Container(nz->get_name());
63 this->add_child(child_container);
64 child_container->create_hierarchy(nz);
69 int Container::get_child_position(const Container* child) const
71 auto it = std::find_if(begin(children_), end(children_),
72 [&child](const std::unique_ptr<Container>& c) { return c.get() == child; });
73 return it == end(children_) ? -1 : static_cast<int>(std::distance(begin(children_), it));
76 std::vector<int> Container::get_hierarchy()
78 if (parent_ == nullptr) {
80 std::vector<int> hier_list = {top_level};
82 } else if (parent_->has_children()) {
83 int child_nb = parent_->get_child_position(this);
84 xbt_assert(child_nb > -1);
85 std::vector<int> hier_list = parent_->get_hierarchy();
86 hier_list.insert(hier_list.begin(), child_nb);
89 // we are in the last level
90 return parent_->get_hierarchy();
94 std::string Container::get_hierarchy_as_string()
96 std::string output("");
98 std::vector<int> hier_list = this->get_hierarchy();
101 for (auto const& id : hier_list) {
106 output += std::to_string(id);
112 void Container::print_resources(FILE* jed_file)
114 xbt_assert(not this->resource_list.empty());
116 std::string resid = this->get_hierarchy_as_string();
118 fprintf(jed_file, " <rset id=\"%s\" nb=\"%zu\" names=\"", resid.c_str(), this->resource_list.size());
120 for (auto const& res : this->resource_list) {
125 const char * res_name = sg_host_get_name(res);
126 fprintf(jed_file, "%s", res_name);
128 fprintf(jed_file, "\" />\n");
131 void Container::print(FILE* jed_file)
133 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
134 if (not children_.empty()) {
135 for (auto const& child : children_) {
136 child->print(jed_file);
139 this->print_resources(jed_file);
141 fprintf(jed_file, " </res>\n");
144 } // namespace jedule
145 } // namespace simgrid
147 static void add_subsets_to(std::vector<simgrid::jedule::Subset>& subset_list, const std::vector<const char*>& hostgroup,
148 jed_container_t parent)
150 // get ids for each host
153 // create subset for each id group
155 xbt_assert( parent != nullptr );
157 std::vector<unsigned int> id_list;
159 for (auto const& host_name : hostgroup) {
160 xbt_assert( host_name != nullptr );
161 const simgrid::jedule::Container* parent_cont = host2_simgrid_parent_container.at(host_name);
162 unsigned int id = parent_cont->get_id_by_name(host_name);
163 id_list.push_back(id);
165 std::sort(id_list.begin(), id_list.end());
167 size_t nb_ids = id_list.size();
170 for (size_t i = 0; i < nb_ids; i++) {
171 if (id_list[i] - id_list[pos] > 1) {
172 subset_list.emplace_back(id_list[start], id_list[pos], parent);
175 if (i == nb_ids - 1) {
176 subset_list.emplace_back(id_list[i], id_list[i], parent);
179 if (i == nb_ids - 1) {
180 subset_list.emplace_back(id_list[start], id_list[i], parent);
187 void get_resource_selection_by_hosts(std::vector<simgrid::jedule::Subset>& subset_list,
188 const std::vector<sg_host_t>& host_list)
190 // for each host name
191 // find parent container
192 // group by parent container
193 std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
194 for (auto const& host : host_list) {
195 const char *host_name = sg_host_get_name(host);
196 const simgrid::jedule::Container* parent = host2_simgrid_parent_container.at(host_name);
197 xbt_assert( parent != nullptr );
199 auto host_group = parent2hostgroup.find(parent->get_cname());
200 if (host_group == parent2hostgroup.end())
201 parent2hostgroup.insert({parent->get_cname(), std::vector<const char*>(1, host_name)});
203 host_group->second.push_back(host_name);
206 for (auto const& elm : parent2hostgroup) {
207 jed_container_t parent = container_name2container.at(elm.first);
208 add_subsets_to(subset_list, elm.second, parent);