From 065236be228eba302c8f4e5ce7f3758f9e720fc0 Mon Sep 17 00:00:00 2001 From: Anton Reinhard Date: Tue, 29 Aug 2023 12:57:46 +0200 Subject: [PATCH] Add documentation to every function and automatic doc html building (#6) Reviewed-on: https://code.woubery.com/Rubydragon/MetagraphOptimization.jl/pulls/6 Co-authored-by: Anton Reinhard Co-committed-by: Anton Reinhard --- .gitattributes | 4 +- .gitea/workflows/julia-package-ci.yml | 159 ++++++++++++++++++++++++-- Project.toml | 1 - docs/Project.toml | 4 + docs/make.jl | 29 +++++ docs/src/contribution.md | 3 + docs/src/index.md | 26 +++++ docs/src/lib/internals/diff.md | 22 ++++ docs/src/lib/internals/graph.md | 50 ++++++++ docs/src/lib/internals/models.md | 28 +++++ docs/src/lib/internals/node.md | 43 +++++++ docs/src/lib/internals/operation.md | 57 +++++++++ docs/src/lib/internals/task.md | 36 ++++++ docs/src/lib/internals/utility.md | 17 +++ docs/src/lib/public.md | 24 ++++ docs/src/manual.md | 3 + examples/Project.toml | 4 - src/MetagraphOptimization.jl | 78 ++++++++----- src/diff/print.jl | 5 + src/diff/properties.jl | 7 +- src/diff/type.jl | 5 + src/graph/compare.jl | 25 +++- src/graph/interface.jl | 27 ++++- src/graph/mute.jl | 92 ++++++++++++++- src/graph/print.jl | 12 +- src/graph/properties.jl | 10 ++ src/graph/type.jl | 43 +++++-- src/graph/validate.jl | 11 +- src/models/abc/parse.jl | 26 ++++- src/models/abc/properties.jl | 85 +++++++++++++- src/models/abc/types.jl | 46 +++++++- src/node/compare.jl | 20 ++++ src/node/create.jl | 30 +++++ src/node/print.jl | 10 ++ src/node/properties.jl | 67 +++++++++-- src/node/type.jl | 45 +++++++- src/node/validate.jl | 25 +++- src/operation/apply.jl | 78 ++++++++++++- src/operation/clean.jl | 37 +++++- src/operation/find.jl | 40 ++++++- src/operation/get.jl | 7 +- src/operation/print.jl | 20 ++++ src/operation/type.jl | 91 ++++++++++++++- src/operation/utility.jl | 60 +++++++++- src/operation/validate.jl | 42 +++++++ src/task/compare.jl | 15 +++ src/task/create.jl | 14 +++ src/task/print.jl | 5 + src/task/properties.jl | 54 ++++++++- src/task/type.jl | 29 ++++- src/trie.jl | 49 +++++++- src/utility.jl | 40 ++++++- test/unit_tests_tasks.jl | 9 -- 53 files changed, 1628 insertions(+), 141 deletions(-) create mode 100644 docs/Project.toml create mode 100644 docs/make.jl create mode 100644 docs/src/contribution.md create mode 100644 docs/src/index.md create mode 100644 docs/src/lib/internals/diff.md create mode 100644 docs/src/lib/internals/graph.md create mode 100644 docs/src/lib/internals/models.md create mode 100644 docs/src/lib/internals/node.md create mode 100644 docs/src/lib/internals/operation.md create mode 100644 docs/src/lib/internals/task.md create mode 100644 docs/src/lib/internals/utility.md create mode 100644 docs/src/lib/public.md create mode 100644 docs/src/manual.md create mode 100644 src/task/create.jl diff --git a/.gitattributes b/.gitattributes index 074c188..8aff49f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,2 @@ -examples/AB->ABBBBBBB.txt filter=lfs diff=lfs merge=lfs -text -examples/AB->ABBBBBBBBB.txt filter=lfs diff=lfs merge=lfs -text +input/AB->ABBBBBBBBB.txt filter=lfs diff=lfs merge=lfs -text +input/AB->ABBBBBBB.txt filter=lfs diff=lfs merge=lfs -text diff --git a/.gitea/workflows/julia-package-ci.yml b/.gitea/workflows/julia-package-ci.yml index b81d758..bcf8481 100644 --- a/.gitea/workflows/julia-package-ci.yml +++ b/.gitea/workflows/julia-package-ci.yml @@ -1,30 +1,110 @@ -name: Test +name: MetagraphOptimization_CI on: [push] +env: + # keep the depot directly in the repository for the cache + JULIA_DEPOT_PATH: './.julia' + jobs: - test: + prepare: runs-on: arch-latest steps: - #- name: Get git-lfs - # run: apt-get update && apt-get install git-lfs - - name: Checkout repository uses: actions/checkout@v3 with: fetch-depth: 0 - #- name: Checkout LFS objects - # run: git lfs checkout - - name: Setup Julia environment uses: https://github.com/julia-actions/setup-julia@v1.9.2 with: version: '1.9.2' + # needed for the file hashing, should be removed when ${{ hashFiles('**/Project.toml') }} is supported in gitea + - name: Setup go environment + uses: actions/setup-go@v3 + with: + go-version: '1.20' + + - name: Hash files + uses: https://gitea.com/actions/go-hashfiles@v0.0.1 + id: get-hash + with: + patterns: |- + **/Project.toml + + - name: Restore Cache + uses: actions/cache/restore@v3 + id: cache-restore + with: + path: | + .julia/artifacts + .julia/packages + .julia/registries + key: julia-${{ steps.get-hash.outputs.hash }} + + - name: Check cache hit + if: steps.cache-restore.outputs.cache-hit == 'true' + run: exit 0 + - name: Install dependencies - run: julia --project=./ -e 'import Pkg; Pkg.instantiate()' + run: | + julia --project=./ -e 'import Pkg; Pkg.instantiate(); Pkg.precompile()' + julia --project=examples/ -e 'import Pkg; Pkg.develop(Pkg.PackageSpec(path=pwd())); Pkg.instantiate(); Pkg.precompile()' + julia --project=docs/ -e 'import Pkg; Pkg.develop(Pkg.PackageSpec(path=pwd())); Pkg.instantiate(); Pkg.precompile()' + + - name: Cache Julia packages + uses: actions/cache/save@v3 + with: + path: | + .julia/artifacts + .julia/packages + .julia/registries + key: julia-${{ steps.get-hash.outputs.hash }} + + test: + needs: prepare + runs-on: arch-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Julia environment + uses: https://github.com/julia-actions/setup-julia@v1.9.2 + with: + version: '1.9.2' + + # needed for the file hashing, should be removed when ${{ hashFiles('**/Project.toml') }} is supported in gitea + - name: Setup go environment + uses: actions/setup-go@v3 + with: + go-version: '1.20' + + - name: Hash files + uses: https://gitea.com/actions/go-hashfiles@v0.0.1 + id: get-hash + with: + patterns: |- + **/Project.toml + + - name: Restore cached Julia packages + uses: actions/cache/restore@v3 + with: + path: | + .julia/artifacts + .julia/packages + .julia/registries + key: julia-${{ steps.get-hash.outputs.hash }} + + - name: Install dependencies + run: | + julia --project=./ -e 'import Pkg; Pkg.instantiate(); Pkg.precompile()' + julia --project=examples/ -e 'import Pkg; Pkg.develop(Pkg.PackageSpec(path=pwd())); Pkg.instantiate(); Pkg.precompile()' + julia --project=docs/ -e 'import Pkg; Pkg.develop(Pkg.PackageSpec(path=pwd())); Pkg.instantiate(); Pkg.precompile()' - name: Format check run: | @@ -43,4 +123,63 @@ jobs: run: julia --project=./ -t 4 -e 'import Pkg; Pkg.test()' -O0 - name: Run examples - run: julia --project=examples/ -t 4 -e 'import Pkg; Pkg.develop(Pkg.PackageSpec(path=pwd())); Pkg.instantiate(); include("examples/import_bench.jl")' -O3 + run: julia --project=examples/ -t 4 -e 'include("examples/import_bench.jl")' -O3 + + docs: + needs: prepare + runs-on: arch-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Julia environment + uses: https://github.com/julia-actions/setup-julia@v1.9.2 + with: + version: '1.9.2' + + # needed for the file hashing, should be removed when ${{ hashFiles('**/Project.toml') }} is supported in gitea + - name: Setup go environment + uses: actions/setup-go@v3 + with: + go-version: '1.20' + + - name: Hash files + uses: https://gitea.com/actions/go-hashfiles@v0.0.1 + id: get-hash + with: + patterns: |- + **/Project.toml + + - name: Restore cached Julia packages + uses: actions/cache/restore@v3 + with: + path: | + .julia/artifacts + .julia/packages + .julia/registries + key: julia-${{ steps.get-hash.outputs.hash }} + + - name: Install dependencies + run: | + julia --project=./ -e 'import Pkg; Pkg.instantiate(); Pkg.precompile()' + julia --project=examples/ -e 'import Pkg; Pkg.develop(Pkg.PackageSpec(path=pwd())); Pkg.instantiate(); Pkg.precompile()' + julia --project=docs/ -e 'import Pkg; Pkg.develop(Pkg.PackageSpec(path=pwd())); Pkg.instantiate(); Pkg.precompile()' + + - name: Build docs + run: julia --project=docs/ docs/make.jl + + - name: Upload artifacts + uses: actions/upload-artifact@v3 + with: + name: web-doc + path: docs/build/ + + #- name: Webhook Trigger + # uses: https://github.com/zzzze/webhook-trigger@master + # continue-on-error: true + # with: + # data: "{\"event\":\"action_completed\", \"download_url\":\"deckardcain.local:8099/something\"}" + # webhook_url: ${{ secrets.WEBHOOK_URL }} diff --git a/Project.toml b/Project.toml index 238caf5..5e8350a 100644 --- a/Project.toml +++ b/Project.toml @@ -6,7 +6,6 @@ version = "0.1.0" [deps] DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" -Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" diff --git a/docs/Project.toml b/docs/Project.toml new file mode 100644 index 0000000..340c2ff --- /dev/null +++ b/docs/Project.toml @@ -0,0 +1,4 @@ +[deps] +Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" +DocumenterTools = "35a29f4d-8980-5a13-9543-d66fff28ecb8" +MetagraphOptimization = "3e869610-d48d-4942-ba70-c1b702a33ca4" diff --git a/docs/make.jl b/docs/make.jl new file mode 100644 index 0000000..142681d --- /dev/null +++ b/docs/make.jl @@ -0,0 +1,29 @@ +using Documenter +using MetagraphOptimization + +makedocs( + #format = Documenter.LaTeX(platform=""), + + root = "docs", + source = "src", + build = "build", + clean = true, + doctest = true, + modules = Module[MetagraphOptimization], + repo = "https://code.woubery.com/Rubydragon/MetagraphOptimization.jl/src/branch/{commit}{path}#L{line}", + sitename = "MetagraphOptimization.jl", + pages = [ + "index.md", + "Manual" => "manual.md", + "Library" => [ + "Graph" => "lib/internals/graph.md", + "Node" => "lib/internals/node.md", + "Task" => "lib/internals/task.md", + "Operation" => "lib/internals/operation.md", + "Models" => "lib/internals/models.md", + "Diff" => "lib/internals/diff.md", + "Utility" => "lib/internals/utility.md", + ], + "Contribution" => "contribution.md", + ], +) diff --git a/docs/src/contribution.md b/docs/src/contribution.md new file mode 100644 index 0000000..474e324 --- /dev/null +++ b/docs/src/contribution.md @@ -0,0 +1,3 @@ +# Contribution + +This is currently in development for a diploma thesis and is therefore private and impossible to contribute to. diff --git a/docs/src/index.md b/docs/src/index.md new file mode 100644 index 0000000..544a2e5 --- /dev/null +++ b/docs/src/index.md @@ -0,0 +1,26 @@ +# MetagraphOptimization.jl + +*A domain-specific DAG-optimizer* + +## Package Features +- Read a DAG from a file +- Analyze its properties +- Mute the graph using the operations NodeFusion, NodeReduction and NodeSplit + +## Coming Soon: +- Add Code Generation from finished DAG +- Add optimization algorithms and strategies + +## Library Outline + +```@contents +Pages = [ + "lib/public.md", + "lib/internals.md" +] +``` + +### [Index](@id main-index) +```@index +Pages = ["lib/public.md"] +``` diff --git a/docs/src/lib/internals/diff.md b/docs/src/lib/internals/diff.md new file mode 100644 index 0000000..aedf082 --- /dev/null +++ b/docs/src/lib/internals/diff.md @@ -0,0 +1,22 @@ +# Diff + +## Type +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["diff/type.jl"] +Order = [:type] +``` + +## Properties +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["diff/properties.jl"] +Order = [:function] +``` + +## Printing +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["diff/print.jl"] +Order = [:function] +``` diff --git a/docs/src/lib/internals/graph.md b/docs/src/lib/internals/graph.md new file mode 100644 index 0000000..f3c1807 --- /dev/null +++ b/docs/src/lib/internals/graph.md @@ -0,0 +1,50 @@ +# Graph + +## Type +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["graph/type.jl"] +Order = [:type] +``` + +## Interface +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["graph/interface.jl"] +Order = [:function] +``` + +## Compare +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["graph/compare.jl"] +Order = [:function] +``` + +## Mute +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["graph/mute.jl"] +Order = [:function] +``` + +## Print +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["graph/print.jl"] +Order = [:function] +``` + +## Properties +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["graph/properties.jl"] +Order = [:function] +``` + +## Validate +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["graph/validate.jl"] +Order = [:function] +``` diff --git a/docs/src/lib/internals/models.md b/docs/src/lib/internals/models.md new file mode 100644 index 0000000..d28254d --- /dev/null +++ b/docs/src/lib/internals/models.md @@ -0,0 +1,28 @@ +# Models + +## ABC-Model + +### Types +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["models/abc/types.jl"] +Order = [:type, :constant] +``` + +### Parse +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["models/abc/parse.jl"] +Order = [:function] +``` + +### Properties +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["models/abc/properties.jl"] +Order = [:function] +``` + +## QED-Model + +*To be added* diff --git a/docs/src/lib/internals/node.md b/docs/src/lib/internals/node.md new file mode 100644 index 0000000..1ffcde7 --- /dev/null +++ b/docs/src/lib/internals/node.md @@ -0,0 +1,43 @@ +# Node + +## Type +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["node/type.jl"] +Order = [:type] +``` + +## Create +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["node/create.jl"] +Order = [:function] +``` + +## Compare +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["node/compare.jl"] +Order = [:function] +``` + +## Properties +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["node/properties.jl"] +Order = [:function] +``` + +## Print +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["node/print.jl"] +Order = [:function] +``` + +## Validate +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["node/validate.jl"] +Order = [:function] +``` diff --git a/docs/src/lib/internals/operation.md b/docs/src/lib/internals/operation.md new file mode 100644 index 0000000..9e56bb2 --- /dev/null +++ b/docs/src/lib/internals/operation.md @@ -0,0 +1,57 @@ +# Operation + +## Types +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["operation/type.jl"] +Order = [:type] +``` + +## Find +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["operation/find.jl"] +Order = [:function] +``` + +## Apply +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["operation/apply.jl"] +Order = [:function] +``` + +## Get +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["operation/get.jl"] +Order = [:function] +``` + +## Clean +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["operation/clean.jl"] +Order = [:function] +``` + +## Utility +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["operation/utility.jl"] +Order = [:function] +``` + +## Print +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["operation/print.jl"] +Order = [:function] +``` + +## Validate +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["operation/validate.jl"] +Order = [:function] +``` diff --git a/docs/src/lib/internals/task.md b/docs/src/lib/internals/task.md new file mode 100644 index 0000000..e7debbb --- /dev/null +++ b/docs/src/lib/internals/task.md @@ -0,0 +1,36 @@ +# Task + +## Type +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["task/type.jl"] +Order = [:type] +``` + +## Create +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["task/create.jl"] +Order = [:function] +``` + +## Compare +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["task/compare.jl"] +Order = [:function] +``` + +## Properties +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["task/properties.jl"] +Order = [:function] +``` + +## Print +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["task/print.jl"] +Order = [:function] +``` diff --git a/docs/src/lib/internals/utility.md b/docs/src/lib/internals/utility.md new file mode 100644 index 0000000..ca034d8 --- /dev/null +++ b/docs/src/lib/internals/utility.md @@ -0,0 +1,17 @@ +# Utility + +## Helper Functions +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["utility.jl"] +Order = [:type, :function] +``` + +## Trie Helper +This is a simple implementation of a [Trie Data Structure](https://en.wikipedia.org/wiki/Trie) to greatly improve the performance of the Node Reduction search. + +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["trie.jl"] +Order = [:type, :function] +``` diff --git a/docs/src/lib/public.md b/docs/src/lib/public.md new file mode 100644 index 0000000..2157e07 --- /dev/null +++ b/docs/src/lib/public.md @@ -0,0 +1,24 @@ +# Public Documentation + +Documentation for `MetagraphOptimization.jl`'s public interface. + +See the Internals section of the manual for documentation of everything else. + +```@autodocs +Modules = [MetagraphOptimization] +Pages = ["MetagraphOptimization.jl"] +Order = [:module] +``` + +## Contents + +```@contents +Pages = ["public.md"] +Depth = 2 +``` + +## Index + +```@index +Pages = ["public.md"] +``` diff --git a/docs/src/manual.md b/docs/src/manual.md new file mode 100644 index 0000000..827c6b2 --- /dev/null +++ b/docs/src/manual.md @@ -0,0 +1,3 @@ +# Manual + +This will become a manual. diff --git a/examples/Project.toml b/examples/Project.toml index 68e6047..ac0e043 100644 --- a/examples/Project.toml +++ b/examples/Project.toml @@ -1,7 +1,3 @@ [deps] BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" MetagraphOptimization = "3e869610-d48d-4942-ba70-c1b702a33ca4" -PProf = "e4faabce-9ead-11e9-39d9-4379958e3056" -Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" -ProfileView = "c46f51b8-102a-5cf2-8d2c-8597cb0e0da7" -Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" diff --git a/src/MetagraphOptimization.jl b/src/MetagraphOptimization.jl index c75c14c..e4a0061 100644 --- a/src/MetagraphOptimization.jl +++ b/src/MetagraphOptimization.jl @@ -1,35 +1,52 @@ +""" + MetagraphOptimization + +A module containing tools to work on DAGs. +""" module MetagraphOptimization -export Node, Edge, ComputeTaskNode, DataTaskNode, DAG -export AbstractTask, - AbstractComputeTask, AbstractDataTask, DataTask, FusedComputeTask -export make_node, - make_edge, - insert_node, - insert_edge, - is_entry_node, - is_exit_node, - parents, - children, - compute, - graph_properties, - get_exit_node, - is_valid -export NodeFusion, - NodeReduction, - NodeSplit, - push_operation!, - pop_operation!, - can_pop, - reset_graph!, - get_operations -export parse_abc, - ComputeTaskP, - ComputeTaskS1, - ComputeTaskS2, - ComputeTaskV, - ComputeTaskU, - ComputeTaskSum +export DAG +export Node +export Edge +export ComputeTaskNode +export DataTaskNode +export AbstractTask +export AbstractComputeTask +export AbstractDataTask +export DataTask +export FusedComputeTask + +export make_node +export make_edge +export insert_node +export insert_edge +export is_entry_node +export is_exit_node +export parents +export children +export compute +export graph_properties +export get_exit_node +export is_valid + +export Operation +export AppliedOperation +export NodeFusion +export NodeReduction +export NodeSplit +export push_operation! +export pop_operation! +export can_pop +export reset_graph! +export get_operations + +export parse_abc +export ComputeTaskP +export ComputeTaskS1 +export ComputeTaskS2 +export ComputeTaskV +export ComputeTaskU +export ComputeTaskSum export ==, in, show, isempty, delete!, length @@ -79,6 +96,7 @@ include("operation/get.jl") include("operation/print.jl") include("operation/validate.jl") +include("task/create.jl") include("task/compare.jl") include("task/print.jl") include("task/properties.jl") diff --git a/src/diff/print.jl b/src/diff/print.jl index d949341..5f6c5ff 100644 --- a/src/diff/print.jl +++ b/src/diff/print.jl @@ -1,3 +1,8 @@ +""" + show(io::IO, diff::Diff) + +Pretty-print a [`Diff`](@ref). Called via print, println and co. +""" function show(io::IO, diff::Diff) print(io, "Nodes: ") print(io, length(diff.addedNodes) + length(diff.removedNodes)) diff --git a/src/diff/properties.jl b/src/diff/properties.jl index 63dfe67..86411ac 100644 --- a/src/diff/properties.jl +++ b/src/diff/properties.jl @@ -1,4 +1,9 @@ -# return a namedtuple of the lengths of the added/removed nodes/edges +""" + length(diff::Diff) + +Return a named tuple of the lengths of the added/removed nodes/edges. +The fields are `.addedNodes`, `.addedEdges`, `.removedNodes` and `.removedEdges`. +""" function length(diff::Diff) return ( addedNodes = length(diff.addedNodes), diff --git a/src/diff/type.jl b/src/diff/type.jl index e7e7471..b3b7de3 100644 --- a/src/diff/type.jl +++ b/src/diff/type.jl @@ -1,3 +1,8 @@ +""" + Diff + +A named tuple representing a difference of added and removed nodes and edges on a [`DAG`](@ref). +""" const Diff = NamedTuple{ (:addedNodes, :removedNodes, :addedEdges, :removedEdges), Tuple{Vector{Node}, Vector{Node}, Vector{Edge}, Vector{Edge}}, diff --git a/src/graph/compare.jl b/src/graph/compare.jl index 8a67523..0845de3 100644 --- a/src/graph/compare.jl +++ b/src/graph/compare.jl @@ -1,7 +1,30 @@ +""" + in(node::Node, graph::DAG) +Check whether the node is part of the graph. +""" in(node::Node, graph::DAG) = node in graph.nodes -in(edge::Edge, graph::DAG) = edge in graph.edges +""" + in(edge::Edge, graph::DAG) + +Check whether the edge is part of the graph. +""" +function in(edge::Edge, graph::DAG) + n1 = edge.edge[1] + n2 = edge.edge[2] + if !(n1 in graph) || !(n2 in graph) + return false + end + + return n1 in n2.children +end + +""" + ==(n1::Node, n2::Node, g::DAG) + +Check equality of two nodes in a graph. +""" function ==(n1::Node, n2::Node, g::DAG) if typeof(n1) != typeof(n2) return false diff --git a/src/graph/interface.jl b/src/graph/interface.jl index 20df42a..450419c 100644 --- a/src/graph/interface.jl +++ b/src/graph/interface.jl @@ -1,6 +1,10 @@ -# user interface on the DAG +""" + push_operation!(graph::DAG, operation::Operation) -# applies a new operation to the end of the graph +Apply a new operation to the graph. + +See also: [`DAG`](@ref), [`pop_operation!`](@ref) +""" function push_operation!(graph::DAG, operation::Operation) # 1.: Add the operation to the DAG push!(graph.operationsToApply, operation) @@ -8,7 +12,13 @@ function push_operation!(graph::DAG, operation::Operation) return nothing end -# reverts the latest applied operation, essentially like a ctrl+z for +""" + pop_operation!(graph::DAG) + +Revert the latest applied operation on the graph. + +See also: [`DAG`](@ref), [`push_operation!`](@ref) +""" function pop_operation!(graph::DAG) # 1.: Remove the operation from the appliedChain of the DAG if !isempty(graph.operationsToApply) @@ -23,10 +33,19 @@ function pop_operation!(graph::DAG) return nothing end +""" + can_pop(graph::DAG) + +Return `true` if [`pop_operation!`](@ref) is possible, `false` otherwise. +""" can_pop(graph::DAG) = !isempty(graph.operationsToApply) || !isempty(graph.appliedOperations) -# reset the graph to its initial state with no operations applied +""" + reset_graph!(graph::DAG) + +Reset the graph to its initial state with no operations applied. +""" function reset_graph!(graph::DAG) while (can_pop(graph)) pop_operation!(graph) diff --git a/src/graph/mute.jl b/src/graph/mute.jl index 94e68c8..e030fbd 100644 --- a/src/graph/mute.jl +++ b/src/graph/mute.jl @@ -3,6 +3,18 @@ # 2: keep track of what was changed for the diff (if track == true) # 3: invalidate operation caches +""" + insert_node!(graph::DAG, node::Node; track = true, invalidate_cache = true) + +Insert the node into the graph. + +## Keyword Arguments +`track::Bool`: Whether to add the changes to the [`DAG`](@ref)'s [`Diff`](@ref). Should be set `false` in parsing or graph creation functions for performance. + +`invalidate_cache::Bool`: Whether to invalidate caches associated with the changes. Should also be turned off for graph creation or parsing. + +See also: [`remove_node!`](@ref), [`insert_edge!`](@ref), [`remove_edge!`](@ref) +""" function insert_node!( graph::DAG, node::Node, @@ -26,6 +38,18 @@ function insert_node!( return node end +""" + insert_edge!(graph::DAG, node1::Node, node2::Node; track = true, invalidate_cache = true) + +Insert the edge between node1 (child) and node2 (parent) into the graph. + +## Keyword Arguments +`track::Bool`: Whether to add the changes to the [`DAG`](@ref)'s [`Diff`](@ref). Should be set `false` in parsing or graph creation functions for performance. + +`invalidate_cache::Bool`: Whether to invalidate caches associated with the changes. Should also be turned off for graph creation or parsing. + +See also: [`insert_node!`](@ref), [`remove_node!`](@ref), [`remove_edge!`](@ref) +""" function insert_edge!( graph::DAG, node1::Node, @@ -59,6 +83,18 @@ function insert_edge!( return nothing end +""" + remove_node!(graph::DAG, node::Node; track = true, invalidate_cache = true) + +Remove the node from the graph. + +## Keyword Arguments +`track::Bool`: Whether to add the changes to the [`DAG`](@ref)'s [`Diff`](@ref). Should be set `false` in parsing or graph creation functions for performance. + +`invalidate_cache::Bool`: Whether to invalidate caches associated with the changes. Should also be turned off for graph creation or parsing. + +See also: [`insert_node!`](@ref), [`insert_edge!`](@ref), [`remove_edge!`](@ref) +""" function remove_node!( graph::DAG, node::Node, @@ -86,6 +122,18 @@ function remove_node!( return nothing end +""" + remove_edge!(graph::DAG, node1::Node, node2::Node; track = true, invalidate_cache = true) + +Remove the edge between node1 (child) and node2 (parent) into the graph. + +## Keyword Arguments +`track::Bool`: Whether to add the changes to the [`DAG`](@ref)'s [`Diff`](@ref). Should be set `false` in parsing or graph creation functions for performance. + +`invalidate_cache::Bool`: Whether to invalidate caches associated with the changes. Should also be turned off for graph creation or parsing. + +See also: [`insert_node!`](@ref), [`remove_node!`](@ref), [`insert_edge!`](@ref) +""" function remove_edge!( graph::DAG, node1::Node, @@ -131,12 +179,24 @@ function remove_edge!( return nothing end -# return the graph "difference" since last time this function was called +""" + get_snapshot_diff(graph::DAG) + +Return the graph's [`Diff`](@ref) since last time this function was called. + +See also: [`revert_diff!`](@ref), [`AppliedOperation`](@ref) and [`revert_operation!`](@ref) +""" function get_snapshot_diff(graph::DAG) return swapfield!(graph, :diff, Diff()) end -# function to invalidate the operation caches for a given NodeFusion +""" + invalidate_caches!(graph::DAG, operation::NodeFusion) + +Invalidate the operation caches for a given [`NodeFusion`](@ref). + +This deletes the operation from the graph's possible operations and from the involved nodes' own operation caches. +""" function invalidate_caches!(graph::DAG, operation::NodeFusion) delete!(graph.possibleOperations, operation) @@ -149,7 +209,13 @@ function invalidate_caches!(graph::DAG, operation::NodeFusion) return nothing end -# function to invalidate the operation caches for a given NodeReduction +""" + invalidate_caches!(graph::DAG, operation::NodeReduction) + +Invalidate the operation caches for a given [`NodeReduction`](@ref). + +This deletes the operation from the graph's possible operations and from the involved nodes' own operation caches. +""" function invalidate_caches!(graph::DAG, operation::NodeReduction) delete!(graph.possibleOperations, operation) @@ -160,7 +226,13 @@ function invalidate_caches!(graph::DAG, operation::NodeReduction) return nothing end -# function to invalidate the operation caches for a given NodeSplit +""" + invalidate_caches!(graph::DAG, operation::NodeSplit) + +Invalidate the operation caches for a given [`NodeSplit`](@ref). + +This deletes the operation from the graph's possible operations and from the involved nodes' own operation caches. +""" function invalidate_caches!(graph::DAG, operation::NodeSplit) delete!(graph.possibleOperations, operation) @@ -171,7 +243,11 @@ function invalidate_caches!(graph::DAG, operation::NodeSplit) return nothing end -# function to invalidate the operation caches of a ComputeTaskNode +""" + invalidate_operation_caches!(graph::DAG, node::ComputeTaskNode) + +Invalidate the operation caches of the given node through calls to the respective [`invalidate_caches!`](@ref) functions. +""" function invalidate_operation_caches!(graph::DAG, node::ComputeTaskNode) if !ismissing(node.nodeReduction) invalidate_caches!(graph, node.nodeReduction) @@ -185,7 +261,11 @@ function invalidate_operation_caches!(graph::DAG, node::ComputeTaskNode) return nothing end -# function to invalidate the operation caches of a DataTaskNode +""" + invalidate_operation_caches!(graph::DAG, node::DataTaskNode) + +Invalidate the operation caches of the given node through calls to the respective [`invalidate_caches!`](@ref) functions. +""" function invalidate_operation_caches!(graph::DAG, node::DataTaskNode) if !ismissing(node.nodeReduction) invalidate_caches!(graph, node.nodeReduction) diff --git a/src/graph/print.jl b/src/graph/print.jl index 7717724..729b14c 100644 --- a/src/graph/print.jl +++ b/src/graph/print.jl @@ -1,4 +1,9 @@ -function show_nodes(io, graph::DAG) +""" + show_nodes(io::IO, graph::DAG) + +Print a graph's nodes. Should only be used for small graphs as it prints every node in a list. +""" +function show_nodes(io::IO, graph::DAG) print(io, "[") first = true for n in graph.nodes @@ -12,6 +17,11 @@ function show_nodes(io, graph::DAG) return print(io, "]") end +""" + show(io::IO, graph::DAG) + +Print the given graph to io. If there are too many nodes it will print only a summary of them. +""" function show(io::IO, graph::DAG) println(io, "Graph:") print(io, " Nodes: ") diff --git a/src/graph/properties.jl b/src/graph/properties.jl index da254d2..ed5e985 100644 --- a/src/graph/properties.jl +++ b/src/graph/properties.jl @@ -1,3 +1,8 @@ +""" + graph_properties(graph::DAG) + +Return the graph's properties, a named tuple with fields `.data`, `.compute_effort`, `.compute_intensity`, `.nodes` (number of nodes) and `.edges` (number of edges). +""" function graph_properties(graph::DAG) # make sure the graph is fully generated apply_all!(graph) @@ -23,6 +28,11 @@ function graph_properties(graph::DAG) return result end +""" + get_exit_node(graph::DAG) + +Return the graph's exit node. This assumes the graph only has a single exit node. If the graph has multiple exit nodes, the one encountered first will be returned. +""" function get_exit_node(graph::DAG) for node in graph.nodes if (is_exit_node(node)) diff --git a/src/graph/type.jl b/src/graph/type.jl index 06cc115..f65c228 100644 --- a/src/graph/type.jl +++ b/src/graph/type.jl @@ -1,21 +1,28 @@ using DataStructures +""" + PossibleOperations + +A struct storing all possible operations on a [`DAG`](@ref). +To get the [`PossibleOperations`](@ref) on a [`DAG`](@ref), use [`get_operations`](@ref). +""" mutable struct PossibleOperations nodeFusions::Set{NodeFusion} nodeReductions::Set{NodeReduction} nodeSplits::Set{NodeSplit} end -function PossibleOperations() - return PossibleOperations( - Set{NodeFusion}(), - Set{NodeReduction}(), - Set{NodeSplit}(), - ) -end +""" + DAG -# The actual state of the DAG is the initial state given by the set of nodes -# but with all the operations in appliedChain applied in order +The representation of the graph as a set of [`Node`](@ref)s. + +A DAG can be loaded using the appropriate parse function, e.g. [`parse_abc`](@ref). + +[`Operation`](@ref)s can be applied on it using [`push_operation!`](@ref) and reverted using [`pop_operation!`](@ref) like a stack. +To get the set of possible operations, use [`get_operations`](@ref). +The members of the object should not be manually accessed, instead always use the provided interface functions. +""" mutable struct DAG nodes::Set{Node} @@ -36,6 +43,24 @@ mutable struct DAG diff::Diff end +""" + PossibleOperations() + +Construct and return an empty [`PossibleOperations`](@ref) object. +""" +function PossibleOperations() + return PossibleOperations( + Set{NodeFusion}(), + Set{NodeReduction}(), + Set{NodeSplit}(), + ) +end + +""" + DAG() + +Construct and return an empty [`DAG`](@ref). +""" function DAG() return DAG( Set{Node}(), diff --git a/src/graph/validate.jl b/src/graph/validate.jl index d2c7dba..c033d2f 100644 --- a/src/graph/validate.jl +++ b/src/graph/validate.jl @@ -1,4 +1,8 @@ -# check whether the given graph is connected +""" + is_connected(graph::DAG) + +Return whether the given graph is connected. +""" function is_connected(graph::DAG) nodeQueue = Deque{Node}() push!(nodeQueue, get_exit_node(graph)) @@ -16,6 +20,11 @@ function is_connected(graph::DAG) return length(seenNodes) == length(graph.nodes) end +""" + is_valid(graph::DAG) + +Validate the entire graph using asserts. Intended for testing with `@assert is_valid(graph)`. +""" function is_valid(graph::DAG) for node in graph.nodes @assert is_valid(graph, node) diff --git a/src/models/abc/parse.jl b/src/models/abc/parse.jl index e5ed4c5..9553dc2 100644 --- a/src/models/abc/parse.jl +++ b/src/models/abc/parse.jl @@ -1,11 +1,14 @@ -using Printf - # functions for importing DAGs from a file regex_a = r"^[A-C]\d+$" # Regex for the initial particles regex_c = r"^[A-C]\(([^']*),([^']*)\)$" # Regex for the combinations of 2 particles regex_m = r"^M\(([^']*),([^']*),([^']*)\)$" # Regex for the combinations of 3 particles regex_plus = r"^\+$" # Regex for the sum +""" + parse_nodes(input::AbstractString) + +Parse the given string into a vector of strings containing each node. +""" function parse_nodes(input::AbstractString) regex = r"'([^']*)'" matches = eachmatch(regex, input) @@ -13,6 +16,11 @@ function parse_nodes(input::AbstractString) return output end +""" + parse_edges(input::AbstractString) + +Parse the given string into a vector of strings containing each edge. Currently unused since the entire graph can be read from just the node names. +""" function parse_edges(input::AbstractString) regex = r"\('([^']*)', '([^']*)'\)" matches = eachmatch(regex, input) @@ -20,7 +28,13 @@ function parse_edges(input::AbstractString) return output end -# reads an abc-model process from the given file +""" + parse_abc(filename::String; verbose::Bool = false) + +Read an abc-model process from the given file. If `verbose` is set to true, print some progress information to stdout. + +Returns a valid [`DAG`](@ref). +""" function parse_abc(filename::String, verbose::Bool = false) file = open(filename, "r") @@ -63,9 +77,11 @@ function parse_abc(filename::String, verbose::Bool = false) noNodes += 1 if (noNodes % 100 == 0) if (verbose) - @printf "\rReading Nodes... %.2f%%" ( - 100.0 * noNodes / nodesToRead + percent = string( + round(100.0 * noNodes / nodesToRead, digits = 2), + "%", ) + print("\rReading Nodes... $percent") end end if occursin(regex_a, node) diff --git a/src/models/abc/properties.jl b/src/models/abc/properties.jl index a4f6506..fcc254b 100644 --- a/src/models/abc/properties.jl +++ b/src/models/abc/properties.jl @@ -1,21 +1,102 @@ -# define compute_efforts tasks computation -# put some "random" numbers here for now +""" + compute_effort(t::ComputeTaskS1) + +Return the compute effort of an S1 task. +""" compute_effort(t::ComputeTaskS1) = 10 + +""" + compute_effort(t::ComputeTaskS2) + +Return the compute effort of an S2 task. +""" compute_effort(t::ComputeTaskS2) = 10 + +""" + compute_effort(t::ComputeTaskU) + +Return the compute effort of a U task. +""" compute_effort(t::ComputeTaskU) = 6 + +""" + compute_effort(t::ComputeTaskV) + +Return the compute effort of a V task. +""" compute_effort(t::ComputeTaskV) = 20 + +""" + compute_effort(t::ComputeTaskP) + +Return the compute effort of a P task. +""" compute_effort(t::ComputeTaskP) = 15 + +""" + compute_effort(t::ComputeTaskSum) + +Return the compute effort of a Sum task. + +Note: This is a constant compute effort, even though sum scales with the number of its inputs. Since there is only ever a single sum node in a graph generated from the ABC-Model, +this doesn't matter. +""" compute_effort(t::ComputeTaskSum) = 1 +""" + show(io::IO, t::DataTask) + +Print the data task to io. +""" function show(io::IO, t::DataTask) return print(io, "Data", t.data) end +""" + show(io::IO, t::ComputeTaskS1) + +Print the S1 task to io. +""" show(io::IO, t::ComputeTaskS1) = print("ComputeS1") + +""" + show(io::IO, t::ComputeTaskS2) + +Print the S2 task to io. +""" show(io::IO, t::ComputeTaskS2) = print("ComputeS2") + +""" + show(io::IO, t::ComputeTaskP) + +Print the P task to io. +""" show(io::IO, t::ComputeTaskP) = print("ComputeP") + +""" + show(io::IO, t::ComputeTaskU) + +Print the U task to io. +""" show(io::IO, t::ComputeTaskU) = print("ComputeU") + +""" + show(io::IO, t::ComputeTaskV) + +Print the V task to io. +""" show(io::IO, t::ComputeTaskV) = print("ComputeV") + +""" + show(io::IO, t::ComputeTaskSum) + +Print the sum task to io. +""" show(io::IO, t::ComputeTaskSum) = print("ComputeSum") +""" + copy(t::DataTask) + +Copy the data task and return it. +""" copy(t::DataTask) = DataTask(t.data) diff --git a/src/models/abc/types.jl b/src/models/abc/types.jl index 5404b21..a160128 100644 --- a/src/models/abc/types.jl +++ b/src/models/abc/types.jl @@ -1,25 +1,59 @@ +""" + DataTask <: AbstractDataTask + +Task representing a specific data transfer in the ABC Model. +""" struct DataTask <: AbstractDataTask data::UInt64 end -# S task with 1 child +""" + ComputeTaskS1 <: AbstractComputeTask + +S task with a single child. +""" struct ComputeTaskS1 <: AbstractComputeTask end -# S task with 2 children +""" + ComputeTaskS2 <: AbstractComputeTask + +S task with two children. +""" struct ComputeTaskS2 <: AbstractComputeTask end -# P task with 0 children +""" + ComputeTaskP <: AbstractComputeTask + +P task with no children. +""" struct ComputeTaskP <: AbstractComputeTask end -# v task with 2 children +""" + ComputeTaskV <: AbstractComputeTask + +v task with two children. +""" struct ComputeTaskV <: AbstractComputeTask end -# u task with 1 child +""" + ComputeTaskU <: AbstractComputeTask + +u task with a single child. +""" struct ComputeTaskU <: AbstractComputeTask end -# task that sums all its inputs, n children +""" + ComputeTaskSum <: AbstractComputeTask + +Task that sums all its inputs, n children. +""" struct ComputeTaskSum <: AbstractComputeTask end +""" + ABC_TASKS + +Constant vector of all tasks of the ABC-Model. +""" ABC_TASKS = [ DataTask, ComputeTaskS1, diff --git a/src/node/compare.jl b/src/node/compare.jl index 3743690..d2e2c04 100644 --- a/src/node/compare.jl +++ b/src/node/compare.jl @@ -1,15 +1,35 @@ +""" + ==(e1::Edge, e2::Edge) + +Equality comparison between two edges. +""" function ==(e1::Edge, e2::Edge) return e1.edge[1] == e2.edge[1] && e1.edge[2] == e2.edge[2] end +""" + ==(n1::Node, n2::Node) + +Fallback equality comparison between two nodes. For equal node types, the more specific versions of this function will be called. +""" function ==(n1::Node, n2::Node) return false end +""" + ==(n1::ComputeTaskNode, n2::ComputeTaskNode) + +Equality comparison between two [`ComputeTaskNode`](@ref)s. +""" function ==(n1::ComputeTaskNode, n2::ComputeTaskNode) return n1.id == n2.id end +""" + ==(n1::DataTaskNode, n2::DataTaskNode) + +Equality comparison between two [`DataTaskNode`](@ref)s. +""" function ==(n1::DataTaskNode, n2::DataTaskNode) return n1.id == n2.id end diff --git a/src/node/create.jl b/src/node/create.jl index b13d990..dd01d05 100644 --- a/src/node/create.jl +++ b/src/node/create.jl @@ -1,23 +1,53 @@ +""" + make_node(t::AbstractTask) + +Fallback implementation of `make_node` for an [`AbstractTask`](@ref), throwing an error. +""" function make_node(t::AbstractTask) return error("Cannot make a node from this task type") end +""" + make_node(t::AbstractDataTask) + +Construct and return a new [`DataTaskNode`](@ref) with the given task. +""" function make_node(t::AbstractDataTask) return DataTaskNode(t) end +""" + make_node(t::AbstractComputeTask) + +Construct and return a new [`ComputeTaskNode`](@ref) with the given task. +""" function make_node(t::AbstractComputeTask) return ComputeTaskNode(t) end +""" + make_edge(n1::Node, n2::Node) + +Fallback implementation of `make_edge` throwing an error. If you got this error it likely means you tried to construct an edge between two nodes of the same type. +""" function make_edge(n1::Node, n2::Node) return error("Can only create edges from compute to data node or reverse") end +""" + make_edge(n1::ComputeTaskNode, n2::DataTaskNode) + +Construct and return a new [`Edge`](@ref) pointing from `n1` (child) to `n2` (parent). +""" function make_edge(n1::ComputeTaskNode, n2::DataTaskNode) return Edge((n1, n2)) end +""" + make_edge(n1::DataTaskNode, n2::ComputeTaskNode) + +Construct and return a new [`Edge`](@ref) pointing from `n1` (child) to `n2` (parent). +""" function make_edge(n1::DataTaskNode, n2::ComputeTaskNode) return Edge((n1, n2)) end diff --git a/src/node/print.jl b/src/node/print.jl index debaa88..3a6ee1a 100644 --- a/src/node/print.jl +++ b/src/node/print.jl @@ -1,7 +1,17 @@ +""" + show(io::IO, n::Node) + +Print a short string representation of the node to io. +""" function show(io::IO, n::Node) return print(io, "Node(", n.task, ")") end +""" + show(io::IO, e::Edge) + +Print a short string representation of the edge to io. +""" function show(io::IO, e::Edge) return print(io, "Edge(", e.edge[1], ", ", e.edge[2], ")") end diff --git a/src/node/properties.jl b/src/node/properties.jl index abbe252..2edb400 100644 --- a/src/node/properties.jl +++ b/src/node/properties.jl @@ -1,17 +1,46 @@ +""" + is_entry_node(node::Node) + +Return whether this node is an entry node in its graph, i.e., it has no children. +""" is_entry_node(node::Node) = length(node.children) == 0 + +""" + is_exit_node(node::Node) + +Return whether this node is an exit node of its graph, i.e., it has no parents. +""" is_exit_node(node::Node) = length(node.parents) == 0 -# children = prerequisite nodes, nodes that need to execute before the task, edges point into this task +""" + children(node::Node) + +Return a copy of the node's children so it can safely be muted without changing the node in the graph. + +A node's children are its prerequisite nodes, nodes that need to execute before the task of this node. +""" function children(node::Node) return copy(node.children) end -# parents = subsequent nodes, nodes that need this node to execute, edges point from this task +""" + parents(node::Node) + +Return a copy of the node's parents so it can safely be muted without changing the node in the graph. + +A node's parents are its subsequent nodes, nodes that need this node to execute. +""" function parents(node::Node) return copy(node.parents) end -# siblings = all children of any parents, no duplicates, includes the node itself +""" + siblings(node::Node) + +Return a vector of all siblings of this node. + +A node's siblings are all children of any of its parents. The result contains no duplicates and includes the node itself. +""" function siblings(node::Node) result = Set{Node}() push!(result, node) @@ -22,7 +51,16 @@ function siblings(node::Node) return result end -# partners = all parents of any children, no duplicates, includes the node itself +""" + partners(node::Node) + +Return a vector of all partners of this node. + +A node's partners are all parents of any of its children. The result contains no duplicates and includes the node itself. + +Note: This is very slow when there are multiple children with many parents. +This is less of a problem in [`siblings(node::Node)`](@ref) because (depending on the model) there are no nodes with a large number of children, or only a single one. +""" function partners(node::Node) result = Set{Node}() push!(result, node) @@ -33,8 +71,11 @@ function partners(node::Node) return result end -# alternative version to partners(Node), avoiding allocation of a new set -# works on the given set and returns nothing +""" + partners(node::Node, set::Set{Node}) + +Alternative version to [`partners(node::Node)`](@ref), avoiding allocation of a new set. Works on the given set and returns `nothing`. +""" function partners(node::Node, set::Set{Node}) push!(set, node) for child in node.children @@ -43,10 +84,20 @@ function partners(node::Node, set::Set{Node}) return nothing end -function is_parent(potential_parent, node) +""" + is_parent(potential_parent::Node, node::Node) + +Return whether the `potential_parent` is a parent of `node`. +""" +function is_parent(potential_parent::Node, node::Node) return potential_parent in node.parents end -function is_child(potential_child, node) +""" + is_child(potential_child::Node, node::Node) + +Return whether the `potential_child` is a child of `node`. +""" +function is_child(potential_child::Node, node::Node) return potential_child in node.children end diff --git a/src/node/type.jl b/src/node/type.jl index 45ff267..db32f1b 100644 --- a/src/node/type.jl +++ b/src/node/type.jl @@ -5,12 +5,33 @@ using Base.Threads # TODO: reliably find out how many threads we're running with (nthreads() returns 1 when precompiling :/) rng = [Random.MersenneTwister(0) for _ in 1:32] +""" + Node + +The abstract base type of every node. + +See [`DataTaskNode`](@ref), [`ComputeTaskNode`](@ref) and [`make_node`](@ref). +""" abstract type Node end # declare this type here because it's needed # the specific operations are declared in graph.jl abstract type Operation end +""" + DataTaskNode <: Node + +Any node that transfers data and does no computation. + +# Fields +`.task`: The node's data task type. Usually [`DataTask`](@ref).\\ +`.parents`: A vector of the node's parents (i.e. nodes that depend on this one).\\ +`.children`: A vector of the node's children (i.e. nodes that this one depends on).\\ +`.id`: The node's id. Improves the speed of comparisons.\\ +`.nodeReduction`: Either this node's [`NodeReduction`](@ref) or `missing`, if none. There can only be at most one.\\ +`.nodeSplit`: Either this node's [`NodeSplit`](@ref) or `missing`, if none. There can only be at most one.\\ +`.nodeFusion`: Either this node's [`NodeFusion`](@ref) or `missing`, if none. There can only be at most one for DataTaskNodes. +""" mutable struct DataTaskNode <: Node task::AbstractDataTask @@ -33,7 +54,20 @@ mutable struct DataTaskNode <: Node nodeFusion::Union{Operation, Missing} end -# same as DataTaskNode +""" + ComputeTaskNode <: Node + +Any node that transfers data and does no computation. + +# Fields +`.task`: The node's data task type. Usually [`DataTask`](@ref).\\ +`.parents`: A vector of the node's parents (i.e. nodes that depend on this one).\\ +`.children`: A vector of the node's children (i.e. nodes that this one depends on).\\ +`.id`: The node's id. Improves the speed of comparisons.\\ +`.nodeReduction`: Either this node's [`NodeReduction`](@ref) or `missing`, if none. There can only be at most one.\\ +`.nodeSplit`: Either this node's [`NodeSplit`](@ref) or `missing`, if none. There can only be at most one.\\ +`.nodeFusion`: A vector of this node's [`NodeFusion`](@ref)s. For a ComputeTaskNode there can be any number of these, unlike the DataTaskNodes. +""" mutable struct ComputeTaskNode <: Node task::AbstractComputeTask parents::Vector{Node} @@ -66,6 +100,15 @@ ComputeTaskNode(t::AbstractComputeTask) = ComputeTaskNode( Vector{NodeFusion}(), ) +""" + Edge + +Type of an edge in the graph. Edges can only exist between a [`DataTaskNode`](@ref) and a [`ComputeTaskNode`](@ref) or vice versa, not between two of the same type of node. + +An edge always points from child to parent: `child = e.edge[1]` and `parent = e.edge[2]`. + +The child is the prerequisite node of the parent. +""" struct Edge # edge points from child to parent edge::Union{ diff --git a/src/node/validate.jl b/src/node/validate.jl index 5cd1244..a16df17 100644 --- a/src/node/validate.jl +++ b/src/node/validate.jl @@ -1,3 +1,12 @@ +""" + is_valid_node(graph::DAG, node::Node) + +Verify that a given node is valid in the graph. Call like `@test is_valid_node(g, n)`. Uses `@assert` to fail if something is invalid but also provide an error message. + +This function is very performance intensive and should only be used when testing or debugging. + +See also this function's specific versions for the concrete Node types [`is_valid(graph::DAG, node::ComputeTaskNode)`](@ref) and [`is_valid(graph::DAG, node::DataTaskNode)`](@ref). +""" function is_valid_node(graph::DAG, node::Node) @assert node in graph "Node is not part of the given graph!" @@ -22,7 +31,13 @@ function is_valid_node(graph::DAG, node::Node) return true end -# call with @assert +""" + is_valid(graph::DAG, node::ComputeTaskNode) + +Verify that the given compute node is valid in the graph. Call with `@assert` or `@test` when testing or debugging. + +This also calls [`is_valid_node(graph::DAG, node::Node)`](@ref). +""" function is_valid(graph::DAG, node::ComputeTaskNode) @assert is_valid_node(graph, node) @@ -32,7 +47,13 @@ function is_valid(graph::DAG, node::ComputeTaskNode) return true end -# call with @assert +""" + is_valid(graph::DAG, node::DataTaskNode) + +Verify that the given compute node is valid in the graph. Call with `@assert` or `@test` when testing or debugging. + +This also calls [`is_valid_node(graph::DAG, node::Node)`](@ref). +""" function is_valid(graph::DAG, node::DataTaskNode) @assert is_valid_node(graph, node) diff --git a/src/operation/apply.jl b/src/operation/apply.jl index f61953a..6230322 100644 --- a/src/operation/apply.jl +++ b/src/operation/apply.jl @@ -1,6 +1,8 @@ -# functions that apply graph operations +""" + apply_all!(graph::DAG) -# applies all unapplied operations in the DAG +Apply all unapplied operations in the DAG. Is automatically called in all functions that require the latest state of the [`DAG`](@ref). +""" function apply_all!(graph::DAG) while !isempty(graph.operationsToApply) # get next operation to apply from front of the deque @@ -15,10 +17,22 @@ function apply_all!(graph::DAG) return nothing end +""" + apply_operation!(graph::DAG, operation::Operation) + +Fallback implementation of apply_operation! for unimplemented operation types, throwing an error. +""" function apply_operation!(graph::DAG, operation::Operation) return error("Unknown operation type!") end +""" + apply_operation!(graph::DAG, operation::NodeFusion) + +Apply the given [`NodeFusion`](@ref) to the graph. Generic wrapper around [`node_fusion!`](@ref). + +Return an [`AppliedNodeFusion`](@ref) object generated from the graph's [`Diff`](@ref). +""" function apply_operation!(graph::DAG, operation::NodeFusion) diff = node_fusion!( graph, @@ -29,37 +43,74 @@ function apply_operation!(graph::DAG, operation::NodeFusion) return AppliedNodeFusion(operation, diff) end +""" + apply_operation!(graph::DAG, operation::NodeReduction) + +Apply the given [`NodeReduction`](@ref) to the graph. Generic wrapper around [`node_reduction!`](@ref). + +Return an [`AppliedNodeReduction`](@ref) object generated from the graph's [`Diff`](@ref). +""" function apply_operation!(graph::DAG, operation::NodeReduction) diff = node_reduction!(graph, operation.input) return AppliedNodeReduction(operation, diff) end +""" + apply_operation!(graph::DAG, operation::NodeSplit) + +Apply the given [`NodeSplit`](@ref) to the graph. Generic wrapper around [`node_split!`](@ref). + +Return an [`AppliedNodeSplit`](@ref) object generated from the graph's [`Diff`](@ref). +""" function apply_operation!(graph::DAG, operation::NodeSplit) diff = node_split!(graph, operation.input) return AppliedNodeSplit(operation, diff) end +""" + revert_operation!(graph::DAG, operation::AppliedOperation) +Fallback implementation of operation reversion for unimplemented operation types, throwing an error. +""" function revert_operation!(graph::DAG, operation::AppliedOperation) return error("Unknown operation type!") end +""" + revert_operation!(graph::DAG, operation::AppliedNodeFusion) + +Revert the applied node fusion on the graph. Return the original [`NodeFusion`](@ref) operation. +""" function revert_operation!(graph::DAG, operation::AppliedNodeFusion) revert_diff!(graph, operation.diff) return operation.operation end +""" + revert_operation!(graph::DAG, operation::AppliedNodeReduction) + +Revert the applied node fusion on the graph. Return the original [`NodeReduction`](@ref) operation. +""" function revert_operation!(graph::DAG, operation::AppliedNodeReduction) revert_diff!(graph, operation.diff) return operation.operation end +""" + revert_operation!(graph::DAG, operation::AppliedNodeSplit) + +Revert the applied node fusion on the graph. Return the original [`NodeSplit`](@ref) operation. +""" function revert_operation!(graph::DAG, operation::AppliedNodeSplit) revert_diff!(graph, operation.diff) return operation.operation end +""" + revert_diff!(graph::DAG, diff::Diff) +Revert the given diff on the graph. Used to revert the individual [`AppliedOperation`](@ref)s with [`revert_operation!`](@ref). +""" function revert_diff!(graph::DAG, diff::Diff) # add removed nodes, remove added nodes, same for edges # note the order @@ -76,9 +127,16 @@ function revert_diff!(graph::DAG, diff::Diff) for edge in diff.removedEdges insert_edge!(graph, edge.edge[1], edge.edge[2], false) end + return nothing end -# Fuse nodes n1 -> n2 -> n3 together into one node, return the applied difference to the graph +""" + node_fusion!(graph::DAG, n1::ComputeTaskNode, n2::DataTaskNode, n3::ComputeTaskNode) + +Fuse nodes n1 -> n2 -> n3 together into one node, return the applied difference to the graph. + +For details see [`NodeFusion`](@ref). +""" function node_fusion!( graph::DAG, n1::ComputeTaskNode, @@ -139,6 +197,13 @@ function node_fusion!( return get_snapshot_diff(graph) end +""" + node_reduction!(graph::DAG, nodes::Vector{Node}) + +Reduce the given nodes together into one node, return the applied difference to the graph. + +For details see [`NodeReduction`](@ref). +""" function node_reduction!(graph::DAG, nodes::Vector{Node}) # @assert is_valid_node_reduction_input(graph, nodes) @@ -178,6 +243,13 @@ function node_reduction!(graph::DAG, nodes::Vector{Node}) return get_snapshot_diff(graph) end +""" + node_split!(graph::DAG, n1::Node) + +Split the given node into one node per parent, return the applied difference to the graph. + +For details see [`NodeSplit`](@ref). +""" function node_split!(graph::DAG, n1::Node) # @assert is_valid_node_split_input(graph, n1) diff --git a/src/operation/clean.jl b/src/operation/clean.jl index 142e21a..e42a2a3 100644 --- a/src/operation/clean.jl +++ b/src/operation/clean.jl @@ -1,9 +1,14 @@ -# functions for "cleaning" nodes, i.e. regenerating the possible operations for a node +# These are functions for "cleaning" nodes, i.e. regenerating the possible operations for a node -# function to find node fusions involving the given node if it's a data node -# pushes the found fusion everywhere it needs to be and returns nothing +""" + find_fusions!(graph::DAG, node::DataTaskNode) + +Find node fusions involving the given data node. The function pushes the found [`NodeFusion`](@ref) (if any) everywhere it needs to be and returns nothing. + +Does nothing if the node already has a node fusion set. Since it's a data node, only one node fusion can be possible with it. +""" function find_fusions!(graph::DAG, node::DataTaskNode) - # if there is already a fusion here, skip + # if there is already a fusion here, skip to avoid duplicates if !ismissing(node.nodeFusion) return nothing end @@ -32,7 +37,11 @@ function find_fusions!(graph::DAG, node::DataTaskNode) return nothing end +""" + find_fusions!(graph::DAG, node::ComputeTaskNode) +Find node fusions involving the given compute node. The function pushes the found [`NodeFusion`](@ref)s (if any) everywhere they need to be and returns nothing. +""" function find_fusions!(graph::DAG, node::ComputeTaskNode) # just find fusions in neighbouring DataTaskNodes for child in node.children @@ -46,6 +55,11 @@ function find_fusions!(graph::DAG, node::ComputeTaskNode) return nothing end +""" + find_reductions!(graph::DAG, node::Node) + +Find node reductions involving the given node. The function pushes the found [`NodeReduction`](@ref) (if any) everywhere it needs to be and returns nothing. +""" function find_reductions!(graph::DAG, node::Node) # there can only be one reduction per node, avoid adding duplicates if !ismissing(node.nodeReduction) @@ -91,6 +105,11 @@ function find_reductions!(graph::DAG, node::Node) return nothing end +""" + find_splits!(graph::DAG, node::Node) + +Find the node split of the given node. The function pushes the found [`NodeSplit`](@ref) (if any) everywhere it needs to be and returns nothing. +""" function find_splits!(graph::DAG, node::Node) if !ismissing(node.nodeSplit) return nothing @@ -105,11 +124,17 @@ function find_splits!(graph::DAG, node::Node) return nothing end -# "clean" the operations on a dirty node +""" + clean_node!(graph::DAG, node::Node) + +Sort this node's parent and child sets, then find fusions, reductions and splits involving it. Needs to be called after the node was changed in some way. +""" function clean_node!(graph::DAG, node::Node) sort_node!(node) find_fusions!(graph, node) find_reductions!(graph, node) - return find_splits!(graph, node) + find_splits!(graph, node) + + return nothing end diff --git a/src/operation/find.jl b/src/operation/find.jl index e851e21..89acc3a 100644 --- a/src/operation/find.jl +++ b/src/operation/find.jl @@ -2,6 +2,11 @@ using Base.Threads +""" + insert_operation!(nf::NodeFusion, locks::Dict{ComputeTaskNode, SpinLock}) + +Insert the given node fusion into its input nodes' operation caches. For the compute nodes, locking via the given `locks` is employed to have safe multi-threading. For a large set of nodes, contention on the locks should be very small. +""" function insert_operation!( nf::NodeFusion, locks::Dict{ComputeTaskNode, SpinLock}, @@ -20,6 +25,11 @@ function insert_operation!( return nothing end +""" + insert_operation!(nf::NodeReduction) + +Insert the given node reduction into its input nodes' operation caches. This is thread-safe. +""" function insert_operation!(nr::NodeReduction) for n in nr.input n.nodeReduction = nr @@ -27,11 +37,21 @@ function insert_operation!(nr::NodeReduction) return nothing end +""" + insert_operation!(nf::NodeSplit) + +Insert the given node split into its input node's operation cache. This is thread-safe. +""" function insert_operation!(ns::NodeSplit) ns.input.nodeSplit = ns return nothing end +""" + nr_insertion!(operations::PossibleOperations, nodeReductions::Vector{Vector{NodeReduction}}) + +Insert the node reductions into the graph and the nodes' caches. Employs multithreading for speedup. +""" function nr_insertion!( operations::PossibleOperations, nodeReductions::Vector{Vector{NodeReduction}}, @@ -58,6 +78,11 @@ function nr_insertion!( return nothing end +""" + nf_insertion!(graph::DAG, operations::PossibleOperations, nodeFusions::Vector{Vector{NodeFusion}}) + +Insert the node fusions into the graph and the nodes' caches. Employs multithreading for speedup. +""" function nf_insertion!( graph::DAG, operations::PossibleOperations, @@ -92,6 +117,11 @@ function nf_insertion!( return nothing end +""" + ns_insertion!(operations::PossibleOperations, nodeSplits::Vector{Vector{NodeSplits}}) + +Insert the node splits into the graph and the nodes' caches. Employs multithreading for speedup. +""" function ns_insertion!( operations::PossibleOperations, nodeSplits::Vector{Vector{NodeSplit}}, @@ -118,8 +148,14 @@ function ns_insertion!( return nothing end -# function to generate all possible operations on the graph -function generate_options(graph::DAG) +""" + generate_operations(graph::DAG) + +Generate all possible operations on the graph. Used initially when the graph is freshly assembled or parsed. Uses multithreading for speedup. + +Safely inserts all the found operations into the graph and its nodes. +""" +function generate_operations(graph::DAG) generatedFusions = [Vector{NodeFusion}() for _ in 1:nthreads()] generatedReductions = [Vector{NodeReduction}() for _ in 1:nthreads()] generatedSplits = [Vector{NodeSplit}() for _ in 1:nthreads()] diff --git a/src/operation/get.jl b/src/operation/get.jl index b527fb1..bb8653a 100644 --- a/src/operation/get.jl +++ b/src/operation/get.jl @@ -2,11 +2,16 @@ using Base.Threads +""" + get_operations(graph::DAG) + +Return the [`PossibleOperations`](@ref) of the graph at the current state. +""" function get_operations(graph::DAG) apply_all!(graph) if isempty(graph.possibleOperations) - generate_options(graph) + generate_operations(graph) end for node in graph.dirtyNodes diff --git a/src/operation/print.jl b/src/operation/print.jl index 39f5e8c..61239be 100644 --- a/src/operation/print.jl +++ b/src/operation/print.jl @@ -1,3 +1,8 @@ +""" + show(io::IO, ops::PossibleOperations) + +Print a string representation of the set of possible operations to io. +""" function show(io::IO, ops::PossibleOperations) print(io, length(ops.nodeFusions)) println(io, " Node Fusions: ") @@ -16,6 +21,11 @@ function show(io::IO, ops::PossibleOperations) end end +""" + show(io::IO, op::NodeReduction) + +Print a string representation of the node reduction to io. +""" function show(io::IO, op::NodeReduction) print(io, "NR: ") print(io, length(op.input)) @@ -23,11 +33,21 @@ function show(io::IO, op::NodeReduction) return print(io, op.input[1].task) end +""" + show(io::IO, op::NodeSplit) + +Print a string representation of the node split to io. +""" function show(io::IO, op::NodeSplit) print(io, "NS: ") return print(io, op.input.task) end +""" + show(io::IO, op::NodeFusion) + +Print a string representation of the node fusion to io. +""" function show(io::IO, op::NodeFusion) print(io, "NF: ") print(io, op.input[1].task) diff --git a/src/operation/type.jl b/src/operation/type.jl index c9c68b7..edc67c3 100644 --- a/src/operation/type.jl +++ b/src/operation/type.jl @@ -1,33 +1,116 @@ -# An abstract base class for operations -# an operation can be applied to a DAG +""" + Operation + +An abstract base class for operations. An operation can be applied to a [`DAG`](@ref), changing its nodes and edges. + +Possible operations on a [`DAG`](@ref) can be retrieved using [`get_operations`](@ref). + +See also: [`push_operation!`](@ref), [`pop_operation!`](@ref) +""" abstract type Operation end -# An abstract base class for already applied operations -# an applied operation can be reversed iff it is the last applied operation on the DAG +""" + AppliedOperation + +An abstract base class for already applied operations. +An applied operation can be reversed iff it is the last applied operation on the DAG. +Every applied operation stores a [`Diff`](@ref) from when it was initially applied to be able to revert the operation. + +See also: [`revert_operation!`](@ref). +""" abstract type AppliedOperation end +""" + NodeFusion <: Operation + +The NodeFusion operation. Represents the fusing of a chain of compute node -> data node -> compute node. + +After the node fusion is applied, the graph has 2 fewer nodes and edges, and a new [`FusedComputeTask`](@ref) with the two input compute nodes as parts. + +# Requirements for successful application + +A chain of (n1, n2, n3) can be fused if: +- All nodes are in the graph. +- (n1, n2) is an edge in the graph. +- (n2, n3) is an edge in the graph. +- n2 has exactly one parent (n3) and exactly one child (n1). +- n1 has exactly one parent (n2). + +[`is_valid_node_fusion_input`](@ref) can be used to `@assert` these requirements. + +See also: [`can_fuse`](@ref) +""" struct NodeFusion <: Operation input::Tuple{ComputeTaskNode, DataTaskNode, ComputeTaskNode} end +""" + AppliedNodeFusion <: AppliedOperation + +The applied version of the [`NodeFusion`](@ref). +""" struct AppliedNodeFusion <: AppliedOperation operation::NodeFusion diff::Diff end +""" + NodeReduction <: Operation + +The NodeReduction operation. Represents the reduction of two or more nodes with one another. +Only one of the input nodes is kept, while all others are deleted and their parents are accumulated in the kept node's parents instead. + +After the node reduction is applied, the graph has `length(nr.input) - 1` fewer nodes. + +# Requirements for successful application + +A vector of nodes can be reduced if: +- All nodes are in the graph. +- All nodes have the same task type. +- All nodes have the same set of children. + +[`is_valid_node_reduction_input`](@ref) can be used to `@assert` these requirements. + +See also: [`can_reduce`](@ref) +""" struct NodeReduction <: Operation input::Vector{Node} end +""" + AppliedNodeReduction <: AppliedOperation + +The applied version of the [`NodeReduction`](@ref). +""" struct AppliedNodeReduction <: AppliedOperation operation::NodeReduction diff::Diff end +""" + NodeSplit <: Operation + +The NodeSplit operation. Represents the split of its input node into one node for each of its parents. It is the reverse operation to the [`NodeReduction`](@ref). + +# Requirements for successful application + +A node can be split if: +- It is in the graph. +- It has at least 2 parents. + +[`is_valid_node_split_input`](@ref) can be used to `@assert` these requirements. + +See also: [`can_split`](@ref) +""" struct NodeSplit <: Operation input::Node end +""" + AppliedNodeSplit <: AppliedOperation + +The applied version of the [`NodeSplit`](@ref). +""" struct AppliedNodeSplit <: AppliedOperation operation::NodeSplit diff::Diff diff --git a/src/operation/utility.jl b/src/operation/utility.jl index 97297b0..2c1bae5 100644 --- a/src/operation/utility.jl +++ b/src/operation/utility.jl @@ -1,10 +1,19 @@ +""" + isempty(operations::PossibleOperations) +Return whether `operations` is empty, i.e. all of its fields are empty. +""" function isempty(operations::PossibleOperations) return isempty(operations.nodeFusions) && isempty(operations.nodeReductions) && isempty(operations.nodeSplits) end +""" + length(operations::PossibleOperations) + +Return a named tuple with the number of each of the operation types as a named tuple. The fields are named the same as the [`PossibleOperations`](@ref)'. +""" function length(operations::PossibleOperations) return ( nodeFusions = length(operations.nodeFusions), @@ -13,22 +22,41 @@ function length(operations::PossibleOperations) ) end +""" + delete!(operations::PossibleOperations, op::NodeFusion) + +Delete the given node fusion from the possible operations. +""" function delete!(operations::PossibleOperations, op::NodeFusion) delete!(operations.nodeFusions, op) return operations end +""" + delete!(operations::PossibleOperations, op::NodeReduction) + +Delete the given node reduction from the possible operations. +""" function delete!(operations::PossibleOperations, op::NodeReduction) delete!(operations.nodeReductions, op) return operations end +""" + delete!(operations::PossibleOperations, op::NodeSplit) + +Delete the given node split from the possible operations. +""" function delete!(operations::PossibleOperations, op::NodeSplit) delete!(operations.nodeSplits, op) return operations end +""" + can_fuse(n1::ComputeTaskNode, n2::DataTaskNode, n3::ComputeTaskNode) +Return whether the given nodes can be fused. See [`NodeFusion`](@ref) for the requirements. +""" function can_fuse(n1::ComputeTaskNode, n2::DataTaskNode, n3::ComputeTaskNode) if !is_child(n1, n2) || !is_child(n2, n3) # the checks are redundant but maybe a good sanity check @@ -44,6 +72,11 @@ function can_fuse(n1::ComputeTaskNode, n2::DataTaskNode, n3::ComputeTaskNode) return true end +""" + can_reduce(n1::Node, n2::Node) + +Return whether the given two nodes can be reduced. See [`NodeReduction`](@ref) for the requirements. +""" function can_reduce(n1::Node, n2::Node) if (n1.task != n2.task) return false @@ -86,26 +119,49 @@ function can_reduce(n1::Node, n2::Node) return Set(n1.children) == Set(n2.children) end +""" + can_split(n1::Node) + +Return whether the given node can be split. See [`NodeSplit`](@ref) for the requirements. +""" function can_split(n::Node) return length(parents(n)) > 1 end +""" + ==(op1::Operation, op2::Operation) + +Fallback implementation of operation equality. Return false. Actual comparisons are done by the overloads of same type operation comparisons. +""" function ==(op1::Operation, op2::Operation) return false end +""" + ==(op1::NodeFusion, op2::NodeFusion) + +Equality comparison between two node fusions. Two node fusions are considered equal if they have the same inputs. +""" function ==(op1::NodeFusion, op2::NodeFusion) # there can only be one node fusion on a given data task, so if the data task is the same, the fusion is the same return op1.input[2] == op2.input[2] end +""" + ==(op1::NodeReduction, op2::NodeReduction) + +Equality comparison between two node reductions. Two node reductions are considered equal when they have the same inputs. +""" function ==(op1::NodeReduction, op2::NodeReduction) # node reductions are equal exactly if their first input is the same return op1.input[1].id == op2.input[1].id end +""" + ==(op1::NodeSplit, op2::NodeSplit) + +Equality comparison between two node splits. Two node splits are considered equal if they have the same input node. +""" function ==(op1::NodeSplit, op2::NodeSplit) return op1.input == op2.input end - -copy(id::UUID) = UUID(id.value) diff --git a/src/operation/validate.jl b/src/operation/validate.jl index 5b8dca5..5d41e87 100644 --- a/src/operation/validate.jl +++ b/src/operation/validate.jl @@ -2,6 +2,13 @@ # should be called with @assert # the functions throw their own errors though, to still have helpful error messages +""" + is_valid_node_fusion_input(graph::DAG, n1::ComputeTaskNode, n2::DataTaskNode, n3::ComputeTaskNode) + +Assert for a gven node fusion input whether the nodes can be fused. For the requirements of a node fusion see [`NodeFusion`](@ref). + +Intended for use with `@assert` or `@test`. +""" function is_valid_node_fusion_input( graph::DAG, n1::ComputeTaskNode, @@ -52,6 +59,13 @@ function is_valid_node_fusion_input( return true end +""" + is_valid_node_reduction_input(graph::DAG, nodes::Vector{Node}) + +Assert for a gven node reduction input whether the nodes can be reduced. For the requirements of a node reduction see [`NodeReduction`](@ref). + +Intended for use with `@assert` or `@test`. +""" function is_valid_node_reduction_input(graph::DAG, nodes::Vector{Node}) for n in nodes if n ∉ graph @@ -88,6 +102,13 @@ function is_valid_node_reduction_input(graph::DAG, nodes::Vector{Node}) return true end +""" + is_valid_node_split_input(graph::DAG, n1::Node) + +Assert for a gven node split input whether the node can be split. For the requirements of a node split see [`NodeSplit`](@ref). + +Intended for use with `@assert` or `@test`. +""" function is_valid_node_split_input(graph::DAG, n1::Node) if n1 ∉ graph throw( @@ -108,18 +129,39 @@ function is_valid_node_split_input(graph::DAG, n1::Node) return true end +""" + is_valid(graph::DAG, nr::NodeReduction) + +Assert for a given [`NodeReduction`](@ref) whether it is a valid operation in the graph. + +Intended for use with `@assert` or `@test`. +""" function is_valid(graph::DAG, nr::NodeReduction) @assert is_valid_node_reduction_input(graph, nr.input) @assert nr in graph.possibleOperations.nodeReductions "NodeReduction is not part of the graph's possible operations!" return true end +""" + is_valid(graph::DAG, nr::NodeSplit) + +Assert for a given [`NodeSplit`](@ref) whether it is a valid operation in the graph. + +Intended for use with `@assert` or `@test`. +""" function is_valid(graph::DAG, ns::NodeSplit) @assert is_valid_node_split_input(graph, ns.input) @assert ns in graph.possibleOperations.nodeSplits "NodeSplit is not part of the graph's possible operations!" return true end +""" + is_valid(graph::DAG, nr::NodeFusion) + +Assert for a given [`NodeFusion`](@ref) whether it is a valid operation in the graph. + +Intended for use with `@assert` or `@test`. +""" function is_valid(graph::DAG, nf::NodeFusion) @assert is_valid_node_fusion_input( graph, diff --git a/src/task/compare.jl b/src/task/compare.jl index cb31f94..e960550 100644 --- a/src/task/compare.jl +++ b/src/task/compare.jl @@ -1,11 +1,26 @@ +""" + ==(t1::AbstractTask, t2::AbstractTask) + +Fallback implementation of equality comparison between two abstract tasks. Always returns false. For equal specific types of t1 and t2, a more specific comparison is called instead, doing an actual comparison. +""" function ==(t1::AbstractTask, t2::AbstractTask) return false end +""" + ==(t1::AbstractComputeTask, t2::AbstractComputeTask) + +Equality comparison between two compute tasks. +""" function ==(t1::AbstractComputeTask, t2::AbstractComputeTask) return typeof(t1) == typeof(t2) end +""" + ==(t1::AbstractDataTask, t2::AbstractDataTask) + +Equality comparison between two data tasks. +""" function ==(t1::AbstractDataTask, t2::AbstractDataTask) return data(t1) == data(t2) end diff --git a/src/task/create.jl b/src/task/create.jl new file mode 100644 index 0000000..9124984 --- /dev/null +++ b/src/task/create.jl @@ -0,0 +1,14 @@ +""" + copy(t::AbstractDataTask) + +Fallback implementation of the copy of an abstract data task, throwing an error. +""" +copy(t::AbstractDataTask) = + error("Need to implement copying for your data tasks!") + +""" + copy(t::AbstractComputeTask) + +Return a copy of the given compute task. +""" +copy(t::AbstractComputeTask) = typeof(t)() diff --git a/src/task/print.jl b/src/task/print.jl index 3848e4c..5909b5f 100644 --- a/src/task/print.jl +++ b/src/task/print.jl @@ -1,3 +1,8 @@ +""" + show(io::IO, t::FusedComputeTask) + +Print a string representation of the fused compute task to io. +""" function show(io::IO, t::FusedComputeTask) (T1, T2) = get_types(t) return print(io, "ComputeFuse(", T1(), ", ", T2(), ")") diff --git a/src/task/properties.jl b/src/task/properties.jl index 5b6defc..dd120e9 100644 --- a/src/task/properties.jl +++ b/src/task/properties.jl @@ -1,22 +1,64 @@ +""" + compute(t::AbstractTask; data...) + +Fallback implementation of the compute function of a compute task, throwing an error. +""" function compute(t::AbstractTask; data...) return error("Need to implement compute()") end +""" + compute_effort(t::AbstractTask) + +Fallback implementation of the compute effort of a task, throwing an error. +""" function compute_effort(t::AbstractTask) # default implementation using compute return error("Need to implement compute_effort()") end +""" + data(t::AbstractTask) + +Fallback implementation of the data of a task, throwing an error. +""" function data(t::AbstractTask) return error("Need to implement data()") end +""" + compute_effort(t::AbstractDataTask) + +Return the compute effort of a data task, always zero, regardless of the specific task. +""" compute_effort(t::AbstractDataTask) = 0 + +""" + compute(t::AbstractDataTask; data...) + +The compute function of a data task, always the identity function, regardless of the specific task. +""" compute(t::AbstractDataTask; data...) = data + +""" + data(t::AbstractDataTask) + +Return the data of a data task. Given by the task's `.data` field. +""" data(t::AbstractDataTask) = getfield(t, :data) +""" + data(t::AbstractComputeTask) + +Return the data of a compute task, always zero, regardless of the specific task. +""" data(t::AbstractComputeTask) = 0 +""" + compute_effort(t::FusedComputeTask) + +Return the compute effort of a fused compute task. +""" function compute_effort(t::FusedComputeTask) (T1, T2) = collect(typeof(t).parameters) return compute_effort(T1()) + compute_effort(T2()) @@ -25,9 +67,9 @@ end # actual compute functions for the tasks can stay undefined for now # compute(t::ComputeTaskU, data::Any) = mycomputation(data) -function compute_intensity(t::AbstractTask)::UInt64 - if data(t) == 0 - return typemax(UInt64) - end - return compute_effort(t) / data(t) -end +""" + get_types(::FusedComputeTask{T1, T2}) + +Return a tuple of a the fused compute task's components' types. +""" +get_types(::FusedComputeTask{T1, T2}) where {T1, T2} = (T1, T2) diff --git a/src/task/type.jl b/src/task/type.jl index c9e6f1b..5d5e329 100644 --- a/src/task/type.jl +++ b/src/task/type.jl @@ -1,13 +1,30 @@ +""" + AbstractTask + +The shared base type for any task. +""" abstract type AbstractTask end +""" + AbstractComputeTask <: AbstractTask + +The shared base type for any compute task. +""" abstract type AbstractComputeTask <: AbstractTask end + +""" + AbstractDataTask <: AbstractTask + +The shared base type for any data task. +""" abstract type AbstractDataTask <: AbstractTask end +""" + FusedComputeTask{T1 <: AbstractComputeTask, T2 <: AbstractComputeTask} <: AbstractComputeTask + +A fused compute task made up of the computation of first `T1` and then `T2`. + +Also see: [`get_types`](@ref). +""" struct FusedComputeTask{T1 <: AbstractComputeTask, T2 <: AbstractComputeTask} <: AbstractComputeTask end - -get_types(::FusedComputeTask{T1, T2}) where {T1, T2} = (T1, T2) - -copy(t::AbstractDataTask) = - error("Need to implement copying for your data tasks!") -copy(t::AbstractComputeTask) = typeof(t)() diff --git a/src/trie.jl b/src/trie.jl index 77ff648..7637e19 100644 --- a/src/trie.jl +++ b/src/trie.jl @@ -1,26 +1,49 @@ +""" + NodeIdTrie -# helper struct for NodeTrie +Helper struct for [`NodeTrie`](@ref). After the Trie's first level, every Trie level contains the vector of nodes that had children up to that level, and the TrieNode's children by UUID of the node's children. +""" mutable struct NodeIdTrie value::Vector{Node} children::Dict{UUID, NodeIdTrie} end -# Trie data structure for node reduction, inserts nodes by children -# Assumes that given nodes have ordered vectors of children (see sort_node) -# First level is the task type and thus does not have a value -# Should be constructed with all Types that will be used +""" + NodeTrie + +Trie data structure for node reduction, inserts nodes by children. +Assumes that given nodes have ordered vectors of children (see [`sort_node!`](@ref)). +First insertion level is the node's own task type and thus does not have a value (every node has a task type). + +See also: [`insert!`](@ref) and [`collect`](@ref) +""" mutable struct NodeTrie children::Dict{DataType, NodeIdTrie} end +""" + NodeTrie() + +Constructor for an empty [`NodeTrie`](@ref). +""" function NodeTrie() return NodeTrie(Dict{DataType, NodeIdTrie}()) end +""" + NodeIdTrie() + +Constructor for an empty [`NodeIdTrie`](@ref). +""" function NodeIdTrie() return NodeIdTrie(Vector{Node}(), Dict{UUID, NodeIdTrie}()) end +""" + insert_helper!(trie::NodeIdTrie, node::Node, depth::Int) + +Insert the given node into the trie. The depth is used to iterate through the trie layers, while the function calls itself recursively until it ran through all children of the node. +""" function insert_helper!(trie::NodeIdTrie, node::Node, depth::Int) if (length(node.children) == depth) push!(trie.value, node) @@ -36,6 +59,11 @@ function insert_helper!(trie::NodeIdTrie, node::Node, depth::Int) return insert_helper!(trie.children[id], node, depth) end +""" + insert!(trie::NodeTrie, node::Node) + +Insert the given node into the trie. It's sorted by its type in the first layer, then by its children in the following layers. +""" function insert!(trie::NodeTrie, node::Node) t = typeof(node.task) if (!haskey(trie.children, t)) @@ -44,6 +72,11 @@ function insert!(trie::NodeTrie, node::Node) return insert_helper!(trie.children[typeof(node.task)], node, 0) end +""" + collect_helper(trie::NodeIdTrie, acc::Set{Vector{Node}}) + +Collects the Vectors of this [`NodeIdTrie`](@ref) node and all its children and puts them in the `acc` argument. +""" function collect_helper(trie::NodeIdTrie, acc::Set{Vector{Node}}) if (length(trie.value) >= 2) push!(acc, trie.value) @@ -55,7 +88,11 @@ function collect_helper(trie::NodeIdTrie, acc::Set{Vector{Node}}) return nothing end -# returns all sets of multiple nodes that have accumulated in leaves +""" + collect(trie::NodeTrie) + +Return all sets of at least 2 [`Node`](@ref)s that have accumulated in leaves of the trie. +""" function collect(trie::NodeTrie) acc = Set{Vector{Node}}() for (t, child) in trie.children diff --git a/src/utility.jl b/src/utility.jl index 96ad4a3..2d4b39e 100644 --- a/src/utility.jl +++ b/src/utility.jl @@ -1,3 +1,15 @@ +""" + bytes_to_human_readable(bytes) + +Return a human readable string representation of the given number. + +```jldoctest +julia> using MetagraphOptimization + +julia> bytes_to_human_readable(4096) +"4.0 KiB" +``` +""" function bytes_to_human_readable(bytes) units = ["B", "KiB", "MiB", "GiB", "TiB"] unit_index = 1 @@ -8,15 +20,31 @@ function bytes_to_human_readable(bytes) return string(round(bytes, sigdigits = 4), " ", units[unit_index]) end +""" + lt_nodes(n1::Node, n2::Node) + +Less-Than comparison between nodes. Uses the nodes' ids to sort. +""" function lt_nodes(n1::Node, n2::Node) return n1.id < n2.id end +""" + sort_node!(node::Node) + +Sort the nodes' parents and children vectors. The vectors are mostly very short so sorting does not take a lot of time. +Sorted nodes are required to make the finding of [`NodeReduction`](@ref)s a lot faster using the [`NodeTrie`](@ref) data structure. +""" function sort_node!(node::Node) sort!(node.children, lt = lt_nodes) return sort!(node.parents, lt = lt_nodes) end +""" + mem(graph::DAG) + +Return the memory footprint of the graph in Byte. Should be the same result as `Base.summarysize(graph)` but a lot faster. +""" function mem(graph::DAG) size = 0 size += Base.summarysize(graph.nodes, exclude = Union{Node}) @@ -42,12 +70,20 @@ function mem(graph::DAG) return size += sizeof(diff) end -# calculate the size of this operation in Byte +""" + mem(op::Operation) + +Return the memory footprint of the operation in Byte. Used in [`mem(graph::DAG)`](@ref). Unlike `Base.summarysize()` this doesn't follow all references which would yield (almost) the size of the entire graph. +""" function mem(op::Operation) return Base.summarysize(op, exclude = Union{Node}) end -# calculate the size of this node in Byte +""" + mem(op::Operation) + +Return the memory footprint of the node in Byte. Used in [`mem(graph::DAG)`](@ref). Unlike `Base.summarysize()` this doesn't follow all references which would yield (almost) the size of the entire graph. +""" function mem(node::Node) return Base.summarysize(node, exclude = Union{Node, Operation}) end diff --git a/test/unit_tests_tasks.jl b/test/unit_tests_tasks.jl index b4d6c96..c039ece 100644 --- a/test/unit_tests_tasks.jl +++ b/test/unit_tests_tasks.jl @@ -28,15 +28,6 @@ @test MetagraphOptimization.data(Data10) == 10 @test MetagraphOptimization.data(Data20) == 20 - @test MetagraphOptimization.compute_intensity(S1) == typemax(UInt64) - @test MetagraphOptimization.compute_intensity(S2) == typemax(UInt64) - @test MetagraphOptimization.compute_intensity(U) == typemax(UInt64) - @test MetagraphOptimization.compute_intensity(V) == typemax(UInt64) - @test MetagraphOptimization.compute_intensity(P) == typemax(UInt64) - @test MetagraphOptimization.compute_intensity(Sum) == typemax(UInt64) - @test MetagraphOptimization.compute_intensity(Data10) == 0 - @test MetagraphOptimization.compute_intensity(Data20) == 0 - @test S1 != S2 @test Data10 != Data20