# Help with parallelizing indirect inference estimation

**URL:** <https://discourse.julialang.org/t/help-with-parallelizing-indirect-inference-estimation/64487>\
**Category:** Performance\
**Tags:** parallel\
**Created:** [July 12, 2021, 6:33am UTC](https://discourse.julialang.org/t/help-with-parallelizing-indirect-inference-estimation/64487 "2021-07-12T06:33:25Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [July 28, 2021, 8:31am UTC](https://discourse.julialang.org/t/help-with-parallelizing-indirect-inference-estimation/64487/8 "2021-07-28T08:31:23Z")

</div>

I played around with this a bit. If I set M=1000, then I can get about a 30-40% speedup using 2 threads versus one. Using more threads does not help. My belief is that garbage collection interferes with performance using threads, as, from what I have read, any garbage collection will stop all threads. So, with too many threads, the probability of all of them getting stopped goes up. I have found in some experimentation that MPI gives good performance, see [An embarrassingly parallel problem: threads or MPI?](https://discourse.julialang.org/t/an-embarrassingly-parallel-problem-threads-or-mpi/48049). To limit garbage collection when using threads, trying to avoid allocations is a good strategy, I believe.

The code I used for threading for this problem is

```julia
# indirect inference objective function
function iiobj(β, θ, x, u0, M)
    ys = simulate(x, β, u0, M)
    m = zeros(size(x,2))
    Threads.@threads for i in 1:M
    #for i in 1:M
    m .+= ols(x,ys[i])
    end
    m = m ./ M
    return sum((θ - m).^2) 
end

```

For optimization, I set the criterion using an anonymous function:

```julia
obj = β -> iiobj(β, θ, x, u0, M)  

```

---

_[View the full topic](https://discourse.julialang.org/t/help-with-parallelizing-indirect-inference-estimation/64487)._
