1 /* Copyright (c) 2010-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "simgrid/jedule/jedule.hpp"
8 #include "simgrid/jedule/jedule_platform.hpp"
9 #include "simgrid/s4u/As.hpp"
10 #include "simgrid/host.h"
12 #include "xbt/asserts.h"
13 #include "xbt/dynar.h"
25 Container::Container(std::string name)
27 container_name2container.insert({this->name, this});
30 Container::~Container(){
31 if(!this->children.empty()) {
32 for (auto child: this->children){
38 void Container::addChild(jed_container_t child){
39 xbt_assert(this != nullptr);
40 xbt_assert(child != nullptr);
41 this->children.push_back(child);
45 void Container::addResources(std::vector<sg_host_t> hosts){
47 this->children.clear();
50 //FIXME do we need to sort?: xbt_dynar_sort_strings(host_names);
52 for (auto 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::createHierarchy(AS_t from_as){
62 xbt_dict_cursor_t cursor = nullptr;
65 xbt_dict_t routing_sons = from_as->children();
67 if (xbt_dict_is_empty(routing_sons)) {
69 // add hosts to jedule platform
70 xbt_dynar_t table = from_as->hosts();
71 unsigned int dynar_cursor;
74 std::vector<sg_host_t> hosts;
76 xbt_dynar_foreach(table, dynar_cursor, host) {
77 hosts.push_back(host);
79 this->addResources(hosts);
80 xbt_dynar_free(&table);
82 xbt_dict_foreach(routing_sons, cursor, key, elem) {
83 jed_container_t child_container = new simgrid::jedule::Container(std::string(elem->name()));
84 this->addChild(child_container);
85 child_container->createHierarchy(elem);
90 std::vector<int> Container::getHierarchy() {
91 xbt_assert( this!= nullptr );
93 if(this->parent != nullptr ) {
95 if(!this->parent->children.empty()) {
96 // we are in the last level
97 return this->parent->getHierarchy();
102 for (auto child : this->parent->children) {
110 xbt_assert( child_nb > - 1);
111 std::vector<int> heir_list = this->parent->getHierarchy();
112 heir_list.insert(heir_list.begin(), child_nb);
117 std::vector<int> heir_list = {top_level};
122 std::string Container::getHierarchyAsString(){
123 std::string output("");
125 std::vector<int> heir_list = this->getHierarchy();
127 unsigned int length = heir_list.size();
129 for (auto id : heir_list) {
130 output += std::to_string(id);
131 if( i != length-1 ) {
139 void Container::printResources(FILE * jed_file){
141 xbt_assert(!this->resource_list.empty());
143 unsigned int res_nb = this->resource_list.size();
144 std::string resid = this->getHierarchyAsString();
146 fprintf(jed_file, " <rset id=\"%s\" nb=\"%u\" names=\"", resid.c_str(), res_nb);
147 for (auto res: this->resource_list) {
148 const char * res_name = sg_host_get_name(res);
149 fprintf(jed_file, "%s", res_name);
150 if( i != res_nb-1 ) {
151 fprintf(jed_file, "|");
155 fprintf(jed_file, "\" />\n");
158 void Container::print(FILE* jed_file) {
159 xbt_assert( this != nullptr );
160 fprintf(jed_file, " <res name=\"%s\">\n", this->name.c_str());
161 if( !this->children.empty()){
162 for (auto child: this->children) {
163 child->print(jed_file);
166 this->printResources(jed_file);
168 fprintf(jed_file, " </res>\n");
174 static int compare_ids(const void *num1, const void *num2) {
175 return *((int*) num1) - *((int*) num2);
178 static void add_subset_to(xbt_dynar_t subset_list, int start, int end, jed_container_t parent) {
179 jed_res_subset_t subset;
181 xbt_assert( subset_list != nullptr );
182 xbt_assert( parent != nullptr );
184 subset = xbt_new0(s_jed_res_subset_t,1);
185 subset->start_idx = start;
186 subset->nres = end-start+1;
187 subset->parent = parent;
189 xbt_dynar_push(subset_list, &subset);
192 static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup, jed_container_t parent) {
197 // get ids for each host
200 // create subset for each id group
202 xbt_assert( subset_list != nullptr );
203 xbt_assert( hostgroup != nullptr );
204 xbt_assert( parent != nullptr );
206 xbt_dynar_t id_list = xbt_dynar_new(sizeof(int), nullptr);
208 xbt_dynar_foreach(hostgroup, iter, host_name) {
209 jed_container_t parent;
210 xbt_assert( host_name != nullptr );
211 parent = (jed_container_t)host2_simgrid_parent_container.at(host_name);
212 id = parent->name2id.at(host_name);
213 xbt_dynar_push(id_list, &id);
215 int nb_ids = xbt_dynar_length(id_list);
216 xbt_dynar_sort(id_list, &compare_ids);
218 int *id_ar = static_cast<int*>(xbt_dynar_to_array(id_list));
224 for(int i=0; i<nb_ids; i++) {
225 if( id_ar[i] - id_ar[pos] > 1 ) {
226 add_subset_to( subset_list, id_ar[start], id_ar[pos], parent );
229 if( i == nb_ids-1 ) {
230 add_subset_to( subset_list, id_ar[i], id_ar[i], parent );
233 if( i == nb_ids-1 ) {
234 add_subset_to( subset_list, id_ar[start], id_ar[i], parent );
244 void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list, std::vector<sg_host_t> *host_list) {
245 std::unordered_map<const char*, xbt_dynar_t> parent2hostgroup; // group hosts by parent
247 xbt_assert( host_list != nullptr );
249 // for each host name
250 // find parent container
251 // group by parent container
253 for (auto host: *host_list) {
254 const char *host_name = sg_host_get_name(host);
255 jed_container_t parent = (jed_container_t)host2_simgrid_parent_container.at(host_name);
256 xbt_assert( parent != nullptr );
258 auto host_group = parent2hostgroup.find(parent->name.c_str());
259 if (host_group == parent2hostgroup.end()){
260 xbt_dynar_t group = xbt_dynar_new(sizeof(char*), nullptr);
261 xbt_dynar_push(group, &host_name);
262 parent2hostgroup.insert({parent->name.c_str(), group});
264 xbt_dynar_push(host_group->second, &host_name);
268 for (auto elm: parent2hostgroup) {
269 jed_container_t parent = (jed_container_t)container_name2container.at(elm.first);
270 add_subsets_to(subset_list, elm.second, parent);
271 xbt_dynar_free_container(&elm.second);