JuliaSymbolics / SymbolicUtils.jl

Symbolic expressions, rewriting and simplification
https://docs.sciml.ai/SymbolicUtils/stable/
Other
536 stars 107 forks source link

`simplify_fractions` with `@threads` gives `Bijection` errors #408

Open jkosata opened 2 years ago

jkosata commented 2 years ago

MWE using 2 threads:

@variables a,b
expr = ((a+b + (a-b)/a)/a)^10;

for j in 1:10000
    Threads.@threads for i in 1:10
        simplify_fractions(expand(expr))
    end
end

gives

TaskFailedException

    nested task error: One of x or y already in this Bijection

Stacktrace:
      [1] error(s::String)
        @ Base ./error.jl:33
      [2] setindex!(b::Bijections.Bijection{Any, Any}, y::SymbolicUtils.Sym{Real, Base.ImmutableDict{DataType, Any}}, x::DynamicPolynomials.PolyVar{true})
        @ Bijections ~/.julia/packages/Bijections/krqKv/src/Bijections.jl:74
      [3] polyize(x::SymbolicUtils.Sym{Real, Base.ImmutableDict{DataType, Any}}, pvar2sym::Bijections.Bijection{Any, Any}, sym2term::Dict{SymbolicUtils.Sym, Any}, vtype::Type, pow::Function, Fs::Type, recurse::Bool)
        @ SymbolicUtils ~/.julia/packages/SymbolicUtils/fgHzN/src/polyform.jl:150

The threaded loop needs to be ran many times for the error to appear. In the code I am using where the expressions are much larger, the error appears much more often.

shashi commented 2 years ago

This code is not thread safe! But shouldn't be hard to make it.

jkosata commented 2 years ago

I suspected that might be the case, but it's not obvious to me why since neither expand or simplify_fractions mutate its argument.

shashi commented 2 years ago

PolyForm uses some global state to reuse DynamicPolynomials variables.