Fix topoligical ordering on the graph

This commit is contained in:
2023-09-05 12:14:41 +02:00
parent 7a1a97dac8
commit 0f78053ccf
6 changed files with 105 additions and 27 deletions

View File

@@ -7,29 +7,30 @@ function gen_code(graph::DAG)
nodeQueue = PriorityQueue{Node, Int}()
inputSyms = Vector{Symbol}()
# use a priority equal to the number of unseen children -> 0 are nodes that can be added
for node in get_entry_nodes(graph)
enqueue!(nodeQueue, node => 1)
push!(
inputSyms,
Symbol("data_$(replace(string(node.id), "-"=>"_"))_in"),
)
enqueue!(nodeQueue, node => 0)
push!(inputSyms, Symbol("data_$(to_var_name(node.id))_in"))
end
node = nothing
while !isempty(nodeQueue)
prio = peek(nodeQueue)[2]
@assert peek(nodeQueue)[2] == 0
node = dequeue!(nodeQueue)
push!(code, get_expression(node))
for parent in node.parents
# reduce the priority of all parents by one
if (!haskey(nodeQueue, parent))
enqueue!(nodeQueue, parent => prio + length(parent.children))
enqueue!(nodeQueue, parent => length(parent.children) - 1)
else
nodeQueue[parent] = nodeQueue[parent] - 1
end
end
end
# node is now the last node we looked at -> the output node
outSym = Symbol("data_$(replace(string(node.id), "-"=>"_"))")
outSym = Symbol("data_$(to_var_name(node.id))")
return (
code = Expr(:block, code...),
@@ -38,6 +39,28 @@ function gen_code(graph::DAG)
)
end
function execute(generated_code, input::Vector{Particle})
(code, inputSymbols, outputSymbol) = generated_code
@assert length(input) == length(inputSymbols)
assignInputs = Vector{Expr}()
for i in 1:length(input)
push!(
assignInputs,
Meta.parse(
"$(inputSymbols[i]) = ParticleValue(Particle($(input[i]).P0, $(input[i]).P1, $(input[i]).P2, $(input[i]).P3, $(input[i]).m), 1.0)",
),
)
end
assignInputs = Expr(:block, assignInputs...)
eval(assignInputs)
eval(code)
eval(Meta.parse("result = $outputSymbol"))
return result
end
function execute(graph::DAG, input::Vector{Particle})
(code, inputSymbols, outputSymbol) = gen_code(graph)