为什么map(fib,array)是类型稳定的,为什么pmap(fib,array)会导致类型不稳定?

时间:2019-06-17 14:31:35

标签: parallel-processing julia pmap

我正在玩pmap,但是注意到它会转换我的类型稳定的Fibonacci函数(递归版本)为不稳定类型。。想知道为什么以及我做错什么了

julia> using BenchmarkTools
julia> using Distributed
julia> a = rand(1:35,100)
julia> addprocs(3)
julia> @everywhere function fib(n)
         if n == 0 return 0 end
         if n == 1 return 1 end
         return fib(n-1) + fib(n-2)
       end
julia> @code_warntype fib(35) # Body::Int64
julia> @code_warntype map(fib,a)
Body::Array{Int64,1}
1 ─ %1 = %new(Base.Generator{Array{Int64,1},typeof(fib)}, fib, A)::Base.Generator{Array{Int64,1},typeof(fib)}
│   %2 = invoke Base._collect(_3::Array{Int64,1}, %1::Base.Generator{Array{Int64,1},typeof(fib)}, $(QuoteNode(Base.EltypeUnknown()))::Base.EltypeUnknown, $(QuoteNode(Base.HasShape{1}()))::Base.HasShape{1})::Array{Int64,1}
└──      return %2
julia> @code_warntype pmap(fib,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, fib::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

0 个答案:

没有答案