diff --git a/.github/workflows/partitioner-benchmark.yml b/.github/workflows/partitioner-benchmark.yml new file mode 100644 index 00000000..9b266934 --- /dev/null +++ b/.github/workflows/partitioner-benchmark.yml @@ -0,0 +1,65 @@ +name: Partitioner Benchmark + +on: + push: + pull_request: + branches: [ sb-graph-dev ] + +concurrency: + group: partitioner-benchmark-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: write + +jobs: + benchmark: + runs-on: ubuntu-24.04 + timeout-minutes: 120 + + steps: + - uses: actions/checkout@v2 + + - name: Install deps. + run: | + sudo apt-get update + sudo apt-get install libboost1.83-dev + sudo apt-get install libboost-program-options-dev + sudo apt-get install cmake + sudo apt-get install g++ + sudo apt-get install rapidjson-dev + + - name: Configure + run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Release + + - name: Build sbg-partitioner + run: cmake --build build -j --target sbg-partitioner + + - name: Run benchmark + run: | + python3 test/partitioner/benchmark/run_benchmark.py \ + --binary build/algorithms/partitioner/sbg-partitioner \ + --output bench.json \ + --repetitions 3 \ + --timeout 900 + + - name: Upload raw results + if: always() + uses: actions/upload-artifact@v4 + with: + name: partitioner-bench-${{ github.run_id }} + path: bench.json + retention-days: 90 + + - name: Compare against history / store baseline + uses: benchmark-action/github-action-benchmark@v1 + with: + tool: 'customSmallerIsBetter' + output-file-path: bench.json + github-token: ${{ secrets.GITHUB_TOKEN }} + benchmark-data-dir-path: dev/bench/partitioner + alert-threshold: '120%' + fail-on-alert: true + comment-on-alert: false + auto-push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/sb-graph-dev' }} + save-data-file: ${{ github.event_name == 'push' && github.ref == 'refs/heads/sb-graph-dev' }} diff --git a/README.md b/README.md index 4cdd6640..4dc88fa3 100755 --- a/README.md +++ b/README.md @@ -95,6 +95,32 @@ extension. The bin/sbg-parser and bin/sbg-eval binaries accept these files as their input. Also, the `make test` command in the root directory will execute all of the existing tests. +## Partitioner Benchmark + +`test/partitioner/benchmark/run_benchmark.py` runs `sbg-partitioner` over a +fixed set of models, sizes and partition counts, checks that every run +succeeds, and reports the median execution time (build + partition) across a +few repetitions. To run it locally: + +``` +cmake --build build --target sbg-partitioner +python3 test/partitioner/benchmark/run_benchmark.py \ + --binary build/algorithms/partitioner/sbg-partitioner \ + --output bench.json +``` + +The `partitioner-benchmark` GitHub workflow runs this on every push and on +pull requests targeting `sb-graph-dev`. Results are compared against the +latest history stored on the `gh-pages` branch +(`dev/bench/partitioner`, viewable as a chart if GitHub Pages is enabled for +that branch); only pushes to `sb-graph-dev` update that history. If any case +is more than 20% slower than the stored baseline, the workflow fails and +GitHub e-mails whoever pushed the change (per their notification settings). + +One case, `air_conditioners_cont` with size 1000 and 16 partitions, is +excluded: the KL refinement does not converge for it in a reasonable time. +This is tracked as a separate issue, not a benchmark limitation. + ## Licensing Please see the file called LICENSE. diff --git a/algorithms/CMakeLists.txt b/algorithms/CMakeLists.txt index e6f3bbcd..aca6821a 100644 --- a/algorithms/CMakeLists.txt +++ b/algorithms/CMakeLists.txt @@ -2,6 +2,6 @@ add_subdirectory(cc) add_subdirectory(matching) add_subdirectory(mfvs) -#add_subdirectory(partitioner) +add_subdirectory(partitioner) add_subdirectory(scc) add_subdirectory(sorting) diff --git a/algorithms/cc/cc.cpp b/algorithms/cc/cc.cpp index aa018a4c..c4cc7937 100644 --- a/algorithms/cc/cc.cpp +++ b/algorithms/cc/cc.cpp @@ -63,7 +63,7 @@ PWMap connectedComponents(const SBG& sbg) rmap = aux_rmap; rmap = rmap.mapInf(); } - } while (rmap != old_rmap); + } while (rmap != old_rmap); rmap.compact(); return rmap; @@ -72,6 +72,6 @@ PWMap connectedComponents(const SBG& sbg) return PWMap{}; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/matching/bfs_matching.cpp b/algorithms/matching/bfs_matching.cpp index 80d7e8cd..0690641d 100644 --- a/algorithms/matching/bfs_matching.cpp +++ b/algorithms/matching/bfs_matching.cpp @@ -31,22 +31,17 @@ namespace detail { // BFS Matching Algorithm ------------------------------------------------------ //////////////////////////////////////////////////////////////////////////////// -BFSMatching::ExitCondition::ExitCondition(bool full_match, bool found_paths) - : full_match_(full_match), found_paths_(found_paths) {} +BFSMatching::ExitCondition::ExitCondition(bool full_match, bool found_paths) : full_match_(full_match), found_paths_(found_paths) {} bool BFSMatching::ExitCondition::full_match() { return full_match_; } bool BFSMatching::ExitCondition::found_paths() { return found_paths_; } -bool BFSMatching::ExitCondition::isSatisfied() -{ - return full_match_ || !found_paths_; -} +bool BFSMatching::ExitCondition::isSatisfied() { return full_match_ || !found_paths_; } // Algorithm ------------------------------------------------------------------- -BFSMatching::BFSMatching() : _M(), _dsbg() , _direction(Direction::kForward) - , _X(), _Y() {} +BFSMatching::BFSMatching() : _M(), _dsbg(), _direction(Direction::kForward), _X(), _Y() {} void BFSMatching::swapEdgesDirection(const Set& E) { @@ -75,8 +70,7 @@ PWMap BFSMatching::partitionSubsetEdges() const std::size_t arity = free_edges.arity(); unsigned int j = 1; PWMap Emap = _dsbg.Emap(); - _dsbg.foreachSetEdge([&](const IntTuple& SE) - { + _dsbg.foreachSetEdge([&](const IntTuple& SE) { Set domain_edges = Emap.preImage(Set{SE}); Expression matched_expr{IntTuple{arity, j}}; @@ -100,8 +94,7 @@ Set BFSMatching::directedStep(const Set& E) // Calculate unmatched vertices in the side determined by the current // direction of edges Set matched_forward_vertices = mapB.image(_M); - Set unmatched_forward_vertices - = _X.difference(matched_forward_vertices); + Set unmatched_forward_vertices = _X.difference(matched_forward_vertices); // Detect paths leading to unmatched_forward_vertices DirectedSBG restricted_dsbg{_dsbg.V(), _dsbg.Vmap(), mapB, mapD, Emap}; @@ -186,8 +179,8 @@ MatchData BFSMatching::calculate(const BipartiteSBG& bsbg) return result; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/matching/bfs_paths.cpp b/algorithms/matching/bfs_paths.cpp index dab908cc..53bb3b0b 100644 --- a/algorithms/matching/bfs_paths.cpp +++ b/algorithms/matching/bfs_paths.cpp @@ -47,7 +47,7 @@ Set BFSPaths::calculate(const DirectedSBG& dsbg, const Set& endings) // A record of allowed edges to keep out cycle edges Set E = dsbg.E(); // Ingoing edges to vertices that reach endings - Set ingoing = mapD.preImage(endings); + Set ingoing = mapD.preImage(endings); // A record of visited set-edges Set visitedSE; do { @@ -61,11 +61,11 @@ Set BFSPaths::calculate(const DirectedSBG& dsbg, const Set& endings) // Edges that lead to a successor Set Ei = auxD.equalImage(ith_smap.composition(auxB)); // Visited set-edges - Set repeatedSE = visitedSE.intersection(Emap.image(Ei)); + Set repeatedSE = visitedSE.intersection(Emap.image(Ei)); if (!repeatedSE.isEmpty()) { // Propose candidate successors for repetitive paths Set Eplus = Emap.preImage(repeatedSE); - PWMap smap_plus = auxB.restrict(Eplus).minAdj(auxD.restrict(Eplus)); + PWMap smap_plus = auxB.restrict(Eplus).minAdj(auxD.restrict(Eplus)); Util::DEBUG_LOG << "smap_plus: " << smap_plus << "\n"; ith_smap = ith_smap.combine(smap_plus); visitedSE = visitedSE.difference(repeatedSE); @@ -99,8 +99,8 @@ Set BFSPaths::calculate(const DirectedSBG& dsbg, const Set& endings) return P; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/matching/match_data.cpp b/algorithms/matching/match_data.cpp index e4e16d31..e259af2a 100644 --- a/algorithms/matching/match_data.cpp +++ b/algorithms/matching/match_data.cpp @@ -27,27 +27,26 @@ namespace LIB { std::ostream& operator<<(std::ostream& out, const Direction& direction) { switch (direction) { - case Direction::kForward: { - out << "forward"; - break; - } - - case Direction::kBackward: { - out << "backward"; - break; - } - - default: { - Util::ERROR("Unsupported direction"); - break; - } + case Direction::kForward: { + out << "forward"; + break; + } + + case Direction::kBackward: { + out << "backward"; + break; + } + + default: { + Util::ERROR("Unsupported direction"); + break; + } } return out; } -MatchData::MatchData(BipartiteSBG bsbg, Set M, bool full_match) - : _bsbg(bsbg), _M(M), _full_match(full_match) {} +MatchData::MatchData(BipartiteSBG bsbg, Set M, bool full_match) : _bsbg(bsbg), _M(M), _full_match(full_match) {} const BipartiteSBG& MatchData::bsbg() const { return _bsbg; } @@ -67,6 +66,6 @@ std::ostream& operator<<(std::ostream& out, const MatchData& data) return out; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/matching/matching.cpp b/algorithms/matching/matching.cpp index 2b2c09f1..fe493d9f 100644 --- a/algorithms/matching/matching.cpp +++ b/algorithms/matching/matching.cpp @@ -27,23 +27,23 @@ namespace SBG { namespace LIB { //////////////////////////////////////////////////////////////////////////////// -// Matching Algorithm Interface ------------------------------------------------ +// Matching Algorithm Interface ------------------------------------------------ //////////////////////////////////////////////////////////////////////////////// Matching::Matching() : _impl() { MatchKind kind = MATCH_IMPL.kind(); switch (kind) { - case MatchKind::kBFSPaths: { - _impl = detail::BFSMatching{}; - break; - } - - default: { - Util::ERROR("Unsupported matching algorithm implementation"); - break; - } - } + case MatchKind::kBFSPaths: { + _impl = detail::BFSMatching{}; + break; + } + + default: { + Util::ERROR("Unsupported matching algorithm implementation"); + break; + } + } } MatchData Matching::calculate(const BipartiteSBG& bsbg) @@ -53,6 +53,6 @@ MatchData Matching::calculate(const BipartiteSBG& bsbg) return std::visit([&](auto& a) { return a.calculate(bsbg); }, _impl); } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/matching/matching_impl.cpp b/algorithms/matching/matching_impl.cpp old mode 100755 new mode 100644 index 920b2c4f..32c674e5 --- a/algorithms/matching/matching_impl.cpp +++ b/algorithms/matching/matching_impl.cpp @@ -31,15 +31,15 @@ namespace LIB { std::ostream& operator<<(std::ostream& out, const MatchKind kind) { switch (kind) { - case MatchKind::kBFSPaths: { - out << "BFS paths"; - break; - } - - default: { - Util::ERROR("Unsupported matching algorithm implementation"); - break; - } + case MatchKind::kBFSPaths: { + out << "BFS paths"; + break; + } + + default: { + Util::ERROR("Unsupported matching algorithm implementation"); + break; + } } return out; @@ -57,6 +57,6 @@ const MatchKind& MatchImplementation::kind() const { return _kind; } void MatchImplementation::set_match_fact(MatchKind kind) { _kind = kind; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/mfvs/greedy_mfvs.cpp b/algorithms/mfvs/greedy_mfvs.cpp index ffa0a806..0a390610 100644 --- a/algorithms/mfvs/greedy_mfvs.cpp +++ b/algorithms/mfvs/greedy_mfvs.cpp @@ -38,7 +38,7 @@ namespace detail { GreedyMFVS::GreedyMFVS() {} /** - * @brief It calculates the minimum vertex with maximum degree. + * @brief It calculates the minimum vertex with maximum degree. */ IntTuple maxDegreeVertex(const DirectedSBG& dsbg) { @@ -104,8 +104,8 @@ Set GreedyMFVS::calculate(const DirectedSBG& input_dsbg) const return fvs_result; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/mfvs/mfvs_impl.cpp b/algorithms/mfvs/mfvs_impl.cpp old mode 100755 new mode 100644 index 0976265b..9a60a05d --- a/algorithms/mfvs/mfvs_impl.cpp +++ b/algorithms/mfvs/mfvs_impl.cpp @@ -25,31 +25,31 @@ namespace SBG { namespace LIB { //////////////////////////////////////////////////////////////////////////////// -// MFVS implementations -------------------------------------------------------- +// MFVS implementations -------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// std::ostream& operator<<(std::ostream& out, const MFVSKind kind) { switch (kind) { - case MFVSKind::kGreedy: { - out << "maximum degree greedy"; - break; - } - - case MFVSKind::kSmallSV: { - out << "smallest set-vertex"; - break; - } - - case MFVSKind::kParallelSCC: { - out << "maximum degree from smallest set-vertex of each SCC"; - break; - } - - default: { - Util::ERROR("MFVSKind::operator<<: unsupported MFVS implementation"); - break; - } + case MFVSKind::kGreedy: { + out << "maximum degree greedy"; + break; + } + + case MFVSKind::kSmallSV: { + out << "smallest set-vertex"; + break; + } + + case MFVSKind::kParallelSCC: { + out << "maximum degree from smallest set-vertex of each SCC"; + break; + } + + default: { + Util::ERROR("MFVSKind::operator<<: unsupported MFVS implementation"); + break; + } } return out; @@ -67,6 +67,6 @@ const MFVSKind& MFVSImplementation::kind() const { return _kind; } void MFVSImplementation::set_mfvs_fact(MFVSKind kind) { _kind = kind; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/mfvs/min_feedback_vertex_set.cpp b/algorithms/mfvs/min_feedback_vertex_set.cpp index 77e0e4ae..13caaaac 100644 --- a/algorithms/mfvs/min_feedback_vertex_set.cpp +++ b/algorithms/mfvs/min_feedback_vertex_set.cpp @@ -34,25 +34,25 @@ MinFeedbackVertexSet::MinFeedbackVertexSet() : _impl() { MFVSKind kind = MFVS_IMPL.kind(); switch (kind) { - case MFVSKind::kGreedy: { - _impl = detail::GreedyMFVS{}; - break; - } - - case MFVSKind::kSmallSV: { - _impl = detail::SmallestSVMFVS{}; - break; - } - - case MFVSKind::kParallelSCC: { - _impl = detail::ParallelSCCMFVS{}; - break; - } - - default: { - Util::ERROR("MinFeedbackVertexSet: unsupported MFVS implementation"); - break; - } + case MFVSKind::kGreedy: { + _impl = detail::GreedyMFVS{}; + break; + } + + case MFVSKind::kSmallSV: { + _impl = detail::SmallestSVMFVS{}; + break; + } + + case MFVSKind::kParallelSCC: { + _impl = detail::ParallelSCCMFVS{}; + break; + } + + default: { + Util::ERROR("MinFeedbackVertexSet: unsupported MFVS implementation"); + break; + } } } @@ -63,6 +63,6 @@ Set MinFeedbackVertexSet::calculate(const DirectedSBG& dsbg) return std::visit([&](auto& a) { return a.calculate(dsbg); }, _impl); } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/mfvs/parallel_scc.cpp b/algorithms/mfvs/parallel_scc.cpp index 38962739..4bdab071 100644 --- a/algorithms/mfvs/parallel_scc.cpp +++ b/algorithms/mfvs/parallel_scc.cpp @@ -118,7 +118,7 @@ Set ParallelSCCMFVS::calculate(const DirectedSBG& input_dsbg) Set Vj_SV = Vmap.image(Vj); Set repeatedSV = visitedSV.intersection(Vj_SV); if (!repeatedSV.isEmpty()) { - fvs_result = fvs_result.cup(Vmap.preImage(Vj_SV)); + fvs_result = fvs_result.cup(Vmap.preImage(Vj_SV)); } else { visitedSV = visitedSV.cup(Vj_SV); } @@ -137,8 +137,8 @@ Set ParallelSCCMFVS::calculate(const DirectedSBG& input_dsbg) return fvs_result; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/mfvs/smallest_sv_mfvs.cpp b/algorithms/mfvs/smallest_sv_mfvs.cpp index b83c3ac2..e3d90fc0 100644 --- a/algorithms/mfvs/smallest_sv_mfvs.cpp +++ b/algorithms/mfvs/smallest_sv_mfvs.cpp @@ -49,7 +49,7 @@ Set getVerticesFromSmallestSV(const PWMap& Vmap) Set remaining = Vmap_image; while (!remaining.isEmpty()) { Set jth_sv{remaining.minElem()}; - std::size_t jth_sz = Vmap.preImage(jth_sv).cardinal(); + std::size_t jth_sz = Vmap.preImage(jth_sv).cardinal(); if (jth_sz < min_sz) { min_sz = jth_sz; } @@ -73,7 +73,7 @@ Set getVerticesFromSmallestSV(const PWMap& Vmap) } /** - * @brief It calculates the minimum vertex of maximum degree of V. + * @brief It calculates the minimum vertex of maximum degree of V. */ IntTuple getMaxDegreeVertex(const Set& V, const DirectedSBG& dsbg) { @@ -141,8 +141,8 @@ Set SmallestSVMFVS::calculate(const DirectedSBG& input_dsbg) const return fvs_result; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/partitioner/CMakeLists.txt b/algorithms/partitioner/CMakeLists.txt index 844383c5..e0690340 100644 --- a/algorithms/partitioner/CMakeLists.txt +++ b/algorithms/partitioner/CMakeLists.txt @@ -9,6 +9,11 @@ target_include_directories(sbg-part-lib ${PROJECT_SOURCE_DIR} ) +set(ENABLE_MEMORY_BENCHMARKING OFF) +if(ENABLE_MEMORY_BENCHMARKING) + add_compile_definitions(USE_MEMORY_TRACKER) +endif() + target_sources( sbg-part-lib PRIVATE @@ -17,7 +22,6 @@ target_sources( dfs_on_sbg.cpp kernighan_lin_partitioner.cpp partition_graph.cpp - partition_graph_cc.cpp partition_metrics_api.cpp partition_strategy.cpp weighted_sb_graph.cpp @@ -51,6 +55,8 @@ target_sources( main.cpp ) +target_include_directories(sbg-partitioner PRIVATE ${PROJECT_SOURCE_DIR}) + target_link_libraries( sbg-partitioner PRIVATE @@ -78,3 +84,11 @@ install( WORLD_EXECUTE COMPONENT sbg-partitioner ) + +install( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sbgraph/algorithms/partitioner + FILES_MATCHING PATTERN "*.hpp" + PATTERN "partition_graph_cc.hpp" EXCLUDE + PATTERN "utils" EXCLUDE +) diff --git a/algorithms/partitioner/README.md b/algorithms/partitioner/README.md index 7c650b85..62de94ed 100644 --- a/algorithms/partitioner/README.md +++ b/algorithms/partitioner/README.md @@ -50,42 +50,7 @@ For example: To run an example and get the resultant partition you can run: -`./bin/sbg-partitioner -f examples/air_conditioners.json -p 4 -g output.json` - -The output will be written in the file `output.json` in JSON format: - -``` -{ - "partitions": [ - {"nodes": [ - [[0,24]], - [[100,124]], - [[200,224]], - [[300,324]] - ]}, - {"nodes": [ - [[25,49]], - [[125,149]], - [[225,249]], - [[325,349]] - ]}, - {"nodes": [ - [[50,74]], - [[150,174]], - [[250,274]], - [[350,374]] - ]}, - {"nodes": [ - [[75,99]], - [[175,199]], - [[275,299]], - [[375,399]] - ]} - ] -} -``` - -where each `node` object is a set of intervals, that represents a set of nodes for each partition. +`./bin/sbg-partitioner -f examples/air_conditioners.json -p 4` ## Test suites @@ -118,3 +83,64 @@ in the branch `sb-graph-dev`. * `-e` [optional argument] imbalance epsilon, a value between 0 and 1. Output files with the metrics will be output in the directory passed as an argument. + + +## External tools + +# Graph Partitioner (`grap_partitioner`) + +A command-line tool designed to partition graphs using various partitioning algorithms and balance constraints. It provides execution and computation time metrics to help benchmark and analyze partitioning performance across different algorithms. + +--- + +## Usage + +```bash +external_tools/install/bin/graph_partitioner -f -n -m -i + +``` + +--- + +## Command-Line Options + +| Flag | Argument | Description | Required | +| --- | --- | --- | --- | +| `-f` | `` | Path to the SBG input JSON file or generated graph input. | Yes | +| `-n` | `` | Target number of partitions (integer). | Yes | +| `-m` | `` | Partitioning algorithm to execute. | Yes | +| `-i` | `` | Allowed partition imbalance ratio/threshold. | Yes | +| `-h` | — | Display usage information and exit. | No | + +--- + +## Supported Partition Methods (`-m`) + +* **`SBG`** — Streaming/Balanced Graph partitioner +* **`Scotch`** — Dual/Static graph partitioner +* **`Metis`** — Family of serial partitioning algorithms + +--- + +## Examples + +Partition a graph file into 4 parts using Metis with an imbalance factor of 0.03: + +```bash +./graph_partitioner -f input_graph.json -n 4 -m Metis -i 0.03 + +``` + +Run partitioning using KaHIP on a generated graph: + +```bash +./graph_partitioner -f data/network.sbg.json -n 8 -m SBG -i 0.05 + +``` + +Display help: + +```bash +./graph_partitioner -h + +``` diff --git a/algorithms/partitioner/build_sb_graph.cpp b/algorithms/partitioner/build_sb_graph.cpp index f6d3a531..e38dd602 100644 --- a/algorithms/partitioner/build_sb_graph.cpp +++ b/algorithms/partitioner/build_sb_graph.cpp @@ -26,9 +26,11 @@ #include #include #include +#include #include -#include -#include +#include +#include +#include #include "build_sb_graph.hpp" #include "partition_graph.hpp" @@ -41,6 +43,13 @@ using namespace rapidjson; using namespace std; using namespace SBG::LIB; +using SBG::LIB::detail::ExpressionImpl; +using SBG::LIB::detail::Interval; +using SBG::LIB::detail::LinearExpr; +using SBG::LIB::detail::MultiDimInter; +using SBG::LIB::detail::OrderedSet; +using SBG::LIB::detail::SetAccess; +using SBG::LIB::detail::SetAccessKey; using namespace SBG::Util; namespace sbg_partitioner { @@ -48,9 +57,24 @@ namespace sbg_partitioner { // Using an unnamed namespace to define functions with internal linkage namespace { +/// Builds an expression from its linear expressions, one per dimension. +Expression to_expression(const ExpressionImpl& exps) +{ + if (exps.empty()) { + return Expression(); + } + + Expression expression(exps.front().slope(), exps.front().offset()); + for (size_t i = 1; i < exps.size(); i++) { + expression = expression.cartesianProduct(Expression(exps[i].slope(), exps[i].offset())); + } + + return expression; +} + struct Var { string id; - vector> exps; + vector> exps; list defs; unsigned cost = 1; }; @@ -124,7 +148,7 @@ vector read_var_object(const rapidjson::Value& var_array) assert(value.HasMember("exp") and value["exp"].IsArray()); auto expressions = value["exp"].GetArray(); - vector> exps; + vector> exps; // assert(expressions.Size() == 1 and "this is just for testing air conditioner example!!"); for (const auto& exp_array : expressions) { assert(exp_array.IsArray()); @@ -208,9 +232,10 @@ map create_node_objects_from_json(const Document& document) /// Creates a set of nodes, taking into accout the offset of each one to avoid collisions. tuple create_set_of_nodes(const map& nodes, map& node_offsets, int& max_value) { + SetAccessKey key = SetAccess::key(); // We start to build out set of intervals from 0 int current_max = 0; - Set node_set = SET_FACT.createSet(); + Set node_set; NodeWeight weights; for (const auto& [id, node] : nodes) { @@ -219,7 +244,7 @@ tuple create_set_of_nodes(const map& nodes, map create_set_of_nodes(const map& nodes, map create_set_of_nodes(const map& nodes, map> read_left_vars(const Node& node, const string& var_id = "") +vector> read_left_vars(const Node& node, const string& var_id = "") { const auto& l_nodes = node.lhs; assert(l_nodes.size() > 0); - vector> exps; + vector> exps; for (const auto& l_node : l_nodes) { if (not var_id.empty() and l_node.id != var_id) { continue; } Var var = l_node; - Exp exp; + ExpressionImpl exp; for (const auto& values : var.exps) { - exp.emplaceBack(LExp(RATIONAL(values.first, 1), RATIONAL(values.second, 1))); + exp.push_back(LinearExpr(Rational(values.first, 1), Rational(values.second, 1))); } exps.push_back({var, exp}); @@ -282,30 +307,30 @@ vector> read_left_vars(const Node& node, const string& var_id = " /// @param pre_image Subset of the domain of the expression we want to connect /// @param edge_domain Domain of the map /// @param var_exp Original expression of the variable -Map create_set_edge_map(const Set& pre_image, const Set& edge_domain, const Exp& var_exps, int set_offset) +Map create_set_edge_map(const Set& pre_image, const Set& edge_domain, const ExpressionImpl& var_exps, int set_offset) { SBG::LIB::Map map; - Exp map_exps; + ExpressionImpl map_exps; int i = 0; - for (const auto& var_exp : var_exps.exps()) { + for (const auto& var_exp : var_exps) { // If the slope is 0, we just return the expression. if (var_exp.slope() == 0) { logging::sbg_log << "Creating constant interval" << endl; - INT offset = var_exp.offset().numerator(); + Int offset = var_exp.offset().numerator(); if (i == 0) { offset += set_offset; } - LExp map_exp(var_exp.slope(), offset); - map_exps.emplaceBack(map_exp); + LinearExpr map_exp(var_exp.slope(), offset); + map_exps.push_back(map_exp); i++; continue; } // We start from the original offset and substract the domain offset - INT offset = var_exp.offset().numerator(); + Int offset = var_exp.offset().numerator(); if (i == 0) { offset += set_offset; } @@ -313,19 +338,19 @@ Map create_set_edge_map(const Set& pre_image, const Set& edge_domain, const Exp& offset = offset - edge_domain.minElem()[i]; // Then we take the minimum element of the pre-image and add the offset - INT min_elem = pre_image.minElem()[i]; + Int min_elem = pre_image.minElem()[i]; offset += min_elem; // We set the offset - LExp map_exp = LExp(var_exp.slope(), RATIONAL(offset, 1)); + LinearExpr map_exp = LinearExpr(var_exp.slope(), Rational(offset, 1)); - map_exps.emplaceBack(map_exp); + map_exps.push_back(map_exp); i++; } - logging::sbg_log << "created " << i << " maps out of " << var_exps << endl; + logging::sbg_log << "created " << i << " maps out of " << to_expression(var_exps) << endl; - map = Map(edge_domain, map_exps); + map = Map(edge_domain, to_expression(map_exps)); return map; } @@ -333,26 +358,28 @@ Map create_set_edge_map(const Set& pre_image, const Set& edge_domain, const Exp& template Set get_node_domain(Node node) { + SetAccessKey key = SetAccess::key(); // Domain of the node candidate - Set node_intervals = SET_FACT.createSet(); - SetPiece node_set_piece; + Set node_intervals; + MultiDimInter node_set_piece; for (const auto& node_interval : node.intervals) { Interval interval(node_interval.first, 1, node_interval.second); node_set_piece.emplaceBack(interval); } - node_intervals.emplace(node_set_piece); + node_intervals = node_intervals.cup(key.createSet(OrderedSet(node_set_piece))); return node_intervals; } Set get_edge_domain(Set image_intersection_set, Set& edge_set, int& max_value) { + SetAccessKey key = SetAccess::key(); logging::sbg_log << "get_edge_domain " << image_intersection_set << ", " << edge_set << endl; - Set edge_domain_set = SET_FACT.createSet(); + Set edge_domain_set; // we know it only has one dimension, so we take the first one - auto image_intersection_first_set_piece = *image_intersection_set.begin(); + auto image_intersection_first_set_piece = key.pieces(get(key.impl(image_intersection_set))).front(); auto image_intersection_first_interval = image_intersection_first_set_piece[0]; auto domain_offset = image_intersection_first_interval.end() - image_intersection_first_interval.begin(); @@ -360,46 +387,48 @@ Set get_edge_domain(Set image_intersection_set, Set& edge_set, int& max_value) edge_domain = Interval(max_value, 1, max_value + domain_offset); max_value = edge_domain.end() + 1; - edge_domain_set.emplaceBack(edge_domain); + edge_domain_set = edge_domain_set.cup(key.createSet(OrderedSet(edge_domain))); return edge_domain_set; } -tuple create_graph_edges(const std::map& nodes, const map& node_offsets, int& max_value, bool compact_maps) +tuple create_graph_edges(const std::map& nodes, const map& node_offsets, int& max_value, + bool compact_maps) { - Set edge_set = SET_FACT.createSet(); // Our set of edges - PWMap rhs_maps = PW_FACT.createPWMap(); // Map object of one of the sides - PWMap lhs_maps = PW_FACT.createPWMap(); // Map object of one of the other side - EdgeCost costs; // Weight of edges + SetAccessKey key = SetAccess::key(); + Set edge_set; // Our set of edges + PWMap rhs_maps = PWMap(); // Map object of one of the sides + PWMap lhs_maps = PWMap(); // Map object of one of the other side + EdgeCost costs; // Weight of edges for (const auto& [id, node] : nodes) { logging::sbg_log << "Looking for connections with " << id << endl; // Define the equation intervals (without offsets) - Set current_node_domain = SET_FACT.createSet(); - SetPiece interval_set_piece; + Set current_node_domain; + MultiDimInter interval_set_piece; // assert(node.intervals.size() == 1); for (const auto& node_interval : node.intervals) { Interval interval(node_interval.first, 1, node_interval.second); interval_set_piece.emplaceBack(interval); } - current_node_domain.emplace(interval_set_piece); + current_node_domain = current_node_domain.cup(key.createSet(OrderedSet(interval_set_piece))); - vector> this_node_exps = read_left_vars(node); + vector> this_node_exps = read_left_vars(node); // Now, iterate the right hand side expresions to connect them to their definitions. for (const Var& right_var : node.rhs) { - Exp right_exps; + ExpressionImpl right_exps; for (const auto& exp_values : right_var.exps) { - LExp right_exp = LExp(RATIONAL(exp_values.first, 1), RATIONAL(exp_values.second, 1)); - right_exps.emplaceBack(right_exp); + LinearExpr right_exp = LinearExpr(Rational(exp_values.first, 1), Rational(exp_values.second, 1)); + right_exps.push_back(right_exp); } // Now, calculate the image of the rhs expression - auto rhs_map = Map(current_node_domain, right_exps); + auto rhs_map = Map(current_node_domain, to_expression(right_exps)); // This is the image *used* by this expression, wwe want to // check where is defined. - auto used_node_image = Map(current_node_domain, right_exps).image(); + auto used_node_image = Map(current_node_domain, to_expression(right_exps)).image(); // Definitions of this variable are on defs field. We want to check if // intersects with any node @@ -418,7 +447,7 @@ tuple create_graph_edges(const std::map& } // Now, get the image. - auto node_candidate_CanonMap = Map(node_candidate_domain, node_candidate_exps); + auto node_candidate_CanonMap = Map(node_candidate_domain, to_expression(node_candidate_exps)); auto node_candidate_image = node_candidate_CanonMap.image(); // we want to see if the intersection of the images is not empty @@ -437,57 +466,57 @@ tuple create_graph_edges(const std::map& #if CHECK_1_N_REL // we have to use the first map of candidate node - if (node_candidate_exps.exps()[0].slope() == 0) { + if (node_candidate_exps[0].slope() == 0) { logging::sbg_log << "This should be 1-N " << node_candidate_domain << endl; auto node_size = get_set_size(node_candidate_domain); Set edge_domain_set = get_edge_domain(image_intersection_set, edge_set, max_value); - SetPiece edge_domain_set_first_interval = *edge_domain_set.begin(); + MultiDimInter edge_domain_set_first_interval = *edge_domain_set.begin(); - SetPiece first_interval = *node_candidate_domain.begin(); + MultiDimInter first_interval = *node_candidate_domain.begin(); int offset = first_interval[0].begin() + node_offsets.at(i) - edge_domain_set_first_interval[0].begin(); logging::sbg_log << "node offset " << node_offsets.at(i) << ", " << edge_domain_set << " so offset is " << offset << endl; - Map to_node_candidate = Map(edge_domain_set, LExp(1, RATIONAL(offset, 1))); + Map to_node_candidate = Map(edge_domain_set, Expression(Rational(1), Rational(offset, 1))); logging::sbg_log << "to_node_candidate " << to_node_candidate << endl; logging::sbg_log << to_node_candidate << endl; - auto im = Interval(node_candidate_exps.exps()[0].offset().numerator(), 1, node_candidate_exps.exps()[0].offset().numerator()); - auto im_set = SET_FACT.createSet(im); + auto im = Interval(node_candidate_exps[0].offset().numerator(), 1, node_candidate_exps[0].offset().numerator()); + auto im_set = key.createSet(OrderedSet(im)); Map to_current_node = create_set_edge_map(im_set, edge_domain_set, - Exp(LExp(0, node_candidate_exps.exps()[0].offset())), node_offsets.at(id)); + ExpressionImpl{LinearExpr(0, node_candidate_exps[0].offset())}, node_offsets.at(id)); logging::sbg_log << "to_current_node " << to_current_node << endl; - lhs_maps.emplaceBack(to_current_node); - rhs_maps.emplaceBack(to_node_candidate); + lhs_maps.insert(to_current_node); + rhs_maps.insert(to_node_candidate); continue; - } else if (exp.exps()[0].slope() == 0) { + } else if (exp[0].slope() == 0) { logging::sbg_log << "This should be N-1" << endl; auto first_interval_current_node_domain = *current_node_domain.begin(); auto node_size = first_interval_current_node_domain[0].end() - first_interval_current_node_domain[0].begin(); - auto first_interval_image_intersection_set = *image_intersection_set.begin(); + auto first_interval_image_intersection_set = key.pieces(get(key.impl(image_intersection_set))).front(); auto image_intersection_set_interval = Interval(first_interval_image_intersection_set[0].begin(), 1, first_interval_image_intersection_set[0].begin() + node_size); - image_intersection_set = SET_FACT.createSet(image_intersection_set_interval); + image_intersection_set = key.createSet(OrderedSet(image_intersection_set_interval)); Set edge_domain_set = get_edge_domain(image_intersection_set, edge_set, max_value); auto edge_domain_set_first_interval = *edge_domain_set.begin(); int offset = first_interval_current_node_domain[0].begin() + node_offsets.at(id) - edge_domain_set_first_interval[0].begin(); - Map to_current_node = Map(edge_domain_set, LExp(1, RATIONAL(offset, 1))); + Map to_current_node = Map(edge_domain_set, Expression(Rational(1), Rational(offset, 1))); logging::sbg_log << "to_current_node " << to_current_node << endl; - auto im = Interval(exp.exps()[0].offset().numerator(), 1, exp.exps()[0].offset().numerator()); - auto im_set = SET_FACT.createSet(im); + auto im = Interval(exp[0].offset().numerator(), 1, exp[0].offset().numerator()); + auto im_set = key.createSet(OrderedSet(im)); Map to_node_candidate = create_set_edge_map(im_set, edge_domain_set, - Exp(LExp(0, node_candidate_exps.exps()[0].offset())), node_offsets.at(i)); + ExpressionImpl{LinearExpr(0, node_candidate_exps[0].offset())}, node_offsets.at(i)); logging::sbg_log << "to_node_candidate " << to_node_candidate << endl; - lhs_maps.emplaceBack(to_current_node); - rhs_maps.emplaceBack(to_node_candidate); + lhs_maps.insert(to_current_node); + rhs_maps.insert(to_node_candidate); continue; } @@ -498,12 +527,11 @@ tuple create_graph_edges(const std::map& Set edge_domain_set = get_edge_domain(image_intersection_set, edge_set_copy, max_value_copy); // Create map to node candidate - auto first_lhs_node_candidate = - Exp(LExp(RATIONAL(node_candidate.lhs[0].exps[0].first, 1), RATIONAL(node_candidate.lhs[0].exps[0].second, 1))); - auto pre_ima_candidate = node_candidate_CanonMap.dom(); + auto first_lhs_node_candidate = ExpressionImpl{ + LinearExpr(Rational(node_candidate.lhs[0].exps[0].first, 1), Rational(node_candidate.lhs[0].exps[0].second, 1))}; + auto pre_ima_candidate = node_candidate_CanonMap.domain(); logging::sbg_log << "pre_ima_candidate " << pre_ima_candidate << " from " << node_candidate_CanonMap << endl; - auto node_candidate_map = - create_set_edge_map(pre_ima_candidate, edge_domain_set, first_lhs_node_candidate, node_offsets.at(i)); + auto node_candidate_map = create_set_edge_map(pre_ima_candidate, edge_domain_set, first_lhs_node_candidate, node_offsets.at(i)); auto node_candidate_map_image = node_candidate_map.image(); logging::sbg_log << "map is " << node_candidate_map << endl; logging::sbg_log << "image: " << node_candidate_map_image << endl; @@ -511,16 +539,16 @@ tuple create_graph_edges(const std::map& // Create map to current node auto pre_image_current_node = rhs_map.preImage(image_intersection_set); - auto im_map = Map(pre_image_current_node, exp); - auto im = im_map.dom(); + auto im_map = Map(pre_image_current_node, to_expression(exp)); + auto im = im_map.domain(); auto current_node_map = create_set_edge_map(im, edge_domain_set, exp, node_offsets.at(id)); auto current_node_map_image = current_node_map.image(); logging::sbg_log << "map is " << current_node_map << endl; logging::sbg_log << "image: " << current_node_map_image << endl; if (not(current_node_map_image == node_candidate_map_image)) { - lhs_maps.emplaceBack(current_node_map); - rhs_maps.emplaceBack(node_candidate_map); + lhs_maps.insert(current_node_map); + rhs_maps.insert(node_candidate_map); edge_set = edge_set_copy; max_value = max_value_copy; } else { @@ -535,8 +563,8 @@ tuple create_graph_edges(const std::map& } if (compact_maps) { - rhs_maps = rhs_maps.compact(); - lhs_maps = lhs_maps.compact(); + rhs_maps.compact(); + lhs_maps.compact(); } return {edge_set, rhs_maps, lhs_maps, costs}; @@ -558,41 +586,96 @@ SBG::LIB::WeightedSBGraph create_sb_graph(const std::map& nodes, bool auto [edge_set, left_maps, right_maps, costs] = create_graph_edges(nodes, node_offsets, max_value, compact_maps); // Now, let's create a graph - SBG::LIB::WeightedSBGraph graph(node_set, PW_FACT.createPWMap(), left_maps, right_maps, PW_FACT.createPWMap(), - PW_FACT.createPWMap()); // This will be our graph + SBG::LIB::WeightedSBGraph graph(node_set, PWMap(), left_maps, right_maps, PWMap()); // This will be our graph return graph; } -unsigned add_adjacent_nodes(const Map& incoming_map, const Map& arrival_map, const Set& node, Set& adjacents) +size_t add_adjacent_nodes(const Map& incoming_map, const Map& arrival_map, const Set& node, Set& adjacents) { - auto map_image = incoming_map.dom(); + auto map_image = incoming_map.domain(); auto node_map_intersection = map_image.intersection(node); - unsigned qty = 0; + size_t qty = 0; if (not node_map_intersection.isEmpty()) { auto pre_image = incoming_map.preImage(node_map_intersection); auto adjs = arrival_map.image(pre_image); qty += get_node_size(adjs, NodeWeight()); - for_each(adjs.begin(), adjs.end(), [&adjacents](const auto& b) { adjacents.emplace(b); }); + adjacents = adjacents.cup(adjs); } return qty; } +/// @brief Splits the dimensions [dim, arity) of a set piece into its first n elements (in lexicographic +/// order) and the remaining ones. It requires 0 < n < cardinal of those dimensions. +/// @return a pair with the pieces of the taken elements and the pieces of the remaining ones. +pair, vector> split_first_elements(const MultiDimInter& piece, size_t dim, size_t n) +{ + const Interval& head = piece[dim]; + const Int step = head.step(); + + size_t inner = 1; + for (size_t d = dim + 1; d < piece.arity(); d++) { + inner *= piece[d].cardinal(); + } + + // Builds [begin:step:end] x sub, or [begin:step:end] x piece[dim + 1 ..] when there is no sub. + auto make_piece = [&](Int begin, Int end, const MultiDimInter* sub) { + MultiDimInter result(Interval(begin, step, end)); + if (sub) { + for (const Interval& i : *sub) { + result.emplaceBack(i); + } + } else { + for (size_t d = dim + 1; d < piece.arity(); d++) { + result.emplaceBack(piece[d]); + } + } + return result; + }; + + const size_t full = n / inner; + const size_t rest = n % inner; + const Int row = head.begin() + Int(full) * step; + + vector taken, remaining; + if (full > 0) { + taken.push_back(make_piece(head.begin(), row - step, nullptr)); + } + + Int next_row = row; + if (rest > 0) { + auto [sub_taken, sub_remaining] = split_first_elements(piece, dim + 1, rest); + for (const auto& sub : sub_taken) { + taken.push_back(make_piece(row, row, &sub)); + } + for (const auto& sub : sub_remaining) { + remaining.push_back(make_piece(row, row, &sub)); + } + next_row += step; + } + + if (next_row <= head.end()) { + remaining.push_back(make_piece(next_row, head.end(), nullptr)); + } + + return make_pair(taken, remaining); +} + } // namespace -pair cut_interval(const SetPiece& interval, int cut_value) +pair cut_interval(const MultiDimInter& interval, int cut_value) { - int interval_begin = interval.intervals().front().begin(); - int interval_end = interval.intervals().front().end(); + int interval_begin = interval[0].begin(); + int interval_end = interval[0].end(); Interval interval_1(interval_begin, 1, cut_value); Interval interval_2(cut_value + 1, 1, interval_end); - SetPiece set_1; + MultiDimInter set_1; set_1.emplaceBack(interval_1); - SetPiece set_2; + MultiDimInter set_2; set_2.emplaceBack(interval_2); return make_pair(set_1, set_2); @@ -600,9 +683,10 @@ pair cut_interval(const SetPiece& interval, int cut_value) size_t get_set_size(const Set& set) { + SetAccessKey key = SetAccess::key(); size_t size = 0; - for (auto s : set) { + for (auto s : key.pieces(get(key.impl(set)))) { for (size_t i = 0; i < s.arity(); i++) { auto sp = s[i]; size += sp.end() - sp.begin() + 1; @@ -612,6 +696,8 @@ size_t get_set_size(const Set& set) return size; } +Set to_set(const MultiDimInter& piece) { return SetAccess::key().createSet(OrderedSet(piece)); } + SBG::LIB::WeightedSBGraph build_sb_graph(const string& filename, bool compact_maps) { logging::sbg_log << "Reading " << filename << "..." << endl; @@ -635,19 +721,21 @@ SBG::LIB::WeightedSBGraph build_sb_graph(const string& filename, bool compact_ma Set get_adjacents(const SBG::LIB::SBG& graph, const Set& node) { - Set adjacents = SET_FACT.createSet(); + Set adjacents; // Fill adjacents unsigned acc = 0; - for (auto it1 = graph.map1().begin(), it2 = graph.map2().begin(); it1 != graph.map1().end() and it2 != graph.map2().end(); ++it1, ++it2) { - const auto map1 = *it1; - const auto map2 = *it2; + PWMap pw_map1 = graph.map1(); + PWMap pw_map2 = graph.map2(); + for (auto it1 = pw_map1.begin(), it2 = pw_map2.begin(); it1 != pw_map1.end() and it2 != pw_map2.end(); ++it1, ++it2) { + const Map& map1 = *it1; + const Map& map2 = *it2; acc += add_adjacent_nodes(map1, map2, node, adjacents); - auto map2_minus_map1_dom = map2.dom().difference(map1.dom()); + auto map2_minus_map1_dom = map2.domain().difference(map1.domain()); if (not map2_minus_map1_dom.isEmpty()) { - Map map2_ = Map(map2_minus_map1_dom, map2.exp()); + Map map2_ = Map(map2_minus_map1_dom, map2.law()); acc += add_adjacent_nodes(map2_, map1, node, adjacents); } @@ -656,101 +744,82 @@ Set get_adjacents(const SBG::LIB::SBG& graph, const Set& node) return adjacents; } -pair cut_bidimensional_interval(const SetPiece& set_piece, size_t s) +pair cut_interval_by_dimension(const Set& set_piece, const NodeWeight& node_weight, std::size_t size) { - logging::sbg_log << "cutting interval " << set_piece << ", " << s << endl; - - auto size_node_2 = get_node_size(SetPiece(set_piece.intervals()[1]), NodeWeight()); - - unsigned ammount_of_rows = s / size_node_2; - - unsigned rest = s % size_node_2; - - logging::sbg_log << "Ammount of rows " << ammount_of_rows << endl; - - Set OrdSet_ret = SET_FACT.createSet(); - SetPiece interval_2 = *set_piece.intervals().begin(); - - if (ammount_of_rows > 0) { - SetPiece interval_1; - tie(interval_1, interval_2) = cut_interval(set_piece.intervals().front(), set_piece.intervals().front().begin() + ammount_of_rows - 1); - logging::sbg_log << "Interval cut in " << ammount_of_rows << ": " << interval_1 << ", " << interval_2 << endl; - if (interval_2.arity() == 0 and rest > 0) { - interval_2 = Interval(interval_1.intervals().front().end(), 1, interval_1.intervals().front().end()); - } - interval_1.emplaceBack(set_piece.intervals()[1]); - OrdSet_ret.emplace(interval_1); + SetAccessKey key = SetAccess::key(); + if (set_piece.isEmpty()) { + return make_pair(Set(), Set()); } - if (rest > 0) { - SetPiece rest_set_piece; - rest_set_piece.emplaceBack(Interval(interval_2.intervals().front().begin(), 1, interval_2.intervals().front().begin())); - SetPiece interval_3, interval_4; - tie(interval_3, interval_4) = cut_interval(set_piece.intervals()[1], set_piece.intervals()[1].begin() + rest - 1); - rest_set_piece.emplaceBack(interval_3.intervals().front()); - - OrdSet_ret.emplace(rest_set_piece); + if (size == 0) { + return make_pair(Set(), set_piece); } - Set set = SET_FACT.createSet(set_piece); - Set remaining = set.difference(OrdSet_ret); + // OrderedSet pieces are already sorted by minElem(), so we can walk them in lexicographic order. + OrderedSet::OrdMDICollection taken, remaining; + for (const MultiDimInter& piece : key.pieces(get(key.impl(set_piece)))) { + if (size == 0) { + remaining.push_back(piece); + continue; + } - logging::sbg_log << "original " << set_piece << ", " << OrdSet_ret << ", " << remaining << endl; + const size_t cost = unsigned(get_set_cost(piece, node_weight)); + const size_t available = piece.cardinal(); + const size_t n = min(available, size / cost); - return make_pair(OrdSet_ret, remaining); -} + if (n == 0) { + remaining.push_back(piece); + continue; + } -pair cut_interval_by_dimension(Set& set_piece, const NodeWeight& node_weight, std::size_t size) -{ - if (set_piece.isEmpty()) { - return make_pair(SET_FACT.createSet(), SET_FACT.createSet()); - } + if (n == available) { + taken.push_back(piece); + } else { + auto [sub_taken, sub_remaining] = split_first_elements(piece, 0, n); + taken.insert(taken.end(), sub_taken.begin(), sub_taken.end()); + remaining.insert(remaining.end(), sub_remaining.begin(), sub_remaining.end()); + } - if (size == 0) { - return make_pair(SET_FACT.createSet(), set_piece); + size -= n * cost; } - size_t actual_size = size / unsigned(get_set_cost(*set_piece.begin(), node_weight)); + // OrderedSet::cup expects its pieces sorted by minElem(). Pieces are disjoint by construction, + // but with multi-piece inputs a partial-row remainder can sort after a later untouched piece. + auto by_min_elem = [](const MultiDimInter& a, const MultiDimInter& b) { return a.minElem() < b.minElem(); }; + std::sort(taken.begin(), taken.end(), by_min_elem); + std::sort(remaining.begin(), remaining.end(), by_min_elem); - SetPiece p_1, p_2; - auto i1 = *set_piece.begin(); - tie(p_1, p_2) = cut_interval(i1.intervals().front(), i1.intervals().front().begin() + actual_size - 1); - return make_pair(SET_FACT.createSet(p_1), SET_FACT.createSet(p_2)); + return make_pair(key.createSet(OrderedSet(std::move(taken))), key.createSet(OrderedSet(std::move(remaining)))); } -unsigned get_node_size(const SetPiece& node, const NodeWeight& node_weight) +size_t get_node_size(const MultiDimInter& node, const NodeWeight& node_weight) { int weight = get_set_cost(node, node_weight); - unsigned acc = node.intervals().front().end() - node.intervals().front().begin() + 1; - - for (size_t i = 1; i < node.intervals().size(); i++) { - auto interval = node.intervals()[i]; - acc = acc * (interval.end() - interval.begin() + 1); - } - + size_t acc = node.cardinal(); acc *= weight; return acc; } -unsigned get_node_size(const Set& node, const NodeWeight& node_weight) +size_t get_node_size(const Set& node, const NodeWeight& node_weight) { + SetAccessKey key = SetAccess::key(); if (node.isEmpty()) { return 0; } - unsigned size = 0; - for (const auto& set_piece : node) { + size_t size = 0; + for (const auto& set_piece : key.pieces(get(key.impl(node)))) { size += get_node_size(set_piece, node_weight); } return size; } -unsigned get_partition_size(const vector& node, const NodeWeight& node_weight) +size_t get_partition_size(const vector& node, const NodeWeight& node_weight) { - unsigned size = 0; + size_t size = 0; for (const auto& set_piece : node) { size += set_piece.cardinal(); } @@ -758,7 +827,7 @@ unsigned get_partition_size(const vector& node, const NodeWeight& node return size; } -unsigned get_edge_set_cost(const SBG::LIB::SetPiece& edges, const EdgeCost& edge_cost) +unsigned get_edge_set_cost(const MultiDimInter& edges, const EdgeCost& edge_cost) { if (edges.isEmpty()) { return 0; @@ -766,10 +835,10 @@ unsigned get_edge_set_cost(const SBG::LIB::SetPiece& edges, const EdgeCost& edge int weight = get_set_cost(edges, edge_cost); // currently, all edges have cost 1 - unsigned acc = edges.intervals().front().end() - edges.intervals().front().begin() + 1; + unsigned acc = edges[0].end() - edges[0].begin() + 1; - for (size_t i = 1; i < edges.intervals().size(); i++) { - auto interval = edges.intervals()[i]; + for (size_t i = 1; i < edges.arity(); i++) { + auto interval = edges[i]; acc = acc * (interval.end() - interval.begin() + 1); } @@ -780,12 +849,13 @@ unsigned get_edge_set_cost(const SBG::LIB::SetPiece& edges, const EdgeCost& edge unsigned get_edge_set_cost(const SBG::LIB::Set& edges, const EdgeCost& edge_cost) { + SetAccessKey key = SetAccess::key(); if (edges.isEmpty()) { return 0; } unsigned size = 0; - for (const auto& set_piece : edges) { + for (const auto& set_piece : key.pieces(get(key.impl(edges)))) { size += get_edge_set_cost(set_piece, edge_cost); } @@ -794,18 +864,19 @@ unsigned get_edge_set_cost(const SBG::LIB::Set& edges, const EdgeCost& edge_cost void flatten_set(Set& set, const WeightedSBGraph& graph) { + SetAccessKey key = SetAccess::key(); if ((not set.isEmpty()) and set.arity() > 1) { logging::sbg_log << "flatten_set for sets with " << set.arity() << " is not implemented" << endl; return; } - auto canonize = [](vector& set_vector) { + auto canonize = [](vector& set_vector) { if (set_vector.empty()) { return set_vector; } sort_partition_intervals(set_vector); - vector new_set_vector = {*set_vector.begin()}; + vector new_set_vector = {*set_vector.begin()}; for (size_t new_set_vector_idx = 0, set_vector_idx = 1; set_vector_idx < set_vector.size(); set_vector_idx++) { if (new_set_vector[new_set_vector_idx][0].end() + 1 == set_vector[set_vector_idx][0].begin()) { new_set_vector[new_set_vector_idx] = @@ -819,17 +890,20 @@ void flatten_set(Set& set, const WeightedSBGraph& graph) return new_set_vector; }; - Set new_partition = SET_FACT.createSet(); - for (const auto& v : graph.V()) { - vector set_piece_this_node_vector; - for (auto set_piece : set) { + Set new_partition; + for (const auto& v : key.pieces(get(key.impl(graph.V())))) { + vector set_piece_this_node_vector; + for (auto set_piece : key.pieces(get(key.impl(set)))) { if (not v.intersection(set_piece).isEmpty()) { set_piece_this_node_vector.emplace_back(set_piece); } } set_piece_this_node_vector = canonize(set_piece_this_node_vector); - new_partition = new_partition.cup(from_vector(set_piece_this_node_vector)); + vector set_this_vector_nodes; + for_each(set_piece_this_node_vector.begin(), set_piece_this_node_vector.end(), + [&set_this_vector_nodes, &key](const auto& sp) { set_this_vector_nodes.push_back(key.createSet(OrderedSet(sp))); }); + new_partition = new_partition.cup(from_vector(set_this_vector_nodes)); } auto diff = set.difference(new_partition); @@ -840,76 +914,450 @@ void flatten_set(Set& set, const WeightedSBGraph& graph) SBG::LIB::WeightedSBGraph create_air_conditioners_graph() { - Set nodes = SET_FACT.createSet(); - nodes.emplaceBack(Interval(0, 1, 99)); // th - nodes.emplaceBack(Interval(100, 1, 100)); // ierr - nodes.emplaceBack(Interval(101, 1, 101)); // ptotal - nodes.emplaceBack(Interval(102, 1, 103)); // ev_1 - nodes.emplaceBack(Interval(104, 1, 105)); // ev_2 - nodes.emplaceBack(Interval(106, 1, 106)); // ev_3 - nodes.emplaceBack(Interval(107, 1, 107)); // ev_4 - nodes.emplaceBack(Interval(108, 1, 108)); // ev_5 - nodes.emplaceBack(Interval(109, 1, 158)); // ev_6 - nodes.emplaceBack(Interval(159, 1, 208)); // ev_7 - nodes.emplaceBack(Interval(209, 1, 308)); // ev_8 + Set nodes; + nodes = nodes.cup(Set(0, 1, 99)); // th + nodes = nodes.cup(Set(100, 1, 100)); // ierr + nodes = nodes.cup(Set(101, 1, 101)); // ptotal + nodes = nodes.cup(Set(102, 1, 103)); // ev_1 + nodes = nodes.cup(Set(104, 1, 105)); // ev_2 + nodes = nodes.cup(Set(106, 1, 106)); // ev_3 + nodes = nodes.cup(Set(107, 1, 107)); // ev_4 + nodes = nodes.cup(Set(108, 1, 108)); // ev_5 + nodes = nodes.cup(Set(109, 1, 158)); // ev_6 + nodes = nodes.cup(Set(159, 1, 208)); // ev_7 + nodes = nodes.cup(Set(209, 1, 308)); // ev_8 // maps - PWMap lhs_maps = PW_FACT.createPWMap(); - PWMap rhs_maps = PW_FACT.createPWMap(); + PWMap lhs_maps = PWMap(); + PWMap rhs_maps = PWMap(); - lhs_maps.emplaceBack(Map(Interval(319, 1, 368), Exp(LExp(1, RATIONAL(-319, 1))))); // E1 - rhs_maps.emplaceBack(Map(Interval(319, 1, 368), Exp(LExp(1, RATIONAL(-210, 1))))); + lhs_maps.insert(Map(Set(319, 1, 368), Expression(Rational(1), Rational(-319, 1)))); // E1 + rhs_maps.insert(Map(Set(319, 1, 368), Expression(Rational(1), Rational(-210, 1)))); - lhs_maps.emplaceBack(Map(Interval(369, 1, 418), Exp(LExp(1, RATIONAL(-319, 1))))); // E2 - rhs_maps.emplaceBack(Map(Interval(369, 1, 418), Exp(LExp(1, RATIONAL(-210, 1))))); + lhs_maps.insert(Map(Set(369, 1, 418), Expression(Rational(1), Rational(-319, 1)))); // E2 + rhs_maps.insert(Map(Set(369, 1, 418), Expression(Rational(1), Rational(-210, 1)))); - lhs_maps.emplaceBack(Map(Interval(419, 1, 518), Exp(LExp(1, RATIONAL(-419, 1))))); // E3 - rhs_maps.emplaceBack(Map(Interval(419, 1, 518), Exp(LExp(1, RATIONAL(-210, 1))))); + lhs_maps.insert(Map(Set(419, 1, 518), Expression(Rational(1), Rational(-419, 1)))); // E3 + rhs_maps.insert(Map(Set(419, 1, 518), Expression(Rational(1), Rational(-210, 1)))); - lhs_maps.emplaceBack(Map(Interval(519, 1, 519), Exp(LExp(0, 100)))); // E4 - rhs_maps.emplaceBack(Map(Interval(519, 1, 519), Exp(LExp(0, 106)))); + lhs_maps.insert(Map(Set(519, 1, 519), Expression(Rational(0), 100))); // E4 + rhs_maps.insert(Map(Set(519, 1, 519), Expression(Rational(0), 106))); - lhs_maps.emplaceBack(Map(Interval(520, 1, 520), Exp(LExp(0, 100)))); // E5 - rhs_maps.emplaceBack(Map(Interval(520, 1, 520), Exp(LExp(0, 107)))); + lhs_maps.insert(Map(Set(520, 1, 520), Expression(Rational(0), 100))); // E5 + rhs_maps.insert(Map(Set(520, 1, 520), Expression(Rational(0), 107))); - lhs_maps.emplaceBack(Map(Interval(521, 1, 521), Exp(LExp(0, 100)))); // E6 - rhs_maps.emplaceBack(Map(Interval(521, 1, 521), Exp(LExp(0, 108)))); + lhs_maps.insert(Map(Set(521, 1, 521), Expression(Rational(0), 100))); // E6 + rhs_maps.insert(Map(Set(521, 1, 521), Expression(Rational(0), 108))); - lhs_maps.emplaceBack(Map(Interval(522, 1, 523), Exp(LExp(1, RATIONAL(-420, 1))))); // E7 - rhs_maps.emplaceBack(Map(Interval(522, 1, 523), Exp(LExp(1, RATIONAL(-418, 1))))); + lhs_maps.insert(Map(Set(522, 1, 523), Expression(Rational(1), Rational(-420, 1)))); // E7 + rhs_maps.insert(Map(Set(522, 1, 523), Expression(Rational(1), Rational(-418, 1)))); - lhs_maps.emplaceBack(Map(Interval(524, 1, 525), Exp(LExp(1, RATIONAL(-420, 1))))); // E8 - rhs_maps.emplaceBack(Map(Interval(524, 1, 525), Exp(LExp(0, 101)))); + lhs_maps.insert(Map(Set(524, 1, 525), Expression(Rational(1), Rational(-420, 1)))); // E8 + rhs_maps.insert(Map(Set(524, 1, 525), Expression(Rational(0), 101))); - lhs_maps.emplaceBack(Map(Interval(526, 1, 526), Exp(LExp(0, 106)))); // E9 - rhs_maps.emplaceBack(Map(Interval(526, 1, 526), Exp(LExp(0, 108)))); + lhs_maps.insert(Map(Set(526, 1, 526), Expression(Rational(0), 106))); // E9 + rhs_maps.insert(Map(Set(526, 1, 526), Expression(Rational(0), 108))); - lhs_maps.emplaceBack(Map(Interval(527, 1, 527), Exp(LExp(0, 107)))); // E10 - rhs_maps.emplaceBack(Map(Interval(527, 1, 527), Exp(LExp(0, 108)))); + lhs_maps.insert(Map(Set(527, 1, 527), Expression(Rational(0), 107))); // E10 + rhs_maps.insert(Map(Set(527, 1, 527), Expression(Rational(0), 108))); - lhs_maps.emplaceBack(Map(Interval(528, 1, 528), Exp(LExp(0, 101)))); // E11 - rhs_maps.emplaceBack(Map(Interval(528, 1, 528), Exp(LExp(0, 108)))); + lhs_maps.insert(Map(Set(528, 1, 528), Expression(Rational(0), 101))); // E11 + rhs_maps.insert(Map(Set(528, 1, 528), Expression(Rational(0), 108))); - lhs_maps.emplaceBack(Map(Interval(529, 1, 578), Exp(LExp(0, 102)))); // E12 - rhs_maps.emplaceBack(Map(Interval(529, 1, 578), Exp(LExp(1, RATIONAL(-420, 1))))); + lhs_maps.insert(Map(Set(529, 1, 578), Expression(Rational(0), 102))); // E12 + rhs_maps.insert(Map(Set(529, 1, 578), Expression(Rational(1), Rational(-420, 1)))); - lhs_maps.emplaceBack(Map(Interval(579, 1, 628), Exp(LExp(0, 103)))); // E13 - rhs_maps.emplaceBack(Map(Interval(579, 1, 628), Exp(LExp(1, RATIONAL(-420, 1))))); + lhs_maps.insert(Map(Set(579, 1, 628), Expression(Rational(0), 103))); // E13 + rhs_maps.insert(Map(Set(579, 1, 628), Expression(Rational(1), Rational(-420, 1)))); - lhs_maps.emplaceBack(Map(Interval(629, 1, 678), Exp(LExp(0, 108)))); // E14 - rhs_maps.emplaceBack(Map(Interval(629, 1, 678), Exp(LExp(1, RATIONAL(-520, 1))))); + lhs_maps.insert(Map(Set(629, 1, 678), Expression(Rational(0), 108))); // E14 + rhs_maps.insert(Map(Set(629, 1, 678), Expression(Rational(1), Rational(-520, 1)))); - lhs_maps.emplaceBack(Map(Interval(679, 1, 729), Exp(LExp(0, 108)))); // E15 - rhs_maps.emplaceBack(Map(Interval(679, 1, 729), Exp(LExp(1, RATIONAL(-520, 1))))); + lhs_maps.insert(Map(Set(679, 1, 729), Expression(Rational(0), 108))); // E15 + rhs_maps.insert(Map(Set(679, 1, 729), Expression(Rational(1), Rational(-520, 1)))); - auto vmap = PW_FACT.createPWMap(); - auto vsap = PW_FACT.createPWMap(); - auto emap = PW_FACT.createPWMap(); + auto vmap = PWMap(); + auto emap = PWMap(); // Now, let's build a graph! - SBG::LIB::WeightedSBGraph graph(nodes, vmap, rhs_maps, lhs_maps, emap, vsap); // This will be our graph + SBG::LIB::WeightedSBGraph graph(nodes, vmap, rhs_maps, lhs_maps, emap); // This will be our graph return graph; } +WeightedSBGraph create_air_conditioners_with_controller_graph(int size, int sections, bool duplicate_1_n_conns) +{ + SetAccessKey key = SetAccess::key(); + int quarter = size / sections; + int start_q = (2 * size) + 13; + + Set nodes; + + // th [label="N1=th {0:999}"] + auto th = Interval(0, 1, size - 1); + nodes = nodes.cup(key.createSet(OrderedSet(th))); + int current_offset = size; + + // ierr [label="N2=ierr{1000:1000}"] + int ierr_domain_init = current_offset; + auto ierr = Interval(current_offset, 1, current_offset); + nodes = nodes.cup(key.createSet(OrderedSet(ierr))); + current_offset += 1; + + // ptotal [label="N3=ptotal {1001:1001}"] + int ptotal_domain_init = current_offset; + auto ptotal = Interval(current_offset, 1, current_offset); + nodes = nodes.cup(key.createSet(OrderedSet(ptotal))); + current_offset += 1; + + // ev_1 [label="N4=ev_1 {1002:1005}" + int ev_1_domain_init = current_offset; + auto ev_1 = Interval(current_offset, 1, current_offset + sections - 1); + nodes = nodes.cup(key.createSet(OrderedSet(ev_1))); + current_offset += sections; + + // ev_2 [label="N5=ev_2 {1006:1009}"] + int ev_2_domain_init = current_offset; + auto ev_2 = Interval(current_offset, 1, current_offset + sections - 1); + nodes = nodes.cup(key.createSet(OrderedSet(ev_2))); + current_offset += sections; + + // ev_3 [label="N6=ev_3 {1010:1010}"] + int ev_3_domain_init = current_offset; + auto ev_3 = Interval(current_offset, 1, current_offset); + nodes = nodes.cup(key.createSet(OrderedSet(ev_3))); + current_offset += 1; + + // ev_4 [label="N7=ev_4 {1011:1011}"] + int ev_4_domain_init = current_offset; + auto ev_4 = Interval(current_offset, 1, current_offset); + nodes = nodes.cup(key.createSet(OrderedSet(ev_4))); + current_offset += 1; + + // ev_5 [label="N8=ev_5 {1012:1012}"] + int ev_5_domain_init = current_offset; + auto ev_5 = Interval(current_offset, 1, current_offset); + nodes = nodes.cup(key.createSet(OrderedSet(ev_5))); + current_offset += 1; + + // ev_6 [label="N9=ev_6 {1013:2012}"] + int ev_6_domain_init = current_offset; + auto ev_6 = Interval(current_offset, 1, current_offset + (size - 1)); + nodes = nodes.cup(key.createSet(OrderedSet(ev_6))); + current_offset += size; + + // nodes from 10 to 13 in the example with 4 sections + // ev_7 [label="N10=ev_7 {2013:2262}"] + // ev_8 [label="N11=ev_8 {2263:2512}"] + // ev_9 [label="N12=ev_9 {2513:2762}"] + int part_total_domain_init = current_offset; + for (int i = 0; i < sections; i++) { + auto ev_i = Interval(current_offset, 1, current_offset + quarter - 1); + nodes = nodes.cup(key.createSet(OrderedSet(ev_i))); + current_offset += quarter; + } + + // maps + PWMap rhs_maps = PWMap(); + PWMap lhs_maps = PWMap(); + + // th -> ev_6 [label="<- {3013:4012} -> ", arrowhead="none"] + rhs_maps.insert(Map(Set(current_offset, 1, current_offset + (size - 1)), Expression(Rational(1), Rational(-current_offset, 1)))); + lhs_maps.insert( + Map(Set(current_offset, 1, current_offset + (size - 1)), Expression(Rational(1), Rational(ev_6_domain_init - current_offset, 1)))); + current_offset += size; + + if (duplicate_1_n_conns) { + rhs_maps.insert(Map(Set(current_offset, 1, current_offset + (size - 1)), Expression(Rational(1), Rational(-current_offset, 1)))); + lhs_maps.insert( + Map(Set(current_offset, 1, current_offset + (size - 1)), Expression(Rational(1), Rational(ev_6_domain_init - current_offset, 1)))); + current_offset += size; + } + + // th -> ev_7 [label="<- {4013:4262} -> ", arrowhead="none"] + // th -> ev_8 [label="<- {4263:4512} -> ", arrowhead="none"] + // th -> ev_9 [label="<- {4513:4762} -> ", arrowhead="none"] + // th -> ev_10 [label="<- {4763:5012} -> ", arrowhead="none"] + for (int i = 0; i < sections; i++) { + rhs_maps.insert( + Map(Set(current_offset, 1, current_offset + (quarter - 1)), Expression(Rational(1), Rational(-current_offset + i * quarter, 1)))); + lhs_maps.insert(Map(Set(current_offset, 1, current_offset + (quarter - 1)), + Expression(Rational(1), Rational(part_total_domain_init + i * quarter - current_offset, 1)))); + current_offset += quarter; + } + if (duplicate_1_n_conns) { + for (int i = 0; i < sections; i++) { + rhs_maps.insert( + Map(Set(current_offset, 1, current_offset + (quarter - 1)), Expression(Rational(1), Rational(-current_offset + i * quarter, 1)))); + lhs_maps.insert(Map(Set(current_offset, 1, current_offset + (quarter - 1)), + Expression(Rational(1), Rational(part_total_domain_init + i * quarter - current_offset, 1)))); + current_offset += quarter; + } + } + + // ierr -> ev_3 [label="<- {5013:5013} -> ", arrowhead="none"] + rhs_maps.insert(Map(Set(current_offset, 1, current_offset), Expression(Rational(0), ierr_domain_init))); + lhs_maps.insert(Map(Set(current_offset, 1, current_offset), Expression(Rational(0), ev_3_domain_init))); + current_offset += 1; + + // ierr -> ev_4 [label="<- {5014:5014} -> ", arrowhead="none"] + rhs_maps.insert(Map(Set(current_offset, 1, current_offset), Expression(Rational(0), ierr_domain_init))); + lhs_maps.insert(Map(Set(current_offset, 1, current_offset), Expression(Rational(0), ev_4_domain_init))); + current_offset += 1; + + // ierr -> ev_5 [label="<- {5015:5015} -> ", arrowhead="none"] + rhs_maps.insert(Map(Set(current_offset, 1, current_offset), Expression(Rational(0), ierr_domain_init))); + lhs_maps.insert(Map(Set(current_offset, 1, current_offset), Expression(Rational(0), ev_5_domain_init))); + current_offset += 1; + + // ptotal -> ev_1 [label="1001 <- {5016:5019} -> ", arrowhead="none"] + rhs_maps.insert(Map(Set(current_offset, 1, current_offset + sections - 1), Expression(Rational(0), ptotal_domain_init))); + lhs_maps.insert( + Map(Set(current_offset, 1, current_offset + sections - 1), Expression(Rational(1), Rational(ev_1_domain_init - current_offset, 1)))); + current_offset += sections; + + // ptotal -> ev_2 [label="1001 <- {5020:5023} -> ", arrowhead="none"] + rhs_maps.insert(Map(Set(current_offset, 1, current_offset + sections - 1), Expression(Rational(0), ptotal_domain_init))); + lhs_maps.insert( + Map(Set(current_offset, 1, current_offset + sections - 1), Expression(Rational(1), Rational(ev_2_domain_init - current_offset, 1)))); + current_offset += sections; + + // ptotal -> ev_5 [label="1001 <- {5024:5024} -> 1012", arrowhead="none"] + rhs_maps.insert(Map(Set(current_offset, 1, current_offset), Expression(Rational(0), ptotal_domain_init))); + lhs_maps.insert(Map(Set(current_offset, 1, current_offset), Expression(Rational(0), ev_5_domain_init))); + current_offset += 1; + + // ev_1 -> ev_2 [label=" <- {5025:5028} -> ", arrowhead="none"] + rhs_maps.insert( + Map(Set(current_offset, 1, current_offset + sections - 1), Expression(Rational(1), Rational(ev_1_domain_init - current_offset, 1)))); + lhs_maps.insert( + Map(Set(current_offset, 1, current_offset + sections - 1), Expression(Rational(1), Rational(ev_2_domain_init - current_offset, 1)))); + current_offset += sections; + + // ev_1 -> ev_7 [label="1002 <- {5029:5278} -> ", arrowhead="none"] + // ev_1 -> ev_8 [label="1003 <- {5279:5528} -> ", arrowhead="none"] + // ev_1 -> ev_9 [label="1004 <- {5529:5778} -> ", arrowhead="none"] + // ev_1 -> ev_10 [label="1005 <- {5779:6028} -> ", arrowhead="none"] + for (int i = 0; i < sections; i++) { + rhs_maps.insert(Map(Set(current_offset, 1, current_offset + quarter - 1), Expression(Rational(0), ev_1_domain_init))); + lhs_maps.insert(Map(Set(current_offset, 1, current_offset + quarter - 1), + Expression(Rational(1), Rational(part_total_domain_init - current_offset + quarter * i, 1)))); + current_offset += quarter; + } + if (duplicate_1_n_conns) { + for (int i = 0; i < sections; i++) { + rhs_maps.insert(Map(Set(current_offset, 1, current_offset + quarter - 1), Expression(Rational(0), ev_1_domain_init))); + lhs_maps.insert(Map(Set(current_offset, 1, current_offset + quarter - 1), + Expression(Rational(1), Rational(part_total_domain_init - current_offset + quarter * i, 1)))); + current_offset += quarter; + } + } + + // ev_3 -> ev_5 [label="1010 <- {6029:6029} -> 1012", arrowhead="none"] + rhs_maps.insert(Map(Set(current_offset, 1, current_offset), Expression(Rational(0), ev_3_domain_init))); + lhs_maps.insert(Map(Set(current_offset, 1, current_offset), Expression(Rational(0), ev_5_domain_init))); + current_offset += 1; + + // ev_4 -> ev_5 [label="1011 <- {6030:6030} -> 1012", arrowhead="none"] + rhs_maps.insert(Map(Set(current_offset, 1, current_offset), Expression(Rational(0), ev_4_domain_init))); + lhs_maps.insert(Map(Set(current_offset, 1, current_offset), Expression(Rational(0), ev_5_domain_init))); + current_offset += 1; + + // ev_5 -> ev_7 [label="1012 <- {6031:6280} -> ", arrowhead="none"] + // ev_5 -> ev_8 [label="1012 <- {6281:6530} -> ", arrowhead="none"] + // ev_5 -> ev_9 [label="1012 <- {6531:6780} -> ", arrowhead="none"] + // ev_5 -> ev_10 [label="1012 <- {6781:7030} -> ", arrowhead="none"] + for (int i = 0; i < sections; i++) { + rhs_maps.insert(Map(Set(current_offset, 1, current_offset + quarter - 1), Expression(Rational(0), ev_5_domain_init))); + lhs_maps.insert(Map(Set(current_offset, 1, current_offset + quarter - 1), + Expression(Rational(1), Rational(-current_offset + part_total_domain_init + i * quarter, 1)))); + current_offset += quarter; + } + + auto vmap = PWMap(); + auto emap = PWMap(); + + // Now, let's build a graph! + SBG::LIB::WeightedSBGraph graph(nodes, vmap, lhs_maps, rhs_maps, emap); // This will be our graph + + cout << graph << endl; + + return graph; +} + +WeightedSBGraph create_advection2D_graph(int size) +{ + Set nodes; + + // Real u[N,N]; + nodes = nodes.cup(Set(0, 1, size - 1).cartesianProduct(Set(0, 1, size - 1))); + int current_offset_1 = size; + int current_offset_2 = size; + + // maps + PWMap rhs_maps = PWMap(); + PWMap lhs_maps = PWMap(); + + // Borders + // for i in 2:N loop + // der(u[i,1])=-ax*u[i,1]/dx - ay*(u[i,1] - u[i-1,1])/dy + r*(u[i,1]^2-u[i,1]^3); + // end for; + rhs_maps.insert(Map(Set(current_offset_1, 1, current_offset_1 + size - 2).cartesianProduct(Set(current_offset_2, 1, current_offset_2)), + Expression(Rational(1), Rational(-current_offset_1 + 1, 1)) + .cartesianProduct(Expression(Rational(0), 0)))); // row fixed in 1 (first one) + lhs_maps.insert(Map(Set(current_offset_1, 1, current_offset_1 + size - 2).cartesianProduct(Set(current_offset_2, 1, current_offset_2)), + Expression(Rational(1), Rational(-current_offset_1, 1)).cartesianProduct(Expression(Rational(0), 0)))); + current_offset_1 += size - 1; + current_offset_2 += 1; + + // Borders + // for j in 2:N loop + // der(u[1,j])=-ax*(u[1,j] - u[1,j-1])/dx - ay*u[1,j]/dy + r*(u[1,j]^2-u[1,j]^3); + // end for; + rhs_maps.insert(Map(Set(current_offset_1, 1, current_offset_1).cartesianProduct(Set(current_offset_2, 1, current_offset_2 + size - 2)), + Expression(Rational(0), 0) + .cartesianProduct(Expression(Rational(1), Rational(-current_offset_2 + 1, 1))))); // row fixed in 1 (first one) + lhs_maps.insert(Map(Set(current_offset_1, 1, current_offset_1).cartesianProduct(Set(current_offset_2, 1, current_offset_2 + size - 2)), + Expression(Rational(0), 0).cartesianProduct(Expression(Rational(1), Rational(-current_offset_2, 1))))); + current_offset_1 += 1; + current_offset_2 += size - 1; + + // Rest of the grid. + // for i in 2:N, j in 2:N loop + // der(u[i, j]) -> u[i,j-1] + rhs_maps.insert( + Map(Set(current_offset_1, 1, current_offset_1 + size - 2).cartesianProduct(Set(current_offset_2, 1, current_offset_2 + size - 2)), + Expression(Rational(1), Rational(-current_offset_1 + 1, 1)) + .cartesianProduct(Expression(Rational(1), Rational(-current_offset_2 + 1, 1))))); // row fixed in 1 (first one) + lhs_maps.insert( + Map(Set(current_offset_1, 1, current_offset_1 + size - 2).cartesianProduct(Set(current_offset_2, 1, current_offset_2 + size - 2)), + Expression(Rational(1), Rational(-current_offset_1 + 1, 1)) + .cartesianProduct(Expression(Rational(1), Rational(-current_offset_2, 1))))); + current_offset_1 += size - 1; + current_offset_2 += size - 1; + // der(u[i, j]) -> u[i-1,j]) + rhs_maps.insert( + Map(Set(current_offset_1, 1, current_offset_1 + size - 2).cartesianProduct(Set(current_offset_2, 1, current_offset_2 + size - 2)), + Expression(Rational(1), Rational(-current_offset_1 + 1, 1)) + .cartesianProduct(Expression(Rational(1), Rational(-current_offset_2 + 1, 1))))); // row fixed in 1 (first one) + lhs_maps.insert( + Map(Set(current_offset_1, 1, current_offset_1 + size - 2).cartesianProduct(Set(current_offset_2, 1, current_offset_2 + size - 2)), + Expression(Rational(1), Rational(-current_offset_1, 1)) + .cartesianProduct(Expression(Rational(1), Rational(-current_offset_2 + 1, 1))))); + current_offset_1 += size - 1; + current_offset_2 += size - 1; + + auto vmap = PWMap(); + auto emap = PWMap(); + + // Now, let's build a graph! + SBG::LIB::WeightedSBGraph graph(nodes, vmap, rhs_maps, lhs_maps, emap); // This will be our graph + + // cout << graph << endl; + + return graph; +} + +WeightedSBGraph create_starpu_graph(int size) +{ + // Tiles A[N,N] + Set nodes = Set(0, 1, size - 1).cartesianProduct(Set(0, 1, size - 1)); + int current_offset_1 = size; + int current_offset_2 = size; + + // maps + PWMap rhs_maps = PWMap(); // edge -> (X, Y), the dependent tile + PWMap lhs_maps = PWMap(); // edge -> the tile (X, Y) depends on + + // dgetrf_nopiv on (k, k) affects the tiles of row k and column k after it: + // (k, Y) depends on (k, k), for Y in k+1:N-1 + // (X, k) depends on (k, k), for X in k+1:N-1 + // Edge (current_offset_1 + X, current_offset_2 + Y) stands for tile (X, Y), so each + // off-diagonal tile has one edge, and its pivot is (k, k) with k = min(X, Y). + Set getrf_edges; + for (int k = 0; k < size - 1; k++) { + // row k, right of the diagonal + Set domain = + Set(current_offset_1 + k, 1, current_offset_1 + k).cartesianProduct(Set(current_offset_2 + k + 1, 1, current_offset_2 + size - 1)); + // column k, below the diagonal + domain = domain.cup( + Set(current_offset_1 + k + 1, 1, current_offset_1 + size - 1).cartesianProduct(Set(current_offset_2 + k, 1, current_offset_2 + k))); + + lhs_maps.insert(Map(domain, Expression(Rational(0), k).cartesianProduct(Expression(Rational(0), k)))); // -> (k, k) + getrf_edges = getrf_edges.cup(domain); + } + // -> (X, Y): the edge grid translated back to the tile grid + rhs_maps.insert(Map( + getrf_edges, + Expression(Rational(1), Rational(-current_offset_1, 1)).cartesianProduct(Expression(Rational(1), Rational(-current_offset_2, 1))))); + current_offset_1 += size; + current_offset_2 += size; + + // dtrsm on (X, k) and (k, Y) affects dgemm on (X, Y) at step k: + // (X, Y) depends on (X, k) and on (k, Y), for X, Y in k+1:N-1 + // Edge (current_offset_1 + X, current_offset_2 + Y) stands for tile (X, Y), and the + // offsets move after each map so the edge domains don't overlap. + for (int k = 0; k < size - 1; k++) { + // (X, Y) -> (X, k), previous element in the same row + Set domain = Set(current_offset_1 + k + 1, 1, current_offset_1 + size - 1) + .cartesianProduct(Set(current_offset_2 + k + 1, 1, current_offset_2 + size - 1)); + rhs_maps.insert(Map( + domain, + Expression(Rational(1), Rational(-current_offset_1, 1)).cartesianProduct(Expression(Rational(1), Rational(-current_offset_2, 1))))); + lhs_maps.insert(Map(domain, Expression(Rational(1), Rational(-current_offset_1, 1)).cartesianProduct(Expression(Rational(0), k)))); + current_offset_1 += size; + current_offset_2 += size; + + // (X, Y) -> (k, Y), previous element in the same column + domain = Set(current_offset_1 + k + 1, 1, current_offset_1 + size - 1) + .cartesianProduct(Set(current_offset_2 + k + 1, 1, current_offset_2 + size - 1)); + rhs_maps.insert(Map( + domain, + Expression(Rational(1), Rational(-current_offset_1, 1)).cartesianProduct(Expression(Rational(1), Rational(-current_offset_2, 1))))); + lhs_maps.insert(Map(domain, Expression(Rational(0), k).cartesianProduct(Expression(Rational(1), Rational(-current_offset_2, 1))))); + current_offset_1 += size; + current_offset_2 += size; + } + + auto vmap = PWMap(); + auto emap = PWMap(); + + rhs_maps.compact(); + lhs_maps.compact(); + + // Now, let's build a graph! + SBG::LIB::WeightedSBGraph graph(nodes, vmap, rhs_maps, lhs_maps, emap); // This will be our graph + + return graph; +} + +Set split_sets_according_to_relations(const WeightedSBGraph& sb_graph) +{ + set endpoints = {}; + SetAccessKey key = SetAccess::key(); + PWMap pw_map1 = sb_graph.map1(); + PWMap pw_map2 = sb_graph.map2(); + for (auto it1 = pw_map1.begin(), it2 = pw_map2.begin(); it1 != pw_map1.end() and it2 != pw_map2.end(); ++it1, ++it2) { + const Interval im1 = key.pieces(get(key.impl((*it1).image()))).front()[0]; + endpoints.insert(im1.begin()); + endpoints.insert(im1.end() + 1); + + const Interval im2 = key.pieces(get(key.impl((*it2).image()))).front()[0]; + endpoints.insert(im2.begin()); + endpoints.insert(im2.end() + 1); + } + + auto vertices = Set(); + for (auto it = endpoints.begin();;) { + int current = *it; + ++it; + if (it == endpoints.end()) { + break; + } + vertices = vertices.cup(Set(current, 1, *it - 1)); + } + + return vertices; +} + } // namespace sbg_partitioner diff --git a/algorithms/partitioner/build_sb_graph.hpp b/algorithms/partitioner/build_sb_graph.hpp index 878d90f8..fdfd9252 100644 --- a/algorithms/partitioner/build_sb_graph.hpp +++ b/algorithms/partitioner/build_sb_graph.hpp @@ -19,6 +19,9 @@ #pragma once +#include +#include +#include #include #include "weighted_sb_graph.hpp" @@ -37,7 +40,7 @@ SBG::LIB::WeightedSBGraph build_sb_graph(const std::string& filename, bool compa /// @param image_interval Image we want to get the pre image from /// @param expression Expression to get the pre image /// @return pre image as an interval -SBG::LIB::Interval get_pre_image(const SBG::LIB::Interval& image_interval, const SBG::LIB::LExp& expression); +SBG::LIB::detail::Interval get_pre_image(const SBG::LIB::detail::Interval& image_interval, const SBG::LIB::detail::LinearExpr& expression); /// Takes a graph and a set of nodes and return a set of nodes connected with the mentioned set. @@ -53,15 +56,15 @@ SBG::LIB::Set get_adjacents(const SBG::LIB::SBG& graph, const SBG::LIB::Set& nod /// {[1:10], [1:10]} has 100 elements. /// @param node input set we want to calculate the size. /// @return the number of elements -unsigned get_node_size(const SBG::LIB::SetPiece& node, const SBG::LIB::NodeWeight& node_weight); +size_t get_node_size(const SBG::LIB::detail::MultiDimInter& node, const SBG::LIB::NodeWeight& node_weight); /// Takes each set piece and calculates its size, it returns the sum of them -unsigned get_node_size(const SBG::LIB::Set& node, const SBG::LIB::NodeWeight& node_weight); +size_t get_node_size(const SBG::LIB::Set& node, const SBG::LIB::NodeWeight& node_weight); /// Takes each set piece of the partition and calculates its size, it returns the sum of them -unsigned get_partition_size(const std::vector& node, const SBG::LIB::NodeWeight& node_weight); +size_t get_partition_size(const std::vector& node, const SBG::LIB::NodeWeight& node_weight); /// Takes a set of edges and compute its cost. @@ -73,7 +76,7 @@ unsigned get_edge_set_cost(const SBG::LIB::Set& edges, const SBG::LIB::EdgeCost& /// Takes a set piece of edges and compute its cost. /// @param edges - input set piece edges we want to calculate the size. /// @return the cost of edges (number of elements * communication cost). -unsigned get_edge_set_cost(const SBG::LIB::SetPiece& node, const SBG::LIB::EdgeCost& edge_cost); +unsigned get_edge_set_cost(const SBG::LIB::detail::MultiDimInter& node, const SBG::LIB::EdgeCost& edge_cost); /// Flattens a set of nodes according to the graph nodes. @@ -85,18 +88,36 @@ void flatten_set(SBG::LIB::Set &set, const SBG::LIB::WeightedSBGraph& graph); SBG::LIB::WeightedSBGraph create_air_conditioners_graph(); +SBG::LIB::WeightedSBGraph create_air_conditioners_with_controller_graph(int size, int sections, bool duplicate_1_n_conns); + + +SBG::LIB::WeightedSBGraph create_advection2D_graph(int size); + + +SBG::LIB::WeightedSBGraph create_starpu_graph(int size); + + +/*** + * @brief Splits sets according to their relations, defined by the piecewise maps of the graph. + */ +SBG::LIB::Set split_sets_according_to_relations(const SBG::LIB::WeightedSBGraph& sb_graph); + + +/// Wraps a single MultiDimInter piece into a Set without requiring set_detail.hpp in callers. +SBG::LIB::Set to_set(const SBG::LIB::detail::MultiDimInter& piece); + + /// It returns the edge cost or node weight of the input set. It looks for a key in cost that intersects /// the input set, and returns its value. If no key intersects the input set, it will return 1. /// @param set input set we want to know the cost or weight/ /// @param costs hashtable with set/costs. /// @return the cost of set. template -int get_set_cost(const SBG::LIB::SetPiece& set_piece, const T& costs) +int get_set_cost(const SBG::LIB::Set& set, const T& costs) { int weight = 1; - auto set = SBG::LIB::SET_FACT.createSet(set_piece); for (const auto& [cost_set, w] : costs) { - if (set.intersection(cost_set).size() > 0) { + if (not set.intersection(cost_set).isEmpty()) { weight = costs.at(cost_set); } } @@ -105,14 +126,21 @@ int get_set_cost(const SBG::LIB::SetPiece& set_piece, const T& costs) } +template +int get_set_cost(const SBG::LIB::detail::MultiDimInter& set_piece, const T& costs) +{ + return get_set_cost(to_set(set_piece), costs); +} + + size_t get_set_size(const SBG::LIB::Set& set); std::pair cut_interval_by_dimension( - SBG::LIB::Set& set_piece, + const SBG::LIB::Set& set_piece, const SBG::LIB::NodeWeight& node_weight, std::size_t size); -std::pair cut_interval(const SBG::LIB::SetPiece& interval, int cut_value); +std::pair cut_interval(const SBG::LIB::detail::MultiDimInter& interval, int cut_value); } // namespace sbg-partitioner diff --git a/algorithms/partitioner/communication_cost.cpp b/algorithms/partitioner/communication_cost.cpp index cd97d421..4e4a4bfb 100644 --- a/algorithms/partitioner/communication_cost.cpp +++ b/algorithms/partitioner/communication_cost.cpp @@ -16,23 +16,29 @@ ******************************************************************************/ +#include + #include #include +#include #include #include "communication_cost.hpp" #include "partition_graph.hpp" -#include "partition_graph_cc.hpp" #include "sbg_partitioner_log.hpp" - using namespace std; using namespace SBG::LIB; +using SBG::LIB::detail::MultiDimInter; +using SBG::LIB::detail::OrderedSet; +using SBG::LIB::detail::SetAccess; +using SBG::LIB::detail::SetAccessKey; namespace sbg_partitioner { -std::size_t SetPieceHash::set_piece_hash(const SetPiece& set_piece) { +std::size_t SetPieceHash::set_piece_hash(const MultiDimInter& set_piece) +{ constexpr size_t magic_number = 0x9e3779b9; std::size_t seed = 0; for (const auto& interval : set_piece) { @@ -43,361 +49,344 @@ std::size_t SetPieceHash::set_piece_hash(const SetPiece& set_piece) { return seed; } -std::size_t SetPieceHash::operator()(const SetPiece& set_piece) const { +std::size_t SetPieceHash::operator()(const MultiDimInter& set_piece) const +{ std::size_t seed = set_piece_hash(set_piece); return seed; } - std::size_t SetHash::operator()(const SBG::LIB::Set& set) const { - std::size_t seed = set.size(); - for (const auto& set_piece : set) { - seed ^= SetPieceHash::set_piece_hash(set_piece) + 0x9e3779b9 + (seed << 6) + (seed >> 2); - } - return seed; + SetAccessKey key = SetAccess::key(); + const auto set_pieces = key.pieces(get(key.impl(set))); + std::size_t seed = set_pieces.size(); + for (const auto& set_piece : set_pieces) { + seed ^= SetPieceHash::set_piece_hash(set_piece) + 0x9e3779b9 + (seed << 6) + (seed >> 2); + } + return seed; } - -unordered_map CommunicationCost::_communication_by_set_piece = {}; +unordered_map CommunicationCost::_communication_by_set_piece = {}; +unordered_map CommunicationCost::_communication_by_set_piece_with_common_edges = {}; +unordered_map CommunicationCost::_inner_edges = {}; namespace internal { - // the only real instance CommunicationCostPtr cost_matrix = nullptr; - namespace { - -Set set_piece_communication(const SetPiece& nodes, const WeightedSBGraph& graph) +tuple set_piece_communication(const Set& node_set, const WeightedSBGraph& graph) { - // convert nodes into a set - auto node_set = SET_FACT.createSet(nodes); + // convert nodes into a set + // auto node_set = create_set(nodes); - // compute preImage of map1 and map2 to get the edges that connects `nodes` - auto edges_map1 = graph.map1().preImage(node_set); - auto edges_map2 = graph.map2().preImage(node_set); + // compute preImage of map1 and map2 to get the edges that connects `nodes` + auto edges_map1 = graph.map1().preImage(node_set); + auto edges_map2 = graph.map2().preImage(node_set); - // Now compute the disjoint union to remove loop edges - auto common_edges = edges_map1.intersection(edges_map2); - auto communication = edges_map1.cup(edges_map2).difference(common_edges); + auto edges = edges_map1.cup(edges_map2); - return communication; -} + // Now compute the disjoint union to remove loop edges + auto common_edges = edges_map1.intersection(edges_map2); + auto non_common_edges = edges.difference(common_edges); + return {edges, non_common_edges, common_edges}; } -} +} // namespace +} // namespace internal CommunicationCost::CommunicationCost(const WeightedSBGraph& graph, PartitionMap partitions) - : ICommunicationCost(), - _graph(graph), - _partitions(partitions) + : ICommunicationCost(), _graph(graph), _partitions(partitions) { - initialize(); + initialize(); } - -void CommunicationCost::initialize() +pair CommunicationCost::get_part_communication(unsigned id) { - // compute cost by interval and partitions - _cost_by_partition.reserve(_partitions.size()); - _ec_cost_by_interval.reserve(_partitions.size()); - _ic_cost_by_interval.reserve(_partitions.size()); - for (size_t i = 0; i < _partitions.size(); i++) { - Set partition_i_communication = SET_FACT.createSet(); - Set internal_communication_partition_i = SET_FACT.createSet(); - - for (const auto& node : _partitions.at(i)) { - if (_communication_by_set_piece.find(node) == _communication_by_set_piece.end()) { - _communication_by_set_piece.insert({node, internal::set_piece_communication(node, _graph)}); - } - - const auto& node_edges = _communication_by_set_piece.at(node); - internal_communication_partition_i = node_edges.intersection(partition_i_communication).cup(internal_communication_partition_i); - partition_i_communication = partition_i_communication.cup(node_edges); - } - - auto ec_parition_i = partition_i_communication.difference(internal_communication_partition_i); - _cost_by_partition.emplace_back(make_pair(ec_parition_i, move(internal_communication_partition_i))); - - _ic_cost_by_interval.emplace_back(); // save space for this, will be filled on demand - _ec_cost_by_interval.emplace_back(); + Set partition_i_communication; + Set internal_communication_partition_i; + + for (const auto& node : _partitions.at(id)) { + if (_communication_by_set_piece.find(node) == _communication_by_set_piece.end()) { + auto [edges, non_common_edges, common_edges] = internal::set_piece_communication(node, _graph); + _communication_by_set_piece.insert({node, non_common_edges}); + _communication_by_set_piece_with_common_edges.insert({node, edges}); + _inner_edges.insert({node, common_edges}); } -} + // } + // for (const auto& node : _partitions.at(id)) { + const auto& node_edges = _communication_by_set_piece_with_common_edges.at(node); -void CommunicationCost::update_partitions(PartitionMap& partitions, optional>> modified_partitions) -{ - _partitions = partitions; - if (modified_partitions) { - // now, update communication for partitions that were updated - for (size_t i : modified_partitions->get()) { - Set partition_i_communication = SET_FACT.createSet(); - Set internal_communication_partition_i = SET_FACT.createSet(); - - for (const auto& node : _partitions.at(i)) { - if (_communication_by_set_piece.find(node) == _communication_by_set_piece.end()) { - _communication_by_set_piece.insert({node, internal::set_piece_communication(node, _graph)}); - } - - const auto& node_edges = _communication_by_set_piece.at(node); - internal_communication_partition_i = node_edges.intersection(partition_i_communication).cup(internal_communication_partition_i); - partition_i_communication = partition_i_communication.cup(node_edges); - } - - auto ec_parition_i = partition_i_communication.difference(internal_communication_partition_i); - _cost_by_partition[i] = (make_pair(ec_parition_i, move(internal_communication_partition_i))); - - _ic_cost_by_interval[i].clear(); - _ec_cost_by_interval[i].clear(); - } - } else { - // if modified partitions was not provided, update everything - _cost_by_partition.clear(); - _ec_cost_by_interval.clear(); - _ic_cost_by_interval.clear(); - initialize(); - } -} + // this takes care of the base case + internal_communication_partition_i = internal_communication_partition_i.cup(_inner_edges.at(node)); + + internal_communication_partition_i = node_edges.intersection(partition_i_communication).cup(internal_communication_partition_i); + partition_i_communication = partition_i_communication.cup(node_edges); + } + auto ec_parition_i = partition_i_communication.difference(internal_communication_partition_i); -Set CommunicationCost::get_ec_by_partition_id(unsigned partition_id) -{ - return _cost_by_partition[partition_id].first; + return {ec_parition_i, internal_communication_partition_i}; } - -pair CommunicationCost::compute_ec_ic(unsigned partition_id, const SetPiece& nodes) +void CommunicationCost::initialize() { - if (_communication_by_set_piece.find(nodes) == _communication_by_set_piece.end()) { - _communication_by_set_piece.insert({nodes, internal::set_piece_communication(nodes, _graph)}); - } - - auto communication = _communication_by_set_piece.at(nodes); - - auto ec = communication.intersection(_cost_by_partition[partition_id].first); - auto ic = communication.difference(ec); - _ec_cost_by_interval[partition_id].insert({nodes, ec}); - _ic_cost_by_interval[partition_id].insert({nodes, ic}); - - return { ec, ic }; + cout << "CommunicationCost::initialize " << _partitions << endl; + // compute cost by interval and partitions + _cost_by_partition.reserve(_partitions.size()); + _ec_cost_by_interval.reserve(_partitions.size()); + _ic_cost_by_interval.reserve(_partitions.size()); + for (size_t i = 0; i < _partitions.size(); i++) { + auto [ec_i, ic_i] = get_part_communication(i); + _cost_by_partition.emplace_back(make_pair(ec_i, ic_i)); + + _ic_cost_by_interval.emplace_back(); // save space for this, will be filled on demand + _ec_cost_by_interval.emplace_back(); + } } - -Set CommunicationCost::get_ec_by_interval(unsigned partition_id, const SetPiece& nodes) +void CommunicationCost::update_partitions(PartitionMap& partitions, optional>> modified_partitions) { - if (_ec_cost_by_interval[partition_id].find(nodes) != _ec_cost_by_interval[partition_id].end()) { - return _ec_cost_by_interval[partition_id].at(nodes); - } - - if (_communication_by_set_piece.find(nodes) == _communication_by_set_piece.end()) { - _communication_by_set_piece.insert({nodes, internal::set_piece_communication(nodes, _graph)}); + _partitions = partitions; + if (modified_partitions) { + // now, update communication for partitions that were updated + for (size_t i : modified_partitions->get()) { + auto [ec_i, ic_i] = get_part_communication(i); + _cost_by_partition[i] = (make_pair(ec_i, ic_i)); + + _ic_cost_by_interval[i].clear(); + _ec_cost_by_interval[i].clear(); } - - auto [ec, _] = compute_ec_ic(partition_id, nodes); - - return ec; + } else { + // if modified partitions was not provided, update everything + _cost_by_partition.clear(); + _ec_cost_by_interval.clear(); + _ic_cost_by_interval.clear(); + initialize(); + } } +Set CommunicationCost::get_ec_by_partition_id(unsigned partition_id) { return _cost_by_partition[partition_id].first; } -Set CommunicationCost::get_ic_by_interval(unsigned partition_id, const SetPiece& nodes) +pair CommunicationCost::compute_ec_ic(unsigned partition_id, const Set& nodes) { - if (_ic_cost_by_interval[partition_id].find(nodes) != _ic_cost_by_interval[partition_id].end()) { - return _ic_cost_by_interval[partition_id].at(nodes); - } + if (_communication_by_set_piece.find(nodes) == _communication_by_set_piece.end()) { + auto [edges, non_common_edges, common_edges] = internal::set_piece_communication(nodes, _graph); + _communication_by_set_piece.insert({nodes, non_common_edges}); + _communication_by_set_piece_with_common_edges.insert({nodes, edges}); + _inner_edges.insert({nodes, common_edges}); + } - if (_communication_by_set_piece.find(nodes) == _communication_by_set_piece.end()) { - _communication_by_set_piece.insert({nodes, internal::set_piece_communication(nodes, _graph)}); - } + auto communication = _communication_by_set_piece.at(nodes); - auto [_, ic] = compute_ec_ic(partition_id, nodes); + auto ec = communication.intersection(_cost_by_partition[partition_id].first); + auto ic = communication.difference(ec); + _ec_cost_by_interval[partition_id].insert({nodes, ec}); + _ic_cost_by_interval[partition_id].insert({nodes, ic}); - return ic; + return {ec, ic}; } - -Set CommunicationCost::get_set_piece_edges(const SBG::LIB::SetPiece& nodes) +Set CommunicationCost::get_ec_by_interval(unsigned partition_id, const Set& nodes) { - if (_communication_by_set_piece.find(nodes) == _communication_by_set_piece.end()) { - _communication_by_set_piece.insert({nodes, internal::set_piece_communication(nodes, _graph)}); - } - - return _communication_by_set_piece.at(nodes); -} - - -void CommunicationCost::clear_communication_cache() { _communication_by_set_piece.clear(); } - - + if (_ec_cost_by_interval[partition_id].find(nodes) != _ec_cost_by_interval[partition_id].end()) { + return _ec_cost_by_interval[partition_id].at(nodes); + } -CommunicationCostSync::CommunicationCostSync(const WeightedSBGraph& graph, PartitionMap partitions) - :ICommunicationCost(), - _comm_cost(graph, partitions) -{} + if (_communication_by_set_piece.find(nodes) == _communication_by_set_piece.end()) { + auto [edges, non_common_edges, common_edges] = internal::set_piece_communication(nodes, _graph); + _communication_by_set_piece.insert({nodes, non_common_edges}); + _communication_by_set_piece_with_common_edges.insert({nodes, edges}); + _inner_edges.insert({nodes, common_edges}); + } + auto [ec, _] = compute_ec_ic(partition_id, nodes); -void CommunicationCostSync::update_partitions(PartitionMap& partitions, optional>> modified_partitions) -{ - const lock_guard lock(_mutex); - _comm_cost.update_partitions(partitions, modified_partitions); + return ec; } - -Set CommunicationCostSync::get_ec_by_partition_id(unsigned partition_id) +Set CommunicationCost::get_ic_by_interval(unsigned partition_id, const Set& nodes) { - const lock_guard lock(_mutex); - return _comm_cost.get_ec_by_partition_id(partition_id); -} - + if (_ic_cost_by_interval[partition_id].find(nodes) != _ic_cost_by_interval[partition_id].end()) { + return _ic_cost_by_interval[partition_id].at(nodes); + } -Set CommunicationCostSync::get_ec_by_interval(unsigned partition_id, const SetPiece& nodes) -{ - const lock_guard lock(_mutex); - return _comm_cost.get_ec_by_interval(partition_id, nodes); -} + if (_communication_by_set_piece.find(nodes) == _communication_by_set_piece.end()) { + auto [edges, non_common_edges, common_edges] = internal::set_piece_communication(nodes, _graph); + _communication_by_set_piece.insert({nodes, non_common_edges}); + _communication_by_set_piece_with_common_edges.insert({nodes, edges}); + _inner_edges.insert({nodes, common_edges}); + } + auto [_, ic] = compute_ec_ic(partition_id, nodes); -Set CommunicationCostSync::get_ic_by_interval(unsigned partition_id, const SetPiece& nodes) -{ - const lock_guard lock(_mutex); - return _comm_cost.get_ic_by_interval(partition_id, nodes); + return ic; } - -Set CommunicationCostSync::get_set_piece_edges(const SetPiece& nodes) +Set CommunicationCost::get_set_piece_edges(const SBG::LIB::Set& nodes) { - const lock_guard lock(_mutex); - return _comm_cost.get_set_piece_edges(nodes); -} - + if (_communication_by_set_piece.find(nodes) == _communication_by_set_piece.end()) { + auto [edges, non_common_edges, common_edges] = internal::set_piece_communication(nodes, _graph); + _communication_by_set_piece.insert({nodes, non_common_edges}); + _communication_by_set_piece_with_common_edges.insert({nodes, edges}); + _inner_edges.insert({nodes, common_edges}); + } -void CommunicationCostSync::clear_communication_cache() -{ - const lock_guard lock(_mutex); - return _comm_cost.clear_communication_cache(); + return _communication_by_set_piece.at(nodes); } - - -CommunicationCostCC::CommunicationCostCC(const WeightedSBGraph& graph, const using_cc::SetPointers& nodes) - : _graph(graph), - _sorted_nodes(nodes), - _set_piece_indices(PW_FACT.createPWMap()) +void CommunicationCost::clear_communication_cache() { - initialize(); + _communication_by_set_piece.clear(); + _communication_by_set_piece_with_common_edges.clear(); + _inner_edges.clear(); } - -void CommunicationCostCC::initialize() +CommunicationCostSync::CommunicationCostSync(const WeightedSBGraph& graph, PartitionMap partitions) + : ICommunicationCost(), _comm_cost(graph, partitions) { - _adjacency_matrix = vector >(_sorted_nodes.size(), vector(_sorted_nodes.size(), 0)); - - for (size_t i = 0; i < _sorted_nodes.size(); i++) { - const auto& set_piece_pointer = _sorted_nodes.at(i); - auto edges = get_set_piece_edges(set_piece_pointer.set_piece); - - for (size_t j = i + 1; j < _sorted_nodes.size(); j++) { - const auto& set_piece_pointer_j = _sorted_nodes.at(j); - auto edges2 = get_set_piece_edges(set_piece_pointer_j.set_piece); - auto shared_edges_cardinal = edges.intersection(edges2).cardinal(); - if (not edges.intersection(edges2).isEmpty()) { - logging::sbg_log << set_piece_pointer.set_piece << ", " << set_piece_pointer_j.set_piece << ": " << shared_edges_cardinal << endl; - _adjacency_matrix[set_piece_pointer.index][set_piece_pointer_j.index] = shared_edges_cardinal; - _adjacency_matrix[set_piece_pointer_j.index][set_piece_pointer.index] = shared_edges_cardinal; - } - } - } - -#ifdef SBG_PARTITIONER_LOGGING - for (size_t i = 0; i < _sorted_nodes.size(); i++) { - for (size_t j = 0; j < _sorted_nodes.size(); j++) { - cout << unsigned(_adjacency_matrix[_sorted_nodes.at(i).index][_sorted_nodes.at(j).index]) << " "; - } - cout << endl; - } -#endif } - -unsigned CommunicationCostCC::get_communication(const SBG::LIB::SetPiece& a, const SBG::LIB::SetPiece& b) const +void CommunicationCostSync::update_partitions(PartitionMap& partitions, optional>> modified_partitions) { - auto a_set = SET_FACT.createSet(a); - auto b_set = SET_FACT.createSet(b); - - unsigned a_idx = (*_set_piece_indices.image(a_set).begin())[0].begin(); - unsigned b_idx = (*_set_piece_indices.image(b_set).begin())[0].begin(); - - unsigned cost = _adjacency_matrix[a_idx][b_idx]; - - return cost; + const lock_guard lock(_mutex); + _comm_cost.update_partitions(partitions, modified_partitions); } - -unsigned CommunicationCostCC::get_communication(const SBG::LIB::SetPiece& a) const +Set CommunicationCostSync::get_ec_by_partition_id(unsigned partition_id) { - auto a_set = SET_FACT.createSet(a); - unsigned a_idx = (*_set_piece_indices.image(a_set).begin())[0].begin(); - unsigned cost = 0; - for_each(_adjacency_matrix[a_idx].cbegin(), _adjacency_matrix[a_idx].cend(), [&cost](unsigned c) { cost += c; }); - - return cost; + const lock_guard lock(_mutex); + return _comm_cost.get_ec_by_partition_id(partition_id); } - -unsigned CommunicationCostCC::get_communication(unsigned a_idx, unsigned b_idx) const +Set CommunicationCostSync::get_ec_by_interval(unsigned partition_id, const Set& nodes) { - return _adjacency_matrix[a_idx][b_idx]; + const lock_guard lock(_mutex); + return _comm_cost.get_ec_by_interval(partition_id, nodes); } - -unsigned CommunicationCostCC::get_communication(unsigned idx) const +Set CommunicationCostSync::get_ic_by_interval(unsigned partition_id, const Set& nodes) { - const auto& row = _adjacency_matrix[idx]; - - return std::accumulate(row.begin(), row.end(), 0U); // 0U ensures sum starts as an unsigned + const lock_guard lock(_mutex); + return _comm_cost.get_ic_by_interval(partition_id, nodes); } - -Set CommunicationCostCC::get_set_piece_edges(const SBG::LIB::SetPiece& nodes) +Set CommunicationCostSync::get_set_piece_edges(const Set& nodes) { - if (_communication_by_set_piece.find(nodes) == _communication_by_set_piece.end()) { - _communication_by_set_piece.insert({nodes, internal::set_piece_communication(nodes, _graph)}); - } - - return _communication_by_set_piece.at(nodes); + const lock_guard lock(_mutex); + return _comm_cost.get_set_piece_edges(nodes); } - -unsigned CommunicationCostCC::get_index(const SBG::LIB::SetPiece& nodes) const +void CommunicationCostSync::clear_communication_cache() { - return (*_set_piece_indices.image(SET_FACT.createSet(nodes)).begin())[0].begin(); -} - - + const lock_guard lock(_mutex); + return _comm_cost.clear_communication_cache(); +} + +// CommunicationCostCC::CommunicationCostCC(const WeightedSBGraph& graph, const using_cc::SetPointers& nodes) +// : _graph(graph), _sorted_nodes(nodes), _set_piece_indices(PWMap()) +// { +// initialize(); +// } + +// void CommunicationCostCC::initialize() +// { +// _adjacency_matrix = vector>(_sorted_nodes.size(), vector(_sorted_nodes.size(), 0)); + +// for (size_t i = 0; i < _sorted_nodes.size(); i++) { +// const auto& set_piece_pointer = _sorted_nodes.at(i); +// auto edges = get_set_piece_edges(set_piece_pointer.set_piece); + +// for (size_t j = i + 1; j < _sorted_nodes.size(); j++) { +// const auto& set_piece_pointer_j = _sorted_nodes.at(j); +// auto edges2 = get_set_piece_edges(set_piece_pointer_j.set_piece); +// auto shared_edges_cardinal = edges.intersection(edges2).cardinal(); +// if (not edges.intersection(edges2).isEmpty()) { +// logging::sbg_log << set_piece_pointer.set_piece << ", " << set_piece_pointer_j.set_piece << ": " << shared_edges_cardinal << +// endl; _adjacency_matrix[set_piece_pointer.index][set_piece_pointer_j.index] = shared_edges_cardinal; +// _adjacency_matrix[set_piece_pointer_j.index][set_piece_pointer.index] = shared_edges_cardinal; +// } +// } +// } + +// #ifdef SBG_PARTITIONER_LOGGING +// for (size_t i = 0; i < _sorted_nodes.size(); i++) { +// for (size_t j = 0; j < _sorted_nodes.size(); j++) { +// cout << unsigned(_adjacency_matrix[_sorted_nodes.at(i).index][_sorted_nodes.at(j).index]) << " "; +// } +// cout << endl; +// } +// #endif +// } + +// unsigned CommunicationCostCC::get_communication(const MultiDimInter& a, const MultiDimInter& b) const +// { +// auto a_set = create_set(a); +// auto b_set = create_set(b); + +// unsigned a_idx = (*_set_piece_indices.image(a_set).begin())[0].begin(); +// unsigned b_idx = (*_set_piece_indices.image(b_set).begin())[0].begin(); + +// unsigned cost = _adjacency_matrix[a_idx][b_idx]; + +// return cost; +// } + +// unsigned CommunicationCostCC::get_communication(const MultiDimInter& a) const +// { +// auto a_set = create_set(a); +// unsigned a_idx = (*_set_piece_indices.image(a_set).begin())[0].begin(); +// unsigned cost = 0; +// for_each(_adjacency_matrix[a_idx].cbegin(), _adjacency_matrix[a_idx].cend(), [&cost](unsigned c) { cost += c; }); + +// return cost; +// } + +// unsigned CommunicationCostCC::get_communication(unsigned a_idx, unsigned b_idx) const { return _adjacency_matrix[a_idx][b_idx]; } + +// unsigned CommunicationCostCC::get_communication(unsigned idx) const +// { +// const auto& row = _adjacency_matrix[idx]; + +// return std::accumulate(row.begin(), row.end(), 0U); // 0U ensures sum starts as an unsigned +// } + +// Set CommunicationCostCC::get_set_piece_edges(const SBG::LIB::Set& nodes) +// { +// if (_communication_by_set_piece.find(nodes) == _communication_by_set_piece.end()) { +// auto [edges, non_common_edges, common_edges] = internal::set_piece_communication(nodes, _graph); +// _communication_by_set_piece.insert({nodes, non_common_edges}); +// } + +// return _communication_by_set_piece.at(nodes); +// } + +// unsigned CommunicationCostCC::get_index(const SBG::LIB::Set& nodes) const +// { +// return (*_set_piece_indices.image(nodes).begin())[0].begin(); +// } CommunicationCostPtr create_communication_cost(const WeightedSBGraph& graph, PartitionMap partitions, bool multithreading_enabled) { - if (multithreading_enabled) { - return make_unique(graph, partitions); - } else { - return make_unique(graph, partitions); - } + if (multithreading_enabled) { + return make_unique(graph, partitions); + } else { + return make_unique(graph, partitions); + } } -void set_communication_cost(CommunicationCostPtr&& cost_matrix) -{ - internal::cost_matrix = move(cost_matrix); -} +void set_communication_cost(CommunicationCostPtr&& cost_matrix) { internal::cost_matrix = move(cost_matrix); } ICommunicationCost& get_communication_cost() { - assert(internal::cost_matrix); - return *internal::cost_matrix; + assert(internal::cost_matrix); + return *internal::cost_matrix; } -} +} // namespace sbg_partitioner diff --git a/algorithms/partitioner/communication_cost.hpp b/algorithms/partitioner/communication_cost.hpp index 7b7d77a4..19f4561e 100644 --- a/algorithms/partitioner/communication_cost.hpp +++ b/algorithms/partitioner/communication_cost.hpp @@ -23,6 +23,7 @@ #include #include +#include #include #include "sbg_partitioner_types.hpp" @@ -33,8 +34,8 @@ namespace sbg_partitioner { struct SetPieceHash { SetPieceHash() = default; - static std::size_t set_piece_hash(const SBG::LIB::SetPiece& set_piece); - std::size_t operator()(const SBG::LIB::SetPiece& set_piece) const; + static std::size_t set_piece_hash(const SBG::LIB::detail::MultiDimInter& set_piece); + std::size_t operator()(const SBG::LIB::detail::MultiDimInter& set_piece) const; }; struct SetHash { @@ -76,7 +77,7 @@ class ICommunicationCost { * @note the member function is non-const since _ec_cost_by_interval and _ic_cost_by_interval may be updated to prevent to be recomputed. */ - virtual SBG::LIB::Set get_ec_by_interval(unsigned partition_id, const SBG::LIB::SetPiece& nodes) = 0; + virtual SBG::LIB::Set get_ec_by_interval(unsigned partition_id, const SBG::LIB::Set& nodes) = 0; /** * It returns the edges that communicate the set piece nodes with other nodes in partition `partition_id`. @@ -87,10 +88,10 @@ class ICommunicationCost { * @return Internal edges of the given set piece. * @note the member function is non-const since _ec_cost_by_interval and _ic_cost_by_interval may be updated to prevent to be recomputed. */ - virtual SBG::LIB::Set get_ic_by_interval(unsigned partition_id, const SBG::LIB::SetPiece& nodes) = 0; + virtual SBG::LIB::Set get_ic_by_interval(unsigned partition_id, const SBG::LIB::Set& nodes) = 0; - virtual SBG::LIB::Set get_set_piece_edges(const SBG::LIB::SetPiece& nodes) = 0; + virtual SBG::LIB::Set get_set_piece_edges(const SBG::LIB::Set& nodes) = 0; virtual void clear_communication_cache() = 0; }; @@ -112,11 +113,11 @@ class CommunicationCost : public ICommunicationCost { SBG::LIB::Set get_ec_by_partition_id(unsigned partition_id) override; // non-const since _cost_by_partition may be updated - SBG::LIB::Set get_ec_by_interval(unsigned partition_id, const SBG::LIB::SetPiece& nodes) override; + SBG::LIB::Set get_ec_by_interval(unsigned partition_id, const SBG::LIB::Set& nodes) override; - SBG::LIB::Set get_ic_by_interval(unsigned partition_id, const SBG::LIB::SetPiece& nodes) override; + SBG::LIB::Set get_ic_by_interval(unsigned partition_id, const SBG::LIB::Set& nodes) override; - SBG::LIB::Set get_set_piece_edges(const SBG::LIB::SetPiece& nodes) override; + SBG::LIB::Set get_set_piece_edges(const SBG::LIB::Set& nodes) override; void clear_communication_cache() override; @@ -125,13 +126,18 @@ class CommunicationCost : public ICommunicationCost { PartitionMap _partitions; // since communication is independent from the partitions, we can share it between many objects - static std::unordered_map _communication_by_set_piece; + static std::unordered_map _communication_by_set_piece; + static std::unordered_map _communication_by_set_piece_with_common_edges; + static std::unordered_map _inner_edges; std::vector> _cost_by_partition; - std::vector> _ec_cost_by_interval; - std::vector> _ic_cost_by_interval; + std::vector> _ec_cost_by_interval; + std::vector> _ic_cost_by_interval; void initialize(); - std::pair compute_ec_ic(unsigned partition_id, const SBG::LIB::SetPiece& nodes); + + std::pair get_part_communication(unsigned id); + + std::pair compute_ec_ic(unsigned partition_id, const SBG::LIB::Set& nodes); }; @@ -148,11 +154,11 @@ class CommunicationCostSync : public ICommunicationCost { SBG::LIB::Set get_ec_by_partition_id(unsigned partition_id) override; // non-const since _cost_by_partition may be updated - SBG::LIB::Set get_ec_by_interval(unsigned partition_id, const SBG::LIB::SetPiece& nodes) override; + SBG::LIB::Set get_ec_by_interval(unsigned partition_id, const SBG::LIB::Set& nodes) override; - SBG::LIB::Set get_ic_by_interval(unsigned partition_id, const SBG::LIB::SetPiece& nodes) override; + SBG::LIB::Set get_ic_by_interval(unsigned partition_id, const SBG::LIB::Set& nodes) override; - SBG::LIB::Set get_set_piece_edges(const SBG::LIB::SetPiece& nodes) override; + SBG::LIB::Set get_set_piece_edges(const SBG::LIB::Set& nodes) override; void clear_communication_cache() override; @@ -162,38 +168,38 @@ class CommunicationCostSync : public ICommunicationCost { }; -class CommunicationCostCC { -public: - CommunicationCostCC(const SBG::LIB::WeightedSBGraph& graph, const using_cc::SetPointers& nodes); +// class CommunicationCostCC { +// public: +// CommunicationCostCC(const SBG::LIB::WeightedSBGraph& graph, const using_cc::SetPointers& nodes); - ~CommunicationCostCC() = default; +// ~CommunicationCostCC() = default; - unsigned get_communication(const SBG::LIB::SetPiece& a, const SBG::LIB::SetPiece& b) const; - unsigned get_communication(unsigned a_idx, unsigned b_idx) const; - unsigned get_communication(unsigned idx) const; - unsigned get_communication(const SBG::LIB::SetPiece& a) const; +// unsigned get_communication(const SBG::LIB::Set& a, const SBG::LIB::Set& b) const; +// unsigned get_communication(unsigned a_idx, unsigned b_idx) const; +// unsigned get_communication(unsigned idx) const; +// unsigned get_communication(const SBG::LIB::Set& a) const; - SBG::LIB::Set get_set_piece_edges(const SBG::LIB::SetPiece& nodes); +// SBG::LIB::Set get_set_piece_edges(const SBG::LIB::Set& nodes); - unsigned get_index(const SBG::LIB::SetPiece& nodes) const; +// unsigned get_index(const SBG::LIB::Set& nodes) const; -private: - void initialize(); +// private: +// void initialize(); - using AdjacencyMatrix = std::vector>; +// using AdjacencyMatrix = std::vector>; - const SBG::LIB::WeightedSBGraph& _graph; // read-only members +// const SBG::LIB::WeightedSBGraph& _graph; // read-only members - using_cc::SetPointers _sorted_nodes; - SBG::LIB::PWMap _set_piece_indices; - AdjacencyMatrix _adjacency_matrix; - std::unordered_map _communication_by_set_piece; +// using_cc::SetPointers _sorted_nodes; +// SBG::LIB::PWMap _set_piece_indices; +// AdjacencyMatrix _adjacency_matrix; +// std::unordered_map _communication_by_set_piece; - std::vector> _cost_by_partition; - std::vector> _ec_cost_by_interval; - std::vector> _ic_cost_by_interval; +// std::vector> _cost_by_partition; +// std::vector> _ec_cost_by_interval; +// std::vector> _ic_cost_by_interval; -}; +// }; CommunicationCostPtr create_communication_cost(const SBG::LIB::WeightedSBGraph& graph, PartitionMap partitions, bool multithreading_enabled); diff --git a/algorithms/partitioner/dfs_on_sbg.cpp b/algorithms/partitioner/dfs_on_sbg.cpp index e6ce17a9..9b0b29e4 100644 --- a/algorithms/partitioner/dfs_on_sbg.cpp +++ b/algorithms/partitioner/dfs_on_sbg.cpp @@ -19,6 +19,9 @@ #include +#include +#include + #include "dfs_on_sbg.hpp" #include "sbg_partitioner_log.hpp" #include "weighted_sb_graph.hpp" @@ -26,6 +29,9 @@ using namespace std; using namespace SBG::LIB; +using SBG::LIB::detail::OrderedSet; +using SBG::LIB::detail::SetAccess; +using SBG::LIB::detail::SetAccessKey; using namespace SBG::Util; namespace sbg_partitioner { @@ -43,33 +49,38 @@ void initialize_partitioning(SBG::LIB::WeightedSBGraph& graph, unsigned number_o void add_strategy(PartitionStrategyPtr&& strategy, bool pre_order) { sort_object.add_partition_strategy(move(strategy), pre_order); } -vector>> partitionate() +vector>> partitionate() { assert(initialized and "Partioning was not inialized"); sort_object.start(); sort_object.iterate(); - vector>> partitions = sort_object.partitions(); + vector>> partitions = sort_object.partitions(); return partitions; } // This is just a dirty trick to create an empty object -DFS::DFS() : _nodes(SET_FACT.createSet()) {} +DFS::DFS() : _nodes(Set()) {} DFS::DFS(SBG::LIB::WeightedSBGraph& graph, unsigned number_of_partitions) - : _number_of_partitions(number_of_partitions), _root_node_idx(nullopt), _graph(&graph), _nodes(graph.V()) + : _number_of_partitions(number_of_partitions), + _root_node_idx(nullopt), + _graph(&graph), + _nodes(graph.V()), + _node_pieces(SetAccess::key().pieces(get(SetAccess::key().impl(graph.V())))) { initialize_adjacents(); } void DFS::initialize_adjacents() { + SetAccessKey key = SetAccess::key(); // Fill adjacents - for (auto it = _nodes.begin(); it != _nodes.end(); ++it) { + for (auto it = _node_pieces.begin(); it != _node_pieces.end(); ++it) { const auto incoming_node = *it; - auto incoming_node_set = SET_FACT.createSet(incoming_node); + auto incoming_node_set = key.createSet(OrderedSet(incoming_node)); auto pre_im_1 = _graph->map1().preImage(incoming_node_set); auto adjacent_nodes_1 = _graph->map2().image(pre_im_1); @@ -79,10 +90,10 @@ void DFS::initialize_adjacents() auto adjacent_nodes = adjacent_nodes_1.cup(adjacent_nodes_2).difference(incoming_node_set); - for (auto it_2 = _nodes.begin(); it_2 != _nodes.end(); ++it_2) { + for (auto it_2 = _node_pieces.begin(); it_2 != _node_pieces.end(); ++it_2) { if (it_2 != it) { const auto potential_arriving_node = *it_2; - const auto potential_arriving_node_set = SET_FACT.createSet(potential_arriving_node); + const auto potential_arriving_node_set = key.createSet(OrderedSet(potential_arriving_node)); if (not potential_arriving_node_set.intersection(adjacent_nodes).isEmpty()) { _adjacent[it].insert(it_2); } @@ -91,12 +102,12 @@ void DFS::initialize_adjacents() } // Choosing root node - _root_node_idx = _nodes.begin(); + _root_node_idx = _node_pieces.begin(); auto current_max_adj_values = _adjacent[*_root_node_idx].size(); - auto it = _nodes.begin(); + auto it = _node_pieces.begin(); ++it; - for (; it != _nodes.end(); ++it) { + for (; it != _node_pieces.end(); ++it) { if (_adjacent[it].size() > current_max_adj_values) { _root_node_idx = it; current_max_adj_values = _adjacent[*_root_node_idx].size(); @@ -108,14 +119,15 @@ void DFS::initialize_adjacents() void DFS::add_adjacent_nodes(const node_identifier id, const PWMap& map, const Set& edge) { - const auto edge_map_intersection = edge.intersection(map.dom()); + SetAccessKey key = SetAccess::key(); + const auto edge_map_intersection = edge.intersection(map.domain()); if (not edge_map_intersection.isEmpty()) { const auto map_image = map.image(edge_map_intersection); - for (auto it = _nodes.begin(); it != _nodes.end(); ++it) { + for (auto it = _node_pieces.begin(); it != _node_pieces.end(); ++it) { if (it != id) { const auto potential_arriving_node = *it; - const auto potential_arriving_node_set = SET_FACT.createSet(potential_arriving_node); + const auto potential_arriving_node_set = key.createSet(OrderedSet(potential_arriving_node)); if (potential_arriving_node_set.intersection(map_image) == potential_arriving_node_set) { _adjacent[id].insert(it); } @@ -155,9 +167,9 @@ void DFS::iterate() // At this point, _partially_visited is empty, that means all nodes connected to // the root node were visited. Let's see if there is any isolated node. - finished = _visited.size() == _nodes.size(); + finished = _visited.size() == _node_pieces.size(); if (not finished) { - for (node_identifier i = _nodes.begin(); i != _nodes.end(); ++i) { + for (node_identifier i = _node_pieces.begin(); i != _node_pieces.end(); ++i) { if (find(_visited.begin(), _visited.end(), i) == _visited.end()) { add_it_partially(i); break; @@ -186,9 +198,9 @@ void DFS::fill_current_node_stack() } } -vector>> DFS::partitions() const +vector>> DFS::partitions() const { - vector>> partitions; + vector>> partitions; for (size_t i = 0; i < _partition_strategy_pre_order.size(); i++) { partitions.push_back(_partition_strategy_pre_order[i]->partitions()); @@ -227,7 +239,8 @@ void DFS::add_it_definitely(node_identifier id) void DFS::add_it_to_a_partition(node_identifier id, bool pre_order) { - auto v = *id; + SetAccessKey key = SetAccess::key(); + auto v = key.createSet(OrderedSet(*id)); if (pre_order) { for (size_t i = 0; i < _partition_strategy_pre_order.size(); i++) { (*_partition_strategy_pre_order[i])(v); diff --git a/algorithms/partitioner/dfs_on_sbg.hpp b/algorithms/partitioner/dfs_on_sbg.hpp index 6ad57024..12976310 100644 --- a/algorithms/partitioner/dfs_on_sbg.hpp +++ b/algorithms/partitioner/dfs_on_sbg.hpp @@ -25,6 +25,7 @@ #include #include +#include #include #include "partition_strategy.hpp" @@ -41,7 +42,7 @@ void initialize_partitioning(SBG::LIB::WeightedSBGraph& graph, unsigned number_o void add_strategy(PartitionStrategyPtr&& strategy, bool pre_order); -std::vector>> partitionate(); +std::vector>> partitionate(); class DFS { @@ -62,13 +63,13 @@ class DFS { void iterate(); - std::vector>> partitions() const; + std::vector>> partitions() const; /// @note strategy object should live while this class does void add_partition_strategy(PartitionStrategyPtr&& strategy, bool pre_order); private: - typedef SBG::LIB::Set::Iterator node_identifier; + typedef std::vector::const_iterator node_identifier; unsigned _number_of_partitions; @@ -82,6 +83,7 @@ class DFS { SBG::LIB::WeightedSBGraph* _graph; SBG::LIB::Set _nodes; + std::vector _node_pieces; std::vector _partition_strategy_pre_order; std::vector _partition_strategy_post_order; diff --git a/algorithms/partitioner/kernighan_lin_partitioner.cpp b/algorithms/partitioner/kernighan_lin_partitioner.cpp index 02045576..489bfc27 100644 --- a/algorithms/partitioner/kernighan_lin_partitioner.cpp +++ b/algorithms/partitioner/kernighan_lin_partitioner.cpp @@ -16,12 +16,15 @@ ******************************************************************************/ +#include #include +#include #include #include #include -#include +#include +#include #include "build_sb_graph.hpp" #include "communication_cost.hpp" @@ -29,12 +32,18 @@ #include "sbg_partitioner_log.hpp" #define PARTITION_IMBALANCE_DEBUG 0 +#define TEST_EDGE_CUT 0 // This code is based on https://github.com/CIFASIS/sbg-partitioner/discussions/17 using namespace std; using namespace SBG::LIB; +using SBG::LIB::detail::Interval; +using SBG::LIB::detail::MultiDimInter; +using SBG::LIB::detail::OrderedSet; +using SBG::LIB::detail::SetAccess; +using SBG::LIB::detail::SetAccessKey; using namespace SBG::Util; namespace sbg_partitioner { @@ -44,63 +53,80 @@ using ec_ic = std::pair; // Using unnamed namespace to define functions with internal linkage namespace { -pair compute_lmin_lmax(const WeightedSBGraph& graph, unsigned number_of_partitions, const float imbalance_epsilon) +pair compute_lmin_lmax(const WeightedSBGraph& graph, unsigned number_of_partitions, const float imbalance_epsilon) { - unsigned w_v = get_node_size(graph.V(), graph.get_node_weights()); - unsigned B = ceil(w_v / number_of_partitions); - int im = imbalance_epsilon * B; - unsigned LMin = B - im; - unsigned LMax = B + im; + size_t w_v = get_node_size(graph.V(), graph.get_node_weights()); + size_t B = ceil(w_v / number_of_partitions); + long int im = imbalance_epsilon * B; + size_t LMin = B - im; + size_t LMax = B + im; return make_pair(LMin, LMax); } -pair generate_gain_matrix(const WeightedSBGraph& graph, ICommunicationCost& cost_matrix, unsigned partition_a_id, - Partition& partition_a, unsigned partition_b_id, Partition& partition_b, unsigned LMin, - unsigned LMax) +GainObjectImbalance generate_gain_object(int a_idx, unsigned partition_a_id, Partition partition_a, int b_idx, unsigned partition_b_id, + Partition partition_b, ICommunicationCost& comm) { - SBG::Util::Internal::TimeProfiler profiler("generate_gain_matrix"); - // create the max_gain object with a dummy initialization, any gain will be greater than -infinity - optional max_gain = nullopt; - CostMatrixImbalance local_cost_matrix; + auto get_edges_by_set = [](const auto& set, auto fun) { + auto comm_aff = Set(); + comm_aff = comm_aff.cup(fun(set)); + // for_each(set.begin(), set.end(), [&comm_aff, &fun](const auto sp) { comm_aff = comm_aff.cup(fun(sp)); }); - for (size_t i = 0; i < partition_a.size(); i++) { - for (size_t j = 0; j < partition_b.size(); j++) { - auto set_i_a = partition_a.at(i); - auto set_j_b = partition_b.at(j); - if (set_i_a.cardinal() != set_j_b.cardinal()) { - unsigned size = min(set_j_b.cardinal(), set_i_a.cardinal()); - set_i_a = cut_interval(set_i_a, set_i_a.begin()->begin() + size - 1).first; - set_j_b = cut_interval(set_j_b, set_j_b.begin()->begin() + size - 1).first; - } + return comm_aff; + }; - auto ic_i_a = cost_matrix.get_ic_by_interval(partition_a_id, set_i_a); - auto ec_i_a = cost_matrix.get_ec_by_interval(partition_a_id, set_i_a); - ec_i_a = ec_i_a.intersection(cost_matrix.get_ec_by_partition_id(partition_b_id)); + // auto& comm = get_communication_cost(); + auto set_a = partition_a.at(a_idx); + auto set_b = partition_b.at(b_idx); + if (set_a.cardinal() != set_b.cardinal()) { + unsigned size = min(set_b.cardinal(), set_a.cardinal()); + set_a = cut_interval_by_dimension(set_a, {}, size).first; + set_b = cut_interval_by_dimension(set_b, {}, size).first; + } - logging::sbg_log << "Node " << i << ", " << set_i_a << " ec: " << ec_i_a << " and ic: " << ic_i_a << endl; + auto ic_a = get_edges_by_set(set_a, [&partition_a_id, &comm](const auto& sp) { return comm.get_ic_by_interval(partition_a_id, sp); }); + auto ec_a = get_edges_by_set(set_a, [&partition_a_id, &comm](const auto& sp) { return comm.get_ec_by_interval(partition_a_id, sp); }); + ec_a = ec_a.intersection(comm.get_ec_by_partition_id(partition_b_id)); - auto ic_j_b = cost_matrix.get_ic_by_interval(partition_b_id, set_j_b); - auto ec_j_b = cost_matrix.get_ec_by_interval(partition_b_id, set_j_b); - ec_j_b = ec_j_b.intersection(cost_matrix.get_ec_by_partition_id(partition_a_id)); + logging::sbg_log << "Node " << a_idx << ", " << set_a << " ec: " << ec_a << " and ic: " << ic_a << endl; - logging::sbg_log << "Node " << j << ", " << set_j_b << " ec: " << ec_j_b << " and ic: " << ic_j_b << endl; + auto ic_b = get_edges_by_set(set_b, [&partition_b_id, &comm](const auto& sp) { return comm.get_ic_by_interval(partition_b_id, sp); }); + auto ec_b = get_edges_by_set(set_b, [&partition_b_id, &comm](const auto& sp) { return comm.get_ec_by_interval(partition_b_id, sp); }); + ec_b = ec_b.intersection(comm.get_ec_by_partition_id(partition_a_id)); - auto ec_edges = ec_i_a.cup(ec_j_b).difference(ec_i_a.intersection(ec_j_b)); // disjointCup does not seem to be working - auto ic_edges = ic_i_a.cup(ic_j_b); + logging::sbg_log << "Node " << b_idx << ", " << set_b << " ec: " << ec_b << " and ic: " << ic_b << endl; - int gain = ec_edges.cardinal() - ic_edges.cardinal(); - local_cost_matrix.emplace_back(i, j, gain, ec_i_a, ic_i_a, set_i_a.cardinal(), ec_j_b, ic_j_b, set_j_b.cardinal()); + auto ec_edges = ec_a.cup(ec_b).difference(ec_a.intersection(ec_b)); // disjointCup does not seem to be working + auto ic_edges = ic_a.cup(ic_b); + + int gain = ec_edges.cardinal() - ic_edges.cardinal(); + return GainObjectImbalance(a_idx, b_idx, gain, ec_a, ic_a, set_a.cardinal(), ec_b, ic_b, set_b.cardinal()); +} + +pair generate_gain_matrix(const WeightedSBGraph& graph, ICommunicationCost& cost_matrix, + unsigned partition_a_id, Partition& partition_a, + unsigned partition_b_id, Partition& partition_b, unsigned LMin, + unsigned LMax) +{ + SBG::Util::Internal::TimeProfiler profiler("generate_gain_matrix"); + // create the max_gain object with a dummy initialization, any gain will be greater than -infinity + optional max_gain = nullopt; + CostMatrixImbalance local_cost_matrix; + + for (size_t i = 0; i < partition_a.size(); i++) { + for (size_t j = 0; j < partition_b.size(); j++) { + auto gain_obj = generate_gain_object(i, partition_a_id, partition_a, j, partition_b_id, partition_b, get_communication_cost()); + local_cost_matrix.push_back(move(gain_obj)); if ((not max_gain) or local_cost_matrix.back().gain > max_gain->gain) { - max_gain = local_cost_matrix.back(); + max_gain = local_cost_matrix.back(); } } } assert(max_gain); - return { *max_gain, local_cost_matrix }; + return {*max_gain, local_cost_matrix}; } // Partition a and b (A_c and B_c in the definition) are the remining nodes to be visited, not the actual partitions @@ -108,19 +134,19 @@ pair, pair> update_sets(Partition& partition_a, Partiti Set& current_moved_partition_b, const GainObjectImbalance& gain_object, const WeightedSBGraph& graph) { - auto node_a = SET_FACT.createSet(partition_a[gain_object.a_idx]); + auto node_a = partition_a[gain_object.a_idx]; size_t partition_size_a = get_node_size(node_a, graph.get_node_weights()); bool node_a_is_fully_used = partition_size_a == gain_object.size_a; - Set rest_a = SET_FACT.createSet(); + Set rest_a; if (not node_a_is_fully_used) { tie(node_a, rest_a) = cut_interval_by_dimension(node_a, graph.get_node_weights(), gain_object.size_a); logging::sbg_log << "cut_interval_by_dimension " << gain_object.size_a << ": " << node_a << rest_a << endl; } - auto node_b = SET_FACT.createSet(partition_b[gain_object.b_idx]); + auto node_b = partition_b[gain_object.b_idx]; size_t partition_size_b = get_node_size(node_b, graph.get_node_weights()); bool node_b_is_fully_used = partition_size_b == gain_object.size_b; - Set rest_b = SET_FACT.createSet(); + Set rest_b; if (not node_b_is_fully_used) { tie(node_b, rest_b) = cut_interval_by_dimension(node_b, graph.get_node_weights(), gain_object.size_b); logging::sbg_log << "cut_interval_by_dimension " << gain_object.size_b << ": " << node_b << rest_b << endl; @@ -128,13 +154,18 @@ pair, pair> update_sets(Partition& partition_a, Partiti auto update_partition = [](Partition& partition, const Set& set) { for (Partition::iterator it = partition.begin(); it != partition.end(); ++it) { - auto set_p = SET_FACT.createSet(*it); + auto set_p = *it; if (not set_p.intersection(set).isEmpty()) { set_p = set_p.difference(set); if (set_p.isEmpty()) { partition.erase(it); } else { - *it = *set_p.begin(); + *it = set_p; + // if (set_p.size() > 1) { + // Set::Iterator next_it = set_p.begin(); + // ++next_it; + // partition.push_back(*next_it); + // } } break; } @@ -153,32 +184,50 @@ pair, pair> update_sets(Partition& partition_a, Partiti return make_pair(make_pair(node_a, rest_a), make_pair(node_b, rest_b)); } -GainObjectImbalance update_diff(CostMatrixImbalance& cost_matrix, Partition& remaining_partition_a, Set& moved_from_partition_a, - pair affected_node_a, Partition& remaining_partition_b, Set& moved_from_partition_b, - pair affected_node_b, const WeightedSBGraph& graph, const NodeWeight& node_weight, - const GainObjectImbalance& gain_object, unsigned LMin, unsigned LMax) +Set get_set_comm(const Set& s, const WeightedSBGraph& graph) +{ + auto d1 = graph.map1().preImage(s); + auto d2 = graph.map2().preImage(s); + auto d = d1.cup(d2).difference(d1.intersection(d2)); + return d; +} + +Set get_part_internal_comm(const Set& s, const WeightedSBGraph& graph) +{ + auto d1 = graph.map1().preImage(s); + auto im1 = graph.map2().image(d1); + auto internal_im1 = im1.intersection(s); + auto e1 = graph.map2().preImage(internal_im1); + e1 = e1.intersection(d1); + + auto d2 = graph.map2().preImage(s); + auto im2 = graph.map1().image(d2); + auto internal_im2 = im2.intersection(s); + auto e2 = graph.map1().preImage(internal_im2); + e2 = e2.intersection(d2); + + return e1.cup(e2); +} + +GainObjectImbalance update_diff(CostMatrixImbalance& cost_matrix, const Partition& remaining_partition_a, const Set& moved_from_partition_a, + pair affected_node_a, const Partition& remaining_partition_b, const Set& moved_from_partition_b, + pair affected_node_b, const WeightedSBGraph& graph, const NodeWeight& node_weight, + const GainObjectImbalance& gain_object, const Set& ic_part_a, const Set& ic_part_b, const Set& ec, + unsigned LMin, unsigned LMax) { SBG::Util::Internal::TimeProfiler profiler("update_diff"); logging::sbg_log << affected_node_a.first << ", " << affected_node_a.second << endl; logging::sbg_log << affected_node_b.first << ", " << affected_node_b.second << endl; - if (cost_matrix.empty()) { + if (cost_matrix.empty() and (remaining_partition_a.empty() or remaining_partition_b.empty())) { logging::sbg_log << "Cost matrix is empty, nothing to update." << endl; + logging::sbg_log << remaining_partition_a << ", " << remaining_partition_b << endl; return gain_object; } // Firstly, check if indexes need fixing. Three possible causes. - size_t affected_node_a_size = get_node_size(affected_node_a.second, node_weight); - bool node_a_fully_used = affected_node_a_size == 0; - - size_t affected_node_b_size = get_node_size(affected_node_b.second, node_weight); - bool node_b_fully_used = affected_node_b_size == 0; - - unsigned size_a = get_partition_size(remaining_partition_a, node_weight); - size_a += get_node_size(moved_from_partition_b, node_weight); - - unsigned size_b = get_partition_size(remaining_partition_b, node_weight); - size_b += get_node_size(moved_from_partition_a, node_weight); + bool node_a_fully_used = affected_node_a.second.cardinal() == 0; + bool node_b_fully_used = affected_node_b.second.cardinal() == 0; // all the interval was used // fix indexes: @@ -207,54 +256,76 @@ GainObjectImbalance update_diff(CostMatrixImbalance& cost_matrix, Partition& rem } // now, update ic and ec according to the last changes - auto affected_nodes = affected_node_a.first.cup(affected_node_b.first); - auto discarded_edges = graph.map1().preImage(affected_nodes).cup(graph.map2().preImage(affected_nodes)); CostMatrixImbalance new_cost_matrix; - if (cost_matrix.empty()) { - logging::sbg_log << "After updating cost matrix is empty, nothing to update." << endl; - return gain_object; - } + // if (cost_matrix.empty()) { + // logging::sbg_log << "After updating cost matrix is empty, nothing to update." << endl; + // return gain_object; + // } // using a reference to copy the element only once when returning optional max_gain_object = nullopt; for (auto g : cost_matrix) { - bool change = false; + if (((not node_a_fully_used) and g.a_idx == gain_object.a_idx) or ((not node_b_fully_used) and g.b_idx == gain_object.b_idx)) { + // node a was not fully used so we need to update ec and ic + + auto set_a = remaining_partition_a.at(g.a_idx); + auto set_b = remaining_partition_b.at(g.b_idx); + if (set_a.cardinal() != set_b.cardinal()) { + size_t size = min(set_a.cardinal(), set_b.cardinal()); + set_a = cut_interval_by_dimension(set_a, graph.get_node_weights(), size).first; + set_b = cut_interval_by_dimension(set_b, graph.get_node_weights(), size).first; + } - if ((not node_a_fully_used) and g.a_idx == gain_object.a_idx) { - auto new_size = min(affected_node_a_size, g.size_b); - g.size_a = new_size; - g.size_b = new_size; - change = true; - } + auto ic_a = get_set_comm(set_a, graph).intersection(ic_part_a); + auto ec_a = get_set_comm(set_a, graph).intersection(ec); - if ((not node_b_fully_used) and g.b_idx == gain_object.b_idx) { - auto new_size = min(affected_node_b_size, g.size_a); - g.size_a = new_size; - g.size_b = new_size; - change = true; - } + auto ic_b = get_set_comm(set_b, graph).intersection(ic_part_b); + auto ec_b = get_set_comm(set_b, graph).intersection(ec); - if (not g.ic_nodes_a.intersection(discarded_edges).isEmpty() or not g.ec_nodes_a.intersection(discarded_edges).isEmpty()) { - g.ic_nodes_a = g.ic_nodes_a.difference(discarded_edges); - g.ec_nodes_a = g.ec_nodes_a.difference(discarded_edges); - change = true; - } + auto ec_edges = ec_a.cup(ec_b).difference(ec_a.intersection(ec_b)); // disjointCup does not seem to be working + auto ic_edges = ic_a.cup(ic_b); - if (not g.ic_nodes_b.intersection(discarded_edges).isEmpty() or not g.ec_nodes_b.intersection(discarded_edges).isEmpty()) { - g.ic_nodes_b = g.ic_nodes_b.difference(discarded_edges); - g.ec_nodes_b = g.ec_nodes_b.difference(discarded_edges); - change = true; + int gain = ec_edges.cardinal() - ic_edges.cardinal(); + g = GainObjectImbalance(g.a_idx, g.b_idx, gain, ec_a, ic_a, set_a.cardinal(), ec_b, ic_b, set_b.cardinal()); + new_cost_matrix.push_back(move(g)); + if ((not max_gain_object) or new_cost_matrix.back().gain > max_gain_object->gain) { + max_gain_object = new_cost_matrix.back(); + } + continue; } - if (change) { - auto ec_nodes = g.ec_nodes_a.cup(g.ec_nodes_b).difference(g.ec_nodes_a.intersection(g.ec_nodes_b)); - auto ic_nodes = g.ic_nodes_a.cup(g.ic_nodes_b); + // recompute gain + auto get_edges_by_set = [&graph](const auto& set) { + auto& comm = get_communication_cost(); + auto comm_aff = comm.get_set_piece_edges(set); + // for_each(set.begin(), set.end(), [&comm_aff, &comm](const auto sp) { comm_aff = comm_aff.cup(comm.get_set_piece_edges(sp)); }); - // calculate gain - int gain = ec_nodes.cardinal() - ic_nodes.cardinal(); - g.gain = gain; - } + return comm_aff; + }; + + auto comm_aff_a = get_edges_by_set(affected_node_a.first); + auto comm_aff_b = get_edges_by_set(affected_node_b.first); + + // cout << "comm a: " << affected_node_a.first << " " << comm_aff_a << endl; + // cout << "comm b: " << affected_node_b.first << " " << comm_aff_b << endl; + + auto a = remaining_partition_a.at(g.a_idx); + auto set_g_a = cut_interval_by_dimension(a, graph.get_node_weights(), g.size_a).first; + auto comm_a = get_edges_by_set(set_g_a); + // cout << "actual comm of" << set_g_a << ": " << comm_a << endl; + g.ec_nodes_a = g.ec_nodes_a.difference(comm_aff_b).cup(comm_a.intersection(comm_aff_a)); + g.ic_nodes_a = g.ic_nodes_a.difference(comm_aff_a).cup(comm_a.intersection(comm_aff_b)); + + auto b = remaining_partition_b.at(g.b_idx); + auto set_g_b = cut_interval_by_dimension(b, graph.get_node_weights(), g.size_b).first; + auto comm_b = get_edges_by_set(set_g_b); + // cout << "actual comm of "<< set_g_b << ": " << comm_b << endl; + g.ec_nodes_b = g.ec_nodes_b.difference(comm_aff_a).cup(comm_b.intersection(comm_aff_b)); + g.ic_nodes_b = g.ic_nodes_b.difference(comm_aff_b).cup(comm_b.intersection(comm_aff_a)); + + auto ec_nodes = g.ec_nodes_a.cup(g.ec_nodes_b).difference(g.ec_nodes_a.intersection(g.ec_nodes_b)); + g.gain = ec_nodes.cardinal() - g.ic_nodes_a.cup(g.ic_nodes_b).cardinal(); new_cost_matrix.push_back(move(g)); @@ -264,16 +335,20 @@ GainObjectImbalance update_diff(CostMatrixImbalance& cost_matrix, Partition& rem } cost_matrix = new_cost_matrix; + if (cost_matrix.empty()) { + logging::sbg_log << "After updating cost matrix is empty, nothing to update." << endl; + return gain_object; + } + assert(max_gain_object); #if PARTITION_IMBALANCE_DEBUG logging::sbg_log << remaining_partition_a << ", " << remaining_partition_b << ", " << gain_object << ", " << cost_matrix << endl; #endif - return *max_gain_object; + return *max_gain_object; } - void update_sum(int& par_sum, int g, int& max_par_sum, pair& max_par_sum_set, const Set& a_v, const Set& b_v) { par_sum += g; @@ -283,41 +358,121 @@ void update_sum(int& par_sum, int g, int& max_par_sum, pair& max_par_s } } +Set get_me_edgecut(const Partition& partition_a, const Partition& partition_b, const WeightedSBGraph& graph) +{ + auto d = graph.map1().preImage(from_vector(partition_a)); + auto im = graph.map2().image(d); + auto ec_nodes = im.intersection(from_vector(partition_b)); + auto external_communication = graph.map2().preImage(ec_nodes); + // avoid oversizing external communication + external_communication = external_communication.intersection(d); + + auto d2 = graph.map2().preImage(from_vector(partition_a)); + auto im2 = graph.map1().image(d2); + auto ec_nodes2 = im2.intersection(from_vector(partition_b)); + auto external_communication2 = graph.map1().preImage(ec_nodes2); + // avoid oversizing external communication + external_communication2 = external_communication2.intersection(d2); + + external_communication = external_communication.cup(external_communication2); + + return external_communication; +} int kl_sbg_imbalance(const WeightedSBGraph& graph, ICommunicationCost& cost_matrix, unsigned partition_a_id, Partition& partition_a, unsigned partition_b_id, Partition& partition_b, unsigned LMin, unsigned LMax) { + SetAccessKey key = SetAccess::key(); #if PARTITION_IMBALANCE_DEBUG logging::sbg_log << "Algorithm starts with " << partition_a << ", " << partition_b << endl; #endif auto a_c = partition_a; auto b_c = partition_b; int max_par_sum = 0; - auto max_par_sum_set = make_pair(SET_FACT.createSet(), SET_FACT.createSet()); + auto max_par_sum_set = make_pair(Set(), Set()); int par_sum = 0; - Set a_v = SET_FACT.createSet(); - Set b_v = SET_FACT.createSet(); + Set a_v; + Set b_v; const auto node_weights = graph.get_node_weights(); auto [g, gm] = generate_gain_matrix(graph, cost_matrix, partition_a_id, partition_a, partition_b_id, partition_b, LMin, LMax); + auto& comm = get_communication_cost(); + auto ic_a = get_part_internal_comm(from_vector(partition_a), graph); + auto ic_b = get_part_internal_comm(from_vector(partition_b), graph); + auto ec = comm.get_ec_by_partition_id(partition_a_id).intersection(comm.get_ec_by_partition_id(partition_b_id)); + #if PARTITION_IMBALANCE_DEBUG logging::sbg_log << LMin << ", " << LMax << gm << endl; #endif +#if TEST_EDGE_CUT + int prev_edge_cut = get_me_edgecut(partition_a, partition_b, graph).cardinal(); + logging::sbg_log << "previous edge cut: " << prev_edge_cut << "\nparts: " << partition_a << " , " << partition_b << endl; +#endif + while ((not a_c.empty()) and (not b_c.empty())) { - logging::sbg_log << "inside the while " << a_c << ", " << b_c << " "; + logging::sbg_log << "inside the while " << a_c << " -- " << b_c << " "; logging::sbg_log << get_partition_size(a_c, node_weights) << ", " << get_partition_size(b_c, node_weights) << endl; logging::sbg_log << gm << endl; + if (gm.empty()) { + cout << "Gain matrix is empty but parts have not been exausted" << endl; + break; + } + assert(not gm.empty()); logging::sbg_log << g << endl; - pair a_ = {SET_FACT.createSet(), SET_FACT.createSet()}, b_ = {SET_FACT.createSet(), SET_FACT.createSet()}; + pair a_ = {Set(), Set()}, b_ = {Set(), Set()}; tie(a_, b_) = update_sets(a_c, b_c, a_v, b_v, g, graph); - auto new_max_gain = update_diff(gm, a_c, a_v, a_, b_c, b_v, b_, graph, node_weights, g, LMin, LMax); + + // update part comm + auto edges_moved_from_a = get_set_comm(a_.first, graph); + auto ic_moved_from_a = ic_a.intersection(edges_moved_from_a); + auto ec_moved_from_a = ec.intersection(edges_moved_from_a); + + auto edges_moved_from_b = get_set_comm(b_.first, graph); + auto ic_moved_from_b = ic_b.intersection(edges_moved_from_b); + auto ec_moved_from_b = ec.intersection(edges_moved_from_b); + + auto ec_moved = ec_moved_from_a.cup(ec_moved_from_b).difference(ec_moved_from_a.intersection(ec_moved_from_b)); + auto ic_moved = ic_moved_from_a.cup(ic_moved_from_b); + + ec = ec.difference(ec_moved).cup(ic_moved); + ic_a = ic_a.difference(ic_moved_from_a); + ic_b = ic_b.cup(ec_moved_from_a); + + ic_b = ic_b.difference(ic_moved_from_b); + ic_a = ic_a.cup(ec_moved_from_b); + + auto new_max_gain = update_diff(gm, a_c, a_v, a_, b_c, b_v, b_, graph, node_weights, g, ic_a, ic_b, ec, LMin, LMax); update_sum(par_sum, g.gain, max_par_sum, max_par_sum_set, a_v, b_v); +#if TEST_EDGE_CUT + // create a local scope to avoid mixing things + { + auto partition_a_set = from_vector(a_c).cup(b_v); + flatten_set(partition_a_set, graph); + Partition temp_partition_a; + for_each(partition_a_set.begin(), partition_a_set.end(), + [&temp_partition_a](auto s) { temp_partition_a.push_back(key.createSet(OrderedSet(s))); }); + + auto partition_b_set = from_vector(b_c).cup(a_v); + flatten_set(partition_b_set, graph); + Partition temp_partition_b; + for_each(partition_b_set.begin(), partition_b_set.end(), + [&temp_partition_b](auto s) { temp_partition_b.push_back(key.createSet(OrderedSet(s))); }); + + int current_edge_cut = get_me_edgecut(temp_partition_a, temp_partition_b, graph).cardinal(); + int expected_gain = prev_edge_cut - current_edge_cut; + if (expected_gain != par_sum) { + cout << "we expect " << expected_gain /*<< " from " << prev_edge_cut << " and " << current_edge_cut*/ << " but we get " << par_sum + << " we are " << ((expected_gain == par_sum) ? "OK" : "bad") << endl; + // cout << "parts: " << temp_partition_a << ", " << temp_partition_b << endl; + } + } +#endif g = move(new_max_gain); } @@ -327,14 +482,18 @@ int kl_sbg_imbalance(const WeightedSBGraph& graph, ICommunicationCost& cost_matr partition_a_set = partition_a_set.cup(max_par_sum_set.second); flatten_set(partition_a_set, graph); partition_a.clear(); - for_each(partition_a_set.begin(), partition_a_set.end(), [&partition_a](auto s) { partition_a.push_back(s); }); + for (const auto& s : key.pieces(get(key.impl(partition_a_set)))) { + partition_a.push_back(key.createSet(OrderedSet(s))); + } auto partition_b_set = from_vector(partition_b); partition_b_set = partition_b_set.difference(max_par_sum_set.second); partition_b_set = partition_b_set.cup(max_par_sum_set.first); flatten_set(partition_b_set, graph); partition_b.clear(); - for_each(partition_b_set.begin(), partition_b_set.end(), [&partition_b](auto s) { partition_b.push_back(s); }); + for (const auto& s : key.pieces(get(key.impl(partition_b_set)))) { + partition_b.push_back(key.createSet(OrderedSet(s))); + } } #if PARTITION_IMBALANCE_DEBUG @@ -376,6 +535,8 @@ KLSbgPartitionerResult kl_sbg_partitioner_function(const WeightedSBGraph& graph, continue; } + cout << "Refining bisection for partitions " << i << ", " << j << endl; + auto p_1_copy = partitions.at(i); auto p_2_copy = partitions.at(j); KLBipartResult current_gain = kl_sbg_bipart_imbalance(graph, cost_matrix, i, p_1_copy, j, p_2_copy, LMin, LMax); @@ -485,10 +646,11 @@ string get_pretty_sb_graph(const SBG::LIB::SBG& g) rapidjson::Document::AllocatorType& allocator = json_doc.GetAllocator(); json_doc.SetObject(); + SetAccessKey key = SetAccess::key(); rapidjson::Value obj_nodes(rapidjson::kArrayType); - for (const SetPiece& set_piece : g.V()) { + for (const MultiDimInter& set_piece : key.pieces(get(key.impl(g.V())))) { rapidjson::Value obj_set_piece_array(rapidjson::kArrayType); - for (const Interval& interval : set_piece.intervals()) { + for (const Interval& interval : set_piece) { rapidjson::Value obj_interval_array(rapidjson::kArrayType); rapidjson::Value begin(rapidjson::kNumberType); @@ -507,52 +669,37 @@ string get_pretty_sb_graph(const SBG::LIB::SBG& g) json_doc.AddMember("nodes", obj_nodes, allocator); - auto map_parser = [&json_doc, &allocator](const auto& maps, const char* key) { + auto map_parser = [&json_doc, &allocator, &key](PWMap maps, const char* name) { rapidjson::Value obj_maps(rapidjson::kArrayType); for (const auto& map1 : maps) { rapidjson::Value map_1_obj(rapidjson::kObjectType); - const Set& domain = map1.dom(); + const Set& domain = map1.domain(); rapidjson::Value domain_obj(rapidjson::kArrayType); - for (const auto& interval : domain) { + for (const auto& interval : key.pieces(get(key.impl(domain)))) { rapidjson::Value obj_interval_array(rapidjson::kArrayType); rapidjson::Value begin(rapidjson::kNumberType); - begin.SetUint(interval.intervals()[0].begin()); + begin.SetUint(interval[0].begin()); obj_interval_array.PushBack(begin, allocator); rapidjson::Value end(rapidjson::kNumberType); - end.SetUint(interval.intervals()[0].end()); + end.SetUint(interval[0].end()); obj_interval_array.PushBack(end, allocator); domain_obj.PushBack(obj_interval_array, allocator); } map_1_obj.AddMember("domain", domain_obj, allocator); - rapidjson::Value exp_obj(rapidjson::kArrayType); - for (const auto& exp : map1.exp()) { - rapidjson::Value obj_exp_array(rapidjson::kArrayType); - - rapidjson::Value slope(rapidjson::kNumberType); - float _slope = exp.slope().numerator(); - slope.SetFloat(_slope); - obj_exp_array.PushBack(slope, allocator); - - rapidjson::Value offset(rapidjson::kNumberType); - float _offset = exp.offset().numerator(); - slope.SetFloat(_offset); - obj_exp_array.PushBack(offset, allocator); - - exp_obj.PushBack(obj_exp_array, allocator); - } + rapidjson::Value exp_obj = map1.law().toJSON(allocator); map_1_obj.AddMember("exp", exp_obj, allocator); obj_maps.PushBack(map_1_obj, allocator); } - rapidjson::Value key_value(key, allocator); + rapidjson::Value name_value(name, allocator); - json_doc.AddMember(key_value, obj_maps, allocator); + json_doc.AddMember(name_value, obj_maps, allocator); }; map_parser(g.map1(), "map1"); @@ -568,10 +715,11 @@ string get_pretty_sb_graph(const SBG::LIB::SBG& g) return json_data; } -void kl_sbg_imbalance_partitioner(const WeightedSBGraph& graph, PartitionMap& partitions, const float imbalance_epsilon, const bool enable_multithreading) +void kl_sbg_imbalance_partitioner(const WeightedSBGraph& graph, PartitionMap& partitions, const float imbalance_epsilon, + const bool enable_multithreading) { - auto [LMin, LMax] = imbalance_epsilon > 0.0 ? compute_lmin_lmax(graph, partitions.size(), imbalance_epsilon) - : make_pair(0, 0); + auto [LMin, LMax] = + imbalance_epsilon > 0.0 ? compute_lmin_lmax(graph, partitions.size(), imbalance_epsilon) : make_pair(0, 0); bool change = true; int counter = 0; @@ -636,7 +784,7 @@ void kl_sbg_imbalance_partitioner(const WeightedSBGraph& graph, PartitionMap& pa if (max_gain_it != gains.end()) { best_gain = *max_gain_it; } else { - best_gain = KLSbgPartitionerResult{0, 0, -Inf, {}, {}}; + best_gain = KLSbgPartitionerResult{0, 0, numeric_limits::min(), {}, {}}; } } } diff --git a/algorithms/partitioner/main.cpp b/algorithms/partitioner/main.cpp index 8cae3789..441ab9c0 100644 --- a/algorithms/partitioner/main.cpp +++ b/algorithms/partitioner/main.cpp @@ -25,434 +25,487 @@ #include #include #include +#include #include -#include -#include +#include +#include +#include #include "build_sb_graph.hpp" #include "communication_cost.hpp" #include "kernighan_lin_partitioner.hpp" #include "partition_graph.hpp" -#include "partition_graph_cc.hpp" #include "partition_metrics_api.hpp" #include "sbg_partitioner_log.hpp" #include "sbg_partitioner_types.hpp" #include "weighted_sb_graph.hpp" +#ifdef USE_MEMORY_TRACKER +// memory benchmarking +#include + +struct MemoryTracker { + size_t total_allocated = 0; + size_t total_freed = 0; + size_t current_usage() const { return total_allocated - total_freed; } +}; + +static MemoryTracker tracker; + +// Scalar new +void* operator new(size_t size) +{ + tracker.total_allocated += size; + return std::malloc(size); +} + +// Scalar delete +void operator delete(void* memory, size_t size) noexcept +{ + tracker.total_freed += size; + std::free(memory); +} + +// Array new +void* operator new[](size_t size) +{ + tracker.total_allocated += size; + return std::malloc(size); +} + +// Array delete +void operator delete[](void* memory, size_t size) noexcept +{ + tracker.total_freed += size; + std::free(memory); +} +#endif using namespace std; using namespace sbg_partitioner; - static void read_configuration_file(const string& filename, PartitionerParams& params) { - - // helper function - auto to_bool = [](string& str) { - std::transform(str.begin(), str.end(), str.begin(), ::tolower); - std::istringstream is(str); - bool b; - is >> std::boolalpha >> b; - return b; - }; - - ifstream ifs(filename); - string line; - while (std::getline(ifs, line)) { - std::istringstream is_line(line); - std::string key; - if(std::getline(is_line, key, '=')) { - std::string value; - if(std::getline(is_line, value)) { - if (key == "filename") { - params.filename = value; - } else if (key == "partitions") { - params.number_of_partitions = stoi(value); - } else if (key == "imbalance") { - params.epsilon = stof(value); - } else if (key == "multithreading") { - params.enable_multithreading = to_bool(value); - } else if (key == "initial_partition_strategy") { - params.initial_partition_strategy = InitialPartitionStrategy(stoi(value)); - } else if (key == "metrics") { - params.compute_metrics = to_bool(value); - } else if (key == "partitions_directory") { - params.directory = value; - } else { - cerr << "Key " << key << " not found, it will be ignored" << endl; - } - } + // helper function + auto to_bool = [](string& str) { + std::transform(str.begin(), str.end(), str.begin(), ::tolower); + std::istringstream is(str); + bool b; + is >> std::boolalpha >> b; + return b; + }; + + ifstream ifs(filename); + string line; + while (std::getline(ifs, line)) { + std::istringstream is_line(line); + std::string key; + if (std::getline(is_line, key, '=')) { + std::string value; + if (std::getline(is_line, value)) { + if (key == "filename") { + params.filename = value; + } else if (key == "partitions") { + params.number_of_partitions = stoi(value); + } else if (key == "imbalance") { + params.epsilon = stof(value); + } else if (key == "multithreading") { + params.enable_multithreading = to_bool(value); + } else if (key == "initial_partition_strategy") { + params.initial_partition_strategy = InitialPartitionStrategy(stoi(value)); + } else if (key == "metrics") { + params.compute_metrics = to_bool(value); + } else if (key == "partitions_directory") { + params.directory = value; + } else { + cerr << "Key " << key << " not found, it will be ignored" << endl; } + } } + } } - static void usage() { - cout << "Usage sbg-partitioner" << endl; - cout << endl; - - cout << "-c, --config-filename Path to the configuration file, a file with key/values " - "that represents the partitioner configuration. If a flag and value by configuration " - "file are passed, the flag will be used." - << endl; - cout << "-f, --filename Path to the input file, a json file that represents " + cout << "Usage sbg-partitioner" << endl; + cout << endl; + + cout << "-c, --config-filename Path to the configuration file, a file with key/values " + "that represents the partitioner configuration. If a flag and value by configuration " + "file are passed, the flag will be used." + << endl; + cout << "-f, --filename Path to the input file, a json file that represents " "the model we want to partitionate." - << endl; - cout << "-p, --partitions Number of partitions." << endl; - cout << "-h, --help Display this information and exit." << endl; - cout << "-v, --version Display version information and exit." << endl; - cout << "-g Output file path." << endl; - cout << "-e Imbalance epsilon, a value between 0 and 1." << endl; - cout << "-m, --compute-metrics If enabled, computes partition quality metrics." << endl; - cout << "-d, --directory Directory with partitions obtianed by other partitioners, " + << endl; + cout << "-p, --partitions Number of partitions." << endl; + cout << "-h, --help Display this information and exit." << endl; + cout << "-v, --version Display version information and exit." << endl; + cout << "-g Output file path." << endl; + cout << "-e Imbalance epsilon, a value between 0 and 1." << endl; + cout << "-m, --compute-metrics If enabled, computes partition quality metrics." << endl; + cout << "-d, --directory Directory with partitions obtianed by other partitioners, " "we want to run quality metrics against them." - << endl; - cout << "-t, --enable-multithreading Enable multithreading during optimization." << endl; - cout << "-i, --initial-partition-strategy Choose a particular initial partition strategy. If this " - "flag is disabaled, all strategies will be computed and the best partition will be chosen.\n" - "\tValue\tSearching algorithm\tStrategy\tOrder\n" - "\t0\tDepth first search\tDistributive\tpreorder\n" - "\t1\tDepth first search\tDistributive\tpostorder\n" - "\t2\tDepth first search\tGreedy\t\tpreorder\n" - "\t3\tDepth first search\tGreedy\t\tpostorder\n" - << endl; - - cout << endl; - cout << "SBG Partitioner home page: https://github.com/CIFASIS/sbg-partitioner " << endl; + << endl; + cout << "-t, --enable-multithreading Enable multithreading during optimization." << endl; + cout << "-i, --initial-partition-strategy Choose a particular initial partition strategy. If this " + "flag is disabaled, all strategies will be computed and the best partition will be chosen.\n" + "\tValue\tSearching algorithm\tStrategy\tOrder\n" + "\t0\tDepth first search\tDistributive\tpreorder\n" + "\t1\tDepth first search\tDistributive\tpostorder\n" + "\t2\tDepth first search\tGreedy\t\tpreorder\n" + "\t3\tDepth first search\tGreedy\t\tpostorder\n" + << endl; + + cout << endl; + cout << "SBG Partitioner home page: https://github.com/CIFASIS/sbg-partitioner " << endl; } - static void version() { - cout << "SBG Partitioner 1.0.0" << endl; - cout << "License GPLv3+: GNU GPL version 3 or later " << endl; - cout << "This is free software: you are free to change and redistribute it." << endl; - cout << "There is NO WARRANTY, to the extent permitted by law." << endl; + cout << "SBG Partitioner 1.0.0" << endl; + cout << "License GPLv3+: GNU GPL version 3 or later " << endl; + cout << "This is free software: you are free to change and redistribute it." << endl; + cout << "There is NO WARRANTY, to the extent permitted by law." << endl; } void sort_before_print(PartitionMap partitions, const SBG::LIB::WeightedSBGraph& sb_graph) { - for (auto& p : partitions) { - sort_partition_intervals(p); - } - cout << "partitions: " << partitions << endl; + for (auto& p : partitions) { + sort_partition_intervals(p); + } + cout << "partitions: " << partitions << endl; } - void read_directory(const std::string& name, std::vector& v) { - auto path_leaf_string = [](const std::filesystem::directory_entry& entry) { return entry.path().string(); }; + auto path_leaf_string = [](const std::filesystem::directory_entry& entry) { return entry.path().string(); }; - std::filesystem::path p(name); - std::filesystem::directory_iterator start(p); - std::filesystem::directory_iterator end; - std::transform(start, end, std::back_inserter(v), path_leaf_string); + std::filesystem::path p(name); + std::filesystem::directory_iterator start(p); + std::filesystem::directory_iterator end; + std::transform(start, end, std::back_inserter(v), path_leaf_string); } - -tuple, PartitionMap, double, double> partitionate_traditional( - const PartitionerParams& params) +int get_air_conditioners_controller_size(const string& name) { - auto start_build_graph = chrono::high_resolution_clock::now(); - auto sb_graph = build_sb_graph(params.filename->c_str()); - auto end_build_graph = chrono::high_resolution_clock::now(); - auto time_to_build_graph = chrono::duration(end_build_graph - start_build_graph).count(); - - cout << "sb_graph: " << sb_graph << endl; - - auto start_partitionate = chrono::high_resolution_clock::now(); - auto partitions = best_initial_partition(sb_graph, *params.number_of_partitions, params.initial_partition_strategy, params.enable_multithreading); - cout << "chosen partition " << partitions << endl; - - kl_sbg_imbalance_partitioner(sb_graph, partitions, params.epsilon, params.enable_multithreading); - auto end_partitionate = chrono::high_resolution_clock::now(); - auto time_to_partitionate = chrono::duration(end_partitionate - start_partitionate).count(); - - return { make_unique(move(sb_graph)), partitions, time_to_build_graph, time_to_partitionate }; + size_t last_underscore = name.find_last_of('_'); + size_t last_dot = name.find_last_of('.'); + + if (last_underscore != std::string::npos && last_dot != std::string::npos) { + // Extract the string between '_' and '.' + std::string size_str = name.substr(last_underscore + 1, last_dot - last_underscore - 1); + int size = std::stoi(size_str); + return size; + } + + cerr << "the file does not have the expected format" << std::endl; + throw 1; } - -tuple, PartitionMap, double, double> partitionate_using_adjacency_matrix(const PartitionerParams& params) +int get_advection2D_size(const string& name) { - auto start_build_graph = chrono::high_resolution_clock::now(); - auto sb_graph = make_unique(build_sb_graph(params.filename->c_str(), false)); - auto end_build_graph = chrono::high_resolution_clock::now(); - auto time_to_build_graph = chrono::duration(end_build_graph - start_build_graph).count(); - - cout << "sb_graph: " << *sb_graph << endl; + size_t last_underscore = name.find_last_of('_'); + size_t last_dot = name.find_last_of('.'); + + if (last_underscore != std::string::npos && last_dot != std::string::npos) { + // Extract the string between '_' and '.' + std::string size_str = name.substr(last_underscore + 1, last_dot - last_underscore - 1); + int size = std::stoi(size_str); + return size; + } + + cerr << "the file does not have the expected format" << std::endl; + throw 1; +} - auto injective_conn = using_cc::split_nodes_into_injective_domains(*sb_graph); +SBG::LIB::WeightedSBGraph get_sbg(const std::string& filename, unsigned number_of_parts, bool duplicate_conns) +{ + if (filename.find("air_conditioners_cont") != std::string::npos) { + auto size = get_air_conditioners_controller_size(filename); + auto sb_graph = create_air_conditioners_with_controller_graph(size, number_of_parts, duplicate_conns); + return sb_graph; + } else if (filename.find("advection2D") != std::string::npos) { + auto size = get_air_conditioners_controller_size(filename); + auto sb_graph = create_advection2D_graph(size); + return sb_graph; + } else { + auto sb_graph = build_sb_graph(filename, false); + cout << sb_graph << endl; + return sb_graph; + } +} - logging::sbg_log << "injective connections " << injective_conn << endl; -#if SBG_PARTITIONER_LOGGING - logging::sbg_log << "remaining " << sb_graph->V().difference(injective_conn) << endl; +SBG::LIB::WeightedSBGraph build_computational_sbg(const PartitionerParams& params) +{ + cout << "Building computational SB Graph" << endl; + SBG::LIB::WeightedSBGraph sb_graph = get_sbg(*params.filename, *params.number_of_partitions, true); + + if (not params.filename->find("air_conditioners_cont") != std::string::npos) { + return sb_graph; + } + return sb_graph; + + // This is a hack to get intervals from their relations + auto new_vertices = split_sets_according_to_relations(sb_graph); + + // check that all vertices were included + if (sanity_check_enabled) { + auto diff1 = new_vertices.difference(sb_graph.V()); + assert(diff1.isEmpty()); + + auto diff2 = sb_graph.V().difference(new_vertices); + assert(diff2.isEmpty()); + + cout << "diff1 " << diff1 << ", " << "diff2 " << diff2 << " " << diff2.isEmpty() << endl; + } + +#ifdef SBG_PARTITIONER_LOGGING + cout << "sb_graph: " << sb_graph << endl; + cout << "connections:\n"; + SBG::LIB::PWMap pw_map1 = sb_graph.map1(); + SBG::LIB::PWMap pw_map2 = sb_graph.map2(); + for (auto it1 = pw_map1.begin(), it2 = pw_map2.begin(); it1 != pw_map1.end() and it2 != pw_map2.end(); ++it1, ++it2) { + auto n1 = (*it1).image(); + auto n2 = (*it2).image(); + cout << n1 << ", " << n2 << " from " << (*it1).domain() << "\n"; + } + cout << endl; #endif - auto start_partitionate = chrono::high_resolution_clock::now(); - using_cc::SetPointers sorted_nodes = {}; - unsigned index = 0; - for (const auto& s : injective_conn) { - sorted_nodes.push_back(using_cc::SetPointer(index++, s, 0, s.cardinal())); - } - - sbg_partitioner::CommunicationCostCC comm_cc(*sb_graph, sorted_nodes); - - std::vector sorted_partitions; - auto new_graph = SBG::LIB::WeightedSBGraph( - injective_conn, - sb_graph->Vmap(), - sb_graph->map1(), - sb_graph->map2(), - sb_graph->Emap(), - sb_graph->subEmap() - ); - - auto non_sorted_partitions = best_initial_partition(new_graph, *params.number_of_partitions, params.initial_partition_strategy, params.enable_multithreading); - - // logging::sbg_log << "converting nodes into set pointers" << endl; - auto start_conversion = chrono::high_resolution_clock::now(); - for (const auto& partition : non_sorted_partitions) { - sorted_partitions.emplace_back(); - for (const auto& v : partition) { - for (const auto& n : sorted_nodes) { - if (not v.intersection(n.set_piece).isEmpty()) { - size_t offset = v.begin()[0].begin() - n.set_piece.begin()[0].begin(); - sorted_partitions.back().emplace_back(n.index, n.set_piece, offset, v.cardinal()); - } - } - } - } - auto end_conversion = chrono::high_resolution_clock::now(); - auto conversion_time = chrono::duration(end_conversion - start_conversion).count(); - // auto sorted_partitions = using_cc::best_initial_partition(*sb_graph, sorted_nodes, comm_cc, *params.number_of_partitions, InitialPartitionStrategy::DFS_DISTRIBUTIVE_POSTORDER); - - if (sanity_check_enabled) { - sanity_check(*sb_graph, sbg_partitioner::using_cc::rebuild_partitions(sorted_nodes, sorted_partitions), *params.number_of_partitions); - } - - using_cc::kl_sbg_imbalance_partitioner(*sb_graph, sorted_nodes, sorted_partitions, comm_cc, 0.); + auto map1 = sb_graph.map1(); + map1.compact(); + auto map2 = sb_graph.map2(); + map2.compact(); - auto partitions = sbg_partitioner::using_cc::rebuild_partitions(sorted_nodes, sorted_partitions); - auto end_partitionate = chrono::high_resolution_clock::now(); - auto time_to_partitionate = chrono::duration(end_partitionate - start_partitionate).count(); - time_to_partitionate -= conversion_time; - - if (sanity_check_enabled) { - sanity_check(*sb_graph, partitions, *params.number_of_partitions); - } - - return { move(sb_graph), partitions, time_to_build_graph, time_to_partitionate }; + return SBG::LIB::WeightedSBGraph(new_vertices, SBG::LIB::PWMap(), map1, map2, SBG::LIB::PWMap()); } +tuple run_partitioner(const PartitionerParams& params) +{ + auto start_building_graph = chrono::high_resolution_clock::now(); + auto filename = *params.filename; + auto sb_graph = build_computational_sbg(params); + auto end_building_graph = chrono::high_resolution_clock::now(); + auto time_to_build_graph = chrono::duration(end_building_graph - start_building_graph).count(); + cout << sb_graph << endl; + + auto start_partitionate = chrono::high_resolution_clock::now(); + auto partition = + best_initial_partition(sb_graph, *params.number_of_partitions, params.initial_partition_strategy, params.enable_multithreading); + cout << "chosen partition " << partition << endl; + + kl_sbg_imbalance_partitioner(sb_graph, partition, params.epsilon, params.enable_multithreading); + auto end_partitionate = chrono::high_resolution_clock::now(); + auto time_to_partitionate = chrono::duration(end_partitionate - start_partitionate).count(); + + if (params.dump_results) { + // graph may be compacted, so compute it again + auto actual_sbg = get_sbg(*params.filename, *params.number_of_partitions, false); + metrics::dump_results(actual_sbg, *params.filename, partition); + } + + return {sb_graph, partition, time_to_build_graph, time_to_partitionate}; +} static struct option long_options[] = {{"config-filename", required_argument, 0, 'c'}, - {"filename", required_argument, 0, 'f'}, {"partitions", required_argument, 0, 'p'}, - {"output-file", required_argument, 0, 'g'}, {"output-graph", required_argument, 0, 'o'}, - {"compute-metrics", no_argument, 0, 'm'}, {"directory", required_argument, 0, 'd'}, - {"initial-partition-strategy", required_argument, 0, 'i'}, {"enable-multithreading", no_argument, 0, 't'}, - {"use-adjacency-matrix", no_argument, 0, 'k'}, {"version", no_argument, 0, 'v'}, + {"filename", required_argument, 0, 'f'}, + {"partitions", required_argument, 0, 'p'}, + {"output-file", required_argument, 0, 'g'}, + {"output-graph", required_argument, 0, 'o'}, + {"compute-metrics", no_argument, 0, 'm'}, + {"directory", required_argument, 0, 'd'}, + {"initial-partition-strategy", required_argument, 0, 'i'}, + {"enable-multithreading", no_argument, 0, 't'}, + {"dump-results", no_argument, 0, 'r'}, + {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}}; - int main(int argc, char** argv) { - int opt; - PartitionerParams params; - - // look for configuration file - while (true) - { - int option_index = 0; - opt = getopt_long(argc, argv, "c:f:p:e:o:g:d:i:ktmvh:", long_options, &option_index); - if (opt == EOF) break; - - switch (opt) { - case 'c': - if (optarg) { - params.config_filename = string(optarg); - } - break; - - default: - continue; - } + SBG::LIB::SET_IMPL.set_set_fact(SBG::LIB::SetKind::kOrdered); + + int opt; + PartitionerParams params; + + // look for configuration file + while (true) { + int option_index = 0; + opt = getopt_long(argc, argv, "c:f:p:e:r:g:d:i:tmvh:", long_options, &option_index); + if (opt == EOF) break; + + switch (opt) { + case 'c': + if (optarg) { + params.config_filename = string(optarg); + } + break; + + default: + continue; } - - // read configuration file if exists, before reading flags - if (params.config_filename) { - read_configuration_file(*params.config_filename, params); - } - - // reset it to read values again. We reed them again because flags overwrite values from configuration file - optind = 0; - while (true) { - int option_index = 0; - opt = getopt_long(argc, argv, "c:f:p:e:o:g:d:i:tmvh:", long_options, &option_index); - if (opt == EOF) break; - - switch (opt) { - case 'c': // already read - break; - - case 'f': - if (optarg) { - params.filename = string(optarg); - } - break; - - case 'p': - if (optarg) { - params.number_of_partitions = atoi(optarg); - } - break; - - case 'o': - if (optarg) { - params.output_sb_graph = string(optarg); - } - break; - - case 'g': - if (optarg) { - params.output_file = string(optarg); - } - break; - - case 'e': - if (optarg) { - params.epsilon = atof(optarg); - } - break; - - case 'm': - params.compute_metrics = true; - break; - - case 'd': - if (optarg) { - params.directory = string(optarg); - } - break; - - case 'i': - if (optarg) { - params.initial_partition_strategy = InitialPartitionStrategy(atoi(optarg)); - } - break; - - case 't': - params.enable_multithreading = true; - break; - - case 'k': - params.use_connected_components = true; - break; - - case 'v': - version(); - exit(0); - - case 'h': - usage(); - exit(0); - - case '?': - usage(); - exit(-1); - break; - - default: - abort(); - } + } + + // read configuration file if exists, before reading flags + if (params.config_filename) { + read_configuration_file(*params.config_filename, params); + } + + // reset it to read values again. We reed them again because flags overwrite values from configuration file + optind = 0; + while (true) { + int option_index = 0; + opt = getopt_long(argc, argv, "c:f:p:e:r:g:d:i:tmvh:", long_options, &option_index); + if (opt == EOF) break; + + switch (opt) { + case 'c': // already read + break; + + case 'f': + if (optarg) { + params.filename = string(optarg); + } + break; + + case 'p': + if (optarg) { + params.number_of_partitions = atoi(optarg); + } + break; + + case 'r': + params.dump_results = true; + break; + + case 'g': + if (optarg) { + params.output_file = string(optarg); + } + break; + + case 'e': + if (optarg) { + params.epsilon = atof(optarg); + } + break; + + case 'm': + params.compute_metrics = true; + break; + + case 'd': + if (optarg) { + params.directory = string(optarg); + } + break; + + case 'i': + if (optarg) { + params.initial_partition_strategy = InitialPartitionStrategy(atoi(optarg)); + } + break; + + case 't': + params.enable_multithreading = true; + break; + + case 'v': + version(); + exit(0); + + case 'h': + usage(); + exit(0); + + case '?': + usage(); + exit(-1); + break; + + default: + abort(); } + } - if (not params.filename or not params.number_of_partitions) { - usage(); - exit(1); - } + if (not params.filename or not params.number_of_partitions) { + usage(); + exit(1); + } - if (params.epsilon < 0 or params.epsilon > 1) { - usage(); - exit(1); - } + if (params.epsilon < 0 or params.epsilon > 1) { + usage(); + exit(1); + } - cout << "filename is " << *params.filename << endl; - cout << "number of partitions is " << *params.number_of_partitions << endl; + cout << "filename is " << *params.filename << endl; + cout << "number of partitions is " << *params.number_of_partitions << endl; - optional s; - if (params.output_sb_graph) { - s = ""; - } + auto [sb_graph, partitions, time_to_build_graph, time_to_partitionate] = run_partitioner(params); - unique_ptr sb_graph; - PartitionMap partitions; - double time_to_build_graph, time_to_partitionate = 0.0; - if (not params.use_connected_components) { - tie(sb_graph, partitions, time_to_build_graph, time_to_partitionate) = partitionate_traditional(params); - } else { - tie(sb_graph, partitions, time_to_build_graph, time_to_partitionate) = partitionate_using_adjacency_matrix(params); - } + cout << "Partition obtained after refining:\n" << partitions << endl; - if (not sb_graph) { - cerr << "Something happened and the graph was not even created. Returning earlier" << endl; - return 1; - } + if (params.compute_metrics) { + map metrics; - if (params.compute_metrics) { - map metrics; + int edge_cut = metrics::edge_cut(partitions, sb_graph); - int edge_cut = metrics::edge_cut(partitions, *sb_graph); + auto [comm_volume, max_comm_volume] = metrics::communication_volume(partitions, sb_graph); - auto [comm_volume, max_comm_volume] = metrics::communication_volume(partitions, *sb_graph); + auto max_imb = metrics::maximum_imbalance(partitions, sb_graph); - auto max_imb = metrics::maximum_imbalance(partitions, *sb_graph); + metrics::communication_metrics comm_metrics = metrics::communication_metrics{edge_cut, comm_volume, max_comm_volume, max_imb}; + metrics["sbg-partitioner"] = comm_metrics; - metrics::communication_metrics comm_metrics = metrics::communication_metrics{edge_cut, comm_volume, max_comm_volume, max_imb}; - metrics["sbg-partitioner"] = comm_metrics; + if (params.compute_metrics and params.directory) { + std::vector dir_files; + read_directory(*params.directory, dir_files); - for (const auto& [f, m] : metrics) { - cout << f << ": " << m << endl; + for (const auto& f : dir_files) { + if (f.substr(f.find_last_of(".") + 1) == "csv") { + cout << "Ignoring " << f << endl; + continue; } + cout << f << endl; - if (params.compute_metrics and params.directory) { - std::vector dir_files; - read_directory(*params.directory, dir_files); + auto partition_from_file = metrics::read_partition_from_file(f, sb_graph, *params.number_of_partitions); - for (const auto& f : dir_files) { - auto partition_from_file = metrics::read_partition_from_file(f, *sb_graph); + int edge_cut = metrics::edge_cut(partition_from_file, sb_graph); - int edge_cut = metrics::edge_cut(partition_from_file, *sb_graph); + auto [comm_volume, max_comm_volume] = metrics::communication_volume(partition_from_file, sb_graph); - auto [comm_volume, max_comm_volume] = metrics::communication_volume(partition_from_file, *sb_graph); + auto max_imb = metrics::maximum_imbalance(partition_from_file, sb_graph); - auto max_imb = metrics::maximum_imbalance(partition_from_file, *sb_graph); - - metrics::communication_metrics comm_metrics = metrics::communication_metrics{edge_cut, comm_volume, max_comm_volume, max_imb}; - metrics[std::filesystem::path(f).filename().string()] = comm_metrics; - } - } + metrics::communication_metrics comm_metrics = metrics::communication_metrics{ + edge_cut, + comm_volume, + max_comm_volume, + }; + metrics[std::filesystem::path(f).filename().string()] = comm_metrics; + } } - // print profiler results if they are enabled - SBG::Util::time_profiler_results(); - - cout << "time_to_build_graph = " << time_to_build_graph << " ms" << endl; - cout << "time_to_partitionate = " << time_to_partitionate << " ms" << endl; - - if (sanity_check_enabled) { - sanity_check(*sb_graph, partitions, *params.number_of_partitions); + ofstream metrics_file(*params.directory + "/metrics.csv"); + for (const auto& [f, m] : metrics) { + cout << f << ": " << m << endl; + metrics_file << f << ", " << m << endl; } + } - if (s) { - s = get_pretty_sb_graph(*sb_graph); - } + // print profiler results if they are enabled + SBG::Util::time_profiler_results(); + + cout << "time_to_build_graph = " << time_to_build_graph << " ms" << endl; + cout << "time_to_partitionate = " << time_to_partitionate << " ms" << endl; - sort_before_print(partitions, *sb_graph); + if (sanity_check_enabled) { + sanity_check(sb_graph, partitions, *params.number_of_partitions); + } - string output = get_output(partitions); +#ifdef USE_MEMORY_TRACKER + std::cout << tracker.total_allocated << " bytes were allocated during this execution." << std::endl; +#endif - return 0; + return 0; } diff --git a/algorithms/partitioner/partition_graph.cpp b/algorithms/partitioner/partition_graph.cpp index 0e74f54f..321b40d1 100644 --- a/algorithms/partitioner/partition_graph.cpp +++ b/algorithms/partitioner/partition_graph.cpp @@ -22,7 +22,8 @@ #include #include #include -#include +#include +#include #include "build_sb_graph.hpp" #include "communication_cost.hpp" @@ -30,10 +31,13 @@ #include "partition_graph.hpp" #include "sbg_partitioner_log.hpp" - using namespace std; using namespace SBG::LIB; +using SBG::LIB::detail::Interval; +using SBG::LIB::detail::OrderedSet; +using SBG::LIB::detail::SetAccess; +using SBG::LIB::detail::SetAccessKey; using namespace SBG::Util; using namespace sbg_partitioner::search; @@ -53,23 +57,21 @@ Set get_communication_edges(Set partition, const PWMap& map_1, const PWMap& map_ return comm_edges; } +// [[maybe_unused]] size_t get_partition_communication(WeightedSBGraph& graph, const PartitionMap& partitions) +// { +// Set s = create_set(); +// for (size_t i = 0; i < partitions.size(); i++) { +// auto ss = get_connectivity_set(graph, partitions, i); +// s = ss.cup(s); +// } -[[maybe_unused]] size_t get_partition_communication(WeightedSBGraph& graph, const PartitionMap& partitions) -{ - Set s = SET_FACT.createSet(); - for (size_t i = 0; i < partitions.size(); i++) { - auto ss = get_connectivity_set(graph, partitions, i); - s = ss.cup(s); - } - - size_t size = get_set_size(s); - - return size; -} +// size_t size = get_set_size(s); +// return size; +// } vector make_initial_partitions(SBG::LIB::WeightedSBGraph& graph, unsigned number_of_partitions, - const InitialPartitionStrategy strategy) + const InitialPartitionStrategy strategy) { vector partitions_sets; initialize_partitioning(graph, number_of_partitions); @@ -96,28 +98,29 @@ vector make_initial_partitions(SBG::LIB::WeightedSBGraph& graph, u add_strategy(move(s4), not pre_order); } - vector>> partitions = partitionate(); + vector>> partitions = partitionate(); for (const auto& partition : partitions) { PartitionMap partition_set; for (const auto& [id, set] : partition) { - Set one_partition_set = SET_FACT.createSet(); + Set one_partition_set; Partition p; for (auto& s : set) { - SetPiece intervals; - if (not s.intervals().empty()) { - for (size_t i = 0; i < s.intervals().size(); i++) { - Interval interv = s.intervals()[i]; - intervals.emplaceBack(interv); - } - } - p.emplace_back(intervals); + // Set intervals = create_set(); + // if (not s.intervals().empty()) { + // for (size_t i = 0; i < s.arity(); i++) { + // Interval interv = s[i]; + // intervals.emplaceBack(interv); + // } + // } + p.emplace_back(s); } partition_set.push_back(p); } partitions_sets.push_back(move(partition_set)); } + cout << *partitions_sets.begin() << endl; for_each(partitions_sets.begin(), partitions_sets.end(), [&graph, number_of_partitions](PartitionMap& p) { logging::sbg_log << p << endl; @@ -131,63 +134,62 @@ vector make_initial_partitions(SBG::LIB::WeightedSBGraph& graph, u } // namespace - // we could cache solutions here -Set from_vector(const Partition& partition) { - Set partition_set = SET_FACT.createSet(); - for (size_t i = 0; i < partition.size(); i++) { - partition_set.emplace(partition[i]); - } +Set from_vector(const Partition& partition) +{ + Set partition_set; + for (size_t i = 0; i < partition.size(); i++) { + partition_set = partition_set.cup(partition[i]); + } - return partition_set; + return partition_set; } - Partition to_vector(const Set& partition_set) { - Partition partition; - for (auto set_piece : partition_set) { - partition.push_back(move(set_piece)); - } + SetAccessKey key = SetAccess::key(); + Partition partition; + for (const auto& set_piece : key.pieces(get(key.impl(partition_set)))) { + partition.push_back(key.createSet(OrderedSet(set_piece))); + } - return partition; + return partition; } - -PartitionMap best_initial_partition(WeightedSBGraph& graph, unsigned number_of_partitions, - const InitialPartitionStrategy strategy, bool multithreading_enabled, bool create_comm_cost) +PartitionMap best_initial_partition(WeightedSBGraph& graph, unsigned number_of_partitions, const InitialPartitionStrategy strategy, + bool multithreading_enabled, bool create_comm_cost) { logging::sbg_log << "computing strategy number " << strategy << endl; std::vector partition_maps = make_initial_partitions(graph, number_of_partitions, strategy); auto& best_initial_partitions = partition_maps.front(); + cout << "best_initial_partitions " << best_initial_partitions << endl; CommunicationCostPtr comm_cost; if (create_comm_cost) { comm_cost = create_communication_cost(graph, best_initial_partitions, multithreading_enabled); } if (strategy == InitialPartitionStrategy::ALL) { - - auto best_communication_set = SET_FACT.createSet(); + auto best_communication_set = Set(); for (unsigned i = 0; i < number_of_partitions; i++) { - best_communication_set = best_communication_set.cup(comm_cost->get_ec_by_partition_id(i)); + best_communication_set = best_communication_set.cup(comm_cost->get_ec_by_partition_id(i)); } size_t best_communication_set_size = best_communication_set.cardinal(); for (size_t i = 1; i < partition_maps.size(); i++) { - auto temp_intial_partitions = partition_maps[i]; - CommunicationCostPtr temp_comm_cost = create_communication_cost(graph, temp_intial_partitions, multithreading_enabled); + auto temp_intial_partitions = partition_maps[i]; + CommunicationCostPtr temp_comm_cost = create_communication_cost(graph, temp_intial_partitions, multithreading_enabled); - auto temp_partition_comm = SET_FACT.createSet(); - for (unsigned i = 0; i < number_of_partitions; i++) { - temp_partition_comm = temp_partition_comm.cup(comm_cost->get_ec_by_partition_id(i)); - } + auto temp_partition_comm = Set(); + for (unsigned i = 0; i < number_of_partitions; i++) { + temp_partition_comm = temp_partition_comm.cup(temp_comm_cost->get_ec_by_partition_id(i)); + } - size_t temp_intial_partitions_size = temp_partition_comm.cardinal(); + size_t temp_intial_partitions_size = temp_partition_comm.cardinal(); - if (temp_intial_partitions_size < best_communication_set_size) { - comm_cost = move(temp_comm_cost); - best_initial_partitions = move(partition_maps[i]); - } + if (temp_intial_partitions_size < best_communication_set_size) { + comm_cost = move(temp_comm_cost); + best_initial_partitions = move(partition_maps[i]); + } } logging::sbg_log << "Best is " << best_initial_partitions << " with communication " << best_communication_set << endl; @@ -200,30 +202,32 @@ PartitionMap best_initial_partition(WeightedSBGraph& graph, unsigned number_of_p return best_initial_partitions; } -Set get_connectivity_set(SBG::LIB::SBG& graph, const PartitionMap& partitions, size_t partition_index) -{ - const auto& partition_vector = partitions.at(partition_index); - Set partition = SET_FACT.createSet(); - for_each(partition_vector.cbegin(), partition_vector.cend(), [&partition] (auto s) { partition.emplaceBack(s); }); +// Set get_connectivity_set(SBG::LIB::SBG& graph, const PartitionMap& partitions, size_t partition_index) +// { +// const auto& partition_vector = partitions.at(partition_index); +// Set partition = create_set(); +// for_each(partition_vector.cbegin(), partition_vector.cend(), [&partition](auto s) { partition.emplaceBack(s); }); - auto comm_edges_1 = get_communication_edges(partition, graph.map1(), graph.map2()); - auto comm_edges_2 = get_communication_edges(partition, graph.map2(), graph.map1()); - auto comm_edges = comm_edges_1.cup(comm_edges_2); - - return comm_edges; -} +// auto comm_edges_1 = get_communication_edges(partition, graph.map1(), graph.map2()); +// auto comm_edges_2 = get_communication_edges(partition, graph.map2(), graph.map1()); +// auto comm_edges = comm_edges_1.cup(comm_edges_2); +// return comm_edges; +// } void sanity_check(const WeightedSBGraph& graph, const PartitionMap& partitions_set, unsigned number_of_partitions) { // This is just a sanity check - Set nodes_to_check = SET_FACT.createSet(); + Set nodes_to_check; for (unsigned i = 0; i < number_of_partitions; i++) { + cout << i << ", " << partitions_set[i] << endl; auto s_i = from_vector(partitions_set[i]); assert(s_i.intersection(nodes_to_check).isEmpty()); nodes_to_check = nodes_to_check.cup(s_i); } + cout << "sanity check " << nodes_to_check << ", " << graph.V() << endl; + Set diff = nodes_to_check.difference(graph.V()); logging::sbg_log << "diff1 " << diff << endl; assert(get_node_size(diff, graph.get_node_weights()) == 0 and "The intial partition has more elements than the graph"); @@ -250,69 +254,58 @@ void sanity_check(const WeightedSBGraph& graph, const PartitionMap& partitions_s string get_output(const PartitionMap& partition_map) { - rapidjson::Document json_doc; - rapidjson::Document::AllocatorType& allocator = json_doc.GetAllocator(); - json_doc.SetObject(); - - rapidjson::Value obj_partitions(rapidjson::kArrayType); - for (size_t i = 0; i < partition_map.size(); i++) { - rapidjson::Value obj_partition(rapidjson::kArrayType); - const auto& partition = partition_map.at(i); - for (auto it = partition.begin(); it != partition.end(); ++it) { - const SetPiece& set_piece = *it; - rapidjson::Value obj_intervals(rapidjson::kArrayType); - obj_intervals.SetArray(); - for (const Interval& interval : set_piece.intervals()) { - rapidjson::Value obj_interval(rapidjson::kArrayType); - rapidjson::Value begin(rapidjson::kNumberType); - begin.SetUint(interval.begin()); - obj_interval.PushBack(begin, allocator); - rapidjson::Value end(rapidjson::kNumberType); - end.SetUint(interval.end()); - obj_interval.PushBack(end, allocator); - - obj_intervals.PushBack(obj_interval, allocator); - } - - obj_partition.PushBack(obj_intervals, allocator); - } - - rapidjson::Value obj_nodes(rapidjson::kObjectType); - obj_nodes.AddMember("nodes", obj_partition, allocator); - - obj_partitions.PushBack(obj_nodes, allocator); - } - - json_doc.AddMember("partitions", obj_partitions, allocator); - - // Write the JSON data to the file - rapidjson::StringBuffer s; - rapidjson::Writer writer(s); - json_doc.Accept(writer); - string json_data = string(s.GetString()); - - return json_data; + // rapidjson::Document json_doc; + // rapidjson::Document::AllocatorType& allocator = json_doc.GetAllocator(); + // json_doc.SetObject(); + + // rapidjson::Value obj_partitions(rapidjson::kArrayType); + // for (size_t i = 0; i < partition_map.size(); i++) { + // rapidjson::Value obj_partition(rapidjson::kArrayType); + // const auto& partition = partition_map.at(i); + // for (auto it = partition.begin(); it != partition.end(); ++it) { + // const auto& set_piece = *it; + // rapidjson::Value obj_intervals(rapidjson::kArrayType); + // obj_intervals.SetArray(); + // for (const Interval& interval : set_piece) { + // rapidjson::Value obj_interval(rapidjson::kArrayType); + // rapidjson::Value begin(rapidjson::kNumberType); + // begin.SetUint(interval.begin()); + // obj_interval.PushBack(begin, allocator); + // rapidjson::Value end(rapidjson::kNumberType); + // end.SetUint(interval.end()); + // obj_interval.PushBack(end, allocator); + + // obj_intervals.PushBack(obj_interval, allocator); + // } + + // obj_partition.PushBack(obj_intervals, allocator); + // } + + // rapidjson::Value obj_nodes(rapidjson::kObjectType); + // obj_nodes.AddMember("nodes", obj_partition, allocator); + + // obj_partitions.PushBack(obj_nodes, allocator); + // } + + // json_doc.AddMember("partitions", obj_partitions, allocator); + + // // Write the JSON data to the file + // rapidjson::StringBuffer s; + // rapidjson::Writer writer(s); + // json_doc.Accept(writer); + // string json_data = string(s.GetString()); + + // return json_data; + return ""; } - -void sort_partition_intervals(Partition& p) -{ - constexpr auto compare_intervals = [](const SBG::LIB::SetPiece& s1, const SBG::LIB::SetPiece& s2) { - return s1[0].begin() < s2[0].begin(); - }; - - sort(p.begin(), p.end(), compare_intervals); -} - - ostream& operator<<(ostream& os, const Partition& partition) { - for_each(partition.cbegin(), partition.cend(), [&os] (const auto& p) { os << p << " "; }); + for_each(partition.cbegin(), partition.cend(), [&os](const auto& p) { os << p << " "; }); - return os; + return os; } - ostream& operator<<(ostream& os, const PartitionMap& partitions) { for (size_t i = 0; i < partitions.size(); i++) { diff --git a/algorithms/partitioner/partition_graph.hpp b/algorithms/partitioner/partition_graph.hpp index 4167b2a9..48100ed7 100644 --- a/algorithms/partitioner/partition_graph.hpp +++ b/algorithms/partitioner/partition_graph.hpp @@ -22,7 +22,6 @@ #include #include -#include #include #include "partitioner_params.hpp" @@ -67,7 +66,13 @@ SBG::LIB::Set get_connectivity_set( /// @brief It sorts intervals from smallest to largest from a given partition partition. /// @param p - partition to be sorted. -void sort_partition_intervals(Partition& p); +template +void sort_partition_intervals(std::vector& p) +{ + constexpr auto compare_intervals = [](const auto& s1, const auto& s2) { return s1.minElem() < s2.minElem(); }; + + std::sort(p.begin(), p.end(), compare_intervals); +} /// @brief Pretty print for a given partition. diff --git a/algorithms/partitioner/partition_graph_cc.cpp b/algorithms/partitioner/partition_graph_cc.cpp index 7844cbec..47ee7d8a 100644 --- a/algorithms/partitioner/partition_graph_cc.cpp +++ b/algorithms/partitioner/partition_graph_cc.cpp @@ -24,10 +24,8 @@ #include "build_sb_graph.hpp" #include "dfs_on_sbg.hpp" #include "partition_graph.hpp" -#include "partition_graph_cc.hpp" #include "sbg_partitioner_log.hpp" - using namespace std; using namespace SBG::LIB; @@ -35,818 +33,635 @@ namespace sbg_partitioner { namespace using_cc { - ostream& operator<<(ostream& os, const SetPointer& set_pointer) { - os << "[" << set_pointer.index << ", " << set_pointer.set_piece << ", " << set_pointer.offset << ", " << set_pointer.size << "]"; + os << "[" << set_pointer.index << ", " << set_pointer.set_piece << ", " << set_pointer.offset << ", " << set_pointer.size << "]"; - return os; + return os; } - ostream& operator<<(ostream& os, const SetPointers& set_pointers) { - os << "{"; - for (const auto& s : set_pointers) { - os << s << " "; - } - os << "}"; + os << "{"; + for (const auto& s : set_pointers) { + os << s << " "; + } + os << "}"; - return os; + return os; } - struct GainObject { - SetPointer a; - SetPointer b; - size_t size; - float gain; - - GainObject(SetPointer a_idx, SetPointer b_idx, size_t size, float gain) - : a(move(a_idx)), - b(move(b_idx)), - size(size), - gain(gain) - {} - - bool operator==(const GainObject& other) { - return a == other.a and - b == other.b and - size == other.size and - gain == other.gain; - } + SetPointer a; + SetPointer b; + size_t size; + float gain; + + GainObject(SetPointer a_idx, SetPointer b_idx, size_t size, float gain) : a(move(a_idx)), b(move(b_idx)), size(size), gain(gain) {} + + bool operator==(const GainObject& other) { return a == other.a and b == other.b and size == other.size and gain == other.gain; } }; typedef vector GainObjects; - ostream& operator<<(ostream& os, const GainObject& gain_obj) { - os << "{ " - << gain_obj.a - << ", " - << gain_obj.b - << ", size: " - << gain_obj.size - << ", gain: " - << gain_obj.gain - << "}"; - - return os; -} + os << "{ " << gain_obj.a << ", " << gain_obj.b << ", size: " << gain_obj.size << ", gain: " << gain_obj.gain << "}"; + return os; +} ostream& operator<<(ostream& os, const GainObjects& gains) { - for (const auto& g : gains) { - os << g << "\n"; - } + for (const auto& g : gains) { + os << g << "\n"; + } - return os; + return os; } - namespace { Set granular_union(const Set& s_1, const Set& s_2) { - set points; - for (const auto& set_piece : s_1) { - points.insert((set_piece)[0].begin()); - points.insert(set_piece[0].end() + 1); - } - - for (const auto& set_piece : s_2) { - points.insert(set_piece[0].begin()); - points.insert(set_piece[0].end() + 1); - } - - vector points_vector(points.begin(), points.end()); - - vector granular_union_vector; - for (size_t i = 0; i < points_vector.size() - 1; i++) { - granular_union_vector.push_back(SBG::LIB::Interval(points_vector[i], 1, points_vector[i + 1] - 1)); - } - - auto s1_s2_cup = s_1.cup(s_2); - auto granular_union_set = SBG::LIB::SET_FACT.createSet(); - for (const auto& set_piece : granular_union_vector) { - auto set = SBG::LIB::SET_FACT.createSet(set_piece); - if (not s1_s2_cup.intersection(set).isEmpty()){ - granular_union_set = granular_union_set.cup(set); - } + set points; + for (const auto& set_piece : s_1) { + points.insert((set_piece)[0].begin()); + points.insert(set_piece[0].end() + 1); + } + + for (const auto& set_piece : s_2) { + points.insert(set_piece[0].begin()); + points.insert(set_piece[0].end() + 1); + } + + vector points_vector(points.begin(), points.end()); + + vector granular_union_vector; + for (size_t i = 0; i < points_vector.size() - 1; i++) { + granular_union_vector.push_back(SBG::LIB::Interval(points_vector[i], 1, points_vector[i + 1] - 1)); + } + + auto s1_s2_cup = s_1.cup(s_2); + auto granular_union_set = SBG::LIB::SET_FACT.createSet(); + for (const auto& set_piece : granular_union_vector) { + auto set = SBG::LIB::SET_FACT.createSet(set_piece); + if (not s1_s2_cup.intersection(set).isEmpty()) { + granular_union_set = granular_union_set.cup(set); } + } - return granular_union_set; + return granular_union_set; } - bool set_comparison(const Set& s, const Set& q) { - if (s.size() != q.size()) { - return false; - } - - for (auto i1 = s.begin(), i2 = q.begin(); i1 != s.end() and i1 != q.end(); ++i1, ++i2) { - auto sp1 = *i1; - auto sp2 = *i2; - if (not (sp1 == sp2)) { - return false; - } + if (s.size() != q.size()) { + return false; + } + + for (auto i1 = s.begin(), i2 = q.begin(); i1 != s.end() and i1 != q.end(); ++i1, ++i2) { + auto sp1 = *i1; + auto sp2 = *i2; + if (not(sp1 == sp2)) { + return false; } + } - return true; + return true; }; - pair take_granular_option(const Set& set_1, const Set& set_2) { - vector lenghts; - assert(set_1.cardinal() == set_2.cardinal()); - - auto vector_set_1 = to_vector(set_1); - auto vector_set_2 = to_vector(set_2); + vector lenghts; + assert(set_1.cardinal() == set_2.cardinal()); - auto new_set_1 = SBG::LIB::SET_FACT.createSet(); - auto new_set_2 = SBG::LIB::SET_FACT.createSet(); - for (size_t i = 0, j = 0; i < vector_set_1.size() and j < vector_set_2.size();) { - unsigned l1 = vector_set_1.at(i).cardinal(); - unsigned l2 = vector_set_2.at(j).cardinal(); + auto vector_set_1 = to_vector(set_1); + auto vector_set_2 = to_vector(set_2); - unsigned l = min(l1, l2); + auto new_set_1 = SBG::LIB::SET_FACT.createSet(); + auto new_set_2 = SBG::LIB::SET_FACT.createSet(); + for (size_t i = 0, j = 0; i < vector_set_1.size() and j < vector_set_2.size();) { + unsigned l1 = vector_set_1.at(i).cardinal(); + unsigned l2 = vector_set_2.at(j).cardinal(); - new_set_1.emplaceBack(SBG::LIB::Interval(vector_set_1.at(i).begin()[0].begin(), 1, vector_set_1.at(i).begin()[0].begin() + l - 1)); - new_set_2.emplaceBack(SBG::LIB::Interval(vector_set_2.at(j).begin()[0].begin(), 1, vector_set_2.at(j).begin()[0].begin() + l - 1)); + unsigned l = min(l1, l2); - if (vector_set_1.at(i).begin()[0].begin() + l > vector_set_1.at(i).begin()[0].end()) { - i++; - } else { - vector_set_1[i] = SBG::LIB::Interval(vector_set_1.at(i).begin()[0].begin() + l, 1, vector_set_1.at(i).begin()[0].end()); - } + new_set_1.emplaceBack(SBG::LIB::Interval(vector_set_1.at(i).begin()[0].begin(), 1, vector_set_1.at(i).begin()[0].begin() + l - 1)); + new_set_2.emplaceBack(SBG::LIB::Interval(vector_set_2.at(j).begin()[0].begin(), 1, vector_set_2.at(j).begin()[0].begin() + l - 1)); - if (vector_set_2.at(j).begin()[0].begin() + l > vector_set_2.at(j).begin()[0].end()) { - j++; - } else { - vector_set_2[j] = SBG::LIB::Interval(vector_set_2.at(j).begin()[0].begin() + l, 1, vector_set_2.at(j).begin()[0].end()); - } + if (vector_set_1.at(i).begin()[0].begin() + l > vector_set_1.at(i).begin()[0].end()) { + i++; + } else { + vector_set_1[i] = SBG::LIB::Interval(vector_set_1.at(i).begin()[0].begin() + l, 1, vector_set_1.at(i).begin()[0].end()); } - // logging::sbg_log << "take_granular_option " << new_set_1 << ", " << new_set_2 << endl; - - return {new_set_1, new_set_2}; -} - - -list> granularize_intervals_into_injective_domains(const WeightedSBGraph& sb_graph) -{ - list> linked_intervals; - - for (auto it1 = sb_graph.map1().begin(), it2 = sb_graph.map2().begin(); it1 != sb_graph.map1().end() and it2 != sb_graph.map2().end(); ++it1, ++it2) { - auto slope1 = (*it1).exp().exps()[0].slope(); - auto slope2 = (*it2).exp().exps()[0].slope(); - - if ((slope1 > 0 and slope2 > 0) or (*it1).dom().cardinal() == 1) { - auto im1_ = (*it1).image(); - auto im2_ = (*it2).image(); - - auto [im1, im2] = take_granular_option(im1_, im2_); - - linked_intervals.push_back(make_pair(im1, im2)); - } + if (vector_set_2.at(j).begin()[0].begin() + l > vector_set_2.at(j).begin()[0].end()) { + j++; + } else { + vector_set_2[j] = SBG::LIB::Interval(vector_set_2.at(j).begin()[0].begin() + l, 1, vector_set_2.at(j).begin()[0].end()); } + } - bool change = true; - while (change) { - change = false; - for (const auto& [im1, im2] : linked_intervals) { - for (auto& [p1, p2] : linked_intervals) { - if (not p1.intersection(im1).isEmpty()) { - // logging::sbg_log << "intersection between " << p1 << ", " << im1 << " " << granular_union(im1.intersection(p1), p1) << endl; - auto new_p1 = p1; - auto new_p2 = p2; - new_p1 = granular_union(im1.intersection(new_p1), new_p1); - tie(new_p1, new_p2) = take_granular_option(new_p1, new_p2); - - if (not (set_comparison(p1, new_p1) and set_comparison(p2, new_p2))) { - p1 = new_p1; - p2 = new_p2; - change = true; - } - } - - if (not p2.intersection(im1).isEmpty()) { - // logging::sbg_log << "intersection between " << p2 << ", " << im1 << " " << granular_union(im1.intersection(p2), p2) << endl; - auto new_p1 = p1; - auto new_p2 = p2; - new_p2 = granular_union(im1.intersection(new_p2), new_p2); - tie(new_p1, new_p2) = take_granular_option(new_p1, new_p2); - - if (not (set_comparison(p1, new_p1) and set_comparison(p2, new_p2))) { - p1 = new_p1; - p2 = new_p2; - change = true; - } - } - - if (not p1.intersection(im2).isEmpty()) { - // logging::sbg_log << "intersection between " << p1 << ", " << im2 << " " << granular_union(im2.intersection(p1), p1) << endl; - auto new_p1 = p1; - auto new_p2 = p2; - new_p1 = granular_union(im2.intersection(new_p1), new_p1); - tie(new_p1, new_p2) = take_granular_option(new_p1, new_p2); - - if (not (set_comparison(p1, new_p1) and set_comparison(p2, new_p2))) { - p1 = new_p1; - p2 = new_p2; - change = true; - } - } - - if (not p2.intersection(im2).isEmpty()) { - // logging::sbg_log << "intersection between " << p2 << ", " << im2 << " " << granular_union(im2.intersection(p2), p2) << endl; - auto new_p1 = p1; - auto new_p2 = p2; - new_p2 = granular_union(im2.intersection(new_p2), new_p2); - tie(new_p1, new_p2) = take_granular_option(new_p1, new_p2); - - if (not (set_comparison(p1, new_p1) and set_comparison(p2, new_p2))) { - p1 = new_p1; - p2 = new_p2; - change = true; - } - } - } - } - } + // logging::sbg_log << "take_granular_option " << new_set_1 << ", " << new_set_2 << endl; - return linked_intervals; -} - - -Set convert_linked_intervals_into_sets(const list>& linked_intervals) -{ - auto injective_conn = SBG::LIB::SET_FACT.createSet(); - set points; - for (const auto& [p1, p2] : linked_intervals) { - for (const auto& p : p1){ - points.insert(p[0].begin()); - points.insert(p[0].end() + 1); - } - - for (const auto& p : p2){ - points.insert(p[0].begin()); - points.insert(p[0].end() + 1); - } - } - - logging::sbg_log << "["; - for_each(points.begin(), points.end(), [](auto& p) { logging::sbg_log << p << " "; }); - logging::sbg_log << "]" << endl; - - vector points_vector(points.begin(), points.end()); - - for (size_t i = 0; i < points_vector.size() - 1; i++) { - injective_conn.emplaceBack(SBG::LIB::Interval(points_vector[i], 1, points_vector[i + 1] - 1)); - } - - return injective_conn; -} - -} - - -Set split_nodes_into_injective_domains(const WeightedSBGraph& sb_graph) -{ - auto linked_intervals = granularize_intervals_into_injective_domains(sb_graph); - auto injective_conn = convert_linked_intervals_into_sets(linked_intervals); - cout << "injective_conn: " << injective_conn << endl; - - return injective_conn; + return {new_set_1, new_set_2}; } +} // namespace namespace { -float get_comm_between_two_set_pieces( - const SetPointer& a, - const SetPointer& b, - const CommunicationCostCC& cost_matrix_cc) +float get_comm_between_two_set_pieces(const SetPointer& a, const SetPointer& b, const CommunicationCostCC& cost_matrix_cc) { - auto comm = cost_matrix_cc.get_communication(a.index, b.index); - if (comm == 0) { - return 0; - } - - size_t offset = max(b.offset, a.offset); - size_t end_by_offset = min(b.offset + b.size - 1, a.offset + a.size - 1); - float alpha = 0.; - if (offset <= end_by_offset) { - size_t actual_size = end_by_offset - offset + 1; - assert(a.set_piece.cardinal() == b.set_piece.cardinal()); - alpha = float(actual_size) / a.set_piece.cardinal(); - } - - return comm * alpha; + auto comm = cost_matrix_cc.get_communication(a.index, b.index); + if (comm == 0) { + return 0; + } + + size_t offset = max(b.offset, a.offset); + size_t end_by_offset = min(b.offset + b.size - 1, a.offset + a.size - 1); + float alpha = 0.; + if (offset <= end_by_offset) { + size_t actual_size = end_by_offset - offset + 1; + assert(a.set_piece.cardinal() == b.set_piece.cardinal()); + alpha = float(actual_size) / a.set_piece.cardinal(); + } + + return comm * alpha; } - -float compute_D( - const SetPointer& set_pointer, - const SetPointers& partition, - const SetPointers& complementary_partition, - const CommunicationCostCC& cost_matrix_cc) +float compute_D(const SetPointer& set_pointer, const SetPointers& partition, const SetPointers& complementary_partition, + const CommunicationCostCC& cost_matrix_cc) { - float ic = 0; - for (size_t i = 0; i < partition.size(); i++) { - if (partition.at(i).index == set_pointer.index) { - continue; - } - ic += get_comm_between_two_set_pieces(set_pointer, partition.at(i), cost_matrix_cc); + float ic = 0; + for (size_t i = 0; i < partition.size(); i++) { + if (partition.at(i).index == set_pointer.index) { + continue; } + ic += get_comm_between_two_set_pieces(set_pointer, partition.at(i), cost_matrix_cc); + } - float ec = 0; - for (size_t i = 0; i < complementary_partition.size(); i++) { - ec += get_comm_between_two_set_pieces(set_pointer, complementary_partition.at(i), cost_matrix_cc); - } + float ec = 0; + for (size_t i = 0; i < complementary_partition.size(); i++) { + ec += get_comm_between_two_set_pieces(set_pointer, complementary_partition.at(i), cost_matrix_cc); + } - float D = ec - ic; - return D; + float D = ec - ic; + return D; } - -[[maybe_unused]]unordered_map compute_Ds(const SetPointers& partition, - const SetPointers& complementary_partition, - const CommunicationCostCC& cost_matrix_cc) +[[maybe_unused]] unordered_map compute_Ds(const SetPointers& partition, const SetPointers& complementary_partition, + const CommunicationCostCC& cost_matrix_cc) { - unordered_map Ds = {}; + unordered_map Ds = {}; - for (size_t i = 0; i < partition.size(); i++) { - float D = compute_D(partition.at(i), partition, complementary_partition, cost_matrix_cc); - Ds.insert({partition.at(i).index, D}); - } - - return Ds; -} + for (size_t i = 0; i < partition.size(); i++) { + float D = compute_D(partition.at(i), partition, complementary_partition, cost_matrix_cc); + Ds.insert({partition.at(i).index, D}); + } + return Ds; } +} // namespace -tuple, GainObjects> compute_gains( - const SetPointers& partition_a, - const SetPointers& partition_b, - CommunicationCostCC& cost_matrix_cc) +tuple, GainObjects> compute_gains(const SetPointers& partition_a, const SetPointers& partition_b, + CommunicationCostCC& cost_matrix_cc) { - GainObjects gains; - int max_gain_index = -1; - - for (size_t i = 0; i < partition_a.size(); i++) { + GainObjects gains; + int max_gain_index = -1; - auto p_a_i = partition_a.at(i); - - for (size_t j = 0; j < partition_b.size(); j++) { + for (size_t i = 0; i < partition_a.size(); i++) { + auto p_a_i = partition_a.at(i); - auto p_b_j = partition_b.at(j); + for (size_t j = 0; j < partition_b.size(); j++) { + auto p_b_j = partition_b.at(j); - int s = min(p_a_i.size, p_b_j.size); + int s = min(p_a_i.size, p_b_j.size); - float D_i = compute_D(SetPointer(p_a_i.index, p_a_i.set_piece, p_a_i.offset, p_a_i.size), partition_a, partition_b, cost_matrix_cc); - float D_j = compute_D(SetPointer(p_b_j.index, p_b_j.set_piece, p_b_j.offset, p_b_j.size), partition_b, partition_a, cost_matrix_cc); + float D_i = compute_D(SetPointer(p_a_i.index, p_a_i.set_piece, p_a_i.offset, p_a_i.size), partition_a, partition_b, cost_matrix_cc); + float D_j = compute_D(SetPointer(p_b_j.index, p_b_j.set_piece, p_b_j.offset, p_b_j.size), partition_b, partition_a, cost_matrix_cc); - float gain = D_i + D_j; + float gain = D_i + D_j; - gain -= 2 * get_comm_between_two_set_pieces(p_a_i, p_b_j, cost_matrix_cc); + gain -= 2 * get_comm_between_two_set_pieces(p_a_i, p_b_j, cost_matrix_cc); - gains.emplace_back(p_a_i, p_b_j, s, gain); + gains.emplace_back(p_a_i, p_b_j, s, gain); - if (max_gain_index < 0 or gains.at(max_gain_index).gain < gain) { - max_gain_index = gains.size() - 1; - } - } + if (max_gain_index < 0 or gains.at(max_gain_index).gain < gain) { + max_gain_index = gains.size() - 1; + } } + } - if (max_gain_index < 0) { - return { nullopt, gains }; - } + if (max_gain_index < 0) { + return {nullopt, gains}; + } - return { gains.at(max_gain_index), gains }; + return {gains.at(max_gain_index), gains}; } - namespace { void remove_elements_from_partition(SetPointers& partition, const SetPointers& max_sum_set) { - for (const auto& p : max_sum_set) { - logging::sbg_log << "moving from a " << p << endl; - auto it = find_if(partition.begin(), partition.end(), [&p](const auto& s) { return s.index == p.index and s.offset == p.offset; } ); - assert(it != partition.end()); - logging::sbg_log << "found " << *it << endl; - if (it->size == p.size) { - partition.erase(it); - } else { - logging::sbg_log << "sizes " << it->size << ", " << p.size << endl; - it->size -= p.size; - it->offset += p.size; - logging::sbg_log << "new size " << it->size << endl; - logging::sbg_log << partition << endl; - } + for (const auto& p : max_sum_set) { + logging::sbg_log << "moving from a " << p << endl; + auto it = find_if(partition.begin(), partition.end(), [&p](const auto& s) { return s.index == p.index and s.offset == p.offset; }); + assert(it != partition.end()); + logging::sbg_log << "found " << *it << endl; + if (it->size == p.size) { + partition.erase(it); + } else { + logging::sbg_log << "sizes " << it->size << ", " << p.size << endl; + it->size -= p.size; + it->offset += p.size; + logging::sbg_log << "new size " << it->size << endl; + logging::sbg_log << partition << endl; } + } } - void add_elements_to_partition(SetPointers& partition, const SetPointers& max_sum_set) { - for (const auto& p : max_sum_set) { - assert(p.size > 0); - auto it = find_if(partition.begin(), partition.end(), [&p](const auto& s) { return s.index == p.index; } ); - if (it == partition.end()) { - partition.push_back(p); - } else { - logging::sbg_log << "add_elements_to_partition " << *it << ", " << p.size << ", " << p.offset << " "; - if (it->offset == p.offset) { - cout << "SAME OFFSET! This shouldn't happen " << *it << ", " << p << endl; - it->size += p.size; - } else { - partition.push_back(p); - } - logging::sbg_log << partition << endl; - } + for (const auto& p : max_sum_set) { + assert(p.size > 0); + auto it = find_if(partition.begin(), partition.end(), [&p](const auto& s) { return s.index == p.index; }); + if (it == partition.end()) { + partition.push_back(p); + } else { + logging::sbg_log << "add_elements_to_partition " << *it << ", " << p.size << ", " << p.offset << " "; + if (it->offset == p.offset) { + cout << "SAME OFFSET! This shouldn't happen " << *it << ", " << p << endl; + it->size += p.size; + } else { + partition.push_back(p); + } + logging::sbg_log << partition << endl; } + } } - void flatten_set(SetPointers& set_pointers) { - // 1. Define the custom logic - // Comparator for sorting (Strict Weak Ordering) - auto sortComp = [](const SetPointer& a, const SetPointer& b) { - if (a.index != b.index) { - return a.index < b.index; - } else { - return a.offset < b.offset; - } - }; - - // Predicate for finding duplicates (Equality) - auto areEqual = [](const SetPointer& a, const SetPointer& b) { - return a.index == b.index; - }; - - std::sort(set_pointers.begin(), set_pointers.end(), sortComp); - - // Start searching from the beginning - auto it = set_pointers.begin(); - - while (it != set_pointers.end()) { - // adjacent_find searches for two consecutive identical elements - it = std::adjacent_find(it, set_pointers.end(), areEqual); - - if (it != set_pointers.end()) { - logging::sbg_log << "Duplicate index found: " << it->index - << " (" << *it << " matches " << *(it+1) << ")\n"; - logging::sbg_log << it->set_piece.begin()[0].begin() + it->offset + it->size - << ", " - << (it + 1)->set_piece.begin()[0].begin() + (it + 1)->offset - << endl; - - // if this condition is not achivied, we found - if (it->set_piece.begin()[0].begin() + it->offset + it->size == (it + 1)->set_piece.begin()[0].begin() + (it + 1)->offset) { - it->size += (it+1)->size; - it = set_pointers.erase(it + 1); - } else { - // Skip past the current duplicate to find the next one - // We increment twice to avoid finding the same pair again if there are 3 copies - it++; - } - } + // 1. Define the custom logic + // Comparator for sorting (Strict Weak Ordering) + auto sortComp = [](const SetPointer& a, const SetPointer& b) { + if (a.index != b.index) { + return a.index < b.index; + } else { + return a.offset < b.offset; } + }; + + // Predicate for finding duplicates (Equality) + auto areEqual = [](const SetPointer& a, const SetPointer& b) { return a.index == b.index; }; + + std::sort(set_pointers.begin(), set_pointers.end(), sortComp); + + // Start searching from the beginning + auto it = set_pointers.begin(); + + while (it != set_pointers.end()) { + // adjacent_find searches for two consecutive identical elements + it = std::adjacent_find(it, set_pointers.end(), areEqual); + + if (it != set_pointers.end()) { + logging::sbg_log << "Duplicate index found: " << it->index << " (" << *it << " matches " << *(it + 1) << ")\n"; + logging::sbg_log << it->set_piece.begin()[0].begin() + it->offset + it->size << ", " + << (it + 1)->set_piece.begin()[0].begin() + (it + 1)->offset << endl; + + // if this condition is not achivied, we found + if (it->set_piece.begin()[0].begin() + it->offset + it->size == (it + 1)->set_piece.begin()[0].begin() + (it + 1)->offset) { + it->size += (it + 1)->size; + it = set_pointers.erase(it + 1); + } else { + // Skip past the current duplicate to find the next one + // We increment twice to avoid finding the same pair again if there are 3 copies + it++; + } + } + } } -} - +} // namespace -int bisection( - const SBG::LIB::WeightedSBGraph& graph, - CommunicationCostCC& cost_matrix, - SetPointers& partition_a, - SetPointers& partition_b, - unsigned LMin, - unsigned LMax) +int bisection(const SBG::LIB::WeightedSBGraph& graph, CommunicationCostCC& cost_matrix, SetPointers& partition_a, SetPointers& partition_b, + unsigned LMin, unsigned LMax) { - logging::sbg_log << "calling bisection with\n" << partition_a << "\n" << partition_b << endl; - auto partition_a_copy = partition_a; - auto partition_b_copy = partition_b; - - int par_sum = 0; - int max_par_sum = 0; - pair max_par_sum_set = {}; - auto [max_gain, gm] = compute_gains(partition_a_copy, partition_b_copy, cost_matrix); - - if (not max_gain) { - return 0; + logging::sbg_log << "calling bisection with\n" << partition_a << "\n" << partition_b << endl; + auto partition_a_copy = partition_a; + auto partition_b_copy = partition_b; + + int par_sum = 0; + int max_par_sum = 0; + pair max_par_sum_set = {}; + auto [max_gain, gm] = compute_gains(partition_a_copy, partition_b_copy, cost_matrix); + + if (not max_gain) { + return 0; + } + + SetPointers a_v, b_v; + + assert(gm.size() > 0); + + while (not partition_a_copy.empty() and not partition_b_copy.empty()) { + logging::sbg_log << gm << endl; + logging::sbg_log << "max_gain " << *max_gain << endl; + + // save partial sets and sum + par_sum += max_gain->gain; + a_v.emplace_back(max_gain->a.index, max_gain->a.set_piece, max_gain->a.offset, max_gain->size); + b_v.emplace_back(max_gain->b.index, max_gain->b.set_piece, max_gain->b.offset, max_gain->size); + if (par_sum > max_par_sum) { + max_par_sum = par_sum; + max_par_sum_set = make_pair(a_v, b_v); } - SetPointers a_v, b_v; - - assert(gm.size() > 0); - - while (not partition_a_copy.empty() and not partition_b_copy.empty()) { - logging::sbg_log << gm << endl; - logging::sbg_log << "max_gain " << *max_gain << endl; - - // save partial sets and sum - par_sum += max_gain->gain; - a_v.emplace_back(max_gain->a.index, max_gain->a.set_piece, max_gain->a.offset, max_gain->size); - b_v.emplace_back(max_gain->b.index, max_gain->b.set_piece, max_gain->b.offset, max_gain->size); - if (par_sum > max_par_sum) { - max_par_sum = par_sum; - max_par_sum_set = make_pair(a_v, b_v); - } - - // remove chosen elements for partition a - partition_a_copy.erase(remove(partition_a_copy.begin(), partition_a_copy.end(), max_gain->a)); - if (max_gain->size < max_gain->a.size) { - partition_a_copy.emplace_back(max_gain->a.index, max_gain->a.set_piece, max_gain->a.offset + max_gain->size, max_gain->a.size - max_gain->size); - } + // remove chosen elements for partition a + partition_a_copy.erase(remove(partition_a_copy.begin(), partition_a_copy.end(), max_gain->a)); + if (max_gain->size < max_gain->a.size) { + partition_a_copy.emplace_back(max_gain->a.index, max_gain->a.set_piece, max_gain->a.offset + max_gain->size, + max_gain->a.size - max_gain->size); + } - // remove chosen elements for partition b - partition_b_copy.erase(remove(partition_b_copy.begin(), partition_b_copy.end(), max_gain->b)); - if (max_gain->size < max_gain->b.size) { - partition_b_copy.emplace_back(max_gain->b.index, max_gain->b.set_piece, max_gain->b.offset + max_gain->size, max_gain->b.size - max_gain->size); - } + // remove chosen elements for partition b + partition_b_copy.erase(remove(partition_b_copy.begin(), partition_b_copy.end(), max_gain->b)); + if (max_gain->size < max_gain->b.size) { + partition_b_copy.emplace_back(max_gain->b.index, max_gain->b.set_piece, max_gain->b.offset + max_gain->size, + max_gain->b.size - max_gain->size); + } - // update gains - if (not partition_a_copy.empty() and not partition_b_copy.empty()) { - tie(max_gain, gm) = compute_gains(partition_a_copy, partition_b_copy, cost_matrix); - } + // update gains + if (not partition_a_copy.empty() and not partition_b_copy.empty()) { + tie(max_gain, gm) = compute_gains(partition_a_copy, partition_b_copy, cost_matrix); } + } #ifdef SBG_PARTITIONER_LOGGING - auto prev_a = partition_a; - auto prev_b = partition_b; + auto prev_a = partition_a; + auto prev_b = partition_b; #endif - remove_elements_from_partition(partition_a, max_par_sum_set.first); - remove_elements_from_partition(partition_b, max_par_sum_set.second); + remove_elements_from_partition(partition_a, max_par_sum_set.first); + remove_elements_from_partition(partition_b, max_par_sum_set.second); - add_elements_to_partition(partition_a, max_par_sum_set.second); - add_elements_to_partition(partition_b, max_par_sum_set.first); + add_elements_to_partition(partition_a, max_par_sum_set.second); + add_elements_to_partition(partition_b, max_par_sum_set.first); - flatten_set(partition_a); - flatten_set(partition_b); + flatten_set(partition_a); + flatten_set(partition_b); #ifdef SBG_PARTITIONER_LOGGING - logging::sbg_log << "result:\nmax par sum: " << max_par_sum << "\nprev_a: " << prev_a << "\nprev_b: " << prev_b << "\nmax_par_set: " << max_par_sum_set.first << "\n" << max_par_sum_set.second << "\npartition a: " << partition_a << "\npartition b: " << partition_b << endl; + logging::sbg_log << "result:\nmax par sum: " << max_par_sum << "\nprev_a: " << prev_a << "\nprev_b: " << prev_b + << "\nmax_par_set: " << max_par_sum_set.first << "\n" + << max_par_sum_set.second << "\npartition a: " << partition_a << "\npartition b: " << partition_b << endl; #endif - return max_par_sum; + return max_par_sum; } - PartitionMap rebuild_partitions(const SetPointers& sorted_nodes, const vector& partitions) { - logging::sbg_log << "rebuilding partitions" << endl; - PartitionMap new_partitions; - new_partitions.reserve(partitions.size()); - for (const auto& partition : partitions) { - new_partitions.emplace_back(); - for (const auto& set_pointer : partition) { - unsigned begin = set_pointer.set_piece.begin()[0].begin() + set_pointer.offset; - new_partitions.back().push_back(Interval(begin, 1, begin + set_pointer.size - 1)); - } + logging::sbg_log << "rebuilding partitions" << endl; + PartitionMap new_partitions; + new_partitions.reserve(partitions.size()); + for (const auto& partition : partitions) { + new_partitions.emplace_back(); + for (const auto& set_pointer : partition) { + unsigned begin = set_pointer.set_piece.begin()[0].begin() + set_pointer.offset; + new_partitions.back().push_back(Interval(begin, 1, begin + set_pointer.size - 1)); } + } - return new_partitions; + return new_partitions; } - BisectionResult refine_partitions_function(const WeightedSBGraph& graph, const vector& partitions, CommunicationCostCC& cost_matrix, unsigned LMin, unsigned LMax, list& gains) { - BisectionResult best_gain = BisectionResult{0, 0, -1, {}, {}}; - for (size_t i = 0; i < partitions.size(); i++) { - for (size_t j = i + 1; j < partitions.size(); j++) { - auto gain_comp = [i, j](const BisectionResult& g) { return (g.i == i and g.j == j) or (g.i == j and g.j == i); }; - - auto gain_it = find_if(gains.begin(), gains.end(), gain_comp); - if (gain_it != gains.end()) { - logging::sbg_log << "Between " << i << " and " << j << " was already computed, " << gain_it->gain << endl; - continue; - } - - auto p_1_copy = partitions.at(i); - auto p_2_copy = partitions.at(j); - int current_gain = bisection(graph, cost_matrix, p_1_copy, p_2_copy, LMin, LMax); - logging::sbg_log << "current_gain for " << i << ", " << j << " is " << current_gain << endl; - gains.emplace_back(BisectionResult{i, j, current_gain, p_1_copy, p_2_copy}); - } + BisectionResult best_gain = BisectionResult{0, 0, -1, {}, {}}; + for (size_t i = 0; i < partitions.size(); i++) { + for (size_t j = i + 1; j < partitions.size(); j++) { + auto gain_comp = [i, j](const BisectionResult& g) { return (g.i == i and g.j == j) or (g.i == j and g.j == i); }; + + auto gain_it = find_if(gains.begin(), gains.end(), gain_comp); + if (gain_it != gains.end()) { + logging::sbg_log << "Between " << i << " and " << j << " was already computed, " << gain_it->gain << endl; + continue; + } + + auto p_1_copy = partitions.at(i); + auto p_2_copy = partitions.at(j); + int current_gain = bisection(graph, cost_matrix, p_1_copy, p_2_copy, LMin, LMax); + logging::sbg_log << "current_gain for " << i << ", " << j << " is " << current_gain << endl; + gains.emplace_back(BisectionResult{i, j, current_gain, p_1_copy, p_2_copy}); } + } - for_each(gains.begin(), gains.end(), [&best_gain](const BisectionResult& current_gain) { - if (current_gain.gain > best_gain.gain) { - best_gain = current_gain; - } - }); + for_each(gains.begin(), gains.end(), [&best_gain](const BisectionResult& current_gain) { + if (current_gain.gain > best_gain.gain) { + best_gain = current_gain; + } + }); - return best_gain; + return best_gain; } - void kl_sbg_imbalance_partitioner(const WeightedSBGraph& graph, const SetPointers& sorted_nodes, vector& partitions, CommunicationCostCC& cost_matrix, const float imbalance_epsilon) { - auto [LMin, LMax] = make_pair(0, 0); - bool change = true; - int counter = 0; - - list gains; - while (change) { - cout << "*****ITERATION NUMBER " << counter++ << endl; - change = false; - - BisectionResult best_gain; - best_gain = refine_partitions_function(graph, partitions, cost_matrix, LMin, LMax, gains); - - logging::sbg_log << "Best gain results is: " << best_gain.gain << endl; - logging::sbg_log << "How many gains: " << gains.size() << endl; - - auto gain_comp = [&best_gain](const BisectionResult& g) { - return g.i == best_gain.i or g.j == best_gain.j or g.i == best_gain.j or g.j == best_gain.i; - }; - - int it_counter = 0; - cout << "best gain: " << best_gain.gain << endl; - while ((not gains.empty()) and best_gain.gain > 0) { - logging::sbg_log << "change number " << it_counter << " changing " << best_gain.i << ", " << best_gain.j << endl; - it_counter++; - change = true; - partitions[best_gain.i] = best_gain.A; - partitions[best_gain.j] = best_gain.B; - if (sanity_check_enabled) { - sanity_check(graph, rebuild_partitions(sorted_nodes, partitions), partitions.size()); - } + auto [LMin, LMax] = make_pair(0, 0); + bool change = true; + int counter = 0; - gains.erase(std::remove_if(gains.begin(), gains.end(), gain_comp), gains.end()); + list gains; + while (change) { + cout << "*****ITERATION NUMBER " << counter++ << endl; + change = false; - logging::sbg_log << "best gain is " << best_gain.gain << endl; - logging::sbg_log << "gains length " << gains.size() << endl; + BisectionResult best_gain; + best_gain = refine_partitions_function(graph, partitions, cost_matrix, LMin, LMax, gains); - if (not gains.empty()) { - auto max_gain_it = max_element(gains.begin(), gains.end(), - [](const BisectionResult& a, const BisectionResult& b) { return a.gain < b.gain; }); - assert(max_gain_it != gains.end()); - if (max_gain_it != gains.end()) { - best_gain = *max_gain_it; - } else { - best_gain = BisectionResult{0, 0, -Inf, {}, {}}; - } - } + logging::sbg_log << "Best gain results is: " << best_gain.gain << endl; + logging::sbg_log << "How many gains: " << gains.size() << endl; + + auto gain_comp = [&best_gain](const BisectionResult& g) { + return g.i == best_gain.i or g.j == best_gain.j or g.i == best_gain.j or g.j == best_gain.i; + }; + + int it_counter = 0; + cout << "best gain: " << best_gain.gain << endl; + while ((not gains.empty()) and best_gain.gain > 0) { + logging::sbg_log << "change number " << it_counter << " changing " << best_gain.i << ", " << best_gain.j << endl; + it_counter++; + change = true; + partitions[best_gain.i] = best_gain.A; + partitions[best_gain.j] = best_gain.B; + if (sanity_check_enabled) { + sanity_check(graph, rebuild_partitions(sorted_nodes, partitions), partitions.size()); + } + + gains.erase(std::remove_if(gains.begin(), gains.end(), gain_comp), gains.end()); + + logging::sbg_log << "best gain is " << best_gain.gain << endl; + logging::sbg_log << "gains length " << gains.size() << endl; + + if (not gains.empty()) { + auto max_gain_it = + max_element(gains.begin(), gains.end(), [](const BisectionResult& a, const BisectionResult& b) { return a.gain < b.gain; }); + assert(max_gain_it != gains.end()); + if (max_gain_it != gains.end()) { + best_gain = *max_gain_it; + } else { + best_gain = BisectionResult{0, 0, -Inf, {}, {}}; } + } } + } } - -vector best_initial_partition( - const WeightedSBGraph& graph, - const SetPointers& sorted_nodes, - const CommunicationCostCC& comm_cc, - unsigned number_of_partitions, - const InitialPartitionStrategy strategy) +vector best_initial_partition(const WeightedSBGraph& graph, const SetPointers& sorted_nodes, + const CommunicationCostCC& comm_cc, unsigned number_of_partitions, + const InitialPartitionStrategy strategy) { - unsigned edge_cut = 0; - vector partitions; - partitions.reserve(number_of_partitions); - - const bool all_strategies = strategy == InitialPartitionStrategy::ALL; - if (all_strategies or strategy == InitialPartitionStrategy::DFS_GREEDY_POSTORDER or strategy == InitialPartitionStrategy::DFS_GREEDY_PREORDER) { - auto nodes_to_add = sorted_nodes; - float expected_size_ = float(graph.V().cardinal()); - expected_size_ /= number_of_partitions; - unsigned expected_size = ceil(expected_size_); - - for (unsigned i = 0; i < number_of_partitions; i++) { - unsigned current_size = 0; - partitions.emplace_back(); - for (auto it = nodes_to_add.begin(); it != nodes_to_add.end();) { - if (it->size + current_size > expected_size) { - unsigned s = expected_size - current_size; - partitions[i].emplace_back(it->index, it->set_piece, it->offset, s); - it->size -= s; - it->offset += s; - current_size += s; - // we don't update the iterator in this case - } else { - partitions[i].push_back(*it); - current_size += it->size; - it = nodes_to_add.erase(it); - } - - if (current_size >= expected_size and i < number_of_partitions - 1) { - break; - } - } + unsigned edge_cut = 0; + vector partitions; + partitions.reserve(number_of_partitions); + + const bool all_strategies = strategy == InitialPartitionStrategy::ALL; + if (all_strategies or strategy == InitialPartitionStrategy::DFS_GREEDY_POSTORDER or + strategy == InitialPartitionStrategy::DFS_GREEDY_PREORDER) { + auto nodes_to_add = sorted_nodes; + float expected_size_ = float(graph.V().cardinal()); + expected_size_ /= number_of_partitions; + unsigned expected_size = ceil(expected_size_); + + for (unsigned i = 0; i < number_of_partitions; i++) { + unsigned current_size = 0; + partitions.emplace_back(); + for (auto it = nodes_to_add.begin(); it != nodes_to_add.end();) { + if (it->size + current_size > expected_size) { + unsigned s = expected_size - current_size; + partitions[i].emplace_back(it->index, it->set_piece, it->offset, s); + it->size -= s; + it->offset += s; + current_size += s; + // we don't update the iterator in this case + } else { + partitions[i].push_back(*it); + current_size += it->size; + it = nodes_to_add.erase(it); } + if (current_size >= expected_size and i < number_of_partitions - 1) { + break; + } + } + } + #ifdef SBG_PARTITIONER_LOGGING - auto partitions_map = sbg_partitioner::using_cc::rebuild_partitions(sorted_nodes, partitions); - sanity_check(graph, partitions_map, number_of_partitions); + auto partitions_map = sbg_partitioner::using_cc::rebuild_partitions(sorted_nodes, partitions); + sanity_check(graph, partitions_map, number_of_partitions); #endif - for (size_t i = 0; i < number_of_partitions; i++) { - const auto& p_i = partitions.at(i); - for (size_t j = i + 1; j < number_of_partitions; j++) { - const auto& p_j = partitions.at(j); - for (const auto& set_piece_i : p_i) { - for (const auto& set_piece_j : p_j) { - edge_cut += comm_cc.get_communication(set_piece_i.index, set_piece_j.index); - } - } - } + for (size_t i = 0; i < number_of_partitions; i++) { + const auto& p_i = partitions.at(i); + for (size_t j = i + 1; j < number_of_partitions; j++) { + const auto& p_j = partitions.at(j); + for (const auto& set_piece_i : p_i) { + for (const auto& set_piece_j : p_j) { + edge_cut += comm_cc.get_communication(set_piece_i.index, set_piece_j.index); + } } + } } + } + + if (all_strategies or strategy == InitialPartitionStrategy::DFS_DISTRIBUTIVE_PREORDER or + strategy == InitialPartitionStrategy::DFS_DISTRIBUTIVE_POSTORDER) { + vector size_by_partition(number_of_partitions, 0); + vector temp_partition = vector(number_of_partitions, SetPointers()); + + for (auto v : sorted_nodes) { + int quotient = v.size / number_of_partitions; + int rest = v.size % number_of_partitions; + logging::sbg_log << v << " size " << v.size << " quotient " << quotient << ", rest " << rest << endl; + + size_t current_offset = 0; + unsigned min_size = 0; + + if (rest > 0) { + for (size_t i = 1; i < number_of_partitions; i++) { + if (size_by_partition.at(i) < size_by_partition.at(min_size)) { + min_size = i; + } + } + } - if (all_strategies or strategy == InitialPartitionStrategy::DFS_DISTRIBUTIVE_PREORDER or strategy == InitialPartitionStrategy::DFS_DISTRIBUTIVE_POSTORDER) { - vector size_by_partition(number_of_partitions, 0); - vector temp_partition = vector(number_of_partitions, SetPointers()); - - for (auto v : sorted_nodes) { - int quotient = v.size / number_of_partitions; - int rest = v.size % number_of_partitions; - logging::sbg_log << v << " size " << v.size << " quotient " << quotient << ", rest " << rest << endl; - - size_t current_offset = 0; - unsigned min_size = 0; - - if (rest > 0) { - for (size_t i = 1; i < number_of_partitions; i++) { - if (size_by_partition.at(i) < size_by_partition.at(min_size)) { - min_size = i; - } - } - } - - if (quotient > 0) { - - for (size_t i = 0; i < number_of_partitions; i++) { - auto current_quotient = quotient; - if (rest > 0 and i == min_size){ - current_quotient += rest; - } - - temp_partition[i].emplace_back(v.index, v.set_piece, current_offset, current_quotient); - size_by_partition[i] += current_quotient; - current_offset += current_quotient; - } - } else { - temp_partition[min_size].emplace_back(v.index, v.set_piece, current_offset, rest); - } - // // if quotient is greater than 0, we need to extend one of the intervals - // if (quotient > 0) { - // // the last one inserted is `v`, so we add rest to it - // assert(v.set_piece == temp_partition[min_size].back().set_piece); - // temp_partition[min_size].back().size += rest; - // size_by_partition[min_size] += rest; - // } else { // quotient is 0, we need to insert it - // temp_partition[min_size].push_back(v); - // } - // } + if (quotient > 0) { + for (size_t i = 0; i < number_of_partitions; i++) { + auto current_quotient = quotient; + if (rest > 0 and i == min_size) { + current_quotient += rest; + } + + temp_partition[i].emplace_back(v.index, v.set_piece, current_offset, current_quotient); + size_by_partition[i] += current_quotient; + current_offset += current_quotient; } + } else { + temp_partition[min_size].emplace_back(v.index, v.set_piece, current_offset, rest); + } + // // if quotient is greater than 0, we need to extend one of the intervals + // if (quotient > 0) { + // // the last one inserted is `v`, so we add rest to it + // assert(v.set_piece == temp_partition[min_size].back().set_piece); + // temp_partition[min_size].back().size += rest; + // size_by_partition[min_size] += rest; + // } else { // quotient is 0, we need to insert it + // temp_partition[min_size].push_back(v); + // } + // } + } #ifdef SBG_PARTITIONER_LOGGING - auto partitions_map = sbg_partitioner::using_cc::rebuild_partitions(sorted_nodes, temp_partition); - sanity_check(graph, partitions_map, number_of_partitions); + auto partitions_map = sbg_partitioner::using_cc::rebuild_partitions(sorted_nodes, temp_partition); + sanity_check(graph, partitions_map, number_of_partitions); #endif - unsigned this_edge_cut = 0; - if (not partitions.empty()) { - for (size_t i = 0; i < number_of_partitions; i++) { - const auto& p_i = partitions.at(i); - for (size_t j = i + 1; j < number_of_partitions; j++) { - const auto& p_j = partitions.at(j); - for (const auto& set_piece_i : p_i) { - for (const auto& set_piece_j : p_j) { - this_edge_cut += comm_cc.get_communication(set_piece_i.index, set_piece_j.index); - } - } - } - } - - logging::sbg_log << "this_edge_cut " << this_edge_cut << " edge_cut " << edge_cut << endl; - if (this_edge_cut < edge_cut) { - partitions = temp_partition; + unsigned this_edge_cut = 0; + if (not partitions.empty()) { + for (size_t i = 0; i < number_of_partitions; i++) { + const auto& p_i = partitions.at(i); + for (size_t j = i + 1; j < number_of_partitions; j++) { + const auto& p_j = partitions.at(j); + for (const auto& set_piece_i : p_i) { + for (const auto& set_piece_j : p_j) { + this_edge_cut += comm_cc.get_communication(set_piece_i.index, set_piece_j.index); } - } else { - partitions = temp_partition; - } + } + } + } + + logging::sbg_log << "this_edge_cut " << this_edge_cut << " edge_cut " << edge_cut << endl; + if (this_edge_cut < edge_cut) { + partitions = temp_partition; + } + } else { + partitions = temp_partition; } + } #ifdef SBG_PARTITIONER_LOGGING - auto partitions_as_set = sbg_partitioner::using_cc::rebuild_partitions(sorted_nodes, partitions); - sanity_check(graph, partitions_as_set, number_of_partitions); + auto partitions_as_set = sbg_partitioner::using_cc::rebuild_partitions(sorted_nodes, partitions); + sanity_check(graph, partitions_as_set, number_of_partitions); #endif - return partitions; + return partitions; } -} +} // namespace using_cc -} +} // namespace sbg_partitioner diff --git a/algorithms/partitioner/partition_graph_cc.hpp b/algorithms/partitioner/partition_graph_cc.hpp index 719caf15..9b732339 100644 --- a/algorithms/partitioner/partition_graph_cc.hpp +++ b/algorithms/partitioner/partition_graph_cc.hpp @@ -52,9 +52,6 @@ std::vector best_initial_partition( const InitialPartitionStrategy strategy); -SBG::LIB::Set split_nodes_into_injective_domains(const SBG::LIB::WeightedSBGraph& sb_graph); - - int bisection( const SBG::LIB::WeightedSBGraph& graph, CommunicationCostCC& cost_matrix, diff --git a/algorithms/partitioner/partition_metrics_api.cpp b/algorithms/partitioner/partition_metrics_api.cpp index 4202fb5d..c5ac7aed 100644 --- a/algorithms/partitioner/partition_metrics_api.cpp +++ b/algorithms/partitioner/partition_metrics_api.cpp @@ -16,9 +16,12 @@ ******************************************************************************/ +#include #include #include +#include +#include #include "build_sb_graph.hpp" #include "partition_metrics_api.hpp" @@ -27,210 +30,279 @@ using namespace std; using namespace SBG::LIB; +using SBG::LIB::detail::Interval; +using SBG::LIB::detail::MultiDimInter; +using SBG::LIB::detail::OrderedSet; +using SBG::LIB::detail::SetAccess; +using SBG::LIB::detail::SetAccessKey; using namespace SBG::Util; -namespace sbg_partitioner{ +namespace sbg_partitioner { namespace metrics { namespace { -Set get_edge_cut( - const Set& partition, - const PWMap& maps_1, - const PWMap& maps_2) +Set get_edge_cut(const Set& partition, const PWMap& maps_1, const PWMap& maps_2) { - auto d = maps_1.preImage(partition); - auto im = maps_2.image(d); - auto ec_nodes = im.difference(partition); - auto external_communication = maps_2.preImage(ec_nodes); - // avoid oversizing external communication - external_communication = external_communication.intersection(d); - - return external_communication; + auto d = maps_1.preImage(partition); + auto im = maps_2.image(d); + auto ec_nodes = im.difference(partition); + auto external_communication = maps_2.preImage(ec_nodes); + // avoid oversizing external communication + external_communication = external_communication.intersection(d); + + return external_communication; } - -int communication_volume_one_dim(const PartitionMap& partitions, const SetPiece& v, const WeightedSBGraph& sb_graph, unsigned i) +int communication_volume_one_dim(const PartitionMap& partitions, const MultiDimInter& v, const WeightedSBGraph& sb_graph, unsigned i) { - // avoid repeating this - auto look_for_adjacents = [](const Set& nodes, const PWMap& map1, const PWMap& map2) { - auto involved_edges1 = map1.preImage(nodes); - auto arrival_nodes1 = map2.image(involved_edges1); - arrival_nodes1 = arrival_nodes1.difference(nodes); - - auto involved_edges2 = map2.preImage(nodes); - auto arrival_nodes2 = map1.image(involved_edges2); - arrival_nodes2 = arrival_nodes2.difference(nodes); - - auto arrival_nodes = arrival_nodes1.cup(arrival_nodes2); - - return arrival_nodes; - }; - - int acc = 0; - for (unsigned value = v.intervals()[0].begin(); value <= v.intervals()[0].end(); value += v.intervals()[0].step()) { - int local_total_vol = 0; - Interval set_piece = Interval(value, v.intervals()[0].step(), value); - auto s = SET_FACT.createSet(set_piece); - const Set adjacents = look_for_adjacents(s, sb_graph.map1(), sb_graph.map2()); - - for (unsigned j = 0; j < partitions.size(); j++) { - if (i == j) { - continue; - } - - const auto& p_set = partitions.at(j); - - if (not adjacents.intersection(from_vector(p_set)).isEmpty()) { - local_total_vol += 1; - } - } - - acc += local_total_vol; + SetAccessKey key = SetAccess::key(); + // avoid repeating this + auto look_for_adjacents = [](const Set& nodes, const PWMap& map1, const PWMap& map2) { + auto involved_edges1 = map1.preImage(nodes); + auto arrival_nodes1 = map2.image(involved_edges1); + arrival_nodes1 = arrival_nodes1.difference(nodes); + + auto involved_edges2 = map2.preImage(nodes); + auto arrival_nodes2 = map1.image(involved_edges2); + arrival_nodes2 = arrival_nodes2.difference(nodes); + + auto arrival_nodes = arrival_nodes1.cup(arrival_nodes2); + + return arrival_nodes; + }; + + int acc = 0; + for (unsigned value = v[0].begin(); value <= v[0].end(); value += v[0].step()) { + int local_total_vol = 0; + Interval set_piece = Interval(value, v[0].step(), value); + auto s = key.createSet(OrderedSet(set_piece)); + const Set adjacents = look_for_adjacents(s, sb_graph.map1(), sb_graph.map2()); + + for (unsigned j = 0; j < partitions.size(); j++) { + if (i == j) { + continue; + } + + const auto& p_set = partitions.at(j); + + if (not adjacents.intersection(from_vector(p_set)).isEmpty()) { + local_total_vol += 1; + } } - return acc; -} + acc += local_total_vol; + } + return acc; +} +// ver void write_node_by_partition(const PartitionMap& partitions, const WeightedSBGraph& sb_graph) { - Partition nodes; - nodes.reserve(sb_graph.V().size()); - for (auto v : sb_graph.V()) { - nodes.push_back(v); - } - - sort_partition_intervals(nodes); - - // expand it and write it - vector partition_by_node; - ofstream output_file("output.txt"); - for (const auto& n : nodes) { - for (unsigned v_0 = n.intervals()[0].begin(); v_0 <= n.intervals()[0].end(); v_0++) { - SetPiece set_piece; - set_piece.emplaceBack(Interval(v_0, n.intervals()[0].step(), v_0)); - for (size_t i = 0; i < partitions.size(); i++) { - Partition p = partitions.at(i); - auto p_set = from_vector(p); - if (not SET_FACT.createSet(set_piece).intersection(p_set).isEmpty()) { - partition_by_node.push_back(i); - output_file << to_string(i) << endl; - break; - } - } - } - } + // Partition nodes; + // nodes.reserve(sb_graph.V().size()); + // for (auto v : sb_graph.V()) { + // nodes.push_back(v); + // } + + // sort_partition_intervals(nodes); + + // // expand it and write it + // vector partition_by_node; + // ofstream output_file("output.txt"); + // for (const auto& n : nodes) { + // for (unsigned v_0 = n[0].begin(); v_0 <= n[0].end(); v_0++) { + // MultiDimInter set_piece; + // set_piece.emplaceBack(Interval(v_0, n[0].step(), v_0)); + // for (size_t i = 0; i < partitions.size(); i++) { + // Partition p = partitions.at(i); + // auto p_set = from_vector(p); + // if (not create_set(set_piece).intersection(p_set).isEmpty()) { + // partition_by_node.push_back(i); + // output_file << to_string(i) << endl; + // break; + // } + // } + // } + // } } -} +} // namespace int edge_cut(const PartitionMap& partitions, const WeightedSBGraph& sb_graph) { - Set ec = SET_FACT.createSet(); - const auto& maps_1 = sb_graph.map1(); - const auto& maps_2 = sb_graph.map2(); - for (size_t i = 0; i < partitions.size(); i++) { - Set partition = from_vector(partitions.at(i)); - ec = ec.cup(get_edge_cut(partition, maps_1, maps_2)); - ec = ec.cup(get_edge_cut(partition, maps_2, maps_1)); + cout << "computing edge cut " << endl; + Set ec; + const auto& maps_1 = sb_graph.map1(); + const auto& maps_2 = sb_graph.map2(); + for (size_t i = 0; i < partitions.size(); i++) { + cout << "partition " << i << endl; + Set partition = from_vector(partitions.at(i)); + ec = ec.cup(get_edge_cut(partition, maps_1, maps_2)); + ec = ec.cup(get_edge_cut(partition, maps_2, maps_1)); + } + + int weight = get_edge_set_cost(ec, sb_graph.get_edge_costs()); + cout << "ec is " << weight << endl; + + return weight; +} + +// ver +pair communication_volume(const PartitionMap& partitions, const WeightedSBGraph& sb_graph) +{ + int comm_vol = 0; + int max_comm_vol = 0; + for (unsigned i = 0; i < partitions.size(); i++) { + int communication_volume_partition = 0; + const auto& p = partitions.at(i); + + for (const auto& v : p) { + // communication_volume_partition += communication_volume_one_dim(partitions, v, sb_graph, i); } - int weight = get_edge_set_cost(ec, sb_graph.get_edge_costs()); + comm_vol += communication_volume_partition; + max_comm_vol = max(communication_volume_partition, max_comm_vol); + } - return weight; + return {comm_vol, max_comm_vol}; } - -pair communication_volume(const PartitionMap& partitions, const WeightedSBGraph& sb_graph) +float maximum_imbalance(const PartitionMap& partitions, const WeightedSBGraph& sb_graph) { - int comm_vol = 0; - int max_comm_vol = 0; - for (unsigned i = 0; i < partitions.size(); i++) { - int communication_volume_partition = 0; - const auto& p = partitions.at(i); - - for (const auto& v : p) { - communication_volume_partition += communication_volume_one_dim(partitions, v, sb_graph, i); - } + unsigned number_of_nodes = get_node_size(sb_graph.V(), sb_graph.get_node_weights()); + float expected_imb = number_of_nodes / partitions.size(); - comm_vol += communication_volume_partition; - max_comm_vol = max(communication_volume_partition, max_comm_vol); - } + float max_imbalance = 0.; + for (const auto& p : partitions) { + unsigned size_of_p = get_partition_size(p, sb_graph.get_node_weights()); + float imbalance_p = abs(expected_imb - float(size_of_p)) / expected_imb; + max_imbalance = max(max_imbalance, imbalance_p); + } - return {comm_vol, max_comm_vol}; -} + write_node_by_partition(partitions, sb_graph); + return max_imbalance; +} -float maximum_imbalance(const PartitionMap& partitions, const WeightedSBGraph& sb_graph) +// ver +PartitionMap read_partition_from_file(const string& name, const WeightedSBGraph& sb_graph, int number_of_partitions) { - unsigned number_of_nodes = get_node_size(sb_graph.V(), sb_graph.get_node_weights()); - float expected_imb = number_of_nodes / partitions.size(); - - float max_imbalance = 0.; - for (const auto& p : partitions) { - unsigned size_of_p = get_partition_size(p, sb_graph.get_node_weights()); - float imbalance_p = abs(expected_imb - float(size_of_p)) / expected_imb; - max_imbalance = max(max_imbalance, imbalance_p); + ifstream file(name); + string line; // String to store each line of the file. + + PartitionMap partitions(number_of_partitions, Partition()); + if (file.is_open()) { + // Read each line from the file and store it in the + // 'line' variable. + int node_counter = 0; + while (getline(file, line)) { + // partitions[stoi(line)].push_back({Interval(node_counter, 1, node_counter)}); + node_counter++; } - write_node_by_partition(partitions, sb_graph); + // Close the file stream once all lines have been + // read. + file.close(); + } else { + // Print an error message to the standard error + // stream if the file cannot be opened. + cerr << "Unable to open file! " << name << endl; + } - return max_imbalance; + return partitions; } - -PartitionMap read_partition_from_file(const string& name, const WeightedSBGraph& sb_graph) +ostream& operator<<(ostream& os, const communication_metrics& comm_metrics) { + os << "communication metrics, edge cut: " << comm_metrics.edge_cut << ", communication volume: " << comm_metrics.comm_volume + << ", maximum communication volume: " << comm_metrics.max_comm_volume << ", maximum imbalance: " << comm_metrics.maximum_imbalance; - ifstream file(name); - string line; // String to store each line of the file. - - PartitionMap partitions; - if (file.is_open()) { - // Read each line from the file and store it in the - // 'line' variable. - int node_counter = 0; - while (getline(file, line)) { - partitions.push_back({Interval(node_counter, 1, node_counter)}); - node_counter++; - } + return os; +} - for (auto& s: partitions) { - auto s_set = from_vector(s); - flatten_set(s_set, sb_graph); - s = to_vector(s_set); +void dump_results(const SBG::LIB::WeightedSBGraph& sb_graph, const std::string& filename, const PartitionMap& partition) +{ + SetAccessKey key = SetAccess::key(); + ofstream edges_file(std::filesystem::path(filename).stem().string() + "_edges.txt"); + auto edges = sb_graph.E(); + edges.compact(); + if (edges.arity() == 2) { + size_t row_size = key.pieces(get(key.impl(sb_graph.V()))).front()[0].cardinal(); + for (const auto& edge : key.pieces(get(key.impl(edges)))) { + auto departure = key.pieces(get(key.impl(sb_graph.map1().image(key.createSet(OrderedSet(edge)))))); + auto arrival = key.pieces(get(key.impl(sb_graph.map2().image(key.createSet(OrderedSet(edge)))))); + if (departure.size() > 1 or arrival.size() > 1) { + cout << departure.size() << "\n" << arrival.size() << endl; + cout << "we are not ready for this" << endl; + throw 1; + } + + for (size_t piece_idx = 0; piece_idx < departure.size() and piece_idx < arrival.size(); piece_idx++) { + // set pieces + const auto& set_piece_dep = departure[piece_idx]; + const auto& set_piece_arr = arrival[piece_idx]; + for (size_t dep_row_val = set_piece_dep[0].begin(), arr_row_val = set_piece_arr[0].begin(); + dep_row_val <= size_t(set_piece_dep[0].end()) and arr_row_val <= size_t(set_piece_arr[0].end()); + dep_row_val++, arr_row_val++) { + for (size_t dep_col_val = set_piece_dep[1].begin(), arr_col_val = set_piece_arr[1].begin(); + dep_col_val <= size_t(set_piece_dep[1].end()) and arr_col_val <= size_t(set_piece_arr[1].end()); + dep_col_val++, arr_col_val++) { + size_t dep_val = dep_row_val * row_size + dep_col_val; + size_t arr_val = arr_row_val * row_size + arr_col_val; + edges_file << dep_val << " " << arr_val << endl; + assert(sb_graph.V().cardinal() > dep_val); + assert(sb_graph.V().cardinal() > arr_val); + } } - - // Close the file stream once all lines have been - // read. - file.close(); + } } - else { - // Print an error message to the standard error - // stream if the file cannot be opened. - cerr << "Unable to open file! " << name << endl; + } else { + for (size_t i = key.pieces(get(key.impl(edges))).front()[0].begin(); + i <= size_t(key.pieces(get(key.impl(edges))).front()[0].end()); i++) { + auto departure = + key.pieces(get(key.impl(sb_graph.map1().image(key.createSet(OrderedSet(Interval(i))))))).front()[0].begin(); + auto arrival = + key.pieces(get(key.impl(sb_graph.map2().image(key.createSet(OrderedSet(Interval(i))))))).front()[0].begin(); + edges_file << departure << " " << arrival << endl; } + } + cout << "connections ok" << endl; + + vector partition_vector(sb_graph.V().cardinal(), 0); + if (sb_graph.V().arity() == 2) { + size_t row_size = key.pieces(get(key.impl(sb_graph.V()))).front()[0].cardinal(); + for (size_t i = 0; i < partition.size(); i++) { + const auto& p = partition.at(i); + for (const auto& set : p) { + for (const auto& sp : key.pieces(get(key.impl(set)))) { + for (size_t row = sp[0].begin(); row <= size_t(sp[0].end()); row++) { + for (size_t col = sp[1].begin(); col <= size_t(sp[1].end()); col++) { + auto val = row * row_size + col; + partition_vector[val] = i; + } + } + } + } + } + } else { + for (unsigned i = 0; i < partition.size(); i++) { + for (size_t vert_idx = 0; vert_idx < partition.at(i).size(); vert_idx++) { + const auto first_interval = key.pieces(get(key.impl(partition.at(i).at(vert_idx)))).front()[0]; + for (size_t val = first_interval.begin(); val <= size_t(first_interval.end()); val++) { + partition_vector[val] = i; + } + } + } + } + cout << "parts ok" << endl; - return partitions; -} - - -ostream& operator<<(ostream& os, const communication_metrics& comm_metrics) -{ - os << "communication metrics, edge cut: " - << comm_metrics.edge_cut - << ", communication volume: " - << comm_metrics.comm_volume - << ", maximum communication volume: " - << comm_metrics.max_comm_volume - << ", maximum imbalance: " - << comm_metrics.maximum_imbalance; - - return os; + ofstream parts_file(std::filesystem::path(filename).stem().string() + "_parts.txt"); + for_each(partition_vector.begin(), partition_vector.end(), [&parts_file](const auto val) { parts_file << val << "\n"; }); } -} +} // namespace metrics -} +} // namespace sbg_partitioner diff --git a/algorithms/partitioner/partition_metrics_api.hpp b/algorithms/partitioner/partition_metrics_api.hpp index 39bf05e6..1e631a4d 100644 --- a/algorithms/partitioner/partition_metrics_api.hpp +++ b/algorithms/partitioner/partition_metrics_api.hpp @@ -41,9 +41,11 @@ std::pair communication_volume( float maximum_imbalance(const PartitionMap& partitions, const SBG::LIB::WeightedSBGraph& sb_graph); -PartitionMap read_partition_from_file(const std::string& name, const SBG::LIB::WeightedSBGraph& sb_graph); +PartitionMap read_partition_from_file(const std::string& name, const SBG::LIB::WeightedSBGraph& sb_graph, int number_of_partitions); std::ostream& operator<<(std::ostream& os, const communication_metrics& comm_metrics); + +void dump_results(const SBG::LIB::WeightedSBGraph& sb_graph, const std::string& filename, const PartitionMap& partition); } } diff --git a/algorithms/partitioner/partition_strategy.cpp b/algorithms/partitioner/partition_strategy.cpp index 23a56411..39e19cd0 100644 --- a/algorithms/partitioner/partition_strategy.cpp +++ b/algorithms/partitioner/partition_strategy.cpp @@ -19,6 +19,8 @@ #include +#include + #include "build_sb_graph.hpp" #include "partition_strategy.hpp" #include "sbg_partitioner_log.hpp" @@ -28,33 +30,33 @@ using namespace std; using namespace SBG::LIB; +using SBG::LIB::detail::OrderedSet; +using SBG::LIB::detail::SetAccess; +using SBG::LIB::detail::SetAccessKey; namespace sbg_partitioner { PartitionStrategyGreedy::PartitionStrategyGreedy(unsigned number_of_partitions, const SBG::LIB::WeightedSBGraph graph) - : PartitionStrategy(), - _number_of_partitions(number_of_partitions), - _current_partition(0), - _node_weight(graph.get_node_weights()) + : PartitionStrategy(), _number_of_partitions(number_of_partitions), _current_partition(0), _node_weight(graph.get_node_weights()) { // get total of nodes by accumulating all interval values _total_of_nodes = get_node_size(graph.V(), SBG::LIB::NodeWeight()); size_t actual_total_of_nodes = get_node_size(graph.V(), _node_weight); - unsigned min_amount_by_partition = actual_total_of_nodes / number_of_partitions; - unsigned surplus = actual_total_of_nodes % number_of_partitions; + size_t min_amount_by_partition = actual_total_of_nodes / number_of_partitions; + size_t surplus = actual_total_of_nodes % number_of_partitions; // unsigned acceptable_surplus = ceil(min_amount_by_partition * 0.05); // logging::sbg_log << "PartitionStrategyGreedy::PartitionStrategyGreedy " << actual_total_of_nodes << ", " << min_amount_by_partition << // ", " << surplus << endl; - _expected_size_by_partition = min_amount_by_partition + surplus; + _expected_size_by_partition = min_amount_by_partition; // + surplus; for (unsigned i = 0; i < number_of_partitions; i++) { _current_size_by_partition[i] = 0; } } -vector sort_keys_by_value(const map& current_size_by_partition) +vector sort_keys_by_value(const map& current_size_by_partition) { vector keys; keys.reserve(current_size_by_partition.size()); @@ -70,23 +72,23 @@ vector sort_keys_by_value(const map& current_size_ return keys; } -void PartitionStrategyGreedy::operator()(const SetPiece& node) +void PartitionStrategyGreedy::operator()(const Set& node_to_be_added) { + SetAccessKey key = SetAccess::key(); #if DEBUG_PARTITION_STRATEGY_ENABLED logging::sbg_log << "Adding node " << node << endl; #endif - Set node_to_be_added = SET_FACT.createSet(node); // let's just work with sizes - map size_by_partition; - unsigned pending_node_elements = get_node_size(node, SBG::LIB::NodeWeight()); - int node_weight = get_set_cost(node, _node_weight); + map size_by_partition; + size_t pending_node_elements = get_node_size(node_to_be_added, SBG::LIB::NodeWeight()); + int node_weight = get_set_cost(node_to_be_added, _node_weight); auto keys_sort_by_value = sort_keys_by_value(_current_size_by_partition); for (const auto i : keys_sort_by_value) { if (_expected_size_by_partition > _current_size_by_partition[i]) { // nothing to do for now auto available = _expected_size_by_partition - _current_size_by_partition[i]; - unsigned elements_can_take = available / node_weight; + size_t elements_can_take = available / node_weight; elements_can_take = min(pending_node_elements, elements_can_take); pending_node_elements -= elements_can_take; size_by_partition[i] = elements_can_take * node_weight; @@ -99,13 +101,13 @@ void PartitionStrategyGreedy::operator()(const SetPiece& node) // look for the partion that has least elements unsigned p = keys_sort_by_value.front(); - unsigned to_be_added = pending_node_elements * node_weight; + size_t to_be_added = pending_node_elements * node_weight; size_by_partition[p] += to_be_added; _current_size_by_partition[p] += to_be_added; } - Set remaining_node = SET_FACT.createSet(node); + Set remaining_node = node_to_be_added; for (unsigned i = 0; i < _number_of_partitions; i++) { if (size_by_partition[i] == 0) { continue; @@ -113,22 +115,21 @@ void PartitionStrategyGreedy::operator()(const SetPiece& node) auto& p = _partitions[i]; - Set node_to_be_added = SET_FACT.createSet(); + Set node_to_be_added; tie(node_to_be_added, remaining_node) = cut_interval_by_dimension(remaining_node, _node_weight, size_by_partition[i]); - for_each(node_to_be_added.begin(), node_to_be_added.end(), [&p](const auto& set_piece) { p.insert(set_piece); }); + for (const auto& set_piece : key.pieces(get(key.impl(node_to_be_added)))) { + p.insert(key.createSet(OrderedSet(set_piece))); + } } } -map> PartitionStrategyGreedy::partitions() const { return _partitions; } +map> PartitionStrategyGreedy::partitions() const { return _partitions; } /* PartitionStrategyDistributive */ PartitionStrategyDistributive::PartitionStrategyDistributive(unsigned number_of_partitions, const WeightedSBGraph graph) - : PartitionStrategy(), - _number_of_partitions(number_of_partitions), - _nodes(graph.V()), - _node_weight(graph.get_node_weights()) + : PartitionStrategy(), _number_of_partitions(number_of_partitions), _nodes(graph.V()), _node_weight(graph.get_node_weights()) { for (unsigned i = 0; i < _number_of_partitions; i++) { auto p = make_pair(i, 0); @@ -139,11 +140,11 @@ PartitionStrategyDistributive::PartitionStrategyDistributive(unsigned number_of_ // Using an unnamed manespace to define functions with internal linkage namespace { -void add_surplus_sorting_by_value(const map& current_size_by_partition, map& size_by_partition, - unsigned surplus) +void add_surplus_sorting_by_value(const map& current_size_by_partition, map& size_by_partition, + size_t surplus) { // Declare vector of pairs - vector> current_size_by_partition_vector; + vector> current_size_by_partition_vector; // Copy key-value pair from Map // to vector of pairs @@ -153,7 +154,7 @@ void add_surplus_sorting_by_value(const map& current_size_by // Sort using comparator function sort(current_size_by_partition_vector.begin(), current_size_by_partition_vector.end(), - [](const pair& a, const pair& b) { return a.second < b.second; }); + [](const pair& a, const pair& b) { return a.second < b.second; }); // Print the sorted value for (const auto& it : current_size_by_partition_vector) { @@ -168,16 +169,17 @@ void add_surplus_sorting_by_value(const map& current_size_by } // namespace -void PartitionStrategyDistributive::operator()(const SBG::LIB::SetPiece& node) +void PartitionStrategyDistributive::operator()(const SBG::LIB::Set& node_to_be_added) { + SetAccessKey key = SetAccess::key(); #if DEBUG_PARTITION_STRATEGY_ENABLED - logging::sbg_log << "Adding " << node << " distributively to partitions" << endl; + logging::sbg_log << "Adding " << node_to_be_added << " distributively to partitions" << endl; #endif - auto s = get_node_size(node, NodeWeight()); - unsigned size_by_part = s / _number_of_partitions; - unsigned surplus = s % _number_of_partitions; + auto s = get_node_size(node_to_be_added, NodeWeight()); + size_t size_by_part = s / _number_of_partitions; + size_t surplus = s % _number_of_partitions; - map size_by_partition; + map size_by_partition; for (const auto [i, p] : _current_size_by_partition) { size_by_partition[i] = size_by_part; @@ -187,7 +189,7 @@ void PartitionStrategyDistributive::operator()(const SBG::LIB::SetPiece& node) add_surplus_sorting_by_value(_current_size_by_partition, size_by_partition, surplus); } - Set node_to_be_added = SET_FACT.createSet(node); + Set remaining_nodes = node_to_be_added; for (const auto [i, n] : size_by_partition) { #if DEBUG_PARTITION_STRATEGY_ENABLED logging::sbg_log << "For partition " << i << " size: " << n << endl; @@ -197,17 +199,19 @@ void PartitionStrategyDistributive::operator()(const SBG::LIB::SetPiece& node) } auto& p = _partitions[i]; - Set temp_node = SET_FACT.createSet(); - tie(temp_node, node_to_be_added) = cut_interval_by_dimension(node_to_be_added, NodeWeight(), size_by_partition[i]); + Set temp_node; + tie(temp_node, remaining_nodes) = cut_interval_by_dimension(remaining_nodes, NodeWeight(), size_by_partition[i]); #if DEBUG_PARTITION_STRATEGY_ENABLED - logging::sbg_log << "About to add " << temp_node << " to " << i << ", remaining: " << node_to_be_added << endl; + logging::sbg_log << "About to add " << temp_node << " to " << i << ", remaining: " << remaining_nodes << endl; #endif - for_each(temp_node.begin(), temp_node.end(), [&p](const SetPiece& s) { p.insert(s); }); + for (const auto& set_piece : key.pieces(get(key.impl(temp_node)))) { + p.insert(key.createSet(OrderedSet(set_piece))); + } _current_size_by_partition[i] += get_node_size(temp_node, _node_weight); } } -map> PartitionStrategyDistributive::partitions() const { return _partitions; } +map> PartitionStrategyDistributive::partitions() const { return _partitions; } ostream& operator<<(ostream& os, const PartitionStrategy& pgraph) { diff --git a/algorithms/partitioner/partition_strategy.hpp b/algorithms/partitioner/partition_strategy.hpp index bc807808..ae6aa105 100644 --- a/algorithms/partitioner/partition_strategy.hpp +++ b/algorithms/partitioner/partition_strategy.hpp @@ -31,6 +31,7 @@ namespace sbg_partitioner { + class PartitionStrategy { public: @@ -38,9 +39,9 @@ class PartitionStrategy virtual ~PartitionStrategy() = default; - virtual void operator() (const SBG::LIB::SetPiece& node) = 0; + virtual void operator() (const SBG::LIB::Set& node) = 0; - virtual std::map> partitions() const = 0; + virtual std::map> partitions() const = 0; }; class PartitionStrategyGreedy : public PartitionStrategy @@ -48,11 +49,11 @@ class PartitionStrategyGreedy : public PartitionStrategy public: PartitionStrategyGreedy(unsigned number_of_partitions, const SBG::LIB::WeightedSBGraph graph); - virtual ~PartitionStrategyGreedy() = default; + ~PartitionStrategyGreedy() = default; - virtual void operator() (const SBG::LIB::SetPiece& node); + void operator() (const SBG::LIB::Set& node); - virtual std::map> partitions() const; + std::map> partitions() const; private: unsigned _number_of_partitions; @@ -60,9 +61,9 @@ class PartitionStrategyGreedy : public PartitionStrategy unsigned _total_of_nodes; unsigned _acceptable_surplus; unsigned _acceptable_amount; - std::map> _partitions; + std::map> _partitions; size_t _expected_size_by_partition; - std::map _current_size_by_partition; + std::map _current_size_by_partition; SBG::LIB::NodeWeight _node_weight; }; @@ -80,20 +81,20 @@ class PartitionStrategyDistributive : public PartitionStrategy public: PartitionStrategyDistributive(unsigned number_of_partitions, const SBG::LIB::WeightedSBGraph graph); - virtual ~PartitionStrategyDistributive() = default; + ~PartitionStrategyDistributive() = default; - virtual void operator() (const SBG::LIB::SetPiece& node); + void operator() (const SBG::LIB::Set& node); - virtual std::map> partitions() const; + std::map> partitions() const; private: unsigned _number_of_partitions; unsigned _current_partition; - unsigned _total_of_nodes; - unsigned _acceptable_surplus; - unsigned _acceptable_amount; - std::map> _partitions; - std::map _current_size_by_partition; + size_t _total_of_nodes; + size_t _acceptable_surplus; + size_t _acceptable_amount; + std::map> _partitions; + std::map _current_size_by_partition; SBG::LIB::Set _nodes; SBG::LIB::NodeWeight _node_weight; }; diff --git a/algorithms/partitioner/partitioner_params.hpp b/algorithms/partitioner/partitioner_params.hpp index e63e09cc..2eccd694 100644 --- a/algorithms/partitioner/partitioner_params.hpp +++ b/algorithms/partitioner/partitioner_params.hpp @@ -40,12 +40,11 @@ struct PartitionerParams std::optional directory = std::nullopt; std::optional number_of_partitions = std::nullopt; std::optional output_file; - std::optional output_sb_graph = std::nullopt; float epsilon = 0.0; InitialPartitionStrategy initial_partition_strategy = InitialPartitionStrategy::ALL; bool enable_multithreading = false; bool compute_metrics = false; - bool use_connected_components = false; + bool dump_results = false; }; } \ No newline at end of file diff --git a/algorithms/partitioner/sbg_partitioner_types.hpp b/algorithms/partitioner/sbg_partitioner_types.hpp index 66f6bb4f..bfd5ad74 100644 --- a/algorithms/partitioner/sbg_partitioner_types.hpp +++ b/algorithms/partitioner/sbg_partitioner_types.hpp @@ -19,6 +19,7 @@ #include +#include #include #pragma once @@ -26,7 +27,7 @@ namespace sbg_partitioner { -typedef std::vector Partition; +typedef std::vector Partition; typedef std::vector PartitionMap; @@ -41,11 +42,11 @@ namespace using_cc { struct SetPointer { unsigned index; - SBG::LIB::SetPiece set_piece; + SBG::LIB::detail::MultiDimInter set_piece; size_t offset; size_t size; - SetPointer(unsigned index, const SBG::LIB::SetPiece& set_piece, size_t offset, size_t size) + SetPointer(unsigned index, const SBG::LIB::detail::MultiDimInter& set_piece, size_t offset, size_t size) : index(index), set_piece(set_piece), offset(offset), diff --git a/algorithms/partitioner/utils/call_compute_metris_on_batch.py b/algorithms/partitioner/utils/call_compute_metris_on_batch.py new file mode 100644 index 00000000..8d52e12a --- /dev/null +++ b/algorithms/partitioner/utils/call_compute_metris_on_batch.py @@ -0,0 +1,35 @@ +import glob +import os +import re +import subprocess +import sys + +base_edges_dir = "../../.." +base_partition_dir = "../../../test/partitioner/external_tools/data" + +# Pattern to capture model and size from filenames like "advection2D_100_edges.txt" +edge_pattern = re.compile(r"^(.*)_([0-9]+)_edges\.txt$") + +for edges_path in glob.glob(os.path.join(base_edges_dir, "*_edges.txt")): + filename = os.path.basename(edges_path) + match = edge_pattern.match(filename) + + if not match: + continue + + model, size = match.group(1), match.group(2) + model_size = f"{model}_{size}" + + # Locate all partition files matching this model and size + partition_glob = os.path.join(base_partition_dir, f"{model_size}.json-*.partition") + partition_files = glob.glob(partition_glob) + + for partition_path in partition_files: + cmd = ["python3", "compute_metrics.py", partition_path, edges_path] + # print(f"Running: {' '.join(cmd)}") + + result = subprocess.run(cmd, capture_output=True, text=True) + if result.returncode != 0: + print(f"[ERROR] Failed on {partition_path}:\n{result.stderr}", file=sys.stderr) + else: + print(result.stdout) \ No newline at end of file diff --git a/algorithms/partitioner/utils/compute_metrics.py b/algorithms/partitioner/utils/compute_metrics.py new file mode 100644 index 00000000..3db54d53 --- /dev/null +++ b/algorithms/partitioner/utils/compute_metrics.py @@ -0,0 +1,62 @@ +import numpy as np +import pandas as pd +import sys +import os + +def compute_partition_metrics(partition_path, graph_path): + # 1. Efficiently load the data + print(os.path.basename(partition_path), end=' ') + # Adjust sep if your file uses something other than whitespace + edges = np.loadtxt(graph_path, dtype=np.int32) + partitions = np.loadtxt(partition_path, dtype=np.int32) + + src = edges[:, 0] + tgt = edges[:, 1] + + # 2. Identify Cut Edges + # node_partitions[node_id] gives the partition ID + p_src = partitions[src] + p_tgt = partitions[tgt] + + cut_mask = p_src != p_tgt + edge_cut = np.sum(cut_mask) + + # 3. Calculate Communication Volume per Partition + # We need unique pairs of (source_partition, target_partition) + # for every node that has an external neighbor. + + # Filter only the cut edges + cut_src_nodes = src[cut_mask] + cut_tgt_partitions = p_tgt[cut_mask] + cut_src_partitions = p_src[cut_mask] + + # Create a unique key for (SourceNode, TargetPartition) + # This represents: "Node X needs to talk to Partition Y" + node_to_part_key = (cut_src_nodes.astype(np.int64) << 32) | cut_tgt_partitions.astype(np.int64) + + # Get unique interactions only + unique_keys = np.unique(node_to_part_key) + + # Extract the source node ID back from the unique keys to find its partition + # (Bit-shifting back down to get the original 32-bit source node ID) + unique_src_nodes = (unique_keys >> 32).astype(np.int32) + unique_src_partitions = partitions[unique_src_nodes] + + # 4. Aggregate by Partition + # Count how many unique "node-to-remote-partition" interactions each partition has + part_series = pd.Series(unique_src_partitions) + volume_per_partition = part_series.value_counts() + + total_comm_volume = volume_per_partition.sum() + max_comm_volume = volume_per_partition.max() + max_part_id = volume_per_partition.idxmax() if volume_per_partition.size > 0 else 0 + + return { + "edge_cut": str(edge_cut), + "total_volume": str(total_comm_volume), + "max_volume": str(max_comm_volume), + "max_partition": str(max_part_id) + } + +results = compute_partition_metrics(sys.argv[1], sys.argv[2]) +print(results) \ No newline at end of file diff --git a/algorithms/partitioner/weighted_sb_graph.cpp b/algorithms/partitioner/weighted_sb_graph.cpp index 2e10c85f..1e9596bc 100644 --- a/algorithms/partitioner/weighted_sb_graph.cpp +++ b/algorithms/partitioner/weighted_sb_graph.cpp @@ -16,38 +16,68 @@ ******************************************************************************/ - #include "weighted_sb_graph.hpp" - using namespace std; - namespace SBG { namespace LIB { +WeightedSBGraph::WeightedSBGraph(WeightedSBGraph& graph) + : SBG(graph), _node_weights(graph.get_node_weights()), _edge_costs(graph.get_edge_costs()) +{ +} + +WeightedSBGraph::WeightedSBGraph(WeightedSBGraph&& graph) + : SBG(graph), _node_weights(graph.get_node_weights()), _edge_costs(graph.get_edge_costs()) +{ +} + +WeightedSBGraph::WeightedSBGraph(const Set& V, const PWMap& Vmap, const PWMap& map1, const PWMap& map2, const PWMap& Emap) + : SBG(V, Vmap, map1, map2, Emap) +{ +} + +WeightedSBGraph::~WeightedSBGraph() = default; + +void WeightedSBGraph::set_node_weights(NodeWeight& node_weights) { _node_weights = std::move(node_weights); } + +NodeWeight WeightedSBGraph::get_node_weights() const { return _node_weights; } + +void WeightedSBGraph::set_node_weight(const Set& node_set, int weight) { _node_weights[node_set] = weight; } + +int WeightedSBGraph::get_node_weight(const Set& node_set) const { return _node_weights.at(node_set); } + +void WeightedSBGraph::set_edge_costs(EdgeCost& edge_costs) { _edge_costs = std::move(edge_costs); } + +EdgeCost WeightedSBGraph::get_edge_costs() const { return _edge_costs; } + +void WeightedSBGraph::set_edge_cost(const Set& edge_set, unsigned cost) { _edge_costs[edge_set] = cost; } + +unsigned WeightedSBGraph::get_edge_cost(const Set& edge_set) const { return _edge_costs.at(edge_set); } + ostream& operator<<(ostream& os, const WeightedSBGraph& graph) { - os << SBG(graph); + os << SBG(graph); - os << "node weight = << "; - for (const auto& [set, weight] : graph.get_node_weights()) { - os << set << " ↦ " << weight << ", "; - } + os << "node weight = << "; + for (const auto& [set, weight] : graph.get_node_weights()) { + os << set << " ↦ " << weight << ", "; + } - os << "\b >>" << endl; + os << "\b >>" << endl; - os << "edge costs = << "; - for (const auto& [set, weight] : graph.get_edge_costs()) { - os << set << " ↦ " << weight << ", "; - } + os << "edge costs = << "; + for (const auto& [set, weight] : graph.get_edge_costs()) { + os << set << " ↦ " << weight << ", "; + } - os << "\b >>" << endl; + os << "\b >>" << endl; - return os; + return os; } -} +} // namespace LIB -} +} // namespace SBG diff --git a/algorithms/partitioner/weighted_sb_graph.hpp b/algorithms/partitioner/weighted_sb_graph.hpp index 41674de4..0f07979a 100644 --- a/algorithms/partitioner/weighted_sb_graph.hpp +++ b/algorithms/partitioner/weighted_sb_graph.hpp @@ -41,39 +41,36 @@ using NodeWeight = std::map; class WeightedSBGraph : public SBG { public: - WeightedSBGraph(WeightedSBGraph& graph) - : SBG(graph), - _node_weights(graph.get_node_weights()), - _edge_costs(graph.get_edge_costs()) - {} - WeightedSBGraph(WeightedSBGraph&& graph) - : SBG(graph), - _node_weights(graph.get_node_weights()), - _edge_costs(graph.get_edge_costs()) - {} + WeightedSBGraph(WeightedSBGraph& graph); + + WeightedSBGraph(WeightedSBGraph&& graph); WeightedSBGraph(const Set &V, const PWMap &Vmap , const PWMap &map1, const PWMap &map2 - , const PWMap &Emap, const PWMap &subEmap) - : SBG(V, Vmap, map1, map2, Emap, subEmap) - {} + , const PWMap &Emap); + + + WeightedSBGraph(const WeightedSBGraph&) = delete; + WeightedSBGraph operator=(WeightedSBGraph&& other) = delete; + + ~WeightedSBGraph(); - void set_node_weights(NodeWeight& node_weights) { _node_weights = std::move(node_weights); } + void set_node_weights(NodeWeight& node_weights); - NodeWeight get_node_weights() const { return _node_weights; } + NodeWeight get_node_weights() const; - void set_node_weight(const Set& node_set, int weight) { _node_weights[node_set] = weight; } + void set_node_weight(const Set& node_set, int weight); - int get_node_weight(const Set& node_set) const { return _node_weights.at(node_set); } + int get_node_weight(const Set& node_set) const; - void set_edge_costs(EdgeCost& edge_costs) { _edge_costs = std::move(edge_costs); } + void set_edge_costs(EdgeCost& edge_costs); - EdgeCost get_edge_costs() const { return _edge_costs; } + EdgeCost get_edge_costs() const; - void set_edge_cost(const Set& edge_set, unsigned cost) { _edge_costs[edge_set] = cost; } + void set_edge_cost(const Set& edge_set, unsigned cost); - unsigned get_edge_cost(const Set& edge_set) const { return _edge_costs.at(edge_set); } + unsigned get_edge_cost(const Set& edge_set) const; private: NodeWeight _node_weights; diff --git a/algorithms/scc/decreasing_edges_mrv.cpp b/algorithms/scc/decreasing_edges_mrv.cpp index 6f79431f..9746c427 100644 --- a/algorithms/scc/decreasing_edges_mrv.cpp +++ b/algorithms/scc/decreasing_edges_mrv.cpp @@ -48,12 +48,12 @@ PWMap LtEdgesMRV::detectRepetition(const PWMap& decreasing_smap) const { // Get repetitive paths. Set Vj = decreasing_smap.domain().difference(decreasing_smap.image()); - Set repetitive_vertices = Vj; ///< Vertices traversed by the repetition. + Set repetitive_vertices = Vj; ///< Vertices traversed by the repetition. for (unsigned int j = 0; j < _n; ++j) { Vj = _smap.image(Vj); repetitive_vertices = repetitive_vertices.disjointCup(repetitive_vertices); } - + return decreasing_smap.combine(_smap).restrict(repetitive_vertices); } @@ -76,14 +76,11 @@ PWMap LtEdgesMRV::repetition(const PWMap& decreasing_smap) const // Keep endings of the paths untouched. Set decreasing_repd = _rmap.preImage(_rmap.image(mapD.image(E_repetition))); - smap_plus = smap_plus.restrict( - smap_plus.domain().difference(decreasing_repd) - ); + smap_plus = smap_plus.restrict(smap_plus.domain().difference(decreasing_repd)); return smap_plus; } - PWMap LtEdgesMRV::calculate(const DirectedSBG& dsbg) { Util::DEBUG_LOG << "LtEdgesMRV dsbg:\n" << dsbg << "\n\n"; @@ -121,7 +118,7 @@ PWMap LtEdgesMRV::calculate(const DirectedSBG& dsbg) if (!repeatedSE.isEmpty()) { smap_plus = repetition(decreasing_smap); visitedSE = visitedSE.difference(Emap.image(globalE)); - globalE = Set{}; + globalE = Set{}; _n = 0; } else { visitedSE = std::move(visitedSE).disjointCup(Emap.image(Pj)); @@ -140,6 +137,6 @@ PWMap LtEdgesMRV::calculate(const DirectedSBG& dsbg) return _rmap; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/scc/decreasing_rmap_mrv.cpp b/algorithms/scc/decreasing_rmap_mrv.cpp index 14978c26..045f0050 100644 --- a/algorithms/scc/decreasing_rmap_mrv.cpp +++ b/algorithms/scc/decreasing_rmap_mrv.cpp @@ -54,7 +54,7 @@ Set LtRmapMRV::decreasingRepresentative() return decreasing_edges; } -PWMap LtRmapMRV::repetition(const Set& E) +PWMap LtRmapMRV::repetition(const Set& E) { // Extend edges of the repetition. PWMap Emap = _dsbg.Emap(); @@ -76,7 +76,6 @@ PWMap LtRmapMRV::repetition(const Set& E) return smap_plus; } - PWMap LtRmapMRV::calculate(const DirectedSBG& dsbg) { Util::DEBUG_LOG << "LtRmapMRV dsbg:\n" << dsbg << "\n\n"; @@ -100,7 +99,7 @@ PWMap LtRmapMRV::calculate(const DirectedSBG& dsbg) PWMap smap_plus; Set repeatedSE = visitedSE.intersection(Emap.image(E)); - if (!repeatedSE.isEmpty()) { // Handle repetition. + if (!repeatedSE.isEmpty()) { // Handle repetition. smap_plus = repetition(E); } else { visitedSE = std::move(visitedSE).disjointCup(Emap.image(E)); @@ -116,6 +115,6 @@ PWMap LtRmapMRV::calculate(const DirectedSBG& dsbg) return _rmap.mapInf(); } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/scc/minadj_mrv.cpp b/algorithms/scc/minadj_mrv.cpp index 2121245c..cefcaaaf 100644 --- a/algorithms/scc/minadj_mrv.cpp +++ b/algorithms/scc/minadj_mrv.cpp @@ -144,7 +144,7 @@ PWMap MinAdjMRV::calculate(const DirectedSBG& dsbg) Util::DEBUG_LOG << "rmap after rec: " << rmap << "\n\n"; } - } while (!Vc.isEmpty()); + } while (!Vc.isEmpty()); return rmap; } @@ -152,6 +152,6 @@ PWMap MinAdjMRV::calculate(const DirectedSBG& dsbg) return PWMap{}; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/scc/minreach_scc.cpp b/algorithms/scc/minreach_scc.cpp index 797bbb2b..2664052b 100644 --- a/algorithms/scc/minreach_scc.cpp +++ b/algorithms/scc/minreach_scc.cpp @@ -97,8 +97,7 @@ PWMap MinReachSCCV1::sccStep() PWMap rmapD = new_rmap.composition(_dsbg.mapD()); Set Esame = rmapB.equalImage(rmapD); _E = Esame; - Util::DEBUG_LOG << "MinReachSCCV1 erased edges: " - << _dsbg.E().difference(_E) << "\n\n"; + Util::DEBUG_LOG << "MinReachSCCV1 erased edges: " << _dsbg.E().difference(_E) << "\n\n"; // Swap directions swapEdgesDirection(_E); @@ -124,8 +123,7 @@ PWMap MinReachSCCV2::sccStep() PWMap rmapD = new_rmap.composition(_dsbg.mapD()); Set Esame = rmapB.equalImage(rmapD); _E = Esame; - Util::DEBUG_LOG << "MinReachSCCV2 erased edges: " - << _dsbg.E().difference(_E) << "\n\n"; + Util::DEBUG_LOG << "MinReachSCCV2 erased edges: " << _dsbg.E().difference(_E) << "\n\n"; // Swap directions swapEdgesDirection(_E); @@ -151,8 +149,7 @@ PWMap MinReachSCCV3::sccStep() PWMap rmapD = new_rmap.composition(_dsbg.mapD()); Set Esame = rmapB.equalImage(rmapD); _E = Esame; - Util::DEBUG_LOG << "MinReachSCCV3 erased edges: " - << _dsbg.E().difference(_E) << "\n\n"; + Util::DEBUG_LOG << "MinReachSCCV3 erased edges: " << _dsbg.E().difference(_E) << "\n\n"; // Swap directions swapEdgesDirection(_E); @@ -160,8 +157,8 @@ PWMap MinReachSCCV3::sccStep() return new_rmap; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/scc/scc.cpp b/algorithms/scc/scc.cpp index beb8b212..d7b06048 100644 --- a/algorithms/scc/scc.cpp +++ b/algorithms/scc/scc.cpp @@ -36,25 +36,25 @@ SCC::SCC() : _impl() { SCCKind kind = SCC_IMPL.kind(); switch (kind) { - case SCCKind::kMinReachV1: { - _impl = detail::MinReachSCCV1{}; - break; - } - - case SCCKind::kMinReachV2: { - _impl = detail::MinReachSCCV2{}; - break; - } - - case SCCKind::kMinReachV3: { - _impl = detail::MinReachSCCV3{}; - break; - } - - default: { - Util::ERROR("Unsupported SCC implementation"); - break; - } + case SCCKind::kMinReachV1: { + _impl = detail::MinReachSCCV1{}; + break; + } + + case SCCKind::kMinReachV2: { + _impl = detail::MinReachSCCV2{}; + break; + } + + case SCCKind::kMinReachV3: { + _impl = detail::MinReachSCCV3{}; + break; + } + + default: { + Util::ERROR("Unsupported SCC implementation"); + break; + } } } @@ -65,6 +65,6 @@ SCCData SCC::calculate(const DirectedSBG& dsbg) return std::visit([&](auto& a) { return a.calculate(dsbg); }, _impl); } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/scc/scc_data.cpp b/algorithms/scc/scc_data.cpp index 4ab15603..841c9473 100644 --- a/algorithms/scc/scc_data.cpp +++ b/algorithms/scc/scc_data.cpp @@ -23,8 +23,7 @@ namespace SBG { namespace LIB { -SCCData::SCCData(DirectedSBG dsbg, PWMap rmap, Set Ediff) - : _dsbg(dsbg), _rmap(rmap), _Ediff(Ediff) {} +SCCData::SCCData(DirectedSBG dsbg, PWMap rmap, Set Ediff) : _dsbg(dsbg), _rmap(rmap), _Ediff(Ediff) {} const DirectedSBG& SCCData::dsbg() const { return _dsbg; } @@ -32,6 +31,6 @@ const PWMap& SCCData::rmap() const { return _rmap; } const Set& SCCData::Ediff() const { return _Ediff; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/scc/scc_impl.cpp b/algorithms/scc/scc_impl.cpp old mode 100755 new mode 100644 index ff303118..894b20fb --- a/algorithms/scc/scc_impl.cpp +++ b/algorithms/scc/scc_impl.cpp @@ -25,31 +25,31 @@ namespace SBG { namespace LIB { //////////////////////////////////////////////////////////////////////////////// -// SCC implementations --------------------------------------------------------- +// SCC implementations --------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// std::ostream& operator<<(std::ostream& out, const SCCKind kind) { switch (kind) { - case SCCKind::kMinReachV1: { - out << "minimum reachable V1"; - break; - } - - case SCCKind::kMinReachV2: { - out << "minimum reachable V2"; - break; - } - - case SCCKind::kMinReachV3: { - out << "minimum reachable V3"; - break; - } - - default: { - Util::ERROR("Unsupported SCC implementation"); - break; - } + case SCCKind::kMinReachV1: { + out << "minimum reachable V1"; + break; + } + + case SCCKind::kMinReachV2: { + out << "minimum reachable V2"; + break; + } + + case SCCKind::kMinReachV3: { + out << "minimum reachable V3"; + break; + } + + default: { + Util::ERROR("Unsupported SCC implementation"); + break; + } } return out; @@ -67,6 +67,6 @@ const SCCKind& SCCImplementation::kind() const { return _kind; } void SCCImplementation::set_scc_fact(SCCKind kind) { _kind = kind; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/sorting/topological/min_vertex_ts.cpp b/algorithms/sorting/topological/min_vertex_ts.cpp index b958a569..e3f61651 100644 --- a/algorithms/sorting/topological/min_vertex_ts.cpp +++ b/algorithms/sorting/topological/min_vertex_ts.cpp @@ -44,9 +44,9 @@ PWMap MinVertexTS::extendRepetition() PWMap smap_plus; for (std::size_t k = 1; _depth >= k; ++k) { std::size_t sz = _repetitionSV.size(); - Set domain_plus = Vmap.preImage(_repetitionSV[sz-k]); + Set domain_plus = Vmap.preImage(_repetitionSV[sz - k]); sz = _repetition_sort.size(); - smap_plus.emplace(domain_plus, _repetition_sort[sz-k].law()); + smap_plus.emplace(domain_plus, _repetition_sort[sz - k].law()); } // Take out vertices that are assigned a value that is not in V. @@ -81,8 +81,7 @@ PWMap MinVertexTS::repetition(const PWMap& Emap) Set dependent_vertices = dsbg_copy.mapD().image().intersection(plus_domain); PWMap rmap = PWMap{dependent_vertices}.combine(smap_plus).mapInf(); - Set indep_vertices = plus_domain - .difference(rmap.preImage(dependent_vertices)); + Set indep_vertices = plus_domain.difference(rmap.preImage(dependent_vertices)); PWMap result = smap_plus.restrict(indep_vertices); return result; @@ -92,7 +91,7 @@ PWMap MinVertexTS::repetition(const PWMap& Emap) IntTuple MinVertexTS::getVertex(Set old_Vj) { - Set V = _dsbg.V(); + Set V = _dsbg.V(); PWMap mapD = _dsbg.mapD(); // Independent vertices with no incoming edges. @@ -121,8 +120,7 @@ IntTuple MinVertexTS::getVertex(Set old_Vj) // Algorithm ------------------------------------------------------------------- -PWMap MinVertexTS::calculate(const DirectedSBG& dsbg - , const PWMap& scc_map) +PWMap MinVertexTS::calculate(const DirectedSBG& dsbg, const PWMap& scc_map) { Util::DEBUG_LOG << "Topological sort dsbg:\n" << dsbg << "\n\n"; @@ -185,8 +183,8 @@ PWMap MinVertexTS::calculate(const DirectedSBG& dsbg return _smap; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/sorting/topological/topological_sorting.cpp b/algorithms/sorting/topological/topological_sorting.cpp index f44a7e77..7a6be904 100644 --- a/algorithms/sorting/topological/topological_sorting.cpp +++ b/algorithms/sorting/topological/topological_sorting.cpp @@ -34,16 +34,15 @@ TopologicalSorting::TopologicalSorting() : _impl() { TSKind kind = TS_IMPL.kind(); switch (kind) { - case TSKind::kMinVertex: { - _impl = detail::MinVertexTS{}; - break; - } - - default: { - Util::ERROR("TopologicalSorting::operator<<: unsupported topological " - , "sorting algorithm implementation"); - } - } + case TSKind::kMinVertex: { + _impl = detail::MinVertexTS{}; + break; + } + + default: { + Util::ERROR("TopologicalSorting::operator<<: unsupported topological ", "sorting algorithm implementation"); + } + } } PWMap TopologicalSorting::calculate(const DirectedSBG& dsbg, const PWMap& pmap) @@ -54,6 +53,6 @@ PWMap TopologicalSorting::calculate(const DirectedSBG& dsbg, const PWMap& pmap) return std::visit([&](auto& a) { return a.calculate(dsbg, pmap); }, _impl); } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/sorting/topological/ts_impl.cpp b/algorithms/sorting/topological/ts_impl.cpp old mode 100755 new mode 100644 index 182a8a2d..1bf0a298 --- a/algorithms/sorting/topological/ts_impl.cpp +++ b/algorithms/sorting/topological/ts_impl.cpp @@ -31,16 +31,15 @@ namespace LIB { std::ostream& operator<<(std::ostream& out, const TSKind kind) { switch (kind) { - case TSKind::kMinVertex: { - out << "minimum vertex"; - break; - } - - default: { - Util::ERROR("TSKind::operator<<: unsupported topological sorting " - , "algorithm implementation"); - break; - } + case TSKind::kMinVertex: { + out << "minimum vertex"; + break; + } + + default: { + Util::ERROR("TSKind::operator<<: unsupported topological sorting ", "algorithm implementation"); + break; + } } return out; @@ -58,6 +57,6 @@ const TSKind& TSImplementation::kind() const { return _kind; } void TSImplementation::set_ts_fact(TSKind kind) { _kind = kind; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/algorithms/sorting/topological/ts_utils.cpp b/algorithms/sorting/topological/ts_utils.cpp index 04fc506f..a4c07643 100644 --- a/algorithms/sorting/topological/ts_utils.cpp +++ b/algorithms/sorting/topological/ts_utils.cpp @@ -49,7 +49,7 @@ Set SortedSets::accumulated(int n) const { Set result; std::size_t sz = _sets.size(); - for (int j = sz-n; j < sz; ++j) { + for (int j = sz - n; j < sz; ++j) { result = std::move(result).disjointCup(_sets[j]); } return result; @@ -72,14 +72,12 @@ void checkSort(PWMap& smap, const DirectedSBG& dsbg) { // Check that the order is unique. Set fixed_points = smap.fixedPoints(); - Util::ERROR_UNLESS(fixed_points.cardinal() == 1 - , "checkSort: the order ", smap, " has multiple ends\n"); + Util::ERROR_UNLESS(fixed_points.cardinal() == 1, "checkSort: the order ", smap, " has multiple ends\n"); Set domain = smap.domain(); Set image = smap.image(); Set starts = domain.difference(image); - Util::ERROR_UNLESS(starts.cardinal() == 1 - , "checkSort: the order ", smap, " has multiple starts\n"); + Util::ERROR_UNLESS(starts.cardinal() == 1, "checkSort: the order ", smap, " has multiple starts\n"); // Check if it is injective, that is, if is an actual order. Set visited_img; @@ -93,8 +91,7 @@ void checkSort(PWMap& smap, const DirectedSBG& dsbg) // Check if the sort is total. Set V = dsbg.V(); - Util::ERROR_UNLESS(domain == V && image == V.difference(starts) - , "checkSort: the order ", smap, " is partial\n"); + Util::ERROR_UNLESS(domain == V && image == V.difference(starts), "checkSort: the order ", smap, " is partial\n"); // Delete dependencies of constant depth. PWMap smap_copy = smap; @@ -107,8 +104,7 @@ void checkSort(PWMap& smap, const DirectedSBG& dsbg) // Check order for long dependencies. PWMap smap_inv = smap.restrict(copy.V().difference(fixed_points)).inverse(); - copy.foreachSetEdge([&](const IntTuple& SE) - { + copy.foreachSetEdge([&](const IntTuple& SE) { Set E = copy.Emap().preImage(SE); PWMap mapB = copy.mapB().restrict(E); PWMap mapD = copy.mapD().restrict(E); @@ -120,11 +116,10 @@ void checkSort(PWMap& smap, const DirectedSBG& dsbg) rmap = PWMap{intermediate}.combine(smap_inv).mapInf(); Set before = rmap.preImage(init); - Util::ERROR_UNLESS(init.difference(before).isEmpty() - , "checkSort: the order ", smap, " is partial\n"); + Util::ERROR_UNLESS(init.difference(before).isEmpty(), "checkSort: the order ", smap, " is partial\n"); }); } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/ast/expression.cpp b/ast/expression.cpp old mode 100755 new mode 100644 index c61c5124..31ea76ac --- a/ast/expression.cpp +++ b/ast/expression.cpp @@ -31,18 +31,13 @@ namespace AST { Rational::Rational() : _numerator(0), _denominator(1) {} -Rational::Rational(Expr numerator, Expr denominator) - : _numerator(numerator), _denominator(denominator) {} +Rational::Rational(Expr numerator, Expr denominator) : _numerator(numerator), _denominator(denominator) {} const Expr& Rational::numerator() const { return _numerator; } const Expr& Rational::denominator() const { return _denominator; } -bool Rational::operator==(const Rational& other) const -{ - return _numerator == other._numerator - && _denominator == other._denominator; -} +bool Rational::operator==(const Rational& other) const { return _numerator == other._numerator && _denominator == other._denominator; } std::ostream& operator<<(std::ostream& out, const Rational& r) { @@ -57,8 +52,7 @@ std::ostream& operator<<(std::ostream& out, const Rational& r) // Interval -------------------------------------------------------------------- -Interval::Interval(Expr begin, Expr step, Expr end) - : _begin(begin), _step(step), _end(end) {} +Interval::Interval(Expr begin, Expr step, Expr end) : _begin(begin), _step(step), _end(end) {} const Expr& Interval::begin() const { return _begin; } @@ -66,14 +60,10 @@ const Expr& Interval::step() const { return _step; } const Expr& Interval::end() const { return _end; } -bool Interval::operator==(const Interval& other) const -{ - return _begin == other._begin && _step == other._step - && _step == other._step; -} +bool Interval::operator==(const Interval& other) const { return _begin == other._begin && _step == other._step && _step == other._step; } std::ostream& operator<<(std::ostream& out, const Interval& i) -{ +{ out << "[" << i.begin() << ":" << i.step() << ":" << i.end() << "]"; return out; @@ -85,10 +75,7 @@ MultiDimInter::MultiDimInter(ExprList intervals) : _intervals(intervals) {} const ExprList& MultiDimInter::intervals() const { return _intervals; } -bool MultiDimInter::operator==(const MultiDimInter& other) const -{ - return _intervals == other._intervals; -} +bool MultiDimInter::operator==(const MultiDimInter& other) const { return _intervals == other._intervals; } std::ostream& operator<<(std::ostream& out, const MultiDimInter& mdi) { @@ -98,13 +85,13 @@ std::ostream& operator<<(std::ostream& out, const MultiDimInter& mdi) if (sz > 0) { auto it = aux.intervals().begin(); for (int i = 0; i < sz - 1; ++i) { - out << *it << " x "; + out << *it << " x "; ++it; } out << *it; } - return out; + return out; } // Set ------------------------------------------------------------------------- @@ -113,10 +100,7 @@ Set::Set(ExprList pieces) : _pieces(pieces) {} const ExprList& Set::pieces() const { return _pieces; } -bool Set::operator==(const Set& other) const -{ - return _pieces == other._pieces; -} +bool Set::operator==(const Set& other) const { return _pieces == other._pieces; } std::ostream& operator<<(std::ostream& out, const Set& s) { @@ -127,29 +111,25 @@ std::ostream& operator<<(std::ostream& out, const Set& s) if (sz > 0) { auto it = aux.pieces().begin(); for (int i = 0; i < sz - 1; ++i) { - out << *it << ", "; + out << *it << ", "; ++it; } out << *it; } out << "}"; - return out; + return out; } // Linear expression ----------------------------------------------------------- -LinearExpr::LinearExpr(Expr slope, Expr offset) - : _slope(slope), _offset(offset) {} +LinearExpr::LinearExpr(Expr slope, Expr offset) : _slope(slope), _offset(offset) {} const Expr& LinearExpr::slope() const { return _slope; } const Expr& LinearExpr::offset() const { return _offset; } -bool LinearExpr::operator==(const LinearExpr& other) const -{ - return _slope == other._slope && _offset == other._offset; -} +bool LinearExpr::operator==(const LinearExpr& other) const { return _slope == other._slope && _offset == other._offset; } std::ostream& operator<<(std::ostream& out, const LinearExpr& le) { @@ -164,10 +144,7 @@ MDLExp::MDLExp(ExprList exps) : _exps(exps) {} const ExprList& MDLExp::exps() const { return _exps; } -bool MDLExp::operator==(const MDLExp& other) const -{ - return _exps == other._exps; -} +bool MDLExp::operator==(const MDLExp& other) const { return _exps == other._exps; } std::ostream& operator<<(std::ostream& out, const MDLExp& le) { @@ -183,7 +160,7 @@ std::ostream& operator<<(std::ostream& out, const MDLExp& le) out << *it; } - return out; + return out; } // SBG map --------------------------------------------------------------------- @@ -194,10 +171,7 @@ const Expr& LinearMap::domain() const { return _domain; } const Expr& LinearMap::law() const { return _law; } -bool LinearMap::operator==(const LinearMap& other) const -{ - return _domain == other._domain && _law == other._law; -} +bool LinearMap::operator==(const LinearMap& other) const { return _domain == other._domain && _law == other._law; } std::ostream& operator<<(std::ostream& out, const LinearMap& lmap) { @@ -212,16 +186,13 @@ PWLMap::PWLMap(ExprList maps) : _maps(maps) {} const ExprList& PWLMap::maps() const { return _maps; } -bool PWLMap::operator==(const PWLMap& other) const -{ - return _maps == other._maps; -} +bool PWLMap::operator==(const PWLMap& other) const { return _maps == other._maps; } std::ostream& operator<<(std::ostream& out, const PWLMap& pwl) { PWLMap aux = pwl; unsigned int sz = aux.maps().size(); - + out << "<<"; if (sz > 0) { auto it = aux.maps().begin(); @@ -238,8 +209,7 @@ std::ostream& operator<<(std::ostream& out, const PWLMap& pwl) // SBG ------------------------------------------------------------------------- -SBG::SBG(Expr V, Expr Vmap, Expr map1, Expr map2, Expr Emap) : _V(V) - , _Vmap(Vmap), _map1(map1), _map2(map2), _Emap(Emap) {} +SBG::SBG(Expr V, Expr Vmap, Expr map1, Expr map2, Expr Emap) : _V(V), _Vmap(Vmap), _map1(map1), _map2(map2), _Emap(Emap) {} const Expr& SBG::V() const { return _V; } @@ -251,10 +221,9 @@ const Expr& SBG::map2() const { return _map2; } const Expr& SBG::Emap() const { return _Emap; } -bool SBG::operator==(const SBG& other) const -{ - return _V == other._V && _Vmap == other._Vmap && _map1 == other._map1 - && _map2 == other._map2 && _Emap == other._Emap; +bool SBG::operator==(const SBG& other) const +{ + return _V == other._V && _Vmap == other._Vmap && _map1 == other._map1 && _map2 == other._map2 && _Emap == other._Emap; } std::ostream& operator<<(std::ostream& out, const SBG& g) @@ -270,19 +239,19 @@ std::ostream& operator<<(std::ostream& out, const SBG& g) // Bipartite SBG --------------------------------------------------------------- -BipartiteSBG::BipartiteSBG(Expr V, Expr Vmap, Expr map1, Expr map2, Expr Emap - , Expr X, Expr Y) - : SBG(V, Vmap, map1, map2, Emap), _X(X), _Y(Y) {} +BipartiteSBG::BipartiteSBG(Expr V, Expr Vmap, Expr map1, Expr map2, Expr Emap, Expr X, Expr Y) + : SBG(V, Vmap, map1, map2, Emap), _X(X), _Y(Y) +{ +} const Expr& BipartiteSBG::X() const { return _X; } const Expr& BipartiteSBG::Y() const { return _Y; } -bool BipartiteSBG::operator==(const BipartiteSBG& other) const -{ - return _V == other._V && _Vmap == other._Vmap && _map1 == other._map1 - && _map2 == other._map2 && _Emap == other._Emap - && _X == other._X && _Y == other._Y; +bool BipartiteSBG::operator==(const BipartiteSBG& other) const +{ + return _V == other._V && _Vmap == other._Vmap && _map1 == other._map1 && _map2 == other._map2 && _Emap == other._Emap && _X == other._X && + _Y == other._Y; } std::ostream& operator<<(std::ostream& out, const BipartiteSBG& g) @@ -300,8 +269,7 @@ std::ostream& operator<<(std::ostream& out, const BipartiteSBG& g) // DSBG ------------------------------------------------------------------------ -DSBG::DSBG(Expr V, Expr Vmap, Expr mapB, Expr mapD, Expr Emap) - : _V(V), _Vmap(Vmap), _mapB(mapB), _mapD(mapD), _Emap(Emap) {} +DSBG::DSBG(Expr V, Expr Vmap, Expr mapB, Expr mapD, Expr Emap) : _V(V), _Vmap(Vmap), _mapB(mapB), _mapD(mapD), _Emap(Emap) {} const Expr& DSBG::V() const { return _V; } @@ -313,10 +281,9 @@ const Expr& DSBG::mapD() const { return _mapD; } const Expr& DSBG::Emap() const { return _Emap; } -bool DSBG::operator==(const DSBG& other) const -{ - return _V == other._V && _Vmap == other._Vmap && _mapB == other._mapB - && _mapD == other._mapD && _Emap == other._Emap; +bool DSBG::operator==(const DSBG& other) const +{ + return _V == other._V && _Vmap == other._Vmap && _mapB == other._mapB && _mapD == other._mapD && _Emap == other._Emap; } std::ostream& operator<<(std::ostream& out, const DSBG& dg) @@ -337,20 +304,20 @@ std::ostream& operator<<(std::ostream& out, const DSBG& dg) std::ostream& operator<<(std::ostream& out, const UnOp& op) { switch (op) { - case UnOp::oppo: - out << "-"; - break; + case UnOp::oppo: + out << "-"; + break; - case UnOp::card: - out << "#"; - break; + case UnOp::card: + out << "#"; + break; - case UnOp::comp: - out << "\'"; - break; + case UnOp::comp: + out << "\'"; + break; - default: - break; + default: + break; } return out; @@ -359,44 +326,44 @@ std::ostream& operator<<(std::ostream& out, const UnOp& op) std::ostream& operator<<(std::ostream& out, const Op& op) { switch (op) { - case Op::add: - out << "+"; - break; + case Op::add: + out << "+"; + break; - case Op::sub: - out << "-"; - break; + case Op::sub: + out << "-"; + break; - case Op::mult: - out << "*"; - break; + case Op::mult: + out << "*"; + break; - case Op::expo: - out << "^"; - break; + case Op::expo: + out << "^"; + break; - case Op::eq: - out << "=="; - break; + case Op::eq: + out << "=="; + break; - case Op::less: - out << "<"; - break; + case Op::less: + out << "<"; + break; - case Op::cap: - out << "/\\"; - break; + case Op::cap: + out << "/\\"; + break; - case Op::cup: - out << "\\/"; - break; + case Op::cup: + out << "\\/"; + break; - case Op::diff: - out << "\\"; - break; + case Op::diff: + out << "\\"; + break; - default: - break; + default: + break; } return out; @@ -408,23 +375,20 @@ UnaryOp::UnaryOp(UnOp op, Expr expr) : _op(op), _expr(expr) {} const UnOp& UnaryOp::op() const { return _op; } -const Expr& UnaryOp::expr() const { return _expr;} +const Expr& UnaryOp::expr() const { return _expr; } -bool UnaryOp::operator==(const UnaryOp& other) const -{ - return _op == other._op && _expr == other._expr; -} +bool UnaryOp::operator==(const UnaryOp& other) const { return _op == other._op && _expr == other._expr; } std::ostream& operator<<(std::ostream& out, const UnaryOp& uop) -{ +{ switch (uop.op()) { - case UnOp::comp: - out << uop.expr() << uop.op(); - break; + case UnOp::comp: + out << uop.expr() << uop.op(); + break; - default: - out << uop.op() << uop.expr(); - break; + default: + out << uop.op() << uop.expr(); + break; } return out; @@ -432,8 +396,7 @@ std::ostream& operator<<(std::ostream& out, const UnaryOp& uop) // Binary operations ----------------------------------------------------------- -BinOp::BinOp(Expr left, Op op, Expr right) - : _left(left), _op(op), _right(right) {} +BinOp::BinOp(Expr left, Op op, Expr right) : _left(left), _op(op), _right(right) {} const Expr& BinOp::left() const { return _left; } @@ -441,14 +404,10 @@ const Op& BinOp::op() const { return _op; } const Expr& BinOp::right() const { return _right; } -bool BinOp::operator==(const BinOp& other) const -{ - return _left == other._left && _op == other._op - && _right == other._right; -} +bool BinOp::operator==(const BinOp& other) const { return _left == other._left && _op == other._op && _right == other._right; } std::ostream& operator<<(std::ostream& out, const BinOp& bop) -{ +{ out << bop.left() << bop.op() << bop.right(); return out; @@ -456,10 +415,7 @@ std::ostream& operator<<(std::ostream& out, const BinOp& bop) // Call ------------------------------------------------------------------------ -Call::Call(Name name, Expr arg) : _name(name) -{ - _args.push_back(arg); -} +Call::Call(Name name, Expr arg) : _name(name) { _args.push_back(arg); } Call::Call(Name name, ExprList args) : _name(name), _args(args) {} @@ -467,10 +423,7 @@ const Name& Call::name() const { return _name; } const ExprList& Call::args() const { return _args; } -bool Call::operator==(const Call& other) const -{ - return _name == other._name && _args == other._args; -} +bool Call::operator==(const Call& other) const { return _name == other._name && _args == other._args; } std::ostream& operator<<(std::ostream& out, const Call& c) { @@ -484,7 +437,7 @@ std::ostream& operator<<(std::ostream& out, const Call& c) } out << c.args()[i]; } - out << ")"; + out << ")"; return out; } @@ -495,10 +448,7 @@ ParenExpr::ParenExpr(Expr e) : _pexpr(e) {} const Expr& ParenExpr::pexpr() const { return _pexpr; } -bool ParenExpr::operator==(const ParenExpr& pe) const -{ - return _pexpr == pe._pexpr; -} +bool ParenExpr::operator==(const ParenExpr& pe) const { return _pexpr == pe._pexpr; } std::ostream& operator<<(std::ostream& out, const ParenExpr& pe) { @@ -516,6 +466,6 @@ std::ostream& operator<<(std::ostream& out, const ExprList& el) return out; } -} // namespace AST +} // namespace AST -} // namespace SBG +} // namespace SBG diff --git a/ast/sbg_program.cpp b/ast/sbg_program.cpp old mode 100755 new mode 100644 index ffcd7ef0..951d4750 --- a/ast/sbg_program.cpp +++ b/ast/sbg_program.cpp @@ -25,28 +25,26 @@ namespace AST { SBGProgram::SBGProgram() : _arity(1) {} -SBGProgram::SBGProgram(StatementList stms, ExprList exprs) - : _arity(1), _stms(stms), _exprs(exprs) {} +SBGProgram::SBGProgram(StatementList stms, ExprList exprs) : _arity(1), _stms(stms), _exprs(exprs) {} -SBGProgram::SBGProgram(std::size_t arity, StatementList stms, ExprList exprs) - : _arity(arity), _stms(stms), _exprs(exprs) {} +SBGProgram::SBGProgram(std::size_t arity, StatementList stms, ExprList exprs) : _arity(arity), _stms(stms), _exprs(exprs) {} const std::size_t SBGProgram::arity() const { return _arity; } -const StatementList& SBGProgram::stms() const { return _stms; } +const StatementList &SBGProgram::stms() const { return _stms; } -const ExprList& SBGProgram::exprs() const { return _exprs; } +const ExprList &SBGProgram::exprs() const { return _exprs; } std::ostream &operator<<(std::ostream &out, const SBGProgram &prog) { if (!prog.stms().empty()) { out << prog.stms() << "\n"; } - out << prog.exprs(); + out << prog.exprs(); return out; } -} // namespace AST +} // namespace AST -} // namespace SBG +} // namespace SBG diff --git a/ast/statement.cpp b/ast/statement.cpp old mode 100755 new mode 100644 index c99b04e5..f4e8f1a4 --- a/ast/statement.cpp +++ b/ast/statement.cpp @@ -25,9 +25,9 @@ namespace AST { Assign::Assign(Name left, Expr right) : _left(left), _right(right) {} -const Name& Assign::left() const { return _left; } +const Name &Assign::left() const { return _left; } -const Expr& Assign::right() const { return _right; } +const Expr &Assign::right() const { return _right; } std::ostream &operator<<(std::ostream &out, const Assign &asgn) { @@ -36,7 +36,8 @@ std::ostream &operator<<(std::ostream &out, const Assign &asgn) return out; } -ConfigDims::ConfigDims(std::size_t arity) : _arity(arity) { +ConfigDims::ConfigDims(std::size_t arity) : _arity(arity) +{ if (arity > 0) { _arity = arity; } @@ -68,6 +69,6 @@ std::ostream &operator<<(std::ostream &out, const StatementList &stml) return out; } -} // namespace AST +} // namespace AST -} // namespace SBG +} // namespace SBG diff --git a/eval/eval_context.cpp b/eval/eval_context.cpp old mode 100755 new mode 100644 index b1cb07c1..454c031b --- a/eval/eval_context.cpp +++ b/eval/eval_context.cpp @@ -29,40 +29,22 @@ EvalContext::EvalContext() : arity_(1), venv_(), fenv_() {} // Getters --------------------------------------------------------------------- -unsigned int EvalContext::arity() const -{ - return arity_; -} +unsigned int EvalContext::arity() const { return arity_; } -VarEnv& EvalContext::venv() -{ - return venv_; -} +VarEnv& EvalContext::venv() { return venv_; } -FuncEnv& EvalContext::fenv() -{ - return fenv_; -} +FuncEnv& EvalContext::fenv() { return fenv_; } // Setters --------------------------------------------------------------------- -void EvalContext::setArity(unsigned int arity) -{ - arity_ = arity; -} +void EvalContext::setArity(unsigned int arity) { arity_ = arity; } -void EvalContext::insertVariable(VarEnv::VKey key, VarEnv::VValue value) -{ - venv_.insert(key, value); -} +void EvalContext::insertVariable(VarEnv::VKey key, VarEnv::VValue value) { venv_.insert(key, value); } -void EvalContext::insertFunction(FuncEnv::FKey key, FuncEnv::FValue value) -{ - fenv_.insert(key, value); -} +void EvalContext::insertFunction(FuncEnv::FKey key, FuncEnv::FValue value) { fenv_.insert(key, value); } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/eval_exec.cpp b/eval/eval_exec.cpp index 2988477b..20d1b3e5 100644 --- a/eval/eval_exec.cpp +++ b/eval/eval_exec.cpp @@ -56,8 +56,7 @@ void printHeader(Util::prog_opts::variables_map vm) std::cout << "Matching algorithm: " << LIB::MATCH_IMPL.kind() << "\n"; std::cout << "SCC algorithm: " << LIB::SCC_IMPL.kind() << "\n"; std::cout << "MFVS algorithm: " << LIB::MFVS_IMPL.kind() << "\n"; - std::cout << "Topological sorting algorithm: " << LIB::TS_IMPL.kind() - << "\n"; + std::cout << "Topological sorting algorithm: " << LIB::TS_IMPL.kind() << "\n"; } std::cout << "-----------------------------------\n"; std::cout << ">>>>>>>>>>> Eval result <<<<<<<<<<<\n"; @@ -70,27 +69,24 @@ void printHeader(Util::prog_opts::variables_map vm) EvalExecutor::EvalExecutor() : _scc_impl(2), _mfvs_impl(2) { - _config.add_options() - ("set_impl,s", Util::prog_opts::value(&_set_impl), - " Desired set implementation:" - "\n - 0 for unordered sets (default option)" - "\n - 1 for ordered sets" - "\n - 2 for unidimensional ordered dense sets") - ("pw_impl,p", Util::prog_opts::value(&_pw_impl), - " Desired PWMap implementation:" - "\n - 0 for unordered PWMaps (default option)" - "\n - 1 for ordered PWMaps" - "\n - 2 for domain ordered PWMaps") - ("scc_impl", Util::prog_opts::value(&_scc_impl), - "Desired SCC algorithm implementation:" - "\n - 0 for V1 of minimum reachable SCC" - "\n - 1 for V2 of minimum reachable SCC" - "\n - 2 for V3 of minimum reachable SCC (default option)") - ("mfvs_impl", Util::prog_opts::value(&_mfvs_impl), - "Desired MFVS algorithm implementation:" - "\n - 0 for degree greedy MFVS" - "\n - 1 for smallest set-vertex MFVS" - "\n - 2 for parallel SCC (default option)"); + _config.add_options()("set_impl,s", Util::prog_opts::value(&_set_impl), + " Desired set implementation:" + "\n - 0 for unordered sets (default option)" + "\n - 1 for ordered sets" + "\n - 2 for unidimensional ordered dense sets")("pw_impl,p", Util::prog_opts::value(&_pw_impl), + " Desired PWMap implementation:" + "\n - 0 for unordered PWMaps (default option)" + "\n - 1 for ordered PWMaps" + "\n - 2 for domain ordered PWMaps")( + "scc_impl", Util::prog_opts::value(&_scc_impl), + "Desired SCC algorithm implementation:" + "\n - 0 for V1 of minimum reachable SCC" + "\n - 1 for V2 of minimum reachable SCC" + "\n - 2 for V3 of minimum reachable SCC (default option)")("mfvs_impl", Util::prog_opts::value(&_mfvs_impl), + "Desired MFVS algorithm implementation:" + "\n - 0 for degree greedy MFVS" + "\n - 1 for smallest set-vertex MFVS" + "\n - 2 for parallel SCC (default option)"); // First option without name is the input file _positional.add("input-file", 1); @@ -104,8 +100,7 @@ detail::EvalUserInput EvalExecutor::chooseImplementation() detail::EvalUserInput result; detail::AutomImplVisitor autom_impl_visitor; - detail::EvalUserInput autom_impl = autom_impl_visitor.visit(Parser::parseFile( - *_input_file, false)); + detail::EvalUserInput autom_impl = autom_impl_visitor.visit(Parser::parseFile(*_input_file, false)); result.set_set_impl(autom_impl.set_impl()); result.set_pw_impl(autom_impl.pw_impl()); @@ -136,16 +131,15 @@ void EvalExecutor::execute(int argc, char* argv[]) // Command line options handling --------------------------------------------- Util::prog_opts::variables_map vm; - store(Util::prog_opts::command_line_parser(argc, argv) - .options(_cmd_line_opts).positional(_positional).run(), vm); + store(Util::prog_opts::command_line_parser(argc, argv).options(_cmd_line_opts).positional(_positional).run(), vm); notify(vm); // Help handling ------------------------------------------------------------- - + if (vm.count("help")) { std::cout << "Usage: filename [options]\n"; std::cout << "Command line options are prioritized over configuration file" - " options."; + " options."; std::cout << _visible << "\n"; return; } @@ -158,8 +152,8 @@ void EvalExecutor::execute(int argc, char* argv[]) } // Optional configuration file handling -------------------------------------- - - if (_config_file) { + + if (_config_file) { std::ifstream config_fs{(*_config_file).c_str()}; if (config_fs) { store(parse_config_file(config_fs, _cfg_file_opts), vm); @@ -177,17 +171,16 @@ void EvalExecutor::execute(int argc, char* argv[]) detail::EvalUserInput input = chooseImplementation(); detail::InputTranslator input_translator; input_translator.translate(input); - ProgramIO eval_result = parseEvalFile(*_input_file); + ProgramIO eval_result = parseEvalFile(*_input_file); printHeader(vm); std::cout << eval_result; Util::Internal::TimeProfiler::print_execution_time(); - } - else { + } else { std::cout << "Usage: filename [options]\n"; Util::ERROR("EvalExecutor: must provide a SBG program filename\n"); } } -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/file_evaluator.cpp b/eval/file_evaluator.cpp index df0485f6..11289f12 100644 --- a/eval/file_evaluator.cpp +++ b/eval/file_evaluator.cpp @@ -32,12 +32,12 @@ ProgramIO parseEvalFile(std::string fname) { AST::SBGProgram parser_result = Parser::parseFile(fname); - detail::ProgramEvaluator program_visit; + detail::ProgramEvaluator program_visit; ProgramIO visit_result = program_visit.evaluate(parser_result); return visit_result; } -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/func_env.cpp b/eval/func_env.cpp old mode 100755 new mode 100644 index 1b74d3ea..a12366ca --- a/eval/func_env.cpp +++ b/eval/func_env.cpp @@ -27,28 +27,16 @@ namespace detail { FuncEnv::FuncEnv() {} -FuncEnv::FIt FuncEnv::begin() const -{ - return _functions.begin(); -} +FuncEnv::FIt FuncEnv::begin() const { return _functions.begin(); } -FuncEnv::FIt FuncEnv::end() const -{ - return _functions.end(); -} +FuncEnv::FIt FuncEnv::end() const { return _functions.end(); } -FuncEnv::FIt FuncEnv::find(const FuncEnv::FKey& key) const -{ - return _functions.find(key); -} +FuncEnv::FIt FuncEnv::find(const FuncEnv::FKey& key) const { return _functions.find(key); } -void FuncEnv::insert(FuncEnv::FKey key, FuncEnv::FValue value) -{ - _functions[key] = value; -} +void FuncEnv::insert(FuncEnv::FKey key, FuncEnv::FValue value) { _functions[key] = value; } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/input_translator.cpp b/eval/input_translator.cpp old mode 100755 new mode 100644 index 8a3b8746..48c75461 --- a/eval/input_translator.cpp +++ b/eval/input_translator.cpp @@ -35,8 +35,9 @@ void InputTranslator::translate(EvalUserInput& input) EvalUserInput::MaybeInt pw = input.pw_impl(); if (s && pw) { if (*s == 0 && *pw == 2) { - Util::ERROR("Domain ordered PWMap should be used with an ordered set" - " implementation\n"); + Util::ERROR( + "Domain ordered PWMap should be used with an ordered set" + " implementation\n"); } else { setSetFactory(*s); setPWFactory(*pw); @@ -58,8 +59,8 @@ void InputTranslator::translate(EvalUserInput& input) } } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/pretty_print.cpp b/eval/pretty_print.cpp old mode 100755 new mode 100644 index 85a6ac2f..9417c9aa --- a/eval/pretty_print.cpp +++ b/eval/pretty_print.cpp @@ -25,10 +25,10 @@ namespace SBG { namespace Eval { -template +template std::ostream& operator<<(std::ostream& out, const std::variant& v) { - std::visit([&out](auto&& arg){ out << arg; }, v); + std::visit([&out](auto&& arg) { out << arg; }, v); return out; } @@ -37,7 +37,7 @@ template std::ostream& operator<<(std::ostream& out, const ExprBaseType& v); std::ostream& operator<<(std::ostream& out, const ExprResult& e) { - out << std::get<0>(e) << "\n --> " << std::get<1>(e) << "\n"; + out << std::get<0>(e) << "\n --> " << std::get<1>(e) << "\n"; return out; } @@ -67,11 +67,9 @@ std::ostream& operator<<(std::ostream& out, const StmResultList& ss) return out; } -ProgramIO::ProgramIO(StmResultList stms, ExprResultList exprs) - : _arity(1), _stms(stms), _exprs(exprs) {} +ProgramIO::ProgramIO(StmResultList stms, ExprResultList exprs) : _arity(1), _stms(stms), _exprs(exprs) {} -ProgramIO::ProgramIO(std::size_t n, StmResultList stms, ExprResultList exprs) - : _arity(n), _stms(stms), _exprs(exprs) {} +ProgramIO::ProgramIO(std::size_t n, StmResultList stms, ExprResultList exprs) : _arity(n), _stms(stms), _exprs(exprs) {} const std::size_t& ProgramIO::arity() const { return _arity; } @@ -90,6 +88,6 @@ std::ostream& operator<<(std::ostream& out, const ProgramIO& p) return out; } -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/user_impl_map.cpp b/eval/user_impl_map.cpp old mode 100755 new mode 100644 index a46231fe..90bd137c --- a/eval/user_impl_map.cpp +++ b/eval/user_impl_map.cpp @@ -34,26 +34,21 @@ namespace detail { UserImplMap::StructImplMap::StructImplMap() : _is_frozen(false) {} -UserImplMap::StructImplMap::SValue& UserImplMap::StructImplMap::operator[] - (const SKey& key) +UserImplMap::StructImplMap::SValue& UserImplMap::StructImplMap::operator[](const SKey& key) { - Util::ERROR_UNLESS(!_is_frozen, "StructImplMap: map is frozen, insertion is" - " prohibited.\n"); + Util::ERROR_UNLESS(!_is_frozen, + "StructImplMap: map is frozen, insertion is" + " prohibited.\n"); return _struct_impls[key]; } -const UserImplMap::StructImplMap::SValue& UserImplMap::StructImplMap::operator[] - (const SKey& key) const +const UserImplMap::StructImplMap::SValue& UserImplMap::StructImplMap::operator[](const SKey& key) const { - Util::ERROR_UNLESS(_struct_impls.find(key) != _struct_impls.end() - , "StructImplMap: undefined implementation.\n"); - return _struct_impls.at(key); + Util::ERROR_UNLESS(_struct_impls.find(key) != _struct_impls.end(), "StructImplMap: undefined implementation.\n"); + return _struct_impls.at(key); } -void UserImplMap::StructImplMap::freeze() -{ - _is_frozen = true; -} +void UserImplMap::StructImplMap::freeze() { _is_frozen = true; } UserImplMap::StructImplMap setMap() { @@ -131,7 +126,7 @@ Kind UserImplMap::getFactory(std::string strct, int impl) return struct_map[impl]; } -} // namespace detail +} // namespace detail //////////////////////////////////////////////////////////////////////////////// // Extra operations ------------------------------------------------------------ @@ -139,22 +134,19 @@ Kind UserImplMap::getFactory(std::string strct, int impl) void setSetFactory(int set_impl) { - LIB::SetKind set_fact = std::get(detail::IMPL_MAP.getFactory( - "set", set_impl)); + LIB::SetKind set_fact = std::get(detail::IMPL_MAP.getFactory("set", set_impl)); LIB::SET_IMPL.set_set_fact(set_fact); } void setPWFactory(int pw_impl) { - LIB::PWMapKind pwmap_fact = std::get( - detail::IMPL_MAP.getFactory("pwmap", pw_impl)); + LIB::PWMapKind pwmap_fact = std::get(detail::IMPL_MAP.getFactory("pwmap", pw_impl)); LIB::PWMAP_IMPL.set_pwmap_fact(pwmap_fact); } void setSCCFactory(int scc_impl) { - LIB::SCCKind scc_fact = std::get( - detail::IMPL_MAP.getFactory("scc", scc_impl)); + LIB::SCCKind scc_fact = std::get(detail::IMPL_MAP.getFactory("scc", scc_impl)); LIB::SCC_IMPL.set_scc_fact(scc_fact); return; @@ -162,13 +154,12 @@ void setSCCFactory(int scc_impl) void setMFVSFactory(int mfvs_impl) { - LIB::MFVSKind mfvs_fact = std::get( - detail::IMPL_MAP.getFactory("mfvs", mfvs_impl)); + LIB::MFVSKind mfvs_fact = std::get(detail::IMPL_MAP.getFactory("mfvs", mfvs_impl)); LIB::MFVS_IMPL.set_mfvs_fact(mfvs_fact); return; } -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/user_input.cpp b/eval/user_input.cpp old mode 100755 new mode 100644 index 7d60ece9..0fb97e05 --- a/eval/user_input.cpp +++ b/eval/user_input.cpp @@ -31,70 +31,34 @@ EvalUserInput::EvalUserInput() {} // Getters --------------------------------------------------------------------- -EvalUserInput::MaybeInt EvalUserInput::set_impl() const -{ - return _set_impl; -} - -EvalUserInput::MaybeInt EvalUserInput::pw_impl() const -{ - return _pw_impl; -} - -EvalUserInput::MaybeInt EvalUserInput::match_impl() const -{ - return _match_impl; -} - -EvalUserInput::MaybeInt EvalUserInput::scc_impl() const -{ - return _scc_impl; -} - -EvalUserInput::MaybeInt EvalUserInput::mfvs_impl() const -{ - return _mfvs_impl; -} - -EvalUserInput::MaybeInt EvalUserInput::ts_impl() const -{ - return _ts_impl; -} +EvalUserInput::MaybeInt EvalUserInput::set_impl() const { return _set_impl; } + +EvalUserInput::MaybeInt EvalUserInput::pw_impl() const { return _pw_impl; } + +EvalUserInput::MaybeInt EvalUserInput::match_impl() const { return _match_impl; } + +EvalUserInput::MaybeInt EvalUserInput::scc_impl() const { return _scc_impl; } + +EvalUserInput::MaybeInt EvalUserInput::mfvs_impl() const { return _mfvs_impl; } + +EvalUserInput::MaybeInt EvalUserInput::ts_impl() const { return _ts_impl; } // Setters --------------------------------------------------------------------- -void EvalUserInput::set_set_impl(MaybeInt set_impl) -{ - _set_impl = set_impl; -} +void EvalUserInput::set_set_impl(MaybeInt set_impl) { _set_impl = set_impl; } -void EvalUserInput::set_pw_impl(MaybeInt pw_impl) -{ - _pw_impl = pw_impl; -} +void EvalUserInput::set_pw_impl(MaybeInt pw_impl) { _pw_impl = pw_impl; } -void EvalUserInput::set_match_impl(MaybeInt match_impl) -{ - _match_impl = match_impl; -} +void EvalUserInput::set_match_impl(MaybeInt match_impl) { _match_impl = match_impl; } -void EvalUserInput::set_scc_impl(MaybeInt scc_impl) -{ - _scc_impl = scc_impl; -} +void EvalUserInput::set_scc_impl(MaybeInt scc_impl) { _scc_impl = scc_impl; } -void EvalUserInput::set_mfvs_impl(MaybeInt mfvs_impl) -{ - _mfvs_impl = mfvs_impl; -} +void EvalUserInput::set_mfvs_impl(MaybeInt mfvs_impl) { _mfvs_impl = mfvs_impl; } -void EvalUserInput::set_ts_impl(MaybeInt ts_impl) -{ - _ts_impl = ts_impl; -} +void EvalUserInput::set_ts_impl(MaybeInt ts_impl) { _ts_impl = ts_impl; } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/var_env.cpp b/eval/var_env.cpp old mode 100755 new mode 100644 index f2a95774..0b4222ad --- a/eval/var_env.cpp +++ b/eval/var_env.cpp @@ -27,28 +27,16 @@ namespace detail { VarEnv::VarEnv() : _variables() {} -VarEnv::VIt VarEnv::begin() const -{ - return _variables.begin(); -} +VarEnv::VIt VarEnv::begin() const { return _variables.begin(); } -VarEnv::VIt VarEnv::end() const -{ - return _variables.end(); -} +VarEnv::VIt VarEnv::end() const { return _variables.end(); } -VarEnv::VIt VarEnv::find(const VarEnv::VKey& key) const -{ - return _variables.find(key); -} +VarEnv::VIt VarEnv::find(const VarEnv::VKey& key) const { return _variables.find(key); } -void VarEnv::insert(VarEnv::VKey key, VarEnv::VValue value) -{ - _variables[key] = value; -} +void VarEnv::insert(VarEnv::VKey key, VarEnv::VValue value) { _variables[key] = value; } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/visitors/autom_impl_visitor.cpp b/eval/visitors/autom_impl_visitor.cpp old mode 100755 new mode 100644 index bbdc5559..2e9e1c32 --- a/eval/visitors/autom_impl_visitor.cpp +++ b/eval/visitors/autom_impl_visitor.cpp @@ -35,8 +35,8 @@ namespace detail { AutomImplVisitor::AutomImplVisitor() {} EvalUserInput AutomImplVisitor::visit(AST::SBGProgram p) const -{ - // Statement inspection ------------------------------------------------------ +{ + // Statement inspection ------------------------------------------------------ AST::IsConfig cfg_visit; EvalContext eval_context; @@ -76,8 +76,8 @@ EvalUserInput AutomImplVisitor::visit(AST::SBGProgram p) const return result; } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/visitors/expr_evaluator.cpp b/eval/visitors/expr_evaluator.cpp old mode 100755 new mode 100644 index 976ab7c1..3722cac7 --- a/eval/visitors/expr_evaluator.cpp +++ b/eval/visitors/expr_evaluator.cpp @@ -34,13 +34,12 @@ namespace detail { // Expression evaluator -------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// -template +template T eval(const ExprEvaluator& visit, AST::Expr e, std::string t = "UNDEF") { ExprBaseType visited = boost::apply_visitor(visit, e); - Util::ERROR_UNLESS(std::holds_alternative(visited) - , "ExprEvaluator: expression ", e, " is not of type ", t, "\n"); + Util::ERROR_UNLESS(std::holds_alternative(visited), "ExprEvaluator: expression ", e, " is not of type ", t, "\n"); return std::get(visited); } @@ -69,26 +68,22 @@ ExprEvaluator::ExprEvaluator(EvalContext& eval_ctx) : _eval_context(eval_ctx) eval_ctx.insertFunction("sort", BuiltInFunctions::topoSortEvaluator); } -ExprBaseType ExprEvaluator::operator()(AST::Integer v) const -{ - return (LIB::Int) v; -} +ExprBaseType ExprEvaluator::operator()(AST::Integer v) const { return (LIB::Int)v; } ExprBaseType ExprEvaluator::operator()(AST::Rational v) const { - return boost::apply_visitor(RationalEvaluator{_eval_context.venv()} - , AST::Expr{v}); + return boost::apply_visitor(RationalEvaluator{_eval_context.venv()}, AST::Expr{v}); } -ExprBaseType ExprEvaluator::operator()(AST::Name v) const +ExprBaseType ExprEvaluator::operator()(AST::Name v) const { auto var_definition = _eval_context.venv().find(v); - if (var_definition != _eval_context.venv().end()) { + if (var_definition != _eval_context.venv().end()) { return var_definition->second; } Util::ERROR("ExprEvaluator: variable ", v, " undefined\n"); - return ExprBaseType{}; + return ExprBaseType{}; } ExprBaseType ExprEvaluator::operator()(AST::UnaryOp v) const @@ -101,7 +96,7 @@ ExprBaseType ExprEvaluator::operator()(AST::UnaryOp v) const } ExprBaseType ExprEvaluator::operator()(AST::BinOp v) const -{ +{ ExprBaseType left = boost::apply_visitor(*this, v.left()); ExprBaseType right = boost::apply_visitor(*this, v.right()); EBTList evaluated_args; @@ -116,7 +111,7 @@ ExprBaseType ExprEvaluator::operator()(AST::Call v) const std::string func_name = v.name(); auto func_definition = _eval_context.fenv().find(func_name); - if (func_definition != _eval_context.fenv().end()) { + if (func_definition != _eval_context.fenv().end()) { std::vector evaluated_args; for (AST::Expr a : v.args()) { evaluated_args.push_back(boost::apply_visitor(*this, a)); @@ -145,7 +140,7 @@ ExprBaseType ExprEvaluator::operator()(AST::MultiDimInter v) const LIB::Set result; int i = 0; - for (const AST::Expr& e : v.intervals()) { + for (const AST::Expr& e : v.intervals()) { LIB::Set kth_component = eval(*this, e, "Set"); if (i == 0) { result = std::move(kth_component); @@ -155,14 +150,12 @@ ExprBaseType ExprEvaluator::operator()(AST::MultiDimInter v) const ++i; } - Util::ERROR_UNLESS(result.arity() == _eval_context.arity() - || result.arity() == 0, "EvalMDI[arity = ", _eval_context.arity() - , "]: arity(", result, ") = ", result.arity(), "\n"); + Util::ERROR_UNLESS(result.arity() == _eval_context.arity() || result.arity() == 0, "EvalMDI[arity = ", _eval_context.arity(), "]: arity(", + result, ") = ", result.arity(), "\n"); return result; } - ExprBaseType ExprEvaluator::operator()(AST::Set v) const { LIB::Set result; @@ -172,9 +165,8 @@ ExprBaseType ExprEvaluator::operator()(AST::Set v) const result = std::move(result).disjointCup(std::move(jth_element)); } - Util::ERROR_UNLESS(result.arity() == _eval_context.arity() - || result.arity() == 0, "ExprEvaluator[arity = ", _eval_context.arity() - , "]: arity(", result, ") = ", result.arity(), "\n"); + Util::ERROR_UNLESS(result.arity() == _eval_context.arity() || result.arity() == 0, "ExprEvaluator[arity = ", _eval_context.arity(), + "]: arity(", result, ") = ", result.arity(), "\n"); return result; } @@ -182,8 +174,7 @@ ExprBaseType ExprEvaluator::operator()(AST::Set v) const ExprBaseType ExprEvaluator::operator()(AST::LinearExpr v) const { LinearExprEvaluator linear_expr_evaluator{_eval_context.venv()}; - LIB::detail::LinearExpr linear_expr - = boost::apply_visitor(linear_expr_evaluator, AST::Expr(v)); + LIB::detail::LinearExpr linear_expr = boost::apply_visitor(linear_expr_evaluator, AST::Expr(v)); return LIB::Expression{linear_expr.slope(), linear_expr.offset()}; } @@ -193,15 +184,12 @@ ExprBaseType ExprEvaluator::operator()(AST::MDLExp v) const LinearExprEvaluator linear_expr_evaluator{_eval_context.venv()}; for (const AST::Expr& e : v.exps()) { - LIB::detail::LinearExpr kth_component - = boost::apply_visitor(linear_expr_evaluator, e); - result = result.cartesianProduct(LIB::Expression{kth_component.slope() - , kth_component.offset()}); + LIB::detail::LinearExpr kth_component = boost::apply_visitor(linear_expr_evaluator, e); + result = result.cartesianProduct(LIB::Expression{kth_component.slope(), kth_component.offset()}); } - Util::ERROR_UNLESS(result.arity() == _eval_context.arity() - || result.arity() == 0, "ExprEvaluator[arity = ", _eval_context.arity(), - "]: arity(", result, ") = ", result.arity(), "\n"); + Util::ERROR_UNLESS(result.arity() == _eval_context.arity() || result.arity() == 0, "ExprEvaluator[arity = ", _eval_context.arity(), + "]: arity(", result, ") = ", result.arity(), "\n"); return result; } @@ -213,9 +201,8 @@ ExprBaseType ExprEvaluator::operator()(AST::LinearMap v) const LIB::Map result{domain, law}; - Util::ERROR_UNLESS(result.arity() == _eval_context.arity() - || result.arity() == 0, "ExprEvaluator[arity = ", _eval_context.arity() - , "]: arity(", result, ") = ", result.arity(), "\n"); + Util::ERROR_UNLESS(result.arity() == _eval_context.arity() || result.arity() == 0, "ExprEvaluator[arity = ", _eval_context.arity(), + "]: arity(", result, ") = ", result.arity(), "\n"); return result; } @@ -228,9 +215,8 @@ ExprBaseType ExprEvaluator::operator()(AST::PWLMap v) const result.insert(eval(*this, e, "Map")); } - Util::ERROR_UNLESS(result.arity() == _eval_context.arity() - || result.arity() == 0, "ExprEvaluator[arity = ", _eval_context.arity() - , "]: arity(", result, ") = ", result.arity(), "\n"); + Util::ERROR_UNLESS(result.arity() == _eval_context.arity() || result.arity() == 0, "ExprEvaluator[arity = ", _eval_context.arity(), + "]: arity(", result, ") = ", result.arity(), "\n"); return result; } @@ -270,13 +256,10 @@ ExprBaseType ExprEvaluator::operator()(AST::DSBG v) const return LIB::DirectedSBG{V, Vmap, mapB, mapD, Emap}; } -ExprBaseType ExprEvaluator::operator()(AST::ParenExpr v) const -{ - return boost::apply_visitor(*this, v.pexpr()); -} +ExprBaseType ExprEvaluator::operator()(AST::ParenExpr v) const { return boost::apply_visitor(*this, v.pexpr()); } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/visitors/func_evaluator.cpp b/eval/visitors/func_evaluator.cpp old mode 100755 new mode 100644 index 47465300..87d3dcbf --- a/eval/visitors/func_evaluator.cpp +++ b/eval/visitors/func_evaluator.cpp @@ -48,211 +48,154 @@ namespace detail { ExprBaseType BuiltInOperators::oppositeEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "oppositeEvaluator: wrong number of arguments\n"); - - auto opposite_evaluator = Util::Overload { - [](LIB::Int a) { return ExprBaseType{-a}; }, - [](LIB::Rational a) { return ExprBaseType{LIB::Rational{-1}*a}; }, - [](LIB::Set a) { return ExprBaseType{a.complement()}; }, - [](auto a) { - Util::ERROR("oppositeEvaluator: wrong type argument ", a - , " for - (opposite)\n"); - return ExprBaseType{LIB::Rational{0}}; - } - }; + Util::ERROR_UNLESS(args.size() == 1, "oppositeEvaluator: wrong number of arguments\n"); + + auto opposite_evaluator = + Util::Overload{[](LIB::Int a) { return ExprBaseType{-a}; }, [](LIB::Rational a) { return ExprBaseType{LIB::Rational{-1} * a}; }, + [](LIB::Set a) { return ExprBaseType{a.complement()}; }, + [](auto a) { + Util::ERROR("oppositeEvaluator: wrong type argument ", a, " for - (opposite)\n"); + return ExprBaseType{LIB::Rational{0}}; + }}; return std::visit(opposite_evaluator, args[0]); } ExprBaseType BuiltInOperators::cardinalEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "cardinalEvaluator: wrong number of arguments\n"); + Util::ERROR_UNLESS(args.size() == 1, "cardinalEvaluator: wrong number of arguments\n"); - const auto cardinal_evaluator = Util::Overload { - [](LIB::Set a) { return (LIB::Int) a.cardinal(); }, - [](auto a) { - Util::ERROR("cardinalEvaluator: wrong argument ", a, " for #\n"); - return (LIB::Int) 0; - } - }; + const auto cardinal_evaluator = Util::Overload{[](LIB::Set a) { return (LIB::Int)a.cardinal(); }, + [](auto a) { + Util::ERROR("cardinalEvaluator: wrong argument ", a, " for #\n"); + return (LIB::Int)0; + }}; return std::visit(cardinal_evaluator, args[0]); } ExprBaseType BuiltInOperators::complementEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "complementEvaluator: wrong number of arguments\n"); - - const auto complement_evaluator = Util::Overload { - [](LIB::Set a) { return ExprBaseType{a.complement()}; }, - [](auto a) { - Util::ERROR("complementEvaluator: wrong argument ", a - , " for \' (complement)\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 1, "complementEvaluator: wrong number of arguments\n"); + + const auto complement_evaluator = Util::Overload{[](LIB::Set a) { return ExprBaseType{a.complement()}; }, + [](auto a) { + Util::ERROR("complementEvaluator: wrong argument ", a, " for \' (complement)\n"); + return ExprBaseType{}; + }}; return std::visit(complement_evaluator, args[0]); } ExprBaseType BuiltInOperators::addEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 2 - , "addEvaluator: wrong number of arguments\n"); - - const auto add_evaluator = Util::Overload { - [](LIB::Int a, LIB::Int b) { return ExprBaseType{a + b}; }, - [](LIB::IntTuple a, LIB::IntTuple b) { return ExprBaseType{a + b}; }, - [](LIB::Rational a, LIB::Rational b) { return ExprBaseType{a + b}; }, - [](LIB::Int a, LIB::Rational b) { - return ExprBaseType{LIB::Rational{a} + b}; - }, - [](LIB::Rational a, LIB::Int b) { - return ExprBaseType{a + LIB::Rational{b}}; - }, - [](LIB::Expression a, LIB::Expression b) { return ExprBaseType{a + b}; }, - [](LIB::Map a, LIB::Map b) { return ExprBaseType{a + b}; }, - [](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a + b}; }, - [](auto a, auto b) { - Util::ERROR("addEvaluator: wrong arguments ", a, ", ", b - , " for operator+\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 2, "addEvaluator: wrong number of arguments\n"); + + const auto add_evaluator = Util::Overload{[](LIB::Int a, LIB::Int b) { return ExprBaseType{a + b}; }, + [](LIB::IntTuple a, LIB::IntTuple b) { return ExprBaseType{a + b}; }, + [](LIB::Rational a, LIB::Rational b) { return ExprBaseType{a + b}; }, + [](LIB::Int a, LIB::Rational b) { return ExprBaseType{LIB::Rational{a} + b}; }, + [](LIB::Rational a, LIB::Int b) { return ExprBaseType{a + LIB::Rational{b}}; }, + [](LIB::Expression a, LIB::Expression b) { return ExprBaseType{a + b}; }, + [](LIB::Map a, LIB::Map b) { return ExprBaseType{a + b}; }, + [](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a + b}; }, + [](auto a, auto b) { + Util::ERROR("addEvaluator: wrong arguments ", a, ", ", b, " for operator+\n"); + return ExprBaseType{}; + }}; return std::visit(add_evaluator, args[0], args[1]); } ExprBaseType BuiltInOperators::subEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 2 - , "subEvaluator: wrong number of arguments\n"); - - const auto sub_evaluator = Util::Overload { - [](LIB::Int a, LIB::Int b) { return ExprBaseType{a - b}; }, - [](LIB::Rational a, LIB::Rational b) { return ExprBaseType{a - b}; }, - [](LIB::Int a, LIB::Rational b) { - return ExprBaseType{LIB::Rational{a} - b}; - }, - [](LIB::Rational a, LIB::Int b) { - return ExprBaseType{a - LIB::Rational{b}}; - }, - [](LIB::Expression a, LIB::Expression b) { return ExprBaseType{a - b}; }, - [](auto a, auto b) { - Util::ERROR("subEvaluator: wrong arguments ", a, ", ", b - , " for operator-\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 2, "subEvaluator: wrong number of arguments\n"); + + const auto sub_evaluator = Util::Overload{[](LIB::Int a, LIB::Int b) { return ExprBaseType{a - b}; }, + [](LIB::Rational a, LIB::Rational b) { return ExprBaseType{a - b}; }, + [](LIB::Int a, LIB::Rational b) { return ExprBaseType{LIB::Rational{a} - b}; }, + [](LIB::Rational a, LIB::Int b) { return ExprBaseType{a - LIB::Rational{b}}; }, + [](LIB::Expression a, LIB::Expression b) { return ExprBaseType{a - b}; }, + [](auto a, auto b) { + Util::ERROR("subEvaluator: wrong arguments ", a, ", ", b, " for operator-\n"); + return ExprBaseType{}; + }}; return std::visit(sub_evaluator, args[0], args[1]); } ExprBaseType BuiltInOperators::multEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 2 - , "multEvaluator: wrong number of arguments\n"); - - const auto mult_evaluator = Util::Overload { - [](LIB::Int a, LIB::Int b) { return ExprBaseType{a*b}; }, - [](LIB::Int a, LIB::Rational b) - { - return ExprBaseType{LIB::Rational{a}*b}; - }, - [](LIB::Rational a, LIB::Int b) - { - return ExprBaseType{a*LIB::Rational{b}}; - }, - [](auto a, auto b) { - Util::ERROR("multEvaluator: wrong arguments ", a, ", ", b - , " for operator*\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 2, "multEvaluator: wrong number of arguments\n"); + + const auto mult_evaluator = Util::Overload{[](LIB::Int a, LIB::Int b) { return ExprBaseType{a * b}; }, + [](LIB::Int a, LIB::Rational b) { return ExprBaseType{LIB::Rational{a} * b}; }, + [](LIB::Rational a, LIB::Int b) { return ExprBaseType{a * LIB::Rational{b}}; }, + [](auto a, auto b) { + Util::ERROR("multEvaluator: wrong arguments ", a, ", ", b, " for operator*\n"); + return ExprBaseType{}; + }}; return std::visit(mult_evaluator, args[0], args[1]); } ExprBaseType BuiltInOperators::eqEvaluator(const EBTList& args) -{ - Util::ERROR_UNLESS(args.size() == 2 - , "eqEvaluator: wrong number of arguments\n"); - - const auto eq_evaluator = Util::Overload { - [](LIB::IntTuple a, LIB::IntTuple b) { return a == b; }, - [](LIB::Rational a, LIB::Rational b) { return a == b; }, - [](LIB::Set a, LIB::Set b) { return a == b; }, - [](LIB::Expression a, LIB::Expression b) { return a == b; }, - [](LIB::Map a, LIB::Map b) { return a == b; }, - [](LIB::PWMap a, LIB::PWMap b) { return a == b; }, - [](auto a, auto b) { - Util::ERROR("eqEvaluator: wrong arguments ", a, ", ", b - , " for operator==\n"); - return false; - } - }; +{ + Util::ERROR_UNLESS(args.size() == 2, "eqEvaluator: wrong number of arguments\n"); + + const auto eq_evaluator = Util::Overload{[](LIB::IntTuple a, LIB::IntTuple b) { return a == b; }, + [](LIB::Rational a, LIB::Rational b) { return a == b; }, + [](LIB::Set a, LIB::Set b) { return a == b; }, + [](LIB::Expression a, LIB::Expression b) { return a == b; }, + [](LIB::Map a, LIB::Map b) { return a == b; }, + [](LIB::PWMap a, LIB::PWMap b) { return a == b; }, + [](auto a, auto b) { + Util::ERROR("eqEvaluator: wrong arguments ", a, ", ", b, " for operator==\n"); + return false; + }}; return std::visit(eq_evaluator, args[0], args[1]); } ExprBaseType BuiltInOperators::lessEvaluator(const EBTList& args) -{ - Util::ERROR_UNLESS(args.size() == 2 - , "lessEvaluator: wrong number of arguments\n"); - - const auto less_evaluator = Util::Overload { - [](LIB::IntTuple a, LIB::IntTuple b) { return a < b; }, - [](LIB::Rational a, LIB::Rational b) { return a < b; }, - [](auto a, auto b) { - Util::ERROR("lessEvaluator: wrong arguments ", a, ", ", b - , " for operator<\n"); - return false; - } - }; +{ + Util::ERROR_UNLESS(args.size() == 2, "lessEvaluator: wrong number of arguments\n"); + + const auto less_evaluator = + Util::Overload{[](LIB::IntTuple a, LIB::IntTuple b) { return a < b; }, [](LIB::Rational a, LIB::Rational b) { return a < b; }, + [](auto a, auto b) { + Util::ERROR("lessEvaluator: wrong arguments ", a, ", ", b, " for operator<\n"); + return false; + }}; return std::visit(less_evaluator, args[0], args[1]); } ExprBaseType BuiltInOperators::capEvaluator(const EBTList& args) -{ - Util::ERROR_UNLESS(args.size() == 2 - , "capEvaluator: wrong number of arguments\n"); - - const auto cap_evaluator = Util::Overload{ - [](LIB::Set a, LIB::Set b) { return ExprBaseType{a.intersection(b)}; }, - [](auto a, auto b) { - Util::ERROR("capEvaluator: wrong arguments ", a, ", ", b - , " for intersection\n"); - return ExprBaseType{}; - } - }; +{ + Util::ERROR_UNLESS(args.size() == 2, "capEvaluator: wrong number of arguments\n"); + + const auto cap_evaluator = Util::Overload{[](LIB::Set a, LIB::Set b) { return ExprBaseType{a.intersection(b)}; }, + [](auto a, auto b) { + Util::ERROR("capEvaluator: wrong arguments ", a, ", ", b, " for intersection\n"); + return ExprBaseType{}; + }}; return std::visit(cap_evaluator, args[0], args[1]); } ExprBaseType BuiltInOperators::cupEvaluator(const EBTList& args) -{ - Util::ERROR_UNLESS(args.size() == 2 - , "cupEvaluator: wrong number of arguments\n"); - - const auto cup_evaluator = Util::Overload{ - [](LIB::Set a, LIB::Set b) { return ExprBaseType{a.cup(b)}; }, - [](auto a, auto b) { - Util::ERROR("cupEvaluator: wrong arguments ", a, ", ", b - , " for union\n"); - return ExprBaseType{}; - } - }; +{ + Util::ERROR_UNLESS(args.size() == 2, "cupEvaluator: wrong number of arguments\n"); + + const auto cup_evaluator = Util::Overload{[](LIB::Set a, LIB::Set b) { return ExprBaseType{a.cup(b)}; }, + [](auto a, auto b) { + Util::ERROR("cupEvaluator: wrong arguments ", a, ", ", b, " for union\n"); + return ExprBaseType{}; + }}; return std::visit(cup_evaluator, args[0], args[1]); } ExprBaseType BuiltInOperators::diffEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 2 - , "diffEvaluator: wrong number of arguments\n"); - - const auto diff_evaluator = Util::Overload{ - [](LIB::Set a, LIB::Set b) { return ExprBaseType{a.difference(b)}; }, - [](auto a, auto b) { - Util::ERROR("diffEvaluator: wrong arguments ", a, ", ", b - , " for difference\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 2, "diffEvaluator: wrong number of arguments\n"); + + const auto diff_evaluator = Util::Overload{[](LIB::Set a, LIB::Set b) { return ExprBaseType{a.difference(b)}; }, + [](auto a, auto b) { + Util::ERROR("diffEvaluator: wrong arguments ", a, ", ", b, " for difference\n"); + return ExprBaseType{}; + }}; return std::visit(diff_evaluator, args[0], args[1]); } @@ -263,27 +206,27 @@ UnaryOpEvaluator::UnaryOpEvaluator() {} ExprBaseType UnaryOpEvaluator::evaluate(EBTList& evaluated_args, AST::UnOp op) { switch (op) { - case AST::UnOp::oppo: { - return BuiltInOperators::oppositeEvaluator(evaluated_args); - break; - } - - case AST::UnOp::card: { - return BuiltInOperators::cardinalEvaluator(evaluated_args); - break; - } - - case AST::UnOp::comp: { - return BuiltInOperators::complementEvaluator(evaluated_args); - break; - } - - default: - Util::ERROR("UnaryOpEvaluator: UnaryOp ", op, " unsupported\n"); - return ExprBaseType{}; + case AST::UnOp::oppo: { + return BuiltInOperators::oppositeEvaluator(evaluated_args); + break; + } + + case AST::UnOp::card: { + return BuiltInOperators::cardinalEvaluator(evaluated_args); + break; + } + + case AST::UnOp::comp: { + return BuiltInOperators::complementEvaluator(evaluated_args); + break; + } + + default: + Util::ERROR("UnaryOpEvaluator: UnaryOp ", op, " unsupported\n"); + return ExprBaseType{}; } - return ExprBaseType{}; + return ExprBaseType{}; } BinOpEvaluator::BinOpEvaluator() {} @@ -291,180 +234,151 @@ BinOpEvaluator::BinOpEvaluator() {} ExprBaseType BinOpEvaluator::evaluate(EBTList& evaluated_args, AST::Op op) { switch (op) { - case AST::Op::add: { - return BuiltInOperators::addEvaluator(evaluated_args); - break; - } - - case AST::Op::sub: { - return BuiltInOperators::subEvaluator(evaluated_args); - break; - } - - case AST::Op::mult: { - return BuiltInOperators::multEvaluator(evaluated_args); - break; - } - - case AST::Op::eq: { - return BuiltInOperators::eqEvaluator(evaluated_args); - break; - } - - case AST::Op::less: { - return BuiltInOperators::lessEvaluator(evaluated_args); - break; - } - - case AST::Op::cap: { - return BuiltInOperators::capEvaluator(evaluated_args); - break; - } - - case AST::Op::cup: { - return BuiltInOperators::cupEvaluator(evaluated_args); - break; - } - - case AST::Op::diff: { - return BuiltInOperators::diffEvaluator(evaluated_args); - break; - } - - default: { - Util::ERROR("BinOpEvaluator: BinOp ", op, " unsupported\n"); - return ExprBaseType{}; - } - } - - return ExprBaseType{}; + case AST::Op::add: { + return BuiltInOperators::addEvaluator(evaluated_args); + break; + } + + case AST::Op::sub: { + return BuiltInOperators::subEvaluator(evaluated_args); + break; + } + + case AST::Op::mult: { + return BuiltInOperators::multEvaluator(evaluated_args); + break; + } + + case AST::Op::eq: { + return BuiltInOperators::eqEvaluator(evaluated_args); + break; + } + + case AST::Op::less: { + return BuiltInOperators::lessEvaluator(evaluated_args); + break; + } + + case AST::Op::cap: { + return BuiltInOperators::capEvaluator(evaluated_args); + break; + } + + case AST::Op::cup: { + return BuiltInOperators::cupEvaluator(evaluated_args); + break; + } + + case AST::Op::diff: { + return BuiltInOperators::diffEvaluator(evaluated_args); + break; + } + + default: { + Util::ERROR("BinOpEvaluator: BinOp ", op, " unsupported\n"); + return ExprBaseType{}; + } + } + + return ExprBaseType{}; } //////////////////////////////////////////////////////////////////////////////// // Built-in functions evaluators ----------------------------------------------- //////////////////////////////////////////////////////////////////////////////// - + ExprBaseType BuiltInFunctions::emptyEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "emptyEvaluator: wrong number of arguments\n"); + Util::ERROR_UNLESS(args.size() == 1, "emptyEvaluator: wrong number of arguments\n"); - const auto empty_evaluator = Util::Overload { - [](LIB::Set a) { return a.isEmpty(); }, - [](auto a) { - Util::ERROR("emptyEvaluator: wrong argument ", a, " for isEmpty\n"); - return false; - } - }; + const auto empty_evaluator = Util::Overload{[](LIB::Set a) { return a.isEmpty(); }, + [](auto a) { + Util::ERROR("emptyEvaluator: wrong argument ", a, " for isEmpty\n"); + return false; + }}; return std::visit(empty_evaluator, args[0]); } - + ExprBaseType BuiltInFunctions::minEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "minEvaluator: wrong number of arguments\n"); + Util::ERROR_UNLESS(args.size() == 1, "minEvaluator: wrong number of arguments\n"); - const auto min_evaluator = Util::Overload { - [](LIB::Set a) { return a.minElem(); }, - [](auto a) { - Util::ERROR("minEvaluator: wrong argument ", a, " for minElem\n"); - return LIB::IntTuple{}; - } - }; + const auto min_evaluator = Util::Overload{[](LIB::Set a) { return a.minElem(); }, + [](auto a) { + Util::ERROR("minEvaluator: wrong argument ", a, " for minElem\n"); + return LIB::IntTuple{}; + }}; return std::visit(min_evaluator, args[0]); } - + ExprBaseType BuiltInFunctions::maxEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "maxEvaluator: wrong number of arguments\n"); + Util::ERROR_UNLESS(args.size() == 1, "maxEvaluator: wrong number of arguments\n"); - const auto max_evaluator = Util::Overload { - [](LIB::Set a) { return a.maxElem(); }, - [](auto a) { - Util::ERROR("maxEvaluator: wrong argument ", a, " for maxElem\n"); - return LIB::IntTuple{}; - } - }; + const auto max_evaluator = Util::Overload{[](LIB::Set a) { return a.maxElem(); }, + [](auto a) { + Util::ERROR("maxEvaluator: wrong argument ", a, " for maxElem\n"); + return LIB::IntTuple{}; + }}; return std::visit(max_evaluator, args[0]); } ExprBaseType BuiltInFunctions::restrictEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 2 - , "restrictEvaluator: wrong number of arguments\n"); - - const auto restrict_evaluator = Util::Overload { - [](LIB::PWMap a, LIB::Set b) { return ExprBaseType{a.restrict(b)}; }, - [](auto a, auto b) { - Util::ERROR("restrictEvaluator: wrong arguments ", a, ", ", b - , " for restrict\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 2, "restrictEvaluator: wrong number of arguments\n"); + + const auto restrict_evaluator = Util::Overload{[](LIB::PWMap a, LIB::Set b) { return ExprBaseType{a.restrict(b)}; }, + [](auto a, auto b) { + Util::ERROR("restrictEvaluator: wrong arguments ", a, ", ", b, " for restrict\n"); + return ExprBaseType{}; + }}; return std::visit(restrict_evaluator, args[0], args[1]); } - + ExprBaseType BuiltInFunctions::composeEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 2 - , "composeEvaluator: wrong number of arguments\n"); - - const auto compose_evaluator = Util::Overload { - [](LIB::Expression a, LIB::Expression b) - { - return ExprBaseType{a.composition(b)}; - }, - [](LIB::Map a, LIB::Map b) { return ExprBaseType{a.composition(b)}; }, - [](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a.composition(b)}; }, - [](auto a, auto b) { - Util::ERROR("composeEvaluator: wrong arguments ", a, ", ", b - , " for compose\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 2, "composeEvaluator: wrong number of arguments\n"); + + const auto compose_evaluator = Util::Overload{[](LIB::Expression a, LIB::Expression b) { return ExprBaseType{a.composition(b)}; }, + [](LIB::Map a, LIB::Map b) { return ExprBaseType{a.composition(b)}; }, + [](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a.composition(b)}; }, + [](auto a, auto b) { + Util::ERROR("composeEvaluator: wrong arguments ", a, ", ", b, " for compose\n"); + return ExprBaseType{}; + }}; return std::visit(compose_evaluator, args[0], args[1]); } - + ExprBaseType BuiltInFunctions::inverseEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "inverseEvaluator: wrong number of arguments\n"); - - const auto inverse_evaluator = Util::Overload { - [](LIB::Expression a) { return ExprBaseType{a.inverse()}; }, - [](LIB::PWMap a) { return ExprBaseType{a.inverse()}; }, - [](auto a) { - Util::ERROR("inverseEvaluator: wrong arguments ", a, " for inverse\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 1, "inverseEvaluator: wrong number of arguments\n"); + + const auto inverse_evaluator = + Util::Overload{[](LIB::Expression a) { return ExprBaseType{a.inverse()}; }, [](LIB::PWMap a) { return ExprBaseType{a.inverse()}; }, + [](auto a) { + Util::ERROR("inverseEvaluator: wrong arguments ", a, " for inverse\n"); + return ExprBaseType{}; + }}; return std::visit(inverse_evaluator, args[0]); } - + ExprBaseType BuiltInFunctions::imageEvaluator(const EBTList& args) { if (args.size() == 1) { - const auto image_evaluator = Util::Overload { - [](LIB::Map a) { return ExprBaseType{a.image()}; }, - [](LIB::PWMap a) { return ExprBaseType{a.image()}; }, - [](auto a) { - Util::ERROR("imageEvaluator: wrong argument ", a, " for image\n"); - return ExprBaseType{}; - } - }; + const auto image_evaluator = + Util::Overload{[](LIB::Map a) { return ExprBaseType{a.image()}; }, [](LIB::PWMap a) { return ExprBaseType{a.image()}; }, + [](auto a) { + Util::ERROR("imageEvaluator: wrong argument ", a, " for image\n"); + return ExprBaseType{}; + }}; return std::visit(image_evaluator, args[0]); - } - else if (args.size() == 2) { - const auto image2_evaluator = Util::Overload { - [](LIB::Set a, LIB::Map b) { return ExprBaseType{b.image(a)}; }, - [](LIB::Set a, LIB::PWMap b) { return ExprBaseType{b.image(a)}; }, - [](auto a, auto b) { - Util::ERROR("imageEvaluator: wrong arguments ", a, ", ", b - , " for image\n"); - return ExprBaseType{}; - } - }; + } else if (args.size() == 2) { + const auto image2_evaluator = Util::Overload{[](LIB::Set a, LIB::Map b) { return ExprBaseType{b.image(a)}; }, + [](LIB::Set a, LIB::PWMap b) { return ExprBaseType{b.image(a)}; }, + [](auto a, auto b) { + Util::ERROR("imageEvaluator: wrong arguments ", a, ", ", b, " for image\n"); + return ExprBaseType{}; + }}; return std::visit(image2_evaluator, args[0], args[1]); } @@ -472,214 +386,162 @@ ExprBaseType BuiltInFunctions::imageEvaluator(const EBTList& args) return ExprBaseType{}; } - + ExprBaseType BuiltInFunctions::preImageEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 2 - , "preImageEvaluator: wrong number of arguments\n"); - - const auto pre_image_evaluator = Util::Overload { - [](LIB::Set a, LIB::Map b) { return ExprBaseType{b.preImage(a)}; }, - [](LIB::Set a, LIB::PWMap b) { return ExprBaseType{b.preImage(a)}; }, - [](auto a, auto b) { - Util::ERROR("preImageEvaluator: wrong arguments ", a, ", ", b - , " for pre-image2\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 2, "preImageEvaluator: wrong number of arguments\n"); + + const auto pre_image_evaluator = Util::Overload{[](LIB::Set a, LIB::Map b) { return ExprBaseType{b.preImage(a)}; }, + [](LIB::Set a, LIB::PWMap b) { return ExprBaseType{b.preImage(a)}; }, + [](auto a, auto b) { + Util::ERROR("preImageEvaluator: wrong arguments ", a, ", ", b, " for pre-image2\n"); + return ExprBaseType{}; + }}; return std::visit(pre_image_evaluator, args[0], args[1]); } - + ExprBaseType BuiltInFunctions::domEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "domEvaluator: wrong number of arguments\n"); + Util::ERROR_UNLESS(args.size() == 1, "domEvaluator: wrong number of arguments\n"); - const auto dom_evaluator = Util::Overload { - [](LIB::PWMap a) { return ExprBaseType{a.domain()}; }, - [](auto a) { - Util::ERROR("domEvaluator: wrong arguments ", a, "for dom\n"); - return ExprBaseType{}; - } - }; + const auto dom_evaluator = Util::Overload{[](LIB::PWMap a) { return ExprBaseType{a.domain()}; }, + [](auto a) { + Util::ERROR("domEvaluator: wrong arguments ", a, "for dom\n"); + return ExprBaseType{}; + }}; return std::visit(dom_evaluator, args[0]); } - + ExprBaseType BuiltInFunctions::combineEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 2 - , "combineEvaluator: wrong number of arguments\n"); - - const auto combine_evaluator = Util::Overload { - [](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a.combine(b)}; }, - [](auto a, auto b) { - Util::ERROR("combineEvaluator: wrong arguments ", a, ", ", b - , " for combine\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 2, "combineEvaluator: wrong number of arguments\n"); + + const auto combine_evaluator = Util::Overload{[](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a.combine(b)}; }, + [](auto a, auto b) { + Util::ERROR("combineEvaluator: wrong arguments ", a, ", ", b, " for combine\n"); + return ExprBaseType{}; + }}; return std::visit(combine_evaluator, args[0], args[1]); } - + ExprBaseType BuiltInFunctions::minMapEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 2 - , "minMapEvaluator: wrong number of arguments\n"); - - const auto min_map_evaluator = Util::Overload { - [](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a.min(b)}; }, - [](auto a, auto b) { - Util::ERROR("minMapEvaluator: wrong arguments ", a, ", ", b - , " for minMap\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 2, "minMapEvaluator: wrong number of arguments\n"); + + const auto min_map_evaluator = Util::Overload{[](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a.min(b)}; }, + [](auto a, auto b) { + Util::ERROR("minMapEvaluator: wrong arguments ", a, ", ", b, " for minMap\n"); + return ExprBaseType{}; + }}; return std::visit(min_map_evaluator, args[0], args[1]); } ExprBaseType BuiltInFunctions::minAdjEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 2 - , "minAdjEvaluator: wrong number of arguments\n"); - - const auto min_adj_evaluator = Util::Overload { - [](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a.minAdj(b)}; }, - [](auto a, auto b) { - Util::ERROR("minAdjEvaluator: wrong arguments ", a, ", ", b - , " for minAdj\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 2, "minAdjEvaluator: wrong number of arguments\n"); + + const auto min_adj_evaluator = Util::Overload{[](LIB::PWMap a, LIB::PWMap b) { return ExprBaseType{a.minAdj(b)}; }, + [](auto a, auto b) { + Util::ERROR("minAdjEvaluator: wrong arguments ", a, ", ", b, " for minAdj\n"); + return ExprBaseType{}; + }}; return std::visit(min_adj_evaluator, args[0], args[1]); } - + ExprBaseType BuiltInFunctions::mapInfEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "mapInfEvaluator: wrong number of arguments\n"); + Util::ERROR_UNLESS(args.size() == 1, "mapInfEvaluator: wrong number of arguments\n"); - const auto inf_evaluator = Util::Overload { - [](LIB::PWMap a) { return ExprBaseType{a.mapInf()}; }, - [](auto a) { - Util::ERROR("mapInfEvaluator: wrong argument ", a, " for mapInf\n"); - return ExprBaseType{}; - } - }; + const auto inf_evaluator = Util::Overload{[](LIB::PWMap a) { return ExprBaseType{a.mapInf()}; }, + [](auto a) { + Util::ERROR("mapInfEvaluator: wrong argument ", a, " for mapInf\n"); + return ExprBaseType{}; + }}; return std::visit(inf_evaluator, args[0]); } ExprBaseType BuiltInFunctions::imageMultEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "imageMultEvaluator: wrong number of arguments\n"); - - const auto img_mult_evaluator = Util::Overload { - [](LIB::PWMap a) { return ExprBaseType{a.imageMultiplicity()}; }, - [](auto a) { - Util::ERROR("imageMultEvaluator: wrong argument ", a - , " for imageMultiplicity\n"); - return ExprBaseType{}; - } - }; + Util::ERROR_UNLESS(args.size() == 1, "imageMultEvaluator: wrong number of arguments\n"); + + const auto img_mult_evaluator = Util::Overload{[](LIB::PWMap a) { return ExprBaseType{a.imageMultiplicity()}; }, + [](auto a) { + Util::ERROR("imageMultEvaluator: wrong argument ", a, " for imageMultiplicity\n"); + return ExprBaseType{}; + }}; return std::visit(img_mult_evaluator, args[0]); } - + // Algorithms evaluators ------------------------------------------------------ ExprBaseType BuiltInFunctions::connectedEvaluator(const EBTList& args) -{ - Util::ERROR_UNLESS(args.size() == 1 - , "connectedEvaluator: wrong number of arguments\n"); - - const auto connected_evaluator = Util::Overload { - [](LIB::SBG a) { return ExprBaseType{connectedComponents(a)}; }, - [](auto a) { - Util::ERROR("connectedEvaluator: wrong argument ", a, " for CC\n"); - return ExprBaseType{}; - } - }; +{ + Util::ERROR_UNLESS(args.size() == 1, "connectedEvaluator: wrong number of arguments\n"); + + const auto connected_evaluator = Util::Overload{[](LIB::SBG a) { return ExprBaseType{connectedComponents(a)}; }, + [](auto a) { + Util::ERROR("connectedEvaluator: wrong argument ", a, " for CC\n"); + return ExprBaseType{}; + }}; return std::visit(connected_evaluator, args[0]); } ExprBaseType BuiltInFunctions::matchingEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 2 - , "matchingEvaluator: wrong number of arguments\n"); + Util::ERROR_UNLESS(args.size() == 2, "matchingEvaluator: wrong number of arguments\n"); LIB::Matching match_impl; - const auto matching_evaluator = Util::Overload { - [&match_impl](LIB::BipartiteSBG a, LIB::Int b) { - return ExprBaseType{match_impl.calculate(copy(b, a))}; - }, - [&match_impl](LIB::BipartiteSBG a, LIB::IntTuple b) { - return ExprBaseType{match_impl.calculate(copy(b[0], a))}; - }, - [](auto a, auto b) { - Util::ERROR("matchingEvaluator: wrong arguments ", a, ", ", b - , " for matching\n"); - return ExprBaseType{}; - } - }; + const auto matching_evaluator = + Util::Overload{[&match_impl](LIB::BipartiteSBG a, LIB::Int b) { return ExprBaseType{match_impl.calculate(copy(b, a))}; }, + [&match_impl](LIB::BipartiteSBG a, LIB::IntTuple b) { return ExprBaseType{match_impl.calculate(copy(b[0], a))}; }, + [](auto a, auto b) { + Util::ERROR("matchingEvaluator: wrong arguments ", a, ", ", b, " for matching\n"); + return ExprBaseType{}; + }}; return std::visit(matching_evaluator, args[0], args[1]); } ExprBaseType BuiltInFunctions::sccEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "sccEvaluator: wrong number of arguments\n"); + Util::ERROR_UNLESS(args.size() == 1, "sccEvaluator: wrong number of arguments\n"); LIB::SCC scc_impl; - const auto scc_evaluator = Util::Overload { - [&scc_impl](LIB::DirectedSBG a) { - return ExprBaseType{scc_impl.calculate(a).rmap()}; - }, - [](auto a) { - Util::ERROR("sccEvaluator: wrong argument ", a, " for scc\n"); - return ExprBaseType{}; - } - }; + const auto scc_evaluator = Util::Overload{[&scc_impl](LIB::DirectedSBG a) { return ExprBaseType{scc_impl.calculate(a).rmap()}; }, + [](auto a) { + Util::ERROR("sccEvaluator: wrong argument ", a, " for scc\n"); + return ExprBaseType{}; + }}; return std::visit(scc_evaluator, args[0]); } ExprBaseType BuiltInFunctions::mfvsEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "mfvsEvaluator: wrong number of arguments\n"); + Util::ERROR_UNLESS(args.size() == 1, "mfvsEvaluator: wrong number of arguments\n"); LIB::MinFeedbackVertexSet mfvs_impl; - const auto mfvs_evaluator = Util::Overload { - [&mfvs_impl](LIB::DirectedSBG a) { - return ExprBaseType{mfvs_impl.calculate(a)}; - }, - [](auto a) { - Util::ERROR("mfvsEvaluator: wrong type of argument ", a - , " for MFVS algorithm\n"); - return ExprBaseType{}; - } - }; + const auto mfvs_evaluator = Util::Overload{[&mfvs_impl](LIB::DirectedSBG a) { return ExprBaseType{mfvs_impl.calculate(a)}; }, + [](auto a) { + Util::ERROR("mfvsEvaluator: wrong type of argument ", a, " for MFVS algorithm\n"); + return ExprBaseType{}; + }}; return std::visit(mfvs_evaluator, args[0]); } ExprBaseType BuiltInFunctions::topoSortEvaluator(const EBTList& args) { - Util::ERROR_UNLESS(args.size() == 1 - , "topoSortEvaluator: wrong number of arguments\n"); + Util::ERROR_UNLESS(args.size() == 1, "topoSortEvaluator: wrong number of arguments\n"); LIB::TopologicalSorting ts_impl; - const auto ts_evaluator = Util::Overload { - [&ts_impl](LIB::DirectedSBG a) { - return ExprBaseType{ts_impl.calculate(a, SBG::LIB::PWMap{})}; - }, - [](auto a) { - Util::ERROR("topoSortEvaluator: wrong argument ", a, " for sort\n"); - return ExprBaseType{}; - } - }; + const auto ts_evaluator = Util::Overload{[&ts_impl](LIB::DirectedSBG a) { return ExprBaseType{ts_impl.calculate(a, SBG::LIB::PWMap{})}; }, + [](auto a) { + Util::ERROR("topoSortEvaluator: wrong argument ", a, " for sort\n"); + return ExprBaseType{}; + }}; return std::visit(ts_evaluator, args[0]); } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/visitors/int_evaluator.cpp b/eval/visitors/int_evaluator.cpp old mode 100755 new mode 100644 index 54790a54..d46c64df --- a/eval/visitors/int_evaluator.cpp +++ b/eval/visitors/int_evaluator.cpp @@ -32,22 +32,22 @@ IntEvaluator::IntEvaluator() : _venv() {} IntEvaluator::IntEvaluator(VarEnv &venv) : _venv(venv) {} -LIB::Int IntEvaluator::operator()(AST::Integer v) const { return (LIB::Int) v; } +LIB::Int IntEvaluator::operator()(AST::Integer v) const { return (LIB::Int)v; } -LIB::Int IntEvaluator::operator()(AST::Rational v) const -{ +LIB::Int IntEvaluator::operator()(AST::Rational v) const +{ if (boost::apply_visitor(*this, v.denominator()) == 1) { return boost::apply_visitor(*this, v.numerator()); } Util::ERROR("IntEvaluator: trying to evaluate Rational ", v, "\n"); - return 0; + return 0; } -LIB::Int IntEvaluator::operator()(AST::Name v) const -{ +LIB::Int IntEvaluator::operator()(AST::Name v) const +{ auto var_definition = _venv.find(v); - if (var_definition != _venv.end()) { + if (var_definition != _venv.end()) { ExprBaseType value = var_definition->second; if (std::holds_alternative(value)) { return std::get(value); @@ -64,46 +64,46 @@ LIB::Int IntEvaluator::operator()(AST::Name v) const return 0; } -LIB::Int IntEvaluator::operator()(AST::UnaryOp v) const +LIB::Int IntEvaluator::operator()(AST::UnaryOp v) const { LIB::Int x = boost::apply_visitor(*this, v.expr()); switch (v.op()) { - case AST::UnOp::oppo: { - return -x; - } + case AST::UnOp::oppo: { + return -x; + } - default: { - Util::ERROR("IntEvaluator: BinOp ", v.op(), " unsupported\n"); - return 0; - } + default: { + Util::ERROR("IntEvaluator: BinOp ", v.op(), " unsupported\n"); + return 0; + } } } -LIB::Int IntEvaluator::operator()(AST::BinOp v) const +LIB::Int IntEvaluator::operator()(AST::BinOp v) const { LIB::Int l = boost::apply_visitor(*this, v.left()); LIB::Int r = boost::apply_visitor(*this, v.right()); switch (v.op()) { - case AST::Op::add: { - return l + r; - } + case AST::Op::add: { + return l + r; + } - case AST::Op::sub: { - return l - r; - } + case AST::Op::sub: { + return l - r; + } - case AST::Op::mult: { - return l * r; - } + case AST::Op::mult: { + return l * r; + } - case AST::Op::expo: { - return pow(l, r); - } + case AST::Op::expo: { + return pow(l, r); + } - default: { - Util::ERROR("IntEvaluator: BinOp ", v.op(), " unsupported\n"); - return 0; - } + default: { + Util::ERROR("IntEvaluator: BinOp ", v.op(), " unsupported\n"); + return 0; + } } } @@ -173,13 +173,10 @@ LIB::Int IntEvaluator::operator()(AST::DSBG v) const return 0; } -LIB::Int IntEvaluator::operator()(AST::ParenExpr v) const -{ - return boost::apply_visitor(*this, v.pexpr()); -} +LIB::Int IntEvaluator::operator()(AST::ParenExpr v) const { return boost::apply_visitor(*this, v.pexpr()); } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/visitors/linear_expr_evaluator.cpp b/eval/visitors/linear_expr_evaluator.cpp old mode 100755 new mode 100644 index f4408d80..224fcb65 --- a/eval/visitors/linear_expr_evaluator.cpp +++ b/eval/visitors/linear_expr_evaluator.cpp @@ -30,10 +30,7 @@ namespace detail { LinearExprEvaluator::LinearExprEvaluator(VarEnv &venv) : _venv(venv) {} -LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::Integer v) const -{ - return LIB::detail::LinearExpr{0, LIB::Rational{v}}; -} +LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::Integer v) const { return LIB::detail::LinearExpr{0, LIB::Rational{v}}; } LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::Rational v) const { @@ -49,8 +46,7 @@ LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::Name v) const return LIB::detail::LinearExpr{1, 0}; } - LIB::Rational off = boost::apply_visitor(RationalEvaluator{_venv} - , AST::Expr{v}); + LIB::Rational off = boost::apply_visitor(RationalEvaluator{_venv}, AST::Expr{v}); return LIB::detail::LinearExpr{0, off}; } @@ -59,56 +55,50 @@ LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::UnaryOp v) const LIB::detail::LinearExpr le = boost::apply_visitor(*this, v.expr()); switch (v.op()) { - case AST::UnOp::oppo: { - return LIB::detail::LinearExpr{-le.slope(), -le.offset()}; - } + case AST::UnOp::oppo: { + return LIB::detail::LinearExpr{-le.slope(), -le.offset()}; + } - default: { - Util::ERROR("LinearExprEvaluator: UnaryOp ", v.op() - , " is not arithmetic\n"); - break; - } + default: { + Util::ERROR("LinearExprEvaluator: UnaryOp ", v.op(), " is not arithmetic\n"); + break; + } }; return LIB::detail::LinearExpr{}; } -LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::BinOp v) const +LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::BinOp v) const { LIB::detail::LinearExpr vl = boost::apply_visitor(*this, v.left()); LIB::detail::LinearExpr vr = boost::apply_visitor(*this, v.right()); switch (v.op()) { - case AST::Op::add: { - return LIB::detail::LinearExpr{vl.slope() + vr.slope() - , vl.offset() + vr.offset()}; - } + case AST::Op::add: { + return LIB::detail::LinearExpr{vl.slope() + vr.slope(), vl.offset() + vr.offset()}; + } - case AST::Op::sub: { - return LIB::detail::LinearExpr{vl.slope() - vr.slope() - , vl.offset() - vr.offset()}; - } + case AST::Op::sub: { + return LIB::detail::LinearExpr{vl.slope() - vr.slope(), vl.offset() - vr.offset()}; + } - case AST::Op::mult: { - if (vl.slope() == 0 && vr.slope() == 0) { - return LIB::detail::LinearExpr{0, vr.offset()*vl.offset()}; - } else if (vl.slope() == 0) { - return LIB::detail::LinearExpr{vl.offset()*vr.slope() - , vl.offset()*vr.offset()}; - } else if (vr.slope() == 0) { - return LIB::detail::LinearExpr{vl.slope()*vr.offset() - , vl.offset()*vr.offset()}; - } - - Util::ERROR("LinearExprEvaluator: expression ", v, " is not linear\n"); - break; + case AST::Op::mult: { + if (vl.slope() == 0 && vr.slope() == 0) { + return LIB::detail::LinearExpr{0, vr.offset() * vl.offset()}; + } else if (vl.slope() == 0) { + return LIB::detail::LinearExpr{vl.offset() * vr.slope(), vl.offset() * vr.offset()}; + } else if (vr.slope() == 0) { + return LIB::detail::LinearExpr{vl.slope() * vr.offset(), vl.offset() * vr.offset()}; } - default: { - Util::ERROR("LinearExprEvaluator: BinOp ", v.op() - , " is not arithmetic\n"); - break; - } + Util::ERROR("LinearExprEvaluator: expression ", v, " is not linear\n"); + break; + } + + default: { + Util::ERROR("LinearExprEvaluator: BinOp ", v.op(), " is not arithmetic\n"); + break; + } }; return LIB::detail::LinearExpr{}; @@ -117,84 +107,76 @@ LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::BinOp v) const LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::Call v) const { Util::ERROR("LinearExprEvaluator: trying to evaluate Call ", v, "\n"); - return LIB::detail::LinearExpr{}; + return LIB::detail::LinearExpr{}; } LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::Interval v) const -{ +{ Util::ERROR("LinearExprEvaluator: trying to evaluate Interval ", v, "\n"); - return LIB::detail::LinearExpr{}; + return LIB::detail::LinearExpr{}; } -LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::MultiDimInter v) - const -{ - Util::ERROR("LinearExprEvaluator: trying to evaluate MultiDimInter ", v - , "\n"); - return LIB::detail::LinearExpr{}; +LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::MultiDimInter v) const +{ + Util::ERROR("LinearExprEvaluator: trying to evaluate MultiDimInter ", v, "\n"); + return LIB::detail::LinearExpr{}; } -LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::Set v) const +LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::Set v) const { Util::ERROR("LinearExprEvaluator: trying to evaluate Set ", v, "\n"); - return LIB::detail::LinearExpr{}; + return LIB::detail::LinearExpr{}; } LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::LinearExpr v) const -{ +{ RationalEvaluator visit_rat{_venv}; AST::Expr m = v.slope(); AST::Expr h = v.offset(); - return LIB::detail::LinearExpr{boost::apply_visitor(visit_rat, m) - , boost::apply_visitor(visit_rat, h)}; + return LIB::detail::LinearExpr{boost::apply_visitor(visit_rat, m), boost::apply_visitor(visit_rat, h)}; } -LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::MDLExp v) - const +LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::MDLExp v) const { Util::ERROR("LinearExprEvaluator: trying to evaluate MDLExp ", v, "\n"); - return LIB::detail::LinearExpr{}; + return LIB::detail::LinearExpr{}; } -LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::LinearMap v) const +LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::LinearMap v) const { Util::ERROR("LinearExprEvaluator: trying to evaluate LinearMap ", v, "\n"); - return LIB::detail::LinearExpr{}; + return LIB::detail::LinearExpr{}; } -LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::PWLMap v) const +LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::PWLMap v) const { Util::ERROR("LinearExprEvaluator: trying to evaluate PWLMap ", v, "\n"); - return LIB::detail::LinearExpr{}; + return LIB::detail::LinearExpr{}; } -LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::SBG v) const +LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::SBG v) const { Util::ERROR("LinearExprEvaluator: trying to evaluate SBG ", v, "\n"); - return LIB::detail::LinearExpr{}; + return LIB::detail::LinearExpr{}; } -LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::BipartiteSBG v) - const +LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::BipartiteSBG v) const { Util::ERROR("LinearExprEvaluator: trying to evaluate BipartiteSBG ", v, "\n"); - return LIB::detail::LinearExpr{}; + return LIB::detail::LinearExpr{}; } -LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::DSBG v) const +LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::DSBG v) const { Util::ERROR("LinearExprEvaluator: trying to evaluate DirectedSBG ", v, "\n"); - return LIB::detail::LinearExpr{}; + return LIB::detail::LinearExpr{}; } -LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::ParenExpr v) const -{ - return boost::apply_visitor(*this, v.pexpr()); -} +LIB::detail::LinearExpr LinearExprEvaluator::operator()(AST::ParenExpr v) const { return boost::apply_visitor(*this, v.pexpr()); } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/visitors/program_evaluator.cpp b/eval/visitors/program_evaluator.cpp old mode 100755 new mode 100644 index 397099eb..222be822 --- a/eval/visitors/program_evaluator.cpp +++ b/eval/visitors/program_evaluator.cpp @@ -30,8 +30,8 @@ namespace detail { ProgramEvaluator::ProgramEvaluator() {} -ProgramIO ProgramEvaluator::evaluate(AST::SBGProgram p) const -{ +ProgramIO ProgramEvaluator::evaluate(AST::SBGProgram p) const +{ std::size_t arity = 1; EvalContext eval_context; @@ -62,8 +62,8 @@ ProgramIO ProgramEvaluator::evaluate(AST::SBGProgram p) const return ProgramIO{arity, stms, exprs}; } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/visitors/rational_evaluator.cpp b/eval/visitors/rational_evaluator.cpp old mode 100755 new mode 100644 index b5fd1e03..62ab0c64 --- a/eval/visitors/rational_evaluator.cpp +++ b/eval/visitors/rational_evaluator.cpp @@ -31,22 +31,18 @@ RationalEvaluator::RationalEvaluator() : _venv() {} RationalEvaluator::RationalEvaluator(VarEnv& venv) : _venv(venv) {} -LIB::Rational RationalEvaluator::operator()(AST::Integer v) const -{ - return LIB::Rational{v}; -} +LIB::Rational RationalEvaluator::operator()(AST::Integer v) const { return LIB::Rational{v}; } LIB::Rational RationalEvaluator::operator()(AST::Rational v) const { IntEvaluator visit_int{_venv}; - return LIB::Rational(boost::apply_visitor(visit_int, v.numerator()) - , boost::apply_visitor(visit_int, v.denominator())); + return LIB::Rational(boost::apply_visitor(visit_int, v.numerator()), boost::apply_visitor(visit_int, v.denominator())); } -LIB::Rational RationalEvaluator::operator()(AST::Name v) const +LIB::Rational RationalEvaluator::operator()(AST::Name v) const { auto var_definition = _venv.find(v); - if (var_definition != _venv.end()) { + if (var_definition != _venv.end()) { ExprBaseType value = var_definition->second; if (std::holds_alternative(value)) { return std::get(value); @@ -67,46 +63,46 @@ LIB::Rational RationalEvaluator::operator()(AST::Name v) const return 0; } -LIB::Rational RationalEvaluator::operator()(AST::UnaryOp v) const -{ +LIB::Rational RationalEvaluator::operator()(AST::UnaryOp v) const +{ RationalEvaluator visit_rat{_venv}; LIB::Rational result = boost::apply_visitor(visit_rat, v.expr()); switch (v.op()) { - case AST::UnOp::oppo: { - return -result; - } + case AST::UnOp::oppo: { + return -result; + } - default: { - Util::ERROR("RationalEvaluator: UnaryOp ", v.op(), " unsupported\n"); - return 0; - } + default: { + Util::ERROR("RationalEvaluator: UnaryOp ", v.op(), " unsupported\n"); + return 0; + } } Util::ERROR("RationalEvaluator: UnaryOp ", v.op(), " unsupported\n"); return 0; } -LIB::Rational RationalEvaluator::operator()(AST::BinOp v) const +LIB::Rational RationalEvaluator::operator()(AST::BinOp v) const { LIB::Rational l = boost::apply_visitor(*this, v.left()); LIB::Rational r = boost::apply_visitor(*this, v.right()); switch (v.op()) { - case AST::Op::add: { - return l + r; - } + case AST::Op::add: { + return l + r; + } - case AST::Op::sub: { - return l - r; - } + case AST::Op::sub: { + return l - r; + } - case AST::Op::mult: { - return l * r; - } + case AST::Op::mult: { + return l * r; + } - default: { - Util::ERROR("RationalEvaluator: BinOp ", v.op(), " unsupported\n"); - return 0; - } + default: { + Util::ERROR("RationalEvaluator: BinOp ", v.op(), " unsupported\n"); + return 0; + } } Util::ERROR("RationalEvaluator: BinOp ", v.op(), " unsupported\n"); @@ -179,13 +175,10 @@ LIB::Rational RationalEvaluator::operator()(AST::DSBG v) const return 0; } -LIB::Rational RationalEvaluator::operator()(AST::ParenExpr v) const -{ - return boost::apply_visitor(*this, v.pexpr()); -} +LIB::Rational RationalEvaluator::operator()(AST::ParenExpr v) const { return boost::apply_visitor(*this, v.pexpr()); } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/visitors/set_impl_visitor.cpp b/eval/visitors/set_impl_visitor.cpp old mode 100755 new mode 100644 index f7e9a73d..3471a87c --- a/eval/visitors/set_impl_visitor.cpp +++ b/eval/visitors/set_impl_visitor.cpp @@ -40,12 +40,9 @@ int SetImplExprVisitor::operator()(AST::Rational v) const { return 2; } int SetImplExprVisitor::operator()(AST::Name v) const { return 2; } -int SetImplExprVisitor::operator()(AST::UnaryOp v) const -{ - return boost::apply_visitor(*this, v.expr()); -} +int SetImplExprVisitor::operator()(AST::UnaryOp v) const { return boost::apply_visitor(*this, v.expr()); } -int SetImplExprVisitor::operator()(AST::BinOp v) const +int SetImplExprVisitor::operator()(AST::BinOp v) const { int limpl = boost::apply_visitor(*this, v.left()); int rimpl = boost::apply_visitor(*this, v.right()); @@ -127,7 +124,7 @@ int SetImplExprVisitor::operator()(AST::PWLMap v) const impl = std::min(impl, boost::apply_visitor(*this, e)); } - return impl; + return impl; } int SetImplExprVisitor::operator()(AST::SBG v) const @@ -171,13 +168,10 @@ int SetImplExprVisitor::operator()(AST::DSBG v) const return impl; } -int SetImplExprVisitor::operator()(AST::ParenExpr v) const -{ - return boost::apply_visitor(*this, v.pexpr()); -} +int SetImplExprVisitor::operator()(AST::ParenExpr v) const { return boost::apply_visitor(*this, v.pexpr()); } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/eval/visitors/stm_evaluator.cpp b/eval/visitors/stm_evaluator.cpp old mode 100755 new mode 100644 index 269d4345..ea4d7c35 --- a/eval/visitors/stm_evaluator.cpp +++ b/eval/visitors/stm_evaluator.cpp @@ -28,12 +28,9 @@ namespace detail { StmEvaluator::StmEvaluator(EvalContext& eval_ctx) : _eval_context(eval_ctx) {} -EvalContext& StmEvaluator::eval_context() const -{ - return _eval_context; -} +EvalContext& StmEvaluator::eval_context() const { return _eval_context; } -StmResult StmEvaluator::operator()(AST::Assign assgn) const +StmResult StmEvaluator::operator()(AST::Assign assgn) const { ExprEvaluator eval_expr(_eval_context); ExprBaseType e = boost::apply_visitor(eval_expr, assgn.right()); @@ -42,13 +39,10 @@ StmResult StmEvaluator::operator()(AST::Assign assgn) const return StmResult(assgn.left(), e); } -StmResult StmEvaluator::operator()(AST::ConfigDims cfg) const -{ - return StmResult("", static_cast(cfg.arity())); -} +StmResult StmEvaluator::operator()(AST::ConfigDims cfg) const { return StmResult("", static_cast(cfg.arity())); } -} // namespace detail +} // namespace detail -} // namespace Eval +} // namespace Eval -} // namespace SBG +} // namespace SBG diff --git a/parser/main.cpp b/parser/main.cpp old mode 100755 new mode 100644 index 8e04e861..7e011c2f --- a/parser/main.cpp +++ b/parser/main.cpp @@ -21,12 +21,12 @@ #include -int main(int argc, char** argv) +int main(int argc, char** argv) { std::cout << std::boolalpha; SBG::Parser::ParserExecutor parser_exec; - parser_exec.execute(argc, argv); + parser_exec.execute(argc, argv); return 0; } diff --git a/parser/parser_exec.cpp b/parser/parser_exec.cpp index f6cfb58e..60ee7774 100644 --- a/parser/parser_exec.cpp +++ b/parser/parser_exec.cpp @@ -48,16 +48,15 @@ void ParserExecutor::execute(int arg_count, char* args[]) // Command line options handling --------------------------------------------- Util::prog_opts::variables_map vm; - store(Util::prog_opts::command_line_parser(arg_count, args) - .options(_cmd_line_opts).positional(_positional).run(), vm); + store(Util::prog_opts::command_line_parser(arg_count, args).options(_cmd_line_opts).positional(_positional).run(), vm); notify(vm); // Help handling ------------------------------------------------------------- - + if (vm.count("help")) { std::cout << "Usage: filename [options]\n"; std::cout << "Command line options are prioritized over configuration file" - " options."; + " options."; std::cout << _visible << "\n"; return; } @@ -70,8 +69,8 @@ void ParserExecutor::execute(int arg_count, char* args[]) } // Optional configuration file handling -------------------------------------- - - if (_config_file) { + + if (_config_file) { std::ifstream config_fs((*_config_file).c_str()); if (config_fs) { store(parse_config_file(config_fs, _cfg_file_opts), vm); @@ -83,13 +82,12 @@ void ParserExecutor::execute(int arg_count, char* args[]) if (_input_file) { parseFile(*_input_file); - } - else { + } else { std::cout << "Usage: filename [options]"; Util::ERROR("ParserExecutor: must provide a SBG program filename\n"); } } -} // namespace Parser +} // namespace Parser -} // namespace SBG +} // namespace SBG diff --git a/sbg/bipartite_sbg.cpp b/sbg/bipartite_sbg.cpp index 631d28d9..19a26dca 100644 --- a/sbg/bipartite_sbg.cpp +++ b/sbg/bipartite_sbg.cpp @@ -31,17 +31,13 @@ namespace LIB { // Constructors/Destructors ---------------------------------------------------- -BipartiteSBG::BipartiteSBG() - : _V(), _Vmap() - , _E(), _map1() - , _map2(), _Emap() - , _X(), _Y() {} - -BipartiteSBG::BipartiteSBG(const Set& V, const PWMap& Vmap - , const PWMap& map1, const PWMap& map2, const PWMap& Emap - , const Set& X, const Set& Y) - : _V(V), _Vmap(Vmap), _E(map1.domain().intersection(map2.domain())) - , _map1(map1), _map2(map2), _Emap(Emap), _X(X), _Y(Y) {} +BipartiteSBG::BipartiteSBG() : _V(), _Vmap(), _E(), _map1(), _map2(), _Emap(), _X(), _Y() {} + +BipartiteSBG::BipartiteSBG(const Set& V, const PWMap& Vmap, const PWMap& map1, const PWMap& map2, const PWMap& Emap, const Set& X, + const Set& Y) + : _V(V), _Vmap(Vmap), _E(map1.domain().intersection(map2.domain())), _map1(map1), _map2(map2), _Emap(Emap), _X(X), _Y(Y) +{ +} // Getters --------------------------------------------------------------------- @@ -67,13 +63,11 @@ void BipartiteSBG::addSetVertex(const Set& X, const Set& Y) { Set vertices = X.cup(Y); if (!vertices.intersection(_V).isEmpty()) { - Util::ERROR("BipartiteSBG::addSetVertex: trying to add existing vertices: " - , vertices, " to SBG\n"); + Util::ERROR("BipartiteSBG::addSetVertex: trying to add existing vertices: ", vertices, " to SBG\n"); } else if (!vertices.isEmpty()) { Set set_vertices = _Vmap.image(); std::size_t arity = vertices.arity(); - IntTuple max = set_vertices.isEmpty() ? IntTuple{arity, 0} - : set_vertices.maxElem(); + IntTuple max = set_vertices.isEmpty() ? IntTuple{arity, 0} : set_vertices.maxElem(); _Vmap.emplace(vertices, max + IntTuple{arity, 1}); _X = std::move(_X).cup(X); _Y = std::move(_Y).cup(Y); @@ -86,23 +80,20 @@ void BipartiteSBG::addSetEdge(const PWMap& pw1, const PWMap& pw2) Set edges1 = pw1.domain(); Set edges2 = pw2.domain(); if (edges1 != edges2) { - Util::ERROR("BipartiteSBG::addSetEdge: ", edges1, " is different from " - , edges2, "\n"); + Util::ERROR("BipartiteSBG::addSetEdge: ", edges1, " is different from ", edges2, "\n"); } else if (edges1.intersection(_E).isEmpty()) { Set edges = edges1; if (!edges.isEmpty()) { Set set_edges = _Emap.image(); std::size_t arity = edges.arity(); - IntTuple max = set_edges.isEmpty() ? IntTuple{arity, 0} - : set_edges.maxElem(); + IntTuple max = set_edges.isEmpty() ? IntTuple{arity, 0} : set_edges.maxElem(); _map1 = std::move(_map1).concatenation(pw1); _map2 = std::move(_map2).concatenation(pw2); _Emap.emplace(edges, max + IntTuple{arity, 1}); _E = std::move(_E).cup(std::move(edges)); } } else { - Util::ERROR("BipartiteSBG::addSetEdge: trying to add existing edges: " - , edges1, " to SBG\n"); + Util::ERROR("BipartiteSBG::addSetEdge: trying to add existing edges: ", edges1, " to SBG\n"); } } @@ -152,16 +143,14 @@ BipartiteSBG copy(unsigned int copies, BipartiteSBG sbg) Expression offset_v; for (std::size_t k = 0; k < max_v.arity(); ++k) { - offset_v = offset_v.cartesianProduct(Expression{Rational{1} - , Rational{max_v[k]}}); + offset_v = offset_v.cartesianProduct(Expression{Rational{1}, Rational{max_v[k]}}); } PWMap offset_pw_v{Map{V, offset_v}}; IntTuple max_e = result.E().maxElem(); Expression offset_e; for (std::size_t k = 0; k < max_e.arity(); ++k) { - offset_e = offset_e.cartesianProduct(Expression{Rational{1} - , Rational{max_e[k]}}); + offset_e = offset_e.cartesianProduct(Expression{Rational{1}, Rational{max_e[k]}}); } PWMap offset_pw_e{Map{E, offset_e}}; PWMap inverse_offset_pw_e = offset_pw_e.inverse(); @@ -180,6 +169,6 @@ BipartiteSBG copy(unsigned int copies, BipartiteSBG sbg) return result; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/directed_sbg.cpp b/sbg/directed_sbg.cpp index e9886724..eeffa8d2 100644 --- a/sbg/directed_sbg.cpp +++ b/sbg/directed_sbg.cpp @@ -37,12 +37,10 @@ namespace LIB { DirectedSBG::DirectedSBG() : _V(), _Vmap(), _E(), _mapB(), _mapD(), _Emap() {} -DirectedSBG::DirectedSBG(const Set& V, const PWMap& Vmap - , const PWMap& mapB, const PWMap& mapD - , const PWMap& Emap) - : _V(V), _Vmap(Vmap) - , _E(mapB.domain().intersection(mapD.domain())) - , _mapB(mapB), _mapD(mapD), _Emap(Emap) {} +DirectedSBG::DirectedSBG(const Set& V, const PWMap& Vmap, const PWMap& mapB, const PWMap& mapD, const PWMap& Emap) + : _V(V), _Vmap(Vmap), _E(mapB.domain().intersection(mapD.domain())), _mapB(mapB), _mapD(mapD), _Emap(Emap) +{ +} // Getters --------------------------------------------------------------------- @@ -62,14 +60,12 @@ const PWMap& DirectedSBG::Emap() const { return _Emap; } void DirectedSBG::addSetVertex(const Set& vertices) { - if (!vertices.intersection(_V).isEmpty()){ - Util::ERROR("Trying to add existing vertices: ", vertices - , " to DirectedSBG\n"); + if (!vertices.intersection(_V).isEmpty()) { + Util::ERROR("Trying to add existing vertices: ", vertices, " to DirectedSBG\n"); } else if (!vertices.isEmpty()) { Set set_vertices = _Vmap.image(); std::size_t arity = vertices.arity(); - IntTuple max = set_vertices.isEmpty() ? IntTuple{arity, 0} - : set_vertices.maxElem(); + IntTuple max = set_vertices.isEmpty() ? IntTuple{arity, 0} : set_vertices.maxElem(); _Vmap.emplace(vertices, max + IntTuple{arity, 1}); _V = std::move(_V).cup(std::move(vertices)); } @@ -136,7 +132,6 @@ std::ostream& operator<<(std::ostream& out, const DirectedSBG& dg) DirectedSBG copy(unsigned int copies, DirectedSBG dsbg) { - if (copies == 0) { Util::ERROR("DirectedSBG::copy: zeros copies is not allowed\n"); } @@ -153,21 +148,19 @@ DirectedSBG copy(unsigned int copies, DirectedSBG dsbg) IntTuple max_v = result.V().maxElem(); dsbg.foreachSetVertex([&](const IntTuple& SV) { Set vertices = Vmap.preImage(Set{SV}); - result.addSetVertex(vertices.translate(max_v)); + result.addSetVertex(vertices.translate(max_v)); }); Expression offset_v; for (std::size_t k = 0; k < max_v.arity(); ++k) { - offset_v = offset_v.cartesianProduct(Expression{Rational{1} - , Rational{max_v[k]}}); + offset_v = offset_v.cartesianProduct(Expression{Rational{1}, Rational{max_v[k]}}); } PWMap offset_pw_v{Map{V, offset_v}}; IntTuple max_e = result.E().maxElem(); Expression offset_e; for (std::size_t k = 0; k < max_e.arity(); ++k) { - offset_e = offset_e.cartesianProduct(Expression{Rational{1} - , Rational{max_e[k]}}); + offset_e = offset_e.cartesianProduct(Expression{Rational{1}, Rational{max_e[k]}}); } PWMap offset_pw_e{Map{E, offset_e}}; PWMap inverse_offset_pw_e = offset_pw_e.inverse(); @@ -186,6 +179,6 @@ DirectedSBG copy(unsigned int copies, DirectedSBG dsbg) return result; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/dom_ord_pwmap.cpp b/sbg/dom_ord_pwmap.cpp index 171bf634..6264c463 100644 --- a/sbg/dom_ord_pwmap.cpp +++ b/sbg/dom_ord_pwmap.cpp @@ -45,39 +45,38 @@ namespace detail { // Auxiliary definitions ------------------------------------------------------- class MapLess { -public: - bool operator()(const Map& a, const Map& b) const { - return a.domain().minElem() < b.domain().minElem(); - } + public: + bool operator()(const Map& a, const Map& b) const { return a.domain().minElem() < b.domain().minElem(); } }; // Constructors/Destructors ---------------------------------------------------- DomOrdPWMap::DomOrdPWMap() : _pieces() {} -DomOrdPWMap::DomOrdPWMap(const Set& s) : _pieces() { +DomOrdPWMap::DomOrdPWMap(const Set& s) : _pieces() +{ if (!s.isEmpty()) { _pieces.emplace_back(Map{s, Expression{s.arity(), 1, 0}}); } } -DomOrdPWMap::DomOrdPWMap(const Map& m) : _pieces() { +DomOrdPWMap::DomOrdPWMap(const Map& m) : _pieces() +{ if (!m.domain().isEmpty()) { _pieces.emplace_back(m); } } -DomOrdPWMap::DomOrdPWMap(const std::vector& pieces) : _pieces() { +DomOrdPWMap::DomOrdPWMap(const std::vector& pieces) : _pieces() +{ for (const Map& m : pieces) { insert(m); } } -DomOrdPWMap::DomOrdPWMap(const DomOrdPWMap::OrdMapCollection& pieces) - : _pieces(pieces) {} +DomOrdPWMap::DomOrdPWMap(const DomOrdPWMap::OrdMapCollection& pieces) : _pieces(pieces) {} -DomOrdPWMap::DomOrdPWMap(DomOrdPWMap::OrdMapCollection&& pieces) - : _pieces(std::move(pieces)) {} +DomOrdPWMap::DomOrdPWMap(DomOrdPWMap::OrdMapCollection&& pieces) : _pieces(std::move(pieces)) {} // Getters --------------------------------------------------------------------- @@ -87,16 +86,13 @@ DomOrdPWMap::ConstIt DomOrdPWMap::end() const { return _pieces.end(); } // Setters --------------------------------------------------------------------- -void DomOrdPWMap::insert(const Map& m) -{ - insert(Map{m}); -} +void DomOrdPWMap::insert(const Map& m) { insert(Map{m}); } void DomOrdPWMap::insert(Map&& m) { if (!m.isEmpty()) { MapEntry entry{m}; - if (isEmpty() || _pieces.back() < entry) { + if (isEmpty() || _pieces.back() < entry) { _pieces.push_back(entry); } else { insertHint(0, std::move(m)); @@ -104,10 +100,7 @@ void DomOrdPWMap::insert(Map&& m) } } -void DomOrdPWMap::pushBack(const Map& m) -{ - pushBack(Map{m}); -} +void DomOrdPWMap::pushBack(const Map& m) { pushBack(Map{m}); } void DomOrdPWMap::pushBack(Map&& m) { @@ -116,10 +109,7 @@ void DomOrdPWMap::pushBack(Map&& m) } } -void DomOrdPWMap::pushBack(const MapEntry& entry) -{ - pushBack(MapEntry{entry}); -} +void DomOrdPWMap::pushBack(const MapEntry& entry) { pushBack(MapEntry{entry}); } void DomOrdPWMap::pushBack(MapEntry&& entry) { @@ -128,10 +118,7 @@ void DomOrdPWMap::pushBack(MapEntry&& entry) } } -void DomOrdPWMap::insertHint(std::size_t hint, const Map& m) -{ - insertHint(hint, Map{m}); -} +void DomOrdPWMap::insertHint(std::size_t hint, const Map& m) { insertHint(hint, Map{m}); } void DomOrdPWMap::insertHint(std::size_t hint, Map&& m) { @@ -170,9 +157,8 @@ std::size_t DomOrdPWMap::advanceHint(std::size_t hint, const MapEntry& jth_entry // Traverse -------------------------------------------------------------------- -template -Core traverse(const OrdCollection1& lhs, const OrdCollection2& rhs - , Core core_op) +template +Core traverse(const OrdCollection1& lhs, const OrdCollection2& rhs, Core core_op) { OrdCollection1 short_collection = lhs; OrdCollection2 long_collection = rhs; @@ -187,16 +173,16 @@ Core traverse(const OrdCollection1& lhs, const OrdCollection2& rhs auto short_begin = short_collection.begin(); for (const auto& long_elem : long_collection) { const Perimeter& long_perimeter = long_elem.perimeter(); - + auto prev_index = indexes.before_begin(); auto curr_index = indexes.begin(); while (curr_index != indexes.end()) { size_t idx = *curr_index; const auto& short_elem = *(short_begin + idx); - const Perimeter& short_perimeter = short_elem.perimeter(); - + const Perimeter& short_perimeter = short_elem.perimeter(); + // Here short_elem is "before" long_elem, so it is also "before" all the - // remaining elements in long_collection, thus it can be discarded. + // remaining elements in long_collection, thus it can be discarded. if (short_perimeter.max() < long_perimeter.min()) { curr_index = indexes.erase_after(prev_index); continue; @@ -204,7 +190,7 @@ Core traverse(const OrdCollection1& lhs, const OrdCollection2& rhs // Here short_elem is "after" long_elem, so no comparison is needed, and // the loop of long_collection continues to check if this short_map interacts - // with the following elements of long_collection. + // with the following elements of long_collection. if (long_perimeter.max() < short_perimeter.min()) { break; } @@ -230,8 +216,9 @@ Core traverse(const OrdCollection1& lhs, const OrdCollection2& rhs // Operators ------------------------------------------------------------------- class EqualCore { -public: - bool operator()(const MapEntry& entry1, const MapEntry& entry2) { + public: + bool operator()(const MapEntry& entry1, const MapEntry& entry2) + { Map m1 = entry1.map(); Map m2 = entry2.map(); Set cap_domain = m1.domain().intersection(m2.domain()); @@ -259,11 +246,11 @@ class EqualCore { bool result() const { return _are_equal; } -private: + private: bool _are_equal = true; }; -bool DomOrdPWMap::operator==(const DomOrdPWMap& other) const +bool DomOrdPWMap::operator==(const DomOrdPWMap& other) const { if (domain() != other.domain()) { return false; @@ -276,14 +263,12 @@ bool DomOrdPWMap::operator==(const DomOrdPWMap& other) const return traverse(_pieces, other._pieces, EqualCore{}).result(); } -bool DomOrdPWMap::operator!=(const DomOrdPWMap& other) const -{ - return !(*this == other); -} +bool DomOrdPWMap::operator!=(const DomOrdPWMap& other) const { return !(*this == other); } class AddCore { -public: - bool operator()(const MapEntry& entry1, const MapEntry& entry2) { + public: + bool operator()(const MapEntry& entry1, const MapEntry& entry2) + { Map added = entry1.map() + entry2.map(); _global_pos = _result.advanceHint(_global_pos, entry2); _result.insertHint(_global_pos, added); @@ -294,9 +279,9 @@ class AddCore { DomOrdPWMap result() const { return _result; } -private: + private: DomOrdPWMap _result; - std::size_t _global_pos = 0; + std::size_t _global_pos = 0; }; DomOrdPWMap DomOrdPWMap::operator+(const DomOrdPWMap& other) const @@ -338,7 +323,7 @@ std::size_t DomOrdPWMap::arity() const bool DomOrdPWMap::isEmpty() const { return _pieces.empty(); } -Set DomOrdPWMap::domain() const & +Set DomOrdPWMap::domain() const& { Set result; @@ -360,10 +345,11 @@ Set DomOrdPWMap::domain() && return result; } -template +template class RestrictCore { -public: - bool operator()(const MapEntry& entry, const T& subdom) { + public: + bool operator()(const MapEntry& entry, const T& subdom) + { SetAccessKey key = SetAccess::key(); Set set_subdom = key.createSet(SetImplT{subdom}); _result.insert(entry.map().restrict(set_subdom)); @@ -375,7 +361,7 @@ class RestrictCore { DomOrdPWMap result() const { return DomOrdPWMap(std::move(_result)); } -private: + private: DomOrdPWMap _result; }; @@ -386,24 +372,15 @@ DomOrdPWMap DomOrdPWMap::restrict(const Set& subdom) const } SetAccessKey key = SetAccess::key(); - auto restrict_evaluator = Util::Overload { - [&](const OrderedSet& impl) - { - return traverse(_pieces, key.pieces(impl) - , RestrictCore{}).result(); - }, - [&](const OrdUnidimDenseSet& impl) - { - return traverse(_pieces, key.pieces(impl) - , RestrictCore{}).result(); - }, - [&](const auto& impl) - { - Util::ERROR("DomOrdPWMap::restrict: unsupported ", SET_IMPL.kind() - , " set implementation"); - return DomOrdPWMap{}; - } - }; + auto restrict_evaluator = Util::Overload{ + [&](const OrderedSet& impl) { return traverse(_pieces, key.pieces(impl), RestrictCore{}).result(); }, + [&](const OrdUnidimDenseSet& impl) { + return traverse(_pieces, key.pieces(impl), RestrictCore{}).result(); + }, + [&](const auto& impl) { + Util::ERROR("DomOrdPWMap::restrict: unsupported ", SET_IMPL.kind(), " set implementation"); + return DomOrdPWMap{}; + }}; return std::visit(restrict_evaluator, key.impl(subdom)); } @@ -419,10 +396,7 @@ Set DomOrdPWMap::image() const return result; } -Set DomOrdPWMap::image(const Set& subdom) const -{ - return restrict(subdom).image(); -} +Set DomOrdPWMap::image(const Set& subdom) const { return restrict(subdom).image(); } Set DomOrdPWMap::preImage(const Set& subcodom) const { @@ -454,7 +428,7 @@ DomOrdPWMap DomOrdPWMap::composition(const DomOrdPWMap& other) const for (const MapEntry& other_entry : other._pieces) { Map other_map = other_entry.map(); Set img = other_map.image(); - + Perimeter img_perimeter = img.perimeter(); result.advanceHint(global_pos, other_entry); IntTuple img_max_perimeter = img_perimeter.max(); @@ -468,7 +442,7 @@ DomOrdPWMap DomOrdPWMap::composition(const DomOrdPWMap& other) const } // No possible intersection between the current image and the remaining - // elements of _pieces. + // elements of _pieces. if (img_max_perimeter < entry_perimeter.min()) { break; } @@ -514,25 +488,16 @@ Set DomOrdPWMap::fixedPoints() const // Extra operations ------------------------------------------------------------ -DomOrdPWMap DomOrdPWMap::concatenation(const DomOrdPWMap& other) const & -{ - return DomOrdPWMap{*this}.concatenation(other); -} +DomOrdPWMap DomOrdPWMap::concatenation(const DomOrdPWMap& other) const& { return DomOrdPWMap{*this}.concatenation(other); } -DomOrdPWMap DomOrdPWMap::concatenation(const DomOrdPWMap& other) && -{ - return std::move(*this).concatenation(DomOrdPWMap{other}); -} +DomOrdPWMap DomOrdPWMap::concatenation(const DomOrdPWMap& other) && { return std::move(*this).concatenation(DomOrdPWMap{other}); } -DomOrdPWMap DomOrdPWMap::concatenation(DomOrdPWMap&& other) const & -{ - return DomOrdPWMap{*this}.concatenation(std::move(other)); -} +DomOrdPWMap DomOrdPWMap::concatenation(DomOrdPWMap&& other) const& { return DomOrdPWMap{*this}.concatenation(std::move(other)); } DomOrdPWMap DomOrdPWMap::concatenation(DomOrdPWMap&& other) && { // Special cases - if (isEmpty()) { + if (isEmpty()) { return std::move(other); } @@ -542,16 +507,12 @@ DomOrdPWMap DomOrdPWMap::concatenation(DomOrdPWMap&& other) && OrdMapCollection result; if (_pieces.back() < other._pieces.front()) { - result.insert(result.end(), std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(_pieces.end())); - result.insert(result.end(), std::make_move_iterator(other._pieces.begin()) - , std::make_move_iterator(other._pieces.end())); + result.insert(result.end(), std::make_move_iterator(_pieces.begin()), std::make_move_iterator(_pieces.end())); + result.insert(result.end(), std::make_move_iterator(other._pieces.begin()), std::make_move_iterator(other._pieces.end())); } else if (other._pieces.back() < _pieces.front()) { - result.insert(result.end(), std::make_move_iterator(other._pieces.begin()) - , std::make_move_iterator(other._pieces.end())); - result.insert(result.end(), std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(_pieces.end())); - } else { + result.insert(result.end(), std::make_move_iterator(other._pieces.begin()), std::make_move_iterator(other._pieces.end())); + result.insert(result.end(), std::make_move_iterator(_pieces.begin()), std::make_move_iterator(_pieces.end())); + } else { // General case auto it1 = _pieces.begin(); auto end1 = _pieces.end(); @@ -566,31 +527,20 @@ DomOrdPWMap DomOrdPWMap::concatenation(DomOrdPWMap&& other) && ++it2; } } - result.insert(result.end(), std::make_move_iterator(it1) - , std::make_move_iterator(end1)); - result.insert(result.end(), std::make_move_iterator(it2) - , std::make_move_iterator(end2)); + result.insert(result.end(), std::make_move_iterator(it1), std::make_move_iterator(end1)); + result.insert(result.end(), std::make_move_iterator(it2), std::make_move_iterator(end2)); } return DomOrdPWMap{std::move(result)}; } -DomOrdPWMap DomOrdPWMap::combine(const DomOrdPWMap& other) const & -{ - return DomOrdPWMap{*this}.combine(other); -} +DomOrdPWMap DomOrdPWMap::combine(const DomOrdPWMap& other) const& { return DomOrdPWMap{*this}.combine(other); } -DomOrdPWMap DomOrdPWMap::combine(const DomOrdPWMap& other) && -{ - return std::move(*this).combine(DomOrdPWMap{other}); -} +DomOrdPWMap DomOrdPWMap::combine(const DomOrdPWMap& other) && { return std::move(*this).combine(DomOrdPWMap{other}); } -DomOrdPWMap DomOrdPWMap::combine(DomOrdPWMap&& other) const & -{ - return DomOrdPWMap{*this}.combine(std::move(other)); -} +DomOrdPWMap DomOrdPWMap::combine(DomOrdPWMap&& other) const& { return DomOrdPWMap{*this}.combine(std::move(other)); } -DomOrdPWMap DomOrdPWMap::combine(DomOrdPWMap&& other) && +DomOrdPWMap DomOrdPWMap::combine(DomOrdPWMap&& other) && { if (isEmpty()) { return std::move(other); @@ -629,7 +579,7 @@ DomOrdPWMap DomOrdPWMap::min(const DomOrdPWMap& other) const } Set min_in_pw1 = lessImage(other); - return restrict(min_in_pw1).combine(other.restrict(domain())); + return restrict(min_in_pw1).combine(other.restrict(domain())); } DomOrdPWMap DomOrdPWMap::max(const DomOrdPWMap& other) const @@ -644,19 +594,19 @@ DomOrdPWMap DomOrdPWMap::max(const DomOrdPWMap& other) const } class MinAdjCore { -public: - bool operator()(const MapEntry& entry1, const MapEntry& entry2) { + public: + bool operator()(const MapEntry& entry1, const MapEntry& entry2) + { Map min_adj = entry1.map().minAdj(entry2.map()); if (!min_adj.isEmpty()) { Set min_adj_domain = min_adj.domain(); Set repeated = min_adj_domain.intersection(_visited); if (!repeated.isEmpty()) { DomOrdPWMap min_adj_pw{std::move(min_adj)}; - DomOrdPWMap min_adj_repeated = min_adj_pw.restrict(repeated); + DomOrdPWMap min_adj_repeated = min_adj_pw.restrict(repeated); DomOrdPWMap result_repeated = _result.restrict(repeated); DomOrdPWMap min_map = min_adj_repeated.min(result_repeated); - DomOrdPWMap min_adj_result = std::move(min_map).combine( - std::move(min_adj_pw)); + DomOrdPWMap min_adj_result = std::move(min_map).combine(std::move(min_adj_pw)); _result = std::move(min_adj_result).combine(std::move(_result)); _visited = std::move(_visited).cup(std::move(min_adj_domain)); } else { @@ -672,7 +622,7 @@ class MinAdjCore { DomOrdPWMap result() const { return _result; } -private: + private: DomOrdPWMap _result; Set _visited; }; @@ -687,8 +637,9 @@ DomOrdPWMap DomOrdPWMap::minAdj(const DomOrdPWMap& other) const } class MaxAdjCore { -public: - bool operator()(const MapEntry& entry1, const MapEntry& entry2) { + public: + bool operator()(const MapEntry& entry1, const MapEntry& entry2) + { Map max_adj = entry1.map().maxAdj(entry2.map()); if (!max_adj.isEmpty()) { Set max_adj_domain = max_adj.domain(); @@ -698,8 +649,7 @@ class MaxAdjCore { DomOrdPWMap max_adj_repeated = max_adj_pw.restrict(repeated); DomOrdPWMap result_repeated = _result.restrict(repeated); DomOrdPWMap max_map = max_adj_repeated.max(result_repeated); - DomOrdPWMap max_adj_result = std::move(max_map).combine( - std::move(max_adj_pw)); + DomOrdPWMap max_adj_result = std::move(max_map).combine(std::move(max_adj_pw)); _result = std::move(max_adj_result).combine(std::move(_result)); _visited = std::move(_visited).cup(std::move(max_adj_domain)); } else { @@ -715,7 +665,7 @@ class MaxAdjCore { DomOrdPWMap result() const { return _result; } -private: + private: DomOrdPWMap _result; Set _visited; }; @@ -730,8 +680,9 @@ DomOrdPWMap DomOrdPWMap::maxAdj(const DomOrdPWMap& other) const } class EqualImageCore { -public: - bool operator()(const MapEntry& entry1, const MapEntry& entry2) { + public: + bool operator()(const MapEntry& entry1, const MapEntry& entry2) + { Map m1 = entry1.map(); Map m2 = entry2.map(); Set cap_dom = m1.domain().intersection(m2.domain()); @@ -750,7 +701,7 @@ class EqualImageCore { Set result() const { return _result; } -private: + private: Set _result; }; @@ -768,8 +719,9 @@ Set DomOrdPWMap::equalImage(const DomOrdPWMap& other) const } class LessImageCore { -public: - bool operator()(const MapEntry& entry1, const MapEntry& entry2) { + public: + bool operator()(const MapEntry& entry1, const MapEntry& entry2) + { Set less_map = entry1.map().lessImage(entry2.map()); _result = std::move(_result).disjointCup(std::move(less_map)); @@ -780,7 +732,7 @@ class LessImageCore { Set result() const { return _result; } -private: + private: Set _result; }; @@ -795,7 +747,7 @@ Set DomOrdPWMap::lessImage(const DomOrdPWMap& other) const } return traverse(_pieces, other._pieces, LessImageCore{}).result(); -} +} DomOrdPWMap DomOrdPWMap::imageMultiplicity() const { @@ -809,7 +761,7 @@ DomOrdPWMap DomOrdPWMap::imageMultiplicity() const } return result; -} +} void DomOrdPWMap::compact() { @@ -863,8 +815,7 @@ void DomOrdPWMap::compact() // Non-member functions -------------------------------------------------------- -rapidjson::Value toJSON(DomOrdPWMap pw - , rapidjson::Document::AllocatorType& alloc) +rapidjson::Value toJSON(DomOrdPWMap pw, rapidjson::Document::AllocatorType& alloc) { rapidjson::Value result{rapidjson::kArrayType}; @@ -876,9 +827,8 @@ rapidjson::Value toJSON(DomOrdPWMap pw return result; } -} // namespace detail - -} // namespace LIB +} // namespace detail -} // namespace SBG; +} // namespace LIB +} // namespace SBG diff --git a/sbg/expression.cpp b/sbg/expression.cpp old mode 100755 new mode 100644 index 4c70a1ed..cd8364e4 --- a/sbg/expression.cpp +++ b/sbg/expression.cpp @@ -36,15 +36,10 @@ Expression::Expression(const IntTuple& x) } } -Expression::Expression(const Rational& slope, const Rational& offset) : _impl() -{ - _impl.emplace_back(detail::LinearExpr{slope, offset}); -} +Expression::Expression(const Rational& slope, const Rational& offset) : _impl() { _impl.emplace_back(detail::LinearExpr{slope, offset}); } -Expression::Expression(std::size_t n, const Rational& slope - , const Rational& offset) - : _impl() -{ +Expression::Expression(std::size_t n, const Rational& slope, const Rational& offset) : _impl() +{ detail::LinearExpr linear_expr{slope, offset}; for (unsigned int k = 0; k < n; ++k) { _impl.emplace_back(linear_expr); @@ -60,29 +55,19 @@ Expression::Expression(const IntTuple& from, const IntTuple& to) Expression::Expression(const detail::ExpressionImpl& impl) : _impl(impl) {} -Expression::Expression(detail::ExpressionImpl&& impl) - : _impl(std::move(impl)) {} +Expression::Expression(detail::ExpressionImpl&& impl) : _impl(std::move(impl)) {} // Setters (private) ----------------------------------------------------------- detail::LinearExpr& Expression::operator[](std::size_t n) { return _impl[n]; } -const detail::LinearExpr &Expression::operator[](std::size_t n) const -{ - return _impl[n]; -} +const detail::LinearExpr& Expression::operator[](std::size_t n) const { return _impl[n]; } // Operators ------------------------------------------------------------------- -bool Expression::operator==(const Expression& other) const -{ - return _impl == other._impl; -} +bool Expression::operator==(const Expression& other) const { return _impl == other._impl; } -bool Expression::operator!=(const Expression& other) const -{ - return !(*this == other); -} +bool Expression::operator!=(const Expression& other) const { return !(*this == other); } Expression Expression::operator+(const Expression& other) const { @@ -112,10 +97,10 @@ std::ostream& Expression::print(std::ostream& out) const out << "|"; if (sz > 0) { - for (unsigned int k = 0; k < sz-1; ++k) { + for (unsigned int k = 0; k < sz - 1; ++k) { out << _impl[k] << "|"; } - out << _impl[sz-1]; + out << _impl[sz - 1]; } out << "|"; @@ -221,14 +206,12 @@ Expression Expression::cartesianProduct(const Expression& other) const Expression result; result._impl.insert(result._impl.end(), _impl.begin(), _impl.end()); - result._impl.insert(result._impl.end(), other._impl.begin() - , other._impl.end()); + result._impl.insert(result._impl.end(), other._impl.begin(), other._impl.end()); return result; } -rapidjson::Value Expression::toJSON(rapidjson::Document::AllocatorType& alloc) - const +rapidjson::Value Expression::toJSON(rapidjson::Document::AllocatorType& alloc) const { rapidjson::Value result{rapidjson::kArrayType}; @@ -240,6 +223,6 @@ rapidjson::Value Expression::toJSON(rapidjson::Document::AllocatorType& alloc) return result; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/fixed_points.cpp b/sbg/fixed_points.cpp old mode 100755 new mode 100644 index 030833a0..28830561 --- a/sbg/fixed_points.cpp +++ b/sbg/fixed_points.cpp @@ -25,13 +25,12 @@ namespace LIB { Solution::Solution(const SolutionKind kind) : _kind(kind), _value() {} -Solution::Solution(const SolutionKind kind, const Int value) - : _kind(kind), _value(value) {} +Solution::Solution(const SolutionKind kind, const Int value) : _kind(kind), _value(value) {} const SolutionKind& Solution::kind() const { return _kind; } const std::optional& Solution::value() const { return _value; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/integer.cpp b/sbg/integer.cpp old mode 100755 new mode 100644 index dd1c86e2..b7a4d5ed --- a/sbg/integer.cpp +++ b/sbg/integer.cpp @@ -32,7 +32,7 @@ IntTuple::IntTuple() : _value() {} IntTuple::IntTuple(const Int x) : _value() { _value.emplace_back(x); } IntTuple::IntTuple(const std::size_t k, const Int x) : _value() -{ +{ for (unsigned int j = 0; j < k; ++j) { _value.push_back(x); } @@ -60,10 +60,7 @@ Int& IntTuple::operator[](std::size_t n) { return _value[n]; } const Int& IntTuple::operator[](std::size_t n) const { return _value[n]; } -bool IntTuple::operator==(const IntTuple& other) const -{ - return _value == other._value; -} +bool IntTuple::operator==(const IntTuple& other) const { return _value == other._value; } bool IntTuple::operator!=(const IntTuple& other) const { return !(*this == other); } @@ -80,11 +77,7 @@ bool IntTuple::operator<(const IntTuple& other) const return false; } - -bool IntTuple::operator<=(const IntTuple& other) const -{ - return *this == other || *this < other; -} +bool IntTuple::operator<=(const IntTuple& other) const { return *this == other || *this < other; } IntTuple IntTuple::operator+(const IntTuple& other) const { @@ -110,15 +103,14 @@ std::ostream& operator<<(std::ostream& out, const IntTuple& md) out << aux[0]; } else if (sz > 1) { out << "("; - for (unsigned int j = 0; j < sz - 1; ++j) - out << aux[j] << ", "; - out << aux[sz-1]; + for (unsigned int j = 0; j < sz - 1; ++j) out << aux[j] << ", "; + out << aux[sz - 1]; out << ")"; } return out; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/interval.cpp b/sbg/interval.cpp old mode 100755 new mode 100644 index c3f7568f..dd5499d9 --- a/sbg/interval.cpp +++ b/sbg/interval.cpp @@ -47,11 +47,9 @@ Interval::Interval() : _begin(1), _step(1), _end(0) {} Interval::Interval(const Int x) : _begin(x), _step(1), _end(x) {} -Interval::Interval(const Int begin, const Int step, const Int end) - : _begin(begin), _step(step), _end(end) +Interval::Interval(const Int begin, const Int step, const Int end) : _begin(begin), _step(step), _end(end) { - Util::ERROR_UNLESS(step >= 0 - , "Interval::Interval: step must be non-negative\n"); + Util::ERROR_UNLESS(step >= 0, "Interval::Interval: step must be non-negative\n"); if (end >= begin) { _end = end - ((end - begin) % step); @@ -65,27 +63,17 @@ Interval::Interval(const Int begin, const Int step, const Int end) // Getters --------------------------------------------------------------------- -const Int& Interval::begin() const -{ - return _begin; -} +const Int& Interval::begin() const { return _begin; } -const Int& Interval::step() const -{ - return _step; -} +const Int& Interval::step() const { return _step; } -const Int& Interval::end() const -{ - return _end; -} +const Int& Interval::end() const { return _end; } // Operators ------------------------------------------------------------------- -bool operator==(const Interval& lhs, const Interval& rhs) +bool operator==(const Interval& lhs, const Interval& rhs) { - return (lhs.begin() == rhs.begin()) && (lhs.step() == rhs.step()) - && (lhs.end() == rhs.end()); + return (lhs.begin() == rhs.begin()) && (lhs.step() == rhs.step()) && (lhs.end() == rhs.end()); } bool operator!=(const Interval& lhs, const Interval& rhs) @@ -105,28 +93,22 @@ bool operator!=(const Interval& lhs, const Interval& rhs) return false; } -bool Interval::operator<(const Interval& other) const -{ - return _begin < other._begin; -} +bool Interval::operator<(const Interval& other) const { return _begin < other._begin; } -std::ostream& operator<<(std::ostream& out, const Interval& i) +std::ostream& operator<<(std::ostream& out, const Interval& i) { out << "[" << i.begin(); if (i.step() != 1) { out << ":" << i.step(); } out << ":" << i.end() << "]"; - + return out; } // Set functions --------------------------------------------------------------- -unsigned int Interval::cardinal() const -{ - return std::abs(_end - _begin) / _step + 1; -} +unsigned int Interval::cardinal() const { return std::abs(_end - _begin) / _step + 1; } bool Interval::isEmpty() const { return _end < _begin; } @@ -145,8 +127,7 @@ Interval Interval::intersection(const Interval& other) const } // Two non overlapping intervals with the same step. - if (_step == other._step && !isMember(_begin, other) - && !isMember(other._begin, *this)) { + if (_step == other._step && !isMember(_begin, other) && !isMember(other._begin, *this)) { return Interval{}; } @@ -171,10 +152,7 @@ Interval Interval::intersection(const Interval& other) const // Member functions ------------------------------------------------------------ -Interval Interval::translate(const Int t) const -{ - return Interval{_begin + t, _step, _end + t}; -} +Interval Interval::translate(const Int t) const { return Interval{_begin + t, _step, _end + t}; } Perimeter Interval::perimeter() const { return Perimeter(_begin, _end); } @@ -186,8 +164,7 @@ MaybeInterval Interval::compact(const Interval& other) const } else if (other._end + _step == _begin) { return Interval{other._begin, _step, _end}; } else if (!intersection(other).isEmpty()) { - return Interval{std::min(_begin, other._begin), _step - , std::max(_end, other._end)}; + return Interval{std::min(_begin, other._begin), _step, std::max(_end, other._end)}; } } @@ -210,8 +187,8 @@ rapidjson::Value toJSON(Interval i, rapidjson::Document::AllocatorType& alloc) return result; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/linear_expr.cpp b/sbg/linear_expr.cpp old mode 100755 new mode 100644 index 465094bc..829f5941 --- a/sbg/linear_expr.cpp +++ b/sbg/linear_expr.cpp @@ -32,8 +32,7 @@ namespace detail { LinearExpr::LinearExpr() : _slope(1), _offset(0) {} -LinearExpr::LinearExpr(Rational slope, Rational offset) - : _slope(slope), _offset(offset) {} +LinearExpr::LinearExpr(Rational slope, Rational offset) : _slope(slope), _offset(offset) {} // Getters --------------------------------------------------------------------- @@ -43,25 +42,13 @@ const Rational& LinearExpr::offset() const { return _offset; } // Operators ------------------------------------------------------------------- -bool LinearExpr::operator==(const LinearExpr& other) const -{ - return _slope == other._slope && _offset == other._offset; -} +bool LinearExpr::operator==(const LinearExpr& other) const { return _slope == other._slope && _offset == other._offset; } -bool LinearExpr::operator!=(const LinearExpr& other) const -{ - return !(*this == other); -} +bool LinearExpr::operator!=(const LinearExpr& other) const { return !(*this == other); } -LinearExpr LinearExpr::operator+(const LinearExpr& other) const -{ - return LinearExpr{_slope + other._slope, _offset + other._offset}; -} +LinearExpr LinearExpr::operator+(const LinearExpr& other) const { return LinearExpr{_slope + other._slope, _offset + other._offset}; } -LinearExpr LinearExpr::operator-(const LinearExpr& other) const -{ - return LinearExpr{_slope - other._slope, _offset - other._offset}; -} +LinearExpr LinearExpr::operator-(const LinearExpr& other) const { return LinearExpr{_slope - other._slope, _offset - other._offset}; } std::ostream& operator<<(std::ostream& out, const LinearExpr& le) { @@ -100,15 +87,12 @@ std::ostream& operator<<(std::ostream& out, const LinearExpr& le) // Linear expression functions ------------------------------------------------- -Int LinearExpr::apply(const Int& x) const -{ - return (_slope*x + _offset).toInt(); -} +Int LinearExpr::apply(const Int& x) const { return (_slope * x + _offset).toInt(); } LinearExpr LinearExpr::composition(const LinearExpr& other) const { - Rational new_slope = other._slope*_slope; - Rational new_offset = _slope*other._offset + _offset; + Rational new_slope = other._slope * _slope; + Rational new_offset = _slope * other._offset + _offset; return LinearExpr{new_slope, new_offset}; } @@ -118,7 +102,7 @@ LinearExpr LinearExpr::inverse() const Rational new_slope{0, 1}; Rational new_offset{0, 1}; new_slope = Rational{_slope.denominator(), _slope.numerator()}; - new_offset = (-_offset)/_slope; + new_offset = (-_offset) / _slope; return LinearExpr{new_slope, new_offset}; } @@ -129,13 +113,9 @@ bool LinearExpr::isConstant() const { return _slope == 0; } bool LinearExpr::isInjective() const { return _slope != 0; } -Rational LinearExpr::intersectionPoint(const LinearExpr& other) const -{ - return (other._offset - _offset)/(_slope - other._slope); -} +Rational LinearExpr::intersectionPoint(const LinearExpr& other) const { return (other._offset - _offset) / (_slope - other._slope); } -rapidjson::Value toJSON(LinearExpr le - , rapidjson::Document::AllocatorType& alloc) +rapidjson::Value toJSON(LinearExpr le, rapidjson::Document::AllocatorType& alloc) { rapidjson::Value result{rapidjson::kArrayType}; @@ -154,8 +134,8 @@ rapidjson::Value toJSON(LinearExpr le return result; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/map.cpp b/sbg/map.cpp old mode 100755 new mode 100644 index f92ca5d4..1c5b08ae --- a/sbg/map.cpp +++ b/sbg/map.cpp @@ -34,17 +34,15 @@ namespace LIB { Map::Map() : _domain() {} -Map::Map(const IntTuple& x, const Expression& expr) - : _domain(x), _law(expr) {} +Map::Map(const IntTuple& x, const Expression& expr) : _domain(x), _law(expr) {} Map::Map(const Set& s, const Expression& expr) : _domain(s), _law(expr) {} -Map::Map(Set&& s, Expression&& expr) - : _domain(std::move(s)), _law(std::move(expr)) {} +Map::Map(Set&& s, Expression&& expr) : _domain(std::move(s)), _law(std::move(expr)) {} // Getters --------------------------------------------------------------------- -const Set& Map::domain() const & { return _domain; } +const Set& Map::domain() const& { return _domain; } Set Map::domain() && { return std::move(_domain); } @@ -57,7 +55,7 @@ bool Map::operator==(const Map& other) const if (_domain == other._domain) { if (_domain.cardinal() == 1) { return image() == other.image(); - } else { + } else { return _law == other._law; } } @@ -65,10 +63,7 @@ bool Map::operator==(const Map& other) const return false; } -bool Map::operator!=(const Map& other) const -{ - return !(*this == other); -} +bool Map::operator!=(const Map& other) const { return !(*this == other); } Map Map::operator+(const Map& other) const { @@ -90,20 +85,11 @@ std::size_t Map::arity() const { return _law.arity(); } bool Map::isEmpty() const { return _domain.isEmpty(); } -Map Map::restrict(const Set& subdom) const & -{ - return Map{_domain.intersection(subdom), _law}; -} +Map Map::restrict(const Set& subdom) const& { return Map{_domain.intersection(subdom), _law}; } -Map Map::restrict(const Set& subdom) && -{ - return Map{std::move(_domain).intersection(subdom), _law}; -} +Map Map::restrict(const Set& subdom) && { return Map{std::move(_domain).intersection(subdom), _law}; } -Map Map::restrict(Set&& subdom) const & -{ - return Map{_domain.intersection(std::move(subdom)), _law}; -} +Map Map::restrict(Set&& subdom) const& { return Map{_domain.intersection(std::move(subdom)), _law}; } Set Map::image() const { return image(_domain); } @@ -124,14 +110,10 @@ Set Map::image(const Set& subdom) const Set Map::preImage(const Set& subcodom) const { Set image_subcodom = image().intersection(subcodom); - return detail::MapDetail::preImage(image_subcodom, _law) - .intersection(_domain); + return detail::MapDetail::preImage(image_subcodom, _law).intersection(_domain); } -Map Map::inverse() const -{ - return detail::MapDetail::inverse(_domain, _law); -} +Map Map::inverse() const { return detail::MapDetail::inverse(_domain, _law); } Map Map::composition(const Map& other) const { @@ -211,15 +193,9 @@ Map Map::maxAdj(const Map& other) const return Map{result_domain, result_expr}; } -std::vector Map::reduce() const -{ - return detail::MapDetail::reduce(*this); -} +std::vector Map::reduce() const { return detail::MapDetail::reduce(*this); } -std::vector Map::imageMultiplicity() const -{ - return detail::MapDetail::imageMultiplicity(*this); -} +std::vector Map::imageMultiplicity() const { return detail::MapDetail::imageMultiplicity(*this); } MaybeMap Map::compact(const Map& other) const { @@ -245,6 +221,6 @@ rapidjson::Value toJSON(Map m, rapidjson::Document::AllocatorType& alloc) return result; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/map_detail.cpp b/sbg/map_detail.cpp old mode 100755 new mode 100644 index 9eb7e3b8..2819b40e --- a/sbg/map_detail.cpp +++ b/sbg/map_detail.cpp @@ -56,7 +56,7 @@ Interval image(const Interval& i, const LinearExpr& linear_expr) Int new_begin = linear_expr.apply(i.begin()); Rational m = linear_expr.slope(); Int step = i.step(); - Int new_step = m >= 0 ? (m*step).toInt() : (-m*step).toInt(); + Int new_step = m >= 0 ? (m * step).toInt() : (-m * step).toInt(); Int new_end = linear_expr.apply(i.end()); return Interval{new_begin, new_step, new_end}; @@ -75,9 +75,8 @@ MultiDimInter image(const MultiDimInter& mdi, const ExpressionImpl& expr) return result; } -template -CompactSetImplT compactImage(const CompactSetImplT& s, const ExprImplT& expr - , bool is_injective) +template +CompactSetImplT compactImage(const CompactSetImplT& s, const ExprImplT& expr, bool is_injective) { CompactSetImplT result; @@ -99,28 +98,20 @@ Set MapDetail::image(const Set& s, const Expression& expr) { bool is_injective = expr.isInjective(); SetAccessKey key = SetAccess::key(); - auto image_evaluator = Util::Overload { - [&](const UnorderedSet& a) - { - return key.createSet(detail::compactImage(a, expr._impl, is_injective)); - }, - [&](const OrdUnidimDenseSet& a) - { - return key.createSet(detail::compactImage(a, expr._impl[0], is_injective)); - }, - [&](const OrderedSet& a) - { - return key.createSet(detail::compactImage(a, expr._impl, is_injective)); - }, - [&](const auto& a) - { - Util::ERROR("MapDetail::image: unsupported Set implementation\n"); - return Set{SetKind::kUnordered}; - } - }; + auto image_evaluator = + Util::Overload{[&](const UnorderedSet& a) { + return key.createSet(detail::compactImage(a, expr._impl, is_injective)); + }, + [&](const OrdUnidimDenseSet& a) { + return key.createSet(detail::compactImage(a, expr._impl[0], is_injective)); + }, + [&](const OrderedSet& a) { + return key.createSet(detail::compactImage(a, expr._impl, is_injective)); + }, + [&](const auto& a) { + Util::ERROR("MapDetail::image: unsupported Set implementation\n"); + return Set{SetKind::kUnordered}; + }}; return std::visit(image_evaluator, key.impl(s)); } @@ -152,7 +143,7 @@ MultiDimInter preImage(const MultiDimInter& mdi, const ExpressionImpl& expr) return result; } -template +template CompactSetImplT compactPreImage(const CompactSetImplT& s, const ExprImplT& expr) { CompactSetImplT result; @@ -167,28 +158,18 @@ CompactSetImplT compactPreImage(const CompactSetImplT& s, const ExprImplT& expr) Set MapDetail::preImage(const Set& s, const Expression& expr) { SetAccessKey key = SetAccess::key(); - auto pre_image_evaluator = Util::Overload { - [&](const UnorderedSet& a) - { - return key.createSet(detail::compactPreImage(a, expr._impl)); - }, - [&](const OrdUnidimDenseSet& a) - { - return key.createSet(detail::compactPreImage(a, expr._impl[0])); - }, - [&](const OrderedSet& a) - { - return key.createSet(detail::compactPreImage(a, expr._impl)); - }, - [&](const auto& a) - { - Util::ERROR("MapDetail::preImage: unsupported Set implementation\n"); - return Set{SetKind::kUnordered}; - } - }; + auto pre_image_evaluator = Util::Overload{ + [&](const UnorderedSet& a) { + return key.createSet(detail::compactPreImage(a, expr._impl)); + }, + [&](const OrdUnidimDenseSet& a) { + return key.createSet(detail::compactPreImage(a, expr._impl[0])); + }, + [&](const OrderedSet& a) { return key.createSet(detail::compactPreImage(a, expr._impl)); }, + [&](const auto& a) { + Util::ERROR("MapDetail::preImage: unsupported Set implementation\n"); + return Set{SetKind::kUnordered}; + }}; return std::visit(pre_image_evaluator, key.impl(s)); } @@ -199,7 +180,7 @@ AtomicMap inverse(const Interval& i, const LinearExpr& linear_expr) Interval img = image(i, linear_expr); if (linear_expr.isConstant()) { return AtomicMap{img, LinearExpr{1, i.begin() - img.begin()}}; - } + } return AtomicMap{img, linear_expr.inverse()}; } @@ -221,14 +202,11 @@ AtomicMDMap inverse(const MultiDimInter& mdi, const ExpressionImpl& expr) return AtomicMDMap{result_domain, result_law}; } -template -std::pair compactInverse(const CompactSetImplT& s - , const ExprImplT& expr, bool is_injective) +template +std::pair compactInverse(const CompactSetImplT& s, const ExprImplT& expr, bool is_injective) { - CompactSetImplT image = compactImage( - s, expr, is_injective); - Util::ERROR_UNLESS(s.cardinal() == image.cardinal(), "MapDetail/inverse: " - , "(s, expr) pair is not invertible\n"); + CompactSetImplT image = compactImage(s, expr, is_injective); + Util::ERROR_UNLESS(s.cardinal() == image.cardinal(), "MapDetail/inverse: ", "(s, expr) pair is not invertible\n"); ExprImplT result_law = detail::inverse(*(s.begin()), expr).second; @@ -243,38 +221,29 @@ Map MapDetail::inverse(const Set& s, const Expression& expr) bool is_injective = expr.isInjective(); SetAccessKey key = SetAccess::key(); - auto inverse_evaluator = Util::Overload { - [&](const UnorderedSet& a) - { - auto [domain, law] = compactInverse(a, expr._impl, is_injective); - return Map{key.createSet(domain), Expression{law}}; - }, - [&](const OrdUnidimDenseSet& a) - { - auto [domain, law] = compactInverse(a, expr._impl[0], is_injective); - return Map{key.createSet(domain), Expression{ExpressionImpl{law}}}; - }, - [&](const OrderedSet& a) - { - auto [domain, law] = compactInverse(a, expr._impl, is_injective); - return Map{key.createSet(domain), Expression{law}}; - }, - [&](const auto& a) - { - Util::ERROR("MapDetail::preImage: unsupported Set implementation\n"); - return Set{SetKind::kUnordered}; - } - }; + auto inverse_evaluator = + Util::Overload{[&](const UnorderedSet& a) { + auto [domain, law] = compactInverse(a, expr._impl, is_injective); + return Map{key.createSet(domain), Expression{law}}; + }, + [&](const OrdUnidimDenseSet& a) { + auto [domain, law] = compactInverse(a, expr._impl[0], is_injective); + return Map{key.createSet(domain), Expression{ExpressionImpl{law}}}; + }, + [&](const OrderedSet& a) { + auto [domain, law] = compactInverse(a, expr._impl, is_injective); + return Map{key.createSet(domain), Expression{law}}; + }, + [&](const auto& a) { + Util::ERROR("MapDetail::preImage: unsupported Set implementation\n"); + return Set{SetKind::kUnordered}; + }}; return std::visit(inverse_evaluator, key.impl(s)); } // Less image ------------------------------------------------------------------ -Interval lessImage(const LinearExpr& linear_expr1 - , const LinearExpr& linear_expr2) +Interval lessImage(const LinearExpr& linear_expr1, const LinearExpr& linear_expr2) { Rational m1 = linear_expr1.slope(); Rational m2 = linear_expr2.slope(); @@ -292,12 +261,10 @@ Interval lessImage(const LinearExpr& linear_expr1 if (ceil == floor) { Int floor_minus = floor == kNegInf ? kNegInf : floor - 1; Int ceil_plus = ceil == kPosInf ? kPosInf : ceil + 1; - Interval kth = m1 < m2 ? Interval{ceil_plus, 1, kPosInf} - : Interval {kNegInf, 1, floor_minus}; + Interval kth = m1 < m2 ? Interval{ceil_plus, 1, kPosInf} : Interval{kNegInf, 1, floor_minus}; return kth; } else { - Interval kth = m1 < m2 ? Interval{ceil, 1, kPosInf} - : Interval {kNegInf, 1, floor}; + Interval kth = m1 < m2 ? Interval{ceil, 1, kPosInf} : Interval{kNegInf, 1, floor}; return kth; } } else { @@ -310,8 +277,7 @@ Interval lessImage(const LinearExpr& linear_expr1 return Interval{}; } -std::vector lessImage(const ExpressionImpl& expr1 - , const ExpressionImpl& expr2) +std::vector lessImage(const ExpressionImpl& expr1, const ExpressionImpl& expr2) { std::vector result; @@ -346,9 +312,8 @@ std::vector lessImage(const ExpressionImpl& expr1 return result; } -template -void lessImage(const ExprImplT& expr1 - , const ExprImplT& expr2, CompactSetImplT& result) +template +void lessImage(const ExprImplT& expr1, const ExprImplT& expr2, CompactSetImplT& result) { std::vector less_image = detail::lessImage(expr1, expr2); @@ -362,37 +327,28 @@ Set MapDetail::lessImage(const Expression& expr1, const Expression& expr2) Set result; SetAccessKey key = SetAccess::key(); - auto less_image_evaluator = Util::Overload { - [&](UnorderedSet&& a) - { - detail::lessImage( - expr1._impl, expr2._impl, a); - return key.createSet(a); - }, - [&](OrdUnidimDenseSet&& a) - { - a.pushBack(detail::lessImage(expr1._impl[0], expr2._impl[0])); - return key.createSet(a); - }, - [&](OrderedSet&& a) - { - detail::lessImage( - expr1._impl, expr2._impl, a); - return key.createSet(a); - }, - [&](auto&& a) - { - Util::ERROR("MapDetail::lessImage: unsupported Set implementation\n"); - return Set{SetKind::kUnordered}; - } - }; + auto less_image_evaluator = Util::Overload{[&](UnorderedSet&& a) { + detail::lessImage(expr1._impl, expr2._impl, a); + return key.createSet(a); + }, + [&](OrdUnidimDenseSet&& a) { + a.pushBack(detail::lessImage(expr1._impl[0], expr2._impl[0])); + return key.createSet(a); + }, + [&](OrderedSet&& a) { + detail::lessImage(expr1._impl, expr2._impl, a); + return key.createSet(a); + }, + [&](auto&& a) { + Util::ERROR("MapDetail::lessImage: unsupported Set implementation\n"); + return Set{SetKind::kUnordered}; + }}; return std::visit(less_image_evaluator, key.impl(result)); } // Reduction ------------------------------------------------------------------- -void partition(const Interval& i, const LinearExpr& linear_expr - , AtomicMapVector& result) +void partition(const Interval& i, const LinearExpr& linear_expr, AtomicMapVector& result) { Int h = linear_expr.offset().toInt(); Int absh = std::abs(h); @@ -436,7 +392,7 @@ AtomicMapVector reduce(const Interval& i, const LinearExpr& linear_expr) // Partition of the piece needed if (h % st == 0) { // Is convenient the partition of the piece? - if ((Int) i.cardinal() > h*h) { + if ((Int)i.cardinal() > h * h) { partition(i, linear_expr, result); } else { result.emplace_back(i, linear_expr); @@ -445,7 +401,7 @@ AtomicMapVector reduce(const Interval& i, const LinearExpr& linear_expr) result.emplace_back(i, linear_expr); } - return result; + return result; } bool isReductionEfficient(const ExpressionImpl& expr) @@ -464,8 +420,7 @@ bool isReductionEfficient(const ExpressionImpl& expr) return count == 1; } -AtomicMapVector reduce(const MultiDimInter& mdi, const ExpressionImpl& expr - , unsigned int& k_reduce) +AtomicMapVector reduce(const MultiDimInter& mdi, const ExpressionImpl& expr, unsigned int& k_reduce) { // Identify reducible dimension k_reduce std::size_t arity = mdi.arity(); @@ -474,7 +429,7 @@ AtomicMapVector reduce(const MultiDimInter& mdi, const ExpressionImpl& expr for (std::size_t k = 0; k < arity; ++k) { LinearExpr linear_expr = expr[k]; if (linear_expr.slope() == 1 && linear_expr.offset() != 0) { - k_reduce = k; + k_reduce = k; reducible_interval = mdi[k]; reducible_expr = linear_expr; break; @@ -484,9 +439,8 @@ AtomicMapVector reduce(const MultiDimInter& mdi, const ExpressionImpl& expr return reduce(reducible_interval, reducible_expr); } -template -MapVector MapDetail::compactReduce(const CompactSetImplT& s - , const ExpressionImpl& expr) +template +MapVector MapDetail::compactReduce(const CompactSetImplT& s, const ExpressionImpl& expr) { MapVector result; @@ -508,8 +462,7 @@ MapVector MapDetail::compactReduce(const CompactSetImplT& s return result; } -MapVector MapDetail::reduce(const OrdUnidimDenseSet& s - , const ExpressionImpl& expr) +MapVector MapDetail::reduce(const OrdUnidimDenseSet& s, const ExpressionImpl& expr) { MapVector result; @@ -519,8 +472,7 @@ MapVector MapDetail::reduce(const OrdUnidimDenseSet& s for (const AtomicMap& r : reduced) { OrdUnidimDenseSet domain; domain.pushBack(r.first); - result.emplace_back(key.createSet(domain) - , Expression{ExpressionImpl{r.second}}); + result.emplace_back(key.createSet(domain), Expression{ExpressionImpl{r.second}}); } } @@ -539,25 +491,13 @@ MapVector MapDetail::reduce(const Map& m) } SetAccessKey key = SetAccess::key(); - auto reduce_evaluator = Util::Overload { - [&](const UnorderedSet& a) - { - return compactReduce(a, law._impl); - }, - [&](const OrdUnidimDenseSet& a) - { - return reduce(a, law._impl); - }, - [&](const OrderedSet& a) - { - return compactReduce(a, law._impl); - }, - [&](const auto& a) - { - Util::ERROR("MapDetail::reduce: unsupported Set implementation\n"); - return Set{SetKind::kUnordered}; - } - }; + auto reduce_evaluator = Util::Overload{[&](const UnorderedSet& a) { return compactReduce(a, law._impl); }, + [&](const OrdUnidimDenseSet& a) { return reduce(a, law._impl); }, + [&](const OrderedSet& a) { return compactReduce(a, law._impl); }, + [&](const auto& a) { + Util::ERROR("MapDetail::reduce: unsupported Set implementation\n"); + return Set{SetKind::kUnordered}; + }}; return std::visit(reduce_evaluator, key.impl(m.domain())); } @@ -573,8 +513,7 @@ AtomicMap imageMultiplicity(const Interval& i, const LinearExpr& linear_expr) return AtomicMap{image, LinearExpr{0, 1}}; } -AtomicMDMap imageMultiplicity(const MultiDimInter& mdi - , const ExpressionImpl& expr) +AtomicMDMap imageMultiplicity(const MultiDimInter& mdi, const ExpressionImpl& expr) { MultiDimInter result_mdi; @@ -590,9 +529,8 @@ AtomicMDMap imageMultiplicity(const MultiDimInter& mdi return AtomicMDMap{result_mdi, result_expr}; } -template -MapVector MapDetail::imageMultiplicity(const CompactSetImplT& s - , const ExprImplT& expr) +template +MapVector MapDetail::imageMultiplicity(const CompactSetImplT& s, const ExprImplT& expr) { MapVector result; @@ -602,8 +540,7 @@ MapVector MapDetail::imageMultiplicity(const CompactSetImplT& s bool repeated = false; AtomicMDMap jth = detail::imageMultiplicity(p, expr); - Map p_multiplicity{key.createSet(CompactSetImplT{std::get<0>(jth)}) - , std::get<1>(jth)}; + Map p_multiplicity{key.createSet(CompactSetImplT{std::get<0>(jth)}), std::get<1>(jth)}; for (const Map& m : result) { Set m_domain = m.domain(); Set cap = m_domain.intersection(p_multiplicity.domain()); @@ -643,41 +580,27 @@ MapVector MapDetail::imageMultiplicity(const Map& m) } if (law.isConstant()) { - result.emplace_back(m.image(), Expression{IntTuple{domain.arity() - , m.domain().cardinal()}}); + result.emplace_back(m.image(), Expression{IntTuple{domain.arity(), m.domain().cardinal()}}); return result; } SetAccessKey key = SetAccess::key(); - auto img_mult_evaluator = Util::Overload { - [&](const UnorderedSet& a) - { - return imageMultiplicity( - a, law._impl); - }, - [&](const OrdUnidimDenseSet& a) - { - Util::ERROR("MapDetail::imageMultiplicity: OrdUnidimDenseSet already " - , "solved\n"); - return MapVector{}; - }, - [&](const OrderedSet& a) - { - return imageMultiplicity( - a, law._impl); - }, - [&](const auto& a) - { - Util::ERROR("MapDetail::imageMultiplicity: unsupported Set " - , "implementation\n"); - return MapVector{}; - } - }; + auto img_mult_evaluator = + Util::Overload{[&](const UnorderedSet& a) { return imageMultiplicity(a, law._impl); }, + [&](const OrdUnidimDenseSet& a) { + Util::ERROR("MapDetail::imageMultiplicity: OrdUnidimDenseSet already ", "solved\n"); + return MapVector{}; + }, + [&](const OrderedSet& a) { return imageMultiplicity(a, law._impl); }, + [&](const auto& a) { + Util::ERROR("MapDetail::imageMultiplicity: unsupported Set ", "implementation\n"); + return MapVector{}; + }}; return std::visit(img_mult_evaluator, key.impl(m.domain())); } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/map_entry.cpp b/sbg/map_entry.cpp old mode 100755 new mode 100644 index b7faa29f..a224fe4b --- a/sbg/map_entry.cpp +++ b/sbg/map_entry.cpp @@ -29,25 +29,17 @@ namespace detail { // Map entry ------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// -MapEntry::MapEntry(const Set& s, const Expression& expr) - : _map(s, expr), _perimeter(s.perimeter()) {} +MapEntry::MapEntry(const Set& s, const Expression& expr) : _map(s, expr), _perimeter(s.perimeter()) {} -MapEntry::MapEntry(const Map& m) - : _map(m), _perimeter(m.domain().perimeter()) {} +MapEntry::MapEntry(const Map& m) : _map(m), _perimeter(m.domain().perimeter()) {} const Map& MapEntry::map() const { return _map; } const Perimeter& MapEntry::perimeter() const { return _perimeter; } -bool MapEntry::operator==(const MapEntry& other) const -{ - return _map == other._map; -} +bool MapEntry::operator==(const MapEntry& other) const { return _map == other._map; } -bool MapEntry::operator<(const MapEntry& other) const -{ - return _perimeter.min() < other._perimeter.min(); -} +bool MapEntry::operator<(const MapEntry& other) const { return _perimeter.min() < other._perimeter.min(); } MaybeMapEntry MapEntry::compact(const MapEntry& other) const { @@ -58,9 +50,9 @@ MaybeMapEntry MapEntry::compact(const MapEntry& other) const return {}; } - -} // namespace detail -} // namespace LIB +} // namespace detail + +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/multidim_inter.cpp b/sbg/multidim_inter.cpp old mode 100755 new mode 100644 index a5365798..85f6d49c --- a/sbg/multidim_inter.cpp +++ b/sbg/multidim_inter.cpp @@ -38,13 +38,9 @@ MultiDimInter::MultiDimInter(const IntTuple& x) : _intervals() } } -MultiDimInter::MultiDimInter(const Interval& i) : _intervals() -{ - _intervals.push_back(i); -} +MultiDimInter::MultiDimInter(const Interval& i) : _intervals() { _intervals.push_back(i); } -MultiDimInter::MultiDimInter(const std::size_t k, const Interval& i) - : _intervals() +MultiDimInter::MultiDimInter(const std::size_t k, const Interval& i) : _intervals() { for (unsigned int j = 0; j < k; ++j) { _intervals.push_back(i); @@ -55,20 +51,14 @@ MultiDimInter::MultiDimInter(InterVector iv) : _intervals(std::move(iv)) {} // Getters --------------------------------------------------------------------- -MultiDimInter::ConstIt MultiDimInter::begin() const -{ - return _intervals.begin(); -} +MultiDimInter::ConstIt MultiDimInter::begin() const { return _intervals.begin(); } -MultiDimInter::ConstIt MultiDimInter::end() const -{ - return _intervals.end(); -} +MultiDimInter::ConstIt MultiDimInter::end() const { return _intervals.end(); } // Setters --------------------------------------------------------------------- -void MultiDimInter::pushBack(const Interval& i) -{ +void MultiDimInter::pushBack(const Interval& i) +{ if (!i.isEmpty()) { _intervals.push_back(i); } @@ -76,25 +66,13 @@ void MultiDimInter::pushBack(const Interval& i) // Operators ------------------------------------------------------------------- -Interval& MultiDimInter::operator[](std::size_t n) -{ - return _intervals[n]; -} +Interval& MultiDimInter::operator[](std::size_t n) { return _intervals[n]; } -const Interval& MultiDimInter::operator[](std::size_t n) const -{ - return _intervals[n]; -} +const Interval& MultiDimInter::operator[](std::size_t n) const { return _intervals[n]; } -bool MultiDimInter::operator==(const MultiDimInter& other) const -{ - return _intervals == other._intervals; -} +bool MultiDimInter::operator==(const MultiDimInter& other) const { return _intervals == other._intervals; } -bool MultiDimInter::operator!=(const MultiDimInter& other) const -{ - return !(*this == other); -} +bool MultiDimInter::operator!=(const MultiDimInter& other) const { return !(*this == other); } bool MultiDimInter::operator<(const MultiDimInter& other) const { @@ -109,10 +87,7 @@ bool MultiDimInter::operator<(const MultiDimInter& other) const return minElem() < other.minElem(); } -bool MultiDimInter::operator>(const MultiDimInter& other) const -{ - return !(*this == other && *this < other); -} +bool MultiDimInter::operator>(const MultiDimInter& other) const { return !(*this == other && *this < other); } std::ostream& operator<<(std::ostream& out, const MultiDimInter& mdi) { @@ -122,7 +97,7 @@ std::ostream& operator<<(std::ostream& out, const MultiDimInter& mdi) for (std::size_t j = 0; j < sz - 1; ++j) { out << mdi[j] << "x"; } - out << mdi[sz-1]; + out << mdi[sz - 1]; } return out; @@ -190,7 +165,7 @@ MultiDimInter MultiDimInter::cartesianProduct(const MultiDimInter& other) const MultiDimInter result = *this; for (const Interval& other_i : other._intervals) { - result.pushBack(other_i); + result.pushBack(other_i); } return result; @@ -211,15 +186,9 @@ MultiDimInter MultiDimInter::translate(const IntTuple& t) const return result; } -MultiDimInter MultiDimInter::least(const MultiDimInter& other) const -{ - return std::min(*this, other); -} +MultiDimInter MultiDimInter::least(const MultiDimInter& other) const { return std::min(*this, other); } -Perimeter MultiDimInter::perimeter() const -{ - return Perimeter{minElem(), maxElem()}; -} +Perimeter MultiDimInter::perimeter() const { return Perimeter{minElem(), maxElem()}; } MaybeMDI MultiDimInter::compact(const MultiDimInter& other) const { @@ -256,8 +225,7 @@ MaybeMDI MultiDimInter::compact(const MultiDimInter& other) const // Non-member functions -------------------------------------------------------- -rapidjson::Value toJSON(MultiDimInter mdi - , rapidjson::Document::AllocatorType& alloc) +rapidjson::Value toJSON(MultiDimInter mdi, rapidjson::Document::AllocatorType& alloc) { rapidjson::Value interval_array{rapidjson::kArrayType}; for (const Interval& i : mdi) { @@ -270,8 +238,8 @@ rapidjson::Value toJSON(MultiDimInter mdi return result; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/ord_pwmap.cpp b/sbg/ord_pwmap.cpp index 1e12cbbf..1ebe7da6 100644 --- a/sbg/ord_pwmap.cpp +++ b/sbg/ord_pwmap.cpp @@ -37,39 +37,38 @@ namespace detail { // Auxiliary definitions ------------------------------------------------------- class MapLess { -public: - bool operator()(const Map& a, const Map& b) const { - return a.domain().minElem() < b.domain().minElem(); - } + public: + bool operator()(const Map& a, const Map& b) const { return a.domain().minElem() < b.domain().minElem(); } }; // Constructors/Destructors ---------------------------------------------------- OrdPWMap::OrdPWMap() : _pieces() {} -OrdPWMap::OrdPWMap(const Set& s) : _pieces() { +OrdPWMap::OrdPWMap(const Set& s) : _pieces() +{ if (!s.isEmpty()) { _pieces.emplace_back(Map{s, Expression{s.arity(), 1, 0}}); } } -OrdPWMap::OrdPWMap(const Map& m) : _pieces() { +OrdPWMap::OrdPWMap(const Map& m) : _pieces() +{ if (!m.domain().isEmpty()) { _pieces.emplace_back(m); } } -OrdPWMap::OrdPWMap(const std::vector& pieces) : _pieces() { +OrdPWMap::OrdPWMap(const std::vector& pieces) : _pieces() +{ for (const Map& m : pieces) { insert(m); } } -OrdPWMap::OrdPWMap(const OrdPWMap::OrdMapCollection& pieces) - : _pieces(pieces) {} +OrdPWMap::OrdPWMap(const OrdPWMap::OrdMapCollection& pieces) : _pieces(pieces) {} -OrdPWMap::OrdPWMap(OrdPWMap::OrdMapCollection&& pieces) - : _pieces(std::move(pieces)) {} +OrdPWMap::OrdPWMap(OrdPWMap::OrdMapCollection&& pieces) : _pieces(std::move(pieces)) {} // Getters --------------------------------------------------------------------- @@ -79,16 +78,13 @@ OrdPWMap::ConstIt OrdPWMap::end() const { return _pieces.end(); } // Setters --------------------------------------------------------------------- -void OrdPWMap::insert(const Map& m) -{ - insert(Map{m}); -} +void OrdPWMap::insert(const Map& m) { insert(Map{m}); } void OrdPWMap::insert(Map&& m) { if (!m.isEmpty()) { MapEntry entry{m}; - if (isEmpty() || _pieces.back() < entry) { + if (isEmpty() || _pieces.back() < entry) { _pieces.push_back(entry); } else { insertHint(0, std::move(m)); @@ -96,10 +92,7 @@ void OrdPWMap::insert(Map&& m) } } -void OrdPWMap::pushBack(const Map& m) -{ - pushBack(Map{m}); -} +void OrdPWMap::pushBack(const Map& m) { pushBack(Map{m}); } void OrdPWMap::pushBack(Map&& m) { @@ -108,10 +101,7 @@ void OrdPWMap::pushBack(Map&& m) } } -void OrdPWMap::pushBack(const MapEntry& entry) -{ - pushBack(MapEntry{entry}); -} +void OrdPWMap::pushBack(const MapEntry& entry) { pushBack(MapEntry{entry}); } void OrdPWMap::pushBack(MapEntry&& entry) { @@ -120,10 +110,7 @@ void OrdPWMap::pushBack(MapEntry&& entry) } } -void OrdPWMap::insertHint(std::size_t hint, const Map& m) -{ - insertHint(hint, Map{m}); -} +void OrdPWMap::insertHint(std::size_t hint, const Map& m) { insertHint(hint, Map{m}); } void OrdPWMap::insertHint(std::size_t hint, Map&& m) { @@ -162,14 +149,14 @@ std::size_t OrdPWMap::advanceHint(std::size_t hint, const MapEntry& jth_entry) // Traverse -------------------------------------------------------------------- -template +template Core OrdPWMap::traverse(const OrdPWMap& other, Core core_op) const { OrdMapCollection short_pw = _pieces; OrdMapCollection long_pw = other._pieces; if (!core_op.orderMatters() && long_pw.size() < short_pw.size()) { short_pw = other._pieces; - long_pw = _pieces; + long_pw = _pieces; } // Indexes list corresponding to remaining maps in short_pw @@ -182,16 +169,16 @@ Core OrdPWMap::traverse(const OrdPWMap& other, Core core_op) const auto short_begin = short_pw.begin(); for (const MapEntry& long_mpe : long_pw) { const Perimeter& long_perimeter = long_mpe.perimeter(); - + auto prev_index = indexes.before_begin(); auto curr_index = indexes.begin(); while (curr_index != indexes.end()) { size_t idx = *curr_index; const MapEntry& short_mpe = *(short_begin + idx); - const Perimeter& short_perimeter = short_mpe.perimeter(); - + const Perimeter& short_perimeter = short_mpe.perimeter(); + // Here short_map is "before" long_map, so it is also "before" all the - // remaining sets in long_pw, thus it can be discarded. + // remaining sets in long_pw, thus it can be discarded. if (short_perimeter.max() < long_perimeter.min()) { curr_index = indexes.erase_after(prev_index); continue; @@ -199,7 +186,7 @@ Core OrdPWMap::traverse(const OrdPWMap& other, Core core_op) const // Here short_map is "after" long_map, so no comparison is needed, and // the loop of long_pw continues to check if this short_map interacts - // with the following elements of long_pw. + // with the following elements of long_pw. if (long_perimeter.max() < short_perimeter.min()) { break; } @@ -225,8 +212,9 @@ Core OrdPWMap::traverse(const OrdPWMap& other, Core core_op) const // Operators ------------------------------------------------------------------- class EqualCore { -public: - bool operator()(const MapEntry& entry1, const MapEntry& entry2) { + public: + bool operator()(const MapEntry& entry1, const MapEntry& entry2) + { Map m1 = entry1.map(); Map m2 = entry2.map(); Set cap_domain = m1.domain().intersection(m2.domain()); @@ -254,11 +242,11 @@ class EqualCore { bool result() const { return _are_equal; } -private: + private: bool _are_equal = true; }; -bool OrdPWMap::operator==(const OrdPWMap& other) const +bool OrdPWMap::operator==(const OrdPWMap& other) const { if (domain() != other.domain()) { return false; @@ -271,14 +259,12 @@ bool OrdPWMap::operator==(const OrdPWMap& other) const return traverse(other, EqualCore{}).result(); } -bool OrdPWMap::operator!=(const OrdPWMap& other) const -{ - return !(*this == other); -} +bool OrdPWMap::operator!=(const OrdPWMap& other) const { return !(*this == other); } class AddCore { -public: - bool operator()(const MapEntry& entry1, const MapEntry& entry2) { + public: + bool operator()(const MapEntry& entry1, const MapEntry& entry2) + { Map added = entry1.map() + entry2.map(); _global_pos = _result.advanceHint(_global_pos, entry2); _result.insertHint(_global_pos, added); @@ -289,9 +275,9 @@ class AddCore { OrdPWMap result() const { return _result; } -private: + private: OrdPWMap _result; - std::size_t _global_pos = 0; + std::size_t _global_pos = 0; }; OrdPWMap OrdPWMap::operator+(const OrdPWMap& other) const @@ -333,7 +319,7 @@ std::size_t OrdPWMap::arity() const bool OrdPWMap::isEmpty() const { return _pieces.empty(); } -Set OrdPWMap::domain() const & +Set OrdPWMap::domain() const& { Set result; @@ -357,7 +343,6 @@ Set OrdPWMap::domain() && OrdPWMap OrdPWMap::restrict(const Set& subdom) const { - if (subdom.isEmpty() || isEmpty()) { return OrdPWMap{}; } @@ -373,9 +358,9 @@ OrdPWMap OrdPWMap::restrict(const Set& subdom) const result.insertHint(global_pos, entry.map().restrict(subdom)); continue; } - + // No possible intersection between the subdom and the remaining elements - // of _pieces. + // of _pieces. if (subdom_max_perimeter < entry_perimeter.min()) { break; } @@ -395,10 +380,7 @@ Set OrdPWMap::image() const return result; } -Set OrdPWMap::image(const Set& subdom) const -{ - return restrict(subdom).image(); -} +Set OrdPWMap::image(const Set& subdom) const { return restrict(subdom).image(); } Set OrdPWMap::preImage(const Set& subcodom) const { @@ -430,7 +412,7 @@ OrdPWMap OrdPWMap::composition(const OrdPWMap& other) const for (const MapEntry& other_entry : other._pieces) { Map other_map = other_entry.map(); Set img = other_map.image(); - + Perimeter img_perimeter = img.perimeter(); result.advanceHint(global_pos, other_entry); IntTuple img_max_perimeter = img_perimeter.max(); @@ -444,7 +426,7 @@ OrdPWMap OrdPWMap::composition(const OrdPWMap& other) const } // No possible intersection between the current image and the remaining - // elements of _pieces. + // elements of _pieces. if (img_max_perimeter < entry_perimeter.min()) { break; } @@ -490,25 +472,16 @@ Set OrdPWMap::fixedPoints() const // Extra operations ------------------------------------------------------------ -OrdPWMap OrdPWMap::concatenation(const OrdPWMap& other) const & -{ - return OrdPWMap{*this}.concatenation(other); -} +OrdPWMap OrdPWMap::concatenation(const OrdPWMap& other) const& { return OrdPWMap{*this}.concatenation(other); } -OrdPWMap OrdPWMap::concatenation(const OrdPWMap& other) && -{ - return std::move(*this).concatenation(OrdPWMap{other}); -} +OrdPWMap OrdPWMap::concatenation(const OrdPWMap& other) && { return std::move(*this).concatenation(OrdPWMap{other}); } -OrdPWMap OrdPWMap::concatenation(OrdPWMap&& other) const & -{ - return OrdPWMap{*this}.concatenation(std::move(other)); -} +OrdPWMap OrdPWMap::concatenation(OrdPWMap&& other) const& { return OrdPWMap{*this}.concatenation(std::move(other)); } OrdPWMap OrdPWMap::concatenation(OrdPWMap&& other) && { // Special cases - if (isEmpty()) { + if (isEmpty()) { return std::move(other); } @@ -518,16 +491,12 @@ OrdPWMap OrdPWMap::concatenation(OrdPWMap&& other) && OrdMapCollection result; if (_pieces.back() < other._pieces.front()) { - result.insert(result.end(), std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(_pieces.end())); - result.insert(result.end(), std::make_move_iterator(other._pieces.begin()) - , std::make_move_iterator(other._pieces.end())); + result.insert(result.end(), std::make_move_iterator(_pieces.begin()), std::make_move_iterator(_pieces.end())); + result.insert(result.end(), std::make_move_iterator(other._pieces.begin()), std::make_move_iterator(other._pieces.end())); } else if (other._pieces.back() < _pieces.front()) { - result.insert(result.end(), std::make_move_iterator(other._pieces.begin()) - , std::make_move_iterator(other._pieces.end())); - result.insert(result.end(), std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(_pieces.end())); - } else { + result.insert(result.end(), std::make_move_iterator(other._pieces.begin()), std::make_move_iterator(other._pieces.end())); + result.insert(result.end(), std::make_move_iterator(_pieces.begin()), std::make_move_iterator(_pieces.end())); + } else { // General case auto it1 = _pieces.begin(); auto end1 = _pieces.end(); @@ -542,31 +511,20 @@ OrdPWMap OrdPWMap::concatenation(OrdPWMap&& other) && ++it2; } } - result.insert(result.end(), std::make_move_iterator(it1) - , std::make_move_iterator(end1)); - result.insert(result.end(), std::make_move_iterator(it2) - , std::make_move_iterator(end2)); + result.insert(result.end(), std::make_move_iterator(it1), std::make_move_iterator(end1)); + result.insert(result.end(), std::make_move_iterator(it2), std::make_move_iterator(end2)); } return OrdPWMap{std::move(result)}; } -OrdPWMap OrdPWMap::combine(const OrdPWMap& other) const & -{ - return OrdPWMap{*this}.combine(other); -} +OrdPWMap OrdPWMap::combine(const OrdPWMap& other) const& { return OrdPWMap{*this}.combine(other); } -OrdPWMap OrdPWMap::combine(const OrdPWMap& other) && -{ - return std::move(*this).combine(OrdPWMap{other}); -} +OrdPWMap OrdPWMap::combine(const OrdPWMap& other) && { return std::move(*this).combine(OrdPWMap{other}); } -OrdPWMap OrdPWMap::combine(OrdPWMap&& other) const & -{ - return OrdPWMap{*this}.combine(std::move(other)); -} +OrdPWMap OrdPWMap::combine(OrdPWMap&& other) const& { return OrdPWMap{*this}.combine(std::move(other)); } -OrdPWMap OrdPWMap::combine(OrdPWMap&& other) && +OrdPWMap OrdPWMap::combine(OrdPWMap&& other) && { if (isEmpty()) { return std::move(other); @@ -605,7 +563,7 @@ OrdPWMap OrdPWMap::min(const OrdPWMap& other) const } Set min_in_pw1 = lessImage(other); - return restrict(min_in_pw1).combine(other.restrict(domain())); + return restrict(min_in_pw1).combine(other.restrict(domain())); } OrdPWMap OrdPWMap::max(const OrdPWMap& other) const @@ -620,19 +578,19 @@ OrdPWMap OrdPWMap::max(const OrdPWMap& other) const } class MinAdjCore { -public: - bool operator()(const MapEntry& entry1, const MapEntry& entry2) { + public: + bool operator()(const MapEntry& entry1, const MapEntry& entry2) + { Map min_adj = entry1.map().minAdj(entry2.map()); if (!min_adj.isEmpty()) { Set min_adj_domain = min_adj.domain(); Set repeated = min_adj_domain.intersection(_visited); if (!repeated.isEmpty()) { OrdPWMap min_adj_pw{std::move(min_adj)}; - OrdPWMap min_adj_repeated = min_adj_pw.restrict(repeated); + OrdPWMap min_adj_repeated = min_adj_pw.restrict(repeated); OrdPWMap result_repeated = _result.restrict(repeated); OrdPWMap min_map = min_adj_repeated.min(result_repeated); - OrdPWMap min_adj_result = std::move(min_map).combine( - std::move(min_adj_pw)); + OrdPWMap min_adj_result = std::move(min_map).combine(std::move(min_adj_pw)); _result = std::move(min_adj_result).combine(std::move(_result)); _visited = std::move(_visited).cup(std::move(min_adj_domain)); } else { @@ -648,7 +606,7 @@ class MinAdjCore { OrdPWMap result() const { return _result; } -private: + private: OrdPWMap _result; Set _visited; }; @@ -663,19 +621,19 @@ OrdPWMap OrdPWMap::minAdj(const OrdPWMap& other) const } class MaxAdjCore { -public: - bool operator()(const MapEntry& entry1, const MapEntry& entry2) { + public: + bool operator()(const MapEntry& entry1, const MapEntry& entry2) + { Map max_adj = entry1.map().maxAdj(entry2.map()); if (!max_adj.isEmpty()) { Set max_adj_domain = max_adj.domain(); Set repeated = max_adj_domain.intersection(_visited); if (!repeated.isEmpty()) { OrdPWMap max_adj_pw{std::move(max_adj)}; - OrdPWMap max_adj_repeated = max_adj_pw.restrict(repeated); + OrdPWMap max_adj_repeated = max_adj_pw.restrict(repeated); OrdPWMap result_repeated = _result.restrict(repeated); OrdPWMap max_map = max_adj_repeated.max(result_repeated); - OrdPWMap max_adj_result = std::move(max_map).combine( - std::move(max_adj_pw)); + OrdPWMap max_adj_result = std::move(max_map).combine(std::move(max_adj_pw)); _result = std::move(max_adj_result).combine(std::move(_result)); _visited = std::move(_visited).cup(std::move(max_adj_domain)); } else { @@ -691,7 +649,7 @@ class MaxAdjCore { OrdPWMap result() const { return _result; } -private: + private: OrdPWMap _result; Set _visited; }; @@ -706,8 +664,9 @@ OrdPWMap OrdPWMap::maxAdj(const OrdPWMap& other) const } class EqualImageCore { -public: - bool operator()(const MapEntry& entry1, const MapEntry& entry2) { + public: + bool operator()(const MapEntry& entry1, const MapEntry& entry2) + { Map m1 = entry1.map(); Map m2 = entry2.map(); Set cap_dom = m1.domain().intersection(m2.domain()); @@ -726,7 +685,7 @@ class EqualImageCore { Set result() const { return _result; } -private: + private: Set _result; }; @@ -744,8 +703,9 @@ Set OrdPWMap::equalImage(const OrdPWMap& other) const } class LessImageCore { -public: - bool operator()(const MapEntry& entry1, const MapEntry& entry2) { + public: + bool operator()(const MapEntry& entry1, const MapEntry& entry2) + { Set less_map = entry1.map().lessImage(entry2.map()); _result = std::move(_result).disjointCup(std::move(less_map)); @@ -756,7 +716,7 @@ class LessImageCore { Set result() const { return _result; } -private: + private: Set _result; }; @@ -771,7 +731,7 @@ Set OrdPWMap::lessImage(const OrdPWMap& other) const } return traverse(other, LessImageCore{}).result(); -} +} OrdPWMap OrdPWMap::imageMultiplicity() const { @@ -785,7 +745,7 @@ OrdPWMap OrdPWMap::imageMultiplicity() const } return result; -} +} void OrdPWMap::compact() { @@ -851,9 +811,8 @@ rapidjson::Value toJSON(OrdPWMap pw, rapidjson::Document::AllocatorType& alloc) return result; } -} // namespace detail - -} // namespace LIB +} // namespace detail -} // namespace SBG; +} // namespace LIB +} // namespace SBG diff --git a/sbg/ord_set.cpp b/sbg/ord_set.cpp index 9b2dcb68..252676f6 100644 --- a/sbg/ord_set.cpp +++ b/sbg/ord_set.cpp @@ -54,7 +54,6 @@ bool overlap(const MultiDimInter& mdi1, const MultiDimInter& mdi2) return true; } - //////////////////////////////////////////////////////////////////////////////// // Ordered Set Implementation -------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// @@ -63,10 +62,7 @@ bool overlap(const MultiDimInter& mdi1, const MultiDimInter& mdi2) OrderedSet::OrderedSet() : _pieces() {} -OrderedSet::OrderedSet(const IntTuple& x) : _pieces() -{ - _pieces.push_back(MultiDimInter{x}); -} +OrderedSet::OrderedSet(const IntTuple& x) : _pieces() { _pieces.push_back(MultiDimInter{x}); } OrderedSet::OrderedSet(const Interval& i) : _pieces() { @@ -82,11 +78,9 @@ OrderedSet::OrderedSet(const MultiDimInter& mdi) : _pieces() } } -OrderedSet::OrderedSet(const OrderedSet::OrdMDICollection& pieces) - : _pieces(pieces) {} +OrderedSet::OrderedSet(const OrderedSet::OrdMDICollection& pieces) : _pieces(pieces) {} -OrderedSet::OrderedSet(OrderedSet::OrdMDICollection&& pieces) - : _pieces(std::move(pieces)) {} +OrderedSet::OrderedSet(OrderedSet::OrdMDICollection&& pieces) : _pieces(std::move(pieces)) {} OrderedSet::OrderedSet(const FixedPointsInfo& info) : _pieces() { @@ -143,7 +137,7 @@ Int OrderedSet::advanceHint(Int hint, const MultiDimInter& mdi) } ++hint; } - + return hint; } @@ -161,7 +155,7 @@ void OrderedSet::insertHint(const Int hint, const MultiDimInter& mdi) auto it = _pieces.begin(); auto end = _pieces.end(); std::advance(it, hint); - for (; it != end; ++it) { + for (; it != end; ++it) { if (mdi < *it) { break; } @@ -188,10 +182,7 @@ bool OrderedSet::operator==(const OrderedSet& other) const return difference(other).isEmpty() && other.difference(*this).isEmpty(); } -bool OrderedSet::operator!=(const OrderedSet& other) const -{ - return !(*this == other); -} +bool OrderedSet::operator!=(const OrderedSet& other) const { return !(*this == other); } std::ostream& OrderedSet::print(std::ostream& out) const { @@ -200,9 +191,9 @@ std::ostream& OrderedSet::print(std::ostream& out) const out << "{"; if (sz > 0) { unsigned int j = 0; - for (const MultiDimInter& mdi : _pieces) { + for (const MultiDimInter& mdi : _pieces) { if (j < sz - 1) { - out << mdi << ", "; + out << mdi << ", "; } else { out << mdi; } @@ -230,10 +221,7 @@ unsigned int OrderedSet::cardinal() const bool OrderedSet::isEmpty() const { return _pieces.empty(); } -IntTuple OrderedSet::minElem() const -{ - return _pieces.begin()->minElem(); -} +IntTuple OrderedSet::minElem() const { return _pieces.begin()->minElem(); } IntTuple OrderedSet::maxElem() const { @@ -242,12 +230,11 @@ IntTuple OrderedSet::maxElem() const for (const MultiDimInter& mdi : _pieces) { result = std::max(result, mdi.maxElem()); } - + return result; } -OrderedSet OrderedSet::intersectionEpilogue(const OrderedSet& lhs - , const OrderedSet& rhs) const +OrderedSet OrderedSet::intersectionEpilogue(const OrderedSet& lhs, const OrderedSet& rhs) const { OrderedSet result; @@ -256,10 +243,10 @@ OrderedSet OrderedSet::intersectionEpilogue(const OrderedSet& lhs OrderedSet::OrdMDICollection long_set = rhs._pieces; if (rhs._pieces.size() < lhs._pieces.size()) { short_set = rhs._pieces; - long_set = lhs._pieces; + long_set = lhs._pieces; } - - // Indexes list corresponding to remaining pieces in short_set + + // Indexes list corresponding to remaining pieces in short_set std::forward_list indexes; int short_size = short_set.size(); for (int i = short_size - 1; i >= 0; --i) { @@ -282,7 +269,7 @@ OrderedSet OrderedSet::intersectionEpilogue(const OrderedSet& lhs const auto short_max = short_elem.maxElem(); // Here short_elem is "before" long_elem, so it is also "before" all the - // remaining sets in long_set, thus it can be discarded. + // remaining sets in long_set, thus it can be discarded. if (short_max < long_min) { curr_index = indexes.erase_after(prev_index); continue; @@ -290,7 +277,7 @@ OrderedSet OrderedSet::intersectionEpilogue(const OrderedSet& lhs // Here short_elem is "after" long_elem, so no comparison is needed, and // the loop of long_set continues to check if this short_elem interacts - // with the following elements of long_set. + // with the following elements of long_set. if (long_max < short_min) { break; } @@ -325,24 +312,15 @@ OrderedSet OrderedSet::intersection(const OrderedSet& other) const return intersectionEpilogue(*this, other); } -OrderedSet OrderedSet::cup(const OrderedSet& other) const & -{ - return OrderedSet{*this}.cup(other); -} +OrderedSet OrderedSet::cup(const OrderedSet& other) const& { return OrderedSet{*this}.cup(other); } -OrderedSet OrderedSet::cup(const OrderedSet& other) && -{ - return std::move(*this).cup(OrderedSet{other}); -} +OrderedSet OrderedSet::cup(const OrderedSet& other) && { return std::move(*this).cup(OrderedSet{other}); } -OrderedSet OrderedSet::cup(OrderedSet&& other) const & -{ - return OrderedSet{*this}.cup(std::move(other)); -} +OrderedSet OrderedSet::cup(OrderedSet&& other) const& { return OrderedSet{*this}.cup(std::move(other)); } OrderedSet OrderedSet::cup(OrderedSet&& other) && { - if (isEmpty()) { + if (isEmpty()) { return std::move(other); } @@ -352,15 +330,13 @@ OrderedSet OrderedSet::cup(OrderedSet&& other) && if (maxElem() < other.minElem()) { OrdMDICollection result = std::move(_pieces); - result.insert(result.end(), std::make_move_iterator(other._pieces.begin()) - , std::make_move_iterator(other._pieces.end())); + result.insert(result.end(), std::make_move_iterator(other._pieces.begin()), std::make_move_iterator(other._pieces.end())); return OrderedSet{std::move(result)}; } if (other.maxElem() < minElem()) { OrdMDICollection result = std::move(other._pieces); - result.insert(result.end(), std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(_pieces.end())); + result.insert(result.end(), std::make_move_iterator(_pieces.begin()), std::make_move_iterator(_pieces.end())); return OrderedSet{std::move(result)}; } @@ -384,7 +360,7 @@ OrderedSet OrderedSet::complementAtom() const std::size_t dim = 0; std::size_t global_position = 0; for (const Interval& i : mdi) { - std::size_t local_pos = global_position; + std::size_t local_pos = global_position; // Before interval if (i.begin() != kNegInf) { Interval i_res{0, 1, i.begin() - 1}; @@ -427,8 +403,7 @@ OrderedSet OrderedSet::complementAtom() const return OrderedSet{std::move(result)}; } -void OrderedSet::intersectionComplement(const OrderedSet& - local_complement, const MultiDimInter& mdi) +void OrderedSet::intersectionComplement(const OrderedSet& local_complement, const MultiDimInter& mdi) { // Special cases if (isEmpty()) { @@ -447,9 +422,8 @@ void OrderedSet::intersectionComplement(const OrderedSet& break; } ++it; - } - result._pieces.insert(result.end(), std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(it)); + } + result._pieces.insert(result.end(), std::make_move_iterator(_pieces.begin()), std::make_move_iterator(it)); Int global_position = 0; for (; it != _pieces.end(); ++it) { @@ -537,25 +511,16 @@ std::size_t OrderedSet::arity() const return _pieces.begin()->arity(); } -OrderedSet OrderedSet::disjointCup(const OrderedSet& other) const & -{ - return OrderedSet{*this}.disjointCup(other); -} +OrderedSet OrderedSet::disjointCup(const OrderedSet& other) const& { return OrderedSet{*this}.disjointCup(other); } -OrderedSet OrderedSet::disjointCup(const OrderedSet& other) && -{ - return std::move(*this).disjointCup(OrderedSet{other}); -} +OrderedSet OrderedSet::disjointCup(const OrderedSet& other) && { return std::move(*this).disjointCup(OrderedSet{other}); } -OrderedSet OrderedSet::disjointCup(OrderedSet&& other) const & -{ - return OrderedSet{*this}.disjointCup(std::move(other)); -} +OrderedSet OrderedSet::disjointCup(OrderedSet&& other) const& { return OrderedSet{*this}.disjointCup(std::move(other)); } OrderedSet OrderedSet::disjointCup(OrderedSet&& other) && { // Special cases - if (isEmpty()) { + if (isEmpty()) { return std::move(other); } @@ -565,16 +530,12 @@ OrderedSet OrderedSet::disjointCup(OrderedSet&& other) && OrdMDICollection result; if (_pieces.back() < other._pieces.front()) { - result.insert(result.end(), std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(_pieces.end())); - result.insert(result.end(), std::make_move_iterator(other._pieces.begin()) - , std::make_move_iterator(other._pieces.end())); + result.insert(result.end(), std::make_move_iterator(_pieces.begin()), std::make_move_iterator(_pieces.end())); + result.insert(result.end(), std::make_move_iterator(other._pieces.begin()), std::make_move_iterator(other._pieces.end())); } else if (other._pieces.back() < _pieces.front()) { - result.insert(result.end(), std::make_move_iterator(other._pieces.begin()) - , std::make_move_iterator(other._pieces.end())); - result.insert(result.end(), std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(_pieces.end())); - } else { + result.insert(result.end(), std::make_move_iterator(other._pieces.begin()), std::make_move_iterator(other._pieces.end())); + result.insert(result.end(), std::make_move_iterator(_pieces.begin()), std::make_move_iterator(_pieces.end())); + } else { // General case auto it1 = _pieces.begin(); auto end1 = _pieces.end(); @@ -589,10 +550,8 @@ OrderedSet OrderedSet::disjointCup(OrderedSet&& other) && ++it2; } } - result.insert(result.end(), std::make_move_iterator(it1) - , std::make_move_iterator(end1)); - result.insert(result.end(), std::make_move_iterator(it2) - , std::make_move_iterator(end2)); + result.insert(result.end(), std::make_move_iterator(it1), std::make_move_iterator(end1)); + result.insert(result.end(), std::make_move_iterator(it2), std::make_move_iterator(end2)); } return OrderedSet{std::move(result)}; @@ -638,8 +597,7 @@ void OrderedSet::compact() OrdMDICollection result; if (!isEmpty()) { - MDISet set_result{std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(_pieces.end())}; + MDISet set_result{std::make_move_iterator(_pieces.begin()), std::make_move_iterator(_pieces.end())}; MDISet to_erase; do { MDISet new_set_result; @@ -690,8 +648,8 @@ rapidjson::Value toJSON(OrderedSet s, rapidjson::Document::AllocatorType& alloc) return result; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/ord_unidim_dense_set.cpp b/sbg/ord_unidim_dense_set.cpp index 6b0cdf28..457492f1 100644 --- a/sbg/ord_unidim_dense_set.cpp +++ b/sbg/ord_unidim_dense_set.cpp @@ -32,25 +32,16 @@ namespace detail { // Auxiliary functions --------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// -Interval least(const Interval& lhs, const Interval& rhs) -{ - return lhs < rhs ? lhs : rhs; -} +Interval least(const Interval& lhs, const Interval& rhs) { return lhs < rhs ? lhs : rhs; } -bool overlap(const OrdUnidimDenseSet::OrdIntervalCollection& lhs, - const OrdUnidimDenseSet::OrdIntervalCollection& rhs) +bool overlap(const OrdUnidimDenseSet::OrdIntervalCollection& lhs, const OrdUnidimDenseSet::OrdIntervalCollection& rhs) { - return lhs.back().end() >= rhs.front().begin() - || rhs.back().end() >= lhs.front().begin(); + return lhs.back().end() >= rhs.front().begin() || rhs.back().end() >= lhs.front().begin(); } -Interval intersection(const Interval& lhs, const Interval& rhs) -{ - return lhs.intersection(rhs); -} +Interval intersection(const Interval& lhs, const Interval& rhs) { return lhs.intersection(rhs); } -OrdUnidimDenseSet::OrdIntervalCollection - compact(const OrdUnidimDenseSet::OrdIntervalCollection& c) +OrdUnidimDenseSet::OrdIntervalCollection compact(const OrdUnidimDenseSet::OrdIntervalCollection& c) { OrdUnidimDenseSet::OrdIntervalCollection result; @@ -85,10 +76,7 @@ OrdUnidimDenseSet::OrdIntervalCollection OrdUnidimDenseSet::OrdUnidimDenseSet() : _pieces() {} -OrdUnidimDenseSet::OrdUnidimDenseSet(const Int x) : _pieces() -{ - _pieces.emplace_back(x); -} +OrdUnidimDenseSet::OrdUnidimDenseSet(const Int x) : _pieces() { _pieces.emplace_back(x); } OrdUnidimDenseSet::OrdUnidimDenseSet(const Interval& i) : _pieces() { @@ -97,13 +85,9 @@ OrdUnidimDenseSet::OrdUnidimDenseSet(const Interval& i) : _pieces() } } -OrdUnidimDenseSet::OrdUnidimDenseSet( - const OrdUnidimDenseSet::OrdIntervalCollection& pieces) - : _pieces(pieces) {} +OrdUnidimDenseSet::OrdUnidimDenseSet(const OrdUnidimDenseSet::OrdIntervalCollection& pieces) : _pieces(pieces) {} -OrdUnidimDenseSet::OrdUnidimDenseSet( - OrdUnidimDenseSet::OrdIntervalCollection&& pieces) - : _pieces(std::move(pieces)) {} +OrdUnidimDenseSet::OrdUnidimDenseSet(OrdUnidimDenseSet::OrdIntervalCollection&& pieces) : _pieces(std::move(pieces)) {} OrdUnidimDenseSet::OrdUnidimDenseSet(const FixedPointsInfo& info) { @@ -121,15 +105,9 @@ OrdUnidimDenseSet::OrdUnidimDenseSet(const FixedPointsInfo& info) // Getters --------------------------------------------------------------------- -OrdUnidimDenseSet::ConstIt OrdUnidimDenseSet::begin() const -{ - return _pieces.begin(); -} +OrdUnidimDenseSet::ConstIt OrdUnidimDenseSet::begin() const { return _pieces.begin(); } -OrdUnidimDenseSet::ConstIt OrdUnidimDenseSet::end() const -{ - return _pieces.end(); -} +OrdUnidimDenseSet::ConstIt OrdUnidimDenseSet::end() const { return _pieces.end(); } // Setters --------------------------------------------------------------------- @@ -178,10 +156,7 @@ bool OrdUnidimDenseSet::operator==(const OrdUnidimDenseSet& other) const return detail::compact(_pieces) == detail::compact(other._pieces); } -bool OrdUnidimDenseSet::operator!=(const OrdUnidimDenseSet& other) const -{ - return !(*this == other); -} +bool OrdUnidimDenseSet::operator!=(const OrdUnidimDenseSet& other) const { return !(*this == other); } std::ostream& OrdUnidimDenseSet::print(std::ostream& out) const { @@ -190,7 +165,7 @@ std::ostream& OrdUnidimDenseSet::print(std::ostream& out) const out << "{"; if (sz > 0) { unsigned int j = 0; - for (const Interval& i : _pieces) { + for (const Interval& i : _pieces) { if (j < sz - 1) { out << i << ", "; } else { @@ -220,18 +195,11 @@ unsigned int OrdUnidimDenseSet::cardinal() const bool OrdUnidimDenseSet::isEmpty() const { return _pieces.empty(); } -IntTuple OrdUnidimDenseSet::minElem() const -{ - return IntTuple(_pieces.front().begin()); -} +IntTuple OrdUnidimDenseSet::minElem() const { return IntTuple(_pieces.front().begin()); } -IntTuple OrdUnidimDenseSet::maxElem() const -{ - return IntTuple(_pieces.back().end()); -} +IntTuple OrdUnidimDenseSet::maxElem() const { return IntTuple(_pieces.back().end()); } -OrdUnidimDenseSet OrdUnidimDenseSet::intersection(const OrdUnidimDenseSet& - other) const +OrdUnidimDenseSet OrdUnidimDenseSet::intersection(const OrdUnidimDenseSet& other) const { // Special cases to enhance performance if (isEmpty() || other.isEmpty()) { @@ -249,29 +217,20 @@ OrdUnidimDenseSet OrdUnidimDenseSet::intersection(const OrdUnidimDenseSet& return boundedTraverse(detail::intersection, other); } -OrdUnidimDenseSet OrdUnidimDenseSet::cup(const OrdUnidimDenseSet& other) const & -{ - return OrdUnidimDenseSet{*this}.cup(other); -} +OrdUnidimDenseSet OrdUnidimDenseSet::cup(const OrdUnidimDenseSet& other) const& { return OrdUnidimDenseSet{*this}.cup(other); } -OrdUnidimDenseSet OrdUnidimDenseSet::cup(const OrdUnidimDenseSet& other) && -{ - return std::move(*this).cup(OrdUnidimDenseSet{other}); -} +OrdUnidimDenseSet OrdUnidimDenseSet::cup(const OrdUnidimDenseSet& other) && { return std::move(*this).cup(OrdUnidimDenseSet{other}); } -OrdUnidimDenseSet OrdUnidimDenseSet::cup(OrdUnidimDenseSet&& other) const & -{ - return OrdUnidimDenseSet{*this}.cup(std::move(other)); -} +OrdUnidimDenseSet OrdUnidimDenseSet::cup(OrdUnidimDenseSet&& other) const& { return OrdUnidimDenseSet{*this}.cup(std::move(other)); } -OrdUnidimDenseSet OrdUnidimDenseSet::cup(OrdUnidimDenseSet&& other) && -{ +OrdUnidimDenseSet OrdUnidimDenseSet::cup(OrdUnidimDenseSet&& other) && +{ // Special cases if (other.isEmpty() || _pieces == other._pieces) { return OrdUnidimDenseSet{std::move(_pieces)}; } - if (isEmpty()) { + if (isEmpty()) { return OrdUnidimDenseSet{std::move(other._pieces)}; } @@ -290,7 +249,7 @@ OrdUnidimDenseSet OrdUnidimDenseSet::complement() const OrdUnidimDenseSet result; if (isEmpty()) { - result._pieces.emplace_back(kOneDimUniverse); + result._pieces.emplace_back(kOneDimUniverse); return result; } @@ -316,8 +275,7 @@ OrdUnidimDenseSet OrdUnidimDenseSet::complement() const return result; } -OrdUnidimDenseSet OrdUnidimDenseSet::difference(const OrdUnidimDenseSet& other) - const +OrdUnidimDenseSet OrdUnidimDenseSet::difference(const OrdUnidimDenseSet& other) const { if (isEmpty() || other.isEmpty()) { return OrdUnidimDenseSet{_pieces}; @@ -334,8 +292,7 @@ OrdUnidimDenseSet OrdUnidimDenseSet::difference(const OrdUnidimDenseSet& other) return intersection(other.complement()); } -OrdUnidimDenseSet OrdUnidimDenseSet::cartesianProduct(const OrdUnidimDenseSet& - other) const +OrdUnidimDenseSet OrdUnidimDenseSet::cartesianProduct(const OrdUnidimDenseSet& other) const { Util::ERROR("OrdUnidimDenseSet::cartesianProduct: operation not supported\n"); @@ -353,20 +310,17 @@ std::size_t OrdUnidimDenseSet::arity() const return 1; } -OrdUnidimDenseSet OrdUnidimDenseSet::disjointCup(const OrdUnidimDenseSet& other) - const & +OrdUnidimDenseSet OrdUnidimDenseSet::disjointCup(const OrdUnidimDenseSet& other) const& { return OrdUnidimDenseSet{*this}.disjointCup(other); } -OrdUnidimDenseSet OrdUnidimDenseSet::disjointCup(const OrdUnidimDenseSet& other) - && +OrdUnidimDenseSet OrdUnidimDenseSet::disjointCup(const OrdUnidimDenseSet& other) && { return std::move(*this).disjointCup(OrdUnidimDenseSet{other}); } -OrdUnidimDenseSet OrdUnidimDenseSet::disjointCup(OrdUnidimDenseSet&& other) - const & +OrdUnidimDenseSet OrdUnidimDenseSet::disjointCup(OrdUnidimDenseSet&& other) const& { return OrdUnidimDenseSet{*this}.disjointCup(std::move(other)); } @@ -384,13 +338,11 @@ OrdUnidimDenseSet OrdUnidimDenseSet::disjointCup(OrdUnidimDenseSet&& other) && if (!overlap(_pieces, other._pieces)) { OrdIntervalCollection result; - result.insert(result.end(), std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(_pieces.end())); - result.insert(result.end(), std::make_move_iterator(other._pieces.begin()) - , std::make_move_iterator(other._pieces.end())); + result.insert(result.end(), std::make_move_iterator(_pieces.begin()), std::make_move_iterator(_pieces.end())); + result.insert(result.end(), std::make_move_iterator(other._pieces.begin()), std::make_move_iterator(other._pieces.end())); return result; } - + return traverse(least, other); } @@ -405,10 +357,7 @@ OrdUnidimDenseSet OrdUnidimDenseSet::translate(const IntTuple& t) const return result; } -Perimeter OrdUnidimDenseSet::perimeter() const -{ - return Perimeter{minElem(), maxElem()}; -} +Perimeter OrdUnidimDenseSet::perimeter() const { return Perimeter{minElem(), maxElem()}; } void OrdUnidimDenseSet::compact() { @@ -436,9 +385,7 @@ void OrdUnidimDenseSet::compact() _pieces = std::move(result); } -OrdUnidimDenseSet OrdUnidimDenseSet::boundedTraverse( - Interval f(const Interval&, const Interval&), const OrdUnidimDenseSet& other -) const +OrdUnidimDenseSet OrdUnidimDenseSet::boundedTraverse(Interval f(const Interval&, const Interval&), const OrdUnidimDenseSet& other) const { OrdUnidimDenseSet result; auto it1 = _pieces.begin(); @@ -452,7 +399,7 @@ OrdUnidimDenseSet OrdUnidimDenseSet::boundedTraverse( if (i1.maxElem() < i2.maxElem()) { ++it1; - } else { + } else { ++it2; } } @@ -460,9 +407,7 @@ OrdUnidimDenseSet OrdUnidimDenseSet::boundedTraverse( return result; } -OrdUnidimDenseSet OrdUnidimDenseSet::traverse( - Interval f(const Interval&, const Interval&) , const OrdUnidimDenseSet& other) - const +OrdUnidimDenseSet OrdUnidimDenseSet::traverse(Interval f(const Interval&, const Interval&), const OrdUnidimDenseSet& other) const { OrdUnidimDenseSet result; @@ -489,8 +434,7 @@ OrdUnidimDenseSet OrdUnidimDenseSet::traverse( // Non-member functions -------------------------------------------------------- -rapidjson::Value toJSON(OrdUnidimDenseSet s - , rapidjson::Document::AllocatorType& alloc) +rapidjson::Value toJSON(OrdUnidimDenseSet s, rapidjson::Document::AllocatorType& alloc) { rapidjson::Value interval_array{rapidjson::kArrayType}; for (const Interval& i : s) { @@ -506,8 +450,8 @@ rapidjson::Value toJSON(OrdUnidimDenseSet s return result; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/perimeter.cpp b/sbg/perimeter.cpp old mode 100755 new mode 100644 index d03185c2..5a915037 --- a/sbg/perimeter.cpp +++ b/sbg/perimeter.cpp @@ -27,8 +27,7 @@ namespace LIB { // Set perimeter --------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// -Perimeter::Perimeter(const IntTuple& min, const IntTuple& max) - : _min(min), _max(max) {} +Perimeter::Perimeter(const IntTuple& min, const IntTuple& max) : _min(min), _max(max) {} const IntTuple& Perimeter::min() const { return _min; } @@ -42,10 +41,10 @@ bool Perimeter::overlap(const Perimeter& other) const return false; } } - + return true; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/pw_map.cpp b/sbg/pw_map.cpp index ad54b90f..cea5d5c0 100644 --- a/sbg/pw_map.cpp +++ b/sbg/pw_map.cpp @@ -38,10 +38,8 @@ PWMap::ConstIt::ConstIt(detail::OrdPWMap::ConstIt it) : _it(it) {} const Map& PWMap::ConstIt::operator*() { - auto it_visitor = SBG::Util::Overload { - [](const detail::UnordPWMap::ConstIt& i) -> const Map& { return *i; }, - [](const detail::OrdPWMap::ConstIt& i) -> const Map& { return i->map(); } - }; + auto it_visitor = SBG::Util::Overload{[](const detail::UnordPWMap::ConstIt& i) -> const Map& { return *i; }, + [](const detail::OrdPWMap::ConstIt& i) -> const Map& { return i->map(); }}; return std::visit(it_visitor, _it); } @@ -51,15 +49,9 @@ PWMap::ConstIt PWMap::ConstIt::operator++() return *this; } -bool PWMap::ConstIt::operator==(const ConstIt& other) -{ - return _it == other._it; -} +bool PWMap::ConstIt::operator==(const ConstIt& other) { return _it == other._it; } -bool PWMap::ConstIt::operator!=(const ConstIt& other) -{ - return _it != other._it; -} +bool PWMap::ConstIt::operator!=(const ConstIt& other) { return _it != other._it; } //////////////////////////////////////////////////////////////////////////////// // PWMap ---------------------------------------------------------------------- @@ -71,25 +63,25 @@ PWMap::PWMap() : _impl() { PWMapKind kind = PWMAP_IMPL.kind(); switch (kind) { - case PWMapKind::kUnordered: { - _impl = detail::UnordPWMap{}; - break; - } - - case PWMapKind::kOrdered: { - _impl = detail::OrdPWMap{}; - break; - } - - case PWMapKind::kDomOrdered: { - _impl = detail::DomOrdPWMap{}; - break; - } - - default: { - Util::ERROR("Unsupported ", kind, " PWMap implementation\n"); - break; - } + case PWMapKind::kUnordered: { + _impl = detail::UnordPWMap{}; + break; + } + + case PWMapKind::kOrdered: { + _impl = detail::OrdPWMap{}; + break; + } + + case PWMapKind::kDomOrdered: { + _impl = detail::DomOrdPWMap{}; + break; + } + + default: { + Util::ERROR("Unsupported ", kind, " PWMap implementation\n"); + break; + } } } @@ -97,51 +89,51 @@ PWMap::PWMap(Set s) : _impl() { PWMapKind kind = PWMAP_IMPL.kind(); switch (kind) { - case PWMapKind::kUnordered: { - _impl = detail::UnordPWMap{s}; - break; - } - - case PWMapKind::kOrdered: { - _impl = detail::OrdPWMap{s}; - break; - } - - case PWMapKind::kDomOrdered: { - _impl = detail::DomOrdPWMap{s}; - break; - } - - default: { - Util::ERROR("Unsupported ", kind, " PWMap implementation\n"); - break; - } + case PWMapKind::kUnordered: { + _impl = detail::UnordPWMap{s}; + break; + } + + case PWMapKind::kOrdered: { + _impl = detail::OrdPWMap{s}; + break; + } + + case PWMapKind::kDomOrdered: { + _impl = detail::DomOrdPWMap{s}; + break; + } + + default: { + Util::ERROR("Unsupported ", kind, " PWMap implementation\n"); + break; + } } } PWMap::PWMap(Map m) : _impl() -{ +{ PWMapKind kind = PWMAP_IMPL.kind(); switch (kind) { - case PWMapKind::kUnordered: { - _impl = detail::UnordPWMap{m}; - break; - } - - case PWMapKind::kOrdered: { - _impl = detail::OrdPWMap{m}; - break; - } - - case PWMapKind::kDomOrdered: { - _impl = detail::DomOrdPWMap{m}; - break; - } - - default: { - Util::ERROR("Unsupported ", kind, " PWMap implementation\n"); - break; - } + case PWMapKind::kUnordered: { + _impl = detail::UnordPWMap{m}; + break; + } + + case PWMapKind::kOrdered: { + _impl = detail::OrdPWMap{m}; + break; + } + + case PWMapKind::kDomOrdered: { + _impl = detail::DomOrdPWMap{m}; + break; + } + + default: { + Util::ERROR("Unsupported ", kind, " PWMap implementation\n"); + break; + } } } @@ -153,60 +145,51 @@ PWMap::PWMap(detail::PWMapImpl&& impl) : _impl(std::move(impl)) {} PWMap::ConstIt PWMap::begin() { - return std::visit([](const auto& a) { return PWMap::ConstIt(a.begin()); } - , _impl); + return std::visit([](const auto& a) { return PWMap::ConstIt(a.begin()); }, _impl); } PWMap::ConstIt PWMap::end() { - return std::visit([](const auto& a) { return PWMap::ConstIt(a.end()); } - , _impl); + return std::visit([](const auto& a) { return PWMap::ConstIt(a.end()); }, _impl); } // Setters --------------------------------------------------------------------- void PWMap::insert(const Map& m) { - std::visit([&m](auto& a) -> void { a.insert(m); } , _impl); + std::visit([&m](auto& a) -> void { a.insert(m); }, _impl); } void PWMap::insert(Map&& m) { - std::visit([move_m = std::move(m)](auto& a) { a.insert(move_m); } - , _impl); + std::visit([move_m = std::move(m)](auto& a) { a.insert(move_m); }, _impl); } // Operators ------------------------------------------------------------------- -bool PWMap::operator==(const PWMap& other) const -{ - return _impl == other._impl; -} +bool PWMap::operator==(const PWMap& other) const { return _impl == other._impl; } -bool PWMap::operator!=(const PWMap& other) const -{ - return !(*this == other); -} +bool PWMap::operator!=(const PWMap& other) const { return !(*this == other); } PWMap PWMap::operator+(const PWMap& other) const { - return std::visit([](const auto& a, const auto& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return PWMap{a + b}; - } else { - Util::ERROR("PWMap::operator+: mismatched implementations\n"); - return PWMap{}; - } - } - , _impl, other._impl); + return std::visit( + [](const auto& a, const auto& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return PWMap{a + b}; + } else { + Util::ERROR("PWMap::operator+: mismatched implementations\n"); + return PWMap{}; + } + }, + _impl, other._impl); } std::ostream& PWMap::print(std::ostream& out) const { - std::visit([&out](const auto& a) -> void { a.print(out); } , _impl); + std::visit([&out](const auto& a) -> void { a.print(out); }, _impl); return out; } @@ -228,7 +211,7 @@ bool PWMap::isEmpty() const return std::visit([](const auto& a) { return a.isEmpty(); }, _impl); } -Set PWMap::domain() const & +Set PWMap::domain() const& { return std::visit([](const auto& a) { return a.domain(); }, _impl); } @@ -240,14 +223,9 @@ Set PWMap::domain() && PWMap PWMap::restrict(const Set& subdom) const { - return std::visit([&](const auto& a) -> PWMap - { - return PWMap{a.restrict(subdom)}; - } - , _impl); + return std::visit([&](const auto& a) -> PWMap { return PWMap{a.restrict(subdom)}; }, _impl); } - Set PWMap::image() const { return std::visit([](const auto& a) { return a.image(); }, _impl); @@ -255,42 +233,38 @@ Set PWMap::image() const Set PWMap::image(const Set& subdom) const { - return std::visit([&subdom](const auto& a) { return a.image(subdom); } - , _impl); + return std::visit([&subdom](const auto& a) { return a.image(subdom); }, _impl); } Set PWMap::preImage(const Set& subcodom) const { - return std::visit([&subcodom](const auto& a) { return a.preImage(subcodom); } - , _impl); + return std::visit([&subcodom](const auto& a) { return a.preImage(subcodom); }, _impl); } PWMap PWMap::inverse() const { - return std::visit([](const auto& a) { return PWMap{a.inverse()}; } - , _impl); + return std::visit([](const auto& a) { return PWMap{a.inverse()}; }, _impl); } PWMap PWMap::composition(const PWMap& other) const { - return std::visit([](const auto& a, const auto& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return PWMap{a.composition(b)}; - } else { - Util::ERROR("PWMap::composition: mismatched implementations\n"); - return PWMap{}; - } - } - , _impl, other._impl); + return std::visit( + [](const auto& a, const auto& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return PWMap{a.composition(b)}; + } else { + Util::ERROR("PWMap::composition: mismatched implementations\n"); + return PWMap{}; + } + }, + _impl, other._impl); } PWMap PWMap::mapInf() const { - return std::visit([](const auto& a) { return PWMap{a.mapInf()}; } - , _impl); + return std::visit([](const auto& a) { return PWMap{a.mapInf()}; }, _impl); } Set PWMap::fixedPoints() const @@ -300,168 +274,149 @@ Set PWMap::fixedPoints() const // Extra operations ------------------------------------------------------------ -PWMap PWMap::concatenation(const PWMap& other) const & -{ - return PWMap{*this}.concatenation(other); -} +PWMap PWMap::concatenation(const PWMap& other) const& { return PWMap{*this}.concatenation(other); } -PWMap PWMap::concatenation(const PWMap& other) && -{ - return std::move(*this).concatenation(PWMap{other}); -} +PWMap PWMap::concatenation(const PWMap& other) && { return std::move(*this).concatenation(PWMap{other}); } -PWMap PWMap::concatenation(PWMap&& other) const & -{ - return PWMap{*this}.concatenation(std::move(other)); -} +PWMap PWMap::concatenation(PWMap&& other) const& { return PWMap{*this}.concatenation(std::move(other)); } PWMap PWMap::concatenation(PWMap&& other) && { - return std::visit([](auto&& a, auto&& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return PWMap{std::move(a).concatenation(std::move(b))}; - } else { - Util::ERROR("PWMap::concatenation: mismatched implementations\n"); - return PWMap{}; - } - } - , std::move(_impl), std::move(other._impl)); + return std::visit( + [](auto&& a, auto&& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return PWMap{std::move(a).concatenation(std::move(b))}; + } else { + Util::ERROR("PWMap::concatenation: mismatched implementations\n"); + return PWMap{}; + } + }, + std::move(_impl), std::move(other._impl)); } -PWMap PWMap::combine(const PWMap& other) const & -{ - return PWMap{*this}.combine(other); -} +PWMap PWMap::combine(const PWMap& other) const& { return PWMap{*this}.combine(other); } -PWMap PWMap::combine(const PWMap& other) && -{ - return std::move(*this).combine(PWMap{other}); -} +PWMap PWMap::combine(const PWMap& other) && { return std::move(*this).combine(PWMap{other}); } -PWMap PWMap::combine(PWMap&& other) const & -{ - return PWMap{*this}.combine(std::move(other)); -} +PWMap PWMap::combine(PWMap&& other) const& { return PWMap{*this}.combine(std::move(other)); } PWMap PWMap::combine(PWMap&& other) && { - return std::visit([](auto&& a, auto&& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return PWMap{std::move(a).combine(std::move(b))}; - } else { - Util::ERROR("PWMap::combine: mismatched implementations\n"); - return PWMap{}; - } - } - , std::move(_impl), std::move(other._impl)); + return std::visit( + [](auto&& a, auto&& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return PWMap{std::move(a).combine(std::move(b))}; + } else { + Util::ERROR("PWMap::combine: mismatched implementations\n"); + return PWMap{}; + } + }, + std::move(_impl), std::move(other._impl)); } PWMap PWMap::min(const PWMap& other) const { - return std::visit([](const auto& a, const auto& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return PWMap{a.min(b)}; - } else { - Util::ERROR("PWMap::min: mismatched implementations\n"); - return PWMap{}; - } - } - , _impl, other._impl); + return std::visit( + [](const auto& a, const auto& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return PWMap{a.min(b)}; + } else { + Util::ERROR("PWMap::min: mismatched implementations\n"); + return PWMap{}; + } + }, + _impl, other._impl); } PWMap PWMap::max(const PWMap& other) const { - return std::visit([](const auto& a, const auto& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return PWMap{a.max(b)}; - } else { - Util::ERROR("PWMap::max: mismatched implementations\n"); - return PWMap{}; - } - } - , _impl, other._impl); + return std::visit( + [](const auto& a, const auto& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return PWMap{a.max(b)}; + } else { + Util::ERROR("PWMap::max: mismatched implementations\n"); + return PWMap{}; + } + }, + _impl, other._impl); } PWMap PWMap::minAdj(const PWMap& other) const { - return std::visit([](const auto& a, const auto& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return PWMap{a.minAdj(b)}; - } else { - Util::ERROR("PWMap::minAdj: mismatched implementations\n"); - return PWMap{}; - } - } - , _impl, other._impl); + return std::visit( + [](const auto& a, const auto& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return PWMap{a.minAdj(b)}; + } else { + Util::ERROR("PWMap::minAdj: mismatched implementations\n"); + return PWMap{}; + } + }, + _impl, other._impl); } PWMap PWMap::maxAdj(const PWMap& other) const { - return std::visit([](const auto& a, const auto& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return PWMap{a.maxAdj(b)}; - } else { - Util::ERROR("PWMap::maxAdj: mismatched implementations\n"); - return PWMap{}; - } - } - , _impl, other._impl); + return std::visit( + [](const auto& a, const auto& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return PWMap{a.maxAdj(b)}; + } else { + Util::ERROR("PWMap::maxAdj: mismatched implementations\n"); + return PWMap{}; + } + }, + _impl, other._impl); } Set PWMap::equalImage(const PWMap& other) const { - return std::visit([](const auto& a, const auto& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return a.equalImage(b); - } else { - Util::ERROR("PWMap::equalImage: mismatched implementations\n"); - return Set{}; - } - } - , _impl, other._impl); + return std::visit( + [](const auto& a, const auto& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return a.equalImage(b); + } else { + Util::ERROR("PWMap::equalImage: mismatched implementations\n"); + return Set{}; + } + }, + _impl, other._impl); } Set PWMap::lessImage(const PWMap& other) const { - return std::visit([](const auto& a, const auto& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return a.lessImage(b); - } else { - Util::ERROR("PWMap::lessImage: mismatched implementations\n"); - return Set{}; - } - } - , _impl, other._impl); + return std::visit( + [](const auto& a, const auto& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return a.lessImage(b); + } else { + Util::ERROR("PWMap::lessImage: mismatched implementations\n"); + return Set{}; + } + }, + _impl, other._impl); } PWMap PWMap::imageMultiplicity() const { - return std::visit([](const auto& a) { return PWMap{a.imageMultiplicity()}; } - , _impl); + return std::visit([](const auto& a) { return PWMap{a.imageMultiplicity()}; }, _impl); } void PWMap::compact() @@ -471,8 +426,7 @@ void PWMap::compact() rapidjson::Value PWMap::toJSON(rapidjson::Document::AllocatorType& alloc) const { - return std::visit([&alloc](auto a) { return detail::toJSON(a, alloc); } - , _impl); + return std::visit([&alloc](auto a) { return detail::toJSON(a, alloc); }, _impl); } } // namespace LIB diff --git a/sbg/pwmap_detail.cpp b/sbg/pwmap_detail.cpp old mode 100755 new mode 100644 index dcd2f454..925d5573 --- a/sbg/pwmap_detail.cpp +++ b/sbg/pwmap_detail.cpp @@ -28,16 +28,13 @@ namespace detail { PWMap PWMapAccessKey::reduce(PWMap pw) const { auto& impl = pw._impl; - return std::visit( - [](auto&& impl) -> PWMap { - return PWMap{impl.reduce()}; - }, impl); + return std::visit([](auto&& impl) -> PWMap { return PWMap{impl.reduce()}; }, impl); } PWMapAccessKey PWMapAccess::key() { return PWMapAccessKey{}; }; -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/pwmap_impl.cpp b/sbg/pwmap_impl.cpp index 3ce96a8e..b91d6001 100644 --- a/sbg/pwmap_impl.cpp +++ b/sbg/pwmap_impl.cpp @@ -31,25 +31,25 @@ namespace LIB { std::ostream& operator<<(std::ostream& out, PWMapKind kind) { switch (kind) { - case PWMapKind::kUnordered: { - out << "unordered"; - break; - } - - case PWMapKind::kOrdered: { - out << "ordered"; - break; - } - - case PWMapKind::kDomOrdered: { - out << "domain ordered"; - break; - } - - default: { - Util::ERROR("Unsupported ", kind, " implementation\n"); - break; - } + case PWMapKind::kUnordered: { + out << "unordered"; + break; + } + + case PWMapKind::kOrdered: { + out << "ordered"; + break; + } + + case PWMapKind::kDomOrdered: { + out << "domain ordered"; + break; + } + + default: { + Util::ERROR("Unsupported ", kind, " implementation\n"); + break; + } } return out; @@ -65,11 +65,8 @@ PWMapImplementation& PWMapImplementation::instance() const PWMapKind& PWMapImplementation::kind() const { return _kind; } -void PWMapImplementation::set_pwmap_fact(PWMapKind kind) -{ - _kind = kind; -} +void PWMapImplementation::set_pwmap_fact(PWMapKind kind) { _kind = kind; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/rational.cpp b/sbg/rational.cpp old mode 100755 new mode 100644 index 89cd38a1..41f3f668 --- a/sbg/rational.cpp +++ b/sbg/rational.cpp @@ -41,7 +41,7 @@ Rational::Rational(Int n, Int d) : _value() // Getters --------------------------------------------------------------------- -const Rational::RationalT& Rational::value() const { return _value; } +const Rational::RationalT& Rational::value() const { return _value; } Int Rational::numerator() const { return _value.numerator(); } @@ -49,60 +49,27 @@ Int Rational::denominator() const { return _value.denominator(); } // Operators ------------------------------------------------------------------- -bool Rational::operator==(const Rational& r) const -{ - return _value == r._value; -} +bool Rational::operator==(const Rational& r) const { return _value == r._value; } -bool Rational::operator!=(const Rational& r) const -{ - return _value != r._value; -} +bool Rational::operator!=(const Rational& r) const { return _value != r._value; } -bool Rational::operator<(const Rational& r) const -{ - return _value < r._value; -} +bool Rational::operator<(const Rational& r) const { return _value < r._value; } -bool Rational::operator>(const Rational& r) const -{ - return _value > r._value; -} +bool Rational::operator>(const Rational& r) const { return _value > r._value; } -bool Rational::operator>=(const Rational& r) const -{ - return _value >= r._value; -} +bool Rational::operator>=(const Rational& r) const { return _value >= r._value; } -bool Rational::operator==(const Int& other) const -{ - return numerator() == other && denominator() == 1; -} +bool Rational::operator==(const Int& other) const { return numerator() == other && denominator() == 1; } -Rational Rational::operator-() const -{ - return Rational{-_value}; -} +Rational Rational::operator-() const { return Rational{-_value}; } -Rational Rational::operator+(const Rational& other) const -{ - return Rational{_value + other._value}; -} +Rational Rational::operator+(const Rational& other) const { return Rational{_value + other._value}; } -Rational Rational::operator-(const Rational& other) const -{ - return Rational{_value - other._value}; -} +Rational Rational::operator-(const Rational& other) const { return Rational{_value - other._value}; } -Rational Rational::operator*(const Rational& other) const -{ - return Rational{_value*other._value}; -} +Rational Rational::operator*(const Rational& other) const { return Rational{_value * other._value}; } -Rational Rational::operator/(const Rational& other) const -{ - return Rational{_value/other._value}; -} +Rational Rational::operator/(const Rational& other) const { return Rational{_value / other._value}; } // Member functions ------------------------------------------------------------ @@ -114,10 +81,7 @@ Int Rational::toInt() const return 0; } -Int Rational::floor() const -{ - return boost::rational_cast(_value); -} +Int Rational::floor() const { return boost::rational_cast(_value); } Int Rational::ceiling() const { @@ -142,6 +106,6 @@ std::ostream& operator<<(std::ostream& out, const Rational& r) return out; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/sbg.cpp b/sbg/sbg.cpp index f585d16a..88ff5d86 100644 --- a/sbg/sbg.cpp +++ b/sbg/sbg.cpp @@ -33,14 +33,12 @@ namespace LIB { // Constructors/Destructors ---------------------------------------------------- -SBG::SBG() - : _V(), _Vmap(), _E(), _map1(), _map2(), _Emap() {} +SBG::SBG() : _V(), _Vmap(), _E(), _map1(), _map2(), _Emap() {} -SBG::SBG(const Set& V, const PWMap& Vmap - , const PWMap& map1, const PWMap& map2 - , const PWMap& Emap) - : _V(V), _Vmap(Vmap), _E(map1.domain().intersection(map2.domain())) - , _map1(map1), _map2(map2), _Emap(Emap) {} +SBG::SBG(const Set& V, const PWMap& Vmap, const PWMap& map1, const PWMap& map2, const PWMap& Emap) + : _V(V), _Vmap(Vmap), _E(map1.domain().intersection(map2.domain())), _map1(map1), _map2(map2), _Emap(Emap) +{ +} // Getters --------------------------------------------------------------------- @@ -61,13 +59,11 @@ const PWMap& SBG::Emap() const { return _Emap; } void SBG::addSetVertex(const Set& vertices) { if (!vertices.intersection(_V).isEmpty()) { - Util::ERROR("SBG::addSetVertex: trying to add existing vertices: ", vertices - , " to SBG\n"); + Util::ERROR("SBG::addSetVertex: trying to add existing vertices: ", vertices, " to SBG\n"); } else if (!vertices.isEmpty()) { Set set_vertices = _Vmap.image(); std::size_t arity = vertices.arity(); - IntTuple max = set_vertices.isEmpty() ? IntTuple{arity, 0} - : set_vertices.maxElem(); + IntTuple max = set_vertices.isEmpty() ? IntTuple{arity, 0} : set_vertices.maxElem(); _Vmap.emplace(vertices, max + IntTuple{arity, 1}); _V = std::move(_V).cup(std::move(vertices)); } @@ -78,23 +74,20 @@ void SBG::addSetEdge(const PWMap& pw1, const PWMap& pw2) Set edges1 = pw1.domain(); Set edges2 = pw2.domain(); if (edges1 != edges2) { - Util::ERROR("SBG::addSetEdge: ", edges1, " is different from ", edges2 - , "\n"); + Util::ERROR("SBG::addSetEdge: ", edges1, " is different from ", edges2, "\n"); } else if (edges1.intersection(_E).isEmpty()) { Set edges = edges1; if (!edges.isEmpty()) { Set set_edges = _Emap.image(); std::size_t arity = edges.arity(); - IntTuple max = set_edges.isEmpty() ? IntTuple{arity, 0} - : set_edges.maxElem(); + IntTuple max = set_edges.isEmpty() ? IntTuple{arity, 0} : set_edges.maxElem(); _map1 = std::move(_map1).concatenation(pw1); _map2 = std::move(_map2).concatenation(pw2); _Emap.emplace(edges, max + IntTuple{arity, 1}); _E = std::move(_E).cup(std::move(edges)); } } else { - Util::ERROR("SBG::addSetEdge: trying to add existing edges: ", edges1 - , " to SBG\n"); + Util::ERROR("SBG::addSetEdge: trying to add existing edges: ", edges1, " to SBG\n"); } } @@ -124,6 +117,6 @@ std::ostream& operator<<(std::ostream& out, const SBG& g) return out; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/set.cpp b/sbg/set.cpp index 374d4216..80c2da96 100644 --- a/sbg/set.cpp +++ b/sbg/set.cpp @@ -37,25 +37,25 @@ Set::Set() : _impl() { SetKind kind = SET_IMPL.kind(); switch (kind) { - case SetKind::kUnordered: { - _impl = detail::UnorderedSet{}; - break; - } - - case SetKind::kOrdered: { - _impl = detail::OrderedSet{}; - break; - } - - case SetKind::kOrdUnidimDense: { - _impl = detail::OrdUnidimDenseSet{}; - break; - } - - default: { - Util::ERROR("Unsupported ", kind, " Set implementation\n"); - break; - } + case SetKind::kUnordered: { + _impl = detail::UnorderedSet{}; + break; + } + + case SetKind::kOrdered: { + _impl = detail::OrderedSet{}; + break; + } + + case SetKind::kOrdUnidimDense: { + _impl = detail::OrdUnidimDenseSet{}; + break; + } + + default: { + Util::ERROR("Unsupported ", kind, " Set implementation\n"); + break; + } } } @@ -63,25 +63,25 @@ Set::Set(const IntTuple& x) : _impl() { SetKind kind = SET_IMPL.kind(); switch (kind) { - case SetKind::kUnordered: { - _impl = detail::UnorderedSet{x}; - break; - } - - case SetKind::kOrdered: { - _impl = detail::OrderedSet{x}; - break; - } - - case SetKind::kOrdUnidimDense: { - _impl = detail::OrdUnidimDenseSet{x[0]}; - break; - } - - default: { - Util::ERROR("Unsupported ", kind, " Set implementation\n"); - break; - } + case SetKind::kUnordered: { + _impl = detail::UnorderedSet{x}; + break; + } + + case SetKind::kOrdered: { + _impl = detail::OrderedSet{x}; + break; + } + + case SetKind::kOrdUnidimDense: { + _impl = detail::OrdUnidimDenseSet{x[0]}; + break; + } + + default: { + Util::ERROR("Unsupported ", kind, " Set implementation\n"); + break; + } } } @@ -89,25 +89,25 @@ Set::Set(IntTuple&& x) : _impl() { SetKind kind = SET_IMPL.kind(); switch (kind) { - case SetKind::kUnordered: { - _impl = detail::UnorderedSet{x}; - break; - } - - case SetKind::kOrdered: { - _impl = detail::OrderedSet{x}; - break; - } - - case SetKind::kOrdUnidimDense: { - _impl = detail::OrdUnidimDenseSet{x[0]}; - break; - } - - default: { - Util::ERROR("Unsupported ", kind, " Set implementation\n"); - break; - } + case SetKind::kUnordered: { + _impl = detail::UnorderedSet{x}; + break; + } + + case SetKind::kOrdered: { + _impl = detail::OrderedSet{x}; + break; + } + + case SetKind::kOrdUnidimDense: { + _impl = detail::OrdUnidimDenseSet{x[0]}; + break; + } + + default: { + Util::ERROR("Unsupported ", kind, " Set implementation\n"); + break; + } } } @@ -115,25 +115,25 @@ Set::Set(const Int lo, const Int step, const Int hi) : _impl() { SetKind kind = SET_IMPL.kind(); switch (kind) { - case SetKind::kUnordered: { - _impl = detail::UnorderedSet{detail::Interval(lo, step, hi)}; - break; - } - - case SetKind::kOrdered: { - _impl = detail::OrderedSet{detail::Interval(lo, step, hi)}; - break; - } - - case SetKind::kOrdUnidimDense: { - _impl = detail::OrdUnidimDenseSet{detail::Interval(lo, step, hi)}; - break; - } - - default: { - Util::ERROR("Unsupported ", kind, " Set implementation\n"); - break; - } + case SetKind::kUnordered: { + _impl = detail::UnorderedSet{detail::Interval(lo, step, hi)}; + break; + } + + case SetKind::kOrdered: { + _impl = detail::OrderedSet{detail::Interval(lo, step, hi)}; + break; + } + + case SetKind::kOrdUnidimDense: { + _impl = detail::OrdUnidimDenseSet{detail::Interval(lo, step, hi)}; + break; + } + + default: { + Util::ERROR("Unsupported ", kind, " Set implementation\n"); + break; + } } } @@ -141,25 +141,25 @@ Set::Set(const FixedPointsInfo& info) : _impl() { SetKind kind = SET_IMPL.kind(); switch (kind) { - case SetKind::kUnordered: { - _impl = detail::UnorderedSet{info}; - break; - } - - case SetKind::kOrdered: { - _impl = detail::OrderedSet{info}; - break; - } - - case SetKind::kOrdUnidimDense: { - _impl = detail::OrdUnidimDenseSet{info}; - break; - } - - default: { - Util::ERROR("Unsupported ", kind, " Set implementation\n"); - break; - } + case SetKind::kUnordered: { + _impl = detail::UnorderedSet{info}; + break; + } + + case SetKind::kOrdered: { + _impl = detail::OrderedSet{info}; + break; + } + + case SetKind::kOrdUnidimDense: { + _impl = detail::OrdUnidimDenseSet{info}; + break; + } + + default: { + Util::ERROR("Unsupported ", kind, " Set implementation\n"); + break; + } } } @@ -169,19 +169,13 @@ Set::Set(detail::SetImpl&& impl) : _impl(std::move(impl)) {} // Operators ------------------------------------------------------------------- -bool Set::operator==(const Set& other) const -{ - return _impl == other._impl; -} +bool Set::operator==(const Set& other) const { return _impl == other._impl; } -bool Set::operator!=(const Set& other) const -{ - return !(*this == other); -} +bool Set::operator!=(const Set& other) const { return !(*this == other); } std::ostream& Set::print(std::ostream& out) const { - std::visit([&out](const auto& a) -> void { a.print(out); } , _impl); + std::visit([&out](const auto& a) -> void { a.print(out); }, _impl); return out; } @@ -213,90 +207,79 @@ IntTuple Set::maxElem() const return std::visit([](const auto& a) { return a.maxElem(); }, _impl); } -Set Set::intersection(const Set& other) const & +Set Set::intersection(const Set& other) const& { - return std::visit([](const auto& a, const auto& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return Set{a.intersection(b)}; - } else { - Util::ERROR("Set::intersection: mismatched implementations\n"); - return Set{}; - } - } - , _impl, other._impl); + return std::visit( + [](const auto& a, const auto& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return Set{a.intersection(b)}; + } else { + Util::ERROR("Set::intersection: mismatched implementations\n"); + return Set{}; + } + }, + _impl, other._impl); } -Set Set::cup(const Set& other) const & -{ - return Set{*this}.cup(other); -} +Set Set::cup(const Set& other) const& { return Set{*this}.cup(other); } -Set Set::cup(const Set& other) && -{ - return std::move(*this).cup(Set{other}); -} +Set Set::cup(const Set& other) && { return std::move(*this).cup(Set{other}); } -Set Set::cup(Set&& other) const & -{ - return Set{*this}.cup(std::move(other)); -} +Set Set::cup(Set&& other) const& { return Set{*this}.cup(std::move(other)); } Set Set::cup(Set&& other) && { - return std::visit([](auto&& a, auto&& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return Set{std::move(a).cup(std::move(b))}; - } else { - Util::ERROR("Set::cup: mismatched implementations\n"); - return Set{}; - } - } - , std::move(_impl), std::move(other._impl)); + return std::visit( + [](auto&& a, auto&& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return Set{std::move(a).cup(std::move(b))}; + } else { + Util::ERROR("Set::cup: mismatched implementations\n"); + return Set{}; + } + }, + std::move(_impl), std::move(other._impl)); } Set Set::complement() const { - return std::visit( - [](const auto& a) -> Set { return Set{a.complement()}; } - , _impl); + return std::visit([](const auto& a) -> Set { return Set{a.complement()}; }, _impl); } Set Set::difference(const Set& other) const { - return std::visit([](const auto& a, const auto& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return Set{a.difference(b)}; - } else { - Util::ERROR("Set::difference: mismatched implementations\n"); - return Set{}; - } - } - , _impl, other._impl); + return std::visit( + [](const auto& a, const auto& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return Set{a.difference(b)}; + } else { + Util::ERROR("Set::difference: mismatched implementations\n"); + return Set{}; + } + }, + _impl, other._impl); } Set Set::cartesianProduct(const Set& other) const { - return std::visit([](const auto& a, const auto& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return Set{a.cartesianProduct(b)}; - } else { - Util::ERROR("Set::cartesianProduct: mismatched implementations\n"); - return Set{}; - } - } - , _impl, other._impl); + return std::visit( + [](const auto& a, const auto& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return Set{a.cartesianProduct(b)}; + } else { + Util::ERROR("Set::cartesianProduct: mismatched implementations\n"); + return Set{}; + } + }, + _impl, other._impl); } // Extra operations ------------------------------------------------------------ @@ -306,42 +289,31 @@ std::size_t Set::arity() const return std::visit([](const auto& a) { return a.arity(); }, _impl); } -Set Set::disjointCup(const Set& other) const & -{ - return Set{*this}.disjointCup(other); -} +Set Set::disjointCup(const Set& other) const& { return Set{*this}.disjointCup(other); } -Set Set::disjointCup(const Set& other) && -{ - return std::move(*this).disjointCup(Set{other}); -} +Set Set::disjointCup(const Set& other) && { return std::move(*this).disjointCup(Set{other}); } -Set Set::disjointCup(Set&& other) const & -{ - return Set{*this}.disjointCup(std::move(other)); -} +Set Set::disjointCup(Set&& other) const& { return Set{*this}.disjointCup(std::move(other)); } Set Set::disjointCup(Set&& other) && { - return std::visit([](auto&& a, auto&& b) - { - using A = std::decay_t; - using B = std::decay_t; - if constexpr (std::is_same_v) { - return Set{std::move(a).disjointCup(std::move(b))}; - } else { - Util::ERROR("Set::disjointCup: mismatched implementations\n"); - return Set{}; - } - } - , std::move(_impl), std::move(other._impl)); + return std::visit( + [](auto&& a, auto&& b) { + using A = std::decay_t; + using B = std::decay_t; + if constexpr (std::is_same_v) { + return Set{std::move(a).disjointCup(std::move(b))}; + } else { + Util::ERROR("Set::disjointCup: mismatched implementations\n"); + return Set{}; + } + }, + std::move(_impl), std::move(other._impl)); } Set Set::translate(const IntTuple& t) const { - return std::visit( - [&t](const auto& a) -> Set { return Set{a.translate(t)}; } - , _impl); + return std::visit([&t](const auto& a) -> Set { return Set{a.translate(t)}; }, _impl); } Perimeter Set::perimeter() const @@ -349,12 +321,14 @@ Perimeter Set::perimeter() const return std::visit([](const auto& a) { return a.perimeter(); }, _impl); } -void Set::compact() { std::visit([](auto& a) { a.compact(); }, _impl); } +void Set::compact() +{ + std::visit([](auto& a) { a.compact(); }, _impl); +} rapidjson::Value Set::toJSON(rapidjson::Document::AllocatorType& alloc) const { - return std::visit([&alloc](auto a) { return detail::toJSON(a, alloc); } - , _impl); + return std::visit([&alloc](auto a) { return detail::toJSON(a, alloc); }, _impl); } } // namespace LIB diff --git a/sbg/set_detail.cpp b/sbg/set_detail.cpp old mode 100755 new mode 100644 index 4a5bd3ea..9f05967a --- a/sbg/set_detail.cpp +++ b/sbg/set_detail.cpp @@ -34,7 +34,7 @@ SetImpl SetAccessKey::impl(Set s) const { return s._impl; } Set SetAccessKey::createSet(SetImpl s_impl) const { return Set{s_impl}; } -template +template std::vector SetAccessKey::flatten(const SetImplT& s) const { std::vector result; @@ -46,13 +46,13 @@ std::vector SetAccessKey::flatten(const SetImplT& s) const IntTuple min_elem = mdi.minElem(); IntTuple x = min_elem; unsigned int mdi_sz = mdi.cardinal(); - unsigned int div = mdi_sz; + unsigned int div = mdi_sz; for (unsigned int j = 0; j < mdi_sz; ++j) { unsigned int rem = j; for (std::size_t k = arity; k > 0; --k) { - Interval i = mdi[k-1]; + Interval i = mdi[k - 1]; unsigned int kth_card = i.cardinal(); - x[k] = i.begin() + i.step()*fmod(rem, kth_card); + x[k] = i.begin() + i.step() * fmod(rem, kth_card); rem /= kth_card; } } @@ -76,39 +76,20 @@ std::vector SetAccessKey::flatten(const OrdUnidimDenseSet& s) const std::vector SetAccessKey::flatten(const Set& s) const { - auto flatten_evaluator = Util::Overload { - [&](const UnorderedSet& a) - { - return flatten(a); - }, - [&](const OrdUnidimDenseSet& a) - { - return flatten(a); - }, - [&](const OrderedSet& a) - { - return flatten(a); - }, - [&](const auto& a) { return std::vector{}; } - }; + auto flatten_evaluator = Util::Overload{ + [&](const UnorderedSet& a) { return flatten(a); }, [&](const OrdUnidimDenseSet& a) { return flatten(a); }, + [&](const OrderedSet& a) { return flatten(a); }, [&](const auto& a) { return std::vector{}; }}; return std::visit(flatten_evaluator, s._impl); } -OrderedSet::OrdMDICollection SetAccessKey::pieces(OrderedSet s) const -{ - return s._pieces; -} +OrderedSet::OrdMDICollection SetAccessKey::pieces(OrderedSet s) const { return s._pieces; } -OrdUnidimDenseSet::OrdIntervalCollection SetAccessKey::pieces( - OrdUnidimDenseSet s) const -{ - return s._pieces; -} +OrdUnidimDenseSet::OrdIntervalCollection SetAccessKey::pieces(OrdUnidimDenseSet s) const { return s._pieces; } SetAccessKey SetAccess::key() { return SetAccessKey{}; }; -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/set_impl.cpp b/sbg/set_impl.cpp index ff6877d0..0853b59f 100644 --- a/sbg/set_impl.cpp +++ b/sbg/set_impl.cpp @@ -31,25 +31,25 @@ namespace LIB { std::ostream& operator<<(std::ostream& out, const SetKind kind) { switch (kind) { - case SetKind::kUnordered: { - out << "unordered"; - break; - } - - case SetKind::kOrdered: { - out << "ordered"; - break; - } - - case SetKind::kOrdUnidimDense: { - out << "uni-dimensional ordered dense"; - break; - } - - default: { - Util::ERROR("Unsupported ", kind, " Set implementation\n"); - break; - } + case SetKind::kUnordered: { + out << "unordered"; + break; + } + + case SetKind::kOrdered: { + out << "ordered"; + break; + } + + case SetKind::kOrdUnidimDense: { + out << "uni-dimensional ordered dense"; + break; + } + + default: { + Util::ERROR("Unsupported ", kind, " Set implementation\n"); + break; + } } return out; @@ -67,6 +67,6 @@ const SetKind& SetImplementation::kind() const { return _kind; } void SetImplementation::set_set_fact(SetKind kind) { _kind = kind; } -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/sbg/unord_pwmap.cpp b/sbg/unord_pwmap.cpp index 3388f8bb..d9b149ac 100644 --- a/sbg/unord_pwmap.cpp +++ b/sbg/unord_pwmap.cpp @@ -36,33 +36,31 @@ namespace detail { // Auxiliary definitions ------------------------------------------------------- class MapLess { -public: - bool operator()(const Map& a, const Map& b) const { - return a.domain().minElem() < b.domain().minElem(); - } + public: + bool operator()(const Map& a, const Map& b) const { return a.domain().minElem() < b.domain().minElem(); } }; // Constructors/Destructors ---------------------------------------------------- UnordPWMap::UnordPWMap() : _pieces() {} -UnordPWMap::UnordPWMap(const Set& s) : _pieces() { +UnordPWMap::UnordPWMap(const Set& s) : _pieces() +{ if (!s.isEmpty()) { _pieces.emplace_back(s, Expression{s.arity(), 1, 0}); } } -UnordPWMap::UnordPWMap(const Map& m) : _pieces() { +UnordPWMap::UnordPWMap(const Map& m) : _pieces() +{ if (!m.domain().isEmpty()) { _pieces.push_back(m); } } -UnordPWMap::UnordPWMap(const UnordPWMap::UnordMapCollection& pieces) - : _pieces(pieces) {} +UnordPWMap::UnordPWMap(const UnordPWMap::UnordMapCollection& pieces) : _pieces(pieces) {} -UnordPWMap::UnordPWMap(UnordPWMap::UnordMapCollection&& pieces) - : _pieces(std::move(pieces)) {} +UnordPWMap::UnordPWMap(UnordPWMap::UnordMapCollection&& pieces) : _pieces(std::move(pieces)) {} // Getters --------------------------------------------------------------------- @@ -102,7 +100,7 @@ void UnordPWMap::pushBack(Map&& m) // Operators ------------------------------------------------------------------- -bool UnordPWMap::operator==(const UnordPWMap& other) const +bool UnordPWMap::operator==(const UnordPWMap& other) const { if (domain() != other.domain()) { return false; @@ -134,17 +132,14 @@ bool UnordPWMap::operator==(const UnordPWMap& other) const return true; } -bool UnordPWMap::operator!=(const UnordPWMap& other) const -{ - return !(*this == other); -} +bool UnordPWMap::operator!=(const UnordPWMap& other) const { return !(*this == other); } UnordPWMap UnordPWMap::operator+(const UnordPWMap& other) const { UnordPWMap result; - - for (const Map& m1 : _pieces) { - for (const Map& m2 : other._pieces) { + + for (const Map& m1 : _pieces) { + for (const Map& m2 : other._pieces) { result.pushBack(m1 + m2); } } @@ -182,7 +177,7 @@ std::size_t UnordPWMap::arity() const bool UnordPWMap::isEmpty() const { return _pieces.empty(); } -Set UnordPWMap::domain() const & +Set UnordPWMap::domain() const& { Set result; @@ -226,10 +221,7 @@ Set UnordPWMap::image() const return result; } -Set UnordPWMap::image(const Set& subdom) const -{ - return restrict(subdom).image(); -} +Set UnordPWMap::image(const Set& subdom) const { return restrict(subdom).image(); } Set UnordPWMap::preImage(const Set& subcodom) const { @@ -257,7 +249,7 @@ UnordPWMap UnordPWMap::composition(const UnordPWMap& other) const { UnordPWMap result; - for (const Map& m1 : _pieces) { + for (const Map& m1 : _pieces) { for (const Map& m2 : other._pieces) { result.pushBack(m1.composition(m2)); } @@ -316,20 +308,11 @@ Set UnordPWMap::fixedPoints() const // Extra operations ------------------------------------------------------------ -UnordPWMap UnordPWMap::concatenation(const UnordPWMap& other) const & -{ - return UnordPWMap{*this}.concatenation(other); -} +UnordPWMap UnordPWMap::concatenation(const UnordPWMap& other) const& { return UnordPWMap{*this}.concatenation(other); } -UnordPWMap UnordPWMap::concatenation(const UnordPWMap& other) && -{ - return std::move(*this).concatenation(UnordPWMap{other}); -} +UnordPWMap UnordPWMap::concatenation(const UnordPWMap& other) && { return std::move(*this).concatenation(UnordPWMap{other}); } -UnordPWMap UnordPWMap::concatenation(UnordPWMap&& other) const & -{ - return UnordPWMap{*this}.concatenation(std::move(other)); -} +UnordPWMap UnordPWMap::concatenation(UnordPWMap&& other) const& { return UnordPWMap{*this}.concatenation(std::move(other)); } UnordPWMap UnordPWMap::concatenation(UnordPWMap&& other) && { @@ -342,30 +325,19 @@ UnordPWMap UnordPWMap::concatenation(UnordPWMap&& other) && } UnordMapCollection result; - result.insert(result.end(), std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(_pieces.end())); - result.insert(result.end(), std::make_move_iterator(other._pieces.begin()) - , std::make_move_iterator(other._pieces.end())); + result.insert(result.end(), std::make_move_iterator(_pieces.begin()), std::make_move_iterator(_pieces.end())); + result.insert(result.end(), std::make_move_iterator(other._pieces.begin()), std::make_move_iterator(other._pieces.end())); return UnordPWMap{std::move(result)}; } -UnordPWMap UnordPWMap::combine(const UnordPWMap& other) const & -{ - return UnordPWMap{*this}.combine(other); -} +UnordPWMap UnordPWMap::combine(const UnordPWMap& other) const& { return UnordPWMap{*this}.combine(other); } -UnordPWMap UnordPWMap::combine(const UnordPWMap& other) && -{ - return std::move(*this).combine(UnordPWMap{other}); -} +UnordPWMap UnordPWMap::combine(const UnordPWMap& other) && { return std::move(*this).combine(UnordPWMap{other}); } -UnordPWMap UnordPWMap::combine(UnordPWMap&& other) const & -{ - return UnordPWMap{*this}.combine(std::move(other)); -} +UnordPWMap UnordPWMap::combine(UnordPWMap&& other) const& { return UnordPWMap{*this}.combine(std::move(other)); } -UnordPWMap UnordPWMap::combine(UnordPWMap&& other) && +UnordPWMap UnordPWMap::combine(UnordPWMap&& other) && { if (isEmpty()) { return std::move(other); @@ -390,7 +362,7 @@ UnordPWMap UnordPWMap::min(const UnordPWMap& other) const } Set min_in_pw1 = lessImage(other); - return restrict(min_in_pw1).combine(other.restrict(domain())); + return restrict(min_in_pw1).combine(other.restrict(domain())); } UnordPWMap UnordPWMap::max(const UnordPWMap& other) const @@ -401,7 +373,7 @@ UnordPWMap UnordPWMap::max(const UnordPWMap& other) const Set min_in_pw1 = lessImage(other); Set less_eq_in_pw1 = min_in_pw1.disjointCup(equalImage(other)); - return other.restrict(less_eq_in_pw1).combine(restrict(other.domain())); + return other.restrict(less_eq_in_pw1).combine(restrict(other.domain())); } UnordPWMap UnordPWMap::minAdj(const UnordPWMap& other) const @@ -417,7 +389,7 @@ UnordPWMap UnordPWMap::minAdj(const UnordPWMap& other) const Set repeated = min_adj_domain.intersection(visited); if (!repeated.isEmpty()) { UnordPWMap min_adj_pw{std::move(min_adj)}; - UnordPWMap min_adj_repeated = min_adj_pw.restrict(repeated); + UnordPWMap min_adj_repeated = min_adj_pw.restrict(repeated); UnordPWMap result_repeated = result.restrict(repeated); UnordPWMap min_map = min_adj_repeated.min(result_repeated); UnordPWMap min_adj_result = min_map.combine(std::move(min_adj_pw)); @@ -447,7 +419,7 @@ UnordPWMap UnordPWMap::maxAdj(const UnordPWMap& other) const Set repeated = max_adj_domain.intersection(visited); if (!repeated.isEmpty()) { UnordPWMap max_adj_pw{std::move(max_adj)}; - UnordPWMap max_adj_repeated = max_adj_pw.restrict(repeated); + UnordPWMap max_adj_repeated = max_adj_pw.restrict(repeated); UnordPWMap result_repeated = result.restrict(repeated); UnordPWMap max_map = max_adj_repeated.max(result_repeated); UnordPWMap max_adj_result = max_map.combine(std::move(max_adj_pw)); @@ -501,7 +473,7 @@ Set UnordPWMap::lessImage(const UnordPWMap& other) const } } - return min_in_pw1; + return min_in_pw1; } UnordPWMap UnordPWMap::imageMultiplicity() const @@ -516,7 +488,7 @@ UnordPWMap UnordPWMap::imageMultiplicity() const } return result; -} +} void UnordPWMap::compact() { @@ -569,8 +541,7 @@ void UnordPWMap::compact() // Non-member functions -------------------------------------------------------- -rapidjson::Value toJSON(UnordPWMap pw - , rapidjson::Document::AllocatorType& alloc) +rapidjson::Value toJSON(UnordPWMap pw, rapidjson::Document::AllocatorType& alloc) { rapidjson::Value result{rapidjson::kArrayType}; @@ -582,9 +553,8 @@ rapidjson::Value toJSON(UnordPWMap pw return result; } -} // namespace detail - -} // namespace LIB +} // namespace detail -} // namespace SBG; +} // namespace LIB +} // namespace SBG diff --git a/sbg/unord_set.cpp b/sbg/unord_set.cpp index e730fb0c..1ed03d5f 100644 --- a/sbg/unord_set.cpp +++ b/sbg/unord_set.cpp @@ -33,10 +33,7 @@ namespace detail { // Auxiliary functions --------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// -bool overlap(const UnorderedSet& lhs, const UnorderedSet& rhs) -{ - return rhs.minElem() < lhs.maxElem() || lhs.minElem() < rhs.maxElem(); -} +bool overlap(const UnorderedSet& lhs, const UnorderedSet& rhs) { return rhs.minElem() < lhs.maxElem() || lhs.minElem() < rhs.maxElem(); } //////////////////////////////////////////////////////////////////////////////// // Unordered Set Implementation ------------------------------------------------ @@ -46,10 +43,7 @@ bool overlap(const UnorderedSet& lhs, const UnorderedSet& rhs) UnorderedSet::UnorderedSet() : _pieces() {} -UnorderedSet::UnorderedSet(const IntTuple& x) : _pieces() -{ - _pieces.emplace_back(MultiDimInter{x}); -} +UnorderedSet::UnorderedSet(const IntTuple& x) : _pieces() { _pieces.emplace_back(MultiDimInter{x}); } UnorderedSet::UnorderedSet(const Interval& i) : _pieces() { @@ -65,11 +59,9 @@ UnorderedSet::UnorderedSet(const MultiDimInter& mdi) : _pieces() } } -UnorderedSet::UnorderedSet(const UnorderedSet::MDIUnordCollection& pieces) - : _pieces(pieces) {} +UnorderedSet::UnorderedSet(const UnorderedSet::MDIUnordCollection& pieces) : _pieces(pieces) {} -UnorderedSet::UnorderedSet(UnorderedSet::MDIUnordCollection&& pieces) - : _pieces(std::move(pieces)) {} +UnorderedSet::UnorderedSet(UnorderedSet::MDIUnordCollection&& pieces) : _pieces(std::move(pieces)) {} UnorderedSet::UnorderedSet(const FixedPointsInfo& info) : _pieces() { @@ -123,10 +115,7 @@ bool UnorderedSet::operator==(const UnorderedSet& other) const return difference(other).isEmpty() && other.difference(*this).isEmpty(); } -bool UnorderedSet::operator!=(const UnorderedSet& other) const -{ - return !(*this == other); -} +bool UnorderedSet::operator!=(const UnorderedSet& other) const { return !(*this == other); } std::ostream& UnorderedSet::print(std::ostream& out) const { @@ -135,9 +124,9 @@ std::ostream& UnorderedSet::print(std::ostream& out) const out << "{"; if (sz > 0) { unsigned int j = 0; - for (const MultiDimInter& mdi : _pieces) { + for (const MultiDimInter& mdi : _pieces) { if (j < sz - 1) { - out << mdi << ", "; + out << mdi << ", "; } else { out << mdi; } @@ -172,7 +161,7 @@ IntTuple UnorderedSet::minElem() const for (const MultiDimInter& mdi : _pieces) { result = std::min(result, mdi.minElem()); } - + return result; } @@ -183,12 +172,11 @@ IntTuple UnorderedSet::maxElem() const for (const MultiDimInter& mdi : _pieces) { result = std::max(result, mdi.maxElem()); } - + return result; } -UnorderedSet intersectionEpilogue(const UnorderedSet::MDIUnordCollection& lhs - , const UnorderedSet::MDIUnordCollection& rhs) +UnorderedSet intersectionEpilogue(const UnorderedSet::MDIUnordCollection& lhs, const UnorderedSet::MDIUnordCollection& rhs) { // General case UnorderedSet result; @@ -214,24 +202,15 @@ UnorderedSet UnorderedSet::intersection(const UnorderedSet& other) const return intersectionEpilogue(_pieces, other._pieces); } -UnorderedSet UnorderedSet::cup(const UnorderedSet& other) const & -{ - return UnorderedSet{*this}.cup(other); -} +UnorderedSet UnorderedSet::cup(const UnorderedSet& other) const& { return UnorderedSet{*this}.cup(other); } -UnorderedSet UnorderedSet::cup(const UnorderedSet& other) && -{ - return std::move(*this).cup(UnorderedSet{other}); -} +UnorderedSet UnorderedSet::cup(const UnorderedSet& other) && { return std::move(*this).cup(UnorderedSet{other}); } -UnorderedSet UnorderedSet::cup(UnorderedSet&& other) const & -{ - return UnorderedSet{*this}.cup(std::move(other)); -} +UnorderedSet UnorderedSet::cup(UnorderedSet&& other) const& { return UnorderedSet{*this}.cup(std::move(other)); } UnorderedSet UnorderedSet::cup(UnorderedSet&& other) && { - if (isEmpty()) { + if (isEmpty()) { return std::move(other); } @@ -241,8 +220,7 @@ UnorderedSet UnorderedSet::cup(UnorderedSet&& other) && if (maxElem() < other.minElem() || other.maxElem() < minElem()) { MDIUnordCollection result = std::move(_pieces); - result.insert(result.end(), std::make_move_iterator(other._pieces.begin()) - , std::make_move_iterator(other._pieces.end())); + result.insert(result.end(), std::make_move_iterator(other._pieces.begin()), std::make_move_iterator(other._pieces.end())); return UnorderedSet{std::move(result)}; } @@ -366,24 +344,15 @@ std::size_t UnorderedSet::arity() const return _pieces.begin()->arity(); } -UnorderedSet UnorderedSet::disjointCup(const UnorderedSet& other) const & -{ - return UnorderedSet{*this}.disjointCup(other); -} +UnorderedSet UnorderedSet::disjointCup(const UnorderedSet& other) const& { return UnorderedSet{*this}.disjointCup(other); } -UnorderedSet UnorderedSet::disjointCup(const UnorderedSet& other) && -{ - return std::move(*this).disjointCup(UnorderedSet{other}); -} +UnorderedSet UnorderedSet::disjointCup(const UnorderedSet& other) && { return std::move(*this).disjointCup(UnorderedSet{other}); } -UnorderedSet UnorderedSet::disjointCup(UnorderedSet&& other) const & -{ - return UnorderedSet{*this}.disjointCup(std::move(other)); -} +UnorderedSet UnorderedSet::disjointCup(UnorderedSet&& other) const& { return UnorderedSet{*this}.disjointCup(std::move(other)); } UnorderedSet UnorderedSet::disjointCup(UnorderedSet&& other) && { - if (isEmpty()) { + if (isEmpty()) { return std::move(other); } @@ -392,10 +361,8 @@ UnorderedSet UnorderedSet::disjointCup(UnorderedSet&& other) && } MDIUnordCollection result; - result.insert(result.end(), std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(_pieces.end())); - result.insert(result.end(), std::make_move_iterator(other._pieces.begin()) - , std::make_move_iterator(other._pieces.end())); + result.insert(result.end(), std::make_move_iterator(_pieces.begin()), std::make_move_iterator(_pieces.end())); + result.insert(result.end(), std::make_move_iterator(other._pieces.begin()), std::make_move_iterator(other._pieces.end())); return UnorderedSet{std::move(result)}; } @@ -440,8 +407,7 @@ void UnorderedSet::compact() MDIUnordCollection result; if (!isEmpty()) { - MDISet set_result{std::make_move_iterator(_pieces.begin()) - , std::make_move_iterator(_pieces.end())}; + MDISet set_result{std::make_move_iterator(_pieces.begin()), std::make_move_iterator(_pieces.end())}; MDISet to_erase; do { MDISet new_set_result; @@ -479,8 +445,7 @@ void UnorderedSet::compact() // Non-member functions -------------------------------------------------------- -rapidjson::Value toJSON(UnorderedSet s - , rapidjson::Document::AllocatorType& alloc) +rapidjson::Value toJSON(UnorderedSet s, rapidjson::Document::AllocatorType& alloc) { rapidjson::Value mdi_array{rapidjson::kArrayType}; for (const MultiDimInter& mdi : s) { @@ -493,8 +458,8 @@ rapidjson::Value toJSON(UnorderedSet s return result; } -} // namespace detail +} // namespace detail -} // namespace LIB +} // namespace LIB -} // namespace SBG +} // namespace SBG diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 63b560dc..96e82604 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -10,12 +10,11 @@ file(MAKE_DIRECTORY add_subdirectory(eval) add_subdirectory(parser) -#add_subdirectory(partitioner) +add_subdirectory(partitioner) add_subdirectory(performance) add_custom_target(test COMMAND echo "Running Tests." ) -add_dependencies(test run-eval-test run-parser-test) -#add_dependencies(test run-eval-test run-parser-test run-partitioner-test) +add_dependencies(test run-eval-test run-parser-test run-partitioner-test) diff --git a/test/partitioner/CMakeLists.txt b/test/partitioner/CMakeLists.txt index f2e1e78b..f5dcd7c7 100644 --- a/test/partitioner/CMakeLists.txt +++ b/test/partitioner/CMakeLists.txt @@ -14,17 +14,15 @@ target_compile_options(partitioner-test PRIVATE "-Wall") # Link the library target_link_libraries(partitioner-test - PRIVATE - sbg-dev - sbg-part-lib - ${GOOGLE_TEST_LIB} - ${GOOGLE_MOCK_LIB} - pthread + PRIVATE + sbg-part-lib + GTest::gtest + GTest::gmock + GTest::gtest_main + pthread ) -target_include_directories(partitioner-test PRIVATE ${GOOGLE_TEST_INCLUDE} ${PROJECT_SOURCE_DIR}) - -target_link_directories(partitioner-test PRIVATE ${GOOGLE_TEST_INSTALL}/lib) +target_include_directories(partitioner-test PRIVATE ${PROJECT_SOURCE_DIR}) # Set output directory for executable set_target_properties(partitioner-test PROPERTIES diff --git a/test/partitioner/benchmark/run_benchmark.py b/test/partitioner/benchmark/run_benchmark.py new file mode 100755 index 00000000..1b6bf0ac --- /dev/null +++ b/test/partitioner/benchmark/run_benchmark.py @@ -0,0 +1,190 @@ +#!/usr/bin/env python3 +"""Runs sbg-partitioner over a fixed set of models/sizes/partition counts, +checks that each run succeeds, and writes a customSmallerIsBetter JSON file +for github-action-benchmark. + +Usage: + run_benchmark.py --binary build/algorithms/partitioner/sbg-partitioner \ + --output bench.json [--repetitions 3] [--timeout 900] +""" + +import argparse +import json +import re +import statistics +import subprocess +import sys +from pathlib import Path + +REPO_ROOT = Path(__file__).resolve().parents[3] +AIR_CONDITIONERS_DATA_DIR = REPO_ROOT / "test" / "partitioner" / "external_tools" / "data" + +SIZES_PARTS = [ + (1000, 4), + (1000, 8), + (1000, 16), + (10000, 4), + (100000, 4), +] + +TIME_BUILD_RE = re.compile(r"time_to_build_graph\s*=\s*([\d.]+)\s*ms") +TIME_PARTITION_RE = re.compile(r"time_to_partitionate\s*=\s*([\d.]+)\s*ms") + + +def advection2d_filename(size: int) -> str: + # Not read from disk: get_sbg() only parses the size out of the name. + return f"advection2D_{size}.json" + + +def air_conditioners_filename(size: int) -> str: + # Read from disk: build_sb_graph() parses the real json file. + path = AIR_CONDITIONERS_DATA_DIR / f"air_conditioners_{size}.json" + if not path.exists(): + raise FileNotFoundError(f"missing fixture: {path}") + return str(path) + + +def air_conditioners_cont_filename(size: int) -> str: + # Not read from disk: get_sbg() only parses the size out of the name. + return f"air_conditioners_cont_{size}.json" + + +MODELS = [ + ("advection2D", advection2d_filename), + ("air_conditioners", air_conditioners_filename), + ("air_conditioners_cont", air_conditioners_cont_filename), +] + +# (model, size, parts) combinations known not to converge in a reasonable time. +# air_conditioners_cont/1000/16: the KL refinement does not converge within 5+ +# minutes for this tiny model with this many partitions. Tracked separately; +# not a benchmark issue. +EXCLUDED_CASES = { + ("air_conditioners_cont", 1000, 16), +} + + +def build_cases(): + cases = [] + for model_name, filename_fn in MODELS: + for size, parts in SIZES_PARTS: + if (model_name, size, parts) in EXCLUDED_CASES: + continue + cases.append( + { + "model": model_name, + "size": size, + "parts": parts, + "filename_fn": filename_fn, + } + ) + return cases + + +def run_once(binary: str, filename: str, parts: int, timeout: int): + cmd = [binary, "-f", filename, "-p", str(parts)] + try: + result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout) + except subprocess.TimeoutExpired: + return None, f"timed out after {timeout}s" + + if result.returncode != 0: + return None, f"exit code {result.returncode}\nstderr:\n{result.stderr[-4000:]}" + + build_match = TIME_BUILD_RE.search(result.stdout) + partition_match = TIME_PARTITION_RE.search(result.stdout) + if not build_match or not partition_match: + return None, "missing timing output in stdout" + + build_ms = float(build_match.group(1)) + partition_ms = float(partition_match.group(1)) + return (build_ms, partition_ms), None + + +def run_case(binary: str, case: dict, repetitions: int, timeout: int): + name = f"{case['model']} size={case['size']} parts={case['parts']}" + filename = case["filename_fn"](case["size"]) + + runs = [] + for i in range(repetitions): + timings, error = run_once(binary, filename, case["parts"], timeout) + if timings is None: + return { + "name": name, + "ok": False, + "error": f"run {i + 1}/{repetitions} failed: {error}", + } + runs.append(timings) + + totals = [build_ms + partition_ms for build_ms, partition_ms in runs] + median_total = statistics.median(totals) + median_build = statistics.median(b for b, _ in runs) + median_partition = statistics.median(p for _, p in runs) + + return { + "name": name, + "ok": True, + "value": median_total, + "build_ms": median_build, + "partition_ms": median_partition, + "runs": totals, + } + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--binary", required=True, help="path to the sbg-partitioner executable") + parser.add_argument("--output", required=True, help="path to write the benchmark JSON to") + parser.add_argument("--repetitions", type=int, default=3, help="runs per case (median is reported)") + parser.add_argument("--timeout", type=int, default=900, help="timeout in seconds for a single run") + args = parser.parse_args() + + binary = str(Path(args.binary).resolve()) + if not Path(binary).exists(): + print(f"error: binary not found: {binary}", file=sys.stderr) + return 1 + + cases = build_cases() + results = [] + had_failure = False + + for case in cases: + try: + result = run_case(binary, case, args.repetitions, args.timeout) + except FileNotFoundError as e: + result = {"name": f"{case['model']} size={case['size']} parts={case['parts']}", "ok": False, "error": str(e)} + + results.append(result) + if result["ok"]: + print(f"[OK] {result['name']}: {result['value']:.1f} ms (median of {args.repetitions})") + else: + had_failure = True + print(f"[FAIL] {result['name']}: {result['error']}") + + benchmark_entries = [ + { + "name": r["name"], + "unit": "ms", + "value": r["value"], + "extra": f"build={r['build_ms']:.1f}ms partition={r['partition_ms']:.1f}ms runs={r['runs']}", + } + for r in results + if r["ok"] + ] + + output_path = Path(args.output) + output_path.parent.mkdir(parents=True, exist_ok=True) + output_path.write_text(json.dumps(benchmark_entries, indent=2)) + + print() + print(f"Wrote {len(benchmark_entries)}/{len(cases)} successful results to {output_path}") + + if had_failure: + print("error: one or more benchmark cases failed", file=sys.stderr) + return 1 + + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/test/partitioner/external_tools/.gitignore b/test/partitioner/external_tools/.gitignore new file mode 100644 index 00000000..e8260bff --- /dev/null +++ b/test/partitioner/external_tools/.gitignore @@ -0,0 +1,8 @@ +install/ +3rd-party/KaHIP/deploy/ +3rd-party/boost/ +3rd-party/sbg/ +data/ +build/ +*.csv +*.partition diff --git a/test/partitioner/external_tools/3rd-party/CMakeLists.txt b/test/partitioner/external_tools/3rd-party/CMakeLists.txt index d240400d..00377cd4 100644 --- a/test/partitioner/external_tools/3rd-party/CMakeLists.txt +++ b/test/partitioner/external_tools/3rd-party/CMakeLists.txt @@ -1,25 +1,45 @@ set(3RD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}) set(KAHIP_LIB "${CMAKE_INSTALL_PREFIX}/lib/libkahip.a") +set(SBG_LIB "$ENV{HOME}/.local/sbgraph/lib/libsbgraph.a") + +option(BUILD_KAHIP_LIB "Build KaHIP Library" OFF) + +if (BUILD_KAHIP_LIB) + add_custom_command( + OUTPUT ${KAHIP_LIB} + COMMAND ${CMAKE_COMMAND} -E echo "Building KaHIP." + COMMAND ${CMAKE_COMMAND} -E tar xvzf ${3RD_PARTY_DIR}/KaHIP/KaHIP-3.16.tar.gz + COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/KaHIP-3.16 && ./compile_withcmake.sh + + COMMAND ${CMAKE_COMMAND} -E echo "Installing KaHIP." + COMMAND ${CMAKE_COMMAND} -E rm -rf ${3RD_PARTY_DIR}/KaHIP/deploy + COMMAND mv ${CMAKE_CURRENT_BINARY_DIR}/KaHIP-3.16/deploy ${3RD_PARTY_DIR}/KaHIP/ + ) +else() + add_custom_command( + OUTPUT ${KAHIP_LIB} + COMMAND ${CMAKE_COMMAND} -E echo "Skipping KaHIP installation." + ) + +endif() + +set(SBG_LIB_COPY "${3RD_PARTY_DIR}/sbg/libsbgraph.a") add_custom_command( - OUTPUT ${KAHIP_LIB} - COMMAND ${CMAKE_COMMAND} -E echo "Building KaHIP." - COMMAND ${CMAKE_COMMAND} -E tar xvzf ${3RD_PARTY_DIR}/KaHIP/KaHIP-3.16.tar.gz - COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/KaHIP-3.16 && ./compile_withcmake.sh - COMMAND ${CMAKE_COMMAND} -E echo "Installing KaHIP." - COMMAND ${CMAKE_COMMAND} -E rm -rf ${3RD_PARTY_DIR}/KaHIP/deploy - COMMAND mv ${CMAKE_CURRENT_BINARY_DIR}/KaHIP-3.16/deploy ${3RD_PARTY_DIR}/KaHIP/ - - COMMAND ${CMAKE_COMMAND} -E echo "Installing SBG Library." + OUTPUT ${SBG_LIB_COPY} + DEPENDS ${SBG_LIB} + COMMAND ${CMAKE_COMMAND} -E echo "Copying SBG Library to ${3RD_PARTY_DIR}/sbg" COMMAND ${CMAKE_COMMAND} -E rm -rf ${3RD_PARTY_DIR}/sbg - COMMAND mkdir -p ${3RD_PARTY_DIR}/sbg - COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/../../../../install/lib/* ${3RD_PARTY_DIR}/sbg + COMMAND ${CMAKE_COMMAND} -E make_directory ${3RD_PARTY_DIR}/sbg + COMMAND cp $ENV{HOME}/.local/sbgraph/lib/*.a ${3RD_PARTY_DIR}/sbg ) + add_custom_target( 3rd-party-libs - DEPENDS ${KAHIP_LIB} + DEPENDS ${KAHIP_LIB} + DEPENDS ${SBG_LIB_COPY} ) add_dependencies(graph-partitioner 3rd-party-libs) diff --git a/test/partitioner/external_tools/CMakeLists.txt b/test/partitioner/external_tools/CMakeLists.txt index fd8d5025..a44ddcf9 100644 --- a/test/partitioner/external_tools/CMakeLists.txt +++ b/test/partitioner/external_tools/CMakeLists.txt @@ -4,9 +4,9 @@ project(ExtPartitioners) set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/install) -# Set default build type to Debug if not specified +# Set default build type to Release if not specified if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type" FORCE) + set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE) endif() set(CMAKE_CXX_STANDARD 17) @@ -17,9 +17,9 @@ add_library(graph-partitioner STATIC) set_target_properties(graph-partitioner PROPERTIES DEBUG_POSTFIX "d") -target_include_directories(graph-partitioner PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories(graph-partitioner PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} $ENV{HOME}/.local/sbgraph/include) -target_include_directories(graph-partitioner PRIVATE 3rd-party/KaHIP/deploy ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/../../../) +target_include_directories(graph-partitioner PRIVATE 3rd-party/KaHIP/deploy ${PROJECT_SOURCE_DIR}) target_sources( graph-partitioner @@ -53,8 +53,8 @@ target_link_libraries( scotcherr kahip.a sbg-part-lib - sbg-util-lib - sbg-dev + sbgraph + boost_program_options ) # install diff --git a/test/partitioner/external_tools/create_latex_table.py b/test/partitioner/external_tools/create_latex_table.py new file mode 100644 index 00000000..134d5f8a --- /dev/null +++ b/test/partitioner/external_tools/create_latex_table.py @@ -0,0 +1,47 @@ +import pandas as pd +import os +import sys + +def generate_latex_table(csv_file): + df = pd.read_csv(csv_file) + + # We take the first two descriptive columns, Average, and Standard_Deviation + # Based on your previous files: + # col 0: ID (N), col 1: Type, col -2: Average, col -1: Std Dev + + latex_lines = [] + + # Header Construction + latex_lines.append(r"\begin{table}[ht]") + latex_lines.append(r"\centering") + latex_lines.append(r"\caption{Summary of Results: Mean and Standard Deviation}") + latex_lines.append(r"\label{tab:processed_results}") + + # Defining the tabular environment (4 columns: N, Type, Mean, Std) + latex_lines.append(r"\begin{tabular}{lccc}") + latex_lines.append(r"\toprule") + latex_lines.append(r"\begin{tabular}[c]{@{}l@{}}Model \\ Name\end{tabular} & \begin{tabular}[c]{@{}l@{}}Number of \\ Partitions\end{tabular} & \begin{tabular}[c]{@{}l@{}}Average \\ (ms)\end{tabular} & \begin{tabular}[c]{@{}l@{}}Standard deviation \\ (ms)\end{tabular} \\") + latex_lines.append(r"\midrule") + + # Row Processing + for index, row in df.iterrows(): + # Clean values: format numbers to 2 decimal places + n_val = str(row.iloc[0]) + desc_val = str(row.iloc[1]) + avg_val = f"{row['Average']:.2f}" + std_val = f"{row['Standard_Deviation']:.2f}" + + # Add a row to the table + latex_lines.append(f" {n_val} & {desc_val} & ${avg_val}$ & ${std_val}$ \\\\") + + # Footer + latex_lines.append(r"\bottomrule") + latex_lines.append(r"\end{tabular}") + latex_lines.append(r"\end{table}") + + return "\n".join(latex_lines) + +if os.path.exists(sys.argv[1]): + latex_code = generate_latex_table(sys.argv[1]) + print(latex_code) + \ No newline at end of file diff --git a/test/partitioner/external_tools/data/advection_1000.json b/test/partitioner/external_tools/data/advection_1000.json new file mode 100644 index 00000000..2bcbefac --- /dev/null +++ b/test/partitioner/external_tools/data/advection_1000.json @@ -0,0 +1 @@ +{"nodes": [{"id": 1, "interval": [[1, 1]], "lhs": [{"id": "X1", "exp": [[0, 1]], "defs": []}], "rhs": [{"id": "X1", "exp": [[0, 1]], "defs": [1, 2]}]}, {"id": 2, "interval": [[2, 1000]], "lhs": [{"id": "X1", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "X1", "exp": [[1, 0]], "defs": [1, 2]}, {"id": "X1", "exp": [[1, -1]], "defs": [1, 2]}]}]} diff --git a/test/partitioner/external_tools/data/air_conditioners_1000.json b/test/partitioner/external_tools/data/air_conditioners_1000.json new file mode 100644 index 00000000..723dd22c --- /dev/null +++ b/test/partitioner/external_tools/data/air_conditioners_1000.json @@ -0,0 +1 @@ +{"nodes": [{"id": 1, "interval": [[1, 1000]], "lhs": [{"id": "th", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "on", "exp": [[1, 0]], "defs": [2]}, {"id": "noise", "exp": [[1, 0]], "defs": [4]}]}, {"id": 2, "interval": [[1, 1000]], "lhs": [{"id": "ev_1", "exp": [[1, 0]], "defs": []}, {"id": "on", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "th", "exp": [[1, 0]], "defs": [1]}, {"id": "tref", "exp": [[1, 0]], "defs": [3]}]}, {"id": 3, "interval": [[1, 1000]], "lhs": [{"id": "ev_2", "exp": [[1, 0]], "defs": []}, {"id": "tref", "exp": [[1, 0]], "defs": []}, {"id": "nextTref", "exp": [[1, 0]], "defs": []}], "rhs": []}, {"id": 4, "interval": [[1, 1000]], "lhs": [{"id": "ev_3", "exp": [[1, 0]], "defs": []}, {"id": "nextSample", "exp": [[2, 0]], "defs": []}, {"id": "noise", "exp": [[1, 0]], "defs": []}], "rhs": []}]} diff --git a/test/partitioner/external_tools/data/air_conditioners_10000.json b/test/partitioner/external_tools/data/air_conditioners_10000.json new file mode 100644 index 00000000..eadd179a --- /dev/null +++ b/test/partitioner/external_tools/data/air_conditioners_10000.json @@ -0,0 +1 @@ +{"nodes": [{"id": 1, "interval": [[1, 10000]], "lhs": [{"id": "th", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "on", "exp": [[1, 0]], "defs": [2]}, {"id": "noise", "exp": [[1, 0]], "defs": [4]}]}, {"id": 2, "interval": [[1, 10000]], "lhs": [{"id": "ev_1", "exp": [[1, 0]], "defs": []}, {"id": "on", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "th", "exp": [[1, 0]], "defs": [1]}, {"id": "tref", "exp": [[1, 0]], "defs": [3]}]}, {"id": 3, "interval": [[1, 10000]], "lhs": [{"id": "ev_2", "exp": [[1, 0]], "defs": []}, {"id": "tref", "exp": [[1, 0]], "defs": []}, {"id": "nextTref", "exp": [[1, 0]], "defs": []}], "rhs": []}, {"id": 4, "interval": [[1, 10000]], "lhs": [{"id": "ev_3", "exp": [[1, 0]], "defs": []}, {"id": "nextSample", "exp": [[2, 0]], "defs": []}, {"id": "noise", "exp": [[1, 0]], "defs": []}], "rhs": []}]} diff --git a/test/partitioner/external_tools/data/air_conditioners_100000.json b/test/partitioner/external_tools/data/air_conditioners_100000.json new file mode 100644 index 00000000..d848a2cb --- /dev/null +++ b/test/partitioner/external_tools/data/air_conditioners_100000.json @@ -0,0 +1 @@ +{"nodes": [{"id": 1, "interval": [[1, 100000]], "lhs": [{"id": "th", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "on", "exp": [[1, 0]], "defs": [2]}, {"id": "noise", "exp": [[1, 0]], "defs": [4]}]}, {"id": 2, "interval": [[1, 100000]], "lhs": [{"id": "ev_1", "exp": [[1, 0]], "defs": []}, {"id": "on", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "th", "exp": [[1, 0]], "defs": [1]}, {"id": "tref", "exp": [[1, 0]], "defs": [3]}]}, {"id": 3, "interval": [[1, 100000]], "lhs": [{"id": "ev_2", "exp": [[1, 0]], "defs": []}, {"id": "tref", "exp": [[1, 0]], "defs": []}, {"id": "nextTref", "exp": [[1, 0]], "defs": []}], "rhs": []}, {"id": 4, "interval": [[1, 100000]], "lhs": [{"id": "ev_3", "exp": [[1, 0]], "defs": []}, {"id": "nextSample", "exp": [[2, 0]], "defs": []}, {"id": "noise", "exp": [[1, 0]], "defs": []}], "rhs": []}]} diff --git a/test/partitioner/external_tools/data/air_conditioners_1000000.json b/test/partitioner/external_tools/data/air_conditioners_1000000.json new file mode 100644 index 00000000..79e70f75 --- /dev/null +++ b/test/partitioner/external_tools/data/air_conditioners_1000000.json @@ -0,0 +1 @@ +{"nodes": [{"id": 1, "interval": [[1, 1000000]], "lhs": [{"id": "th", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "on", "exp": [[1, 0]], "defs": [2]}, {"id": "noise", "exp": [[1, 0]], "defs": [4]}]}, {"id": 2, "interval": [[1, 1000000]], "lhs": [{"id": "ev_1", "exp": [[1, 0]], "defs": []}, {"id": "on", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "th", "exp": [[1, 0]], "defs": [1]}, {"id": "tref", "exp": [[1, 0]], "defs": [3]}]}, {"id": 3, "interval": [[1, 1000000]], "lhs": [{"id": "ev_2", "exp": [[1, 0]], "defs": []}, {"id": "tref", "exp": [[1, 0]], "defs": []}, {"id": "nextTref", "exp": [[1, 0]], "defs": []}], "rhs": []}, {"id": 4, "interval": [[1, 1000000]], "lhs": [{"id": "ev_3", "exp": [[1, 0]], "defs": []}, {"id": "nextSample", "exp": [[2, 0]], "defs": []}, {"id": "noise", "exp": [[1, 0]], "defs": []}], "rhs": []}]} diff --git a/test/partitioner/external_tools/data/air_conditioners_cont_4_1000.json b/test/partitioner/external_tools/data/air_conditioners_cont_4_1000.json new file mode 100644 index 00000000..11c6fc55 --- /dev/null +++ b/test/partitioner/external_tools/data/air_conditioners_cont_4_1000.json @@ -0,0 +1 @@ +{"nodes": [{"id": 1, "interval": [[1, 1000]], "lhs": [{"id": "th", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "on", "exp": [[1, 0]], "defs": [10, 11, 12, 13]}, {"id": "noise", "exp": [[1, 0]], "defs": [9]}, {"id": "th", "exp": [[1, 0]], "defs": [1]}]}, {"id": 2, "interval": [[1, 1]], "lhs": [{"id": "ierr", "exp": [[0, 1]], "defs": []}], "rhs": [{"id": "pref", "exp": [[0, 1]], "defs": [6, 7]}, {"id": "ptotals", "exp": [[0, 1]], "defs": [8]}]}, {"id": 3, "interval": [[1, 1]], "lhs": [{"id": "ptotal", "exp": [[0, 1]], "defs": []}], "rhs": []}, {"id": 4, "interval": [[1, 4]], "lhs": [{"id": "ev_1", "exp": [[1, 0]], "defs": []}, {"id": "partSample", "exp": [[1, 0]], "defs": []}, {"id": "update", "exp": [[1, 0]], "defs": []}, {"id": "sendPartTotal", "exp": [[1, 0]], "defs": []}, {"id": "partTotal", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "partSample", "exp": [[1, 0]], "defs": [4]}, {"id": "partTotal", "exp": [[1, 0]], "defs": [4, 10, 11, 12, 13]}]}, {"id": 5, "interval": [[1, 4]], "lhs": [{"id": "ev_2", "exp": [[1, 0]], "defs": []}, {"id": "ptotal", "exp": [[1, 0]], "defs": []}, {"id": "update", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "update", "exp": [[1, 0]], "defs": [4, 5]}, {"id": "ptotal", "exp": [[1, 0]], "defs": [3, 5]}, {"id": "sendPartTotal", "exp": [[1, 0]], "defs": [4]}]}, {"id": 6, "interval": [[1, 1]], "lhs": [{"id": "ev_3", "exp": [[0, 1]], "defs": []}, {"id": "pref", "exp": [[0, 1]], "defs": []}], "rhs": []}, {"id": 7, "interval": [[1, 1]], "lhs": [{"id": "ev_4", "exp": [[0, 1]], "defs": []}, {"id": "pref", "exp": [[0, 1]], "defs": []}], "rhs": []}, {"id": 8, "interval": [[1, 1]], "lhs": [{"id": "ev_5", "exp": [[0, 1]], "defs": []}, {"id": "nextSample", "exp": [[0, 1]], "defs": []}, {"id": "ptotals", "exp": [[0, 1]], "defs": []}, {"id": "dtref", "exp": [[0, 1]], "defs": []}], "rhs": [{"id": "nextSample", "exp": [[0, 1]], "defs": [8]}, {"id": "ptotal", "exp": [[0, 1]], "defs": [3, 5]}, {"id": "ptotals", "exp": [[0, 1]], "defs": [8]}, {"id": "pref", "exp": [[0, 1]], "defs": [6, 7]}, {"id": "ierr", "exp": [[0, 1]], "defs": [2]}]}, {"id": 9, "interval": [[1, 1000]], "lhs": [{"id": "ev_6", "exp": [[1, 0]], "defs": []}, {"id": "sampleNoise", "exp": [[1, 0]], "defs": []}, {"id": "inputs", "exp": [[1, 0]], "defs": []}, {"id": "noise", "exp": [[1, 0]], "defs": []}], "rhs": [{"id": "sampleNoise", "exp": [[1, 0]], "defs": [10]}, {"id": "inputs", "exp": [[1, 0]], "defs": [10]}]}, {"id": 10, "interval": [[1, 250]], "lhs": [{"id": "ev_7", "exp": [[1, 0]], "defs": []}, {"id": "on", "exp": [[1, 0]], "defs": []}, {"id": "partTotal", "exp": [[0, 1]], "defs": []}], "rhs": [{"id": "th", "exp": [[1, 0]], "defs": [1]}, {"id": "on", "exp": [[1, 0]], "defs": [10, 11, 12, 13]}, {"id": "dtref", "exp": [[0, 1]], "defs": [8]}]}, {"id": 11, "interval": [[251, 500]], "lhs": [{"id": "ev_8", "exp": [[1, 0]], "defs": []}, {"id": "on", "exp": [[1, 0]], "defs": []}, {"id": "partTotal", "exp": [[0, 2]], "defs": []}], "rhs": [{"id": "th", "exp": [[1, 0]], "defs": [1]}, {"id": "on", "exp": [[1, 0]], "defs": [10, 11, 12, 13]}, {"id": "dtref", "exp": [[0, 1]], "defs": [8]}]}, {"id": 12, "interval": [[501, 750]], "lhs": [{"id": "ev_9", "exp": [[1, 0]], "defs": []}, {"id": "on", "exp": [[1, 0]], "defs": []}, {"id": "partTotal", "exp": [[0, 3]], "defs": []}], "rhs": [{"id": "th", "exp": [[1, 0]], "defs": [1]}, {"id": "on", "exp": [[1, 0]], "defs": [10, 11, 12, 13]}, {"id": "dtref", "exp": [[0, 1]], "defs": [8]}]}, {"id": 13, "interval": [[751, 1000]], "lhs": [{"id": "ev_10", "exp": [[1, 0]], "defs": []}, {"id": "on", "exp": [[1, 0]], "defs": []}, {"id": "partTotal", "exp": [[0, 4]], "defs": []}], "rhs": [{"id": "th", "exp": [[1, 0]], "defs": [1]}, {"id": "on", "exp": [[1, 0]], "defs": [10, 11, 12, 13]}, {"id": "dtref", "exp": [[0, 1]], "defs": [8]}]}]} diff --git a/test/partitioner/external_tools/experiments/adv_dif_reac/generate_advection.py b/test/partitioner/external_tools/experiments/adv_dif_reac/generate_advection.py new file mode 100644 index 00000000..2912ee7b --- /dev/null +++ b/test/partitioner/external_tools/experiments/adv_dif_reac/generate_advection.py @@ -0,0 +1,65 @@ +import argparse +import json + + +def generate_json(elements: int) -> str: + json_data = { + "nodes": [ + { + "id": 1, + "interval": [[1, 1]], + "lhs": [ + { + "id": "X1", + "exp": [[0, 1]], + "defs": [] + } + ], + "rhs": [ + { + "id": "X1", + "exp": [[0, 1]], + "defs": [1, 2] + } + ] + }, + { + "id": 2, + "interval":[ [2, int(elements)]], + "lhs": [ + { + "id": "X1", + "exp": [[1, 0]], + "defs": [] + } + ], + "rhs": [ + { + "id": "X1", + "exp": [[1, 0]], + "defs": [1, 2] + }, + { + "id": "X1", + "exp": [[1, -1]], + "defs": [1, 2] + } + ] + } + ] + } + + return json.dumps(json_data) + + +def main(): + parser = argparse.ArgumentParser(description='Generate SBG JSON input.') + parser.add_argument('size', type=int, help='The number of elements.') + + args = parser.parse_args() + + json_data = generate_json(args.size) + print(json_data) + +if __name__ == '__main__': + main() diff --git a/test/partitioner/external_tools/experiments/airconds/generate_air_conditioners.py b/test/partitioner/external_tools/experiments/airconds/generate_air_conditioners.py new file mode 100644 index 00000000..85411b43 --- /dev/null +++ b/test/partitioner/external_tools/experiments/airconds/generate_air_conditioners.py @@ -0,0 +1,125 @@ +import argparse +import json + + +def generate_json(number_of_air_conditioners: int) -> str: + json_data = { + "nodes": [ + { + "id": 1, + "interval": [[1, int(number_of_air_conditioners)]], + "lhs": [ + { + "id": "th", + "exp": [[1, 0]], + "defs": [] + } + ], + "rhs": [ + { + "id": "on", + "exp": [[1, 0]], + "defs": [2] + }, + { + "id": "noise", + "exp": [[1, 0]], + "defs": [4] + } + ] + }, + { + "id": 2, + "interval": [[1, int(number_of_air_conditioners)]], + "lhs": [ + { + "id": "ev_1", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "on", + "exp": [[1, 0]], + "defs": [] + } + + ], + "rhs": [ + { + "id": "th", + "exp": [[1, 0]], + "defs": [1] + }, + { + "id": "tref", + "exp": [[1, 0]], + "defs": [3] + } + ] + }, + { + "id": 3, + "interval": [[1, int(number_of_air_conditioners)]], + "lhs": [ + { + "id": "ev_2", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "tref", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "nextTref", + "exp": [[1, 0]], + "defs": [] + } + + ], + "rhs": [ + ] + }, + { + "id": 4, + "interval": [[1, int(number_of_air_conditioners)]], + "lhs": [ + { + "id": "ev_3", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "nextSample", + "exp": [[2, 0]], + "defs": [] + }, + { + "id": "noise", + "exp": [[1, 0]], + "defs": [] + } + + ], + "rhs": [ + ] + } + + ] + } + + return json.dumps(json_data) + + +def main(): + parser = argparse.ArgumentParser(description='Generate SBG JSON input from spiking neurons Modelica model.') + parser.add_argument('size', type=int, help='The number of air conditioners population.') + + args = parser.parse_args() + + json_data = generate_json(args.size) + print(json_data) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/test/partitioner/external_tools/experiments/airconds_cont/generate_air_conditioners_cont.py b/test/partitioner/external_tools/experiments/airconds_cont/generate_air_conditioners_cont.py new file mode 100644 index 00000000..769368af --- /dev/null +++ b/test/partitioner/external_tools/experiments/airconds_cont/generate_air_conditioners_cont.py @@ -0,0 +1,355 @@ +import argparse +import json + +def generate_section_controller_records(sections, airs_per_section, enumerate_sections, on_definitions): + records = [] + # Generate list for partial total node definition + for section_nbr in range(sections): + record = { + "id": int(10 + section_nbr), + "interval": [[int(airs_per_section * section_nbr + 1), int(airs_per_section + airs_per_section * section_nbr)]], + "lhs": [ + { + "id": "ev_" + str(7 + int(section_nbr)), + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "on", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "partTotal", + "exp": [[0, int(section_nbr + 1)]], + "defs": [] + } + ], + "rhs": [ + { + "id": "th", + "exp": [[1, 0]], + "defs": [1] + }, + { + "id": "on", + "exp": [[1, 0]], + "defs": on_definitions + }, + { + "id": "dtref", + "exp": [[0, 1]], + "defs": [8] + } + ] + } + records.append(record) + + return json.dumps(records) + +def generate_json(sections, size): + """ + Generates a JSON file input for the air conditioners with controller Modelica model. + + Parameters: + sections (int): The number of sections in which the air conditioners population is divided. + size (int): Size of the air conditioners population. + + Returns: + str: A JSON-formatted string for air conditioners with controller Modelica model used as SBG Partitioner input. + """ + airs_per_section = size/sections + enumerate_sections = [int(4)] + [int(10 + i) for i in range(sections)] + on_definitions = [int(10 + i) for i in range(sections)] + data = { + "nodes": [ + { + "id": 1, + "interval": [[1, size]], + "lhs": [ + { + "id": "th", + "exp": [[1, 0]], + "defs": [] + } + ], + "rhs": [ + { + "id": "on", + "exp": [[1, 0]], + "defs": on_definitions + }, + { + "id": "noise", + "exp": [[1, 0]], + "defs": [9] + }, + { + "id": "th", + "exp": [[1, 0]], + "defs": [1] + } + ] + }, + { + "id": 2, + "interval": [[1, 1]], + "lhs": [ + { + "id": "ierr", + "exp": [[0, 1]], + "defs": [] + } + ], + "rhs": [ + { + "id": "pref", + "exp": [[0, 1]], + "defs": [6, 7] + }, + { + "id": "ptotals", + "exp": [[0, 1]], + "defs": [8] + } + ] + }, + { + "id": 3, + "interval": [[1, 1]], + "lhs": [ + { + "id": "ptotal", + "exp": [[0, 1]], + "defs": [] + } + ], + "rhs": [] + }, + { + "id": 4, + "interval": [[1, sections]], + "lhs": [ + { + "id": "ev_1", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "partSample", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "update", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "sendPartTotal", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "partTotal", + "exp": [[1, 0]], + "defs": [] + } + ], + "rhs": [ + { + "id": "partSample", + "exp": [[1, 0]], + "defs": [4] + }, + { + "id": "partTotal", + "exp": [[1, 0]], + "defs": enumerate_sections + } + ] + }, + { + "id": 5, + "interval": [[1, sections]], + "lhs": [ + { + "id": "ev_2", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "ptotal", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "update", + "exp": [[1, 0]], + "defs": [] + } + ], + "rhs": [ + { + "id": "update", + "exp": [[1, 0]], + "defs": [4, 5] + }, + { + "id": "ptotal", + "exp": [[1, 0]], + "defs": [3, 5] + }, + { + "id": "sendPartTotal", + "exp": [[1, 0]], + "defs": [4] + } + ] + }, + { + "id": 6, + "interval": [[1, 1]], + "lhs": [ + { + "id": "ev_3", + "exp": [[0, 1]], + "defs": [] + }, + { + "id": "pref", + "exp": [[0, 1]], + "defs": [] + } + ], + "rhs": [] + }, + { + "id": 7, + "interval": [[1, 1]], + "lhs": [ + { + "id": "ev_4", + "exp": [[0, 1]], + "defs": [] + }, + { + "id": "pref", + "exp": [[0, 1]], + "defs": [] + } + ], + "rhs": [] + }, + { + "id": 8, + "interval": [[1, 1]], + "lhs": [ + { + "id": "ev_5", + "exp": [[0, 1]], + "defs": [] + }, + { + "id": "nextSample", + "exp": [[0, 1]], + "defs": [] + }, + { + "id": "ptotals", + "exp": [[0, 1]], + "defs": [] + }, + { + "id": "dtref", + "exp": [[0, 1]], + "defs": [] + } + ], + "rhs": [ + { + "id": "nextSample", + "exp": [[0, 1]], + "defs": [8] + }, + { + "id": "ptotal", + "exp": [[0, 1]], + "defs": [3, 5] + }, + { + "id": "ptotals", + "exp": [[0, 1]], + "defs": [8] + }, + { + "id": "pref", + "exp": [[0, 1]], + "defs": [6, 7] + }, + { + "id": "ierr", + "exp": [[0, 1]], + "defs": [2] + } + ] + }, + { + "id": 9, + "interval": [[1, size]], + "lhs": [ + { + "id": "ev_6", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "sampleNoise", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "inputs", + "exp": [[1, 0]], + "defs": [] + }, + { + "id": "noise", + "exp": [[1, 0]], + "defs": [] + } + ], + "rhs": [ + { + "id": "sampleNoise", + "exp": [[1, 0]], + "defs": [10] + }, + { + "id": "inputs", + "exp": [[1, 0]], + "defs": [10] + } + ] + } + ] + } + + # Concatenate section controller records + section_records = json.loads(generate_section_controller_records(sections, airs_per_section, enumerate_sections, on_definitions)) + data["nodes"].extend(section_records) + + return json.dumps(data) + +def main(): + parser = argparse.ArgumentParser(description='Generate SBG JSON input from air conditioners with controller Modelica model.') + parser.add_argument('sections', type=int, help='The number of sections for air conditioners controllers.') + parser.add_argument('size', type=int, help='The size of the air conditioners population.') + + args = parser.parse_args() + + result = generate_json(args.sections, args.size) + print(result) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/test/partitioner/external_tools/graph_partitioner.cpp b/test/partitioner/external_tools/graph_partitioner.cpp index 20d88e5d..6ec4a64c 100644 --- a/test/partitioner/external_tools/graph_partitioner.cpp +++ b/test/partitioner/external_tools/graph_partitioner.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include @@ -37,17 +36,23 @@ #include "graph_partitioner.hpp" #include +#include +#include +#include #include -constexpr const char *VALID_PARTITION_METHODS = "{ Scotch, Metis, HMetis, Kahip}"; +constexpr const char *VALID_PARTITION_METHODS = "{ Scotch, Metis, HMetis, Kahip, SBG }"; // Initialize the map outside the function static const std::unordered_map PARTITION_METHOD_MAP = {{"Metis", PartitionMethod::Metis}, {"HMetis", PartitionMethod::HMetis}, {"Scotch", PartitionMethod::Scotch}, - {"Kahip", PartitionMethod::Kahip}}; + {"Kahip", PartitionMethod::Kahip}, + {"SBG", PartitionMethod::SBG}}; -GraphPartitioner::GraphPartitioner(const std::string &name) : _name(name) { generateInputGraph(); } +static std::unique_ptr sbg_graph; + +GraphPartitioner::GraphPartitioner(const std::string &name, unsigned int parts) : _name(name), _nbr_parts(parts) { generateInputGraph(); } std::string GraphPartitioner::validPartitionMethodsStr() { @@ -63,21 +68,69 @@ std::string GraphPartitioner::validPartitionMethodsStr() return valid_methods.str(); } -Partition GraphPartitioner::createPartition(const std::string &partition_method_name, unsigned int partitions) +static void write_node_by_partition(const sbg_partitioner::PartitionMap &partition, const SBG::LIB::WeightedSBGraph &sb_graph, + const std::string &partition_method_name) { + std::cout << "writing results of SBG to " << partition_method_name << std::endl; + auto nodes = sbg_partitioner::to_vector(sb_graph.V()); + + sbg_partitioner::sort_partition_intervals(nodes); + + // expand it and write it + std::ofstream output_file(partition_method_name); + std::vector partition_vector(sb_graph.V().cardinal(), 0); + + if (sb_graph.V().arity() == 2) { + auto v0 = sbg_partitioner::to_vector(sb_graph.V())[0]; + size_t row_size = v0.maxElem()[0] - v0.minElem()[0] + 1; + for (size_t i = 0; i < partition.size(); i++) { + const auto &p = partition.at(i); + for (const auto &set : p) { + for (const auto &piece : sbg_partitioner::to_vector(set)) { + for (size_t row = piece.minElem()[0]; row <= piece.maxElem()[0]; row++) { + for (size_t col = piece.minElem()[1]; col <= piece.maxElem()[1]; col++) { + auto val = row * row_size + col; + partition_vector[val] = i; + } + } + } + } + } + } else { + for (unsigned i = 0; i < partition.size(); i++) { + for (const auto &set : partition.at(i)) { + for (const auto &piece : sbg_partitioner::to_vector(set)) { + for (size_t val = piece.minElem()[0]; val <= piece.maxElem()[0]; val++) { + partition_vector[val] = i; + } + } + } + } + } + + for_each(partition_vector.begin(), partition_vector.end(), [&output_file](const auto val) { output_file << val << "\n"; }); + std::cout << "parts ok" << std::endl; +} + +std::tuple> GraphPartitioner::createPartition(const std::string &partition_method_name, + unsigned int partitions, bool save_to_file) +{ + std::cout << "GraphPartitioner::createPartition " << partition_method_name << std::endl; Partition partition; PartitionMethod partition_method = partitionMethod(partition_method_name); if (partition_method == PartitionMethod::Unknown) { std::cerr << "Unknown partition method, valid values are: " << validPartitionMethodsStr() << std::endl; - return partition; + return {partition, std::chrono::duration()}; } partition.resize(_nbr_vtxs); _nbr_parts = partitions; - partition.resize(_nbr_vtxs); + // just in case we are using sbg + sbg_partitioner::PartitionMap sbg_partitions = {}; + sbg_partitions.resize(_nbr_parts); auto start = std::chrono::high_resolution_clock::now(); @@ -87,7 +140,8 @@ Partition GraphPartitioner::createPartition(const std::string &partition_method_ partitionUsingMetis(partition); break; case PartitionMethod::HMetis: - partitionUsingHMetis(partition); + // for now, disabled + // partitionUsingHMetis(partition); break; case PartitionMethod::Scotch: partitionUsingScotch(partition); @@ -95,6 +149,9 @@ Partition GraphPartitioner::createPartition(const std::string &partition_method_ case PartitionMethod::Kahip: partitionUsingKaHip(partition); break; + case PartitionMethod::SBG: + partitionUsingSBG(sbg_partitions); + break; default: break; } @@ -103,10 +160,20 @@ Partition GraphPartitioner::createPartition(const std::string &partition_method_ auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration duration = end - start; - std::cout << "Partition Time: " << duration.count() << " seconds." << std::endl; + std::cout << "Partition Time: " << std::chrono::duration_cast(duration).count() << " miliseconds." + << std::endl; - savePartitionToFile(partition, partition_method_name); - return partition; + if (save_to_file) { + const std::string fileName = + _name + "-" + partition_method_name + "-" + std::to_string(_nbr_parts) + "-" + std::to_string(_nbr_vtxs) + ".partition"; + if (partition_method == PartitionMethod::SBG) { + write_node_by_partition(sbg_partitions, *sbg_graph, fileName); + } else { + savePartitionToFile(partition, fileName); + } + } + + return {partition, duration}; } bool GraphPartitioner::endsWithJson() { return _name.size() >= 5 && _name.compare(_name.size() - 5, 5, ".json") == 0; } @@ -120,10 +187,56 @@ void GraphPartitioner::generateInputGraph() } } +namespace { +int get_air_conditioners_controller_size(const std::string &name) +{ + size_t last_underscore = name.find_last_of('_'); + size_t last_dot = name.find_last_of('.'); + + if (last_underscore != std::string::npos && last_dot != std::string::npos) { + // Extract the string between '_' and '.' + std::string size_str = name.substr(last_underscore + 1, last_dot - last_underscore - 1); + int size = std::stoi(size_str); + return size; + } + + std::cerr << "the file does not have the expected format" << std::endl; + throw 1; +} +} // namespace + void GraphPartitioner::readGraphFromJson() { - auto sb_graph = sbg_partitioner::build_sb_graph(_name); - readGraphFromSBG(sb_graph); + std::cout << "GraphPartitioner::readGraphFromJson" << std::endl; + if (_name.find("air_conditioners_cont") != std::string::npos) { + std::cout << "Creating graph for air conditioners with controller" << std::endl; + auto size = get_air_conditioners_controller_size(_name); + auto temp_sbg_graph = sbg_partitioner::create_air_conditioners_with_controller_graph(size, _nbr_parts, true); + + { + auto new_vertices = sbg_partitioner::split_sets_according_to_relations(temp_sbg_graph); + auto map1 = temp_sbg_graph.map1(); + map1.compact(); + auto map2 = temp_sbg_graph.map2(); + map2.compact(); + sbg_graph.reset(new SBG::LIB::WeightedSBGraph(new_vertices, SBG::LIB::PWMap(), map1, map2, SBG::LIB::PWMap())); + } + + // sbg_graph.reset(new SBG::LIB::WeightedSBGraph(temp_sbg_graph)); + + } else if (_name.find("advection2D") != std::string::npos) { + std::cout << "Creating graph for air conditioners with controller" << std::endl; + auto size = get_air_conditioners_controller_size(_name); + auto temp_sbg_graph = sbg_partitioner::create_advection2D_graph(size); + sbg_graph.reset(new SBG::LIB::WeightedSBGraph(temp_sbg_graph)); + } else { + std::cout << "Creating graph from file " << _name << std::endl; + auto temp_sbg_graph = sbg_partitioner::build_sb_graph(_name); + sbg_graph.reset(new SBG::LIB::WeightedSBGraph(temp_sbg_graph)); + } + std::cout << *sbg_graph << std::endl; + + readGraphFromSBG(); } void GraphPartitioner::readGraph() @@ -188,10 +301,9 @@ PartitionMethod GraphPartitioner::partitionMethod(const std::string &partition_m return PartitionMethod::Unknown; } -void GraphPartitioner::savePartitionToFile(const Partition &partition, const std::string &method_name) const +void GraphPartitioner::savePartitionToFile(const Partition &partition, const std::string &file_name) const { - std::string fileName = _name + "-" + method_name + "-" + std::to_string(_nbr_parts) + "-" + std::to_string(_nbr_vtxs) + ".partition"; - std::ofstream file(fileName); + std::ofstream file(file_name); if (file.is_open()) { for (const auto &value : partition.values) { file << value << std::endl; @@ -207,13 +319,17 @@ void GraphPartitioner::partitionUsingMetis(Partition &partition) std::array options; METIS_SetDefaultOptions(options.data()); - options[METIS_OPTION_CONTIG] = 1; + options[METIS_OPTION_CONTIG] = 0; options[METIS_OPTION_PTYPE] = METIS_PTYPE_KWAY; options[METIS_OPTION_OBJTYPE] = METIS_OBJTYPE_VOL; options[METIS_OPTION_SEED] = 1; - METIS_PartGraphKway(&_nbr_vtxs, &ncon, _xadj.data(), _adjncy.data(), _vwgt.data(), nullptr, _ewgt.data(), &_nbr_parts, nullptr, nullptr, - options.data(), &edgecut, partition.values.data()); + auto result = METIS_PartGraphKway(&_nbr_vtxs, &ncon, _xadj.data(), _adjncy.data(), _vwgt.data(), nullptr, _ewgt.data(), &_nbr_parts, + nullptr, nullptr, options.data(), &edgecut, partition.values.data()); + if (result != METIS_OK) { + std::cerr << "There was an error in METIS" << std::endl; + throw 1; + } } void GraphPartitioner::partitionUsingHMetis(Partition &partition) @@ -274,34 +390,29 @@ void GraphPartitioner::readPartitionFile(const std::string &file_name, Partition void GraphPartitioner::partitionUsingScotch(Partition &partition) { - SCOTCH_Graph *graph_sc = SCOTCH_graphAlloc(); - if (graph_sc == nullptr) { - std::cerr << "Error allocating graph" << std::endl; - return; - } + SCOTCH_Graph graph_sc; + SCOTCH_Strat strat; - SCOTCH_Strat *strat = SCOTCH_stratAlloc(); - if (SCOTCH_stratInit(strat)) { - std::cerr << "Error allocating graph" << std::endl; - return; - } + SCOTCH_graphInit(&graph_sc); + SCOTCH_stratInit(&strat); - if (SCOTCH_stratGraphMapBuild(strat, SCOTCH_STRATDEFAULT, 4, 0.05)) { - std::cerr << "Error allocating graph" << std::endl; - return; + if (SCOTCH_stratGraphMapBuild(&strat, SCOTCH_STRATQUALITY, _nbr_parts, 0.05)) { + std::cerr << "Error building graph partition strategy" << std::endl; + throw 1; } - if (SCOTCH_graphBuild(graph_sc, 0, _nbr_vtxs, _xadj.data(), nullptr, _vwgt.data(), nullptr, _edges, _adjncy.data(), _ewgt.data()) != 0) { + if (SCOTCH_graphBuild(&graph_sc, 0, _nbr_vtxs, _xadj.data(), NULL, NULL, NULL, _edges, _adjncy.data(), NULL) != 0) { std::cerr << "Error: Scotch Graph Build" << std::endl; - return; + throw 1; } - if (SCOTCH_graphPart(graph_sc, _nbr_parts, strat, partition.values.data()) != 0) { + + if (SCOTCH_graphPart(&graph_sc, _nbr_parts, &strat, partition.values.data()) != 0) { std::cerr << "Error: Scotch Graph Partition" << std::endl; - return; + throw 1; } - SCOTCH_stratExit(strat); - SCOTCH_graphFree(graph_sc); + SCOTCH_stratExit(&strat); + SCOTCH_graphExit(&graph_sc); } void GraphPartitioner::partitionUsingKaHip(Partition &partition) @@ -314,51 +425,101 @@ void GraphPartitioner::partitionUsingKaHip(Partition &partition) partition.values.data()); } -void GraphPartitioner::readGraphFromSBG(const SBG::LIB::WeightedSBGraph &sbg_graph) +void GraphPartitioner::partitionUsingSBG(sbg_partitioner::PartitionMap &partitions) { - _nbr_vtxs = sbg_graph.V().cardinal(); - _edges = sbg_graph.E().cardinal(); - // asumming all setpiece are unidimensional and have step 1 - int max_node = -1; - for (const auto &v : sbg_graph.V()) { - if (int(v.begin()[0].end()) > max_node) { - max_node = int(v.begin()[0].end()); + std::cout << "GraphPartitioner::partitionUsingSBG" << std::endl; + partitions = sbg_partitioner::best_initial_partition(*sbg_graph, _nbr_parts, sbg_partitioner::InitialPartitionStrategy::ALL, false); + // std::cout << "chosen partition " << partitions << std::endl; + + sbg_partitioner::kl_sbg_imbalance_partitioner(*sbg_graph, partitions, _imbalance, false); + std::for_each(partitions.begin(), partitions.end(), [](const auto &p) { + std::for_each(p.begin(), p.end(), [](const auto &s) { std::cout << s << " "; }); + std::cout << std::endl; + }); +} + +void GraphPartitioner::addRow(const SBG::LIB::Set &s) +{ + auto edges1 = sbg_graph->map1().preImage(s); + auto nodes1 = sbg_graph->map2().image(edges1); + + auto edges2 = sbg_graph->map2().preImage(s); + auto nodes2 = sbg_graph->map1().image(edges2); + + auto nodes = nodes1.cup(nodes2); + auto nodes_vector = std::vector(); + nodes_vector.reserve(nodes.cardinal()); + + auto v0 = sbg_partitioner::to_vector(sbg_graph->V())[0]; + size_t row_size = v0.maxElem()[0] - v0.minElem()[0] + 1; + + for (const auto &n : sbg_partitioner::to_vector(nodes)) { + for (auto row = n.minElem()[0]; row <= n.maxElem()[0]; row++) { + for (auto col = n.minElem()[1]; col <= n.maxElem()[1]; col++) { + nodes_vector.push_back(row_size * row + col); + } } } - _xadj.push_back(0); - - for (int i = 0; i <= max_node; i++) { - auto s = SBG::LIB::SET_FACT.createSet(SBG::LIB::Interval(i, 1, i)); - auto edges1 = sbg_graph.map1().preImage(s); - auto nodes1 = sbg_graph.map2().image(edges1); + sort(nodes_vector.begin(), nodes_vector.end()); - auto edges2 = sbg_graph.map2().preImage(s); - auto nodes2 = sbg_graph.map1().image(edges2); + for_each(nodes_vector.cbegin(), nodes_vector.cend(), [this](const auto &val) { _adjncy.push_back(int(val)); }); + _xadj.push_back(_xadj.back() + nodes_vector.size()); +} - auto nodes = nodes1.cup(nodes2); - auto nodes_vector = std::vector(); - nodes_vector.reserve(nodes.cardinal()); +void GraphPartitioner::readGraphFromSBG() +{ + std::cout << "GraphPartitioner::readGraphFromSBG" << std::endl; + _nbr_vtxs = sbg_graph->V().cardinal(); + // asumming all setpiece are unidimensional and have step 1 - for (const auto &n : nodes) { - for (int val = n.begin()[0].begin(); val <= n.begin()[0].end(); val++) { - nodes_vector.push_back(val); + _xadj.push_back(0); + std::cout << "arity: " << sbg_graph->V().arity() << std::endl; + if (sbg_graph->V().arity() == 2) { + for (const auto &v : sbg_partitioner::to_vector(sbg_graph->V())) { + for (size_t i = v.minElem()[0]; i <= v.maxElem()[0]; i++) { + for (size_t j = v.minElem()[1]; j <= v.maxElem()[1]; j++) { + SBG::LIB::IntTuple t(i); + t.pushBack(j); + addRow(SBG::LIB::Set(t)); + } } } + } else { + for (int i = 0; i <= _nbr_vtxs; i++) { + auto s = SBG::LIB::Set(i, 1, i); + auto edges1 = sbg_graph->map1().preImage(s); + auto nodes1 = sbg_graph->map2().image(edges1); + + auto edges2 = sbg_graph->map2().preImage(s); + auto nodes2 = sbg_graph->map1().image(edges2); + + auto nodes = nodes1.cup(nodes2); + auto nodes_vector = std::vector(); + nodes_vector.reserve(nodes.cardinal()); + + for (const auto &n_piece : sbg_partitioner::to_vector(nodes)) { + for (int val = n_piece.minElem()[0]; val <= n_piece.maxElem()[0]; val++) { + nodes_vector.push_back(val); + } + } - sort(nodes_vector.begin(), nodes_vector.end()); + sort(nodes_vector.begin(), nodes_vector.end()); - for_each(nodes_vector.cbegin(), nodes_vector.cend(), [this](const auto &val) { _adjncy.push_back(int(val)); }); - _xadj.push_back(_xadj.back() + nodes_vector.size()); + for_each(nodes_vector.cbegin(), nodes_vector.cend(), [this](const auto &val) { _adjncy.push_back(int(val)); }); + _xadj.push_back(_xadj.back() + nodes_vector.size()); + } } - /*// @todo: Add logging, for the moment just comment the code. - for (int i = 0; i < _nbr_vtxs; ++i) { + _edges = grp_t(_adjncy.size()); + + // @todo: Add logging, for the moment just comment the code. + /*for (int i = 0; i < _nbr_vtxs; ++i) { std::cout << "Node " << i << " Connections: "; - for(int j = _xadj[i]; j < _xadj[i+1]; ++j) { + for (int j = _xadj[i]; j < _xadj[i + 1]; ++j) { std::cout << _adjncy[j] << " "; } - std:: cout << std::endl; + std::cout << std::endl; }*/ // @todo: Read weights files. diff --git a/test/partitioner/external_tools/graph_partitioner.hpp b/test/partitioner/external_tools/graph_partitioner.hpp index 278fe234..92a1ea8c 100644 --- a/test/partitioner/external_tools/graph_partitioner.hpp +++ b/test/partitioner/external_tools/graph_partitioner.hpp @@ -17,11 +17,16 @@ *****************************************************************************/ +#pragma once + +#include #include #include +#include #include #include +#include namespace SBG { namespace LIB { @@ -29,7 +34,6 @@ namespace SBG { } } - using grp_t = int; enum class PartitionMethod { @@ -37,6 +41,7 @@ enum class PartitionMethod { HMetis, Scotch, Kahip, + SBG, Unknown }; @@ -49,9 +54,9 @@ struct Partition { class GraphPartitioner { public: - explicit GraphPartitioner(const std::string &name); + explicit GraphPartitioner(const std::string &name, unsigned int parts); - Partition createPartition(const std::string& partition_method, unsigned int partitions); + std::tuple> createPartition(const std::string& partition_method, unsigned int partitions, bool save_to_file); static std::string validPartitionMethodsStr(); @@ -65,11 +70,12 @@ class GraphPartitioner { void readGraphFromJson(); void readGraph(); - void savePartitionToFile(const Partition &partition, const std::string& method_name) const; + void savePartitionToFile(const Partition &partition, const std::string& file_name) const; void readPartitionFile(const std::string &file_name, Partition &partition) const; void partitionUsingMetis(Partition &partition); void partitionUsingHMetis(Partition &partition); + void partitionUsingSBG(sbg_partitioner::PartitionMap& partitions); void createHMetisGraphFile(const std::string &file_name); void executeKhmetis(const std::string &h_graph_name) const; @@ -77,7 +83,9 @@ class GraphPartitioner { void partitionUsingKaHip(Partition &partition); - void readGraphFromSBG(const SBG::LIB::WeightedSBGraph& sbg_graph); + void readGraphFromSBG(); + + void addRow(const SBG::LIB::Set& set); std::string _name; grp_t _edges; diff --git a/test/partitioner/external_tools/main.cpp b/test/partitioner/external_tools/main.cpp index dbb3162d..c639fbe0 100644 --- a/test/partitioner/external_tools/main.cpp +++ b/test/partitioner/external_tools/main.cpp @@ -25,6 +25,10 @@ #include #include "graph_partitioner.hpp" +#include + +static int number_of_executions = 15; +static int number_of_cold_executions = 5; void usage() { @@ -61,31 +65,93 @@ void parseArgs(int argc, char* argv[], std::string& sbg_json_input, int& partiti } } - if (sbg_json_input.empty() || partitions <= 0 || partition_method.empty() || imbalance < 0) { + if (sbg_json_input.empty() || partitions <= 0 || imbalance < 0) { usage(); exit(EXIT_FAILURE); } } +void addRecord(const std::string& filename, const std::string& method, int partitions, + const std::vector>& durations) +{ + // Check if the file already exists before we open it + bool exists = std::filesystem::exists(filename); + + // Open in append mode + std::ofstream myFile(filename, std::ios::app); + + if (!myFile.is_open()) { + std::cerr << "Error opening file!" << std::endl; + return; + } + + // If the file is new, write the header first + if (!exists) { + myFile << "Method,Partitions"; + for (size_t i = 0; i < number_of_executions - number_of_cold_executions; i++) { + myFile << ", exec " << i; + } + myFile << "\n"; + } + + // Add the new row + myFile << method << "," << partitions; + for (const auto& d : durations) { + myFile << "," << d.count(); + } + myFile << "\n"; + + myFile.close(); + std::cout << "Results in " << filename << std::endl; +} + int main(int argc, char* argv[]) { + SBG::LIB::SET_IMPL.set_set_fact(SBG::LIB::SetKind::kOrdered); + std::string json_file_name; - int partitions; + int parts; std::string partition_method; int imbalance; - parseArgs(argc, argv, json_file_name, partitions, partition_method, imbalance); + parseArgs(argc, argv, json_file_name, parts, partition_method, imbalance); + + std::cout << "Partitioning " << json_file_name << " into " << parts << " parts using " << partition_method << std::endl; + + std::vector methods; + if (partition_method.empty()) { + methods = {"SBG", "Metis", "Scotch", "Kahip"}; + } else { + methods = {partition_method}; + } + + GraphPartitioner partitioner(json_file_name, parts); + for (const auto& m : methods) { + std::vector> durations; - GraphPartitioner partitioner(json_file_name); - Partition partition = partitioner.createPartition(partition_method, partitions); + for (size_t i = 0; i < number_of_executions; i++) { + std::cout << "running " << i << " of " << number_of_executions << std::endl; + auto [partition, duration] = partitioner.createPartition(m, parts, i == number_of_executions - 1); + + if (i < number_of_cold_executions) { + continue; + } + + durations.push_back(duration); + } + + std::filesystem::path json_file_name_path(json_file_name); + + addRecord(json_file_name_path.filename().replace_extension("csv"), m, parts, durations); + } // write results to a file - std::filesystem::path json_filesystem_path(json_file_name); - json_filesystem_path = json_filesystem_path.replace_extension(); - std::ofstream output_file(json_filesystem_path.filename().string() + "_" + partition_method + "_" + std::to_string(partitions) + - "_output.txt"); - std::for_each(partition.values.cbegin(), partition.values.cend(), - [&output_file](const auto& val) { output_file << std::to_string(val) << "\n"; }); + // std::filesystem::path json_filesystem_path(json_file_name); + // json_filesystem_path = json_filesystem_path.replace_extension(); + // std::ofstream output_file(json_filesystem_path.filename().string() + "_" + partition_method + "_" + std::to_string(partitions) + + // "_output.txt"); + // std::for_each(partition.values.cbegin(), partition.values.cend(), + // [&output_file](const auto& val) { output_file << std::to_string(val) << "\n"; }); return 0; } \ No newline at end of file diff --git a/test/partitioner/external_tools/plot_csv.py b/test/partitioner/external_tools/plot_csv.py new file mode 100644 index 00000000..13dcd773 --- /dev/null +++ b/test/partitioner/external_tools/plot_csv.py @@ -0,0 +1,54 @@ +import matplotlib.pyplot as plt +import pandas as pd +import os + +# Loop through all CSV files in the current directory +for file in os.listdir('.'): + if file.endswith('.csv') and not file.startswith('processed_'): + # Load the file + df = pd.read_csv(file) + + # Select the 10 numeric columns (indices 2 to 11) + # Assuming first two columns are at index 0 and 1 + numeric_data = df.iloc[:, 2:12] + + # Compute calculations + df['Average'] = numeric_data.mean(axis=1) + df['Standard_Deviation'] = numeric_data.std(axis=1) + + # Save the result to a new file + output_filename = f'processed_{file}' + df.to_csv(output_filename, index=False) + print(f"Processed: {file} -> {output_filename}") + + # Load the processed data + df = pd.read_csv(output_filename) + + # Use the first column as the identifier for the X-axis + x_labels = df.iloc[:, 0].astype(str) + # Select the 10 numeric columns + numeric_cols = df.columns[2:12] + + plt.figure(figsize=(12, 6)) + + # 1. Scatter plot: For each row identifier, plot all 10 numeric values + for col in numeric_cols: + plt.scatter(x_labels, df[col], color='blue', alpha=0.3, label='_nolegend_') + + # Add a single dummy scatter for the legend + plt.scatter([], [], color='blue', alpha=0.3, label='Individual Values (10 per row)') + + # 2. Line plot: Connect the row-wise averages + plt.plot(x_labels, df['Average'], color='red', marker='s', + linewidth=2, label='Row Average') + + # Formatting the plot + plt.title('Numeric Distribution per Row and Overall Average Trend') + plt.xlabel('Row Identifiers (First Column)') + plt.ylabel('Numeric Values') + plt.xticks(rotation=45) + plt.legend() + plt.grid(True, linestyle='--', alpha=0.5) + plt.tight_layout() + + plt.savefig(f'{output_filename}.png') \ No newline at end of file diff --git a/test/partitioner/external_tools/run_sbg_partitioner.bash b/test/partitioner/external_tools/run_sbg_partitioner.bash new file mode 100644 index 00000000..9abd5df6 --- /dev/null +++ b/test/partitioner/external_tools/run_sbg_partitioner.bash @@ -0,0 +1,17 @@ +#!/bin/bash + +METHODS=("SBG") + +call_partitioner_test() { + local size="$1" + echo Running garph partitioner for file $arg with $n parts + for method in "${METHODS[@]}"; do + file="advection2D_${size}.json" + echo running -f $file -n $n -m $method + "./install/bin/graph_partitioner" -f "$file" -n 4 -m "$method" + done +} + +# call_partitioner_test 100 +call_partitioner_test 1000 +call_partitioner_test 10000 diff --git a/test/partitioner/main.cpp b/test/partitioner/main.cpp index f2216be1..baba51f8 100644 --- a/test/partitioner/main.cpp +++ b/test/partitioner/main.cpp @@ -20,10 +20,14 @@ #include #include +#include + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleMock(&argc, argv); + SBG::LIB::SET_IMPL.set_set_fact(SBG::LIB::SetKind::kOrdered); + return RUN_ALL_TESTS(); } diff --git a/test/partitioner/memory_monitor.py b/test/partitioner/memory_monitor.py new file mode 100644 index 00000000..ba7858ff --- /dev/null +++ b/test/partitioner/memory_monitor.py @@ -0,0 +1,72 @@ +import subprocess +import psutil +import time +import matplotlib.pyplot as plt +from pathlib import Path + + +def monitor(command): + # Start the C++ process + proc = subprocess.Popen(command, shell=True) + mem_usage = [] + mem_usage2 = [] + timestamps = [] + start_time = time.time() + + while proc.poll() is None: # While process is running + try: + # Get RSS memory in MB + mem_info = psutil.Process(proc.pid).memory_full_info() + mem = mem_info.uss / (1024 * 1024) + mem_usage.append(mem) + + rss = mem_info.rss / (1024 * 1024) + mem_usage2.append(rss) + # rss = mem_info.rss / (1024 * 1024) # Physical RAM + # vms = mem_info.vms / (1024 * 1024) # Virtual Memory + timestamps.append(time.time() - start_time) + except psutil.NoSuchProcess: + break + time.sleep(0.05) # Sample every 50ms + + return timestamps, mem_usage, mem_usage2 + +# Run and Plot +inputs = [ + "/workspaces/sb-graph/test/partitioner/external_tools/data/advection_1000.json", + "/workspaces/sb-graph/test/partitioner/external_tools/data/advection_10000.json", + "/workspaces/sb-graph/test/partitioner/external_tools/data/advection_100000.json" +] + +colors = { + inputs[0]: 'red', + inputs[1]: 'blue', + inputs[2]: 'green' +} + +colors2 = { + inputs[0]: 'brown', + inputs[1]: 'skyblue', + inputs[2]: 'black' +} + + +# Generate the plot +plt.figure(figsize=(10, 6)) +plt.title("Memory Consumption Timeline") +plt.xlabel("Time (seconds)") +plt.ylabel("Memory Usage (MB)") +plt.grid(True) + +for f in inputs: + for _ in range(1): + times, mem, mem2 = monitor(f"sleep 1; /workspaces/sb-graph/install/bin/sbg-partitioner -f {f} -p 16") + + # plt.plot(times, mem, color=colors[f]) + plt.plot(times, mem2, color=colors2[f], label=Path(f).stem) +plt.legend() + +# Save the plot instead of showing it +output_file = "memory_benchmark_advection_16.svg" +plt.savefig(output_file, format='svg', bbox_inches='tight') +print(f"Benchmark finished. Results saved to {output_file}") \ No newline at end of file diff --git a/test/partitioner/partitioner_test.cpp b/test/partitioner/partitioner_test.cpp index e2b5d447..fcf723a3 100644 --- a/test/partitioner/partitioner_test.cpp +++ b/test/partitioner/partitioner_test.cpp @@ -22,28 +22,29 @@ #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include /// @file parser_test.cpp /// using namespace SBG::LIB; +using SBG::LIB::detail::Interval; +using SBG::LIB::detail::MultiDimInter; +using namespace sbg_partitioner; /// @addtogroup SBGraphParserTests /// @{ -class PartitionerTests : public testing::TestWithParam { -}; +class PartitionerTests : public testing::TestWithParam {}; static const std::string PARTITIONER_DATA_DIR = "../../partitioner/data/"; const std::string get_full_file_name(const std::string json_file) { - const std::string FULL_FILE_NAME = PARTITIONER_DATA_DIR + json_file; - return FULL_FILE_NAME; + const std::string FULL_FILE_NAME = PARTITIONER_DATA_DIR + json_file; + return FULL_FILE_NAME; } /** @@ -58,33 +59,33 @@ const std::string get_full_file_name(const std::string json_file) * @param rhs_map_domain rhs map domain. * @param rhs_map_exps rhs map expresions. */ -static void test_create_sb_graph(const std::string& filename, const std::vector& node_intervals, - const std::vector& lhs_map_domain, const std::vector& lhs_map_exps, - const std::vector& rhs_map_domain, const std::vector& rhs_map_exps) +static void test_create_sb_graph(const std::string& filename, const std::vector& node_intervals, + const std::vector& lhs_map_domain, const std::vector& lhs_map_exps, + const std::vector& rhs_map_domain, const std::vector& rhs_map_exps) { auto sb_graph = sbg_partitioner::build_sb_graph(filename); // create nodes of the expected graph - auto expected_nodes = SET_FACT.createSet(); + auto expected_nodes = Set(); for (const auto& n : node_intervals) { - expected_nodes.emplaceBack(n); + expected_nodes = expected_nodes.cup(n); } EXPECT_EQ(expected_nodes, sb_graph.V()); // create edges of the expected graph through its maps - PWMap lhs_maps = PW_FACT.createPWMap(); + PWMap lhs_maps = PWMap(); for (size_t i = 0; i < lhs_map_domain.size(); i++) { - lhs_maps.emplaceBack(Map(lhs_map_domain[i], lhs_map_exps[i])); + lhs_maps.insert(Map(lhs_map_domain[i], lhs_map_exps[i])); } - PWMap rhs_maps = PW_FACT.createPWMap(); + PWMap rhs_maps = PWMap(); for (size_t i = 0; i < rhs_map_domain.size(); i++) { - rhs_maps.emplaceBack(Map(rhs_map_domain[i], rhs_map_exps[i])); + rhs_maps.insert(Map(rhs_map_domain[i], rhs_map_exps[i])); } // test edges are correct - EXPECT_EQ(true, lhs_maps.dom() == sb_graph.E()); - EXPECT_EQ(true, rhs_maps.dom() == sb_graph.E()); + EXPECT_EQ(true, lhs_maps.domain() == sb_graph.E()); + EXPECT_EQ(true, rhs_maps.domain() == sb_graph.E()); //// test that maps are as expected EXPECT_EQ(true, lhs_maps == sb_graph.map1()); @@ -93,173 +94,240 @@ static void test_create_sb_graph(const std::string& filename, const std::vector< TEST(create_sb_graph, PartitionerTests) { - test_create_sb_graph(get_full_file_name("air_conditioners_1000.json"), - {Interval(0, 1, 999), Interval(1000, 1, 1999), Interval(2000, 1, 2999), Interval(3000, 1, 3999)}, - {Interval(4000, 1, 4999), Interval(5000, 1, 5999), Interval(6000, 1, 6999), Interval(7000, 1, 7999)}, - {Exp(LExp(1, RATIONAL(-3000, 1))), Exp(LExp(1, RATIONAL(-2000, 1))), Exp(LExp(1, RATIONAL(-6000, 1))), - Exp(LExp(1, RATIONAL(-5000, 1)))}, - {Interval(4000, 1, 4999), Interval(5000, 1, 5999), Interval(6000, 1, 6999), Interval(7000, 1, 7999)}, - {Exp(LExp(1, RATIONAL(-4000, 1))), Exp(LExp(1, RATIONAL(-5000, 1))), Exp(LExp(1, RATIONAL(-5000, 1))), - Exp(LExp(1, RATIONAL(-6000, 1)))}); - - test_create_sb_graph(get_full_file_name("advection.json"), {Interval(0, 1, 0), Interval(1, 1, 99)}, - {Interval(100, 1, 100), Interval(101, 1, 198)}, {Exp(LExp(0, 0)), Exp(LExp(1, RATIONAL(-100, 1)))}, - {Interval(100, 1, 100), Interval(101, 1, 198)}, {Exp(LExp(1, RATIONAL(-99, 1))), Exp(LExp(1, RATIONAL(-99, 1)))}); + test_create_sb_graph( + get_full_file_name("air_conditioners_1000.json"), {Set(0, 1, 999), Set(1000, 1, 1999), Set(2000, 1, 2999), Set(3000, 1, 3999)}, + {Set(4000, 1, 4999), Set(5000, 1, 5999), Set(6000, 1, 6999), Set(7000, 1, 7999)}, + {Expression(Rational(1), -3000), Expression(Rational(1), -2000), Expression(Rational(1), -6000), Expression(Rational(1), -5000)}, + {Set(4000, 1, 4999), Set(5000, 1, 5999), Set(6000, 1, 6999), Set(7000, 1, 7999)}, + {Expression(Rational(1), -4000), Expression(Rational(1), -5000), Expression(Rational(1), -5000), Expression(Rational(1), -6000)}); + + test_create_sb_graph(get_full_file_name("advection.json"), {Set(0, 1, 0), Set(1, 1, 99)}, {Set(100, 1, 100), Set(101, 1, 198)}, + {Expression(Rational(0), 0), Expression(Rational(1), -100)}, {Set(100, 1, 100), Set(101, 1, 198)}, + {Expression(Rational(1), -99), Expression(Rational(1), -99)}); } TEST(initial_partition, PartitionerTests) { - auto sb_graph = sbg_partitioner::build_sb_graph(get_full_file_name("air_conditioners_1000.json")); - - constexpr bool enable_multithreading = false; - sbg_partitioner::PartitionMap partition = sbg_partitioner::best_initial_partition(sb_graph, 4, sbg_partitioner::InitialPartitionStrategy::ALL, enable_multithreading); - - auto expected_distributed_pre_order_0 = SET_FACT.createSet(); - expected_distributed_pre_order_0.emplaceBack(Interval(0, 1, 249)); - expected_distributed_pre_order_0.emplaceBack(Interval(1000, 1, 1249)); - expected_distributed_pre_order_0.emplaceBack(Interval(2000, 1, 2249)); - expected_distributed_pre_order_0.emplaceBack(Interval(3000, 1, 3249)); - Set partition_0 = sbg_partitioner::from_vector(partition.at(0)); - EXPECT_EQ(expected_distributed_pre_order_0, partition_0); - - auto expected_distributed_pre_order_1 = SET_FACT.createSet(); - expected_distributed_pre_order_1.emplaceBack(Interval(250, 1, 499)); - expected_distributed_pre_order_1.emplaceBack(Interval(1250, 1, 1499)); - expected_distributed_pre_order_1.emplaceBack(Interval(2250, 1, 2499)); - expected_distributed_pre_order_1.emplaceBack(Interval(3250, 1, 3499)); - Set partition_1 = sbg_partitioner::from_vector(partition.at(1)); - EXPECT_EQ(expected_distributed_pre_order_1, partition_1); - - auto expected_distributed_pre_order_2 = SET_FACT.createSet(); - expected_distributed_pre_order_2.emplaceBack(Interval(500, 1, 749)); - expected_distributed_pre_order_2.emplaceBack(Interval(1500, 1, 1749)); - expected_distributed_pre_order_2.emplaceBack(Interval(2500, 1, 2749)); - expected_distributed_pre_order_2.emplaceBack(Interval(3500, 1, 3749)); - Set partition_2 = sbg_partitioner::from_vector(partition.at(2)); - EXPECT_EQ(expected_distributed_pre_order_2, partition_2); - - auto expected_distributed_pre_order_3 = SET_FACT.createSet(); - expected_distributed_pre_order_3.emplaceBack(Interval(750, 1, 999)); - expected_distributed_pre_order_3.emplaceBack(Interval(1750, 1, 1999)); - expected_distributed_pre_order_3.emplaceBack(Interval(2750, 1, 2999)); - expected_distributed_pre_order_3.emplaceBack(Interval(3750, 1, 3999)); - Set partition_3 = sbg_partitioner::from_vector(partition.at(3)); - EXPECT_EQ(expected_distributed_pre_order_3, partition_3); - - sbg_partitioner::get_communication_cost().clear_communication_cache(); + auto sb_graph = sbg_partitioner::build_sb_graph(get_full_file_name("air_conditioners_1000.json")); + + constexpr bool enable_multithreading = false; + sbg_partitioner::PartitionMap partition = + sbg_partitioner::best_initial_partition(sb_graph, 4, sbg_partitioner::InitialPartitionStrategy::ALL, enable_multithreading); + + auto expected_distributed_pre_order_0 = Set(); + expected_distributed_pre_order_0 = expected_distributed_pre_order_0.cup(Set(0, 1, 249)); + expected_distributed_pre_order_0 = expected_distributed_pre_order_0.cup(Set(1000, 1, 1249)); + expected_distributed_pre_order_0 = expected_distributed_pre_order_0.cup(Set(2000, 1, 2249)); + expected_distributed_pre_order_0 = expected_distributed_pre_order_0.cup(Set(3000, 1, 3249)); + Set partition_0 = sbg_partitioner::from_vector(partition.at(0)); + EXPECT_EQ(expected_distributed_pre_order_0, partition_0); + + auto expected_distributed_pre_order_1 = Set(); + expected_distributed_pre_order_1 = expected_distributed_pre_order_1.cup(Set(250, 1, 499)); + expected_distributed_pre_order_1 = expected_distributed_pre_order_1.cup(Set(1250, 1, 1499)); + expected_distributed_pre_order_1 = expected_distributed_pre_order_1.cup(Set(2250, 1, 2499)); + expected_distributed_pre_order_1 = expected_distributed_pre_order_1.cup(Set(3250, 1, 3499)); + Set partition_1 = sbg_partitioner::from_vector(partition.at(1)); + EXPECT_EQ(expected_distributed_pre_order_1, partition_1); + + auto expected_distributed_pre_order_2 = Set(); + expected_distributed_pre_order_2 = expected_distributed_pre_order_2.cup(Set(500, 1, 749)); + expected_distributed_pre_order_2 = expected_distributed_pre_order_2.cup(Set(1500, 1, 1749)); + expected_distributed_pre_order_2 = expected_distributed_pre_order_2.cup(Set(2500, 1, 2749)); + expected_distributed_pre_order_2 = expected_distributed_pre_order_2.cup(Set(3500, 1, 3749)); + Set partition_2 = sbg_partitioner::from_vector(partition.at(2)); + EXPECT_EQ(expected_distributed_pre_order_2, partition_2); + + auto expected_distributed_pre_order_3 = Set(); + expected_distributed_pre_order_3 = expected_distributed_pre_order_3.cup(Set(750, 1, 999)); + expected_distributed_pre_order_3 = expected_distributed_pre_order_3.cup(Set(1750, 1, 1999)); + expected_distributed_pre_order_3 = expected_distributed_pre_order_3.cup(Set(2750, 1, 2999)); + expected_distributed_pre_order_3 = expected_distributed_pre_order_3.cup(Set(3750, 1, 3999)); + Set partition_3 = sbg_partitioner::from_vector(partition.at(3)); + EXPECT_EQ(expected_distributed_pre_order_3, partition_3); + + sbg_partitioner::get_communication_cost().clear_communication_cache(); } static void test_partitioning(const std::string& filename, int number_of_partitions) { - auto sb_graph = sbg_partitioner::build_sb_graph(filename); - constexpr bool enable_multithreading = false; - auto partitions = sbg_partitioner::best_initial_partition(sb_graph, number_of_partitions, sbg_partitioner::InitialPartitionStrategy::ALL, enable_multithreading); - sbg_partitioner::kl_sbg_imbalance_partitioner(sb_graph, partitions, 0.0, enable_multithreading); + auto sb_graph = sbg_partitioner::build_sb_graph(filename); + constexpr bool enable_multithreading = false; + auto partitions = sbg_partitioner::best_initial_partition(sb_graph, number_of_partitions, sbg_partitioner::InitialPartitionStrategy::ALL, + enable_multithreading); + sbg_partitioner::kl_sbg_imbalance_partitioner(sb_graph, partitions, 0.0, enable_multithreading); - sbg_partitioner::sanity_check(sb_graph, partitions, number_of_partitions); + sbg_partitioner::sanity_check(sb_graph, partitions, number_of_partitions); - // clear cache after running it - sbg_partitioner::get_communication_cost().clear_communication_cache(); + // clear cache after running it + sbg_partitioner::get_communication_cost().clear_communication_cache(); } TEST(partitioning, PartitionerTests) { - test_partitioning(get_full_file_name("advection.json"), 2); - test_partitioning(get_full_file_name("advection.json"), 4); - test_partitioning(get_full_file_name("advection.json"), 8); + test_partitioning(get_full_file_name("advection.json"), 2); + test_partitioning(get_full_file_name("advection.json"), 4); + test_partitioning(get_full_file_name("advection.json"), 8); - test_partitioning(get_full_file_name("air_conditioners_1000.json"), 2); - test_partitioning(get_full_file_name("air_conditioners_1000.json"), 4); - test_partitioning(get_full_file_name("air_conditioners_1000.json"), 8); + test_partitioning(get_full_file_name("air_conditioners_1000.json"), 2); + test_partitioning(get_full_file_name("air_conditioners_1000.json"), 4); + test_partitioning(get_full_file_name("air_conditioners_1000.json"), 8); - test_partitioning(get_full_file_name("air_conditioners_cont_4_1000.json"), 4); + test_partitioning(get_full_file_name("air_conditioners_cont_4_1000.json"), 4); } -TEST(test_adjacency_matrix, PartitionerTests) +namespace { + +Set make_set(const std::vector>>& pieces) { - std::array files = { "air_conditioners_1000.json", "air_conditioners_cont_4_1000.json", "advection.json" }; - - for (const auto& f : files) { - // clear communication cache - sbg_partitioner::get_communication_cost().clear_communication_cache(); - - auto sb_graph = sbg_partitioner::build_sb_graph(get_full_file_name(f), false); - - auto injective_conn = sbg_partitioner::using_cc::split_nodes_into_injective_domains(sb_graph); - - sbg_partitioner::using_cc::SetPointers sorted_nodes = {}; - unsigned index = 0; - for (const auto& s : injective_conn) { - sorted_nodes.push_back(sbg_partitioner::using_cc::SetPointer(index++, s, 0, s.cardinal())); - } - - sbg_partitioner::CommunicationCostCC comm_cc(sb_graph, sorted_nodes); - - for (size_t i = 0; i < sorted_nodes.size(); i++) { - const auto& s1 = sorted_nodes.at(i); - for (size_t j = i + 1; j < sorted_nodes.size(); j++) { - const auto& s2 = sorted_nodes.at(j); - - auto comm = comm_cc.get_communication(s1.index, s2.index); - if (comm > 0) { - EXPECT_EQ(s1.set_piece.cardinal(), s2.set_piece.cardinal()); - } - } - } - - std::vector sorted_partitions; - auto new_graph = SBG::LIB::WeightedSBGraph( - injective_conn, - sb_graph.Vmap(), - sb_graph.map1(), - sb_graph.map2(), - sb_graph.Emap(), - sb_graph.subEmap() - ); - - auto non_sorted_partitions = sbg_partitioner::best_initial_partition(new_graph, 2, sbg_partitioner::InitialPartitionStrategy::ALL, false); - - for (const auto& partition : non_sorted_partitions) { - sorted_partitions.emplace_back(); - for (const auto& v : partition) { - for (const auto& n : sorted_nodes) { - if (not v.intersection(n.set_piece).isEmpty()) { - size_t offset = v.begin()[0].begin() - n.set_piece.begin()[0].begin(); - sorted_partitions.back().emplace_back(n.index, n.set_piece, offset, v.cardinal()); - } - } - } - } - - auto comm = sbg_partitioner::CommunicationCost(new_graph, non_sorted_partitions); - for (size_t i = 0; i < sorted_partitions.size(); i++) { - const auto p1 = sorted_partitions.at(i); - for (const auto& s1 : p1) { - auto common_edges = comm.get_ec_by_interval(i, s1.set_piece); - common_edges = common_edges.cup(comm.get_ic_by_interval(i, s1.set_piece)); - auto comm_number = comm_cc.get_communication(s1.index); - - EXPECT_EQ(common_edges.cardinal(), comm_number); - } - } - - for (size_t i = 0; i < sorted_partitions.size(); i++) { - const auto p1 = sorted_partitions.at(i); - for (size_t j = i + 1; j < sorted_partitions.size(); j++) { - const auto p2 = sorted_partitions.at(j); - for (const auto& s1 : p1) { - for (const auto& s2 : p2) { - if (s1.index == s2.index) { continue; } - auto edges_1 = comm.get_set_piece_edges(s1.set_piece); - auto edges_2 = comm.get_set_piece_edges(s2.set_piece); - auto edges = edges_1.intersection(edges_2); - auto comm_number = comm_cc.get_communication(s1.index, s2.index); - - EXPECT_EQ(edges.cardinal(), comm_number); - } - } - } - } + Set s; + for (const auto& piece : pieces) { + MultiDimInter mdi; + for (const auto& [lo, step, hi] : piece) { + mdi.emplaceBack(Interval(lo, step, hi)); } + s = s.cup(sbg_partitioner::to_set(mdi)); + } + return s; +} + +// Compares sets by element content, not by piece layout, since cup/difference may fragment pieces. +void expect_same_set(const Set& actual, const Set& expected, const std::string& label) +{ + EXPECT_TRUE(actual.difference(expected).isEmpty()) << label << ": actual has elements not in expected: " << actual.difference(expected); + EXPECT_TRUE(expected.difference(actual).isEmpty()) << label << ": expected has elements not in actual: " << expected.difference(actual); +} + +void check_cut(const Set& input, size_t size, const Set& expected_taken, const Set& expected_remaining, const std::string& label) +{ + auto [taken, remaining] = cut_interval_by_dimension(input, NodeWeight(), size); + expect_same_set(taken, expected_taken, label + " taken"); + expect_same_set(remaining, expected_remaining, label + " remaining"); + EXPECT_TRUE(taken.intersection(remaining).isEmpty()) << label << ": taken and remaining overlap"; + expect_same_set(taken.cup(remaining), input, label + " union"); + EXPECT_EQ(taken.cardinal(), expected_taken.cardinal()) << label << " taken cardinal"; + EXPECT_EQ(remaining.cardinal(), expected_remaining.cardinal()) << label << " remaining cardinal"; +} + +} // namespace + +TEST(cut_interval_by_dimension, PartitionerTests) +{ + // 1-D: take 4 from [0:9]. + check_cut(make_set({{{0, 1, 9}}}), 4, make_set({{{0, 1, 3}}}), make_set({{{4, 1, 9}}}), "1D"); + + // 2-D: take 10 from [0:2]x[0:3] -> {[0:1]x[0:3], [2:2]x[0:1]}, remaining {[2:2]x[2:3]}. + check_cut(make_set({{{0, 1, 2}, {0, 1, 3}}}), 10, make_set({{{0, 1, 1}, {0, 1, 3}}, {{2, 1, 2}, {0, 1, 1}}}), + make_set({{{2, 1, 2}, {2, 1, 3}}}), "2D"); + + // 3-D: take 17 from [0:1]x[0:2]x[0:3] (cardinal 24). + check_cut(make_set({{{0, 1, 1}, {0, 1, 2}, {0, 1, 3}}}), 17, + make_set({{{0, 1, 0}, {0, 1, 2}, {0, 1, 3}}, {{1, 1, 1}, {0, 1, 0}, {0, 1, 3}}, {{1, 1, 1}, {1, 1, 1}, {0, 1, 0}}}), + make_set({{{1, 1, 1}, {1, 1, 1}, {1, 1, 3}}, {{1, 1, 1}, {2, 1, 2}, {0, 1, 3}}}), "3D"); + + // Multi-piece 2-D (lex-ordered pieces): first full, second partially cut. + check_cut(make_set({{{0, 1, 1}, {0, 1, 1}}, {{5, 1, 6}, {0, 1, 1}}}), 6, make_set({{{0, 1, 1}, {0, 1, 1}}, {{5, 1, 5}, {0, 1, 1}}}), + make_set({{{6, 1, 6}, {0, 1, 1}}}), "multi-piece 2D"); + + // Interleaved pieces (partial remainder sorts after a later untouched piece — exercises the sort before OrderedSet). + check_cut(make_set({{{0, 1, 1}, {0, 1, 1}}, {{0, 1, 1}, {5, 1, 6}}}), 3, make_set({{{0, 1, 0}, {0, 1, 1}}, {{1, 1, 1}, {0, 1, 0}}}), + make_set({{{0, 1, 1}, {5, 1, 6}}, {{1, 1, 1}, {1, 1, 1}}}), "interleaved pieces"); + + // Edge: size == 0. + Set input_edge = make_set({{{0, 1, 2}, {0, 1, 3}}}); + check_cut(input_edge, 0, Set(), input_edge, "size==0"); + + // Edge: size >= cardinal. + check_cut(input_edge, input_edge.cardinal(), input_edge, Set(), "size==cardinal"); + check_cut(input_edge, input_edge.cardinal() + 42, input_edge, Set(), "size>cardinal"); } + +// TEST(DISABLED_test_adjacency_matrix, PartitionerTests) +// { +// std::array files = { "air_conditioners_1000.json", "air_conditioners_cont_4_1000.json", "advection.json" }; + +// for (const auto& f : files) { +// // clear communication cache +// sbg_partitioner::get_communication_cost().clear_communication_cache(); + +// auto sb_graph = sbg_partitioner::build_sb_graph(get_full_file_name(f), false); + +// auto injective_conn = sbg_partitioner::split_sets_according_to_relations(sb_graph); + +// sbg_partitioner::using_cc::SetPointers sorted_nodes = {}; +// unsigned index = 0; +// for (const auto& s : injective_conn) { +// sorted_nodes.push_back(sbg_partitioner::using_cc::SetPointer(index++, s, 0, s.cardinal())); +// } + +// sbg_partitioner::CommunicationCostCC comm_cc(sb_graph, sorted_nodes); + +// for (size_t i = 0; i < sorted_nodes.size(); i++) { +// const auto& s1 = sorted_nodes.at(i); +// for (size_t j = i + 1; j < sorted_nodes.size(); j++) { +// const auto& s2 = sorted_nodes.at(j); + +// auto comm = comm_cc.get_communication(s1.index, s2.index); +// if (comm > 0) { +// EXPECT_EQ(s1.set_piece.cardinal(), s2.set_piece.cardinal()); +// } +// } +// } + +// std::vector sorted_partitions; +// auto new_graph = SBG::LIB::WeightedSBGraph( +// injective_conn, +// sb_graph.Vmap(), +// sb_graph.map1(), +// sb_graph.map2(), +// sb_graph.Emap(), +// sb_graph.subEmap() +// ); + +// auto non_sorted_partitions = sbg_partitioner::best_initial_partition(new_graph, 2, +// sbg_partitioner::InitialPartitionStrategy::ALL, false); + +// for (const auto& partition : non_sorted_partitions) { +// sorted_partitions.emplace_back(); +// for (const auto& v : partition) { +// for (const auto& n : sorted_nodes) { +// if (not v.intersection(n.set_piece).isEmpty()) { +// size_t offset = v.begin()[0].begin() - n.set_piece.begin()[0].begin(); +// sorted_partitions.back().emplace_back(n.index, n.set_piece, offset, v.cardinal()); +// } +// } +// } +// } + +// auto comm = sbg_partitioner::CommunicationCost(new_graph, non_sorted_partitions); +// for (size_t i = 0; i < sorted_partitions.size(); i++) { +// const auto p1 = sorted_partitions.at(i); +// for (const auto& s1 : p1) { +// auto common_edges = comm.get_ec_by_interval(i, s1.set_piece); +// common_edges = common_edges.cup(comm.get_ic_by_interval(i, s1.set_piece)); +// auto comm_number = comm_cc.get_communication(s1.index); + +// EXPECT_EQ(common_edges.cardinal(), comm_number); +// } +// } + +// for (size_t i = 0; i < sorted_partitions.size(); i++) { +// const auto p1 = sorted_partitions.at(i); +// for (size_t j = i + 1; j < sorted_partitions.size(); j++) { +// const auto p2 = sorted_partitions.at(j); +// for (const auto& s1 : p1) { +// for (const auto& s2 : p2) { +// if (s1.index == s2.index) { continue; } +// auto edges_1 = comm.get_set_piece_edges(s1.set_piece); +// auto edges_2 = comm.get_set_piece_edges(s2.set_piece); +// auto edges = edges_1.intersection(edges_2); +// auto comm_number = comm_cc.get_communication(s1.index, s2.index); + +// EXPECT_EQ(edges.cardinal(), comm_number); +// } +// } +// } +// } +// } +// } /// @} diff --git a/test/partitioner/sim_models/.gitignore b/test/partitioner/sim_models/.gitignore new file mode 100644 index 00000000..4a024568 --- /dev/null +++ b/test/partitioner/sim_models/.gitignore @@ -0,0 +1,3 @@ +*.partition +*.csv +sim_failed/ diff --git a/test/partitioner/sim_models/advection_2D/advection2D.c b/test/partitioner/sim_models/advection_2D/advection2D.c new file mode 100644 index 00000000..3a6fe589 --- /dev/null +++ b/test/partitioner/sim_models/advection_2D/advection2D.c @@ -0,0 +1,458 @@ +#include +#include +#include +#include + +#include "advection2D.h" +#include +#include +#include +#include +#include + +void MOD_settings(SD_simulationSettings settings) +{ + settings->debug = 0; + settings->parallel = FALSE; + settings->hybrid = FALSE; + settings->method = 11; +} + +void MOD_definition(double *x, double *d, double *a, double t, double *dx) +{ + int _d1; + int _d2; + int i; + int j; + _der_u(1, 1) = -_ax * _u(1, 1) / _dx - _ay * _u(1, 1) / _dy + _r * (pow(_u(1, 1), 2) - pow(_u(1, 1), 3)); + for (i = 2; i <= 20; i += 1) { + _der_u(i, 1) = -_ax * _u(i, 1) / _dx - _ay * (_u(i, 1) - _u(i - 1, 1)) / _dy + _r * (pow(_u(i, 1), 2) - pow(_u(i, 1), 3)); + } + for (j = 2; j <= 20; j += 1) { + _der_u(1, j) = -_ax * (_u(1, j) - _u(1, j - 1)) / _dx - _ay * _u(1, j) / _dy + _r * (pow(_u(1, j), 2) - pow(_u(1, j), 3)); + } + for (i = 2; i <= 20; i += 1) { + for (j = 2; j <= 20; j += 1) { + _der_u(i, j) = + -_ax * (_u(i, j) - _u(i, j - 1)) / _dx - _ay * (_u(i, j) - _u(i - 1, j)) / _dy + _r * (pow(_u(i, j), 2) - pow(_u(i, j), 3)); + } + } +} + +void MOD_zeroCrossing(int idx, double *x, double *d, double *a, double t, double *zc) {} + +void MOD_handlerPos(int idx, double *x, double *d, double *a, double t) {} + +void MOD_handlerNeg(int idx, double *x, double *d, double *a, double t) {} + +void MOD_output(int idx, double *x, double *d, double *a, double t, double *out) +{ + switch (idx) { + case _eval_out_exp_1: { + _out = _u(20, 20); + return; + } + case _eval_out_exp_2: { + _out = _u(1, 1); + return; + } + } +} + +void MOD_jacobian(double *x, double *d, double *a, double t, SD_jacMatrices dvdx, double *jac) +{ + int row, row_t, eq_var, c_row, c_row_g; + int col, col_g, col_t; + int x_ind; + double aux; + int _d1; + int _d2; + int _rg_d1; + int _rg_d2; + int i; + int j; + SD_cleanJacMatrices(dvdx); + for (row = 1; row <= 1; row++) { + c_row = _c_index(row); + x_ind = _idx_u(1, 1); + col = pos(dvdx->df_dx[0]->index[c_row], dvdx->df_dx[0]->size[c_row], x_ind); + aux = 0; + dvdx->df_dx[0]->value[c_row][col] += aux; + } + for (row = 1; row <= 19; row++) { + c_row = _c_index(row); + _get_eq_3_var_idxs(row, eq_var); + _get_u_idxs(eq_var); + if ((1 <= 1 && 1 <= 1) && (2 <= _d2 && _d2 <= 20)) { + x_ind = _idx_u(1, _d2); + col = pos(dvdx->df_dx[2]->index[c_row], dvdx->df_dx[2]->size[c_row], x_ind); + _apply_usage_eq_3(1, _d2); + aux = 0; + dvdx->df_dx[2]->value[c_row][col] += aux; + } + if ((1 <= 1 && 1 <= 1) && (1 <= _d2 - 1 && _d2 - 1 <= 19)) { + x_ind = _idx_u(1, _d2 - 1); + col = pos(dvdx->df_dx[2]->index[c_row], dvdx->df_dx[2]->size[c_row], x_ind); + _apply_usage_eq_3(1, _d2); + aux = 0; + dvdx->df_dx[2]->value[c_row][col] += aux; + } + } + for (row = 1; row <= 19; row++) { + c_row = _c_index(row); + _get_eq_2_var_idxs(row, eq_var); + _get_u_idxs(eq_var); + if ((2 <= _d1 && _d1 <= 20) && (1 <= 1 && 1 <= 1)) { + x_ind = _idx_u(_d1, 1); + col = pos(dvdx->df_dx[1]->index[c_row], dvdx->df_dx[1]->size[c_row], x_ind); + _apply_usage_eq_2(_d1, 1); + aux = 0; + dvdx->df_dx[1]->value[c_row][col] += aux; + } + if ((1 <= _d1 - 1 && _d1 - 1 <= 19) && (1 <= 1 && 1 <= 1)) { + x_ind = _idx_u(_d1 - 1, 1); + col = pos(dvdx->df_dx[1]->index[c_row], dvdx->df_dx[1]->size[c_row], x_ind); + _apply_usage_eq_2(_d1, 1); + aux = 0; + dvdx->df_dx[1]->value[c_row][col] += aux; + } + } + for (row = 1; row <= 361; row++) { + c_row = _c_index(row); + _get_eq_4_var_idxs(row, eq_var); + _get_u_idxs(eq_var); + if ((2 <= _d1 && _d1 <= 20) && (2 <= _d2 && _d2 <= 20)) { + x_ind = _idx_u(_d1, _d2); + col = pos(dvdx->df_dx[3]->index[c_row], dvdx->df_dx[3]->size[c_row], x_ind); + _apply_usage_eq_4(_d1, _d2); + aux = 0; + dvdx->df_dx[3]->value[c_row][col] += aux; + } + if ((2 <= _d1 && _d1 <= 20) && (1 <= _d2 - 1 && _d2 - 1 <= 19)) { + x_ind = _idx_u(_d1, _d2 - 1); + col = pos(dvdx->df_dx[3]->index[c_row], dvdx->df_dx[3]->size[c_row], x_ind); + _apply_usage_eq_4(_d1, _d2); + aux = 0; + dvdx->df_dx[3]->value[c_row][col] += aux; + } + if ((1 <= _d1 - 1 && _d1 - 1 <= 19) && (2 <= _d2 && _d2 <= 20)) { + x_ind = _idx_u(_d1 - 1, _d2); + col = pos(dvdx->df_dx[3]->index[c_row], dvdx->df_dx[3]->size[c_row], x_ind); + _apply_usage_eq_4(_d1, _d2); + aux = 0; + dvdx->df_dx[3]->value[c_row][col] += aux; + } + } + // Assign Jacobian Matrix values for equation: 0 + for (row = 0; row < 1; row++) { + for (col = 0; col < dvdx->df_dx[0]->size[row]; col++) { + row_t = dvdx->df_dx[0]->index[row][col]; + _assign_jac(row_t, dvdx->df_dx[0]->value[row][col]); + } + } + // Assign Jacobian Matrix values for equation: 2 + for (row = 0; row < 19; row++) { + for (col = 0; col < dvdx->df_dx[2]->size[row]; col++) { + row_t = dvdx->df_dx[2]->index[row][col]; + _assign_jac(row_t, dvdx->df_dx[2]->value[row][col]); + } + } + // Assign Jacobian Matrix values for equation: 1 + for (row = 0; row < 19; row++) { + for (col = 0; col < dvdx->df_dx[1]->size[row]; col++) { + row_t = dvdx->df_dx[1]->index[row][col]; + _assign_jac(row_t, dvdx->df_dx[1]->value[row][col]); + } + } + // Assign Jacobian Matrix values for equation: 3 + for (row = 0; row < 361; row++) { + for (col = 0; col < dvdx->df_dx[3]->size[row]; col++) { + row_t = dvdx->df_dx[3]->index[row][col]; + _assign_jac(row_t, dvdx->df_dx[3]->value[row][col]); + } + } +} + +void CLC_initializeDataStructs(CLC_simulator simulator) +{ + simulator->data = CLC_Data(400, 0, 0, 0, 0, 4, 0, "advection2D"); + CLC_data modelData = simulator->data; + MODEL_DATA_ACCESS(modelData) + int *states = (int *)malloc(400 * sizeof(int)); + int *outputs = (int *)malloc(2 * sizeof(int)); + int row, eq_var, c_row; + int x_ind; + int _d1; + int _d2; + int _rg_d1; + int _rg_d2; + int i; + int j; + _ax = 1; + _ay = 1; + _r = 1; + _dx = 1; + _dy = 1; + _init_u(1, 1) = 1; + modelData->nSD[_idx_u(1, 1)]++; + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->nSD[_idx_u(_d1, 1)]++; + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->nSD[_idx_u(_d1 - 1, 1)]++; + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->nSD[_idx_u(1, _d1)]++; + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->nSD[_idx_u(1, _d1 - 1)]++; + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + for (_d2 = 2; _d2 <= 20; _d2 += 1) { + modelData->nSD[_idx_u(_d1, _d2)]++; + } + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + for (_d2 = 2; _d2 <= 20; _d2 += 1) { + modelData->nSD[_idx_u(_d1, _d2 - 1)]++; + } + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + for (_d2 = 2; _d2 <= 20; _d2 += 1) { + modelData->nSD[_idx_u(_d1 - 1, _d2)]++; + } + } + modelData->nDS[_idx_u(1, 1)]++; + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->nDS[_idx_u(_d1, 1)]++; + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->nDS[_idx_u(_d1, 1)]++; + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->nDS[_idx_u(1, _d1)]++; + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->nDS[_idx_u(1, _d1)]++; + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + for (_d2 = 2; _d2 <= 20; _d2 += 1) { + modelData->nDS[_idx_u(_d1, _d2)]++; + } + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + for (_d2 = 2; _d2 <= 20; _d2 += 1) { + modelData->nDS[_idx_u(_d1, _d2)]++; + } + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + for (_d2 = 2; _d2 <= 20; _d2 += 1) { + modelData->nDS[_idx_u(_d1, _d2)]++; + } + } + for (row = 1; row <= 1; row++) { + c_row = _c_index(row); + modelData->jac_matrices->df_dx[0]->size[c_row]++; + } + for (row = 1; row <= 19; row++) { + c_row = _c_index(row); + _get_eq_3_var_idxs(row, eq_var); + _get_u_idxs(eq_var); + if ((1 <= 1 && 1 <= 1) && (2 <= _d2 && _d2 <= 20)) { + modelData->jac_matrices->df_dx[2]->size[c_row]++; + } + if ((1 <= 1 && 1 <= 1) && (1 <= _d2 - 1 && _d2 - 1 <= 19)) { + modelData->jac_matrices->df_dx[2]->size[c_row]++; + } + } + for (row = 1; row <= 19; row++) { + c_row = _c_index(row); + _get_eq_2_var_idxs(row, eq_var); + _get_u_idxs(eq_var); + if ((2 <= _d1 && _d1 <= 20) && (1 <= 1 && 1 <= 1)) { + modelData->jac_matrices->df_dx[1]->size[c_row]++; + } + if ((1 <= _d1 - 1 && _d1 - 1 <= 19) && (1 <= 1 && 1 <= 1)) { + modelData->jac_matrices->df_dx[1]->size[c_row]++; + } + } + for (row = 1; row <= 361; row++) { + c_row = _c_index(row); + _get_eq_4_var_idxs(row, eq_var); + _get_u_idxs(eq_var); + if ((2 <= _d1 && _d1 <= 20) && (2 <= _d2 && _d2 <= 20)) { + modelData->jac_matrices->df_dx[3]->size[c_row]++; + } + if ((2 <= _d1 && _d1 <= 20) && (1 <= _d2 - 1 && _d2 - 1 <= 19)) { + modelData->jac_matrices->df_dx[3]->size[c_row]++; + } + if ((1 <= _d1 - 1 && _d1 - 1 <= 19) && (2 <= _d2 && _d2 <= 20)) { + modelData->jac_matrices->df_dx[3]->size[c_row]++; + } + } + CLC_allocDataMatrix(modelData); + cleanVector(states, 0, 400); + modelData->SD[_idx_u(1, 1)][states[_idx_u(1, 1)]++] = _idx_u(1, 1); + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->SD[_idx_u(_d1, 1)][states[_idx_u(_d1, 1)]++] = _idx_u(_d1, 1); + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->SD[_idx_u(_d1 - 1, 1)][states[_idx_u(_d1 - 1, 1)]++] = _idx_u(_d1, 1); + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->SD[_idx_u(1, _d1)][states[_idx_u(1, _d1)]++] = _idx_u(1, _d1); + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->SD[_idx_u(1, _d1 - 1)][states[_idx_u(1, _d1 - 1)]++] = _idx_u(1, _d1); + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + for (_d2 = 2; _d2 <= 20; _d2 += 1) { + modelData->SD[_idx_u(_d1, _d2)][states[_idx_u(_d1, _d2)]++] = _idx_u(_d1, _d2); + } + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + for (_d2 = 2; _d2 <= 20; _d2 += 1) { + modelData->SD[_idx_u(_d1, _d2 - 1)][states[_idx_u(_d1, _d2 - 1)]++] = _idx_u(_d1, _d2); + } + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + for (_d2 = 2; _d2 <= 20; _d2 += 1) { + modelData->SD[_idx_u(_d1 - 1, _d2)][states[_idx_u(_d1 - 1, _d2)]++] = _idx_u(_d1, _d2); + } + } + cleanVector(states, 0, 400); + modelData->DS[_idx_u(1, 1)][states[_idx_u(1, 1)]++] = _idx_u(1, 1); + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->DS[_idx_u(_d1, 1)][states[_idx_u(_d1, 1)]++] = _idx_u(_d1, 1); + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->DS[_idx_u(_d1, 1)][states[_idx_u(_d1, 1)]++] = _idx_u(_d1 - 1, 1); + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->DS[_idx_u(1, _d1)][states[_idx_u(1, _d1)]++] = _idx_u(1, _d1); + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + modelData->DS[_idx_u(1, _d1)][states[_idx_u(1, _d1)]++] = _idx_u(1, _d1 - 1); + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + for (_d2 = 2; _d2 <= 20; _d2 += 1) { + modelData->DS[_idx_u(_d1, _d2)][states[_idx_u(_d1, _d2)]++] = _idx_u(_d1, _d2); + } + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + for (_d2 = 2; _d2 <= 20; _d2 += 1) { + modelData->DS[_idx_u(_d1, _d2)][states[_idx_u(_d1, _d2)]++] = _idx_u(_d1, _d2 - 1); + } + } + for (_d1 = 2; _d1 <= 20; _d1 += 1) { + for (_d2 = 2; _d2 <= 20; _d2 += 1) { + modelData->DS[_idx_u(_d1, _d2)][states[_idx_u(_d1, _d2)]++] = _idx_u(_d1 - 1, _d2); + } + } + cleanVector(states, 0, 400); + cleanVector(states, 0, 400); + for (row = 1; row <= 1; row++) { + c_row = _c_index(row); + x_ind = _idx_u(1, 1); + if (in(modelData->jac_matrices->df_dx[0]->index[c_row], modelData->jac_matrices->df_dx[0]->size[c_row], x_ind)) { + modelData->jac_matrices->df_dx[0]->size[c_row]--; + } else { + modelData->jac_matrices->df_dx[0]->index[c_row][states[c_row]++] = x_ind; + } + } + cleanVector(states, 0, 400); + for (row = 1; row <= 19; row++) { + c_row = _c_index(row); + _get_eq_3_var_idxs(row, eq_var); + _get_u_idxs(eq_var); + if ((1 <= 1 && 1 <= 1) && (2 <= _d2 && _d2 <= 20)) { + x_ind = _idx_u(1, _d2); + if (in(modelData->jac_matrices->df_dx[2]->index[c_row], modelData->jac_matrices->df_dx[2]->size[c_row], x_ind)) { + modelData->jac_matrices->df_dx[2]->size[c_row]--; + } else { + modelData->jac_matrices->df_dx[2]->index[c_row][states[c_row]++] = x_ind; + } + } + if ((1 <= 1 && 1 <= 1) && (1 <= _d2 - 1 && _d2 - 1 <= 19)) { + x_ind = _idx_u(1, _d2 - 1); + if (in(modelData->jac_matrices->df_dx[2]->index[c_row], modelData->jac_matrices->df_dx[2]->size[c_row], x_ind)) { + modelData->jac_matrices->df_dx[2]->size[c_row]--; + } else { + modelData->jac_matrices->df_dx[2]->index[c_row][states[c_row]++] = x_ind; + } + } + } + cleanVector(states, 0, 400); + for (row = 1; row <= 19; row++) { + c_row = _c_index(row); + _get_eq_2_var_idxs(row, eq_var); + _get_u_idxs(eq_var); + if ((2 <= _d1 && _d1 <= 20) && (1 <= 1 && 1 <= 1)) { + x_ind = _idx_u(_d1, 1); + if (in(modelData->jac_matrices->df_dx[1]->index[c_row], modelData->jac_matrices->df_dx[1]->size[c_row], x_ind)) { + modelData->jac_matrices->df_dx[1]->size[c_row]--; + } else { + modelData->jac_matrices->df_dx[1]->index[c_row][states[c_row]++] = x_ind; + } + } + if ((1 <= _d1 - 1 && _d1 - 1 <= 19) && (1 <= 1 && 1 <= 1)) { + x_ind = _idx_u(_d1 - 1, 1); + if (in(modelData->jac_matrices->df_dx[1]->index[c_row], modelData->jac_matrices->df_dx[1]->size[c_row], x_ind)) { + modelData->jac_matrices->df_dx[1]->size[c_row]--; + } else { + modelData->jac_matrices->df_dx[1]->index[c_row][states[c_row]++] = x_ind; + } + } + } + cleanVector(states, 0, 400); + for (row = 1; row <= 361; row++) { + c_row = _c_index(row); + _get_eq_4_var_idxs(row, eq_var); + _get_u_idxs(eq_var); + if ((2 <= _d1 && _d1 <= 20) && (2 <= _d2 && _d2 <= 20)) { + x_ind = _idx_u(_d1, _d2); + if (in(modelData->jac_matrices->df_dx[3]->index[c_row], modelData->jac_matrices->df_dx[3]->size[c_row], x_ind)) { + modelData->jac_matrices->df_dx[3]->size[c_row]--; + } else { + modelData->jac_matrices->df_dx[3]->index[c_row][states[c_row]++] = x_ind; + } + } + if ((2 <= _d1 && _d1 <= 20) && (1 <= _d2 - 1 && _d2 - 1 <= 19)) { + x_ind = _idx_u(_d1, _d2 - 1); + if (in(modelData->jac_matrices->df_dx[3]->index[c_row], modelData->jac_matrices->df_dx[3]->size[c_row], x_ind)) { + modelData->jac_matrices->df_dx[3]->size[c_row]--; + } else { + modelData->jac_matrices->df_dx[3]->index[c_row][states[c_row]++] = x_ind; + } + } + if ((1 <= _d1 - 1 && _d1 - 1 <= 19) && (2 <= _d2 && _d2 <= 20)) { + x_ind = _idx_u(_d1 - 1, _d2); + if (in(modelData->jac_matrices->df_dx[3]->index[c_row], modelData->jac_matrices->df_dx[3]->size[c_row], x_ind)) { + modelData->jac_matrices->df_dx[3]->size[c_row]--; + } else { + modelData->jac_matrices->df_dx[3]->index[c_row][states[c_row]++] = x_ind; + } + } + } + SD_setupJacMatrices(modelData->jac_matrices); + simulator->output = SD_Output("advection2D", 2, 0, 400, NULL, 0, 0, CI_Step, SD_Memory, MOD_output); + SD_output modelOutput = simulator->output; + modelOutput->nOS[_idx_out_exp_2]++; + modelOutput->nOS[_idx_out_exp_1]++; + modelOutput->nSO[_idx_u(1, 1)]++; + modelOutput->nSO[_idx_u(20, 20)]++; + SD_allocOutputMatrix(modelOutput, 400, 0); + sprintf(modelOutput->variable[_idx_out_exp_1].name, "u[20,20]"); + sprintf(modelOutput->variable[_idx_out_exp_2].name, "u[1,1]"); + cleanVector(outputs, 0, 2); + modelOutput->OS[_idx_out_exp_2][outputs[_idx_out_exp_2]++] = _idx_u(1, 1); + modelOutput->OS[_idx_out_exp_1][outputs[_idx_out_exp_1]++] = _idx_u(20, 20); + cleanVector(states, 0, 400); + modelOutput->SO[_idx_u(1, 1)][states[_idx_u(1, 1)]++] = _idx_out_exp_2; + modelOutput->SO[_idx_u(20, 20)][states[_idx_u(20, 20)]++] = _idx_out_exp_1; + simulator->model = CLC_Model(MOD_definition, MOD_zeroCrossing, MOD_handlerPos, MOD_handlerNeg, MOD_jacobian); + free(states); + free(outputs); +} + +void QSS_initializeDataStructs(QSS_simulator simulator) {} diff --git a/test/partitioner/sim_models/advection_2D/advection2D.h b/test/partitioner/sim_models/advection_2D/advection2D.h new file mode 100644 index 00000000..7c8b330d --- /dev/null +++ b/test/partitioner/sim_models/advection_2D/advection2D.h @@ -0,0 +1,97 @@ +// Model data access macro. + +#define MODEL_DATA_ACCESS(m) double* x = m->x; + +// Model Variables Macros + +// Macros definition for variable: _out_exp_1 +#define _idx_out_exp_1 0 +#define _eval_out_exp_1 0 + +// Macros definition for variable: _out_exp_2 +#define _idx_out_exp_2 1 +#define _eval_out_exp_2 1 + +// Macros definition for variable: ax +#define _ax __PAR__ax + +// Macros definition for variable: ay +#define _ay __PAR__ay + +// Macros definition for variable: dx +#define _dx __PAR__dx + +// Macros definition for variable: dy +#define _dy __PAR__dy + +// Macros definition for variable: r +#define _r __PAR__r + +// Macros definition for variable: u +#define _idx_u(d1, d2) ((d1 - 1) * 20 + (d2 - 1)) +#define _u(d1, d2) x[_idx_u(d1, d2)] +#define _init_u(d1, d2) x[_idx_u(d1, d2)] +#define _eval_u(d1, d2) ((d1 - 1) * 20 + (d2 - 1)) +#define _is_var_u(idx) idx >= 0 && idx < 400 +#define _get_u_idxs(idx) \ + _d1 = ((idx) / 20) + 1; \ + _d2 = ((idx) % 20) + 1; + +// Model Parameters Declaration + +// Macro for parameter: ax +double __PAR__ax; +// Macro for parameter: ay +double __PAR__ay; +// Macro for parameter: dx +double __PAR__dx; +// Macro for parameter: dy +double __PAR__dy; +// Macro for parameter: r +double __PAR__r; + +// Derivative Equations Macros + +// Macros for equation: 1 + +// Macros for equation: 2 +#define _apply_usage_eq_2(_d1, _d2) i = _d1; +#define _get_eq_2_var_idxs(row, var) \ + _rg_d1 = 1 + (row - 1) + 1; \ + _rg_d2 = 1; \ + var = _idx_u(_rg_d1, _rg_d2); +// Macros for equation: 3 +#define _apply_usage_eq_3(_d1, _d2) j = _d2; +#define _get_eq_3_var_idxs(row, var) \ + _rg_d2 = 1 + (row - 1) + 1; \ + _rg_d1 = 1; \ + var = _idx_u(_rg_d1, _rg_d2); +// Macros for equation: 4 +#define _apply_usage_eq_4(_d1, _d2) \ + i = _d1; \ + j = _d2; +#define _get_eq_4_var_idxs(row, var) \ + _rg_d1 = 1 + ((row - 1) / 19) + 1; \ + _rg_d2 = 1 + ((row - 1) % 19) + 1; \ + var = _idx_u(_rg_d1, _rg_d2); + +// Output Equations Macros + +// Macros for output equation: 1 + +// Macros for output equation: 2 + +#define _out out[0] + +// Jacobian Macros definition. +#define _assign_jac(r, val) \ + col_t = dvdx->df_dx_t->size[r] + dvdx->df_dx_t->index[r][0]; \ + dvdx->df_dx_t->index[r][0]++; \ + jac[col_t] = val; +#define _c_index(i) (i - 1) + +#define _time t + +// Derivative Macros definition. +// Derivative definition for variable: u +#define _der_u(d1, d2) dx[((d1 - 1) * 20 + (d2 - 1))] diff --git a/test/partitioner/sim_models/advection_2D/advection2D.mo b/test/partitioner/sim_models/advection_2D/advection2D.mo new file mode 100644 index 00000000..66975df4 --- /dev/null +++ b/test/partitioner/sim_models/advection_2D/advection2D.mo @@ -0,0 +1,57 @@ +model advection2D + constant Integer N = 1000; + parameter Real ax; + parameter Real ay; + parameter Real r; + parameter Real dx; + parameter Real dy; + Real u[N,N]; + + initial algorithm + + ax := 1; + ay := 1; + r := 1; + dx := 1; + dy := 1; + + u[1,1] := 1; + + equation + + // Origin equation + der(u[1,1])=-ax*u[1,1]/dx - ay*u[1,1]/dy + r*(u[1,1]^2-u[1,1]^3); + + // Borders + for i in 2:N loop + der(u[i,1])=-ax*u[i,1]/dx - ay*(u[i,1] - u[i-1,1])/dy + r*(u[i,1]^2-u[i,1]^3); + end for; + + // Borders + for j in 2:N loop + der(u[1,j])=-ax*(u[1,j] - u[1,j-1])/dx - ay*u[1,j]/dy + r*(u[1,j]^2-u[1,j]^3); + end for; + + // Rest of the grid. + for i in 2:N, j in 2:N loop + der(u[i, j])=-ax*(u[i,j] - u[i,j-1])/dx - ay*(u[i,j] - u[i-1,j])/dy + r*(u[i,j]^2-u[i,j]^3); + end for; + annotation( + experiment( + MMO_Description = " Advection", + MMO_Solver = LIQSS2, + MMO_Parallel = true, + MMO_PartitionMethod = Manual, + MMO_LPS = 12, + MMO_DT_Min = 1, + Jacobian = Dense, + MMO_BDF_PDepth = 1, + MMO_BDF_Max_Step = 0, + StartTime = 0.0, + StopTime = 300, + Tolerance = {1e-3}, + AbsTolerance = {1e-3} + ) +); + +end advection2D; diff --git a/test/partitioner/sim_models/advection_2D/run_advection.bash b/test/partitioner/sim_models/advection_2D/run_advection.bash new file mode 100644 index 00000000..4a1ffd6d --- /dev/null +++ b/test/partitioner/sim_models/advection_2D/run_advection.bash @@ -0,0 +1,20 @@ +#!/bin/bash + +echo "Starting to process partition files..." + +# 1. Loop through all files ending in .partition +for file in *.partition; do + + # Check if any matching files actually exist (prevents errors if the folder is empty) + [ -e "$file" ] || continue + + echo "--------------------------------------------" + echo "Found partition file: $file" + echo "--------------------------------------------" + + # 2. Run the simulation + python3 ../sim_models.py -m advection2D.mo -p "$file" + +done + +echo "All partition files processed!" \ No newline at end of file diff --git a/test/partitioner/sim_models/air_conditioners/airconds.mo b/test/partitioner/sim_models/air_conditioners/airconds.mo new file mode 100644 index 00000000..b2b053a6 --- /dev/null +++ b/test/partitioner/sim_models/air_conditioners/airconds.mo @@ -0,0 +1,80 @@ +model airconds + import math; + constant Integer N = 100000; + parameter Real CAP[N], RES[N], POT[N], THA = 32,pmax=0; + Real th[N]; + discrete Real on[N]; + discrete Real tref[N]; + discrete Real nextSample[N]; + discrete Real noise[N]; + discrete Real nextTref[N]; + + initial algorithm + for i in 1:N loop + th[i] := rand(4)+ 18; + CAP[i] := rand(100)+ 550; + RES[i] := rand(0.4)+ 1.8; + POT[i] := rand(2)+ 13; + pmax:=pmax+POT[i]; + nextSample[i] := 1; + noise[i] := rand(2)-1; + tref[i] := 20; + nextTref[i] := 1000; + end for; + for i in 1:N loop + if th[i] - tref[i] - 0.5 > 0 then + on[i] := 1; + end if; + end for; + + equation + for i in 1:N loop + der(th[i]) = (THA/RES[i]-POT[i]*on[i]-th[i]/RES[i]+noise[i]/RES[i])/CAP[i]; + end for; + + algorithm + for i in 1:N loop + when th[i] - tref[i] + on[i] - 0.5 > 0 then + on[i] := 1; + elsewhen th[i] - tref[i] + on[i] - 0.5 < 0 then + on[i] := 0; + end when; + end for; + for i in 1:N loop + when time > nextTref[i] then + if (nextTref[i] == 1000) then + tref[i] := 20.5; + else + tref[i] := 20; + end if; + nextTref[i] := 2000; + end when; + end for; + for i in 1:N loop + when time > nextSample[i] then + nextSample[i] := nextSample[i]+1; + noise[i] := 2*abs(sin(i*time))-1; + end when; + end for; + annotation( + experiment( + MMO_Description = "Power consumption in a large population of air conditioners.", + MMO_Solver = QSS2, + MMO_Period = {3000/5000}, + MMO_Parallel = true, + MMO_PartitionMethod = Metis, + MMO_LPS = 4, + MMO_DT_Synch = SD_DT_Fixed, + MMO_DT_Min = 3000, + MMO_OutputType = CI_Sampled, + Jacobian = Dense, + MMO_BDF_PDepth = 1, + MMO_BDF_Max_Step = 0, + StartTime = 0, + StopTime = 3000, + Tolerance = {1e-3}, + AbsTolerance = {1e-3} + ) +); + +end airconds; diff --git a/test/partitioner/sim_models/air_conditioners/run_air_conditioners.bash b/test/partitioner/sim_models/air_conditioners/run_air_conditioners.bash new file mode 100644 index 00000000..11f7c27e --- /dev/null +++ b/test/partitioner/sim_models/air_conditioners/run_air_conditioners.bash @@ -0,0 +1,21 @@ +#!/bin/bash + +echo "Starting to process partition files..." + + +# 1. Loop through all files ending in .partition +for file in *.partition; do + + # Check if any matching files actually exist (prevents errors if the folder is empty) + [ -e "$file" ] || continue + + echo "--------------------------------------------" + echo "Found partition file: $file" + echo "--------------------------------------------" + + # 2. Run the simulation + python3 ../sim_models.py -m airconds.mo -p "$file" + +done + +echo "All partition files processed!" \ No newline at end of file diff --git a/test/partitioner/sim_models/airconds_cont/airconds_cont.mo b/test/partitioner/sim_models/airconds_cont/airconds_cont.mo index 0cd8109c..d7bc32e1 100644 --- a/test/partitioner/sim_models/airconds_cont/airconds_cont.mo +++ b/test/partitioner/sim_models/airconds_cont/airconds_cont.mo @@ -7,8 +7,8 @@ model airconds_cont end getSection; import math; - constant Integer N = 4000; - constant Integer SECTIONS = 4; + constant Integer N = 8000; + constant Integer SECTIONS = 8; parameter Real CAP[N], RES[N], POT[N], THA = 32,pmax=0,Kp=1,Ki=1,tref=20; parameter Integer sections[N]; parameter Integer sections_init[SECTIONS]; @@ -114,23 +114,23 @@ model airconds_cont end when; end for; annotation( + experiment( + MMO_Description = "Control of the power consumption of a large populaion of air conditioners.", + MMO_Solver = QSS2, + MMO_Period = {3000/5000}, + MMO_Parallel = true, + MMO_PartitionMethod = Manual, + MMO_LPS = 8, + MMO_DT_Synch = SD_DT_Fixed, + MMO_DT_Min = 10, + Jacobian = Dense, + MMO_BDF_PDepth = 1, + MMO_BDF_Max_Step = 0, + StartTime = 0, + StopTime = 3000, + Tolerance = {1e-4}, + AbsTolerance = {1e-4} + ) +); - experiment( - MMO_Description="Control of the power consumption of a large populaion of air conditioners.", - MMO_Solver=QSS2, - MMO_Period={3000/5000}, - MMO_Parallel=true, - MMO_PartitionMethod=Manual, - MMO_LPS=4, - MMO_DT_Synch=SD_DT_Fixed, - MMO_DT_Min=10, - MMO_Output={ptotals}, - Jacobian=Dense, - MMO_BDF_PDepth=1, - MMO_BDF_Max_Step=0, - StartTime=0, - StopTime=3000, - Tolerance={1e-4}, - AbsTolerance={1e-4} - )); end airconds_cont; diff --git a/test/partitioner/sim_models/airconds_cont/airconds_cont.part b/test/partitioner/sim_models/airconds_cont/airconds_cont.part deleted file mode 100644 index 7f663bb8..00000000 --- a/test/partitioner/sim_models/airconds_cont/airconds_cont.part +++ /dev/null @@ -1,12013 +0,0 @@ -1 -0 -0 -0 -0 -1 -1 -0 -0 -0 -0 -0 -0 -1 -1 -1 -1 -1 -0 -0 -0 -1 -1 -1 -1 -1 -0 -1 -0 -1 -1 -0 -1 -0 -0 -1 -1 -0 -1 -0 -1 -0 -1 -1 -0 -1 -1 -0 -0 -1 -1 -0 -1 -0 -1 -0 -0 -0 -0 -0 -0 -0 -1 -1 -1 -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-3 diff --git a/test/partitioner/sim_models/airconds_cont/run_airconds_cont.bash b/test/partitioner/sim_models/airconds_cont/run_airconds_cont.bash new file mode 100644 index 00000000..92bcaa9a --- /dev/null +++ b/test/partitioner/sim_models/airconds_cont/run_airconds_cont.bash @@ -0,0 +1,21 @@ +#!/bin/bash + +echo "Starting to process partition files..." + + +# 1. Loop through all files ending in .partition +for file in *.partition; do + + # Check if any matching files actually exist (prevents errors if the folder is empty) + [ -e "$file" ] || continue + + echo "--------------------------------------------" + echo "Found partition file: $file" + echo "--------------------------------------------" + + # 2. Run the simulation + python3 ../sim_models.py -m airconds_cont.mo -p "$file" + +done + +echo "All partition files processed!" \ No newline at end of file diff --git a/test/partitioner/sim_models/airconds_cont/sections.c b/test/partitioner/sim_models/airconds_cont/sections.c index 68a5926b..944fe53b 100644 --- a/test/partitioner/sim_models/airconds_cont/sections.c +++ b/test/partitioner/sim_models/airconds_cont/sections.c @@ -1,6 +1,6 @@ -int -getSection(int i) +int getSection(int i) { - int ret = ((i-1)/1000)+1; + int partition_size = 1000; + int ret = ((i - 1) / partition_size) + 1; return (ret); } diff --git a/test/partitioner/sim_models/sim_models.py b/test/partitioner/sim_models/sim_models.py index 186c08ca..52019181 100644 --- a/test/partitioner/sim_models/sim_models.py +++ b/test/partitioner/sim_models/sim_models.py @@ -1,26 +1,24 @@ #!/usr/bin/env python3 """ -Initial example script that runs `airconds_cont` model which has a parallel config on the sim, -we should use this as a template to generate the entire pipeline from partition calcaultion to -simulation results. -Extensions: -* Modify the model to allow different sizes. -* Modify the config to setup different number of LPs -* Filter the simulation results. +Tuned simulation pipeline script running the QSS Solver model. +Accepts the model path and the partition filename as arguments to +dynamically extract sizing configuration and update model constants. Requirements: -* Download and install QSS Solver from: https://github.com/CIFASIS/qss-solver/releases/tag/v6.0.2 +* QSS Solver installed with python bindings available. Run: -python ./sim_models.py +python ./sim_models.py --model ./airconds_cont/airconds_cont.mo --partition airconds_cont_100_.json-sbg-4-1213.partition """ +import argparse import glob import os import sys import logging import re from pathlib import Path +import shutil # Add the qss_solver module to the Python path # Adjust this path according to your installation @@ -29,13 +27,10 @@ try: from qss_solver import ( file_handlers, - # Simulation functions compile_model, execute_model, run, - # Model manipulation functions annotations, set_annotations, - constants, parameters, set_constant, set_parameter, set_constants, set_parameters, + constants, set_constants, parameters, set_parameters, config, set_config, - # Result functions simulation_log, output_files ) except ImportError as e: @@ -43,47 +38,34 @@ print("Make sure the qss_solver package is in your Python path") sys.exit(1) + def concatenate_proc_logs_from_dir(directory, model_name, output_filename=None): - """ - Concatenates files with pattern: {model_name}-{proc_number}.log - from a specific input directory. - """ - # 1. Ensure the directory path is absolute or correctly relative + """Concatenates files with pattern: {model_name}-{proc_number}.log""" directory = os.path.expanduser(directory) - - # 2. Define the search pattern within that directory search_pattern = os.path.join(directory, f"{model_name}-*.log") - - # 3. Find all matching files files = glob.glob(search_pattern) if not files: print(f"No log files found for model '{model_name}' in: {directory}") return - # 4. Sort files numerically by the proc_number def extract_proc_num(filename): - # We look for the number at the end of the filename string match = re.search(r'-(\d+)\.log$', filename) return int(match.group(1)) if match else -1 files.sort(key=extract_proc_num) - # 5. Determine output path (defaulting to the same input directory) if not output_filename: output_filename = os.path.join(directory, f"{model_name}-combined.log") else: - # If a filename is provided without a path, put it in the input dir if not os.path.isabs(output_filename) and os.sep not in output_filename: output_filename = os.path.join(directory, output_filename) try: with open(output_filename, 'a') as outfile: for fname in files: - # We skip the output file if it already exists to avoid recursion if os.path.abspath(fname) == os.path.abspath(output_filename): continue - print(f"Appending {os.path.basename(fname)}...") with open(fname, 'r') as infile: outfile.write(infile.read()) @@ -91,20 +73,13 @@ def extract_proc_num(filename): print(f"\nSuccessfully created: {output_filename}") return output_filename - except Exception as e: - print(f"An error occurred: {e}") + print(f"An error occurred while concatenating logs: {e}") + def setup_environment(): - """ - Configure required environment variables for QSS Solver. - - Adjust these paths according to your QSS Solver installation. - """ - # Base installation directory + """Configure required environment variables for QSS Solver.""" qss_base = Path.home() / "qss-solver" - - # Required environment variables env_vars = { 'MMOC_BIN': os.path.join(qss_base, 'bin'), 'MMOC_MODELS': os.path.join(qss_base, 'models'), @@ -112,12 +87,10 @@ def setup_environment(): 'MMOC_BUILD': os.path.join(qss_base, 'build') } - # Set environment variables for var, value in env_vars.items(): os.environ[var] = value print(f"Set {var} = {value}") - # Create directories if they don't exist for var in ['MMOC_OUTPUT', 'MMOC_BUILD']: path = os.environ[var] Path(path).mkdir(parents=True, exist_ok=True) @@ -125,6 +98,7 @@ def setup_environment(): return env_vars + def configure_logging(): """Configure logging for detailed output.""" logging.basicConfig( @@ -136,37 +110,29 @@ def configure_logging(): ] ) + def clean_parallel_log(file_path): try: - # 1. Read the existing content with open(file_path, 'r') as f: content = f.read() - # 2. Apply cleaning logic - # Remove "Parallel Simulation Statistics:" cleaned = content.replace("Parallel Simulation Statistics:", "") - - # Remove (DD.DD%) patterns cleaned = re.sub(r'\(\d+\.?\d*\s*%\)', '', cleaned) - - # Remove specific symbols and units cleaned = cleaned.replace("%", "").replace("MBytes", "") - # Clean up whitespace line by line final_lines = [line.strip() for line in cleaned.splitlines()] final_output = "\n".join(final_lines) - # 3. Overwrite the original file with cleaned data with open(file_path, 'w') as f: f.write(final_output) print(f"Successfully cleaned and updated: {file_path}") - except FileNotFoundError: print(f"Error: The file '{file_path}' was not found.") except Exception as e: print(f"An error occurred: {e}") + def sim_results(log_file): clean_parallel_log(log_file) print("\nSimulation results:") @@ -175,62 +141,158 @@ def sim_results(log_file): for key, value in log_data.items(): print(f" {key}: {value}") + def process_model_logs(directory, model_name, proc_func): - """ - Lists files matching {model_name}-{natural_number}.log and - applies processing_func to each file path. - """ directory = os.path.expanduser(directory) - # Pattern to find potential matches search_pattern = os.path.join(directory, f"{model_name}-*.log") - - # Regex to strictly match {model_name}-{natural_number}.log - # \d+ ensures it is a natural number (1 or more digits) pattern = re.compile(rf"^{re.escape(model_name)}-(\d+)\.log$") matched_files = [] - - # 1. List and filter files for filepath in glob.glob(search_pattern): filename = os.path.basename(filepath) if pattern.match(filename): matched_files.append(filepath) - # 2. Sort numerically (LP 0, LP 1, LP 2...) matched_files.sort(key=lambda x: int(pattern.match(os.path.basename(x)).group(1))) if not matched_files: print(f"No files found matching the pattern in {directory}") return - # 3. Apply your custom function print(f"Found {len(matched_files)} files. Starting processing...") for log_path in matched_files: proc_func(log_path) - print("All files processed successfully.") -def execute_simulation(model_name): + +def extract_size_from_partition(partition_name): """ - Basic example: compile and run a model with default settings. - - Args: - model_name (str): full path to the model. + Extracts the 'size' value from the partition filename layout: + model_name_size_.json-method-4-1213.partition """ - print(f"\n=== Basic Simulation Example: {model_name} ===") + # Pattern tracks the digits between the model prefix separator and the trailing target items + partition_p = Path(partition_name).resolve() + print(str(partition_p.name)) + pattern = r'(\d+)_?\.json-[^-]+-(\d+)-' + # match = re.search(r'(\d+)_?\.json-', partition_name) + match = re.search(pattern, str(partition_p)) + if match: + size = int(match.group(1)) + parts = int(match.group(2)) + return size, parts + else: + raise ValueError( + f"Could not parse size and parts from partition filename layout: '{partition_name}'" + ) + if match: + return int(match.group(1)) + else: + raise ValueError( + f"Could not parse size variant integer from partition filename layout: '{partition_name}'" + ) + + +def generate_sections_file(size, partitions, destination): + """ + Generates the C file and copies it to the specified build directory. + """ + filename = "sections.c" + partition_size = size // partitions + + c_code = f"""int +getSection(int i) +{{ + int partition_size = {partition_size}; + int ret = ((i - 1) / partition_size) + 1; + return (ret); +}} +""" + try: + # 1. Write the file + with open(filename, "w") as f: + f.write(c_code) + print(f"Successfully generated '{filename}' with partition_size {partition_size}.") + + # 2. Copy the file to the destination + if not os.path.exists(destination): + print(f"Error: Destination directory '{destination}' does not exist.") + return + + shutil.copy(filename, destination) + print(f"Successfully copied '{filename}' to '{destination}'.") + + except IOError as e: + print(f"An error occurred during file operations: {e}") + except PermissionError: + print(f"Permission denied: Unable to write to '{destination}'.") + + +def execute_simulation(model_path, partition_name): + """ + Compiles and executes the model dynamically, assigning N size constraints first. + """ + print(f"\n=== Executing Automated Pipeline for: {model_path} ===") - model_file = model_name + model_p = Path(model_path).resolve() + model_file = str(model_p) + model = file_handlers.get_file_name(model_file) + + partition_p = Path(partition_name).resolve() + + size, parts = extract_size_from_partition(partition_name) + print(f"Extracted targeted structural size: {size}") + print(f"Extracted parts: {parts}") + + # Derive target destination (.part in the model's directory) + target_partition_path = model_p.with_suffix('.part') + build_target_path = new_path = Path(os.environ['MMOC_BUILD']) / model + print("BUILD TARGET") + print(build_target_path) - # Step 1: Check current model annotations - print("Current model annotations:") + # Step 1: Copy custom partition to the required solver location + try: + print(f"Copying partition: {partition_p.name} -> {target_partition_path}") + shutil.copy2(Path(partition_name).resolve(), target_partition_path) + shutil.copy2(target_partition_path, build_target_path) + except Exception as e: + print(f"Failed to copy partition file: {e}") + return False + + # Step 1: Parse and adapt Constant N values + try: + + current_constants = constants(model_file) or {} + print(f"Current structural Constants: {current_constants}") + + # Updating constant N size property + set_constants(model_file, {"N": size, "SECTIONS" : parts}) + print(f"Successfully set Constant 'N' to: {size}") + print(f"Successfully set Constant 'SECTIONS' to: {parts}") + + except Exception as e: + print(f"Failed handling structural setup attributes: {e}") + return False + + # Step 2: Read current model annotations + print("\nCurrent model annotations:") current_ann = annotations(model_file) + if "MMO_Output" in current_ann: + del current_ann["MMO_Output"] + current_ann["MMO_LPS"] = parts + current_ann["MMO_Parallel"] = "true" + set_annotations(model_file, current_ann) + current_ann = annotations(model_file) + if current_ann: for key, value in current_ann.items(): print(f" {key}: {value}") else: print(" No annotations found") - - # Step 2: Compile the model + + if model == "airconds_cont": + generate_sections_file(size, parts, build_target_path) + + # Step 3: Compile the model print(f"\nCompiling model: {model_file}") if compile_model(model_file): print(" Compilation successful") @@ -238,7 +300,7 @@ def execute_simulation(model_name): print(" Compilation failed") return False - # Step 3: Run the simulation + # Step 4: Run the simulation print(f"\nRunning simulation: {model_file}") if execute_model(model_file): print(" Simulation completed successfully") @@ -246,172 +308,45 @@ def execute_simulation(model_name): print(" Simulation failed") return False - # Step 4: Get simulation results - - model = file_handlers.get_file_name(model_name) + # Step 5: Get simulation results model_log_path = file_handlers.get_full_path(model, 'MMOC_LOG') sim_results(model_log_path) process_model_logs(file_handlers.get_base_path(model_log_path, 'MMOC_LOG'), model, sim_results) return True -def example_advanced_configuration(model_name): - """ - Advanced example: modify model parameters and annotations before simulation. - - Args: - model_name (str): Name of the Modelica model file (without .mo extension) - """ - print(f"\n=== Advanced Configuration Example: {model_name} ===") - - model_file = f"{model_name}.mo" - - # Step 1: Read current model parameters and constants - print("Current model parameters:") - try: - current_params = parameters(model_file) - if current_params: - for key, value in current_params.items(): - print(f" {key}: {value}") - else: - print(" No parameters found") - except Exception as e: - print(f" Error reading parameters: {e}") - - print("\nCurrent model constants:") - try: - current_constants = constants(model_file) - if current_constants: - for key, value in current_constants.items(): - print(f" {key}: {value}") - else: - print(" No constants found") - except Exception as e: - print(f" Error reading constants: {e}") - - # Step 2: Modify model annotations - new_annotations = { - 'startTime': 0.0, - 'stopTime': 10.0, - 'tolerance': 1e-8, - 'stepSize': 1e-4 - } - - print(f"\nSetting new annotations:") - for key, value in new_annotations.items(): - print(f" {key}: {value}") - - set_annotations(model_file, new_annotations) - - # Step 3: Modify model parameters (example values) - new_parameters = { - 'k': 2.5, - 'omega': 1.0, - 'damping': 0.1 - } - - print(f"\nSetting new parameters:") - for key, value in new_parameters.items(): - print(f" {key}: {value}") - - try: - set_parameters(model_file, new_parameters) - except Exception as e: - print(f" Warning: Could not set parameters: {e}") - - # Step 4: Run the complete simulation - print(f"\nRunning complete simulation: {model_file}") - success = run(model_file, "") # With optimization flags - - if success: - print(" Simulation completed successfully") - - # Step 5: Process results - log_data = simulation_log(model_name) - if log_data: - print("\nPerformance metrics:") - for key, value in log_data.items(): - print(f" {key}: {value} ms") - else: - print(" Simulation failed") - - return success -def example_batch_simulation(model_name, parameter_sets): - """ - Batch simulation example: run multiple simulations with different parameter sets. - - Args: - model_name (str): Name of the Modelica model file - parameter_sets (list): List of dictionaries containing parameter sets - """ - print(f"\n=== Batch Simulation Example: {model_name} ===") - - model_file = f"{model_name}.mo" - results = [] - - for i, params in enumerate(parameter_sets): - print(f"\n--- Simulation {i+1}/{len(parameter_sets)} ---") - print(f"Parameters: {params}") - - # Set parameters for this run - try: - set_parameters(model_file, params) - except Exception as e: - print(f" Warning: Could not set parameters: {e}") - continue - - # Run simulation - success = run(model_file) - - if success: - # Collect results - log_data = simulation_log(model_name) - result = { - 'run': i+1, - 'parameters': params, - 'simulation_time': log_data.get('Simulation time', 0) if log_data else 0, - 'compilation_time': log_data.get('Compilation time', 0) if log_data else 0 - } - results.append(result) - print(f" Success: Sim time = {result['simulation_time']} ms") - else: - print(f" Failed") - - # Summary - print(f"\n=== Batch Simulation Summary ===") - print(f"Total runs: {len(parameter_sets)}") - print(f"Successful runs: {len(results)}") +def main(): + parser = argparse.ArgumentParser(description="QSS Solver Pipeline Utility") + parser.add_argument( + '-m', '--model', + type=str, + required=True, + help="Path layout pointing to target Modelica (.mo) file context" + ) + parser.add_argument( + '-p', '--partition', + type=str, + required=True, + help="Full or localized naming structure of the partitioned graph layout instance" + ) - if results: - avg_sim_time = sum(r['simulation_time'] for r in results) / len(results) - avg_comp_time = sum(r['compilation_time'] for r in results) / len(results) - print(f"Average simulation time: {avg_sim_time:.2f} ms") - print(f"Average compilation time: {avg_comp_time:.2f} ms") + args = parser.parse_args() - return results - -def main(): - """Main function demonstrating various simulation scenarios.""" print("QSS Solver Python API - Autonomous Simulation Example") print("=" * 60) - # Setup configure_logging() - env_vars = setup_environment() - - - model_name = str(Path.cwd()) + "/airconds_cont/airconds_cont.mo" + setup_environment() try: - execute_simulation(model_name) - + execute_simulation(args.model, args.partition) except Exception as e: - print(f"Error during simulation: {e}") - logging.error(f"Simulation error: {e}", exc_info=True) + print(f"Error during execution pipeline: {e}") + logging.error(f"Simulation error trace: {e}", exc_info=True) print("\n" + "=" * 60) - print("Example completed successfully!") - print(f"Check the log file: simulation_example.log") + print("Pipeline execution cycle completed.") + if __name__ == "__main__": main() diff --git a/test/performance/bm_exec.cpp b/test/performance/bm_exec.cpp index 053dee93..56f8ba47 100644 --- a/test/performance/bm_exec.cpp +++ b/test/performance/bm_exec.cpp @@ -63,31 +63,27 @@ void printHeader(Util::prog_opts::variables_map vm) // Benchmark Executor ---------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// -BMExecutor::BMExecutor() - : _benchmark(0), _set_impl(0), _pw_impl(0), _scc_impl(1) +BMExecutor::BMExecutor() : _benchmark(0), _set_impl(0), _pw_impl(0), _scc_impl(1) { - _config.add_options() - ("benchmark,b", Util::prog_opts::value(&_benchmark), - "Desired benchmark:" - "\n - 0 for set benchmark" - "\n - 1 for PWMap benchmark" - "\n - 2 for matching benchmark" - "\n - 3 for SCC benchmark") - ("set_impl,s", Util::prog_opts::value(&_set_impl), - " Desired set implementation:" - "\n - 0 for unordered sets (default option)" - "\n - 1 for ordered sets" - "\n - 2 for unidimensional ordered dense sets") - ("pw_impl,p", Util::prog_opts::value(&_pw_impl), - " Desired PWMap implementation:" - "\n - 0 for unordered PWMaps (default option)" - "\n - 1 for ordered PWMaps" - "\n - 2 for domain ordered PWMaps") - ("scc_impl", Util::prog_opts::value(&_scc_impl), - "Desired SCC algorithm implementation:" - "\n - 0 for V1 of minimum reachable SCC" - "\n - 1 for V2 of minimum reachable SCC (default option)") - ("boost", "Executes Boost Graph Library algorithms for scalar graphs"); + _config.add_options()("benchmark,b", Util::prog_opts::value(&_benchmark), + "Desired benchmark:" + "\n - 0 for set benchmark" + "\n - 1 for PWMap benchmark" + "\n - 2 for matching benchmark" + "\n - 3 for SCC benchmark")("set_impl,s", Util::prog_opts::value(&_set_impl), + " Desired set implementation:" + "\n - 0 for unordered sets (default option)" + "\n - 1 for ordered sets" + "\n - 2 for unidimensional ordered dense sets")( + "pw_impl,p", Util::prog_opts::value(&_pw_impl), + " Desired PWMap implementation:" + "\n - 0 for unordered PWMaps (default option)" + "\n - 1 for ordered PWMaps" + "\n - 2 for domain ordered PWMaps")("scc_impl", Util::prog_opts::value(&_scc_impl), + "Desired SCC algorithm implementation:" + "\n - 0 for V1 of minimum reachable SCC" + "\n - 1 for V2 of minimum reachable SCC (default option)")( + "boost", "Executes Boost Graph Library algorithms for scalar graphs"); _positional.add("input-file", 1); _cmd_line_opts.add(_generic).add(_config).add(_hidden); @@ -100,17 +96,15 @@ void BMExecutor::execute(int argc, char* argv[]) // Command line options handling --------------------------------------------- Util::prog_opts::variables_map vm; - store(Util::prog_opts::command_line_parser(argc, argv) - .options(_cmd_line_opts).positional(_positional).allow_unregistered().run() - , vm); + store(Util::prog_opts::command_line_parser(argc, argv).options(_cmd_line_opts).positional(_positional).allow_unregistered().run(), vm); notify(vm); // Help handling ------------------------------------------------------------- - + if (vm.count("help")) { std::cout << "Usage: [input-file] [options]\n"; std::cout << "Command line options are prioritized over configuration file" - " options."; + " options."; std::cout << _visible << "\n"; return; } @@ -123,8 +117,8 @@ void BMExecutor::execute(int argc, char* argv[]) } // Optional configuration file handling -------------------------------------- - - if (_config_file) { + + if (_config_file) { std::ifstream config_fs{(*_config_file).c_str()}; if (config_fs) { store(parse_config_file(config_fs, _cfg_file_opts), vm); @@ -143,57 +137,53 @@ void BMExecutor::execute(int argc, char* argv[]) if (_input_file) { registerBoostBenchmarks(*_input_file); } else { - Util::ERROR("BoostExecutor: must provide a SBG program filename to run " - , "Boost benchmark\n"); + Util::ERROR("BoostExecutor: must provide a SBG program filename to run ", "Boost benchmark\n"); } } else { switch (*_benchmark) { - case 0: { - registerSetBenchmarks(); - break; - } + case 0: { + registerSetBenchmarks(); + break; + } - case 1: { - registerPWMapBenchmarks(); - break; - } + case 1: { + registerPWMapBenchmarks(); + break; + } - case 2: { - if (_input_file) { - registerMatchingBenchmarks(*_input_file); - } else { - registerMatchingBenchmarks(); - } - break; + case 2: { + if (_input_file) { + registerMatchingBenchmarks(*_input_file); + } else { + registerMatchingBenchmarks(); } + break; + } - case 3: { - if (_input_file) { - registerSCCBenchmarks(*_input_file); - } else { - registerSCCBenchmarks(); - } - break; + case 3: { + if (_input_file) { + registerSCCBenchmarks(*_input_file); + } else { + registerSCCBenchmarks(); } + break; + } - default: { - break; - } + default: { + break; + } } } - Util::prog_opts::parsed_options parsed - = Util::prog_opts::command_line_parser(argc, argv).options(_cmd_line_opts) - .allow_unregistered().run(); - std::vector to_pass_further - = Util::prog_opts::collect_unrecognized(parsed.options - , Util::prog_opts::exclude_positional); + Util::prog_opts::parsed_options parsed = + Util::prog_opts::command_line_parser(argc, argv).options(_cmd_line_opts).allow_unregistered().run(); + std::vector to_pass_further = Util::prog_opts::collect_unrecognized(parsed.options, Util::prog_opts::exclude_positional); ::benchmark::Initialize(&argc, argv); ::benchmark::RunSpecifiedBenchmarks(); } -} // namespace detail +} // namespace detail -} // namespace perf +} // namespace perf -} // namespace SBG +} // namespace SBG diff --git a/test/performance/boost/boost_bm.cpp b/test/performance/boost/boost_bm.cpp index 0f6cace4..8001e140 100644 --- a/test/performance/boost/boost_bm.cpp +++ b/test/performance/boost/boost_bm.cpp @@ -56,8 +56,7 @@ static void BM_BoostMatching(benchmark::State& state, std::string filename) state.SetComplexityN(N); } -static void BM_BoostSCC(benchmark::State& state - , std::string filename) +static void BM_BoostSCC(benchmark::State& state, std::string filename) { int N = state.range(0); @@ -77,8 +76,7 @@ static void BM_BoostSCC(benchmark::State& state state.SetComplexityN(N); } -static void BM_BoostSCCWithBuilder(benchmark::State& state - , std::string filename) +static void BM_BoostSCCWithBuilder(benchmark::State& state, std::string filename) { int N = state.range(0); @@ -104,33 +102,29 @@ static void BM_BoostSCCWithBuilder(benchmark::State& state void registerBoostBenchmarks(std::string filename) { - benchmark::RegisterBenchmark( - ("BM_BoostMatching/" + filename).c_str(), - [filename](benchmark::State& state) { - BM_BoostMatching(state, filename); - } - )->RangeMultiplier(10)->Range(100, 1e6)->Complexity() - ->Unit(benchmark::kMillisecond); - - benchmark::RegisterBenchmark( - ("BM_BoostSCC/" + filename).c_str(), - [filename](benchmark::State& state) { - BM_BoostSCC(state, filename); - } - )->RangeMultiplier(10)->Range(100, 1e6)->Complexity() - ->Unit(benchmark::kMillisecond); - - benchmark::RegisterBenchmark( - ("BM_BoostSCCWithBuilder/" + filename).c_str(), - [filename](benchmark::State& state) { - BM_BoostSCCWithBuilder(state, filename); - } - )->RangeMultiplier(10)->Range(100, 1e6)->Complexity() - ->Unit(benchmark::kMillisecond); + benchmark::RegisterBenchmark(("BM_BoostMatching/" + filename).c_str(), + [filename](benchmark::State& state) { BM_BoostMatching(state, filename); }) + ->RangeMultiplier(10) + ->Range(100, 1e6) + ->Complexity() + ->Unit(benchmark::kMillisecond); + + benchmark::RegisterBenchmark(("BM_BoostSCC/" + filename).c_str(), [filename](benchmark::State& state) { BM_BoostSCC(state, filename); }) + ->RangeMultiplier(10) + ->Range(100, 1e6) + ->Complexity() + ->Unit(benchmark::kMillisecond); + + benchmark::RegisterBenchmark(("BM_BoostSCCWithBuilder/" + filename).c_str(), + [filename](benchmark::State& state) { BM_BoostSCCWithBuilder(state, filename); }) + ->RangeMultiplier(10) + ->Range(100, 1e6) + ->Complexity() + ->Unit(benchmark::kMillisecond); } -} // namespace detail +} // namespace detail -} // namespace perf +} // namespace perf -} // namespace SBG +} // namespace SBG diff --git a/test/performance/boost/scalar_graph.cpp b/test/performance/boost/scalar_graph.cpp index 0d1bc73b..8d49cf89 100644 --- a/test/performance/boost/scalar_graph.cpp +++ b/test/performance/boost/scalar_graph.cpp @@ -52,18 +52,11 @@ std::ostream& operator<<(std::ostream& out, const Graph& g) BipartiteGraph::BipartiteGraph() : _graph(), _partition() {} -BipartiteGraph::BipartiteGraph(Graph&& graph, std::vector&& partition) - : _graph(graph), _partition(partition) {} +BipartiteGraph::BipartiteGraph(Graph&& graph, std::vector&& partition) : _graph(graph), _partition(partition) {} -const Graph& BipartiteGraph::graph() const -{ - return _graph; -} +const Graph& BipartiteGraph::graph() const { return _graph; } -const std::vector& BipartiteGraph::partition() const -{ - return _partition; -} +const std::vector& BipartiteGraph::partition() const { return _partition; } std::ostream& operator<<(std::ostream& out, const BipartiteGraph& bgraph) { @@ -96,8 +89,8 @@ std::ostream& operator<<(std::ostream& out, const DirectedGraph& dgraph) return out; } -} // namespace detail +} // namespace detail -} // namespace perf +} // namespace perf -} // namespace SBG +} // namespace SBG diff --git a/test/performance/boost/scalar_graph_builder.cpp b/test/performance/boost/scalar_graph_builder.cpp index 4dd0cc1a..ba2738e9 100644 --- a/test/performance/boost/scalar_graph_builder.cpp +++ b/test/performance/boost/scalar_graph_builder.cpp @@ -30,21 +30,20 @@ namespace perf { namespace detail { +using SBG::LIB::BipartiteSBG; using SBG::LIB::Int; using SBG::LIB::IntTuple; +using SBG::LIB::PWMap; using SBG::LIB::Set; -using SBG::LIB::detail::SetAccessKey; -using SBG::LIB::detail::SetAccess; using SBG::LIB::detail::MaybeIntTuple; -using SBG::LIB::PWMap; -using SBG::LIB::BipartiteSBG; +using SBG::LIB::detail::SetAccess; +using SBG::LIB::detail::SetAccessKey; //////////////////////////////////////////////////////////////////////////////// // Scalar undirected graph builder --------------------------------------------- //////////////////////////////////////////////////////////////////////////////// -ScalarGraphBuilder::ScalarGraphBuilder(BipartiteSBG bsbg) - : _bsbg(bsbg), _vertex_map(), _partition() {} +ScalarGraphBuilder::ScalarGraphBuilder(BipartiteSBG bsbg) : _bsbg(bsbg), _vertex_map(), _partition() {} BipartiteGraph ScalarGraphBuilder::build() { @@ -54,8 +53,7 @@ BipartiteGraph ScalarGraphBuilder::build() return BipartiteGraph(std::move(graph), std::move(_partition)); } -BipartiteGraph ScalarGraphBuilder::build(Int number_vertices - , EdgeVector& E, std::vector&& partition) +BipartiteGraph ScalarGraphBuilder::build(Int number_vertices, EdgeVector& E, std::vector&& partition) { Graph graph(E.begin(), E.end(), number_vertices); return BipartiteGraph(std::move(graph), std::move(_partition)); @@ -63,7 +61,6 @@ BipartiteGraph ScalarGraphBuilder::build(Int number_vertices void ScalarGraphBuilder::translateVertices() { - Set V = _bsbg.V(); if (V.isEmpty()) { return; @@ -75,10 +72,9 @@ void ScalarGraphBuilder::translateVertices() Int count = 0; SetAccessKey key = SetAccess::key(); std::vector vertices = key.flatten(V); - for (const IntTuple& v : vertices) { + for (const IntTuple& v : vertices) { _vertex_map[v] = count; - _partition.emplace_back( - SBG::LIB::Set{v}.intersection(X).isEmpty()); + _partition.emplace_back(SBG::LIB::Set{v}.intersection(X).isEmpty()); ++count; } } @@ -107,8 +103,8 @@ EdgeVector ScalarGraphBuilder::getEdgeList() return result; } -} // namespace detail +} // namespace detail -} // namespace perf +} // namespace perf -} // namespace SBG +} // namespace SBG diff --git a/test/performance/boost/scc_graph_builder.cpp b/test/performance/boost/scc_graph_builder.cpp index ebd66bed..61729032 100644 --- a/test/performance/boost/scc_graph_builder.cpp +++ b/test/performance/boost/scc_graph_builder.cpp @@ -45,9 +45,7 @@ std::size_t EdgeHash::operator()(const Edge& e) const // Builder for the algebraic loop detection stage ------------------------------ //////////////////////////////////////////////////////////////////////////////// -SCCGraphBuilder::SCCGraphBuilder(BipartiteGraph&& bgraph - , VertexVector&& matching) - : _bgraph(bgraph), _matching(matching) {} +SCCGraphBuilder::SCCGraphBuilder(BipartiteGraph&& bgraph, VertexVector&& matching) : _bgraph(bgraph), _matching(matching) {} DirectedGraph SCCGraphBuilder::build() { @@ -56,8 +54,7 @@ DirectedGraph SCCGraphBuilder::build() return build(number_vertices, edges); } -DirectedGraph SCCGraphBuilder::build(SBG::LIB::Int number_vertices - , EdgeVector& E) +DirectedGraph SCCGraphBuilder::build(SBG::LIB::Int number_vertices, EdgeVector& E) { return DirectedGraph(E.begin(), E.end(), number_vertices); } @@ -96,10 +93,8 @@ EdgeVector SCCGraphBuilder::getEdgeList() // Detect unmatched edge if (match_u != v) { Vertex match_v = _matching[v]; - Vertex newv = v < match_v ? _vertex_map[Edge(v, match_v)] - : _vertex_map[Edge(match_v, v)]; - Vertex newu = u < match_u ? _vertex_map[Edge(u, match_u)] - : _vertex_map[Edge(match_u, u)]; + Vertex newv = v < match_v ? _vertex_map[Edge(v, match_v)] : _vertex_map[Edge(match_v, v)]; + Vertex newu = u < match_u ? _vertex_map[Edge(u, match_u)] : _vertex_map[Edge(match_u, u)]; Edge e_result(newv, newu); if (!partition[u] && partition[v]) { result.emplace_back(Edge(newu, newv)); @@ -112,8 +107,8 @@ EdgeVector SCCGraphBuilder::getEdgeList() return result; } -} // namespace detail +} // namespace detail -} // namespace perf +} // namespace perf -} // namespace SBG +} // namespace SBG diff --git a/test/performance/matching_bm.cpp b/test/performance/matching_bm.cpp index 237f7bda..2cb67188 100644 --- a/test/performance/matching_bm.cpp +++ b/test/performance/matching_bm.cpp @@ -39,7 +39,7 @@ static void BM_Matching(benchmark::State& state, std::string filename) int N = state.range(0); LIB::Matching match_algorithm; - LIB::BipartiteSBG match_sbg = generateSBG(filename, N, 1); + LIB::BipartiteSBG match_sbg = generateSBG(filename, N, 1); for (auto _ : state) { benchmark::DoNotOptimize(match_algorithm.calculate(match_sbg)); } @@ -53,8 +53,7 @@ static void BM_MatchingCopies(benchmark::State& state, std::string filename) // Calculate Matching LIB::Matching match_algorithm; - LIB::BipartiteSBG match_sbg = generateSBG( - filename, 100, N); + LIB::BipartiteSBG match_sbg = generateSBG(filename, 100, N); for (auto _ : state) { benchmark::DoNotOptimize(match_algorithm.calculate(match_sbg)); @@ -68,21 +67,18 @@ static void BM_MatchingCopies(benchmark::State& state, std::string filename) void registerMatchingBenchmarks(std::string filename) { - benchmark::RegisterBenchmark( - ("BM_Matching/" + filename).c_str(), - [filename](benchmark::State& state) { - BM_Matching(state, filename); - } - )->RangeMultiplier(10)->Range(100, 1e6)->Complexity() - ->Unit(benchmark::kMillisecond); - - benchmark::RegisterBenchmark( - ("BM_MatchingCopies/" + filename).c_str(), - [filename](benchmark::State& state) { - BM_MatchingCopies(state, filename); - } - )->RangeMultiplier(2)->Range(1, 128)->Complexity() - ->Unit(benchmark::kMillisecond); + benchmark::RegisterBenchmark(("BM_Matching/" + filename).c_str(), [filename](benchmark::State& state) { BM_Matching(state, filename); }) + ->RangeMultiplier(10) + ->Range(100, 1e6) + ->Complexity() + ->Unit(benchmark::kMillisecond); + + benchmark::RegisterBenchmark(("BM_MatchingCopies/" + filename).c_str(), + [filename](benchmark::State& state) { BM_MatchingCopies(state, filename); }) + ->RangeMultiplier(2) + ->Range(1, 128) + ->Complexity() + ->Unit(benchmark::kMillisecond); } void registerMatchingBenchmarks() @@ -92,8 +88,8 @@ void registerMatchingBenchmarks() registerMatchingBenchmarks("../../matching_TestRL3.test"); } -} // namespace detail +} // namespace detail -} // namespace perf +} // namespace perf -} // namespace SBG +} // namespace SBG diff --git a/test/performance/pwmap_bm.cpp b/test/performance/pwmap_bm.cpp index 0bb3cf44..86c32bb1 100644 --- a/test/performance/pwmap_bm.cpp +++ b/test/performance/pwmap_bm.cpp @@ -214,47 +214,39 @@ static void BM_PWMapCompact(benchmark::State& state) void registerPWMapBenchmarks() { - BENCHMARK(BM_PWMapDenseDom)->RangeMultiplier(10)->Range(10, 1e3) - ->Complexity(); - + BENCHMARK(BM_PWMapDenseDom)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); + BENCHMARK(BM_PWMapEq)->RangeMultiplier(10)->Range(10, 1e2)->Complexity(); - + BENCHMARK(BM_PWMapSum)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); - + BENCHMARK(BM_PWMapDom)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); - - BENCHMARK(BM_PWMapRestrict)->RangeMultiplier(10)->Range(10, 1e3) - ->Complexity(); - + + BENCHMARK(BM_PWMapRestrict)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); + BENCHMARK(BM_PWMapImage)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); - - BENCHMARK(BM_PWMapPreImage)->RangeMultiplier(10)->Range(10, 1e3) - ->Complexity(); - - BENCHMARK(BM_PWMapComposition)->RangeMultiplier(10)->Range(10, 1e3) - ->Complexity(); - - BENCHMARK(BM_PWMapConcat)->RangeMultiplier(10)->Range(10, 1e3) - ->Complexity(); - + + BENCHMARK(BM_PWMapPreImage)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); + + BENCHMARK(BM_PWMapComposition)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); + + BENCHMARK(BM_PWMapConcat)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); + BENCHMARK(BM_PWMapCombine)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); BENCHMARK(BM_PWMapReduce)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); - + BENCHMARK(BM_PWMapMinAdj)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); - - BENCHMARK(BM_PWMapEqImage)->RangeMultiplier(10)->Range(10, 1e3) - ->Complexity(); - - BENCHMARK(BM_PWMapLessImage)->RangeMultiplier(10)->Range(10, 1e3) - ->Complexity(); - - BENCHMARK(BM_PWMapCompact)->RangeMultiplier(10)->Range(10, 1e2) - ->Complexity(); + + BENCHMARK(BM_PWMapEqImage)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); + + BENCHMARK(BM_PWMapLessImage)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); + + BENCHMARK(BM_PWMapCompact)->RangeMultiplier(10)->Range(10, 1e2)->Complexity(); } -} // namespace detail +} // namespace detail -} // namespace perf +} // namespace perf -} // namespace SBG +} // namespace SBG diff --git a/test/performance/scc_bm.cpp b/test/performance/scc_bm.cpp index fa3470d2..d2a1b149 100644 --- a/test/performance/scc_bm.cpp +++ b/test/performance/scc_bm.cpp @@ -50,8 +50,7 @@ static void BM_SCC(benchmark::State& state, std::string filename) static void BM_SCCCopies(benchmark::State& state, std::string filename) { int N = state.range(0); - LIB::DirectedSBG scc_dsbg = generateSBG( - filename, 100, N); + LIB::DirectedSBG scc_dsbg = generateSBG(filename, 100, N); LIB::SCC scc_algorithm; for (auto _ : state) { @@ -66,29 +65,23 @@ static void BM_SCCCopies(benchmark::State& state, std::string filename) void registerSCCBenchmarks(std::string filename) { - benchmark::RegisterBenchmark( - ("BM_SCC/" + filename).c_str(), - [filename](benchmark::State& state) { - BM_SCC(state, filename); - } - )->RangeMultiplier(10)->Range(100, 1e6)->Complexity() - ->Unit(benchmark::kMillisecond); - - benchmark::RegisterBenchmark( - ("BM_SCCCopies/" + filename).c_str(), - [filename](benchmark::State& state) { - BM_SCCCopies(state, filename); - } - )->RangeMultiplier(2)->Range(1, 128)->Complexity() - ->Unit(benchmark::kMillisecond); + benchmark::RegisterBenchmark(("BM_SCC/" + filename).c_str(), [filename](benchmark::State& state) { BM_SCC(state, filename); }) + ->RangeMultiplier(10) + ->Range(100, 1e6) + ->Complexity() + ->Unit(benchmark::kMillisecond); + + benchmark::RegisterBenchmark(("BM_SCCCopies/" + filename).c_str(), [filename](benchmark::State& state) { BM_SCCCopies(state, filename); }) + ->RangeMultiplier(2) + ->Range(1, 128) + ->Complexity() + ->Unit(benchmark::kMillisecond); } -void registerSCCBenchmarks() -{ -} +void registerSCCBenchmarks() {} -} // namespace detail +} // namespace detail -} // namespace perf +} // namespace perf -} // namespace SBG +} // namespace SBG diff --git a/test/performance/set_bm.cpp b/test/performance/set_bm.cpp index dca967b9..b7594cc3 100644 --- a/test/performance/set_bm.cpp +++ b/test/performance/set_bm.cpp @@ -33,8 +33,8 @@ namespace perf { namespace detail { -using SBG::LIB::detail::Interval; using SBG::LIB::Set; +using SBG::LIB::detail::Interval; //////////////////////////////////////////////////////////////////////////////// // Benchmarks ------------------------------------------------------------------ @@ -125,25 +125,22 @@ static void BM_SetCompact(benchmark::State& state) void registerSetBenchmarks() { BENCHMARK(BM_SetEq)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); - + BENCHMARK(BM_SetCap)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); - + BENCHMARK(BM_SetCup)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); - - BENCHMARK(BM_SetComplement)->RangeMultiplier(10)->Range(10, 1e3) - ->Complexity(); - + + BENCHMARK(BM_SetComplement)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); + BENCHMARK(BM_SetDiff)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); - - BENCHMARK(BM_SetDisjointUnion)->RangeMultiplier(10)->Range(10, 1e3) - ->Complexity(); - - BENCHMARK(BM_SetCompact)->RangeMultiplier(10)->Range(10, 1e2) - ->Complexity(); + + BENCHMARK(BM_SetDisjointUnion)->RangeMultiplier(10)->Range(10, 1e3)->Complexity(); + + BENCHMARK(BM_SetCompact)->RangeMultiplier(10)->Range(10, 1e2)->Complexity(); } -} // namespace detail +} // namespace detail -} // namespace perf +} // namespace perf -} // namespace SBG +} // namespace SBG diff --git a/test/performance/utils.cpp b/test/performance/utils.cpp index 7ae85200..f08db2c5 100644 --- a/test/performance/utils.cpp +++ b/test/performance/utils.cpp @@ -44,16 +44,16 @@ namespace perf { namespace detail { -using SBG::LIB::Int; -using SBG::LIB::Rational; -using SBG::LIB::detail::Interval; -using SBG::LIB::Set; -using SBG::LIB::Expression; -using SBG::LIB::PWMap; using SBG::LIB::BipartiteSBG; using SBG::LIB::DirectedSBG; -using SBG::LIB::Matching; +using SBG::LIB::Expression; +using SBG::LIB::Int; using SBG::LIB::MatchData; +using SBG::LIB::Matching; +using SBG::LIB::PWMap; +using SBG::LIB::Rational; +using SBG::LIB::Set; +using SBG::LIB::detail::Interval; //////////////////////////////////////////////////////////////////////////////// // File Manipulation ----------------------------------------------------------- @@ -93,7 +93,7 @@ bool updateN(const std::string& filename, int N) return true; } -template +template SBGT generateSBG(std::string filename, int N, int copies) { std::streambuf* original_buf = std::cout.rdbuf(); @@ -105,7 +105,7 @@ SBGT generateSBG(std::string filename, int N, int copies) // Get graph from file SBGT g; - SBG::Eval::ProgramIO eval_result = SBG::Eval::parseEvalFile(filename); + SBG::Eval::ProgramIO eval_result = SBG::Eval::parseEvalFile(filename); for (const SBG::Eval::ExprResult& ev : eval_result.exprs()) { SBG::Eval::ExprBaseType e = std::get<1>(ev); if (std::holds_alternative(e)) { @@ -118,10 +118,8 @@ SBGT generateSBG(std::string filename, int N, int copies) return copy(copies, g); } -template BipartiteSBG generateSBG( - std::string filename, int N, int copies); -template DirectedSBG generateSBG( - std::string filename, int N, int copies); +template BipartiteSBG generateSBG(std::string filename, int N, int copies); +template DirectedSBG generateSBG(std::string filename, int N, int copies); MatchData calculateMatching(std::string filename, int N, int copies) { @@ -130,7 +128,7 @@ MatchData calculateMatching(std::string filename, int N, int copies) Matching match_algorithm; MatchData match_result = match_algorithm.calculate(match_sbg); - return match_result; + return match_result; } //////////////////////////////////////////////////////////////////////////////// @@ -144,12 +142,12 @@ std::pair nonDisjointPieces(Int set_sz) Set s1; Set s2; for (unsigned int h = 0; h < set_sz; ++h) { - Interval i1{h*inter_sz, 1, (h + 1)*inter_sz - 1}; + Interval i1{h * inter_sz, 1, (h + 1) * inter_sz - 1}; Set jth_s1{i1.begin(), i1.step(), i1.end()}; s1 = s1.disjointCup(jth_s1); - Int off = inter_sz/2; - Interval i2{off + (h*inter_sz), 1, off + (h + 1)*inter_sz - 1}; + Int off = inter_sz / 2; + Interval i2{off + (h * inter_sz), 1, off + (h + 1) * inter_sz - 1}; Set jth_s2{i2.begin(), i2.step(), i2.end()}; s2 = s2.disjointCup(jth_s2); } @@ -168,9 +166,9 @@ std::pair interlacedPieces(Int set_sz) Set s1; Set s2; for (SBG::LIB::Int j = 0; j < set_sz; j += 2) { - Set jth_s1{j*inter_sz, 1, (j + 1)*inter_sz - 1}; + Set jth_s1{j * inter_sz, 1, (j + 1) * inter_sz - 1}; s1 = std::move(s1.disjointCup(jth_s1)); - Set jth_s2{(j + 1)*inter_sz, 1, (j + 2)*inter_sz - 1}; + Set jth_s2{(j + 1) * inter_sz, 1, (j + 2) * inter_sz - 1}; s2 = std::move(s2.disjointCup(jth_s2)); } @@ -189,9 +187,9 @@ PWMap denseDom(Int map_sz) PWMap pw; for (unsigned int j = 0; j < map_sz; ++j) { Set domain; - Int offset = j*set_sz*inter_sz; + Int offset = j * set_sz * inter_sz; for (unsigned int h = 0; h < set_sz; ++h) { - Interval i{offset + (h*inter_sz), 1, offset + (h + 1)*inter_sz - 1}; + Interval i{offset + (h * inter_sz), 1, offset + (h + 1) * inter_sz - 1}; Set jth_domain{i.begin(), i.step(), i.end()}; domain = std::move(domain).disjointCup(std::move(jth_domain)); } @@ -214,14 +212,14 @@ std::pair minAdjMaps(Int map_sz) for (unsigned int j = 0; j < map_sz; ++j) { Set domain1; Set domain2; - Int offset = j*set_sz*inter_sz; + Int offset = j * set_sz * inter_sz; for (unsigned int h = 0; h < set_sz; ++h) { - Interval i1{offset + (h*inter_sz), 1, offset + (h + 1)*inter_sz - 1}; + Interval i1{offset + (h * inter_sz), 1, offset + (h + 1) * inter_sz - 1}; Set jth_domain1{i1.begin(), i1.step(), i1.end()}; domain1 = std::move(domain1).disjointCup(std::move(jth_domain1)); - Int offset2 = offset + inter_sz/2; - Interval i2{offset2 + (h*inter_sz), 1, offset2 + (h + 1)*inter_sz - 1}; + Int offset2 = offset + inter_sz / 2; + Interval i2{offset2 + (h * inter_sz), 1, offset2 + (h + 1) * inter_sz - 1}; Set jth_domain2{i2.begin(), i2.step(), i2.end()}; domain2 = std::move(domain2).disjointCup(std::move(jth_domain2)); } @@ -247,14 +245,14 @@ std::pair interlacedMaps(Int map_sz) for (unsigned int j = 0; j < map_sz; ++j) { Set domain1; Set domain2; - Int offset = j*set_sz*inter_sz; + Int offset = j * set_sz * inter_sz; for (unsigned int h = 0; h < set_sz; h += 2) { - Interval i1{offset + (h*inter_sz), 1, offset + (h + 1)*inter_sz - 1}; + Interval i1{offset + (h * inter_sz), 1, offset + (h + 1) * inter_sz - 1}; Set jth_domain1{i1.begin(), i1.step(), i1.end()}; domain1 = std::move(domain1).disjointCup(std::move(jth_domain1)); Int offset2 = offset + inter_sz; - Interval i2{offset2 + (h*inter_sz), 1, offset2 + (h + 1)*inter_sz - 1}; + Interval i2{offset2 + (h * inter_sz), 1, offset2 + (h + 1) * inter_sz - 1}; Set jth_domain2{i2.begin(), i2.step(), i2.end()}; domain2 = std::move(domain2).disjointCup(std::move(jth_domain2)); } @@ -281,14 +279,14 @@ std::pair nonDisjointMaps(Int map_sz) for (unsigned int j = 0; j < map_sz; ++j) { Set domain1; Set domain2; - Int offset = j*set_sz*inter_sz; + Int offset = j * set_sz * inter_sz; for (unsigned int h = 0; h < set_sz; ++h) { - Interval i1{offset + (h*inter_sz), 1, offset + (h + 1)*inter_sz - 1}; + Interval i1{offset + (h * inter_sz), 1, offset + (h + 1) * inter_sz - 1}; Set jth_domain1{i1.begin(), i1.step(), i1.end()}; domain1 = std::move(domain1).disjointCup(std::move(jth_domain1)); - Int offset2 = offset + inter_sz/2; - Interval i2{offset2 + (h*inter_sz), 1, offset2 + (h + 1)*inter_sz - 1}; + Int offset2 = offset + inter_sz / 2; + Interval i2{offset2 + (h * inter_sz), 1, offset2 + (h + 1) * inter_sz - 1}; Set jth_domain2{i2.begin(), i2.step(), i2.end()}; domain2 = std::move(domain2).disjointCup(std::move(jth_domain2)); } @@ -313,9 +311,9 @@ PWMap reducibleMaps(Int map_sz) PWMap pw; for (unsigned int j = 0; j < map_sz; ++j) { Set domain; - Int offset = j*set_sz*inter_sz; + Int offset = j * set_sz * inter_sz; for (unsigned int h = 0; h < set_sz; ++h) { - Interval i{offset + (h*inter_sz), 1, offset + (h + 1)*inter_sz - 1}; + Interval i{offset + (h * inter_sz), 1, offset + (h + 1) * inter_sz - 1}; Set jth_domain{i.begin(), i.step(), i.end()}; domain = std::move(domain).disjointCup(std::move(jth_domain)); } @@ -331,10 +329,8 @@ PWMap reducibleMaps(Int map_sz) return pw; } +} // namespace detail +} // namespace perf -} // namespace detail - -} // namespace perf - -} // namespace SBG +} // namespace SBG diff --git a/util/logger.cpp b/util/logger.cpp old mode 100755 new mode 100644 index e831e4fc..f922dc8a --- a/util/logger.cpp +++ b/util/logger.cpp @@ -35,11 +35,8 @@ SBGLogger& SBGLogger::instance() void SBGLogger::setLevel(LogLevel lvl) { _level = lvl; } -std::ostream& SBGLogger::log(LogLevel msg_lvl) -{ - return (msg_lvl <= _level) ? _file : null_stream; -} +std::ostream& SBGLogger::log(LogLevel msg_lvl) { return (msg_lvl <= _level) ? _file : null_stream; } -} // namespace Util +} // namespace Util -} // namespace SBG +} // namespace SBG diff --git a/util/time_profiler.cpp b/util/time_profiler.cpp index 98a1cbe8..5d3cb2ee 100644 --- a/util/time_profiler.cpp +++ b/util/time_profiler.cpp @@ -23,24 +23,19 @@ namespace SBG { namespace Util { -void time_profiler_results() -{ - Internal::TimeProfiler::print_execution_time(); -} +void time_profiler_results() { Internal::TimeProfiler::print_execution_time(); } namespace Internal { std::unordered_map TimeProfiler::_execution_time = {}; -TimeProfiler::TimeProfiler(std::string&& function_name) - : _function_name(move(function_name)) +TimeProfiler::TimeProfiler(std::string&& function_name) : _function_name(move(function_name)) { if (time_profiler_enabled) { _start = std::chrono::high_resolution_clock::now(); } } - TimeProfiler::~TimeProfiler() { if (time_profiler_enabled) { @@ -53,8 +48,8 @@ TimeProfiler::~TimeProfiler() } } - -void TimeProfiler::print_execution_time() { +void TimeProfiler::print_execution_time() +{ if (time_profiler_enabled) { for (auto&& pair : TimeProfiler::_execution_time) { std::cout << pair.first << ": " << pair.second << " ms" << std::endl; @@ -62,7 +57,8 @@ void TimeProfiler::print_execution_time() { } } -void TimeProfiler::print_execution_time(std::string function_name) { +void TimeProfiler::print_execution_time(std::string function_name) +{ if (time_profiler_enabled) { for (auto&& pair : TimeProfiler::_execution_time) { if (pair.first == function_name) { @@ -72,8 +68,8 @@ void TimeProfiler::print_execution_time(std::string function_name) { } } -} // namespace Internal +} // namespace Internal -} // namespace Util +} // namespace Util -} // namespace SBG +} // namespace SBG diff --git a/util/user_input_handler.cpp b/util/user_input_handler.cpp index 244fb2b0..b7d4e3ae 100644 --- a/util/user_input_handler.cpp +++ b/util/user_input_handler.cpp @@ -34,10 +34,8 @@ namespace Util { void version() { std::cout << "SBG library v4.0.0" << std::endl; - std::cout << "License GPLv3+: GNU GPL version 3 or later" - << " \n"; - std::cout << "This is free software: you are free to change and redistribute" - << "it.\n"; + std::cout << "License GPLv3+: GNU GPL version 3 or later" << " \n"; + std::cout << "This is free software: you are free to change and redistribute" << "it.\n"; std::cout << "There is NO WARRANTY, to the extent permitted by law.\n"; } @@ -45,22 +43,20 @@ void version() // User Input Handler ---------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// -UserInputHandler::UserInputHandler() : _generic("Generic options") - , _config("Configuration options"), _hidden("Hidden options") - , _cmd_line_opts("Command line options") - , _cfg_file_opts("Configuration file options"), _positional() +UserInputHandler::UserInputHandler() + : _generic("Generic options"), + _config("Configuration options"), + _hidden("Hidden options"), + _cmd_line_opts("Command line options"), + _cfg_file_opts("Configuration file options"), + _positional() { - _generic.add_options() - ("help,h", "Prints all available options") - ("version,v", "Displays version information") - ("debug,d", "Activates debug messages") - ("config,c", prog_opts::value(&_config_file), "Configuration filename"); - - _hidden.add_options() - ("input-file", prog_opts::value(&_input_file) - , "Input SBG program"); + _generic.add_options()("help,h", "Prints all available options")("version,v", "Displays version information")( + "debug,d", "Activates debug messages")("config,c", prog_opts::value(&_config_file), "Configuration filename"); + + _hidden.add_options()("input-file", prog_opts::value(&_input_file), "Input SBG program"); } -} // namespace Util +} // namespace Util -} // namespace SBG +} // namespace SBG