Is pmap type stable?

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

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
1 Like

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

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