# How to do \`@parallel for i = 1:n, j = 1:m\` like thing?

**URL:** <https://discourse.julialang.org/t/how-to-do-parallel-for-i-1-n-j-1-m-like-thing/7871>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 20, 2017, 5:23am UTC](https://discourse.julialang.org/t/how-to-do-parallel-for-i-1-n-j-1-m-like-thing/7871 "2017-12-20T05:23:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)\
**Post date:** [December 20, 2017, 5:23am UTC](https://discourse.julialang.org/t/how-to-do-parallel-for-i-1-n-j-1-m-like-thing/7871/1 "2017-12-20T05:23:51Z")

</div>

I want to use multidimensional SharedArray to calculate in parallel.  
From the documents ([https://docs.julialang.org/en/stable/manual/parallel-computing/](https://docs.julialang.org/en/stable/manual/parallel-computing/)), the following code calculates in parallel for 1-dimensional array:

```julia
a = SharedArray{Float64}(10)
@parallel for i = 1:10
    a[i] = i
end

```

How can I do this for 2-dimensional array like the following?

```julia
# this is not runnable
a = SharedArray{Float64}(10, 5)
@parallel for i = 1:10, j = 1:5
    a[i, j] = i + j
end

```

---

<div class="post-metadata">

**Author:** ![benelsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benelsen/32/1620_2.png) [@benelsen](https://discourse.julialang.org/u/benelsen)\
**Post date:** [December 20, 2017, 6:06pm UTC](https://discourse.julialang.org/t/how-to-do-parallel-for-i-1-n-j-1-m-like-thing/7871/2 "2017-12-20T18:06:46Z")

</div>

The simplest method I know of is

```julia
a = SharedArray{Float64}(10, 5)
@parallel for k in eachindex(a)
    i, j = ind2sub(a, k)
    a[k] = i + j
    # or: a[i, j] = i + j
end

```

If you don’t need the subscripts themselves you can (obviously) simply do

```julia
a = SharedArray{Float64}(10, 5)
@parallel for k in eachindex(a)
    a[k] = k
end

```

In the upcoming 0.7 (didn’t work in 0.6 for me) you can use @parallel with CartesianIndices:

```julia
a = SharedArray{Float64}(10, 5)
r = CartesianIndices(size(a))
@parallel for k in r
    i, j = k.I
    a[k] = i + j
end

```

---

<div class="post-metadata">

**Author:** ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)\
**Post date:** [December 20, 2017, 8:52pm UTC](https://discourse.julialang.org/t/how-to-do-parallel-for-i-1-n-j-1-m-like-thing/7871/3 "2017-12-20T20:52:59Z")

</div>

I didn’t know about ind2sub. Thanks for your help!

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [December 20, 2017, 9:02pm UTC](https://discourse.julialang.org/t/how-to-do-parallel-for-i-1-n-j-1-m-like-thing/7871/4 "2017-12-20T21:02:59Z")

</div>

Depending on the dimensions of your shared array, you might consider nested for loops:

```julia
a = SharedArray{Float64}(10, 5)
@parallel for i = 1:10
    for j = 1:5
        a[i, j] = i + j
    end
end

```

This partitions the outer loop across workers, and doesn’t require `ind2sub` or equivalent.

---

<div class="post-metadata">

**Author:** ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)\
**Post date:** [December 20, 2017, 9:07pm UTC](https://discourse.julialang.org/t/how-to-do-parallel-for-i-1-n-j-1-m-like-thing/7871/5 "2017-12-20T21:07:39Z")

</div>

Can this partition the inner loop? I want to partition for each (i, j) and I need to specify (i, j).

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [December 20, 2017, 9:25pm UTC](https://discourse.julialang.org/t/how-to-do-parallel-for-i-1-n-j-1-m-like-thing/7871/6 "2017-12-20T21:25:43Z")

</div>

> [@paalon](#):
>
> Can this partition the inner loop? I want to partition for each (i, j) and I need to specify (i, j).

No. (But not really sure what you mean here)

The outer loop will be partitioned across the workers.  
So in this example (10xN array), say you have 5 workers. Then each worker will process 2 iterations of the outer loop (i.e. each worker will execute the full inner loop twice)  
In general this should work if (outer loop dimension) \> (number of workers)

---

<div class="post-metadata">

**Author:** ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)\
**Post date:** [February 5, 2018, 10:03am UTC](https://discourse.julialang.org/t/how-to-do-parallel-for-i-1-n-j-1-m-like-thing/7871/7 "2018-02-05T10:03:43Z")

</div>

```julia
# parallel calculation

@everywhere function test(i, j)
    sleep(0.01)
    println(i, j)
end

@everywhere function test1(i, m)
    @sync @parallel for j = 1:m
        test(i, j)
    end
end

function test_series(n, m)
    for i = 1:n
        for j = 1:m
            test(i, j)
        end
    end
end

function test_parallel_single_loop(n, m)
    @sync @parallel for i = 1:n
        for j = 1:m
            test(i, j)
        end
    end
end

function test_parallel_double_loop(n, m)
    @sync @parallel for i = 1:n
        test1(i, m)
    end
end

n = 10
m = 10

function calc()
    @time test_series(n, m)
    @time test_parallel_single_loop(n, m)
    @time test_parallel_double_loop(n, m)
    @time test_series(n, m)
    @time test_parallel_single_loop(n, m)
    @time test_parallel_double_loop(n, m)
end

calc()

```

I mean the difference among them.
