JuliaLang / JuliaLang/Distributed.jl

Type inference fails for `@distributed for` but not for `mapreduce`

Open
#160 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
55
Forks
19
PR merge metrics
No merged PRs in 30d

Description

In some cases, type inference works for mapreduce but fails for @distributed for.

function foo()
    A = Array{Float64}(undef, 10)
    result = @distributed (+) for _ in 1:100
        rand!(A)
        sum(A)
    end
end

function bar()
    A = Array{Float64}(undef, 10)
    result = mapreduce(+, 1:100) do _
        rand!(A)
        sum(A)
    end
end
julia> @code_warntype foo()
Variables
  #self#::Core.Const(foo)
  JuliaLang/julia#272::var"#272#273"{Vector{Float64}}
  result::Any
  A::Vector{Float64}

Body::Any
1 ─ %1 = Core.apply_type(Main.Array, Main.Float64)::Core.Const(Array{Float64, N} where N)
│        (A = (%1)(Main.undef, 10))
│   %3 = Main.:(var"#272#273")::Core.Const(var"#272#273")
│   %4 = Core.typeof(A)::Core.Const(Vector{Float64})
│   %5 = Core.apply_type(%3, %4)::Core.Const(var"#272#273"{Vector{Float64}})
│        (#272 = %new(%5, A))
│   %7 = JuliaLang/julia#272::var"#272#273"{Vector{Float64}}
│   %8 = (1:100)::Core.Const(1:100)
│   %9 = Distributed.preduce(Main.:+, %7, %8)::Any
│        (result = %9)
└──      return %9

julia> @code_warntype bar()
Variables
  #self#::Core.Const(bar)
  JuliaLang/julia#274::var"#274#275"{Vector{Float64}}
  result::Float64
  A::Vector{Float64}

Body::Float64
1 ─ %1 = Core.apply_type(Main.Array, Main.Float64)::Core.Const(Array{Float64, N} where N)
│        (A = (%1)(Main.undef, 10))
│   %3 = Main.:(var"#274#275")::Core.Const(var"#274#275")
│   %4 = Core.typeof(A)::Core.Const(Vector{Float64})
│   %5 = Core.apply_type(%3, %4)::Core.Const(var"#274#275"{Vector{Float64}})
│        (#274 = %new(%5, A))
│   %7 = JuliaLang/julia#274::var"#274#275"{Vector{Float64}}
│   %8 = (1:100)::Core.Const(1:100)
│   %9 = Main.mapreduce(%7, Main.:+, %8)::Float64
│        (result = %9)
└──      return %9

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the foo and bar examples in the report, then compare their @code_warntype output. Start by tracing the @distributed for path through Distributed.preduce and contrast it with mapreduce. Done means the distributed example no longer produces an Any result when the equivalent mapreduce inference is Float64.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
distributed-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.