Initial Code

This commit is contained in:
Anton Reinhard 2023-05-25 17:20:16 +02:00
parent 965ce37e74
commit 264d5f834b
9 changed files with 169 additions and 0 deletions

2
.gitignore vendored
View File

@ -24,3 +24,5 @@ docs/site/
# environment.
Manifest.toml
# vscode workspace directory
.vscode

4
Project.toml Normal file
View File

@ -0,0 +1,4 @@
name = "metagraph_optimization"
uuid = "3e869610-d48d-4942-ba70-c1b702a33ca4"
authors = ["Anton Reinhard <anton.reinhard@wandelbots.com>"]
version = "0.1.0"

4
src/graph.jl Normal file
View File

@ -0,0 +1,4 @@
mutable struct DAG
nodes::Vector{Node}
edges::Vector{Edge}
end

56
src/graph_functions.jl Normal file
View File

@ -0,0 +1,56 @@
in(node::Node, graph::DAG) = node in graph.nodes
in(edge::Edge, graph::DAG) = edge in graph.edges
function ==(n1::Node, n2::Node, g::DAG)
if typeof(n1) != typeof(n2)
return false
end
if !(n1 in g) || !(n2 in g)
return false
end
return n1.task == n2.task && children(n1, g) == children(n2, g)
end
# children = prerequisite nodes, nodes that need to execute before the task, edges point into this task
function children(graph::DAG, node::Node)
if !(node in graph)
error("Cannot get children of a node that's not in the given graph")
end
result::Vector{Edge}
for edge in graph.edges
if (edge[2] == node)
push!(result, edge)
end
end
return result
end
# parents = subsequent nodes, nodes that need this node to execute, edges point from this task
function parents(graph::DAG, node::Node)
if !(node in graph)
error("Cannot get parents of a node that's not in the given graph")
end
result::Vector{Edge}
for edge in graph.edges
if (edge[1] == node)
push!(result, edge)
end
end
return result
end
is_entry_node(graph::DAG, node::Node) = size(children(graph, node)) == 0
is_exit_node(graph::DAG, node::Node) = size(parents(graph, node)) == 0
function insert_node(graph::DAG, node::Node)
push!(graph.nodes, node)
end
function insert_edge(graph::DAG, edge::Edge)
push!(graph.edges, edge)
end

View File

@ -0,0 +1,14 @@
module metagraph_optimization
include("tasks.jl")
include("nodes.jl")
include("graph.jl")
include("task_functions.jl")
include("node_functions.jl")
include("graph_functions.jl")
export ==, in, Node, Edge, ComputeTaskNode, DataTaskNode, DAG
export AbstractTask, AbstractComputeTask, AbstractDataTask, DataTask, ComputeTaskP, ComputeTaskS1, ComputeTaskS2, ComputeTaskV, ComputeTaskU
export insert_node, insert_edge, is_entry_node, is_exit_node, parents, children
end # module metagraph_optimization

0
src/node_functions.jl Normal file
View File

13
src/nodes.jl Normal file
View File

@ -0,0 +1,13 @@
abstract type Node end
struct DataTaskNode <: Node
task::AbstractTask
end
struct ComputeTaskNode <: Node
task::AbstractTask
end
struct Edge
edge::Union{Tuple{Ref{DataTaskNode}, Ref{ComputeTaskNode}}, Tuple{Ref{ComputeTaskNode}, Ref{DataTaskNode}}}
end

48
src/task_functions.jl Normal file
View File

@ -0,0 +1,48 @@
function compute(t::AbstractTask; data...)
error("Need to implement compute()")
end
function compute_effort(t::AbstractTask)
# default implementation using compute
error("Need to implement compute_effort()")
end
function data(t::AbstractTask)
error("Need to implement data()")
end
compute_effort(t::AbstractDataTask) = 0
compute(t::AbstractDataTask; data...) = data
data(t::AbstractDataTask) = getfield(t, :data)
data(t::AbstractComputeTask) = 0
# define compute_efforts tasks computation
# put some "random" numbers here for now
compute_effort(t::ComputeTaskS1) = 10
compute_effort(t::ComputeTaskS2) = 10
compute_effort(t::ComputeTaskU) = 6
compute_effort(t::ComputeTaskV) = 20
compute_effort(t::ComputeTaskP) = 15
# actual compute functions for the tasks can stay undefined for now
# compute(t::ComputeTaskU, data::Any) = mycomputation(data)
function computation_intensity(t::AbstractTask)::UInt32
if data(t) == 0
return typemax(UInt32)
end
return compute_effort(t) / data(t)
end
function ==(t1::AbstractTask, t2::AbstractTask)
return false
end
function ==(t1::AbstractComputeTask, t2::AbstractComputeTask)
return typeof(t1) == typeof(t2)
end
function ==(t1::AbstractDataTask, t2::AbstractDataTask)
return data(t1) == data(t2)
end

28
src/tasks.jl Normal file
View File

@ -0,0 +1,28 @@
abstract type AbstractTask end
abstract type AbstractComputeTask <: AbstractTask end
abstract type AbstractDataTask <: AbstractTask end
struct DataTask <: AbstractDataTask
data::UInt32
end
# S task with 1 child
struct ComputeTaskS1 <: AbstractComputeTask
end
# S task with 2 children
struct ComputeTaskS2 <: AbstractComputeTask
end
# P task with 0 children
struct ComputeTaskP <: AbstractComputeTask
end
# v task with 2 children
struct ComputeTaskV <: AbstractComputeTask
end
# u task with 1 child
struct ComputeTaskU <: AbstractComputeTask
end