Add AB->AB Graph and fix many many things

This commit is contained in:
Anton Reinhard 2023-05-26 19:25:21 +02:00
parent 55cf8e4f17
commit 572b0adf0f
10 changed files with 248 additions and 20 deletions

View File

@ -2,3 +2,7 @@ name = "metagraph_optimization"
uuid = "3e869610-d48d-4942-ba70-c1b702a33ca4"
authors = ["Anton Reinhard <anton.reinhard@wandelbots.com>"]
version = "0.1.0"
[deps]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

121
examples/ABAB_graph.jl Normal file
View File

@ -0,0 +1,121 @@
using metagraph_optimization
function main()
graph = DAG()
# s to output (exit node)
d_exit = make_node(DataTask(10))
# final s compute
s0 = make_node(ComputeTaskS2())
# data from v0 and v1 to s0
d_v0_s0 = make_node(DataTask(5))
d_v1_s0 = make_node(DataTask(5))
# v0 and v1 compute
v0 = make_node(ComputeTaskV())
v1 = make_node(ComputeTaskV())
# data from uB, uA, uBp and uAp to v0 and v1
d_uB_v0 = make_node(DataTask(3))
d_uA_v0 = make_node(DataTask(3))
d_uBp_v1 = make_node(DataTask(3))
d_uAp_v1 = make_node(DataTask(3))
# uB, uA, uBp and uAp computes
uB = make_node(ComputeTaskU())
uA = make_node(ComputeTaskU())
uBp = make_node(ComputeTaskU())
uAp = make_node(ComputeTaskU())
# data from PB, PA, PBp and PAp to uB, uA, uBp and uAp
d_PB_uB = make_node(DataTask(6))
d_PA_uA = make_node(DataTask(6))
d_PBp_uBp = make_node(DataTask(6))
d_PAp_uAp = make_node(DataTask(6))
# P computes PB, PA, PBp and PAp
PB = make_node(ComputeTaskP())
PA = make_node(ComputeTaskP())
PBp = make_node(ComputeTaskP())
PAp = make_node(ComputeTaskP())
# entry nodes getting data for P computes
d_PB = make_node(DataTask(4))
d_PA = make_node(DataTask(4))
d_PBp = make_node(DataTask(4))
d_PAp = make_node(DataTask(4))
# now insert them all
insert_node(graph, d_exit)
insert_node(graph, s0)
insert_node(graph, d_v0_s0)
insert_node(graph, d_v1_s0)
insert_node(graph, v0)
insert_node(graph, v1)
insert_node(graph, d_uB_v0)
insert_node(graph, d_uA_v0)
insert_node(graph, d_uBp_v1)
insert_node(graph, d_uAp_v1)
insert_node(graph, uB)
insert_node(graph, uA)
insert_node(graph, uBp)
insert_node(graph, uAp)
insert_node(graph, d_PB_uB)
insert_node(graph, d_PA_uA)
insert_node(graph, d_PBp_uBp)
insert_node(graph, d_PAp_uAp)
insert_node(graph, PB)
insert_node(graph, PA)
insert_node(graph, PBp)
insert_node(graph, PAp)
insert_node(graph, d_PB)
insert_node(graph, d_PA)
insert_node(graph, d_PBp)
insert_node(graph, d_PAp)
# now for all the edgese
insert_edge(graph, make_edge(d_PB, PB))
insert_edge(graph, make_edge(d_PA, PA))
insert_edge(graph, make_edge(d_PBp, PBp))
insert_edge(graph, make_edge(d_PAp, PAp))
insert_edge(graph, make_edge(PB, d_PB_uB))
insert_edge(graph, make_edge(PA, d_PA_uA))
insert_edge(graph, make_edge(PBp, d_PBp_uBp))
insert_edge(graph, make_edge(PAp, d_PAp_uAp))
insert_edge(graph, make_edge(d_PB_uB, uB))
insert_edge(graph, make_edge(d_PA_uA, uA))
insert_edge(graph, make_edge(d_PBp_uBp, uBp))
insert_edge(graph, make_edge(d_PAp_uAp, uAp))
insert_edge(graph, make_edge(uB, d_uB_v0))
insert_edge(graph, make_edge(uA, d_uA_v0))
insert_edge(graph, make_edge(uBp, d_uBp_v1))
insert_edge(graph, make_edge(uAp, d_uAp_v1))
insert_edge(graph, make_edge(d_uB_v0, v0))
insert_edge(graph, make_edge(d_uA_v0, v0))
insert_edge(graph, make_edge(d_uBp_v1, v1))
insert_edge(graph, make_edge(d_uAp_v1, v1))
insert_edge(graph, make_edge(v0, d_v0_s0))
insert_edge(graph, make_edge(v1, d_v1_s0))
insert_edge(graph, make_edge(d_v0_s0, s0))
insert_edge(graph, make_edge(d_v1_s0, s0))
insert_edge(graph, make_edge(s0, d_exit))
print(graph)
node_fusion(graph, PB, d_PB_uB, uB)
println("Same graph after node fusion")
print(graph)
end
main()

