# Parallel independent loops in julia

**URL:** <https://discourse.julialang.org/t/parallel-independent-loops-in-julia/52737>\
**Category:** Performance\
**Tags:** parallel\
**Created:** [January 2, 2021, 3:39pm UTC](https://discourse.julialang.org/t/parallel-independent-loops-in-julia/52737 "2021-01-02T15:39:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nishan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nishan/32/37958_2.png) [@Nishan](https://discourse.julialang.org/u/Nishan)\
**Post date:** [January 2, 2021, 3:39pm UTC](https://discourse.julialang.org/t/parallel-independent-loops-in-julia/52737/1 "2021-01-02T15:39:39Z")

</div>

Hello everyone, I am new to julia and currently I am converting my tensor networks codes from python to julia and till now it seems great and I could do it without much pain and just by rewriting it in julia I have gained some significant speedup. Now ,I want to parallelize it. For the starter I want to use simple form of parallelism where I have a function that takes some parameters and does some heavy calculations strictly in serial (DMRG and TDVP) and gives the output which is an MPS. I want to run this this functions for several parameters which are completely independent runs. I saw julia has @threads macro for these kind of problems but on a quick search I also found other methods like Floops.jl, ThreadX.jl etc. I am looking forward to suggestions from people who have been through similar cases. Thank you.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 2, 2021, 4:22pm UTC](https://discourse.julialang.org/t/parallel-independent-loops-in-julia/52737/2 "2021-01-02T16:22:48Z")

</div>

Check out [GitHub - mcabbott/Tullio.jl: ⅀](https://github.com/mcabbott/Tullio.jl) for easy but performant, threaded, Tensor operations.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 2, 2021, 4:48pm UTC](https://discourse.julialang.org/t/parallel-independent-loops-in-julia/52737/3 "2021-01-02T16:48:39Z")

</div>

Another option for running multiple independent executions is the `@spawn` macro.

I also like the [ThreadPools](https://github.com/tro3/ThreadPools.jl) package a lot, it allows more control over the parallelization (but it seems that the developer is writing something exactly now, so I will leave it here 🙂 ).

---

<div class="post-metadata">

**Author:** ![tro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tro3/32/12355_2.png) [@tro3](https://discourse.julialang.org/u/tro3)\
**Post date:** [January 2, 2021, 4:54pm UTC](https://discourse.julialang.org/t/parallel-independent-loops-in-julia/52737/4 "2021-01-02T16:54:41Z")

</div>

For what you are describing, running a function with batches of differing parameters, `Threads.@threads` should be fine.

```julia
params = [
    (a1, b1),
    (a2, b2),
]
results = Vector(undef, length(params))
@threads for (i, (a,b)) in collect(enumerate(params))
    results[i] = f(a,b)
end

```

If the jobs are nonuniform, you might get a speedup with the queued scheduling in the [ThreadPools.jl](https://github.com/tro3/ThreadPools.jl) package.

```julia
using ThreadPools

as = [a1, a2]
bs = [b1, b2]
results = qmap(f, as, bs)

```

---

<div class="post-metadata">

**Author:** ![tro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tro3/32/12355_2.png) [@tro3](https://discourse.julialang.org/u/tro3)\
**Post date:** [January 2, 2021, 5:09pm UTC](https://discourse.julialang.org/t/parallel-independent-loops-in-julia/52737/5 "2021-01-02T17:09:27Z")

</div>

Alternately, BTW, if you want to keep the params grouped by run, you can always:

```julia
using ThreadPools

params = [
    (a1, b1),
    (a2, b2),
]
results = qmap(x->f(x...), params)

```

---

<div class="post-metadata">

**Author:** ![Nishan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nishan/32/37958_2.png) [@Nishan](https://discourse.julialang.org/u/Nishan)\
**Post date:** [January 3, 2021, 2:55pm UTC](https://discourse.julialang.org/t/parallel-independent-loops-in-julia/52737/6 "2021-01-03T14:55:07Z")

</div>

Thank you, my jobs are fairly uniform so @threads should be fine.
