JuliaPlots / InspectDR.jl

Fast, interactive Julia/GTK+ plots (+Smith charts +Gtk widget +Cairo-only images)
MIT License
68 stars 9 forks source link

Inspectdr initialization trashes Threads performance #27

Closed mattcbro closed 2 years ago

mattcbro commented 3 years ago

Initializing with the inspectdr() command causes threaded code to run up to 7000 times slower. Here is the test code from issue https://github.com/JuliaLang/julia/issues/40535

# test simple thread idea
using Plots
# uncommenting inspectdr() causes the threaded version of genprojmat to run up to 7000 times slower
#inspectdr()
using LinearAlgebra
using BenchmarkTools

#using QThread
##
""" A matlab version of Julias QR decomposition.
        Q,R = flatqr(X) """
function flatqr(X)
     F = qr(X)
     return(Matrix(F.Q), F.R)
end

""" Circularly symmetric Complex noise """
function cgauss(varargin...)

   Z = (1 ./ sqrt(2.)).* (randn(varargin)+im.*randn(varargin))
end

""" complex zeros functions since I forget how to write the type signatures """
function czeros(x...)
    y = zeros(Complex{Float64}, x) ;
    return(y)
end # function czeros

""" Generate all the projection matrices.   Use threaded loop to exploit embarassing parallel problem """
function genprojmat(xdata)
    Mants, Nf, Ns = size(xdata)
    Qall = czeros(Ns, Mants, Nf)
    Threads.@threads for k = 1:Nf
        Qx, Rx = flatqr(xdata[:,k,:]')
        Qall[:, :, k] = Qx
    end
    return(Qall)
end

""" Generate all the projection matrices.  Non threaded case  is much faster.  Why? """
function genprojmatnt(xdata)
    Mants, Nf, Ns = size(xdata)
    Qall = czeros(Ns, Mants, Nf)
    for k = 1:Nf
        Qx, Rx = flatqr(xdata[:,k,:]')
        Qall[:, :, k] = Qx
    end
    return(Qall)
end
##

# Set up the input data
Mants =4
Nf = 24
nsgn = 0.1
apr = cgauss(4)
chan = cgauss(Nf)
ac = apr * transpose(chan)

#    Mants, Nf, Ns = size(xdata)
Ns = 1024
K = 3
xdata = nsgn .* cgauss(Mants, Nf, Ns)
st = randn(Ns)
for q=1:Ns
    xdata[:,:,q] = xdata[:,:,q] + ac .* st[q]
end

# threaded version
@btime genprojmat(xdata) ;

# not threaded version
@btime genprojmatnt(xdata) ;
ma-laforge commented 3 years ago

Hi @mattcbro,

I don't have time to look into this right now. Maybe next week.

For what it's worth:

I'm guessing this has something to do with either Gtk.jl or one of Plots.jl's dependencies - simply because I doubt InspectDR itself does much to cause side effects regarding threads - especially if all you do is load up the code (not actually using the plot yet).

That being said, I am kind of interested in eventually adding threading support to InspectDR, so investigating this bug might help me at least get familiar with the basic threading API.

mattcbro commented 3 years ago

It's definitely real. I got bit by it again running a simulation. It was taking an eternity to run and then I remembered I was using inspectdr(). Had to kill the process and restart it. After that it ran in about 5 seconds.

I'm pretty busy myself right now so I understand the delay. Whenever you get a chance to look at this is fine by me. The interaction is quite strange. Of course the julia people are saying it's inspectdr's fault LOL.

Oh there is this bug in Gtk that was cited as well, so you may be right about the Gtk thing. https://github.com/JuliaGraphics/Gtk.jl/issues/503

ufechner7 commented 2 years ago

The linked bug in Gtk was closed. Shouldn't this issue be also closed as resolved in Gtk then?

ma-laforge commented 2 years ago

Agreed. Most likely was a Gtk bug. I completely forgot about this issue.

Someone can re-open if this is still a problem.