Skip to content

Multithreading slow with ImageView #243

@wavecast

Description

@wavecast

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions