Skip to content

Commit

Permalink
Merge pull request #148 from numericalEFT/computgraph_daniel
Browse files Browse the repository at this point in the history
Update AbstractGraph interface
  • Loading branch information
dcerkoney authored Oct 26, 2023
2 parents d174936 + 2b18b45 commit 050b8a2
Show file tree
Hide file tree
Showing 13 changed files with 830 additions and 356 deletions.
2 changes: 1 addition & 1 deletion src/FeynmanDiagram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export Graph, FeynmanGraph, FeynmanProperties

export isequiv, drop_topology, is_external, is_internal, diagram_type, orders, vertices, topology
export external_legs, external_indices, external_operators, external_labels
export linear_combination, feynman_diagram, propagator, interaction, external_vertex
export multi_product, linear_combination, feynman_diagram, propagator, interaction, external_vertex
# export DiagramType, Interaction, ExternalVertex, Propagator, SelfEnergy, VertexDiag, GreenDiag, GenericDiag

# export standardize_order!
Expand Down
2 changes: 1 addition & 1 deletion src/backend/compiler.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Compilers
using ..ComputationalGraphs
import ..ComputationalGraphs: FeynmanGraph
import ..ComputationalGraphs: id, name, set_name!, operator, subgraphs, subgraph_factors, factor

using ..AbstractTrees
using ..RuntimeGeneratedFunctions
Expand Down
82 changes: 48 additions & 34 deletions src/backend/static.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
function _to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
"""
function to_static(operator::Type, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector)
Returns the static representation of a computational graph node `g` with operator `operator`, subgraphs `subgraphs`, and subgraph factors `subgraph_factors`.
"""
function to_static(operator::Type, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector)
error(
"Static representation for computational graph nodes with operator $(operator) not yet implemented! " *
"Please define a method `to_static(::Type{$(operator)}, subgraphs::$(typeof(subgraphs)), subgraph_factors::$(typeof(subgraph_factors)))`."
)
end

