JuliaImages / DitherPunk.jl

Dithering algorithms in Julia.
MIT License
60 stars 3 forks source link

Add multithreading to ordered dithering #96

Closed adrhill closed 1 year ago

adrhill commented 2 years ago

@johnnychen94 is there a specific order Threads.@threads, @inbounds and @views are best called in?

codecov[bot] commented 2 years ago

Codecov Report

Merging #96 (f663b45) into master (d723bd1) will decrease coverage by 0.37%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #96      +/-   ##
==========================================
- Coverage   97.95%   97.57%   -0.38%     
==========================================
  Files          12       12              
  Lines         293      289       -4     
==========================================
- Hits          287      282       -5     
- Misses          6        7       +1     
Impacted Files Coverage Δ
src/ordered.jl 96.61% <100.00%> (-1.73%) :arrow_down:
src/braille.jl 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

adrhill commented 2 years ago

Looks like benchmarks are broken on master, so here are some local benchmarks:

Master v3.0.3

julia> using DitherPunk, TestImages, ColorSchemes

julia> img = rand(Gray, 1000, 1000);

julia> imc = testimage("fabio_color_256");

julia> alg = Bayer(3);

julia> cs = ColorSchemes.jet;

julia> @benchmark dither($img, $alg)

BenchmarkTools.Trial: 2574 samples with 1 evaluation.
 Range (min … max):  745.018 μs … 17.749 ms  ┊ GC (min … max):  0.00% … 91.93%
 Time  (median):       1.203 ms              ┊ GC (median):     0.00%
 Time  (mean ± σ):     1.942 ms ±  2.794 ms  ┊ GC (mean ± σ):  38.72% ± 22.87%

  ▂▇█                                                           
  ████▄▄▁▃▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▃▅▅▅▅▅▄▆▆▇▆▇▅▆▇▆▇▆▆▇▅▅▆▄▄▅ █
  745 μs        Histogram: log(frequency) by time        14 ms <

 Memory estimate: 7.65 MiB, allocs estimate: 5.

julia> @benchmark dither($imc, $alg, $cs)
BenchmarkTools.Trial: 8 samples with 1 evaluation.
 Range (min … max):  622.878 ms … 683.338 ms  ┊ GC (min … max): 2.68% … 3.48%
 Time  (median):     639.255 ms               ┊ GC (median):    4.63%
 Time  (mean ± σ):   642.701 ms ±  20.433 ms  ┊ GC (mean ± σ):  4.45% ± 1.52%

  ███            █ █            ██                            █  
  ███▁▁▁▁▁▁▁▁▁▁▁▁█▁█▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█ ▁
  623 ms           Histogram: frequency by time          683 ms <

 Memory estimate: 155.58 MiB, allocs estimate: 331455.

This PR

julia> Threads.nthreads()
8

ulia> @benchmark dither($img, $alg)
BenchmarkTools.Trial: 2403 samples with 1 evaluation.
 Range (min … max):  666.558 μs … 29.729 ms  ┊ GC (min … max):  0.00% … 97.15%
 Time  (median):     756.130 μs              ┊ GC (median):     0.00%
 Time  (mean ± σ):     2.071 ms ±  4.782 ms  ┊ GC (mean ± σ):  59.81% ± 23.67%

  █▄                                                            
  ███▅▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▃▁▃▃▄▄▅▃▅▄▆▆▁▆▅▅▆▆▆▅▆▆▆▆▅▅▅▆▅▅▅▁▆ █
  667 μs        Histogram: log(frequency) by time        24 ms <

 Memory estimate: 7.65 MiB, allocs estimate: 54.

julia> @benchmark dither($imc, $alg, $cs)
BenchmarkTools.Trial: 26 samples with 1 evaluation.
 Range (min … max):  103.469 ms … 423.564 ms  ┊ GC (min … max):  0.00% … 69.35%
 Time  (median):     118.208 ms               ┊ GC (median):     0.00%
 Time  (mean ± σ):   195.786 ms ± 115.151 ms  ┊ GC (mean ± σ):  41.62% ± 32.75%

  ▃▁█▁                                                           
  ████▁▁▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▄▄▄▁▁▄▁▁▄▁▇▁▁▁▁▁▁▁▁▁▄ ▁
  103 ms           Histogram: frequency by time          424 ms <
adrhill commented 2 years ago

Looks like the performance increase on grayscale images isn't as clear-cut as I thought when compared to the previously used @simd.

adrhill commented 1 year ago

Closing this since performance increases weren't clear-cut in the first place and @simd seems to be the better option.