Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CairoMakie thread safety #3777

Merged
merged 2 commits into from Apr 10, 2024
Merged

Conversation

jkrumbiegel
Copy link
Collaborator

@jkrumbiegel jkrumbiegel commented Apr 8, 2024

cf #2391 and #812

After I added locks for font handles in JuliaGraphics/FreeTypeAbstraction.jl#87 I also add a few locks around font handling in CairoMakie. With that I don't get crashes anymore for this code:

function threadtest(n)
    fs = [scatter(1:10; axis = (; title = "$i")) for i in 1:n]
    @time Threads.@threads for i in 1:n
        save("temp/scatter_$i.png", fs[i])
    end
    return
end

threadtest(100)

For some reason I sometimes get hangs if I create the plots in threads as well, but that's not an issue from this PR which is only concerned with the rendering part. On an M1 macbook with 10 threads I get 3.109981 seconds (438.09 k allocations: 434.883 MiB, 0.85% gc time) for the example above without threads, and 0.437824 seconds (439.49 k allocations: 435.019 MiB) with threads enabled, which would be a speedup factor of 7.1.

@MakieBot
Copy link
Collaborator

MakieBot commented Apr 8, 2024

Compile Times benchmark

Note, that these numbers may fluctuate on the CI servers, so take them with a grain of salt. All benchmark results are based on the mean time and negative percent mean faster than the base branch. Note, that GLMakie + WGLMakie run on an emulated GPU, so the runtime benchmark is much slower. Results are from running:

using_time = @ctime using Backend
# Compile time
create_time = @ctime fig = scatter(1:4; color=1:4, colormap=:turbo, markersize=20, visible=true)
display_time = @ctime Makie.colorbuffer(display(fig))
# Runtime
create_time = @benchmark fig = scatter(1:4; color=1:4, colormap=:turbo, markersize=20, visible=true)
display_time = @benchmark Makie.colorbuffer(fig)
using create display create display
GLMakie 3.41s (3.37, 3.45) 0.02+- 169.25ms (166.07, 178.52) 4.37+- 476.51ms (472.86, 483.87) 3.84+- 7.50ms (7.27, 7.64) 0.14+- 26.03ms (25.93, 26.13) 0.08+-
master 3.41s (3.38, 3.46) 0.03+- 170.48ms (166.90, 176.70) 3.51+- 479.33ms (471.49, 495.26) 7.85+- 7.51ms (7.42, 7.58) 0.06+- 25.90ms (25.83, 25.96) 0.05+-
evaluation 1.00x invariant, -0.0s (-0.13d, 0.82p, 0.03std) 1.01x invariant, -1.23ms (-0.31d, 0.57p, 3.94std) 1.01x invariant, -2.82ms (-0.46d, 0.42p, 5.84std) 1.00x invariant, -0.01ms (-0.11d, 0.84p, 0.10std) 0.99x slower X, 0.13ms (2.13d, 0.00p, 0.06std)
CairoMakie 2.95s (2.92, 2.99) 0.03+- 163.82ms (161.35, 165.22) 1.33+- 100.60ms (98.17, 103.51) 1.65+- 7.22ms (7.10, 7.55) 0.16+- 889.78μs (885.45, 893.67) 3.10+-
master 2.95s (2.93, 2.99) 0.02+- 164.15ms (162.04, 165.99) 1.37+- 100.23ms (98.55, 102.89) 1.34+- 7.14ms (7.08, 7.21) 0.05+- 889.19μs (883.59, 892.58) 3.48+-
evaluation 1.00x invariant, 0.0s (0.05d, 0.93p, 0.03std) 1.00x invariant, -0.33ms (-0.24d, 0.66p, 1.35std) 1.00x invariant, 0.37ms (0.25d, 0.65p, 1.50std) 0.99x invariant, 0.08ms (0.68d, 0.24p, 0.10std) 1.00x invariant, 0.59μs (0.18d, 0.74p, 3.29std)
WGLMakie 3.57s (3.54, 3.65) 0.04+- 283.34ms (163.86, 371.50) 106.18+- 8.55s (8.42, 8.81) 0.14+- 9.34ms (9.07, 10.44) 0.49+- 68.80ms (67.56, 69.87) 0.94+-
master 3.55s (3.53, 3.58) 0.02+- 366.31ms (363.04, 373.83) 3.79+- 8.46s (8.41, 8.52) 0.05+- 9.15ms (9.12, 9.19) 0.03+- 68.33ms (66.92, 69.60) 0.82+-
evaluation 0.99x invariant, 0.02s (0.68d, 0.23p, 0.03std) 1.29x noisy🤷‍♀️, -82.97ms (-1.10d, 0.08p, 54.98std) 0.99x invariant, 0.09s (0.86d, 0.15p, 0.09std) 0.98x invariant, 0.19ms (0.54d, 0.35p, 0.26std) 0.99x invariant, 0.47ms (0.53d, 0.34p, 0.88std)

@jkrumbiegel jkrumbiegel merged commit d47671d into master Apr 10, 2024
18 checks passed
@jkrumbiegel jkrumbiegel deleted the jk/cairomakie-thread-safety branch April 10, 2024 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants