Add AB->AB Graph and fix many many things

This commit is contained in:
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 @@ 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