# IntervalRootFinding -- Multithreading causes random errors?

**URL:** <https://discourse.julialang.org/t/intervalrootfinding-multithreading-causes-random-errors/97247>\
**Category:** Optimization (Mathematical)\
**Tags:** multithreading, rootfinding\
**Created:** [April 8, 2023, 11:54am UTC](https://discourse.julialang.org/t/intervalrootfinding-multithreading-causes-random-errors/97247 "2023-04-08T11:54:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shmuley95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shmuley95/32/33249_2.png) [@Shmuley95](https://discourse.julialang.org/u/Shmuley95)\
**Post date:** [April 8, 2023, 11:54am UTC](https://discourse.julialang.org/t/intervalrootfinding-multithreading-causes-random-errors/97247/1 "2023-04-08T11:54:17Z")

</div>

I have found some erratic behaviour for the [IntervalRootFinding.jl](https://github.com/JuliaIntervals/IntervalRootFinding.jl) package when using multithreading. Specifically, when using multiple threads, the `roots` interface will sometimes return either false positives for non-existing roots, or fail to identify roots. Moreover, the errors vary at every run.

The minimum working example below repeatedly looks for roots on an interval without roots, and then counts the number of unique roots found. It should always return 1. When using one thread, everything is fine, but as soon as I run the same code with multiple threads, random errors start creeping up.

```julia
using IntervalRootFinding
let
	results = []
	Threads.@threads for iter in 1:1000	
		f(x) = (x^-1)-1
		result = roots(f, 0.0001..10000)
		push!(results, result)
	end
	length(unique(results))
end

```

Am I doing something wrong, or is this an issue within the package? I’ve tried this on multiple computers, and the problem already emerges when starting the REPL with multiple cores.

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [April 8, 2023, 2:31pm UTC](https://discourse.julialang.org/t/intervalrootfinding-multithreading-causes-random-errors/97247/2 "2023-04-08T14:31:48Z")

</div>

Maybe `IntervalRootFinding.jl` is not threads safe?  
I skimmed the documentation and nothing struck me.

You may need to implement a parallelization strategy  
using the API.

A PR for that or a documentation clarification would likely  
be welcome.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [April 8, 2023, 2:45pm UTC](https://discourse.julialang.org/t/intervalrootfinding-multithreading-causes-random-errors/97247/3 "2023-04-08T14:45:29Z")

</div>

I do not know about the details of your issue, but I saw that you push inside a threaded loop. I thought that pushing was not thread safe ? Could you try again with preassigned indices on the result array instead of pushing ?

---

<div class="post-metadata">

**Author:** ![Shmuley95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shmuley95/32/33249_2.png) [@Shmuley95](https://discourse.julialang.org/u/Shmuley95)\
**Post date:** [April 8, 2023, 3:20pm UTC](https://discourse.julialang.org/t/intervalrootfinding-multithreading-causes-random-errors/97247/4 "2023-04-08T15:20:46Z")

</div>

The problem persists

```julia
using IntervalRootFinding
let
	results = Array{Any}(undef,1000)
	Threads.@threads for iter in 1:1000	
		f(x) = (x^-1)-1
		result = roots(f, 0.0001..10000)
		results[iter] = result
	end
	length(unique(results))
end

```

---

<div class="post-metadata">

**Author:** ![Shmuley95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shmuley95/32/33249_2.png) [@Shmuley95](https://discourse.julialang.org/u/Shmuley95)\
**Post date:** [April 8, 2023, 3:23pm UTC](https://discourse.julialang.org/t/intervalrootfinding-multithreading-causes-random-errors/97247/5 "2023-04-08T15:23:51Z")

</div>

I am indeed afraid that `IntervalRootFinding.jl` is not threads safe, but I can neither figure out why, nor how to apply parallelisation in a safe alternative way.
