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(std::string name) : name(std::move(name))
29 container_name2container.insert({this->name, this});
32 Container::~Container()
34 if (not this->children.empty())
35 for (auto const& child : this->children)
39 void Container::add_child(jed_container_t child)
41 xbt_assert(child != nullptr);
42 this->children.push_back(child);
46 void Container::add_resources(std::vector<sg_host_t> hosts)
49 this->children.clear();
52 for (auto const& host : hosts) {
53 const char *host_name = sg_host_get_name(host);
54 this->name2id.insert({host_name, this->last_id_});
56 host2_simgrid_parent_container.insert({host_name, this});
57 this->resource_list.push_back(host);
61 void Container::create_hierarchy(sg_netzone_t from_as)
64 if (from_as->get_children().empty()) {
66 // add hosts to jedule platform
67 std::vector<sg_host_t> table = from_as->get_all_hosts();
68 this->add_resources(table);
70 for (auto const& nz : from_as->get_children()) {
71 jed_container_t child_container = new simgrid::jedule::Container(std::string(nz->get_cname()));
72 this->add_child(child_container);
73 child_container->create_hierarchy(nz);
78 std::vector<int> Container::get_hierarchy()
80 if(this->parent != nullptr ) {
82 if (not this->parent->children.empty()) {
83 // we are in the last level
84 return this->parent->get_hierarchy();
89 for (auto const& child : this->parent->children) {
97 xbt_assert( child_nb > - 1);
98 std::vector<int> heir_list = this->parent->get_hierarchy();
99 heir_list.insert(heir_list.begin(), child_nb);
104 std::vector<int> heir_list = {top_level};
109 std::string Container::get_hierarchy_as_string()
111 std::string output("");
113 std::vector<int> heir_list = this->get_hierarchy();
115 unsigned int length = heir_list.size();
117 for (auto const& id : heir_list) {
118 output += std::to_string(id);
119 if( i != length-1 ) {
127 void Container::print_resources(FILE* jed_file)
130 xbt_assert(not this->resource_list.empty());
132 unsigned int res_nb = this->resource_list.size();
133 std::string resid = this->get_hierarchy_as_string();
135 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
136 for (auto const& res : this->resource_list) {
137 const char * res_name = sg_host_get_name(res);
138 fprintf(jed_file, "%s", res_name);
139 if( i != res_nb-1 ) {
140 fprintf(jed_file, "|");
144 fprintf(jed_file, "\" />\n");
147 void Container::print(FILE* jed_file)
149 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
150 if (not this->children.empty()) {
151 for (auto const& child : this->children) {
152 child->print(jed_file);
155 this->print_resources(jed_file);
157 fprintf(jed_file, " </res>\n");
163 static void add_subsets_to(std::vector<jed_subset_t> *subset_list, std::vector<const char*> hostgroup, jed_container_t parent)
165 // get ids for each host
168 // create subset for each id group
170 xbt_assert( parent != nullptr );
172 std::vector<unsigned int> id_list;
174 for (auto const& host_name : hostgroup) {
175 xbt_assert( host_name != nullptr );
176 jed_container_t parent_cont = host2_simgrid_parent_container.at(host_name);
177 unsigned int id = parent_cont->name2id.at(host_name);
178 id_list.push_back(id);
180 unsigned int nb_ids = id_list.size();
181 std::sort(id_list.begin(), id_list.end());
186 for(unsigned int i=0; i<nb_ids; i++) {
187 if( id_list[i] - id_list[pos] > 1 ) {
188 subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[pos], parent));
191 if( i == nb_ids-1 ) {
192 subset_list->push_back(new simgrid::jedule::Subset(id_list[i], id_list[i], parent));
195 if( i == nb_ids-1 ) {
196 subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[i], parent));
205 void get_resource_selection_by_hosts(std::vector<jed_subset_t> *subset_list, std::vector<sg_host_t> *host_list)
207 xbt_assert( host_list != nullptr );
208 // for each host name
209 // find parent container
210 // group by parent container
211 std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
212 for (auto const& host : *host_list) {
213 const char *host_name = sg_host_get_name(host);
214 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
215 xbt_assert( parent != nullptr );
217 auto host_group = parent2hostgroup.find(parent->name.c_str());
218 if (host_group == parent2hostgroup.end())
219 parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
221 host_group->second.push_back(host_name);
224 for (auto const& elm : parent2hostgroup) {
225 jed_container_t parent = container_name2container.at(elm.first);
226 add_subsets_to(subset_list, elm.second, parent);