# Is pmap type stable?

**URL:** <https://discourse.julialang.org/t/is-pmap-type-stable/25450>\
**Category:** General Usage\
**Created:** [June 19, 2019, 2:57pm UTC](https://discourse.julialang.org/t/is-pmap-type-stable/25450 "2019-06-19T14:57:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [June 19, 2019, 2:57pm UTC](https://discourse.julialang.org/t/is-pmap-type-stable/25450/1 "2019-06-19T14:57:17Z")

</div>

Using `@code_warntype` on `pmap`, this is reported as type unstable, is this true ? Is there a way to make it type stable ?

```julia
julia> using Distributed, BenchmarkTools
julia> a = rand(1:35,100) # Int64[100]
julia> addprocs(3)
julia> @everywhere function f(n)
             if n == 0 return 0 end
             if n == 1 return 1 end
             return f(n-1) + f(n-2)
         end
julia> @benchmark results = map(f,a) # serialised: median time: 371 ms
julia> @benchmark results = pmap(f,a) # parallelised: median time: 157 ms
julia> map(f,a) # Int64[100]
julia> pmap(f,a) # Int64[100]
julia> @code_warntype map(f,a)
Body::Array{Int64,1}
1 ─ %1 = %new(Base.Generator{Array{Int64,1},typeof(f)}, f, A)::Base.Generator{Array{Int64,1},typeof(f)}
│ %2 = invoke Base._collect(_3::Array{Int64,1}, %1::Base.Generator{Array{Int64,1},typeof(f)}, $(QuoteNode(Base.EltypeUnknown()))::Base.EltypeUnknown, $(QuoteNode(Base.HasShape{1}()))::Base.HasShape{1})::Array{Int64,1}
└── return %2
julia> @code_warntype pmap(f,a)
Body::Any
1 ─ %1 = invoke Distributed.default_worker_pool()::Union{Nothing, WorkerPool}
│ %2 = Distributed.pmap::typeof(pmap)
│ %3 = (isa)(%1, WorkerPool)::Bool
└── goto #3 if not %3
2 ─ %5 = π (%1, WorkerPool)
│ %6 = invoke %2(_2::Function, %5::WorkerPool, _3::Array{Int64,1})::Any
└── goto #6
3 ─ %8 = (isa)(%1, Nothing)::Bool
└── goto #5 if not %8
4 ─ %10 = invoke Distributed.:(#pmap#226)($(QuoteNode(Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}()))::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, Distributed.pmap::Function, f::Function, nothing::Nothing, _3::Array{Int64,1})::Any
└── goto #6
5 ─ (Core.throw)(ErrorException("fatal error in type inference (type bound)"))
└── $(Expr(:unreachable))
6 ┄ %14 = φ (#2 => %6, #4 => %10)::Any
└── goto #7
7 ─ return %14

```

---

<div class="post-metadata">

**Author:** ![kaslusimoes](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kaslusimoes](https://discourse.julialang.org/u/kaslusimoes)\
**Post date:** [July 16, 2020, 9:57pm UTC](https://discourse.julialang.org/t/is-pmap-type-stable/25450/2 "2020-07-16T21:57:03Z")

</div>

Hi @sylvaticus, did you find any solution to your problem?

I might be facing something similar, but am not sure it’s my code’s fault or something else. I think the below is a MWE for what I’m facing, if more people at the community might have a suggestion

```julia
f(x::Float64) = [x, x^2, x^3]

function try_type(ini::Float64, final::Float64)
    u = pmap(x -> f(x), ini:1:final)
    u = reduce(hcat, u)

    return u
end

function try_type2(ini::Float64, final::Float64)
    u = pmap(x -> f(x), ini:1:final)
    v = reduce(hcat, u)

    return v
end

@code_warntype try_type(1., 3.)
# Variables
# #self#::Core.Compiler.Const(try_type, false)
# ini::Float64
# final::Float64
# #52::var"#52#53"
# u::Any
# 
# Body::Any
# 1 ─ (#52 = %new(Main.:(var"#52#53")))
# │ %2 = #52::Core.Compiler.Const(var"#52#53"(), false)
# │ %3 = (ini:1:final)::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
# │ (u = Main.pmap(%2, %3))
# │ (u = Main.reduce(Main.hcat, u::Array{T,N} where T where N))
# └── return u

@code_warntype try_type2(1., 3.)
# Variables
# #self#::Core.Compiler.Const(try_type2, false)
# ini::Float64
# final::Float64
# #54::var"#54#55"
# u::Array{T,N} where T where N
# v::Any
# 
# Body::Any
# 1 ─ (#54 = %new(Main.:(var"#54#55")))
# │ %2 = #54::Core.Compiler.Const(var"#54#55"(), false)
# │ %3 = (ini:1:final)::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
# │ (u = Main.pmap(%2, %3))
# │ (v = Main.reduce(Main.hcat, u))
# └── return v

```
