You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a problem that uses multithreading and I've set it up to copy some integrators defining a differential equation problem. I'm running into issues with KLUFactorization(), namely my method fails when KLUFactorization(; reuse_symbolic = true) and is correct when reuse_symbolic = false. Below is an example that shows the issue. I thought deepcopy should duplicate any of the caches used so that there is no communication. As Chris suggests in the discourse thread, it's possible that deepcopy might just be copying a pointer that refers to some memory in C.
using OrdinaryDiffEq, LinearSolve, SparseArrays, Base.Threads
function ode_fnc(du::AbstractVector{T}, u, p, t) where {T}
J, λ = p
fill!(du, zero(T))
for i in eachindex(du)
for j in J[i, :].nzind
du[i] += u[j]
end
du[i] *= λ * u[i]
end
return nothing
end
function sum_integrator_serial(integrator, λ_rng)
reinit!(integrator)
s = 0.0
for λ in λ_rng
integrator.p[2] = λ
solve!(integrator)
for j in eachindex(integrator.sol.u)
s += sum(integrator.sol.u[j])
end
reinit!(integrator)
end
return s
end
function sum_integrator_parallel(integrator, λ_rng)
reinit!(integrator)
nt = Threads.nthreads()
integrators = [deepcopy(integrator) for _ in 1:nt]
s = Threads.Atomic{Float64}(0.0)
Threads.@threads for λ in λ_rng
id = Threads.threadid()
integrators[id].p[2] = λ
solve!(integrators[id])
for j in eachindex(integrators[id].sol.u)
Threads.atomic_add!(s, sum(integrators[id].sol.u[j]))
end
reinit!(integrators[id])
end
return s[]
end
n, p = 200, 0.02
J = sprandn(n,n,p)
u0, tspan = ones(n), (0.0, 0.1)
jac_prototype = deepcopy(J)
ode_fnc_2 = ODEFunction(ode_fnc; jac_prototype)
params = [J, 0.1]
prob = ODEProblem(ode_fnc_2, u0, tspan, params)
alg = TRBDF2(linsolve = KLUFactorization(; reuse_symbolic=false))
integrator = init(prob, alg; saveat = 0.01)
λ_rng = 0.1:0.001:0.5
s_serial = sum_integrator(integrator, λ_rng)
s_parallel = sum_integrator_parallel(integrator, λ_rng)
This gives s_serial and s_parallel to be essentially the same value. But instead if I have reuse_symbolic = true:
This is an issue for my post at https://discourse.julialang.org/t/duplicating-integrator-with-klufactorization-reuse-symbolic-true/91635 which I'll re-paste here.
I have a problem that uses multithreading and I've set it up to copy some integrators defining a differential equation problem. I'm running into issues with
KLUFactorization()
, namely my method fails whenKLUFactorization(; reuse_symbolic = true)
and is correct whenreuse_symbolic = false
. Below is an example that shows the issue. I thoughtdeepcopy
should duplicate any of the caches used so that there is no communication. As Chris suggests in the discourse thread, it's possible thatdeepcopy
might just be copying a pointer that refers to some memory in C.This gives
s_serial
ands_parallel
to be essentially the same value. But instead if I havereuse_symbolic = true
:I get the error above.
The text was updated successfully, but these errors were encountered: