Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

How to write type-stable dataflow graphs

$
0
0

I’m trying to implement a dataflow graph for basic arithmetic and encountering type instability for all but trivially simple graphs. How to rewrite this in a type-stable way?

module Inst # for "type-instability" 
import Base.:* 

abstract type Op end # trait; indicates the arithmetic operation performed 
struct Sum <: Op end 
struct Prod <: Op end 

struct Node{S<:Op, C} # a node in the graph; C is the type of the children. 
    children::Vector{C} 
end 
Node(T::Type{S}, children::Vector{C}) where {S<:Op, C}= Node{T, C}(children) 
*(g::Node{S, C}, xx) where {S, C}= Node{S,C}([xx*tt for tt in g.children])
*(xx, g::Node)= *(g, xx) 

# apply executes the operation 
apply(g::Node{S, C}) where {S <: Op,C}= apply(S(), g) 
apply(::Sum, g)= sum(apply.(g.children)) 
apply(::Prod, g)= prod(apply.(g.children)) # recurse apply into children 
apply(x::T) where {T<:Number}= x # evaluate leaf-nodes of graph 
end 

using .Inst 
# verify correctness of implementation...
s= Inst.Node(Inst.Sum, [1,2])
@show Inst.apply(s) 
p= Inst.Node(Inst.Prod, [3,4])
@show Inst.apply(p) 
g= Inst.Node(Inst.Prod, [s, p])
@show Inst.apply(g) 

@code_warntype Inst.apply(s) # type stable 
@code_warntype Inst.apply(p) # type stable 
@code_warntype Inst.apply(g) #<-- why is there type instability here and how to do it better? 

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles