JuliaParallel / Dagger.jl

A framework for out-of-core and parallel execution
Other
638 stars 67 forks source link

multiple threads not work #439

Closed kongdd closed 12 months ago

kongdd commented 1 year ago

I am a begining user of Dagger.jl. I found this example at https://juliaparallel.org/Dagger.jl/dev/use-cases/parallel-nested-loops/. But in my test, multiple threads not work for Dagger.jl. Any suggestion?

using Dagger, Random, Distributions, StatsBase, DataFrames

function f(dist, len, reps, σ)
  v = Vector{Float64}(undef, len) # avoiding allocations
  maximum(mean(rand!(dist, v)) for _ in 1:reps) / σ
end

function experiments_threads(dists, lens, K=1000)
  res = DataFrame()
  lck = ReentrantLock()
  Threads.@threads for T in dists
    dist = T()
    σ = std(dist)
    for L in lens
      z = f(dist, L, K, σ)
      Threads.lock(lck) do
        push!(res, (; T, σ, L, z))
      end
    end
  end
  res
end

function experiments_dagger(dists, lens, K=1000)
  res = DataFrame()
  @sync for T in dists
    dist = T()
    σ = Dagger.@spawn std(dist)
    for L in lens
      z = Dagger.@spawn f(dist, L, K, σ)
      push!(res, (; T, σ, L, z))
    end
  end
  res.z = fetch.(res.z)
  res.σ = fetch.(res.σ)
  res
end

dists = [Cosine, Epanechnikov, Laplace, Logistic, Normal, NormalCanon, PGeneralizedGaussian, SkewNormal, SkewedExponentialPower, SymTriangularDist]
lens = [10, 20, 50, 100, 200, 500]

@time experiments_threads(dists, lens)
# julia -t 8 dagger_01.jl
# 4.329949 seconds (2.12 M allocations: 143.102 MiB, 292.15% compilation time)
@time experiments_dagger(dists, lens)
# julia -t 8 dagger_01.jl
# 34.630007 seconds (27.31 M allocations: 1.770 GiB, 2.67% gc time, 199.54% compilation time)

Threads

4.329949 seconds (2.12 M allocations: 143.102 MiB, 292.15% compilation time)

Dagger

34.630007 seconds (27.31 M allocations: 1.770 GiB, 2.67% gc time, 199.54% compilation time)

jpsamaroo commented 1 year ago

Note that just running @time ... once is an invalid way to benchmark Julia code, as you're including compile time (see the "292.15% compilation time" for example). You'll want to re-run that call at least once more in your Julia session to get the timing without compilation.

jpsamaroo commented 12 months ago

Closing as this is not a bug in Dagger