X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/066bc21e2478c4a57290c8847351b72de8e0419c..9735172c2d9037d0dc86a53ebae3ca195bc86fca:/src/simdag/sd_dotloader.cpp diff --git a/src/simdag/sd_dotloader.cpp b/src/simdag/sd_dotloader.cpp index b2008872e7..860f4092d4 100644 --- a/src/simdag/sd_dotloader.cpp +++ b/src/simdag/sd_dotloader.cpp @@ -1,25 +1,135 @@ -/* Copyright (c) 2009-2017. The SimGrid Team. +/* Copyright (c) 2009-2021. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ #include "simdag_private.hpp" +#include "simgrid/s4u/Activity.hpp" +#include "simgrid/s4u/Comm.hpp" +#include "simgrid/s4u/Engine.hpp" +#include "simgrid/s4u/Exec.hpp" #include "simgrid/simdag.h" #include "src/internal_config.h" -#include "xbt/file.h" +#include "xbt/file.hpp" +#include #include #include - -XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files"); +#include #if HAVE_GRAPHVIZ #include +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files"); + +namespace simgrid { +namespace s4u { + +std::vector create_DAG_from_dot(const std::string& filename) +{ + FILE* in_file = fopen(filename.c_str(), "r"); + xbt_assert(in_file != nullptr, "Failed to open file: %s", filename.c_str()); + + Agraph_t* dag_dot = agread(in_file, NIL(Agdisc_t*)); + + std::unordered_map activities; + std::vector dag; + + ActivityPtr root; + ActivityPtr end; + ActivityPtr act; + /* Create all the nodes */ + Agnode_t* node = nullptr; + for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) { + char* name = agnameof(node); + double amount = atof(agget(node, (char*)"size")); + + if (activities.find(name) == activities.end()) { + XBT_DEBUG("See ", name, amount); + act = Exec::init()->set_name(name)->set_flops_amount(amount)->vetoable_start(); + activities.insert({std::string(name), act}); + if (strcmp(name, "root") && strcmp(name, "end")) + dag.push_back(act); + } else { + XBT_WARN("Exec '%s' is defined more than once", name); + } + } + /*Check if 'root' and 'end' nodes have been explicitly declared. If not, create them. */ + if (activities.find("root") == activities.end()) + root = Exec::init()->set_name("root")->set_flops_amount(0)->vetoable_start(); + else + root = activities.at("root"); + + if (activities.find("end") == activities.end()) + end = Exec::init()->set_name("end")->set_flops_amount(0)->vetoable_start(); + else + end = activities.at("end"); + + /* Create edges */ + std::vector edges; + for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) { + edges.clear(); + for (Agedge_t* edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge)) + edges.push_back(edge); + + /* Be sure edges are sorted */ + std::sort(edges.begin(), edges.end(), [](const Agedge_t* a, const Agedge_t* b) { return AGSEQ(a) < AGSEQ(b); }); + + for (Agedge_t* edge : edges) { + const char* src_name = agnameof(agtail(edge)); + const char* dst_name = agnameof(aghead(edge)); + double size = atof(agget(edge, (char*)"size")); + + ActivityPtr src = activities.at(src_name); + ActivityPtr dst = activities.at(dst_name); + if (size > 0) { + std::string name = std::string(src_name) + "->" + dst_name; + XBT_DEBUG("See ", name.c_str(), size); + if (activities.find(name) == activities.end()) { + act = Comm::sendto_init()->set_name(name)->set_payload_size(size)->vetoable_start(); + src->add_successor(act); + act->add_successor(dst); + activities.insert({name, act}); + dag.push_back(act); + } else { + XBT_WARN("Comm '%s' is defined more than once", name.c_str()); + } + } else { + src->add_successor(dst); + } + } + } + + XBT_DEBUG("All activities have been created, put %s at the beginning and %s at the end", root->get_cname(), + end->get_cname()); + dag.insert(dag.begin(), root); + dag.push_back(end); + + /* Connect entry tasks to 'root', and exit tasks to 'end'*/ + for (const auto& a : dag) { + if (a->dependencies_solved() && a != root) { + XBT_DEBUG("Activity '%s' has no dependencies. Add dependency from 'root'", a->get_cname()); + root->add_successor(a); + } + + if (a->is_waited_by() == 0 && a != end) { + XBT_DEBUG("Activity '%s' has no successors. Add dependency to 'end'", a->get_cname()); + a->add_successor(end); + } + } + agclose(dag_dot); + fclose(in_file); + + return dag; +} + +} // namespace s4u +} // namespace simgrid + xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool schedule); static void dot_task_p_free(void *task) { - SD_task_destroy(*(SD_task_t *)task); + (*(SD_task_t*)task)->destroy(); } /** @brief loads a DOT file describing a DAG @@ -28,7 +138,7 @@ static void dot_task_p_free(void *task) { * The size attribute of a node describes: * - for a compute task: the amount of flops to execute * - for a communication task : the amount of bytes to transfer - * If this attribute is ommited, the default value is zero. + * If this attribute is omitted, the default value is zero. */ xbt_dynar_t SD_dotload(const char *filename) { return SD_dotload_generic(filename, true, false); @@ -42,23 +152,12 @@ xbt_dynar_t SD_dotload_with_sched(const char *filename) { return SD_dotload_generic(filename, true, true); } -static int edge_compare(const void *a, const void *b) -{ - unsigned va = AGSEQ(*(Agedge_t **)a); - unsigned vb = AGSEQ(*(Agedge_t **)b); - if (va == vb) - return 0; - else - return (va < vb ? -1 : 1); -} - xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool schedule) { xbt_assert(filename, "Unable to use a null file descriptor\n"); FILE *in_file = fopen(filename, "r"); xbt_assert(in_file != nullptr, "Failed to open file: %s", filename); - unsigned int i; SD_task_t root; SD_task_t end; SD_task_t task; @@ -79,11 +178,11 @@ xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool sched if (jobs.find(name) == jobs.end()) { if (sequential) { XBT_DEBUG("See ", name, amount); - task = SD_task_create_comp_seq(name, nullptr , amount); + task = simgrid::sd::Task::create_comp_seq(name, amount, nullptr); } else { double alpha = atof(agget(node, (char *) "alpha")); XBT_DEBUG("See ", name, amount, alpha); - task = SD_task_create_comp_par_amdahl(name, nullptr , amount, alpha); + task = simgrid::sd::Task::create_comp_par_amdahl(name, amount, nullptr, alpha); } jobs.insert({std::string(name), task}); @@ -91,8 +190,7 @@ xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool sched if (strcmp(name,"root") && strcmp(name,"end")) xbt_dynar_push(result, &task); - if ((sequential) && - ((schedule && schedule_success) || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose))) { + if (sequential && ((schedule && schedule_success) || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose))) { /* try to take the information to schedule the task only if all is right*/ char *char_performer = agget(node, (char *) "performer"); char *char_order = agget(node, (char *) "order"); @@ -102,20 +200,21 @@ xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool sched if ((performer != -1 && order != -1) && performer < static_cast(sg_host_count())) { /* required parameters are given and less performers than hosts are required */ - XBT_DEBUG ("Task '%s' is scheduled on workstation '%d' in position '%d'", task->name, performer, order); - try { - computer = computers.at(char_performer); - } catch (std::out_of_range& unfound) { - computer = new std::vector; + XBT_DEBUG("Task '%s' is scheduled on workstation '%d' in position '%d'", task->get_cname(), performer, order); + auto comp = computers.find(char_performer); + if (comp != computers.end()) { + computer = comp->second; + } else { + computer = new std::vector(); computers.insert({char_performer, computer}); } if (static_cast(order) < computer->size()) { - SD_task_t task_test = computer->at(order); + const_SD_task_t task_test = computer->at(order); if (task_test && task_test != task) { /* the user gave the same order to several tasks */ schedule_success = false; XBT_VERB("Task '%s' wants to start on performer '%s' at the same position '%s' as task '%s'", - task_test->name, char_performer, char_order, task->name); + task_test->get_cname(), char_performer, char_order, task->get_cname()); continue; } } else @@ -125,7 +224,7 @@ xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool sched } else { /* one of required parameters is not given */ schedule_success = false; - XBT_VERB("The schedule is ignored, task '%s' can not be scheduled on %d hosts", task->name, performer); + XBT_VERB("The schedule is ignored, task '%s' can not be scheduled on %d hosts", task->get_cname(), performer); } } } else { @@ -135,34 +234,33 @@ xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool sched /*Check if 'root' and 'end' nodes have been explicitly declared. If not, create them. */ if (jobs.find("root") == jobs.end()) - root = (sequential ? SD_task_create_comp_seq("root", nullptr, 0) - : SD_task_create_comp_par_amdahl("root", nullptr, 0, 0)); + root = (sequential ? simgrid::sd::Task::create_comp_seq("root", 0, nullptr) + : simgrid::sd::Task::create_comp_par_amdahl("root", 0, nullptr, 0)); else root = jobs.at("root"); - SD_task_set_state(root, SD_SCHEDULABLE); /* by design the root task is always SCHEDULABLE */ + root->set_state(SD_SCHEDULABLE); /* by design the root task is always SCHEDULABLE */ xbt_dynar_insert_at(result, 0, &root); /* Put it at the beginning of the dynar */ if (jobs.find("end") == jobs.end()) - end = (sequential ? SD_task_create_comp_seq("end", nullptr, 0) - : SD_task_create_comp_par_amdahl("end", nullptr, 0, 0)); + end = (sequential ? simgrid::sd::Task::create_comp_seq("end", 0, nullptr) + : simgrid::sd::Task::create_comp_par_amdahl("end", 0, nullptr, 0)); else end = jobs.at("end"); /* Create edges */ - xbt_dynar_t edges = xbt_dynar_new(sizeof(Agedge_t*), nullptr); + std::vector edges; for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) { - Agedge_t * edge; - xbt_dynar_reset(edges); - for (edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge)) - xbt_dynar_push_as(edges, Agedge_t *, edge); + edges.clear(); + for (Agedge_t* edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge)) + edges.push_back(edge); /* Be sure edges are sorted */ - xbt_dynar_sort(edges, edge_compare); + std::sort(edges.begin(), edges.end(), [](const Agedge_t* a, const Agedge_t* b) { return AGSEQ(a) < AGSEQ(b); }); - xbt_dynar_foreach(edges, i, edge) { - char *src_name=agnameof(agtail(edge)); - char *dst_name=agnameof(aghead(edge)); + for (Agedge_t* edge : edges) { + const char* src_name = agnameof(agtail(edge)); + const char* dst_name = agnameof(aghead(edge)); double size = atof(agget(edge, (char *) "size")); SD_task_t src = jobs.at(src_name); @@ -173,36 +271,36 @@ xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool sched XBT_DEBUG("See ", name.c_str(), size); if (jobs.find(name) == jobs.end()) { if (sequential) - task = SD_task_create_comm_e2e(name.c_str(), nullptr, size); + task = simgrid::sd::Task::create_comm_e2e(name.c_str(), size, nullptr); else - task = SD_task_create_comm_par_mxn_1d_block(name.c_str(), nullptr, size); - SD_task_dependency_add(nullptr, nullptr, src, task); - SD_task_dependency_add(nullptr, nullptr, task, dst); + task = simgrid::sd::Task::create_comm_par_mxn_1d_block(name.c_str(), size, nullptr); + SD_task_dependency_add(src, task); + SD_task_dependency_add(task, dst); jobs.insert({name, task}); xbt_dynar_push(result, &task); } else { XBT_WARN("Task '%s' is defined more than once", name.c_str()); } } else { - SD_task_dependency_add(nullptr, nullptr, src, dst); + SD_task_dependency_add(src, dst); } } } - xbt_dynar_free(&edges); - XBT_DEBUG("All tasks have been created, put %s at the end of the dynar", end->name); + XBT_DEBUG("All tasks have been created, put %s at the end of the dynar", end->get_cname()); xbt_dynar_push(result, &end); /* Connect entry tasks to 'root', and exit tasks to 'end'*/ + unsigned i; xbt_dynar_foreach (result, i, task){ - if (task->predecessors->empty() && task->inputs->empty() && task != root) { - XBT_DEBUG("Task '%s' has no source. Add dependency from 'root'", task->name); - SD_task_dependency_add(nullptr, nullptr, root, task); + if (task->has_unsolved_dependencies() == 0 && task != root) { + XBT_DEBUG("Task '%s' has no source. Add dependency from 'root'", task->get_cname()); + SD_task_dependency_add(root, task); } - if (task->successors->empty() && task->outputs->empty() && task != end) { - XBT_DEBUG("Task '%s' has no destination. Add dependency to 'end'", task->name); - SD_task_dependency_add(nullptr, nullptr, task, end); + if (task->is_waited_by() == 0 && task != end) { + XBT_DEBUG("Task '%s' has no destination. Add dependency to 'end'", task->get_cname()); + SD_task_dependency_add(task, end); } } @@ -211,24 +309,25 @@ xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool sched if(schedule){ if (schedule_success) { - const sg_host_t *workstations = sg_host_list (); - for (auto elm : computers) { + std::vector hosts = simgrid::s4u::Engine::get_instance()->get_all_hosts(); + + for (auto const& elm : computers) { SD_task_t previous_task = nullptr; - for (auto task : *elm.second) { + for (auto const& cur_task : *elm.second) { /* add dependency between the previous and the task to avoid parallel execution */ - if(task){ - if (previous_task && not SD_task_dependency_exists(previous_task, task)) - SD_task_dependency_add(nullptr, nullptr, previous_task, task); + if (cur_task) { + if (previous_task && not SD_task_dependency_exists(previous_task, cur_task)) + SD_task_dependency_add(previous_task, cur_task); - SD_task_schedulel(task, 1, workstations[atoi(elm.first.c_str())]); - previous_task = task; + cur_task->schedulev({hosts[std::stoi(elm.first)]}); + previous_task = cur_task; } } delete elm.second; } } else { XBT_WARN("The scheduling is ignored"); - for (auto elm : computers) + for (auto const& elm : computers) delete elm.second; xbt_dynar_free(&result); result = nullptr; @@ -236,15 +335,24 @@ xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool sched } if (result && not acyclic_graph_detail(result)) { - char* base = xbt_basename(filename); - XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.", base); - free(base); + std::string base = simgrid::xbt::Path(filename).get_base_name(); + XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.", base.c_str()); xbt_dynar_free(&result); result = nullptr; } return result; } #else +namespace simgrid { +namespace s4u { +std::vector create_DAG_from_dot(const std::string& filename) +{ + xbt_die("create_DAG_from_dot() is not usable because graphviz was not found.\n" + "Please install graphviz, graphviz-dev, and libgraphviz-dev (and erase CMakeCache.txt) before recompiling."); +} +} // namespace s4u +} // namespace simgrid + xbt_dynar_t SD_dotload(const char *filename) { xbt_die("SD_dotload_generic() is not usable because graphviz was not found.\n" "Please install graphviz, graphviz-dev, and libgraphviz-dev (and erase CMakeCache.txt) before recompiling.");