# Parallelizing for loop in the computation of a gradient

**URL:** <https://discourse.julialang.org/t/parallelizing-for-loop-in-the-computation-of-a-gradient/9154>\
**Category:** Performance\
**Tags:** question\
**Created:** [February 19, 2018, 5:50am UTC](https://discourse.julialang.org/t/parallelizing-for-loop-in-the-computation-of-a-gradient/9154 "2018-02-19T05:50:49Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [February 19, 2018, 12:41pm UTC](https://discourse.julialang.org/t/parallelizing-for-loop-in-the-computation-of-a-gradient/9154/7 "2018-02-19T12:41:51Z")

</div>

Regarding `Threads.@threads`, I was playing around with that myself this weekend and was also surprised by slowdowns and huge increases in allocations compared to the non-threaded version. In my case it turned out to be because of the old nemesis, [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276), as `@threads` creates a closure internally.

You should compare the `@code_warntype` for the non-parallelized version to what you get with `Threads.@threads`. If you’re seeing that variable types aren’t properly inferred anymore, the issue is likely to be what I described above. One of the standard workarounds, which worked in my case, is to use a `let` block, as described in [https://github.com/JuliaLang/julia/issues/15276#issuecomment-318598339](https://github.com/JuliaLang/julia/issues/15276#issuecomment-318598339). On 0.6.2 however, part of the issue for me was that one of the variables created inside `@threads` (`range`) is also used in a closure. This has been fixed in master: [https://github.com/JuliaLang/julia/pull/24688](https://github.com/JuliaLang/julia/pull/24688).

---

_[View the full topic](https://discourse.julialang.org/t/parallelizing-for-loop-in-the-computation-of-a-gradient/9154)._