View File

@ -2,3 +2,7 @@ mutable struct DAG
nodes::Vector{Node}
edges::Vector{Edge}
end
function DAG()
return DAG(Vector{Node}(), Vector{Edge}())
end

View File

@ -18,10 +18,10 @@ function children(graph::DAG, node::Node)
error("Cannot get children of a node that's not in the given graph")
end
result::Vector{Edge}
result = Vector{Node}()
for edge in graph.edges
if (edge[2] == node)
push!(result, edge)
if (edge.edge[2][] == node)
push!(result, edge.edge[1][])
end
end
@ -34,10 +34,10 @@ function parents(graph::DAG, node::Node)
error("Cannot get parents of a node that's not in the given graph")
end
result::Vector{Edge}
result = Vector{Node}()
for edge in graph.edges
if (edge[1] == node)
push!(result, edge)
if (edge.edge[1][] == node)
push!(result, edge.edge[2][])
end
end
@ -68,7 +68,7 @@ function remove_edge(graph::DAG, edge::Edge)
end
function data(graph::DAG)
data_sum::Int32 = 0
data_sum::Int64 = 0
for node in graph.nodes
data_sum = data_sum + data(node.task)
end
@ -76,7 +76,7 @@ function data(graph::DAG)
end
function compute_effort(graph::DAG)
compute_effort_sum::Int32 = 0
compute_effort_sum::Int64 = 0
for node in graph.nodes
compute_effort_sum = compute_effort_sum + compute_effort(node.task)
end
@ -86,7 +86,48 @@ end
function compute_intensity(graph::DAG)
data_sum = data(graph)
if data_sum == 0
return typemax(UInt32)
return typemax(UInt64)
end
return compute_effort(graph) / data_sum
end
function show_nodes(io, graph::DAG)
print(io, "[")
first = true
for n in graph.nodes
if first
first = false
else
print(", ")
end
print(io, n)
end
print(io, "]")
end
function show_edges(io, graph::DAG)
print(io, "[")
first = true
for e in graph.edges
if first
first = false
else
print(", ")
end
print(io, e)
end
print(io, "]")
end
function show(io::IO, graph::DAG)
println(io, "Graph:")
print(" Nodes: ")
show_nodes(io, graph)
println()
print(" Edges: ")
show_edges(io, graph)
println()
println(" Total Compute Effort: ", compute_effort(graph))
println(" Total Data Transfer: ", data(graph))
println(" Total Compute Intensity: ", compute_intensity(graph))
end

View File

