Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

substitute is not thread-safe #1447

Open
ysfoo opened this issue Feb 24, 2025 · 2 comments
Open

substitute is not thread-safe #1447

ysfoo opened this issue Feb 24, 2025 · 2 comments

Comments

@ysfoo
Copy link

ysfoo commented Feb 24, 2025

I've ran into errors (including seg faults) when using substitute in a multi-threaded for loop, see e.g.

using Catalyst

t = default_t()
species_vec = @species X1(t) X2(t) X3(t)
@parameters k[1:6]
x_pairs = [(xa, xb) for xa in species_vec, xb in species_vec if xa !== xb]
rx_vec = [
	Reaction(kval, [xa], [xb]) for ((xa, xb), kval) in zip(x_pairs, k)
]

@Threads.threads for _ in 1:1000
    rates = substitute.(oderatelaw.(rx_vec), Ref([k => ones(length(k))]));
    # other code using `rates` omitted
end

As someone who isn't familiar with the internals of Symbolics.jl, my guess is that this is due to the use of parallelism in Symbolics.jl. Is there some way to 'disable' parallelism, or other ways to make this code thread-safe?

Trace:

ERROR: LoadError: TaskFailedException

    nested task error: UndefRefError: access to undefined reference
    Stacktrace:
      [1] getindex
        @ ./essentials.jl:917 [inlined]
      [2] iterate
        @ ./array.jl:902 [inlined]
      [3] iterate
        @ ./generator.jl:45 [inlined]
      [4] collect_to!(dest::Vector{Any}, itr::Base.Generator{Vector{Any}, SymbolicUtils.var"#343#345"{Bool, Dict{SymbolicUtils.BasicSymbolic{Vector{Real}}, Vector{Float64}}}}, offs::Int64, st::Int64)
        @ Base ./array.jl:849
      [5] collect_to!(dest::Vector{SymbolicUtils.BasicSymbolic{Real}}, itr::Base.Generator{Vector{Any}, SymbolicUtils.var"#343#345"{Bool, Dict{SymbolicUtils.BasicSymbolic{Vector{Real}}, Vector{Float64}}}}, offs::Int64, st::Int64)
        @ Base ./array.jl:857
      [6] collect_to_with_first!(dest::Vector{SymbolicUtils.BasicSymbolic{Real}}, v1::SymbolicUtils.BasicSymbolic{Real}, itr::Base.Generator{Vector{Any}, SymbolicUtils.var"#343#345"{Bool, Dict{SymbolicUtils.BasicSymbolic{Vector{Real}}, Vector{Float64}}}}, st::Int64)
        @ Base ./array.jl:827
      [7] _collect(c::Vector{Any}, itr::Base.Generator{Vector{Any}, SymbolicUtils.var"#343#345"{Bool, Dict{SymbolicUtils.BasicSymbolic{Vector{Real}}, Vector{Float64}}}}, ::Base.EltypeUnknown, isz::Base.HasShape{1})
        @ Base ./array.jl:821
      [8] collect_similar
        @ ./array.jl:720 [inlined]
      [9] map
        @ ./abstractarray.jl:3371 [inlined]
     [10] substitute(expr::SymbolicUtils.BasicSymbolic{Real}, dict::Dict{SymbolicUtils.BasicSymbolic{Vector{Real}}, Vector{Float64}}; fold::Bool)
        @ SymbolicUtils ~/.julia/packages/SymbolicUtils/Su6xO/src/substitute.jl:23
     [11] substitute
        @ ~/.julia/packages/SymbolicUtils/Su6xO/src/substitute.jl:16 [inlined]
     [12] (::Symbolics.var"#21#23"{Symbolics.var"#21#22#24"{Dict{SymbolicUtils.BasicSymbolic{Vector{Real}}, Vector{Float64}}}})(expr::SymbolicUtils.BasicSymbolic{Real})
        @ Symbolics ~/.julia/packages/Symbolics/Pl7kR/src/num.jl:93
     [13] substitute(expr::SymbolicUtils.BasicSymbolic{Real}, s::Vector{Pair{Symbolics.Arr{Num, 1}, Vector{Float64}}}; kw::@Kwargs{})
        @ Symbolics ~/.julia/packages/Symbolics/Pl7kR/src/num.jl:79
     [14] _broadcast_getindex_evalf
        @ ./broadcast.jl:678 [inlined]
     [15] _broadcast_getindex
        @ ./broadcast.jl:651 [inlined]
     [16] getindex
        @ ./broadcast.jl:610 [inlined]
     [17] copyto_nonleaf!(dest::Vector{SymbolicUtils.BasicSymbolic{Real}}, bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Tuple{Base.OneTo{Int64}}, typeof(substitute), Tuple{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(oderatelaw), Tuple{Base.Broadcast.Extruded{Vector{Reaction{Any, Int64}}, Tuple{Bool}, Tuple{Int64}}}}, Base.RefValue{Vector{Pair{Symbolics.Arr{Num, 1}, Vector{Float64}}}}}}, iter::Base.OneTo{Int64}, state::Int64, count::Int64)
        @ Base.Broadcast ./broadcast.jl:1082
     [18] copy
        @ ./broadcast.jl:919 [inlined]
     [19] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(substitute), Tuple{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(oderatelaw), Tuple{Vector{Reaction{Any, Int64}}}}, Base.RefValue{Vector{Pair{Symbolics.Arr{Num, 1}, Vector{Float64}}}}}})
        @ Base.Broadcast ./broadcast.jl:872
     [20] macro expansion
        @ .../test.jl:12 [inlined]
     [21] (::var"#18#threadsfor_fun#8"{var"#18#threadsfor_fun#7#9"{UnitRange{Int64}}})(tid::Int64; onethread::Bool)
        @ Main ./threadingconstructs.jl:253
     [22] #18#threadsfor_fun
        @ ./threadingconstructs.jl:220 [inlined]
     [23] (::Base.Threads.var"#1#2"{var"#18#threadsfor_fun#8"{var"#18#threadsfor_fun#7#9"{UnitRange{Int64}}}, Int64})()
        @ Base.Threads ./threadingconstructs.jl:154

...and 3 more exceptions.

Stacktrace:
 [1] threading_run(fun::var"#18#threadsfor_fun#8"{var"#18#threadsfor_fun#7#9"{UnitRange{Int64}}}, static::Bool)
   @ Base.Threads ./threadingconstructs.jl:173
 [2] macro expansion
   @ ./threadingconstructs.jl:190 [inlined]
 [3] top-level scope
   @ .../test.jl:189
in expression starting at .../test.jl:11
@ChrisRackauckas
Copy link
Member

@bowenszhu @AayushSabharwal is this a result of hash consing?

@bowenszhu
Copy link
Member

is this a result of hash consing?

I think so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants