- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.7k
Closed
Labels
bugIndicates an unexpected problem or unintended behaviorIndicates an unexpected problem or unintended behaviorperformanceMust go fasterMust go faster
Milestone
Description
Consider this small example, which @times within a Task, allows the Task to migrate threads, and then shows that the results are garbage when it switches threads:
# compilation-task-migration-17-example.jl
@sync begin
    # Start a long task that yields a lot, and is likely to switch tasks
    # Run a `@time` measurement in there, to report the compilation time.
    Threads.@spawn begin
        @time begin
            Core.println("start thread: ",Threads.threadid())
            for _ in 1:5
                sleep(0.2)  # might switch threads
            end
            @eval module M ; f(x,y) = x+y; end
            @eval M.f(1,2)
            Core.println("end thread: ",Threads.threadid())
        end
    end
    # Start some heavy work to occupy the thread that the above task started on.
    @sync for _ in 1:Threads.nthreads()
        Threads.@spawn for _ in 1:10 peakflops() end
    end
endOne time it reported 0% compilation time, the next time it reports 4327426979.58% compilation time 😅
julia> versioninfo()
Julia Version 1.7.0-beta3
Commit e76c9dad42 (2021-07-07 08:12 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin19.6.0)
  CPU: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.0 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 4
  
julia> include("compilation-task-migration-17-example.jl")
start thread: 1
WARNING: replacing module M.
end thread: 3
  4.352289 seconds (18.97 k allocations: 2.391 GiB, 6.06% gc time)
julia> include("compilation-task-migration-17-example.jl")
start thread: 2
WARNING: replacing module M.
end thread: 3
  4.262751 seconds (18.97 k allocations: 2.391 GiB, 5.35% gc time, 4327426979.58% compilation time)(For completeness, here's the same test on julia 1.6):
julia> versioninfo()
Julia Version 1.6.2
Commit 1b93d53fc4 (2021-07-14 15:36 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin19.6.0)
  CPU: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 4
julia> include("compilation-task-migration-17-example.jl")
start thread: 2
end thread: 2
  5.200354 seconds (3.24 M allocations: 2.563 GiB, 6.75% gc time, 0.33% compilation time)
julia> include("compilation-task-migration-17-example.jl")
start thread: 2
WARNING: replacing module M.
end thread: 2
  5.840806 seconds (18.27 k allocations: 2.391 GiB, 8.02% gc time, 0.05% compilation time)Metadata
Metadata
Assignees
Labels
bugIndicates an unexpected problem or unintended behaviorIndicates an unexpected problem or unintended behaviorperformanceMust go fasterMust go faster