@ -23,7 +23,7 @@ function node_fusion(graph::DAG, n1::ComputeTaskNode, n2::DataTaskNode, n3::Comp
remove_node(graph, n3)
# create new node with the fused compute task
new_node = ComputeTaskNode(FusedComputeTask{typeof(n1), typeof(n3)}())
new_node = ComputeTaskNode(FusedComputeTask{typeof(n1.task), typeof(n3.task)}())
insert_node(graph, new_node)
for child in n1_children

View File

@ -1,4 +1,7 @@
module metagraph_optimization
import Base.show
import Base.==
import Base.in
include("tasks.jl")
include("nodes.jl")
@ -8,9 +11,11 @@ include("node_functions.jl")
include("graph_functions.jl")
include("graph_optimizations.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, compute, data, compute_effort, compute_intensity
export Node, Edge, ComputeTaskNode, DataTaskNode, DAG
export AbstractTask, AbstractComputeTask, AbstractDataTask, DataTask, ComputeTaskP, ComputeTaskS1, ComputeTaskS2, ComputeTaskV, ComputeTaskU, FusedComputeTask
export make_node, make_edge, insert_node, insert_edge, is_entry_node, is_exit_node, parents, children, compute, data, compute_effort, compute_intensity
export node_fusion, node_reduction, node_split
export ==, in, show
end # module metagraph_optimization

View File

@ -1,12 +1,35 @@
function make_node(t::AbstractTask)
error("Cannot make a node from this task type")
end
function make_node(t::AbstractDataTask)
return DataTaskNode(t)
end
function make_node(t::AbstractComputeTask)
return ComputeTaskNode(t)
end
function make_edge(n1::Node, n2::Node)
error("Can only create edges from compute to data node or reverse")
end
function make_edge(n1::ComputeTaskNode, n2::DataTaskNode)
return Edge(Tuple{ComputeTaskNode, DataTaskNode}(Ref(n1), Ref(n2)))
return Edge((Ref(n1), Ref(n2)))
end
function make_edge(n1::DataTaskNode, n2::ComputeTaskNode)
return Edge(Tuple{DataTaskNode, ComputeTaskNode}(Ref(n2), Ref(n3)))
return Edge((Ref(n1), Ref(n2)))
end
function show(io::IO, n::Node)
print(io, "Node(", n.task, ")")
end
function show(io::IO, e::Edge)
print(io, "Edge(", e.edge[1][], ", ", e.edge[2][], ")")
end
function ==(e1::Edge, e2::Edge)
return e1.edge[1][] == e2.edge[1][] && e1.edge[2][] == e2.edge[2][]
end

View File

@ -1,11 +1,26 @@
using Random
using UUIDs
rng = Random.MersenneTwister(0);
abstract type Node end
struct DataTaskNode <: Node
task::AbstractTask
task::AbstractDataTask
id::Base.UUID # need a unique identifier unique to every *constructed* node
end
struct ComputeTaskNode <: Node
task::AbstractTask
task::AbstractComputeTask
id::Base.UUID # need a unique identifier unique to every *constructed* node
end
function DataTaskNode(t::AbstractDataTask)
return DataTaskNode(t, UUIDs.uuid1(rng))
end
function ComputeTaskNode(t::AbstractComputeTask)
return ComputeTaskNode(t, UUIDs.uuid1(rng))
end
struct Edge

View File

@ -32,13 +32,28 @@ end
# actual compute functions for the tasks can stay undefined for now
# compute(t::ComputeTaskU, data::Any) = mycomputation(data)
function compute_intensity(t::AbstractTask)::UInt32
function compute_intensity(t::AbstractTask)::UInt64
if data(t) == 0
return typemax(UInt32)
return typemax(UInt64)
end
return compute_effort(t) / data(t)
end
function show(io::IO, t::DataTask)
print(io, "Data", t.data)
end
show(io::IO, t::ComputeTaskS1) = print("ComputeS1")
show(io::IO, t::ComputeTaskS2) = print("ComputeS2")
show(io::IO, t::ComputeTaskP) = print("ComputeP")
show(io::IO, t::ComputeTaskU) = print("ComputeU")
show(io::IO, t::ComputeTaskV) = print("ComputeV")
function show(io::IO, t::FusedComputeTask)
(T1, T2) = collect(typeof(t).parameters)
print(io, "ComputeFuse(", T1(), ", ", T2(), ")")
end
function ==(t1::AbstractTask, t2::AbstractTask)
return false
end

View File

@ -4,7 +4,7 @@ abstract type AbstractComputeTask <: AbstractTask end
abstract type AbstractDataTask <: AbstractTask end
struct DataTask <: AbstractDataTask
data::UInt32
data::UInt64
end
# S task with 1 child