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;
28 Container::Container(std::string name): name(name)
30 container_name2container.insert({this->name, this});
33 Container::~Container()
35 if (not this->children.empty())
36 for (auto const& child : this->children)
40 void Container::add_child(jed_container_t child)
42 xbt_assert(child != nullptr);
43 this->children.push_back(child);
47 void Container::add_resources(std::vector<sg_host_t> hosts)
50 this->children.clear();
53 for (auto const& host : hosts) {
54 const char *host_name = sg_host_get_name(host);
55 this->name2id.insert({host_name, this->last_id_});
57 host2_simgrid_parent_container.insert({host_name, this});
58 this->resource_list.push_back(host);
62 void Container::create_hierarchy(sg_netzone_t from_as)
65 if (from_as->get_children().empty()) {
67 // add hosts to jedule platform
68 std::vector<sg_host_t> table = from_as->get_all_hosts();
69 this->add_resources(table);
71 for (auto const& nz : from_as->get_children()) {
72 jed_container_t child_container = new simgrid::jedule::Container(std::string(nz->get_cname()));
73 this->add_child(child_container);
74 child_container->create_hierarchy(nz);
79 std::vector<int> Container::get_hierarchy()
81 if(this->parent != nullptr ) {
83 if (not this->parent->children.empty()) {
84 // we are in the last level
85 return this->parent->get_hierarchy();
90 for (auto const& child : this->parent->children) {
98 xbt_assert( child_nb > - 1);
99 std::vector<int> heir_list = this->parent->get_hierarchy();
100 heir_list.insert(heir_list.begin(), child_nb);
105 std::vector<int> heir_list = {top_level};
110 std::string Container::get_hierarchy_as_string()
112 std::string output("");
114 std::vector<int> heir_list = this->get_hierarchy();
116 unsigned int length = heir_list.size();
118 for (auto const& id : heir_list) {
119 output += std::to_string(id);
120 if( i != length-1 ) {
128 void Container::print_resources(FILE* jed_file)
131 xbt_assert(not this->resource_list.empty());
133 unsigned int res_nb = this->resource_list.size();
134 std::string resid = this->get_hierarchy_as_string();
136 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
137 for (auto const& res : this->resource_list) {
138 const char * res_name = sg_host_get_name(res);
139 fprintf(jed_file, "%s", res_name);
140 if( i != res_nb-1 ) {
141 fprintf(jed_file, "|");
145 fprintf(jed_file, "\" />\n");
148 void Container::print(FILE* jed_file)
150 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
151 if (not this->children.empty()) {
152 for (auto const& child : this->children) {
153 child->print(jed_file);
156 this->print_resources(jed_file);
158 fprintf(jed_file, " </res>\n");
164 static void add_subsets_to(std::vector<jed_subset_t> *subset_list, std::vector<const char*> hostgroup, jed_container_t parent)
166 // get ids for each host
169 // create subset for each id group
171 xbt_assert( parent != nullptr );
173 std::vector<unsigned int> id_list;
175 for (auto const& host_name : hostgroup) {
176 xbt_assert( host_name != nullptr );
177 jed_container_t parent_cont = host2_simgrid_parent_container.at(host_name);
178 unsigned int id = parent_cont->name2id.at(host_name);
179 id_list.push_back(id);
181 unsigned int nb_ids = id_list.size();
182 std::sort(id_list.begin(), id_list.end());
187 for(unsigned int i=0; i<nb_ids; i++) {
188 if( id_list[i] - id_list[pos] > 1 ) {
189 subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[pos], parent));
192 if( i == nb_ids-1 ) {
193 subset_list->push_back(new simgrid::jedule::Subset(id_list[i], id_list[i], parent));
196 if( i == nb_ids-1 ) {
197 subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[i], parent));
206 void get_resource_selection_by_hosts(std::vector<jed_subset_t> *subset_list, std::vector<sg_host_t> *host_list)
208 xbt_assert( host_list != nullptr );
209 // for each host name
210 // find parent container
211 // group by parent container
212 std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
213 for (auto const& host : *host_list) {
214 const char *host_name = sg_host_get_name(host);
215 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
216 xbt_assert( parent != nullptr );
218 auto host_group = parent2hostgroup.find(parent->name.c_str());
219 if (host_group == parent2hostgroup.end())
220 parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
222 host_group->second.push_back(host_name);
225 for (auto const& elm : parent2hostgroup) {
226 jed_container_t parent = container_name2container.at(elm.first);
227 add_subsets_to(subset_list, elm.second, parent);