JuliaImages / ImageView.jl

Interactive display of images and movies
MIT License
136 stars 32 forks source link

Multithreading slow with ImageView #243

Closed wavecast closed 3 years ago

wavecast commented 3 years ago

I find ImageView useful and would like to use it along with multithreading. It seems that using ImageView increases the run time of multithreaded programs signficantly. For example the below MWE runs in under a second when run with a single thread ("julia --threads 1") but can take minutes when run with four threads. There's 0% CPU usage during most of that time. It's not necessary to call any methods in ImageView - just 'using' the package is sufficient.

For an MWE put the following in a file and include() it in the REPL.

using Dates
using ImageView

function f()
end

function spawn_thread()
    task = Threads.@spawn f()
    wait(task)
end

function spawn_threads()
    println("Threads.nthreads() $(Threads.nthreads())")
    println("$(Dates.now())  threadid $(Threads.threadid())")

    start = Dates.now()

    for i in 1:5000
        i % 500 == 0 && println("$(Dates.now())  i $i")

        spawn_thread()
    end

    println("Took $(Dates.canonicalize(Dates.CompoundPeriod( Dates.now() - start)))")
end

spawn_threads()

Here's an example run:

julia-1.5.3 --threads 4
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.5.3 (2020-11-09)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> include("imageview_slows_multithreading.jl")
Threads.nthreads() 4
2021-02-26T12:16:05.712  threadid 1
2021-02-26T12:16:58.847  i 500
2021-02-26T12:17:12.289  i 1000
2021-02-26T12:17:39.865  i 1500
2021-02-26T12:17:45.048  i 2000
2021-02-26T12:17:58.622  i 2500
2021-02-26T12:18:42.654  i 3000
2021-02-26T12:18:52.632  i 3500
2021-02-26T12:18:53.543  i 4000
2021-02-26T12:18:56.081  i 4500
2021-02-26T12:20:22.048  i 5000
Took 4 minutes, 16 seconds, 257 milliseconds

(@v1.5) pkg> status ImageView
Status `~/.julia/environments/v1.5/Project.toml`
  [86fae568] ImageView v0.10.13
RalphAS commented 3 years ago

This looks like a problem with a dependency, Gtk.jl, which has some confusing process management logic. Do you see the same thing with just using Gtk?

timholy commented 3 years ago

https://github.com/JuliaLang/julia/issues/35552

We really need to fix that...

wavecast commented 3 years ago

This looks like a problem with a dependency, Gtk.jl, which has some confusing process management logic. Do you see the same thing with just using Gtk?

Yes, it gives the same behavior with just "using Gtk"

timholy commented 3 years ago

I'm going to close this here, since short of using a different GUI toolkit there's nothing to change in this package.

permanentrance commented 3 years ago

Linking this here: Performance speed up when wrapping script with Juno.@profiler

Running a separate julia REPL with julia -t 1 fixed my issue