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 child: this->children)
34 void Container::addChild(jed_container_t child)
36 xbt_assert(this != nullptr);
37 xbt_assert(child != nullptr);
38 this->children.push_back(child);
42 void Container::addResources(std::vector<sg_host_t> hosts)
45 this->children.clear();
48 //FIXME do we need to sort?: xbt_dynar_sort_strings(host_names);
50 for (auto host : hosts) {
51 const char *host_name = sg_host_get_name(host);
52 this->name2id.insert({host_name, this->last_id});
54 host2_simgrid_parent_container.insert({host_name, this});
55 this->resource_list.push_back(host);
59 void Container::createHierarchy(sg_netzone_t from_as)
62 if (from_as->getChildren()->empty()) {
64 // add hosts to jedule platform
65 std::vector<sg_host_t> table;
66 from_as->getHosts(&table);
67 this->addResources(table);
69 for (auto nz : *from_as->getChildren()) {
70 jed_container_t child_container = new simgrid::jedule::Container(std::string(nz->getCname()));
71 this->addChild(child_container);
72 child_container->createHierarchy(nz);
77 std::vector<int> Container::getHierarchy()
79 xbt_assert( this!= nullptr );
81 if(this->parent != nullptr ) {
83 if (not this->parent->children.empty()) {
84 // we are in the last level
85 return this->parent->getHierarchy();
90 for (auto child : this->parent->children) {
98 xbt_assert( child_nb > - 1);
99 std::vector<int> heir_list = this->parent->getHierarchy();
100 heir_list.insert(heir_list.begin(), child_nb);
105 std::vector<int> heir_list = {top_level};
110 std::string Container::getHierarchyAsString()
112 std::string output("");
114 std::vector<int> heir_list = this->getHierarchy();
116 unsigned int length = heir_list.size();
118 for (auto id : heir_list) {
119 output += std::to_string(id);
120 if( i != length-1 ) {
128 void Container::printResources(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->getHierarchyAsString();
136 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
137 for (auto 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 xbt_assert( this != nullptr );
151 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
152 if (not this->children.empty()) {
153 for (auto child: this->children) {
154 child->print(jed_file);
157 this->printResources(jed_file);
159 fprintf(jed_file, " </res>\n");
165 static void add_subsets_to(std::vector<jed_subset_t> *subset_list, std::vector<const char*> hostgroup, jed_container_t parent)
167 // get ids for each host
170 // create subset for each id group
172 xbt_assert( parent != nullptr );
174 std::vector<unsigned int> id_list;
176 for (auto host_name : hostgroup) {
177 xbt_assert( host_name != nullptr );
178 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
179 unsigned int id = parent->name2id.at(host_name);
180 id_list.push_back(id);
182 unsigned int nb_ids = id_list.size();
183 std::sort(id_list.begin(), id_list.end());
188 for(unsigned int i=0; i<nb_ids; i++) {
189 if( id_list[i] - id_list[pos] > 1 ) {
190 subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[pos], parent));
193 if( i == nb_ids-1 ) {
194 subset_list->push_back(new simgrid::jedule::Subset(id_list[i], id_list[i], parent));
197 if( i == nb_ids-1 ) {
198 subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[i], parent));
207 void get_resource_selection_by_hosts(std::vector<jed_subset_t> *subset_list, std::vector<sg_host_t> *host_list)
209 xbt_assert( host_list != nullptr );
210 // for each host name
211 // find parent container
212 // group by parent container
213 std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
214 for (auto host: *host_list) {
215 const char *host_name = sg_host_get_name(host);
216 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
217 xbt_assert( parent != nullptr );
219 auto host_group = parent2hostgroup.find(parent->name.c_str());
220 if (host_group == parent2hostgroup.end())
221 parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
223 host_group->second.push_back(host_name);
226 for (auto elm: parent2hostgroup) {
227 jed_container_t parent = container_name2container.at(elm.first);
228 add_subsets_to(subset_list, elm.second, parent);