timholy / ComputationalResources.jl

Julia package for selecting hardware, libraries, and algorithms for a computation
Other
34 stars 5 forks source link

erratic perf using multithreading #18

Closed wizebt closed 4 years ago

wizebt commented 4 years ago

This is a duplicate from counter perf in ImageFiltering. using ImageFiltering, BenchmarkTools, ComputationalResources Test program to reproduce

using ImageFiltering, BenchmarkTools, ComputationalResources

function test_imfilter(; opt=1)
    gk = Kernel.gaussian(2.0f0)
    I = rand(Float32,1000 ,1000)
    println("julia_num_threads=$(Threads.nthreads())")

    for i = 1:9
        start = time()
        if opt == 1
            J = imfilter(CPU1(Algorithm.FIR()), I, gk)
        elseif opt == 2
            J = imfilter(I, gk)
        else
            error("Wrong opt value $opt, either 1 (for CPU1), 2 CPUThreads")
        end
        t = round(time() - start, digits=6)
        println("$i imfilter $t sec")
    end
end

Issue occurs when multithreading using ImageView or Gtk

julia> include("test_imfilter.jl")
test_imfilter (generic function with 1 method)

julia> test_imfilter(opt=1)
julia_num_threads=16
1 imfilter 0.020194 sec
2 imfilter 0.028331 sec
3 imfilter 0.020721 sec
4 imfilter 0.020388 sec
5 imfilter 0.02025 sec
6 imfilter 0.022854 sec
7 imfilter 0.016506 sec
8 imfilter 0.017528 sec
9 imfilter 0.017142 sec

julia> test_imfilter(opt=2)
julia_num_threads=16
1 imfilter 0.279932 sec
2 imfilter 0.006712 sec
3 imfilter 0.006302 sec
4 imfilter 0.011404 sec
5 imfilter 0.003632 sec
6 imfilter 0.004423 sec
7 imfilter 0.002673 sec
8 imfilter 0.002693 sec
9 imfilter 0.002655 sec

julia> test_imfilter(opt=2)
julia_num_threads=16
1 imfilter 0.018668 sec
2 imfilter 0.007212 sec
3 imfilter 0.007689 sec
4 imfilter 0.005306 sec
5 imfilter 0.004047 sec
6 imfilter 0.003786 sec
7 imfilter 0.006157 sec
8 imfilter 0.003978 sec
9 imfilter 0.003345 sec

julia> using ImageView
Gtk-Message: 09:00:34.274: Failed to load module "canberra-gtk-module"
Gtk-Message: 09:00:34.274: Failed to load module "canberra-gtk-module"

julia> test_imfilter(opt=2)
julia_num_threads=16
1 imfilter 5.009421 sec
2 imfilter 5.017527 sec
3 imfilter 4.156348 sec
4 imfilter 0.012405 sec
5 imfilter 5.022431 sec
6 imfilter 0.008102 sec
7 imfilter 0.00735 sec
8 imfilter 5.013069 sec
9 imfilter 5.016844 sec

julia> test_imfilter(opt=1)
julia_num_threads=16
1 imfilter 0.03658 sec
2 imfilter 0.01693 sec
3 imfilter 0.01708 sec
4 imfilter 0.017126 sec
5 imfilter 0.019604 sec
6 imfilter 0.019881 sec
7 imfilter 0.016709 sec
8 imfilter 0.017145 sec
9 imfilter 0.016654 sec

In this part, see interrupt trace below

Julia> test_imfilter(opt=2)
julia_num_threads=16
1 imfilter 5.01519 sec
2 imfilter 1.482307 sec
^CERROR: InterruptException:
Stacktrace:
 [1] try_yieldto(::typeof(Base.ensure_rescheduled), ::Base.RefValue{Task}) at ./task.jl:645
 [2] wait() at ./task.jl:710
 [3] wait(::Base.GenericCondition{Base.Threads.SpinLock}) at ./condition.jl:106
 [4] _wait(::Task) at ./task.jl:238
 [5] wait(::Task) at ./task.jl:265
 [6] macro expansion at ./threadingconstructs.jl:69 [inlined]
 [7] _imfilter_tiled_threads!(::CPU1{ImageFiltering.Algorithm.FIRTiled{2}}, ::Array{Float32,2}, ::OffsetArrays.OffsetArray{Float32,2,Array{Float32,2}}, ::OffsetArrays.OffsetArray{Float32,2,Array{Float32,2}}, ::OffsetArrays.OffsetArray{Float32,2,Array{Float32,2}}, ::NoPad{Pad{2}}, ::Array{Tuple{UnitRange{Int64},UnitRange{Int64}},1}, ::Array{Array{Float32,2},1}) at /home/manu/.julia/packages/ImageFiltering/jhBno/src/imfilter.jl:858
...
timholy commented 4 years ago

This is just an "intent-capture" package and it doesn't do anything on its own (check the src/ directory, there's almost no code here). I don't see the issue report in ImageFiltering. I'd transfer this issue to ImageFiltering but GitHub doesn't let me transfer across organization boundaries, so I'm closing here.

timholy commented 4 years ago

xref https://github.com/JuliaImages/ImageFiltering.jl/issues/161