function to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
if length(subgraphs) == 1
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "(g$(subgraphs[1].id)$factor_str)"
Expand All @@ -8,7 +20,7 @@ function _to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{Graph{F,W
end
end

function _to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
if length(subgraphs) == 1
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "(g$(subgraphs[1].id)$factor_str)"
Expand All @@ -34,7 +46,7 @@ function _to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{FeynmanGr
end
end

function _to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
if length(subgraphs) == 1
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "(g$(subgraphs[1].id)$factor_str)"
Expand All @@ -50,36 +62,37 @@ function _to_static(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{Feyn
end

"""
function to_julia_str(graphs::AbstractVector{<:AbstractGraph}; root::AbstractVector{Int}=[g.id for g in graphs], name::String="eval_graph!")
function to_julia_str(graphs::AbstractVector{<:AbstractGraph}; root::AbstractVector{Int}=[id(g) for g in graphs], name::String="eval_graph!")
Compile a list of graphs into a string for a julia static function. The function takes two arguments: `root` and `leaf`.
`root` is a vector of the root node ids of the graphs, and `leaf` is a vector of the leaf nodes' weights of the graphs.
"""
function to_julia_str(graphs::AbstractVector{<:AbstractGraph}; root::AbstractVector{Int}=[g.id for g in graphs], name::String="eval_graph!")
function to_julia_str(graphs::AbstractVector{<:AbstractGraph}; root::AbstractVector{Int}=[id(g) for g in graphs], name::String="eval_graph!")
head = "function $name(root::AbstractVector, leaf::AbstractVector)\n "
body = ""
leafidx = 1
inds_visitedleaf = Int[]
inds_visitednode = Int[]
for graph in graphs
for g in PostOrderDFS(graph) #leaf first search
target = "g$(g.id)"
g_id = id(g)
target = "g$(g_id)"
isroot = false
if g.id in root
target_root = "root[$(findfirst(x -> x == g.id, root))]"
if g_id in root
target_root = "root[$(findfirst(x -> x == g_id, root))]"
isroot = true
end
if isempty(g.subgraphs) #leaf
g.id in inds_visitedleaf && continue
factor_str = g.factor == 1 ? "" : " * $(g.factor)"
if isempty(subgraphs(g)) #leaf
g_id in inds_visitedleaf && continue
factor_str = factor(g) == 1 ? "" : " * $(factor(g))"
body *= " $target = leaf[$leafidx]$factor_str\n "
leafidx += 1
push!(inds_visitedleaf, g.id)
push!(inds_visitedleaf, g_id)
else
g.id in inds_visitednode && continue
factor_str = g.factor == 1 ? "" : " * $(g.factor)"
body *= " $target = $(_to_static(g.operator, g.subgraphs, g.subgraph_factors))$factor_str\n "
push!(inds_visitednode, g.id)
g_id in inds_visitednode && continue
factor_str = factor(g) == 1 ? "" : " * $(factor(g))"
body *= " $target = $(_to_static(operator(g), subgraphs(g), subgraph_factors(g)))$factor_str\n "
push!(inds_visitednode, g_id)
end
if isroot
body *= " $target_root = $target\n "
Expand All @@ -91,7 +104,7 @@ function to_julia_str(graphs::AbstractVector{<:AbstractGraph}; root::AbstractVec
end

"""
function to_julia_str(graphs::AbstractVector{<:AbstractGraph}, leafMap::Dict{Int,Int}; root::AbstractVector{Int}=[g.id for g in graphs],
function to_julia_str(graphs::AbstractVector{<:AbstractGraph}, leafMap::Dict{Int,Int}; root::AbstractVector{Int}=[id(g) for g in graphs],
name::String="eval_graph!")
Compile a list of Feynman graphs into a string for a julia static function. The complied function takes two arguments: `root` and `leafVal`.
Expand All @@ -100,33 +113,34 @@ Compile a list of Feynman graphs into a string for a julia static function. The
# Arguments:
- `graphs` (AbstractVector{G}): The vector object representing the Feynman graphs,
- `leafMap (Dict{Int,Int})`: The mapping dictionary from the id of each leaf to the index of the leaf weight's table `leafVal`.
- `root` (AbstractVector{Int}, optional): The vector of the root node ids of the graphs (defaults to `[g.id for g in graphs]`).
- `root` (AbstractVector{Int}, optional): The vector of the root node ids of the graphs (defaults to `[id(g) for g in graphs]`).
- `name` (String,optional): The name of the complied function (defaults to `"eval_graph!"`).
"""
function to_julia_str(graphs::AbstractVector{<:AbstractGraph}, leafMap::Dict{Int,Int}; root::AbstractVector{Int}=[g.id for g in graphs],
function to_julia_str(graphs::AbstractVector{<:AbstractGraph}, leafMap::Dict{Int,Int}; root::AbstractVector{Int}=[id(g) for g in graphs],
name::String="eval_graph!")
head = "function $name(root::AbstractVector, leafVal::AbstractVector)\n "
body = ""
inds_visitedleaf = Int[]
inds_visitednode = Int[]
for graph in graphs
for g in PostOrderDFS(graph) #leaf first search
target = "g$(g.id)"
g_id = id(g)
target = "g$(g_id)"
isroot = false
if g.id in root
target_root = "root[$(findfirst(x -> x == g.id, root))]"
if g_id in root
target_root = "root[$(findfirst(x -> x == g_id, root))]"
isroot = true
end
if isempty(g.subgraphs) #leaf
g.id in inds_visitedleaf && continue
factor_str = g.factor == 1 ? "" : " * $(g.factor)"
body *= " $target = leafVal[$(leafMap[g.id])]$factor_str\n "
push!(inds_visitedleaf, g.id)
if isempty(subgraphs(g)) #leaf
g_id in inds_visitedleaf && continue
factor_str = factor(g) == 1 ? "" : " * $(factor(g))"
body *= " $target = leafVal[$(leafMap[g_id])]$factor_str\n "
push!(inds_visitedleaf, g_id)
else
g.id in inds_visitednode && continue
factor_str = g.factor == 1 ? "" : " * $(g.factor)"
body *= " $target = $(_to_static(g.operator, g.subgraphs, g.subgraph_factors))$factor_str\n "
push!(inds_visitednode, g.id)
g_id in inds_visitednode && continue
factor_str = factor(g) == 1 ? "" : " * $(factor(g))"
body *= " $target = $(_to_static(operator(g), subgraphs(g), subgraph_factors(g)))$factor_str\n "
push!(inds_visitednode, g_id)
end
if isroot
body *= " $target_root = $target\n "
Expand All @@ -138,7 +152,7 @@ function to_julia_str(graphs::AbstractVector{<:AbstractGraph}, leafMap::Dict{Int
end

"""
function compile(graphs::AbstractVector{<:AbstractGraph}; root::AbstractVector{Int}=[g.id for g in graphs])
function compile(graphs::AbstractVector{<:AbstractGraph}; root::AbstractVector{Int}=[id(g) for g in graphs])
Compile a list of graphs into a julia static function.
The function takes two arguments: `root` and `leaf`. `root` is a vector of the root node ids of the graphs, and `leaf` is a vector of the leaf node ids of the graphs.
Expand All @@ -160,15 +174,15 @@ leaf = [1.0, 2.0]
```
"""
function compile(graphs::AbstractVector{<:AbstractGraph};
root::AbstractVector{Int}=[g.id for g in graphs])
root::AbstractVector{Int}=[id(g) for g in graphs])
# this function return a runtime generated function defined by compile()
func_string = to_julia_str(graphs; root=root, name="func_name!")
func_expr = Meta.parse(func_string)
return @RuntimeGeneratedFunction(func_expr)
end

function compile(graphs::AbstractVector{<:AbstractGraph}, leafMap::Dict{Int,Int};
root::AbstractVector{Int}=[g.id for g in graphs])
root::AbstractVector{Int}=[id(g) for g in graphs])
# this function return a runtime generated function defined by compile()
func_string = to_julia_str(graphs, leafMap; root=root, name="func_name!")
func_expr = Meta.parse(func_string)
Expand Down
3 changes: 2 additions & 1 deletion src/computational_graph/ComputationalGraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ export unary_istrivial, isassociative, isequiv

include("graph.jl")
include("feynmangraph.jl")
include("conversions.jl")

export Graph, FeynmanGraph, FeynmanProperties
# export DiagramType

export isequiv, drop_topology, is_external, is_internal, diagram_type, orders, vertices, topology
export external_legs, external_indices, external_operators, external_labels
export linear_combination, feynman_diagram, propagator, interaction, external_vertex
export multi_product, linear_combination, feynman_diagram, propagator, interaction, external_vertex

# export Prod, Sum
# export DiagramType, Interaction, ExternalVertex, Propagator, SelfEnergy, VertexDiag, GreenDiag, GenericDiag
Expand Down
Loading

0 comments on commit 050b8a2

Please sign in to comment.