|
| 1 | +# Use Case: Parallel Nested Loops |
| 2 | + |
| 3 | +One of the many applications of Dagger is that it can be used as a drop-in |
| 4 | +replacement for nested multi-threaded loops that would otherwise be written |
| 5 | +with `Threads.@threads`. |
| 6 | + |
| 7 | +Consider a simplified scenario where you want to calculate the maximum mean |
| 8 | +values of random samples of various lengths that have been generated by several |
| 9 | +distributions provided by the Distributions.jl package. The results should be |
| 10 | +collected into a DataFrame. We have the following function: |
| 11 | + |
| 12 | +```julia |
| 13 | +using Dagger, Random, Distributions, StatsBase, DataFrames |
| 14 | + |
| 15 | +function f(dist, len, reps, σ) |
| 16 | + v = Vector{Float64}(undef, len) # avoiding allocations |
| 17 | + maximum(mean(rand!(dist, v)) for _ in 1:reps)/σ |
| 18 | +end |
| 19 | +``` |
| 20 | + |
| 21 | +Let us consider the following probability distributions for numerical |
| 22 | +experiments, all of which have expected values equal to zero, and the following |
| 23 | +lengths of vectors: |
| 24 | + |
| 25 | +```julia |
| 26 | +dists = [Cosine, Epanechnikov, Laplace, Logistic, Normal, NormalCanon, PGeneralizedGaussian, SkewNormal, SkewedExponentialPower, SymTriangularDist] |
| 27 | +lens = [10, 20, 50, 100, 200, 500] |
| 28 | +``` |
| 29 | + |
| 30 | +Using `Threads.@threads` those experiments could be parallelized as: |
| 31 | + |
| 32 | +```julia |
| 33 | +function experiments_threads(dists, lens, K=1000) |
| 34 | + res = DataFrame() |
| 35 | + lck = ReentrantLock() |
| 36 | + Threads.@threads for T in dists |
| 37 | + dist = T() |
| 38 | + σ = std(dist) |
| 39 | + for L in lens |
| 40 | + z = f(dist, L, K, σ) |
| 41 | + Threads.lock(lck) do |
| 42 | + push!(res, (;T, σ, L, z)) |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + res |
| 47 | +end |
| 48 | +``` |
| 49 | + |
| 50 | +Note that `DataFrames.push!` is not a thread safe operation and hence we need |
| 51 | +to utilize a locking mechanism in order to avoid two threads appending the |
| 52 | +DataFrame at the same time. |
| 53 | + |
| 54 | +The same code could be rewritten in Dagger as: |
| 55 | + |
| 56 | +```julia |
| 57 | +function experiments_dagger(dists, lens, K=1000) |
| 58 | + res = DataFrame() |
| 59 | + @sync for T in dists |
| 60 | + dist = T() |
| 61 | + σ = Dagger.@spawn std(dist) |
| 62 | + for L in lens |
| 63 | + z = Dagger.@spawn f(dist, L, K, σ) |
| 64 | + push!(res, (;T, σ, L, z)) |
| 65 | + end |
| 66 | + end |
| 67 | + res.z = fetch.(res.z) |
| 68 | + res.σ = fetch.(res.σ) |
| 69 | + res |
| 70 | +end |
| 71 | +``` |
| 72 | + |
| 73 | +In this code we have job interdependence. Firstly, we are calculating the |
| 74 | +standard deviation `σ` and than we are using that value in the function `f`. |
| 75 | +Since `Dagger.@spawn` yields an `EagerThunk` rather than actual values, we need |
| 76 | +to use the `fetch` function to obtain those values. In this example, the value |
| 77 | +fetching is perfomed once all computations are completed (note that `@sync` |
| 78 | +preceding the loop forces the loop to wait for all jobs to complete). Also, |
| 79 | +note that contrary to the previous example, we do not need to implement locking |
| 80 | +as we are just pushing the `EagerThunk` results of `Dagger.@spawn` serially |
| 81 | +into the DataFrame (which is fast since `Dagger.@spawn` doesn't block). |
| 82 | + |
| 83 | +The above use case scenario has been tested by running `julia -t 8` (or with |
| 84 | +`JULIA_NUM_THREADS=8` as environment variable). The `Threads.@threads` code |
| 85 | +takes 1.8 seconds to run, while the Dagger code, which is also one line |
| 86 | +shorter, runs around 0.9 seconds, resulting in a 2x speedup. |
0 commit comments