# Parallel for loop without reduction

**URL:** https://discourse.julialang.org/t/parallel-for-loop-without-reduction/3863
**Category:** New to Julia
**Created:** [May 22, 2017, 9:40pm UTC](https://discourse.julialang.org/t/parallel-for-loop-without-reduction/3863 "2017-05-22T21:40:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [May 22, 2017, 9:40pm UTC](https://discourse.julialang.org/t/parallel-for-loop-without-reduction/3863/1 "2017-05-22T21:40:57Z")

</div>

I believe I have a problem using a parallel for loop without a reduction operator/function. I tried the following code, hoping to get a pmap-like behaviour:

```julia
a = @parallel for i in 1:100
      i^2
end;
a

```

But this was the result:

```julia
1-element Array{Future,1}:
 Future(1,1,3,#NULL)

```

Also fetch(a) gives me the same thing:

```julia
1-element Array{Future,1}:
 Future(1,1,3,#NULL)

```

And `fetch(a[1])` sometimes freezes the Julia session and sometimes returns nothing. Please let me know how to use the parallel for loop in a way that mimics pmap.

Also is there a similar abstraction to this for GPUs, i.e. a GPU for loop? I know ArrayFire in C++ offers a gfor loop, but is there something similar which uses CUDANative.jl?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [May 22, 2017, 10:11pm UTC](https://discourse.julialang.org/t/parallel-for-loop-without-reduction/3863/2 "2017-05-22T22:11:41Z")

</div>

Reduce with `(vcat)`.

Why not just use `pmap` with a larger batch size?

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [May 22, 2017, 10:16pm UTC](https://discourse.julialang.org/t/parallel-for-loop-without-reduction/3863/3 "2017-05-22T22:16:42Z")

</div>

Right, I could. I tried reducing with vcat, it worked, but it was super slow. I could use pmap, but I found this example in the documentation and thought it was often more convenient when programming, so I wondered why it doesn’t work.

I think changing this to a pmap of an anonymous function behind the scenes could make it faster than using vcat, maybe worth modifying the source code of @parallel.

---

<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: [May 23, 2017, 4:01am UTC](https://discourse.julialang.org/t/parallel-for-loop-without-reduction/3863/4 "2017-05-23T04:01:45Z")

</div>

Maybe look at `@parallel` with `SharedArray`?

```julia
a = SharedArray(Int,100);
@parallel for i in 1:100
    a[i] = i^2
end;
a

```

[https://docs.julialang.org/en/stable/manual/parallel-computing/#parallel-map-and-loops](https://docs.julialang.org/en/stable/manual/parallel-computing/#parallel-map-and-loops)
