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/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::addChild(jed_container_t child)
42 xbt_assert(child != nullptr);
43 this->children.push_back(child);
47 void Container::addResources(std::vector<sg_host_t> hosts)
50 this->children.clear();
53 //FIXME do we need to sort?: xbt_dynar_sort_strings(host_names);
55 for (auto const& host : hosts) {
56 const char *host_name = sg_host_get_name(host);
57 this->name2id.insert({host_name, this->last_id});
59 host2_simgrid_parent_container.insert({host_name, this});
60 this->resource_list.push_back(host);
64 void Container::createHierarchy(sg_netzone_t from_as)
67 if (from_as->getChildren()->empty()) {
69 // add hosts to jedule platform
70 std::vector<sg_host_t> table;
71 from_as->getHosts(&table);
72 this->addResources(table);
74 for (auto const& nz : *from_as->getChildren()) {
75 jed_container_t child_container = new simgrid::jedule::Container(std::string(nz->getCname()));
76 this->addChild(child_container);
77 child_container->createHierarchy(nz);
82 std::vector<int> Container::getHierarchy()
84 if(this->parent != nullptr ) {
86 if (not this->parent->children.empty()) {
87 // we are in the last level
88 return this->parent->getHierarchy();
93 for (auto const& child : this->parent->children) {
101 xbt_assert( child_nb > - 1);
102 std::vector<int> heir_list = this->parent->getHierarchy();
103 heir_list.insert(heir_list.begin(), child_nb);
108 std::vector<int> heir_list = {top_level};
113 std::string Container::getHierarchyAsString()
115 std::string output("");
117 std::vector<int> heir_list = this->getHierarchy();
119 unsigned int length = heir_list.size();
121 for (auto const& id : heir_list) {
122 output += std::to_string(id);
123 if( i != length-1 ) {
131 void Container::printResources(FILE * jed_file)
134 xbt_assert(not this->resource_list.empty());
136 unsigned int res_nb = this->resource_list.size();
137 std::string resid = this->getHierarchyAsString();
139 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
140 for (auto const& res : this->resource_list) {
141 const char * res_name = sg_host_get_name(res);
142 fprintf(jed_file, "%s", res_name);
143 if( i != res_nb-1 ) {
144 fprintf(jed_file, "|");
148 fprintf(jed_file, "\" />\n");
151 void Container::print(FILE* jed_file)
153 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
154 if (not this->children.empty()) {
155 for (auto const& child : this->children) {
156 child->print(jed_file);
159 this->printResources(jed_file);
161 fprintf(jed_file, " </res>\n");
167 static void add_subsets_to(std::vector<jed_subset_t> *subset_list, std::vector<const char*> hostgroup, jed_container_t parent)
169 // get ids for each host
172 // create subset for each id group
174 xbt_assert( parent != nullptr );
176 std::vector<unsigned int> id_list;
178 for (auto const& host_name : hostgroup) {
179 xbt_assert( host_name != nullptr );
180 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
181 unsigned int id = parent->name2id.at(host_name);
182 id_list.push_back(id);
184 unsigned int nb_ids = id_list.size();
185 std::sort(id_list.begin(), id_list.end());
190 for(unsigned int i=0; i<nb_ids; i++) {
191 if( id_list[i] - id_list[pos] > 1 ) {
192 subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[pos], parent));
195 if( i == nb_ids-1 ) {
196 subset_list->push_back(new simgrid::jedule::Subset(id_list[i], id_list[i], parent));
199 if( i == nb_ids-1 ) {
200 subset_list->push_back(new simgrid::jedule::Subset(id_list[start], id_list[i], parent));
209 void get_resource_selection_by_hosts(std::vector<jed_subset_t> *subset_list, std::vector<sg_host_t> *host_list)
211 xbt_assert( host_list != nullptr );
212 // for each host name
213 // find parent container
214 // group by parent container
215 std::unordered_map<const char*, std::vector<const char*>> parent2hostgroup;
216 for (auto const& host : *host_list) {
217 const char *host_name = sg_host_get_name(host);
218 jed_container_t parent = host2_simgrid_parent_container.at(host_name);
219 xbt_assert( parent != nullptr );
221 auto host_group = parent2hostgroup.find(parent->name.c_str());
222 if (host_group == parent2hostgroup.end())
223 parent2hostgroup.insert({parent->name.c_str(), std::vector<const char*>(1,host_name)});
225 host_group->second.push_back(host_name);
228 for (auto const& elm : parent2hostgroup) {
229 jed_container_t parent = container_name2container.at(elm.first);
230 add_subsets_to(subset_list, elm.second, parent);