# Can I distribute several tasks over several workers?

**URL:** <https://discourse.julialang.org/t/can-i-distribute-several-tasks-over-several-workers/64659>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 14, 2021, 8:39pm UTC](https://discourse.julialang.org/t/can-i-distribute-several-tasks-over-several-workers/64659 "2021-07-14T20:39:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 14, 2021, 8:39pm UTC](https://discourse.julialang.org/t/can-i-distribute-several-tasks-over-several-workers/64659/1 "2021-07-14T20:39:50Z")

</div>

Hello,  
How can I distribute several tasks on several workers? Such as there are three tasks below (in this case , the three tasks are writing on the same variable Y):

```julia
x1=[1 3 4 5];
x2=[1 3 4 5];
x3=[1 3 4 5];
Y=zeros(4,4);

#Task-1
    if ~isempty(x1)
       for i in 1:size(x1,2)
           Y[i,i] += x1[i];
       end
    end

#Task-2
    if ~isempty(x2)
       for i in 1:size(x2,2)
           Y[i,i] += x2[i];
       end
    end

#Task-3
    if ~isempty(x3)
       for i in 1:size(x3,2)
           Y[i,i] += x3[i];
       end
    end

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [July 14, 2021, 8:58pm UTC](https://discourse.julialang.org/t/can-i-distribute-several-tasks-over-several-workers/64659/2 "2021-07-14T20:58:33Z")

</div>

Avoid the data race and partition ranges of i.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 14, 2021, 9:02pm UTC](https://discourse.julialang.org/t/can-i-distribute-several-tasks-over-several-workers/64659/3 "2021-07-14T21:02:58Z")

</div>

Actually, in this case the range of i is from 1:4 for x1, x2, and x3, but in other cases in my code it is different so the partition of i ranges is hard to achieve.  
If there is no common variable Y between the tasks, what is the possible way to run a task (set of lines) on a worker?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [July 14, 2021, 10:51pm UTC](https://discourse.julialang.org/t/can-i-distribute-several-tasks-over-several-workers/64659/4 "2021-07-14T22:51:22Z")

</div>

> [@Amro](#):
>
> the three tasks are writing on the same variable Y

I was just trying to deter you from attempting to write to the same variable. Otherwise, it sounds like you are asking about basic distributed computing. See

[https://docs.julialang.org/en/v1/manual/distributed-computing/](https://docs.julialang.org/en/v1/manual/distributed-computing/)  
[Distributed Computing · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Distributed/#Distributed.remotecall-Tuple%7BAny,%20Integer,%20Vararg%7BAny,%20N%7D%20where%20N)}

and particularly:

[https://docs.julialang.org/en/v1/manual/distributed-computing/#Data-Movement](https://docs.julialang.org/en/v1/manual/distributed-computing/#Data-Movement)

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 14, 2021, 11:54pm UTC](https://discourse.julialang.org/t/can-i-distribute-several-tasks-over-several-workers/64659/6 "2021-07-14T23:54:08Z")

</div>

Noted,  
So, if I want to parallelize the below code,

```julia
x1=[1 3 4 5];
x2=[1 3 4 5];
Y=zeros(4,4);

#Task-1
    if ~isempty(x1)
       for i in 1:size(x1,2)
           Y[i,i] += x1[i];
       end
    end
#Task-2
    if ~isempty(x2)
       for i in 1:size(x2,2)
           Y[i,i] += x2[i];
       end
    end

```

Can I write the below one?

```julia
$ ./julia -p 2
@everywhere x1=[1 3 4 5];
@everywhere x2=[1 3 4 5];
Y=zeros(4,4);

#Task-1
s1 = @spawnat 1 begin
r[i,i]=zeros(2,2);
    if ~isempty(x1)
       for i in 1:2
           r[i,i] += x1[i];
       end
    end
end
#Task-2
s2 = @spawnat 2 begin
r[i,i]=zeros(2,2);
    if ~isempty(x2)
       for i in 3:4
           r[i,i] += x2[i];
       end
    end
end
Y[1:2,1:2]= fetch(s1)
Y[3:4,3:4]= fetch(s2)

```
