1 /* Copyright (c) 2010-2017. 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/s4u/NetZone.hpp"
8 #include "xbt/asserts.h"
11 #if SIMGRID_HAVE_JEDULE
15 Subset::Subset(int start_idx, int end_idx, Container* parent)
16 : start_idx(start_idx), parent(parent)
18 nres=end_idx-start_idx+1;
22 Container::Container(std::string name): name(name)
24 container_name2container.insert({this->name, this});
27 Container::~Container()
29 if (not this->children.empty())
30 for (auto const& child : this->children)
34 void Container::addChild(jed_container_t child)
36 xbt_assert(child != nullptr);
37 this->children.push_back(child);
41 void Container::addResources(std::vector<sg_host_t> hosts)
44 this->children.clear();
47 //FIXME do we need to sort?: xbt_dynar_sort_strings(host_names);
49 for (auto const& host : hosts) {
50 const char *host_name = sg_host_get_name(host);
51 this->name2id.insert({host_name, this->last_id});
53 host2_simgrid_parent_container.insert({host_name, this});
54 this->resource_list.push_back(host);
58 void Container::createHierarchy(sg_netzone_t from_as)
61 if (from_as->getChildren()->empty()) {
63 // add hosts to jedule platform
64 std::vector<sg_host_t> table;
65 from_as->getHosts(&table);
66 this->addResources(table);
68 for (auto const& nz : *from_as->getChildren()) {
69 jed_container_t child_container = new simgrid::jedule::Container(std::string(nz->getCname()));
70 this->addChild(child_container);
71 child_container->createHierarchy(nz);
76 std::vector<int> Container::getHierarchy()
78 if(this->parent != nullptr ) {
80 if (not this->parent->children.empty()) {
81 // we are in the last level
82 return this->parent->getHierarchy();
87 for (auto const& child : this->parent->children) {
95 xbt_assert( child_nb > - 1);
96 std::vector<int> heir_list = this->parent->getHierarchy();
97 heir_list.insert(heir_list.begin(), child_nb);
102 std::vector<int> heir_list = {top_level};
107 std::string Container::getHierarchyAsString()
109 std::string output("");
111 std::vector<int> heir_list = this->getHierarchy();
113 unsigned int length = heir_list.size();
115 for (auto const& id : heir_list) {
116 output += std::to_string(id);
117 if( i != length-1 ) {
125 void Container::printResources(FILE * jed_file)
128 xbt_assert(not this->resource_list.empty());
130 unsigned int res_nb = this->resource_list.size();
131 std::string resid = this->getHierarchyAsString();
133 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
134 for (auto const& res : this->resource_list) {
135 const char * res_name = sg_host_get_name(res);
136 fprintf(jed_file, "%s", res_name);
137 if( i != res_nb-1 ) {
138 fprintf(jed_file, "|");
142 fprintf(jed_file, "\" />\n");
145 void Container::print(FILE* jed_file)
147 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
148 if (not this->children.empty()) {
149 for (auto const& child : this->children) {
150 child->print(jed_file);
153 this->printResources(jed_file);
155 fprintf(jed_file, " </res>\n");
161 static void add_subsets_to(std::vector<jed_subset_t> *subset_list, std::vector<const char*> hostgroup, jed_container_t parent)
163 // get ids for each host
166 // create subset for each id group
168 xbt_assert( parent != nullptr );
170 std::vector<unsigned int> id_list;
172 for (auto const& host_name : hostgroup) {
173 xbt_assert( host_name != nullptr );
174 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
175 unsigned int id = parent->name2id.at(host_name);
176 id_list.push_back(id);
178 unsigned int nb_ids = id_list.size();
179 std::sort(id_list.begin(), id_list.end());
184 for(unsigned int i=0; i<nb_ids; i++) {
185 if( id_list[i] - id_list[pos] > 1 ) {
186 subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[pos], parent));
189 if( i == nb_ids-1 ) {
190 subset_list->push_back(new simgrid::jedule::Subset(id_list[i], id_list[i], parent));
193 if( i == nb_ids-1 ) {
194 subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[i], parent));
203 void get_resource_selection_by_hosts(std::vector<jed_subset_t> *subset_list, std::vector<sg_host_t> *host_list)
205 xbt_assert( host_list != nullptr );
206 // for each host name
207 // find parent container
208 // group by parent container
209 std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
210 for (auto const& host : *host_list) {
211 const char *host_name = sg_host_get_name(host);
212 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
213 xbt_assert( parent != nullptr );
215 auto host_group = parent2hostgroup.find(parent->name.c_str());
216 if (host_group == parent2hostgroup.end())
217 parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
219 host_group->second.push_back(host_name);
222 for (auto const& elm : parent2hostgroup) {
223 jed_container_t parent = container_name2container.at(elm.first);
224 add_subsets_to(subset_list, elm.second, parent);