# Can I use \`pmap\` on a recursive program?

**URL:** <https://discourse.julialang.org/t/can-i-use-pmap-on-a-recursive-program/114684>\
**Category:** General Usage\
**Tags:** question, recursion, parallel-computing\
**Created:** [May 24, 2024, 3:17pm UTC](https://discourse.julialang.org/t/can-i-use-pmap-on-a-recursive-program/114684 "2024-05-24T15:17:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 24, 2024, 3:17pm UTC](https://discourse.julialang.org/t/can-i-use-pmap-on-a-recursive-program/114684/1 "2024-05-24T15:17:53Z")

</div>

Can I use `pmap` on a recursive program? Take the following recursive function as an example:

```julia
using Distributed
addprocs(3)
function mymax(x)
    n = length(x)
    if n == 1
        x[1]
    else
        y = pmap(mymax, [x[1:Int(floor(n/2))], x[Int(floor(n/2))+1:end]])
        maximum(y)
    end
end
mymax(rand(10))

```

![image](https://global.discourse-cdn.com/julialang/original/3X/c/3/c3f46b26bc8a8a898eb2d847a5be00f8f6055312.png)

Since the `myfun` function acts independently on each branch, I want the system to coordinate multiple processes to compute them in a distributed way. Can I? And how?

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 25, 2024, 6:46am UTC](https://discourse.julialang.org/t/can-i-use-pmap-on-a-recursive-program/114684/2 "2024-05-25T06:46:16Z")

</div>

```julia

@everywhere function mymax(x)
    n = length(x)
    if n == 1
        x[1]
    else
        y = fetch.(map([x[1:Int(floor(n/2))], x[Int(floor(n/2))+1:end]]) do z
            @spawnat :any @show mymax(z)
        end)
        maximum(y)
    end
end
mymax(rand(10))

```
